blob: 2847c0c291de67cc2efe54b05d92cbc0cbe38290 [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>
Marcin Wojtasdc35a102016-03-14 09:39:03 +010033#include "mvneta_bm.h"
Jisheng Zhang0e03f562016-01-20 19:27:22 +080034#include <net/ip.h>
35#include <net/ipv6.h>
36#include <net/tso.h>
Thomas Petazzonic5aff182012-08-17 14:04:28 +030037
38/* Registers */
39#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
Marcin Wojtase5bdf682015-11-30 13:27:42 +010040#define MVNETA_RXQ_HW_BUF_ALLOC BIT(0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +010041#define MVNETA_RXQ_SHORT_POOL_ID_SHIFT 4
42#define MVNETA_RXQ_SHORT_POOL_ID_MASK 0x30
43#define MVNETA_RXQ_LONG_POOL_ID_SHIFT 6
44#define MVNETA_RXQ_LONG_POOL_ID_MASK 0xc0
Thomas Petazzonic5aff182012-08-17 14:04:28 +030045#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
46#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
47#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
48#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
49#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
50#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
51#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
52#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
53#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
54#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
55#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
56#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
57#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
Marcin Wojtasdc35a102016-03-14 09:39:03 +010058#define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool) (0x1700 + ((pool) << 2))
59#define MVNETA_PORT_POOL_BUFFER_SZ_SHIFT 3
60#define MVNETA_PORT_POOL_BUFFER_SZ_MASK 0xfff8
Thomas Petazzonic5aff182012-08-17 14:04:28 +030061#define MVNETA_PORT_RX_RESET 0x1cc0
62#define MVNETA_PORT_RX_DMA_RESET BIT(0)
63#define MVNETA_PHY_ADDR 0x2000
64#define MVNETA_PHY_ADDR_MASK 0x1f
65#define MVNETA_MBUS_RETRY 0x2010
66#define MVNETA_UNIT_INTR_CAUSE 0x2080
67#define MVNETA_UNIT_CONTROL 0x20B0
68#define MVNETA_PHY_POLLING_ENABLE BIT(1)
69#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
70#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
71#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
72#define MVNETA_BASE_ADDR_ENABLE 0x2290
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +010073#define MVNETA_ACCESS_PROTECT_ENABLE 0x2294
Thomas Petazzonic5aff182012-08-17 14:04:28 +030074#define MVNETA_PORT_CONFIG 0x2400
75#define MVNETA_UNI_PROMISC_MODE BIT(0)
76#define MVNETA_DEF_RXQ(q) ((q) << 1)
77#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
78#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
79#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
80#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
81#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
82#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
83#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
84 MVNETA_DEF_RXQ_ARP(q) | \
85 MVNETA_DEF_RXQ_TCP(q) | \
86 MVNETA_DEF_RXQ_UDP(q) | \
87 MVNETA_DEF_RXQ_BPDU(q) | \
88 MVNETA_TX_UNSET_ERR_SUM | \
89 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
90#define MVNETA_PORT_CONFIG_EXTEND 0x2404
91#define MVNETA_MAC_ADDR_LOW 0x2414
92#define MVNETA_MAC_ADDR_HIGH 0x2418
93#define MVNETA_SDMA_CONFIG 0x241c
94#define MVNETA_SDMA_BRST_SIZE_16 4
Thomas Petazzonic5aff182012-08-17 14:04:28 +030095#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
96#define MVNETA_RX_NO_DATA_SWAP BIT(4)
97#define MVNETA_TX_NO_DATA_SWAP BIT(5)
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +020098#define MVNETA_DESC_SWAP BIT(6)
Thomas Petazzonic5aff182012-08-17 14:04:28 +030099#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
100#define MVNETA_PORT_STATUS 0x2444
101#define MVNETA_TX_IN_PRGRS BIT(1)
102#define MVNETA_TX_FIFO_EMPTY BIT(8)
103#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200104#define MVNETA_SERDES_CFG 0x24A0
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +0200105#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +0200106#define MVNETA_QSGMII_SERDES_PROTO 0x0667
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300107#define MVNETA_TYPE_PRIO 0x24bc
108#define MVNETA_FORCE_UNI BIT(21)
109#define MVNETA_TXQ_CMD_1 0x24e4
110#define MVNETA_TXQ_CMD 0x2448
111#define MVNETA_TXQ_DISABLE_SHIFT 8
112#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
Andrew Lunne4839112015-10-22 18:37:36 +0100113#define MVNETA_RX_DISCARD_FRAME_COUNT 0x2484
114#define MVNETA_OVERRUN_FRAME_COUNT 0x2488
Stas Sergeev898b2972015-04-01 20:32:49 +0300115#define MVNETA_GMAC_CLOCK_DIVIDER 0x24f4
116#define MVNETA_GMAC_1MS_CLOCK_ENABLE BIT(31)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300117#define MVNETA_ACC_MODE 0x2500
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100118#define MVNETA_BM_ADDRESS 0x2504
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300119#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
120#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
121#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100122#define MVNETA_CPU_RXQ_ACCESS(rxq) BIT(rxq)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +0100123#define MVNETA_CPU_TXQ_ACCESS(txq) BIT(txq + 8)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300124#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
willy tarreau40ba35e2014-01-16 08:20:10 +0100125
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +0100126/* Exception Interrupt Port/Queue Cause register
127 *
128 * Their behavior depend of the mapping done using the PCPX2Q
129 * registers. For a given CPU if the bit associated to a queue is not
130 * set, then for the register a read from this CPU will always return
131 * 0 and a write won't do anything
132 */
willy tarreau40ba35e2014-01-16 08:20:10 +0100133
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300134#define MVNETA_INTR_NEW_CAUSE 0x25a0
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300135#define MVNETA_INTR_NEW_MASK 0x25a4
willy tarreau40ba35e2014-01-16 08:20:10 +0100136
137/* bits 0..7 = TXQ SENT, one bit per queue.
138 * bits 8..15 = RXQ OCCUP, one bit per queue.
139 * bits 16..23 = RXQ FREE, one bit per queue.
140 * bit 29 = OLD_REG_SUM, see old reg ?
141 * bit 30 = TX_ERR_SUM, one bit for 4 ports
142 * bit 31 = MISC_SUM, one bit for 4 ports
143 */
144#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
145#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
146#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
147#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
Stas Sergeev898b2972015-04-01 20:32:49 +0300148#define MVNETA_MISCINTR_INTR_MASK BIT(31)
willy tarreau40ba35e2014-01-16 08:20:10 +0100149
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300150#define MVNETA_INTR_OLD_CAUSE 0x25a8
151#define MVNETA_INTR_OLD_MASK 0x25ac
willy tarreau40ba35e2014-01-16 08:20:10 +0100152
153/* Data Path Port/Queue Cause Register */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300154#define MVNETA_INTR_MISC_CAUSE 0x25b0
155#define MVNETA_INTR_MISC_MASK 0x25b4
willy tarreau40ba35e2014-01-16 08:20:10 +0100156
157#define MVNETA_CAUSE_PHY_STATUS_CHANGE BIT(0)
158#define MVNETA_CAUSE_LINK_CHANGE BIT(1)
159#define MVNETA_CAUSE_PTP BIT(4)
160
161#define MVNETA_CAUSE_INTERNAL_ADDR_ERR BIT(7)
162#define MVNETA_CAUSE_RX_OVERRUN BIT(8)
163#define MVNETA_CAUSE_RX_CRC_ERROR BIT(9)
164#define MVNETA_CAUSE_RX_LARGE_PKT BIT(10)
165#define MVNETA_CAUSE_TX_UNDERUN BIT(11)
166#define MVNETA_CAUSE_PRBS_ERR BIT(12)
167#define MVNETA_CAUSE_PSC_SYNC_CHANGE BIT(13)
168#define MVNETA_CAUSE_SERDES_SYNC_ERR BIT(14)
169
170#define MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT 16
171#define MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
172#define MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
173
174#define MVNETA_CAUSE_TXQ_ERROR_SHIFT 24
175#define MVNETA_CAUSE_TXQ_ERROR_ALL_MASK (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
176#define MVNETA_CAUSE_TXQ_ERROR_MASK(q) (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
177
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300178#define MVNETA_INTR_ENABLE 0x25b8
179#define MVNETA_TXQ_INTR_ENABLE_ALL_MASK 0x0000ff00
Marcin Wojtasdc1aadf2015-11-30 13:27:43 +0100180#define MVNETA_RXQ_INTR_ENABLE_ALL_MASK 0x000000ff
willy tarreau40ba35e2014-01-16 08:20:10 +0100181
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300182#define MVNETA_RXQ_CMD 0x2680
183#define MVNETA_RXQ_DISABLE_SHIFT 8
184#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
185#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
186#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
187#define MVNETA_GMAC_CTRL_0 0x2c00
188#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
189#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
190#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
191#define MVNETA_GMAC_CTRL_2 0x2c08
Stas Sergeev898b2972015-04-01 20:32:49 +0300192#define MVNETA_GMAC2_INBAND_AN_ENABLE BIT(0)
Thomas Petazzonia79121d2014-03-26 00:25:41 +0100193#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300194#define MVNETA_GMAC2_PORT_RGMII BIT(4)
195#define MVNETA_GMAC2_PORT_RESET BIT(6)
196#define MVNETA_GMAC_STATUS 0x2c10
197#define MVNETA_GMAC_LINK_UP BIT(0)
198#define MVNETA_GMAC_SPEED_1000 BIT(1)
199#define MVNETA_GMAC_SPEED_100 BIT(2)
200#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
201#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
202#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
203#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
204#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
205#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
206#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
207#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Stas Sergeev898b2972015-04-01 20:32:49 +0300208#define MVNETA_GMAC_INBAND_AN_ENABLE BIT(2)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300209#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
210#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200211#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Stas Sergeev898b2972015-04-01 20:32:49 +0300212#define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300213#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
Thomas Petazzoni71408602013-09-04 16:21:18 +0200214#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Andrew Lunne4839112015-10-22 18:37:36 +0100215#define MVNETA_MIB_COUNTERS_BASE 0x3000
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300216#define MVNETA_MIB_LATE_COLLISION 0x7c
217#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
218#define MVNETA_DA_FILT_OTH_MCAST 0x3500
219#define MVNETA_DA_FILT_UCAST_BASE 0x3600
220#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
221#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
222#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
223#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
224#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
225#define MVNETA_TXQ_DEC_SENT_SHIFT 16
226#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
227#define MVNETA_TXQ_SENT_DESC_SHIFT 16
228#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
229#define MVNETA_PORT_TX_RESET 0x3cf0
230#define MVNETA_PORT_TX_DMA_RESET BIT(0)
231#define MVNETA_TX_MTU 0x3e0c
232#define MVNETA_TX_TOKEN_SIZE 0x3e14
233#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
234#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
235#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
236
237#define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK 0xff
238
239/* Descriptor ring Macros */
240#define MVNETA_QUEUE_NEXT_DESC(q, index) \
241 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
242
243/* Various constants */
244
245/* Coalescing */
willy tarreauaebea2b2014-12-02 08:13:04 +0100246#define MVNETA_TXDONE_COAL_PKTS 1
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300247#define MVNETA_RX_COAL_PKTS 32
248#define MVNETA_RX_COAL_USEC 100
249
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100250/* The two bytes Marvell header. Either contains a special value used
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300251 * by Marvell switches when a specific hardware mode is enabled (not
252 * supported by this driver) or is filled automatically by zeroes on
253 * the RX side. Those two bytes being at the front of the Ethernet
254 * header, they allow to have the IP header aligned on a 4 bytes
255 * boundary automatically: the hardware skips those two bytes on its
256 * own.
257 */
258#define MVNETA_MH_SIZE 2
259
260#define MVNETA_VLAN_TAG_LEN 4
261
262#define MVNETA_CPU_D_CACHE_LINE_SIZE 32
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 Garcia2adb7192014-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, \
302 MVNETA_CPU_D_CACHE_LINE_SIZE)
303
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;
Maxime Ripardf8642882015-09-25 18:09:38 +0200385 struct notifier_block cpu_notifier;
Gregory CLEMENT90b74c02015-12-09 18:23:48 +0100386 int rxq_def;
Gregory CLEMENT58885112016-02-04 22:09:28 +0100387 /* Protect the access to the percpu interrupt registers,
388 * ensuring that the configuration remains coherent.
389 */
390 spinlock_t lock;
Gregory CLEMENT120cfa52016-02-04 22:09:29 +0100391 bool is_stopped;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300392
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300393 /* Core clock */
Thomas Petazzoni189dd622012-11-19 14:15:25 +0100394 struct clk *clk;
Jisheng Zhang15cc4a42016-01-20 19:27:24 +0800395 /* AXI clock */
396 struct clk *clk_bus;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300397 u8 mcast_count[256];
398 u16 tx_ring_size;
399 u16 rx_ring_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300400
401 struct mii_bus *mii_bus;
402 struct phy_device *phy_dev;
403 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 Garcia2adb7192014-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
564 /* Virtual address of the RX DMA descriptors array */
565 struct mvneta_rx_desc *descs;
566
567 /* DMA address of the RX DMA descriptors array */
568 dma_addr_t descs_phys;
569
570 /* Index of the last RX DMA descriptor */
571 int last_desc;
572
573 /* Index of the next RX DMA descriptor to process */
574 int next_desc_to_proc;
575};
576
Ezequiel Garciaedadb7f2014-05-22 20:07:01 -0300577/* The hardware supports eight (8) rx queues, but we are only allowing
578 * the first one to be used. Therefore, let's just allocate one queue.
579 */
Maxime Ripardd8936652015-09-25 18:09:37 +0200580static int rxq_number = 8;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300581static int txq_number = 8;
582
583static int rxq_def;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300584
willy tarreauf19fadf2014-01-16 08:20:17 +0100585static int rx_copybreak __read_mostly = 256;
586
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100587/* HW BM need that each port be identify by a unique ID */
588static int global_port_id;
589
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300590#define MVNETA_DRIVER_NAME "mvneta"
591#define MVNETA_DRIVER_VERSION "1.0"
592
593/* Utility/helper methods */
594
595/* Write helper method */
596static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
597{
598 writel(data, pp->base + offset);
599}
600
601/* Read helper method */
602static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
603{
604 return readl(pp->base + offset);
605}
606
607/* Increment txq get counter */
608static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
609{
610 txq->txq_get_index++;
611 if (txq->txq_get_index == txq->size)
612 txq->txq_get_index = 0;
613}
614
615/* Increment txq put counter */
616static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
617{
618 txq->txq_put_index++;
619 if (txq->txq_put_index == txq->size)
620 txq->txq_put_index = 0;
621}
622
623
624/* Clear all MIB counters */
625static void mvneta_mib_counters_clear(struct mvneta_port *pp)
626{
627 int i;
628 u32 dummy;
629
630 /* Perform dummy reads from MIB counters */
631 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
632 dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
Andrew Lunne4839112015-10-22 18:37:36 +0100633 dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
634 dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300635}
636
637/* Get System Network Statistics */
638struct rtnl_link_stats64 *mvneta_get_stats64(struct net_device *dev,
639 struct rtnl_link_stats64 *stats)
640{
641 struct mvneta_port *pp = netdev_priv(dev);
642 unsigned int start;
willy tarreau74c41b02014-01-16 08:20:08 +0100643 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300644
willy tarreau74c41b02014-01-16 08:20:08 +0100645 for_each_possible_cpu(cpu) {
646 struct mvneta_pcpu_stats *cpu_stats;
647 u64 rx_packets;
648 u64 rx_bytes;
649 u64 tx_packets;
650 u64 tx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300651
willy tarreau74c41b02014-01-16 08:20:08 +0100652 cpu_stats = per_cpu_ptr(pp->stats, cpu);
653 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700654 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
willy tarreau74c41b02014-01-16 08:20:08 +0100655 rx_packets = cpu_stats->rx_packets;
656 rx_bytes = cpu_stats->rx_bytes;
657 tx_packets = cpu_stats->tx_packets;
658 tx_bytes = cpu_stats->tx_bytes;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700659 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300660
willy tarreau74c41b02014-01-16 08:20:08 +0100661 stats->rx_packets += rx_packets;
662 stats->rx_bytes += rx_bytes;
663 stats->tx_packets += tx_packets;
664 stats->tx_bytes += tx_bytes;
665 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300666
667 stats->rx_errors = dev->stats.rx_errors;
668 stats->rx_dropped = dev->stats.rx_dropped;
669
670 stats->tx_dropped = dev->stats.tx_dropped;
671
672 return stats;
673}
674
675/* Rx descriptors helper methods */
676
willy tarreau54282132014-01-16 08:20:14 +0100677/* Checks whether the RX descriptor having this status is both the first
678 * and the last descriptor for the RX packet. Each RX packet is currently
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300679 * received through a single RX descriptor, so not having each RX
680 * descriptor with its first and last bits set is an error
681 */
willy tarreau54282132014-01-16 08:20:14 +0100682static int mvneta_rxq_desc_is_first_last(u32 status)
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300683{
willy tarreau54282132014-01-16 08:20:14 +0100684 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300685 MVNETA_RXD_FIRST_LAST_DESC;
686}
687
688/* Add number of descriptors ready to receive new packets */
689static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
690 struct mvneta_rx_queue *rxq,
691 int ndescs)
692{
693 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100694 * be added at once
695 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300696 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
697 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
698 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
699 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
700 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
701 }
702
703 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
704 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
705}
706
707/* Get number of RX descriptors occupied by received packets */
708static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
709 struct mvneta_rx_queue *rxq)
710{
711 u32 val;
712
713 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
714 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
715}
716
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100717/* Update num of rx desc called upon return from rx path or
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300718 * from mvneta_rxq_drop_pkts().
719 */
720static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
721 struct mvneta_rx_queue *rxq,
722 int rx_done, int rx_filled)
723{
724 u32 val;
725
726 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
727 val = rx_done |
728 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
729 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
730 return;
731 }
732
733 /* Only 255 descriptors can be added at once */
734 while ((rx_done > 0) || (rx_filled > 0)) {
735 if (rx_done <= 0xff) {
736 val = rx_done;
737 rx_done = 0;
738 } else {
739 val = 0xff;
740 rx_done -= 0xff;
741 }
742 if (rx_filled <= 0xff) {
743 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
744 rx_filled = 0;
745 } else {
746 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
747 rx_filled -= 0xff;
748 }
749 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
750 }
751}
752
753/* Get pointer to next RX descriptor to be processed by SW */
754static struct mvneta_rx_desc *
755mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
756{
757 int rx_desc = rxq->next_desc_to_proc;
758
759 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
willy tarreau34e41792014-01-16 08:20:15 +0100760 prefetch(rxq->descs + rxq->next_desc_to_proc);
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300761 return rxq->descs + rx_desc;
762}
763
764/* Change maximum receive size of the port. */
765static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
766{
767 u32 val;
768
769 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
770 val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
771 val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
772 MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
773 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
774}
775
776
777/* Set rx queue offset */
778static void mvneta_rxq_offset_set(struct mvneta_port *pp,
779 struct mvneta_rx_queue *rxq,
780 int offset)
781{
782 u32 val;
783
784 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
785 val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
786
787 /* Offset is in */
788 val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
789 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
790}
791
792
793/* Tx descriptors helper methods */
794
795/* Update HW with number of TX descriptors to be sent */
796static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
797 struct mvneta_tx_queue *txq,
798 int pend_desc)
799{
800 u32 val;
801
802 /* Only 255 descriptors can be added at once ; Assume caller
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100803 * process TX desriptors in quanta less than 256
804 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300805 val = pend_desc;
806 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
807}
808
809/* Get pointer to next TX descriptor to be processed (send) by HW */
810static struct mvneta_tx_desc *
811mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
812{
813 int tx_desc = txq->next_desc_to_proc;
814
815 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
816 return txq->descs + tx_desc;
817}
818
819/* Release the last allocated TX descriptor. Useful to handle DMA
Thomas Petazzoni6a20c172012-11-19 11:41:25 +0100820 * mapping failures in the TX path.
821 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +0300822static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
823{
824 if (txq->next_desc_to_proc == 0)
825 txq->next_desc_to_proc = txq->last_desc - 1;
826 else
827 txq->next_desc_to_proc--;
828}
829
830/* Set rxq buf size */
831static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
832 struct mvneta_rx_queue *rxq,
833 int buf_size)
834{
835 u32 val;
836
837 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
838
839 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
840 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
841
842 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
843}
844
845/* Disable buffer management (BM) */
846static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
847 struct mvneta_rx_queue *rxq)
848{
849 u32 val;
850
851 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
852 val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
853 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
854}
855
Marcin Wojtasdc35a102016-03-14 09:39:03 +0100856/* Enable buffer management (BM) */
857static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
858 struct mvneta_rx_queue *rxq)
859{
860 u32 val;
861
862 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
863 val |= MVNETA_RXQ_HW_BUF_ALLOC;
864 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
865}
866
867/* Notify HW about port's assignment of pool for bigger packets */
868static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
869 struct mvneta_rx_queue *rxq)
870{
871 u32 val;
872
873 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
874 val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
875 val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
876
877 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
878}
879
880/* Notify HW about port's assignment of pool for smaller packets */
881static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
882 struct mvneta_rx_queue *rxq)
883{
884 u32 val;
885
886 val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
887 val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
888 val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
889
890 mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
891}
892
893/* Set port's receive buffer size for assigned BM pool */
894static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
895 int buf_size,
896 u8 pool_id)
897{
898 u32 val;
899
900 if (!IS_ALIGNED(buf_size, 8)) {
901 dev_warn(pp->dev->dev.parent,
902 "illegal buf_size value %d, round to %d\n",
903 buf_size, ALIGN(buf_size, 8));
904 buf_size = ALIGN(buf_size, 8);
905 }
906
907 val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
908 val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
909 mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
910}
911
912/* Configure MBUS window in order to enable access BM internal SRAM */
913static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
914 u8 target, u8 attr)
915{
916 u32 win_enable, win_protect;
917 int i;
918
919 win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
920
921 if (pp->bm_win_id < 0) {
922 /* Find first not occupied window */
923 for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
924 if (win_enable & (1 << i)) {
925 pp->bm_win_id = i;
926 break;
927 }
928 }
929 if (i == MVNETA_MAX_DECODE_WIN)
930 return -ENOMEM;
931 } else {
932 i = pp->bm_win_id;
933 }
934
935 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
936 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
937
938 if (i < 4)
939 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
940
941 mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
942 (attr << 8) | target);
943
944 mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
945
946 win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
947 win_protect |= 3 << (2 * i);
948 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
949
950 win_enable &= ~(1 << i);
951 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
952
953 return 0;
954}
955
956/* Assign and initialize pools for port. In case of fail
957 * buffer manager will remain disabled for current port.
958 */
959static int mvneta_bm_port_init(struct platform_device *pdev,
960 struct mvneta_port *pp)
961{
962 struct device_node *dn = pdev->dev.of_node;
963 u32 long_pool_id, short_pool_id, wsize;
964 u8 target, attr;
965 int err;
966
967 /* Get BM window information */
968 err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
969 &target, &attr);
970 if (err < 0)
971 return err;
972
973 pp->bm_win_id = -1;
974
975 /* Open NETA -> BM window */
976 err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
977 target, attr);
978 if (err < 0) {
979 netdev_info(pp->dev, "fail to configure mbus window to BM\n");
980 return err;
981 }
982
983 if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
984 netdev_info(pp->dev, "missing long pool id\n");
985 return -EINVAL;
986 }
987
988 /* Create port's long pool depending on mtu */
989 pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
990 MVNETA_BM_LONG, pp->id,
991 MVNETA_RX_PKT_SIZE(pp->dev->mtu));
992 if (!pp->pool_long) {
993 netdev_info(pp->dev, "fail to obtain long pool for port\n");
994 return -ENOMEM;
995 }
996
997 pp->pool_long->port_map |= 1 << pp->id;
998
999 mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1000 pp->pool_long->id);
1001
1002 /* If short pool id is not defined, assume using single pool */
1003 if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1004 short_pool_id = long_pool_id;
1005
1006 /* Create port's short pool */
1007 pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1008 MVNETA_BM_SHORT, pp->id,
1009 MVNETA_BM_SHORT_PKT_SIZE);
1010 if (!pp->pool_short) {
1011 netdev_info(pp->dev, "fail to obtain short pool for port\n");
1012 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1013 return -ENOMEM;
1014 }
1015
1016 if (short_pool_id != long_pool_id) {
1017 pp->pool_short->port_map |= 1 << pp->id;
1018 mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1019 pp->pool_short->id);
1020 }
1021
1022 return 0;
1023}
1024
1025/* Update settings of a pool for bigger packets */
1026static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1027{
1028 struct mvneta_bm_pool *bm_pool = pp->pool_long;
1029 int num;
1030
1031 /* Release all buffers from long pool */
1032 mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1033 if (bm_pool->buf_num) {
1034 WARN(1, "cannot free all buffers in pool %d\n",
1035 bm_pool->id);
1036 goto bm_mtu_err;
1037 }
1038
1039 bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1040 bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1041 bm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1042 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1043
1044 /* Fill entire long pool */
1045 num = mvneta_bm_bufs_add(pp->bm_priv, bm_pool, bm_pool->size);
1046 if (num != bm_pool->size) {
1047 WARN(1, "pool %d: %d of %d allocated\n",
1048 bm_pool->id, num, bm_pool->size);
1049 goto bm_mtu_err;
1050 }
1051 mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1052
1053 return;
1054
1055bm_mtu_err:
1056 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1057 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1058
1059 pp->bm_priv = NULL;
1060 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1061 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1062}
1063
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001064/* Start the Ethernet port RX and TX activity */
1065static void mvneta_port_up(struct mvneta_port *pp)
1066{
1067 int queue;
1068 u32 q_map;
1069
1070 /* Enable all initialized TXs. */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001071 q_map = 0;
1072 for (queue = 0; queue < txq_number; queue++) {
1073 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1074 if (txq->descs != NULL)
1075 q_map |= (1 << queue);
1076 }
1077 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1078
1079 /* Enable all initialized RXQs. */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001080 for (queue = 0; queue < rxq_number; queue++) {
1081 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1082
1083 if (rxq->descs != NULL)
1084 q_map |= (1 << queue);
1085 }
1086 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001087}
1088
1089/* Stop the Ethernet port activity */
1090static void mvneta_port_down(struct mvneta_port *pp)
1091{
1092 u32 val;
1093 int count;
1094
1095 /* Stop Rx port activity. Check port Rx activity. */
1096 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1097
1098 /* Issue stop command for active channels only */
1099 if (val != 0)
1100 mvreg_write(pp, MVNETA_RXQ_CMD,
1101 val << MVNETA_RXQ_DISABLE_SHIFT);
1102
1103 /* Wait for all Rx activity to terminate. */
1104 count = 0;
1105 do {
1106 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1107 netdev_warn(pp->dev,
1108 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
1109 val);
1110 break;
1111 }
1112 mdelay(1);
1113
1114 val = mvreg_read(pp, MVNETA_RXQ_CMD);
1115 } while (val & 0xff);
1116
1117 /* Stop Tx port activity. Check port Tx activity. Issue stop
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001118 * command for active channels only
1119 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001120 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1121
1122 if (val != 0)
1123 mvreg_write(pp, MVNETA_TXQ_CMD,
1124 (val << MVNETA_TXQ_DISABLE_SHIFT));
1125
1126 /* Wait for all Tx activity to terminate. */
1127 count = 0;
1128 do {
1129 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1130 netdev_warn(pp->dev,
1131 "TIMEOUT for TX stopped status=0x%08x\n",
1132 val);
1133 break;
1134 }
1135 mdelay(1);
1136
1137 /* Check TX Command reg that all Txqs are stopped */
1138 val = mvreg_read(pp, MVNETA_TXQ_CMD);
1139
1140 } while (val & 0xff);
1141
1142 /* Double check to verify that TX FIFO is empty */
1143 count = 0;
1144 do {
1145 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1146 netdev_warn(pp->dev,
1147 "TX FIFO empty timeout status=0x08%x\n",
1148 val);
1149 break;
1150 }
1151 mdelay(1);
1152
1153 val = mvreg_read(pp, MVNETA_PORT_STATUS);
1154 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1155 (val & MVNETA_TX_IN_PRGRS));
1156
1157 udelay(200);
1158}
1159
1160/* Enable the port by setting the port enable bit of the MAC control register */
1161static void mvneta_port_enable(struct mvneta_port *pp)
1162{
1163 u32 val;
1164
1165 /* Enable port */
1166 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1167 val |= MVNETA_GMAC0_PORT_ENABLE;
1168 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1169}
1170
1171/* Disable the port and wait for about 200 usec before retuning */
1172static void mvneta_port_disable(struct mvneta_port *pp)
1173{
1174 u32 val;
1175
1176 /* Reset the Enable bit in the Serial Control Register */
1177 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1178 val &= ~MVNETA_GMAC0_PORT_ENABLE;
1179 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1180
1181 udelay(200);
1182}
1183
1184/* Multicast tables methods */
1185
1186/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1187static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1188{
1189 int offset;
1190 u32 val;
1191
1192 if (queue == -1) {
1193 val = 0;
1194 } else {
1195 val = 0x1 | (queue << 1);
1196 val |= (val << 24) | (val << 16) | (val << 8);
1197 }
1198
1199 for (offset = 0; offset <= 0xc; offset += 4)
1200 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1201}
1202
1203/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1204static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1205{
1206 int offset;
1207 u32 val;
1208
1209 if (queue == -1) {
1210 val = 0;
1211 } else {
1212 val = 0x1 | (queue << 1);
1213 val |= (val << 24) | (val << 16) | (val << 8);
1214 }
1215
1216 for (offset = 0; offset <= 0xfc; offset += 4)
1217 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1218
1219}
1220
1221/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1222static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1223{
1224 int offset;
1225 u32 val;
1226
1227 if (queue == -1) {
1228 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1229 val = 0;
1230 } else {
1231 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1232 val = 0x1 | (queue << 1);
1233 val |= (val << 24) | (val << 16) | (val << 8);
1234 }
1235
1236 for (offset = 0; offset <= 0xfc; offset += 4)
1237 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1238}
1239
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001240static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
1241{
1242 u32 val;
1243
1244 if (enable) {
1245 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1246 val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1247 MVNETA_GMAC_FORCE_LINK_DOWN |
1248 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1249 val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1250 MVNETA_GMAC_AN_SPEED_EN |
1251 MVNETA_GMAC_AN_DUPLEX_EN;
1252 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1253
1254 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1255 val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1256 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1257
1258 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1259 val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
1260 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1261 } else {
1262 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1263 val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1264 MVNETA_GMAC_AN_SPEED_EN |
1265 MVNETA_GMAC_AN_DUPLEX_EN);
1266 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1267
1268 val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1269 val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
1270 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1271
1272 val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1273 val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
1274 mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1275 }
1276}
1277
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001278static void mvneta_percpu_unmask_interrupt(void *arg)
1279{
1280 struct mvneta_port *pp = arg;
1281
1282 /* All the queue are unmasked, but actually only the ones
1283 * mapped to this CPU will be unmasked
1284 */
1285 mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1286 MVNETA_RX_INTR_MASK_ALL |
1287 MVNETA_TX_INTR_MASK_ALL |
1288 MVNETA_MISCINTR_INTR_MASK);
1289}
1290
1291static void mvneta_percpu_mask_interrupt(void *arg)
1292{
1293 struct mvneta_port *pp = arg;
1294
1295 /* All the queue are masked, but actually only the ones
1296 * mapped to this CPU will be masked
1297 */
1298 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1299 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1300 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1301}
1302
1303static void mvneta_percpu_clear_intr_cause(void *arg)
1304{
1305 struct mvneta_port *pp = arg;
1306
1307 /* All the queue are cleared, but actually only the ones
1308 * mapped to this CPU will be cleared
1309 */
1310 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1311 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1312 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1313}
1314
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001315/* This method sets defaults to the NETA port:
1316 * Clears interrupt Cause and Mask registers.
1317 * Clears all MAC tables.
1318 * Sets defaults to all registers.
1319 * Resets RX and TX descriptor rings.
1320 * Resets PHY.
1321 * This method can be called after mvneta_port_down() to return the port
1322 * settings to defaults.
1323 */
1324static void mvneta_defaults_set(struct mvneta_port *pp)
1325{
1326 int cpu;
1327 int queue;
1328 u32 val;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001329 int max_cpu = num_present_cpus();
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001330
1331 /* Clear all Cause registers */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001332 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001333
1334 /* Mask all interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01001335 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001336 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1337
1338 /* Enable MBUS Retry bit16 */
1339 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1340
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001341 /* Set CPU queue access map. CPUs are assigned to the RX and
1342 * TX queues modulo their number. If there is only one TX
1343 * queue then it is assigned to the CPU associated to the
1344 * default RX queue.
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001345 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001346 for_each_present_cpu(cpu) {
1347 int rxq_map = 0, txq_map = 0;
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001348 int rxq, txq;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001349
1350 for (rxq = 0; rxq < rxq_number; rxq++)
1351 if ((rxq % max_cpu) == cpu)
1352 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1353
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01001354 for (txq = 0; txq < txq_number; txq++)
1355 if ((txq % max_cpu) == cpu)
1356 txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1357
1358 /* With only one TX queue we configure a special case
1359 * which will allow to get all the irq on a single
1360 * CPU
1361 */
1362 if (txq_number == 1)
1363 txq_map = (cpu == pp->rxq_def) ?
1364 MVNETA_CPU_TXQ_ACCESS(1) : 0;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01001365
1366 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1367 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001368
1369 /* Reset RX and TX DMAs */
1370 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1371 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1372
1373 /* Disable Legacy WRR, Disable EJP, Release from reset */
1374 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1375 for (queue = 0; queue < txq_number; queue++) {
1376 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1377 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1378 }
1379
1380 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1381 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1382
1383 /* Set Port Acceleration Mode */
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001384 if (pp->bm_priv)
1385 /* HW buffer management + legacy parser */
1386 val = MVNETA_ACC_MODE_EXT2;
1387 else
1388 /* SW buffer management + legacy parser */
1389 val = MVNETA_ACC_MODE_EXT1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001390 mvreg_write(pp, MVNETA_ACC_MODE, val);
1391
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001392 if (pp->bm_priv)
1393 mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1394
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001395 /* Update val of portCfg register accordingly with all RxQueue types */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01001396 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001397 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1398
1399 val = 0;
1400 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1401 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1402
1403 /* Build PORT_SDMA_CONFIG_REG */
1404 val = 0;
1405
1406 /* Default burst size */
1407 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1408 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001409 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001410
Thomas Petazzoni9ad8fef2013-07-29 15:21:28 +02001411#if defined(__BIG_ENDIAN)
1412 val |= MVNETA_DESC_SWAP;
1413#endif
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001414
1415 /* Assign port SDMA configuration */
1416 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1417
Thomas Petazzoni71408602013-09-04 16:21:18 +02001418 /* Disable PHY polling in hardware, since we're using the
1419 * kernel phylib to do this.
1420 */
1421 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1422 val &= ~MVNETA_PHY_POLLING_ENABLE;
1423 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1424
Stas Sergeev0c0744f2015-12-02 20:35:11 +03001425 mvneta_set_autoneg(pp, pp->use_inband_status);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001426 mvneta_set_ucast_table(pp, -1);
1427 mvneta_set_special_mcast_table(pp, -1);
1428 mvneta_set_other_mcast_table(pp, -1);
1429
1430 /* Set port interrupt enable register - default enable all */
1431 mvreg_write(pp, MVNETA_INTR_ENABLE,
1432 (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1433 | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
Andrew Lunne4839112015-10-22 18:37:36 +01001434
1435 mvneta_mib_counters_clear(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001436}
1437
1438/* Set max sizes for tx queues */
1439static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1440
1441{
1442 u32 val, size, mtu;
1443 int queue;
1444
1445 mtu = max_tx_size * 8;
1446 if (mtu > MVNETA_TX_MTU_MAX)
1447 mtu = MVNETA_TX_MTU_MAX;
1448
1449 /* Set MTU */
1450 val = mvreg_read(pp, MVNETA_TX_MTU);
1451 val &= ~MVNETA_TX_MTU_MAX;
1452 val |= mtu;
1453 mvreg_write(pp, MVNETA_TX_MTU, val);
1454
1455 /* TX token size and all TXQs token size must be larger that MTU */
1456 val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1457
1458 size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1459 if (size < mtu) {
1460 size = mtu;
1461 val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1462 val |= size;
1463 mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1464 }
1465 for (queue = 0; queue < txq_number; queue++) {
1466 val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1467
1468 size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1469 if (size < mtu) {
1470 size = mtu;
1471 val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1472 val |= size;
1473 mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1474 }
1475 }
1476}
1477
1478/* Set unicast address */
1479static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1480 int queue)
1481{
1482 unsigned int unicast_reg;
1483 unsigned int tbl_offset;
1484 unsigned int reg_offset;
1485
1486 /* Locate the Unicast table entry */
1487 last_nibble = (0xf & last_nibble);
1488
1489 /* offset from unicast tbl base */
1490 tbl_offset = (last_nibble / 4) * 4;
1491
1492 /* offset within the above reg */
1493 reg_offset = last_nibble % 4;
1494
1495 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1496
1497 if (queue == -1) {
1498 /* Clear accepts frame bit at specified unicast DA tbl entry */
1499 unicast_reg &= ~(0xff << (8 * reg_offset));
1500 } else {
1501 unicast_reg &= ~(0xff << (8 * reg_offset));
1502 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1503 }
1504
1505 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1506}
1507
1508/* Set mac address */
1509static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1510 int queue)
1511{
1512 unsigned int mac_h;
1513 unsigned int mac_l;
1514
1515 if (queue != -1) {
1516 mac_l = (addr[4] << 8) | (addr[5]);
1517 mac_h = (addr[0] << 24) | (addr[1] << 16) |
1518 (addr[2] << 8) | (addr[3] << 0);
1519
1520 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1521 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1522 }
1523
1524 /* Accept frames of this address */
1525 mvneta_set_ucast_addr(pp, addr[5], queue);
1526}
1527
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001528/* Set the number of packets that will be received before RX interrupt
1529 * will be generated by HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001530 */
1531static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1532 struct mvneta_rx_queue *rxq, u32 value)
1533{
1534 mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1535 value | MVNETA_RXQ_NON_OCCUPIED(0));
1536 rxq->pkts_coal = value;
1537}
1538
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001539/* Set the time delay in usec before RX interrupt will be generated by
1540 * HW.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001541 */
1542static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1543 struct mvneta_rx_queue *rxq, u32 value)
1544{
Thomas Petazzoni189dd622012-11-19 14:15:25 +01001545 u32 val;
1546 unsigned long clk_rate;
1547
1548 clk_rate = clk_get_rate(pp->clk);
1549 val = (clk_rate / 1000000) * value;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001550
1551 mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1552 rxq->time_coal = value;
1553}
1554
1555/* Set threshold for TX_DONE pkts coalescing */
1556static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1557 struct mvneta_tx_queue *txq, u32 value)
1558{
1559 u32 val;
1560
1561 val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1562
1563 val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1564 val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1565
1566 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1567
1568 txq->done_pkts_coal = value;
1569}
1570
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001571/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1572static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1573 u32 phys_addr, u32 cookie)
1574{
1575 rx_desc->buf_cookie = cookie;
1576 rx_desc->buf_phys_addr = phys_addr;
1577}
1578
1579/* Decrement sent descriptors counter */
1580static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1581 struct mvneta_tx_queue *txq,
1582 int sent_desc)
1583{
1584 u32 val;
1585
1586 /* Only 255 TX descriptors can be updated at once */
1587 while (sent_desc > 0xff) {
1588 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1589 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1590 sent_desc = sent_desc - 0xff;
1591 }
1592
1593 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1594 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1595}
1596
1597/* Get number of TX descriptors already sent by HW */
1598static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1599 struct mvneta_tx_queue *txq)
1600{
1601 u32 val;
1602 int sent_desc;
1603
1604 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1605 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1606 MVNETA_TXQ_SENT_DESC_SHIFT;
1607
1608 return sent_desc;
1609}
1610
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001611/* Get number of sent descriptors and decrement counter.
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001612 * The number of sent descriptors is returned.
1613 */
1614static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1615 struct mvneta_tx_queue *txq)
1616{
1617 int sent_desc;
1618
1619 /* Get number of sent descriptors */
1620 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1621
1622 /* Decrement sent descriptors counter */
1623 if (sent_desc)
1624 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1625
1626 return sent_desc;
1627}
1628
1629/* Set TXQ descriptors fields relevant for CSUM calculation */
1630static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1631 int ip_hdr_len, int l4_proto)
1632{
1633 u32 command;
1634
1635 /* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01001636 * G_L4_chk, L4_type; required only for checksum
1637 * calculation
1638 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001639 command = l3_offs << MVNETA_TX_L3_OFF_SHIFT;
1640 command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1641
Thomas Fitzsimmons0a198582014-07-08 19:44:07 -04001642 if (l3_proto == htons(ETH_P_IP))
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001643 command |= MVNETA_TXD_IP_CSUM;
1644 else
1645 command |= MVNETA_TX_L3_IP6;
1646
1647 if (l4_proto == IPPROTO_TCP)
1648 command |= MVNETA_TX_L4_CSUM_FULL;
1649 else if (l4_proto == IPPROTO_UDP)
1650 command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1651 else
1652 command |= MVNETA_TX_L4_CSUM_NOT;
1653
1654 return command;
1655}
1656
1657
1658/* Display more error info */
1659static void mvneta_rx_error(struct mvneta_port *pp,
1660 struct mvneta_rx_desc *rx_desc)
1661{
1662 u32 status = rx_desc->status;
1663
willy tarreau54282132014-01-16 08:20:14 +01001664 if (!mvneta_rxq_desc_is_first_last(status)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001665 netdev_err(pp->dev,
1666 "bad rx status %08x (buffer oversize), size=%d\n",
willy tarreau54282132014-01-16 08:20:14 +01001667 status, rx_desc->data_size);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001668 return;
1669 }
1670
1671 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1672 case MVNETA_RXD_ERR_CRC:
1673 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1674 status, rx_desc->data_size);
1675 break;
1676 case MVNETA_RXD_ERR_OVERRUN:
1677 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1678 status, rx_desc->data_size);
1679 break;
1680 case MVNETA_RXD_ERR_LEN:
1681 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1682 status, rx_desc->data_size);
1683 break;
1684 case MVNETA_RXD_ERR_RESOURCE:
1685 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1686 status, rx_desc->data_size);
1687 break;
1688 }
1689}
1690
willy tarreau54282132014-01-16 08:20:14 +01001691/* Handle RX checksum offload based on the descriptor's status */
1692static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001693 struct sk_buff *skb)
1694{
willy tarreau54282132014-01-16 08:20:14 +01001695 if ((status & MVNETA_RXD_L3_IP4) &&
1696 (status & MVNETA_RXD_L4_CSUM_OK)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001697 skb->csum = 0;
1698 skb->ip_summed = CHECKSUM_UNNECESSARY;
1699 return;
1700 }
1701
1702 skb->ip_summed = CHECKSUM_NONE;
1703}
1704
willy tarreau6c498972014-01-16 08:20:12 +01001705/* Return tx queue pointer (find last set bit) according to <cause> returned
1706 * form tx_done reg. <cause> must not be null. The return value is always a
1707 * valid queue for matching the first one found in <cause>.
1708 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001709static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1710 u32 cause)
1711{
1712 int queue = fls(cause) - 1;
1713
willy tarreau6c498972014-01-16 08:20:12 +01001714 return &pp->txqs[queue];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001715}
1716
1717/* Free tx queue skbuffs */
1718static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1719 struct mvneta_tx_queue *txq, int num)
1720{
1721 int i;
1722
1723 for (i = 0; i < num; i++) {
1724 struct mvneta_tx_desc *tx_desc = txq->descs +
1725 txq->txq_get_index;
1726 struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1727
1728 mvneta_txq_inc_get(txq);
1729
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03001730 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1731 dma_unmap_single(pp->dev->dev.parent,
1732 tx_desc->buf_phys_addr,
1733 tx_desc->data_size, DMA_TO_DEVICE);
Ezequiel Garciaba7e46e2014-05-30 13:40:06 -03001734 if (!skb)
1735 continue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001736 dev_kfree_skb_any(skb);
1737 }
1738}
1739
1740/* Handle end of transmission */
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001741static void mvneta_txq_done(struct mvneta_port *pp,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001742 struct mvneta_tx_queue *txq)
1743{
1744 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1745 int tx_done;
1746
1747 tx_done = mvneta_txq_sent_desc_proc(pp, txq);
Arnaud Ebalardcd713192014-01-16 08:20:19 +01001748 if (!tx_done)
1749 return;
1750
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001751 mvneta_txq_bufs_free(pp, txq, tx_done);
1752
1753 txq->count -= tx_done;
1754
1755 if (netif_tx_queue_stopped(nq)) {
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03001756 if (txq->count <= txq->tx_wake_threshold)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001757 netif_tx_wake_queue(nq);
1758 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001759}
1760
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001761void *mvneta_frag_alloc(unsigned int frag_size)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001762{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001763 if (likely(frag_size <= PAGE_SIZE))
1764 return netdev_alloc_frag(frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001765 else
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001766 return kmalloc(frag_size, GFP_ATOMIC);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001767}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001768EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001769
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001770void mvneta_frag_free(unsigned int frag_size, void *data)
willy tarreau8ec2cd42014-01-16 08:20:16 +01001771{
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001772 if (likely(frag_size <= PAGE_SIZE))
Alexander Duyck13dc0d22015-05-06 21:12:14 -07001773 skb_free_frag(data);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001774 else
1775 kfree(data);
1776}
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001777EXPORT_SYMBOL_GPL(mvneta_frag_free);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001778
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001779/* Refill processing for SW buffer management */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001780static int mvneta_rx_refill(struct mvneta_port *pp,
1781 struct mvneta_rx_desc *rx_desc)
1782
1783{
1784 dma_addr_t phys_addr;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001785 void *data;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001786
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001787 data = mvneta_frag_alloc(pp->frag_size);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001788 if (!data)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001789 return -ENOMEM;
1790
willy tarreau8ec2cd42014-01-16 08:20:16 +01001791 phys_addr = dma_map_single(pp->dev->dev.parent, data,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001792 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1793 DMA_FROM_DEVICE);
1794 if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001795 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001796 return -ENOMEM;
1797 }
1798
willy tarreau8ec2cd42014-01-16 08:20:16 +01001799 mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001800 return 0;
1801}
1802
1803/* Handle tx checksum */
1804static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1805{
1806 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1807 int ip_hdr_len = 0;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001808 __be16 l3_proto = vlan_get_protocol(skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001809 u8 l4_proto;
1810
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001811 if (l3_proto == htons(ETH_P_IP)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001812 struct iphdr *ip4h = ip_hdr(skb);
1813
1814 /* Calculate IPv4 checksum and L4 checksum */
1815 ip_hdr_len = ip4h->ihl;
1816 l4_proto = ip4h->protocol;
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001817 } else if (l3_proto == htons(ETH_P_IPV6)) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001818 struct ipv6hdr *ip6h = ipv6_hdr(skb);
1819
1820 /* Read l4_protocol from one of IPv6 extra headers */
1821 if (skb_network_header_len(skb) > 0)
1822 ip_hdr_len = (skb_network_header_len(skb) >> 2);
1823 l4_proto = ip6h->nexthdr;
1824 } else
1825 return MVNETA_TX_L4_CSUM_NOT;
1826
1827 return mvneta_txq_desc_csum(skb_network_offset(skb),
Vlad Yasevich817dbfa2014-08-25 10:34:54 -04001828 l3_proto, ip_hdr_len, l4_proto);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001829 }
1830
1831 return MVNETA_TX_L4_CSUM_NOT;
1832}
1833
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001834/* Drop packets received by the RXQ and free buffers */
1835static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1836 struct mvneta_rx_queue *rxq)
1837{
1838 int rx_done, i;
1839
1840 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001841 if (rx_done)
1842 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1843
1844 if (pp->bm_priv) {
1845 for (i = 0; i < rx_done; i++) {
1846 struct mvneta_rx_desc *rx_desc =
1847 mvneta_rxq_next_desc_get(rxq);
1848 u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1849 struct mvneta_bm_pool *bm_pool;
1850
1851 bm_pool = &pp->bm_priv->bm_pools[pool_id];
1852 /* Return dropped buffer to the pool */
1853 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1854 rx_desc->buf_phys_addr);
1855 }
1856 return;
1857 }
1858
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001859 for (i = 0; i < rxq->size; i++) {
1860 struct mvneta_rx_desc *rx_desc = rxq->descs + i;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001861 void *data = (void *)rx_desc->buf_cookie;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001862
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001863 dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
Ezequiel Garciaa328f3a2013-12-05 13:35:37 -03001864 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001865 mvneta_frag_free(pp->frag_size, data);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001866 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001867}
1868
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001869/* Main rx processing when using software buffer management */
1870static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1871 struct mvneta_rx_queue *rxq)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001872{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001873 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001874 struct net_device *dev = pp->dev;
Simon Guinota84e3282015-07-19 13:00:53 +02001875 int rx_done;
willy tarreaudc4277d2014-01-16 08:20:07 +01001876 u32 rcvd_pkts = 0;
1877 u32 rcvd_bytes = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001878
1879 /* Get number of received packets */
1880 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1881
1882 if (rx_todo > rx_done)
1883 rx_todo = rx_done;
1884
1885 rx_done = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001886
1887 /* Fairness NAPI loop */
1888 while (rx_done < rx_todo) {
1889 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1890 struct sk_buff *skb;
willy tarreau8ec2cd42014-01-16 08:20:16 +01001891 unsigned char *data;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001892 dma_addr_t phys_addr;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001893 u32 rx_status, frag_size;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001894 int rx_bytes, err;
1895
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001896 rx_done++;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001897 rx_status = rx_desc->status;
willy tarreauf19fadf2014-01-16 08:20:17 +01001898 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001899 data = (unsigned char *)rx_desc->buf_cookie;
Simon Guinotdaf158d2015-09-15 22:41:21 +02001900 phys_addr = rx_desc->buf_phys_addr;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001901
willy tarreau54282132014-01-16 08:20:14 +01001902 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
willy tarreauf19fadf2014-01-16 08:20:17 +01001903 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001904err_drop_frame:
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001905 dev->stats.rx_errors++;
1906 mvneta_rx_error(pp, rx_desc);
willy tarreau8ec2cd42014-01-16 08:20:16 +01001907 /* leave the descriptor untouched */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001908 continue;
1909 }
1910
willy tarreauf19fadf2014-01-16 08:20:17 +01001911 if (rx_bytes <= rx_copybreak) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001912 /* better copy a small frame and not unmap the DMA region */
willy tarreauf19fadf2014-01-16 08:20:17 +01001913 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1914 if (unlikely(!skb))
1915 goto err_drop_frame;
1916
1917 dma_sync_single_range_for_cpu(dev->dev.parent,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001918 rx_desc->buf_phys_addr,
1919 MVNETA_MH_SIZE + NET_SKB_PAD,
1920 rx_bytes,
1921 DMA_FROM_DEVICE);
willy tarreauf19fadf2014-01-16 08:20:17 +01001922 memcpy(skb_put(skb, rx_bytes),
1923 data + MVNETA_MH_SIZE + NET_SKB_PAD,
1924 rx_bytes);
1925
1926 skb->protocol = eth_type_trans(skb, dev);
1927 mvneta_rx_csum(pp, rx_status, skb);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001928 napi_gro_receive(&port->napi, skb);
willy tarreauf19fadf2014-01-16 08:20:17 +01001929
1930 rcvd_pkts++;
1931 rcvd_bytes += rx_bytes;
1932
1933 /* leave the descriptor and buffer untouched */
1934 continue;
1935 }
1936
Simon Guinota84e3282015-07-19 13:00:53 +02001937 /* Refill processing */
1938 err = mvneta_rx_refill(pp, rx_desc);
1939 if (err) {
1940 netdev_err(dev, "Linux processing - Can't refill\n");
1941 rxq->missed++;
1942 goto err_drop_frame;
1943 }
1944
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001945 frag_size = pp->frag_size;
1946
1947 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
willy tarreauf19fadf2014-01-16 08:20:17 +01001948
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001949 /* After refill old buffer has to be unmapped regardless
1950 * the skb is successfully built or not.
1951 */
Simon Guinotdaf158d2015-09-15 22:41:21 +02001952 dma_unmap_single(dev->dev.parent, phys_addr,
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001953 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1954 DMA_FROM_DEVICE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001955
Marcin Wojtas26c17a172015-11-30 13:27:44 +01001956 if (!skb)
1957 goto err_drop_frame;
1958
willy tarreaudc4277d2014-01-16 08:20:07 +01001959 rcvd_pkts++;
1960 rcvd_bytes += rx_bytes;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001961
1962 /* Linux processing */
willy tarreau8ec2cd42014-01-16 08:20:16 +01001963 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001964 skb_put(skb, rx_bytes);
1965
1966 skb->protocol = eth_type_trans(skb, dev);
1967
willy tarreau54282132014-01-16 08:20:14 +01001968 mvneta_rx_csum(pp, rx_status, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001969
Maxime Ripard12bb03b2015-09-25 18:09:36 +02001970 napi_gro_receive(&port->napi, skb);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001971 }
1972
willy tarreaudc4277d2014-01-16 08:20:07 +01001973 if (rcvd_pkts) {
willy tarreau74c41b02014-01-16 08:20:08 +01001974 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1975
1976 u64_stats_update_begin(&stats->syncp);
1977 stats->rx_packets += rcvd_pkts;
1978 stats->rx_bytes += rcvd_bytes;
1979 u64_stats_update_end(&stats->syncp);
willy tarreaudc4277d2014-01-16 08:20:07 +01001980 }
1981
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001982 /* Update rxq management counters */
Simon Guinota84e3282015-07-19 13:00:53 +02001983 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03001984
1985 return rx_done;
1986}
1987
Marcin Wojtasdc35a102016-03-14 09:39:03 +01001988/* Main rx processing when using hardware buffer management */
1989static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
1990 struct mvneta_rx_queue *rxq)
1991{
1992 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
1993 struct net_device *dev = pp->dev;
1994 int rx_done;
1995 u32 rcvd_pkts = 0;
1996 u32 rcvd_bytes = 0;
1997
1998 /* Get number of received packets */
1999 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2000
2001 if (rx_todo > rx_done)
2002 rx_todo = rx_done;
2003
2004 rx_done = 0;
2005
2006 /* Fairness NAPI loop */
2007 while (rx_done < rx_todo) {
2008 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2009 struct mvneta_bm_pool *bm_pool = NULL;
2010 struct sk_buff *skb;
2011 unsigned char *data;
2012 dma_addr_t phys_addr;
2013 u32 rx_status, frag_size;
2014 int rx_bytes, err;
2015 u8 pool_id;
2016
2017 rx_done++;
2018 rx_status = rx_desc->status;
2019 rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2020 data = (unsigned char *)rx_desc->buf_cookie;
2021 phys_addr = rx_desc->buf_phys_addr;
2022 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2023 bm_pool = &pp->bm_priv->bm_pools[pool_id];
2024
2025 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2026 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2027err_drop_frame_ret_pool:
2028 /* Return the buffer to the pool */
2029 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2030 rx_desc->buf_phys_addr);
2031err_drop_frame:
2032 dev->stats.rx_errors++;
2033 mvneta_rx_error(pp, rx_desc);
2034 /* leave the descriptor untouched */
2035 continue;
2036 }
2037
2038 if (rx_bytes <= rx_copybreak) {
2039 /* better copy a small frame and not unmap the DMA region */
2040 skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2041 if (unlikely(!skb))
2042 goto err_drop_frame_ret_pool;
2043
2044 dma_sync_single_range_for_cpu(dev->dev.parent,
2045 rx_desc->buf_phys_addr,
2046 MVNETA_MH_SIZE + NET_SKB_PAD,
2047 rx_bytes,
2048 DMA_FROM_DEVICE);
2049 memcpy(skb_put(skb, rx_bytes),
2050 data + MVNETA_MH_SIZE + NET_SKB_PAD,
2051 rx_bytes);
2052
2053 skb->protocol = eth_type_trans(skb, dev);
2054 mvneta_rx_csum(pp, rx_status, skb);
2055 napi_gro_receive(&port->napi, skb);
2056
2057 rcvd_pkts++;
2058 rcvd_bytes += rx_bytes;
2059
2060 /* Return the buffer to the pool */
2061 mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2062 rx_desc->buf_phys_addr);
2063
2064 /* leave the descriptor and buffer untouched */
2065 continue;
2066 }
2067
2068 /* Refill processing */
2069 err = mvneta_bm_pool_refill(pp->bm_priv, bm_pool);
2070 if (err) {
2071 netdev_err(dev, "Linux processing - Can't refill\n");
2072 rxq->missed++;
2073 goto err_drop_frame_ret_pool;
2074 }
2075
2076 frag_size = bm_pool->frag_size;
2077
2078 skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2079
2080 /* After refill old buffer has to be unmapped regardless
2081 * the skb is successfully built or not.
2082 */
2083 dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2084 bm_pool->buf_size, DMA_FROM_DEVICE);
2085 if (!skb)
2086 goto err_drop_frame;
2087
2088 rcvd_pkts++;
2089 rcvd_bytes += rx_bytes;
2090
2091 /* Linux processing */
2092 skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2093 skb_put(skb, rx_bytes);
2094
2095 skb->protocol = eth_type_trans(skb, dev);
2096
2097 mvneta_rx_csum(pp, rx_status, skb);
2098
2099 napi_gro_receive(&port->napi, skb);
2100 }
2101
2102 if (rcvd_pkts) {
2103 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2104
2105 u64_stats_update_begin(&stats->syncp);
2106 stats->rx_packets += rcvd_pkts;
2107 stats->rx_bytes += rcvd_bytes;
2108 u64_stats_update_end(&stats->syncp);
2109 }
2110
2111 /* Update rxq management counters */
2112 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2113
2114 return rx_done;
2115}
2116
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002117static inline void
2118mvneta_tso_put_hdr(struct sk_buff *skb,
2119 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2120{
2121 struct mvneta_tx_desc *tx_desc;
2122 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2123
2124 txq->tx_skb[txq->txq_put_index] = NULL;
2125 tx_desc = mvneta_txq_next_desc_get(txq);
2126 tx_desc->data_size = hdr_len;
2127 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2128 tx_desc->command |= MVNETA_TXD_F_DESC;
2129 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2130 txq->txq_put_index * TSO_HEADER_SIZE;
2131 mvneta_txq_inc_put(txq);
2132}
2133
2134static inline int
2135mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2136 struct sk_buff *skb, char *data, int size,
2137 bool last_tcp, bool is_last)
2138{
2139 struct mvneta_tx_desc *tx_desc;
2140
2141 tx_desc = mvneta_txq_next_desc_get(txq);
2142 tx_desc->data_size = size;
2143 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2144 size, DMA_TO_DEVICE);
2145 if (unlikely(dma_mapping_error(dev->dev.parent,
2146 tx_desc->buf_phys_addr))) {
2147 mvneta_txq_desc_put(txq);
2148 return -ENOMEM;
2149 }
2150
2151 tx_desc->command = 0;
2152 txq->tx_skb[txq->txq_put_index] = NULL;
2153
2154 if (last_tcp) {
2155 /* last descriptor in the TCP packet */
2156 tx_desc->command = MVNETA_TXD_L_DESC;
2157
2158 /* last descriptor in SKB */
2159 if (is_last)
2160 txq->tx_skb[txq->txq_put_index] = skb;
2161 }
2162 mvneta_txq_inc_put(txq);
2163 return 0;
2164}
2165
2166static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2167 struct mvneta_tx_queue *txq)
2168{
2169 int total_len, data_left;
2170 int desc_count = 0;
2171 struct mvneta_port *pp = netdev_priv(dev);
2172 struct tso_t tso;
2173 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2174 int i;
2175
2176 /* Count needed descriptors */
2177 if ((txq->count + tso_count_descs(skb)) >= txq->size)
2178 return 0;
2179
2180 if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2181 pr_info("*** Is this even possible???!?!?\n");
2182 return 0;
2183 }
2184
2185 /* Initialize the TSO handler, and prepare the first payload */
2186 tso_start(skb, &tso);
2187
2188 total_len = skb->len - hdr_len;
2189 while (total_len > 0) {
2190 char *hdr;
2191
2192 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2193 total_len -= data_left;
2194 desc_count++;
2195
2196 /* prepare packet headers: MAC + IP + TCP */
2197 hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2198 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2199
2200 mvneta_tso_put_hdr(skb, pp, txq);
2201
2202 while (data_left > 0) {
2203 int size;
2204 desc_count++;
2205
2206 size = min_t(int, tso.size, data_left);
2207
2208 if (mvneta_tso_put_data(dev, txq, skb,
2209 tso.data, size,
2210 size == data_left,
2211 total_len == 0))
2212 goto err_release;
2213 data_left -= size;
2214
2215 tso_build_data(skb, &tso, size);
2216 }
2217 }
2218
2219 return desc_count;
2220
2221err_release:
2222 /* Release all used data descriptors; header descriptors must not
2223 * be DMA-unmapped.
2224 */
2225 for (i = desc_count - 1; i >= 0; i--) {
2226 struct mvneta_tx_desc *tx_desc = txq->descs + i;
Ezequiel Garcia2e3173a2014-05-30 13:40:07 -03002227 if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002228 dma_unmap_single(pp->dev->dev.parent,
2229 tx_desc->buf_phys_addr,
2230 tx_desc->data_size,
2231 DMA_TO_DEVICE);
2232 mvneta_txq_desc_put(txq);
2233 }
2234 return 0;
2235}
2236
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002237/* Handle tx fragmentation processing */
2238static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2239 struct mvneta_tx_queue *txq)
2240{
2241 struct mvneta_tx_desc *tx_desc;
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002242 int i, nr_frags = skb_shinfo(skb)->nr_frags;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002243
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002244 for (i = 0; i < nr_frags; i++) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002245 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2246 void *addr = page_address(frag->page.p) + frag->page_offset;
2247
2248 tx_desc = mvneta_txq_next_desc_get(txq);
2249 tx_desc->data_size = frag->size;
2250
2251 tx_desc->buf_phys_addr =
2252 dma_map_single(pp->dev->dev.parent, addr,
2253 tx_desc->data_size, DMA_TO_DEVICE);
2254
2255 if (dma_mapping_error(pp->dev->dev.parent,
2256 tx_desc->buf_phys_addr)) {
2257 mvneta_txq_desc_put(txq);
2258 goto error;
2259 }
2260
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002261 if (i == nr_frags - 1) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002262 /* Last descriptor */
2263 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002264 txq->tx_skb[txq->txq_put_index] = skb;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002265 } else {
2266 /* Descriptor in the middle: Not First, Not Last */
2267 tx_desc->command = 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002268 txq->tx_skb[txq->txq_put_index] = NULL;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002269 }
Ezequiel Garcia3d4ea022014-05-22 20:06:57 -03002270 mvneta_txq_inc_put(txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002271 }
2272
2273 return 0;
2274
2275error:
2276 /* Release all descriptors that were used to map fragments of
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002277 * this packet, as well as the corresponding DMA mappings
2278 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002279 for (i = i - 1; i >= 0; i--) {
2280 tx_desc = txq->descs + i;
2281 dma_unmap_single(pp->dev->dev.parent,
2282 tx_desc->buf_phys_addr,
2283 tx_desc->data_size,
2284 DMA_TO_DEVICE);
2285 mvneta_txq_desc_put(txq);
2286 }
2287
2288 return -ENOMEM;
2289}
2290
2291/* Main tx processing */
2292static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2293{
2294 struct mvneta_port *pp = netdev_priv(dev);
Willy Tarreauee40a112013-04-11 23:00:37 +02002295 u16 txq_id = skb_get_queue_mapping(skb);
2296 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002297 struct mvneta_tx_desc *tx_desc;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002298 int len = skb->len;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002299 int frags = 0;
2300 u32 tx_cmd;
2301
2302 if (!netif_running(dev))
2303 goto out;
2304
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002305 if (skb_is_gso(skb)) {
2306 frags = mvneta_tx_tso(skb, dev, txq);
2307 goto out;
2308 }
2309
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002310 frags = skb_shinfo(skb)->nr_frags + 1;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002311
2312 /* Get a descriptor for the first part of the packet */
2313 tx_desc = mvneta_txq_next_desc_get(txq);
2314
2315 tx_cmd = mvneta_skb_tx_csum(pp, skb);
2316
2317 tx_desc->data_size = skb_headlen(skb);
2318
2319 tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2320 tx_desc->data_size,
2321 DMA_TO_DEVICE);
2322 if (unlikely(dma_mapping_error(dev->dev.parent,
2323 tx_desc->buf_phys_addr))) {
2324 mvneta_txq_desc_put(txq);
2325 frags = 0;
2326 goto out;
2327 }
2328
2329 if (frags == 1) {
2330 /* First and Last descriptor */
2331 tx_cmd |= MVNETA_TXD_FLZ_DESC;
2332 tx_desc->command = tx_cmd;
2333 txq->tx_skb[txq->txq_put_index] = skb;
2334 mvneta_txq_inc_put(txq);
2335 } else {
2336 /* First but not Last */
2337 tx_cmd |= MVNETA_TXD_F_DESC;
2338 txq->tx_skb[txq->txq_put_index] = NULL;
2339 mvneta_txq_inc_put(txq);
2340 tx_desc->command = tx_cmd;
2341 /* Continue with other skb fragments */
2342 if (mvneta_tx_frag_process(pp, skb, txq)) {
2343 dma_unmap_single(dev->dev.parent,
2344 tx_desc->buf_phys_addr,
2345 tx_desc->data_size,
2346 DMA_TO_DEVICE);
2347 mvneta_txq_desc_put(txq);
2348 frags = 0;
2349 goto out;
2350 }
2351 }
2352
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002353out:
2354 if (frags > 0) {
willy tarreau74c41b02014-01-16 08:20:08 +01002355 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002356 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2357
2358 txq->count += frags;
2359 mvneta_txq_pend_desc_add(pp, txq, frags);
2360
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002361 if (txq->count >= txq->tx_stop_threshold)
Ezequiel Garciae19d2dd2014-05-19 13:59:54 -03002362 netif_tx_stop_queue(nq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002363
willy tarreau74c41b02014-01-16 08:20:08 +01002364 u64_stats_update_begin(&stats->syncp);
2365 stats->tx_packets++;
Eric Dumazet5f478b42014-12-02 04:30:59 -08002366 stats->tx_bytes += len;
willy tarreau74c41b02014-01-16 08:20:08 +01002367 u64_stats_update_end(&stats->syncp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002368 } else {
2369 dev->stats.tx_dropped++;
2370 dev_kfree_skb_any(skb);
2371 }
2372
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002373 return NETDEV_TX_OK;
2374}
2375
2376
2377/* Free tx resources, when resetting a port */
2378static void mvneta_txq_done_force(struct mvneta_port *pp,
2379 struct mvneta_tx_queue *txq)
2380
2381{
2382 int tx_done = txq->count;
2383
2384 mvneta_txq_bufs_free(pp, txq, tx_done);
2385
2386 /* reset txq */
2387 txq->count = 0;
2388 txq->txq_put_index = 0;
2389 txq->txq_get_index = 0;
2390}
2391
willy tarreau6c498972014-01-16 08:20:12 +01002392/* Handle tx done - called in softirq context. The <cause_tx_done> argument
2393 * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2394 */
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002395static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002396{
2397 struct mvneta_tx_queue *txq;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002398 struct netdev_queue *nq;
2399
willy tarreau6c498972014-01-16 08:20:12 +01002400 while (cause_tx_done) {
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002401 txq = mvneta_tx_done_policy(pp, cause_tx_done);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002402
2403 nq = netdev_get_tx_queue(pp->dev, txq->id);
2404 __netif_tx_lock(nq, smp_processor_id());
2405
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002406 if (txq->count)
2407 mvneta_txq_done(pp, txq);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002408
2409 __netif_tx_unlock(nq);
2410 cause_tx_done &= ~((1 << txq->id));
2411 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002412}
2413
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002414/* Compute crc8 of the specified address, using a unique algorithm ,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002415 * according to hw spec, different than generic crc8 algorithm
2416 */
2417static int mvneta_addr_crc(unsigned char *addr)
2418{
2419 int crc = 0;
2420 int i;
2421
2422 for (i = 0; i < ETH_ALEN; i++) {
2423 int j;
2424
2425 crc = (crc ^ addr[i]) << 8;
2426 for (j = 7; j >= 0; j--) {
2427 if (crc & (0x100 << j))
2428 crc ^= 0x107 << j;
2429 }
2430 }
2431
2432 return crc;
2433}
2434
2435/* This method controls the net device special MAC multicast support.
2436 * The Special Multicast Table for MAC addresses supports MAC of the form
2437 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2438 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2439 * Table entries in the DA-Filter table. This method set the Special
2440 * Multicast Table appropriate entry.
2441 */
2442static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2443 unsigned char last_byte,
2444 int queue)
2445{
2446 unsigned int smc_table_reg;
2447 unsigned int tbl_offset;
2448 unsigned int reg_offset;
2449
2450 /* Register offset from SMC table base */
2451 tbl_offset = (last_byte / 4);
2452 /* Entry offset within the above reg */
2453 reg_offset = last_byte % 4;
2454
2455 smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2456 + tbl_offset * 4));
2457
2458 if (queue == -1)
2459 smc_table_reg &= ~(0xff << (8 * reg_offset));
2460 else {
2461 smc_table_reg &= ~(0xff << (8 * reg_offset));
2462 smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2463 }
2464
2465 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2466 smc_table_reg);
2467}
2468
2469/* This method controls the network device Other MAC multicast support.
2470 * The Other Multicast Table is used for multicast of another type.
2471 * A CRC-8 is used as an index to the Other Multicast Table entries
2472 * in the DA-Filter table.
2473 * The method gets the CRC-8 value from the calling routine and
2474 * sets the Other Multicast Table appropriate entry according to the
2475 * specified CRC-8 .
2476 */
2477static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2478 unsigned char crc8,
2479 int queue)
2480{
2481 unsigned int omc_table_reg;
2482 unsigned int tbl_offset;
2483 unsigned int reg_offset;
2484
2485 tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2486 reg_offset = crc8 % 4; /* Entry offset within the above reg */
2487
2488 omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2489
2490 if (queue == -1) {
2491 /* Clear accepts frame bit at specified Other DA table entry */
2492 omc_table_reg &= ~(0xff << (8 * reg_offset));
2493 } else {
2494 omc_table_reg &= ~(0xff << (8 * reg_offset));
2495 omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2496 }
2497
2498 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2499}
2500
2501/* The network device supports multicast using two tables:
2502 * 1) Special Multicast Table for MAC addresses of the form
2503 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2504 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2505 * Table entries in the DA-Filter table.
2506 * 2) Other Multicast Table for multicast of another type. A CRC-8 value
2507 * is used as an index to the Other Multicast Table entries in the
2508 * DA-Filter table.
2509 */
2510static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2511 int queue)
2512{
2513 unsigned char crc_result = 0;
2514
2515 if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2516 mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2517 return 0;
2518 }
2519
2520 crc_result = mvneta_addr_crc(p_addr);
2521 if (queue == -1) {
2522 if (pp->mcast_count[crc_result] == 0) {
2523 netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2524 crc_result);
2525 return -EINVAL;
2526 }
2527
2528 pp->mcast_count[crc_result]--;
2529 if (pp->mcast_count[crc_result] != 0) {
2530 netdev_info(pp->dev,
2531 "After delete there are %d valid Mcast for crc8=0x%02x\n",
2532 pp->mcast_count[crc_result], crc_result);
2533 return -EINVAL;
2534 }
2535 } else
2536 pp->mcast_count[crc_result]++;
2537
2538 mvneta_set_other_mcast_addr(pp, crc_result, queue);
2539
2540 return 0;
2541}
2542
2543/* Configure Fitering mode of Ethernet port */
2544static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2545 int is_promisc)
2546{
2547 u32 port_cfg_reg, val;
2548
2549 port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2550
2551 val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2552
2553 /* Set / Clear UPM bit in port configuration register */
2554 if (is_promisc) {
2555 /* Accept all Unicast addresses */
2556 port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2557 val |= MVNETA_FORCE_UNI;
2558 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2559 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2560 } else {
2561 /* Reject all Unicast addresses */
2562 port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2563 val &= ~MVNETA_FORCE_UNI;
2564 }
2565
2566 mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2567 mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2568}
2569
2570/* register unicast and multicast addresses */
2571static void mvneta_set_rx_mode(struct net_device *dev)
2572{
2573 struct mvneta_port *pp = netdev_priv(dev);
2574 struct netdev_hw_addr *ha;
2575
2576 if (dev->flags & IFF_PROMISC) {
2577 /* Accept all: Multicast + Unicast */
2578 mvneta_rx_unicast_promisc_set(pp, 1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002579 mvneta_set_ucast_table(pp, pp->rxq_def);
2580 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2581 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002582 } else {
2583 /* Accept single Unicast */
2584 mvneta_rx_unicast_promisc_set(pp, 0);
2585 mvneta_set_ucast_table(pp, -1);
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002586 mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002587
2588 if (dev->flags & IFF_ALLMULTI) {
2589 /* Accept all multicast */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002590 mvneta_set_special_mcast_table(pp, pp->rxq_def);
2591 mvneta_set_other_mcast_table(pp, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002592 } else {
2593 /* Accept only initialized multicast */
2594 mvneta_set_special_mcast_table(pp, -1);
2595 mvneta_set_other_mcast_table(pp, -1);
2596
2597 if (!netdev_mc_empty(dev)) {
2598 netdev_for_each_mc_addr(ha, dev) {
2599 mvneta_mcast_addr_set(pp, ha->addr,
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01002600 pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002601 }
2602 }
2603 }
2604 }
2605}
2606
2607/* Interrupt handling - the callback for request_irq() */
2608static irqreturn_t mvneta_isr(int irq, void *dev_id)
2609{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002610 struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002611
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002612 disable_percpu_irq(port->pp->dev->irq);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002613 napi_schedule(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002614
2615 return IRQ_HANDLED;
2616}
2617
Stas Sergeev898b2972015-04-01 20:32:49 +03002618static int mvneta_fixed_link_update(struct mvneta_port *pp,
2619 struct phy_device *phy)
2620{
2621 struct fixed_phy_status status;
2622 struct fixed_phy_status changed = {};
2623 u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2624
2625 status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2626 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2627 status.speed = SPEED_1000;
2628 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2629 status.speed = SPEED_100;
2630 else
2631 status.speed = SPEED_10;
2632 status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2633 changed.link = 1;
2634 changed.speed = 1;
2635 changed.duplex = 1;
2636 fixed_phy_update_state(phy, &status, &changed);
2637 return 0;
2638}
2639
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002640/* NAPI handler
2641 * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2642 * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2643 * Bits 8 -15 of the cause Rx Tx register indicate that are received
2644 * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2645 * Each CPU has its own causeRxTx register
2646 */
2647static int mvneta_poll(struct napi_struct *napi, int budget)
2648{
2649 int rx_done = 0;
2650 u32 cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002651 int rx_queue;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002652 struct mvneta_port *pp = netdev_priv(napi->dev);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002653 struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002654
2655 if (!netif_running(pp->dev)) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002656 napi_complete(&port->napi);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002657 return rx_done;
2658 }
2659
2660 /* Read cause register */
Stas Sergeev898b2972015-04-01 20:32:49 +03002661 cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2662 if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2663 u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2664
2665 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2666 if (pp->use_inband_status && (cause_misc &
2667 (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2668 MVNETA_CAUSE_LINK_CHANGE |
2669 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
2670 mvneta_fixed_link_update(pp, pp->phy_dev);
2671 }
2672 }
willy tarreau71f6d1b2014-01-16 08:20:11 +01002673
2674 /* Release Tx descriptors */
2675 if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
Arnaud Ebalard0713a862014-01-16 08:20:18 +01002676 mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
willy tarreau71f6d1b2014-01-16 08:20:11 +01002677 cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2678 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002679
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002680 /* For the case where the last mvneta_poll did not process all
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002681 * RX packets
2682 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002683 rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2684
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002685 cause_rx_tx |= port->cause_rx_tx;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002686
2687 if (rx_queue) {
2688 rx_queue = rx_queue - 1;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002689 if (pp->bm_priv)
2690 rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2691 else
2692 rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002693 }
2694
Maxime Ripardd8936652015-09-25 18:09:37 +02002695 budget -= rx_done;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002696
2697 if (budget > 0) {
2698 cause_rx_tx = 0;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002699 napi_complete(&port->napi);
2700 enable_percpu_irq(pp->dev->irq, 0);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002701 }
2702
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002703 port->cause_rx_tx = cause_rx_tx;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002704 return rx_done;
2705}
2706
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002707/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2708static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2709 int num)
2710{
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002711 int i;
2712
2713 for (i = 0; i < num; i++) {
willy tarreaua1a65ab2014-01-16 08:20:13 +01002714 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2715 if (mvneta_rx_refill(pp, rxq->descs + i) != 0) {
2716 netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs filled\n",
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002717 __func__, rxq->id, i, num);
2718 break;
2719 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002720 }
2721
2722 /* Add this number of RX descriptors as non occupied (ready to
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01002723 * get packets)
2724 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002725 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2726
2727 return i;
2728}
2729
2730/* Free all packets pending transmit from all TXQs and reset TX port */
2731static void mvneta_tx_reset(struct mvneta_port *pp)
2732{
2733 int queue;
2734
Ezequiel Garcia96728502014-05-22 20:06:59 -03002735 /* free the skb's in the tx ring */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002736 for (queue = 0; queue < txq_number; queue++)
2737 mvneta_txq_done_force(pp, &pp->txqs[queue]);
2738
2739 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2740 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2741}
2742
2743static void mvneta_rx_reset(struct mvneta_port *pp)
2744{
2745 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2746 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2747}
2748
2749/* Rx/Tx queue initialization/cleanup methods */
2750
2751/* Create a specified RX queue */
2752static int mvneta_rxq_init(struct mvneta_port *pp,
2753 struct mvneta_rx_queue *rxq)
2754
2755{
2756 rxq->size = pp->rx_ring_size;
2757
2758 /* Allocate memory for RX descriptors */
2759 rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2760 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2761 &rxq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002762 if (rxq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002763 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002764
2765 BUG_ON(rxq->descs !=
2766 PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2767
2768 rxq->last_desc = rxq->size - 1;
2769
2770 /* Set Rx descriptors queue starting address */
2771 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2772 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2773
2774 /* Set Offset */
2775 mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2776
2777 /* Set coalescing pkts and time */
2778 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2779 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2780
Marcin Wojtasdc35a102016-03-14 09:39:03 +01002781 if (!pp->bm_priv) {
2782 /* Fill RXQ with buffers from RX pool */
2783 mvneta_rxq_buf_size_set(pp, rxq,
2784 MVNETA_RX_BUF_SIZE(pp->pkt_size));
2785 mvneta_rxq_bm_disable(pp, rxq);
2786 } else {
2787 mvneta_rxq_bm_enable(pp, rxq);
2788 mvneta_rxq_long_pool_set(pp, rxq);
2789 mvneta_rxq_short_pool_set(pp, rxq);
2790 }
2791
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002792 mvneta_rxq_fill(pp, rxq, rxq->size);
2793
2794 return 0;
2795}
2796
2797/* Cleanup Rx queue */
2798static void mvneta_rxq_deinit(struct mvneta_port *pp,
2799 struct mvneta_rx_queue *rxq)
2800{
2801 mvneta_rxq_drop_pkts(pp, rxq);
2802
2803 if (rxq->descs)
2804 dma_free_coherent(pp->dev->dev.parent,
2805 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2806 rxq->descs,
2807 rxq->descs_phys);
2808
2809 rxq->descs = NULL;
2810 rxq->last_desc = 0;
2811 rxq->next_desc_to_proc = 0;
2812 rxq->descs_phys = 0;
2813}
2814
2815/* Create and initialize a tx queue */
2816static int mvneta_txq_init(struct mvneta_port *pp,
2817 struct mvneta_tx_queue *txq)
2818{
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002819 int cpu;
2820
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002821 txq->size = pp->tx_ring_size;
2822
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03002823 /* A queue must always have room for at least one skb.
2824 * Therefore, stop the queue when the free entries reaches
2825 * the maximum number of descriptors per skb.
2826 */
2827 txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2828 txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2829
2830
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002831 /* Allocate memory for TX descriptors */
2832 txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2833 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2834 &txq->descs_phys, GFP_KERNEL);
Joe Perchesd0320f72013-03-14 13:07:21 +00002835 if (txq->descs == NULL)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002836 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002837
2838 /* Make sure descriptor address is cache line size aligned */
2839 BUG_ON(txq->descs !=
2840 PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2841
2842 txq->last_desc = txq->size - 1;
2843
2844 /* Set maximum bandwidth for enabled TXQs */
2845 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2846 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2847
2848 /* Set Tx descriptors queue starting address */
2849 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2850 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2851
2852 txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2853 if (txq->tx_skb == NULL) {
2854 dma_free_coherent(pp->dev->dev.parent,
2855 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2856 txq->descs, txq->descs_phys);
2857 return -ENOMEM;
2858 }
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002859
2860 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2861 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2862 txq->size * TSO_HEADER_SIZE,
2863 &txq->tso_hdrs_phys, GFP_KERNEL);
2864 if (txq->tso_hdrs == NULL) {
2865 kfree(txq->tx_skb);
2866 dma_free_coherent(pp->dev->dev.parent,
2867 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2868 txq->descs, txq->descs_phys);
2869 return -ENOMEM;
2870 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002871 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2872
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01002873 /* Setup XPS mapping */
2874 if (txq_number > 1)
2875 cpu = txq->id % num_present_cpus();
2876 else
2877 cpu = pp->rxq_def % num_present_cpus();
2878 cpumask_set_cpu(cpu, &txq->affinity_mask);
2879 netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2880
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002881 return 0;
2882}
2883
2884/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2885static void mvneta_txq_deinit(struct mvneta_port *pp,
2886 struct mvneta_tx_queue *txq)
2887{
2888 kfree(txq->tx_skb);
2889
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03002890 if (txq->tso_hdrs)
2891 dma_free_coherent(pp->dev->dev.parent,
2892 txq->size * TSO_HEADER_SIZE,
2893 txq->tso_hdrs, txq->tso_hdrs_phys);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002894 if (txq->descs)
2895 dma_free_coherent(pp->dev->dev.parent,
2896 txq->size * MVNETA_DESC_ALIGNED_SIZE,
2897 txq->descs, txq->descs_phys);
2898
2899 txq->descs = NULL;
2900 txq->last_desc = 0;
2901 txq->next_desc_to_proc = 0;
2902 txq->descs_phys = 0;
2903
2904 /* Set minimum bandwidth for disabled TXQs */
2905 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2906 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2907
2908 /* Set Tx descriptors queue starting address and size */
2909 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2910 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2911}
2912
2913/* Cleanup all Tx queues */
2914static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2915{
2916 int queue;
2917
2918 for (queue = 0; queue < txq_number; queue++)
2919 mvneta_txq_deinit(pp, &pp->txqs[queue]);
2920}
2921
2922/* Cleanup all Rx queues */
2923static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2924{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002925 int queue;
2926
2927 for (queue = 0; queue < txq_number; queue++)
2928 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002929}
2930
2931
2932/* Init all Rx queues */
2933static int mvneta_setup_rxqs(struct mvneta_port *pp)
2934{
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002935 int queue;
2936
2937 for (queue = 0; queue < rxq_number; queue++) {
2938 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
2939
2940 if (err) {
2941 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2942 __func__, queue);
2943 mvneta_cleanup_rxqs(pp);
2944 return err;
2945 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002946 }
2947
2948 return 0;
2949}
2950
2951/* Init all tx queues */
2952static int mvneta_setup_txqs(struct mvneta_port *pp)
2953{
2954 int queue;
2955
2956 for (queue = 0; queue < txq_number; queue++) {
2957 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2958 if (err) {
2959 netdev_err(pp->dev, "%s: can't create txq=%d\n",
2960 __func__, queue);
2961 mvneta_cleanup_txqs(pp);
2962 return err;
2963 }
2964 }
2965
2966 return 0;
2967}
2968
2969static void mvneta_start_dev(struct mvneta_port *pp)
2970{
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01002971 int cpu;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002972
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002973 mvneta_max_rx_size_set(pp, pp->pkt_size);
2974 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2975
2976 /* start the Rx/Tx activity */
2977 mvneta_port_enable(pp);
2978
2979 /* Enable polling on the port */
Gregory CLEMENT129219e2016-02-04 22:09:23 +01002980 for_each_online_cpu(cpu) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02002981 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2982
2983 napi_enable(&port->napi);
2984 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002985
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01002986 /* Unmask interrupts. It has to be done from each CPU */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01002987 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
2988
Stas Sergeev898b2972015-04-01 20:32:49 +03002989 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2990 MVNETA_CAUSE_PHY_STATUS_CHANGE |
2991 MVNETA_CAUSE_LINK_CHANGE |
2992 MVNETA_CAUSE_PSC_SYNC_CHANGE);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03002993
2994 phy_start(pp->phy_dev);
2995 netif_tx_start_all_queues(pp->dev);
2996}
2997
2998static void mvneta_stop_dev(struct mvneta_port *pp)
2999{
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003000 unsigned int cpu;
3001
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003002 phy_stop(pp->phy_dev);
3003
Gregory CLEMENT129219e2016-02-04 22:09:23 +01003004 for_each_online_cpu(cpu) {
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003005 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3006
3007 napi_disable(&port->napi);
3008 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003009
3010 netif_carrier_off(pp->dev);
3011
3012 mvneta_port_down(pp);
3013 netif_tx_stop_all_queues(pp->dev);
3014
3015 /* Stop the port activity */
3016 mvneta_port_disable(pp);
3017
3018 /* Clear all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003019 on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003020
3021 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003022 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003023
3024 mvneta_tx_reset(pp);
3025 mvneta_rx_reset(pp);
3026}
3027
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003028/* Return positive if MTU is valid */
3029static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
3030{
3031 if (mtu < 68) {
3032 netdev_err(dev, "cannot change mtu to less than 68\n");
3033 return -EINVAL;
3034 }
3035
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003036 /* 9676 == 9700 - 20 and rounding to 8 */
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003037 if (mtu > 9676) {
3038 netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
3039 mtu = 9676;
3040 }
3041
3042 if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3043 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3044 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3045 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3046 }
3047
3048 return mtu;
3049}
3050
3051/* Change the device mtu */
3052static int mvneta_change_mtu(struct net_device *dev, int mtu)
3053{
3054 struct mvneta_port *pp = netdev_priv(dev);
3055 int ret;
3056
3057 mtu = mvneta_check_mtu_valid(dev, mtu);
3058 if (mtu < 0)
3059 return -EINVAL;
3060
3061 dev->mtu = mtu;
3062
Simon Guinotb65657f2015-06-30 16:20:22 +02003063 if (!netif_running(dev)) {
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003064 if (pp->bm_priv)
3065 mvneta_bm_update_mtu(pp, mtu);
3066
Simon Guinotb65657f2015-06-30 16:20:22 +02003067 netdev_update_features(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003068 return 0;
Simon Guinotb65657f2015-06-30 16:20:22 +02003069 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003070
Thomas Petazzoni6a20c172012-11-19 11:41:25 +01003071 /* The interface is running, so we have to force a
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003072 * reallocation of the queues
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003073 */
3074 mvneta_stop_dev(pp);
3075
3076 mvneta_cleanup_txqs(pp);
3077 mvneta_cleanup_rxqs(pp);
3078
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003079 if (pp->bm_priv)
3080 mvneta_bm_update_mtu(pp, mtu);
3081
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003082 pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003083 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3084 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003085
3086 ret = mvneta_setup_rxqs(pp);
3087 if (ret) {
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003088 netdev_err(dev, "unable to setup rxqs after MTU change\n");
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003089 return ret;
3090 }
3091
Ezequiel Garciaa92dbd92014-05-22 20:06:58 -03003092 ret = mvneta_setup_txqs(pp);
3093 if (ret) {
3094 netdev_err(dev, "unable to setup txqs after MTU change\n");
3095 return ret;
3096 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003097
3098 mvneta_start_dev(pp);
3099 mvneta_port_up(pp);
3100
Simon Guinotb65657f2015-06-30 16:20:22 +02003101 netdev_update_features(dev);
3102
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003103 return 0;
3104}
3105
Simon Guinotb65657f2015-06-30 16:20:22 +02003106static netdev_features_t mvneta_fix_features(struct net_device *dev,
3107 netdev_features_t features)
3108{
3109 struct mvneta_port *pp = netdev_priv(dev);
3110
3111 if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3112 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3113 netdev_info(dev,
3114 "Disable IP checksum for MTU greater than %dB\n",
3115 pp->tx_csum_limit);
3116 }
3117
3118 return features;
3119}
3120
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003121/* Get mac address */
3122static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3123{
3124 u32 mac_addr_l, mac_addr_h;
3125
3126 mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3127 mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3128 addr[0] = (mac_addr_h >> 24) & 0xFF;
3129 addr[1] = (mac_addr_h >> 16) & 0xFF;
3130 addr[2] = (mac_addr_h >> 8) & 0xFF;
3131 addr[3] = mac_addr_h & 0xFF;
3132 addr[4] = (mac_addr_l >> 8) & 0xFF;
3133 addr[5] = mac_addr_l & 0xFF;
3134}
3135
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003136/* Handle setting mac address */
3137static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3138{
3139 struct mvneta_port *pp = netdev_priv(dev);
Ezequiel Garciae68de362014-05-22 20:07:00 -03003140 struct sockaddr *sockaddr = addr;
3141 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003142
Ezequiel Garciae68de362014-05-22 20:07:00 -03003143 ret = eth_prepare_mac_addr_change(dev, addr);
3144 if (ret < 0)
3145 return ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003146 /* Remove previous address table entry */
3147 mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3148
3149 /* Set new addr in hw */
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01003150 mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003151
Ezequiel Garciae68de362014-05-22 20:07:00 -03003152 eth_commit_mac_addr_change(dev, addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003153 return 0;
3154}
3155
3156static void mvneta_adjust_link(struct net_device *ndev)
3157{
3158 struct mvneta_port *pp = netdev_priv(ndev);
3159 struct phy_device *phydev = pp->phy_dev;
3160 int status_change = 0;
3161
3162 if (phydev->link) {
3163 if ((pp->speed != phydev->speed) ||
3164 (pp->duplex != phydev->duplex)) {
3165 u32 val;
3166
3167 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3168 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3169 MVNETA_GMAC_CONFIG_GMII_SPEED |
Stas Sergeev898b2972015-04-01 20:32:49 +03003170 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003171
3172 if (phydev->duplex)
3173 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3174
3175 if (phydev->speed == SPEED_1000)
3176 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
Thomas Petazzoni4d12bc62014-07-08 10:49:43 +02003177 else if (phydev->speed == SPEED_100)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003178 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3179
3180 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3181
3182 pp->duplex = phydev->duplex;
3183 pp->speed = phydev->speed;
3184 }
3185 }
3186
3187 if (phydev->link != pp->link) {
3188 if (!phydev->link) {
3189 pp->duplex = -1;
3190 pp->speed = 0;
3191 }
3192
3193 pp->link = phydev->link;
3194 status_change = 1;
3195 }
3196
3197 if (status_change) {
3198 if (phydev->link) {
Stas Sergeev898b2972015-04-01 20:32:49 +03003199 if (!pp->use_inband_status) {
3200 u32 val = mvreg_read(pp,
3201 MVNETA_GMAC_AUTONEG_CONFIG);
3202 val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3203 val |= MVNETA_GMAC_FORCE_LINK_PASS;
3204 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3205 val);
3206 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003207 mvneta_port_up(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003208 } else {
Stas Sergeev898b2972015-04-01 20:32:49 +03003209 if (!pp->use_inband_status) {
3210 u32 val = mvreg_read(pp,
3211 MVNETA_GMAC_AUTONEG_CONFIG);
3212 val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3213 val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3214 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3215 val);
3216 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003217 mvneta_port_down(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003218 }
Ezequiel Garcia0089b742014-10-31 12:57:20 -03003219 phy_print_status(phydev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003220 }
3221}
3222
3223static int mvneta_mdio_probe(struct mvneta_port *pp)
3224{
3225 struct phy_device *phy_dev;
3226
3227 phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
3228 pp->phy_interface);
3229 if (!phy_dev) {
3230 netdev_err(pp->dev, "could not find the PHY\n");
3231 return -ENODEV;
3232 }
3233
3234 phy_dev->supported &= PHY_GBIT_FEATURES;
3235 phy_dev->advertising = phy_dev->supported;
3236
3237 pp->phy_dev = phy_dev;
3238 pp->link = 0;
3239 pp->duplex = 0;
3240 pp->speed = 0;
3241
3242 return 0;
3243}
3244
3245static void mvneta_mdio_remove(struct mvneta_port *pp)
3246{
3247 phy_disconnect(pp->phy_dev);
3248 pp->phy_dev = NULL;
3249}
3250
Maxime Ripardf8642882015-09-25 18:09:38 +02003251static void mvneta_percpu_enable(void *arg)
3252{
3253 struct mvneta_port *pp = arg;
3254
3255 enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3256}
3257
3258static void mvneta_percpu_disable(void *arg)
3259{
3260 struct mvneta_port *pp = arg;
3261
3262 disable_percpu_irq(pp->dev->irq);
3263}
3264
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003265/* Electing a CPU must be done in an atomic way: it should be done
3266 * after or before the removal/insertion of a CPU and this function is
3267 * not reentrant.
3268 */
Maxime Ripardf8642882015-09-25 18:09:38 +02003269static void mvneta_percpu_elect(struct mvneta_port *pp)
3270{
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003271 int elected_cpu = 0, max_cpu, cpu, i = 0;
Maxime Ripardf8642882015-09-25 18:09:38 +02003272
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003273 /* Use the cpu associated to the rxq when it is online, in all
3274 * the other cases, use the cpu 0 which can't be offline.
3275 */
3276 if (cpu_online(pp->rxq_def))
3277 elected_cpu = pp->rxq_def;
3278
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003279 max_cpu = num_present_cpus();
Maxime Ripardf8642882015-09-25 18:09:38 +02003280
3281 for_each_online_cpu(cpu) {
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003282 int rxq_map = 0, txq_map = 0;
3283 int rxq;
3284
3285 for (rxq = 0; rxq < rxq_number; rxq++)
3286 if ((rxq % max_cpu) == cpu)
3287 rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3288
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003289 if (cpu == elected_cpu)
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003290 /* Map the default receive queue queue to the
3291 * elected CPU
Maxime Ripardf8642882015-09-25 18:09:38 +02003292 */
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003293 rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003294
3295 /* We update the TX queue map only if we have one
3296 * queue. In this case we associate the TX queue to
3297 * the CPU bound to the default RX queue
3298 */
3299 if (txq_number == 1)
Gregory CLEMENTcad5d842016-02-04 22:09:24 +01003300 txq_map = (cpu == elected_cpu) ?
Gregory CLEMENT50bf8cb2015-12-09 18:23:51 +01003301 MVNETA_CPU_TXQ_ACCESS(1) : 0;
3302 else
3303 txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3304 MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3305
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003306 mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3307
3308 /* Update the interrupt mask on each CPU according the
3309 * new mapping
3310 */
3311 smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3312 pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003313 i++;
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003314
Maxime Ripardf8642882015-09-25 18:09:38 +02003315 }
3316};
3317
3318static int mvneta_percpu_notifier(struct notifier_block *nfb,
3319 unsigned long action, void *hcpu)
3320{
3321 struct mvneta_port *pp = container_of(nfb, struct mvneta_port,
3322 cpu_notifier);
3323 int cpu = (unsigned long)hcpu, other_cpu;
3324 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3325
3326 switch (action) {
3327 case CPU_ONLINE:
3328 case CPU_ONLINE_FROZEN:
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003329 spin_lock(&pp->lock);
3330 /* Configuring the driver for a new CPU while the
3331 * driver is stopping is racy, so just avoid it.
3332 */
3333 if (pp->is_stopped) {
3334 spin_unlock(&pp->lock);
3335 break;
3336 }
Maxime Ripardf8642882015-09-25 18:09:38 +02003337 netif_tx_stop_all_queues(pp->dev);
3338
3339 /* We have to synchronise on tha napi of each CPU
3340 * except the one just being waked up
3341 */
3342 for_each_online_cpu(other_cpu) {
3343 if (other_cpu != cpu) {
3344 struct mvneta_pcpu_port *other_port =
3345 per_cpu_ptr(pp->ports, other_cpu);
3346
3347 napi_synchronize(&other_port->napi);
3348 }
3349 }
3350
3351 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003352 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003353 napi_enable(&port->napi);
3354
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003355
3356 /* Enable per-CPU interrupts on the CPU that is
3357 * brought up.
3358 */
3359 smp_call_function_single(cpu, mvneta_percpu_enable,
3360 pp, true);
3361
Maxime Ripardf8642882015-09-25 18:09:38 +02003362 /* Enable per-CPU interrupt on the one CPU we care
3363 * about.
3364 */
3365 mvneta_percpu_elect(pp);
3366
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003367 /* Unmask all ethernet port interrupts */
3368 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003369 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3370 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3371 MVNETA_CAUSE_LINK_CHANGE |
3372 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3373 netif_tx_start_all_queues(pp->dev);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003374 spin_unlock(&pp->lock);
Maxime Ripardf8642882015-09-25 18:09:38 +02003375 break;
3376 case CPU_DOWN_PREPARE:
3377 case CPU_DOWN_PREPARE_FROZEN:
3378 netif_tx_stop_all_queues(pp->dev);
Gregory CLEMENT58885112016-02-04 22:09:28 +01003379 /* Thanks to this lock we are sure that any pending
3380 * cpu election is done
3381 */
3382 spin_lock(&pp->lock);
Maxime Ripardf8642882015-09-25 18:09:38 +02003383 /* Mask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003384 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT58885112016-02-04 22:09:28 +01003385 spin_unlock(&pp->lock);
Maxime Ripardf8642882015-09-25 18:09:38 +02003386
3387 napi_synchronize(&port->napi);
3388 napi_disable(&port->napi);
3389 /* Disable per-CPU interrupts on the CPU that is
3390 * brought down.
3391 */
3392 smp_call_function_single(cpu, mvneta_percpu_disable,
3393 pp, true);
3394
3395 break;
3396 case CPU_DEAD:
3397 case CPU_DEAD_FROZEN:
3398 /* Check if a new CPU must be elected now this on is down */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003399 spin_lock(&pp->lock);
Maxime Ripardf8642882015-09-25 18:09:38 +02003400 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003401 spin_unlock(&pp->lock);
Maxime Ripardf8642882015-09-25 18:09:38 +02003402 /* Unmask all ethernet port interrupts */
Gregory CLEMENTdb488c12016-02-04 22:09:27 +01003403 on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
Maxime Ripardf8642882015-09-25 18:09:38 +02003404 mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3405 MVNETA_CAUSE_PHY_STATUS_CHANGE |
3406 MVNETA_CAUSE_LINK_CHANGE |
3407 MVNETA_CAUSE_PSC_SYNC_CHANGE);
3408 netif_tx_start_all_queues(pp->dev);
3409 break;
3410 }
3411
3412 return NOTIFY_OK;
3413}
3414
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003415static int mvneta_open(struct net_device *dev)
3416{
3417 struct mvneta_port *pp = netdev_priv(dev);
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003418 int ret;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003419
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003420 pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
willy tarreau8ec2cd42014-01-16 08:20:16 +01003421 pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3422 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003423
3424 ret = mvneta_setup_rxqs(pp);
3425 if (ret)
3426 return ret;
3427
3428 ret = mvneta_setup_txqs(pp);
3429 if (ret)
3430 goto err_cleanup_rxqs;
3431
3432 /* Connect to port interrupt line */
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003433 ret = request_percpu_irq(pp->dev->irq, mvneta_isr,
3434 MVNETA_DRIVER_NAME, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003435 if (ret) {
3436 netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3437 goto err_cleanup_txqs;
3438 }
3439
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003440 /* Enable per-CPU interrupt on all the CPU to handle our RX
3441 * queue interrupts
3442 */
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003443 on_each_cpu(mvneta_percpu_enable, pp, true);
Gregory CLEMENT2dcf75e2015-12-09 18:23:49 +01003444
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003445 pp->is_stopped = false;
Maxime Ripardf8642882015-09-25 18:09:38 +02003446 /* Register a CPU notifier to handle the case where our CPU
3447 * might be taken offline.
3448 */
3449 register_cpu_notifier(&pp->cpu_notifier);
3450
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");
3457 goto err_free_irq;
3458 }
3459
3460 mvneta_start_dev(pp);
3461
3462 return 0;
3463
3464err_free_irq:
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003465 free_percpu_irq(pp->dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003466err_cleanup_txqs:
3467 mvneta_cleanup_txqs(pp);
3468err_cleanup_rxqs:
3469 mvneta_cleanup_rxqs(pp);
3470 return ret;
3471}
3472
3473/* Stop the port, free port interrupt line */
3474static int mvneta_stop(struct net_device *dev)
3475{
3476 struct mvneta_port *pp = netdev_priv(dev);
3477
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003478 /* Inform that we are stopping so we don't want to setup the
3479 * driver for new CPUs in the notifiers
3480 */
3481 spin_lock(&pp->lock);
3482 pp->is_stopped = true;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003483 mvneta_stop_dev(pp);
3484 mvneta_mdio_remove(pp);
Maxime Ripardf8642882015-09-25 18:09:38 +02003485 unregister_cpu_notifier(&pp->cpu_notifier);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003486 /* Now that the notifier are unregistered, we can release le
3487 * lock
3488 */
3489 spin_unlock(&pp->lock);
Gregory CLEMENT129219e2016-02-04 22:09:23 +01003490 on_each_cpu(mvneta_percpu_disable, pp, true);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003491 free_percpu_irq(dev->irq, pp->ports);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003492 mvneta_cleanup_rxqs(pp);
3493 mvneta_cleanup_txqs(pp);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003494
3495 return 0;
3496}
3497
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003498static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3499{
3500 struct mvneta_port *pp = netdev_priv(dev);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003501
3502 if (!pp->phy_dev)
3503 return -ENOTSUPP;
3504
Stas Sergeevecf7b362015-04-01 19:23:29 +03003505 return phy_mii_ioctl(pp->phy_dev, ifr, cmd);
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003506}
3507
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003508/* Ethtool methods */
3509
3510/* Get settings (phy address, speed) for ethtools */
3511int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3512{
3513 struct mvneta_port *pp = netdev_priv(dev);
3514
3515 if (!pp->phy_dev)
3516 return -ENODEV;
3517
3518 return phy_ethtool_gset(pp->phy_dev, cmd);
3519}
3520
3521/* Set settings (phy address, speed) for ethtools */
3522int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
3523{
3524 struct mvneta_port *pp = netdev_priv(dev);
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003525 struct phy_device *phydev = pp->phy_dev;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003526
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003527 if (!phydev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003528 return -ENODEV;
3529
Stas Sergeev0c0744f2015-12-02 20:35:11 +03003530 if ((cmd->autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
3531 u32 val;
3532
3533 mvneta_set_autoneg(pp, cmd->autoneg == AUTONEG_ENABLE);
3534
3535 if (cmd->autoneg == AUTONEG_DISABLE) {
3536 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3537 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3538 MVNETA_GMAC_CONFIG_GMII_SPEED |
3539 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3540
3541 if (phydev->duplex)
3542 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3543
3544 if (phydev->speed == SPEED_1000)
3545 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3546 else if (phydev->speed == SPEED_100)
3547 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3548
3549 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3550 }
3551
3552 pp->use_inband_status = (cmd->autoneg == AUTONEG_ENABLE);
3553 netdev_info(pp->dev, "autoneg status set to %i\n",
3554 pp->use_inband_status);
3555
3556 if (netif_running(dev)) {
3557 mvneta_port_down(pp);
3558 mvneta_port_up(pp);
3559 }
3560 }
3561
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003562 return phy_ethtool_sset(pp->phy_dev, cmd);
3563}
3564
3565/* Set interrupt coalescing for ethtools */
3566static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3567 struct ethtool_coalesce *c)
3568{
3569 struct mvneta_port *pp = netdev_priv(dev);
3570 int queue;
3571
3572 for (queue = 0; queue < rxq_number; queue++) {
3573 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3574 rxq->time_coal = c->rx_coalesce_usecs;
3575 rxq->pkts_coal = c->rx_max_coalesced_frames;
3576 mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3577 mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3578 }
3579
3580 for (queue = 0; queue < txq_number; queue++) {
3581 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3582 txq->done_pkts_coal = c->tx_max_coalesced_frames;
3583 mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3584 }
3585
3586 return 0;
3587}
3588
3589/* get coalescing for ethtools */
3590static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3591 struct ethtool_coalesce *c)
3592{
3593 struct mvneta_port *pp = netdev_priv(dev);
3594
3595 c->rx_coalesce_usecs = pp->rxqs[0].time_coal;
3596 c->rx_max_coalesced_frames = pp->rxqs[0].pkts_coal;
3597
3598 c->tx_max_coalesced_frames = pp->txqs[0].done_pkts_coal;
3599 return 0;
3600}
3601
3602
3603static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3604 struct ethtool_drvinfo *drvinfo)
3605{
3606 strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3607 sizeof(drvinfo->driver));
3608 strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3609 sizeof(drvinfo->version));
3610 strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3611 sizeof(drvinfo->bus_info));
3612}
3613
3614
3615static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3616 struct ethtool_ringparam *ring)
3617{
3618 struct mvneta_port *pp = netdev_priv(netdev);
3619
3620 ring->rx_max_pending = MVNETA_MAX_RXD;
3621 ring->tx_max_pending = MVNETA_MAX_TXD;
3622 ring->rx_pending = pp->rx_ring_size;
3623 ring->tx_pending = pp->tx_ring_size;
3624}
3625
3626static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3627 struct ethtool_ringparam *ring)
3628{
3629 struct mvneta_port *pp = netdev_priv(dev);
3630
3631 if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3632 return -EINVAL;
3633 pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3634 ring->rx_pending : MVNETA_MAX_RXD;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03003635
3636 pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3637 MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3638 if (pp->tx_ring_size != ring->tx_pending)
3639 netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3640 pp->tx_ring_size, ring->tx_pending);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003641
3642 if (netif_running(dev)) {
3643 mvneta_stop(dev);
3644 if (mvneta_open(dev)) {
3645 netdev_err(dev,
3646 "error on opening device after ring param change\n");
3647 return -ENOMEM;
3648 }
3649 }
3650
3651 return 0;
3652}
3653
Russell King9b0cdef2015-10-22 18:37:30 +01003654static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3655 u8 *data)
3656{
3657 if (sset == ETH_SS_STATS) {
3658 int i;
3659
3660 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3661 memcpy(data + i * ETH_GSTRING_LEN,
3662 mvneta_statistics[i].name, ETH_GSTRING_LEN);
3663 }
3664}
3665
3666static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3667{
3668 const struct mvneta_statistic *s;
3669 void __iomem *base = pp->base;
3670 u32 high, low, val;
Jisheng Zhang2c832292016-01-20 16:36:25 +08003671 u64 val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003672 int i;
3673
3674 for (i = 0, s = mvneta_statistics;
3675 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3676 s++, i++) {
Russell King9b0cdef2015-10-22 18:37:30 +01003677 switch (s->type) {
3678 case T_REG_32:
3679 val = readl_relaxed(base + s->offset);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003680 pp->ethtool_stats[i] += val;
Russell King9b0cdef2015-10-22 18:37:30 +01003681 break;
3682 case T_REG_64:
3683 /* Docs say to read low 32-bit then high */
3684 low = readl_relaxed(base + s->offset);
3685 high = readl_relaxed(base + s->offset + 4);
Jisheng Zhang2c832292016-01-20 16:36:25 +08003686 val64 = (u64)high << 32 | low;
3687 pp->ethtool_stats[i] += val64;
Russell King9b0cdef2015-10-22 18:37:30 +01003688 break;
3689 }
Russell King9b0cdef2015-10-22 18:37:30 +01003690 }
3691}
3692
3693static void mvneta_ethtool_get_stats(struct net_device *dev,
3694 struct ethtool_stats *stats, u64 *data)
3695{
3696 struct mvneta_port *pp = netdev_priv(dev);
3697 int i;
3698
3699 mvneta_ethtool_update_stats(pp);
3700
3701 for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3702 *data++ = pp->ethtool_stats[i];
3703}
3704
3705static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3706{
3707 if (sset == ETH_SS_STATS)
3708 return ARRAY_SIZE(mvneta_statistics);
3709 return -EOPNOTSUPP;
3710}
3711
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003712static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3713{
3714 return MVNETA_RSS_LU_TABLE_SIZE;
3715}
3716
3717static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3718 struct ethtool_rxnfc *info,
3719 u32 *rules __always_unused)
3720{
3721 switch (info->cmd) {
3722 case ETHTOOL_GRXRINGS:
3723 info->data = rxq_number;
3724 return 0;
3725 case ETHTOOL_GRXFH:
3726 return -EOPNOTSUPP;
3727 default:
3728 return -EOPNOTSUPP;
3729 }
3730}
3731
3732static int mvneta_config_rss(struct mvneta_port *pp)
3733{
3734 int cpu;
3735 u32 val;
3736
3737 netif_tx_stop_all_queues(pp->dev);
3738
Gregory CLEMENT6b125d62016-02-04 22:09:25 +01003739 on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003740
3741 /* We have to synchronise on the napi of each CPU */
3742 for_each_online_cpu(cpu) {
3743 struct mvneta_pcpu_port *pcpu_port =
3744 per_cpu_ptr(pp->ports, cpu);
3745
3746 napi_synchronize(&pcpu_port->napi);
3747 napi_disable(&pcpu_port->napi);
3748 }
3749
3750 pp->rxq_def = pp->indir[0];
3751
3752 /* Update unicast mapping */
3753 mvneta_set_rx_mode(pp->dev);
3754
3755 /* Update val of portCfg register accordingly with all RxQueue types */
3756 val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3757 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3758
3759 /* Update the elected CPU matching the new rxq_def */
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003760 spin_lock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003761 mvneta_percpu_elect(pp);
Gregory CLEMENT120cfa52016-02-04 22:09:29 +01003762 spin_unlock(&pp->lock);
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003763
3764 /* We have to synchronise on the napi of each CPU */
3765 for_each_online_cpu(cpu) {
3766 struct mvneta_pcpu_port *pcpu_port =
3767 per_cpu_ptr(pp->ports, cpu);
3768
3769 napi_enable(&pcpu_port->napi);
3770 }
3771
3772 netif_tx_start_all_queues(pp->dev);
3773
3774 return 0;
3775}
3776
3777static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3778 const u8 *key, const u8 hfunc)
3779{
3780 struct mvneta_port *pp = netdev_priv(dev);
3781 /* We require at least one supported parameter to be changed
3782 * and no change in any of the unsupported parameters
3783 */
3784 if (key ||
3785 (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3786 return -EOPNOTSUPP;
3787
3788 if (!indir)
3789 return 0;
3790
3791 memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3792
3793 return mvneta_config_rss(pp);
3794}
3795
3796static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3797 u8 *hfunc)
3798{
3799 struct mvneta_port *pp = netdev_priv(dev);
3800
3801 if (hfunc)
3802 *hfunc = ETH_RSS_HASH_TOP;
3803
3804 if (!indir)
3805 return 0;
3806
3807 memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3808
3809 return 0;
3810}
3811
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003812static const struct net_device_ops mvneta_netdev_ops = {
3813 .ndo_open = mvneta_open,
3814 .ndo_stop = mvneta_stop,
3815 .ndo_start_xmit = mvneta_tx,
3816 .ndo_set_rx_mode = mvneta_set_rx_mode,
3817 .ndo_set_mac_address = mvneta_set_mac_addr,
3818 .ndo_change_mtu = mvneta_change_mtu,
Simon Guinotb65657f2015-06-30 16:20:22 +02003819 .ndo_fix_features = mvneta_fix_features,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003820 .ndo_get_stats64 = mvneta_get_stats64,
Thomas Petazzoni15f59452013-09-04 16:26:52 +02003821 .ndo_do_ioctl = mvneta_ioctl,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003822};
3823
3824const struct ethtool_ops mvneta_eth_tool_ops = {
3825 .get_link = ethtool_op_get_link,
3826 .get_settings = mvneta_ethtool_get_settings,
3827 .set_settings = mvneta_ethtool_set_settings,
3828 .set_coalesce = mvneta_ethtool_set_coalesce,
3829 .get_coalesce = mvneta_ethtool_get_coalesce,
3830 .get_drvinfo = mvneta_ethtool_get_drvinfo,
3831 .get_ringparam = mvneta_ethtool_get_ringparam,
3832 .set_ringparam = mvneta_ethtool_set_ringparam,
Russell King9b0cdef2015-10-22 18:37:30 +01003833 .get_strings = mvneta_ethtool_get_strings,
3834 .get_ethtool_stats = mvneta_ethtool_get_stats,
3835 .get_sset_count = mvneta_ethtool_get_sset_count,
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01003836 .get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
3837 .get_rxnfc = mvneta_ethtool_get_rxnfc,
3838 .get_rxfh = mvneta_ethtool_get_rxfh,
3839 .set_rxfh = mvneta_ethtool_set_rxfh,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003840};
3841
3842/* Initialize hw */
Ezequiel Garcia96728502014-05-22 20:06:59 -03003843static int mvneta_init(struct device *dev, struct mvneta_port *pp)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003844{
3845 int queue;
3846
3847 /* Disable port */
3848 mvneta_port_disable(pp);
3849
3850 /* Set port default values */
3851 mvneta_defaults_set(pp);
3852
Ezequiel Garcia96728502014-05-22 20:06:59 -03003853 pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
3854 GFP_KERNEL);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003855 if (!pp->txqs)
3856 return -ENOMEM;
3857
3858 /* Initialize TX descriptor rings */
3859 for (queue = 0; queue < txq_number; queue++) {
3860 struct mvneta_tx_queue *txq = &pp->txqs[queue];
3861 txq->id = queue;
3862 txq->size = pp->tx_ring_size;
3863 txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
3864 }
3865
Ezequiel Garcia96728502014-05-22 20:06:59 -03003866 pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
3867 GFP_KERNEL);
3868 if (!pp->rxqs)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003869 return -ENOMEM;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003870
3871 /* Create Rx descriptor rings */
3872 for (queue = 0; queue < rxq_number; queue++) {
3873 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3874 rxq->id = queue;
3875 rxq->size = pp->rx_ring_size;
3876 rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
3877 rxq->time_coal = MVNETA_RX_COAL_USEC;
3878 }
3879
3880 return 0;
3881}
3882
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003883/* platform glue : initialize decoding windows */
Greg KH03ce7582012-12-21 13:42:15 +00003884static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
3885 const struct mbus_dram_target_info *dram)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003886{
3887 u32 win_enable;
3888 u32 win_protect;
3889 int i;
3890
3891 for (i = 0; i < 6; i++) {
3892 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
3893 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
3894
3895 if (i < 4)
3896 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
3897 }
3898
3899 win_enable = 0x3f;
3900 win_protect = 0;
3901
3902 for (i = 0; i < dram->num_cs; i++) {
3903 const struct mbus_dram_window *cs = dram->cs + i;
3904 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
3905 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
3906
3907 mvreg_write(pp, MVNETA_WIN_SIZE(i),
3908 (cs->size - 1) & 0xffff0000);
3909
3910 win_enable &= ~(1 << i);
3911 win_protect |= 3 << (2 * i);
3912 }
3913
3914 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
Marcin Wojtasdb6ba9a2015-11-30 13:27:41 +01003915 mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003916}
3917
3918/* Power up the port */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003919static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003920{
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003921 u32 ctrl;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003922
3923 /* MAC Cause register should be cleared */
3924 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
3925
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003926 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003927
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003928 /* Even though it might look weird, when we're configured in
3929 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3930 */
3931 switch(phy_mode) {
3932 case PHY_INTERFACE_MODE_QSGMII:
3933 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
3934 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
3935 break;
3936 case PHY_INTERFACE_MODE_SGMII:
3937 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
3938 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
3939 break;
3940 case PHY_INTERFACE_MODE_RGMII:
3941 case PHY_INTERFACE_MODE_RGMII_ID:
3942 ctrl |= MVNETA_GMAC2_PORT_RGMII;
3943 break;
3944 default:
3945 return -EINVAL;
3946 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003947
3948 /* Cancel Port Reset */
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003949 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
3950 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003951
3952 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3953 MVNETA_GMAC2_PORT_RESET) != 0)
3954 continue;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02003955
3956 return 0;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003957}
3958
3959/* Device initialization routine */
Greg KH03ce7582012-12-21 13:42:15 +00003960static int mvneta_probe(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003961{
3962 const struct mbus_dram_target_info *dram_target_info;
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01003963 struct resource *res;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003964 struct device_node *dn = pdev->dev.of_node;
3965 struct device_node *phy_node;
Marcin Wojtasdc35a102016-03-14 09:39:03 +01003966 struct device_node *bm_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003967 struct mvneta_port *pp;
3968 struct net_device *dev;
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00003969 const char *dt_mac_addr;
3970 char hw_mac_addr[ETH_ALEN];
3971 const char *mac_from;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07003972 const char *managed;
Marcin Wojtas9110ee02015-11-30 13:27:45 +01003973 int tx_csum_limit;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003974 int phy_mode;
3975 int err;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02003976 int cpu;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003977
Willy Tarreauee40a112013-04-11 23:00:37 +02003978 dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03003979 if (!dev)
3980 return -ENOMEM;
3981
3982 dev->irq = irq_of_parse_and_map(dn, 0);
3983 if (dev->irq == 0) {
3984 err = -EINVAL;
3985 goto err_free_netdev;
3986 }
3987
3988 phy_node = of_parse_phandle(dn, "phy", 0);
3989 if (!phy_node) {
Thomas Petazzoni83895be2014-05-16 16:14:06 +02003990 if (!of_phy_is_fixed_link(dn)) {
3991 dev_err(&pdev->dev, "no PHY specified\n");
3992 err = -ENODEV;
3993 goto err_free_irq;
3994 }
3995
3996 err = of_phy_register_fixed_link(dn);
3997 if (err < 0) {
3998 dev_err(&pdev->dev, "cannot register fixed PHY\n");
3999 goto err_free_irq;
4000 }
4001
4002 /* In the case of a fixed PHY, the DT node associated
4003 * to the PHY is the Ethernet MAC DT node.
4004 */
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004005 phy_node = of_node_get(dn);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004006 }
4007
4008 phy_mode = of_get_phy_mode(dn);
4009 if (phy_mode < 0) {
4010 dev_err(&pdev->dev, "incorrect phy-mode\n");
4011 err = -EINVAL;
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004012 goto err_put_phy_node;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004013 }
4014
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004015 dev->tx_queue_len = MVNETA_MAX_TXD;
4016 dev->watchdog_timeo = 5 * HZ;
4017 dev->netdev_ops = &mvneta_netdev_ops;
4018
Wilfried Klaebe7ad24ea2014-05-11 00:12:32 +00004019 dev->ethtool_ops = &mvneta_eth_tool_ops;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004020
4021 pp = netdev_priv(dev);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004022 pp->phy_node = phy_node;
4023 pp->phy_interface = phy_mode;
Stas Sergeevf8af8e62015-07-20 17:49:58 -07004024
4025 err = of_property_read_string(dn, "managed", &managed);
4026 pp->use_inband_status = (err == 0 &&
4027 strcmp(managed, "in-band-status") == 0);
Maxime Ripardf8642882015-09-25 18:09:38 +02004028 pp->cpu_notifier.notifier_call = mvneta_percpu_notifier;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004029
Gregory CLEMENT90b74c02015-12-09 18:23:48 +01004030 pp->rxq_def = rxq_def;
4031
Gregory CLEMENT9a401de2015-12-09 18:23:50 +01004032 pp->indir[0] = rxq_def;
4033
Jisheng Zhang2804ba42016-01-20 19:27:23 +08004034 pp->clk = devm_clk_get(&pdev->dev, "core");
4035 if (IS_ERR(pp->clk))
4036 pp->clk = devm_clk_get(&pdev->dev, NULL);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004037 if (IS_ERR(pp->clk)) {
4038 err = PTR_ERR(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004039 goto err_put_phy_node;
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004040 }
4041
4042 clk_prepare_enable(pp->clk);
4043
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004044 pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4045 if (!IS_ERR(pp->clk_bus))
4046 clk_prepare_enable(pp->clk_bus);
4047
Thomas Petazzonic3f0dd32014-03-27 11:39:29 +01004048 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4049 pp->base = devm_ioremap_resource(&pdev->dev, res);
4050 if (IS_ERR(pp->base)) {
4051 err = PTR_ERR(pp->base);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004052 goto err_clk;
4053 }
4054
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004055 /* Alloc per-cpu port structure */
4056 pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4057 if (!pp->ports) {
4058 err = -ENOMEM;
4059 goto err_clk;
4060 }
4061
willy tarreau74c41b02014-01-16 08:20:08 +01004062 /* Alloc per-cpu stats */
WANG Cong1c213bd2014-02-13 11:46:28 -08004063 pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
willy tarreau74c41b02014-01-16 08:20:08 +01004064 if (!pp->stats) {
4065 err = -ENOMEM;
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004066 goto err_free_ports;
willy tarreau74c41b02014-01-16 08:20:08 +01004067 }
4068
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004069 dt_mac_addr = of_get_mac_address(dn);
Luka Perkov6c7a9a32013-10-30 00:10:01 +01004070 if (dt_mac_addr) {
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004071 mac_from = "device tree";
4072 memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4073 } else {
4074 mvneta_get_mac_addr(pp, hw_mac_addr);
4075 if (is_valid_ether_addr(hw_mac_addr)) {
4076 mac_from = "hardware";
4077 memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4078 } else {
4079 mac_from = "random";
4080 eth_hw_addr_random(dev);
4081 }
4082 }
4083
Marcin Wojtas9110ee02015-11-30 13:27:45 +01004084 if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4085 if (tx_csum_limit < 0 ||
4086 tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4087 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4088 dev_info(&pdev->dev,
4089 "Wrong TX csum limit in DT, set to %dB\n",
4090 MVNETA_TX_CSUM_DEF_SIZE);
4091 }
4092 } else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4093 tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4094 } else {
4095 tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4096 }
4097
4098 pp->tx_csum_limit = tx_csum_limit;
Simon Guinotb65657f2015-06-30 16:20:22 +02004099
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004100 dram_target_info = mv_mbus_dram_info();
4101 if (dram_target_info)
4102 mvneta_conf_mbus_windows(pp, dram_target_info);
4103
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004104 pp->tx_ring_size = MVNETA_MAX_TXD;
4105 pp->rx_ring_size = MVNETA_MAX_RXD;
4106
4107 pp->dev = dev;
4108 SET_NETDEV_DEV(dev, &pdev->dev);
4109
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004110 pp->id = global_port_id++;
4111
4112 /* Obtain access to BM resources if enabled and already initialized */
4113 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4114 if (bm_node && bm_node->data) {
4115 pp->bm_priv = bm_node->data;
4116 err = mvneta_bm_port_init(pdev, pp);
4117 if (err < 0) {
4118 dev_info(&pdev->dev, "use SW buffer management\n");
4119 pp->bm_priv = NULL;
4120 }
4121 }
4122
Ezequiel Garcia96728502014-05-22 20:06:59 -03004123 err = mvneta_init(&pdev->dev, pp);
4124 if (err < 0)
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004125 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004126
4127 err = mvneta_port_power_up(pp, phy_mode);
4128 if (err < 0) {
4129 dev_err(&pdev->dev, "can't power up port\n");
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004130 goto err_netdev;
Thomas Petazzoni3f1dd4b2014-04-15 15:50:20 +02004131 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004132
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004133 for_each_present_cpu(cpu) {
4134 struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4135
4136 netif_napi_add(dev, &port->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4137 port->pp = pp;
4138 }
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004139
Ezequiel Garcia2adb7192014-05-19 13:59:55 -03004140 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
Ezequiel Garcia01ef26c2014-05-19 13:59:53 -03004141 dev->hw_features |= dev->features;
4142 dev->vlan_features |= dev->features;
willy tarreaub50b72d2013-04-06 08:47:01 +00004143 dev->priv_flags |= IFF_UNICAST_FLT;
Ezequiel Garcia8eef5f92014-05-30 13:40:05 -03004144 dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
willy tarreaub50b72d2013-04-06 08:47:01 +00004145
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004146 err = register_netdev(dev);
4147 if (err < 0) {
4148 dev_err(&pdev->dev, "failed to register\n");
Ezequiel Garcia96728502014-05-22 20:06:59 -03004149 goto err_free_stats;
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004150 }
4151
Thomas Petazzoni8cc3e432013-06-04 04:52:23 +00004152 netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4153 dev->dev_addr);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004154
4155 platform_set_drvdata(pdev, pp->dev);
4156
Stas Sergeev898b2972015-04-01 20:32:49 +03004157 if (pp->use_inband_status) {
4158 struct phy_device *phy = of_phy_find_device(dn);
4159
4160 mvneta_fixed_link_update(pp, phy);
Russell King04d53b22015-09-24 20:36:18 +01004161
Andrew Lunne5a03bf2016-01-06 20:11:16 +01004162 put_device(&phy->mdio.dev);
Stas Sergeev898b2972015-04-01 20:32:49 +03004163 }
4164
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004165 return 0;
4166
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004167err_netdev:
4168 unregister_netdev(dev);
4169 if (pp->bm_priv) {
4170 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4171 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4172 1 << pp->id);
4173 }
willy tarreau74c41b02014-01-16 08:20:08 +01004174err_free_stats:
4175 free_percpu(pp->stats);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004176err_free_ports:
4177 free_percpu(pp->ports);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004178err_clk:
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004179 clk_disable_unprepare(pp->clk_bus);
Arnaud Patard \(Rtp\)5445eaf2013-07-29 21:56:48 +02004180 clk_disable_unprepare(pp->clk);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004181err_put_phy_node:
4182 of_node_put(phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004183err_free_irq:
4184 irq_dispose_mapping(dev->irq);
4185err_free_netdev:
4186 free_netdev(dev);
4187 return err;
4188}
4189
4190/* Device removal routine */
Greg KH03ce7582012-12-21 13:42:15 +00004191static int mvneta_remove(struct platform_device *pdev)
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004192{
4193 struct net_device *dev = platform_get_drvdata(pdev);
4194 struct mvneta_port *pp = netdev_priv(dev);
4195
4196 unregister_netdev(dev);
Jisheng Zhang15cc4a42016-01-20 19:27:24 +08004197 clk_disable_unprepare(pp->clk_bus);
Thomas Petazzoni189dd622012-11-19 14:15:25 +01004198 clk_disable_unprepare(pp->clk);
Maxime Ripard12bb03b2015-09-25 18:09:36 +02004199 free_percpu(pp->ports);
willy tarreau74c41b02014-01-16 08:20:08 +01004200 free_percpu(pp->stats);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004201 irq_dispose_mapping(dev->irq);
Uwe Kleine-Königc891c242014-08-07 21:58:46 +02004202 of_node_put(pp->phy_node);
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004203 free_netdev(dev);
4204
Marcin Wojtasdc35a102016-03-14 09:39:03 +01004205 if (pp->bm_priv) {
4206 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4207 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4208 1 << pp->id);
4209 }
4210
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004211 return 0;
4212}
4213
4214static const struct of_device_id mvneta_match[] = {
4215 { .compatible = "marvell,armada-370-neta" },
Simon Guinotf522a972015-06-30 16:20:20 +02004216 { .compatible = "marvell,armada-xp-neta" },
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004217 { }
4218};
4219MODULE_DEVICE_TABLE(of, mvneta_match);
4220
4221static struct platform_driver mvneta_driver = {
4222 .probe = mvneta_probe,
Greg KH03ce7582012-12-21 13:42:15 +00004223 .remove = mvneta_remove,
Thomas Petazzonic5aff182012-08-17 14:04:28 +03004224 .driver = {
4225 .name = MVNETA_DRIVER_NAME,
4226 .of_match_table = mvneta_match,
4227 },
4228};
4229
4230module_platform_driver(mvneta_driver);
4231
4232MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4233MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4234MODULE_LICENSE("GPL");
4235
4236module_param(rxq_number, int, S_IRUGO);
4237module_param(txq_number, int, S_IRUGO);
4238
4239module_param(rxq_def, int, S_IRUGO);
willy tarreauf19fadf2014-01-16 08:20:17 +01004240module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);