blob: 44d665887b50ad1d668eb7dc7be2f5c25e51e041 [file] [log] [blame]
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001/*
2 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
3 *
4 * Copyright (C) 2012 Marvell
5 *
6 * Rami Rosen <rosenr@marvell.com>
7 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
Jisheng Zhang0e03f562016-01-20 19:27:22 +080014#include <linux/clk.h>
15#include <linux/cpu.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030016#include <linux/etherdevice.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080017#include <linux/if_vlan.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030018#include <linux/inetdevice.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080019#include <linux/interrupt.h>
20#include <linux/io.h>
21#include <linux/kernel.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030022#include <linux/mbus.h>
23#include <linux/module.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080024#include <linux/netdevice.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030025#include <linux/of.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080026#include <linux/of_address.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030027#include <linux/of_irq.h>
28#include <linux/of_mdio.h>
29#include <linux/of_net.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030030#include <linux/phy.h>
Russell King503f9aa92018-01-02 17:24:44 +000031#include <linux/phylink.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080032#include <linux/platform_device.h>
33#include <linux/skbuff.h>
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +010034#include <net/hwbm.h>
Marcin Wojtasdc35a102016-03-14 09:39:03 +010035#include "mvneta_bm.h"
Jisheng Zhang0e03f562016-01-20 19:27:22 +080036#include <net/ip.h>
37#include <net/ipv6.h>
38#include <net/tso.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030039
40/* Registers */
41#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
Marcin Wojtase5bdf682015-11-30 13:27:42 +010042#define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +010043#define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
44#define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
45#define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
46#define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
Thomas Petazzonic5aff182012-08-17 14:04:28 +030047#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
48#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
49#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
50#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
51#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
52#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
53#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
54#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
55#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
56#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
57#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
58#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
59#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
Marcin Wojtasdc35a102016-03-14 09:39:03 +010060#define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
61#define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
62#define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
Thomas Petazzonic5aff182012-08-17 14:04:28 +030063#define MVNETA_PORT_RX_RESET 0x1cc0
64#define MVNETA_PORT_RX_DMA_RESET BIT(0)
65#define MVNETA_PHY_ADDR 0x2000
66#define MVNETA_PHY_ADDR_MASK 0x1f
67#define MVNETA_MBUS_RETRY 0x2010
68#define MVNETA_UNIT_INTR_CAUSE 0x2080
69#define MVNETA_UNIT_CONTROL 0x20B0
70#define MVNETA_PHY_POLLING_ENABLE BIT(1)
71#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
72#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
73#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
74#define MVNETA_BASE_ADDR_ENABLE 0x2290
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +010075#define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
Thomas Petazzonic5aff182012-08-17 14:04:28 +030076#define MVNETA_PORT_CONFIG 0x2400
77#define MVNETA_UNI_PROMISC_MODE BIT(0)
78#define MVNETA_DEF_RXQ(q) ((q) << 1)
79#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
80#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
81#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
82#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
83#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
84#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
85#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
86 MVNETA_DEF_RXQ_ARP(q) | \
87 MVNETA_DEF_RXQ_TCP(q) | \
88 MVNETA_DEF_RXQ_UDP(q) | \
89 MVNETA_DEF_RXQ_BPDU(q) | \
90 MVNETA_TX_UNSET_ERR_SUM | \
91 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92#define MVNETA_PORT_CONFIG_EXTEND 0x2404
93#define MVNETA_MAC_ADDR_LOW 0x2414
94#define MVNETA_MAC_ADDR_HIGH 0x2418
95#define MVNETA_SDMA_CONFIG 0x241c
96#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030097#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
98#define MVNETA_RX_NO_DATA_SWAP BIT(4)
99#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200100#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300101#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
102#define MVNETA_PORT_STATUS 0x2444
103#define MVNETA_TX_IN_PRGRS BIT(1)
104#define MVNETA_TX_FIFO_EMPTY BIT(8)
105#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200106#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200107#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200108#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300109#define MVNETA_TYPE_PRIO 0x24bc
110#define MVNETA_FORCE_UNI BIT(21)
111#define MVNETA_TXQ_CMD_1 0x24e4
112#define MVNETA_TXQ_CMD 0x2448
113#define MVNETA_TXQ_DISABLE_SHIFT 8
114#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Andrew Lunne4839112015-10-22 18:37:36 +0100115#define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
116#define MVNETA_OVERRUN_FRAME_COUNT 0x2488
Stas Sergeev898b29702015-04-01 20:32:49 +0300117#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
118#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300119#define MVNETA_ACC_MODE 0x2500
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100120#define MVNETA_BM_ADDRESS 0x2504
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300121#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
122#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
123#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100124#define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100125#define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300126#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100127
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100128/* Exception Interrupt Port/Queue Cause register
129 *
130 * Their behavior depend of the mapping done using the PCPX2Q
131 * registers. For a given CPU if the bit associated to a queue is not
132 * set, then for the register a read from this CPU will always return
133 * 0 and a write won't do anything
134 */
willy tarreau40ba35e2014-01-16 08:20:10 +0100135
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300136#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300137#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100138
139/* bits 0..7 = TXQ SENT, one bit per queue.
140 * bits 8..15 = RXQ OCCUP, one bit per queue.
141 * bits 16..23 = RXQ FREE, one bit per queue.
142 * bit 29 = OLD_REG_SUM, see old reg ?
143 * bit 30 = TX_ERR_SUM, one bit for 4 ports
144 * bit 31 = MISC_SUM, one bit for 4 ports
145 */
146#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
147#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
148#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
149#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b29702015-04-01 20:32:49 +0300150#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100151
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300152#define MVNETA_INTR_OLD_CAUSE 0x25a8
153#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100154
155/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300156#define MVNETA_INTR_MISC_CAUSE 0x25b0
157#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100158
159#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
160#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
161#define MVNETA_CAUSE_PTP BIT(4)
162
163#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
164#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
165#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
166#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
167#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
168#define MVNETA_CAUSE_PRBS_ERR BIT(12)
169#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
170#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
171
172#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
173#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
174#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
175
176#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
177#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
178#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
179
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300180#define MVNETA_INTR_ENABLE 0x25b8
181#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
Marcin Wojtasdc1aadf2015-11-30 13:27:43 +0100182#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
willy tarreau40ba35e2014-01-16 08:20:10 +0100183
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300184#define MVNETA_RXQ_CMD 0x2680
185#define MVNETA_RXQ_DISABLE_SHIFT 8
186#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
187#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
188#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
189#define MVNETA_GMAC_CTRL_0 0x2c00
190#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
191#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
Russell King22f4bf82018-01-02 17:24:54 +0000192#define MVNETA_GMAC0_PORT_1000BASE_X BIT(1)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300193#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
194#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b29702015-04-01 20:32:49 +0300195#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100196#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300197#define MVNETA_GMAC2_PORT_RGMII BIT(4)
198#define MVNETA_GMAC2_PORT_RESET BIT(6)
199#define MVNETA_GMAC_STATUS 0x2c10
200#define MVNETA_GMAC_LINK_UP BIT(0)
201#define MVNETA_GMAC_SPEED_1000 BIT(1)
202#define MVNETA_GMAC_SPEED_100 BIT(2)
203#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
204#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
205#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
206#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
207#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
Russell King503f9aa92018-01-02 17:24:44 +0000208#define MVNETA_GMAC_AN_COMPLETE BIT(11)
209#define MVNETA_GMAC_SYNC_OK BIT(14)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300210#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
211#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
212#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b29702015-04-01 20:32:49 +0300213#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Russell King22f4bf82018-01-02 17:24:54 +0000214#define MVNETA_GMAC_AN_BYPASS_ENABLE BIT(3)
215#define MVNETA_GMAC_INBAND_RESTART_AN BIT(4)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300216#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
217#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200218#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Russell King22f4bf82018-01-02 17:24:54 +0000219#define MVNETA_GMAC_CONFIG_FLOW_CTRL BIT(8)
220#define MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL BIT(9)
Stas Sergeev898b29702015-04-01 20:32:49 +0300221#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300222#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200223#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Andrew Lunne4839112015-10-22 18:37:36 +0100224#define MVNETA_MIB_COUNTERS_BASE 0x3000
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300225#define MVNETA_MIB_LATE_COLLISION 0x7c
226#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
227#define MVNETA_DA_FILT_OTH_MCAST 0x3500
228#define MVNETA_DA_FILT_UCAST_BASE 0x3600
229#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
230#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
231#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
232#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
233#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
234#define MVNETA_TXQ_DEC_SENT_SHIFT 16
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100235#define MVNETA_TXQ_DEC_SENT_MASK 0xff
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300236#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
237#define MVNETA_TXQ_SENT_DESC_SHIFT 16
238#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
239#define MVNETA_PORT_TX_RESET 0x3cf0
240#define MVNETA_PORT_TX_DMA_RESET BIT(0)
241#define MVNETA_TX_MTU 0x3e0c
242#define MVNETA_TX_TOKEN_SIZE 0x3e14
243#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
244#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
245#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
246
Russell King6d81f452018-01-02 17:25:04 +0000247#define MVNETA_LPI_CTRL_0 0x2cc0
248#define MVNETA_LPI_CTRL_1 0x2cc4
249#define MVNETA_LPI_REQUEST_ENABLE BIT(0)
250#define MVNETA_LPI_CTRL_2 0x2cc8
251#define MVNETA_LPI_STATUS 0x2ccc
252
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300253#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
254
255/* Descriptor ring Macros */
256#define MVNETA_QUEUE_NEXT_DESC(q, index) \
257 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
258
259/* Various constants */
260
261/* Coalescing */
Dmitri Epshtein06708f82016-07-06 04:18:58 +0200262#define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300263#define MVNETA_RX_COAL_PKTS 32
264#define MVNETA_RX_COAL_USEC 100
265
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100266/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300267 * by Marvell switches when a specific hardware mode is enabled (not
268 * supported by this driver) or is filled automatically by zeroes on
269 * the RX side. Those two bytes being at the front of the Ethernet
270 * header, they allow to have the IP header aligned on a 4 bytes
271 * boundary automatically: the hardware skips those two bytes on its
272 * own.
273 */
274#define MVNETA_MH_SIZE 2
275
276#define MVNETA_VLAN_TAG_LEN 4
277
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100278#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300279#define MVNETA_TX_CSUM_MAX_SIZE 9800
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100280#define MVNETA_ACC_MODE_EXT1 1
281#define MVNETA_ACC_MODE_EXT2 2
282
283#define MVNETA_MAX_DECODE_WIN 6
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300284
285/* Timeout constants */
286#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
287#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
288#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
289
290#define MVNETA_TX_MTU_MAX 0x3ffff
291
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100292/* The RSS lookup table actually has 256 entries but we do not use
293 * them yet
294 */
295#define MVNETA_RSS_LU_TABLE_SIZE 1
296
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300297/* Max number of Rx descriptors */
298#define MVNETA_MAX_RXD 128
299
300/* Max number of Tx descriptors */
301#define MVNETA_MAX_TXD 532
302
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300303/* Max number of allowed TCP segments for software TSO */
304#define MVNETA_MAX_TSO_SEGS 100
305
306#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
307
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300308/* descriptor aligned size */
309#define MVNETA_DESC_ALIGNED_SIZE 32
310
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100311/* Number of bytes to be taken into account by HW when putting incoming data
312 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
313 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
314 */
315#define MVNETA_RX_PKT_OFFSET_CORRECTION 64
316
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300317#define MVNETA_RX_PKT_SIZE(mtu) \
318 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
319 ETH_HLEN + ETH_FCS_LEN, \
Jisheng Zhangc66e98c2016-04-01 17:12:49 +0800320 cache_line_size())
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300321
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300322#define IS_TSO_HEADER(txq, addr) \
323 ((addr >= txq->tso_hdrs_phys) && \
324 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
325
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100326#define MVNETA_RX_GET_BM_POOL_ID(rxd) \
327 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300328
Russell King6d81f452018-01-02 17:25:04 +0000329enum {
330 ETHTOOL_STAT_EEE_WAKEUP,
331 ETHTOOL_MAX_STATS,
332};
333
Russell King9b0cdef2015-10-22 18:37:30 +0100334struct mvneta_statistic {
335 unsigned short offset;
336 unsigned short type;
337 const char name[ETH_GSTRING_LEN];
338};
339
340#define T_REG_32 32
341#define T_REG_64 64
Russell King6d81f452018-01-02 17:25:04 +0000342#define T_SW 1
Russell King9b0cdef2015-10-22 18:37:30 +0100343
344static const struct mvneta_statistic mvneta_statistics[] = {
345 { 0x3000, T_REG_64, "good_octets_received", },
346 { 0x3010, T_REG_32, "good_frames_received", },
347 { 0x3008, T_REG_32, "bad_octets_received", },
348 { 0x3014, T_REG_32, "bad_frames_received", },
349 { 0x3018, T_REG_32, "broadcast_frames_received", },
350 { 0x301c, T_REG_32, "multicast_frames_received", },
351 { 0x3050, T_REG_32, "unrec_mac_control_received", },
352 { 0x3058, T_REG_32, "good_fc_received", },
353 { 0x305c, T_REG_32, "bad_fc_received", },
354 { 0x3060, T_REG_32, "undersize_received", },
355 { 0x3064, T_REG_32, "fragments_received", },
356 { 0x3068, T_REG_32, "oversize_received", },
357 { 0x306c, T_REG_32, "jabber_received", },
358 { 0x3070, T_REG_32, "mac_receive_error", },
359 { 0x3074, T_REG_32, "bad_crc_event", },
360 { 0x3078, T_REG_32, "collision", },
361 { 0x307c, T_REG_32, "late_collision", },
362 { 0x2484, T_REG_32, "rx_discard", },
363 { 0x2488, T_REG_32, "rx_overrun", },
364 { 0x3020, T_REG_32, "frames_64_octets", },
365 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
366 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
367 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
368 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
369 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
370 { 0x3038, T_REG_64, "good_octets_sent", },
371 { 0x3040, T_REG_32, "good_frames_sent", },
372 { 0x3044, T_REG_32, "excessive_collision", },
373 { 0x3048, T_REG_32, "multicast_frames_sent", },
374 { 0x304c, T_REG_32, "broadcast_frames_sent", },
375 { 0x3054, T_REG_32, "fc_sent", },
376 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
Russell King6d81f452018-01-02 17:25:04 +0000377 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
Russell King9b0cdef2015-10-22 18:37:30 +0100378};
379
willy tarreau74c41b02014-01-16 08:20:08 +0100380struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300381 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100382 u64 rx_packets;
383 u64 rx_bytes;
384 u64 tx_packets;
385 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300386};
387
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200388struct mvneta_pcpu_port {
389 /* Pointer to the shared port */
390 struct mvneta_port *pp;
391
392 /* Pointer to the CPU-local NAPI struct */
393 struct napi_struct napi;
394
395 /* Cause of the previous interrupt */
396 u32 cause_rx_tx;
397};
398
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300399struct mvneta_port {
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100400 u8 id;
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200401 struct mvneta_pcpu_port __percpu *ports;
402 struct mvneta_pcpu_stats __percpu *stats;
403
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300404 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100405 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300406 void __iomem *base;
407 struct mvneta_rx_queue *rxqs;
408 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300409 struct net_device *dev;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200410 struct hlist_node node_online;
411 struct hlist_node node_dead;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100412 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100413 /* Protect the access to the percpu interrupt registers,
414 * ensuring that the configuration remains coherent.
415 */
416 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100417 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300418
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100419 u32 cause_rx_tx;
420 struct napi_struct napi;
421
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300422 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100423 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800424 /* AXI clock */
425 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300426 u8 mcast_count[256];
427 u16 tx_ring_size;
428 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300429
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300430 phy_interface_t phy_interface;
Russell King503f9aa92018-01-02 17:24:44 +0000431 struct device_node *dn;
Simon Guinotb65657f2015-06-30 16:20:22 +0200432 unsigned int tx_csum_limit;
Russell King503f9aa92018-01-02 17:24:44 +0000433 struct phylink *phylink;
Russell King9b0cdef2015-10-22 18:37:30 +0100434
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100435 struct mvneta_bm *bm_priv;
436 struct mvneta_bm_pool *pool_long;
437 struct mvneta_bm_pool *pool_short;
438 int bm_win_id;
439
Russell King6d81f452018-01-02 17:25:04 +0000440 bool eee_enabled;
441 bool eee_active;
442 bool tx_lpi_enabled;
443
Russell King9b0cdef2015-10-22 18:37:30 +0100444 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100445
446 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100447
448 /* Flags for special SoC configurations */
449 bool neta_armada3700;
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100450 u16 rx_offset_correction;
Jane Li9768b452017-03-16 16:22:28 +0800451 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300452};
453
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100454/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300455 * layout of the transmit and reception DMA descriptors, and their
456 * layout is therefore defined by the hardware design
457 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200458
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300459#define MVNETA_TX_L3_OFF_SHIFT 0
460#define MVNETA_TX_IP_HLEN_SHIFT 8
461#define MVNETA_TX_L4_UDP BIT(16)
462#define MVNETA_TX_L3_IP6 BIT(17)
463#define MVNETA_TXD_IP_CSUM BIT(18)
464#define MVNETA_TXD_Z_PAD BIT(19)
465#define MVNETA_TXD_L_DESC BIT(20)
466#define MVNETA_TXD_F_DESC BIT(21)
467#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
468 MVNETA_TXD_L_DESC | \
469 MVNETA_TXD_F_DESC)
470#define MVNETA_TX_L4_CSUM_FULL BIT(30)
471#define MVNETA_TX_L4_CSUM_NOT BIT(31)
472
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300473#define MVNETA_RXD_ERR_CRC 0x0
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100474#define MVNETA_RXD_BM_POOL_SHIFT 13
475#define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300476#define MVNETA_RXD_ERR_SUMMARY BIT(16)
477#define MVNETA_RXD_ERR_OVERRUN BIT(17)
478#define MVNETA_RXD_ERR_LEN BIT(18)
479#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
480#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
481#define MVNETA_RXD_L3_IP4 BIT(25)
482#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
483#define MVNETA_RXD_L4_CSUM_OK BIT(30)
484
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200485#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200486struct mvneta_tx_desc {
487 u32 command; /* Options used by HW for packet transmitting.*/
488 u16 reserverd1; /* csum_l4 (for future use) */
489 u16 data_size; /* Data size of transmitted packet in bytes */
490 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
491 u32 reserved2; /* hw_cmd - (for future use, PMT) */
492 u32 reserved3[4]; /* Reserved - (for future use) */
493};
494
495struct mvneta_rx_desc {
496 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300497 u16 reserved1; /* pnc_info - (for future use, PnC) */
498 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200499
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300500 u32 buf_phys_addr; /* Physical address of the buffer */
501 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200502
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300503 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
504 u16 reserved3; /* prefetch_cmd, for future use */
505 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200506
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300507 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
508 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
509};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200510#else
511struct mvneta_tx_desc {
512 u16 data_size; /* Data size of transmitted packet in bytes */
513 u16 reserverd1; /* csum_l4 (for future use) */
514 u32 command; /* Options used by HW for packet transmitting.*/
515 u32 reserved2; /* hw_cmd - (for future use, PMT) */
516 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
517 u32 reserved3[4]; /* Reserved - (for future use) */
518};
519
520struct mvneta_rx_desc {
521 u16 data_size; /* Size of received packet in bytes */
522 u16 reserved1; /* pnc_info - (for future use, PnC) */
523 u32 status; /* Info about received packet */
524
525 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
526 u32 buf_phys_addr; /* Physical address of the buffer */
527
528 u16 reserved4; /* csum_l4 - (for future use, PnC) */
529 u16 reserved3; /* prefetch_cmd, for future use */
530 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
531
532 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
533 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
534};
535#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300536
537struct mvneta_tx_queue {
538 /* Number of this TX queue, in the range 0-7 */
539 u8 id;
540
541 /* Number of TX DMA descriptors in the descriptor ring */
542 int size;
543
544 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100545 * descriptor ring
546 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300547 int count;
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100548 int pending;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300549 int tx_stop_threshold;
550 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300551
552 /* Array of transmitted skb */
553 struct sk_buff **tx_skb;
554
555 /* Index of last TX DMA descriptor that was inserted */
556 int txq_put_index;
557
558 /* Index of the TX DMA descriptor to be cleaned up */
559 int txq_get_index;
560
561 u32 done_pkts_coal;
562
563 /* Virtual address of the TX DMA descriptors array */
564 struct mvneta_tx_desc *descs;
565
566 /* DMA address of the TX DMA descriptors array */
567 dma_addr_t descs_phys;
568
569 /* Index of the last TX DMA descriptor */
570 int last_desc;
571
572 /* Index of the next TX DMA descriptor to process */
573 int next_desc_to_proc;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300574
575 /* DMA buffers for TSO headers */
576 char *tso_hdrs;
577
578 /* DMA address of TSO headers */
579 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100580
581 /* Affinity mask for CPUs*/
582 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300583};
584
585struct mvneta_rx_queue {
586 /* rx queue number, in the range 0-7 */
587 u8 id;
588
589 /* num of rx descriptors in the rx descriptor ring */
590 int size;
591
592 /* counter of times when mvneta_refill() failed */
593 int missed;
594
595 u32 pkts_coal;
596 u32 time_coal;
597
Gregory CLEMENTf88bee12016-12-01 18:03:06 +0100598 /* Virtual address of the RX buffer */
599 void **buf_virt_addr;
600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300601 /* Virtual address of the RX DMA descriptors array */
602 struct mvneta_rx_desc *descs;
603
604 /* DMA address of the RX DMA descriptors array */
605 dma_addr_t descs_phys;
606
607 /* Index of the last RX DMA descriptor */
608 int last_desc;
609
610 /* Index of the next RX DMA descriptor to process */
611 int next_desc_to_proc;
612};
613
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200614static enum cpuhp_state online_hpstate;
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300615/* The hardware supports eight (8) rx queues, but we are only allowing
616 * the first one to be used. Therefore, let's just allocate one queue.
617 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200618static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300619static int txq_number = 8;
620
621static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300622
willy tarreauf19fadf2014-01-16 08:20:17 +0100623static int rx_copybreak __read_mostly = 256;
624
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100625/* HW BM need that each port be identify by a unique ID */
626static int global_port_id;
627
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300628#define MVNETA_DRIVER_NAME "mvneta"
629#define MVNETA_DRIVER_VERSION "1.0"
630
631/* Utility/helper methods */
632
633/* Write helper method */
634static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
635{
636 writel(data, pp->base + offset);
637}
638
639/* Read helper method */
640static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
641{
642 return readl(pp->base + offset);
643}
644
645/* Increment txq get counter */
646static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
647{
648 txq->txq_get_index++;
649 if (txq->txq_get_index == txq->size)
650 txq->txq_get_index = 0;
651}
652
653/* Increment txq put counter */
654static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
655{
656 txq->txq_put_index++;
657 if (txq->txq_put_index == txq->size)
658 txq->txq_put_index = 0;
659}
660
661
662/* Clear all MIB counters */
663static void mvneta_mib_counters_clear(struct mvneta_port *pp)
664{
665 int i;
666 u32 dummy;
667
668 /* Perform dummy reads from MIB counters */
669 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
670 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100671 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
672 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300673}
674
675/* Get System Network Statistics */
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800676static void
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +0800677mvneta_get_stats64(struct net_device *dev,
678 struct rtnl_link_stats64 *stats)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300679{
680 struct mvneta_port *pp = netdev_priv(dev);
681 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100682 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300683
willy tarreau74c41b02014-01-16 08:20:08 +0100684 for_each_possible_cpu(cpu) {
685 struct mvneta_pcpu_stats *cpu_stats;
686 u64 rx_packets;
687 u64 rx_bytes;
688 u64 tx_packets;
689 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300690
willy tarreau74c41b02014-01-16 08:20:08 +0100691 cpu_stats = per_cpu_ptr(pp->stats, cpu);
692 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700693 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100694 rx_packets = cpu_stats->rx_packets;
695 rx_bytes = cpu_stats->rx_bytes;
696 tx_packets = cpu_stats->tx_packets;
697 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700698 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300699
willy tarreau74c41b02014-01-16 08:20:08 +0100700 stats->rx_packets += rx_packets;
701 stats->rx_bytes += rx_bytes;
702 stats->tx_packets += tx_packets;
703 stats->tx_bytes += tx_bytes;
704 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300705
706 stats->rx_errors = dev->stats.rx_errors;
707 stats->rx_dropped = dev->stats.rx_dropped;
708
709 stats->tx_dropped = dev->stats.tx_dropped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300710}
711
712/* Rx descriptors helper methods */
713
willy tarreau54282132014-01-16 08:20:14 +0100714/* Checks whether the RX descriptor having this status is both the first
715 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300716 * received through a single RX descriptor, so not having each RX
717 * descriptor with its first and last bits set is an error
718 */
willy tarreau54282132014-01-16 08:20:14 +0100719static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300720{
willy tarreau54282132014-01-16 08:20:14 +0100721 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300722 MVNETA_RXD_FIRST_LAST_DESC;
723}
724
725/* Add number of descriptors ready to receive new packets */
726static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
727 struct mvneta_rx_queue *rxq,
728 int ndescs)
729{
730 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100731 * be added at once
732 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300733 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
734 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
735 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
736 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
737 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
738 }
739
740 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
741 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
742}
743
744/* Get number of RX descriptors occupied by received packets */
745static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
746 struct mvneta_rx_queue *rxq)
747{
748 u32 val;
749
750 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
751 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
752}
753
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100754/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300755 * from mvneta_rxq_drop_pkts().
756 */
757static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
758 struct mvneta_rx_queue *rxq,
759 int rx_done, int rx_filled)
760{
761 u32 val;
762
763 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
764 val = rx_done |
765 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
766 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
767 return;
768 }
769
770 /* Only 255 descriptors can be added at once */
771 while ((rx_done > 0) || (rx_filled > 0)) {
772 if (rx_done <= 0xff) {
773 val = rx_done;
774 rx_done = 0;
775 } else {
776 val = 0xff;
777 rx_done -= 0xff;
778 }
779 if (rx_filled <= 0xff) {
780 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
781 rx_filled = 0;
782 } else {
783 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
784 rx_filled -= 0xff;
785 }
786 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
787 }
788}
789
790/* Get pointer to next RX descriptor to be processed by SW */
791static struct mvneta_rx_desc *
792mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
793{
794 int rx_desc = rxq->next_desc_to_proc;
795
796 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100797 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300798 return rxq->descs + rx_desc;
799}
800
801/* Change maximum receive size of the port. */
802static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
803{
804 u32 val;
805
806 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
807 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
808 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
809 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
810 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
811}
812
813
814/* Set rx queue offset */
815static void mvneta_rxq_offset_set(struct mvneta_port *pp,
816 struct mvneta_rx_queue *rxq,
817 int offset)
818{
819 u32 val;
820
821 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
822 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
823
824 /* Offset is in */
825 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
826 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
827}
828
829
830/* Tx descriptors helper methods */
831
832/* Update HW with number of TX descriptors to be sent */
833static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
834 struct mvneta_tx_queue *txq,
835 int pend_desc)
836{
837 u32 val;
838
Simon Guinot0d637852017-11-13 16:27:02 +0100839 pend_desc += txq->pending;
840
841 /* Only 255 Tx descriptors can be added at once */
842 do {
843 val = min(pend_desc, 255);
844 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
845 pend_desc -= val;
846 } while (pend_desc > 0);
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100847 txq->pending = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300848}
849
850/* Get pointer to next TX descriptor to be processed (send) by HW */
851static struct mvneta_tx_desc *
852mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
853{
854 int tx_desc = txq->next_desc_to_proc;
855
856 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
857 return txq->descs + tx_desc;
858}
859
860/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100861 * mapping failures in the TX path.
862 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300863static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
864{
865 if (txq->next_desc_to_proc == 0)
866 txq->next_desc_to_proc = txq->last_desc - 1;
867 else
868 txq->next_desc_to_proc--;
869}
870
871/* Set rxq buf size */
872static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
873 struct mvneta_rx_queue *rxq,
874 int buf_size)
875{
876 u32 val;
877
878 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
879
880 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
881 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
882
883 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
884}
885
886/* Disable buffer management (BM) */
887static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
888 struct mvneta_rx_queue *rxq)
889{
890 u32 val;
891
892 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
893 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
894 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
895}
896
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100897/* Enable buffer management (BM) */
898static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
899 struct mvneta_rx_queue *rxq)
900{
901 u32 val;
902
903 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
904 val |= MVNETA_RXQ_HW_BUF_ALLOC;
905 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
906}
907
908/* Notify HW about port's assignment of pool for bigger packets */
909static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
910 struct mvneta_rx_queue *rxq)
911{
912 u32 val;
913
914 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
915 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
916 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
917
918 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
919}
920
921/* Notify HW about port's assignment of pool for smaller packets */
922static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
923 struct mvneta_rx_queue *rxq)
924{
925 u32 val;
926
927 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
928 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
929 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
930
931 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
932}
933
934/* Set port's receive buffer size for assigned BM pool */
935static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
936 int buf_size,
937 u8 pool_id)
938{
939 u32 val;
940
941 if (!IS_ALIGNED(buf_size, 8)) {
942 dev_warn(pp->dev->dev.parent,
943 "illegal buf_size value %d, round to %d\n",
944 buf_size, ALIGN(buf_size, 8));
945 buf_size = ALIGN(buf_size, 8);
946 }
947
948 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
949 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
950 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
951}
952
953/* Configure MBUS window in order to enable access BM internal SRAM */
954static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
955 u8 target, u8 attr)
956{
957 u32 win_enable, win_protect;
958 int i;
959
960 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
961
962 if (pp->bm_win_id < 0) {
963 /* Find first not occupied window */
964 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
965 if (win_enable & (1 << i)) {
966 pp->bm_win_id = i;
967 break;
968 }
969 }
970 if (i == MVNETA_MAX_DECODE_WIN)
971 return -ENOMEM;
972 } else {
973 i = pp->bm_win_id;
974 }
975
976 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
977 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
978
979 if (i < 4)
980 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
981
982 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
983 (attr << 8) | target);
984
985 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
986
987 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
988 win_protect |= 3 << (2 * i);
989 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
990
991 win_enable &= ~(1 << i);
992 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
993
994 return 0;
995}
996
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100997static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100998{
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100999 u32 wsize;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001000 u8 target, attr;
1001 int err;
1002
1003 /* Get BM window information */
1004 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1005 &target, &attr);
1006 if (err < 0)
1007 return err;
1008
1009 pp->bm_win_id = -1;
1010
1011 /* Open NETA -> BM window */
1012 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1013 target, attr);
1014 if (err < 0) {
1015 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1016 return err;
1017 }
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001018 return 0;
1019}
1020
1021/* Assign and initialize pools for port. In case of fail
1022 * buffer manager will remain disabled for current port.
1023 */
1024static int mvneta_bm_port_init(struct platform_device *pdev,
1025 struct mvneta_port *pp)
1026{
1027 struct device_node *dn = pdev->dev.of_node;
1028 u32 long_pool_id, short_pool_id;
1029
1030 if (!pp->neta_armada3700) {
1031 int ret;
1032
1033 ret = mvneta_bm_port_mbus_init(pp);
1034 if (ret)
1035 return ret;
1036 }
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001037
1038 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1039 netdev_info(pp->dev, "missing long pool id\n");
1040 return -EINVAL;
1041 }
1042
1043 /* Create port's long pool depending on mtu */
1044 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1045 MVNETA_BM_LONG, pp->id,
1046 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1047 if (!pp->pool_long) {
1048 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1049 return -ENOMEM;
1050 }
1051
1052 pp->pool_long->port_map |= 1 << pp->id;
1053
1054 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1055 pp->pool_long->id);
1056
1057 /* If short pool id is not defined, assume using single pool */
1058 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1059 short_pool_id = long_pool_id;
1060
1061 /* Create port's short pool */
1062 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1063 MVNETA_BM_SHORT, pp->id,
1064 MVNETA_BM_SHORT_PKT_SIZE);
1065 if (!pp->pool_short) {
1066 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1067 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1068 return -ENOMEM;
1069 }
1070
1071 if (short_pool_id != long_pool_id) {
1072 pp->pool_short->port_map |= 1 << pp->id;
1073 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1074 pp->pool_short->id);
1075 }
1076
1077 return 0;
1078}
1079
1080/* Update settings of a pool for bigger packets */
1081static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1082{
1083 struct mvneta_bm_pool *bm_pool = pp->pool_long;
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001084 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001085 int num;
1086
1087 /* Release all buffers from long pool */
1088 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001089 if (hwbm_pool->buf_num) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001090 WARN(1, "cannot free all buffers in pool %d\n",
1091 bm_pool->id);
1092 goto bm_mtu_err;
1093 }
1094
1095 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1096 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001097 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1098 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001099
1100 /* Fill entire long pool */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001101 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1102 if (num != hwbm_pool->size) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001103 WARN(1, "pool %d: %d of %d allocated\n",
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001104 bm_pool->id, num, hwbm_pool->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001105 goto bm_mtu_err;
1106 }
1107 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1108
1109 return;
1110
1111bm_mtu_err:
1112 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1113 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1114
1115 pp->bm_priv = NULL;
1116 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1117 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1118}
1119
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001120/* Start the Ethernet port RX and TX activity */
1121static void mvneta_port_up(struct mvneta_port *pp)
1122{
1123 int queue;
1124 u32 q_map;
1125
1126 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001127 q_map = 0;
1128 for (queue = 0; queue < txq_number; queue++) {
1129 struct mvneta_tx_queue *txq = &pp->txqs[queue];
Markus Elfringf95936c2017-04-16 22:45:33 +02001130 if (txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001131 q_map |= (1 << queue);
1132 }
1133 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1134
1135 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001136 for (queue = 0; queue < rxq_number; queue++) {
1137 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1138
Markus Elfringf95936c2017-04-16 22:45:33 +02001139 if (rxq->descs)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001140 q_map |= (1 << queue);
1141 }
1142 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001143}
1144
1145/* Stop the Ethernet port activity */
1146static void mvneta_port_down(struct mvneta_port *pp)
1147{
1148 u32 val;
1149 int count;
1150
1151 /* Stop Rx port activity. Check port Rx activity. */
1152 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1153
1154 /* Issue stop command for active channels only */
1155 if (val != 0)
1156 mvreg_write(pp, MVNETA_RXQ_CMD,
1157 val << MVNETA_RXQ_DISABLE_SHIFT);
1158
1159 /* Wait for all Rx activity to terminate. */
1160 count = 0;
1161 do {
1162 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1163 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001164 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001165 val);
1166 break;
1167 }
1168 mdelay(1);
1169
1170 val = mvreg_read(pp, MVNETA_RXQ_CMD);
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001171 } while (val & MVNETA_RXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001172
1173 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001174 * command for active channels only
1175 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001176 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1177
1178 if (val != 0)
1179 mvreg_write(pp, MVNETA_TXQ_CMD,
1180 (val << MVNETA_TXQ_DISABLE_SHIFT));
1181
1182 /* Wait for all Tx activity to terminate. */
1183 count = 0;
1184 do {
1185 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1186 netdev_warn(pp->dev,
1187 "TIMEOUT for TX stopped status=0x%08x\n",
1188 val);
1189 break;
1190 }
1191 mdelay(1);
1192
1193 /* Check TX Command reg that all Txqs are stopped */
1194 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1195
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001196 } while (val & MVNETA_TXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001197
1198 /* Double check to verify that TX FIFO is empty */
1199 count = 0;
1200 do {
1201 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1202 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001203 "TX FIFO empty timeout status=0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001204 val);
1205 break;
1206 }
1207 mdelay(1);
1208
1209 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1210 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1211 (val & MVNETA_TX_IN_PRGRS));
1212
1213 udelay(200);
1214}
1215
1216/* Enable the port by setting the port enable bit of the MAC control register */
1217static void mvneta_port_enable(struct mvneta_port *pp)
1218{
1219 u32 val;
1220
1221 /* Enable port */
1222 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1223 val |= MVNETA_GMAC0_PORT_ENABLE;
1224 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1225}
1226
1227/* Disable the port and wait for about 200 usec before retuning */
1228static void mvneta_port_disable(struct mvneta_port *pp)
1229{
1230 u32 val;
1231
1232 /* Reset the Enable bit in the Serial Control Register */
1233 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1234 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1235 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1236
1237 udelay(200);
1238}
1239
1240/* Multicast tables methods */
1241
1242/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1243static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1244{
1245 int offset;
1246 u32 val;
1247
1248 if (queue == -1) {
1249 val = 0;
1250 } else {
1251 val = 0x1 | (queue << 1);
1252 val |= (val << 24) | (val << 16) | (val << 8);
1253 }
1254
1255 for (offset = 0; offset <= 0xc; offset += 4)
1256 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1257}
1258
1259/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1260static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1261{
1262 int offset;
1263 u32 val;
1264
1265 if (queue == -1) {
1266 val = 0;
1267 } else {
1268 val = 0x1 | (queue << 1);
1269 val |= (val << 24) | (val << 16) | (val << 8);
1270 }
1271
1272 for (offset = 0; offset <= 0xfc; offset += 4)
1273 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1274
1275}
1276
1277/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1278static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1279{
1280 int offset;
1281 u32 val;
1282
1283 if (queue == -1) {
1284 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1285 val = 0;
1286 } else {
1287 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1288 val = 0x1 | (queue << 1);
1289 val |= (val << 24) | (val << 16) | (val << 8);
1290 }
1291
1292 for (offset = 0; offset <= 0xfc; offset += 4)
1293 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1294}
1295
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001296static void mvneta_percpu_unmask_interrupt(void *arg)
1297{
1298 struct mvneta_port *pp = arg;
1299
1300 /* All the queue are unmasked, but actually only the ones
1301 * mapped to this CPU will be unmasked
1302 */
1303 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1304 MVNETA_RX_INTR_MASK_ALL |
1305 MVNETA_TX_INTR_MASK_ALL |
1306 MVNETA_MISCINTR_INTR_MASK);
1307}
1308
1309static void mvneta_percpu_mask_interrupt(void *arg)
1310{
1311 struct mvneta_port *pp = arg;
1312
1313 /* All the queue are masked, but actually only the ones
1314 * mapped to this CPU will be masked
1315 */
1316 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1317 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1318 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1319}
1320
1321static void mvneta_percpu_clear_intr_cause(void *arg)
1322{
1323 struct mvneta_port *pp = arg;
1324
1325 /* All the queue are cleared, but actually only the ones
1326 * mapped to this CPU will be cleared
1327 */
1328 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1329 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1330 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1331}
1332
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001333/* This method sets defaults to the NETA port:
1334 * Clears interrupt Cause and Mask registers.
1335 * Clears all MAC tables.
1336 * Sets defaults to all registers.
1337 * Resets RX and TX descriptor rings.
1338 * Resets PHY.
1339 * This method can be called after mvneta_port_down() to return the port
1340 * settings to defaults.
1341 */
1342static void mvneta_defaults_set(struct mvneta_port *pp)
1343{
1344 int cpu;
1345 int queue;
1346 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001347 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001348
1349 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001350 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001351
1352 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001353 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001354 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1355
1356 /* Enable MBUS Retry bit16 */
1357 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1358
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001359 /* Set CPU queue access map. CPUs are assigned to the RX and
1360 * TX queues modulo their number. If there is only one TX
1361 * queue then it is assigned to the CPU associated to the
1362 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001363 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001364 for_each_present_cpu(cpu) {
1365 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001366 int rxq, txq;
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001367 if (!pp->neta_armada3700) {
1368 for (rxq = 0; rxq < rxq_number; rxq++)
1369 if ((rxq % max_cpu) == cpu)
1370 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001371
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001372 for (txq = 0; txq < txq_number; txq++)
1373 if ((txq % max_cpu) == cpu)
1374 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001375
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001376 /* With only one TX queue we configure a special case
1377 * which will allow to get all the irq on a single
1378 * CPU
1379 */
1380 if (txq_number == 1)
1381 txq_map = (cpu == pp->rxq_def) ?
1382 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001383
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001384 } else {
1385 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1386 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1387 }
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001388
1389 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1390 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001391
1392 /* Reset RX and TX DMAs */
1393 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1394 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1395
1396 /* Disable Legacy WRR, Disable EJP, Release from reset */
1397 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1398 for (queue = 0; queue < txq_number; queue++) {
1399 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1400 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1401 }
1402
1403 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1404 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1405
1406 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001407 if (pp->bm_priv)
1408 /* HW buffer management + legacy parser */
1409 val = MVNETA_ACC_MODE_EXT2;
1410 else
1411 /* SW buffer management + legacy parser */
1412 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001413 mvreg_write(pp, MVNETA_ACC_MODE, val);
1414
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001415 if (pp->bm_priv)
1416 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1417
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001418 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001419 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001420 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1421
1422 val = 0;
1423 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1424 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1425
1426 /* Build PORT_SDMA_CONFIG_REG */
1427 val = 0;
1428
1429 /* Default burst size */
1430 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1431 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001432 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001433
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001434#if defined(__BIG_ENDIAN)
1435 val |= MVNETA_DESC_SWAP;
1436#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001437
1438 /* Assign port SDMA configuration */
1439 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1440
Thomas Petazzoni71408602013-09-04 16:21:18 +02001441 /* Disable PHY polling in hardware, since we're using the
1442 * kernel phylib to do this.
1443 */
1444 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1445 val &= ~MVNETA_PHY_POLLING_ENABLE;
1446 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1447
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001448 mvneta_set_ucast_table(pp, -1);
1449 mvneta_set_special_mcast_table(pp, -1);
1450 mvneta_set_other_mcast_table(pp, -1);
1451
1452 /* Set port interrupt enable register - default enable all */
1453 mvreg_write(pp, MVNETA_INTR_ENABLE,
1454 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1455 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001456
1457 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001458}
1459
1460/* Set max sizes for tx queues */
1461static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1462
1463{
1464 u32 val, size, mtu;
1465 int queue;
1466
1467 mtu = max_tx_size * 8;
1468 if (mtu > MVNETA_TX_MTU_MAX)
1469 mtu = MVNETA_TX_MTU_MAX;
1470
1471 /* Set MTU */
1472 val = mvreg_read(pp, MVNETA_TX_MTU);
1473 val &= ~MVNETA_TX_MTU_MAX;
1474 val |= mtu;
1475 mvreg_write(pp, MVNETA_TX_MTU, val);
1476
1477 /* TX token size and all TXQs token size must be larger that MTU */
1478 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1479
1480 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1481 if (size < mtu) {
1482 size = mtu;
1483 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1484 val |= size;
1485 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1486 }
1487 for (queue = 0; queue < txq_number; queue++) {
1488 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1489
1490 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1491 if (size < mtu) {
1492 size = mtu;
1493 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1494 val |= size;
1495 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1496 }
1497 }
1498}
1499
1500/* Set unicast address */
1501static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1502 int queue)
1503{
1504 unsigned int unicast_reg;
1505 unsigned int tbl_offset;
1506 unsigned int reg_offset;
1507
1508 /* Locate the Unicast table entry */
1509 last_nibble = (0xf & last_nibble);
1510
1511 /* offset from unicast tbl base */
1512 tbl_offset = (last_nibble / 4) * 4;
1513
1514 /* offset within the above reg */
1515 reg_offset = last_nibble % 4;
1516
1517 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1518
1519 if (queue == -1) {
1520 /* Clear accepts frame bit at specified unicast DA tbl entry */
1521 unicast_reg &= ~(0xff << (8 * reg_offset));
1522 } else {
1523 unicast_reg &= ~(0xff << (8 * reg_offset));
1524 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1525 }
1526
1527 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1528}
1529
1530/* Set mac address */
1531static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1532 int queue)
1533{
1534 unsigned int mac_h;
1535 unsigned int mac_l;
1536
1537 if (queue != -1) {
1538 mac_l = (addr[4] << 8) | (addr[5]);
1539 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1540 (addr[2] << 8) | (addr[3] << 0);
1541
1542 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1543 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1544 }
1545
1546 /* Accept frames of this address */
1547 mvneta_set_ucast_addr(pp, addr[5], queue);
1548}
1549
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001550/* Set the number of packets that will be received before RX interrupt
1551 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001552 */
1553static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1554 struct mvneta_rx_queue *rxq, u32 value)
1555{
1556 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1557 value | MVNETA_RXQ_NON_OCCUPIED(0));
1558 rxq->pkts_coal = value;
1559}
1560
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001561/* Set the time delay in usec before RX interrupt will be generated by
1562 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001563 */
1564static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1565 struct mvneta_rx_queue *rxq, u32 value)
1566{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001567 u32 val;
1568 unsigned long clk_rate;
1569
1570 clk_rate = clk_get_rate(pp->clk);
1571 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001572
1573 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1574 rxq->time_coal = value;
1575}
1576
1577/* Set threshold for TX_DONE pkts coalescing */
1578static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1579 struct mvneta_tx_queue *txq, u32 value)
1580{
1581 u32 val;
1582
1583 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1584
1585 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1586 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1587
1588 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1589
1590 txq->done_pkts_coal = value;
1591}
1592
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001593/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1594static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001595 u32 phys_addr, void *virt_addr,
1596 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001597{
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001598 int i;
1599
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001600 rx_desc->buf_phys_addr = phys_addr;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001601 i = rx_desc - rxq->descs;
1602 rxq->buf_virt_addr[i] = virt_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001603}
1604
1605/* Decrement sent descriptors counter */
1606static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1607 struct mvneta_tx_queue *txq,
1608 int sent_desc)
1609{
1610 u32 val;
1611
1612 /* Only 255 TX descriptors can be updated at once */
1613 while (sent_desc > 0xff) {
1614 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1615 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1616 sent_desc = sent_desc - 0xff;
1617 }
1618
1619 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1620 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1621}
1622
1623/* Get number of TX descriptors already sent by HW */
1624static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1625 struct mvneta_tx_queue *txq)
1626{
1627 u32 val;
1628 int sent_desc;
1629
1630 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1631 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1632 MVNETA_TXQ_SENT_DESC_SHIFT;
1633
1634 return sent_desc;
1635}
1636
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001637/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001638 * The number of sent descriptors is returned.
1639 */
1640static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1641 struct mvneta_tx_queue *txq)
1642{
1643 int sent_desc;
1644
1645 /* Get number of sent descriptors */
1646 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1647
1648 /* Decrement sent descriptors counter */
1649 if (sent_desc)
1650 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1651
1652 return sent_desc;
1653}
1654
1655/* Set TXQ descriptors fields relevant for CSUM calculation */
1656static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1657 int ip_hdr_len, int l4_proto)
1658{
1659 u32 command;
1660
1661 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001662 * G_L4_chk, L4_type; required only for checksum
1663 * calculation
1664 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001665 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1666 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1667
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001668 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001669 command |= MVNETA_TXD_IP_CSUM;
1670 else
1671 command |= MVNETA_TX_L3_IP6;
1672
1673 if (l4_proto == IPPROTO_TCP)
1674 command |= MVNETA_TX_L4_CSUM_FULL;
1675 else if (l4_proto == IPPROTO_UDP)
1676 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1677 else
1678 command |= MVNETA_TX_L4_CSUM_NOT;
1679
1680 return command;
1681}
1682
1683
1684/* Display more error info */
1685static void mvneta_rx_error(struct mvneta_port *pp,
1686 struct mvneta_rx_desc *rx_desc)
1687{
1688 u32 status = rx_desc->status;
1689
willy tarreau54282132014-01-16 08:20:14 +01001690 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001691 netdev_err(pp->dev,
1692 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001693 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001694 return;
1695 }
1696
1697 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1698 case MVNETA_RXD_ERR_CRC:
1699 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1700 status, rx_desc->data_size);
1701 break;
1702 case MVNETA_RXD_ERR_OVERRUN:
1703 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1704 status, rx_desc->data_size);
1705 break;
1706 case MVNETA_RXD_ERR_LEN:
1707 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1708 status, rx_desc->data_size);
1709 break;
1710 case MVNETA_RXD_ERR_RESOURCE:
1711 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1712 status, rx_desc->data_size);
1713 break;
1714 }
1715}
1716
willy tarreau54282132014-01-16 08:20:14 +01001717/* Handle RX checksum offload based on the descriptor's status */
1718static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001719 struct sk_buff *skb)
1720{
willy tarreau54282132014-01-16 08:20:14 +01001721 if ((status & MVNETA_RXD_L3_IP4) &&
1722 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001723 skb->csum = 0;
1724 skb->ip_summed = CHECKSUM_UNNECESSARY;
1725 return;
1726 }
1727
1728 skb->ip_summed = CHECKSUM_NONE;
1729}
1730
willy tarreau6c498972014-01-16 08:20:12 +01001731/* Return tx queue pointer (find last set bit) according to <cause> returned
1732 * form tx_done reg. <cause> must not be null. The return value is always a
1733 * valid queue for matching the first one found in <cause>.
1734 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001735static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1736 u32 cause)
1737{
1738 int queue = fls(cause) - 1;
1739
willy tarreau6c498972014-01-16 08:20:12 +01001740 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001741}
1742
1743/* Free tx queue skbuffs */
1744static void mvneta_txq_bufs_free(struct mvneta_port *pp,
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001745 struct mvneta_tx_queue *txq, int num,
1746 struct netdev_queue *nq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001747{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001748 unsigned int bytes_compl = 0, pkts_compl = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001749 int i;
1750
1751 for (i = 0; i < num; i++) {
1752 struct mvneta_tx_desc *tx_desc = txq->descs +
1753 txq->txq_get_index;
1754 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1755
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001756 if (skb) {
1757 bytes_compl += skb->len;
1758 pkts_compl++;
1759 }
1760
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001761 mvneta_txq_inc_get(txq);
1762
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001763 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1764 dma_unmap_single(pp->dev->dev.parent,
1765 tx_desc->buf_phys_addr,
1766 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001767 if (!skb)
1768 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001769 dev_kfree_skb_any(skb);
1770 }
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001771
1772 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001773}
1774
1775/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001776static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001777 struct mvneta_tx_queue *txq)
1778{
1779 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1780 int tx_done;
1781
1782 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001783 if (!tx_done)
1784 return;
1785
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001786 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001787
1788 txq->count -= tx_done;
1789
1790 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001791 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001792 netif_tx_wake_queue(nq);
1793 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001794}
1795
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001796void *mvneta_frag_alloc(unsigned int frag_size)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001797{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001798 if (likely(frag_size <= PAGE_SIZE))
1799 return netdev_alloc_frag(frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001800 else
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001801 return kmalloc(frag_size, GFP_ATOMIC);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001802}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001803EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001804
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001805void mvneta_frag_free(unsigned int frag_size, void *data)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001806{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001807 if (likely(frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001808 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001809 else
1810 kfree(data);
1811}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001812EXPORT_SYMBOL_GPL(mvneta_frag_free);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001813
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001814/* Refill processing for SW buffer management */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001815static int mvneta_rx_refill(struct mvneta_port *pp,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001816 struct mvneta_rx_desc *rx_desc,
1817 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001818
1819{
1820 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001821 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001822
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001823 data = mvneta_frag_alloc(pp->frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001824 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001825 return -ENOMEM;
1826
willy tarreau8ec2cd42014-01-16 08:20:16 +01001827 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001828 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1829 DMA_FROM_DEVICE);
1830 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001831 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001832 return -ENOMEM;
1833 }
1834
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01001835 phys_addr += pp->rx_offset_correction;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001836 mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001837 return 0;
1838}
1839
1840/* Handle tx checksum */
1841static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1842{
1843 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1844 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001845 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001846 u8 l4_proto;
1847
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001848 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001849 struct iphdr *ip4h = ip_hdr(skb);
1850
1851 /* Calculate IPv4 checksum and L4 checksum */
1852 ip_hdr_len = ip4h->ihl;
1853 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001854 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001855 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1856
1857 /* Read l4_protocol from one of IPv6 extra headers */
1858 if (skb_network_header_len(skb) > 0)
1859 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1860 l4_proto = ip6h->nexthdr;
1861 } else
1862 return MVNETA_TX_L4_CSUM_NOT;
1863
1864 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001865 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001866 }
1867
1868 return MVNETA_TX_L4_CSUM_NOT;
1869}
1870
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001871/* Drop packets received by the RXQ and free buffers */
1872static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1873 struct mvneta_rx_queue *rxq)
1874{
1875 int rx_done, i;
1876
1877 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001878 if (rx_done)
1879 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1880
1881 if (pp->bm_priv) {
1882 for (i = 0; i < rx_done; i++) {
1883 struct mvneta_rx_desc *rx_desc =
1884 mvneta_rxq_next_desc_get(rxq);
1885 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1886 struct mvneta_bm_pool *bm_pool;
1887
1888 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1889 /* Return dropped buffer to the pool */
1890 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1891 rx_desc->buf_phys_addr);
1892 }
1893 return;
1894 }
1895
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001896 for (i = 0; i < rxq->size; i++) {
1897 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001898 void *data = rxq->buf_virt_addr[i];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001899
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001900 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001901 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001902 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001903 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001904}
1905
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001906/* Main rx processing when using software buffer management */
1907static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1908 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001909{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001910 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001911 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001912 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001913 u32 rcvd_pkts = 0;
1914 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001915
1916 /* Get number of received packets */
1917 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1918
1919 if (rx_todo > rx_done)
1920 rx_todo = rx_done;
1921
1922 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001923
1924 /* Fairness NAPI loop */
1925 while (rx_done < rx_todo) {
1926 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1927 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001928 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001929 dma_addr_t phys_addr;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001930 u32 rx_status, frag_size;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001931 int rx_bytes, err, index;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001932
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001933 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001934 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001935 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001936 index = rx_desc - rxq->descs;
1937 data = rxq->buf_virt_addr[index];
Simon Guinotdaf158d2015-09-15 22:41:21 +02001938 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001939
willy tarreau54282132014-01-16 08:20:14 +01001940 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001941 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
Yelena Krivosheev2eecb2e2017-12-19 17:59:47 +01001942 mvneta_rx_error(pp, rx_desc);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001943err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001944 dev->stats.rx_errors++;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001945 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001946 continue;
1947 }
1948
willy tarreauf19fadf2014-01-16 08:20:17 +01001949 if (rx_bytes <= rx_copybreak) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001950 /* better copy a small frame and not unmap the DMA region */
willy tarreauf19fadf2014-01-16 08:20:17 +01001951 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1952 if (unlikely(!skb))
1953 goto err_drop_frame;
1954
1955 dma_sync_single_range_for_cpu(dev->dev.parent,
Gregory CLEMENTac83b7d2016-12-01 18:03:04 +01001956 phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001957 MVNETA_MH_SIZE + NET_SKB_PAD,
1958 rx_bytes,
1959 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02001960 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
1961 rx_bytes);
willy tarreauf19fadf2014-01-16 08:20:17 +01001962
1963 skb->protocol = eth_type_trans(skb, dev);
1964 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001965 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001966
1967 rcvd_pkts++;
1968 rcvd_bytes += rx_bytes;
1969
1970 /* leave the descriptor and buffer untouched */
1971 continue;
1972 }
1973
Simon Guinota84e3282015-07-19 13:00:53 +02001974 /* Refill processing */
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001975 err = mvneta_rx_refill(pp, rx_desc, rxq);
Simon Guinota84e3282015-07-19 13:00:53 +02001976 if (err) {
1977 netdev_err(dev, "Linux processing - Can't refill\n");
1978 rxq->missed++;
1979 goto err_drop_frame;
1980 }
1981
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001982 frag_size = pp->frag_size;
1983
1984 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01001985
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001986 /* After refill old buffer has to be unmapped regardless
1987 * the skb is successfully built or not.
1988 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02001989 dma_unmap_single(dev->dev.parent, phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001990 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1991 DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001992
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001993 if (!skb)
1994 goto err_drop_frame;
1995
willy tarreaudc4277d2014-01-16 08:20:07 +01001996 rcvd_pkts++;
1997 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001998
1999 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01002000 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002001 skb_put(skb, rx_bytes);
2002
2003 skb->protocol = eth_type_trans(skb, dev);
2004
willy tarreau54282132014-01-16 08:20:14 +01002005 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002006
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002007 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002008 }
2009
willy tarreaudc4277d2014-01-16 08:20:07 +01002010 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01002011 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2012
2013 u64_stats_update_begin(&stats->syncp);
2014 stats->rx_packets += rcvd_pkts;
2015 stats->rx_bytes += rcvd_bytes;
2016 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01002017 }
2018
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002019 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02002020 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002021
2022 return rx_done;
2023}
2024
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002025/* Main rx processing when using hardware buffer management */
2026static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2027 struct mvneta_rx_queue *rxq)
2028{
2029 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2030 struct net_device *dev = pp->dev;
2031 int rx_done;
2032 u32 rcvd_pkts = 0;
2033 u32 rcvd_bytes = 0;
2034
2035 /* Get number of received packets */
2036 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2037
2038 if (rx_todo > rx_done)
2039 rx_todo = rx_done;
2040
2041 rx_done = 0;
2042
2043 /* Fairness NAPI loop */
2044 while (rx_done < rx_todo) {
2045 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2046 struct mvneta_bm_pool *bm_pool = NULL;
2047 struct sk_buff *skb;
2048 unsigned char *data;
2049 dma_addr_t phys_addr;
2050 u32 rx_status, frag_size;
2051 int rx_bytes, err;
2052 u8 pool_id;
2053
2054 rx_done++;
2055 rx_status = rx_desc->status;
2056 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002057 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002058 phys_addr = rx_desc->buf_phys_addr;
2059 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2060 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2061
2062 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2063 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2064err_drop_frame_ret_pool:
2065 /* Return the buffer to the pool */
2066 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2067 rx_desc->buf_phys_addr);
2068err_drop_frame:
2069 dev->stats.rx_errors++;
2070 mvneta_rx_error(pp, rx_desc);
2071 /* leave the descriptor untouched */
2072 continue;
2073 }
2074
2075 if (rx_bytes <= rx_copybreak) {
2076 /* better copy a small frame and not unmap the DMA region */
2077 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2078 if (unlikely(!skb))
2079 goto err_drop_frame_ret_pool;
2080
2081 dma_sync_single_range_for_cpu(dev->dev.parent,
2082 rx_desc->buf_phys_addr,
2083 MVNETA_MH_SIZE + NET_SKB_PAD,
2084 rx_bytes,
2085 DMA_FROM_DEVICE);
Johannes Berg59ae1d12017-06-16 14:29:20 +02002086 skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2087 rx_bytes);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002088
2089 skb->protocol = eth_type_trans(skb, dev);
2090 mvneta_rx_csum(pp, rx_status, skb);
2091 napi_gro_receive(&port->napi, skb);
2092
2093 rcvd_pkts++;
2094 rcvd_bytes += rx_bytes;
2095
2096 /* Return the buffer to the pool */
2097 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2098 rx_desc->buf_phys_addr);
2099
2100 /* leave the descriptor and buffer untouched */
2101 continue;
2102 }
2103
2104 /* Refill processing */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002105 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002106 if (err) {
2107 netdev_err(dev, "Linux processing - Can't refill\n");
2108 rxq->missed++;
2109 goto err_drop_frame_ret_pool;
2110 }
2111
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002112 frag_size = bm_pool->hwbm_pool.frag_size;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002113
2114 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2115
2116 /* After refill old buffer has to be unmapped regardless
2117 * the skb is successfully built or not.
2118 */
2119 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2120 bm_pool->buf_size, DMA_FROM_DEVICE);
2121 if (!skb)
2122 goto err_drop_frame;
2123
2124 rcvd_pkts++;
2125 rcvd_bytes += rx_bytes;
2126
2127 /* Linux processing */
2128 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2129 skb_put(skb, rx_bytes);
2130
2131 skb->protocol = eth_type_trans(skb, dev);
2132
2133 mvneta_rx_csum(pp, rx_status, skb);
2134
2135 napi_gro_receive(&port->napi, skb);
2136 }
2137
2138 if (rcvd_pkts) {
2139 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2140
2141 u64_stats_update_begin(&stats->syncp);
2142 stats->rx_packets += rcvd_pkts;
2143 stats->rx_bytes += rcvd_bytes;
2144 u64_stats_update_end(&stats->syncp);
2145 }
2146
2147 /* Update rxq management counters */
2148 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2149
2150 return rx_done;
2151}
2152
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002153static inline void
2154mvneta_tso_put_hdr(struct sk_buff *skb,
2155 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2156{
2157 struct mvneta_tx_desc *tx_desc;
2158 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2159
2160 txq->tx_skb[txq->txq_put_index] = NULL;
2161 tx_desc = mvneta_txq_next_desc_get(txq);
2162 tx_desc->data_size = hdr_len;
2163 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2164 tx_desc->command |= MVNETA_TXD_F_DESC;
2165 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2166 txq->txq_put_index * TSO_HEADER_SIZE;
2167 mvneta_txq_inc_put(txq);
2168}
2169
2170static inline int
2171mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2172 struct sk_buff *skb, char *data, int size,
2173 bool last_tcp, bool is_last)
2174{
2175 struct mvneta_tx_desc *tx_desc;
2176
2177 tx_desc = mvneta_txq_next_desc_get(txq);
2178 tx_desc->data_size = size;
2179 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2180 size, DMA_TO_DEVICE);
2181 if (unlikely(dma_mapping_error(dev->dev.parent,
2182 tx_desc->buf_phys_addr))) {
2183 mvneta_txq_desc_put(txq);
2184 return -ENOMEM;
2185 }
2186
2187 tx_desc->command = 0;
2188 txq->tx_skb[txq->txq_put_index] = NULL;
2189
2190 if (last_tcp) {
2191 /* last descriptor in the TCP packet */
2192 tx_desc->command = MVNETA_TXD_L_DESC;
2193
2194 /* last descriptor in SKB */
2195 if (is_last)
2196 txq->tx_skb[txq->txq_put_index] = skb;
2197 }
2198 mvneta_txq_inc_put(txq);
2199 return 0;
2200}
2201
2202static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2203 struct mvneta_tx_queue *txq)
2204{
2205 int total_len, data_left;
2206 int desc_count = 0;
2207 struct mvneta_port *pp = netdev_priv(dev);
2208 struct tso_t tso;
2209 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2210 int i;
2211
2212 /* Count needed descriptors */
2213 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2214 return 0;
2215
2216 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2217 pr_info("*** Is this even possible???!?!?\n");
2218 return 0;
2219 }
2220
2221 /* Initialize the TSO handler, and prepare the first payload */
2222 tso_start(skb, &tso);
2223
2224 total_len = skb->len - hdr_len;
2225 while (total_len > 0) {
2226 char *hdr;
2227
2228 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2229 total_len -= data_left;
2230 desc_count++;
2231
2232 /* prepare packet headers: MAC + IP + TCP */
2233 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2234 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2235
2236 mvneta_tso_put_hdr(skb, pp, txq);
2237
2238 while (data_left > 0) {
2239 int size;
2240 desc_count++;
2241
2242 size = min_t(int, tso.size, data_left);
2243
2244 if (mvneta_tso_put_data(dev, txq, skb,
2245 tso.data, size,
2246 size == data_left,
2247 total_len == 0))
2248 goto err_release;
2249 data_left -= size;
2250
2251 tso_build_data(skb, &tso, size);
2252 }
2253 }
2254
2255 return desc_count;
2256
2257err_release:
2258 /* Release all used data descriptors; header descriptors must not
2259 * be DMA-unmapped.
2260 */
2261 for (i = desc_count - 1; i >= 0; i--) {
2262 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002263 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002264 dma_unmap_single(pp->dev->dev.parent,
2265 tx_desc->buf_phys_addr,
2266 tx_desc->data_size,
2267 DMA_TO_DEVICE);
2268 mvneta_txq_desc_put(txq);
2269 }
2270 return 0;
2271}
2272
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002273/* Handle tx fragmentation processing */
2274static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2275 struct mvneta_tx_queue *txq)
2276{
2277 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002278 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002279
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002280 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002281 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2282 void *addr = page_address(frag->page.p) + frag->page_offset;
2283
2284 tx_desc = mvneta_txq_next_desc_get(txq);
2285 tx_desc->data_size = frag->size;
2286
2287 tx_desc->buf_phys_addr =
2288 dma_map_single(pp->dev->dev.parent, addr,
2289 tx_desc->data_size, DMA_TO_DEVICE);
2290
2291 if (dma_mapping_error(pp->dev->dev.parent,
2292 tx_desc->buf_phys_addr)) {
2293 mvneta_txq_desc_put(txq);
2294 goto error;
2295 }
2296
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002297 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002298 /* Last descriptor */
2299 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002300 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002301 } else {
2302 /* Descriptor in the middle: Not First, Not Last */
2303 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002304 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002305 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002306 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002307 }
2308
2309 return 0;
2310
2311error:
2312 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002313 * this packet, as well as the corresponding DMA mappings
2314 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002315 for (i = i - 1; i >= 0; i--) {
2316 tx_desc = txq->descs + i;
2317 dma_unmap_single(pp->dev->dev.parent,
2318 tx_desc->buf_phys_addr,
2319 tx_desc->data_size,
2320 DMA_TO_DEVICE);
2321 mvneta_txq_desc_put(txq);
2322 }
2323
2324 return -ENOMEM;
2325}
2326
2327/* Main tx processing */
2328static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2329{
2330 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002331 u16 txq_id = skb_get_queue_mapping(skb);
2332 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002333 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002334 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002335 int frags = 0;
2336 u32 tx_cmd;
2337
2338 if (!netif_running(dev))
2339 goto out;
2340
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002341 if (skb_is_gso(skb)) {
2342 frags = mvneta_tx_tso(skb, dev, txq);
2343 goto out;
2344 }
2345
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002346 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002347
2348 /* Get a descriptor for the first part of the packet */
2349 tx_desc = mvneta_txq_next_desc_get(txq);
2350
2351 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2352
2353 tx_desc->data_size = skb_headlen(skb);
2354
2355 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2356 tx_desc->data_size,
2357 DMA_TO_DEVICE);
2358 if (unlikely(dma_mapping_error(dev->dev.parent,
2359 tx_desc->buf_phys_addr))) {
2360 mvneta_txq_desc_put(txq);
2361 frags = 0;
2362 goto out;
2363 }
2364
2365 if (frags == 1) {
2366 /* First and Last descriptor */
2367 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2368 tx_desc->command = tx_cmd;
2369 txq->tx_skb[txq->txq_put_index] = skb;
2370 mvneta_txq_inc_put(txq);
2371 } else {
2372 /* First but not Last */
2373 tx_cmd |= MVNETA_TXD_F_DESC;
2374 txq->tx_skb[txq->txq_put_index] = NULL;
2375 mvneta_txq_inc_put(txq);
2376 tx_desc->command = tx_cmd;
2377 /* Continue with other skb fragments */
2378 if (mvneta_tx_frag_process(pp, skb, txq)) {
2379 dma_unmap_single(dev->dev.parent,
2380 tx_desc->buf_phys_addr,
2381 tx_desc->data_size,
2382 DMA_TO_DEVICE);
2383 mvneta_txq_desc_put(txq);
2384 frags = 0;
2385 goto out;
2386 }
2387 }
2388
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002389out:
2390 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01002391 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002392 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2393
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002394 netdev_tx_sent_queue(nq, len);
2395
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002396 txq->count += frags;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002397 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002398 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002399
Simon Guinot2a90f7e2017-01-16 18:08:31 +01002400 if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2401 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2402 mvneta_txq_pend_desc_add(pp, txq, frags);
2403 else
2404 txq->pending += frags;
2405
willy tarreau74c41b02014-01-16 08:20:08 +01002406 u64_stats_update_begin(&stats->syncp);
2407 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002408 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01002409 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002410 } else {
2411 dev->stats.tx_dropped++;
2412 dev_kfree_skb_any(skb);
2413 }
2414
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002415 return NETDEV_TX_OK;
2416}
2417
2418
2419/* Free tx resources, when resetting a port */
2420static void mvneta_txq_done_force(struct mvneta_port *pp,
2421 struct mvneta_tx_queue *txq)
2422
2423{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002424 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002425 int tx_done = txq->count;
2426
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002427 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002428
2429 /* reset txq */
2430 txq->count = 0;
2431 txq->txq_put_index = 0;
2432 txq->txq_get_index = 0;
2433}
2434
willy tarreau6c498972014-01-16 08:20:12 +01002435/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2436 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2437 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002438static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002439{
2440 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002441 struct netdev_queue *nq;
2442
willy tarreau6c498972014-01-16 08:20:12 +01002443 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002444 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002445
2446 nq = netdev_get_tx_queue(pp->dev, txq->id);
2447 __netif_tx_lock(nq, smp_processor_id());
2448
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002449 if (txq->count)
2450 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002451
2452 __netif_tx_unlock(nq);
2453 cause_tx_done &= ~((1 << txq->id));
2454 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002455}
2456
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002457/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002458 * according to hw spec, different than generic crc8 algorithm
2459 */
2460static int mvneta_addr_crc(unsigned char *addr)
2461{
2462 int crc = 0;
2463 int i;
2464
2465 for (i = 0; i < ETH_ALEN; i++) {
2466 int j;
2467
2468 crc = (crc ^ addr[i]) << 8;
2469 for (j = 7; j >= 0; j--) {
2470 if (crc & (0x100 << j))
2471 crc ^= 0x107 << j;
2472 }
2473 }
2474
2475 return crc;
2476}
2477
2478/* This method controls the net device special MAC multicast support.
2479 * The Special Multicast Table for MAC addresses supports MAC of the form
2480 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2481 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2482 * Table entries in the DA-Filter table. This method set the Special
2483 * Multicast Table appropriate entry.
2484 */
2485static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2486 unsigned char last_byte,
2487 int queue)
2488{
2489 unsigned int smc_table_reg;
2490 unsigned int tbl_offset;
2491 unsigned int reg_offset;
2492
2493 /* Register offset from SMC table base */
2494 tbl_offset = (last_byte / 4);
2495 /* Entry offset within the above reg */
2496 reg_offset = last_byte % 4;
2497
2498 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2499 + tbl_offset * 4));
2500
2501 if (queue == -1)
2502 smc_table_reg &= ~(0xff << (8 * reg_offset));
2503 else {
2504 smc_table_reg &= ~(0xff << (8 * reg_offset));
2505 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2506 }
2507
2508 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2509 smc_table_reg);
2510}
2511
2512/* This method controls the network device Other MAC multicast support.
2513 * The Other Multicast Table is used for multicast of another type.
2514 * A CRC-8 is used as an index to the Other Multicast Table entries
2515 * in the DA-Filter table.
2516 * The method gets the CRC-8 value from the calling routine and
2517 * sets the Other Multicast Table appropriate entry according to the
2518 * specified CRC-8 .
2519 */
2520static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2521 unsigned char crc8,
2522 int queue)
2523{
2524 unsigned int omc_table_reg;
2525 unsigned int tbl_offset;
2526 unsigned int reg_offset;
2527
2528 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2529 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2530
2531 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2532
2533 if (queue == -1) {
2534 /* Clear accepts frame bit at specified Other DA table entry */
2535 omc_table_reg &= ~(0xff << (8 * reg_offset));
2536 } else {
2537 omc_table_reg &= ~(0xff << (8 * reg_offset));
2538 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2539 }
2540
2541 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2542}
2543
2544/* The network device supports multicast using two tables:
2545 * 1) Special Multicast Table for MAC addresses of the form
2546 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2547 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2548 * Table entries in the DA-Filter table.
2549 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2550 * is used as an index to the Other Multicast Table entries in the
2551 * DA-Filter table.
2552 */
2553static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2554 int queue)
2555{
2556 unsigned char crc_result = 0;
2557
2558 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2559 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2560 return 0;
2561 }
2562
2563 crc_result = mvneta_addr_crc(p_addr);
2564 if (queue == -1) {
2565 if (pp->mcast_count[crc_result] == 0) {
2566 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2567 crc_result);
2568 return -EINVAL;
2569 }
2570
2571 pp->mcast_count[crc_result]--;
2572 if (pp->mcast_count[crc_result] != 0) {
2573 netdev_info(pp->dev,
2574 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2575 pp->mcast_count[crc_result], crc_result);
2576 return -EINVAL;
2577 }
2578 } else
2579 pp->mcast_count[crc_result]++;
2580
2581 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2582
2583 return 0;
2584}
2585
2586/* Configure Fitering mode of Ethernet port */
2587static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2588 int is_promisc)
2589{
2590 u32 port_cfg_reg, val;
2591
2592 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2593
2594 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2595
2596 /* Set / Clear UPM bit in port configuration register */
2597 if (is_promisc) {
2598 /* Accept all Unicast addresses */
2599 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2600 val |= MVNETA_FORCE_UNI;
2601 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2602 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2603 } else {
2604 /* Reject all Unicast addresses */
2605 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2606 val &= ~MVNETA_FORCE_UNI;
2607 }
2608
2609 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2610 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2611}
2612
2613/* register unicast and multicast addresses */
2614static void mvneta_set_rx_mode(struct net_device *dev)
2615{
2616 struct mvneta_port *pp = netdev_priv(dev);
2617 struct netdev_hw_addr *ha;
2618
2619 if (dev->flags & IFF_PROMISC) {
2620 /* Accept all: Multicast + Unicast */
2621 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002622 mvneta_set_ucast_table(pp, pp->rxq_def);
2623 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2624 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002625 } else {
2626 /* Accept single Unicast */
2627 mvneta_rx_unicast_promisc_set(pp, 0);
2628 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002629 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002630
2631 if (dev->flags & IFF_ALLMULTI) {
2632 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002633 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2634 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002635 } else {
2636 /* Accept only initialized multicast */
2637 mvneta_set_special_mcast_table(pp, -1);
2638 mvneta_set_other_mcast_table(pp, -1);
2639
2640 if (!netdev_mc_empty(dev)) {
2641 netdev_for_each_mc_addr(ha, dev) {
2642 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002643 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002644 }
2645 }
2646 }
2647 }
2648}
2649
2650/* Interrupt handling - the callback for request_irq() */
2651static irqreturn_t mvneta_isr(int irq, void *dev_id)
2652{
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002653 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2654
2655 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2656 napi_schedule(&pp->napi);
2657
2658 return IRQ_HANDLED;
2659}
2660
2661/* Interrupt handling - the callback for request_percpu_irq() */
2662static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2663{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002664 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002665
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002666 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002667 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002668
2669 return IRQ_HANDLED;
2670}
2671
Russell King503f9aa92018-01-02 17:24:44 +00002672static void mvneta_link_change(struct mvneta_port *pp)
Stas Sergeev898b29702015-04-01 20:32:49 +03002673{
Stas Sergeev898b29702015-04-01 20:32:49 +03002674 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2675
Russell King503f9aa92018-01-02 17:24:44 +00002676 phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
Stas Sergeev898b29702015-04-01 20:32:49 +03002677}
2678
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002679/* NAPI handler
2680 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2681 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2682 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2683 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2684 * Each CPU has its own causeRxTx register
2685 */
2686static int mvneta_poll(struct napi_struct *napi, int budget)
2687{
2688 int rx_done = 0;
2689 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002690 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002691 struct mvneta_port *pp = netdev_priv(napi->dev);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002692 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002693
2694 if (!netif_running(pp->dev)) {
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002695 napi_complete(napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002696 return rx_done;
2697 }
2698
2699 /* Read cause register */
Stas Sergeev898b29702015-04-01 20:32:49 +03002700 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2701 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2702 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2703
2704 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
Russell King503f9aa92018-01-02 17:24:44 +00002705
2706 if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2707 MVNETA_CAUSE_LINK_CHANGE |
2708 MVNETA_CAUSE_PSC_SYNC_CHANGE))
2709 mvneta_link_change(pp);
Stas Sergeev898b29702015-04-01 20:32:49 +03002710 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002711
2712 /* Release Tx descriptors */
2713 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002714 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002715 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2716 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002717
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002718 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002719 * RX packets
2720 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002721 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2722
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002723 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2724 port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002725
2726 if (rx_queue) {
2727 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002728 if (pp->bm_priv)
2729 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2730 else
2731 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002732 }
2733
Eric Dumazet6ad20162017-01-30 08:22:01 -08002734 if (rx_done < budget) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002735 cause_rx_tx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002736 napi_complete_done(napi, rx_done);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002737
2738 if (pp->neta_armada3700) {
2739 unsigned long flags;
2740
2741 local_irq_save(flags);
2742 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2743 MVNETA_RX_INTR_MASK(rxq_number) |
2744 MVNETA_TX_INTR_MASK(txq_number) |
2745 MVNETA_MISCINTR_INTR_MASK);
2746 local_irq_restore(flags);
2747 } else {
2748 enable_percpu_irq(pp->dev->irq, 0);
2749 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002750 }
2751
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002752 if (pp->neta_armada3700)
2753 pp->cause_rx_tx = cause_rx_tx;
2754 else
2755 port->cause_rx_tx = cause_rx_tx;
2756
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002757 return rx_done;
2758}
2759
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002760/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2761static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2762 int num)
2763{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002764 int i;
2765
2766 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002767 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002768 if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002769 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002770 __func__, rxq->id, i, num);
2771 break;
2772 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002773 }
2774
2775 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002776 * get packets)
2777 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002778 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2779
2780 return i;
2781}
2782
2783/* Free all packets pending transmit from all TXQs and reset TX port */
2784static void mvneta_tx_reset(struct mvneta_port *pp)
2785{
2786 int queue;
2787
Ezequiel Garcia96728502014-05-22 20:06:59 -03002788 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002789 for (queue = 0; queue < txq_number; queue++)
2790 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2791
2792 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2793 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2794}
2795
2796static void mvneta_rx_reset(struct mvneta_port *pp)
2797{
2798 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2799 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2800}
2801
2802/* Rx/Tx queue initialization/cleanup methods */
2803
2804/* Create a specified RX queue */
2805static int mvneta_rxq_init(struct mvneta_port *pp,
2806 struct mvneta_rx_queue *rxq)
2807
2808{
2809 rxq->size = pp->rx_ring_size;
2810
2811 /* Allocate memory for RX descriptors */
2812 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2813 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2814 &rxq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002815 if (!rxq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002816 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002817
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002818 rxq->last_desc = rxq->size - 1;
2819
2820 /* Set Rx descriptors queue starting address */
2821 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2822 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2823
2824 /* Set Offset */
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01002825 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002826
2827 /* Set coalescing pkts and time */
2828 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2829 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2830
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002831 if (!pp->bm_priv) {
2832 /* Fill RXQ with buffers from RX pool */
2833 mvneta_rxq_buf_size_set(pp, rxq,
2834 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2835 mvneta_rxq_bm_disable(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002836 mvneta_rxq_fill(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002837 } else {
2838 mvneta_rxq_bm_enable(pp, rxq);
2839 mvneta_rxq_long_pool_set(pp, rxq);
2840 mvneta_rxq_short_pool_set(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002841 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002842 }
2843
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002844 return 0;
2845}
2846
2847/* Cleanup Rx queue */
2848static void mvneta_rxq_deinit(struct mvneta_port *pp,
2849 struct mvneta_rx_queue *rxq)
2850{
2851 mvneta_rxq_drop_pkts(pp, rxq);
2852
2853 if (rxq->descs)
2854 dma_free_coherent(pp->dev->dev.parent,
2855 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2856 rxq->descs,
2857 rxq->descs_phys);
2858
2859 rxq->descs = NULL;
2860 rxq->last_desc = 0;
2861 rxq->next_desc_to_proc = 0;
2862 rxq->descs_phys = 0;
2863}
2864
2865/* Create and initialize a tx queue */
2866static int mvneta_txq_init(struct mvneta_port *pp,
2867 struct mvneta_tx_queue *txq)
2868{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002869 int cpu;
2870
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002871 txq->size = pp->tx_ring_size;
2872
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002873 /* A queue must always have room for at least one skb.
2874 * Therefore, stop the queue when the free entries reaches
2875 * the maximum number of descriptors per skb.
2876 */
2877 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2878 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2879
2880
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002881 /* Allocate memory for TX descriptors */
2882 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2883 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2884 &txq->descs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002885 if (!txq->descs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002886 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002887
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002888 txq->last_desc = txq->size - 1;
2889
2890 /* Set maximum bandwidth for enabled TXQs */
2891 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2892 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2893
2894 /* Set Tx descriptors queue starting address */
2895 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2896 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2897
Markus Elfringd441b682017-04-16 22:11:22 +02002898 txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
2899 GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002900 if (!txq->tx_skb) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002901 dma_free_coherent(pp->dev->dev.parent,
2902 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2903 txq->descs, txq->descs_phys);
2904 return -ENOMEM;
2905 }
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002906
2907 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2908 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2909 txq->size * TSO_HEADER_SIZE,
2910 &txq->tso_hdrs_phys, GFP_KERNEL);
Markus Elfringf95936c2017-04-16 22:45:33 +02002911 if (!txq->tso_hdrs) {
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002912 kfree(txq->tx_skb);
2913 dma_free_coherent(pp->dev->dev.parent,
2914 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2915 txq->descs, txq->descs_phys);
2916 return -ENOMEM;
2917 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002918 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2919
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002920 /* Setup XPS mapping */
2921 if (txq_number > 1)
2922 cpu = txq->id % num_present_cpus();
2923 else
2924 cpu = pp->rxq_def % num_present_cpus();
2925 cpumask_set_cpu(cpu, &txq->affinity_mask);
2926 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2927
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002928 return 0;
2929}
2930
2931/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2932static void mvneta_txq_deinit(struct mvneta_port *pp,
2933 struct mvneta_tx_queue *txq)
2934{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002935 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2936
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002937 kfree(txq->tx_skb);
2938
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002939 if (txq->tso_hdrs)
2940 dma_free_coherent(pp->dev->dev.parent,
2941 txq->size * TSO_HEADER_SIZE,
2942 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002943 if (txq->descs)
2944 dma_free_coherent(pp->dev->dev.parent,
2945 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2946 txq->descs, txq->descs_phys);
2947
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002948 netdev_tx_reset_queue(nq);
2949
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002950 txq->descs = NULL;
2951 txq->last_desc = 0;
2952 txq->next_desc_to_proc = 0;
2953 txq->descs_phys = 0;
2954
2955 /* Set minimum bandwidth for disabled TXQs */
2956 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2957 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2958
2959 /* Set Tx descriptors queue starting address and size */
2960 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2961 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2962}
2963
2964/* Cleanup all Tx queues */
2965static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2966{
2967 int queue;
2968
2969 for (queue = 0; queue < txq_number; queue++)
2970 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2971}
2972
2973/* Cleanup all Rx queues */
2974static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2975{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002976 int queue;
2977
Yelena Krivosheevca5902a2017-12-19 17:59:46 +01002978 for (queue = 0; queue < rxq_number; queue++)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002979 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002980}
2981
2982
2983/* Init all Rx queues */
2984static int mvneta_setup_rxqs(struct mvneta_port *pp)
2985{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002986 int queue;
2987
2988 for (queue = 0; queue < rxq_number; queue++) {
2989 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2990
2991 if (err) {
2992 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2993 __func__, queue);
2994 mvneta_cleanup_rxqs(pp);
2995 return err;
2996 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002997 }
2998
2999 return 0;
3000}
3001
3002/* Init all tx queues */
3003static int mvneta_setup_txqs(struct mvneta_port *pp)
3004{
3005 int queue;
3006
3007 for (queue = 0; queue < txq_number; queue++) {
3008 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3009 if (err) {
3010 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3011 __func__, queue);
3012 mvneta_cleanup_txqs(pp);
3013 return err;
3014 }
3015 }
3016
3017 return 0;
3018}
3019
3020static void mvneta_start_dev(struct mvneta_port *pp)
3021{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003022 int cpu;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003023
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003024 mvneta_max_rx_size_set(pp, pp->pkt_size);
3025 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3026
3027 /* start the Rx/Tx activity */
3028 mvneta_port_enable(pp);
3029
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003030 if (!pp->neta_armada3700) {
3031 /* Enable polling on the port */
3032 for_each_online_cpu(cpu) {
3033 struct mvneta_pcpu_port *port =
3034 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003035
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003036 napi_enable(&port->napi);
3037 }
3038 } else {
3039 napi_enable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003040 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003041
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003042 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003043 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3044
Stas Sergeev898b29702015-04-01 20:32:49 +03003045 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3046 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3047 MVNETA_CAUSE_LINK_CHANGE |
3048 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003049
Russell King503f9aa92018-01-02 17:24:44 +00003050 phylink_start(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003051 netif_tx_start_all_queues(pp->dev);
3052}
3053
3054static void mvneta_stop_dev(struct mvneta_port *pp)
3055{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003056 unsigned int cpu;
3057
Russell King503f9aa92018-01-02 17:24:44 +00003058 phylink_stop(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003059
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003060 if (!pp->neta_armada3700) {
3061 for_each_online_cpu(cpu) {
3062 struct mvneta_pcpu_port *port =
3063 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003064
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003065 napi_disable(&port->napi);
3066 }
3067 } else {
3068 napi_disable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003069 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003070
3071 netif_carrier_off(pp->dev);
3072
3073 mvneta_port_down(pp);
3074 netif_tx_stop_all_queues(pp->dev);
3075
3076 /* Stop the port activity */
3077 mvneta_port_disable(pp);
3078
3079 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003080 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003081
3082 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003083 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003084
3085 mvneta_tx_reset(pp);
3086 mvneta_rx_reset(pp);
3087}
3088
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003089static void mvneta_percpu_enable(void *arg)
3090{
3091 struct mvneta_port *pp = arg;
3092
3093 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3094}
3095
3096static void mvneta_percpu_disable(void *arg)
3097{
3098 struct mvneta_port *pp = arg;
3099
3100 disable_percpu_irq(pp->dev->irq);
3101}
3102
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003103/* Change the device mtu */
3104static int mvneta_change_mtu(struct net_device *dev, int mtu)
3105{
3106 struct mvneta_port *pp = netdev_priv(dev);
3107 int ret;
3108
Jarod Wilson57779872016-10-17 15:54:06 -04003109 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3110 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3111 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3112 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3113 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003114
3115 dev->mtu = mtu;
3116
Simon Guinotb65657f2015-06-30 16:20:22 +02003117 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003118 if (pp->bm_priv)
3119 mvneta_bm_update_mtu(pp, mtu);
3120
Simon Guinotb65657f2015-06-30 16:20:22 +02003121 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003122 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003123 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003124
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003125 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003126 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003127 */
3128 mvneta_stop_dev(pp);
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003129 on_each_cpu(mvneta_percpu_disable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003130
3131 mvneta_cleanup_txqs(pp);
3132 mvneta_cleanup_rxqs(pp);
3133
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003134 if (pp->bm_priv)
3135 mvneta_bm_update_mtu(pp, mtu);
3136
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003137 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003138 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3139 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003140
3141 ret = mvneta_setup_rxqs(pp);
3142 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003143 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003144 return ret;
3145 }
3146
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003147 ret = mvneta_setup_txqs(pp);
3148 if (ret) {
3149 netdev_err(dev, "unable to setup txqs after MTU change\n");
3150 return ret;
3151 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003152
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003153 on_each_cpu(mvneta_percpu_enable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003154 mvneta_start_dev(pp);
3155 mvneta_port_up(pp);
3156
Simon Guinotb65657f2015-06-30 16:20:22 +02003157 netdev_update_features(dev);
3158
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003159 return 0;
3160}
3161
Simon Guinotb65657f2015-06-30 16:20:22 +02003162static netdev_features_t mvneta_fix_features(struct net_device *dev,
3163 netdev_features_t features)
3164{
3165 struct mvneta_port *pp = netdev_priv(dev);
3166
3167 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3168 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3169 netdev_info(dev,
3170 "Disable IP checksum for MTU greater than %dB\n",
3171 pp->tx_csum_limit);
3172 }
3173
3174 return features;
3175}
3176
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003177/* Get mac address */
3178static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3179{
3180 u32 mac_addr_l, mac_addr_h;
3181
3182 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3183 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3184 addr[0] = (mac_addr_h >> 24) & 0xFF;
3185 addr[1] = (mac_addr_h >> 16) & 0xFF;
3186 addr[2] = (mac_addr_h >> 8) & 0xFF;
3187 addr[3] = mac_addr_h & 0xFF;
3188 addr[4] = (mac_addr_l >> 8) & 0xFF;
3189 addr[5] = mac_addr_l & 0xFF;
3190}
3191
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003192/* Handle setting mac address */
3193static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3194{
3195 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003196 struct sockaddr *sockaddr = addr;
3197 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003198
Ezequiel Garciae68de362014-05-22 20:07:00 -03003199 ret = eth_prepare_mac_addr_change(dev, addr);
3200 if (ret < 0)
3201 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003202 /* Remove previous address table entry */
3203 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3204
3205 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003206 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003207
Ezequiel Garciae68de362014-05-22 20:07:00 -03003208 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003209 return 0;
3210}
3211
Russell King503f9aa92018-01-02 17:24:44 +00003212static void mvneta_validate(struct net_device *ndev, unsigned long *supported,
3213 struct phylink_link_state *state)
Russell Kingfc548b92018-01-02 17:24:39 +00003214{
Russell King503f9aa92018-01-02 17:24:44 +00003215 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
Russell Kingfc548b92018-01-02 17:24:39 +00003216
Russell King22f4bf82018-01-02 17:24:54 +00003217 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
Russell King503f9aa92018-01-02 17:24:44 +00003218 if (state->interface != PHY_INTERFACE_MODE_NA &&
3219 state->interface != PHY_INTERFACE_MODE_QSGMII &&
3220 state->interface != PHY_INTERFACE_MODE_SGMII &&
Russell King22f4bf82018-01-02 17:24:54 +00003221 !phy_interface_mode_is_8023z(state->interface) &&
Russell King503f9aa92018-01-02 17:24:44 +00003222 !phy_interface_mode_is_rgmii(state->interface)) {
3223 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3224 return;
Russell Kingfc548b92018-01-02 17:24:39 +00003225 }
Russell King503f9aa92018-01-02 17:24:44 +00003226
3227 /* Allow all the expected bits */
3228 phylink_set(mask, Autoneg);
3229 phylink_set_port_modes(mask);
3230
Russell King4932a912018-01-02 17:24:59 +00003231 /* Asymmetric pause is unsupported */
3232 phylink_set(mask, Pause);
Russell King503f9aa92018-01-02 17:24:44 +00003233 /* Half-duplex at speeds higher than 100Mbit is unsupported */
3234 phylink_set(mask, 1000baseT_Full);
3235 phylink_set(mask, 1000baseX_Full);
Russell King22f4bf82018-01-02 17:24:54 +00003236
3237 if (!phy_interface_mode_is_8023z(state->interface)) {
3238 /* 10M and 100M are only supported in non-802.3z mode */
3239 phylink_set(mask, 10baseT_Half);
3240 phylink_set(mask, 10baseT_Full);
3241 phylink_set(mask, 100baseT_Half);
3242 phylink_set(mask, 100baseT_Full);
3243 }
Russell King503f9aa92018-01-02 17:24:44 +00003244
3245 bitmap_and(supported, supported, mask,
3246 __ETHTOOL_LINK_MODE_MASK_NBITS);
3247 bitmap_and(state->advertising, state->advertising, mask,
3248 __ETHTOOL_LINK_MODE_MASK_NBITS);
Russell Kingfc548b92018-01-02 17:24:39 +00003249}
3250
Russell King503f9aa92018-01-02 17:24:44 +00003251static int mvneta_mac_link_state(struct net_device *ndev,
3252 struct phylink_link_state *state)
3253{
3254 struct mvneta_port *pp = netdev_priv(ndev);
3255 u32 gmac_stat;
3256
3257 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3258
3259 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3260 state->speed = SPEED_1000;
3261 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3262 state->speed = SPEED_100;
3263 else
3264 state->speed = SPEED_10;
3265
3266 state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3267 state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3268 state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3269
3270 state->pause = 0;
Russell King4932a912018-01-02 17:24:59 +00003271 if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
3272 state->pause |= MLO_PAUSE_RX;
3273 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
3274 state->pause |= MLO_PAUSE_TX;
Russell King503f9aa92018-01-02 17:24:44 +00003275
3276 return 1;
3277}
3278
Russell King22f4bf82018-01-02 17:24:54 +00003279static void mvneta_mac_an_restart(struct net_device *ndev)
3280{
3281 struct mvneta_port *pp = netdev_priv(ndev);
3282 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3283
3284 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3285 gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
3286 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3287 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
3288}
3289
Russell King503f9aa92018-01-02 17:24:44 +00003290static void mvneta_mac_config(struct net_device *ndev, unsigned int mode,
3291 const struct phylink_link_state *state)
3292{
3293 struct mvneta_port *pp = netdev_priv(ndev);
Russell King22f4bf82018-01-02 17:24:54 +00003294 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
Russell King503f9aa92018-01-02 17:24:44 +00003295 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3296 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3297 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3298
Russell King22f4bf82018-01-02 17:24:54 +00003299 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
Russell King32699952018-01-02 17:24:49 +00003300 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
3301 MVNETA_GMAC2_PORT_RESET);
Russell King503f9aa92018-01-02 17:24:44 +00003302 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3303 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3304 MVNETA_GMAC_INBAND_RESTART_AN |
3305 MVNETA_GMAC_CONFIG_MII_SPEED |
3306 MVNETA_GMAC_CONFIG_GMII_SPEED |
3307 MVNETA_GMAC_AN_SPEED_EN |
Russell King22f4bf82018-01-02 17:24:54 +00003308 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
3309 MVNETA_GMAC_CONFIG_FLOW_CTRL |
Russell King503f9aa92018-01-02 17:24:44 +00003310 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3311 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
3312 MVNETA_GMAC_AN_DUPLEX_EN);
3313
Russell King32699952018-01-02 17:24:49 +00003314 /* Even though it might look weird, when we're configured in
3315 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3316 */
3317 new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
3318
3319 if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
Russell King22f4bf82018-01-02 17:24:54 +00003320 state->interface == PHY_INTERFACE_MODE_SGMII ||
3321 phy_interface_mode_is_8023z(state->interface))
Russell King32699952018-01-02 17:24:49 +00003322 new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
3323
Russell King4932a912018-01-02 17:24:59 +00003324 if (phylink_test(state->advertising, Pause))
3325 new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3326 if (state->pause & MLO_PAUSE_TXRX_MASK)
3327 new_an |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
3328
Russell King503f9aa92018-01-02 17:24:44 +00003329 if (!phylink_autoneg_inband(mode)) {
3330 /* Phy or fixed speed */
3331 if (state->duplex)
3332 new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3333
3334 if (state->speed == SPEED_1000)
3335 new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3336 else if (state->speed == SPEED_100)
3337 new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
Russell King22f4bf82018-01-02 17:24:54 +00003338 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
Russell King503f9aa92018-01-02 17:24:44 +00003339 /* SGMII mode receives the state from the PHY */
3340 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3341 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3342 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3343 MVNETA_GMAC_FORCE_LINK_PASS)) |
3344 MVNETA_GMAC_INBAND_AN_ENABLE |
3345 MVNETA_GMAC_AN_SPEED_EN |
3346 MVNETA_GMAC_AN_DUPLEX_EN;
Russell King22f4bf82018-01-02 17:24:54 +00003347 } else {
3348 /* 802.3z negotiation - only 1000base-X */
3349 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
3350 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3351 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3352 MVNETA_GMAC_FORCE_LINK_PASS)) |
3353 MVNETA_GMAC_INBAND_AN_ENABLE |
3354 MVNETA_GMAC_CONFIG_GMII_SPEED |
3355 /* The MAC only supports FD mode */
3356 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
Russell King4932a912018-01-02 17:24:59 +00003357
3358 if (state->pause & MLO_PAUSE_AN && state->an_enabled)
3359 new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
Russell King503f9aa92018-01-02 17:24:44 +00003360 }
3361
3362 /* Armada 370 documentation says we can only change the port mode
3363 * and in-band enable when the link is down, so force it down
3364 * while making these changes. We also do this for GMAC_CTRL2 */
Russell King22f4bf82018-01-02 17:24:54 +00003365 if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
3366 (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
Russell King503f9aa92018-01-02 17:24:44 +00003367 (new_an ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3368 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3369 (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3370 MVNETA_GMAC_FORCE_LINK_DOWN);
3371 }
3372
Russell King22f4bf82018-01-02 17:24:54 +00003373 if (new_ctrl0 != gmac_ctrl0)
3374 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
Russell King503f9aa92018-01-02 17:24:44 +00003375 if (new_ctrl2 != gmac_ctrl2)
3376 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
3377 if (new_clk != gmac_clk)
3378 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3379 if (new_an != gmac_an)
3380 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
Russell King32699952018-01-02 17:24:49 +00003381
3382 if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
3383 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3384 MVNETA_GMAC2_PORT_RESET) != 0)
3385 continue;
3386 }
Russell King503f9aa92018-01-02 17:24:44 +00003387}
3388
Russell King6d81f452018-01-02 17:25:04 +00003389static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
3390{
3391 u32 lpi_ctl1;
3392
3393 lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
3394 if (enable)
3395 lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
3396 else
3397 lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
3398 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
3399}
3400
Russell King503f9aa92018-01-02 17:24:44 +00003401static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode)
Russell Kingfc548b92018-01-02 17:24:39 +00003402{
3403 struct mvneta_port *pp = netdev_priv(ndev);
3404 u32 val;
3405
Russell King503f9aa92018-01-02 17:24:44 +00003406 mvneta_port_down(pp);
3407
3408 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003409 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3410 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3411 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3412 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3413 }
Russell King6d81f452018-01-02 17:25:04 +00003414
3415 pp->eee_active = false;
3416 mvneta_set_eee(pp, false);
Russell Kingfc548b92018-01-02 17:24:39 +00003417}
3418
Russell King503f9aa92018-01-02 17:24:44 +00003419static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode,
3420 struct phy_device *phy)
Russell Kingfc548b92018-01-02 17:24:39 +00003421{
3422 struct mvneta_port *pp = netdev_priv(ndev);
3423 u32 val;
3424
Russell King503f9aa92018-01-02 17:24:44 +00003425 if (!phylink_autoneg_inband(mode)) {
Russell Kingfc548b92018-01-02 17:24:39 +00003426 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3427 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3428 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3429 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3430 }
3431
3432 mvneta_port_up(pp);
Russell King6d81f452018-01-02 17:25:04 +00003433
3434 if (phy && pp->eee_enabled) {
3435 pp->eee_active = phy_init_eee(phy, 0) >= 0;
3436 mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
3437 }
Russell Kingfc548b92018-01-02 17:24:39 +00003438}
3439
Russell King503f9aa92018-01-02 17:24:44 +00003440static const struct phylink_mac_ops mvneta_phylink_ops = {
3441 .validate = mvneta_validate,
3442 .mac_link_state = mvneta_mac_link_state,
Russell King22f4bf82018-01-02 17:24:54 +00003443 .mac_an_restart = mvneta_mac_an_restart,
Russell King503f9aa92018-01-02 17:24:44 +00003444 .mac_config = mvneta_mac_config,
3445 .mac_link_down = mvneta_mac_link_down,
3446 .mac_link_up = mvneta_mac_link_up,
3447};
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003448
3449static int mvneta_mdio_probe(struct mvneta_port *pp)
3450{
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003451 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
Russell King503f9aa92018-01-02 17:24:44 +00003452 int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003453
Russell King503f9aa92018-01-02 17:24:44 +00003454 if (err)
3455 netdev_err(pp->dev, "could not attach PHY: %d\n", err);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003456
Russell King503f9aa92018-01-02 17:24:44 +00003457 phylink_ethtool_get_wol(pp->phylink, &wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08003458 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
3459
Russell King503f9aa92018-01-02 17:24:44 +00003460 return err;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003461}
3462
3463static void mvneta_mdio_remove(struct mvneta_port *pp)
3464{
Russell King503f9aa92018-01-02 17:24:44 +00003465 phylink_disconnect_phy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003466}
3467
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003468/* Electing a CPU must be done in an atomic way: it should be done
3469 * after or before the removal/insertion of a CPU and this function is
3470 * not reentrant.
3471 */
Maxime Ripardf8642882015-09-25 18:09:38 +02003472static void mvneta_percpu_elect(struct mvneta_port *pp)
3473{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003474 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003475
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003476 /* Use the cpu associated to the rxq when it is online, in all
3477 * the other cases, use the cpu 0 which can't be offline.
3478 */
3479 if (cpu_online(pp->rxq_def))
3480 elected_cpu = pp->rxq_def;
3481
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003482 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02003483
3484 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003485 int rxq_map = 0, txq_map = 0;
3486 int rxq;
3487
3488 for (rxq = 0; rxq < rxq_number; rxq++)
3489 if ((rxq % max_cpu) == cpu)
3490 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3491
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003492 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003493 /* Map the default receive queue queue to the
3494 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02003495 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003496 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003497
3498 /* We update the TX queue map only if we have one
3499 * queue. In this case we associate the TX queue to
3500 * the CPU bound to the default RX queue
3501 */
3502 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003503 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003504 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3505 else
3506 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3507 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3508
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003509 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3510
3511 /* Update the interrupt mask on each CPU according the
3512 * new mapping
3513 */
3514 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3515 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003516 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003517
Maxime Ripardf8642882015-09-25 18:09:38 +02003518 }
3519};
3520
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003521static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
Maxime Ripardf8642882015-09-25 18:09:38 +02003522{
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003523 int other_cpu;
3524 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3525 node_online);
Maxime Ripardf8642882015-09-25 18:09:38 +02003526 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3527
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003528
3529 spin_lock(&pp->lock);
3530 /*
3531 * Configuring the driver for a new CPU while the driver is
3532 * stopping is racy, so just avoid it.
3533 */
3534 if (pp->is_stopped) {
3535 spin_unlock(&pp->lock);
3536 return 0;
3537 }
3538 netif_tx_stop_all_queues(pp->dev);
3539
3540 /*
3541 * We have to synchronise on tha napi of each CPU except the one
3542 * just being woken up
3543 */
3544 for_each_online_cpu(other_cpu) {
3545 if (other_cpu != cpu) {
3546 struct mvneta_pcpu_port *other_port =
3547 per_cpu_ptr(pp->ports, other_cpu);
3548
3549 napi_synchronize(&other_port->napi);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003550 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003551 }
3552
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003553 /* Mask all ethernet port interrupts */
3554 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3555 napi_enable(&port->napi);
3556
3557 /*
3558 * Enable per-CPU interrupts on the CPU that is
3559 * brought up.
3560 */
3561 mvneta_percpu_enable(pp);
3562
3563 /*
3564 * Enable per-CPU interrupt on the one CPU we care
3565 * about.
3566 */
3567 mvneta_percpu_elect(pp);
3568
3569 /* Unmask all ethernet port interrupts */
3570 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3571 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3572 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3573 MVNETA_CAUSE_LINK_CHANGE |
3574 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3575 netif_tx_start_all_queues(pp->dev);
3576 spin_unlock(&pp->lock);
3577 return 0;
3578}
3579
3580static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3581{
3582 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3583 node_online);
3584 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3585
3586 /*
3587 * Thanks to this lock we are sure that any pending cpu election is
3588 * done.
3589 */
3590 spin_lock(&pp->lock);
3591 /* Mask all ethernet port interrupts */
3592 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3593 spin_unlock(&pp->lock);
3594
3595 napi_synchronize(&port->napi);
3596 napi_disable(&port->napi);
3597 /* Disable per-CPU interrupts on the CPU that is brought down. */
3598 mvneta_percpu_disable(pp);
3599 return 0;
3600}
3601
3602static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3603{
3604 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3605 node_dead);
3606
3607 /* Check if a new CPU must be elected now this on is down */
3608 spin_lock(&pp->lock);
3609 mvneta_percpu_elect(pp);
3610 spin_unlock(&pp->lock);
3611 /* Unmask all ethernet port interrupts */
3612 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3613 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3614 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3615 MVNETA_CAUSE_LINK_CHANGE |
3616 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3617 netif_tx_start_all_queues(pp->dev);
3618 return 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003619}
3620
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003621static int mvneta_open(struct net_device *dev)
3622{
3623 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003624 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003625
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003626 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003627 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3628 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003629
3630 ret = mvneta_setup_rxqs(pp);
3631 if (ret)
3632 return ret;
3633
3634 ret = mvneta_setup_txqs(pp);
3635 if (ret)
3636 goto err_cleanup_rxqs;
3637
3638 /* Connect to port interrupt line */
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003639 if (pp->neta_armada3700)
3640 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3641 dev->name, pp);
3642 else
3643 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3644 dev->name, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003645 if (ret) {
3646 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3647 goto err_cleanup_txqs;
3648 }
3649
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003650 if (!pp->neta_armada3700) {
3651 /* Enable per-CPU interrupt on all the CPU to handle our RX
3652 * queue interrupts
3653 */
3654 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003655
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003656 pp->is_stopped = false;
3657 /* Register a CPU notifier to handle the case where our CPU
3658 * might be taken offline.
3659 */
3660 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3661 &pp->node_online);
3662 if (ret)
3663 goto err_free_irq;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003664
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003665 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3666 &pp->node_dead);
3667 if (ret)
3668 goto err_free_online_hp;
3669 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003670
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003671 /* In default link is down */
3672 netif_carrier_off(pp->dev);
3673
3674 ret = mvneta_mdio_probe(pp);
3675 if (ret < 0) {
3676 netdev_err(dev, "cannot probe MDIO bus\n");
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003677 goto err_free_dead_hp;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003678 }
3679
3680 mvneta_start_dev(pp);
3681
3682 return 0;
3683
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003684err_free_dead_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003685 if (!pp->neta_armada3700)
3686 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3687 &pp->node_dead);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003688err_free_online_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003689 if (!pp->neta_armada3700)
3690 cpuhp_state_remove_instance_nocalls(online_hpstate,
3691 &pp->node_online);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003692err_free_irq:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003693 if (pp->neta_armada3700) {
3694 free_irq(pp->dev->irq, pp);
3695 } else {
3696 on_each_cpu(mvneta_percpu_disable, pp, true);
3697 free_percpu_irq(pp->dev->irq, pp->ports);
3698 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003699err_cleanup_txqs:
3700 mvneta_cleanup_txqs(pp);
3701err_cleanup_rxqs:
3702 mvneta_cleanup_rxqs(pp);
3703 return ret;
3704}
3705
3706/* Stop the port, free port interrupt line */
3707static int mvneta_stop(struct net_device *dev)
3708{
3709 struct mvneta_port *pp = netdev_priv(dev);
3710
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003711 if (!pp->neta_armada3700) {
3712 /* Inform that we are stopping so we don't want to setup the
3713 * driver for new CPUs in the notifiers. The code of the
3714 * notifier for CPU online is protected by the same spinlock,
3715 * so when we get the lock, the notifer work is done.
3716 */
3717 spin_lock(&pp->lock);
3718 pp->is_stopped = true;
3719 spin_unlock(&pp->lock);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003720
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003721 mvneta_stop_dev(pp);
3722 mvneta_mdio_remove(pp);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003723
Dan Carpenterd26aac22016-12-07 14:32:17 +03003724 cpuhp_state_remove_instance_nocalls(online_hpstate,
3725 &pp->node_online);
3726 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3727 &pp->node_dead);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003728 on_each_cpu(mvneta_percpu_disable, pp, true);
3729 free_percpu_irq(dev->irq, pp->ports);
3730 } else {
3731 mvneta_stop_dev(pp);
3732 mvneta_mdio_remove(pp);
3733 free_irq(dev->irq, pp);
3734 }
3735
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003736 mvneta_cleanup_rxqs(pp);
3737 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003738
3739 return 0;
3740}
3741
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003742static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3743{
Russell King503f9aa92018-01-02 17:24:44 +00003744 struct mvneta_port *pp = netdev_priv(dev);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003745
Russell King503f9aa92018-01-02 17:24:44 +00003746 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003747}
3748
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003749/* Ethtool methods */
3750
Philippe Reynes013ad402016-07-30 17:42:12 +02003751/* Set link ksettings (phy address, speed) for ethtools */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +08003752static int
3753mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3754 const struct ethtool_link_ksettings *cmd)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003755{
Philippe Reynes013ad402016-07-30 17:42:12 +02003756 struct mvneta_port *pp = netdev_priv(ndev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003757
Russell King503f9aa92018-01-02 17:24:44 +00003758 return phylink_ethtool_ksettings_set(pp->phylink, cmd);
3759}
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003760
Russell King503f9aa92018-01-02 17:24:44 +00003761/* Get link ksettings for ethtools */
3762static int
3763mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
3764 struct ethtool_link_ksettings *cmd)
3765{
3766 struct mvneta_port *pp = netdev_priv(ndev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003767
Russell King503f9aa92018-01-02 17:24:44 +00003768 return phylink_ethtool_ksettings_get(pp->phylink, cmd);
3769}
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003770
Russell King503f9aa92018-01-02 17:24:44 +00003771static int mvneta_ethtool_nway_reset(struct net_device *dev)
3772{
3773 struct mvneta_port *pp = netdev_priv(dev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003774
Russell King503f9aa92018-01-02 17:24:44 +00003775 return phylink_ethtool_nway_reset(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003776}
3777
3778/* Set interrupt coalescing for ethtools */
3779static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3780 struct ethtool_coalesce *c)
3781{
3782 struct mvneta_port *pp = netdev_priv(dev);
3783 int queue;
3784
3785 for (queue = 0; queue < rxq_number; queue++) {
3786 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3787 rxq->time_coal = c->rx_coalesce_usecs;
3788 rxq->pkts_coal = c->rx_max_coalesced_frames;
3789 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3790 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3791 }
3792
3793 for (queue = 0; queue < txq_number; queue++) {
3794 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3795 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3796 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3797 }
3798
3799 return 0;
3800}
3801
3802/* get coalescing for ethtools */
3803static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3804 struct ethtool_coalesce *c)
3805{
3806 struct mvneta_port *pp = netdev_priv(dev);
3807
3808 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3809 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3810
3811 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3812 return 0;
3813}
3814
3815
3816static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3817 struct ethtool_drvinfo *drvinfo)
3818{
3819 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3820 sizeof(drvinfo->driver));
3821 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3822 sizeof(drvinfo->version));
3823 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3824 sizeof(drvinfo->bus_info));
3825}
3826
3827
3828static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3829 struct ethtool_ringparam *ring)
3830{
3831 struct mvneta_port *pp = netdev_priv(netdev);
3832
3833 ring->rx_max_pending = MVNETA_MAX_RXD;
3834 ring->tx_max_pending = MVNETA_MAX_TXD;
3835 ring->rx_pending = pp->rx_ring_size;
3836 ring->tx_pending = pp->tx_ring_size;
3837}
3838
3839static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3840 struct ethtool_ringparam *ring)
3841{
3842 struct mvneta_port *pp = netdev_priv(dev);
3843
3844 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3845 return -EINVAL;
3846 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3847 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003848
3849 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3850 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3851 if (pp->tx_ring_size != ring->tx_pending)
3852 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3853 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003854
3855 if (netif_running(dev)) {
3856 mvneta_stop(dev);
3857 if (mvneta_open(dev)) {
3858 netdev_err(dev,
3859 "error on opening device after ring param change\n");
3860 return -ENOMEM;
3861 }
3862 }
3863
3864 return 0;
3865}
3866
Russell King4932a912018-01-02 17:24:59 +00003867static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
3868 struct ethtool_pauseparam *pause)
3869{
3870 struct mvneta_port *pp = netdev_priv(dev);
3871
3872 phylink_ethtool_get_pauseparam(pp->phylink, pause);
3873}
3874
3875static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
3876 struct ethtool_pauseparam *pause)
3877{
3878 struct mvneta_port *pp = netdev_priv(dev);
3879
3880 return phylink_ethtool_set_pauseparam(pp->phylink, pause);
3881}
3882
Russell King9b0cdef2015-10-22 18:37:30 +01003883static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3884 u8 *data)
3885{
3886 if (sset == ETH_SS_STATS) {
3887 int i;
3888
3889 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3890 memcpy(data + i * ETH_GSTRING_LEN,
3891 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3892 }
3893}
3894
3895static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3896{
3897 const struct mvneta_statistic *s;
3898 void __iomem *base = pp->base;
Russell King6d81f452018-01-02 17:25:04 +00003899 u32 high, low;
3900 u64 val;
Russell King9b0cdef2015-10-22 18:37:30 +01003901 int i;
3902
3903 for (i = 0, s = mvneta_statistics;
3904 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3905 s++, i++) {
Russell King6d81f452018-01-02 17:25:04 +00003906 val = 0;
3907
Russell King9b0cdef2015-10-22 18:37:30 +01003908 switch (s->type) {
3909 case T_REG_32:
3910 val = readl_relaxed(base + s->offset);
3911 break;
3912 case T_REG_64:
3913 /* Docs say to read low 32-bit then high */
3914 low = readl_relaxed(base + s->offset);
3915 high = readl_relaxed(base + s->offset + 4);
Russell King6d81f452018-01-02 17:25:04 +00003916 val = (u64)high << 32 | low;
3917 break;
3918 case T_SW:
3919 switch (s->offset) {
3920 case ETHTOOL_STAT_EEE_WAKEUP:
3921 val = phylink_get_eee_err(pp->phylink);
3922 break;
3923 }
Russell King9b0cdef2015-10-22 18:37:30 +01003924 break;
3925 }
Russell King6d81f452018-01-02 17:25:04 +00003926
3927 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003928 }
3929}
3930
3931static void mvneta_ethtool_get_stats(struct net_device *dev,
3932 struct ethtool_stats *stats, u64 *data)
3933{
3934 struct mvneta_port *pp = netdev_priv(dev);
3935 int i;
3936
3937 mvneta_ethtool_update_stats(pp);
3938
3939 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3940 *data++ = pp->ethtool_stats[i];
3941}
3942
3943static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3944{
3945 if (sset == ETH_SS_STATS)
3946 return ARRAY_SIZE(mvneta_statistics);
3947 return -EOPNOTSUPP;
3948}
3949
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003950static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3951{
3952 return MVNETA_RSS_LU_TABLE_SIZE;
3953}
3954
3955static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3956 struct ethtool_rxnfc *info,
3957 u32 *rules __always_unused)
3958{
3959 switch (info->cmd) {
3960 case ETHTOOL_GRXRINGS:
3961 info->data = rxq_number;
3962 return 0;
3963 case ETHTOOL_GRXFH:
3964 return -EOPNOTSUPP;
3965 default:
3966 return -EOPNOTSUPP;
3967 }
3968}
3969
3970static int mvneta_config_rss(struct mvneta_port *pp)
3971{
3972 int cpu;
3973 u32 val;
3974
3975 netif_tx_stop_all_queues(pp->dev);
3976
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003977 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003978
3979 /* We have to synchronise on the napi of each CPU */
3980 for_each_online_cpu(cpu) {
3981 struct mvneta_pcpu_port *pcpu_port =
3982 per_cpu_ptr(pp->ports, cpu);
3983
3984 napi_synchronize(&pcpu_port->napi);
3985 napi_disable(&pcpu_port->napi);
3986 }
3987
3988 pp->rxq_def = pp->indir[0];
3989
3990 /* Update unicast mapping */
3991 mvneta_set_rx_mode(pp->dev);
3992
3993 /* Update val of portCfg register accordingly with all RxQueue types */
3994 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3995 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3996
3997 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003998 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003999 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01004000 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004001
4002 /* We have to synchronise on the napi of each CPU */
4003 for_each_online_cpu(cpu) {
4004 struct mvneta_pcpu_port *pcpu_port =
4005 per_cpu_ptr(pp->ports, cpu);
4006
4007 napi_enable(&pcpu_port->napi);
4008 }
4009
4010 netif_tx_start_all_queues(pp->dev);
4011
4012 return 0;
4013}
4014
4015static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
4016 const u8 *key, const u8 hfunc)
4017{
4018 struct mvneta_port *pp = netdev_priv(dev);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004019
4020 /* Current code for Armada 3700 doesn't support RSS features yet */
4021 if (pp->neta_armada3700)
4022 return -EOPNOTSUPP;
4023
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004024 /* We require at least one supported parameter to be changed
4025 * and no change in any of the unsupported parameters
4026 */
4027 if (key ||
4028 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
4029 return -EOPNOTSUPP;
4030
4031 if (!indir)
4032 return 0;
4033
4034 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
4035
4036 return mvneta_config_rss(pp);
4037}
4038
4039static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
4040 u8 *hfunc)
4041{
4042 struct mvneta_port *pp = netdev_priv(dev);
4043
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004044 /* Current code for Armada 3700 doesn't support RSS features yet */
4045 if (pp->neta_armada3700)
4046 return -EOPNOTSUPP;
4047
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004048 if (hfunc)
4049 *hfunc = ETH_RSS_HASH_TOP;
4050
4051 if (!indir)
4052 return 0;
4053
4054 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
4055
4056 return 0;
4057}
4058
Jingju Houb60a00f2017-02-06 14:58:13 +08004059static void mvneta_ethtool_get_wol(struct net_device *dev,
4060 struct ethtool_wolinfo *wol)
4061{
Russell King503f9aa92018-01-02 17:24:44 +00004062 struct mvneta_port *pp = netdev_priv(dev);
Jingju Houb60a00f2017-02-06 14:58:13 +08004063
Russell King503f9aa92018-01-02 17:24:44 +00004064 phylink_ethtool_get_wol(pp->phylink, wol);
Jingju Houb60a00f2017-02-06 14:58:13 +08004065}
4066
4067static int mvneta_ethtool_set_wol(struct net_device *dev,
4068 struct ethtool_wolinfo *wol)
4069{
Russell King503f9aa92018-01-02 17:24:44 +00004070 struct mvneta_port *pp = netdev_priv(dev);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08004071 int ret;
4072
Russell King503f9aa92018-01-02 17:24:44 +00004073 ret = phylink_ethtool_set_wol(pp->phylink, wol);
Jisheng Zhang82960ff2017-04-14 19:07:32 +08004074 if (!ret)
4075 device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
4076
4077 return ret;
Jingju Houb60a00f2017-02-06 14:58:13 +08004078}
4079
Russell King6d81f452018-01-02 17:25:04 +00004080static int mvneta_ethtool_get_eee(struct net_device *dev,
4081 struct ethtool_eee *eee)
4082{
4083 struct mvneta_port *pp = netdev_priv(dev);
4084 u32 lpi_ctl0;
4085
4086 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4087
4088 eee->eee_enabled = pp->eee_enabled;
4089 eee->eee_active = pp->eee_active;
4090 eee->tx_lpi_enabled = pp->tx_lpi_enabled;
4091 eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale;
4092
4093 return phylink_ethtool_get_eee(pp->phylink, eee);
4094}
4095
4096static int mvneta_ethtool_set_eee(struct net_device *dev,
4097 struct ethtool_eee *eee)
4098{
4099 struct mvneta_port *pp = netdev_priv(dev);
4100 u32 lpi_ctl0;
4101
4102 /* The Armada 37x documents do not give limits for this other than
4103 * it being an 8-bit register. */
4104 if (eee->tx_lpi_enabled &&
4105 (eee->tx_lpi_timer < 0 || eee->tx_lpi_timer > 255))
4106 return -EINVAL;
4107
4108 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4109 lpi_ctl0 &= ~(0xff << 8);
4110 lpi_ctl0 |= eee->tx_lpi_timer << 8;
4111 mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0);
4112
4113 pp->eee_enabled = eee->eee_enabled;
4114 pp->tx_lpi_enabled = eee->tx_lpi_enabled;
4115
4116 mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled);
4117
4118 return phylink_ethtool_set_eee(pp->phylink, eee);
4119}
4120
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004121static const struct net_device_ops mvneta_netdev_ops = {
4122 .ndo_open = mvneta_open,
4123 .ndo_stop = mvneta_stop,
4124 .ndo_start_xmit = mvneta_tx,
4125 .ndo_set_rx_mode = mvneta_set_rx_mode,
4126 .ndo_set_mac_address = mvneta_set_mac_addr,
4127 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02004128 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004129 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02004130 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004131};
4132
Jisheng Zhang4581be42017-02-16 17:07:39 +08004133static const struct ethtool_ops mvneta_eth_tool_ops = {
Russell King503f9aa92018-01-02 17:24:44 +00004134 .nway_reset = mvneta_ethtool_nway_reset,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004135 .get_link = ethtool_op_get_link,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004136 .set_coalesce = mvneta_ethtool_set_coalesce,
4137 .get_coalesce = mvneta_ethtool_get_coalesce,
4138 .get_drvinfo = mvneta_ethtool_get_drvinfo,
4139 .get_ringparam = mvneta_ethtool_get_ringparam,
4140 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King4932a912018-01-02 17:24:59 +00004141 .get_pauseparam = mvneta_ethtool_get_pauseparam,
4142 .set_pauseparam = mvneta_ethtool_set_pauseparam,
Russell King9b0cdef2015-10-22 18:37:30 +01004143 .get_strings = mvneta_ethtool_get_strings,
4144 .get_ethtool_stats = mvneta_ethtool_get_stats,
4145 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004146 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
4147 .get_rxnfc = mvneta_ethtool_get_rxnfc,
4148 .get_rxfh = mvneta_ethtool_get_rxfh,
4149 .set_rxfh = mvneta_ethtool_set_rxfh,
Russell King503f9aa92018-01-02 17:24:44 +00004150 .get_link_ksettings = mvneta_ethtool_get_link_ksettings,
Philippe Reynes013ad402016-07-30 17:42:12 +02004151 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
Jingju Houb60a00f2017-02-06 14:58:13 +08004152 .get_wol = mvneta_ethtool_get_wol,
4153 .set_wol = mvneta_ethtool_set_wol,
Russell King6d81f452018-01-02 17:25:04 +00004154 .get_eee = mvneta_ethtool_get_eee,
4155 .set_eee = mvneta_ethtool_set_eee,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004156};
4157
4158/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03004159static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004160{
4161 int queue;
4162
4163 /* Disable port */
4164 mvneta_port_disable(pp);
4165
4166 /* Set port default values */
4167 mvneta_defaults_set(pp);
4168
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004169 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004170 if (!pp->txqs)
4171 return -ENOMEM;
4172
4173 /* Initialize TX descriptor rings */
4174 for (queue = 0; queue < txq_number; queue++) {
4175 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4176 txq->id = queue;
4177 txq->size = pp->tx_ring_size;
4178 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4179 }
4180
Markus Elfring5d6312ed2017-04-16 21:45:38 +02004181 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
Ezequiel Garcia96728502014-05-22 20:06:59 -03004182 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004183 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004184
4185 /* Create Rx descriptor rings */
4186 for (queue = 0; queue < rxq_number; queue++) {
4187 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4188 rxq->id = queue;
4189 rxq->size = pp->rx_ring_size;
4190 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4191 rxq->time_coal = MVNETA_RX_COAL_USEC;
Markus Elfring29110632017-04-16 21:23:19 +02004192 rxq->buf_virt_addr
4193 = devm_kmalloc_array(pp->dev->dev.parent,
4194 rxq->size,
4195 sizeof(*rxq->buf_virt_addr),
4196 GFP_KERNEL);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01004197 if (!rxq->buf_virt_addr)
4198 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004199 }
4200
4201 return 0;
4202}
4203
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004204/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00004205static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4206 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004207{
4208 u32 win_enable;
4209 u32 win_protect;
4210 int i;
4211
4212 for (i = 0; i < 6; i++) {
4213 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4214 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4215
4216 if (i < 4)
4217 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4218 }
4219
4220 win_enable = 0x3f;
4221 win_protect = 0;
4222
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004223 if (dram) {
4224 for (i = 0; i < dram->num_cs; i++) {
4225 const struct mbus_dram_window *cs = dram->cs + i;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004226
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004227 mvreg_write(pp, MVNETA_WIN_BASE(i),
4228 (cs->base & 0xffff0000) |
4229 (cs->mbus_attr << 8) |
4230 dram->mbus_dram_target_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004231
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004232 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4233 (cs->size - 1) & 0xffff0000);
4234
4235 win_enable &= ~(1 << i);
4236 win_protect |= 3 << (2 * i);
4237 }
4238 } else {
4239 /* For Armada3700 open default 4GB Mbus window, leaving
4240 * arbitration of target/attribute to a different layer
4241 * of configuration.
4242 */
4243 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4244 win_enable &= ~BIT(0);
4245 win_protect = 3;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004246 }
4247
4248 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01004249 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004250}
4251
4252/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004253static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004254{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004255 /* MAC Cause register should be cleared */
4256 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4257
Russell King32699952018-01-02 17:24:49 +00004258 if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004259 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
Russell King22f4bf82018-01-02 17:24:54 +00004260 else if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
4261 phy_mode == PHY_INTERFACE_MODE_1000BASEX)
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004262 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
Russell King32699952018-01-02 17:24:49 +00004263 else if (!phy_interface_mode_is_rgmii(phy_mode))
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004264 return -EINVAL;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004265
4266 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004267}
4268
4269/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00004270static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004271{
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004272 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004273 struct device_node *dn = pdev->dev.of_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004274 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004275 struct mvneta_port *pp;
4276 struct net_device *dev;
Russell King503f9aa92018-01-02 17:24:44 +00004277 struct phylink *phylink;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004278 const char *dt_mac_addr;
4279 char hw_mac_addr[ETH_ALEN];
4280 const char *mac_from;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004281 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004282 int phy_mode;
4283 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004284 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004285
Willy Tarreauee40a112013-04-11 23:00:37 +02004286 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004287 if (!dev)
4288 return -ENOMEM;
4289
4290 dev->irq = irq_of_parse_and_map(dn, 0);
4291 if (dev->irq == 0) {
4292 err = -EINVAL;
4293 goto err_free_netdev;
4294 }
4295
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004296 phy_mode = of_get_phy_mode(dn);
4297 if (phy_mode < 0) {
4298 dev_err(&pdev->dev, "incorrect phy-mode\n");
4299 err = -EINVAL;
Russell King503f9aa92018-01-02 17:24:44 +00004300 goto err_free_irq;
4301 }
4302
4303 phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode,
4304 &mvneta_phylink_ops);
4305 if (IS_ERR(phylink)) {
4306 err = PTR_ERR(phylink);
4307 goto err_free_irq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004308 }
4309
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004310 dev->tx_queue_len = MVNETA_MAX_TXD;
4311 dev->watchdog_timeo = 5 * HZ;
4312 dev->netdev_ops = &mvneta_netdev_ops;
4313
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004314 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004315
4316 pp = netdev_priv(dev);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01004317 spin_lock_init(&pp->lock);
Russell King503f9aa92018-01-02 17:24:44 +00004318 pp->phylink = phylink;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004319 pp->phy_interface = phy_mode;
Russell King503f9aa92018-01-02 17:24:44 +00004320 pp->dn = dn;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004321
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01004322 pp->rxq_def = rxq_def;
4323
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01004324 /* Set RX packet offset correction for platforms, whose
4325 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4326 * platforms and 0B for 32-bit ones.
4327 */
4328 pp->rx_offset_correction =
4329 max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
4330
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004331 pp->indir[0] = rxq_def;
4332
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004333 /* Get special SoC configurations */
4334 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4335 pp->neta_armada3700 = true;
4336
Jisheng Zhang2804ba42016-01-20 19:27:23 +08004337 pp->clk = devm_clk_get(&pdev->dev, "core");
4338 if (IS_ERR(pp->clk))
4339 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004340 if (IS_ERR(pp->clk)) {
4341 err = PTR_ERR(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00004342 goto err_free_phylink;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004343 }
4344
4345 clk_prepare_enable(pp->clk);
4346
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004347 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4348 if (!IS_ERR(pp->clk_bus))
4349 clk_prepare_enable(pp->clk_bus);
4350
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004351 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4352 pp->base = devm_ioremap_resource(&pdev->dev, res);
4353 if (IS_ERR(pp->base)) {
4354 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004355 goto err_clk;
4356 }
4357
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004358 /* Alloc per-cpu port structure */
4359 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4360 if (!pp->ports) {
4361 err = -ENOMEM;
4362 goto err_clk;
4363 }
4364
willy tarreau74c41b02014-01-16 08:20:08 +01004365 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08004366 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01004367 if (!pp->stats) {
4368 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004369 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01004370 }
4371
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004372 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01004373 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004374 mac_from = "device tree";
4375 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4376 } else {
4377 mvneta_get_mac_addr(pp, hw_mac_addr);
4378 if (is_valid_ether_addr(hw_mac_addr)) {
4379 mac_from = "hardware";
4380 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4381 } else {
4382 mac_from = "random";
4383 eth_hw_addr_random(dev);
4384 }
4385 }
4386
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004387 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4388 if (tx_csum_limit < 0 ||
4389 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4390 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4391 dev_info(&pdev->dev,
4392 "Wrong TX csum limit in DT, set to %dB\n",
4393 MVNETA_TX_CSUM_DEF_SIZE);
4394 }
4395 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4396 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4397 } else {
4398 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4399 }
4400
4401 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02004402
Jane Li9768b452017-03-16 16:22:28 +08004403 pp->dram_target_info = mv_mbus_dram_info();
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004404 /* Armada3700 requires setting default configuration of Mbus
4405 * windows, however without using filled mbus_dram_target_info
4406 * structure.
4407 */
Jane Li9768b452017-03-16 16:22:28 +08004408 if (pp->dram_target_info || pp->neta_armada3700)
4409 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004410
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004411 pp->tx_ring_size = MVNETA_MAX_TXD;
4412 pp->rx_ring_size = MVNETA_MAX_RXD;
4413
4414 pp->dev = dev;
4415 SET_NETDEV_DEV(dev, &pdev->dev);
4416
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004417 pp->id = global_port_id++;
4418
4419 /* Obtain access to BM resources if enabled and already initialized */
4420 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4421 if (bm_node && bm_node->data) {
4422 pp->bm_priv = bm_node->data;
4423 err = mvneta_bm_port_init(pdev, pp);
4424 if (err < 0) {
4425 dev_info(&pdev->dev, "use SW buffer management\n");
4426 pp->bm_priv = NULL;
4427 }
4428 }
Peter Chend4e4da02016-08-01 15:02:36 +08004429 of_node_put(bm_node);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004430
Ezequiel Garcia96728502014-05-22 20:06:59 -03004431 err = mvneta_init(&pdev->dev, pp);
4432 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004433 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004434
4435 err = mvneta_port_power_up(pp, phy_mode);
4436 if (err < 0) {
4437 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004438 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004439 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004440
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004441 /* Armada3700 network controller does not support per-cpu
4442 * operation, so only single NAPI should be initialized.
4443 */
4444 if (pp->neta_armada3700) {
4445 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4446 } else {
4447 for_each_present_cpu(cpu) {
4448 struct mvneta_pcpu_port *port =
4449 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004450
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004451 netif_napi_add(dev, &port->napi, mvneta_poll,
4452 NAPI_POLL_WEIGHT);
4453 port->pp = pp;
4454 }
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004455 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004456
Andrew Pilloudc54a5042017-09-01 07:49:49 -07004457 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03004458 dev->hw_features |= dev->features;
4459 dev->vlan_features |= dev->features;
Andrew Lunn97db8af2016-11-24 00:08:13 +01004460 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004461 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00004462
Jarod Wilson57779872016-10-17 15:54:06 -04004463 /* MTU range: 68 - 9676 */
4464 dev->min_mtu = ETH_MIN_MTU;
4465 /* 9676 == 9700 - 20 and rounding to 8 */
4466 dev->max_mtu = 9676;
4467
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004468 err = register_netdev(dev);
4469 if (err < 0) {
4470 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03004471 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004472 }
4473
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004474 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4475 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004476
4477 platform_set_drvdata(pdev, pp->dev);
4478
4479 return 0;
4480
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004481err_netdev:
4482 unregister_netdev(dev);
4483 if (pp->bm_priv) {
4484 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4485 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4486 1 << pp->id);
4487 }
willy tarreau74c41b02014-01-16 08:20:08 +01004488err_free_stats:
4489 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004490err_free_ports:
4491 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004492err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004493 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004494 clk_disable_unprepare(pp->clk);
Russell King503f9aa92018-01-02 17:24:44 +00004495err_free_phylink:
4496 if (pp->phylink)
4497 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004498err_free_irq:
4499 irq_dispose_mapping(dev->irq);
4500err_free_netdev:
4501 free_netdev(dev);
4502 return err;
4503}
4504
4505/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00004506static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004507{
4508 struct net_device *dev = platform_get_drvdata(pdev);
4509 struct mvneta_port *pp = netdev_priv(dev);
4510
4511 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004512 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004513 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004514 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01004515 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004516 irq_dispose_mapping(dev->irq);
Russell King503f9aa92018-01-02 17:24:44 +00004517 phylink_destroy(pp->phylink);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004518 free_netdev(dev);
4519
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004520 if (pp->bm_priv) {
4521 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4522 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4523 1 << pp->id);
4524 }
4525
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004526 return 0;
4527}
4528
Jane Li9768b452017-03-16 16:22:28 +08004529#ifdef CONFIG_PM_SLEEP
4530static int mvneta_suspend(struct device *device)
4531{
4532 struct net_device *dev = dev_get_drvdata(device);
4533 struct mvneta_port *pp = netdev_priv(dev);
4534
Russell King3b8bc672018-01-02 17:24:34 +00004535 rtnl_lock();
Jane Li9768b452017-03-16 16:22:28 +08004536 if (netif_running(dev))
4537 mvneta_stop(dev);
Russell King3b8bc672018-01-02 17:24:34 +00004538 rtnl_unlock();
Jane Li9768b452017-03-16 16:22:28 +08004539 netif_device_detach(dev);
4540 clk_disable_unprepare(pp->clk_bus);
4541 clk_disable_unprepare(pp->clk);
4542 return 0;
4543}
4544
4545static int mvneta_resume(struct device *device)
4546{
4547 struct platform_device *pdev = to_platform_device(device);
4548 struct net_device *dev = dev_get_drvdata(device);
4549 struct mvneta_port *pp = netdev_priv(dev);
4550 int err;
4551
4552 clk_prepare_enable(pp->clk);
4553 if (!IS_ERR(pp->clk_bus))
4554 clk_prepare_enable(pp->clk_bus);
4555 if (pp->dram_target_info || pp->neta_armada3700)
4556 mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4557 if (pp->bm_priv) {
4558 err = mvneta_bm_port_init(pdev, pp);
4559 if (err < 0) {
4560 dev_info(&pdev->dev, "use SW buffer management\n");
4561 pp->bm_priv = NULL;
4562 }
4563 }
4564 mvneta_defaults_set(pp);
4565 err = mvneta_port_power_up(pp, pp->phy_interface);
4566 if (err < 0) {
4567 dev_err(device, "can't power up port\n");
4568 return err;
4569 }
4570
Jane Li9768b452017-03-16 16:22:28 +08004571 netif_device_attach(dev);
Russell King3b8bc672018-01-02 17:24:34 +00004572 rtnl_lock();
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004573 if (netif_running(dev)) {
Jane Li9768b452017-03-16 16:22:28 +08004574 mvneta_open(dev);
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004575 mvneta_set_rx_mode(dev);
4576 }
Russell King3b8bc672018-01-02 17:24:34 +00004577 rtnl_unlock();
Jisheng Zhangd6956ac2017-03-29 16:47:19 +08004578
Jane Li9768b452017-03-16 16:22:28 +08004579 return 0;
4580}
4581#endif
4582
4583static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
4584
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004585static const struct of_device_id mvneta_match[] = {
4586 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02004587 { .compatible = "marvell,armada-xp-neta" },
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004588 { .compatible = "marvell,armada-3700-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004589 { }
4590};
4591MODULE_DEVICE_TABLE(of, mvneta_match);
4592
4593static struct platform_driver mvneta_driver = {
4594 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00004595 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004596 .driver = {
4597 .name = MVNETA_DRIVER_NAME,
4598 .of_match_table = mvneta_match,
Jane Li9768b452017-03-16 16:22:28 +08004599 .pm = &mvneta_pm_ops,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004600 },
4601};
4602
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004603static int __init mvneta_driver_init(void)
4604{
4605 int ret;
4606
4607 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4608 mvneta_cpu_online,
4609 mvneta_cpu_down_prepare);
4610 if (ret < 0)
4611 goto out;
4612 online_hpstate = ret;
4613 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4614 NULL, mvneta_cpu_dead);
4615 if (ret)
4616 goto err_dead;
4617
4618 ret = platform_driver_register(&mvneta_driver);
4619 if (ret)
4620 goto err;
4621 return 0;
4622
4623err:
4624 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4625err_dead:
4626 cpuhp_remove_multi_state(online_hpstate);
4627out:
4628 return ret;
4629}
4630module_init(mvneta_driver_init);
4631
4632static void __exit mvneta_driver_exit(void)
4633{
4634 platform_driver_unregister(&mvneta_driver);
4635 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4636 cpuhp_remove_multi_state(online_hpstate);
4637}
4638module_exit(mvneta_driver_exit);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004639
4640MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4641MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4642MODULE_LICENSE("GPL");
4643
4644module_param(rxq_number, int, S_IRUGO);
4645module_param(txq_number, int, S_IRUGO);
4646
4647module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01004648module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);