blob: 0ad2f3f7da85a029b5dea7dd3ce67b69d4ff8605 [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>
Russell King503f9aa92018-01-02 17:24:44 +000031#include <linux/phylink.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080032#include <linux/platform_device.h>
33#include <linux/skbuff.h>
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +010034#include <net/hwbm.h>
Marcin Wojtasdc35a102016-03-14 09:39:03 +010035#include "mvneta_bm.h"
Jisheng Zhang0e03f562016-01-20 19:27:22 +080036#include <net/ip.h>
37#include <net/ipv6.h>
38#include <net/tso.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030039
40/* Registers */
41#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
Marcin Wojtase5bdf682015-11-30 13:27:42 +010042#define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +010043#define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
44#define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
45#define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
46#define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
Thomas Petazzonic5aff182012-08-17 14:04:28 +030047#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
48#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
49#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
50#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
51#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
52#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
53#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
54#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
55#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
56#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
57#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
58#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
59#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
Marcin Wojtasdc35a102016-03-14 09:39:03 +010060#define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
61#define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
62#define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
Thomas Petazzonic5aff182012-08-17 14:04:28 +030063#define MVNETA_PORT_RX_RESET 0x1cc0
64#define MVNETA_PORT_RX_DMA_RESET BIT(0)
65#define MVNETA_PHY_ADDR 0x2000
66#define MVNETA_PHY_ADDR_MASK 0x1f
67#define MVNETA_MBUS_RETRY 0x2010
68#define MVNETA_UNIT_INTR_CAUSE 0x2080
69#define MVNETA_UNIT_CONTROL 0x20B0
70#define MVNETA_PHY_POLLING_ENABLE BIT(1)
71#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
72#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
73#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
74#define MVNETA_BASE_ADDR_ENABLE 0x2290
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +010075#define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
Thomas Petazzonic5aff182012-08-17 14:04:28 +030076#define MVNETA_PORT_CONFIG 0x2400
77#define MVNETA_UNI_PROMISC_MODE BIT(0)
78#define MVNETA_DEF_RXQ(q) ((q) << 1)
79#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
80#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
81#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
82#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
83#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
84#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
85#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
86 MVNETA_DEF_RXQ_ARP(q) | \
87 MVNETA_DEF_RXQ_TCP(q) | \
88 MVNETA_DEF_RXQ_UDP(q) | \
89 MVNETA_DEF_RXQ_BPDU(q) | \
90 MVNETA_TX_UNSET_ERR_SUM | \
91 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92#define MVNETA_PORT_CONFIG_EXTEND 0x2404
93#define MVNETA_MAC_ADDR_LOW 0x2414
94#define MVNETA_MAC_ADDR_HIGH 0x2418
95#define MVNETA_SDMA_CONFIG 0x241c
96#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030097#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
98#define MVNETA_RX_NO_DATA_SWAP BIT(4)
99#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200100#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300101#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
102#define MVNETA_PORT_STATUS 0x2444
103#define MVNETA_TX_IN_PRGRS BIT(1)
104#define MVNETA_TX_FIFO_EMPTY BIT(8)
105#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200106#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200107#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200108#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300109#define MVNETA_TYPE_PRIO 0x24bc
110#define MVNETA_FORCE_UNI BIT(21)
111#define MVNETA_TXQ_CMD_1 0x24e4
112#define MVNETA_TXQ_CMD 0x2448
113#define MVNETA_TXQ_DISABLE_SHIFT 8
114#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Andrew Lunne4839112015-10-22 18:37:36 +0100115#define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
116#define MVNETA_OVERRUN_FRAME_COUNT 0x2488
Stas Sergeev898b29702015-04-01 20:32:49 +0300117#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
118#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300119#define MVNETA_ACC_MODE 0x2500
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100120#define MVNETA_BM_ADDRESS 0x2504
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300121#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
122#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
123#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100124#define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100125#define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300126#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100127
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100128/* Exception Interrupt Port/Queue Cause register
129 *
130 * Their behavior depend of the mapping done using the PCPX2Q
131 * registers. For a given CPU if the bit associated to a queue is not
132 * set, then for the register a read from this CPU will always return
133 * 0 and a write won't do anything
134 */
willy tarreau40ba35e2014-01-16 08:20:10 +0100135
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300136#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300137#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100138
139/* bits 0..7 = TXQ SENT, one bit per queue.
140 * bits 8..15 = RXQ OCCUP, one bit per queue.
141 * bits 16..23 = RXQ FREE, one bit per queue.
142 * bit 29 = OLD_REG_SUM, see old reg ?
143 * bit 30 = TX_ERR_SUM, one bit for 4 ports
144 * bit 31 = MISC_SUM, one bit for 4 ports
145 */
146#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
147#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
148#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
149#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b29702015-04-01 20:32:49 +0300150#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100151
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300152#define MVNETA_INTR_OLD_CAUSE 0x25a8
153#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100154
155/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300156#define MVNETA_INTR_MISC_CAUSE 0x25b0
157#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100158
159#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
160#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
161#define MVNETA_CAUSE_PTP BIT(4)
162
163#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
164#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
165#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
166#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
167#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
168#define MVNETA_CAUSE_PRBS_ERR BIT(12)
169#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
170#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
171
172#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
173#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
174#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
175
176#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
177#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
178#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
179
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300180#define MVNETA_INTR_ENABLE 0x25b8
181#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
Marcin Wojtasdc1aadf2015-11-30 13:27:43 +0100182#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
willy tarreau40ba35e2014-01-16 08:20:10 +0100183
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300184#define MVNETA_RXQ_CMD 0x2680
185#define MVNETA_RXQ_DISABLE_SHIFT 8
186#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
187#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
188#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
189#define MVNETA_GMAC_CTRL_0 0x2c00
190#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
191#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
Russell King22f4bf82018-01-02 17:24:54 +0000192#define MVNETA_GMAC0_PORT_1000BASE_X BIT(1)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300193#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
194#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b29702015-04-01 20:32:49 +0300195#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100196#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300197#define MVNETA_GMAC2_PORT_RGMII BIT(4)
198#define MVNETA_GMAC2_PORT_RESET BIT(6)
199#define MVNETA_GMAC_STATUS 0x2c10
200#define MVNETA_GMAC_LINK_UP BIT(0)
201#define MVNETA_GMAC_SPEED_1000 BIT(1)
202#define MVNETA_GMAC_SPEED_100 BIT(2)
203#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
204#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
205#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
206#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
207#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
Russell King503f9aa92018-01-02 17:24:44 +0000208#define MVNETA_GMAC_AN_COMPLETE BIT(11)
209#define MVNETA_GMAC_SYNC_OK BIT(14)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300210#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
211#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
212#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b29702015-04-01 20:32:49 +0300213#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Russell King22f4bf82018-01-02 17:24:54 +0000214#define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3)
215#define MVNETA_GMAC_INBAND_RESTART_AN BIT(4)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300216#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
217#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200218#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Russell King22f4bf82018-01-02 17:24:54 +0000219#define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8)
220#define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9)
Stas Sergeev898b29702015-04-01 20:32:49 +0300221#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300222#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200223#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Andrew Lunne4839112015-10-22 18:37:36 +0100224#define MVNETA_MIB_COUNTERS_BASE 0x3000
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300225#define MVNETA_MIB_LATE_COLLISION 0x7c
226#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
227#define MVNETA_DA_FILT_OTH_MCAST 0x3500
228#define MVNETA_DA_FILT_UCAST_BASE 0x3600
229#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
230#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
231#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
232#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
233#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
234#define MVNETA_TXQ_DEC_SENT_SHIFT 16
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100235#define MVNETA_TXQ_DEC_SENT_MASK 0xff
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300236#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
237#define MVNETA_TXQ_SENT_DESC_SHIFT 16
238#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
239#define MVNETA_PORT_TX_RESET 0x3cf0
240#define MVNETA_PORT_TX_DMA_RESET BIT(0)
241#define MVNETA_TX_MTU 0x3e0c
242#define MVNETA_TX_TOKEN_SIZE 0x3e14
243#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
244#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
245#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
246
Russell King6d81f452018-01-02 17:25:04 +0000247#define MVNETA_LPI_CTRL_0 0x2cc0
248#define MVNETA_LPI_CTRL_1 0x2cc4
249#define MVNETA_LPI_REQUEST_ENABLE BIT(0)
250#define MVNETA_LPI_CTRL_2 0x2cc8
251#define MVNETA_LPI_STATUS 0x2ccc
252
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300253#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
254
255/* Descriptor ring Macros */
256#define MVNETA_QUEUE_NEXT_DESC(q, index) \
257 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
258
259/* Various constants */
260
261/* Coalescing */
Dmitri Epshtein06708f82016-07-06 04:18:58 +0200262#define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300263#define MVNETA_RX_COAL_PKTS 32
264#define MVNETA_RX_COAL_USEC 100
265
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100266/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300267 * by Marvell switches when a specific hardware mode is enabled (not
268 * supported by this driver) or is filled automatically by zeroes on
269 * the RX side. Those two bytes being at the front of the Ethernet
270 * header, they allow to have the IP header aligned on a 4 bytes
271 * boundary automatically: the hardware skips those two bytes on its
272 * own.
273 */
274#define MVNETA_MH_SIZE 2
275
276#define MVNETA_VLAN_TAG_LEN 4
277
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100278#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300279#define MVNETA_TX_CSUM_MAX_SIZE 9800
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100280#define MVNETA_ACC_MODE_EXT1 1
281#define MVNETA_ACC_MODE_EXT2 2
282
283#define MVNETA_MAX_DECODE_WIN 6
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300284
285/* Timeout constants */
286#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
287#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
288#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
289
290#define MVNETA_TX_MTU_MAX 0x3ffff
291
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100292/* The RSS lookup table actually has 256 entries but we do not use
293 * them yet
294 */
295#define MVNETA_RSS_LU_TABLE_SIZE 1
296
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300297/* Max number of Rx descriptors */
298#define MVNETA_MAX_RXD 128
299
300/* Max number of Tx descriptors */
301#define MVNETA_MAX_TXD 532
302
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300303/* Max number of allowed TCP segments for software TSO */
304#define MVNETA_MAX_TSO_SEGS 100
305
306#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
307
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300308/* descriptor aligned size */
309#define MVNETA_DESC_ALIGNED_SIZE 32
310
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100311/* Number of bytes to be taken into account by HW when putting incoming data
312 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
313 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
314 */
315#define MVNETA_RX_PKT_OFFSET_CORRECTION 64
316
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300317#define MVNETA_RX_PKT_SIZE(mtu) \
318 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
319 ETH_HLEN + ETH_FCS_LEN, \
Jisheng Zhangc66e98c2016-04-01 17:12:49 +0800320 cache_line_size())
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300321
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300322#define IS_TSO_HEADER(txq, addr) \
323 ((addr >= txq->tso_hdrs_phys) && \
324 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
325
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100326#define MVNETA_RX_GET_BM_POOL_ID(rxd) \
327 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300328
Russell King6d81f452018-01-02 17:25:04 +0000329enum {
330 ETHTOOL_STAT_EEE_WAKEUP,
331 ETHTOOL_MAX_STATS,
332};
333
Russell King9b0cdef2015-10-22 18:37:30 +0100334struct mvneta_statistic {
335 unsigned short offset;
336 unsigned short type;
337 const char name[ETH_GSTRING_LEN];
338};
339
340#define T_REG_32 32
341#define T_REG_64 64
Russell King6d81f452018-01-02 17:25:04 +0000342#define T_SW 1
Russell King9b0cdef2015-10-22 18:37:30 +0100343
344static const struct mvneta_statistic mvneta_statistics[] = {
345 { 0x3000, T_REG_64, "good_octets_received", },
346 { 0x3010, T_REG_32, "good_frames_received", },
347 { 0x3008, T_REG_32, "bad_octets_received", },
348 { 0x3014, T_REG_32, "bad_frames_received", },
349 { 0x3018, T_REG_32, "broadcast_frames_received", },
350 { 0x301c, T_REG_32, "multicast_frames_received", },
351 { 0x3050, T_REG_32, "unrec_mac_control_received", },
352 { 0x3058, T_REG_32, "good_fc_received", },
353 { 0x305c, T_REG_32, "bad_fc_received", },
354 { 0x3060, T_REG_32, "undersize_received", },
355 { 0x3064, T_REG_32, "fragments_received", },
356 { 0x3068, T_REG_32, "oversize_received", },
357 { 0x306c, T_REG_32, "jabber_received", },
358 { 0x3070, T_REG_32, "mac_receive_error", },
359 { 0x3074, T_REG_32, "bad_crc_event", },
360 { 0x3078, T_REG_32, "collision", },
361 { 0x307c, T_REG_32, "late_collision", },
362 { 0x2484, T_REG_32, "rx_discard", },
363 { 0x2488, T_REG_32, "rx_overrun", },
364 { 0x3020, T_REG_32, "frames_64_octets", },
365 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
366 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
367 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
368 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
369 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
370 { 0x3038, T_REG_64, "good_octets_sent", },
371 { 0x3040, T_REG_32, "good_frames_sent", },
372 { 0x3044, T_REG_32, "excessive_collision", },
373 { 0x3048, T_REG_32, "multicast_frames_sent", },
374 { 0x304c, T_REG_32, "broadcast_frames_sent", },
375 { 0x3054, T_REG_32, "fc_sent", },
376 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
Russell King6d81f452018-01-02 17:25:04 +0000377 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
Russell King9b0cdef2015-10-22 18:37:30 +0100378};
379
willy tarreau74c41b02014-01-16 08:20:08 +0100380struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300381 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100382 u64 rx_packets;
383 u64 rx_bytes;
384 u64 tx_packets;
385 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300386};
387
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200388struct mvneta_pcpu_port {
389 /* Pointer to the shared port */
390 struct mvneta_port *pp;
391
392 /* Pointer to the CPU-local NAPI struct */
393 struct napi_struct napi;
394
395 /* Cause of the previous interrupt */
396 u32 cause_rx_tx;
397};
398
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300399struct mvneta_port {
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100400 u8 id;
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200401 struct mvneta_pcpu_port __percpu *ports;
402 struct mvneta_pcpu_stats __percpu *stats;
403
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300404 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100405 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300406 void __iomem *base;
407 struct mvneta_rx_queue *rxqs;
408 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300409 struct net_device *dev;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200410 struct hlist_node node_online;
411 struct hlist_node node_dead;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100412 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100413 /* Protect the access to the percpu interrupt registers,
414 * ensuring that the configuration remains coherent.
415 */
416 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100417 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300418
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100419 u32 cause_rx_tx;
420 struct napi_struct napi;
421
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300422 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100423 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800424 /* AXI clock */
425 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300426 u8 mcast_count[256];
427 u16 tx_ring_size;
428 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300429
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300430 phy_interface_t phy_interface;
Russell King503f9aa92018-01-02 17:24:44 +0000431 struct device_node *dn;
Simon Guinotb65657f2015-06-30 16:20:22 +0200432 unsigned int tx_csum_limit;
Russell King503f9aa92018-01-02 17:24:44 +0000433 struct phylink *phylink;
Russell King9b0cdef2015-10-22 18:37:30 +0100434
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100435 struct mvneta_bm *bm_priv;
436 struct mvneta_bm_pool *pool_long;
437 struct mvneta_bm_pool *pool_short;
438 int bm_win_id;
439
Russell King6d81f452018-01-02 17:25:04 +0000440 bool eee_enabled;
441 bool eee_active;
442 bool tx_lpi_enabled;
443
Russell King9b0cdef2015-10-22 18:37:30 +0100444 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100445
446 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100447
448 /* Flags for special SoC configurations */
449 bool neta_armada3700;
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100450 u16 rx_offset_correction;
Jane Li9768b452017-03-16 16:22:28 +0800451 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300452};
453
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100454/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300455 * layout of the transmit and reception DMA descriptors, and their
456 * layout is therefore defined by the hardware design
457 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200458
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300459#define MVNETA_TX_L3_OFF_SHIFT 0
460#define MVNETA_TX_IP_HLEN_SHIFT 8
461#define MVNETA_TX_L4_UDP BIT(16)
462#define MVNETA_TX_L3_IP6 BIT(17)
463#define MVNETA_TXD_IP_CSUM BIT(18)
464#define MVNETA_TXD_Z_PAD BIT(19)
465#define MVNETA_TXD_L_DESC BIT(20)
466#define MVNETA_TXD_F_DESC BIT(21)
467#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
468 MVNETA_TXD_L_DESC | \
469 MVNETA_TXD_F_DESC)
470#define MVNETA_TX_L4_CSUM_FULL BIT(30)
471#define MVNETA_TX_L4_CSUM_NOT BIT(31)
472
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300473#define MVNETA_RXD_ERR_CRC 0x0
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100474#define MVNETA_RXD_BM_POOL_SHIFT 13
475#define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300476#define MVNETA_RXD_ERR_SUMMARY BIT(16)
477#define MVNETA_RXD_ERR_OVERRUN BIT(17)
478#define MVNETA_RXD_ERR_LEN BIT(18)
479#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
480#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
481#define MVNETA_RXD_L3_IP4 BIT(25)
482#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
483#define MVNETA_RXD_L4_CSUM_OK BIT(30)
484
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200485#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200486struct mvneta_tx_desc {
487 u32 command; /* Options used by HW for packet transmitting.*/
488 u16 reserverd1; /* csum_l4 (for future use) */
489 u16 data_size; /* Data size of transmitted packet in bytes */
490 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
491 u32 reserved2; /* hw_cmd - (for future use, PMT) */
492 u32 reserved3[4]; /* Reserved - (for future use) */
493};
494
495struct mvneta_rx_desc {
496 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300497 u16 reserved1; /* pnc_info - (for future use, PnC) */
498 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200499
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300500 u32 buf_phys_addr; /* Physical address of the buffer */
501 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200502
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300503 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
504 u16 reserved3; /* prefetch_cmd, for future use */
505 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200506
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300507 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
508 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
509};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200510#else
511struct mvneta_tx_desc {
512 u16 data_size; /* Data size of transmitted packet in bytes */
513 u16 reserverd1; /* csum_l4 (for future use) */
514 u32 command; /* Options used by HW for packet transmitting.*/
515 u32 reserved2; /* hw_cmd - (for future use, PMT) */
516 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
517 u32 reserved3[4]; /* Reserved - (for future use) */
518};
519
520struct mvneta_rx_desc {
521 u16 data_size; /* Size of received packet in bytes */
522 u16 reserved1; /* pnc_info - (for future use, PnC) */
523 u32 status; /* Info about received packet */
524
525 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
526 u32 buf_phys_addr; /* Physical address of the buffer */
527
528 u16 reserved4; /* csum_l4 - (for future use, PnC) */
529 u16 reserved3; /* prefetch_cmd, for future use */
530 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
531
532 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
533 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
534};
535#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300536
537struct mvneta_tx_queue {
538 /* Number of this TX queue, in the range 0-7 */
539 u8 id;
540
541 /* Number of TX DMA descriptors in the descriptor ring */
542 int size;
543
544 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100545 * descriptor ring
546 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300547 int count;
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100548 int pending;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300549 int tx_stop_threshold;
550 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300551
552 /* Array of transmitted skb */
553 struct sk_buff **tx_skb;
554
555 /* Index of last TX DMA descriptor that was inserted */
556 int txq_put_index;
557
558 /* Index of the TX DMA descriptor to be cleaned up */
559 int txq_get_index;
560
561 u32 done_pkts_coal;
562
563 /* Virtual address of the TX DMA descriptors array */
564 struct mvneta_tx_desc *descs;
565
566 /* DMA address of the TX DMA descriptors array */
567 dma_addr_t descs_phys;
568
569 /* Index of the last TX DMA descriptor */
570 int last_desc;
571
572 /* Index of the next TX DMA descriptor to process */
573 int next_desc_to_proc;
Ezequiel Garcia2adb7192014-05-19 13:59:55 -0300574
575 /* DMA buffers for TSO headers */
576 char *tso_hdrs;
577
578 /* DMA address of TSO headers */
579 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100580
581 /* Affinity mask for CPUs*/
582 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300583};
584
585struct mvneta_rx_queue {
586 /* rx queue number, in the range 0-7 */
587 u8 id;
588
589 /* num of rx descriptors in the rx descriptor ring */
590 int size;
591
592 /* counter of times when mvneta_refill() failed */
593 int missed;
594
595 u32 pkts_coal;
596 u32 time_coal;
597
Gregory CLEMENTf88bee12016-12-01 18:03:06 +0100598 /* Virtual address of the RX buffer */
599 void **buf_virt_addr;
600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300601 /* Virtual address of the RX DMA descriptors array */
602 struct mvneta_rx_desc *descs;
603
604 /* DMA address of the RX DMA descriptors array */
605 dma_addr_t descs_phys;
606
607 /* Index of the last RX DMA descriptor */
608 int last_desc;
609
610 /* Index of the next RX DMA descriptor to process */
611 int next_desc_to_proc;
612};
613
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200614static enum cpuhp_state online_hpstate;
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300615/* The hardware supports eight (8) rx queues, but we are only allowing
616 * the first one to be used. Therefore, let's just allocate one queue.
617 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200618static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300619static int txq_number = 8;
620
621static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300622
willy tarreauf19fadf2014-01-16 08:20:17 +0100623static int rx_copybreak __read_mostly = 256;
624
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100625/* HW BM need that each port be identify by a unique ID */
626static int global_port_id;
627
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300628#define MVNETA_DRIVER_NAME "mvneta"
629#define MVNETA_DRIVER_VERSION "1.0"
630
631/* Utility/helper methods */
632
633/* Write helper method */
634static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
635{
636 writel(data, pp->base + offset);
637}
638
639/* Read helper method */
640static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
641{
642 return readl(pp->base + offset);
643}
644
645/* Increment txq get counter */
646static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
647{
648 txq->txq_get_index++;
649 if (txq->txq_get_index == txq->size)
650 txq->txq_get_index = 0;
651}
652
653/* Increment txq put counter */
654static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
655{
656 txq->txq_put_index++;
657 if (txq->txq_put_index == txq->size)
658 txq->txq_put_index = 0;
659}
660
661
662/* Clear all MIB counters */
663static void mvneta_mib_counters_clear(struct mvneta_port *pp)
664{
665 int i;
666 u32 dummy;
667
668 /* Perform dummy reads from MIB counters */
669 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
670 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100671 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
672 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300673}
674
675/* Get System Network Statistics */
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800676static void
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +0800677mvneta_get_stats64(struct net_device *dev,
678 struct rtnl_link_stats64 *stats)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300679{
680 struct mvneta_port *pp = netdev_priv(dev);
681 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100682 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300683
willy tarreau74c41b02014-01-16 08:20:08 +0100684 for_each_possible_cpu(cpu) {
685 struct mvneta_pcpu_stats *cpu_stats;
686 u64 rx_packets;
687 u64 rx_bytes;
688 u64 tx_packets;
689 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300690
willy tarreau74c41b02014-01-16 08:20:08 +0100691 cpu_stats = per_cpu_ptr(pp->stats, cpu);
692 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700693 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100694 rx_packets = cpu_stats->rx_packets;
695 rx_bytes = cpu_stats->rx_bytes;
696 tx_packets = cpu_stats->tx_packets;
697 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700698 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300699
willy tarreau74c41b02014-01-16 08:20:08 +0100700 stats->rx_packets += rx_packets;
701 stats->rx_bytes += rx_bytes;
702 stats->tx_packets += tx_packets;
703 stats->tx_bytes += tx_bytes;
704 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300705
706 stats->rx_errors = dev->stats.rx_errors;
707 stats->rx_dropped = dev->stats.rx_dropped;
708
709 stats->tx_dropped = dev->stats.tx_dropped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300710}
711
712/* Rx descriptors helper methods */
713
willy tarreau54282132014-01-16 08:20:14 +0100714/* Checks whether the RX descriptor having this status is both the first
715 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300716 * received through a single RX descriptor, so not having each RX
717 * descriptor with its first and last bits set is an error
718 */
willy tarreau54282132014-01-16 08:20:14 +0100719static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300720{
willy tarreau54282132014-01-16 08:20:14 +0100721 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300722 MVNETA_RXD_FIRST_LAST_DESC;
723}
724
725/* Add number of descriptors ready to receive new packets */
726static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
727 struct mvneta_rx_queue *rxq,
728 int ndescs)
729{
730 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100731 * be added at once
732 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300733 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
734 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
735 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
736 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
737 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
738 }
739
740 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
741 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
742}
743
744/* Get number of RX descriptors occupied by received packets */
745static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
746 struct mvneta_rx_queue *rxq)
747{
748 u32 val;
749
750 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
751 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
752}
753
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100754/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300755 * from mvneta_rxq_drop_pkts().
756 */
757static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
758 struct mvneta_rx_queue *rxq,
759 int rx_done, int rx_filled)
760{
761 u32 val;
762
763 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
764 val = rx_done |
765 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
766 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
767 return;
768 }
769
770 /* Only 255 descriptors can be added at once */
771 while ((rx_done > 0) || (rx_filled > 0)) {
772 if (rx_done <= 0xff) {
773 val = rx_done;
774 rx_done = 0;
775 } else {
776 val = 0xff;
777 rx_done -= 0xff;
778 }
779 if (rx_filled <= 0xff) {
780 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
781 rx_filled = 0;
782 } else {
783 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
784 rx_filled -= 0xff;
785 }
786 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
787 }
788}
789
790/* Get pointer to next RX descriptor to be processed by SW */
791static struct mvneta_rx_desc *
792mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
793{
794 int rx_desc = rxq->next_desc_to_proc;
795
796 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100797 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300798 return rxq->descs + rx_desc;
799}
800
801/* Change maximum receive size of the port. */
802static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
803{
804 u32 val;
805
806 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
807 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
808 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
809 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
810 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
811}
812
813
814/* Set rx queue offset */
815static void mvneta_rxq_offset_set(struct mvneta_port *pp,
816 struct mvneta_rx_queue *rxq,
817 int offset)
818{
819 u32 val;
820
821 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
822 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
823
824 /* Offset is in */
825 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
826 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
827}
828
829
830/* Tx descriptors helper methods */
831
832/* Update HW with number of TX descriptors to be sent */
833static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
834 struct mvneta_tx_queue *txq,
835 int pend_desc)
836{
837 u32 val;
838
Simon Guinot0d637852017-11-13 16:27:02 +0100839 pend_desc += txq->pending;
840
841 /* Only 255 Tx descriptors can be added at once */
842 do {
843 val = min(pend_desc, 255);
844 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
845 pend_desc -= val;
846 } while (pend_desc > 0);
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100847 txq->pending = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300848}
849
850/* Get pointer to next TX descriptor to be processed (send) by HW */
851static struct mvneta_tx_desc *
852mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
853{
854 int tx_desc = txq->next_desc_to_proc;
855
856 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
857 return txq->descs + tx_desc;
858}
859
860/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100861 * mapping failures in the TX path.
862 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300863static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
864{
865 if (txq->next_desc_to_proc == 0)
866 txq->next_desc_to_proc = txq->last_desc - 1;
867 else
868 txq->next_desc_to_proc--;
869}
870
871/* Set rxq buf size */
872static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
873 struct mvneta_rx_queue *rxq,
874 int buf_size)
875{
876 u32 val;
877
878 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
879
880 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
881 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
882
883 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
884}
885
886/* Disable buffer management (BM) */
887static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
888 struct mvneta_rx_queue *rxq)
889{
890 u32 val;
891
892 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
893 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
894 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
895}
896
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100897/* Enable buffer management (BM) */
898static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
899 struct mvneta_rx_queue *rxq)
900{
901 u32 val;
902
903 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
904 val |= MVNETA_RXQ_HW_BUF_ALLOC;
905 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
906}
907
908/* Notify HW about port's assignment of pool for bigger packets */
909static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
910 struct mvneta_rx_queue *rxq)
911{
912 u32 val;
913
914 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
915 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
916 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
917
918 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
919}
920
921/* Notify HW about port's assignment of pool for smaller packets */
922static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
923 struct mvneta_rx_queue *rxq)
924{
925 u32 val;
926
927 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
928 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
929 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
930
931 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
932}
933
934/* Set port's receive buffer size for assigned BM pool */
935static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
936 int buf_size,
937 u8 pool_id)
938{
939 u32 val;
940
941 if (!IS_ALIGNED(buf_size, 8)) {
942 dev_warn(pp->dev->dev.parent,
943 "illegal buf_size value %d, round to %d\n",
944 buf_size, ALIGN(buf_size, 8));
945 buf_size = ALIGN(buf_size, 8);
946 }
947
948 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
949 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
950 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
951}
952
953/* Configure MBUS window in order to enable access BM internal SRAM */
954static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
955 u8 target, u8 attr)
956{
957 u32 win_enable, win_protect;
958 int i;
959
960 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
961
962 if (pp->bm_win_id < 0) {
963 /* Find first not occupied window */
964 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
965 if (win_enable & (1 << i)) {
966 pp->bm_win_id = i;
967 break;
968 }
969 }
970 if (i == MVNETA_MAX_DECODE_WIN)
971 return -ENOMEM;
972 } else {
973 i = pp->bm_win_id;
974 }
975
976 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
977 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
978
979 if (i < 4)
980 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
981
982 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
983 (attr << 8) | target);
984
985 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
986
987 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
988 win_protect |= 3 << (2 * i);
989 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
990
991 win_enable &= ~(1 << i);
992 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
993
994 return 0;
995}
996
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100997static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100998{
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100999 u32 wsize;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001000 u8 target, attr;
1001 int err;
1002
1003 /* Get BM window information */
1004 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1005 &target, &attr);
1006 if (err < 0)
1007 return err;
1008
1009 pp->bm_win_id = -1;
1010
1011 /* Open NETA -> BM window */
1012 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1013 target, attr);
1014 if (err < 0) {
1015 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1016 return err;
1017 }
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001018 return 0;
1019}
1020
1021/* Assign and initialize pools for port. In case of fail
1022 * buffer manager will remain disabled for current port.
1023 */
1024static int mvneta_bm_port_init(struct platform_device *pdev,
1025 struct mvneta_port *pp)
1026{
1027 struct device_node *dn = pdev->dev.of_node;
1028 u32 long_pool_id, short_pool_id;
1029
1030 if (!pp->neta_armada3700) {
1031 int ret;
1032
1033 ret = mvneta_bm_port_mbus_init(pp);
1034 if (ret)
1035 return ret;
1036 }
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001037
1038 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1039 netdev_info(pp->dev, "missing long pool id\n");
1040 return -EINVAL;
1041 }
1042
1043 /* Create port's long pool depending on mtu */
1044 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1045 MVNETA_BM_LONG, pp->id,
1046 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1047 if (!pp->pool_long) {
1048 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1049 return -ENOMEM;
1050 }
1051
1052 pp->pool_long->port_map |= 1 << pp->id;
1053
1054 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1055 pp->pool_long->id);
1056
1057 /* If short pool id is not defined, assume using single pool */
1058 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1059 short_pool_id = long_pool_id;
1060
1061 /* Create port's short pool */
1062 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1063 MVNETA_BM_SHORT, pp->id,
1064 MVNETA_BM_SHORT_PKT_SIZE);
1065 if (!pp->pool_short) {
1066 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1067 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1068 return -ENOMEM;
1069 }
1070
1071 if (short_pool_id != long_pool_id) {
1072 pp->pool_short->port_map |= 1 << pp->id;
1073 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1074 pp->pool_short->id);
1075 }
1076
1077 return 0;
1078}
1079
1080/* Update settings of a pool for bigger packets */
1081static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1082{
1083 struct mvneta_bm_pool *bm_pool = pp->pool_long;
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001084 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001085 int num;
1086
1087 /* Release all buffers from long pool */
1088 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001089 if (hwbm_pool->buf_num) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001090 WARN(1, "cannot free all buffers in pool %d\n",
1091 bm_pool->id);
1092 goto bm_mtu_err;
1093 }
1094
1095 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1096 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001097 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1098 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001099
1100 /* Fill entire long pool */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001101 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1102 if (num != hwbm_pool->size) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001103 WARN(1, "pool %d: %d of %d allocated\n",
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001104 bm_pool->id, num, hwbm_pool->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001105 goto bm_mtu_err;
1106 }
1107 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1108
1109 return;
1110
1111bm_mtu_err:
1112 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1113 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1114
1115 pp->bm_priv = NULL;
1116 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1117 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1118}
1119
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001120/* Start the Ethernet port RX and TX activity */
1121static void mvneta_port_up(struct mvneta_port *pp)
1122{
1123 int queue;
1124 u32 q_map;
1125
1126 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001127 q_map = 0;
1128 for (queue = 0; queue < txq_number; queue++) {
1129 struct mvneta_tx_queue *txq = &pp->txqs[queue];
Markus Elfringf95936c2017-04-16 22:45:33 +02001130 if (txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001131 q_map |= (1 << queue);
1132 }
1133 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1134
Yelena Krivosheeve81b5e02018-03-30 12:05:31 +02001135 q_map = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001136 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001137 for (queue = 0; queue < rxq_number; queue++) {
1138 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1139
Markus Elfringf95936c2017-04-16 22:45:33 +02001140 if (rxq->descs)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001141 q_map |= (1 << queue);
1142 }
1143 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001144}
1145
1146/* Stop the Ethernet port activity */
1147static void mvneta_port_down(struct mvneta_port *pp)
1148{
1149 u32 val;
1150 int count;
1151
1152 /* Stop Rx port activity. Check port Rx activity. */
1153 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1154
1155 /* Issue stop command for active channels only */
1156 if (val != 0)
1157 mvreg_write(pp, MVNETA_RXQ_CMD,
1158 val << MVNETA_RXQ_DISABLE_SHIFT);
1159
1160 /* Wait for all Rx activity to terminate. */
1161 count = 0;
1162 do {
1163 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1164 netdev_warn(pp->dev,
Dmitri Epshtein0838abb2016-03-12 18:44:19 +01001165 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001166 val);
1167 break;
1168 }
1169 mdelay(1);
1170
1171 val = mvreg_read(pp, MVNETA_RXQ_CMD);
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001172 } while (val & MVNETA_RXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001173
1174 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001175 * command for active channels only
1176 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001177 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1178
1179 if (val != 0)
1180 mvreg_write(pp, MVNETA_TXQ_CMD,
1181 (val << MVNETA_TXQ_DISABLE_SHIFT));
1182
1183 /* Wait for all Tx activity to terminate. */
1184 count = 0;
1185 do {
1186 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1187 netdev_warn(pp->dev,
1188 "TIMEOUT for TX stopped status=0x%08x\n",
1189 val);
1190 break;
1191 }
1192 mdelay(1);
1193
1194 /* Check TX Command reg that all Txqs are stopped */
1195 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1196
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001197 } while (val & MVNETA_TXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001198
1199 /* Double check to verify that TX FIFO is empty */
1200 count = 0;
1201 do {
1202 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1203 netdev_warn(pp->dev,
Dmitri Epshtein0838abb2016-03-12 18:44:19 +01001204 "TX FIFO empty timeout status=0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001205 val);
1206 break;
1207 }
1208 mdelay(1);
1209
1210 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1211 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1212 (val & MVNETA_TX_IN_PRGRS));
1213
1214 udelay(200);
1215}
1216
1217/* Enable the port by setting the port enable bit of the MAC control register */
1218static void mvneta_port_enable(struct mvneta_port *pp)
1219{
1220 u32 val;
1221
1222 /* Enable port */
1223 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1224 val |= MVNETA_GMAC0_PORT_ENABLE;
1225 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1226}
1227
1228/* Disable the port and wait for about 200 usec before retuning */
1229static void mvneta_port_disable(struct mvneta_port *pp)
1230{
1231 u32 val;
1232
1233 /* Reset the Enable bit in the Serial Control Register */
1234 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1235 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1236 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1237
1238 udelay(200);
1239}
1240
1241/* Multicast tables methods */
1242
1243/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1244static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1245{
1246 int offset;
1247 u32 val;
1248
1249 if (queue == -1) {
1250 val = 0;
1251 } else {
1252 val = 0x1 | (queue << 1);
1253 val |= (val << 24) | (val << 16) | (val << 8);
1254 }
1255
1256 for (offset = 0; offset <= 0xc; offset += 4)
1257 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1258}
1259
1260/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1261static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1262{
1263 int offset;
1264 u32 val;
1265
1266 if (queue == -1) {
1267 val = 0;
1268 } else {
1269 val = 0x1 | (queue << 1);
1270 val |= (val << 24) | (val << 16) | (val << 8);
1271 }
1272
1273 for (offset = 0; offset <= 0xfc; offset += 4)
1274 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1275
1276}
1277
1278/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1279static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1280{
1281 int offset;
1282 u32 val;
1283
1284 if (queue == -1) {
1285 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1286 val = 0;
1287 } else {
1288 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1289 val = 0x1 | (queue << 1);
1290 val |= (val << 24) | (val << 16) | (val << 8);
1291 }
1292
1293 for (offset = 0; offset <= 0xfc; offset += 4)
1294 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1295}
1296
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001297static void mvneta_percpu_unmask_interrupt(void *arg)
1298{
1299 struct mvneta_port *pp = arg;
1300
1301 /* All the queue are unmasked, but actually only the ones
1302 * mapped to this CPU will be unmasked
1303 */
1304 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1305 MVNETA_RX_INTR_MASK_ALL |
1306 MVNETA_TX_INTR_MASK_ALL |
1307 MVNETA_MISCINTR_INTR_MASK);
1308}
1309
1310static void mvneta_percpu_mask_interrupt(void *arg)
1311{
1312 struct mvneta_port *pp = arg;
1313
1314 /* All the queue are masked, but actually only the ones
1315 * mapped to this CPU will be masked
1316 */
1317 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1318 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1319 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1320}
1321
1322static void mvneta_percpu_clear_intr_cause(void *arg)
1323{
1324 struct mvneta_port *pp = arg;
1325
1326 /* All the queue are cleared, but actually only the ones
1327 * mapped to this CPU will be cleared
1328 */
1329 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1330 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1331 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1332}
1333
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001334/* This method sets defaults to the NETA port:
1335 * Clears interrupt Cause and Mask registers.
1336 * Clears all MAC tables.
1337 * Sets defaults to all registers.
1338 * Resets RX and TX descriptor rings.
1339 * Resets PHY.
1340 * This method can be called after mvneta_port_down() to return the port
1341 * settings to defaults.
1342 */
1343static void mvneta_defaults_set(struct mvneta_port *pp)
1344{
1345 int cpu;
1346 int queue;
1347 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001348 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001349
1350 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001351 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001352
1353 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001354 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001355 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1356
1357 /* Enable MBUS Retry bit16 */
1358 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1359
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001360 /* Set CPU queue access map. CPUs are assigned to the RX and
1361 * TX queues modulo their number. If there is only one TX
1362 * queue then it is assigned to the CPU associated to the
1363 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001364 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001365 for_each_present_cpu(cpu) {
1366 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001367 int rxq, txq;
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001368 if (!pp->neta_armada3700) {
1369 for (rxq = 0; rxq < rxq_number; rxq++)
1370 if ((rxq % max_cpu) == cpu)
1371 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001372
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001373 for (txq = 0; txq < txq_number; txq++)
1374 if ((txq % max_cpu) == cpu)
1375 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001376
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001377 /* With only one TX queue we configure a special case
1378 * which will allow to get all the irq on a single
1379 * CPU
1380 */
1381 if (txq_number == 1)
1382 txq_map = (cpu == pp->rxq_def) ?
1383 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001384
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001385 } else {
1386 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1387 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1388 }
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001389
1390 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1391 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001392
1393 /* Reset RX and TX DMAs */
1394 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1395 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1396
1397 /* Disable Legacy WRR, Disable EJP, Release from reset */
1398 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1399 for (queue = 0; queue < txq_number; queue++) {
1400 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1401 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1402 }
1403
1404 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1405 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1406
1407 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001408 if (pp->bm_priv)
1409 /* HW buffer management + legacy parser */
1410 val = MVNETA_ACC_MODE_EXT2;
1411 else
1412 /* SW buffer management + legacy parser */
1413 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001414 mvreg_write(pp, MVNETA_ACC_MODE, val);
1415
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001416 if (pp->bm_priv)
1417 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1418
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001419 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001420 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001421 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1422
1423 val = 0;
1424 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1425 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1426
1427 /* Build PORT_SDMA_CONFIG_REG */
1428 val = 0;
1429
1430 /* Default burst size */
1431 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1432 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001433 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001434
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001435#if defined(__BIG_ENDIAN)
1436 val |= MVNETA_DESC_SWAP;
1437#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001438
1439 /* Assign port SDMA configuration */
1440 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1441
Thomas Petazzoni71408602013-09-04 16:21:18 +02001442 /* Disable PHY polling in hardware, since we're using the
1443 * kernel phylib to do this.
1444 */
1445 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1446 val &= ~MVNETA_PHY_POLLING_ENABLE;
1447 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1448
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001449 mvneta_set_ucast_table(pp, -1);
1450 mvneta_set_special_mcast_table(pp, -1);
1451 mvneta_set_other_mcast_table(pp, -1);
1452
1453 /* Set port interrupt enable register - default enable all */
1454 mvreg_write(pp, MVNETA_INTR_ENABLE,
1455 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1456 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001457
1458 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001459}
1460
1461/* Set max sizes for tx queues */
1462static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1463
1464{
1465 u32 val, size, mtu;
1466 int queue;
1467
1468 mtu = max_tx_size * 8;
1469 if (mtu > MVNETA_TX_MTU_MAX)
1470 mtu = MVNETA_TX_MTU_MAX;
1471
1472 /* Set MTU */
1473 val = mvreg_read(pp, MVNETA_TX_MTU);
1474 val &= ~MVNETA_TX_MTU_MAX;
1475 val |= mtu;
1476 mvreg_write(pp, MVNETA_TX_MTU, val);
1477
1478 /* TX token size and all TXQs token size must be larger that MTU */
1479 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1480
1481 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1482 if (size < mtu) {
1483 size = mtu;
1484 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1485 val |= size;
1486 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1487 }
1488 for (queue = 0; queue < txq_number; queue++) {
1489 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1490
1491 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1492 if (size < mtu) {
1493 size = mtu;
1494 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1495 val |= size;
1496 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1497 }
1498 }
1499}
1500
1501/* Set unicast address */
1502static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1503 int queue)
1504{
1505 unsigned int unicast_reg;
1506 unsigned int tbl_offset;
1507 unsigned int reg_offset;
1508
1509 /* Locate the Unicast table entry */
1510 last_nibble = (0xf & last_nibble);
1511
1512 /* offset from unicast tbl base */
1513 tbl_offset = (last_nibble / 4) * 4;
1514
1515 /* offset within the above reg */
1516 reg_offset = last_nibble % 4;
1517
1518 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1519
1520 if (queue == -1) {
1521 /* Clear accepts frame bit at specified unicast DA tbl entry */
1522 unicast_reg &= ~(0xff << (8 * reg_offset));
1523 } else {
1524 unicast_reg &= ~(0xff << (8 * reg_offset));
1525 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1526 }
1527
1528 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1529}
1530
1531/* Set mac address */
1532static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1533 int queue)
1534{
1535 unsigned int mac_h;
1536 unsigned int mac_l;
1537
1538 if (queue != -1) {
1539 mac_l = (addr[4] << 8) | (addr[5]);
1540 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1541 (addr[2] << 8) | (addr[3] << 0);
1542
1543 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1544 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1545 }
1546
1547 /* Accept frames of this address */
1548 mvneta_set_ucast_addr(pp, addr[5], queue);
1549}
1550
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001551/* Set the number of packets that will be received before RX interrupt
1552 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001553 */
1554static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1555 struct mvneta_rx_queue *rxq, u32 value)
1556{
1557 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1558 value | MVNETA_RXQ_NON_OCCUPIED(0));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001559}
1560
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001561/* Set the time delay in usec before RX interrupt will be generated by
1562 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001563 */
1564static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1565 struct mvneta_rx_queue *rxq, u32 value)
1566{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001567 u32 val;
1568 unsigned long clk_rate;
1569
1570 clk_rate = clk_get_rate(pp->clk);
1571 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001572
1573 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001574}
1575
1576/* Set threshold for TX_DONE pkts coalescing */
1577static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1578 struct mvneta_tx_queue *txq, u32 value)
1579{
1580 u32 val;
1581
1582 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1583
1584 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1585 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1586
1587 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001588}
1589
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001590/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1591static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001592 u32 phys_addr, void *virt_addr,
1593 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001594{
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001595 int i;
1596
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001597 rx_desc->buf_phys_addr = phys_addr;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001598 i = rx_desc - rxq->descs;
1599 rxq->buf_virt_addr[i] = virt_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001600}
1601
1602/* Decrement sent descriptors counter */
1603static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1604 struct mvneta_tx_queue *txq,
1605 int sent_desc)
1606{
1607 u32 val;
1608
1609 /* Only 255 TX descriptors can be updated at once */
1610 while (sent_desc > 0xff) {
1611 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1612 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1613 sent_desc = sent_desc - 0xff;
1614 }
1615
1616 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1617 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1618}
1619
1620/* Get number of TX descriptors already sent by HW */
1621static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1622 struct mvneta_tx_queue *txq)
1623{
1624 u32 val;
1625 int sent_desc;
1626
1627 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1628 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1629 MVNETA_TXQ_SENT_DESC_SHIFT;
1630
1631 return sent_desc;
1632}
1633
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001634/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001635 * The number of sent descriptors is returned.
1636 */
1637static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1638 struct mvneta_tx_queue *txq)
1639{
1640 int sent_desc;
1641
1642 /* Get number of sent descriptors */
1643 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1644
1645 /* Decrement sent descriptors counter */
1646 if (sent_desc)
1647 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1648
1649 return sent_desc;
1650}
1651
1652/* Set TXQ descriptors fields relevant for CSUM calculation */
1653static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1654 int ip_hdr_len, int l4_proto)
1655{
1656 u32 command;
1657
1658 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001659 * G_L4_chk, L4_type; required only for checksum
1660 * calculation
1661 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001662 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1663 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1664
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001665 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001666 command |= MVNETA_TXD_IP_CSUM;
1667 else
1668 command |= MVNETA_TX_L3_IP6;
1669
1670 if (l4_proto == IPPROTO_TCP)
1671 command |= MVNETA_TX_L4_CSUM_FULL;
1672 else if (l4_proto == IPPROTO_UDP)
1673 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1674 else
1675 command |= MVNETA_TX_L4_CSUM_NOT;
1676
1677 return command;
1678}
1679
1680
1681/* Display more error info */
1682static void mvneta_rx_error(struct mvneta_port *pp,
1683 struct mvneta_rx_desc *rx_desc)
1684{
1685 u32 status = rx_desc->status;
1686
willy tarreau54282132014-01-16 08:20:14 +01001687 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001688 netdev_err(pp->dev,
1689 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001690 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001691 return;
1692 }
1693
1694 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1695 case MVNETA_RXD_ERR_CRC:
1696 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1697 status, rx_desc->data_size);
1698 break;
1699 case MVNETA_RXD_ERR_OVERRUN:
1700 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1701 status, rx_desc->data_size);
1702 break;
1703 case MVNETA_RXD_ERR_LEN:
1704 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1705 status, rx_desc->data_size);
1706 break;
1707 case MVNETA_RXD_ERR_RESOURCE:
1708 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1709 status, rx_desc->data_size);
1710 break;
1711 }
1712}
1713
willy tarreau54282132014-01-16 08:20:14 +01001714/* Handle RX checksum offload based on the descriptor's status */
1715static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001716 struct sk_buff *skb)
1717{
willy tarreau54282132014-01-16 08:20:14 +01001718 if ((status & MVNETA_RXD_L3_IP4) &&
1719 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001720 skb->csum = 0;
1721 skb->ip_summed = CHECKSUM_UNNECESSARY;
1722 return;
1723 }
1724
1725 skb->ip_summed = CHECKSUM_NONE;
1726}
1727
willy tarreau6c498972014-01-16 08:20:12 +01001728/* Return tx queue pointer (find last set bit) according to <cause> returned
1729 * form tx_done reg. <cause> must not be null. The return value is always a
1730 * valid queue for matching the first one found in <cause>.
1731 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001732static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1733 u32 cause)
1734{
1735 int queue = fls(cause) - 1;
1736
willy tarreau6c498972014-01-16 08:20:12 +01001737 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001738}
1739
1740/* Free tx queue skbuffs */
1741static void mvneta_txq_bufs_free(struct mvneta_port *pp,
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001742 struct mvneta_tx_queue *txq, int num,
1743 struct netdev_queue *nq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001744{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001745 unsigned int bytes_compl = 0, pkts_compl = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001746 int i;
1747
1748 for (i = 0; i < num; i++) {
1749 struct mvneta_tx_desc *tx_desc = txq->descs +
1750 txq->txq_get_index;
1751 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1752
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001753 if (skb) {
1754 bytes_compl += skb->len;
1755 pkts_compl++;
1756 }
1757
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001758 mvneta_txq_inc_get(txq);
1759
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001760 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1761 dma_unmap_single(pp->dev->dev.parent,
1762 tx_desc->buf_phys_addr,
1763 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001764 if (!skb)
1765 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001766 dev_kfree_skb_any(skb);
1767 }
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001768
1769 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001770}
1771
1772/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001773static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001774 struct mvneta_tx_queue *txq)
1775{
1776 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1777 int tx_done;
1778
1779 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001780 if (!tx_done)
1781 return;
1782
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001783 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001784
1785 txq->count -= tx_done;
1786
1787 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001788 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001789 netif_tx_wake_queue(nq);
1790 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001791}
1792
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001793void *mvneta_frag_alloc(unsigned int frag_size)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001794{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001795 if (likely(frag_size <= PAGE_SIZE))
1796 return netdev_alloc_frag(frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001797 else
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001798 return kmalloc(frag_size, GFP_ATOMIC);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001799}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001800EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001801
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001802void mvneta_frag_free(unsigned int frag_size, void *data)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001803{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001804 if (likely(frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001805 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001806 else
1807 kfree(data);
1808}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001809EXPORT_SYMBOL_GPL(mvneta_frag_free);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001810
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001811/* Refill processing for SW buffer management */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001812static int mvneta_rx_refill(struct mvneta_port *pp,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001813 struct mvneta_rx_desc *rx_desc,
1814 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001815
1816{
1817 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001818 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001819
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001820 data = mvneta_frag_alloc(pp->frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001821 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001822 return -ENOMEM;
1823
willy tarreau8ec2cd42014-01-16 08:20:16 +01001824 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001825 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1826 DMA_FROM_DEVICE);
1827 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001828 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001829 return -ENOMEM;
1830 }
1831
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01001832 phys_addr += pp->rx_offset_correction;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001833 mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001834 return 0;
1835}
1836
1837/* Handle tx checksum */
1838static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1839{
1840 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1841 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001842 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001843 u8 l4_proto;
1844
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001845 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001846 struct iphdr *ip4h = ip_hdr(skb);
1847
1848 /* Calculate IPv4 checksum and L4 checksum */
1849 ip_hdr_len = ip4h->ihl;
1850 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001851 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001852 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1853
1854 /* Read l4_protocol from one of IPv6 extra headers */
1855 if (skb_network_header_len(skb) > 0)
1856 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1857 l4_proto = ip6h->nexthdr;
1858 } else
1859 return MVNETA_TX_L4_CSUM_NOT;
1860
1861 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001862 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001863 }
1864
1865 return MVNETA_TX_L4_CSUM_NOT;
1866}
1867
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001868/* Drop packets received by the RXQ and free buffers */
1869static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1870 struct mvneta_rx_queue *rxq)
1871{
1872 int rx_done, i;
1873
1874 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001875 if (rx_done)
1876 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1877
1878 if (pp->bm_priv) {
1879 for (i = 0; i < rx_done; i++) {
1880 struct mvneta_rx_desc *rx_desc =
1881 mvneta_rxq_next_desc_get(rxq);
1882 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1883 struct mvneta_bm_pool *bm_pool;
1884
1885 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1886 /* Return dropped buffer to the pool */
1887 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1888 rx_desc->buf_phys_addr);
1889 }
1890 return;
1891 }
1892
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001893 for (i = 0; i < rxq->size; i++) {
1894 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001895 void *data = rxq->buf_virt_addr[i];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001896
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001897 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001898 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001899 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001900 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001901}
1902
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001903/* Main rx processing when using software buffer management */
1904static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1905 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001906{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001907 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001908 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001909 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001910 u32 rcvd_pkts = 0;
1911 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001912
1913 /* Get number of received packets */
1914 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1915
1916 if (rx_todo > rx_done)
1917 rx_todo = rx_done;
1918
1919 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001920
1921 /* Fairness NAPI loop */
1922 while (rx_done < rx_todo) {
1923 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1924 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001925 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001926 dma_addr_t phys_addr;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001927 u32 rx_status, frag_size;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001928 int rx_bytes, err, index;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001929
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001930 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001931 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001932 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001933 index = rx_desc - rxq->descs;
1934 data = rxq->buf_virt_addr[index];
Antoine Tenart271f7ff2018-06-22 10:15:39 +02001935 phys_addr = rx_desc->buf_phys_addr - pp->rx_offset_correction;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001936
willy tarreau54282132014-01-16 08:20:14 +01001937 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001938 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
Yelena Krivosheev2eecb2e2017-12-19 17:59:47 +01001939 mvneta_rx_error(pp, rx_desc);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001940err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001941 dev->stats.rx_errors++;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001942 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001943 continue;
1944 }
1945
willy tarreauf19fadf2014-01-16 08:20:17 +01001946 if (rx_bytes <= rx_copybreak) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001947 /* better copy a small frame and not unmap the DMA region */
willy tarreauf19fadf2014-01-16 08:20:17 +01001948 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1949 if (unlikely(!skb))
1950 goto err_drop_frame;
1951
1952 dma_sync_single_range_for_cpu(dev->dev.parent,
Gregory CLEMENTac83b7d2016-12-01 18:03:04 +01001953 phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001954 MVNETA_MH_SIZE + NET_SKB_PAD,
1955 rx_bytes,
1956 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02001957 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
1958 rx_bytes);
willy tarreauf19fadf2014-01-16 08:20:17 +01001959
1960 skb->protocol = eth_type_trans(skb, dev);
1961 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001962 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001963
1964 rcvd_pkts++;
1965 rcvd_bytes += rx_bytes;
1966
1967 /* leave the descriptor and buffer untouched */
1968 continue;
1969 }
1970
Simon Guinota84e3282015-07-19 13:00:53 +02001971 /* Refill processing */
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001972 err = mvneta_rx_refill(pp, rx_desc, rxq);
Simon Guinota84e3282015-07-19 13:00:53 +02001973 if (err) {
1974 netdev_err(dev, "Linux processing - Can't refill\n");
1975 rxq->missed++;
1976 goto err_drop_frame;
1977 }
1978
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001979 frag_size = pp->frag_size;
1980
1981 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01001982
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001983 /* After refill old buffer has to be unmapped regardless
1984 * the skb is successfully built or not.
1985 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02001986 dma_unmap_single(dev->dev.parent, phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001987 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1988 DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001989
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001990 if (!skb)
1991 goto err_drop_frame;
1992
willy tarreaudc4277d2014-01-16 08:20:07 +01001993 rcvd_pkts++;
1994 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001995
1996 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001997 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001998 skb_put(skb, rx_bytes);
1999
2000 skb->protocol = eth_type_trans(skb, dev);
2001
willy tarreau54282132014-01-16 08:20:14 +01002002 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002003
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002004 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002005 }
2006
willy tarreaudc4277d2014-01-16 08:20:07 +01002007 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01002008 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2009
2010 u64_stats_update_begin(&stats->syncp);
2011 stats->rx_packets += rcvd_pkts;
2012 stats->rx_bytes += rcvd_bytes;
2013 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01002014 }
2015
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002016 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02002017 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002018
2019 return rx_done;
2020}
2021
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002022/* Main rx processing when using hardware buffer management */
2023static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2024 struct mvneta_rx_queue *rxq)
2025{
2026 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2027 struct net_device *dev = pp->dev;
2028 int rx_done;
2029 u32 rcvd_pkts = 0;
2030 u32 rcvd_bytes = 0;
2031
2032 /* Get number of received packets */
2033 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2034
2035 if (rx_todo > rx_done)
2036 rx_todo = rx_done;
2037
2038 rx_done = 0;
2039
2040 /* Fairness NAPI loop */
2041 while (rx_done < rx_todo) {
2042 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2043 struct mvneta_bm_pool *bm_pool = NULL;
2044 struct sk_buff *skb;
2045 unsigned char *data;
2046 dma_addr_t phys_addr;
2047 u32 rx_status, frag_size;
2048 int rx_bytes, err;
2049 u8 pool_id;
2050
2051 rx_done++;
2052 rx_status = rx_desc->status;
2053 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002054 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002055 phys_addr = rx_desc->buf_phys_addr;
2056 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2057 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2058
2059 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2060 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2061err_drop_frame_ret_pool:
2062 /* Return the buffer to the pool */
2063 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2064 rx_desc->buf_phys_addr);
2065err_drop_frame:
2066 dev->stats.rx_errors++;
2067 mvneta_rx_error(pp, rx_desc);
2068 /* leave the descriptor untouched */
2069 continue;
2070 }
2071
2072 if (rx_bytes <= rx_copybreak) {
2073 /* better copy a small frame and not unmap the DMA region */
2074 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2075 if (unlikely(!skb))
2076 goto err_drop_frame_ret_pool;
2077
2078 dma_sync_single_range_for_cpu(dev->dev.parent,
2079 rx_desc->buf_phys_addr,
2080 MVNETA_MH_SIZE + NET_SKB_PAD,
2081 rx_bytes,
2082 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02002083 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2084 rx_bytes);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002085
2086 skb->protocol = eth_type_trans(skb, dev);
2087 mvneta_rx_csum(pp, rx_status, skb);
2088 napi_gro_receive(&port->napi, skb);
2089
2090 rcvd_pkts++;
2091 rcvd_bytes += rx_bytes;
2092
2093 /* Return the buffer to the pool */
2094 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2095 rx_desc->buf_phys_addr);
2096
2097 /* leave the descriptor and buffer untouched */
2098 continue;
2099 }
2100
2101 /* Refill processing */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002102 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002103 if (err) {
2104 netdev_err(dev, "Linux processing - Can't refill\n");
2105 rxq->missed++;
2106 goto err_drop_frame_ret_pool;
2107 }
2108
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002109 frag_size = bm_pool->hwbm_pool.frag_size;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002110
2111 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2112
2113 /* After refill old buffer has to be unmapped regardless
2114 * the skb is successfully built or not.
2115 */
2116 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2117 bm_pool->buf_size, DMA_FROM_DEVICE);
2118 if (!skb)
2119 goto err_drop_frame;
2120
2121 rcvd_pkts++;
2122 rcvd_bytes += rx_bytes;
2123
2124 /* Linux processing */
2125 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2126 skb_put(skb, rx_bytes);
2127
2128 skb->protocol = eth_type_trans(skb, dev);
2129
2130 mvneta_rx_csum(pp, rx_status, skb);
2131
2132 napi_gro_receive(&port->napi, skb);
2133 }
2134
2135 if (rcvd_pkts) {
2136 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2137
2138 u64_stats_update_begin(&stats->syncp);
2139 stats->rx_packets += rcvd_pkts;
2140 stats->rx_bytes += rcvd_bytes;
2141 u64_stats_update_end(&stats->syncp);
2142 }
2143
2144 /* Update rxq management counters */
2145 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2146
2147 return rx_done;
2148}
2149
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002150static inline void
2151mvneta_tso_put_hdr(struct sk_buff *skb,
2152 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2153{
2154 struct mvneta_tx_desc *tx_desc;
2155 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2156
2157 txq->tx_skb[txq->txq_put_index] = NULL;
2158 tx_desc = mvneta_txq_next_desc_get(txq);
2159 tx_desc->data_size = hdr_len;
2160 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2161 tx_desc->command |= MVNETA_TXD_F_DESC;
2162 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2163 txq->txq_put_index * TSO_HEADER_SIZE;
2164 mvneta_txq_inc_put(txq);
2165}
2166
2167static inline int
2168mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2169 struct sk_buff *skb, char *data, int size,
2170 bool last_tcp, bool is_last)
2171{
2172 struct mvneta_tx_desc *tx_desc;
2173
2174 tx_desc = mvneta_txq_next_desc_get(txq);
2175 tx_desc->data_size = size;
2176 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2177 size, DMA_TO_DEVICE);
2178 if (unlikely(dma_mapping_error(dev->dev.parent,
2179 tx_desc->buf_phys_addr))) {
2180 mvneta_txq_desc_put(txq);
2181 return -ENOMEM;
2182 }
2183
2184 tx_desc->command = 0;
2185 txq->tx_skb[txq->txq_put_index] = NULL;
2186
2187 if (last_tcp) {
2188 /* last descriptor in the TCP packet */
2189 tx_desc->command = MVNETA_TXD_L_DESC;
2190
2191 /* last descriptor in SKB */
2192 if (is_last)
2193 txq->tx_skb[txq->txq_put_index] = skb;
2194 }
2195 mvneta_txq_inc_put(txq);
2196 return 0;
2197}
2198
2199static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2200 struct mvneta_tx_queue *txq)
2201{
2202 int total_len, data_left;
2203 int desc_count = 0;
2204 struct mvneta_port *pp = netdev_priv(dev);
2205 struct tso_t tso;
2206 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2207 int i;
2208
2209 /* Count needed descriptors */
2210 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2211 return 0;
2212
2213 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2214 pr_info("*** Is this even possible???!?!?\n");
2215 return 0;
2216 }
2217
2218 /* Initialize the TSO handler, and prepare the first payload */
2219 tso_start(skb, &tso);
2220
2221 total_len = skb->len - hdr_len;
2222 while (total_len > 0) {
2223 char *hdr;
2224
2225 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2226 total_len -= data_left;
2227 desc_count++;
2228
2229 /* prepare packet headers: MAC + IP + TCP */
2230 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2231 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2232
2233 mvneta_tso_put_hdr(skb, pp, txq);
2234
2235 while (data_left > 0) {
2236 int size;
2237 desc_count++;
2238
2239 size = min_t(int, tso.size, data_left);
2240
2241 if (mvneta_tso_put_data(dev, txq, skb,
2242 tso.data, size,
2243 size == data_left,
2244 total_len == 0))
2245 goto err_release;
2246 data_left -= size;
2247
2248 tso_build_data(skb, &tso, size);
2249 }
2250 }
2251
2252 return desc_count;
2253
2254err_release:
2255 /* Release all used data descriptors; header descriptors must not
2256 * be DMA-unmapped.
2257 */
2258 for (i = desc_count - 1; i >= 0; i--) {
2259 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002260 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002261 dma_unmap_single(pp->dev->dev.parent,
2262 tx_desc->buf_phys_addr,
2263 tx_desc->data_size,
2264 DMA_TO_DEVICE);
2265 mvneta_txq_desc_put(txq);
2266 }
2267 return 0;
2268}
2269
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002270/* Handle tx fragmentation processing */
2271static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2272 struct mvneta_tx_queue *txq)
2273{
2274 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002275 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002276
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002277 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002278 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2279 void *addr = page_address(frag->page.p) + frag->page_offset;
2280
2281 tx_desc = mvneta_txq_next_desc_get(txq);
2282 tx_desc->data_size = frag->size;
2283
2284 tx_desc->buf_phys_addr =
2285 dma_map_single(pp->dev->dev.parent, addr,
2286 tx_desc->data_size, DMA_TO_DEVICE);
2287
2288 if (dma_mapping_error(pp->dev->dev.parent,
2289 tx_desc->buf_phys_addr)) {
2290 mvneta_txq_desc_put(txq);
2291 goto error;
2292 }
2293
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002294 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002295 /* Last descriptor */
2296 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002297 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002298 } else {
2299 /* Descriptor in the middle: Not First, Not Last */
2300 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002301 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002302 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002303 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002304 }
2305
2306 return 0;
2307
2308error:
2309 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002310 * this packet, as well as the corresponding DMA mappings
2311 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002312 for (i = i - 1; i >= 0; i--) {
2313 tx_desc = txq->descs + i;
2314 dma_unmap_single(pp->dev->dev.parent,
2315 tx_desc->buf_phys_addr,
2316 tx_desc->data_size,
2317 DMA_TO_DEVICE);
2318 mvneta_txq_desc_put(txq);
2319 }
2320
2321 return -ENOMEM;
2322}
2323
2324/* Main tx processing */
2325static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2326{
2327 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002328 u16 txq_id = skb_get_queue_mapping(skb);
2329 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002330 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002331 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002332 int frags = 0;
2333 u32 tx_cmd;
2334
2335 if (!netif_running(dev))
2336 goto out;
2337
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002338 if (skb_is_gso(skb)) {
2339 frags = mvneta_tx_tso(skb, dev, txq);
2340 goto out;
2341 }
2342
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002343 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002344
2345 /* Get a descriptor for the first part of the packet */
2346 tx_desc = mvneta_txq_next_desc_get(txq);
2347
2348 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2349
2350 tx_desc->data_size = skb_headlen(skb);
2351
2352 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2353 tx_desc->data_size,
2354 DMA_TO_DEVICE);
2355 if (unlikely(dma_mapping_error(dev->dev.parent,
2356 tx_desc->buf_phys_addr))) {
2357 mvneta_txq_desc_put(txq);
2358 frags = 0;
2359 goto out;
2360 }
2361
2362 if (frags == 1) {
2363 /* First and Last descriptor */
2364 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2365 tx_desc->command = tx_cmd;
2366 txq->tx_skb[txq->txq_put_index] = skb;
2367 mvneta_txq_inc_put(txq);
2368 } else {
2369 /* First but not Last */
2370 tx_cmd |= MVNETA_TXD_F_DESC;
2371 txq->tx_skb[txq->txq_put_index] = NULL;
2372 mvneta_txq_inc_put(txq);
2373 tx_desc->command = tx_cmd;
2374 /* Continue with other skb fragments */
2375 if (mvneta_tx_frag_process(pp, skb, txq)) {
2376 dma_unmap_single(dev->dev.parent,
2377 tx_desc->buf_phys_addr,
2378 tx_desc->data_size,
2379 DMA_TO_DEVICE);
2380 mvneta_txq_desc_put(txq);
2381 frags = 0;
2382 goto out;
2383 }
2384 }
2385
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002386out:
2387 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01002388 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002389 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2390
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002391 netdev_tx_sent_queue(nq, len);
2392
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002393 txq->count += frags;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002394 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002395 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002396
Simon Guinot2a90f7e2017-01-16 18:08:31 +01002397 if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2398 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2399 mvneta_txq_pend_desc_add(pp, txq, frags);
2400 else
2401 txq->pending += frags;
2402
willy tarreau74c41b02014-01-16 08:20:08 +01002403 u64_stats_update_begin(&stats->syncp);
2404 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002405 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01002406 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002407 } else {
2408 dev->stats.tx_dropped++;
2409 dev_kfree_skb_any(skb);
2410 }
2411
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002412 return NETDEV_TX_OK;
2413}
2414
2415
2416/* Free tx resources, when resetting a port */
2417static void mvneta_txq_done_force(struct mvneta_port *pp,
2418 struct mvneta_tx_queue *txq)
2419
2420{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002421 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002422 int tx_done = txq->count;
2423
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002424 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002425
2426 /* reset txq */
2427 txq->count = 0;
2428 txq->txq_put_index = 0;
2429 txq->txq_get_index = 0;
2430}
2431
willy tarreau6c498972014-01-16 08:20:12 +01002432/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2433 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2434 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002435static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002436{
2437 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002438 struct netdev_queue *nq;
2439
willy tarreau6c498972014-01-16 08:20:12 +01002440 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002441 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002442
2443 nq = netdev_get_tx_queue(pp->dev, txq->id);
2444 __netif_tx_lock(nq, smp_processor_id());
2445
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002446 if (txq->count)
2447 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002448
2449 __netif_tx_unlock(nq);
2450 cause_tx_done &= ~((1 << txq->id));
2451 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002452}
2453
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002454/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002455 * according to hw spec, different than generic crc8 algorithm
2456 */
2457static int mvneta_addr_crc(unsigned char *addr)
2458{
2459 int crc = 0;
2460 int i;
2461
2462 for (i = 0; i < ETH_ALEN; i++) {
2463 int j;
2464
2465 crc = (crc ^ addr[i]) << 8;
2466 for (j = 7; j >= 0; j--) {
2467 if (crc & (0x100 << j))
2468 crc ^= 0x107 << j;
2469 }
2470 }
2471
2472 return crc;
2473}
2474
2475/* This method controls the net device special MAC multicast support.
2476 * The Special Multicast Table for MAC addresses supports MAC of the form
2477 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2478 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2479 * Table entries in the DA-Filter table. This method set the Special
2480 * Multicast Table appropriate entry.
2481 */
2482static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2483 unsigned char last_byte,
2484 int queue)
2485{
2486 unsigned int smc_table_reg;
2487 unsigned int tbl_offset;
2488 unsigned int reg_offset;
2489
2490 /* Register offset from SMC table base */
2491 tbl_offset = (last_byte / 4);
2492 /* Entry offset within the above reg */
2493 reg_offset = last_byte % 4;
2494
2495 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2496 + tbl_offset * 4));
2497
2498 if (queue == -1)
2499 smc_table_reg &= ~(0xff << (8 * reg_offset));
2500 else {
2501 smc_table_reg &= ~(0xff << (8 * reg_offset));
2502 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2503 }
2504
2505 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2506 smc_table_reg);
2507}
2508
2509/* This method controls the network device Other MAC multicast support.
2510 * The Other Multicast Table is used for multicast of another type.
2511 * A CRC-8 is used as an index to the Other Multicast Table entries
2512 * in the DA-Filter table.
2513 * The method gets the CRC-8 value from the calling routine and
2514 * sets the Other Multicast Table appropriate entry according to the
2515 * specified CRC-8 .
2516 */
2517static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2518 unsigned char crc8,
2519 int queue)
2520{
2521 unsigned int omc_table_reg;
2522 unsigned int tbl_offset;
2523 unsigned int reg_offset;
2524
2525 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2526 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2527
2528 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2529
2530 if (queue == -1) {
2531 /* Clear accepts frame bit at specified Other DA table entry */
2532 omc_table_reg &= ~(0xff << (8 * reg_offset));
2533 } else {
2534 omc_table_reg &= ~(0xff << (8 * reg_offset));
2535 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2536 }
2537
2538 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2539}
2540
2541/* The network device supports multicast using two tables:
2542 * 1) Special Multicast Table for MAC addresses of the form
2543 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2544 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2545 * Table entries in the DA-Filter table.
2546 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2547 * is used as an index to the Other Multicast Table entries in the
2548 * DA-Filter table.
2549 */
2550static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2551 int queue)
2552{
2553 unsigned char crc_result = 0;
2554
2555 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2556 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2557 return 0;
2558 }
2559
2560 crc_result = mvneta_addr_crc(p_addr);
2561 if (queue == -1) {
2562 if (pp->mcast_count[crc_result] == 0) {
2563 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2564 crc_result);
2565 return -EINVAL;
2566 }
2567
2568 pp->mcast_count[crc_result]--;
2569 if (pp->mcast_count[crc_result] != 0) {
2570 netdev_info(pp->dev,
2571 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2572 pp->mcast_count[crc_result], crc_result);
2573 return -EINVAL;
2574 }
2575 } else
2576 pp->mcast_count[crc_result]++;
2577
2578 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2579
2580 return 0;
2581}
2582
2583/* Configure Fitering mode of Ethernet port */
2584static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2585 int is_promisc)
2586{
2587 u32 port_cfg_reg, val;
2588
2589 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2590
2591 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2592
2593 /* Set / Clear UPM bit in port configuration register */
2594 if (is_promisc) {
2595 /* Accept all Unicast addresses */
2596 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2597 val |= MVNETA_FORCE_UNI;
2598 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2599 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2600 } else {
2601 /* Reject all Unicast addresses */
2602 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2603 val &= ~MVNETA_FORCE_UNI;
2604 }
2605
2606 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2607 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2608}
2609
2610/* register unicast and multicast addresses */
2611static void mvneta_set_rx_mode(struct net_device *dev)
2612{
2613 struct mvneta_port *pp = netdev_priv(dev);
2614 struct netdev_hw_addr *ha;
2615
2616 if (dev->flags & IFF_PROMISC) {
2617 /* Accept all: Multicast + Unicast */
2618 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002619 mvneta_set_ucast_table(pp, pp->rxq_def);
2620 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2621 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002622 } else {
2623 /* Accept single Unicast */
2624 mvneta_rx_unicast_promisc_set(pp, 0);
2625 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002626 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002627
2628 if (dev->flags & IFF_ALLMULTI) {
2629 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002630 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2631 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002632 } else {
2633 /* Accept only initialized multicast */
2634 mvneta_set_special_mcast_table(pp, -1);
2635 mvneta_set_other_mcast_table(pp, -1);
2636
2637 if (!netdev_mc_empty(dev)) {
2638 netdev_for_each_mc_addr(ha, dev) {
2639 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002640 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002641 }
2642 }
2643 }
2644 }
2645}
2646
2647/* Interrupt handling - the callback for request_irq() */
2648static irqreturn_t mvneta_isr(int irq, void *dev_id)
2649{
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002650 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2651
2652 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2653 napi_schedule(&pp->napi);
2654
2655 return IRQ_HANDLED;
2656}
2657
2658/* Interrupt handling - the callback for request_percpu_irq() */
2659static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2660{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002661 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002662
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002663 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002664 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002665
2666 return IRQ_HANDLED;
2667}
2668
Russell King503f9aa92018-01-02 17:24:44 +00002669static void mvneta_link_change(struct mvneta_port *pp)
Stas Sergeev898b29702015-04-01 20:32:49 +03002670{
Stas Sergeev898b29702015-04-01 20:32:49 +03002671 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2672
Russell King503f9aa92018-01-02 17:24:44 +00002673 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
Stas Sergeev898b29702015-04-01 20:32:49 +03002674}
2675
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002676/* NAPI handler
2677 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2678 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2679 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2680 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2681 * Each CPU has its own causeRxTx register
2682 */
2683static int mvneta_poll(struct napi_struct *napi, int budget)
2684{
2685 int rx_done = 0;
2686 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002687 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002688 struct mvneta_port *pp = netdev_priv(napi->dev);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002689 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002690
2691 if (!netif_running(pp->dev)) {
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002692 napi_complete(napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002693 return rx_done;
2694 }
2695
2696 /* Read cause register */
Stas Sergeev898b29702015-04-01 20:32:49 +03002697 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2698 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2699 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2700
2701 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
Russell King503f9aa92018-01-02 17:24:44 +00002702
2703 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
Russell King856b2cc2018-01-02 17:25:09 +00002704 MVNETA_CAUSE_LINK_CHANGE))
Russell King503f9aa92018-01-02 17:24:44 +00002705 mvneta_link_change(pp);
Stas Sergeev898b29702015-04-01 20:32:49 +03002706 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002707
2708 /* Release Tx descriptors */
2709 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002710 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002711 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2712 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002713
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002714 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002715 * RX packets
2716 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002717 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2718
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002719 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2720 port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002721
2722 if (rx_queue) {
2723 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002724 if (pp->bm_priv)
2725 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2726 else
2727 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002728 }
2729
Eric Dumazet6ad20162017-01-30 08:22:01 -08002730 if (rx_done < budget) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002731 cause_rx_tx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002732 napi_complete_done(napi, rx_done);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002733
2734 if (pp->neta_armada3700) {
2735 unsigned long flags;
2736
2737 local_irq_save(flags);
2738 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2739 MVNETA_RX_INTR_MASK(rxq_number) |
2740 MVNETA_TX_INTR_MASK(txq_number) |
2741 MVNETA_MISCINTR_INTR_MASK);
2742 local_irq_restore(flags);
2743 } else {
2744 enable_percpu_irq(pp->dev->irq, 0);
2745 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002746 }
2747
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002748 if (pp->neta_armada3700)
2749 pp->cause_rx_tx = cause_rx_tx;
2750 else
2751 port->cause_rx_tx = cause_rx_tx;
2752
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002753 return rx_done;
2754}
2755
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002756/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2757static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2758 int num)
2759{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002760 int i;
2761
2762 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002763 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002764 if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002765 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002766 __func__, rxq->id, i, num);
2767 break;
2768 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002769 }
2770
2771 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002772 * get packets)
2773 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002774 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2775
2776 return i;
2777}
2778
2779/* Free all packets pending transmit from all TXQs and reset TX port */
2780static void mvneta_tx_reset(struct mvneta_port *pp)
2781{
2782 int queue;
2783
Ezequiel Garcia96728502014-05-22 20:06:59 -03002784 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002785 for (queue = 0; queue < txq_number; queue++)
2786 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2787
2788 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2789 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2790}
2791
2792static void mvneta_rx_reset(struct mvneta_port *pp)
2793{
2794 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2795 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2796}
2797
2798/* Rx/Tx queue initialization/cleanup methods */
2799
Jisheng Zhang4a188a62018-04-02 11:23:43 +08002800static int mvneta_rxq_sw_init(struct mvneta_port *pp,
2801 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002802{
2803 rxq->size = pp->rx_ring_size;
2804
2805 /* Allocate memory for RX descriptors */
2806 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2807 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2808 &rxq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002809 if (!rxq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002810 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002811
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002812 rxq->last_desc = rxq->size - 1;
2813
Jisheng Zhang4a188a62018-04-02 11:23:43 +08002814 return 0;
2815}
2816
2817static void mvneta_rxq_hw_init(struct mvneta_port *pp,
2818 struct mvneta_rx_queue *rxq)
2819{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002820 /* Set Rx descriptors queue starting address */
2821 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2822 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2823
2824 /* Set Offset */
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01002825 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002826
2827 /* Set coalescing pkts and time */
2828 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2829 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2830
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002831 if (!pp->bm_priv) {
2832 /* Fill RXQ with buffers from RX pool */
2833 mvneta_rxq_buf_size_set(pp, rxq,
2834 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2835 mvneta_rxq_bm_disable(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002836 mvneta_rxq_fill(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002837 } else {
2838 mvneta_rxq_bm_enable(pp, rxq);
2839 mvneta_rxq_long_pool_set(pp, rxq);
2840 mvneta_rxq_short_pool_set(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002841 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002842 }
Jisheng Zhang4a188a62018-04-02 11:23:43 +08002843}
2844
2845/* Create a specified RX queue */
2846static int mvneta_rxq_init(struct mvneta_port *pp,
2847 struct mvneta_rx_queue *rxq)
2848
2849{
2850 int ret;
2851
2852 ret = mvneta_rxq_sw_init(pp, rxq);
2853 if (ret < 0)
2854 return ret;
2855
2856 mvneta_rxq_hw_init(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002857
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002858 return 0;
2859}
2860
2861/* Cleanup Rx queue */
2862static void mvneta_rxq_deinit(struct mvneta_port *pp,
2863 struct mvneta_rx_queue *rxq)
2864{
2865 mvneta_rxq_drop_pkts(pp, rxq);
2866
2867 if (rxq->descs)
2868 dma_free_coherent(pp->dev->dev.parent,
2869 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2870 rxq->descs,
2871 rxq->descs_phys);
2872
2873 rxq->descs = NULL;
2874 rxq->last_desc = 0;
2875 rxq->next_desc_to_proc = 0;
2876 rxq->descs_phys = 0;
2877}
2878
Jisheng Zhang4a188a62018-04-02 11:23:43 +08002879static int mvneta_txq_sw_init(struct mvneta_port *pp,
2880 struct mvneta_tx_queue *txq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002881{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002882 int cpu;
2883
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002884 txq->size = pp->tx_ring_size;
2885
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002886 /* A queue must always have room for at least one skb.
2887 * Therefore, stop the queue when the free entries reaches
2888 * the maximum number of descriptors per skb.
2889 */
2890 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2891 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2892
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002893 /* Allocate memory for TX descriptors */
2894 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2895 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2896 &txq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002897 if (!txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002898 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002899
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002900 txq->last_desc = txq->size - 1;
2901
Markus Elfringd441b682017-04-16 22:11:22 +02002902 txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
2903 GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002904 if (!txq->tx_skb) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002905 dma_free_coherent(pp->dev->dev.parent,
2906 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2907 txq->descs, txq->descs_phys);
2908 return -ENOMEM;
2909 }
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002910
2911 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2912 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2913 txq->size * TSO_HEADER_SIZE,
2914 &txq->tso_hdrs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002915 if (!txq->tso_hdrs) {
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002916 kfree(txq->tx_skb);
2917 dma_free_coherent(pp->dev->dev.parent,
2918 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2919 txq->descs, txq->descs_phys);
2920 return -ENOMEM;
2921 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002922
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002923 /* Setup XPS mapping */
2924 if (txq_number > 1)
2925 cpu = txq->id % num_present_cpus();
2926 else
2927 cpu = pp->rxq_def % num_present_cpus();
2928 cpumask_set_cpu(cpu, &txq->affinity_mask);
2929 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2930
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002931 return 0;
2932}
2933
Jisheng Zhang4a188a62018-04-02 11:23:43 +08002934static void mvneta_txq_hw_init(struct mvneta_port *pp,
2935 struct mvneta_tx_queue *txq)
2936{
2937 /* Set maximum bandwidth for enabled TXQs */
2938 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2939 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2940
2941 /* Set Tx descriptors queue starting address */
2942 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2943 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2944
2945 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2946}
2947
2948/* Create and initialize a tx queue */
2949static int mvneta_txq_init(struct mvneta_port *pp,
2950 struct mvneta_tx_queue *txq)
2951{
2952 int ret;
2953
2954 ret = mvneta_txq_sw_init(pp, txq);
2955 if (ret < 0)
2956 return ret;
2957
2958 mvneta_txq_hw_init(pp, txq);
2959
2960 return 0;
2961}
2962
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002963/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
Jisheng Zhang4a188a62018-04-02 11:23:43 +08002964static void mvneta_txq_sw_deinit(struct mvneta_port *pp,
2965 struct mvneta_tx_queue *txq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002966{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002967 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2968
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002969 kfree(txq->tx_skb);
2970
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002971 if (txq->tso_hdrs)
2972 dma_free_coherent(pp->dev->dev.parent,
2973 txq->size * TSO_HEADER_SIZE,
2974 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002975 if (txq->descs)
2976 dma_free_coherent(pp->dev->dev.parent,
2977 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2978 txq->descs, txq->descs_phys);
2979
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002980 netdev_tx_reset_queue(nq);
2981
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002982 txq->descs = NULL;
2983 txq->last_desc = 0;
2984 txq->next_desc_to_proc = 0;
2985 txq->descs_phys = 0;
Jisheng Zhang4a188a62018-04-02 11:23:43 +08002986}
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002987
Jisheng Zhang4a188a62018-04-02 11:23:43 +08002988static void mvneta_txq_hw_deinit(struct mvneta_port *pp,
2989 struct mvneta_tx_queue *txq)
2990{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002991 /* Set minimum bandwidth for disabled TXQs */
2992 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2993 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2994
2995 /* Set Tx descriptors queue starting address and size */
2996 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2997 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2998}
2999
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003000static void mvneta_txq_deinit(struct mvneta_port *pp,
3001 struct mvneta_tx_queue *txq)
3002{
3003 mvneta_txq_sw_deinit(pp, txq);
3004 mvneta_txq_hw_deinit(pp, txq);
3005}
3006
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003007/* Cleanup all Tx queues */
3008static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3009{
3010 int queue;
3011
3012 for (queue = 0; queue < txq_number; queue++)
3013 mvneta_txq_deinit(pp, &pp->txqs[queue]);
3014}
3015
3016/* Cleanup all Rx queues */
3017static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3018{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003019 int queue;
3020
Yelena Krivosheevca5902a2017-12-19 17:59:46 +01003021 for (queue = 0; queue < rxq_number; queue++)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003022 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003023}
3024
3025
3026/* Init all Rx queues */
3027static int mvneta_setup_rxqs(struct mvneta_port *pp)
3028{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003029 int queue;
3030
3031 for (queue = 0; queue < rxq_number; queue++) {
3032 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3033
3034 if (err) {
3035 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3036 __func__, queue);
3037 mvneta_cleanup_rxqs(pp);
3038 return err;
3039 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003040 }
3041
3042 return 0;
3043}
3044
3045/* Init all tx queues */
3046static int mvneta_setup_txqs(struct mvneta_port *pp)
3047{
3048 int queue;
3049
3050 for (queue = 0; queue < txq_number; queue++) {
3051 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3052 if (err) {
3053 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3054 __func__, queue);
3055 mvneta_cleanup_txqs(pp);
3056 return err;
3057 }
3058 }
3059
3060 return 0;
3061}
3062
3063static void mvneta_start_dev(struct mvneta_port *pp)
3064{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003065 int cpu;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003066
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003067 mvneta_max_rx_size_set(pp, pp->pkt_size);
3068 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3069
3070 /* start the Rx/Tx activity */
3071 mvneta_port_enable(pp);
3072
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003073 if (!pp->neta_armada3700) {
3074 /* Enable polling on the port */
3075 for_each_online_cpu(cpu) {
3076 struct mvneta_pcpu_port *port =
3077 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003078
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003079 napi_enable(&port->napi);
3080 }
3081 } else {
3082 napi_enable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003083 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003084
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003085 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003086 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3087
Stas Sergeev898b29702015-04-01 20:32:49 +03003088 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3089 MVNETA_CAUSE_PHY_STATUS_CHANGE |
Russell King856b2cc2018-01-02 17:25:09 +00003090 MVNETA_CAUSE_LINK_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003091
Russell King503f9aa92018-01-02 17:24:44 +00003092 phylink_start(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003093 netif_tx_start_all_queues(pp->dev);
3094}
3095
3096static void mvneta_stop_dev(struct mvneta_port *pp)
3097{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003098 unsigned int cpu;
3099
Russell King503f9aa92018-01-02 17:24:44 +00003100 phylink_stop(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003101
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003102 if (!pp->neta_armada3700) {
3103 for_each_online_cpu(cpu) {
3104 struct mvneta_pcpu_port *port =
3105 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003106
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003107 napi_disable(&port->napi);
3108 }
3109 } else {
3110 napi_disable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003111 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003112
3113 netif_carrier_off(pp->dev);
3114
3115 mvneta_port_down(pp);
3116 netif_tx_stop_all_queues(pp->dev);
3117
3118 /* Stop the port activity */
3119 mvneta_port_disable(pp);
3120
3121 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003122 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003123
3124 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003125 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003126
3127 mvneta_tx_reset(pp);
3128 mvneta_rx_reset(pp);
3129}
3130
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003131static void mvneta_percpu_enable(void *arg)
3132{
3133 struct mvneta_port *pp = arg;
3134
3135 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3136}
3137
3138static void mvneta_percpu_disable(void *arg)
3139{
3140 struct mvneta_port *pp = arg;
3141
3142 disable_percpu_irq(pp->dev->irq);
3143}
3144
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003145/* Change the device mtu */
3146static int mvneta_change_mtu(struct net_device *dev, int mtu)
3147{
3148 struct mvneta_port *pp = netdev_priv(dev);
3149 int ret;
3150
Jarod Wilson57779872016-10-17 15:54:06 -04003151 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3152 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3153 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3154 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3155 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003156
3157 dev->mtu = mtu;
3158
Simon Guinotb65657f2015-06-30 16:20:22 +02003159 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003160 if (pp->bm_priv)
3161 mvneta_bm_update_mtu(pp, mtu);
3162
Simon Guinotb65657f2015-06-30 16:20:22 +02003163 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003164 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003165 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003166
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003167 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003168 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003169 */
3170 mvneta_stop_dev(pp);
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003171 on_each_cpu(mvneta_percpu_disable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003172
3173 mvneta_cleanup_txqs(pp);
3174 mvneta_cleanup_rxqs(pp);
3175
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003176 if (pp->bm_priv)
3177 mvneta_bm_update_mtu(pp, mtu);
3178
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003179 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003180 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3181 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003182
3183 ret = mvneta_setup_rxqs(pp);
3184 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003185 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003186 return ret;
3187 }
3188
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003189 ret = mvneta_setup_txqs(pp);
3190 if (ret) {
3191 netdev_err(dev, "unable to setup txqs after MTU change\n");
3192 return ret;
3193 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003194
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003195 on_each_cpu(mvneta_percpu_enable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003196 mvneta_start_dev(pp);
3197 mvneta_port_up(pp);
3198
Simon Guinotb65657f2015-06-30 16:20:22 +02003199 netdev_update_features(dev);
3200
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003201 return 0;
3202}
3203
Simon Guinotb65657f2015-06-30 16:20:22 +02003204static netdev_features_t mvneta_fix_features(struct net_device *dev,
3205 netdev_features_t features)
3206{
3207 struct mvneta_port *pp = netdev_priv(dev);
3208
3209 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3210 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3211 netdev_info(dev,
3212 "Disable IP checksum for MTU greater than %dB\n",
3213 pp->tx_csum_limit);
3214 }
3215
3216 return features;
3217}
3218
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003219/* Get mac address */
3220static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3221{
3222 u32 mac_addr_l, mac_addr_h;
3223
3224 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3225 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3226 addr[0] = (mac_addr_h >> 24) & 0xFF;
3227 addr[1] = (mac_addr_h >> 16) & 0xFF;
3228 addr[2] = (mac_addr_h >> 8) & 0xFF;
3229 addr[3] = mac_addr_h & 0xFF;
3230 addr[4] = (mac_addr_l >> 8) & 0xFF;
3231 addr[5] = mac_addr_l & 0xFF;
3232}
3233
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003234/* Handle setting mac address */
3235static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3236{
3237 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003238 struct sockaddr *sockaddr = addr;
3239 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003240
Ezequiel Garciae68de362014-05-22 20:07:00 -03003241 ret = eth_prepare_mac_addr_change(dev, addr);
3242 if (ret < 0)
3243 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003244 /* Remove previous address table entry */
3245 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3246
3247 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003248 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003249
Ezequiel Garciae68de362014-05-22 20:07:00 -03003250 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003251 return 0;
3252}
3253
Russell King503f9aa92018-01-02 17:24:44 +00003254static void mvneta_validate(struct net_device *ndev, unsigned long *supported,
3255 struct phylink_link_state *state)
Russell Kingfc548b92018-01-02 17:24:39 +00003256{
Russell King503f9aa92018-01-02 17:24:44 +00003257 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
Russell Kingfc548b92018-01-02 17:24:39 +00003258
Russell King22f4bf82018-01-02 17:24:54 +00003259 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
Russell King503f9aa92018-01-02 17:24:44 +00003260 if (state->interface != PHY_INTERFACE_MODE_NA &&
3261 state->interface != PHY_INTERFACE_MODE_QSGMII &&
3262 state->interface != PHY_INTERFACE_MODE_SGMII &&
Russell King22f4bf82018-01-02 17:24:54 +00003263 !phy_interface_mode_is_8023z(state->interface) &&
Russell King503f9aa92018-01-02 17:24:44 +00003264 !phy_interface_mode_is_rgmii(state->interface)) {
3265 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3266 return;
Russell Kingfc548b92018-01-02 17:24:39 +00003267 }
Russell King503f9aa92018-01-02 17:24:44 +00003268
3269 /* Allow all the expected bits */
3270 phylink_set(mask, Autoneg);
3271 phylink_set_port_modes(mask);
3272
Russell King4932a912018-01-02 17:24:59 +00003273 /* Asymmetric pause is unsupported */
3274 phylink_set(mask, Pause);
Russell King503f9aa92018-01-02 17:24:44 +00003275 /* Half-duplex at speeds higher than 100Mbit is unsupported */
3276 phylink_set(mask, 1000baseT_Full);
3277 phylink_set(mask, 1000baseX_Full);
Russell King22f4bf82018-01-02 17:24:54 +00003278
3279 if (!phy_interface_mode_is_8023z(state->interface)) {
3280 /* 10M and 100M are only supported in non-802.3z mode */
3281 phylink_set(mask, 10baseT_Half);
3282 phylink_set(mask, 10baseT_Full);
3283 phylink_set(mask, 100baseT_Half);
3284 phylink_set(mask, 100baseT_Full);
3285 }
Russell King503f9aa92018-01-02 17:24:44 +00003286
3287 bitmap_and(supported, supported, mask,
3288 __ETHTOOL_LINK_MODE_MASK_NBITS);
3289 bitmap_and(state->advertising, state->advertising, mask,
3290 __ETHTOOL_LINK_MODE_MASK_NBITS);
Russell Kingfc548b92018-01-02 17:24:39 +00003291}
3292
Russell King503f9aa92018-01-02 17:24:44 +00003293static int mvneta_mac_link_state(struct net_device *ndev,
3294 struct phylink_link_state *state)
3295{
3296 struct mvneta_port *pp = netdev_priv(ndev);
3297 u32 gmac_stat;
3298
3299 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3300
3301 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3302 state->speed = SPEED_1000;
3303 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3304 state->speed = SPEED_100;
3305 else
3306 state->speed = SPEED_10;
3307
3308 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3309 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3310 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3311
3312 state->pause = 0;
Russell King4932a912018-01-02 17:24:59 +00003313 if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
3314 state->pause |= MLO_PAUSE_RX;
3315 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
3316 state->pause |= MLO_PAUSE_TX;
Russell King503f9aa92018-01-02 17:24:44 +00003317
3318 return 1;
3319}
3320
Russell King22f4bf82018-01-02 17:24:54 +00003321static void mvneta_mac_an_restart(struct net_device *ndev)
3322{
3323 struct mvneta_port *pp = netdev_priv(ndev);
3324 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3325
3326 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3327 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
3328 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3329 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
3330}
3331
Russell King503f9aa92018-01-02 17:24:44 +00003332static void mvneta_mac_config(struct net_device *ndev, unsigned int mode,
3333 const struct phylink_link_state *state)
3334{
3335 struct mvneta_port *pp = netdev_priv(ndev);
Russell King22f4bf82018-01-02 17:24:54 +00003336 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
Russell King503f9aa92018-01-02 17:24:44 +00003337 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3338 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3339 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3340
Russell King22f4bf82018-01-02 17:24:54 +00003341 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
Russell King32699952018-01-02 17:24:49 +00003342 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
3343 MVNETA_GMAC2_PORT_RESET);
Russell King503f9aa92018-01-02 17:24:44 +00003344 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3345 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3346 MVNETA_GMAC_INBAND_RESTART_AN |
3347 MVNETA_GMAC_CONFIG_MII_SPEED |
3348 MVNETA_GMAC_CONFIG_GMII_SPEED |
3349 MVNETA_GMAC_AN_SPEED_EN |
Russell King22f4bf82018-01-02 17:24:54 +00003350 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
3351 MVNETA_GMAC_CONFIG_FLOW_CTRL |
Russell King503f9aa92018-01-02 17:24:44 +00003352 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3353 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
3354 MVNETA_GMAC_AN_DUPLEX_EN);
3355
Russell King32699952018-01-02 17:24:49 +00003356 /* Even though it might look weird, when we're configured in
3357 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3358 */
3359 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
3360
3361 if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
Russell King22f4bf82018-01-02 17:24:54 +00003362 state->interface == PHY_INTERFACE_MODE_SGMII ||
3363 phy_interface_mode_is_8023z(state->interface))
Russell King32699952018-01-02 17:24:49 +00003364 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
3365
Russell King4932a912018-01-02 17:24:59 +00003366 if (phylink_test(state->advertising, Pause))
3367 new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3368 if (state->pause & MLO_PAUSE_TXRX_MASK)
3369 new_an |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
3370
Russell King503f9aa92018-01-02 17:24:44 +00003371 if (!phylink_autoneg_inband(mode)) {
3372 /* Phy or fixed speed */
3373 if (state->duplex)
3374 new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3375
3376 if (state->speed == SPEED_1000)
3377 new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3378 else if (state->speed == SPEED_100)
3379 new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
Russell King22f4bf82018-01-02 17:24:54 +00003380 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Russell King503f9aa92018-01-02 17:24:44 +00003381 /* SGMII mode receives the state from the PHY */
3382 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3383 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3384 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3385 MVNETA_GMAC_FORCE_LINK_PASS)) |
3386 MVNETA_GMAC_INBAND_AN_ENABLE |
3387 MVNETA_GMAC_AN_SPEED_EN |
3388 MVNETA_GMAC_AN_DUPLEX_EN;
Russell King22f4bf82018-01-02 17:24:54 +00003389 } else {
3390 /* 802.3z negotiation - only 1000base-X */
3391 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
3392 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3393 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3394 MVNETA_GMAC_FORCE_LINK_PASS)) |
3395 MVNETA_GMAC_INBAND_AN_ENABLE |
3396 MVNETA_GMAC_CONFIG_GMII_SPEED |
3397 /* The MAC only supports FD mode */
3398 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
Russell King4932a912018-01-02 17:24:59 +00003399
3400 if (state->pause & MLO_PAUSE_AN && state->an_enabled)
3401 new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
Russell King503f9aa92018-01-02 17:24:44 +00003402 }
3403
3404 /* Armada 370 documentation says we can only change the port mode
3405 * and in-band enable when the link is down, so force it down
3406 * while making these changes. We also do this for GMAC_CTRL2 */
Russell King22f4bf82018-01-02 17:24:54 +00003407 if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
3408 (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
Russell King503f9aa92018-01-02 17:24:44 +00003409 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3410 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3411 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3412 MVNETA_GMAC_FORCE_LINK_DOWN);
3413 }
3414
Russell King22f4bf82018-01-02 17:24:54 +00003415 if (new_ctrl0 != gmac_ctrl0)
3416 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
Russell King503f9aa92018-01-02 17:24:44 +00003417 if (new_ctrl2 != gmac_ctrl2)
3418 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
3419 if (new_clk != gmac_clk)
3420 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3421 if (new_an != gmac_an)
3422 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
Russell King32699952018-01-02 17:24:49 +00003423
3424 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
3425 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3426 MVNETA_GMAC2_PORT_RESET) != 0)
3427 continue;
3428 }
Russell King503f9aa92018-01-02 17:24:44 +00003429}
3430
Russell King6d81f452018-01-02 17:25:04 +00003431static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
3432{
3433 u32 lpi_ctl1;
3434
3435 lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
3436 if (enable)
3437 lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
3438 else
3439 lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
3440 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
3441}
3442
Florian Fainellic6ab3002018-03-28 15:44:15 -07003443static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode,
3444 phy_interface_t interface)
Russell Kingfc548b92018-01-02 17:24:39 +00003445{
3446 struct mvneta_port *pp = netdev_priv(ndev);
3447 u32 val;
3448
Russell King503f9aa92018-01-02 17:24:44 +00003449 mvneta_port_down(pp);
3450
3451 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003452 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3453 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3454 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3455 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3456 }
Russell King6d81f452018-01-02 17:25:04 +00003457
3458 pp->eee_active = false;
3459 mvneta_set_eee(pp, false);
Russell Kingfc548b92018-01-02 17:24:39 +00003460}
3461
Russell King503f9aa92018-01-02 17:24:44 +00003462static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode,
Florian Fainellic6ab3002018-03-28 15:44:15 -07003463 phy_interface_t interface,
Russell King503f9aa92018-01-02 17:24:44 +00003464 struct phy_device *phy)
Russell Kingfc548b92018-01-02 17:24:39 +00003465{
3466 struct mvneta_port *pp = netdev_priv(ndev);
3467 u32 val;
3468
Russell King503f9aa92018-01-02 17:24:44 +00003469 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003470 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3471 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3472 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3473 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3474 }
3475
3476 mvneta_port_up(pp);
Russell King6d81f452018-01-02 17:25:04 +00003477
3478 if (phy && pp->eee_enabled) {
3479 pp->eee_active = phy_init_eee(phy, 0) >= 0;
3480 mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
3481 }
Russell Kingfc548b92018-01-02 17:24:39 +00003482}
3483
Russell King503f9aa92018-01-02 17:24:44 +00003484static const struct phylink_mac_ops mvneta_phylink_ops = {
3485 .validate = mvneta_validate,
3486 .mac_link_state = mvneta_mac_link_state,
Russell King22f4bf82018-01-02 17:24:54 +00003487 .mac_an_restart = mvneta_mac_an_restart,
Russell King503f9aa92018-01-02 17:24:44 +00003488 .mac_config = mvneta_mac_config,
3489 .mac_link_down = mvneta_mac_link_down,
3490 .mac_link_up = mvneta_mac_link_up,
3491};
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003492
3493static int mvneta_mdio_probe(struct mvneta_port *pp)
3494{
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003495 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Russell King503f9aa92018-01-02 17:24:44 +00003496 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003497
Russell King503f9aa92018-01-02 17:24:44 +00003498 if (err)
3499 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003500
Russell King503f9aa92018-01-02 17:24:44 +00003501 phylink_ethtool_get_wol(pp->phylink, &wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003502 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
3503
Russell King503f9aa92018-01-02 17:24:44 +00003504 return err;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003505}
3506
3507static void mvneta_mdio_remove(struct mvneta_port *pp)
3508{
Russell King503f9aa92018-01-02 17:24:44 +00003509 phylink_disconnect_phy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003510}
3511
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003512/* Electing a CPU must be done in an atomic way: it should be done
3513 * after or before the removal/insertion of a CPU and this function is
3514 * not reentrant.
3515 */
Maxime Ripardf8642882015-09-25 18:09:38 +02003516static void mvneta_percpu_elect(struct mvneta_port *pp)
3517{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003518 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003519
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003520 /* Use the cpu associated to the rxq when it is online, in all
3521 * the other cases, use the cpu 0 which can't be offline.
3522 */
3523 if (cpu_online(pp->rxq_def))
3524 elected_cpu = pp->rxq_def;
3525
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003526 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02003527
3528 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003529 int rxq_map = 0, txq_map = 0;
3530 int rxq;
3531
3532 for (rxq = 0; rxq < rxq_number; rxq++)
3533 if ((rxq % max_cpu) == cpu)
3534 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3535
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003536 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003537 /* Map the default receive queue queue to the
3538 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02003539 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003540 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003541
3542 /* We update the TX queue map only if we have one
3543 * queue. In this case we associate the TX queue to
3544 * the CPU bound to the default RX queue
3545 */
3546 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003547 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003548 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3549 else
3550 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3551 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3552
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003553 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3554
3555 /* Update the interrupt mask on each CPU according the
3556 * new mapping
3557 */
3558 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3559 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003560 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003561
Maxime Ripardf8642882015-09-25 18:09:38 +02003562 }
3563};
3564
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003565static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
Maxime Ripardf8642882015-09-25 18:09:38 +02003566{
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003567 int other_cpu;
3568 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3569 node_online);
Maxime Ripardf8642882015-09-25 18:09:38 +02003570 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3571
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003572
3573 spin_lock(&pp->lock);
3574 /*
3575 * Configuring the driver for a new CPU while the driver is
3576 * stopping is racy, so just avoid it.
3577 */
3578 if (pp->is_stopped) {
3579 spin_unlock(&pp->lock);
3580 return 0;
3581 }
3582 netif_tx_stop_all_queues(pp->dev);
3583
3584 /*
3585 * We have to synchronise on tha napi of each CPU except the one
3586 * just being woken up
3587 */
3588 for_each_online_cpu(other_cpu) {
3589 if (other_cpu != cpu) {
3590 struct mvneta_pcpu_port *other_port =
3591 per_cpu_ptr(pp->ports, other_cpu);
3592
3593 napi_synchronize(&other_port->napi);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003594 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003595 }
3596
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003597 /* Mask all ethernet port interrupts */
3598 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3599 napi_enable(&port->napi);
3600
3601 /*
3602 * Enable per-CPU interrupts on the CPU that is
3603 * brought up.
3604 */
3605 mvneta_percpu_enable(pp);
3606
3607 /*
3608 * Enable per-CPU interrupt on the one CPU we care
3609 * about.
3610 */
3611 mvneta_percpu_elect(pp);
3612
3613 /* Unmask all ethernet port interrupts */
3614 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3615 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3616 MVNETA_CAUSE_PHY_STATUS_CHANGE |
Russell King856b2cc2018-01-02 17:25:09 +00003617 MVNETA_CAUSE_LINK_CHANGE);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003618 netif_tx_start_all_queues(pp->dev);
3619 spin_unlock(&pp->lock);
3620 return 0;
3621}
3622
3623static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3624{
3625 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3626 node_online);
3627 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3628
3629 /*
3630 * Thanks to this lock we are sure that any pending cpu election is
3631 * done.
3632 */
3633 spin_lock(&pp->lock);
3634 /* Mask all ethernet port interrupts */
3635 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3636 spin_unlock(&pp->lock);
3637
3638 napi_synchronize(&port->napi);
3639 napi_disable(&port->napi);
3640 /* Disable per-CPU interrupts on the CPU that is brought down. */
3641 mvneta_percpu_disable(pp);
3642 return 0;
3643}
3644
3645static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3646{
3647 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3648 node_dead);
3649
3650 /* Check if a new CPU must be elected now this on is down */
3651 spin_lock(&pp->lock);
3652 mvneta_percpu_elect(pp);
3653 spin_unlock(&pp->lock);
3654 /* Unmask all ethernet port interrupts */
3655 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3656 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3657 MVNETA_CAUSE_PHY_STATUS_CHANGE |
Russell King856b2cc2018-01-02 17:25:09 +00003658 MVNETA_CAUSE_LINK_CHANGE);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003659 netif_tx_start_all_queues(pp->dev);
3660 return 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003661}
3662
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003663static int mvneta_open(struct net_device *dev)
3664{
3665 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003666 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003667
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003668 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003669 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3670 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003671
3672 ret = mvneta_setup_rxqs(pp);
3673 if (ret)
3674 return ret;
3675
3676 ret = mvneta_setup_txqs(pp);
3677 if (ret)
3678 goto err_cleanup_rxqs;
3679
3680 /* Connect to port interrupt line */
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003681 if (pp->neta_armada3700)
3682 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3683 dev->name, pp);
3684 else
3685 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3686 dev->name, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003687 if (ret) {
3688 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3689 goto err_cleanup_txqs;
3690 }
3691
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003692 if (!pp->neta_armada3700) {
3693 /* Enable per-CPU interrupt on all the CPU to handle our RX
3694 * queue interrupts
3695 */
3696 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003697
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003698 pp->is_stopped = false;
3699 /* Register a CPU notifier to handle the case where our CPU
3700 * might be taken offline.
3701 */
3702 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3703 &pp->node_online);
3704 if (ret)
3705 goto err_free_irq;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003706
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003707 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3708 &pp->node_dead);
3709 if (ret)
3710 goto err_free_online_hp;
3711 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003712
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003713 /* In default link is down */
3714 netif_carrier_off(pp->dev);
3715
3716 ret = mvneta_mdio_probe(pp);
3717 if (ret < 0) {
3718 netdev_err(dev, "cannot probe MDIO bus\n");
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003719 goto err_free_dead_hp;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003720 }
3721
3722 mvneta_start_dev(pp);
3723
3724 return 0;
3725
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003726err_free_dead_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003727 if (!pp->neta_armada3700)
3728 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3729 &pp->node_dead);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003730err_free_online_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003731 if (!pp->neta_armada3700)
3732 cpuhp_state_remove_instance_nocalls(online_hpstate,
3733 &pp->node_online);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003734err_free_irq:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003735 if (pp->neta_armada3700) {
3736 free_irq(pp->dev->irq, pp);
3737 } else {
3738 on_each_cpu(mvneta_percpu_disable, pp, true);
3739 free_percpu_irq(pp->dev->irq, pp->ports);
3740 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003741err_cleanup_txqs:
3742 mvneta_cleanup_txqs(pp);
3743err_cleanup_rxqs:
3744 mvneta_cleanup_rxqs(pp);
3745 return ret;
3746}
3747
3748/* Stop the port, free port interrupt line */
3749static int mvneta_stop(struct net_device *dev)
3750{
3751 struct mvneta_port *pp = netdev_priv(dev);
3752
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003753 if (!pp->neta_armada3700) {
3754 /* Inform that we are stopping so we don't want to setup the
3755 * driver for new CPUs in the notifiers. The code of the
3756 * notifier for CPU online is protected by the same spinlock,
3757 * so when we get the lock, the notifer work is done.
3758 */
3759 spin_lock(&pp->lock);
3760 pp->is_stopped = true;
3761 spin_unlock(&pp->lock);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003762
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003763 mvneta_stop_dev(pp);
3764 mvneta_mdio_remove(pp);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003765
Dan Carpenterd26aac22016-12-07 14:32:17 +03003766 cpuhp_state_remove_instance_nocalls(online_hpstate,
3767 &pp->node_online);
3768 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3769 &pp->node_dead);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003770 on_each_cpu(mvneta_percpu_disable, pp, true);
3771 free_percpu_irq(dev->irq, pp->ports);
3772 } else {
3773 mvneta_stop_dev(pp);
3774 mvneta_mdio_remove(pp);
3775 free_irq(dev->irq, pp);
3776 }
3777
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003778 mvneta_cleanup_rxqs(pp);
3779 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003780
3781 return 0;
3782}
3783
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003784static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3785{
Russell King503f9aa92018-01-02 17:24:44 +00003786 struct mvneta_port *pp = netdev_priv(dev);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003787
Russell King503f9aa92018-01-02 17:24:44 +00003788 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003789}
3790
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003791/* Ethtool methods */
3792
Philippe Reynes013ad402016-07-30 17:42:12 +02003793/* Set link ksettings (phy address, speed) for ethtools */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +08003794static int
3795mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3796 const struct ethtool_link_ksettings *cmd)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003797{
Philippe Reynes013ad402016-07-30 17:42:12 +02003798 struct mvneta_port *pp = netdev_priv(ndev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003799
Russell King503f9aa92018-01-02 17:24:44 +00003800 return phylink_ethtool_ksettings_set(pp->phylink, cmd);
3801}
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003802
Russell King503f9aa92018-01-02 17:24:44 +00003803/* Get link ksettings for ethtools */
3804static int
3805mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
3806 struct ethtool_link_ksettings *cmd)
3807{
3808 struct mvneta_port *pp = netdev_priv(ndev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003809
Russell King503f9aa92018-01-02 17:24:44 +00003810 return phylink_ethtool_ksettings_get(pp->phylink, cmd);
3811}
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003812
Russell King503f9aa92018-01-02 17:24:44 +00003813static int mvneta_ethtool_nway_reset(struct net_device *dev)
3814{
3815 struct mvneta_port *pp = netdev_priv(dev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003816
Russell King503f9aa92018-01-02 17:24:44 +00003817 return phylink_ethtool_nway_reset(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003818}
3819
3820/* Set interrupt coalescing for ethtools */
3821static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3822 struct ethtool_coalesce *c)
3823{
3824 struct mvneta_port *pp = netdev_priv(dev);
3825 int queue;
3826
3827 for (queue = 0; queue < rxq_number; queue++) {
3828 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3829 rxq->time_coal = c->rx_coalesce_usecs;
3830 rxq->pkts_coal = c->rx_max_coalesced_frames;
3831 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3832 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3833 }
3834
3835 for (queue = 0; queue < txq_number; queue++) {
3836 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3837 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3838 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3839 }
3840
3841 return 0;
3842}
3843
3844/* get coalescing for ethtools */
3845static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3846 struct ethtool_coalesce *c)
3847{
3848 struct mvneta_port *pp = netdev_priv(dev);
3849
3850 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3851 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3852
3853 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3854 return 0;
3855}
3856
3857
3858static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3859 struct ethtool_drvinfo *drvinfo)
3860{
3861 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3862 sizeof(drvinfo->driver));
3863 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3864 sizeof(drvinfo->version));
3865 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3866 sizeof(drvinfo->bus_info));
3867}
3868
3869
3870static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3871 struct ethtool_ringparam *ring)
3872{
3873 struct mvneta_port *pp = netdev_priv(netdev);
3874
3875 ring->rx_max_pending = MVNETA_MAX_RXD;
3876 ring->tx_max_pending = MVNETA_MAX_TXD;
3877 ring->rx_pending = pp->rx_ring_size;
3878 ring->tx_pending = pp->tx_ring_size;
3879}
3880
3881static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3882 struct ethtool_ringparam *ring)
3883{
3884 struct mvneta_port *pp = netdev_priv(dev);
3885
3886 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3887 return -EINVAL;
3888 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3889 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003890
3891 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3892 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3893 if (pp->tx_ring_size != ring->tx_pending)
3894 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3895 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003896
3897 if (netif_running(dev)) {
3898 mvneta_stop(dev);
3899 if (mvneta_open(dev)) {
3900 netdev_err(dev,
3901 "error on opening device after ring param change\n");
3902 return -ENOMEM;
3903 }
3904 }
3905
3906 return 0;
3907}
3908
Russell King4932a912018-01-02 17:24:59 +00003909static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
3910 struct ethtool_pauseparam *pause)
3911{
3912 struct mvneta_port *pp = netdev_priv(dev);
3913
3914 phylink_ethtool_get_pauseparam(pp->phylink, pause);
3915}
3916
3917static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
3918 struct ethtool_pauseparam *pause)
3919{
3920 struct mvneta_port *pp = netdev_priv(dev);
3921
3922 return phylink_ethtool_set_pauseparam(pp->phylink, pause);
3923}
3924
Russell King9b0cdef2015-10-22 18:37:30 +01003925static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3926 u8 *data)
3927{
3928 if (sset == ETH_SS_STATS) {
3929 int i;
3930
3931 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3932 memcpy(data + i * ETH_GSTRING_LEN,
3933 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3934 }
3935}
3936
3937static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3938{
3939 const struct mvneta_statistic *s;
3940 void __iomem *base = pp->base;
Russell King6d81f452018-01-02 17:25:04 +00003941 u32 high, low;
3942 u64 val;
Russell King9b0cdef2015-10-22 18:37:30 +01003943 int i;
3944
3945 for (i = 0, s = mvneta_statistics;
3946 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3947 s++, i++) {
Russell King6d81f452018-01-02 17:25:04 +00003948 val = 0;
3949
Russell King9b0cdef2015-10-22 18:37:30 +01003950 switch (s->type) {
3951 case T_REG_32:
3952 val = readl_relaxed(base + s->offset);
3953 break;
3954 case T_REG_64:
3955 /* Docs say to read low 32-bit then high */
3956 low = readl_relaxed(base + s->offset);
3957 high = readl_relaxed(base + s->offset + 4);
Russell King6d81f452018-01-02 17:25:04 +00003958 val = (u64)high << 32 | low;
3959 break;
3960 case T_SW:
3961 switch (s->offset) {
3962 case ETHTOOL_STAT_EEE_WAKEUP:
3963 val = phylink_get_eee_err(pp->phylink);
3964 break;
3965 }
Russell King9b0cdef2015-10-22 18:37:30 +01003966 break;
3967 }
Russell King6d81f452018-01-02 17:25:04 +00003968
3969 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003970 }
3971}
3972
3973static void mvneta_ethtool_get_stats(struct net_device *dev,
3974 struct ethtool_stats *stats, u64 *data)
3975{
3976 struct mvneta_port *pp = netdev_priv(dev);
3977 int i;
3978
3979 mvneta_ethtool_update_stats(pp);
3980
3981 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3982 *data++ = pp->ethtool_stats[i];
3983}
3984
3985static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3986{
3987 if (sset == ETH_SS_STATS)
3988 return ARRAY_SIZE(mvneta_statistics);
3989 return -EOPNOTSUPP;
3990}
3991
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003992static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3993{
3994 return MVNETA_RSS_LU_TABLE_SIZE;
3995}
3996
3997static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3998 struct ethtool_rxnfc *info,
3999 u32 *rules __always_unused)
4000{
4001 switch (info->cmd) {
4002 case ETHTOOL_GRXRINGS:
4003 info->data = rxq_number;
4004 return 0;
4005 case ETHTOOL_GRXFH:
4006 return -EOPNOTSUPP;
4007 default:
4008 return -EOPNOTSUPP;
4009 }
4010}
4011
4012static int mvneta_config_rss(struct mvneta_port *pp)
4013{
4014 int cpu;
4015 u32 val;
4016
4017 netif_tx_stop_all_queues(pp->dev);
4018
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01004019 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004020
4021 /* We have to synchronise on the napi of each CPU */
4022 for_each_online_cpu(cpu) {
4023 struct mvneta_pcpu_port *pcpu_port =
4024 per_cpu_ptr(pp->ports, cpu);
4025
4026 napi_synchronize(&pcpu_port->napi);
4027 napi_disable(&pcpu_port->napi);
4028 }
4029
4030 pp->rxq_def = pp->indir[0];
4031
4032 /* Update unicast mapping */
4033 mvneta_set_rx_mode(pp->dev);
4034
4035 /* Update val of portCfg register accordingly with all RxQueue types */
4036 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
4037 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
4038
4039 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01004040 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004041 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01004042 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004043
4044 /* We have to synchronise on the napi of each CPU */
4045 for_each_online_cpu(cpu) {
4046 struct mvneta_pcpu_port *pcpu_port =
4047 per_cpu_ptr(pp->ports, cpu);
4048
4049 napi_enable(&pcpu_port->napi);
4050 }
4051
4052 netif_tx_start_all_queues(pp->dev);
4053
4054 return 0;
4055}
4056
4057static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
4058 const u8 *key, const u8 hfunc)
4059{
4060 struct mvneta_port *pp = netdev_priv(dev);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004061
4062 /* Current code for Armada 3700 doesn't support RSS features yet */
4063 if (pp->neta_armada3700)
4064 return -EOPNOTSUPP;
4065
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004066 /* We require at least one supported parameter to be changed
4067 * and no change in any of the unsupported parameters
4068 */
4069 if (key ||
4070 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
4071 return -EOPNOTSUPP;
4072
4073 if (!indir)
4074 return 0;
4075
4076 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
4077
4078 return mvneta_config_rss(pp);
4079}
4080
4081static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
4082 u8 *hfunc)
4083{
4084 struct mvneta_port *pp = netdev_priv(dev);
4085
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004086 /* Current code for Armada 3700 doesn't support RSS features yet */
4087 if (pp->neta_armada3700)
4088 return -EOPNOTSUPP;
4089
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004090 if (hfunc)
4091 *hfunc = ETH_RSS_HASH_TOP;
4092
4093 if (!indir)
4094 return 0;
4095
4096 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
4097
4098 return 0;
4099}
4100
Jingju Houb60a00f2017-02-06 14:58:13 +08004101static void mvneta_ethtool_get_wol(struct net_device *dev,
4102 struct ethtool_wolinfo *wol)
4103{
Russell King503f9aa92018-01-02 17:24:44 +00004104 struct mvneta_port *pp = netdev_priv(dev);
Jingju Houb60a00f2017-02-06 14:58:13 +08004105
Russell King503f9aa92018-01-02 17:24:44 +00004106 phylink_ethtool_get_wol(pp->phylink, wol);
Jingju Houb60a00f2017-02-06 14:58:13 +08004107}
4108
4109static int mvneta_ethtool_set_wol(struct net_device *dev,
4110 struct ethtool_wolinfo *wol)
4111{
Russell King503f9aa92018-01-02 17:24:44 +00004112 struct mvneta_port *pp = netdev_priv(dev);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08004113 int ret;
4114
Russell King503f9aa92018-01-02 17:24:44 +00004115 ret = phylink_ethtool_set_wol(pp->phylink, wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08004116 if (!ret)
4117 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
4118
4119 return ret;
Jingju Houb60a00f2017-02-06 14:58:13 +08004120}
4121
Russell King6d81f452018-01-02 17:25:04 +00004122static int mvneta_ethtool_get_eee(struct net_device *dev,
4123 struct ethtool_eee *eee)
4124{
4125 struct mvneta_port *pp = netdev_priv(dev);
4126 u32 lpi_ctl0;
4127
4128 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4129
4130 eee->eee_enabled = pp->eee_enabled;
4131 eee->eee_active = pp->eee_active;
4132 eee->tx_lpi_enabled = pp->tx_lpi_enabled;
4133 eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale;
4134
4135 return phylink_ethtool_get_eee(pp->phylink, eee);
4136}
4137
4138static int mvneta_ethtool_set_eee(struct net_device *dev,
4139 struct ethtool_eee *eee)
4140{
4141 struct mvneta_port *pp = netdev_priv(dev);
4142 u32 lpi_ctl0;
4143
4144 /* The Armada 37x documents do not give limits for this other than
4145 * it being an 8-bit register. */
4146 if (eee->tx_lpi_enabled &&
4147 (eee->tx_lpi_timer < 0 || eee->tx_lpi_timer > 255))
4148 return -EINVAL;
4149
4150 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4151 lpi_ctl0 &= ~(0xff << 8);
4152 lpi_ctl0 |= eee->tx_lpi_timer << 8;
4153 mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0);
4154
4155 pp->eee_enabled = eee->eee_enabled;
4156 pp->tx_lpi_enabled = eee->tx_lpi_enabled;
4157
4158 mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled);
4159
4160 return phylink_ethtool_set_eee(pp->phylink, eee);
4161}
4162
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004163static const struct net_device_ops mvneta_netdev_ops = {
4164 .ndo_open = mvneta_open,
4165 .ndo_stop = mvneta_stop,
4166 .ndo_start_xmit = mvneta_tx,
4167 .ndo_set_rx_mode = mvneta_set_rx_mode,
4168 .ndo_set_mac_address = mvneta_set_mac_addr,
4169 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02004170 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004171 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02004172 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004173};
4174
Jisheng Zhang4581be42017-02-16 17:07:39 +08004175static const struct ethtool_ops mvneta_eth_tool_ops = {
Russell King503f9aa92018-01-02 17:24:44 +00004176 .nway_reset = mvneta_ethtool_nway_reset,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004177 .get_link = ethtool_op_get_link,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004178 .set_coalesce = mvneta_ethtool_set_coalesce,
4179 .get_coalesce = mvneta_ethtool_get_coalesce,
4180 .get_drvinfo = mvneta_ethtool_get_drvinfo,
4181 .get_ringparam = mvneta_ethtool_get_ringparam,
4182 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King4932a912018-01-02 17:24:59 +00004183 .get_pauseparam = mvneta_ethtool_get_pauseparam,
4184 .set_pauseparam = mvneta_ethtool_set_pauseparam,
Russell King9b0cdef2015-10-22 18:37:30 +01004185 .get_strings = mvneta_ethtool_get_strings,
4186 .get_ethtool_stats = mvneta_ethtool_get_stats,
4187 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004188 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
4189 .get_rxnfc = mvneta_ethtool_get_rxnfc,
4190 .get_rxfh = mvneta_ethtool_get_rxfh,
4191 .set_rxfh = mvneta_ethtool_set_rxfh,
Russell King503f9aa92018-01-02 17:24:44 +00004192 .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
Philippe Reynes013ad402016-07-30 17:42:12 +02004193 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
Jingju Houb60a00f2017-02-06 14:58:13 +08004194 .get_wol = mvneta_ethtool_get_wol,
4195 .set_wol = mvneta_ethtool_set_wol,
Russell King6d81f452018-01-02 17:25:04 +00004196 .get_eee = mvneta_ethtool_get_eee,
4197 .set_eee = mvneta_ethtool_set_eee,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004198};
4199
4200/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03004201static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004202{
4203 int queue;
4204
4205 /* Disable port */
4206 mvneta_port_disable(pp);
4207
4208 /* Set port default values */
4209 mvneta_defaults_set(pp);
4210
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004211 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004212 if (!pp->txqs)
4213 return -ENOMEM;
4214
4215 /* Initialize TX descriptor rings */
4216 for (queue = 0; queue < txq_number; queue++) {
4217 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4218 txq->id = queue;
4219 txq->size = pp->tx_ring_size;
4220 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4221 }
4222
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004223 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
Ezequiel Garcia96728502014-05-22 20:06:59 -03004224 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004225 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004226
4227 /* Create Rx descriptor rings */
4228 for (queue = 0; queue < rxq_number; queue++) {
4229 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4230 rxq->id = queue;
4231 rxq->size = pp->rx_ring_size;
4232 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4233 rxq->time_coal = MVNETA_RX_COAL_USEC;
Markus Elfring29110632017-04-16 21:23:19 +02004234 rxq->buf_virt_addr
4235 = devm_kmalloc_array(pp->dev->dev.parent,
4236 rxq->size,
4237 sizeof(*rxq->buf_virt_addr),
4238 GFP_KERNEL);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01004239 if (!rxq->buf_virt_addr)
4240 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004241 }
4242
4243 return 0;
4244}
4245
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004246/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00004247static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4248 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004249{
4250 u32 win_enable;
4251 u32 win_protect;
4252 int i;
4253
4254 for (i = 0; i < 6; i++) {
4255 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4256 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4257
4258 if (i < 4)
4259 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4260 }
4261
4262 win_enable = 0x3f;
4263 win_protect = 0;
4264
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004265 if (dram) {
4266 for (i = 0; i < dram->num_cs; i++) {
4267 const struct mbus_dram_window *cs = dram->cs + i;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004268
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004269 mvreg_write(pp, MVNETA_WIN_BASE(i),
4270 (cs->base & 0xffff0000) |
4271 (cs->mbus_attr << 8) |
4272 dram->mbus_dram_target_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004273
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004274 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4275 (cs->size - 1) & 0xffff0000);
4276
4277 win_enable &= ~(1 << i);
4278 win_protect |= 3 << (2 * i);
4279 }
4280 } else {
4281 /* For Armada3700 open default 4GB Mbus window, leaving
4282 * arbitration of target/attribute to a different layer
4283 * of configuration.
4284 */
4285 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4286 win_enable &= ~BIT(0);
4287 win_protect = 3;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004288 }
4289
4290 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01004291 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004292}
4293
4294/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004295static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004296{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004297 /* MAC Cause register should be cleared */
4298 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4299
Russell King32699952018-01-02 17:24:49 +00004300 if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004301 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
Russell King22f4bf82018-01-02 17:24:54 +00004302 else if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
4303 phy_mode == PHY_INTERFACE_MODE_1000BASEX)
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004304 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
Russell King32699952018-01-02 17:24:49 +00004305 else if (!phy_interface_mode_is_rgmii(phy_mode))
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004306 return -EINVAL;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004307
4308 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004309}
4310
4311/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00004312static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004313{
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004314 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004315 struct device_node *dn = pdev->dev.of_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004316 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004317 struct mvneta_port *pp;
4318 struct net_device *dev;
Russell King503f9aa92018-01-02 17:24:44 +00004319 struct phylink *phylink;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004320 const char *dt_mac_addr;
4321 char hw_mac_addr[ETH_ALEN];
4322 const char *mac_from;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004323 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004324 int phy_mode;
4325 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004326 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004327
Willy Tarreauee40a112013-04-11 23:00:37 +02004328 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004329 if (!dev)
4330 return -ENOMEM;
4331
4332 dev->irq = irq_of_parse_and_map(dn, 0);
4333 if (dev->irq == 0) {
4334 err = -EINVAL;
4335 goto err_free_netdev;
4336 }
4337
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004338 phy_mode = of_get_phy_mode(dn);
4339 if (phy_mode < 0) {
4340 dev_err(&pdev->dev, "incorrect phy-mode\n");
4341 err = -EINVAL;
Russell King503f9aa92018-01-02 17:24:44 +00004342 goto err_free_irq;
4343 }
4344
4345 phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode,
4346 &mvneta_phylink_ops);
4347 if (IS_ERR(phylink)) {
4348 err = PTR_ERR(phylink);
4349 goto err_free_irq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004350 }
4351
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004352 dev->tx_queue_len = MVNETA_MAX_TXD;
4353 dev->watchdog_timeo = 5 * HZ;
4354 dev->netdev_ops = &mvneta_netdev_ops;
4355
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004356 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004357
4358 pp = netdev_priv(dev);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01004359 spin_lock_init(&pp->lock);
Russell King503f9aa92018-01-02 17:24:44 +00004360 pp->phylink = phylink;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004361 pp->phy_interface = phy_mode;
Russell King503f9aa92018-01-02 17:24:44 +00004362 pp->dn = dn;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004363
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01004364 pp->rxq_def = rxq_def;
4365
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01004366 /* Set RX packet offset correction for platforms, whose
4367 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4368 * platforms and 0B for 32-bit ones.
4369 */
4370 pp->rx_offset_correction =
4371 max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
4372
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004373 pp->indir[0] = rxq_def;
4374
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004375 /* Get special SoC configurations */
4376 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4377 pp->neta_armada3700 = true;
4378
Jisheng Zhang2804ba42016-01-20 19:27:23 +08004379 pp->clk = devm_clk_get(&pdev->dev, "core");
4380 if (IS_ERR(pp->clk))
4381 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004382 if (IS_ERR(pp->clk)) {
4383 err = PTR_ERR(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00004384 goto err_free_phylink;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004385 }
4386
4387 clk_prepare_enable(pp->clk);
4388
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004389 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4390 if (!IS_ERR(pp->clk_bus))
4391 clk_prepare_enable(pp->clk_bus);
4392
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004393 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4394 pp->base = devm_ioremap_resource(&pdev->dev, res);
4395 if (IS_ERR(pp->base)) {
4396 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004397 goto err_clk;
4398 }
4399
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004400 /* Alloc per-cpu port structure */
4401 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4402 if (!pp->ports) {
4403 err = -ENOMEM;
4404 goto err_clk;
4405 }
4406
willy tarreau74c41b02014-01-16 08:20:08 +01004407 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08004408 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01004409 if (!pp->stats) {
4410 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004411 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01004412 }
4413
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004414 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01004415 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004416 mac_from = "device tree";
4417 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4418 } else {
4419 mvneta_get_mac_addr(pp, hw_mac_addr);
4420 if (is_valid_ether_addr(hw_mac_addr)) {
4421 mac_from = "hardware";
4422 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4423 } else {
4424 mac_from = "random";
4425 eth_hw_addr_random(dev);
4426 }
4427 }
4428
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004429 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4430 if (tx_csum_limit < 0 ||
4431 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4432 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4433 dev_info(&pdev->dev,
4434 "Wrong TX csum limit in DT, set to %dB\n",
4435 MVNETA_TX_CSUM_DEF_SIZE);
4436 }
4437 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4438 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4439 } else {
4440 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4441 }
4442
4443 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02004444
Jane Li9768b452017-03-16 16:22:28 +08004445 pp->dram_target_info = mv_mbus_dram_info();
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004446 /* Armada3700 requires setting default configuration of Mbus
4447 * windows, however without using filled mbus_dram_target_info
4448 * structure.
4449 */
Jane Li9768b452017-03-16 16:22:28 +08004450 if (pp->dram_target_info || pp->neta_armada3700)
4451 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004452
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004453 pp->tx_ring_size = MVNETA_MAX_TXD;
4454 pp->rx_ring_size = MVNETA_MAX_RXD;
4455
4456 pp->dev = dev;
4457 SET_NETDEV_DEV(dev, &pdev->dev);
4458
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004459 pp->id = global_port_id++;
4460
4461 /* Obtain access to BM resources if enabled and already initialized */
4462 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4463 if (bm_node && bm_node->data) {
4464 pp->bm_priv = bm_node->data;
4465 err = mvneta_bm_port_init(pdev, pp);
4466 if (err < 0) {
4467 dev_info(&pdev->dev, "use SW buffer management\n");
4468 pp->bm_priv = NULL;
4469 }
4470 }
Peter Chend4e4da02016-08-01 15:02:36 +08004471 of_node_put(bm_node);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004472
Ezequiel Garcia96728502014-05-22 20:06:59 -03004473 err = mvneta_init(&pdev->dev, pp);
4474 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004475 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004476
4477 err = mvneta_port_power_up(pp, phy_mode);
4478 if (err < 0) {
4479 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004480 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004481 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004482
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004483 /* Armada3700 network controller does not support per-cpu
4484 * operation, so only single NAPI should be initialized.
4485 */
4486 if (pp->neta_armada3700) {
4487 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4488 } else {
4489 for_each_present_cpu(cpu) {
4490 struct mvneta_pcpu_port *port =
4491 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004492
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004493 netif_napi_add(dev, &port->napi, mvneta_poll,
4494 NAPI_POLL_WEIGHT);
4495 port->pp = pp;
4496 }
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004497 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004498
Andrew Pilloudc54a5042017-09-01 07:49:49 -07004499 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03004500 dev->hw_features |= dev->features;
4501 dev->vlan_features |= dev->features;
Andrew Lunn97db8af2016-11-24 00:08:13 +01004502 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004503 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00004504
Jarod Wilson57779872016-10-17 15:54:06 -04004505 /* MTU range: 68 - 9676 */
4506 dev->min_mtu = ETH_MIN_MTU;
4507 /* 9676 == 9700 - 20 and rounding to 8 */
4508 dev->max_mtu = 9676;
4509
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004510 err = register_netdev(dev);
4511 if (err < 0) {
4512 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03004513 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004514 }
4515
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004516 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4517 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004518
4519 platform_set_drvdata(pdev, pp->dev);
4520
4521 return 0;
4522
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004523err_netdev:
4524 unregister_netdev(dev);
4525 if (pp->bm_priv) {
4526 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4527 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4528 1 << pp->id);
4529 }
willy tarreau74c41b02014-01-16 08:20:08 +01004530err_free_stats:
4531 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004532err_free_ports:
4533 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004534err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004535 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004536 clk_disable_unprepare(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00004537err_free_phylink:
4538 if (pp->phylink)
4539 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004540err_free_irq:
4541 irq_dispose_mapping(dev->irq);
4542err_free_netdev:
4543 free_netdev(dev);
4544 return err;
4545}
4546
4547/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00004548static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004549{
4550 struct net_device *dev = platform_get_drvdata(pdev);
4551 struct mvneta_port *pp = netdev_priv(dev);
4552
4553 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004554 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004555 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004556 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01004557 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004558 irq_dispose_mapping(dev->irq);
Russell King503f9aa92018-01-02 17:24:44 +00004559 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004560 free_netdev(dev);
4561
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004562 if (pp->bm_priv) {
4563 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4564 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4565 1 << pp->id);
4566 }
4567
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004568 return 0;
4569}
4570
Jane Li9768b452017-03-16 16:22:28 +08004571#ifdef CONFIG_PM_SLEEP
4572static int mvneta_suspend(struct device *device)
4573{
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004574 int queue;
Jane Li9768b452017-03-16 16:22:28 +08004575 struct net_device *dev = dev_get_drvdata(device);
4576 struct mvneta_port *pp = netdev_priv(dev);
4577
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004578 if (!netif_running(dev))
4579 goto clean_exit;
4580
4581 if (!pp->neta_armada3700) {
4582 spin_lock(&pp->lock);
4583 pp->is_stopped = true;
4584 spin_unlock(&pp->lock);
4585
4586 cpuhp_state_remove_instance_nocalls(online_hpstate,
4587 &pp->node_online);
4588 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4589 &pp->node_dead);
4590 }
4591
Russell King3b8bc672018-01-02 17:24:34 +00004592 rtnl_lock();
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004593 mvneta_stop_dev(pp);
Russell King3b8bc672018-01-02 17:24:34 +00004594 rtnl_unlock();
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004595
4596 for (queue = 0; queue < rxq_number; queue++) {
4597 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4598
4599 mvneta_rxq_drop_pkts(pp, rxq);
4600 }
4601
4602 for (queue = 0; queue < txq_number; queue++) {
4603 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4604
4605 mvneta_txq_hw_deinit(pp, txq);
4606 }
4607
4608clean_exit:
Jane Li9768b452017-03-16 16:22:28 +08004609 netif_device_detach(dev);
4610 clk_disable_unprepare(pp->clk_bus);
4611 clk_disable_unprepare(pp->clk);
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004612
Jane Li9768b452017-03-16 16:22:28 +08004613 return 0;
4614}
4615
4616static int mvneta_resume(struct device *device)
4617{
4618 struct platform_device *pdev = to_platform_device(device);
4619 struct net_device *dev = dev_get_drvdata(device);
4620 struct mvneta_port *pp = netdev_priv(dev);
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004621 int err, queue;
Jane Li9768b452017-03-16 16:22:28 +08004622
4623 clk_prepare_enable(pp->clk);
4624 if (!IS_ERR(pp->clk_bus))
4625 clk_prepare_enable(pp->clk_bus);
4626 if (pp->dram_target_info || pp->neta_armada3700)
4627 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4628 if (pp->bm_priv) {
4629 err = mvneta_bm_port_init(pdev, pp);
4630 if (err < 0) {
4631 dev_info(&pdev->dev, "use SW buffer management\n");
4632 pp->bm_priv = NULL;
4633 }
4634 }
4635 mvneta_defaults_set(pp);
4636 err = mvneta_port_power_up(pp, pp->phy_interface);
4637 if (err < 0) {
4638 dev_err(device, "can't power up port\n");
4639 return err;
4640 }
4641
Jane Li9768b452017-03-16 16:22:28 +08004642 netif_device_attach(dev);
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004643
4644 if (!netif_running(dev))
4645 return 0;
4646
4647 for (queue = 0; queue < rxq_number; queue++) {
4648 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4649
4650 rxq->next_desc_to_proc = 0;
4651 mvneta_rxq_hw_init(pp, rxq);
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004652 }
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004653
4654 for (queue = 0; queue < txq_number; queue++) {
4655 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4656
4657 txq->next_desc_to_proc = 0;
4658 mvneta_txq_hw_init(pp, txq);
4659 }
4660
4661 if (!pp->neta_armada3700) {
4662 spin_lock(&pp->lock);
4663 pp->is_stopped = false;
4664 spin_unlock(&pp->lock);
4665 cpuhp_state_add_instance_nocalls(online_hpstate,
4666 &pp->node_online);
4667 cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4668 &pp->node_dead);
4669 }
4670
4671 rtnl_lock();
4672 mvneta_start_dev(pp);
Russell King3b8bc672018-01-02 17:24:34 +00004673 rtnl_unlock();
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08004674 mvneta_set_rx_mode(dev);
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004675
Jane Li9768b452017-03-16 16:22:28 +08004676 return 0;
4677}
4678#endif
4679
4680static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
4681
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004682static const struct of_device_id mvneta_match[] = {
4683 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02004684 { .compatible = "marvell,armada-xp-neta" },
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004685 { .compatible = "marvell,armada-3700-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004686 { }
4687};
4688MODULE_DEVICE_TABLE(of, mvneta_match);
4689
4690static struct platform_driver mvneta_driver = {
4691 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00004692 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004693 .driver = {
4694 .name = MVNETA_DRIVER_NAME,
4695 .of_match_table = mvneta_match,
Jane Li9768b452017-03-16 16:22:28 +08004696 .pm = &mvneta_pm_ops,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004697 },
4698};
4699
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004700static int __init mvneta_driver_init(void)
4701{
4702 int ret;
4703
4704 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4705 mvneta_cpu_online,
4706 mvneta_cpu_down_prepare);
4707 if (ret < 0)
4708 goto out;
4709 online_hpstate = ret;
4710 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4711 NULL, mvneta_cpu_dead);
4712 if (ret)
4713 goto err_dead;
4714
4715 ret = platform_driver_register(&mvneta_driver);
4716 if (ret)
4717 goto err;
4718 return 0;
4719
4720err:
4721 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4722err_dead:
4723 cpuhp_remove_multi_state(online_hpstate);
4724out:
4725 return ret;
4726}
4727module_init(mvneta_driver_init);
4728
4729static void __exit mvneta_driver_exit(void)
4730{
4731 platform_driver_unregister(&mvneta_driver);
4732 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4733 cpuhp_remove_multi_state(online_hpstate);
4734}
4735module_exit(mvneta_driver_exit);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004736
4737MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4738MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4739MODULE_LICENSE("GPL");
4740
Joe Perchesd3757ba2018-03-23 16:34:44 -07004741module_param(rxq_number, int, 0444);
4742module_param(txq_number, int, 0444);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004743
Joe Perchesd3757ba2018-03-23 16:34:44 -07004744module_param(rxq_def, int, 0444);
4745module_param(rx_copybreak, int, 0644);