blob: 61dd4462411c03511d6121d75ca65dc36c7f688f [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 King9303ab22017-02-07 15:03:00 -080031#include <linux/phy_fixed.h>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080032#include <linux/platform_device.h>
33#include <linux/skbuff.h>
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +010034#include <net/hwbm.h>
Marcin Wojtasdc35a102016-03-14 09:39:03 +010035#include "mvneta_bm.h"
Jisheng Zhang0e03f562016-01-20 19:27:22 +080036#include <net/ip.h>
37#include <net/ipv6.h>
38#include <net/tso.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030039
40/* Registers */
41#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
Marcin Wojtase5bdf682015-11-30 13:27:42 +010042#define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +010043#define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
44#define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
45#define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
46#define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
Thomas Petazzonic5aff182012-08-17 14:04:28 +030047#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
48#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
49#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
50#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
51#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
52#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
53#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
54#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
55#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
56#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
57#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
58#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
59#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
Marcin Wojtasdc35a102016-03-14 09:39:03 +010060#define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
61#define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
62#define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
Thomas Petazzonic5aff182012-08-17 14:04:28 +030063#define MVNETA_PORT_RX_RESET 0x1cc0
64#define MVNETA_PORT_RX_DMA_RESET BIT(0)
65#define MVNETA_PHY_ADDR 0x2000
66#define MVNETA_PHY_ADDR_MASK 0x1f
67#define MVNETA_MBUS_RETRY 0x2010
68#define MVNETA_UNIT_INTR_CAUSE 0x2080
69#define MVNETA_UNIT_CONTROL 0x20B0
70#define MVNETA_PHY_POLLING_ENABLE BIT(1)
71#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
72#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
73#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
74#define MVNETA_BASE_ADDR_ENABLE 0x2290
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +010075#define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
Thomas Petazzonic5aff182012-08-17 14:04:28 +030076#define MVNETA_PORT_CONFIG 0x2400
77#define MVNETA_UNI_PROMISC_MODE BIT(0)
78#define MVNETA_DEF_RXQ(q) ((q) << 1)
79#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
80#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
81#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
82#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
83#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
84#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
85#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
86 MVNETA_DEF_RXQ_ARP(q) | \
87 MVNETA_DEF_RXQ_TCP(q) | \
88 MVNETA_DEF_RXQ_UDP(q) | \
89 MVNETA_DEF_RXQ_BPDU(q) | \
90 MVNETA_TX_UNSET_ERR_SUM | \
91 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92#define MVNETA_PORT_CONFIG_EXTEND 0x2404
93#define MVNETA_MAC_ADDR_LOW 0x2414
94#define MVNETA_MAC_ADDR_HIGH 0x2418
95#define MVNETA_SDMA_CONFIG 0x241c
96#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030097#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
98#define MVNETA_RX_NO_DATA_SWAP BIT(4)
99#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200100#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300101#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
102#define MVNETA_PORT_STATUS 0x2444
103#define MVNETA_TX_IN_PRGRS BIT(1)
104#define MVNETA_TX_FIFO_EMPTY BIT(8)
105#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200106#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200107#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200108#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300109#define MVNETA_TYPE_PRIO 0x24bc
110#define MVNETA_FORCE_UNI BIT(21)
111#define MVNETA_TXQ_CMD_1 0x24e4
112#define MVNETA_TXQ_CMD 0x2448
113#define MVNETA_TXQ_DISABLE_SHIFT 8
114#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Andrew Lunne4839112015-10-22 18:37:36 +0100115#define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
116#define MVNETA_OVERRUN_FRAME_COUNT 0x2488
Stas Sergeev898b29702015-04-01 20:32:49 +0300117#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
118#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300119#define MVNETA_ACC_MODE 0x2500
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100120#define MVNETA_BM_ADDRESS 0x2504
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300121#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
122#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
123#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100124#define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100125#define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300126#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100127
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100128/* Exception Interrupt Port/Queue Cause register
129 *
130 * Their behavior depend of the mapping done using the PCPX2Q
131 * registers. For a given CPU if the bit associated to a queue is not
132 * set, then for the register a read from this CPU will always return
133 * 0 and a write won't do anything
134 */
willy tarreau40ba35e2014-01-16 08:20:10 +0100135
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300136#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300137#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100138
139/* bits 0..7 = TXQ SENT, one bit per queue.
140 * bits 8..15 = RXQ OCCUP, one bit per queue.
141 * bits 16..23 = RXQ FREE, one bit per queue.
142 * bit 29 = OLD_REG_SUM, see old reg ?
143 * bit 30 = TX_ERR_SUM, one bit for 4 ports
144 * bit 31 = MISC_SUM, one bit for 4 ports
145 */
146#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
147#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
148#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
149#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b29702015-04-01 20:32:49 +0300150#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100151
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300152#define MVNETA_INTR_OLD_CAUSE 0x25a8
153#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100154
155/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300156#define MVNETA_INTR_MISC_CAUSE 0x25b0
157#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100158
159#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
160#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
161#define MVNETA_CAUSE_PTP BIT(4)
162
163#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
164#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
165#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
166#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
167#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
168#define MVNETA_CAUSE_PRBS_ERR BIT(12)
169#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
170#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
171
172#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
173#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
174#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
175
176#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
177#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
178#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
179
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300180#define MVNETA_INTR_ENABLE 0x25b8
181#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
Marcin Wojtasdc1aadf2015-11-30 13:27:43 +0100182#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
willy tarreau40ba35e2014-01-16 08:20:10 +0100183
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300184#define MVNETA_RXQ_CMD 0x2680
185#define MVNETA_RXQ_DISABLE_SHIFT 8
186#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
187#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
188#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
189#define MVNETA_GMAC_CTRL_0 0x2c00
190#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
191#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
192#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
193#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b29702015-04-01 20:32:49 +0300194#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100195#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300196#define MVNETA_GMAC2_PORT_RGMII BIT(4)
197#define MVNETA_GMAC2_PORT_RESET BIT(6)
198#define MVNETA_GMAC_STATUS 0x2c10
199#define MVNETA_GMAC_LINK_UP BIT(0)
200#define MVNETA_GMAC_SPEED_1000 BIT(1)
201#define MVNETA_GMAC_SPEED_100 BIT(2)
202#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
203#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
204#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
205#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
206#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
207#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
208#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
209#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b29702015-04-01 20:32:49 +0300210#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300211#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
212#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200213#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Stas Sergeev898b29702015-04-01 20:32:49 +0300214#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300215#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200216#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Andrew Lunne4839112015-10-22 18:37:36 +0100217#define MVNETA_MIB_COUNTERS_BASE 0x3000
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300218#define MVNETA_MIB_LATE_COLLISION 0x7c
219#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
220#define MVNETA_DA_FILT_OTH_MCAST 0x3500
221#define MVNETA_DA_FILT_UCAST_BASE 0x3600
222#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
223#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
224#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
225#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
226#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
227#define MVNETA_TXQ_DEC_SENT_SHIFT 16
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100228#define MVNETA_TXQ_DEC_SENT_MASK 0xff
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300229#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
230#define MVNETA_TXQ_SENT_DESC_SHIFT 16
231#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
232#define MVNETA_PORT_TX_RESET 0x3cf0
233#define MVNETA_PORT_TX_DMA_RESET BIT(0)
234#define MVNETA_TX_MTU 0x3e0c
235#define MVNETA_TX_TOKEN_SIZE 0x3e14
236#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
237#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
238#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
239
240#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
241
242/* Descriptor ring Macros */
243#define MVNETA_QUEUE_NEXT_DESC(q, index) \
244 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
245
246/* Various constants */
247
248/* Coalescing */
Dmitri Epshtein06708f82016-07-06 04:18:58 +0200249#define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300250#define MVNETA_RX_COAL_PKTS 32
251#define MVNETA_RX_COAL_USEC 100
252
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100253/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300254 * by Marvell switches when a specific hardware mode is enabled (not
255 * supported by this driver) or is filled automatically by zeroes on
256 * the RX side. Those two bytes being at the front of the Ethernet
257 * header, they allow to have the IP header aligned on a 4 bytes
258 * boundary automatically: the hardware skips those two bytes on its
259 * own.
260 */
261#define MVNETA_MH_SIZE 2
262
263#define MVNETA_VLAN_TAG_LEN 4
264
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100265#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300266#define MVNETA_TX_CSUM_MAX_SIZE 9800
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100267#define MVNETA_ACC_MODE_EXT1 1
268#define MVNETA_ACC_MODE_EXT2 2
269
270#define MVNETA_MAX_DECODE_WIN 6
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300271
272/* Timeout constants */
273#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
274#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
275#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
276
277#define MVNETA_TX_MTU_MAX 0x3ffff
278
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100279/* The RSS lookup table actually has 256 entries but we do not use
280 * them yet
281 */
282#define MVNETA_RSS_LU_TABLE_SIZE 1
283
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300284/* TSO header size */
285#define TSO_HEADER_SIZE 128
286
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300287/* Max number of Rx descriptors */
288#define MVNETA_MAX_RXD 128
289
290/* Max number of Tx descriptors */
291#define MVNETA_MAX_TXD 532
292
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300293/* Max number of allowed TCP segments for software TSO */
294#define MVNETA_MAX_TSO_SEGS 100
295
296#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
297
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300298/* descriptor aligned size */
299#define MVNETA_DESC_ALIGNED_SIZE 32
300
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100301/* Number of bytes to be taken into account by HW when putting incoming data
302 * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
303 * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
304 */
305#define MVNETA_RX_PKT_OFFSET_CORRECTION 64
306
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300307#define MVNETA_RX_PKT_SIZE(mtu) \
308 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
309 ETH_HLEN + ETH_FCS_LEN, \
Jisheng Zhangc66e98c2016-04-01 17:12:49 +0800310 cache_line_size())
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300311
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300312#define IS_TSO_HEADER(txq, addr) \
313 ((addr >= txq->tso_hdrs_phys) && \
314 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
315
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100316#define MVNETA_RX_GET_BM_POOL_ID(rxd) \
317 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300318
Russell King9b0cdef2015-10-22 18:37:30 +0100319struct mvneta_statistic {
320 unsigned short offset;
321 unsigned short type;
322 const char name[ETH_GSTRING_LEN];
323};
324
325#define T_REG_32 32
326#define T_REG_64 64
327
328static const struct mvneta_statistic mvneta_statistics[] = {
329 { 0x3000, T_REG_64, "good_octets_received", },
330 { 0x3010, T_REG_32, "good_frames_received", },
331 { 0x3008, T_REG_32, "bad_octets_received", },
332 { 0x3014, T_REG_32, "bad_frames_received", },
333 { 0x3018, T_REG_32, "broadcast_frames_received", },
334 { 0x301c, T_REG_32, "multicast_frames_received", },
335 { 0x3050, T_REG_32, "unrec_mac_control_received", },
336 { 0x3058, T_REG_32, "good_fc_received", },
337 { 0x305c, T_REG_32, "bad_fc_received", },
338 { 0x3060, T_REG_32, "undersize_received", },
339 { 0x3064, T_REG_32, "fragments_received", },
340 { 0x3068, T_REG_32, "oversize_received", },
341 { 0x306c, T_REG_32, "jabber_received", },
342 { 0x3070, T_REG_32, "mac_receive_error", },
343 { 0x3074, T_REG_32, "bad_crc_event", },
344 { 0x3078, T_REG_32, "collision", },
345 { 0x307c, T_REG_32, "late_collision", },
346 { 0x2484, T_REG_32, "rx_discard", },
347 { 0x2488, T_REG_32, "rx_overrun", },
348 { 0x3020, T_REG_32, "frames_64_octets", },
349 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
350 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
351 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
352 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
353 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
354 { 0x3038, T_REG_64, "good_octets_sent", },
355 { 0x3040, T_REG_32, "good_frames_sent", },
356 { 0x3044, T_REG_32, "excessive_collision", },
357 { 0x3048, T_REG_32, "multicast_frames_sent", },
358 { 0x304c, T_REG_32, "broadcast_frames_sent", },
359 { 0x3054, T_REG_32, "fc_sent", },
360 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
361};
362
willy tarreau74c41b02014-01-16 08:20:08 +0100363struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300364 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100365 u64 rx_packets;
366 u64 rx_bytes;
367 u64 tx_packets;
368 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300369};
370
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200371struct mvneta_pcpu_port {
372 /* Pointer to the shared port */
373 struct mvneta_port *pp;
374
375 /* Pointer to the CPU-local NAPI struct */
376 struct napi_struct napi;
377
378 /* Cause of the previous interrupt */
379 u32 cause_rx_tx;
380};
381
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300382struct mvneta_port {
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100383 u8 id;
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200384 struct mvneta_pcpu_port __percpu *ports;
385 struct mvneta_pcpu_stats __percpu *stats;
386
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300387 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100388 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300389 void __iomem *base;
390 struct mvneta_rx_queue *rxqs;
391 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300392 struct net_device *dev;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200393 struct hlist_node node_online;
394 struct hlist_node node_dead;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100395 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100396 /* Protect the access to the percpu interrupt registers,
397 * ensuring that the configuration remains coherent.
398 */
399 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100400 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300401
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100402 u32 cause_rx_tx;
403 struct napi_struct napi;
404
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300405 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100406 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800407 /* AXI clock */
408 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300409 u8 mcast_count[256];
410 u16 tx_ring_size;
411 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300412
413 struct mii_bus *mii_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300414 phy_interface_t phy_interface;
415 struct device_node *phy_node;
416 unsigned int link;
417 unsigned int duplex;
418 unsigned int speed;
Simon Guinotb65657f2015-06-30 16:20:22 +0200419 unsigned int tx_csum_limit;
Stas Sergeev0c0744f2015-12-02 20:35:11 +0300420 unsigned int use_inband_status:1;
Russell King9b0cdef2015-10-22 18:37:30 +0100421
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100422 struct mvneta_bm *bm_priv;
423 struct mvneta_bm_pool *pool_long;
424 struct mvneta_bm_pool *pool_short;
425 int bm_win_id;
426
Russell King9b0cdef2015-10-22 18:37:30 +0100427 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100428
429 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100430
431 /* Flags for special SoC configurations */
432 bool neta_armada3700;
Marcin Wojtas8d5047c2016-12-01 18:03:07 +0100433 u16 rx_offset_correction;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300434};
435
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100436/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300437 * layout of the transmit and reception DMA descriptors, and their
438 * layout is therefore defined by the hardware design
439 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200440
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300441#define MVNETA_TX_L3_OFF_SHIFT 0
442#define MVNETA_TX_IP_HLEN_SHIFT 8
443#define MVNETA_TX_L4_UDP BIT(16)
444#define MVNETA_TX_L3_IP6 BIT(17)
445#define MVNETA_TXD_IP_CSUM BIT(18)
446#define MVNETA_TXD_Z_PAD BIT(19)
447#define MVNETA_TXD_L_DESC BIT(20)
448#define MVNETA_TXD_F_DESC BIT(21)
449#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
450 MVNETA_TXD_L_DESC | \
451 MVNETA_TXD_F_DESC)
452#define MVNETA_TX_L4_CSUM_FULL BIT(30)
453#define MVNETA_TX_L4_CSUM_NOT BIT(31)
454
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300455#define MVNETA_RXD_ERR_CRC 0x0
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100456#define MVNETA_RXD_BM_POOL_SHIFT 13
457#define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300458#define MVNETA_RXD_ERR_SUMMARY BIT(16)
459#define MVNETA_RXD_ERR_OVERRUN BIT(17)
460#define MVNETA_RXD_ERR_LEN BIT(18)
461#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
462#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
463#define MVNETA_RXD_L3_IP4 BIT(25)
464#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
465#define MVNETA_RXD_L4_CSUM_OK BIT(30)
466
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200467#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200468struct mvneta_tx_desc {
469 u32 command; /* Options used by HW for packet transmitting.*/
470 u16 reserverd1; /* csum_l4 (for future use) */
471 u16 data_size; /* Data size of transmitted packet in bytes */
472 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
473 u32 reserved2; /* hw_cmd - (for future use, PMT) */
474 u32 reserved3[4]; /* Reserved - (for future use) */
475};
476
477struct mvneta_rx_desc {
478 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300479 u16 reserved1; /* pnc_info - (for future use, PnC) */
480 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200481
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300482 u32 buf_phys_addr; /* Physical address of the buffer */
483 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200484
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300485 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
486 u16 reserved3; /* prefetch_cmd, for future use */
487 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200488
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300489 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
490 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
491};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200492#else
493struct mvneta_tx_desc {
494 u16 data_size; /* Data size of transmitted packet in bytes */
495 u16 reserverd1; /* csum_l4 (for future use) */
496 u32 command; /* Options used by HW for packet transmitting.*/
497 u32 reserved2; /* hw_cmd - (for future use, PMT) */
498 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
499 u32 reserved3[4]; /* Reserved - (for future use) */
500};
501
502struct mvneta_rx_desc {
503 u16 data_size; /* Size of received packet in bytes */
504 u16 reserved1; /* pnc_info - (for future use, PnC) */
505 u32 status; /* Info about received packet */
506
507 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
508 u32 buf_phys_addr; /* Physical address of the buffer */
509
510 u16 reserved4; /* csum_l4 - (for future use, PnC) */
511 u16 reserved3; /* prefetch_cmd, for future use */
512 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
513
514 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
515 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
516};
517#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300518
519struct mvneta_tx_queue {
520 /* Number of this TX queue, in the range 0-7 */
521 u8 id;
522
523 /* Number of TX DMA descriptors in the descriptor ring */
524 int size;
525
526 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100527 * descriptor ring
528 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300529 int count;
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100530 int pending;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300531 int tx_stop_threshold;
532 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300533
534 /* Array of transmitted skb */
535 struct sk_buff **tx_skb;
536
537 /* Index of last TX DMA descriptor that was inserted */
538 int txq_put_index;
539
540 /* Index of the TX DMA descriptor to be cleaned up */
541 int txq_get_index;
542
543 u32 done_pkts_coal;
544
545 /* Virtual address of the TX DMA descriptors array */
546 struct mvneta_tx_desc *descs;
547
548 /* DMA address of the TX DMA descriptors array */
549 dma_addr_t descs_phys;
550
551 /* Index of the last TX DMA descriptor */
552 int last_desc;
553
554 /* Index of the next TX DMA descriptor to process */
555 int next_desc_to_proc;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300556
557 /* DMA buffers for TSO headers */
558 char *tso_hdrs;
559
560 /* DMA address of TSO headers */
561 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100562
563 /* Affinity mask for CPUs*/
564 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300565};
566
567struct mvneta_rx_queue {
568 /* rx queue number, in the range 0-7 */
569 u8 id;
570
571 /* num of rx descriptors in the rx descriptor ring */
572 int size;
573
574 /* counter of times when mvneta_refill() failed */
575 int missed;
576
577 u32 pkts_coal;
578 u32 time_coal;
579
Gregory CLEMENTf88bee12016-12-01 18:03:06 +0100580 /* Virtual address of the RX buffer */
581 void **buf_virt_addr;
582
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300583 /* Virtual address of the RX DMA descriptors array */
584 struct mvneta_rx_desc *descs;
585
586 /* DMA address of the RX DMA descriptors array */
587 dma_addr_t descs_phys;
588
589 /* Index of the last RX DMA descriptor */
590 int last_desc;
591
592 /* Index of the next RX DMA descriptor to process */
593 int next_desc_to_proc;
594};
595
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200596static enum cpuhp_state online_hpstate;
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300597/* The hardware supports eight (8) rx queues, but we are only allowing
598 * the first one to be used. Therefore, let's just allocate one queue.
599 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200600static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300601static int txq_number = 8;
602
603static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300604
willy tarreauf19fadf2014-01-16 08:20:17 +0100605static int rx_copybreak __read_mostly = 256;
606
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100607/* HW BM need that each port be identify by a unique ID */
608static int global_port_id;
609
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300610#define MVNETA_DRIVER_NAME "mvneta"
611#define MVNETA_DRIVER_VERSION "1.0"
612
613/* Utility/helper methods */
614
615/* Write helper method */
616static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
617{
618 writel(data, pp->base + offset);
619}
620
621/* Read helper method */
622static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
623{
624 return readl(pp->base + offset);
625}
626
627/* Increment txq get counter */
628static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
629{
630 txq->txq_get_index++;
631 if (txq->txq_get_index == txq->size)
632 txq->txq_get_index = 0;
633}
634
635/* Increment txq put counter */
636static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
637{
638 txq->txq_put_index++;
639 if (txq->txq_put_index == txq->size)
640 txq->txq_put_index = 0;
641}
642
643
644/* Clear all MIB counters */
645static void mvneta_mib_counters_clear(struct mvneta_port *pp)
646{
647 int i;
648 u32 dummy;
649
650 /* Perform dummy reads from MIB counters */
651 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
652 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100653 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
654 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300655}
656
657/* Get System Network Statistics */
stephen hemmingerbc1f4472017-01-06 19:12:52 -0800658static void
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +0800659mvneta_get_stats64(struct net_device *dev,
660 struct rtnl_link_stats64 *stats)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300661{
662 struct mvneta_port *pp = netdev_priv(dev);
663 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100664 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300665
willy tarreau74c41b02014-01-16 08:20:08 +0100666 for_each_possible_cpu(cpu) {
667 struct mvneta_pcpu_stats *cpu_stats;
668 u64 rx_packets;
669 u64 rx_bytes;
670 u64 tx_packets;
671 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300672
willy tarreau74c41b02014-01-16 08:20:08 +0100673 cpu_stats = per_cpu_ptr(pp->stats, cpu);
674 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700675 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100676 rx_packets = cpu_stats->rx_packets;
677 rx_bytes = cpu_stats->rx_bytes;
678 tx_packets = cpu_stats->tx_packets;
679 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700680 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300681
willy tarreau74c41b02014-01-16 08:20:08 +0100682 stats->rx_packets += rx_packets;
683 stats->rx_bytes += rx_bytes;
684 stats->tx_packets += tx_packets;
685 stats->tx_bytes += tx_bytes;
686 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300687
688 stats->rx_errors = dev->stats.rx_errors;
689 stats->rx_dropped = dev->stats.rx_dropped;
690
691 stats->tx_dropped = dev->stats.tx_dropped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300692}
693
694/* Rx descriptors helper methods */
695
willy tarreau54282132014-01-16 08:20:14 +0100696/* Checks whether the RX descriptor having this status is both the first
697 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300698 * received through a single RX descriptor, so not having each RX
699 * descriptor with its first and last bits set is an error
700 */
willy tarreau54282132014-01-16 08:20:14 +0100701static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300702{
willy tarreau54282132014-01-16 08:20:14 +0100703 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300704 MVNETA_RXD_FIRST_LAST_DESC;
705}
706
707/* Add number of descriptors ready to receive new packets */
708static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
709 struct mvneta_rx_queue *rxq,
710 int ndescs)
711{
712 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100713 * be added at once
714 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300715 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
716 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
717 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
718 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
719 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
720 }
721
722 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
723 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
724}
725
726/* Get number of RX descriptors occupied by received packets */
727static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
728 struct mvneta_rx_queue *rxq)
729{
730 u32 val;
731
732 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
733 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
734}
735
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100736/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300737 * from mvneta_rxq_drop_pkts().
738 */
739static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
740 struct mvneta_rx_queue *rxq,
741 int rx_done, int rx_filled)
742{
743 u32 val;
744
745 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
746 val = rx_done |
747 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
748 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
749 return;
750 }
751
752 /* Only 255 descriptors can be added at once */
753 while ((rx_done > 0) || (rx_filled > 0)) {
754 if (rx_done <= 0xff) {
755 val = rx_done;
756 rx_done = 0;
757 } else {
758 val = 0xff;
759 rx_done -= 0xff;
760 }
761 if (rx_filled <= 0xff) {
762 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
763 rx_filled = 0;
764 } else {
765 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
766 rx_filled -= 0xff;
767 }
768 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
769 }
770}
771
772/* Get pointer to next RX descriptor to be processed by SW */
773static struct mvneta_rx_desc *
774mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
775{
776 int rx_desc = rxq->next_desc_to_proc;
777
778 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100779 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300780 return rxq->descs + rx_desc;
781}
782
783/* Change maximum receive size of the port. */
784static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
785{
786 u32 val;
787
788 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
789 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
790 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
791 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
792 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
793}
794
795
796/* Set rx queue offset */
797static void mvneta_rxq_offset_set(struct mvneta_port *pp,
798 struct mvneta_rx_queue *rxq,
799 int offset)
800{
801 u32 val;
802
803 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
804 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
805
806 /* Offset is in */
807 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
808 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
809}
810
811
812/* Tx descriptors helper methods */
813
814/* Update HW with number of TX descriptors to be sent */
815static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
816 struct mvneta_tx_queue *txq,
817 int pend_desc)
818{
819 u32 val;
820
821 /* Only 255 descriptors can be added at once ; Assume caller
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100822 * process TX desriptors in quanta less than 256
823 */
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100824 val = pend_desc + txq->pending;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300825 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
Simon Guinot2a90f7e2017-01-16 18:08:31 +0100826 txq->pending = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300827}
828
829/* Get pointer to next TX descriptor to be processed (send) by HW */
830static struct mvneta_tx_desc *
831mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
832{
833 int tx_desc = txq->next_desc_to_proc;
834
835 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
836 return txq->descs + tx_desc;
837}
838
839/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100840 * mapping failures in the TX path.
841 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300842static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
843{
844 if (txq->next_desc_to_proc == 0)
845 txq->next_desc_to_proc = txq->last_desc - 1;
846 else
847 txq->next_desc_to_proc--;
848}
849
850/* Set rxq buf size */
851static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
852 struct mvneta_rx_queue *rxq,
853 int buf_size)
854{
855 u32 val;
856
857 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
858
859 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
860 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
861
862 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
863}
864
865/* Disable buffer management (BM) */
866static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
867 struct mvneta_rx_queue *rxq)
868{
869 u32 val;
870
871 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
872 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
873 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
874}
875
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100876/* Enable buffer management (BM) */
877static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
878 struct mvneta_rx_queue *rxq)
879{
880 u32 val;
881
882 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
883 val |= MVNETA_RXQ_HW_BUF_ALLOC;
884 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
885}
886
887/* Notify HW about port's assignment of pool for bigger packets */
888static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
889 struct mvneta_rx_queue *rxq)
890{
891 u32 val;
892
893 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
894 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
895 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
896
897 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
898}
899
900/* Notify HW about port's assignment of pool for smaller packets */
901static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
902 struct mvneta_rx_queue *rxq)
903{
904 u32 val;
905
906 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
907 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
908 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
909
910 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
911}
912
913/* Set port's receive buffer size for assigned BM pool */
914static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
915 int buf_size,
916 u8 pool_id)
917{
918 u32 val;
919
920 if (!IS_ALIGNED(buf_size, 8)) {
921 dev_warn(pp->dev->dev.parent,
922 "illegal buf_size value %d, round to %d\n",
923 buf_size, ALIGN(buf_size, 8));
924 buf_size = ALIGN(buf_size, 8);
925 }
926
927 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
928 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
929 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
930}
931
932/* Configure MBUS window in order to enable access BM internal SRAM */
933static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
934 u8 target, u8 attr)
935{
936 u32 win_enable, win_protect;
937 int i;
938
939 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
940
941 if (pp->bm_win_id < 0) {
942 /* Find first not occupied window */
943 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
944 if (win_enable & (1 << i)) {
945 pp->bm_win_id = i;
946 break;
947 }
948 }
949 if (i == MVNETA_MAX_DECODE_WIN)
950 return -ENOMEM;
951 } else {
952 i = pp->bm_win_id;
953 }
954
955 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
956 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
957
958 if (i < 4)
959 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
960
961 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
962 (attr << 8) | target);
963
964 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
965
966 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
967 win_protect |= 3 << (2 * i);
968 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
969
970 win_enable &= ~(1 << i);
971 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
972
973 return 0;
974}
975
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100976static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100977{
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100978 u32 wsize;
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100979 u8 target, attr;
980 int err;
981
982 /* Get BM window information */
983 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
984 &target, &attr);
985 if (err < 0)
986 return err;
987
988 pp->bm_win_id = -1;
989
990 /* Open NETA -> BM window */
991 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
992 target, attr);
993 if (err < 0) {
994 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
995 return err;
996 }
Marcin Wojtas2636ac32016-12-01 18:03:09 +0100997 return 0;
998}
999
1000/* Assign and initialize pools for port. In case of fail
1001 * buffer manager will remain disabled for current port.
1002 */
1003static int mvneta_bm_port_init(struct platform_device *pdev,
1004 struct mvneta_port *pp)
1005{
1006 struct device_node *dn = pdev->dev.of_node;
1007 u32 long_pool_id, short_pool_id;
1008
1009 if (!pp->neta_armada3700) {
1010 int ret;
1011
1012 ret = mvneta_bm_port_mbus_init(pp);
1013 if (ret)
1014 return ret;
1015 }
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001016
1017 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1018 netdev_info(pp->dev, "missing long pool id\n");
1019 return -EINVAL;
1020 }
1021
1022 /* Create port's long pool depending on mtu */
1023 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1024 MVNETA_BM_LONG, pp->id,
1025 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1026 if (!pp->pool_long) {
1027 netdev_info(pp->dev, "fail to obtain long pool for port\n");
1028 return -ENOMEM;
1029 }
1030
1031 pp->pool_long->port_map |= 1 << pp->id;
1032
1033 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1034 pp->pool_long->id);
1035
1036 /* If short pool id is not defined, assume using single pool */
1037 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1038 short_pool_id = long_pool_id;
1039
1040 /* Create port's short pool */
1041 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1042 MVNETA_BM_SHORT, pp->id,
1043 MVNETA_BM_SHORT_PKT_SIZE);
1044 if (!pp->pool_short) {
1045 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1046 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1047 return -ENOMEM;
1048 }
1049
1050 if (short_pool_id != long_pool_id) {
1051 pp->pool_short->port_map |= 1 << pp->id;
1052 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1053 pp->pool_short->id);
1054 }
1055
1056 return 0;
1057}
1058
1059/* Update settings of a pool for bigger packets */
1060static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1061{
1062 struct mvneta_bm_pool *bm_pool = pp->pool_long;
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001063 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001064 int num;
1065
1066 /* Release all buffers from long pool */
1067 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001068 if (hwbm_pool->buf_num) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001069 WARN(1, "cannot free all buffers in pool %d\n",
1070 bm_pool->id);
1071 goto bm_mtu_err;
1072 }
1073
1074 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1075 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001076 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1077 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001078
1079 /* Fill entire long pool */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001080 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1081 if (num != hwbm_pool->size) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001082 WARN(1, "pool %d: %d of %d allocated\n",
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001083 bm_pool->id, num, hwbm_pool->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001084 goto bm_mtu_err;
1085 }
1086 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1087
1088 return;
1089
1090bm_mtu_err:
1091 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1092 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1093
1094 pp->bm_priv = NULL;
1095 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1096 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1097}
1098
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001099/* Start the Ethernet port RX and TX activity */
1100static void mvneta_port_up(struct mvneta_port *pp)
1101{
1102 int queue;
1103 u32 q_map;
1104
1105 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001106 q_map = 0;
1107 for (queue = 0; queue < txq_number; queue++) {
1108 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1109 if (txq->descs != NULL)
1110 q_map |= (1 << queue);
1111 }
1112 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1113
1114 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001115 for (queue = 0; queue < rxq_number; queue++) {
1116 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1117
1118 if (rxq->descs != NULL)
1119 q_map |= (1 << queue);
1120 }
1121 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001122}
1123
1124/* Stop the Ethernet port activity */
1125static void mvneta_port_down(struct mvneta_port *pp)
1126{
1127 u32 val;
1128 int count;
1129
1130 /* Stop Rx port activity. Check port Rx activity. */
1131 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1132
1133 /* Issue stop command for active channels only */
1134 if (val != 0)
1135 mvreg_write(pp, MVNETA_RXQ_CMD,
1136 val << MVNETA_RXQ_DISABLE_SHIFT);
1137
1138 /* Wait for all Rx activity to terminate. */
1139 count = 0;
1140 do {
1141 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1142 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001143 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001144 val);
1145 break;
1146 }
1147 mdelay(1);
1148
1149 val = mvreg_read(pp, MVNETA_RXQ_CMD);
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001150 } while (val & MVNETA_RXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001151
1152 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001153 * command for active channels only
1154 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001155 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1156
1157 if (val != 0)
1158 mvreg_write(pp, MVNETA_TXQ_CMD,
1159 (val << MVNETA_TXQ_DISABLE_SHIFT));
1160
1161 /* Wait for all Tx activity to terminate. */
1162 count = 0;
1163 do {
1164 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1165 netdev_warn(pp->dev,
1166 "TIMEOUT for TX stopped status=0x%08x\n",
1167 val);
1168 break;
1169 }
1170 mdelay(1);
1171
1172 /* Check TX Command reg that all Txqs are stopped */
1173 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1174
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001175 } while (val & MVNETA_TXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001176
1177 /* Double check to verify that TX FIFO is empty */
1178 count = 0;
1179 do {
1180 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1181 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001182 "TX FIFO empty timeout status=0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001183 val);
1184 break;
1185 }
1186 mdelay(1);
1187
1188 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1189 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1190 (val & MVNETA_TX_IN_PRGRS));
1191
1192 udelay(200);
1193}
1194
1195/* Enable the port by setting the port enable bit of the MAC control register */
1196static void mvneta_port_enable(struct mvneta_port *pp)
1197{
1198 u32 val;
1199
1200 /* Enable port */
1201 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1202 val |= MVNETA_GMAC0_PORT_ENABLE;
1203 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1204}
1205
1206/* Disable the port and wait for about 200 usec before retuning */
1207static void mvneta_port_disable(struct mvneta_port *pp)
1208{
1209 u32 val;
1210
1211 /* Reset the Enable bit in the Serial Control Register */
1212 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1213 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1214 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1215
1216 udelay(200);
1217}
1218
1219/* Multicast tables methods */
1220
1221/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1222static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1223{
1224 int offset;
1225 u32 val;
1226
1227 if (queue == -1) {
1228 val = 0;
1229 } else {
1230 val = 0x1 | (queue << 1);
1231 val |= (val << 24) | (val << 16) | (val << 8);
1232 }
1233
1234 for (offset = 0; offset <= 0xc; offset += 4)
1235 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1236}
1237
1238/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1239static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1240{
1241 int offset;
1242 u32 val;
1243
1244 if (queue == -1) {
1245 val = 0;
1246 } else {
1247 val = 0x1 | (queue << 1);
1248 val |= (val << 24) | (val << 16) | (val << 8);
1249 }
1250
1251 for (offset = 0; offset <= 0xfc; offset += 4)
1252 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1253
1254}
1255
1256/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1257static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1258{
1259 int offset;
1260 u32 val;
1261
1262 if (queue == -1) {
1263 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1264 val = 0;
1265 } else {
1266 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1267 val = 0x1 | (queue << 1);
1268 val |= (val << 24) | (val << 16) | (val << 8);
1269 }
1270
1271 for (offset = 0; offset <= 0xfc; offset += 4)
1272 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1273}
1274
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001275static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
1276{
1277 u32 val;
1278
1279 if (enable) {
1280 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1281 val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1282 MVNETA_GMAC_FORCE_LINK_DOWN |
1283 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1284 val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1285 MVNETA_GMAC_AN_SPEED_EN |
1286 MVNETA_GMAC_AN_DUPLEX_EN;
1287 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1288
1289 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1290 val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1291 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1292
1293 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1294 val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
1295 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1296 } else {
1297 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1298 val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1299 MVNETA_GMAC_AN_SPEED_EN |
1300 MVNETA_GMAC_AN_DUPLEX_EN);
1301 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1302
1303 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1304 val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
1305 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1306
1307 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1308 val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
1309 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1310 }
1311}
1312
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001313static void mvneta_percpu_unmask_interrupt(void *arg)
1314{
1315 struct mvneta_port *pp = arg;
1316
1317 /* All the queue are unmasked, but actually only the ones
1318 * mapped to this CPU will be unmasked
1319 */
1320 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1321 MVNETA_RX_INTR_MASK_ALL |
1322 MVNETA_TX_INTR_MASK_ALL |
1323 MVNETA_MISCINTR_INTR_MASK);
1324}
1325
1326static void mvneta_percpu_mask_interrupt(void *arg)
1327{
1328 struct mvneta_port *pp = arg;
1329
1330 /* All the queue are masked, but actually only the ones
1331 * mapped to this CPU will be masked
1332 */
1333 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1334 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1335 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1336}
1337
1338static void mvneta_percpu_clear_intr_cause(void *arg)
1339{
1340 struct mvneta_port *pp = arg;
1341
1342 /* All the queue are cleared, but actually only the ones
1343 * mapped to this CPU will be cleared
1344 */
1345 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1346 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1347 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1348}
1349
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001350/* This method sets defaults to the NETA port:
1351 * Clears interrupt Cause and Mask registers.
1352 * Clears all MAC tables.
1353 * Sets defaults to all registers.
1354 * Resets RX and TX descriptor rings.
1355 * Resets PHY.
1356 * This method can be called after mvneta_port_down() to return the port
1357 * settings to defaults.
1358 */
1359static void mvneta_defaults_set(struct mvneta_port *pp)
1360{
1361 int cpu;
1362 int queue;
1363 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001364 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001365
1366 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001367 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001368
1369 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001370 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001371 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1372
1373 /* Enable MBUS Retry bit16 */
1374 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1375
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001376 /* Set CPU queue access map. CPUs are assigned to the RX and
1377 * TX queues modulo their number. If there is only one TX
1378 * queue then it is assigned to the CPU associated to the
1379 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001380 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001381 for_each_present_cpu(cpu) {
1382 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001383 int rxq, txq;
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001384 if (!pp->neta_armada3700) {
1385 for (rxq = 0; rxq < rxq_number; rxq++)
1386 if ((rxq % max_cpu) == cpu)
1387 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001388
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001389 for (txq = 0; txq < txq_number; txq++)
1390 if ((txq % max_cpu) == cpu)
1391 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001392
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001393 /* With only one TX queue we configure a special case
1394 * which will allow to get all the irq on a single
1395 * CPU
1396 */
1397 if (txq_number == 1)
1398 txq_map = (cpu == pp->rxq_def) ?
1399 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001400
Marcin Wojtas2636ac32016-12-01 18:03:09 +01001401 } else {
1402 txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1403 rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1404 }
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001405
1406 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1407 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001408
1409 /* Reset RX and TX DMAs */
1410 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1411 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1412
1413 /* Disable Legacy WRR, Disable EJP, Release from reset */
1414 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1415 for (queue = 0; queue < txq_number; queue++) {
1416 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1417 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1418 }
1419
1420 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1421 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1422
1423 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001424 if (pp->bm_priv)
1425 /* HW buffer management + legacy parser */
1426 val = MVNETA_ACC_MODE_EXT2;
1427 else
1428 /* SW buffer management + legacy parser */
1429 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001430 mvreg_write(pp, MVNETA_ACC_MODE, val);
1431
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001432 if (pp->bm_priv)
1433 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1434
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001435 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001436 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001437 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1438
1439 val = 0;
1440 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1441 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1442
1443 /* Build PORT_SDMA_CONFIG_REG */
1444 val = 0;
1445
1446 /* Default burst size */
1447 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1448 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001449 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001450
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001451#if defined(__BIG_ENDIAN)
1452 val |= MVNETA_DESC_SWAP;
1453#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001454
1455 /* Assign port SDMA configuration */
1456 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1457
Thomas Petazzoni71408602013-09-04 16:21:18 +02001458 /* Disable PHY polling in hardware, since we're using the
1459 * kernel phylib to do this.
1460 */
1461 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1462 val &= ~MVNETA_PHY_POLLING_ENABLE;
1463 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1464
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001465 mvneta_set_autoneg(pp, pp->use_inband_status);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001466 mvneta_set_ucast_table(pp, -1);
1467 mvneta_set_special_mcast_table(pp, -1);
1468 mvneta_set_other_mcast_table(pp, -1);
1469
1470 /* Set port interrupt enable register - default enable all */
1471 mvreg_write(pp, MVNETA_INTR_ENABLE,
1472 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1473 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001474
1475 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001476}
1477
1478/* Set max sizes for tx queues */
1479static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1480
1481{
1482 u32 val, size, mtu;
1483 int queue;
1484
1485 mtu = max_tx_size * 8;
1486 if (mtu > MVNETA_TX_MTU_MAX)
1487 mtu = MVNETA_TX_MTU_MAX;
1488
1489 /* Set MTU */
1490 val = mvreg_read(pp, MVNETA_TX_MTU);
1491 val &= ~MVNETA_TX_MTU_MAX;
1492 val |= mtu;
1493 mvreg_write(pp, MVNETA_TX_MTU, val);
1494
1495 /* TX token size and all TXQs token size must be larger that MTU */
1496 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1497
1498 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1499 if (size < mtu) {
1500 size = mtu;
1501 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1502 val |= size;
1503 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1504 }
1505 for (queue = 0; queue < txq_number; queue++) {
1506 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1507
1508 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1509 if (size < mtu) {
1510 size = mtu;
1511 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1512 val |= size;
1513 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1514 }
1515 }
1516}
1517
1518/* Set unicast address */
1519static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1520 int queue)
1521{
1522 unsigned int unicast_reg;
1523 unsigned int tbl_offset;
1524 unsigned int reg_offset;
1525
1526 /* Locate the Unicast table entry */
1527 last_nibble = (0xf & last_nibble);
1528
1529 /* offset from unicast tbl base */
1530 tbl_offset = (last_nibble / 4) * 4;
1531
1532 /* offset within the above reg */
1533 reg_offset = last_nibble % 4;
1534
1535 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1536
1537 if (queue == -1) {
1538 /* Clear accepts frame bit at specified unicast DA tbl entry */
1539 unicast_reg &= ~(0xff << (8 * reg_offset));
1540 } else {
1541 unicast_reg &= ~(0xff << (8 * reg_offset));
1542 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1543 }
1544
1545 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1546}
1547
1548/* Set mac address */
1549static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1550 int queue)
1551{
1552 unsigned int mac_h;
1553 unsigned int mac_l;
1554
1555 if (queue != -1) {
1556 mac_l = (addr[4] << 8) | (addr[5]);
1557 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1558 (addr[2] << 8) | (addr[3] << 0);
1559
1560 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1561 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1562 }
1563
1564 /* Accept frames of this address */
1565 mvneta_set_ucast_addr(pp, addr[5], queue);
1566}
1567
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001568/* Set the number of packets that will be received before RX interrupt
1569 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001570 */
1571static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1572 struct mvneta_rx_queue *rxq, u32 value)
1573{
1574 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1575 value | MVNETA_RXQ_NON_OCCUPIED(0));
1576 rxq->pkts_coal = value;
1577}
1578
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001579/* Set the time delay in usec before RX interrupt will be generated by
1580 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001581 */
1582static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1583 struct mvneta_rx_queue *rxq, u32 value)
1584{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001585 u32 val;
1586 unsigned long clk_rate;
1587
1588 clk_rate = clk_get_rate(pp->clk);
1589 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001590
1591 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1592 rxq->time_coal = value;
1593}
1594
1595/* Set threshold for TX_DONE pkts coalescing */
1596static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1597 struct mvneta_tx_queue *txq, u32 value)
1598{
1599 u32 val;
1600
1601 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1602
1603 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1604 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1605
1606 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1607
1608 txq->done_pkts_coal = value;
1609}
1610
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001611/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1612static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001613 u32 phys_addr, void *virt_addr,
1614 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001615{
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001616 int i;
1617
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001618 rx_desc->buf_phys_addr = phys_addr;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001619 i = rx_desc - rxq->descs;
1620 rxq->buf_virt_addr[i] = virt_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001621}
1622
1623/* Decrement sent descriptors counter */
1624static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1625 struct mvneta_tx_queue *txq,
1626 int sent_desc)
1627{
1628 u32 val;
1629
1630 /* Only 255 TX descriptors can be updated at once */
1631 while (sent_desc > 0xff) {
1632 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1633 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1634 sent_desc = sent_desc - 0xff;
1635 }
1636
1637 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1638 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1639}
1640
1641/* Get number of TX descriptors already sent by HW */
1642static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1643 struct mvneta_tx_queue *txq)
1644{
1645 u32 val;
1646 int sent_desc;
1647
1648 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1649 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1650 MVNETA_TXQ_SENT_DESC_SHIFT;
1651
1652 return sent_desc;
1653}
1654
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001655/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001656 * The number of sent descriptors is returned.
1657 */
1658static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1659 struct mvneta_tx_queue *txq)
1660{
1661 int sent_desc;
1662
1663 /* Get number of sent descriptors */
1664 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1665
1666 /* Decrement sent descriptors counter */
1667 if (sent_desc)
1668 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1669
1670 return sent_desc;
1671}
1672
1673/* Set TXQ descriptors fields relevant for CSUM calculation */
1674static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1675 int ip_hdr_len, int l4_proto)
1676{
1677 u32 command;
1678
1679 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001680 * G_L4_chk, L4_type; required only for checksum
1681 * calculation
1682 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001683 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1684 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1685
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001686 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001687 command |= MVNETA_TXD_IP_CSUM;
1688 else
1689 command |= MVNETA_TX_L3_IP6;
1690
1691 if (l4_proto == IPPROTO_TCP)
1692 command |= MVNETA_TX_L4_CSUM_FULL;
1693 else if (l4_proto == IPPROTO_UDP)
1694 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1695 else
1696 command |= MVNETA_TX_L4_CSUM_NOT;
1697
1698 return command;
1699}
1700
1701
1702/* Display more error info */
1703static void mvneta_rx_error(struct mvneta_port *pp,
1704 struct mvneta_rx_desc *rx_desc)
1705{
1706 u32 status = rx_desc->status;
1707
willy tarreau54282132014-01-16 08:20:14 +01001708 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001709 netdev_err(pp->dev,
1710 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001711 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001712 return;
1713 }
1714
1715 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1716 case MVNETA_RXD_ERR_CRC:
1717 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1718 status, rx_desc->data_size);
1719 break;
1720 case MVNETA_RXD_ERR_OVERRUN:
1721 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1722 status, rx_desc->data_size);
1723 break;
1724 case MVNETA_RXD_ERR_LEN:
1725 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1726 status, rx_desc->data_size);
1727 break;
1728 case MVNETA_RXD_ERR_RESOURCE:
1729 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1730 status, rx_desc->data_size);
1731 break;
1732 }
1733}
1734
willy tarreau54282132014-01-16 08:20:14 +01001735/* Handle RX checksum offload based on the descriptor's status */
1736static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001737 struct sk_buff *skb)
1738{
willy tarreau54282132014-01-16 08:20:14 +01001739 if ((status & MVNETA_RXD_L3_IP4) &&
1740 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001741 skb->csum = 0;
1742 skb->ip_summed = CHECKSUM_UNNECESSARY;
1743 return;
1744 }
1745
1746 skb->ip_summed = CHECKSUM_NONE;
1747}
1748
willy tarreau6c498972014-01-16 08:20:12 +01001749/* Return tx queue pointer (find last set bit) according to <cause> returned
1750 * form tx_done reg. <cause> must not be null. The return value is always a
1751 * valid queue for matching the first one found in <cause>.
1752 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001753static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1754 u32 cause)
1755{
1756 int queue = fls(cause) - 1;
1757
willy tarreau6c498972014-01-16 08:20:12 +01001758 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001759}
1760
1761/* Free tx queue skbuffs */
1762static void mvneta_txq_bufs_free(struct mvneta_port *pp,
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001763 struct mvneta_tx_queue *txq, int num,
1764 struct netdev_queue *nq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001765{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001766 unsigned int bytes_compl = 0, pkts_compl = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001767 int i;
1768
1769 for (i = 0; i < num; i++) {
1770 struct mvneta_tx_desc *tx_desc = txq->descs +
1771 txq->txq_get_index;
1772 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1773
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001774 if (skb) {
1775 bytes_compl += skb->len;
1776 pkts_compl++;
1777 }
1778
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001779 mvneta_txq_inc_get(txq);
1780
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001781 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1782 dma_unmap_single(pp->dev->dev.parent,
1783 tx_desc->buf_phys_addr,
1784 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001785 if (!skb)
1786 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001787 dev_kfree_skb_any(skb);
1788 }
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001789
1790 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001791}
1792
1793/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001794static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001795 struct mvneta_tx_queue *txq)
1796{
1797 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1798 int tx_done;
1799
1800 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001801 if (!tx_done)
1802 return;
1803
Marcin Wojtasa29b6232017-01-16 18:08:32 +01001804 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001805
1806 txq->count -= tx_done;
1807
1808 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001809 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001810 netif_tx_wake_queue(nq);
1811 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001812}
1813
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001814void *mvneta_frag_alloc(unsigned int frag_size)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001815{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001816 if (likely(frag_size <= PAGE_SIZE))
1817 return netdev_alloc_frag(frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001818 else
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001819 return kmalloc(frag_size, GFP_ATOMIC);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001820}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001821EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001822
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001823void mvneta_frag_free(unsigned int frag_size, void *data)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001824{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001825 if (likely(frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001826 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001827 else
1828 kfree(data);
1829}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001830EXPORT_SYMBOL_GPL(mvneta_frag_free);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001831
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001832/* Refill processing for SW buffer management */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001833static int mvneta_rx_refill(struct mvneta_port *pp,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001834 struct mvneta_rx_desc *rx_desc,
1835 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001836
1837{
1838 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001839 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001840
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001841 data = mvneta_frag_alloc(pp->frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001842 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001843 return -ENOMEM;
1844
willy tarreau8ec2cd42014-01-16 08:20:16 +01001845 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001846 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1847 DMA_FROM_DEVICE);
1848 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001849 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001850 return -ENOMEM;
1851 }
1852
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01001853 phys_addr += pp->rx_offset_correction;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001854 mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001855 return 0;
1856}
1857
1858/* Handle tx checksum */
1859static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1860{
1861 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1862 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001863 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001864 u8 l4_proto;
1865
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001866 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001867 struct iphdr *ip4h = ip_hdr(skb);
1868
1869 /* Calculate IPv4 checksum and L4 checksum */
1870 ip_hdr_len = ip4h->ihl;
1871 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001872 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001873 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1874
1875 /* Read l4_protocol from one of IPv6 extra headers */
1876 if (skb_network_header_len(skb) > 0)
1877 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1878 l4_proto = ip6h->nexthdr;
1879 } else
1880 return MVNETA_TX_L4_CSUM_NOT;
1881
1882 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001883 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001884 }
1885
1886 return MVNETA_TX_L4_CSUM_NOT;
1887}
1888
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001889/* Drop packets received by the RXQ and free buffers */
1890static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1891 struct mvneta_rx_queue *rxq)
1892{
1893 int rx_done, i;
1894
1895 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001896 if (rx_done)
1897 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1898
1899 if (pp->bm_priv) {
1900 for (i = 0; i < rx_done; i++) {
1901 struct mvneta_rx_desc *rx_desc =
1902 mvneta_rxq_next_desc_get(rxq);
1903 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1904 struct mvneta_bm_pool *bm_pool;
1905
1906 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1907 /* Return dropped buffer to the pool */
1908 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1909 rx_desc->buf_phys_addr);
1910 }
1911 return;
1912 }
1913
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001914 for (i = 0; i < rxq->size; i++) {
1915 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001916 void *data = rxq->buf_virt_addr[i];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001917
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001918 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001919 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001920 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001921 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001922}
1923
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001924/* Main rx processing when using software buffer management */
1925static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1926 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001927{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001928 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001929 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001930 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001931 u32 rcvd_pkts = 0;
1932 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001933
1934 /* Get number of received packets */
1935 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1936
1937 if (rx_todo > rx_done)
1938 rx_todo = rx_done;
1939
1940 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001941
1942 /* Fairness NAPI loop */
1943 while (rx_done < rx_todo) {
1944 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1945 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001946 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001947 dma_addr_t phys_addr;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001948 u32 rx_status, frag_size;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001949 int rx_bytes, err, index;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001950
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001951 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001952 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001953 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001954 index = rx_desc - rxq->descs;
1955 data = rxq->buf_virt_addr[index];
Simon Guinotdaf158d2015-09-15 22:41:21 +02001956 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001957
willy tarreau54282132014-01-16 08:20:14 +01001958 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001959 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001960err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001961 dev->stats.rx_errors++;
1962 mvneta_rx_error(pp, rx_desc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001963 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001964 continue;
1965 }
1966
willy tarreauf19fadf2014-01-16 08:20:17 +01001967 if (rx_bytes <= rx_copybreak) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001968 /* better copy a small frame and not unmap the DMA region */
willy tarreauf19fadf2014-01-16 08:20:17 +01001969 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1970 if (unlikely(!skb))
1971 goto err_drop_frame;
1972
1973 dma_sync_single_range_for_cpu(dev->dev.parent,
Gregory CLEMENTac83b7d2016-12-01 18:03:04 +01001974 phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001975 MVNETA_MH_SIZE + NET_SKB_PAD,
1976 rx_bytes,
1977 DMA_FROM_DEVICE);
willy tarreauf19fadf2014-01-16 08:20:17 +01001978 memcpy(skb_put(skb, rx_bytes),
1979 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1980 rx_bytes);
1981
1982 skb->protocol = eth_type_trans(skb, dev);
1983 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001984 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001985
1986 rcvd_pkts++;
1987 rcvd_bytes += rx_bytes;
1988
1989 /* leave the descriptor and buffer untouched */
1990 continue;
1991 }
1992
Simon Guinota84e3282015-07-19 13:00:53 +02001993 /* Refill processing */
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001994 err = mvneta_rx_refill(pp, rx_desc, rxq);
Simon Guinota84e3282015-07-19 13:00:53 +02001995 if (err) {
1996 netdev_err(dev, "Linux processing - Can't refill\n");
1997 rxq->missed++;
1998 goto err_drop_frame;
1999 }
2000
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002001 frag_size = pp->frag_size;
2002
2003 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01002004
Marcin Wojtas26c17a172015-11-30 13:27:44 +01002005 /* After refill old buffer has to be unmapped regardless
2006 * the skb is successfully built or not.
2007 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02002008 dma_unmap_single(dev->dev.parent, phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002009 MVNETA_RX_BUF_SIZE(pp->pkt_size),
2010 DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002011
Marcin Wojtas26c17a172015-11-30 13:27:44 +01002012 if (!skb)
2013 goto err_drop_frame;
2014
willy tarreaudc4277d2014-01-16 08:20:07 +01002015 rcvd_pkts++;
2016 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002017
2018 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01002019 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002020 skb_put(skb, rx_bytes);
2021
2022 skb->protocol = eth_type_trans(skb, dev);
2023
willy tarreau54282132014-01-16 08:20:14 +01002024 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002025
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002026 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002027 }
2028
willy tarreaudc4277d2014-01-16 08:20:07 +01002029 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01002030 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2031
2032 u64_stats_update_begin(&stats->syncp);
2033 stats->rx_packets += rcvd_pkts;
2034 stats->rx_bytes += rcvd_bytes;
2035 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01002036 }
2037
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002038 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02002039 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002040
2041 return rx_done;
2042}
2043
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002044/* Main rx processing when using hardware buffer management */
2045static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2046 struct mvneta_rx_queue *rxq)
2047{
2048 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2049 struct net_device *dev = pp->dev;
2050 int rx_done;
2051 u32 rcvd_pkts = 0;
2052 u32 rcvd_bytes = 0;
2053
2054 /* Get number of received packets */
2055 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2056
2057 if (rx_todo > rx_done)
2058 rx_todo = rx_done;
2059
2060 rx_done = 0;
2061
2062 /* Fairness NAPI loop */
2063 while (rx_done < rx_todo) {
2064 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2065 struct mvneta_bm_pool *bm_pool = NULL;
2066 struct sk_buff *skb;
2067 unsigned char *data;
2068 dma_addr_t phys_addr;
2069 u32 rx_status, frag_size;
2070 int rx_bytes, err;
2071 u8 pool_id;
2072
2073 rx_done++;
2074 rx_status = rx_desc->status;
2075 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002076 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002077 phys_addr = rx_desc->buf_phys_addr;
2078 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2079 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2080
2081 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2082 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2083err_drop_frame_ret_pool:
2084 /* Return the buffer to the pool */
2085 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2086 rx_desc->buf_phys_addr);
2087err_drop_frame:
2088 dev->stats.rx_errors++;
2089 mvneta_rx_error(pp, rx_desc);
2090 /* leave the descriptor untouched */
2091 continue;
2092 }
2093
2094 if (rx_bytes <= rx_copybreak) {
2095 /* better copy a small frame and not unmap the DMA region */
2096 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2097 if (unlikely(!skb))
2098 goto err_drop_frame_ret_pool;
2099
2100 dma_sync_single_range_for_cpu(dev->dev.parent,
2101 rx_desc->buf_phys_addr,
2102 MVNETA_MH_SIZE + NET_SKB_PAD,
2103 rx_bytes,
2104 DMA_FROM_DEVICE);
2105 memcpy(skb_put(skb, rx_bytes),
2106 data + MVNETA_MH_SIZE + NET_SKB_PAD,
2107 rx_bytes);
2108
2109 skb->protocol = eth_type_trans(skb, dev);
2110 mvneta_rx_csum(pp, rx_status, skb);
2111 napi_gro_receive(&port->napi, skb);
2112
2113 rcvd_pkts++;
2114 rcvd_bytes += rx_bytes;
2115
2116 /* Return the buffer to the pool */
2117 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2118 rx_desc->buf_phys_addr);
2119
2120 /* leave the descriptor and buffer untouched */
2121 continue;
2122 }
2123
2124 /* Refill processing */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002125 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002126 if (err) {
2127 netdev_err(dev, "Linux processing - Can't refill\n");
2128 rxq->missed++;
2129 goto err_drop_frame_ret_pool;
2130 }
2131
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002132 frag_size = bm_pool->hwbm_pool.frag_size;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002133
2134 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2135
2136 /* After refill old buffer has to be unmapped regardless
2137 * the skb is successfully built or not.
2138 */
2139 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2140 bm_pool->buf_size, DMA_FROM_DEVICE);
2141 if (!skb)
2142 goto err_drop_frame;
2143
2144 rcvd_pkts++;
2145 rcvd_bytes += rx_bytes;
2146
2147 /* Linux processing */
2148 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2149 skb_put(skb, rx_bytes);
2150
2151 skb->protocol = eth_type_trans(skb, dev);
2152
2153 mvneta_rx_csum(pp, rx_status, skb);
2154
2155 napi_gro_receive(&port->napi, skb);
2156 }
2157
2158 if (rcvd_pkts) {
2159 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2160
2161 u64_stats_update_begin(&stats->syncp);
2162 stats->rx_packets += rcvd_pkts;
2163 stats->rx_bytes += rcvd_bytes;
2164 u64_stats_update_end(&stats->syncp);
2165 }
2166
2167 /* Update rxq management counters */
2168 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2169
2170 return rx_done;
2171}
2172
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002173static inline void
2174mvneta_tso_put_hdr(struct sk_buff *skb,
2175 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2176{
2177 struct mvneta_tx_desc *tx_desc;
2178 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2179
2180 txq->tx_skb[txq->txq_put_index] = NULL;
2181 tx_desc = mvneta_txq_next_desc_get(txq);
2182 tx_desc->data_size = hdr_len;
2183 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2184 tx_desc->command |= MVNETA_TXD_F_DESC;
2185 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2186 txq->txq_put_index * TSO_HEADER_SIZE;
2187 mvneta_txq_inc_put(txq);
2188}
2189
2190static inline int
2191mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2192 struct sk_buff *skb, char *data, int size,
2193 bool last_tcp, bool is_last)
2194{
2195 struct mvneta_tx_desc *tx_desc;
2196
2197 tx_desc = mvneta_txq_next_desc_get(txq);
2198 tx_desc->data_size = size;
2199 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2200 size, DMA_TO_DEVICE);
2201 if (unlikely(dma_mapping_error(dev->dev.parent,
2202 tx_desc->buf_phys_addr))) {
2203 mvneta_txq_desc_put(txq);
2204 return -ENOMEM;
2205 }
2206
2207 tx_desc->command = 0;
2208 txq->tx_skb[txq->txq_put_index] = NULL;
2209
2210 if (last_tcp) {
2211 /* last descriptor in the TCP packet */
2212 tx_desc->command = MVNETA_TXD_L_DESC;
2213
2214 /* last descriptor in SKB */
2215 if (is_last)
2216 txq->tx_skb[txq->txq_put_index] = skb;
2217 }
2218 mvneta_txq_inc_put(txq);
2219 return 0;
2220}
2221
2222static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2223 struct mvneta_tx_queue *txq)
2224{
2225 int total_len, data_left;
2226 int desc_count = 0;
2227 struct mvneta_port *pp = netdev_priv(dev);
2228 struct tso_t tso;
2229 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2230 int i;
2231
2232 /* Count needed descriptors */
2233 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2234 return 0;
2235
2236 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2237 pr_info("*** Is this even possible???!?!?\n");
2238 return 0;
2239 }
2240
2241 /* Initialize the TSO handler, and prepare the first payload */
2242 tso_start(skb, &tso);
2243
2244 total_len = skb->len - hdr_len;
2245 while (total_len > 0) {
2246 char *hdr;
2247
2248 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2249 total_len -= data_left;
2250 desc_count++;
2251
2252 /* prepare packet headers: MAC + IP + TCP */
2253 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2254 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2255
2256 mvneta_tso_put_hdr(skb, pp, txq);
2257
2258 while (data_left > 0) {
2259 int size;
2260 desc_count++;
2261
2262 size = min_t(int, tso.size, data_left);
2263
2264 if (mvneta_tso_put_data(dev, txq, skb,
2265 tso.data, size,
2266 size == data_left,
2267 total_len == 0))
2268 goto err_release;
2269 data_left -= size;
2270
2271 tso_build_data(skb, &tso, size);
2272 }
2273 }
2274
2275 return desc_count;
2276
2277err_release:
2278 /* Release all used data descriptors; header descriptors must not
2279 * be DMA-unmapped.
2280 */
2281 for (i = desc_count - 1; i >= 0; i--) {
2282 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002283 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002284 dma_unmap_single(pp->dev->dev.parent,
2285 tx_desc->buf_phys_addr,
2286 tx_desc->data_size,
2287 DMA_TO_DEVICE);
2288 mvneta_txq_desc_put(txq);
2289 }
2290 return 0;
2291}
2292
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002293/* Handle tx fragmentation processing */
2294static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2295 struct mvneta_tx_queue *txq)
2296{
2297 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002298 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002299
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002300 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002301 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2302 void *addr = page_address(frag->page.p) + frag->page_offset;
2303
2304 tx_desc = mvneta_txq_next_desc_get(txq);
2305 tx_desc->data_size = frag->size;
2306
2307 tx_desc->buf_phys_addr =
2308 dma_map_single(pp->dev->dev.parent, addr,
2309 tx_desc->data_size, DMA_TO_DEVICE);
2310
2311 if (dma_mapping_error(pp->dev->dev.parent,
2312 tx_desc->buf_phys_addr)) {
2313 mvneta_txq_desc_put(txq);
2314 goto error;
2315 }
2316
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002317 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002318 /* Last descriptor */
2319 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002320 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002321 } else {
2322 /* Descriptor in the middle: Not First, Not Last */
2323 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002324 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002325 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002326 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002327 }
2328
2329 return 0;
2330
2331error:
2332 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002333 * this packet, as well as the corresponding DMA mappings
2334 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002335 for (i = i - 1; i >= 0; i--) {
2336 tx_desc = txq->descs + i;
2337 dma_unmap_single(pp->dev->dev.parent,
2338 tx_desc->buf_phys_addr,
2339 tx_desc->data_size,
2340 DMA_TO_DEVICE);
2341 mvneta_txq_desc_put(txq);
2342 }
2343
2344 return -ENOMEM;
2345}
2346
2347/* Main tx processing */
2348static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2349{
2350 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002351 u16 txq_id = skb_get_queue_mapping(skb);
2352 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002353 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002354 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002355 int frags = 0;
2356 u32 tx_cmd;
2357
2358 if (!netif_running(dev))
2359 goto out;
2360
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002361 if (skb_is_gso(skb)) {
2362 frags = mvneta_tx_tso(skb, dev, txq);
2363 goto out;
2364 }
2365
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002366 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002367
2368 /* Get a descriptor for the first part of the packet */
2369 tx_desc = mvneta_txq_next_desc_get(txq);
2370
2371 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2372
2373 tx_desc->data_size = skb_headlen(skb);
2374
2375 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2376 tx_desc->data_size,
2377 DMA_TO_DEVICE);
2378 if (unlikely(dma_mapping_error(dev->dev.parent,
2379 tx_desc->buf_phys_addr))) {
2380 mvneta_txq_desc_put(txq);
2381 frags = 0;
2382 goto out;
2383 }
2384
2385 if (frags == 1) {
2386 /* First and Last descriptor */
2387 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2388 tx_desc->command = tx_cmd;
2389 txq->tx_skb[txq->txq_put_index] = skb;
2390 mvneta_txq_inc_put(txq);
2391 } else {
2392 /* First but not Last */
2393 tx_cmd |= MVNETA_TXD_F_DESC;
2394 txq->tx_skb[txq->txq_put_index] = NULL;
2395 mvneta_txq_inc_put(txq);
2396 tx_desc->command = tx_cmd;
2397 /* Continue with other skb fragments */
2398 if (mvneta_tx_frag_process(pp, skb, txq)) {
2399 dma_unmap_single(dev->dev.parent,
2400 tx_desc->buf_phys_addr,
2401 tx_desc->data_size,
2402 DMA_TO_DEVICE);
2403 mvneta_txq_desc_put(txq);
2404 frags = 0;
2405 goto out;
2406 }
2407 }
2408
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002409out:
2410 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01002411 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002412 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2413
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002414 netdev_tx_sent_queue(nq, len);
2415
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002416 txq->count += frags;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002417 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002418 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002419
Simon Guinot2a90f7e2017-01-16 18:08:31 +01002420 if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2421 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2422 mvneta_txq_pend_desc_add(pp, txq, frags);
2423 else
2424 txq->pending += frags;
2425
willy tarreau74c41b02014-01-16 08:20:08 +01002426 u64_stats_update_begin(&stats->syncp);
2427 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002428 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01002429 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002430 } else {
2431 dev->stats.tx_dropped++;
2432 dev_kfree_skb_any(skb);
2433 }
2434
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002435 return NETDEV_TX_OK;
2436}
2437
2438
2439/* Free tx resources, when resetting a port */
2440static void mvneta_txq_done_force(struct mvneta_port *pp,
2441 struct mvneta_tx_queue *txq)
2442
2443{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002444 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002445 int tx_done = txq->count;
2446
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002447 mvneta_txq_bufs_free(pp, txq, tx_done, nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002448
2449 /* reset txq */
2450 txq->count = 0;
2451 txq->txq_put_index = 0;
2452 txq->txq_get_index = 0;
2453}
2454
willy tarreau6c498972014-01-16 08:20:12 +01002455/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2456 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2457 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002458static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002459{
2460 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002461 struct netdev_queue *nq;
2462
willy tarreau6c498972014-01-16 08:20:12 +01002463 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002464 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002465
2466 nq = netdev_get_tx_queue(pp->dev, txq->id);
2467 __netif_tx_lock(nq, smp_processor_id());
2468
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002469 if (txq->count)
2470 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002471
2472 __netif_tx_unlock(nq);
2473 cause_tx_done &= ~((1 << txq->id));
2474 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002475}
2476
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002477/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002478 * according to hw spec, different than generic crc8 algorithm
2479 */
2480static int mvneta_addr_crc(unsigned char *addr)
2481{
2482 int crc = 0;
2483 int i;
2484
2485 for (i = 0; i < ETH_ALEN; i++) {
2486 int j;
2487
2488 crc = (crc ^ addr[i]) << 8;
2489 for (j = 7; j >= 0; j--) {
2490 if (crc & (0x100 << j))
2491 crc ^= 0x107 << j;
2492 }
2493 }
2494
2495 return crc;
2496}
2497
2498/* This method controls the net device special MAC multicast support.
2499 * The Special Multicast Table for MAC addresses supports MAC of the form
2500 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2501 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2502 * Table entries in the DA-Filter table. This method set the Special
2503 * Multicast Table appropriate entry.
2504 */
2505static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2506 unsigned char last_byte,
2507 int queue)
2508{
2509 unsigned int smc_table_reg;
2510 unsigned int tbl_offset;
2511 unsigned int reg_offset;
2512
2513 /* Register offset from SMC table base */
2514 tbl_offset = (last_byte / 4);
2515 /* Entry offset within the above reg */
2516 reg_offset = last_byte % 4;
2517
2518 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2519 + tbl_offset * 4));
2520
2521 if (queue == -1)
2522 smc_table_reg &= ~(0xff << (8 * reg_offset));
2523 else {
2524 smc_table_reg &= ~(0xff << (8 * reg_offset));
2525 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2526 }
2527
2528 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2529 smc_table_reg);
2530}
2531
2532/* This method controls the network device Other MAC multicast support.
2533 * The Other Multicast Table is used for multicast of another type.
2534 * A CRC-8 is used as an index to the Other Multicast Table entries
2535 * in the DA-Filter table.
2536 * The method gets the CRC-8 value from the calling routine and
2537 * sets the Other Multicast Table appropriate entry according to the
2538 * specified CRC-8 .
2539 */
2540static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2541 unsigned char crc8,
2542 int queue)
2543{
2544 unsigned int omc_table_reg;
2545 unsigned int tbl_offset;
2546 unsigned int reg_offset;
2547
2548 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2549 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2550
2551 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2552
2553 if (queue == -1) {
2554 /* Clear accepts frame bit at specified Other DA table entry */
2555 omc_table_reg &= ~(0xff << (8 * reg_offset));
2556 } else {
2557 omc_table_reg &= ~(0xff << (8 * reg_offset));
2558 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2559 }
2560
2561 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2562}
2563
2564/* The network device supports multicast using two tables:
2565 * 1) Special Multicast Table for MAC addresses of the form
2566 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2567 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2568 * Table entries in the DA-Filter table.
2569 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2570 * is used as an index to the Other Multicast Table entries in the
2571 * DA-Filter table.
2572 */
2573static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2574 int queue)
2575{
2576 unsigned char crc_result = 0;
2577
2578 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2579 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2580 return 0;
2581 }
2582
2583 crc_result = mvneta_addr_crc(p_addr);
2584 if (queue == -1) {
2585 if (pp->mcast_count[crc_result] == 0) {
2586 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2587 crc_result);
2588 return -EINVAL;
2589 }
2590
2591 pp->mcast_count[crc_result]--;
2592 if (pp->mcast_count[crc_result] != 0) {
2593 netdev_info(pp->dev,
2594 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2595 pp->mcast_count[crc_result], crc_result);
2596 return -EINVAL;
2597 }
2598 } else
2599 pp->mcast_count[crc_result]++;
2600
2601 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2602
2603 return 0;
2604}
2605
2606/* Configure Fitering mode of Ethernet port */
2607static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2608 int is_promisc)
2609{
2610 u32 port_cfg_reg, val;
2611
2612 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2613
2614 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2615
2616 /* Set / Clear UPM bit in port configuration register */
2617 if (is_promisc) {
2618 /* Accept all Unicast addresses */
2619 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2620 val |= MVNETA_FORCE_UNI;
2621 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2622 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2623 } else {
2624 /* Reject all Unicast addresses */
2625 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2626 val &= ~MVNETA_FORCE_UNI;
2627 }
2628
2629 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2630 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2631}
2632
2633/* register unicast and multicast addresses */
2634static void mvneta_set_rx_mode(struct net_device *dev)
2635{
2636 struct mvneta_port *pp = netdev_priv(dev);
2637 struct netdev_hw_addr *ha;
2638
2639 if (dev->flags & IFF_PROMISC) {
2640 /* Accept all: Multicast + Unicast */
2641 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002642 mvneta_set_ucast_table(pp, pp->rxq_def);
2643 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2644 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002645 } else {
2646 /* Accept single Unicast */
2647 mvneta_rx_unicast_promisc_set(pp, 0);
2648 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002649 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002650
2651 if (dev->flags & IFF_ALLMULTI) {
2652 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002653 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2654 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002655 } else {
2656 /* Accept only initialized multicast */
2657 mvneta_set_special_mcast_table(pp, -1);
2658 mvneta_set_other_mcast_table(pp, -1);
2659
2660 if (!netdev_mc_empty(dev)) {
2661 netdev_for_each_mc_addr(ha, dev) {
2662 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002663 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002664 }
2665 }
2666 }
2667 }
2668}
2669
2670/* Interrupt handling - the callback for request_irq() */
2671static irqreturn_t mvneta_isr(int irq, void *dev_id)
2672{
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002673 struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2674
2675 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2676 napi_schedule(&pp->napi);
2677
2678 return IRQ_HANDLED;
2679}
2680
2681/* Interrupt handling - the callback for request_percpu_irq() */
2682static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2683{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002684 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002685
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002686 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002687 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002688
2689 return IRQ_HANDLED;
2690}
2691
Stas Sergeev898b29702015-04-01 20:32:49 +03002692static int mvneta_fixed_link_update(struct mvneta_port *pp,
2693 struct phy_device *phy)
2694{
2695 struct fixed_phy_status status;
2696 struct fixed_phy_status changed = {};
2697 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2698
2699 status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2700 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2701 status.speed = SPEED_1000;
2702 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2703 status.speed = SPEED_100;
2704 else
2705 status.speed = SPEED_10;
2706 status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2707 changed.link = 1;
2708 changed.speed = 1;
2709 changed.duplex = 1;
2710 fixed_phy_update_state(phy, &status, &changed);
2711 return 0;
2712}
2713
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002714/* NAPI handler
2715 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2716 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2717 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2718 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2719 * Each CPU has its own causeRxTx register
2720 */
2721static int mvneta_poll(struct napi_struct *napi, int budget)
2722{
2723 int rx_done = 0;
2724 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002725 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002726 struct mvneta_port *pp = netdev_priv(napi->dev);
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002727 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002728 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002729
2730 if (!netif_running(pp->dev)) {
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002731 napi_complete(napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002732 return rx_done;
2733 }
2734
2735 /* Read cause register */
Stas Sergeev898b29702015-04-01 20:32:49 +03002736 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2737 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2738 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2739
2740 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2741 if (pp->use_inband_status && (cause_misc &
2742 (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2743 MVNETA_CAUSE_LINK_CHANGE |
2744 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002745 mvneta_fixed_link_update(pp, ndev->phydev);
Stas Sergeev898b29702015-04-01 20:32:49 +03002746 }
2747 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002748
2749 /* Release Tx descriptors */
2750 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002751 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002752 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2753 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002754
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002755 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002756 * RX packets
2757 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002758 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2759
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002760 cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2761 port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002762
2763 if (rx_queue) {
2764 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002765 if (pp->bm_priv)
2766 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2767 else
2768 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002769 }
2770
Eric Dumazet6ad20162017-01-30 08:22:01 -08002771 if (rx_done < budget) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002772 cause_rx_tx = 0;
Eric Dumazet6ad20162017-01-30 08:22:01 -08002773 napi_complete_done(napi, rx_done);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002774
2775 if (pp->neta_armada3700) {
2776 unsigned long flags;
2777
2778 local_irq_save(flags);
2779 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2780 MVNETA_RX_INTR_MASK(rxq_number) |
2781 MVNETA_TX_INTR_MASK(txq_number) |
2782 MVNETA_MISCINTR_INTR_MASK);
2783 local_irq_restore(flags);
2784 } else {
2785 enable_percpu_irq(pp->dev->irq, 0);
2786 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002787 }
2788
Marcin Wojtas2636ac32016-12-01 18:03:09 +01002789 if (pp->neta_armada3700)
2790 pp->cause_rx_tx = cause_rx_tx;
2791 else
2792 port->cause_rx_tx = cause_rx_tx;
2793
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002794 return rx_done;
2795}
2796
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002797/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2798static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2799 int num)
2800{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002801 int i;
2802
2803 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002804 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002805 if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002806 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002807 __func__, rxq->id, i, num);
2808 break;
2809 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002810 }
2811
2812 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002813 * get packets)
2814 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002815 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2816
2817 return i;
2818}
2819
2820/* Free all packets pending transmit from all TXQs and reset TX port */
2821static void mvneta_tx_reset(struct mvneta_port *pp)
2822{
2823 int queue;
2824
Ezequiel Garcia96728502014-05-22 20:06:59 -03002825 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002826 for (queue = 0; queue < txq_number; queue++)
2827 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2828
2829 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2830 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2831}
2832
2833static void mvneta_rx_reset(struct mvneta_port *pp)
2834{
2835 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2836 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2837}
2838
2839/* Rx/Tx queue initialization/cleanup methods */
2840
2841/* Create a specified RX queue */
2842static int mvneta_rxq_init(struct mvneta_port *pp,
2843 struct mvneta_rx_queue *rxq)
2844
2845{
2846 rxq->size = pp->rx_ring_size;
2847
2848 /* Allocate memory for RX descriptors */
2849 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2850 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2851 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002852 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002853 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002854
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002855 rxq->last_desc = rxq->size - 1;
2856
2857 /* Set Rx descriptors queue starting address */
2858 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2859 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2860
2861 /* Set Offset */
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01002862 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002863
2864 /* Set coalescing pkts and time */
2865 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2866 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2867
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002868 if (!pp->bm_priv) {
2869 /* Fill RXQ with buffers from RX pool */
2870 mvneta_rxq_buf_size_set(pp, rxq,
2871 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2872 mvneta_rxq_bm_disable(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002873 mvneta_rxq_fill(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002874 } else {
2875 mvneta_rxq_bm_enable(pp, rxq);
2876 mvneta_rxq_long_pool_set(pp, rxq);
2877 mvneta_rxq_short_pool_set(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002878 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002879 }
2880
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002881 return 0;
2882}
2883
2884/* Cleanup Rx queue */
2885static void mvneta_rxq_deinit(struct mvneta_port *pp,
2886 struct mvneta_rx_queue *rxq)
2887{
2888 mvneta_rxq_drop_pkts(pp, rxq);
2889
2890 if (rxq->descs)
2891 dma_free_coherent(pp->dev->dev.parent,
2892 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2893 rxq->descs,
2894 rxq->descs_phys);
2895
2896 rxq->descs = NULL;
2897 rxq->last_desc = 0;
2898 rxq->next_desc_to_proc = 0;
2899 rxq->descs_phys = 0;
2900}
2901
2902/* Create and initialize a tx queue */
2903static int mvneta_txq_init(struct mvneta_port *pp,
2904 struct mvneta_tx_queue *txq)
2905{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002906 int cpu;
2907
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002908 txq->size = pp->tx_ring_size;
2909
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002910 /* A queue must always have room for at least one skb.
2911 * Therefore, stop the queue when the free entries reaches
2912 * the maximum number of descriptors per skb.
2913 */
2914 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2915 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2916
2917
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002918 /* Allocate memory for TX descriptors */
2919 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2920 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2921 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002922 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002923 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002924
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002925 txq->last_desc = txq->size - 1;
2926
2927 /* Set maximum bandwidth for enabled TXQs */
2928 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2929 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2930
2931 /* Set Tx descriptors queue starting address */
2932 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2933 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2934
2935 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2936 if (txq->tx_skb == NULL) {
2937 dma_free_coherent(pp->dev->dev.parent,
2938 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2939 txq->descs, txq->descs_phys);
2940 return -ENOMEM;
2941 }
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002942
2943 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2944 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2945 txq->size * TSO_HEADER_SIZE,
2946 &txq->tso_hdrs_phys, GFP_KERNEL);
2947 if (txq->tso_hdrs == NULL) {
2948 kfree(txq->tx_skb);
2949 dma_free_coherent(pp->dev->dev.parent,
2950 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2951 txq->descs, txq->descs_phys);
2952 return -ENOMEM;
2953 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002954 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2955
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002956 /* Setup XPS mapping */
2957 if (txq_number > 1)
2958 cpu = txq->id % num_present_cpus();
2959 else
2960 cpu = pp->rxq_def % num_present_cpus();
2961 cpumask_set_cpu(cpu, &txq->affinity_mask);
2962 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2963
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002964 return 0;
2965}
2966
2967/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2968static void mvneta_txq_deinit(struct mvneta_port *pp,
2969 struct mvneta_tx_queue *txq)
2970{
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002971 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2972
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002973 kfree(txq->tx_skb);
2974
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002975 if (txq->tso_hdrs)
2976 dma_free_coherent(pp->dev->dev.parent,
2977 txq->size * TSO_HEADER_SIZE,
2978 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002979 if (txq->descs)
2980 dma_free_coherent(pp->dev->dev.parent,
2981 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2982 txq->descs, txq->descs_phys);
2983
Marcin Wojtasa29b6232017-01-16 18:08:32 +01002984 netdev_tx_reset_queue(nq);
2985
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002986 txq->descs = NULL;
2987 txq->last_desc = 0;
2988 txq->next_desc_to_proc = 0;
2989 txq->descs_phys = 0;
2990
2991 /* Set minimum bandwidth for disabled TXQs */
2992 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2993 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2994
2995 /* Set Tx descriptors queue starting address and size */
2996 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2997 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2998}
2999
3000/* Cleanup all Tx queues */
3001static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3002{
3003 int queue;
3004
3005 for (queue = 0; queue < txq_number; queue++)
3006 mvneta_txq_deinit(pp, &pp->txqs[queue]);
3007}
3008
3009/* Cleanup all Rx queues */
3010static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3011{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003012 int queue;
3013
3014 for (queue = 0; queue < txq_number; queue++)
3015 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003016}
3017
3018
3019/* Init all Rx queues */
3020static int mvneta_setup_rxqs(struct mvneta_port *pp)
3021{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003022 int queue;
3023
3024 for (queue = 0; queue < rxq_number; queue++) {
3025 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3026
3027 if (err) {
3028 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3029 __func__, queue);
3030 mvneta_cleanup_rxqs(pp);
3031 return err;
3032 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003033 }
3034
3035 return 0;
3036}
3037
3038/* Init all tx queues */
3039static int mvneta_setup_txqs(struct mvneta_port *pp)
3040{
3041 int queue;
3042
3043 for (queue = 0; queue < txq_number; queue++) {
3044 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3045 if (err) {
3046 netdev_err(pp->dev, "%s: can't create txq=%d\n",
3047 __func__, queue);
3048 mvneta_cleanup_txqs(pp);
3049 return err;
3050 }
3051 }
3052
3053 return 0;
3054}
3055
3056static void mvneta_start_dev(struct mvneta_port *pp)
3057{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003058 int cpu;
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003059 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003060
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003061 mvneta_max_rx_size_set(pp, pp->pkt_size);
3062 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3063
3064 /* start the Rx/Tx activity */
3065 mvneta_port_enable(pp);
3066
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003067 if (!pp->neta_armada3700) {
3068 /* Enable polling on the port */
3069 for_each_online_cpu(cpu) {
3070 struct mvneta_pcpu_port *port =
3071 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003072
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003073 napi_enable(&port->napi);
3074 }
3075 } else {
3076 napi_enable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003077 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003078
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003079 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003080 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3081
Stas Sergeev898b29702015-04-01 20:32:49 +03003082 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3083 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3084 MVNETA_CAUSE_LINK_CHANGE |
3085 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003086
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003087 phy_start(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003088 netif_tx_start_all_queues(pp->dev);
3089}
3090
3091static void mvneta_stop_dev(struct mvneta_port *pp)
3092{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003093 unsigned int cpu;
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003094 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003095
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003096 phy_stop(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003097
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003098 if (!pp->neta_armada3700) {
3099 for_each_online_cpu(cpu) {
3100 struct mvneta_pcpu_port *port =
3101 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003102
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003103 napi_disable(&port->napi);
3104 }
3105 } else {
3106 napi_disable(&pp->napi);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003107 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003108
3109 netif_carrier_off(pp->dev);
3110
3111 mvneta_port_down(pp);
3112 netif_tx_stop_all_queues(pp->dev);
3113
3114 /* Stop the port activity */
3115 mvneta_port_disable(pp);
3116
3117 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003118 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003119
3120 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003121 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003122
3123 mvneta_tx_reset(pp);
3124 mvneta_rx_reset(pp);
3125}
3126
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003127static void mvneta_percpu_enable(void *arg)
3128{
3129 struct mvneta_port *pp = arg;
3130
3131 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3132}
3133
3134static void mvneta_percpu_disable(void *arg)
3135{
3136 struct mvneta_port *pp = arg;
3137
3138 disable_percpu_irq(pp->dev->irq);
3139}
3140
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003141/* Change the device mtu */
3142static int mvneta_change_mtu(struct net_device *dev, int mtu)
3143{
3144 struct mvneta_port *pp = netdev_priv(dev);
3145 int ret;
3146
Jarod Wilson57779872016-10-17 15:54:06 -04003147 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3148 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3149 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3150 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3151 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003152
3153 dev->mtu = mtu;
3154
Simon Guinotb65657f2015-06-30 16:20:22 +02003155 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003156 if (pp->bm_priv)
3157 mvneta_bm_update_mtu(pp, mtu);
3158
Simon Guinotb65657f2015-06-30 16:20:22 +02003159 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003160 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003161 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003162
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003163 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003164 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003165 */
3166 mvneta_stop_dev(pp);
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003167 on_each_cpu(mvneta_percpu_disable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003168
3169 mvneta_cleanup_txqs(pp);
3170 mvneta_cleanup_rxqs(pp);
3171
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003172 if (pp->bm_priv)
3173 mvneta_bm_update_mtu(pp, mtu);
3174
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003175 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003176 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3177 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003178
3179 ret = mvneta_setup_rxqs(pp);
3180 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003181 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003182 return ret;
3183 }
3184
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003185 ret = mvneta_setup_txqs(pp);
3186 if (ret) {
3187 netdev_err(dev, "unable to setup txqs after MTU change\n");
3188 return ret;
3189 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003190
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003191 on_each_cpu(mvneta_percpu_enable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003192 mvneta_start_dev(pp);
3193 mvneta_port_up(pp);
3194
Simon Guinotb65657f2015-06-30 16:20:22 +02003195 netdev_update_features(dev);
3196
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003197 return 0;
3198}
3199
Simon Guinotb65657f2015-06-30 16:20:22 +02003200static netdev_features_t mvneta_fix_features(struct net_device *dev,
3201 netdev_features_t features)
3202{
3203 struct mvneta_port *pp = netdev_priv(dev);
3204
3205 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3206 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3207 netdev_info(dev,
3208 "Disable IP checksum for MTU greater than %dB\n",
3209 pp->tx_csum_limit);
3210 }
3211
3212 return features;
3213}
3214
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003215/* Get mac address */
3216static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3217{
3218 u32 mac_addr_l, mac_addr_h;
3219
3220 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3221 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3222 addr[0] = (mac_addr_h >> 24) & 0xFF;
3223 addr[1] = (mac_addr_h >> 16) & 0xFF;
3224 addr[2] = (mac_addr_h >> 8) & 0xFF;
3225 addr[3] = mac_addr_h & 0xFF;
3226 addr[4] = (mac_addr_l >> 8) & 0xFF;
3227 addr[5] = mac_addr_l & 0xFF;
3228}
3229
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003230/* Handle setting mac address */
3231static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3232{
3233 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003234 struct sockaddr *sockaddr = addr;
3235 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003236
Ezequiel Garciae68de362014-05-22 20:07:00 -03003237 ret = eth_prepare_mac_addr_change(dev, addr);
3238 if (ret < 0)
3239 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003240 /* Remove previous address table entry */
3241 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3242
3243 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003244 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003245
Ezequiel Garciae68de362014-05-22 20:07:00 -03003246 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003247 return 0;
3248}
3249
3250static void mvneta_adjust_link(struct net_device *ndev)
3251{
3252 struct mvneta_port *pp = netdev_priv(ndev);
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003253 struct phy_device *phydev = ndev->phydev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003254 int status_change = 0;
3255
3256 if (phydev->link) {
3257 if ((pp->speed != phydev->speed) ||
3258 (pp->duplex != phydev->duplex)) {
3259 u32 val;
3260
3261 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3262 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3263 MVNETA_GMAC_CONFIG_GMII_SPEED |
Stas Sergeev898b29702015-04-01 20:32:49 +03003264 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003265
3266 if (phydev->duplex)
3267 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3268
3269 if (phydev->speed == SPEED_1000)
3270 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni4d12bc62014-07-08 10:49:43 +02003271 else if (phydev->speed == SPEED_100)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003272 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3273
3274 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3275
3276 pp->duplex = phydev->duplex;
3277 pp->speed = phydev->speed;
3278 }
3279 }
3280
3281 if (phydev->link != pp->link) {
3282 if (!phydev->link) {
3283 pp->duplex = -1;
3284 pp->speed = 0;
3285 }
3286
3287 pp->link = phydev->link;
3288 status_change = 1;
3289 }
3290
3291 if (status_change) {
3292 if (phydev->link) {
Stas Sergeev898b29702015-04-01 20:32:49 +03003293 if (!pp->use_inband_status) {
3294 u32 val = mvreg_read(pp,
3295 MVNETA_GMAC_AUTONEG_CONFIG);
3296 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3297 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3298 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3299 val);
3300 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003301 mvneta_port_up(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003302 } else {
Stas Sergeev898b29702015-04-01 20:32:49 +03003303 if (!pp->use_inband_status) {
3304 u32 val = mvreg_read(pp,
3305 MVNETA_GMAC_AUTONEG_CONFIG);
3306 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3307 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3308 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3309 val);
3310 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003311 mvneta_port_down(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003312 }
Ezequiel Garcia0089b742014-10-31 12:57:20 -03003313 phy_print_status(phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003314 }
3315}
3316
3317static int mvneta_mdio_probe(struct mvneta_port *pp)
3318{
3319 struct phy_device *phy_dev;
3320
3321 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
3322 pp->phy_interface);
3323 if (!phy_dev) {
3324 netdev_err(pp->dev, "could not find the PHY\n");
3325 return -ENODEV;
3326 }
3327
3328 phy_dev->supported &= PHY_GBIT_FEATURES;
3329 phy_dev->advertising = phy_dev->supported;
3330
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003331 pp->link = 0;
3332 pp->duplex = 0;
3333 pp->speed = 0;
3334
3335 return 0;
3336}
3337
3338static void mvneta_mdio_remove(struct mvneta_port *pp)
3339{
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003340 struct net_device *ndev = pp->dev;
3341
3342 phy_disconnect(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003343}
3344
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003345/* Electing a CPU must be done in an atomic way: it should be done
3346 * after or before the removal/insertion of a CPU and this function is
3347 * not reentrant.
3348 */
Maxime Ripardf8642882015-09-25 18:09:38 +02003349static void mvneta_percpu_elect(struct mvneta_port *pp)
3350{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003351 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003352
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003353 /* Use the cpu associated to the rxq when it is online, in all
3354 * the other cases, use the cpu 0 which can't be offline.
3355 */
3356 if (cpu_online(pp->rxq_def))
3357 elected_cpu = pp->rxq_def;
3358
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003359 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02003360
3361 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003362 int rxq_map = 0, txq_map = 0;
3363 int rxq;
3364
3365 for (rxq = 0; rxq < rxq_number; rxq++)
3366 if ((rxq % max_cpu) == cpu)
3367 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3368
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003369 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003370 /* Map the default receive queue queue to the
3371 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02003372 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003373 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003374
3375 /* We update the TX queue map only if we have one
3376 * queue. In this case we associate the TX queue to
3377 * the CPU bound to the default RX queue
3378 */
3379 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003380 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003381 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3382 else
3383 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3384 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3385
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003386 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3387
3388 /* Update the interrupt mask on each CPU according the
3389 * new mapping
3390 */
3391 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3392 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003393 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003394
Maxime Ripardf8642882015-09-25 18:09:38 +02003395 }
3396};
3397
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003398static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
Maxime Ripardf8642882015-09-25 18:09:38 +02003399{
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003400 int other_cpu;
3401 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3402 node_online);
Maxime Ripardf8642882015-09-25 18:09:38 +02003403 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3404
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003405
3406 spin_lock(&pp->lock);
3407 /*
3408 * Configuring the driver for a new CPU while the driver is
3409 * stopping is racy, so just avoid it.
3410 */
3411 if (pp->is_stopped) {
3412 spin_unlock(&pp->lock);
3413 return 0;
3414 }
3415 netif_tx_stop_all_queues(pp->dev);
3416
3417 /*
3418 * We have to synchronise on tha napi of each CPU except the one
3419 * just being woken up
3420 */
3421 for_each_online_cpu(other_cpu) {
3422 if (other_cpu != cpu) {
3423 struct mvneta_pcpu_port *other_port =
3424 per_cpu_ptr(pp->ports, other_cpu);
3425
3426 napi_synchronize(&other_port->napi);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003427 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003428 }
3429
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003430 /* Mask all ethernet port interrupts */
3431 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3432 napi_enable(&port->napi);
3433
3434 /*
3435 * Enable per-CPU interrupts on the CPU that is
3436 * brought up.
3437 */
3438 mvneta_percpu_enable(pp);
3439
3440 /*
3441 * Enable per-CPU interrupt on the one CPU we care
3442 * about.
3443 */
3444 mvneta_percpu_elect(pp);
3445
3446 /* Unmask all ethernet port interrupts */
3447 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3448 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3449 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3450 MVNETA_CAUSE_LINK_CHANGE |
3451 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3452 netif_tx_start_all_queues(pp->dev);
3453 spin_unlock(&pp->lock);
3454 return 0;
3455}
3456
3457static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3458{
3459 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3460 node_online);
3461 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3462
3463 /*
3464 * Thanks to this lock we are sure that any pending cpu election is
3465 * done.
3466 */
3467 spin_lock(&pp->lock);
3468 /* Mask all ethernet port interrupts */
3469 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3470 spin_unlock(&pp->lock);
3471
3472 napi_synchronize(&port->napi);
3473 napi_disable(&port->napi);
3474 /* Disable per-CPU interrupts on the CPU that is brought down. */
3475 mvneta_percpu_disable(pp);
3476 return 0;
3477}
3478
3479static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3480{
3481 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3482 node_dead);
3483
3484 /* Check if a new CPU must be elected now this on is down */
3485 spin_lock(&pp->lock);
3486 mvneta_percpu_elect(pp);
3487 spin_unlock(&pp->lock);
3488 /* Unmask all ethernet port interrupts */
3489 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3490 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3491 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3492 MVNETA_CAUSE_LINK_CHANGE |
3493 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3494 netif_tx_start_all_queues(pp->dev);
3495 return 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003496}
3497
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003498static int mvneta_open(struct net_device *dev)
3499{
3500 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003501 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003502
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003503 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003504 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3505 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003506
3507 ret = mvneta_setup_rxqs(pp);
3508 if (ret)
3509 return ret;
3510
3511 ret = mvneta_setup_txqs(pp);
3512 if (ret)
3513 goto err_cleanup_rxqs;
3514
3515 /* Connect to port interrupt line */
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003516 if (pp->neta_armada3700)
3517 ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3518 dev->name, pp);
3519 else
3520 ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3521 dev->name, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003522 if (ret) {
3523 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3524 goto err_cleanup_txqs;
3525 }
3526
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003527 if (!pp->neta_armada3700) {
3528 /* Enable per-CPU interrupt on all the CPU to handle our RX
3529 * queue interrupts
3530 */
3531 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003532
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003533 pp->is_stopped = false;
3534 /* Register a CPU notifier to handle the case where our CPU
3535 * might be taken offline.
3536 */
3537 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3538 &pp->node_online);
3539 if (ret)
3540 goto err_free_irq;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003541
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003542 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3543 &pp->node_dead);
3544 if (ret)
3545 goto err_free_online_hp;
3546 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003547
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003548 /* In default link is down */
3549 netif_carrier_off(pp->dev);
3550
3551 ret = mvneta_mdio_probe(pp);
3552 if (ret < 0) {
3553 netdev_err(dev, "cannot probe MDIO bus\n");
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003554 goto err_free_dead_hp;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003555 }
3556
3557 mvneta_start_dev(pp);
3558
3559 return 0;
3560
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003561err_free_dead_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003562 if (!pp->neta_armada3700)
3563 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3564 &pp->node_dead);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003565err_free_online_hp:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003566 if (!pp->neta_armada3700)
3567 cpuhp_state_remove_instance_nocalls(online_hpstate,
3568 &pp->node_online);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003569err_free_irq:
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003570 if (pp->neta_armada3700) {
3571 free_irq(pp->dev->irq, pp);
3572 } else {
3573 on_each_cpu(mvneta_percpu_disable, pp, true);
3574 free_percpu_irq(pp->dev->irq, pp->ports);
3575 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003576err_cleanup_txqs:
3577 mvneta_cleanup_txqs(pp);
3578err_cleanup_rxqs:
3579 mvneta_cleanup_rxqs(pp);
3580 return ret;
3581}
3582
3583/* Stop the port, free port interrupt line */
3584static int mvneta_stop(struct net_device *dev)
3585{
3586 struct mvneta_port *pp = netdev_priv(dev);
3587
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003588 if (!pp->neta_armada3700) {
3589 /* Inform that we are stopping so we don't want to setup the
3590 * driver for new CPUs in the notifiers. The code of the
3591 * notifier for CPU online is protected by the same spinlock,
3592 * so when we get the lock, the notifer work is done.
3593 */
3594 spin_lock(&pp->lock);
3595 pp->is_stopped = true;
3596 spin_unlock(&pp->lock);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003597
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003598 mvneta_stop_dev(pp);
3599 mvneta_mdio_remove(pp);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003600
Dan Carpenterd26aac22016-12-07 14:32:17 +03003601 cpuhp_state_remove_instance_nocalls(online_hpstate,
3602 &pp->node_online);
3603 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3604 &pp->node_dead);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003605 on_each_cpu(mvneta_percpu_disable, pp, true);
3606 free_percpu_irq(dev->irq, pp->ports);
3607 } else {
3608 mvneta_stop_dev(pp);
3609 mvneta_mdio_remove(pp);
3610 free_irq(dev->irq, pp);
3611 }
3612
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003613 mvneta_cleanup_rxqs(pp);
3614 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003615
3616 return 0;
3617}
3618
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003619static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3620{
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003621 if (!dev->phydev)
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003622 return -ENOTSUPP;
3623
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003624 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003625}
3626
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003627/* Ethtool methods */
3628
Philippe Reynes013ad402016-07-30 17:42:12 +02003629/* Set link ksettings (phy address, speed) for ethtools */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +08003630static int
3631mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3632 const struct ethtool_link_ksettings *cmd)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003633{
Philippe Reynes013ad402016-07-30 17:42:12 +02003634 struct mvneta_port *pp = netdev_priv(ndev);
3635 struct phy_device *phydev = ndev->phydev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003636
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003637 if (!phydev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003638 return -ENODEV;
3639
Philippe Reynes013ad402016-07-30 17:42:12 +02003640 if ((cmd->base.autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003641 u32 val;
3642
Philippe Reynes013ad402016-07-30 17:42:12 +02003643 mvneta_set_autoneg(pp, cmd->base.autoneg == AUTONEG_ENABLE);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003644
Philippe Reynes013ad402016-07-30 17:42:12 +02003645 if (cmd->base.autoneg == AUTONEG_DISABLE) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003646 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3647 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3648 MVNETA_GMAC_CONFIG_GMII_SPEED |
3649 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3650
3651 if (phydev->duplex)
3652 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3653
3654 if (phydev->speed == SPEED_1000)
3655 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3656 else if (phydev->speed == SPEED_100)
3657 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3658
3659 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3660 }
3661
Philippe Reynes013ad402016-07-30 17:42:12 +02003662 pp->use_inband_status = (cmd->base.autoneg == AUTONEG_ENABLE);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003663 netdev_info(pp->dev, "autoneg status set to %i\n",
3664 pp->use_inband_status);
3665
Philippe Reynes013ad402016-07-30 17:42:12 +02003666 if (netif_running(ndev)) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003667 mvneta_port_down(pp);
3668 mvneta_port_up(pp);
3669 }
3670 }
3671
Philippe Reynes013ad402016-07-30 17:42:12 +02003672 return phy_ethtool_ksettings_set(ndev->phydev, cmd);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003673}
3674
3675/* Set interrupt coalescing for ethtools */
3676static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3677 struct ethtool_coalesce *c)
3678{
3679 struct mvneta_port *pp = netdev_priv(dev);
3680 int queue;
3681
3682 for (queue = 0; queue < rxq_number; queue++) {
3683 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3684 rxq->time_coal = c->rx_coalesce_usecs;
3685 rxq->pkts_coal = c->rx_max_coalesced_frames;
3686 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3687 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3688 }
3689
3690 for (queue = 0; queue < txq_number; queue++) {
3691 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3692 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3693 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3694 }
3695
3696 return 0;
3697}
3698
3699/* get coalescing for ethtools */
3700static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3701 struct ethtool_coalesce *c)
3702{
3703 struct mvneta_port *pp = netdev_priv(dev);
3704
3705 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3706 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3707
3708 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3709 return 0;
3710}
3711
3712
3713static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3714 struct ethtool_drvinfo *drvinfo)
3715{
3716 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3717 sizeof(drvinfo->driver));
3718 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3719 sizeof(drvinfo->version));
3720 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3721 sizeof(drvinfo->bus_info));
3722}
3723
3724
3725static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3726 struct ethtool_ringparam *ring)
3727{
3728 struct mvneta_port *pp = netdev_priv(netdev);
3729
3730 ring->rx_max_pending = MVNETA_MAX_RXD;
3731 ring->tx_max_pending = MVNETA_MAX_TXD;
3732 ring->rx_pending = pp->rx_ring_size;
3733 ring->tx_pending = pp->tx_ring_size;
3734}
3735
3736static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3737 struct ethtool_ringparam *ring)
3738{
3739 struct mvneta_port *pp = netdev_priv(dev);
3740
3741 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3742 return -EINVAL;
3743 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3744 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003745
3746 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3747 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3748 if (pp->tx_ring_size != ring->tx_pending)
3749 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3750 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003751
3752 if (netif_running(dev)) {
3753 mvneta_stop(dev);
3754 if (mvneta_open(dev)) {
3755 netdev_err(dev,
3756 "error on opening device after ring param change\n");
3757 return -ENOMEM;
3758 }
3759 }
3760
3761 return 0;
3762}
3763
Russell King9b0cdef2015-10-22 18:37:30 +01003764static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3765 u8 *data)
3766{
3767 if (sset == ETH_SS_STATS) {
3768 int i;
3769
3770 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3771 memcpy(data + i * ETH_GSTRING_LEN,
3772 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3773 }
3774}
3775
3776static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3777{
3778 const struct mvneta_statistic *s;
3779 void __iomem *base = pp->base;
3780 u32 high, low, val;
Jisheng Zhang2c832292016-01-20 16:36:25 +08003781 u64 val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003782 int i;
3783
3784 for (i = 0, s = mvneta_statistics;
3785 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3786 s++, i++) {
Russell King9b0cdef2015-10-22 18:37:30 +01003787 switch (s->type) {
3788 case T_REG_32:
3789 val = readl_relaxed(base + s->offset);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003790 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003791 break;
3792 case T_REG_64:
3793 /* Docs say to read low 32-bit then high */
3794 low = readl_relaxed(base + s->offset);
3795 high = readl_relaxed(base + s->offset + 4);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003796 val64 = (u64)high << 32 | low;
3797 pp->ethtool_stats[i] += val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003798 break;
3799 }
Russell King9b0cdef2015-10-22 18:37:30 +01003800 }
3801}
3802
3803static void mvneta_ethtool_get_stats(struct net_device *dev,
3804 struct ethtool_stats *stats, u64 *data)
3805{
3806 struct mvneta_port *pp = netdev_priv(dev);
3807 int i;
3808
3809 mvneta_ethtool_update_stats(pp);
3810
3811 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3812 *data++ = pp->ethtool_stats[i];
3813}
3814
3815static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3816{
3817 if (sset == ETH_SS_STATS)
3818 return ARRAY_SIZE(mvneta_statistics);
3819 return -EOPNOTSUPP;
3820}
3821
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003822static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3823{
3824 return MVNETA_RSS_LU_TABLE_SIZE;
3825}
3826
3827static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3828 struct ethtool_rxnfc *info,
3829 u32 *rules __always_unused)
3830{
3831 switch (info->cmd) {
3832 case ETHTOOL_GRXRINGS:
3833 info->data = rxq_number;
3834 return 0;
3835 case ETHTOOL_GRXFH:
3836 return -EOPNOTSUPP;
3837 default:
3838 return -EOPNOTSUPP;
3839 }
3840}
3841
3842static int mvneta_config_rss(struct mvneta_port *pp)
3843{
3844 int cpu;
3845 u32 val;
3846
3847 netif_tx_stop_all_queues(pp->dev);
3848
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003849 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003850
3851 /* We have to synchronise on the napi of each CPU */
3852 for_each_online_cpu(cpu) {
3853 struct mvneta_pcpu_port *pcpu_port =
3854 per_cpu_ptr(pp->ports, cpu);
3855
3856 napi_synchronize(&pcpu_port->napi);
3857 napi_disable(&pcpu_port->napi);
3858 }
3859
3860 pp->rxq_def = pp->indir[0];
3861
3862 /* Update unicast mapping */
3863 mvneta_set_rx_mode(pp->dev);
3864
3865 /* Update val of portCfg register accordingly with all RxQueue types */
3866 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3867 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3868
3869 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003870 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003871 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003872 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003873
3874 /* We have to synchronise on the napi of each CPU */
3875 for_each_online_cpu(cpu) {
3876 struct mvneta_pcpu_port *pcpu_port =
3877 per_cpu_ptr(pp->ports, cpu);
3878
3879 napi_enable(&pcpu_port->napi);
3880 }
3881
3882 netif_tx_start_all_queues(pp->dev);
3883
3884 return 0;
3885}
3886
3887static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3888 const u8 *key, const u8 hfunc)
3889{
3890 struct mvneta_port *pp = netdev_priv(dev);
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003891
3892 /* Current code for Armada 3700 doesn't support RSS features yet */
3893 if (pp->neta_armada3700)
3894 return -EOPNOTSUPP;
3895
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003896 /* We require at least one supported parameter to be changed
3897 * and no change in any of the unsupported parameters
3898 */
3899 if (key ||
3900 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3901 return -EOPNOTSUPP;
3902
3903 if (!indir)
3904 return 0;
3905
3906 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3907
3908 return mvneta_config_rss(pp);
3909}
3910
3911static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3912 u8 *hfunc)
3913{
3914 struct mvneta_port *pp = netdev_priv(dev);
3915
Marcin Wojtas2636ac32016-12-01 18:03:09 +01003916 /* Current code for Armada 3700 doesn't support RSS features yet */
3917 if (pp->neta_armada3700)
3918 return -EOPNOTSUPP;
3919
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003920 if (hfunc)
3921 *hfunc = ETH_RSS_HASH_TOP;
3922
3923 if (!indir)
3924 return 0;
3925
3926 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3927
3928 return 0;
3929}
3930
Jingju Houb60a00f2017-02-06 14:58:13 +08003931static void mvneta_ethtool_get_wol(struct net_device *dev,
3932 struct ethtool_wolinfo *wol)
3933{
3934 wol->supported = 0;
3935 wol->wolopts = 0;
3936
3937 if (dev->phydev)
3938 phy_ethtool_get_wol(dev->phydev, wol);
3939}
3940
3941static int mvneta_ethtool_set_wol(struct net_device *dev,
3942 struct ethtool_wolinfo *wol)
3943{
3944 if (!dev->phydev)
3945 return -EOPNOTSUPP;
3946
3947 return phy_ethtool_set_wol(dev->phydev, wol);
3948}
3949
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003950static const struct net_device_ops mvneta_netdev_ops = {
3951 .ndo_open = mvneta_open,
3952 .ndo_stop = mvneta_stop,
3953 .ndo_start_xmit = mvneta_tx,
3954 .ndo_set_rx_mode = mvneta_set_rx_mode,
3955 .ndo_set_mac_address = mvneta_set_mac_addr,
3956 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02003957 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003958 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003959 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003960};
3961
Jisheng Zhang4581be42017-02-16 17:07:39 +08003962static const struct ethtool_ops mvneta_eth_tool_ops = {
Florian Fainelli5489ee82016-11-15 11:19:47 -08003963 .nway_reset = phy_ethtool_nway_reset,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003964 .get_link = ethtool_op_get_link,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003965 .set_coalesce = mvneta_ethtool_set_coalesce,
3966 .get_coalesce = mvneta_ethtool_get_coalesce,
3967 .get_drvinfo = mvneta_ethtool_get_drvinfo,
3968 .get_ringparam = mvneta_ethtool_get_ringparam,
3969 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King9b0cdef2015-10-22 18:37:30 +01003970 .get_strings = mvneta_ethtool_get_strings,
3971 .get_ethtool_stats = mvneta_ethtool_get_stats,
3972 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003973 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
3974 .get_rxnfc = mvneta_ethtool_get_rxnfc,
3975 .get_rxfh = mvneta_ethtool_get_rxfh,
3976 .set_rxfh = mvneta_ethtool_set_rxfh,
Philippe Reynes013ad402016-07-30 17:42:12 +02003977 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3978 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
Jingju Houb60a00f2017-02-06 14:58:13 +08003979 .get_wol = mvneta_ethtool_get_wol,
3980 .set_wol = mvneta_ethtool_set_wol,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003981};
3982
3983/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03003984static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003985{
3986 int queue;
3987
3988 /* Disable port */
3989 mvneta_port_disable(pp);
3990
3991 /* Set port default values */
3992 mvneta_defaults_set(pp);
3993
Ezequiel Garcia96728502014-05-22 20:06:59 -03003994 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
3995 GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003996 if (!pp->txqs)
3997 return -ENOMEM;
3998
3999 /* Initialize TX descriptor rings */
4000 for (queue = 0; queue < txq_number; queue++) {
4001 struct mvneta_tx_queue *txq = &pp->txqs[queue];
4002 txq->id = queue;
4003 txq->size = pp->tx_ring_size;
4004 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4005 }
4006
Ezequiel Garcia96728502014-05-22 20:06:59 -03004007 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
4008 GFP_KERNEL);
4009 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004010 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004011
4012 /* Create Rx descriptor rings */
4013 for (queue = 0; queue < rxq_number; queue++) {
4014 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4015 rxq->id = queue;
4016 rxq->size = pp->rx_ring_size;
4017 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4018 rxq->time_coal = MVNETA_RX_COAL_USEC;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01004019 rxq->buf_virt_addr = devm_kmalloc(pp->dev->dev.parent,
4020 rxq->size * sizeof(void *),
4021 GFP_KERNEL);
4022 if (!rxq->buf_virt_addr)
4023 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004024 }
4025
4026 return 0;
4027}
4028
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004029/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00004030static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4031 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004032{
4033 u32 win_enable;
4034 u32 win_protect;
4035 int i;
4036
4037 for (i = 0; i < 6; i++) {
4038 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4039 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4040
4041 if (i < 4)
4042 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4043 }
4044
4045 win_enable = 0x3f;
4046 win_protect = 0;
4047
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004048 if (dram) {
4049 for (i = 0; i < dram->num_cs; i++) {
4050 const struct mbus_dram_window *cs = dram->cs + i;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004051
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004052 mvreg_write(pp, MVNETA_WIN_BASE(i),
4053 (cs->base & 0xffff0000) |
4054 (cs->mbus_attr << 8) |
4055 dram->mbus_dram_target_id);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004056
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004057 mvreg_write(pp, MVNETA_WIN_SIZE(i),
4058 (cs->size - 1) & 0xffff0000);
4059
4060 win_enable &= ~(1 << i);
4061 win_protect |= 3 << (2 * i);
4062 }
4063 } else {
4064 /* For Armada3700 open default 4GB Mbus window, leaving
4065 * arbitration of target/attribute to a different layer
4066 * of configuration.
4067 */
4068 mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4069 win_enable &= ~BIT(0);
4070 win_protect = 3;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004071 }
4072
4073 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01004074 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004075}
4076
4077/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004078static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004079{
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004080 u32 ctrl;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004081
4082 /* MAC Cause register should be cleared */
4083 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4084
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004085 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004086
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004087 /* Even though it might look weird, when we're configured in
4088 * SGMII or QSGMII mode, the RGMII bit needs to be set.
4089 */
4090 switch(phy_mode) {
4091 case PHY_INTERFACE_MODE_QSGMII:
4092 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
4093 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
4094 break;
4095 case PHY_INTERFACE_MODE_SGMII:
4096 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
4097 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
4098 break;
4099 case PHY_INTERFACE_MODE_RGMII:
4100 case PHY_INTERFACE_MODE_RGMII_ID:
4101 ctrl |= MVNETA_GMAC2_PORT_RGMII;
4102 break;
4103 default:
4104 return -EINVAL;
4105 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004106
4107 /* Cancel Port Reset */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004108 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
4109 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004110
4111 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
4112 MVNETA_GMAC2_PORT_RESET) != 0)
4113 continue;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004114
4115 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004116}
4117
4118/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00004119static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004120{
4121 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004122 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004123 struct device_node *dn = pdev->dev.of_node;
4124 struct device_node *phy_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004125 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004126 struct mvneta_port *pp;
4127 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004128 const char *dt_mac_addr;
4129 char hw_mac_addr[ETH_ALEN];
4130 const char *mac_from;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07004131 const char *managed;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004132 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004133 int phy_mode;
4134 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004135 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004136
Willy Tarreauee40a112013-04-11 23:00:37 +02004137 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004138 if (!dev)
4139 return -ENOMEM;
4140
4141 dev->irq = irq_of_parse_and_map(dn, 0);
4142 if (dev->irq == 0) {
4143 err = -EINVAL;
4144 goto err_free_netdev;
4145 }
4146
4147 phy_node = of_parse_phandle(dn, "phy", 0);
4148 if (!phy_node) {
Thomas Petazzoni83895be2014-05-16 16:14:06 +02004149 if (!of_phy_is_fixed_link(dn)) {
4150 dev_err(&pdev->dev, "no PHY specified\n");
4151 err = -ENODEV;
4152 goto err_free_irq;
4153 }
4154
4155 err = of_phy_register_fixed_link(dn);
4156 if (err < 0) {
4157 dev_err(&pdev->dev, "cannot register fixed PHY\n");
4158 goto err_free_irq;
4159 }
4160
4161 /* In the case of a fixed PHY, the DT node associated
4162 * to the PHY is the Ethernet MAC DT node.
4163 */
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004164 phy_node = of_node_get(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004165 }
4166
4167 phy_mode = of_get_phy_mode(dn);
4168 if (phy_mode < 0) {
4169 dev_err(&pdev->dev, "incorrect phy-mode\n");
4170 err = -EINVAL;
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004171 goto err_put_phy_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004172 }
4173
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004174 dev->tx_queue_len = MVNETA_MAX_TXD;
4175 dev->watchdog_timeo = 5 * HZ;
4176 dev->netdev_ops = &mvneta_netdev_ops;
4177
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004178 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004179
4180 pp = netdev_priv(dev);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01004181 spin_lock_init(&pp->lock);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004182 pp->phy_node = phy_node;
4183 pp->phy_interface = phy_mode;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07004184
4185 err = of_property_read_string(dn, "managed", &managed);
4186 pp->use_inband_status = (err == 0 &&
4187 strcmp(managed, "in-band-status") == 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004188
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01004189 pp->rxq_def = rxq_def;
4190
Marcin Wojtas8d5047c2016-12-01 18:03:07 +01004191 /* Set RX packet offset correction for platforms, whose
4192 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4193 * platforms and 0B for 32-bit ones.
4194 */
4195 pp->rx_offset_correction =
4196 max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
4197
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004198 pp->indir[0] = rxq_def;
4199
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004200 /* Get special SoC configurations */
4201 if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4202 pp->neta_armada3700 = true;
4203
Jisheng Zhang2804ba42016-01-20 19:27:23 +08004204 pp->clk = devm_clk_get(&pdev->dev, "core");
4205 if (IS_ERR(pp->clk))
4206 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004207 if (IS_ERR(pp->clk)) {
4208 err = PTR_ERR(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004209 goto err_put_phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004210 }
4211
4212 clk_prepare_enable(pp->clk);
4213
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004214 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4215 if (!IS_ERR(pp->clk_bus))
4216 clk_prepare_enable(pp->clk_bus);
4217
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004218 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4219 pp->base = devm_ioremap_resource(&pdev->dev, res);
4220 if (IS_ERR(pp->base)) {
4221 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004222 goto err_clk;
4223 }
4224
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004225 /* Alloc per-cpu port structure */
4226 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4227 if (!pp->ports) {
4228 err = -ENOMEM;
4229 goto err_clk;
4230 }
4231
willy tarreau74c41b02014-01-16 08:20:08 +01004232 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08004233 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01004234 if (!pp->stats) {
4235 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004236 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01004237 }
4238
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004239 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01004240 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004241 mac_from = "device tree";
4242 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4243 } else {
4244 mvneta_get_mac_addr(pp, hw_mac_addr);
4245 if (is_valid_ether_addr(hw_mac_addr)) {
4246 mac_from = "hardware";
4247 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4248 } else {
4249 mac_from = "random";
4250 eth_hw_addr_random(dev);
4251 }
4252 }
4253
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004254 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4255 if (tx_csum_limit < 0 ||
4256 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4257 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4258 dev_info(&pdev->dev,
4259 "Wrong TX csum limit in DT, set to %dB\n",
4260 MVNETA_TX_CSUM_DEF_SIZE);
4261 }
4262 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4263 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4264 } else {
4265 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4266 }
4267
4268 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02004269
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004270 dram_target_info = mv_mbus_dram_info();
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004271 /* Armada3700 requires setting default configuration of Mbus
4272 * windows, however without using filled mbus_dram_target_info
4273 * structure.
4274 */
4275 if (dram_target_info || pp->neta_armada3700)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004276 mvneta_conf_mbus_windows(pp, dram_target_info);
4277
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004278 pp->tx_ring_size = MVNETA_MAX_TXD;
4279 pp->rx_ring_size = MVNETA_MAX_RXD;
4280
4281 pp->dev = dev;
4282 SET_NETDEV_DEV(dev, &pdev->dev);
4283
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004284 pp->id = global_port_id++;
4285
4286 /* Obtain access to BM resources if enabled and already initialized */
4287 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4288 if (bm_node && bm_node->data) {
4289 pp->bm_priv = bm_node->data;
4290 err = mvneta_bm_port_init(pdev, pp);
4291 if (err < 0) {
4292 dev_info(&pdev->dev, "use SW buffer management\n");
4293 pp->bm_priv = NULL;
4294 }
4295 }
Peter Chend4e4da02016-08-01 15:02:36 +08004296 of_node_put(bm_node);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004297
Ezequiel Garcia96728502014-05-22 20:06:59 -03004298 err = mvneta_init(&pdev->dev, pp);
4299 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004300 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004301
4302 err = mvneta_port_power_up(pp, phy_mode);
4303 if (err < 0) {
4304 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004305 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004306 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004307
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004308 /* Armada3700 network controller does not support per-cpu
4309 * operation, so only single NAPI should be initialized.
4310 */
4311 if (pp->neta_armada3700) {
4312 netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4313 } else {
4314 for_each_present_cpu(cpu) {
4315 struct mvneta_pcpu_port *port =
4316 per_cpu_ptr(pp->ports, cpu);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004317
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004318 netif_napi_add(dev, &port->napi, mvneta_poll,
4319 NAPI_POLL_WEIGHT);
4320 port->pp = pp;
4321 }
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004322 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004323
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03004324 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03004325 dev->hw_features |= dev->features;
4326 dev->vlan_features |= dev->features;
Andrew Lunn97db8af2016-11-24 00:08:13 +01004327 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004328 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00004329
Jarod Wilson57779872016-10-17 15:54:06 -04004330 /* MTU range: 68 - 9676 */
4331 dev->min_mtu = ETH_MIN_MTU;
4332 /* 9676 == 9700 - 20 and rounding to 8 */
4333 dev->max_mtu = 9676;
4334
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004335 err = register_netdev(dev);
4336 if (err < 0) {
4337 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03004338 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004339 }
4340
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004341 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4342 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004343
4344 platform_set_drvdata(pdev, pp->dev);
4345
Stas Sergeev898b29702015-04-01 20:32:49 +03004346 if (pp->use_inband_status) {
4347 struct phy_device *phy = of_phy_find_device(dn);
4348
4349 mvneta_fixed_link_update(pp, phy);
Russell King04d53b22015-09-24 20:36:18 +01004350
Andrew Lunne5a03bf2016-01-06 20:11:16 +01004351 put_device(&phy->mdio.dev);
Stas Sergeev898b29702015-04-01 20:32:49 +03004352 }
4353
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004354 return 0;
4355
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004356err_netdev:
4357 unregister_netdev(dev);
4358 if (pp->bm_priv) {
4359 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4360 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4361 1 << pp->id);
4362 }
willy tarreau74c41b02014-01-16 08:20:08 +01004363err_free_stats:
4364 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004365err_free_ports:
4366 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004367err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004368 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004369 clk_disable_unprepare(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004370err_put_phy_node:
4371 of_node_put(phy_node);
Johan Hovold5a57a302016-11-28 19:25:04 +01004372 if (of_phy_is_fixed_link(dn))
4373 of_phy_deregister_fixed_link(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004374err_free_irq:
4375 irq_dispose_mapping(dev->irq);
4376err_free_netdev:
4377 free_netdev(dev);
4378 return err;
4379}
4380
4381/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00004382static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004383{
4384 struct net_device *dev = platform_get_drvdata(pdev);
Johan Hovold5a57a302016-11-28 19:25:04 +01004385 struct device_node *dn = pdev->dev.of_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004386 struct mvneta_port *pp = netdev_priv(dev);
4387
4388 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004389 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004390 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004391 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01004392 free_percpu(pp->stats);
Johan Hovold5a57a302016-11-28 19:25:04 +01004393 if (of_phy_is_fixed_link(dn))
4394 of_phy_deregister_fixed_link(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004395 irq_dispose_mapping(dev->irq);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004396 of_node_put(pp->phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004397 free_netdev(dev);
4398
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004399 if (pp->bm_priv) {
4400 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4401 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4402 1 << pp->id);
4403 }
4404
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004405 return 0;
4406}
4407
4408static const struct of_device_id mvneta_match[] = {
4409 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02004410 { .compatible = "marvell,armada-xp-neta" },
Marcin Wojtas2636ac32016-12-01 18:03:09 +01004411 { .compatible = "marvell,armada-3700-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004412 { }
4413};
4414MODULE_DEVICE_TABLE(of, mvneta_match);
4415
4416static struct platform_driver mvneta_driver = {
4417 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00004418 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004419 .driver = {
4420 .name = MVNETA_DRIVER_NAME,
4421 .of_match_table = mvneta_match,
4422 },
4423};
4424
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004425static int __init mvneta_driver_init(void)
4426{
4427 int ret;
4428
4429 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4430 mvneta_cpu_online,
4431 mvneta_cpu_down_prepare);
4432 if (ret < 0)
4433 goto out;
4434 online_hpstate = ret;
4435 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4436 NULL, mvneta_cpu_dead);
4437 if (ret)
4438 goto err_dead;
4439
4440 ret = platform_driver_register(&mvneta_driver);
4441 if (ret)
4442 goto err;
4443 return 0;
4444
4445err:
4446 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4447err_dead:
4448 cpuhp_remove_multi_state(online_hpstate);
4449out:
4450 return ret;
4451}
4452module_init(mvneta_driver_init);
4453
4454static void __exit mvneta_driver_exit(void)
4455{
4456 platform_driver_unregister(&mvneta_driver);
4457 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4458 cpuhp_remove_multi_state(online_hpstate);
4459}
4460module_exit(mvneta_driver_exit);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004461
4462MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4463MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4464MODULE_LICENSE("GPL");
4465
4466module_param(rxq_number, int, S_IRUGO);
4467module_param(txq_number, int, S_IRUGO);
4468
4469module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01004470module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);