blob: 7e586e6bfb18a96142db19379d5a2344bacca6f3 [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>
Jisheng Zhang0e03f562016-01-20 19:27:22 +080031#include <linux/platform_device.h>
32#include <linux/skbuff.h>
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +010033#include <net/hwbm.h>
Marcin Wojtasdc35a102016-03-14 09:39:03 +010034#include "mvneta_bm.h"
Jisheng Zhang0e03f562016-01-20 19:27:22 +080035#include <net/ip.h>
36#include <net/ipv6.h>
37#include <net/tso.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030038
39/* Registers */
40#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
Marcin Wojtase5bdf682015-11-30 13:27:42 +010041#define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +010042#define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
43#define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
44#define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
45#define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
Thomas Petazzonic5aff182012-08-17 14:04:28 +030046#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
47#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
48#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
49#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
50#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
51#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
52#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
53#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
54#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
55#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
56#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
57#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
58#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
Marcin Wojtasdc35a102016-03-14 09:39:03 +010059#define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
60#define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
61#define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
Thomas Petazzonic5aff182012-08-17 14:04:28 +030062#define MVNETA_PORT_RX_RESET 0x1cc0
63#define MVNETA_PORT_RX_DMA_RESET BIT(0)
64#define MVNETA_PHY_ADDR 0x2000
65#define MVNETA_PHY_ADDR_MASK 0x1f
66#define MVNETA_MBUS_RETRY 0x2010
67#define MVNETA_UNIT_INTR_CAUSE 0x2080
68#define MVNETA_UNIT_CONTROL 0x20B0
69#define MVNETA_PHY_POLLING_ENABLE BIT(1)
70#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
71#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
72#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
73#define MVNETA_BASE_ADDR_ENABLE 0x2290
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +010074#define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
Thomas Petazzonic5aff182012-08-17 14:04:28 +030075#define MVNETA_PORT_CONFIG 0x2400
76#define MVNETA_UNI_PROMISC_MODE BIT(0)
77#define MVNETA_DEF_RXQ(q) ((q) << 1)
78#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
79#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
80#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
81#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
82#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
83#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
84#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
85 MVNETA_DEF_RXQ_ARP(q) | \
86 MVNETA_DEF_RXQ_TCP(q) | \
87 MVNETA_DEF_RXQ_UDP(q) | \
88 MVNETA_DEF_RXQ_BPDU(q) | \
89 MVNETA_TX_UNSET_ERR_SUM | \
90 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
91#define MVNETA_PORT_CONFIG_EXTEND 0x2404
92#define MVNETA_MAC_ADDR_LOW 0x2414
93#define MVNETA_MAC_ADDR_HIGH 0x2418
94#define MVNETA_SDMA_CONFIG 0x241c
95#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030096#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
97#define MVNETA_RX_NO_DATA_SWAP BIT(4)
98#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +020099#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300100#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
101#define MVNETA_PORT_STATUS 0x2444
102#define MVNETA_TX_IN_PRGRS BIT(1)
103#define MVNETA_TX_FIFO_EMPTY BIT(8)
104#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200105#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200106#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200107#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300108#define MVNETA_TYPE_PRIO 0x24bc
109#define MVNETA_FORCE_UNI BIT(21)
110#define MVNETA_TXQ_CMD_1 0x24e4
111#define MVNETA_TXQ_CMD 0x2448
112#define MVNETA_TXQ_DISABLE_SHIFT 8
113#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Andrew Lunne4839112015-10-22 18:37:36 +0100114#define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
115#define MVNETA_OVERRUN_FRAME_COUNT 0x2488
Stas Sergeev898b29702015-04-01 20:32:49 +0300116#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
117#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300118#define MVNETA_ACC_MODE 0x2500
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100119#define MVNETA_BM_ADDRESS 0x2504
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300120#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
121#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
122#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100123#define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100124#define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300125#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100126
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100127/* Exception Interrupt Port/Queue Cause register
128 *
129 * Their behavior depend of the mapping done using the PCPX2Q
130 * registers. For a given CPU if the bit associated to a queue is not
131 * set, then for the register a read from this CPU will always return
132 * 0 and a write won't do anything
133 */
willy tarreau40ba35e2014-01-16 08:20:10 +0100134
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300135#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300136#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100137
138/* bits 0..7 = TXQ SENT, one bit per queue.
139 * bits 8..15 = RXQ OCCUP, one bit per queue.
140 * bits 16..23 = RXQ FREE, one bit per queue.
141 * bit 29 = OLD_REG_SUM, see old reg ?
142 * bit 30 = TX_ERR_SUM, one bit for 4 ports
143 * bit 31 = MISC_SUM, one bit for 4 ports
144 */
145#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
146#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
147#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
148#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b29702015-04-01 20:32:49 +0300149#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100150
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300151#define MVNETA_INTR_OLD_CAUSE 0x25a8
152#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100153
154/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300155#define MVNETA_INTR_MISC_CAUSE 0x25b0
156#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100157
158#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
159#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
160#define MVNETA_CAUSE_PTP BIT(4)
161
162#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
163#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
164#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
165#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
166#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
167#define MVNETA_CAUSE_PRBS_ERR BIT(12)
168#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
169#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
170
171#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
172#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
173#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
174
175#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
176#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
177#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
178
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300179#define MVNETA_INTR_ENABLE 0x25b8
180#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
Marcin Wojtasdc1aadf2015-11-30 13:27:43 +0100181#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
willy tarreau40ba35e2014-01-16 08:20:10 +0100182
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300183#define MVNETA_RXQ_CMD 0x2680
184#define MVNETA_RXQ_DISABLE_SHIFT 8
185#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
186#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
187#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
188#define MVNETA_GMAC_CTRL_0 0x2c00
189#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
190#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
191#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
192#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b29702015-04-01 20:32:49 +0300193#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100194#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300195#define MVNETA_GMAC2_PORT_RGMII BIT(4)
196#define MVNETA_GMAC2_PORT_RESET BIT(6)
197#define MVNETA_GMAC_STATUS 0x2c10
198#define MVNETA_GMAC_LINK_UP BIT(0)
199#define MVNETA_GMAC_SPEED_1000 BIT(1)
200#define MVNETA_GMAC_SPEED_100 BIT(2)
201#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
202#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
203#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
204#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
205#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
206#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
207#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
208#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b29702015-04-01 20:32:49 +0300209#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300210#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
211#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200212#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Stas Sergeev898b29702015-04-01 20:32:49 +0300213#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300214#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200215#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Andrew Lunne4839112015-10-22 18:37:36 +0100216#define MVNETA_MIB_COUNTERS_BASE 0x3000
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300217#define MVNETA_MIB_LATE_COLLISION 0x7c
218#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
219#define MVNETA_DA_FILT_OTH_MCAST 0x3500
220#define MVNETA_DA_FILT_UCAST_BASE 0x3600
221#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
222#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
223#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
224#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
225#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
226#define MVNETA_TXQ_DEC_SENT_SHIFT 16
227#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
228#define MVNETA_TXQ_SENT_DESC_SHIFT 16
229#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
230#define MVNETA_PORT_TX_RESET 0x3cf0
231#define MVNETA_PORT_TX_DMA_RESET BIT(0)
232#define MVNETA_TX_MTU 0x3e0c
233#define MVNETA_TX_TOKEN_SIZE 0x3e14
234#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
235#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
236#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
237
238#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
239
240/* Descriptor ring Macros */
241#define MVNETA_QUEUE_NEXT_DESC(q, index) \
242 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
243
244/* Various constants */
245
246/* Coalescing */
Dmitri Epshtein06708f82016-07-06 04:18:58 +0200247#define MVNETA_TXDONE_COAL_PKTS 0 /* interrupt per packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300248#define MVNETA_RX_COAL_PKTS 32
249#define MVNETA_RX_COAL_USEC 100
250
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100251/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300252 * by Marvell switches when a specific hardware mode is enabled (not
253 * supported by this driver) or is filled automatically by zeroes on
254 * the RX side. Those two bytes being at the front of the Ethernet
255 * header, they allow to have the IP header aligned on a 4 bytes
256 * boundary automatically: the hardware skips those two bytes on its
257 * own.
258 */
259#define MVNETA_MH_SIZE 2
260
261#define MVNETA_VLAN_TAG_LEN 4
262
Marcin Wojtas9110ee02015-11-30 13:27:45 +0100263#define MVNETA_TX_CSUM_DEF_SIZE 1600
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300264#define MVNETA_TX_CSUM_MAX_SIZE 9800
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100265#define MVNETA_ACC_MODE_EXT1 1
266#define MVNETA_ACC_MODE_EXT2 2
267
268#define MVNETA_MAX_DECODE_WIN 6
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300269
270/* Timeout constants */
271#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
272#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
273#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
274
275#define MVNETA_TX_MTU_MAX 0x3ffff
276
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100277/* The RSS lookup table actually has 256 entries but we do not use
278 * them yet
279 */
280#define MVNETA_RSS_LU_TABLE_SIZE 1
281
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300282/* TSO header size */
283#define TSO_HEADER_SIZE 128
284
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300285/* Max number of Rx descriptors */
286#define MVNETA_MAX_RXD 128
287
288/* Max number of Tx descriptors */
289#define MVNETA_MAX_TXD 532
290
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300291/* Max number of allowed TCP segments for software TSO */
292#define MVNETA_MAX_TSO_SEGS 100
293
294#define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
295
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300296/* descriptor aligned size */
297#define MVNETA_DESC_ALIGNED_SIZE 32
298
299#define MVNETA_RX_PKT_SIZE(mtu) \
300 ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
301 ETH_HLEN + ETH_FCS_LEN, \
Jisheng Zhangc66e98c2016-04-01 17:12:49 +0800302 cache_line_size())
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300303
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -0300304#define IS_TSO_HEADER(txq, addr) \
305 ((addr >= txq->tso_hdrs_phys) && \
306 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
307
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100308#define MVNETA_RX_GET_BM_POOL_ID(rxd) \
309 (((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300310
Russell King9b0cdef2015-10-22 18:37:30 +0100311struct mvneta_statistic {
312 unsigned short offset;
313 unsigned short type;
314 const char name[ETH_GSTRING_LEN];
315};
316
317#define T_REG_32 32
318#define T_REG_64 64
319
320static const struct mvneta_statistic mvneta_statistics[] = {
321 { 0x3000, T_REG_64, "good_octets_received", },
322 { 0x3010, T_REG_32, "good_frames_received", },
323 { 0x3008, T_REG_32, "bad_octets_received", },
324 { 0x3014, T_REG_32, "bad_frames_received", },
325 { 0x3018, T_REG_32, "broadcast_frames_received", },
326 { 0x301c, T_REG_32, "multicast_frames_received", },
327 { 0x3050, T_REG_32, "unrec_mac_control_received", },
328 { 0x3058, T_REG_32, "good_fc_received", },
329 { 0x305c, T_REG_32, "bad_fc_received", },
330 { 0x3060, T_REG_32, "undersize_received", },
331 { 0x3064, T_REG_32, "fragments_received", },
332 { 0x3068, T_REG_32, "oversize_received", },
333 { 0x306c, T_REG_32, "jabber_received", },
334 { 0x3070, T_REG_32, "mac_receive_error", },
335 { 0x3074, T_REG_32, "bad_crc_event", },
336 { 0x3078, T_REG_32, "collision", },
337 { 0x307c, T_REG_32, "late_collision", },
338 { 0x2484, T_REG_32, "rx_discard", },
339 { 0x2488, T_REG_32, "rx_overrun", },
340 { 0x3020, T_REG_32, "frames_64_octets", },
341 { 0x3024, T_REG_32, "frames_65_to_127_octets", },
342 { 0x3028, T_REG_32, "frames_128_to_255_octets", },
343 { 0x302c, T_REG_32, "frames_256_to_511_octets", },
344 { 0x3030, T_REG_32, "frames_512_to_1023_octets", },
345 { 0x3034, T_REG_32, "frames_1024_to_max_octets", },
346 { 0x3038, T_REG_64, "good_octets_sent", },
347 { 0x3040, T_REG_32, "good_frames_sent", },
348 { 0x3044, T_REG_32, "excessive_collision", },
349 { 0x3048, T_REG_32, "multicast_frames_sent", },
350 { 0x304c, T_REG_32, "broadcast_frames_sent", },
351 { 0x3054, T_REG_32, "fc_sent", },
352 { 0x300c, T_REG_32, "internal_mac_transmit_err", },
353};
354
willy tarreau74c41b02014-01-16 08:20:08 +0100355struct mvneta_pcpu_stats {
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300356 struct u64_stats_sync syncp;
willy tarreau74c41b02014-01-16 08:20:08 +0100357 u64 rx_packets;
358 u64 rx_bytes;
359 u64 tx_packets;
360 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300361};
362
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200363struct mvneta_pcpu_port {
364 /* Pointer to the shared port */
365 struct mvneta_port *pp;
366
367 /* Pointer to the CPU-local NAPI struct */
368 struct napi_struct napi;
369
370 /* Cause of the previous interrupt */
371 u32 cause_rx_tx;
372};
373
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300374struct mvneta_port {
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100375 u8 id;
Maxime Ripard12bb03b2015-09-25 18:09:36 +0200376 struct mvneta_pcpu_port __percpu *ports;
377 struct mvneta_pcpu_stats __percpu *stats;
378
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300379 int pkt_size;
willy tarreau8ec2cd42014-01-16 08:20:16 +0100380 unsigned int frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300381 void __iomem *base;
382 struct mvneta_rx_queue *rxqs;
383 struct mvneta_tx_queue *txqs;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300384 struct net_device *dev;
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200385 struct hlist_node node_online;
386 struct hlist_node node_dead;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100387 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100388 /* Protect the access to the percpu interrupt registers,
389 * ensuring that the configuration remains coherent.
390 */
391 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100392 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300393
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300394 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100395 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800396 /* AXI clock */
397 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300398 u8 mcast_count[256];
399 u16 tx_ring_size;
400 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300401
402 struct mii_bus *mii_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300403 phy_interface_t phy_interface;
404 struct device_node *phy_node;
405 unsigned int link;
406 unsigned int duplex;
407 unsigned int speed;
Simon Guinotb65657f2015-06-30 16:20:22 +0200408 unsigned int tx_csum_limit;
Stas Sergeev0c0744f2015-12-02 20:35:11 +0300409 unsigned int use_inband_status:1;
Russell King9b0cdef2015-10-22 18:37:30 +0100410
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100411 struct mvneta_bm *bm_priv;
412 struct mvneta_bm_pool *pool_long;
413 struct mvneta_bm_pool *pool_short;
414 int bm_win_id;
415
Russell King9b0cdef2015-10-22 18:37:30 +0100416 u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
Gregory CLEMENT9a401de2015-12-09 18:23:50 +0100417
418 u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300419};
420
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100421/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300422 * layout of the transmit and reception DMA descriptors, and their
423 * layout is therefore defined by the hardware design
424 */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200425
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300426#define MVNETA_TX_L3_OFF_SHIFT 0
427#define MVNETA_TX_IP_HLEN_SHIFT 8
428#define MVNETA_TX_L4_UDP BIT(16)
429#define MVNETA_TX_L3_IP6 BIT(17)
430#define MVNETA_TXD_IP_CSUM BIT(18)
431#define MVNETA_TXD_Z_PAD BIT(19)
432#define MVNETA_TXD_L_DESC BIT(20)
433#define MVNETA_TXD_F_DESC BIT(21)
434#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
435 MVNETA_TXD_L_DESC | \
436 MVNETA_TXD_F_DESC)
437#define MVNETA_TX_L4_CSUM_FULL BIT(30)
438#define MVNETA_TX_L4_CSUM_NOT BIT(31)
439
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300440#define MVNETA_RXD_ERR_CRC 0x0
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100441#define MVNETA_RXD_BM_POOL_SHIFT 13
442#define MVNETA_RXD_BM_POOL_MASK (BIT(13) | BIT(14))
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300443#define MVNETA_RXD_ERR_SUMMARY BIT(16)
444#define MVNETA_RXD_ERR_OVERRUN BIT(17)
445#define MVNETA_RXD_ERR_LEN BIT(18)
446#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
447#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
448#define MVNETA_RXD_L3_IP4 BIT(25)
449#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
450#define MVNETA_RXD_L4_CSUM_OK BIT(30)
451
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200452#if defined(__LITTLE_ENDIAN)
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200453struct mvneta_tx_desc {
454 u32 command; /* Options used by HW for packet transmitting.*/
455 u16 reserverd1; /* csum_l4 (for future use) */
456 u16 data_size; /* Data size of transmitted packet in bytes */
457 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
458 u32 reserved2; /* hw_cmd - (for future use, PMT) */
459 u32 reserved3[4]; /* Reserved - (for future use) */
460};
461
462struct mvneta_rx_desc {
463 u32 status; /* Info about received packet */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300464 u16 reserved1; /* pnc_info - (for future use, PnC) */
465 u16 data_size; /* Size of received packet in bytes */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200466
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300467 u32 buf_phys_addr; /* Physical address of the buffer */
468 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200469
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300470 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
471 u16 reserved3; /* prefetch_cmd, for future use */
472 u16 reserved4; /* csum_l4 - (for future use, PnC) */
Thomas Petazzoni6083ed42013-07-29 15:21:27 +0200473
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300474 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
475 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
476};
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +0200477#else
478struct mvneta_tx_desc {
479 u16 data_size; /* Data size of transmitted packet in bytes */
480 u16 reserverd1; /* csum_l4 (for future use) */
481 u32 command; /* Options used by HW for packet transmitting.*/
482 u32 reserved2; /* hw_cmd - (for future use, PMT) */
483 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
484 u32 reserved3[4]; /* Reserved - (for future use) */
485};
486
487struct mvneta_rx_desc {
488 u16 data_size; /* Size of received packet in bytes */
489 u16 reserved1; /* pnc_info - (for future use, PnC) */
490 u32 status; /* Info about received packet */
491
492 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
493 u32 buf_phys_addr; /* Physical address of the buffer */
494
495 u16 reserved4; /* csum_l4 - (for future use, PnC) */
496 u16 reserved3; /* prefetch_cmd, for future use */
497 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
498
499 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
500 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
501};
502#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300503
504struct mvneta_tx_queue {
505 /* Number of this TX queue, in the range 0-7 */
506 u8 id;
507
508 /* Number of TX DMA descriptors in the descriptor ring */
509 int size;
510
511 /* Number of currently used TX DMA descriptor in the
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100512 * descriptor ring
513 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300514 int count;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -0300515 int tx_stop_threshold;
516 int tx_wake_threshold;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300517
518 /* Array of transmitted skb */
519 struct sk_buff **tx_skb;
520
521 /* Index of last TX DMA descriptor that was inserted */
522 int txq_put_index;
523
524 /* Index of the TX DMA descriptor to be cleaned up */
525 int txq_get_index;
526
527 u32 done_pkts_coal;
528
529 /* Virtual address of the TX DMA descriptors array */
530 struct mvneta_tx_desc *descs;
531
532 /* DMA address of the TX DMA descriptors array */
533 dma_addr_t descs_phys;
534
535 /* Index of the last TX DMA descriptor */
536 int last_desc;
537
538 /* Index of the next TX DMA descriptor to process */
539 int next_desc_to_proc;
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -0300540
541 /* DMA buffers for TSO headers */
542 char *tso_hdrs;
543
544 /* DMA address of TSO headers */
545 dma_addr_t tso_hdrs_phys;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100546
547 /* Affinity mask for CPUs*/
548 cpumask_t affinity_mask;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300549};
550
551struct mvneta_rx_queue {
552 /* rx queue number, in the range 0-7 */
553 u8 id;
554
555 /* num of rx descriptors in the rx descriptor ring */
556 int size;
557
558 /* counter of times when mvneta_refill() failed */
559 int missed;
560
561 u32 pkts_coal;
562 u32 time_coal;
563
Gregory CLEMENTf88bee12016-12-01 18:03:06 +0100564 /* Virtual address of the RX buffer */
565 void **buf_virt_addr;
566
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300567 /* Virtual address of the RX DMA descriptors array */
568 struct mvneta_rx_desc *descs;
569
570 /* DMA address of the RX DMA descriptors array */
571 dma_addr_t descs_phys;
572
573 /* Index of the last RX DMA descriptor */
574 int last_desc;
575
576 /* Index of the next RX DMA descriptor to process */
577 int next_desc_to_proc;
578};
579
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +0200580static enum cpuhp_state online_hpstate;
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300581/* The hardware supports eight (8) rx queues, but we are only allowing
582 * the first one to be used. Therefore, let's just allocate one queue.
583 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200584static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300585static int txq_number = 8;
586
587static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300588
willy tarreauf19fadf2014-01-16 08:20:17 +0100589static int rx_copybreak __read_mostly = 256;
590
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100591/* HW BM need that each port be identify by a unique ID */
592static int global_port_id;
593
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300594#define MVNETA_DRIVER_NAME "mvneta"
595#define MVNETA_DRIVER_VERSION "1.0"
596
597/* Utility/helper methods */
598
599/* Write helper method */
600static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
601{
602 writel(data, pp->base + offset);
603}
604
605/* Read helper method */
606static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
607{
608 return readl(pp->base + offset);
609}
610
611/* Increment txq get counter */
612static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
613{
614 txq->txq_get_index++;
615 if (txq->txq_get_index == txq->size)
616 txq->txq_get_index = 0;
617}
618
619/* Increment txq put counter */
620static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
621{
622 txq->txq_put_index++;
623 if (txq->txq_put_index == txq->size)
624 txq->txq_put_index = 0;
625}
626
627
628/* Clear all MIB counters */
629static void mvneta_mib_counters_clear(struct mvneta_port *pp)
630{
631 int i;
632 u32 dummy;
633
634 /* Perform dummy reads from MIB counters */
635 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
636 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100637 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
638 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300639}
640
641/* Get System Network Statistics */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +0800642static struct rtnl_link_stats64 *
643mvneta_get_stats64(struct net_device *dev,
644 struct rtnl_link_stats64 *stats)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300645{
646 struct mvneta_port *pp = netdev_priv(dev);
647 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100648 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300649
willy tarreau74c41b02014-01-16 08:20:08 +0100650 for_each_possible_cpu(cpu) {
651 struct mvneta_pcpu_stats *cpu_stats;
652 u64 rx_packets;
653 u64 rx_bytes;
654 u64 tx_packets;
655 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300656
willy tarreau74c41b02014-01-16 08:20:08 +0100657 cpu_stats = per_cpu_ptr(pp->stats, cpu);
658 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700659 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100660 rx_packets = cpu_stats->rx_packets;
661 rx_bytes = cpu_stats->rx_bytes;
662 tx_packets = cpu_stats->tx_packets;
663 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700664 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300665
willy tarreau74c41b02014-01-16 08:20:08 +0100666 stats->rx_packets += rx_packets;
667 stats->rx_bytes += rx_bytes;
668 stats->tx_packets += tx_packets;
669 stats->tx_bytes += tx_bytes;
670 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300671
672 stats->rx_errors = dev->stats.rx_errors;
673 stats->rx_dropped = dev->stats.rx_dropped;
674
675 stats->tx_dropped = dev->stats.tx_dropped;
676
677 return stats;
678}
679
680/* Rx descriptors helper methods */
681
willy tarreau54282132014-01-16 08:20:14 +0100682/* Checks whether the RX descriptor having this status is both the first
683 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300684 * received through a single RX descriptor, so not having each RX
685 * descriptor with its first and last bits set is an error
686 */
willy tarreau54282132014-01-16 08:20:14 +0100687static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300688{
willy tarreau54282132014-01-16 08:20:14 +0100689 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300690 MVNETA_RXD_FIRST_LAST_DESC;
691}
692
693/* Add number of descriptors ready to receive new packets */
694static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
695 struct mvneta_rx_queue *rxq,
696 int ndescs)
697{
698 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100699 * be added at once
700 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300701 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
702 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
703 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
704 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
705 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
706 }
707
708 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
709 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
710}
711
712/* Get number of RX descriptors occupied by received packets */
713static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
714 struct mvneta_rx_queue *rxq)
715{
716 u32 val;
717
718 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
719 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
720}
721
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100722/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300723 * from mvneta_rxq_drop_pkts().
724 */
725static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
726 struct mvneta_rx_queue *rxq,
727 int rx_done, int rx_filled)
728{
729 u32 val;
730
731 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
732 val = rx_done |
733 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
734 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
735 return;
736 }
737
738 /* Only 255 descriptors can be added at once */
739 while ((rx_done > 0) || (rx_filled > 0)) {
740 if (rx_done <= 0xff) {
741 val = rx_done;
742 rx_done = 0;
743 } else {
744 val = 0xff;
745 rx_done -= 0xff;
746 }
747 if (rx_filled <= 0xff) {
748 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
749 rx_filled = 0;
750 } else {
751 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
752 rx_filled -= 0xff;
753 }
754 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
755 }
756}
757
758/* Get pointer to next RX descriptor to be processed by SW */
759static struct mvneta_rx_desc *
760mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
761{
762 int rx_desc = rxq->next_desc_to_proc;
763
764 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100765 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300766 return rxq->descs + rx_desc;
767}
768
769/* Change maximum receive size of the port. */
770static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
771{
772 u32 val;
773
774 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
775 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
776 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
777 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
778 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
779}
780
781
782/* Set rx queue offset */
783static void mvneta_rxq_offset_set(struct mvneta_port *pp,
784 struct mvneta_rx_queue *rxq,
785 int offset)
786{
787 u32 val;
788
789 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
790 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
791
792 /* Offset is in */
793 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
794 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
795}
796
797
798/* Tx descriptors helper methods */
799
800/* Update HW with number of TX descriptors to be sent */
801static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
802 struct mvneta_tx_queue *txq,
803 int pend_desc)
804{
805 u32 val;
806
807 /* Only 255 descriptors can be added at once ; Assume caller
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100808 * process TX desriptors in quanta less than 256
809 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300810 val = pend_desc;
811 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
812}
813
814/* Get pointer to next TX descriptor to be processed (send) by HW */
815static struct mvneta_tx_desc *
816mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
817{
818 int tx_desc = txq->next_desc_to_proc;
819
820 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
821 return txq->descs + tx_desc;
822}
823
824/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100825 * mapping failures in the TX path.
826 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300827static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
828{
829 if (txq->next_desc_to_proc == 0)
830 txq->next_desc_to_proc = txq->last_desc - 1;
831 else
832 txq->next_desc_to_proc--;
833}
834
835/* Set rxq buf size */
836static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
837 struct mvneta_rx_queue *rxq,
838 int buf_size)
839{
840 u32 val;
841
842 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
843
844 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
845 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
846
847 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
848}
849
850/* Disable buffer management (BM) */
851static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
852 struct mvneta_rx_queue *rxq)
853{
854 u32 val;
855
856 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
857 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
858 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
859}
860
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100861/* Enable buffer management (BM) */
862static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
863 struct mvneta_rx_queue *rxq)
864{
865 u32 val;
866
867 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
868 val |= MVNETA_RXQ_HW_BUF_ALLOC;
869 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
870}
871
872/* Notify HW about port's assignment of pool for bigger packets */
873static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
874 struct mvneta_rx_queue *rxq)
875{
876 u32 val;
877
878 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
879 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
880 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
881
882 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
883}
884
885/* Notify HW about port's assignment of pool for smaller packets */
886static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
887 struct mvneta_rx_queue *rxq)
888{
889 u32 val;
890
891 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
892 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
893 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
894
895 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
896}
897
898/* Set port's receive buffer size for assigned BM pool */
899static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
900 int buf_size,
901 u8 pool_id)
902{
903 u32 val;
904
905 if (!IS_ALIGNED(buf_size, 8)) {
906 dev_warn(pp->dev->dev.parent,
907 "illegal buf_size value %d, round to %d\n",
908 buf_size, ALIGN(buf_size, 8));
909 buf_size = ALIGN(buf_size, 8);
910 }
911
912 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
913 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
914 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
915}
916
917/* Configure MBUS window in order to enable access BM internal SRAM */
918static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
919 u8 target, u8 attr)
920{
921 u32 win_enable, win_protect;
922 int i;
923
924 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
925
926 if (pp->bm_win_id < 0) {
927 /* Find first not occupied window */
928 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
929 if (win_enable & (1 << i)) {
930 pp->bm_win_id = i;
931 break;
932 }
933 }
934 if (i == MVNETA_MAX_DECODE_WIN)
935 return -ENOMEM;
936 } else {
937 i = pp->bm_win_id;
938 }
939
940 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
941 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
942
943 if (i < 4)
944 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
945
946 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
947 (attr << 8) | target);
948
949 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
950
951 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
952 win_protect |= 3 << (2 * i);
953 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
954
955 win_enable &= ~(1 << i);
956 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
957
958 return 0;
959}
960
961/* Assign and initialize pools for port. In case of fail
962 * buffer manager will remain disabled for current port.
963 */
964static int mvneta_bm_port_init(struct platform_device *pdev,
965 struct mvneta_port *pp)
966{
967 struct device_node *dn = pdev->dev.of_node;
968 u32 long_pool_id, short_pool_id, wsize;
969 u8 target, attr;
970 int err;
971
972 /* Get BM window information */
973 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
974 &target, &attr);
975 if (err < 0)
976 return err;
977
978 pp->bm_win_id = -1;
979
980 /* Open NETA -> BM window */
981 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
982 target, attr);
983 if (err < 0) {
984 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
985 return err;
986 }
987
988 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
989 netdev_info(pp->dev, "missing long pool id\n");
990 return -EINVAL;
991 }
992
993 /* Create port's long pool depending on mtu */
994 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
995 MVNETA_BM_LONG, pp->id,
996 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
997 if (!pp->pool_long) {
998 netdev_info(pp->dev, "fail to obtain long pool for port\n");
999 return -ENOMEM;
1000 }
1001
1002 pp->pool_long->port_map |= 1 << pp->id;
1003
1004 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1005 pp->pool_long->id);
1006
1007 /* If short pool id is not defined, assume using single pool */
1008 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1009 short_pool_id = long_pool_id;
1010
1011 /* Create port's short pool */
1012 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1013 MVNETA_BM_SHORT, pp->id,
1014 MVNETA_BM_SHORT_PKT_SIZE);
1015 if (!pp->pool_short) {
1016 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1017 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1018 return -ENOMEM;
1019 }
1020
1021 if (short_pool_id != long_pool_id) {
1022 pp->pool_short->port_map |= 1 << pp->id;
1023 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1024 pp->pool_short->id);
1025 }
1026
1027 return 0;
1028}
1029
1030/* Update settings of a pool for bigger packets */
1031static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1032{
1033 struct mvneta_bm_pool *bm_pool = pp->pool_long;
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001034 struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001035 int num;
1036
1037 /* Release all buffers from long pool */
1038 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001039 if (hwbm_pool->buf_num) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001040 WARN(1, "cannot free all buffers in pool %d\n",
1041 bm_pool->id);
1042 goto bm_mtu_err;
1043 }
1044
1045 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1046 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001047 hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1048 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001049
1050 /* Fill entire long pool */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001051 num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1052 if (num != hwbm_pool->size) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001053 WARN(1, "pool %d: %d of %d allocated\n",
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01001054 bm_pool->id, num, hwbm_pool->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001055 goto bm_mtu_err;
1056 }
1057 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1058
1059 return;
1060
1061bm_mtu_err:
1062 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1063 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1064
1065 pp->bm_priv = NULL;
1066 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1067 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1068}
1069
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001070/* Start the Ethernet port RX and TX activity */
1071static void mvneta_port_up(struct mvneta_port *pp)
1072{
1073 int queue;
1074 u32 q_map;
1075
1076 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001077 q_map = 0;
1078 for (queue = 0; queue < txq_number; queue++) {
1079 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1080 if (txq->descs != NULL)
1081 q_map |= (1 << queue);
1082 }
1083 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1084
1085 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001086 for (queue = 0; queue < rxq_number; queue++) {
1087 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1088
1089 if (rxq->descs != NULL)
1090 q_map |= (1 << queue);
1091 }
1092 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001093}
1094
1095/* Stop the Ethernet port activity */
1096static void mvneta_port_down(struct mvneta_port *pp)
1097{
1098 u32 val;
1099 int count;
1100
1101 /* Stop Rx port activity. Check port Rx activity. */
1102 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1103
1104 /* Issue stop command for active channels only */
1105 if (val != 0)
1106 mvreg_write(pp, MVNETA_RXQ_CMD,
1107 val << MVNETA_RXQ_DISABLE_SHIFT);
1108
1109 /* Wait for all Rx activity to terminate. */
1110 count = 0;
1111 do {
1112 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1113 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001114 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001115 val);
1116 break;
1117 }
1118 mdelay(1);
1119
1120 val = mvreg_read(pp, MVNETA_RXQ_CMD);
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001121 } while (val & MVNETA_RXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001122
1123 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001124 * command for active channels only
1125 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001126 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1127
1128 if (val != 0)
1129 mvreg_write(pp, MVNETA_TXQ_CMD,
1130 (val << MVNETA_TXQ_DISABLE_SHIFT));
1131
1132 /* Wait for all Tx activity to terminate. */
1133 count = 0;
1134 do {
1135 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1136 netdev_warn(pp->dev,
1137 "TIMEOUT for TX stopped status=0x%08x\n",
1138 val);
1139 break;
1140 }
1141 mdelay(1);
1142
1143 /* Check TX Command reg that all Txqs are stopped */
1144 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1145
Dmitri Epshteina3703fb2016-03-12 18:44:20 +01001146 } while (val & MVNETA_TXQ_ENABLE_MASK);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001147
1148 /* Double check to verify that TX FIFO is empty */
1149 count = 0;
1150 do {
1151 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1152 netdev_warn(pp->dev,
Dmitri Epshtein0838abb32016-03-12 18:44:19 +01001153 "TX FIFO empty timeout status=0x%08x\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001154 val);
1155 break;
1156 }
1157 mdelay(1);
1158
1159 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1160 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1161 (val & MVNETA_TX_IN_PRGRS));
1162
1163 udelay(200);
1164}
1165
1166/* Enable the port by setting the port enable bit of the MAC control register */
1167static void mvneta_port_enable(struct mvneta_port *pp)
1168{
1169 u32 val;
1170
1171 /* Enable port */
1172 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1173 val |= MVNETA_GMAC0_PORT_ENABLE;
1174 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1175}
1176
1177/* Disable the port and wait for about 200 usec before retuning */
1178static void mvneta_port_disable(struct mvneta_port *pp)
1179{
1180 u32 val;
1181
1182 /* Reset the Enable bit in the Serial Control Register */
1183 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1184 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1185 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1186
1187 udelay(200);
1188}
1189
1190/* Multicast tables methods */
1191
1192/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1193static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1194{
1195 int offset;
1196 u32 val;
1197
1198 if (queue == -1) {
1199 val = 0;
1200 } else {
1201 val = 0x1 | (queue << 1);
1202 val |= (val << 24) | (val << 16) | (val << 8);
1203 }
1204
1205 for (offset = 0; offset <= 0xc; offset += 4)
1206 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1207}
1208
1209/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1210static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1211{
1212 int offset;
1213 u32 val;
1214
1215 if (queue == -1) {
1216 val = 0;
1217 } else {
1218 val = 0x1 | (queue << 1);
1219 val |= (val << 24) | (val << 16) | (val << 8);
1220 }
1221
1222 for (offset = 0; offset <= 0xfc; offset += 4)
1223 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1224
1225}
1226
1227/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1228static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1229{
1230 int offset;
1231 u32 val;
1232
1233 if (queue == -1) {
1234 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1235 val = 0;
1236 } else {
1237 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1238 val = 0x1 | (queue << 1);
1239 val |= (val << 24) | (val << 16) | (val << 8);
1240 }
1241
1242 for (offset = 0; offset <= 0xfc; offset += 4)
1243 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1244}
1245
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001246static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
1247{
1248 u32 val;
1249
1250 if (enable) {
1251 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1252 val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1253 MVNETA_GMAC_FORCE_LINK_DOWN |
1254 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1255 val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1256 MVNETA_GMAC_AN_SPEED_EN |
1257 MVNETA_GMAC_AN_DUPLEX_EN;
1258 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1259
1260 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1261 val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1262 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1263
1264 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1265 val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
1266 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1267 } else {
1268 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1269 val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1270 MVNETA_GMAC_AN_SPEED_EN |
1271 MVNETA_GMAC_AN_DUPLEX_EN);
1272 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1273
1274 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1275 val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
1276 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1277
1278 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1279 val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
1280 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1281 }
1282}
1283
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001284static void mvneta_percpu_unmask_interrupt(void *arg)
1285{
1286 struct mvneta_port *pp = arg;
1287
1288 /* All the queue are unmasked, but actually only the ones
1289 * mapped to this CPU will be unmasked
1290 */
1291 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1292 MVNETA_RX_INTR_MASK_ALL |
1293 MVNETA_TX_INTR_MASK_ALL |
1294 MVNETA_MISCINTR_INTR_MASK);
1295}
1296
1297static void mvneta_percpu_mask_interrupt(void *arg)
1298{
1299 struct mvneta_port *pp = arg;
1300
1301 /* All the queue are masked, but actually only the ones
1302 * mapped to this CPU will be masked
1303 */
1304 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1305 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1306 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1307}
1308
1309static void mvneta_percpu_clear_intr_cause(void *arg)
1310{
1311 struct mvneta_port *pp = arg;
1312
1313 /* All the queue are cleared, but actually only the ones
1314 * mapped to this CPU will be cleared
1315 */
1316 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1317 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1318 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1319}
1320
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001321/* This method sets defaults to the NETA port:
1322 * Clears interrupt Cause and Mask registers.
1323 * Clears all MAC tables.
1324 * Sets defaults to all registers.
1325 * Resets RX and TX descriptor rings.
1326 * Resets PHY.
1327 * This method can be called after mvneta_port_down() to return the port
1328 * settings to defaults.
1329 */
1330static void mvneta_defaults_set(struct mvneta_port *pp)
1331{
1332 int cpu;
1333 int queue;
1334 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001335 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001336
1337 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001338 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001339
1340 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001341 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001342 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1343
1344 /* Enable MBUS Retry bit16 */
1345 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1346
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001347 /* Set CPU queue access map. CPUs are assigned to the RX and
1348 * TX queues modulo their number. If there is only one TX
1349 * queue then it is assigned to the CPU associated to the
1350 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001351 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001352 for_each_present_cpu(cpu) {
1353 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001354 int rxq, txq;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001355
1356 for (rxq = 0; rxq < rxq_number; rxq++)
1357 if ((rxq % max_cpu) == cpu)
1358 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1359
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001360 for (txq = 0; txq < txq_number; txq++)
1361 if ((txq % max_cpu) == cpu)
1362 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1363
1364 /* With only one TX queue we configure a special case
1365 * which will allow to get all the irq on a single
1366 * CPU
1367 */
1368 if (txq_number == 1)
1369 txq_map = (cpu == pp->rxq_def) ?
1370 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001371
1372 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1373 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001374
1375 /* Reset RX and TX DMAs */
1376 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1377 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1378
1379 /* Disable Legacy WRR, Disable EJP, Release from reset */
1380 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1381 for (queue = 0; queue < txq_number; queue++) {
1382 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1383 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1384 }
1385
1386 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1387 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1388
1389 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001390 if (pp->bm_priv)
1391 /* HW buffer management + legacy parser */
1392 val = MVNETA_ACC_MODE_EXT2;
1393 else
1394 /* SW buffer management + legacy parser */
1395 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001396 mvreg_write(pp, MVNETA_ACC_MODE, val);
1397
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001398 if (pp->bm_priv)
1399 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1400
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001401 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001402 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001403 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1404
1405 val = 0;
1406 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1407 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1408
1409 /* Build PORT_SDMA_CONFIG_REG */
1410 val = 0;
1411
1412 /* Default burst size */
1413 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1414 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001415 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001416
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001417#if defined(__BIG_ENDIAN)
1418 val |= MVNETA_DESC_SWAP;
1419#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001420
1421 /* Assign port SDMA configuration */
1422 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1423
Thomas Petazzoni71408602013-09-04 16:21:18 +02001424 /* Disable PHY polling in hardware, since we're using the
1425 * kernel phylib to do this.
1426 */
1427 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1428 val &= ~MVNETA_PHY_POLLING_ENABLE;
1429 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1430
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001431 mvneta_set_autoneg(pp, pp->use_inband_status);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001432 mvneta_set_ucast_table(pp, -1);
1433 mvneta_set_special_mcast_table(pp, -1);
1434 mvneta_set_other_mcast_table(pp, -1);
1435
1436 /* Set port interrupt enable register - default enable all */
1437 mvreg_write(pp, MVNETA_INTR_ENABLE,
1438 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1439 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001440
1441 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001442}
1443
1444/* Set max sizes for tx queues */
1445static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1446
1447{
1448 u32 val, size, mtu;
1449 int queue;
1450
1451 mtu = max_tx_size * 8;
1452 if (mtu > MVNETA_TX_MTU_MAX)
1453 mtu = MVNETA_TX_MTU_MAX;
1454
1455 /* Set MTU */
1456 val = mvreg_read(pp, MVNETA_TX_MTU);
1457 val &= ~MVNETA_TX_MTU_MAX;
1458 val |= mtu;
1459 mvreg_write(pp, MVNETA_TX_MTU, val);
1460
1461 /* TX token size and all TXQs token size must be larger that MTU */
1462 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1463
1464 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1465 if (size < mtu) {
1466 size = mtu;
1467 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1468 val |= size;
1469 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1470 }
1471 for (queue = 0; queue < txq_number; queue++) {
1472 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1473
1474 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1475 if (size < mtu) {
1476 size = mtu;
1477 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1478 val |= size;
1479 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1480 }
1481 }
1482}
1483
1484/* Set unicast address */
1485static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1486 int queue)
1487{
1488 unsigned int unicast_reg;
1489 unsigned int tbl_offset;
1490 unsigned int reg_offset;
1491
1492 /* Locate the Unicast table entry */
1493 last_nibble = (0xf & last_nibble);
1494
1495 /* offset from unicast tbl base */
1496 tbl_offset = (last_nibble / 4) * 4;
1497
1498 /* offset within the above reg */
1499 reg_offset = last_nibble % 4;
1500
1501 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1502
1503 if (queue == -1) {
1504 /* Clear accepts frame bit at specified unicast DA tbl entry */
1505 unicast_reg &= ~(0xff << (8 * reg_offset));
1506 } else {
1507 unicast_reg &= ~(0xff << (8 * reg_offset));
1508 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1509 }
1510
1511 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1512}
1513
1514/* Set mac address */
1515static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1516 int queue)
1517{
1518 unsigned int mac_h;
1519 unsigned int mac_l;
1520
1521 if (queue != -1) {
1522 mac_l = (addr[4] << 8) | (addr[5]);
1523 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1524 (addr[2] << 8) | (addr[3] << 0);
1525
1526 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1527 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1528 }
1529
1530 /* Accept frames of this address */
1531 mvneta_set_ucast_addr(pp, addr[5], queue);
1532}
1533
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001534/* Set the number of packets that will be received before RX interrupt
1535 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001536 */
1537static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1538 struct mvneta_rx_queue *rxq, u32 value)
1539{
1540 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1541 value | MVNETA_RXQ_NON_OCCUPIED(0));
1542 rxq->pkts_coal = value;
1543}
1544
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001545/* Set the time delay in usec before RX interrupt will be generated by
1546 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001547 */
1548static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1549 struct mvneta_rx_queue *rxq, u32 value)
1550{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001551 u32 val;
1552 unsigned long clk_rate;
1553
1554 clk_rate = clk_get_rate(pp->clk);
1555 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001556
1557 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1558 rxq->time_coal = value;
1559}
1560
1561/* Set threshold for TX_DONE pkts coalescing */
1562static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1563 struct mvneta_tx_queue *txq, u32 value)
1564{
1565 u32 val;
1566
1567 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1568
1569 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1570 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1571
1572 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1573
1574 txq->done_pkts_coal = value;
1575}
1576
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001577/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1578static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001579 u32 phys_addr, void *virt_addr,
1580 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001581{
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001582 int i;
1583
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001584 rx_desc->buf_phys_addr = phys_addr;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001585 i = rx_desc - rxq->descs;
1586 rxq->buf_virt_addr[i] = virt_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001587}
1588
1589/* Decrement sent descriptors counter */
1590static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1591 struct mvneta_tx_queue *txq,
1592 int sent_desc)
1593{
1594 u32 val;
1595
1596 /* Only 255 TX descriptors can be updated at once */
1597 while (sent_desc > 0xff) {
1598 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1599 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1600 sent_desc = sent_desc - 0xff;
1601 }
1602
1603 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1604 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1605}
1606
1607/* Get number of TX descriptors already sent by HW */
1608static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1609 struct mvneta_tx_queue *txq)
1610{
1611 u32 val;
1612 int sent_desc;
1613
1614 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1615 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1616 MVNETA_TXQ_SENT_DESC_SHIFT;
1617
1618 return sent_desc;
1619}
1620
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001621/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001622 * The number of sent descriptors is returned.
1623 */
1624static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1625 struct mvneta_tx_queue *txq)
1626{
1627 int sent_desc;
1628
1629 /* Get number of sent descriptors */
1630 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1631
1632 /* Decrement sent descriptors counter */
1633 if (sent_desc)
1634 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1635
1636 return sent_desc;
1637}
1638
1639/* Set TXQ descriptors fields relevant for CSUM calculation */
1640static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1641 int ip_hdr_len, int l4_proto)
1642{
1643 u32 command;
1644
1645 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001646 * G_L4_chk, L4_type; required only for checksum
1647 * calculation
1648 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001649 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1650 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1651
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001652 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001653 command |= MVNETA_TXD_IP_CSUM;
1654 else
1655 command |= MVNETA_TX_L3_IP6;
1656
1657 if (l4_proto == IPPROTO_TCP)
1658 command |= MVNETA_TX_L4_CSUM_FULL;
1659 else if (l4_proto == IPPROTO_UDP)
1660 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1661 else
1662 command |= MVNETA_TX_L4_CSUM_NOT;
1663
1664 return command;
1665}
1666
1667
1668/* Display more error info */
1669static void mvneta_rx_error(struct mvneta_port *pp,
1670 struct mvneta_rx_desc *rx_desc)
1671{
1672 u32 status = rx_desc->status;
1673
willy tarreau54282132014-01-16 08:20:14 +01001674 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001675 netdev_err(pp->dev,
1676 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001677 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001678 return;
1679 }
1680
1681 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1682 case MVNETA_RXD_ERR_CRC:
1683 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1684 status, rx_desc->data_size);
1685 break;
1686 case MVNETA_RXD_ERR_OVERRUN:
1687 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1688 status, rx_desc->data_size);
1689 break;
1690 case MVNETA_RXD_ERR_LEN:
1691 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1692 status, rx_desc->data_size);
1693 break;
1694 case MVNETA_RXD_ERR_RESOURCE:
1695 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1696 status, rx_desc->data_size);
1697 break;
1698 }
1699}
1700
willy tarreau54282132014-01-16 08:20:14 +01001701/* Handle RX checksum offload based on the descriptor's status */
1702static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001703 struct sk_buff *skb)
1704{
willy tarreau54282132014-01-16 08:20:14 +01001705 if ((status & MVNETA_RXD_L3_IP4) &&
1706 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001707 skb->csum = 0;
1708 skb->ip_summed = CHECKSUM_UNNECESSARY;
1709 return;
1710 }
1711
1712 skb->ip_summed = CHECKSUM_NONE;
1713}
1714
willy tarreau6c498972014-01-16 08:20:12 +01001715/* Return tx queue pointer (find last set bit) according to <cause> returned
1716 * form tx_done reg. <cause> must not be null. The return value is always a
1717 * valid queue for matching the first one found in <cause>.
1718 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001719static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1720 u32 cause)
1721{
1722 int queue = fls(cause) - 1;
1723
willy tarreau6c498972014-01-16 08:20:12 +01001724 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001725}
1726
1727/* Free tx queue skbuffs */
1728static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1729 struct mvneta_tx_queue *txq, int num)
1730{
1731 int i;
1732
1733 for (i = 0; i < num; i++) {
1734 struct mvneta_tx_desc *tx_desc = txq->descs +
1735 txq->txq_get_index;
1736 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1737
1738 mvneta_txq_inc_get(txq);
1739
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001740 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1741 dma_unmap_single(pp->dev->dev.parent,
1742 tx_desc->buf_phys_addr,
1743 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001744 if (!skb)
1745 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001746 dev_kfree_skb_any(skb);
1747 }
1748}
1749
1750/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001751static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001752 struct mvneta_tx_queue *txq)
1753{
1754 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1755 int tx_done;
1756
1757 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001758 if (!tx_done)
1759 return;
1760
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001761 mvneta_txq_bufs_free(pp, txq, tx_done);
1762
1763 txq->count -= tx_done;
1764
1765 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001766 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001767 netif_tx_wake_queue(nq);
1768 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001769}
1770
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001771void *mvneta_frag_alloc(unsigned int frag_size)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001772{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001773 if (likely(frag_size <= PAGE_SIZE))
1774 return netdev_alloc_frag(frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001775 else
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001776 return kmalloc(frag_size, GFP_ATOMIC);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001777}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001778EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001779
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001780void mvneta_frag_free(unsigned int frag_size, void *data)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001781{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001782 if (likely(frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001783 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001784 else
1785 kfree(data);
1786}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001787EXPORT_SYMBOL_GPL(mvneta_frag_free);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001788
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001789/* Refill processing for SW buffer management */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001790static int mvneta_rx_refill(struct mvneta_port *pp,
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001791 struct mvneta_rx_desc *rx_desc,
1792 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001793
1794{
1795 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001796 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001797
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001798 data = mvneta_frag_alloc(pp->frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001799 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001800 return -ENOMEM;
1801
willy tarreau8ec2cd42014-01-16 08:20:16 +01001802 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001803 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1804 DMA_FROM_DEVICE);
1805 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001806 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001807 return -ENOMEM;
1808 }
1809
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001810 mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001811 return 0;
1812}
1813
1814/* Handle tx checksum */
1815static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1816{
1817 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1818 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001819 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001820 u8 l4_proto;
1821
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001822 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001823 struct iphdr *ip4h = ip_hdr(skb);
1824
1825 /* Calculate IPv4 checksum and L4 checksum */
1826 ip_hdr_len = ip4h->ihl;
1827 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001828 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001829 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1830
1831 /* Read l4_protocol from one of IPv6 extra headers */
1832 if (skb_network_header_len(skb) > 0)
1833 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1834 l4_proto = ip6h->nexthdr;
1835 } else
1836 return MVNETA_TX_L4_CSUM_NOT;
1837
1838 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001839 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001840 }
1841
1842 return MVNETA_TX_L4_CSUM_NOT;
1843}
1844
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001845/* Drop packets received by the RXQ and free buffers */
1846static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1847 struct mvneta_rx_queue *rxq)
1848{
1849 int rx_done, i;
1850
1851 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001852 if (rx_done)
1853 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1854
1855 if (pp->bm_priv) {
1856 for (i = 0; i < rx_done; i++) {
1857 struct mvneta_rx_desc *rx_desc =
1858 mvneta_rxq_next_desc_get(rxq);
1859 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1860 struct mvneta_bm_pool *bm_pool;
1861
1862 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1863 /* Return dropped buffer to the pool */
1864 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1865 rx_desc->buf_phys_addr);
1866 }
1867 return;
1868 }
1869
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001870 for (i = 0; i < rxq->size; i++) {
1871 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001872 void *data = rxq->buf_virt_addr[i];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001873
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001874 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001875 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001876 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001877 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001878}
1879
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001880/* Main rx processing when using software buffer management */
1881static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1882 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001883{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001884 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001885 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001886 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001887 u32 rcvd_pkts = 0;
1888 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001889
1890 /* Get number of received packets */
1891 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1892
1893 if (rx_todo > rx_done)
1894 rx_todo = rx_done;
1895
1896 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001897
1898 /* Fairness NAPI loop */
1899 while (rx_done < rx_todo) {
1900 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1901 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001902 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001903 dma_addr_t phys_addr;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001904 u32 rx_status, frag_size;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001905 int rx_bytes, err, index;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001906
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001907 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001908 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001909 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001910 index = rx_desc - rxq->descs;
1911 data = rxq->buf_virt_addr[index];
Simon Guinotdaf158d2015-09-15 22:41:21 +02001912 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001913
willy tarreau54282132014-01-16 08:20:14 +01001914 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001915 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001916err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001917 dev->stats.rx_errors++;
1918 mvneta_rx_error(pp, rx_desc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001919 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001920 continue;
1921 }
1922
willy tarreauf19fadf2014-01-16 08:20:17 +01001923 if (rx_bytes <= rx_copybreak) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001924 /* better copy a small frame and not unmap the DMA region */
willy tarreauf19fadf2014-01-16 08:20:17 +01001925 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1926 if (unlikely(!skb))
1927 goto err_drop_frame;
1928
1929 dma_sync_single_range_for_cpu(dev->dev.parent,
Gregory CLEMENTac83b7d2016-12-01 18:03:04 +01001930 phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001931 MVNETA_MH_SIZE + NET_SKB_PAD,
1932 rx_bytes,
1933 DMA_FROM_DEVICE);
willy tarreauf19fadf2014-01-16 08:20:17 +01001934 memcpy(skb_put(skb, rx_bytes),
1935 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1936 rx_bytes);
1937
1938 skb->protocol = eth_type_trans(skb, dev);
1939 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001940 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001941
1942 rcvd_pkts++;
1943 rcvd_bytes += rx_bytes;
1944
1945 /* leave the descriptor and buffer untouched */
1946 continue;
1947 }
1948
Simon Guinota84e3282015-07-19 13:00:53 +02001949 /* Refill processing */
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01001950 err = mvneta_rx_refill(pp, rx_desc, rxq);
Simon Guinota84e3282015-07-19 13:00:53 +02001951 if (err) {
1952 netdev_err(dev, "Linux processing - Can't refill\n");
1953 rxq->missed++;
1954 goto err_drop_frame;
1955 }
1956
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001957 frag_size = pp->frag_size;
1958
1959 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01001960
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001961 /* After refill old buffer has to be unmapped regardless
1962 * the skb is successfully built or not.
1963 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02001964 dma_unmap_single(dev->dev.parent, phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001965 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1966 DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001967
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001968 if (!skb)
1969 goto err_drop_frame;
1970
willy tarreaudc4277d2014-01-16 08:20:07 +01001971 rcvd_pkts++;
1972 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001973
1974 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001975 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001976 skb_put(skb, rx_bytes);
1977
1978 skb->protocol = eth_type_trans(skb, dev);
1979
willy tarreau54282132014-01-16 08:20:14 +01001980 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001981
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001982 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001983 }
1984
willy tarreaudc4277d2014-01-16 08:20:07 +01001985 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001986 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1987
1988 u64_stats_update_begin(&stats->syncp);
1989 stats->rx_packets += rcvd_pkts;
1990 stats->rx_bytes += rcvd_bytes;
1991 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001992 }
1993
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001994 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02001995 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001996
1997 return rx_done;
1998}
1999
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002000/* Main rx processing when using hardware buffer management */
2001static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2002 struct mvneta_rx_queue *rxq)
2003{
2004 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2005 struct net_device *dev = pp->dev;
2006 int rx_done;
2007 u32 rcvd_pkts = 0;
2008 u32 rcvd_bytes = 0;
2009
2010 /* Get number of received packets */
2011 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2012
2013 if (rx_todo > rx_done)
2014 rx_todo = rx_done;
2015
2016 rx_done = 0;
2017
2018 /* Fairness NAPI loop */
2019 while (rx_done < rx_todo) {
2020 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2021 struct mvneta_bm_pool *bm_pool = NULL;
2022 struct sk_buff *skb;
2023 unsigned char *data;
2024 dma_addr_t phys_addr;
2025 u32 rx_status, frag_size;
2026 int rx_bytes, err;
2027 u8 pool_id;
2028
2029 rx_done++;
2030 rx_status = rx_desc->status;
2031 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002032 data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002033 phys_addr = rx_desc->buf_phys_addr;
2034 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2035 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2036
2037 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2038 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2039err_drop_frame_ret_pool:
2040 /* Return the buffer to the pool */
2041 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2042 rx_desc->buf_phys_addr);
2043err_drop_frame:
2044 dev->stats.rx_errors++;
2045 mvneta_rx_error(pp, rx_desc);
2046 /* leave the descriptor untouched */
2047 continue;
2048 }
2049
2050 if (rx_bytes <= rx_copybreak) {
2051 /* better copy a small frame and not unmap the DMA region */
2052 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2053 if (unlikely(!skb))
2054 goto err_drop_frame_ret_pool;
2055
2056 dma_sync_single_range_for_cpu(dev->dev.parent,
2057 rx_desc->buf_phys_addr,
2058 MVNETA_MH_SIZE + NET_SKB_PAD,
2059 rx_bytes,
2060 DMA_FROM_DEVICE);
2061 memcpy(skb_put(skb, rx_bytes),
2062 data + MVNETA_MH_SIZE + NET_SKB_PAD,
2063 rx_bytes);
2064
2065 skb->protocol = eth_type_trans(skb, dev);
2066 mvneta_rx_csum(pp, rx_status, skb);
2067 napi_gro_receive(&port->napi, skb);
2068
2069 rcvd_pkts++;
2070 rcvd_bytes += rx_bytes;
2071
2072 /* Return the buffer to the pool */
2073 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2074 rx_desc->buf_phys_addr);
2075
2076 /* leave the descriptor and buffer untouched */
2077 continue;
2078 }
2079
2080 /* Refill processing */
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002081 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002082 if (err) {
2083 netdev_err(dev, "Linux processing - Can't refill\n");
2084 rxq->missed++;
2085 goto err_drop_frame_ret_pool;
2086 }
2087
Gregory CLEMENTbaa11eb2016-03-14 09:39:05 +01002088 frag_size = bm_pool->hwbm_pool.frag_size;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002089
2090 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2091
2092 /* After refill old buffer has to be unmapped regardless
2093 * the skb is successfully built or not.
2094 */
2095 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2096 bm_pool->buf_size, DMA_FROM_DEVICE);
2097 if (!skb)
2098 goto err_drop_frame;
2099
2100 rcvd_pkts++;
2101 rcvd_bytes += rx_bytes;
2102
2103 /* Linux processing */
2104 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2105 skb_put(skb, rx_bytes);
2106
2107 skb->protocol = eth_type_trans(skb, dev);
2108
2109 mvneta_rx_csum(pp, rx_status, skb);
2110
2111 napi_gro_receive(&port->napi, skb);
2112 }
2113
2114 if (rcvd_pkts) {
2115 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2116
2117 u64_stats_update_begin(&stats->syncp);
2118 stats->rx_packets += rcvd_pkts;
2119 stats->rx_bytes += rcvd_bytes;
2120 u64_stats_update_end(&stats->syncp);
2121 }
2122
2123 /* Update rxq management counters */
2124 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2125
2126 return rx_done;
2127}
2128
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002129static inline void
2130mvneta_tso_put_hdr(struct sk_buff *skb,
2131 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2132{
2133 struct mvneta_tx_desc *tx_desc;
2134 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2135
2136 txq->tx_skb[txq->txq_put_index] = NULL;
2137 tx_desc = mvneta_txq_next_desc_get(txq);
2138 tx_desc->data_size = hdr_len;
2139 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2140 tx_desc->command |= MVNETA_TXD_F_DESC;
2141 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2142 txq->txq_put_index * TSO_HEADER_SIZE;
2143 mvneta_txq_inc_put(txq);
2144}
2145
2146static inline int
2147mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2148 struct sk_buff *skb, char *data, int size,
2149 bool last_tcp, bool is_last)
2150{
2151 struct mvneta_tx_desc *tx_desc;
2152
2153 tx_desc = mvneta_txq_next_desc_get(txq);
2154 tx_desc->data_size = size;
2155 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2156 size, DMA_TO_DEVICE);
2157 if (unlikely(dma_mapping_error(dev->dev.parent,
2158 tx_desc->buf_phys_addr))) {
2159 mvneta_txq_desc_put(txq);
2160 return -ENOMEM;
2161 }
2162
2163 tx_desc->command = 0;
2164 txq->tx_skb[txq->txq_put_index] = NULL;
2165
2166 if (last_tcp) {
2167 /* last descriptor in the TCP packet */
2168 tx_desc->command = MVNETA_TXD_L_DESC;
2169
2170 /* last descriptor in SKB */
2171 if (is_last)
2172 txq->tx_skb[txq->txq_put_index] = skb;
2173 }
2174 mvneta_txq_inc_put(txq);
2175 return 0;
2176}
2177
2178static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2179 struct mvneta_tx_queue *txq)
2180{
2181 int total_len, data_left;
2182 int desc_count = 0;
2183 struct mvneta_port *pp = netdev_priv(dev);
2184 struct tso_t tso;
2185 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2186 int i;
2187
2188 /* Count needed descriptors */
2189 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2190 return 0;
2191
2192 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2193 pr_info("*** Is this even possible???!?!?\n");
2194 return 0;
2195 }
2196
2197 /* Initialize the TSO handler, and prepare the first payload */
2198 tso_start(skb, &tso);
2199
2200 total_len = skb->len - hdr_len;
2201 while (total_len > 0) {
2202 char *hdr;
2203
2204 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2205 total_len -= data_left;
2206 desc_count++;
2207
2208 /* prepare packet headers: MAC + IP + TCP */
2209 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2210 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2211
2212 mvneta_tso_put_hdr(skb, pp, txq);
2213
2214 while (data_left > 0) {
2215 int size;
2216 desc_count++;
2217
2218 size = min_t(int, tso.size, data_left);
2219
2220 if (mvneta_tso_put_data(dev, txq, skb,
2221 tso.data, size,
2222 size == data_left,
2223 total_len == 0))
2224 goto err_release;
2225 data_left -= size;
2226
2227 tso_build_data(skb, &tso, size);
2228 }
2229 }
2230
2231 return desc_count;
2232
2233err_release:
2234 /* Release all used data descriptors; header descriptors must not
2235 * be DMA-unmapped.
2236 */
2237 for (i = desc_count - 1; i >= 0; i--) {
2238 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002239 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002240 dma_unmap_single(pp->dev->dev.parent,
2241 tx_desc->buf_phys_addr,
2242 tx_desc->data_size,
2243 DMA_TO_DEVICE);
2244 mvneta_txq_desc_put(txq);
2245 }
2246 return 0;
2247}
2248
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002249/* Handle tx fragmentation processing */
2250static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2251 struct mvneta_tx_queue *txq)
2252{
2253 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002254 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002255
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002256 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002257 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2258 void *addr = page_address(frag->page.p) + frag->page_offset;
2259
2260 tx_desc = mvneta_txq_next_desc_get(txq);
2261 tx_desc->data_size = frag->size;
2262
2263 tx_desc->buf_phys_addr =
2264 dma_map_single(pp->dev->dev.parent, addr,
2265 tx_desc->data_size, DMA_TO_DEVICE);
2266
2267 if (dma_mapping_error(pp->dev->dev.parent,
2268 tx_desc->buf_phys_addr)) {
2269 mvneta_txq_desc_put(txq);
2270 goto error;
2271 }
2272
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002273 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002274 /* Last descriptor */
2275 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002276 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002277 } else {
2278 /* Descriptor in the middle: Not First, Not Last */
2279 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002280 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002281 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002282 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002283 }
2284
2285 return 0;
2286
2287error:
2288 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002289 * this packet, as well as the corresponding DMA mappings
2290 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002291 for (i = i - 1; i >= 0; i--) {
2292 tx_desc = txq->descs + i;
2293 dma_unmap_single(pp->dev->dev.parent,
2294 tx_desc->buf_phys_addr,
2295 tx_desc->data_size,
2296 DMA_TO_DEVICE);
2297 mvneta_txq_desc_put(txq);
2298 }
2299
2300 return -ENOMEM;
2301}
2302
2303/* Main tx processing */
2304static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2305{
2306 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002307 u16 txq_id = skb_get_queue_mapping(skb);
2308 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002309 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002310 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002311 int frags = 0;
2312 u32 tx_cmd;
2313
2314 if (!netif_running(dev))
2315 goto out;
2316
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002317 if (skb_is_gso(skb)) {
2318 frags = mvneta_tx_tso(skb, dev, txq);
2319 goto out;
2320 }
2321
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002322 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002323
2324 /* Get a descriptor for the first part of the packet */
2325 tx_desc = mvneta_txq_next_desc_get(txq);
2326
2327 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2328
2329 tx_desc->data_size = skb_headlen(skb);
2330
2331 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2332 tx_desc->data_size,
2333 DMA_TO_DEVICE);
2334 if (unlikely(dma_mapping_error(dev->dev.parent,
2335 tx_desc->buf_phys_addr))) {
2336 mvneta_txq_desc_put(txq);
2337 frags = 0;
2338 goto out;
2339 }
2340
2341 if (frags == 1) {
2342 /* First and Last descriptor */
2343 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2344 tx_desc->command = tx_cmd;
2345 txq->tx_skb[txq->txq_put_index] = skb;
2346 mvneta_txq_inc_put(txq);
2347 } else {
2348 /* First but not Last */
2349 tx_cmd |= MVNETA_TXD_F_DESC;
2350 txq->tx_skb[txq->txq_put_index] = NULL;
2351 mvneta_txq_inc_put(txq);
2352 tx_desc->command = tx_cmd;
2353 /* Continue with other skb fragments */
2354 if (mvneta_tx_frag_process(pp, skb, txq)) {
2355 dma_unmap_single(dev->dev.parent,
2356 tx_desc->buf_phys_addr,
2357 tx_desc->data_size,
2358 DMA_TO_DEVICE);
2359 mvneta_txq_desc_put(txq);
2360 frags = 0;
2361 goto out;
2362 }
2363 }
2364
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002365out:
2366 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01002367 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002368 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2369
2370 txq->count += frags;
2371 mvneta_txq_pend_desc_add(pp, txq, frags);
2372
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002373 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002374 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002375
willy tarreau74c41b02014-01-16 08:20:08 +01002376 u64_stats_update_begin(&stats->syncp);
2377 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002378 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01002379 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002380 } else {
2381 dev->stats.tx_dropped++;
2382 dev_kfree_skb_any(skb);
2383 }
2384
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002385 return NETDEV_TX_OK;
2386}
2387
2388
2389/* Free tx resources, when resetting a port */
2390static void mvneta_txq_done_force(struct mvneta_port *pp,
2391 struct mvneta_tx_queue *txq)
2392
2393{
2394 int tx_done = txq->count;
2395
2396 mvneta_txq_bufs_free(pp, txq, tx_done);
2397
2398 /* reset txq */
2399 txq->count = 0;
2400 txq->txq_put_index = 0;
2401 txq->txq_get_index = 0;
2402}
2403
willy tarreau6c498972014-01-16 08:20:12 +01002404/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2405 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2406 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002407static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002408{
2409 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002410 struct netdev_queue *nq;
2411
willy tarreau6c498972014-01-16 08:20:12 +01002412 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002413 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002414
2415 nq = netdev_get_tx_queue(pp->dev, txq->id);
2416 __netif_tx_lock(nq, smp_processor_id());
2417
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002418 if (txq->count)
2419 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002420
2421 __netif_tx_unlock(nq);
2422 cause_tx_done &= ~((1 << txq->id));
2423 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002424}
2425
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002426/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002427 * according to hw spec, different than generic crc8 algorithm
2428 */
2429static int mvneta_addr_crc(unsigned char *addr)
2430{
2431 int crc = 0;
2432 int i;
2433
2434 for (i = 0; i < ETH_ALEN; i++) {
2435 int j;
2436
2437 crc = (crc ^ addr[i]) << 8;
2438 for (j = 7; j >= 0; j--) {
2439 if (crc & (0x100 << j))
2440 crc ^= 0x107 << j;
2441 }
2442 }
2443
2444 return crc;
2445}
2446
2447/* This method controls the net device special MAC multicast support.
2448 * The Special Multicast Table for MAC addresses supports MAC of the form
2449 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2450 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2451 * Table entries in the DA-Filter table. This method set the Special
2452 * Multicast Table appropriate entry.
2453 */
2454static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2455 unsigned char last_byte,
2456 int queue)
2457{
2458 unsigned int smc_table_reg;
2459 unsigned int tbl_offset;
2460 unsigned int reg_offset;
2461
2462 /* Register offset from SMC table base */
2463 tbl_offset = (last_byte / 4);
2464 /* Entry offset within the above reg */
2465 reg_offset = last_byte % 4;
2466
2467 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2468 + tbl_offset * 4));
2469
2470 if (queue == -1)
2471 smc_table_reg &= ~(0xff << (8 * reg_offset));
2472 else {
2473 smc_table_reg &= ~(0xff << (8 * reg_offset));
2474 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2475 }
2476
2477 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2478 smc_table_reg);
2479}
2480
2481/* This method controls the network device Other MAC multicast support.
2482 * The Other Multicast Table is used for multicast of another type.
2483 * A CRC-8 is used as an index to the Other Multicast Table entries
2484 * in the DA-Filter table.
2485 * The method gets the CRC-8 value from the calling routine and
2486 * sets the Other Multicast Table appropriate entry according to the
2487 * specified CRC-8 .
2488 */
2489static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2490 unsigned char crc8,
2491 int queue)
2492{
2493 unsigned int omc_table_reg;
2494 unsigned int tbl_offset;
2495 unsigned int reg_offset;
2496
2497 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2498 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2499
2500 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2501
2502 if (queue == -1) {
2503 /* Clear accepts frame bit at specified Other DA table entry */
2504 omc_table_reg &= ~(0xff << (8 * reg_offset));
2505 } else {
2506 omc_table_reg &= ~(0xff << (8 * reg_offset));
2507 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2508 }
2509
2510 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2511}
2512
2513/* The network device supports multicast using two tables:
2514 * 1) Special Multicast Table for MAC addresses of the form
2515 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2516 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2517 * Table entries in the DA-Filter table.
2518 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2519 * is used as an index to the Other Multicast Table entries in the
2520 * DA-Filter table.
2521 */
2522static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2523 int queue)
2524{
2525 unsigned char crc_result = 0;
2526
2527 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2528 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2529 return 0;
2530 }
2531
2532 crc_result = mvneta_addr_crc(p_addr);
2533 if (queue == -1) {
2534 if (pp->mcast_count[crc_result] == 0) {
2535 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2536 crc_result);
2537 return -EINVAL;
2538 }
2539
2540 pp->mcast_count[crc_result]--;
2541 if (pp->mcast_count[crc_result] != 0) {
2542 netdev_info(pp->dev,
2543 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2544 pp->mcast_count[crc_result], crc_result);
2545 return -EINVAL;
2546 }
2547 } else
2548 pp->mcast_count[crc_result]++;
2549
2550 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2551
2552 return 0;
2553}
2554
2555/* Configure Fitering mode of Ethernet port */
2556static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2557 int is_promisc)
2558{
2559 u32 port_cfg_reg, val;
2560
2561 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2562
2563 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2564
2565 /* Set / Clear UPM bit in port configuration register */
2566 if (is_promisc) {
2567 /* Accept all Unicast addresses */
2568 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2569 val |= MVNETA_FORCE_UNI;
2570 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2571 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2572 } else {
2573 /* Reject all Unicast addresses */
2574 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2575 val &= ~MVNETA_FORCE_UNI;
2576 }
2577
2578 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2579 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2580}
2581
2582/* register unicast and multicast addresses */
2583static void mvneta_set_rx_mode(struct net_device *dev)
2584{
2585 struct mvneta_port *pp = netdev_priv(dev);
2586 struct netdev_hw_addr *ha;
2587
2588 if (dev->flags & IFF_PROMISC) {
2589 /* Accept all: Multicast + Unicast */
2590 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002591 mvneta_set_ucast_table(pp, pp->rxq_def);
2592 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2593 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002594 } else {
2595 /* Accept single Unicast */
2596 mvneta_rx_unicast_promisc_set(pp, 0);
2597 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002598 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002599
2600 if (dev->flags & IFF_ALLMULTI) {
2601 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002602 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2603 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002604 } else {
2605 /* Accept only initialized multicast */
2606 mvneta_set_special_mcast_table(pp, -1);
2607 mvneta_set_other_mcast_table(pp, -1);
2608
2609 if (!netdev_mc_empty(dev)) {
2610 netdev_for_each_mc_addr(ha, dev) {
2611 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002612 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002613 }
2614 }
2615 }
2616 }
2617}
2618
2619/* Interrupt handling - the callback for request_irq() */
2620static irqreturn_t mvneta_isr(int irq, void *dev_id)
2621{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002622 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002623
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002624 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002625 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002626
2627 return IRQ_HANDLED;
2628}
2629
Stas Sergeev898b29702015-04-01 20:32:49 +03002630static int mvneta_fixed_link_update(struct mvneta_port *pp,
2631 struct phy_device *phy)
2632{
2633 struct fixed_phy_status status;
2634 struct fixed_phy_status changed = {};
2635 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2636
2637 status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2638 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2639 status.speed = SPEED_1000;
2640 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2641 status.speed = SPEED_100;
2642 else
2643 status.speed = SPEED_10;
2644 status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2645 changed.link = 1;
2646 changed.speed = 1;
2647 changed.duplex = 1;
2648 fixed_phy_update_state(phy, &status, &changed);
2649 return 0;
2650}
2651
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002652/* NAPI handler
2653 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2654 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2655 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2656 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2657 * Each CPU has its own causeRxTx register
2658 */
2659static int mvneta_poll(struct napi_struct *napi, int budget)
2660{
2661 int rx_done = 0;
2662 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002663 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002664 struct mvneta_port *pp = netdev_priv(napi->dev);
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002665 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002666 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002667
2668 if (!netif_running(pp->dev)) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002669 napi_complete(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002670 return rx_done;
2671 }
2672
2673 /* Read cause register */
Stas Sergeev898b29702015-04-01 20:32:49 +03002674 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2675 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2676 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2677
2678 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2679 if (pp->use_inband_status && (cause_misc &
2680 (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2681 MVNETA_CAUSE_LINK_CHANGE |
2682 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002683 mvneta_fixed_link_update(pp, ndev->phydev);
Stas Sergeev898b29702015-04-01 20:32:49 +03002684 }
2685 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002686
2687 /* Release Tx descriptors */
2688 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002689 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002690 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2691 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002692
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002693 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002694 * RX packets
2695 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002696 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2697
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002698 cause_rx_tx |= port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002699
2700 if (rx_queue) {
2701 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002702 if (pp->bm_priv)
2703 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2704 else
2705 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002706 }
2707
Maxime Ripardd8936652015-09-25 18:09:37 +02002708 budget -= rx_done;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002709
2710 if (budget > 0) {
2711 cause_rx_tx = 0;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002712 napi_complete(&port->napi);
2713 enable_percpu_irq(pp->dev->irq, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002714 }
2715
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002716 port->cause_rx_tx = cause_rx_tx;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002717 return rx_done;
2718}
2719
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002720/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2721static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2722 int num)
2723{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002724 int i;
2725
2726 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002727 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01002728 if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002729 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002730 __func__, rxq->id, i, num);
2731 break;
2732 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002733 }
2734
2735 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002736 * get packets)
2737 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002738 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2739
2740 return i;
2741}
2742
2743/* Free all packets pending transmit from all TXQs and reset TX port */
2744static void mvneta_tx_reset(struct mvneta_port *pp)
2745{
2746 int queue;
2747
Ezequiel Garcia96728502014-05-22 20:06:59 -03002748 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002749 for (queue = 0; queue < txq_number; queue++)
2750 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2751
2752 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2753 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2754}
2755
2756static void mvneta_rx_reset(struct mvneta_port *pp)
2757{
2758 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2759 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2760}
2761
2762/* Rx/Tx queue initialization/cleanup methods */
2763
2764/* Create a specified RX queue */
2765static int mvneta_rxq_init(struct mvneta_port *pp,
2766 struct mvneta_rx_queue *rxq)
2767
2768{
2769 rxq->size = pp->rx_ring_size;
2770
2771 /* Allocate memory for RX descriptors */
2772 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2773 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2774 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002775 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002776 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002777
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002778 rxq->last_desc = rxq->size - 1;
2779
2780 /* Set Rx descriptors queue starting address */
2781 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2782 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2783
2784 /* Set Offset */
2785 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2786
2787 /* Set coalescing pkts and time */
2788 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2789 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2790
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002791 if (!pp->bm_priv) {
2792 /* Fill RXQ with buffers from RX pool */
2793 mvneta_rxq_buf_size_set(pp, rxq,
2794 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2795 mvneta_rxq_bm_disable(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002796 mvneta_rxq_fill(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002797 } else {
2798 mvneta_rxq_bm_enable(pp, rxq);
2799 mvneta_rxq_long_pool_set(pp, rxq);
2800 mvneta_rxq_short_pool_set(pp, rxq);
Gregory CLEMENTe9f64992016-12-01 18:03:05 +01002801 mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002802 }
2803
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002804 return 0;
2805}
2806
2807/* Cleanup Rx queue */
2808static void mvneta_rxq_deinit(struct mvneta_port *pp,
2809 struct mvneta_rx_queue *rxq)
2810{
2811 mvneta_rxq_drop_pkts(pp, rxq);
2812
2813 if (rxq->descs)
2814 dma_free_coherent(pp->dev->dev.parent,
2815 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2816 rxq->descs,
2817 rxq->descs_phys);
2818
2819 rxq->descs = NULL;
2820 rxq->last_desc = 0;
2821 rxq->next_desc_to_proc = 0;
2822 rxq->descs_phys = 0;
2823}
2824
2825/* Create and initialize a tx queue */
2826static int mvneta_txq_init(struct mvneta_port *pp,
2827 struct mvneta_tx_queue *txq)
2828{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002829 int cpu;
2830
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002831 txq->size = pp->tx_ring_size;
2832
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002833 /* A queue must always have room for at least one skb.
2834 * Therefore, stop the queue when the free entries reaches
2835 * the maximum number of descriptors per skb.
2836 */
2837 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2838 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2839
2840
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002841 /* Allocate memory for TX descriptors */
2842 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2843 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2844 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002845 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002846 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002847
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002848 txq->last_desc = txq->size - 1;
2849
2850 /* Set maximum bandwidth for enabled TXQs */
2851 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2852 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2853
2854 /* Set Tx descriptors queue starting address */
2855 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2856 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2857
2858 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2859 if (txq->tx_skb == NULL) {
2860 dma_free_coherent(pp->dev->dev.parent,
2861 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2862 txq->descs, txq->descs_phys);
2863 return -ENOMEM;
2864 }
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002865
2866 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2867 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2868 txq->size * TSO_HEADER_SIZE,
2869 &txq->tso_hdrs_phys, GFP_KERNEL);
2870 if (txq->tso_hdrs == NULL) {
2871 kfree(txq->tx_skb);
2872 dma_free_coherent(pp->dev->dev.parent,
2873 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2874 txq->descs, txq->descs_phys);
2875 return -ENOMEM;
2876 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002877 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2878
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002879 /* Setup XPS mapping */
2880 if (txq_number > 1)
2881 cpu = txq->id % num_present_cpus();
2882 else
2883 cpu = pp->rxq_def % num_present_cpus();
2884 cpumask_set_cpu(cpu, &txq->affinity_mask);
2885 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2886
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002887 return 0;
2888}
2889
2890/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2891static void mvneta_txq_deinit(struct mvneta_port *pp,
2892 struct mvneta_tx_queue *txq)
2893{
2894 kfree(txq->tx_skb);
2895
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03002896 if (txq->tso_hdrs)
2897 dma_free_coherent(pp->dev->dev.parent,
2898 txq->size * TSO_HEADER_SIZE,
2899 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002900 if (txq->descs)
2901 dma_free_coherent(pp->dev->dev.parent,
2902 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2903 txq->descs, txq->descs_phys);
2904
2905 txq->descs = NULL;
2906 txq->last_desc = 0;
2907 txq->next_desc_to_proc = 0;
2908 txq->descs_phys = 0;
2909
2910 /* Set minimum bandwidth for disabled TXQs */
2911 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2912 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2913
2914 /* Set Tx descriptors queue starting address and size */
2915 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2916 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2917}
2918
2919/* Cleanup all Tx queues */
2920static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2921{
2922 int queue;
2923
2924 for (queue = 0; queue < txq_number; queue++)
2925 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2926}
2927
2928/* Cleanup all Rx queues */
2929static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2930{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002931 int queue;
2932
2933 for (queue = 0; queue < txq_number; queue++)
2934 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002935}
2936
2937
2938/* Init all Rx queues */
2939static int mvneta_setup_rxqs(struct mvneta_port *pp)
2940{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002941 int queue;
2942
2943 for (queue = 0; queue < rxq_number; queue++) {
2944 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2945
2946 if (err) {
2947 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2948 __func__, queue);
2949 mvneta_cleanup_rxqs(pp);
2950 return err;
2951 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002952 }
2953
2954 return 0;
2955}
2956
2957/* Init all tx queues */
2958static int mvneta_setup_txqs(struct mvneta_port *pp)
2959{
2960 int queue;
2961
2962 for (queue = 0; queue < txq_number; queue++) {
2963 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2964 if (err) {
2965 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2966 __func__, queue);
2967 mvneta_cleanup_txqs(pp);
2968 return err;
2969 }
2970 }
2971
2972 return 0;
2973}
2974
2975static void mvneta_start_dev(struct mvneta_port *pp)
2976{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01002977 int cpu;
Philippe Reynesc6c022e2016-07-30 17:42:11 +02002978 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002979
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002980 mvneta_max_rx_size_set(pp, pp->pkt_size);
2981 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2982
2983 /* start the Rx/Tx activity */
2984 mvneta_port_enable(pp);
2985
2986 /* Enable polling on the port */
Gregory CLEMENT129219e2016-02-04 22:09:23 +01002987 for_each_online_cpu(cpu) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002988 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2989
2990 napi_enable(&port->napi);
2991 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002992
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002993 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01002994 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
2995
Stas Sergeev898b29702015-04-01 20:32:49 +03002996 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2997 MVNETA_CAUSE_PHY_STATUS_CHANGE |
2998 MVNETA_CAUSE_LINK_CHANGE |
2999 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003000
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003001 phy_start(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003002 netif_tx_start_all_queues(pp->dev);
3003}
3004
3005static void mvneta_stop_dev(struct mvneta_port *pp)
3006{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003007 unsigned int cpu;
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003008 struct net_device *ndev = pp->dev;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003009
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003010 phy_stop(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003011
Gregory CLEMENT129219e2016-02-04 22:09:23 +01003012 for_each_online_cpu(cpu) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003013 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3014
3015 napi_disable(&port->napi);
3016 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003017
3018 netif_carrier_off(pp->dev);
3019
3020 mvneta_port_down(pp);
3021 netif_tx_stop_all_queues(pp->dev);
3022
3023 /* Stop the port activity */
3024 mvneta_port_disable(pp);
3025
3026 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003027 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003028
3029 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003030 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003031
3032 mvneta_tx_reset(pp);
3033 mvneta_rx_reset(pp);
3034}
3035
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003036static void mvneta_percpu_enable(void *arg)
3037{
3038 struct mvneta_port *pp = arg;
3039
3040 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3041}
3042
3043static void mvneta_percpu_disable(void *arg)
3044{
3045 struct mvneta_port *pp = arg;
3046
3047 disable_percpu_irq(pp->dev->irq);
3048}
3049
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003050/* Change the device mtu */
3051static int mvneta_change_mtu(struct net_device *dev, int mtu)
3052{
3053 struct mvneta_port *pp = netdev_priv(dev);
3054 int ret;
3055
Jarod Wilson57779872016-10-17 15:54:06 -04003056 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3057 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3058 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3059 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3060 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003061
3062 dev->mtu = mtu;
3063
Simon Guinotb65657f2015-06-30 16:20:22 +02003064 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003065 if (pp->bm_priv)
3066 mvneta_bm_update_mtu(pp, mtu);
3067
Simon Guinotb65657f2015-06-30 16:20:22 +02003068 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003069 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003070 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003071
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003072 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003073 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003074 */
3075 mvneta_stop_dev(pp);
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003076 on_each_cpu(mvneta_percpu_disable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003077
3078 mvneta_cleanup_txqs(pp);
3079 mvneta_cleanup_rxqs(pp);
3080
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003081 if (pp->bm_priv)
3082 mvneta_bm_update_mtu(pp, mtu);
3083
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003084 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003085 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3086 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003087
3088 ret = mvneta_setup_rxqs(pp);
3089 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003090 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003091 return ret;
3092 }
3093
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003094 ret = mvneta_setup_txqs(pp);
3095 if (ret) {
3096 netdev_err(dev, "unable to setup txqs after MTU change\n");
3097 return ret;
3098 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003099
Marcin Wojtasdb5dd0d2016-04-01 15:21:18 +02003100 on_each_cpu(mvneta_percpu_enable, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003101 mvneta_start_dev(pp);
3102 mvneta_port_up(pp);
3103
Simon Guinotb65657f2015-06-30 16:20:22 +02003104 netdev_update_features(dev);
3105
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003106 return 0;
3107}
3108
Simon Guinotb65657f2015-06-30 16:20:22 +02003109static netdev_features_t mvneta_fix_features(struct net_device *dev,
3110 netdev_features_t features)
3111{
3112 struct mvneta_port *pp = netdev_priv(dev);
3113
3114 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3115 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3116 netdev_info(dev,
3117 "Disable IP checksum for MTU greater than %dB\n",
3118 pp->tx_csum_limit);
3119 }
3120
3121 return features;
3122}
3123
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003124/* Get mac address */
3125static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3126{
3127 u32 mac_addr_l, mac_addr_h;
3128
3129 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3130 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3131 addr[0] = (mac_addr_h >> 24) & 0xFF;
3132 addr[1] = (mac_addr_h >> 16) & 0xFF;
3133 addr[2] = (mac_addr_h >> 8) & 0xFF;
3134 addr[3] = mac_addr_h & 0xFF;
3135 addr[4] = (mac_addr_l >> 8) & 0xFF;
3136 addr[5] = mac_addr_l & 0xFF;
3137}
3138
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003139/* Handle setting mac address */
3140static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3141{
3142 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003143 struct sockaddr *sockaddr = addr;
3144 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003145
Ezequiel Garciae68de362014-05-22 20:07:00 -03003146 ret = eth_prepare_mac_addr_change(dev, addr);
3147 if (ret < 0)
3148 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003149 /* Remove previous address table entry */
3150 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3151
3152 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003153 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003154
Ezequiel Garciae68de362014-05-22 20:07:00 -03003155 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003156 return 0;
3157}
3158
3159static void mvneta_adjust_link(struct net_device *ndev)
3160{
3161 struct mvneta_port *pp = netdev_priv(ndev);
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003162 struct phy_device *phydev = ndev->phydev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003163 int status_change = 0;
3164
3165 if (phydev->link) {
3166 if ((pp->speed != phydev->speed) ||
3167 (pp->duplex != phydev->duplex)) {
3168 u32 val;
3169
3170 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3171 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3172 MVNETA_GMAC_CONFIG_GMII_SPEED |
Stas Sergeev898b29702015-04-01 20:32:49 +03003173 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003174
3175 if (phydev->duplex)
3176 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3177
3178 if (phydev->speed == SPEED_1000)
3179 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni4d12bc62014-07-08 10:49:43 +02003180 else if (phydev->speed == SPEED_100)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003181 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3182
3183 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3184
3185 pp->duplex = phydev->duplex;
3186 pp->speed = phydev->speed;
3187 }
3188 }
3189
3190 if (phydev->link != pp->link) {
3191 if (!phydev->link) {
3192 pp->duplex = -1;
3193 pp->speed = 0;
3194 }
3195
3196 pp->link = phydev->link;
3197 status_change = 1;
3198 }
3199
3200 if (status_change) {
3201 if (phydev->link) {
Stas Sergeev898b29702015-04-01 20:32:49 +03003202 if (!pp->use_inband_status) {
3203 u32 val = mvreg_read(pp,
3204 MVNETA_GMAC_AUTONEG_CONFIG);
3205 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3206 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3207 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3208 val);
3209 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003210 mvneta_port_up(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003211 } else {
Stas Sergeev898b29702015-04-01 20:32:49 +03003212 if (!pp->use_inband_status) {
3213 u32 val = mvreg_read(pp,
3214 MVNETA_GMAC_AUTONEG_CONFIG);
3215 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3216 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3217 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3218 val);
3219 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003220 mvneta_port_down(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003221 }
Ezequiel Garcia0089b742014-10-31 12:57:20 -03003222 phy_print_status(phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003223 }
3224}
3225
3226static int mvneta_mdio_probe(struct mvneta_port *pp)
3227{
3228 struct phy_device *phy_dev;
3229
3230 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
3231 pp->phy_interface);
3232 if (!phy_dev) {
3233 netdev_err(pp->dev, "could not find the PHY\n");
3234 return -ENODEV;
3235 }
3236
3237 phy_dev->supported &= PHY_GBIT_FEATURES;
3238 phy_dev->advertising = phy_dev->supported;
3239
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003240 pp->link = 0;
3241 pp->duplex = 0;
3242 pp->speed = 0;
3243
3244 return 0;
3245}
3246
3247static void mvneta_mdio_remove(struct mvneta_port *pp)
3248{
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003249 struct net_device *ndev = pp->dev;
3250
3251 phy_disconnect(ndev->phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003252}
3253
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003254/* Electing a CPU must be done in an atomic way: it should be done
3255 * after or before the removal/insertion of a CPU and this function is
3256 * not reentrant.
3257 */
Maxime Ripardf8642882015-09-25 18:09:38 +02003258static void mvneta_percpu_elect(struct mvneta_port *pp)
3259{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003260 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003261
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003262 /* Use the cpu associated to the rxq when it is online, in all
3263 * the other cases, use the cpu 0 which can't be offline.
3264 */
3265 if (cpu_online(pp->rxq_def))
3266 elected_cpu = pp->rxq_def;
3267
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003268 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02003269
3270 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003271 int rxq_map = 0, txq_map = 0;
3272 int rxq;
3273
3274 for (rxq = 0; rxq < rxq_number; rxq++)
3275 if ((rxq % max_cpu) == cpu)
3276 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3277
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003278 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003279 /* Map the default receive queue queue to the
3280 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02003281 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003282 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003283
3284 /* We update the TX queue map only if we have one
3285 * queue. In this case we associate the TX queue to
3286 * the CPU bound to the default RX queue
3287 */
3288 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003289 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003290 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3291 else
3292 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3293 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3294
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003295 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3296
3297 /* Update the interrupt mask on each CPU according the
3298 * new mapping
3299 */
3300 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3301 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003302 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003303
Maxime Ripardf8642882015-09-25 18:09:38 +02003304 }
3305};
3306
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003307static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
Maxime Ripardf8642882015-09-25 18:09:38 +02003308{
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003309 int other_cpu;
3310 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3311 node_online);
Maxime Ripardf8642882015-09-25 18:09:38 +02003312 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3313
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003314
3315 spin_lock(&pp->lock);
3316 /*
3317 * Configuring the driver for a new CPU while the driver is
3318 * stopping is racy, so just avoid it.
3319 */
3320 if (pp->is_stopped) {
3321 spin_unlock(&pp->lock);
3322 return 0;
3323 }
3324 netif_tx_stop_all_queues(pp->dev);
3325
3326 /*
3327 * We have to synchronise on tha napi of each CPU except the one
3328 * just being woken up
3329 */
3330 for_each_online_cpu(other_cpu) {
3331 if (other_cpu != cpu) {
3332 struct mvneta_pcpu_port *other_port =
3333 per_cpu_ptr(pp->ports, other_cpu);
3334
3335 napi_synchronize(&other_port->napi);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003336 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003337 }
3338
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003339 /* Mask all ethernet port interrupts */
3340 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3341 napi_enable(&port->napi);
3342
3343 /*
3344 * Enable per-CPU interrupts on the CPU that is
3345 * brought up.
3346 */
3347 mvneta_percpu_enable(pp);
3348
3349 /*
3350 * Enable per-CPU interrupt on the one CPU we care
3351 * about.
3352 */
3353 mvneta_percpu_elect(pp);
3354
3355 /* Unmask all ethernet port interrupts */
3356 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3357 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3358 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3359 MVNETA_CAUSE_LINK_CHANGE |
3360 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3361 netif_tx_start_all_queues(pp->dev);
3362 spin_unlock(&pp->lock);
3363 return 0;
3364}
3365
3366static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3367{
3368 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3369 node_online);
3370 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3371
3372 /*
3373 * Thanks to this lock we are sure that any pending cpu election is
3374 * done.
3375 */
3376 spin_lock(&pp->lock);
3377 /* Mask all ethernet port interrupts */
3378 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3379 spin_unlock(&pp->lock);
3380
3381 napi_synchronize(&port->napi);
3382 napi_disable(&port->napi);
3383 /* Disable per-CPU interrupts on the CPU that is brought down. */
3384 mvneta_percpu_disable(pp);
3385 return 0;
3386}
3387
3388static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3389{
3390 struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3391 node_dead);
3392
3393 /* Check if a new CPU must be elected now this on is down */
3394 spin_lock(&pp->lock);
3395 mvneta_percpu_elect(pp);
3396 spin_unlock(&pp->lock);
3397 /* Unmask all ethernet port interrupts */
3398 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3399 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3400 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3401 MVNETA_CAUSE_LINK_CHANGE |
3402 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3403 netif_tx_start_all_queues(pp->dev);
3404 return 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003405}
3406
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003407static int mvneta_open(struct net_device *dev)
3408{
3409 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003410 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003411
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003412 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003413 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3414 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003415
3416 ret = mvneta_setup_rxqs(pp);
3417 if (ret)
3418 return ret;
3419
3420 ret = mvneta_setup_txqs(pp);
3421 if (ret)
3422 goto err_cleanup_rxqs;
3423
3424 /* Connect to port interrupt line */
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003425 ret = request_percpu_irq(pp->dev->irq, mvneta_isr,
3426 MVNETA_DRIVER_NAME, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003427 if (ret) {
3428 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3429 goto err_cleanup_txqs;
3430 }
3431
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003432 /* Enable per-CPU interrupt on all the CPU to handle our RX
3433 * queue interrupts
3434 */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003435 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003436
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003437 pp->is_stopped = false;
Maxime Ripardf8642882015-09-25 18:09:38 +02003438 /* Register a CPU notifier to handle the case where our CPU
3439 * might be taken offline.
3440 */
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003441 ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3442 &pp->node_online);
3443 if (ret)
3444 goto err_free_irq;
3445
3446 ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3447 &pp->node_dead);
3448 if (ret)
3449 goto err_free_online_hp;
Maxime Ripardf8642882015-09-25 18:09:38 +02003450
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003451 /* In default link is down */
3452 netif_carrier_off(pp->dev);
3453
3454 ret = mvneta_mdio_probe(pp);
3455 if (ret < 0) {
3456 netdev_err(dev, "cannot probe MDIO bus\n");
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003457 goto err_free_dead_hp;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003458 }
3459
3460 mvneta_start_dev(pp);
3461
3462 return 0;
3463
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003464err_free_dead_hp:
3465 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3466 &pp->node_dead);
3467err_free_online_hp:
3468 cpuhp_state_remove_instance_nocalls(online_hpstate, &pp->node_online);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003469err_free_irq:
Russell King - ARM Linux3d8c4532016-06-30 10:36:15 +01003470 on_each_cpu(mvneta_percpu_disable, pp, true);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003471 free_percpu_irq(pp->dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003472err_cleanup_txqs:
3473 mvneta_cleanup_txqs(pp);
3474err_cleanup_rxqs:
3475 mvneta_cleanup_rxqs(pp);
3476 return ret;
3477}
3478
3479/* Stop the port, free port interrupt line */
3480static int mvneta_stop(struct net_device *dev)
3481{
3482 struct mvneta_port *pp = netdev_priv(dev);
3483
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003484 /* Inform that we are stopping so we don't want to setup the
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003485 * driver for new CPUs in the notifiers. The code of the
3486 * notifier for CPU online is protected by the same spinlock,
3487 * so when we get the lock, the notifer work is done.
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003488 */
3489 spin_lock(&pp->lock);
3490 pp->is_stopped = true;
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01003491 spin_unlock(&pp->lock);
3492
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003493 mvneta_stop_dev(pp);
3494 mvneta_mdio_remove(pp);
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02003495
3496 cpuhp_state_remove_instance_nocalls(online_hpstate, &pp->node_online);
3497 cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3498 &pp->node_dead);
Gregory CLEMENT129219e2016-02-04 22:09:23 +01003499 on_each_cpu(mvneta_percpu_disable, pp, true);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003500 free_percpu_irq(dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003501 mvneta_cleanup_rxqs(pp);
3502 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003503
3504 return 0;
3505}
3506
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003507static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3508{
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003509 if (!dev->phydev)
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003510 return -ENOTSUPP;
3511
Philippe Reynesc6c022e2016-07-30 17:42:11 +02003512 return phy_mii_ioctl(dev->phydev, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003513}
3514
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003515/* Ethtool methods */
3516
Philippe Reynes013ad402016-07-30 17:42:12 +02003517/* Set link ksettings (phy address, speed) for ethtools */
Baoyou Xie2dc0d2b2016-09-25 17:20:41 +08003518static int
3519mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3520 const struct ethtool_link_ksettings *cmd)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003521{
Philippe Reynes013ad402016-07-30 17:42:12 +02003522 struct mvneta_port *pp = netdev_priv(ndev);
3523 struct phy_device *phydev = ndev->phydev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003524
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003525 if (!phydev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003526 return -ENODEV;
3527
Philippe Reynes013ad402016-07-30 17:42:12 +02003528 if ((cmd->base.autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003529 u32 val;
3530
Philippe Reynes013ad402016-07-30 17:42:12 +02003531 mvneta_set_autoneg(pp, cmd->base.autoneg == AUTONEG_ENABLE);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003532
Philippe Reynes013ad402016-07-30 17:42:12 +02003533 if (cmd->base.autoneg == AUTONEG_DISABLE) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003534 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3535 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3536 MVNETA_GMAC_CONFIG_GMII_SPEED |
3537 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3538
3539 if (phydev->duplex)
3540 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3541
3542 if (phydev->speed == SPEED_1000)
3543 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3544 else if (phydev->speed == SPEED_100)
3545 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3546
3547 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3548 }
3549
Philippe Reynes013ad402016-07-30 17:42:12 +02003550 pp->use_inband_status = (cmd->base.autoneg == AUTONEG_ENABLE);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003551 netdev_info(pp->dev, "autoneg status set to %i\n",
3552 pp->use_inband_status);
3553
Philippe Reynes013ad402016-07-30 17:42:12 +02003554 if (netif_running(ndev)) {
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003555 mvneta_port_down(pp);
3556 mvneta_port_up(pp);
3557 }
3558 }
3559
Philippe Reynes013ad402016-07-30 17:42:12 +02003560 return phy_ethtool_ksettings_set(ndev->phydev, cmd);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003561}
3562
3563/* Set interrupt coalescing for ethtools */
3564static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3565 struct ethtool_coalesce *c)
3566{
3567 struct mvneta_port *pp = netdev_priv(dev);
3568 int queue;
3569
3570 for (queue = 0; queue < rxq_number; queue++) {
3571 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3572 rxq->time_coal = c->rx_coalesce_usecs;
3573 rxq->pkts_coal = c->rx_max_coalesced_frames;
3574 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3575 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3576 }
3577
3578 for (queue = 0; queue < txq_number; queue++) {
3579 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3580 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3581 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3582 }
3583
3584 return 0;
3585}
3586
3587/* get coalescing for ethtools */
3588static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3589 struct ethtool_coalesce *c)
3590{
3591 struct mvneta_port *pp = netdev_priv(dev);
3592
3593 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3594 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3595
3596 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3597 return 0;
3598}
3599
3600
3601static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3602 struct ethtool_drvinfo *drvinfo)
3603{
3604 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3605 sizeof(drvinfo->driver));
3606 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3607 sizeof(drvinfo->version));
3608 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3609 sizeof(drvinfo->bus_info));
3610}
3611
3612
3613static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3614 struct ethtool_ringparam *ring)
3615{
3616 struct mvneta_port *pp = netdev_priv(netdev);
3617
3618 ring->rx_max_pending = MVNETA_MAX_RXD;
3619 ring->tx_max_pending = MVNETA_MAX_TXD;
3620 ring->rx_pending = pp->rx_ring_size;
3621 ring->tx_pending = pp->tx_ring_size;
3622}
3623
3624static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3625 struct ethtool_ringparam *ring)
3626{
3627 struct mvneta_port *pp = netdev_priv(dev);
3628
3629 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3630 return -EINVAL;
3631 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3632 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003633
3634 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3635 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3636 if (pp->tx_ring_size != ring->tx_pending)
3637 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3638 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003639
3640 if (netif_running(dev)) {
3641 mvneta_stop(dev);
3642 if (mvneta_open(dev)) {
3643 netdev_err(dev,
3644 "error on opening device after ring param change\n");
3645 return -ENOMEM;
3646 }
3647 }
3648
3649 return 0;
3650}
3651
Russell King9b0cdef2015-10-22 18:37:30 +01003652static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3653 u8 *data)
3654{
3655 if (sset == ETH_SS_STATS) {
3656 int i;
3657
3658 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3659 memcpy(data + i * ETH_GSTRING_LEN,
3660 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3661 }
3662}
3663
3664static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3665{
3666 const struct mvneta_statistic *s;
3667 void __iomem *base = pp->base;
3668 u32 high, low, val;
Jisheng Zhang2c832292016-01-20 16:36:25 +08003669 u64 val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003670 int i;
3671
3672 for (i = 0, s = mvneta_statistics;
3673 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3674 s++, i++) {
Russell King9b0cdef2015-10-22 18:37:30 +01003675 switch (s->type) {
3676 case T_REG_32:
3677 val = readl_relaxed(base + s->offset);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003678 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003679 break;
3680 case T_REG_64:
3681 /* Docs say to read low 32-bit then high */
3682 low = readl_relaxed(base + s->offset);
3683 high = readl_relaxed(base + s->offset + 4);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003684 val64 = (u64)high << 32 | low;
3685 pp->ethtool_stats[i] += val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003686 break;
3687 }
Russell King9b0cdef2015-10-22 18:37:30 +01003688 }
3689}
3690
3691static void mvneta_ethtool_get_stats(struct net_device *dev,
3692 struct ethtool_stats *stats, u64 *data)
3693{
3694 struct mvneta_port *pp = netdev_priv(dev);
3695 int i;
3696
3697 mvneta_ethtool_update_stats(pp);
3698
3699 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3700 *data++ = pp->ethtool_stats[i];
3701}
3702
3703static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3704{
3705 if (sset == ETH_SS_STATS)
3706 return ARRAY_SIZE(mvneta_statistics);
3707 return -EOPNOTSUPP;
3708}
3709
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003710static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3711{
3712 return MVNETA_RSS_LU_TABLE_SIZE;
3713}
3714
3715static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3716 struct ethtool_rxnfc *info,
3717 u32 *rules __always_unused)
3718{
3719 switch (info->cmd) {
3720 case ETHTOOL_GRXRINGS:
3721 info->data = rxq_number;
3722 return 0;
3723 case ETHTOOL_GRXFH:
3724 return -EOPNOTSUPP;
3725 default:
3726 return -EOPNOTSUPP;
3727 }
3728}
3729
3730static int mvneta_config_rss(struct mvneta_port *pp)
3731{
3732 int cpu;
3733 u32 val;
3734
3735 netif_tx_stop_all_queues(pp->dev);
3736
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003737 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003738
3739 /* We have to synchronise on the napi of each CPU */
3740 for_each_online_cpu(cpu) {
3741 struct mvneta_pcpu_port *pcpu_port =
3742 per_cpu_ptr(pp->ports, cpu);
3743
3744 napi_synchronize(&pcpu_port->napi);
3745 napi_disable(&pcpu_port->napi);
3746 }
3747
3748 pp->rxq_def = pp->indir[0];
3749
3750 /* Update unicast mapping */
3751 mvneta_set_rx_mode(pp->dev);
3752
3753 /* Update val of portCfg register accordingly with all RxQueue types */
3754 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3755 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3756
3757 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003758 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003759 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003760 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003761
3762 /* We have to synchronise on the napi of each CPU */
3763 for_each_online_cpu(cpu) {
3764 struct mvneta_pcpu_port *pcpu_port =
3765 per_cpu_ptr(pp->ports, cpu);
3766
3767 napi_enable(&pcpu_port->napi);
3768 }
3769
3770 netif_tx_start_all_queues(pp->dev);
3771
3772 return 0;
3773}
3774
3775static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3776 const u8 *key, const u8 hfunc)
3777{
3778 struct mvneta_port *pp = netdev_priv(dev);
3779 /* We require at least one supported parameter to be changed
3780 * and no change in any of the unsupported parameters
3781 */
3782 if (key ||
3783 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3784 return -EOPNOTSUPP;
3785
3786 if (!indir)
3787 return 0;
3788
3789 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3790
3791 return mvneta_config_rss(pp);
3792}
3793
3794static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3795 u8 *hfunc)
3796{
3797 struct mvneta_port *pp = netdev_priv(dev);
3798
3799 if (hfunc)
3800 *hfunc = ETH_RSS_HASH_TOP;
3801
3802 if (!indir)
3803 return 0;
3804
3805 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3806
3807 return 0;
3808}
3809
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003810static const struct net_device_ops mvneta_netdev_ops = {
3811 .ndo_open = mvneta_open,
3812 .ndo_stop = mvneta_stop,
3813 .ndo_start_xmit = mvneta_tx,
3814 .ndo_set_rx_mode = mvneta_set_rx_mode,
3815 .ndo_set_mac_address = mvneta_set_mac_addr,
3816 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02003817 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003818 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003819 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003820};
3821
3822const struct ethtool_ops mvneta_eth_tool_ops = {
Florian Fainelli5489ee82016-11-15 11:19:47 -08003823 .nway_reset = phy_ethtool_nway_reset,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003824 .get_link = ethtool_op_get_link,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003825 .set_coalesce = mvneta_ethtool_set_coalesce,
3826 .get_coalesce = mvneta_ethtool_get_coalesce,
3827 .get_drvinfo = mvneta_ethtool_get_drvinfo,
3828 .get_ringparam = mvneta_ethtool_get_ringparam,
3829 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King9b0cdef2015-10-22 18:37:30 +01003830 .get_strings = mvneta_ethtool_get_strings,
3831 .get_ethtool_stats = mvneta_ethtool_get_stats,
3832 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003833 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
3834 .get_rxnfc = mvneta_ethtool_get_rxnfc,
3835 .get_rxfh = mvneta_ethtool_get_rxfh,
3836 .set_rxfh = mvneta_ethtool_set_rxfh,
Philippe Reynes013ad402016-07-30 17:42:12 +02003837 .get_link_ksettings = phy_ethtool_get_link_ksettings,
3838 .set_link_ksettings = mvneta_ethtool_set_link_ksettings,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003839};
3840
3841/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03003842static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003843{
3844 int queue;
3845
3846 /* Disable port */
3847 mvneta_port_disable(pp);
3848
3849 /* Set port default values */
3850 mvneta_defaults_set(pp);
3851
Ezequiel Garcia96728502014-05-22 20:06:59 -03003852 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
3853 GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003854 if (!pp->txqs)
3855 return -ENOMEM;
3856
3857 /* Initialize TX descriptor rings */
3858 for (queue = 0; queue < txq_number; queue++) {
3859 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3860 txq->id = queue;
3861 txq->size = pp->tx_ring_size;
3862 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
3863 }
3864
Ezequiel Garcia96728502014-05-22 20:06:59 -03003865 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
3866 GFP_KERNEL);
3867 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003868 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003869
3870 /* Create Rx descriptor rings */
3871 for (queue = 0; queue < rxq_number; queue++) {
3872 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3873 rxq->id = queue;
3874 rxq->size = pp->rx_ring_size;
3875 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
3876 rxq->time_coal = MVNETA_RX_COAL_USEC;
Gregory CLEMENTf88bee12016-12-01 18:03:06 +01003877 rxq->buf_virt_addr = devm_kmalloc(pp->dev->dev.parent,
3878 rxq->size * sizeof(void *),
3879 GFP_KERNEL);
3880 if (!rxq->buf_virt_addr)
3881 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003882 }
3883
3884 return 0;
3885}
3886
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003887/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00003888static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
3889 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003890{
3891 u32 win_enable;
3892 u32 win_protect;
3893 int i;
3894
3895 for (i = 0; i < 6; i++) {
3896 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
3897 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
3898
3899 if (i < 4)
3900 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
3901 }
3902
3903 win_enable = 0x3f;
3904 win_protect = 0;
3905
3906 for (i = 0; i < dram->num_cs; i++) {
3907 const struct mbus_dram_window *cs = dram->cs + i;
3908 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
3909 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
3910
3911 mvreg_write(pp, MVNETA_WIN_SIZE(i),
3912 (cs->size - 1) & 0xffff0000);
3913
3914 win_enable &= ~(1 << i);
3915 win_protect |= 3 << (2 * i);
3916 }
3917
3918 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01003919 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003920}
3921
3922/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003923static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003924{
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003925 u32 ctrl;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003926
3927 /* MAC Cause register should be cleared */
3928 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
3929
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003930 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003931
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003932 /* Even though it might look weird, when we're configured in
3933 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3934 */
3935 switch(phy_mode) {
3936 case PHY_INTERFACE_MODE_QSGMII:
3937 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
3938 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
3939 break;
3940 case PHY_INTERFACE_MODE_SGMII:
3941 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
3942 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
3943 break;
3944 case PHY_INTERFACE_MODE_RGMII:
3945 case PHY_INTERFACE_MODE_RGMII_ID:
3946 ctrl |= MVNETA_GMAC2_PORT_RGMII;
3947 break;
3948 default:
3949 return -EINVAL;
3950 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003951
3952 /* Cancel Port Reset */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003953 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
3954 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003955
3956 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3957 MVNETA_GMAC2_PORT_RESET) != 0)
3958 continue;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003959
3960 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003961}
3962
3963/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00003964static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003965{
3966 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01003967 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003968 struct device_node *dn = pdev->dev.of_node;
3969 struct device_node *phy_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003970 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003971 struct mvneta_port *pp;
3972 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003973 const char *dt_mac_addr;
3974 char hw_mac_addr[ETH_ALEN];
3975 const char *mac_from;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07003976 const char *managed;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01003977 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003978 int phy_mode;
3979 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003980 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003981
Willy Tarreauee40a112013-04-11 23:00:37 +02003982 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003983 if (!dev)
3984 return -ENOMEM;
3985
3986 dev->irq = irq_of_parse_and_map(dn, 0);
3987 if (dev->irq == 0) {
3988 err = -EINVAL;
3989 goto err_free_netdev;
3990 }
3991
3992 phy_node = of_parse_phandle(dn, "phy", 0);
3993 if (!phy_node) {
Thomas Petazzoni83895be2014-05-16 16:14:06 +02003994 if (!of_phy_is_fixed_link(dn)) {
3995 dev_err(&pdev->dev, "no PHY specified\n");
3996 err = -ENODEV;
3997 goto err_free_irq;
3998 }
3999
4000 err = of_phy_register_fixed_link(dn);
4001 if (err < 0) {
4002 dev_err(&pdev->dev, "cannot register fixed PHY\n");
4003 goto err_free_irq;
4004 }
4005
4006 /* In the case of a fixed PHY, the DT node associated
4007 * to the PHY is the Ethernet MAC DT node.
4008 */
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004009 phy_node = of_node_get(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004010 }
4011
4012 phy_mode = of_get_phy_mode(dn);
4013 if (phy_mode < 0) {
4014 dev_err(&pdev->dev, "incorrect phy-mode\n");
4015 err = -EINVAL;
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004016 goto err_put_phy_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004017 }
4018
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004019 dev->tx_queue_len = MVNETA_MAX_TXD;
4020 dev->watchdog_timeo = 5 * HZ;
4021 dev->netdev_ops = &mvneta_netdev_ops;
4022
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004023 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004024
4025 pp = netdev_priv(dev);
Gregory CLEMENT1c2722a2016-03-12 18:44:17 +01004026 spin_lock_init(&pp->lock);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004027 pp->phy_node = phy_node;
4028 pp->phy_interface = phy_mode;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07004029
4030 err = of_property_read_string(dn, "managed", &managed);
4031 pp->use_inband_status = (err == 0 &&
4032 strcmp(managed, "in-band-status") == 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004033
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01004034 pp->rxq_def = rxq_def;
4035
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004036 pp->indir[0] = rxq_def;
4037
Jisheng Zhang2804ba42016-01-20 19:27:23 +08004038 pp->clk = devm_clk_get(&pdev->dev, "core");
4039 if (IS_ERR(pp->clk))
4040 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004041 if (IS_ERR(pp->clk)) {
4042 err = PTR_ERR(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004043 goto err_put_phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004044 }
4045
4046 clk_prepare_enable(pp->clk);
4047
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004048 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4049 if (!IS_ERR(pp->clk_bus))
4050 clk_prepare_enable(pp->clk_bus);
4051
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004052 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4053 pp->base = devm_ioremap_resource(&pdev->dev, res);
4054 if (IS_ERR(pp->base)) {
4055 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004056 goto err_clk;
4057 }
4058
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004059 /* Alloc per-cpu port structure */
4060 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4061 if (!pp->ports) {
4062 err = -ENOMEM;
4063 goto err_clk;
4064 }
4065
willy tarreau74c41b02014-01-16 08:20:08 +01004066 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08004067 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01004068 if (!pp->stats) {
4069 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004070 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01004071 }
4072
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004073 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01004074 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004075 mac_from = "device tree";
4076 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4077 } else {
4078 mvneta_get_mac_addr(pp, hw_mac_addr);
4079 if (is_valid_ether_addr(hw_mac_addr)) {
4080 mac_from = "hardware";
4081 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4082 } else {
4083 mac_from = "random";
4084 eth_hw_addr_random(dev);
4085 }
4086 }
4087
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004088 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4089 if (tx_csum_limit < 0 ||
4090 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4091 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4092 dev_info(&pdev->dev,
4093 "Wrong TX csum limit in DT, set to %dB\n",
4094 MVNETA_TX_CSUM_DEF_SIZE);
4095 }
4096 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4097 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4098 } else {
4099 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4100 }
4101
4102 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02004103
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004104 dram_target_info = mv_mbus_dram_info();
4105 if (dram_target_info)
4106 mvneta_conf_mbus_windows(pp, dram_target_info);
4107
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004108 pp->tx_ring_size = MVNETA_MAX_TXD;
4109 pp->rx_ring_size = MVNETA_MAX_RXD;
4110
4111 pp->dev = dev;
4112 SET_NETDEV_DEV(dev, &pdev->dev);
4113
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004114 pp->id = global_port_id++;
4115
4116 /* Obtain access to BM resources if enabled and already initialized */
4117 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4118 if (bm_node && bm_node->data) {
4119 pp->bm_priv = bm_node->data;
4120 err = mvneta_bm_port_init(pdev, pp);
4121 if (err < 0) {
4122 dev_info(&pdev->dev, "use SW buffer management\n");
4123 pp->bm_priv = NULL;
4124 }
4125 }
Peter Chend4e4da02016-08-01 15:02:36 +08004126 of_node_put(bm_node);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004127
Ezequiel Garcia96728502014-05-22 20:06:59 -03004128 err = mvneta_init(&pdev->dev, pp);
4129 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004130 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004131
4132 err = mvneta_port_power_up(pp, phy_mode);
4133 if (err < 0) {
4134 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004135 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004136 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004137
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004138 for_each_present_cpu(cpu) {
4139 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4140
4141 netif_napi_add(dev, &port->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4142 port->pp = pp;
4143 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004144
Ezequiel Garcia2adb719d2014-05-19 13:59:55 -03004145 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03004146 dev->hw_features |= dev->features;
4147 dev->vlan_features |= dev->features;
Andrew Lunn97db8af2016-11-24 00:08:13 +01004148 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004149 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00004150
Jarod Wilson57779872016-10-17 15:54:06 -04004151 /* MTU range: 68 - 9676 */
4152 dev->min_mtu = ETH_MIN_MTU;
4153 /* 9676 == 9700 - 20 and rounding to 8 */
4154 dev->max_mtu = 9676;
4155
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004156 err = register_netdev(dev);
4157 if (err < 0) {
4158 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03004159 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004160 }
4161
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004162 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4163 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004164
4165 platform_set_drvdata(pdev, pp->dev);
4166
Stas Sergeev898b29702015-04-01 20:32:49 +03004167 if (pp->use_inband_status) {
4168 struct phy_device *phy = of_phy_find_device(dn);
4169
4170 mvneta_fixed_link_update(pp, phy);
Russell King04d53b22015-09-24 20:36:18 +01004171
Andrew Lunne5a03bf2016-01-06 20:11:16 +01004172 put_device(&phy->mdio.dev);
Stas Sergeev898b29702015-04-01 20:32:49 +03004173 }
4174
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004175 return 0;
4176
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004177err_netdev:
4178 unregister_netdev(dev);
4179 if (pp->bm_priv) {
4180 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4181 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4182 1 << pp->id);
4183 }
willy tarreau74c41b02014-01-16 08:20:08 +01004184err_free_stats:
4185 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004186err_free_ports:
4187 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004188err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004189 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004190 clk_disable_unprepare(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004191err_put_phy_node:
4192 of_node_put(phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004193err_free_irq:
4194 irq_dispose_mapping(dev->irq);
4195err_free_netdev:
4196 free_netdev(dev);
4197 return err;
4198}
4199
4200/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00004201static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004202{
4203 struct net_device *dev = platform_get_drvdata(pdev);
4204 struct mvneta_port *pp = netdev_priv(dev);
4205
4206 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004207 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004208 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004209 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01004210 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004211 irq_dispose_mapping(dev->irq);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004212 of_node_put(pp->phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004213 free_netdev(dev);
4214
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004215 if (pp->bm_priv) {
4216 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4217 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4218 1 << pp->id);
4219 }
4220
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004221 return 0;
4222}
4223
4224static const struct of_device_id mvneta_match[] = {
4225 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02004226 { .compatible = "marvell,armada-xp-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004227 { }
4228};
4229MODULE_DEVICE_TABLE(of, mvneta_match);
4230
4231static struct platform_driver mvneta_driver = {
4232 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00004233 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004234 .driver = {
4235 .name = MVNETA_DRIVER_NAME,
4236 .of_match_table = mvneta_match,
4237 },
4238};
4239
Sebastian Andrzej Siewior84a3f4d2016-08-18 14:57:23 +02004240static int __init mvneta_driver_init(void)
4241{
4242 int ret;
4243
4244 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4245 mvneta_cpu_online,
4246 mvneta_cpu_down_prepare);
4247 if (ret < 0)
4248 goto out;
4249 online_hpstate = ret;
4250 ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4251 NULL, mvneta_cpu_dead);
4252 if (ret)
4253 goto err_dead;
4254
4255 ret = platform_driver_register(&mvneta_driver);
4256 if (ret)
4257 goto err;
4258 return 0;
4259
4260err:
4261 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4262err_dead:
4263 cpuhp_remove_multi_state(online_hpstate);
4264out:
4265 return ret;
4266}
4267module_init(mvneta_driver_init);
4268
4269static void __exit mvneta_driver_exit(void)
4270{
4271 platform_driver_unregister(&mvneta_driver);
4272 cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4273 cpuhp_remove_multi_state(online_hpstate);
4274}
4275module_exit(mvneta_driver_exit);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004276
4277MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4278MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4279MODULE_LICENSE("GPL");
4280
4281module_param(rxq_number, int, S_IRUGO);
4282module_param(txq_number, int, S_IRUGO);
4283
4284module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01004285module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);