blob: fabe17bd39f99a4ea0434f4ccd75807273043712 [file] [log] [blame]
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001/*
2 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Rami Rosen <rosenr@marvell.com>
7 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
Jisheng Zhang0e03f562016-01-20 19:27:22 +080014#include <linux/clk.h>
15#include <linux/cpu.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030016#include <linux/etherdevice.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080017#include <linux/if_vlan.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030018#include <linux/inetdevice.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030022#include <linux/mbus.h>
23#include <linux/module.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080024#include <linux/netdevice.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030025#include <linux/of.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080026#include <linux/of_address.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030027#include <linux/of_irq.h>
28#include <linux/of_mdio.h>
29#include <linux/of_net.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030030#include <linux/phy.h>
Russell King503f9aa92018-01-02 17:24:44 +000031#include <linux/phylink.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080032#include <linux/platform_device.h>
33#include <linux/skbuff.h>
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +010034#include <net/hwbm.h>
Marcin Wojtasdc35a102016-03-14 09:39:03 +010035#include "mvneta_bm.h"
Jisheng Zhang0e03f562016-01-20 19:27:22 +080036#include <net/ip.h>
37#include <net/ipv6.h>
38#include <net/tso.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030039
40/* Registers */
41#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
Marcin Wojtase5bdf682015-11-30 13:27:42 +010042#define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +010043#define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
44#define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
45#define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
46#define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
Thomas Petazzonic5aff182012-08-17 14:04:28 +030047#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
48#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
49#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
50#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
51#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
52#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
53#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
54#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
55#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
56#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
57#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
58#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
59#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
Marcin Wojtasdc35a102016-03-14 09:39:03 +010060#define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
61#define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
62#define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
Thomas Petazzonic5aff182012-08-17 14:04:28 +030063#define MVNETA_PORT_RX_RESET 0x1cc0
64#define MVNETA_PORT_RX_DMA_RESET BIT(0)
65#define MVNETA_PHY_ADDR 0x2000
66#define MVNETA_PHY_ADDR_MASK 0x1f
67#define MVNETA_MBUS_RETRY 0x2010
68#define MVNETA_UNIT_INTR_CAUSE 0x2080
69#define MVNETA_UNIT_CONTROL 0x20B0
70#define MVNETA_PHY_POLLING_ENABLE BIT(1)
71#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
72#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
73#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
74#define MVNETA_BASE_ADDR_ENABLE 0x2290
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +010075#define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
Thomas Petazzonic5aff182012-08-17 14:04:28 +030076#define MVNETA_PORT_CONFIG 0x2400
77#define MVNETA_UNI_PROMISC_MODE BIT(0)
78#define MVNETA_DEF_RXQ(q) ((q) << 1)
79#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
80#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
81#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
82#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
83#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
84#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
85#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
86 MVNETA_DEF_RXQ_ARP(q) | \
87 MVNETA_DEF_RXQ_TCP(q) | \
88 MVNETA_DEF_RXQ_UDP(q) | \
89 MVNETA_DEF_RXQ_BPDU(q) | \
90 MVNETA_TX_UNSET_ERR_SUM | \
91 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92#define MVNETA_PORT_CONFIG_EXTEND 0x2404
93#define MVNETA_MAC_ADDR_LOW 0x2414
94#define MVNETA_MAC_ADDR_HIGH 0x2418
95#define MVNETA_SDMA_CONFIG 0x241c
96#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030097#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
98#define MVNETA_RX_NO_DATA_SWAP BIT(4)
99#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200100#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300101#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
102#define MVNETA_PORT_STATUS 0x2444
103#define MVNETA_TX_IN_PRGRS BIT(1)
104#define MVNETA_TX_FIFO_EMPTY BIT(8)
105#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200106#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200107#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200108#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300109#define MVNETA_TYPE_PRIO 0x24bc
110#define MVNETA_FORCE_UNI BIT(21)
111#define MVNETA_TXQ_CMD_1 0x24e4
112#define MVNETA_TXQ_CMD 0x2448
113#define MVNETA_TXQ_DISABLE_SHIFT 8
114#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Andrew Lunne4839112015-10-22 18:37:36 +0100115#define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
116#define MVNETA_OVERRUN_FRAME_COUNT 0x2488
Stas Sergeev898b29702015-04-01 20:32:49 +0300117#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
118#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300119#define MVNETA_ACC_MODE 0x2500
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100120#define MVNETA_BM_ADDRESS 0x2504
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300121#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
122#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
123#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100124#define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100125#define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300126#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100127
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100128/* Exception Interrupt Port/Queue Cause register
129 *
130 * Their behavior depend of the mapping done using the PCPX2Q
131 * registers. For a given CPU if the bit associated to a queue is not
132 * set, then for the register a read from this CPU will always return
133 * 0 and a write won't do anything
134 */
willy tarreau40ba35e2014-01-16 08:20:10 +0100135
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300136#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300137#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100138
139/* bits 0..7 = TXQ SENT, one bit per queue.
140 * bits 8..15 = RXQ OCCUP, one bit per queue.
141 * bits 16..23 = RXQ FREE, one bit per queue.
142 * bit 29 = OLD_REG_SUM, see old reg ?
143 * bit 30 = TX_ERR_SUM, one bit for 4 ports
144 * bit 31 = MISC_SUM, one bit for 4 ports
145 */
146#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
147#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
148#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
149#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b29702015-04-01 20:32:49 +0300150#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100151
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300152#define MVNETA_INTR_OLD_CAUSE 0x25a8
153#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100154
155/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300156#define MVNETA_INTR_MISC_CAUSE 0x25b0
157#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100158
159#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
160#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
161#define MVNETA_CAUSE_PTP BIT(4)
162
163#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
164#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
165#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
166#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
167#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
168#define MVNETA_CAUSE_PRBS_ERR BIT(12)
169#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
170#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
171
172#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
173#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
174#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
175
176#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
177#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
178#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
179
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300180#define MVNETA_INTR_ENABLE 0x25b8
181#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
Marcin Wojtasdc1aadf2015-11-30 13:27:43 +0100182#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
willy tarreau40ba35e2014-01-16 08:20:10 +0100183
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300184#define MVNETA_RXQ_CMD 0x2680
185#define MVNETA_RXQ_DISABLE_SHIFT 8
186#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
187#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
188#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
189#define MVNETA_GMAC_CTRL_0 0x2c00
190#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
191#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
192#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
193#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b29702015-04-01 20:32:49 +0300194#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100195#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300196#define MVNETA_GMAC2_PORT_RGMII BIT(4)
197#define MVNETA_GMAC2_PORT_RESET BIT(6)
198#define MVNETA_GMAC_STATUS 0x2c10
199#define MVNETA_GMAC_LINK_UP BIT(0)
200#define MVNETA_GMAC_SPEED_1000 BIT(1)
201#define MVNETA_GMAC_SPEED_100 BIT(2)
202#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
203#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
204#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
205#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
206#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
Russell King503f9aa92018-01-02 17:24:44 +0000207#define MVNETA_GMAC_AN_COMPLETE BIT(11)
208#define MVNETA_GMAC_SYNC_OK BIT(14)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300209#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
210#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
211#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b29702015-04-01 20:32:49 +0300212#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300213#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
214#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200215#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Stas Sergeev898b29702015-04-01 20:32:49 +0300216#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300217#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200218#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Andrew Lunne4839112015-10-22 18:37:36 +0100219#define MVNETA_MIB_COUNTERS_BASE 0x3000
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300220#define MVNETA_MIB_LATE_COLLISION 0x7c
221#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
222#define MVNETA_DA_FILT_OTH_MCAST 0x3500
223#define MVNETA_DA_FILT_UCAST_BASE 0x3600
224#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
225#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
226#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
227#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
228#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
229#define MVNETA_TXQ_DEC_SENT_SHIFT 16
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100230#define MVNETA_TXQ_DEC_SENT_MASK 0xff
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300231#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
232#define MVNETA_TXQ_SENT_DESC_SHIFT 16
233#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
234#define MVNETA_PORT_TX_RESET 0x3cf0
235#define MVNETA_PORT_TX_DMA_RESET BIT(0)
236#define MVNETA_TX_MTU 0x3e0c
237#define MVNETA_TX_TOKEN_SIZE 0x3e14
238#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
239#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
240#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
241
242#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
243
244/* Descriptor ring Macros */
245#define MVNETA_QUEUE_NEXT_DESC(q, index) \
246 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
247
248/* Various constants */
249
250/* Coalescing */
Dmitri Epshtein06708f82016-07-06 04:18:58 +0200251#define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300252#define MVNETA_RX_COAL_PKTS 32
253#define MVNETA_RX_COAL_USEC 100
254
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100255/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300256 * by Marvell switches when a specific hardware mode is enabled (not
257 * supported by this driver) or is filled automatically by zeroes on
258 * the RX side. Those two bytes being at the front of the Ethernet
259 * header, they allow to have the IP header aligned on a 4 bytes
260 * boundary automatically: the hardware skips those two bytes on its
261 * own.
262 */
263#define MVNETA_MH_SIZE 2
264
265#define MVNETA_VLAN_TAG_LEN 4
266
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100267#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300268#define MVNETA_TX_CSUM_MAX_SIZE 9800
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100269#define MVNETA_ACC_MODE_EXT1 1
270#define MVNETA_ACC_MODE_EXT2 2
271
272#define MVNETA_MAX_DECODE_WIN 6
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300273
274/* Timeout constants */
275#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
276#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
277#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
278
279#define MVNETA_TX_MTU_MAX 0x3ffff
280
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100281/* The RSS lookup table actually has 256 entries but we do not use
282 * them yet
283 */
284#define MVNETA_RSS_LU_TABLE_SIZE 1
285
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300286/* Max number of Rx descriptors */
287#define MVNETA_MAX_RXD 128
288
289/* Max number of Tx descriptors */
290#define MVNETA_MAX_TXD 532
291
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300292/* Max number of allowed TCP segments for software TSO */
293#define MVNETA_MAX_TSO_SEGS 100
294
295#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
296
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300297/* descriptor aligned size */
298#define MVNETA_DESC_ALIGNED_SIZE 32
299
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100300/* Number of bytes to be taken into account by HW when putting incoming data
301 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
302 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
303 */
304#define MVNETA_RX_PKT_OFFSET_CORRECTION 64
305
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300306#define MVNETA_RX_PKT_SIZE(mtu) \
307 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
308 ETH_HLEN + ETH_FCS_LEN, \
Jisheng Zhangc66e98c2016-04-01 17:12:49 +0800309 cache_line_size())
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300310
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300311#define IS_TSO_HEADER(txq, addr) \
312 ((addr >= txq->tso_hdrs_phys) && \
313 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
314
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100315#define MVNETA_RX_GET_BM_POOL_ID(rxd) \
316 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300317
Russell King9b0cdef2015-10-22 18:37:30 +0100318struct mvneta_statistic {
319 unsigned short offset;
320 unsigned short type;
321 const char name[ETH_GSTRING_LEN];
322};
323
324#define T_REG_32 32
325#define T_REG_64 64
326
327static const struct mvneta_statistic mvneta_statistics[] = {
328 { 0x3000, T_REG_64, "good_octets_received", },
329 { 0x3010, T_REG_32, "good_frames_received", },
330 { 0x3008, T_REG_32, "bad_octets_received", },
331 { 0x3014, T_REG_32, "bad_frames_received", },
332 { 0x3018, T_REG_32, "broadcast_frames_received", },
333 { 0x301c, T_REG_32, "multicast_frames_received", },
334 { 0x3050, T_REG_32, "unrec_mac_control_received", },
335 { 0x3058, T_REG_32, "good_fc_received", },
336 { 0x305c, T_REG_32, "bad_fc_received", },
337 { 0x3060, T_REG_32, "undersize_received", },
338 { 0x3064, T_REG_32, "fragments_received", },
339 { 0x3068, T_REG_32, "oversize_received", },
340 { 0x306c, T_REG_32, "jabber_received", },
341 { 0x3070, T_REG_32, "mac_receive_error", },
342 { 0x3074, T_REG_32, "bad_crc_event", },
343 { 0x3078, T_REG_32, "collision", },
344 { 0x307c, T_REG_32, "late_collision", },
345 { 0x2484, T_REG_32, "rx_discard", },
346 { 0x2488, T_REG_32, "rx_overrun", },
347 { 0x3020, T_REG_32, "frames_64_octets", },
348 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
349 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
350 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
351 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
352 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
353 { 0x3038, T_REG_64, "good_octets_sent", },
354 { 0x3040, T_REG_32, "good_frames_sent", },
355 { 0x3044, T_REG_32, "excessive_collision", },
356 { 0x3048, T_REG_32, "multicast_frames_sent", },
357 { 0x304c, T_REG_32, "broadcast_frames_sent", },
358 { 0x3054, T_REG_32, "fc_sent", },
359 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
360};
361
willy tarreau74c41b02014-01-16 08:20:08 +0100362struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300363 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100364 u64 rx_packets;
365 u64 rx_bytes;
366 u64 tx_packets;
367 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300368};
369
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200370struct mvneta_pcpu_port {
371 /* Pointer to the shared port */
372 struct mvneta_port *pp;
373
374 /* Pointer to the CPU-local NAPI struct */
375 struct napi_struct napi;
376
377 /* Cause of the previous interrupt */
378 u32 cause_rx_tx;
379};
380
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300381struct mvneta_port {
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100382 u8 id;
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200383 struct mvneta_pcpu_port __percpu *ports;
384 struct mvneta_pcpu_stats __percpu *stats;
385
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300386 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100387 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300388 void __iomem *base;
389 struct mvneta_rx_queue *rxqs;
390 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300391 struct net_device *dev;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200392 struct hlist_node node_online;
393 struct hlist_node node_dead;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100394 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100395 /* Protect the access to the percpu interrupt registers,
396 * ensuring that the configuration remains coherent.
397 */
398 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100399 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300400
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100401 u32 cause_rx_tx;
402 struct napi_struct napi;
403
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300404 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100405 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800406 /* AXI clock */
407 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300408 u8 mcast_count[256];
409 u16 tx_ring_size;
410 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300411
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300412 phy_interface_t phy_interface;
Russell King503f9aa92018-01-02 17:24:44 +0000413 struct device_node *dn;
Simon Guinotb65657f2015-06-30 16:20:22 +0200414 unsigned int tx_csum_limit;
Russell King503f9aa92018-01-02 17:24:44 +0000415 struct phylink *phylink;
Russell King9b0cdef2015-10-22 18:37:30 +0100416
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100417 struct mvneta_bm *bm_priv;
418 struct mvneta_bm_pool *pool_long;
419 struct mvneta_bm_pool *pool_short;
420 int bm_win_id;
421
Russell King9b0cdef2015-10-22 18:37:30 +0100422 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100423
424 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100425
426 /* Flags for special SoC configurations */
427 bool neta_armada3700;
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100428 u16 rx_offset_correction;
Jane Li9768b452017-03-16 16:22:28 +0800429 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300430};
431
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100432/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300433 * layout of the transmit and reception DMA descriptors, and their
434 * layout is therefore defined by the hardware design
435 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200436
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300437#define MVNETA_TX_L3_OFF_SHIFT 0
438#define MVNETA_TX_IP_HLEN_SHIFT 8
439#define MVNETA_TX_L4_UDP BIT(16)
440#define MVNETA_TX_L3_IP6 BIT(17)
441#define MVNETA_TXD_IP_CSUM BIT(18)
442#define MVNETA_TXD_Z_PAD BIT(19)
443#define MVNETA_TXD_L_DESC BIT(20)
444#define MVNETA_TXD_F_DESC BIT(21)
445#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
446 MVNETA_TXD_L_DESC | \
447 MVNETA_TXD_F_DESC)
448#define MVNETA_TX_L4_CSUM_FULL BIT(30)
449#define MVNETA_TX_L4_CSUM_NOT BIT(31)
450
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300451#define MVNETA_RXD_ERR_CRC 0x0
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100452#define MVNETA_RXD_BM_POOL_SHIFT 13
453#define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300454#define MVNETA_RXD_ERR_SUMMARY BIT(16)
455#define MVNETA_RXD_ERR_OVERRUN BIT(17)
456#define MVNETA_RXD_ERR_LEN BIT(18)
457#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
458#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
459#define MVNETA_RXD_L3_IP4 BIT(25)
460#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
461#define MVNETA_RXD_L4_CSUM_OK BIT(30)
462
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200463#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200464struct mvneta_tx_desc {
465 u32 command; /* Options used by HW for packet transmitting.*/
466 u16 reserverd1; /* csum_l4 (for future use) */
467 u16 data_size; /* Data size of transmitted packet in bytes */
468 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
469 u32 reserved2; /* hw_cmd - (for future use, PMT) */
470 u32 reserved3[4]; /* Reserved - (for future use) */
471};
472
473struct mvneta_rx_desc {
474 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300475 u16 reserved1; /* pnc_info - (for future use, PnC) */
476 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200477
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300478 u32 buf_phys_addr; /* Physical address of the buffer */
479 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200480
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300481 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
482 u16 reserved3; /* prefetch_cmd, for future use */
483 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200484
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300485 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
486 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
487};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200488#else
489struct mvneta_tx_desc {
490 u16 data_size; /* Data size of transmitted packet in bytes */
491 u16 reserverd1; /* csum_l4 (for future use) */
492 u32 command; /* Options used by HW for packet transmitting.*/
493 u32 reserved2; /* hw_cmd - (for future use, PMT) */
494 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
495 u32 reserved3[4]; /* Reserved - (for future use) */
496};
497
498struct mvneta_rx_desc {
499 u16 data_size; /* Size of received packet in bytes */
500 u16 reserved1; /* pnc_info - (for future use, PnC) */
501 u32 status; /* Info about received packet */
502
503 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
504 u32 buf_phys_addr; /* Physical address of the buffer */
505
506 u16 reserved4; /* csum_l4 - (for future use, PnC) */
507 u16 reserved3; /* prefetch_cmd, for future use */
508 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
509
510 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
511 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
512};
513#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300514
515struct mvneta_tx_queue {
516 /* Number of this TX queue, in the range 0-7 */
517 u8 id;
518
519 /* Number of TX DMA descriptors in the descriptor ring */
520 int size;
521
522 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100523 * descriptor ring
524 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300525 int count;
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100526 int pending;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300527 int tx_stop_threshold;
528 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300529
530 /* Array of transmitted skb */
531 struct sk_buff **tx_skb;
532
533 /* Index of last TX DMA descriptor that was inserted */
534 int txq_put_index;
535
536 /* Index of the TX DMA descriptor to be cleaned up */
537 int txq_get_index;
538
539 u32 done_pkts_coal;
540
541 /* Virtual address of the TX DMA descriptors array */
542 struct mvneta_tx_desc *descs;
543
544 /* DMA address of the TX DMA descriptors array */
545 dma_addr_t descs_phys;
546
547 /* Index of the last TX DMA descriptor */
548 int last_desc;
549
550 /* Index of the next TX DMA descriptor to process */
551 int next_desc_to_proc;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300552
553 /* DMA buffers for TSO headers */
554 char *tso_hdrs;
555
556 /* DMA address of TSO headers */
557 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100558
559 /* Affinity mask for CPUs*/
560 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300561};
562
563struct mvneta_rx_queue {
564 /* rx queue number, in the range 0-7 */
565 u8 id;
566
567 /* num of rx descriptors in the rx descriptor ring */
568 int size;
569
570 /* counter of times when mvneta_refill() failed */
571 int missed;
572
573 u32 pkts_coal;
574 u32 time_coal;
575
Gregory CLEMENTf88bee12016-12-01 18:03:06 +0100576 /* Virtual address of the RX buffer */
577 void **buf_virt_addr;
578
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300579 /* Virtual address of the RX DMA descriptors array */
580 struct mvneta_rx_desc *descs;
581
582 /* DMA address of the RX DMA descriptors array */
583 dma_addr_t descs_phys;
584
585 /* Index of the last RX DMA descriptor */
586 int last_desc;
587
588 /* Index of the next RX DMA descriptor to process */
589 int next_desc_to_proc;
590};
591
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200592static enum cpuhp_state online_hpstate;
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300593/* The hardware supports eight (8) rx queues, but we are only allowing
594 * the first one to be used. Therefore, let's just allocate one queue.
595 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200596static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300597static int txq_number = 8;
598
599static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300600
willy tarreauf19fadf2014-01-16 08:20:17 +0100601static int rx_copybreak __read_mostly = 256;
602
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100603/* HW BM need that each port be identify by a unique ID */
604static int global_port_id;
605
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300606#define MVNETA_DRIVER_NAME "mvneta"
607#define MVNETA_DRIVER_VERSION "1.0"
608
609/* Utility/helper methods */
610
611/* Write helper method */
612static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
613{
614 writel(data, pp->base + offset);
615}
616
617/* Read helper method */
618static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
619{
620 return readl(pp->base + offset);
621}
622
623/* Increment txq get counter */
624static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
625{
626 txq->txq_get_index++;
627 if (txq->txq_get_index == txq->size)
628 txq->txq_get_index = 0;
629}
630
631/* Increment txq put counter */
632static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
633{
634 txq->txq_put_index++;
635 if (txq->txq_put_index == txq->size)
636 txq->txq_put_index = 0;
637}
638
639
640/* Clear all MIB counters */
641static void mvneta_mib_counters_clear(struct mvneta_port *pp)
642{
643 int i;
644 u32 dummy;
645
646 /* Perform dummy reads from MIB counters */
647 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
648 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100649 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
650 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300651}
652
653/* Get System Network Statistics */
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800654static void
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +0800655mvneta_get_stats64(struct net_device *dev,
656 struct rtnl_link_stats64 *stats)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300657{
658 struct mvneta_port *pp = netdev_priv(dev);
659 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100660 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300661
willy tarreau74c41b02014-01-16 08:20:08 +0100662 for_each_possible_cpu(cpu) {
663 struct mvneta_pcpu_stats *cpu_stats;
664 u64 rx_packets;
665 u64 rx_bytes;
666 u64 tx_packets;
667 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300668
willy tarreau74c41b02014-01-16 08:20:08 +0100669 cpu_stats = per_cpu_ptr(pp->stats, cpu);
670 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700671 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100672 rx_packets = cpu_stats->rx_packets;
673 rx_bytes = cpu_stats->rx_bytes;
674 tx_packets = cpu_stats->tx_packets;
675 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700676 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300677
willy tarreau74c41b02014-01-16 08:20:08 +0100678 stats->rx_packets += rx_packets;
679 stats->rx_bytes += rx_bytes;
680 stats->tx_packets += tx_packets;
681 stats->tx_bytes += tx_bytes;
682 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300683
684 stats->rx_errors = dev->stats.rx_errors;
685 stats->rx_dropped = dev->stats.rx_dropped;
686
687 stats->tx_dropped = dev->stats.tx_dropped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300688}
689
690/* Rx descriptors helper methods */
691
willy tarreau54282132014-01-16 08:20:14 +0100692/* Checks whether the RX descriptor having this status is both the first
693 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300694 * received through a single RX descriptor, so not having each RX
695 * descriptor with its first and last bits set is an error
696 */
willy tarreau54282132014-01-16 08:20:14 +0100697static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300698{
willy tarreau54282132014-01-16 08:20:14 +0100699 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300700 MVNETA_RXD_FIRST_LAST_DESC;
701}
702
703/* Add number of descriptors ready to receive new packets */
704static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
705 struct mvneta_rx_queue *rxq,
706 int ndescs)
707{
708 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100709 * be added at once
710 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300711 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
712 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
713 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
714 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
715 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
716 }
717
718 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
719 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
720}
721
722/* Get number of RX descriptors occupied by received packets */
723static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
724 struct mvneta_rx_queue *rxq)
725{
726 u32 val;
727
728 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
729 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
730}
731
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100732/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300733 * from mvneta_rxq_drop_pkts().
734 */
735static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
736 struct mvneta_rx_queue *rxq,
737 int rx_done, int rx_filled)
738{
739 u32 val;
740
741 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
742 val = rx_done |
743 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
744 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
745 return;
746 }
747
748 /* Only 255 descriptors can be added at once */
749 while ((rx_done > 0) || (rx_filled > 0)) {
750 if (rx_done <= 0xff) {
751 val = rx_done;
752 rx_done = 0;
753 } else {
754 val = 0xff;
755 rx_done -= 0xff;
756 }
757 if (rx_filled <= 0xff) {
758 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
759 rx_filled = 0;
760 } else {
761 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
762 rx_filled -= 0xff;
763 }
764 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
765 }
766}
767
768/* Get pointer to next RX descriptor to be processed by SW */
769static struct mvneta_rx_desc *
770mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
771{
772 int rx_desc = rxq->next_desc_to_proc;
773
774 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100775 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300776 return rxq->descs + rx_desc;
777}
778
779/* Change maximum receive size of the port. */
780static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
781{
782 u32 val;
783
784 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
785 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
786 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
787 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
788 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
789}
790
791
792/* Set rx queue offset */
793static void mvneta_rxq_offset_set(struct mvneta_port *pp,
794 struct mvneta_rx_queue *rxq,
795 int offset)
796{
797 u32 val;
798
799 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
800 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
801
802 /* Offset is in */
803 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
804 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
805}
806
807
808/* Tx descriptors helper methods */
809
810/* Update HW with number of TX descriptors to be sent */
811static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
812 struct mvneta_tx_queue *txq,
813 int pend_desc)
814{
815 u32 val;
816
Simon Guinot0d637852017-11-13 16:27:02 +0100817 pend_desc += txq->pending;
818
819 /* Only 255 Tx descriptors can be added at once */
820 do {
821 val = min(pend_desc, 255);
822 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
823 pend_desc -= val;
824 } while (pend_desc > 0);
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100825 txq->pending = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300826}
827
828/* Get pointer to next TX descriptor to be processed (send) by HW */
829static struct mvneta_tx_desc *
830mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
831{
832 int tx_desc = txq->next_desc_to_proc;
833
834 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
835 return txq->descs + tx_desc;
836}
837
838/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100839 * mapping failures in the TX path.
840 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300841static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
842{
843 if (txq->next_desc_to_proc == 0)
844 txq->next_desc_to_proc = txq->last_desc - 1;
845 else
846 txq->next_desc_to_proc--;
847}
848
849/* Set rxq buf size */
850static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
851 struct mvneta_rx_queue *rxq,
852 int buf_size)
853{
854 u32 val;
855
856 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
857
858 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
859 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
860
861 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
862}
863
864/* Disable buffer management (BM) */
865static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
866 struct mvneta_rx_queue *rxq)
867{
868 u32 val;
869
870 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
871 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
872 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
873}
874
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100875/* Enable buffer management (BM) */
876static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
877 struct mvneta_rx_queue *rxq)
878{
879 u32 val;
880
881 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
882 val |= MVNETA_RXQ_HW_BUF_ALLOC;
883 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
884}
885
886/* Notify HW about port's assignment of pool for bigger packets */
887static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
888 struct mvneta_rx_queue *rxq)
889{
890 u32 val;
891
892 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
893 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
894 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
895
896 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
897}
898
899/* Notify HW about port's assignment of pool for smaller packets */
900static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
901 struct mvneta_rx_queue *rxq)
902{
903 u32 val;
904
905 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
906 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
907 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
908
909 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
910}
911
912/* Set port's receive buffer size for assigned BM pool */
913static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
914 int buf_size,
915 u8 pool_id)
916{
917 u32 val;
918
919 if (!IS_ALIGNED(buf_size, 8)) {
920 dev_warn(pp->dev->dev.parent,
921 "illegal buf_size value %d, round to %d\n",
922 buf_size, ALIGN(buf_size, 8));
923 buf_size = ALIGN(buf_size, 8);
924 }
925
926 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
927 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
928 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
929}
930
931/* Configure MBUS window in order to enable access BM internal SRAM */
932static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
933 u8 target, u8 attr)
934{
935 u32 win_enable, win_protect;
936 int i;
937
938 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
939
940 if (pp->bm_win_id < 0) {
941 /* Find first not occupied window */
942 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
943 if (win_enable & (1 << i)) {
944 pp->bm_win_id = i;
945 break;
946 }
947 }
948 if (i == MVNETA_MAX_DECODE_WIN)
949 return -ENOMEM;
950 } else {
951 i = pp->bm_win_id;
952 }
953
954 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
955 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
956
957 if (i < 4)
958 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
959
960 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
961 (attr << 8) | target);
962
963 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
964
965 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
966 win_protect |= 3 << (2 * i);
967 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
968
969 win_enable &= ~(1 << i);
970 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
971
972 return 0;
973}
974
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100975static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100976{
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100977 u32 wsize;
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100978 u8 target, attr;
979 int err;
980
981 /* Get BM window information */
982 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
983 &target, &attr);
984 if (err < 0)
985 return err;
986
987 pp->bm_win_id = -1;
988
989 /* Open NETA -> BM window */
990 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
991 target, attr);
992 if (err < 0) {
993 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
994 return err;
995 }
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100996 return 0;
997}
998
999/* Assign and initialize pools for port. In case of fail
1000 * buffer manager will remain disabled for current port.
1001 */
1002static int mvneta_bm_port_init(struct platform_device *pdev,
1003 struct mvneta_port *pp)
1004{
1005 struct device_node *dn = pdev->dev.of_node;
1006 u32 long_pool_id, short_pool_id;
1007
1008 if (!pp->neta_armada3700) {
1009 int ret;
1010
1011 ret = mvneta_bm_port_mbus_init(pp);
1012 if (ret)
1013 return ret;
1014 }
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001015
1016 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1017 netdev_info(pp->dev, "missing long pool id\n");
1018 return -EINVAL;
1019 }
1020
1021 /* Create port's long pool depending on mtu */
1022 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1023 MVNETA_BM_LONG, pp->id,
1024 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1025 if (!pp->pool_long) {
1026 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1027 return -ENOMEM;
1028 }
1029
1030 pp->pool_long->port_map |= 1 << pp->id;
1031
1032 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1033 pp->pool_long->id);
1034
1035 /* If short pool id is not defined, assume using single pool */
1036 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1037 short_pool_id = long_pool_id;
1038
1039 /* Create port's short pool */
1040 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1041 MVNETA_BM_SHORT, pp->id,
1042 MVNETA_BM_SHORT_PKT_SIZE);
1043 if (!pp->pool_short) {
1044 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1045 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1046 return -ENOMEM;
1047 }
1048
1049 if (short_pool_id != long_pool_id) {
1050 pp->pool_short->port_map |= 1 << pp->id;
1051 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1052 pp->pool_short->id);
1053 }
1054
1055 return 0;
1056}
1057
1058/* Update settings of a pool for bigger packets */
1059static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1060{
1061 struct mvneta_bm_pool *bm_pool = pp->pool_long;
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001062 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001063 int num;
1064
1065 /* Release all buffers from long pool */
1066 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001067 if (hwbm_pool->buf_num) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001068 WARN(1, "cannot free all buffers in pool %d\n",
1069 bm_pool->id);
1070 goto bm_mtu_err;
1071 }
1072
1073 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1074 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001075 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1076 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001077
1078 /* Fill entire long pool */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001079 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1080 if (num != hwbm_pool->size) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001081 WARN(1, "pool %d: %d of %d allocated\n",
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001082 bm_pool->id, num, hwbm_pool->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001083 goto bm_mtu_err;
1084 }
1085 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1086
1087 return;
1088
1089bm_mtu_err:
1090 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1091 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1092
1093 pp->bm_priv = NULL;
1094 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1095 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1096}
1097
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001098/* Start the Ethernet port RX and TX activity */
1099static void mvneta_port_up(struct mvneta_port *pp)
1100{
1101 int queue;
1102 u32 q_map;
1103
1104 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001105 q_map = 0;
1106 for (queue = 0; queue < txq_number; queue++) {
1107 struct mvneta_tx_queue *txq = &pp->txqs[queue];
Markus Elfringf95936c2017-04-16 22:45:33 +02001108 if (txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001109 q_map |= (1 << queue);
1110 }
1111 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1112
1113 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001114 for (queue = 0; queue < rxq_number; queue++) {
1115 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1116
Markus Elfringf95936c2017-04-16 22:45:33 +02001117 if (rxq->descs)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001118 q_map |= (1 << queue);
1119 }
1120 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001121}
1122
1123/* Stop the Ethernet port activity */
1124static void mvneta_port_down(struct mvneta_port *pp)
1125{
1126 u32 val;
1127 int count;
1128
1129 /* Stop Rx port activity. Check port Rx activity. */
1130 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1131
1132 /* Issue stop command for active channels only */
1133 if (val != 0)
1134 mvreg_write(pp, MVNETA_RXQ_CMD,
1135 val << MVNETA_RXQ_DISABLE_SHIFT);
1136
1137 /* Wait for all Rx activity to terminate. */
1138 count = 0;
1139 do {
1140 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1141 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001142 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001143 val);
1144 break;
1145 }
1146 mdelay(1);
1147
1148 val = mvreg_read(pp, MVNETA_RXQ_CMD);
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001149 } while (val & MVNETA_RXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001150
1151 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001152 * command for active channels only
1153 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001154 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1155
1156 if (val != 0)
1157 mvreg_write(pp, MVNETA_TXQ_CMD,
1158 (val << MVNETA_TXQ_DISABLE_SHIFT));
1159
1160 /* Wait for all Tx activity to terminate. */
1161 count = 0;
1162 do {
1163 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1164 netdev_warn(pp->dev,
1165 "TIMEOUT for TX stopped status=0x%08x\n",
1166 val);
1167 break;
1168 }
1169 mdelay(1);
1170
1171 /* Check TX Command reg that all Txqs are stopped */
1172 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1173
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001174 } while (val & MVNETA_TXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001175
1176 /* Double check to verify that TX FIFO is empty */
1177 count = 0;
1178 do {
1179 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1180 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001181 "TX FIFO empty timeout status=0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001182 val);
1183 break;
1184 }
1185 mdelay(1);
1186
1187 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1188 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1189 (val & MVNETA_TX_IN_PRGRS));
1190
1191 udelay(200);
1192}
1193
1194/* Enable the port by setting the port enable bit of the MAC control register */
1195static void mvneta_port_enable(struct mvneta_port *pp)
1196{
1197 u32 val;
1198
1199 /* Enable port */
1200 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1201 val |= MVNETA_GMAC0_PORT_ENABLE;
1202 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1203}
1204
1205/* Disable the port and wait for about 200 usec before retuning */
1206static void mvneta_port_disable(struct mvneta_port *pp)
1207{
1208 u32 val;
1209
1210 /* Reset the Enable bit in the Serial Control Register */
1211 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1212 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1213 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1214
1215 udelay(200);
1216}
1217
1218/* Multicast tables methods */
1219
1220/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1221static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1222{
1223 int offset;
1224 u32 val;
1225
1226 if (queue == -1) {
1227 val = 0;
1228 } else {
1229 val = 0x1 | (queue << 1);
1230 val |= (val << 24) | (val << 16) | (val << 8);
1231 }
1232
1233 for (offset = 0; offset <= 0xc; offset += 4)
1234 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1235}
1236
1237/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1238static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1239{
1240 int offset;
1241 u32 val;
1242
1243 if (queue == -1) {
1244 val = 0;
1245 } else {
1246 val = 0x1 | (queue << 1);
1247 val |= (val << 24) | (val << 16) | (val << 8);
1248 }
1249
1250 for (offset = 0; offset <= 0xfc; offset += 4)
1251 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1252
1253}
1254
1255/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1256static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1257{
1258 int offset;
1259 u32 val;
1260
1261 if (queue == -1) {
1262 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1263 val = 0;
1264 } else {
1265 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1266 val = 0x1 | (queue << 1);
1267 val |= (val << 24) | (val << 16) | (val << 8);
1268 }
1269
1270 for (offset = 0; offset <= 0xfc; offset += 4)
1271 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1272}
1273
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001274static void mvneta_percpu_unmask_interrupt(void *arg)
1275{
1276 struct mvneta_port *pp = arg;
1277
1278 /* All the queue are unmasked, but actually only the ones
1279 * mapped to this CPU will be unmasked
1280 */
1281 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1282 MVNETA_RX_INTR_MASK_ALL |
1283 MVNETA_TX_INTR_MASK_ALL |
1284 MVNETA_MISCINTR_INTR_MASK);
1285}
1286
1287static void mvneta_percpu_mask_interrupt(void *arg)
1288{
1289 struct mvneta_port *pp = arg;
1290
1291 /* All the queue are masked, but actually only the ones
1292 * mapped to this CPU will be masked
1293 */
1294 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1295 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1296 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1297}
1298
1299static void mvneta_percpu_clear_intr_cause(void *arg)
1300{
1301 struct mvneta_port *pp = arg;
1302
1303 /* All the queue are cleared, but actually only the ones
1304 * mapped to this CPU will be cleared
1305 */
1306 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1307 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1308 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1309}
1310
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001311/* This method sets defaults to the NETA port:
1312 * Clears interrupt Cause and Mask registers.
1313 * Clears all MAC tables.
1314 * Sets defaults to all registers.
1315 * Resets RX and TX descriptor rings.
1316 * Resets PHY.
1317 * This method can be called after mvneta_port_down() to return the port
1318 * settings to defaults.
1319 */
1320static void mvneta_defaults_set(struct mvneta_port *pp)
1321{
1322 int cpu;
1323 int queue;
1324 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001325 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001326
1327 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001328 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001329
1330 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001331 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001332 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1333
1334 /* Enable MBUS Retry bit16 */
1335 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1336
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001337 /* Set CPU queue access map. CPUs are assigned to the RX and
1338 * TX queues modulo their number. If there is only one TX
1339 * queue then it is assigned to the CPU associated to the
1340 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001341 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001342 for_each_present_cpu(cpu) {
1343 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001344 int rxq, txq;
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001345 if (!pp->neta_armada3700) {
1346 for (rxq = 0; rxq < rxq_number; rxq++)
1347 if ((rxq % max_cpu) == cpu)
1348 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001349
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001350 for (txq = 0; txq < txq_number; txq++)
1351 if ((txq % max_cpu) == cpu)
1352 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001353
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001354 /* With only one TX queue we configure a special case
1355 * which will allow to get all the irq on a single
1356 * CPU
1357 */
1358 if (txq_number == 1)
1359 txq_map = (cpu == pp->rxq_def) ?
1360 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001361
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001362 } else {
1363 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1364 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1365 }
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001366
1367 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1368 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001369
1370 /* Reset RX and TX DMAs */
1371 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1372 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1373
1374 /* Disable Legacy WRR, Disable EJP, Release from reset */
1375 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1376 for (queue = 0; queue < txq_number; queue++) {
1377 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1378 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1379 }
1380
1381 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1382 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1383
1384 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001385 if (pp->bm_priv)
1386 /* HW buffer management + legacy parser */
1387 val = MVNETA_ACC_MODE_EXT2;
1388 else
1389 /* SW buffer management + legacy parser */
1390 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001391 mvreg_write(pp, MVNETA_ACC_MODE, val);
1392
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001393 if (pp->bm_priv)
1394 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1395
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001396 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001397 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001398 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1399
1400 val = 0;
1401 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1402 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1403
1404 /* Build PORT_SDMA_CONFIG_REG */
1405 val = 0;
1406
1407 /* Default burst size */
1408 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1409 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001410 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001411
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001412#if defined(__BIG_ENDIAN)
1413 val |= MVNETA_DESC_SWAP;
1414#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001415
1416 /* Assign port SDMA configuration */
1417 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1418
Thomas Petazzoni71408602013-09-04 16:21:18 +02001419 /* Disable PHY polling in hardware, since we're using the
1420 * kernel phylib to do this.
1421 */
1422 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1423 val &= ~MVNETA_PHY_POLLING_ENABLE;
1424 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1425
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001426 mvneta_set_ucast_table(pp, -1);
1427 mvneta_set_special_mcast_table(pp, -1);
1428 mvneta_set_other_mcast_table(pp, -1);
1429
1430 /* Set port interrupt enable register - default enable all */
1431 mvreg_write(pp, MVNETA_INTR_ENABLE,
1432 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1433 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001434
1435 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001436}
1437
1438/* Set max sizes for tx queues */
1439static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1440
1441{
1442 u32 val, size, mtu;
1443 int queue;
1444
1445 mtu = max_tx_size * 8;
1446 if (mtu > MVNETA_TX_MTU_MAX)
1447 mtu = MVNETA_TX_MTU_MAX;
1448
1449 /* Set MTU */
1450 val = mvreg_read(pp, MVNETA_TX_MTU);
1451 val &= ~MVNETA_TX_MTU_MAX;
1452 val |= mtu;
1453 mvreg_write(pp, MVNETA_TX_MTU, val);
1454
1455 /* TX token size and all TXQs token size must be larger that MTU */
1456 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1457
1458 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1459 if (size < mtu) {
1460 size = mtu;
1461 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1462 val |= size;
1463 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1464 }
1465 for (queue = 0; queue < txq_number; queue++) {
1466 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1467
1468 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1469 if (size < mtu) {
1470 size = mtu;
1471 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1472 val |= size;
1473 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1474 }
1475 }
1476}
1477
1478/* Set unicast address */
1479static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1480 int queue)
1481{
1482 unsigned int unicast_reg;
1483 unsigned int tbl_offset;
1484 unsigned int reg_offset;
1485
1486 /* Locate the Unicast table entry */
1487 last_nibble = (0xf & last_nibble);
1488
1489 /* offset from unicast tbl base */
1490 tbl_offset = (last_nibble / 4) * 4;
1491
1492 /* offset within the above reg */
1493 reg_offset = last_nibble % 4;
1494
1495 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1496
1497 if (queue == -1) {
1498 /* Clear accepts frame bit at specified unicast DA tbl entry */
1499 unicast_reg &= ~(0xff << (8 * reg_offset));
1500 } else {
1501 unicast_reg &= ~(0xff << (8 * reg_offset));
1502 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1503 }
1504
1505 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1506}
1507
1508/* Set mac address */
1509static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1510 int queue)
1511{
1512 unsigned int mac_h;
1513 unsigned int mac_l;
1514
1515 if (queue != -1) {
1516 mac_l = (addr[4] << 8) | (addr[5]);
1517 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1518 (addr[2] << 8) | (addr[3] << 0);
1519
1520 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1521 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1522 }
1523
1524 /* Accept frames of this address */
1525 mvneta_set_ucast_addr(pp, addr[5], queue);
1526}
1527
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001528/* Set the number of packets that will be received before RX interrupt
1529 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001530 */
1531static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1532 struct mvneta_rx_queue *rxq, u32 value)
1533{
1534 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1535 value | MVNETA_RXQ_NON_OCCUPIED(0));
1536 rxq->pkts_coal = value;
1537}
1538
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001539/* Set the time delay in usec before RX interrupt will be generated by
1540 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001541 */
1542static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1543 struct mvneta_rx_queue *rxq, u32 value)
1544{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001545 u32 val;
1546 unsigned long clk_rate;
1547
1548 clk_rate = clk_get_rate(pp->clk);
1549 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001550
1551 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1552 rxq->time_coal = value;
1553}
1554
1555/* Set threshold for TX_DONE pkts coalescing */
1556static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1557 struct mvneta_tx_queue *txq, u32 value)
1558{
1559 u32 val;
1560
1561 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1562
1563 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1564 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1565
1566 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1567
1568 txq->done_pkts_coal = value;
1569}
1570
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001571/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1572static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001573 u32 phys_addr, void *virt_addr,
1574 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001575{
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001576 int i;
1577
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001578 rx_desc->buf_phys_addr = phys_addr;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001579 i = rx_desc - rxq->descs;
1580 rxq->buf_virt_addr[i] = virt_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001581}
1582
1583/* Decrement sent descriptors counter */
1584static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1585 struct mvneta_tx_queue *txq,
1586 int sent_desc)
1587{
1588 u32 val;
1589
1590 /* Only 255 TX descriptors can be updated at once */
1591 while (sent_desc > 0xff) {
1592 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1593 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1594 sent_desc = sent_desc - 0xff;
1595 }
1596
1597 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1598 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1599}
1600
1601/* Get number of TX descriptors already sent by HW */
1602static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1603 struct mvneta_tx_queue *txq)
1604{
1605 u32 val;
1606 int sent_desc;
1607
1608 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1609 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1610 MVNETA_TXQ_SENT_DESC_SHIFT;
1611
1612 return sent_desc;
1613}
1614
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001615/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001616 * The number of sent descriptors is returned.
1617 */
1618static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1619 struct mvneta_tx_queue *txq)
1620{
1621 int sent_desc;
1622
1623 /* Get number of sent descriptors */
1624 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1625
1626 /* Decrement sent descriptors counter */
1627 if (sent_desc)
1628 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1629
1630 return sent_desc;
1631}
1632
1633/* Set TXQ descriptors fields relevant for CSUM calculation */
1634static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1635 int ip_hdr_len, int l4_proto)
1636{
1637 u32 command;
1638
1639 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001640 * G_L4_chk, L4_type; required only for checksum
1641 * calculation
1642 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001643 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1644 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1645
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001646 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001647 command |= MVNETA_TXD_IP_CSUM;
1648 else
1649 command |= MVNETA_TX_L3_IP6;
1650
1651 if (l4_proto == IPPROTO_TCP)
1652 command |= MVNETA_TX_L4_CSUM_FULL;
1653 else if (l4_proto == IPPROTO_UDP)
1654 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1655 else
1656 command |= MVNETA_TX_L4_CSUM_NOT;
1657
1658 return command;
1659}
1660
1661
1662/* Display more error info */
1663static void mvneta_rx_error(struct mvneta_port *pp,
1664 struct mvneta_rx_desc *rx_desc)
1665{
1666 u32 status = rx_desc->status;
1667
willy tarreau54282132014-01-16 08:20:14 +01001668 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001669 netdev_err(pp->dev,
1670 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001671 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001672 return;
1673 }
1674
1675 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1676 case MVNETA_RXD_ERR_CRC:
1677 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1678 status, rx_desc->data_size);
1679 break;
1680 case MVNETA_RXD_ERR_OVERRUN:
1681 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1682 status, rx_desc->data_size);
1683 break;
1684 case MVNETA_RXD_ERR_LEN:
1685 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1686 status, rx_desc->data_size);
1687 break;
1688 case MVNETA_RXD_ERR_RESOURCE:
1689 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1690 status, rx_desc->data_size);
1691 break;
1692 }
1693}
1694
willy tarreau54282132014-01-16 08:20:14 +01001695/* Handle RX checksum offload based on the descriptor's status */
1696static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001697 struct sk_buff *skb)
1698{
willy tarreau54282132014-01-16 08:20:14 +01001699 if ((status & MVNETA_RXD_L3_IP4) &&
1700 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001701 skb->csum = 0;
1702 skb->ip_summed = CHECKSUM_UNNECESSARY;
1703 return;
1704 }
1705
1706 skb->ip_summed = CHECKSUM_NONE;
1707}
1708
willy tarreau6c498972014-01-16 08:20:12 +01001709/* Return tx queue pointer (find last set bit) according to <cause> returned
1710 * form tx_done reg. <cause> must not be null. The return value is always a
1711 * valid queue for matching the first one found in <cause>.
1712 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001713static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1714 u32 cause)
1715{
1716 int queue = fls(cause) - 1;
1717
willy tarreau6c498972014-01-16 08:20:12 +01001718 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001719}
1720
1721/* Free tx queue skbuffs */
1722static void mvneta_txq_bufs_free(struct mvneta_port *pp,
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001723 struct mvneta_tx_queue *txq, int num,
1724 struct netdev_queue *nq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001725{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001726 unsigned int bytes_compl = 0, pkts_compl = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001727 int i;
1728
1729 for (i = 0; i < num; i++) {
1730 struct mvneta_tx_desc *tx_desc = txq->descs +
1731 txq->txq_get_index;
1732 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1733
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001734 if (skb) {
1735 bytes_compl += skb->len;
1736 pkts_compl++;
1737 }
1738
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001739 mvneta_txq_inc_get(txq);
1740
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001741 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1742 dma_unmap_single(pp->dev->dev.parent,
1743 tx_desc->buf_phys_addr,
1744 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001745 if (!skb)
1746 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001747 dev_kfree_skb_any(skb);
1748 }
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001749
1750 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001751}
1752
1753/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001754static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001755 struct mvneta_tx_queue *txq)
1756{
1757 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1758 int tx_done;
1759
1760 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001761 if (!tx_done)
1762 return;
1763
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001764 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001765
1766 txq->count -= tx_done;
1767
1768 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001769 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001770 netif_tx_wake_queue(nq);
1771 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001772}
1773
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001774void *mvneta_frag_alloc(unsigned int frag_size)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001775{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001776 if (likely(frag_size <= PAGE_SIZE))
1777 return netdev_alloc_frag(frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001778 else
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001779 return kmalloc(frag_size, GFP_ATOMIC);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001780}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001781EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001782
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001783void mvneta_frag_free(unsigned int frag_size, void *data)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001784{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001785 if (likely(frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001786 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001787 else
1788 kfree(data);
1789}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001790EXPORT_SYMBOL_GPL(mvneta_frag_free);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001791
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001792/* Refill processing for SW buffer management */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001793static int mvneta_rx_refill(struct mvneta_port *pp,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001794 struct mvneta_rx_desc *rx_desc,
1795 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001796
1797{
1798 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001799 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001800
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001801 data = mvneta_frag_alloc(pp->frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001802 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001803 return -ENOMEM;
1804
willy tarreau8ec2cd42014-01-16 08:20:16 +01001805 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001806 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1807 DMA_FROM_DEVICE);
1808 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001809 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001810 return -ENOMEM;
1811 }
1812
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01001813 phys_addr += pp->rx_offset_correction;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001814 mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001815 return 0;
1816}
1817
1818/* Handle tx checksum */
1819static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1820{
1821 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1822 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001823 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001824 u8 l4_proto;
1825
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001826 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001827 struct iphdr *ip4h = ip_hdr(skb);
1828
1829 /* Calculate IPv4 checksum and L4 checksum */
1830 ip_hdr_len = ip4h->ihl;
1831 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001832 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001833 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1834
1835 /* Read l4_protocol from one of IPv6 extra headers */
1836 if (skb_network_header_len(skb) > 0)
1837 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1838 l4_proto = ip6h->nexthdr;
1839 } else
1840 return MVNETA_TX_L4_CSUM_NOT;
1841
1842 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001843 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001844 }
1845
1846 return MVNETA_TX_L4_CSUM_NOT;
1847}
1848
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001849/* Drop packets received by the RXQ and free buffers */
1850static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1851 struct mvneta_rx_queue *rxq)
1852{
1853 int rx_done, i;
1854
1855 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001856 if (rx_done)
1857 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1858
1859 if (pp->bm_priv) {
1860 for (i = 0; i < rx_done; i++) {
1861 struct mvneta_rx_desc *rx_desc =
1862 mvneta_rxq_next_desc_get(rxq);
1863 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1864 struct mvneta_bm_pool *bm_pool;
1865
1866 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1867 /* Return dropped buffer to the pool */
1868 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1869 rx_desc->buf_phys_addr);
1870 }
1871 return;
1872 }
1873
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001874 for (i = 0; i < rxq->size; i++) {
1875 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001876 void *data = rxq->buf_virt_addr[i];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001877
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001878 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001879 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001880 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001881 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001882}
1883
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001884/* Main rx processing when using software buffer management */
1885static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1886 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001887{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001888 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001889 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001890 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001891 u32 rcvd_pkts = 0;
1892 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001893
1894 /* Get number of received packets */
1895 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1896
1897 if (rx_todo > rx_done)
1898 rx_todo = rx_done;
1899
1900 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001901
1902 /* Fairness NAPI loop */
1903 while (rx_done < rx_todo) {
1904 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1905 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001906 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001907 dma_addr_t phys_addr;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001908 u32 rx_status, frag_size;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001909 int rx_bytes, err, index;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001910
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001911 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001912 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001913 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001914 index = rx_desc - rxq->descs;
1915 data = rxq->buf_virt_addr[index];
Simon Guinotdaf158d2015-09-15 22:41:21 +02001916 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001917
willy tarreau54282132014-01-16 08:20:14 +01001918 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001919 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
Yelena Krivosheev2eecb2e2017-12-19 17:59:47 +01001920 mvneta_rx_error(pp, rx_desc);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001921err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001922 dev->stats.rx_errors++;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001923 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001924 continue;
1925 }
1926
willy tarreauf19fadf2014-01-16 08:20:17 +01001927 if (rx_bytes <= rx_copybreak) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001928 /* better copy a small frame and not unmap the DMA region */
willy tarreauf19fadf2014-01-16 08:20:17 +01001929 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1930 if (unlikely(!skb))
1931 goto err_drop_frame;
1932
1933 dma_sync_single_range_for_cpu(dev->dev.parent,
Gregory CLEMENTac83b7d2016-12-01 18:03:04 +01001934 phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001935 MVNETA_MH_SIZE + NET_SKB_PAD,
1936 rx_bytes,
1937 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02001938 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
1939 rx_bytes);
willy tarreauf19fadf2014-01-16 08:20:17 +01001940
1941 skb->protocol = eth_type_trans(skb, dev);
1942 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001943 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001944
1945 rcvd_pkts++;
1946 rcvd_bytes += rx_bytes;
1947
1948 /* leave the descriptor and buffer untouched */
1949 continue;
1950 }
1951
Simon Guinota84e3282015-07-19 13:00:53 +02001952 /* Refill processing */
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001953 err = mvneta_rx_refill(pp, rx_desc, rxq);
Simon Guinota84e3282015-07-19 13:00:53 +02001954 if (err) {
1955 netdev_err(dev, "Linux processing - Can't refill\n");
1956 rxq->missed++;
1957 goto err_drop_frame;
1958 }
1959
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001960 frag_size = pp->frag_size;
1961
1962 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01001963
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001964 /* After refill old buffer has to be unmapped regardless
1965 * the skb is successfully built or not.
1966 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02001967 dma_unmap_single(dev->dev.parent, phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001968 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1969 DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001970
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001971 if (!skb)
1972 goto err_drop_frame;
1973
willy tarreaudc4277d2014-01-16 08:20:07 +01001974 rcvd_pkts++;
1975 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001976
1977 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001978 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001979 skb_put(skb, rx_bytes);
1980
1981 skb->protocol = eth_type_trans(skb, dev);
1982
willy tarreau54282132014-01-16 08:20:14 +01001983 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001984
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001985 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001986 }
1987
willy tarreaudc4277d2014-01-16 08:20:07 +01001988 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001989 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1990
1991 u64_stats_update_begin(&stats->syncp);
1992 stats->rx_packets += rcvd_pkts;
1993 stats->rx_bytes += rcvd_bytes;
1994 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001995 }
1996
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001997 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02001998 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001999
2000 return rx_done;
2001}
2002
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002003/* Main rx processing when using hardware buffer management */
2004static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2005 struct mvneta_rx_queue *rxq)
2006{
2007 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2008 struct net_device *dev = pp->dev;
2009 int rx_done;
2010 u32 rcvd_pkts = 0;
2011 u32 rcvd_bytes = 0;
2012
2013 /* Get number of received packets */
2014 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2015
2016 if (rx_todo > rx_done)
2017 rx_todo = rx_done;
2018
2019 rx_done = 0;
2020
2021 /* Fairness NAPI loop */
2022 while (rx_done < rx_todo) {
2023 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2024 struct mvneta_bm_pool *bm_pool = NULL;
2025 struct sk_buff *skb;
2026 unsigned char *data;
2027 dma_addr_t phys_addr;
2028 u32 rx_status, frag_size;
2029 int rx_bytes, err;
2030 u8 pool_id;
2031
2032 rx_done++;
2033 rx_status = rx_desc->status;
2034 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002035 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002036 phys_addr = rx_desc->buf_phys_addr;
2037 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2038 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2039
2040 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2041 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2042err_drop_frame_ret_pool:
2043 /* Return the buffer to the pool */
2044 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2045 rx_desc->buf_phys_addr);
2046err_drop_frame:
2047 dev->stats.rx_errors++;
2048 mvneta_rx_error(pp, rx_desc);
2049 /* leave the descriptor untouched */
2050 continue;
2051 }
2052
2053 if (rx_bytes <= rx_copybreak) {
2054 /* better copy a small frame and not unmap the DMA region */
2055 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2056 if (unlikely(!skb))
2057 goto err_drop_frame_ret_pool;
2058
2059 dma_sync_single_range_for_cpu(dev->dev.parent,
2060 rx_desc->buf_phys_addr,
2061 MVNETA_MH_SIZE + NET_SKB_PAD,
2062 rx_bytes,
2063 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02002064 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2065 rx_bytes);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002066
2067 skb->protocol = eth_type_trans(skb, dev);
2068 mvneta_rx_csum(pp, rx_status, skb);
2069 napi_gro_receive(&port->napi, skb);
2070
2071 rcvd_pkts++;
2072 rcvd_bytes += rx_bytes;
2073
2074 /* Return the buffer to the pool */
2075 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2076 rx_desc->buf_phys_addr);
2077
2078 /* leave the descriptor and buffer untouched */
2079 continue;
2080 }
2081
2082 /* Refill processing */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002083 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002084 if (err) {
2085 netdev_err(dev, "Linux processing - Can't refill\n");
2086 rxq->missed++;
2087 goto err_drop_frame_ret_pool;
2088 }
2089
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002090 frag_size = bm_pool->hwbm_pool.frag_size;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002091
2092 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2093
2094 /* After refill old buffer has to be unmapped regardless
2095 * the skb is successfully built or not.
2096 */
2097 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2098 bm_pool->buf_size, DMA_FROM_DEVICE);
2099 if (!skb)
2100 goto err_drop_frame;
2101
2102 rcvd_pkts++;
2103 rcvd_bytes += rx_bytes;
2104
2105 /* Linux processing */
2106 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2107 skb_put(skb, rx_bytes);
2108
2109 skb->protocol = eth_type_trans(skb, dev);
2110
2111 mvneta_rx_csum(pp, rx_status, skb);
2112
2113 napi_gro_receive(&port->napi, skb);
2114 }
2115
2116 if (rcvd_pkts) {
2117 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2118
2119 u64_stats_update_begin(&stats->syncp);
2120 stats->rx_packets += rcvd_pkts;
2121 stats->rx_bytes += rcvd_bytes;
2122 u64_stats_update_end(&stats->syncp);
2123 }
2124
2125 /* Update rxq management counters */
2126 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2127
2128 return rx_done;
2129}
2130
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002131static inline void
2132mvneta_tso_put_hdr(struct sk_buff *skb,
2133 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2134{
2135 struct mvneta_tx_desc *tx_desc;
2136 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2137
2138 txq->tx_skb[txq->txq_put_index] = NULL;
2139 tx_desc = mvneta_txq_next_desc_get(txq);
2140 tx_desc->data_size = hdr_len;
2141 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2142 tx_desc->command |= MVNETA_TXD_F_DESC;
2143 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2144 txq->txq_put_index * TSO_HEADER_SIZE;
2145 mvneta_txq_inc_put(txq);
2146}
2147
2148static inline int
2149mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2150 struct sk_buff *skb, char *data, int size,
2151 bool last_tcp, bool is_last)
2152{
2153 struct mvneta_tx_desc *tx_desc;
2154
2155 tx_desc = mvneta_txq_next_desc_get(txq);
2156 tx_desc->data_size = size;
2157 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2158 size, DMA_TO_DEVICE);
2159 if (unlikely(dma_mapping_error(dev->dev.parent,
2160 tx_desc->buf_phys_addr))) {
2161 mvneta_txq_desc_put(txq);
2162 return -ENOMEM;
2163 }
2164
2165 tx_desc->command = 0;
2166 txq->tx_skb[txq->txq_put_index] = NULL;
2167
2168 if (last_tcp) {
2169 /* last descriptor in the TCP packet */
2170 tx_desc->command = MVNETA_TXD_L_DESC;
2171
2172 /* last descriptor in SKB */
2173 if (is_last)
2174 txq->tx_skb[txq->txq_put_index] = skb;
2175 }
2176 mvneta_txq_inc_put(txq);
2177 return 0;
2178}
2179
2180static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2181 struct mvneta_tx_queue *txq)
2182{
2183 int total_len, data_left;
2184 int desc_count = 0;
2185 struct mvneta_port *pp = netdev_priv(dev);
2186 struct tso_t tso;
2187 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2188 int i;
2189
2190 /* Count needed descriptors */
2191 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2192 return 0;
2193
2194 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2195 pr_info("*** Is this even possible???!?!?\n");
2196 return 0;
2197 }
2198
2199 /* Initialize the TSO handler, and prepare the first payload */
2200 tso_start(skb, &tso);
2201
2202 total_len = skb->len - hdr_len;
2203 while (total_len > 0) {
2204 char *hdr;
2205
2206 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2207 total_len -= data_left;
2208 desc_count++;
2209
2210 /* prepare packet headers: MAC + IP + TCP */
2211 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2212 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2213
2214 mvneta_tso_put_hdr(skb, pp, txq);
2215
2216 while (data_left > 0) {
2217 int size;
2218 desc_count++;
2219
2220 size = min_t(int, tso.size, data_left);
2221
2222 if (mvneta_tso_put_data(dev, txq, skb,
2223 tso.data, size,
2224 size == data_left,
2225 total_len == 0))
2226 goto err_release;
2227 data_left -= size;
2228
2229 tso_build_data(skb, &tso, size);
2230 }
2231 }
2232
2233 return desc_count;
2234
2235err_release:
2236 /* Release all used data descriptors; header descriptors must not
2237 * be DMA-unmapped.
2238 */
2239 for (i = desc_count - 1; i >= 0; i--) {
2240 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002241 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002242 dma_unmap_single(pp->dev->dev.parent,
2243 tx_desc->buf_phys_addr,
2244 tx_desc->data_size,
2245 DMA_TO_DEVICE);
2246 mvneta_txq_desc_put(txq);
2247 }
2248 return 0;
2249}
2250
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002251/* Handle tx fragmentation processing */
2252static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2253 struct mvneta_tx_queue *txq)
2254{
2255 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002256 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002257
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002258 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002259 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2260 void *addr = page_address(frag->page.p) + frag->page_offset;
2261
2262 tx_desc = mvneta_txq_next_desc_get(txq);
2263 tx_desc->data_size = frag->size;
2264
2265 tx_desc->buf_phys_addr =
2266 dma_map_single(pp->dev->dev.parent, addr,
2267 tx_desc->data_size, DMA_TO_DEVICE);
2268
2269 if (dma_mapping_error(pp->dev->dev.parent,
2270 tx_desc->buf_phys_addr)) {
2271 mvneta_txq_desc_put(txq);
2272 goto error;
2273 }
2274
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002275 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002276 /* Last descriptor */
2277 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002278 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002279 } else {
2280 /* Descriptor in the middle: Not First, Not Last */
2281 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002282 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002283 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002284 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002285 }
2286
2287 return 0;
2288
2289error:
2290 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002291 * this packet, as well as the corresponding DMA mappings
2292 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002293 for (i = i - 1; i >= 0; i--) {
2294 tx_desc = txq->descs + i;
2295 dma_unmap_single(pp->dev->dev.parent,
2296 tx_desc->buf_phys_addr,
2297 tx_desc->data_size,
2298 DMA_TO_DEVICE);
2299 mvneta_txq_desc_put(txq);
2300 }
2301
2302 return -ENOMEM;
2303}
2304
2305/* Main tx processing */
2306static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2307{
2308 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002309 u16 txq_id = skb_get_queue_mapping(skb);
2310 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002311 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002312 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002313 int frags = 0;
2314 u32 tx_cmd;
2315
2316 if (!netif_running(dev))
2317 goto out;
2318
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002319 if (skb_is_gso(skb)) {
2320 frags = mvneta_tx_tso(skb, dev, txq);
2321 goto out;
2322 }
2323
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002324 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002325
2326 /* Get a descriptor for the first part of the packet */
2327 tx_desc = mvneta_txq_next_desc_get(txq);
2328
2329 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2330
2331 tx_desc->data_size = skb_headlen(skb);
2332
2333 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2334 tx_desc->data_size,
2335 DMA_TO_DEVICE);
2336 if (unlikely(dma_mapping_error(dev->dev.parent,
2337 tx_desc->buf_phys_addr))) {
2338 mvneta_txq_desc_put(txq);
2339 frags = 0;
2340 goto out;
2341 }
2342
2343 if (frags == 1) {
2344 /* First and Last descriptor */
2345 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2346 tx_desc->command = tx_cmd;
2347 txq->tx_skb[txq->txq_put_index] = skb;
2348 mvneta_txq_inc_put(txq);
2349 } else {
2350 /* First but not Last */
2351 tx_cmd |= MVNETA_TXD_F_DESC;
2352 txq->tx_skb[txq->txq_put_index] = NULL;
2353 mvneta_txq_inc_put(txq);
2354 tx_desc->command = tx_cmd;
2355 /* Continue with other skb fragments */
2356 if (mvneta_tx_frag_process(pp, skb, txq)) {
2357 dma_unmap_single(dev->dev.parent,
2358 tx_desc->buf_phys_addr,
2359 tx_desc->data_size,
2360 DMA_TO_DEVICE);
2361 mvneta_txq_desc_put(txq);
2362 frags = 0;
2363 goto out;
2364 }
2365 }
2366
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002367out:
2368 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01002369 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002370 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2371
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002372 netdev_tx_sent_queue(nq, len);
2373
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002374 txq->count += frags;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002375 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002376 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002377
Simon Guinot2a90f7e2017-01-16 18:08:31 +01002378 if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2379 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2380 mvneta_txq_pend_desc_add(pp, txq, frags);
2381 else
2382 txq->pending += frags;
2383
willy tarreau74c41b02014-01-16 08:20:08 +01002384 u64_stats_update_begin(&stats->syncp);
2385 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002386 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01002387 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002388 } else {
2389 dev->stats.tx_dropped++;
2390 dev_kfree_skb_any(skb);
2391 }
2392
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002393 return NETDEV_TX_OK;
2394}
2395
2396
2397/* Free tx resources, when resetting a port */
2398static void mvneta_txq_done_force(struct mvneta_port *pp,
2399 struct mvneta_tx_queue *txq)
2400
2401{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002402 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002403 int tx_done = txq->count;
2404
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002405 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002406
2407 /* reset txq */
2408 txq->count = 0;
2409 txq->txq_put_index = 0;
2410 txq->txq_get_index = 0;
2411}
2412
willy tarreau6c498972014-01-16 08:20:12 +01002413/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2414 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2415 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002416static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002417{
2418 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002419 struct netdev_queue *nq;
2420
willy tarreau6c498972014-01-16 08:20:12 +01002421 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002422 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002423
2424 nq = netdev_get_tx_queue(pp->dev, txq->id);
2425 __netif_tx_lock(nq, smp_processor_id());
2426
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002427 if (txq->count)
2428 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002429
2430 __netif_tx_unlock(nq);
2431 cause_tx_done &= ~((1 << txq->id));
2432 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002433}
2434
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002435/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002436 * according to hw spec, different than generic crc8 algorithm
2437 */
2438static int mvneta_addr_crc(unsigned char *addr)
2439{
2440 int crc = 0;
2441 int i;
2442
2443 for (i = 0; i < ETH_ALEN; i++) {
2444 int j;
2445
2446 crc = (crc ^ addr[i]) << 8;
2447 for (j = 7; j >= 0; j--) {
2448 if (crc & (0x100 << j))
2449 crc ^= 0x107 << j;
2450 }
2451 }
2452
2453 return crc;
2454}
2455
2456/* This method controls the net device special MAC multicast support.
2457 * The Special Multicast Table for MAC addresses supports MAC of the form
2458 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2459 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2460 * Table entries in the DA-Filter table. This method set the Special
2461 * Multicast Table appropriate entry.
2462 */
2463static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2464 unsigned char last_byte,
2465 int queue)
2466{
2467 unsigned int smc_table_reg;
2468 unsigned int tbl_offset;
2469 unsigned int reg_offset;
2470
2471 /* Register offset from SMC table base */
2472 tbl_offset = (last_byte / 4);
2473 /* Entry offset within the above reg */
2474 reg_offset = last_byte % 4;
2475
2476 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2477 + tbl_offset * 4));
2478
2479 if (queue == -1)
2480 smc_table_reg &= ~(0xff << (8 * reg_offset));
2481 else {
2482 smc_table_reg &= ~(0xff << (8 * reg_offset));
2483 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2484 }
2485
2486 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2487 smc_table_reg);
2488}
2489
2490/* This method controls the network device Other MAC multicast support.
2491 * The Other Multicast Table is used for multicast of another type.
2492 * A CRC-8 is used as an index to the Other Multicast Table entries
2493 * in the DA-Filter table.
2494 * The method gets the CRC-8 value from the calling routine and
2495 * sets the Other Multicast Table appropriate entry according to the
2496 * specified CRC-8 .
2497 */
2498static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2499 unsigned char crc8,
2500 int queue)
2501{
2502 unsigned int omc_table_reg;
2503 unsigned int tbl_offset;
2504 unsigned int reg_offset;
2505
2506 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2507 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2508
2509 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2510
2511 if (queue == -1) {
2512 /* Clear accepts frame bit at specified Other DA table entry */
2513 omc_table_reg &= ~(0xff << (8 * reg_offset));
2514 } else {
2515 omc_table_reg &= ~(0xff << (8 * reg_offset));
2516 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2517 }
2518
2519 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2520}
2521
2522/* The network device supports multicast using two tables:
2523 * 1) Special Multicast Table for MAC addresses of the form
2524 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2525 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2526 * Table entries in the DA-Filter table.
2527 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2528 * is used as an index to the Other Multicast Table entries in the
2529 * DA-Filter table.
2530 */
2531static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2532 int queue)
2533{
2534 unsigned char crc_result = 0;
2535
2536 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2537 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2538 return 0;
2539 }
2540
2541 crc_result = mvneta_addr_crc(p_addr);
2542 if (queue == -1) {
2543 if (pp->mcast_count[crc_result] == 0) {
2544 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2545 crc_result);
2546 return -EINVAL;
2547 }
2548
2549 pp->mcast_count[crc_result]--;
2550 if (pp->mcast_count[crc_result] != 0) {
2551 netdev_info(pp->dev,
2552 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2553 pp->mcast_count[crc_result], crc_result);
2554 return -EINVAL;
2555 }
2556 } else
2557 pp->mcast_count[crc_result]++;
2558
2559 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2560
2561 return 0;
2562}
2563
2564/* Configure Fitering mode of Ethernet port */
2565static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2566 int is_promisc)
2567{
2568 u32 port_cfg_reg, val;
2569
2570 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2571
2572 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2573
2574 /* Set / Clear UPM bit in port configuration register */
2575 if (is_promisc) {
2576 /* Accept all Unicast addresses */
2577 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2578 val |= MVNETA_FORCE_UNI;
2579 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2580 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2581 } else {
2582 /* Reject all Unicast addresses */
2583 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2584 val &= ~MVNETA_FORCE_UNI;
2585 }
2586
2587 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2588 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2589}
2590
2591/* register unicast and multicast addresses */
2592static void mvneta_set_rx_mode(struct net_device *dev)
2593{
2594 struct mvneta_port *pp = netdev_priv(dev);
2595 struct netdev_hw_addr *ha;
2596
2597 if (dev->flags & IFF_PROMISC) {
2598 /* Accept all: Multicast + Unicast */
2599 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002600 mvneta_set_ucast_table(pp, pp->rxq_def);
2601 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2602 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002603 } else {
2604 /* Accept single Unicast */
2605 mvneta_rx_unicast_promisc_set(pp, 0);
2606 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002607 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002608
2609 if (dev->flags & IFF_ALLMULTI) {
2610 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002611 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2612 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002613 } else {
2614 /* Accept only initialized multicast */
2615 mvneta_set_special_mcast_table(pp, -1);
2616 mvneta_set_other_mcast_table(pp, -1);
2617
2618 if (!netdev_mc_empty(dev)) {
2619 netdev_for_each_mc_addr(ha, dev) {
2620 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002621 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002622 }
2623 }
2624 }
2625 }
2626}
2627
2628/* Interrupt handling - the callback for request_irq() */
2629static irqreturn_t mvneta_isr(int irq, void *dev_id)
2630{
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002631 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2632
2633 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2634 napi_schedule(&pp->napi);
2635
2636 return IRQ_HANDLED;
2637}
2638
2639/* Interrupt handling - the callback for request_percpu_irq() */
2640static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2641{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002642 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002643
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002644 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002645 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002646
2647 return IRQ_HANDLED;
2648}
2649
Russell King503f9aa92018-01-02 17:24:44 +00002650static void mvneta_link_change(struct mvneta_port *pp)
Stas Sergeev898b29702015-04-01 20:32:49 +03002651{
Stas Sergeev898b29702015-04-01 20:32:49 +03002652 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2653
Russell King503f9aa92018-01-02 17:24:44 +00002654 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
Stas Sergeev898b29702015-04-01 20:32:49 +03002655}
2656
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002657/* NAPI handler
2658 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2659 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2660 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2661 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2662 * Each CPU has its own causeRxTx register
2663 */
2664static int mvneta_poll(struct napi_struct *napi, int budget)
2665{
2666 int rx_done = 0;
2667 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002668 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002669 struct mvneta_port *pp = netdev_priv(napi->dev);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002670 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002671
2672 if (!netif_running(pp->dev)) {
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002673 napi_complete(napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002674 return rx_done;
2675 }
2676
2677 /* Read cause register */
Stas Sergeev898b29702015-04-01 20:32:49 +03002678 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2679 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2680 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2681
2682 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
Russell King503f9aa92018-01-02 17:24:44 +00002683
2684 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2685 MVNETA_CAUSE_LINK_CHANGE |
2686 MVNETA_CAUSE_PSC_SYNC_CHANGE))
2687 mvneta_link_change(pp);
Stas Sergeev898b29702015-04-01 20:32:49 +03002688 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002689
2690 /* Release Tx descriptors */
2691 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002692 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002693 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2694 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002695
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002696 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002697 * RX packets
2698 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002699 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2700
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002701 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2702 port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002703
2704 if (rx_queue) {
2705 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002706 if (pp->bm_priv)
2707 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2708 else
2709 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002710 }
2711
Eric Dumazet6ad20162017-01-30 08:22:01 -08002712 if (rx_done < budget) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002713 cause_rx_tx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002714 napi_complete_done(napi, rx_done);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002715
2716 if (pp->neta_armada3700) {
2717 unsigned long flags;
2718
2719 local_irq_save(flags);
2720 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2721 MVNETA_RX_INTR_MASK(rxq_number) |
2722 MVNETA_TX_INTR_MASK(txq_number) |
2723 MVNETA_MISCINTR_INTR_MASK);
2724 local_irq_restore(flags);
2725 } else {
2726 enable_percpu_irq(pp->dev->irq, 0);
2727 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002728 }
2729
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002730 if (pp->neta_armada3700)
2731 pp->cause_rx_tx = cause_rx_tx;
2732 else
2733 port->cause_rx_tx = cause_rx_tx;
2734
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002735 return rx_done;
2736}
2737
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002738/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2739static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2740 int num)
2741{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002742 int i;
2743
2744 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002745 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002746 if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002747 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002748 __func__, rxq->id, i, num);
2749 break;
2750 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002751 }
2752
2753 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002754 * get packets)
2755 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002756 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2757
2758 return i;
2759}
2760
2761/* Free all packets pending transmit from all TXQs and reset TX port */
2762static void mvneta_tx_reset(struct mvneta_port *pp)
2763{
2764 int queue;
2765
Ezequiel Garcia96728502014-05-22 20:06:59 -03002766 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002767 for (queue = 0; queue < txq_number; queue++)
2768 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2769
2770 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2771 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2772}
2773
2774static void mvneta_rx_reset(struct mvneta_port *pp)
2775{
2776 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2777 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2778}
2779
2780/* Rx/Tx queue initialization/cleanup methods */
2781
2782/* Create a specified RX queue */
2783static int mvneta_rxq_init(struct mvneta_port *pp,
2784 struct mvneta_rx_queue *rxq)
2785
2786{
2787 rxq->size = pp->rx_ring_size;
2788
2789 /* Allocate memory for RX descriptors */
2790 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2791 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2792 &rxq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002793 if (!rxq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002794 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002795
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002796 rxq->last_desc = rxq->size - 1;
2797
2798 /* Set Rx descriptors queue starting address */
2799 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2800 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2801
2802 /* Set Offset */
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01002803 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002804
2805 /* Set coalescing pkts and time */
2806 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2807 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2808
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002809 if (!pp->bm_priv) {
2810 /* Fill RXQ with buffers from RX pool */
2811 mvneta_rxq_buf_size_set(pp, rxq,
2812 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2813 mvneta_rxq_bm_disable(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002814 mvneta_rxq_fill(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002815 } else {
2816 mvneta_rxq_bm_enable(pp, rxq);
2817 mvneta_rxq_long_pool_set(pp, rxq);
2818 mvneta_rxq_short_pool_set(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002819 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002820 }
2821
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002822 return 0;
2823}
2824
2825/* Cleanup Rx queue */
2826static void mvneta_rxq_deinit(struct mvneta_port *pp,
2827 struct mvneta_rx_queue *rxq)
2828{
2829 mvneta_rxq_drop_pkts(pp, rxq);
2830
2831 if (rxq->descs)
2832 dma_free_coherent(pp->dev->dev.parent,
2833 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2834 rxq->descs,
2835 rxq->descs_phys);
2836
2837 rxq->descs = NULL;
2838 rxq->last_desc = 0;
2839 rxq->next_desc_to_proc = 0;
2840 rxq->descs_phys = 0;
2841}
2842
2843/* Create and initialize a tx queue */
2844static int mvneta_txq_init(struct mvneta_port *pp,
2845 struct mvneta_tx_queue *txq)
2846{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002847 int cpu;
2848
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002849 txq->size = pp->tx_ring_size;
2850
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002851 /* A queue must always have room for at least one skb.
2852 * Therefore, stop the queue when the free entries reaches
2853 * the maximum number of descriptors per skb.
2854 */
2855 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2856 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2857
2858
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002859 /* Allocate memory for TX descriptors */
2860 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2861 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2862 &txq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002863 if (!txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002864 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002865
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002866 txq->last_desc = txq->size - 1;
2867
2868 /* Set maximum bandwidth for enabled TXQs */
2869 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2870 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2871
2872 /* Set Tx descriptors queue starting address */
2873 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2874 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2875
Markus Elfringd441b682017-04-16 22:11:22 +02002876 txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
2877 GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002878 if (!txq->tx_skb) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002879 dma_free_coherent(pp->dev->dev.parent,
2880 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2881 txq->descs, txq->descs_phys);
2882 return -ENOMEM;
2883 }
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002884
2885 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2886 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2887 txq->size * TSO_HEADER_SIZE,
2888 &txq->tso_hdrs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002889 if (!txq->tso_hdrs) {
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002890 kfree(txq->tx_skb);
2891 dma_free_coherent(pp->dev->dev.parent,
2892 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2893 txq->descs, txq->descs_phys);
2894 return -ENOMEM;
2895 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002896 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2897
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002898 /* Setup XPS mapping */
2899 if (txq_number > 1)
2900 cpu = txq->id % num_present_cpus();
2901 else
2902 cpu = pp->rxq_def % num_present_cpus();
2903 cpumask_set_cpu(cpu, &txq->affinity_mask);
2904 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2905
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002906 return 0;
2907}
2908
2909/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2910static void mvneta_txq_deinit(struct mvneta_port *pp,
2911 struct mvneta_tx_queue *txq)
2912{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002913 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2914
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002915 kfree(txq->tx_skb);
2916
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002917 if (txq->tso_hdrs)
2918 dma_free_coherent(pp->dev->dev.parent,
2919 txq->size * TSO_HEADER_SIZE,
2920 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002921 if (txq->descs)
2922 dma_free_coherent(pp->dev->dev.parent,
2923 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2924 txq->descs, txq->descs_phys);
2925
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002926 netdev_tx_reset_queue(nq);
2927
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002928 txq->descs = NULL;
2929 txq->last_desc = 0;
2930 txq->next_desc_to_proc = 0;
2931 txq->descs_phys = 0;
2932
2933 /* Set minimum bandwidth for disabled TXQs */
2934 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2935 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2936
2937 /* Set Tx descriptors queue starting address and size */
2938 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2939 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2940}
2941
2942/* Cleanup all Tx queues */
2943static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2944{
2945 int queue;
2946
2947 for (queue = 0; queue < txq_number; queue++)
2948 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2949}
2950
2951/* Cleanup all Rx queues */
2952static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2953{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002954 int queue;
2955
Yelena Krivosheevca5902a2017-12-19 17:59:46 +01002956 for (queue = 0; queue < rxq_number; queue++)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002957 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002958}
2959
2960
2961/* Init all Rx queues */
2962static int mvneta_setup_rxqs(struct mvneta_port *pp)
2963{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002964 int queue;
2965
2966 for (queue = 0; queue < rxq_number; queue++) {
2967 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2968
2969 if (err) {
2970 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2971 __func__, queue);
2972 mvneta_cleanup_rxqs(pp);
2973 return err;
2974 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002975 }
2976
2977 return 0;
2978}
2979
2980/* Init all tx queues */
2981static int mvneta_setup_txqs(struct mvneta_port *pp)
2982{
2983 int queue;
2984
2985 for (queue = 0; queue < txq_number; queue++) {
2986 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2987 if (err) {
2988 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2989 __func__, queue);
2990 mvneta_cleanup_txqs(pp);
2991 return err;
2992 }
2993 }
2994
2995 return 0;
2996}
2997
2998static void mvneta_start_dev(struct mvneta_port *pp)
2999{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003000 int cpu;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003001
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003002 mvneta_max_rx_size_set(pp, pp->pkt_size);
3003 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3004
3005 /* start the Rx/Tx activity */
3006 mvneta_port_enable(pp);
3007
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003008 if (!pp->neta_armada3700) {
3009 /* Enable polling on the port */
3010 for_each_online_cpu(cpu) {
3011 struct mvneta_pcpu_port *port =
3012 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003013
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003014 napi_enable(&port->napi);
3015 }
3016 } else {
3017 napi_enable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003018 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003019
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003020 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003021 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3022
Stas Sergeev898b29702015-04-01 20:32:49 +03003023 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3024 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3025 MVNETA_CAUSE_LINK_CHANGE |
3026 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003027
Russell King503f9aa92018-01-02 17:24:44 +00003028 phylink_start(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003029 netif_tx_start_all_queues(pp->dev);
3030}
3031
3032static void mvneta_stop_dev(struct mvneta_port *pp)
3033{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003034 unsigned int cpu;
3035
Russell King503f9aa92018-01-02 17:24:44 +00003036 phylink_stop(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003037
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003038 if (!pp->neta_armada3700) {
3039 for_each_online_cpu(cpu) {
3040 struct mvneta_pcpu_port *port =
3041 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003042
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003043 napi_disable(&port->napi);
3044 }
3045 } else {
3046 napi_disable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003047 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003048
3049 netif_carrier_off(pp->dev);
3050
3051 mvneta_port_down(pp);
3052 netif_tx_stop_all_queues(pp->dev);
3053
3054 /* Stop the port activity */
3055 mvneta_port_disable(pp);
3056
3057 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003058 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003059
3060 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003061 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003062
3063 mvneta_tx_reset(pp);
3064 mvneta_rx_reset(pp);
3065}
3066
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003067static void mvneta_percpu_enable(void *arg)
3068{
3069 struct mvneta_port *pp = arg;
3070
3071 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3072}
3073
3074static void mvneta_percpu_disable(void *arg)
3075{
3076 struct mvneta_port *pp = arg;
3077
3078 disable_percpu_irq(pp->dev->irq);
3079}
3080
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003081/* Change the device mtu */
3082static int mvneta_change_mtu(struct net_device *dev, int mtu)
3083{
3084 struct mvneta_port *pp = netdev_priv(dev);
3085 int ret;
3086
Jarod Wilson57779872016-10-17 15:54:06 -04003087 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3088 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3089 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3090 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3091 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003092
3093 dev->mtu = mtu;
3094
Simon Guinotb65657f2015-06-30 16:20:22 +02003095 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003096 if (pp->bm_priv)
3097 mvneta_bm_update_mtu(pp, mtu);
3098
Simon Guinotb65657f2015-06-30 16:20:22 +02003099 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003100 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003101 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003102
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003103 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003104 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003105 */
3106 mvneta_stop_dev(pp);
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003107 on_each_cpu(mvneta_percpu_disable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003108
3109 mvneta_cleanup_txqs(pp);
3110 mvneta_cleanup_rxqs(pp);
3111
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003112 if (pp->bm_priv)
3113 mvneta_bm_update_mtu(pp, mtu);
3114
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003115 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003116 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3117 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003118
3119 ret = mvneta_setup_rxqs(pp);
3120 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003121 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003122 return ret;
3123 }
3124
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003125 ret = mvneta_setup_txqs(pp);
3126 if (ret) {
3127 netdev_err(dev, "unable to setup txqs after MTU change\n");
3128 return ret;
3129 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003130
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003131 on_each_cpu(mvneta_percpu_enable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003132 mvneta_start_dev(pp);
3133 mvneta_port_up(pp);
3134
Simon Guinotb65657f2015-06-30 16:20:22 +02003135 netdev_update_features(dev);
3136
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003137 return 0;
3138}
3139
Simon Guinotb65657f2015-06-30 16:20:22 +02003140static netdev_features_t mvneta_fix_features(struct net_device *dev,
3141 netdev_features_t features)
3142{
3143 struct mvneta_port *pp = netdev_priv(dev);
3144
3145 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3146 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3147 netdev_info(dev,
3148 "Disable IP checksum for MTU greater than %dB\n",
3149 pp->tx_csum_limit);
3150 }
3151
3152 return features;
3153}
3154
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003155/* Get mac address */
3156static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3157{
3158 u32 mac_addr_l, mac_addr_h;
3159
3160 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3161 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3162 addr[0] = (mac_addr_h >> 24) & 0xFF;
3163 addr[1] = (mac_addr_h >> 16) & 0xFF;
3164 addr[2] = (mac_addr_h >> 8) & 0xFF;
3165 addr[3] = mac_addr_h & 0xFF;
3166 addr[4] = (mac_addr_l >> 8) & 0xFF;
3167 addr[5] = mac_addr_l & 0xFF;
3168}
3169
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003170/* Handle setting mac address */
3171static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3172{
3173 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003174 struct sockaddr *sockaddr = addr;
3175 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003176
Ezequiel Garciae68de362014-05-22 20:07:00 -03003177 ret = eth_prepare_mac_addr_change(dev, addr);
3178 if (ret < 0)
3179 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003180 /* Remove previous address table entry */
3181 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3182
3183 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003184 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003185
Ezequiel Garciae68de362014-05-22 20:07:00 -03003186 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003187 return 0;
3188}
3189
Russell King503f9aa92018-01-02 17:24:44 +00003190static void mvneta_validate(struct net_device *ndev, unsigned long *supported,
3191 struct phylink_link_state *state)
Russell Kingfc548b92018-01-02 17:24:39 +00003192{
Russell King503f9aa92018-01-02 17:24:44 +00003193 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
Russell Kingfc548b92018-01-02 17:24:39 +00003194
Russell King503f9aa92018-01-02 17:24:44 +00003195 /* We only support QSGMII, SGMII and RGMII modes */
3196 if (state->interface != PHY_INTERFACE_MODE_NA &&
3197 state->interface != PHY_INTERFACE_MODE_QSGMII &&
3198 state->interface != PHY_INTERFACE_MODE_SGMII &&
3199 !phy_interface_mode_is_rgmii(state->interface)) {
3200 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3201 return;
Russell Kingfc548b92018-01-02 17:24:39 +00003202 }
Russell King503f9aa92018-01-02 17:24:44 +00003203
3204 /* Allow all the expected bits */
3205 phylink_set(mask, Autoneg);
3206 phylink_set_port_modes(mask);
3207
3208 /* Half-duplex at speeds higher than 100Mbit is unsupported */
3209 phylink_set(mask, 1000baseT_Full);
3210 phylink_set(mask, 1000baseX_Full);
3211 phylink_set(mask, 10baseT_Half);
3212 phylink_set(mask, 10baseT_Full);
3213 phylink_set(mask, 100baseT_Half);
3214 phylink_set(mask, 100baseT_Full);
3215
3216 bitmap_and(supported, supported, mask,
3217 __ETHTOOL_LINK_MODE_MASK_NBITS);
3218 bitmap_and(state->advertising, state->advertising, mask,
3219 __ETHTOOL_LINK_MODE_MASK_NBITS);
Russell Kingfc548b92018-01-02 17:24:39 +00003220}
3221
Russell King503f9aa92018-01-02 17:24:44 +00003222static int mvneta_mac_link_state(struct net_device *ndev,
3223 struct phylink_link_state *state)
3224{
3225 struct mvneta_port *pp = netdev_priv(ndev);
3226 u32 gmac_stat;
3227
3228 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3229
3230 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3231 state->speed = SPEED_1000;
3232 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3233 state->speed = SPEED_100;
3234 else
3235 state->speed = SPEED_10;
3236
3237 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3238 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3239 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3240
3241 state->pause = 0;
3242
3243 return 1;
3244}
3245
3246static void mvneta_mac_config(struct net_device *ndev, unsigned int mode,
3247 const struct phylink_link_state *state)
3248{
3249 struct mvneta_port *pp = netdev_priv(ndev);
3250 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3251 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3252 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3253
3254 new_ctrl2 = gmac_ctrl2 & ~MVNETA_GMAC2_INBAND_AN_ENABLE;
3255 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3256 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3257 MVNETA_GMAC_INBAND_RESTART_AN |
3258 MVNETA_GMAC_CONFIG_MII_SPEED |
3259 MVNETA_GMAC_CONFIG_GMII_SPEED |
3260 MVNETA_GMAC_AN_SPEED_EN |
3261 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3262 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
3263 MVNETA_GMAC_AN_DUPLEX_EN);
3264
3265 if (!phylink_autoneg_inband(mode)) {
3266 /* Phy or fixed speed */
3267 if (state->duplex)
3268 new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3269
3270 if (state->speed == SPEED_1000)
3271 new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3272 else if (state->speed == SPEED_100)
3273 new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
3274 } else {
3275 /* SGMII mode receives the state from the PHY */
3276 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3277 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3278 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3279 MVNETA_GMAC_FORCE_LINK_PASS)) |
3280 MVNETA_GMAC_INBAND_AN_ENABLE |
3281 MVNETA_GMAC_AN_SPEED_EN |
3282 MVNETA_GMAC_AN_DUPLEX_EN;
3283 }
3284
3285 /* Armada 370 documentation says we can only change the port mode
3286 * and in-band enable when the link is down, so force it down
3287 * while making these changes. We also do this for GMAC_CTRL2 */
3288 if ((new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
3289 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3290 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3291 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3292 MVNETA_GMAC_FORCE_LINK_DOWN);
3293 }
3294
3295 if (new_ctrl2 != gmac_ctrl2)
3296 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
3297 if (new_clk != gmac_clk)
3298 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3299 if (new_an != gmac_an)
3300 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
3301}
3302
3303static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode)
Russell Kingfc548b92018-01-02 17:24:39 +00003304{
3305 struct mvneta_port *pp = netdev_priv(ndev);
3306 u32 val;
3307
Russell King503f9aa92018-01-02 17:24:44 +00003308 mvneta_port_down(pp);
3309
3310 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003311 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3312 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3313 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3314 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3315 }
Russell Kingfc548b92018-01-02 17:24:39 +00003316}
3317
Russell King503f9aa92018-01-02 17:24:44 +00003318static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode,
3319 struct phy_device *phy)
Russell Kingfc548b92018-01-02 17:24:39 +00003320{
3321 struct mvneta_port *pp = netdev_priv(ndev);
3322 u32 val;
3323
Russell King503f9aa92018-01-02 17:24:44 +00003324 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003325 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3326 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3327 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3328 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3329 }
3330
3331 mvneta_port_up(pp);
3332}
3333
Russell King503f9aa92018-01-02 17:24:44 +00003334static const struct phylink_mac_ops mvneta_phylink_ops = {
3335 .validate = mvneta_validate,
3336 .mac_link_state = mvneta_mac_link_state,
3337 .mac_config = mvneta_mac_config,
3338 .mac_link_down = mvneta_mac_link_down,
3339 .mac_link_up = mvneta_mac_link_up,
3340};
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003341
3342static int mvneta_mdio_probe(struct mvneta_port *pp)
3343{
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003344 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Russell King503f9aa92018-01-02 17:24:44 +00003345 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003346
Russell King503f9aa92018-01-02 17:24:44 +00003347 if (err)
3348 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003349
Russell King503f9aa92018-01-02 17:24:44 +00003350 phylink_ethtool_get_wol(pp->phylink, &wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003351 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
3352
Russell King503f9aa92018-01-02 17:24:44 +00003353 return err;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003354}
3355
3356static void mvneta_mdio_remove(struct mvneta_port *pp)
3357{
Russell King503f9aa92018-01-02 17:24:44 +00003358 phylink_disconnect_phy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003359}
3360
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003361/* Electing a CPU must be done in an atomic way: it should be done
3362 * after or before the removal/insertion of a CPU and this function is
3363 * not reentrant.
3364 */
Maxime Ripardf8642882015-09-25 18:09:38 +02003365static void mvneta_percpu_elect(struct mvneta_port *pp)
3366{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003367 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003368
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003369 /* Use the cpu associated to the rxq when it is online, in all
3370 * the other cases, use the cpu 0 which can't be offline.
3371 */
3372 if (cpu_online(pp->rxq_def))
3373 elected_cpu = pp->rxq_def;
3374
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003375 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02003376
3377 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003378 int rxq_map = 0, txq_map = 0;
3379 int rxq;
3380
3381 for (rxq = 0; rxq < rxq_number; rxq++)
3382 if ((rxq % max_cpu) == cpu)
3383 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3384
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003385 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003386 /* Map the default receive queue queue to the
3387 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02003388 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003389 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003390
3391 /* We update the TX queue map only if we have one
3392 * queue. In this case we associate the TX queue to
3393 * the CPU bound to the default RX queue
3394 */
3395 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003396 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003397 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3398 else
3399 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3400 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3401
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003402 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3403
3404 /* Update the interrupt mask on each CPU according the
3405 * new mapping
3406 */
3407 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3408 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003409 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003410
Maxime Ripardf8642882015-09-25 18:09:38 +02003411 }
3412};
3413
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003414static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
Maxime Ripardf8642882015-09-25 18:09:38 +02003415{
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003416 int other_cpu;
3417 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3418 node_online);
Maxime Ripardf8642882015-09-25 18:09:38 +02003419 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3420
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003421
3422 spin_lock(&pp->lock);
3423 /*
3424 * Configuring the driver for a new CPU while the driver is
3425 * stopping is racy, so just avoid it.
3426 */
3427 if (pp->is_stopped) {
3428 spin_unlock(&pp->lock);
3429 return 0;
3430 }
3431 netif_tx_stop_all_queues(pp->dev);
3432
3433 /*
3434 * We have to synchronise on tha napi of each CPU except the one
3435 * just being woken up
3436 */
3437 for_each_online_cpu(other_cpu) {
3438 if (other_cpu != cpu) {
3439 struct mvneta_pcpu_port *other_port =
3440 per_cpu_ptr(pp->ports, other_cpu);
3441
3442 napi_synchronize(&other_port->napi);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003443 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003444 }
3445
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003446 /* Mask all ethernet port interrupts */
3447 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3448 napi_enable(&port->napi);
3449
3450 /*
3451 * Enable per-CPU interrupts on the CPU that is
3452 * brought up.
3453 */
3454 mvneta_percpu_enable(pp);
3455
3456 /*
3457 * Enable per-CPU interrupt on the one CPU we care
3458 * about.
3459 */
3460 mvneta_percpu_elect(pp);
3461
3462 /* Unmask all ethernet port interrupts */
3463 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3464 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3465 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3466 MVNETA_CAUSE_LINK_CHANGE |
3467 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3468 netif_tx_start_all_queues(pp->dev);
3469 spin_unlock(&pp->lock);
3470 return 0;
3471}
3472
3473static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3474{
3475 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3476 node_online);
3477 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3478
3479 /*
3480 * Thanks to this lock we are sure that any pending cpu election is
3481 * done.
3482 */
3483 spin_lock(&pp->lock);
3484 /* Mask all ethernet port interrupts */
3485 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3486 spin_unlock(&pp->lock);
3487
3488 napi_synchronize(&port->napi);
3489 napi_disable(&port->napi);
3490 /* Disable per-CPU interrupts on the CPU that is brought down. */
3491 mvneta_percpu_disable(pp);
3492 return 0;
3493}
3494
3495static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3496{
3497 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3498 node_dead);
3499
3500 /* Check if a new CPU must be elected now this on is down */
3501 spin_lock(&pp->lock);
3502 mvneta_percpu_elect(pp);
3503 spin_unlock(&pp->lock);
3504 /* Unmask all ethernet port interrupts */
3505 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3506 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3507 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3508 MVNETA_CAUSE_LINK_CHANGE |
3509 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3510 netif_tx_start_all_queues(pp->dev);
3511 return 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003512}
3513
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003514static int mvneta_open(struct net_device *dev)
3515{
3516 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003517 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003518
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003519 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003520 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3521 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003522
3523 ret = mvneta_setup_rxqs(pp);
3524 if (ret)
3525 return ret;
3526
3527 ret = mvneta_setup_txqs(pp);
3528 if (ret)
3529 goto err_cleanup_rxqs;
3530
3531 /* Connect to port interrupt line */
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003532 if (pp->neta_armada3700)
3533 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3534 dev->name, pp);
3535 else
3536 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3537 dev->name, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003538 if (ret) {
3539 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3540 goto err_cleanup_txqs;
3541 }
3542
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003543 if (!pp->neta_armada3700) {
3544 /* Enable per-CPU interrupt on all the CPU to handle our RX
3545 * queue interrupts
3546 */
3547 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003548
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003549 pp->is_stopped = false;
3550 /* Register a CPU notifier to handle the case where our CPU
3551 * might be taken offline.
3552 */
3553 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3554 &pp->node_online);
3555 if (ret)
3556 goto err_free_irq;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003557
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003558 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3559 &pp->node_dead);
3560 if (ret)
3561 goto err_free_online_hp;
3562 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003563
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003564 /* In default link is down */
3565 netif_carrier_off(pp->dev);
3566
3567 ret = mvneta_mdio_probe(pp);
3568 if (ret < 0) {
3569 netdev_err(dev, "cannot probe MDIO bus\n");
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003570 goto err_free_dead_hp;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003571 }
3572
3573 mvneta_start_dev(pp);
3574
3575 return 0;
3576
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003577err_free_dead_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003578 if (!pp->neta_armada3700)
3579 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3580 &pp->node_dead);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003581err_free_online_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003582 if (!pp->neta_armada3700)
3583 cpuhp_state_remove_instance_nocalls(online_hpstate,
3584 &pp->node_online);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003585err_free_irq:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003586 if (pp->neta_armada3700) {
3587 free_irq(pp->dev->irq, pp);
3588 } else {
3589 on_each_cpu(mvneta_percpu_disable, pp, true);
3590 free_percpu_irq(pp->dev->irq, pp->ports);
3591 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003592err_cleanup_txqs:
3593 mvneta_cleanup_txqs(pp);
3594err_cleanup_rxqs:
3595 mvneta_cleanup_rxqs(pp);
3596 return ret;
3597}
3598
3599/* Stop the port, free port interrupt line */
3600static int mvneta_stop(struct net_device *dev)
3601{
3602 struct mvneta_port *pp = netdev_priv(dev);
3603
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003604 if (!pp->neta_armada3700) {
3605 /* Inform that we are stopping so we don't want to setup the
3606 * driver for new CPUs in the notifiers. The code of the
3607 * notifier for CPU online is protected by the same spinlock,
3608 * so when we get the lock, the notifer work is done.
3609 */
3610 spin_lock(&pp->lock);
3611 pp->is_stopped = true;
3612 spin_unlock(&pp->lock);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003613
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003614 mvneta_stop_dev(pp);
3615 mvneta_mdio_remove(pp);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003616
Dan Carpenterd26aac22016-12-07 14:32:17 +03003617 cpuhp_state_remove_instance_nocalls(online_hpstate,
3618 &pp->node_online);
3619 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3620 &pp->node_dead);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003621 on_each_cpu(mvneta_percpu_disable, pp, true);
3622 free_percpu_irq(dev->irq, pp->ports);
3623 } else {
3624 mvneta_stop_dev(pp);
3625 mvneta_mdio_remove(pp);
3626 free_irq(dev->irq, pp);
3627 }
3628
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003629 mvneta_cleanup_rxqs(pp);
3630 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003631
3632 return 0;
3633}
3634
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003635static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3636{
Russell King503f9aa92018-01-02 17:24:44 +00003637 struct mvneta_port *pp = netdev_priv(dev);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003638
Russell King503f9aa92018-01-02 17:24:44 +00003639 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003640}
3641
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003642/* Ethtool methods */
3643
Philippe Reynes013ad402016-07-30 17:42:12 +02003644/* Set link ksettings (phy address, speed) for ethtools */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +08003645static int
3646mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3647 const struct ethtool_link_ksettings *cmd)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003648{
Philippe Reynes013ad402016-07-30 17:42:12 +02003649 struct mvneta_port *pp = netdev_priv(ndev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003650
Russell King503f9aa92018-01-02 17:24:44 +00003651 return phylink_ethtool_ksettings_set(pp->phylink, cmd);
3652}
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003653
Russell King503f9aa92018-01-02 17:24:44 +00003654/* Get link ksettings for ethtools */
3655static int
3656mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
3657 struct ethtool_link_ksettings *cmd)
3658{
3659 struct mvneta_port *pp = netdev_priv(ndev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003660
Russell King503f9aa92018-01-02 17:24:44 +00003661 return phylink_ethtool_ksettings_get(pp->phylink, cmd);
3662}
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003663
Russell King503f9aa92018-01-02 17:24:44 +00003664static int mvneta_ethtool_nway_reset(struct net_device *dev)
3665{
3666 struct mvneta_port *pp = netdev_priv(dev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003667
Russell King503f9aa92018-01-02 17:24:44 +00003668 return phylink_ethtool_nway_reset(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003669}
3670
3671/* Set interrupt coalescing for ethtools */
3672static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3673 struct ethtool_coalesce *c)
3674{
3675 struct mvneta_port *pp = netdev_priv(dev);
3676 int queue;
3677
3678 for (queue = 0; queue < rxq_number; queue++) {
3679 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3680 rxq->time_coal = c->rx_coalesce_usecs;
3681 rxq->pkts_coal = c->rx_max_coalesced_frames;
3682 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3683 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3684 }
3685
3686 for (queue = 0; queue < txq_number; queue++) {
3687 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3688 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3689 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3690 }
3691
3692 return 0;
3693}
3694
3695/* get coalescing for ethtools */
3696static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3697 struct ethtool_coalesce *c)
3698{
3699 struct mvneta_port *pp = netdev_priv(dev);
3700
3701 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3702 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3703
3704 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3705 return 0;
3706}
3707
3708
3709static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3710 struct ethtool_drvinfo *drvinfo)
3711{
3712 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3713 sizeof(drvinfo->driver));
3714 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3715 sizeof(drvinfo->version));
3716 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3717 sizeof(drvinfo->bus_info));
3718}
3719
3720
3721static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3722 struct ethtool_ringparam *ring)
3723{
3724 struct mvneta_port *pp = netdev_priv(netdev);
3725
3726 ring->rx_max_pending = MVNETA_MAX_RXD;
3727 ring->tx_max_pending = MVNETA_MAX_TXD;
3728 ring->rx_pending = pp->rx_ring_size;
3729 ring->tx_pending = pp->tx_ring_size;
3730}
3731
3732static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3733 struct ethtool_ringparam *ring)
3734{
3735 struct mvneta_port *pp = netdev_priv(dev);
3736
3737 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3738 return -EINVAL;
3739 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3740 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003741
3742 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3743 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3744 if (pp->tx_ring_size != ring->tx_pending)
3745 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3746 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003747
3748 if (netif_running(dev)) {
3749 mvneta_stop(dev);
3750 if (mvneta_open(dev)) {
3751 netdev_err(dev,
3752 "error on opening device after ring param change\n");
3753 return -ENOMEM;
3754 }
3755 }
3756
3757 return 0;
3758}
3759
Russell King9b0cdef2015-10-22 18:37:30 +01003760static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3761 u8 *data)
3762{
3763 if (sset == ETH_SS_STATS) {
3764 int i;
3765
3766 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3767 memcpy(data + i * ETH_GSTRING_LEN,
3768 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3769 }
3770}
3771
3772static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3773{
3774 const struct mvneta_statistic *s;
3775 void __iomem *base = pp->base;
3776 u32 high, low, val;
Jisheng Zhang2c832292016-01-20 16:36:25 +08003777 u64 val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003778 int i;
3779
3780 for (i = 0, s = mvneta_statistics;
3781 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3782 s++, i++) {
Russell King9b0cdef2015-10-22 18:37:30 +01003783 switch (s->type) {
3784 case T_REG_32:
3785 val = readl_relaxed(base + s->offset);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003786 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003787 break;
3788 case T_REG_64:
3789 /* Docs say to read low 32-bit then high */
3790 low = readl_relaxed(base + s->offset);
3791 high = readl_relaxed(base + s->offset + 4);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003792 val64 = (u64)high << 32 | low;
3793 pp->ethtool_stats[i] += val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003794 break;
3795 }
Russell King9b0cdef2015-10-22 18:37:30 +01003796 }
3797}
3798
3799static void mvneta_ethtool_get_stats(struct net_device *dev,
3800 struct ethtool_stats *stats, u64 *data)
3801{
3802 struct mvneta_port *pp = netdev_priv(dev);
3803 int i;
3804
3805 mvneta_ethtool_update_stats(pp);
3806
3807 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3808 *data++ = pp->ethtool_stats[i];
3809}
3810
3811static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3812{
3813 if (sset == ETH_SS_STATS)
3814 return ARRAY_SIZE(mvneta_statistics);
3815 return -EOPNOTSUPP;
3816}
3817
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003818static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3819{
3820 return MVNETA_RSS_LU_TABLE_SIZE;
3821}
3822
3823static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3824 struct ethtool_rxnfc *info,
3825 u32 *rules __always_unused)
3826{
3827 switch (info->cmd) {
3828 case ETHTOOL_GRXRINGS:
3829 info->data = rxq_number;
3830 return 0;
3831 case ETHTOOL_GRXFH:
3832 return -EOPNOTSUPP;
3833 default:
3834 return -EOPNOTSUPP;
3835 }
3836}
3837
3838static int mvneta_config_rss(struct mvneta_port *pp)
3839{
3840 int cpu;
3841 u32 val;
3842
3843 netif_tx_stop_all_queues(pp->dev);
3844
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003845 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003846
3847 /* We have to synchronise on the napi of each CPU */
3848 for_each_online_cpu(cpu) {
3849 struct mvneta_pcpu_port *pcpu_port =
3850 per_cpu_ptr(pp->ports, cpu);
3851
3852 napi_synchronize(&pcpu_port->napi);
3853 napi_disable(&pcpu_port->napi);
3854 }
3855
3856 pp->rxq_def = pp->indir[0];
3857
3858 /* Update unicast mapping */
3859 mvneta_set_rx_mode(pp->dev);
3860
3861 /* Update val of portCfg register accordingly with all RxQueue types */
3862 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3863 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3864
3865 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003866 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003867 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003868 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003869
3870 /* We have to synchronise on the napi of each CPU */
3871 for_each_online_cpu(cpu) {
3872 struct mvneta_pcpu_port *pcpu_port =
3873 per_cpu_ptr(pp->ports, cpu);
3874
3875 napi_enable(&pcpu_port->napi);
3876 }
3877
3878 netif_tx_start_all_queues(pp->dev);
3879
3880 return 0;
3881}
3882
3883static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3884 const u8 *key, const u8 hfunc)
3885{
3886 struct mvneta_port *pp = netdev_priv(dev);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003887
3888 /* Current code for Armada 3700 doesn't support RSS features yet */
3889 if (pp->neta_armada3700)
3890 return -EOPNOTSUPP;
3891
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003892 /* We require at least one supported parameter to be changed
3893 * and no change in any of the unsupported parameters
3894 */
3895 if (key ||
3896 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3897 return -EOPNOTSUPP;
3898
3899 if (!indir)
3900 return 0;
3901
3902 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3903
3904 return mvneta_config_rss(pp);
3905}
3906
3907static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3908 u8 *hfunc)
3909{
3910 struct mvneta_port *pp = netdev_priv(dev);
3911
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003912 /* Current code for Armada 3700 doesn't support RSS features yet */
3913 if (pp->neta_armada3700)
3914 return -EOPNOTSUPP;
3915
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003916 if (hfunc)
3917 *hfunc = ETH_RSS_HASH_TOP;
3918
3919 if (!indir)
3920 return 0;
3921
3922 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3923
3924 return 0;
3925}
3926
Jingju Houb60a00f2017-02-06 14:58:13 +08003927static void mvneta_ethtool_get_wol(struct net_device *dev,
3928 struct ethtool_wolinfo *wol)
3929{
Russell King503f9aa92018-01-02 17:24:44 +00003930 struct mvneta_port *pp = netdev_priv(dev);
Jingju Houb60a00f2017-02-06 14:58:13 +08003931
Russell King503f9aa92018-01-02 17:24:44 +00003932 phylink_ethtool_get_wol(pp->phylink, wol);
Jingju Houb60a00f2017-02-06 14:58:13 +08003933}
3934
3935static int mvneta_ethtool_set_wol(struct net_device *dev,
3936 struct ethtool_wolinfo *wol)
3937{
Russell King503f9aa92018-01-02 17:24:44 +00003938 struct mvneta_port *pp = netdev_priv(dev);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003939 int ret;
3940
Russell King503f9aa92018-01-02 17:24:44 +00003941 ret = phylink_ethtool_set_wol(pp->phylink, wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003942 if (!ret)
3943 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
3944
3945 return ret;
Jingju Houb60a00f2017-02-06 14:58:13 +08003946}
3947
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003948static const struct net_device_ops mvneta_netdev_ops = {
3949 .ndo_open = mvneta_open,
3950 .ndo_stop = mvneta_stop,
3951 .ndo_start_xmit = mvneta_tx,
3952 .ndo_set_rx_mode = mvneta_set_rx_mode,
3953 .ndo_set_mac_address = mvneta_set_mac_addr,
3954 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02003955 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003956 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003957 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003958};
3959
Jisheng Zhang4581be42017-02-16 17:07:39 +08003960static const struct ethtool_ops mvneta_eth_tool_ops = {
Russell King503f9aa92018-01-02 17:24:44 +00003961 .nway_reset = mvneta_ethtool_nway_reset,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003962 .get_link = ethtool_op_get_link,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003963 .set_coalesce = mvneta_ethtool_set_coalesce,
3964 .get_coalesce = mvneta_ethtool_get_coalesce,
3965 .get_drvinfo = mvneta_ethtool_get_drvinfo,
3966 .get_ringparam = mvneta_ethtool_get_ringparam,
3967 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King9b0cdef2015-10-22 18:37:30 +01003968 .get_strings = mvneta_ethtool_get_strings,
3969 .get_ethtool_stats = mvneta_ethtool_get_stats,
3970 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003971 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
3972 .get_rxnfc = mvneta_ethtool_get_rxnfc,
3973 .get_rxfh = mvneta_ethtool_get_rxfh,
3974 .set_rxfh = mvneta_ethtool_set_rxfh,
Russell King503f9aa92018-01-02 17:24:44 +00003975 .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
Philippe Reynes013ad402016-07-30 17:42:12 +02003976 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
Jingju Houb60a00f2017-02-06 14:58:13 +08003977 .get_wol = mvneta_ethtool_get_wol,
3978 .set_wol = mvneta_ethtool_set_wol,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003979};
3980
3981/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03003982static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003983{
3984 int queue;
3985
3986 /* Disable port */
3987 mvneta_port_disable(pp);
3988
3989 /* Set port default values */
3990 mvneta_defaults_set(pp);
3991
Markus Elfring5d6312ed2017-04-16 21:45:38 +02003992 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003993 if (!pp->txqs)
3994 return -ENOMEM;
3995
3996 /* Initialize TX descriptor rings */
3997 for (queue = 0; queue < txq_number; queue++) {
3998 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3999 txq->id = queue;
4000 txq->size = pp->tx_ring_size;
4001 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4002 }
4003
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004004 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
Ezequiel Garcia96728502014-05-22 20:06:59 -03004005 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004006 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004007
4008 /* Create Rx descriptor rings */
4009 for (queue = 0; queue < rxq_number; queue++) {
4010 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4011 rxq->id = queue;
4012 rxq->size = pp->rx_ring_size;
4013 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4014 rxq->time_coal = MVNETA_RX_COAL_USEC;
Markus Elfring29110632017-04-16 21:23:19 +02004015 rxq->buf_virt_addr
4016 = devm_kmalloc_array(pp->dev->dev.parent,
4017 rxq->size,
4018 sizeof(*rxq->buf_virt_addr),
4019 GFP_KERNEL);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01004020 if (!rxq->buf_virt_addr)
4021 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004022 }
4023
4024 return 0;
4025}
4026
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004027/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00004028static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4029 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004030{
4031 u32 win_enable;
4032 u32 win_protect;
4033 int i;
4034
4035 for (i = 0; i < 6; i++) {
4036 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4037 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4038
4039 if (i < 4)
4040 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4041 }
4042
4043 win_enable = 0x3f;
4044 win_protect = 0;
4045
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004046 if (dram) {
4047 for (i = 0; i < dram->num_cs; i++) {
4048 const struct mbus_dram_window *cs = dram->cs + i;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004049
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004050 mvreg_write(pp, MVNETA_WIN_BASE(i),
4051 (cs->base & 0xffff0000) |
4052 (cs->mbus_attr << 8) |
4053 dram->mbus_dram_target_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004054
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004055 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4056 (cs->size - 1) & 0xffff0000);
4057
4058 win_enable &= ~(1 << i);
4059 win_protect |= 3 << (2 * i);
4060 }
4061 } else {
4062 /* For Armada3700 open default 4GB Mbus window, leaving
4063 * arbitration of target/attribute to a different layer
4064 * of configuration.
4065 */
4066 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4067 win_enable &= ~BIT(0);
4068 win_protect = 3;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004069 }
4070
4071 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01004072 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004073}
4074
4075/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004076static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004077{
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004078 u32 ctrl;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004079
4080 /* MAC Cause register should be cleared */
4081 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4082
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004083 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004084
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004085 /* Even though it might look weird, when we're configured in
4086 * SGMII or QSGMII mode, the RGMII bit needs to be set.
4087 */
4088 switch(phy_mode) {
4089 case PHY_INTERFACE_MODE_QSGMII:
4090 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
4091 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
4092 break;
4093 case PHY_INTERFACE_MODE_SGMII:
4094 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
4095 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
4096 break;
4097 case PHY_INTERFACE_MODE_RGMII:
4098 case PHY_INTERFACE_MODE_RGMII_ID:
Jisheng Zhanga38d20d2017-03-29 16:42:26 +08004099 case PHY_INTERFACE_MODE_RGMII_RXID:
4100 case PHY_INTERFACE_MODE_RGMII_TXID:
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004101 ctrl |= MVNETA_GMAC2_PORT_RGMII;
4102 break;
4103 default:
4104 return -EINVAL;
4105 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004106
4107 /* Cancel Port Reset */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004108 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
4109 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004110
4111 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
4112 MVNETA_GMAC2_PORT_RESET) != 0)
4113 continue;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004114
4115 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004116}
4117
4118/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00004119static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004120{
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004121 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004122 struct device_node *dn = pdev->dev.of_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004123 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004124 struct mvneta_port *pp;
4125 struct net_device *dev;
Russell King503f9aa92018-01-02 17:24:44 +00004126 struct phylink *phylink;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004127 const char *dt_mac_addr;
4128 char hw_mac_addr[ETH_ALEN];
4129 const char *mac_from;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004130 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004131 int phy_mode;
4132 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004133 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004134
Willy Tarreauee40a112013-04-11 23:00:37 +02004135 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004136 if (!dev)
4137 return -ENOMEM;
4138
4139 dev->irq = irq_of_parse_and_map(dn, 0);
4140 if (dev->irq == 0) {
4141 err = -EINVAL;
4142 goto err_free_netdev;
4143 }
4144
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004145 phy_mode = of_get_phy_mode(dn);
4146 if (phy_mode < 0) {
4147 dev_err(&pdev->dev, "incorrect phy-mode\n");
4148 err = -EINVAL;
Russell King503f9aa92018-01-02 17:24:44 +00004149 goto err_free_irq;
4150 }
4151
4152 phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode,
4153 &mvneta_phylink_ops);
4154 if (IS_ERR(phylink)) {
4155 err = PTR_ERR(phylink);
4156 goto err_free_irq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004157 }
4158
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004159 dev->tx_queue_len = MVNETA_MAX_TXD;
4160 dev->watchdog_timeo = 5 * HZ;
4161 dev->netdev_ops = &mvneta_netdev_ops;
4162
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004163 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004164
4165 pp = netdev_priv(dev);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01004166 spin_lock_init(&pp->lock);
Russell King503f9aa92018-01-02 17:24:44 +00004167 pp->phylink = phylink;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004168 pp->phy_interface = phy_mode;
Russell King503f9aa92018-01-02 17:24:44 +00004169 pp->dn = dn;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004170
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01004171 pp->rxq_def = rxq_def;
4172
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01004173 /* Set RX packet offset correction for platforms, whose
4174 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4175 * platforms and 0B for 32-bit ones.
4176 */
4177 pp->rx_offset_correction =
4178 max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
4179
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004180 pp->indir[0] = rxq_def;
4181
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004182 /* Get special SoC configurations */
4183 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4184 pp->neta_armada3700 = true;
4185
Jisheng Zhang2804ba42016-01-20 19:27:23 +08004186 pp->clk = devm_clk_get(&pdev->dev, "core");
4187 if (IS_ERR(pp->clk))
4188 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004189 if (IS_ERR(pp->clk)) {
4190 err = PTR_ERR(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00004191 goto err_free_phylink;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004192 }
4193
4194 clk_prepare_enable(pp->clk);
4195
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004196 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4197 if (!IS_ERR(pp->clk_bus))
4198 clk_prepare_enable(pp->clk_bus);
4199
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004200 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4201 pp->base = devm_ioremap_resource(&pdev->dev, res);
4202 if (IS_ERR(pp->base)) {
4203 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004204 goto err_clk;
4205 }
4206
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004207 /* Alloc per-cpu port structure */
4208 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4209 if (!pp->ports) {
4210 err = -ENOMEM;
4211 goto err_clk;
4212 }
4213
willy tarreau74c41b02014-01-16 08:20:08 +01004214 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08004215 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01004216 if (!pp->stats) {
4217 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004218 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01004219 }
4220
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004221 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01004222 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004223 mac_from = "device tree";
4224 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4225 } else {
4226 mvneta_get_mac_addr(pp, hw_mac_addr);
4227 if (is_valid_ether_addr(hw_mac_addr)) {
4228 mac_from = "hardware";
4229 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4230 } else {
4231 mac_from = "random";
4232 eth_hw_addr_random(dev);
4233 }
4234 }
4235
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004236 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4237 if (tx_csum_limit < 0 ||
4238 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4239 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4240 dev_info(&pdev->dev,
4241 "Wrong TX csum limit in DT, set to %dB\n",
4242 MVNETA_TX_CSUM_DEF_SIZE);
4243 }
4244 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4245 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4246 } else {
4247 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4248 }
4249
4250 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02004251
Jane Li9768b452017-03-16 16:22:28 +08004252 pp->dram_target_info = mv_mbus_dram_info();
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004253 /* Armada3700 requires setting default configuration of Mbus
4254 * windows, however without using filled mbus_dram_target_info
4255 * structure.
4256 */
Jane Li9768b452017-03-16 16:22:28 +08004257 if (pp->dram_target_info || pp->neta_armada3700)
4258 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004259
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004260 pp->tx_ring_size = MVNETA_MAX_TXD;
4261 pp->rx_ring_size = MVNETA_MAX_RXD;
4262
4263 pp->dev = dev;
4264 SET_NETDEV_DEV(dev, &pdev->dev);
4265
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004266 pp->id = global_port_id++;
4267
4268 /* Obtain access to BM resources if enabled and already initialized */
4269 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4270 if (bm_node && bm_node->data) {
4271 pp->bm_priv = bm_node->data;
4272 err = mvneta_bm_port_init(pdev, pp);
4273 if (err < 0) {
4274 dev_info(&pdev->dev, "use SW buffer management\n");
4275 pp->bm_priv = NULL;
4276 }
4277 }
Peter Chend4e4da02016-08-01 15:02:36 +08004278 of_node_put(bm_node);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004279
Ezequiel Garcia96728502014-05-22 20:06:59 -03004280 err = mvneta_init(&pdev->dev, pp);
4281 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004282 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004283
4284 err = mvneta_port_power_up(pp, phy_mode);
4285 if (err < 0) {
4286 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004287 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004288 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004289
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004290 /* Armada3700 network controller does not support per-cpu
4291 * operation, so only single NAPI should be initialized.
4292 */
4293 if (pp->neta_armada3700) {
4294 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4295 } else {
4296 for_each_present_cpu(cpu) {
4297 struct mvneta_pcpu_port *port =
4298 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004299
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004300 netif_napi_add(dev, &port->napi, mvneta_poll,
4301 NAPI_POLL_WEIGHT);
4302 port->pp = pp;
4303 }
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004304 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004305
Andrew Pilloudc54a5042017-09-01 07:49:49 -07004306 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03004307 dev->hw_features |= dev->features;
4308 dev->vlan_features |= dev->features;
Andrew Lunn97db8af2016-11-24 00:08:13 +01004309 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004310 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00004311
Jarod Wilson57779872016-10-17 15:54:06 -04004312 /* MTU range: 68 - 9676 */
4313 dev->min_mtu = ETH_MIN_MTU;
4314 /* 9676 == 9700 - 20 and rounding to 8 */
4315 dev->max_mtu = 9676;
4316
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004317 err = register_netdev(dev);
4318 if (err < 0) {
4319 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03004320 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004321 }
4322
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004323 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4324 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004325
4326 platform_set_drvdata(pdev, pp->dev);
4327
4328 return 0;
4329
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004330err_netdev:
4331 unregister_netdev(dev);
4332 if (pp->bm_priv) {
4333 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4334 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4335 1 << pp->id);
4336 }
willy tarreau74c41b02014-01-16 08:20:08 +01004337err_free_stats:
4338 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004339err_free_ports:
4340 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004341err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004342 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004343 clk_disable_unprepare(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00004344err_free_phylink:
4345 if (pp->phylink)
4346 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004347err_free_irq:
4348 irq_dispose_mapping(dev->irq);
4349err_free_netdev:
4350 free_netdev(dev);
4351 return err;
4352}
4353
4354/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00004355static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004356{
4357 struct net_device *dev = platform_get_drvdata(pdev);
4358 struct mvneta_port *pp = netdev_priv(dev);
4359
4360 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004361 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004362 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004363 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01004364 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004365 irq_dispose_mapping(dev->irq);
Russell King503f9aa92018-01-02 17:24:44 +00004366 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004367 free_netdev(dev);
4368
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004369 if (pp->bm_priv) {
4370 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4371 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4372 1 << pp->id);
4373 }
4374
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004375 return 0;
4376}
4377
Jane Li9768b452017-03-16 16:22:28 +08004378#ifdef CONFIG_PM_SLEEP
4379static int mvneta_suspend(struct device *device)
4380{
4381 struct net_device *dev = dev_get_drvdata(device);
4382 struct mvneta_port *pp = netdev_priv(dev);
4383
Russell King3b8bc672018-01-02 17:24:34 +00004384 rtnl_lock();
Jane Li9768b452017-03-16 16:22:28 +08004385 if (netif_running(dev))
4386 mvneta_stop(dev);
Russell King3b8bc672018-01-02 17:24:34 +00004387 rtnl_unlock();
Jane Li9768b452017-03-16 16:22:28 +08004388 netif_device_detach(dev);
4389 clk_disable_unprepare(pp->clk_bus);
4390 clk_disable_unprepare(pp->clk);
4391 return 0;
4392}
4393
4394static int mvneta_resume(struct device *device)
4395{
4396 struct platform_device *pdev = to_platform_device(device);
4397 struct net_device *dev = dev_get_drvdata(device);
4398 struct mvneta_port *pp = netdev_priv(dev);
4399 int err;
4400
4401 clk_prepare_enable(pp->clk);
4402 if (!IS_ERR(pp->clk_bus))
4403 clk_prepare_enable(pp->clk_bus);
4404 if (pp->dram_target_info || pp->neta_armada3700)
4405 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4406 if (pp->bm_priv) {
4407 err = mvneta_bm_port_init(pdev, pp);
4408 if (err < 0) {
4409 dev_info(&pdev->dev, "use SW buffer management\n");
4410 pp->bm_priv = NULL;
4411 }
4412 }
4413 mvneta_defaults_set(pp);
4414 err = mvneta_port_power_up(pp, pp->phy_interface);
4415 if (err < 0) {
4416 dev_err(device, "can't power up port\n");
4417 return err;
4418 }
4419
Jane Li9768b452017-03-16 16:22:28 +08004420 netif_device_attach(dev);
Russell King3b8bc672018-01-02 17:24:34 +00004421 rtnl_lock();
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004422 if (netif_running(dev)) {
Jane Li9768b452017-03-16 16:22:28 +08004423 mvneta_open(dev);
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004424 mvneta_set_rx_mode(dev);
4425 }
Russell King3b8bc672018-01-02 17:24:34 +00004426 rtnl_unlock();
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004427
Jane Li9768b452017-03-16 16:22:28 +08004428 return 0;
4429}
4430#endif
4431
4432static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
4433
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004434static const struct of_device_id mvneta_match[] = {
4435 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02004436 { .compatible = "marvell,armada-xp-neta" },
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004437 { .compatible = "marvell,armada-3700-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004438 { }
4439};
4440MODULE_DEVICE_TABLE(of, mvneta_match);
4441
4442static struct platform_driver mvneta_driver = {
4443 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00004444 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004445 .driver = {
4446 .name = MVNETA_DRIVER_NAME,
4447 .of_match_table = mvneta_match,
Jane Li9768b452017-03-16 16:22:28 +08004448 .pm = &mvneta_pm_ops,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004449 },
4450};
4451
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004452static int __init mvneta_driver_init(void)
4453{
4454 int ret;
4455
4456 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4457 mvneta_cpu_online,
4458 mvneta_cpu_down_prepare);
4459 if (ret < 0)
4460 goto out;
4461 online_hpstate = ret;
4462 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4463 NULL, mvneta_cpu_dead);
4464 if (ret)
4465 goto err_dead;
4466
4467 ret = platform_driver_register(&mvneta_driver);
4468 if (ret)
4469 goto err;
4470 return 0;
4471
4472err:
4473 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4474err_dead:
4475 cpuhp_remove_multi_state(online_hpstate);
4476out:
4477 return ret;
4478}
4479module_init(mvneta_driver_init);
4480
4481static void __exit mvneta_driver_exit(void)
4482{
4483 platform_driver_unregister(&mvneta_driver);
4484 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4485 cpuhp_remove_multi_state(online_hpstate);
4486}
4487module_exit(mvneta_driver_exit);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004488
4489MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4490MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4491MODULE_LICENSE("GPL");
4492
4493module_param(rxq_number, int, S_IRUGO);
4494module_param(txq_number, int, S_IRUGO);
4495
4496module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01004497module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);