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 
14c5aff182SThomas Petazzoni #include <linux/kernel.h>
15c5aff182SThomas Petazzoni #include <linux/netdevice.h>
16c5aff182SThomas Petazzoni #include <linux/etherdevice.h>
17c5aff182SThomas Petazzoni #include <linux/platform_device.h>
18c5aff182SThomas Petazzoni #include <linux/skbuff.h>
19c5aff182SThomas Petazzoni #include <linux/inetdevice.h>
20c5aff182SThomas Petazzoni #include <linux/mbus.h>
21c5aff182SThomas Petazzoni #include <linux/module.h>
22c5aff182SThomas Petazzoni #include <linux/interrupt.h>
232d39d120SDavid S. Miller #include <linux/if_vlan.h>
24c5aff182SThomas Petazzoni #include <net/ip.h>
25c5aff182SThomas Petazzoni #include <net/ipv6.h>
26c3f0dd38SThomas Petazzoni #include <linux/io.h>
272adb719dSEzequiel Garcia #include <net/tso.h>
28c5aff182SThomas Petazzoni #include <linux/of.h>
29c5aff182SThomas Petazzoni #include <linux/of_irq.h>
30c5aff182SThomas Petazzoni #include <linux/of_mdio.h>
31c5aff182SThomas Petazzoni #include <linux/of_net.h>
32c5aff182SThomas Petazzoni #include <linux/of_address.h>
33c5aff182SThomas Petazzoni #include <linux/phy.h>
34189dd626SThomas Petazzoni #include <linux/clk.h>
35f8642885SMaxime Ripard #include <linux/cpu.h>
36c5aff182SThomas Petazzoni 
37c5aff182SThomas Petazzoni /* Registers */
38c5aff182SThomas Petazzoni #define MVNETA_RXQ_CONFIG_REG(q)                (0x1400 + ((q) << 2))
39c5aff182SThomas Petazzoni #define      MVNETA_RXQ_HW_BUF_ALLOC            BIT(1)
40c5aff182SThomas Petazzoni #define      MVNETA_RXQ_PKT_OFFSET_ALL_MASK     (0xf    << 8)
41c5aff182SThomas Petazzoni #define      MVNETA_RXQ_PKT_OFFSET_MASK(offs)   ((offs) << 8)
42c5aff182SThomas Petazzoni #define MVNETA_RXQ_THRESHOLD_REG(q)             (0x14c0 + ((q) << 2))
43c5aff182SThomas Petazzoni #define      MVNETA_RXQ_NON_OCCUPIED(v)         ((v) << 16)
44c5aff182SThomas Petazzoni #define MVNETA_RXQ_BASE_ADDR_REG(q)             (0x1480 + ((q) << 2))
45c5aff182SThomas Petazzoni #define MVNETA_RXQ_SIZE_REG(q)                  (0x14a0 + ((q) << 2))
46c5aff182SThomas Petazzoni #define      MVNETA_RXQ_BUF_SIZE_SHIFT          19
47c5aff182SThomas Petazzoni #define      MVNETA_RXQ_BUF_SIZE_MASK           (0x1fff << 19)
48c5aff182SThomas Petazzoni #define MVNETA_RXQ_STATUS_REG(q)                (0x14e0 + ((q) << 2))
49c5aff182SThomas Petazzoni #define      MVNETA_RXQ_OCCUPIED_ALL_MASK       0x3fff
50c5aff182SThomas Petazzoni #define MVNETA_RXQ_STATUS_UPDATE_REG(q)         (0x1500 + ((q) << 2))
51c5aff182SThomas Petazzoni #define      MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT  16
52c5aff182SThomas Petazzoni #define      MVNETA_RXQ_ADD_NON_OCCUPIED_MAX    255
53c5aff182SThomas Petazzoni #define MVNETA_PORT_RX_RESET                    0x1cc0
54c5aff182SThomas Petazzoni #define      MVNETA_PORT_RX_DMA_RESET           BIT(0)
55c5aff182SThomas Petazzoni #define MVNETA_PHY_ADDR                         0x2000
56c5aff182SThomas Petazzoni #define      MVNETA_PHY_ADDR_MASK               0x1f
57c5aff182SThomas Petazzoni #define MVNETA_MBUS_RETRY                       0x2010
58c5aff182SThomas Petazzoni #define MVNETA_UNIT_INTR_CAUSE                  0x2080
59c5aff182SThomas Petazzoni #define MVNETA_UNIT_CONTROL                     0x20B0
60c5aff182SThomas Petazzoni #define      MVNETA_PHY_POLLING_ENABLE          BIT(1)
61c5aff182SThomas Petazzoni #define MVNETA_WIN_BASE(w)                      (0x2200 + ((w) << 3))
62c5aff182SThomas Petazzoni #define MVNETA_WIN_SIZE(w)                      (0x2204 + ((w) << 3))
63c5aff182SThomas Petazzoni #define MVNETA_WIN_REMAP(w)                     (0x2280 + ((w) << 2))
64c5aff182SThomas Petazzoni #define MVNETA_BASE_ADDR_ENABLE                 0x2290
65c5aff182SThomas Petazzoni #define MVNETA_PORT_CONFIG                      0x2400
66c5aff182SThomas Petazzoni #define      MVNETA_UNI_PROMISC_MODE            BIT(0)
67c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ(q)                  ((q) << 1)
68c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ_ARP(q)              ((q) << 4)
69c5aff182SThomas Petazzoni #define      MVNETA_TX_UNSET_ERR_SUM            BIT(12)
70c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ_TCP(q)              ((q) << 16)
71c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ_UDP(q)              ((q) << 19)
72c5aff182SThomas Petazzoni #define      MVNETA_DEF_RXQ_BPDU(q)             ((q) << 22)
73c5aff182SThomas Petazzoni #define      MVNETA_RX_CSUM_WITH_PSEUDO_HDR     BIT(25)
74c5aff182SThomas Petazzoni #define      MVNETA_PORT_CONFIG_DEFL_VALUE(q)   (MVNETA_DEF_RXQ(q)       | \
75c5aff182SThomas Petazzoni 						 MVNETA_DEF_RXQ_ARP(q)	 | \
76c5aff182SThomas Petazzoni 						 MVNETA_DEF_RXQ_TCP(q)	 | \
77c5aff182SThomas Petazzoni 						 MVNETA_DEF_RXQ_UDP(q)	 | \
78c5aff182SThomas Petazzoni 						 MVNETA_DEF_RXQ_BPDU(q)	 | \
79c5aff182SThomas Petazzoni 						 MVNETA_TX_UNSET_ERR_SUM | \
80c5aff182SThomas Petazzoni 						 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
81c5aff182SThomas Petazzoni #define MVNETA_PORT_CONFIG_EXTEND                0x2404
82c5aff182SThomas Petazzoni #define MVNETA_MAC_ADDR_LOW                      0x2414
83c5aff182SThomas Petazzoni #define MVNETA_MAC_ADDR_HIGH                     0x2418
84c5aff182SThomas Petazzoni #define MVNETA_SDMA_CONFIG                       0x241c
85c5aff182SThomas Petazzoni #define      MVNETA_SDMA_BRST_SIZE_16            4
86c5aff182SThomas Petazzoni #define      MVNETA_RX_BRST_SZ_MASK(burst)       ((burst) << 1)
87c5aff182SThomas Petazzoni #define      MVNETA_RX_NO_DATA_SWAP              BIT(4)
88c5aff182SThomas Petazzoni #define      MVNETA_TX_NO_DATA_SWAP              BIT(5)
899ad8fef6SThomas Petazzoni #define      MVNETA_DESC_SWAP                    BIT(6)
90c5aff182SThomas Petazzoni #define      MVNETA_TX_BRST_SZ_MASK(burst)       ((burst) << 22)
91c5aff182SThomas Petazzoni #define MVNETA_PORT_STATUS                       0x2444
92c5aff182SThomas Petazzoni #define      MVNETA_TX_IN_PRGRS                  BIT(1)
93c5aff182SThomas Petazzoni #define      MVNETA_TX_FIFO_EMPTY                BIT(8)
94c5aff182SThomas Petazzoni #define MVNETA_RX_MIN_FRAME_SIZE                 0x247c
953f1dd4bcSThomas Petazzoni #define MVNETA_SERDES_CFG			 0x24A0
965445eaf3SArnaud Patard \(Rtp\) #define      MVNETA_SGMII_SERDES_PROTO		 0x0cc7
973f1dd4bcSThomas Petazzoni #define      MVNETA_QSGMII_SERDES_PROTO		 0x0667
98c5aff182SThomas Petazzoni #define MVNETA_TYPE_PRIO                         0x24bc
99c5aff182SThomas Petazzoni #define      MVNETA_FORCE_UNI                    BIT(21)
100c5aff182SThomas Petazzoni #define MVNETA_TXQ_CMD_1                         0x24e4
101c5aff182SThomas Petazzoni #define MVNETA_TXQ_CMD                           0x2448
102c5aff182SThomas Petazzoni #define      MVNETA_TXQ_DISABLE_SHIFT            8
103c5aff182SThomas Petazzoni #define      MVNETA_TXQ_ENABLE_MASK              0x000000ff
104e483911fSAndrew Lunn #define MVNETA_RX_DISCARD_FRAME_COUNT		 0x2484
105e483911fSAndrew Lunn #define MVNETA_OVERRUN_FRAME_COUNT		 0x2488
106898b2970SStas Sergeev #define MVNETA_GMAC_CLOCK_DIVIDER                0x24f4
107898b2970SStas Sergeev #define      MVNETA_GMAC_1MS_CLOCK_ENABLE        BIT(31)
108c5aff182SThomas Petazzoni #define MVNETA_ACC_MODE                          0x2500
109c5aff182SThomas Petazzoni #define MVNETA_CPU_MAP(cpu)                      (0x2540 + ((cpu) << 2))
110c5aff182SThomas Petazzoni #define      MVNETA_CPU_RXQ_ACCESS_ALL_MASK      0x000000ff
111c5aff182SThomas Petazzoni #define      MVNETA_CPU_TXQ_ACCESS_ALL_MASK      0x0000ff00
112c5aff182SThomas Petazzoni #define MVNETA_RXQ_TIME_COAL_REG(q)              (0x2580 + ((q) << 2))
11340ba35e7Swilly tarreau 
11440ba35e7Swilly tarreau /* Exception Interrupt Port/Queue Cause register */
11540ba35e7Swilly tarreau 
116c5aff182SThomas Petazzoni #define MVNETA_INTR_NEW_CAUSE                    0x25a0
117c5aff182SThomas Petazzoni #define MVNETA_INTR_NEW_MASK                     0x25a4
11840ba35e7Swilly tarreau 
11940ba35e7Swilly tarreau /* bits  0..7  = TXQ SENT, one bit per queue.
12040ba35e7Swilly tarreau  * bits  8..15 = RXQ OCCUP, one bit per queue.
12140ba35e7Swilly tarreau  * bits 16..23 = RXQ FREE, one bit per queue.
12240ba35e7Swilly tarreau  * bit  29 = OLD_REG_SUM, see old reg ?
12340ba35e7Swilly tarreau  * bit  30 = TX_ERR_SUM, one bit for 4 ports
12440ba35e7Swilly tarreau  * bit  31 = MISC_SUM,   one bit for 4 ports
12540ba35e7Swilly tarreau  */
12640ba35e7Swilly tarreau #define      MVNETA_TX_INTR_MASK(nr_txqs)        (((1 << nr_txqs) - 1) << 0)
12740ba35e7Swilly tarreau #define      MVNETA_TX_INTR_MASK_ALL             (0xff << 0)
12840ba35e7Swilly tarreau #define      MVNETA_RX_INTR_MASK(nr_rxqs)        (((1 << nr_rxqs) - 1) << 8)
12940ba35e7Swilly tarreau #define      MVNETA_RX_INTR_MASK_ALL             (0xff << 8)
130898b2970SStas Sergeev #define      MVNETA_MISCINTR_INTR_MASK           BIT(31)
13140ba35e7Swilly tarreau 
132c5aff182SThomas Petazzoni #define MVNETA_INTR_OLD_CAUSE                    0x25a8
133c5aff182SThomas Petazzoni #define MVNETA_INTR_OLD_MASK                     0x25ac
13440ba35e7Swilly tarreau 
13540ba35e7Swilly tarreau /* Data Path Port/Queue Cause Register */
136c5aff182SThomas Petazzoni #define MVNETA_INTR_MISC_CAUSE                   0x25b0
137c5aff182SThomas Petazzoni #define MVNETA_INTR_MISC_MASK                    0x25b4
13840ba35e7Swilly tarreau 
13940ba35e7Swilly tarreau #define      MVNETA_CAUSE_PHY_STATUS_CHANGE      BIT(0)
14040ba35e7Swilly tarreau #define      MVNETA_CAUSE_LINK_CHANGE            BIT(1)
14140ba35e7Swilly tarreau #define      MVNETA_CAUSE_PTP                    BIT(4)
14240ba35e7Swilly tarreau 
14340ba35e7Swilly tarreau #define      MVNETA_CAUSE_INTERNAL_ADDR_ERR      BIT(7)
14440ba35e7Swilly tarreau #define      MVNETA_CAUSE_RX_OVERRUN             BIT(8)
14540ba35e7Swilly tarreau #define      MVNETA_CAUSE_RX_CRC_ERROR           BIT(9)
14640ba35e7Swilly tarreau #define      MVNETA_CAUSE_RX_LARGE_PKT           BIT(10)
14740ba35e7Swilly tarreau #define      MVNETA_CAUSE_TX_UNDERUN             BIT(11)
14840ba35e7Swilly tarreau #define      MVNETA_CAUSE_PRBS_ERR               BIT(12)
14940ba35e7Swilly tarreau #define      MVNETA_CAUSE_PSC_SYNC_CHANGE        BIT(13)
15040ba35e7Swilly tarreau #define      MVNETA_CAUSE_SERDES_SYNC_ERR        BIT(14)
15140ba35e7Swilly tarreau 
15240ba35e7Swilly tarreau #define      MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT    16
15340ba35e7Swilly tarreau #define      MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK   (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
15440ba35e7Swilly tarreau #define      MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
15540ba35e7Swilly tarreau 
15640ba35e7Swilly tarreau #define      MVNETA_CAUSE_TXQ_ERROR_SHIFT        24
15740ba35e7Swilly tarreau #define      MVNETA_CAUSE_TXQ_ERROR_ALL_MASK     (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
15840ba35e7Swilly tarreau #define      MVNETA_CAUSE_TXQ_ERROR_MASK(q)      (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
15940ba35e7Swilly tarreau 
160c5aff182SThomas Petazzoni #define MVNETA_INTR_ENABLE                       0x25b8
161c5aff182SThomas Petazzoni #define      MVNETA_TXQ_INTR_ENABLE_ALL_MASK     0x0000ff00
16240ba35e7Swilly tarreau #define      MVNETA_RXQ_INTR_ENABLE_ALL_MASK     0xff000000  // note: neta says it's 0x000000FF
16340ba35e7Swilly tarreau 
164c5aff182SThomas Petazzoni #define MVNETA_RXQ_CMD                           0x2680
165c5aff182SThomas Petazzoni #define      MVNETA_RXQ_DISABLE_SHIFT            8
166c5aff182SThomas Petazzoni #define      MVNETA_RXQ_ENABLE_MASK              0x000000ff
167c5aff182SThomas Petazzoni #define MVETH_TXQ_TOKEN_COUNT_REG(q)             (0x2700 + ((q) << 4))
168c5aff182SThomas Petazzoni #define MVETH_TXQ_TOKEN_CFG_REG(q)               (0x2704 + ((q) << 4))
169c5aff182SThomas Petazzoni #define MVNETA_GMAC_CTRL_0                       0x2c00
170c5aff182SThomas Petazzoni #define      MVNETA_GMAC_MAX_RX_SIZE_SHIFT       2
171c5aff182SThomas Petazzoni #define      MVNETA_GMAC_MAX_RX_SIZE_MASK        0x7ffc
172c5aff182SThomas Petazzoni #define      MVNETA_GMAC0_PORT_ENABLE            BIT(0)
173c5aff182SThomas Petazzoni #define MVNETA_GMAC_CTRL_2                       0x2c08
174898b2970SStas Sergeev #define      MVNETA_GMAC2_INBAND_AN_ENABLE       BIT(0)
175a79121d3SThomas Petazzoni #define      MVNETA_GMAC2_PCS_ENABLE             BIT(3)
176c5aff182SThomas Petazzoni #define      MVNETA_GMAC2_PORT_RGMII             BIT(4)
177c5aff182SThomas Petazzoni #define      MVNETA_GMAC2_PORT_RESET             BIT(6)
178c5aff182SThomas Petazzoni #define MVNETA_GMAC_STATUS                       0x2c10
179c5aff182SThomas Petazzoni #define      MVNETA_GMAC_LINK_UP                 BIT(0)
180c5aff182SThomas Petazzoni #define      MVNETA_GMAC_SPEED_1000              BIT(1)
181c5aff182SThomas Petazzoni #define      MVNETA_GMAC_SPEED_100               BIT(2)
182c5aff182SThomas Petazzoni #define      MVNETA_GMAC_FULL_DUPLEX             BIT(3)
183c5aff182SThomas Petazzoni #define      MVNETA_GMAC_RX_FLOW_CTRL_ENABLE     BIT(4)
184c5aff182SThomas Petazzoni #define      MVNETA_GMAC_TX_FLOW_CTRL_ENABLE     BIT(5)
185c5aff182SThomas Petazzoni #define      MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE     BIT(6)
186c5aff182SThomas Petazzoni #define      MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE     BIT(7)
187c5aff182SThomas Petazzoni #define MVNETA_GMAC_AUTONEG_CONFIG               0x2c0c
188c5aff182SThomas Petazzoni #define      MVNETA_GMAC_FORCE_LINK_DOWN         BIT(0)
189c5aff182SThomas Petazzoni #define      MVNETA_GMAC_FORCE_LINK_PASS         BIT(1)
190898b2970SStas Sergeev #define      MVNETA_GMAC_INBAND_AN_ENABLE        BIT(2)
191c5aff182SThomas Petazzoni #define      MVNETA_GMAC_CONFIG_MII_SPEED        BIT(5)
192c5aff182SThomas Petazzoni #define      MVNETA_GMAC_CONFIG_GMII_SPEED       BIT(6)
19371408602SThomas Petazzoni #define      MVNETA_GMAC_AN_SPEED_EN             BIT(7)
194898b2970SStas Sergeev #define      MVNETA_GMAC_AN_FLOW_CTRL_EN         BIT(11)
195c5aff182SThomas Petazzoni #define      MVNETA_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
19671408602SThomas Petazzoni #define      MVNETA_GMAC_AN_DUPLEX_EN            BIT(13)
197e483911fSAndrew Lunn #define MVNETA_MIB_COUNTERS_BASE                 0x3000
198c5aff182SThomas Petazzoni #define      MVNETA_MIB_LATE_COLLISION           0x7c
199c5aff182SThomas Petazzoni #define MVNETA_DA_FILT_SPEC_MCAST                0x3400
200c5aff182SThomas Petazzoni #define MVNETA_DA_FILT_OTH_MCAST                 0x3500
201c5aff182SThomas Petazzoni #define MVNETA_DA_FILT_UCAST_BASE                0x3600
202c5aff182SThomas Petazzoni #define MVNETA_TXQ_BASE_ADDR_REG(q)              (0x3c00 + ((q) << 2))
203c5aff182SThomas Petazzoni #define MVNETA_TXQ_SIZE_REG(q)                   (0x3c20 + ((q) << 2))
204c5aff182SThomas Petazzoni #define      MVNETA_TXQ_SENT_THRESH_ALL_MASK     0x3fff0000
205c5aff182SThomas Petazzoni #define      MVNETA_TXQ_SENT_THRESH_MASK(coal)   ((coal) << 16)
206c5aff182SThomas Petazzoni #define MVNETA_TXQ_UPDATE_REG(q)                 (0x3c60 + ((q) << 2))
207c5aff182SThomas Petazzoni #define      MVNETA_TXQ_DEC_SENT_SHIFT           16
208c5aff182SThomas Petazzoni #define MVNETA_TXQ_STATUS_REG(q)                 (0x3c40 + ((q) << 2))
209c5aff182SThomas Petazzoni #define      MVNETA_TXQ_SENT_DESC_SHIFT          16
210c5aff182SThomas Petazzoni #define      MVNETA_TXQ_SENT_DESC_MASK           0x3fff0000
211c5aff182SThomas Petazzoni #define MVNETA_PORT_TX_RESET                     0x3cf0
212c5aff182SThomas Petazzoni #define      MVNETA_PORT_TX_DMA_RESET            BIT(0)
213c5aff182SThomas Petazzoni #define MVNETA_TX_MTU                            0x3e0c
214c5aff182SThomas Petazzoni #define MVNETA_TX_TOKEN_SIZE                     0x3e14
215c5aff182SThomas Petazzoni #define      MVNETA_TX_TOKEN_SIZE_MAX            0xffffffff
216c5aff182SThomas Petazzoni #define MVNETA_TXQ_TOKEN_SIZE_REG(q)             (0x3e40 + ((q) << 2))
217c5aff182SThomas Petazzoni #define      MVNETA_TXQ_TOKEN_SIZE_MAX           0x7fffffff
218c5aff182SThomas Petazzoni 
219c5aff182SThomas Petazzoni #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK	 0xff
220c5aff182SThomas Petazzoni 
221c5aff182SThomas Petazzoni /* Descriptor ring Macros */
222c5aff182SThomas Petazzoni #define MVNETA_QUEUE_NEXT_DESC(q, index)	\
223c5aff182SThomas Petazzoni 	(((index) < (q)->last_desc) ? ((index) + 1) : 0)
224c5aff182SThomas Petazzoni 
225c5aff182SThomas Petazzoni /* Various constants */
226c5aff182SThomas Petazzoni 
227c5aff182SThomas Petazzoni /* Coalescing */
228aebea2baSwilly tarreau #define MVNETA_TXDONE_COAL_PKTS		1
229c5aff182SThomas Petazzoni #define MVNETA_RX_COAL_PKTS		32
230c5aff182SThomas Petazzoni #define MVNETA_RX_COAL_USEC		100
231c5aff182SThomas Petazzoni 
2326a20c175SThomas Petazzoni /* The two bytes Marvell header. Either contains a special value used
233c5aff182SThomas Petazzoni  * by Marvell switches when a specific hardware mode is enabled (not
234c5aff182SThomas Petazzoni  * supported by this driver) or is filled automatically by zeroes on
235c5aff182SThomas Petazzoni  * the RX side. Those two bytes being at the front of the Ethernet
236c5aff182SThomas Petazzoni  * header, they allow to have the IP header aligned on a 4 bytes
237c5aff182SThomas Petazzoni  * boundary automatically: the hardware skips those two bytes on its
238c5aff182SThomas Petazzoni  * own.
239c5aff182SThomas Petazzoni  */
240c5aff182SThomas Petazzoni #define MVNETA_MH_SIZE			2
241c5aff182SThomas Petazzoni 
242c5aff182SThomas Petazzoni #define MVNETA_VLAN_TAG_LEN             4
243c5aff182SThomas Petazzoni 
244c5aff182SThomas Petazzoni #define MVNETA_CPU_D_CACHE_LINE_SIZE    32
245c5aff182SThomas Petazzoni #define MVNETA_TX_CSUM_MAX_SIZE		9800
246c5aff182SThomas Petazzoni #define MVNETA_ACC_MODE_EXT		1
247c5aff182SThomas Petazzoni 
248c5aff182SThomas Petazzoni /* Timeout constants */
249c5aff182SThomas Petazzoni #define MVNETA_TX_DISABLE_TIMEOUT_MSEC	1000
250c5aff182SThomas Petazzoni #define MVNETA_RX_DISABLE_TIMEOUT_MSEC	1000
251c5aff182SThomas Petazzoni #define MVNETA_TX_FIFO_EMPTY_TIMEOUT	10000
252c5aff182SThomas Petazzoni 
253c5aff182SThomas Petazzoni #define MVNETA_TX_MTU_MAX		0x3ffff
254c5aff182SThomas Petazzoni 
2552adb719dSEzequiel Garcia /* TSO header size */
2562adb719dSEzequiel Garcia #define TSO_HEADER_SIZE 128
2572adb719dSEzequiel Garcia 
258c5aff182SThomas Petazzoni /* Max number of Rx descriptors */
259c5aff182SThomas Petazzoni #define MVNETA_MAX_RXD 128
260c5aff182SThomas Petazzoni 
261c5aff182SThomas Petazzoni /* Max number of Tx descriptors */
262c5aff182SThomas Petazzoni #define MVNETA_MAX_TXD 532
263c5aff182SThomas Petazzoni 
2648eef5f97SEzequiel Garcia /* Max number of allowed TCP segments for software TSO */
2658eef5f97SEzequiel Garcia #define MVNETA_MAX_TSO_SEGS 100
2668eef5f97SEzequiel Garcia 
2678eef5f97SEzequiel Garcia #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
2688eef5f97SEzequiel Garcia 
269c5aff182SThomas Petazzoni /* descriptor aligned size */
270c5aff182SThomas Petazzoni #define MVNETA_DESC_ALIGNED_SIZE	32
271c5aff182SThomas Petazzoni 
272c5aff182SThomas Petazzoni #define MVNETA_RX_PKT_SIZE(mtu) \
273c5aff182SThomas Petazzoni 	ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
274c5aff182SThomas Petazzoni 	      ETH_HLEN + ETH_FCS_LEN,			     \
275c5aff182SThomas Petazzoni 	      MVNETA_CPU_D_CACHE_LINE_SIZE)
276c5aff182SThomas Petazzoni 
2772e3173a3SEzequiel Garcia #define IS_TSO_HEADER(txq, addr) \
2782e3173a3SEzequiel Garcia 	((addr >= txq->tso_hdrs_phys) && \
2792e3173a3SEzequiel Garcia 	 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
2802e3173a3SEzequiel Garcia 
281c5aff182SThomas Petazzoni #define MVNETA_RX_BUF_SIZE(pkt_size)   ((pkt_size) + NET_SKB_PAD)
282c5aff182SThomas Petazzoni 
2839b0cdefaSRussell King struct mvneta_statistic {
2849b0cdefaSRussell King 	unsigned short offset;
2859b0cdefaSRussell King 	unsigned short type;
2869b0cdefaSRussell King 	const char name[ETH_GSTRING_LEN];
2879b0cdefaSRussell King };
2889b0cdefaSRussell King 
2899b0cdefaSRussell King #define T_REG_32	32
2909b0cdefaSRussell King #define T_REG_64	64
2919b0cdefaSRussell King 
2929b0cdefaSRussell King static const struct mvneta_statistic mvneta_statistics[] = {
2939b0cdefaSRussell King 	{ 0x3000, T_REG_64, "good_octets_received", },
2949b0cdefaSRussell King 	{ 0x3010, T_REG_32, "good_frames_received", },
2959b0cdefaSRussell King 	{ 0x3008, T_REG_32, "bad_octets_received", },
2969b0cdefaSRussell King 	{ 0x3014, T_REG_32, "bad_frames_received", },
2979b0cdefaSRussell King 	{ 0x3018, T_REG_32, "broadcast_frames_received", },
2989b0cdefaSRussell King 	{ 0x301c, T_REG_32, "multicast_frames_received", },
2999b0cdefaSRussell King 	{ 0x3050, T_REG_32, "unrec_mac_control_received", },
3009b0cdefaSRussell King 	{ 0x3058, T_REG_32, "good_fc_received", },
3019b0cdefaSRussell King 	{ 0x305c, T_REG_32, "bad_fc_received", },
3029b0cdefaSRussell King 	{ 0x3060, T_REG_32, "undersize_received", },
3039b0cdefaSRussell King 	{ 0x3064, T_REG_32, "fragments_received", },
3049b0cdefaSRussell King 	{ 0x3068, T_REG_32, "oversize_received", },
3059b0cdefaSRussell King 	{ 0x306c, T_REG_32, "jabber_received", },
3069b0cdefaSRussell King 	{ 0x3070, T_REG_32, "mac_receive_error", },
3079b0cdefaSRussell King 	{ 0x3074, T_REG_32, "bad_crc_event", },
3089b0cdefaSRussell King 	{ 0x3078, T_REG_32, "collision", },
3099b0cdefaSRussell King 	{ 0x307c, T_REG_32, "late_collision", },
3109b0cdefaSRussell King 	{ 0x2484, T_REG_32, "rx_discard", },
3119b0cdefaSRussell King 	{ 0x2488, T_REG_32, "rx_overrun", },
3129b0cdefaSRussell King 	{ 0x3020, T_REG_32, "frames_64_octets", },
3139b0cdefaSRussell King 	{ 0x3024, T_REG_32, "frames_65_to_127_octets", },
3149b0cdefaSRussell King 	{ 0x3028, T_REG_32, "frames_128_to_255_octets", },
3159b0cdefaSRussell King 	{ 0x302c, T_REG_32, "frames_256_to_511_octets", },
3169b0cdefaSRussell King 	{ 0x3030, T_REG_32, "frames_512_to_1023_octets", },
3179b0cdefaSRussell King 	{ 0x3034, T_REG_32, "frames_1024_to_max_octets", },
3189b0cdefaSRussell King 	{ 0x3038, T_REG_64, "good_octets_sent", },
3199b0cdefaSRussell King 	{ 0x3040, T_REG_32, "good_frames_sent", },
3209b0cdefaSRussell King 	{ 0x3044, T_REG_32, "excessive_collision", },
3219b0cdefaSRussell King 	{ 0x3048, T_REG_32, "multicast_frames_sent", },
3229b0cdefaSRussell King 	{ 0x304c, T_REG_32, "broadcast_frames_sent", },
3239b0cdefaSRussell King 	{ 0x3054, T_REG_32, "fc_sent", },
3249b0cdefaSRussell King 	{ 0x300c, T_REG_32, "internal_mac_transmit_err", },
3259b0cdefaSRussell King };
3269b0cdefaSRussell King 
32774c41b04Swilly tarreau struct mvneta_pcpu_stats {
328c5aff182SThomas Petazzoni 	struct	u64_stats_sync syncp;
32974c41b04Swilly tarreau 	u64	rx_packets;
33074c41b04Swilly tarreau 	u64	rx_bytes;
33174c41b04Swilly tarreau 	u64	tx_packets;
33274c41b04Swilly tarreau 	u64	tx_bytes;
333c5aff182SThomas Petazzoni };
334c5aff182SThomas Petazzoni 
33512bb03b4SMaxime Ripard struct mvneta_pcpu_port {
33612bb03b4SMaxime Ripard 	/* Pointer to the shared port */
33712bb03b4SMaxime Ripard 	struct mvneta_port	*pp;
33812bb03b4SMaxime Ripard 
33912bb03b4SMaxime Ripard 	/* Pointer to the CPU-local NAPI struct */
34012bb03b4SMaxime Ripard 	struct napi_struct	napi;
34112bb03b4SMaxime Ripard 
34212bb03b4SMaxime Ripard 	/* Cause of the previous interrupt */
34312bb03b4SMaxime Ripard 	u32			cause_rx_tx;
34412bb03b4SMaxime Ripard };
34512bb03b4SMaxime Ripard 
346c5aff182SThomas Petazzoni struct mvneta_port {
34712bb03b4SMaxime Ripard 	struct mvneta_pcpu_port __percpu	*ports;
34812bb03b4SMaxime Ripard 	struct mvneta_pcpu_stats __percpu	*stats;
34912bb03b4SMaxime Ripard 
350c5aff182SThomas Petazzoni 	int pkt_size;
3518ec2cd48Swilly tarreau 	unsigned int frag_size;
352c5aff182SThomas Petazzoni 	void __iomem *base;
353c5aff182SThomas Petazzoni 	struct mvneta_rx_queue *rxqs;
354c5aff182SThomas Petazzoni 	struct mvneta_tx_queue *txqs;
355c5aff182SThomas Petazzoni 	struct net_device *dev;
356f8642885SMaxime Ripard 	struct notifier_block cpu_notifier;
357c5aff182SThomas Petazzoni 
358c5aff182SThomas Petazzoni 	/* Core clock */
359189dd626SThomas Petazzoni 	struct clk *clk;
360c5aff182SThomas Petazzoni 	u8 mcast_count[256];
361c5aff182SThomas Petazzoni 	u16 tx_ring_size;
362c5aff182SThomas Petazzoni 	u16 rx_ring_size;
363c5aff182SThomas Petazzoni 
364c5aff182SThomas Petazzoni 	struct mii_bus *mii_bus;
365c5aff182SThomas Petazzoni 	struct phy_device *phy_dev;
366c5aff182SThomas Petazzoni 	phy_interface_t phy_interface;
367c5aff182SThomas Petazzoni 	struct device_node *phy_node;
368c5aff182SThomas Petazzoni 	unsigned int link;
369c5aff182SThomas Petazzoni 	unsigned int duplex;
370c5aff182SThomas Petazzoni 	unsigned int speed;
371b65657fcSSimon Guinot 	unsigned int tx_csum_limit;
372898b2970SStas Sergeev 	int use_inband_status:1;
3739b0cdefaSRussell King 
3749b0cdefaSRussell King 	u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
375c5aff182SThomas Petazzoni };
376c5aff182SThomas Petazzoni 
3776a20c175SThomas Petazzoni /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
378c5aff182SThomas Petazzoni  * layout of the transmit and reception DMA descriptors, and their
379c5aff182SThomas Petazzoni  * layout is therefore defined by the hardware design
380c5aff182SThomas Petazzoni  */
3816083ed44SThomas Petazzoni 
382c5aff182SThomas Petazzoni #define MVNETA_TX_L3_OFF_SHIFT	0
383c5aff182SThomas Petazzoni #define MVNETA_TX_IP_HLEN_SHIFT	8
384c5aff182SThomas Petazzoni #define MVNETA_TX_L4_UDP	BIT(16)
385c5aff182SThomas Petazzoni #define MVNETA_TX_L3_IP6	BIT(17)
386c5aff182SThomas Petazzoni #define MVNETA_TXD_IP_CSUM	BIT(18)
387c5aff182SThomas Petazzoni #define MVNETA_TXD_Z_PAD	BIT(19)
388c5aff182SThomas Petazzoni #define MVNETA_TXD_L_DESC	BIT(20)
389c5aff182SThomas Petazzoni #define MVNETA_TXD_F_DESC	BIT(21)
390c5aff182SThomas Petazzoni #define MVNETA_TXD_FLZ_DESC	(MVNETA_TXD_Z_PAD  | \
391c5aff182SThomas Petazzoni 				 MVNETA_TXD_L_DESC | \
392c5aff182SThomas Petazzoni 				 MVNETA_TXD_F_DESC)
393c5aff182SThomas Petazzoni #define MVNETA_TX_L4_CSUM_FULL	BIT(30)
394c5aff182SThomas Petazzoni #define MVNETA_TX_L4_CSUM_NOT	BIT(31)
395c5aff182SThomas Petazzoni 
396c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_CRC		0x0
397c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_SUMMARY		BIT(16)
398c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_OVERRUN		BIT(17)
399c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_LEN		BIT(18)
400c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_RESOURCE		(BIT(17) | BIT(18))
401c5aff182SThomas Petazzoni #define MVNETA_RXD_ERR_CODE_MASK	(BIT(17) | BIT(18))
402c5aff182SThomas Petazzoni #define MVNETA_RXD_L3_IP4		BIT(25)
403c5aff182SThomas Petazzoni #define MVNETA_RXD_FIRST_LAST_DESC	(BIT(26) | BIT(27))
404c5aff182SThomas Petazzoni #define MVNETA_RXD_L4_CSUM_OK		BIT(30)
405c5aff182SThomas Petazzoni 
4069ad8fef6SThomas Petazzoni #if defined(__LITTLE_ENDIAN)
4076083ed44SThomas Petazzoni struct mvneta_tx_desc {
4086083ed44SThomas Petazzoni 	u32  command;		/* Options used by HW for packet transmitting.*/
4096083ed44SThomas Petazzoni 	u16  reserverd1;	/* csum_l4 (for future use)		*/
4106083ed44SThomas Petazzoni 	u16  data_size;		/* Data size of transmitted packet in bytes */
4116083ed44SThomas Petazzoni 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
4126083ed44SThomas Petazzoni 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
4136083ed44SThomas Petazzoni 	u32  reserved3[4];	/* Reserved - (for future use)		*/
4146083ed44SThomas Petazzoni };
4156083ed44SThomas Petazzoni 
4166083ed44SThomas Petazzoni struct mvneta_rx_desc {
4176083ed44SThomas Petazzoni 	u32  status;		/* Info about received packet		*/
418c5aff182SThomas Petazzoni 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
419c5aff182SThomas Petazzoni 	u16  data_size;		/* Size of received packet in bytes	*/
4206083ed44SThomas Petazzoni 
421c5aff182SThomas Petazzoni 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
422c5aff182SThomas Petazzoni 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
4236083ed44SThomas Petazzoni 
424c5aff182SThomas Petazzoni 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
425c5aff182SThomas Petazzoni 	u16  reserved3;		/* prefetch_cmd, for future use		*/
426c5aff182SThomas Petazzoni 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
4276083ed44SThomas Petazzoni 
428c5aff182SThomas Petazzoni 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
429c5aff182SThomas Petazzoni 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
430c5aff182SThomas Petazzoni };
4319ad8fef6SThomas Petazzoni #else
4329ad8fef6SThomas Petazzoni struct mvneta_tx_desc {
4339ad8fef6SThomas Petazzoni 	u16  data_size;		/* Data size of transmitted packet in bytes */
4349ad8fef6SThomas Petazzoni 	u16  reserverd1;	/* csum_l4 (for future use)		*/
4359ad8fef6SThomas Petazzoni 	u32  command;		/* Options used by HW for packet transmitting.*/
4369ad8fef6SThomas Petazzoni 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
4379ad8fef6SThomas Petazzoni 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
4389ad8fef6SThomas Petazzoni 	u32  reserved3[4];	/* Reserved - (for future use)		*/
4399ad8fef6SThomas Petazzoni };
4409ad8fef6SThomas Petazzoni 
4419ad8fef6SThomas Petazzoni struct mvneta_rx_desc {
4429ad8fef6SThomas Petazzoni 	u16  data_size;		/* Size of received packet in bytes	*/
4439ad8fef6SThomas Petazzoni 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
4449ad8fef6SThomas Petazzoni 	u32  status;		/* Info about received packet		*/
4459ad8fef6SThomas Petazzoni 
4469ad8fef6SThomas Petazzoni 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
4479ad8fef6SThomas Petazzoni 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
4489ad8fef6SThomas Petazzoni 
4499ad8fef6SThomas Petazzoni 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
4509ad8fef6SThomas Petazzoni 	u16  reserved3;		/* prefetch_cmd, for future use		*/
4519ad8fef6SThomas Petazzoni 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
4529ad8fef6SThomas Petazzoni 
4539ad8fef6SThomas Petazzoni 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
4549ad8fef6SThomas Petazzoni 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
4559ad8fef6SThomas Petazzoni };
4569ad8fef6SThomas Petazzoni #endif
457c5aff182SThomas Petazzoni 
458c5aff182SThomas Petazzoni struct mvneta_tx_queue {
459c5aff182SThomas Petazzoni 	/* Number of this TX queue, in the range 0-7 */
460c5aff182SThomas Petazzoni 	u8 id;
461c5aff182SThomas Petazzoni 
462c5aff182SThomas Petazzoni 	/* Number of TX DMA descriptors in the descriptor ring */
463c5aff182SThomas Petazzoni 	int size;
464c5aff182SThomas Petazzoni 
465c5aff182SThomas Petazzoni 	/* Number of currently used TX DMA descriptor in the
4666a20c175SThomas Petazzoni 	 * descriptor ring
4676a20c175SThomas Petazzoni 	 */
468c5aff182SThomas Petazzoni 	int count;
4698eef5f97SEzequiel Garcia 	int tx_stop_threshold;
4708eef5f97SEzequiel Garcia 	int tx_wake_threshold;
471c5aff182SThomas Petazzoni 
472c5aff182SThomas Petazzoni 	/* Array of transmitted skb */
473c5aff182SThomas Petazzoni 	struct sk_buff **tx_skb;
474c5aff182SThomas Petazzoni 
475c5aff182SThomas Petazzoni 	/* Index of last TX DMA descriptor that was inserted */
476c5aff182SThomas Petazzoni 	int txq_put_index;
477c5aff182SThomas Petazzoni 
478c5aff182SThomas Petazzoni 	/* Index of the TX DMA descriptor to be cleaned up */
479c5aff182SThomas Petazzoni 	int txq_get_index;
480c5aff182SThomas Petazzoni 
481c5aff182SThomas Petazzoni 	u32 done_pkts_coal;
482c5aff182SThomas Petazzoni 
483c5aff182SThomas Petazzoni 	/* Virtual address of the TX DMA descriptors array */
484c5aff182SThomas Petazzoni 	struct mvneta_tx_desc *descs;
485c5aff182SThomas Petazzoni 
486c5aff182SThomas Petazzoni 	/* DMA address of the TX DMA descriptors array */
487c5aff182SThomas Petazzoni 	dma_addr_t descs_phys;
488c5aff182SThomas Petazzoni 
489c5aff182SThomas Petazzoni 	/* Index of the last TX DMA descriptor */
490c5aff182SThomas Petazzoni 	int last_desc;
491c5aff182SThomas Petazzoni 
492c5aff182SThomas Petazzoni 	/* Index of the next TX DMA descriptor to process */
493c5aff182SThomas Petazzoni 	int next_desc_to_proc;
4942adb719dSEzequiel Garcia 
4952adb719dSEzequiel Garcia 	/* DMA buffers for TSO headers */
4962adb719dSEzequiel Garcia 	char *tso_hdrs;
4972adb719dSEzequiel Garcia 
4982adb719dSEzequiel Garcia 	/* DMA address of TSO headers */
4992adb719dSEzequiel Garcia 	dma_addr_t tso_hdrs_phys;
500c5aff182SThomas Petazzoni };
501c5aff182SThomas Petazzoni 
502c5aff182SThomas Petazzoni struct mvneta_rx_queue {
503c5aff182SThomas Petazzoni 	/* rx queue number, in the range 0-7 */
504c5aff182SThomas Petazzoni 	u8 id;
505c5aff182SThomas Petazzoni 
506c5aff182SThomas Petazzoni 	/* num of rx descriptors in the rx descriptor ring */
507c5aff182SThomas Petazzoni 	int size;
508c5aff182SThomas Petazzoni 
509c5aff182SThomas Petazzoni 	/* counter of times when mvneta_refill() failed */
510c5aff182SThomas Petazzoni 	int missed;
511c5aff182SThomas Petazzoni 
512c5aff182SThomas Petazzoni 	u32 pkts_coal;
513c5aff182SThomas Petazzoni 	u32 time_coal;
514c5aff182SThomas Petazzoni 
515c5aff182SThomas Petazzoni 	/* Virtual address of the RX DMA descriptors array */
516c5aff182SThomas Petazzoni 	struct mvneta_rx_desc *descs;
517c5aff182SThomas Petazzoni 
518c5aff182SThomas Petazzoni 	/* DMA address of the RX DMA descriptors array */
519c5aff182SThomas Petazzoni 	dma_addr_t descs_phys;
520c5aff182SThomas Petazzoni 
521c5aff182SThomas Petazzoni 	/* Index of the last RX DMA descriptor */
522c5aff182SThomas Petazzoni 	int last_desc;
523c5aff182SThomas Petazzoni 
524c5aff182SThomas Petazzoni 	/* Index of the next RX DMA descriptor to process */
525c5aff182SThomas Petazzoni 	int next_desc_to_proc;
526c5aff182SThomas Petazzoni };
527c5aff182SThomas Petazzoni 
528edadb7faSEzequiel Garcia /* The hardware supports eight (8) rx queues, but we are only allowing
529edadb7faSEzequiel Garcia  * the first one to be used. Therefore, let's just allocate one queue.
530edadb7faSEzequiel Garcia  */
531d8936657SMaxime Ripard static int rxq_number = 8;
532c5aff182SThomas Petazzoni static int txq_number = 8;
533c5aff182SThomas Petazzoni 
534c5aff182SThomas Petazzoni static int rxq_def;
535c5aff182SThomas Petazzoni 
536f19fadfcSwilly tarreau static int rx_copybreak __read_mostly = 256;
537f19fadfcSwilly tarreau 
538c5aff182SThomas Petazzoni #define MVNETA_DRIVER_NAME "mvneta"
539c5aff182SThomas Petazzoni #define MVNETA_DRIVER_VERSION "1.0"
540c5aff182SThomas Petazzoni 
541c5aff182SThomas Petazzoni /* Utility/helper methods */
542c5aff182SThomas Petazzoni 
543c5aff182SThomas Petazzoni /* Write helper method */
544c5aff182SThomas Petazzoni static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
545c5aff182SThomas Petazzoni {
546c5aff182SThomas Petazzoni 	writel(data, pp->base + offset);
547c5aff182SThomas Petazzoni }
548c5aff182SThomas Petazzoni 
549c5aff182SThomas Petazzoni /* Read helper method */
550c5aff182SThomas Petazzoni static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
551c5aff182SThomas Petazzoni {
552c5aff182SThomas Petazzoni 	return readl(pp->base + offset);
553c5aff182SThomas Petazzoni }
554c5aff182SThomas Petazzoni 
555c5aff182SThomas Petazzoni /* Increment txq get counter */
556c5aff182SThomas Petazzoni static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
557c5aff182SThomas Petazzoni {
558c5aff182SThomas Petazzoni 	txq->txq_get_index++;
559c5aff182SThomas Petazzoni 	if (txq->txq_get_index == txq->size)
560c5aff182SThomas Petazzoni 		txq->txq_get_index = 0;
561c5aff182SThomas Petazzoni }
562c5aff182SThomas Petazzoni 
563c5aff182SThomas Petazzoni /* Increment txq put counter */
564c5aff182SThomas Petazzoni static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
565c5aff182SThomas Petazzoni {
566c5aff182SThomas Petazzoni 	txq->txq_put_index++;
567c5aff182SThomas Petazzoni 	if (txq->txq_put_index == txq->size)
568c5aff182SThomas Petazzoni 		txq->txq_put_index = 0;
569c5aff182SThomas Petazzoni }
570c5aff182SThomas Petazzoni 
571c5aff182SThomas Petazzoni 
572c5aff182SThomas Petazzoni /* Clear all MIB counters */
573c5aff182SThomas Petazzoni static void mvneta_mib_counters_clear(struct mvneta_port *pp)
574c5aff182SThomas Petazzoni {
575c5aff182SThomas Petazzoni 	int i;
576c5aff182SThomas Petazzoni 	u32 dummy;
577c5aff182SThomas Petazzoni 
578c5aff182SThomas Petazzoni 	/* Perform dummy reads from MIB counters */
579c5aff182SThomas Petazzoni 	for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
580c5aff182SThomas Petazzoni 		dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
581e483911fSAndrew Lunn 	dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
582e483911fSAndrew Lunn 	dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
583c5aff182SThomas Petazzoni }
584c5aff182SThomas Petazzoni 
585c5aff182SThomas Petazzoni /* Get System Network Statistics */
586c5aff182SThomas Petazzoni struct rtnl_link_stats64 *mvneta_get_stats64(struct net_device *dev,
587c5aff182SThomas Petazzoni 					     struct rtnl_link_stats64 *stats)
588c5aff182SThomas Petazzoni {
589c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
590c5aff182SThomas Petazzoni 	unsigned int start;
59174c41b04Swilly tarreau 	int cpu;
592c5aff182SThomas Petazzoni 
59374c41b04Swilly tarreau 	for_each_possible_cpu(cpu) {
59474c41b04Swilly tarreau 		struct mvneta_pcpu_stats *cpu_stats;
59574c41b04Swilly tarreau 		u64 rx_packets;
59674c41b04Swilly tarreau 		u64 rx_bytes;
59774c41b04Swilly tarreau 		u64 tx_packets;
59874c41b04Swilly tarreau 		u64 tx_bytes;
599c5aff182SThomas Petazzoni 
60074c41b04Swilly tarreau 		cpu_stats = per_cpu_ptr(pp->stats, cpu);
601c5aff182SThomas Petazzoni 		do {
60257a7744eSEric W. Biederman 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
60374c41b04Swilly tarreau 			rx_packets = cpu_stats->rx_packets;
60474c41b04Swilly tarreau 			rx_bytes   = cpu_stats->rx_bytes;
60574c41b04Swilly tarreau 			tx_packets = cpu_stats->tx_packets;
60674c41b04Swilly tarreau 			tx_bytes   = cpu_stats->tx_bytes;
60757a7744eSEric W. Biederman 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
608c5aff182SThomas Petazzoni 
60974c41b04Swilly tarreau 		stats->rx_packets += rx_packets;
61074c41b04Swilly tarreau 		stats->rx_bytes   += rx_bytes;
61174c41b04Swilly tarreau 		stats->tx_packets += tx_packets;
61274c41b04Swilly tarreau 		stats->tx_bytes   += tx_bytes;
61374c41b04Swilly tarreau 	}
614c5aff182SThomas Petazzoni 
615c5aff182SThomas Petazzoni 	stats->rx_errors	= dev->stats.rx_errors;
616c5aff182SThomas Petazzoni 	stats->rx_dropped	= dev->stats.rx_dropped;
617c5aff182SThomas Petazzoni 
618c5aff182SThomas Petazzoni 	stats->tx_dropped	= dev->stats.tx_dropped;
619c5aff182SThomas Petazzoni 
620c5aff182SThomas Petazzoni 	return stats;
621c5aff182SThomas Petazzoni }
622c5aff182SThomas Petazzoni 
623c5aff182SThomas Petazzoni /* Rx descriptors helper methods */
624c5aff182SThomas Petazzoni 
6255428213cSwilly tarreau /* Checks whether the RX descriptor having this status is both the first
6265428213cSwilly tarreau  * and the last descriptor for the RX packet. Each RX packet is currently
627c5aff182SThomas Petazzoni  * received through a single RX descriptor, so not having each RX
628c5aff182SThomas Petazzoni  * descriptor with its first and last bits set is an error
629c5aff182SThomas Petazzoni  */
6305428213cSwilly tarreau static int mvneta_rxq_desc_is_first_last(u32 status)
631c5aff182SThomas Petazzoni {
6325428213cSwilly tarreau 	return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
633c5aff182SThomas Petazzoni 		MVNETA_RXD_FIRST_LAST_DESC;
634c5aff182SThomas Petazzoni }
635c5aff182SThomas Petazzoni 
636c5aff182SThomas Petazzoni /* Add number of descriptors ready to receive new packets */
637c5aff182SThomas Petazzoni static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
638c5aff182SThomas Petazzoni 					  struct mvneta_rx_queue *rxq,
639c5aff182SThomas Petazzoni 					  int ndescs)
640c5aff182SThomas Petazzoni {
641c5aff182SThomas Petazzoni 	/* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
6426a20c175SThomas Petazzoni 	 * be added at once
6436a20c175SThomas Petazzoni 	 */
644c5aff182SThomas Petazzoni 	while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
645c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
646c5aff182SThomas Petazzoni 			    (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
647c5aff182SThomas Petazzoni 			     MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
648c5aff182SThomas Petazzoni 		ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
649c5aff182SThomas Petazzoni 	}
650c5aff182SThomas Petazzoni 
651c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
652c5aff182SThomas Petazzoni 		    (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
653c5aff182SThomas Petazzoni }
654c5aff182SThomas Petazzoni 
655c5aff182SThomas Petazzoni /* Get number of RX descriptors occupied by received packets */
656c5aff182SThomas Petazzoni static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
657c5aff182SThomas Petazzoni 					struct mvneta_rx_queue *rxq)
658c5aff182SThomas Petazzoni {
659c5aff182SThomas Petazzoni 	u32 val;
660c5aff182SThomas Petazzoni 
661c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
662c5aff182SThomas Petazzoni 	return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
663c5aff182SThomas Petazzoni }
664c5aff182SThomas Petazzoni 
6656a20c175SThomas Petazzoni /* Update num of rx desc called upon return from rx path or
666c5aff182SThomas Petazzoni  * from mvneta_rxq_drop_pkts().
667c5aff182SThomas Petazzoni  */
668c5aff182SThomas Petazzoni static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
669c5aff182SThomas Petazzoni 				       struct mvneta_rx_queue *rxq,
670c5aff182SThomas Petazzoni 				       int rx_done, int rx_filled)
671c5aff182SThomas Petazzoni {
672c5aff182SThomas Petazzoni 	u32 val;
673c5aff182SThomas Petazzoni 
674c5aff182SThomas Petazzoni 	if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
675c5aff182SThomas Petazzoni 		val = rx_done |
676c5aff182SThomas Petazzoni 		  (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
677c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
678c5aff182SThomas Petazzoni 		return;
679c5aff182SThomas Petazzoni 	}
680c5aff182SThomas Petazzoni 
681c5aff182SThomas Petazzoni 	/* Only 255 descriptors can be added at once */
682c5aff182SThomas Petazzoni 	while ((rx_done > 0) || (rx_filled > 0)) {
683c5aff182SThomas Petazzoni 		if (rx_done <= 0xff) {
684c5aff182SThomas Petazzoni 			val = rx_done;
685c5aff182SThomas Petazzoni 			rx_done = 0;
686c5aff182SThomas Petazzoni 		} else {
687c5aff182SThomas Petazzoni 			val = 0xff;
688c5aff182SThomas Petazzoni 			rx_done -= 0xff;
689c5aff182SThomas Petazzoni 		}
690c5aff182SThomas Petazzoni 		if (rx_filled <= 0xff) {
691c5aff182SThomas Petazzoni 			val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
692c5aff182SThomas Petazzoni 			rx_filled = 0;
693c5aff182SThomas Petazzoni 		} else {
694c5aff182SThomas Petazzoni 			val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
695c5aff182SThomas Petazzoni 			rx_filled -= 0xff;
696c5aff182SThomas Petazzoni 		}
697c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
698c5aff182SThomas Petazzoni 	}
699c5aff182SThomas Petazzoni }
700c5aff182SThomas Petazzoni 
701c5aff182SThomas Petazzoni /* Get pointer to next RX descriptor to be processed by SW */
702c5aff182SThomas Petazzoni static struct mvneta_rx_desc *
703c5aff182SThomas Petazzoni mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
704c5aff182SThomas Petazzoni {
705c5aff182SThomas Petazzoni 	int rx_desc = rxq->next_desc_to_proc;
706c5aff182SThomas Petazzoni 
707c5aff182SThomas Petazzoni 	rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
70834e4179dSwilly tarreau 	prefetch(rxq->descs + rxq->next_desc_to_proc);
709c5aff182SThomas Petazzoni 	return rxq->descs + rx_desc;
710c5aff182SThomas Petazzoni }
711c5aff182SThomas Petazzoni 
712c5aff182SThomas Petazzoni /* Change maximum receive size of the port. */
713c5aff182SThomas Petazzoni static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
714c5aff182SThomas Petazzoni {
715c5aff182SThomas Petazzoni 	u32 val;
716c5aff182SThomas Petazzoni 
717c5aff182SThomas Petazzoni 	val =  mvreg_read(pp, MVNETA_GMAC_CTRL_0);
718c5aff182SThomas Petazzoni 	val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
719c5aff182SThomas Petazzoni 	val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
720c5aff182SThomas Petazzoni 		MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
721c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
722c5aff182SThomas Petazzoni }
723c5aff182SThomas Petazzoni 
724c5aff182SThomas Petazzoni 
725c5aff182SThomas Petazzoni /* Set rx queue offset */
726c5aff182SThomas Petazzoni static void mvneta_rxq_offset_set(struct mvneta_port *pp,
727c5aff182SThomas Petazzoni 				  struct mvneta_rx_queue *rxq,
728c5aff182SThomas Petazzoni 				  int offset)
729c5aff182SThomas Petazzoni {
730c5aff182SThomas Petazzoni 	u32 val;
731c5aff182SThomas Petazzoni 
732c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
733c5aff182SThomas Petazzoni 	val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
734c5aff182SThomas Petazzoni 
735c5aff182SThomas Petazzoni 	/* Offset is in */
736c5aff182SThomas Petazzoni 	val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
737c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
738c5aff182SThomas Petazzoni }
739c5aff182SThomas Petazzoni 
740c5aff182SThomas Petazzoni 
741c5aff182SThomas Petazzoni /* Tx descriptors helper methods */
742c5aff182SThomas Petazzoni 
743c5aff182SThomas Petazzoni /* Update HW with number of TX descriptors to be sent */
744c5aff182SThomas Petazzoni static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
745c5aff182SThomas Petazzoni 				     struct mvneta_tx_queue *txq,
746c5aff182SThomas Petazzoni 				     int pend_desc)
747c5aff182SThomas Petazzoni {
748c5aff182SThomas Petazzoni 	u32 val;
749c5aff182SThomas Petazzoni 
750c5aff182SThomas Petazzoni 	/* Only 255 descriptors can be added at once ; Assume caller
7516a20c175SThomas Petazzoni 	 * process TX desriptors in quanta less than 256
7526a20c175SThomas Petazzoni 	 */
753c5aff182SThomas Petazzoni 	val = pend_desc;
754c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
755c5aff182SThomas Petazzoni }
756c5aff182SThomas Petazzoni 
757c5aff182SThomas Petazzoni /* Get pointer to next TX descriptor to be processed (send) by HW */
758c5aff182SThomas Petazzoni static struct mvneta_tx_desc *
759c5aff182SThomas Petazzoni mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
760c5aff182SThomas Petazzoni {
761c5aff182SThomas Petazzoni 	int tx_desc = txq->next_desc_to_proc;
762c5aff182SThomas Petazzoni 
763c5aff182SThomas Petazzoni 	txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
764c5aff182SThomas Petazzoni 	return txq->descs + tx_desc;
765c5aff182SThomas Petazzoni }
766c5aff182SThomas Petazzoni 
767c5aff182SThomas Petazzoni /* Release the last allocated TX descriptor. Useful to handle DMA
7686a20c175SThomas Petazzoni  * mapping failures in the TX path.
7696a20c175SThomas Petazzoni  */
770c5aff182SThomas Petazzoni static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
771c5aff182SThomas Petazzoni {
772c5aff182SThomas Petazzoni 	if (txq->next_desc_to_proc == 0)
773c5aff182SThomas Petazzoni 		txq->next_desc_to_proc = txq->last_desc - 1;
774c5aff182SThomas Petazzoni 	else
775c5aff182SThomas Petazzoni 		txq->next_desc_to_proc--;
776c5aff182SThomas Petazzoni }
777c5aff182SThomas Petazzoni 
778c5aff182SThomas Petazzoni /* Set rxq buf size */
779c5aff182SThomas Petazzoni static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
780c5aff182SThomas Petazzoni 				    struct mvneta_rx_queue *rxq,
781c5aff182SThomas Petazzoni 				    int buf_size)
782c5aff182SThomas Petazzoni {
783c5aff182SThomas Petazzoni 	u32 val;
784c5aff182SThomas Petazzoni 
785c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
786c5aff182SThomas Petazzoni 
787c5aff182SThomas Petazzoni 	val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
788c5aff182SThomas Petazzoni 	val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
789c5aff182SThomas Petazzoni 
790c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
791c5aff182SThomas Petazzoni }
792c5aff182SThomas Petazzoni 
793c5aff182SThomas Petazzoni /* Disable buffer management (BM) */
794c5aff182SThomas Petazzoni static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
795c5aff182SThomas Petazzoni 				  struct mvneta_rx_queue *rxq)
796c5aff182SThomas Petazzoni {
797c5aff182SThomas Petazzoni 	u32 val;
798c5aff182SThomas Petazzoni 
799c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
800c5aff182SThomas Petazzoni 	val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
801c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
802c5aff182SThomas Petazzoni }
803c5aff182SThomas Petazzoni 
804c5aff182SThomas Petazzoni /* Start the Ethernet port RX and TX activity */
805c5aff182SThomas Petazzoni static void mvneta_port_up(struct mvneta_port *pp)
806c5aff182SThomas Petazzoni {
807c5aff182SThomas Petazzoni 	int queue;
808c5aff182SThomas Petazzoni 	u32 q_map;
809c5aff182SThomas Petazzoni 
810c5aff182SThomas Petazzoni 	/* Enable all initialized TXs. */
811c5aff182SThomas Petazzoni 	q_map = 0;
812c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
813c5aff182SThomas Petazzoni 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
814c5aff182SThomas Petazzoni 		if (txq->descs != NULL)
815c5aff182SThomas Petazzoni 			q_map |= (1 << queue);
816c5aff182SThomas Petazzoni 	}
817c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
818c5aff182SThomas Petazzoni 
819c5aff182SThomas Petazzoni 	/* Enable all initialized RXQs. */
820d8936657SMaxime Ripard 	mvreg_write(pp, MVNETA_RXQ_CMD, BIT(rxq_def));
821c5aff182SThomas Petazzoni }
822c5aff182SThomas Petazzoni 
823c5aff182SThomas Petazzoni /* Stop the Ethernet port activity */
824c5aff182SThomas Petazzoni static void mvneta_port_down(struct mvneta_port *pp)
825c5aff182SThomas Petazzoni {
826c5aff182SThomas Petazzoni 	u32 val;
827c5aff182SThomas Petazzoni 	int count;
828c5aff182SThomas Petazzoni 
829c5aff182SThomas Petazzoni 	/* Stop Rx port activity. Check port Rx activity. */
830c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
831c5aff182SThomas Petazzoni 
832c5aff182SThomas Petazzoni 	/* Issue stop command for active channels only */
833c5aff182SThomas Petazzoni 	if (val != 0)
834c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_RXQ_CMD,
835c5aff182SThomas Petazzoni 			    val << MVNETA_RXQ_DISABLE_SHIFT);
836c5aff182SThomas Petazzoni 
837c5aff182SThomas Petazzoni 	/* Wait for all Rx activity to terminate. */
838c5aff182SThomas Petazzoni 	count = 0;
839c5aff182SThomas Petazzoni 	do {
840c5aff182SThomas Petazzoni 		if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
841c5aff182SThomas Petazzoni 			netdev_warn(pp->dev,
842c5aff182SThomas Petazzoni 				    "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
843c5aff182SThomas Petazzoni 				    val);
844c5aff182SThomas Petazzoni 			break;
845c5aff182SThomas Petazzoni 		}
846c5aff182SThomas Petazzoni 		mdelay(1);
847c5aff182SThomas Petazzoni 
848c5aff182SThomas Petazzoni 		val = mvreg_read(pp, MVNETA_RXQ_CMD);
849c5aff182SThomas Petazzoni 	} while (val & 0xff);
850c5aff182SThomas Petazzoni 
851c5aff182SThomas Petazzoni 	/* Stop Tx port activity. Check port Tx activity. Issue stop
8526a20c175SThomas Petazzoni 	 * command for active channels only
8536a20c175SThomas Petazzoni 	 */
854c5aff182SThomas Petazzoni 	val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
855c5aff182SThomas Petazzoni 
856c5aff182SThomas Petazzoni 	if (val != 0)
857c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_TXQ_CMD,
858c5aff182SThomas Petazzoni 			    (val << MVNETA_TXQ_DISABLE_SHIFT));
859c5aff182SThomas Petazzoni 
860c5aff182SThomas Petazzoni 	/* Wait for all Tx activity to terminate. */
861c5aff182SThomas Petazzoni 	count = 0;
862c5aff182SThomas Petazzoni 	do {
863c5aff182SThomas Petazzoni 		if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
864c5aff182SThomas Petazzoni 			netdev_warn(pp->dev,
865c5aff182SThomas Petazzoni 				    "TIMEOUT for TX stopped status=0x%08x\n",
866c5aff182SThomas Petazzoni 				    val);
867c5aff182SThomas Petazzoni 			break;
868c5aff182SThomas Petazzoni 		}
869c5aff182SThomas Petazzoni 		mdelay(1);
870c5aff182SThomas Petazzoni 
871c5aff182SThomas Petazzoni 		/* Check TX Command reg that all Txqs are stopped */
872c5aff182SThomas Petazzoni 		val = mvreg_read(pp, MVNETA_TXQ_CMD);
873c5aff182SThomas Petazzoni 
874c5aff182SThomas Petazzoni 	} while (val & 0xff);
875c5aff182SThomas Petazzoni 
876c5aff182SThomas Petazzoni 	/* Double check to verify that TX FIFO is empty */
877c5aff182SThomas Petazzoni 	count = 0;
878c5aff182SThomas Petazzoni 	do {
879c5aff182SThomas Petazzoni 		if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
880c5aff182SThomas Petazzoni 			netdev_warn(pp->dev,
881c5aff182SThomas Petazzoni 				    "TX FIFO empty timeout status=0x08%x\n",
882c5aff182SThomas Petazzoni 				    val);
883c5aff182SThomas Petazzoni 			break;
884c5aff182SThomas Petazzoni 		}
885c5aff182SThomas Petazzoni 		mdelay(1);
886c5aff182SThomas Petazzoni 
887c5aff182SThomas Petazzoni 		val = mvreg_read(pp, MVNETA_PORT_STATUS);
888c5aff182SThomas Petazzoni 	} while (!(val & MVNETA_TX_FIFO_EMPTY) &&
889c5aff182SThomas Petazzoni 		 (val & MVNETA_TX_IN_PRGRS));
890c5aff182SThomas Petazzoni 
891c5aff182SThomas Petazzoni 	udelay(200);
892c5aff182SThomas Petazzoni }
893c5aff182SThomas Petazzoni 
894c5aff182SThomas Petazzoni /* Enable the port by setting the port enable bit of the MAC control register */
895c5aff182SThomas Petazzoni static void mvneta_port_enable(struct mvneta_port *pp)
896c5aff182SThomas Petazzoni {
897c5aff182SThomas Petazzoni 	u32 val;
898c5aff182SThomas Petazzoni 
899c5aff182SThomas Petazzoni 	/* Enable port */
900c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
901c5aff182SThomas Petazzoni 	val |= MVNETA_GMAC0_PORT_ENABLE;
902c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
903c5aff182SThomas Petazzoni }
904c5aff182SThomas Petazzoni 
905c5aff182SThomas Petazzoni /* Disable the port and wait for about 200 usec before retuning */
906c5aff182SThomas Petazzoni static void mvneta_port_disable(struct mvneta_port *pp)
907c5aff182SThomas Petazzoni {
908c5aff182SThomas Petazzoni 	u32 val;
909c5aff182SThomas Petazzoni 
910c5aff182SThomas Petazzoni 	/* Reset the Enable bit in the Serial Control Register */
911c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
912c5aff182SThomas Petazzoni 	val &= ~MVNETA_GMAC0_PORT_ENABLE;
913c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
914c5aff182SThomas Petazzoni 
915c5aff182SThomas Petazzoni 	udelay(200);
916c5aff182SThomas Petazzoni }
917c5aff182SThomas Petazzoni 
918c5aff182SThomas Petazzoni /* Multicast tables methods */
919c5aff182SThomas Petazzoni 
920c5aff182SThomas Petazzoni /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
921c5aff182SThomas Petazzoni static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
922c5aff182SThomas Petazzoni {
923c5aff182SThomas Petazzoni 	int offset;
924c5aff182SThomas Petazzoni 	u32 val;
925c5aff182SThomas Petazzoni 
926c5aff182SThomas Petazzoni 	if (queue == -1) {
927c5aff182SThomas Petazzoni 		val = 0;
928c5aff182SThomas Petazzoni 	} else {
929c5aff182SThomas Petazzoni 		val = 0x1 | (queue << 1);
930c5aff182SThomas Petazzoni 		val |= (val << 24) | (val << 16) | (val << 8);
931c5aff182SThomas Petazzoni 	}
932c5aff182SThomas Petazzoni 
933c5aff182SThomas Petazzoni 	for (offset = 0; offset <= 0xc; offset += 4)
934c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
935c5aff182SThomas Petazzoni }
936c5aff182SThomas Petazzoni 
937c5aff182SThomas Petazzoni /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
938c5aff182SThomas Petazzoni static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
939c5aff182SThomas Petazzoni {
940c5aff182SThomas Petazzoni 	int offset;
941c5aff182SThomas Petazzoni 	u32 val;
942c5aff182SThomas Petazzoni 
943c5aff182SThomas Petazzoni 	if (queue == -1) {
944c5aff182SThomas Petazzoni 		val = 0;
945c5aff182SThomas Petazzoni 	} else {
946c5aff182SThomas Petazzoni 		val = 0x1 | (queue << 1);
947c5aff182SThomas Petazzoni 		val |= (val << 24) | (val << 16) | (val << 8);
948c5aff182SThomas Petazzoni 	}
949c5aff182SThomas Petazzoni 
950c5aff182SThomas Petazzoni 	for (offset = 0; offset <= 0xfc; offset += 4)
951c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
952c5aff182SThomas Petazzoni 
953c5aff182SThomas Petazzoni }
954c5aff182SThomas Petazzoni 
955c5aff182SThomas Petazzoni /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
956c5aff182SThomas Petazzoni static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
957c5aff182SThomas Petazzoni {
958c5aff182SThomas Petazzoni 	int offset;
959c5aff182SThomas Petazzoni 	u32 val;
960c5aff182SThomas Petazzoni 
961c5aff182SThomas Petazzoni 	if (queue == -1) {
962c5aff182SThomas Petazzoni 		memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
963c5aff182SThomas Petazzoni 		val = 0;
964c5aff182SThomas Petazzoni 	} else {
965c5aff182SThomas Petazzoni 		memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
966c5aff182SThomas Petazzoni 		val = 0x1 | (queue << 1);
967c5aff182SThomas Petazzoni 		val |= (val << 24) | (val << 16) | (val << 8);
968c5aff182SThomas Petazzoni 	}
969c5aff182SThomas Petazzoni 
970c5aff182SThomas Petazzoni 	for (offset = 0; offset <= 0xfc; offset += 4)
971c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
972c5aff182SThomas Petazzoni }
973c5aff182SThomas Petazzoni 
974c5aff182SThomas Petazzoni /* This method sets defaults to the NETA port:
975c5aff182SThomas Petazzoni  *	Clears interrupt Cause and Mask registers.
976c5aff182SThomas Petazzoni  *	Clears all MAC tables.
977c5aff182SThomas Petazzoni  *	Sets defaults to all registers.
978c5aff182SThomas Petazzoni  *	Resets RX and TX descriptor rings.
979c5aff182SThomas Petazzoni  *	Resets PHY.
980c5aff182SThomas Petazzoni  * This method can be called after mvneta_port_down() to return the port
981c5aff182SThomas Petazzoni  *	settings to defaults.
982c5aff182SThomas Petazzoni  */
983c5aff182SThomas Petazzoni static void mvneta_defaults_set(struct mvneta_port *pp)
984c5aff182SThomas Petazzoni {
985c5aff182SThomas Petazzoni 	int cpu;
986c5aff182SThomas Petazzoni 	int queue;
987c5aff182SThomas Petazzoni 	u32 val;
988c5aff182SThomas Petazzoni 
989c5aff182SThomas Petazzoni 	/* Clear all Cause registers */
990c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
991c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
992c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
993c5aff182SThomas Petazzoni 
994c5aff182SThomas Petazzoni 	/* Mask all interrupts */
995c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
996c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
997c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
998c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
999c5aff182SThomas Petazzoni 
1000c5aff182SThomas Petazzoni 	/* Enable MBUS Retry bit16 */
1001c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1002c5aff182SThomas Petazzoni 
1003c5aff182SThomas Petazzoni 	/* Set CPU queue access map - all CPUs have access to all RX
10046a20c175SThomas Petazzoni 	 * queues and to all TX queues
10056a20c175SThomas Petazzoni 	 */
10062502d0efSMaxime Ripard 	for_each_present_cpu(cpu)
1007c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_CPU_MAP(cpu),
1008c5aff182SThomas Petazzoni 			    (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
1009c5aff182SThomas Petazzoni 			     MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
1010c5aff182SThomas Petazzoni 
1011c5aff182SThomas Petazzoni 	/* Reset RX and TX DMAs */
1012c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1013c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1014c5aff182SThomas Petazzoni 
1015c5aff182SThomas Petazzoni 	/* Disable Legacy WRR, Disable EJP, Release from reset */
1016c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1017c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
1018c5aff182SThomas Petazzoni 		mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1019c5aff182SThomas Petazzoni 		mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1020c5aff182SThomas Petazzoni 	}
1021c5aff182SThomas Petazzoni 
1022c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1023c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1024c5aff182SThomas Petazzoni 
1025c5aff182SThomas Petazzoni 	/* Set Port Acceleration Mode */
1026c5aff182SThomas Petazzoni 	val = MVNETA_ACC_MODE_EXT;
1027c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_ACC_MODE, val);
1028c5aff182SThomas Petazzoni 
1029c5aff182SThomas Petazzoni 	/* Update val of portCfg register accordingly with all RxQueue types */
1030c5aff182SThomas Petazzoni 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
1031c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1032c5aff182SThomas Petazzoni 
1033c5aff182SThomas Petazzoni 	val = 0;
1034c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1035c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1036c5aff182SThomas Petazzoni 
1037c5aff182SThomas Petazzoni 	/* Build PORT_SDMA_CONFIG_REG */
1038c5aff182SThomas Petazzoni 	val = 0;
1039c5aff182SThomas Petazzoni 
1040c5aff182SThomas Petazzoni 	/* Default burst size */
1041c5aff182SThomas Petazzoni 	val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1042c5aff182SThomas Petazzoni 	val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
10439ad8fef6SThomas Petazzoni 	val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1044c5aff182SThomas Petazzoni 
10459ad8fef6SThomas Petazzoni #if defined(__BIG_ENDIAN)
10469ad8fef6SThomas Petazzoni 	val |= MVNETA_DESC_SWAP;
10479ad8fef6SThomas Petazzoni #endif
1048c5aff182SThomas Petazzoni 
1049c5aff182SThomas Petazzoni 	/* Assign port SDMA configuration */
1050c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1051c5aff182SThomas Petazzoni 
105271408602SThomas Petazzoni 	/* Disable PHY polling in hardware, since we're using the
105371408602SThomas Petazzoni 	 * kernel phylib to do this.
105471408602SThomas Petazzoni 	 */
105571408602SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
105671408602SThomas Petazzoni 	val &= ~MVNETA_PHY_POLLING_ENABLE;
105771408602SThomas Petazzoni 	mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
105871408602SThomas Petazzoni 
1059898b2970SStas Sergeev 	if (pp->use_inband_status) {
1060898b2970SStas Sergeev 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1061898b2970SStas Sergeev 		val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1062898b2970SStas Sergeev 			 MVNETA_GMAC_FORCE_LINK_DOWN |
1063898b2970SStas Sergeev 			 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1064898b2970SStas Sergeev 		val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1065898b2970SStas Sergeev 		       MVNETA_GMAC_AN_SPEED_EN |
1066898b2970SStas Sergeev 		       MVNETA_GMAC_AN_DUPLEX_EN;
1067898b2970SStas Sergeev 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1068898b2970SStas Sergeev 		val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1069898b2970SStas Sergeev 		val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1070898b2970SStas Sergeev 		mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1071538761b7SStas Sergeev 	} else {
1072538761b7SStas Sergeev 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1073538761b7SStas Sergeev 		val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1074538761b7SStas Sergeev 		       MVNETA_GMAC_AN_SPEED_EN |
1075538761b7SStas Sergeev 		       MVNETA_GMAC_AN_DUPLEX_EN);
1076538761b7SStas Sergeev 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1077898b2970SStas Sergeev 	}
1078898b2970SStas Sergeev 
1079c5aff182SThomas Petazzoni 	mvneta_set_ucast_table(pp, -1);
1080c5aff182SThomas Petazzoni 	mvneta_set_special_mcast_table(pp, -1);
1081c5aff182SThomas Petazzoni 	mvneta_set_other_mcast_table(pp, -1);
1082c5aff182SThomas Petazzoni 
1083c5aff182SThomas Petazzoni 	/* Set port interrupt enable register - default enable all */
1084c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_ENABLE,
1085c5aff182SThomas Petazzoni 		    (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1086c5aff182SThomas Petazzoni 		     | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1087e483911fSAndrew Lunn 
1088e483911fSAndrew Lunn 	mvneta_mib_counters_clear(pp);
1089c5aff182SThomas Petazzoni }
1090c5aff182SThomas Petazzoni 
1091c5aff182SThomas Petazzoni /* Set max sizes for tx queues */
1092c5aff182SThomas Petazzoni static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1093c5aff182SThomas Petazzoni 
1094c5aff182SThomas Petazzoni {
1095c5aff182SThomas Petazzoni 	u32 val, size, mtu;
1096c5aff182SThomas Petazzoni 	int queue;
1097c5aff182SThomas Petazzoni 
1098c5aff182SThomas Petazzoni 	mtu = max_tx_size * 8;
1099c5aff182SThomas Petazzoni 	if (mtu > MVNETA_TX_MTU_MAX)
1100c5aff182SThomas Petazzoni 		mtu = MVNETA_TX_MTU_MAX;
1101c5aff182SThomas Petazzoni 
1102c5aff182SThomas Petazzoni 	/* Set MTU */
1103c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TX_MTU);
1104c5aff182SThomas Petazzoni 	val &= ~MVNETA_TX_MTU_MAX;
1105c5aff182SThomas Petazzoni 	val |= mtu;
1106c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TX_MTU, val);
1107c5aff182SThomas Petazzoni 
1108c5aff182SThomas Petazzoni 	/* TX token size and all TXQs token size must be larger that MTU */
1109c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1110c5aff182SThomas Petazzoni 
1111c5aff182SThomas Petazzoni 	size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1112c5aff182SThomas Petazzoni 	if (size < mtu) {
1113c5aff182SThomas Petazzoni 		size = mtu;
1114c5aff182SThomas Petazzoni 		val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1115c5aff182SThomas Petazzoni 		val |= size;
1116c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1117c5aff182SThomas Petazzoni 	}
1118c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
1119c5aff182SThomas Petazzoni 		val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1120c5aff182SThomas Petazzoni 
1121c5aff182SThomas Petazzoni 		size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1122c5aff182SThomas Petazzoni 		if (size < mtu) {
1123c5aff182SThomas Petazzoni 			size = mtu;
1124c5aff182SThomas Petazzoni 			val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1125c5aff182SThomas Petazzoni 			val |= size;
1126c5aff182SThomas Petazzoni 			mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1127c5aff182SThomas Petazzoni 		}
1128c5aff182SThomas Petazzoni 	}
1129c5aff182SThomas Petazzoni }
1130c5aff182SThomas Petazzoni 
1131c5aff182SThomas Petazzoni /* Set unicast address */
1132c5aff182SThomas Petazzoni static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1133c5aff182SThomas Petazzoni 				  int queue)
1134c5aff182SThomas Petazzoni {
1135c5aff182SThomas Petazzoni 	unsigned int unicast_reg;
1136c5aff182SThomas Petazzoni 	unsigned int tbl_offset;
1137c5aff182SThomas Petazzoni 	unsigned int reg_offset;
1138c5aff182SThomas Petazzoni 
1139c5aff182SThomas Petazzoni 	/* Locate the Unicast table entry */
1140c5aff182SThomas Petazzoni 	last_nibble = (0xf & last_nibble);
1141c5aff182SThomas Petazzoni 
1142c5aff182SThomas Petazzoni 	/* offset from unicast tbl base */
1143c5aff182SThomas Petazzoni 	tbl_offset = (last_nibble / 4) * 4;
1144c5aff182SThomas Petazzoni 
1145c5aff182SThomas Petazzoni 	/* offset within the above reg  */
1146c5aff182SThomas Petazzoni 	reg_offset = last_nibble % 4;
1147c5aff182SThomas Petazzoni 
1148c5aff182SThomas Petazzoni 	unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1149c5aff182SThomas Petazzoni 
1150c5aff182SThomas Petazzoni 	if (queue == -1) {
1151c5aff182SThomas Petazzoni 		/* Clear accepts frame bit at specified unicast DA tbl entry */
1152c5aff182SThomas Petazzoni 		unicast_reg &= ~(0xff << (8 * reg_offset));
1153c5aff182SThomas Petazzoni 	} else {
1154c5aff182SThomas Petazzoni 		unicast_reg &= ~(0xff << (8 * reg_offset));
1155c5aff182SThomas Petazzoni 		unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1156c5aff182SThomas Petazzoni 	}
1157c5aff182SThomas Petazzoni 
1158c5aff182SThomas Petazzoni 	mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1159c5aff182SThomas Petazzoni }
1160c5aff182SThomas Petazzoni 
1161c5aff182SThomas Petazzoni /* Set mac address */
1162c5aff182SThomas Petazzoni static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1163c5aff182SThomas Petazzoni 				int queue)
1164c5aff182SThomas Petazzoni {
1165c5aff182SThomas Petazzoni 	unsigned int mac_h;
1166c5aff182SThomas Petazzoni 	unsigned int mac_l;
1167c5aff182SThomas Petazzoni 
1168c5aff182SThomas Petazzoni 	if (queue != -1) {
1169c5aff182SThomas Petazzoni 		mac_l = (addr[4] << 8) | (addr[5]);
1170c5aff182SThomas Petazzoni 		mac_h = (addr[0] << 24) | (addr[1] << 16) |
1171c5aff182SThomas Petazzoni 			(addr[2] << 8) | (addr[3] << 0);
1172c5aff182SThomas Petazzoni 
1173c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1174c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1175c5aff182SThomas Petazzoni 	}
1176c5aff182SThomas Petazzoni 
1177c5aff182SThomas Petazzoni 	/* Accept frames of this address */
1178c5aff182SThomas Petazzoni 	mvneta_set_ucast_addr(pp, addr[5], queue);
1179c5aff182SThomas Petazzoni }
1180c5aff182SThomas Petazzoni 
11816a20c175SThomas Petazzoni /* Set the number of packets that will be received before RX interrupt
11826a20c175SThomas Petazzoni  * will be generated by HW.
1183c5aff182SThomas Petazzoni  */
1184c5aff182SThomas Petazzoni static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1185c5aff182SThomas Petazzoni 				    struct mvneta_rx_queue *rxq, u32 value)
1186c5aff182SThomas Petazzoni {
1187c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1188c5aff182SThomas Petazzoni 		    value | MVNETA_RXQ_NON_OCCUPIED(0));
1189c5aff182SThomas Petazzoni 	rxq->pkts_coal = value;
1190c5aff182SThomas Petazzoni }
1191c5aff182SThomas Petazzoni 
11926a20c175SThomas Petazzoni /* Set the time delay in usec before RX interrupt will be generated by
11936a20c175SThomas Petazzoni  * HW.
1194c5aff182SThomas Petazzoni  */
1195c5aff182SThomas Petazzoni static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1196c5aff182SThomas Petazzoni 				    struct mvneta_rx_queue *rxq, u32 value)
1197c5aff182SThomas Petazzoni {
1198189dd626SThomas Petazzoni 	u32 val;
1199189dd626SThomas Petazzoni 	unsigned long clk_rate;
1200189dd626SThomas Petazzoni 
1201189dd626SThomas Petazzoni 	clk_rate = clk_get_rate(pp->clk);
1202189dd626SThomas Petazzoni 	val = (clk_rate / 1000000) * value;
1203c5aff182SThomas Petazzoni 
1204c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1205c5aff182SThomas Petazzoni 	rxq->time_coal = value;
1206c5aff182SThomas Petazzoni }
1207c5aff182SThomas Petazzoni 
1208c5aff182SThomas Petazzoni /* Set threshold for TX_DONE pkts coalescing */
1209c5aff182SThomas Petazzoni static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1210c5aff182SThomas Petazzoni 					 struct mvneta_tx_queue *txq, u32 value)
1211c5aff182SThomas Petazzoni {
1212c5aff182SThomas Petazzoni 	u32 val;
1213c5aff182SThomas Petazzoni 
1214c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1215c5aff182SThomas Petazzoni 
1216c5aff182SThomas Petazzoni 	val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1217c5aff182SThomas Petazzoni 	val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1218c5aff182SThomas Petazzoni 
1219c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1220c5aff182SThomas Petazzoni 
1221c5aff182SThomas Petazzoni 	txq->done_pkts_coal = value;
1222c5aff182SThomas Petazzoni }
1223c5aff182SThomas Petazzoni 
1224c5aff182SThomas Petazzoni /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1225c5aff182SThomas Petazzoni static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1226c5aff182SThomas Petazzoni 				u32 phys_addr, u32 cookie)
1227c5aff182SThomas Petazzoni {
1228c5aff182SThomas Petazzoni 	rx_desc->buf_cookie = cookie;
1229c5aff182SThomas Petazzoni 	rx_desc->buf_phys_addr = phys_addr;
1230c5aff182SThomas Petazzoni }
1231c5aff182SThomas Petazzoni 
1232c5aff182SThomas Petazzoni /* Decrement sent descriptors counter */
1233c5aff182SThomas Petazzoni static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1234c5aff182SThomas Petazzoni 				     struct mvneta_tx_queue *txq,
1235c5aff182SThomas Petazzoni 				     int sent_desc)
1236c5aff182SThomas Petazzoni {
1237c5aff182SThomas Petazzoni 	u32 val;
1238c5aff182SThomas Petazzoni 
1239c5aff182SThomas Petazzoni 	/* Only 255 TX descriptors can be updated at once */
1240c5aff182SThomas Petazzoni 	while (sent_desc > 0xff) {
1241c5aff182SThomas Petazzoni 		val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1242c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1243c5aff182SThomas Petazzoni 		sent_desc = sent_desc - 0xff;
1244c5aff182SThomas Petazzoni 	}
1245c5aff182SThomas Petazzoni 
1246c5aff182SThomas Petazzoni 	val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1247c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1248c5aff182SThomas Petazzoni }
1249c5aff182SThomas Petazzoni 
1250c5aff182SThomas Petazzoni /* Get number of TX descriptors already sent by HW */
1251c5aff182SThomas Petazzoni static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1252c5aff182SThomas Petazzoni 					struct mvneta_tx_queue *txq)
1253c5aff182SThomas Petazzoni {
1254c5aff182SThomas Petazzoni 	u32 val;
1255c5aff182SThomas Petazzoni 	int sent_desc;
1256c5aff182SThomas Petazzoni 
1257c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1258c5aff182SThomas Petazzoni 	sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1259c5aff182SThomas Petazzoni 		MVNETA_TXQ_SENT_DESC_SHIFT;
1260c5aff182SThomas Petazzoni 
1261c5aff182SThomas Petazzoni 	return sent_desc;
1262c5aff182SThomas Petazzoni }
1263c5aff182SThomas Petazzoni 
12646a20c175SThomas Petazzoni /* Get number of sent descriptors and decrement counter.
1265c5aff182SThomas Petazzoni  *  The number of sent descriptors is returned.
1266c5aff182SThomas Petazzoni  */
1267c5aff182SThomas Petazzoni static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1268c5aff182SThomas Petazzoni 				     struct mvneta_tx_queue *txq)
1269c5aff182SThomas Petazzoni {
1270c5aff182SThomas Petazzoni 	int sent_desc;
1271c5aff182SThomas Petazzoni 
1272c5aff182SThomas Petazzoni 	/* Get number of sent descriptors */
1273c5aff182SThomas Petazzoni 	sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1274c5aff182SThomas Petazzoni 
1275c5aff182SThomas Petazzoni 	/* Decrement sent descriptors counter */
1276c5aff182SThomas Petazzoni 	if (sent_desc)
1277c5aff182SThomas Petazzoni 		mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1278c5aff182SThomas Petazzoni 
1279c5aff182SThomas Petazzoni 	return sent_desc;
1280c5aff182SThomas Petazzoni }
1281c5aff182SThomas Petazzoni 
1282c5aff182SThomas Petazzoni /* Set TXQ descriptors fields relevant for CSUM calculation */
1283c5aff182SThomas Petazzoni static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1284c5aff182SThomas Petazzoni 				int ip_hdr_len, int l4_proto)
1285c5aff182SThomas Petazzoni {
1286c5aff182SThomas Petazzoni 	u32 command;
1287c5aff182SThomas Petazzoni 
1288c5aff182SThomas Petazzoni 	/* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
12896a20c175SThomas Petazzoni 	 * G_L4_chk, L4_type; required only for checksum
12906a20c175SThomas Petazzoni 	 * calculation
12916a20c175SThomas Petazzoni 	 */
1292c5aff182SThomas Petazzoni 	command =  l3_offs    << MVNETA_TX_L3_OFF_SHIFT;
1293c5aff182SThomas Petazzoni 	command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1294c5aff182SThomas Petazzoni 
12950a198587SThomas Fitzsimmons 	if (l3_proto == htons(ETH_P_IP))
1296c5aff182SThomas Petazzoni 		command |= MVNETA_TXD_IP_CSUM;
1297c5aff182SThomas Petazzoni 	else
1298c5aff182SThomas Petazzoni 		command |= MVNETA_TX_L3_IP6;
1299c5aff182SThomas Petazzoni 
1300c5aff182SThomas Petazzoni 	if (l4_proto == IPPROTO_TCP)
1301c5aff182SThomas Petazzoni 		command |=  MVNETA_TX_L4_CSUM_FULL;
1302c5aff182SThomas Petazzoni 	else if (l4_proto == IPPROTO_UDP)
1303c5aff182SThomas Petazzoni 		command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1304c5aff182SThomas Petazzoni 	else
1305c5aff182SThomas Petazzoni 		command |= MVNETA_TX_L4_CSUM_NOT;
1306c5aff182SThomas Petazzoni 
1307c5aff182SThomas Petazzoni 	return command;
1308c5aff182SThomas Petazzoni }
1309c5aff182SThomas Petazzoni 
1310c5aff182SThomas Petazzoni 
1311c5aff182SThomas Petazzoni /* Display more error info */
1312c5aff182SThomas Petazzoni static void mvneta_rx_error(struct mvneta_port *pp,
1313c5aff182SThomas Petazzoni 			    struct mvneta_rx_desc *rx_desc)
1314c5aff182SThomas Petazzoni {
1315c5aff182SThomas Petazzoni 	u32 status = rx_desc->status;
1316c5aff182SThomas Petazzoni 
13175428213cSwilly tarreau 	if (!mvneta_rxq_desc_is_first_last(status)) {
1318c5aff182SThomas Petazzoni 		netdev_err(pp->dev,
1319c5aff182SThomas Petazzoni 			   "bad rx status %08x (buffer oversize), size=%d\n",
13205428213cSwilly tarreau 			   status, rx_desc->data_size);
1321c5aff182SThomas Petazzoni 		return;
1322c5aff182SThomas Petazzoni 	}
1323c5aff182SThomas Petazzoni 
1324c5aff182SThomas Petazzoni 	switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1325c5aff182SThomas Petazzoni 	case MVNETA_RXD_ERR_CRC:
1326c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1327c5aff182SThomas Petazzoni 			   status, rx_desc->data_size);
1328c5aff182SThomas Petazzoni 		break;
1329c5aff182SThomas Petazzoni 	case MVNETA_RXD_ERR_OVERRUN:
1330c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1331c5aff182SThomas Petazzoni 			   status, rx_desc->data_size);
1332c5aff182SThomas Petazzoni 		break;
1333c5aff182SThomas Petazzoni 	case MVNETA_RXD_ERR_LEN:
1334c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1335c5aff182SThomas Petazzoni 			   status, rx_desc->data_size);
1336c5aff182SThomas Petazzoni 		break;
1337c5aff182SThomas Petazzoni 	case MVNETA_RXD_ERR_RESOURCE:
1338c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1339c5aff182SThomas Petazzoni 			   status, rx_desc->data_size);
1340c5aff182SThomas Petazzoni 		break;
1341c5aff182SThomas Petazzoni 	}
1342c5aff182SThomas Petazzoni }
1343c5aff182SThomas Petazzoni 
13445428213cSwilly tarreau /* Handle RX checksum offload based on the descriptor's status */
13455428213cSwilly tarreau static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
1346c5aff182SThomas Petazzoni 			   struct sk_buff *skb)
1347c5aff182SThomas Petazzoni {
13485428213cSwilly tarreau 	if ((status & MVNETA_RXD_L3_IP4) &&
13495428213cSwilly tarreau 	    (status & MVNETA_RXD_L4_CSUM_OK)) {
1350c5aff182SThomas Petazzoni 		skb->csum = 0;
1351c5aff182SThomas Petazzoni 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1352c5aff182SThomas Petazzoni 		return;
1353c5aff182SThomas Petazzoni 	}
1354c5aff182SThomas Petazzoni 
1355c5aff182SThomas Petazzoni 	skb->ip_summed = CHECKSUM_NONE;
1356c5aff182SThomas Petazzoni }
1357c5aff182SThomas Petazzoni 
13586c498974Swilly tarreau /* Return tx queue pointer (find last set bit) according to <cause> returned
13596c498974Swilly tarreau  * form tx_done reg. <cause> must not be null. The return value is always a
13606c498974Swilly tarreau  * valid queue for matching the first one found in <cause>.
13616c498974Swilly tarreau  */
1362c5aff182SThomas Petazzoni static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1363c5aff182SThomas Petazzoni 						     u32 cause)
1364c5aff182SThomas Petazzoni {
1365c5aff182SThomas Petazzoni 	int queue = fls(cause) - 1;
1366c5aff182SThomas Petazzoni 
13676c498974Swilly tarreau 	return &pp->txqs[queue];
1368c5aff182SThomas Petazzoni }
1369c5aff182SThomas Petazzoni 
1370c5aff182SThomas Petazzoni /* Free tx queue skbuffs */
1371c5aff182SThomas Petazzoni static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1372c5aff182SThomas Petazzoni 				 struct mvneta_tx_queue *txq, int num)
1373c5aff182SThomas Petazzoni {
1374c5aff182SThomas Petazzoni 	int i;
1375c5aff182SThomas Petazzoni 
1376c5aff182SThomas Petazzoni 	for (i = 0; i < num; i++) {
1377c5aff182SThomas Petazzoni 		struct mvneta_tx_desc *tx_desc = txq->descs +
1378c5aff182SThomas Petazzoni 			txq->txq_get_index;
1379c5aff182SThomas Petazzoni 		struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1380c5aff182SThomas Petazzoni 
1381c5aff182SThomas Petazzoni 		mvneta_txq_inc_get(txq);
1382c5aff182SThomas Petazzoni 
13832e3173a3SEzequiel Garcia 		if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
13842e3173a3SEzequiel Garcia 			dma_unmap_single(pp->dev->dev.parent,
13852e3173a3SEzequiel Garcia 					 tx_desc->buf_phys_addr,
1386c5aff182SThomas Petazzoni 					 tx_desc->data_size, DMA_TO_DEVICE);
1387ba7e46efSEzequiel Garcia 		if (!skb)
1388ba7e46efSEzequiel Garcia 			continue;
1389c5aff182SThomas Petazzoni 		dev_kfree_skb_any(skb);
1390c5aff182SThomas Petazzoni 	}
1391c5aff182SThomas Petazzoni }
1392c5aff182SThomas Petazzoni 
1393c5aff182SThomas Petazzoni /* Handle end of transmission */
1394cd713199SArnaud Ebalard static void mvneta_txq_done(struct mvneta_port *pp,
1395c5aff182SThomas Petazzoni 			   struct mvneta_tx_queue *txq)
1396c5aff182SThomas Petazzoni {
1397c5aff182SThomas Petazzoni 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1398c5aff182SThomas Petazzoni 	int tx_done;
1399c5aff182SThomas Petazzoni 
1400c5aff182SThomas Petazzoni 	tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1401cd713199SArnaud Ebalard 	if (!tx_done)
1402cd713199SArnaud Ebalard 		return;
1403cd713199SArnaud Ebalard 
1404c5aff182SThomas Petazzoni 	mvneta_txq_bufs_free(pp, txq, tx_done);
1405c5aff182SThomas Petazzoni 
1406c5aff182SThomas Petazzoni 	txq->count -= tx_done;
1407c5aff182SThomas Petazzoni 
1408c5aff182SThomas Petazzoni 	if (netif_tx_queue_stopped(nq)) {
14098eef5f97SEzequiel Garcia 		if (txq->count <= txq->tx_wake_threshold)
1410c5aff182SThomas Petazzoni 			netif_tx_wake_queue(nq);
1411c5aff182SThomas Petazzoni 	}
1412c5aff182SThomas Petazzoni }
1413c5aff182SThomas Petazzoni 
14148ec2cd48Swilly tarreau static void *mvneta_frag_alloc(const struct mvneta_port *pp)
14158ec2cd48Swilly tarreau {
14168ec2cd48Swilly tarreau 	if (likely(pp->frag_size <= PAGE_SIZE))
14178ec2cd48Swilly tarreau 		return netdev_alloc_frag(pp->frag_size);
14188ec2cd48Swilly tarreau 	else
14198ec2cd48Swilly tarreau 		return kmalloc(pp->frag_size, GFP_ATOMIC);
14208ec2cd48Swilly tarreau }
14218ec2cd48Swilly tarreau 
14228ec2cd48Swilly tarreau static void mvneta_frag_free(const struct mvneta_port *pp, void *data)
14238ec2cd48Swilly tarreau {
14248ec2cd48Swilly tarreau 	if (likely(pp->frag_size <= PAGE_SIZE))
142513dc0d2bSAlexander Duyck 		skb_free_frag(data);
14268ec2cd48Swilly tarreau 	else
14278ec2cd48Swilly tarreau 		kfree(data);
14288ec2cd48Swilly tarreau }
14298ec2cd48Swilly tarreau 
1430c5aff182SThomas Petazzoni /* Refill processing */
1431c5aff182SThomas Petazzoni static int mvneta_rx_refill(struct mvneta_port *pp,
1432c5aff182SThomas Petazzoni 			    struct mvneta_rx_desc *rx_desc)
1433c5aff182SThomas Petazzoni 
1434c5aff182SThomas Petazzoni {
1435c5aff182SThomas Petazzoni 	dma_addr_t phys_addr;
14368ec2cd48Swilly tarreau 	void *data;
1437c5aff182SThomas Petazzoni 
14388ec2cd48Swilly tarreau 	data = mvneta_frag_alloc(pp);
14398ec2cd48Swilly tarreau 	if (!data)
1440c5aff182SThomas Petazzoni 		return -ENOMEM;
1441c5aff182SThomas Petazzoni 
14428ec2cd48Swilly tarreau 	phys_addr = dma_map_single(pp->dev->dev.parent, data,
1443c5aff182SThomas Petazzoni 				   MVNETA_RX_BUF_SIZE(pp->pkt_size),
1444c5aff182SThomas Petazzoni 				   DMA_FROM_DEVICE);
1445c5aff182SThomas Petazzoni 	if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
14468ec2cd48Swilly tarreau 		mvneta_frag_free(pp, data);
1447c5aff182SThomas Petazzoni 		return -ENOMEM;
1448c5aff182SThomas Petazzoni 	}
1449c5aff182SThomas Petazzoni 
14508ec2cd48Swilly tarreau 	mvneta_rx_desc_fill(rx_desc, phys_addr, (u32)data);
1451c5aff182SThomas Petazzoni 	return 0;
1452c5aff182SThomas Petazzoni }
1453c5aff182SThomas Petazzoni 
1454c5aff182SThomas Petazzoni /* Handle tx checksum */
1455c5aff182SThomas Petazzoni static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1456c5aff182SThomas Petazzoni {
1457c5aff182SThomas Petazzoni 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1458c5aff182SThomas Petazzoni 		int ip_hdr_len = 0;
1459817dbfa5SVlad Yasevich 		__be16 l3_proto = vlan_get_protocol(skb);
1460c5aff182SThomas Petazzoni 		u8 l4_proto;
1461c5aff182SThomas Petazzoni 
1462817dbfa5SVlad Yasevich 		if (l3_proto == htons(ETH_P_IP)) {
1463c5aff182SThomas Petazzoni 			struct iphdr *ip4h = ip_hdr(skb);
1464c5aff182SThomas Petazzoni 
1465c5aff182SThomas Petazzoni 			/* Calculate IPv4 checksum and L4 checksum */
1466c5aff182SThomas Petazzoni 			ip_hdr_len = ip4h->ihl;
1467c5aff182SThomas Petazzoni 			l4_proto = ip4h->protocol;
1468817dbfa5SVlad Yasevich 		} else if (l3_proto == htons(ETH_P_IPV6)) {
1469c5aff182SThomas Petazzoni 			struct ipv6hdr *ip6h = ipv6_hdr(skb);
1470c5aff182SThomas Petazzoni 
1471c5aff182SThomas Petazzoni 			/* Read l4_protocol from one of IPv6 extra headers */
1472c5aff182SThomas Petazzoni 			if (skb_network_header_len(skb) > 0)
1473c5aff182SThomas Petazzoni 				ip_hdr_len = (skb_network_header_len(skb) >> 2);
1474c5aff182SThomas Petazzoni 			l4_proto = ip6h->nexthdr;
1475c5aff182SThomas Petazzoni 		} else
1476c5aff182SThomas Petazzoni 			return MVNETA_TX_L4_CSUM_NOT;
1477c5aff182SThomas Petazzoni 
1478c5aff182SThomas Petazzoni 		return mvneta_txq_desc_csum(skb_network_offset(skb),
1479817dbfa5SVlad Yasevich 					    l3_proto, ip_hdr_len, l4_proto);
1480c5aff182SThomas Petazzoni 	}
1481c5aff182SThomas Petazzoni 
1482c5aff182SThomas Petazzoni 	return MVNETA_TX_L4_CSUM_NOT;
1483c5aff182SThomas Petazzoni }
1484c5aff182SThomas Petazzoni 
1485c5aff182SThomas Petazzoni /* Drop packets received by the RXQ and free buffers */
1486c5aff182SThomas Petazzoni static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1487c5aff182SThomas Petazzoni 				 struct mvneta_rx_queue *rxq)
1488c5aff182SThomas Petazzoni {
1489c5aff182SThomas Petazzoni 	int rx_done, i;
1490c5aff182SThomas Petazzoni 
1491c5aff182SThomas Petazzoni 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1492c5aff182SThomas Petazzoni 	for (i = 0; i < rxq->size; i++) {
1493c5aff182SThomas Petazzoni 		struct mvneta_rx_desc *rx_desc = rxq->descs + i;
14948ec2cd48Swilly tarreau 		void *data = (void *)rx_desc->buf_cookie;
1495c5aff182SThomas Petazzoni 
1496c5aff182SThomas Petazzoni 		dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
1497a328f3a0SEzequiel Garcia 				 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
14988c94ddbcSJustin Maggard 		mvneta_frag_free(pp, data);
1499c5aff182SThomas Petazzoni 	}
1500c5aff182SThomas Petazzoni 
1501c5aff182SThomas Petazzoni 	if (rx_done)
1502c5aff182SThomas Petazzoni 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1503c5aff182SThomas Petazzoni }
1504c5aff182SThomas Petazzoni 
1505c5aff182SThomas Petazzoni /* Main rx processing */
1506c5aff182SThomas Petazzoni static int mvneta_rx(struct mvneta_port *pp, int rx_todo,
1507c5aff182SThomas Petazzoni 		     struct mvneta_rx_queue *rxq)
1508c5aff182SThomas Petazzoni {
150912bb03b4SMaxime Ripard 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
1510c5aff182SThomas Petazzoni 	struct net_device *dev = pp->dev;
1511a84e3289SSimon Guinot 	int rx_done;
1512dc4277ddSwilly tarreau 	u32 rcvd_pkts = 0;
1513dc4277ddSwilly tarreau 	u32 rcvd_bytes = 0;
1514c5aff182SThomas Petazzoni 
1515c5aff182SThomas Petazzoni 	/* Get number of received packets */
1516c5aff182SThomas Petazzoni 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1517c5aff182SThomas Petazzoni 
1518c5aff182SThomas Petazzoni 	if (rx_todo > rx_done)
1519c5aff182SThomas Petazzoni 		rx_todo = rx_done;
1520c5aff182SThomas Petazzoni 
1521c5aff182SThomas Petazzoni 	rx_done = 0;
1522c5aff182SThomas Petazzoni 
1523c5aff182SThomas Petazzoni 	/* Fairness NAPI loop */
1524c5aff182SThomas Petazzoni 	while (rx_done < rx_todo) {
1525c5aff182SThomas Petazzoni 		struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1526c5aff182SThomas Petazzoni 		struct sk_buff *skb;
15278ec2cd48Swilly tarreau 		unsigned char *data;
1528daf158d0SSimon Guinot 		dma_addr_t phys_addr;
1529c5aff182SThomas Petazzoni 		u32 rx_status;
1530c5aff182SThomas Petazzoni 		int rx_bytes, err;
1531c5aff182SThomas Petazzoni 
1532c5aff182SThomas Petazzoni 		rx_done++;
1533c5aff182SThomas Petazzoni 		rx_status = rx_desc->status;
1534f19fadfcSwilly tarreau 		rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
15358ec2cd48Swilly tarreau 		data = (unsigned char *)rx_desc->buf_cookie;
1536daf158d0SSimon Guinot 		phys_addr = rx_desc->buf_phys_addr;
1537c5aff182SThomas Petazzoni 
15385428213cSwilly tarreau 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1539f19fadfcSwilly tarreau 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1540f19fadfcSwilly tarreau 		err_drop_frame:
1541c5aff182SThomas Petazzoni 			dev->stats.rx_errors++;
1542c5aff182SThomas Petazzoni 			mvneta_rx_error(pp, rx_desc);
15438ec2cd48Swilly tarreau 			/* leave the descriptor untouched */
1544c5aff182SThomas Petazzoni 			continue;
1545c5aff182SThomas Petazzoni 		}
1546c5aff182SThomas Petazzoni 
1547f19fadfcSwilly tarreau 		if (rx_bytes <= rx_copybreak) {
1548f19fadfcSwilly tarreau 			/* better copy a small frame and not unmap the DMA region */
1549f19fadfcSwilly tarreau 			skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1550f19fadfcSwilly tarreau 			if (unlikely(!skb))
1551f19fadfcSwilly tarreau 				goto err_drop_frame;
1552f19fadfcSwilly tarreau 
1553f19fadfcSwilly tarreau 			dma_sync_single_range_for_cpu(dev->dev.parent,
1554f19fadfcSwilly tarreau 			                              rx_desc->buf_phys_addr,
1555f19fadfcSwilly tarreau 			                              MVNETA_MH_SIZE + NET_SKB_PAD,
1556f19fadfcSwilly tarreau 			                              rx_bytes,
1557f19fadfcSwilly tarreau 			                              DMA_FROM_DEVICE);
1558f19fadfcSwilly tarreau 			memcpy(skb_put(skb, rx_bytes),
1559f19fadfcSwilly tarreau 			       data + MVNETA_MH_SIZE + NET_SKB_PAD,
1560f19fadfcSwilly tarreau 			       rx_bytes);
1561f19fadfcSwilly tarreau 
1562f19fadfcSwilly tarreau 			skb->protocol = eth_type_trans(skb, dev);
1563f19fadfcSwilly tarreau 			mvneta_rx_csum(pp, rx_status, skb);
156412bb03b4SMaxime Ripard 			napi_gro_receive(&port->napi, skb);
1565f19fadfcSwilly tarreau 
1566f19fadfcSwilly tarreau 			rcvd_pkts++;
1567f19fadfcSwilly tarreau 			rcvd_bytes += rx_bytes;
1568f19fadfcSwilly tarreau 
1569f19fadfcSwilly tarreau 			/* leave the descriptor and buffer untouched */
1570f19fadfcSwilly tarreau 			continue;
1571f19fadfcSwilly tarreau 		}
1572f19fadfcSwilly tarreau 
1573a84e3289SSimon Guinot 		/* Refill processing */
1574a84e3289SSimon Guinot 		err = mvneta_rx_refill(pp, rx_desc);
1575a84e3289SSimon Guinot 		if (err) {
1576a84e3289SSimon Guinot 			netdev_err(dev, "Linux processing - Can't refill\n");
1577a84e3289SSimon Guinot 			rxq->missed++;
1578a84e3289SSimon Guinot 			goto err_drop_frame;
1579a84e3289SSimon Guinot 		}
1580a84e3289SSimon Guinot 
1581f19fadfcSwilly tarreau 		skb = build_skb(data, pp->frag_size > PAGE_SIZE ? 0 : pp->frag_size);
1582f19fadfcSwilly tarreau 		if (!skb)
1583f19fadfcSwilly tarreau 			goto err_drop_frame;
1584f19fadfcSwilly tarreau 
1585daf158d0SSimon Guinot 		dma_unmap_single(dev->dev.parent, phys_addr,
1586a328f3a0SEzequiel Garcia 				 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
1587c5aff182SThomas Petazzoni 
1588dc4277ddSwilly tarreau 		rcvd_pkts++;
1589dc4277ddSwilly tarreau 		rcvd_bytes += rx_bytes;
1590c5aff182SThomas Petazzoni 
1591c5aff182SThomas Petazzoni 		/* Linux processing */
15928ec2cd48Swilly tarreau 		skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
1593c5aff182SThomas Petazzoni 		skb_put(skb, rx_bytes);
1594c5aff182SThomas Petazzoni 
1595c5aff182SThomas Petazzoni 		skb->protocol = eth_type_trans(skb, dev);
1596c5aff182SThomas Petazzoni 
15975428213cSwilly tarreau 		mvneta_rx_csum(pp, rx_status, skb);
1598c5aff182SThomas Petazzoni 
159912bb03b4SMaxime Ripard 		napi_gro_receive(&port->napi, skb);
1600c5aff182SThomas Petazzoni 	}
1601c5aff182SThomas Petazzoni 
1602dc4277ddSwilly tarreau 	if (rcvd_pkts) {
160374c41b04Swilly tarreau 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
160474c41b04Swilly tarreau 
160574c41b04Swilly tarreau 		u64_stats_update_begin(&stats->syncp);
160674c41b04Swilly tarreau 		stats->rx_packets += rcvd_pkts;
160774c41b04Swilly tarreau 		stats->rx_bytes   += rcvd_bytes;
160874c41b04Swilly tarreau 		u64_stats_update_end(&stats->syncp);
1609dc4277ddSwilly tarreau 	}
1610dc4277ddSwilly tarreau 
1611c5aff182SThomas Petazzoni 	/* Update rxq management counters */
1612a84e3289SSimon Guinot 	mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1613c5aff182SThomas Petazzoni 
1614c5aff182SThomas Petazzoni 	return rx_done;
1615c5aff182SThomas Petazzoni }
1616c5aff182SThomas Petazzoni 
16172adb719dSEzequiel Garcia static inline void
16182adb719dSEzequiel Garcia mvneta_tso_put_hdr(struct sk_buff *skb,
16192adb719dSEzequiel Garcia 		   struct mvneta_port *pp, struct mvneta_tx_queue *txq)
16202adb719dSEzequiel Garcia {
16212adb719dSEzequiel Garcia 	struct mvneta_tx_desc *tx_desc;
16222adb719dSEzequiel Garcia 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
16232adb719dSEzequiel Garcia 
16242adb719dSEzequiel Garcia 	txq->tx_skb[txq->txq_put_index] = NULL;
16252adb719dSEzequiel Garcia 	tx_desc = mvneta_txq_next_desc_get(txq);
16262adb719dSEzequiel Garcia 	tx_desc->data_size = hdr_len;
16272adb719dSEzequiel Garcia 	tx_desc->command = mvneta_skb_tx_csum(pp, skb);
16282adb719dSEzequiel Garcia 	tx_desc->command |= MVNETA_TXD_F_DESC;
16292adb719dSEzequiel Garcia 	tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
16302adb719dSEzequiel Garcia 				 txq->txq_put_index * TSO_HEADER_SIZE;
16312adb719dSEzequiel Garcia 	mvneta_txq_inc_put(txq);
16322adb719dSEzequiel Garcia }
16332adb719dSEzequiel Garcia 
16342adb719dSEzequiel Garcia static inline int
16352adb719dSEzequiel Garcia mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
16362adb719dSEzequiel Garcia 		    struct sk_buff *skb, char *data, int size,
16372adb719dSEzequiel Garcia 		    bool last_tcp, bool is_last)
16382adb719dSEzequiel Garcia {
16392adb719dSEzequiel Garcia 	struct mvneta_tx_desc *tx_desc;
16402adb719dSEzequiel Garcia 
16412adb719dSEzequiel Garcia 	tx_desc = mvneta_txq_next_desc_get(txq);
16422adb719dSEzequiel Garcia 	tx_desc->data_size = size;
16432adb719dSEzequiel Garcia 	tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
16442adb719dSEzequiel Garcia 						size, DMA_TO_DEVICE);
16452adb719dSEzequiel Garcia 	if (unlikely(dma_mapping_error(dev->dev.parent,
16462adb719dSEzequiel Garcia 		     tx_desc->buf_phys_addr))) {
16472adb719dSEzequiel Garcia 		mvneta_txq_desc_put(txq);
16482adb719dSEzequiel Garcia 		return -ENOMEM;
16492adb719dSEzequiel Garcia 	}
16502adb719dSEzequiel Garcia 
16512adb719dSEzequiel Garcia 	tx_desc->command = 0;
16522adb719dSEzequiel Garcia 	txq->tx_skb[txq->txq_put_index] = NULL;
16532adb719dSEzequiel Garcia 
16542adb719dSEzequiel Garcia 	if (last_tcp) {
16552adb719dSEzequiel Garcia 		/* last descriptor in the TCP packet */
16562adb719dSEzequiel Garcia 		tx_desc->command = MVNETA_TXD_L_DESC;
16572adb719dSEzequiel Garcia 
16582adb719dSEzequiel Garcia 		/* last descriptor in SKB */
16592adb719dSEzequiel Garcia 		if (is_last)
16602adb719dSEzequiel Garcia 			txq->tx_skb[txq->txq_put_index] = skb;
16612adb719dSEzequiel Garcia 	}
16622adb719dSEzequiel Garcia 	mvneta_txq_inc_put(txq);
16632adb719dSEzequiel Garcia 	return 0;
16642adb719dSEzequiel Garcia }
16652adb719dSEzequiel Garcia 
16662adb719dSEzequiel Garcia static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
16672adb719dSEzequiel Garcia 			 struct mvneta_tx_queue *txq)
16682adb719dSEzequiel Garcia {
16692adb719dSEzequiel Garcia 	int total_len, data_left;
16702adb719dSEzequiel Garcia 	int desc_count = 0;
16712adb719dSEzequiel Garcia 	struct mvneta_port *pp = netdev_priv(dev);
16722adb719dSEzequiel Garcia 	struct tso_t tso;
16732adb719dSEzequiel Garcia 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
16742adb719dSEzequiel Garcia 	int i;
16752adb719dSEzequiel Garcia 
16762adb719dSEzequiel Garcia 	/* Count needed descriptors */
16772adb719dSEzequiel Garcia 	if ((txq->count + tso_count_descs(skb)) >= txq->size)
16782adb719dSEzequiel Garcia 		return 0;
16792adb719dSEzequiel Garcia 
16802adb719dSEzequiel Garcia 	if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
16812adb719dSEzequiel Garcia 		pr_info("*** Is this even  possible???!?!?\n");
16822adb719dSEzequiel Garcia 		return 0;
16832adb719dSEzequiel Garcia 	}
16842adb719dSEzequiel Garcia 
16852adb719dSEzequiel Garcia 	/* Initialize the TSO handler, and prepare the first payload */
16862adb719dSEzequiel Garcia 	tso_start(skb, &tso);
16872adb719dSEzequiel Garcia 
16882adb719dSEzequiel Garcia 	total_len = skb->len - hdr_len;
16892adb719dSEzequiel Garcia 	while (total_len > 0) {
16902adb719dSEzequiel Garcia 		char *hdr;
16912adb719dSEzequiel Garcia 
16922adb719dSEzequiel Garcia 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
16932adb719dSEzequiel Garcia 		total_len -= data_left;
16942adb719dSEzequiel Garcia 		desc_count++;
16952adb719dSEzequiel Garcia 
16962adb719dSEzequiel Garcia 		/* prepare packet headers: MAC + IP + TCP */
16972adb719dSEzequiel Garcia 		hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
16982adb719dSEzequiel Garcia 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
16992adb719dSEzequiel Garcia 
17002adb719dSEzequiel Garcia 		mvneta_tso_put_hdr(skb, pp, txq);
17012adb719dSEzequiel Garcia 
17022adb719dSEzequiel Garcia 		while (data_left > 0) {
17032adb719dSEzequiel Garcia 			int size;
17042adb719dSEzequiel Garcia 			desc_count++;
17052adb719dSEzequiel Garcia 
17062adb719dSEzequiel Garcia 			size = min_t(int, tso.size, data_left);
17072adb719dSEzequiel Garcia 
17082adb719dSEzequiel Garcia 			if (mvneta_tso_put_data(dev, txq, skb,
17092adb719dSEzequiel Garcia 						 tso.data, size,
17102adb719dSEzequiel Garcia 						 size == data_left,
17112adb719dSEzequiel Garcia 						 total_len == 0))
17122adb719dSEzequiel Garcia 				goto err_release;
17132adb719dSEzequiel Garcia 			data_left -= size;
17142adb719dSEzequiel Garcia 
17152adb719dSEzequiel Garcia 			tso_build_data(skb, &tso, size);
17162adb719dSEzequiel Garcia 		}
17172adb719dSEzequiel Garcia 	}
17182adb719dSEzequiel Garcia 
17192adb719dSEzequiel Garcia 	return desc_count;
17202adb719dSEzequiel Garcia 
17212adb719dSEzequiel Garcia err_release:
17222adb719dSEzequiel Garcia 	/* Release all used data descriptors; header descriptors must not
17232adb719dSEzequiel Garcia 	 * be DMA-unmapped.
17242adb719dSEzequiel Garcia 	 */
17252adb719dSEzequiel Garcia 	for (i = desc_count - 1; i >= 0; i--) {
17262adb719dSEzequiel Garcia 		struct mvneta_tx_desc *tx_desc = txq->descs + i;
17272e3173a3SEzequiel Garcia 		if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
17282adb719dSEzequiel Garcia 			dma_unmap_single(pp->dev->dev.parent,
17292adb719dSEzequiel Garcia 					 tx_desc->buf_phys_addr,
17302adb719dSEzequiel Garcia 					 tx_desc->data_size,
17312adb719dSEzequiel Garcia 					 DMA_TO_DEVICE);
17322adb719dSEzequiel Garcia 		mvneta_txq_desc_put(txq);
17332adb719dSEzequiel Garcia 	}
17342adb719dSEzequiel Garcia 	return 0;
17352adb719dSEzequiel Garcia }
17362adb719dSEzequiel Garcia 
1737c5aff182SThomas Petazzoni /* Handle tx fragmentation processing */
1738c5aff182SThomas Petazzoni static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
1739c5aff182SThomas Petazzoni 				  struct mvneta_tx_queue *txq)
1740c5aff182SThomas Petazzoni {
1741c5aff182SThomas Petazzoni 	struct mvneta_tx_desc *tx_desc;
17423d4ea02fSEzequiel Garcia 	int i, nr_frags = skb_shinfo(skb)->nr_frags;
1743c5aff182SThomas Petazzoni 
17443d4ea02fSEzequiel Garcia 	for (i = 0; i < nr_frags; i++) {
1745c5aff182SThomas Petazzoni 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1746c5aff182SThomas Petazzoni 		void *addr = page_address(frag->page.p) + frag->page_offset;
1747c5aff182SThomas Petazzoni 
1748c5aff182SThomas Petazzoni 		tx_desc = mvneta_txq_next_desc_get(txq);
1749c5aff182SThomas Petazzoni 		tx_desc->data_size = frag->size;
1750c5aff182SThomas Petazzoni 
1751c5aff182SThomas Petazzoni 		tx_desc->buf_phys_addr =
1752c5aff182SThomas Petazzoni 			dma_map_single(pp->dev->dev.parent, addr,
1753c5aff182SThomas Petazzoni 				       tx_desc->data_size, DMA_TO_DEVICE);
1754c5aff182SThomas Petazzoni 
1755c5aff182SThomas Petazzoni 		if (dma_mapping_error(pp->dev->dev.parent,
1756c5aff182SThomas Petazzoni 				      tx_desc->buf_phys_addr)) {
1757c5aff182SThomas Petazzoni 			mvneta_txq_desc_put(txq);
1758c5aff182SThomas Petazzoni 			goto error;
1759c5aff182SThomas Petazzoni 		}
1760c5aff182SThomas Petazzoni 
17613d4ea02fSEzequiel Garcia 		if (i == nr_frags - 1) {
1762c5aff182SThomas Petazzoni 			/* Last descriptor */
1763c5aff182SThomas Petazzoni 			tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
1764c5aff182SThomas Petazzoni 			txq->tx_skb[txq->txq_put_index] = skb;
1765c5aff182SThomas Petazzoni 		} else {
1766c5aff182SThomas Petazzoni 			/* Descriptor in the middle: Not First, Not Last */
1767c5aff182SThomas Petazzoni 			tx_desc->command = 0;
1768c5aff182SThomas Petazzoni 			txq->tx_skb[txq->txq_put_index] = NULL;
1769c5aff182SThomas Petazzoni 		}
17703d4ea02fSEzequiel Garcia 		mvneta_txq_inc_put(txq);
1771c5aff182SThomas Petazzoni 	}
1772c5aff182SThomas Petazzoni 
1773c5aff182SThomas Petazzoni 	return 0;
1774c5aff182SThomas Petazzoni 
1775c5aff182SThomas Petazzoni error:
1776c5aff182SThomas Petazzoni 	/* Release all descriptors that were used to map fragments of
17776a20c175SThomas Petazzoni 	 * this packet, as well as the corresponding DMA mappings
17786a20c175SThomas Petazzoni 	 */
1779c5aff182SThomas Petazzoni 	for (i = i - 1; i >= 0; i--) {
1780c5aff182SThomas Petazzoni 		tx_desc = txq->descs + i;
1781c5aff182SThomas Petazzoni 		dma_unmap_single(pp->dev->dev.parent,
1782c5aff182SThomas Petazzoni 				 tx_desc->buf_phys_addr,
1783c5aff182SThomas Petazzoni 				 tx_desc->data_size,
1784c5aff182SThomas Petazzoni 				 DMA_TO_DEVICE);
1785c5aff182SThomas Petazzoni 		mvneta_txq_desc_put(txq);
1786c5aff182SThomas Petazzoni 	}
1787c5aff182SThomas Petazzoni 
1788c5aff182SThomas Petazzoni 	return -ENOMEM;
1789c5aff182SThomas Petazzoni }
1790c5aff182SThomas Petazzoni 
1791c5aff182SThomas Petazzoni /* Main tx processing */
1792c5aff182SThomas Petazzoni static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
1793c5aff182SThomas Petazzoni {
1794c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
1795ee40a116SWilly Tarreau 	u16 txq_id = skb_get_queue_mapping(skb);
1796ee40a116SWilly Tarreau 	struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
1797c5aff182SThomas Petazzoni 	struct mvneta_tx_desc *tx_desc;
17985f478b41SEric Dumazet 	int len = skb->len;
1799c5aff182SThomas Petazzoni 	int frags = 0;
1800c5aff182SThomas Petazzoni 	u32 tx_cmd;
1801c5aff182SThomas Petazzoni 
1802c5aff182SThomas Petazzoni 	if (!netif_running(dev))
1803c5aff182SThomas Petazzoni 		goto out;
1804c5aff182SThomas Petazzoni 
18052adb719dSEzequiel Garcia 	if (skb_is_gso(skb)) {
18062adb719dSEzequiel Garcia 		frags = mvneta_tx_tso(skb, dev, txq);
18072adb719dSEzequiel Garcia 		goto out;
18082adb719dSEzequiel Garcia 	}
18092adb719dSEzequiel Garcia 
1810c5aff182SThomas Petazzoni 	frags = skb_shinfo(skb)->nr_frags + 1;
1811c5aff182SThomas Petazzoni 
1812c5aff182SThomas Petazzoni 	/* Get a descriptor for the first part of the packet */
1813c5aff182SThomas Petazzoni 	tx_desc = mvneta_txq_next_desc_get(txq);
1814c5aff182SThomas Petazzoni 
1815c5aff182SThomas Petazzoni 	tx_cmd = mvneta_skb_tx_csum(pp, skb);
1816c5aff182SThomas Petazzoni 
1817c5aff182SThomas Petazzoni 	tx_desc->data_size = skb_headlen(skb);
1818c5aff182SThomas Petazzoni 
1819c5aff182SThomas Petazzoni 	tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
1820c5aff182SThomas Petazzoni 						tx_desc->data_size,
1821c5aff182SThomas Petazzoni 						DMA_TO_DEVICE);
1822c5aff182SThomas Petazzoni 	if (unlikely(dma_mapping_error(dev->dev.parent,
1823c5aff182SThomas Petazzoni 				       tx_desc->buf_phys_addr))) {
1824c5aff182SThomas Petazzoni 		mvneta_txq_desc_put(txq);
1825c5aff182SThomas Petazzoni 		frags = 0;
1826c5aff182SThomas Petazzoni 		goto out;
1827c5aff182SThomas Petazzoni 	}
1828c5aff182SThomas Petazzoni 
1829c5aff182SThomas Petazzoni 	if (frags == 1) {
1830c5aff182SThomas Petazzoni 		/* First and Last descriptor */
1831c5aff182SThomas Petazzoni 		tx_cmd |= MVNETA_TXD_FLZ_DESC;
1832c5aff182SThomas Petazzoni 		tx_desc->command = tx_cmd;
1833c5aff182SThomas Petazzoni 		txq->tx_skb[txq->txq_put_index] = skb;
1834c5aff182SThomas Petazzoni 		mvneta_txq_inc_put(txq);
1835c5aff182SThomas Petazzoni 	} else {
1836c5aff182SThomas Petazzoni 		/* First but not Last */
1837c5aff182SThomas Petazzoni 		tx_cmd |= MVNETA_TXD_F_DESC;
1838c5aff182SThomas Petazzoni 		txq->tx_skb[txq->txq_put_index] = NULL;
1839c5aff182SThomas Petazzoni 		mvneta_txq_inc_put(txq);
1840c5aff182SThomas Petazzoni 		tx_desc->command = tx_cmd;
1841c5aff182SThomas Petazzoni 		/* Continue with other skb fragments */
1842c5aff182SThomas Petazzoni 		if (mvneta_tx_frag_process(pp, skb, txq)) {
1843c5aff182SThomas Petazzoni 			dma_unmap_single(dev->dev.parent,
1844c5aff182SThomas Petazzoni 					 tx_desc->buf_phys_addr,
1845c5aff182SThomas Petazzoni 					 tx_desc->data_size,
1846c5aff182SThomas Petazzoni 					 DMA_TO_DEVICE);
1847c5aff182SThomas Petazzoni 			mvneta_txq_desc_put(txq);
1848c5aff182SThomas Petazzoni 			frags = 0;
1849c5aff182SThomas Petazzoni 			goto out;
1850c5aff182SThomas Petazzoni 		}
1851c5aff182SThomas Petazzoni 	}
1852c5aff182SThomas Petazzoni 
1853e19d2ddaSEzequiel Garcia out:
1854e19d2ddaSEzequiel Garcia 	if (frags > 0) {
1855e19d2ddaSEzequiel Garcia 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1856e19d2ddaSEzequiel Garcia 		struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
1857e19d2ddaSEzequiel Garcia 
1858c5aff182SThomas Petazzoni 		txq->count += frags;
1859c5aff182SThomas Petazzoni 		mvneta_txq_pend_desc_add(pp, txq, frags);
1860c5aff182SThomas Petazzoni 
18618eef5f97SEzequiel Garcia 		if (txq->count >= txq->tx_stop_threshold)
1862c5aff182SThomas Petazzoni 			netif_tx_stop_queue(nq);
1863c5aff182SThomas Petazzoni 
186474c41b04Swilly tarreau 		u64_stats_update_begin(&stats->syncp);
186574c41b04Swilly tarreau 		stats->tx_packets++;
18665f478b41SEric Dumazet 		stats->tx_bytes  += len;
186774c41b04Swilly tarreau 		u64_stats_update_end(&stats->syncp);
1868c5aff182SThomas Petazzoni 	} else {
1869c5aff182SThomas Petazzoni 		dev->stats.tx_dropped++;
1870c5aff182SThomas Petazzoni 		dev_kfree_skb_any(skb);
1871c5aff182SThomas Petazzoni 	}
1872c5aff182SThomas Petazzoni 
1873c5aff182SThomas Petazzoni 	return NETDEV_TX_OK;
1874c5aff182SThomas Petazzoni }
1875c5aff182SThomas Petazzoni 
1876c5aff182SThomas Petazzoni 
1877c5aff182SThomas Petazzoni /* Free tx resources, when resetting a port */
1878c5aff182SThomas Petazzoni static void mvneta_txq_done_force(struct mvneta_port *pp,
1879c5aff182SThomas Petazzoni 				  struct mvneta_tx_queue *txq)
1880c5aff182SThomas Petazzoni 
1881c5aff182SThomas Petazzoni {
1882c5aff182SThomas Petazzoni 	int tx_done = txq->count;
1883c5aff182SThomas Petazzoni 
1884c5aff182SThomas Petazzoni 	mvneta_txq_bufs_free(pp, txq, tx_done);
1885c5aff182SThomas Petazzoni 
1886c5aff182SThomas Petazzoni 	/* reset txq */
1887c5aff182SThomas Petazzoni 	txq->count = 0;
1888c5aff182SThomas Petazzoni 	txq->txq_put_index = 0;
1889c5aff182SThomas Petazzoni 	txq->txq_get_index = 0;
1890c5aff182SThomas Petazzoni }
1891c5aff182SThomas Petazzoni 
18926c498974Swilly tarreau /* Handle tx done - called in softirq context. The <cause_tx_done> argument
18936c498974Swilly tarreau  * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
18946c498974Swilly tarreau  */
18950713a86aSArnaud Ebalard static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
1896c5aff182SThomas Petazzoni {
1897c5aff182SThomas Petazzoni 	struct mvneta_tx_queue *txq;
1898c5aff182SThomas Petazzoni 	struct netdev_queue *nq;
1899c5aff182SThomas Petazzoni 
19006c498974Swilly tarreau 	while (cause_tx_done) {
1901c5aff182SThomas Petazzoni 		txq = mvneta_tx_done_policy(pp, cause_tx_done);
1902c5aff182SThomas Petazzoni 
1903c5aff182SThomas Petazzoni 		nq = netdev_get_tx_queue(pp->dev, txq->id);
1904c5aff182SThomas Petazzoni 		__netif_tx_lock(nq, smp_processor_id());
1905c5aff182SThomas Petazzoni 
19060713a86aSArnaud Ebalard 		if (txq->count)
19070713a86aSArnaud Ebalard 			mvneta_txq_done(pp, txq);
1908c5aff182SThomas Petazzoni 
1909c5aff182SThomas Petazzoni 		__netif_tx_unlock(nq);
1910c5aff182SThomas Petazzoni 		cause_tx_done &= ~((1 << txq->id));
1911c5aff182SThomas Petazzoni 	}
1912c5aff182SThomas Petazzoni }
1913c5aff182SThomas Petazzoni 
19146a20c175SThomas Petazzoni /* Compute crc8 of the specified address, using a unique algorithm ,
1915c5aff182SThomas Petazzoni  * according to hw spec, different than generic crc8 algorithm
1916c5aff182SThomas Petazzoni  */
1917c5aff182SThomas Petazzoni static int mvneta_addr_crc(unsigned char *addr)
1918c5aff182SThomas Petazzoni {
1919c5aff182SThomas Petazzoni 	int crc = 0;
1920c5aff182SThomas Petazzoni 	int i;
1921c5aff182SThomas Petazzoni 
1922c5aff182SThomas Petazzoni 	for (i = 0; i < ETH_ALEN; i++) {
1923c5aff182SThomas Petazzoni 		int j;
1924c5aff182SThomas Petazzoni 
1925c5aff182SThomas Petazzoni 		crc = (crc ^ addr[i]) << 8;
1926c5aff182SThomas Petazzoni 		for (j = 7; j >= 0; j--) {
1927c5aff182SThomas Petazzoni 			if (crc & (0x100 << j))
1928c5aff182SThomas Petazzoni 				crc ^= 0x107 << j;
1929c5aff182SThomas Petazzoni 		}
1930c5aff182SThomas Petazzoni 	}
1931c5aff182SThomas Petazzoni 
1932c5aff182SThomas Petazzoni 	return crc;
1933c5aff182SThomas Petazzoni }
1934c5aff182SThomas Petazzoni 
1935c5aff182SThomas Petazzoni /* This method controls the net device special MAC multicast support.
1936c5aff182SThomas Petazzoni  * The Special Multicast Table for MAC addresses supports MAC of the form
1937c5aff182SThomas Petazzoni  * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
1938c5aff182SThomas Petazzoni  * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1939c5aff182SThomas Petazzoni  * Table entries in the DA-Filter table. This method set the Special
1940c5aff182SThomas Petazzoni  * Multicast Table appropriate entry.
1941c5aff182SThomas Petazzoni  */
1942c5aff182SThomas Petazzoni static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
1943c5aff182SThomas Petazzoni 					  unsigned char last_byte,
1944c5aff182SThomas Petazzoni 					  int queue)
1945c5aff182SThomas Petazzoni {
1946c5aff182SThomas Petazzoni 	unsigned int smc_table_reg;
1947c5aff182SThomas Petazzoni 	unsigned int tbl_offset;
1948c5aff182SThomas Petazzoni 	unsigned int reg_offset;
1949c5aff182SThomas Petazzoni 
1950c5aff182SThomas Petazzoni 	/* Register offset from SMC table base    */
1951c5aff182SThomas Petazzoni 	tbl_offset = (last_byte / 4);
1952c5aff182SThomas Petazzoni 	/* Entry offset within the above reg */
1953c5aff182SThomas Petazzoni 	reg_offset = last_byte % 4;
1954c5aff182SThomas Petazzoni 
1955c5aff182SThomas Petazzoni 	smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
1956c5aff182SThomas Petazzoni 					+ tbl_offset * 4));
1957c5aff182SThomas Petazzoni 
1958c5aff182SThomas Petazzoni 	if (queue == -1)
1959c5aff182SThomas Petazzoni 		smc_table_reg &= ~(0xff << (8 * reg_offset));
1960c5aff182SThomas Petazzoni 	else {
1961c5aff182SThomas Petazzoni 		smc_table_reg &= ~(0xff << (8 * reg_offset));
1962c5aff182SThomas Petazzoni 		smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1963c5aff182SThomas Petazzoni 	}
1964c5aff182SThomas Petazzoni 
1965c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
1966c5aff182SThomas Petazzoni 		    smc_table_reg);
1967c5aff182SThomas Petazzoni }
1968c5aff182SThomas Petazzoni 
1969c5aff182SThomas Petazzoni /* This method controls the network device Other MAC multicast support.
1970c5aff182SThomas Petazzoni  * The Other Multicast Table is used for multicast of another type.
1971c5aff182SThomas Petazzoni  * A CRC-8 is used as an index to the Other Multicast Table entries
1972c5aff182SThomas Petazzoni  * in the DA-Filter table.
1973c5aff182SThomas Petazzoni  * The method gets the CRC-8 value from the calling routine and
1974c5aff182SThomas Petazzoni  * sets the Other Multicast Table appropriate entry according to the
1975c5aff182SThomas Petazzoni  * specified CRC-8 .
1976c5aff182SThomas Petazzoni  */
1977c5aff182SThomas Petazzoni static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
1978c5aff182SThomas Petazzoni 					unsigned char crc8,
1979c5aff182SThomas Petazzoni 					int queue)
1980c5aff182SThomas Petazzoni {
1981c5aff182SThomas Petazzoni 	unsigned int omc_table_reg;
1982c5aff182SThomas Petazzoni 	unsigned int tbl_offset;
1983c5aff182SThomas Petazzoni 	unsigned int reg_offset;
1984c5aff182SThomas Petazzoni 
1985c5aff182SThomas Petazzoni 	tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
1986c5aff182SThomas Petazzoni 	reg_offset = crc8 % 4;	     /* Entry offset within the above reg   */
1987c5aff182SThomas Petazzoni 
1988c5aff182SThomas Petazzoni 	omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
1989c5aff182SThomas Petazzoni 
1990c5aff182SThomas Petazzoni 	if (queue == -1) {
1991c5aff182SThomas Petazzoni 		/* Clear accepts frame bit at specified Other DA table entry */
1992c5aff182SThomas Petazzoni 		omc_table_reg &= ~(0xff << (8 * reg_offset));
1993c5aff182SThomas Petazzoni 	} else {
1994c5aff182SThomas Petazzoni 		omc_table_reg &= ~(0xff << (8 * reg_offset));
1995c5aff182SThomas Petazzoni 		omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1996c5aff182SThomas Petazzoni 	}
1997c5aff182SThomas Petazzoni 
1998c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
1999c5aff182SThomas Petazzoni }
2000c5aff182SThomas Petazzoni 
2001c5aff182SThomas Petazzoni /* The network device supports multicast using two tables:
2002c5aff182SThomas Petazzoni  *    1) Special Multicast Table for MAC addresses of the form
2003c5aff182SThomas Petazzoni  *       0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2004c5aff182SThomas Petazzoni  *       The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2005c5aff182SThomas Petazzoni  *       Table entries in the DA-Filter table.
2006c5aff182SThomas Petazzoni  *    2) Other Multicast Table for multicast of another type. A CRC-8 value
2007c5aff182SThomas Petazzoni  *       is used as an index to the Other Multicast Table entries in the
2008c5aff182SThomas Petazzoni  *       DA-Filter table.
2009c5aff182SThomas Petazzoni  */
2010c5aff182SThomas Petazzoni static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2011c5aff182SThomas Petazzoni 				 int queue)
2012c5aff182SThomas Petazzoni {
2013c5aff182SThomas Petazzoni 	unsigned char crc_result = 0;
2014c5aff182SThomas Petazzoni 
2015c5aff182SThomas Petazzoni 	if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2016c5aff182SThomas Petazzoni 		mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2017c5aff182SThomas Petazzoni 		return 0;
2018c5aff182SThomas Petazzoni 	}
2019c5aff182SThomas Petazzoni 
2020c5aff182SThomas Petazzoni 	crc_result = mvneta_addr_crc(p_addr);
2021c5aff182SThomas Petazzoni 	if (queue == -1) {
2022c5aff182SThomas Petazzoni 		if (pp->mcast_count[crc_result] == 0) {
2023c5aff182SThomas Petazzoni 			netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2024c5aff182SThomas Petazzoni 				    crc_result);
2025c5aff182SThomas Petazzoni 			return -EINVAL;
2026c5aff182SThomas Petazzoni 		}
2027c5aff182SThomas Petazzoni 
2028c5aff182SThomas Petazzoni 		pp->mcast_count[crc_result]--;
2029c5aff182SThomas Petazzoni 		if (pp->mcast_count[crc_result] != 0) {
2030c5aff182SThomas Petazzoni 			netdev_info(pp->dev,
2031c5aff182SThomas Petazzoni 				    "After delete there are %d valid Mcast for crc8=0x%02x\n",
2032c5aff182SThomas Petazzoni 				    pp->mcast_count[crc_result], crc_result);
2033c5aff182SThomas Petazzoni 			return -EINVAL;
2034c5aff182SThomas Petazzoni 		}
2035c5aff182SThomas Petazzoni 	} else
2036c5aff182SThomas Petazzoni 		pp->mcast_count[crc_result]++;
2037c5aff182SThomas Petazzoni 
2038c5aff182SThomas Petazzoni 	mvneta_set_other_mcast_addr(pp, crc_result, queue);
2039c5aff182SThomas Petazzoni 
2040c5aff182SThomas Petazzoni 	return 0;
2041c5aff182SThomas Petazzoni }
2042c5aff182SThomas Petazzoni 
2043c5aff182SThomas Petazzoni /* Configure Fitering mode of Ethernet port */
2044c5aff182SThomas Petazzoni static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2045c5aff182SThomas Petazzoni 					  int is_promisc)
2046c5aff182SThomas Petazzoni {
2047c5aff182SThomas Petazzoni 	u32 port_cfg_reg, val;
2048c5aff182SThomas Petazzoni 
2049c5aff182SThomas Petazzoni 	port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2050c5aff182SThomas Petazzoni 
2051c5aff182SThomas Petazzoni 	val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2052c5aff182SThomas Petazzoni 
2053c5aff182SThomas Petazzoni 	/* Set / Clear UPM bit in port configuration register */
2054c5aff182SThomas Petazzoni 	if (is_promisc) {
2055c5aff182SThomas Petazzoni 		/* Accept all Unicast addresses */
2056c5aff182SThomas Petazzoni 		port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2057c5aff182SThomas Petazzoni 		val |= MVNETA_FORCE_UNI;
2058c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2059c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2060c5aff182SThomas Petazzoni 	} else {
2061c5aff182SThomas Petazzoni 		/* Reject all Unicast addresses */
2062c5aff182SThomas Petazzoni 		port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2063c5aff182SThomas Petazzoni 		val &= ~MVNETA_FORCE_UNI;
2064c5aff182SThomas Petazzoni 	}
2065c5aff182SThomas Petazzoni 
2066c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2067c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2068c5aff182SThomas Petazzoni }
2069c5aff182SThomas Petazzoni 
2070c5aff182SThomas Petazzoni /* register unicast and multicast addresses */
2071c5aff182SThomas Petazzoni static void mvneta_set_rx_mode(struct net_device *dev)
2072c5aff182SThomas Petazzoni {
2073c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2074c5aff182SThomas Petazzoni 	struct netdev_hw_addr *ha;
2075c5aff182SThomas Petazzoni 
2076c5aff182SThomas Petazzoni 	if (dev->flags & IFF_PROMISC) {
2077c5aff182SThomas Petazzoni 		/* Accept all: Multicast + Unicast */
2078c5aff182SThomas Petazzoni 		mvneta_rx_unicast_promisc_set(pp, 1);
2079c5aff182SThomas Petazzoni 		mvneta_set_ucast_table(pp, rxq_def);
2080c5aff182SThomas Petazzoni 		mvneta_set_special_mcast_table(pp, rxq_def);
2081c5aff182SThomas Petazzoni 		mvneta_set_other_mcast_table(pp, rxq_def);
2082c5aff182SThomas Petazzoni 	} else {
2083c5aff182SThomas Petazzoni 		/* Accept single Unicast */
2084c5aff182SThomas Petazzoni 		mvneta_rx_unicast_promisc_set(pp, 0);
2085c5aff182SThomas Petazzoni 		mvneta_set_ucast_table(pp, -1);
2086c5aff182SThomas Petazzoni 		mvneta_mac_addr_set(pp, dev->dev_addr, rxq_def);
2087c5aff182SThomas Petazzoni 
2088c5aff182SThomas Petazzoni 		if (dev->flags & IFF_ALLMULTI) {
2089c5aff182SThomas Petazzoni 			/* Accept all multicast */
2090c5aff182SThomas Petazzoni 			mvneta_set_special_mcast_table(pp, rxq_def);
2091c5aff182SThomas Petazzoni 			mvneta_set_other_mcast_table(pp, rxq_def);
2092c5aff182SThomas Petazzoni 		} else {
2093c5aff182SThomas Petazzoni 			/* Accept only initialized multicast */
2094c5aff182SThomas Petazzoni 			mvneta_set_special_mcast_table(pp, -1);
2095c5aff182SThomas Petazzoni 			mvneta_set_other_mcast_table(pp, -1);
2096c5aff182SThomas Petazzoni 
2097c5aff182SThomas Petazzoni 			if (!netdev_mc_empty(dev)) {
2098c5aff182SThomas Petazzoni 				netdev_for_each_mc_addr(ha, dev) {
2099c5aff182SThomas Petazzoni 					mvneta_mcast_addr_set(pp, ha->addr,
2100c5aff182SThomas Petazzoni 							      rxq_def);
2101c5aff182SThomas Petazzoni 				}
2102c5aff182SThomas Petazzoni 			}
2103c5aff182SThomas Petazzoni 		}
2104c5aff182SThomas Petazzoni 	}
2105c5aff182SThomas Petazzoni }
2106c5aff182SThomas Petazzoni 
2107c5aff182SThomas Petazzoni /* Interrupt handling - the callback for request_irq() */
2108c5aff182SThomas Petazzoni static irqreturn_t mvneta_isr(int irq, void *dev_id)
2109c5aff182SThomas Petazzoni {
211012bb03b4SMaxime Ripard 	struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
2111c5aff182SThomas Petazzoni 
211212bb03b4SMaxime Ripard 	disable_percpu_irq(port->pp->dev->irq);
211312bb03b4SMaxime Ripard 	napi_schedule(&port->napi);
2114c5aff182SThomas Petazzoni 
2115c5aff182SThomas Petazzoni 	return IRQ_HANDLED;
2116c5aff182SThomas Petazzoni }
2117c5aff182SThomas Petazzoni 
2118898b2970SStas Sergeev static int mvneta_fixed_link_update(struct mvneta_port *pp,
2119898b2970SStas Sergeev 				    struct phy_device *phy)
2120898b2970SStas Sergeev {
2121898b2970SStas Sergeev 	struct fixed_phy_status status;
2122898b2970SStas Sergeev 	struct fixed_phy_status changed = {};
2123898b2970SStas Sergeev 	u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2124898b2970SStas Sergeev 
2125898b2970SStas Sergeev 	status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2126898b2970SStas Sergeev 	if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2127898b2970SStas Sergeev 		status.speed = SPEED_1000;
2128898b2970SStas Sergeev 	else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2129898b2970SStas Sergeev 		status.speed = SPEED_100;
2130898b2970SStas Sergeev 	else
2131898b2970SStas Sergeev 		status.speed = SPEED_10;
2132898b2970SStas Sergeev 	status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2133898b2970SStas Sergeev 	changed.link = 1;
2134898b2970SStas Sergeev 	changed.speed = 1;
2135898b2970SStas Sergeev 	changed.duplex = 1;
2136898b2970SStas Sergeev 	fixed_phy_update_state(phy, &status, &changed);
2137898b2970SStas Sergeev 	return 0;
2138898b2970SStas Sergeev }
2139898b2970SStas Sergeev 
2140c5aff182SThomas Petazzoni /* NAPI handler
2141c5aff182SThomas Petazzoni  * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2142c5aff182SThomas Petazzoni  * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2143c5aff182SThomas Petazzoni  * Bits 8 -15 of the cause Rx Tx register indicate that are received
2144c5aff182SThomas Petazzoni  * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2145c5aff182SThomas Petazzoni  * Each CPU has its own causeRxTx register
2146c5aff182SThomas Petazzoni  */
2147c5aff182SThomas Petazzoni static int mvneta_poll(struct napi_struct *napi, int budget)
2148c5aff182SThomas Petazzoni {
2149c5aff182SThomas Petazzoni 	int rx_done = 0;
2150c5aff182SThomas Petazzoni 	u32 cause_rx_tx;
2151c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(napi->dev);
215212bb03b4SMaxime Ripard 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2153c5aff182SThomas Petazzoni 
2154c5aff182SThomas Petazzoni 	if (!netif_running(pp->dev)) {
215512bb03b4SMaxime Ripard 		napi_complete(&port->napi);
2156c5aff182SThomas Petazzoni 		return rx_done;
2157c5aff182SThomas Petazzoni 	}
2158c5aff182SThomas Petazzoni 
2159c5aff182SThomas Petazzoni 	/* Read cause register */
2160898b2970SStas Sergeev 	cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2161898b2970SStas Sergeev 	if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2162898b2970SStas Sergeev 		u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2163898b2970SStas Sergeev 
2164898b2970SStas Sergeev 		mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2165898b2970SStas Sergeev 		if (pp->use_inband_status && (cause_misc &
2166898b2970SStas Sergeev 				(MVNETA_CAUSE_PHY_STATUS_CHANGE |
2167898b2970SStas Sergeev 				 MVNETA_CAUSE_LINK_CHANGE |
2168898b2970SStas Sergeev 				 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
2169898b2970SStas Sergeev 			mvneta_fixed_link_update(pp, pp->phy_dev);
2170898b2970SStas Sergeev 		}
2171898b2970SStas Sergeev 	}
217271f6d1b3Swilly tarreau 
217371f6d1b3Swilly tarreau 	/* Release Tx descriptors */
217471f6d1b3Swilly tarreau 	if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
21750713a86aSArnaud Ebalard 		mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
217671f6d1b3Swilly tarreau 		cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
217771f6d1b3Swilly tarreau 	}
2178c5aff182SThomas Petazzoni 
21796a20c175SThomas Petazzoni 	/* For the case where the last mvneta_poll did not process all
2180c5aff182SThomas Petazzoni 	 * RX packets
2181c5aff182SThomas Petazzoni 	 */
218212bb03b4SMaxime Ripard 	cause_rx_tx |= port->cause_rx_tx;
2183c5aff182SThomas Petazzoni 	rx_done = mvneta_rx(pp, budget, &pp->rxqs[rxq_def]);
2184c5aff182SThomas Petazzoni 	budget -= rx_done;
2185c5aff182SThomas Petazzoni 
2186c5aff182SThomas Petazzoni 	if (budget > 0) {
2187c5aff182SThomas Petazzoni 		cause_rx_tx = 0;
218812bb03b4SMaxime Ripard 		napi_complete(&port->napi);
218912bb03b4SMaxime Ripard 		enable_percpu_irq(pp->dev->irq, 0);
2190c5aff182SThomas Petazzoni 	}
2191c5aff182SThomas Petazzoni 
219212bb03b4SMaxime Ripard 	port->cause_rx_tx = cause_rx_tx;
2193c5aff182SThomas Petazzoni 	return rx_done;
2194c5aff182SThomas Petazzoni }
2195c5aff182SThomas Petazzoni 
2196c5aff182SThomas Petazzoni /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2197c5aff182SThomas Petazzoni static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2198c5aff182SThomas Petazzoni 			   int num)
2199c5aff182SThomas Petazzoni {
2200c5aff182SThomas Petazzoni 	int i;
2201c5aff182SThomas Petazzoni 
2202c5aff182SThomas Petazzoni 	for (i = 0; i < num; i++) {
2203a1a65ab1Swilly tarreau 		memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2204a1a65ab1Swilly tarreau 		if (mvneta_rx_refill(pp, rxq->descs + i) != 0) {
2205a1a65ab1Swilly tarreau 			netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs  filled\n",
2206c5aff182SThomas Petazzoni 				__func__, rxq->id, i, num);
2207c5aff182SThomas Petazzoni 			break;
2208c5aff182SThomas Petazzoni 		}
2209c5aff182SThomas Petazzoni 	}
2210c5aff182SThomas Petazzoni 
2211c5aff182SThomas Petazzoni 	/* Add this number of RX descriptors as non occupied (ready to
22126a20c175SThomas Petazzoni 	 * get packets)
22136a20c175SThomas Petazzoni 	 */
2214c5aff182SThomas Petazzoni 	mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2215c5aff182SThomas Petazzoni 
2216c5aff182SThomas Petazzoni 	return i;
2217c5aff182SThomas Petazzoni }
2218c5aff182SThomas Petazzoni 
2219c5aff182SThomas Petazzoni /* Free all packets pending transmit from all TXQs and reset TX port */
2220c5aff182SThomas Petazzoni static void mvneta_tx_reset(struct mvneta_port *pp)
2221c5aff182SThomas Petazzoni {
2222c5aff182SThomas Petazzoni 	int queue;
2223c5aff182SThomas Petazzoni 
22249672850bSEzequiel Garcia 	/* free the skb's in the tx ring */
2225c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++)
2226c5aff182SThomas Petazzoni 		mvneta_txq_done_force(pp, &pp->txqs[queue]);
2227c5aff182SThomas Petazzoni 
2228c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2229c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2230c5aff182SThomas Petazzoni }
2231c5aff182SThomas Petazzoni 
2232c5aff182SThomas Petazzoni static void mvneta_rx_reset(struct mvneta_port *pp)
2233c5aff182SThomas Petazzoni {
2234c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2235c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2236c5aff182SThomas Petazzoni }
2237c5aff182SThomas Petazzoni 
2238c5aff182SThomas Petazzoni /* Rx/Tx queue initialization/cleanup methods */
2239c5aff182SThomas Petazzoni 
2240c5aff182SThomas Petazzoni /* Create a specified RX queue */
2241c5aff182SThomas Petazzoni static int mvneta_rxq_init(struct mvneta_port *pp,
2242c5aff182SThomas Petazzoni 			   struct mvneta_rx_queue *rxq)
2243c5aff182SThomas Petazzoni 
2244c5aff182SThomas Petazzoni {
2245c5aff182SThomas Petazzoni 	rxq->size = pp->rx_ring_size;
2246c5aff182SThomas Petazzoni 
2247c5aff182SThomas Petazzoni 	/* Allocate memory for RX descriptors */
2248c5aff182SThomas Petazzoni 	rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2249c5aff182SThomas Petazzoni 					rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2250c5aff182SThomas Petazzoni 					&rxq->descs_phys, GFP_KERNEL);
2251d0320f75SJoe Perches 	if (rxq->descs == NULL)
2252c5aff182SThomas Petazzoni 		return -ENOMEM;
2253c5aff182SThomas Petazzoni 
2254c5aff182SThomas Petazzoni 	BUG_ON(rxq->descs !=
2255c5aff182SThomas Petazzoni 	       PTR_ALIGN(rxq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2256c5aff182SThomas Petazzoni 
2257c5aff182SThomas Petazzoni 	rxq->last_desc = rxq->size - 1;
2258c5aff182SThomas Petazzoni 
2259c5aff182SThomas Petazzoni 	/* Set Rx descriptors queue starting address */
2260c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2261c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2262c5aff182SThomas Petazzoni 
2263c5aff182SThomas Petazzoni 	/* Set Offset */
2264c5aff182SThomas Petazzoni 	mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD);
2265c5aff182SThomas Petazzoni 
2266c5aff182SThomas Petazzoni 	/* Set coalescing pkts and time */
2267c5aff182SThomas Petazzoni 	mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2268c5aff182SThomas Petazzoni 	mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2269c5aff182SThomas Petazzoni 
2270c5aff182SThomas Petazzoni 	/* Fill RXQ with buffers from RX pool */
2271c5aff182SThomas Petazzoni 	mvneta_rxq_buf_size_set(pp, rxq, MVNETA_RX_BUF_SIZE(pp->pkt_size));
2272c5aff182SThomas Petazzoni 	mvneta_rxq_bm_disable(pp, rxq);
2273c5aff182SThomas Petazzoni 	mvneta_rxq_fill(pp, rxq, rxq->size);
2274c5aff182SThomas Petazzoni 
2275c5aff182SThomas Petazzoni 	return 0;
2276c5aff182SThomas Petazzoni }
2277c5aff182SThomas Petazzoni 
2278c5aff182SThomas Petazzoni /* Cleanup Rx queue */
2279c5aff182SThomas Petazzoni static void mvneta_rxq_deinit(struct mvneta_port *pp,
2280c5aff182SThomas Petazzoni 			      struct mvneta_rx_queue *rxq)
2281c5aff182SThomas Petazzoni {
2282c5aff182SThomas Petazzoni 	mvneta_rxq_drop_pkts(pp, rxq);
2283c5aff182SThomas Petazzoni 
2284c5aff182SThomas Petazzoni 	if (rxq->descs)
2285c5aff182SThomas Petazzoni 		dma_free_coherent(pp->dev->dev.parent,
2286c5aff182SThomas Petazzoni 				  rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2287c5aff182SThomas Petazzoni 				  rxq->descs,
2288c5aff182SThomas Petazzoni 				  rxq->descs_phys);
2289c5aff182SThomas Petazzoni 
2290c5aff182SThomas Petazzoni 	rxq->descs             = NULL;
2291c5aff182SThomas Petazzoni 	rxq->last_desc         = 0;
2292c5aff182SThomas Petazzoni 	rxq->next_desc_to_proc = 0;
2293c5aff182SThomas Petazzoni 	rxq->descs_phys        = 0;
2294c5aff182SThomas Petazzoni }
2295c5aff182SThomas Petazzoni 
2296c5aff182SThomas Petazzoni /* Create and initialize a tx queue */
2297c5aff182SThomas Petazzoni static int mvneta_txq_init(struct mvneta_port *pp,
2298c5aff182SThomas Petazzoni 			   struct mvneta_tx_queue *txq)
2299c5aff182SThomas Petazzoni {
2300c5aff182SThomas Petazzoni 	txq->size = pp->tx_ring_size;
2301c5aff182SThomas Petazzoni 
23028eef5f97SEzequiel Garcia 	/* A queue must always have room for at least one skb.
23038eef5f97SEzequiel Garcia 	 * Therefore, stop the queue when the free entries reaches
23048eef5f97SEzequiel Garcia 	 * the maximum number of descriptors per skb.
23058eef5f97SEzequiel Garcia 	 */
23068eef5f97SEzequiel Garcia 	txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
23078eef5f97SEzequiel Garcia 	txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
23088eef5f97SEzequiel Garcia 
23098eef5f97SEzequiel Garcia 
2310c5aff182SThomas Petazzoni 	/* Allocate memory for TX descriptors */
2311c5aff182SThomas Petazzoni 	txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2312c5aff182SThomas Petazzoni 					txq->size * MVNETA_DESC_ALIGNED_SIZE,
2313c5aff182SThomas Petazzoni 					&txq->descs_phys, GFP_KERNEL);
2314d0320f75SJoe Perches 	if (txq->descs == NULL)
2315c5aff182SThomas Petazzoni 		return -ENOMEM;
2316c5aff182SThomas Petazzoni 
2317c5aff182SThomas Petazzoni 	/* Make sure descriptor address is cache line size aligned  */
2318c5aff182SThomas Petazzoni 	BUG_ON(txq->descs !=
2319c5aff182SThomas Petazzoni 	       PTR_ALIGN(txq->descs, MVNETA_CPU_D_CACHE_LINE_SIZE));
2320c5aff182SThomas Petazzoni 
2321c5aff182SThomas Petazzoni 	txq->last_desc = txq->size - 1;
2322c5aff182SThomas Petazzoni 
2323c5aff182SThomas Petazzoni 	/* Set maximum bandwidth for enabled TXQs */
2324c5aff182SThomas Petazzoni 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2325c5aff182SThomas Petazzoni 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2326c5aff182SThomas Petazzoni 
2327c5aff182SThomas Petazzoni 	/* Set Tx descriptors queue starting address */
2328c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2329c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2330c5aff182SThomas Petazzoni 
2331c5aff182SThomas Petazzoni 	txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2332c5aff182SThomas Petazzoni 	if (txq->tx_skb == NULL) {
2333c5aff182SThomas Petazzoni 		dma_free_coherent(pp->dev->dev.parent,
2334c5aff182SThomas Petazzoni 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
2335c5aff182SThomas Petazzoni 				  txq->descs, txq->descs_phys);
2336c5aff182SThomas Petazzoni 		return -ENOMEM;
2337c5aff182SThomas Petazzoni 	}
23382adb719dSEzequiel Garcia 
23392adb719dSEzequiel Garcia 	/* Allocate DMA buffers for TSO MAC/IP/TCP headers */
23402adb719dSEzequiel Garcia 	txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
23412adb719dSEzequiel Garcia 					   txq->size * TSO_HEADER_SIZE,
23422adb719dSEzequiel Garcia 					   &txq->tso_hdrs_phys, GFP_KERNEL);
23432adb719dSEzequiel Garcia 	if (txq->tso_hdrs == NULL) {
23442adb719dSEzequiel Garcia 		kfree(txq->tx_skb);
23452adb719dSEzequiel Garcia 		dma_free_coherent(pp->dev->dev.parent,
23462adb719dSEzequiel Garcia 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
23472adb719dSEzequiel Garcia 				  txq->descs, txq->descs_phys);
23482adb719dSEzequiel Garcia 		return -ENOMEM;
23492adb719dSEzequiel Garcia 	}
2350c5aff182SThomas Petazzoni 	mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2351c5aff182SThomas Petazzoni 
2352c5aff182SThomas Petazzoni 	return 0;
2353c5aff182SThomas Petazzoni }
2354c5aff182SThomas Petazzoni 
2355c5aff182SThomas Petazzoni /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2356c5aff182SThomas Petazzoni static void mvneta_txq_deinit(struct mvneta_port *pp,
2357c5aff182SThomas Petazzoni 			      struct mvneta_tx_queue *txq)
2358c5aff182SThomas Petazzoni {
2359c5aff182SThomas Petazzoni 	kfree(txq->tx_skb);
2360c5aff182SThomas Petazzoni 
23612adb719dSEzequiel Garcia 	if (txq->tso_hdrs)
23622adb719dSEzequiel Garcia 		dma_free_coherent(pp->dev->dev.parent,
23632adb719dSEzequiel Garcia 				  txq->size * TSO_HEADER_SIZE,
23642adb719dSEzequiel Garcia 				  txq->tso_hdrs, txq->tso_hdrs_phys);
2365c5aff182SThomas Petazzoni 	if (txq->descs)
2366c5aff182SThomas Petazzoni 		dma_free_coherent(pp->dev->dev.parent,
2367c5aff182SThomas Petazzoni 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
2368c5aff182SThomas Petazzoni 				  txq->descs, txq->descs_phys);
2369c5aff182SThomas Petazzoni 
2370c5aff182SThomas Petazzoni 	txq->descs             = NULL;
2371c5aff182SThomas Petazzoni 	txq->last_desc         = 0;
2372c5aff182SThomas Petazzoni 	txq->next_desc_to_proc = 0;
2373c5aff182SThomas Petazzoni 	txq->descs_phys        = 0;
2374c5aff182SThomas Petazzoni 
2375c5aff182SThomas Petazzoni 	/* Set minimum bandwidth for disabled TXQs */
2376c5aff182SThomas Petazzoni 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2377c5aff182SThomas Petazzoni 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2378c5aff182SThomas Petazzoni 
2379c5aff182SThomas Petazzoni 	/* Set Tx descriptors queue starting address and size */
2380c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2381c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2382c5aff182SThomas Petazzoni }
2383c5aff182SThomas Petazzoni 
2384c5aff182SThomas Petazzoni /* Cleanup all Tx queues */
2385c5aff182SThomas Petazzoni static void mvneta_cleanup_txqs(struct mvneta_port *pp)
2386c5aff182SThomas Petazzoni {
2387c5aff182SThomas Petazzoni 	int queue;
2388c5aff182SThomas Petazzoni 
2389c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++)
2390c5aff182SThomas Petazzoni 		mvneta_txq_deinit(pp, &pp->txqs[queue]);
2391c5aff182SThomas Petazzoni }
2392c5aff182SThomas Petazzoni 
2393c5aff182SThomas Petazzoni /* Cleanup all Rx queues */
2394c5aff182SThomas Petazzoni static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
2395c5aff182SThomas Petazzoni {
2396d8936657SMaxime Ripard 	mvneta_rxq_deinit(pp, &pp->rxqs[rxq_def]);
2397c5aff182SThomas Petazzoni }
2398c5aff182SThomas Petazzoni 
2399c5aff182SThomas Petazzoni 
2400c5aff182SThomas Petazzoni /* Init all Rx queues */
2401c5aff182SThomas Petazzoni static int mvneta_setup_rxqs(struct mvneta_port *pp)
2402c5aff182SThomas Petazzoni {
2403d8936657SMaxime Ripard 	int err = mvneta_rxq_init(pp, &pp->rxqs[rxq_def]);
2404c5aff182SThomas Petazzoni 	if (err) {
2405c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "%s: can't create rxq=%d\n",
2406d8936657SMaxime Ripard 			   __func__, rxq_def);
2407c5aff182SThomas Petazzoni 		mvneta_cleanup_rxqs(pp);
2408c5aff182SThomas Petazzoni 		return err;
2409c5aff182SThomas Petazzoni 	}
2410c5aff182SThomas Petazzoni 
2411c5aff182SThomas Petazzoni 	return 0;
2412c5aff182SThomas Petazzoni }
2413c5aff182SThomas Petazzoni 
2414c5aff182SThomas Petazzoni /* Init all tx queues */
2415c5aff182SThomas Petazzoni static int mvneta_setup_txqs(struct mvneta_port *pp)
2416c5aff182SThomas Petazzoni {
2417c5aff182SThomas Petazzoni 	int queue;
2418c5aff182SThomas Petazzoni 
2419c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
2420c5aff182SThomas Petazzoni 		int err = mvneta_txq_init(pp, &pp->txqs[queue]);
2421c5aff182SThomas Petazzoni 		if (err) {
2422c5aff182SThomas Petazzoni 			netdev_err(pp->dev, "%s: can't create txq=%d\n",
2423c5aff182SThomas Petazzoni 				   __func__, queue);
2424c5aff182SThomas Petazzoni 			mvneta_cleanup_txqs(pp);
2425c5aff182SThomas Petazzoni 			return err;
2426c5aff182SThomas Petazzoni 		}
2427c5aff182SThomas Petazzoni 	}
2428c5aff182SThomas Petazzoni 
2429c5aff182SThomas Petazzoni 	return 0;
2430c5aff182SThomas Petazzoni }
2431c5aff182SThomas Petazzoni 
2432c5aff182SThomas Petazzoni static void mvneta_start_dev(struct mvneta_port *pp)
2433c5aff182SThomas Petazzoni {
243412bb03b4SMaxime Ripard 	unsigned int cpu;
243512bb03b4SMaxime Ripard 
2436c5aff182SThomas Petazzoni 	mvneta_max_rx_size_set(pp, pp->pkt_size);
2437c5aff182SThomas Petazzoni 	mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
2438c5aff182SThomas Petazzoni 
2439c5aff182SThomas Petazzoni 	/* start the Rx/Tx activity */
2440c5aff182SThomas Petazzoni 	mvneta_port_enable(pp);
2441c5aff182SThomas Petazzoni 
2442c5aff182SThomas Petazzoni 	/* Enable polling on the port */
244312bb03b4SMaxime Ripard 	for_each_present_cpu(cpu) {
244412bb03b4SMaxime Ripard 		struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
244512bb03b4SMaxime Ripard 
244612bb03b4SMaxime Ripard 		napi_enable(&port->napi);
244712bb03b4SMaxime Ripard 	}
2448c5aff182SThomas Petazzoni 
2449c5aff182SThomas Petazzoni 	/* Unmask interrupts */
2450c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2451898b2970SStas Sergeev 		    MVNETA_RX_INTR_MASK(rxq_number) |
2452898b2970SStas Sergeev 		    MVNETA_TX_INTR_MASK(txq_number) |
2453898b2970SStas Sergeev 		    MVNETA_MISCINTR_INTR_MASK);
2454898b2970SStas Sergeev 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2455898b2970SStas Sergeev 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
2456898b2970SStas Sergeev 		    MVNETA_CAUSE_LINK_CHANGE |
2457898b2970SStas Sergeev 		    MVNETA_CAUSE_PSC_SYNC_CHANGE);
2458c5aff182SThomas Petazzoni 
2459c5aff182SThomas Petazzoni 	phy_start(pp->phy_dev);
2460c5aff182SThomas Petazzoni 	netif_tx_start_all_queues(pp->dev);
2461c5aff182SThomas Petazzoni }
2462c5aff182SThomas Petazzoni 
2463c5aff182SThomas Petazzoni static void mvneta_stop_dev(struct mvneta_port *pp)
2464c5aff182SThomas Petazzoni {
246512bb03b4SMaxime Ripard 	unsigned int cpu;
246612bb03b4SMaxime Ripard 
2467c5aff182SThomas Petazzoni 	phy_stop(pp->phy_dev);
2468c5aff182SThomas Petazzoni 
246912bb03b4SMaxime Ripard 	for_each_present_cpu(cpu) {
247012bb03b4SMaxime Ripard 		struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
247112bb03b4SMaxime Ripard 
247212bb03b4SMaxime Ripard 		napi_disable(&port->napi);
247312bb03b4SMaxime Ripard 	}
2474c5aff182SThomas Petazzoni 
2475c5aff182SThomas Petazzoni 	netif_carrier_off(pp->dev);
2476c5aff182SThomas Petazzoni 
2477c5aff182SThomas Petazzoni 	mvneta_port_down(pp);
2478c5aff182SThomas Petazzoni 	netif_tx_stop_all_queues(pp->dev);
2479c5aff182SThomas Petazzoni 
2480c5aff182SThomas Petazzoni 	/* Stop the port activity */
2481c5aff182SThomas Petazzoni 	mvneta_port_disable(pp);
2482c5aff182SThomas Petazzoni 
2483c5aff182SThomas Petazzoni 	/* Clear all ethernet port interrupts */
2484c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2485c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
2486c5aff182SThomas Petazzoni 
2487c5aff182SThomas Petazzoni 	/* Mask all ethernet port interrupts */
2488c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2489c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2490c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2491c5aff182SThomas Petazzoni 
2492c5aff182SThomas Petazzoni 	mvneta_tx_reset(pp);
2493c5aff182SThomas Petazzoni 	mvneta_rx_reset(pp);
2494c5aff182SThomas Petazzoni }
2495c5aff182SThomas Petazzoni 
2496c5aff182SThomas Petazzoni /* Return positive if MTU is valid */
2497c5aff182SThomas Petazzoni static int mvneta_check_mtu_valid(struct net_device *dev, int mtu)
2498c5aff182SThomas Petazzoni {
2499c5aff182SThomas Petazzoni 	if (mtu < 68) {
2500c5aff182SThomas Petazzoni 		netdev_err(dev, "cannot change mtu to less than 68\n");
2501c5aff182SThomas Petazzoni 		return -EINVAL;
2502c5aff182SThomas Petazzoni 	}
2503c5aff182SThomas Petazzoni 
2504c5aff182SThomas Petazzoni 	/* 9676 == 9700 - 20 and rounding to 8 */
2505c5aff182SThomas Petazzoni 	if (mtu > 9676) {
2506c5aff182SThomas Petazzoni 		netdev_info(dev, "Illegal MTU value %d, round to 9676\n", mtu);
2507c5aff182SThomas Petazzoni 		mtu = 9676;
2508c5aff182SThomas Petazzoni 	}
2509c5aff182SThomas Petazzoni 
2510c5aff182SThomas Petazzoni 	if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
2511c5aff182SThomas Petazzoni 		netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
2512c5aff182SThomas Petazzoni 			mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
2513c5aff182SThomas Petazzoni 		mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
2514c5aff182SThomas Petazzoni 	}
2515c5aff182SThomas Petazzoni 
2516c5aff182SThomas Petazzoni 	return mtu;
2517c5aff182SThomas Petazzoni }
2518c5aff182SThomas Petazzoni 
2519c5aff182SThomas Petazzoni /* Change the device mtu */
2520c5aff182SThomas Petazzoni static int mvneta_change_mtu(struct net_device *dev, int mtu)
2521c5aff182SThomas Petazzoni {
2522c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2523c5aff182SThomas Petazzoni 	int ret;
2524c5aff182SThomas Petazzoni 
2525c5aff182SThomas Petazzoni 	mtu = mvneta_check_mtu_valid(dev, mtu);
2526c5aff182SThomas Petazzoni 	if (mtu < 0)
2527c5aff182SThomas Petazzoni 		return -EINVAL;
2528c5aff182SThomas Petazzoni 
2529c5aff182SThomas Petazzoni 	dev->mtu = mtu;
2530c5aff182SThomas Petazzoni 
2531b65657fcSSimon Guinot 	if (!netif_running(dev)) {
2532b65657fcSSimon Guinot 		netdev_update_features(dev);
2533c5aff182SThomas Petazzoni 		return 0;
2534b65657fcSSimon Guinot 	}
2535c5aff182SThomas Petazzoni 
25366a20c175SThomas Petazzoni 	/* The interface is running, so we have to force a
2537a92dbd96SEzequiel Garcia 	 * reallocation of the queues
2538c5aff182SThomas Petazzoni 	 */
2539c5aff182SThomas Petazzoni 	mvneta_stop_dev(pp);
2540c5aff182SThomas Petazzoni 
2541c5aff182SThomas Petazzoni 	mvneta_cleanup_txqs(pp);
2542c5aff182SThomas Petazzoni 	mvneta_cleanup_rxqs(pp);
2543c5aff182SThomas Petazzoni 
2544a92dbd96SEzequiel Garcia 	pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
25458ec2cd48Swilly tarreau 	pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
25468ec2cd48Swilly tarreau 	                SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2547c5aff182SThomas Petazzoni 
2548c5aff182SThomas Petazzoni 	ret = mvneta_setup_rxqs(pp);
2549c5aff182SThomas Petazzoni 	if (ret) {
2550a92dbd96SEzequiel Garcia 		netdev_err(dev, "unable to setup rxqs after MTU change\n");
2551c5aff182SThomas Petazzoni 		return ret;
2552c5aff182SThomas Petazzoni 	}
2553c5aff182SThomas Petazzoni 
2554a92dbd96SEzequiel Garcia 	ret = mvneta_setup_txqs(pp);
2555a92dbd96SEzequiel Garcia 	if (ret) {
2556a92dbd96SEzequiel Garcia 		netdev_err(dev, "unable to setup txqs after MTU change\n");
2557a92dbd96SEzequiel Garcia 		return ret;
2558a92dbd96SEzequiel Garcia 	}
2559c5aff182SThomas Petazzoni 
2560c5aff182SThomas Petazzoni 	mvneta_start_dev(pp);
2561c5aff182SThomas Petazzoni 	mvneta_port_up(pp);
2562c5aff182SThomas Petazzoni 
2563b65657fcSSimon Guinot 	netdev_update_features(dev);
2564b65657fcSSimon Guinot 
2565c5aff182SThomas Petazzoni 	return 0;
2566c5aff182SThomas Petazzoni }
2567c5aff182SThomas Petazzoni 
2568b65657fcSSimon Guinot static netdev_features_t mvneta_fix_features(struct net_device *dev,
2569b65657fcSSimon Guinot 					     netdev_features_t features)
2570b65657fcSSimon Guinot {
2571b65657fcSSimon Guinot 	struct mvneta_port *pp = netdev_priv(dev);
2572b65657fcSSimon Guinot 
2573b65657fcSSimon Guinot 	if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
2574b65657fcSSimon Guinot 		features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
2575b65657fcSSimon Guinot 		netdev_info(dev,
2576b65657fcSSimon Guinot 			    "Disable IP checksum for MTU greater than %dB\n",
2577b65657fcSSimon Guinot 			    pp->tx_csum_limit);
2578b65657fcSSimon Guinot 	}
2579b65657fcSSimon Guinot 
2580b65657fcSSimon Guinot 	return features;
2581b65657fcSSimon Guinot }
2582b65657fcSSimon Guinot 
25838cc3e439SThomas Petazzoni /* Get mac address */
25848cc3e439SThomas Petazzoni static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
25858cc3e439SThomas Petazzoni {
25868cc3e439SThomas Petazzoni 	u32 mac_addr_l, mac_addr_h;
25878cc3e439SThomas Petazzoni 
25888cc3e439SThomas Petazzoni 	mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
25898cc3e439SThomas Petazzoni 	mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
25908cc3e439SThomas Petazzoni 	addr[0] = (mac_addr_h >> 24) & 0xFF;
25918cc3e439SThomas Petazzoni 	addr[1] = (mac_addr_h >> 16) & 0xFF;
25928cc3e439SThomas Petazzoni 	addr[2] = (mac_addr_h >> 8) & 0xFF;
25938cc3e439SThomas Petazzoni 	addr[3] = mac_addr_h & 0xFF;
25948cc3e439SThomas Petazzoni 	addr[4] = (mac_addr_l >> 8) & 0xFF;
25958cc3e439SThomas Petazzoni 	addr[5] = mac_addr_l & 0xFF;
25968cc3e439SThomas Petazzoni }
25978cc3e439SThomas Petazzoni 
2598c5aff182SThomas Petazzoni /* Handle setting mac address */
2599c5aff182SThomas Petazzoni static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
2600c5aff182SThomas Petazzoni {
2601c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2602e68de360SEzequiel Garcia 	struct sockaddr *sockaddr = addr;
2603e68de360SEzequiel Garcia 	int ret;
2604c5aff182SThomas Petazzoni 
2605e68de360SEzequiel Garcia 	ret = eth_prepare_mac_addr_change(dev, addr);
2606e68de360SEzequiel Garcia 	if (ret < 0)
2607e68de360SEzequiel Garcia 		return ret;
2608c5aff182SThomas Petazzoni 	/* Remove previous address table entry */
2609c5aff182SThomas Petazzoni 	mvneta_mac_addr_set(pp, dev->dev_addr, -1);
2610c5aff182SThomas Petazzoni 
2611c5aff182SThomas Petazzoni 	/* Set new addr in hw */
2612e68de360SEzequiel Garcia 	mvneta_mac_addr_set(pp, sockaddr->sa_data, rxq_def);
2613c5aff182SThomas Petazzoni 
2614e68de360SEzequiel Garcia 	eth_commit_mac_addr_change(dev, addr);
2615c5aff182SThomas Petazzoni 	return 0;
2616c5aff182SThomas Petazzoni }
2617c5aff182SThomas Petazzoni 
2618c5aff182SThomas Petazzoni static void mvneta_adjust_link(struct net_device *ndev)
2619c5aff182SThomas Petazzoni {
2620c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(ndev);
2621c5aff182SThomas Petazzoni 	struct phy_device *phydev = pp->phy_dev;
2622c5aff182SThomas Petazzoni 	int status_change = 0;
2623c5aff182SThomas Petazzoni 
2624c5aff182SThomas Petazzoni 	if (phydev->link) {
2625c5aff182SThomas Petazzoni 		if ((pp->speed != phydev->speed) ||
2626c5aff182SThomas Petazzoni 		    (pp->duplex != phydev->duplex)) {
2627c5aff182SThomas Petazzoni 			u32 val;
2628c5aff182SThomas Petazzoni 
2629c5aff182SThomas Petazzoni 			val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
2630c5aff182SThomas Petazzoni 			val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
2631c5aff182SThomas Petazzoni 				 MVNETA_GMAC_CONFIG_GMII_SPEED |
2632898b2970SStas Sergeev 				 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
2633c5aff182SThomas Petazzoni 
2634c5aff182SThomas Petazzoni 			if (phydev->duplex)
2635c5aff182SThomas Petazzoni 				val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
2636c5aff182SThomas Petazzoni 
2637c5aff182SThomas Petazzoni 			if (phydev->speed == SPEED_1000)
2638c5aff182SThomas Petazzoni 				val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
26394d12bc63SThomas Petazzoni 			else if (phydev->speed == SPEED_100)
2640c5aff182SThomas Petazzoni 				val |= MVNETA_GMAC_CONFIG_MII_SPEED;
2641c5aff182SThomas Petazzoni 
2642c5aff182SThomas Petazzoni 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
2643c5aff182SThomas Petazzoni 
2644c5aff182SThomas Petazzoni 			pp->duplex = phydev->duplex;
2645c5aff182SThomas Petazzoni 			pp->speed  = phydev->speed;
2646c5aff182SThomas Petazzoni 		}
2647c5aff182SThomas Petazzoni 	}
2648c5aff182SThomas Petazzoni 
2649c5aff182SThomas Petazzoni 	if (phydev->link != pp->link) {
2650c5aff182SThomas Petazzoni 		if (!phydev->link) {
2651c5aff182SThomas Petazzoni 			pp->duplex = -1;
2652c5aff182SThomas Petazzoni 			pp->speed = 0;
2653c5aff182SThomas Petazzoni 		}
2654c5aff182SThomas Petazzoni 
2655c5aff182SThomas Petazzoni 		pp->link = phydev->link;
2656c5aff182SThomas Petazzoni 		status_change = 1;
2657c5aff182SThomas Petazzoni 	}
2658c5aff182SThomas Petazzoni 
2659c5aff182SThomas Petazzoni 	if (status_change) {
2660c5aff182SThomas Petazzoni 		if (phydev->link) {
2661898b2970SStas Sergeev 			if (!pp->use_inband_status) {
2662898b2970SStas Sergeev 				u32 val = mvreg_read(pp,
2663898b2970SStas Sergeev 						  MVNETA_GMAC_AUTONEG_CONFIG);
2664898b2970SStas Sergeev 				val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
2665898b2970SStas Sergeev 				val |= MVNETA_GMAC_FORCE_LINK_PASS;
2666898b2970SStas Sergeev 				mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
2667898b2970SStas Sergeev 					    val);
2668898b2970SStas Sergeev 			}
2669c5aff182SThomas Petazzoni 			mvneta_port_up(pp);
2670c5aff182SThomas Petazzoni 		} else {
2671898b2970SStas Sergeev 			if (!pp->use_inband_status) {
2672898b2970SStas Sergeev 				u32 val = mvreg_read(pp,
2673898b2970SStas Sergeev 						  MVNETA_GMAC_AUTONEG_CONFIG);
2674898b2970SStas Sergeev 				val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
2675898b2970SStas Sergeev 				val |= MVNETA_GMAC_FORCE_LINK_DOWN;
2676898b2970SStas Sergeev 				mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
2677898b2970SStas Sergeev 					    val);
2678898b2970SStas Sergeev 			}
2679c5aff182SThomas Petazzoni 			mvneta_port_down(pp);
2680c5aff182SThomas Petazzoni 		}
26810089b745SEzequiel Garcia 		phy_print_status(phydev);
2682c5aff182SThomas Petazzoni 	}
2683c5aff182SThomas Petazzoni }
2684c5aff182SThomas Petazzoni 
2685c5aff182SThomas Petazzoni static int mvneta_mdio_probe(struct mvneta_port *pp)
2686c5aff182SThomas Petazzoni {
2687c5aff182SThomas Petazzoni 	struct phy_device *phy_dev;
2688c5aff182SThomas Petazzoni 
2689c5aff182SThomas Petazzoni 	phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
2690c5aff182SThomas Petazzoni 				 pp->phy_interface);
2691c5aff182SThomas Petazzoni 	if (!phy_dev) {
2692c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "could not find the PHY\n");
2693c5aff182SThomas Petazzoni 		return -ENODEV;
2694c5aff182SThomas Petazzoni 	}
2695c5aff182SThomas Petazzoni 
2696c5aff182SThomas Petazzoni 	phy_dev->supported &= PHY_GBIT_FEATURES;
2697c5aff182SThomas Petazzoni 	phy_dev->advertising = phy_dev->supported;
2698c5aff182SThomas Petazzoni 
2699c5aff182SThomas Petazzoni 	pp->phy_dev = phy_dev;
2700c5aff182SThomas Petazzoni 	pp->link    = 0;
2701c5aff182SThomas Petazzoni 	pp->duplex  = 0;
2702c5aff182SThomas Petazzoni 	pp->speed   = 0;
2703c5aff182SThomas Petazzoni 
2704c5aff182SThomas Petazzoni 	return 0;
2705c5aff182SThomas Petazzoni }
2706c5aff182SThomas Petazzoni 
2707c5aff182SThomas Petazzoni static void mvneta_mdio_remove(struct mvneta_port *pp)
2708c5aff182SThomas Petazzoni {
2709c5aff182SThomas Petazzoni 	phy_disconnect(pp->phy_dev);
2710c5aff182SThomas Petazzoni 	pp->phy_dev = NULL;
2711c5aff182SThomas Petazzoni }
2712c5aff182SThomas Petazzoni 
2713f8642885SMaxime Ripard static void mvneta_percpu_enable(void *arg)
2714f8642885SMaxime Ripard {
2715f8642885SMaxime Ripard 	struct mvneta_port *pp = arg;
2716f8642885SMaxime Ripard 
2717f8642885SMaxime Ripard 	enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
2718f8642885SMaxime Ripard }
2719f8642885SMaxime Ripard 
2720f8642885SMaxime Ripard static void mvneta_percpu_disable(void *arg)
2721f8642885SMaxime Ripard {
2722f8642885SMaxime Ripard 	struct mvneta_port *pp = arg;
2723f8642885SMaxime Ripard 
2724f8642885SMaxime Ripard 	disable_percpu_irq(pp->dev->irq);
2725f8642885SMaxime Ripard }
2726f8642885SMaxime Ripard 
2727f8642885SMaxime Ripard static void mvneta_percpu_elect(struct mvneta_port *pp)
2728f8642885SMaxime Ripard {
2729f8642885SMaxime Ripard 	int online_cpu_idx, cpu, i = 0;
2730f8642885SMaxime Ripard 
2731f8642885SMaxime Ripard 	online_cpu_idx = rxq_def % num_online_cpus();
2732f8642885SMaxime Ripard 
2733f8642885SMaxime Ripard 	for_each_online_cpu(cpu) {
2734f8642885SMaxime Ripard 		if (i == online_cpu_idx)
2735f8642885SMaxime Ripard 			/* Enable per-CPU interrupt on the one CPU we
2736f8642885SMaxime Ripard 			 * just elected
2737f8642885SMaxime Ripard 			 */
2738f8642885SMaxime Ripard 			smp_call_function_single(cpu, mvneta_percpu_enable,
2739f8642885SMaxime Ripard 						pp, true);
2740f8642885SMaxime Ripard 		else
2741f8642885SMaxime Ripard 			/* Disable per-CPU interrupt on all the other CPU */
2742f8642885SMaxime Ripard 			smp_call_function_single(cpu, mvneta_percpu_disable,
2743f8642885SMaxime Ripard 						pp, true);
2744f8642885SMaxime Ripard 		i++;
2745f8642885SMaxime Ripard 	}
2746f8642885SMaxime Ripard };
2747f8642885SMaxime Ripard 
2748f8642885SMaxime Ripard static int mvneta_percpu_notifier(struct notifier_block *nfb,
2749f8642885SMaxime Ripard 				  unsigned long action, void *hcpu)
2750f8642885SMaxime Ripard {
2751f8642885SMaxime Ripard 	struct mvneta_port *pp = container_of(nfb, struct mvneta_port,
2752f8642885SMaxime Ripard 					      cpu_notifier);
2753f8642885SMaxime Ripard 	int cpu = (unsigned long)hcpu, other_cpu;
2754f8642885SMaxime Ripard 	struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
2755f8642885SMaxime Ripard 
2756f8642885SMaxime Ripard 	switch (action) {
2757f8642885SMaxime Ripard 	case CPU_ONLINE:
2758f8642885SMaxime Ripard 	case CPU_ONLINE_FROZEN:
2759f8642885SMaxime Ripard 		netif_tx_stop_all_queues(pp->dev);
2760f8642885SMaxime Ripard 
2761f8642885SMaxime Ripard 		/* We have to synchronise on tha napi of each CPU
2762f8642885SMaxime Ripard 		 * except the one just being waked up
2763f8642885SMaxime Ripard 		 */
2764f8642885SMaxime Ripard 		for_each_online_cpu(other_cpu) {
2765f8642885SMaxime Ripard 			if (other_cpu != cpu) {
2766f8642885SMaxime Ripard 				struct mvneta_pcpu_port *other_port =
2767f8642885SMaxime Ripard 					per_cpu_ptr(pp->ports, other_cpu);
2768f8642885SMaxime Ripard 
2769f8642885SMaxime Ripard 				napi_synchronize(&other_port->napi);
2770f8642885SMaxime Ripard 			}
2771f8642885SMaxime Ripard 		}
2772f8642885SMaxime Ripard 
2773f8642885SMaxime Ripard 		/* Mask all ethernet port interrupts */
2774f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2775f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2776f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2777f8642885SMaxime Ripard 		napi_enable(&port->napi);
2778f8642885SMaxime Ripard 
2779f8642885SMaxime Ripard 		/* Enable per-CPU interrupt on the one CPU we care
2780f8642885SMaxime Ripard 		 * about.
2781f8642885SMaxime Ripard 		 */
2782f8642885SMaxime Ripard 		mvneta_percpu_elect(pp);
2783f8642885SMaxime Ripard 
2784f8642885SMaxime Ripard 		/* Unmask all ethernet port interrupts */
2785f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2786f8642885SMaxime Ripard 			MVNETA_RX_INTR_MASK(rxq_number) |
2787f8642885SMaxime Ripard 			MVNETA_TX_INTR_MASK(txq_number) |
2788f8642885SMaxime Ripard 			MVNETA_MISCINTR_INTR_MASK);
2789f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2790f8642885SMaxime Ripard 			MVNETA_CAUSE_PHY_STATUS_CHANGE |
2791f8642885SMaxime Ripard 			MVNETA_CAUSE_LINK_CHANGE |
2792f8642885SMaxime Ripard 			MVNETA_CAUSE_PSC_SYNC_CHANGE);
2793f8642885SMaxime Ripard 		netif_tx_start_all_queues(pp->dev);
2794f8642885SMaxime Ripard 		break;
2795f8642885SMaxime Ripard 	case CPU_DOWN_PREPARE:
2796f8642885SMaxime Ripard 	case CPU_DOWN_PREPARE_FROZEN:
2797f8642885SMaxime Ripard 		netif_tx_stop_all_queues(pp->dev);
2798f8642885SMaxime Ripard 		/* Mask all ethernet port interrupts */
2799f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2800f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
2801f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
2802f8642885SMaxime Ripard 
2803f8642885SMaxime Ripard 		napi_synchronize(&port->napi);
2804f8642885SMaxime Ripard 		napi_disable(&port->napi);
2805f8642885SMaxime Ripard 		/* Disable per-CPU interrupts on the CPU that is
2806f8642885SMaxime Ripard 		 * brought down.
2807f8642885SMaxime Ripard 		 */
2808f8642885SMaxime Ripard 		smp_call_function_single(cpu, mvneta_percpu_disable,
2809f8642885SMaxime Ripard 					 pp, true);
2810f8642885SMaxime Ripard 
2811f8642885SMaxime Ripard 		break;
2812f8642885SMaxime Ripard 	case CPU_DEAD:
2813f8642885SMaxime Ripard 	case CPU_DEAD_FROZEN:
2814f8642885SMaxime Ripard 		/* Check if a new CPU must be elected now this on is down */
2815f8642885SMaxime Ripard 		mvneta_percpu_elect(pp);
2816f8642885SMaxime Ripard 		/* Unmask all ethernet port interrupts */
2817f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2818f8642885SMaxime Ripard 			MVNETA_RX_INTR_MASK(rxq_number) |
2819f8642885SMaxime Ripard 			MVNETA_TX_INTR_MASK(txq_number) |
2820f8642885SMaxime Ripard 			MVNETA_MISCINTR_INTR_MASK);
2821f8642885SMaxime Ripard 		mvreg_write(pp, MVNETA_INTR_MISC_MASK,
2822f8642885SMaxime Ripard 			MVNETA_CAUSE_PHY_STATUS_CHANGE |
2823f8642885SMaxime Ripard 			MVNETA_CAUSE_LINK_CHANGE |
2824f8642885SMaxime Ripard 			MVNETA_CAUSE_PSC_SYNC_CHANGE);
2825f8642885SMaxime Ripard 		netif_tx_start_all_queues(pp->dev);
2826f8642885SMaxime Ripard 		break;
2827f8642885SMaxime Ripard 	}
2828f8642885SMaxime Ripard 
2829f8642885SMaxime Ripard 	return NOTIFY_OK;
2830f8642885SMaxime Ripard }
2831f8642885SMaxime Ripard 
2832c5aff182SThomas Petazzoni static int mvneta_open(struct net_device *dev)
2833c5aff182SThomas Petazzoni {
2834c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2835c5aff182SThomas Petazzoni 	int ret;
2836c5aff182SThomas Petazzoni 
2837c5aff182SThomas Petazzoni 	pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
28388ec2cd48Swilly tarreau 	pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
28398ec2cd48Swilly tarreau 	                SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
2840c5aff182SThomas Petazzoni 
2841c5aff182SThomas Petazzoni 	ret = mvneta_setup_rxqs(pp);
2842c5aff182SThomas Petazzoni 	if (ret)
2843c5aff182SThomas Petazzoni 		return ret;
2844c5aff182SThomas Petazzoni 
2845c5aff182SThomas Petazzoni 	ret = mvneta_setup_txqs(pp);
2846c5aff182SThomas Petazzoni 	if (ret)
2847c5aff182SThomas Petazzoni 		goto err_cleanup_rxqs;
2848c5aff182SThomas Petazzoni 
2849c5aff182SThomas Petazzoni 	/* Connect to port interrupt line */
285012bb03b4SMaxime Ripard 	ret = request_percpu_irq(pp->dev->irq, mvneta_isr,
285112bb03b4SMaxime Ripard 				 MVNETA_DRIVER_NAME, pp->ports);
2852c5aff182SThomas Petazzoni 	if (ret) {
2853c5aff182SThomas Petazzoni 		netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
2854c5aff182SThomas Petazzoni 		goto err_cleanup_txqs;
2855c5aff182SThomas Petazzoni 	}
2856c5aff182SThomas Petazzoni 
2857f8642885SMaxime Ripard 	/* Even though the documentation says that request_percpu_irq
2858f8642885SMaxime Ripard 	 * doesn't enable the interrupts automatically, it actually
2859f8642885SMaxime Ripard 	 * does so on the local CPU.
2860f8642885SMaxime Ripard 	 *
2861f8642885SMaxime Ripard 	 * Make sure it's disabled.
2862f8642885SMaxime Ripard 	 */
2863f8642885SMaxime Ripard 	mvneta_percpu_disable(pp);
2864f8642885SMaxime Ripard 
2865f8642885SMaxime Ripard 	/* Elect a CPU to handle our RX queue interrupt */
2866f8642885SMaxime Ripard 	mvneta_percpu_elect(pp);
2867f8642885SMaxime Ripard 
2868f8642885SMaxime Ripard 	/* Register a CPU notifier to handle the case where our CPU
2869f8642885SMaxime Ripard 	 * might be taken offline.
2870f8642885SMaxime Ripard 	 */
2871f8642885SMaxime Ripard 	register_cpu_notifier(&pp->cpu_notifier);
2872f8642885SMaxime Ripard 
2873c5aff182SThomas Petazzoni 	/* In default link is down */
2874c5aff182SThomas Petazzoni 	netif_carrier_off(pp->dev);
2875c5aff182SThomas Petazzoni 
2876c5aff182SThomas Petazzoni 	ret = mvneta_mdio_probe(pp);
2877c5aff182SThomas Petazzoni 	if (ret < 0) {
2878c5aff182SThomas Petazzoni 		netdev_err(dev, "cannot probe MDIO bus\n");
2879c5aff182SThomas Petazzoni 		goto err_free_irq;
2880c5aff182SThomas Petazzoni 	}
2881c5aff182SThomas Petazzoni 
2882c5aff182SThomas Petazzoni 	mvneta_start_dev(pp);
2883c5aff182SThomas Petazzoni 
2884c5aff182SThomas Petazzoni 	return 0;
2885c5aff182SThomas Petazzoni 
2886c5aff182SThomas Petazzoni err_free_irq:
288712bb03b4SMaxime Ripard 	free_percpu_irq(pp->dev->irq, pp->ports);
2888c5aff182SThomas Petazzoni err_cleanup_txqs:
2889c5aff182SThomas Petazzoni 	mvneta_cleanup_txqs(pp);
2890c5aff182SThomas Petazzoni err_cleanup_rxqs:
2891c5aff182SThomas Petazzoni 	mvneta_cleanup_rxqs(pp);
2892c5aff182SThomas Petazzoni 	return ret;
2893c5aff182SThomas Petazzoni }
2894c5aff182SThomas Petazzoni 
2895c5aff182SThomas Petazzoni /* Stop the port, free port interrupt line */
2896c5aff182SThomas Petazzoni static int mvneta_stop(struct net_device *dev)
2897c5aff182SThomas Petazzoni {
2898c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2899f8642885SMaxime Ripard 	int cpu;
2900c5aff182SThomas Petazzoni 
2901c5aff182SThomas Petazzoni 	mvneta_stop_dev(pp);
2902c5aff182SThomas Petazzoni 	mvneta_mdio_remove(pp);
2903f8642885SMaxime Ripard 	unregister_cpu_notifier(&pp->cpu_notifier);
2904f8642885SMaxime Ripard 	for_each_present_cpu(cpu)
2905f8642885SMaxime Ripard 		smp_call_function_single(cpu, mvneta_percpu_disable, pp, true);
290612bb03b4SMaxime Ripard 	free_percpu_irq(dev->irq, pp->ports);
2907c5aff182SThomas Petazzoni 	mvneta_cleanup_rxqs(pp);
2908c5aff182SThomas Petazzoni 	mvneta_cleanup_txqs(pp);
2909c5aff182SThomas Petazzoni 
2910c5aff182SThomas Petazzoni 	return 0;
2911c5aff182SThomas Petazzoni }
2912c5aff182SThomas Petazzoni 
291315f59456SThomas Petazzoni static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
291415f59456SThomas Petazzoni {
291515f59456SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
291615f59456SThomas Petazzoni 
291715f59456SThomas Petazzoni 	if (!pp->phy_dev)
291815f59456SThomas Petazzoni 		return -ENOTSUPP;
291915f59456SThomas Petazzoni 
2920ecf7b361SStas Sergeev 	return phy_mii_ioctl(pp->phy_dev, ifr, cmd);
292115f59456SThomas Petazzoni }
292215f59456SThomas Petazzoni 
2923c5aff182SThomas Petazzoni /* Ethtool methods */
2924c5aff182SThomas Petazzoni 
2925c5aff182SThomas Petazzoni /* Get settings (phy address, speed) for ethtools */
2926c5aff182SThomas Petazzoni int mvneta_ethtool_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2927c5aff182SThomas Petazzoni {
2928c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2929c5aff182SThomas Petazzoni 
2930c5aff182SThomas Petazzoni 	if (!pp->phy_dev)
2931c5aff182SThomas Petazzoni 		return -ENODEV;
2932c5aff182SThomas Petazzoni 
2933c5aff182SThomas Petazzoni 	return phy_ethtool_gset(pp->phy_dev, cmd);
2934c5aff182SThomas Petazzoni }
2935c5aff182SThomas Petazzoni 
2936c5aff182SThomas Petazzoni /* Set settings (phy address, speed) for ethtools */
2937c5aff182SThomas Petazzoni int mvneta_ethtool_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
2938c5aff182SThomas Petazzoni {
2939c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2940c5aff182SThomas Petazzoni 
2941c5aff182SThomas Petazzoni 	if (!pp->phy_dev)
2942c5aff182SThomas Petazzoni 		return -ENODEV;
2943c5aff182SThomas Petazzoni 
2944c5aff182SThomas Petazzoni 	return phy_ethtool_sset(pp->phy_dev, cmd);
2945c5aff182SThomas Petazzoni }
2946c5aff182SThomas Petazzoni 
2947c5aff182SThomas Petazzoni /* Set interrupt coalescing for ethtools */
2948c5aff182SThomas Petazzoni static int mvneta_ethtool_set_coalesce(struct net_device *dev,
2949c5aff182SThomas Petazzoni 				       struct ethtool_coalesce *c)
2950c5aff182SThomas Petazzoni {
2951c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2952c5aff182SThomas Petazzoni 	int queue;
2953c5aff182SThomas Petazzoni 
2954c5aff182SThomas Petazzoni 	for (queue = 0; queue < rxq_number; queue++) {
2955c5aff182SThomas Petazzoni 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
2956c5aff182SThomas Petazzoni 		rxq->time_coal = c->rx_coalesce_usecs;
2957c5aff182SThomas Petazzoni 		rxq->pkts_coal = c->rx_max_coalesced_frames;
2958c5aff182SThomas Petazzoni 		mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2959c5aff182SThomas Petazzoni 		mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2960c5aff182SThomas Petazzoni 	}
2961c5aff182SThomas Petazzoni 
2962c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
2963c5aff182SThomas Petazzoni 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
2964c5aff182SThomas Petazzoni 		txq->done_pkts_coal = c->tx_max_coalesced_frames;
2965c5aff182SThomas Petazzoni 		mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2966c5aff182SThomas Petazzoni 	}
2967c5aff182SThomas Petazzoni 
2968c5aff182SThomas Petazzoni 	return 0;
2969c5aff182SThomas Petazzoni }
2970c5aff182SThomas Petazzoni 
2971c5aff182SThomas Petazzoni /* get coalescing for ethtools */
2972c5aff182SThomas Petazzoni static int mvneta_ethtool_get_coalesce(struct net_device *dev,
2973c5aff182SThomas Petazzoni 				       struct ethtool_coalesce *c)
2974c5aff182SThomas Petazzoni {
2975c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
2976c5aff182SThomas Petazzoni 
2977c5aff182SThomas Petazzoni 	c->rx_coalesce_usecs        = pp->rxqs[0].time_coal;
2978c5aff182SThomas Petazzoni 	c->rx_max_coalesced_frames  = pp->rxqs[0].pkts_coal;
2979c5aff182SThomas Petazzoni 
2980c5aff182SThomas Petazzoni 	c->tx_max_coalesced_frames =  pp->txqs[0].done_pkts_coal;
2981c5aff182SThomas Petazzoni 	return 0;
2982c5aff182SThomas Petazzoni }
2983c5aff182SThomas Petazzoni 
2984c5aff182SThomas Petazzoni 
2985c5aff182SThomas Petazzoni static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
2986c5aff182SThomas Petazzoni 				    struct ethtool_drvinfo *drvinfo)
2987c5aff182SThomas Petazzoni {
2988c5aff182SThomas Petazzoni 	strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
2989c5aff182SThomas Petazzoni 		sizeof(drvinfo->driver));
2990c5aff182SThomas Petazzoni 	strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
2991c5aff182SThomas Petazzoni 		sizeof(drvinfo->version));
2992c5aff182SThomas Petazzoni 	strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
2993c5aff182SThomas Petazzoni 		sizeof(drvinfo->bus_info));
2994c5aff182SThomas Petazzoni }
2995c5aff182SThomas Petazzoni 
2996c5aff182SThomas Petazzoni 
2997c5aff182SThomas Petazzoni static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
2998c5aff182SThomas Petazzoni 					 struct ethtool_ringparam *ring)
2999c5aff182SThomas Petazzoni {
3000c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(netdev);
3001c5aff182SThomas Petazzoni 
3002c5aff182SThomas Petazzoni 	ring->rx_max_pending = MVNETA_MAX_RXD;
3003c5aff182SThomas Petazzoni 	ring->tx_max_pending = MVNETA_MAX_TXD;
3004c5aff182SThomas Petazzoni 	ring->rx_pending = pp->rx_ring_size;
3005c5aff182SThomas Petazzoni 	ring->tx_pending = pp->tx_ring_size;
3006c5aff182SThomas Petazzoni }
3007c5aff182SThomas Petazzoni 
3008c5aff182SThomas Petazzoni static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3009c5aff182SThomas Petazzoni 					struct ethtool_ringparam *ring)
3010c5aff182SThomas Petazzoni {
3011c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
3012c5aff182SThomas Petazzoni 
3013c5aff182SThomas Petazzoni 	if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3014c5aff182SThomas Petazzoni 		return -EINVAL;
3015c5aff182SThomas Petazzoni 	pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3016c5aff182SThomas Petazzoni 		ring->rx_pending : MVNETA_MAX_RXD;
30178eef5f97SEzequiel Garcia 
30188eef5f97SEzequiel Garcia 	pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
30198eef5f97SEzequiel Garcia 				   MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
30208eef5f97SEzequiel Garcia 	if (pp->tx_ring_size != ring->tx_pending)
30218eef5f97SEzequiel Garcia 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
30228eef5f97SEzequiel Garcia 			    pp->tx_ring_size, ring->tx_pending);
3023c5aff182SThomas Petazzoni 
3024c5aff182SThomas Petazzoni 	if (netif_running(dev)) {
3025c5aff182SThomas Petazzoni 		mvneta_stop(dev);
3026c5aff182SThomas Petazzoni 		if (mvneta_open(dev)) {
3027c5aff182SThomas Petazzoni 			netdev_err(dev,
3028c5aff182SThomas Petazzoni 				   "error on opening device after ring param change\n");
3029c5aff182SThomas Petazzoni 			return -ENOMEM;
3030c5aff182SThomas Petazzoni 		}
3031c5aff182SThomas Petazzoni 	}
3032c5aff182SThomas Petazzoni 
3033c5aff182SThomas Petazzoni 	return 0;
3034c5aff182SThomas Petazzoni }
3035c5aff182SThomas Petazzoni 
30369b0cdefaSRussell King static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
30379b0cdefaSRussell King 				       u8 *data)
30389b0cdefaSRussell King {
30399b0cdefaSRussell King 	if (sset == ETH_SS_STATS) {
30409b0cdefaSRussell King 		int i;
30419b0cdefaSRussell King 
30429b0cdefaSRussell King 		for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
30439b0cdefaSRussell King 			memcpy(data + i * ETH_GSTRING_LEN,
30449b0cdefaSRussell King 			       mvneta_statistics[i].name, ETH_GSTRING_LEN);
30459b0cdefaSRussell King 	}
30469b0cdefaSRussell King }
30479b0cdefaSRussell King 
30489b0cdefaSRussell King static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
30499b0cdefaSRussell King {
30509b0cdefaSRussell King 	const struct mvneta_statistic *s;
30519b0cdefaSRussell King 	void __iomem *base = pp->base;
30529b0cdefaSRussell King 	u32 high, low, val;
30539b0cdefaSRussell King 	int i;
30549b0cdefaSRussell King 
30559b0cdefaSRussell King 	for (i = 0, s = mvneta_statistics;
30569b0cdefaSRussell King 	     s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
30579b0cdefaSRussell King 	     s++, i++) {
30589b0cdefaSRussell King 		val = 0;
30599b0cdefaSRussell King 
30609b0cdefaSRussell King 		switch (s->type) {
30619b0cdefaSRussell King 		case T_REG_32:
30629b0cdefaSRussell King 			val = readl_relaxed(base + s->offset);
30639b0cdefaSRussell King 			break;
30649b0cdefaSRussell King 		case T_REG_64:
30659b0cdefaSRussell King 			/* Docs say to read low 32-bit then high */
30669b0cdefaSRussell King 			low = readl_relaxed(base + s->offset);
30679b0cdefaSRussell King 			high = readl_relaxed(base + s->offset + 4);
30689b0cdefaSRussell King 			val = (u64)high << 32 | low;
30699b0cdefaSRussell King 			break;
30709b0cdefaSRussell King 		}
30719b0cdefaSRussell King 
30729b0cdefaSRussell King 		pp->ethtool_stats[i] += val;
30739b0cdefaSRussell King 	}
30749b0cdefaSRussell King }
30759b0cdefaSRussell King 
30769b0cdefaSRussell King static void mvneta_ethtool_get_stats(struct net_device *dev,
30779b0cdefaSRussell King 				     struct ethtool_stats *stats, u64 *data)
30789b0cdefaSRussell King {
30799b0cdefaSRussell King 	struct mvneta_port *pp = netdev_priv(dev);
30809b0cdefaSRussell King 	int i;
30819b0cdefaSRussell King 
30829b0cdefaSRussell King 	mvneta_ethtool_update_stats(pp);
30839b0cdefaSRussell King 
30849b0cdefaSRussell King 	for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
30859b0cdefaSRussell King 		*data++ = pp->ethtool_stats[i];
30869b0cdefaSRussell King }
30879b0cdefaSRussell King 
30889b0cdefaSRussell King static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
30899b0cdefaSRussell King {
30909b0cdefaSRussell King 	if (sset == ETH_SS_STATS)
30919b0cdefaSRussell King 		return ARRAY_SIZE(mvneta_statistics);
30929b0cdefaSRussell King 	return -EOPNOTSUPP;
30939b0cdefaSRussell King }
30949b0cdefaSRussell King 
3095c5aff182SThomas Petazzoni static const struct net_device_ops mvneta_netdev_ops = {
3096c5aff182SThomas Petazzoni 	.ndo_open            = mvneta_open,
3097c5aff182SThomas Petazzoni 	.ndo_stop            = mvneta_stop,
3098c5aff182SThomas Petazzoni 	.ndo_start_xmit      = mvneta_tx,
3099c5aff182SThomas Petazzoni 	.ndo_set_rx_mode     = mvneta_set_rx_mode,
3100c5aff182SThomas Petazzoni 	.ndo_set_mac_address = mvneta_set_mac_addr,
3101c5aff182SThomas Petazzoni 	.ndo_change_mtu      = mvneta_change_mtu,
3102b65657fcSSimon Guinot 	.ndo_fix_features    = mvneta_fix_features,
3103c5aff182SThomas Petazzoni 	.ndo_get_stats64     = mvneta_get_stats64,
310415f59456SThomas Petazzoni 	.ndo_do_ioctl        = mvneta_ioctl,
3105c5aff182SThomas Petazzoni };
3106c5aff182SThomas Petazzoni 
3107c5aff182SThomas Petazzoni const struct ethtool_ops mvneta_eth_tool_ops = {
3108c5aff182SThomas Petazzoni 	.get_link       = ethtool_op_get_link,
3109c5aff182SThomas Petazzoni 	.get_settings   = mvneta_ethtool_get_settings,
3110c5aff182SThomas Petazzoni 	.set_settings   = mvneta_ethtool_set_settings,
3111c5aff182SThomas Petazzoni 	.set_coalesce   = mvneta_ethtool_set_coalesce,
3112c5aff182SThomas Petazzoni 	.get_coalesce   = mvneta_ethtool_get_coalesce,
3113c5aff182SThomas Petazzoni 	.get_drvinfo    = mvneta_ethtool_get_drvinfo,
3114c5aff182SThomas Petazzoni 	.get_ringparam  = mvneta_ethtool_get_ringparam,
3115c5aff182SThomas Petazzoni 	.set_ringparam	= mvneta_ethtool_set_ringparam,
31169b0cdefaSRussell King 	.get_strings	= mvneta_ethtool_get_strings,
31179b0cdefaSRussell King 	.get_ethtool_stats = mvneta_ethtool_get_stats,
31189b0cdefaSRussell King 	.get_sset_count	= mvneta_ethtool_get_sset_count,
3119c5aff182SThomas Petazzoni };
3120c5aff182SThomas Petazzoni 
3121c5aff182SThomas Petazzoni /* Initialize hw */
31229672850bSEzequiel Garcia static int mvneta_init(struct device *dev, struct mvneta_port *pp)
3123c5aff182SThomas Petazzoni {
3124c5aff182SThomas Petazzoni 	int queue;
3125c5aff182SThomas Petazzoni 
3126c5aff182SThomas Petazzoni 	/* Disable port */
3127c5aff182SThomas Petazzoni 	mvneta_port_disable(pp);
3128c5aff182SThomas Petazzoni 
3129c5aff182SThomas Petazzoni 	/* Set port default values */
3130c5aff182SThomas Petazzoni 	mvneta_defaults_set(pp);
3131c5aff182SThomas Petazzoni 
31329672850bSEzequiel Garcia 	pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
3133c5aff182SThomas Petazzoni 				GFP_KERNEL);
3134c5aff182SThomas Petazzoni 	if (!pp->txqs)
3135c5aff182SThomas Petazzoni 		return -ENOMEM;
3136c5aff182SThomas Petazzoni 
3137c5aff182SThomas Petazzoni 	/* Initialize TX descriptor rings */
3138c5aff182SThomas Petazzoni 	for (queue = 0; queue < txq_number; queue++) {
3139c5aff182SThomas Petazzoni 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
3140c5aff182SThomas Petazzoni 		txq->id = queue;
3141c5aff182SThomas Petazzoni 		txq->size = pp->tx_ring_size;
3142c5aff182SThomas Petazzoni 		txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
3143c5aff182SThomas Petazzoni 	}
3144c5aff182SThomas Petazzoni 
31459672850bSEzequiel Garcia 	pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
3146c5aff182SThomas Petazzoni 				GFP_KERNEL);
31479672850bSEzequiel Garcia 	if (!pp->rxqs)
3148c5aff182SThomas Petazzoni 		return -ENOMEM;
3149c5aff182SThomas Petazzoni 
3150c5aff182SThomas Petazzoni 	/* Create Rx descriptor rings */
3151c5aff182SThomas Petazzoni 	for (queue = 0; queue < rxq_number; queue++) {
3152c5aff182SThomas Petazzoni 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3153c5aff182SThomas Petazzoni 		rxq->id = queue;
3154c5aff182SThomas Petazzoni 		rxq->size = pp->rx_ring_size;
3155c5aff182SThomas Petazzoni 		rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
3156c5aff182SThomas Petazzoni 		rxq->time_coal = MVNETA_RX_COAL_USEC;
3157c5aff182SThomas Petazzoni 	}
3158c5aff182SThomas Petazzoni 
3159c5aff182SThomas Petazzoni 	return 0;
3160c5aff182SThomas Petazzoni }
3161c5aff182SThomas Petazzoni 
3162c5aff182SThomas Petazzoni /* platform glue : initialize decoding windows */
316303ce758eSGreg KH static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
3164c5aff182SThomas Petazzoni 				     const struct mbus_dram_target_info *dram)
3165c5aff182SThomas Petazzoni {
3166c5aff182SThomas Petazzoni 	u32 win_enable;
3167c5aff182SThomas Petazzoni 	u32 win_protect;
3168c5aff182SThomas Petazzoni 	int i;
3169c5aff182SThomas Petazzoni 
3170c5aff182SThomas Petazzoni 	for (i = 0; i < 6; i++) {
3171c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
3172c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
3173c5aff182SThomas Petazzoni 
3174c5aff182SThomas Petazzoni 		if (i < 4)
3175c5aff182SThomas Petazzoni 			mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
3176c5aff182SThomas Petazzoni 	}
3177c5aff182SThomas Petazzoni 
3178c5aff182SThomas Petazzoni 	win_enable = 0x3f;
3179c5aff182SThomas Petazzoni 	win_protect = 0;
3180c5aff182SThomas Petazzoni 
3181c5aff182SThomas Petazzoni 	for (i = 0; i < dram->num_cs; i++) {
3182c5aff182SThomas Petazzoni 		const struct mbus_dram_window *cs = dram->cs + i;
3183c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
3184c5aff182SThomas Petazzoni 			    (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
3185c5aff182SThomas Petazzoni 
3186c5aff182SThomas Petazzoni 		mvreg_write(pp, MVNETA_WIN_SIZE(i),
3187c5aff182SThomas Petazzoni 			    (cs->size - 1) & 0xffff0000);
3188c5aff182SThomas Petazzoni 
3189c5aff182SThomas Petazzoni 		win_enable &= ~(1 << i);
3190c5aff182SThomas Petazzoni 		win_protect |= 3 << (2 * i);
3191c5aff182SThomas Petazzoni 	}
3192c5aff182SThomas Petazzoni 
3193c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
3194c5aff182SThomas Petazzoni }
3195c5aff182SThomas Petazzoni 
3196c5aff182SThomas Petazzoni /* Power up the port */
31973f1dd4bcSThomas Petazzoni static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
3198c5aff182SThomas Petazzoni {
31993f1dd4bcSThomas Petazzoni 	u32 ctrl;
3200c5aff182SThomas Petazzoni 
3201c5aff182SThomas Petazzoni 	/* MAC Cause register should be cleared */
3202c5aff182SThomas Petazzoni 	mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
3203c5aff182SThomas Petazzoni 
32043f1dd4bcSThomas Petazzoni 	ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3205c5aff182SThomas Petazzoni 
32063f1dd4bcSThomas Petazzoni 	/* Even though it might look weird, when we're configured in
32073f1dd4bcSThomas Petazzoni 	 * SGMII or QSGMII mode, the RGMII bit needs to be set.
32083f1dd4bcSThomas Petazzoni 	 */
32093f1dd4bcSThomas Petazzoni 	switch(phy_mode) {
32103f1dd4bcSThomas Petazzoni 	case PHY_INTERFACE_MODE_QSGMII:
32113f1dd4bcSThomas Petazzoni 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
32123f1dd4bcSThomas Petazzoni 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
32133f1dd4bcSThomas Petazzoni 		break;
32143f1dd4bcSThomas Petazzoni 	case PHY_INTERFACE_MODE_SGMII:
32153f1dd4bcSThomas Petazzoni 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
32163f1dd4bcSThomas Petazzoni 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
32173f1dd4bcSThomas Petazzoni 		break;
32183f1dd4bcSThomas Petazzoni 	case PHY_INTERFACE_MODE_RGMII:
32193f1dd4bcSThomas Petazzoni 	case PHY_INTERFACE_MODE_RGMII_ID:
32203f1dd4bcSThomas Petazzoni 		ctrl |= MVNETA_GMAC2_PORT_RGMII;
32213f1dd4bcSThomas Petazzoni 		break;
32223f1dd4bcSThomas Petazzoni 	default:
32233f1dd4bcSThomas Petazzoni 		return -EINVAL;
32243f1dd4bcSThomas Petazzoni 	}
3225c5aff182SThomas Petazzoni 
3226898b2970SStas Sergeev 	if (pp->use_inband_status)
3227898b2970SStas Sergeev 		ctrl |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3228898b2970SStas Sergeev 
3229c5aff182SThomas Petazzoni 	/* Cancel Port Reset */
32303f1dd4bcSThomas Petazzoni 	ctrl &= ~MVNETA_GMAC2_PORT_RESET;
32313f1dd4bcSThomas Petazzoni 	mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
3232c5aff182SThomas Petazzoni 
3233c5aff182SThomas Petazzoni 	while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3234c5aff182SThomas Petazzoni 		MVNETA_GMAC2_PORT_RESET) != 0)
3235c5aff182SThomas Petazzoni 		continue;
32363f1dd4bcSThomas Petazzoni 
32373f1dd4bcSThomas Petazzoni 	return 0;
3238c5aff182SThomas Petazzoni }
3239c5aff182SThomas Petazzoni 
3240c5aff182SThomas Petazzoni /* Device initialization routine */
324103ce758eSGreg KH static int mvneta_probe(struct platform_device *pdev)
3242c5aff182SThomas Petazzoni {
3243c5aff182SThomas Petazzoni 	const struct mbus_dram_target_info *dram_target_info;
3244c3f0dd38SThomas Petazzoni 	struct resource *res;
3245c5aff182SThomas Petazzoni 	struct device_node *dn = pdev->dev.of_node;
3246c5aff182SThomas Petazzoni 	struct device_node *phy_node;
3247c5aff182SThomas Petazzoni 	struct mvneta_port *pp;
3248c5aff182SThomas Petazzoni 	struct net_device *dev;
32498cc3e439SThomas Petazzoni 	const char *dt_mac_addr;
32508cc3e439SThomas Petazzoni 	char hw_mac_addr[ETH_ALEN];
32518cc3e439SThomas Petazzoni 	const char *mac_from;
3252f8af8e6eSStas Sergeev 	const char *managed;
3253c5aff182SThomas Petazzoni 	int phy_mode;
3254c5aff182SThomas Petazzoni 	int err;
325512bb03b4SMaxime Ripard 	int cpu;
3256c5aff182SThomas Petazzoni 
3257ee40a116SWilly Tarreau 	dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
3258c5aff182SThomas Petazzoni 	if (!dev)
3259c5aff182SThomas Petazzoni 		return -ENOMEM;
3260c5aff182SThomas Petazzoni 
3261c5aff182SThomas Petazzoni 	dev->irq = irq_of_parse_and_map(dn, 0);
3262c5aff182SThomas Petazzoni 	if (dev->irq == 0) {
3263c5aff182SThomas Petazzoni 		err = -EINVAL;
3264c5aff182SThomas Petazzoni 		goto err_free_netdev;
3265c5aff182SThomas Petazzoni 	}
3266c5aff182SThomas Petazzoni 
3267c5aff182SThomas Petazzoni 	phy_node = of_parse_phandle(dn, "phy", 0);
3268c5aff182SThomas Petazzoni 	if (!phy_node) {
326983895bedSThomas Petazzoni 		if (!of_phy_is_fixed_link(dn)) {
327083895bedSThomas Petazzoni 			dev_err(&pdev->dev, "no PHY specified\n");
3271c5aff182SThomas Petazzoni 			err = -ENODEV;
3272c5aff182SThomas Petazzoni 			goto err_free_irq;
3273c5aff182SThomas Petazzoni 		}
3274c5aff182SThomas Petazzoni 
327583895bedSThomas Petazzoni 		err = of_phy_register_fixed_link(dn);
327683895bedSThomas Petazzoni 		if (err < 0) {
327783895bedSThomas Petazzoni 			dev_err(&pdev->dev, "cannot register fixed PHY\n");
327883895bedSThomas Petazzoni 			goto err_free_irq;
327983895bedSThomas Petazzoni 		}
328083895bedSThomas Petazzoni 
328183895bedSThomas Petazzoni 		/* In the case of a fixed PHY, the DT node associated
328283895bedSThomas Petazzoni 		 * to the PHY is the Ethernet MAC DT node.
328383895bedSThomas Petazzoni 		 */
3284c891c24cSUwe Kleine-König 		phy_node = of_node_get(dn);
328583895bedSThomas Petazzoni 	}
328683895bedSThomas Petazzoni 
3287c5aff182SThomas Petazzoni 	phy_mode = of_get_phy_mode(dn);
3288c5aff182SThomas Petazzoni 	if (phy_mode < 0) {
3289c5aff182SThomas Petazzoni 		dev_err(&pdev->dev, "incorrect phy-mode\n");
3290c5aff182SThomas Petazzoni 		err = -EINVAL;
3291c891c24cSUwe Kleine-König 		goto err_put_phy_node;
3292c5aff182SThomas Petazzoni 	}
3293c5aff182SThomas Petazzoni 
3294c5aff182SThomas Petazzoni 	dev->tx_queue_len = MVNETA_MAX_TXD;
3295c5aff182SThomas Petazzoni 	dev->watchdog_timeo = 5 * HZ;
3296c5aff182SThomas Petazzoni 	dev->netdev_ops = &mvneta_netdev_ops;
3297c5aff182SThomas Petazzoni 
32987ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &mvneta_eth_tool_ops;
3299c5aff182SThomas Petazzoni 
3300c5aff182SThomas Petazzoni 	pp = netdev_priv(dev);
3301c5aff182SThomas Petazzoni 	pp->phy_node = phy_node;
3302c5aff182SThomas Petazzoni 	pp->phy_interface = phy_mode;
3303f8af8e6eSStas Sergeev 
3304f8af8e6eSStas Sergeev 	err = of_property_read_string(dn, "managed", &managed);
3305f8af8e6eSStas Sergeev 	pp->use_inband_status = (err == 0 &&
3306f8af8e6eSStas Sergeev 				 strcmp(managed, "in-band-status") == 0);
3307f8642885SMaxime Ripard 	pp->cpu_notifier.notifier_call = mvneta_percpu_notifier;
3308c5aff182SThomas Petazzoni 
3309189dd626SThomas Petazzoni 	pp->clk = devm_clk_get(&pdev->dev, NULL);
3310189dd626SThomas Petazzoni 	if (IS_ERR(pp->clk)) {
3311189dd626SThomas Petazzoni 		err = PTR_ERR(pp->clk);
3312c891c24cSUwe Kleine-König 		goto err_put_phy_node;
3313189dd626SThomas Petazzoni 	}
3314189dd626SThomas Petazzoni 
3315189dd626SThomas Petazzoni 	clk_prepare_enable(pp->clk);
3316189dd626SThomas Petazzoni 
3317c3f0dd38SThomas Petazzoni 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3318c3f0dd38SThomas Petazzoni 	pp->base = devm_ioremap_resource(&pdev->dev, res);
3319c3f0dd38SThomas Petazzoni 	if (IS_ERR(pp->base)) {
3320c3f0dd38SThomas Petazzoni 		err = PTR_ERR(pp->base);
33215445eaf3SArnaud Patard \(Rtp\) 		goto err_clk;
33225445eaf3SArnaud Patard \(Rtp\) 	}
33235445eaf3SArnaud Patard \(Rtp\) 
332412bb03b4SMaxime Ripard 	/* Alloc per-cpu port structure */
332512bb03b4SMaxime Ripard 	pp->ports = alloc_percpu(struct mvneta_pcpu_port);
332612bb03b4SMaxime Ripard 	if (!pp->ports) {
332712bb03b4SMaxime Ripard 		err = -ENOMEM;
332812bb03b4SMaxime Ripard 		goto err_clk;
332912bb03b4SMaxime Ripard 	}
333012bb03b4SMaxime Ripard 
333174c41b04Swilly tarreau 	/* Alloc per-cpu stats */
33321c213bd2SWANG Cong 	pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
333374c41b04Swilly tarreau 	if (!pp->stats) {
333474c41b04Swilly tarreau 		err = -ENOMEM;
333512bb03b4SMaxime Ripard 		goto err_free_ports;
333674c41b04Swilly tarreau 	}
333774c41b04Swilly tarreau 
33388cc3e439SThomas Petazzoni 	dt_mac_addr = of_get_mac_address(dn);
33396c7a9a3cSLuka Perkov 	if (dt_mac_addr) {
33408cc3e439SThomas Petazzoni 		mac_from = "device tree";
33418cc3e439SThomas Petazzoni 		memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
33428cc3e439SThomas Petazzoni 	} else {
33438cc3e439SThomas Petazzoni 		mvneta_get_mac_addr(pp, hw_mac_addr);
33448cc3e439SThomas Petazzoni 		if (is_valid_ether_addr(hw_mac_addr)) {
33458cc3e439SThomas Petazzoni 			mac_from = "hardware";
33468cc3e439SThomas Petazzoni 			memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
33478cc3e439SThomas Petazzoni 		} else {
33488cc3e439SThomas Petazzoni 			mac_from = "random";
33498cc3e439SThomas Petazzoni 			eth_hw_addr_random(dev);
33508cc3e439SThomas Petazzoni 		}
33518cc3e439SThomas Petazzoni 	}
33528cc3e439SThomas Petazzoni 
3353b65657fcSSimon Guinot 	if (of_device_is_compatible(dn, "marvell,armada-370-neta"))
3354b65657fcSSimon Guinot 		pp->tx_csum_limit = 1600;
3355b65657fcSSimon Guinot 
3356c5aff182SThomas Petazzoni 	pp->tx_ring_size = MVNETA_MAX_TXD;
3357c5aff182SThomas Petazzoni 	pp->rx_ring_size = MVNETA_MAX_RXD;
3358c5aff182SThomas Petazzoni 
3359c5aff182SThomas Petazzoni 	pp->dev = dev;
3360c5aff182SThomas Petazzoni 	SET_NETDEV_DEV(dev, &pdev->dev);
3361c5aff182SThomas Petazzoni 
33629672850bSEzequiel Garcia 	err = mvneta_init(&pdev->dev, pp);
33639672850bSEzequiel Garcia 	if (err < 0)
336474c41b04Swilly tarreau 		goto err_free_stats;
33653f1dd4bcSThomas Petazzoni 
33663f1dd4bcSThomas Petazzoni 	err = mvneta_port_power_up(pp, phy_mode);
33673f1dd4bcSThomas Petazzoni 	if (err < 0) {
33683f1dd4bcSThomas Petazzoni 		dev_err(&pdev->dev, "can't power up port\n");
33699672850bSEzequiel Garcia 		goto err_free_stats;
33703f1dd4bcSThomas Petazzoni 	}
3371c5aff182SThomas Petazzoni 
3372c5aff182SThomas Petazzoni 	dram_target_info = mv_mbus_dram_info();
3373c5aff182SThomas Petazzoni 	if (dram_target_info)
3374c5aff182SThomas Petazzoni 		mvneta_conf_mbus_windows(pp, dram_target_info);
3375c5aff182SThomas Petazzoni 
337612bb03b4SMaxime Ripard 	for_each_present_cpu(cpu) {
337712bb03b4SMaxime Ripard 		struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
337812bb03b4SMaxime Ripard 
337912bb03b4SMaxime Ripard 		netif_napi_add(dev, &port->napi, mvneta_poll, NAPI_POLL_WEIGHT);
338012bb03b4SMaxime Ripard 		port->pp = pp;
338112bb03b4SMaxime Ripard 	}
3382c5aff182SThomas Petazzoni 
33832adb719dSEzequiel Garcia 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
338401ef26caSEzequiel Garcia 	dev->hw_features |= dev->features;
338501ef26caSEzequiel Garcia 	dev->vlan_features |= dev->features;
3386b50b72deSwilly tarreau 	dev->priv_flags |= IFF_UNICAST_FLT;
33878eef5f97SEzequiel Garcia 	dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
3388b50b72deSwilly tarreau 
3389c5aff182SThomas Petazzoni 	err = register_netdev(dev);
3390c5aff182SThomas Petazzoni 	if (err < 0) {
3391c5aff182SThomas Petazzoni 		dev_err(&pdev->dev, "failed to register\n");
33929672850bSEzequiel Garcia 		goto err_free_stats;
3393c5aff182SThomas Petazzoni 	}
3394c5aff182SThomas Petazzoni 
33958cc3e439SThomas Petazzoni 	netdev_info(dev, "Using %s mac address %pM\n", mac_from,
33968cc3e439SThomas Petazzoni 		    dev->dev_addr);
3397c5aff182SThomas Petazzoni 
3398c5aff182SThomas Petazzoni 	platform_set_drvdata(pdev, pp->dev);
3399c5aff182SThomas Petazzoni 
3400898b2970SStas Sergeev 	if (pp->use_inband_status) {
3401898b2970SStas Sergeev 		struct phy_device *phy = of_phy_find_device(dn);
3402898b2970SStas Sergeev 
3403898b2970SStas Sergeev 		mvneta_fixed_link_update(pp, phy);
340404d53b20SRussell King 
340504d53b20SRussell King 		put_device(&phy->dev);
3406898b2970SStas Sergeev 	}
3407898b2970SStas Sergeev 
3408c5aff182SThomas Petazzoni 	return 0;
3409c5aff182SThomas Petazzoni 
341074c41b04Swilly tarreau err_free_stats:
341174c41b04Swilly tarreau 	free_percpu(pp->stats);
341212bb03b4SMaxime Ripard err_free_ports:
341312bb03b4SMaxime Ripard 	free_percpu(pp->ports);
34145445eaf3SArnaud Patard \(Rtp\) err_clk:
34155445eaf3SArnaud Patard \(Rtp\) 	clk_disable_unprepare(pp->clk);
3416c891c24cSUwe Kleine-König err_put_phy_node:
3417c891c24cSUwe Kleine-König 	of_node_put(phy_node);
3418c5aff182SThomas Petazzoni err_free_irq:
3419c5aff182SThomas Petazzoni 	irq_dispose_mapping(dev->irq);
3420c5aff182SThomas Petazzoni err_free_netdev:
3421c5aff182SThomas Petazzoni 	free_netdev(dev);
3422c5aff182SThomas Petazzoni 	return err;
3423c5aff182SThomas Petazzoni }
3424c5aff182SThomas Petazzoni 
3425c5aff182SThomas Petazzoni /* Device removal routine */
342603ce758eSGreg KH static int mvneta_remove(struct platform_device *pdev)
3427c5aff182SThomas Petazzoni {
3428c5aff182SThomas Petazzoni 	struct net_device  *dev = platform_get_drvdata(pdev);
3429c5aff182SThomas Petazzoni 	struct mvneta_port *pp = netdev_priv(dev);
3430c5aff182SThomas Petazzoni 
3431c5aff182SThomas Petazzoni 	unregister_netdev(dev);
3432189dd626SThomas Petazzoni 	clk_disable_unprepare(pp->clk);
343312bb03b4SMaxime Ripard 	free_percpu(pp->ports);
343474c41b04Swilly tarreau 	free_percpu(pp->stats);
3435c5aff182SThomas Petazzoni 	irq_dispose_mapping(dev->irq);
3436c891c24cSUwe Kleine-König 	of_node_put(pp->phy_node);
3437c5aff182SThomas Petazzoni 	free_netdev(dev);
3438c5aff182SThomas Petazzoni 
3439c5aff182SThomas Petazzoni 	return 0;
3440c5aff182SThomas Petazzoni }
3441c5aff182SThomas Petazzoni 
3442c5aff182SThomas Petazzoni static const struct of_device_id mvneta_match[] = {
3443c5aff182SThomas Petazzoni 	{ .compatible = "marvell,armada-370-neta" },
3444f522a975SSimon Guinot 	{ .compatible = "marvell,armada-xp-neta" },
3445c5aff182SThomas Petazzoni 	{ }
3446c5aff182SThomas Petazzoni };
3447c5aff182SThomas Petazzoni MODULE_DEVICE_TABLE(of, mvneta_match);
3448c5aff182SThomas Petazzoni 
3449c5aff182SThomas Petazzoni static struct platform_driver mvneta_driver = {
3450c5aff182SThomas Petazzoni 	.probe = mvneta_probe,
345103ce758eSGreg KH 	.remove = mvneta_remove,
3452c5aff182SThomas Petazzoni 	.driver = {
3453c5aff182SThomas Petazzoni 		.name = MVNETA_DRIVER_NAME,
3454c5aff182SThomas Petazzoni 		.of_match_table = mvneta_match,
3455c5aff182SThomas Petazzoni 	},
3456c5aff182SThomas Petazzoni };
3457c5aff182SThomas Petazzoni 
3458c5aff182SThomas Petazzoni module_platform_driver(mvneta_driver);
3459c5aff182SThomas Petazzoni 
3460c5aff182SThomas Petazzoni MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
3461c5aff182SThomas Petazzoni MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
3462c5aff182SThomas Petazzoni MODULE_LICENSE("GPL");
3463c5aff182SThomas Petazzoni 
3464c5aff182SThomas Petazzoni module_param(rxq_number, int, S_IRUGO);
3465c5aff182SThomas Petazzoni module_param(txq_number, int, S_IRUGO);
3466c5aff182SThomas Petazzoni 
3467c5aff182SThomas Petazzoni module_param(rxq_def, int, S_IRUGO);
3468f19fadfcSwilly tarreau module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);
3469