blob: 011cd26953d934394ddd261678e19b77d13ba112 [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>
Russell Kinga10c1c82019-02-07 16:19:26 +000030#include <linux/phy/phy.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030031#include <linux/phy.h>
Russell King503f9aa92018-01-02 17:24:44 +000032#include <linux/phylink.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080033#include <linux/platform_device.h>
34#include <linux/skbuff.h>
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +010035#include <net/hwbm.h>
Marcin Wojtasdc35a102016-03-14 09:39:03 +010036#include "mvneta_bm.h"
Jisheng Zhang0e03f562016-01-20 19:27:22 +080037#include <net/ip.h>
38#include <net/ipv6.h>
39#include <net/tso.h>
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +020040#include <net/page_pool.h>
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +020041#include <linux/bpf_trace.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030042
43/* Registers */
44#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
Marcin Wojtase5bdf682015-11-30 13:27:42 +010045#define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +010046#define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
47#define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
48#define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
49#define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
Thomas Petazzonic5aff182012-08-17 14:04:28 +030050#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
51#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
52#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
53#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
54#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
55#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
56#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
57#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
58#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
59#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
60#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
61#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
62#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
Marcin Wojtasdc35a102016-03-14 09:39:03 +010063#define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
64#define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
65#define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
Thomas Petazzonic5aff182012-08-17 14:04:28 +030066#define MVNETA_PORT_RX_RESET 0x1cc0
67#define MVNETA_PORT_RX_DMA_RESET BIT(0)
68#define MVNETA_PHY_ADDR 0x2000
69#define MVNETA_PHY_ADDR_MASK 0x1f
70#define MVNETA_MBUS_RETRY 0x2010
71#define MVNETA_UNIT_INTR_CAUSE 0x2080
72#define MVNETA_UNIT_CONTROL 0x20B0
73#define MVNETA_PHY_POLLING_ENABLE BIT(1)
74#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
75#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
76#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
77#define MVNETA_BASE_ADDR_ENABLE 0x2290
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +010078#define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
Thomas Petazzonic5aff182012-08-17 14:04:28 +030079#define MVNETA_PORT_CONFIG 0x2400
80#define MVNETA_UNI_PROMISC_MODE BIT(0)
81#define MVNETA_DEF_RXQ(q) ((q) << 1)
82#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
83#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
84#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
85#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
86#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
87#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
88#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
89 MVNETA_DEF_RXQ_ARP(q) | \
90 MVNETA_DEF_RXQ_TCP(q) | \
91 MVNETA_DEF_RXQ_UDP(q) | \
92 MVNETA_DEF_RXQ_BPDU(q) | \
93 MVNETA_TX_UNSET_ERR_SUM | \
94 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
95#define MVNETA_PORT_CONFIG_EXTEND 0x2404
96#define MVNETA_MAC_ADDR_LOW 0x2414
97#define MVNETA_MAC_ADDR_HIGH 0x2418
98#define MVNETA_SDMA_CONFIG 0x241c
99#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300100#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
101#define MVNETA_RX_NO_DATA_SWAP BIT(4)
102#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200103#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300104#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
105#define MVNETA_PORT_STATUS 0x2444
106#define MVNETA_TX_IN_PRGRS BIT(1)
107#define MVNETA_TX_FIFO_EMPTY BIT(8)
108#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200109#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200110#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200111#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300112#define MVNETA_TYPE_PRIO 0x24bc
113#define MVNETA_FORCE_UNI BIT(21)
114#define MVNETA_TXQ_CMD_1 0x24e4
115#define MVNETA_TXQ_CMD 0x2448
116#define MVNETA_TXQ_DISABLE_SHIFT 8
117#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Andrew Lunne4839112015-10-22 18:37:36 +0100118#define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
119#define MVNETA_OVERRUN_FRAME_COUNT 0x2488
Stas Sergeev898b29702015-04-01 20:32:49 +0300120#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
121#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300122#define MVNETA_ACC_MODE 0x2500
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100123#define MVNETA_BM_ADDRESS 0x2504
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300124#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
125#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
126#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100127#define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100128#define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300129#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100130
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100131/* Exception Interrupt Port/Queue Cause register
132 *
133 * Their behavior depend of the mapping done using the PCPX2Q
134 * registers. For a given CPU if the bit associated to a queue is not
135 * set, then for the register a read from this CPU will always return
136 * 0 and a write won't do anything
137 */
willy tarreau40ba35e2014-01-16 08:20:10 +0100138
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300139#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300140#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100141
142/* bits 0..7 = TXQ SENT, one bit per queue.
143 * bits 8..15 = RXQ OCCUP, one bit per queue.
144 * bits 16..23 = RXQ FREE, one bit per queue.
145 * bit 29 = OLD_REG_SUM, see old reg ?
146 * bit 30 = TX_ERR_SUM, one bit for 4 ports
147 * bit 31 = MISC_SUM, one bit for 4 ports
148 */
149#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
150#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
151#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
152#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b29702015-04-01 20:32:49 +0300153#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100154
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300155#define MVNETA_INTR_OLD_CAUSE 0x25a8
156#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100157
158/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300159#define MVNETA_INTR_MISC_CAUSE 0x25b0
160#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100161
162#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
163#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
164#define MVNETA_CAUSE_PTP BIT(4)
165
166#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
167#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
168#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
169#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
170#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
171#define MVNETA_CAUSE_PRBS_ERR BIT(12)
172#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
173#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
174
175#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
176#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
177#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
178
179#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
180#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
181#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
182
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300183#define MVNETA_INTR_ENABLE 0x25b8
184#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
Marcin Wojtasdc1aadf2015-11-30 13:27:43 +0100185#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
willy tarreau40ba35e2014-01-16 08:20:10 +0100186
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300187#define MVNETA_RXQ_CMD 0x2680
188#define MVNETA_RXQ_DISABLE_SHIFT 8
189#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
190#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
191#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
192#define MVNETA_GMAC_CTRL_0 0x2c00
193#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
194#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
Russell King22f4bf82018-01-02 17:24:54 +0000195#define MVNETA_GMAC0_PORT_1000BASE_X BIT(1)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300196#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
197#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b29702015-04-01 20:32:49 +0300198#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100199#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300200#define MVNETA_GMAC2_PORT_RGMII BIT(4)
201#define MVNETA_GMAC2_PORT_RESET BIT(6)
202#define MVNETA_GMAC_STATUS 0x2c10
203#define MVNETA_GMAC_LINK_UP BIT(0)
204#define MVNETA_GMAC_SPEED_1000 BIT(1)
205#define MVNETA_GMAC_SPEED_100 BIT(2)
206#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
207#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
208#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
209#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
210#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
Russell King503f9aa92018-01-02 17:24:44 +0000211#define MVNETA_GMAC_AN_COMPLETE BIT(11)
212#define MVNETA_GMAC_SYNC_OK BIT(14)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300213#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
214#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
215#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b29702015-04-01 20:32:49 +0300216#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Russell King22f4bf82018-01-02 17:24:54 +0000217#define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3)
218#define MVNETA_GMAC_INBAND_RESTART_AN BIT(4)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300219#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
220#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200221#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Russell King22f4bf82018-01-02 17:24:54 +0000222#define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8)
223#define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9)
Stas Sergeev898b29702015-04-01 20:32:49 +0300224#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300225#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200226#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Maxime Chevallierda58a932018-09-25 15:59:39 +0200227#define MVNETA_GMAC_CTRL_4 0x2c90
228#define MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE BIT(1)
Andrew Lunne4839112015-10-22 18:37:36 +0100229#define MVNETA_MIB_COUNTERS_BASE 0x3000
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300230#define MVNETA_MIB_LATE_COLLISION 0x7c
231#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
232#define MVNETA_DA_FILT_OTH_MCAST 0x3500
233#define MVNETA_DA_FILT_UCAST_BASE 0x3600
234#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
235#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
236#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
237#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
238#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
239#define MVNETA_TXQ_DEC_SENT_SHIFT 16
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100240#define MVNETA_TXQ_DEC_SENT_MASK 0xff
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300241#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
242#define MVNETA_TXQ_SENT_DESC_SHIFT 16
243#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
244#define MVNETA_PORT_TX_RESET 0x3cf0
245#define MVNETA_PORT_TX_DMA_RESET BIT(0)
246#define MVNETA_TX_MTU 0x3e0c
247#define MVNETA_TX_TOKEN_SIZE 0x3e14
248#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
249#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
250#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
251
Russell King6d81f452018-01-02 17:25:04 +0000252#define MVNETA_LPI_CTRL_0 0x2cc0
253#define MVNETA_LPI_CTRL_1 0x2cc4
254#define MVNETA_LPI_REQUEST_ENABLE BIT(0)
255#define MVNETA_LPI_CTRL_2 0x2cc8
256#define MVNETA_LPI_STATUS 0x2ccc
257
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300258#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
259
260/* Descriptor ring Macros */
261#define MVNETA_QUEUE_NEXT_DESC(q, index) \
262 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
263
264/* Various constants */
265
266/* Coalescing */
Dmitri Epshtein06708f82016-07-06 04:18:58 +0200267#define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300268#define MVNETA_RX_COAL_PKTS 32
269#define MVNETA_RX_COAL_USEC 100
270
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100271/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300272 * by Marvell switches when a specific hardware mode is enabled (not
273 * supported by this driver) or is filled automatically by zeroes on
274 * the RX side. Those two bytes being at the front of the Ethernet
275 * header, they allow to have the IP header aligned on a 4 bytes
276 * boundary automatically: the hardware skips those two bytes on its
277 * own.
278 */
279#define MVNETA_MH_SIZE 2
280
281#define MVNETA_VLAN_TAG_LEN 4
282
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100283#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300284#define MVNETA_TX_CSUM_MAX_SIZE 9800
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100285#define MVNETA_ACC_MODE_EXT1 1
286#define MVNETA_ACC_MODE_EXT2 2
287
288#define MVNETA_MAX_DECODE_WIN 6
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300289
290/* Timeout constants */
291#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
292#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
293#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
294
295#define MVNETA_TX_MTU_MAX 0x3ffff
296
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100297/* The RSS lookup table actually has 256 entries but we do not use
298 * them yet
299 */
300#define MVNETA_RSS_LU_TABLE_SIZE 1
301
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300302/* Max number of Rx descriptors */
Yelena Krivosheevc307e2a2018-07-18 18:10:53 +0200303#define MVNETA_MAX_RXD 512
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300304
305/* Max number of Tx descriptors */
Yelena Krivosheevc307e2a2018-07-18 18:10:53 +0200306#define MVNETA_MAX_TXD 1024
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300307
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300308/* Max number of allowed TCP segments for software TSO */
309#define MVNETA_MAX_TSO_SEGS 100
310
311#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
312
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300313/* descriptor aligned size */
314#define MVNETA_DESC_ALIGNED_SIZE 32
315
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100316/* Number of bytes to be taken into account by HW when putting incoming data
317 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
318 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
319 */
320#define MVNETA_RX_PKT_OFFSET_CORRECTION 64
321
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300322#define MVNETA_RX_PKT_SIZE(mtu) \
323 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
324 ETH_HLEN + ETH_FCS_LEN, \
Jisheng Zhangc66e98c2016-04-01 17:12:49 +0800325 cache_line_size())
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300326
Sven Auhagenca23cb02020-05-23 12:14:08 +0200327/* Driver assumes that the last 3 bits are 0 */
328#define MVNETA_SKB_HEADROOM (max(XDP_PACKET_HEADROOM, NET_SKB_PAD) & ~0x7)
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +0200329#define MVNETA_SKB_PAD (SKB_DATA_ALIGN(sizeof(struct skb_shared_info) + \
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +0200330 MVNETA_SKB_HEADROOM))
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +0200331#define MVNETA_SKB_SIZE(len) (SKB_DATA_ALIGN(len) + MVNETA_SKB_PAD)
332#define MVNETA_MAX_RX_BUF_SIZE (PAGE_SIZE - MVNETA_SKB_PAD)
333
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300334#define IS_TSO_HEADER(txq, addr) \
335 ((addr >= txq->tso_hdrs_phys) && \
336 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
337
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100338#define MVNETA_RX_GET_BM_POOL_ID(rxd) \
339 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300340
Russell King6d81f452018-01-02 17:25:04 +0000341enum {
342 ETHTOOL_STAT_EEE_WAKEUP,
Gregory CLEMENT17a96da2018-07-18 18:10:54 +0200343 ETHTOOL_STAT_SKB_ALLOC_ERR,
344 ETHTOOL_STAT_REFILL_ERR,
Lorenzo Bianconi3d866522020-02-16 22:07:32 +0100345 ETHTOOL_XDP_REDIRECT,
346 ETHTOOL_XDP_PASS,
347 ETHTOOL_XDP_DROP,
348 ETHTOOL_XDP_TX,
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +0100349 ETHTOOL_XDP_TX_ERR,
350 ETHTOOL_XDP_XMIT,
351 ETHTOOL_XDP_XMIT_ERR,
Russell King6d81f452018-01-02 17:25:04 +0000352 ETHTOOL_MAX_STATS,
353};
354
Russell King9b0cdef2015-10-22 18:37:30 +0100355struct mvneta_statistic {
356 unsigned short offset;
357 unsigned short type;
358 const char name[ETH_GSTRING_LEN];
359};
360
361#define T_REG_32 32
362#define T_REG_64 64
Russell King6d81f452018-01-02 17:25:04 +0000363#define T_SW 1
Russell King9b0cdef2015-10-22 18:37:30 +0100364
Lorenzo Bianconi6c8a8cf2020-02-16 22:07:33 +0100365#define MVNETA_XDP_PASS 0
366#define MVNETA_XDP_DROPPED BIT(0)
367#define MVNETA_XDP_TX BIT(1)
368#define MVNETA_XDP_REDIR BIT(2)
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +0200369
Russell King9b0cdef2015-10-22 18:37:30 +0100370static const struct mvneta_statistic mvneta_statistics[] = {
371 { 0x3000, T_REG_64, "good_octets_received", },
372 { 0x3010, T_REG_32, "good_frames_received", },
373 { 0x3008, T_REG_32, "bad_octets_received", },
374 { 0x3014, T_REG_32, "bad_frames_received", },
375 { 0x3018, T_REG_32, "broadcast_frames_received", },
376 { 0x301c, T_REG_32, "multicast_frames_received", },
377 { 0x3050, T_REG_32, "unrec_mac_control_received", },
378 { 0x3058, T_REG_32, "good_fc_received", },
379 { 0x305c, T_REG_32, "bad_fc_received", },
380 { 0x3060, T_REG_32, "undersize_received", },
381 { 0x3064, T_REG_32, "fragments_received", },
382 { 0x3068, T_REG_32, "oversize_received", },
383 { 0x306c, T_REG_32, "jabber_received", },
384 { 0x3070, T_REG_32, "mac_receive_error", },
385 { 0x3074, T_REG_32, "bad_crc_event", },
386 { 0x3078, T_REG_32, "collision", },
387 { 0x307c, T_REG_32, "late_collision", },
388 { 0x2484, T_REG_32, "rx_discard", },
389 { 0x2488, T_REG_32, "rx_overrun", },
390 { 0x3020, T_REG_32, "frames_64_octets", },
391 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
392 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
393 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
394 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
395 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
396 { 0x3038, T_REG_64, "good_octets_sent", },
397 { 0x3040, T_REG_32, "good_frames_sent", },
398 { 0x3044, T_REG_32, "excessive_collision", },
399 { 0x3048, T_REG_32, "multicast_frames_sent", },
400 { 0x304c, T_REG_32, "broadcast_frames_sent", },
401 { 0x3054, T_REG_32, "fc_sent", },
402 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
Russell King6d81f452018-01-02 17:25:04 +0000403 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
Gregory CLEMENT17a96da2018-07-18 18:10:54 +0200404 { ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", },
405 { ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", },
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +0100406 { ETHTOOL_XDP_REDIRECT, T_SW, "rx_xdp_redirect", },
407 { ETHTOOL_XDP_PASS, T_SW, "rx_xdp_pass", },
408 { ETHTOOL_XDP_DROP, T_SW, "rx_xdp_drop", },
409 { ETHTOOL_XDP_TX, T_SW, "rx_xdp_tx", },
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +0100410 { ETHTOOL_XDP_TX_ERR, T_SW, "rx_xdp_tx_errors", },
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +0100411 { ETHTOOL_XDP_XMIT, T_SW, "tx_xdp_xmit", },
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +0100412 { ETHTOOL_XDP_XMIT_ERR, T_SW, "tx_xdp_xmit_errors", },
Russell King9b0cdef2015-10-22 18:37:30 +0100413};
414
Lorenzo Bianconi320d5442020-02-16 22:07:31 +0100415struct mvneta_stats {
416 u64 rx_packets;
417 u64 rx_bytes;
418 u64 tx_packets;
419 u64 tx_bytes;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +0100420 /* xdp */
421 u64 xdp_redirect;
422 u64 xdp_pass;
423 u64 xdp_drop;
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +0100424 u64 xdp_xmit;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +0100425 u64 xdp_xmit_err;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +0100426 u64 xdp_tx;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +0100427 u64 xdp_tx_err;
Lorenzo Bianconi320d5442020-02-16 22:07:31 +0100428};
429
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +0100430struct mvneta_ethtool_stats {
Lorenzo Bianconi320d5442020-02-16 22:07:31 +0100431 struct mvneta_stats ps;
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +0100432 u64 skb_alloc_error;
433 u64 refill_error;
434};
435
willy tarreau74c41b02014-01-16 08:20:08 +0100436struct mvneta_pcpu_stats {
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +0100437 struct u64_stats_sync syncp;
438
439 struct mvneta_ethtool_stats es;
Lorenzo Bianconic35947b2020-02-06 10:14:39 +0100440 u64 rx_dropped;
441 u64 rx_errors;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300442};
443
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200444struct mvneta_pcpu_port {
445 /* Pointer to the shared port */
446 struct mvneta_port *pp;
447
448 /* Pointer to the CPU-local NAPI struct */
449 struct napi_struct napi;
450
451 /* Cause of the previous interrupt */
452 u32 cause_rx_tx;
453};
454
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300455struct mvneta_port {
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100456 u8 id;
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200457 struct mvneta_pcpu_port __percpu *ports;
458 struct mvneta_pcpu_stats __percpu *stats;
459
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300460 int pkt_size;
461 void __iomem *base;
462 struct mvneta_rx_queue *rxqs;
463 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300464 struct net_device *dev;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200465 struct hlist_node node_online;
466 struct hlist_node node_dead;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100467 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100468 /* Protect the access to the percpu interrupt registers,
469 * ensuring that the configuration remains coherent.
470 */
471 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100472 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300473
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100474 u32 cause_rx_tx;
475 struct napi_struct napi;
476
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +0200477 struct bpf_prog *xdp_prog;
478
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300479 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100480 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800481 /* AXI clock */
482 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300483 u8 mcast_count[256];
484 u16 tx_ring_size;
485 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300486
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300487 phy_interface_t phy_interface;
Russell King503f9aa92018-01-02 17:24:44 +0000488 struct device_node *dn;
Simon Guinotb65657f2015-06-30 16:20:22 +0200489 unsigned int tx_csum_limit;
Russell King503f9aa92018-01-02 17:24:44 +0000490 struct phylink *phylink;
Ioana Ciornei44cc27e2019-05-28 20:38:12 +0300491 struct phylink_config phylink_config;
Russell Kinga10c1c82019-02-07 16:19:26 +0000492 struct phy *comphy;
Russell King9b0cdef2015-10-22 18:37:30 +0100493
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100494 struct mvneta_bm *bm_priv;
495 struct mvneta_bm_pool *pool_long;
496 struct mvneta_bm_pool *pool_short;
497 int bm_win_id;
498
Russell King6d81f452018-01-02 17:25:04 +0000499 bool eee_enabled;
500 bool eee_active;
501 bool tx_lpi_enabled;
502
Russell King9b0cdef2015-10-22 18:37:30 +0100503 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100504
505 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100506
507 /* Flags for special SoC configurations */
508 bool neta_armada3700;
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100509 u16 rx_offset_correction;
Jane Li9768b452017-03-16 16:22:28 +0800510 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300511};
512
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100513/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300514 * layout of the transmit and reception DMA descriptors, and their
515 * layout is therefore defined by the hardware design
516 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200517
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300518#define MVNETA_TX_L3_OFF_SHIFT 0
519#define MVNETA_TX_IP_HLEN_SHIFT 8
520#define MVNETA_TX_L4_UDP BIT(16)
521#define MVNETA_TX_L3_IP6 BIT(17)
522#define MVNETA_TXD_IP_CSUM BIT(18)
523#define MVNETA_TXD_Z_PAD BIT(19)
524#define MVNETA_TXD_L_DESC BIT(20)
525#define MVNETA_TXD_F_DESC BIT(21)
526#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
527 MVNETA_TXD_L_DESC | \
528 MVNETA_TXD_F_DESC)
529#define MVNETA_TX_L4_CSUM_FULL BIT(30)
530#define MVNETA_TX_L4_CSUM_NOT BIT(31)
531
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300532#define MVNETA_RXD_ERR_CRC 0x0
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100533#define MVNETA_RXD_BM_POOL_SHIFT 13
534#define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300535#define MVNETA_RXD_ERR_SUMMARY BIT(16)
536#define MVNETA_RXD_ERR_OVERRUN BIT(17)
537#define MVNETA_RXD_ERR_LEN BIT(18)
538#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
539#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
540#define MVNETA_RXD_L3_IP4 BIT(25)
Yelena Krivosheev562e2f42018-07-18 18:10:57 +0200541#define MVNETA_RXD_LAST_DESC BIT(26)
542#define MVNETA_RXD_FIRST_DESC BIT(27)
543#define MVNETA_RXD_FIRST_LAST_DESC (MVNETA_RXD_FIRST_DESC | \
544 MVNETA_RXD_LAST_DESC)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300545#define MVNETA_RXD_L4_CSUM_OK BIT(30)
546
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200547#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200548struct mvneta_tx_desc {
549 u32 command; /* Options used by HW for packet transmitting.*/
Alexandre Bellonifbd1d522018-11-09 17:37:20 +0100550 u16 reserved1; /* csum_l4 (for future use) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200551 u16 data_size; /* Data size of transmitted packet in bytes */
552 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
553 u32 reserved2; /* hw_cmd - (for future use, PMT) */
554 u32 reserved3[4]; /* Reserved - (for future use) */
555};
556
557struct mvneta_rx_desc {
558 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300559 u16 reserved1; /* pnc_info - (for future use, PnC) */
560 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200561
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300562 u32 buf_phys_addr; /* Physical address of the buffer */
563 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200564
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300565 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
566 u16 reserved3; /* prefetch_cmd, for future use */
567 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200568
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300569 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
570 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
571};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200572#else
573struct mvneta_tx_desc {
574 u16 data_size; /* Data size of transmitted packet in bytes */
Alexandre Bellonifbd1d522018-11-09 17:37:20 +0100575 u16 reserved1; /* csum_l4 (for future use) */
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200576 u32 command; /* Options used by HW for packet transmitting.*/
577 u32 reserved2; /* hw_cmd - (for future use, PMT) */
578 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
579 u32 reserved3[4]; /* Reserved - (for future use) */
580};
581
582struct mvneta_rx_desc {
583 u16 data_size; /* Size of received packet in bytes */
584 u16 reserved1; /* pnc_info - (for future use, PnC) */
585 u32 status; /* Info about received packet */
586
587 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
588 u32 buf_phys_addr; /* Physical address of the buffer */
589
590 u16 reserved4; /* csum_l4 - (for future use, PnC) */
591 u16 reserved3; /* prefetch_cmd, for future use */
592 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
593
594 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
595 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
596};
597#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300598
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +0200599enum mvneta_tx_buf_type {
600 MVNETA_TYPE_SKB,
601 MVNETA_TYPE_XDP_TX,
602 MVNETA_TYPE_XDP_NDO,
603};
604
605struct mvneta_tx_buf {
606 enum mvneta_tx_buf_type type;
607 union {
608 struct xdp_frame *xdpf;
609 struct sk_buff *skb;
610 };
611};
612
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300613struct mvneta_tx_queue {
614 /* Number of this TX queue, in the range 0-7 */
615 u8 id;
616
617 /* Number of TX DMA descriptors in the descriptor ring */
618 int size;
619
620 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100621 * descriptor ring
622 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300623 int count;
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100624 int pending;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300625 int tx_stop_threshold;
626 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300627
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +0200628 /* Array of transmitted buffers */
629 struct mvneta_tx_buf *buf;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300630
631 /* Index of last TX DMA descriptor that was inserted */
632 int txq_put_index;
633
634 /* Index of the TX DMA descriptor to be cleaned up */
635 int txq_get_index;
636
637 u32 done_pkts_coal;
638
639 /* Virtual address of the TX DMA descriptors array */
640 struct mvneta_tx_desc *descs;
641
642 /* DMA address of the TX DMA descriptors array */
643 dma_addr_t descs_phys;
644
645 /* Index of the last TX DMA descriptor */
646 int last_desc;
647
648 /* Index of the next TX DMA descriptor to process */
649 int next_desc_to_proc;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300650
651 /* DMA buffers for TSO headers */
652 char *tso_hdrs;
653
654 /* DMA address of TSO headers */
655 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100656
657 /* Affinity mask for CPUs*/
658 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300659};
660
661struct mvneta_rx_queue {
662 /* rx queue number, in the range 0-7 */
663 u8 id;
664
665 /* num of rx descriptors in the rx descriptor ring */
666 int size;
667
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300668 u32 pkts_coal;
669 u32 time_coal;
670
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +0200671 /* page_pool */
672 struct page_pool *page_pool;
673 struct xdp_rxq_info xdp_rxq;
674
Gregory CLEMENTf88bee12016-12-01 18:03:06 +0100675 /* Virtual address of the RX buffer */
676 void **buf_virt_addr;
677
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300678 /* Virtual address of the RX DMA descriptors array */
679 struct mvneta_rx_desc *descs;
680
681 /* DMA address of the RX DMA descriptors array */
682 dma_addr_t descs_phys;
683
684 /* Index of the last RX DMA descriptor */
685 int last_desc;
686
687 /* Index of the next RX DMA descriptor to process */
688 int next_desc_to_proc;
Gregory CLEMENT17a96da2018-07-18 18:10:54 +0200689
Yelena Krivosheev562e2f42018-07-18 18:10:57 +0200690 /* Index of first RX DMA descriptor to refill */
691 int first_to_refill;
692 u32 refill_num;
693
694 /* pointer to uncomplete skb buffer */
695 struct sk_buff *skb;
696 int left_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300697};
698
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200699static enum cpuhp_state online_hpstate;
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300700/* The hardware supports eight (8) rx queues, but we are only allowing
701 * the first one to be used. Therefore, let's just allocate one queue.
702 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200703static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300704static int txq_number = 8;
705
706static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300707
willy tarreauf19fadf2014-01-16 08:20:17 +0100708static int rx_copybreak __read_mostly = 256;
709
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100710/* HW BM need that each port be identify by a unique ID */
711static int global_port_id;
712
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300713#define MVNETA_DRIVER_NAME "mvneta"
714#define MVNETA_DRIVER_VERSION "1.0"
715
716/* Utility/helper methods */
717
718/* Write helper method */
719static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
720{
721 writel(data, pp->base + offset);
722}
723
724/* Read helper method */
725static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
726{
727 return readl(pp->base + offset);
728}
729
730/* Increment txq get counter */
731static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
732{
733 txq->txq_get_index++;
734 if (txq->txq_get_index == txq->size)
735 txq->txq_get_index = 0;
736}
737
738/* Increment txq put counter */
739static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
740{
741 txq->txq_put_index++;
742 if (txq->txq_put_index == txq->size)
743 txq->txq_put_index = 0;
744}
745
746
747/* Clear all MIB counters */
748static void mvneta_mib_counters_clear(struct mvneta_port *pp)
749{
750 int i;
751 u32 dummy;
752
753 /* Perform dummy reads from MIB counters */
754 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
755 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100756 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
757 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300758}
759
760/* Get System Network Statistics */
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800761static void
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +0800762mvneta_get_stats64(struct net_device *dev,
763 struct rtnl_link_stats64 *stats)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300764{
765 struct mvneta_port *pp = netdev_priv(dev);
766 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100767 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300768
willy tarreau74c41b02014-01-16 08:20:08 +0100769 for_each_possible_cpu(cpu) {
770 struct mvneta_pcpu_stats *cpu_stats;
771 u64 rx_packets;
772 u64 rx_bytes;
Lorenzo Bianconic35947b2020-02-06 10:14:39 +0100773 u64 rx_dropped;
774 u64 rx_errors;
willy tarreau74c41b02014-01-16 08:20:08 +0100775 u64 tx_packets;
776 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300777
willy tarreau74c41b02014-01-16 08:20:08 +0100778 cpu_stats = per_cpu_ptr(pp->stats, cpu);
779 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700780 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
Lorenzo Bianconi320d5442020-02-16 22:07:31 +0100781 rx_packets = cpu_stats->es.ps.rx_packets;
782 rx_bytes = cpu_stats->es.ps.rx_bytes;
Lorenzo Bianconic35947b2020-02-06 10:14:39 +0100783 rx_dropped = cpu_stats->rx_dropped;
784 rx_errors = cpu_stats->rx_errors;
Lorenzo Bianconi320d5442020-02-16 22:07:31 +0100785 tx_packets = cpu_stats->es.ps.tx_packets;
786 tx_bytes = cpu_stats->es.ps.tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700787 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300788
willy tarreau74c41b02014-01-16 08:20:08 +0100789 stats->rx_packets += rx_packets;
790 stats->rx_bytes += rx_bytes;
Lorenzo Bianconic35947b2020-02-06 10:14:39 +0100791 stats->rx_dropped += rx_dropped;
792 stats->rx_errors += rx_errors;
willy tarreau74c41b02014-01-16 08:20:08 +0100793 stats->tx_packets += tx_packets;
794 stats->tx_bytes += tx_bytes;
795 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300796
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300797 stats->tx_dropped = dev->stats.tx_dropped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300798}
799
800/* Rx descriptors helper methods */
801
willy tarreau54282132014-01-16 08:20:14 +0100802/* Checks whether the RX descriptor having this status is both the first
803 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300804 * received through a single RX descriptor, so not having each RX
805 * descriptor with its first and last bits set is an error
806 */
willy tarreau54282132014-01-16 08:20:14 +0100807static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300808{
willy tarreau54282132014-01-16 08:20:14 +0100809 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300810 MVNETA_RXD_FIRST_LAST_DESC;
811}
812
813/* Add number of descriptors ready to receive new packets */
814static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
815 struct mvneta_rx_queue *rxq,
816 int ndescs)
817{
818 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100819 * be added at once
820 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300821 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
822 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
823 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
824 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
825 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
826 }
827
828 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
829 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
830}
831
832/* Get number of RX descriptors occupied by received packets */
833static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
834 struct mvneta_rx_queue *rxq)
835{
836 u32 val;
837
838 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
839 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
840}
841
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100842/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300843 * from mvneta_rxq_drop_pkts().
844 */
845static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
846 struct mvneta_rx_queue *rxq,
847 int rx_done, int rx_filled)
848{
849 u32 val;
850
851 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
852 val = rx_done |
853 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
854 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
855 return;
856 }
857
858 /* Only 255 descriptors can be added at once */
859 while ((rx_done > 0) || (rx_filled > 0)) {
860 if (rx_done <= 0xff) {
861 val = rx_done;
862 rx_done = 0;
863 } else {
864 val = 0xff;
865 rx_done -= 0xff;
866 }
867 if (rx_filled <= 0xff) {
868 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
869 rx_filled = 0;
870 } else {
871 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
872 rx_filled -= 0xff;
873 }
874 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
875 }
876}
877
878/* Get pointer to next RX descriptor to be processed by SW */
879static struct mvneta_rx_desc *
880mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
881{
882 int rx_desc = rxq->next_desc_to_proc;
883
884 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100885 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300886 return rxq->descs + rx_desc;
887}
888
889/* Change maximum receive size of the port. */
890static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
891{
892 u32 val;
893
894 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
895 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
896 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
897 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
898 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
899}
900
901
902/* Set rx queue offset */
903static void mvneta_rxq_offset_set(struct mvneta_port *pp,
904 struct mvneta_rx_queue *rxq,
905 int offset)
906{
907 u32 val;
908
909 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
910 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
911
912 /* Offset is in */
913 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
914 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
915}
916
917
918/* Tx descriptors helper methods */
919
920/* Update HW with number of TX descriptors to be sent */
921static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
922 struct mvneta_tx_queue *txq,
923 int pend_desc)
924{
925 u32 val;
926
Simon Guinot0d637852017-11-13 16:27:02 +0100927 pend_desc += txq->pending;
928
929 /* Only 255 Tx descriptors can be added at once */
930 do {
931 val = min(pend_desc, 255);
932 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
933 pend_desc -= val;
934 } while (pend_desc > 0);
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100935 txq->pending = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300936}
937
938/* Get pointer to next TX descriptor to be processed (send) by HW */
939static struct mvneta_tx_desc *
940mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
941{
942 int tx_desc = txq->next_desc_to_proc;
943
944 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
945 return txq->descs + tx_desc;
946}
947
948/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100949 * mapping failures in the TX path.
950 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300951static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
952{
953 if (txq->next_desc_to_proc == 0)
954 txq->next_desc_to_proc = txq->last_desc - 1;
955 else
956 txq->next_desc_to_proc--;
957}
958
959/* Set rxq buf size */
960static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
961 struct mvneta_rx_queue *rxq,
962 int buf_size)
963{
964 u32 val;
965
966 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
967
968 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
969 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
970
971 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
972}
973
974/* Disable buffer management (BM) */
975static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
976 struct mvneta_rx_queue *rxq)
977{
978 u32 val;
979
980 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
981 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
982 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
983}
984
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100985/* Enable buffer management (BM) */
986static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
987 struct mvneta_rx_queue *rxq)
988{
989 u32 val;
990
991 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
992 val |= MVNETA_RXQ_HW_BUF_ALLOC;
993 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
994}
995
996/* Notify HW about port's assignment of pool for bigger packets */
997static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
998 struct mvneta_rx_queue *rxq)
999{
1000 u32 val;
1001
1002 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
1003 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
1004 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
1005
1006 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
1007}
1008
1009/* Notify HW about port's assignment of pool for smaller packets */
1010static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
1011 struct mvneta_rx_queue *rxq)
1012{
1013 u32 val;
1014
1015 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
1016 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
1017 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
1018
1019 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
1020}
1021
1022/* Set port's receive buffer size for assigned BM pool */
1023static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
1024 int buf_size,
1025 u8 pool_id)
1026{
1027 u32 val;
1028
1029 if (!IS_ALIGNED(buf_size, 8)) {
1030 dev_warn(pp->dev->dev.parent,
1031 "illegal buf_size value %d, round to %d\n",
1032 buf_size, ALIGN(buf_size, 8));
1033 buf_size = ALIGN(buf_size, 8);
1034 }
1035
1036 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
1037 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
1038 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
1039}
1040
1041/* Configure MBUS window in order to enable access BM internal SRAM */
1042static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
1043 u8 target, u8 attr)
1044{
1045 u32 win_enable, win_protect;
1046 int i;
1047
1048 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
1049
1050 if (pp->bm_win_id < 0) {
1051 /* Find first not occupied window */
1052 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
1053 if (win_enable & (1 << i)) {
1054 pp->bm_win_id = i;
1055 break;
1056 }
1057 }
1058 if (i == MVNETA_MAX_DECODE_WIN)
1059 return -ENOMEM;
1060 } else {
1061 i = pp->bm_win_id;
1062 }
1063
1064 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1065 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1066
1067 if (i < 4)
1068 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1069
1070 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
1071 (attr << 8) | target);
1072
1073 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
1074
1075 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
1076 win_protect |= 3 << (2 * i);
1077 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
1078
1079 win_enable &= ~(1 << i);
1080 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1081
1082 return 0;
1083}
1084
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001085static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001086{
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001087 u32 wsize;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001088 u8 target, attr;
1089 int err;
1090
1091 /* Get BM window information */
1092 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1093 &target, &attr);
1094 if (err < 0)
1095 return err;
1096
1097 pp->bm_win_id = -1;
1098
1099 /* Open NETA -> BM window */
1100 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1101 target, attr);
1102 if (err < 0) {
1103 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1104 return err;
1105 }
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001106 return 0;
1107}
1108
1109/* Assign and initialize pools for port. In case of fail
1110 * buffer manager will remain disabled for current port.
1111 */
1112static int mvneta_bm_port_init(struct platform_device *pdev,
1113 struct mvneta_port *pp)
1114{
1115 struct device_node *dn = pdev->dev.of_node;
1116 u32 long_pool_id, short_pool_id;
1117
1118 if (!pp->neta_armada3700) {
1119 int ret;
1120
1121 ret = mvneta_bm_port_mbus_init(pp);
1122 if (ret)
1123 return ret;
1124 }
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001125
1126 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1127 netdev_info(pp->dev, "missing long pool id\n");
1128 return -EINVAL;
1129 }
1130
1131 /* Create port's long pool depending on mtu */
1132 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1133 MVNETA_BM_LONG, pp->id,
1134 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1135 if (!pp->pool_long) {
1136 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1137 return -ENOMEM;
1138 }
1139
1140 pp->pool_long->port_map |= 1 << pp->id;
1141
1142 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1143 pp->pool_long->id);
1144
1145 /* If short pool id is not defined, assume using single pool */
1146 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1147 short_pool_id = long_pool_id;
1148
1149 /* Create port's short pool */
1150 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1151 MVNETA_BM_SHORT, pp->id,
1152 MVNETA_BM_SHORT_PKT_SIZE);
1153 if (!pp->pool_short) {
1154 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1155 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1156 return -ENOMEM;
1157 }
1158
1159 if (short_pool_id != long_pool_id) {
1160 pp->pool_short->port_map |= 1 << pp->id;
1161 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1162 pp->pool_short->id);
1163 }
1164
1165 return 0;
1166}
1167
1168/* Update settings of a pool for bigger packets */
1169static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1170{
1171 struct mvneta_bm_pool *bm_pool = pp->pool_long;
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001172 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001173 int num;
1174
1175 /* Release all buffers from long pool */
1176 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001177 if (hwbm_pool->buf_num) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001178 WARN(1, "cannot free all buffers in pool %d\n",
1179 bm_pool->id);
1180 goto bm_mtu_err;
1181 }
1182
1183 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1184 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001185 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1186 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001187
1188 /* Fill entire long pool */
Sebastian Andrzej Siewior6dcdd882019-06-07 21:20:40 +02001189 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001190 if (num != hwbm_pool->size) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001191 WARN(1, "pool %d: %d of %d allocated\n",
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001192 bm_pool->id, num, hwbm_pool->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001193 goto bm_mtu_err;
1194 }
1195 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1196
1197 return;
1198
1199bm_mtu_err:
1200 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1201 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1202
1203 pp->bm_priv = NULL;
Lorenzo Bianconi44efc782020-01-29 12:50:53 +01001204 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001205 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1206 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1207}
1208
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001209/* Start the Ethernet port RX and TX activity */
1210static void mvneta_port_up(struct mvneta_port *pp)
1211{
1212 int queue;
1213 u32 q_map;
1214
1215 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001216 q_map = 0;
1217 for (queue = 0; queue < txq_number; queue++) {
1218 struct mvneta_tx_queue *txq = &pp->txqs[queue];
Markus Elfringf95936c2017-04-16 22:45:33 +02001219 if (txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001220 q_map |= (1 << queue);
1221 }
1222 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1223
Yelena Krivosheeve81b5e02018-03-30 12:05:31 +02001224 q_map = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001225 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001226 for (queue = 0; queue < rxq_number; queue++) {
1227 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1228
Markus Elfringf95936c2017-04-16 22:45:33 +02001229 if (rxq->descs)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001230 q_map |= (1 << queue);
1231 }
1232 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001233}
1234
1235/* Stop the Ethernet port activity */
1236static void mvneta_port_down(struct mvneta_port *pp)
1237{
1238 u32 val;
1239 int count;
1240
1241 /* Stop Rx port activity. Check port Rx activity. */
1242 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1243
1244 /* Issue stop command for active channels only */
1245 if (val != 0)
1246 mvreg_write(pp, MVNETA_RXQ_CMD,
1247 val << MVNETA_RXQ_DISABLE_SHIFT);
1248
1249 /* Wait for all Rx activity to terminate. */
1250 count = 0;
1251 do {
1252 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1253 netdev_warn(pp->dev,
Dmitri Epshtein0838abb2016-03-12 18:44:19 +01001254 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001255 val);
1256 break;
1257 }
1258 mdelay(1);
1259
1260 val = mvreg_read(pp, MVNETA_RXQ_CMD);
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001261 } while (val & MVNETA_RXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001262
1263 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001264 * command for active channels only
1265 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001266 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1267
1268 if (val != 0)
1269 mvreg_write(pp, MVNETA_TXQ_CMD,
1270 (val << MVNETA_TXQ_DISABLE_SHIFT));
1271
1272 /* Wait for all Tx activity to terminate. */
1273 count = 0;
1274 do {
1275 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1276 netdev_warn(pp->dev,
1277 "TIMEOUT for TX stopped status=0x%08x\n",
1278 val);
1279 break;
1280 }
1281 mdelay(1);
1282
1283 /* Check TX Command reg that all Txqs are stopped */
1284 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1285
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001286 } while (val & MVNETA_TXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001287
1288 /* Double check to verify that TX FIFO is empty */
1289 count = 0;
1290 do {
1291 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1292 netdev_warn(pp->dev,
Dmitri Epshtein0838abb2016-03-12 18:44:19 +01001293 "TX FIFO empty timeout status=0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001294 val);
1295 break;
1296 }
1297 mdelay(1);
1298
1299 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1300 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1301 (val & MVNETA_TX_IN_PRGRS));
1302
1303 udelay(200);
1304}
1305
1306/* Enable the port by setting the port enable bit of the MAC control register */
1307static void mvneta_port_enable(struct mvneta_port *pp)
1308{
1309 u32 val;
1310
1311 /* Enable port */
1312 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1313 val |= MVNETA_GMAC0_PORT_ENABLE;
1314 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1315}
1316
1317/* Disable the port and wait for about 200 usec before retuning */
1318static void mvneta_port_disable(struct mvneta_port *pp)
1319{
1320 u32 val;
1321
1322 /* Reset the Enable bit in the Serial Control Register */
1323 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1324 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1325 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1326
1327 udelay(200);
1328}
1329
1330/* Multicast tables methods */
1331
1332/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1333static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1334{
1335 int offset;
1336 u32 val;
1337
1338 if (queue == -1) {
1339 val = 0;
1340 } else {
1341 val = 0x1 | (queue << 1);
1342 val |= (val << 24) | (val << 16) | (val << 8);
1343 }
1344
1345 for (offset = 0; offset <= 0xc; offset += 4)
1346 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1347}
1348
1349/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1350static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1351{
1352 int offset;
1353 u32 val;
1354
1355 if (queue == -1) {
1356 val = 0;
1357 } else {
1358 val = 0x1 | (queue << 1);
1359 val |= (val << 24) | (val << 16) | (val << 8);
1360 }
1361
1362 for (offset = 0; offset <= 0xfc; offset += 4)
1363 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1364
1365}
1366
1367/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1368static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1369{
1370 int offset;
1371 u32 val;
1372
1373 if (queue == -1) {
1374 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1375 val = 0;
1376 } else {
1377 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1378 val = 0x1 | (queue << 1);
1379 val |= (val << 24) | (val << 16) | (val << 8);
1380 }
1381
1382 for (offset = 0; offset <= 0xfc; offset += 4)
1383 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1384}
1385
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001386static void mvneta_percpu_unmask_interrupt(void *arg)
1387{
1388 struct mvneta_port *pp = arg;
1389
1390 /* All the queue are unmasked, but actually only the ones
1391 * mapped to this CPU will be unmasked
1392 */
1393 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1394 MVNETA_RX_INTR_MASK_ALL |
1395 MVNETA_TX_INTR_MASK_ALL |
1396 MVNETA_MISCINTR_INTR_MASK);
1397}
1398
1399static void mvneta_percpu_mask_interrupt(void *arg)
1400{
1401 struct mvneta_port *pp = arg;
1402
1403 /* All the queue are masked, but actually only the ones
1404 * mapped to this CPU will be masked
1405 */
1406 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1407 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1408 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1409}
1410
1411static void mvneta_percpu_clear_intr_cause(void *arg)
1412{
1413 struct mvneta_port *pp = arg;
1414
1415 /* All the queue are cleared, but actually only the ones
1416 * mapped to this CPU will be cleared
1417 */
1418 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1419 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1420 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1421}
1422
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001423/* This method sets defaults to the NETA port:
1424 * Clears interrupt Cause and Mask registers.
1425 * Clears all MAC tables.
1426 * Sets defaults to all registers.
1427 * Resets RX and TX descriptor rings.
1428 * Resets PHY.
1429 * This method can be called after mvneta_port_down() to return the port
1430 * settings to defaults.
1431 */
1432static void mvneta_defaults_set(struct mvneta_port *pp)
1433{
1434 int cpu;
1435 int queue;
1436 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001437 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001438
1439 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001440 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001441
1442 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001443 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001444 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1445
1446 /* Enable MBUS Retry bit16 */
1447 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1448
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001449 /* Set CPU queue access map. CPUs are assigned to the RX and
1450 * TX queues modulo their number. If there is only one TX
1451 * queue then it is assigned to the CPU associated to the
1452 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001453 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001454 for_each_present_cpu(cpu) {
1455 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001456 int rxq, txq;
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001457 if (!pp->neta_armada3700) {
1458 for (rxq = 0; rxq < rxq_number; rxq++)
1459 if ((rxq % max_cpu) == cpu)
1460 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001461
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001462 for (txq = 0; txq < txq_number; txq++)
1463 if ((txq % max_cpu) == cpu)
1464 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001465
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001466 /* With only one TX queue we configure a special case
1467 * which will allow to get all the irq on a single
1468 * CPU
1469 */
1470 if (txq_number == 1)
1471 txq_map = (cpu == pp->rxq_def) ?
1472 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001473
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001474 } else {
1475 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1476 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1477 }
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001478
1479 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1480 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001481
1482 /* Reset RX and TX DMAs */
1483 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1484 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1485
1486 /* Disable Legacy WRR, Disable EJP, Release from reset */
1487 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1488 for (queue = 0; queue < txq_number; queue++) {
1489 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1490 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1491 }
1492
1493 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1494 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1495
1496 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001497 if (pp->bm_priv)
1498 /* HW buffer management + legacy parser */
1499 val = MVNETA_ACC_MODE_EXT2;
1500 else
1501 /* SW buffer management + legacy parser */
1502 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001503 mvreg_write(pp, MVNETA_ACC_MODE, val);
1504
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001505 if (pp->bm_priv)
1506 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1507
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001508 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001509 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001510 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1511
1512 val = 0;
1513 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1514 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1515
1516 /* Build PORT_SDMA_CONFIG_REG */
1517 val = 0;
1518
1519 /* Default burst size */
1520 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1521 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001522 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001523
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001524#if defined(__BIG_ENDIAN)
1525 val |= MVNETA_DESC_SWAP;
1526#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001527
1528 /* Assign port SDMA configuration */
1529 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1530
Thomas Petazzoni71408602013-09-04 16:21:18 +02001531 /* Disable PHY polling in hardware, since we're using the
1532 * kernel phylib to do this.
1533 */
1534 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1535 val &= ~MVNETA_PHY_POLLING_ENABLE;
1536 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1537
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001538 mvneta_set_ucast_table(pp, -1);
1539 mvneta_set_special_mcast_table(pp, -1);
1540 mvneta_set_other_mcast_table(pp, -1);
1541
1542 /* Set port interrupt enable register - default enable all */
1543 mvreg_write(pp, MVNETA_INTR_ENABLE,
1544 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1545 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001546
1547 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001548}
1549
1550/* Set max sizes for tx queues */
1551static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1552
1553{
1554 u32 val, size, mtu;
1555 int queue;
1556
1557 mtu = max_tx_size * 8;
1558 if (mtu > MVNETA_TX_MTU_MAX)
1559 mtu = MVNETA_TX_MTU_MAX;
1560
1561 /* Set MTU */
1562 val = mvreg_read(pp, MVNETA_TX_MTU);
1563 val &= ~MVNETA_TX_MTU_MAX;
1564 val |= mtu;
1565 mvreg_write(pp, MVNETA_TX_MTU, val);
1566
1567 /* TX token size and all TXQs token size must be larger that MTU */
1568 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1569
1570 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1571 if (size < mtu) {
1572 size = mtu;
1573 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1574 val |= size;
1575 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1576 }
1577 for (queue = 0; queue < txq_number; queue++) {
1578 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1579
1580 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1581 if (size < mtu) {
1582 size = mtu;
1583 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1584 val |= size;
1585 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1586 }
1587 }
1588}
1589
1590/* Set unicast address */
1591static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1592 int queue)
1593{
1594 unsigned int unicast_reg;
1595 unsigned int tbl_offset;
1596 unsigned int reg_offset;
1597
1598 /* Locate the Unicast table entry */
1599 last_nibble = (0xf & last_nibble);
1600
1601 /* offset from unicast tbl base */
1602 tbl_offset = (last_nibble / 4) * 4;
1603
1604 /* offset within the above reg */
1605 reg_offset = last_nibble % 4;
1606
1607 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1608
1609 if (queue == -1) {
1610 /* Clear accepts frame bit at specified unicast DA tbl entry */
1611 unicast_reg &= ~(0xff << (8 * reg_offset));
1612 } else {
1613 unicast_reg &= ~(0xff << (8 * reg_offset));
1614 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1615 }
1616
1617 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1618}
1619
1620/* Set mac address */
1621static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1622 int queue)
1623{
1624 unsigned int mac_h;
1625 unsigned int mac_l;
1626
1627 if (queue != -1) {
1628 mac_l = (addr[4] << 8) | (addr[5]);
1629 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1630 (addr[2] << 8) | (addr[3] << 0);
1631
1632 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1633 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1634 }
1635
1636 /* Accept frames of this address */
1637 mvneta_set_ucast_addr(pp, addr[5], queue);
1638}
1639
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001640/* Set the number of packets that will be received before RX interrupt
1641 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001642 */
1643static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1644 struct mvneta_rx_queue *rxq, u32 value)
1645{
1646 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1647 value | MVNETA_RXQ_NON_OCCUPIED(0));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001648}
1649
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001650/* Set the time delay in usec before RX interrupt will be generated by
1651 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001652 */
1653static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1654 struct mvneta_rx_queue *rxq, u32 value)
1655{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001656 u32 val;
1657 unsigned long clk_rate;
1658
1659 clk_rate = clk_get_rate(pp->clk);
1660 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001661
1662 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001663}
1664
1665/* Set threshold for TX_DONE pkts coalescing */
1666static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1667 struct mvneta_tx_queue *txq, u32 value)
1668{
1669 u32 val;
1670
1671 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1672
1673 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1674 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1675
1676 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001677}
1678
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001679/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1680static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001681 u32 phys_addr, void *virt_addr,
1682 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001683{
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001684 int i;
1685
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001686 rx_desc->buf_phys_addr = phys_addr;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001687 i = rx_desc - rxq->descs;
1688 rxq->buf_virt_addr[i] = virt_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001689}
1690
1691/* Decrement sent descriptors counter */
1692static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1693 struct mvneta_tx_queue *txq,
1694 int sent_desc)
1695{
1696 u32 val;
1697
1698 /* Only 255 TX descriptors can be updated at once */
1699 while (sent_desc > 0xff) {
1700 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1701 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1702 sent_desc = sent_desc - 0xff;
1703 }
1704
1705 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1706 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1707}
1708
1709/* Get number of TX descriptors already sent by HW */
1710static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1711 struct mvneta_tx_queue *txq)
1712{
1713 u32 val;
1714 int sent_desc;
1715
1716 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1717 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1718 MVNETA_TXQ_SENT_DESC_SHIFT;
1719
1720 return sent_desc;
1721}
1722
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001723/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001724 * The number of sent descriptors is returned.
1725 */
1726static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1727 struct mvneta_tx_queue *txq)
1728{
1729 int sent_desc;
1730
1731 /* Get number of sent descriptors */
1732 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1733
1734 /* Decrement sent descriptors counter */
1735 if (sent_desc)
1736 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1737
1738 return sent_desc;
1739}
1740
1741/* Set TXQ descriptors fields relevant for CSUM calculation */
1742static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1743 int ip_hdr_len, int l4_proto)
1744{
1745 u32 command;
1746
1747 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001748 * G_L4_chk, L4_type; required only for checksum
1749 * calculation
1750 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001751 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1752 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1753
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001754 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001755 command |= MVNETA_TXD_IP_CSUM;
1756 else
1757 command |= MVNETA_TX_L3_IP6;
1758
1759 if (l4_proto == IPPROTO_TCP)
1760 command |= MVNETA_TX_L4_CSUM_FULL;
1761 else if (l4_proto == IPPROTO_UDP)
1762 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1763 else
1764 command |= MVNETA_TX_L4_CSUM_NOT;
1765
1766 return command;
1767}
1768
1769
1770/* Display more error info */
1771static void mvneta_rx_error(struct mvneta_port *pp,
1772 struct mvneta_rx_desc *rx_desc)
1773{
Lorenzo Bianconic35947b2020-02-06 10:14:39 +01001774 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001775 u32 status = rx_desc->status;
1776
Lorenzo Bianconic35947b2020-02-06 10:14:39 +01001777 /* update per-cpu counter */
1778 u64_stats_update_begin(&stats->syncp);
1779 stats->rx_errors++;
1780 u64_stats_update_end(&stats->syncp);
1781
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001782 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1783 case MVNETA_RXD_ERR_CRC:
1784 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1785 status, rx_desc->data_size);
1786 break;
1787 case MVNETA_RXD_ERR_OVERRUN:
1788 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1789 status, rx_desc->data_size);
1790 break;
1791 case MVNETA_RXD_ERR_LEN:
1792 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1793 status, rx_desc->data_size);
1794 break;
1795 case MVNETA_RXD_ERR_RESOURCE:
1796 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1797 status, rx_desc->data_size);
1798 break;
1799 }
1800}
1801
willy tarreau54282132014-01-16 08:20:14 +01001802/* Handle RX checksum offload based on the descriptor's status */
1803static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001804 struct sk_buff *skb)
1805{
Yelena Krivosheevf945cec2018-07-18 18:10:56 +02001806 if ((pp->dev->features & NETIF_F_RXCSUM) &&
1807 (status & MVNETA_RXD_L3_IP4) &&
willy tarreau54282132014-01-16 08:20:14 +01001808 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001809 skb->csum = 0;
1810 skb->ip_summed = CHECKSUM_UNNECESSARY;
1811 return;
1812 }
1813
1814 skb->ip_summed = CHECKSUM_NONE;
1815}
1816
willy tarreau6c498972014-01-16 08:20:12 +01001817/* Return tx queue pointer (find last set bit) according to <cause> returned
1818 * form tx_done reg. <cause> must not be null. The return value is always a
1819 * valid queue for matching the first one found in <cause>.
1820 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001821static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1822 u32 cause)
1823{
1824 int queue = fls(cause) - 1;
1825
willy tarreau6c498972014-01-16 08:20:12 +01001826 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001827}
1828
1829/* Free tx queue skbuffs */
1830static void mvneta_txq_bufs_free(struct mvneta_port *pp,
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001831 struct mvneta_tx_queue *txq, int num,
1832 struct netdev_queue *nq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001833{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001834 unsigned int bytes_compl = 0, pkts_compl = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001835 int i;
1836
1837 for (i = 0; i < num; i++) {
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02001838 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_get_index];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001839 struct mvneta_tx_desc *tx_desc = txq->descs +
1840 txq->txq_get_index;
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001841
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001842 mvneta_txq_inc_get(txq);
1843
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02001844 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr) &&
1845 buf->type != MVNETA_TYPE_XDP_TX)
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001846 dma_unmap_single(pp->dev->dev.parent,
1847 tx_desc->buf_phys_addr,
1848 tx_desc->data_size, DMA_TO_DEVICE);
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02001849 if (buf->type == MVNETA_TYPE_SKB && buf->skb) {
1850 bytes_compl += buf->skb->len;
1851 pkts_compl++;
1852 dev_kfree_skb_any(buf->skb);
1853 } else if (buf->type == MVNETA_TYPE_XDP_TX ||
1854 buf->type == MVNETA_TYPE_XDP_NDO) {
1855 xdp_return_frame(buf->xdpf);
1856 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001857 }
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001858
1859 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001860}
1861
1862/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001863static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001864 struct mvneta_tx_queue *txq)
1865{
1866 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1867 int tx_done;
1868
1869 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001870 if (!tx_done)
1871 return;
1872
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001873 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001874
1875 txq->count -= tx_done;
1876
1877 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001878 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001879 netif_tx_wake_queue(nq);
1880 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001881}
1882
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001883/* Refill processing for SW buffer management */
Gregory CLEMENT7e47fd82018-07-18 18:10:55 +02001884/* Allocate page per descriptor */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001885static int mvneta_rx_refill(struct mvneta_port *pp,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001886 struct mvneta_rx_desc *rx_desc,
Gregory CLEMENT7e47fd82018-07-18 18:10:55 +02001887 struct mvneta_rx_queue *rxq,
1888 gfp_t gfp_mask)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001889{
1890 dma_addr_t phys_addr;
Gregory CLEMENT7e47fd82018-07-18 18:10:55 +02001891 struct page *page;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001892
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02001893 page = page_pool_alloc_pages(rxq->page_pool,
1894 gfp_mask | __GFP_NOWARN);
Gregory CLEMENT7e47fd82018-07-18 18:10:55 +02001895 if (!page)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001896 return -ENOMEM;
1897
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02001898 phys_addr = page_pool_get_dma_addr(page) + pp->rx_offset_correction;
Gregory CLEMENT7e47fd82018-07-18 18:10:55 +02001899 mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq);
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02001900
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001901 return 0;
1902}
1903
1904/* Handle tx checksum */
1905static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1906{
1907 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1908 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001909 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001910 u8 l4_proto;
1911
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001912 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001913 struct iphdr *ip4h = ip_hdr(skb);
1914
1915 /* Calculate IPv4 checksum and L4 checksum */
1916 ip_hdr_len = ip4h->ihl;
1917 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001918 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001919 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1920
1921 /* Read l4_protocol from one of IPv6 extra headers */
1922 if (skb_network_header_len(skb) > 0)
1923 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1924 l4_proto = ip6h->nexthdr;
1925 } else
1926 return MVNETA_TX_L4_CSUM_NOT;
1927
1928 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001929 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001930 }
1931
1932 return MVNETA_TX_L4_CSUM_NOT;
1933}
1934
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001935/* Drop packets received by the RXQ and free buffers */
1936static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1937 struct mvneta_rx_queue *rxq)
1938{
1939 int rx_done, i;
1940
1941 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001942 if (rx_done)
1943 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1944
1945 if (pp->bm_priv) {
1946 for (i = 0; i < rx_done; i++) {
1947 struct mvneta_rx_desc *rx_desc =
1948 mvneta_rxq_next_desc_get(rxq);
1949 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1950 struct mvneta_bm_pool *bm_pool;
1951
1952 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1953 /* Return dropped buffer to the pool */
1954 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1955 rx_desc->buf_phys_addr);
1956 }
1957 return;
1958 }
1959
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001960 for (i = 0; i < rxq->size; i++) {
1961 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001962 void *data = rxq->buf_virt_addr[i];
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02001963 if (!data || !(rx_desc->buf_phys_addr))
1964 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001965
Ilias Apalodimas458de8a2020-02-20 09:41:55 +02001966 page_pool_put_full_page(rxq->page_pool, data, false);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001967 }
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02001968 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
1969 xdp_rxq_info_unreg(&rxq->xdp_rxq);
1970 page_pool_destroy(rxq->page_pool);
1971 rxq->page_pool = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001972}
1973
Lorenzo Bianconiff519e22019-10-19 10:13:21 +02001974static void
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01001975mvneta_update_stats(struct mvneta_port *pp,
1976 struct mvneta_stats *ps)
Lorenzo Bianconiff519e22019-10-19 10:13:21 +02001977{
1978 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1979
1980 u64_stats_update_begin(&stats->syncp);
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01001981 stats->es.ps.rx_packets += ps->rx_packets;
1982 stats->es.ps.rx_bytes += ps->rx_bytes;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01001983 /* xdp */
1984 stats->es.ps.xdp_redirect += ps->xdp_redirect;
1985 stats->es.ps.xdp_pass += ps->xdp_pass;
1986 stats->es.ps.xdp_drop += ps->xdp_drop;
Lorenzo Bianconiff519e22019-10-19 10:13:21 +02001987 u64_stats_update_end(&stats->syncp);
1988}
1989
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02001990static inline
1991int mvneta_rx_refill_queue(struct mvneta_port *pp, struct mvneta_rx_queue *rxq)
1992{
1993 struct mvneta_rx_desc *rx_desc;
1994 int curr_desc = rxq->first_to_refill;
1995 int i;
1996
1997 for (i = 0; (i < rxq->refill_num) && (i < 64); i++) {
1998 rx_desc = rxq->descs + curr_desc;
1999 if (!(rx_desc->buf_phys_addr)) {
2000 if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) {
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01002001 struct mvneta_pcpu_stats *stats;
2002
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002003 pr_err("Can't refill queue %d. Done %d from %d\n",
2004 rxq->id, i, rxq->refill_num);
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01002005
2006 stats = this_cpu_ptr(pp->stats);
2007 u64_stats_update_begin(&stats->syncp);
2008 stats->es.refill_error++;
2009 u64_stats_update_end(&stats->syncp);
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002010 break;
2011 }
2012 }
2013 curr_desc = MVNETA_QUEUE_NEXT_DESC(rxq, curr_desc);
2014 }
2015 rxq->refill_num -= i;
2016 rxq->first_to_refill = curr_desc;
2017
2018 return i;
2019}
2020
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002021static int
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002022mvneta_xdp_submit_frame(struct mvneta_port *pp, struct mvneta_tx_queue *txq,
2023 struct xdp_frame *xdpf, bool dma_map)
2024{
2025 struct mvneta_tx_desc *tx_desc;
2026 struct mvneta_tx_buf *buf;
2027 dma_addr_t dma_addr;
2028
2029 if (txq->count >= txq->tx_stop_threshold)
2030 return MVNETA_XDP_DROPPED;
2031
2032 tx_desc = mvneta_txq_next_desc_get(txq);
2033
2034 buf = &txq->buf[txq->txq_put_index];
2035 if (dma_map) {
2036 /* ndo_xdp_xmit */
2037 dma_addr = dma_map_single(pp->dev->dev.parent, xdpf->data,
2038 xdpf->len, DMA_TO_DEVICE);
2039 if (dma_mapping_error(pp->dev->dev.parent, dma_addr)) {
2040 mvneta_txq_desc_put(txq);
2041 return MVNETA_XDP_DROPPED;
2042 }
2043 buf->type = MVNETA_TYPE_XDP_NDO;
2044 } else {
2045 struct page *page = virt_to_page(xdpf->data);
2046
2047 dma_addr = page_pool_get_dma_addr(page) +
2048 sizeof(*xdpf) + xdpf->headroom;
2049 dma_sync_single_for_device(pp->dev->dev.parent, dma_addr,
2050 xdpf->len, DMA_BIDIRECTIONAL);
2051 buf->type = MVNETA_TYPE_XDP_TX;
2052 }
2053 buf->xdpf = xdpf;
2054
2055 tx_desc->command = MVNETA_TXD_FLZ_DESC;
2056 tx_desc->buf_phys_addr = dma_addr;
2057 tx_desc->data_size = xdpf->len;
2058
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002059 mvneta_txq_inc_put(txq);
2060 txq->pending++;
2061 txq->count++;
2062
2063 return MVNETA_XDP_TX;
2064}
2065
2066static int
2067mvneta_xdp_xmit_back(struct mvneta_port *pp, struct xdp_buff *xdp)
2068{
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01002069 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002070 struct mvneta_tx_queue *txq;
2071 struct netdev_queue *nq;
2072 struct xdp_frame *xdpf;
2073 int cpu;
2074 u32 ret;
2075
Lorenzo Bianconi1b698fa2020-05-28 22:47:29 +02002076 xdpf = xdp_convert_buff_to_frame(xdp);
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002077 if (unlikely(!xdpf))
2078 return MVNETA_XDP_DROPPED;
2079
2080 cpu = smp_processor_id();
2081 txq = &pp->txqs[cpu % txq_number];
2082 nq = netdev_get_tx_queue(pp->dev, txq->id);
2083
2084 __netif_tx_lock(nq, cpu);
2085 ret = mvneta_xdp_submit_frame(pp, txq, xdpf, false);
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01002086 if (ret == MVNETA_XDP_TX) {
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01002087 u64_stats_update_begin(&stats->syncp);
2088 stats->es.ps.tx_bytes += xdpf->len;
2089 stats->es.ps.tx_packets++;
2090 stats->es.ps.xdp_tx++;
2091 u64_stats_update_end(&stats->syncp);
2092
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002093 mvneta_txq_pend_desc_add(pp, txq, 0);
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01002094 } else {
2095 u64_stats_update_begin(&stats->syncp);
2096 stats->es.ps.xdp_tx_err++;
2097 u64_stats_update_end(&stats->syncp);
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01002098 }
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002099 __netif_tx_unlock(nq);
2100
2101 return ret;
2102}
2103
2104static int
2105mvneta_xdp_xmit(struct net_device *dev, int num_frame,
2106 struct xdp_frame **frames, u32 flags)
2107{
2108 struct mvneta_port *pp = netdev_priv(dev);
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01002109 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2110 int i, nxmit_byte = 0, nxmit = num_frame;
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002111 int cpu = smp_processor_id();
2112 struct mvneta_tx_queue *txq;
2113 struct netdev_queue *nq;
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002114 u32 ret;
2115
2116 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2117 return -EINVAL;
2118
2119 txq = &pp->txqs[cpu % txq_number];
2120 nq = netdev_get_tx_queue(pp->dev, txq->id);
2121
2122 __netif_tx_lock(nq, cpu);
2123 for (i = 0; i < num_frame; i++) {
2124 ret = mvneta_xdp_submit_frame(pp, txq, frames[i], true);
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01002125 if (ret == MVNETA_XDP_TX) {
2126 nxmit_byte += frames[i]->len;
2127 } else {
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002128 xdp_return_frame_rx_napi(frames[i]);
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01002129 nxmit--;
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002130 }
2131 }
2132
2133 if (unlikely(flags & XDP_XMIT_FLUSH))
2134 mvneta_txq_pend_desc_add(pp, txq, 0);
2135 __netif_tx_unlock(nq);
2136
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01002137 u64_stats_update_begin(&stats->syncp);
2138 stats->es.ps.tx_bytes += nxmit_byte;
2139 stats->es.ps.tx_packets += nxmit;
2140 stats->es.ps.xdp_xmit += nxmit;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01002141 stats->es.ps.xdp_xmit_err += num_frame - nxmit;
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01002142 u64_stats_update_end(&stats->syncp);
2143
2144 return nxmit;
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002145}
2146
2147static int
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002148mvneta_run_xdp(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002149 struct bpf_prog *prog, struct xdp_buff *xdp,
2150 struct mvneta_stats *stats)
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002151{
Jesper Dangaard Brouer494f44d52020-05-14 12:49:17 +02002152 unsigned int len, sync;
2153 struct page *page;
Lorenzo Bianconi8c4df832020-01-14 11:21:16 +01002154 u32 ret, act;
2155
2156 len = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2157 act = bpf_prog_run_xdp(prog, xdp);
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002158
Jesper Dangaard Brouer494f44d52020-05-14 12:49:17 +02002159 /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
2160 sync = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2161 sync = max(sync, len);
2162
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002163 switch (act) {
2164 case XDP_PASS:
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01002165 stats->xdp_pass++;
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002166 return MVNETA_XDP_PASS;
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002167 case XDP_REDIRECT: {
2168 int err;
2169
2170 err = xdp_do_redirect(pp->dev, xdp, prog);
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01002171 if (unlikely(err)) {
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002172 ret = MVNETA_XDP_DROPPED;
Jesper Dangaard Brouer494f44d52020-05-14 12:49:17 +02002173 page = virt_to_head_page(xdp->data);
2174 page_pool_put_page(rxq->page_pool, page, sync, true);
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002175 } else {
2176 ret = MVNETA_XDP_REDIR;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01002177 stats->xdp_redirect++;
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002178 }
2179 break;
2180 }
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002181 case XDP_TX:
2182 ret = mvneta_xdp_xmit_back(pp, xdp);
Jesper Dangaard Brouer494f44d52020-05-14 12:49:17 +02002183 if (ret != MVNETA_XDP_TX) {
2184 page = virt_to_head_page(xdp->data);
2185 page_pool_put_page(rxq->page_pool, page, sync, true);
2186 }
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02002187 break;
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002188 default:
2189 bpf_warn_invalid_xdp_action(act);
2190 /* fall through */
2191 case XDP_ABORTED:
2192 trace_xdp_exception(pp->dev, prog, act);
2193 /* fall through */
2194 case XDP_DROP:
Jesper Dangaard Brouer494f44d52020-05-14 12:49:17 +02002195 page = virt_to_head_page(xdp->data);
2196 page_pool_put_page(rxq->page_pool, page, sync, true);
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002197 ret = MVNETA_XDP_DROPPED;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01002198 stats->xdp_drop++;
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002199 break;
2200 }
2201
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002202 stats->rx_bytes += xdp->data_end - xdp->data;
2203 stats->rx_packets++;
2204
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002205 return ret;
2206}
2207
2208static int
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002209mvneta_swbm_rx_frame(struct mvneta_port *pp,
2210 struct mvneta_rx_desc *rx_desc,
2211 struct mvneta_rx_queue *rxq,
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002212 struct xdp_buff *xdp,
2213 struct bpf_prog *xdp_prog,
Lorenzo Bianconi6c8a8cf2020-02-16 22:07:33 +01002214 struct page *page,
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002215 struct mvneta_stats *stats)
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002216{
2217 unsigned char *data = page_address(page);
2218 int data_len = -MVNETA_MH_SIZE, len;
2219 struct net_device *dev = pp->dev;
2220 enum dma_data_direction dma_dir;
Lorenzo Bianconi6c8a8cf2020-02-16 22:07:33 +01002221 int ret = 0;
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002222
2223 if (MVNETA_SKB_SIZE(rx_desc->data_size) > PAGE_SIZE) {
2224 len = MVNETA_MAX_RX_BUF_SIZE;
2225 data_len += len;
2226 } else {
2227 len = rx_desc->data_size;
2228 data_len += len - ETH_FCS_LEN;
2229 }
2230
2231 dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2232 dma_sync_single_for_cpu(dev->dev.parent,
2233 rx_desc->buf_phys_addr,
2234 len, dma_dir);
2235
Lorenzo Bianconifa383f62019-10-19 10:13:25 +02002236 /* Prefetch header */
2237 prefetch(data);
2238
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002239 xdp->data_hard_start = data;
Lorenzo Bianconib37fa92e2019-11-14 01:25:55 +02002240 xdp->data = data + pp->rx_offset_correction + MVNETA_MH_SIZE;
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002241 xdp->data_end = xdp->data + data_len;
2242 xdp_set_data_meta_invalid(xdp);
2243
2244 if (xdp_prog) {
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002245 ret = mvneta_run_xdp(pp, rxq, xdp_prog, xdp, stats);
Lorenzo Bianconi6c8a8cf2020-02-16 22:07:33 +01002246 if (ret)
2247 goto out;
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002248 }
2249
2250 rxq->skb = build_skb(xdp->data_hard_start, PAGE_SIZE);
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002251 if (unlikely(!rxq->skb)) {
Lorenzo Bianconic35947b2020-02-06 10:14:39 +01002252 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2253
2254 netdev_err(dev, "Can't allocate skb on queue %d\n", rxq->id);
Lorenzo Bianconic35947b2020-02-06 10:14:39 +01002255
2256 u64_stats_update_begin(&stats->syncp);
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01002257 stats->es.skb_alloc_error++;
Lorenzo Bianconic35947b2020-02-06 10:14:39 +01002258 stats->rx_dropped++;
2259 u64_stats_update_end(&stats->syncp);
2260
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002261 return -ENOMEM;
2262 }
2263 page_pool_release_page(rxq->page_pool, page);
2264
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002265 skb_reserve(rxq->skb,
2266 xdp->data - xdp->data_hard_start);
2267 skb_put(rxq->skb, xdp->data_end - xdp->data);
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002268 mvneta_rx_csum(pp, rx_desc->status, rxq->skb);
2269
2270 rxq->left_size = rx_desc->data_size - len;
Lorenzo Bianconi6c8a8cf2020-02-16 22:07:33 +01002271
2272out:
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002273 rx_desc->buf_phys_addr = 0;
2274
Lorenzo Bianconi6c8a8cf2020-02-16 22:07:33 +01002275 return ret;
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002276}
2277
2278static void
2279mvneta_swbm_add_rx_fragment(struct mvneta_port *pp,
2280 struct mvneta_rx_desc *rx_desc,
2281 struct mvneta_rx_queue *rxq,
2282 struct page *page)
2283{
2284 struct net_device *dev = pp->dev;
2285 enum dma_data_direction dma_dir;
2286 int data_len, len;
2287
2288 if (rxq->left_size > MVNETA_MAX_RX_BUF_SIZE) {
2289 len = MVNETA_MAX_RX_BUF_SIZE;
2290 data_len = len;
2291 } else {
2292 len = rxq->left_size;
2293 data_len = len - ETH_FCS_LEN;
2294 }
2295 dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2296 dma_sync_single_for_cpu(dev->dev.parent,
2297 rx_desc->buf_phys_addr,
2298 len, dma_dir);
2299 if (data_len > 0) {
2300 /* refill descriptor with new buffer later */
2301 skb_add_rx_frag(rxq->skb,
2302 skb_shinfo(rxq->skb)->nr_frags,
Lorenzo Bianconib37fa92e2019-11-14 01:25:55 +02002303 page, pp->rx_offset_correction, data_len,
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002304 PAGE_SIZE);
2305 }
2306 page_pool_release_page(rxq->page_pool, page);
2307 rx_desc->buf_phys_addr = 0;
2308 rxq->left_size -= len;
2309}
2310
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002311/* Main rx processing when using software buffer management */
Andrew Lunn7a86f052018-07-18 18:10:50 +02002312static int mvneta_rx_swbm(struct napi_struct *napi,
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002313 struct mvneta_port *pp, int budget,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002314 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002315{
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002316 int rx_proc = 0, rx_todo, refill;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002317 struct net_device *dev = pp->dev;
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002318 struct mvneta_stats ps = {};
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002319 struct bpf_prog *xdp_prog;
2320 struct xdp_buff xdp_buf;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002321
2322 /* Get number of received packets */
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002323 rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002324
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002325 rcu_read_lock();
2326 xdp_prog = READ_ONCE(pp->xdp_prog);
2327 xdp_buf.rxq = &rxq->xdp_rxq;
Jesper Dangaard Brouer494f44d52020-05-14 12:49:17 +02002328 xdp_buf.frame_sz = PAGE_SIZE;
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002329
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002330 /* Fairness NAPI loop */
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002331 while (rx_proc < budget && rx_proc < rx_todo) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002332 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002333 u32 rx_status, index;
Gregory CLEMENT7e47fd82018-07-18 18:10:55 +02002334 struct page *page;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002335
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002336 index = rx_desc - rxq->descs;
Gregory CLEMENT7e47fd82018-07-18 18:10:55 +02002337 page = (struct page *)rxq->buf_virt_addr[index];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002338
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002339 rx_status = rx_desc->status;
2340 rx_proc++;
2341 rxq->refill_num++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002342
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002343 if (rx_status & MVNETA_RXD_FIRST_DESC) {
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002344 int err;
2345
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002346 /* Check errors only for FIRST descriptor */
2347 if (rx_status & MVNETA_RXD_ERR_SUMMARY) {
2348 mvneta_rx_error(pp, rx_desc);
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002349 /* leave the descriptor untouched */
2350 continue;
2351 }
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002352
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002353 err = mvneta_swbm_rx_frame(pp, rx_desc, rxq, &xdp_buf,
Lorenzo Bianconi6c8a8cf2020-02-16 22:07:33 +01002354 xdp_prog, page, &ps);
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002355 if (err)
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002356 continue;
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002357 } else {
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002358 if (unlikely(!rxq->skb)) {
2359 pr_debug("no skb for rx_status 0x%x\n",
2360 rx_status);
2361 continue;
2362 }
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02002363 mvneta_swbm_add_rx_fragment(pp, rx_desc, rxq, page);
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002364 } /* Middle or Last descriptor */
2365
2366 if (!(rx_status & MVNETA_RXD_LAST_DESC))
2367 /* no last descriptor this time */
2368 continue;
2369
2370 if (rxq->left_size) {
2371 pr_err("get last desc, but left_size (%d) != 0\n",
2372 rxq->left_size);
2373 dev_kfree_skb_any(rxq->skb);
2374 rxq->left_size = 0;
2375 rxq->skb = NULL;
willy tarreauf19fadf2014-01-16 08:20:17 +01002376 continue;
2377 }
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002378
2379 ps.rx_bytes += rxq->skb->len;
2380 ps.rx_packets++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002381
2382 /* Linux processing */
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002383 rxq->skb->protocol = eth_type_trans(rxq->skb, dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002384
Jisheng Zhangd28118e2018-08-31 16:09:13 +08002385 napi_gro_receive(napi, rxq->skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002386
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002387 /* clean uncomplete skb pointer in queue */
2388 rxq->skb = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002389 }
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002390 rcu_read_unlock();
2391
Lorenzo Bianconi6c8a8cf2020-02-16 22:07:33 +01002392 if (ps.xdp_redirect)
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02002393 xdp_do_flush_map();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002394
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002395 if (ps.rx_packets)
2396 mvneta_update_stats(pp, &ps);
willy tarreaudc4277d2014-01-16 08:20:07 +01002397
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002398 /* return some buffers to hardware queue, one at a time is too slow */
2399 refill = mvneta_rx_refill_queue(pp, rxq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002400
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02002401 /* Update rxq management counters */
2402 mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill);
2403
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002404 return ps.rx_packets;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002405}
2406
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002407/* Main rx processing when using hardware buffer management */
Andrew Lunn7a86f052018-07-18 18:10:50 +02002408static int mvneta_rx_hwbm(struct napi_struct *napi,
2409 struct mvneta_port *pp, int rx_todo,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002410 struct mvneta_rx_queue *rxq)
2411{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002412 struct net_device *dev = pp->dev;
2413 int rx_done;
2414 u32 rcvd_pkts = 0;
2415 u32 rcvd_bytes = 0;
2416
2417 /* Get number of received packets */
2418 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2419
2420 if (rx_todo > rx_done)
2421 rx_todo = rx_done;
2422
2423 rx_done = 0;
2424
2425 /* Fairness NAPI loop */
2426 while (rx_done < rx_todo) {
2427 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2428 struct mvneta_bm_pool *bm_pool = NULL;
2429 struct sk_buff *skb;
2430 unsigned char *data;
2431 dma_addr_t phys_addr;
2432 u32 rx_status, frag_size;
2433 int rx_bytes, err;
2434 u8 pool_id;
2435
2436 rx_done++;
2437 rx_status = rx_desc->status;
2438 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002439 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002440 phys_addr = rx_desc->buf_phys_addr;
2441 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2442 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2443
2444 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2445 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2446err_drop_frame_ret_pool:
2447 /* Return the buffer to the pool */
2448 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2449 rx_desc->buf_phys_addr);
2450err_drop_frame:
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002451 mvneta_rx_error(pp, rx_desc);
2452 /* leave the descriptor untouched */
2453 continue;
2454 }
2455
2456 if (rx_bytes <= rx_copybreak) {
2457 /* better copy a small frame and not unmap the DMA region */
2458 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2459 if (unlikely(!skb))
2460 goto err_drop_frame_ret_pool;
2461
Russell Kinga8fef9b2019-02-15 13:55:47 +00002462 dma_sync_single_range_for_cpu(&pp->bm_priv->pdev->dev,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002463 rx_desc->buf_phys_addr,
2464 MVNETA_MH_SIZE + NET_SKB_PAD,
2465 rx_bytes,
2466 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02002467 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2468 rx_bytes);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002469
2470 skb->protocol = eth_type_trans(skb, dev);
2471 mvneta_rx_csum(pp, rx_status, skb);
Andrew Lunn7a86f052018-07-18 18:10:50 +02002472 napi_gro_receive(napi, skb);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002473
2474 rcvd_pkts++;
2475 rcvd_bytes += rx_bytes;
2476
2477 /* Return the buffer to the pool */
2478 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2479 rx_desc->buf_phys_addr);
2480
2481 /* leave the descriptor and buffer untouched */
2482 continue;
2483 }
2484
2485 /* Refill processing */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002486 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002487 if (err) {
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01002488 struct mvneta_pcpu_stats *stats;
2489
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002490 netdev_err(dev, "Linux processing - Can't refill\n");
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01002491
2492 stats = this_cpu_ptr(pp->stats);
2493 u64_stats_update_begin(&stats->syncp);
2494 stats->es.refill_error++;
2495 u64_stats_update_end(&stats->syncp);
2496
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002497 goto err_drop_frame_ret_pool;
2498 }
2499
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002500 frag_size = bm_pool->hwbm_pool.frag_size;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002501
2502 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2503
2504 /* After refill old buffer has to be unmapped regardless
2505 * the skb is successfully built or not.
2506 */
2507 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2508 bm_pool->buf_size, DMA_FROM_DEVICE);
2509 if (!skb)
2510 goto err_drop_frame;
2511
2512 rcvd_pkts++;
2513 rcvd_bytes += rx_bytes;
2514
2515 /* Linux processing */
2516 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2517 skb_put(skb, rx_bytes);
2518
2519 skb->protocol = eth_type_trans(skb, dev);
2520
2521 mvneta_rx_csum(pp, rx_status, skb);
2522
Andrew Lunn7a86f052018-07-18 18:10:50 +02002523 napi_gro_receive(napi, skb);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002524 }
2525
Lorenzo Bianconi69de66f2020-02-16 22:07:30 +01002526 if (rcvd_pkts) {
2527 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2528
2529 u64_stats_update_begin(&stats->syncp);
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002530 stats->es.ps.rx_packets += rcvd_pkts;
2531 stats->es.ps.rx_bytes += rcvd_bytes;
Lorenzo Bianconi69de66f2020-02-16 22:07:30 +01002532 u64_stats_update_end(&stats->syncp);
2533 }
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002534
2535 /* Update rxq management counters */
2536 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2537
2538 return rx_done;
2539}
2540
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002541static inline void
2542mvneta_tso_put_hdr(struct sk_buff *skb,
2543 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2544{
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002545 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002546 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2547 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002548
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002549 tx_desc = mvneta_txq_next_desc_get(txq);
2550 tx_desc->data_size = hdr_len;
2551 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2552 tx_desc->command |= MVNETA_TXD_F_DESC;
2553 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2554 txq->txq_put_index * TSO_HEADER_SIZE;
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002555 buf->type = MVNETA_TYPE_SKB;
2556 buf->skb = NULL;
2557
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002558 mvneta_txq_inc_put(txq);
2559}
2560
2561static inline int
2562mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2563 struct sk_buff *skb, char *data, int size,
2564 bool last_tcp, bool is_last)
2565{
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002566 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002567 struct mvneta_tx_desc *tx_desc;
2568
2569 tx_desc = mvneta_txq_next_desc_get(txq);
2570 tx_desc->data_size = size;
2571 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2572 size, DMA_TO_DEVICE);
2573 if (unlikely(dma_mapping_error(dev->dev.parent,
2574 tx_desc->buf_phys_addr))) {
2575 mvneta_txq_desc_put(txq);
2576 return -ENOMEM;
2577 }
2578
2579 tx_desc->command = 0;
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002580 buf->type = MVNETA_TYPE_SKB;
2581 buf->skb = NULL;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002582
2583 if (last_tcp) {
2584 /* last descriptor in the TCP packet */
2585 tx_desc->command = MVNETA_TXD_L_DESC;
2586
2587 /* last descriptor in SKB */
2588 if (is_last)
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002589 buf->skb = skb;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002590 }
2591 mvneta_txq_inc_put(txq);
2592 return 0;
2593}
2594
2595static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2596 struct mvneta_tx_queue *txq)
2597{
2598 int total_len, data_left;
2599 int desc_count = 0;
2600 struct mvneta_port *pp = netdev_priv(dev);
2601 struct tso_t tso;
2602 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2603 int i;
2604
2605 /* Count needed descriptors */
2606 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2607 return 0;
2608
2609 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2610 pr_info("*** Is this even possible???!?!?\n");
2611 return 0;
2612 }
2613
2614 /* Initialize the TSO handler, and prepare the first payload */
2615 tso_start(skb, &tso);
2616
2617 total_len = skb->len - hdr_len;
2618 while (total_len > 0) {
2619 char *hdr;
2620
2621 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2622 total_len -= data_left;
2623 desc_count++;
2624
2625 /* prepare packet headers: MAC + IP + TCP */
2626 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2627 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2628
2629 mvneta_tso_put_hdr(skb, pp, txq);
2630
2631 while (data_left > 0) {
2632 int size;
2633 desc_count++;
2634
2635 size = min_t(int, tso.size, data_left);
2636
2637 if (mvneta_tso_put_data(dev, txq, skb,
2638 tso.data, size,
2639 size == data_left,
2640 total_len == 0))
2641 goto err_release;
2642 data_left -= size;
2643
2644 tso_build_data(skb, &tso, size);
2645 }
2646 }
2647
2648 return desc_count;
2649
2650err_release:
2651 /* Release all used data descriptors; header descriptors must not
2652 * be DMA-unmapped.
2653 */
2654 for (i = desc_count - 1; i >= 0; i--) {
2655 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002656 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002657 dma_unmap_single(pp->dev->dev.parent,
2658 tx_desc->buf_phys_addr,
2659 tx_desc->data_size,
2660 DMA_TO_DEVICE);
2661 mvneta_txq_desc_put(txq);
2662 }
2663 return 0;
2664}
2665
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002666/* Handle tx fragmentation processing */
2667static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2668 struct mvneta_tx_queue *txq)
2669{
2670 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002671 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002672
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002673 for (i = 0; i < nr_frags; i++) {
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002674 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002675 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
Matthew Wilcox (Oracle)d7840972019-07-22 20:08:25 -07002676 void *addr = skb_frag_address(frag);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002677
2678 tx_desc = mvneta_txq_next_desc_get(txq);
Matthew Wilcox (Oracle)d7840972019-07-22 20:08:25 -07002679 tx_desc->data_size = skb_frag_size(frag);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002680
2681 tx_desc->buf_phys_addr =
2682 dma_map_single(pp->dev->dev.parent, addr,
2683 tx_desc->data_size, DMA_TO_DEVICE);
2684
2685 if (dma_mapping_error(pp->dev->dev.parent,
2686 tx_desc->buf_phys_addr)) {
2687 mvneta_txq_desc_put(txq);
2688 goto error;
2689 }
2690
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002691 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002692 /* Last descriptor */
2693 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002694 buf->skb = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002695 } else {
2696 /* Descriptor in the middle: Not First, Not Last */
2697 tx_desc->command = 0;
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002698 buf->skb = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002699 }
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002700 buf->type = MVNETA_TYPE_SKB;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002701 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002702 }
2703
2704 return 0;
2705
2706error:
2707 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002708 * this packet, as well as the corresponding DMA mappings
2709 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002710 for (i = i - 1; i >= 0; i--) {
2711 tx_desc = txq->descs + i;
2712 dma_unmap_single(pp->dev->dev.parent,
2713 tx_desc->buf_phys_addr,
2714 tx_desc->data_size,
2715 DMA_TO_DEVICE);
2716 mvneta_txq_desc_put(txq);
2717 }
2718
2719 return -ENOMEM;
2720}
2721
2722/* Main tx processing */
YueHaibingf03508c2018-09-19 18:19:26 +08002723static netdev_tx_t mvneta_tx(struct sk_buff *skb, struct net_device *dev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002724{
2725 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002726 u16 txq_id = skb_get_queue_mapping(skb);
2727 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002728 struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002729 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002730 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002731 int frags = 0;
2732 u32 tx_cmd;
2733
2734 if (!netif_running(dev))
2735 goto out;
2736
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002737 if (skb_is_gso(skb)) {
2738 frags = mvneta_tx_tso(skb, dev, txq);
2739 goto out;
2740 }
2741
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002742 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002743
2744 /* Get a descriptor for the first part of the packet */
2745 tx_desc = mvneta_txq_next_desc_get(txq);
2746
2747 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2748
2749 tx_desc->data_size = skb_headlen(skb);
2750
2751 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2752 tx_desc->data_size,
2753 DMA_TO_DEVICE);
2754 if (unlikely(dma_mapping_error(dev->dev.parent,
2755 tx_desc->buf_phys_addr))) {
2756 mvneta_txq_desc_put(txq);
2757 frags = 0;
2758 goto out;
2759 }
2760
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002761 buf->type = MVNETA_TYPE_SKB;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002762 if (frags == 1) {
2763 /* First and Last descriptor */
2764 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2765 tx_desc->command = tx_cmd;
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002766 buf->skb = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002767 mvneta_txq_inc_put(txq);
2768 } else {
2769 /* First but not Last */
2770 tx_cmd |= MVNETA_TXD_F_DESC;
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02002771 buf->skb = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002772 mvneta_txq_inc_put(txq);
2773 tx_desc->command = tx_cmd;
2774 /* Continue with other skb fragments */
2775 if (mvneta_tx_frag_process(pp, skb, txq)) {
2776 dma_unmap_single(dev->dev.parent,
2777 tx_desc->buf_phys_addr,
2778 tx_desc->data_size,
2779 DMA_TO_DEVICE);
2780 mvneta_txq_desc_put(txq);
2781 frags = 0;
2782 goto out;
2783 }
2784 }
2785
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002786out:
2787 if (frags > 0) {
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002788 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
Lorenzo Bianconi69de66f2020-02-16 22:07:30 +01002789 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002790
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002791 netdev_tx_sent_queue(nq, len);
2792
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002793 txq->count += frags;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002794 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002795 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002796
Florian Westphal6b16f9e2019-04-01 16:42:14 +02002797 if (!netdev_xmit_more() || netif_xmit_stopped(nq) ||
Simon Guinot2a90f7e2017-01-16 18:08:31 +01002798 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2799 mvneta_txq_pend_desc_add(pp, txq, frags);
2800 else
2801 txq->pending += frags;
2802
Lorenzo Bianconi69de66f2020-02-16 22:07:30 +01002803 u64_stats_update_begin(&stats->syncp);
Lorenzo Bianconi320d5442020-02-16 22:07:31 +01002804 stats->es.ps.tx_bytes += len;
2805 stats->es.ps.tx_packets++;
Lorenzo Bianconi69de66f2020-02-16 22:07:30 +01002806 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002807 } else {
2808 dev->stats.tx_dropped++;
2809 dev_kfree_skb_any(skb);
2810 }
2811
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002812 return NETDEV_TX_OK;
2813}
2814
2815
2816/* Free tx resources, when resetting a port */
2817static void mvneta_txq_done_force(struct mvneta_port *pp,
2818 struct mvneta_tx_queue *txq)
2819
2820{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002821 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002822 int tx_done = txq->count;
2823
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002824 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002825
2826 /* reset txq */
2827 txq->count = 0;
2828 txq->txq_put_index = 0;
2829 txq->txq_get_index = 0;
2830}
2831
willy tarreau6c498972014-01-16 08:20:12 +01002832/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2833 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2834 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002835static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002836{
2837 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002838 struct netdev_queue *nq;
Jisheng Zhangbd9f1ee2018-08-31 16:11:09 +08002839 int cpu = smp_processor_id();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002840
willy tarreau6c498972014-01-16 08:20:12 +01002841 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002842 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002843
2844 nq = netdev_get_tx_queue(pp->dev, txq->id);
Jisheng Zhangbd9f1ee2018-08-31 16:11:09 +08002845 __netif_tx_lock(nq, cpu);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002846
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002847 if (txq->count)
2848 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002849
2850 __netif_tx_unlock(nq);
2851 cause_tx_done &= ~((1 << txq->id));
2852 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002853}
2854
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002855/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002856 * according to hw spec, different than generic crc8 algorithm
2857 */
2858static int mvneta_addr_crc(unsigned char *addr)
2859{
2860 int crc = 0;
2861 int i;
2862
2863 for (i = 0; i < ETH_ALEN; i++) {
2864 int j;
2865
2866 crc = (crc ^ addr[i]) << 8;
2867 for (j = 7; j >= 0; j--) {
2868 if (crc & (0x100 << j))
2869 crc ^= 0x107 << j;
2870 }
2871 }
2872
2873 return crc;
2874}
2875
2876/* This method controls the net device special MAC multicast support.
2877 * The Special Multicast Table for MAC addresses supports MAC of the form
2878 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2879 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2880 * Table entries in the DA-Filter table. This method set the Special
2881 * Multicast Table appropriate entry.
2882 */
2883static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2884 unsigned char last_byte,
2885 int queue)
2886{
2887 unsigned int smc_table_reg;
2888 unsigned int tbl_offset;
2889 unsigned int reg_offset;
2890
2891 /* Register offset from SMC table base */
2892 tbl_offset = (last_byte / 4);
2893 /* Entry offset within the above reg */
2894 reg_offset = last_byte % 4;
2895
2896 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2897 + tbl_offset * 4));
2898
2899 if (queue == -1)
2900 smc_table_reg &= ~(0xff << (8 * reg_offset));
2901 else {
2902 smc_table_reg &= ~(0xff << (8 * reg_offset));
2903 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2904 }
2905
2906 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2907 smc_table_reg);
2908}
2909
2910/* This method controls the network device Other MAC multicast support.
2911 * The Other Multicast Table is used for multicast of another type.
2912 * A CRC-8 is used as an index to the Other Multicast Table entries
2913 * in the DA-Filter table.
2914 * The method gets the CRC-8 value from the calling routine and
2915 * sets the Other Multicast Table appropriate entry according to the
2916 * specified CRC-8 .
2917 */
2918static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2919 unsigned char crc8,
2920 int queue)
2921{
2922 unsigned int omc_table_reg;
2923 unsigned int tbl_offset;
2924 unsigned int reg_offset;
2925
2926 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2927 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2928
2929 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2930
2931 if (queue == -1) {
2932 /* Clear accepts frame bit at specified Other DA table entry */
2933 omc_table_reg &= ~(0xff << (8 * reg_offset));
2934 } else {
2935 omc_table_reg &= ~(0xff << (8 * reg_offset));
2936 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2937 }
2938
2939 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2940}
2941
2942/* The network device supports multicast using two tables:
2943 * 1) Special Multicast Table for MAC addresses of the form
2944 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2945 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2946 * Table entries in the DA-Filter table.
2947 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2948 * is used as an index to the Other Multicast Table entries in the
2949 * DA-Filter table.
2950 */
2951static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2952 int queue)
2953{
2954 unsigned char crc_result = 0;
2955
2956 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2957 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2958 return 0;
2959 }
2960
2961 crc_result = mvneta_addr_crc(p_addr);
2962 if (queue == -1) {
2963 if (pp->mcast_count[crc_result] == 0) {
2964 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2965 crc_result);
2966 return -EINVAL;
2967 }
2968
2969 pp->mcast_count[crc_result]--;
2970 if (pp->mcast_count[crc_result] != 0) {
2971 netdev_info(pp->dev,
2972 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2973 pp->mcast_count[crc_result], crc_result);
2974 return -EINVAL;
2975 }
2976 } else
2977 pp->mcast_count[crc_result]++;
2978
2979 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2980
2981 return 0;
2982}
2983
2984/* Configure Fitering mode of Ethernet port */
2985static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2986 int is_promisc)
2987{
2988 u32 port_cfg_reg, val;
2989
2990 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2991
2992 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2993
2994 /* Set / Clear UPM bit in port configuration register */
2995 if (is_promisc) {
2996 /* Accept all Unicast addresses */
2997 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2998 val |= MVNETA_FORCE_UNI;
2999 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
3000 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
3001 } else {
3002 /* Reject all Unicast addresses */
3003 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
3004 val &= ~MVNETA_FORCE_UNI;
3005 }
3006
3007 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
3008 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
3009}
3010
3011/* register unicast and multicast addresses */
3012static void mvneta_set_rx_mode(struct net_device *dev)
3013{
3014 struct mvneta_port *pp = netdev_priv(dev);
3015 struct netdev_hw_addr *ha;
3016
3017 if (dev->flags & IFF_PROMISC) {
3018 /* Accept all: Multicast + Unicast */
3019 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003020 mvneta_set_ucast_table(pp, pp->rxq_def);
3021 mvneta_set_special_mcast_table(pp, pp->rxq_def);
3022 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003023 } else {
3024 /* Accept single Unicast */
3025 mvneta_rx_unicast_promisc_set(pp, 0);
3026 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003027 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003028
3029 if (dev->flags & IFF_ALLMULTI) {
3030 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003031 mvneta_set_special_mcast_table(pp, pp->rxq_def);
3032 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003033 } else {
3034 /* Accept only initialized multicast */
3035 mvneta_set_special_mcast_table(pp, -1);
3036 mvneta_set_other_mcast_table(pp, -1);
3037
3038 if (!netdev_mc_empty(dev)) {
3039 netdev_for_each_mc_addr(ha, dev) {
3040 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003041 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003042 }
3043 }
3044 }
3045 }
3046}
3047
3048/* Interrupt handling - the callback for request_irq() */
3049static irqreturn_t mvneta_isr(int irq, void *dev_id)
3050{
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003051 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
3052
3053 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
3054 napi_schedule(&pp->napi);
3055
3056 return IRQ_HANDLED;
3057}
3058
3059/* Interrupt handling - the callback for request_percpu_irq() */
3060static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
3061{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003062 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003063
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003064 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003065 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003066
3067 return IRQ_HANDLED;
3068}
3069
Russell King503f9aa92018-01-02 17:24:44 +00003070static void mvneta_link_change(struct mvneta_port *pp)
Stas Sergeev898b29702015-04-01 20:32:49 +03003071{
Stas Sergeev898b29702015-04-01 20:32:49 +03003072 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3073
Russell King503f9aa92018-01-02 17:24:44 +00003074 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
Stas Sergeev898b29702015-04-01 20:32:49 +03003075}
3076
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003077/* NAPI handler
3078 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
3079 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
3080 * Bits 8 -15 of the cause Rx Tx register indicate that are received
3081 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
3082 * Each CPU has its own causeRxTx register
3083 */
3084static int mvneta_poll(struct napi_struct *napi, int budget)
3085{
3086 int rx_done = 0;
3087 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003088 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003089 struct mvneta_port *pp = netdev_priv(napi->dev);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003090 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003091
3092 if (!netif_running(pp->dev)) {
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003093 napi_complete(napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003094 return rx_done;
3095 }
3096
3097 /* Read cause register */
Stas Sergeev898b29702015-04-01 20:32:49 +03003098 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
3099 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
3100 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
3101
3102 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
Russell King503f9aa92018-01-02 17:24:44 +00003103
3104 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
Russell King856b2cc2018-01-02 17:25:09 +00003105 MVNETA_CAUSE_LINK_CHANGE))
Russell King503f9aa92018-01-02 17:24:44 +00003106 mvneta_link_change(pp);
Stas Sergeev898b29702015-04-01 20:32:49 +03003107 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01003108
3109 /* Release Tx descriptors */
3110 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01003111 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01003112 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
3113 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003114
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003115 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003116 * RX packets
3117 */
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003118 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
3119 port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003120
Jisheng Zhang065fd832020-03-16 22:56:36 +08003121 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003122 if (rx_queue) {
3123 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003124 if (pp->bm_priv)
Andrew Lunn7a86f052018-07-18 18:10:50 +02003125 rx_done = mvneta_rx_hwbm(napi, pp, budget,
3126 &pp->rxqs[rx_queue]);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003127 else
Andrew Lunn7a86f052018-07-18 18:10:50 +02003128 rx_done = mvneta_rx_swbm(napi, pp, budget,
3129 &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003130 }
3131
Eric Dumazet6ad20162017-01-30 08:22:01 -08003132 if (rx_done < budget) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003133 cause_rx_tx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08003134 napi_complete_done(napi, rx_done);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003135
3136 if (pp->neta_armada3700) {
3137 unsigned long flags;
3138
3139 local_irq_save(flags);
3140 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
3141 MVNETA_RX_INTR_MASK(rxq_number) |
3142 MVNETA_TX_INTR_MASK(txq_number) |
3143 MVNETA_MISCINTR_INTR_MASK);
3144 local_irq_restore(flags);
3145 } else {
3146 enable_percpu_irq(pp->dev->irq, 0);
3147 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003148 }
3149
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003150 if (pp->neta_armada3700)
3151 pp->cause_rx_tx = cause_rx_tx;
3152 else
3153 port->cause_rx_tx = cause_rx_tx;
3154
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003155 return rx_done;
3156}
3157
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02003158static int mvneta_create_page_pool(struct mvneta_port *pp,
3159 struct mvneta_rx_queue *rxq, int size)
3160{
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02003161 struct bpf_prog *xdp_prog = READ_ONCE(pp->xdp_prog);
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02003162 struct page_pool_params pp_params = {
3163 .order = 0,
Lorenzo Bianconi07e13ed2019-11-20 16:54:19 +02003164 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02003165 .pool_size = size,
Lorenzo Bianconi1657adc2020-01-13 10:28:12 +01003166 .nid = NUMA_NO_NODE,
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02003167 .dev = pp->dev->dev.parent,
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02003168 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
Lorenzo Bianconi07e13ed2019-11-20 16:54:19 +02003169 .offset = pp->rx_offset_correction,
3170 .max_len = MVNETA_MAX_RX_BUF_SIZE,
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02003171 };
3172 int err;
3173
3174 rxq->page_pool = page_pool_create(&pp_params);
3175 if (IS_ERR(rxq->page_pool)) {
3176 err = PTR_ERR(rxq->page_pool);
3177 rxq->page_pool = NULL;
3178 return err;
3179 }
3180
3181 err = xdp_rxq_info_reg(&rxq->xdp_rxq, pp->dev, rxq->id);
3182 if (err < 0)
3183 goto err_free_pp;
3184
3185 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
3186 rxq->page_pool);
3187 if (err)
3188 goto err_unregister_rxq;
3189
3190 return 0;
3191
3192err_unregister_rxq:
3193 xdp_rxq_info_unreg(&rxq->xdp_rxq);
3194err_free_pp:
3195 page_pool_destroy(rxq->page_pool);
3196 rxq->page_pool = NULL;
3197 return err;
3198}
3199
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003200/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
3201static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
3202 int num)
3203{
Lorenzo Bianconi568a3fa2019-10-19 10:13:22 +02003204 int i, err;
3205
3206 err = mvneta_create_page_pool(pp, rxq, num);
3207 if (err < 0)
3208 return err;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003209
3210 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01003211 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
Gregory CLEMENT7e47fd82018-07-18 18:10:55 +02003212 if (mvneta_rx_refill(pp, rxq->descs + i, rxq,
3213 GFP_KERNEL) != 0) {
3214 netdev_err(pp->dev,
3215 "%s:rxq %d, %d of %d buffs filled\n",
3216 __func__, rxq->id, i, num);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003217 break;
3218 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003219 }
3220
3221 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003222 * get packets)
3223 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003224 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
3225
3226 return i;
3227}
3228
3229/* Free all packets pending transmit from all TXQs and reset TX port */
3230static void mvneta_tx_reset(struct mvneta_port *pp)
3231{
3232 int queue;
3233
Ezequiel Garcia96728502014-05-22 20:06:59 -03003234 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003235 for (queue = 0; queue < txq_number; queue++)
3236 mvneta_txq_done_force(pp, &pp->txqs[queue]);
3237
3238 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
3239 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
3240}
3241
3242static void mvneta_rx_reset(struct mvneta_port *pp)
3243{
3244 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
3245 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
3246}
3247
3248/* Rx/Tx queue initialization/cleanup methods */
3249
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003250static int mvneta_rxq_sw_init(struct mvneta_port *pp,
3251 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003252{
3253 rxq->size = pp->rx_ring_size;
3254
3255 /* Allocate memory for RX descriptors */
3256 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
3257 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
3258 &rxq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02003259 if (!rxq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003260 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003261
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003262 rxq->last_desc = rxq->size - 1;
3263
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003264 return 0;
3265}
3266
3267static void mvneta_rxq_hw_init(struct mvneta_port *pp,
3268 struct mvneta_rx_queue *rxq)
3269{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003270 /* Set Rx descriptors queue starting address */
3271 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
3272 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
3273
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003274 /* Set coalescing pkts and time */
3275 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3276 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3277
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003278 if (!pp->bm_priv) {
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02003279 /* Set Offset */
3280 mvneta_rxq_offset_set(pp, rxq, 0);
Marcin Wojtase735fd52018-12-11 13:56:49 +01003281 mvneta_rxq_buf_size_set(pp, rxq, PAGE_SIZE < SZ_64K ?
Lorenzo Bianconi8dc9a082019-10-19 10:13:23 +02003282 MVNETA_MAX_RX_BUF_SIZE :
Marcin Wojtase735fd52018-12-11 13:56:49 +01003283 MVNETA_RX_BUF_SIZE(pp->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003284 mvneta_rxq_bm_disable(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01003285 mvneta_rxq_fill(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003286 } else {
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02003287 /* Set Offset */
3288 mvneta_rxq_offset_set(pp, rxq,
3289 NET_SKB_PAD - pp->rx_offset_correction);
3290
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003291 mvneta_rxq_bm_enable(pp, rxq);
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02003292 /* Fill RXQ with buffers from RX pool */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003293 mvneta_rxq_long_pool_set(pp, rxq);
3294 mvneta_rxq_short_pool_set(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01003295 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003296 }
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003297}
3298
3299/* Create a specified RX queue */
3300static int mvneta_rxq_init(struct mvneta_port *pp,
3301 struct mvneta_rx_queue *rxq)
3302
3303{
3304 int ret;
3305
3306 ret = mvneta_rxq_sw_init(pp, rxq);
3307 if (ret < 0)
3308 return ret;
3309
3310 mvneta_rxq_hw_init(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003311
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003312 return 0;
3313}
3314
3315/* Cleanup Rx queue */
3316static void mvneta_rxq_deinit(struct mvneta_port *pp,
3317 struct mvneta_rx_queue *rxq)
3318{
3319 mvneta_rxq_drop_pkts(pp, rxq);
3320
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02003321 if (rxq->skb)
3322 dev_kfree_skb_any(rxq->skb);
3323
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003324 if (rxq->descs)
3325 dma_free_coherent(pp->dev->dev.parent,
3326 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
3327 rxq->descs,
3328 rxq->descs_phys);
3329
3330 rxq->descs = NULL;
3331 rxq->last_desc = 0;
3332 rxq->next_desc_to_proc = 0;
3333 rxq->descs_phys = 0;
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02003334 rxq->first_to_refill = 0;
3335 rxq->refill_num = 0;
3336 rxq->skb = NULL;
3337 rxq->left_size = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003338}
3339
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003340static int mvneta_txq_sw_init(struct mvneta_port *pp,
3341 struct mvneta_tx_queue *txq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003342{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003343 int cpu;
3344
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003345 txq->size = pp->tx_ring_size;
3346
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003347 /* A queue must always have room for at least one skb.
3348 * Therefore, stop the queue when the free entries reaches
3349 * the maximum number of descriptors per skb.
3350 */
3351 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
3352 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
3353
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003354 /* Allocate memory for TX descriptors */
3355 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
3356 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3357 &txq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02003358 if (!txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003359 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003360
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003361 txq->last_desc = txq->size - 1;
3362
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02003363 txq->buf = kmalloc_array(txq->size, sizeof(*txq->buf), GFP_KERNEL);
3364 if (!txq->buf) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003365 dma_free_coherent(pp->dev->dev.parent,
3366 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3367 txq->descs, txq->descs_phys);
3368 return -ENOMEM;
3369 }
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03003370
3371 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
3372 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
3373 txq->size * TSO_HEADER_SIZE,
3374 &txq->tso_hdrs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02003375 if (!txq->tso_hdrs) {
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02003376 kfree(txq->buf);
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03003377 dma_free_coherent(pp->dev->dev.parent,
3378 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3379 txq->descs, txq->descs_phys);
3380 return -ENOMEM;
3381 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003382
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003383 /* Setup XPS mapping */
3384 if (txq_number > 1)
3385 cpu = txq->id % num_present_cpus();
3386 else
3387 cpu = pp->rxq_def % num_present_cpus();
3388 cpumask_set_cpu(cpu, &txq->affinity_mask);
3389 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
3390
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003391 return 0;
3392}
3393
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003394static void mvneta_txq_hw_init(struct mvneta_port *pp,
3395 struct mvneta_tx_queue *txq)
3396{
3397 /* Set maximum bandwidth for enabled TXQs */
3398 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
3399 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
3400
3401 /* Set Tx descriptors queue starting address */
3402 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
3403 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
3404
3405 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3406}
3407
3408/* Create and initialize a tx queue */
3409static int mvneta_txq_init(struct mvneta_port *pp,
3410 struct mvneta_tx_queue *txq)
3411{
3412 int ret;
3413
3414 ret = mvneta_txq_sw_init(pp, txq);
3415 if (ret < 0)
3416 return ret;
3417
3418 mvneta_txq_hw_init(pp, txq);
3419
3420 return 0;
3421}
3422
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003423/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003424static void mvneta_txq_sw_deinit(struct mvneta_port *pp,
3425 struct mvneta_tx_queue *txq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003426{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01003427 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
3428
Lorenzo Bianconi9e58c8b2019-10-19 10:13:26 +02003429 kfree(txq->buf);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003430
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03003431 if (txq->tso_hdrs)
3432 dma_free_coherent(pp->dev->dev.parent,
3433 txq->size * TSO_HEADER_SIZE,
3434 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003435 if (txq->descs)
3436 dma_free_coherent(pp->dev->dev.parent,
3437 txq->size * MVNETA_DESC_ALIGNED_SIZE,
3438 txq->descs, txq->descs_phys);
3439
Marcin Wojtasa29b6232017-01-16 18:08:32 +01003440 netdev_tx_reset_queue(nq);
3441
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003442 txq->descs = NULL;
3443 txq->last_desc = 0;
3444 txq->next_desc_to_proc = 0;
3445 txq->descs_phys = 0;
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003446}
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003447
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003448static void mvneta_txq_hw_deinit(struct mvneta_port *pp,
3449 struct mvneta_tx_queue *txq)
3450{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003451 /* Set minimum bandwidth for disabled TXQs */
3452 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
3453 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
3454
3455 /* Set Tx descriptors queue starting address and size */
3456 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
3457 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
3458}
3459
Jisheng Zhang4a188a62018-04-02 11:23:43 +08003460static void mvneta_txq_deinit(struct mvneta_port *pp,
3461 struct mvneta_tx_queue *txq)
3462{
3463 mvneta_txq_sw_deinit(pp, txq);
3464 mvneta_txq_hw_deinit(pp, txq);
3465}
3466
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003467/* Cleanup all Tx queues */
3468static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3469{
3470 int queue;
3471
3472 for (queue = 0; queue < txq_number; queue++)
3473 mvneta_txq_deinit(pp, &pp->txqs[queue]);
3474}
3475
3476/* Cleanup all Rx queues */
3477static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3478{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003479 int queue;
3480
Yelena Krivosheevca5902a2017-12-19 17:59:46 +01003481 for (queue = 0; queue < rxq_number; queue++)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003482 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003483}
3484
3485
3486/* Init all Rx queues */
3487static int mvneta_setup_rxqs(struct mvneta_port *pp)
3488{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003489 int queue;
3490
3491 for (queue = 0; queue < rxq_number; queue++) {
3492 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3493
3494 if (err) {
3495 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3496 __func__, queue);
3497 mvneta_cleanup_rxqs(pp);
3498 return err;
3499 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003500 }
3501
3502 return 0;
3503}
3504
3505/* Init all tx queues */
3506static int mvneta_setup_txqs(struct mvneta_port *pp)
3507{
3508 int queue;
3509
3510 for (queue = 0; queue < txq_number; queue++) {
3511 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3512 if (err) {
3513 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3514 __func__, queue);
3515 mvneta_cleanup_txqs(pp);
3516 return err;
3517 }
3518 }
3519
3520 return 0;
3521}
3522
Marek Behún031b9222019-02-25 17:43:03 +01003523static int mvneta_comphy_init(struct mvneta_port *pp)
3524{
3525 int ret;
3526
3527 if (!pp->comphy)
3528 return 0;
3529
3530 ret = phy_set_mode_ext(pp->comphy, PHY_MODE_ETHERNET,
3531 pp->phy_interface);
3532 if (ret)
3533 return ret;
3534
3535 return phy_power_on(pp->comphy);
3536}
3537
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003538static void mvneta_start_dev(struct mvneta_port *pp)
3539{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003540 int cpu;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003541
Marek Behún031b9222019-02-25 17:43:03 +01003542 WARN_ON(mvneta_comphy_init(pp));
Russell Kinga10c1c82019-02-07 16:19:26 +00003543
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003544 mvneta_max_rx_size_set(pp, pp->pkt_size);
3545 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3546
3547 /* start the Rx/Tx activity */
3548 mvneta_port_enable(pp);
3549
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003550 if (!pp->neta_armada3700) {
3551 /* Enable polling on the port */
3552 for_each_online_cpu(cpu) {
3553 struct mvneta_pcpu_port *port =
3554 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003555
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003556 napi_enable(&port->napi);
3557 }
3558 } else {
3559 napi_enable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003560 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003561
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003562 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003563 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3564
Stas Sergeev898b29702015-04-01 20:32:49 +03003565 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3566 MVNETA_CAUSE_PHY_STATUS_CHANGE |
Russell King856b2cc2018-01-02 17:25:09 +00003567 MVNETA_CAUSE_LINK_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003568
Russell King503f9aa92018-01-02 17:24:44 +00003569 phylink_start(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003570 netif_tx_start_all_queues(pp->dev);
3571}
3572
3573static void mvneta_stop_dev(struct mvneta_port *pp)
3574{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003575 unsigned int cpu;
3576
Russell King503f9aa92018-01-02 17:24:44 +00003577 phylink_stop(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003578
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003579 if (!pp->neta_armada3700) {
3580 for_each_online_cpu(cpu) {
3581 struct mvneta_pcpu_port *port =
3582 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003583
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003584 napi_disable(&port->napi);
3585 }
3586 } else {
3587 napi_disable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003588 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003589
3590 netif_carrier_off(pp->dev);
3591
3592 mvneta_port_down(pp);
3593 netif_tx_stop_all_queues(pp->dev);
3594
3595 /* Stop the port activity */
3596 mvneta_port_disable(pp);
3597
3598 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003599 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003600
3601 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003602 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003603
3604 mvneta_tx_reset(pp);
3605 mvneta_rx_reset(pp);
Russell Kinga10c1c82019-02-07 16:19:26 +00003606
3607 WARN_ON(phy_power_off(pp->comphy));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003608}
3609
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003610static void mvneta_percpu_enable(void *arg)
3611{
3612 struct mvneta_port *pp = arg;
3613
3614 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3615}
3616
3617static void mvneta_percpu_disable(void *arg)
3618{
3619 struct mvneta_port *pp = arg;
3620
3621 disable_percpu_irq(pp->dev->irq);
3622}
3623
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003624/* Change the device mtu */
3625static int mvneta_change_mtu(struct net_device *dev, int mtu)
3626{
3627 struct mvneta_port *pp = netdev_priv(dev);
3628 int ret;
3629
Jarod Wilson57779872016-10-17 15:54:06 -04003630 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3631 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3632 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3633 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3634 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003635
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02003636 if (pp->xdp_prog && mtu > MVNETA_MAX_RX_BUF_SIZE) {
3637 netdev_info(dev, "Illegal MTU value %d for XDP mode\n", mtu);
3638 return -EINVAL;
3639 }
3640
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003641 dev->mtu = mtu;
3642
Simon Guinotb65657f2015-06-30 16:20:22 +02003643 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003644 if (pp->bm_priv)
3645 mvneta_bm_update_mtu(pp, mtu);
3646
Simon Guinotb65657f2015-06-30 16:20:22 +02003647 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003648 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003649 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003650
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003651 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003652 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003653 */
3654 mvneta_stop_dev(pp);
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003655 on_each_cpu(mvneta_percpu_disable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003656
3657 mvneta_cleanup_txqs(pp);
3658 mvneta_cleanup_rxqs(pp);
3659
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003660 if (pp->bm_priv)
3661 mvneta_bm_update_mtu(pp, mtu);
3662
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003663 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003664
3665 ret = mvneta_setup_rxqs(pp);
3666 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003667 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003668 return ret;
3669 }
3670
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003671 ret = mvneta_setup_txqs(pp);
3672 if (ret) {
3673 netdev_err(dev, "unable to setup txqs after MTU change\n");
3674 return ret;
3675 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003676
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003677 on_each_cpu(mvneta_percpu_enable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003678 mvneta_start_dev(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003679
Simon Guinotb65657f2015-06-30 16:20:22 +02003680 netdev_update_features(dev);
3681
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003682 return 0;
3683}
3684
Simon Guinotb65657f2015-06-30 16:20:22 +02003685static netdev_features_t mvneta_fix_features(struct net_device *dev,
3686 netdev_features_t features)
3687{
3688 struct mvneta_port *pp = netdev_priv(dev);
3689
3690 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3691 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3692 netdev_info(dev,
3693 "Disable IP checksum for MTU greater than %dB\n",
3694 pp->tx_csum_limit);
3695 }
3696
3697 return features;
3698}
3699
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003700/* Get mac address */
3701static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3702{
3703 u32 mac_addr_l, mac_addr_h;
3704
3705 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3706 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3707 addr[0] = (mac_addr_h >> 24) & 0xFF;
3708 addr[1] = (mac_addr_h >> 16) & 0xFF;
3709 addr[2] = (mac_addr_h >> 8) & 0xFF;
3710 addr[3] = mac_addr_h & 0xFF;
3711 addr[4] = (mac_addr_l >> 8) & 0xFF;
3712 addr[5] = mac_addr_l & 0xFF;
3713}
3714
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003715/* Handle setting mac address */
3716static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3717{
3718 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003719 struct sockaddr *sockaddr = addr;
3720 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003721
Ezequiel Garciae68de362014-05-22 20:07:00 -03003722 ret = eth_prepare_mac_addr_change(dev, addr);
3723 if (ret < 0)
3724 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003725 /* Remove previous address table entry */
3726 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3727
3728 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003729 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003730
Ezequiel Garciae68de362014-05-22 20:07:00 -03003731 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003732 return 0;
3733}
3734
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003735static void mvneta_validate(struct phylink_config *config,
3736 unsigned long *supported,
Russell King503f9aa92018-01-02 17:24:44 +00003737 struct phylink_link_state *state)
Russell Kingfc548b92018-01-02 17:24:39 +00003738{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003739 struct net_device *ndev = to_net_dev(config->dev);
Russell Kinga10c1c82019-02-07 16:19:26 +00003740 struct mvneta_port *pp = netdev_priv(ndev);
Russell King503f9aa92018-01-02 17:24:44 +00003741 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
Russell Kingfc548b92018-01-02 17:24:39 +00003742
Russell King22f4bf82018-01-02 17:24:54 +00003743 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
Russell King503f9aa92018-01-02 17:24:44 +00003744 if (state->interface != PHY_INTERFACE_MODE_NA &&
3745 state->interface != PHY_INTERFACE_MODE_QSGMII &&
3746 state->interface != PHY_INTERFACE_MODE_SGMII &&
Russell King22f4bf82018-01-02 17:24:54 +00003747 !phy_interface_mode_is_8023z(state->interface) &&
Russell King503f9aa92018-01-02 17:24:44 +00003748 !phy_interface_mode_is_rgmii(state->interface)) {
3749 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3750 return;
Russell Kingfc548b92018-01-02 17:24:39 +00003751 }
Russell King503f9aa92018-01-02 17:24:44 +00003752
3753 /* Allow all the expected bits */
3754 phylink_set(mask, Autoneg);
3755 phylink_set_port_modes(mask);
3756
Russell King4932a912018-01-02 17:24:59 +00003757 /* Asymmetric pause is unsupported */
3758 phylink_set(mask, Pause);
Maxime Chevallierda58a932018-09-25 15:59:39 +02003759
Maxime Chevallier83e65df2018-11-09 09:17:33 +01003760 /* Half-duplex at speeds higher than 100Mbit is unsupported */
Russell Kinga10c1c82019-02-07 16:19:26 +00003761 if (pp->comphy || state->interface != PHY_INTERFACE_MODE_2500BASEX) {
3762 phylink_set(mask, 1000baseT_Full);
3763 phylink_set(mask, 1000baseX_Full);
3764 }
3765 if (pp->comphy || state->interface == PHY_INTERFACE_MODE_2500BASEX) {
Maxime Chevalliereda3d1b2019-03-27 17:31:06 +01003766 phylink_set(mask, 2500baseT_Full);
Russell Kinga10c1c82019-02-07 16:19:26 +00003767 phylink_set(mask, 2500baseX_Full);
3768 }
Russell King22f4bf82018-01-02 17:24:54 +00003769
3770 if (!phy_interface_mode_is_8023z(state->interface)) {
3771 /* 10M and 100M are only supported in non-802.3z mode */
3772 phylink_set(mask, 10baseT_Half);
3773 phylink_set(mask, 10baseT_Full);
3774 phylink_set(mask, 100baseT_Half);
3775 phylink_set(mask, 100baseT_Full);
3776 }
Russell King503f9aa92018-01-02 17:24:44 +00003777
3778 bitmap_and(supported, supported, mask,
3779 __ETHTOOL_LINK_MODE_MASK_NBITS);
3780 bitmap_and(state->advertising, state->advertising, mask,
3781 __ETHTOOL_LINK_MODE_MASK_NBITS);
Russell Kinga10c1c82019-02-07 16:19:26 +00003782
3783 /* We can only operate at 2500BaseX or 1000BaseX. If requested
3784 * to advertise both, only report advertising at 2500BaseX.
3785 */
3786 phylink_helper_basex_speed(state);
Russell Kingfc548b92018-01-02 17:24:39 +00003787}
3788
Russell Kingd46b7e42019-11-21 00:36:22 +00003789static void mvneta_mac_pcs_get_state(struct phylink_config *config,
3790 struct phylink_link_state *state)
Russell King503f9aa92018-01-02 17:24:44 +00003791{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003792 struct net_device *ndev = to_net_dev(config->dev);
Russell King503f9aa92018-01-02 17:24:44 +00003793 struct mvneta_port *pp = netdev_priv(ndev);
3794 u32 gmac_stat;
3795
3796 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3797
3798 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
Russell Kinga10c1c82019-02-07 16:19:26 +00003799 state->speed =
3800 state->interface == PHY_INTERFACE_MODE_2500BASEX ?
3801 SPEED_2500 : SPEED_1000;
Russell King503f9aa92018-01-02 17:24:44 +00003802 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3803 state->speed = SPEED_100;
3804 else
3805 state->speed = SPEED_10;
3806
3807 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3808 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3809 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3810
3811 state->pause = 0;
Russell King4932a912018-01-02 17:24:59 +00003812 if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
3813 state->pause |= MLO_PAUSE_RX;
3814 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
3815 state->pause |= MLO_PAUSE_TX;
Russell King503f9aa92018-01-02 17:24:44 +00003816}
3817
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003818static void mvneta_mac_an_restart(struct phylink_config *config)
Russell King22f4bf82018-01-02 17:24:54 +00003819{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003820 struct net_device *ndev = to_net_dev(config->dev);
Russell King22f4bf82018-01-02 17:24:54 +00003821 struct mvneta_port *pp = netdev_priv(ndev);
3822 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3823
3824 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3825 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
3826 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3827 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
3828}
3829
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003830static void mvneta_mac_config(struct phylink_config *config, unsigned int mode,
3831 const struct phylink_link_state *state)
Russell King503f9aa92018-01-02 17:24:44 +00003832{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003833 struct net_device *ndev = to_net_dev(config->dev);
Russell King503f9aa92018-01-02 17:24:44 +00003834 struct mvneta_port *pp = netdev_priv(ndev);
Russell King22f4bf82018-01-02 17:24:54 +00003835 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
Russell King503f9aa92018-01-02 17:24:44 +00003836 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
Maxime Chevallierda58a932018-09-25 15:59:39 +02003837 u32 new_ctrl4, gmac_ctrl4 = mvreg_read(pp, MVNETA_GMAC_CTRL_4);
Russell King503f9aa92018-01-02 17:24:44 +00003838 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3839 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3840
Russell King22f4bf82018-01-02 17:24:54 +00003841 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
Russell King32699952018-01-02 17:24:49 +00003842 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
3843 MVNETA_GMAC2_PORT_RESET);
Maxime Chevallierda58a932018-09-25 15:59:39 +02003844 new_ctrl4 = gmac_ctrl4 & ~(MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE);
Russell King503f9aa92018-01-02 17:24:44 +00003845 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3846 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3847 MVNETA_GMAC_INBAND_RESTART_AN |
Russell King503f9aa92018-01-02 17:24:44 +00003848 MVNETA_GMAC_AN_SPEED_EN |
Russell King22f4bf82018-01-02 17:24:54 +00003849 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
Russell King503f9aa92018-01-02 17:24:44 +00003850 MVNETA_GMAC_AN_FLOW_CTRL_EN |
Russell King503f9aa92018-01-02 17:24:44 +00003851 MVNETA_GMAC_AN_DUPLEX_EN);
3852
Russell King32699952018-01-02 17:24:49 +00003853 /* Even though it might look weird, when we're configured in
3854 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3855 */
3856 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
3857
3858 if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
Russell King22f4bf82018-01-02 17:24:54 +00003859 state->interface == PHY_INTERFACE_MODE_SGMII ||
3860 phy_interface_mode_is_8023z(state->interface))
Russell King32699952018-01-02 17:24:49 +00003861 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
3862
Russell King4932a912018-01-02 17:24:59 +00003863 if (phylink_test(state->advertising, Pause))
3864 new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
Russell King4932a912018-01-02 17:24:59 +00003865
Russell King503f9aa92018-01-02 17:24:44 +00003866 if (!phylink_autoneg_inband(mode)) {
Russell Kingff03f0b2020-02-26 10:24:12 +00003867 /* Phy or fixed speed - nothing to do, leave the
3868 * configured speed, duplex and flow control as-is.
3869 */
Russell King22f4bf82018-01-02 17:24:54 +00003870 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Russell King503f9aa92018-01-02 17:24:44 +00003871 /* SGMII mode receives the state from the PHY */
3872 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3873 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3874 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
Russell Kingff03f0b2020-02-26 10:24:12 +00003875 MVNETA_GMAC_FORCE_LINK_PASS |
3876 MVNETA_GMAC_CONFIG_MII_SPEED |
3877 MVNETA_GMAC_CONFIG_GMII_SPEED |
3878 MVNETA_GMAC_CONFIG_FULL_DUPLEX)) |
Russell King503f9aa92018-01-02 17:24:44 +00003879 MVNETA_GMAC_INBAND_AN_ENABLE |
3880 MVNETA_GMAC_AN_SPEED_EN |
3881 MVNETA_GMAC_AN_DUPLEX_EN;
Russell King22f4bf82018-01-02 17:24:54 +00003882 } else {
3883 /* 802.3z negotiation - only 1000base-X */
3884 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
3885 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3886 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
Russell Kingff03f0b2020-02-26 10:24:12 +00003887 MVNETA_GMAC_FORCE_LINK_PASS |
3888 MVNETA_GMAC_CONFIG_MII_SPEED)) |
Russell King22f4bf82018-01-02 17:24:54 +00003889 MVNETA_GMAC_INBAND_AN_ENABLE |
3890 MVNETA_GMAC_CONFIG_GMII_SPEED |
3891 /* The MAC only supports FD mode */
3892 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
Russell King4932a912018-01-02 17:24:59 +00003893
3894 if (state->pause & MLO_PAUSE_AN && state->an_enabled)
3895 new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
Russell King503f9aa92018-01-02 17:24:44 +00003896 }
3897
3898 /* Armada 370 documentation says we can only change the port mode
3899 * and in-band enable when the link is down, so force it down
3900 * while making these changes. We also do this for GMAC_CTRL2 */
Russell King22f4bf82018-01-02 17:24:54 +00003901 if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
3902 (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
Russell King503f9aa92018-01-02 17:24:44 +00003903 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3904 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3905 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3906 MVNETA_GMAC_FORCE_LINK_DOWN);
3907 }
3908
Russell Kinga10c1c82019-02-07 16:19:26 +00003909
Maxime Chevallierda58a932018-09-25 15:59:39 +02003910 /* When at 2.5G, the link partner can send frames with shortened
3911 * preambles.
3912 */
3913 if (state->speed == SPEED_2500)
3914 new_ctrl4 |= MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE;
3915
Marek Behún031b9222019-02-25 17:43:03 +01003916 if (pp->comphy && pp->phy_interface != state->interface &&
Russell Kinga10c1c82019-02-07 16:19:26 +00003917 (state->interface == PHY_INTERFACE_MODE_SGMII ||
3918 state->interface == PHY_INTERFACE_MODE_1000BASEX ||
Marek Behún031b9222019-02-25 17:43:03 +01003919 state->interface == PHY_INTERFACE_MODE_2500BASEX)) {
3920 pp->phy_interface = state->interface;
3921
3922 WARN_ON(phy_power_off(pp->comphy));
3923 WARN_ON(mvneta_comphy_init(pp));
3924 }
Russell Kinga10c1c82019-02-07 16:19:26 +00003925
Russell King22f4bf82018-01-02 17:24:54 +00003926 if (new_ctrl0 != gmac_ctrl0)
3927 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
Russell King503f9aa92018-01-02 17:24:44 +00003928 if (new_ctrl2 != gmac_ctrl2)
3929 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
Maxime Chevallierda58a932018-09-25 15:59:39 +02003930 if (new_ctrl4 != gmac_ctrl4)
3931 mvreg_write(pp, MVNETA_GMAC_CTRL_4, new_ctrl4);
Russell King503f9aa92018-01-02 17:24:44 +00003932 if (new_clk != gmac_clk)
3933 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3934 if (new_an != gmac_an)
3935 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
Russell King32699952018-01-02 17:24:49 +00003936
3937 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
3938 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3939 MVNETA_GMAC2_PORT_RESET) != 0)
3940 continue;
3941 }
Russell King503f9aa92018-01-02 17:24:44 +00003942}
3943
Russell King6d81f452018-01-02 17:25:04 +00003944static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
3945{
3946 u32 lpi_ctl1;
3947
3948 lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
3949 if (enable)
3950 lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
3951 else
3952 lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
3953 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
3954}
3955
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003956static void mvneta_mac_link_down(struct phylink_config *config,
3957 unsigned int mode, phy_interface_t interface)
Russell Kingfc548b92018-01-02 17:24:39 +00003958{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003959 struct net_device *ndev = to_net_dev(config->dev);
Russell Kingfc548b92018-01-02 17:24:39 +00003960 struct mvneta_port *pp = netdev_priv(ndev);
3961 u32 val;
3962
Russell King503f9aa92018-01-02 17:24:44 +00003963 mvneta_port_down(pp);
3964
3965 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003966 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3967 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3968 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3969 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3970 }
Russell King6d81f452018-01-02 17:25:04 +00003971
3972 pp->eee_active = false;
3973 mvneta_set_eee(pp, false);
Russell Kingfc548b92018-01-02 17:24:39 +00003974}
3975
Russell King91a208f2020-02-26 10:23:41 +00003976static void mvneta_mac_link_up(struct phylink_config *config,
3977 struct phy_device *phy,
3978 unsigned int mode, phy_interface_t interface,
3979 int speed, int duplex,
3980 bool tx_pause, bool rx_pause)
Russell Kingfc548b92018-01-02 17:24:39 +00003981{
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03003982 struct net_device *ndev = to_net_dev(config->dev);
Russell Kingfc548b92018-01-02 17:24:39 +00003983 struct mvneta_port *pp = netdev_priv(ndev);
3984 u32 val;
3985
Russell King503f9aa92018-01-02 17:24:44 +00003986 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003987 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
Russell Kingff03f0b2020-02-26 10:24:12 +00003988 val &= ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3989 MVNETA_GMAC_CONFIG_MII_SPEED |
3990 MVNETA_GMAC_CONFIG_GMII_SPEED |
3991 MVNETA_GMAC_CONFIG_FLOW_CTRL |
3992 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
Russell Kingfc548b92018-01-02 17:24:39 +00003993 val |= MVNETA_GMAC_FORCE_LINK_PASS;
Russell Kingff03f0b2020-02-26 10:24:12 +00003994
3995 if (speed == SPEED_1000 || speed == SPEED_2500)
3996 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3997 else if (speed == SPEED_100)
3998 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3999
4000 if (duplex == DUPLEX_FULL)
4001 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
4002
4003 if (tx_pause || rx_pause)
4004 val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
4005
4006 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4007 } else {
4008 /* When inband doesn't cover flow control or flow control is
4009 * disabled, we need to manually configure it. This bit will
4010 * only have effect if MVNETA_GMAC_AN_FLOW_CTRL_EN is unset.
4011 */
4012 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4013 val &= ~MVNETA_GMAC_CONFIG_FLOW_CTRL;
4014
4015 if (tx_pause || rx_pause)
4016 val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
4017
Russell Kingfc548b92018-01-02 17:24:39 +00004018 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4019 }
4020
4021 mvneta_port_up(pp);
Russell King6d81f452018-01-02 17:25:04 +00004022
4023 if (phy && pp->eee_enabled) {
4024 pp->eee_active = phy_init_eee(phy, 0) >= 0;
4025 mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
4026 }
Russell Kingfc548b92018-01-02 17:24:39 +00004027}
4028
Russell King503f9aa92018-01-02 17:24:44 +00004029static const struct phylink_mac_ops mvneta_phylink_ops = {
4030 .validate = mvneta_validate,
Russell Kingd46b7e42019-11-21 00:36:22 +00004031 .mac_pcs_get_state = mvneta_mac_pcs_get_state,
Russell King22f4bf82018-01-02 17:24:54 +00004032 .mac_an_restart = mvneta_mac_an_restart,
Russell King503f9aa92018-01-02 17:24:44 +00004033 .mac_config = mvneta_mac_config,
4034 .mac_link_down = mvneta_mac_link_down,
4035 .mac_link_up = mvneta_mac_link_up,
4036};
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004037
4038static int mvneta_mdio_probe(struct mvneta_port *pp)
4039{
Jisheng Zhang82960ff2017-04-14 19:07:32 +08004040 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Russell King503f9aa92018-01-02 17:24:44 +00004041 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004042
Russell King503f9aa92018-01-02 17:24:44 +00004043 if (err)
4044 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004045
Russell King503f9aa92018-01-02 17:24:44 +00004046 phylink_ethtool_get_wol(pp->phylink, &wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08004047 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
4048
Russell King503f9aa92018-01-02 17:24:44 +00004049 return err;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004050}
4051
4052static void mvneta_mdio_remove(struct mvneta_port *pp)
4053{
Russell King503f9aa92018-01-02 17:24:44 +00004054 phylink_disconnect_phy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004055}
4056
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01004057/* Electing a CPU must be done in an atomic way: it should be done
4058 * after or before the removal/insertion of a CPU and this function is
4059 * not reentrant.
4060 */
Maxime Ripardf8642882015-09-25 18:09:38 +02004061static void mvneta_percpu_elect(struct mvneta_port *pp)
4062{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01004063 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02004064
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01004065 /* Use the cpu associated to the rxq when it is online, in all
4066 * the other cases, use the cpu 0 which can't be offline.
4067 */
4068 if (cpu_online(pp->rxq_def))
4069 elected_cpu = pp->rxq_def;
4070
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01004071 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02004072
4073 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01004074 int rxq_map = 0, txq_map = 0;
4075 int rxq;
4076
4077 for (rxq = 0; rxq < rxq_number; rxq++)
4078 if ((rxq % max_cpu) == cpu)
4079 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
4080
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01004081 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01004082 /* Map the default receive queue queue to the
4083 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02004084 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01004085 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01004086
4087 /* We update the TX queue map only if we have one
4088 * queue. In this case we associate the TX queue to
4089 * the CPU bound to the default RX queue
4090 */
4091 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01004092 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01004093 MVNETA_CPU_TXQ_ACCESS(1) : 0;
4094 else
4095 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
4096 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
4097
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01004098 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
4099
4100 /* Update the interrupt mask on each CPU according the
4101 * new mapping
4102 */
4103 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
4104 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02004105 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01004106
Maxime Ripardf8642882015-09-25 18:09:38 +02004107 }
4108};
4109
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004110static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
Maxime Ripardf8642882015-09-25 18:09:38 +02004111{
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004112 int other_cpu;
4113 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4114 node_online);
Maxime Ripardf8642882015-09-25 18:09:38 +02004115 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4116
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004117
4118 spin_lock(&pp->lock);
4119 /*
4120 * Configuring the driver for a new CPU while the driver is
4121 * stopping is racy, so just avoid it.
4122 */
4123 if (pp->is_stopped) {
4124 spin_unlock(&pp->lock);
4125 return 0;
4126 }
4127 netif_tx_stop_all_queues(pp->dev);
4128
4129 /*
4130 * We have to synchronise on tha napi of each CPU except the one
4131 * just being woken up
4132 */
4133 for_each_online_cpu(other_cpu) {
4134 if (other_cpu != cpu) {
4135 struct mvneta_pcpu_port *other_port =
4136 per_cpu_ptr(pp->ports, other_cpu);
4137
4138 napi_synchronize(&other_port->napi);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01004139 }
Maxime Ripardf8642882015-09-25 18:09:38 +02004140 }
4141
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004142 /* Mask all ethernet port interrupts */
4143 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4144 napi_enable(&port->napi);
4145
4146 /*
4147 * Enable per-CPU interrupts on the CPU that is
4148 * brought up.
4149 */
4150 mvneta_percpu_enable(pp);
4151
4152 /*
4153 * Enable per-CPU interrupt on the one CPU we care
4154 * about.
4155 */
4156 mvneta_percpu_elect(pp);
4157
4158 /* Unmask all ethernet port interrupts */
4159 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
4160 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
4161 MVNETA_CAUSE_PHY_STATUS_CHANGE |
Russell King856b2cc2018-01-02 17:25:09 +00004162 MVNETA_CAUSE_LINK_CHANGE);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004163 netif_tx_start_all_queues(pp->dev);
4164 spin_unlock(&pp->lock);
4165 return 0;
4166}
4167
4168static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
4169{
4170 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4171 node_online);
4172 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4173
4174 /*
4175 * Thanks to this lock we are sure that any pending cpu election is
4176 * done.
4177 */
4178 spin_lock(&pp->lock);
4179 /* Mask all ethernet port interrupts */
4180 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4181 spin_unlock(&pp->lock);
4182
4183 napi_synchronize(&port->napi);
4184 napi_disable(&port->napi);
4185 /* Disable per-CPU interrupts on the CPU that is brought down. */
4186 mvneta_percpu_disable(pp);
4187 return 0;
4188}
4189
4190static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
4191{
4192 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4193 node_dead);
4194
4195 /* Check if a new CPU must be elected now this on is down */
4196 spin_lock(&pp->lock);
4197 mvneta_percpu_elect(pp);
4198 spin_unlock(&pp->lock);
4199 /* Unmask all ethernet port interrupts */
4200 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
4201 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
4202 MVNETA_CAUSE_PHY_STATUS_CHANGE |
Russell King856b2cc2018-01-02 17:25:09 +00004203 MVNETA_CAUSE_LINK_CHANGE);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004204 netif_tx_start_all_queues(pp->dev);
4205 return 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02004206}
4207
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004208static int mvneta_open(struct net_device *dev)
4209{
4210 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01004211 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004212
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004213 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
4214
4215 ret = mvneta_setup_rxqs(pp);
4216 if (ret)
4217 return ret;
4218
4219 ret = mvneta_setup_txqs(pp);
4220 if (ret)
4221 goto err_cleanup_rxqs;
4222
4223 /* Connect to port interrupt line */
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004224 if (pp->neta_armada3700)
4225 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
4226 dev->name, pp);
4227 else
4228 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
4229 dev->name, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004230 if (ret) {
4231 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
4232 goto err_cleanup_txqs;
4233 }
4234
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004235 if (!pp->neta_armada3700) {
4236 /* Enable per-CPU interrupt on all the CPU to handle our RX
4237 * queue interrupts
4238 */
4239 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01004240
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004241 pp->is_stopped = false;
4242 /* Register a CPU notifier to handle the case where our CPU
4243 * might be taken offline.
4244 */
4245 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
4246 &pp->node_online);
4247 if (ret)
4248 goto err_free_irq;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004249
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004250 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4251 &pp->node_dead);
4252 if (ret)
4253 goto err_free_online_hp;
4254 }
Maxime Ripardf8642882015-09-25 18:09:38 +02004255
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004256 ret = mvneta_mdio_probe(pp);
4257 if (ret < 0) {
4258 netdev_err(dev, "cannot probe MDIO bus\n");
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004259 goto err_free_dead_hp;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004260 }
4261
4262 mvneta_start_dev(pp);
4263
4264 return 0;
4265
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004266err_free_dead_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004267 if (!pp->neta_armada3700)
4268 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4269 &pp->node_dead);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004270err_free_online_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004271 if (!pp->neta_armada3700)
4272 cpuhp_state_remove_instance_nocalls(online_hpstate,
4273 &pp->node_online);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004274err_free_irq:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004275 if (pp->neta_armada3700) {
4276 free_irq(pp->dev->irq, pp);
4277 } else {
4278 on_each_cpu(mvneta_percpu_disable, pp, true);
4279 free_percpu_irq(pp->dev->irq, pp->ports);
4280 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004281err_cleanup_txqs:
4282 mvneta_cleanup_txqs(pp);
4283err_cleanup_rxqs:
4284 mvneta_cleanup_rxqs(pp);
4285 return ret;
4286}
4287
4288/* Stop the port, free port interrupt line */
4289static int mvneta_stop(struct net_device *dev)
4290{
4291 struct mvneta_port *pp = netdev_priv(dev);
4292
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004293 if (!pp->neta_armada3700) {
4294 /* Inform that we are stopping so we don't want to setup the
4295 * driver for new CPUs in the notifiers. The code of the
4296 * notifier for CPU online is protected by the same spinlock,
4297 * so when we get the lock, the notifer work is done.
4298 */
4299 spin_lock(&pp->lock);
4300 pp->is_stopped = true;
4301 spin_unlock(&pp->lock);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01004302
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004303 mvneta_stop_dev(pp);
4304 mvneta_mdio_remove(pp);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004305
Dan Carpenterd26aac22016-12-07 14:32:17 +03004306 cpuhp_state_remove_instance_nocalls(online_hpstate,
4307 &pp->node_online);
4308 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4309 &pp->node_dead);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004310 on_each_cpu(mvneta_percpu_disable, pp, true);
4311 free_percpu_irq(dev->irq, pp->ports);
4312 } else {
4313 mvneta_stop_dev(pp);
4314 mvneta_mdio_remove(pp);
4315 free_irq(dev->irq, pp);
4316 }
4317
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004318 mvneta_cleanup_rxqs(pp);
4319 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004320
4321 return 0;
4322}
4323
Thomas Petazzoni15f59452013-09-04 16:26:52 +02004324static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4325{
Russell King503f9aa92018-01-02 17:24:44 +00004326 struct mvneta_port *pp = netdev_priv(dev);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02004327
Russell King503f9aa92018-01-02 17:24:44 +00004328 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02004329}
4330
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02004331static int mvneta_xdp_setup(struct net_device *dev, struct bpf_prog *prog,
4332 struct netlink_ext_ack *extack)
4333{
4334 bool need_update, running = netif_running(dev);
4335 struct mvneta_port *pp = netdev_priv(dev);
4336 struct bpf_prog *old_prog;
4337
4338 if (prog && dev->mtu > MVNETA_MAX_RX_BUF_SIZE) {
4339 NL_SET_ERR_MSG_MOD(extack, "Jumbo frames not supported on XDP");
4340 return -EOPNOTSUPP;
4341 }
4342
Sven Auhagen79572c92020-01-25 08:07:03 +00004343 if (pp->bm_priv) {
4344 NL_SET_ERR_MSG_MOD(extack,
4345 "Hardware Buffer Management not supported on XDP");
4346 return -EOPNOTSUPP;
4347 }
4348
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02004349 need_update = !!pp->xdp_prog != !!prog;
4350 if (running && need_update)
4351 mvneta_stop(dev);
4352
4353 old_prog = xchg(&pp->xdp_prog, prog);
4354 if (old_prog)
4355 bpf_prog_put(old_prog);
4356
4357 if (running && need_update)
4358 return mvneta_open(dev);
4359
4360 return 0;
4361}
4362
4363static int mvneta_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4364{
4365 struct mvneta_port *pp = netdev_priv(dev);
4366
4367 switch (xdp->command) {
4368 case XDP_SETUP_PROG:
4369 return mvneta_xdp_setup(dev, xdp->prog, xdp->extack);
4370 case XDP_QUERY_PROG:
4371 xdp->prog_id = pp->xdp_prog ? pp->xdp_prog->aux->id : 0;
4372 return 0;
4373 default:
4374 return -EINVAL;
4375 }
4376}
4377
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004378/* Ethtool methods */
4379
Philippe Reynes013ad402016-07-30 17:42:12 +02004380/* Set link ksettings (phy address, speed) for ethtools */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +08004381static int
4382mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
4383 const struct ethtool_link_ksettings *cmd)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004384{
Philippe Reynes013ad402016-07-30 17:42:12 +02004385 struct mvneta_port *pp = netdev_priv(ndev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004386
Russell King503f9aa92018-01-02 17:24:44 +00004387 return phylink_ethtool_ksettings_set(pp->phylink, cmd);
4388}
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004389
Russell King503f9aa92018-01-02 17:24:44 +00004390/* Get link ksettings for ethtools */
4391static int
4392mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
4393 struct ethtool_link_ksettings *cmd)
4394{
4395 struct mvneta_port *pp = netdev_priv(ndev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03004396
Russell King503f9aa92018-01-02 17:24:44 +00004397 return phylink_ethtool_ksettings_get(pp->phylink, cmd);
4398}
Stas Sergeev0c0744f2015-12-02 20:35:11 +03004399
Russell King503f9aa92018-01-02 17:24:44 +00004400static int mvneta_ethtool_nway_reset(struct net_device *dev)
4401{
4402 struct mvneta_port *pp = netdev_priv(dev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03004403
Russell King503f9aa92018-01-02 17:24:44 +00004404 return phylink_ethtool_nway_reset(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004405}
4406
4407/* Set interrupt coalescing for ethtools */
4408static int mvneta_ethtool_set_coalesce(struct net_device *dev,
4409 struct ethtool_coalesce *c)
4410{
4411 struct mvneta_port *pp = netdev_priv(dev);
4412 int queue;
4413
4414 for (queue = 0; queue < rxq_number; queue++) {
4415 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4416 rxq->time_coal = c->rx_coalesce_usecs;
4417 rxq->pkts_coal = c->rx_max_coalesced_frames;
4418 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
4419 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
4420 }
4421
4422 for (queue = 0; queue < txq_number; queue++) {
4423 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4424 txq->done_pkts_coal = c->tx_max_coalesced_frames;
4425 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
4426 }
4427
4428 return 0;
4429}
4430
4431/* get coalescing for ethtools */
4432static int mvneta_ethtool_get_coalesce(struct net_device *dev,
4433 struct ethtool_coalesce *c)
4434{
4435 struct mvneta_port *pp = netdev_priv(dev);
4436
4437 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
4438 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
4439
4440 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
4441 return 0;
4442}
4443
4444
4445static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
4446 struct ethtool_drvinfo *drvinfo)
4447{
4448 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
4449 sizeof(drvinfo->driver));
4450 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
4451 sizeof(drvinfo->version));
4452 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
4453 sizeof(drvinfo->bus_info));
4454}
4455
4456
4457static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
4458 struct ethtool_ringparam *ring)
4459{
4460 struct mvneta_port *pp = netdev_priv(netdev);
4461
4462 ring->rx_max_pending = MVNETA_MAX_RXD;
4463 ring->tx_max_pending = MVNETA_MAX_TXD;
4464 ring->rx_pending = pp->rx_ring_size;
4465 ring->tx_pending = pp->tx_ring_size;
4466}
4467
4468static int mvneta_ethtool_set_ringparam(struct net_device *dev,
4469 struct ethtool_ringparam *ring)
4470{
4471 struct mvneta_port *pp = netdev_priv(dev);
4472
4473 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
4474 return -EINVAL;
4475 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
4476 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004477
4478 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
4479 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
4480 if (pp->tx_ring_size != ring->tx_pending)
4481 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
4482 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004483
4484 if (netif_running(dev)) {
4485 mvneta_stop(dev);
4486 if (mvneta_open(dev)) {
4487 netdev_err(dev,
4488 "error on opening device after ring param change\n");
4489 return -ENOMEM;
4490 }
4491 }
4492
4493 return 0;
4494}
4495
Russell King4932a912018-01-02 17:24:59 +00004496static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
4497 struct ethtool_pauseparam *pause)
4498{
4499 struct mvneta_port *pp = netdev_priv(dev);
4500
4501 phylink_ethtool_get_pauseparam(pp->phylink, pause);
4502}
4503
4504static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
4505 struct ethtool_pauseparam *pause)
4506{
4507 struct mvneta_port *pp = netdev_priv(dev);
4508
4509 return phylink_ethtool_set_pauseparam(pp->phylink, pause);
4510}
4511
Russell King9b0cdef2015-10-22 18:37:30 +01004512static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
4513 u8 *data)
4514{
4515 if (sset == ETH_SS_STATS) {
4516 int i;
4517
4518 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4519 memcpy(data + i * ETH_GSTRING_LEN,
4520 mvneta_statistics[i].name, ETH_GSTRING_LEN);
4521 }
4522}
4523
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004524static void
4525mvneta_ethtool_update_pcpu_stats(struct mvneta_port *pp,
4526 struct mvneta_ethtool_stats *es)
4527{
4528 unsigned int start;
4529 int cpu;
4530
4531 for_each_possible_cpu(cpu) {
4532 struct mvneta_pcpu_stats *stats;
4533 u64 skb_alloc_error;
4534 u64 refill_error;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01004535 u64 xdp_redirect;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01004536 u64 xdp_xmit_err;
4537 u64 xdp_tx_err;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01004538 u64 xdp_pass;
4539 u64 xdp_drop;
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01004540 u64 xdp_xmit;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01004541 u64 xdp_tx;
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004542
4543 stats = per_cpu_ptr(pp->stats, cpu);
4544 do {
4545 start = u64_stats_fetch_begin_irq(&stats->syncp);
4546 skb_alloc_error = stats->es.skb_alloc_error;
4547 refill_error = stats->es.refill_error;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01004548 xdp_redirect = stats->es.ps.xdp_redirect;
4549 xdp_pass = stats->es.ps.xdp_pass;
4550 xdp_drop = stats->es.ps.xdp_drop;
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01004551 xdp_xmit = stats->es.ps.xdp_xmit;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01004552 xdp_xmit_err = stats->es.ps.xdp_xmit_err;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01004553 xdp_tx = stats->es.ps.xdp_tx;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01004554 xdp_tx_err = stats->es.ps.xdp_tx_err;
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004555 } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
4556
4557 es->skb_alloc_error += skb_alloc_error;
4558 es->refill_error += refill_error;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01004559 es->ps.xdp_redirect += xdp_redirect;
4560 es->ps.xdp_pass += xdp_pass;
4561 es->ps.xdp_drop += xdp_drop;
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01004562 es->ps.xdp_xmit += xdp_xmit;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01004563 es->ps.xdp_xmit_err += xdp_xmit_err;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01004564 es->ps.xdp_tx += xdp_tx;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01004565 es->ps.xdp_tx_err += xdp_tx_err;
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004566 }
4567}
4568
Russell King9b0cdef2015-10-22 18:37:30 +01004569static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
4570{
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004571 struct mvneta_ethtool_stats stats = {};
Russell King9b0cdef2015-10-22 18:37:30 +01004572 const struct mvneta_statistic *s;
4573 void __iomem *base = pp->base;
Russell King6d81f452018-01-02 17:25:04 +00004574 u32 high, low;
4575 u64 val;
Russell King9b0cdef2015-10-22 18:37:30 +01004576 int i;
4577
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004578 mvneta_ethtool_update_pcpu_stats(pp, &stats);
Russell King9b0cdef2015-10-22 18:37:30 +01004579 for (i = 0, s = mvneta_statistics;
4580 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
4581 s++, i++) {
Russell King9b0cdef2015-10-22 18:37:30 +01004582 switch (s->type) {
4583 case T_REG_32:
4584 val = readl_relaxed(base + s->offset);
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004585 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01004586 break;
4587 case T_REG_64:
4588 /* Docs say to read low 32-bit then high */
4589 low = readl_relaxed(base + s->offset);
4590 high = readl_relaxed(base + s->offset + 4);
Russell King6d81f452018-01-02 17:25:04 +00004591 val = (u64)high << 32 | low;
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004592 pp->ethtool_stats[i] += val;
Russell King6d81f452018-01-02 17:25:04 +00004593 break;
4594 case T_SW:
4595 switch (s->offset) {
4596 case ETHTOOL_STAT_EEE_WAKEUP:
4597 val = phylink_get_eee_err(pp->phylink);
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004598 pp->ethtool_stats[i] += val;
Russell King6d81f452018-01-02 17:25:04 +00004599 break;
Gregory CLEMENT17a96da2018-07-18 18:10:54 +02004600 case ETHTOOL_STAT_SKB_ALLOC_ERR:
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004601 pp->ethtool_stats[i] = stats.skb_alloc_error;
Gregory CLEMENT17a96da2018-07-18 18:10:54 +02004602 break;
4603 case ETHTOOL_STAT_REFILL_ERR:
Lorenzo Bianconi9ac41f32020-02-16 22:07:29 +01004604 pp->ethtool_stats[i] = stats.refill_error;
Gregory CLEMENT17a96da2018-07-18 18:10:54 +02004605 break;
Lorenzo Bianconi3d866522020-02-16 22:07:32 +01004606 case ETHTOOL_XDP_REDIRECT:
4607 pp->ethtool_stats[i] = stats.ps.xdp_redirect;
4608 break;
4609 case ETHTOOL_XDP_PASS:
4610 pp->ethtool_stats[i] = stats.ps.xdp_pass;
4611 break;
4612 case ETHTOOL_XDP_DROP:
4613 pp->ethtool_stats[i] = stats.ps.xdp_drop;
4614 break;
4615 case ETHTOOL_XDP_TX:
4616 pp->ethtool_stats[i] = stats.ps.xdp_tx;
4617 break;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01004618 case ETHTOOL_XDP_TX_ERR:
4619 pp->ethtool_stats[i] = stats.ps.xdp_tx_err;
4620 break;
Lorenzo Bianconi7d51a012020-02-19 10:57:37 +01004621 case ETHTOOL_XDP_XMIT:
4622 pp->ethtool_stats[i] = stats.ps.xdp_xmit;
4623 break;
Jesper Dangaard Brouer150709192020-03-02 14:46:28 +01004624 case ETHTOOL_XDP_XMIT_ERR:
4625 pp->ethtool_stats[i] = stats.ps.xdp_xmit_err;
4626 break;
Russell King6d81f452018-01-02 17:25:04 +00004627 }
Russell King9b0cdef2015-10-22 18:37:30 +01004628 break;
4629 }
Russell King9b0cdef2015-10-22 18:37:30 +01004630 }
4631}
4632
4633static void mvneta_ethtool_get_stats(struct net_device *dev,
4634 struct ethtool_stats *stats, u64 *data)
4635{
4636 struct mvneta_port *pp = netdev_priv(dev);
4637 int i;
4638
4639 mvneta_ethtool_update_stats(pp);
4640
4641 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4642 *data++ = pp->ethtool_stats[i];
4643}
4644
4645static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
4646{
4647 if (sset == ETH_SS_STATS)
4648 return ARRAY_SIZE(mvneta_statistics);
4649 return -EOPNOTSUPP;
4650}
4651
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004652static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
4653{
4654 return MVNETA_RSS_LU_TABLE_SIZE;
4655}
4656
4657static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
4658 struct ethtool_rxnfc *info,
4659 u32 *rules __always_unused)
4660{
4661 switch (info->cmd) {
4662 case ETHTOOL_GRXRINGS:
4663 info->data = rxq_number;
4664 return 0;
4665 case ETHTOOL_GRXFH:
4666 return -EOPNOTSUPP;
4667 default:
4668 return -EOPNOTSUPP;
4669 }
4670}
4671
4672static int mvneta_config_rss(struct mvneta_port *pp)
4673{
4674 int cpu;
4675 u32 val;
4676
4677 netif_tx_stop_all_queues(pp->dev);
4678
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01004679 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004680
Jisheng Zhang0f5c6c32018-08-10 11:36:27 +08004681 if (!pp->neta_armada3700) {
4682 /* We have to synchronise on the napi of each CPU */
4683 for_each_online_cpu(cpu) {
4684 struct mvneta_pcpu_port *pcpu_port =
4685 per_cpu_ptr(pp->ports, cpu);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004686
Jisheng Zhang0f5c6c32018-08-10 11:36:27 +08004687 napi_synchronize(&pcpu_port->napi);
4688 napi_disable(&pcpu_port->napi);
4689 }
4690 } else {
4691 napi_synchronize(&pp->napi);
4692 napi_disable(&pp->napi);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004693 }
4694
4695 pp->rxq_def = pp->indir[0];
4696
4697 /* Update unicast mapping */
4698 mvneta_set_rx_mode(pp->dev);
4699
4700 /* Update val of portCfg register accordingly with all RxQueue types */
4701 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
4702 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
4703
4704 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01004705 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004706 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01004707 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004708
Jisheng Zhang0f5c6c32018-08-10 11:36:27 +08004709 if (!pp->neta_armada3700) {
4710 /* We have to synchronise on the napi of each CPU */
4711 for_each_online_cpu(cpu) {
4712 struct mvneta_pcpu_port *pcpu_port =
4713 per_cpu_ptr(pp->ports, cpu);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004714
Jisheng Zhang0f5c6c32018-08-10 11:36:27 +08004715 napi_enable(&pcpu_port->napi);
4716 }
4717 } else {
4718 napi_enable(&pp->napi);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004719 }
4720
4721 netif_tx_start_all_queues(pp->dev);
4722
4723 return 0;
4724}
4725
4726static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
4727 const u8 *key, const u8 hfunc)
4728{
4729 struct mvneta_port *pp = netdev_priv(dev);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004730
4731 /* Current code for Armada 3700 doesn't support RSS features yet */
4732 if (pp->neta_armada3700)
4733 return -EOPNOTSUPP;
4734
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004735 /* We require at least one supported parameter to be changed
4736 * and no change in any of the unsupported parameters
4737 */
4738 if (key ||
4739 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
4740 return -EOPNOTSUPP;
4741
4742 if (!indir)
4743 return 0;
4744
4745 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
4746
4747 return mvneta_config_rss(pp);
4748}
4749
4750static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
4751 u8 *hfunc)
4752{
4753 struct mvneta_port *pp = netdev_priv(dev);
4754
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004755 /* Current code for Armada 3700 doesn't support RSS features yet */
4756 if (pp->neta_armada3700)
4757 return -EOPNOTSUPP;
4758
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004759 if (hfunc)
4760 *hfunc = ETH_RSS_HASH_TOP;
4761
4762 if (!indir)
4763 return 0;
4764
4765 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
4766
4767 return 0;
4768}
4769
Jingju Houb60a00f2017-02-06 14:58:13 +08004770static void mvneta_ethtool_get_wol(struct net_device *dev,
4771 struct ethtool_wolinfo *wol)
4772{
Russell King503f9aa92018-01-02 17:24:44 +00004773 struct mvneta_port *pp = netdev_priv(dev);
Jingju Houb60a00f2017-02-06 14:58:13 +08004774
Russell King503f9aa92018-01-02 17:24:44 +00004775 phylink_ethtool_get_wol(pp->phylink, wol);
Jingju Houb60a00f2017-02-06 14:58:13 +08004776}
4777
4778static int mvneta_ethtool_set_wol(struct net_device *dev,
4779 struct ethtool_wolinfo *wol)
4780{
Russell King503f9aa92018-01-02 17:24:44 +00004781 struct mvneta_port *pp = netdev_priv(dev);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08004782 int ret;
4783
Russell King503f9aa92018-01-02 17:24:44 +00004784 ret = phylink_ethtool_set_wol(pp->phylink, wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08004785 if (!ret)
4786 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
4787
4788 return ret;
Jingju Houb60a00f2017-02-06 14:58:13 +08004789}
4790
Russell King6d81f452018-01-02 17:25:04 +00004791static int mvneta_ethtool_get_eee(struct net_device *dev,
4792 struct ethtool_eee *eee)
4793{
4794 struct mvneta_port *pp = netdev_priv(dev);
4795 u32 lpi_ctl0;
4796
4797 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4798
4799 eee->eee_enabled = pp->eee_enabled;
4800 eee->eee_active = pp->eee_active;
4801 eee->tx_lpi_enabled = pp->tx_lpi_enabled;
4802 eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale;
4803
4804 return phylink_ethtool_get_eee(pp->phylink, eee);
4805}
4806
4807static int mvneta_ethtool_set_eee(struct net_device *dev,
4808 struct ethtool_eee *eee)
4809{
4810 struct mvneta_port *pp = netdev_priv(dev);
4811 u32 lpi_ctl0;
4812
4813 /* The Armada 37x documents do not give limits for this other than
4814 * it being an 8-bit register. */
YueHaibinge4a3e9f2018-11-22 14:42:00 +08004815 if (eee->tx_lpi_enabled && eee->tx_lpi_timer > 255)
Russell King6d81f452018-01-02 17:25:04 +00004816 return -EINVAL;
4817
4818 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4819 lpi_ctl0 &= ~(0xff << 8);
4820 lpi_ctl0 |= eee->tx_lpi_timer << 8;
4821 mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0);
4822
4823 pp->eee_enabled = eee->eee_enabled;
4824 pp->tx_lpi_enabled = eee->tx_lpi_enabled;
4825
4826 mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled);
4827
4828 return phylink_ethtool_set_eee(pp->phylink, eee);
4829}
4830
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004831static const struct net_device_ops mvneta_netdev_ops = {
4832 .ndo_open = mvneta_open,
4833 .ndo_stop = mvneta_stop,
4834 .ndo_start_xmit = mvneta_tx,
4835 .ndo_set_rx_mode = mvneta_set_rx_mode,
4836 .ndo_set_mac_address = mvneta_set_mac_addr,
4837 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02004838 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004839 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02004840 .ndo_do_ioctl = mvneta_ioctl,
Lorenzo Bianconi0db51da2019-10-19 10:13:24 +02004841 .ndo_bpf = mvneta_xdp,
Lorenzo Bianconib0a43db2019-10-19 10:13:27 +02004842 .ndo_xdp_xmit = mvneta_xdp_xmit,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004843};
4844
Jisheng Zhang4581be42017-02-16 17:07:39 +08004845static const struct ethtool_ops mvneta_eth_tool_ops = {
Jakub Kicinski16e8d8b2020-03-12 21:07:51 -07004846 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
4847 ETHTOOL_COALESCE_MAX_FRAMES,
Russell King503f9aa92018-01-02 17:24:44 +00004848 .nway_reset = mvneta_ethtool_nway_reset,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004849 .get_link = ethtool_op_get_link,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004850 .set_coalesce = mvneta_ethtool_set_coalesce,
4851 .get_coalesce = mvneta_ethtool_get_coalesce,
4852 .get_drvinfo = mvneta_ethtool_get_drvinfo,
4853 .get_ringparam = mvneta_ethtool_get_ringparam,
4854 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King4932a912018-01-02 17:24:59 +00004855 .get_pauseparam = mvneta_ethtool_get_pauseparam,
4856 .set_pauseparam = mvneta_ethtool_set_pauseparam,
Russell King9b0cdef2015-10-22 18:37:30 +01004857 .get_strings = mvneta_ethtool_get_strings,
4858 .get_ethtool_stats = mvneta_ethtool_get_stats,
4859 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004860 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
4861 .get_rxnfc = mvneta_ethtool_get_rxnfc,
4862 .get_rxfh = mvneta_ethtool_get_rxfh,
4863 .set_rxfh = mvneta_ethtool_set_rxfh,
Russell King503f9aa92018-01-02 17:24:44 +00004864 .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
Philippe Reynes013ad402016-07-30 17:42:12 +02004865 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
Jingju Houb60a00f2017-02-06 14:58:13 +08004866 .get_wol = mvneta_ethtool_get_wol,
4867 .set_wol = mvneta_ethtool_set_wol,
Russell King6d81f452018-01-02 17:25:04 +00004868 .get_eee = mvneta_ethtool_get_eee,
4869 .set_eee = mvneta_ethtool_set_eee,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004870};
4871
4872/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03004873static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004874{
4875 int queue;
4876
4877 /* Disable port */
4878 mvneta_port_disable(pp);
4879
4880 /* Set port default values */
4881 mvneta_defaults_set(pp);
4882
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004883 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004884 if (!pp->txqs)
4885 return -ENOMEM;
4886
4887 /* Initialize TX descriptor rings */
4888 for (queue = 0; queue < txq_number; queue++) {
4889 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4890 txq->id = queue;
4891 txq->size = pp->tx_ring_size;
4892 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4893 }
4894
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004895 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
Ezequiel Garcia96728502014-05-22 20:06:59 -03004896 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004897 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004898
4899 /* Create Rx descriptor rings */
4900 for (queue = 0; queue < rxq_number; queue++) {
4901 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4902 rxq->id = queue;
4903 rxq->size = pp->rx_ring_size;
4904 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4905 rxq->time_coal = MVNETA_RX_COAL_USEC;
Markus Elfring29110632017-04-16 21:23:19 +02004906 rxq->buf_virt_addr
4907 = devm_kmalloc_array(pp->dev->dev.parent,
4908 rxq->size,
4909 sizeof(*rxq->buf_virt_addr),
4910 GFP_KERNEL);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01004911 if (!rxq->buf_virt_addr)
4912 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004913 }
4914
4915 return 0;
4916}
4917
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004918/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00004919static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4920 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004921{
4922 u32 win_enable;
4923 u32 win_protect;
4924 int i;
4925
4926 for (i = 0; i < 6; i++) {
4927 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4928 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4929
4930 if (i < 4)
4931 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4932 }
4933
4934 win_enable = 0x3f;
4935 win_protect = 0;
4936
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004937 if (dram) {
4938 for (i = 0; i < dram->num_cs; i++) {
4939 const struct mbus_dram_window *cs = dram->cs + i;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004940
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004941 mvreg_write(pp, MVNETA_WIN_BASE(i),
4942 (cs->base & 0xffff0000) |
4943 (cs->mbus_attr << 8) |
4944 dram->mbus_dram_target_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004945
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004946 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4947 (cs->size - 1) & 0xffff0000);
4948
4949 win_enable &= ~(1 << i);
4950 win_protect |= 3 << (2 * i);
4951 }
4952 } else {
4953 /* For Armada3700 open default 4GB Mbus window, leaving
4954 * arbitration of target/attribute to a different layer
4955 * of configuration.
4956 */
4957 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4958 win_enable &= ~BIT(0);
4959 win_protect = 3;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004960 }
4961
4962 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01004963 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004964}
4965
4966/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004967static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004968{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004969 /* MAC Cause register should be cleared */
4970 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4971
Russell King32699952018-01-02 17:24:49 +00004972 if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004973 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
Russell King22f4bf82018-01-02 17:24:54 +00004974 else if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
Russell Kinga10c1c82019-02-07 16:19:26 +00004975 phy_interface_mode_is_8023z(phy_mode))
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004976 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
Russell King32699952018-01-02 17:24:49 +00004977 else if (!phy_interface_mode_is_rgmii(phy_mode))
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004978 return -EINVAL;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004979
4980 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004981}
4982
4983/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00004984static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004985{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004986 struct device_node *dn = pdev->dev.of_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004987 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004988 struct mvneta_port *pp;
4989 struct net_device *dev;
Russell King503f9aa92018-01-02 17:24:44 +00004990 struct phylink *phylink;
Russell Kinga10c1c82019-02-07 16:19:26 +00004991 struct phy *comphy;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004992 const char *dt_mac_addr;
4993 char hw_mac_addr[ETH_ALEN];
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01004994 phy_interface_t phy_mode;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004995 const char *mac_from;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004996 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004997 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004998 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004999
Rosen Peneva3ddd942019-04-23 13:46:03 -07005000 dev = devm_alloc_etherdev_mqs(&pdev->dev, sizeof(struct mvneta_port),
5001 txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005002 if (!dev)
5003 return -ENOMEM;
5004
5005 dev->irq = irq_of_parse_and_map(dn, 0);
Rosen Peneva3ddd942019-04-23 13:46:03 -07005006 if (dev->irq == 0)
5007 return -EINVAL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005008
Andrew Lunn0c65b2b2019-11-04 02:40:33 +01005009 err = of_get_phy_mode(dn, &phy_mode);
5010 if (err) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005011 dev_err(&pdev->dev, "incorrect phy-mode\n");
Russell King503f9aa92018-01-02 17:24:44 +00005012 goto err_free_irq;
5013 }
5014
Russell Kinga10c1c82019-02-07 16:19:26 +00005015 comphy = devm_of_phy_get(&pdev->dev, dn, NULL);
5016 if (comphy == ERR_PTR(-EPROBE_DEFER)) {
5017 err = -EPROBE_DEFER;
5018 goto err_free_irq;
5019 } else if (IS_ERR(comphy)) {
5020 comphy = NULL;
5021 }
5022
Ioana Ciornei44cc27e2019-05-28 20:38:12 +03005023 pp = netdev_priv(dev);
5024 spin_lock_init(&pp->lock);
5025
5026 pp->phylink_config.dev = &dev->dev;
5027 pp->phylink_config.type = PHYLINK_NETDEV;
5028
5029 phylink = phylink_create(&pp->phylink_config, pdev->dev.fwnode,
5030 phy_mode, &mvneta_phylink_ops);
Russell King503f9aa92018-01-02 17:24:44 +00005031 if (IS_ERR(phylink)) {
5032 err = PTR_ERR(phylink);
5033 goto err_free_irq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005034 }
5035
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005036 dev->tx_queue_len = MVNETA_MAX_TXD;
5037 dev->watchdog_timeo = 5 * HZ;
5038 dev->netdev_ops = &mvneta_netdev_ops;
5039
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00005040 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005041
Russell King503f9aa92018-01-02 17:24:44 +00005042 pp->phylink = phylink;
Russell Kinga10c1c82019-02-07 16:19:26 +00005043 pp->comphy = comphy;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005044 pp->phy_interface = phy_mode;
Russell King503f9aa92018-01-02 17:24:44 +00005045 pp->dn = dn;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005046
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01005047 pp->rxq_def = rxq_def;
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01005048 pp->indir[0] = rxq_def;
5049
Marcin Wojtas2636ac32016-12-01 18:03:09 +01005050 /* Get special SoC configurations */
5051 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
5052 pp->neta_armada3700 = true;
5053
Jisheng Zhang2804ba42016-01-20 19:27:23 +08005054 pp->clk = devm_clk_get(&pdev->dev, "core");
5055 if (IS_ERR(pp->clk))
5056 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01005057 if (IS_ERR(pp->clk)) {
5058 err = PTR_ERR(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00005059 goto err_free_phylink;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01005060 }
5061
5062 clk_prepare_enable(pp->clk);
5063
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08005064 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
5065 if (!IS_ERR(pp->clk_bus))
5066 clk_prepare_enable(pp->clk_bus);
5067
Jisheng Zhang00c33af2019-07-25 07:48:04 +00005068 pp->base = devm_platform_ioremap_resource(pdev, 0);
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01005069 if (IS_ERR(pp->base)) {
5070 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02005071 goto err_clk;
5072 }
5073
Maxime Ripard12bb03b2015-09-25 18:09:36 +02005074 /* Alloc per-cpu port structure */
5075 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
5076 if (!pp->ports) {
5077 err = -ENOMEM;
5078 goto err_clk;
5079 }
5080
willy tarreau74c41b02014-01-16 08:20:08 +01005081 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08005082 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01005083 if (!pp->stats) {
5084 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02005085 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01005086 }
5087
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00005088 dt_mac_addr = of_get_mac_address(dn);
Petr Å tetiara51645f2019-05-06 23:27:04 +02005089 if (!IS_ERR(dt_mac_addr)) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00005090 mac_from = "device tree";
Petr Å tetiar2d2924a2019-05-10 11:35:17 +02005091 ether_addr_copy(dev->dev_addr, dt_mac_addr);
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00005092 } else {
5093 mvneta_get_mac_addr(pp, hw_mac_addr);
5094 if (is_valid_ether_addr(hw_mac_addr)) {
5095 mac_from = "hardware";
5096 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
5097 } else {
5098 mac_from = "random";
5099 eth_hw_addr_random(dev);
5100 }
5101 }
5102
Marcin Wojtas9110ee02015-11-30 13:27:45 +01005103 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
5104 if (tx_csum_limit < 0 ||
5105 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
5106 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
5107 dev_info(&pdev->dev,
5108 "Wrong TX csum limit in DT, set to %dB\n",
5109 MVNETA_TX_CSUM_DEF_SIZE);
5110 }
5111 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
5112 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
5113 } else {
5114 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
5115 }
5116
5117 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02005118
Jane Li9768b452017-03-16 16:22:28 +08005119 pp->dram_target_info = mv_mbus_dram_info();
Marcin Wojtas2636ac32016-12-01 18:03:09 +01005120 /* Armada3700 requires setting default configuration of Mbus
5121 * windows, however without using filled mbus_dram_target_info
5122 * structure.
5123 */
Jane Li9768b452017-03-16 16:22:28 +08005124 if (pp->dram_target_info || pp->neta_armada3700)
5125 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005126
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005127 pp->tx_ring_size = MVNETA_MAX_TXD;
5128 pp->rx_ring_size = MVNETA_MAX_RXD;
5129
5130 pp->dev = dev;
5131 SET_NETDEV_DEV(dev, &pdev->dev);
5132
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005133 pp->id = global_port_id++;
5134
5135 /* Obtain access to BM resources if enabled and already initialized */
5136 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
Gregory CLEMENT965cbbe2018-07-18 18:10:52 +02005137 if (bm_node) {
5138 pp->bm_priv = mvneta_bm_get(bm_node);
5139 if (pp->bm_priv) {
5140 err = mvneta_bm_port_init(pdev, pp);
5141 if (err < 0) {
5142 dev_info(&pdev->dev,
5143 "use SW buffer management\n");
5144 mvneta_bm_put(pp->bm_priv);
5145 pp->bm_priv = NULL;
5146 }
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005147 }
Yelena Krivosheev562e2f42018-07-18 18:10:57 +02005148 /* Set RX packet offset correction for platforms, whose
5149 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
5150 * platforms and 0B for 32-bit ones.
5151 */
5152 pp->rx_offset_correction = max(0,
5153 NET_SKB_PAD -
5154 MVNETA_RX_PKT_OFFSET_CORRECTION);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005155 }
Peter Chend4e4da02016-08-01 15:02:36 +08005156 of_node_put(bm_node);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005157
Lorenzo Bianconi44efc782020-01-29 12:50:53 +01005158 /* sw buffer management */
5159 if (!pp->bm_priv)
5160 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
5161
Ezequiel Garcia96728502014-05-22 20:06:59 -03005162 err = mvneta_init(&pdev->dev, pp);
5163 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005164 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02005165
5166 err = mvneta_port_power_up(pp, phy_mode);
5167 if (err < 0) {
5168 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005169 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02005170 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005171
Marcin Wojtas2636ac32016-12-01 18:03:09 +01005172 /* Armada3700 network controller does not support per-cpu
5173 * operation, so only single NAPI should be initialized.
5174 */
5175 if (pp->neta_armada3700) {
5176 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
5177 } else {
5178 for_each_present_cpu(cpu) {
5179 struct mvneta_pcpu_port *port =
5180 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02005181
Marcin Wojtas2636ac32016-12-01 18:03:09 +01005182 netif_napi_add(dev, &port->napi, mvneta_poll,
5183 NAPI_POLL_WEIGHT);
5184 port->pp = pp;
5185 }
Maxime Ripard12bb03b2015-09-25 18:09:36 +02005186 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005187
Jisheng Zhang77729882018-08-31 16:10:03 +08005188 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5189 NETIF_F_TSO | NETIF_F_RXCSUM;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03005190 dev->hw_features |= dev->features;
5191 dev->vlan_features |= dev->features;
Andrew Lunn97db8af2016-11-24 00:08:13 +01005192 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03005193 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00005194
Jarod Wilson57779872016-10-17 15:54:06 -04005195 /* MTU range: 68 - 9676 */
5196 dev->min_mtu = ETH_MIN_MTU;
5197 /* 9676 == 9700 - 20 and rounding to 8 */
5198 dev->max_mtu = 9676;
5199
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005200 err = register_netdev(dev);
5201 if (err < 0) {
5202 dev_err(&pdev->dev, "failed to register\n");
Jisheng Zhangd484e06e2019-05-27 11:04:17 +00005203 goto err_netdev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005204 }
5205
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00005206 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
5207 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005208
5209 platform_set_drvdata(pdev, pp->dev);
5210
5211 return 0;
5212
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005213err_netdev:
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005214 if (pp->bm_priv) {
5215 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
5216 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
5217 1 << pp->id);
Gregory CLEMENT965cbbe2018-07-18 18:10:52 +02005218 mvneta_bm_put(pp->bm_priv);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005219 }
willy tarreau74c41b02014-01-16 08:20:08 +01005220 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02005221err_free_ports:
5222 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02005223err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08005224 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02005225 clk_disable_unprepare(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00005226err_free_phylink:
5227 if (pp->phylink)
5228 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005229err_free_irq:
5230 irq_dispose_mapping(dev->irq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005231 return err;
5232}
5233
5234/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00005235static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005236{
5237 struct net_device *dev = platform_get_drvdata(pdev);
5238 struct mvneta_port *pp = netdev_priv(dev);
5239
5240 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08005241 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01005242 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02005243 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01005244 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005245 irq_dispose_mapping(dev->irq);
Russell King503f9aa92018-01-02 17:24:44 +00005246 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005247
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005248 if (pp->bm_priv) {
5249 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
5250 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
5251 1 << pp->id);
Gregory CLEMENT965cbbe2018-07-18 18:10:52 +02005252 mvneta_bm_put(pp->bm_priv);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01005253 }
5254
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005255 return 0;
5256}
5257
Jane Li9768b452017-03-16 16:22:28 +08005258#ifdef CONFIG_PM_SLEEP
5259static int mvneta_suspend(struct device *device)
5260{
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005261 int queue;
Jane Li9768b452017-03-16 16:22:28 +08005262 struct net_device *dev = dev_get_drvdata(device);
5263 struct mvneta_port *pp = netdev_priv(dev);
5264
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005265 if (!netif_running(dev))
5266 goto clean_exit;
5267
5268 if (!pp->neta_armada3700) {
5269 spin_lock(&pp->lock);
5270 pp->is_stopped = true;
5271 spin_unlock(&pp->lock);
5272
5273 cpuhp_state_remove_instance_nocalls(online_hpstate,
5274 &pp->node_online);
5275 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
5276 &pp->node_dead);
5277 }
5278
Russell King3b8bc672018-01-02 17:24:34 +00005279 rtnl_lock();
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005280 mvneta_stop_dev(pp);
Russell King3b8bc672018-01-02 17:24:34 +00005281 rtnl_unlock();
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005282
5283 for (queue = 0; queue < rxq_number; queue++) {
5284 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5285
5286 mvneta_rxq_drop_pkts(pp, rxq);
5287 }
5288
5289 for (queue = 0; queue < txq_number; queue++) {
5290 struct mvneta_tx_queue *txq = &pp->txqs[queue];
5291
5292 mvneta_txq_hw_deinit(pp, txq);
5293 }
5294
5295clean_exit:
Jane Li9768b452017-03-16 16:22:28 +08005296 netif_device_detach(dev);
5297 clk_disable_unprepare(pp->clk_bus);
5298 clk_disable_unprepare(pp->clk);
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005299
Jane Li9768b452017-03-16 16:22:28 +08005300 return 0;
5301}
5302
5303static int mvneta_resume(struct device *device)
5304{
5305 struct platform_device *pdev = to_platform_device(device);
5306 struct net_device *dev = dev_get_drvdata(device);
5307 struct mvneta_port *pp = netdev_priv(dev);
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005308 int err, queue;
Jane Li9768b452017-03-16 16:22:28 +08005309
5310 clk_prepare_enable(pp->clk);
5311 if (!IS_ERR(pp->clk_bus))
5312 clk_prepare_enable(pp->clk_bus);
5313 if (pp->dram_target_info || pp->neta_armada3700)
5314 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
5315 if (pp->bm_priv) {
5316 err = mvneta_bm_port_init(pdev, pp);
5317 if (err < 0) {
5318 dev_info(&pdev->dev, "use SW buffer management\n");
Lorenzo Bianconi44efc782020-01-29 12:50:53 +01005319 pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
Jane Li9768b452017-03-16 16:22:28 +08005320 pp->bm_priv = NULL;
5321 }
5322 }
5323 mvneta_defaults_set(pp);
5324 err = mvneta_port_power_up(pp, pp->phy_interface);
5325 if (err < 0) {
5326 dev_err(device, "can't power up port\n");
5327 return err;
5328 }
5329
Jane Li9768b452017-03-16 16:22:28 +08005330 netif_device_attach(dev);
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005331
5332 if (!netif_running(dev))
5333 return 0;
5334
5335 for (queue = 0; queue < rxq_number; queue++) {
5336 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5337
5338 rxq->next_desc_to_proc = 0;
5339 mvneta_rxq_hw_init(pp, rxq);
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08005340 }
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005341
5342 for (queue = 0; queue < txq_number; queue++) {
5343 struct mvneta_tx_queue *txq = &pp->txqs[queue];
5344
5345 txq->next_desc_to_proc = 0;
5346 mvneta_txq_hw_init(pp, txq);
5347 }
5348
5349 if (!pp->neta_armada3700) {
5350 spin_lock(&pp->lock);
5351 pp->is_stopped = false;
5352 spin_unlock(&pp->lock);
5353 cpuhp_state_add_instance_nocalls(online_hpstate,
5354 &pp->node_online);
5355 cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
5356 &pp->node_dead);
5357 }
5358
5359 rtnl_lock();
5360 mvneta_start_dev(pp);
Russell King3b8bc672018-01-02 17:24:34 +00005361 rtnl_unlock();
Jisheng Zhang1799cdd22018-04-02 11:24:59 +08005362 mvneta_set_rx_mode(dev);
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08005363
Jane Li9768b452017-03-16 16:22:28 +08005364 return 0;
5365}
5366#endif
5367
5368static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
5369
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005370static const struct of_device_id mvneta_match[] = {
5371 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02005372 { .compatible = "marvell,armada-xp-neta" },
Marcin Wojtas2636ac32016-12-01 18:03:09 +01005373 { .compatible = "marvell,armada-3700-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005374 { }
5375};
5376MODULE_DEVICE_TABLE(of, mvneta_match);
5377
5378static struct platform_driver mvneta_driver = {
5379 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00005380 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005381 .driver = {
5382 .name = MVNETA_DRIVER_NAME,
5383 .of_match_table = mvneta_match,
Jane Li9768b452017-03-16 16:22:28 +08005384 .pm = &mvneta_pm_ops,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005385 },
5386};
5387
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02005388static int __init mvneta_driver_init(void)
5389{
5390 int ret;
5391
Christophe JAILLET664d0352020-04-12 23:20:34 +02005392 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvneta:online",
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02005393 mvneta_cpu_online,
5394 mvneta_cpu_down_prepare);
5395 if (ret < 0)
5396 goto out;
5397 online_hpstate = ret;
5398 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
5399 NULL, mvneta_cpu_dead);
5400 if (ret)
5401 goto err_dead;
5402
5403 ret = platform_driver_register(&mvneta_driver);
5404 if (ret)
5405 goto err;
5406 return 0;
5407
5408err:
5409 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
5410err_dead:
5411 cpuhp_remove_multi_state(online_hpstate);
5412out:
5413 return ret;
5414}
5415module_init(mvneta_driver_init);
5416
5417static void __exit mvneta_driver_exit(void)
5418{
5419 platform_driver_unregister(&mvneta_driver);
5420 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
5421 cpuhp_remove_multi_state(online_hpstate);
5422}
5423module_exit(mvneta_driver_exit);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005424
5425MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
5426MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
5427MODULE_LICENSE("GPL");
5428
Joe Perchesd3757ba2018-03-23 16:34:44 -07005429module_param(rxq_number, int, 0444);
5430module_param(txq_number, int, 0444);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03005431
Joe Perchesd3757ba2018-03-23 16:34:44 -07005432module_param(rxq_def, int, 0444);
5433module_param(rx_copybreak, int, 0644);