1c5aff182SThomas Petazzoni /*
2c5aff182SThomas Petazzoni  * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
3c5aff182SThomas Petazzoni  *
4c5aff182SThomas Petazzoni  * Copyright (C) 2012 Marvell
5c5aff182SThomas Petazzoni  *
6c5aff182SThomas Petazzoni  * Rami Rosen <rosenr@marvell.com>
7c5aff182SThomas Petazzoni  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
8c5aff182SThomas Petazzoni  *
9c5aff182SThomas Petazzoni  * This file is licensed under the terms of the GNU General Public
10c5aff182SThomas Petazzoni  * License version 2. This program is licensed "as is" without any
11c5aff182SThomas Petazzoni  * warranty of any kind, whether express or implied.
12c5aff182SThomas Petazzoni  */
13c5aff182SThomas Petazzoni 
140e03f563SJisheng Zhang #include <linux/clk.h>
150e03f563SJisheng Zhang #include <linux/cpu.h>
16c5aff182SThomas Petazzoni #include <linux/etherdevice.h>
170e03f563SJisheng Zhang #include <linux/if_vlan.h>
18c5aff182SThomas Petazzoni #include <linux/inetdevice.h>
190e03f563SJisheng Zhang #include <linux/interrupt.h>
200e03f563SJisheng Zhang #include <linux/io.h>
210e03f563SJisheng Zhang #include <linux/kernel.h>
22c5aff182SThomas Petazzoni #include <linux/mbus.h>
23c5aff182SThomas Petazzoni #include <linux/module.h>
240e03f563SJisheng Zhang #include <linux/netdevice.h>
25c5aff182SThomas Petazzoni #include <linux/of.h>
260e03f563SJisheng Zhang #include <linux/of_address.h>
27c5aff182SThomas Petazzoni #include <linux/of_irq.h>
28c5aff182SThomas Petazzoni #include <linux/of_mdio.h>
29c5aff182SThomas Petazzoni #include <linux/of_net.h>
30c5aff182SThomas Petazzoni #include <linux/phy.h>
31503f9aa9SRussell King #include <linux/phylink.h>
320e03f563SJisheng Zhang #include <linux/platform_device.h>
330e03f563SJisheng Zhang #include <linux/skbuff.h>
34baa11ebcSGregory CLEMENT #include <net/hwbm.h>
35dc35a10fSMarcin Wojtas #include "mvneta_bm.h"
360e03f563SJisheng Zhang #include <net/ip.h>
370e03f563SJisheng Zhang #include <net/ipv6.h>
380e03f563SJisheng Zhang #include <net/tso.h>
39c5aff182SThomas Petazzoni 
40c5aff182SThomas Petazzoni /* Registers */
41c5aff182SThomas Petazzoni #define MVNETA_RXQ_CONFIG_REG(q)                (0x1400 + ((q) << 2))
42e5bdf689SMarcin Wojtas #define      MVNETA_RXQ_HW_BUF_ALLOC            BIT(0)
43dc35a10fSMarcin Wojtas #define      MVNETA_RXQ_SHORT_POOL_ID_SHIFT	4
44dc35a10fSMarcin Wojtas #define      MVNETA_RXQ_SHORT_POOL_ID_MASK	0x30
45dc35a10fSMarcin Wojtas #define      MVNETA_RXQ_LONG_POOL_ID_SHIFT	6
46dc35a10fSMarcin Wojtas #define      MVNETA_RXQ_LONG_POOL_ID_MASK	0xc0
47c5aff182SThomas Petazzoni #define      MVNETA_RXQ_PKT_OFFSET_ALL_MASK     (0xf    << 8)
48c5aff182SThomas Petazzoni #define      MVNETA_RXQ_PKT_OFFSET_MASK(offs)   ((offs) << 8)
49c5aff182SThomas Petazzoni #define MVNETA_RXQ_THRESHOLD_REG(q)             (0x14c0 + ((q) << 2))
50c5aff182SThomas Petazzoni #define      MVNETA_RXQ_NON_OCCUPIED(v)         ((v) << 16)
51c5aff182SThomas Petazzoni #define MVNETA_RXQ_BASE_ADDR_REG(q)             (0x1480 + ((q) << 2))
52c5aff182SThomas Petazzoni #define MVNETA_RXQ_SIZE_REG(q)                  (0x14a0 + ((q) << 2))
53c5aff182SThomas Petazzoni #define      MVNETA_RXQ_BUF_SIZE_SHIFT          19
54c5aff182SThomas Petazzoni #define      MVNETA_RXQ_BUF_SIZE_MASK           (0x1fff << 19)
55c5aff182SThomas Petazzoni #define MVNETA_RXQ_STATUS_REG(q)                (0x14e0 + ((q) << 2))
56c5aff182SThomas Petazzoni #define      MVNETA_RXQ_OCCUPIED_ALL_MASK       0x3fff
57c5aff182SThomas Petazzoni #define MVNETA_RXQ_STATUS_UPDATE_REG(q)         (0x1500 + ((q) << 2))
58c5aff182SThomas Petazzoni #define      MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT  16
59c5aff182SThomas Petazzoni #define      MVNETA_RXQ_ADD_NON_OCCUPIED_MAX    255
60dc35a10fSMarcin Wojtas #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool)	(0x1700 + ((pool) << 2))
61dc35a10fSMarcin Wojtas #define      MVNETA_PORT_POOL_BUFFER_SZ_SHIFT	3
62dc35a10fSMarcin Wojtas #define      MVNETA_PORT_POOL_BUFFER_SZ_MASK	0xfff8
63c5aff182SThomas Petazzoni #define MVNETA_PORT_RX_RESET                    0x1cc0
64c5aff182SThomas Petazzoni #define      MVNETA_PORT_RX_DMA_RESET           BIT(0)
65c5aff182SThomas Petazzoni #define MVNETA_PHY_ADDR                         0x2000
66c5aff182SThomas Petazzoni #define      MVNETA_PHY_ADDR_MASK               0x1f
67c5aff182SThomas Petazzoni #define MVNETA_MBUS_RETRY                       0x2010
68c5aff182SThomas Petazzoni #define MVNETA_UNIT_INTR_CAUSE                  0x2080
69c5aff182SThomas Petazzoni #define MVNETA_UNIT_CONTROL                     0x20B0
70c5aff182SThomas Petazzoni #define      MVNETA_PHY_POLLING_ENABLE          BIT(1)
71c5aff182SThomas Petazzoni #define MVNETA_WIN_BASE(w)                      (0x2200 + ((w) << 3))
72c5aff182SThomas Petazzoni #define MVNETA_WIN_SIZE(w)                      (0x2204 + ((w) << 3))
73c5aff182SThomas Petazzoni #define MVNETA_WIN_REMAP(w)                     (0x2280 + ((w) << 2))
74c5aff182SThomas Petazzoni #define MVNETA_BASE_ADDR_ENABLE                 0x2290
75db6ba9a5SMarcin Wojtas #define MVNETA_ACCESS_PROTECT_ENABLE            0x2294
76c5aff182SThomas Petazzoni #define MVNETA_PORT_CONFIG                      0x2400
77c5aff182SThomas Petazzoni #define      MVNETA_UNI_PROMISC_MODE            BIT(0)
78c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ(q)                  ((q) << 1)
79c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ_ARP(q)              ((q) << 4)
80c5aff182SThomas Petazzoni #define      MVNETA_TX_UNSET_ERR_SUM            BIT(12)
81c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ_TCP(q)              ((q) << 16)
82c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ_UDP(q)              ((q) << 19)
83c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ_BPDU(q)             ((q) << 22)
84c5aff182SThomas Petazzoni #define      MVNETA_RX_CSUM_WITH_PSEUDO_HDR     BIT(25)
85c5aff182SThomas Petazzoni #define      MVNETA_PORT_CONFIG_DEFL_VALUE(q)   (MVNETA_DEF_RXQ(q)       | \
86c5aff182SThomas Petazzoni 						 MVNETA_DEF_RXQ_ARP(q)	 | \
87c5aff182SThomas Petazzoni 						 MVNETA_DEF_RXQ_TCP(q)	 | \
88c5aff182SThomas Petazzoni 						 MVNETA_DEF_RXQ_UDP(q)	 | \
89c5aff182SThomas Petazzoni 						 MVNETA_DEF_RXQ_BPDU(q)	 | \
90c5aff182SThomas Petazzoni 						 MVNETA_TX_UNSET_ERR_SUM | \
91c5aff182SThomas Petazzoni 						 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92c5aff182SThomas Petazzoni #define MVNETA_PORT_CONFIG_EXTEND                0x2404
93c5aff182SThomas Petazzoni #define MVNETA_MAC_ADDR_LOW                      0x2414
94c5aff182SThomas Petazzoni #define MVNETA_MAC_ADDR_HIGH                     0x2418
95c5aff182SThomas Petazzoni #define MVNETA_SDMA_CONFIG                       0x241c
96c5aff182SThomas Petazzoni #define      MVNETA_SDMA_BRST_SIZE_16            4
97c5aff182SThomas Petazzoni #define      MVNETA_RX_BRST_SZ_MASK(burst)       ((burst) << 1)
98c5aff182SThomas Petazzoni #define      MVNETA_RX_NO_DATA_SWAP              BIT(4)
99c5aff182SThomas Petazzoni #define      MVNETA_TX_NO_DATA_SWAP              BIT(5)
1009ad8fef6SThomas Petazzoni #define      MVNETA_DESC_SWAP                    BIT(6)
101c5aff182SThomas Petazzoni #define      MVNETA_TX_BRST_SZ_MASK(burst)       ((burst) << 22)
102c5aff182SThomas Petazzoni #define MVNETA_PORT_STATUS                       0x2444
103c5aff182SThomas Petazzoni #define      MVNETA_TX_IN_PRGRS                  BIT(1)
104c5aff182SThomas Petazzoni #define      MVNETA_TX_FIFO_EMPTY                BIT(8)
105c5aff182SThomas Petazzoni #define MVNETA_RX_MIN_FRAME_SIZE                 0x247c
1063f1dd4bcSThomas Petazzoni #define MVNETA_SERDES_CFG			 0x24A0
1075445eaf3SArnaud Patard \(Rtp\) #define      MVNETA_SGMII_SERDES_PROTO		 0x0cc7
1083f1dd4bcSThomas Petazzoni #define      MVNETA_QSGMII_SERDES_PROTO		 0x0667
109c5aff182SThomas Petazzoni #define MVNETA_TYPE_PRIO                         0x24bc
110c5aff182SThomas Petazzoni #define      MVNETA_FORCE_UNI                    BIT(21)
111c5aff182SThomas Petazzoni #define MVNETA_TXQ_CMD_1                         0x24e4
112c5aff182SThomas Petazzoni #define MVNETA_TXQ_CMD                           0x2448
113c5aff182SThomas Petazzoni #define      MVNETA_TXQ_DISABLE_SHIFT            8
114c5aff182SThomas Petazzoni #define      MVNETA_TXQ_ENABLE_MASK              0x000000ff
115e483911fSAndrew Lunn #define MVNETA_RX_DISCARD_FRAME_COUNT		 0x2484
116e483911fSAndrew Lunn #define MVNETA_OVERRUN_FRAME_COUNT		 0x2488
117898b2970SStas Sergeev #define MVNETA_GMAC_CLOCK_DIVIDER                0x24f4
118898b2970SStas Sergeev #define      MVNETA_GMAC_1MS_CLOCK_ENABLE        BIT(31)
119c5aff182SThomas Petazzoni #define MVNETA_ACC_MODE                          0x2500
120dc35a10fSMarcin Wojtas #define MVNETA_BM_ADDRESS                        0x2504
121c5aff182SThomas Petazzoni #define MVNETA_CPU_MAP(cpu)                      (0x2540 + ((cpu) << 2))
122c5aff182SThomas Petazzoni #define      MVNETA_CPU_RXQ_ACCESS_ALL_MASK      0x000000ff
123c5aff182SThomas Petazzoni #define      MVNETA_CPU_TXQ_ACCESS_ALL_MASK      0x0000ff00
1242dcf75e2SGregory CLEMENT #define      MVNETA_CPU_RXQ_ACCESS(rxq)		 BIT(rxq)
12550bf8cb6SGregory CLEMENT #define      MVNETA_CPU_TXQ_ACCESS(txq)		 BIT(txq + 8)
126c5aff182SThomas Petazzoni #define MVNETA_RXQ_TIME_COAL_REG(q)              (0x2580 + ((q) << 2))
12740ba35e7Swilly tarreau 
1282dcf75e2SGregory CLEMENT /* Exception Interrupt Port/Queue Cause register
1292dcf75e2SGregory CLEMENT  *
1302dcf75e2SGregory CLEMENT  * Their behavior depend of the mapping done using the PCPX2Q
1312dcf75e2SGregory CLEMENT  * registers. For a given CPU if the bit associated to a queue is not
1322dcf75e2SGregory CLEMENT  * set, then for the register a read from this CPU will always return
1332dcf75e2SGregory CLEMENT  * 0 and a write won't do anything
1342dcf75e2SGregory CLEMENT  */
13540ba35e7Swilly tarreau 
136c5aff182SThomas Petazzoni #define MVNETA_INTR_NEW_CAUSE                    0x25a0
137c5aff182SThomas Petazzoni #define MVNETA_INTR_NEW_MASK                     0x25a4
13840ba35e7Swilly tarreau 
13940ba35e7Swilly tarreau /* bits  0..7  = TXQ SENT, one bit per queue.
14040ba35e7Swilly tarreau  * bits  8..15 = RXQ OCCUP, one bit per queue.
14140ba35e7Swilly tarreau  * bits 16..23 = RXQ FREE, one bit per queue.
14240ba35e7Swilly tarreau  * bit  29 = OLD_REG_SUM, see old reg ?
14340ba35e7Swilly tarreau  * bit  30 = TX_ERR_SUM, one bit for 4 ports
14440ba35e7Swilly tarreau  * bit  31 = MISC_SUM,   one bit for 4 ports
14540ba35e7Swilly tarreau  */
14640ba35e7Swilly tarreau #define      MVNETA_TX_INTR_MASK(nr_txqs)        (((1 << nr_txqs) - 1) << 0)
14740ba35e7Swilly tarreau #define      MVNETA_TX_INTR_MASK_ALL             (0xff << 0)
14840ba35e7Swilly tarreau #define      MVNETA_RX_INTR_MASK(nr_rxqs)        (((1 << nr_rxqs) - 1) << 8)
14940ba35e7Swilly tarreau #define      MVNETA_RX_INTR_MASK_ALL             (0xff << 8)
150898b2970SStas Sergeev #define      MVNETA_MISCINTR_INTR_MASK           BIT(31)
15140ba35e7Swilly tarreau 
152c5aff182SThomas Petazzoni #define MVNETA_INTR_OLD_CAUSE                    0x25a8
153c5aff182SThomas Petazzoni #define MVNETA_INTR_OLD_MASK                     0x25ac
15440ba35e7Swilly tarreau 
15540ba35e7Swilly tarreau /* Data Path Port/Queue Cause Register */
156c5aff182SThomas Petazzoni #define MVNETA_INTR_MISC_CAUSE                   0x25b0
157c5aff182SThomas Petazzoni #define MVNETA_INTR_MISC_MASK                    0x25b4
15840ba35e7Swilly tarreau 
15940ba35e7Swilly tarreau #define      MVNETA_CAUSE_PHY_STATUS_CHANGE      BIT(0)
16040ba35e7Swilly tarreau #define      MVNETA_CAUSE_LINK_CHANGE            BIT(1)
16140ba35e7Swilly tarreau #define      MVNETA_CAUSE_PTP                    BIT(4)
16240ba35e7Swilly tarreau 
16340ba35e7Swilly tarreau #define      MVNETA_CAUSE_INTERNAL_ADDR_ERR      BIT(7)
16440ba35e7Swilly tarreau #define      MVNETA_CAUSE_RX_OVERRUN             BIT(8)
16540ba35e7Swilly tarreau #define      MVNETA_CAUSE_RX_CRC_ERROR           BIT(9)
16640ba35e7Swilly tarreau #define      MVNETA_CAUSE_RX_LARGE_PKT           BIT(10)
16740ba35e7Swilly tarreau #define      MVNETA_CAUSE_TX_UNDERUN             BIT(11)
16840ba35e7Swilly tarreau #define      MVNETA_CAUSE_PRBS_ERR               BIT(12)
16940ba35e7Swilly tarreau #define      MVNETA_CAUSE_PSC_SYNC_CHANGE        BIT(13)
17040ba35e7Swilly tarreau #define      MVNETA_CAUSE_SERDES_SYNC_ERR        BIT(14)
17140ba35e7Swilly tarreau 
17240ba35e7Swilly tarreau #define      MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT    16
17340ba35e7Swilly tarreau #define      MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK   (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
17440ba35e7Swilly tarreau #define      MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
17540ba35e7Swilly tarreau 
17640ba35e7Swilly tarreau #define      MVNETA_CAUSE_TXQ_ERROR_SHIFT        24
17740ba35e7Swilly tarreau #define      MVNETA_CAUSE_TXQ_ERROR_ALL_MASK     (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
17840ba35e7Swilly tarreau #define      MVNETA_CAUSE_TXQ_ERROR_MASK(q)      (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
17940ba35e7Swilly tarreau 
180c5aff182SThomas Petazzoni #define MVNETA_INTR_ENABLE                       0x25b8
181c5aff182SThomas Petazzoni #define      MVNETA_TXQ_INTR_ENABLE_ALL_MASK     0x0000ff00
182dc1aadf6SMarcin Wojtas #define      MVNETA_RXQ_INTR_ENABLE_ALL_MASK     0x000000ff
18340ba35e7Swilly tarreau 
184c5aff182SThomas Petazzoni #define MVNETA_RXQ_CMD                           0x2680
185c5aff182SThomas Petazzoni #define      MVNETA_RXQ_DISABLE_SHIFT            8
186c5aff182SThomas Petazzoni #define      MVNETA_RXQ_ENABLE_MASK              0x000000ff
187c5aff182SThomas Petazzoni #define MVETH_TXQ_TOKEN_COUNT_REG(q)             (0x2700 + ((q) << 4))
188c5aff182SThomas Petazzoni #define MVETH_TXQ_TOKEN_CFG_REG(q)               (0x2704 + ((q) << 4))
189c5aff182SThomas Petazzoni #define MVNETA_GMAC_CTRL_0                       0x2c00
190c5aff182SThomas Petazzoni #define      MVNETA_GMAC_MAX_RX_SIZE_SHIFT       2
191c5aff182SThomas Petazzoni #define      MVNETA_GMAC_MAX_RX_SIZE_MASK        0x7ffc
19222f4bf8aSRussell King #define      MVNETA_GMAC0_PORT_1000BASE_X        BIT(1)
193c5aff182SThomas Petazzoni #define      MVNETA_GMAC0_PORT_ENABLE            BIT(0)
194c5aff182SThomas Petazzoni #define MVNETA_GMAC_CTRL_2                       0x2c08
195898b2970SStas Sergeev #define      MVNETA_GMAC2_INBAND_AN_ENABLE       BIT(0)
196a79121d3SThomas Petazzoni #define      MVNETA_GMAC2_PCS_ENABLE             BIT(3)
197c5aff182SThomas Petazzoni #define      MVNETA_GMAC2_PORT_RGMII             BIT(4)
198c5aff182SThomas Petazzoni #define      MVNETA_GMAC2_PORT_RESET             BIT(6)
199c5aff182SThomas Petazzoni #define MVNETA_GMAC_STATUS                       0x2c10
200c5aff182SThomas Petazzoni #define      MVNETA_GMAC_LINK_UP                 BIT(0)
201c5aff182SThomas Petazzoni #define      MVNETA_GMAC_SPEED_1000              BIT(1)
202c5aff182SThomas Petazzoni #define      MVNETA_GMAC_SPEED_100               BIT(2)
203c5aff182SThomas Petazzoni #define      MVNETA_GMAC_FULL_DUPLEX             BIT(3)
204c5aff182SThomas Petazzoni #define      MVNETA_GMAC_RX_FLOW_CTRL_ENABLE     BIT(4)
205c5aff182SThomas Petazzoni #define      MVNETA_GMAC_TX_FLOW_CTRL_ENABLE     BIT(5)
206c5aff182SThomas Petazzoni #define      MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE     BIT(6)
207c5aff182SThomas Petazzoni #define      MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE     BIT(7)
208503f9aa9SRussell King #define      MVNETA_GMAC_AN_COMPLETE             BIT(11)
209503f9aa9SRussell King #define      MVNETA_GMAC_SYNC_OK                 BIT(14)
210c5aff182SThomas Petazzoni #define MVNETA_GMAC_AUTONEG_CONFIG               0x2c0c
211c5aff182SThomas Petazzoni #define      MVNETA_GMAC_FORCE_LINK_DOWN         BIT(0)
212c5aff182SThomas Petazzoni #define      MVNETA_GMAC_FORCE_LINK_PASS         BIT(1)
213898b2970SStas Sergeev #define      MVNETA_GMAC_INBAND_AN_ENABLE        BIT(2)
21422f4bf8aSRussell King #define      MVNETA_GMAC_AN_BYPASS_ENABLE        BIT(3)
21522f4bf8aSRussell King #define      MVNETA_GMAC_INBAND_RESTART_AN       BIT(4)
216c5aff182SThomas Petazzoni #define      MVNETA_GMAC_CONFIG_MII_SPEED        BIT(5)
217c5aff182SThomas Petazzoni #define      MVNETA_GMAC_CONFIG_GMII_SPEED       BIT(6)
21871408602SThomas Petazzoni #define      MVNETA_GMAC_AN_SPEED_EN             BIT(7)
21922f4bf8aSRussell King #define      MVNETA_GMAC_CONFIG_FLOW_CTRL        BIT(8)
22022f4bf8aSRussell King #define      MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL    BIT(9)
221898b2970SStas Sergeev #define      MVNETA_GMAC_AN_FLOW_CTRL_EN         BIT(11)
222c5aff182SThomas Petazzoni #define      MVNETA_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
22371408602SThomas Petazzoni #define      MVNETA_GMAC_AN_DUPLEX_EN            BIT(13)
224e483911fSAndrew Lunn #define MVNETA_MIB_COUNTERS_BASE                 0x3000
225c5aff182SThomas Petazzoni #define      MVNETA_MIB_LATE_COLLISION           0x7c
226c5aff182SThomas Petazzoni #define MVNETA_DA_FILT_SPEC_MCAST                0x3400
227c5aff182SThomas Petazzoni #define MVNETA_DA_FILT_OTH_MCAST                 0x3500
228c5aff182SThomas Petazzoni #define MVNETA_DA_FILT_UCAST_BASE                0x3600
229c5aff182SThomas Petazzoni #define MVNETA_TXQ_BASE_ADDR_REG(q)              (0x3c00 + ((q) << 2))
230c5aff182SThomas Petazzoni #define MVNETA_TXQ_SIZE_REG(q)                   (0x3c20 + ((q) << 2))
231c5aff182SThomas Petazzoni #define      MVNETA_TXQ_SENT_THRESH_ALL_MASK     0x3fff0000
232c5aff182SThomas Petazzoni #define      MVNETA_TXQ_SENT_THRESH_MASK(coal)   ((coal) << 16)
233c5aff182SThomas Petazzoni #define MVNETA_TXQ_UPDATE_REG(q)                 (0x3c60 + ((q) << 2))
234c5aff182SThomas Petazzoni #define      MVNETA_TXQ_DEC_SENT_SHIFT           16
2352a90f7e1SSimon Guinot #define      MVNETA_TXQ_DEC_SENT_MASK            0xff
236c5aff182SThomas Petazzoni #define MVNETA_TXQ_STATUS_REG(q)                 (0x3c40 + ((q) << 2))
237c5aff182SThomas Petazzoni #define      MVNETA_TXQ_SENT_DESC_SHIFT          16
238c5aff182SThomas Petazzoni #define      MVNETA_TXQ_SENT_DESC_MASK           0x3fff0000
239c5aff182SThomas Petazzoni #define MVNETA_PORT_TX_RESET                     0x3cf0
240c5aff182SThomas Petazzoni #define      MVNETA_PORT_TX_DMA_RESET            BIT(0)
241c5aff182SThomas Petazzoni #define MVNETA_TX_MTU                            0x3e0c
242c5aff182SThomas Petazzoni #define MVNETA_TX_TOKEN_SIZE                     0x3e14
243c5aff182SThomas Petazzoni #define      MVNETA_TX_TOKEN_SIZE_MAX            0xffffffff
244c5aff182SThomas Petazzoni #define MVNETA_TXQ_TOKEN_SIZE_REG(q)             (0x3e40 + ((q) << 2))
245c5aff182SThomas Petazzoni #define      MVNETA_TXQ_TOKEN_SIZE_MAX           0x7fffffff
246c5aff182SThomas Petazzoni 
2476d81f451SRussell King #define MVNETA_LPI_CTRL_0                        0x2cc0
2486d81f451SRussell King #define MVNETA_LPI_CTRL_1                        0x2cc4
2496d81f451SRussell King #define      MVNETA_LPI_REQUEST_ENABLE           BIT(0)
2506d81f451SRussell King #define MVNETA_LPI_CTRL_2                        0x2cc8
2516d81f451SRussell King #define MVNETA_LPI_STATUS                        0x2ccc
2526d81f451SRussell King 
253c5aff182SThomas Petazzoni #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK	 0xff
254c5aff182SThomas Petazzoni 
255c5aff182SThomas Petazzoni /* Descriptor ring Macros */
256c5aff182SThomas Petazzoni #define MVNETA_QUEUE_NEXT_DESC(q, index)	\
257c5aff182SThomas Petazzoni 	(((index) < (q)->last_desc) ? ((index) + 1) : 0)
258c5aff182SThomas Petazzoni 
259c5aff182SThomas Petazzoni /* Various constants */
260c5aff182SThomas Petazzoni 
261c5aff182SThomas Petazzoni /* Coalescing */
26206708f81SDmitri Epshtein #define MVNETA_TXDONE_COAL_PKTS		0	/* interrupt per packet */
263c5aff182SThomas Petazzoni #define MVNETA_RX_COAL_PKTS		32
264c5aff182SThomas Petazzoni #define MVNETA_RX_COAL_USEC		100
265c5aff182SThomas Petazzoni 
2666a20c175SThomas Petazzoni /* The two bytes Marvell header. Either contains a special value used
267c5aff182SThomas Petazzoni  * by Marvell switches when a specific hardware mode is enabled (not
268c5aff182SThomas Petazzoni  * supported by this driver) or is filled automatically by zeroes on
269c5aff182SThomas Petazzoni  * the RX side. Those two bytes being at the front of the Ethernet
270c5aff182SThomas Petazzoni  * header, they allow to have the IP header aligned on a 4 bytes
271c5aff182SThomas Petazzoni  * boundary automatically: the hardware skips those two bytes on its
272c5aff182SThomas Petazzoni  * own.
273c5aff182SThomas Petazzoni  */
274c5aff182SThomas Petazzoni #define MVNETA_MH_SIZE			2
275c5aff182SThomas Petazzoni 
276c5aff182SThomas Petazzoni #define MVNETA_VLAN_TAG_LEN             4
277c5aff182SThomas Petazzoni 
2789110ee07SMarcin Wojtas #define MVNETA_TX_CSUM_DEF_SIZE		1600
279c5aff182SThomas Petazzoni #define MVNETA_TX_CSUM_MAX_SIZE		9800
280dc35a10fSMarcin Wojtas #define MVNETA_ACC_MODE_EXT1		1
281dc35a10fSMarcin Wojtas #define MVNETA_ACC_MODE_EXT2		2
282dc35a10fSMarcin Wojtas 
283dc35a10fSMarcin Wojtas #define MVNETA_MAX_DECODE_WIN		6
284c5aff182SThomas Petazzoni 
285c5aff182SThomas Petazzoni /* Timeout constants */
286c5aff182SThomas Petazzoni #define MVNETA_TX_DISABLE_TIMEOUT_MSEC	1000
287c5aff182SThomas Petazzoni #define MVNETA_RX_DISABLE_TIMEOUT_MSEC	1000
288c5aff182SThomas Petazzoni #define MVNETA_TX_FIFO_EMPTY_TIMEOUT	10000
289c5aff182SThomas Petazzoni 
290c5aff182SThomas Petazzoni #define MVNETA_TX_MTU_MAX		0x3ffff
291c5aff182SThomas Petazzoni 
2929a401deaSGregory CLEMENT /* The RSS lookup table actually has 256 entries but we do not use
2939a401deaSGregory CLEMENT  * them yet
2949a401deaSGregory CLEMENT  */
2959a401deaSGregory CLEMENT #define MVNETA_RSS_LU_TABLE_SIZE	1
2969a401deaSGregory CLEMENT 
297c5aff182SThomas Petazzoni /* Max number of Rx descriptors */
298c5aff182SThomas Petazzoni #define MVNETA_MAX_RXD 128
299c5aff182SThomas Petazzoni 
300c5aff182SThomas Petazzoni /* Max number of Tx descriptors */
301c5aff182SThomas Petazzoni #define MVNETA_MAX_TXD 532
302c5aff182SThomas Petazzoni 
3038eef5f97SEzequiel Garcia /* Max number of allowed TCP segments for software TSO */
3048eef5f97SEzequiel Garcia #define MVNETA_MAX_TSO_SEGS 100
3058eef5f97SEzequiel Garcia 
3068eef5f97SEzequiel Garcia #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
3078eef5f97SEzequiel Garcia 
308c5aff182SThomas Petazzoni /* descriptor aligned size */
309c5aff182SThomas Petazzoni #define MVNETA_DESC_ALIGNED_SIZE	32
310c5aff182SThomas Petazzoni 
3118d5047cfSMarcin Wojtas /* Number of bytes to be taken into account by HW when putting incoming data
3128d5047cfSMarcin Wojtas  * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
3138d5047cfSMarcin Wojtas  * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
3148d5047cfSMarcin Wojtas  */
3158d5047cfSMarcin Wojtas #define MVNETA_RX_PKT_OFFSET_CORRECTION		64
3168d5047cfSMarcin Wojtas 
317c5aff182SThomas Petazzoni #define MVNETA_RX_PKT_SIZE(mtu) \
318c5aff182SThomas Petazzoni 	ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
319c5aff182SThomas Petazzoni 	      ETH_HLEN + ETH_FCS_LEN,			     \
320c66e98c9SJisheng Zhang 	      cache_line_size())
321c5aff182SThomas Petazzoni 
3222e3173a3SEzequiel Garcia #define IS_TSO_HEADER(txq, addr) \
3232e3173a3SEzequiel Garcia 	((addr >= txq->tso_hdrs_phys) && \
3242e3173a3SEzequiel Garcia 	 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
3252e3173a3SEzequiel Garcia 
326dc35a10fSMarcin Wojtas #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
327dc35a10fSMarcin Wojtas 	(((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
328c5aff182SThomas Petazzoni 
3296d81f451SRussell King enum {
3306d81f451SRussell King 	ETHTOOL_STAT_EEE_WAKEUP,
3316d81f451SRussell King 	ETHTOOL_MAX_STATS,
3326d81f451SRussell King };
3336d81f451SRussell King 
3349b0cdefaSRussell King struct mvneta_statistic {
3359b0cdefaSRussell King 	unsigned short offset;
3369b0cdefaSRussell King 	unsigned short type;
3379b0cdefaSRussell King 	const char name[ETH_GSTRING_LEN];
3389b0cdefaSRussell King };
3399b0cdefaSRussell King 
3409b0cdefaSRussell King #define T_REG_32	32
3419b0cdefaSRussell King #define T_REG_64	64
3426d81f451SRussell King #define T_SW		1
3439b0cdefaSRussell King 
3449b0cdefaSRussell King static const struct mvneta_statistic mvneta_statistics[] = {
3459b0cdefaSRussell King 	{ 0x3000, T_REG_64, "good_octets_received", },
3469b0cdefaSRussell King 	{ 0x3010, T_REG_32, "good_frames_received", },
3479b0cdefaSRussell King 	{ 0x3008, T_REG_32, "bad_octets_received", },
3489b0cdefaSRussell King 	{ 0x3014, T_REG_32, "bad_frames_received", },
3499b0cdefaSRussell King 	{ 0x3018, T_REG_32, "broadcast_frames_received", },
3509b0cdefaSRussell King 	{ 0x301c, T_REG_32, "multicast_frames_received", },
3519b0cdefaSRussell King 	{ 0x3050, T_REG_32, "unrec_mac_control_received", },
3529b0cdefaSRussell King 	{ 0x3058, T_REG_32, "good_fc_received", },
3539b0cdefaSRussell King 	{ 0x305c, T_REG_32, "bad_fc_received", },
3549b0cdefaSRussell King 	{ 0x3060, T_REG_32, "undersize_received", },
3559b0cdefaSRussell King 	{ 0x3064, T_REG_32, "fragments_received", },
3569b0cdefaSRussell King 	{ 0x3068, T_REG_32, "oversize_received", },
3579b0cdefaSRussell King 	{ 0x306c, T_REG_32, "jabber_received", },
3589b0cdefaSRussell King 	{ 0x3070, T_REG_32, "mac_receive_error", },
3599b0cdefaSRussell King 	{ 0x3074, T_REG_32, "bad_crc_event", },
3609b0cdefaSRussell King 	{ 0x3078, T_REG_32, "collision", },
3619b0cdefaSRussell King 	{ 0x307c, T_REG_32, "late_collision", },
3629b0cdefaSRussell King 	{ 0x2484, T_REG_32, "rx_discard", },
3639b0cdefaSRussell King 	{ 0x2488, T_REG_32, "rx_overrun", },
3649b0cdefaSRussell King 	{ 0x3020, T_REG_32, "frames_64_octets", },
3659b0cdefaSRussell King 	{ 0x3024, T_REG_32, "frames_65_to_127_octets", },
3669b0cdefaSRussell King 	{ 0x3028, T_REG_32, "frames_128_to_255_octets", },
3679b0cdefaSRussell King 	{ 0x302c, T_REG_32, "frames_256_to_511_octets", },
3689b0cdefaSRussell King 	{ 0x3030, T_REG_32, "frames_512_to_1023_octets", },
3699b0cdefaSRussell King 	{ 0x3034, T_REG_32, "frames_1024_to_max_octets", },
3709b0cdefaSRussell King 	{ 0x3038, T_REG_64, "good_octets_sent", },
3719b0cdefaSRussell King 	{ 0x3040, T_REG_32, "good_frames_sent", },
3729b0cdefaSRussell King 	{ 0x3044, T_REG_32, "excessive_collision", },
3739b0cdefaSRussell King 	{ 0x3048, T_REG_32, "multicast_frames_sent", },
3749b0cdefaSRussell King 	{ 0x304c, T_REG_32, "broadcast_frames_sent", },
3759b0cdefaSRussell King 	{ 0x3054, T_REG_32, "fc_sent", },
3769b0cdefaSRussell King 	{ 0x300c, T_REG_32, "internal_mac_transmit_err", },
3776d81f451SRussell King 	{ ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
3789b0cdefaSRussell King };
3799b0cdefaSRussell King 
38074c41b04Swilly tarreau struct mvneta_pcpu_stats {
381c5aff182SThomas Petazzoni 	struct	u64_stats_sync syncp;
38274c41b04Swilly tarreau 	u64	rx_packets;
38374c41b04Swilly tarreau 	u64	rx_bytes;
38474c41b04Swilly tarreau 	u64	tx_packets;
38574c41b04Swilly tarreau 	u64	tx_bytes;
386c5aff182SThomas Petazzoni };
387c5aff182SThomas Petazzoni 
38812bb03b4SMaxime Ripard struct mvneta_pcpu_port {
38912bb03b4SMaxime Ripard 	/* Pointer to the shared port */
39012bb03b4SMaxime Ripard 	struct mvneta_port	*pp;
39112bb03b4SMaxime Ripard 
39212bb03b4SMaxime Ripard 	/* Pointer to the CPU-local NAPI struct */
39312bb03b4SMaxime Ripard 	struct napi_struct	napi;
39412bb03b4SMaxime Ripard 
39512bb03b4SMaxime Ripard 	/* Cause of the previous interrupt */
39612bb03b4SMaxime Ripard 	u32			cause_rx_tx;
39712bb03b4SMaxime Ripard };
39812bb03b4SMaxime Ripard 
399c5aff182SThomas Petazzoni struct mvneta_port {
400dc35a10fSMarcin Wojtas 	u8 id;
40112bb03b4SMaxime Ripard 	struct mvneta_pcpu_port __percpu	*ports;
40212bb03b4SMaxime Ripard 	struct mvneta_pcpu_stats __percpu	*stats;
40312bb03b4SMaxime Ripard 
404c5aff182SThomas Petazzoni 	int pkt_size;
4058ec2cd48Swilly tarreau 	unsigned int frag_size;
406c5aff182SThomas Petazzoni 	void __iomem *base;
407c5aff182SThomas Petazzoni 	struct mvneta_rx_queue *rxqs;
408c5aff182SThomas Petazzoni 	struct mvneta_tx_queue *txqs;
409c5aff182SThomas Petazzoni 	struct net_device *dev;
41084a3f4dbSSebastian Andrzej Siewior 	struct hlist_node node_online;
41184a3f4dbSSebastian Andrzej Siewior 	struct hlist_node node_dead;
41290b74c01SGregory CLEMENT 	int rxq_def;
4135888511eSGregory CLEMENT 	/* Protect the access to the percpu interrupt registers,
4145888511eSGregory CLEMENT 	 * ensuring that the configuration remains coherent.
4155888511eSGregory CLEMENT 	 */
4165888511eSGregory CLEMENT 	spinlock_t lock;
417120cfa50SGregory CLEMENT 	bool is_stopped;
418c5aff182SThomas Petazzoni 
4192636ac3cSMarcin Wojtas 	u32 cause_rx_tx;
4202636ac3cSMarcin Wojtas 	struct napi_struct napi;
4212636ac3cSMarcin Wojtas 
422c5aff182SThomas Petazzoni 	/* Core clock */
423189dd626SThomas Petazzoni 	struct clk *clk;
42415cc4a4aSJisheng Zhang 	/* AXI clock */
42515cc4a4aSJisheng Zhang 	struct clk *clk_bus;
426c5aff182SThomas Petazzoni 	u8 mcast_count[256];
427c5aff182SThomas Petazzoni 	u16 tx_ring_size;
428c5aff182SThomas Petazzoni 	u16 rx_ring_size;
429c5aff182SThomas Petazzoni 
430c5aff182SThomas Petazzoni 	phy_interface_t phy_interface;
431503f9aa9SRussell King 	struct device_node *dn;
432b65657fcSSimon Guinot 	unsigned int tx_csum_limit;
433503f9aa9SRussell King 	struct phylink *phylink;
4349b0cdefaSRussell King 
435dc35a10fSMarcin Wojtas 	struct mvneta_bm *bm_priv;
436dc35a10fSMarcin Wojtas 	struct mvneta_bm_pool *pool_long;
437dc35a10fSMarcin Wojtas 	struct mvneta_bm_pool *pool_short;
438dc35a10fSMarcin Wojtas 	int bm_win_id;
439dc35a10fSMarcin Wojtas 
4406d81f451SRussell King 	bool eee_enabled;
4416d81f451SRussell King 	bool eee_active;
4426d81f451SRussell King 	bool tx_lpi_enabled;
4436d81f451SRussell King 
4449b0cdefaSRussell King 	u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
4459a401deaSGregory CLEMENT 
4469a401deaSGregory CLEMENT 	u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
4472636ac3cSMarcin Wojtas 
4482636ac3cSMarcin Wojtas 	/* Flags for special SoC configurations */
4492636ac3cSMarcin Wojtas 	bool neta_armada3700;
4508d5047cfSMarcin Wojtas 	u16 rx_offset_correction;
4519768b45cSJane Li 	const struct mbus_dram_target_info *dram_target_info;
452c5aff182SThomas Petazzoni };
453c5aff182SThomas Petazzoni 
4546a20c175SThomas Petazzoni /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
455c5aff182SThomas Petazzoni  * layout of the transmit and reception DMA descriptors, and their
456c5aff182SThomas Petazzoni  * layout is therefore defined by the hardware design
457c5aff182SThomas Petazzoni  */
4586083ed44SThomas Petazzoni 
459c5aff182SThomas Petazzoni #define MVNETA_TX_L3_OFF_SHIFT	0
460c5aff182SThomas Petazzoni #define MVNETA_TX_IP_HLEN_SHIFT	8
461c5aff182SThomas Petazzoni #define MVNETA_TX_L4_UDP	BIT(16)
462c5aff182SThomas Petazzoni #define MVNETA_TX_L3_IP6	BIT(17)
463c5aff182SThomas Petazzoni #define MVNETA_TXD_IP_CSUM	BIT(18)
464c5aff182SThomas Petazzoni #define MVNETA_TXD_Z_PAD	BIT(19)
465c5aff182SThomas Petazzoni #define MVNETA_TXD_L_DESC	BIT(20)
466c5aff182SThomas Petazzoni #define MVNETA_TXD_F_DESC	BIT(21)
467c5aff182SThomas Petazzoni #define MVNETA_TXD_FLZ_DESC	(MVNETA_TXD_Z_PAD  | \
468c5aff182SThomas Petazzoni 				 MVNETA_TXD_L_DESC | \
469c5aff182SThomas Petazzoni 				 MVNETA_TXD_F_DESC)
470c5aff182SThomas Petazzoni #define MVNETA_TX_L4_CSUM_FULL	BIT(30)
471c5aff182SThomas Petazzoni #define MVNETA_TX_L4_CSUM_NOT	BIT(31)
472c5aff182SThomas Petazzoni 
473c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_CRC		0x0
474dc35a10fSMarcin Wojtas #define MVNETA_RXD_BM_POOL_SHIFT	13
475dc35a10fSMarcin Wojtas #define MVNETA_RXD_BM_POOL_MASK		(BIT(13) | BIT(14))
476c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_SUMMARY		BIT(16)
477c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_OVERRUN		BIT(17)
478c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_LEN		BIT(18)
479c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_RESOURCE		(BIT(17) | BIT(18))
480c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_CODE_MASK	(BIT(17) | BIT(18))
481c5aff182SThomas Petazzoni #define MVNETA_RXD_L3_IP4		BIT(25)
482c5aff182SThomas Petazzoni #define MVNETA_RXD_FIRST_LAST_DESC	(BIT(26) | BIT(27))
483c5aff182SThomas Petazzoni #define MVNETA_RXD_L4_CSUM_OK		BIT(30)
484c5aff182SThomas Petazzoni 
4859ad8fef6SThomas Petazzoni #if defined(__LITTLE_ENDIAN)
4866083ed44SThomas Petazzoni struct mvneta_tx_desc {
4876083ed44SThomas Petazzoni 	u32  command;		/* Options used by HW for packet transmitting.*/
4886083ed44SThomas Petazzoni 	u16  reserverd1;	/* csum_l4 (for future use)		*/
4896083ed44SThomas Petazzoni 	u16  data_size;		/* Data size of transmitted packet in bytes */
4906083ed44SThomas Petazzoni 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
4916083ed44SThomas Petazzoni 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
4926083ed44SThomas Petazzoni 	u32  reserved3[4];	/* Reserved - (for future use)		*/
4936083ed44SThomas Petazzoni };
4946083ed44SThomas Petazzoni 
4956083ed44SThomas Petazzoni struct mvneta_rx_desc {
4966083ed44SThomas Petazzoni 	u32  status;		/* Info about received packet		*/
497c5aff182SThomas Petazzoni 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
498c5aff182SThomas Petazzoni 	u16  data_size;		/* Size of received packet in bytes	*/
4996083ed44SThomas Petazzoni 
500c5aff182SThomas Petazzoni 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
501c5aff182SThomas Petazzoni 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
5026083ed44SThomas Petazzoni 
503c5aff182SThomas Petazzoni 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
504c5aff182SThomas Petazzoni 	u16  reserved3;		/* prefetch_cmd, for future use		*/
505c5aff182SThomas Petazzoni 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
5066083ed44SThomas Petazzoni 
507c5aff182SThomas Petazzoni 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
508c5aff182SThomas Petazzoni 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
509c5aff182SThomas Petazzoni };
5109ad8fef6SThomas Petazzoni #else
5119ad8fef6SThomas Petazzoni struct mvneta_tx_desc {
5129ad8fef6SThomas Petazzoni 	u16  data_size;		/* Data size of transmitted packet in bytes */
5139ad8fef6SThomas Petazzoni 	u16  reserverd1;	/* csum_l4 (for future use)		*/
5149ad8fef6SThomas Petazzoni 	u32  command;		/* Options used by HW for packet transmitting.*/
5159ad8fef6SThomas Petazzoni 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
5169ad8fef6SThomas Petazzoni 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
5179ad8fef6SThomas Petazzoni 	u32  reserved3[4];	/* Reserved - (for future use)		*/
5189ad8fef6SThomas Petazzoni };
5199ad8fef6SThomas Petazzoni 
5209ad8fef6SThomas Petazzoni struct mvneta_rx_desc {
5219ad8fef6SThomas Petazzoni 	u16  data_size;		/* Size of received packet in bytes	*/
5229ad8fef6SThomas Petazzoni 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
5239ad8fef6SThomas Petazzoni 	u32  status;		/* Info about received packet		*/
5249ad8fef6SThomas Petazzoni 
5259ad8fef6SThomas Petazzoni 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
5269ad8fef6SThomas Petazzoni 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
5279ad8fef6SThomas Petazzoni 
5289ad8fef6SThomas Petazzoni 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
5299ad8fef6SThomas Petazzoni 	u16  reserved3;		/* prefetch_cmd, for future use		*/
5309ad8fef6SThomas Petazzoni 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
5319ad8fef6SThomas Petazzoni 
5329ad8fef6SThomas Petazzoni 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
5339ad8fef6SThomas Petazzoni 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
5349ad8fef6SThomas Petazzoni };
5359ad8fef6SThomas Petazzoni #endif
536c5aff182SThomas Petazzoni 
537c5aff182SThomas Petazzoni struct mvneta_tx_queue {
538c5aff182SThomas Petazzoni 	/* Number of this TX queue, in the range 0-7 */
539c5aff182SThomas Petazzoni 	u8 id;
540c5aff182SThomas Petazzoni 
541c5aff182SThomas Petazzoni 	/* Number of TX DMA descriptors in the descriptor ring */
542c5aff182SThomas Petazzoni 	int size;
543c5aff182SThomas Petazzoni 
544c5aff182SThomas Petazzoni 	/* Number of currently used TX DMA descriptor in the
5456a20c175SThomas Petazzoni 	 * descriptor ring
5466a20c175SThomas Petazzoni 	 */
547c5aff182SThomas Petazzoni 	int count;
5482a90f7e1SSimon Guinot 	int pending;
5498eef5f97SEzequiel Garcia 	int tx_stop_threshold;
5508eef5f97SEzequiel Garcia 	int tx_wake_threshold;
551c5aff182SThomas Petazzoni 
552c5aff182SThomas Petazzoni 	/* Array of transmitted skb */
553c5aff182SThomas Petazzoni 	struct sk_buff **tx_skb;
554c5aff182SThomas Petazzoni 
555c5aff182SThomas Petazzoni 	/* Index of last TX DMA descriptor that was inserted */
556c5aff182SThomas Petazzoni 	int txq_put_index;
557c5aff182SThomas Petazzoni 
558c5aff182SThomas Petazzoni 	/* Index of the TX DMA descriptor to be cleaned up */
559c5aff182SThomas Petazzoni 	int txq_get_index;
560c5aff182SThomas Petazzoni 
561c5aff182SThomas Petazzoni 	u32 done_pkts_coal;
562c5aff182SThomas Petazzoni 
563c5aff182SThomas Petazzoni 	/* Virtual address of the TX DMA descriptors array */
564c5aff182SThomas Petazzoni 	struct mvneta_tx_desc *descs;
565c5aff182SThomas Petazzoni 
566c5aff182SThomas Petazzoni 	/* DMA address of the TX DMA descriptors array */
567c5aff182SThomas Petazzoni 	dma_addr_t descs_phys;
568c5aff182SThomas Petazzoni 
569c5aff182SThomas Petazzoni 	/* Index of the last TX DMA descriptor */
570c5aff182SThomas Petazzoni 	int last_desc;
571c5aff182SThomas Petazzoni 
572c5aff182SThomas Petazzoni 	/* Index of the next TX DMA descriptor to process */
573c5aff182SThomas Petazzoni 	int next_desc_to_proc;
5742adb719dSEzequiel Garcia 
5752adb719dSEzequiel Garcia 	/* DMA buffers for TSO headers */
5762adb719dSEzequiel Garcia 	char *tso_hdrs;
5772adb719dSEzequiel Garcia 
5782adb719dSEzequiel Garcia 	/* DMA address of TSO headers */
5792adb719dSEzequiel Garcia 	dma_addr_t tso_hdrs_phys;
58050bf8cb6SGregory CLEMENT 
58150bf8cb6SGregory CLEMENT 	/* Affinity mask for CPUs*/
58250bf8cb6SGregory CLEMENT 	cpumask_t affinity_mask;
583c5aff182SThomas Petazzoni };
584c5aff182SThomas Petazzoni 
585c5aff182SThomas Petazzoni struct mvneta_rx_queue {
586c5aff182SThomas Petazzoni 	/* rx queue number, in the range 0-7 */
587c5aff182SThomas Petazzoni 	u8 id;
588c5aff182SThomas Petazzoni 
589c5aff182SThomas Petazzoni 	/* num of rx descriptors in the rx descriptor ring */
590c5aff182SThomas Petazzoni 	int size;
591c5aff182SThomas Petazzoni 
592c5aff182SThomas Petazzoni 	/* counter of times when mvneta_refill() failed */
593c5aff182SThomas Petazzoni 	int missed;
594c5aff182SThomas Petazzoni 
595c5aff182SThomas Petazzoni 	u32 pkts_coal;
596c5aff182SThomas Petazzoni 	u32 time_coal;
597c5aff182SThomas Petazzoni 
598f88bee1cSGregory CLEMENT 	/* Virtual address of the RX buffer */
599f88bee1cSGregory CLEMENT 	void  **buf_virt_addr;
600f88bee1cSGregory CLEMENT 
601c5aff182SThomas Petazzoni 	/* Virtual address of the RX DMA descriptors array */
602c5aff182SThomas Petazzoni 	struct mvneta_rx_desc *descs;
603c5aff182SThomas Petazzoni 
604c5aff182SThomas Petazzoni 	/* DMA address of the RX DMA descriptors array */
605c5aff182SThomas Petazzoni 	dma_addr_t descs_phys;
606c5aff182SThomas Petazzoni 
607c5aff182SThomas Petazzoni 	/* Index of the last RX DMA descriptor */
608c5aff182SThomas Petazzoni 	int last_desc;
609c5aff182SThomas Petazzoni 
610c5aff182SThomas Petazzoni 	/* Index of the next RX DMA descriptor to process */
611c5aff182SThomas Petazzoni 	int next_desc_to_proc;
612c5aff182SThomas Petazzoni };
613c5aff182SThomas Petazzoni 
61484a3f4dbSSebastian Andrzej Siewior static enum cpuhp_state online_hpstate;
615edadb7faSEzequiel Garcia /* The hardware supports eight (8) rx queues, but we are only allowing
616edadb7faSEzequiel Garcia  * the first one to be used. Therefore, let's just allocate one queue.
617edadb7faSEzequiel Garcia  */
618d8936657SMaxime Ripard static int rxq_number = 8;
619c5aff182SThomas Petazzoni static int txq_number = 8;
620c5aff182SThomas Petazzoni 
621c5aff182SThomas Petazzoni static int rxq_def;
622c5aff182SThomas Petazzoni 
623f19fadfcSwilly tarreau static int rx_copybreak __read_mostly = 256;
624f19fadfcSwilly tarreau 
625dc35a10fSMarcin Wojtas /* HW BM need that each port be identify by a unique ID */
626dc35a10fSMarcin Wojtas static int global_port_id;
627dc35a10fSMarcin Wojtas 
628c5aff182SThomas Petazzoni #define MVNETA_DRIVER_NAME "mvneta"
629c5aff182SThomas Petazzoni #define MVNETA_DRIVER_VERSION "1.0"
630c5aff182SThomas Petazzoni 
631c5aff182SThomas Petazzoni /* Utility/helper methods */
632c5aff182SThomas Petazzoni 
633c5aff182SThomas Petazzoni /* Write helper method */
634c5aff182SThomas Petazzoni static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
635c5aff182SThomas Petazzoni {
636c5aff182SThomas Petazzoni 	writel(data, pp->base + offset);
637c5aff182SThomas Petazzoni }
638c5aff182SThomas Petazzoni 
639c5aff182SThomas Petazzoni /* Read helper method */
640c5aff182SThomas Petazzoni static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
641c5aff182SThomas Petazzoni {
642c5aff182SThomas Petazzoni 	return readl(pp->base + offset);
643c5aff182SThomas Petazzoni }
644c5aff182SThomas Petazzoni 
645c5aff182SThomas Petazzoni /* Increment txq get counter */
646c5aff182SThomas Petazzoni static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
647c5aff182SThomas Petazzoni {
648c5aff182SThomas Petazzoni 	txq->txq_get_index++;
649c5aff182SThomas Petazzoni 	if (txq->txq_get_index == txq->size)
650c5aff182SThomas Petazzoni 		txq->txq_get_index = 0;
651c5aff182SThomas Petazzoni }
652c5aff182SThomas Petazzoni 
653c5aff182SThomas Petazzoni /* Increment txq put counter */
654c5aff182SThomas Petazzoni static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
655c5aff182SThomas Petazzoni {
656c5aff182SThomas Petazzoni 	txq->txq_put_index++;
657c5aff182SThomas Petazzoni 	if (txq->txq_put_index == txq->size)
658c5aff182SThomas Petazzoni 		txq->txq_put_index = 0;
659c5aff182SThomas Petazzoni }
660c5aff182SThomas Petazzoni 
661c5aff182SThomas Petazzoni 
662c5aff182SThomas Petazzoni /* Clear all MIB counters */
663c5aff182SThomas Petazzoni static void mvneta_mib_counters_clear(struct mvneta_port *pp)
664c5aff182SThomas Petazzoni {
665c5aff182SThomas Petazzoni 	int i;
666c5aff182SThomas Petazzoni 	u32 dummy;
667c5aff182SThomas Petazzoni 
668c5aff182SThomas Petazzoni 	/* Perform dummy reads from MIB counters */
669c5aff182SThomas Petazzoni 	for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
670c5aff182SThomas Petazzoni 		dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
671e483911fSAndrew Lunn 	dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
672e483911fSAndrew Lunn 	dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
673c5aff182SThomas Petazzoni }
674c5aff182SThomas Petazzoni 
675c5aff182SThomas Petazzoni /* Get System Network Statistics */
676bc1f4470Sstephen hemminger static void
6772dc0d2b4SBaoyou Xie mvneta_get_stats64(struct net_device *dev,
678c5aff182SThomas Petazzoni 		   struct rtnl_link_stats64 *stats)
679c5aff182SThomas Petazzoni {
680c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
681c5aff182SThomas Petazzoni 	unsigned int start;
68274c41b04Swilly tarreau 	int cpu;
683c5aff182SThomas Petazzoni 
68474c41b04Swilly tarreau 	for_each_possible_cpu(cpu) {
68574c41b04Swilly tarreau 		struct mvneta_pcpu_stats *cpu_stats;
68674c41b04Swilly tarreau 		u64 rx_packets;
68774c41b04Swilly tarreau 		u64 rx_bytes;
68874c41b04Swilly tarreau 		u64 tx_packets;
68974c41b04Swilly tarreau 		u64 tx_bytes;
690c5aff182SThomas Petazzoni 
69174c41b04Swilly tarreau 		cpu_stats = per_cpu_ptr(pp->stats, cpu);
692c5aff182SThomas Petazzoni 		do {
69357a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
69474c41b04Swilly tarreau 			rx_packets = cpu_stats->rx_packets;
69574c41b04Swilly tarreau 			rx_bytes   = cpu_stats->rx_bytes;
69674c41b04Swilly tarreau 			tx_packets = cpu_stats->tx_packets;
69774c41b04Swilly tarreau 			tx_bytes   = cpu_stats->tx_bytes;
69857a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
699c5aff182SThomas Petazzoni 
70074c41b04Swilly tarreau 		stats->rx_packets += rx_packets;
70174c41b04Swilly tarreau 		stats->rx_bytes   += rx_bytes;
70274c41b04Swilly tarreau 		stats->tx_packets += tx_packets;
70374c41b04Swilly tarreau 		stats->tx_bytes   += tx_bytes;
70474c41b04Swilly tarreau 	}
705c5aff182SThomas Petazzoni 
706c5aff182SThomas Petazzoni 	stats->rx_errors	= dev->stats.rx_errors;
707c5aff182SThomas Petazzoni 	stats->rx_dropped	= dev->stats.rx_dropped;
708c5aff182SThomas Petazzoni 
709c5aff182SThomas Petazzoni 	stats->tx_dropped	= dev->stats.tx_dropped;
710c5aff182SThomas Petazzoni }
711c5aff182SThomas Petazzoni 
712c5aff182SThomas Petazzoni /* Rx descriptors helper methods */
713c5aff182SThomas Petazzoni 
7145428213cSwilly tarreau /* Checks whether the RX descriptor having this status is both the first
7155428213cSwilly tarreau  * and the last descriptor for the RX packet. Each RX packet is currently
716c5aff182SThomas Petazzoni  * received through a single RX descriptor, so not having each RX
717c5aff182SThomas Petazzoni  * descriptor with its first and last bits set is an error
718c5aff182SThomas Petazzoni  */
7195428213cSwilly tarreau static int mvneta_rxq_desc_is_first_last(u32 status)
720c5aff182SThomas Petazzoni {
7215428213cSwilly tarreau 	return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
722c5aff182SThomas Petazzoni 		MVNETA_RXD_FIRST_LAST_DESC;
723c5aff182SThomas Petazzoni }
724c5aff182SThomas Petazzoni 
725c5aff182SThomas Petazzoni /* Add number of descriptors ready to receive new packets */
726c5aff182SThomas Petazzoni static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
727c5aff182SThomas Petazzoni 					  struct mvneta_rx_queue *rxq,
728c5aff182SThomas Petazzoni 					  int ndescs)
729c5aff182SThomas Petazzoni {
730c5aff182SThomas Petazzoni 	/* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
7316a20c175SThomas Petazzoni 	 * be added at once
7326a20c175SThomas Petazzoni 	 */
733c5aff182SThomas Petazzoni 	while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
734c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
735c5aff182SThomas Petazzoni 			    (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
736c5aff182SThomas Petazzoni 			     MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
737c5aff182SThomas Petazzoni 		ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
738c5aff182SThomas Petazzoni 	}
739c5aff182SThomas Petazzoni 
740c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
741c5aff182SThomas Petazzoni 		    (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
742c5aff182SThomas Petazzoni }
743c5aff182SThomas Petazzoni 
744c5aff182SThomas Petazzoni /* Get number of RX descriptors occupied by received packets */
745c5aff182SThomas Petazzoni static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
746c5aff182SThomas Petazzoni 					struct mvneta_rx_queue *rxq)
747c5aff182SThomas Petazzoni {
748c5aff182SThomas Petazzoni 	u32 val;
749c5aff182SThomas Petazzoni 
750c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
751c5aff182SThomas Petazzoni 	return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
752c5aff182SThomas Petazzoni }
753c5aff182SThomas Petazzoni 
7546a20c175SThomas Petazzoni /* Update num of rx desc called upon return from rx path or
755c5aff182SThomas Petazzoni  * from mvneta_rxq_drop_pkts().
756c5aff182SThomas Petazzoni  */
757c5aff182SThomas Petazzoni static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
758c5aff182SThomas Petazzoni 				       struct mvneta_rx_queue *rxq,
759c5aff182SThomas Petazzoni 				       int rx_done, int rx_filled)
760c5aff182SThomas Petazzoni {
761c5aff182SThomas Petazzoni 	u32 val;
762c5aff182SThomas Petazzoni 
763c5aff182SThomas Petazzoni 	if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
764c5aff182SThomas Petazzoni 		val = rx_done |
765c5aff182SThomas Petazzoni 		  (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
766c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
767c5aff182SThomas Petazzoni 		return;
768c5aff182SThomas Petazzoni 	}
769c5aff182SThomas Petazzoni 
770c5aff182SThomas Petazzoni 	/* Only 255 descriptors can be added at once */
771c5aff182SThomas Petazzoni 	while ((rx_done > 0) || (rx_filled > 0)) {
772c5aff182SThomas Petazzoni 		if (rx_done <= 0xff) {
773c5aff182SThomas Petazzoni 			val = rx_done;
774c5aff182SThomas Petazzoni 			rx_done = 0;
775c5aff182SThomas Petazzoni 		} else {
776c5aff182SThomas Petazzoni 			val = 0xff;
777c5aff182SThomas Petazzoni 			rx_done -= 0xff;
778c5aff182SThomas Petazzoni 		}
779c5aff182SThomas Petazzoni 		if (rx_filled <= 0xff) {
780c5aff182SThomas Petazzoni 			val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
781c5aff182SThomas Petazzoni 			rx_filled = 0;
782c5aff182SThomas Petazzoni 		} else {
783c5aff182SThomas Petazzoni 			val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
784c5aff182SThomas Petazzoni 			rx_filled -= 0xff;
785c5aff182SThomas Petazzoni 		}
786c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
787c5aff182SThomas Petazzoni 	}
788c5aff182SThomas Petazzoni }
789c5aff182SThomas Petazzoni 
790c5aff182SThomas Petazzoni /* Get pointer to next RX descriptor to be processed by SW */
791c5aff182SThomas Petazzoni static struct mvneta_rx_desc *
792c5aff182SThomas Petazzoni mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
793c5aff182SThomas Petazzoni {
794c5aff182SThomas Petazzoni 	int rx_desc = rxq->next_desc_to_proc;
795c5aff182SThomas Petazzoni 
796c5aff182SThomas Petazzoni 	rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
79734e4179dSwilly tarreau 	prefetch(rxq->descs + rxq->next_desc_to_proc);
798c5aff182SThomas Petazzoni 	return rxq->descs + rx_desc;
799c5aff182SThomas Petazzoni }
800c5aff182SThomas Petazzoni 
801c5aff182SThomas Petazzoni /* Change maximum receive size of the port. */
802c5aff182SThomas Petazzoni static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
803c5aff182SThomas Petazzoni {
804c5aff182SThomas Petazzoni 	u32 val;
805c5aff182SThomas Petazzoni 
806c5aff182SThomas Petazzoni 	val =  mvreg_read(pp, MVNETA_GMAC_CTRL_0);
807c5aff182SThomas Petazzoni 	val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
808c5aff182SThomas Petazzoni 	val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
809c5aff182SThomas Petazzoni 		MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
810c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
811c5aff182SThomas Petazzoni }
812c5aff182SThomas Petazzoni 
813c5aff182SThomas Petazzoni 
814c5aff182SThomas Petazzoni /* Set rx queue offset */
815c5aff182SThomas Petazzoni static void mvneta_rxq_offset_set(struct mvneta_port *pp,
816c5aff182SThomas Petazzoni 				  struct mvneta_rx_queue *rxq,
817c5aff182SThomas Petazzoni 				  int offset)
818c5aff182SThomas Petazzoni {
819c5aff182SThomas Petazzoni 	u32 val;
820c5aff182SThomas Petazzoni 
821c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
822c5aff182SThomas Petazzoni 	val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
823c5aff182SThomas Petazzoni 
824c5aff182SThomas Petazzoni 	/* Offset is in */
825c5aff182SThomas Petazzoni 	val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
826c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
827c5aff182SThomas Petazzoni }
828c5aff182SThomas Petazzoni 
829c5aff182SThomas Petazzoni 
830c5aff182SThomas Petazzoni /* Tx descriptors helper methods */
831c5aff182SThomas Petazzoni 
832c5aff182SThomas Petazzoni /* Update HW with number of TX descriptors to be sent */
833c5aff182SThomas Petazzoni static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
834c5aff182SThomas Petazzoni 				     struct mvneta_tx_queue *txq,
835c5aff182SThomas Petazzoni 				     int pend_desc)
836c5aff182SThomas Petazzoni {
837c5aff182SThomas Petazzoni 	u32 val;
838c5aff182SThomas Petazzoni 
8390d63785cSSimon Guinot 	pend_desc += txq->pending;
8400d63785cSSimon Guinot 
8410d63785cSSimon Guinot 	/* Only 255 Tx descriptors can be added at once */
8420d63785cSSimon Guinot 	do {
8430d63785cSSimon Guinot 		val = min(pend_desc, 255);
844c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
8450d63785cSSimon Guinot 		pend_desc -= val;
8460d63785cSSimon Guinot 	} while (pend_desc > 0);
8472a90f7e1SSimon Guinot 	txq->pending = 0;
848c5aff182SThomas Petazzoni }
849c5aff182SThomas Petazzoni 
850c5aff182SThomas Petazzoni /* Get pointer to next TX descriptor to be processed (send) by HW */
851c5aff182SThomas Petazzoni static struct mvneta_tx_desc *
852c5aff182SThomas Petazzoni mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
853c5aff182SThomas Petazzoni {
854c5aff182SThomas Petazzoni 	int tx_desc = txq->next_desc_to_proc;
855c5aff182SThomas Petazzoni 
856c5aff182SThomas Petazzoni 	txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
857c5aff182SThomas Petazzoni 	return txq->descs + tx_desc;
858c5aff182SThomas Petazzoni }
859c5aff182SThomas Petazzoni 
860c5aff182SThomas Petazzoni /* Release the last allocated TX descriptor. Useful to handle DMA
8616a20c175SThomas Petazzoni  * mapping failures in the TX path.
8626a20c175SThomas Petazzoni  */
863c5aff182SThomas Petazzoni static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
864c5aff182SThomas Petazzoni {
865c5aff182SThomas Petazzoni 	if (txq->next_desc_to_proc == 0)
866c5aff182SThomas Petazzoni 		txq->next_desc_to_proc = txq->last_desc - 1;
867c5aff182SThomas Petazzoni 	else
868c5aff182SThomas Petazzoni 		txq->next_desc_to_proc--;
869c5aff182SThomas Petazzoni }
870c5aff182SThomas Petazzoni 
871c5aff182SThomas Petazzoni /* Set rxq buf size */
872c5aff182SThomas Petazzoni static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
873c5aff182SThomas Petazzoni 				    struct mvneta_rx_queue *rxq,
874c5aff182SThomas Petazzoni 				    int buf_size)
875c5aff182SThomas Petazzoni {
876c5aff182SThomas Petazzoni 	u32 val;
877c5aff182SThomas Petazzoni 
878c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
879c5aff182SThomas Petazzoni 
880c5aff182SThomas Petazzoni 	val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
881c5aff182SThomas Petazzoni 	val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
882c5aff182SThomas Petazzoni 
883c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
884c5aff182SThomas Petazzoni }
885c5aff182SThomas Petazzoni 
886c5aff182SThomas Petazzoni /* Disable buffer management (BM) */
887c5aff182SThomas Petazzoni static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
888c5aff182SThomas Petazzoni 				  struct mvneta_rx_queue *rxq)
889c5aff182SThomas Petazzoni {
890c5aff182SThomas Petazzoni 	u32 val;
891c5aff182SThomas Petazzoni 
892c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
893c5aff182SThomas Petazzoni 	val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
894c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
895c5aff182SThomas Petazzoni }
896c5aff182SThomas Petazzoni 
897dc35a10fSMarcin Wojtas /* Enable buffer management (BM) */
898dc35a10fSMarcin Wojtas static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
899dc35a10fSMarcin Wojtas 				 struct mvneta_rx_queue *rxq)
900dc35a10fSMarcin Wojtas {
901dc35a10fSMarcin Wojtas 	u32 val;
902dc35a10fSMarcin Wojtas 
903dc35a10fSMarcin Wojtas 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
904dc35a10fSMarcin Wojtas 	val |= MVNETA_RXQ_HW_BUF_ALLOC;
905dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
906dc35a10fSMarcin Wojtas }
907dc35a10fSMarcin Wojtas 
908dc35a10fSMarcin Wojtas /* Notify HW about port's assignment of pool for bigger packets */
909dc35a10fSMarcin Wojtas static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
910dc35a10fSMarcin Wojtas 				     struct mvneta_rx_queue *rxq)
911dc35a10fSMarcin Wojtas {
912dc35a10fSMarcin Wojtas 	u32 val;
913dc35a10fSMarcin Wojtas 
914dc35a10fSMarcin Wojtas 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
915dc35a10fSMarcin Wojtas 	val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
916dc35a10fSMarcin Wojtas 	val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
917dc35a10fSMarcin Wojtas 
918dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
919dc35a10fSMarcin Wojtas }
920dc35a10fSMarcin Wojtas 
921dc35a10fSMarcin Wojtas /* Notify HW about port's assignment of pool for smaller packets */
922dc35a10fSMarcin Wojtas static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
923dc35a10fSMarcin Wojtas 				      struct mvneta_rx_queue *rxq)
924dc35a10fSMarcin Wojtas {
925dc35a10fSMarcin Wojtas 	u32 val;
926dc35a10fSMarcin Wojtas 
927dc35a10fSMarcin Wojtas 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
928dc35a10fSMarcin Wojtas 	val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
929dc35a10fSMarcin Wojtas 	val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
930dc35a10fSMarcin Wojtas 
931dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
932dc35a10fSMarcin Wojtas }
933dc35a10fSMarcin Wojtas 
934dc35a10fSMarcin Wojtas /* Set port's receive buffer size for assigned BM pool */
935dc35a10fSMarcin Wojtas static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
936dc35a10fSMarcin Wojtas 					      int buf_size,
937dc35a10fSMarcin Wojtas 					      u8 pool_id)
938dc35a10fSMarcin Wojtas {
939dc35a10fSMarcin Wojtas 	u32 val;
940dc35a10fSMarcin Wojtas 
941dc35a10fSMarcin Wojtas 	if (!IS_ALIGNED(buf_size, 8)) {
942dc35a10fSMarcin Wojtas 		dev_warn(pp->dev->dev.parent,
943dc35a10fSMarcin Wojtas 			 "illegal buf_size value %d, round to %d\n",
944dc35a10fSMarcin Wojtas 			 buf_size, ALIGN(buf_size, 8));
945dc35a10fSMarcin Wojtas 		buf_size = ALIGN(buf_size, 8);
946dc35a10fSMarcin Wojtas 	}
947dc35a10fSMarcin Wojtas 
948dc35a10fSMarcin Wojtas 	val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
949dc35a10fSMarcin Wojtas 	val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
950dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
951dc35a10fSMarcin Wojtas }
952dc35a10fSMarcin Wojtas 
953dc35a10fSMarcin Wojtas /* Configure MBUS window in order to enable access BM internal SRAM */
954dc35a10fSMarcin Wojtas static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
955dc35a10fSMarcin Wojtas 				  u8 target, u8 attr)
956dc35a10fSMarcin Wojtas {
957dc35a10fSMarcin Wojtas 	u32 win_enable, win_protect;
958dc35a10fSMarcin Wojtas 	int i;
959dc35a10fSMarcin Wojtas 
960dc35a10fSMarcin Wojtas 	win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
961dc35a10fSMarcin Wojtas 
962dc35a10fSMarcin Wojtas 	if (pp->bm_win_id < 0) {
963dc35a10fSMarcin Wojtas 		/* Find first not occupied window */
964dc35a10fSMarcin Wojtas 		for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
965dc35a10fSMarcin Wojtas 			if (win_enable & (1 << i)) {
966dc35a10fSMarcin Wojtas 				pp->bm_win_id = i;
967dc35a10fSMarcin Wojtas 				break;
968dc35a10fSMarcin Wojtas 			}
969dc35a10fSMarcin Wojtas 		}
970dc35a10fSMarcin Wojtas 		if (i == MVNETA_MAX_DECODE_WIN)
971dc35a10fSMarcin Wojtas 			return -ENOMEM;
972dc35a10fSMarcin Wojtas 	} else {
973dc35a10fSMarcin Wojtas 		i = pp->bm_win_id;
974dc35a10fSMarcin Wojtas 	}
975dc35a10fSMarcin Wojtas 
976dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
977dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
978dc35a10fSMarcin Wojtas 
979dc35a10fSMarcin Wojtas 	if (i < 4)
980dc35a10fSMarcin Wojtas 		mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
981dc35a10fSMarcin Wojtas 
982dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
983dc35a10fSMarcin Wojtas 		    (attr << 8) | target);
984dc35a10fSMarcin Wojtas 
985dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
986dc35a10fSMarcin Wojtas 
987dc35a10fSMarcin Wojtas 	win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
988dc35a10fSMarcin Wojtas 	win_protect |= 3 << (2 * i);
989dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
990dc35a10fSMarcin Wojtas 
991dc35a10fSMarcin Wojtas 	win_enable &= ~(1 << i);
992dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
993dc35a10fSMarcin Wojtas 
994dc35a10fSMarcin Wojtas 	return 0;
995dc35a10fSMarcin Wojtas }
996dc35a10fSMarcin Wojtas 
9972636ac3cSMarcin Wojtas static  int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
998dc35a10fSMarcin Wojtas {
9992636ac3cSMarcin Wojtas 	u32 wsize;
1000dc35a10fSMarcin Wojtas 	u8 target, attr;
1001dc35a10fSMarcin Wojtas 	int err;
1002dc35a10fSMarcin Wojtas 
1003dc35a10fSMarcin Wojtas 	/* Get BM window information */
1004dc35a10fSMarcin Wojtas 	err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1005dc35a10fSMarcin Wojtas 					 &target, &attr);
1006dc35a10fSMarcin Wojtas 	if (err < 0)
1007dc35a10fSMarcin Wojtas 		return err;
1008dc35a10fSMarcin Wojtas 
1009dc35a10fSMarcin Wojtas 	pp->bm_win_id = -1;
1010dc35a10fSMarcin Wojtas 
1011dc35a10fSMarcin Wojtas 	/* Open NETA -> BM window */
1012dc35a10fSMarcin Wojtas 	err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1013dc35a10fSMarcin Wojtas 				     target, attr);
1014dc35a10fSMarcin Wojtas 	if (err < 0) {
1015dc35a10fSMarcin Wojtas 		netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1016dc35a10fSMarcin Wojtas 		return err;
1017dc35a10fSMarcin Wojtas 	}
10182636ac3cSMarcin Wojtas 	return 0;
10192636ac3cSMarcin Wojtas }
10202636ac3cSMarcin Wojtas 
10212636ac3cSMarcin Wojtas /* Assign and initialize pools for port. In case of fail
10222636ac3cSMarcin Wojtas  * buffer manager will remain disabled for current port.
10232636ac3cSMarcin Wojtas  */
10242636ac3cSMarcin Wojtas static int mvneta_bm_port_init(struct platform_device *pdev,
10252636ac3cSMarcin Wojtas 			       struct mvneta_port *pp)
10262636ac3cSMarcin Wojtas {
10272636ac3cSMarcin Wojtas 	struct device_node *dn = pdev->dev.of_node;
10282636ac3cSMarcin Wojtas 	u32 long_pool_id, short_pool_id;
10292636ac3cSMarcin Wojtas 
10302636ac3cSMarcin Wojtas 	if (!pp->neta_armada3700) {
10312636ac3cSMarcin Wojtas 		int ret;
10322636ac3cSMarcin Wojtas 
10332636ac3cSMarcin Wojtas 		ret = mvneta_bm_port_mbus_init(pp);
10342636ac3cSMarcin Wojtas 		if (ret)
10352636ac3cSMarcin Wojtas 			return ret;
10362636ac3cSMarcin Wojtas 	}
1037dc35a10fSMarcin Wojtas 
1038dc35a10fSMarcin Wojtas 	if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1039dc35a10fSMarcin Wojtas 		netdev_info(pp->dev, "missing long pool id\n");
1040dc35a10fSMarcin Wojtas 		return -EINVAL;
1041dc35a10fSMarcin Wojtas 	}
1042dc35a10fSMarcin Wojtas 
1043dc35a10fSMarcin Wojtas 	/* Create port's long pool depending on mtu */
1044dc35a10fSMarcin Wojtas 	pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1045dc35a10fSMarcin Wojtas 					   MVNETA_BM_LONG, pp->id,
1046dc35a10fSMarcin Wojtas 					   MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1047dc35a10fSMarcin Wojtas 	if (!pp->pool_long) {
1048dc35a10fSMarcin Wojtas 		netdev_info(pp->dev, "fail to obtain long pool for port\n");
1049dc35a10fSMarcin Wojtas 		return -ENOMEM;
1050dc35a10fSMarcin Wojtas 	}
1051dc35a10fSMarcin Wojtas 
1052dc35a10fSMarcin Wojtas 	pp->pool_long->port_map |= 1 << pp->id;
1053dc35a10fSMarcin Wojtas 
1054dc35a10fSMarcin Wojtas 	mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1055dc35a10fSMarcin Wojtas 				   pp->pool_long->id);
1056dc35a10fSMarcin Wojtas 
1057dc35a10fSMarcin Wojtas 	/* If short pool id is not defined, assume using single pool */
1058dc35a10fSMarcin Wojtas 	if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1059dc35a10fSMarcin Wojtas 		short_pool_id = long_pool_id;
1060dc35a10fSMarcin Wojtas 
1061dc35a10fSMarcin Wojtas 	/* Create port's short pool */
1062dc35a10fSMarcin Wojtas 	pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1063dc35a10fSMarcin Wojtas 					    MVNETA_BM_SHORT, pp->id,
1064dc35a10fSMarcin Wojtas 					    MVNETA_BM_SHORT_PKT_SIZE);
1065dc35a10fSMarcin Wojtas 	if (!pp->pool_short) {
1066dc35a10fSMarcin Wojtas 		netdev_info(pp->dev, "fail to obtain short pool for port\n");
1067dc35a10fSMarcin Wojtas 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1068dc35a10fSMarcin Wojtas 		return -ENOMEM;
1069dc35a10fSMarcin Wojtas 	}
1070dc35a10fSMarcin Wojtas 
1071dc35a10fSMarcin Wojtas 	if (short_pool_id != long_pool_id) {
1072dc35a10fSMarcin Wojtas 		pp->pool_short->port_map |= 1 << pp->id;
1073dc35a10fSMarcin Wojtas 		mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1074dc35a10fSMarcin Wojtas 					   pp->pool_short->id);
1075dc35a10fSMarcin Wojtas 	}
1076dc35a10fSMarcin Wojtas 
1077dc35a10fSMarcin Wojtas 	return 0;
1078dc35a10fSMarcin Wojtas }
1079dc35a10fSMarcin Wojtas 
1080dc35a10fSMarcin Wojtas /* Update settings of a pool for bigger packets */
1081dc35a10fSMarcin Wojtas static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1082dc35a10fSMarcin Wojtas {
1083dc35a10fSMarcin Wojtas 	struct mvneta_bm_pool *bm_pool = pp->pool_long;
1084baa11ebcSGregory CLEMENT 	struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
1085dc35a10fSMarcin Wojtas 	int num;
1086dc35a10fSMarcin Wojtas 
1087dc35a10fSMarcin Wojtas 	/* Release all buffers from long pool */
1088dc35a10fSMarcin Wojtas 	mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1089baa11ebcSGregory CLEMENT 	if (hwbm_pool->buf_num) {
1090dc35a10fSMarcin Wojtas 		WARN(1, "cannot free all buffers in pool %d\n",
1091dc35a10fSMarcin Wojtas 		     bm_pool->id);
1092dc35a10fSMarcin Wojtas 		goto bm_mtu_err;
1093dc35a10fSMarcin Wojtas 	}
1094dc35a10fSMarcin Wojtas 
1095dc35a10fSMarcin Wojtas 	bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1096dc35a10fSMarcin Wojtas 	bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1097baa11ebcSGregory CLEMENT 	hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1098dc35a10fSMarcin Wojtas 			SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1099dc35a10fSMarcin Wojtas 
1100dc35a10fSMarcin Wojtas 	/* Fill entire long pool */
1101baa11ebcSGregory CLEMENT 	num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1102baa11ebcSGregory CLEMENT 	if (num != hwbm_pool->size) {
1103dc35a10fSMarcin Wojtas 		WARN(1, "pool %d: %d of %d allocated\n",
1104baa11ebcSGregory CLEMENT 		     bm_pool->id, num, hwbm_pool->size);
1105dc35a10fSMarcin Wojtas 		goto bm_mtu_err;
1106dc35a10fSMarcin Wojtas 	}
1107dc35a10fSMarcin Wojtas 	mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1108dc35a10fSMarcin Wojtas 
1109dc35a10fSMarcin Wojtas 	return;
1110dc35a10fSMarcin Wojtas 
1111dc35a10fSMarcin Wojtas bm_mtu_err:
1112dc35a10fSMarcin Wojtas 	mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1113dc35a10fSMarcin Wojtas 	mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1114dc35a10fSMarcin Wojtas 
1115dc35a10fSMarcin Wojtas 	pp->bm_priv = NULL;
1116dc35a10fSMarcin Wojtas 	mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1117dc35a10fSMarcin Wojtas 	netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1118dc35a10fSMarcin Wojtas }
1119dc35a10fSMarcin Wojtas 
1120c5aff182SThomas Petazzoni /* Start the Ethernet port RX and TX activity */
1121c5aff182SThomas Petazzoni static void mvneta_port_up(struct mvneta_port *pp)
1122c5aff182SThomas Petazzoni {
1123c5aff182SThomas Petazzoni 	int queue;
1124c5aff182SThomas Petazzoni 	u32 q_map;
1125c5aff182SThomas Petazzoni 
1126c5aff182SThomas Petazzoni 	/* Enable all initialized TXs. */
1127c5aff182SThomas Petazzoni 	q_map = 0;
1128c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
1129c5aff182SThomas Petazzoni 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
1130f95936ccSMarkus Elfring 		if (txq->descs)
1131c5aff182SThomas Petazzoni 			q_map |= (1 << queue);
1132c5aff182SThomas Petazzoni 	}
1133c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1134c5aff182SThomas Petazzoni 
1135c5aff182SThomas Petazzoni 	/* Enable all initialized RXQs. */
11362dcf75e2SGregory CLEMENT 	for (queue = 0; queue < rxq_number; queue++) {
11372dcf75e2SGregory CLEMENT 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
11382dcf75e2SGregory CLEMENT 
1139f95936ccSMarkus Elfring 		if (rxq->descs)
11402dcf75e2SGregory CLEMENT 			q_map |= (1 << queue);
11412dcf75e2SGregory CLEMENT 	}
11422dcf75e2SGregory CLEMENT 	mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
1143c5aff182SThomas Petazzoni }
1144c5aff182SThomas Petazzoni 
1145c5aff182SThomas Petazzoni /* Stop the Ethernet port activity */
1146c5aff182SThomas Petazzoni static void mvneta_port_down(struct mvneta_port *pp)
1147c5aff182SThomas Petazzoni {
1148c5aff182SThomas Petazzoni 	u32 val;
1149c5aff182SThomas Petazzoni 	int count;
1150c5aff182SThomas Petazzoni 
1151c5aff182SThomas Petazzoni 	/* Stop Rx port activity. Check port Rx activity. */
1152c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1153c5aff182SThomas Petazzoni 
1154c5aff182SThomas Petazzoni 	/* Issue stop command for active channels only */
1155c5aff182SThomas Petazzoni 	if (val != 0)
1156c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_RXQ_CMD,
1157c5aff182SThomas Petazzoni 			    val << MVNETA_RXQ_DISABLE_SHIFT);
1158c5aff182SThomas Petazzoni 
1159c5aff182SThomas Petazzoni 	/* Wait for all Rx activity to terminate. */
1160c5aff182SThomas Petazzoni 	count = 0;
1161c5aff182SThomas Petazzoni 	do {
1162c5aff182SThomas Petazzoni 		if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1163c5aff182SThomas Petazzoni 			netdev_warn(pp->dev,
11640838abb3SDmitri Epshtein 				    "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1165c5aff182SThomas Petazzoni 				    val);
1166c5aff182SThomas Petazzoni 			break;
1167c5aff182SThomas Petazzoni 		}
1168c5aff182SThomas Petazzoni 		mdelay(1);
1169c5aff182SThomas Petazzoni 
1170c5aff182SThomas Petazzoni 		val = mvreg_read(pp, MVNETA_RXQ_CMD);
1171a3703fb3SDmitri Epshtein 	} while (val & MVNETA_RXQ_ENABLE_MASK);
1172c5aff182SThomas Petazzoni 
1173c5aff182SThomas Petazzoni 	/* Stop Tx port activity. Check port Tx activity. Issue stop
11746a20c175SThomas Petazzoni 	 * command for active channels only
11756a20c175SThomas Petazzoni 	 */
1176c5aff182SThomas Petazzoni 	val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1177c5aff182SThomas Petazzoni 
1178c5aff182SThomas Petazzoni 	if (val != 0)
1179c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_TXQ_CMD,
1180c5aff182SThomas Petazzoni 			    (val << MVNETA_TXQ_DISABLE_SHIFT));
1181c5aff182SThomas Petazzoni 
1182c5aff182SThomas Petazzoni 	/* Wait for all Tx activity to terminate. */
1183c5aff182SThomas Petazzoni 	count = 0;
1184c5aff182SThomas Petazzoni 	do {
1185c5aff182SThomas Petazzoni 		if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1186c5aff182SThomas Petazzoni 			netdev_warn(pp->dev,
1187c5aff182SThomas Petazzoni 				    "TIMEOUT for TX stopped status=0x%08x\n",
1188c5aff182SThomas Petazzoni 				    val);
1189c5aff182SThomas Petazzoni 			break;
1190c5aff182SThomas Petazzoni 		}
1191c5aff182SThomas Petazzoni 		mdelay(1);
1192c5aff182SThomas Petazzoni 
1193c5aff182SThomas Petazzoni 		/* Check TX Command reg that all Txqs are stopped */
1194c5aff182SThomas Petazzoni 		val = mvreg_read(pp, MVNETA_TXQ_CMD);
1195c5aff182SThomas Petazzoni 
1196a3703fb3SDmitri Epshtein 	} while (val & MVNETA_TXQ_ENABLE_MASK);
1197c5aff182SThomas Petazzoni 
1198c5aff182SThomas Petazzoni 	/* Double check to verify that TX FIFO is empty */
1199c5aff182SThomas Petazzoni 	count = 0;
1200c5aff182SThomas Petazzoni 	do {
1201c5aff182SThomas Petazzoni 		if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1202c5aff182SThomas Petazzoni 			netdev_warn(pp->dev,
12030838abb3SDmitri Epshtein 				    "TX FIFO empty timeout status=0x%08x\n",
1204c5aff182SThomas Petazzoni 				    val);
1205c5aff182SThomas Petazzoni 			break;
1206c5aff182SThomas Petazzoni 		}
1207c5aff182SThomas Petazzoni 		mdelay(1);
1208c5aff182SThomas Petazzoni 
1209c5aff182SThomas Petazzoni 		val = mvreg_read(pp, MVNETA_PORT_STATUS);
1210c5aff182SThomas Petazzoni 	} while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1211c5aff182SThomas Petazzoni 		 (val & MVNETA_TX_IN_PRGRS));
1212c5aff182SThomas Petazzoni 
1213c5aff182SThomas Petazzoni 	udelay(200);
1214c5aff182SThomas Petazzoni }
1215c5aff182SThomas Petazzoni 
1216c5aff182SThomas Petazzoni /* Enable the port by setting the port enable bit of the MAC control register */
1217c5aff182SThomas Petazzoni static void mvneta_port_enable(struct mvneta_port *pp)
1218c5aff182SThomas Petazzoni {
1219c5aff182SThomas Petazzoni 	u32 val;
1220c5aff182SThomas Petazzoni 
1221c5aff182SThomas Petazzoni 	/* Enable port */
1222c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1223c5aff182SThomas Petazzoni 	val |= MVNETA_GMAC0_PORT_ENABLE;
1224c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1225c5aff182SThomas Petazzoni }
1226c5aff182SThomas Petazzoni 
1227c5aff182SThomas Petazzoni /* Disable the port and wait for about 200 usec before retuning */
1228c5aff182SThomas Petazzoni static void mvneta_port_disable(struct mvneta_port *pp)
1229c5aff182SThomas Petazzoni {
1230c5aff182SThomas Petazzoni 	u32 val;
1231c5aff182SThomas Petazzoni 
1232c5aff182SThomas Petazzoni 	/* Reset the Enable bit in the Serial Control Register */
1233c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1234c5aff182SThomas Petazzoni 	val &= ~MVNETA_GMAC0_PORT_ENABLE;
1235c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1236c5aff182SThomas Petazzoni 
1237c5aff182SThomas Petazzoni 	udelay(200);
1238c5aff182SThomas Petazzoni }
1239c5aff182SThomas Petazzoni 
1240c5aff182SThomas Petazzoni /* Multicast tables methods */
1241c5aff182SThomas Petazzoni 
1242c5aff182SThomas Petazzoni /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1243c5aff182SThomas Petazzoni static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1244c5aff182SThomas Petazzoni {
1245c5aff182SThomas Petazzoni 	int offset;
1246c5aff182SThomas Petazzoni 	u32 val;
1247c5aff182SThomas Petazzoni 
1248c5aff182SThomas Petazzoni 	if (queue == -1) {
1249c5aff182SThomas Petazzoni 		val = 0;
1250c5aff182SThomas Petazzoni 	} else {
1251c5aff182SThomas Petazzoni 		val = 0x1 | (queue << 1);
1252c5aff182SThomas Petazzoni 		val |= (val << 24) | (val << 16) | (val << 8);
1253c5aff182SThomas Petazzoni 	}
1254c5aff182SThomas Petazzoni 
1255c5aff182SThomas Petazzoni 	for (offset = 0; offset <= 0xc; offset += 4)
1256c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1257c5aff182SThomas Petazzoni }
1258c5aff182SThomas Petazzoni 
1259c5aff182SThomas Petazzoni /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1260c5aff182SThomas Petazzoni static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1261c5aff182SThomas Petazzoni {
1262c5aff182SThomas Petazzoni 	int offset;
1263c5aff182SThomas Petazzoni 	u32 val;
1264c5aff182SThomas Petazzoni 
1265c5aff182SThomas Petazzoni 	if (queue == -1) {
1266c5aff182SThomas Petazzoni 		val = 0;
1267c5aff182SThomas Petazzoni 	} else {
1268c5aff182SThomas Petazzoni 		val = 0x1 | (queue << 1);
1269c5aff182SThomas Petazzoni 		val |= (val << 24) | (val << 16) | (val << 8);
1270c5aff182SThomas Petazzoni 	}
1271c5aff182SThomas Petazzoni 
1272c5aff182SThomas Petazzoni 	for (offset = 0; offset <= 0xfc; offset += 4)
1273c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1274c5aff182SThomas Petazzoni 
1275c5aff182SThomas Petazzoni }
1276c5aff182SThomas Petazzoni 
1277c5aff182SThomas Petazzoni /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1278c5aff182SThomas Petazzoni static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1279c5aff182SThomas Petazzoni {
1280c5aff182SThomas Petazzoni 	int offset;
1281c5aff182SThomas Petazzoni 	u32 val;
1282c5aff182SThomas Petazzoni 
1283c5aff182SThomas Petazzoni 	if (queue == -1) {
1284c5aff182SThomas Petazzoni 		memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1285c5aff182SThomas Petazzoni 		val = 0;
1286c5aff182SThomas Petazzoni 	} else {
1287c5aff182SThomas Petazzoni 		memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1288c5aff182SThomas Petazzoni 		val = 0x1 | (queue << 1);
1289c5aff182SThomas Petazzoni 		val |= (val << 24) | (val << 16) | (val << 8);
1290c5aff182SThomas Petazzoni 	}
1291c5aff182SThomas Petazzoni 
1292c5aff182SThomas Petazzoni 	for (offset = 0; offset <= 0xfc; offset += 4)
1293c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1294c5aff182SThomas Petazzoni }
1295c5aff182SThomas Petazzoni 
1296db488c10SGregory CLEMENT static void mvneta_percpu_unmask_interrupt(void *arg)
1297db488c10SGregory CLEMENT {
1298db488c10SGregory CLEMENT 	struct mvneta_port *pp = arg;
1299db488c10SGregory CLEMENT 
1300db488c10SGregory CLEMENT 	/* All the queue are unmasked, but actually only the ones
1301db488c10SGregory CLEMENT 	 * mapped to this CPU will be unmasked
1302db488c10SGregory CLEMENT 	 */
1303db488c10SGregory CLEMENT 	mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1304db488c10SGregory CLEMENT 		    MVNETA_RX_INTR_MASK_ALL |
1305db488c10SGregory CLEMENT 		    MVNETA_TX_INTR_MASK_ALL |
1306db488c10SGregory CLEMENT 		    MVNETA_MISCINTR_INTR_MASK);
1307db488c10SGregory CLEMENT }
1308db488c10SGregory CLEMENT 
1309db488c10SGregory CLEMENT static void mvneta_percpu_mask_interrupt(void *arg)
1310db488c10SGregory CLEMENT {
1311db488c10SGregory CLEMENT 	struct mvneta_port *pp = arg;
1312db488c10SGregory CLEMENT 
1313db488c10SGregory CLEMENT 	/* All the queue are masked, but actually only the ones
1314db488c10SGregory CLEMENT 	 * mapped to this CPU will be masked
1315db488c10SGregory CLEMENT 	 */
1316db488c10SGregory CLEMENT 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1317db488c10SGregory CLEMENT 	mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1318db488c10SGregory CLEMENT 	mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1319db488c10SGregory CLEMENT }
1320db488c10SGregory CLEMENT 
1321db488c10SGregory CLEMENT static void mvneta_percpu_clear_intr_cause(void *arg)
1322db488c10SGregory CLEMENT {
1323db488c10SGregory CLEMENT 	struct mvneta_port *pp = arg;
1324db488c10SGregory CLEMENT 
1325db488c10SGregory CLEMENT 	/* All the queue are cleared, but actually only the ones
1326db488c10SGregory CLEMENT 	 * mapped to this CPU will be cleared
1327db488c10SGregory CLEMENT 	 */
1328db488c10SGregory CLEMENT 	mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1329db488c10SGregory CLEMENT 	mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1330db488c10SGregory CLEMENT 	mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1331db488c10SGregory CLEMENT }
1332db488c10SGregory CLEMENT 
1333c5aff182SThomas Petazzoni /* This method sets defaults to the NETA port:
1334c5aff182SThomas Petazzoni  *	Clears interrupt Cause and Mask registers.
1335c5aff182SThomas Petazzoni  *	Clears all MAC tables.
1336c5aff182SThomas Petazzoni  *	Sets defaults to all registers.
1337c5aff182SThomas Petazzoni  *	Resets RX and TX descriptor rings.
1338c5aff182SThomas Petazzoni  *	Resets PHY.
1339c5aff182SThomas Petazzoni  * This method can be called after mvneta_port_down() to return the port
1340c5aff182SThomas Petazzoni  *	settings to defaults.
1341c5aff182SThomas Petazzoni  */
1342c5aff182SThomas Petazzoni static void mvneta_defaults_set(struct mvneta_port *pp)
1343c5aff182SThomas Petazzoni {
1344c5aff182SThomas Petazzoni 	int cpu;
1345c5aff182SThomas Petazzoni 	int queue;
1346c5aff182SThomas Petazzoni 	u32 val;
13472dcf75e2SGregory CLEMENT 	int max_cpu = num_present_cpus();
1348c5aff182SThomas Petazzoni 
1349c5aff182SThomas Petazzoni 	/* Clear all Cause registers */
1350db488c10SGregory CLEMENT 	on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
1351c5aff182SThomas Petazzoni 
1352c5aff182SThomas Petazzoni 	/* Mask all interrupts */
1353db488c10SGregory CLEMENT 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
1354c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1355c5aff182SThomas Petazzoni 
1356c5aff182SThomas Petazzoni 	/* Enable MBUS Retry bit16 */
1357c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1358c5aff182SThomas Petazzoni 
135950bf8cb6SGregory CLEMENT 	/* Set CPU queue access map. CPUs are assigned to the RX and
136050bf8cb6SGregory CLEMENT 	 * TX queues modulo their number. If there is only one TX
136150bf8cb6SGregory CLEMENT 	 * queue then it is assigned to the CPU associated to the
136250bf8cb6SGregory CLEMENT 	 * default RX queue.
13636a20c175SThomas Petazzoni 	 */
13642dcf75e2SGregory CLEMENT 	for_each_present_cpu(cpu) {
13652dcf75e2SGregory CLEMENT 		int rxq_map = 0, txq_map = 0;
136650bf8cb6SGregory CLEMENT 		int rxq, txq;
13672636ac3cSMarcin Wojtas 		if (!pp->neta_armada3700) {
13682dcf75e2SGregory CLEMENT 			for (rxq = 0; rxq < rxq_number; rxq++)
13692dcf75e2SGregory CLEMENT 				if ((rxq % max_cpu) == cpu)
13702dcf75e2SGregory CLEMENT 					rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
13712dcf75e2SGregory CLEMENT 
137250bf8cb6SGregory CLEMENT 			for (txq = 0; txq < txq_number; txq++)
137350bf8cb6SGregory CLEMENT 				if ((txq % max_cpu) == cpu)
137450bf8cb6SGregory CLEMENT 					txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
137550bf8cb6SGregory CLEMENT 
137650bf8cb6SGregory CLEMENT 			/* With only one TX queue we configure a special case
137750bf8cb6SGregory CLEMENT 			 * which will allow to get all the irq on a single
137850bf8cb6SGregory CLEMENT 			 * CPU
137950bf8cb6SGregory CLEMENT 			 */
138050bf8cb6SGregory CLEMENT 			if (txq_number == 1)
138150bf8cb6SGregory CLEMENT 				txq_map = (cpu == pp->rxq_def) ?
138250bf8cb6SGregory CLEMENT 					MVNETA_CPU_TXQ_ACCESS(1) : 0;
13832dcf75e2SGregory CLEMENT 
13842636ac3cSMarcin Wojtas 		} else {
13852636ac3cSMarcin Wojtas 			txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
13862636ac3cSMarcin Wojtas 			rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
13872636ac3cSMarcin Wojtas 		}
13882636ac3cSMarcin Wojtas 
13892dcf75e2SGregory CLEMENT 		mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
13902dcf75e2SGregory CLEMENT 	}
1391c5aff182SThomas Petazzoni 
1392c5aff182SThomas Petazzoni 	/* Reset RX and TX DMAs */
1393c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1394c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1395c5aff182SThomas Petazzoni 
1396c5aff182SThomas Petazzoni 	/* Disable Legacy WRR, Disable EJP, Release from reset */
1397c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1398c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
1399c5aff182SThomas Petazzoni 		mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1400c5aff182SThomas Petazzoni 		mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1401c5aff182SThomas Petazzoni 	}
1402c5aff182SThomas Petazzoni 
1403c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1404c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1405c5aff182SThomas Petazzoni 
1406c5aff182SThomas Petazzoni 	/* Set Port Acceleration Mode */
1407dc35a10fSMarcin Wojtas 	if (pp->bm_priv)
1408dc35a10fSMarcin Wojtas 		/* HW buffer management + legacy parser */
1409dc35a10fSMarcin Wojtas 		val = MVNETA_ACC_MODE_EXT2;
1410dc35a10fSMarcin Wojtas 	else
1411dc35a10fSMarcin Wojtas 		/* SW buffer management + legacy parser */
1412dc35a10fSMarcin Wojtas 		val = MVNETA_ACC_MODE_EXT1;
1413c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_ACC_MODE, val);
1414c5aff182SThomas Petazzoni 
1415dc35a10fSMarcin Wojtas 	if (pp->bm_priv)
1416dc35a10fSMarcin Wojtas 		mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1417dc35a10fSMarcin Wojtas 
1418c5aff182SThomas Petazzoni 	/* Update val of portCfg register accordingly with all RxQueue types */
141990b74c01SGregory CLEMENT 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
1420c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1421c5aff182SThomas Petazzoni 
1422c5aff182SThomas Petazzoni 	val = 0;
1423c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1424c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1425c5aff182SThomas Petazzoni 
1426c5aff182SThomas Petazzoni 	/* Build PORT_SDMA_CONFIG_REG */
1427c5aff182SThomas Petazzoni 	val = 0;
1428c5aff182SThomas Petazzoni 
1429c5aff182SThomas Petazzoni 	/* Default burst size */
1430c5aff182SThomas Petazzoni 	val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1431c5aff182SThomas Petazzoni 	val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
14329ad8fef6SThomas Petazzoni 	val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1433c5aff182SThomas Petazzoni 
14349ad8fef6SThomas Petazzoni #if defined(__BIG_ENDIAN)
14359ad8fef6SThomas Petazzoni 	val |= MVNETA_DESC_SWAP;
14369ad8fef6SThomas Petazzoni #endif
1437c5aff182SThomas Petazzoni 
1438c5aff182SThomas Petazzoni 	/* Assign port SDMA configuration */
1439c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1440c5aff182SThomas Petazzoni 
144171408602SThomas Petazzoni 	/* Disable PHY polling in hardware, since we're using the
144271408602SThomas Petazzoni 	 * kernel phylib to do this.
144371408602SThomas Petazzoni 	 */
144471408602SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
144571408602SThomas Petazzoni 	val &= ~MVNETA_PHY_POLLING_ENABLE;
144671408602SThomas Petazzoni 	mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
144771408602SThomas Petazzoni 
1448c5aff182SThomas Petazzoni 	mvneta_set_ucast_table(pp, -1);
1449c5aff182SThomas Petazzoni 	mvneta_set_special_mcast_table(pp, -1);
1450c5aff182SThomas Petazzoni 	mvneta_set_other_mcast_table(pp, -1);
1451c5aff182SThomas Petazzoni 
1452c5aff182SThomas Petazzoni 	/* Set port interrupt enable register - default enable all */
1453c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_ENABLE,
1454c5aff182SThomas Petazzoni 		    (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1455c5aff182SThomas Petazzoni 		     | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1456e483911fSAndrew Lunn 
1457e483911fSAndrew Lunn 	mvneta_mib_counters_clear(pp);
1458c5aff182SThomas Petazzoni }
1459c5aff182SThomas Petazzoni 
1460c5aff182SThomas Petazzoni /* Set max sizes for tx queues */
1461c5aff182SThomas Petazzoni static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1462c5aff182SThomas Petazzoni 
1463c5aff182SThomas Petazzoni {
1464c5aff182SThomas Petazzoni 	u32 val, size, mtu;
1465c5aff182SThomas Petazzoni 	int queue;
1466c5aff182SThomas Petazzoni 
1467c5aff182SThomas Petazzoni 	mtu = max_tx_size * 8;
1468c5aff182SThomas Petazzoni 	if (mtu > MVNETA_TX_MTU_MAX)
1469c5aff182SThomas Petazzoni 		mtu = MVNETA_TX_MTU_MAX;
1470c5aff182SThomas Petazzoni 
1471c5aff182SThomas Petazzoni 	/* Set MTU */
1472c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TX_MTU);
1473c5aff182SThomas Petazzoni 	val &= ~MVNETA_TX_MTU_MAX;
1474c5aff182SThomas Petazzoni 	val |= mtu;
1475c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TX_MTU, val);
1476c5aff182SThomas Petazzoni 
1477c5aff182SThomas Petazzoni 	/* TX token size and all TXQs token size must be larger that MTU */
1478c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1479c5aff182SThomas Petazzoni 
1480c5aff182SThomas Petazzoni 	size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1481c5aff182SThomas Petazzoni 	if (size < mtu) {
1482c5aff182SThomas Petazzoni 		size = mtu;
1483c5aff182SThomas Petazzoni 		val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1484c5aff182SThomas Petazzoni 		val |= size;
1485c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1486c5aff182SThomas Petazzoni 	}
1487c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
1488c5aff182SThomas Petazzoni 		val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1489c5aff182SThomas Petazzoni 
1490c5aff182SThomas Petazzoni 		size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1491c5aff182SThomas Petazzoni 		if (size < mtu) {
1492c5aff182SThomas Petazzoni 			size = mtu;
1493c5aff182SThomas Petazzoni 			val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1494c5aff182SThomas Petazzoni 			val |= size;
1495c5aff182SThomas Petazzoni 			mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1496c5aff182SThomas Petazzoni 		}
1497c5aff182SThomas Petazzoni 	}
1498c5aff182SThomas Petazzoni }
1499c5aff182SThomas Petazzoni 
1500c5aff182SThomas Petazzoni /* Set unicast address */
1501c5aff182SThomas Petazzoni static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1502c5aff182SThomas Petazzoni 				  int queue)
1503c5aff182SThomas Petazzoni {
1504c5aff182SThomas Petazzoni 	unsigned int unicast_reg;
1505c5aff182SThomas Petazzoni 	unsigned int tbl_offset;
1506c5aff182SThomas Petazzoni 	unsigned int reg_offset;
1507c5aff182SThomas Petazzoni 
1508c5aff182SThomas Petazzoni 	/* Locate the Unicast table entry */
1509c5aff182SThomas Petazzoni 	last_nibble = (0xf & last_nibble);
1510c5aff182SThomas Petazzoni 
1511c5aff182SThomas Petazzoni 	/* offset from unicast tbl base */
1512c5aff182SThomas Petazzoni 	tbl_offset = (last_nibble / 4) * 4;
1513c5aff182SThomas Petazzoni 
1514c5aff182SThomas Petazzoni 	/* offset within the above reg  */
1515c5aff182SThomas Petazzoni 	reg_offset = last_nibble % 4;
1516c5aff182SThomas Petazzoni 
1517c5aff182SThomas Petazzoni 	unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1518c5aff182SThomas Petazzoni 
1519c5aff182SThomas Petazzoni 	if (queue == -1) {
1520c5aff182SThomas Petazzoni 		/* Clear accepts frame bit at specified unicast DA tbl entry */
1521c5aff182SThomas Petazzoni 		unicast_reg &= ~(0xff << (8 * reg_offset));
1522c5aff182SThomas Petazzoni 	} else {
1523c5aff182SThomas Petazzoni 		unicast_reg &= ~(0xff << (8 * reg_offset));
1524c5aff182SThomas Petazzoni 		unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1525c5aff182SThomas Petazzoni 	}
1526c5aff182SThomas Petazzoni 
1527c5aff182SThomas Petazzoni 	mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1528c5aff182SThomas Petazzoni }
1529c5aff182SThomas Petazzoni 
1530c5aff182SThomas Petazzoni /* Set mac address */
1531c5aff182SThomas Petazzoni static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1532c5aff182SThomas Petazzoni 				int queue)
1533c5aff182SThomas Petazzoni {
1534c5aff182SThomas Petazzoni 	unsigned int mac_h;
1535c5aff182SThomas Petazzoni 	unsigned int mac_l;
1536c5aff182SThomas Petazzoni 
1537c5aff182SThomas Petazzoni 	if (queue != -1) {
1538c5aff182SThomas Petazzoni 		mac_l = (addr[4] << 8) | (addr[5]);
1539c5aff182SThomas Petazzoni 		mac_h = (addr[0] << 24) | (addr[1] << 16) |
1540c5aff182SThomas Petazzoni 			(addr[2] << 8) | (addr[3] << 0);
1541c5aff182SThomas Petazzoni 
1542c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1543c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1544c5aff182SThomas Petazzoni 	}
1545c5aff182SThomas Petazzoni 
1546c5aff182SThomas Petazzoni 	/* Accept frames of this address */
1547c5aff182SThomas Petazzoni 	mvneta_set_ucast_addr(pp, addr[5], queue);
1548c5aff182SThomas Petazzoni }
1549c5aff182SThomas Petazzoni 
15506a20c175SThomas Petazzoni /* Set the number of packets that will be received before RX interrupt
15516a20c175SThomas Petazzoni  * will be generated by HW.
1552c5aff182SThomas Petazzoni  */
1553c5aff182SThomas Petazzoni static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1554c5aff182SThomas Petazzoni 				    struct mvneta_rx_queue *rxq, u32 value)
1555c5aff182SThomas Petazzoni {
1556c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1557c5aff182SThomas Petazzoni 		    value | MVNETA_RXQ_NON_OCCUPIED(0));
1558c5aff182SThomas Petazzoni 	rxq->pkts_coal = value;
1559c5aff182SThomas Petazzoni }
1560c5aff182SThomas Petazzoni 
15616a20c175SThomas Petazzoni /* Set the time delay in usec before RX interrupt will be generated by
15626a20c175SThomas Petazzoni  * HW.
1563c5aff182SThomas Petazzoni  */
1564c5aff182SThomas Petazzoni static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1565c5aff182SThomas Petazzoni 				    struct mvneta_rx_queue *rxq, u32 value)
1566c5aff182SThomas Petazzoni {
1567189dd626SThomas Petazzoni 	u32 val;
1568189dd626SThomas Petazzoni 	unsigned long clk_rate;
1569189dd626SThomas Petazzoni 
1570189dd626SThomas Petazzoni 	clk_rate = clk_get_rate(pp->clk);
1571189dd626SThomas Petazzoni 	val = (clk_rate / 1000000) * value;
1572c5aff182SThomas Petazzoni 
1573c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1574c5aff182SThomas Petazzoni 	rxq->time_coal = value;
1575c5aff182SThomas Petazzoni }
1576c5aff182SThomas Petazzoni 
1577c5aff182SThomas Petazzoni /* Set threshold for TX_DONE pkts coalescing */
1578c5aff182SThomas Petazzoni static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1579c5aff182SThomas Petazzoni 					 struct mvneta_tx_queue *txq, u32 value)
1580c5aff182SThomas Petazzoni {
1581c5aff182SThomas Petazzoni 	u32 val;
1582c5aff182SThomas Petazzoni 
1583c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1584c5aff182SThomas Petazzoni 
1585c5aff182SThomas Petazzoni 	val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1586c5aff182SThomas Petazzoni 	val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1587c5aff182SThomas Petazzoni 
1588c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1589c5aff182SThomas Petazzoni 
1590c5aff182SThomas Petazzoni 	txq->done_pkts_coal = value;
1591c5aff182SThomas Petazzoni }
1592c5aff182SThomas Petazzoni 
1593c5aff182SThomas Petazzoni /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1594c5aff182SThomas Petazzoni static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1595f88bee1cSGregory CLEMENT 				u32 phys_addr, void *virt_addr,
1596f88bee1cSGregory CLEMENT 				struct mvneta_rx_queue *rxq)
1597c5aff182SThomas Petazzoni {
1598f88bee1cSGregory CLEMENT 	int i;
1599f88bee1cSGregory CLEMENT 
1600c5aff182SThomas Petazzoni 	rx_desc->buf_phys_addr = phys_addr;
1601f88bee1cSGregory CLEMENT 	i = rx_desc - rxq->descs;
1602f88bee1cSGregory CLEMENT 	rxq->buf_virt_addr[i] = virt_addr;
1603c5aff182SThomas Petazzoni }
1604c5aff182SThomas Petazzoni 
1605c5aff182SThomas Petazzoni /* Decrement sent descriptors counter */
1606c5aff182SThomas Petazzoni static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1607c5aff182SThomas Petazzoni 				     struct mvneta_tx_queue *txq,
1608c5aff182SThomas Petazzoni 				     int sent_desc)
1609c5aff182SThomas Petazzoni {
1610c5aff182SThomas Petazzoni 	u32 val;
1611c5aff182SThomas Petazzoni 
1612c5aff182SThomas Petazzoni 	/* Only 255 TX descriptors can be updated at once */
1613c5aff182SThomas Petazzoni 	while (sent_desc > 0xff) {
1614c5aff182SThomas Petazzoni 		val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1615c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1616c5aff182SThomas Petazzoni 		sent_desc = sent_desc - 0xff;
1617c5aff182SThomas Petazzoni 	}
1618c5aff182SThomas Petazzoni 
1619c5aff182SThomas Petazzoni 	val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1620c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1621c5aff182SThomas Petazzoni }
1622c5aff182SThomas Petazzoni 
1623c5aff182SThomas Petazzoni /* Get number of TX descriptors already sent by HW */
1624c5aff182SThomas Petazzoni static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1625c5aff182SThomas Petazzoni 					struct mvneta_tx_queue *txq)
1626c5aff182SThomas Petazzoni {
1627c5aff182SThomas Petazzoni 	u32 val;
1628c5aff182SThomas Petazzoni 	int sent_desc;
1629c5aff182SThomas Petazzoni 
1630c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1631c5aff182SThomas Petazzoni 	sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1632c5aff182SThomas Petazzoni 		MVNETA_TXQ_SENT_DESC_SHIFT;
1633c5aff182SThomas Petazzoni 
1634c5aff182SThomas Petazzoni 	return sent_desc;
1635c5aff182SThomas Petazzoni }
1636c5aff182SThomas Petazzoni 
16376a20c175SThomas Petazzoni /* Get number of sent descriptors and decrement counter.
1638c5aff182SThomas Petazzoni  *  The number of sent descriptors is returned.
1639c5aff182SThomas Petazzoni  */
1640c5aff182SThomas Petazzoni static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1641c5aff182SThomas Petazzoni 				     struct mvneta_tx_queue *txq)
1642c5aff182SThomas Petazzoni {
1643c5aff182SThomas Petazzoni 	int sent_desc;
1644c5aff182SThomas Petazzoni 
1645c5aff182SThomas Petazzoni 	/* Get number of sent descriptors */
1646c5aff182SThomas Petazzoni 	sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1647c5aff182SThomas Petazzoni 
1648c5aff182SThomas Petazzoni 	/* Decrement sent descriptors counter */
1649c5aff182SThomas Petazzoni 	if (sent_desc)
1650c5aff182SThomas Petazzoni 		mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1651c5aff182SThomas Petazzoni 
1652c5aff182SThomas Petazzoni 	return sent_desc;
1653c5aff182SThomas Petazzoni }
1654c5aff182SThomas Petazzoni 
1655c5aff182SThomas Petazzoni /* Set TXQ descriptors fields relevant for CSUM calculation */
1656c5aff182SThomas Petazzoni static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1657c5aff182SThomas Petazzoni 				int ip_hdr_len, int l4_proto)
1658c5aff182SThomas Petazzoni {
1659c5aff182SThomas Petazzoni 	u32 command;
1660c5aff182SThomas Petazzoni 
1661c5aff182SThomas Petazzoni 	/* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
16626a20c175SThomas Petazzoni 	 * G_L4_chk, L4_type; required only for checksum
16636a20c175SThomas Petazzoni 	 * calculation
16646a20c175SThomas Petazzoni 	 */
1665c5aff182SThomas Petazzoni 	command =  l3_offs    << MVNETA_TX_L3_OFF_SHIFT;
1666c5aff182SThomas Petazzoni 	command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1667c5aff182SThomas Petazzoni 
16680a198587SThomas Fitzsimmons 	if (l3_proto == htons(ETH_P_IP))
1669c5aff182SThomas Petazzoni 		command |= MVNETA_TXD_IP_CSUM;
1670c5aff182SThomas Petazzoni 	else
1671c5aff182SThomas Petazzoni 		command |= MVNETA_TX_L3_IP6;
1672c5aff182SThomas Petazzoni 
1673c5aff182SThomas Petazzoni 	if (l4_proto == IPPROTO_TCP)
1674c5aff182SThomas Petazzoni 		command |=  MVNETA_TX_L4_CSUM_FULL;
1675c5aff182SThomas Petazzoni 	else if (l4_proto == IPPROTO_UDP)
1676c5aff182SThomas Petazzoni 		command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1677c5aff182SThomas Petazzoni 	else
1678c5aff182SThomas Petazzoni 		command |= MVNETA_TX_L4_CSUM_NOT;
1679c5aff182SThomas Petazzoni 
1680c5aff182SThomas Petazzoni 	return command;
1681c5aff182SThomas Petazzoni }
1682c5aff182SThomas Petazzoni 
1683c5aff182SThomas Petazzoni 
1684c5aff182SThomas Petazzoni /* Display more error info */
1685c5aff182SThomas Petazzoni static void mvneta_rx_error(struct mvneta_port *pp,
1686c5aff182SThomas Petazzoni 			    struct mvneta_rx_desc *rx_desc)
1687c5aff182SThomas Petazzoni {
1688c5aff182SThomas Petazzoni 	u32 status = rx_desc->status;
1689c5aff182SThomas Petazzoni 
16905428213cSwilly tarreau 	if (!mvneta_rxq_desc_is_first_last(status)) {
1691c5aff182SThomas Petazzoni 		netdev_err(pp->dev,
1692c5aff182SThomas Petazzoni 			   "bad rx status %08x (buffer oversize), size=%d\n",
16935428213cSwilly tarreau 			   status, rx_desc->data_size);
1694c5aff182SThomas Petazzoni 		return;
1695c5aff182SThomas Petazzoni 	}
1696c5aff182SThomas Petazzoni 
1697c5aff182SThomas Petazzoni 	switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1698c5aff182SThomas Petazzoni 	case MVNETA_RXD_ERR_CRC:
1699c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1700c5aff182SThomas Petazzoni 			   status, rx_desc->data_size);
1701c5aff182SThomas Petazzoni 		break;
1702c5aff182SThomas Petazzoni 	case MVNETA_RXD_ERR_OVERRUN:
1703c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1704c5aff182SThomas Petazzoni 			   status, rx_desc->data_size);
1705c5aff182SThomas Petazzoni 		break;
1706c5aff182SThomas Petazzoni 	case MVNETA_RXD_ERR_LEN:
1707c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1708c5aff182SThomas Petazzoni 			   status, rx_desc->data_size);
1709c5aff182SThomas Petazzoni 		break;
1710c5aff182SThomas Petazzoni 	case MVNETA_RXD_ERR_RESOURCE:
1711c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1712c5aff182SThomas Petazzoni 			   status, rx_desc->data_size);
1713c5aff182SThomas Petazzoni 		break;
1714c5aff182SThomas Petazzoni 	}
1715c5aff182SThomas Petazzoni }
1716c5aff182SThomas Petazzoni 
17175428213cSwilly tarreau /* Handle RX checksum offload based on the descriptor's status */
17185428213cSwilly tarreau static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
1719c5aff182SThomas Petazzoni 			   struct sk_buff *skb)
1720c5aff182SThomas Petazzoni {
17215428213cSwilly tarreau 	if ((status & MVNETA_RXD_L3_IP4) &&
17225428213cSwilly tarreau 	    (status & MVNETA_RXD_L4_CSUM_OK)) {
1723c5aff182SThomas Petazzoni 		skb->csum = 0;
1724c5aff182SThomas Petazzoni 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1725c5aff182SThomas Petazzoni 		return;
1726c5aff182SThomas Petazzoni 	}
1727c5aff182SThomas Petazzoni 
1728c5aff182SThomas Petazzoni 	skb->ip_summed = CHECKSUM_NONE;
1729c5aff182SThomas Petazzoni }
1730c5aff182SThomas Petazzoni 
17316c498974Swilly tarreau /* Return tx queue pointer (find last set bit) according to <cause> returned
17326c498974Swilly tarreau  * form tx_done reg. <cause> must not be null. The return value is always a
17336c498974Swilly tarreau  * valid queue for matching the first one found in <cause>.
17346c498974Swilly tarreau  */
1735c5aff182SThomas Petazzoni static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1736c5aff182SThomas Petazzoni 						     u32 cause)
1737c5aff182SThomas Petazzoni {
1738c5aff182SThomas Petazzoni 	int queue = fls(cause) - 1;
1739c5aff182SThomas Petazzoni 
17406c498974Swilly tarreau 	return &pp->txqs[queue];
1741c5aff182SThomas Petazzoni }
1742c5aff182SThomas Petazzoni 
1743c5aff182SThomas Petazzoni /* Free tx queue skbuffs */
1744c5aff182SThomas Petazzoni static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1745a29b6235SMarcin Wojtas 				 struct mvneta_tx_queue *txq, int num,
1746a29b6235SMarcin Wojtas 				 struct netdev_queue *nq)
1747c5aff182SThomas Petazzoni {
1748a29b6235SMarcin Wojtas 	unsigned int bytes_compl = 0, pkts_compl = 0;
1749c5aff182SThomas Petazzoni 	int i;
1750c5aff182SThomas Petazzoni 
1751c5aff182SThomas Petazzoni 	for (i = 0; i < num; i++) {
1752c5aff182SThomas Petazzoni 		struct mvneta_tx_desc *tx_desc = txq->descs +
1753c5aff182SThomas Petazzoni 			txq->txq_get_index;
1754c5aff182SThomas Petazzoni 		struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1755c5aff182SThomas Petazzoni 
1756a29b6235SMarcin Wojtas 		if (skb) {
1757a29b6235SMarcin Wojtas 			bytes_compl += skb->len;
1758a29b6235SMarcin Wojtas 			pkts_compl++;
1759a29b6235SMarcin Wojtas 		}
1760a29b6235SMarcin Wojtas 
1761c5aff182SThomas Petazzoni 		mvneta_txq_inc_get(txq);
1762c5aff182SThomas Petazzoni 
17632e3173a3SEzequiel Garcia 		if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
17642e3173a3SEzequiel Garcia 			dma_unmap_single(pp->dev->dev.parent,
17652e3173a3SEzequiel Garcia 					 tx_desc->buf_phys_addr,
1766c5aff182SThomas Petazzoni 					 tx_desc->data_size, DMA_TO_DEVICE);
1767ba7e46efSEzequiel Garcia 		if (!skb)
1768ba7e46efSEzequiel Garcia 			continue;
1769c5aff182SThomas Petazzoni 		dev_kfree_skb_any(skb);
1770c5aff182SThomas Petazzoni 	}
1771a29b6235SMarcin Wojtas 
1772a29b6235SMarcin Wojtas 	netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
1773c5aff182SThomas Petazzoni }
1774c5aff182SThomas Petazzoni 
1775c5aff182SThomas Petazzoni /* Handle end of transmission */
1776cd713199SArnaud Ebalard static void mvneta_txq_done(struct mvneta_port *pp,
1777c5aff182SThomas Petazzoni 			   struct mvneta_tx_queue *txq)
1778c5aff182SThomas Petazzoni {
1779c5aff182SThomas Petazzoni 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1780c5aff182SThomas Petazzoni 	int tx_done;
1781c5aff182SThomas Petazzoni 
1782c5aff182SThomas Petazzoni 	tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1783cd713199SArnaud Ebalard 	if (!tx_done)
1784cd713199SArnaud Ebalard 		return;
1785cd713199SArnaud Ebalard 
1786a29b6235SMarcin Wojtas 	mvneta_txq_bufs_free(pp, txq, tx_done, nq);
1787c5aff182SThomas Petazzoni 
1788c5aff182SThomas Petazzoni 	txq->count -= tx_done;
1789c5aff182SThomas Petazzoni 
1790c5aff182SThomas Petazzoni 	if (netif_tx_queue_stopped(nq)) {
17918eef5f97SEzequiel Garcia 		if (txq->count <= txq->tx_wake_threshold)
1792c5aff182SThomas Petazzoni 			netif_tx_wake_queue(nq);
1793c5aff182SThomas Petazzoni 	}
1794c5aff182SThomas Petazzoni }
1795c5aff182SThomas Petazzoni 
1796dc35a10fSMarcin Wojtas void *mvneta_frag_alloc(unsigned int frag_size)
17978ec2cd48Swilly tarreau {
1798dc35a10fSMarcin Wojtas 	if (likely(frag_size <= PAGE_SIZE))
1799dc35a10fSMarcin Wojtas 		return netdev_alloc_frag(frag_size);
18008ec2cd48Swilly tarreau 	else
1801dc35a10fSMarcin Wojtas 		return kmalloc(frag_size, GFP_ATOMIC);
18028ec2cd48Swilly tarreau }
1803dc35a10fSMarcin Wojtas EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
18048ec2cd48Swilly tarreau 
1805dc35a10fSMarcin Wojtas void mvneta_frag_free(unsigned int frag_size, void *data)
18068ec2cd48Swilly tarreau {
1807dc35a10fSMarcin Wojtas 	if (likely(frag_size <= PAGE_SIZE))
180813dc0d2bSAlexander Duyck 		skb_free_frag(data);
18098ec2cd48Swilly tarreau 	else
18108ec2cd48Swilly tarreau 		kfree(data);
18118ec2cd48Swilly tarreau }
1812dc35a10fSMarcin Wojtas EXPORT_SYMBOL_GPL(mvneta_frag_free);
18138ec2cd48Swilly tarreau 
1814dc35a10fSMarcin Wojtas /* Refill processing for SW buffer management */
1815c5aff182SThomas Petazzoni static int mvneta_rx_refill(struct mvneta_port *pp,
1816f88bee1cSGregory CLEMENT 			    struct mvneta_rx_desc *rx_desc,
1817f88bee1cSGregory CLEMENT 			    struct mvneta_rx_queue *rxq)
1818c5aff182SThomas Petazzoni 
1819c5aff182SThomas Petazzoni {
1820c5aff182SThomas Petazzoni 	dma_addr_t phys_addr;
18218ec2cd48Swilly tarreau 	void *data;
1822c5aff182SThomas Petazzoni 
1823dc35a10fSMarcin Wojtas 	data = mvneta_frag_alloc(pp->frag_size);
18248ec2cd48Swilly tarreau 	if (!data)
1825c5aff182SThomas Petazzoni 		return -ENOMEM;
1826c5aff182SThomas Petazzoni 
18278ec2cd48Swilly tarreau 	phys_addr = dma_map_single(pp->dev->dev.parent, data,
1828c5aff182SThomas Petazzoni 				   MVNETA_RX_BUF_SIZE(pp->pkt_size),
1829c5aff182SThomas Petazzoni 				   DMA_FROM_DEVICE);
1830c5aff182SThomas Petazzoni 	if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1831dc35a10fSMarcin Wojtas 		mvneta_frag_free(pp->frag_size, data);
1832c5aff182SThomas Petazzoni 		return -ENOMEM;
1833c5aff182SThomas Petazzoni 	}
1834c5aff182SThomas Petazzoni 
18358d5047cfSMarcin Wojtas 	phys_addr += pp->rx_offset_correction;
1836f88bee1cSGregory CLEMENT 	mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
1837c5aff182SThomas Petazzoni 	return 0;
1838c5aff182SThomas Petazzoni }
1839c5aff182SThomas Petazzoni 
1840c5aff182SThomas Petazzoni /* Handle tx checksum */
1841c5aff182SThomas Petazzoni static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1842c5aff182SThomas Petazzoni {
1843c5aff182SThomas Petazzoni 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1844c5aff182SThomas Petazzoni 		int ip_hdr_len = 0;
1845817dbfa5SVlad Yasevich 		__be16 l3_proto = vlan_get_protocol(skb);
1846c5aff182SThomas Petazzoni 		u8 l4_proto;
1847c5aff182SThomas Petazzoni 
1848817dbfa5SVlad Yasevich 		if (l3_proto == htons(ETH_P_IP)) {
1849c5aff182SThomas Petazzoni 			struct iphdr *ip4h = ip_hdr(skb);
1850c5aff182SThomas Petazzoni 
1851c5aff182SThomas Petazzoni 			/* Calculate IPv4 checksum and L4 checksum */
1852c5aff182SThomas Petazzoni 			ip_hdr_len = ip4h->ihl;
1853c5aff182SThomas Petazzoni 			l4_proto = ip4h->protocol;
1854817dbfa5SVlad Yasevich 		} else if (l3_proto == htons(ETH_P_IPV6)) {
1855c5aff182SThomas Petazzoni 			struct ipv6hdr *ip6h = ipv6_hdr(skb);
1856c5aff182SThomas Petazzoni 
1857c5aff182SThomas Petazzoni 			/* Read l4_protocol from one of IPv6 extra headers */
1858c5aff182SThomas Petazzoni 			if (skb_network_header_len(skb) > 0)
1859c5aff182SThomas Petazzoni 				ip_hdr_len = (skb_network_header_len(skb) >> 2);
1860c5aff182SThomas Petazzoni 			l4_proto = ip6h->nexthdr;
1861c5aff182SThomas Petazzoni 		} else
1862c5aff182SThomas Petazzoni 			return MVNETA_TX_L4_CSUM_NOT;
1863c5aff182SThomas Petazzoni 
1864c5aff182SThomas Petazzoni 		return mvneta_txq_desc_csum(skb_network_offset(skb),
1865817dbfa5SVlad Yasevich 					    l3_proto, ip_hdr_len, l4_proto);
1866c5aff182SThomas Petazzoni 	}
1867c5aff182SThomas Petazzoni 
1868c5aff182SThomas Petazzoni 	return MVNETA_TX_L4_CSUM_NOT;
1869c5aff182SThomas Petazzoni }
1870c5aff182SThomas Petazzoni 
1871c5aff182SThomas Petazzoni /* Drop packets received by the RXQ and free buffers */
1872c5aff182SThomas Petazzoni static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1873c5aff182SThomas Petazzoni 				 struct mvneta_rx_queue *rxq)
1874c5aff182SThomas Petazzoni {
1875c5aff182SThomas Petazzoni 	int rx_done, i;
1876c5aff182SThomas Petazzoni 
1877c5aff182SThomas Petazzoni 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1878dc35a10fSMarcin Wojtas 	if (rx_done)
1879dc35a10fSMarcin Wojtas 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1880dc35a10fSMarcin Wojtas 
1881dc35a10fSMarcin Wojtas 	if (pp->bm_priv) {
1882dc35a10fSMarcin Wojtas 		for (i = 0; i < rx_done; i++) {
1883dc35a10fSMarcin Wojtas 			struct mvneta_rx_desc *rx_desc =
1884dc35a10fSMarcin Wojtas 						  mvneta_rxq_next_desc_get(rxq);
1885dc35a10fSMarcin Wojtas 			u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1886dc35a10fSMarcin Wojtas 			struct mvneta_bm_pool *bm_pool;
1887dc35a10fSMarcin Wojtas 
1888dc35a10fSMarcin Wojtas 			bm_pool = &pp->bm_priv->bm_pools[pool_id];
1889dc35a10fSMarcin Wojtas 			/* Return dropped buffer to the pool */
1890dc35a10fSMarcin Wojtas 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1891dc35a10fSMarcin Wojtas 					      rx_desc->buf_phys_addr);
1892dc35a10fSMarcin Wojtas 		}
1893dc35a10fSMarcin Wojtas 		return;
1894dc35a10fSMarcin Wojtas 	}
1895dc35a10fSMarcin Wojtas 
1896c5aff182SThomas Petazzoni 	for (i = 0; i < rxq->size; i++) {
1897c5aff182SThomas Petazzoni 		struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1898f88bee1cSGregory CLEMENT 		void *data = rxq->buf_virt_addr[i];
1899c5aff182SThomas Petazzoni 
1900c5aff182SThomas Petazzoni 		dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
1901a328f3a0SEzequiel Garcia 				 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
1902dc35a10fSMarcin Wojtas 		mvneta_frag_free(pp->frag_size, data);
1903dc35a10fSMarcin Wojtas 	}
1904c5aff182SThomas Petazzoni }
1905c5aff182SThomas Petazzoni 
1906dc35a10fSMarcin Wojtas /* Main rx processing when using software buffer management */
1907dc35a10fSMarcin Wojtas static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1908c5aff182SThomas Petazzoni 			  struct mvneta_rx_queue *rxq)
1909c5aff182SThomas Petazzoni {
191012bb03b4SMaxime Ripard 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
1911c5aff182SThomas Petazzoni 	struct net_device *dev = pp->dev;
1912a84e3289SSimon Guinot 	int rx_done;
1913dc4277ddSwilly tarreau 	u32 rcvd_pkts = 0;
1914dc4277ddSwilly tarreau 	u32 rcvd_bytes = 0;
1915c5aff182SThomas Petazzoni 
1916c5aff182SThomas Petazzoni 	/* Get number of received packets */
1917c5aff182SThomas Petazzoni 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1918c5aff182SThomas Petazzoni 
1919c5aff182SThomas Petazzoni 	if (rx_todo > rx_done)
1920c5aff182SThomas Petazzoni 		rx_todo = rx_done;
1921c5aff182SThomas Petazzoni 
1922c5aff182SThomas Petazzoni 	rx_done = 0;
1923c5aff182SThomas Petazzoni 
1924c5aff182SThomas Petazzoni 	/* Fairness NAPI loop */
1925c5aff182SThomas Petazzoni 	while (rx_done < rx_todo) {
1926c5aff182SThomas Petazzoni 		struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1927c5aff182SThomas Petazzoni 		struct sk_buff *skb;
19288ec2cd48Swilly tarreau 		unsigned char *data;
1929daf158d0SSimon Guinot 		dma_addr_t phys_addr;
1930dc35a10fSMarcin Wojtas 		u32 rx_status, frag_size;
1931f88bee1cSGregory CLEMENT 		int rx_bytes, err, index;
1932c5aff182SThomas Petazzoni 
1933c5aff182SThomas Petazzoni 		rx_done++;
1934c5aff182SThomas Petazzoni 		rx_status = rx_desc->status;
1935f19fadfcSwilly tarreau 		rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
1936f88bee1cSGregory CLEMENT 		index = rx_desc - rxq->descs;
1937f88bee1cSGregory CLEMENT 		data = rxq->buf_virt_addr[index];
1938daf158d0SSimon Guinot 		phys_addr = rx_desc->buf_phys_addr;
1939c5aff182SThomas Petazzoni 
19405428213cSwilly tarreau 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1941f19fadfcSwilly tarreau 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
19422eecb2e0SYelena Krivosheev 			mvneta_rx_error(pp, rx_desc);
1943f19fadfcSwilly tarreau err_drop_frame:
1944c5aff182SThomas Petazzoni 			dev->stats.rx_errors++;
19458ec2cd48Swilly tarreau 			/* leave the descriptor untouched */
1946c5aff182SThomas Petazzoni 			continue;
1947c5aff182SThomas Petazzoni 		}
1948c5aff182SThomas Petazzoni 
1949f19fadfcSwilly tarreau 		if (rx_bytes <= rx_copybreak) {
1950f19fadfcSwilly tarreau 		/* better copy a small frame and not unmap the DMA region */
1951f19fadfcSwilly tarreau 			skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1952f19fadfcSwilly tarreau 			if (unlikely(!skb))
1953f19fadfcSwilly tarreau 				goto err_drop_frame;
1954f19fadfcSwilly tarreau 
1955f19fadfcSwilly tarreau 			dma_sync_single_range_for_cpu(dev->dev.parent,
1956ac83b7ddSGregory CLEMENT 						      phys_addr,
1957f19fadfcSwilly tarreau 						      MVNETA_MH_SIZE + NET_SKB_PAD,
1958f19fadfcSwilly tarreau 						      rx_bytes,
1959f19fadfcSwilly tarreau 						      DMA_FROM_DEVICE);
196059ae1d12SJohannes Berg 			skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
1961f19fadfcSwilly tarreau 				     rx_bytes);
1962f19fadfcSwilly tarreau 
1963f19fadfcSwilly tarreau 			skb->protocol = eth_type_trans(skb, dev);
1964f19fadfcSwilly tarreau 			mvneta_rx_csum(pp, rx_status, skb);
196512bb03b4SMaxime Ripard 			napi_gro_receive(&port->napi, skb);
1966f19fadfcSwilly tarreau 
1967f19fadfcSwilly tarreau 			rcvd_pkts++;
1968f19fadfcSwilly tarreau 			rcvd_bytes += rx_bytes;
1969f19fadfcSwilly tarreau 
1970f19fadfcSwilly tarreau 			/* leave the descriptor and buffer untouched */
1971f19fadfcSwilly tarreau 			continue;
1972f19fadfcSwilly tarreau 		}
1973f19fadfcSwilly tarreau 
1974a84e3289SSimon Guinot 		/* Refill processing */
1975f88bee1cSGregory CLEMENT 		err = mvneta_rx_refill(pp, rx_desc, rxq);
1976a84e3289SSimon Guinot 		if (err) {
1977a84e3289SSimon Guinot 			netdev_err(dev, "Linux processing - Can't refill\n");
1978a84e3289SSimon Guinot 			rxq->missed++;
1979a84e3289SSimon Guinot 			goto err_drop_frame;
1980a84e3289SSimon Guinot 		}
1981a84e3289SSimon Guinot 
1982dc35a10fSMarcin Wojtas 		frag_size = pp->frag_size;
1983dc35a10fSMarcin Wojtas 
1984dc35a10fSMarcin Wojtas 		skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
1985f19fadfcSwilly tarreau 
198626c17a17SMarcin Wojtas 		/* After refill old buffer has to be unmapped regardless
198726c17a17SMarcin Wojtas 		 * the skb is successfully built or not.
198826c17a17SMarcin Wojtas 		 */
1989daf158d0SSimon Guinot 		dma_unmap_single(dev->dev.parent, phys_addr,
1990dc35a10fSMarcin Wojtas 				 MVNETA_RX_BUF_SIZE(pp->pkt_size),
1991dc35a10fSMarcin Wojtas 				 DMA_FROM_DEVICE);
1992c5aff182SThomas Petazzoni 
199326c17a17SMarcin Wojtas 		if (!skb)
199426c17a17SMarcin Wojtas 			goto err_drop_frame;
199526c17a17SMarcin Wojtas 
1996dc4277ddSwilly tarreau 		rcvd_pkts++;
1997dc4277ddSwilly tarreau 		rcvd_bytes += rx_bytes;
1998c5aff182SThomas Petazzoni 
1999c5aff182SThomas Petazzoni 		/* Linux processing */
20008ec2cd48Swilly tarreau 		skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2001c5aff182SThomas Petazzoni 		skb_put(skb, rx_bytes);
2002c5aff182SThomas Petazzoni 
2003c5aff182SThomas Petazzoni 		skb->protocol = eth_type_trans(skb, dev);
2004c5aff182SThomas Petazzoni 
20055428213cSwilly tarreau 		mvneta_rx_csum(pp, rx_status, skb);
2006c5aff182SThomas Petazzoni 
200712bb03b4SMaxime Ripard 		napi_gro_receive(&port->napi, skb);
2008c5aff182SThomas Petazzoni 	}
2009c5aff182SThomas Petazzoni 
2010dc4277ddSwilly tarreau 	if (rcvd_pkts) {
201174c41b04Swilly tarreau 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
201274c41b04Swilly tarreau 
201374c41b04Swilly tarreau 		u64_stats_update_begin(&stats->syncp);
201474c41b04Swilly tarreau 		stats->rx_packets += rcvd_pkts;
201574c41b04Swilly tarreau 		stats->rx_bytes   += rcvd_bytes;
201674c41b04Swilly tarreau 		u64_stats_update_end(&stats->syncp);
2017dc4277ddSwilly tarreau 	}
2018dc4277ddSwilly tarreau 
2019c5aff182SThomas Petazzoni 	/* Update rxq management counters */
2020a84e3289SSimon Guinot 	mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2021c5aff182SThomas Petazzoni 
2022c5aff182SThomas Petazzoni 	return rx_done;
2023c5aff182SThomas Petazzoni }
2024c5aff182SThomas Petazzoni 
2025dc35a10fSMarcin Wojtas /* Main rx processing when using hardware buffer management */
2026dc35a10fSMarcin Wojtas static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2027dc35a10fSMarcin Wojtas 			  struct mvneta_rx_queue *rxq)
2028dc35a10fSMarcin Wojtas {
2029dc35a10fSMarcin Wojtas 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2030dc35a10fSMarcin Wojtas 	struct net_device *dev = pp->dev;
2031dc35a10fSMarcin Wojtas 	int rx_done;
2032dc35a10fSMarcin Wojtas 	u32 rcvd_pkts = 0;
2033dc35a10fSMarcin Wojtas 	u32 rcvd_bytes = 0;
2034dc35a10fSMarcin Wojtas 
2035dc35a10fSMarcin Wojtas 	/* Get number of received packets */
2036dc35a10fSMarcin Wojtas 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2037dc35a10fSMarcin Wojtas 
2038dc35a10fSMarcin Wojtas 	if (rx_todo > rx_done)
2039dc35a10fSMarcin Wojtas 		rx_todo = rx_done;
2040dc35a10fSMarcin Wojtas 
2041dc35a10fSMarcin Wojtas 	rx_done = 0;
2042dc35a10fSMarcin Wojtas 
2043dc35a10fSMarcin Wojtas 	/* Fairness NAPI loop */
2044dc35a10fSMarcin Wojtas 	while (rx_done < rx_todo) {
2045dc35a10fSMarcin Wojtas 		struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2046dc35a10fSMarcin Wojtas 		struct mvneta_bm_pool *bm_pool = NULL;
2047dc35a10fSMarcin Wojtas 		struct sk_buff *skb;
2048dc35a10fSMarcin Wojtas 		unsigned char *data;
2049dc35a10fSMarcin Wojtas 		dma_addr_t phys_addr;
2050dc35a10fSMarcin Wojtas 		u32 rx_status, frag_size;
2051dc35a10fSMarcin Wojtas 		int rx_bytes, err;
2052dc35a10fSMarcin Wojtas 		u8 pool_id;
2053dc35a10fSMarcin Wojtas 
2054dc35a10fSMarcin Wojtas 		rx_done++;
2055dc35a10fSMarcin Wojtas 		rx_status = rx_desc->status;
2056dc35a10fSMarcin Wojtas 		rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2057f88bee1cSGregory CLEMENT 		data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
2058dc35a10fSMarcin Wojtas 		phys_addr = rx_desc->buf_phys_addr;
2059dc35a10fSMarcin Wojtas 		pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2060dc35a10fSMarcin Wojtas 		bm_pool = &pp->bm_priv->bm_pools[pool_id];
2061dc35a10fSMarcin Wojtas 
2062dc35a10fSMarcin Wojtas 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2063dc35a10fSMarcin Wojtas 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2064dc35a10fSMarcin Wojtas err_drop_frame_ret_pool:
2065dc35a10fSMarcin Wojtas 			/* Return the buffer to the pool */
2066dc35a10fSMarcin Wojtas 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2067dc35a10fSMarcin Wojtas 					      rx_desc->buf_phys_addr);
2068dc35a10fSMarcin Wojtas err_drop_frame:
2069dc35a10fSMarcin Wojtas 			dev->stats.rx_errors++;
2070dc35a10fSMarcin Wojtas 			mvneta_rx_error(pp, rx_desc);
2071dc35a10fSMarcin Wojtas 			/* leave the descriptor untouched */
2072dc35a10fSMarcin Wojtas 			continue;
2073dc35a10fSMarcin Wojtas 		}
2074dc35a10fSMarcin Wojtas 
2075dc35a10fSMarcin Wojtas 		if (rx_bytes <= rx_copybreak) {
2076dc35a10fSMarcin Wojtas 			/* better copy a small frame and not unmap the DMA region */
2077dc35a10fSMarcin Wojtas 			skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2078dc35a10fSMarcin Wojtas 			if (unlikely(!skb))
2079dc35a10fSMarcin Wojtas 				goto err_drop_frame_ret_pool;
2080dc35a10fSMarcin Wojtas 
2081dc35a10fSMarcin Wojtas 			dma_sync_single_range_for_cpu(dev->dev.parent,
2082dc35a10fSMarcin Wojtas 			                              rx_desc->buf_phys_addr,
2083dc35a10fSMarcin Wojtas 			                              MVNETA_MH_SIZE + NET_SKB_PAD,
2084dc35a10fSMarcin Wojtas 			                              rx_bytes,
2085dc35a10fSMarcin Wojtas 			                              DMA_FROM_DEVICE);
208659ae1d12SJohannes Berg 			skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2087dc35a10fSMarcin Wojtas 				     rx_bytes);
2088dc35a10fSMarcin Wojtas 
2089dc35a10fSMarcin Wojtas 			skb->protocol = eth_type_trans(skb, dev);
2090dc35a10fSMarcin Wojtas 			mvneta_rx_csum(pp, rx_status, skb);
2091dc35a10fSMarcin Wojtas 			napi_gro_receive(&port->napi, skb);
2092dc35a10fSMarcin Wojtas 
2093dc35a10fSMarcin Wojtas 			rcvd_pkts++;
2094dc35a10fSMarcin Wojtas 			rcvd_bytes += rx_bytes;
2095dc35a10fSMarcin Wojtas 
2096dc35a10fSMarcin Wojtas 			/* Return the buffer to the pool */
2097dc35a10fSMarcin Wojtas 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2098dc35a10fSMarcin Wojtas 					      rx_desc->buf_phys_addr);
2099dc35a10fSMarcin Wojtas 
2100dc35a10fSMarcin Wojtas 			/* leave the descriptor and buffer untouched */
2101dc35a10fSMarcin Wojtas 			continue;
2102dc35a10fSMarcin Wojtas 		}
2103dc35a10fSMarcin Wojtas 
2104dc35a10fSMarcin Wojtas 		/* Refill processing */
2105baa11ebcSGregory CLEMENT 		err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
2106dc35a10fSMarcin Wojtas 		if (err) {
2107dc35a10fSMarcin Wojtas 			netdev_err(dev, "Linux processing - Can't refill\n");
2108dc35a10fSMarcin Wojtas 			rxq->missed++;
2109dc35a10fSMarcin Wojtas 			goto err_drop_frame_ret_pool;
2110dc35a10fSMarcin Wojtas 		}
2111dc35a10fSMarcin Wojtas 
2112baa11ebcSGregory CLEMENT 		frag_size = bm_pool->hwbm_pool.frag_size;
2113dc35a10fSMarcin Wojtas 
2114dc35a10fSMarcin Wojtas 		skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2115dc35a10fSMarcin Wojtas 
2116dc35a10fSMarcin Wojtas 		/* After refill old buffer has to be unmapped regardless
2117dc35a10fSMarcin Wojtas 		 * the skb is successfully built or not.
2118dc35a10fSMarcin Wojtas 		 */
2119dc35a10fSMarcin Wojtas 		dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2120dc35a10fSMarcin Wojtas 				 bm_pool->buf_size, DMA_FROM_DEVICE);
2121dc35a10fSMarcin Wojtas 		if (!skb)
2122dc35a10fSMarcin Wojtas 			goto err_drop_frame;
2123dc35a10fSMarcin Wojtas 
2124dc35a10fSMarcin Wojtas 		rcvd_pkts++;
2125dc35a10fSMarcin Wojtas 		rcvd_bytes += rx_bytes;
2126dc35a10fSMarcin Wojtas 
2127dc35a10fSMarcin Wojtas 		/* Linux processing */
2128dc35a10fSMarcin Wojtas 		skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2129dc35a10fSMarcin Wojtas 		skb_put(skb, rx_bytes);
2130dc35a10fSMarcin Wojtas 
2131dc35a10fSMarcin Wojtas 		skb->protocol = eth_type_trans(skb, dev);
2132dc35a10fSMarcin Wojtas 
2133dc35a10fSMarcin Wojtas 		mvneta_rx_csum(pp, rx_status, skb);
2134dc35a10fSMarcin Wojtas 
2135dc35a10fSMarcin Wojtas 		napi_gro_receive(&port->napi, skb);
2136dc35a10fSMarcin Wojtas 	}
2137dc35a10fSMarcin Wojtas 
2138dc35a10fSMarcin Wojtas 	if (rcvd_pkts) {
2139dc35a10fSMarcin Wojtas 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2140dc35a10fSMarcin Wojtas 
2141dc35a10fSMarcin Wojtas 		u64_stats_update_begin(&stats->syncp);
2142dc35a10fSMarcin Wojtas 		stats->rx_packets += rcvd_pkts;
2143dc35a10fSMarcin Wojtas 		stats->rx_bytes   += rcvd_bytes;
2144dc35a10fSMarcin Wojtas 		u64_stats_update_end(&stats->syncp);
2145dc35a10fSMarcin Wojtas 	}
2146dc35a10fSMarcin Wojtas 
2147dc35a10fSMarcin Wojtas 	/* Update rxq management counters */
2148dc35a10fSMarcin Wojtas 	mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2149dc35a10fSMarcin Wojtas 
2150dc35a10fSMarcin Wojtas 	return rx_done;
2151dc35a10fSMarcin Wojtas }
2152dc35a10fSMarcin Wojtas 
21532adb719dSEzequiel Garcia static inline void
21542adb719dSEzequiel Garcia mvneta_tso_put_hdr(struct sk_buff *skb,
21552adb719dSEzequiel Garcia 		   struct mvneta_port *pp, struct mvneta_tx_queue *txq)
21562adb719dSEzequiel Garcia {
21572adb719dSEzequiel Garcia 	struct mvneta_tx_desc *tx_desc;
21582adb719dSEzequiel Garcia 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
21592adb719dSEzequiel Garcia 
21602adb719dSEzequiel Garcia 	txq->tx_skb[txq->txq_put_index] = NULL;
21612adb719dSEzequiel Garcia 	tx_desc = mvneta_txq_next_desc_get(txq);
21622adb719dSEzequiel Garcia 	tx_desc->data_size = hdr_len;
21632adb719dSEzequiel Garcia 	tx_desc->command = mvneta_skb_tx_csum(pp, skb);
21642adb719dSEzequiel Garcia 	tx_desc->command |= MVNETA_TXD_F_DESC;
21652adb719dSEzequiel Garcia 	tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
21662adb719dSEzequiel Garcia 				 txq->txq_put_index * TSO_HEADER_SIZE;
21672adb719dSEzequiel Garcia 	mvneta_txq_inc_put(txq);
21682adb719dSEzequiel Garcia }
21692adb719dSEzequiel Garcia 
21702adb719dSEzequiel Garcia static inline int
21712adb719dSEzequiel Garcia mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
21722adb719dSEzequiel Garcia 		    struct sk_buff *skb, char *data, int size,
21732adb719dSEzequiel Garcia 		    bool last_tcp, bool is_last)
21742adb719dSEzequiel Garcia {
21752adb719dSEzequiel Garcia 	struct mvneta_tx_desc *tx_desc;
21762adb719dSEzequiel Garcia 
21772adb719dSEzequiel Garcia 	tx_desc = mvneta_txq_next_desc_get(txq);
21782adb719dSEzequiel Garcia 	tx_desc->data_size = size;
21792adb719dSEzequiel Garcia 	tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
21802adb719dSEzequiel Garcia 						size, DMA_TO_DEVICE);
21812adb719dSEzequiel Garcia 	if (unlikely(dma_mapping_error(dev->dev.parent,
21822adb719dSEzequiel Garcia 		     tx_desc->buf_phys_addr))) {
21832adb719dSEzequiel Garcia 		mvneta_txq_desc_put(txq);
21842adb719dSEzequiel Garcia 		return -ENOMEM;
21852adb719dSEzequiel Garcia 	}
21862adb719dSEzequiel Garcia 
21872adb719dSEzequiel Garcia 	tx_desc->command = 0;
21882adb719dSEzequiel Garcia 	txq->tx_skb[txq->txq_put_index] = NULL;
21892adb719dSEzequiel Garcia 
21902adb719dSEzequiel Garcia 	if (last_tcp) {
21912adb719dSEzequiel Garcia 		/* last descriptor in the TCP packet */
21922adb719dSEzequiel Garcia 		tx_desc->command = MVNETA_TXD_L_DESC;
21932adb719dSEzequiel Garcia 
21942adb719dSEzequiel Garcia 		/* last descriptor in SKB */
21952adb719dSEzequiel Garcia 		if (is_last)
21962adb719dSEzequiel Garcia 			txq->tx_skb[txq->txq_put_index] = skb;
21972adb719dSEzequiel Garcia 	}
21982adb719dSEzequiel Garcia 	mvneta_txq_inc_put(txq);
21992adb719dSEzequiel Garcia 	return 0;
22002adb719dSEzequiel Garcia }
22012adb719dSEzequiel Garcia 
22022adb719dSEzequiel Garcia static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
22032adb719dSEzequiel Garcia 			 struct mvneta_tx_queue *txq)
22042adb719dSEzequiel Garcia {
22052adb719dSEzequiel Garcia 	int total_len, data_left;
22062adb719dSEzequiel Garcia 	int desc_count = 0;
22072adb719dSEzequiel Garcia 	struct mvneta_port *pp = netdev_priv(dev);
22082adb719dSEzequiel Garcia 	struct tso_t tso;
22092adb719dSEzequiel Garcia 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
22102adb719dSEzequiel Garcia 	int i;
22112adb719dSEzequiel Garcia 
22122adb719dSEzequiel Garcia 	/* Count needed descriptors */
22132adb719dSEzequiel Garcia 	if ((txq->count + tso_count_descs(skb)) >= txq->size)
22142adb719dSEzequiel Garcia 		return 0;
22152adb719dSEzequiel Garcia 
22162adb719dSEzequiel Garcia 	if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
22172adb719dSEzequiel Garcia 		pr_info("*** Is this even  possible???!?!?\n");
22182adb719dSEzequiel Garcia 		return 0;
22192adb719dSEzequiel Garcia 	}
22202adb719dSEzequiel Garcia 
22212adb719dSEzequiel Garcia 	/* Initialize the TSO handler, and prepare the first payload */
22222adb719dSEzequiel Garcia 	tso_start(skb, &tso);
22232adb719dSEzequiel Garcia 
22242adb719dSEzequiel Garcia 	total_len = skb->len - hdr_len;
22252adb719dSEzequiel Garcia 	while (total_len > 0) {
22262adb719dSEzequiel Garcia 		char *hdr;
22272adb719dSEzequiel Garcia 
22282adb719dSEzequiel Garcia 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
22292adb719dSEzequiel Garcia 		total_len -= data_left;
22302adb719dSEzequiel Garcia 		desc_count++;
22312adb719dSEzequiel Garcia 
22322adb719dSEzequiel Garcia 		/* prepare packet headers: MAC + IP + TCP */
22332adb719dSEzequiel Garcia 		hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
22342adb719dSEzequiel Garcia 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
22352adb719dSEzequiel Garcia 
22362adb719dSEzequiel Garcia 		mvneta_tso_put_hdr(skb, pp, txq);
22372adb719dSEzequiel Garcia 
22382adb719dSEzequiel Garcia 		while (data_left > 0) {
22392adb719dSEzequiel Garcia 			int size;
22402adb719dSEzequiel Garcia 			desc_count++;
22412adb719dSEzequiel Garcia 
22422adb719dSEzequiel Garcia 			size = min_t(int, tso.size, data_left);
22432adb719dSEzequiel Garcia 
22442adb719dSEzequiel Garcia 			if (mvneta_tso_put_data(dev, txq, skb,
22452adb719dSEzequiel Garcia 						 tso.data, size,
22462adb719dSEzequiel Garcia 						 size == data_left,
22472adb719dSEzequiel Garcia 						 total_len == 0))
22482adb719dSEzequiel Garcia 				goto err_release;
22492adb719dSEzequiel Garcia 			data_left -= size;
22502adb719dSEzequiel Garcia 
22512adb719dSEzequiel Garcia 			tso_build_data(skb, &tso, size);
22522adb719dSEzequiel Garcia 		}
22532adb719dSEzequiel Garcia 	}
22542adb719dSEzequiel Garcia 
22552adb719dSEzequiel Garcia 	return desc_count;
22562adb719dSEzequiel Garcia 
22572adb719dSEzequiel Garcia err_release:
22582adb719dSEzequiel Garcia 	/* Release all used data descriptors; header descriptors must not
22592adb719dSEzequiel Garcia 	 * be DMA-unmapped.
22602adb719dSEzequiel Garcia 	 */
22612adb719dSEzequiel Garcia 	for (i = desc_count - 1; i >= 0; i--) {
22622adb719dSEzequiel Garcia 		struct mvneta_tx_desc *tx_desc = txq->descs + i;
22632e3173a3SEzequiel Garcia 		if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
22642adb719dSEzequiel Garcia 			dma_unmap_single(pp->dev->dev.parent,
22652adb719dSEzequiel Garcia 					 tx_desc->buf_phys_addr,
22662adb719dSEzequiel Garcia 					 tx_desc->data_size,
22672adb719dSEzequiel Garcia 					 DMA_TO_DEVICE);
22682adb719dSEzequiel Garcia 		mvneta_txq_desc_put(txq);
22692adb719dSEzequiel Garcia 	}
22702adb719dSEzequiel Garcia 	return 0;
22712adb719dSEzequiel Garcia }
22722adb719dSEzequiel Garcia 
2273c5aff182SThomas Petazzoni /* Handle tx fragmentation processing */
2274c5aff182SThomas Petazzoni static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2275c5aff182SThomas Petazzoni 				  struct mvneta_tx_queue *txq)
2276c5aff182SThomas Petazzoni {
2277c5aff182SThomas Petazzoni 	struct mvneta_tx_desc *tx_desc;
22783d4ea02fSEzequiel Garcia 	int i, nr_frags = skb_shinfo(skb)->nr_frags;
2279c5aff182SThomas Petazzoni 
22803d4ea02fSEzequiel Garcia 	for (i = 0; i < nr_frags; i++) {
2281c5aff182SThomas Petazzoni 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2282c5aff182SThomas Petazzoni 		void *addr = page_address(frag->page.p) + frag->page_offset;
2283c5aff182SThomas Petazzoni 
2284c5aff182SThomas Petazzoni 		tx_desc = mvneta_txq_next_desc_get(txq);
2285c5aff182SThomas Petazzoni 		tx_desc->data_size = frag->size;
2286c5aff182SThomas Petazzoni 
2287c5aff182SThomas Petazzoni 		tx_desc->buf_phys_addr =
2288c5aff182SThomas Petazzoni 			dma_map_single(pp->dev->dev.parent, addr,
2289c5aff182SThomas Petazzoni 				       tx_desc->data_size, DMA_TO_DEVICE);
2290c5aff182SThomas Petazzoni 
2291c5aff182SThomas Petazzoni 		if (dma_mapping_error(pp->dev->dev.parent,
2292c5aff182SThomas Petazzoni 				      tx_desc->buf_phys_addr)) {
2293c5aff182SThomas Petazzoni 			mvneta_txq_desc_put(txq);
2294c5aff182SThomas Petazzoni 			goto error;
2295c5aff182SThomas Petazzoni 		}
2296c5aff182SThomas Petazzoni 
22973d4ea02fSEzequiel Garcia 		if (i == nr_frags - 1) {
2298c5aff182SThomas Petazzoni 			/* Last descriptor */
2299c5aff182SThomas Petazzoni 			tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2300c5aff182SThomas Petazzoni 			txq->tx_skb[txq->txq_put_index] = skb;
2301c5aff182SThomas Petazzoni 		} else {
2302c5aff182SThomas Petazzoni 			/* Descriptor in the middle: Not First, Not Last */
2303c5aff182SThomas Petazzoni 			tx_desc->command = 0;
2304c5aff182SThomas Petazzoni 			txq->tx_skb[txq->txq_put_index] = NULL;
2305c5aff182SThomas Petazzoni 		}
23063d4ea02fSEzequiel Garcia 		mvneta_txq_inc_put(txq);
2307c5aff182SThomas Petazzoni 	}
2308c5aff182SThomas Petazzoni 
2309c5aff182SThomas Petazzoni 	return 0;
2310c5aff182SThomas Petazzoni 
2311c5aff182SThomas Petazzoni error:
2312c5aff182SThomas Petazzoni 	/* Release all descriptors that were used to map fragments of
23136a20c175SThomas Petazzoni 	 * this packet, as well as the corresponding DMA mappings
23146a20c175SThomas Petazzoni 	 */
2315c5aff182SThomas Petazzoni 	for (i = i - 1; i >= 0; i--) {
2316c5aff182SThomas Petazzoni 		tx_desc = txq->descs + i;
2317c5aff182SThomas Petazzoni 		dma_unmap_single(pp->dev->dev.parent,
2318c5aff182SThomas Petazzoni 				 tx_desc->buf_phys_addr,
2319c5aff182SThomas Petazzoni 				 tx_desc->data_size,
2320c5aff182SThomas Petazzoni 				 DMA_TO_DEVICE);
2321c5aff182SThomas Petazzoni 		mvneta_txq_desc_put(txq);
2322c5aff182SThomas Petazzoni 	}
2323c5aff182SThomas Petazzoni 
2324c5aff182SThomas Petazzoni 	return -ENOMEM;
2325c5aff182SThomas Petazzoni }
2326c5aff182SThomas Petazzoni 
2327c5aff182SThomas Petazzoni /* Main tx processing */
2328c5aff182SThomas Petazzoni static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2329c5aff182SThomas Petazzoni {
2330c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2331ee40a116SWilly Tarreau 	u16 txq_id = skb_get_queue_mapping(skb);
2332ee40a116SWilly Tarreau 	struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2333c5aff182SThomas Petazzoni 	struct mvneta_tx_desc *tx_desc;
23345f478b41SEric Dumazet 	int len = skb->len;
2335c5aff182SThomas Petazzoni 	int frags = 0;
2336c5aff182SThomas Petazzoni 	u32 tx_cmd;
2337c5aff182SThomas Petazzoni 
2338c5aff182SThomas Petazzoni 	if (!netif_running(dev))
2339c5aff182SThomas Petazzoni 		goto out;
2340c5aff182SThomas Petazzoni 
23412adb719dSEzequiel Garcia 	if (skb_is_gso(skb)) {
23422adb719dSEzequiel Garcia 		frags = mvneta_tx_tso(skb, dev, txq);
23432adb719dSEzequiel Garcia 		goto out;
23442adb719dSEzequiel Garcia 	}
23452adb719dSEzequiel Garcia 
2346c5aff182SThomas Petazzoni 	frags = skb_shinfo(skb)->nr_frags + 1;
2347c5aff182SThomas Petazzoni 
2348c5aff182SThomas Petazzoni 	/* Get a descriptor for the first part of the packet */
2349c5aff182SThomas Petazzoni 	tx_desc = mvneta_txq_next_desc_get(txq);
2350c5aff182SThomas Petazzoni 
2351c5aff182SThomas Petazzoni 	tx_cmd = mvneta_skb_tx_csum(pp, skb);
2352c5aff182SThomas Petazzoni 
2353c5aff182SThomas Petazzoni 	tx_desc->data_size = skb_headlen(skb);
2354c5aff182SThomas Petazzoni 
2355c5aff182SThomas Petazzoni 	tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2356c5aff182SThomas Petazzoni 						tx_desc->data_size,
2357c5aff182SThomas Petazzoni 						DMA_TO_DEVICE);
2358c5aff182SThomas Petazzoni 	if (unlikely(dma_mapping_error(dev->dev.parent,
2359c5aff182SThomas Petazzoni 				       tx_desc->buf_phys_addr))) {
2360c5aff182SThomas Petazzoni 		mvneta_txq_desc_put(txq);
2361c5aff182SThomas Petazzoni 		frags = 0;
2362c5aff182SThomas Petazzoni 		goto out;
2363c5aff182SThomas Petazzoni 	}
2364c5aff182SThomas Petazzoni 
2365c5aff182SThomas Petazzoni 	if (frags == 1) {
2366c5aff182SThomas Petazzoni 		/* First and Last descriptor */
2367c5aff182SThomas Petazzoni 		tx_cmd |= MVNETA_TXD_FLZ_DESC;
2368c5aff182SThomas Petazzoni 		tx_desc->command = tx_cmd;
2369c5aff182SThomas Petazzoni 		txq->tx_skb[txq->txq_put_index] = skb;
2370c5aff182SThomas Petazzoni 		mvneta_txq_inc_put(txq);
2371c5aff182SThomas Petazzoni 	} else {
2372c5aff182SThomas Petazzoni 		/* First but not Last */
2373c5aff182SThomas Petazzoni 		tx_cmd |= MVNETA_TXD_F_DESC;
2374c5aff182SThomas Petazzoni 		txq->tx_skb[txq->txq_put_index] = NULL;
2375c5aff182SThomas Petazzoni 		mvneta_txq_inc_put(txq);
2376c5aff182SThomas Petazzoni 		tx_desc->command = tx_cmd;
2377c5aff182SThomas Petazzoni 		/* Continue with other skb fragments */
2378c5aff182SThomas Petazzoni 		if (mvneta_tx_frag_process(pp, skb, txq)) {
2379c5aff182SThomas Petazzoni 			dma_unmap_single(dev->dev.parent,
2380c5aff182SThomas Petazzoni 					 tx_desc->buf_phys_addr,
2381c5aff182SThomas Petazzoni 					 tx_desc->data_size,
2382c5aff182SThomas Petazzoni 					 DMA_TO_DEVICE);
2383c5aff182SThomas Petazzoni 			mvneta_txq_desc_put(txq);
2384c5aff182SThomas Petazzoni 			frags = 0;
2385c5aff182SThomas Petazzoni 			goto out;
2386c5aff182SThomas Petazzoni 		}
2387c5aff182SThomas Petazzoni 	}
2388c5aff182SThomas Petazzoni 
2389e19d2ddaSEzequiel Garcia out:
2390e19d2ddaSEzequiel Garcia 	if (frags > 0) {
2391e19d2ddaSEzequiel Garcia 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2392e19d2ddaSEzequiel Garcia 		struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2393e19d2ddaSEzequiel Garcia 
2394a29b6235SMarcin Wojtas 		netdev_tx_sent_queue(nq, len);
2395a29b6235SMarcin Wojtas 
2396c5aff182SThomas Petazzoni 		txq->count += frags;
23978eef5f97SEzequiel Garcia 		if (txq->count >= txq->tx_stop_threshold)
2398c5aff182SThomas Petazzoni 			netif_tx_stop_queue(nq);
2399c5aff182SThomas Petazzoni 
24002a90f7e1SSimon Guinot 		if (!skb->xmit_more || netif_xmit_stopped(nq) ||
24012a90f7e1SSimon Guinot 		    txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
24022a90f7e1SSimon Guinot 			mvneta_txq_pend_desc_add(pp, txq, frags);
24032a90f7e1SSimon Guinot 		else
24042a90f7e1SSimon Guinot 			txq->pending += frags;
24052a90f7e1SSimon Guinot 
240674c41b04Swilly tarreau 		u64_stats_update_begin(&stats->syncp);
240774c41b04Swilly tarreau 		stats->tx_packets++;
24085f478b41SEric Dumazet 		stats->tx_bytes  += len;
240974c41b04Swilly tarreau 		u64_stats_update_end(&stats->syncp);
2410c5aff182SThomas Petazzoni 	} else {
2411c5aff182SThomas Petazzoni 		dev->stats.tx_dropped++;
2412c5aff182SThomas Petazzoni 		dev_kfree_skb_any(skb);
2413c5aff182SThomas Petazzoni 	}
2414c5aff182SThomas Petazzoni 
2415c5aff182SThomas Petazzoni 	return NETDEV_TX_OK;
2416c5aff182SThomas Petazzoni }
2417c5aff182SThomas Petazzoni 
2418c5aff182SThomas Petazzoni 
2419c5aff182SThomas Petazzoni /* Free tx resources, when resetting a port */
2420c5aff182SThomas Petazzoni static void mvneta_txq_done_force(struct mvneta_port *pp,
2421c5aff182SThomas Petazzoni 				  struct mvneta_tx_queue *txq)
2422c5aff182SThomas Petazzoni 
2423c5aff182SThomas Petazzoni {
2424a29b6235SMarcin Wojtas 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2425c5aff182SThomas Petazzoni 	int tx_done = txq->count;
2426c5aff182SThomas Petazzoni 
2427a29b6235SMarcin Wojtas 	mvneta_txq_bufs_free(pp, txq, tx_done, nq);
2428c5aff182SThomas Petazzoni 
2429c5aff182SThomas Petazzoni 	/* reset txq */
2430c5aff182SThomas Petazzoni 	txq->count = 0;
2431c5aff182SThomas Petazzoni 	txq->txq_put_index = 0;
2432c5aff182SThomas Petazzoni 	txq->txq_get_index = 0;
2433c5aff182SThomas Petazzoni }
2434c5aff182SThomas Petazzoni 
24356c498974Swilly tarreau /* Handle tx done - called in softirq context. The <cause_tx_done> argument
24366c498974Swilly tarreau  * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
24376c498974Swilly tarreau  */
24380713a86aSArnaud Ebalard static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
2439c5aff182SThomas Petazzoni {
2440c5aff182SThomas Petazzoni 	struct mvneta_tx_queue *txq;
2441c5aff182SThomas Petazzoni 	struct netdev_queue *nq;
2442c5aff182SThomas Petazzoni 
24436c498974Swilly tarreau 	while (cause_tx_done) {
2444c5aff182SThomas Petazzoni 		txq = mvneta_tx_done_policy(pp, cause_tx_done);
2445c5aff182SThomas Petazzoni 
2446c5aff182SThomas Petazzoni 		nq = netdev_get_tx_queue(pp->dev, txq->id);
2447c5aff182SThomas Petazzoni 		__netif_tx_lock(nq, smp_processor_id());
2448c5aff182SThomas Petazzoni 
24490713a86aSArnaud Ebalard 		if (txq->count)
24500713a86aSArnaud Ebalard 			mvneta_txq_done(pp, txq);
2451c5aff182SThomas Petazzoni 
2452c5aff182SThomas Petazzoni 		__netif_tx_unlock(nq);
2453c5aff182SThomas Petazzoni 		cause_tx_done &= ~((1 << txq->id));
2454c5aff182SThomas Petazzoni 	}
2455c5aff182SThomas Petazzoni }
2456c5aff182SThomas Petazzoni 
24576a20c175SThomas Petazzoni /* Compute crc8 of the specified address, using a unique algorithm ,
2458c5aff182SThomas Petazzoni  * according to hw spec, different than generic crc8 algorithm
2459c5aff182SThomas Petazzoni  */
2460c5aff182SThomas Petazzoni static int mvneta_addr_crc(unsigned char *addr)
2461c5aff182SThomas Petazzoni {
2462c5aff182SThomas Petazzoni 	int crc = 0;
2463c5aff182SThomas Petazzoni 	int i;
2464c5aff182SThomas Petazzoni 
2465c5aff182SThomas Petazzoni 	for (i = 0; i < ETH_ALEN; i++) {
2466c5aff182SThomas Petazzoni 		int j;
2467c5aff182SThomas Petazzoni 
2468c5aff182SThomas Petazzoni 		crc = (crc ^ addr[i]) << 8;
2469c5aff182SThomas Petazzoni 		for (j = 7; j >= 0; j--) {
2470c5aff182SThomas Petazzoni 			if (crc & (0x100 << j))
2471c5aff182SThomas Petazzoni 				crc ^= 0x107 << j;
2472c5aff182SThomas Petazzoni 		}
2473c5aff182SThomas Petazzoni 	}
2474c5aff182SThomas Petazzoni 
2475c5aff182SThomas Petazzoni 	return crc;
2476c5aff182SThomas Petazzoni }
2477c5aff182SThomas Petazzoni 
2478c5aff182SThomas Petazzoni /* This method controls the net device special MAC multicast support.
2479c5aff182SThomas Petazzoni  * The Special Multicast Table for MAC addresses supports MAC of the form
2480c5aff182SThomas Petazzoni  * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2481c5aff182SThomas Petazzoni  * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2482c5aff182SThomas Petazzoni  * Table entries in the DA-Filter table. This method set the Special
2483c5aff182SThomas Petazzoni  * Multicast Table appropriate entry.
2484c5aff182SThomas Petazzoni  */
2485c5aff182SThomas Petazzoni static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2486c5aff182SThomas Petazzoni 					  unsigned char last_byte,
2487c5aff182SThomas Petazzoni 					  int queue)
2488c5aff182SThomas Petazzoni {
2489c5aff182SThomas Petazzoni 	unsigned int smc_table_reg;
2490c5aff182SThomas Petazzoni 	unsigned int tbl_offset;
2491c5aff182SThomas Petazzoni 	unsigned int reg_offset;
2492c5aff182SThomas Petazzoni 
2493c5aff182SThomas Petazzoni 	/* Register offset from SMC table base    */
2494c5aff182SThomas Petazzoni 	tbl_offset = (last_byte / 4);
2495c5aff182SThomas Petazzoni 	/* Entry offset within the above reg */
2496c5aff182SThomas Petazzoni 	reg_offset = last_byte % 4;
2497c5aff182SThomas Petazzoni 
2498c5aff182SThomas Petazzoni 	smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2499c5aff182SThomas Petazzoni 					+ tbl_offset * 4));
2500c5aff182SThomas Petazzoni 
2501c5aff182SThomas Petazzoni 	if (queue == -1)
2502c5aff182SThomas Petazzoni 		smc_table_reg &= ~(0xff << (8 * reg_offset));
2503c5aff182SThomas Petazzoni 	else {
2504c5aff182SThomas Petazzoni 		smc_table_reg &= ~(0xff << (8 * reg_offset));
2505c5aff182SThomas Petazzoni 		smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2506c5aff182SThomas Petazzoni 	}
2507c5aff182SThomas Petazzoni 
2508c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2509c5aff182SThomas Petazzoni 		    smc_table_reg);
2510c5aff182SThomas Petazzoni }
2511c5aff182SThomas Petazzoni 
2512c5aff182SThomas Petazzoni /* This method controls the network device Other MAC multicast support.
2513c5aff182SThomas Petazzoni  * The Other Multicast Table is used for multicast of another type.
2514c5aff182SThomas Petazzoni  * A CRC-8 is used as an index to the Other Multicast Table entries
2515c5aff182SThomas Petazzoni  * in the DA-Filter table.
2516c5aff182SThomas Petazzoni  * The method gets the CRC-8 value from the calling routine and
2517c5aff182SThomas Petazzoni  * sets the Other Multicast Table appropriate entry according to the
2518c5aff182SThomas Petazzoni  * specified CRC-8 .
2519c5aff182SThomas Petazzoni  */
2520c5aff182SThomas Petazzoni static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2521c5aff182SThomas Petazzoni 					unsigned char crc8,
2522c5aff182SThomas Petazzoni 					int queue)
2523c5aff182SThomas Petazzoni {
2524c5aff182SThomas Petazzoni 	unsigned int omc_table_reg;
2525c5aff182SThomas Petazzoni 	unsigned int tbl_offset;
2526c5aff182SThomas Petazzoni 	unsigned int reg_offset;
2527c5aff182SThomas Petazzoni 
2528c5aff182SThomas Petazzoni 	tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2529c5aff182SThomas Petazzoni 	reg_offset = crc8 % 4;	     /* Entry offset within the above reg   */
2530c5aff182SThomas Petazzoni 
2531c5aff182SThomas Petazzoni 	omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2532c5aff182SThomas Petazzoni 
2533c5aff182SThomas Petazzoni 	if (queue == -1) {
2534c5aff182SThomas Petazzoni 		/* Clear accepts frame bit at specified Other DA table entry */
2535c5aff182SThomas Petazzoni 		omc_table_reg &= ~(0xff << (8 * reg_offset));
2536c5aff182SThomas Petazzoni 	} else {
2537c5aff182SThomas Petazzoni 		omc_table_reg &= ~(0xff << (8 * reg_offset));
2538c5aff182SThomas Petazzoni 		omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2539c5aff182SThomas Petazzoni 	}
2540c5aff182SThomas Petazzoni 
2541c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2542c5aff182SThomas Petazzoni }
2543c5aff182SThomas Petazzoni 
2544c5aff182SThomas Petazzoni /* The network device supports multicast using two tables:
2545c5aff182SThomas Petazzoni  *    1) Special Multicast Table for MAC addresses of the form
2546c5aff182SThomas Petazzoni  *       0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2547c5aff182SThomas Petazzoni  *       The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2548c5aff182SThomas Petazzoni  *       Table entries in the DA-Filter table.
2549c5aff182SThomas Petazzoni  *    2) Other Multicast Table for multicast of another type. A CRC-8 value
2550c5aff182SThomas Petazzoni  *       is used as an index to the Other Multicast Table entries in the
2551c5aff182SThomas Petazzoni  *       DA-Filter table.
2552c5aff182SThomas Petazzoni  */
2553c5aff182SThomas Petazzoni static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2554c5aff182SThomas Petazzoni 				 int queue)
2555c5aff182SThomas Petazzoni {
2556c5aff182SThomas Petazzoni 	unsigned char crc_result = 0;
2557c5aff182SThomas Petazzoni 
2558c5aff182SThomas Petazzoni 	if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2559c5aff182SThomas Petazzoni 		mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2560c5aff182SThomas Petazzoni 		return 0;
2561c5aff182SThomas Petazzoni 	}
2562c5aff182SThomas Petazzoni 
2563c5aff182SThomas Petazzoni 	crc_result = mvneta_addr_crc(p_addr);
2564c5aff182SThomas Petazzoni 	if (queue == -1) {
2565c5aff182SThomas Petazzoni 		if (pp->mcast_count[crc_result] == 0) {
2566c5aff182SThomas Petazzoni 			netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2567c5aff182SThomas Petazzoni 				    crc_result);
2568c5aff182SThomas Petazzoni 			return -EINVAL;
2569c5aff182SThomas Petazzoni 		}
2570c5aff182SThomas Petazzoni 
2571c5aff182SThomas Petazzoni 		pp->mcast_count[crc_result]--;
2572c5aff182SThomas Petazzoni 		if (pp->mcast_count[crc_result] != 0) {
2573c5aff182SThomas Petazzoni 			netdev_info(pp->dev,
2574c5aff182SThomas Petazzoni 				    "After delete there are %d valid Mcast for crc8=0x%02x\n",
2575c5aff182SThomas Petazzoni 				    pp->mcast_count[crc_result], crc_result);
2576c5aff182SThomas Petazzoni 			return -EINVAL;
2577c5aff182SThomas Petazzoni 		}
2578c5aff182SThomas Petazzoni 	} else
2579c5aff182SThomas Petazzoni 		pp->mcast_count[crc_result]++;
2580c5aff182SThomas Petazzoni 
2581c5aff182SThomas Petazzoni 	mvneta_set_other_mcast_addr(pp, crc_result, queue);
2582c5aff182SThomas Petazzoni 
2583c5aff182SThomas Petazzoni 	return 0;
2584c5aff182SThomas Petazzoni }
2585c5aff182SThomas Petazzoni 
2586c5aff182SThomas Petazzoni /* Configure Fitering mode of Ethernet port */
2587c5aff182SThomas Petazzoni static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2588c5aff182SThomas Petazzoni 					  int is_promisc)
2589c5aff182SThomas Petazzoni {
2590c5aff182SThomas Petazzoni 	u32 port_cfg_reg, val;
2591c5aff182SThomas Petazzoni 
2592c5aff182SThomas Petazzoni 	port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2593c5aff182SThomas Petazzoni 
2594c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2595c5aff182SThomas Petazzoni 
2596c5aff182SThomas Petazzoni 	/* Set / Clear UPM bit in port configuration register */
2597c5aff182SThomas Petazzoni 	if (is_promisc) {
2598c5aff182SThomas Petazzoni 		/* Accept all Unicast addresses */
2599c5aff182SThomas Petazzoni 		port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2600c5aff182SThomas Petazzoni 		val |= MVNETA_FORCE_UNI;
2601c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2602c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2603c5aff182SThomas Petazzoni 	} else {
2604c5aff182SThomas Petazzoni 		/* Reject all Unicast addresses */
2605c5aff182SThomas Petazzoni 		port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2606c5aff182SThomas Petazzoni 		val &= ~MVNETA_FORCE_UNI;
2607c5aff182SThomas Petazzoni 	}
2608c5aff182SThomas Petazzoni 
2609c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2610c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2611c5aff182SThomas Petazzoni }
2612c5aff182SThomas Petazzoni 
2613c5aff182SThomas Petazzoni /* register unicast and multicast addresses */
2614c5aff182SThomas Petazzoni static void mvneta_set_rx_mode(struct net_device *dev)
2615c5aff182SThomas Petazzoni {
2616c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2617c5aff182SThomas Petazzoni 	struct netdev_hw_addr *ha;
2618c5aff182SThomas Petazzoni 
2619c5aff182SThomas Petazzoni 	if (dev->flags & IFF_PROMISC) {
2620c5aff182SThomas Petazzoni 		/* Accept all: Multicast + Unicast */
2621c5aff182SThomas Petazzoni 		mvneta_rx_unicast_promisc_set(pp, 1);
262290b74c01SGregory CLEMENT 		mvneta_set_ucast_table(pp, pp->rxq_def);
262390b74c01SGregory CLEMENT 		mvneta_set_special_mcast_table(pp, pp->rxq_def);
262490b74c01SGregory CLEMENT 		mvneta_set_other_mcast_table(pp, pp->rxq_def);
2625c5aff182SThomas Petazzoni 	} else {
2626c5aff182SThomas Petazzoni 		/* Accept single Unicast */
2627c5aff182SThomas Petazzoni 		mvneta_rx_unicast_promisc_set(pp, 0);
2628c5aff182SThomas Petazzoni 		mvneta_set_ucast_table(pp, -1);
262990b74c01SGregory CLEMENT 		mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
2630c5aff182SThomas Petazzoni 
2631c5aff182SThomas Petazzoni 		if (dev->flags & IFF_ALLMULTI) {
2632c5aff182SThomas Petazzoni 			/* Accept all multicast */
263390b74c01SGregory CLEMENT 			mvneta_set_special_mcast_table(pp, pp->rxq_def);
263490b74c01SGregory CLEMENT 			mvneta_set_other_mcast_table(pp, pp->rxq_def);
2635c5aff182SThomas Petazzoni 		} else {
2636c5aff182SThomas Petazzoni 			/* Accept only initialized multicast */
2637c5aff182SThomas Petazzoni 			mvneta_set_special_mcast_table(pp, -1);
2638c5aff182SThomas Petazzoni 			mvneta_set_other_mcast_table(pp, -1);
2639c5aff182SThomas Petazzoni 
2640c5aff182SThomas Petazzoni 			if (!netdev_mc_empty(dev)) {
2641c5aff182SThomas Petazzoni 				netdev_for_each_mc_addr(ha, dev) {
2642c5aff182SThomas Petazzoni 					mvneta_mcast_addr_set(pp, ha->addr,
264390b74c01SGregory CLEMENT 							      pp->rxq_def);
2644c5aff182SThomas Petazzoni 				}
2645c5aff182SThomas Petazzoni 			}
2646c5aff182SThomas Petazzoni 		}
2647c5aff182SThomas Petazzoni 	}
2648c5aff182SThomas Petazzoni }
2649c5aff182SThomas Petazzoni 
2650c5aff182SThomas Petazzoni /* Interrupt handling - the callback for request_irq() */
2651c5aff182SThomas Petazzoni static irqreturn_t mvneta_isr(int irq, void *dev_id)
2652c5aff182SThomas Petazzoni {
26532636ac3cSMarcin Wojtas 	struct mvneta_port *pp = (struct mvneta_port *)dev_id;
26542636ac3cSMarcin Wojtas 
26552636ac3cSMarcin Wojtas 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
26562636ac3cSMarcin Wojtas 	napi_schedule(&pp->napi);
26572636ac3cSMarcin Wojtas 
26582636ac3cSMarcin Wojtas 	return IRQ_HANDLED;
26592636ac3cSMarcin Wojtas }
26602636ac3cSMarcin Wojtas 
26612636ac3cSMarcin Wojtas /* Interrupt handling - the callback for request_percpu_irq() */
26622636ac3cSMarcin Wojtas static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
26632636ac3cSMarcin Wojtas {
266412bb03b4SMaxime Ripard 	struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
2665c5aff182SThomas Petazzoni 
266612bb03b4SMaxime Ripard 	disable_percpu_irq(port->pp->dev->irq);
266712bb03b4SMaxime Ripard 	napi_schedule(&port->napi);
2668c5aff182SThomas Petazzoni 
2669c5aff182SThomas Petazzoni 	return IRQ_HANDLED;
2670c5aff182SThomas Petazzoni }
2671c5aff182SThomas Petazzoni 
2672503f9aa9SRussell King static void mvneta_link_change(struct mvneta_port *pp)
2673898b2970SStas Sergeev {
2674898b2970SStas Sergeev 	u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2675898b2970SStas Sergeev 
2676503f9aa9SRussell King 	phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
2677898b2970SStas Sergeev }
2678898b2970SStas Sergeev 
2679c5aff182SThomas Petazzoni /* NAPI handler
2680c5aff182SThomas Petazzoni  * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2681c5aff182SThomas Petazzoni  * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2682c5aff182SThomas Petazzoni  * Bits 8 -15 of the cause Rx Tx register indicate that are received
2683c5aff182SThomas Petazzoni  * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2684c5aff182SThomas Petazzoni  * Each CPU has its own causeRxTx register
2685c5aff182SThomas Petazzoni  */
2686c5aff182SThomas Petazzoni static int mvneta_poll(struct napi_struct *napi, int budget)
2687c5aff182SThomas Petazzoni {
2688c5aff182SThomas Petazzoni 	int rx_done = 0;
2689c5aff182SThomas Petazzoni 	u32 cause_rx_tx;
26902dcf75e2SGregory CLEMENT 	int rx_queue;
2691c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(napi->dev);
269212bb03b4SMaxime Ripard 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2693c5aff182SThomas Petazzoni 
2694c5aff182SThomas Petazzoni 	if (!netif_running(pp->dev)) {
26952636ac3cSMarcin Wojtas 		napi_complete(napi);
2696c5aff182SThomas Petazzoni 		return rx_done;
2697c5aff182SThomas Petazzoni 	}
2698c5aff182SThomas Petazzoni 
2699c5aff182SThomas Petazzoni 	/* Read cause register */
2700898b2970SStas Sergeev 	cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2701898b2970SStas Sergeev 	if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2702898b2970SStas Sergeev 		u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2703898b2970SStas Sergeev 
2704898b2970SStas Sergeev 		mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2705503f9aa9SRussell King 
2706503f9aa9SRussell King 		if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
2707856b2cc5SRussell King 				  MVNETA_CAUSE_LINK_CHANGE))
2708503f9aa9SRussell King 			mvneta_link_change(pp);
2709898b2970SStas Sergeev 	}
271071f6d1b3Swilly tarreau 
271171f6d1b3Swilly tarreau 	/* Release Tx descriptors */
271271f6d1b3Swilly tarreau 	if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
27130713a86aSArnaud Ebalard 		mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
271471f6d1b3Swilly tarreau 		cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
271571f6d1b3Swilly tarreau 	}
2716c5aff182SThomas Petazzoni 
27176a20c175SThomas Petazzoni 	/* For the case where the last mvneta_poll did not process all
2718c5aff182SThomas Petazzoni 	 * RX packets
2719c5aff182SThomas Petazzoni 	 */
27202dcf75e2SGregory CLEMENT 	rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
27212dcf75e2SGregory CLEMENT 
27222636ac3cSMarcin Wojtas 	cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
27232636ac3cSMarcin Wojtas 		port->cause_rx_tx;
27242dcf75e2SGregory CLEMENT 
27252dcf75e2SGregory CLEMENT 	if (rx_queue) {
27262dcf75e2SGregory CLEMENT 		rx_queue = rx_queue - 1;
2727dc35a10fSMarcin Wojtas 		if (pp->bm_priv)
2728dc35a10fSMarcin Wojtas 			rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2729dc35a10fSMarcin Wojtas 		else
2730dc35a10fSMarcin Wojtas 			rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
27312dcf75e2SGregory CLEMENT 	}
27322dcf75e2SGregory CLEMENT 
27336ad20165SEric Dumazet 	if (rx_done < budget) {
2734c5aff182SThomas Petazzoni 		cause_rx_tx = 0;
27356ad20165SEric Dumazet 		napi_complete_done(napi, rx_done);
27362636ac3cSMarcin Wojtas 
27372636ac3cSMarcin Wojtas 		if (pp->neta_armada3700) {
27382636ac3cSMarcin Wojtas 			unsigned long flags;
27392636ac3cSMarcin Wojtas 
27402636ac3cSMarcin Wojtas 			local_irq_save(flags);
27412636ac3cSMarcin Wojtas 			mvreg_write(pp, MVNETA_INTR_NEW_MASK,
27422636ac3cSMarcin Wojtas 				    MVNETA_RX_INTR_MASK(rxq_number) |
27432636ac3cSMarcin Wojtas 				    MVNETA_TX_INTR_MASK(txq_number) |
27442636ac3cSMarcin Wojtas 				    MVNETA_MISCINTR_INTR_MASK);
27452636ac3cSMarcin Wojtas 			local_irq_restore(flags);
27462636ac3cSMarcin Wojtas 		} else {
274712bb03b4SMaxime Ripard 			enable_percpu_irq(pp->dev->irq, 0);
2748c5aff182SThomas Petazzoni 		}
27492636ac3cSMarcin Wojtas 	}
2750c5aff182SThomas Petazzoni 
27512636ac3cSMarcin Wojtas 	if (pp->neta_armada3700)
27522636ac3cSMarcin Wojtas 		pp->cause_rx_tx = cause_rx_tx;
27532636ac3cSMarcin Wojtas 	else
275412bb03b4SMaxime Ripard 		port->cause_rx_tx = cause_rx_tx;
27552636ac3cSMarcin Wojtas 
2756c5aff182SThomas Petazzoni 	return rx_done;
2757c5aff182SThomas Petazzoni }
2758c5aff182SThomas Petazzoni 
2759c5aff182SThomas Petazzoni /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2760c5aff182SThomas Petazzoni static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2761c5aff182SThomas Petazzoni 			   int num)
2762c5aff182SThomas Petazzoni {
2763c5aff182SThomas Petazzoni 	int i;
2764c5aff182SThomas Petazzoni 
2765c5aff182SThomas Petazzoni 	for (i = 0; i < num; i++) {
2766a1a65ab1Swilly tarreau 		memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2767f88bee1cSGregory CLEMENT 		if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
2768a1a65ab1Swilly tarreau 			netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs  filled\n",
2769c5aff182SThomas Petazzoni 				__func__, rxq->id, i, num);
2770c5aff182SThomas Petazzoni 			break;
2771c5aff182SThomas Petazzoni 		}
2772c5aff182SThomas Petazzoni 	}
2773c5aff182SThomas Petazzoni 
2774c5aff182SThomas Petazzoni 	/* Add this number of RX descriptors as non occupied (ready to
27756a20c175SThomas Petazzoni 	 * get packets)
27766a20c175SThomas Petazzoni 	 */
2777c5aff182SThomas Petazzoni 	mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2778c5aff182SThomas Petazzoni 
2779c5aff182SThomas Petazzoni 	return i;
2780c5aff182SThomas Petazzoni }
2781c5aff182SThomas Petazzoni 
2782c5aff182SThomas Petazzoni /* Free all packets pending transmit from all TXQs and reset TX port */
2783c5aff182SThomas Petazzoni static void mvneta_tx_reset(struct mvneta_port *pp)
2784c5aff182SThomas Petazzoni {
2785c5aff182SThomas Petazzoni 	int queue;
2786c5aff182SThomas Petazzoni 
27879672850bSEzequiel Garcia 	/* free the skb's in the tx ring */
2788c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++)
2789c5aff182SThomas Petazzoni 		mvneta_txq_done_force(pp, &pp->txqs[queue]);
2790c5aff182SThomas Petazzoni 
2791c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2792c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2793c5aff182SThomas Petazzoni }
2794c5aff182SThomas Petazzoni 
2795c5aff182SThomas Petazzoni static void mvneta_rx_reset(struct mvneta_port *pp)
2796c5aff182SThomas Petazzoni {
2797c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2798c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2799c5aff182SThomas Petazzoni }
2800c5aff182SThomas Petazzoni 
2801c5aff182SThomas Petazzoni /* Rx/Tx queue initialization/cleanup methods */
2802c5aff182SThomas Petazzoni 
2803c5aff182SThomas Petazzoni /* Create a specified RX queue */
2804c5aff182SThomas Petazzoni static int mvneta_rxq_init(struct mvneta_port *pp,
2805c5aff182SThomas Petazzoni 			   struct mvneta_rx_queue *rxq)
2806c5aff182SThomas Petazzoni 
2807c5aff182SThomas Petazzoni {
2808c5aff182SThomas Petazzoni 	rxq->size = pp->rx_ring_size;
2809c5aff182SThomas Petazzoni 
2810c5aff182SThomas Petazzoni 	/* Allocate memory for RX descriptors */
2811c5aff182SThomas Petazzoni 	rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2812c5aff182SThomas Petazzoni 					rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2813c5aff182SThomas Petazzoni 					&rxq->descs_phys, GFP_KERNEL);
2814f95936ccSMarkus Elfring 	if (!rxq->descs)
2815c5aff182SThomas Petazzoni 		return -ENOMEM;
2816c5aff182SThomas Petazzoni 
2817c5aff182SThomas Petazzoni 	rxq->last_desc = rxq->size - 1;
2818c5aff182SThomas Petazzoni 
2819c5aff182SThomas Petazzoni 	/* Set Rx descriptors queue starting address */
2820c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2821c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2822c5aff182SThomas Petazzoni 
2823c5aff182SThomas Petazzoni 	/* Set Offset */
28248d5047cfSMarcin Wojtas 	mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
2825c5aff182SThomas Petazzoni 
2826c5aff182SThomas Petazzoni 	/* Set coalescing pkts and time */
2827c5aff182SThomas Petazzoni 	mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2828c5aff182SThomas Petazzoni 	mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2829c5aff182SThomas Petazzoni 
2830dc35a10fSMarcin Wojtas 	if (!pp->bm_priv) {
2831c5aff182SThomas Petazzoni 		/* Fill RXQ with buffers from RX pool */
2832dc35a10fSMarcin Wojtas 		mvneta_rxq_buf_size_set(pp, rxq,
2833dc35a10fSMarcin Wojtas 					MVNETA_RX_BUF_SIZE(pp->pkt_size));
2834c5aff182SThomas Petazzoni 		mvneta_rxq_bm_disable(pp, rxq);
2835e9f64999SGregory CLEMENT 		mvneta_rxq_fill(pp, rxq, rxq->size);
2836dc35a10fSMarcin Wojtas 	} else {
2837dc35a10fSMarcin Wojtas 		mvneta_rxq_bm_enable(pp, rxq);
2838dc35a10fSMarcin Wojtas 		mvneta_rxq_long_pool_set(pp, rxq);
2839dc35a10fSMarcin Wojtas 		mvneta_rxq_short_pool_set(pp, rxq);
2840e9f64999SGregory CLEMENT 		mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
2841dc35a10fSMarcin Wojtas 	}
2842dc35a10fSMarcin Wojtas 
2843c5aff182SThomas Petazzoni 	return 0;
2844c5aff182SThomas Petazzoni }
2845c5aff182SThomas Petazzoni 
2846c5aff182SThomas Petazzoni /* Cleanup Rx queue */
2847c5aff182SThomas Petazzoni static void mvneta_rxq_deinit(struct mvneta_port *pp,
2848c5aff182SThomas Petazzoni 			      struct mvneta_rx_queue *rxq)
2849c5aff182SThomas Petazzoni {
2850c5aff182SThomas Petazzoni 	mvneta_rxq_drop_pkts(pp, rxq);
2851c5aff182SThomas Petazzoni 
2852c5aff182SThomas Petazzoni 	if (rxq->descs)
2853c5aff182SThomas Petazzoni 		dma_free_coherent(pp->dev->dev.parent,
2854c5aff182SThomas Petazzoni 				  rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2855c5aff182SThomas Petazzoni 				  rxq->descs,
2856c5aff182SThomas Petazzoni 				  rxq->descs_phys);
2857c5aff182SThomas Petazzoni 
2858c5aff182SThomas Petazzoni 	rxq->descs             = NULL;
2859c5aff182SThomas Petazzoni 	rxq->last_desc         = 0;
2860c5aff182SThomas Petazzoni 	rxq->next_desc_to_proc = 0;
2861c5aff182SThomas Petazzoni 	rxq->descs_phys        = 0;
2862c5aff182SThomas Petazzoni }
2863c5aff182SThomas Petazzoni 
2864c5aff182SThomas Petazzoni /* Create and initialize a tx queue */
2865c5aff182SThomas Petazzoni static int mvneta_txq_init(struct mvneta_port *pp,
2866c5aff182SThomas Petazzoni 			   struct mvneta_tx_queue *txq)
2867c5aff182SThomas Petazzoni {
286850bf8cb6SGregory CLEMENT 	int cpu;
286950bf8cb6SGregory CLEMENT 
2870c5aff182SThomas Petazzoni 	txq->size = pp->tx_ring_size;
2871c5aff182SThomas Petazzoni 
28728eef5f97SEzequiel Garcia 	/* A queue must always have room for at least one skb.
28738eef5f97SEzequiel Garcia 	 * Therefore, stop the queue when the free entries reaches
28748eef5f97SEzequiel Garcia 	 * the maximum number of descriptors per skb.
28758eef5f97SEzequiel Garcia 	 */
28768eef5f97SEzequiel Garcia 	txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
28778eef5f97SEzequiel Garcia 	txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
28788eef5f97SEzequiel Garcia 
28798eef5f97SEzequiel Garcia 
2880c5aff182SThomas Petazzoni 	/* Allocate memory for TX descriptors */
2881c5aff182SThomas Petazzoni 	txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2882c5aff182SThomas Petazzoni 					txq->size * MVNETA_DESC_ALIGNED_SIZE,
2883c5aff182SThomas Petazzoni 					&txq->descs_phys, GFP_KERNEL);
2884f95936ccSMarkus Elfring 	if (!txq->descs)
2885c5aff182SThomas Petazzoni 		return -ENOMEM;
2886c5aff182SThomas Petazzoni 
2887c5aff182SThomas Petazzoni 	txq->last_desc = txq->size - 1;
2888c5aff182SThomas Petazzoni 
2889c5aff182SThomas Petazzoni 	/* Set maximum bandwidth for enabled TXQs */
2890c5aff182SThomas Petazzoni 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2891c5aff182SThomas Petazzoni 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2892c5aff182SThomas Petazzoni 
2893c5aff182SThomas Petazzoni 	/* Set Tx descriptors queue starting address */
2894c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2895c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2896c5aff182SThomas Petazzoni 
2897d441b688SMarkus Elfring 	txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
2898d441b688SMarkus Elfring 				    GFP_KERNEL);
2899f95936ccSMarkus Elfring 	if (!txq->tx_skb) {
2900c5aff182SThomas Petazzoni 		dma_free_coherent(pp->dev->dev.parent,
2901c5aff182SThomas Petazzoni 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
2902c5aff182SThomas Petazzoni 				  txq->descs, txq->descs_phys);
2903c5aff182SThomas Petazzoni 		return -ENOMEM;
2904c5aff182SThomas Petazzoni 	}
29052adb719dSEzequiel Garcia 
29062adb719dSEzequiel Garcia 	/* Allocate DMA buffers for TSO MAC/IP/TCP headers */
29072adb719dSEzequiel Garcia 	txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
29082adb719dSEzequiel Garcia 					   txq->size * TSO_HEADER_SIZE,
29092adb719dSEzequiel Garcia 					   &txq->tso_hdrs_phys, GFP_KERNEL);
2910f95936ccSMarkus Elfring 	if (!txq->tso_hdrs) {
29112adb719dSEzequiel Garcia 		kfree(txq->tx_skb);
29122adb719dSEzequiel Garcia 		dma_free_coherent(pp->dev->dev.parent,
29132adb719dSEzequiel Garcia 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
29142adb719dSEzequiel Garcia 				  txq->descs, txq->descs_phys);
29152adb719dSEzequiel Garcia 		return -ENOMEM;
29162adb719dSEzequiel Garcia 	}
2917c5aff182SThomas Petazzoni 	mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2918c5aff182SThomas Petazzoni 
291950bf8cb6SGregory CLEMENT 	/* Setup XPS mapping */
292050bf8cb6SGregory CLEMENT 	if (txq_number > 1)
292150bf8cb6SGregory CLEMENT 		cpu = txq->id % num_present_cpus();
292250bf8cb6SGregory CLEMENT 	else
292350bf8cb6SGregory CLEMENT 		cpu = pp->rxq_def % num_present_cpus();
292450bf8cb6SGregory CLEMENT 	cpumask_set_cpu(cpu, &txq->affinity_mask);
292550bf8cb6SGregory CLEMENT 	netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
292650bf8cb6SGregory CLEMENT 
2927c5aff182SThomas Petazzoni 	return 0;
2928c5aff182SThomas Petazzoni }
2929c5aff182SThomas Petazzoni 
2930c5aff182SThomas Petazzoni /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2931c5aff182SThomas Petazzoni static void mvneta_txq_deinit(struct mvneta_port *pp,
2932c5aff182SThomas Petazzoni 			      struct mvneta_tx_queue *txq)
2933c5aff182SThomas Petazzoni {
2934a29b6235SMarcin Wojtas 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2935a29b6235SMarcin Wojtas 
2936c5aff182SThomas Petazzoni 	kfree(txq->tx_skb);
2937c5aff182SThomas Petazzoni 
29382adb719dSEzequiel Garcia 	if (txq->tso_hdrs)
29392adb719dSEzequiel Garcia 		dma_free_coherent(pp->dev->dev.parent,
29402adb719dSEzequiel Garcia 				  txq->size * TSO_HEADER_SIZE,
29412adb719dSEzequiel Garcia 				  txq->tso_hdrs, txq->tso_hdrs_phys);
2942c5aff182SThomas Petazzoni 	if (txq->descs)
2943c5aff182SThomas Petazzoni 		dma_free_coherent(pp->dev->dev.parent,
2944c5aff182SThomas Petazzoni 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
2945c5aff182SThomas Petazzoni 				  txq->descs, txq->descs_phys);
2946c5aff182SThomas Petazzoni 
2947a29b6235SMarcin Wojtas 	netdev_tx_reset_queue(nq);
2948a29b6235SMarcin Wojtas 
2949c5aff182SThomas Petazzoni 	txq->descs             = NULL;
2950c5aff182SThomas Petazzoni 	txq->last_desc         = 0;
2951c5aff182SThomas Petazzoni 	txq->next_desc_to_proc = 0;
2952c5aff182SThomas Petazzoni 	txq->descs_phys        = 0;
2953c5aff182SThomas Petazzoni 
2954c5aff182SThomas Petazzoni 	/* Set minimum bandwidth for disabled TXQs */
2955c5aff182SThomas Petazzoni 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2956c5aff182SThomas Petazzoni 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2957c5aff182SThomas Petazzoni 
2958c5aff182SThomas Petazzoni 	/* Set Tx descriptors queue starting address and size */
2959c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2960c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2961c5aff182SThomas Petazzoni }
2962c5aff182SThomas Petazzoni 
2963c5aff182SThomas Petazzoni /* Cleanup all Tx queues */
2964c5aff182SThomas Petazzoni static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2965c5aff182SThomas Petazzoni {
2966c5aff182SThomas Petazzoni 	int queue;
2967c5aff182SThomas Petazzoni 
2968c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++)
2969c5aff182SThomas Petazzoni 		mvneta_txq_deinit(pp, &pp->txqs[queue]);
2970c5aff182SThomas Petazzoni }
2971c5aff182SThomas Petazzoni 
2972c5aff182SThomas Petazzoni /* Cleanup all Rx queues */
2973c5aff182SThomas Petazzoni static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2974c5aff182SThomas Petazzoni {
29752dcf75e2SGregory CLEMENT 	int queue;
29762dcf75e2SGregory CLEMENT 
2977ca5902a6SYelena Krivosheev 	for (queue = 0; queue < rxq_number; queue++)
29782dcf75e2SGregory CLEMENT 		mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
2979c5aff182SThomas Petazzoni }
2980c5aff182SThomas Petazzoni 
2981c5aff182SThomas Petazzoni 
2982c5aff182SThomas Petazzoni /* Init all Rx queues */
2983c5aff182SThomas Petazzoni static int mvneta_setup_rxqs(struct mvneta_port *pp)
2984c5aff182SThomas Petazzoni {
29852dcf75e2SGregory CLEMENT 	int queue;
29862dcf75e2SGregory CLEMENT 
29872dcf75e2SGregory CLEMENT 	for (queue = 0; queue < rxq_number; queue++) {
29882dcf75e2SGregory CLEMENT 		int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
29892dcf75e2SGregory CLEMENT 
2990c5aff182SThomas Petazzoni 		if (err) {
2991c5aff182SThomas Petazzoni 			netdev_err(pp->dev, "%s: can't create rxq=%d\n",
29922dcf75e2SGregory CLEMENT 				   __func__, queue);
2993c5aff182SThomas Petazzoni 			mvneta_cleanup_rxqs(pp);
2994c5aff182SThomas Petazzoni 			return err;
2995c5aff182SThomas Petazzoni 		}
29962dcf75e2SGregory CLEMENT 	}
2997c5aff182SThomas Petazzoni 
2998c5aff182SThomas Petazzoni 	return 0;
2999c5aff182SThomas Petazzoni }
3000c5aff182SThomas Petazzoni 
3001c5aff182SThomas Petazzoni /* Init all tx queues */
3002c5aff182SThomas Petazzoni static int mvneta_setup_txqs(struct mvneta_port *pp)
3003c5aff182SThomas Petazzoni {
3004c5aff182SThomas Petazzoni 	int queue;
3005c5aff182SThomas Petazzoni 
3006c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
3007c5aff182SThomas Petazzoni 		int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3008c5aff182SThomas Petazzoni 		if (err) {
3009c5aff182SThomas Petazzoni 			netdev_err(pp->dev, "%s: can't create txq=%d\n",
3010c5aff182SThomas Petazzoni 				   __func__, queue);
3011c5aff182SThomas Petazzoni 			mvneta_cleanup_txqs(pp);
3012c5aff182SThomas Petazzoni 			return err;
3013c5aff182SThomas Petazzoni 		}
3014c5aff182SThomas Petazzoni 	}
3015c5aff182SThomas Petazzoni 
3016c5aff182SThomas Petazzoni 	return 0;
3017c5aff182SThomas Petazzoni }
3018c5aff182SThomas Petazzoni 
3019c5aff182SThomas Petazzoni static void mvneta_start_dev(struct mvneta_port *pp)
3020c5aff182SThomas Petazzoni {
30216b125d63SGregory CLEMENT 	int cpu;
302212bb03b4SMaxime Ripard 
3023c5aff182SThomas Petazzoni 	mvneta_max_rx_size_set(pp, pp->pkt_size);
3024c5aff182SThomas Petazzoni 	mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3025c5aff182SThomas Petazzoni 
3026c5aff182SThomas Petazzoni 	/* start the Rx/Tx activity */
3027c5aff182SThomas Petazzoni 	mvneta_port_enable(pp);
3028c5aff182SThomas Petazzoni 
30292636ac3cSMarcin Wojtas 	if (!pp->neta_armada3700) {
3030c5aff182SThomas Petazzoni 		/* Enable polling on the port */
3031129219e4SGregory CLEMENT 		for_each_online_cpu(cpu) {
30322636ac3cSMarcin Wojtas 			struct mvneta_pcpu_port *port =
30332636ac3cSMarcin Wojtas 				per_cpu_ptr(pp->ports, cpu);
303412bb03b4SMaxime Ripard 
303512bb03b4SMaxime Ripard 			napi_enable(&port->napi);
303612bb03b4SMaxime Ripard 		}
30372636ac3cSMarcin Wojtas 	} else {
30382636ac3cSMarcin Wojtas 		napi_enable(&pp->napi);
30392636ac3cSMarcin Wojtas 	}
3040c5aff182SThomas Petazzoni 
30412dcf75e2SGregory CLEMENT 	/* Unmask interrupts. It has to be done from each CPU */
30426b125d63SGregory CLEMENT 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
30436b125d63SGregory CLEMENT 
3044898b2970SStas Sergeev 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3045898b2970SStas Sergeev 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
3046856b2cc5SRussell King 		    MVNETA_CAUSE_LINK_CHANGE);
3047c5aff182SThomas Petazzoni 
3048503f9aa9SRussell King 	phylink_start(pp->phylink);
3049c5aff182SThomas Petazzoni 	netif_tx_start_all_queues(pp->dev);
3050c5aff182SThomas Petazzoni }
3051c5aff182SThomas Petazzoni 
3052c5aff182SThomas Petazzoni static void mvneta_stop_dev(struct mvneta_port *pp)
3053c5aff182SThomas Petazzoni {
305412bb03b4SMaxime Ripard 	unsigned int cpu;
305512bb03b4SMaxime Ripard 
3056503f9aa9SRussell King 	phylink_stop(pp->phylink);
3057c5aff182SThomas Petazzoni 
30582636ac3cSMarcin Wojtas 	if (!pp->neta_armada3700) {
3059129219e4SGregory CLEMENT 		for_each_online_cpu(cpu) {
30602636ac3cSMarcin Wojtas 			struct mvneta_pcpu_port *port =
30612636ac3cSMarcin Wojtas 				per_cpu_ptr(pp->ports, cpu);
306212bb03b4SMaxime Ripard 
306312bb03b4SMaxime Ripard 			napi_disable(&port->napi);
306412bb03b4SMaxime Ripard 		}
30652636ac3cSMarcin Wojtas 	} else {
30662636ac3cSMarcin Wojtas 		napi_disable(&pp->napi);
30672636ac3cSMarcin Wojtas 	}
3068c5aff182SThomas Petazzoni 
3069c5aff182SThomas Petazzoni 	netif_carrier_off(pp->dev);
3070c5aff182SThomas Petazzoni 
3071c5aff182SThomas Petazzoni 	mvneta_port_down(pp);
3072c5aff182SThomas Petazzoni 	netif_tx_stop_all_queues(pp->dev);
3073c5aff182SThomas Petazzoni 
3074c5aff182SThomas Petazzoni 	/* Stop the port activity */
3075c5aff182SThomas Petazzoni 	mvneta_port_disable(pp);
3076c5aff182SThomas Petazzoni 
3077c5aff182SThomas Petazzoni 	/* Clear all ethernet port interrupts */
3078db488c10SGregory CLEMENT 	on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
3079c5aff182SThomas Petazzoni 
3080c5aff182SThomas Petazzoni 	/* Mask all ethernet port interrupts */
3081db488c10SGregory CLEMENT 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3082c5aff182SThomas Petazzoni 
3083c5aff182SThomas Petazzoni 	mvneta_tx_reset(pp);
3084c5aff182SThomas Petazzoni 	mvneta_rx_reset(pp);
3085c5aff182SThomas Petazzoni }
3086c5aff182SThomas Petazzoni 
3087db5dd0dbSMarcin Wojtas static void mvneta_percpu_enable(void *arg)
3088db5dd0dbSMarcin Wojtas {
3089db5dd0dbSMarcin Wojtas 	struct mvneta_port *pp = arg;
3090db5dd0dbSMarcin Wojtas 
3091db5dd0dbSMarcin Wojtas 	enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3092db5dd0dbSMarcin Wojtas }
3093db5dd0dbSMarcin Wojtas 
3094db5dd0dbSMarcin Wojtas static void mvneta_percpu_disable(void *arg)
3095db5dd0dbSMarcin Wojtas {
3096db5dd0dbSMarcin Wojtas 	struct mvneta_port *pp = arg;
3097db5dd0dbSMarcin Wojtas 
3098db5dd0dbSMarcin Wojtas 	disable_percpu_irq(pp->dev->irq);
3099db5dd0dbSMarcin Wojtas }
3100db5dd0dbSMarcin Wojtas 
3101c5aff182SThomas Petazzoni /* Change the device mtu */
3102c5aff182SThomas Petazzoni static int mvneta_change_mtu(struct net_device *dev, int mtu)
3103c5aff182SThomas Petazzoni {
3104c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
3105c5aff182SThomas Petazzoni 	int ret;
3106c5aff182SThomas Petazzoni 
31075777987eSJarod Wilson 	if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
31085777987eSJarod Wilson 		netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
31095777987eSJarod Wilson 			    mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
31105777987eSJarod Wilson 		mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
31115777987eSJarod Wilson 	}
3112c5aff182SThomas Petazzoni 
3113c5aff182SThomas Petazzoni 	dev->mtu = mtu;
3114c5aff182SThomas Petazzoni 
3115b65657fcSSimon Guinot 	if (!netif_running(dev)) {
3116dc35a10fSMarcin Wojtas 		if (pp->bm_priv)
3117dc35a10fSMarcin Wojtas 			mvneta_bm_update_mtu(pp, mtu);
3118dc35a10fSMarcin Wojtas 
3119b65657fcSSimon Guinot 		netdev_update_features(dev);
3120c5aff182SThomas Petazzoni 		return 0;
3121b65657fcSSimon Guinot 	}
3122c5aff182SThomas Petazzoni 
31236a20c175SThomas Petazzoni 	/* The interface is running, so we have to force a
3124a92dbd96SEzequiel Garcia 	 * reallocation of the queues
3125c5aff182SThomas Petazzoni 	 */
3126c5aff182SThomas Petazzoni 	mvneta_stop_dev(pp);
3127db5dd0dbSMarcin Wojtas 	on_each_cpu(mvneta_percpu_disable, pp, true);
3128c5aff182SThomas Petazzoni 
3129c5aff182SThomas Petazzoni 	mvneta_cleanup_txqs(pp);
3130c5aff182SThomas Petazzoni 	mvneta_cleanup_rxqs(pp);
3131c5aff182SThomas Petazzoni 
3132dc35a10fSMarcin Wojtas 	if (pp->bm_priv)
3133dc35a10fSMarcin Wojtas 		mvneta_bm_update_mtu(pp, mtu);
3134dc35a10fSMarcin Wojtas 
3135a92dbd96SEzequiel Garcia 	pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
31368ec2cd48Swilly tarreau 	pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
31378ec2cd48Swilly tarreau 	                SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3138c5aff182SThomas Petazzoni 
3139c5aff182SThomas Petazzoni 	ret = mvneta_setup_rxqs(pp);
3140c5aff182SThomas Petazzoni 	if (ret) {
3141a92dbd96SEzequiel Garcia 		netdev_err(dev, "unable to setup rxqs after MTU change\n");
3142c5aff182SThomas Petazzoni 		return ret;
3143c5aff182SThomas Petazzoni 	}
3144c5aff182SThomas Petazzoni 
3145a92dbd96SEzequiel Garcia 	ret = mvneta_setup_txqs(pp);
3146a92dbd96SEzequiel Garcia 	if (ret) {
3147a92dbd96SEzequiel Garcia 		netdev_err(dev, "unable to setup txqs after MTU change\n");
3148a92dbd96SEzequiel Garcia 		return ret;
3149a92dbd96SEzequiel Garcia 	}
3150c5aff182SThomas Petazzoni 
3151db5dd0dbSMarcin Wojtas 	on_each_cpu(mvneta_percpu_enable, pp, true);
3152c5aff182SThomas Petazzoni 	mvneta_start_dev(pp);
3153c5aff182SThomas Petazzoni 	mvneta_port_up(pp);
3154c5aff182SThomas Petazzoni 
3155b65657fcSSimon Guinot 	netdev_update_features(dev);
3156b65657fcSSimon Guinot 
3157c5aff182SThomas Petazzoni 	return 0;
3158c5aff182SThomas Petazzoni }
3159c5aff182SThomas Petazzoni 
3160b65657fcSSimon Guinot static netdev_features_t mvneta_fix_features(struct net_device *dev,
3161b65657fcSSimon Guinot 					     netdev_features_t features)
3162b65657fcSSimon Guinot {
3163b65657fcSSimon Guinot 	struct mvneta_port *pp = netdev_priv(dev);
3164b65657fcSSimon Guinot 
3165b65657fcSSimon Guinot 	if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3166b65657fcSSimon Guinot 		features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3167b65657fcSSimon Guinot 		netdev_info(dev,
3168b65657fcSSimon Guinot 			    "Disable IP checksum for MTU greater than %dB\n",
3169b65657fcSSimon Guinot 			    pp->tx_csum_limit);
3170b65657fcSSimon Guinot 	}
3171b65657fcSSimon Guinot 
3172b65657fcSSimon Guinot 	return features;
3173b65657fcSSimon Guinot }
3174b65657fcSSimon Guinot 
31758cc3e439SThomas Petazzoni /* Get mac address */
31768cc3e439SThomas Petazzoni static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
31778cc3e439SThomas Petazzoni {
31788cc3e439SThomas Petazzoni 	u32 mac_addr_l, mac_addr_h;
31798cc3e439SThomas Petazzoni 
31808cc3e439SThomas Petazzoni 	mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
31818cc3e439SThomas Petazzoni 	mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
31828cc3e439SThomas Petazzoni 	addr[0] = (mac_addr_h >> 24) & 0xFF;
31838cc3e439SThomas Petazzoni 	addr[1] = (mac_addr_h >> 16) & 0xFF;
31848cc3e439SThomas Petazzoni 	addr[2] = (mac_addr_h >> 8) & 0xFF;
31858cc3e439SThomas Petazzoni 	addr[3] = mac_addr_h & 0xFF;
31868cc3e439SThomas Petazzoni 	addr[4] = (mac_addr_l >> 8) & 0xFF;
31878cc3e439SThomas Petazzoni 	addr[5] = mac_addr_l & 0xFF;
31888cc3e439SThomas Petazzoni }
31898cc3e439SThomas Petazzoni 
3190c5aff182SThomas Petazzoni /* Handle setting mac address */
3191c5aff182SThomas Petazzoni static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3192c5aff182SThomas Petazzoni {
3193c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
3194e68de360SEzequiel Garcia 	struct sockaddr *sockaddr = addr;
3195e68de360SEzequiel Garcia 	int ret;
3196c5aff182SThomas Petazzoni 
3197e68de360SEzequiel Garcia 	ret = eth_prepare_mac_addr_change(dev, addr);
3198e68de360SEzequiel Garcia 	if (ret < 0)
3199e68de360SEzequiel Garcia 		return ret;
3200c5aff182SThomas Petazzoni 	/* Remove previous address table entry */
3201c5aff182SThomas Petazzoni 	mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3202c5aff182SThomas Petazzoni 
3203c5aff182SThomas Petazzoni 	/* Set new addr in hw */
320490b74c01SGregory CLEMENT 	mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
3205c5aff182SThomas Petazzoni 
3206e68de360SEzequiel Garcia 	eth_commit_mac_addr_change(dev, addr);
3207c5aff182SThomas Petazzoni 	return 0;
3208c5aff182SThomas Petazzoni }
3209c5aff182SThomas Petazzoni 
3210503f9aa9SRussell King static void mvneta_validate(struct net_device *ndev, unsigned long *supported,
3211503f9aa9SRussell King 			    struct phylink_link_state *state)
3212503f9aa9SRussell King {
3213503f9aa9SRussell King 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
3214503f9aa9SRussell King 
321522f4bf8aSRussell King 	/* We only support QSGMII, SGMII, 802.3z and RGMII modes */
3216503f9aa9SRussell King 	if (state->interface != PHY_INTERFACE_MODE_NA &&
3217503f9aa9SRussell King 	    state->interface != PHY_INTERFACE_MODE_QSGMII &&
3218503f9aa9SRussell King 	    state->interface != PHY_INTERFACE_MODE_SGMII &&
321922f4bf8aSRussell King 	    !phy_interface_mode_is_8023z(state->interface) &&
3220503f9aa9SRussell King 	    !phy_interface_mode_is_rgmii(state->interface)) {
3221503f9aa9SRussell King 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
3222503f9aa9SRussell King 		return;
3223503f9aa9SRussell King 	}
3224503f9aa9SRussell King 
3225503f9aa9SRussell King 	/* Allow all the expected bits */
3226503f9aa9SRussell King 	phylink_set(mask, Autoneg);
3227503f9aa9SRussell King 	phylink_set_port_modes(mask);
3228503f9aa9SRussell King 
32294932a918SRussell King 	/* Asymmetric pause is unsupported */
32304932a918SRussell King 	phylink_set(mask, Pause);
3231503f9aa9SRussell King 	/* Half-duplex at speeds higher than 100Mbit is unsupported */
3232503f9aa9SRussell King 	phylink_set(mask, 1000baseT_Full);
3233503f9aa9SRussell King 	phylink_set(mask, 1000baseX_Full);
323422f4bf8aSRussell King 
323522f4bf8aSRussell King 	if (!phy_interface_mode_is_8023z(state->interface)) {
323622f4bf8aSRussell King 		/* 10M and 100M are only supported in non-802.3z mode */
3237503f9aa9SRussell King 		phylink_set(mask, 10baseT_Half);
3238503f9aa9SRussell King 		phylink_set(mask, 10baseT_Full);
3239503f9aa9SRussell King 		phylink_set(mask, 100baseT_Half);
3240503f9aa9SRussell King 		phylink_set(mask, 100baseT_Full);
324122f4bf8aSRussell King 	}
3242503f9aa9SRussell King 
3243503f9aa9SRussell King 	bitmap_and(supported, supported, mask,
3244503f9aa9SRussell King 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
3245503f9aa9SRussell King 	bitmap_and(state->advertising, state->advertising, mask,
3246503f9aa9SRussell King 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
3247503f9aa9SRussell King }
3248503f9aa9SRussell King 
3249503f9aa9SRussell King static int mvneta_mac_link_state(struct net_device *ndev,
3250503f9aa9SRussell King 				 struct phylink_link_state *state)
3251c5aff182SThomas Petazzoni {
3252c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(ndev);
3253503f9aa9SRussell King 	u32 gmac_stat;
3254c5aff182SThomas Petazzoni 
3255503f9aa9SRussell King 	gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3256503f9aa9SRussell King 
3257503f9aa9SRussell King 	if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3258503f9aa9SRussell King 		state->speed = SPEED_1000;
3259503f9aa9SRussell King 	else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3260503f9aa9SRussell King 		state->speed = SPEED_100;
3261503f9aa9SRussell King 	else
3262503f9aa9SRussell King 		state->speed = SPEED_10;
3263503f9aa9SRussell King 
3264503f9aa9SRussell King 	state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3265503f9aa9SRussell King 	state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3266503f9aa9SRussell King 	state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3267503f9aa9SRussell King 
3268503f9aa9SRussell King 	state->pause = 0;
32694932a918SRussell King 	if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
32704932a918SRussell King 		state->pause |= MLO_PAUSE_RX;
32714932a918SRussell King 	if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
32724932a918SRussell King 		state->pause |= MLO_PAUSE_TX;
3273503f9aa9SRussell King 
3274503f9aa9SRussell King 	return 1;
3275503f9aa9SRussell King }
3276503f9aa9SRussell King 
327722f4bf8aSRussell King static void mvneta_mac_an_restart(struct net_device *ndev)
327822f4bf8aSRussell King {
327922f4bf8aSRussell King 	struct mvneta_port *pp = netdev_priv(ndev);
328022f4bf8aSRussell King 	u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
328122f4bf8aSRussell King 
328222f4bf8aSRussell King 	mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
328322f4bf8aSRussell King 		    gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
328422f4bf8aSRussell King 	mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
328522f4bf8aSRussell King 		    gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
328622f4bf8aSRussell King }
328722f4bf8aSRussell King 
3288503f9aa9SRussell King static void mvneta_mac_config(struct net_device *ndev, unsigned int mode,
3289503f9aa9SRussell King 	const struct phylink_link_state *state)
3290503f9aa9SRussell King {
3291503f9aa9SRussell King 	struct mvneta_port *pp = netdev_priv(ndev);
329222f4bf8aSRussell King 	u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
3293503f9aa9SRussell King 	u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3294503f9aa9SRussell King 	u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3295503f9aa9SRussell King 	u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3296503f9aa9SRussell King 
329722f4bf8aSRussell King 	new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
329832699954SRussell King 	new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
329932699954SRussell King 				   MVNETA_GMAC2_PORT_RESET);
3300503f9aa9SRussell King 	new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3301503f9aa9SRussell King 	new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3302503f9aa9SRussell King 			     MVNETA_GMAC_INBAND_RESTART_AN |
3303503f9aa9SRussell King 			     MVNETA_GMAC_CONFIG_MII_SPEED |
3304c5aff182SThomas Petazzoni 			     MVNETA_GMAC_CONFIG_GMII_SPEED |
3305503f9aa9SRussell King 			     MVNETA_GMAC_AN_SPEED_EN |
330622f4bf8aSRussell King 			     MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
330722f4bf8aSRussell King 			     MVNETA_GMAC_CONFIG_FLOW_CTRL |
3308503f9aa9SRussell King 			     MVNETA_GMAC_AN_FLOW_CTRL_EN |
3309503f9aa9SRussell King 			     MVNETA_GMAC_CONFIG_FULL_DUPLEX |
3310503f9aa9SRussell King 			     MVNETA_GMAC_AN_DUPLEX_EN);
3311c5aff182SThomas Petazzoni 
331232699954SRussell King 	/* Even though it might look weird, when we're configured in
331332699954SRussell King 	 * SGMII or QSGMII mode, the RGMII bit needs to be set.
331432699954SRussell King 	 */
331532699954SRussell King 	new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
331632699954SRussell King 
331732699954SRussell King 	if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
331822f4bf8aSRussell King 	    state->interface == PHY_INTERFACE_MODE_SGMII ||
331922f4bf8aSRussell King 	    phy_interface_mode_is_8023z(state->interface))
332032699954SRussell King 		new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
332132699954SRussell King 
33224932a918SRussell King 	if (phylink_test(state->advertising, Pause))
33234932a918SRussell King 		new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
33244932a918SRussell King 	if (state->pause & MLO_PAUSE_TXRX_MASK)
33254932a918SRussell King 		new_an |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
33264932a918SRussell King 
3327503f9aa9SRussell King 	if (!phylink_autoneg_inband(mode)) {
3328503f9aa9SRussell King 		/* Phy or fixed speed */
3329503f9aa9SRussell King 		if (state->duplex)
3330503f9aa9SRussell King 			new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3331c5aff182SThomas Petazzoni 
3332503f9aa9SRussell King 		if (state->speed == SPEED_1000)
3333503f9aa9SRussell King 			new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3334503f9aa9SRussell King 		else if (state->speed == SPEED_100)
3335503f9aa9SRussell King 			new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
333622f4bf8aSRussell King 	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
3337503f9aa9SRussell King 		/* SGMII mode receives the state from the PHY */
3338503f9aa9SRussell King 		new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3339503f9aa9SRussell King 		new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3340503f9aa9SRussell King 		new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3341503f9aa9SRussell King 				     MVNETA_GMAC_FORCE_LINK_PASS)) |
3342503f9aa9SRussell King 			 MVNETA_GMAC_INBAND_AN_ENABLE |
3343503f9aa9SRussell King 			 MVNETA_GMAC_AN_SPEED_EN |
3344503f9aa9SRussell King 			 MVNETA_GMAC_AN_DUPLEX_EN;
334522f4bf8aSRussell King 	} else {
334622f4bf8aSRussell King 		/* 802.3z negotiation - only 1000base-X */
334722f4bf8aSRussell King 		new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
334822f4bf8aSRussell King 		new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
334922f4bf8aSRussell King 		new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
335022f4bf8aSRussell King 				     MVNETA_GMAC_FORCE_LINK_PASS)) |
335122f4bf8aSRussell King 			 MVNETA_GMAC_INBAND_AN_ENABLE |
335222f4bf8aSRussell King 			 MVNETA_GMAC_CONFIG_GMII_SPEED |
335322f4bf8aSRussell King 			 /* The MAC only supports FD mode */
335422f4bf8aSRussell King 			 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
33554932a918SRussell King 
33564932a918SRussell King 		if (state->pause & MLO_PAUSE_AN && state->an_enabled)
33574932a918SRussell King 			new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
3358c5aff182SThomas Petazzoni 	}
3359c5aff182SThomas Petazzoni 
3360503f9aa9SRussell King 	/* Armada 370 documentation says we can only change the port mode
3361503f9aa9SRussell King 	 * and in-band enable when the link is down, so force it down
3362503f9aa9SRussell King 	 * while making these changes. We also do this for GMAC_CTRL2 */
336322f4bf8aSRussell King 	if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
336422f4bf8aSRussell King 	    (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
3365503f9aa9SRussell King 	    (new_an  ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3366503f9aa9SRussell King 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3367503f9aa9SRussell King 			    (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3368503f9aa9SRussell King 			    MVNETA_GMAC_FORCE_LINK_DOWN);
3369503f9aa9SRussell King 	}
3370503f9aa9SRussell King 
337122f4bf8aSRussell King 	if (new_ctrl0 != gmac_ctrl0)
337222f4bf8aSRussell King 		mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
3373503f9aa9SRussell King 	if (new_ctrl2 != gmac_ctrl2)
3374503f9aa9SRussell King 		mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
3375503f9aa9SRussell King 	if (new_clk != gmac_clk)
3376503f9aa9SRussell King 		mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3377503f9aa9SRussell King 	if (new_an != gmac_an)
3378503f9aa9SRussell King 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
337932699954SRussell King 
338032699954SRussell King 	if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
338132699954SRussell King 		while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
338232699954SRussell King 			MVNETA_GMAC2_PORT_RESET) != 0)
338332699954SRussell King 			continue;
338432699954SRussell King 	}
3385503f9aa9SRussell King }
3386503f9aa9SRussell King 
33876d81f451SRussell King static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
33886d81f451SRussell King {
33896d81f451SRussell King 	u32 lpi_ctl1;
33906d81f451SRussell King 
33916d81f451SRussell King 	lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
33926d81f451SRussell King 	if (enable)
33936d81f451SRussell King 		lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
33946d81f451SRussell King 	else
33956d81f451SRussell King 		lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
33966d81f451SRussell King 	mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
33976d81f451SRussell King }
33986d81f451SRussell King 
3399503f9aa9SRussell King static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode)
3400fc548b99SRussell King {
3401fc548b99SRussell King 	struct mvneta_port *pp = netdev_priv(ndev);
3402fc548b99SRussell King 	u32 val;
3403fc548b99SRussell King 
3404503f9aa9SRussell King 	mvneta_port_down(pp);
3405503f9aa9SRussell King 
3406503f9aa9SRussell King 	if (!phylink_autoneg_inband(mode)) {
3407fc548b99SRussell King 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3408fc548b99SRussell King 		val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3409fc548b99SRussell King 		val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3410fc548b99SRussell King 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3411fc548b99SRussell King 	}
34126d81f451SRussell King 
34136d81f451SRussell King 	pp->eee_active = false;
34146d81f451SRussell King 	mvneta_set_eee(pp, false);
3415fc548b99SRussell King }
3416fc548b99SRussell King 
3417503f9aa9SRussell King static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode,
3418503f9aa9SRussell King 			       struct phy_device *phy)
3419fc548b99SRussell King {
3420fc548b99SRussell King 	struct mvneta_port *pp = netdev_priv(ndev);
3421fc548b99SRussell King 	u32 val;
3422fc548b99SRussell King 
3423503f9aa9SRussell King 	if (!phylink_autoneg_inband(mode)) {
3424fc548b99SRussell King 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3425fc548b99SRussell King 		val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3426fc548b99SRussell King 		val |= MVNETA_GMAC_FORCE_LINK_PASS;
3427fc548b99SRussell King 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3428fc548b99SRussell King 	}
3429fc548b99SRussell King 
3430fc548b99SRussell King 	mvneta_port_up(pp);
34316d81f451SRussell King 
34326d81f451SRussell King 	if (phy && pp->eee_enabled) {
34336d81f451SRussell King 		pp->eee_active = phy_init_eee(phy, 0) >= 0;
34346d81f451SRussell King 		mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
34356d81f451SRussell King 	}
3436fc548b99SRussell King }
3437fc548b99SRussell King 
3438503f9aa9SRussell King static const struct phylink_mac_ops mvneta_phylink_ops = {
3439503f9aa9SRussell King 	.validate = mvneta_validate,
3440503f9aa9SRussell King 	.mac_link_state = mvneta_mac_link_state,
344122f4bf8aSRussell King 	.mac_an_restart = mvneta_mac_an_restart,
3442503f9aa9SRussell King 	.mac_config = mvneta_mac_config,
3443503f9aa9SRussell King 	.mac_link_down = mvneta_mac_link_down,
3444503f9aa9SRussell King 	.mac_link_up = mvneta_mac_link_up,
3445503f9aa9SRussell King };
3446c5aff182SThomas Petazzoni 
3447c5aff182SThomas Petazzoni static int mvneta_mdio_probe(struct mvneta_port *pp)
3448c5aff182SThomas Petazzoni {
344982960fffSJisheng Zhang 	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
3450503f9aa9SRussell King 	int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
3451c5aff182SThomas Petazzoni 
3452503f9aa9SRussell King 	if (err)
3453503f9aa9SRussell King 		netdev_err(pp->dev, "could not attach PHY: %d\n", err);
3454c5aff182SThomas Petazzoni 
3455503f9aa9SRussell King 	phylink_ethtool_get_wol(pp->phylink, &wol);
345682960fffSJisheng Zhang 	device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
345782960fffSJisheng Zhang 
3458503f9aa9SRussell King 	return err;
3459c5aff182SThomas Petazzoni }
3460c5aff182SThomas Petazzoni 
3461c5aff182SThomas Petazzoni static void mvneta_mdio_remove(struct mvneta_port *pp)
3462c5aff182SThomas Petazzoni {
3463503f9aa9SRussell King 	phylink_disconnect_phy(pp->phylink);
3464c5aff182SThomas Petazzoni }
3465c5aff182SThomas Petazzoni 
3466120cfa50SGregory CLEMENT /* Electing a CPU must be done in an atomic way: it should be done
3467120cfa50SGregory CLEMENT  * after or before the removal/insertion of a CPU and this function is
3468120cfa50SGregory CLEMENT  * not reentrant.
3469120cfa50SGregory CLEMENT  */
3470f8642885SMaxime Ripard static void mvneta_percpu_elect(struct mvneta_port *pp)
3471f8642885SMaxime Ripard {
3472cad5d847SGregory CLEMENT 	int elected_cpu = 0, max_cpu, cpu, i = 0;
3473f8642885SMaxime Ripard 
3474cad5d847SGregory CLEMENT 	/* Use the cpu associated to the rxq when it is online, in all
3475cad5d847SGregory CLEMENT 	 * the other cases, use the cpu 0 which can't be offline.
3476cad5d847SGregory CLEMENT 	 */
3477cad5d847SGregory CLEMENT 	if (cpu_online(pp->rxq_def))
3478cad5d847SGregory CLEMENT 		elected_cpu = pp->rxq_def;
3479cad5d847SGregory CLEMENT 
34802dcf75e2SGregory CLEMENT 	max_cpu = num_present_cpus();
3481f8642885SMaxime Ripard 
3482f8642885SMaxime Ripard 	for_each_online_cpu(cpu) {
34832dcf75e2SGregory CLEMENT 		int rxq_map = 0, txq_map = 0;
34842dcf75e2SGregory CLEMENT 		int rxq;
34852dcf75e2SGregory CLEMENT 
34862dcf75e2SGregory CLEMENT 		for (rxq = 0; rxq < rxq_number; rxq++)
34872dcf75e2SGregory CLEMENT 			if ((rxq % max_cpu) == cpu)
34882dcf75e2SGregory CLEMENT 				rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
34892dcf75e2SGregory CLEMENT 
3490cad5d847SGregory CLEMENT 		if (cpu == elected_cpu)
349150bf8cb6SGregory CLEMENT 			/* Map the default receive queue queue to the
349250bf8cb6SGregory CLEMENT 			 * elected CPU
3493f8642885SMaxime Ripard 			 */
34942dcf75e2SGregory CLEMENT 			rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
349550bf8cb6SGregory CLEMENT 
349650bf8cb6SGregory CLEMENT 		/* We update the TX queue map only if we have one
349750bf8cb6SGregory CLEMENT 		 * queue. In this case we associate the TX queue to
349850bf8cb6SGregory CLEMENT 		 * the CPU bound to the default RX queue
349950bf8cb6SGregory CLEMENT 		 */
350050bf8cb6SGregory CLEMENT 		if (txq_number == 1)
3501cad5d847SGregory CLEMENT 			txq_map = (cpu == elected_cpu) ?
350250bf8cb6SGregory CLEMENT 				MVNETA_CPU_TXQ_ACCESS(1) : 0;
350350bf8cb6SGregory CLEMENT 		else
350450bf8cb6SGregory CLEMENT 			txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
350550bf8cb6SGregory CLEMENT 				MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
350650bf8cb6SGregory CLEMENT 
35072dcf75e2SGregory CLEMENT 		mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
35082dcf75e2SGregory CLEMENT 
35092dcf75e2SGregory CLEMENT 		/* Update the interrupt mask on each CPU according the
35102dcf75e2SGregory CLEMENT 		 * new mapping
35112dcf75e2SGregory CLEMENT 		 */
35122dcf75e2SGregory CLEMENT 		smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3513f8642885SMaxime Ripard 					 pp, true);
3514f8642885SMaxime Ripard 		i++;
35152dcf75e2SGregory CLEMENT 
3516f8642885SMaxime Ripard 	}
3517f8642885SMaxime Ripard };
3518f8642885SMaxime Ripard 
351984a3f4dbSSebastian Andrzej Siewior static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
3520f8642885SMaxime Ripard {
352184a3f4dbSSebastian Andrzej Siewior 	int other_cpu;
352284a3f4dbSSebastian Andrzej Siewior 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
352384a3f4dbSSebastian Andrzej Siewior 						  node_online);
3524f8642885SMaxime Ripard 	struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3525f8642885SMaxime Ripard 
352684a3f4dbSSebastian Andrzej Siewior 
3527120cfa50SGregory CLEMENT 	spin_lock(&pp->lock);
352884a3f4dbSSebastian Andrzej Siewior 	/*
352984a3f4dbSSebastian Andrzej Siewior 	 * Configuring the driver for a new CPU while the driver is
353084a3f4dbSSebastian Andrzej Siewior 	 * stopping is racy, so just avoid it.
3531120cfa50SGregory CLEMENT 	 */
3532120cfa50SGregory CLEMENT 	if (pp->is_stopped) {
3533120cfa50SGregory CLEMENT 		spin_unlock(&pp->lock);
353484a3f4dbSSebastian Andrzej Siewior 		return 0;
3535120cfa50SGregory CLEMENT 	}
3536f8642885SMaxime Ripard 	netif_tx_stop_all_queues(pp->dev);
3537f8642885SMaxime Ripard 
353884a3f4dbSSebastian Andrzej Siewior 	/*
353984a3f4dbSSebastian Andrzej Siewior 	 * We have to synchronise on tha napi of each CPU except the one
354084a3f4dbSSebastian Andrzej Siewior 	 * just being woken up
3541f8642885SMaxime Ripard 	 */
3542f8642885SMaxime Ripard 	for_each_online_cpu(other_cpu) {
3543f8642885SMaxime Ripard 		if (other_cpu != cpu) {
3544f8642885SMaxime Ripard 			struct mvneta_pcpu_port *other_port =
3545f8642885SMaxime Ripard 				per_cpu_ptr(pp->ports, other_cpu);
3546f8642885SMaxime Ripard 
3547f8642885SMaxime Ripard 			napi_synchronize(&other_port->napi);
3548f8642885SMaxime Ripard 		}
3549f8642885SMaxime Ripard 	}
3550f8642885SMaxime Ripard 
3551f8642885SMaxime Ripard 	/* Mask all ethernet port interrupts */
3552db488c10SGregory CLEMENT 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3553f8642885SMaxime Ripard 	napi_enable(&port->napi);
3554f8642885SMaxime Ripard 
355584a3f4dbSSebastian Andrzej Siewior 	/*
355684a3f4dbSSebastian Andrzej Siewior 	 * Enable per-CPU interrupts on the CPU that is
35572dcf75e2SGregory CLEMENT 	 * brought up.
35582dcf75e2SGregory CLEMENT 	 */
35590e28bf93SAnna-Maria Gleixner 	mvneta_percpu_enable(pp);
35602dcf75e2SGregory CLEMENT 
356184a3f4dbSSebastian Andrzej Siewior 	/*
356284a3f4dbSSebastian Andrzej Siewior 	 * Enable per-CPU interrupt on the one CPU we care
3563f8642885SMaxime Ripard 	 * about.
3564f8642885SMaxime Ripard 	 */
3565f8642885SMaxime Ripard 	mvneta_percpu_elect(pp);
3566f8642885SMaxime Ripard 
3567db488c10SGregory CLEMENT 	/* Unmask all ethernet port interrupts */
3568db488c10SGregory CLEMENT 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3569f8642885SMaxime Ripard 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3570f8642885SMaxime Ripard 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
3571856b2cc5SRussell King 		    MVNETA_CAUSE_LINK_CHANGE);
3572f8642885SMaxime Ripard 	netif_tx_start_all_queues(pp->dev);
3573120cfa50SGregory CLEMENT 	spin_unlock(&pp->lock);
357484a3f4dbSSebastian Andrzej Siewior 	return 0;
357584a3f4dbSSebastian Andrzej Siewior }
357684a3f4dbSSebastian Andrzej Siewior 
357784a3f4dbSSebastian Andrzej Siewior static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
357884a3f4dbSSebastian Andrzej Siewior {
357984a3f4dbSSebastian Andrzej Siewior 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
358084a3f4dbSSebastian Andrzej Siewior 						  node_online);
358184a3f4dbSSebastian Andrzej Siewior 	struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
358284a3f4dbSSebastian Andrzej Siewior 
358384a3f4dbSSebastian Andrzej Siewior 	/*
358484a3f4dbSSebastian Andrzej Siewior 	 * Thanks to this lock we are sure that any pending cpu election is
358584a3f4dbSSebastian Andrzej Siewior 	 * done.
35865888511eSGregory CLEMENT 	 */
35875888511eSGregory CLEMENT 	spin_lock(&pp->lock);
3588f8642885SMaxime Ripard 	/* Mask all ethernet port interrupts */
3589db488c10SGregory CLEMENT 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
35905888511eSGregory CLEMENT 	spin_unlock(&pp->lock);
3591f8642885SMaxime Ripard 
3592f8642885SMaxime Ripard 	napi_synchronize(&port->napi);
3593f8642885SMaxime Ripard 	napi_disable(&port->napi);
359484a3f4dbSSebastian Andrzej Siewior 	/* Disable per-CPU interrupts on the CPU that is brought down. */
35950e28bf93SAnna-Maria Gleixner 	mvneta_percpu_disable(pp);
359684a3f4dbSSebastian Andrzej Siewior 	return 0;
359784a3f4dbSSebastian Andrzej Siewior }
3598f8642885SMaxime Ripard 
359984a3f4dbSSebastian Andrzej Siewior static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
360084a3f4dbSSebastian Andrzej Siewior {
360184a3f4dbSSebastian Andrzej Siewior 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
360284a3f4dbSSebastian Andrzej Siewior 						  node_dead);
360384a3f4dbSSebastian Andrzej Siewior 
3604f8642885SMaxime Ripard 	/* Check if a new CPU must be elected now this on is down */
3605120cfa50SGregory CLEMENT 	spin_lock(&pp->lock);
3606f8642885SMaxime Ripard 	mvneta_percpu_elect(pp);
3607120cfa50SGregory CLEMENT 	spin_unlock(&pp->lock);
3608f8642885SMaxime Ripard 	/* Unmask all ethernet port interrupts */
3609db488c10SGregory CLEMENT 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3610f8642885SMaxime Ripard 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3611f8642885SMaxime Ripard 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
3612856b2cc5SRussell King 		    MVNETA_CAUSE_LINK_CHANGE);
3613f8642885SMaxime Ripard 	netif_tx_start_all_queues(pp->dev);
361484a3f4dbSSebastian Andrzej Siewior 	return 0;
3615f8642885SMaxime Ripard }
3616f8642885SMaxime Ripard 
3617c5aff182SThomas Petazzoni static int mvneta_open(struct net_device *dev)
3618c5aff182SThomas Petazzoni {
3619c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
36206b125d63SGregory CLEMENT 	int ret;
3621c5aff182SThomas Petazzoni 
3622c5aff182SThomas Petazzoni 	pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
36238ec2cd48Swilly tarreau 	pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
36248ec2cd48Swilly tarreau 	                SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3625c5aff182SThomas Petazzoni 
3626c5aff182SThomas Petazzoni 	ret = mvneta_setup_rxqs(pp);
3627c5aff182SThomas Petazzoni 	if (ret)
3628c5aff182SThomas Petazzoni 		return ret;
3629c5aff182SThomas Petazzoni 
3630c5aff182SThomas Petazzoni 	ret = mvneta_setup_txqs(pp);
3631c5aff182SThomas Petazzoni 	if (ret)
3632c5aff182SThomas Petazzoni 		goto err_cleanup_rxqs;
3633c5aff182SThomas Petazzoni 
3634c5aff182SThomas Petazzoni 	/* Connect to port interrupt line */
36352636ac3cSMarcin Wojtas 	if (pp->neta_armada3700)
36362636ac3cSMarcin Wojtas 		ret = request_irq(pp->dev->irq, mvneta_isr, 0,
36372636ac3cSMarcin Wojtas 				  dev->name, pp);
36382636ac3cSMarcin Wojtas 	else
36392636ac3cSMarcin Wojtas 		ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
36402636ac3cSMarcin Wojtas 					 dev->name, pp->ports);
3641c5aff182SThomas Petazzoni 	if (ret) {
3642c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3643c5aff182SThomas Petazzoni 		goto err_cleanup_txqs;
3644c5aff182SThomas Petazzoni 	}
3645c5aff182SThomas Petazzoni 
36462636ac3cSMarcin Wojtas 	if (!pp->neta_armada3700) {
36472dcf75e2SGregory CLEMENT 		/* Enable per-CPU interrupt on all the CPU to handle our RX
36482dcf75e2SGregory CLEMENT 		 * queue interrupts
36492dcf75e2SGregory CLEMENT 		 */
36506b125d63SGregory CLEMENT 		on_each_cpu(mvneta_percpu_enable, pp, true);
36512dcf75e2SGregory CLEMENT 
3652120cfa50SGregory CLEMENT 		pp->is_stopped = false;
3653f8642885SMaxime Ripard 		/* Register a CPU notifier to handle the case where our CPU
3654f8642885SMaxime Ripard 		 * might be taken offline.
3655f8642885SMaxime Ripard 		 */
365684a3f4dbSSebastian Andrzej Siewior 		ret = cpuhp_state_add_instance_nocalls(online_hpstate,
365784a3f4dbSSebastian Andrzej Siewior 						       &pp->node_online);
365884a3f4dbSSebastian Andrzej Siewior 		if (ret)
365984a3f4dbSSebastian Andrzej Siewior 			goto err_free_irq;
366084a3f4dbSSebastian Andrzej Siewior 
366184a3f4dbSSebastian Andrzej Siewior 		ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
366284a3f4dbSSebastian Andrzej Siewior 						       &pp->node_dead);
366384a3f4dbSSebastian Andrzej Siewior 		if (ret)
366484a3f4dbSSebastian Andrzej Siewior 			goto err_free_online_hp;
36652636ac3cSMarcin Wojtas 	}
3666f8642885SMaxime Ripard 
3667c5aff182SThomas Petazzoni 	/* In default link is down */
3668c5aff182SThomas Petazzoni 	netif_carrier_off(pp->dev);
3669c5aff182SThomas Petazzoni 
3670c5aff182SThomas Petazzoni 	ret = mvneta_mdio_probe(pp);
3671c5aff182SThomas Petazzoni 	if (ret < 0) {
3672c5aff182SThomas Petazzoni 		netdev_err(dev, "cannot probe MDIO bus\n");
367384a3f4dbSSebastian Andrzej Siewior 		goto err_free_dead_hp;
3674c5aff182SThomas Petazzoni 	}
3675c5aff182SThomas Petazzoni 
3676c5aff182SThomas Petazzoni 	mvneta_start_dev(pp);
3677c5aff182SThomas Petazzoni 
3678c5aff182SThomas Petazzoni 	return 0;
3679c5aff182SThomas Petazzoni 
368084a3f4dbSSebastian Andrzej Siewior err_free_dead_hp:
36812636ac3cSMarcin Wojtas 	if (!pp->neta_armada3700)
368284a3f4dbSSebastian Andrzej Siewior 		cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
368384a3f4dbSSebastian Andrzej Siewior 						    &pp->node_dead);
368484a3f4dbSSebastian Andrzej Siewior err_free_online_hp:
36852636ac3cSMarcin Wojtas 	if (!pp->neta_armada3700)
36862636ac3cSMarcin Wojtas 		cpuhp_state_remove_instance_nocalls(online_hpstate,
36872636ac3cSMarcin Wojtas 						    &pp->node_online);
3688c5aff182SThomas Petazzoni err_free_irq:
36892636ac3cSMarcin Wojtas 	if (pp->neta_armada3700) {
36902636ac3cSMarcin Wojtas 		free_irq(pp->dev->irq, pp);
36912636ac3cSMarcin Wojtas 	} else {
36923d8c4530SRussell King - ARM Linux 		on_each_cpu(mvneta_percpu_disable, pp, true);
369312bb03b4SMaxime Ripard 		free_percpu_irq(pp->dev->irq, pp->ports);
36942636ac3cSMarcin Wojtas 	}
3695c5aff182SThomas Petazzoni err_cleanup_txqs:
3696c5aff182SThomas Petazzoni 	mvneta_cleanup_txqs(pp);
3697c5aff182SThomas Petazzoni err_cleanup_rxqs:
3698c5aff182SThomas Petazzoni 	mvneta_cleanup_rxqs(pp);
3699c5aff182SThomas Petazzoni 	return ret;
3700c5aff182SThomas Petazzoni }
3701c5aff182SThomas Petazzoni 
3702c5aff182SThomas Petazzoni /* Stop the port, free port interrupt line */
3703c5aff182SThomas Petazzoni static int mvneta_stop(struct net_device *dev)
3704c5aff182SThomas Petazzoni {
3705c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
3706c5aff182SThomas Petazzoni 
37072636ac3cSMarcin Wojtas 	if (!pp->neta_armada3700) {
3708120cfa50SGregory CLEMENT 		/* Inform that we are stopping so we don't want to setup the
37091c2722a9SGregory CLEMENT 		 * driver for new CPUs in the notifiers. The code of the
37101c2722a9SGregory CLEMENT 		 * notifier for CPU online is protected by the same spinlock,
37111c2722a9SGregory CLEMENT 		 * so when we get the lock, the notifer work is done.
3712120cfa50SGregory CLEMENT 		 */
3713120cfa50SGregory CLEMENT 		spin_lock(&pp->lock);
3714120cfa50SGregory CLEMENT 		pp->is_stopped = true;
37151c2722a9SGregory CLEMENT 		spin_unlock(&pp->lock);
37161c2722a9SGregory CLEMENT 
3717c5aff182SThomas Petazzoni 		mvneta_stop_dev(pp);
3718c5aff182SThomas Petazzoni 		mvneta_mdio_remove(pp);
371984a3f4dbSSebastian Andrzej Siewior 
3720d26aac2dSDan Carpenter 		cpuhp_state_remove_instance_nocalls(online_hpstate,
3721d26aac2dSDan Carpenter 						    &pp->node_online);
372284a3f4dbSSebastian Andrzej Siewior 		cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
372384a3f4dbSSebastian Andrzej Siewior 						    &pp->node_dead);
3724129219e4SGregory CLEMENT 		on_each_cpu(mvneta_percpu_disable, pp, true);
372512bb03b4SMaxime Ripard 		free_percpu_irq(dev->irq, pp->ports);
37262636ac3cSMarcin Wojtas 	} else {
37272636ac3cSMarcin Wojtas 		mvneta_stop_dev(pp);
37282636ac3cSMarcin Wojtas 		mvneta_mdio_remove(pp);
37292636ac3cSMarcin Wojtas 		free_irq(dev->irq, pp);
37302636ac3cSMarcin Wojtas 	}
37312636ac3cSMarcin Wojtas 
3732c5aff182SThomas Petazzoni 	mvneta_cleanup_rxqs(pp);
3733c5aff182SThomas Petazzoni 	mvneta_cleanup_txqs(pp);
3734c5aff182SThomas Petazzoni 
3735c5aff182SThomas Petazzoni 	return 0;
3736c5aff182SThomas Petazzoni }
3737c5aff182SThomas Petazzoni 
373815f59456SThomas Petazzoni static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
373915f59456SThomas Petazzoni {
3740503f9aa9SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
374115f59456SThomas Petazzoni 
3742503f9aa9SRussell King 	return phylink_mii_ioctl(pp->phylink, ifr, cmd);
374315f59456SThomas Petazzoni }
374415f59456SThomas Petazzoni 
3745c5aff182SThomas Petazzoni /* Ethtool methods */
3746c5aff182SThomas Petazzoni 
3747013ad40dSPhilippe Reynes /* Set link ksettings (phy address, speed) for ethtools */
37482dc0d2b4SBaoyou Xie static int
37492dc0d2b4SBaoyou Xie mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3750013ad40dSPhilippe Reynes 				  const struct ethtool_link_ksettings *cmd)
3751c5aff182SThomas Petazzoni {
3752013ad40dSPhilippe Reynes 	struct mvneta_port *pp = netdev_priv(ndev);
3753c5aff182SThomas Petazzoni 
3754503f9aa9SRussell King 	return phylink_ethtool_ksettings_set(pp->phylink, cmd);
37550c0744fcSStas Sergeev }
37560c0744fcSStas Sergeev 
3757503f9aa9SRussell King /* Get link ksettings for ethtools */
3758503f9aa9SRussell King static int
3759503f9aa9SRussell King mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
3760503f9aa9SRussell King 				  struct ethtool_link_ksettings *cmd)
3761503f9aa9SRussell King {
3762503f9aa9SRussell King 	struct mvneta_port *pp = netdev_priv(ndev);
37630c0744fcSStas Sergeev 
3764503f9aa9SRussell King 	return phylink_ethtool_ksettings_get(pp->phylink, cmd);
37650c0744fcSStas Sergeev }
37660c0744fcSStas Sergeev 
3767503f9aa9SRussell King static int mvneta_ethtool_nway_reset(struct net_device *dev)
3768503f9aa9SRussell King {
3769503f9aa9SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
3770503f9aa9SRussell King 
3771503f9aa9SRussell King 	return phylink_ethtool_nway_reset(pp->phylink);
3772c5aff182SThomas Petazzoni }
3773c5aff182SThomas Petazzoni 
3774c5aff182SThomas Petazzoni /* Set interrupt coalescing for ethtools */
3775c5aff182SThomas Petazzoni static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3776c5aff182SThomas Petazzoni 				       struct ethtool_coalesce *c)
3777c5aff182SThomas Petazzoni {
3778c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
3779c5aff182SThomas Petazzoni 	int queue;
3780c5aff182SThomas Petazzoni 
3781c5aff182SThomas Petazzoni 	for (queue = 0; queue < rxq_number; queue++) {
3782c5aff182SThomas Petazzoni 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3783c5aff182SThomas Petazzoni 		rxq->time_coal = c->rx_coalesce_usecs;
3784c5aff182SThomas Petazzoni 		rxq->pkts_coal = c->rx_max_coalesced_frames;
3785c5aff182SThomas Petazzoni 		mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3786c5aff182SThomas Petazzoni 		mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3787c5aff182SThomas Petazzoni 	}
3788c5aff182SThomas Petazzoni 
3789c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
3790c5aff182SThomas Petazzoni 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
3791c5aff182SThomas Petazzoni 		txq->done_pkts_coal = c->tx_max_coalesced_frames;
3792c5aff182SThomas Petazzoni 		mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3793c5aff182SThomas Petazzoni 	}
3794c5aff182SThomas Petazzoni 
3795c5aff182SThomas Petazzoni 	return 0;
3796c5aff182SThomas Petazzoni }
3797c5aff182SThomas Petazzoni 
3798c5aff182SThomas Petazzoni /* get coalescing for ethtools */
3799c5aff182SThomas Petazzoni static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3800c5aff182SThomas Petazzoni 				       struct ethtool_coalesce *c)
3801c5aff182SThomas Petazzoni {
3802c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
3803c5aff182SThomas Petazzoni 
3804c5aff182SThomas Petazzoni 	c->rx_coalesce_usecs        = pp->rxqs[0].time_coal;
3805c5aff182SThomas Petazzoni 	c->rx_max_coalesced_frames  = pp->rxqs[0].pkts_coal;
3806c5aff182SThomas Petazzoni 
3807c5aff182SThomas Petazzoni 	c->tx_max_coalesced_frames =  pp->txqs[0].done_pkts_coal;
3808c5aff182SThomas Petazzoni 	return 0;
3809c5aff182SThomas Petazzoni }
3810c5aff182SThomas Petazzoni 
3811c5aff182SThomas Petazzoni 
3812c5aff182SThomas Petazzoni static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3813c5aff182SThomas Petazzoni 				    struct ethtool_drvinfo *drvinfo)
3814c5aff182SThomas Petazzoni {
3815c5aff182SThomas Petazzoni 	strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3816c5aff182SThomas Petazzoni 		sizeof(drvinfo->driver));
3817c5aff182SThomas Petazzoni 	strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3818c5aff182SThomas Petazzoni 		sizeof(drvinfo->version));
3819c5aff182SThomas Petazzoni 	strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3820c5aff182SThomas Petazzoni 		sizeof(drvinfo->bus_info));
3821c5aff182SThomas Petazzoni }
3822c5aff182SThomas Petazzoni 
3823c5aff182SThomas Petazzoni 
3824c5aff182SThomas Petazzoni static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3825c5aff182SThomas Petazzoni 					 struct ethtool_ringparam *ring)
3826c5aff182SThomas Petazzoni {
3827c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(netdev);
3828c5aff182SThomas Petazzoni 
3829c5aff182SThomas Petazzoni 	ring->rx_max_pending = MVNETA_MAX_RXD;
3830c5aff182SThomas Petazzoni 	ring->tx_max_pending = MVNETA_MAX_TXD;
3831c5aff182SThomas Petazzoni 	ring->rx_pending = pp->rx_ring_size;
3832c5aff182SThomas Petazzoni 	ring->tx_pending = pp->tx_ring_size;
3833c5aff182SThomas Petazzoni }
3834c5aff182SThomas Petazzoni 
3835c5aff182SThomas Petazzoni static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3836c5aff182SThomas Petazzoni 					struct ethtool_ringparam *ring)
3837c5aff182SThomas Petazzoni {
3838c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
3839c5aff182SThomas Petazzoni 
3840c5aff182SThomas Petazzoni 	if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3841c5aff182SThomas Petazzoni 		return -EINVAL;
3842c5aff182SThomas Petazzoni 	pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3843c5aff182SThomas Petazzoni 		ring->rx_pending : MVNETA_MAX_RXD;
38448eef5f97SEzequiel Garcia 
38458eef5f97SEzequiel Garcia 	pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
38468eef5f97SEzequiel Garcia 				   MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
38478eef5f97SEzequiel Garcia 	if (pp->tx_ring_size != ring->tx_pending)
38488eef5f97SEzequiel Garcia 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
38498eef5f97SEzequiel Garcia 			    pp->tx_ring_size, ring->tx_pending);
3850c5aff182SThomas Petazzoni 
3851c5aff182SThomas Petazzoni 	if (netif_running(dev)) {
3852c5aff182SThomas Petazzoni 		mvneta_stop(dev);
3853c5aff182SThomas Petazzoni 		if (mvneta_open(dev)) {
3854c5aff182SThomas Petazzoni 			netdev_err(dev,
3855c5aff182SThomas Petazzoni 				   "error on opening device after ring param change\n");
3856c5aff182SThomas Petazzoni 			return -ENOMEM;
3857c5aff182SThomas Petazzoni 		}
3858c5aff182SThomas Petazzoni 	}
3859c5aff182SThomas Petazzoni 
3860c5aff182SThomas Petazzoni 	return 0;
3861c5aff182SThomas Petazzoni }
3862c5aff182SThomas Petazzoni 
38634932a918SRussell King static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
38644932a918SRussell King 					  struct ethtool_pauseparam *pause)
38654932a918SRussell King {
38664932a918SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
38674932a918SRussell King 
38684932a918SRussell King 	phylink_ethtool_get_pauseparam(pp->phylink, pause);
38694932a918SRussell King }
38704932a918SRussell King 
38714932a918SRussell King static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
38724932a918SRussell King 					 struct ethtool_pauseparam *pause)
38734932a918SRussell King {
38744932a918SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
38754932a918SRussell King 
38764932a918SRussell King 	return phylink_ethtool_set_pauseparam(pp->phylink, pause);
38774932a918SRussell King }
38784932a918SRussell King 
38799b0cdefaSRussell King static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
38809b0cdefaSRussell King 				       u8 *data)
38819b0cdefaSRussell King {
38829b0cdefaSRussell King 	if (sset == ETH_SS_STATS) {
38839b0cdefaSRussell King 		int i;
38849b0cdefaSRussell King 
38859b0cdefaSRussell King 		for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
38869b0cdefaSRussell King 			memcpy(data + i * ETH_GSTRING_LEN,
38879b0cdefaSRussell King 			       mvneta_statistics[i].name, ETH_GSTRING_LEN);
38889b0cdefaSRussell King 	}
38899b0cdefaSRussell King }
38909b0cdefaSRussell King 
38919b0cdefaSRussell King static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
38929b0cdefaSRussell King {
38939b0cdefaSRussell King 	const struct mvneta_statistic *s;
38949b0cdefaSRussell King 	void __iomem *base = pp->base;
38956d81f451SRussell King 	u32 high, low;
38966d81f451SRussell King 	u64 val;
38979b0cdefaSRussell King 	int i;
38989b0cdefaSRussell King 
38999b0cdefaSRussell King 	for (i = 0, s = mvneta_statistics;
39009b0cdefaSRussell King 	     s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
39019b0cdefaSRussell King 	     s++, i++) {
39026d81f451SRussell King 		val = 0;
39036d81f451SRussell King 
39049b0cdefaSRussell King 		switch (s->type) {
39059b0cdefaSRussell King 		case T_REG_32:
39069b0cdefaSRussell King 			val = readl_relaxed(base + s->offset);
39079b0cdefaSRussell King 			break;
39089b0cdefaSRussell King 		case T_REG_64:
39099b0cdefaSRussell King 			/* Docs say to read low 32-bit then high */
39109b0cdefaSRussell King 			low = readl_relaxed(base + s->offset);
39119b0cdefaSRussell King 			high = readl_relaxed(base + s->offset + 4);
39126d81f451SRussell King 			val = (u64)high << 32 | low;
39136d81f451SRussell King 			break;
39146d81f451SRussell King 		case T_SW:
39156d81f451SRussell King 			switch (s->offset) {
39166d81f451SRussell King 			case ETHTOOL_STAT_EEE_WAKEUP:
39176d81f451SRussell King 				val = phylink_get_eee_err(pp->phylink);
39189b0cdefaSRussell King 				break;
39199b0cdefaSRussell King 			}
39206d81f451SRussell King 			break;
39216d81f451SRussell King 		}
39226d81f451SRussell King 
39236d81f451SRussell King 		pp->ethtool_stats[i] += val;
39249b0cdefaSRussell King 	}
39259b0cdefaSRussell King }
39269b0cdefaSRussell King 
39279b0cdefaSRussell King static void mvneta_ethtool_get_stats(struct net_device *dev,
39289b0cdefaSRussell King 				     struct ethtool_stats *stats, u64 *data)
39299b0cdefaSRussell King {
39309b0cdefaSRussell King 	struct mvneta_port *pp = netdev_priv(dev);
39319b0cdefaSRussell King 	int i;
39329b0cdefaSRussell King 
39339b0cdefaSRussell King 	mvneta_ethtool_update_stats(pp);
39349b0cdefaSRussell King 
39359b0cdefaSRussell King 	for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
39369b0cdefaSRussell King 		*data++ = pp->ethtool_stats[i];
39379b0cdefaSRussell King }
39389b0cdefaSRussell King 
39399b0cdefaSRussell King static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
39409b0cdefaSRussell King {
39419b0cdefaSRussell King 	if (sset == ETH_SS_STATS)
39429b0cdefaSRussell King 		return ARRAY_SIZE(mvneta_statistics);
39439b0cdefaSRussell King 	return -EOPNOTSUPP;
39449b0cdefaSRussell King }
39459b0cdefaSRussell King 
39469a401deaSGregory CLEMENT static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
39479a401deaSGregory CLEMENT {
39489a401deaSGregory CLEMENT 	return MVNETA_RSS_LU_TABLE_SIZE;
39499a401deaSGregory CLEMENT }
39509a401deaSGregory CLEMENT 
39519a401deaSGregory CLEMENT static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
39529a401deaSGregory CLEMENT 				    struct ethtool_rxnfc *info,
39539a401deaSGregory CLEMENT 				    u32 *rules __always_unused)
39549a401deaSGregory CLEMENT {
39559a401deaSGregory CLEMENT 	switch (info->cmd) {
39569a401deaSGregory CLEMENT 	case ETHTOOL_GRXRINGS:
39579a401deaSGregory CLEMENT 		info->data =  rxq_number;
39589a401deaSGregory CLEMENT 		return 0;
39599a401deaSGregory CLEMENT 	case ETHTOOL_GRXFH:
39609a401deaSGregory CLEMENT 		return -EOPNOTSUPP;
39619a401deaSGregory CLEMENT 	default:
39629a401deaSGregory CLEMENT 		return -EOPNOTSUPP;
39639a401deaSGregory CLEMENT 	}
39649a401deaSGregory CLEMENT }
39659a401deaSGregory CLEMENT 
39669a401deaSGregory CLEMENT static int  mvneta_config_rss(struct mvneta_port *pp)
39679a401deaSGregory CLEMENT {
39689a401deaSGregory CLEMENT 	int cpu;
39699a401deaSGregory CLEMENT 	u32 val;
39709a401deaSGregory CLEMENT 
39719a401deaSGregory CLEMENT 	netif_tx_stop_all_queues(pp->dev);
39729a401deaSGregory CLEMENT 
39736b125d63SGregory CLEMENT 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
39749a401deaSGregory CLEMENT 
39759a401deaSGregory CLEMENT 	/* We have to synchronise on the napi of each CPU */
39769a401deaSGregory CLEMENT 	for_each_online_cpu(cpu) {
39779a401deaSGregory CLEMENT 		struct mvneta_pcpu_port *pcpu_port =
39789a401deaSGregory CLEMENT 			per_cpu_ptr(pp->ports, cpu);
39799a401deaSGregory CLEMENT 
39809a401deaSGregory CLEMENT 		napi_synchronize(&pcpu_port->napi);
39819a401deaSGregory CLEMENT 		napi_disable(&pcpu_port->napi);
39829a401deaSGregory CLEMENT 	}
39839a401deaSGregory CLEMENT 
39849a401deaSGregory CLEMENT 	pp->rxq_def = pp->indir[0];
39859a401deaSGregory CLEMENT 
39869a401deaSGregory CLEMENT 	/* Update unicast mapping */
39879a401deaSGregory CLEMENT 	mvneta_set_rx_mode(pp->dev);
39889a401deaSGregory CLEMENT 
39899a401deaSGregory CLEMENT 	/* Update val of portCfg register accordingly with all RxQueue types */
39909a401deaSGregory CLEMENT 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
39919a401deaSGregory CLEMENT 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
39929a401deaSGregory CLEMENT 
39939a401deaSGregory CLEMENT 	/* Update the elected CPU matching the new rxq_def */
3994120cfa50SGregory CLEMENT 	spin_lock(&pp->lock);
39959a401deaSGregory CLEMENT 	mvneta_percpu_elect(pp);
3996120cfa50SGregory CLEMENT 	spin_unlock(&pp->lock);
39979a401deaSGregory CLEMENT 
39989a401deaSGregory CLEMENT 	/* We have to synchronise on the napi of each CPU */
39999a401deaSGregory CLEMENT 	for_each_online_cpu(cpu) {
40009a401deaSGregory CLEMENT 		struct mvneta_pcpu_port *pcpu_port =
40019a401deaSGregory CLEMENT 			per_cpu_ptr(pp->ports, cpu);
40029a401deaSGregory CLEMENT 
40039a401deaSGregory CLEMENT 		napi_enable(&pcpu_port->napi);
40049a401deaSGregory CLEMENT 	}
40059a401deaSGregory CLEMENT 
40069a401deaSGregory CLEMENT 	netif_tx_start_all_queues(pp->dev);
40079a401deaSGregory CLEMENT 
40089a401deaSGregory CLEMENT 	return 0;
40099a401deaSGregory CLEMENT }
40109a401deaSGregory CLEMENT 
40119a401deaSGregory CLEMENT static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
40129a401deaSGregory CLEMENT 				   const u8 *key, const u8 hfunc)
40139a401deaSGregory CLEMENT {
40149a401deaSGregory CLEMENT 	struct mvneta_port *pp = netdev_priv(dev);
40152636ac3cSMarcin Wojtas 
40162636ac3cSMarcin Wojtas 	/* Current code for Armada 3700 doesn't support RSS features yet */
40172636ac3cSMarcin Wojtas 	if (pp->neta_armada3700)
40182636ac3cSMarcin Wojtas 		return -EOPNOTSUPP;
40192636ac3cSMarcin Wojtas 
40209a401deaSGregory CLEMENT 	/* We require at least one supported parameter to be changed
40219a401deaSGregory CLEMENT 	 * and no change in any of the unsupported parameters
40229a401deaSGregory CLEMENT 	 */
40239a401deaSGregory CLEMENT 	if (key ||
40249a401deaSGregory CLEMENT 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
40259a401deaSGregory CLEMENT 		return -EOPNOTSUPP;
40269a401deaSGregory CLEMENT 
40279a401deaSGregory CLEMENT 	if (!indir)
40289a401deaSGregory CLEMENT 		return 0;
40299a401deaSGregory CLEMENT 
40309a401deaSGregory CLEMENT 	memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
40319a401deaSGregory CLEMENT 
40329a401deaSGregory CLEMENT 	return mvneta_config_rss(pp);
40339a401deaSGregory CLEMENT }
40349a401deaSGregory CLEMENT 
40359a401deaSGregory CLEMENT static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
40369a401deaSGregory CLEMENT 				   u8 *hfunc)
40379a401deaSGregory CLEMENT {
40389a401deaSGregory CLEMENT 	struct mvneta_port *pp = netdev_priv(dev);
40399a401deaSGregory CLEMENT 
40402636ac3cSMarcin Wojtas 	/* Current code for Armada 3700 doesn't support RSS features yet */
40412636ac3cSMarcin Wojtas 	if (pp->neta_armada3700)
40422636ac3cSMarcin Wojtas 		return -EOPNOTSUPP;
40432636ac3cSMarcin Wojtas 
40449a401deaSGregory CLEMENT 	if (hfunc)
40459a401deaSGregory CLEMENT 		*hfunc = ETH_RSS_HASH_TOP;
40469a401deaSGregory CLEMENT 
40479a401deaSGregory CLEMENT 	if (!indir)
40489a401deaSGregory CLEMENT 		return 0;
40499a401deaSGregory CLEMENT 
40509a401deaSGregory CLEMENT 	memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
40519a401deaSGregory CLEMENT 
40529a401deaSGregory CLEMENT 	return 0;
40539a401deaSGregory CLEMENT }
40549a401deaSGregory CLEMENT 
4055b60a00f9SJingju Hou static void mvneta_ethtool_get_wol(struct net_device *dev,
4056b60a00f9SJingju Hou 				   struct ethtool_wolinfo *wol)
4057b60a00f9SJingju Hou {
4058503f9aa9SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
4059b60a00f9SJingju Hou 
4060503f9aa9SRussell King 	phylink_ethtool_get_wol(pp->phylink, wol);
4061b60a00f9SJingju Hou }
4062b60a00f9SJingju Hou 
4063b60a00f9SJingju Hou static int mvneta_ethtool_set_wol(struct net_device *dev,
4064b60a00f9SJingju Hou 				  struct ethtool_wolinfo *wol)
4065b60a00f9SJingju Hou {
4066503f9aa9SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
406782960fffSJisheng Zhang 	int ret;
406882960fffSJisheng Zhang 
4069503f9aa9SRussell King 	ret = phylink_ethtool_set_wol(pp->phylink, wol);
407082960fffSJisheng Zhang 	if (!ret)
407182960fffSJisheng Zhang 		device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
407282960fffSJisheng Zhang 
407382960fffSJisheng Zhang 	return ret;
4074b60a00f9SJingju Hou }
4075b60a00f9SJingju Hou 
4076c554f531SRussell King static int mvneta_ethtool_get_module_info(struct net_device *dev,
4077c554f531SRussell King 					  struct ethtool_modinfo *modinfo)
4078c554f531SRussell King {
4079c554f531SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
4080c554f531SRussell King 
4081c554f531SRussell King 	return phylink_ethtool_get_module_info(pp->phylink, modinfo);
4082c554f531SRussell King }
4083c554f531SRussell King 
4084c554f531SRussell King static int mvneta_ethtool_get_module_eeprom(struct net_device *dev,
4085c554f531SRussell King 					    struct ethtool_eeprom *ee, u8 *buf)
4086c554f531SRussell King {
4087c554f531SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
4088c554f531SRussell King 
4089c554f531SRussell King 	return phylink_ethtool_get_module_eeprom(pp->phylink, ee, buf);
4090c554f531SRussell King }
4091c554f531SRussell King 
40926d81f451SRussell King static int mvneta_ethtool_get_eee(struct net_device *dev,
40936d81f451SRussell King 				  struct ethtool_eee *eee)
40946d81f451SRussell King {
40956d81f451SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
40966d81f451SRussell King 	u32 lpi_ctl0;
40976d81f451SRussell King 
40986d81f451SRussell King 	lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
40996d81f451SRussell King 
41006d81f451SRussell King 	eee->eee_enabled = pp->eee_enabled;
41016d81f451SRussell King 	eee->eee_active = pp->eee_active;
41026d81f451SRussell King 	eee->tx_lpi_enabled = pp->tx_lpi_enabled;
41036d81f451SRussell King 	eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale;
41046d81f451SRussell King 
41056d81f451SRussell King 	return phylink_ethtool_get_eee(pp->phylink, eee);
41066d81f451SRussell King }
41076d81f451SRussell King 
41086d81f451SRussell King static int mvneta_ethtool_set_eee(struct net_device *dev,
41096d81f451SRussell King 				  struct ethtool_eee *eee)
41106d81f451SRussell King {
41116d81f451SRussell King 	struct mvneta_port *pp = netdev_priv(dev);
41126d81f451SRussell King 	u32 lpi_ctl0;
41136d81f451SRussell King 
41146d81f451SRussell King 	/* The Armada 37x documents do not give limits for this other than
41156d81f451SRussell King 	 * it being an 8-bit register. */
41166d81f451SRussell King 	if (eee->tx_lpi_enabled &&
41176d81f451SRussell King 	    (eee->tx_lpi_timer < 0 || eee->tx_lpi_timer > 255))
41186d81f451SRussell King 		return -EINVAL;
41196d81f451SRussell King 
41206d81f451SRussell King 	lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
41216d81f451SRussell King 	lpi_ctl0 &= ~(0xff << 8);
41226d81f451SRussell King 	lpi_ctl0 |= eee->tx_lpi_timer << 8;
41236d81f451SRussell King 	mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0);
41246d81f451SRussell King 
41256d81f451SRussell King 	pp->eee_enabled = eee->eee_enabled;
41266d81f451SRussell King 	pp->tx_lpi_enabled = eee->tx_lpi_enabled;
41276d81f451SRussell King 
41286d81f451SRussell King 	mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled);
41296d81f451SRussell King 
41306d81f451SRussell King 	return phylink_ethtool_set_eee(pp->phylink, eee);
41316d81f451SRussell King }
41326d81f451SRussell King 
4133c5aff182SThomas Petazzoni static const struct net_device_ops mvneta_netdev_ops = {
4134c5aff182SThomas Petazzoni 	.ndo_open            = mvneta_open,
4135c5aff182SThomas Petazzoni 	.ndo_stop            = mvneta_stop,
4136c5aff182SThomas Petazzoni 	.ndo_start_xmit      = mvneta_tx,
4137c5aff182SThomas Petazzoni 	.ndo_set_rx_mode     = mvneta_set_rx_mode,
4138c5aff182SThomas Petazzoni 	.ndo_set_mac_address = mvneta_set_mac_addr,
4139c5aff182SThomas Petazzoni 	.ndo_change_mtu      = mvneta_change_mtu,
4140b65657fcSSimon Guinot 	.ndo_fix_features    = mvneta_fix_features,
4141c5aff182SThomas Petazzoni 	.ndo_get_stats64     = mvneta_get_stats64,
414215f59456SThomas Petazzoni 	.ndo_do_ioctl        = mvneta_ioctl,
4143c5aff182SThomas Petazzoni };
4144c5aff182SThomas Petazzoni 
41454581be42SJisheng Zhang static const struct ethtool_ops mvneta_eth_tool_ops = {
4146503f9aa9SRussell King 	.nway_reset	= mvneta_ethtool_nway_reset,
4147c5aff182SThomas Petazzoni 	.get_link       = ethtool_op_get_link,
4148c5aff182SThomas Petazzoni 	.set_coalesce   = mvneta_ethtool_set_coalesce,
4149c5aff182SThomas Petazzoni 	.get_coalesce   = mvneta_ethtool_get_coalesce,
4150c5aff182SThomas Petazzoni 	.get_drvinfo    = mvneta_ethtool_get_drvinfo,
4151c5aff182SThomas Petazzoni 	.get_ringparam  = mvneta_ethtool_get_ringparam,
4152c5aff182SThomas Petazzoni 	.set_ringparam	= mvneta_ethtool_set_ringparam,
41534932a918SRussell King 	.get_pauseparam	= mvneta_ethtool_get_pauseparam,
41544932a918SRussell King 	.set_pauseparam	= mvneta_ethtool_set_pauseparam,
41559b0cdefaSRussell King 	.get_strings	= mvneta_ethtool_get_strings,
41569b0cdefaSRussell King 	.get_ethtool_stats = mvneta_ethtool_get_stats,
41579b0cdefaSRussell King 	.get_sset_count	= mvneta_ethtool_get_sset_count,
41589a401deaSGregory CLEMENT 	.get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
41599a401deaSGregory CLEMENT 	.get_rxnfc	= mvneta_ethtool_get_rxnfc,
41609a401deaSGregory CLEMENT 	.get_rxfh	= mvneta_ethtool_get_rxfh,
41619a401deaSGregory CLEMENT 	.set_rxfh	= mvneta_ethtool_set_rxfh,
4162503f9aa9SRussell King 	.get_link_ksettings = mvneta_ethtool_get_link_ksettings,
4163013ad40dSPhilippe Reynes 	.set_link_ksettings = mvneta_ethtool_set_link_ksettings,
4164b60a00f9SJingju Hou 	.get_wol        = mvneta_ethtool_get_wol,
4165b60a00f9SJingju Hou 	.set_wol        = mvneta_ethtool_set_wol,
4166c554f531SRussell King 	.get_module_info = mvneta_ethtool_get_module_info,
4167c554f531SRussell King 	.get_module_eeprom = mvneta_ethtool_get_module_eeprom,
41686d81f451SRussell King 	.get_eee	= mvneta_ethtool_get_eee,
41696d81f451SRussell King 	.set_eee	= mvneta_ethtool_set_eee,
4170c5aff182SThomas Petazzoni };
4171c5aff182SThomas Petazzoni 
4172c5aff182SThomas Petazzoni /* Initialize hw */
41739672850bSEzequiel Garcia static int mvneta_init(struct device *dev, struct mvneta_port *pp)
4174c5aff182SThomas Petazzoni {
4175c5aff182SThomas Petazzoni 	int queue;
4176c5aff182SThomas Petazzoni 
4177c5aff182SThomas Petazzoni 	/* Disable port */
4178c5aff182SThomas Petazzoni 	mvneta_port_disable(pp);
4179c5aff182SThomas Petazzoni 
4180c5aff182SThomas Petazzoni 	/* Set port default values */
4181c5aff182SThomas Petazzoni 	mvneta_defaults_set(pp);
4182c5aff182SThomas Petazzoni 
41835d6312edSMarkus Elfring 	pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
4184c5aff182SThomas Petazzoni 	if (!pp->txqs)
4185c5aff182SThomas Petazzoni 		return -ENOMEM;
4186c5aff182SThomas Petazzoni 
4187c5aff182SThomas Petazzoni 	/* Initialize TX descriptor rings */
4188c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
4189c5aff182SThomas Petazzoni 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
4190c5aff182SThomas Petazzoni 		txq->id = queue;
4191c5aff182SThomas Petazzoni 		txq->size = pp->tx_ring_size;
4192c5aff182SThomas Petazzoni 		txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4193c5aff182SThomas Petazzoni 	}
4194c5aff182SThomas Petazzoni 
41955d6312edSMarkus Elfring 	pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
41969672850bSEzequiel Garcia 	if (!pp->rxqs)
4197c5aff182SThomas Petazzoni 		return -ENOMEM;
4198c5aff182SThomas Petazzoni 
4199c5aff182SThomas Petazzoni 	/* Create Rx descriptor rings */
4200c5aff182SThomas Petazzoni 	for (queue = 0; queue < rxq_number; queue++) {
4201c5aff182SThomas Petazzoni 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4202c5aff182SThomas Petazzoni 		rxq->id = queue;
4203c5aff182SThomas Petazzoni 		rxq->size = pp->rx_ring_size;
4204c5aff182SThomas Petazzoni 		rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4205c5aff182SThomas Petazzoni 		rxq->time_coal = MVNETA_RX_COAL_USEC;
420629110630SMarkus Elfring 		rxq->buf_virt_addr
420729110630SMarkus Elfring 			= devm_kmalloc_array(pp->dev->dev.parent,
420829110630SMarkus Elfring 					     rxq->size,
420929110630SMarkus Elfring 					     sizeof(*rxq->buf_virt_addr),
4210f88bee1cSGregory CLEMENT 					     GFP_KERNEL);
4211f88bee1cSGregory CLEMENT 		if (!rxq->buf_virt_addr)
4212f88bee1cSGregory CLEMENT 			return -ENOMEM;
4213c5aff182SThomas Petazzoni 	}
4214c5aff182SThomas Petazzoni 
4215c5aff182SThomas Petazzoni 	return 0;
4216c5aff182SThomas Petazzoni }
4217c5aff182SThomas Petazzoni 
4218c5aff182SThomas Petazzoni /* platform glue : initialize decoding windows */
421903ce758eSGreg KH static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4220c5aff182SThomas Petazzoni 				     const struct mbus_dram_target_info *dram)
4221c5aff182SThomas Petazzoni {
4222c5aff182SThomas Petazzoni 	u32 win_enable;
4223c5aff182SThomas Petazzoni 	u32 win_protect;
4224c5aff182SThomas Petazzoni 	int i;
4225c5aff182SThomas Petazzoni 
4226c5aff182SThomas Petazzoni 	for (i = 0; i < 6; i++) {
4227c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4228c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4229c5aff182SThomas Petazzoni 
4230c5aff182SThomas Petazzoni 		if (i < 4)
4231c5aff182SThomas Petazzoni 			mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4232c5aff182SThomas Petazzoni 	}
4233c5aff182SThomas Petazzoni 
4234c5aff182SThomas Petazzoni 	win_enable = 0x3f;
4235c5aff182SThomas Petazzoni 	win_protect = 0;
4236c5aff182SThomas Petazzoni 
42372636ac3cSMarcin Wojtas 	if (dram) {
4238c5aff182SThomas Petazzoni 		for (i = 0; i < dram->num_cs; i++) {
4239c5aff182SThomas Petazzoni 			const struct mbus_dram_window *cs = dram->cs + i;
42402636ac3cSMarcin Wojtas 
42412636ac3cSMarcin Wojtas 			mvreg_write(pp, MVNETA_WIN_BASE(i),
42422636ac3cSMarcin Wojtas 				    (cs->base & 0xffff0000) |
42432636ac3cSMarcin Wojtas 				    (cs->mbus_attr << 8) |
42442636ac3cSMarcin Wojtas 				    dram->mbus_dram_target_id);
4245c5aff182SThomas Petazzoni 
4246c5aff182SThomas Petazzoni 			mvreg_write(pp, MVNETA_WIN_SIZE(i),
4247c5aff182SThomas Petazzoni 				    (cs->size - 1) & 0xffff0000);
4248c5aff182SThomas Petazzoni 
4249c5aff182SThomas Petazzoni 			win_enable &= ~(1 << i);
4250c5aff182SThomas Petazzoni 			win_protect |= 3 << (2 * i);
4251c5aff182SThomas Petazzoni 		}
42522636ac3cSMarcin Wojtas 	} else {
42532636ac3cSMarcin Wojtas 		/* For Armada3700 open default 4GB Mbus window, leaving
42542636ac3cSMarcin Wojtas 		 * arbitration of target/attribute to a different layer
42552636ac3cSMarcin Wojtas 		 * of configuration.
42562636ac3cSMarcin Wojtas 		 */
42572636ac3cSMarcin Wojtas 		mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
42582636ac3cSMarcin Wojtas 		win_enable &= ~BIT(0);
42592636ac3cSMarcin Wojtas 		win_protect = 3;
42602636ac3cSMarcin Wojtas 	}
4261c5aff182SThomas Petazzoni 
4262c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
4263db6ba9a5SMarcin Wojtas 	mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
4264c5aff182SThomas Petazzoni }
4265c5aff182SThomas Petazzoni 
4266c5aff182SThomas Petazzoni /* Power up the port */
42673f1dd4bcSThomas Petazzoni static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
4268c5aff182SThomas Petazzoni {
4269c5aff182SThomas Petazzoni 	/* MAC Cause register should be cleared */
4270c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4271c5aff182SThomas Petazzoni 
427232699954SRussell King 	if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
42733f1dd4bcSThomas Petazzoni 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
427422f4bf8aSRussell King 	else if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
427522f4bf8aSRussell King 		 phy_mode == PHY_INTERFACE_MODE_1000BASEX)
42763f1dd4bcSThomas Petazzoni 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
427732699954SRussell King 	else if (!phy_interface_mode_is_rgmii(phy_mode))
42783f1dd4bcSThomas Petazzoni 		return -EINVAL;
42793f1dd4bcSThomas Petazzoni 
42803f1dd4bcSThomas Petazzoni 	return 0;
4281c5aff182SThomas Petazzoni }
4282c5aff182SThomas Petazzoni 
4283c5aff182SThomas Petazzoni /* Device initialization routine */
428403ce758eSGreg KH static int mvneta_probe(struct platform_device *pdev)
4285c5aff182SThomas Petazzoni {
4286c3f0dd38SThomas Petazzoni 	struct resource *res;
4287c5aff182SThomas Petazzoni 	struct device_node *dn = pdev->dev.of_node;
4288dc35a10fSMarcin Wojtas 	struct device_node *bm_node;
4289c5aff182SThomas Petazzoni 	struct mvneta_port *pp;
4290c5aff182SThomas Petazzoni 	struct net_device *dev;
4291503f9aa9SRussell King 	struct phylink *phylink;
42928cc3e439SThomas Petazzoni 	const char *dt_mac_addr;
42938cc3e439SThomas Petazzoni 	char hw_mac_addr[ETH_ALEN];
42948cc3e439SThomas Petazzoni 	const char *mac_from;
42959110ee07SMarcin Wojtas 	int tx_csum_limit;
4296c5aff182SThomas Petazzoni 	int phy_mode;
4297c5aff182SThomas Petazzoni 	int err;
429812bb03b4SMaxime Ripard 	int cpu;
4299c5aff182SThomas Petazzoni 
4300ee40a116SWilly Tarreau 	dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
4301c5aff182SThomas Petazzoni 	if (!dev)
4302c5aff182SThomas Petazzoni 		return -ENOMEM;
4303c5aff182SThomas Petazzoni 
4304c5aff182SThomas Petazzoni 	dev->irq = irq_of_parse_and_map(dn, 0);
4305c5aff182SThomas Petazzoni 	if (dev->irq == 0) {
4306c5aff182SThomas Petazzoni 		err = -EINVAL;
4307c5aff182SThomas Petazzoni 		goto err_free_netdev;
4308c5aff182SThomas Petazzoni 	}
4309c5aff182SThomas Petazzoni 
4310c5aff182SThomas Petazzoni 	phy_mode = of_get_phy_mode(dn);
4311c5aff182SThomas Petazzoni 	if (phy_mode < 0) {
4312c5aff182SThomas Petazzoni 		dev_err(&pdev->dev, "incorrect phy-mode\n");
4313c5aff182SThomas Petazzoni 		err = -EINVAL;
4314503f9aa9SRussell King 		goto err_free_irq;
4315503f9aa9SRussell King 	}
4316503f9aa9SRussell King 
4317503f9aa9SRussell King 	phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode,
4318503f9aa9SRussell King 				 &mvneta_phylink_ops);
4319503f9aa9SRussell King 	if (IS_ERR(phylink)) {
4320503f9aa9SRussell King 		err = PTR_ERR(phylink);
4321503f9aa9SRussell King 		goto err_free_irq;
4322c5aff182SThomas Petazzoni 	}
4323c5aff182SThomas Petazzoni 
4324c5aff182SThomas Petazzoni 	dev->tx_queue_len = MVNETA_MAX_TXD;
4325c5aff182SThomas Petazzoni 	dev->watchdog_timeo = 5 * HZ;
4326c5aff182SThomas Petazzoni 	dev->netdev_ops = &mvneta_netdev_ops;
4327c5aff182SThomas Petazzoni 
43287ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &mvneta_eth_tool_ops;
4329c5aff182SThomas Petazzoni 
4330c5aff182SThomas Petazzoni 	pp = netdev_priv(dev);
43311c2722a9SGregory CLEMENT 	spin_lock_init(&pp->lock);
4332503f9aa9SRussell King 	pp->phylink = phylink;
4333c5aff182SThomas Petazzoni 	pp->phy_interface = phy_mode;
4334503f9aa9SRussell King 	pp->dn = dn;
4335c5aff182SThomas Petazzoni 
433690b74c01SGregory CLEMENT 	pp->rxq_def = rxq_def;
433790b74c01SGregory CLEMENT 
43388d5047cfSMarcin Wojtas 	/* Set RX packet offset correction for platforms, whose
43398d5047cfSMarcin Wojtas 	 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
43408d5047cfSMarcin Wojtas 	 * platforms and 0B for 32-bit ones.
43418d5047cfSMarcin Wojtas 	 */
43428d5047cfSMarcin Wojtas 	pp->rx_offset_correction =
43438d5047cfSMarcin Wojtas 		max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
43448d5047cfSMarcin Wojtas 
43459a401deaSGregory CLEMENT 	pp->indir[0] = rxq_def;
43469a401deaSGregory CLEMENT 
43472636ac3cSMarcin Wojtas 	/* Get special SoC configurations */
43482636ac3cSMarcin Wojtas 	if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
43492636ac3cSMarcin Wojtas 		pp->neta_armada3700 = true;
43502636ac3cSMarcin Wojtas 
43512804ba4eSJisheng Zhang 	pp->clk = devm_clk_get(&pdev->dev, "core");
43522804ba4eSJisheng Zhang 	if (IS_ERR(pp->clk))
4353189dd626SThomas Petazzoni 		pp->clk = devm_clk_get(&pdev->dev, NULL);
4354189dd626SThomas Petazzoni 	if (IS_ERR(pp->clk)) {
4355189dd626SThomas Petazzoni 		err = PTR_ERR(pp->clk);
4356503f9aa9SRussell King 		goto err_free_phylink;
4357189dd626SThomas Petazzoni 	}
4358189dd626SThomas Petazzoni 
4359189dd626SThomas Petazzoni 	clk_prepare_enable(pp->clk);
4360189dd626SThomas Petazzoni 
436115cc4a4aSJisheng Zhang 	pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
436215cc4a4aSJisheng Zhang 	if (!IS_ERR(pp->clk_bus))
436315cc4a4aSJisheng Zhang 		clk_prepare_enable(pp->clk_bus);
436415cc4a4aSJisheng Zhang 
4365c3f0dd38SThomas Petazzoni 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4366c3f0dd38SThomas Petazzoni 	pp->base = devm_ioremap_resource(&pdev->dev, res);
4367c3f0dd38SThomas Petazzoni 	if (IS_ERR(pp->base)) {
4368c3f0dd38SThomas Petazzoni 		err = PTR_ERR(pp->base);
43695445eaf3SArnaud Patard \(Rtp\) 		goto err_clk;
43705445eaf3SArnaud Patard \(Rtp\) 	}
43715445eaf3SArnaud Patard \(Rtp\) 
437212bb03b4SMaxime Ripard 	/* Alloc per-cpu port structure */
437312bb03b4SMaxime Ripard 	pp->ports = alloc_percpu(struct mvneta_pcpu_port);
437412bb03b4SMaxime Ripard 	if (!pp->ports) {
437512bb03b4SMaxime Ripard 		err = -ENOMEM;
437612bb03b4SMaxime Ripard 		goto err_clk;
437712bb03b4SMaxime Ripard 	}
437812bb03b4SMaxime Ripard 
437974c41b04Swilly tarreau 	/* Alloc per-cpu stats */
43801c213bd2SWANG Cong 	pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
438174c41b04Swilly tarreau 	if (!pp->stats) {
438274c41b04Swilly tarreau 		err = -ENOMEM;
438312bb03b4SMaxime Ripard 		goto err_free_ports;
438474c41b04Swilly tarreau 	}
438574c41b04Swilly tarreau 
43868cc3e439SThomas Petazzoni 	dt_mac_addr = of_get_mac_address(dn);
43876c7a9a3cSLuka Perkov 	if (dt_mac_addr) {
43888cc3e439SThomas Petazzoni 		mac_from = "device tree";
43898cc3e439SThomas Petazzoni 		memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
43908cc3e439SThomas Petazzoni 	} else {
43918cc3e439SThomas Petazzoni 		mvneta_get_mac_addr(pp, hw_mac_addr);
43928cc3e439SThomas Petazzoni 		if (is_valid_ether_addr(hw_mac_addr)) {
43938cc3e439SThomas Petazzoni 			mac_from = "hardware";
43948cc3e439SThomas Petazzoni 			memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
43958cc3e439SThomas Petazzoni 		} else {
43968cc3e439SThomas Petazzoni 			mac_from = "random";
43978cc3e439SThomas Petazzoni 			eth_hw_addr_random(dev);
43988cc3e439SThomas Petazzoni 		}
43998cc3e439SThomas Petazzoni 	}
44008cc3e439SThomas Petazzoni 
44019110ee07SMarcin Wojtas 	if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
44029110ee07SMarcin Wojtas 		if (tx_csum_limit < 0 ||
44039110ee07SMarcin Wojtas 		    tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
44049110ee07SMarcin Wojtas 			tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
44059110ee07SMarcin Wojtas 			dev_info(&pdev->dev,
44069110ee07SMarcin Wojtas 				 "Wrong TX csum limit in DT, set to %dB\n",
44079110ee07SMarcin Wojtas 				 MVNETA_TX_CSUM_DEF_SIZE);
44089110ee07SMarcin Wojtas 		}
44099110ee07SMarcin Wojtas 	} else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
44109110ee07SMarcin Wojtas 		tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
44119110ee07SMarcin Wojtas 	} else {
44129110ee07SMarcin Wojtas 		tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
44139110ee07SMarcin Wojtas 	}
44149110ee07SMarcin Wojtas 
44159110ee07SMarcin Wojtas 	pp->tx_csum_limit = tx_csum_limit;
4416b65657fcSSimon Guinot 
44179768b45cSJane Li 	pp->dram_target_info = mv_mbus_dram_info();
44182636ac3cSMarcin Wojtas 	/* Armada3700 requires setting default configuration of Mbus
44192636ac3cSMarcin Wojtas 	 * windows, however without using filled mbus_dram_target_info
44202636ac3cSMarcin Wojtas 	 * structure.
44212636ac3cSMarcin Wojtas 	 */
44229768b45cSJane Li 	if (pp->dram_target_info || pp->neta_armada3700)
44239768b45cSJane Li 		mvneta_conf_mbus_windows(pp, pp->dram_target_info);
4424dc35a10fSMarcin Wojtas 
4425c5aff182SThomas Petazzoni 	pp->tx_ring_size = MVNETA_MAX_TXD;
4426c5aff182SThomas Petazzoni 	pp->rx_ring_size = MVNETA_MAX_RXD;
4427c5aff182SThomas Petazzoni 
4428c5aff182SThomas Petazzoni 	pp->dev = dev;
4429c5aff182SThomas Petazzoni 	SET_NETDEV_DEV(dev, &pdev->dev);
4430c5aff182SThomas Petazzoni 
4431dc35a10fSMarcin Wojtas 	pp->id = global_port_id++;
4432dc35a10fSMarcin Wojtas 
4433dc35a10fSMarcin Wojtas 	/* Obtain access to BM resources if enabled and already initialized */
4434dc35a10fSMarcin Wojtas 	bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4435dc35a10fSMarcin Wojtas 	if (bm_node && bm_node->data) {
4436dc35a10fSMarcin Wojtas 		pp->bm_priv = bm_node->data;
4437dc35a10fSMarcin Wojtas 		err = mvneta_bm_port_init(pdev, pp);
4438dc35a10fSMarcin Wojtas 		if (err < 0) {
4439dc35a10fSMarcin Wojtas 			dev_info(&pdev->dev, "use SW buffer management\n");
4440dc35a10fSMarcin Wojtas 			pp->bm_priv = NULL;
4441dc35a10fSMarcin Wojtas 		}
4442dc35a10fSMarcin Wojtas 	}
4443d4e4da00SPeter Chen 	of_node_put(bm_node);
4444dc35a10fSMarcin Wojtas 
44459672850bSEzequiel Garcia 	err = mvneta_init(&pdev->dev, pp);
44469672850bSEzequiel Garcia 	if (err < 0)
4447dc35a10fSMarcin Wojtas 		goto err_netdev;
44483f1dd4bcSThomas Petazzoni 
44493f1dd4bcSThomas Petazzoni 	err = mvneta_port_power_up(pp, phy_mode);
44503f1dd4bcSThomas Petazzoni 	if (err < 0) {
44513f1dd4bcSThomas Petazzoni 		dev_err(&pdev->dev, "can't power up port\n");
4452dc35a10fSMarcin Wojtas 		goto err_netdev;
44533f1dd4bcSThomas Petazzoni 	}
4454c5aff182SThomas Petazzoni 
44552636ac3cSMarcin Wojtas 	/* Armada3700 network controller does not support per-cpu
44562636ac3cSMarcin Wojtas 	 * operation, so only single NAPI should be initialized.
44572636ac3cSMarcin Wojtas 	 */
44582636ac3cSMarcin Wojtas 	if (pp->neta_armada3700) {
44592636ac3cSMarcin Wojtas 		netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
44602636ac3cSMarcin Wojtas 	} else {
446112bb03b4SMaxime Ripard 		for_each_present_cpu(cpu) {
44622636ac3cSMarcin Wojtas 			struct mvneta_pcpu_port *port =
44632636ac3cSMarcin Wojtas 				per_cpu_ptr(pp->ports, cpu);
446412bb03b4SMaxime Ripard 
44652636ac3cSMarcin Wojtas 			netif_napi_add(dev, &port->napi, mvneta_poll,
44662636ac3cSMarcin Wojtas 				       NAPI_POLL_WEIGHT);
446712bb03b4SMaxime Ripard 			port->pp = pp;
446812bb03b4SMaxime Ripard 		}
44692636ac3cSMarcin Wojtas 	}
4470c5aff182SThomas Petazzoni 
4471c54a5048SAndrew Pilloud 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_TSO;
447201ef26caSEzequiel Garcia 	dev->hw_features |= dev->features;
447301ef26caSEzequiel Garcia 	dev->vlan_features |= dev->features;
447497db8afaSAndrew Lunn 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
44758eef5f97SEzequiel Garcia 	dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
4476b50b72deSwilly tarreau 
44775777987eSJarod Wilson 	/* MTU range: 68 - 9676 */
44785777987eSJarod Wilson 	dev->min_mtu = ETH_MIN_MTU;
44795777987eSJarod Wilson 	/* 9676 == 9700 - 20 and rounding to 8 */
44805777987eSJarod Wilson 	dev->max_mtu = 9676;
44815777987eSJarod Wilson 
4482c5aff182SThomas Petazzoni 	err = register_netdev(dev);
4483c5aff182SThomas Petazzoni 	if (err < 0) {
4484c5aff182SThomas Petazzoni 		dev_err(&pdev->dev, "failed to register\n");
44859672850bSEzequiel Garcia 		goto err_free_stats;
4486c5aff182SThomas Petazzoni 	}
4487c5aff182SThomas Petazzoni 
44888cc3e439SThomas Petazzoni 	netdev_info(dev, "Using %s mac address %pM\n", mac_from,
44898cc3e439SThomas Petazzoni 		    dev->dev_addr);
4490c5aff182SThomas Petazzoni 
4491c5aff182SThomas Petazzoni 	platform_set_drvdata(pdev, pp->dev);
4492c5aff182SThomas Petazzoni 
4493c5aff182SThomas Petazzoni 	return 0;
4494c5aff182SThomas Petazzoni 
4495dc35a10fSMarcin Wojtas err_netdev:
4496dc35a10fSMarcin Wojtas 	unregister_netdev(dev);
4497dc35a10fSMarcin Wojtas 	if (pp->bm_priv) {
4498dc35a10fSMarcin Wojtas 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4499dc35a10fSMarcin Wojtas 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4500dc35a10fSMarcin Wojtas 				       1 << pp->id);
4501dc35a10fSMarcin Wojtas 	}
450274c41b04Swilly tarreau err_free_stats:
450374c41b04Swilly tarreau 	free_percpu(pp->stats);
450412bb03b4SMaxime Ripard err_free_ports:
450512bb03b4SMaxime Ripard 	free_percpu(pp->ports);
45065445eaf3SArnaud Patard \(Rtp\) err_clk:
450715cc4a4aSJisheng Zhang 	clk_disable_unprepare(pp->clk_bus);
45085445eaf3SArnaud Patard \(Rtp\) 	clk_disable_unprepare(pp->clk);
4509503f9aa9SRussell King err_free_phylink:
4510503f9aa9SRussell King 	if (pp->phylink)
4511503f9aa9SRussell King 		phylink_destroy(pp->phylink);
4512c5aff182SThomas Petazzoni err_free_irq:
4513c5aff182SThomas Petazzoni 	irq_dispose_mapping(dev->irq);
4514c5aff182SThomas Petazzoni err_free_netdev:
4515c5aff182SThomas Petazzoni 	free_netdev(dev);
4516c5aff182SThomas Petazzoni 	return err;
4517c5aff182SThomas Petazzoni }
4518c5aff182SThomas Petazzoni 
4519c5aff182SThomas Petazzoni /* Device removal routine */
452003ce758eSGreg KH static int mvneta_remove(struct platform_device *pdev)
4521c5aff182SThomas Petazzoni {
4522c5aff182SThomas Petazzoni 	struct net_device  *dev = platform_get_drvdata(pdev);
4523c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
4524c5aff182SThomas Petazzoni 
4525c5aff182SThomas Petazzoni 	unregister_netdev(dev);
452615cc4a4aSJisheng Zhang 	clk_disable_unprepare(pp->clk_bus);
4527189dd626SThomas Petazzoni 	clk_disable_unprepare(pp->clk);
452812bb03b4SMaxime Ripard 	free_percpu(pp->ports);
452974c41b04Swilly tarreau 	free_percpu(pp->stats);
4530c5aff182SThomas Petazzoni 	irq_dispose_mapping(dev->irq);
4531503f9aa9SRussell King 	phylink_destroy(pp->phylink);
4532c5aff182SThomas Petazzoni 	free_netdev(dev);
4533c5aff182SThomas Petazzoni 
4534dc35a10fSMarcin Wojtas 	if (pp->bm_priv) {
4535dc35a10fSMarcin Wojtas 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4536dc35a10fSMarcin Wojtas 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4537dc35a10fSMarcin Wojtas 				       1 << pp->id);
4538dc35a10fSMarcin Wojtas 	}
4539dc35a10fSMarcin Wojtas 
4540c5aff182SThomas Petazzoni 	return 0;
4541c5aff182SThomas Petazzoni }
4542c5aff182SThomas Petazzoni 
45439768b45cSJane Li #ifdef CONFIG_PM_SLEEP
45449768b45cSJane Li static int mvneta_suspend(struct device *device)
45459768b45cSJane Li {
45469768b45cSJane Li 	struct net_device *dev = dev_get_drvdata(device);
45479768b45cSJane Li 	struct mvneta_port *pp = netdev_priv(dev);
45489768b45cSJane Li 
45493b8bc674SRussell King 	rtnl_lock();
45509768b45cSJane Li 	if (netif_running(dev))
45519768b45cSJane Li 		mvneta_stop(dev);
45523b8bc674SRussell King 	rtnl_unlock();
45539768b45cSJane Li 	netif_device_detach(dev);
45549768b45cSJane Li 	clk_disable_unprepare(pp->clk_bus);
45559768b45cSJane Li 	clk_disable_unprepare(pp->clk);
45569768b45cSJane Li 	return 0;
45579768b45cSJane Li }
45589768b45cSJane Li 
45599768b45cSJane Li static int mvneta_resume(struct device *device)
45609768b45cSJane Li {
45619768b45cSJane Li 	struct platform_device *pdev = to_platform_device(device);
45629768b45cSJane Li 	struct net_device *dev = dev_get_drvdata(device);
45639768b45cSJane Li 	struct mvneta_port *pp = netdev_priv(dev);
45649768b45cSJane Li 	int err;
45659768b45cSJane Li 
45669768b45cSJane Li 	clk_prepare_enable(pp->clk);
45679768b45cSJane Li 	if (!IS_ERR(pp->clk_bus))
45689768b45cSJane Li 		clk_prepare_enable(pp->clk_bus);
45699768b45cSJane Li 	if (pp->dram_target_info || pp->neta_armada3700)
45709768b45cSJane Li 		mvneta_conf_mbus_windows(pp, pp->dram_target_info);
45719768b45cSJane Li 	if (pp->bm_priv) {
45729768b45cSJane Li 		err = mvneta_bm_port_init(pdev, pp);
45739768b45cSJane Li 		if (err < 0) {
45749768b45cSJane Li 			dev_info(&pdev->dev, "use SW buffer management\n");
45759768b45cSJane Li 			pp->bm_priv = NULL;
45769768b45cSJane Li 		}
45779768b45cSJane Li 	}
45789768b45cSJane Li 	mvneta_defaults_set(pp);
45799768b45cSJane Li 	err = mvneta_port_power_up(pp, pp->phy_interface);
45809768b45cSJane Li 	if (err < 0) {
45819768b45cSJane Li 		dev_err(device, "can't power up port\n");
45829768b45cSJane Li 		return err;
45839768b45cSJane Li 	}
45849768b45cSJane Li 
45859768b45cSJane Li 	netif_device_attach(dev);
45863b8bc674SRussell King 	rtnl_lock();
4587d6956ac8SJisheng Zhang 	if (netif_running(dev)) {
45889768b45cSJane Li 		mvneta_open(dev);
4589d6956ac8SJisheng Zhang 		mvneta_set_rx_mode(dev);
4590d6956ac8SJisheng Zhang 	}
45913b8bc674SRussell King 	rtnl_unlock();
4592d6956ac8SJisheng Zhang 
45939768b45cSJane Li 	return 0;
45949768b45cSJane Li }
45959768b45cSJane Li #endif
45969768b45cSJane Li 
45979768b45cSJane Li static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
45989768b45cSJane Li 
4599c5aff182SThomas Petazzoni static const struct of_device_id mvneta_match[] = {
4600c5aff182SThomas Petazzoni 	{ .compatible = "marvell,armada-370-neta" },
4601f522a975SSimon Guinot 	{ .compatible = "marvell,armada-xp-neta" },
46022636ac3cSMarcin Wojtas 	{ .compatible = "marvell,armada-3700-neta" },
4603c5aff182SThomas Petazzoni 	{ }
4604c5aff182SThomas Petazzoni };
4605c5aff182SThomas Petazzoni MODULE_DEVICE_TABLE(of, mvneta_match);
4606c5aff182SThomas Petazzoni 
4607c5aff182SThomas Petazzoni static struct platform_driver mvneta_driver = {
4608c5aff182SThomas Petazzoni 	.probe = mvneta_probe,
460903ce758eSGreg KH 	.remove = mvneta_remove,
4610c5aff182SThomas Petazzoni 	.driver = {
4611c5aff182SThomas Petazzoni 		.name = MVNETA_DRIVER_NAME,
4612c5aff182SThomas Petazzoni 		.of_match_table = mvneta_match,
46139768b45cSJane Li 		.pm = &mvneta_pm_ops,
4614c5aff182SThomas Petazzoni 	},
4615c5aff182SThomas Petazzoni };
4616c5aff182SThomas Petazzoni 
461784a3f4dbSSebastian Andrzej Siewior static int __init mvneta_driver_init(void)
461884a3f4dbSSebastian Andrzej Siewior {
461984a3f4dbSSebastian Andrzej Siewior 	int ret;
462084a3f4dbSSebastian Andrzej Siewior 
462184a3f4dbSSebastian Andrzej Siewior 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
462284a3f4dbSSebastian Andrzej Siewior 				      mvneta_cpu_online,
462384a3f4dbSSebastian Andrzej Siewior 				      mvneta_cpu_down_prepare);
462484a3f4dbSSebastian Andrzej Siewior 	if (ret < 0)
462584a3f4dbSSebastian Andrzej Siewior 		goto out;
462684a3f4dbSSebastian Andrzej Siewior 	online_hpstate = ret;
462784a3f4dbSSebastian Andrzej Siewior 	ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
462884a3f4dbSSebastian Andrzej Siewior 				      NULL, mvneta_cpu_dead);
462984a3f4dbSSebastian Andrzej Siewior 	if (ret)
463084a3f4dbSSebastian Andrzej Siewior 		goto err_dead;
463184a3f4dbSSebastian Andrzej Siewior 
463284a3f4dbSSebastian Andrzej Siewior 	ret = platform_driver_register(&mvneta_driver);
463384a3f4dbSSebastian Andrzej Siewior 	if (ret)
463484a3f4dbSSebastian Andrzej Siewior 		goto err;
463584a3f4dbSSebastian Andrzej Siewior 	return 0;
463684a3f4dbSSebastian Andrzej Siewior 
463784a3f4dbSSebastian Andrzej Siewior err:
463884a3f4dbSSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
463984a3f4dbSSebastian Andrzej Siewior err_dead:
464084a3f4dbSSebastian Andrzej Siewior 	cpuhp_remove_multi_state(online_hpstate);
464184a3f4dbSSebastian Andrzej Siewior out:
464284a3f4dbSSebastian Andrzej Siewior 	return ret;
464384a3f4dbSSebastian Andrzej Siewior }
464484a3f4dbSSebastian Andrzej Siewior module_init(mvneta_driver_init);
464584a3f4dbSSebastian Andrzej Siewior 
464684a3f4dbSSebastian Andrzej Siewior static void __exit mvneta_driver_exit(void)
464784a3f4dbSSebastian Andrzej Siewior {
464884a3f4dbSSebastian Andrzej Siewior 	platform_driver_unregister(&mvneta_driver);
464984a3f4dbSSebastian Andrzej Siewior 	cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
465084a3f4dbSSebastian Andrzej Siewior 	cpuhp_remove_multi_state(online_hpstate);
465184a3f4dbSSebastian Andrzej Siewior }
465284a3f4dbSSebastian Andrzej Siewior module_exit(mvneta_driver_exit);
4653c5aff182SThomas Petazzoni 
4654c5aff182SThomas Petazzoni MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4655c5aff182SThomas Petazzoni MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4656c5aff182SThomas Petazzoni MODULE_LICENSE("GPL");
4657c5aff182SThomas Petazzoni 
4658d3757ba4SJoe Perches module_param(rxq_number, int, 0444);
4659d3757ba4SJoe Perches module_param(txq_number, int, 0444);
4660c5aff182SThomas Petazzoni 
4661d3757ba4SJoe Perches module_param(rxq_def, int, 0444);
4662d3757ba4SJoe Perches module_param(rx_copybreak, int, 0644);
4663