blob: 73643dbd5cb0d57949b64f86fbff6f886ac6fe86 [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
247#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
248
249/* Descriptor ring Macros */
250#define MVNETA_QUEUE_NEXT_DESC(q, index) \
251 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
252
253/* Various constants */
254
255/* Coalescing */
Dmitri Epshtein06708f82016-07-06 04:18:58 +0200256#define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300257#define MVNETA_RX_COAL_PKTS 32
258#define MVNETA_RX_COAL_USEC 100
259
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100260/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300261 * by Marvell switches when a specific hardware mode is enabled (not
262 * supported by this driver) or is filled automatically by zeroes on
263 * the RX side. Those two bytes being at the front of the Ethernet
264 * header, they allow to have the IP header aligned on a 4 bytes
265 * boundary automatically: the hardware skips those two bytes on its
266 * own.
267 */
268#define MVNETA_MH_SIZE 2
269
270#define MVNETA_VLAN_TAG_LEN 4
271
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100272#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300273#define MVNETA_TX_CSUM_MAX_SIZE 9800
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100274#define MVNETA_ACC_MODE_EXT1 1
275#define MVNETA_ACC_MODE_EXT2 2
276
277#define MVNETA_MAX_DECODE_WIN 6
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300278
279/* Timeout constants */
280#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
281#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
282#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
283
284#define MVNETA_TX_MTU_MAX 0x3ffff
285
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100286/* The RSS lookup table actually has 256 entries but we do not use
287 * them yet
288 */
289#define MVNETA_RSS_LU_TABLE_SIZE 1
290
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300291/* Max number of Rx descriptors */
292#define MVNETA_MAX_RXD 128
293
294/* Max number of Tx descriptors */
295#define MVNETA_MAX_TXD 532
296
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300297/* Max number of allowed TCP segments for software TSO */
298#define MVNETA_MAX_TSO_SEGS 100
299
300#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
301
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300302/* descriptor aligned size */
303#define MVNETA_DESC_ALIGNED_SIZE 32
304
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100305/* Number of bytes to be taken into account by HW when putting incoming data
306 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
307 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
308 */
309#define MVNETA_RX_PKT_OFFSET_CORRECTION 64
310
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300311#define MVNETA_RX_PKT_SIZE(mtu) \
312 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
313 ETH_HLEN + ETH_FCS_LEN, \
Jisheng Zhangc66e98c2016-04-01 17:12:49 +0800314 cache_line_size())
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300315
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300316#define IS_TSO_HEADER(txq, addr) \
317 ((addr >= txq->tso_hdrs_phys) && \
318 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
319
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100320#define MVNETA_RX_GET_BM_POOL_ID(rxd) \
321 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300322
Russell King9b0cdef2015-10-22 18:37:30 +0100323struct mvneta_statistic {
324 unsigned short offset;
325 unsigned short type;
326 const char name[ETH_GSTRING_LEN];
327};
328
329#define T_REG_32 32
330#define T_REG_64 64
331
332static const struct mvneta_statistic mvneta_statistics[] = {
333 { 0x3000, T_REG_64, "good_octets_received", },
334 { 0x3010, T_REG_32, "good_frames_received", },
335 { 0x3008, T_REG_32, "bad_octets_received", },
336 { 0x3014, T_REG_32, "bad_frames_received", },
337 { 0x3018, T_REG_32, "broadcast_frames_received", },
338 { 0x301c, T_REG_32, "multicast_frames_received", },
339 { 0x3050, T_REG_32, "unrec_mac_control_received", },
340 { 0x3058, T_REG_32, "good_fc_received", },
341 { 0x305c, T_REG_32, "bad_fc_received", },
342 { 0x3060, T_REG_32, "undersize_received", },
343 { 0x3064, T_REG_32, "fragments_received", },
344 { 0x3068, T_REG_32, "oversize_received", },
345 { 0x306c, T_REG_32, "jabber_received", },
346 { 0x3070, T_REG_32, "mac_receive_error", },
347 { 0x3074, T_REG_32, "bad_crc_event", },
348 { 0x3078, T_REG_32, "collision", },
349 { 0x307c, T_REG_32, "late_collision", },
350 { 0x2484, T_REG_32, "rx_discard", },
351 { 0x2488, T_REG_32, "rx_overrun", },
352 { 0x3020, T_REG_32, "frames_64_octets", },
353 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
354 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
355 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
356 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
357 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
358 { 0x3038, T_REG_64, "good_octets_sent", },
359 { 0x3040, T_REG_32, "good_frames_sent", },
360 { 0x3044, T_REG_32, "excessive_collision", },
361 { 0x3048, T_REG_32, "multicast_frames_sent", },
362 { 0x304c, T_REG_32, "broadcast_frames_sent", },
363 { 0x3054, T_REG_32, "fc_sent", },
364 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
365};
366
willy tarreau74c41b02014-01-16 08:20:08 +0100367struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300368 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100369 u64 rx_packets;
370 u64 rx_bytes;
371 u64 tx_packets;
372 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300373};
374
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200375struct mvneta_pcpu_port {
376 /* Pointer to the shared port */
377 struct mvneta_port *pp;
378
379 /* Pointer to the CPU-local NAPI struct */
380 struct napi_struct napi;
381
382 /* Cause of the previous interrupt */
383 u32 cause_rx_tx;
384};
385
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300386struct mvneta_port {
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100387 u8 id;
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200388 struct mvneta_pcpu_port __percpu *ports;
389 struct mvneta_pcpu_stats __percpu *stats;
390
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300391 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100392 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300393 void __iomem *base;
394 struct mvneta_rx_queue *rxqs;
395 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300396 struct net_device *dev;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200397 struct hlist_node node_online;
398 struct hlist_node node_dead;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100399 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100400 /* Protect the access to the percpu interrupt registers,
401 * ensuring that the configuration remains coherent.
402 */
403 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100404 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300405
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100406 u32 cause_rx_tx;
407 struct napi_struct napi;
408
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300409 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100410 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800411 /* AXI clock */
412 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300413 u8 mcast_count[256];
414 u16 tx_ring_size;
415 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300416
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300417 phy_interface_t phy_interface;
Russell King503f9aa92018-01-02 17:24:44 +0000418 struct device_node *dn;
Simon Guinotb65657f2015-06-30 16:20:22 +0200419 unsigned int tx_csum_limit;
Russell King503f9aa92018-01-02 17:24:44 +0000420 struct phylink *phylink;
Russell King9b0cdef2015-10-22 18:37:30 +0100421
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100422 struct mvneta_bm *bm_priv;
423 struct mvneta_bm_pool *pool_long;
424 struct mvneta_bm_pool *pool_short;
425 int bm_win_id;
426
Russell King9b0cdef2015-10-22 18:37:30 +0100427 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100428
429 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100430
431 /* Flags for special SoC configurations */
432 bool neta_armada3700;
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100433 u16 rx_offset_correction;
Jane Li9768b452017-03-16 16:22:28 +0800434 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300435};
436
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100437/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300438 * layout of the transmit and reception DMA descriptors, and their
439 * layout is therefore defined by the hardware design
440 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200441
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300442#define MVNETA_TX_L3_OFF_SHIFT 0
443#define MVNETA_TX_IP_HLEN_SHIFT 8
444#define MVNETA_TX_L4_UDP BIT(16)
445#define MVNETA_TX_L3_IP6 BIT(17)
446#define MVNETA_TXD_IP_CSUM BIT(18)
447#define MVNETA_TXD_Z_PAD BIT(19)
448#define MVNETA_TXD_L_DESC BIT(20)
449#define MVNETA_TXD_F_DESC BIT(21)
450#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
451 MVNETA_TXD_L_DESC | \
452 MVNETA_TXD_F_DESC)
453#define MVNETA_TX_L4_CSUM_FULL BIT(30)
454#define MVNETA_TX_L4_CSUM_NOT BIT(31)
455
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300456#define MVNETA_RXD_ERR_CRC 0x0
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100457#define MVNETA_RXD_BM_POOL_SHIFT 13
458#define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300459#define MVNETA_RXD_ERR_SUMMARY BIT(16)
460#define MVNETA_RXD_ERR_OVERRUN BIT(17)
461#define MVNETA_RXD_ERR_LEN BIT(18)
462#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
463#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
464#define MVNETA_RXD_L3_IP4 BIT(25)
465#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
466#define MVNETA_RXD_L4_CSUM_OK BIT(30)
467
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200468#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200469struct mvneta_tx_desc {
470 u32 command; /* Options used by HW for packet transmitting.*/
471 u16 reserverd1; /* csum_l4 (for future use) */
472 u16 data_size; /* Data size of transmitted packet in bytes */
473 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
474 u32 reserved2; /* hw_cmd - (for future use, PMT) */
475 u32 reserved3[4]; /* Reserved - (for future use) */
476};
477
478struct mvneta_rx_desc {
479 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300480 u16 reserved1; /* pnc_info - (for future use, PnC) */
481 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200482
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300483 u32 buf_phys_addr; /* Physical address of the buffer */
484 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200485
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300486 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
487 u16 reserved3; /* prefetch_cmd, for future use */
488 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200489
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300490 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
491 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
492};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200493#else
494struct mvneta_tx_desc {
495 u16 data_size; /* Data size of transmitted packet in bytes */
496 u16 reserverd1; /* csum_l4 (for future use) */
497 u32 command; /* Options used by HW for packet transmitting.*/
498 u32 reserved2; /* hw_cmd - (for future use, PMT) */
499 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
500 u32 reserved3[4]; /* Reserved - (for future use) */
501};
502
503struct mvneta_rx_desc {
504 u16 data_size; /* Size of received packet in bytes */
505 u16 reserved1; /* pnc_info - (for future use, PnC) */
506 u32 status; /* Info about received packet */
507
508 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
509 u32 buf_phys_addr; /* Physical address of the buffer */
510
511 u16 reserved4; /* csum_l4 - (for future use, PnC) */
512 u16 reserved3; /* prefetch_cmd, for future use */
513 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
514
515 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
516 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
517};
518#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300519
520struct mvneta_tx_queue {
521 /* Number of this TX queue, in the range 0-7 */
522 u8 id;
523
524 /* Number of TX DMA descriptors in the descriptor ring */
525 int size;
526
527 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100528 * descriptor ring
529 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300530 int count;
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100531 int pending;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300532 int tx_stop_threshold;
533 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300534
535 /* Array of transmitted skb */
536 struct sk_buff **tx_skb;
537
538 /* Index of last TX DMA descriptor that was inserted */
539 int txq_put_index;
540
541 /* Index of the TX DMA descriptor to be cleaned up */
542 int txq_get_index;
543
544 u32 done_pkts_coal;
545
546 /* Virtual address of the TX DMA descriptors array */
547 struct mvneta_tx_desc *descs;
548
549 /* DMA address of the TX DMA descriptors array */
550 dma_addr_t descs_phys;
551
552 /* Index of the last TX DMA descriptor */
553 int last_desc;
554
555 /* Index of the next TX DMA descriptor to process */
556 int next_desc_to_proc;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300557
558 /* DMA buffers for TSO headers */
559 char *tso_hdrs;
560
561 /* DMA address of TSO headers */
562 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100563
564 /* Affinity mask for CPUs*/
565 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300566};
567
568struct mvneta_rx_queue {
569 /* rx queue number, in the range 0-7 */
570 u8 id;
571
572 /* num of rx descriptors in the rx descriptor ring */
573 int size;
574
575 /* counter of times when mvneta_refill() failed */
576 int missed;
577
578 u32 pkts_coal;
579 u32 time_coal;
580
Gregory CLEMENTf88bee12016-12-01 18:03:06 +0100581 /* Virtual address of the RX buffer */
582 void **buf_virt_addr;
583
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300584 /* Virtual address of the RX DMA descriptors array */
585 struct mvneta_rx_desc *descs;
586
587 /* DMA address of the RX DMA descriptors array */
588 dma_addr_t descs_phys;
589
590 /* Index of the last RX DMA descriptor */
591 int last_desc;
592
593 /* Index of the next RX DMA descriptor to process */
594 int next_desc_to_proc;
595};
596
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200597static enum cpuhp_state online_hpstate;
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300598/* The hardware supports eight (8) rx queues, but we are only allowing
599 * the first one to be used. Therefore, let's just allocate one queue.
600 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200601static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300602static int txq_number = 8;
603
604static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300605
willy tarreauf19fadf2014-01-16 08:20:17 +0100606static int rx_copybreak __read_mostly = 256;
607
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100608/* HW BM need that each port be identify by a unique ID */
609static int global_port_id;
610
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300611#define MVNETA_DRIVER_NAME "mvneta"
612#define MVNETA_DRIVER_VERSION "1.0"
613
614/* Utility/helper methods */
615
616/* Write helper method */
617static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
618{
619 writel(data, pp->base + offset);
620}
621
622/* Read helper method */
623static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
624{
625 return readl(pp->base + offset);
626}
627
628/* Increment txq get counter */
629static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
630{
631 txq->txq_get_index++;
632 if (txq->txq_get_index == txq->size)
633 txq->txq_get_index = 0;
634}
635
636/* Increment txq put counter */
637static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
638{
639 txq->txq_put_index++;
640 if (txq->txq_put_index == txq->size)
641 txq->txq_put_index = 0;
642}
643
644
645/* Clear all MIB counters */
646static void mvneta_mib_counters_clear(struct mvneta_port *pp)
647{
648 int i;
649 u32 dummy;
650
651 /* Perform dummy reads from MIB counters */
652 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
653 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100654 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
655 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300656}
657
658/* Get System Network Statistics */
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800659static void
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +0800660mvneta_get_stats64(struct net_device *dev,
661 struct rtnl_link_stats64 *stats)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300662{
663 struct mvneta_port *pp = netdev_priv(dev);
664 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100665 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300666
willy tarreau74c41b02014-01-16 08:20:08 +0100667 for_each_possible_cpu(cpu) {
668 struct mvneta_pcpu_stats *cpu_stats;
669 u64 rx_packets;
670 u64 rx_bytes;
671 u64 tx_packets;
672 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300673
willy tarreau74c41b02014-01-16 08:20:08 +0100674 cpu_stats = per_cpu_ptr(pp->stats, cpu);
675 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700676 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100677 rx_packets = cpu_stats->rx_packets;
678 rx_bytes = cpu_stats->rx_bytes;
679 tx_packets = cpu_stats->tx_packets;
680 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700681 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300682
willy tarreau74c41b02014-01-16 08:20:08 +0100683 stats->rx_packets += rx_packets;
684 stats->rx_bytes += rx_bytes;
685 stats->tx_packets += tx_packets;
686 stats->tx_bytes += tx_bytes;
687 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300688
689 stats->rx_errors = dev->stats.rx_errors;
690 stats->rx_dropped = dev->stats.rx_dropped;
691
692 stats->tx_dropped = dev->stats.tx_dropped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300693}
694
695/* Rx descriptors helper methods */
696
willy tarreau54282132014-01-16 08:20:14 +0100697/* Checks whether the RX descriptor having this status is both the first
698 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300699 * received through a single RX descriptor, so not having each RX
700 * descriptor with its first and last bits set is an error
701 */
willy tarreau54282132014-01-16 08:20:14 +0100702static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300703{
willy tarreau54282132014-01-16 08:20:14 +0100704 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300705 MVNETA_RXD_FIRST_LAST_DESC;
706}
707
708/* Add number of descriptors ready to receive new packets */
709static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
710 struct mvneta_rx_queue *rxq,
711 int ndescs)
712{
713 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100714 * be added at once
715 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300716 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
717 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
718 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
719 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
720 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
721 }
722
723 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
724 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
725}
726
727/* Get number of RX descriptors occupied by received packets */
728static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
729 struct mvneta_rx_queue *rxq)
730{
731 u32 val;
732
733 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
734 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
735}
736
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100737/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300738 * from mvneta_rxq_drop_pkts().
739 */
740static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
741 struct mvneta_rx_queue *rxq,
742 int rx_done, int rx_filled)
743{
744 u32 val;
745
746 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
747 val = rx_done |
748 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
749 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
750 return;
751 }
752
753 /* Only 255 descriptors can be added at once */
754 while ((rx_done > 0) || (rx_filled > 0)) {
755 if (rx_done <= 0xff) {
756 val = rx_done;
757 rx_done = 0;
758 } else {
759 val = 0xff;
760 rx_done -= 0xff;
761 }
762 if (rx_filled <= 0xff) {
763 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
764 rx_filled = 0;
765 } else {
766 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
767 rx_filled -= 0xff;
768 }
769 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
770 }
771}
772
773/* Get pointer to next RX descriptor to be processed by SW */
774static struct mvneta_rx_desc *
775mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
776{
777 int rx_desc = rxq->next_desc_to_proc;
778
779 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100780 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300781 return rxq->descs + rx_desc;
782}
783
784/* Change maximum receive size of the port. */
785static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
786{
787 u32 val;
788
789 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
790 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
791 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
792 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
793 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
794}
795
796
797/* Set rx queue offset */
798static void mvneta_rxq_offset_set(struct mvneta_port *pp,
799 struct mvneta_rx_queue *rxq,
800 int offset)
801{
802 u32 val;
803
804 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
805 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
806
807 /* Offset is in */
808 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
809 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
810}
811
812
813/* Tx descriptors helper methods */
814
815/* Update HW with number of TX descriptors to be sent */
816static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
817 struct mvneta_tx_queue *txq,
818 int pend_desc)
819{
820 u32 val;
821
Simon Guinot0d637852017-11-13 16:27:02 +0100822 pend_desc += txq->pending;
823
824 /* Only 255 Tx descriptors can be added at once */
825 do {
826 val = min(pend_desc, 255);
827 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
828 pend_desc -= val;
829 } while (pend_desc > 0);
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100830 txq->pending = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300831}
832
833/* Get pointer to next TX descriptor to be processed (send) by HW */
834static struct mvneta_tx_desc *
835mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
836{
837 int tx_desc = txq->next_desc_to_proc;
838
839 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
840 return txq->descs + tx_desc;
841}
842
843/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100844 * mapping failures in the TX path.
845 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300846static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
847{
848 if (txq->next_desc_to_proc == 0)
849 txq->next_desc_to_proc = txq->last_desc - 1;
850 else
851 txq->next_desc_to_proc--;
852}
853
854/* Set rxq buf size */
855static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
856 struct mvneta_rx_queue *rxq,
857 int buf_size)
858{
859 u32 val;
860
861 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
862
863 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
864 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
865
866 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
867}
868
869/* Disable buffer management (BM) */
870static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
871 struct mvneta_rx_queue *rxq)
872{
873 u32 val;
874
875 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
876 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
877 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
878}
879
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100880/* Enable buffer management (BM) */
881static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
882 struct mvneta_rx_queue *rxq)
883{
884 u32 val;
885
886 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
887 val |= MVNETA_RXQ_HW_BUF_ALLOC;
888 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
889}
890
891/* Notify HW about port's assignment of pool for bigger packets */
892static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
893 struct mvneta_rx_queue *rxq)
894{
895 u32 val;
896
897 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
898 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
899 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
900
901 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
902}
903
904/* Notify HW about port's assignment of pool for smaller packets */
905static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
906 struct mvneta_rx_queue *rxq)
907{
908 u32 val;
909
910 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
911 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
912 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
913
914 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
915}
916
917/* Set port's receive buffer size for assigned BM pool */
918static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
919 int buf_size,
920 u8 pool_id)
921{
922 u32 val;
923
924 if (!IS_ALIGNED(buf_size, 8)) {
925 dev_warn(pp->dev->dev.parent,
926 "illegal buf_size value %d, round to %d\n",
927 buf_size, ALIGN(buf_size, 8));
928 buf_size = ALIGN(buf_size, 8);
929 }
930
931 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
932 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
933 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
934}
935
936/* Configure MBUS window in order to enable access BM internal SRAM */
937static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
938 u8 target, u8 attr)
939{
940 u32 win_enable, win_protect;
941 int i;
942
943 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
944
945 if (pp->bm_win_id < 0) {
946 /* Find first not occupied window */
947 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
948 if (win_enable & (1 << i)) {
949 pp->bm_win_id = i;
950 break;
951 }
952 }
953 if (i == MVNETA_MAX_DECODE_WIN)
954 return -ENOMEM;
955 } else {
956 i = pp->bm_win_id;
957 }
958
959 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
960 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
961
962 if (i < 4)
963 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
964
965 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
966 (attr << 8) | target);
967
968 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
969
970 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
971 win_protect |= 3 << (2 * i);
972 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
973
974 win_enable &= ~(1 << i);
975 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
976
977 return 0;
978}
979
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100980static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100981{
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100982 u32 wsize;
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100983 u8 target, attr;
984 int err;
985
986 /* Get BM window information */
987 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
988 &target, &attr);
989 if (err < 0)
990 return err;
991
992 pp->bm_win_id = -1;
993
994 /* Open NETA -> BM window */
995 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
996 target, attr);
997 if (err < 0) {
998 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
999 return err;
1000 }
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001001 return 0;
1002}
1003
1004/* Assign and initialize pools for port. In case of fail
1005 * buffer manager will remain disabled for current port.
1006 */
1007static int mvneta_bm_port_init(struct platform_device *pdev,
1008 struct mvneta_port *pp)
1009{
1010 struct device_node *dn = pdev->dev.of_node;
1011 u32 long_pool_id, short_pool_id;
1012
1013 if (!pp->neta_armada3700) {
1014 int ret;
1015
1016 ret = mvneta_bm_port_mbus_init(pp);
1017 if (ret)
1018 return ret;
1019 }
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001020
1021 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1022 netdev_info(pp->dev, "missing long pool id\n");
1023 return -EINVAL;
1024 }
1025
1026 /* Create port's long pool depending on mtu */
1027 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1028 MVNETA_BM_LONG, pp->id,
1029 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1030 if (!pp->pool_long) {
1031 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1032 return -ENOMEM;
1033 }
1034
1035 pp->pool_long->port_map |= 1 << pp->id;
1036
1037 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1038 pp->pool_long->id);
1039
1040 /* If short pool id is not defined, assume using single pool */
1041 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1042 short_pool_id = long_pool_id;
1043
1044 /* Create port's short pool */
1045 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1046 MVNETA_BM_SHORT, pp->id,
1047 MVNETA_BM_SHORT_PKT_SIZE);
1048 if (!pp->pool_short) {
1049 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1050 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1051 return -ENOMEM;
1052 }
1053
1054 if (short_pool_id != long_pool_id) {
1055 pp->pool_short->port_map |= 1 << pp->id;
1056 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1057 pp->pool_short->id);
1058 }
1059
1060 return 0;
1061}
1062
1063/* Update settings of a pool for bigger packets */
1064static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1065{
1066 struct mvneta_bm_pool *bm_pool = pp->pool_long;
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001067 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001068 int num;
1069
1070 /* Release all buffers from long pool */
1071 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001072 if (hwbm_pool->buf_num) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001073 WARN(1, "cannot free all buffers in pool %d\n",
1074 bm_pool->id);
1075 goto bm_mtu_err;
1076 }
1077
1078 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1079 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001080 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1081 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001082
1083 /* Fill entire long pool */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001084 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1085 if (num != hwbm_pool->size) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001086 WARN(1, "pool %d: %d of %d allocated\n",
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001087 bm_pool->id, num, hwbm_pool->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001088 goto bm_mtu_err;
1089 }
1090 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1091
1092 return;
1093
1094bm_mtu_err:
1095 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1096 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1097
1098 pp->bm_priv = NULL;
1099 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1100 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1101}
1102
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001103/* Start the Ethernet port RX and TX activity */
1104static void mvneta_port_up(struct mvneta_port *pp)
1105{
1106 int queue;
1107 u32 q_map;
1108
1109 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001110 q_map = 0;
1111 for (queue = 0; queue < txq_number; queue++) {
1112 struct mvneta_tx_queue *txq = &pp->txqs[queue];
Markus Elfringf95936c2017-04-16 22:45:33 +02001113 if (txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001114 q_map |= (1 << queue);
1115 }
1116 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1117
1118 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001119 for (queue = 0; queue < rxq_number; queue++) {
1120 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1121
Markus Elfringf95936c2017-04-16 22:45:33 +02001122 if (rxq->descs)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001123 q_map |= (1 << queue);
1124 }
1125 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001126}
1127
1128/* Stop the Ethernet port activity */
1129static void mvneta_port_down(struct mvneta_port *pp)
1130{
1131 u32 val;
1132 int count;
1133
1134 /* Stop Rx port activity. Check port Rx activity. */
1135 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1136
1137 /* Issue stop command for active channels only */
1138 if (val != 0)
1139 mvreg_write(pp, MVNETA_RXQ_CMD,
1140 val << MVNETA_RXQ_DISABLE_SHIFT);
1141
1142 /* Wait for all Rx activity to terminate. */
1143 count = 0;
1144 do {
1145 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1146 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001147 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001148 val);
1149 break;
1150 }
1151 mdelay(1);
1152
1153 val = mvreg_read(pp, MVNETA_RXQ_CMD);
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001154 } while (val & MVNETA_RXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001155
1156 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001157 * command for active channels only
1158 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001159 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1160
1161 if (val != 0)
1162 mvreg_write(pp, MVNETA_TXQ_CMD,
1163 (val << MVNETA_TXQ_DISABLE_SHIFT));
1164
1165 /* Wait for all Tx activity to terminate. */
1166 count = 0;
1167 do {
1168 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1169 netdev_warn(pp->dev,
1170 "TIMEOUT for TX stopped status=0x%08x\n",
1171 val);
1172 break;
1173 }
1174 mdelay(1);
1175
1176 /* Check TX Command reg that all Txqs are stopped */
1177 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1178
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001179 } while (val & MVNETA_TXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001180
1181 /* Double check to verify that TX FIFO is empty */
1182 count = 0;
1183 do {
1184 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1185 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001186 "TX FIFO empty timeout status=0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001187 val);
1188 break;
1189 }
1190 mdelay(1);
1191
1192 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1193 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1194 (val & MVNETA_TX_IN_PRGRS));
1195
1196 udelay(200);
1197}
1198
1199/* Enable the port by setting the port enable bit of the MAC control register */
1200static void mvneta_port_enable(struct mvneta_port *pp)
1201{
1202 u32 val;
1203
1204 /* Enable port */
1205 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1206 val |= MVNETA_GMAC0_PORT_ENABLE;
1207 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1208}
1209
1210/* Disable the port and wait for about 200 usec before retuning */
1211static void mvneta_port_disable(struct mvneta_port *pp)
1212{
1213 u32 val;
1214
1215 /* Reset the Enable bit in the Serial Control Register */
1216 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1217 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1218 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1219
1220 udelay(200);
1221}
1222
1223/* Multicast tables methods */
1224
1225/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1226static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1227{
1228 int offset;
1229 u32 val;
1230
1231 if (queue == -1) {
1232 val = 0;
1233 } else {
1234 val = 0x1 | (queue << 1);
1235 val |= (val << 24) | (val << 16) | (val << 8);
1236 }
1237
1238 for (offset = 0; offset <= 0xc; offset += 4)
1239 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1240}
1241
1242/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1243static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1244{
1245 int offset;
1246 u32 val;
1247
1248 if (queue == -1) {
1249 val = 0;
1250 } else {
1251 val = 0x1 | (queue << 1);
1252 val |= (val << 24) | (val << 16) | (val << 8);
1253 }
1254
1255 for (offset = 0; offset <= 0xfc; offset += 4)
1256 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1257
1258}
1259
1260/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1261static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1262{
1263 int offset;
1264 u32 val;
1265
1266 if (queue == -1) {
1267 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1268 val = 0;
1269 } else {
1270 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1271 val = 0x1 | (queue << 1);
1272 val |= (val << 24) | (val << 16) | (val << 8);
1273 }
1274
1275 for (offset = 0; offset <= 0xfc; offset += 4)
1276 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1277}
1278
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001279static void mvneta_percpu_unmask_interrupt(void *arg)
1280{
1281 struct mvneta_port *pp = arg;
1282
1283 /* All the queue are unmasked, but actually only the ones
1284 * mapped to this CPU will be unmasked
1285 */
1286 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1287 MVNETA_RX_INTR_MASK_ALL |
1288 MVNETA_TX_INTR_MASK_ALL |
1289 MVNETA_MISCINTR_INTR_MASK);
1290}
1291
1292static void mvneta_percpu_mask_interrupt(void *arg)
1293{
1294 struct mvneta_port *pp = arg;
1295
1296 /* All the queue are masked, but actually only the ones
1297 * mapped to this CPU will be masked
1298 */
1299 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1300 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1301 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1302}
1303
1304static void mvneta_percpu_clear_intr_cause(void *arg)
1305{
1306 struct mvneta_port *pp = arg;
1307
1308 /* All the queue are cleared, but actually only the ones
1309 * mapped to this CPU will be cleared
1310 */
1311 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1312 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1313 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1314}
1315
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001316/* This method sets defaults to the NETA port:
1317 * Clears interrupt Cause and Mask registers.
1318 * Clears all MAC tables.
1319 * Sets defaults to all registers.
1320 * Resets RX and TX descriptor rings.
1321 * Resets PHY.
1322 * This method can be called after mvneta_port_down() to return the port
1323 * settings to defaults.
1324 */
1325static void mvneta_defaults_set(struct mvneta_port *pp)
1326{
1327 int cpu;
1328 int queue;
1329 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001330 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001331
1332 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001333 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001334
1335 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001336 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001337 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1338
1339 /* Enable MBUS Retry bit16 */
1340 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1341
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001342 /* Set CPU queue access map. CPUs are assigned to the RX and
1343 * TX queues modulo their number. If there is only one TX
1344 * queue then it is assigned to the CPU associated to the
1345 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001346 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001347 for_each_present_cpu(cpu) {
1348 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001349 int rxq, txq;
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001350 if (!pp->neta_armada3700) {
1351 for (rxq = 0; rxq < rxq_number; rxq++)
1352 if ((rxq % max_cpu) == cpu)
1353 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001354
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001355 for (txq = 0; txq < txq_number; txq++)
1356 if ((txq % max_cpu) == cpu)
1357 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001358
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001359 /* With only one TX queue we configure a special case
1360 * which will allow to get all the irq on a single
1361 * CPU
1362 */
1363 if (txq_number == 1)
1364 txq_map = (cpu == pp->rxq_def) ?
1365 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001366
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001367 } else {
1368 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1369 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1370 }
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001371
1372 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1373 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001374
1375 /* Reset RX and TX DMAs */
1376 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1377 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1378
1379 /* Disable Legacy WRR, Disable EJP, Release from reset */
1380 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1381 for (queue = 0; queue < txq_number; queue++) {
1382 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1383 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1384 }
1385
1386 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1387 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1388
1389 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001390 if (pp->bm_priv)
1391 /* HW buffer management + legacy parser */
1392 val = MVNETA_ACC_MODE_EXT2;
1393 else
1394 /* SW buffer management + legacy parser */
1395 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001396 mvreg_write(pp, MVNETA_ACC_MODE, val);
1397
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001398 if (pp->bm_priv)
1399 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1400
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001401 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001402 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001403 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1404
1405 val = 0;
1406 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1407 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1408
1409 /* Build PORT_SDMA_CONFIG_REG */
1410 val = 0;
1411
1412 /* Default burst size */
1413 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1414 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001415 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001416
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001417#if defined(__BIG_ENDIAN)
1418 val |= MVNETA_DESC_SWAP;
1419#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001420
1421 /* Assign port SDMA configuration */
1422 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1423
Thomas Petazzoni71408602013-09-04 16:21:18 +02001424 /* Disable PHY polling in hardware, since we're using the
1425 * kernel phylib to do this.
1426 */
1427 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1428 val &= ~MVNETA_PHY_POLLING_ENABLE;
1429 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1430
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001431 mvneta_set_ucast_table(pp, -1);
1432 mvneta_set_special_mcast_table(pp, -1);
1433 mvneta_set_other_mcast_table(pp, -1);
1434
1435 /* Set port interrupt enable register - default enable all */
1436 mvreg_write(pp, MVNETA_INTR_ENABLE,
1437 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1438 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001439
1440 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001441}
1442
1443/* Set max sizes for tx queues */
1444static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1445
1446{
1447 u32 val, size, mtu;
1448 int queue;
1449
1450 mtu = max_tx_size * 8;
1451 if (mtu > MVNETA_TX_MTU_MAX)
1452 mtu = MVNETA_TX_MTU_MAX;
1453
1454 /* Set MTU */
1455 val = mvreg_read(pp, MVNETA_TX_MTU);
1456 val &= ~MVNETA_TX_MTU_MAX;
1457 val |= mtu;
1458 mvreg_write(pp, MVNETA_TX_MTU, val);
1459
1460 /* TX token size and all TXQs token size must be larger that MTU */
1461 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1462
1463 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1464 if (size < mtu) {
1465 size = mtu;
1466 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1467 val |= size;
1468 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1469 }
1470 for (queue = 0; queue < txq_number; queue++) {
1471 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1472
1473 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1474 if (size < mtu) {
1475 size = mtu;
1476 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1477 val |= size;
1478 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1479 }
1480 }
1481}
1482
1483/* Set unicast address */
1484static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1485 int queue)
1486{
1487 unsigned int unicast_reg;
1488 unsigned int tbl_offset;
1489 unsigned int reg_offset;
1490
1491 /* Locate the Unicast table entry */
1492 last_nibble = (0xf & last_nibble);
1493
1494 /* offset from unicast tbl base */
1495 tbl_offset = (last_nibble / 4) * 4;
1496
1497 /* offset within the above reg */
1498 reg_offset = last_nibble % 4;
1499
1500 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1501
1502 if (queue == -1) {
1503 /* Clear accepts frame bit at specified unicast DA tbl entry */
1504 unicast_reg &= ~(0xff << (8 * reg_offset));
1505 } else {
1506 unicast_reg &= ~(0xff << (8 * reg_offset));
1507 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1508 }
1509
1510 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1511}
1512
1513/* Set mac address */
1514static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1515 int queue)
1516{
1517 unsigned int mac_h;
1518 unsigned int mac_l;
1519
1520 if (queue != -1) {
1521 mac_l = (addr[4] << 8) | (addr[5]);
1522 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1523 (addr[2] << 8) | (addr[3] << 0);
1524
1525 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1526 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1527 }
1528
1529 /* Accept frames of this address */
1530 mvneta_set_ucast_addr(pp, addr[5], queue);
1531}
1532
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001533/* Set the number of packets that will be received before RX interrupt
1534 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001535 */
1536static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1537 struct mvneta_rx_queue *rxq, u32 value)
1538{
1539 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1540 value | MVNETA_RXQ_NON_OCCUPIED(0));
1541 rxq->pkts_coal = value;
1542}
1543
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001544/* Set the time delay in usec before RX interrupt will be generated by
1545 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001546 */
1547static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1548 struct mvneta_rx_queue *rxq, u32 value)
1549{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001550 u32 val;
1551 unsigned long clk_rate;
1552
1553 clk_rate = clk_get_rate(pp->clk);
1554 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001555
1556 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1557 rxq->time_coal = value;
1558}
1559
1560/* Set threshold for TX_DONE pkts coalescing */
1561static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1562 struct mvneta_tx_queue *txq, u32 value)
1563{
1564 u32 val;
1565
1566 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1567
1568 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1569 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1570
1571 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1572
1573 txq->done_pkts_coal = value;
1574}
1575
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001576/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1577static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001578 u32 phys_addr, void *virt_addr,
1579 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001580{
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001581 int i;
1582
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001583 rx_desc->buf_phys_addr = phys_addr;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001584 i = rx_desc - rxq->descs;
1585 rxq->buf_virt_addr[i] = virt_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001586}
1587
1588/* Decrement sent descriptors counter */
1589static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1590 struct mvneta_tx_queue *txq,
1591 int sent_desc)
1592{
1593 u32 val;
1594
1595 /* Only 255 TX descriptors can be updated at once */
1596 while (sent_desc > 0xff) {
1597 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1598 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1599 sent_desc = sent_desc - 0xff;
1600 }
1601
1602 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1603 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1604}
1605
1606/* Get number of TX descriptors already sent by HW */
1607static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1608 struct mvneta_tx_queue *txq)
1609{
1610 u32 val;
1611 int sent_desc;
1612
1613 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1614 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1615 MVNETA_TXQ_SENT_DESC_SHIFT;
1616
1617 return sent_desc;
1618}
1619
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001620/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001621 * The number of sent descriptors is returned.
1622 */
1623static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1624 struct mvneta_tx_queue *txq)
1625{
1626 int sent_desc;
1627
1628 /* Get number of sent descriptors */
1629 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1630
1631 /* Decrement sent descriptors counter */
1632 if (sent_desc)
1633 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1634
1635 return sent_desc;
1636}
1637
1638/* Set TXQ descriptors fields relevant for CSUM calculation */
1639static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1640 int ip_hdr_len, int l4_proto)
1641{
1642 u32 command;
1643
1644 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001645 * G_L4_chk, L4_type; required only for checksum
1646 * calculation
1647 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001648 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1649 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1650
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001651 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001652 command |= MVNETA_TXD_IP_CSUM;
1653 else
1654 command |= MVNETA_TX_L3_IP6;
1655
1656 if (l4_proto == IPPROTO_TCP)
1657 command |= MVNETA_TX_L4_CSUM_FULL;
1658 else if (l4_proto == IPPROTO_UDP)
1659 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1660 else
1661 command |= MVNETA_TX_L4_CSUM_NOT;
1662
1663 return command;
1664}
1665
1666
1667/* Display more error info */
1668static void mvneta_rx_error(struct mvneta_port *pp,
1669 struct mvneta_rx_desc *rx_desc)
1670{
1671 u32 status = rx_desc->status;
1672
willy tarreau54282132014-01-16 08:20:14 +01001673 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001674 netdev_err(pp->dev,
1675 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001676 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001677 return;
1678 }
1679
1680 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1681 case MVNETA_RXD_ERR_CRC:
1682 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1683 status, rx_desc->data_size);
1684 break;
1685 case MVNETA_RXD_ERR_OVERRUN:
1686 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1687 status, rx_desc->data_size);
1688 break;
1689 case MVNETA_RXD_ERR_LEN:
1690 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1691 status, rx_desc->data_size);
1692 break;
1693 case MVNETA_RXD_ERR_RESOURCE:
1694 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1695 status, rx_desc->data_size);
1696 break;
1697 }
1698}
1699
willy tarreau54282132014-01-16 08:20:14 +01001700/* Handle RX checksum offload based on the descriptor's status */
1701static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001702 struct sk_buff *skb)
1703{
willy tarreau54282132014-01-16 08:20:14 +01001704 if ((status & MVNETA_RXD_L3_IP4) &&
1705 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001706 skb->csum = 0;
1707 skb->ip_summed = CHECKSUM_UNNECESSARY;
1708 return;
1709 }
1710
1711 skb->ip_summed = CHECKSUM_NONE;
1712}
1713
willy tarreau6c498972014-01-16 08:20:12 +01001714/* Return tx queue pointer (find last set bit) according to <cause> returned
1715 * form tx_done reg. <cause> must not be null. The return value is always a
1716 * valid queue for matching the first one found in <cause>.
1717 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001718static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1719 u32 cause)
1720{
1721 int queue = fls(cause) - 1;
1722
willy tarreau6c498972014-01-16 08:20:12 +01001723 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001724}
1725
1726/* Free tx queue skbuffs */
1727static void mvneta_txq_bufs_free(struct mvneta_port *pp,
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001728 struct mvneta_tx_queue *txq, int num,
1729 struct netdev_queue *nq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001730{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001731 unsigned int bytes_compl = 0, pkts_compl = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001732 int i;
1733
1734 for (i = 0; i < num; i++) {
1735 struct mvneta_tx_desc *tx_desc = txq->descs +
1736 txq->txq_get_index;
1737 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1738
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001739 if (skb) {
1740 bytes_compl += skb->len;
1741 pkts_compl++;
1742 }
1743
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001744 mvneta_txq_inc_get(txq);
1745
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001746 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1747 dma_unmap_single(pp->dev->dev.parent,
1748 tx_desc->buf_phys_addr,
1749 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001750 if (!skb)
1751 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001752 dev_kfree_skb_any(skb);
1753 }
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001754
1755 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001756}
1757
1758/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001759static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001760 struct mvneta_tx_queue *txq)
1761{
1762 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1763 int tx_done;
1764
1765 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001766 if (!tx_done)
1767 return;
1768
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001769 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001770
1771 txq->count -= tx_done;
1772
1773 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001774 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001775 netif_tx_wake_queue(nq);
1776 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001777}
1778
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001779void *mvneta_frag_alloc(unsigned int frag_size)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001780{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001781 if (likely(frag_size <= PAGE_SIZE))
1782 return netdev_alloc_frag(frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001783 else
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001784 return kmalloc(frag_size, GFP_ATOMIC);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001785}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001786EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001787
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001788void mvneta_frag_free(unsigned int frag_size, void *data)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001789{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001790 if (likely(frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001791 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001792 else
1793 kfree(data);
1794}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001795EXPORT_SYMBOL_GPL(mvneta_frag_free);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001796
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001797/* Refill processing for SW buffer management */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001798static int mvneta_rx_refill(struct mvneta_port *pp,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001799 struct mvneta_rx_desc *rx_desc,
1800 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001801
1802{
1803 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001804 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001805
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001806 data = mvneta_frag_alloc(pp->frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001807 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001808 return -ENOMEM;
1809
willy tarreau8ec2cd42014-01-16 08:20:16 +01001810 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001811 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1812 DMA_FROM_DEVICE);
1813 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001814 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001815 return -ENOMEM;
1816 }
1817
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01001818 phys_addr += pp->rx_offset_correction;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001819 mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001820 return 0;
1821}
1822
1823/* Handle tx checksum */
1824static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1825{
1826 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1827 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001828 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001829 u8 l4_proto;
1830
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001831 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001832 struct iphdr *ip4h = ip_hdr(skb);
1833
1834 /* Calculate IPv4 checksum and L4 checksum */
1835 ip_hdr_len = ip4h->ihl;
1836 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001837 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001838 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1839
1840 /* Read l4_protocol from one of IPv6 extra headers */
1841 if (skb_network_header_len(skb) > 0)
1842 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1843 l4_proto = ip6h->nexthdr;
1844 } else
1845 return MVNETA_TX_L4_CSUM_NOT;
1846
1847 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001848 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001849 }
1850
1851 return MVNETA_TX_L4_CSUM_NOT;
1852}
1853
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001854/* Drop packets received by the RXQ and free buffers */
1855static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1856 struct mvneta_rx_queue *rxq)
1857{
1858 int rx_done, i;
1859
1860 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001861 if (rx_done)
1862 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1863
1864 if (pp->bm_priv) {
1865 for (i = 0; i < rx_done; i++) {
1866 struct mvneta_rx_desc *rx_desc =
1867 mvneta_rxq_next_desc_get(rxq);
1868 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1869 struct mvneta_bm_pool *bm_pool;
1870
1871 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1872 /* Return dropped buffer to the pool */
1873 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1874 rx_desc->buf_phys_addr);
1875 }
1876 return;
1877 }
1878
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001879 for (i = 0; i < rxq->size; i++) {
1880 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001881 void *data = rxq->buf_virt_addr[i];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001882
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001883 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001884 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001885 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001886 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001887}
1888
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001889/* Main rx processing when using software buffer management */
1890static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1891 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001892{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001893 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001894 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001895 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001896 u32 rcvd_pkts = 0;
1897 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001898
1899 /* Get number of received packets */
1900 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1901
1902 if (rx_todo > rx_done)
1903 rx_todo = rx_done;
1904
1905 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001906
1907 /* Fairness NAPI loop */
1908 while (rx_done < rx_todo) {
1909 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1910 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001911 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001912 dma_addr_t phys_addr;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001913 u32 rx_status, frag_size;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001914 int rx_bytes, err, index;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001915
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001916 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001917 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001918 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001919 index = rx_desc - rxq->descs;
1920 data = rxq->buf_virt_addr[index];
Simon Guinotdaf158d2015-09-15 22:41:21 +02001921 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001922
willy tarreau54282132014-01-16 08:20:14 +01001923 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001924 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
Yelena Krivosheev2eecb2e2017-12-19 17:59:47 +01001925 mvneta_rx_error(pp, rx_desc);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001926err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001927 dev->stats.rx_errors++;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001928 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001929 continue;
1930 }
1931
willy tarreauf19fadf2014-01-16 08:20:17 +01001932 if (rx_bytes <= rx_copybreak) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001933 /* better copy a small frame and not unmap the DMA region */
willy tarreauf19fadf2014-01-16 08:20:17 +01001934 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1935 if (unlikely(!skb))
1936 goto err_drop_frame;
1937
1938 dma_sync_single_range_for_cpu(dev->dev.parent,
Gregory CLEMENTac83b7d2016-12-01 18:03:04 +01001939 phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001940 MVNETA_MH_SIZE + NET_SKB_PAD,
1941 rx_bytes,
1942 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02001943 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
1944 rx_bytes);
willy tarreauf19fadf2014-01-16 08:20:17 +01001945
1946 skb->protocol = eth_type_trans(skb, dev);
1947 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001948 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001949
1950 rcvd_pkts++;
1951 rcvd_bytes += rx_bytes;
1952
1953 /* leave the descriptor and buffer untouched */
1954 continue;
1955 }
1956
Simon Guinota84e3282015-07-19 13:00:53 +02001957 /* Refill processing */
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001958 err = mvneta_rx_refill(pp, rx_desc, rxq);
Simon Guinota84e3282015-07-19 13:00:53 +02001959 if (err) {
1960 netdev_err(dev, "Linux processing - Can't refill\n");
1961 rxq->missed++;
1962 goto err_drop_frame;
1963 }
1964
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001965 frag_size = pp->frag_size;
1966
1967 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01001968
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001969 /* After refill old buffer has to be unmapped regardless
1970 * the skb is successfully built or not.
1971 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02001972 dma_unmap_single(dev->dev.parent, phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001973 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1974 DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001975
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001976 if (!skb)
1977 goto err_drop_frame;
1978
willy tarreaudc4277d2014-01-16 08:20:07 +01001979 rcvd_pkts++;
1980 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001981
1982 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001983 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001984 skb_put(skb, rx_bytes);
1985
1986 skb->protocol = eth_type_trans(skb, dev);
1987
willy tarreau54282132014-01-16 08:20:14 +01001988 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001989
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001990 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001991 }
1992
willy tarreaudc4277d2014-01-16 08:20:07 +01001993 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001994 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1995
1996 u64_stats_update_begin(&stats->syncp);
1997 stats->rx_packets += rcvd_pkts;
1998 stats->rx_bytes += rcvd_bytes;
1999 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01002000 }
2001
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002002 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02002003 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002004
2005 return rx_done;
2006}
2007
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002008/* Main rx processing when using hardware buffer management */
2009static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2010 struct mvneta_rx_queue *rxq)
2011{
2012 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2013 struct net_device *dev = pp->dev;
2014 int rx_done;
2015 u32 rcvd_pkts = 0;
2016 u32 rcvd_bytes = 0;
2017
2018 /* Get number of received packets */
2019 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2020
2021 if (rx_todo > rx_done)
2022 rx_todo = rx_done;
2023
2024 rx_done = 0;
2025
2026 /* Fairness NAPI loop */
2027 while (rx_done < rx_todo) {
2028 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2029 struct mvneta_bm_pool *bm_pool = NULL;
2030 struct sk_buff *skb;
2031 unsigned char *data;
2032 dma_addr_t phys_addr;
2033 u32 rx_status, frag_size;
2034 int rx_bytes, err;
2035 u8 pool_id;
2036
2037 rx_done++;
2038 rx_status = rx_desc->status;
2039 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002040 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002041 phys_addr = rx_desc->buf_phys_addr;
2042 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2043 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2044
2045 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2046 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2047err_drop_frame_ret_pool:
2048 /* Return the buffer to the pool */
2049 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2050 rx_desc->buf_phys_addr);
2051err_drop_frame:
2052 dev->stats.rx_errors++;
2053 mvneta_rx_error(pp, rx_desc);
2054 /* leave the descriptor untouched */
2055 continue;
2056 }
2057
2058 if (rx_bytes <= rx_copybreak) {
2059 /* better copy a small frame and not unmap the DMA region */
2060 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2061 if (unlikely(!skb))
2062 goto err_drop_frame_ret_pool;
2063
2064 dma_sync_single_range_for_cpu(dev->dev.parent,
2065 rx_desc->buf_phys_addr,
2066 MVNETA_MH_SIZE + NET_SKB_PAD,
2067 rx_bytes,
2068 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02002069 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2070 rx_bytes);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002071
2072 skb->protocol = eth_type_trans(skb, dev);
2073 mvneta_rx_csum(pp, rx_status, skb);
2074 napi_gro_receive(&port->napi, skb);
2075
2076 rcvd_pkts++;
2077 rcvd_bytes += rx_bytes;
2078
2079 /* Return the buffer to the pool */
2080 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2081 rx_desc->buf_phys_addr);
2082
2083 /* leave the descriptor and buffer untouched */
2084 continue;
2085 }
2086
2087 /* Refill processing */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002088 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002089 if (err) {
2090 netdev_err(dev, "Linux processing - Can't refill\n");
2091 rxq->missed++;
2092 goto err_drop_frame_ret_pool;
2093 }
2094
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002095 frag_size = bm_pool->hwbm_pool.frag_size;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002096
2097 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2098
2099 /* After refill old buffer has to be unmapped regardless
2100 * the skb is successfully built or not.
2101 */
2102 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2103 bm_pool->buf_size, DMA_FROM_DEVICE);
2104 if (!skb)
2105 goto err_drop_frame;
2106
2107 rcvd_pkts++;
2108 rcvd_bytes += rx_bytes;
2109
2110 /* Linux processing */
2111 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2112 skb_put(skb, rx_bytes);
2113
2114 skb->protocol = eth_type_trans(skb, dev);
2115
2116 mvneta_rx_csum(pp, rx_status, skb);
2117
2118 napi_gro_receive(&port->napi, skb);
2119 }
2120
2121 if (rcvd_pkts) {
2122 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2123
2124 u64_stats_update_begin(&stats->syncp);
2125 stats->rx_packets += rcvd_pkts;
2126 stats->rx_bytes += rcvd_bytes;
2127 u64_stats_update_end(&stats->syncp);
2128 }
2129
2130 /* Update rxq management counters */
2131 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2132
2133 return rx_done;
2134}
2135
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002136static inline void
2137mvneta_tso_put_hdr(struct sk_buff *skb,
2138 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2139{
2140 struct mvneta_tx_desc *tx_desc;
2141 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2142
2143 txq->tx_skb[txq->txq_put_index] = NULL;
2144 tx_desc = mvneta_txq_next_desc_get(txq);
2145 tx_desc->data_size = hdr_len;
2146 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2147 tx_desc->command |= MVNETA_TXD_F_DESC;
2148 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2149 txq->txq_put_index * TSO_HEADER_SIZE;
2150 mvneta_txq_inc_put(txq);
2151}
2152
2153static inline int
2154mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2155 struct sk_buff *skb, char *data, int size,
2156 bool last_tcp, bool is_last)
2157{
2158 struct mvneta_tx_desc *tx_desc;
2159
2160 tx_desc = mvneta_txq_next_desc_get(txq);
2161 tx_desc->data_size = size;
2162 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2163 size, DMA_TO_DEVICE);
2164 if (unlikely(dma_mapping_error(dev->dev.parent,
2165 tx_desc->buf_phys_addr))) {
2166 mvneta_txq_desc_put(txq);
2167 return -ENOMEM;
2168 }
2169
2170 tx_desc->command = 0;
2171 txq->tx_skb[txq->txq_put_index] = NULL;
2172
2173 if (last_tcp) {
2174 /* last descriptor in the TCP packet */
2175 tx_desc->command = MVNETA_TXD_L_DESC;
2176
2177 /* last descriptor in SKB */
2178 if (is_last)
2179 txq->tx_skb[txq->txq_put_index] = skb;
2180 }
2181 mvneta_txq_inc_put(txq);
2182 return 0;
2183}
2184
2185static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2186 struct mvneta_tx_queue *txq)
2187{
2188 int total_len, data_left;
2189 int desc_count = 0;
2190 struct mvneta_port *pp = netdev_priv(dev);
2191 struct tso_t tso;
2192 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2193 int i;
2194
2195 /* Count needed descriptors */
2196 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2197 return 0;
2198
2199 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2200 pr_info("*** Is this even possible???!?!?\n");
2201 return 0;
2202 }
2203
2204 /* Initialize the TSO handler, and prepare the first payload */
2205 tso_start(skb, &tso);
2206
2207 total_len = skb->len - hdr_len;
2208 while (total_len > 0) {
2209 char *hdr;
2210
2211 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2212 total_len -= data_left;
2213 desc_count++;
2214
2215 /* prepare packet headers: MAC + IP + TCP */
2216 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2217 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2218
2219 mvneta_tso_put_hdr(skb, pp, txq);
2220
2221 while (data_left > 0) {
2222 int size;
2223 desc_count++;
2224
2225 size = min_t(int, tso.size, data_left);
2226
2227 if (mvneta_tso_put_data(dev, txq, skb,
2228 tso.data, size,
2229 size == data_left,
2230 total_len == 0))
2231 goto err_release;
2232 data_left -= size;
2233
2234 tso_build_data(skb, &tso, size);
2235 }
2236 }
2237
2238 return desc_count;
2239
2240err_release:
2241 /* Release all used data descriptors; header descriptors must not
2242 * be DMA-unmapped.
2243 */
2244 for (i = desc_count - 1; i >= 0; i--) {
2245 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002246 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002247 dma_unmap_single(pp->dev->dev.parent,
2248 tx_desc->buf_phys_addr,
2249 tx_desc->data_size,
2250 DMA_TO_DEVICE);
2251 mvneta_txq_desc_put(txq);
2252 }
2253 return 0;
2254}
2255
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002256/* Handle tx fragmentation processing */
2257static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2258 struct mvneta_tx_queue *txq)
2259{
2260 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002261 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002262
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002263 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002264 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2265 void *addr = page_address(frag->page.p) + frag->page_offset;
2266
2267 tx_desc = mvneta_txq_next_desc_get(txq);
2268 tx_desc->data_size = frag->size;
2269
2270 tx_desc->buf_phys_addr =
2271 dma_map_single(pp->dev->dev.parent, addr,
2272 tx_desc->data_size, DMA_TO_DEVICE);
2273
2274 if (dma_mapping_error(pp->dev->dev.parent,
2275 tx_desc->buf_phys_addr)) {
2276 mvneta_txq_desc_put(txq);
2277 goto error;
2278 }
2279
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002280 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002281 /* Last descriptor */
2282 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002283 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002284 } else {
2285 /* Descriptor in the middle: Not First, Not Last */
2286 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002287 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002288 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002289 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002290 }
2291
2292 return 0;
2293
2294error:
2295 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002296 * this packet, as well as the corresponding DMA mappings
2297 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002298 for (i = i - 1; i >= 0; i--) {
2299 tx_desc = txq->descs + i;
2300 dma_unmap_single(pp->dev->dev.parent,
2301 tx_desc->buf_phys_addr,
2302 tx_desc->data_size,
2303 DMA_TO_DEVICE);
2304 mvneta_txq_desc_put(txq);
2305 }
2306
2307 return -ENOMEM;
2308}
2309
2310/* Main tx processing */
2311static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2312{
2313 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002314 u16 txq_id = skb_get_queue_mapping(skb);
2315 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002316 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002317 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002318 int frags = 0;
2319 u32 tx_cmd;
2320
2321 if (!netif_running(dev))
2322 goto out;
2323
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002324 if (skb_is_gso(skb)) {
2325 frags = mvneta_tx_tso(skb, dev, txq);
2326 goto out;
2327 }
2328
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002329 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002330
2331 /* Get a descriptor for the first part of the packet */
2332 tx_desc = mvneta_txq_next_desc_get(txq);
2333
2334 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2335
2336 tx_desc->data_size = skb_headlen(skb);
2337
2338 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2339 tx_desc->data_size,
2340 DMA_TO_DEVICE);
2341 if (unlikely(dma_mapping_error(dev->dev.parent,
2342 tx_desc->buf_phys_addr))) {
2343 mvneta_txq_desc_put(txq);
2344 frags = 0;
2345 goto out;
2346 }
2347
2348 if (frags == 1) {
2349 /* First and Last descriptor */
2350 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2351 tx_desc->command = tx_cmd;
2352 txq->tx_skb[txq->txq_put_index] = skb;
2353 mvneta_txq_inc_put(txq);
2354 } else {
2355 /* First but not Last */
2356 tx_cmd |= MVNETA_TXD_F_DESC;
2357 txq->tx_skb[txq->txq_put_index] = NULL;
2358 mvneta_txq_inc_put(txq);
2359 tx_desc->command = tx_cmd;
2360 /* Continue with other skb fragments */
2361 if (mvneta_tx_frag_process(pp, skb, txq)) {
2362 dma_unmap_single(dev->dev.parent,
2363 tx_desc->buf_phys_addr,
2364 tx_desc->data_size,
2365 DMA_TO_DEVICE);
2366 mvneta_txq_desc_put(txq);
2367 frags = 0;
2368 goto out;
2369 }
2370 }
2371
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002372out:
2373 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01002374 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002375 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2376
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002377 netdev_tx_sent_queue(nq, len);
2378
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002379 txq->count += frags;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002380 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002381 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002382
Simon Guinot2a90f7e2017-01-16 18:08:31 +01002383 if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2384 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2385 mvneta_txq_pend_desc_add(pp, txq, frags);
2386 else
2387 txq->pending += frags;
2388
willy tarreau74c41b02014-01-16 08:20:08 +01002389 u64_stats_update_begin(&stats->syncp);
2390 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002391 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01002392 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002393 } else {
2394 dev->stats.tx_dropped++;
2395 dev_kfree_skb_any(skb);
2396 }
2397
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002398 return NETDEV_TX_OK;
2399}
2400
2401
2402/* Free tx resources, when resetting a port */
2403static void mvneta_txq_done_force(struct mvneta_port *pp,
2404 struct mvneta_tx_queue *txq)
2405
2406{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002407 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002408 int tx_done = txq->count;
2409
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002410 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002411
2412 /* reset txq */
2413 txq->count = 0;
2414 txq->txq_put_index = 0;
2415 txq->txq_get_index = 0;
2416}
2417
willy tarreau6c498972014-01-16 08:20:12 +01002418/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2419 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2420 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002421static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002422{
2423 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002424 struct netdev_queue *nq;
2425
willy tarreau6c498972014-01-16 08:20:12 +01002426 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002427 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002428
2429 nq = netdev_get_tx_queue(pp->dev, txq->id);
2430 __netif_tx_lock(nq, smp_processor_id());
2431
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002432 if (txq->count)
2433 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002434
2435 __netif_tx_unlock(nq);
2436 cause_tx_done &= ~((1 << txq->id));
2437 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002438}
2439
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002440/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002441 * according to hw spec, different than generic crc8 algorithm
2442 */
2443static int mvneta_addr_crc(unsigned char *addr)
2444{
2445 int crc = 0;
2446 int i;
2447
2448 for (i = 0; i < ETH_ALEN; i++) {
2449 int j;
2450
2451 crc = (crc ^ addr[i]) << 8;
2452 for (j = 7; j >= 0; j--) {
2453 if (crc & (0x100 << j))
2454 crc ^= 0x107 << j;
2455 }
2456 }
2457
2458 return crc;
2459}
2460
2461/* This method controls the net device special MAC multicast support.
2462 * The Special Multicast Table for MAC addresses supports MAC of the form
2463 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2464 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2465 * Table entries in the DA-Filter table. This method set the Special
2466 * Multicast Table appropriate entry.
2467 */
2468static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2469 unsigned char last_byte,
2470 int queue)
2471{
2472 unsigned int smc_table_reg;
2473 unsigned int tbl_offset;
2474 unsigned int reg_offset;
2475
2476 /* Register offset from SMC table base */
2477 tbl_offset = (last_byte / 4);
2478 /* Entry offset within the above reg */
2479 reg_offset = last_byte % 4;
2480
2481 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2482 + tbl_offset * 4));
2483
2484 if (queue == -1)
2485 smc_table_reg &= ~(0xff << (8 * reg_offset));
2486 else {
2487 smc_table_reg &= ~(0xff << (8 * reg_offset));
2488 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2489 }
2490
2491 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2492 smc_table_reg);
2493}
2494
2495/* This method controls the network device Other MAC multicast support.
2496 * The Other Multicast Table is used for multicast of another type.
2497 * A CRC-8 is used as an index to the Other Multicast Table entries
2498 * in the DA-Filter table.
2499 * The method gets the CRC-8 value from the calling routine and
2500 * sets the Other Multicast Table appropriate entry according to the
2501 * specified CRC-8 .
2502 */
2503static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2504 unsigned char crc8,
2505 int queue)
2506{
2507 unsigned int omc_table_reg;
2508 unsigned int tbl_offset;
2509 unsigned int reg_offset;
2510
2511 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2512 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2513
2514 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2515
2516 if (queue == -1) {
2517 /* Clear accepts frame bit at specified Other DA table entry */
2518 omc_table_reg &= ~(0xff << (8 * reg_offset));
2519 } else {
2520 omc_table_reg &= ~(0xff << (8 * reg_offset));
2521 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2522 }
2523
2524 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2525}
2526
2527/* The network device supports multicast using two tables:
2528 * 1) Special Multicast Table for MAC addresses of the form
2529 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2530 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2531 * Table entries in the DA-Filter table.
2532 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2533 * is used as an index to the Other Multicast Table entries in the
2534 * DA-Filter table.
2535 */
2536static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2537 int queue)
2538{
2539 unsigned char crc_result = 0;
2540
2541 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2542 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2543 return 0;
2544 }
2545
2546 crc_result = mvneta_addr_crc(p_addr);
2547 if (queue == -1) {
2548 if (pp->mcast_count[crc_result] == 0) {
2549 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2550 crc_result);
2551 return -EINVAL;
2552 }
2553
2554 pp->mcast_count[crc_result]--;
2555 if (pp->mcast_count[crc_result] != 0) {
2556 netdev_info(pp->dev,
2557 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2558 pp->mcast_count[crc_result], crc_result);
2559 return -EINVAL;
2560 }
2561 } else
2562 pp->mcast_count[crc_result]++;
2563
2564 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2565
2566 return 0;
2567}
2568
2569/* Configure Fitering mode of Ethernet port */
2570static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2571 int is_promisc)
2572{
2573 u32 port_cfg_reg, val;
2574
2575 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2576
2577 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2578
2579 /* Set / Clear UPM bit in port configuration register */
2580 if (is_promisc) {
2581 /* Accept all Unicast addresses */
2582 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2583 val |= MVNETA_FORCE_UNI;
2584 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2585 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2586 } else {
2587 /* Reject all Unicast addresses */
2588 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2589 val &= ~MVNETA_FORCE_UNI;
2590 }
2591
2592 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2593 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2594}
2595
2596/* register unicast and multicast addresses */
2597static void mvneta_set_rx_mode(struct net_device *dev)
2598{
2599 struct mvneta_port *pp = netdev_priv(dev);
2600 struct netdev_hw_addr *ha;
2601
2602 if (dev->flags & IFF_PROMISC) {
2603 /* Accept all: Multicast + Unicast */
2604 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002605 mvneta_set_ucast_table(pp, pp->rxq_def);
2606 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2607 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002608 } else {
2609 /* Accept single Unicast */
2610 mvneta_rx_unicast_promisc_set(pp, 0);
2611 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002612 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002613
2614 if (dev->flags & IFF_ALLMULTI) {
2615 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002616 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2617 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002618 } else {
2619 /* Accept only initialized multicast */
2620 mvneta_set_special_mcast_table(pp, -1);
2621 mvneta_set_other_mcast_table(pp, -1);
2622
2623 if (!netdev_mc_empty(dev)) {
2624 netdev_for_each_mc_addr(ha, dev) {
2625 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002626 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002627 }
2628 }
2629 }
2630 }
2631}
2632
2633/* Interrupt handling - the callback for request_irq() */
2634static irqreturn_t mvneta_isr(int irq, void *dev_id)
2635{
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002636 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2637
2638 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2639 napi_schedule(&pp->napi);
2640
2641 return IRQ_HANDLED;
2642}
2643
2644/* Interrupt handling - the callback for request_percpu_irq() */
2645static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2646{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002647 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002648
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002649 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002650 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002651
2652 return IRQ_HANDLED;
2653}
2654
Russell King503f9aa92018-01-02 17:24:44 +00002655static void mvneta_link_change(struct mvneta_port *pp)
Stas Sergeev898b29702015-04-01 20:32:49 +03002656{
Stas Sergeev898b29702015-04-01 20:32:49 +03002657 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2658
Russell King503f9aa92018-01-02 17:24:44 +00002659 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
Stas Sergeev898b29702015-04-01 20:32:49 +03002660}
2661
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002662/* NAPI handler
2663 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2664 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2665 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2666 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2667 * Each CPU has its own causeRxTx register
2668 */
2669static int mvneta_poll(struct napi_struct *napi, int budget)
2670{
2671 int rx_done = 0;
2672 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002673 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002674 struct mvneta_port *pp = netdev_priv(napi->dev);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002675 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002676
2677 if (!netif_running(pp->dev)) {
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002678 napi_complete(napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002679 return rx_done;
2680 }
2681
2682 /* Read cause register */
Stas Sergeev898b29702015-04-01 20:32:49 +03002683 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2684 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2685 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2686
2687 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
Russell King503f9aa92018-01-02 17:24:44 +00002688
2689 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2690 MVNETA_CAUSE_LINK_CHANGE |
2691 MVNETA_CAUSE_PSC_SYNC_CHANGE))
2692 mvneta_link_change(pp);
Stas Sergeev898b29702015-04-01 20:32:49 +03002693 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002694
2695 /* Release Tx descriptors */
2696 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002697 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002698 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2699 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002700
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002701 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002702 * RX packets
2703 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002704 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2705
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002706 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2707 port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002708
2709 if (rx_queue) {
2710 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002711 if (pp->bm_priv)
2712 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2713 else
2714 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002715 }
2716
Eric Dumazet6ad20162017-01-30 08:22:01 -08002717 if (rx_done < budget) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002718 cause_rx_tx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002719 napi_complete_done(napi, rx_done);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002720
2721 if (pp->neta_armada3700) {
2722 unsigned long flags;
2723
2724 local_irq_save(flags);
2725 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2726 MVNETA_RX_INTR_MASK(rxq_number) |
2727 MVNETA_TX_INTR_MASK(txq_number) |
2728 MVNETA_MISCINTR_INTR_MASK);
2729 local_irq_restore(flags);
2730 } else {
2731 enable_percpu_irq(pp->dev->irq, 0);
2732 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002733 }
2734
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002735 if (pp->neta_armada3700)
2736 pp->cause_rx_tx = cause_rx_tx;
2737 else
2738 port->cause_rx_tx = cause_rx_tx;
2739
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002740 return rx_done;
2741}
2742
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002743/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2744static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2745 int num)
2746{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002747 int i;
2748
2749 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002750 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002751 if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002752 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002753 __func__, rxq->id, i, num);
2754 break;
2755 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002756 }
2757
2758 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002759 * get packets)
2760 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002761 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2762
2763 return i;
2764}
2765
2766/* Free all packets pending transmit from all TXQs and reset TX port */
2767static void mvneta_tx_reset(struct mvneta_port *pp)
2768{
2769 int queue;
2770
Ezequiel Garcia96728502014-05-22 20:06:59 -03002771 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002772 for (queue = 0; queue < txq_number; queue++)
2773 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2774
2775 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2776 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2777}
2778
2779static void mvneta_rx_reset(struct mvneta_port *pp)
2780{
2781 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2782 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2783}
2784
2785/* Rx/Tx queue initialization/cleanup methods */
2786
2787/* Create a specified RX queue */
2788static int mvneta_rxq_init(struct mvneta_port *pp,
2789 struct mvneta_rx_queue *rxq)
2790
2791{
2792 rxq->size = pp->rx_ring_size;
2793
2794 /* Allocate memory for RX descriptors */
2795 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2796 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2797 &rxq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002798 if (!rxq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002799 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002800
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002801 rxq->last_desc = rxq->size - 1;
2802
2803 /* Set Rx descriptors queue starting address */
2804 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2805 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2806
2807 /* Set Offset */
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01002808 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002809
2810 /* Set coalescing pkts and time */
2811 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2812 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2813
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002814 if (!pp->bm_priv) {
2815 /* Fill RXQ with buffers from RX pool */
2816 mvneta_rxq_buf_size_set(pp, rxq,
2817 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2818 mvneta_rxq_bm_disable(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002819 mvneta_rxq_fill(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002820 } else {
2821 mvneta_rxq_bm_enable(pp, rxq);
2822 mvneta_rxq_long_pool_set(pp, rxq);
2823 mvneta_rxq_short_pool_set(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002824 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002825 }
2826
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002827 return 0;
2828}
2829
2830/* Cleanup Rx queue */
2831static void mvneta_rxq_deinit(struct mvneta_port *pp,
2832 struct mvneta_rx_queue *rxq)
2833{
2834 mvneta_rxq_drop_pkts(pp, rxq);
2835
2836 if (rxq->descs)
2837 dma_free_coherent(pp->dev->dev.parent,
2838 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2839 rxq->descs,
2840 rxq->descs_phys);
2841
2842 rxq->descs = NULL;
2843 rxq->last_desc = 0;
2844 rxq->next_desc_to_proc = 0;
2845 rxq->descs_phys = 0;
2846}
2847
2848/* Create and initialize a tx queue */
2849static int mvneta_txq_init(struct mvneta_port *pp,
2850 struct mvneta_tx_queue *txq)
2851{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002852 int cpu;
2853
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002854 txq->size = pp->tx_ring_size;
2855
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002856 /* A queue must always have room for at least one skb.
2857 * Therefore, stop the queue when the free entries reaches
2858 * the maximum number of descriptors per skb.
2859 */
2860 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2861 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2862
2863
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002864 /* Allocate memory for TX descriptors */
2865 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2866 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2867 &txq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002868 if (!txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002869 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002870
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002871 txq->last_desc = txq->size - 1;
2872
2873 /* Set maximum bandwidth for enabled TXQs */
2874 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2875 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2876
2877 /* Set Tx descriptors queue starting address */
2878 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2879 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2880
Markus Elfringd441b682017-04-16 22:11:22 +02002881 txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
2882 GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002883 if (!txq->tx_skb) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002884 dma_free_coherent(pp->dev->dev.parent,
2885 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2886 txq->descs, txq->descs_phys);
2887 return -ENOMEM;
2888 }
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002889
2890 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2891 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2892 txq->size * TSO_HEADER_SIZE,
2893 &txq->tso_hdrs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002894 if (!txq->tso_hdrs) {
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002895 kfree(txq->tx_skb);
2896 dma_free_coherent(pp->dev->dev.parent,
2897 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2898 txq->descs, txq->descs_phys);
2899 return -ENOMEM;
2900 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002901 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2902
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002903 /* Setup XPS mapping */
2904 if (txq_number > 1)
2905 cpu = txq->id % num_present_cpus();
2906 else
2907 cpu = pp->rxq_def % num_present_cpus();
2908 cpumask_set_cpu(cpu, &txq->affinity_mask);
2909 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2910
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002911 return 0;
2912}
2913
2914/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2915static void mvneta_txq_deinit(struct mvneta_port *pp,
2916 struct mvneta_tx_queue *txq)
2917{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002918 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2919
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002920 kfree(txq->tx_skb);
2921
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002922 if (txq->tso_hdrs)
2923 dma_free_coherent(pp->dev->dev.parent,
2924 txq->size * TSO_HEADER_SIZE,
2925 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002926 if (txq->descs)
2927 dma_free_coherent(pp->dev->dev.parent,
2928 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2929 txq->descs, txq->descs_phys);
2930
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002931 netdev_tx_reset_queue(nq);
2932
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002933 txq->descs = NULL;
2934 txq->last_desc = 0;
2935 txq->next_desc_to_proc = 0;
2936 txq->descs_phys = 0;
2937
2938 /* Set minimum bandwidth for disabled TXQs */
2939 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2940 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2941
2942 /* Set Tx descriptors queue starting address and size */
2943 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2944 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2945}
2946
2947/* Cleanup all Tx queues */
2948static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2949{
2950 int queue;
2951
2952 for (queue = 0; queue < txq_number; queue++)
2953 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2954}
2955
2956/* Cleanup all Rx queues */
2957static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2958{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002959 int queue;
2960
Yelena Krivosheevca5902a2017-12-19 17:59:46 +01002961 for (queue = 0; queue < rxq_number; queue++)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002962 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002963}
2964
2965
2966/* Init all Rx queues */
2967static int mvneta_setup_rxqs(struct mvneta_port *pp)
2968{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002969 int queue;
2970
2971 for (queue = 0; queue < rxq_number; queue++) {
2972 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2973
2974 if (err) {
2975 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2976 __func__, queue);
2977 mvneta_cleanup_rxqs(pp);
2978 return err;
2979 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002980 }
2981
2982 return 0;
2983}
2984
2985/* Init all tx queues */
2986static int mvneta_setup_txqs(struct mvneta_port *pp)
2987{
2988 int queue;
2989
2990 for (queue = 0; queue < txq_number; queue++) {
2991 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2992 if (err) {
2993 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2994 __func__, queue);
2995 mvneta_cleanup_txqs(pp);
2996 return err;
2997 }
2998 }
2999
3000 return 0;
3001}
3002
3003static void mvneta_start_dev(struct mvneta_port *pp)
3004{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003005 int cpu;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003006
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003007 mvneta_max_rx_size_set(pp, pp->pkt_size);
3008 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3009
3010 /* start the Rx/Tx activity */
3011 mvneta_port_enable(pp);
3012
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003013 if (!pp->neta_armada3700) {
3014 /* Enable polling on the port */
3015 for_each_online_cpu(cpu) {
3016 struct mvneta_pcpu_port *port =
3017 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003018
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003019 napi_enable(&port->napi);
3020 }
3021 } else {
3022 napi_enable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003023 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003024
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003025 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003026 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3027
Stas Sergeev898b29702015-04-01 20:32:49 +03003028 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3029 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3030 MVNETA_CAUSE_LINK_CHANGE |
3031 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003032
Russell King503f9aa92018-01-02 17:24:44 +00003033 phylink_start(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003034 netif_tx_start_all_queues(pp->dev);
3035}
3036
3037static void mvneta_stop_dev(struct mvneta_port *pp)
3038{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003039 unsigned int cpu;
3040
Russell King503f9aa92018-01-02 17:24:44 +00003041 phylink_stop(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003042
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003043 if (!pp->neta_armada3700) {
3044 for_each_online_cpu(cpu) {
3045 struct mvneta_pcpu_port *port =
3046 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003047
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003048 napi_disable(&port->napi);
3049 }
3050 } else {
3051 napi_disable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003052 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003053
3054 netif_carrier_off(pp->dev);
3055
3056 mvneta_port_down(pp);
3057 netif_tx_stop_all_queues(pp->dev);
3058
3059 /* Stop the port activity */
3060 mvneta_port_disable(pp);
3061
3062 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003063 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003064
3065 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003066 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003067
3068 mvneta_tx_reset(pp);
3069 mvneta_rx_reset(pp);
3070}
3071
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003072static void mvneta_percpu_enable(void *arg)
3073{
3074 struct mvneta_port *pp = arg;
3075
3076 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3077}
3078
3079static void mvneta_percpu_disable(void *arg)
3080{
3081 struct mvneta_port *pp = arg;
3082
3083 disable_percpu_irq(pp->dev->irq);
3084}
3085
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003086/* Change the device mtu */
3087static int mvneta_change_mtu(struct net_device *dev, int mtu)
3088{
3089 struct mvneta_port *pp = netdev_priv(dev);
3090 int ret;
3091
Jarod Wilson57779872016-10-17 15:54:06 -04003092 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3093 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3094 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3095 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3096 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003097
3098 dev->mtu = mtu;
3099
Simon Guinotb65657f2015-06-30 16:20:22 +02003100 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003101 if (pp->bm_priv)
3102 mvneta_bm_update_mtu(pp, mtu);
3103
Simon Guinotb65657f2015-06-30 16:20:22 +02003104 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003105 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003106 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003107
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003108 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003109 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003110 */
3111 mvneta_stop_dev(pp);
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003112 on_each_cpu(mvneta_percpu_disable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003113
3114 mvneta_cleanup_txqs(pp);
3115 mvneta_cleanup_rxqs(pp);
3116
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003117 if (pp->bm_priv)
3118 mvneta_bm_update_mtu(pp, mtu);
3119
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003120 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003121 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3122 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003123
3124 ret = mvneta_setup_rxqs(pp);
3125 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003126 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003127 return ret;
3128 }
3129
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003130 ret = mvneta_setup_txqs(pp);
3131 if (ret) {
3132 netdev_err(dev, "unable to setup txqs after MTU change\n");
3133 return ret;
3134 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003135
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003136 on_each_cpu(mvneta_percpu_enable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003137 mvneta_start_dev(pp);
3138 mvneta_port_up(pp);
3139
Simon Guinotb65657f2015-06-30 16:20:22 +02003140 netdev_update_features(dev);
3141
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003142 return 0;
3143}
3144
Simon Guinotb65657f2015-06-30 16:20:22 +02003145static netdev_features_t mvneta_fix_features(struct net_device *dev,
3146 netdev_features_t features)
3147{
3148 struct mvneta_port *pp = netdev_priv(dev);
3149
3150 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3151 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3152 netdev_info(dev,
3153 "Disable IP checksum for MTU greater than %dB\n",
3154 pp->tx_csum_limit);
3155 }
3156
3157 return features;
3158}
3159
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003160/* Get mac address */
3161static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3162{
3163 u32 mac_addr_l, mac_addr_h;
3164
3165 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3166 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3167 addr[0] = (mac_addr_h >> 24) & 0xFF;
3168 addr[1] = (mac_addr_h >> 16) & 0xFF;
3169 addr[2] = (mac_addr_h >> 8) & 0xFF;
3170 addr[3] = mac_addr_h & 0xFF;
3171 addr[4] = (mac_addr_l >> 8) & 0xFF;
3172 addr[5] = mac_addr_l & 0xFF;
3173}
3174
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003175/* Handle setting mac address */
3176static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3177{
3178 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003179 struct sockaddr *sockaddr = addr;
3180 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003181
Ezequiel Garciae68de362014-05-22 20:07:00 -03003182 ret = eth_prepare_mac_addr_change(dev, addr);
3183 if (ret < 0)
3184 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003185 /* Remove previous address table entry */
3186 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3187
3188 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003189 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003190
Ezequiel Garciae68de362014-05-22 20:07:00 -03003191 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003192 return 0;
3193}
3194
Russell King503f9aa92018-01-02 17:24:44 +00003195static void mvneta_validate(struct net_device *ndev, unsigned long *supported,
3196 struct phylink_link_state *state)
Russell Kingfc548b92018-01-02 17:24:39 +00003197{
Russell King503f9aa92018-01-02 17:24:44 +00003198 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
Russell Kingfc548b92018-01-02 17:24:39 +00003199
Russell King22f4bf82018-01-02 17:24:54 +00003200 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
Russell King503f9aa92018-01-02 17:24:44 +00003201 if (state->interface != PHY_INTERFACE_MODE_NA &&
3202 state->interface != PHY_INTERFACE_MODE_QSGMII &&
3203 state->interface != PHY_INTERFACE_MODE_SGMII &&
Russell King22f4bf82018-01-02 17:24:54 +00003204 !phy_interface_mode_is_8023z(state->interface) &&
Russell King503f9aa92018-01-02 17:24:44 +00003205 !phy_interface_mode_is_rgmii(state->interface)) {
3206 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3207 return;
Russell Kingfc548b92018-01-02 17:24:39 +00003208 }
Russell King503f9aa92018-01-02 17:24:44 +00003209
3210 /* Allow all the expected bits */
3211 phylink_set(mask, Autoneg);
3212 phylink_set_port_modes(mask);
3213
3214 /* Half-duplex at speeds higher than 100Mbit is unsupported */
3215 phylink_set(mask, 1000baseT_Full);
3216 phylink_set(mask, 1000baseX_Full);
Russell King22f4bf82018-01-02 17:24:54 +00003217
3218 if (!phy_interface_mode_is_8023z(state->interface)) {
3219 /* 10M and 100M are only supported in non-802.3z mode */
3220 phylink_set(mask, 10baseT_Half);
3221 phylink_set(mask, 10baseT_Full);
3222 phylink_set(mask, 100baseT_Half);
3223 phylink_set(mask, 100baseT_Full);
3224 }
Russell King503f9aa92018-01-02 17:24:44 +00003225
3226 bitmap_and(supported, supported, mask,
3227 __ETHTOOL_LINK_MODE_MASK_NBITS);
3228 bitmap_and(state->advertising, state->advertising, mask,
3229 __ETHTOOL_LINK_MODE_MASK_NBITS);
Russell Kingfc548b92018-01-02 17:24:39 +00003230}
3231
Russell King503f9aa92018-01-02 17:24:44 +00003232static int mvneta_mac_link_state(struct net_device *ndev,
3233 struct phylink_link_state *state)
3234{
3235 struct mvneta_port *pp = netdev_priv(ndev);
3236 u32 gmac_stat;
3237
3238 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3239
3240 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3241 state->speed = SPEED_1000;
3242 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3243 state->speed = SPEED_100;
3244 else
3245 state->speed = SPEED_10;
3246
3247 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3248 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3249 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3250
3251 state->pause = 0;
3252
3253 return 1;
3254}
3255
Russell King22f4bf82018-01-02 17:24:54 +00003256static void mvneta_mac_an_restart(struct net_device *ndev)
3257{
3258 struct mvneta_port *pp = netdev_priv(ndev);
3259 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3260
3261 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3262 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
3263 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3264 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
3265}
3266
Russell King503f9aa92018-01-02 17:24:44 +00003267static void mvneta_mac_config(struct net_device *ndev, unsigned int mode,
3268 const struct phylink_link_state *state)
3269{
3270 struct mvneta_port *pp = netdev_priv(ndev);
Russell King22f4bf82018-01-02 17:24:54 +00003271 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
Russell King503f9aa92018-01-02 17:24:44 +00003272 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3273 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3274 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3275
Russell King22f4bf82018-01-02 17:24:54 +00003276 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
Russell King32699952018-01-02 17:24:49 +00003277 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
3278 MVNETA_GMAC2_PORT_RESET);
Russell King503f9aa92018-01-02 17:24:44 +00003279 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3280 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3281 MVNETA_GMAC_INBAND_RESTART_AN |
3282 MVNETA_GMAC_CONFIG_MII_SPEED |
3283 MVNETA_GMAC_CONFIG_GMII_SPEED |
3284 MVNETA_GMAC_AN_SPEED_EN |
Russell King22f4bf82018-01-02 17:24:54 +00003285 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
3286 MVNETA_GMAC_CONFIG_FLOW_CTRL |
Russell King503f9aa92018-01-02 17:24:44 +00003287 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3288 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
3289 MVNETA_GMAC_AN_DUPLEX_EN);
3290
Russell King32699952018-01-02 17:24:49 +00003291 /* Even though it might look weird, when we're configured in
3292 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3293 */
3294 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
3295
3296 if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
Russell King22f4bf82018-01-02 17:24:54 +00003297 state->interface == PHY_INTERFACE_MODE_SGMII ||
3298 phy_interface_mode_is_8023z(state->interface))
Russell King32699952018-01-02 17:24:49 +00003299 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
3300
Russell King503f9aa92018-01-02 17:24:44 +00003301 if (!phylink_autoneg_inband(mode)) {
3302 /* Phy or fixed speed */
3303 if (state->duplex)
3304 new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3305
3306 if (state->speed == SPEED_1000)
3307 new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3308 else if (state->speed == SPEED_100)
3309 new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
Russell King22f4bf82018-01-02 17:24:54 +00003310 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Russell King503f9aa92018-01-02 17:24:44 +00003311 /* SGMII mode receives the state from the PHY */
3312 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3313 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3314 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3315 MVNETA_GMAC_FORCE_LINK_PASS)) |
3316 MVNETA_GMAC_INBAND_AN_ENABLE |
3317 MVNETA_GMAC_AN_SPEED_EN |
3318 MVNETA_GMAC_AN_DUPLEX_EN;
Russell King22f4bf82018-01-02 17:24:54 +00003319 } else {
3320 /* 802.3z negotiation - only 1000base-X */
3321 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
3322 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3323 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3324 MVNETA_GMAC_FORCE_LINK_PASS)) |
3325 MVNETA_GMAC_INBAND_AN_ENABLE |
3326 MVNETA_GMAC_CONFIG_GMII_SPEED |
3327 /* The MAC only supports FD mode */
3328 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
Russell King503f9aa92018-01-02 17:24:44 +00003329 }
3330
3331 /* Armada 370 documentation says we can only change the port mode
3332 * and in-band enable when the link is down, so force it down
3333 * while making these changes. We also do this for GMAC_CTRL2 */
Russell King22f4bf82018-01-02 17:24:54 +00003334 if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
3335 (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
Russell King503f9aa92018-01-02 17:24:44 +00003336 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3337 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3338 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3339 MVNETA_GMAC_FORCE_LINK_DOWN);
3340 }
3341
Russell King22f4bf82018-01-02 17:24:54 +00003342 if (new_ctrl0 != gmac_ctrl0)
3343 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
Russell King503f9aa92018-01-02 17:24:44 +00003344 if (new_ctrl2 != gmac_ctrl2)
3345 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
3346 if (new_clk != gmac_clk)
3347 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3348 if (new_an != gmac_an)
3349 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
Russell King32699952018-01-02 17:24:49 +00003350
3351 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
3352 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3353 MVNETA_GMAC2_PORT_RESET) != 0)
3354 continue;
3355 }
Russell King503f9aa92018-01-02 17:24:44 +00003356}
3357
3358static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode)
Russell Kingfc548b92018-01-02 17:24:39 +00003359{
3360 struct mvneta_port *pp = netdev_priv(ndev);
3361 u32 val;
3362
Russell King503f9aa92018-01-02 17:24:44 +00003363 mvneta_port_down(pp);
3364
3365 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003366 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3367 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3368 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3369 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3370 }
Russell Kingfc548b92018-01-02 17:24:39 +00003371}
3372
Russell King503f9aa92018-01-02 17:24:44 +00003373static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode,
3374 struct phy_device *phy)
Russell Kingfc548b92018-01-02 17:24:39 +00003375{
3376 struct mvneta_port *pp = netdev_priv(ndev);
3377 u32 val;
3378
Russell King503f9aa92018-01-02 17:24:44 +00003379 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003380 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3381 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3382 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3383 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3384 }
3385
3386 mvneta_port_up(pp);
3387}
3388
Russell King503f9aa92018-01-02 17:24:44 +00003389static const struct phylink_mac_ops mvneta_phylink_ops = {
3390 .validate = mvneta_validate,
3391 .mac_link_state = mvneta_mac_link_state,
Russell King22f4bf82018-01-02 17:24:54 +00003392 .mac_an_restart = mvneta_mac_an_restart,
Russell King503f9aa92018-01-02 17:24:44 +00003393 .mac_config = mvneta_mac_config,
3394 .mac_link_down = mvneta_mac_link_down,
3395 .mac_link_up = mvneta_mac_link_up,
3396};
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003397
3398static int mvneta_mdio_probe(struct mvneta_port *pp)
3399{
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003400 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Russell King503f9aa92018-01-02 17:24:44 +00003401 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003402
Russell King503f9aa92018-01-02 17:24:44 +00003403 if (err)
3404 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003405
Russell King503f9aa92018-01-02 17:24:44 +00003406 phylink_ethtool_get_wol(pp->phylink, &wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003407 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
3408
Russell King503f9aa92018-01-02 17:24:44 +00003409 return err;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003410}
3411
3412static void mvneta_mdio_remove(struct mvneta_port *pp)
3413{
Russell King503f9aa92018-01-02 17:24:44 +00003414 phylink_disconnect_phy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003415}
3416
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003417/* Electing a CPU must be done in an atomic way: it should be done
3418 * after or before the removal/insertion of a CPU and this function is
3419 * not reentrant.
3420 */
Maxime Ripardf8642882015-09-25 18:09:38 +02003421static void mvneta_percpu_elect(struct mvneta_port *pp)
3422{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003423 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003424
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003425 /* Use the cpu associated to the rxq when it is online, in all
3426 * the other cases, use the cpu 0 which can't be offline.
3427 */
3428 if (cpu_online(pp->rxq_def))
3429 elected_cpu = pp->rxq_def;
3430
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003431 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02003432
3433 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003434 int rxq_map = 0, txq_map = 0;
3435 int rxq;
3436
3437 for (rxq = 0; rxq < rxq_number; rxq++)
3438 if ((rxq % max_cpu) == cpu)
3439 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3440
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003441 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003442 /* Map the default receive queue queue to the
3443 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02003444 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003445 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003446
3447 /* We update the TX queue map only if we have one
3448 * queue. In this case we associate the TX queue to
3449 * the CPU bound to the default RX queue
3450 */
3451 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003452 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003453 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3454 else
3455 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3456 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3457
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003458 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3459
3460 /* Update the interrupt mask on each CPU according the
3461 * new mapping
3462 */
3463 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3464 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003465 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003466
Maxime Ripardf8642882015-09-25 18:09:38 +02003467 }
3468};
3469
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003470static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
Maxime Ripardf8642882015-09-25 18:09:38 +02003471{
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003472 int other_cpu;
3473 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3474 node_online);
Maxime Ripardf8642882015-09-25 18:09:38 +02003475 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3476
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003477
3478 spin_lock(&pp->lock);
3479 /*
3480 * Configuring the driver for a new CPU while the driver is
3481 * stopping is racy, so just avoid it.
3482 */
3483 if (pp->is_stopped) {
3484 spin_unlock(&pp->lock);
3485 return 0;
3486 }
3487 netif_tx_stop_all_queues(pp->dev);
3488
3489 /*
3490 * We have to synchronise on tha napi of each CPU except the one
3491 * just being woken up
3492 */
3493 for_each_online_cpu(other_cpu) {
3494 if (other_cpu != cpu) {
3495 struct mvneta_pcpu_port *other_port =
3496 per_cpu_ptr(pp->ports, other_cpu);
3497
3498 napi_synchronize(&other_port->napi);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003499 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003500 }
3501
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003502 /* Mask all ethernet port interrupts */
3503 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3504 napi_enable(&port->napi);
3505
3506 /*
3507 * Enable per-CPU interrupts on the CPU that is
3508 * brought up.
3509 */
3510 mvneta_percpu_enable(pp);
3511
3512 /*
3513 * Enable per-CPU interrupt on the one CPU we care
3514 * about.
3515 */
3516 mvneta_percpu_elect(pp);
3517
3518 /* Unmask all ethernet port interrupts */
3519 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3520 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3521 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3522 MVNETA_CAUSE_LINK_CHANGE |
3523 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3524 netif_tx_start_all_queues(pp->dev);
3525 spin_unlock(&pp->lock);
3526 return 0;
3527}
3528
3529static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3530{
3531 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3532 node_online);
3533 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3534
3535 /*
3536 * Thanks to this lock we are sure that any pending cpu election is
3537 * done.
3538 */
3539 spin_lock(&pp->lock);
3540 /* Mask all ethernet port interrupts */
3541 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3542 spin_unlock(&pp->lock);
3543
3544 napi_synchronize(&port->napi);
3545 napi_disable(&port->napi);
3546 /* Disable per-CPU interrupts on the CPU that is brought down. */
3547 mvneta_percpu_disable(pp);
3548 return 0;
3549}
3550
3551static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3552{
3553 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3554 node_dead);
3555
3556 /* Check if a new CPU must be elected now this on is down */
3557 spin_lock(&pp->lock);
3558 mvneta_percpu_elect(pp);
3559 spin_unlock(&pp->lock);
3560 /* Unmask all ethernet port interrupts */
3561 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3562 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3563 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3564 MVNETA_CAUSE_LINK_CHANGE |
3565 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3566 netif_tx_start_all_queues(pp->dev);
3567 return 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003568}
3569
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003570static int mvneta_open(struct net_device *dev)
3571{
3572 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003573 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003574
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003575 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003576 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3577 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003578
3579 ret = mvneta_setup_rxqs(pp);
3580 if (ret)
3581 return ret;
3582
3583 ret = mvneta_setup_txqs(pp);
3584 if (ret)
3585 goto err_cleanup_rxqs;
3586
3587 /* Connect to port interrupt line */
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003588 if (pp->neta_armada3700)
3589 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3590 dev->name, pp);
3591 else
3592 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3593 dev->name, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003594 if (ret) {
3595 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3596 goto err_cleanup_txqs;
3597 }
3598
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003599 if (!pp->neta_armada3700) {
3600 /* Enable per-CPU interrupt on all the CPU to handle our RX
3601 * queue interrupts
3602 */
3603 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003604
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003605 pp->is_stopped = false;
3606 /* Register a CPU notifier to handle the case where our CPU
3607 * might be taken offline.
3608 */
3609 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3610 &pp->node_online);
3611 if (ret)
3612 goto err_free_irq;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003613
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003614 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3615 &pp->node_dead);
3616 if (ret)
3617 goto err_free_online_hp;
3618 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003619
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003620 /* In default link is down */
3621 netif_carrier_off(pp->dev);
3622
3623 ret = mvneta_mdio_probe(pp);
3624 if (ret < 0) {
3625 netdev_err(dev, "cannot probe MDIO bus\n");
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003626 goto err_free_dead_hp;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003627 }
3628
3629 mvneta_start_dev(pp);
3630
3631 return 0;
3632
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003633err_free_dead_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003634 if (!pp->neta_armada3700)
3635 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3636 &pp->node_dead);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003637err_free_online_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003638 if (!pp->neta_armada3700)
3639 cpuhp_state_remove_instance_nocalls(online_hpstate,
3640 &pp->node_online);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003641err_free_irq:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003642 if (pp->neta_armada3700) {
3643 free_irq(pp->dev->irq, pp);
3644 } else {
3645 on_each_cpu(mvneta_percpu_disable, pp, true);
3646 free_percpu_irq(pp->dev->irq, pp->ports);
3647 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003648err_cleanup_txqs:
3649 mvneta_cleanup_txqs(pp);
3650err_cleanup_rxqs:
3651 mvneta_cleanup_rxqs(pp);
3652 return ret;
3653}
3654
3655/* Stop the port, free port interrupt line */
3656static int mvneta_stop(struct net_device *dev)
3657{
3658 struct mvneta_port *pp = netdev_priv(dev);
3659
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003660 if (!pp->neta_armada3700) {
3661 /* Inform that we are stopping so we don't want to setup the
3662 * driver for new CPUs in the notifiers. The code of the
3663 * notifier for CPU online is protected by the same spinlock,
3664 * so when we get the lock, the notifer work is done.
3665 */
3666 spin_lock(&pp->lock);
3667 pp->is_stopped = true;
3668 spin_unlock(&pp->lock);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003669
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003670 mvneta_stop_dev(pp);
3671 mvneta_mdio_remove(pp);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003672
Dan Carpenterd26aac22016-12-07 14:32:17 +03003673 cpuhp_state_remove_instance_nocalls(online_hpstate,
3674 &pp->node_online);
3675 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3676 &pp->node_dead);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003677 on_each_cpu(mvneta_percpu_disable, pp, true);
3678 free_percpu_irq(dev->irq, pp->ports);
3679 } else {
3680 mvneta_stop_dev(pp);
3681 mvneta_mdio_remove(pp);
3682 free_irq(dev->irq, pp);
3683 }
3684
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003685 mvneta_cleanup_rxqs(pp);
3686 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003687
3688 return 0;
3689}
3690
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003691static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3692{
Russell King503f9aa92018-01-02 17:24:44 +00003693 struct mvneta_port *pp = netdev_priv(dev);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003694
Russell King503f9aa92018-01-02 17:24:44 +00003695 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003696}
3697
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003698/* Ethtool methods */
3699
Philippe Reynes013ad402016-07-30 17:42:12 +02003700/* Set link ksettings (phy address, speed) for ethtools */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +08003701static int
3702mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3703 const struct ethtool_link_ksettings *cmd)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003704{
Philippe Reynes013ad402016-07-30 17:42:12 +02003705 struct mvneta_port *pp = netdev_priv(ndev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003706
Russell King503f9aa92018-01-02 17:24:44 +00003707 return phylink_ethtool_ksettings_set(pp->phylink, cmd);
3708}
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003709
Russell King503f9aa92018-01-02 17:24:44 +00003710/* Get link ksettings for ethtools */
3711static int
3712mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
3713 struct ethtool_link_ksettings *cmd)
3714{
3715 struct mvneta_port *pp = netdev_priv(ndev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003716
Russell King503f9aa92018-01-02 17:24:44 +00003717 return phylink_ethtool_ksettings_get(pp->phylink, cmd);
3718}
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003719
Russell King503f9aa92018-01-02 17:24:44 +00003720static int mvneta_ethtool_nway_reset(struct net_device *dev)
3721{
3722 struct mvneta_port *pp = netdev_priv(dev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003723
Russell King503f9aa92018-01-02 17:24:44 +00003724 return phylink_ethtool_nway_reset(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003725}
3726
3727/* Set interrupt coalescing for ethtools */
3728static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3729 struct ethtool_coalesce *c)
3730{
3731 struct mvneta_port *pp = netdev_priv(dev);
3732 int queue;
3733
3734 for (queue = 0; queue < rxq_number; queue++) {
3735 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3736 rxq->time_coal = c->rx_coalesce_usecs;
3737 rxq->pkts_coal = c->rx_max_coalesced_frames;
3738 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3739 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3740 }
3741
3742 for (queue = 0; queue < txq_number; queue++) {
3743 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3744 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3745 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3746 }
3747
3748 return 0;
3749}
3750
3751/* get coalescing for ethtools */
3752static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3753 struct ethtool_coalesce *c)
3754{
3755 struct mvneta_port *pp = netdev_priv(dev);
3756
3757 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3758 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3759
3760 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3761 return 0;
3762}
3763
3764
3765static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3766 struct ethtool_drvinfo *drvinfo)
3767{
3768 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3769 sizeof(drvinfo->driver));
3770 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3771 sizeof(drvinfo->version));
3772 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3773 sizeof(drvinfo->bus_info));
3774}
3775
3776
3777static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3778 struct ethtool_ringparam *ring)
3779{
3780 struct mvneta_port *pp = netdev_priv(netdev);
3781
3782 ring->rx_max_pending = MVNETA_MAX_RXD;
3783 ring->tx_max_pending = MVNETA_MAX_TXD;
3784 ring->rx_pending = pp->rx_ring_size;
3785 ring->tx_pending = pp->tx_ring_size;
3786}
3787
3788static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3789 struct ethtool_ringparam *ring)
3790{
3791 struct mvneta_port *pp = netdev_priv(dev);
3792
3793 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3794 return -EINVAL;
3795 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3796 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003797
3798 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3799 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3800 if (pp->tx_ring_size != ring->tx_pending)
3801 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3802 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003803
3804 if (netif_running(dev)) {
3805 mvneta_stop(dev);
3806 if (mvneta_open(dev)) {
3807 netdev_err(dev,
3808 "error on opening device after ring param change\n");
3809 return -ENOMEM;
3810 }
3811 }
3812
3813 return 0;
3814}
3815
Russell King9b0cdef2015-10-22 18:37:30 +01003816static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3817 u8 *data)
3818{
3819 if (sset == ETH_SS_STATS) {
3820 int i;
3821
3822 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3823 memcpy(data + i * ETH_GSTRING_LEN,
3824 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3825 }
3826}
3827
3828static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3829{
3830 const struct mvneta_statistic *s;
3831 void __iomem *base = pp->base;
3832 u32 high, low, val;
Jisheng Zhang2c832292016-01-20 16:36:25 +08003833 u64 val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003834 int i;
3835
3836 for (i = 0, s = mvneta_statistics;
3837 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3838 s++, i++) {
Russell King9b0cdef2015-10-22 18:37:30 +01003839 switch (s->type) {
3840 case T_REG_32:
3841 val = readl_relaxed(base + s->offset);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003842 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003843 break;
3844 case T_REG_64:
3845 /* Docs say to read low 32-bit then high */
3846 low = readl_relaxed(base + s->offset);
3847 high = readl_relaxed(base + s->offset + 4);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003848 val64 = (u64)high << 32 | low;
3849 pp->ethtool_stats[i] += val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003850 break;
3851 }
Russell King9b0cdef2015-10-22 18:37:30 +01003852 }
3853}
3854
3855static void mvneta_ethtool_get_stats(struct net_device *dev,
3856 struct ethtool_stats *stats, u64 *data)
3857{
3858 struct mvneta_port *pp = netdev_priv(dev);
3859 int i;
3860
3861 mvneta_ethtool_update_stats(pp);
3862
3863 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3864 *data++ = pp->ethtool_stats[i];
3865}
3866
3867static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3868{
3869 if (sset == ETH_SS_STATS)
3870 return ARRAY_SIZE(mvneta_statistics);
3871 return -EOPNOTSUPP;
3872}
3873
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003874static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3875{
3876 return MVNETA_RSS_LU_TABLE_SIZE;
3877}
3878
3879static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3880 struct ethtool_rxnfc *info,
3881 u32 *rules __always_unused)
3882{
3883 switch (info->cmd) {
3884 case ETHTOOL_GRXRINGS:
3885 info->data = rxq_number;
3886 return 0;
3887 case ETHTOOL_GRXFH:
3888 return -EOPNOTSUPP;
3889 default:
3890 return -EOPNOTSUPP;
3891 }
3892}
3893
3894static int mvneta_config_rss(struct mvneta_port *pp)
3895{
3896 int cpu;
3897 u32 val;
3898
3899 netif_tx_stop_all_queues(pp->dev);
3900
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003901 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003902
3903 /* We have to synchronise on the napi of each CPU */
3904 for_each_online_cpu(cpu) {
3905 struct mvneta_pcpu_port *pcpu_port =
3906 per_cpu_ptr(pp->ports, cpu);
3907
3908 napi_synchronize(&pcpu_port->napi);
3909 napi_disable(&pcpu_port->napi);
3910 }
3911
3912 pp->rxq_def = pp->indir[0];
3913
3914 /* Update unicast mapping */
3915 mvneta_set_rx_mode(pp->dev);
3916
3917 /* Update val of portCfg register accordingly with all RxQueue types */
3918 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3919 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3920
3921 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003922 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003923 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003924 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003925
3926 /* We have to synchronise on the napi of each CPU */
3927 for_each_online_cpu(cpu) {
3928 struct mvneta_pcpu_port *pcpu_port =
3929 per_cpu_ptr(pp->ports, cpu);
3930
3931 napi_enable(&pcpu_port->napi);
3932 }
3933
3934 netif_tx_start_all_queues(pp->dev);
3935
3936 return 0;
3937}
3938
3939static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3940 const u8 *key, const u8 hfunc)
3941{
3942 struct mvneta_port *pp = netdev_priv(dev);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003943
3944 /* Current code for Armada 3700 doesn't support RSS features yet */
3945 if (pp->neta_armada3700)
3946 return -EOPNOTSUPP;
3947
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003948 /* We require at least one supported parameter to be changed
3949 * and no change in any of the unsupported parameters
3950 */
3951 if (key ||
3952 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3953 return -EOPNOTSUPP;
3954
3955 if (!indir)
3956 return 0;
3957
3958 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3959
3960 return mvneta_config_rss(pp);
3961}
3962
3963static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3964 u8 *hfunc)
3965{
3966 struct mvneta_port *pp = netdev_priv(dev);
3967
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003968 /* Current code for Armada 3700 doesn't support RSS features yet */
3969 if (pp->neta_armada3700)
3970 return -EOPNOTSUPP;
3971
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003972 if (hfunc)
3973 *hfunc = ETH_RSS_HASH_TOP;
3974
3975 if (!indir)
3976 return 0;
3977
3978 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3979
3980 return 0;
3981}
3982
Jingju Houb60a00f2017-02-06 14:58:13 +08003983static void mvneta_ethtool_get_wol(struct net_device *dev,
3984 struct ethtool_wolinfo *wol)
3985{
Russell King503f9aa92018-01-02 17:24:44 +00003986 struct mvneta_port *pp = netdev_priv(dev);
Jingju Houb60a00f2017-02-06 14:58:13 +08003987
Russell King503f9aa92018-01-02 17:24:44 +00003988 phylink_ethtool_get_wol(pp->phylink, wol);
Jingju Houb60a00f2017-02-06 14:58:13 +08003989}
3990
3991static int mvneta_ethtool_set_wol(struct net_device *dev,
3992 struct ethtool_wolinfo *wol)
3993{
Russell King503f9aa92018-01-02 17:24:44 +00003994 struct mvneta_port *pp = netdev_priv(dev);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003995 int ret;
3996
Russell King503f9aa92018-01-02 17:24:44 +00003997 ret = phylink_ethtool_set_wol(pp->phylink, wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003998 if (!ret)
3999 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
4000
4001 return ret;
Jingju Houb60a00f2017-02-06 14:58:13 +08004002}
4003
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004004static const struct net_device_ops mvneta_netdev_ops = {
4005 .ndo_open = mvneta_open,
4006 .ndo_stop = mvneta_stop,
4007 .ndo_start_xmit = mvneta_tx,
4008 .ndo_set_rx_mode = mvneta_set_rx_mode,
4009 .ndo_set_mac_address = mvneta_set_mac_addr,
4010 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02004011 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004012 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02004013 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004014};
4015
Jisheng Zhang4581be42017-02-16 17:07:39 +08004016static const struct ethtool_ops mvneta_eth_tool_ops = {
Russell King503f9aa92018-01-02 17:24:44 +00004017 .nway_reset = mvneta_ethtool_nway_reset,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004018 .get_link = ethtool_op_get_link,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004019 .set_coalesce = mvneta_ethtool_set_coalesce,
4020 .get_coalesce = mvneta_ethtool_get_coalesce,
4021 .get_drvinfo = mvneta_ethtool_get_drvinfo,
4022 .get_ringparam = mvneta_ethtool_get_ringparam,
4023 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King9b0cdef2015-10-22 18:37:30 +01004024 .get_strings = mvneta_ethtool_get_strings,
4025 .get_ethtool_stats = mvneta_ethtool_get_stats,
4026 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004027 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
4028 .get_rxnfc = mvneta_ethtool_get_rxnfc,
4029 .get_rxfh = mvneta_ethtool_get_rxfh,
4030 .set_rxfh = mvneta_ethtool_set_rxfh,
Russell King503f9aa92018-01-02 17:24:44 +00004031 .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
Philippe Reynes013ad402016-07-30 17:42:12 +02004032 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
Jingju Houb60a00f2017-02-06 14:58:13 +08004033 .get_wol = mvneta_ethtool_get_wol,
4034 .set_wol = mvneta_ethtool_set_wol,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004035};
4036
4037/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03004038static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004039{
4040 int queue;
4041
4042 /* Disable port */
4043 mvneta_port_disable(pp);
4044
4045 /* Set port default values */
4046 mvneta_defaults_set(pp);
4047
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004048 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004049 if (!pp->txqs)
4050 return -ENOMEM;
4051
4052 /* Initialize TX descriptor rings */
4053 for (queue = 0; queue < txq_number; queue++) {
4054 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4055 txq->id = queue;
4056 txq->size = pp->tx_ring_size;
4057 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4058 }
4059
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004060 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
Ezequiel Garcia96728502014-05-22 20:06:59 -03004061 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004062 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004063
4064 /* Create Rx descriptor rings */
4065 for (queue = 0; queue < rxq_number; queue++) {
4066 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4067 rxq->id = queue;
4068 rxq->size = pp->rx_ring_size;
4069 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4070 rxq->time_coal = MVNETA_RX_COAL_USEC;
Markus Elfring29110632017-04-16 21:23:19 +02004071 rxq->buf_virt_addr
4072 = devm_kmalloc_array(pp->dev->dev.parent,
4073 rxq->size,
4074 sizeof(*rxq->buf_virt_addr),
4075 GFP_KERNEL);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01004076 if (!rxq->buf_virt_addr)
4077 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004078 }
4079
4080 return 0;
4081}
4082
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004083/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00004084static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4085 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004086{
4087 u32 win_enable;
4088 u32 win_protect;
4089 int i;
4090
4091 for (i = 0; i < 6; i++) {
4092 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4093 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4094
4095 if (i < 4)
4096 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4097 }
4098
4099 win_enable = 0x3f;
4100 win_protect = 0;
4101
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004102 if (dram) {
4103 for (i = 0; i < dram->num_cs; i++) {
4104 const struct mbus_dram_window *cs = dram->cs + i;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004105
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004106 mvreg_write(pp, MVNETA_WIN_BASE(i),
4107 (cs->base & 0xffff0000) |
4108 (cs->mbus_attr << 8) |
4109 dram->mbus_dram_target_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004110
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004111 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4112 (cs->size - 1) & 0xffff0000);
4113
4114 win_enable &= ~(1 << i);
4115 win_protect |= 3 << (2 * i);
4116 }
4117 } else {
4118 /* For Armada3700 open default 4GB Mbus window, leaving
4119 * arbitration of target/attribute to a different layer
4120 * of configuration.
4121 */
4122 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4123 win_enable &= ~BIT(0);
4124 win_protect = 3;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004125 }
4126
4127 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01004128 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004129}
4130
4131/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004132static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004133{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004134 /* MAC Cause register should be cleared */
4135 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4136
Russell King32699952018-01-02 17:24:49 +00004137 if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004138 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
Russell King22f4bf82018-01-02 17:24:54 +00004139 else if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
4140 phy_mode == PHY_INTERFACE_MODE_1000BASEX)
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004141 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
Russell King32699952018-01-02 17:24:49 +00004142 else if (!phy_interface_mode_is_rgmii(phy_mode))
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004143 return -EINVAL;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004144
4145 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004146}
4147
4148/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00004149static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004150{
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004151 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004152 struct device_node *dn = pdev->dev.of_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004153 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004154 struct mvneta_port *pp;
4155 struct net_device *dev;
Russell King503f9aa92018-01-02 17:24:44 +00004156 struct phylink *phylink;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004157 const char *dt_mac_addr;
4158 char hw_mac_addr[ETH_ALEN];
4159 const char *mac_from;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004160 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004161 int phy_mode;
4162 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004163 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004164
Willy Tarreauee40a112013-04-11 23:00:37 +02004165 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004166 if (!dev)
4167 return -ENOMEM;
4168
4169 dev->irq = irq_of_parse_and_map(dn, 0);
4170 if (dev->irq == 0) {
4171 err = -EINVAL;
4172 goto err_free_netdev;
4173 }
4174
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004175 phy_mode = of_get_phy_mode(dn);
4176 if (phy_mode < 0) {
4177 dev_err(&pdev->dev, "incorrect phy-mode\n");
4178 err = -EINVAL;
Russell King503f9aa92018-01-02 17:24:44 +00004179 goto err_free_irq;
4180 }
4181
4182 phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode,
4183 &mvneta_phylink_ops);
4184 if (IS_ERR(phylink)) {
4185 err = PTR_ERR(phylink);
4186 goto err_free_irq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004187 }
4188
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004189 dev->tx_queue_len = MVNETA_MAX_TXD;
4190 dev->watchdog_timeo = 5 * HZ;
4191 dev->netdev_ops = &mvneta_netdev_ops;
4192
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004193 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004194
4195 pp = netdev_priv(dev);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01004196 spin_lock_init(&pp->lock);
Russell King503f9aa92018-01-02 17:24:44 +00004197 pp->phylink = phylink;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004198 pp->phy_interface = phy_mode;
Russell King503f9aa92018-01-02 17:24:44 +00004199 pp->dn = dn;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004200
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01004201 pp->rxq_def = rxq_def;
4202
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01004203 /* Set RX packet offset correction for platforms, whose
4204 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4205 * platforms and 0B for 32-bit ones.
4206 */
4207 pp->rx_offset_correction =
4208 max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
4209
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004210 pp->indir[0] = rxq_def;
4211
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004212 /* Get special SoC configurations */
4213 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4214 pp->neta_armada3700 = true;
4215
Jisheng Zhang2804ba42016-01-20 19:27:23 +08004216 pp->clk = devm_clk_get(&pdev->dev, "core");
4217 if (IS_ERR(pp->clk))
4218 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004219 if (IS_ERR(pp->clk)) {
4220 err = PTR_ERR(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00004221 goto err_free_phylink;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004222 }
4223
4224 clk_prepare_enable(pp->clk);
4225
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004226 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4227 if (!IS_ERR(pp->clk_bus))
4228 clk_prepare_enable(pp->clk_bus);
4229
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004230 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4231 pp->base = devm_ioremap_resource(&pdev->dev, res);
4232 if (IS_ERR(pp->base)) {
4233 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004234 goto err_clk;
4235 }
4236
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004237 /* Alloc per-cpu port structure */
4238 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4239 if (!pp->ports) {
4240 err = -ENOMEM;
4241 goto err_clk;
4242 }
4243
willy tarreau74c41b02014-01-16 08:20:08 +01004244 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08004245 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01004246 if (!pp->stats) {
4247 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004248 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01004249 }
4250
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004251 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01004252 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004253 mac_from = "device tree";
4254 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4255 } else {
4256 mvneta_get_mac_addr(pp, hw_mac_addr);
4257 if (is_valid_ether_addr(hw_mac_addr)) {
4258 mac_from = "hardware";
4259 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4260 } else {
4261 mac_from = "random";
4262 eth_hw_addr_random(dev);
4263 }
4264 }
4265
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004266 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4267 if (tx_csum_limit < 0 ||
4268 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4269 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4270 dev_info(&pdev->dev,
4271 "Wrong TX csum limit in DT, set to %dB\n",
4272 MVNETA_TX_CSUM_DEF_SIZE);
4273 }
4274 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4275 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4276 } else {
4277 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4278 }
4279
4280 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02004281
Jane Li9768b452017-03-16 16:22:28 +08004282 pp->dram_target_info = mv_mbus_dram_info();
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004283 /* Armada3700 requires setting default configuration of Mbus
4284 * windows, however without using filled mbus_dram_target_info
4285 * structure.
4286 */
Jane Li9768b452017-03-16 16:22:28 +08004287 if (pp->dram_target_info || pp->neta_armada3700)
4288 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004289
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004290 pp->tx_ring_size = MVNETA_MAX_TXD;
4291 pp->rx_ring_size = MVNETA_MAX_RXD;
4292
4293 pp->dev = dev;
4294 SET_NETDEV_DEV(dev, &pdev->dev);
4295
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004296 pp->id = global_port_id++;
4297
4298 /* Obtain access to BM resources if enabled and already initialized */
4299 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4300 if (bm_node && bm_node->data) {
4301 pp->bm_priv = bm_node->data;
4302 err = mvneta_bm_port_init(pdev, pp);
4303 if (err < 0) {
4304 dev_info(&pdev->dev, "use SW buffer management\n");
4305 pp->bm_priv = NULL;
4306 }
4307 }
Peter Chend4e4da02016-08-01 15:02:36 +08004308 of_node_put(bm_node);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004309
Ezequiel Garcia96728502014-05-22 20:06:59 -03004310 err = mvneta_init(&pdev->dev, pp);
4311 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004312 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004313
4314 err = mvneta_port_power_up(pp, phy_mode);
4315 if (err < 0) {
4316 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004317 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004318 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004319
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004320 /* Armada3700 network controller does not support per-cpu
4321 * operation, so only single NAPI should be initialized.
4322 */
4323 if (pp->neta_armada3700) {
4324 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4325 } else {
4326 for_each_present_cpu(cpu) {
4327 struct mvneta_pcpu_port *port =
4328 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004329
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004330 netif_napi_add(dev, &port->napi, mvneta_poll,
4331 NAPI_POLL_WEIGHT);
4332 port->pp = pp;
4333 }
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004334 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004335
Andrew Pilloudc54a5042017-09-01 07:49:49 -07004336 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03004337 dev->hw_features |= dev->features;
4338 dev->vlan_features |= dev->features;
Andrew Lunn97db8af2016-11-24 00:08:13 +01004339 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004340 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00004341
Jarod Wilson57779872016-10-17 15:54:06 -04004342 /* MTU range: 68 - 9676 */
4343 dev->min_mtu = ETH_MIN_MTU;
4344 /* 9676 == 9700 - 20 and rounding to 8 */
4345 dev->max_mtu = 9676;
4346
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004347 err = register_netdev(dev);
4348 if (err < 0) {
4349 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03004350 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004351 }
4352
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004353 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4354 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004355
4356 platform_set_drvdata(pdev, pp->dev);
4357
4358 return 0;
4359
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004360err_netdev:
4361 unregister_netdev(dev);
4362 if (pp->bm_priv) {
4363 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4364 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4365 1 << pp->id);
4366 }
willy tarreau74c41b02014-01-16 08:20:08 +01004367err_free_stats:
4368 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004369err_free_ports:
4370 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004371err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004372 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004373 clk_disable_unprepare(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00004374err_free_phylink:
4375 if (pp->phylink)
4376 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004377err_free_irq:
4378 irq_dispose_mapping(dev->irq);
4379err_free_netdev:
4380 free_netdev(dev);
4381 return err;
4382}
4383
4384/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00004385static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004386{
4387 struct net_device *dev = platform_get_drvdata(pdev);
4388 struct mvneta_port *pp = netdev_priv(dev);
4389
4390 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004391 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004392 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004393 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01004394 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004395 irq_dispose_mapping(dev->irq);
Russell King503f9aa92018-01-02 17:24:44 +00004396 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004397 free_netdev(dev);
4398
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004399 if (pp->bm_priv) {
4400 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4401 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4402 1 << pp->id);
4403 }
4404
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004405 return 0;
4406}
4407
Jane Li9768b452017-03-16 16:22:28 +08004408#ifdef CONFIG_PM_SLEEP
4409static int mvneta_suspend(struct device *device)
4410{
4411 struct net_device *dev = dev_get_drvdata(device);
4412 struct mvneta_port *pp = netdev_priv(dev);
4413
Russell King3b8bc672018-01-02 17:24:34 +00004414 rtnl_lock();
Jane Li9768b452017-03-16 16:22:28 +08004415 if (netif_running(dev))
4416 mvneta_stop(dev);
Russell King3b8bc672018-01-02 17:24:34 +00004417 rtnl_unlock();
Jane Li9768b452017-03-16 16:22:28 +08004418 netif_device_detach(dev);
4419 clk_disable_unprepare(pp->clk_bus);
4420 clk_disable_unprepare(pp->clk);
4421 return 0;
4422}
4423
4424static int mvneta_resume(struct device *device)
4425{
4426 struct platform_device *pdev = to_platform_device(device);
4427 struct net_device *dev = dev_get_drvdata(device);
4428 struct mvneta_port *pp = netdev_priv(dev);
4429 int err;
4430
4431 clk_prepare_enable(pp->clk);
4432 if (!IS_ERR(pp->clk_bus))
4433 clk_prepare_enable(pp->clk_bus);
4434 if (pp->dram_target_info || pp->neta_armada3700)
4435 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4436 if (pp->bm_priv) {
4437 err = mvneta_bm_port_init(pdev, pp);
4438 if (err < 0) {
4439 dev_info(&pdev->dev, "use SW buffer management\n");
4440 pp->bm_priv = NULL;
4441 }
4442 }
4443 mvneta_defaults_set(pp);
4444 err = mvneta_port_power_up(pp, pp->phy_interface);
4445 if (err < 0) {
4446 dev_err(device, "can't power up port\n");
4447 return err;
4448 }
4449
Jane Li9768b452017-03-16 16:22:28 +08004450 netif_device_attach(dev);
Russell King3b8bc672018-01-02 17:24:34 +00004451 rtnl_lock();
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004452 if (netif_running(dev)) {
Jane Li9768b452017-03-16 16:22:28 +08004453 mvneta_open(dev);
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004454 mvneta_set_rx_mode(dev);
4455 }
Russell King3b8bc672018-01-02 17:24:34 +00004456 rtnl_unlock();
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004457
Jane Li9768b452017-03-16 16:22:28 +08004458 return 0;
4459}
4460#endif
4461
4462static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
4463
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004464static const struct of_device_id mvneta_match[] = {
4465 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02004466 { .compatible = "marvell,armada-xp-neta" },
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004467 { .compatible = "marvell,armada-3700-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004468 { }
4469};
4470MODULE_DEVICE_TABLE(of, mvneta_match);
4471
4472static struct platform_driver mvneta_driver = {
4473 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00004474 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004475 .driver = {
4476 .name = MVNETA_DRIVER_NAME,
4477 .of_match_table = mvneta_match,
Jane Li9768b452017-03-16 16:22:28 +08004478 .pm = &mvneta_pm_ops,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004479 },
4480};
4481
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004482static int __init mvneta_driver_init(void)
4483{
4484 int ret;
4485
4486 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4487 mvneta_cpu_online,
4488 mvneta_cpu_down_prepare);
4489 if (ret < 0)
4490 goto out;
4491 online_hpstate = ret;
4492 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4493 NULL, mvneta_cpu_dead);
4494 if (ret)
4495 goto err_dead;
4496
4497 ret = platform_driver_register(&mvneta_driver);
4498 if (ret)
4499 goto err;
4500 return 0;
4501
4502err:
4503 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4504err_dead:
4505 cpuhp_remove_multi_state(online_hpstate);
4506out:
4507 return ret;
4508}
4509module_init(mvneta_driver_init);
4510
4511static void __exit mvneta_driver_exit(void)
4512{
4513 platform_driver_unregister(&mvneta_driver);
4514 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4515 cpuhp_remove_multi_state(online_hpstate);
4516}
4517module_exit(mvneta_driver_exit);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004518
4519MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4520MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4521MODULE_LICENSE("GPL");
4522
4523module_param(rxq_number, int, S_IRUGO);
4524module_param(txq_number, int, S_IRUGO);
4525
4526module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01004527module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);