blob: bb6bc84995a2f43ef5f1b616538e05208861d16b [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 Kinga418b2f2017-10-07 22:36:49 +000031#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 Sergeev898b2972015-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 Sergeev898b2972015-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 Sergeev898b2972015-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 Sergeev898b2972015-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 Sergeev898b2972015-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
228#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
229#define MVNETA_TXQ_SENT_DESC_SHIFT 16
230#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
231#define MVNETA_PORT_TX_RESET 0x3cf0
232#define MVNETA_PORT_TX_DMA_RESET BIT(0)
233#define MVNETA_TX_MTU 0x3e0c
234#define MVNETA_TX_TOKEN_SIZE 0x3e14
235#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
236#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
237#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
238
239#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
240
241/* Descriptor ring Macros */
242#define MVNETA_QUEUE_NEXT_DESC(q, index) \
243 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
244
245/* Various constants */
246
247/* Coalescing */
Dmitri Epshtein06708f82016-07-06 04:18:58 +0200248#define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300249#define MVNETA_RX_COAL_PKTS 32
250#define MVNETA_RX_COAL_USEC 100
251
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100252/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300253 * by Marvell switches when a specific hardware mode is enabled (not
254 * supported by this driver) or is filled automatically by zeroes on
255 * the RX side. Those two bytes being at the front of the Ethernet
256 * header, they allow to have the IP header aligned on a 4 bytes
257 * boundary automatically: the hardware skips those two bytes on its
258 * own.
259 */
260#define MVNETA_MH_SIZE 2
261
262#define MVNETA_VLAN_TAG_LEN 4
263
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100264#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300265#define MVNETA_TX_CSUM_MAX_SIZE 9800
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100266#define MVNETA_ACC_MODE_EXT1 1
267#define MVNETA_ACC_MODE_EXT2 2
268
269#define MVNETA_MAX_DECODE_WIN 6
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300270
271/* Timeout constants */
272#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
273#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
274#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
275
276#define MVNETA_TX_MTU_MAX 0x3ffff
277
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100278/* The RSS lookup table actually has 256 entries but we do not use
279 * them yet
280 */
281#define MVNETA_RSS_LU_TABLE_SIZE 1
282
Ezequiel Garcia2adb7192014-05-19 13:59:55 -0300283/* TSO header size */
284#define TSO_HEADER_SIZE 128
285
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300286/* Max number of Rx descriptors */
287#define MVNETA_MAX_RXD 128
288
289/* Max number of Tx descriptors */
290#define MVNETA_MAX_TXD 532
291
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300292/* Max number of allowed TCP segments for software TSO */
293#define MVNETA_MAX_TSO_SEGS 100
294
295#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
296
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300297/* descriptor aligned size */
298#define MVNETA_DESC_ALIGNED_SIZE 32
299
300#define MVNETA_RX_PKT_SIZE(mtu) \
301 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
302 ETH_HLEN + ETH_FCS_LEN, \
Jisheng Zhangc66e98c2016-04-01 17:12:49 +0800303 cache_line_size())
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300304
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300305#define IS_TSO_HEADER(txq, addr) \
306 ((addr >= txq->tso_hdrs_phys) && \
307 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
308
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100309#define MVNETA_RX_GET_BM_POOL_ID(rxd) \
310 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300311
Russell King9b0cdef2015-10-22 18:37:30 +0100312struct mvneta_statistic {
313 unsigned short offset;
314 unsigned short type;
315 const char name[ETH_GSTRING_LEN];
316};
317
318#define T_REG_32 32
319#define T_REG_64 64
320
321static const struct mvneta_statistic mvneta_statistics[] = {
322 { 0x3000, T_REG_64, "good_octets_received", },
323 { 0x3010, T_REG_32, "good_frames_received", },
324 { 0x3008, T_REG_32, "bad_octets_received", },
325 { 0x3014, T_REG_32, "bad_frames_received", },
326 { 0x3018, T_REG_32, "broadcast_frames_received", },
327 { 0x301c, T_REG_32, "multicast_frames_received", },
328 { 0x3050, T_REG_32, "unrec_mac_control_received", },
329 { 0x3058, T_REG_32, "good_fc_received", },
330 { 0x305c, T_REG_32, "bad_fc_received", },
331 { 0x3060, T_REG_32, "undersize_received", },
332 { 0x3064, T_REG_32, "fragments_received", },
333 { 0x3068, T_REG_32, "oversize_received", },
334 { 0x306c, T_REG_32, "jabber_received", },
335 { 0x3070, T_REG_32, "mac_receive_error", },
336 { 0x3074, T_REG_32, "bad_crc_event", },
337 { 0x3078, T_REG_32, "collision", },
338 { 0x307c, T_REG_32, "late_collision", },
339 { 0x2484, T_REG_32, "rx_discard", },
340 { 0x2488, T_REG_32, "rx_overrun", },
341 { 0x3020, T_REG_32, "frames_64_octets", },
342 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
343 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
344 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
345 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
346 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
347 { 0x3038, T_REG_64, "good_octets_sent", },
348 { 0x3040, T_REG_32, "good_frames_sent", },
349 { 0x3044, T_REG_32, "excessive_collision", },
350 { 0x3048, T_REG_32, "multicast_frames_sent", },
351 { 0x304c, T_REG_32, "broadcast_frames_sent", },
352 { 0x3054, T_REG_32, "fc_sent", },
353 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
354};
355
willy tarreau74c41b02014-01-16 08:20:08 +0100356struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300357 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100358 u64 rx_packets;
359 u64 rx_bytes;
360 u64 tx_packets;
361 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300362};
363
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200364struct mvneta_pcpu_port {
365 /* Pointer to the shared port */
366 struct mvneta_port *pp;
367
368 /* Pointer to the CPU-local NAPI struct */
369 struct napi_struct napi;
370
371 /* Cause of the previous interrupt */
372 u32 cause_rx_tx;
373};
374
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300375struct mvneta_port {
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100376 u8 id;
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200377 struct mvneta_pcpu_port __percpu *ports;
378 struct mvneta_pcpu_stats __percpu *stats;
379
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300380 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100381 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300382 void __iomem *base;
383 struct mvneta_rx_queue *rxqs;
384 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300385 struct net_device *dev;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200386 struct hlist_node node_online;
387 struct hlist_node node_dead;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100388 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100389 /* Protect the access to the percpu interrupt registers,
390 * ensuring that the configuration remains coherent.
391 */
392 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100393 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300394
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300395 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100396 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800397 /* AXI clock */
398 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300399 u8 mcast_count[256];
400 u16 tx_ring_size;
401 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300402
403 struct mii_bus *mii_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300404 phy_interface_t phy_interface;
405 struct device_node *phy_node;
406 unsigned int link;
407 unsigned int duplex;
408 unsigned int speed;
Simon Guinotb65657f2015-06-30 16:20:22 +0200409 unsigned int tx_csum_limit;
Stas Sergeev0c0744f2015-12-02 20:35:11 +0300410 unsigned int use_inband_status:1;
Russell King9b0cdef2015-10-22 18:37:30 +0100411
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100412 struct mvneta_bm *bm_priv;
413 struct mvneta_bm_pool *pool_long;
414 struct mvneta_bm_pool *pool_short;
415 int bm_win_id;
416
Russell King9b0cdef2015-10-22 18:37:30 +0100417 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100418
419 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300420};
421
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100422/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300423 * layout of the transmit and reception DMA descriptors, and their
424 * layout is therefore defined by the hardware design
425 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200426
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300427#define MVNETA_TX_L3_OFF_SHIFT 0
428#define MVNETA_TX_IP_HLEN_SHIFT 8
429#define MVNETA_TX_L4_UDP BIT(16)
430#define MVNETA_TX_L3_IP6 BIT(17)
431#define MVNETA_TXD_IP_CSUM BIT(18)
432#define MVNETA_TXD_Z_PAD BIT(19)
433#define MVNETA_TXD_L_DESC BIT(20)
434#define MVNETA_TXD_F_DESC BIT(21)
435#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
436 MVNETA_TXD_L_DESC | \
437 MVNETA_TXD_F_DESC)
438#define MVNETA_TX_L4_CSUM_FULL BIT(30)
439#define MVNETA_TX_L4_CSUM_NOT BIT(31)
440
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300441#define MVNETA_RXD_ERR_CRC 0x0
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100442#define MVNETA_RXD_BM_POOL_SHIFT 13
443#define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300444#define MVNETA_RXD_ERR_SUMMARY BIT(16)
445#define MVNETA_RXD_ERR_OVERRUN BIT(17)
446#define MVNETA_RXD_ERR_LEN BIT(18)
447#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
448#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
449#define MVNETA_RXD_L3_IP4 BIT(25)
450#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
451#define MVNETA_RXD_L4_CSUM_OK BIT(30)
452
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200453#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200454struct mvneta_tx_desc {
455 u32 command; /* Options used by HW for packet transmitting.*/
456 u16 reserverd1; /* csum_l4 (for future use) */
457 u16 data_size; /* Data size of transmitted packet in bytes */
458 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
459 u32 reserved2; /* hw_cmd - (for future use, PMT) */
460 u32 reserved3[4]; /* Reserved - (for future use) */
461};
462
463struct mvneta_rx_desc {
464 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300465 u16 reserved1; /* pnc_info - (for future use, PnC) */
466 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200467
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300468 u32 buf_phys_addr; /* Physical address of the buffer */
469 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200470
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300471 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
472 u16 reserved3; /* prefetch_cmd, for future use */
473 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200474
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300475 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
476 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
477};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200478#else
479struct mvneta_tx_desc {
480 u16 data_size; /* Data size of transmitted packet in bytes */
481 u16 reserverd1; /* csum_l4 (for future use) */
482 u32 command; /* Options used by HW for packet transmitting.*/
483 u32 reserved2; /* hw_cmd - (for future use, PMT) */
484 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
485 u32 reserved3[4]; /* Reserved - (for future use) */
486};
487
488struct mvneta_rx_desc {
489 u16 data_size; /* Size of received packet in bytes */
490 u16 reserved1; /* pnc_info - (for future use, PnC) */
491 u32 status; /* Info about received packet */
492
493 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
494 u32 buf_phys_addr; /* Physical address of the buffer */
495
496 u16 reserved4; /* csum_l4 - (for future use, PnC) */
497 u16 reserved3; /* prefetch_cmd, for future use */
498 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
499
500 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
501 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
502};
503#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300504
505struct mvneta_tx_queue {
506 /* Number of this TX queue, in the range 0-7 */
507 u8 id;
508
509 /* Number of TX DMA descriptors in the descriptor ring */
510 int size;
511
512 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100513 * descriptor ring
514 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300515 int count;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300516 int tx_stop_threshold;
517 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300518
519 /* Array of transmitted skb */
520 struct sk_buff **tx_skb;
521
522 /* Index of last TX DMA descriptor that was inserted */
523 int txq_put_index;
524
525 /* Index of the TX DMA descriptor to be cleaned up */
526 int txq_get_index;
527
528 u32 done_pkts_coal;
529
530 /* Virtual address of the TX DMA descriptors array */
531 struct mvneta_tx_desc *descs;
532
533 /* DMA address of the TX DMA descriptors array */
534 dma_addr_t descs_phys;
535
536 /* Index of the last TX DMA descriptor */
537 int last_desc;
538
539 /* Index of the next TX DMA descriptor to process */
540 int next_desc_to_proc;
Ezequiel Garcia2adb7192014-05-19 13:59:55 -0300541
542 /* DMA buffers for TSO headers */
543 char *tso_hdrs;
544
545 /* DMA address of TSO headers */
546 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100547
548 /* Affinity mask for CPUs*/
549 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300550};
551
552struct mvneta_rx_queue {
553 /* rx queue number, in the range 0-7 */
554 u8 id;
555
556 /* num of rx descriptors in the rx descriptor ring */
557 int size;
558
559 /* counter of times when mvneta_refill() failed */
560 int missed;
561
562 u32 pkts_coal;
563 u32 time_coal;
564
565 /* Virtual address of the RX DMA descriptors array */
566 struct mvneta_rx_desc *descs;
567
568 /* DMA address of the RX DMA descriptors array */
569 dma_addr_t descs_phys;
570
571 /* Index of the last RX DMA descriptor */
572 int last_desc;
573
574 /* Index of the next RX DMA descriptor to process */
575 int next_desc_to_proc;
576};
577
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200578static enum cpuhp_state online_hpstate;
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300579/* The hardware supports eight (8) rx queues, but we are only allowing
580 * the first one to be used. Therefore, let's just allocate one queue.
581 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200582static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300583static int txq_number = 8;
584
585static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300586
willy tarreauf19fadf2014-01-16 08:20:17 +0100587static int rx_copybreak __read_mostly = 256;
588
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100589/* HW BM need that each port be identify by a unique ID */
590static int global_port_id;
591
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300592#define MVNETA_DRIVER_NAME "mvneta"
593#define MVNETA_DRIVER_VERSION "1.0"
594
595/* Utility/helper methods */
596
597/* Write helper method */
598static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
599{
600 writel(data, pp->base + offset);
601}
602
603/* Read helper method */
604static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
605{
606 return readl(pp->base + offset);
607}
608
609/* Increment txq get counter */
610static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
611{
612 txq->txq_get_index++;
613 if (txq->txq_get_index == txq->size)
614 txq->txq_get_index = 0;
615}
616
617/* Increment txq put counter */
618static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
619{
620 txq->txq_put_index++;
621 if (txq->txq_put_index == txq->size)
622 txq->txq_put_index = 0;
623}
624
625
626/* Clear all MIB counters */
627static void mvneta_mib_counters_clear(struct mvneta_port *pp)
628{
629 int i;
630 u32 dummy;
631
632 /* Perform dummy reads from MIB counters */
633 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
634 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100635 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
636 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300637}
638
639/* Get System Network Statistics */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +0800640static struct rtnl_link_stats64 *
641mvneta_get_stats64(struct net_device *dev,
642 struct rtnl_link_stats64 *stats)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300643{
644 struct mvneta_port *pp = netdev_priv(dev);
645 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100646 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300647
willy tarreau74c41b02014-01-16 08:20:08 +0100648 for_each_possible_cpu(cpu) {
649 struct mvneta_pcpu_stats *cpu_stats;
650 u64 rx_packets;
651 u64 rx_bytes;
652 u64 tx_packets;
653 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300654
willy tarreau74c41b02014-01-16 08:20:08 +0100655 cpu_stats = per_cpu_ptr(pp->stats, cpu);
656 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700657 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100658 rx_packets = cpu_stats->rx_packets;
659 rx_bytes = cpu_stats->rx_bytes;
660 tx_packets = cpu_stats->tx_packets;
661 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700662 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300663
willy tarreau74c41b02014-01-16 08:20:08 +0100664 stats->rx_packets += rx_packets;
665 stats->rx_bytes += rx_bytes;
666 stats->tx_packets += tx_packets;
667 stats->tx_bytes += tx_bytes;
668 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300669
670 stats->rx_errors = dev->stats.rx_errors;
671 stats->rx_dropped = dev->stats.rx_dropped;
672
673 stats->tx_dropped = dev->stats.tx_dropped;
674
675 return stats;
676}
677
678/* Rx descriptors helper methods */
679
willy tarreau54282132014-01-16 08:20:14 +0100680/* Checks whether the RX descriptor having this status is both the first
681 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300682 * received through a single RX descriptor, so not having each RX
683 * descriptor with its first and last bits set is an error
684 */
willy tarreau54282132014-01-16 08:20:14 +0100685static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300686{
willy tarreau54282132014-01-16 08:20:14 +0100687 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300688 MVNETA_RXD_FIRST_LAST_DESC;
689}
690
691/* Add number of descriptors ready to receive new packets */
692static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
693 struct mvneta_rx_queue *rxq,
694 int ndescs)
695{
696 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100697 * be added at once
698 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300699 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
700 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
701 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
702 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
703 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
704 }
705
706 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
707 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
708}
709
710/* Get number of RX descriptors occupied by received packets */
711static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
712 struct mvneta_rx_queue *rxq)
713{
714 u32 val;
715
716 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
717 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
718}
719
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100720/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300721 * from mvneta_rxq_drop_pkts().
722 */
723static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
724 struct mvneta_rx_queue *rxq,
725 int rx_done, int rx_filled)
726{
727 u32 val;
728
729 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
730 val = rx_done |
731 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
732 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
733 return;
734 }
735
736 /* Only 255 descriptors can be added at once */
737 while ((rx_done > 0) || (rx_filled > 0)) {
738 if (rx_done <= 0xff) {
739 val = rx_done;
740 rx_done = 0;
741 } else {
742 val = 0xff;
743 rx_done -= 0xff;
744 }
745 if (rx_filled <= 0xff) {
746 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
747 rx_filled = 0;
748 } else {
749 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
750 rx_filled -= 0xff;
751 }
752 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
753 }
754}
755
756/* Get pointer to next RX descriptor to be processed by SW */
757static struct mvneta_rx_desc *
758mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
759{
760 int rx_desc = rxq->next_desc_to_proc;
761
762 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100763 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300764 return rxq->descs + rx_desc;
765}
766
767/* Change maximum receive size of the port. */
768static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
769{
770 u32 val;
771
772 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
773 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
774 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
775 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
776 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
777}
778
779
780/* Set rx queue offset */
781static void mvneta_rxq_offset_set(struct mvneta_port *pp,
782 struct mvneta_rx_queue *rxq,
783 int offset)
784{
785 u32 val;
786
787 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
788 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
789
790 /* Offset is in */
791 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
792 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
793}
794
795
796/* Tx descriptors helper methods */
797
798/* Update HW with number of TX descriptors to be sent */
799static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
800 struct mvneta_tx_queue *txq,
801 int pend_desc)
802{
803 u32 val;
804
805 /* Only 255 descriptors can be added at once ; Assume caller
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100806 * process TX desriptors in quanta less than 256
807 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300808 val = pend_desc;
809 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
810}
811
812/* Get pointer to next TX descriptor to be processed (send) by HW */
813static struct mvneta_tx_desc *
814mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
815{
816 int tx_desc = txq->next_desc_to_proc;
817
818 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
819 return txq->descs + tx_desc;
820}
821
822/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100823 * mapping failures in the TX path.
824 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300825static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
826{
827 if (txq->next_desc_to_proc == 0)
828 txq->next_desc_to_proc = txq->last_desc - 1;
829 else
830 txq->next_desc_to_proc--;
831}
832
833/* Set rxq buf size */
834static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
835 struct mvneta_rx_queue *rxq,
836 int buf_size)
837{
838 u32 val;
839
840 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
841
842 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
843 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
844
845 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
846}
847
848/* Disable buffer management (BM) */
849static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
850 struct mvneta_rx_queue *rxq)
851{
852 u32 val;
853
854 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
855 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
856 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
857}
858
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100859/* Enable buffer management (BM) */
860static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
861 struct mvneta_rx_queue *rxq)
862{
863 u32 val;
864
865 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
866 val |= MVNETA_RXQ_HW_BUF_ALLOC;
867 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
868}
869
870/* Notify HW about port's assignment of pool for bigger packets */
871static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
872 struct mvneta_rx_queue *rxq)
873{
874 u32 val;
875
876 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
877 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
878 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
879
880 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
881}
882
883/* Notify HW about port's assignment of pool for smaller packets */
884static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
885 struct mvneta_rx_queue *rxq)
886{
887 u32 val;
888
889 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
890 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
891 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
892
893 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
894}
895
896/* Set port's receive buffer size for assigned BM pool */
897static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
898 int buf_size,
899 u8 pool_id)
900{
901 u32 val;
902
903 if (!IS_ALIGNED(buf_size, 8)) {
904 dev_warn(pp->dev->dev.parent,
905 "illegal buf_size value %d, round to %d\n",
906 buf_size, ALIGN(buf_size, 8));
907 buf_size = ALIGN(buf_size, 8);
908 }
909
910 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
911 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
912 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
913}
914
915/* Configure MBUS window in order to enable access BM internal SRAM */
916static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
917 u8 target, u8 attr)
918{
919 u32 win_enable, win_protect;
920 int i;
921
922 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
923
924 if (pp->bm_win_id < 0) {
925 /* Find first not occupied window */
926 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
927 if (win_enable & (1 << i)) {
928 pp->bm_win_id = i;
929 break;
930 }
931 }
932 if (i == MVNETA_MAX_DECODE_WIN)
933 return -ENOMEM;
934 } else {
935 i = pp->bm_win_id;
936 }
937
938 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
939 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
940
941 if (i < 4)
942 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
943
944 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
945 (attr << 8) | target);
946
947 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
948
949 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
950 win_protect |= 3 << (2 * i);
951 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
952
953 win_enable &= ~(1 << i);
954 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
955
956 return 0;
957}
958
959/* Assign and initialize pools for port. In case of fail
960 * buffer manager will remain disabled for current port.
961 */
962static int mvneta_bm_port_init(struct platform_device *pdev,
963 struct mvneta_port *pp)
964{
965 struct device_node *dn = pdev->dev.of_node;
966 u32 long_pool_id, short_pool_id, wsize;
967 u8 target, attr;
968 int err;
969
970 /* Get BM window information */
971 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
972 &target, &attr);
973 if (err < 0)
974 return err;
975
976 pp->bm_win_id = -1;
977
978 /* Open NETA -> BM window */
979 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
980 target, attr);
981 if (err < 0) {
982 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
983 return err;
984 }
985
986 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
987 netdev_info(pp->dev, "missing long pool id\n");
988 return -EINVAL;
989 }
990
991 /* Create port's long pool depending on mtu */
992 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
993 MVNETA_BM_LONG, pp->id,
994 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
995 if (!pp->pool_long) {
996 netdev_info(pp->dev, "fail to obtain long pool for port\n");
997 return -ENOMEM;
998 }
999
1000 pp->pool_long->port_map |= 1 << pp->id;
1001
1002 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1003 pp->pool_long->id);
1004
1005 /* If short pool id is not defined, assume using single pool */
1006 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1007 short_pool_id = long_pool_id;
1008
1009 /* Create port's short pool */
1010 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1011 MVNETA_BM_SHORT, pp->id,
1012 MVNETA_BM_SHORT_PKT_SIZE);
1013 if (!pp->pool_short) {
1014 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1015 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1016 return -ENOMEM;
1017 }
1018
1019 if (short_pool_id != long_pool_id) {
1020 pp->pool_short->port_map |= 1 << pp->id;
1021 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1022 pp->pool_short->id);
1023 }
1024
1025 return 0;
1026}
1027
1028/* Update settings of a pool for bigger packets */
1029static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1030{
1031 struct mvneta_bm_pool *bm_pool = pp->pool_long;
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001032 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001033 int num;
1034
1035 /* Release all buffers from long pool */
1036 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001037 if (hwbm_pool->buf_num) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001038 WARN(1, "cannot free all buffers in pool %d\n",
1039 bm_pool->id);
1040 goto bm_mtu_err;
1041 }
1042
1043 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1044 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001045 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1046 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001047
1048 /* Fill entire long pool */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001049 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1050 if (num != hwbm_pool->size) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001051 WARN(1, "pool %d: %d of %d allocated\n",
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001052 bm_pool->id, num, hwbm_pool->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001053 goto bm_mtu_err;
1054 }
1055 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1056
1057 return;
1058
1059bm_mtu_err:
1060 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1061 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1062
1063 pp->bm_priv = NULL;
1064 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1065 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1066}
1067
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001068/* Start the Ethernet port RX and TX activity */
1069static void mvneta_port_up(struct mvneta_port *pp)
1070{
1071 int queue;
1072 u32 q_map;
1073
1074 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001075 q_map = 0;
1076 for (queue = 0; queue < txq_number; queue++) {
1077 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1078 if (txq->descs != NULL)
1079 q_map |= (1 << queue);
1080 }
1081 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1082
Yelena Krivosheev2b1c1ad2018-03-30 12:05:31 +02001083 q_map = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001084 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001085 for (queue = 0; queue < rxq_number; queue++) {
1086 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1087
1088 if (rxq->descs != NULL)
1089 q_map |= (1 << queue);
1090 }
1091 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001092}
1093
1094/* Stop the Ethernet port activity */
1095static void mvneta_port_down(struct mvneta_port *pp)
1096{
1097 u32 val;
1098 int count;
1099
1100 /* Stop Rx port activity. Check port Rx activity. */
1101 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1102
1103 /* Issue stop command for active channels only */
1104 if (val != 0)
1105 mvreg_write(pp, MVNETA_RXQ_CMD,
1106 val << MVNETA_RXQ_DISABLE_SHIFT);
1107
1108 /* Wait for all Rx activity to terminate. */
1109 count = 0;
1110 do {
1111 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1112 netdev_warn(pp->dev,
Dmitri Epshtein0838abb2016-03-12 18:44:19 +01001113 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001114 val);
1115 break;
1116 }
1117 mdelay(1);
1118
1119 val = mvreg_read(pp, MVNETA_RXQ_CMD);
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001120 } while (val & MVNETA_RXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001121
1122 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001123 * command for active channels only
1124 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001125 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1126
1127 if (val != 0)
1128 mvreg_write(pp, MVNETA_TXQ_CMD,
1129 (val << MVNETA_TXQ_DISABLE_SHIFT));
1130
1131 /* Wait for all Tx activity to terminate. */
1132 count = 0;
1133 do {
1134 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1135 netdev_warn(pp->dev,
1136 "TIMEOUT for TX stopped status=0x%08x\n",
1137 val);
1138 break;
1139 }
1140 mdelay(1);
1141
1142 /* Check TX Command reg that all Txqs are stopped */
1143 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1144
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001145 } while (val & MVNETA_TXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001146
1147 /* Double check to verify that TX FIFO is empty */
1148 count = 0;
1149 do {
1150 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1151 netdev_warn(pp->dev,
Dmitri Epshtein0838abb2016-03-12 18:44:19 +01001152 "TX FIFO empty timeout status=0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001153 val);
1154 break;
1155 }
1156 mdelay(1);
1157
1158 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1159 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1160 (val & MVNETA_TX_IN_PRGRS));
1161
1162 udelay(200);
1163}
1164
1165/* Enable the port by setting the port enable bit of the MAC control register */
1166static void mvneta_port_enable(struct mvneta_port *pp)
1167{
1168 u32 val;
1169
1170 /* Enable port */
1171 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1172 val |= MVNETA_GMAC0_PORT_ENABLE;
1173 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1174}
1175
1176/* Disable the port and wait for about 200 usec before retuning */
1177static void mvneta_port_disable(struct mvneta_port *pp)
1178{
1179 u32 val;
1180
1181 /* Reset the Enable bit in the Serial Control Register */
1182 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1183 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1184 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1185
Yelena Krivosheev405f3d72017-12-19 17:59:45 +01001186 pp->link = 0;
1187 pp->duplex = -1;
1188 pp->speed = 0;
1189
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001190 udelay(200);
1191}
1192
1193/* Multicast tables methods */
1194
1195/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1196static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1197{
1198 int offset;
1199 u32 val;
1200
1201 if (queue == -1) {
1202 val = 0;
1203 } else {
1204 val = 0x1 | (queue << 1);
1205 val |= (val << 24) | (val << 16) | (val << 8);
1206 }
1207
1208 for (offset = 0; offset <= 0xc; offset += 4)
1209 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1210}
1211
1212/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1213static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1214{
1215 int offset;
1216 u32 val;
1217
1218 if (queue == -1) {
1219 val = 0;
1220 } else {
1221 val = 0x1 | (queue << 1);
1222 val |= (val << 24) | (val << 16) | (val << 8);
1223 }
1224
1225 for (offset = 0; offset <= 0xfc; offset += 4)
1226 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1227
1228}
1229
1230/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1231static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1232{
1233 int offset;
1234 u32 val;
1235
1236 if (queue == -1) {
1237 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1238 val = 0;
1239 } else {
1240 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1241 val = 0x1 | (queue << 1);
1242 val |= (val << 24) | (val << 16) | (val << 8);
1243 }
1244
1245 for (offset = 0; offset <= 0xfc; offset += 4)
1246 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1247}
1248
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001249static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
1250{
1251 u32 val;
1252
1253 if (enable) {
1254 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1255 val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1256 MVNETA_GMAC_FORCE_LINK_DOWN |
1257 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1258 val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1259 MVNETA_GMAC_AN_SPEED_EN |
1260 MVNETA_GMAC_AN_DUPLEX_EN;
1261 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1262
1263 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1264 val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1265 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1266
1267 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1268 val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
1269 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1270 } else {
1271 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1272 val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1273 MVNETA_GMAC_AN_SPEED_EN |
1274 MVNETA_GMAC_AN_DUPLEX_EN);
1275 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1276
1277 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1278 val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
1279 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1280
1281 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1282 val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
1283 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1284 }
1285}
1286
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001287static void mvneta_percpu_unmask_interrupt(void *arg)
1288{
1289 struct mvneta_port *pp = arg;
1290
1291 /* All the queue are unmasked, but actually only the ones
1292 * mapped to this CPU will be unmasked
1293 */
1294 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1295 MVNETA_RX_INTR_MASK_ALL |
1296 MVNETA_TX_INTR_MASK_ALL |
1297 MVNETA_MISCINTR_INTR_MASK);
1298}
1299
1300static void mvneta_percpu_mask_interrupt(void *arg)
1301{
1302 struct mvneta_port *pp = arg;
1303
1304 /* All the queue are masked, but actually only the ones
1305 * mapped to this CPU will be masked
1306 */
1307 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1308 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1309 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1310}
1311
1312static void mvneta_percpu_clear_intr_cause(void *arg)
1313{
1314 struct mvneta_port *pp = arg;
1315
1316 /* All the queue are cleared, but actually only the ones
1317 * mapped to this CPU will be cleared
1318 */
1319 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1320 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1321 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1322}
1323
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001324/* This method sets defaults to the NETA port:
1325 * Clears interrupt Cause and Mask registers.
1326 * Clears all MAC tables.
1327 * Sets defaults to all registers.
1328 * Resets RX and TX descriptor rings.
1329 * Resets PHY.
1330 * This method can be called after mvneta_port_down() to return the port
1331 * settings to defaults.
1332 */
1333static void mvneta_defaults_set(struct mvneta_port *pp)
1334{
1335 int cpu;
1336 int queue;
1337 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001338 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001339
1340 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001341 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001342
1343 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001344 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001345 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1346
1347 /* Enable MBUS Retry bit16 */
1348 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1349
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001350 /* Set CPU queue access map. CPUs are assigned to the RX and
1351 * TX queues modulo their number. If there is only one TX
1352 * queue then it is assigned to the CPU associated to the
1353 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001354 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001355 for_each_present_cpu(cpu) {
1356 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001357 int rxq, txq;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001358
1359 for (rxq = 0; rxq < rxq_number; rxq++)
1360 if ((rxq % max_cpu) == cpu)
1361 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1362
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001363 for (txq = 0; txq < txq_number; txq++)
1364 if ((txq % max_cpu) == cpu)
1365 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1366
1367 /* With only one TX queue we configure a special case
1368 * which will allow to get all the irq on a single
1369 * CPU
1370 */
1371 if (txq_number == 1)
1372 txq_map = (cpu == pp->rxq_def) ?
1373 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001374
1375 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1376 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001377
1378 /* Reset RX and TX DMAs */
1379 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1380 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1381
1382 /* Disable Legacy WRR, Disable EJP, Release from reset */
1383 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1384 for (queue = 0; queue < txq_number; queue++) {
1385 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1386 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1387 }
1388
1389 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1390 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1391
1392 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001393 if (pp->bm_priv)
1394 /* HW buffer management + legacy parser */
1395 val = MVNETA_ACC_MODE_EXT2;
1396 else
1397 /* SW buffer management + legacy parser */
1398 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001399 mvreg_write(pp, MVNETA_ACC_MODE, val);
1400
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001401 if (pp->bm_priv)
1402 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1403
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001404 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001405 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001406 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1407
1408 val = 0;
1409 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1410 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1411
1412 /* Build PORT_SDMA_CONFIG_REG */
1413 val = 0;
1414
1415 /* Default burst size */
1416 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1417 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001418 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001419
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001420#if defined(__BIG_ENDIAN)
1421 val |= MVNETA_DESC_SWAP;
1422#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001423
1424 /* Assign port SDMA configuration */
1425 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1426
Thomas Petazzoni71408602013-09-04 16:21:18 +02001427 /* Disable PHY polling in hardware, since we're using the
1428 * kernel phylib to do this.
1429 */
1430 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1431 val &= ~MVNETA_PHY_POLLING_ENABLE;
1432 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1433
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001434 mvneta_set_autoneg(pp, pp->use_inband_status);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001435 mvneta_set_ucast_table(pp, -1);
1436 mvneta_set_special_mcast_table(pp, -1);
1437 mvneta_set_other_mcast_table(pp, -1);
1438
1439 /* Set port interrupt enable register - default enable all */
1440 mvreg_write(pp, MVNETA_INTR_ENABLE,
1441 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1442 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001443
1444 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001445}
1446
1447/* Set max sizes for tx queues */
1448static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1449
1450{
1451 u32 val, size, mtu;
1452 int queue;
1453
1454 mtu = max_tx_size * 8;
1455 if (mtu > MVNETA_TX_MTU_MAX)
1456 mtu = MVNETA_TX_MTU_MAX;
1457
1458 /* Set MTU */
1459 val = mvreg_read(pp, MVNETA_TX_MTU);
1460 val &= ~MVNETA_TX_MTU_MAX;
1461 val |= mtu;
1462 mvreg_write(pp, MVNETA_TX_MTU, val);
1463
1464 /* TX token size and all TXQs token size must be larger that MTU */
1465 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1466
1467 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1468 if (size < mtu) {
1469 size = mtu;
1470 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1471 val |= size;
1472 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1473 }
1474 for (queue = 0; queue < txq_number; queue++) {
1475 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1476
1477 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1478 if (size < mtu) {
1479 size = mtu;
1480 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1481 val |= size;
1482 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1483 }
1484 }
1485}
1486
1487/* Set unicast address */
1488static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1489 int queue)
1490{
1491 unsigned int unicast_reg;
1492 unsigned int tbl_offset;
1493 unsigned int reg_offset;
1494
1495 /* Locate the Unicast table entry */
1496 last_nibble = (0xf & last_nibble);
1497
1498 /* offset from unicast tbl base */
1499 tbl_offset = (last_nibble / 4) * 4;
1500
1501 /* offset within the above reg */
1502 reg_offset = last_nibble % 4;
1503
1504 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1505
1506 if (queue == -1) {
1507 /* Clear accepts frame bit at specified unicast DA tbl entry */
1508 unicast_reg &= ~(0xff << (8 * reg_offset));
1509 } else {
1510 unicast_reg &= ~(0xff << (8 * reg_offset));
1511 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1512 }
1513
1514 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1515}
1516
1517/* Set mac address */
1518static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1519 int queue)
1520{
1521 unsigned int mac_h;
1522 unsigned int mac_l;
1523
1524 if (queue != -1) {
1525 mac_l = (addr[4] << 8) | (addr[5]);
1526 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1527 (addr[2] << 8) | (addr[3] << 0);
1528
1529 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1530 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1531 }
1532
1533 /* Accept frames of this address */
1534 mvneta_set_ucast_addr(pp, addr[5], queue);
1535}
1536
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001537/* Set the number of packets that will be received before RX interrupt
1538 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001539 */
1540static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1541 struct mvneta_rx_queue *rxq, u32 value)
1542{
1543 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1544 value | MVNETA_RXQ_NON_OCCUPIED(0));
1545 rxq->pkts_coal = value;
1546}
1547
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001548/* Set the time delay in usec before RX interrupt will be generated by
1549 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001550 */
1551static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1552 struct mvneta_rx_queue *rxq, u32 value)
1553{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001554 u32 val;
1555 unsigned long clk_rate;
1556
1557 clk_rate = clk_get_rate(pp->clk);
1558 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001559
1560 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1561 rxq->time_coal = value;
1562}
1563
1564/* Set threshold for TX_DONE pkts coalescing */
1565static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1566 struct mvneta_tx_queue *txq, u32 value)
1567{
1568 u32 val;
1569
1570 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1571
1572 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1573 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1574
1575 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1576
1577 txq->done_pkts_coal = value;
1578}
1579
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001580/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1581static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1582 u32 phys_addr, u32 cookie)
1583{
1584 rx_desc->buf_cookie = cookie;
1585 rx_desc->buf_phys_addr = phys_addr;
1586}
1587
1588/* Decrement sent descriptors counter */
1589static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1590 struct mvneta_tx_queue *txq,
1591 int sent_desc)
1592{
1593 u32 val;
1594
1595 /* Only 255 TX descriptors can be updated at once */
1596 while (sent_desc > 0xff) {
1597 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1598 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1599 sent_desc = sent_desc - 0xff;
1600 }
1601
1602 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1603 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1604}
1605
1606/* Get number of TX descriptors already sent by HW */
1607static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1608 struct mvneta_tx_queue *txq)
1609{
1610 u32 val;
1611 int sent_desc;
1612
1613 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1614 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1615 MVNETA_TXQ_SENT_DESC_SHIFT;
1616
1617 return sent_desc;
1618}
1619
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001620/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001621 * The number of sent descriptors is returned.
1622 */
1623static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1624 struct mvneta_tx_queue *txq)
1625{
1626 int sent_desc;
1627
1628 /* Get number of sent descriptors */
1629 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1630
1631 /* Decrement sent descriptors counter */
1632 if (sent_desc)
1633 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1634
1635 return sent_desc;
1636}
1637
1638/* Set TXQ descriptors fields relevant for CSUM calculation */
1639static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1640 int ip_hdr_len, int l4_proto)
1641{
1642 u32 command;
1643
1644 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001645 * G_L4_chk, L4_type; required only for checksum
1646 * calculation
1647 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001648 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1649 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1650
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001651 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001652 command |= MVNETA_TXD_IP_CSUM;
1653 else
1654 command |= MVNETA_TX_L3_IP6;
1655
1656 if (l4_proto == IPPROTO_TCP)
1657 command |= MVNETA_TX_L4_CSUM_FULL;
1658 else if (l4_proto == IPPROTO_UDP)
1659 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1660 else
1661 command |= MVNETA_TX_L4_CSUM_NOT;
1662
1663 return command;
1664}
1665
1666
1667/* Display more error info */
1668static void mvneta_rx_error(struct mvneta_port *pp,
1669 struct mvneta_rx_desc *rx_desc)
1670{
1671 u32 status = rx_desc->status;
1672
willy tarreau54282132014-01-16 08:20:14 +01001673 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001674 netdev_err(pp->dev,
1675 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001676 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001677 return;
1678 }
1679
1680 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1681 case MVNETA_RXD_ERR_CRC:
1682 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1683 status, rx_desc->data_size);
1684 break;
1685 case MVNETA_RXD_ERR_OVERRUN:
1686 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1687 status, rx_desc->data_size);
1688 break;
1689 case MVNETA_RXD_ERR_LEN:
1690 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1691 status, rx_desc->data_size);
1692 break;
1693 case MVNETA_RXD_ERR_RESOURCE:
1694 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1695 status, rx_desc->data_size);
1696 break;
1697 }
1698}
1699
willy tarreau54282132014-01-16 08:20:14 +01001700/* Handle RX checksum offload based on the descriptor's status */
1701static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001702 struct sk_buff *skb)
1703{
willy tarreau54282132014-01-16 08:20:14 +01001704 if ((status & MVNETA_RXD_L3_IP4) &&
1705 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001706 skb->csum = 0;
1707 skb->ip_summed = CHECKSUM_UNNECESSARY;
1708 return;
1709 }
1710
1711 skb->ip_summed = CHECKSUM_NONE;
1712}
1713
willy tarreau6c498972014-01-16 08:20:12 +01001714/* Return tx queue pointer (find last set bit) according to <cause> returned
1715 * form tx_done reg. <cause> must not be null. The return value is always a
1716 * valid queue for matching the first one found in <cause>.
1717 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001718static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1719 u32 cause)
1720{
1721 int queue = fls(cause) - 1;
1722
willy tarreau6c498972014-01-16 08:20:12 +01001723 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001724}
1725
1726/* Free tx queue skbuffs */
1727static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1728 struct mvneta_tx_queue *txq, int num)
1729{
1730 int i;
1731
1732 for (i = 0; i < num; i++) {
1733 struct mvneta_tx_desc *tx_desc = txq->descs +
1734 txq->txq_get_index;
1735 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1736
1737 mvneta_txq_inc_get(txq);
1738
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001739 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1740 dma_unmap_single(pp->dev->dev.parent,
1741 tx_desc->buf_phys_addr,
1742 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001743 if (!skb)
1744 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001745 dev_kfree_skb_any(skb);
1746 }
1747}
1748
1749/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001750static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001751 struct mvneta_tx_queue *txq)
1752{
1753 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1754 int tx_done;
1755
1756 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001757 if (!tx_done)
1758 return;
1759
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001760 mvneta_txq_bufs_free(pp, txq, tx_done);
1761
1762 txq->count -= tx_done;
1763
1764 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001765 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001766 netif_tx_wake_queue(nq);
1767 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001768}
1769
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001770void *mvneta_frag_alloc(unsigned int frag_size)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001771{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001772 if (likely(frag_size <= PAGE_SIZE))
1773 return netdev_alloc_frag(frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001774 else
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001775 return kmalloc(frag_size, GFP_ATOMIC);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001776}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001777EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001778
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001779void mvneta_frag_free(unsigned int frag_size, void *data)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001780{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001781 if (likely(frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001782 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001783 else
1784 kfree(data);
1785}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001786EXPORT_SYMBOL_GPL(mvneta_frag_free);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001787
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001788/* Refill processing for SW buffer management */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001789static int mvneta_rx_refill(struct mvneta_port *pp,
1790 struct mvneta_rx_desc *rx_desc)
1791
1792{
1793 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001794 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001795
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001796 data = mvneta_frag_alloc(pp->frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001797 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001798 return -ENOMEM;
1799
willy tarreau8ec2cd42014-01-16 08:20:16 +01001800 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001801 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1802 DMA_FROM_DEVICE);
1803 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001804 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001805 return -ENOMEM;
1806 }
1807
willy tarreau8ec2cd42014-01-16 08:20:16 +01001808 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001809 return 0;
1810}
1811
1812/* Handle tx checksum */
1813static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1814{
1815 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1816 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001817 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001818 u8 l4_proto;
1819
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001820 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001821 struct iphdr *ip4h = ip_hdr(skb);
1822
1823 /* Calculate IPv4 checksum and L4 checksum */
1824 ip_hdr_len = ip4h->ihl;
1825 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001826 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001827 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1828
1829 /* Read l4_protocol from one of IPv6 extra headers */
1830 if (skb_network_header_len(skb) > 0)
1831 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1832 l4_proto = ip6h->nexthdr;
1833 } else
1834 return MVNETA_TX_L4_CSUM_NOT;
1835
1836 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001837 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001838 }
1839
1840 return MVNETA_TX_L4_CSUM_NOT;
1841}
1842
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001843/* Drop packets received by the RXQ and free buffers */
1844static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1845 struct mvneta_rx_queue *rxq)
1846{
1847 int rx_done, i;
1848
1849 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001850 if (rx_done)
1851 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1852
1853 if (pp->bm_priv) {
1854 for (i = 0; i < rx_done; i++) {
1855 struct mvneta_rx_desc *rx_desc =
1856 mvneta_rxq_next_desc_get(rxq);
1857 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1858 struct mvneta_bm_pool *bm_pool;
1859
1860 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1861 /* Return dropped buffer to the pool */
1862 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1863 rx_desc->buf_phys_addr);
1864 }
1865 return;
1866 }
1867
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001868 for (i = 0; i < rxq->size; i++) {
1869 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001870 void *data = (void *)rx_desc->buf_cookie;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001871
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001872 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001873 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001874 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001875 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001876}
1877
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001878/* Main rx processing when using software buffer management */
1879static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1880 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001881{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001882 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001883 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001884 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001885 u32 rcvd_pkts = 0;
1886 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001887
1888 /* Get number of received packets */
1889 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1890
1891 if (rx_todo > rx_done)
1892 rx_todo = rx_done;
1893
1894 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001895
1896 /* Fairness NAPI loop */
1897 while (rx_done < rx_todo) {
1898 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1899 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001900 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001901 dma_addr_t phys_addr;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001902 u32 rx_status, frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001903 int rx_bytes, err;
1904
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001905 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001906 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001907 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001908 data = (unsigned char *)rx_desc->buf_cookie;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001909 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001910
willy tarreau54282132014-01-16 08:20:14 +01001911 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001912 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
Yelena Krivosheev69cf72b2017-12-19 17:59:47 +01001913 mvneta_rx_error(pp, rx_desc);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001914err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001915 dev->stats.rx_errors++;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001916 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001917 continue;
1918 }
1919
willy tarreauf19fadf2014-01-16 08:20:17 +01001920 if (rx_bytes <= rx_copybreak) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001921 /* better copy a small frame and not unmap the DMA region */
willy tarreauf19fadf2014-01-16 08:20:17 +01001922 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1923 if (unlikely(!skb))
1924 goto err_drop_frame;
1925
1926 dma_sync_single_range_for_cpu(dev->dev.parent,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001927 rx_desc->buf_phys_addr,
1928 MVNETA_MH_SIZE + NET_SKB_PAD,
1929 rx_bytes,
1930 DMA_FROM_DEVICE);
willy tarreauf19fadf2014-01-16 08:20:17 +01001931 memcpy(skb_put(skb, rx_bytes),
1932 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1933 rx_bytes);
1934
1935 skb->protocol = eth_type_trans(skb, dev);
1936 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001937 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001938
1939 rcvd_pkts++;
1940 rcvd_bytes += rx_bytes;
1941
1942 /* leave the descriptor and buffer untouched */
1943 continue;
1944 }
1945
Simon Guinota84e3282015-07-19 13:00:53 +02001946 /* Refill processing */
1947 err = mvneta_rx_refill(pp, rx_desc);
1948 if (err) {
1949 netdev_err(dev, "Linux processing - Can't refill\n");
1950 rxq->missed++;
1951 goto err_drop_frame;
1952 }
1953
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001954 frag_size = pp->frag_size;
1955
1956 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01001957
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001958 /* After refill old buffer has to be unmapped regardless
1959 * the skb is successfully built or not.
1960 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02001961 dma_unmap_single(dev->dev.parent, phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001962 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1963 DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001964
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001965 if (!skb)
1966 goto err_drop_frame;
1967
willy tarreaudc4277d2014-01-16 08:20:07 +01001968 rcvd_pkts++;
1969 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001970
1971 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001972 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001973 skb_put(skb, rx_bytes);
1974
1975 skb->protocol = eth_type_trans(skb, dev);
1976
willy tarreau54282132014-01-16 08:20:14 +01001977 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001978
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001979 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001980 }
1981
willy tarreaudc4277d2014-01-16 08:20:07 +01001982 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001983 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1984
1985 u64_stats_update_begin(&stats->syncp);
1986 stats->rx_packets += rcvd_pkts;
1987 stats->rx_bytes += rcvd_bytes;
1988 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001989 }
1990
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001991 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02001992 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001993
1994 return rx_done;
1995}
1996
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001997/* Main rx processing when using hardware buffer management */
1998static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
1999 struct mvneta_rx_queue *rxq)
2000{
2001 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2002 struct net_device *dev = pp->dev;
2003 int rx_done;
2004 u32 rcvd_pkts = 0;
2005 u32 rcvd_bytes = 0;
2006
2007 /* Get number of received packets */
2008 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2009
2010 if (rx_todo > rx_done)
2011 rx_todo = rx_done;
2012
2013 rx_done = 0;
2014
2015 /* Fairness NAPI loop */
2016 while (rx_done < rx_todo) {
2017 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2018 struct mvneta_bm_pool *bm_pool = NULL;
2019 struct sk_buff *skb;
2020 unsigned char *data;
2021 dma_addr_t phys_addr;
2022 u32 rx_status, frag_size;
2023 int rx_bytes, err;
2024 u8 pool_id;
2025
2026 rx_done++;
2027 rx_status = rx_desc->status;
2028 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2029 data = (unsigned char *)rx_desc->buf_cookie;
2030 phys_addr = rx_desc->buf_phys_addr;
2031 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2032 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2033
2034 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2035 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2036err_drop_frame_ret_pool:
2037 /* Return the buffer to the pool */
2038 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2039 rx_desc->buf_phys_addr);
2040err_drop_frame:
2041 dev->stats.rx_errors++;
2042 mvneta_rx_error(pp, rx_desc);
2043 /* leave the descriptor untouched */
2044 continue;
2045 }
2046
2047 if (rx_bytes <= rx_copybreak) {
2048 /* better copy a small frame and not unmap the DMA region */
2049 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2050 if (unlikely(!skb))
2051 goto err_drop_frame_ret_pool;
2052
Russell Kingeda29122019-02-15 13:55:47 +00002053 dma_sync_single_range_for_cpu(&pp->bm_priv->pdev->dev,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002054 rx_desc->buf_phys_addr,
2055 MVNETA_MH_SIZE + NET_SKB_PAD,
2056 rx_bytes,
2057 DMA_FROM_DEVICE);
2058 memcpy(skb_put(skb, rx_bytes),
2059 data + MVNETA_MH_SIZE + NET_SKB_PAD,
2060 rx_bytes);
2061
2062 skb->protocol = eth_type_trans(skb, dev);
2063 mvneta_rx_csum(pp, rx_status, skb);
2064 napi_gro_receive(&port->napi, skb);
2065
2066 rcvd_pkts++;
2067 rcvd_bytes += rx_bytes;
2068
2069 /* Return the buffer to the pool */
2070 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2071 rx_desc->buf_phys_addr);
2072
2073 /* leave the descriptor and buffer untouched */
2074 continue;
2075 }
2076
2077 /* Refill processing */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002078 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002079 if (err) {
2080 netdev_err(dev, "Linux processing - Can't refill\n");
2081 rxq->missed++;
2082 goto err_drop_frame_ret_pool;
2083 }
2084
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002085 frag_size = bm_pool->hwbm_pool.frag_size;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002086
2087 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2088
2089 /* After refill old buffer has to be unmapped regardless
2090 * the skb is successfully built or not.
2091 */
2092 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2093 bm_pool->buf_size, DMA_FROM_DEVICE);
2094 if (!skb)
2095 goto err_drop_frame;
2096
2097 rcvd_pkts++;
2098 rcvd_bytes += rx_bytes;
2099
2100 /* Linux processing */
2101 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2102 skb_put(skb, rx_bytes);
2103
2104 skb->protocol = eth_type_trans(skb, dev);
2105
2106 mvneta_rx_csum(pp, rx_status, skb);
2107
2108 napi_gro_receive(&port->napi, skb);
2109 }
2110
2111 if (rcvd_pkts) {
2112 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2113
2114 u64_stats_update_begin(&stats->syncp);
2115 stats->rx_packets += rcvd_pkts;
2116 stats->rx_bytes += rcvd_bytes;
2117 u64_stats_update_end(&stats->syncp);
2118 }
2119
2120 /* Update rxq management counters */
2121 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2122
2123 return rx_done;
2124}
2125
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002126static inline void
2127mvneta_tso_put_hdr(struct sk_buff *skb,
2128 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2129{
2130 struct mvneta_tx_desc *tx_desc;
2131 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2132
2133 txq->tx_skb[txq->txq_put_index] = NULL;
2134 tx_desc = mvneta_txq_next_desc_get(txq);
2135 tx_desc->data_size = hdr_len;
2136 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2137 tx_desc->command |= MVNETA_TXD_F_DESC;
2138 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2139 txq->txq_put_index * TSO_HEADER_SIZE;
2140 mvneta_txq_inc_put(txq);
2141}
2142
2143static inline int
2144mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2145 struct sk_buff *skb, char *data, int size,
2146 bool last_tcp, bool is_last)
2147{
2148 struct mvneta_tx_desc *tx_desc;
2149
2150 tx_desc = mvneta_txq_next_desc_get(txq);
2151 tx_desc->data_size = size;
2152 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2153 size, DMA_TO_DEVICE);
2154 if (unlikely(dma_mapping_error(dev->dev.parent,
2155 tx_desc->buf_phys_addr))) {
2156 mvneta_txq_desc_put(txq);
2157 return -ENOMEM;
2158 }
2159
2160 tx_desc->command = 0;
2161 txq->tx_skb[txq->txq_put_index] = NULL;
2162
2163 if (last_tcp) {
2164 /* last descriptor in the TCP packet */
2165 tx_desc->command = MVNETA_TXD_L_DESC;
2166
2167 /* last descriptor in SKB */
2168 if (is_last)
2169 txq->tx_skb[txq->txq_put_index] = skb;
2170 }
2171 mvneta_txq_inc_put(txq);
2172 return 0;
2173}
2174
2175static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2176 struct mvneta_tx_queue *txq)
2177{
2178 int total_len, data_left;
2179 int desc_count = 0;
2180 struct mvneta_port *pp = netdev_priv(dev);
2181 struct tso_t tso;
2182 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2183 int i;
2184
2185 /* Count needed descriptors */
2186 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2187 return 0;
2188
2189 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2190 pr_info("*** Is this even possible???!?!?\n");
2191 return 0;
2192 }
2193
2194 /* Initialize the TSO handler, and prepare the first payload */
2195 tso_start(skb, &tso);
2196
2197 total_len = skb->len - hdr_len;
2198 while (total_len > 0) {
2199 char *hdr;
2200
2201 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2202 total_len -= data_left;
2203 desc_count++;
2204
2205 /* prepare packet headers: MAC + IP + TCP */
2206 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2207 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2208
2209 mvneta_tso_put_hdr(skb, pp, txq);
2210
2211 while (data_left > 0) {
2212 int size;
2213 desc_count++;
2214
2215 size = min_t(int, tso.size, data_left);
2216
2217 if (mvneta_tso_put_data(dev, txq, skb,
2218 tso.data, size,
2219 size == data_left,
2220 total_len == 0))
2221 goto err_release;
2222 data_left -= size;
2223
2224 tso_build_data(skb, &tso, size);
2225 }
2226 }
2227
2228 return desc_count;
2229
2230err_release:
2231 /* Release all used data descriptors; header descriptors must not
2232 * be DMA-unmapped.
2233 */
2234 for (i = desc_count - 1; i >= 0; i--) {
2235 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002236 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002237 dma_unmap_single(pp->dev->dev.parent,
2238 tx_desc->buf_phys_addr,
2239 tx_desc->data_size,
2240 DMA_TO_DEVICE);
2241 mvneta_txq_desc_put(txq);
2242 }
2243 return 0;
2244}
2245
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002246/* Handle tx fragmentation processing */
2247static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2248 struct mvneta_tx_queue *txq)
2249{
2250 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002251 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002252
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002253 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002254 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2255 void *addr = page_address(frag->page.p) + frag->page_offset;
2256
2257 tx_desc = mvneta_txq_next_desc_get(txq);
2258 tx_desc->data_size = frag->size;
2259
2260 tx_desc->buf_phys_addr =
2261 dma_map_single(pp->dev->dev.parent, addr,
2262 tx_desc->data_size, DMA_TO_DEVICE);
2263
2264 if (dma_mapping_error(pp->dev->dev.parent,
2265 tx_desc->buf_phys_addr)) {
2266 mvneta_txq_desc_put(txq);
2267 goto error;
2268 }
2269
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002270 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002271 /* Last descriptor */
2272 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002273 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002274 } else {
2275 /* Descriptor in the middle: Not First, Not Last */
2276 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002277 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002278 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002279 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002280 }
2281
2282 return 0;
2283
2284error:
2285 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002286 * this packet, as well as the corresponding DMA mappings
2287 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002288 for (i = i - 1; i >= 0; i--) {
2289 tx_desc = txq->descs + i;
2290 dma_unmap_single(pp->dev->dev.parent,
2291 tx_desc->buf_phys_addr,
2292 tx_desc->data_size,
2293 DMA_TO_DEVICE);
2294 mvneta_txq_desc_put(txq);
2295 }
2296
2297 return -ENOMEM;
2298}
2299
2300/* Main tx processing */
2301static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2302{
2303 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002304 u16 txq_id = skb_get_queue_mapping(skb);
2305 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002306 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002307 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002308 int frags = 0;
2309 u32 tx_cmd;
2310
2311 if (!netif_running(dev))
2312 goto out;
2313
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002314 if (skb_is_gso(skb)) {
2315 frags = mvneta_tx_tso(skb, dev, txq);
2316 goto out;
2317 }
2318
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002319 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002320
2321 /* Get a descriptor for the first part of the packet */
2322 tx_desc = mvneta_txq_next_desc_get(txq);
2323
2324 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2325
2326 tx_desc->data_size = skb_headlen(skb);
2327
2328 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2329 tx_desc->data_size,
2330 DMA_TO_DEVICE);
2331 if (unlikely(dma_mapping_error(dev->dev.parent,
2332 tx_desc->buf_phys_addr))) {
2333 mvneta_txq_desc_put(txq);
2334 frags = 0;
2335 goto out;
2336 }
2337
2338 if (frags == 1) {
2339 /* First and Last descriptor */
2340 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2341 tx_desc->command = tx_cmd;
2342 txq->tx_skb[txq->txq_put_index] = skb;
2343 mvneta_txq_inc_put(txq);
2344 } else {
2345 /* First but not Last */
2346 tx_cmd |= MVNETA_TXD_F_DESC;
2347 txq->tx_skb[txq->txq_put_index] = NULL;
2348 mvneta_txq_inc_put(txq);
2349 tx_desc->command = tx_cmd;
2350 /* Continue with other skb fragments */
2351 if (mvneta_tx_frag_process(pp, skb, txq)) {
2352 dma_unmap_single(dev->dev.parent,
2353 tx_desc->buf_phys_addr,
2354 tx_desc->data_size,
2355 DMA_TO_DEVICE);
2356 mvneta_txq_desc_put(txq);
2357 frags = 0;
2358 goto out;
2359 }
2360 }
2361
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002362out:
2363 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01002364 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002365 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2366
2367 txq->count += frags;
2368 mvneta_txq_pend_desc_add(pp, txq, frags);
2369
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002370 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002371 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002372
willy tarreau74c41b02014-01-16 08:20:08 +01002373 u64_stats_update_begin(&stats->syncp);
2374 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002375 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01002376 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002377 } else {
2378 dev->stats.tx_dropped++;
2379 dev_kfree_skb_any(skb);
2380 }
2381
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002382 return NETDEV_TX_OK;
2383}
2384
2385
2386/* Free tx resources, when resetting a port */
2387static void mvneta_txq_done_force(struct mvneta_port *pp,
2388 struct mvneta_tx_queue *txq)
2389
2390{
2391 int tx_done = txq->count;
2392
2393 mvneta_txq_bufs_free(pp, txq, tx_done);
2394
2395 /* reset txq */
2396 txq->count = 0;
2397 txq->txq_put_index = 0;
2398 txq->txq_get_index = 0;
2399}
2400
willy tarreau6c498972014-01-16 08:20:12 +01002401/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2402 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2403 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002404static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002405{
2406 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002407 struct netdev_queue *nq;
2408
willy tarreau6c498972014-01-16 08:20:12 +01002409 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002410 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002411
2412 nq = netdev_get_tx_queue(pp->dev, txq->id);
2413 __netif_tx_lock(nq, smp_processor_id());
2414
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002415 if (txq->count)
2416 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002417
2418 __netif_tx_unlock(nq);
2419 cause_tx_done &= ~((1 << txq->id));
2420 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002421}
2422
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002423/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002424 * according to hw spec, different than generic crc8 algorithm
2425 */
2426static int mvneta_addr_crc(unsigned char *addr)
2427{
2428 int crc = 0;
2429 int i;
2430
2431 for (i = 0; i < ETH_ALEN; i++) {
2432 int j;
2433
2434 crc = (crc ^ addr[i]) << 8;
2435 for (j = 7; j >= 0; j--) {
2436 if (crc & (0x100 << j))
2437 crc ^= 0x107 << j;
2438 }
2439 }
2440
2441 return crc;
2442}
2443
2444/* This method controls the net device special MAC multicast support.
2445 * The Special Multicast Table for MAC addresses supports MAC of the form
2446 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2447 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2448 * Table entries in the DA-Filter table. This method set the Special
2449 * Multicast Table appropriate entry.
2450 */
2451static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2452 unsigned char last_byte,
2453 int queue)
2454{
2455 unsigned int smc_table_reg;
2456 unsigned int tbl_offset;
2457 unsigned int reg_offset;
2458
2459 /* Register offset from SMC table base */
2460 tbl_offset = (last_byte / 4);
2461 /* Entry offset within the above reg */
2462 reg_offset = last_byte % 4;
2463
2464 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2465 + tbl_offset * 4));
2466
2467 if (queue == -1)
2468 smc_table_reg &= ~(0xff << (8 * reg_offset));
2469 else {
2470 smc_table_reg &= ~(0xff << (8 * reg_offset));
2471 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2472 }
2473
2474 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2475 smc_table_reg);
2476}
2477
2478/* This method controls the network device Other MAC multicast support.
2479 * The Other Multicast Table is used for multicast of another type.
2480 * A CRC-8 is used as an index to the Other Multicast Table entries
2481 * in the DA-Filter table.
2482 * The method gets the CRC-8 value from the calling routine and
2483 * sets the Other Multicast Table appropriate entry according to the
2484 * specified CRC-8 .
2485 */
2486static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2487 unsigned char crc8,
2488 int queue)
2489{
2490 unsigned int omc_table_reg;
2491 unsigned int tbl_offset;
2492 unsigned int reg_offset;
2493
2494 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2495 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2496
2497 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2498
2499 if (queue == -1) {
2500 /* Clear accepts frame bit at specified Other DA table entry */
2501 omc_table_reg &= ~(0xff << (8 * reg_offset));
2502 } else {
2503 omc_table_reg &= ~(0xff << (8 * reg_offset));
2504 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2505 }
2506
2507 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2508}
2509
2510/* The network device supports multicast using two tables:
2511 * 1) Special Multicast Table for MAC addresses of the form
2512 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2513 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2514 * Table entries in the DA-Filter table.
2515 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2516 * is used as an index to the Other Multicast Table entries in the
2517 * DA-Filter table.
2518 */
2519static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2520 int queue)
2521{
2522 unsigned char crc_result = 0;
2523
2524 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2525 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2526 return 0;
2527 }
2528
2529 crc_result = mvneta_addr_crc(p_addr);
2530 if (queue == -1) {
2531 if (pp->mcast_count[crc_result] == 0) {
2532 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2533 crc_result);
2534 return -EINVAL;
2535 }
2536
2537 pp->mcast_count[crc_result]--;
2538 if (pp->mcast_count[crc_result] != 0) {
2539 netdev_info(pp->dev,
2540 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2541 pp->mcast_count[crc_result], crc_result);
2542 return -EINVAL;
2543 }
2544 } else
2545 pp->mcast_count[crc_result]++;
2546
2547 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2548
2549 return 0;
2550}
2551
2552/* Configure Fitering mode of Ethernet port */
2553static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2554 int is_promisc)
2555{
2556 u32 port_cfg_reg, val;
2557
2558 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2559
2560 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2561
2562 /* Set / Clear UPM bit in port configuration register */
2563 if (is_promisc) {
2564 /* Accept all Unicast addresses */
2565 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2566 val |= MVNETA_FORCE_UNI;
2567 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2568 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2569 } else {
2570 /* Reject all Unicast addresses */
2571 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2572 val &= ~MVNETA_FORCE_UNI;
2573 }
2574
2575 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2576 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2577}
2578
2579/* register unicast and multicast addresses */
2580static void mvneta_set_rx_mode(struct net_device *dev)
2581{
2582 struct mvneta_port *pp = netdev_priv(dev);
2583 struct netdev_hw_addr *ha;
2584
2585 if (dev->flags & IFF_PROMISC) {
2586 /* Accept all: Multicast + Unicast */
2587 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002588 mvneta_set_ucast_table(pp, pp->rxq_def);
2589 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2590 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002591 } else {
2592 /* Accept single Unicast */
2593 mvneta_rx_unicast_promisc_set(pp, 0);
2594 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002595 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002596
2597 if (dev->flags & IFF_ALLMULTI) {
2598 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002599 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2600 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002601 } else {
2602 /* Accept only initialized multicast */
2603 mvneta_set_special_mcast_table(pp, -1);
2604 mvneta_set_other_mcast_table(pp, -1);
2605
2606 if (!netdev_mc_empty(dev)) {
2607 netdev_for_each_mc_addr(ha, dev) {
2608 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002609 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002610 }
2611 }
2612 }
2613 }
2614}
2615
2616/* Interrupt handling - the callback for request_irq() */
2617static irqreturn_t mvneta_isr(int irq, void *dev_id)
2618{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002619 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002620
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002621 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002622 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002623
2624 return IRQ_HANDLED;
2625}
2626
Stas Sergeev898b2972015-04-01 20:32:49 +03002627static int mvneta_fixed_link_update(struct mvneta_port *pp,
2628 struct phy_device *phy)
2629{
2630 struct fixed_phy_status status;
2631 struct fixed_phy_status changed = {};
2632 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2633
2634 status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2635 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2636 status.speed = SPEED_1000;
2637 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2638 status.speed = SPEED_100;
2639 else
2640 status.speed = SPEED_10;
2641 status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2642 changed.link = 1;
2643 changed.speed = 1;
2644 changed.duplex = 1;
2645 fixed_phy_update_state(phy, &status, &changed);
2646 return 0;
2647}
2648
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002649/* NAPI handler
2650 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2651 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2652 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2653 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2654 * Each CPU has its own causeRxTx register
2655 */
2656static int mvneta_poll(struct napi_struct *napi, int budget)
2657{
2658 int rx_done = 0;
2659 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002660 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002661 struct mvneta_port *pp = netdev_priv(napi->dev);
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002662 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002663 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002664
2665 if (!netif_running(pp->dev)) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002666 napi_complete(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002667 return rx_done;
2668 }
2669
2670 /* Read cause register */
Stas Sergeev898b2972015-04-01 20:32:49 +03002671 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2672 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2673 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2674
2675 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2676 if (pp->use_inband_status && (cause_misc &
2677 (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2678 MVNETA_CAUSE_LINK_CHANGE |
2679 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002680 mvneta_fixed_link_update(pp, ndev->phydev);
Stas Sergeev898b2972015-04-01 20:32:49 +03002681 }
2682 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002683
2684 /* Release Tx descriptors */
2685 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002686 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002687 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2688 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002689
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002690 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002691 * RX packets
2692 */
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002693 cause_rx_tx |= port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002694
Jisheng Zhangdabbf0e2020-03-16 22:56:36 +08002695 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002696 if (rx_queue) {
2697 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002698 if (pp->bm_priv)
2699 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2700 else
2701 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002702 }
2703
Maxime Ripardd8936652015-09-25 18:09:37 +02002704 budget -= rx_done;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002705
2706 if (budget > 0) {
2707 cause_rx_tx = 0;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002708 napi_complete(&port->napi);
2709 enable_percpu_irq(pp->dev->irq, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002710 }
2711
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002712 port->cause_rx_tx = cause_rx_tx;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002713 return rx_done;
2714}
2715
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002716/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2717static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2718 int num)
2719{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002720 int i;
2721
2722 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002723 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2724 if (mvneta_rx_refill(pp, rxq->descs + i) != 0) {
2725 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002726 __func__, rxq->id, i, num);
2727 break;
2728 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002729 }
2730
2731 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002732 * get packets)
2733 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002734 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2735
2736 return i;
2737}
2738
2739/* Free all packets pending transmit from all TXQs and reset TX port */
2740static void mvneta_tx_reset(struct mvneta_port *pp)
2741{
2742 int queue;
2743
Ezequiel Garcia96728502014-05-22 20:06:59 -03002744 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002745 for (queue = 0; queue < txq_number; queue++)
2746 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2747
2748 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2749 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2750}
2751
2752static void mvneta_rx_reset(struct mvneta_port *pp)
2753{
2754 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2755 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2756}
2757
2758/* Rx/Tx queue initialization/cleanup methods */
2759
2760/* Create a specified RX queue */
2761static int mvneta_rxq_init(struct mvneta_port *pp,
2762 struct mvneta_rx_queue *rxq)
2763
2764{
2765 rxq->size = pp->rx_ring_size;
2766
2767 /* Allocate memory for RX descriptors */
2768 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2769 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2770 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002771 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002772 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002773
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002774 rxq->last_desc = rxq->size - 1;
2775
2776 /* Set Rx descriptors queue starting address */
2777 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2778 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2779
2780 /* Set Offset */
2781 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2782
2783 /* Set coalescing pkts and time */
2784 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2785 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2786
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002787 if (!pp->bm_priv) {
2788 /* Fill RXQ with buffers from RX pool */
2789 mvneta_rxq_buf_size_set(pp, rxq,
2790 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2791 mvneta_rxq_bm_disable(pp, rxq);
2792 } else {
2793 mvneta_rxq_bm_enable(pp, rxq);
2794 mvneta_rxq_long_pool_set(pp, rxq);
2795 mvneta_rxq_short_pool_set(pp, rxq);
2796 }
2797
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002798 mvneta_rxq_fill(pp, rxq, rxq->size);
2799
2800 return 0;
2801}
2802
2803/* Cleanup Rx queue */
2804static void mvneta_rxq_deinit(struct mvneta_port *pp,
2805 struct mvneta_rx_queue *rxq)
2806{
2807 mvneta_rxq_drop_pkts(pp, rxq);
2808
2809 if (rxq->descs)
2810 dma_free_coherent(pp->dev->dev.parent,
2811 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2812 rxq->descs,
2813 rxq->descs_phys);
2814
2815 rxq->descs = NULL;
2816 rxq->last_desc = 0;
2817 rxq->next_desc_to_proc = 0;
2818 rxq->descs_phys = 0;
2819}
2820
2821/* Create and initialize a tx queue */
2822static int mvneta_txq_init(struct mvneta_port *pp,
2823 struct mvneta_tx_queue *txq)
2824{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002825 int cpu;
2826
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002827 txq->size = pp->tx_ring_size;
2828
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002829 /* A queue must always have room for at least one skb.
2830 * Therefore, stop the queue when the free entries reaches
2831 * the maximum number of descriptors per skb.
2832 */
2833 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2834 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2835
2836
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002837 /* Allocate memory for TX descriptors */
2838 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2839 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2840 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002841 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002842 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002843
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002844 txq->last_desc = txq->size - 1;
2845
2846 /* Set maximum bandwidth for enabled TXQs */
2847 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2848 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2849
2850 /* Set Tx descriptors queue starting address */
2851 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2852 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2853
2854 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2855 if (txq->tx_skb == NULL) {
2856 dma_free_coherent(pp->dev->dev.parent,
2857 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2858 txq->descs, txq->descs_phys);
2859 return -ENOMEM;
2860 }
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002861
2862 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2863 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2864 txq->size * TSO_HEADER_SIZE,
2865 &txq->tso_hdrs_phys, GFP_KERNEL);
2866 if (txq->tso_hdrs == NULL) {
2867 kfree(txq->tx_skb);
2868 dma_free_coherent(pp->dev->dev.parent,
2869 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2870 txq->descs, txq->descs_phys);
2871 return -ENOMEM;
2872 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002873 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2874
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002875 /* Setup XPS mapping */
2876 if (txq_number > 1)
2877 cpu = txq->id % num_present_cpus();
2878 else
2879 cpu = pp->rxq_def % num_present_cpus();
2880 cpumask_set_cpu(cpu, &txq->affinity_mask);
2881 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2882
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002883 return 0;
2884}
2885
2886/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2887static void mvneta_txq_deinit(struct mvneta_port *pp,
2888 struct mvneta_tx_queue *txq)
2889{
2890 kfree(txq->tx_skb);
2891
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002892 if (txq->tso_hdrs)
2893 dma_free_coherent(pp->dev->dev.parent,
2894 txq->size * TSO_HEADER_SIZE,
2895 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002896 if (txq->descs)
2897 dma_free_coherent(pp->dev->dev.parent,
2898 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2899 txq->descs, txq->descs_phys);
2900
2901 txq->descs = NULL;
2902 txq->last_desc = 0;
2903 txq->next_desc_to_proc = 0;
2904 txq->descs_phys = 0;
2905
2906 /* Set minimum bandwidth for disabled TXQs */
2907 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2908 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2909
2910 /* Set Tx descriptors queue starting address and size */
2911 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2912 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2913}
2914
2915/* Cleanup all Tx queues */
2916static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2917{
2918 int queue;
2919
2920 for (queue = 0; queue < txq_number; queue++)
2921 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2922}
2923
2924/* Cleanup all Rx queues */
2925static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2926{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002927 int queue;
2928
Yelena Krivosheeva57f99f42017-12-19 17:59:46 +01002929 for (queue = 0; queue < rxq_number; queue++)
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002930 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002931}
2932
2933
2934/* Init all Rx queues */
2935static int mvneta_setup_rxqs(struct mvneta_port *pp)
2936{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002937 int queue;
2938
2939 for (queue = 0; queue < rxq_number; queue++) {
2940 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2941
2942 if (err) {
2943 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2944 __func__, queue);
2945 mvneta_cleanup_rxqs(pp);
2946 return err;
2947 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002948 }
2949
2950 return 0;
2951}
2952
2953/* Init all tx queues */
2954static int mvneta_setup_txqs(struct mvneta_port *pp)
2955{
2956 int queue;
2957
2958 for (queue = 0; queue < txq_number; queue++) {
2959 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2960 if (err) {
2961 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2962 __func__, queue);
2963 mvneta_cleanup_txqs(pp);
2964 return err;
2965 }
2966 }
2967
2968 return 0;
2969}
2970
2971static void mvneta_start_dev(struct mvneta_port *pp)
2972{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01002973 int cpu;
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002974 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002975
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002976 mvneta_max_rx_size_set(pp, pp->pkt_size);
2977 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2978
2979 /* start the Rx/Tx activity */
2980 mvneta_port_enable(pp);
2981
2982 /* Enable polling on the port */
Gregory CLEMENT129219e2016-02-04 22:09:23 +01002983 for_each_online_cpu(cpu) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002984 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2985
2986 napi_enable(&port->napi);
2987 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002988
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002989 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01002990 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
2991
Stas Sergeev898b2972015-04-01 20:32:49 +03002992 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2993 MVNETA_CAUSE_PHY_STATUS_CHANGE |
2994 MVNETA_CAUSE_LINK_CHANGE |
2995 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002996
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002997 phy_start(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002998 netif_tx_start_all_queues(pp->dev);
2999}
3000
3001static void mvneta_stop_dev(struct mvneta_port *pp)
3002{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003003 unsigned int cpu;
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003004 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003005
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003006 phy_stop(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003007
Gregory CLEMENT129219e2016-02-04 22:09:23 +01003008 for_each_online_cpu(cpu) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003009 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3010
3011 napi_disable(&port->napi);
3012 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003013
3014 netif_carrier_off(pp->dev);
3015
3016 mvneta_port_down(pp);
3017 netif_tx_stop_all_queues(pp->dev);
3018
3019 /* Stop the port activity */
3020 mvneta_port_disable(pp);
3021
3022 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003023 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003024
3025 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003026 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003027
3028 mvneta_tx_reset(pp);
3029 mvneta_rx_reset(pp);
3030}
3031
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003032/* Return positive if MTU is valid */
3033static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
3034{
3035 if (mtu < 68) {
3036 netdev_err(dev, "cannot change mtu to less than 68\n");
3037 return -EINVAL;
3038 }
3039
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003040 /* 9676 == 9700 - 20 and rounding to 8 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003041 if (mtu > 9676) {
3042 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
3043 mtu = 9676;
3044 }
3045
3046 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3047 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3048 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3049 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3050 }
3051
3052 return mtu;
3053}
3054
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003055static void mvneta_percpu_enable(void *arg)
3056{
3057 struct mvneta_port *pp = arg;
3058
3059 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3060}
3061
3062static void mvneta_percpu_disable(void *arg)
3063{
3064 struct mvneta_port *pp = arg;
3065
3066 disable_percpu_irq(pp->dev->irq);
3067}
3068
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003069/* Change the device mtu */
3070static int mvneta_change_mtu(struct net_device *dev, int mtu)
3071{
3072 struct mvneta_port *pp = netdev_priv(dev);
3073 int ret;
3074
3075 mtu = mvneta_check_mtu_valid(dev, mtu);
3076 if (mtu < 0)
3077 return -EINVAL;
3078
3079 dev->mtu = mtu;
3080
Simon Guinotb65657f2015-06-30 16:20:22 +02003081 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003082 if (pp->bm_priv)
3083 mvneta_bm_update_mtu(pp, mtu);
3084
Simon Guinotb65657f2015-06-30 16:20:22 +02003085 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003086 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003087 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003088
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003089 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003090 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003091 */
3092 mvneta_stop_dev(pp);
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003093 on_each_cpu(mvneta_percpu_disable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003094
3095 mvneta_cleanup_txqs(pp);
3096 mvneta_cleanup_rxqs(pp);
3097
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003098 if (pp->bm_priv)
3099 mvneta_bm_update_mtu(pp, mtu);
3100
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003101 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003102 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3103 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003104
3105 ret = mvneta_setup_rxqs(pp);
3106 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003107 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003108 return ret;
3109 }
3110
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003111 ret = mvneta_setup_txqs(pp);
3112 if (ret) {
3113 netdev_err(dev, "unable to setup txqs after MTU change\n");
3114 return ret;
3115 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003116
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003117 on_each_cpu(mvneta_percpu_enable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003118 mvneta_start_dev(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003119
Simon Guinotb65657f2015-06-30 16:20:22 +02003120 netdev_update_features(dev);
3121
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003122 return 0;
3123}
3124
Simon Guinotb65657f2015-06-30 16:20:22 +02003125static netdev_features_t mvneta_fix_features(struct net_device *dev,
3126 netdev_features_t features)
3127{
3128 struct mvneta_port *pp = netdev_priv(dev);
3129
3130 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3131 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3132 netdev_info(dev,
3133 "Disable IP checksum for MTU greater than %dB\n",
3134 pp->tx_csum_limit);
3135 }
3136
3137 return features;
3138}
3139
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003140/* Get mac address */
3141static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3142{
3143 u32 mac_addr_l, mac_addr_h;
3144
3145 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3146 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3147 addr[0] = (mac_addr_h >> 24) & 0xFF;
3148 addr[1] = (mac_addr_h >> 16) & 0xFF;
3149 addr[2] = (mac_addr_h >> 8) & 0xFF;
3150 addr[3] = mac_addr_h & 0xFF;
3151 addr[4] = (mac_addr_l >> 8) & 0xFF;
3152 addr[5] = mac_addr_l & 0xFF;
3153}
3154
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003155/* Handle setting mac address */
3156static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3157{
3158 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003159 struct sockaddr *sockaddr = addr;
3160 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003161
Ezequiel Garciae68de362014-05-22 20:07:00 -03003162 ret = eth_prepare_mac_addr_change(dev, addr);
3163 if (ret < 0)
3164 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003165 /* Remove previous address table entry */
3166 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3167
3168 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003169 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003170
Ezequiel Garciae68de362014-05-22 20:07:00 -03003171 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003172 return 0;
3173}
3174
3175static void mvneta_adjust_link(struct net_device *ndev)
3176{
3177 struct mvneta_port *pp = netdev_priv(ndev);
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003178 struct phy_device *phydev = ndev->phydev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003179 int status_change = 0;
3180
3181 if (phydev->link) {
3182 if ((pp->speed != phydev->speed) ||
3183 (pp->duplex != phydev->duplex)) {
3184 u32 val;
3185
3186 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3187 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3188 MVNETA_GMAC_CONFIG_GMII_SPEED |
Stas Sergeev898b2972015-04-01 20:32:49 +03003189 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003190
3191 if (phydev->duplex)
3192 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3193
3194 if (phydev->speed == SPEED_1000)
3195 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni4d12bc62014-07-08 10:49:43 +02003196 else if (phydev->speed == SPEED_100)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003197 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3198
3199 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3200
3201 pp->duplex = phydev->duplex;
3202 pp->speed = phydev->speed;
3203 }
3204 }
3205
3206 if (phydev->link != pp->link) {
3207 if (!phydev->link) {
3208 pp->duplex = -1;
3209 pp->speed = 0;
3210 }
3211
3212 pp->link = phydev->link;
3213 status_change = 1;
3214 }
3215
3216 if (status_change) {
3217 if (phydev->link) {
Stas Sergeev898b2972015-04-01 20:32:49 +03003218 if (!pp->use_inband_status) {
3219 u32 val = mvreg_read(pp,
3220 MVNETA_GMAC_AUTONEG_CONFIG);
3221 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3222 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3223 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3224 val);
3225 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003226 mvneta_port_up(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003227 } else {
Stas Sergeev898b2972015-04-01 20:32:49 +03003228 if (!pp->use_inband_status) {
3229 u32 val = mvreg_read(pp,
3230 MVNETA_GMAC_AUTONEG_CONFIG);
3231 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3232 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3233 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3234 val);
3235 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003236 mvneta_port_down(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003237 }
Ezequiel Garcia0089b742014-10-31 12:57:20 -03003238 phy_print_status(phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003239 }
3240}
3241
3242static int mvneta_mdio_probe(struct mvneta_port *pp)
3243{
3244 struct phy_device *phy_dev;
3245
3246 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
3247 pp->phy_interface);
3248 if (!phy_dev) {
3249 netdev_err(pp->dev, "could not find the PHY\n");
3250 return -ENODEV;
3251 }
3252
3253 phy_dev->supported &= PHY_GBIT_FEATURES;
3254 phy_dev->advertising = phy_dev->supported;
3255
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003256 pp->link = 0;
3257 pp->duplex = 0;
3258 pp->speed = 0;
3259
3260 return 0;
3261}
3262
3263static void mvneta_mdio_remove(struct mvneta_port *pp)
3264{
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003265 struct net_device *ndev = pp->dev;
3266
3267 phy_disconnect(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003268}
3269
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003270/* Electing a CPU must be done in an atomic way: it should be done
3271 * after or before the removal/insertion of a CPU and this function is
3272 * not reentrant.
3273 */
Maxime Ripardf8642882015-09-25 18:09:38 +02003274static void mvneta_percpu_elect(struct mvneta_port *pp)
3275{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003276 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003277
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003278 /* Use the cpu associated to the rxq when it is online, in all
3279 * the other cases, use the cpu 0 which can't be offline.
3280 */
3281 if (cpu_online(pp->rxq_def))
3282 elected_cpu = pp->rxq_def;
3283
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003284 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02003285
3286 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003287 int rxq_map = 0, txq_map = 0;
3288 int rxq;
3289
3290 for (rxq = 0; rxq < rxq_number; rxq++)
3291 if ((rxq % max_cpu) == cpu)
3292 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3293
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003294 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003295 /* Map the default receive queue queue to the
3296 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02003297 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003298 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003299
3300 /* We update the TX queue map only if we have one
3301 * queue. In this case we associate the TX queue to
3302 * the CPU bound to the default RX queue
3303 */
3304 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003305 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003306 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3307 else
3308 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3309 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3310
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003311 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3312
3313 /* Update the interrupt mask on each CPU according the
3314 * new mapping
3315 */
3316 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3317 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003318 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003319
Maxime Ripardf8642882015-09-25 18:09:38 +02003320 }
3321};
3322
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003323static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
Maxime Ripardf8642882015-09-25 18:09:38 +02003324{
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003325 int other_cpu;
3326 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3327 node_online);
Maxime Ripardf8642882015-09-25 18:09:38 +02003328 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3329
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003330
3331 spin_lock(&pp->lock);
3332 /*
3333 * Configuring the driver for a new CPU while the driver is
3334 * stopping is racy, so just avoid it.
3335 */
3336 if (pp->is_stopped) {
3337 spin_unlock(&pp->lock);
3338 return 0;
3339 }
3340 netif_tx_stop_all_queues(pp->dev);
3341
3342 /*
3343 * We have to synchronise on tha napi of each CPU except the one
3344 * just being woken up
3345 */
3346 for_each_online_cpu(other_cpu) {
3347 if (other_cpu != cpu) {
3348 struct mvneta_pcpu_port *other_port =
3349 per_cpu_ptr(pp->ports, other_cpu);
3350
3351 napi_synchronize(&other_port->napi);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003352 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003353 }
3354
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003355 /* Mask all ethernet port interrupts */
3356 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3357 napi_enable(&port->napi);
3358
3359 /*
3360 * Enable per-CPU interrupts on the CPU that is
3361 * brought up.
3362 */
3363 mvneta_percpu_enable(pp);
3364
3365 /*
3366 * Enable per-CPU interrupt on the one CPU we care
3367 * about.
3368 */
3369 mvneta_percpu_elect(pp);
3370
3371 /* Unmask all ethernet port interrupts */
3372 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3373 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3374 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3375 MVNETA_CAUSE_LINK_CHANGE |
3376 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3377 netif_tx_start_all_queues(pp->dev);
3378 spin_unlock(&pp->lock);
3379 return 0;
3380}
3381
3382static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3383{
3384 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3385 node_online);
3386 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3387
3388 /*
3389 * Thanks to this lock we are sure that any pending cpu election is
3390 * done.
3391 */
3392 spin_lock(&pp->lock);
3393 /* Mask all ethernet port interrupts */
3394 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3395 spin_unlock(&pp->lock);
3396
3397 napi_synchronize(&port->napi);
3398 napi_disable(&port->napi);
3399 /* Disable per-CPU interrupts on the CPU that is brought down. */
3400 mvneta_percpu_disable(pp);
3401 return 0;
3402}
3403
3404static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3405{
3406 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3407 node_dead);
3408
3409 /* Check if a new CPU must be elected now this on is down */
3410 spin_lock(&pp->lock);
3411 mvneta_percpu_elect(pp);
3412 spin_unlock(&pp->lock);
3413 /* Unmask all ethernet port interrupts */
3414 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3415 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3416 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3417 MVNETA_CAUSE_LINK_CHANGE |
3418 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3419 netif_tx_start_all_queues(pp->dev);
3420 return 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003421}
3422
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003423static int mvneta_open(struct net_device *dev)
3424{
3425 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003426 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003427
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003428 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003429 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3430 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003431
3432 ret = mvneta_setup_rxqs(pp);
3433 if (ret)
3434 return ret;
3435
3436 ret = mvneta_setup_txqs(pp);
3437 if (ret)
3438 goto err_cleanup_rxqs;
3439
3440 /* Connect to port interrupt line */
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003441 ret = request_percpu_irq(pp->dev->irq, mvneta_isr,
3442 MVNETA_DRIVER_NAME, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003443 if (ret) {
3444 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3445 goto err_cleanup_txqs;
3446 }
3447
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003448 /* Enable per-CPU interrupt on all the CPU to handle our RX
3449 * queue interrupts
3450 */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003451 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003452
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003453 pp->is_stopped = false;
Maxime Ripardf8642882015-09-25 18:09:38 +02003454 /* Register a CPU notifier to handle the case where our CPU
3455 * might be taken offline.
3456 */
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003457 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3458 &pp->node_online);
3459 if (ret)
3460 goto err_free_irq;
3461
3462 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3463 &pp->node_dead);
3464 if (ret)
3465 goto err_free_online_hp;
Maxime Ripardf8642882015-09-25 18:09:38 +02003466
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003467 /* In default link is down */
3468 netif_carrier_off(pp->dev);
3469
3470 ret = mvneta_mdio_probe(pp);
3471 if (ret < 0) {
3472 netdev_err(dev, "cannot probe MDIO bus\n");
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003473 goto err_free_dead_hp;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003474 }
3475
3476 mvneta_start_dev(pp);
3477
3478 return 0;
3479
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003480err_free_dead_hp:
3481 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3482 &pp->node_dead);
3483err_free_online_hp:
3484 cpuhp_state_remove_instance_nocalls(online_hpstate, &pp->node_online);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003485err_free_irq:
Russell King - ARM Linux3d8c4532016-06-30 10:36:15 +01003486 on_each_cpu(mvneta_percpu_disable, pp, true);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003487 free_percpu_irq(pp->dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003488err_cleanup_txqs:
3489 mvneta_cleanup_txqs(pp);
3490err_cleanup_rxqs:
3491 mvneta_cleanup_rxqs(pp);
3492 return ret;
3493}
3494
3495/* Stop the port, free port interrupt line */
3496static int mvneta_stop(struct net_device *dev)
3497{
3498 struct mvneta_port *pp = netdev_priv(dev);
3499
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003500 /* Inform that we are stopping so we don't want to setup the
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003501 * driver for new CPUs in the notifiers. The code of the
3502 * notifier for CPU online is protected by the same spinlock,
3503 * so when we get the lock, the notifer work is done.
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003504 */
3505 spin_lock(&pp->lock);
3506 pp->is_stopped = true;
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003507 spin_unlock(&pp->lock);
3508
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003509 mvneta_stop_dev(pp);
3510 mvneta_mdio_remove(pp);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003511
3512 cpuhp_state_remove_instance_nocalls(online_hpstate, &pp->node_online);
3513 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3514 &pp->node_dead);
Gregory CLEMENT129219e2016-02-04 22:09:23 +01003515 on_each_cpu(mvneta_percpu_disable, pp, true);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003516 free_percpu_irq(dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003517 mvneta_cleanup_rxqs(pp);
3518 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003519
3520 return 0;
3521}
3522
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003523static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3524{
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003525 if (!dev->phydev)
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003526 return -ENOTSUPP;
3527
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003528 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003529}
3530
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003531/* Ethtool methods */
3532
Philippe Reynes013ad402016-07-30 17:42:12 +02003533/* Set link ksettings (phy address, speed) for ethtools */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +08003534static int
3535mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3536 const struct ethtool_link_ksettings *cmd)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003537{
Philippe Reynes013ad402016-07-30 17:42:12 +02003538 struct mvneta_port *pp = netdev_priv(ndev);
3539 struct phy_device *phydev = ndev->phydev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003540
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003541 if (!phydev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003542 return -ENODEV;
3543
Philippe Reynes013ad402016-07-30 17:42:12 +02003544 if ((cmd->base.autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003545 u32 val;
3546
Philippe Reynes013ad402016-07-30 17:42:12 +02003547 mvneta_set_autoneg(pp, cmd->base.autoneg == AUTONEG_ENABLE);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003548
Philippe Reynes013ad402016-07-30 17:42:12 +02003549 if (cmd->base.autoneg == AUTONEG_DISABLE) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003550 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3551 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3552 MVNETA_GMAC_CONFIG_GMII_SPEED |
3553 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3554
3555 if (phydev->duplex)
3556 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3557
3558 if (phydev->speed == SPEED_1000)
3559 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3560 else if (phydev->speed == SPEED_100)
3561 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3562
3563 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3564 }
3565
Philippe Reynes013ad402016-07-30 17:42:12 +02003566 pp->use_inband_status = (cmd->base.autoneg == AUTONEG_ENABLE);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003567 netdev_info(pp->dev, "autoneg status set to %i\n",
3568 pp->use_inband_status);
3569
Philippe Reynes013ad402016-07-30 17:42:12 +02003570 if (netif_running(ndev)) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003571 mvneta_port_down(pp);
3572 mvneta_port_up(pp);
3573 }
3574 }
3575
Philippe Reynes013ad402016-07-30 17:42:12 +02003576 return phy_ethtool_ksettings_set(ndev->phydev, cmd);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003577}
3578
3579/* Set interrupt coalescing for ethtools */
3580static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3581 struct ethtool_coalesce *c)
3582{
3583 struct mvneta_port *pp = netdev_priv(dev);
3584 int queue;
3585
3586 for (queue = 0; queue < rxq_number; queue++) {
3587 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3588 rxq->time_coal = c->rx_coalesce_usecs;
3589 rxq->pkts_coal = c->rx_max_coalesced_frames;
3590 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3591 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3592 }
3593
3594 for (queue = 0; queue < txq_number; queue++) {
3595 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3596 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3597 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3598 }
3599
3600 return 0;
3601}
3602
3603/* get coalescing for ethtools */
3604static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3605 struct ethtool_coalesce *c)
3606{
3607 struct mvneta_port *pp = netdev_priv(dev);
3608
3609 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3610 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3611
3612 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3613 return 0;
3614}
3615
3616
3617static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3618 struct ethtool_drvinfo *drvinfo)
3619{
3620 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3621 sizeof(drvinfo->driver));
3622 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3623 sizeof(drvinfo->version));
3624 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3625 sizeof(drvinfo->bus_info));
3626}
3627
3628
3629static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3630 struct ethtool_ringparam *ring)
3631{
3632 struct mvneta_port *pp = netdev_priv(netdev);
3633
3634 ring->rx_max_pending = MVNETA_MAX_RXD;
3635 ring->tx_max_pending = MVNETA_MAX_TXD;
3636 ring->rx_pending = pp->rx_ring_size;
3637 ring->tx_pending = pp->tx_ring_size;
3638}
3639
3640static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3641 struct ethtool_ringparam *ring)
3642{
3643 struct mvneta_port *pp = netdev_priv(dev);
3644
3645 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3646 return -EINVAL;
3647 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3648 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003649
3650 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3651 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3652 if (pp->tx_ring_size != ring->tx_pending)
3653 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3654 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003655
3656 if (netif_running(dev)) {
3657 mvneta_stop(dev);
3658 if (mvneta_open(dev)) {
3659 netdev_err(dev,
3660 "error on opening device after ring param change\n");
3661 return -ENOMEM;
3662 }
3663 }
3664
3665 return 0;
3666}
3667
Russell King9b0cdef2015-10-22 18:37:30 +01003668static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3669 u8 *data)
3670{
3671 if (sset == ETH_SS_STATS) {
3672 int i;
3673
3674 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3675 memcpy(data + i * ETH_GSTRING_LEN,
3676 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3677 }
3678}
3679
3680static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3681{
3682 const struct mvneta_statistic *s;
3683 void __iomem *base = pp->base;
3684 u32 high, low, val;
Jisheng Zhang2c832292016-01-20 16:36:25 +08003685 u64 val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003686 int i;
3687
3688 for (i = 0, s = mvneta_statistics;
3689 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3690 s++, i++) {
Russell King9b0cdef2015-10-22 18:37:30 +01003691 switch (s->type) {
3692 case T_REG_32:
3693 val = readl_relaxed(base + s->offset);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003694 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003695 break;
3696 case T_REG_64:
3697 /* Docs say to read low 32-bit then high */
3698 low = readl_relaxed(base + s->offset);
3699 high = readl_relaxed(base + s->offset + 4);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003700 val64 = (u64)high << 32 | low;
3701 pp->ethtool_stats[i] += val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003702 break;
3703 }
Russell King9b0cdef2015-10-22 18:37:30 +01003704 }
3705}
3706
3707static void mvneta_ethtool_get_stats(struct net_device *dev,
3708 struct ethtool_stats *stats, u64 *data)
3709{
3710 struct mvneta_port *pp = netdev_priv(dev);
3711 int i;
3712
3713 mvneta_ethtool_update_stats(pp);
3714
3715 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3716 *data++ = pp->ethtool_stats[i];
3717}
3718
3719static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3720{
3721 if (sset == ETH_SS_STATS)
3722 return ARRAY_SIZE(mvneta_statistics);
3723 return -EOPNOTSUPP;
3724}
3725
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003726static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3727{
3728 return MVNETA_RSS_LU_TABLE_SIZE;
3729}
3730
3731static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3732 struct ethtool_rxnfc *info,
3733 u32 *rules __always_unused)
3734{
3735 switch (info->cmd) {
3736 case ETHTOOL_GRXRINGS:
3737 info->data = rxq_number;
3738 return 0;
3739 case ETHTOOL_GRXFH:
3740 return -EOPNOTSUPP;
3741 default:
3742 return -EOPNOTSUPP;
3743 }
3744}
3745
3746static int mvneta_config_rss(struct mvneta_port *pp)
3747{
3748 int cpu;
3749 u32 val;
3750
3751 netif_tx_stop_all_queues(pp->dev);
3752
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003753 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003754
3755 /* We have to synchronise on the napi of each CPU */
3756 for_each_online_cpu(cpu) {
3757 struct mvneta_pcpu_port *pcpu_port =
3758 per_cpu_ptr(pp->ports, cpu);
3759
3760 napi_synchronize(&pcpu_port->napi);
3761 napi_disable(&pcpu_port->napi);
3762 }
3763
3764 pp->rxq_def = pp->indir[0];
3765
3766 /* Update unicast mapping */
3767 mvneta_set_rx_mode(pp->dev);
3768
3769 /* Update val of portCfg register accordingly with all RxQueue types */
3770 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3771 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3772
3773 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003774 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003775 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003776 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003777
3778 /* We have to synchronise on the napi of each CPU */
3779 for_each_online_cpu(cpu) {
3780 struct mvneta_pcpu_port *pcpu_port =
3781 per_cpu_ptr(pp->ports, cpu);
3782
3783 napi_enable(&pcpu_port->napi);
3784 }
3785
3786 netif_tx_start_all_queues(pp->dev);
3787
3788 return 0;
3789}
3790
3791static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3792 const u8 *key, const u8 hfunc)
3793{
3794 struct mvneta_port *pp = netdev_priv(dev);
3795 /* We require at least one supported parameter to be changed
3796 * and no change in any of the unsupported parameters
3797 */
3798 if (key ||
3799 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3800 return -EOPNOTSUPP;
3801
3802 if (!indir)
3803 return 0;
3804
3805 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3806
3807 return mvneta_config_rss(pp);
3808}
3809
3810static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3811 u8 *hfunc)
3812{
3813 struct mvneta_port *pp = netdev_priv(dev);
3814
3815 if (hfunc)
3816 *hfunc = ETH_RSS_HASH_TOP;
3817
3818 if (!indir)
3819 return 0;
3820
3821 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3822
3823 return 0;
3824}
3825
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003826static const struct net_device_ops mvneta_netdev_ops = {
3827 .ndo_open = mvneta_open,
3828 .ndo_stop = mvneta_stop,
3829 .ndo_start_xmit = mvneta_tx,
3830 .ndo_set_rx_mode = mvneta_set_rx_mode,
3831 .ndo_set_mac_address = mvneta_set_mac_addr,
3832 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02003833 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003834 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003835 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003836};
3837
3838const struct ethtool_ops mvneta_eth_tool_ops = {
3839 .get_link = ethtool_op_get_link,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003840 .set_coalesce = mvneta_ethtool_set_coalesce,
3841 .get_coalesce = mvneta_ethtool_get_coalesce,
3842 .get_drvinfo = mvneta_ethtool_get_drvinfo,
3843 .get_ringparam = mvneta_ethtool_get_ringparam,
3844 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King9b0cdef2015-10-22 18:37:30 +01003845 .get_strings = mvneta_ethtool_get_strings,
3846 .get_ethtool_stats = mvneta_ethtool_get_stats,
3847 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003848 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
3849 .get_rxnfc = mvneta_ethtool_get_rxnfc,
3850 .get_rxfh = mvneta_ethtool_get_rxfh,
3851 .set_rxfh = mvneta_ethtool_set_rxfh,
Philippe Reynes013ad402016-07-30 17:42:12 +02003852 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3853 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003854};
3855
3856/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03003857static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003858{
3859 int queue;
3860
3861 /* Disable port */
3862 mvneta_port_disable(pp);
3863
3864 /* Set port default values */
3865 mvneta_defaults_set(pp);
3866
Ezequiel Garcia96728502014-05-22 20:06:59 -03003867 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
3868 GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003869 if (!pp->txqs)
3870 return -ENOMEM;
3871
3872 /* Initialize TX descriptor rings */
3873 for (queue = 0; queue < txq_number; queue++) {
3874 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3875 txq->id = queue;
3876 txq->size = pp->tx_ring_size;
3877 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
3878 }
3879
Ezequiel Garcia96728502014-05-22 20:06:59 -03003880 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
3881 GFP_KERNEL);
3882 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003883 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003884
3885 /* Create Rx descriptor rings */
3886 for (queue = 0; queue < rxq_number; queue++) {
3887 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3888 rxq->id = queue;
3889 rxq->size = pp->rx_ring_size;
3890 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
3891 rxq->time_coal = MVNETA_RX_COAL_USEC;
3892 }
3893
3894 return 0;
3895}
3896
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003897/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00003898static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
3899 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003900{
3901 u32 win_enable;
3902 u32 win_protect;
3903 int i;
3904
3905 for (i = 0; i < 6; i++) {
3906 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
3907 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
3908
3909 if (i < 4)
3910 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
3911 }
3912
3913 win_enable = 0x3f;
3914 win_protect = 0;
3915
3916 for (i = 0; i < dram->num_cs; i++) {
3917 const struct mbus_dram_window *cs = dram->cs + i;
3918 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
3919 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
3920
3921 mvreg_write(pp, MVNETA_WIN_SIZE(i),
3922 (cs->size - 1) & 0xffff0000);
3923
3924 win_enable &= ~(1 << i);
3925 win_protect |= 3 << (2 * i);
3926 }
3927
3928 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01003929 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003930}
3931
3932/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003933static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003934{
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003935 u32 ctrl;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003936
3937 /* MAC Cause register should be cleared */
3938 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
3939
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003940 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003941
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003942 /* Even though it might look weird, when we're configured in
3943 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3944 */
3945 switch(phy_mode) {
3946 case PHY_INTERFACE_MODE_QSGMII:
3947 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
3948 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
3949 break;
3950 case PHY_INTERFACE_MODE_SGMII:
3951 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
3952 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
3953 break;
3954 case PHY_INTERFACE_MODE_RGMII:
3955 case PHY_INTERFACE_MODE_RGMII_ID:
3956 ctrl |= MVNETA_GMAC2_PORT_RGMII;
3957 break;
3958 default:
3959 return -EINVAL;
3960 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003961
3962 /* Cancel Port Reset */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003963 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
3964 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003965
3966 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3967 MVNETA_GMAC2_PORT_RESET) != 0)
3968 continue;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003969
3970 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003971}
3972
3973/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00003974static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003975{
3976 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01003977 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003978 struct device_node *dn = pdev->dev.of_node;
3979 struct device_node *phy_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003980 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003981 struct mvneta_port *pp;
3982 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003983 const char *dt_mac_addr;
3984 char hw_mac_addr[ETH_ALEN];
3985 const char *mac_from;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07003986 const char *managed;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01003987 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003988 int phy_mode;
3989 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003990 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003991
Willy Tarreauee40a112013-04-11 23:00:37 +02003992 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003993 if (!dev)
3994 return -ENOMEM;
3995
3996 dev->irq = irq_of_parse_and_map(dn, 0);
3997 if (dev->irq == 0) {
3998 err = -EINVAL;
3999 goto err_free_netdev;
4000 }
4001
4002 phy_node = of_parse_phandle(dn, "phy", 0);
4003 if (!phy_node) {
Thomas Petazzoni83895be2014-05-16 16:14:06 +02004004 if (!of_phy_is_fixed_link(dn)) {
4005 dev_err(&pdev->dev, "no PHY specified\n");
4006 err = -ENODEV;
4007 goto err_free_irq;
4008 }
4009
4010 err = of_phy_register_fixed_link(dn);
4011 if (err < 0) {
4012 dev_err(&pdev->dev, "cannot register fixed PHY\n");
4013 goto err_free_irq;
4014 }
4015
4016 /* In the case of a fixed PHY, the DT node associated
4017 * to the PHY is the Ethernet MAC DT node.
4018 */
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004019 phy_node = of_node_get(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004020 }
4021
4022 phy_mode = of_get_phy_mode(dn);
4023 if (phy_mode < 0) {
4024 dev_err(&pdev->dev, "incorrect phy-mode\n");
4025 err = -EINVAL;
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004026 goto err_put_phy_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004027 }
4028
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004029 dev->tx_queue_len = MVNETA_MAX_TXD;
4030 dev->watchdog_timeo = 5 * HZ;
4031 dev->netdev_ops = &mvneta_netdev_ops;
4032
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004033 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004034
4035 pp = netdev_priv(dev);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01004036 spin_lock_init(&pp->lock);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004037 pp->phy_node = phy_node;
4038 pp->phy_interface = phy_mode;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07004039
4040 err = of_property_read_string(dn, "managed", &managed);
4041 pp->use_inband_status = (err == 0 &&
4042 strcmp(managed, "in-band-status") == 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004043
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01004044 pp->rxq_def = rxq_def;
4045
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004046 pp->indir[0] = rxq_def;
4047
Jisheng Zhang2804ba42016-01-20 19:27:23 +08004048 pp->clk = devm_clk_get(&pdev->dev, "core");
4049 if (IS_ERR(pp->clk))
4050 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004051 if (IS_ERR(pp->clk)) {
4052 err = PTR_ERR(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004053 goto err_put_phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004054 }
4055
4056 clk_prepare_enable(pp->clk);
4057
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004058 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4059 if (!IS_ERR(pp->clk_bus))
4060 clk_prepare_enable(pp->clk_bus);
4061
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004062 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4063 pp->base = devm_ioremap_resource(&pdev->dev, res);
4064 if (IS_ERR(pp->base)) {
4065 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004066 goto err_clk;
4067 }
4068
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004069 /* Alloc per-cpu port structure */
4070 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4071 if (!pp->ports) {
4072 err = -ENOMEM;
4073 goto err_clk;
4074 }
4075
willy tarreau74c41b02014-01-16 08:20:08 +01004076 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08004077 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01004078 if (!pp->stats) {
4079 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004080 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01004081 }
4082
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004083 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01004084 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004085 mac_from = "device tree";
4086 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4087 } else {
4088 mvneta_get_mac_addr(pp, hw_mac_addr);
4089 if (is_valid_ether_addr(hw_mac_addr)) {
4090 mac_from = "hardware";
4091 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4092 } else {
4093 mac_from = "random";
4094 eth_hw_addr_random(dev);
4095 }
4096 }
4097
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004098 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4099 if (tx_csum_limit < 0 ||
4100 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4101 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4102 dev_info(&pdev->dev,
4103 "Wrong TX csum limit in DT, set to %dB\n",
4104 MVNETA_TX_CSUM_DEF_SIZE);
4105 }
4106 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4107 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4108 } else {
4109 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4110 }
4111
4112 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02004113
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004114 dram_target_info = mv_mbus_dram_info();
4115 if (dram_target_info)
4116 mvneta_conf_mbus_windows(pp, dram_target_info);
4117
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004118 pp->tx_ring_size = MVNETA_MAX_TXD;
4119 pp->rx_ring_size = MVNETA_MAX_RXD;
4120
4121 pp->dev = dev;
4122 SET_NETDEV_DEV(dev, &pdev->dev);
4123
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004124 pp->id = global_port_id++;
4125
4126 /* Obtain access to BM resources if enabled and already initialized */
4127 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4128 if (bm_node && bm_node->data) {
4129 pp->bm_priv = bm_node->data;
4130 err = mvneta_bm_port_init(pdev, pp);
4131 if (err < 0) {
4132 dev_info(&pdev->dev, "use SW buffer management\n");
4133 pp->bm_priv = NULL;
4134 }
4135 }
Peter Chend4e4da02016-08-01 15:02:36 +08004136 of_node_put(bm_node);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004137
Ezequiel Garcia96728502014-05-22 20:06:59 -03004138 err = mvneta_init(&pdev->dev, pp);
4139 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004140 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004141
4142 err = mvneta_port_power_up(pp, phy_mode);
4143 if (err < 0) {
4144 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004145 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004146 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004147
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004148 for_each_present_cpu(cpu) {
4149 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4150
4151 netif_napi_add(dev, &port->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4152 port->pp = pp;
4153 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004154
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03004155 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03004156 dev->hw_features |= dev->features;
4157 dev->vlan_features |= dev->features;
Andrew Lunn97db8af2016-11-24 00:08:13 +01004158 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004159 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00004160
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004161 err = register_netdev(dev);
4162 if (err < 0) {
4163 dev_err(&pdev->dev, "failed to register\n");
Jisheng Zhangc8e0c412019-05-27 11:04:17 +00004164 goto err_netdev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004165 }
4166
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004167 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4168 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004169
4170 platform_set_drvdata(pdev, pp->dev);
4171
Stas Sergeev898b2972015-04-01 20:32:49 +03004172 if (pp->use_inband_status) {
4173 struct phy_device *phy = of_phy_find_device(dn);
4174
4175 mvneta_fixed_link_update(pp, phy);
Russell King04d53b22015-09-24 20:36:18 +01004176
Andrew Lunne5a03bf2016-01-06 20:11:16 +01004177 put_device(&phy->mdio.dev);
Stas Sergeev898b2972015-04-01 20:32:49 +03004178 }
4179
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004180 return 0;
4181
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004182err_netdev:
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004183 if (pp->bm_priv) {
4184 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4185 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4186 1 << pp->id);
4187 }
willy tarreau74c41b02014-01-16 08:20:08 +01004188 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004189err_free_ports:
4190 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004191err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004192 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004193 clk_disable_unprepare(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004194err_put_phy_node:
4195 of_node_put(phy_node);
Johan Hovold5a57a302016-11-28 19:25:04 +01004196 if (of_phy_is_fixed_link(dn))
4197 of_phy_deregister_fixed_link(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004198err_free_irq:
4199 irq_dispose_mapping(dev->irq);
4200err_free_netdev:
4201 free_netdev(dev);
4202 return err;
4203}
4204
4205/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00004206static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004207{
4208 struct net_device *dev = platform_get_drvdata(pdev);
Johan Hovold5a57a302016-11-28 19:25:04 +01004209 struct device_node *dn = pdev->dev.of_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004210 struct mvneta_port *pp = netdev_priv(dev);
4211
4212 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004213 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004214 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004215 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01004216 free_percpu(pp->stats);
Johan Hovold5a57a302016-11-28 19:25:04 +01004217 if (of_phy_is_fixed_link(dn))
4218 of_phy_deregister_fixed_link(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004219 irq_dispose_mapping(dev->irq);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004220 of_node_put(pp->phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004221 free_netdev(dev);
4222
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004223 if (pp->bm_priv) {
4224 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4225 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4226 1 << pp->id);
4227 }
4228
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004229 return 0;
4230}
4231
4232static const struct of_device_id mvneta_match[] = {
4233 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02004234 { .compatible = "marvell,armada-xp-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004235 { }
4236};
4237MODULE_DEVICE_TABLE(of, mvneta_match);
4238
4239static struct platform_driver mvneta_driver = {
4240 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00004241 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004242 .driver = {
4243 .name = MVNETA_DRIVER_NAME,
4244 .of_match_table = mvneta_match,
4245 },
4246};
4247
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004248static int __init mvneta_driver_init(void)
4249{
4250 int ret;
4251
4252 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4253 mvneta_cpu_online,
4254 mvneta_cpu_down_prepare);
4255 if (ret < 0)
4256 goto out;
4257 online_hpstate = ret;
4258 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4259 NULL, mvneta_cpu_dead);
4260 if (ret)
4261 goto err_dead;
4262
4263 ret = platform_driver_register(&mvneta_driver);
4264 if (ret)
4265 goto err;
4266 return 0;
4267
4268err:
4269 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4270err_dead:
4271 cpuhp_remove_multi_state(online_hpstate);
4272out:
4273 return ret;
4274}
4275module_init(mvneta_driver_init);
4276
4277static void __exit mvneta_driver_exit(void)
4278{
4279 platform_driver_unregister(&mvneta_driver);
4280 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4281 cpuhp_remove_multi_state(online_hpstate);
4282}
4283module_exit(mvneta_driver_exit);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004284
4285MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4286MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4287MODULE_LICENSE("GPL");
4288
4289module_param(rxq_number, int, S_IRUGO);
4290module_param(txq_number, int, S_IRUGO);
4291
4292module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01004293module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);