]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/ethernet/marvell/mvpp2.c
ASoC: fsl_ssi: Fix build error
[linux.git] / drivers / net / ethernet / marvell / mvpp2.c
1 /*
2  * Driver for Marvell PPv2 network controller for Armada 375 SoC.
3  *
4  * Copyright (C) 2014 Marvell
5  *
6  * Marcin Wojtas <mw@semihalf.com>
7  *
8  * This file is licensed under the terms of the GNU General Public
9  * License version 2. This program is licensed "as is" without any
10  * warranty of any kind, whether express or implied.
11  */
12
13 #include <linux/kernel.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
16 #include <linux/platform_device.h>
17 #include <linux/skbuff.h>
18 #include <linux/inetdevice.h>
19 #include <linux/mbus.h>
20 #include <linux/module.h>
21 #include <linux/mfd/syscon.h>
22 #include <linux/interrupt.h>
23 #include <linux/cpumask.h>
24 #include <linux/of.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_mdio.h>
27 #include <linux/of_net.h>
28 #include <linux/of_address.h>
29 #include <linux/of_device.h>
30 #include <linux/phy.h>
31 #include <linux/phy/phy.h>
32 #include <linux/clk.h>
33 #include <linux/hrtimer.h>
34 #include <linux/ktime.h>
35 #include <linux/regmap.h>
36 #include <uapi/linux/ppp_defs.h>
37 #include <net/ip.h>
38 #include <net/ipv6.h>
39 #include <net/tso.h>
40
41 /* Fifo Registers */
42 #define MVPP2_RX_DATA_FIFO_SIZE_REG(port)       (0x00 + 4 * (port))
43 #define MVPP2_RX_ATTR_FIFO_SIZE_REG(port)       (0x20 + 4 * (port))
44 #define MVPP2_RX_MIN_PKT_SIZE_REG               0x60
45 #define MVPP2_RX_FIFO_INIT_REG                  0x64
46 #define MVPP22_TX_FIFO_SIZE_REG(port)           (0x8860 + 4 * (port))
47
48 /* RX DMA Top Registers */
49 #define MVPP2_RX_CTRL_REG(port)                 (0x140 + 4 * (port))
50 #define     MVPP2_RX_LOW_LATENCY_PKT_SIZE(s)    (((s) & 0xfff) << 16)
51 #define     MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK   BIT(31)
52 #define MVPP2_POOL_BUF_SIZE_REG(pool)           (0x180 + 4 * (pool))
53 #define     MVPP2_POOL_BUF_SIZE_OFFSET          5
54 #define MVPP2_RXQ_CONFIG_REG(rxq)               (0x800 + 4 * (rxq))
55 #define     MVPP2_SNOOP_PKT_SIZE_MASK           0x1ff
56 #define     MVPP2_SNOOP_BUF_HDR_MASK            BIT(9)
57 #define     MVPP2_RXQ_POOL_SHORT_OFFS           20
58 #define     MVPP21_RXQ_POOL_SHORT_MASK          0x700000
59 #define     MVPP22_RXQ_POOL_SHORT_MASK          0xf00000
60 #define     MVPP2_RXQ_POOL_LONG_OFFS            24
61 #define     MVPP21_RXQ_POOL_LONG_MASK           0x7000000
62 #define     MVPP22_RXQ_POOL_LONG_MASK           0xf000000
63 #define     MVPP2_RXQ_PACKET_OFFSET_OFFS        28
64 #define     MVPP2_RXQ_PACKET_OFFSET_MASK        0x70000000
65 #define     MVPP2_RXQ_DISABLE_MASK              BIT(31)
66
67 /* Parser Registers */
68 #define MVPP2_PRS_INIT_LOOKUP_REG               0x1000
69 #define     MVPP2_PRS_PORT_LU_MAX               0xf
70 #define     MVPP2_PRS_PORT_LU_MASK(port)        (0xff << ((port) * 4))
71 #define     MVPP2_PRS_PORT_LU_VAL(port, val)    ((val) << ((port) * 4))
72 #define MVPP2_PRS_INIT_OFFS_REG(port)           (0x1004 + ((port) & 4))
73 #define     MVPP2_PRS_INIT_OFF_MASK(port)       (0x3f << (((port) % 4) * 8))
74 #define     MVPP2_PRS_INIT_OFF_VAL(port, val)   ((val) << (((port) % 4) * 8))
75 #define MVPP2_PRS_MAX_LOOP_REG(port)            (0x100c + ((port) & 4))
76 #define     MVPP2_PRS_MAX_LOOP_MASK(port)       (0xff << (((port) % 4) * 8))
77 #define     MVPP2_PRS_MAX_LOOP_VAL(port, val)   ((val) << (((port) % 4) * 8))
78 #define MVPP2_PRS_TCAM_IDX_REG                  0x1100
79 #define MVPP2_PRS_TCAM_DATA_REG(idx)            (0x1104 + (idx) * 4)
80 #define     MVPP2_PRS_TCAM_INV_MASK             BIT(31)
81 #define MVPP2_PRS_SRAM_IDX_REG                  0x1200
82 #define MVPP2_PRS_SRAM_DATA_REG(idx)            (0x1204 + (idx) * 4)
83 #define MVPP2_PRS_TCAM_CTRL_REG                 0x1230
84 #define     MVPP2_PRS_TCAM_EN_MASK              BIT(0)
85
86 /* RSS Registers */
87 #define MVPP22_RSS_INDEX                        0x1500
88 #define     MVPP22_RSS_INDEX_TABLE_ENTRY(idx)   ((idx) << 8)
89 #define     MVPP22_RSS_INDEX_TABLE(idx)         ((idx) << 8)
90 #define     MVPP22_RSS_INDEX_QUEUE(idx)         ((idx) << 16)
91 #define MVPP22_RSS_TABLE_ENTRY                  0x1508
92 #define MVPP22_RSS_TABLE                        0x1510
93 #define     MVPP22_RSS_TABLE_POINTER(p)         (p)
94 #define MVPP22_RSS_WIDTH                        0x150c
95
96 /* Classifier Registers */
97 #define MVPP2_CLS_MODE_REG                      0x1800
98 #define     MVPP2_CLS_MODE_ACTIVE_MASK          BIT(0)
99 #define MVPP2_CLS_PORT_WAY_REG                  0x1810
100 #define     MVPP2_CLS_PORT_WAY_MASK(port)       (1 << (port))
101 #define MVPP2_CLS_LKP_INDEX_REG                 0x1814
102 #define     MVPP2_CLS_LKP_INDEX_WAY_OFFS        6
103 #define MVPP2_CLS_LKP_TBL_REG                   0x1818
104 #define     MVPP2_CLS_LKP_TBL_RXQ_MASK          0xff
105 #define     MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK    BIT(25)
106 #define MVPP2_CLS_FLOW_INDEX_REG                0x1820
107 #define MVPP2_CLS_FLOW_TBL0_REG                 0x1824
108 #define MVPP2_CLS_FLOW_TBL1_REG                 0x1828
109 #define MVPP2_CLS_FLOW_TBL2_REG                 0x182c
110 #define MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port)    (0x1980 + ((port) * 4))
111 #define     MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS     3
112 #define     MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK     0x7
113 #define MVPP2_CLS_SWFWD_P2HQ_REG(port)          (0x19b0 + ((port) * 4))
114 #define MVPP2_CLS_SWFWD_PCTRL_REG               0x19d0
115 #define     MVPP2_CLS_SWFWD_PCTRL_MASK(port)    (1 << (port))
116
117 /* Descriptor Manager Top Registers */
118 #define MVPP2_RXQ_NUM_REG                       0x2040
119 #define MVPP2_RXQ_DESC_ADDR_REG                 0x2044
120 #define     MVPP22_DESC_ADDR_OFFS               8
121 #define MVPP2_RXQ_DESC_SIZE_REG                 0x2048
122 #define     MVPP2_RXQ_DESC_SIZE_MASK            0x3ff0
123 #define MVPP2_RXQ_STATUS_UPDATE_REG(rxq)        (0x3000 + 4 * (rxq))
124 #define     MVPP2_RXQ_NUM_PROCESSED_OFFSET      0
125 #define     MVPP2_RXQ_NUM_NEW_OFFSET            16
126 #define MVPP2_RXQ_STATUS_REG(rxq)               (0x3400 + 4 * (rxq))
127 #define     MVPP2_RXQ_OCCUPIED_MASK             0x3fff
128 #define     MVPP2_RXQ_NON_OCCUPIED_OFFSET       16
129 #define     MVPP2_RXQ_NON_OCCUPIED_MASK         0x3fff0000
130 #define MVPP2_RXQ_THRESH_REG                    0x204c
131 #define     MVPP2_OCCUPIED_THRESH_OFFSET        0
132 #define     MVPP2_OCCUPIED_THRESH_MASK          0x3fff
133 #define MVPP2_RXQ_INDEX_REG                     0x2050
134 #define MVPP2_TXQ_NUM_REG                       0x2080
135 #define MVPP2_TXQ_DESC_ADDR_REG                 0x2084
136 #define MVPP2_TXQ_DESC_SIZE_REG                 0x2088
137 #define     MVPP2_TXQ_DESC_SIZE_MASK            0x3ff0
138 #define MVPP2_TXQ_THRESH_REG                    0x2094
139 #define     MVPP2_TXQ_THRESH_OFFSET             16
140 #define     MVPP2_TXQ_THRESH_MASK               0x3fff
141 #define MVPP2_AGGR_TXQ_UPDATE_REG               0x2090
142 #define MVPP2_TXQ_INDEX_REG                     0x2098
143 #define MVPP2_TXQ_PREF_BUF_REG                  0x209c
144 #define     MVPP2_PREF_BUF_PTR(desc)            ((desc) & 0xfff)
145 #define     MVPP2_PREF_BUF_SIZE_4               (BIT(12) | BIT(13))
146 #define     MVPP2_PREF_BUF_SIZE_16              (BIT(12) | BIT(14))
147 #define     MVPP2_PREF_BUF_THRESH(val)          ((val) << 17)
148 #define     MVPP2_TXQ_DRAIN_EN_MASK             BIT(31)
149 #define MVPP2_TXQ_PENDING_REG                   0x20a0
150 #define     MVPP2_TXQ_PENDING_MASK              0x3fff
151 #define MVPP2_TXQ_INT_STATUS_REG                0x20a4
152 #define MVPP2_TXQ_SENT_REG(txq)                 (0x3c00 + 4 * (txq))
153 #define     MVPP2_TRANSMITTED_COUNT_OFFSET      16
154 #define     MVPP2_TRANSMITTED_COUNT_MASK        0x3fff0000
155 #define MVPP2_TXQ_RSVD_REQ_REG                  0x20b0
156 #define     MVPP2_TXQ_RSVD_REQ_Q_OFFSET         16
157 #define MVPP2_TXQ_RSVD_RSLT_REG                 0x20b4
158 #define     MVPP2_TXQ_RSVD_RSLT_MASK            0x3fff
159 #define MVPP2_TXQ_RSVD_CLR_REG                  0x20b8
160 #define     MVPP2_TXQ_RSVD_CLR_OFFSET           16
161 #define MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu)       (0x2100 + 4 * (cpu))
162 #define     MVPP22_AGGR_TXQ_DESC_ADDR_OFFS      8
163 #define MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu)       (0x2140 + 4 * (cpu))
164 #define     MVPP2_AGGR_TXQ_DESC_SIZE_MASK       0x3ff0
165 #define MVPP2_AGGR_TXQ_STATUS_REG(cpu)          (0x2180 + 4 * (cpu))
166 #define     MVPP2_AGGR_TXQ_PENDING_MASK         0x3fff
167 #define MVPP2_AGGR_TXQ_INDEX_REG(cpu)           (0x21c0 + 4 * (cpu))
168
169 /* MBUS bridge registers */
170 #define MVPP2_WIN_BASE(w)                       (0x4000 + ((w) << 2))
171 #define MVPP2_WIN_SIZE(w)                       (0x4020 + ((w) << 2))
172 #define MVPP2_WIN_REMAP(w)                      (0x4040 + ((w) << 2))
173 #define MVPP2_BASE_ADDR_ENABLE                  0x4060
174
175 /* AXI Bridge Registers */
176 #define MVPP22_AXI_BM_WR_ATTR_REG               0x4100
177 #define MVPP22_AXI_BM_RD_ATTR_REG               0x4104
178 #define MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG      0x4110
179 #define MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG        0x4114
180 #define MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG        0x4118
181 #define MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG        0x411c
182 #define MVPP22_AXI_RX_DATA_WR_ATTR_REG          0x4120
183 #define MVPP22_AXI_TX_DATA_RD_ATTR_REG          0x4130
184 #define MVPP22_AXI_RD_NORMAL_CODE_REG           0x4150
185 #define MVPP22_AXI_RD_SNOOP_CODE_REG            0x4154
186 #define MVPP22_AXI_WR_NORMAL_CODE_REG           0x4160
187 #define MVPP22_AXI_WR_SNOOP_CODE_REG            0x4164
188
189 /* Values for AXI Bridge registers */
190 #define MVPP22_AXI_ATTR_CACHE_OFFS              0
191 #define MVPP22_AXI_ATTR_DOMAIN_OFFS             12
192
193 #define MVPP22_AXI_CODE_CACHE_OFFS              0
194 #define MVPP22_AXI_CODE_DOMAIN_OFFS             4
195
196 #define MVPP22_AXI_CODE_CACHE_NON_CACHE         0x3
197 #define MVPP22_AXI_CODE_CACHE_WR_CACHE          0x7
198 #define MVPP22_AXI_CODE_CACHE_RD_CACHE          0xb
199
200 #define MVPP22_AXI_CODE_DOMAIN_OUTER_DOM        2
201 #define MVPP22_AXI_CODE_DOMAIN_SYSTEM           3
202
203 /* Interrupt Cause and Mask registers */
204 #define MVPP2_ISR_TX_THRESHOLD_REG(port)        (0x5140 + 4 * (port))
205 #define     MVPP2_MAX_ISR_TX_THRESHOLD          0xfffff0
206
207 #define MVPP2_ISR_RX_THRESHOLD_REG(rxq)         (0x5200 + 4 * (rxq))
208 #define     MVPP2_MAX_ISR_RX_THRESHOLD          0xfffff0
209 #define MVPP21_ISR_RXQ_GROUP_REG(port)          (0x5400 + 4 * (port))
210
211 #define MVPP22_ISR_RXQ_GROUP_INDEX_REG          0x5400
212 #define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
213 #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK   0x380
214 #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET 7
215
216 #define MVPP22_ISR_RXQ_GROUP_INDEX_SUBGROUP_MASK 0xf
217 #define MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_MASK   0x380
218
219 #define MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG     0x5404
220 #define MVPP22_ISR_RXQ_SUB_GROUP_STARTQ_MASK    0x1f
221 #define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_MASK      0xf00
222 #define MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET    8
223
224 #define MVPP2_ISR_ENABLE_REG(port)              (0x5420 + 4 * (port))
225 #define     MVPP2_ISR_ENABLE_INTERRUPT(mask)    ((mask) & 0xffff)
226 #define     MVPP2_ISR_DISABLE_INTERRUPT(mask)   (((mask) << 16) & 0xffff0000)
227 #define MVPP2_ISR_RX_TX_CAUSE_REG(port)         (0x5480 + 4 * (port))
228 #define     MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK 0xffff
229 #define     MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK 0xff0000
230 #define     MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET       16
231 #define     MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK    BIT(24)
232 #define     MVPP2_CAUSE_FCS_ERR_MASK            BIT(25)
233 #define     MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK   BIT(26)
234 #define     MVPP2_CAUSE_TX_EXCEPTION_SUM_MASK   BIT(29)
235 #define     MVPP2_CAUSE_RX_EXCEPTION_SUM_MASK   BIT(30)
236 #define     MVPP2_CAUSE_MISC_SUM_MASK           BIT(31)
237 #define MVPP2_ISR_RX_TX_MASK_REG(port)          (0x54a0 + 4 * (port))
238 #define MVPP2_ISR_PON_RX_TX_MASK_REG            0x54bc
239 #define     MVPP2_PON_CAUSE_RXQ_OCCUP_DESC_ALL_MASK     0xffff
240 #define     MVPP2_PON_CAUSE_TXP_OCCUP_DESC_ALL_MASK     0x3fc00000
241 #define     MVPP2_PON_CAUSE_MISC_SUM_MASK               BIT(31)
242 #define MVPP2_ISR_MISC_CAUSE_REG                0x55b0
243
244 /* Buffer Manager registers */
245 #define MVPP2_BM_POOL_BASE_REG(pool)            (0x6000 + ((pool) * 4))
246 #define     MVPP2_BM_POOL_BASE_ADDR_MASK        0xfffff80
247 #define MVPP2_BM_POOL_SIZE_REG(pool)            (0x6040 + ((pool) * 4))
248 #define     MVPP2_BM_POOL_SIZE_MASK             0xfff0
249 #define MVPP2_BM_POOL_READ_PTR_REG(pool)        (0x6080 + ((pool) * 4))
250 #define     MVPP2_BM_POOL_GET_READ_PTR_MASK     0xfff0
251 #define MVPP2_BM_POOL_PTRS_NUM_REG(pool)        (0x60c0 + ((pool) * 4))
252 #define     MVPP2_BM_POOL_PTRS_NUM_MASK         0xfff0
253 #define MVPP2_BM_BPPI_READ_PTR_REG(pool)        (0x6100 + ((pool) * 4))
254 #define MVPP2_BM_BPPI_PTRS_NUM_REG(pool)        (0x6140 + ((pool) * 4))
255 #define     MVPP2_BM_BPPI_PTR_NUM_MASK          0x7ff
256 #define     MVPP2_BM_BPPI_PREFETCH_FULL_MASK    BIT(16)
257 #define MVPP2_BM_POOL_CTRL_REG(pool)            (0x6200 + ((pool) * 4))
258 #define     MVPP2_BM_START_MASK                 BIT(0)
259 #define     MVPP2_BM_STOP_MASK                  BIT(1)
260 #define     MVPP2_BM_STATE_MASK                 BIT(4)
261 #define     MVPP2_BM_LOW_THRESH_OFFS            8
262 #define     MVPP2_BM_LOW_THRESH_MASK            0x7f00
263 #define     MVPP2_BM_LOW_THRESH_VALUE(val)      ((val) << \
264                                                 MVPP2_BM_LOW_THRESH_OFFS)
265 #define     MVPP2_BM_HIGH_THRESH_OFFS           16
266 #define     MVPP2_BM_HIGH_THRESH_MASK           0x7f0000
267 #define     MVPP2_BM_HIGH_THRESH_VALUE(val)     ((val) << \
268                                                 MVPP2_BM_HIGH_THRESH_OFFS)
269 #define MVPP2_BM_INTR_CAUSE_REG(pool)           (0x6240 + ((pool) * 4))
270 #define     MVPP2_BM_RELEASED_DELAY_MASK        BIT(0)
271 #define     MVPP2_BM_ALLOC_FAILED_MASK          BIT(1)
272 #define     MVPP2_BM_BPPE_EMPTY_MASK            BIT(2)
273 #define     MVPP2_BM_BPPE_FULL_MASK             BIT(3)
274 #define     MVPP2_BM_AVAILABLE_BP_LOW_MASK      BIT(4)
275 #define MVPP2_BM_INTR_MASK_REG(pool)            (0x6280 + ((pool) * 4))
276 #define MVPP2_BM_PHY_ALLOC_REG(pool)            (0x6400 + ((pool) * 4))
277 #define     MVPP2_BM_PHY_ALLOC_GRNTD_MASK       BIT(0)
278 #define MVPP2_BM_VIRT_ALLOC_REG                 0x6440
279 #define MVPP22_BM_ADDR_HIGH_ALLOC               0x6444
280 #define     MVPP22_BM_ADDR_HIGH_PHYS_MASK       0xff
281 #define     MVPP22_BM_ADDR_HIGH_VIRT_MASK       0xff00
282 #define     MVPP22_BM_ADDR_HIGH_VIRT_SHIFT      8
283 #define MVPP2_BM_PHY_RLS_REG(pool)              (0x6480 + ((pool) * 4))
284 #define     MVPP2_BM_PHY_RLS_MC_BUFF_MASK       BIT(0)
285 #define     MVPP2_BM_PHY_RLS_PRIO_EN_MASK       BIT(1)
286 #define     MVPP2_BM_PHY_RLS_GRNTD_MASK         BIT(2)
287 #define MVPP2_BM_VIRT_RLS_REG                   0x64c0
288 #define MVPP22_BM_ADDR_HIGH_RLS_REG             0x64c4
289 #define     MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK   0xff
290 #define     MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK   0xff00
291 #define     MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT  8
292
293 /* TX Scheduler registers */
294 #define MVPP2_TXP_SCHED_PORT_INDEX_REG          0x8000
295 #define MVPP2_TXP_SCHED_Q_CMD_REG               0x8004
296 #define     MVPP2_TXP_SCHED_ENQ_MASK            0xff
297 #define     MVPP2_TXP_SCHED_DISQ_OFFSET         8
298 #define MVPP2_TXP_SCHED_CMD_1_REG               0x8010
299 #define MVPP2_TXP_SCHED_PERIOD_REG              0x8018
300 #define MVPP2_TXP_SCHED_MTU_REG                 0x801c
301 #define     MVPP2_TXP_MTU_MAX                   0x7FFFF
302 #define MVPP2_TXP_SCHED_REFILL_REG              0x8020
303 #define     MVPP2_TXP_REFILL_TOKENS_ALL_MASK    0x7ffff
304 #define     MVPP2_TXP_REFILL_PERIOD_ALL_MASK    0x3ff00000
305 #define     MVPP2_TXP_REFILL_PERIOD_MASK(v)     ((v) << 20)
306 #define MVPP2_TXP_SCHED_TOKEN_SIZE_REG          0x8024
307 #define     MVPP2_TXP_TOKEN_SIZE_MAX            0xffffffff
308 #define MVPP2_TXQ_SCHED_REFILL_REG(q)           (0x8040 + ((q) << 2))
309 #define     MVPP2_TXQ_REFILL_TOKENS_ALL_MASK    0x7ffff
310 #define     MVPP2_TXQ_REFILL_PERIOD_ALL_MASK    0x3ff00000
311 #define     MVPP2_TXQ_REFILL_PERIOD_MASK(v)     ((v) << 20)
312 #define MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(q)       (0x8060 + ((q) << 2))
313 #define     MVPP2_TXQ_TOKEN_SIZE_MAX            0x7fffffff
314 #define MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(q)       (0x8080 + ((q) << 2))
315 #define     MVPP2_TXQ_TOKEN_CNTR_MAX            0xffffffff
316
317 /* TX general registers */
318 #define MVPP2_TX_SNOOP_REG                      0x8800
319 #define MVPP2_TX_PORT_FLUSH_REG                 0x8810
320 #define     MVPP2_TX_PORT_FLUSH_MASK(port)      (1 << (port))
321
322 /* LMS registers */
323 #define MVPP2_SRC_ADDR_MIDDLE                   0x24
324 #define MVPP2_SRC_ADDR_HIGH                     0x28
325 #define MVPP2_PHY_AN_CFG0_REG                   0x34
326 #define     MVPP2_PHY_AN_STOP_SMI0_MASK         BIT(7)
327 #define MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG      0x305c
328 #define     MVPP2_EXT_GLOBAL_CTRL_DEFAULT       0x27
329
330 /* Per-port registers */
331 #define MVPP2_GMAC_CTRL_0_REG                   0x0
332 #define     MVPP2_GMAC_PORT_EN_MASK             BIT(0)
333 #define     MVPP2_GMAC_PORT_TYPE_MASK           BIT(1)
334 #define     MVPP2_GMAC_MAX_RX_SIZE_OFFS         2
335 #define     MVPP2_GMAC_MAX_RX_SIZE_MASK         0x7ffc
336 #define     MVPP2_GMAC_MIB_CNTR_EN_MASK         BIT(15)
337 #define MVPP2_GMAC_CTRL_1_REG                   0x4
338 #define     MVPP2_GMAC_PERIODIC_XON_EN_MASK     BIT(1)
339 #define     MVPP2_GMAC_GMII_LB_EN_MASK          BIT(5)
340 #define     MVPP2_GMAC_PCS_LB_EN_BIT            6
341 #define     MVPP2_GMAC_PCS_LB_EN_MASK           BIT(6)
342 #define     MVPP2_GMAC_SA_LOW_OFFS              7
343 #define MVPP2_GMAC_CTRL_2_REG                   0x8
344 #define     MVPP2_GMAC_INBAND_AN_MASK           BIT(0)
345 #define     MVPP2_GMAC_FLOW_CTRL_MASK           GENMASK(2, 1)
346 #define     MVPP2_GMAC_PCS_ENABLE_MASK          BIT(3)
347 #define     MVPP2_GMAC_INTERNAL_CLK_MASK        BIT(4)
348 #define     MVPP2_GMAC_DISABLE_PADDING          BIT(5)
349 #define     MVPP2_GMAC_PORT_RESET_MASK          BIT(6)
350 #define MVPP2_GMAC_AUTONEG_CONFIG               0xc
351 #define     MVPP2_GMAC_FORCE_LINK_DOWN          BIT(0)
352 #define     MVPP2_GMAC_FORCE_LINK_PASS          BIT(1)
353 #define     MVPP2_GMAC_IN_BAND_AUTONEG          BIT(2)
354 #define     MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS   BIT(3)
355 #define     MVPP2_GMAC_CONFIG_MII_SPEED BIT(5)
356 #define     MVPP2_GMAC_CONFIG_GMII_SPEED        BIT(6)
357 #define     MVPP2_GMAC_AN_SPEED_EN              BIT(7)
358 #define     MVPP2_GMAC_FC_ADV_EN                BIT(9)
359 #define     MVPP2_GMAC_FLOW_CTRL_AUTONEG        BIT(11)
360 #define     MVPP2_GMAC_CONFIG_FULL_DUPLEX       BIT(12)
361 #define     MVPP2_GMAC_AN_DUPLEX_EN             BIT(13)
362 #define MVPP2_GMAC_STATUS0                      0x10
363 #define     MVPP2_GMAC_STATUS0_LINK_UP          BIT(0)
364 #define MVPP2_GMAC_PORT_FIFO_CFG_1_REG          0x1c
365 #define     MVPP2_GMAC_TX_FIFO_MIN_TH_OFFS      6
366 #define     MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK  0x1fc0
367 #define     MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(v)   (((v) << 6) & \
368                                         MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK)
369 #define MVPP22_GMAC_INT_STAT                    0x20
370 #define     MVPP22_GMAC_INT_STAT_LINK           BIT(1)
371 #define MVPP22_GMAC_INT_MASK                    0x24
372 #define     MVPP22_GMAC_INT_MASK_LINK_STAT      BIT(1)
373 #define MVPP22_GMAC_CTRL_4_REG                  0x90
374 #define     MVPP22_CTRL4_EXT_PIN_GMII_SEL       BIT(0)
375 #define     MVPP22_CTRL4_DP_CLK_SEL             BIT(5)
376 #define     MVPP22_CTRL4_SYNC_BYPASS_DIS        BIT(6)
377 #define     MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE   BIT(7)
378 #define MVPP22_GMAC_INT_SUM_MASK                0xa4
379 #define     MVPP22_GMAC_INT_SUM_MASK_LINK_STAT  BIT(1)
380
381 /* Per-port XGMAC registers. PPv2.2 only, only for GOP port 0,
382  * relative to port->base.
383  */
384 #define MVPP22_XLG_CTRL0_REG                    0x100
385 #define     MVPP22_XLG_CTRL0_PORT_EN            BIT(0)
386 #define     MVPP22_XLG_CTRL0_MAC_RESET_DIS      BIT(1)
387 #define     MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN    BIT(7)
388 #define     MVPP22_XLG_CTRL0_MIB_CNT_DIS        BIT(14)
389 #define MVPP22_XLG_CTRL1_REG                    0x104
390 #define     MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS        0
391 #define     MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK        0x1fff
392 #define MVPP22_XLG_STATUS                       0x10c
393 #define     MVPP22_XLG_STATUS_LINK_UP           BIT(0)
394 #define MVPP22_XLG_INT_STAT                     0x114
395 #define     MVPP22_XLG_INT_STAT_LINK            BIT(1)
396 #define MVPP22_XLG_INT_MASK                     0x118
397 #define     MVPP22_XLG_INT_MASK_LINK            BIT(1)
398 #define MVPP22_XLG_CTRL3_REG                    0x11c
399 #define     MVPP22_XLG_CTRL3_MACMODESELECT_MASK (7 << 13)
400 #define     MVPP22_XLG_CTRL3_MACMODESELECT_GMAC (0 << 13)
401 #define     MVPP22_XLG_CTRL3_MACMODESELECT_10G  (1 << 13)
402 #define MVPP22_XLG_EXT_INT_MASK                 0x15c
403 #define     MVPP22_XLG_EXT_INT_MASK_XLG         BIT(1)
404 #define     MVPP22_XLG_EXT_INT_MASK_GIG         BIT(2)
405 #define MVPP22_XLG_CTRL4_REG                    0x184
406 #define     MVPP22_XLG_CTRL4_FWD_FC             BIT(5)
407 #define     MVPP22_XLG_CTRL4_FWD_PFC            BIT(6)
408 #define     MVPP22_XLG_CTRL4_MACMODSELECT_GMAC  BIT(12)
409
410 /* SMI registers. PPv2.2 only, relative to priv->iface_base. */
411 #define MVPP22_SMI_MISC_CFG_REG                 0x1204
412 #define     MVPP22_SMI_POLLING_EN               BIT(10)
413
414 #define MVPP22_GMAC_BASE(port)          (0x7000 + (port) * 0x1000 + 0xe00)
415
416 #define MVPP2_CAUSE_TXQ_SENT_DESC_ALL_MASK      0xff
417
418 /* Descriptor ring Macros */
419 #define MVPP2_QUEUE_NEXT_DESC(q, index) \
420         (((index) < (q)->last_desc) ? ((index) + 1) : 0)
421
422 /* XPCS registers. PPv2.2 only */
423 #define MVPP22_MPCS_BASE(port)                  (0x7000 + (port) * 0x1000)
424 #define MVPP22_MPCS_CTRL                        0x14
425 #define     MVPP22_MPCS_CTRL_FWD_ERR_CONN       BIT(10)
426 #define MVPP22_MPCS_CLK_RESET                   0x14c
427 #define     MAC_CLK_RESET_SD_TX                 BIT(0)
428 #define     MAC_CLK_RESET_SD_RX                 BIT(1)
429 #define     MAC_CLK_RESET_MAC                   BIT(2)
430 #define     MVPP22_MPCS_CLK_RESET_DIV_RATIO(n)  ((n) << 4)
431 #define     MVPP22_MPCS_CLK_RESET_DIV_SET       BIT(11)
432
433 /* XPCS registers. PPv2.2 only */
434 #define MVPP22_XPCS_BASE(port)                  (0x7400 + (port) * 0x1000)
435 #define MVPP22_XPCS_CFG0                        0x0
436 #define     MVPP22_XPCS_CFG0_PCS_MODE(n)        ((n) << 3)
437 #define     MVPP22_XPCS_CFG0_ACTIVE_LANE(n)     ((n) << 5)
438
439 /* System controller registers. Accessed through a regmap. */
440 #define GENCONF_SOFT_RESET1                             0x1108
441 #define     GENCONF_SOFT_RESET1_GOP                     BIT(6)
442 #define GENCONF_PORT_CTRL0                              0x1110
443 #define     GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT         BIT(1)
444 #define     GENCONF_PORT_CTRL0_RX_DATA_SAMPLE           BIT(29)
445 #define     GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR        BIT(31)
446 #define GENCONF_PORT_CTRL1                              0x1114
447 #define     GENCONF_PORT_CTRL1_EN(p)                    BIT(p)
448 #define     GENCONF_PORT_CTRL1_RESET(p)                 (BIT(p) << 28)
449 #define GENCONF_CTRL0                                   0x1120
450 #define     GENCONF_CTRL0_PORT0_RGMII                   BIT(0)
451 #define     GENCONF_CTRL0_PORT1_RGMII_MII               BIT(1)
452 #define     GENCONF_CTRL0_PORT1_RGMII                   BIT(2)
453
454 /* Various constants */
455
456 /* Coalescing */
457 #define MVPP2_TXDONE_COAL_PKTS_THRESH   15
458 #define MVPP2_TXDONE_HRTIMER_PERIOD_NS  1000000UL
459 #define MVPP2_TXDONE_COAL_USEC          1000
460 #define MVPP2_RX_COAL_PKTS              32
461 #define MVPP2_RX_COAL_USEC              100
462
463 /* The two bytes Marvell header. Either contains a special value used
464  * by Marvell switches when a specific hardware mode is enabled (not
465  * supported by this driver) or is filled automatically by zeroes on
466  * the RX side. Those two bytes being at the front of the Ethernet
467  * header, they allow to have the IP header aligned on a 4 bytes
468  * boundary automatically: the hardware skips those two bytes on its
469  * own.
470  */
471 #define MVPP2_MH_SIZE                   2
472 #define MVPP2_ETH_TYPE_LEN              2
473 #define MVPP2_PPPOE_HDR_SIZE            8
474 #define MVPP2_VLAN_TAG_LEN              4
475
476 /* Lbtd 802.3 type */
477 #define MVPP2_IP_LBDT_TYPE              0xfffa
478
479 #define MVPP2_TX_CSUM_MAX_SIZE          9800
480
481 /* Timeout constants */
482 #define MVPP2_TX_DISABLE_TIMEOUT_MSEC   1000
483 #define MVPP2_TX_PENDING_TIMEOUT_MSEC   1000
484
485 #define MVPP2_TX_MTU_MAX                0x7ffff
486
487 /* Maximum number of T-CONTs of PON port */
488 #define MVPP2_MAX_TCONT                 16
489
490 /* Maximum number of supported ports */
491 #define MVPP2_MAX_PORTS                 4
492
493 /* Maximum number of TXQs used by single port */
494 #define MVPP2_MAX_TXQ                   8
495
496 /* MVPP2_MAX_TSO_SEGS is the maximum number of fragments to allow in the GSO
497  * skb. As we need a maxium of two descriptors per fragments (1 header, 1 data),
498  * multiply this value by two to count the maximum number of skb descs needed.
499  */
500 #define MVPP2_MAX_TSO_SEGS              300
501 #define MVPP2_MAX_SKB_DESCS             (MVPP2_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
502
503 /* Dfault number of RXQs in use */
504 #define MVPP2_DEFAULT_RXQ               4
505
506 /* Max number of Rx descriptors */
507 #define MVPP2_MAX_RXD                   128
508
509 /* Max number of Tx descriptors */
510 #define MVPP2_MAX_TXD                   1024
511
512 /* Amount of Tx descriptors that can be reserved at once by CPU */
513 #define MVPP2_CPU_DESC_CHUNK            64
514
515 /* Max number of Tx descriptors in each aggregated queue */
516 #define MVPP2_AGGR_TXQ_SIZE             256
517
518 /* Descriptor aligned size */
519 #define MVPP2_DESC_ALIGNED_SIZE         32
520
521 /* Descriptor alignment mask */
522 #define MVPP2_TX_DESC_ALIGN             (MVPP2_DESC_ALIGNED_SIZE - 1)
523
524 /* RX FIFO constants */
525 #define MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB       0x8000
526 #define MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB        0x2000
527 #define MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB        0x1000
528 #define MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB       0x200
529 #define MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB        0x80
530 #define MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB        0x40
531 #define MVPP2_RX_FIFO_PORT_MIN_PKT              0x80
532
533 /* TX FIFO constants */
534 #define MVPP22_TX_FIFO_DATA_SIZE_10KB           0xa
535 #define MVPP22_TX_FIFO_DATA_SIZE_3KB            0x3
536
537 /* RX buffer constants */
538 #define MVPP2_SKB_SHINFO_SIZE \
539         SKB_DATA_ALIGN(sizeof(struct skb_shared_info))
540
541 #define MVPP2_RX_PKT_SIZE(mtu) \
542         ALIGN((mtu) + MVPP2_MH_SIZE + MVPP2_VLAN_TAG_LEN + \
543               ETH_HLEN + ETH_FCS_LEN, cache_line_size())
544
545 #define MVPP2_RX_BUF_SIZE(pkt_size)     ((pkt_size) + NET_SKB_PAD)
546 #define MVPP2_RX_TOTAL_SIZE(buf_size)   ((buf_size) + MVPP2_SKB_SHINFO_SIZE)
547 #define MVPP2_RX_MAX_PKT_SIZE(total_size) \
548         ((total_size) - NET_SKB_PAD - MVPP2_SKB_SHINFO_SIZE)
549
550 #define MVPP2_BIT_TO_BYTE(bit)          ((bit) / 8)
551
552 /* IPv6 max L3 address size */
553 #define MVPP2_MAX_L3_ADDR_SIZE          16
554
555 /* Port flags */
556 #define MVPP2_F_LOOPBACK                BIT(0)
557
558 /* Marvell tag types */
559 enum mvpp2_tag_type {
560         MVPP2_TAG_TYPE_NONE = 0,
561         MVPP2_TAG_TYPE_MH   = 1,
562         MVPP2_TAG_TYPE_DSA  = 2,
563         MVPP2_TAG_TYPE_EDSA = 3,
564         MVPP2_TAG_TYPE_VLAN = 4,
565         MVPP2_TAG_TYPE_LAST = 5
566 };
567
568 /* Parser constants */
569 #define MVPP2_PRS_TCAM_SRAM_SIZE        256
570 #define MVPP2_PRS_TCAM_WORDS            6
571 #define MVPP2_PRS_SRAM_WORDS            4
572 #define MVPP2_PRS_FLOW_ID_SIZE          64
573 #define MVPP2_PRS_FLOW_ID_MASK          0x3f
574 #define MVPP2_PRS_TCAM_ENTRY_INVALID    1
575 #define MVPP2_PRS_TCAM_DSA_TAGGED_BIT   BIT(5)
576 #define MVPP2_PRS_IPV4_HEAD             0x40
577 #define MVPP2_PRS_IPV4_HEAD_MASK        0xf0
578 #define MVPP2_PRS_IPV4_MC               0xe0
579 #define MVPP2_PRS_IPV4_MC_MASK          0xf0
580 #define MVPP2_PRS_IPV4_BC_MASK          0xff
581 #define MVPP2_PRS_IPV4_IHL              0x5
582 #define MVPP2_PRS_IPV4_IHL_MASK         0xf
583 #define MVPP2_PRS_IPV6_MC               0xff
584 #define MVPP2_PRS_IPV6_MC_MASK          0xff
585 #define MVPP2_PRS_IPV6_HOP_MASK         0xff
586 #define MVPP2_PRS_TCAM_PROTO_MASK       0xff
587 #define MVPP2_PRS_TCAM_PROTO_MASK_L     0x3f
588 #define MVPP2_PRS_DBL_VLANS_MAX         100
589
590 /* Tcam structure:
591  * - lookup ID - 4 bits
592  * - port ID - 1 byte
593  * - additional information - 1 byte
594  * - header data - 8 bytes
595  * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(5)->(0).
596  */
597 #define MVPP2_PRS_AI_BITS                       8
598 #define MVPP2_PRS_PORT_MASK                     0xff
599 #define MVPP2_PRS_LU_MASK                       0xf
600 #define MVPP2_PRS_TCAM_DATA_BYTE(offs)          \
601                                     (((offs) - ((offs) % 2)) * 2 + ((offs) % 2))
602 #define MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)       \
603                                               (((offs) * 2) - ((offs) % 2)  + 2)
604 #define MVPP2_PRS_TCAM_AI_BYTE                  16
605 #define MVPP2_PRS_TCAM_PORT_BYTE                17
606 #define MVPP2_PRS_TCAM_LU_BYTE                  20
607 #define MVPP2_PRS_TCAM_EN_OFFS(offs)            ((offs) + 2)
608 #define MVPP2_PRS_TCAM_INV_WORD                 5
609 /* Tcam entries ID */
610 #define MVPP2_PE_DROP_ALL               0
611 #define MVPP2_PE_FIRST_FREE_TID         1
612 #define MVPP2_PE_LAST_FREE_TID          (MVPP2_PRS_TCAM_SRAM_SIZE - 31)
613 #define MVPP2_PE_IP6_EXT_PROTO_UN       (MVPP2_PRS_TCAM_SRAM_SIZE - 30)
614 #define MVPP2_PE_MAC_MC_IP6             (MVPP2_PRS_TCAM_SRAM_SIZE - 29)
615 #define MVPP2_PE_IP6_ADDR_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 28)
616 #define MVPP2_PE_IP4_ADDR_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 27)
617 #define MVPP2_PE_LAST_DEFAULT_FLOW      (MVPP2_PRS_TCAM_SRAM_SIZE - 26)
618 #define MVPP2_PE_FIRST_DEFAULT_FLOW     (MVPP2_PRS_TCAM_SRAM_SIZE - 19)
619 #define MVPP2_PE_EDSA_TAGGED            (MVPP2_PRS_TCAM_SRAM_SIZE - 18)
620 #define MVPP2_PE_EDSA_UNTAGGED          (MVPP2_PRS_TCAM_SRAM_SIZE - 17)
621 #define MVPP2_PE_DSA_TAGGED             (MVPP2_PRS_TCAM_SRAM_SIZE - 16)
622 #define MVPP2_PE_DSA_UNTAGGED           (MVPP2_PRS_TCAM_SRAM_SIZE - 15)
623 #define MVPP2_PE_ETYPE_EDSA_TAGGED      (MVPP2_PRS_TCAM_SRAM_SIZE - 14)
624 #define MVPP2_PE_ETYPE_EDSA_UNTAGGED    (MVPP2_PRS_TCAM_SRAM_SIZE - 13)
625 #define MVPP2_PE_ETYPE_DSA_TAGGED       (MVPP2_PRS_TCAM_SRAM_SIZE - 12)
626 #define MVPP2_PE_ETYPE_DSA_UNTAGGED     (MVPP2_PRS_TCAM_SRAM_SIZE - 11)
627 #define MVPP2_PE_MH_DEFAULT             (MVPP2_PRS_TCAM_SRAM_SIZE - 10)
628 #define MVPP2_PE_DSA_DEFAULT            (MVPP2_PRS_TCAM_SRAM_SIZE - 9)
629 #define MVPP2_PE_IP6_PROTO_UN           (MVPP2_PRS_TCAM_SRAM_SIZE - 8)
630 #define MVPP2_PE_IP4_PROTO_UN           (MVPP2_PRS_TCAM_SRAM_SIZE - 7)
631 #define MVPP2_PE_ETH_TYPE_UN            (MVPP2_PRS_TCAM_SRAM_SIZE - 6)
632 #define MVPP2_PE_VLAN_DBL               (MVPP2_PRS_TCAM_SRAM_SIZE - 5)
633 #define MVPP2_PE_VLAN_NONE              (MVPP2_PRS_TCAM_SRAM_SIZE - 4)
634 #define MVPP2_PE_MAC_MC_ALL             (MVPP2_PRS_TCAM_SRAM_SIZE - 3)
635 #define MVPP2_PE_MAC_PROMISCUOUS        (MVPP2_PRS_TCAM_SRAM_SIZE - 2)
636 #define MVPP2_PE_MAC_NON_PROMISCUOUS    (MVPP2_PRS_TCAM_SRAM_SIZE - 1)
637
638 /* Sram structure
639  * The fields are represented by MVPP2_PRS_TCAM_DATA_REG(3)->(0).
640  */
641 #define MVPP2_PRS_SRAM_RI_OFFS                  0
642 #define MVPP2_PRS_SRAM_RI_WORD                  0
643 #define MVPP2_PRS_SRAM_RI_CTRL_OFFS             32
644 #define MVPP2_PRS_SRAM_RI_CTRL_WORD             1
645 #define MVPP2_PRS_SRAM_RI_CTRL_BITS             32
646 #define MVPP2_PRS_SRAM_SHIFT_OFFS               64
647 #define MVPP2_PRS_SRAM_SHIFT_SIGN_BIT           72
648 #define MVPP2_PRS_SRAM_UDF_OFFS                 73
649 #define MVPP2_PRS_SRAM_UDF_BITS                 8
650 #define MVPP2_PRS_SRAM_UDF_MASK                 0xff
651 #define MVPP2_PRS_SRAM_UDF_SIGN_BIT             81
652 #define MVPP2_PRS_SRAM_UDF_TYPE_OFFS            82
653 #define MVPP2_PRS_SRAM_UDF_TYPE_MASK            0x7
654 #define MVPP2_PRS_SRAM_UDF_TYPE_L3              1
655 #define MVPP2_PRS_SRAM_UDF_TYPE_L4              4
656 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS        85
657 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK        0x3
658 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD         1
659 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP4_ADD     2
660 #define MVPP2_PRS_SRAM_OP_SEL_SHIFT_IP6_ADD     3
661 #define MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS          87
662 #define MVPP2_PRS_SRAM_OP_SEL_UDF_BITS          2
663 #define MVPP2_PRS_SRAM_OP_SEL_UDF_MASK          0x3
664 #define MVPP2_PRS_SRAM_OP_SEL_UDF_ADD           0
665 #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP4_ADD       2
666 #define MVPP2_PRS_SRAM_OP_SEL_UDF_IP6_ADD       3
667 #define MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS         89
668 #define MVPP2_PRS_SRAM_AI_OFFS                  90
669 #define MVPP2_PRS_SRAM_AI_CTRL_OFFS             98
670 #define MVPP2_PRS_SRAM_AI_CTRL_BITS             8
671 #define MVPP2_PRS_SRAM_AI_MASK                  0xff
672 #define MVPP2_PRS_SRAM_NEXT_LU_OFFS             106
673 #define MVPP2_PRS_SRAM_NEXT_LU_MASK             0xf
674 #define MVPP2_PRS_SRAM_LU_DONE_BIT              110
675 #define MVPP2_PRS_SRAM_LU_GEN_BIT               111
676
677 /* Sram result info bits assignment */
678 #define MVPP2_PRS_RI_MAC_ME_MASK                0x1
679 #define MVPP2_PRS_RI_DSA_MASK                   0x2
680 #define MVPP2_PRS_RI_VLAN_MASK                  (BIT(2) | BIT(3))
681 #define MVPP2_PRS_RI_VLAN_NONE                  0x0
682 #define MVPP2_PRS_RI_VLAN_SINGLE                BIT(2)
683 #define MVPP2_PRS_RI_VLAN_DOUBLE                BIT(3)
684 #define MVPP2_PRS_RI_VLAN_TRIPLE                (BIT(2) | BIT(3))
685 #define MVPP2_PRS_RI_CPU_CODE_MASK              0x70
686 #define MVPP2_PRS_RI_CPU_CODE_RX_SPEC           BIT(4)
687 #define MVPP2_PRS_RI_L2_CAST_MASK               (BIT(9) | BIT(10))
688 #define MVPP2_PRS_RI_L2_UCAST                   0x0
689 #define MVPP2_PRS_RI_L2_MCAST                   BIT(9)
690 #define MVPP2_PRS_RI_L2_BCAST                   BIT(10)
691 #define MVPP2_PRS_RI_PPPOE_MASK                 0x800
692 #define MVPP2_PRS_RI_L3_PROTO_MASK              (BIT(12) | BIT(13) | BIT(14))
693 #define MVPP2_PRS_RI_L3_UN                      0x0
694 #define MVPP2_PRS_RI_L3_IP4                     BIT(12)
695 #define MVPP2_PRS_RI_L3_IP4_OPT                 BIT(13)
696 #define MVPP2_PRS_RI_L3_IP4_OTHER               (BIT(12) | BIT(13))
697 #define MVPP2_PRS_RI_L3_IP6                     BIT(14)
698 #define MVPP2_PRS_RI_L3_IP6_EXT                 (BIT(12) | BIT(14))
699 #define MVPP2_PRS_RI_L3_ARP                     (BIT(13) | BIT(14))
700 #define MVPP2_PRS_RI_L3_ADDR_MASK               (BIT(15) | BIT(16))
701 #define MVPP2_PRS_RI_L3_UCAST                   0x0
702 #define MVPP2_PRS_RI_L3_MCAST                   BIT(15)
703 #define MVPP2_PRS_RI_L3_BCAST                   (BIT(15) | BIT(16))
704 #define MVPP2_PRS_RI_IP_FRAG_MASK               0x20000
705 #define MVPP2_PRS_RI_IP_FRAG_TRUE               BIT(17)
706 #define MVPP2_PRS_RI_UDF3_MASK                  0x300000
707 #define MVPP2_PRS_RI_UDF3_RX_SPECIAL            BIT(21)
708 #define MVPP2_PRS_RI_L4_PROTO_MASK              0x1c00000
709 #define MVPP2_PRS_RI_L4_TCP                     BIT(22)
710 #define MVPP2_PRS_RI_L4_UDP                     BIT(23)
711 #define MVPP2_PRS_RI_L4_OTHER                   (BIT(22) | BIT(23))
712 #define MVPP2_PRS_RI_UDF7_MASK                  0x60000000
713 #define MVPP2_PRS_RI_UDF7_IP6_LITE              BIT(29)
714 #define MVPP2_PRS_RI_DROP_MASK                  0x80000000
715
716 /* Sram additional info bits assignment */
717 #define MVPP2_PRS_IPV4_DIP_AI_BIT               BIT(0)
718 #define MVPP2_PRS_IPV6_NO_EXT_AI_BIT            BIT(0)
719 #define MVPP2_PRS_IPV6_EXT_AI_BIT               BIT(1)
720 #define MVPP2_PRS_IPV6_EXT_AH_AI_BIT            BIT(2)
721 #define MVPP2_PRS_IPV6_EXT_AH_LEN_AI_BIT        BIT(3)
722 #define MVPP2_PRS_IPV6_EXT_AH_L4_AI_BIT         BIT(4)
723 #define MVPP2_PRS_SINGLE_VLAN_AI                0
724 #define MVPP2_PRS_DBL_VLAN_AI_BIT               BIT(7)
725
726 /* DSA/EDSA type */
727 #define MVPP2_PRS_TAGGED                true
728 #define MVPP2_PRS_UNTAGGED              false
729 #define MVPP2_PRS_EDSA                  true
730 #define MVPP2_PRS_DSA                   false
731
732 /* MAC entries, shadow udf */
733 enum mvpp2_prs_udf {
734         MVPP2_PRS_UDF_MAC_DEF,
735         MVPP2_PRS_UDF_MAC_RANGE,
736         MVPP2_PRS_UDF_L2_DEF,
737         MVPP2_PRS_UDF_L2_DEF_COPY,
738         MVPP2_PRS_UDF_L2_USER,
739 };
740
741 /* Lookup ID */
742 enum mvpp2_prs_lookup {
743         MVPP2_PRS_LU_MH,
744         MVPP2_PRS_LU_MAC,
745         MVPP2_PRS_LU_DSA,
746         MVPP2_PRS_LU_VLAN,
747         MVPP2_PRS_LU_L2,
748         MVPP2_PRS_LU_PPPOE,
749         MVPP2_PRS_LU_IP4,
750         MVPP2_PRS_LU_IP6,
751         MVPP2_PRS_LU_FLOWS,
752         MVPP2_PRS_LU_LAST,
753 };
754
755 /* L3 cast enum */
756 enum mvpp2_prs_l3_cast {
757         MVPP2_PRS_L3_UNI_CAST,
758         MVPP2_PRS_L3_MULTI_CAST,
759         MVPP2_PRS_L3_BROAD_CAST
760 };
761
762 /* Classifier constants */
763 #define MVPP2_CLS_FLOWS_TBL_SIZE        512
764 #define MVPP2_CLS_FLOWS_TBL_DATA_WORDS  3
765 #define MVPP2_CLS_LKP_TBL_SIZE          64
766 #define MVPP2_CLS_RX_QUEUES             256
767
768 /* RSS constants */
769 #define MVPP22_RSS_TABLE_ENTRIES        32
770
771 /* BM constants */
772 #define MVPP2_BM_POOLS_NUM              8
773 #define MVPP2_BM_LONG_BUF_NUM           1024
774 #define MVPP2_BM_SHORT_BUF_NUM          2048
775 #define MVPP2_BM_POOL_SIZE_MAX          (16*1024 - MVPP2_BM_POOL_PTR_ALIGN/4)
776 #define MVPP2_BM_POOL_PTR_ALIGN         128
777 #define MVPP2_BM_SWF_LONG_POOL(port)    ((port > 2) ? 2 : port)
778 #define MVPP2_BM_SWF_SHORT_POOL         3
779
780 /* BM cookie (32 bits) definition */
781 #define MVPP2_BM_COOKIE_POOL_OFFS       8
782 #define MVPP2_BM_COOKIE_CPU_OFFS        24
783
784 /* BM short pool packet size
785  * These value assure that for SWF the total number
786  * of bytes allocated for each buffer will be 512
787  */
788 #define MVPP2_BM_SHORT_PKT_SIZE         MVPP2_RX_MAX_PKT_SIZE(512)
789
790 #define MVPP21_ADDR_SPACE_SZ            0
791 #define MVPP22_ADDR_SPACE_SZ            SZ_64K
792
793 #define MVPP2_MAX_THREADS               8
794 #define MVPP2_MAX_QVECS                 MVPP2_MAX_THREADS
795
796 enum mvpp2_bm_type {
797         MVPP2_BM_FREE,
798         MVPP2_BM_SWF_LONG,
799         MVPP2_BM_SWF_SHORT
800 };
801
802 /* GMAC MIB Counters register definitions */
803 #define MVPP21_MIB_COUNTERS_OFFSET              0x1000
804 #define MVPP21_MIB_COUNTERS_PORT_SZ             0x400
805 #define MVPP22_MIB_COUNTERS_OFFSET              0x0
806 #define MVPP22_MIB_COUNTERS_PORT_SZ             0x100
807
808 #define MVPP2_MIB_GOOD_OCTETS_RCVD              0x0
809 #define MVPP2_MIB_BAD_OCTETS_RCVD               0x8
810 #define MVPP2_MIB_CRC_ERRORS_SENT               0xc
811 #define MVPP2_MIB_UNICAST_FRAMES_RCVD           0x10
812 #define MVPP2_MIB_BROADCAST_FRAMES_RCVD         0x18
813 #define MVPP2_MIB_MULTICAST_FRAMES_RCVD         0x1c
814 #define MVPP2_MIB_FRAMES_64_OCTETS              0x20
815 #define MVPP2_MIB_FRAMES_65_TO_127_OCTETS       0x24
816 #define MVPP2_MIB_FRAMES_128_TO_255_OCTETS      0x28
817 #define MVPP2_MIB_FRAMES_256_TO_511_OCTETS      0x2c
818 #define MVPP2_MIB_FRAMES_512_TO_1023_OCTETS     0x30
819 #define MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS     0x34
820 #define MVPP2_MIB_GOOD_OCTETS_SENT              0x38
821 #define MVPP2_MIB_UNICAST_FRAMES_SENT           0x40
822 #define MVPP2_MIB_MULTICAST_FRAMES_SENT         0x48
823 #define MVPP2_MIB_BROADCAST_FRAMES_SENT         0x4c
824 #define MVPP2_MIB_FC_SENT                       0x54
825 #define MVPP2_MIB_FC_RCVD                       0x58
826 #define MVPP2_MIB_RX_FIFO_OVERRUN               0x5c
827 #define MVPP2_MIB_UNDERSIZE_RCVD                0x60
828 #define MVPP2_MIB_FRAGMENTS_RCVD                0x64
829 #define MVPP2_MIB_OVERSIZE_RCVD                 0x68
830 #define MVPP2_MIB_JABBER_RCVD                   0x6c
831 #define MVPP2_MIB_MAC_RCV_ERROR                 0x70
832 #define MVPP2_MIB_BAD_CRC_EVENT                 0x74
833 #define MVPP2_MIB_COLLISION                     0x78
834 #define MVPP2_MIB_LATE_COLLISION                0x7c
835
836 #define MVPP2_MIB_COUNTERS_STATS_DELAY          (1 * HZ)
837
838 /* Definitions */
839
840 /* Shared Packet Processor resources */
841 struct mvpp2 {
842         /* Shared registers' base addresses */
843         void __iomem *lms_base;
844         void __iomem *iface_base;
845
846         /* On PPv2.2, each "software thread" can access the base
847          * register through a separate address space, each 64 KB apart
848          * from each other. Typically, such address spaces will be
849          * used per CPU.
850          */
851         void __iomem *swth_base[MVPP2_MAX_THREADS];
852
853         /* On PPv2.2, some port control registers are located into the system
854          * controller space. These registers are accessible through a regmap.
855          */
856         struct regmap *sysctrl_base;
857
858         /* Common clocks */
859         struct clk *pp_clk;
860         struct clk *gop_clk;
861         struct clk *mg_clk;
862         struct clk *axi_clk;
863
864         /* List of pointers to port structures */
865         int port_count;
866         struct mvpp2_port **port_list;
867
868         /* Aggregated TXQs */
869         struct mvpp2_tx_queue *aggr_txqs;
870
871         /* BM pools */
872         struct mvpp2_bm_pool *bm_pools;
873
874         /* PRS shadow table */
875         struct mvpp2_prs_shadow *prs_shadow;
876         /* PRS auxiliary table for double vlan entries control */
877         bool *prs_double_vlans;
878
879         /* Tclk value */
880         u32 tclk;
881
882         /* HW version */
883         enum { MVPP21, MVPP22 } hw_version;
884
885         /* Maximum number of RXQs per port */
886         unsigned int max_port_rxqs;
887
888         /* Workqueue to gather hardware statistics */
889         char queue_name[30];
890         struct workqueue_struct *stats_queue;
891 };
892
893 struct mvpp2_pcpu_stats {
894         struct  u64_stats_sync syncp;
895         u64     rx_packets;
896         u64     rx_bytes;
897         u64     tx_packets;
898         u64     tx_bytes;
899 };
900
901 /* Per-CPU port control */
902 struct mvpp2_port_pcpu {
903         struct hrtimer tx_done_timer;
904         bool timer_scheduled;
905         /* Tasklet for egress finalization */
906         struct tasklet_struct tx_done_tasklet;
907 };
908
909 struct mvpp2_queue_vector {
910         int irq;
911         struct napi_struct napi;
912         enum { MVPP2_QUEUE_VECTOR_SHARED, MVPP2_QUEUE_VECTOR_PRIVATE } type;
913         int sw_thread_id;
914         u16 sw_thread_mask;
915         int first_rxq;
916         int nrxqs;
917         u32 pending_cause_rx;
918         struct mvpp2_port *port;
919 };
920
921 struct mvpp2_port {
922         u8 id;
923
924         /* Index of the port from the "group of ports" complex point
925          * of view
926          */
927         int gop_id;
928
929         int link_irq;
930
931         struct mvpp2 *priv;
932
933         /* Per-port registers' base address */
934         void __iomem *base;
935         void __iomem *stats_base;
936
937         struct mvpp2_rx_queue **rxqs;
938         unsigned int nrxqs;
939         struct mvpp2_tx_queue **txqs;
940         unsigned int ntxqs;
941         struct net_device *dev;
942
943         int pkt_size;
944
945         /* Per-CPU port control */
946         struct mvpp2_port_pcpu __percpu *pcpu;
947
948         /* Flags */
949         unsigned long flags;
950
951         u16 tx_ring_size;
952         u16 rx_ring_size;
953         struct mvpp2_pcpu_stats __percpu *stats;
954         u64 *ethtool_stats;
955
956         /* Per-port work and its lock to gather hardware statistics */
957         struct mutex gather_stats_lock;
958         struct delayed_work stats_work;
959
960         phy_interface_t phy_interface;
961         struct device_node *phy_node;
962         struct phy *comphy;
963         unsigned int link;
964         unsigned int duplex;
965         unsigned int speed;
966
967         struct mvpp2_bm_pool *pool_long;
968         struct mvpp2_bm_pool *pool_short;
969
970         /* Index of first port's physical RXQ */
971         u8 first_rxq;
972
973         struct mvpp2_queue_vector qvecs[MVPP2_MAX_QVECS];
974         unsigned int nqvecs;
975         bool has_tx_irqs;
976
977         u32 tx_time_coal;
978 };
979
980 /* The mvpp2_tx_desc and mvpp2_rx_desc structures describe the
981  * layout of the transmit and reception DMA descriptors, and their
982  * layout is therefore defined by the hardware design
983  */
984
985 #define MVPP2_TXD_L3_OFF_SHIFT          0
986 #define MVPP2_TXD_IP_HLEN_SHIFT         8
987 #define MVPP2_TXD_L4_CSUM_FRAG          BIT(13)
988 #define MVPP2_TXD_L4_CSUM_NOT           BIT(14)
989 #define MVPP2_TXD_IP_CSUM_DISABLE       BIT(15)
990 #define MVPP2_TXD_PADDING_DISABLE       BIT(23)
991 #define MVPP2_TXD_L4_UDP                BIT(24)
992 #define MVPP2_TXD_L3_IP6                BIT(26)
993 #define MVPP2_TXD_L_DESC                BIT(28)
994 #define MVPP2_TXD_F_DESC                BIT(29)
995
996 #define MVPP2_RXD_ERR_SUMMARY           BIT(15)
997 #define MVPP2_RXD_ERR_CODE_MASK         (BIT(13) | BIT(14))
998 #define MVPP2_RXD_ERR_CRC               0x0
999 #define MVPP2_RXD_ERR_OVERRUN           BIT(13)
1000 #define MVPP2_RXD_ERR_RESOURCE          (BIT(13) | BIT(14))
1001 #define MVPP2_RXD_BM_POOL_ID_OFFS       16
1002 #define MVPP2_RXD_BM_POOL_ID_MASK       (BIT(16) | BIT(17) | BIT(18))
1003 #define MVPP2_RXD_HWF_SYNC              BIT(21)
1004 #define MVPP2_RXD_L4_CSUM_OK            BIT(22)
1005 #define MVPP2_RXD_IP4_HEADER_ERR        BIT(24)
1006 #define MVPP2_RXD_L4_TCP                BIT(25)
1007 #define MVPP2_RXD_L4_UDP                BIT(26)
1008 #define MVPP2_RXD_L3_IP4                BIT(28)
1009 #define MVPP2_RXD_L3_IP6                BIT(30)
1010 #define MVPP2_RXD_BUF_HDR               BIT(31)
1011
1012 /* HW TX descriptor for PPv2.1 */
1013 struct mvpp21_tx_desc {
1014         u32 command;            /* Options used by HW for packet transmitting.*/
1015         u8  packet_offset;      /* the offset from the buffer beginning */
1016         u8  phys_txq;           /* destination queue ID                 */
1017         u16 data_size;          /* data size of transmitted packet in bytes */
1018         u32 buf_dma_addr;       /* physical addr of transmitted buffer  */
1019         u32 buf_cookie;         /* cookie for access to TX buffer in tx path */
1020         u32 reserved1[3];       /* hw_cmd (for future use, BM, PON, PNC) */
1021         u32 reserved2;          /* reserved (for future use)            */
1022 };
1023
1024 /* HW RX descriptor for PPv2.1 */
1025 struct mvpp21_rx_desc {
1026         u32 status;             /* info about received packet           */
1027         u16 reserved1;          /* parser_info (for future use, PnC)    */
1028         u16 data_size;          /* size of received packet in bytes     */
1029         u32 buf_dma_addr;       /* physical address of the buffer       */
1030         u32 buf_cookie;         /* cookie for access to RX buffer in rx path */
1031         u16 reserved2;          /* gem_port_id (for future use, PON)    */
1032         u16 reserved3;          /* csum_l4 (for future use, PnC)        */
1033         u8  reserved4;          /* bm_qset (for future use, BM)         */
1034         u8  reserved5;
1035         u16 reserved6;          /* classify_info (for future use, PnC)  */
1036         u32 reserved7;          /* flow_id (for future use, PnC) */
1037         u32 reserved8;
1038 };
1039
1040 /* HW TX descriptor for PPv2.2 */
1041 struct mvpp22_tx_desc {
1042         u32 command;
1043         u8  packet_offset;
1044         u8  phys_txq;
1045         u16 data_size;
1046         u64 reserved1;
1047         u64 buf_dma_addr_ptp;
1048         u64 buf_cookie_misc;
1049 };
1050
1051 /* HW RX descriptor for PPv2.2 */
1052 struct mvpp22_rx_desc {
1053         u32 status;
1054         u16 reserved1;
1055         u16 data_size;
1056         u32 reserved2;
1057         u32 reserved3;
1058         u64 buf_dma_addr_key_hash;
1059         u64 buf_cookie_misc;
1060 };
1061
1062 /* Opaque type used by the driver to manipulate the HW TX and RX
1063  * descriptors
1064  */
1065 struct mvpp2_tx_desc {
1066         union {
1067                 struct mvpp21_tx_desc pp21;
1068                 struct mvpp22_tx_desc pp22;
1069         };
1070 };
1071
1072 struct mvpp2_rx_desc {
1073         union {
1074                 struct mvpp21_rx_desc pp21;
1075                 struct mvpp22_rx_desc pp22;
1076         };
1077 };
1078
1079 struct mvpp2_txq_pcpu_buf {
1080         /* Transmitted SKB */
1081         struct sk_buff *skb;
1082
1083         /* Physical address of transmitted buffer */
1084         dma_addr_t dma;
1085
1086         /* Size transmitted */
1087         size_t size;
1088 };
1089
1090 /* Per-CPU Tx queue control */
1091 struct mvpp2_txq_pcpu {
1092         int cpu;
1093
1094         /* Number of Tx DMA descriptors in the descriptor ring */
1095         int size;
1096
1097         /* Number of currently used Tx DMA descriptor in the
1098          * descriptor ring
1099          */
1100         int count;
1101
1102         int wake_threshold;
1103         int stop_threshold;
1104
1105         /* Number of Tx DMA descriptors reserved for each CPU */
1106         int reserved_num;
1107
1108         /* Infos about transmitted buffers */
1109         struct mvpp2_txq_pcpu_buf *buffs;
1110
1111         /* Index of last TX DMA descriptor that was inserted */
1112         int txq_put_index;
1113
1114         /* Index of the TX DMA descriptor to be cleaned up */
1115         int txq_get_index;
1116
1117         /* DMA buffer for TSO headers */
1118         char *tso_headers;
1119         dma_addr_t tso_headers_dma;
1120 };
1121
1122 struct mvpp2_tx_queue {
1123         /* Physical number of this Tx queue */
1124         u8 id;
1125
1126         /* Logical number of this Tx queue */
1127         u8 log_id;
1128
1129         /* Number of Tx DMA descriptors in the descriptor ring */
1130         int size;
1131
1132         /* Number of currently used Tx DMA descriptor in the descriptor ring */
1133         int count;
1134
1135         /* Per-CPU control of physical Tx queues */
1136         struct mvpp2_txq_pcpu __percpu *pcpu;
1137
1138         u32 done_pkts_coal;
1139
1140         /* Virtual address of thex Tx DMA descriptors array */
1141         struct mvpp2_tx_desc *descs;
1142
1143         /* DMA address of the Tx DMA descriptors array */
1144         dma_addr_t descs_dma;
1145
1146         /* Index of the last Tx DMA descriptor */
1147         int last_desc;
1148
1149         /* Index of the next Tx DMA descriptor to process */
1150         int next_desc_to_proc;
1151 };
1152
1153 struct mvpp2_rx_queue {
1154         /* RX queue number, in the range 0-31 for physical RXQs */
1155         u8 id;
1156
1157         /* Num of rx descriptors in the rx descriptor ring */
1158         int size;
1159
1160         u32 pkts_coal;
1161         u32 time_coal;
1162
1163         /* Virtual address of the RX DMA descriptors array */
1164         struct mvpp2_rx_desc *descs;
1165
1166         /* DMA address of the RX DMA descriptors array */
1167         dma_addr_t descs_dma;
1168
1169         /* Index of the last RX DMA descriptor */
1170         int last_desc;
1171
1172         /* Index of the next RX DMA descriptor to process */
1173         int next_desc_to_proc;
1174
1175         /* ID of port to which physical RXQ is mapped */
1176         int port;
1177
1178         /* Port's logic RXQ number to which physical RXQ is mapped */
1179         int logic_rxq;
1180 };
1181
1182 union mvpp2_prs_tcam_entry {
1183         u32 word[MVPP2_PRS_TCAM_WORDS];
1184         u8  byte[MVPP2_PRS_TCAM_WORDS * 4];
1185 };
1186
1187 union mvpp2_prs_sram_entry {
1188         u32 word[MVPP2_PRS_SRAM_WORDS];
1189         u8  byte[MVPP2_PRS_SRAM_WORDS * 4];
1190 };
1191
1192 struct mvpp2_prs_entry {
1193         u32 index;
1194         union mvpp2_prs_tcam_entry tcam;
1195         union mvpp2_prs_sram_entry sram;
1196 };
1197
1198 struct mvpp2_prs_shadow {
1199         bool valid;
1200         bool finish;
1201
1202         /* Lookup ID */
1203         int lu;
1204
1205         /* User defined offset */
1206         int udf;
1207
1208         /* Result info */
1209         u32 ri;
1210         u32 ri_mask;
1211 };
1212
1213 struct mvpp2_cls_flow_entry {
1214         u32 index;
1215         u32 data[MVPP2_CLS_FLOWS_TBL_DATA_WORDS];
1216 };
1217
1218 struct mvpp2_cls_lookup_entry {
1219         u32 lkpid;
1220         u32 way;
1221         u32 data;
1222 };
1223
1224 struct mvpp2_bm_pool {
1225         /* Pool number in the range 0-7 */
1226         int id;
1227         enum mvpp2_bm_type type;
1228
1229         /* Buffer Pointers Pool External (BPPE) size */
1230         int size;
1231         /* BPPE size in bytes */
1232         int size_bytes;
1233         /* Number of buffers for this pool */
1234         int buf_num;
1235         /* Pool buffer size */
1236         int buf_size;
1237         /* Packet size */
1238         int pkt_size;
1239         int frag_size;
1240
1241         /* BPPE virtual base address */
1242         u32 *virt_addr;
1243         /* BPPE DMA base address */
1244         dma_addr_t dma_addr;
1245
1246         /* Ports using BM pool */
1247         u32 port_map;
1248 };
1249
1250 #define IS_TSO_HEADER(txq_pcpu, addr) \
1251         ((addr) >= (txq_pcpu)->tso_headers_dma && \
1252          (addr) < (txq_pcpu)->tso_headers_dma + \
1253          (txq_pcpu)->size * TSO_HEADER_SIZE)
1254
1255 /* Queue modes */
1256 #define MVPP2_QDIST_SINGLE_MODE 0
1257 #define MVPP2_QDIST_MULTI_MODE  1
1258
1259 static int queue_mode = MVPP2_QDIST_SINGLE_MODE;
1260
1261 module_param(queue_mode, int, 0444);
1262 MODULE_PARM_DESC(queue_mode, "Set queue_mode (single=0, multi=1)");
1263
1264 #define MVPP2_DRIVER_NAME "mvpp2"
1265 #define MVPP2_DRIVER_VERSION "1.0"
1266
1267 /* Utility/helper methods */
1268
1269 static void mvpp2_write(struct mvpp2 *priv, u32 offset, u32 data)
1270 {
1271         writel(data, priv->swth_base[0] + offset);
1272 }
1273
1274 static u32 mvpp2_read(struct mvpp2 *priv, u32 offset)
1275 {
1276         return readl(priv->swth_base[0] + offset);
1277 }
1278
1279 /* These accessors should be used to access:
1280  *
1281  * - per-CPU registers, where each CPU has its own copy of the
1282  *   register.
1283  *
1284  *   MVPP2_BM_VIRT_ALLOC_REG
1285  *   MVPP2_BM_ADDR_HIGH_ALLOC
1286  *   MVPP22_BM_ADDR_HIGH_RLS_REG
1287  *   MVPP2_BM_VIRT_RLS_REG
1288  *   MVPP2_ISR_RX_TX_CAUSE_REG
1289  *   MVPP2_ISR_RX_TX_MASK_REG
1290  *   MVPP2_TXQ_NUM_REG
1291  *   MVPP2_AGGR_TXQ_UPDATE_REG
1292  *   MVPP2_TXQ_RSVD_REQ_REG
1293  *   MVPP2_TXQ_RSVD_RSLT_REG
1294  *   MVPP2_TXQ_SENT_REG
1295  *   MVPP2_RXQ_NUM_REG
1296  *
1297  * - global registers that must be accessed through a specific CPU
1298  *   window, because they are related to an access to a per-CPU
1299  *   register
1300  *
1301  *   MVPP2_BM_PHY_ALLOC_REG    (related to MVPP2_BM_VIRT_ALLOC_REG)
1302  *   MVPP2_BM_PHY_RLS_REG      (related to MVPP2_BM_VIRT_RLS_REG)
1303  *   MVPP2_RXQ_THRESH_REG      (related to MVPP2_RXQ_NUM_REG)
1304  *   MVPP2_RXQ_DESC_ADDR_REG   (related to MVPP2_RXQ_NUM_REG)
1305  *   MVPP2_RXQ_DESC_SIZE_REG   (related to MVPP2_RXQ_NUM_REG)
1306  *   MVPP2_RXQ_INDEX_REG       (related to MVPP2_RXQ_NUM_REG)
1307  *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
1308  *   MVPP2_TXQ_DESC_ADDR_REG   (related to MVPP2_TXQ_NUM_REG)
1309  *   MVPP2_TXQ_DESC_SIZE_REG   (related to MVPP2_TXQ_NUM_REG)
1310  *   MVPP2_TXQ_INDEX_REG       (related to MVPP2_TXQ_NUM_REG)
1311  *   MVPP2_TXQ_PENDING_REG     (related to MVPP2_TXQ_NUM_REG)
1312  *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
1313  *   MVPP2_TXQ_PREF_BUF_REG    (related to MVPP2_TXQ_NUM_REG)
1314  */
1315 static void mvpp2_percpu_write(struct mvpp2 *priv, int cpu,
1316                                u32 offset, u32 data)
1317 {
1318         writel(data, priv->swth_base[cpu] + offset);
1319 }
1320
1321 static u32 mvpp2_percpu_read(struct mvpp2 *priv, int cpu,
1322                              u32 offset)
1323 {
1324         return readl(priv->swth_base[cpu] + offset);
1325 }
1326
1327 static dma_addr_t mvpp2_txdesc_dma_addr_get(struct mvpp2_port *port,
1328                                             struct mvpp2_tx_desc *tx_desc)
1329 {
1330         if (port->priv->hw_version == MVPP21)
1331                 return tx_desc->pp21.buf_dma_addr;
1332         else
1333                 return tx_desc->pp22.buf_dma_addr_ptp & GENMASK_ULL(40, 0);
1334 }
1335
1336 static void mvpp2_txdesc_dma_addr_set(struct mvpp2_port *port,
1337                                       struct mvpp2_tx_desc *tx_desc,
1338                                       dma_addr_t dma_addr)
1339 {
1340         dma_addr_t addr, offset;
1341
1342         addr = dma_addr & ~MVPP2_TX_DESC_ALIGN;
1343         offset = dma_addr & MVPP2_TX_DESC_ALIGN;
1344
1345         if (port->priv->hw_version == MVPP21) {
1346                 tx_desc->pp21.buf_dma_addr = addr;
1347                 tx_desc->pp21.packet_offset = offset;
1348         } else {
1349                 u64 val = (u64)addr;
1350
1351                 tx_desc->pp22.buf_dma_addr_ptp &= ~GENMASK_ULL(40, 0);
1352                 tx_desc->pp22.buf_dma_addr_ptp |= val;
1353                 tx_desc->pp22.packet_offset = offset;
1354         }
1355 }
1356
1357 static size_t mvpp2_txdesc_size_get(struct mvpp2_port *port,
1358                                     struct mvpp2_tx_desc *tx_desc)
1359 {
1360         if (port->priv->hw_version == MVPP21)
1361                 return tx_desc->pp21.data_size;
1362         else
1363                 return tx_desc->pp22.data_size;
1364 }
1365
1366 static void mvpp2_txdesc_size_set(struct mvpp2_port *port,
1367                                   struct mvpp2_tx_desc *tx_desc,
1368                                   size_t size)
1369 {
1370         if (port->priv->hw_version == MVPP21)
1371                 tx_desc->pp21.data_size = size;
1372         else
1373                 tx_desc->pp22.data_size = size;
1374 }
1375
1376 static void mvpp2_txdesc_txq_set(struct mvpp2_port *port,
1377                                  struct mvpp2_tx_desc *tx_desc,
1378                                  unsigned int txq)
1379 {
1380         if (port->priv->hw_version == MVPP21)
1381                 tx_desc->pp21.phys_txq = txq;
1382         else
1383                 tx_desc->pp22.phys_txq = txq;
1384 }
1385
1386 static void mvpp2_txdesc_cmd_set(struct mvpp2_port *port,
1387                                  struct mvpp2_tx_desc *tx_desc,
1388                                  unsigned int command)
1389 {
1390         if (port->priv->hw_version == MVPP21)
1391                 tx_desc->pp21.command = command;
1392         else
1393                 tx_desc->pp22.command = command;
1394 }
1395
1396 static unsigned int mvpp2_txdesc_offset_get(struct mvpp2_port *port,
1397                                             struct mvpp2_tx_desc *tx_desc)
1398 {
1399         if (port->priv->hw_version == MVPP21)
1400                 return tx_desc->pp21.packet_offset;
1401         else
1402                 return tx_desc->pp22.packet_offset;
1403 }
1404
1405 static dma_addr_t mvpp2_rxdesc_dma_addr_get(struct mvpp2_port *port,
1406                                             struct mvpp2_rx_desc *rx_desc)
1407 {
1408         if (port->priv->hw_version == MVPP21)
1409                 return rx_desc->pp21.buf_dma_addr;
1410         else
1411                 return rx_desc->pp22.buf_dma_addr_key_hash & GENMASK_ULL(40, 0);
1412 }
1413
1414 static unsigned long mvpp2_rxdesc_cookie_get(struct mvpp2_port *port,
1415                                              struct mvpp2_rx_desc *rx_desc)
1416 {
1417         if (port->priv->hw_version == MVPP21)
1418                 return rx_desc->pp21.buf_cookie;
1419         else
1420                 return rx_desc->pp22.buf_cookie_misc & GENMASK_ULL(40, 0);
1421 }
1422
1423 static size_t mvpp2_rxdesc_size_get(struct mvpp2_port *port,
1424                                     struct mvpp2_rx_desc *rx_desc)
1425 {
1426         if (port->priv->hw_version == MVPP21)
1427                 return rx_desc->pp21.data_size;
1428         else
1429                 return rx_desc->pp22.data_size;
1430 }
1431
1432 static u32 mvpp2_rxdesc_status_get(struct mvpp2_port *port,
1433                                    struct mvpp2_rx_desc *rx_desc)
1434 {
1435         if (port->priv->hw_version == MVPP21)
1436                 return rx_desc->pp21.status;
1437         else
1438                 return rx_desc->pp22.status;
1439 }
1440
1441 static void mvpp2_txq_inc_get(struct mvpp2_txq_pcpu *txq_pcpu)
1442 {
1443         txq_pcpu->txq_get_index++;
1444         if (txq_pcpu->txq_get_index == txq_pcpu->size)
1445                 txq_pcpu->txq_get_index = 0;
1446 }
1447
1448 static void mvpp2_txq_inc_put(struct mvpp2_port *port,
1449                               struct mvpp2_txq_pcpu *txq_pcpu,
1450                               struct sk_buff *skb,
1451                               struct mvpp2_tx_desc *tx_desc)
1452 {
1453         struct mvpp2_txq_pcpu_buf *tx_buf =
1454                 txq_pcpu->buffs + txq_pcpu->txq_put_index;
1455         tx_buf->skb = skb;
1456         tx_buf->size = mvpp2_txdesc_size_get(port, tx_desc);
1457         tx_buf->dma = mvpp2_txdesc_dma_addr_get(port, tx_desc) +
1458                 mvpp2_txdesc_offset_get(port, tx_desc);
1459         txq_pcpu->txq_put_index++;
1460         if (txq_pcpu->txq_put_index == txq_pcpu->size)
1461                 txq_pcpu->txq_put_index = 0;
1462 }
1463
1464 /* Get number of physical egress port */
1465 static inline int mvpp2_egress_port(struct mvpp2_port *port)
1466 {
1467         return MVPP2_MAX_TCONT + port->id;
1468 }
1469
1470 /* Get number of physical TXQ */
1471 static inline int mvpp2_txq_phys(int port, int txq)
1472 {
1473         return (MVPP2_MAX_TCONT + port) * MVPP2_MAX_TXQ + txq;
1474 }
1475
1476 /* Parser configuration routines */
1477
1478 /* Update parser tcam and sram hw entries */
1479 static int mvpp2_prs_hw_write(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1480 {
1481         int i;
1482
1483         if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1484                 return -EINVAL;
1485
1486         /* Clear entry invalidation bit */
1487         pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] &= ~MVPP2_PRS_TCAM_INV_MASK;
1488
1489         /* Write tcam index - indirect access */
1490         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1491         for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1492                 mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), pe->tcam.word[i]);
1493
1494         /* Write sram index - indirect access */
1495         mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1496         for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1497                 mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), pe->sram.word[i]);
1498
1499         return 0;
1500 }
1501
1502 /* Read tcam entry from hw */
1503 static int mvpp2_prs_hw_read(struct mvpp2 *priv, struct mvpp2_prs_entry *pe)
1504 {
1505         int i;
1506
1507         if (pe->index > MVPP2_PRS_TCAM_SRAM_SIZE - 1)
1508                 return -EINVAL;
1509
1510         /* Write tcam index - indirect access */
1511         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, pe->index);
1512
1513         pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] = mvpp2_read(priv,
1514                               MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD));
1515         if (pe->tcam.word[MVPP2_PRS_TCAM_INV_WORD] & MVPP2_PRS_TCAM_INV_MASK)
1516                 return MVPP2_PRS_TCAM_ENTRY_INVALID;
1517
1518         for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
1519                 pe->tcam.word[i] = mvpp2_read(priv, MVPP2_PRS_TCAM_DATA_REG(i));
1520
1521         /* Write sram index - indirect access */
1522         mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, pe->index);
1523         for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
1524                 pe->sram.word[i] = mvpp2_read(priv, MVPP2_PRS_SRAM_DATA_REG(i));
1525
1526         return 0;
1527 }
1528
1529 /* Invalidate tcam hw entry */
1530 static void mvpp2_prs_hw_inv(struct mvpp2 *priv, int index)
1531 {
1532         /* Write index - indirect access */
1533         mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
1534         mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(MVPP2_PRS_TCAM_INV_WORD),
1535                     MVPP2_PRS_TCAM_INV_MASK);
1536 }
1537
1538 /* Enable shadow table entry and set its lookup ID */
1539 static void mvpp2_prs_shadow_set(struct mvpp2 *priv, int index, int lu)
1540 {
1541         priv->prs_shadow[index].valid = true;
1542         priv->prs_shadow[index].lu = lu;
1543 }
1544
1545 /* Update ri fields in shadow table entry */
1546 static void mvpp2_prs_shadow_ri_set(struct mvpp2 *priv, int index,
1547                                     unsigned int ri, unsigned int ri_mask)
1548 {
1549         priv->prs_shadow[index].ri_mask = ri_mask;
1550         priv->prs_shadow[index].ri = ri;
1551 }
1552
1553 /* Update lookup field in tcam sw entry */
1554 static void mvpp2_prs_tcam_lu_set(struct mvpp2_prs_entry *pe, unsigned int lu)
1555 {
1556         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_LU_BYTE);
1557
1558         pe->tcam.byte[MVPP2_PRS_TCAM_LU_BYTE] = lu;
1559         pe->tcam.byte[enable_off] = MVPP2_PRS_LU_MASK;
1560 }
1561
1562 /* Update mask for single port in tcam sw entry */
1563 static void mvpp2_prs_tcam_port_set(struct mvpp2_prs_entry *pe,
1564                                     unsigned int port, bool add)
1565 {
1566         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1567
1568         if (add)
1569                 pe->tcam.byte[enable_off] &= ~(1 << port);
1570         else
1571                 pe->tcam.byte[enable_off] |= 1 << port;
1572 }
1573
1574 /* Update port map in tcam sw entry */
1575 static void mvpp2_prs_tcam_port_map_set(struct mvpp2_prs_entry *pe,
1576                                         unsigned int ports)
1577 {
1578         unsigned char port_mask = MVPP2_PRS_PORT_MASK;
1579         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1580
1581         pe->tcam.byte[MVPP2_PRS_TCAM_PORT_BYTE] = 0;
1582         pe->tcam.byte[enable_off] &= ~port_mask;
1583         pe->tcam.byte[enable_off] |= ~ports & MVPP2_PRS_PORT_MASK;
1584 }
1585
1586 /* Obtain port map from tcam sw entry */
1587 static unsigned int mvpp2_prs_tcam_port_map_get(struct mvpp2_prs_entry *pe)
1588 {
1589         int enable_off = MVPP2_PRS_TCAM_EN_OFFS(MVPP2_PRS_TCAM_PORT_BYTE);
1590
1591         return ~(pe->tcam.byte[enable_off]) & MVPP2_PRS_PORT_MASK;
1592 }
1593
1594 /* Set byte of data and its enable bits in tcam sw entry */
1595 static void mvpp2_prs_tcam_data_byte_set(struct mvpp2_prs_entry *pe,
1596                                          unsigned int offs, unsigned char byte,
1597                                          unsigned char enable)
1598 {
1599         pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)] = byte;
1600         pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)] = enable;
1601 }
1602
1603 /* Get byte of data and its enable bits from tcam sw entry */
1604 static void mvpp2_prs_tcam_data_byte_get(struct mvpp2_prs_entry *pe,
1605                                          unsigned int offs, unsigned char *byte,
1606                                          unsigned char *enable)
1607 {
1608         *byte = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(offs)];
1609         *enable = pe->tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(offs)];
1610 }
1611
1612 /* Compare tcam data bytes with a pattern */
1613 static bool mvpp2_prs_tcam_data_cmp(struct mvpp2_prs_entry *pe, int offs,
1614                                     u16 data)
1615 {
1616         int off = MVPP2_PRS_TCAM_DATA_BYTE(offs);
1617         u16 tcam_data;
1618
1619         tcam_data = (pe->tcam.byte[off + 1] << 8) | pe->tcam.byte[off];
1620         if (tcam_data != data)
1621                 return false;
1622         return true;
1623 }
1624
1625 /* Update ai bits in tcam sw entry */
1626 static void mvpp2_prs_tcam_ai_update(struct mvpp2_prs_entry *pe,
1627                                      unsigned int bits, unsigned int enable)
1628 {
1629         int i, ai_idx = MVPP2_PRS_TCAM_AI_BYTE;
1630
1631         for (i = 0; i < MVPP2_PRS_AI_BITS; i++) {
1632
1633                 if (!(enable & BIT(i)))
1634                         continue;
1635
1636                 if (bits & BIT(i))
1637                         pe->tcam.byte[ai_idx] |= 1 << i;
1638                 else
1639                         pe->tcam.byte[ai_idx] &= ~(1 << i);
1640         }
1641
1642         pe->tcam.byte[MVPP2_PRS_TCAM_EN_OFFS(ai_idx)] |= enable;
1643 }
1644
1645 /* Get ai bits from tcam sw entry */
1646 static int mvpp2_prs_tcam_ai_get(struct mvpp2_prs_entry *pe)
1647 {
1648         return pe->tcam.byte[MVPP2_PRS_TCAM_AI_BYTE];
1649 }
1650
1651 /* Set ethertype in tcam sw entry */
1652 static void mvpp2_prs_match_etype(struct mvpp2_prs_entry *pe, int offset,
1653                                   unsigned short ethertype)
1654 {
1655         mvpp2_prs_tcam_data_byte_set(pe, offset + 0, ethertype >> 8, 0xff);
1656         mvpp2_prs_tcam_data_byte_set(pe, offset + 1, ethertype & 0xff, 0xff);
1657 }
1658
1659 /* Set bits in sram sw entry */
1660 static void mvpp2_prs_sram_bits_set(struct mvpp2_prs_entry *pe, int bit_num,
1661                                     int val)
1662 {
1663         pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] |= (val << (bit_num % 8));
1664 }
1665
1666 /* Clear bits in sram sw entry */
1667 static void mvpp2_prs_sram_bits_clear(struct mvpp2_prs_entry *pe, int bit_num,
1668                                       int val)
1669 {
1670         pe->sram.byte[MVPP2_BIT_TO_BYTE(bit_num)] &= ~(val << (bit_num % 8));
1671 }
1672
1673 /* Update ri bits in sram sw entry */
1674 static void mvpp2_prs_sram_ri_update(struct mvpp2_prs_entry *pe,
1675                                      unsigned int bits, unsigned int mask)
1676 {
1677         unsigned int i;
1678
1679         for (i = 0; i < MVPP2_PRS_SRAM_RI_CTRL_BITS; i++) {
1680                 int ri_off = MVPP2_PRS_SRAM_RI_OFFS;
1681
1682                 if (!(mask & BIT(i)))
1683                         continue;
1684
1685                 if (bits & BIT(i))
1686                         mvpp2_prs_sram_bits_set(pe, ri_off + i, 1);
1687                 else
1688                         mvpp2_prs_sram_bits_clear(pe, ri_off + i, 1);
1689
1690                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_RI_CTRL_OFFS + i, 1);
1691         }
1692 }
1693
1694 /* Obtain ri bits from sram sw entry */
1695 static int mvpp2_prs_sram_ri_get(struct mvpp2_prs_entry *pe)
1696 {
1697         return pe->sram.word[MVPP2_PRS_SRAM_RI_WORD];
1698 }
1699
1700 /* Update ai bits in sram sw entry */
1701 static void mvpp2_prs_sram_ai_update(struct mvpp2_prs_entry *pe,
1702                                      unsigned int bits, unsigned int mask)
1703 {
1704         unsigned int i;
1705         int ai_off = MVPP2_PRS_SRAM_AI_OFFS;
1706
1707         for (i = 0; i < MVPP2_PRS_SRAM_AI_CTRL_BITS; i++) {
1708
1709                 if (!(mask & BIT(i)))
1710                         continue;
1711
1712                 if (bits & BIT(i))
1713                         mvpp2_prs_sram_bits_set(pe, ai_off + i, 1);
1714                 else
1715                         mvpp2_prs_sram_bits_clear(pe, ai_off + i, 1);
1716
1717                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_AI_CTRL_OFFS + i, 1);
1718         }
1719 }
1720
1721 /* Read ai bits from sram sw entry */
1722 static int mvpp2_prs_sram_ai_get(struct mvpp2_prs_entry *pe)
1723 {
1724         u8 bits;
1725         int ai_off = MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_AI_OFFS);
1726         int ai_en_off = ai_off + 1;
1727         int ai_shift = MVPP2_PRS_SRAM_AI_OFFS % 8;
1728
1729         bits = (pe->sram.byte[ai_off] >> ai_shift) |
1730                (pe->sram.byte[ai_en_off] << (8 - ai_shift));
1731
1732         return bits;
1733 }
1734
1735 /* In sram sw entry set lookup ID field of the tcam key to be used in the next
1736  * lookup interation
1737  */
1738 static void mvpp2_prs_sram_next_lu_set(struct mvpp2_prs_entry *pe,
1739                                        unsigned int lu)
1740 {
1741         int sram_next_off = MVPP2_PRS_SRAM_NEXT_LU_OFFS;
1742
1743         mvpp2_prs_sram_bits_clear(pe, sram_next_off,
1744                                   MVPP2_PRS_SRAM_NEXT_LU_MASK);
1745         mvpp2_prs_sram_bits_set(pe, sram_next_off, lu);
1746 }
1747
1748 /* In the sram sw entry set sign and value of the next lookup offset
1749  * and the offset value generated to the classifier
1750  */
1751 static void mvpp2_prs_sram_shift_set(struct mvpp2_prs_entry *pe, int shift,
1752                                      unsigned int op)
1753 {
1754         /* Set sign */
1755         if (shift < 0) {
1756                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1757                 shift = 0 - shift;
1758         } else {
1759                 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_SHIFT_SIGN_BIT, 1);
1760         }
1761
1762         /* Set value */
1763         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_SHIFT_OFFS)] =
1764                                                            (unsigned char)shift;
1765
1766         /* Reset and set operation */
1767         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS,
1768                                   MVPP2_PRS_SRAM_OP_SEL_SHIFT_MASK);
1769         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_SHIFT_OFFS, op);
1770
1771         /* Set base offset as current */
1772         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1773 }
1774
1775 /* In the sram sw entry set sign and value of the user defined offset
1776  * generated to the classifier
1777  */
1778 static void mvpp2_prs_sram_offset_set(struct mvpp2_prs_entry *pe,
1779                                       unsigned int type, int offset,
1780                                       unsigned int op)
1781 {
1782         /* Set sign */
1783         if (offset < 0) {
1784                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1785                 offset = 0 - offset;
1786         } else {
1787                 mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_SIGN_BIT, 1);
1788         }
1789
1790         /* Set value */
1791         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_OFFS,
1792                                   MVPP2_PRS_SRAM_UDF_MASK);
1793         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_OFFS, offset);
1794         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1795                                         MVPP2_PRS_SRAM_UDF_BITS)] &=
1796               ~(MVPP2_PRS_SRAM_UDF_MASK >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1797         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_UDF_OFFS +
1798                                         MVPP2_PRS_SRAM_UDF_BITS)] |=
1799                                 (offset >> (8 - (MVPP2_PRS_SRAM_UDF_OFFS % 8)));
1800
1801         /* Set offset type */
1802         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS,
1803                                   MVPP2_PRS_SRAM_UDF_TYPE_MASK);
1804         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_UDF_TYPE_OFFS, type);
1805
1806         /* Set offset operation */
1807         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS,
1808                                   MVPP2_PRS_SRAM_OP_SEL_UDF_MASK);
1809         mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS, op);
1810
1811         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1812                                         MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] &=
1813                                              ~(MVPP2_PRS_SRAM_OP_SEL_UDF_MASK >>
1814                                     (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1815
1816         pe->sram.byte[MVPP2_BIT_TO_BYTE(MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS +
1817                                         MVPP2_PRS_SRAM_OP_SEL_UDF_BITS)] |=
1818                              (op >> (8 - (MVPP2_PRS_SRAM_OP_SEL_UDF_OFFS % 8)));
1819
1820         /* Set base offset as current */
1821         mvpp2_prs_sram_bits_clear(pe, MVPP2_PRS_SRAM_OP_SEL_BASE_OFFS, 1);
1822 }
1823
1824 /* Find parser flow entry */
1825 static struct mvpp2_prs_entry *mvpp2_prs_flow_find(struct mvpp2 *priv, int flow)
1826 {
1827         struct mvpp2_prs_entry *pe;
1828         int tid;
1829
1830         pe = kzalloc(sizeof(*pe), GFP_KERNEL);
1831         if (!pe)
1832                 return NULL;
1833         mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
1834
1835         /* Go through the all entires with MVPP2_PRS_LU_FLOWS */
1836         for (tid = MVPP2_PRS_TCAM_SRAM_SIZE - 1; tid >= 0; tid--) {
1837                 u8 bits;
1838
1839                 if (!priv->prs_shadow[tid].valid ||
1840                     priv->prs_shadow[tid].lu != MVPP2_PRS_LU_FLOWS)
1841                         continue;
1842
1843                 pe->index = tid;
1844                 mvpp2_prs_hw_read(priv, pe);
1845                 bits = mvpp2_prs_sram_ai_get(pe);
1846
1847                 /* Sram store classification lookup ID in AI bits [5:0] */
1848                 if ((bits & MVPP2_PRS_FLOW_ID_MASK) == flow)
1849                         return pe;
1850         }
1851         kfree(pe);
1852
1853         return NULL;
1854 }
1855
1856 /* Return first free tcam index, seeking from start to end */
1857 static int mvpp2_prs_tcam_first_free(struct mvpp2 *priv, unsigned char start,
1858                                      unsigned char end)
1859 {
1860         int tid;
1861
1862         if (start > end)
1863                 swap(start, end);
1864
1865         if (end >= MVPP2_PRS_TCAM_SRAM_SIZE)
1866                 end = MVPP2_PRS_TCAM_SRAM_SIZE - 1;
1867
1868         for (tid = start; tid <= end; tid++) {
1869                 if (!priv->prs_shadow[tid].valid)
1870                         return tid;
1871         }
1872
1873         return -EINVAL;
1874 }
1875
1876 /* Enable/disable dropping all mac da's */
1877 static void mvpp2_prs_mac_drop_all_set(struct mvpp2 *priv, int port, bool add)
1878 {
1879         struct mvpp2_prs_entry pe;
1880
1881         if (priv->prs_shadow[MVPP2_PE_DROP_ALL].valid) {
1882                 /* Entry exist - update port only */
1883                 pe.index = MVPP2_PE_DROP_ALL;
1884                 mvpp2_prs_hw_read(priv, &pe);
1885         } else {
1886                 /* Entry doesn't exist - create new */
1887                 memset(&pe, 0, sizeof(pe));
1888                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1889                 pe.index = MVPP2_PE_DROP_ALL;
1890
1891                 /* Non-promiscuous mode for all ports - DROP unknown packets */
1892                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
1893                                          MVPP2_PRS_RI_DROP_MASK);
1894
1895                 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
1896                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
1897
1898                 /* Update shadow table */
1899                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1900
1901                 /* Mask all ports */
1902                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1903         }
1904
1905         /* Update port mask */
1906         mvpp2_prs_tcam_port_set(&pe, port, add);
1907
1908         mvpp2_prs_hw_write(priv, &pe);
1909 }
1910
1911 /* Set port to promiscuous mode */
1912 static void mvpp2_prs_mac_promisc_set(struct mvpp2 *priv, int port, bool add)
1913 {
1914         struct mvpp2_prs_entry pe;
1915
1916         /* Promiscuous mode - Accept unknown packets */
1917
1918         if (priv->prs_shadow[MVPP2_PE_MAC_PROMISCUOUS].valid) {
1919                 /* Entry exist - update port only */
1920                 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1921                 mvpp2_prs_hw_read(priv, &pe);
1922         } else {
1923                 /* Entry doesn't exist - create new */
1924                 memset(&pe, 0, sizeof(pe));
1925                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1926                 pe.index = MVPP2_PE_MAC_PROMISCUOUS;
1927
1928                 /* Continue - set next lookup */
1929                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1930
1931                 /* Set result info bits */
1932                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_UCAST,
1933                                          MVPP2_PRS_RI_L2_CAST_MASK);
1934
1935                 /* Shift to ethertype */
1936                 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1937                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1938
1939                 /* Mask all ports */
1940                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1941
1942                 /* Update shadow table */
1943                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1944         }
1945
1946         /* Update port mask */
1947         mvpp2_prs_tcam_port_set(&pe, port, add);
1948
1949         mvpp2_prs_hw_write(priv, &pe);
1950 }
1951
1952 /* Accept multicast */
1953 static void mvpp2_prs_mac_multi_set(struct mvpp2 *priv, int port, int index,
1954                                     bool add)
1955 {
1956         struct mvpp2_prs_entry pe;
1957         unsigned char da_mc;
1958
1959         /* Ethernet multicast address first byte is
1960          * 0x01 for IPv4 and 0x33 for IPv6
1961          */
1962         da_mc = (index == MVPP2_PE_MAC_MC_ALL) ? 0x01 : 0x33;
1963
1964         if (priv->prs_shadow[index].valid) {
1965                 /* Entry exist - update port only */
1966                 pe.index = index;
1967                 mvpp2_prs_hw_read(priv, &pe);
1968         } else {
1969                 /* Entry doesn't exist - create new */
1970                 memset(&pe, 0, sizeof(pe));
1971                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
1972                 pe.index = index;
1973
1974                 /* Continue - set next lookup */
1975                 mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_DSA);
1976
1977                 /* Set result info bits */
1978                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L2_MCAST,
1979                                          MVPP2_PRS_RI_L2_CAST_MASK);
1980
1981                 /* Update tcam entry data first byte */
1982                 mvpp2_prs_tcam_data_byte_set(&pe, 0, da_mc, 0xff);
1983
1984                 /* Shift to ethertype */
1985                 mvpp2_prs_sram_shift_set(&pe, 2 * ETH_ALEN,
1986                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
1987
1988                 /* Mask all ports */
1989                 mvpp2_prs_tcam_port_map_set(&pe, 0);
1990
1991                 /* Update shadow table */
1992                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
1993         }
1994
1995         /* Update port mask */
1996         mvpp2_prs_tcam_port_set(&pe, port, add);
1997
1998         mvpp2_prs_hw_write(priv, &pe);
1999 }
2000
2001 /* Set entry for dsa packets */
2002 static void mvpp2_prs_dsa_tag_set(struct mvpp2 *priv, int port, bool add,
2003                                   bool tagged, bool extend)
2004 {
2005         struct mvpp2_prs_entry pe;
2006         int tid, shift;
2007
2008         if (extend) {
2009                 tid = tagged ? MVPP2_PE_EDSA_TAGGED : MVPP2_PE_EDSA_UNTAGGED;
2010                 shift = 8;
2011         } else {
2012                 tid = tagged ? MVPP2_PE_DSA_TAGGED : MVPP2_PE_DSA_UNTAGGED;
2013                 shift = 4;
2014         }
2015
2016         if (priv->prs_shadow[tid].valid) {
2017                 /* Entry exist - update port only */
2018                 pe.index = tid;
2019                 mvpp2_prs_hw_read(priv, &pe);
2020         } else {
2021                 /* Entry doesn't exist - create new */
2022                 memset(&pe, 0, sizeof(pe));
2023                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2024                 pe.index = tid;
2025
2026                 /* Shift 4 bytes if DSA tag or 8 bytes in case of EDSA tag*/
2027                 mvpp2_prs_sram_shift_set(&pe, shift,
2028                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2029
2030                 /* Update shadow table */
2031                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2032
2033                 if (tagged) {
2034                         /* Set tagged bit in DSA tag */
2035                         mvpp2_prs_tcam_data_byte_set(&pe, 0,
2036                                                      MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2037                                                      MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2038                         /* Clear all ai bits for next iteration */
2039                         mvpp2_prs_sram_ai_update(&pe, 0,
2040                                                  MVPP2_PRS_SRAM_AI_MASK);
2041                         /* If packet is tagged continue check vlans */
2042                         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2043                 } else {
2044                         /* Set result info bits to 'no vlans' */
2045                         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2046                                                  MVPP2_PRS_RI_VLAN_MASK);
2047                         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2048                 }
2049
2050                 /* Mask all ports */
2051                 mvpp2_prs_tcam_port_map_set(&pe, 0);
2052         }
2053
2054         /* Update port mask */
2055         mvpp2_prs_tcam_port_set(&pe, port, add);
2056
2057         mvpp2_prs_hw_write(priv, &pe);
2058 }
2059
2060 /* Set entry for dsa ethertype */
2061 static void mvpp2_prs_dsa_tag_ethertype_set(struct mvpp2 *priv, int port,
2062                                             bool add, bool tagged, bool extend)
2063 {
2064         struct mvpp2_prs_entry pe;
2065         int tid, shift, port_mask;
2066
2067         if (extend) {
2068                 tid = tagged ? MVPP2_PE_ETYPE_EDSA_TAGGED :
2069                       MVPP2_PE_ETYPE_EDSA_UNTAGGED;
2070                 port_mask = 0;
2071                 shift = 8;
2072         } else {
2073                 tid = tagged ? MVPP2_PE_ETYPE_DSA_TAGGED :
2074                       MVPP2_PE_ETYPE_DSA_UNTAGGED;
2075                 port_mask = MVPP2_PRS_PORT_MASK;
2076                 shift = 4;
2077         }
2078
2079         if (priv->prs_shadow[tid].valid) {
2080                 /* Entry exist - update port only */
2081                 pe.index = tid;
2082                 mvpp2_prs_hw_read(priv, &pe);
2083         } else {
2084                 /* Entry doesn't exist - create new */
2085                 memset(&pe, 0, sizeof(pe));
2086                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2087                 pe.index = tid;
2088
2089                 /* Set ethertype */
2090                 mvpp2_prs_match_etype(&pe, 0, ETH_P_EDSA);
2091                 mvpp2_prs_match_etype(&pe, 2, 0);
2092
2093                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DSA_MASK,
2094                                          MVPP2_PRS_RI_DSA_MASK);
2095                 /* Shift ethertype + 2 byte reserved + tag*/
2096                 mvpp2_prs_sram_shift_set(&pe, 2 + MVPP2_ETH_TYPE_LEN + shift,
2097                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2098
2099                 /* Update shadow table */
2100                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_DSA);
2101
2102                 if (tagged) {
2103                         /* Set tagged bit in DSA tag */
2104                         mvpp2_prs_tcam_data_byte_set(&pe,
2105                                                      MVPP2_ETH_TYPE_LEN + 2 + 3,
2106                                                  MVPP2_PRS_TCAM_DSA_TAGGED_BIT,
2107                                                  MVPP2_PRS_TCAM_DSA_TAGGED_BIT);
2108                         /* Clear all ai bits for next iteration */
2109                         mvpp2_prs_sram_ai_update(&pe, 0,
2110                                                  MVPP2_PRS_SRAM_AI_MASK);
2111                         /* If packet is tagged continue check vlans */
2112                         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2113                 } else {
2114                         /* Set result info bits to 'no vlans' */
2115                         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
2116                                                  MVPP2_PRS_RI_VLAN_MASK);
2117                         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
2118                 }
2119                 /* Mask/unmask all ports, depending on dsa type */
2120                 mvpp2_prs_tcam_port_map_set(&pe, port_mask);
2121         }
2122
2123         /* Update port mask */
2124         mvpp2_prs_tcam_port_set(&pe, port, add);
2125
2126         mvpp2_prs_hw_write(priv, &pe);
2127 }
2128
2129 /* Search for existing single/triple vlan entry */
2130 static struct mvpp2_prs_entry *mvpp2_prs_vlan_find(struct mvpp2 *priv,
2131                                                    unsigned short tpid, int ai)
2132 {
2133         struct mvpp2_prs_entry *pe;
2134         int tid;
2135
2136         pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2137         if (!pe)
2138                 return NULL;
2139         mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2140
2141         /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2142         for (tid = MVPP2_PE_FIRST_FREE_TID;
2143              tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2144                 unsigned int ri_bits, ai_bits;
2145                 bool match;
2146
2147                 if (!priv->prs_shadow[tid].valid ||
2148                     priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2149                         continue;
2150
2151                 pe->index = tid;
2152
2153                 mvpp2_prs_hw_read(priv, pe);
2154                 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid));
2155                 if (!match)
2156                         continue;
2157
2158                 /* Get vlan type */
2159                 ri_bits = mvpp2_prs_sram_ri_get(pe);
2160                 ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2161
2162                 /* Get current ai value from tcam */
2163                 ai_bits = mvpp2_prs_tcam_ai_get(pe);
2164                 /* Clear double vlan bit */
2165                 ai_bits &= ~MVPP2_PRS_DBL_VLAN_AI_BIT;
2166
2167                 if (ai != ai_bits)
2168                         continue;
2169
2170                 if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2171                     ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2172                         return pe;
2173         }
2174         kfree(pe);
2175
2176         return NULL;
2177 }
2178
2179 /* Add/update single/triple vlan entry */
2180 static int mvpp2_prs_vlan_add(struct mvpp2 *priv, unsigned short tpid, int ai,
2181                               unsigned int port_map)
2182 {
2183         struct mvpp2_prs_entry *pe;
2184         int tid_aux, tid;
2185         int ret = 0;
2186
2187         pe = mvpp2_prs_vlan_find(priv, tpid, ai);
2188
2189         if (!pe) {
2190                 /* Create new tcam entry */
2191                 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_LAST_FREE_TID,
2192                                                 MVPP2_PE_FIRST_FREE_TID);
2193                 if (tid < 0)
2194                         return tid;
2195
2196                 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2197                 if (!pe)
2198                         return -ENOMEM;
2199
2200                 /* Get last double vlan tid */
2201                 for (tid_aux = MVPP2_PE_LAST_FREE_TID;
2202                      tid_aux >= MVPP2_PE_FIRST_FREE_TID; tid_aux--) {
2203                         unsigned int ri_bits;
2204
2205                         if (!priv->prs_shadow[tid_aux].valid ||
2206                             priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2207                                 continue;
2208
2209                         pe->index = tid_aux;
2210                         mvpp2_prs_hw_read(priv, pe);
2211                         ri_bits = mvpp2_prs_sram_ri_get(pe);
2212                         if ((ri_bits & MVPP2_PRS_RI_VLAN_MASK) ==
2213                             MVPP2_PRS_RI_VLAN_DOUBLE)
2214                                 break;
2215                 }
2216
2217                 if (tid <= tid_aux) {
2218                         ret = -EINVAL;
2219                         goto free_pe;
2220                 }
2221
2222                 memset(pe, 0, sizeof(*pe));
2223                 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2224                 pe->index = tid;
2225
2226                 mvpp2_prs_match_etype(pe, 0, tpid);
2227
2228                 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_L2);
2229                 /* Shift 4 bytes - skip 1 vlan tag */
2230                 mvpp2_prs_sram_shift_set(pe, MVPP2_VLAN_TAG_LEN,
2231                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2232                 /* Clear all ai bits for next iteration */
2233                 mvpp2_prs_sram_ai_update(pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2234
2235                 if (ai == MVPP2_PRS_SINGLE_VLAN_AI) {
2236                         mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_SINGLE,
2237                                                  MVPP2_PRS_RI_VLAN_MASK);
2238                 } else {
2239                         ai |= MVPP2_PRS_DBL_VLAN_AI_BIT;
2240                         mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_TRIPLE,
2241                                                  MVPP2_PRS_RI_VLAN_MASK);
2242                 }
2243                 mvpp2_prs_tcam_ai_update(pe, ai, MVPP2_PRS_SRAM_AI_MASK);
2244
2245                 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2246         }
2247         /* Update ports' mask */
2248         mvpp2_prs_tcam_port_map_set(pe, port_map);
2249
2250         mvpp2_prs_hw_write(priv, pe);
2251 free_pe:
2252         kfree(pe);
2253
2254         return ret;
2255 }
2256
2257 /* Get first free double vlan ai number */
2258 static int mvpp2_prs_double_vlan_ai_free_get(struct mvpp2 *priv)
2259 {
2260         int i;
2261
2262         for (i = 1; i < MVPP2_PRS_DBL_VLANS_MAX; i++) {
2263                 if (!priv->prs_double_vlans[i])
2264                         return i;
2265         }
2266
2267         return -EINVAL;
2268 }
2269
2270 /* Search for existing double vlan entry */
2271 static struct mvpp2_prs_entry *mvpp2_prs_double_vlan_find(struct mvpp2 *priv,
2272                                                           unsigned short tpid1,
2273                                                           unsigned short tpid2)
2274 {
2275         struct mvpp2_prs_entry *pe;
2276         int tid;
2277
2278         pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2279         if (!pe)
2280                 return NULL;
2281         mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2282
2283         /* Go through the all entries with MVPP2_PRS_LU_VLAN */
2284         for (tid = MVPP2_PE_FIRST_FREE_TID;
2285              tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
2286                 unsigned int ri_mask;
2287                 bool match;
2288
2289                 if (!priv->prs_shadow[tid].valid ||
2290                     priv->prs_shadow[tid].lu != MVPP2_PRS_LU_VLAN)
2291                         continue;
2292
2293                 pe->index = tid;
2294                 mvpp2_prs_hw_read(priv, pe);
2295
2296                 match = mvpp2_prs_tcam_data_cmp(pe, 0, swab16(tpid1))
2297                         && mvpp2_prs_tcam_data_cmp(pe, 4, swab16(tpid2));
2298
2299                 if (!match)
2300                         continue;
2301
2302                 ri_mask = mvpp2_prs_sram_ri_get(pe) & MVPP2_PRS_RI_VLAN_MASK;
2303                 if (ri_mask == MVPP2_PRS_RI_VLAN_DOUBLE)
2304                         return pe;
2305         }
2306         kfree(pe);
2307
2308         return NULL;
2309 }
2310
2311 /* Add or update double vlan entry */
2312 static int mvpp2_prs_double_vlan_add(struct mvpp2 *priv, unsigned short tpid1,
2313                                      unsigned short tpid2,
2314                                      unsigned int port_map)
2315 {
2316         struct mvpp2_prs_entry *pe;
2317         int tid_aux, tid, ai, ret = 0;
2318
2319         pe = mvpp2_prs_double_vlan_find(priv, tpid1, tpid2);
2320
2321         if (!pe) {
2322                 /* Create new tcam entry */
2323                 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2324                                 MVPP2_PE_LAST_FREE_TID);
2325                 if (tid < 0)
2326                         return tid;
2327
2328                 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
2329                 if (!pe)
2330                         return -ENOMEM;
2331
2332                 /* Set ai value for new double vlan entry */
2333                 ai = mvpp2_prs_double_vlan_ai_free_get(priv);
2334                 if (ai < 0) {
2335                         ret = ai;
2336                         goto free_pe;
2337                 }
2338
2339                 /* Get first single/triple vlan tid */
2340                 for (tid_aux = MVPP2_PE_FIRST_FREE_TID;
2341                      tid_aux <= MVPP2_PE_LAST_FREE_TID; tid_aux++) {
2342                         unsigned int ri_bits;
2343
2344                         if (!priv->prs_shadow[tid_aux].valid ||
2345                             priv->prs_shadow[tid_aux].lu != MVPP2_PRS_LU_VLAN)
2346                                 continue;
2347
2348                         pe->index = tid_aux;
2349                         mvpp2_prs_hw_read(priv, pe);
2350                         ri_bits = mvpp2_prs_sram_ri_get(pe);
2351                         ri_bits &= MVPP2_PRS_RI_VLAN_MASK;
2352                         if (ri_bits == MVPP2_PRS_RI_VLAN_SINGLE ||
2353                             ri_bits == MVPP2_PRS_RI_VLAN_TRIPLE)
2354                                 break;
2355                 }
2356
2357                 if (tid >= tid_aux) {
2358                         ret = -ERANGE;
2359                         goto free_pe;
2360                 }
2361
2362                 memset(pe, 0, sizeof(*pe));
2363                 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_VLAN);
2364                 pe->index = tid;
2365
2366                 priv->prs_double_vlans[ai] = true;
2367
2368                 mvpp2_prs_match_etype(pe, 0, tpid1);
2369                 mvpp2_prs_match_etype(pe, 4, tpid2);
2370
2371                 mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_VLAN);
2372                 /* Shift 8 bytes - skip 2 vlan tags */
2373                 mvpp2_prs_sram_shift_set(pe, 2 * MVPP2_VLAN_TAG_LEN,
2374                                          MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2375                 mvpp2_prs_sram_ri_update(pe, MVPP2_PRS_RI_VLAN_DOUBLE,
2376                                          MVPP2_PRS_RI_VLAN_MASK);
2377                 mvpp2_prs_sram_ai_update(pe, ai | MVPP2_PRS_DBL_VLAN_AI_BIT,
2378                                          MVPP2_PRS_SRAM_AI_MASK);
2379
2380                 mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_VLAN);
2381         }
2382
2383         /* Update ports' mask */
2384         mvpp2_prs_tcam_port_map_set(pe, port_map);
2385         mvpp2_prs_hw_write(priv, pe);
2386 free_pe:
2387         kfree(pe);
2388         return ret;
2389 }
2390
2391 /* IPv4 header parsing for fragmentation and L4 offset */
2392 static int mvpp2_prs_ip4_proto(struct mvpp2 *priv, unsigned short proto,
2393                                unsigned int ri, unsigned int ri_mask)
2394 {
2395         struct mvpp2_prs_entry pe;
2396         int tid;
2397
2398         if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2399             (proto != IPPROTO_IGMP))
2400                 return -EINVAL;
2401
2402         /* Not fragmented packet */
2403         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2404                                         MVPP2_PE_LAST_FREE_TID);
2405         if (tid < 0)
2406                 return tid;
2407
2408         memset(&pe, 0, sizeof(pe));
2409         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2410         pe.index = tid;
2411
2412         /* Set next lu to IPv4 */
2413         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2414         mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2415         /* Set L4 offset */
2416         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2417                                   sizeof(struct iphdr) - 4,
2418                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2419         mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2420                                  MVPP2_PRS_IPV4_DIP_AI_BIT);
2421         mvpp2_prs_sram_ri_update(&pe, ri, ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2422
2423         mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00,
2424                                      MVPP2_PRS_TCAM_PROTO_MASK_L);
2425         mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00,
2426                                      MVPP2_PRS_TCAM_PROTO_MASK);
2427
2428         mvpp2_prs_tcam_data_byte_set(&pe, 5, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2429         mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
2430         /* Unmask all ports */
2431         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2432
2433         /* Update shadow table and hw entry */
2434         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2435         mvpp2_prs_hw_write(priv, &pe);
2436
2437         /* Fragmented packet */
2438         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2439                                         MVPP2_PE_LAST_FREE_TID);
2440         if (tid < 0)
2441                 return tid;
2442
2443         pe.index = tid;
2444         /* Clear ri before updating */
2445         pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2446         pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2447         mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2448
2449         mvpp2_prs_sram_ri_update(&pe, ri | MVPP2_PRS_RI_IP_FRAG_TRUE,
2450                                  ri_mask | MVPP2_PRS_RI_IP_FRAG_MASK);
2451
2452         mvpp2_prs_tcam_data_byte_set(&pe, 2, 0x00, 0x0);
2453         mvpp2_prs_tcam_data_byte_set(&pe, 3, 0x00, 0x0);
2454
2455         /* Update shadow table and hw entry */
2456         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2457         mvpp2_prs_hw_write(priv, &pe);
2458
2459         return 0;
2460 }
2461
2462 /* IPv4 L3 multicast or broadcast */
2463 static int mvpp2_prs_ip4_cast(struct mvpp2 *priv, unsigned short l3_cast)
2464 {
2465         struct mvpp2_prs_entry pe;
2466         int mask, tid;
2467
2468         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2469                                         MVPP2_PE_LAST_FREE_TID);
2470         if (tid < 0)
2471                 return tid;
2472
2473         memset(&pe, 0, sizeof(pe));
2474         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
2475         pe.index = tid;
2476
2477         switch (l3_cast) {
2478         case MVPP2_PRS_L3_MULTI_CAST:
2479                 mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV4_MC,
2480                                              MVPP2_PRS_IPV4_MC_MASK);
2481                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2482                                          MVPP2_PRS_RI_L3_ADDR_MASK);
2483                 break;
2484         case  MVPP2_PRS_L3_BROAD_CAST:
2485                 mask = MVPP2_PRS_IPV4_BC_MASK;
2486                 mvpp2_prs_tcam_data_byte_set(&pe, 0, mask, mask);
2487                 mvpp2_prs_tcam_data_byte_set(&pe, 1, mask, mask);
2488                 mvpp2_prs_tcam_data_byte_set(&pe, 2, mask, mask);
2489                 mvpp2_prs_tcam_data_byte_set(&pe, 3, mask, mask);
2490                 mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_BCAST,
2491                                          MVPP2_PRS_RI_L3_ADDR_MASK);
2492                 break;
2493         default:
2494                 return -EINVAL;
2495         }
2496
2497         /* Finished: go to flowid generation */
2498         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2499         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2500
2501         mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
2502                                  MVPP2_PRS_IPV4_DIP_AI_BIT);
2503         /* Unmask all ports */
2504         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2505
2506         /* Update shadow table and hw entry */
2507         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
2508         mvpp2_prs_hw_write(priv, &pe);
2509
2510         return 0;
2511 }
2512
2513 /* Set entries for protocols over IPv6  */
2514 static int mvpp2_prs_ip6_proto(struct mvpp2 *priv, unsigned short proto,
2515                                unsigned int ri, unsigned int ri_mask)
2516 {
2517         struct mvpp2_prs_entry pe;
2518         int tid;
2519
2520         if ((proto != IPPROTO_TCP) && (proto != IPPROTO_UDP) &&
2521             (proto != IPPROTO_ICMPV6) && (proto != IPPROTO_IPIP))
2522                 return -EINVAL;
2523
2524         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2525                                         MVPP2_PE_LAST_FREE_TID);
2526         if (tid < 0)
2527                 return tid;
2528
2529         memset(&pe, 0, sizeof(pe));
2530         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2531         pe.index = tid;
2532
2533         /* Finished: go to flowid generation */
2534         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2535         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2536         mvpp2_prs_sram_ri_update(&pe, ri, ri_mask);
2537         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
2538                                   sizeof(struct ipv6hdr) - 6,
2539                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2540
2541         mvpp2_prs_tcam_data_byte_set(&pe, 0, proto, MVPP2_PRS_TCAM_PROTO_MASK);
2542         mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2543                                  MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2544         /* Unmask all ports */
2545         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2546
2547         /* Write HW */
2548         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2549         mvpp2_prs_hw_write(priv, &pe);
2550
2551         return 0;
2552 }
2553
2554 /* IPv6 L3 multicast entry */
2555 static int mvpp2_prs_ip6_cast(struct mvpp2 *priv, unsigned short l3_cast)
2556 {
2557         struct mvpp2_prs_entry pe;
2558         int tid;
2559
2560         if (l3_cast != MVPP2_PRS_L3_MULTI_CAST)
2561                 return -EINVAL;
2562
2563         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2564                                         MVPP2_PE_LAST_FREE_TID);
2565         if (tid < 0)
2566                 return tid;
2567
2568         memset(&pe, 0, sizeof(pe));
2569         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
2570         pe.index = tid;
2571
2572         /* Finished: go to flowid generation */
2573         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2574         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_MCAST,
2575                                  MVPP2_PRS_RI_L3_ADDR_MASK);
2576         mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
2577                                  MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2578         /* Shift back to IPv6 NH */
2579         mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2580
2581         mvpp2_prs_tcam_data_byte_set(&pe, 0, MVPP2_PRS_IPV6_MC,
2582                                      MVPP2_PRS_IPV6_MC_MASK);
2583         mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
2584         /* Unmask all ports */
2585         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2586
2587         /* Update shadow table and hw entry */
2588         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
2589         mvpp2_prs_hw_write(priv, &pe);
2590
2591         return 0;
2592 }
2593
2594 /* Parser per-port initialization */
2595 static void mvpp2_prs_hw_port_init(struct mvpp2 *priv, int port, int lu_first,
2596                                    int lu_max, int offset)
2597 {
2598         u32 val;
2599
2600         /* Set lookup ID */
2601         val = mvpp2_read(priv, MVPP2_PRS_INIT_LOOKUP_REG);
2602         val &= ~MVPP2_PRS_PORT_LU_MASK(port);
2603         val |=  MVPP2_PRS_PORT_LU_VAL(port, lu_first);
2604         mvpp2_write(priv, MVPP2_PRS_INIT_LOOKUP_REG, val);
2605
2606         /* Set maximum number of loops for packet received from port */
2607         val = mvpp2_read(priv, MVPP2_PRS_MAX_LOOP_REG(port));
2608         val &= ~MVPP2_PRS_MAX_LOOP_MASK(port);
2609         val |= MVPP2_PRS_MAX_LOOP_VAL(port, lu_max);
2610         mvpp2_write(priv, MVPP2_PRS_MAX_LOOP_REG(port), val);
2611
2612         /* Set initial offset for packet header extraction for the first
2613          * searching loop
2614          */
2615         val = mvpp2_read(priv, MVPP2_PRS_INIT_OFFS_REG(port));
2616         val &= ~MVPP2_PRS_INIT_OFF_MASK(port);
2617         val |= MVPP2_PRS_INIT_OFF_VAL(port, offset);
2618         mvpp2_write(priv, MVPP2_PRS_INIT_OFFS_REG(port), val);
2619 }
2620
2621 /* Default flow entries initialization for all ports */
2622 static void mvpp2_prs_def_flow_init(struct mvpp2 *priv)
2623 {
2624         struct mvpp2_prs_entry pe;
2625         int port;
2626
2627         for (port = 0; port < MVPP2_MAX_PORTS; port++) {
2628                 memset(&pe, 0, sizeof(pe));
2629                 mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2630                 pe.index = MVPP2_PE_FIRST_DEFAULT_FLOW - port;
2631
2632                 /* Mask all ports */
2633                 mvpp2_prs_tcam_port_map_set(&pe, 0);
2634
2635                 /* Set flow ID*/
2636                 mvpp2_prs_sram_ai_update(&pe, port, MVPP2_PRS_FLOW_ID_MASK);
2637                 mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
2638
2639                 /* Update shadow table and hw entry */
2640                 mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_FLOWS);
2641                 mvpp2_prs_hw_write(priv, &pe);
2642         }
2643 }
2644
2645 /* Set default entry for Marvell Header field */
2646 static void mvpp2_prs_mh_init(struct mvpp2 *priv)
2647 {
2648         struct mvpp2_prs_entry pe;
2649
2650         memset(&pe, 0, sizeof(pe));
2651
2652         pe.index = MVPP2_PE_MH_DEFAULT;
2653         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MH);
2654         mvpp2_prs_sram_shift_set(&pe, MVPP2_MH_SIZE,
2655                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2656         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_MAC);
2657
2658         /* Unmask all ports */
2659         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2660
2661         /* Update shadow table and hw entry */
2662         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MH);
2663         mvpp2_prs_hw_write(priv, &pe);
2664 }
2665
2666 /* Set default entires (place holder) for promiscuous, non-promiscuous and
2667  * multicast MAC addresses
2668  */
2669 static void mvpp2_prs_mac_init(struct mvpp2 *priv)
2670 {
2671         struct mvpp2_prs_entry pe;
2672
2673         memset(&pe, 0, sizeof(pe));
2674
2675         /* Non-promiscuous mode for all ports - DROP unknown packets */
2676         pe.index = MVPP2_PE_MAC_NON_PROMISCUOUS;
2677         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_MAC);
2678
2679         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_DROP_MASK,
2680                                  MVPP2_PRS_RI_DROP_MASK);
2681         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2682         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2683
2684         /* Unmask all ports */
2685         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2686
2687         /* Update shadow table and hw entry */
2688         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2689         mvpp2_prs_hw_write(priv, &pe);
2690
2691         /* place holders only - no ports */
2692         mvpp2_prs_mac_drop_all_set(priv, 0, false);
2693         mvpp2_prs_mac_promisc_set(priv, 0, false);
2694         mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_ALL, false);
2695         mvpp2_prs_mac_multi_set(priv, 0, MVPP2_PE_MAC_MC_IP6, false);
2696 }
2697
2698 /* Set default entries for various types of dsa packets */
2699 static void mvpp2_prs_dsa_init(struct mvpp2 *priv)
2700 {
2701         struct mvpp2_prs_entry pe;
2702
2703         /* None tagged EDSA entry - place holder */
2704         mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2705                               MVPP2_PRS_EDSA);
2706
2707         /* Tagged EDSA entry - place holder */
2708         mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2709
2710         /* None tagged DSA entry - place holder */
2711         mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_UNTAGGED,
2712                               MVPP2_PRS_DSA);
2713
2714         /* Tagged DSA entry - place holder */
2715         mvpp2_prs_dsa_tag_set(priv, 0, false, MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2716
2717         /* None tagged EDSA ethertype entry - place holder*/
2718         mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2719                                         MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
2720
2721         /* Tagged EDSA ethertype entry - place holder*/
2722         mvpp2_prs_dsa_tag_ethertype_set(priv, 0, false,
2723                                         MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
2724
2725         /* None tagged DSA ethertype entry */
2726         mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2727                                         MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
2728
2729         /* Tagged DSA ethertype entry */
2730         mvpp2_prs_dsa_tag_ethertype_set(priv, 0, true,
2731                                         MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
2732
2733         /* Set default entry, in case DSA or EDSA tag not found */
2734         memset(&pe, 0, sizeof(pe));
2735         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_DSA);
2736         pe.index = MVPP2_PE_DSA_DEFAULT;
2737         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_VLAN);
2738
2739         /* Shift 0 bytes */
2740         mvpp2_prs_sram_shift_set(&pe, 0, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2741         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_MAC);
2742
2743         /* Clear all sram ai bits for next iteration */
2744         mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
2745
2746         /* Unmask all ports */
2747         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2748
2749         mvpp2_prs_hw_write(priv, &pe);
2750 }
2751
2752 /* Match basic ethertypes */
2753 static int mvpp2_prs_etype_init(struct mvpp2 *priv)
2754 {
2755         struct mvpp2_prs_entry pe;
2756         int tid;
2757
2758         /* Ethertype: PPPoE */
2759         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2760                                         MVPP2_PE_LAST_FREE_TID);
2761         if (tid < 0)
2762                 return tid;
2763
2764         memset(&pe, 0, sizeof(pe));
2765         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2766         pe.index = tid;
2767
2768         mvpp2_prs_match_etype(&pe, 0, ETH_P_PPP_SES);
2769
2770         mvpp2_prs_sram_shift_set(&pe, MVPP2_PPPOE_HDR_SIZE,
2771                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2772         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
2773         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_PPPOE_MASK,
2774                                  MVPP2_PRS_RI_PPPOE_MASK);
2775
2776         /* Update shadow table and hw entry */
2777         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2778         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2779         priv->prs_shadow[pe.index].finish = false;
2780         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_PPPOE_MASK,
2781                                 MVPP2_PRS_RI_PPPOE_MASK);
2782         mvpp2_prs_hw_write(priv, &pe);
2783
2784         /* Ethertype: ARP */
2785         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2786                                         MVPP2_PE_LAST_FREE_TID);
2787         if (tid < 0)
2788                 return tid;
2789
2790         memset(&pe, 0, sizeof(pe));
2791         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2792         pe.index = tid;
2793
2794         mvpp2_prs_match_etype(&pe, 0, ETH_P_ARP);
2795
2796         /* Generate flow in the next iteration*/
2797         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2798         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2799         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_ARP,
2800                                  MVPP2_PRS_RI_L3_PROTO_MASK);
2801         /* Set L3 offset */
2802         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2803                                   MVPP2_ETH_TYPE_LEN,
2804                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2805
2806         /* Update shadow table and hw entry */
2807         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2808         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2809         priv->prs_shadow[pe.index].finish = true;
2810         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_ARP,
2811                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2812         mvpp2_prs_hw_write(priv, &pe);
2813
2814         /* Ethertype: LBTD */
2815         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2816                                         MVPP2_PE_LAST_FREE_TID);
2817         if (tid < 0)
2818                 return tid;
2819
2820         memset(&pe, 0, sizeof(pe));
2821         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2822         pe.index = tid;
2823
2824         mvpp2_prs_match_etype(&pe, 0, MVPP2_IP_LBDT_TYPE);
2825
2826         /* Generate flow in the next iteration*/
2827         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2828         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2829         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2830                                  MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2831                                  MVPP2_PRS_RI_CPU_CODE_MASK |
2832                                  MVPP2_PRS_RI_UDF3_MASK);
2833         /* Set L3 offset */
2834         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2835                                   MVPP2_ETH_TYPE_LEN,
2836                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2837
2838         /* Update shadow table and hw entry */
2839         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2840         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2841         priv->prs_shadow[pe.index].finish = true;
2842         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
2843                                 MVPP2_PRS_RI_UDF3_RX_SPECIAL,
2844                                 MVPP2_PRS_RI_CPU_CODE_MASK |
2845                                 MVPP2_PRS_RI_UDF3_MASK);
2846         mvpp2_prs_hw_write(priv, &pe);
2847
2848         /* Ethertype: IPv4 without options */
2849         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2850                                         MVPP2_PE_LAST_FREE_TID);
2851         if (tid < 0)
2852                 return tid;
2853
2854         memset(&pe, 0, sizeof(pe));
2855         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2856         pe.index = tid;
2857
2858         mvpp2_prs_match_etype(&pe, 0, ETH_P_IP);
2859         mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2860                                      MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
2861                                      MVPP2_PRS_IPV4_HEAD_MASK |
2862                                      MVPP2_PRS_IPV4_IHL_MASK);
2863
2864         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
2865         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
2866                                  MVPP2_PRS_RI_L3_PROTO_MASK);
2867         /* Skip eth_type + 4 bytes of IP header */
2868         mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
2869                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2870         /* Set L3 offset */
2871         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2872                                   MVPP2_ETH_TYPE_LEN,
2873                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2874
2875         /* Update shadow table and hw entry */
2876         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2877         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2878         priv->prs_shadow[pe.index].finish = false;
2879         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4,
2880                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2881         mvpp2_prs_hw_write(priv, &pe);
2882
2883         /* Ethertype: IPv4 with options */
2884         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2885                                         MVPP2_PE_LAST_FREE_TID);
2886         if (tid < 0)
2887                 return tid;
2888
2889         pe.index = tid;
2890
2891         /* Clear tcam data before updating */
2892         pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE(MVPP2_ETH_TYPE_LEN)] = 0x0;
2893         pe.tcam.byte[MVPP2_PRS_TCAM_DATA_BYTE_EN(MVPP2_ETH_TYPE_LEN)] = 0x0;
2894
2895         mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
2896                                      MVPP2_PRS_IPV4_HEAD,
2897                                      MVPP2_PRS_IPV4_HEAD_MASK);
2898
2899         /* Clear ri before updating */
2900         pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
2901         pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
2902         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
2903                                  MVPP2_PRS_RI_L3_PROTO_MASK);
2904
2905         /* Update shadow table and hw entry */
2906         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2907         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2908         priv->prs_shadow[pe.index].finish = false;
2909         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP4_OPT,
2910                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2911         mvpp2_prs_hw_write(priv, &pe);
2912
2913         /* Ethertype: IPv6 without options */
2914         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
2915                                         MVPP2_PE_LAST_FREE_TID);
2916         if (tid < 0)
2917                 return tid;
2918
2919         memset(&pe, 0, sizeof(pe));
2920         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2921         pe.index = tid;
2922
2923         mvpp2_prs_match_etype(&pe, 0, ETH_P_IPV6);
2924
2925         /* Skip DIP of IPV6 header */
2926         mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 8 +
2927                                  MVPP2_MAX_L3_ADDR_SIZE,
2928                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
2929         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
2930         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
2931                                  MVPP2_PRS_RI_L3_PROTO_MASK);
2932         /* Set L3 offset */
2933         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2934                                   MVPP2_ETH_TYPE_LEN,
2935                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2936
2937         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2938         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2939         priv->prs_shadow[pe.index].finish = false;
2940         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_IP6,
2941                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2942         mvpp2_prs_hw_write(priv, &pe);
2943
2944         /* Default entry for MVPP2_PRS_LU_L2 - Unknown ethtype */
2945         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
2946         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_L2);
2947         pe.index = MVPP2_PE_ETH_TYPE_UN;
2948
2949         /* Unmask all ports */
2950         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
2951
2952         /* Generate flow in the next iteration*/
2953         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
2954         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
2955         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
2956                                  MVPP2_PRS_RI_L3_PROTO_MASK);
2957         /* Set L3 offset even it's unknown L3 */
2958         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
2959                                   MVPP2_ETH_TYPE_LEN,
2960                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
2961
2962         /* Update shadow table and hw entry */
2963         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_L2);
2964         priv->prs_shadow[pe.index].udf = MVPP2_PRS_UDF_L2_DEF;
2965         priv->prs_shadow[pe.index].finish = true;
2966         mvpp2_prs_shadow_ri_set(priv, pe.index, MVPP2_PRS_RI_L3_UN,
2967                                 MVPP2_PRS_RI_L3_PROTO_MASK);
2968         mvpp2_prs_hw_write(priv, &pe);
2969
2970         return 0;
2971 }
2972
2973 /* Configure vlan entries and detect up to 2 successive VLAN tags.
2974  * Possible options:
2975  * 0x8100, 0x88A8
2976  * 0x8100, 0x8100
2977  * 0x8100
2978  * 0x88A8
2979  */
2980 static int mvpp2_prs_vlan_init(struct platform_device *pdev, struct mvpp2 *priv)
2981 {
2982         struct mvpp2_prs_entry pe;
2983         int err;
2984
2985         priv->prs_double_vlans = devm_kcalloc(&pdev->dev, sizeof(bool),
2986                                               MVPP2_PRS_DBL_VLANS_MAX,
2987                                               GFP_KERNEL);
2988         if (!priv->prs_double_vlans)
2989                 return -ENOMEM;
2990
2991         /* Double VLAN: 0x8100, 0x88A8 */
2992         err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021AD,
2993                                         MVPP2_PRS_PORT_MASK);
2994         if (err)
2995                 return err;
2996
2997         /* Double VLAN: 0x8100, 0x8100 */
2998         err = mvpp2_prs_double_vlan_add(priv, ETH_P_8021Q, ETH_P_8021Q,
2999                                         MVPP2_PRS_PORT_MASK);
3000         if (err)
3001                 return err;
3002
3003         /* Single VLAN: 0x88a8 */
3004         err = mvpp2_prs_vlan_add(priv, ETH_P_8021AD, MVPP2_PRS_SINGLE_VLAN_AI,
3005                                  MVPP2_PRS_PORT_MASK);
3006         if (err)
3007                 return err;
3008
3009         /* Single VLAN: 0x8100 */
3010         err = mvpp2_prs_vlan_add(priv, ETH_P_8021Q, MVPP2_PRS_SINGLE_VLAN_AI,
3011                                  MVPP2_PRS_PORT_MASK);
3012         if (err)
3013                 return err;
3014
3015         /* Set default double vlan entry */
3016         memset(&pe, 0, sizeof(pe));
3017         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3018         pe.index = MVPP2_PE_VLAN_DBL;
3019
3020         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3021         /* Clear ai for next iterations */
3022         mvpp2_prs_sram_ai_update(&pe, 0, MVPP2_PRS_SRAM_AI_MASK);
3023         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_DOUBLE,
3024                                  MVPP2_PRS_RI_VLAN_MASK);
3025
3026         mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_DBL_VLAN_AI_BIT,
3027                                  MVPP2_PRS_DBL_VLAN_AI_BIT);
3028         /* Unmask all ports */
3029         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3030
3031         /* Update shadow table and hw entry */
3032         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3033         mvpp2_prs_hw_write(priv, &pe);
3034
3035         /* Set default vlan none entry */
3036         memset(&pe, 0, sizeof(pe));
3037         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_VLAN);
3038         pe.index = MVPP2_PE_VLAN_NONE;
3039
3040         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_L2);
3041         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_VLAN_NONE,
3042                                  MVPP2_PRS_RI_VLAN_MASK);
3043
3044         /* Unmask all ports */
3045         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3046
3047         /* Update shadow table and hw entry */
3048         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_VLAN);
3049         mvpp2_prs_hw_write(priv, &pe);
3050
3051         return 0;
3052 }
3053
3054 /* Set entries for PPPoE ethertype */
3055 static int mvpp2_prs_pppoe_init(struct mvpp2 *priv)
3056 {
3057         struct mvpp2_prs_entry pe;
3058         int tid;
3059
3060         /* IPv4 over PPPoE with options */
3061         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3062                                         MVPP2_PE_LAST_FREE_TID);
3063         if (tid < 0)
3064                 return tid;
3065
3066         memset(&pe, 0, sizeof(pe));
3067         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3068         pe.index = tid;
3069
3070         mvpp2_prs_match_etype(&pe, 0, PPP_IP);
3071
3072         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3073         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4_OPT,
3074                                  MVPP2_PRS_RI_L3_PROTO_MASK);
3075         /* Skip eth_type + 4 bytes of IP header */
3076         mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3077                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3078         /* Set L3 offset */
3079         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3080                                   MVPP2_ETH_TYPE_LEN,
3081                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3082
3083         /* Update shadow table and hw entry */
3084         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3085         mvpp2_prs_hw_write(priv, &pe);
3086
3087         /* IPv4 over PPPoE without options */
3088         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3089                                         MVPP2_PE_LAST_FREE_TID);
3090         if (tid < 0)
3091                 return tid;
3092
3093         pe.index = tid;
3094
3095         mvpp2_prs_tcam_data_byte_set(&pe, MVPP2_ETH_TYPE_LEN,
3096                                      MVPP2_PRS_IPV4_HEAD | MVPP2_PRS_IPV4_IHL,
3097                                      MVPP2_PRS_IPV4_HEAD_MASK |
3098                                      MVPP2_PRS_IPV4_IHL_MASK);
3099
3100         /* Clear ri before updating */
3101         pe.sram.word[MVPP2_PRS_SRAM_RI_WORD] = 0x0;
3102         pe.sram.word[MVPP2_PRS_SRAM_RI_CTRL_WORD] = 0x0;
3103         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP4,
3104                                  MVPP2_PRS_RI_L3_PROTO_MASK);
3105
3106         /* Update shadow table and hw entry */
3107         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3108         mvpp2_prs_hw_write(priv, &pe);
3109
3110         /* IPv6 over PPPoE */
3111         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3112                                         MVPP2_PE_LAST_FREE_TID);
3113         if (tid < 0)
3114                 return tid;
3115
3116         memset(&pe, 0, sizeof(pe));
3117         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3118         pe.index = tid;
3119
3120         mvpp2_prs_match_etype(&pe, 0, PPP_IPV6);
3121
3122         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3123         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_IP6,
3124                                  MVPP2_PRS_RI_L3_PROTO_MASK);
3125         /* Skip eth_type + 4 bytes of IPv6 header */
3126         mvpp2_prs_sram_shift_set(&pe, MVPP2_ETH_TYPE_LEN + 4,
3127                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3128         /* Set L3 offset */
3129         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3130                                   MVPP2_ETH_TYPE_LEN,
3131                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3132
3133         /* Update shadow table and hw entry */
3134         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3135         mvpp2_prs_hw_write(priv, &pe);
3136
3137         /* Non-IP over PPPoE */
3138         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3139                                         MVPP2_PE_LAST_FREE_TID);
3140         if (tid < 0)
3141                 return tid;
3142
3143         memset(&pe, 0, sizeof(pe));
3144         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_PPPOE);
3145         pe.index = tid;
3146
3147         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN,
3148                                  MVPP2_PRS_RI_L3_PROTO_MASK);
3149
3150         /* Finished: go to flowid generation */
3151         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3152         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3153         /* Set L3 offset even if it's unknown L3 */
3154         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L3,
3155                                   MVPP2_ETH_TYPE_LEN,
3156                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3157
3158         /* Update shadow table and hw entry */
3159         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_PPPOE);
3160         mvpp2_prs_hw_write(priv, &pe);
3161
3162         return 0;
3163 }
3164
3165 /* Initialize entries for IPv4 */
3166 static int mvpp2_prs_ip4_init(struct mvpp2 *priv)
3167 {
3168         struct mvpp2_prs_entry pe;
3169         int err;
3170
3171         /* Set entries for TCP, UDP and IGMP over IPv4 */
3172         err = mvpp2_prs_ip4_proto(priv, IPPROTO_TCP, MVPP2_PRS_RI_L4_TCP,
3173                                   MVPP2_PRS_RI_L4_PROTO_MASK);
3174         if (err)
3175                 return err;
3176
3177         err = mvpp2_prs_ip4_proto(priv, IPPROTO_UDP, MVPP2_PRS_RI_L4_UDP,
3178                                   MVPP2_PRS_RI_L4_PROTO_MASK);
3179         if (err)
3180                 return err;
3181
3182         err = mvpp2_prs_ip4_proto(priv, IPPROTO_IGMP,
3183                                   MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3184                                   MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3185                                   MVPP2_PRS_RI_CPU_CODE_MASK |
3186                                   MVPP2_PRS_RI_UDF3_MASK);
3187         if (err)
3188                 return err;
3189
3190         /* IPv4 Broadcast */
3191         err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_BROAD_CAST);
3192         if (err)
3193                 return err;
3194
3195         /* IPv4 Multicast */
3196         err = mvpp2_prs_ip4_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3197         if (err)
3198                 return err;
3199
3200         /* Default IPv4 entry for unknown protocols */
3201         memset(&pe, 0, sizeof(pe));
3202         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3203         pe.index = MVPP2_PE_IP4_PROTO_UN;
3204
3205         /* Set next lu to IPv4 */
3206         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP4);
3207         mvpp2_prs_sram_shift_set(&pe, 12, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3208         /* Set L4 offset */
3209         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3210                                   sizeof(struct iphdr) - 4,
3211                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3212         mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3213                                  MVPP2_PRS_IPV4_DIP_AI_BIT);
3214         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3215                                  MVPP2_PRS_RI_L4_PROTO_MASK);
3216
3217         mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV4_DIP_AI_BIT);
3218         /* Unmask all ports */
3219         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3220
3221         /* Update shadow table and hw entry */
3222         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3223         mvpp2_prs_hw_write(priv, &pe);
3224
3225         /* Default IPv4 entry for unicast address */
3226         memset(&pe, 0, sizeof(pe));
3227         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP4);
3228         pe.index = MVPP2_PE_IP4_ADDR_UN;
3229
3230         /* Finished: go to flowid generation */
3231         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3232         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3233         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3234                                  MVPP2_PRS_RI_L3_ADDR_MASK);
3235
3236         mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV4_DIP_AI_BIT,
3237                                  MVPP2_PRS_IPV4_DIP_AI_BIT);
3238         /* Unmask all ports */
3239         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3240
3241         /* Update shadow table and hw entry */
3242         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3243         mvpp2_prs_hw_write(priv, &pe);
3244
3245         return 0;
3246 }
3247
3248 /* Initialize entries for IPv6 */
3249 static int mvpp2_prs_ip6_init(struct mvpp2 *priv)
3250 {
3251         struct mvpp2_prs_entry pe;
3252         int tid, err;
3253
3254         /* Set entries for TCP, UDP and ICMP over IPv6 */
3255         err = mvpp2_prs_ip6_proto(priv, IPPROTO_TCP,
3256                                   MVPP2_PRS_RI_L4_TCP,
3257                                   MVPP2_PRS_RI_L4_PROTO_MASK);
3258         if (err)
3259                 return err;
3260
3261         err = mvpp2_prs_ip6_proto(priv, IPPROTO_UDP,
3262                                   MVPP2_PRS_RI_L4_UDP,
3263                                   MVPP2_PRS_RI_L4_PROTO_MASK);
3264         if (err)
3265                 return err;
3266
3267         err = mvpp2_prs_ip6_proto(priv, IPPROTO_ICMPV6,
3268                                   MVPP2_PRS_RI_CPU_CODE_RX_SPEC |
3269                                   MVPP2_PRS_RI_UDF3_RX_SPECIAL,
3270                                   MVPP2_PRS_RI_CPU_CODE_MASK |
3271                                   MVPP2_PRS_RI_UDF3_MASK);
3272         if (err)
3273                 return err;
3274
3275         /* IPv4 is the last header. This is similar case as 6-TCP or 17-UDP */
3276         /* Result Info: UDF7=1, DS lite */
3277         err = mvpp2_prs_ip6_proto(priv, IPPROTO_IPIP,
3278                                   MVPP2_PRS_RI_UDF7_IP6_LITE,
3279                                   MVPP2_PRS_RI_UDF7_MASK);
3280         if (err)
3281                 return err;
3282
3283         /* IPv6 multicast */
3284         err = mvpp2_prs_ip6_cast(priv, MVPP2_PRS_L3_MULTI_CAST);
3285         if (err)
3286                 return err;
3287
3288         /* Entry for checking hop limit */
3289         tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3290                                         MVPP2_PE_LAST_FREE_TID);
3291         if (tid < 0)
3292                 return tid;
3293
3294         memset(&pe, 0, sizeof(pe));
3295         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3296         pe.index = tid;
3297
3298         /* Finished: go to flowid generation */
3299         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3300         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3301         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UN |
3302                                  MVPP2_PRS_RI_DROP_MASK,
3303                                  MVPP2_PRS_RI_L3_PROTO_MASK |
3304                                  MVPP2_PRS_RI_DROP_MASK);
3305
3306         mvpp2_prs_tcam_data_byte_set(&pe, 1, 0x00, MVPP2_PRS_IPV6_HOP_MASK);
3307         mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3308                                  MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3309
3310         /* Update shadow table and hw entry */
3311         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3312         mvpp2_prs_hw_write(priv, &pe);
3313
3314         /* Default IPv6 entry for unknown protocols */
3315         memset(&pe, 0, sizeof(pe));
3316         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3317         pe.index = MVPP2_PE_IP6_PROTO_UN;
3318
3319         /* Finished: go to flowid generation */
3320         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3321         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3322         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3323                                  MVPP2_PRS_RI_L4_PROTO_MASK);
3324         /* Set L4 offset relatively to our current place */
3325         mvpp2_prs_sram_offset_set(&pe, MVPP2_PRS_SRAM_UDF_TYPE_L4,
3326                                   sizeof(struct ipv6hdr) - 4,
3327                                   MVPP2_PRS_SRAM_OP_SEL_UDF_ADD);
3328
3329         mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3330                                  MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3331         /* Unmask all ports */
3332         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3333
3334         /* Update shadow table and hw entry */
3335         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3336         mvpp2_prs_hw_write(priv, &pe);
3337
3338         /* Default IPv6 entry for unknown ext protocols */
3339         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3340         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3341         pe.index = MVPP2_PE_IP6_EXT_PROTO_UN;
3342
3343         /* Finished: go to flowid generation */
3344         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_FLOWS);
3345         mvpp2_prs_sram_bits_set(&pe, MVPP2_PRS_SRAM_LU_GEN_BIT, 1);
3346         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L4_OTHER,
3347                                  MVPP2_PRS_RI_L4_PROTO_MASK);
3348
3349         mvpp2_prs_tcam_ai_update(&pe, MVPP2_PRS_IPV6_EXT_AI_BIT,
3350                                  MVPP2_PRS_IPV6_EXT_AI_BIT);
3351         /* Unmask all ports */
3352         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3353
3354         /* Update shadow table and hw entry */
3355         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP4);
3356         mvpp2_prs_hw_write(priv, &pe);
3357
3358         /* Default IPv6 entry for unicast address */
3359         memset(&pe, 0, sizeof(struct mvpp2_prs_entry));
3360         mvpp2_prs_tcam_lu_set(&pe, MVPP2_PRS_LU_IP6);
3361         pe.index = MVPP2_PE_IP6_ADDR_UN;
3362
3363         /* Finished: go to IPv6 again */
3364         mvpp2_prs_sram_next_lu_set(&pe, MVPP2_PRS_LU_IP6);
3365         mvpp2_prs_sram_ri_update(&pe, MVPP2_PRS_RI_L3_UCAST,
3366                                  MVPP2_PRS_RI_L3_ADDR_MASK);
3367         mvpp2_prs_sram_ai_update(&pe, MVPP2_PRS_IPV6_NO_EXT_AI_BIT,
3368                                  MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3369         /* Shift back to IPV6 NH */
3370         mvpp2_prs_sram_shift_set(&pe, -18, MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3371
3372         mvpp2_prs_tcam_ai_update(&pe, 0, MVPP2_PRS_IPV6_NO_EXT_AI_BIT);
3373         /* Unmask all ports */
3374         mvpp2_prs_tcam_port_map_set(&pe, MVPP2_PRS_PORT_MASK);
3375
3376         /* Update shadow table and hw entry */
3377         mvpp2_prs_shadow_set(priv, pe.index, MVPP2_PRS_LU_IP6);
3378         mvpp2_prs_hw_write(priv, &pe);
3379
3380         return 0;
3381 }
3382
3383 /* Parser default initialization */
3384 static int mvpp2_prs_default_init(struct platform_device *pdev,
3385                                   struct mvpp2 *priv)
3386 {
3387         int err, index, i;
3388
3389         /* Enable tcam table */
3390         mvpp2_write(priv, MVPP2_PRS_TCAM_CTRL_REG, MVPP2_PRS_TCAM_EN_MASK);
3391
3392         /* Clear all tcam and sram entries */
3393         for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++) {
3394                 mvpp2_write(priv, MVPP2_PRS_TCAM_IDX_REG, index);
3395                 for (i = 0; i < MVPP2_PRS_TCAM_WORDS; i++)
3396                         mvpp2_write(priv, MVPP2_PRS_TCAM_DATA_REG(i), 0);
3397
3398                 mvpp2_write(priv, MVPP2_PRS_SRAM_IDX_REG, index);
3399                 for (i = 0; i < MVPP2_PRS_SRAM_WORDS; i++)
3400                         mvpp2_write(priv, MVPP2_PRS_SRAM_DATA_REG(i), 0);
3401         }
3402
3403         /* Invalidate all tcam entries */
3404         for (index = 0; index < MVPP2_PRS_TCAM_SRAM_SIZE; index++)
3405                 mvpp2_prs_hw_inv(priv, index);
3406
3407         priv->prs_shadow = devm_kcalloc(&pdev->dev, MVPP2_PRS_TCAM_SRAM_SIZE,
3408                                         sizeof(*priv->prs_shadow),
3409                                         GFP_KERNEL);
3410         if (!priv->prs_shadow)
3411                 return -ENOMEM;
3412
3413         /* Always start from lookup = 0 */
3414         for (index = 0; index < MVPP2_MAX_PORTS; index++)
3415                 mvpp2_prs_hw_port_init(priv, index, MVPP2_PRS_LU_MH,
3416                                        MVPP2_PRS_PORT_LU_MAX, 0);
3417
3418         mvpp2_prs_def_flow_init(priv);
3419
3420         mvpp2_prs_mh_init(priv);
3421
3422         mvpp2_prs_mac_init(priv);
3423
3424         mvpp2_prs_dsa_init(priv);
3425
3426         err = mvpp2_prs_etype_init(priv);
3427         if (err)
3428                 return err;
3429
3430         err = mvpp2_prs_vlan_init(pdev, priv);
3431         if (err)
3432                 return err;
3433
3434         err = mvpp2_prs_pppoe_init(priv);
3435         if (err)
3436                 return err;
3437
3438         err = mvpp2_prs_ip6_init(priv);
3439         if (err)
3440                 return err;
3441
3442         err = mvpp2_prs_ip4_init(priv);
3443         if (err)
3444                 return err;
3445
3446         return 0;
3447 }
3448
3449 /* Compare MAC DA with tcam entry data */
3450 static bool mvpp2_prs_mac_range_equals(struct mvpp2_prs_entry *pe,
3451                                        const u8 *da, unsigned char *mask)
3452 {
3453         unsigned char tcam_byte, tcam_mask;
3454         int index;
3455
3456         for (index = 0; index < ETH_ALEN; index++) {
3457                 mvpp2_prs_tcam_data_byte_get(pe, index, &tcam_byte, &tcam_mask);
3458                 if (tcam_mask != mask[index])
3459                         return false;
3460
3461                 if ((tcam_mask & tcam_byte) != (da[index] & mask[index]))
3462                         return false;
3463         }
3464
3465         return true;
3466 }
3467
3468 /* Find tcam entry with matched pair <MAC DA, port> */
3469 static struct mvpp2_prs_entry *
3470 mvpp2_prs_mac_da_range_find(struct mvpp2 *priv, int pmap, const u8 *da,
3471                             unsigned char *mask, int udf_type)
3472 {
3473         struct mvpp2_prs_entry *pe;
3474         int tid;
3475
3476         pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
3477         if (!pe)
3478                 return NULL;
3479         mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3480
3481         /* Go through the all entires with MVPP2_PRS_LU_MAC */
3482         for (tid = MVPP2_PE_FIRST_FREE_TID;
3483              tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3484                 unsigned int entry_pmap;
3485
3486                 if (!priv->prs_shadow[tid].valid ||
3487                     (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3488                     (priv->prs_shadow[tid].udf != udf_type))
3489                         continue;
3490
3491                 pe->index = tid;
3492                 mvpp2_prs_hw_read(priv, pe);
3493                 entry_pmap = mvpp2_prs_tcam_port_map_get(pe);
3494
3495                 if (mvpp2_prs_mac_range_equals(pe, da, mask) &&
3496                     entry_pmap == pmap)
3497                         return pe;
3498         }
3499         kfree(pe);
3500
3501         return NULL;
3502 }
3503
3504 /* Update parser's mac da entry */
3505 static int mvpp2_prs_mac_da_accept(struct mvpp2 *priv, int port,
3506                                    const u8 *da, bool add)
3507 {
3508         struct mvpp2_prs_entry *pe;
3509         unsigned int pmap, len, ri;
3510         unsigned char mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3511         int tid;
3512
3513         /* Scan TCAM and see if entry with this <MAC DA, port> already exist */
3514         pe = mvpp2_prs_mac_da_range_find(priv, (1 << port), da, mask,
3515                                          MVPP2_PRS_UDF_MAC_DEF);
3516
3517         /* No such entry */
3518         if (!pe) {
3519                 if (!add)
3520                         return 0;
3521
3522                 /* Create new TCAM entry */
3523                 /* Find first range mac entry*/
3524                 for (tid = MVPP2_PE_FIRST_FREE_TID;
3525                      tid <= MVPP2_PE_LAST_FREE_TID; tid++)
3526                         if (priv->prs_shadow[tid].valid &&
3527                             (priv->prs_shadow[tid].lu == MVPP2_PRS_LU_MAC) &&
3528                             (priv->prs_shadow[tid].udf ==
3529                                                        MVPP2_PRS_UDF_MAC_RANGE))
3530                                 break;
3531
3532                 /* Go through the all entries from first to last */
3533                 tid = mvpp2_prs_tcam_first_free(priv, MVPP2_PE_FIRST_FREE_TID,
3534                                                 tid - 1);
3535                 if (tid < 0)
3536                         return tid;
3537
3538                 pe = kzalloc(sizeof(*pe), GFP_ATOMIC);
3539                 if (!pe)
3540                         return -ENOMEM;
3541                 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_MAC);
3542                 pe->index = tid;
3543
3544                 /* Mask all ports */
3545                 mvpp2_prs_tcam_port_map_set(pe, 0);
3546         }
3547
3548         /* Update port mask */
3549         mvpp2_prs_tcam_port_set(pe, port, add);
3550
3551         /* Invalidate the entry if no ports are left enabled */
3552         pmap = mvpp2_prs_tcam_port_map_get(pe);
3553         if (pmap == 0) {
3554                 if (add) {
3555                         kfree(pe);
3556                         return -EINVAL;
3557                 }
3558                 mvpp2_prs_hw_inv(priv, pe->index);
3559                 priv->prs_shadow[pe->index].valid = false;
3560                 kfree(pe);
3561                 return 0;
3562         }
3563
3564         /* Continue - set next lookup */
3565         mvpp2_prs_sram_next_lu_set(pe, MVPP2_PRS_LU_DSA);
3566
3567         /* Set match on DA */
3568         len = ETH_ALEN;
3569         while (len--)
3570                 mvpp2_prs_tcam_data_byte_set(pe, len, da[len], 0xff);
3571
3572         /* Set result info bits */
3573         if (is_broadcast_ether_addr(da))
3574                 ri = MVPP2_PRS_RI_L2_BCAST;
3575         else if (is_multicast_ether_addr(da))
3576                 ri = MVPP2_PRS_RI_L2_MCAST;
3577         else
3578                 ri = MVPP2_PRS_RI_L2_UCAST | MVPP2_PRS_RI_MAC_ME_MASK;
3579
3580         mvpp2_prs_sram_ri_update(pe, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3581                                  MVPP2_PRS_RI_MAC_ME_MASK);
3582         mvpp2_prs_shadow_ri_set(priv, pe->index, ri, MVPP2_PRS_RI_L2_CAST_MASK |
3583                                 MVPP2_PRS_RI_MAC_ME_MASK);
3584
3585         /* Shift to ethertype */
3586         mvpp2_prs_sram_shift_set(pe, 2 * ETH_ALEN,
3587                                  MVPP2_PRS_SRAM_OP_SEL_SHIFT_ADD);
3588
3589         /* Update shadow table and hw entry */
3590         priv->prs_shadow[pe->index].udf = MVPP2_PRS_UDF_MAC_DEF;
3591         mvpp2_prs_shadow_set(priv, pe->index, MVPP2_PRS_LU_MAC);
3592         mvpp2_prs_hw_write(priv, pe);
3593
3594         kfree(pe);
3595
3596         return 0;
3597 }
3598
3599 static int mvpp2_prs_update_mac_da(struct net_device *dev, const u8 *da)
3600 {
3601         struct mvpp2_port *port = netdev_priv(dev);
3602         int err;
3603
3604         /* Remove old parser entry */
3605         err = mvpp2_prs_mac_da_accept(port->priv, port->id, dev->dev_addr,
3606                                       false);
3607         if (err)
3608                 return err;
3609
3610         /* Add new parser entry */
3611         err = mvpp2_prs_mac_da_accept(port->priv, port->id, da, true);
3612         if (err)
3613                 return err;
3614
3615         /* Set addr in the device */
3616         ether_addr_copy(dev->dev_addr, da);
3617
3618         return 0;
3619 }
3620
3621 /* Delete all port's multicast simple (not range) entries */
3622 static void mvpp2_prs_mcast_del_all(struct mvpp2 *priv, int port)
3623 {
3624         struct mvpp2_prs_entry pe;
3625         int index, tid;
3626
3627         for (tid = MVPP2_PE_FIRST_FREE_TID;
3628              tid <= MVPP2_PE_LAST_FREE_TID; tid++) {
3629                 unsigned char da[ETH_ALEN], da_mask[ETH_ALEN];
3630
3631                 if (!priv->prs_shadow[tid].valid ||
3632                     (priv->prs_shadow[tid].lu != MVPP2_PRS_LU_MAC) ||
3633                     (priv->prs_shadow[tid].udf != MVPP2_PRS_UDF_MAC_DEF))
3634                         continue;
3635
3636                 /* Only simple mac entries */
3637                 pe.index = tid;
3638                 mvpp2_prs_hw_read(priv, &pe);
3639
3640                 /* Read mac addr from entry */
3641                 for (index = 0; index < ETH_ALEN; index++)
3642                         mvpp2_prs_tcam_data_byte_get(&pe, index, &da[index],
3643                                                      &da_mask[index]);
3644
3645                 if (is_multicast_ether_addr(da) && !is_broadcast_ether_addr(da))
3646                         /* Delete this entry */
3647                         mvpp2_prs_mac_da_accept(priv, port, da, false);
3648         }
3649 }
3650
3651 static int mvpp2_prs_tag_mode_set(struct mvpp2 *priv, int port, int type)
3652 {
3653         switch (type) {
3654         case MVPP2_TAG_TYPE_EDSA:
3655                 /* Add port to EDSA entries */
3656                 mvpp2_prs_dsa_tag_set(priv, port, true,
3657                                       MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3658                 mvpp2_prs_dsa_tag_set(priv, port, true,
3659                                       MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3660                 /* Remove port from DSA entries */
3661                 mvpp2_prs_dsa_tag_set(priv, port, false,
3662                                       MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3663                 mvpp2_prs_dsa_tag_set(priv, port, false,
3664                                       MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3665                 break;
3666
3667         case MVPP2_TAG_TYPE_DSA:
3668                 /* Add port to DSA entries */
3669                 mvpp2_prs_dsa_tag_set(priv, port, true,
3670                                       MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3671                 mvpp2_prs_dsa_tag_set(priv, port, true,
3672                                       MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3673                 /* Remove port from EDSA entries */
3674                 mvpp2_prs_dsa_tag_set(priv, port, false,
3675                                       MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3676                 mvpp2_prs_dsa_tag_set(priv, port, false,
3677                                       MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3678                 break;
3679
3680         case MVPP2_TAG_TYPE_MH:
3681         case MVPP2_TAG_TYPE_NONE:
3682                 /* Remove port form EDSA and DSA entries */
3683                 mvpp2_prs_dsa_tag_set(priv, port, false,
3684                                       MVPP2_PRS_TAGGED, MVPP2_PRS_DSA);
3685                 mvpp2_prs_dsa_tag_set(priv, port, false,
3686                                       MVPP2_PRS_UNTAGGED, MVPP2_PRS_DSA);
3687                 mvpp2_prs_dsa_tag_set(priv, port, false,
3688                                       MVPP2_PRS_TAGGED, MVPP2_PRS_EDSA);
3689                 mvpp2_prs_dsa_tag_set(priv, port, false,
3690                                       MVPP2_PRS_UNTAGGED, MVPP2_PRS_EDSA);
3691                 break;
3692
3693         default:
3694                 if ((type < 0) || (type > MVPP2_TAG_TYPE_EDSA))
3695                         return -EINVAL;
3696         }
3697
3698         return 0;
3699 }
3700
3701 /* Set prs flow for the port */
3702 static int mvpp2_prs_def_flow(struct mvpp2_port *port)
3703 {
3704         struct mvpp2_prs_entry *pe;
3705         int tid;
3706
3707         pe = mvpp2_prs_flow_find(port->priv, port->id);
3708
3709         /* Such entry not exist */
3710         if (!pe) {
3711                 /* Go through the all entires from last to first */
3712                 tid = mvpp2_prs_tcam_first_free(port->priv,
3713                                                 MVPP2_PE_LAST_FREE_TID,
3714                                                MVPP2_PE_FIRST_FREE_TID);
3715                 if (tid < 0)
3716                         return tid;
3717
3718                 pe = kzalloc(sizeof(*pe), GFP_KERNEL);
3719                 if (!pe)
3720                         return -ENOMEM;
3721
3722                 mvpp2_prs_tcam_lu_set(pe, MVPP2_PRS_LU_FLOWS);
3723                 pe->index = tid;
3724
3725                 /* Set flow ID*/
3726                 mvpp2_prs_sram_ai_update(pe, port->id, MVPP2_PRS_FLOW_ID_MASK);
3727                 mvpp2_prs_sram_bits_set(pe, MVPP2_PRS_SRAM_LU_DONE_BIT, 1);
3728
3729                 /* Update shadow table */
3730                 mvpp2_prs_shadow_set(port->priv, pe->index, MVPP2_PRS_LU_FLOWS);
3731         }
3732
3733         mvpp2_prs_tcam_port_map_set(pe, (1 << port->id));
3734         mvpp2_prs_hw_write(port->priv, pe);
3735         kfree(pe);
3736
3737         return 0;
3738 }
3739
3740 /* Classifier configuration routines */
3741
3742 /* Update classification flow table registers */
3743 static void mvpp2_cls_flow_write(struct mvpp2 *priv,
3744                                  struct mvpp2_cls_flow_entry *fe)
3745 {
3746         mvpp2_write(priv, MVPP2_CLS_FLOW_INDEX_REG, fe->index);
3747         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL0_REG,  fe->data[0]);
3748         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL1_REG,  fe->data[1]);
3749         mvpp2_write(priv, MVPP2_CLS_FLOW_TBL2_REG,  fe->data[2]);
3750 }
3751
3752 /* Update classification lookup table register */
3753 static void mvpp2_cls_lookup_write(struct mvpp2 *priv,
3754                                    struct mvpp2_cls_lookup_entry *le)
3755 {
3756         u32 val;
3757
3758         val = (le->way << MVPP2_CLS_LKP_INDEX_WAY_OFFS) | le->lkpid;
3759         mvpp2_write(priv, MVPP2_CLS_LKP_INDEX_REG, val);
3760         mvpp2_write(priv, MVPP2_CLS_LKP_TBL_REG, le->data);
3761 }
3762
3763 /* Classifier default initialization */
3764 static void mvpp2_cls_init(struct mvpp2 *priv)
3765 {
3766         struct mvpp2_cls_lookup_entry le;
3767         struct mvpp2_cls_flow_entry fe;
3768         int index;
3769
3770         /* Enable classifier */
3771         mvpp2_write(priv, MVPP2_CLS_MODE_REG, MVPP2_CLS_MODE_ACTIVE_MASK);
3772
3773         /* Clear classifier flow table */
3774         memset(&fe.data, 0, sizeof(fe.data));
3775         for (index = 0; index < MVPP2_CLS_FLOWS_TBL_SIZE; index++) {
3776                 fe.index = index;
3777                 mvpp2_cls_flow_write(priv, &fe);
3778         }
3779
3780         /* Clear classifier lookup table */
3781         le.data = 0;
3782         for (index = 0; index < MVPP2_CLS_LKP_TBL_SIZE; index++) {
3783                 le.lkpid = index;
3784                 le.way = 0;
3785                 mvpp2_cls_lookup_write(priv, &le);
3786
3787                 le.way = 1;
3788                 mvpp2_cls_lookup_write(priv, &le);
3789         }
3790 }
3791
3792 static void mvpp2_cls_port_config(struct mvpp2_port *port)
3793 {
3794         struct mvpp2_cls_lookup_entry le;
3795         u32 val;
3796
3797         /* Set way for the port */
3798         val = mvpp2_read(port->priv, MVPP2_CLS_PORT_WAY_REG);
3799         val &= ~MVPP2_CLS_PORT_WAY_MASK(port->id);
3800         mvpp2_write(port->priv, MVPP2_CLS_PORT_WAY_REG, val);
3801
3802         /* Pick the entry to be accessed in lookup ID decoding table
3803          * according to the way and lkpid.
3804          */
3805         le.lkpid = port->id;
3806         le.way = 0;
3807         le.data = 0;
3808
3809         /* Set initial CPU queue for receiving packets */
3810         le.data &= ~MVPP2_CLS_LKP_TBL_RXQ_MASK;
3811         le.data |= port->first_rxq;
3812
3813         /* Disable classification engines */
3814         le.data &= ~MVPP2_CLS_LKP_TBL_LOOKUP_EN_MASK;
3815
3816         /* Update lookup ID table entry */
3817         mvpp2_cls_lookup_write(port->priv, &le);
3818 }
3819
3820 /* Set CPU queue number for oversize packets */
3821 static void mvpp2_cls_oversize_rxq_set(struct mvpp2_port *port)
3822 {
3823         u32 val;
3824
3825         mvpp2_write(port->priv, MVPP2_CLS_OVERSIZE_RXQ_LOW_REG(port->id),
3826                     port->first_rxq & MVPP2_CLS_OVERSIZE_RXQ_LOW_MASK);
3827
3828         mvpp2_write(port->priv, MVPP2_CLS_SWFWD_P2HQ_REG(port->id),
3829                     (port->first_rxq >> MVPP2_CLS_OVERSIZE_RXQ_LOW_BITS));
3830
3831         val = mvpp2_read(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG);
3832         val |= MVPP2_CLS_SWFWD_PCTRL_MASK(port->id);
3833         mvpp2_write(port->priv, MVPP2_CLS_SWFWD_PCTRL_REG, val);
3834 }
3835
3836 static void *mvpp2_frag_alloc(const struct mvpp2_bm_pool *pool)
3837 {
3838         if (likely(pool->frag_size <= PAGE_SIZE))
3839                 return netdev_alloc_frag(pool->frag_size);
3840         else
3841                 return kmalloc(pool->frag_size, GFP_ATOMIC);
3842 }
3843
3844 static void mvpp2_frag_free(const struct mvpp2_bm_pool *pool, void *data)
3845 {
3846         if (likely(pool->frag_size <= PAGE_SIZE))
3847                 skb_free_frag(data);
3848         else
3849                 kfree(data);
3850 }
3851
3852 /* Buffer Manager configuration routines */
3853
3854 /* Create pool */
3855 static int mvpp2_bm_pool_create(struct platform_device *pdev,
3856                                 struct mvpp2 *priv,
3857                                 struct mvpp2_bm_pool *bm_pool, int size)
3858 {
3859         u32 val;
3860
3861         /* Number of buffer pointers must be a multiple of 16, as per
3862          * hardware constraints
3863          */
3864         if (!IS_ALIGNED(size, 16))
3865                 return -EINVAL;
3866
3867         /* PPv2.1 needs 8 bytes per buffer pointer, PPv2.2 needs 16
3868          * bytes per buffer pointer
3869          */
3870         if (priv->hw_version == MVPP21)
3871                 bm_pool->size_bytes = 2 * sizeof(u32) * size;
3872         else
3873                 bm_pool->size_bytes = 2 * sizeof(u64) * size;
3874
3875         bm_pool->virt_addr = dma_alloc_coherent(&pdev->dev, bm_pool->size_bytes,
3876                                                 &bm_pool->dma_addr,
3877                                                 GFP_KERNEL);
3878         if (!bm_pool->virt_addr)
3879                 return -ENOMEM;
3880
3881         if (!IS_ALIGNED((unsigned long)bm_pool->virt_addr,
3882                         MVPP2_BM_POOL_PTR_ALIGN)) {
3883                 dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3884                                   bm_pool->virt_addr, bm_pool->dma_addr);
3885                 dev_err(&pdev->dev, "BM pool %d is not %d bytes aligned\n",
3886                         bm_pool->id, MVPP2_BM_POOL_PTR_ALIGN);
3887                 return -ENOMEM;
3888         }
3889
3890         mvpp2_write(priv, MVPP2_BM_POOL_BASE_REG(bm_pool->id),
3891                     lower_32_bits(bm_pool->dma_addr));
3892         mvpp2_write(priv, MVPP2_BM_POOL_SIZE_REG(bm_pool->id), size);
3893
3894         val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3895         val |= MVPP2_BM_START_MASK;
3896         mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3897
3898         bm_pool->type = MVPP2_BM_FREE;
3899         bm_pool->size = size;
3900         bm_pool->pkt_size = 0;
3901         bm_pool->buf_num = 0;
3902
3903         return 0;
3904 }
3905
3906 /* Set pool buffer size */
3907 static void mvpp2_bm_pool_bufsize_set(struct mvpp2 *priv,
3908                                       struct mvpp2_bm_pool *bm_pool,
3909                                       int buf_size)
3910 {
3911         u32 val;
3912
3913         bm_pool->buf_size = buf_size;
3914
3915         val = ALIGN(buf_size, 1 << MVPP2_POOL_BUF_SIZE_OFFSET);
3916         mvpp2_write(priv, MVPP2_POOL_BUF_SIZE_REG(bm_pool->id), val);
3917 }
3918
3919 static void mvpp2_bm_bufs_get_addrs(struct device *dev, struct mvpp2 *priv,
3920                                     struct mvpp2_bm_pool *bm_pool,
3921                                     dma_addr_t *dma_addr,
3922                                     phys_addr_t *phys_addr)
3923 {
3924         int cpu = get_cpu();
3925
3926         *dma_addr = mvpp2_percpu_read(priv, cpu,
3927                                       MVPP2_BM_PHY_ALLOC_REG(bm_pool->id));
3928         *phys_addr = mvpp2_percpu_read(priv, cpu, MVPP2_BM_VIRT_ALLOC_REG);
3929
3930         if (priv->hw_version == MVPP22) {
3931                 u32 val;
3932                 u32 dma_addr_highbits, phys_addr_highbits;
3933
3934                 val = mvpp2_percpu_read(priv, cpu, MVPP22_BM_ADDR_HIGH_ALLOC);
3935                 dma_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_PHYS_MASK);
3936                 phys_addr_highbits = (val & MVPP22_BM_ADDR_HIGH_VIRT_MASK) >>
3937                         MVPP22_BM_ADDR_HIGH_VIRT_SHIFT;
3938
3939                 if (sizeof(dma_addr_t) == 8)
3940                         *dma_addr |= (u64)dma_addr_highbits << 32;
3941
3942                 if (sizeof(phys_addr_t) == 8)
3943                         *phys_addr |= (u64)phys_addr_highbits << 32;
3944         }
3945
3946         put_cpu();
3947 }
3948
3949 /* Free all buffers from the pool */
3950 static void mvpp2_bm_bufs_free(struct device *dev, struct mvpp2 *priv,
3951                                struct mvpp2_bm_pool *bm_pool)
3952 {
3953         int i;
3954
3955         for (i = 0; i < bm_pool->buf_num; i++) {
3956                 dma_addr_t buf_dma_addr;
3957                 phys_addr_t buf_phys_addr;
3958                 void *data;
3959
3960                 mvpp2_bm_bufs_get_addrs(dev, priv, bm_pool,
3961                                         &buf_dma_addr, &buf_phys_addr);
3962
3963                 dma_unmap_single(dev, buf_dma_addr,
3964                                  bm_pool->buf_size, DMA_FROM_DEVICE);
3965
3966                 data = (void *)phys_to_virt(buf_phys_addr);
3967                 if (!data)
3968                         break;
3969
3970                 mvpp2_frag_free(bm_pool, data);
3971         }
3972
3973         /* Update BM driver with number of buffers removed from pool */
3974         bm_pool->buf_num -= i;
3975 }
3976
3977 /* Cleanup pool */
3978 static int mvpp2_bm_pool_destroy(struct platform_device *pdev,
3979                                  struct mvpp2 *priv,
3980                                  struct mvpp2_bm_pool *bm_pool)
3981 {
3982         u32 val;
3983
3984         mvpp2_bm_bufs_free(&pdev->dev, priv, bm_pool);
3985         if (bm_pool->buf_num) {
3986                 WARN(1, "cannot free all buffers in pool %d\n", bm_pool->id);
3987                 return 0;
3988         }
3989
3990         val = mvpp2_read(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id));
3991         val |= MVPP2_BM_STOP_MASK;
3992         mvpp2_write(priv, MVPP2_BM_POOL_CTRL_REG(bm_pool->id), val);
3993
3994         dma_free_coherent(&pdev->dev, bm_pool->size_bytes,
3995                           bm_pool->virt_addr,
3996                           bm_pool->dma_addr);
3997         return 0;
3998 }
3999
4000 static int mvpp2_bm_pools_init(struct platform_device *pdev,
4001                                struct mvpp2 *priv)
4002 {
4003         int i, err, size;
4004         struct mvpp2_bm_pool *bm_pool;
4005
4006         /* Create all pools with maximum size */
4007         size = MVPP2_BM_POOL_SIZE_MAX;
4008         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4009                 bm_pool = &priv->bm_pools[i];
4010                 bm_pool->id = i;
4011                 err = mvpp2_bm_pool_create(pdev, priv, bm_pool, size);
4012                 if (err)
4013                         goto err_unroll_pools;
4014                 mvpp2_bm_pool_bufsize_set(priv, bm_pool, 0);
4015         }
4016         return 0;
4017
4018 err_unroll_pools:
4019         dev_err(&pdev->dev, "failed to create BM pool %d, size %d\n", i, size);
4020         for (i = i - 1; i >= 0; i--)
4021                 mvpp2_bm_pool_destroy(pdev, priv, &priv->bm_pools[i]);
4022         return err;
4023 }
4024
4025 static int mvpp2_bm_init(struct platform_device *pdev, struct mvpp2 *priv)
4026 {
4027         int i, err;
4028
4029         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
4030                 /* Mask BM all interrupts */
4031                 mvpp2_write(priv, MVPP2_BM_INTR_MASK_REG(i), 0);
4032                 /* Clear BM cause register */
4033                 mvpp2_write(priv, MVPP2_BM_INTR_CAUSE_REG(i), 0);
4034         }
4035
4036         /* Allocate and initialize BM pools */
4037         priv->bm_pools = devm_kcalloc(&pdev->dev, MVPP2_BM_POOLS_NUM,
4038                                       sizeof(*priv->bm_pools), GFP_KERNEL);
4039         if (!priv->bm_pools)
4040                 return -ENOMEM;
4041
4042         err = mvpp2_bm_pools_init(pdev, priv);
4043         if (err < 0)
4044                 return err;
4045         return 0;
4046 }
4047
4048 /* Attach long pool to rxq */
4049 static void mvpp2_rxq_long_pool_set(struct mvpp2_port *port,
4050                                     int lrxq, int long_pool)
4051 {
4052         u32 val, mask;
4053         int prxq;
4054
4055         /* Get queue physical ID */
4056         prxq = port->rxqs[lrxq]->id;
4057
4058         if (port->priv->hw_version == MVPP21)
4059                 mask = MVPP21_RXQ_POOL_LONG_MASK;
4060         else
4061                 mask = MVPP22_RXQ_POOL_LONG_MASK;
4062
4063         val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4064         val &= ~mask;
4065         val |= (long_pool << MVPP2_RXQ_POOL_LONG_OFFS) & mask;
4066         mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4067 }
4068
4069 /* Attach short pool to rxq */
4070 static void mvpp2_rxq_short_pool_set(struct mvpp2_port *port,
4071                                      int lrxq, int short_pool)
4072 {
4073         u32 val, mask;
4074         int prxq;
4075
4076         /* Get queue physical ID */
4077         prxq = port->rxqs[lrxq]->id;
4078
4079         if (port->priv->hw_version == MVPP21)
4080                 mask = MVPP21_RXQ_POOL_SHORT_MASK;
4081         else
4082                 mask = MVPP22_RXQ_POOL_SHORT_MASK;
4083
4084         val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
4085         val &= ~mask;
4086         val |= (short_pool << MVPP2_RXQ_POOL_SHORT_OFFS) & mask;
4087         mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
4088 }
4089
4090 static void *mvpp2_buf_alloc(struct mvpp2_port *port,
4091                              struct mvpp2_bm_pool *bm_pool,
4092                              dma_addr_t *buf_dma_addr,
4093                              phys_addr_t *buf_phys_addr,
4094                              gfp_t gfp_mask)
4095 {
4096         dma_addr_t dma_addr;
4097         void *data;
4098
4099         data = mvpp2_frag_alloc(bm_pool);
4100         if (!data)
4101                 return NULL;
4102
4103         dma_addr = dma_map_single(port->dev->dev.parent, data,
4104                                   MVPP2_RX_BUF_SIZE(bm_pool->pkt_size),
4105                                   DMA_FROM_DEVICE);
4106         if (unlikely(dma_mapping_error(port->dev->dev.parent, dma_addr))) {
4107                 mvpp2_frag_free(bm_pool, data);
4108                 return NULL;
4109         }
4110         *buf_dma_addr = dma_addr;
4111         *buf_phys_addr = virt_to_phys(data);
4112
4113         return data;
4114 }
4115
4116 /* Release buffer to BM */
4117 static inline void mvpp2_bm_pool_put(struct mvpp2_port *port, int pool,
4118                                      dma_addr_t buf_dma_addr,
4119                                      phys_addr_t buf_phys_addr)
4120 {
4121         int cpu = get_cpu();
4122
4123         if (port->priv->hw_version == MVPP22) {
4124                 u32 val = 0;
4125
4126                 if (sizeof(dma_addr_t) == 8)
4127                         val |= upper_32_bits(buf_dma_addr) &
4128                                 MVPP22_BM_ADDR_HIGH_PHYS_RLS_MASK;
4129
4130                 if (sizeof(phys_addr_t) == 8)
4131                         val |= (upper_32_bits(buf_phys_addr)
4132                                 << MVPP22_BM_ADDR_HIGH_VIRT_RLS_SHIFT) &
4133                                 MVPP22_BM_ADDR_HIGH_VIRT_RLS_MASK;
4134
4135                 mvpp2_percpu_write(port->priv, cpu,
4136                                    MVPP22_BM_ADDR_HIGH_RLS_REG, val);
4137         }
4138
4139         /* MVPP2_BM_VIRT_RLS_REG is not interpreted by HW, and simply
4140          * returned in the "cookie" field of the RX
4141          * descriptor. Instead of storing the virtual address, we
4142          * store the physical address
4143          */
4144         mvpp2_percpu_write(port->priv, cpu,
4145                            MVPP2_BM_VIRT_RLS_REG, buf_phys_addr);
4146         mvpp2_percpu_write(port->priv, cpu,
4147                            MVPP2_BM_PHY_RLS_REG(pool), buf_dma_addr);
4148
4149         put_cpu();
4150 }
4151
4152 /* Allocate buffers for the pool */
4153 static int mvpp2_bm_bufs_add(struct mvpp2_port *port,
4154                              struct mvpp2_bm_pool *bm_pool, int buf_num)
4155 {
4156         int i, buf_size, total_size;
4157         dma_addr_t dma_addr;
4158         phys_addr_t phys_addr;
4159         void *buf;
4160
4161         buf_size = MVPP2_RX_BUF_SIZE(bm_pool->pkt_size);
4162         total_size = MVPP2_RX_TOTAL_SIZE(buf_size);
4163
4164         if (buf_num < 0 ||
4165             (buf_num + bm_pool->buf_num > bm_pool->size)) {
4166                 netdev_err(port->dev,
4167                            "cannot allocate %d buffers for pool %d\n",
4168                            buf_num, bm_pool->id);
4169                 return 0;
4170         }
4171
4172         for (i = 0; i < buf_num; i++) {
4173                 buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr,
4174                                       &phys_addr, GFP_KERNEL);
4175                 if (!buf)
4176                         break;
4177
4178                 mvpp2_bm_pool_put(port, bm_pool->id, dma_addr,
4179                                   phys_addr);
4180         }
4181
4182         /* Update BM driver with number of buffers added to pool */
4183         bm_pool->buf_num += i;
4184
4185         netdev_dbg(port->dev,
4186                    "%s pool %d: pkt_size=%4d, buf_size=%4d, total_size=%4d\n",
4187                    bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4188                    bm_pool->id, bm_pool->pkt_size, buf_size, total_size);
4189
4190         netdev_dbg(port->dev,
4191                    "%s pool %d: %d of %d buffers added\n",
4192                    bm_pool->type == MVPP2_BM_SWF_SHORT ? "short" : " long",
4193                    bm_pool->id, i, buf_num);
4194         return i;
4195 }
4196
4197 /* Notify the driver that BM pool is being used as specific type and return the
4198  * pool pointer on success
4199  */
4200 static struct mvpp2_bm_pool *
4201 mvpp2_bm_pool_use(struct mvpp2_port *port, int pool, enum mvpp2_bm_type type,
4202                   int pkt_size)
4203 {
4204         struct mvpp2_bm_pool *new_pool = &port->priv->bm_pools[pool];
4205         int num;
4206
4207         if (new_pool->type != MVPP2_BM_FREE && new_pool->type != type) {
4208                 netdev_err(port->dev, "mixing pool types is forbidden\n");
4209                 return NULL;
4210         }
4211
4212         if (new_pool->type == MVPP2_BM_FREE)
4213                 new_pool->type = type;
4214
4215         /* Allocate buffers in case BM pool is used as long pool, but packet
4216          * size doesn't match MTU or BM pool hasn't being used yet
4217          */
4218         if (((type == MVPP2_BM_SWF_LONG) && (pkt_size > new_pool->pkt_size)) ||
4219             (new_pool->pkt_size == 0)) {
4220                 int pkts_num;
4221
4222                 /* Set default buffer number or free all the buffers in case
4223                  * the pool is not empty
4224                  */
4225                 pkts_num = new_pool->buf_num;
4226                 if (pkts_num == 0)
4227                         pkts_num = type == MVPP2_BM_SWF_LONG ?
4228                                    MVPP2_BM_LONG_BUF_NUM :
4229                                    MVPP2_BM_SHORT_BUF_NUM;
4230                 else
4231                         mvpp2_bm_bufs_free(port->dev->dev.parent,
4232                                            port->priv, new_pool);
4233
4234                 new_pool->pkt_size = pkt_size;
4235                 new_pool->frag_size =
4236                         SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4237                         MVPP2_SKB_SHINFO_SIZE;
4238
4239                 /* Allocate buffers for this pool */
4240                 num = mvpp2_bm_bufs_add(port, new_pool, pkts_num);
4241                 if (num != pkts_num) {
4242                         WARN(1, "pool %d: %d of %d allocated\n",
4243                              new_pool->id, num, pkts_num);
4244                         return NULL;
4245                 }
4246         }
4247
4248         mvpp2_bm_pool_bufsize_set(port->priv, new_pool,
4249                                   MVPP2_RX_BUF_SIZE(new_pool->pkt_size));
4250
4251         return new_pool;
4252 }
4253
4254 /* Initialize pools for swf */
4255 static int mvpp2_swf_bm_pool_init(struct mvpp2_port *port)
4256 {
4257         int rxq;
4258
4259         if (!port->pool_long) {
4260                 port->pool_long =
4261                        mvpp2_bm_pool_use(port, MVPP2_BM_SWF_LONG_POOL(port->id),
4262                                          MVPP2_BM_SWF_LONG,
4263                                          port->pkt_size);
4264                 if (!port->pool_long)
4265                         return -ENOMEM;
4266
4267                 port->pool_long->port_map |= (1 << port->id);
4268
4269                 for (rxq = 0; rxq < port->nrxqs; rxq++)
4270                         mvpp2_rxq_long_pool_set(port, rxq, port->pool_long->id);
4271         }
4272
4273         if (!port->pool_short) {
4274                 port->pool_short =
4275                         mvpp2_bm_pool_use(port, MVPP2_BM_SWF_SHORT_POOL,
4276                                           MVPP2_BM_SWF_SHORT,
4277                                           MVPP2_BM_SHORT_PKT_SIZE);
4278                 if (!port->pool_short)
4279                         return -ENOMEM;
4280
4281                 port->pool_short->port_map |= (1 << port->id);
4282
4283                 for (rxq = 0; rxq < port->nrxqs; rxq++)
4284                         mvpp2_rxq_short_pool_set(port, rxq,
4285                                                  port->pool_short->id);
4286         }
4287
4288         return 0;
4289 }
4290
4291 static int mvpp2_bm_update_mtu(struct net_device *dev, int mtu)
4292 {
4293         struct mvpp2_port *port = netdev_priv(dev);
4294         struct mvpp2_bm_pool *port_pool = port->pool_long;
4295         int num, pkts_num = port_pool->buf_num;
4296         int pkt_size = MVPP2_RX_PKT_SIZE(mtu);
4297
4298         /* Update BM pool with new buffer size */
4299         mvpp2_bm_bufs_free(dev->dev.parent, port->priv, port_pool);
4300         if (port_pool->buf_num) {
4301                 WARN(1, "cannot free all buffers in pool %d\n", port_pool->id);
4302                 return -EIO;
4303         }
4304
4305         port_pool->pkt_size = pkt_size;
4306         port_pool->frag_size = SKB_DATA_ALIGN(MVPP2_RX_BUF_SIZE(pkt_size)) +
4307                 MVPP2_SKB_SHINFO_SIZE;
4308         num = mvpp2_bm_bufs_add(port, port_pool, pkts_num);
4309         if (num != pkts_num) {
4310                 WARN(1, "pool %d: %d of %d allocated\n",
4311                      port_pool->id, num, pkts_num);
4312                 return -EIO;
4313         }
4314
4315         mvpp2_bm_pool_bufsize_set(port->priv, port_pool,
4316                                   MVPP2_RX_BUF_SIZE(port_pool->pkt_size));
4317         dev->mtu = mtu;
4318         netdev_update_features(dev);
4319         return 0;
4320 }
4321
4322 static inline void mvpp2_interrupts_enable(struct mvpp2_port *port)
4323 {
4324         int i, sw_thread_mask = 0;
4325
4326         for (i = 0; i < port->nqvecs; i++)
4327                 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4328
4329         mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4330                     MVPP2_ISR_ENABLE_INTERRUPT(sw_thread_mask));
4331 }
4332
4333 static inline void mvpp2_interrupts_disable(struct mvpp2_port *port)
4334 {
4335         int i, sw_thread_mask = 0;
4336
4337         for (i = 0; i < port->nqvecs; i++)
4338                 sw_thread_mask |= port->qvecs[i].sw_thread_mask;
4339
4340         mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4341                     MVPP2_ISR_DISABLE_INTERRUPT(sw_thread_mask));
4342 }
4343
4344 static inline void mvpp2_qvec_interrupt_enable(struct mvpp2_queue_vector *qvec)
4345 {
4346         struct mvpp2_port *port = qvec->port;
4347
4348         mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4349                     MVPP2_ISR_ENABLE_INTERRUPT(qvec->sw_thread_mask));
4350 }
4351
4352 static inline void mvpp2_qvec_interrupt_disable(struct mvpp2_queue_vector *qvec)
4353 {
4354         struct mvpp2_port *port = qvec->port;
4355
4356         mvpp2_write(port->priv, MVPP2_ISR_ENABLE_REG(port->id),
4357                     MVPP2_ISR_DISABLE_INTERRUPT(qvec->sw_thread_mask));
4358 }
4359
4360 /* Mask the current CPU's Rx/Tx interrupts
4361  * Called by on_each_cpu(), guaranteed to run with migration disabled,
4362  * using smp_processor_id() is OK.
4363  */
4364 static void mvpp2_interrupts_mask(void *arg)
4365 {
4366         struct mvpp2_port *port = arg;
4367
4368         mvpp2_percpu_write(port->priv, smp_processor_id(),
4369                            MVPP2_ISR_RX_TX_MASK_REG(port->id), 0);
4370 }
4371
4372 /* Unmask the current CPU's Rx/Tx interrupts.
4373  * Called by on_each_cpu(), guaranteed to run with migration disabled,
4374  * using smp_processor_id() is OK.
4375  */
4376 static void mvpp2_interrupts_unmask(void *arg)
4377 {
4378         struct mvpp2_port *port = arg;
4379         u32 val;
4380
4381         val = MVPP2_CAUSE_MISC_SUM_MASK |
4382                 MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4383         if (port->has_tx_irqs)
4384                 val |= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
4385
4386         mvpp2_percpu_write(port->priv, smp_processor_id(),
4387                            MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4388 }
4389
4390 static void
4391 mvpp2_shared_interrupt_mask_unmask(struct mvpp2_port *port, bool mask)
4392 {
4393         u32 val;
4394         int i;
4395
4396         if (port->priv->hw_version != MVPP22)
4397                 return;
4398
4399         if (mask)
4400                 val = 0;
4401         else
4402                 val = MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
4403
4404         for (i = 0; i < port->nqvecs; i++) {
4405                 struct mvpp2_queue_vector *v = port->qvecs + i;
4406
4407                 if (v->type != MVPP2_QUEUE_VECTOR_SHARED)
4408                         continue;
4409
4410                 mvpp2_percpu_write(port->priv, v->sw_thread_id,
4411                                    MVPP2_ISR_RX_TX_MASK_REG(port->id), val);
4412         }
4413 }
4414
4415 /* Port configuration routines */
4416
4417 static void mvpp22_gop_init_rgmii(struct mvpp2_port *port)
4418 {
4419         struct mvpp2 *priv = port->priv;
4420         u32 val;
4421
4422         regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4423         val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT;
4424         regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4425
4426         regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4427         if (port->gop_id == 2)
4428                 val |= GENCONF_CTRL0_PORT0_RGMII | GENCONF_CTRL0_PORT1_RGMII;
4429         else if (port->gop_id == 3)
4430                 val |= GENCONF_CTRL0_PORT1_RGMII_MII;
4431         regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4432 }
4433
4434 static void mvpp22_gop_init_sgmii(struct mvpp2_port *port)
4435 {
4436         struct mvpp2 *priv = port->priv;
4437         u32 val;
4438
4439         regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4440         val |= GENCONF_PORT_CTRL0_BUS_WIDTH_SELECT |
4441                GENCONF_PORT_CTRL0_RX_DATA_SAMPLE;
4442         regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4443
4444         if (port->gop_id > 1) {
4445                 regmap_read(priv->sysctrl_base, GENCONF_CTRL0, &val);
4446                 if (port->gop_id == 2)
4447                         val &= ~GENCONF_CTRL0_PORT0_RGMII;
4448                 else if (port->gop_id == 3)
4449                         val &= ~GENCONF_CTRL0_PORT1_RGMII_MII;
4450                 regmap_write(priv->sysctrl_base, GENCONF_CTRL0, val);
4451         }
4452 }
4453
4454 static void mvpp22_gop_init_10gkr(struct mvpp2_port *port)
4455 {
4456         struct mvpp2 *priv = port->priv;
4457         void __iomem *mpcs = priv->iface_base + MVPP22_MPCS_BASE(port->gop_id);
4458         void __iomem *xpcs = priv->iface_base + MVPP22_XPCS_BASE(port->gop_id);
4459         u32 val;
4460
4461         /* XPCS */
4462         val = readl(xpcs + MVPP22_XPCS_CFG0);
4463         val &= ~(MVPP22_XPCS_CFG0_PCS_MODE(0x3) |
4464                  MVPP22_XPCS_CFG0_ACTIVE_LANE(0x3));
4465         val |= MVPP22_XPCS_CFG0_ACTIVE_LANE(2);
4466         writel(val, xpcs + MVPP22_XPCS_CFG0);
4467
4468         /* MPCS */
4469         val = readl(mpcs + MVPP22_MPCS_CTRL);
4470         val &= ~MVPP22_MPCS_CTRL_FWD_ERR_CONN;
4471         writel(val, mpcs + MVPP22_MPCS_CTRL);
4472
4473         val = readl(mpcs + MVPP22_MPCS_CLK_RESET);
4474         val &= ~(MVPP22_MPCS_CLK_RESET_DIV_RATIO(0x7) | MAC_CLK_RESET_MAC |
4475                  MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX);
4476         val |= MVPP22_MPCS_CLK_RESET_DIV_RATIO(1);
4477         writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4478
4479         val &= ~MVPP22_MPCS_CLK_RESET_DIV_SET;
4480         val |= MAC_CLK_RESET_MAC | MAC_CLK_RESET_SD_RX | MAC_CLK_RESET_SD_TX;
4481         writel(val, mpcs + MVPP22_MPCS_CLK_RESET);
4482 }
4483
4484 static int mvpp22_gop_init(struct mvpp2_port *port)
4485 {
4486         struct mvpp2 *priv = port->priv;
4487         u32 val;
4488
4489         if (!priv->sysctrl_base)
4490                 return 0;
4491
4492         switch (port->phy_interface) {
4493         case PHY_INTERFACE_MODE_RGMII:
4494         case PHY_INTERFACE_MODE_RGMII_ID:
4495         case PHY_INTERFACE_MODE_RGMII_RXID:
4496         case PHY_INTERFACE_MODE_RGMII_TXID:
4497                 if (port->gop_id == 0)
4498                         goto invalid_conf;
4499                 mvpp22_gop_init_rgmii(port);
4500                 break;
4501         case PHY_INTERFACE_MODE_SGMII:
4502                 mvpp22_gop_init_sgmii(port);
4503                 break;
4504         case PHY_INTERFACE_MODE_10GKR:
4505                 if (port->gop_id != 0)
4506                         goto invalid_conf;
4507                 mvpp22_gop_init_10gkr(port);
4508                 break;
4509         default:
4510                 goto unsupported_conf;
4511         }
4512
4513         regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL1, &val);
4514         val |= GENCONF_PORT_CTRL1_RESET(port->gop_id) |
4515                GENCONF_PORT_CTRL1_EN(port->gop_id);
4516         regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL1, val);
4517
4518         regmap_read(priv->sysctrl_base, GENCONF_PORT_CTRL0, &val);
4519         val |= GENCONF_PORT_CTRL0_CLK_DIV_PHASE_CLR;
4520         regmap_write(priv->sysctrl_base, GENCONF_PORT_CTRL0, val);
4521
4522         regmap_read(priv->sysctrl_base, GENCONF_SOFT_RESET1, &val);
4523         val |= GENCONF_SOFT_RESET1_GOP;
4524         regmap_write(priv->sysctrl_base, GENCONF_SOFT_RESET1, val);
4525
4526 unsupported_conf:
4527         return 0;
4528
4529 invalid_conf:
4530         netdev_err(port->dev, "Invalid port configuration\n");
4531         return -EINVAL;
4532 }
4533
4534 static void mvpp22_gop_unmask_irq(struct mvpp2_port *port)
4535 {
4536         u32 val;
4537
4538         if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4539             port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4540                 /* Enable the GMAC link status irq for this port */
4541                 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4542                 val |= MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4543                 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4544         }
4545
4546         if (port->gop_id == 0) {
4547                 /* Enable the XLG/GIG irqs for this port */
4548                 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4549                 if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4550                         val |= MVPP22_XLG_EXT_INT_MASK_XLG;
4551                 else
4552                         val |= MVPP22_XLG_EXT_INT_MASK_GIG;
4553                 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4554         }
4555 }
4556
4557 static void mvpp22_gop_mask_irq(struct mvpp2_port *port)
4558 {
4559         u32 val;
4560
4561         if (port->gop_id == 0) {
4562                 val = readl(port->base + MVPP22_XLG_EXT_INT_MASK);
4563                 val &= ~(MVPP22_XLG_EXT_INT_MASK_XLG |
4564                          MVPP22_XLG_EXT_INT_MASK_GIG);
4565                 writel(val, port->base + MVPP22_XLG_EXT_INT_MASK);
4566         }
4567
4568         if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4569             port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4570                 val = readl(port->base + MVPP22_GMAC_INT_SUM_MASK);
4571                 val &= ~MVPP22_GMAC_INT_SUM_MASK_LINK_STAT;
4572                 writel(val, port->base + MVPP22_GMAC_INT_SUM_MASK);
4573         }
4574 }
4575
4576 static void mvpp22_gop_setup_irq(struct mvpp2_port *port)
4577 {
4578         u32 val;
4579
4580         if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4581             port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4582                 val = readl(port->base + MVPP22_GMAC_INT_MASK);
4583                 val |= MVPP22_GMAC_INT_MASK_LINK_STAT;
4584                 writel(val, port->base + MVPP22_GMAC_INT_MASK);
4585         }
4586
4587         if (port->gop_id == 0) {
4588                 val = readl(port->base + MVPP22_XLG_INT_MASK);
4589                 val |= MVPP22_XLG_INT_MASK_LINK;
4590                 writel(val, port->base + MVPP22_XLG_INT_MASK);
4591         }
4592
4593         mvpp22_gop_unmask_irq(port);
4594 }
4595
4596 static int mvpp22_comphy_init(struct mvpp2_port *port)
4597 {
4598         enum phy_mode mode;
4599         int ret;
4600
4601         if (!port->comphy)
4602                 return 0;
4603
4604         switch (port->phy_interface) {
4605         case PHY_INTERFACE_MODE_SGMII:
4606                 mode = PHY_MODE_SGMII;
4607                 break;
4608         case PHY_INTERFACE_MODE_10GKR:
4609                 mode = PHY_MODE_10GKR;
4610                 break;
4611         default:
4612                 return -EINVAL;
4613         }
4614
4615         ret = phy_set_mode(port->comphy, mode);
4616         if (ret)
4617                 return ret;
4618
4619         return phy_power_on(port->comphy);
4620 }
4621
4622 static void mvpp2_port_mii_gmac_configure_mode(struct mvpp2_port *port)
4623 {
4624         u32 val;
4625
4626         if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4627                 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4628                 val |= MVPP22_CTRL4_SYNC_BYPASS_DIS | MVPP22_CTRL4_DP_CLK_SEL |
4629                        MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4630                 val &= ~MVPP22_CTRL4_EXT_PIN_GMII_SEL;
4631                 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4632
4633                 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4634                 val |= MVPP2_GMAC_DISABLE_PADDING;
4635                 val &= ~MVPP2_GMAC_FLOW_CTRL_MASK;
4636                 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4637         } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
4638                 val = readl(port->base + MVPP22_GMAC_CTRL_4_REG);
4639                 val |= MVPP22_CTRL4_EXT_PIN_GMII_SEL |
4640                        MVPP22_CTRL4_SYNC_BYPASS_DIS |
4641                        MVPP22_CTRL4_QSGMII_BYPASS_ACTIVE;
4642                 val &= ~MVPP22_CTRL4_DP_CLK_SEL;
4643                 writel(val, port->base + MVPP22_GMAC_CTRL_4_REG);
4644
4645                 val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4646                 val &= ~MVPP2_GMAC_DISABLE_PADDING;
4647                 writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4648         }
4649
4650         /* The port is connected to a copper PHY */
4651         val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4652         val &= ~MVPP2_GMAC_PORT_TYPE_MASK;
4653         writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4654
4655         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4656         val |= MVPP2_GMAC_IN_BAND_AUTONEG_BYPASS |
4657                MVPP2_GMAC_AN_SPEED_EN | MVPP2_GMAC_FLOW_CTRL_AUTONEG |
4658                MVPP2_GMAC_AN_DUPLEX_EN;
4659         if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4660                 val |= MVPP2_GMAC_IN_BAND_AUTONEG;
4661         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4662 }
4663
4664 static void mvpp2_port_mii_gmac_configure(struct mvpp2_port *port)
4665 {
4666         u32 val;
4667
4668         /* Force link down */
4669         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4670         val &= ~MVPP2_GMAC_FORCE_LINK_PASS;
4671         val |= MVPP2_GMAC_FORCE_LINK_DOWN;
4672         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4673
4674         /* Set the GMAC in a reset state */
4675         val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4676         val |= MVPP2_GMAC_PORT_RESET_MASK;
4677         writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4678
4679         /* Configure the PCS and in-band AN */
4680         val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4681         if (port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
4682                 val |= MVPP2_GMAC_INBAND_AN_MASK | MVPP2_GMAC_PCS_ENABLE_MASK;
4683         } else if (phy_interface_mode_is_rgmii(port->phy_interface)) {
4684                 val &= ~MVPP2_GMAC_PCS_ENABLE_MASK;
4685         }
4686         writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4687
4688         mvpp2_port_mii_gmac_configure_mode(port);
4689
4690         /* Unset the GMAC reset state */
4691         val = readl(port->base + MVPP2_GMAC_CTRL_2_REG);
4692         val &= ~MVPP2_GMAC_PORT_RESET_MASK;
4693         writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4694
4695         /* Stop forcing link down */
4696         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4697         val &= ~MVPP2_GMAC_FORCE_LINK_DOWN;
4698         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4699 }
4700
4701 static void mvpp2_port_mii_xlg_configure(struct mvpp2_port *port)
4702 {
4703         u32 val;
4704
4705         if (port->gop_id != 0)
4706                 return;
4707
4708         val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4709         val |= MVPP22_XLG_CTRL0_RX_FLOW_CTRL_EN;
4710         writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4711
4712         val = readl(port->base + MVPP22_XLG_CTRL4_REG);
4713         val &= ~MVPP22_XLG_CTRL4_MACMODSELECT_GMAC;
4714         val |= MVPP22_XLG_CTRL4_FWD_FC | MVPP22_XLG_CTRL4_FWD_PFC;
4715         writel(val, port->base + MVPP22_XLG_CTRL4_REG);
4716 }
4717
4718 static void mvpp22_port_mii_set(struct mvpp2_port *port)
4719 {
4720         u32 val;
4721
4722         /* Only GOP port 0 has an XLG MAC */
4723         if (port->gop_id == 0) {
4724                 val = readl(port->base + MVPP22_XLG_CTRL3_REG);
4725                 val &= ~MVPP22_XLG_CTRL3_MACMODESELECT_MASK;
4726
4727                 if (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4728                     port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4729                         val |= MVPP22_XLG_CTRL3_MACMODESELECT_10G;
4730                 else
4731                         val |= MVPP22_XLG_CTRL3_MACMODESELECT_GMAC;
4732
4733                 writel(val, port->base + MVPP22_XLG_CTRL3_REG);
4734         }
4735 }
4736
4737 static void mvpp2_port_mii_set(struct mvpp2_port *port)
4738 {
4739         if (port->priv->hw_version == MVPP22)
4740                 mvpp22_port_mii_set(port);
4741
4742         if (phy_interface_mode_is_rgmii(port->phy_interface) ||
4743             port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4744                 mvpp2_port_mii_gmac_configure(port);
4745         else if (port->phy_interface == PHY_INTERFACE_MODE_10GKR)
4746                 mvpp2_port_mii_xlg_configure(port);
4747 }
4748
4749 static void mvpp2_port_fc_adv_enable(struct mvpp2_port *port)
4750 {
4751         u32 val;
4752
4753         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4754         val |= MVPP2_GMAC_FC_ADV_EN;
4755         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
4756 }
4757
4758 static void mvpp2_port_enable(struct mvpp2_port *port)
4759 {
4760         u32 val;
4761
4762         /* Only GOP port 0 has an XLG MAC */
4763         if (port->gop_id == 0 &&
4764             (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4765              port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4766                 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4767                 val |= MVPP22_XLG_CTRL0_PORT_EN |
4768                        MVPP22_XLG_CTRL0_MAC_RESET_DIS;
4769                 val &= ~MVPP22_XLG_CTRL0_MIB_CNT_DIS;
4770                 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4771         } else {
4772                 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4773                 val |= MVPP2_GMAC_PORT_EN_MASK;
4774                 val |= MVPP2_GMAC_MIB_CNTR_EN_MASK;
4775                 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4776         }
4777 }
4778
4779 static void mvpp2_port_disable(struct mvpp2_port *port)
4780 {
4781         u32 val;
4782
4783         /* Only GOP port 0 has an XLG MAC */
4784         if (port->gop_id == 0 &&
4785             (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
4786              port->phy_interface == PHY_INTERFACE_MODE_10GKR)) {
4787                 val = readl(port->base + MVPP22_XLG_CTRL0_REG);
4788                 val &= ~(MVPP22_XLG_CTRL0_PORT_EN |
4789                          MVPP22_XLG_CTRL0_MAC_RESET_DIS);
4790                 writel(val, port->base + MVPP22_XLG_CTRL0_REG);
4791         } else {
4792                 val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4793                 val &= ~(MVPP2_GMAC_PORT_EN_MASK);
4794                 writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4795         }
4796 }
4797
4798 /* Set IEEE 802.3x Flow Control Xon Packet Transmission Mode */
4799 static void mvpp2_port_periodic_xon_disable(struct mvpp2_port *port)
4800 {
4801         u32 val;
4802
4803         val = readl(port->base + MVPP2_GMAC_CTRL_1_REG) &
4804                     ~MVPP2_GMAC_PERIODIC_XON_EN_MASK;
4805         writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4806 }
4807
4808 /* Configure loopback port */
4809 static void mvpp2_port_loopback_set(struct mvpp2_port *port)
4810 {
4811         u32 val;
4812
4813         val = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
4814
4815         if (port->speed == 1000)
4816                 val |= MVPP2_GMAC_GMII_LB_EN_MASK;
4817         else
4818                 val &= ~MVPP2_GMAC_GMII_LB_EN_MASK;
4819
4820         if (port->phy_interface == PHY_INTERFACE_MODE_SGMII)
4821                 val |= MVPP2_GMAC_PCS_LB_EN_MASK;
4822         else
4823                 val &= ~MVPP2_GMAC_PCS_LB_EN_MASK;
4824
4825         writel(val, port->base + MVPP2_GMAC_CTRL_1_REG);
4826 }
4827
4828 struct mvpp2_ethtool_counter {
4829         unsigned int offset;
4830         const char string[ETH_GSTRING_LEN];
4831         bool reg_is_64b;
4832 };
4833
4834 static u64 mvpp2_read_count(struct mvpp2_port *port,
4835                             const struct mvpp2_ethtool_counter *counter)
4836 {
4837         u64 val;
4838
4839         val = readl(port->stats_base + counter->offset);
4840         if (counter->reg_is_64b)
4841                 val += (u64)readl(port->stats_base + counter->offset + 4) << 32;
4842
4843         return val;
4844 }
4845
4846 /* Due to the fact that software statistics and hardware statistics are, by
4847  * design, incremented at different moments in the chain of packet processing,
4848  * it is very likely that incoming packets could have been dropped after being
4849  * counted by hardware but before reaching software statistics (most probably
4850  * multicast packets), and in the oppposite way, during transmission, FCS bytes
4851  * are added in between as well as TSO skb will be split and header bytes added.
4852  * Hence, statistics gathered from userspace with ifconfig (software) and
4853  * ethtool (hardware) cannot be compared.
4854  */
4855 static const struct mvpp2_ethtool_counter mvpp2_ethtool_regs[] = {
4856         { MVPP2_MIB_GOOD_OCTETS_RCVD, "good_octets_received", true },
4857         { MVPP2_MIB_BAD_OCTETS_RCVD, "bad_octets_received" },
4858         { MVPP2_MIB_CRC_ERRORS_SENT, "crc_errors_sent" },
4859         { MVPP2_MIB_UNICAST_FRAMES_RCVD, "unicast_frames_received" },
4860         { MVPP2_MIB_BROADCAST_FRAMES_RCVD, "broadcast_frames_received" },
4861         { MVPP2_MIB_MULTICAST_FRAMES_RCVD, "multicast_frames_received" },
4862         { MVPP2_MIB_FRAMES_64_OCTETS, "frames_64_octets" },
4863         { MVPP2_MIB_FRAMES_65_TO_127_OCTETS, "frames_65_to_127_octet" },
4864         { MVPP2_MIB_FRAMES_128_TO_255_OCTETS, "frames_128_to_255_octet" },
4865         { MVPP2_MIB_FRAMES_256_TO_511_OCTETS, "frames_256_to_511_octet" },
4866         { MVPP2_MIB_FRAMES_512_TO_1023_OCTETS, "frames_512_to_1023_octet" },
4867         { MVPP2_MIB_FRAMES_1024_TO_MAX_OCTETS, "frames_1024_to_max_octet" },
4868         { MVPP2_MIB_GOOD_OCTETS_SENT, "good_octets_sent", true },
4869         { MVPP2_MIB_UNICAST_FRAMES_SENT, "unicast_frames_sent" },
4870         { MVPP2_MIB_MULTICAST_FRAMES_SENT, "multicast_frames_sent" },
4871         { MVPP2_MIB_BROADCAST_FRAMES_SENT, "broadcast_frames_sent" },
4872         { MVPP2_MIB_FC_SENT, "fc_sent" },
4873         { MVPP2_MIB_FC_RCVD, "fc_received" },
4874         { MVPP2_MIB_RX_FIFO_OVERRUN, "rx_fifo_overrun" },
4875         { MVPP2_MIB_UNDERSIZE_RCVD, "undersize_received" },
4876         { MVPP2_MIB_FRAGMENTS_RCVD, "fragments_received" },
4877         { MVPP2_MIB_OVERSIZE_RCVD, "oversize_received" },
4878         { MVPP2_MIB_JABBER_RCVD, "jabber_received" },
4879         { MVPP2_MIB_MAC_RCV_ERROR, "mac_receive_error" },
4880         { MVPP2_MIB_BAD_CRC_EVENT, "bad_crc_event" },
4881         { MVPP2_MIB_COLLISION, "collision" },
4882         { MVPP2_MIB_LATE_COLLISION, "late_collision" },
4883 };
4884
4885 static void mvpp2_ethtool_get_strings(struct net_device *netdev, u32 sset,
4886                                       u8 *data)
4887 {
4888         if (sset == ETH_SS_STATS) {
4889                 int i;
4890
4891                 for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4892                         memcpy(data + i * ETH_GSTRING_LEN,
4893                                &mvpp2_ethtool_regs[i].string, ETH_GSTRING_LEN);
4894         }
4895 }
4896
4897 static void mvpp2_gather_hw_statistics(struct work_struct *work)
4898 {
4899         struct delayed_work *del_work = to_delayed_work(work);
4900         struct mvpp2_port *port = container_of(del_work, struct mvpp2_port,
4901                                                stats_work);
4902         u64 *pstats;
4903         int i;
4904
4905         mutex_lock(&port->gather_stats_lock);
4906
4907         pstats = port->ethtool_stats;
4908         for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4909                 *pstats++ += mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
4910
4911         /* No need to read again the counters right after this function if it
4912          * was called asynchronously by the user (ie. use of ethtool).
4913          */
4914         cancel_delayed_work(&port->stats_work);
4915         queue_delayed_work(port->priv->stats_queue, &port->stats_work,
4916                            MVPP2_MIB_COUNTERS_STATS_DELAY);
4917
4918         mutex_unlock(&port->gather_stats_lock);
4919 }
4920
4921 static void mvpp2_ethtool_get_stats(struct net_device *dev,
4922                                     struct ethtool_stats *stats, u64 *data)
4923 {
4924         struct mvpp2_port *port = netdev_priv(dev);
4925
4926         /* Update statistics for the given port, then take the lock to avoid
4927          * concurrent accesses on the ethtool_stats structure during its copy.
4928          */
4929         mvpp2_gather_hw_statistics(&port->stats_work.work);
4930
4931         mutex_lock(&port->gather_stats_lock);
4932         memcpy(data, port->ethtool_stats,
4933                sizeof(u64) * ARRAY_SIZE(mvpp2_ethtool_regs));
4934         mutex_unlock(&port->gather_stats_lock);
4935 }
4936
4937 static int mvpp2_ethtool_get_sset_count(struct net_device *dev, int sset)
4938 {
4939         if (sset == ETH_SS_STATS)
4940                 return ARRAY_SIZE(mvpp2_ethtool_regs);
4941
4942         return -EOPNOTSUPP;
4943 }
4944
4945 static void mvpp2_port_reset(struct mvpp2_port *port)
4946 {
4947         u32 val;
4948         unsigned int i;
4949
4950         /* Read the GOP statistics to reset the hardware counters */
4951         for (i = 0; i < ARRAY_SIZE(mvpp2_ethtool_regs); i++)
4952                 mvpp2_read_count(port, &mvpp2_ethtool_regs[i]);
4953
4954         val = readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4955                     ~MVPP2_GMAC_PORT_RESET_MASK;
4956         writel(val, port->base + MVPP2_GMAC_CTRL_2_REG);
4957
4958         while (readl(port->base + MVPP2_GMAC_CTRL_2_REG) &
4959                MVPP2_GMAC_PORT_RESET_MASK)
4960                 continue;
4961 }
4962
4963 /* Change maximum receive size of the port */
4964 static inline void mvpp2_gmac_max_rx_size_set(struct mvpp2_port *port)
4965 {
4966         u32 val;
4967
4968         val = readl(port->base + MVPP2_GMAC_CTRL_0_REG);
4969         val &= ~MVPP2_GMAC_MAX_RX_SIZE_MASK;
4970         val |= (((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4971                     MVPP2_GMAC_MAX_RX_SIZE_OFFS);
4972         writel(val, port->base + MVPP2_GMAC_CTRL_0_REG);
4973 }
4974
4975 /* Change maximum receive size of the port */
4976 static inline void mvpp2_xlg_max_rx_size_set(struct mvpp2_port *port)
4977 {
4978         u32 val;
4979
4980         val =  readl(port->base + MVPP22_XLG_CTRL1_REG);
4981         val &= ~MVPP22_XLG_CTRL1_FRAMESIZELIMIT_MASK;
4982         val |= ((port->pkt_size - MVPP2_MH_SIZE) / 2) <<
4983                MVPP22_XLG_CTRL1_FRAMESIZELIMIT_OFFS;
4984         writel(val, port->base + MVPP22_XLG_CTRL1_REG);
4985 }
4986
4987 /* Set defaults to the MVPP2 port */
4988 static void mvpp2_defaults_set(struct mvpp2_port *port)
4989 {
4990         int tx_port_num, val, queue, ptxq, lrxq;
4991
4992         if (port->priv->hw_version == MVPP21) {
4993                 /* Configure port to loopback if needed */
4994                 if (port->flags & MVPP2_F_LOOPBACK)
4995                         mvpp2_port_loopback_set(port);
4996
4997                 /* Update TX FIFO MIN Threshold */
4998                 val = readl(port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
4999                 val &= ~MVPP2_GMAC_TX_FIFO_MIN_TH_ALL_MASK;
5000                 /* Min. TX threshold must be less than minimal packet length */
5001                 val |= MVPP2_GMAC_TX_FIFO_MIN_TH_MASK(64 - 4 - 2);
5002                 writel(val, port->base + MVPP2_GMAC_PORT_FIFO_CFG_1_REG);
5003         }
5004
5005         /* Disable Legacy WRR, Disable EJP, Release from reset */
5006         tx_port_num = mvpp2_egress_port(port);
5007         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG,
5008                     tx_port_num);
5009         mvpp2_write(port->priv, MVPP2_TXP_SCHED_CMD_1_REG, 0);
5010
5011         /* Close bandwidth for all queues */
5012         for (queue = 0; queue < MVPP2_MAX_TXQ; queue++) {
5013                 ptxq = mvpp2_txq_phys(port->id, queue);
5014                 mvpp2_write(port->priv,
5015                             MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(ptxq), 0);
5016         }
5017
5018         /* Set refill period to 1 usec, refill tokens
5019          * and bucket size to maximum
5020          */
5021         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PERIOD_REG,
5022                     port->priv->tclk / USEC_PER_SEC);
5023         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_REFILL_REG);
5024         val &= ~MVPP2_TXP_REFILL_PERIOD_ALL_MASK;
5025         val |= MVPP2_TXP_REFILL_PERIOD_MASK(1);
5026         val |= MVPP2_TXP_REFILL_TOKENS_ALL_MASK;
5027         mvpp2_write(port->priv, MVPP2_TXP_SCHED_REFILL_REG, val);
5028         val = MVPP2_TXP_TOKEN_SIZE_MAX;
5029         mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5030
5031         /* Set MaximumLowLatencyPacketSize value to 256 */
5032         mvpp2_write(port->priv, MVPP2_RX_CTRL_REG(port->id),
5033                     MVPP2_RX_USE_PSEUDO_FOR_CSUM_MASK |
5034                     MVPP2_RX_LOW_LATENCY_PKT_SIZE(256));
5035
5036         /* Enable Rx cache snoop */
5037         for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
5038                 queue = port->rxqs[lrxq]->id;
5039                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5040                 val |= MVPP2_SNOOP_PKT_SIZE_MASK |
5041                            MVPP2_SNOOP_BUF_HDR_MASK;
5042                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5043         }
5044
5045         /* At default, mask all interrupts to all present cpus */
5046         mvpp2_interrupts_disable(port);
5047 }
5048
5049 /* Enable/disable receiving packets */
5050 static void mvpp2_ingress_enable(struct mvpp2_port *port)
5051 {
5052         u32 val;
5053         int lrxq, queue;
5054
5055         for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
5056                 queue = port->rxqs[lrxq]->id;
5057                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5058                 val &= ~MVPP2_RXQ_DISABLE_MASK;
5059                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5060         }
5061 }
5062
5063 static void mvpp2_ingress_disable(struct mvpp2_port *port)
5064 {
5065         u32 val;
5066         int lrxq, queue;
5067
5068         for (lrxq = 0; lrxq < port->nrxqs; lrxq++) {
5069                 queue = port->rxqs[lrxq]->id;
5070                 val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(queue));
5071                 val |= MVPP2_RXQ_DISABLE_MASK;
5072                 mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(queue), val);
5073         }
5074 }
5075
5076 /* Enable transmit via physical egress queue
5077  * - HW starts take descriptors from DRAM
5078  */
5079 static void mvpp2_egress_enable(struct mvpp2_port *port)
5080 {
5081         u32 qmap;
5082         int queue;
5083         int tx_port_num = mvpp2_egress_port(port);
5084
5085         /* Enable all initialized TXs. */
5086         qmap = 0;
5087         for (queue = 0; queue < port->ntxqs; queue++) {
5088                 struct mvpp2_tx_queue *txq = port->txqs[queue];
5089
5090                 if (txq->descs)
5091                         qmap |= (1 << queue);
5092         }
5093
5094         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5095         mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG, qmap);
5096 }
5097
5098 /* Disable transmit via physical egress queue
5099  * - HW doesn't take descriptors from DRAM
5100  */
5101 static void mvpp2_egress_disable(struct mvpp2_port *port)
5102 {
5103         u32 reg_data;
5104         int delay;
5105         int tx_port_num = mvpp2_egress_port(port);
5106
5107         /* Issue stop command for active channels only */
5108         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5109         reg_data = (mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG)) &
5110                     MVPP2_TXP_SCHED_ENQ_MASK;
5111         if (reg_data != 0)
5112                 mvpp2_write(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG,
5113                             (reg_data << MVPP2_TXP_SCHED_DISQ_OFFSET));
5114
5115         /* Wait for all Tx activity to terminate. */
5116         delay = 0;
5117         do {
5118                 if (delay >= MVPP2_TX_DISABLE_TIMEOUT_MSEC) {
5119                         netdev_warn(port->dev,
5120                                     "Tx stop timed out, status=0x%08x\n",
5121                                     reg_data);
5122                         break;
5123                 }
5124                 mdelay(1);
5125                 delay++;
5126
5127                 /* Check port TX Command register that all
5128                  * Tx queues are stopped
5129                  */
5130                 reg_data = mvpp2_read(port->priv, MVPP2_TXP_SCHED_Q_CMD_REG);
5131         } while (reg_data & MVPP2_TXP_SCHED_ENQ_MASK);
5132 }
5133
5134 /* Rx descriptors helper methods */
5135
5136 /* Get number of Rx descriptors occupied by received packets */
5137 static inline int
5138 mvpp2_rxq_received(struct mvpp2_port *port, int rxq_id)
5139 {
5140         u32 val = mvpp2_read(port->priv, MVPP2_RXQ_STATUS_REG(rxq_id));
5141
5142         return val & MVPP2_RXQ_OCCUPIED_MASK;
5143 }
5144
5145 /* Update Rx queue status with the number of occupied and available
5146  * Rx descriptor slots.
5147  */
5148 static inline void
5149 mvpp2_rxq_status_update(struct mvpp2_port *port, int rxq_id,
5150                         int used_count, int free_count)
5151 {
5152         /* Decrement the number of used descriptors and increment count
5153          * increment the number of free descriptors.
5154          */
5155         u32 val = used_count | (free_count << MVPP2_RXQ_NUM_NEW_OFFSET);
5156
5157         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_UPDATE_REG(rxq_id), val);
5158 }
5159
5160 /* Get pointer to next RX descriptor to be processed by SW */
5161 static inline struct mvpp2_rx_desc *
5162 mvpp2_rxq_next_desc_get(struct mvpp2_rx_queue *rxq)
5163 {
5164         int rx_desc = rxq->next_desc_to_proc;
5165
5166         rxq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(rxq, rx_desc);
5167         prefetch(rxq->descs + rxq->next_desc_to_proc);
5168         return rxq->descs + rx_desc;
5169 }
5170
5171 /* Set rx queue offset */
5172 static void mvpp2_rxq_offset_set(struct mvpp2_port *port,
5173                                  int prxq, int offset)
5174 {
5175         u32 val;
5176
5177         /* Convert offset from bytes to units of 32 bytes */
5178         offset = offset >> 5;
5179
5180         val = mvpp2_read(port->priv, MVPP2_RXQ_CONFIG_REG(prxq));
5181         val &= ~MVPP2_RXQ_PACKET_OFFSET_MASK;
5182
5183         /* Offset is in */
5184         val |= ((offset << MVPP2_RXQ_PACKET_OFFSET_OFFS) &
5185                     MVPP2_RXQ_PACKET_OFFSET_MASK);
5186
5187         mvpp2_write(port->priv, MVPP2_RXQ_CONFIG_REG(prxq), val);
5188 }
5189
5190 /* Tx descriptors helper methods */
5191
5192 /* Get pointer to next Tx descriptor to be processed (send) by HW */
5193 static struct mvpp2_tx_desc *
5194 mvpp2_txq_next_desc_get(struct mvpp2_tx_queue *txq)
5195 {
5196         int tx_desc = txq->next_desc_to_proc;
5197
5198         txq->next_desc_to_proc = MVPP2_QUEUE_NEXT_DESC(txq, tx_desc);
5199         return txq->descs + tx_desc;
5200 }
5201
5202 /* Update HW with number of aggregated Tx descriptors to be sent
5203  *
5204  * Called only from mvpp2_tx(), so migration is disabled, using
5205  * smp_processor_id() is OK.
5206  */
5207 static void mvpp2_aggr_txq_pend_desc_add(struct mvpp2_port *port, int pending)
5208 {
5209         /* aggregated access - relevant TXQ number is written in TX desc */
5210         mvpp2_percpu_write(port->priv, smp_processor_id(),
5211                            MVPP2_AGGR_TXQ_UPDATE_REG, pending);
5212 }
5213
5214
5215 /* Check if there are enough free descriptors in aggregated txq.
5216  * If not, update the number of occupied descriptors and repeat the check.
5217  *
5218  * Called only from mvpp2_tx(), so migration is disabled, using
5219  * smp_processor_id() is OK.
5220  */
5221 static int mvpp2_aggr_desc_num_check(struct mvpp2 *priv,
5222                                      struct mvpp2_tx_queue *aggr_txq, int num)
5223 {
5224         if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE) {
5225                 /* Update number of occupied aggregated Tx descriptors */
5226                 int cpu = smp_processor_id();
5227                 u32 val = mvpp2_read(priv, MVPP2_AGGR_TXQ_STATUS_REG(cpu));
5228
5229                 aggr_txq->count = val & MVPP2_AGGR_TXQ_PENDING_MASK;
5230         }
5231
5232         if ((aggr_txq->count + num) > MVPP2_AGGR_TXQ_SIZE)
5233                 return -ENOMEM;
5234
5235         return 0;
5236 }
5237
5238 /* Reserved Tx descriptors allocation request
5239  *
5240  * Called only from mvpp2_txq_reserved_desc_num_proc(), itself called
5241  * only by mvpp2_tx(), so migration is disabled, using
5242  * smp_processor_id() is OK.
5243  */
5244 static int mvpp2_txq_alloc_reserved_desc(struct mvpp2 *priv,
5245                                          struct mvpp2_tx_queue *txq, int num)
5246 {
5247         u32 val;
5248         int cpu = smp_processor_id();
5249
5250         val = (txq->id << MVPP2_TXQ_RSVD_REQ_Q_OFFSET) | num;
5251         mvpp2_percpu_write(priv, cpu, MVPP2_TXQ_RSVD_REQ_REG, val);
5252
5253         val = mvpp2_percpu_read(priv, cpu, MVPP2_TXQ_RSVD_RSLT_REG);
5254
5255         return val & MVPP2_TXQ_RSVD_RSLT_MASK;
5256 }
5257
5258 /* Check if there are enough reserved descriptors for transmission.
5259  * If not, request chunk of reserved descriptors and check again.
5260  */
5261 static int mvpp2_txq_reserved_desc_num_proc(struct mvpp2 *priv,
5262                                             struct mvpp2_tx_queue *txq,
5263                                             struct mvpp2_txq_pcpu *txq_pcpu,
5264                                             int num)
5265 {
5266         int req, cpu, desc_count;
5267
5268         if (txq_pcpu->reserved_num >= num)
5269                 return 0;
5270
5271         /* Not enough descriptors reserved! Update the reserved descriptor
5272          * count and check again.
5273          */
5274
5275         desc_count = 0;
5276         /* Compute total of used descriptors */
5277         for_each_present_cpu(cpu) {
5278                 struct mvpp2_txq_pcpu *txq_pcpu_aux;
5279
5280                 txq_pcpu_aux = per_cpu_ptr(txq->pcpu, cpu);
5281                 desc_count += txq_pcpu_aux->count;
5282                 desc_count += txq_pcpu_aux->reserved_num;
5283         }
5284
5285         req = max(MVPP2_CPU_DESC_CHUNK, num - txq_pcpu->reserved_num);
5286         desc_count += req;
5287
5288         if (desc_count >
5289            (txq->size - (num_present_cpus() * MVPP2_CPU_DESC_CHUNK)))
5290                 return -ENOMEM;
5291
5292         txq_pcpu->reserved_num += mvpp2_txq_alloc_reserved_desc(priv, txq, req);
5293
5294         /* OK, the descriptor cound has been updated: check again. */
5295         if (txq_pcpu->reserved_num < num)
5296                 return -ENOMEM;
5297         return 0;
5298 }
5299
5300 /* Release the last allocated Tx descriptor. Useful to handle DMA
5301  * mapping failures in the Tx path.
5302  */
5303 static void mvpp2_txq_desc_put(struct mvpp2_tx_queue *txq)
5304 {
5305         if (txq->next_desc_to_proc == 0)
5306                 txq->next_desc_to_proc = txq->last_desc - 1;
5307         else
5308                 txq->next_desc_to_proc--;
5309 }
5310
5311 /* Set Tx descriptors fields relevant for CSUM calculation */
5312 static u32 mvpp2_txq_desc_csum(int l3_offs, int l3_proto,
5313                                int ip_hdr_len, int l4_proto)
5314 {
5315         u32 command;
5316
5317         /* fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
5318          * G_L4_chk, L4_type required only for checksum calculation
5319          */
5320         command = (l3_offs << MVPP2_TXD_L3_OFF_SHIFT);
5321         command |= (ip_hdr_len << MVPP2_TXD_IP_HLEN_SHIFT);
5322         command |= MVPP2_TXD_IP_CSUM_DISABLE;
5323
5324         if (l3_proto == swab16(ETH_P_IP)) {
5325                 command &= ~MVPP2_TXD_IP_CSUM_DISABLE;  /* enable IPv4 csum */
5326                 command &= ~MVPP2_TXD_L3_IP6;           /* enable IPv4 */
5327         } else {
5328                 command |= MVPP2_TXD_L3_IP6;            /* enable IPv6 */
5329         }
5330
5331         if (l4_proto == IPPROTO_TCP) {
5332                 command &= ~MVPP2_TXD_L4_UDP;           /* enable TCP */
5333                 command &= ~MVPP2_TXD_L4_CSUM_FRAG;     /* generate L4 csum */
5334         } else if (l4_proto == IPPROTO_UDP) {
5335                 command |= MVPP2_TXD_L4_UDP;            /* enable UDP */
5336                 command &= ~MVPP2_TXD_L4_CSUM_FRAG;     /* generate L4 csum */
5337         } else {
5338                 command |= MVPP2_TXD_L4_CSUM_NOT;
5339         }
5340
5341         return command;
5342 }
5343
5344 /* Get number of sent descriptors and decrement counter.
5345  * The number of sent descriptors is returned.
5346  * Per-CPU access
5347  *
5348  * Called only from mvpp2_txq_done(), called from mvpp2_tx()
5349  * (migration disabled) and from the TX completion tasklet (migration
5350  * disabled) so using smp_processor_id() is OK.
5351  */
5352 static inline int mvpp2_txq_sent_desc_proc(struct mvpp2_port *port,
5353                                            struct mvpp2_tx_queue *txq)
5354 {
5355         u32 val;
5356
5357         /* Reading status reg resets transmitted descriptor counter */
5358         val = mvpp2_percpu_read(port->priv, smp_processor_id(),
5359                                 MVPP2_TXQ_SENT_REG(txq->id));
5360
5361         return (val & MVPP2_TRANSMITTED_COUNT_MASK) >>
5362                 MVPP2_TRANSMITTED_COUNT_OFFSET;
5363 }
5364
5365 /* Called through on_each_cpu(), so runs on all CPUs, with migration
5366  * disabled, therefore using smp_processor_id() is OK.
5367  */
5368 static void mvpp2_txq_sent_counter_clear(void *arg)
5369 {
5370         struct mvpp2_port *port = arg;
5371         int queue;
5372
5373         for (queue = 0; queue < port->ntxqs; queue++) {
5374                 int id = port->txqs[queue]->id;
5375
5376                 mvpp2_percpu_read(port->priv, smp_processor_id(),
5377                                   MVPP2_TXQ_SENT_REG(id));
5378         }
5379 }
5380
5381 /* Set max sizes for Tx queues */
5382 static void mvpp2_txp_max_tx_size_set(struct mvpp2_port *port)
5383 {
5384         u32     val, size, mtu;
5385         int     txq, tx_port_num;
5386
5387         mtu = port->pkt_size * 8;
5388         if (mtu > MVPP2_TXP_MTU_MAX)
5389                 mtu = MVPP2_TXP_MTU_MAX;
5390
5391         /* WA for wrong Token bucket update: Set MTU value = 3*real MTU value */
5392         mtu = 3 * mtu;
5393
5394         /* Indirect access to registers */
5395         tx_port_num = mvpp2_egress_port(port);
5396         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5397
5398         /* Set MTU */
5399         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_MTU_REG);
5400         val &= ~MVPP2_TXP_MTU_MAX;
5401         val |= mtu;
5402         mvpp2_write(port->priv, MVPP2_TXP_SCHED_MTU_REG, val);
5403
5404         /* TXP token size and all TXQs token size must be larger that MTU */
5405         val = mvpp2_read(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG);
5406         size = val & MVPP2_TXP_TOKEN_SIZE_MAX;
5407         if (size < mtu) {
5408                 size = mtu;
5409                 val &= ~MVPP2_TXP_TOKEN_SIZE_MAX;
5410                 val |= size;
5411                 mvpp2_write(port->priv, MVPP2_TXP_SCHED_TOKEN_SIZE_REG, val);
5412         }
5413
5414         for (txq = 0; txq < port->ntxqs; txq++) {
5415                 val = mvpp2_read(port->priv,
5416                                  MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq));
5417                 size = val & MVPP2_TXQ_TOKEN_SIZE_MAX;
5418
5419                 if (size < mtu) {
5420                         size = mtu;
5421                         val &= ~MVPP2_TXQ_TOKEN_SIZE_MAX;
5422                         val |= size;
5423                         mvpp2_write(port->priv,
5424                                     MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq),
5425                                     val);
5426                 }
5427         }
5428 }
5429
5430 /* Set the number of packets that will be received before Rx interrupt
5431  * will be generated by HW.
5432  */
5433 static void mvpp2_rx_pkts_coal_set(struct mvpp2_port *port,
5434                                    struct mvpp2_rx_queue *rxq)
5435 {
5436         int cpu = get_cpu();
5437
5438         if (rxq->pkts_coal > MVPP2_OCCUPIED_THRESH_MASK)
5439                 rxq->pkts_coal = MVPP2_OCCUPIED_THRESH_MASK;
5440
5441         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5442         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_THRESH_REG,
5443                            rxq->pkts_coal);
5444
5445         put_cpu();
5446 }
5447
5448 /* For some reason in the LSP this is done on each CPU. Why ? */
5449 static void mvpp2_tx_pkts_coal_set(struct mvpp2_port *port,
5450                                    struct mvpp2_tx_queue *txq)
5451 {
5452         int cpu = get_cpu();
5453         u32 val;
5454
5455         if (txq->done_pkts_coal > MVPP2_TXQ_THRESH_MASK)
5456                 txq->done_pkts_coal = MVPP2_TXQ_THRESH_MASK;
5457
5458         val = (txq->done_pkts_coal << MVPP2_TXQ_THRESH_OFFSET);
5459         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5460         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_THRESH_REG, val);
5461
5462         put_cpu();
5463 }
5464
5465 static u32 mvpp2_usec_to_cycles(u32 usec, unsigned long clk_hz)
5466 {
5467         u64 tmp = (u64)clk_hz * usec;
5468
5469         do_div(tmp, USEC_PER_SEC);
5470
5471         return tmp > U32_MAX ? U32_MAX : tmp;
5472 }
5473
5474 static u32 mvpp2_cycles_to_usec(u32 cycles, unsigned long clk_hz)
5475 {
5476         u64 tmp = (u64)cycles * USEC_PER_SEC;
5477
5478         do_div(tmp, clk_hz);
5479
5480         return tmp > U32_MAX ? U32_MAX : tmp;
5481 }
5482
5483 /* Set the time delay in usec before Rx interrupt */
5484 static void mvpp2_rx_time_coal_set(struct mvpp2_port *port,
5485                                    struct mvpp2_rx_queue *rxq)
5486 {
5487         unsigned long freq = port->priv->tclk;
5488         u32 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5489
5490         if (val > MVPP2_MAX_ISR_RX_THRESHOLD) {
5491                 rxq->time_coal =
5492                         mvpp2_cycles_to_usec(MVPP2_MAX_ISR_RX_THRESHOLD, freq);
5493
5494                 /* re-evaluate to get actual register value */
5495                 val = mvpp2_usec_to_cycles(rxq->time_coal, freq);
5496         }
5497
5498         mvpp2_write(port->priv, MVPP2_ISR_RX_THRESHOLD_REG(rxq->id), val);
5499 }
5500
5501 static void mvpp2_tx_time_coal_set(struct mvpp2_port *port)
5502 {
5503         unsigned long freq = port->priv->tclk;
5504         u32 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5505
5506         if (val > MVPP2_MAX_ISR_TX_THRESHOLD) {
5507                 port->tx_time_coal =
5508                         mvpp2_cycles_to_usec(MVPP2_MAX_ISR_TX_THRESHOLD, freq);
5509
5510                 /* re-evaluate to get actual register value */
5511                 val = mvpp2_usec_to_cycles(port->tx_time_coal, freq);
5512         }
5513
5514         mvpp2_write(port->priv, MVPP2_ISR_TX_THRESHOLD_REG(port->id), val);
5515 }
5516
5517 /* Free Tx queue skbuffs */
5518 static void mvpp2_txq_bufs_free(struct mvpp2_port *port,
5519                                 struct mvpp2_tx_queue *txq,
5520                                 struct mvpp2_txq_pcpu *txq_pcpu, int num)
5521 {
5522         int i;
5523
5524         for (i = 0; i < num; i++) {
5525                 struct mvpp2_txq_pcpu_buf *tx_buf =
5526                         txq_pcpu->buffs + txq_pcpu->txq_get_index;
5527
5528                 if (!IS_TSO_HEADER(txq_pcpu, tx_buf->dma))
5529                         dma_unmap_single(port->dev->dev.parent, tx_buf->dma,
5530                                          tx_buf->size, DMA_TO_DEVICE);
5531                 if (tx_buf->skb)
5532                         dev_kfree_skb_any(tx_buf->skb);
5533
5534                 mvpp2_txq_inc_get(txq_pcpu);
5535         }
5536 }
5537
5538 static inline struct mvpp2_rx_queue *mvpp2_get_rx_queue(struct mvpp2_port *port,
5539                                                         u32 cause)
5540 {
5541         int queue = fls(cause) - 1;
5542
5543         return port->rxqs[queue];
5544 }
5545
5546 static inline struct mvpp2_tx_queue *mvpp2_get_tx_queue(struct mvpp2_port *port,
5547                                                         u32 cause)
5548 {
5549         int queue = fls(cause) - 1;
5550
5551         return port->txqs[queue];
5552 }
5553
5554 /* Handle end of transmission */
5555 static void mvpp2_txq_done(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
5556                            struct mvpp2_txq_pcpu *txq_pcpu)
5557 {
5558         struct netdev_queue *nq = netdev_get_tx_queue(port->dev, txq->log_id);
5559         int tx_done;
5560
5561         if (txq_pcpu->cpu != smp_processor_id())
5562                 netdev_err(port->dev, "wrong cpu on the end of Tx processing\n");
5563
5564         tx_done = mvpp2_txq_sent_desc_proc(port, txq);
5565         if (!tx_done)
5566                 return;
5567         mvpp2_txq_bufs_free(port, txq, txq_pcpu, tx_done);
5568
5569         txq_pcpu->count -= tx_done;
5570
5571         if (netif_tx_queue_stopped(nq))
5572                 if (txq_pcpu->count <= txq_pcpu->wake_threshold)
5573                         netif_tx_wake_queue(nq);
5574 }
5575
5576 static unsigned int mvpp2_tx_done(struct mvpp2_port *port, u32 cause,
5577                                   int cpu)
5578 {
5579         struct mvpp2_tx_queue *txq;
5580         struct mvpp2_txq_pcpu *txq_pcpu;
5581         unsigned int tx_todo = 0;
5582
5583         while (cause) {
5584                 txq = mvpp2_get_tx_queue(port, cause);
5585                 if (!txq)
5586                         break;
5587
5588                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5589
5590                 if (txq_pcpu->count) {
5591                         mvpp2_txq_done(port, txq, txq_pcpu);
5592                         tx_todo += txq_pcpu->count;
5593                 }
5594
5595                 cause &= ~(1 << txq->log_id);
5596         }
5597         return tx_todo;
5598 }
5599
5600 /* Rx/Tx queue initialization/cleanup methods */
5601
5602 /* Allocate and initialize descriptors for aggr TXQ */
5603 static int mvpp2_aggr_txq_init(struct platform_device *pdev,
5604                                struct mvpp2_tx_queue *aggr_txq, int cpu,
5605                                struct mvpp2 *priv)
5606 {
5607         u32 txq_dma;
5608
5609         /* Allocate memory for TX descriptors */
5610         aggr_txq->descs = dma_alloc_coherent(&pdev->dev,
5611                                 MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
5612                                 &aggr_txq->descs_dma, GFP_KERNEL);
5613         if (!aggr_txq->descs)
5614                 return -ENOMEM;
5615
5616         aggr_txq->last_desc = MVPP2_AGGR_TXQ_SIZE - 1;
5617
5618         /* Aggr TXQ no reset WA */
5619         aggr_txq->next_desc_to_proc = mvpp2_read(priv,
5620                                                  MVPP2_AGGR_TXQ_INDEX_REG(cpu));
5621
5622         /* Set Tx descriptors queue starting address indirect
5623          * access
5624          */
5625         if (priv->hw_version == MVPP21)
5626                 txq_dma = aggr_txq->descs_dma;
5627         else
5628                 txq_dma = aggr_txq->descs_dma >>
5629                         MVPP22_AGGR_TXQ_DESC_ADDR_OFFS;
5630
5631         mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_ADDR_REG(cpu), txq_dma);
5632         mvpp2_write(priv, MVPP2_AGGR_TXQ_DESC_SIZE_REG(cpu),
5633                     MVPP2_AGGR_TXQ_SIZE);
5634
5635         return 0;
5636 }
5637
5638 /* Create a specified Rx queue */
5639 static int mvpp2_rxq_init(struct mvpp2_port *port,
5640                           struct mvpp2_rx_queue *rxq)
5641
5642 {
5643         u32 rxq_dma;
5644         int cpu;
5645
5646         rxq->size = port->rx_ring_size;
5647
5648         /* Allocate memory for RX descriptors */
5649         rxq->descs = dma_alloc_coherent(port->dev->dev.parent,
5650                                         rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5651                                         &rxq->descs_dma, GFP_KERNEL);
5652         if (!rxq->descs)
5653                 return -ENOMEM;
5654
5655         rxq->last_desc = rxq->size - 1;
5656
5657         /* Zero occupied and non-occupied counters - direct access */
5658         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5659
5660         /* Set Rx descriptors queue starting address - indirect access */
5661         cpu = get_cpu();
5662         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5663         if (port->priv->hw_version == MVPP21)
5664                 rxq_dma = rxq->descs_dma;
5665         else
5666                 rxq_dma = rxq->descs_dma >> MVPP22_DESC_ADDR_OFFS;
5667         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, rxq_dma);
5668         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, rxq->size);
5669         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_INDEX_REG, 0);
5670         put_cpu();
5671
5672         /* Set Offset */
5673         mvpp2_rxq_offset_set(port, rxq->id, NET_SKB_PAD);
5674
5675         /* Set coalescing pkts and time */
5676         mvpp2_rx_pkts_coal_set(port, rxq);
5677         mvpp2_rx_time_coal_set(port, rxq);
5678
5679         /* Add number of descriptors ready for receiving packets */
5680         mvpp2_rxq_status_update(port, rxq->id, 0, rxq->size);
5681
5682         return 0;
5683 }
5684
5685 /* Push packets received by the RXQ to BM pool */
5686 static void mvpp2_rxq_drop_pkts(struct mvpp2_port *port,
5687                                 struct mvpp2_rx_queue *rxq)
5688 {
5689         int rx_received, i;
5690
5691         rx_received = mvpp2_rxq_received(port, rxq->id);
5692         if (!rx_received)
5693                 return;
5694
5695         for (i = 0; i < rx_received; i++) {
5696                 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
5697                 u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
5698                 int pool;
5699
5700                 pool = (status & MVPP2_RXD_BM_POOL_ID_MASK) >>
5701                         MVPP2_RXD_BM_POOL_ID_OFFS;
5702
5703                 mvpp2_bm_pool_put(port, pool,
5704                                   mvpp2_rxdesc_dma_addr_get(port, rx_desc),
5705                                   mvpp2_rxdesc_cookie_get(port, rx_desc));
5706         }
5707         mvpp2_rxq_status_update(port, rxq->id, rx_received, rx_received);
5708 }
5709
5710 /* Cleanup Rx queue */
5711 static void mvpp2_rxq_deinit(struct mvpp2_port *port,
5712                              struct mvpp2_rx_queue *rxq)
5713 {
5714         int cpu;
5715
5716         mvpp2_rxq_drop_pkts(port, rxq);
5717
5718         if (rxq->descs)
5719                 dma_free_coherent(port->dev->dev.parent,
5720                                   rxq->size * MVPP2_DESC_ALIGNED_SIZE,
5721                                   rxq->descs,
5722                                   rxq->descs_dma);
5723
5724         rxq->descs             = NULL;
5725         rxq->last_desc         = 0;
5726         rxq->next_desc_to_proc = 0;
5727         rxq->descs_dma         = 0;
5728
5729         /* Clear Rx descriptors queue starting address and size;
5730          * free descriptor number
5731          */
5732         mvpp2_write(port->priv, MVPP2_RXQ_STATUS_REG(rxq->id), 0);
5733         cpu = get_cpu();
5734         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_NUM_REG, rxq->id);
5735         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_ADDR_REG, 0);
5736         mvpp2_percpu_write(port->priv, cpu, MVPP2_RXQ_DESC_SIZE_REG, 0);
5737         put_cpu();
5738 }
5739
5740 /* Create and initialize a Tx queue */
5741 static int mvpp2_txq_init(struct mvpp2_port *port,
5742                           struct mvpp2_tx_queue *txq)
5743 {
5744         u32 val;
5745         int cpu, desc, desc_per_txq, tx_port_num;
5746         struct mvpp2_txq_pcpu *txq_pcpu;
5747
5748         txq->size = port->tx_ring_size;
5749
5750         /* Allocate memory for Tx descriptors */
5751         txq->descs = dma_alloc_coherent(port->dev->dev.parent,
5752                                 txq->size * MVPP2_DESC_ALIGNED_SIZE,
5753                                 &txq->descs_dma, GFP_KERNEL);
5754         if (!txq->descs)
5755                 return -ENOMEM;
5756
5757         txq->last_desc = txq->size - 1;
5758
5759         /* Set Tx descriptors queue starting address - indirect access */
5760         cpu = get_cpu();
5761         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5762         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG,
5763                            txq->descs_dma);
5764         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG,
5765                            txq->size & MVPP2_TXQ_DESC_SIZE_MASK);
5766         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_INDEX_REG, 0);
5767         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_RSVD_CLR_REG,
5768                            txq->id << MVPP2_TXQ_RSVD_CLR_OFFSET);
5769         val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PENDING_REG);
5770         val &= ~MVPP2_TXQ_PENDING_MASK;
5771         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PENDING_REG, val);
5772
5773         /* Calculate base address in prefetch buffer. We reserve 16 descriptors
5774          * for each existing TXQ.
5775          * TCONTS for PON port must be continuous from 0 to MVPP2_MAX_TCONT
5776          * GBE ports assumed to be continious from 0 to MVPP2_MAX_PORTS
5777          */
5778         desc_per_txq = 16;
5779         desc = (port->id * MVPP2_MAX_TXQ * desc_per_txq) +
5780                (txq->log_id * desc_per_txq);
5781
5782         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG,
5783                            MVPP2_PREF_BUF_PTR(desc) | MVPP2_PREF_BUF_SIZE_16 |
5784                            MVPP2_PREF_BUF_THRESH(desc_per_txq / 2));
5785         put_cpu();
5786
5787         /* WRR / EJP configuration - indirect access */
5788         tx_port_num = mvpp2_egress_port(port);
5789         mvpp2_write(port->priv, MVPP2_TXP_SCHED_PORT_INDEX_REG, tx_port_num);
5790
5791         val = mvpp2_read(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id));
5792         val &= ~MVPP2_TXQ_REFILL_PERIOD_ALL_MASK;
5793         val |= MVPP2_TXQ_REFILL_PERIOD_MASK(1);
5794         val |= MVPP2_TXQ_REFILL_TOKENS_ALL_MASK;
5795         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_REFILL_REG(txq->log_id), val);
5796
5797         val = MVPP2_TXQ_TOKEN_SIZE_MAX;
5798         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_SIZE_REG(txq->log_id),
5799                     val);
5800
5801         for_each_present_cpu(cpu) {
5802                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5803                 txq_pcpu->size = txq->size;
5804                 txq_pcpu->buffs = kmalloc_array(txq_pcpu->size,
5805                                                 sizeof(*txq_pcpu->buffs),
5806                                                 GFP_KERNEL);
5807                 if (!txq_pcpu->buffs)
5808                         goto cleanup;
5809
5810                 txq_pcpu->count = 0;
5811                 txq_pcpu->reserved_num = 0;
5812                 txq_pcpu->txq_put_index = 0;
5813                 txq_pcpu->txq_get_index = 0;
5814
5815                 txq_pcpu->stop_threshold = txq->size - MVPP2_MAX_SKB_DESCS;
5816                 txq_pcpu->wake_threshold = txq_pcpu->stop_threshold / 2;
5817
5818                 txq_pcpu->tso_headers =
5819                         dma_alloc_coherent(port->dev->dev.parent,
5820                                            txq_pcpu->size * TSO_HEADER_SIZE,
5821                                            &txq_pcpu->tso_headers_dma,
5822                                            GFP_KERNEL);
5823                 if (!txq_pcpu->tso_headers)
5824                         goto cleanup;
5825         }
5826
5827         return 0;
5828 cleanup:
5829         for_each_present_cpu(cpu) {
5830                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5831                 kfree(txq_pcpu->buffs);
5832
5833                 dma_free_coherent(port->dev->dev.parent,
5834                                   txq_pcpu->size * TSO_HEADER_SIZE,
5835                                   txq_pcpu->tso_headers,
5836                                   txq_pcpu->tso_headers_dma);
5837         }
5838
5839         dma_free_coherent(port->dev->dev.parent,
5840                           txq->size * MVPP2_DESC_ALIGNED_SIZE,
5841                           txq->descs, txq->descs_dma);
5842
5843         return -ENOMEM;
5844 }
5845
5846 /* Free allocated TXQ resources */
5847 static void mvpp2_txq_deinit(struct mvpp2_port *port,
5848                              struct mvpp2_tx_queue *txq)
5849 {
5850         struct mvpp2_txq_pcpu *txq_pcpu;
5851         int cpu;
5852
5853         for_each_present_cpu(cpu) {
5854                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5855                 kfree(txq_pcpu->buffs);
5856
5857                 dma_free_coherent(port->dev->dev.parent,
5858                                   txq_pcpu->size * TSO_HEADER_SIZE,
5859                                   txq_pcpu->tso_headers,
5860                                   txq_pcpu->tso_headers_dma);
5861         }
5862
5863         if (txq->descs)
5864                 dma_free_coherent(port->dev->dev.parent,
5865                                   txq->size * MVPP2_DESC_ALIGNED_SIZE,
5866                                   txq->descs, txq->descs_dma);
5867
5868         txq->descs             = NULL;
5869         txq->last_desc         = 0;
5870         txq->next_desc_to_proc = 0;
5871         txq->descs_dma         = 0;
5872
5873         /* Set minimum bandwidth for disabled TXQs */
5874         mvpp2_write(port->priv, MVPP2_TXQ_SCHED_TOKEN_CNTR_REG(txq->id), 0);
5875
5876         /* Set Tx descriptors queue starting address and size */
5877         cpu = get_cpu();
5878         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5879         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_ADDR_REG, 0);
5880         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_DESC_SIZE_REG, 0);
5881         put_cpu();
5882 }
5883
5884 /* Cleanup Tx ports */
5885 static void mvpp2_txq_clean(struct mvpp2_port *port, struct mvpp2_tx_queue *txq)
5886 {
5887         struct mvpp2_txq_pcpu *txq_pcpu;
5888         int delay, pending, cpu;
5889         u32 val;
5890
5891         cpu = get_cpu();
5892         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_NUM_REG, txq->id);
5893         val = mvpp2_percpu_read(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG);
5894         val |= MVPP2_TXQ_DRAIN_EN_MASK;
5895         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
5896
5897         /* The napi queue has been stopped so wait for all packets
5898          * to be transmitted.
5899          */
5900         delay = 0;
5901         do {
5902                 if (delay >= MVPP2_TX_PENDING_TIMEOUT_MSEC) {
5903                         netdev_warn(port->dev,
5904                                     "port %d: cleaning queue %d timed out\n",
5905                                     port->id, txq->log_id);
5906                         break;
5907                 }
5908                 mdelay(1);
5909                 delay++;
5910
5911                 pending = mvpp2_percpu_read(port->priv, cpu,
5912                                             MVPP2_TXQ_PENDING_REG);
5913                 pending &= MVPP2_TXQ_PENDING_MASK;
5914         } while (pending);
5915
5916         val &= ~MVPP2_TXQ_DRAIN_EN_MASK;
5917         mvpp2_percpu_write(port->priv, cpu, MVPP2_TXQ_PREF_BUF_REG, val);
5918         put_cpu();
5919
5920         for_each_present_cpu(cpu) {
5921                 txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
5922
5923                 /* Release all packets */
5924                 mvpp2_txq_bufs_free(port, txq, txq_pcpu, txq_pcpu->count);
5925
5926                 /* Reset queue */
5927                 txq_pcpu->count = 0;
5928                 txq_pcpu->txq_put_index = 0;
5929                 txq_pcpu->txq_get_index = 0;
5930         }
5931 }
5932
5933 /* Cleanup all Tx queues */
5934 static void mvpp2_cleanup_txqs(struct mvpp2_port *port)
5935 {
5936         struct mvpp2_tx_queue *txq;
5937         int queue;
5938         u32 val;
5939
5940         val = mvpp2_read(port->priv, MVPP2_TX_PORT_FLUSH_REG);
5941
5942         /* Reset Tx ports and delete Tx queues */
5943         val |= MVPP2_TX_PORT_FLUSH_MASK(port->id);
5944         mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5945
5946         for (queue = 0; queue < port->ntxqs; queue++) {
5947                 txq = port->txqs[queue];
5948                 mvpp2_txq_clean(port, txq);
5949                 mvpp2_txq_deinit(port, txq);
5950         }
5951
5952         on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
5953
5954         val &= ~MVPP2_TX_PORT_FLUSH_MASK(port->id);
5955         mvpp2_write(port->priv, MVPP2_TX_PORT_FLUSH_REG, val);
5956 }
5957
5958 /* Cleanup all Rx queues */
5959 static void mvpp2_cleanup_rxqs(struct mvpp2_port *port)
5960 {
5961         int queue;
5962
5963         for (queue = 0; queue < port->nrxqs; queue++)
5964                 mvpp2_rxq_deinit(port, port->rxqs[queue]);
5965 }
5966
5967 /* Init all Rx queues for port */
5968 static int mvpp2_setup_rxqs(struct mvpp2_port *port)
5969 {
5970         int queue, err;
5971
5972         for (queue = 0; queue < port->nrxqs; queue++) {
5973                 err = mvpp2_rxq_init(port, port->rxqs[queue]);
5974                 if (err)
5975                         goto err_cleanup;
5976         }
5977         return 0;
5978
5979 err_cleanup:
5980         mvpp2_cleanup_rxqs(port);
5981         return err;
5982 }
5983
5984 /* Init all tx queues for port */
5985 static int mvpp2_setup_txqs(struct mvpp2_port *port)
5986 {
5987         struct mvpp2_tx_queue *txq;
5988         int queue, err;
5989
5990         for (queue = 0; queue < port->ntxqs; queue++) {
5991                 txq = port->txqs[queue];
5992                 err = mvpp2_txq_init(port, txq);
5993                 if (err)
5994                         goto err_cleanup;
5995         }
5996
5997         if (port->has_tx_irqs) {
5998                 mvpp2_tx_time_coal_set(port);
5999                 for (queue = 0; queue < port->ntxqs; queue++) {
6000                         txq = port->txqs[queue];
6001                         mvpp2_tx_pkts_coal_set(port, txq);
6002                 }
6003         }
6004
6005         on_each_cpu(mvpp2_txq_sent_counter_clear, port, 1);
6006         return 0;
6007
6008 err_cleanup:
6009         mvpp2_cleanup_txqs(port);
6010         return err;
6011 }
6012
6013 /* The callback for per-port interrupt */
6014 static irqreturn_t mvpp2_isr(int irq, void *dev_id)
6015 {
6016         struct mvpp2_queue_vector *qv = dev_id;
6017
6018         mvpp2_qvec_interrupt_disable(qv);
6019
6020         napi_schedule(&qv->napi);
6021
6022         return IRQ_HANDLED;
6023 }
6024
6025 /* Per-port interrupt for link status changes */
6026 static irqreturn_t mvpp2_link_status_isr(int irq, void *dev_id)
6027 {
6028         struct mvpp2_port *port = (struct mvpp2_port *)dev_id;
6029         struct net_device *dev = port->dev;
6030         bool event = false, link = false;
6031         u32 val;
6032
6033         mvpp22_gop_mask_irq(port);
6034
6035         if (port->gop_id == 0 &&
6036             port->phy_interface == PHY_INTERFACE_MODE_10GKR) {
6037                 val = readl(port->base + MVPP22_XLG_INT_STAT);
6038                 if (val & MVPP22_XLG_INT_STAT_LINK) {
6039                         event = true;
6040                         val = readl(port->base + MVPP22_XLG_STATUS);
6041                         if (val & MVPP22_XLG_STATUS_LINK_UP)
6042                                 link = true;
6043                 }
6044         } else if (phy_interface_mode_is_rgmii(port->phy_interface) ||
6045                    port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6046                 val = readl(port->base + MVPP22_GMAC_INT_STAT);
6047                 if (val & MVPP22_GMAC_INT_STAT_LINK) {
6048                         event = true;
6049                         val = readl(port->base + MVPP2_GMAC_STATUS0);
6050                         if (val & MVPP2_GMAC_STATUS0_LINK_UP)
6051                                 link = true;
6052                 }
6053         }
6054
6055         if (!netif_running(dev) || !event)
6056                 goto handled;
6057
6058         if (link) {
6059                 mvpp2_interrupts_enable(port);
6060
6061                 mvpp2_egress_enable(port);
6062                 mvpp2_ingress_enable(port);
6063                 netif_carrier_on(dev);
6064                 netif_tx_wake_all_queues(dev);
6065         } else {
6066                 netif_tx_stop_all_queues(dev);
6067                 netif_carrier_off(dev);
6068                 mvpp2_ingress_disable(port);
6069                 mvpp2_egress_disable(port);
6070
6071                 mvpp2_interrupts_disable(port);
6072         }
6073
6074 handled:
6075         mvpp22_gop_unmask_irq(port);
6076         return IRQ_HANDLED;
6077 }
6078
6079 static void mvpp2_gmac_set_autoneg(struct mvpp2_port *port,
6080                                    struct phy_device *phydev)
6081 {
6082         u32 val;
6083
6084         if (port->phy_interface != PHY_INTERFACE_MODE_RGMII &&
6085             port->phy_interface != PHY_INTERFACE_MODE_RGMII_ID &&
6086             port->phy_interface != PHY_INTERFACE_MODE_RGMII_RXID &&
6087             port->phy_interface != PHY_INTERFACE_MODE_RGMII_TXID &&
6088             port->phy_interface != PHY_INTERFACE_MODE_SGMII)
6089                 return;
6090
6091         val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6092         val &= ~(MVPP2_GMAC_CONFIG_MII_SPEED |
6093                  MVPP2_GMAC_CONFIG_GMII_SPEED |
6094                  MVPP2_GMAC_CONFIG_FULL_DUPLEX |
6095                  MVPP2_GMAC_AN_SPEED_EN |
6096                  MVPP2_GMAC_AN_DUPLEX_EN);
6097
6098         if (phydev->duplex)
6099                 val |= MVPP2_GMAC_CONFIG_FULL_DUPLEX;
6100
6101         if (phydev->speed == SPEED_1000)
6102                 val |= MVPP2_GMAC_CONFIG_GMII_SPEED;
6103         else if (phydev->speed == SPEED_100)
6104                 val |= MVPP2_GMAC_CONFIG_MII_SPEED;
6105
6106         writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6107 }
6108
6109 /* Adjust link */
6110 static void mvpp2_link_event(struct net_device *dev)
6111 {
6112         struct mvpp2_port *port = netdev_priv(dev);
6113         struct phy_device *phydev = dev->phydev;
6114         bool link_reconfigured = false;
6115         u32 val;
6116
6117         if (phydev->link) {
6118                 if (port->phy_interface != phydev->interface && port->comphy) {
6119                         /* disable current port for reconfiguration */
6120                         mvpp2_interrupts_disable(port);
6121                         netif_carrier_off(port->dev);
6122                         mvpp2_port_disable(port);
6123                         phy_power_off(port->comphy);
6124
6125                         /* comphy reconfiguration */
6126                         port->phy_interface = phydev->interface;
6127                         mvpp22_comphy_init(port);
6128
6129                         /* gop/mac reconfiguration */
6130                         mvpp22_gop_init(port);
6131                         mvpp2_port_mii_set(port);
6132
6133                         link_reconfigured = true;
6134                 }
6135
6136                 if ((port->speed != phydev->speed) ||
6137                     (port->duplex != phydev->duplex)) {
6138                         mvpp2_gmac_set_autoneg(port, phydev);
6139
6140                         port->duplex = phydev->duplex;
6141                         port->speed  = phydev->speed;
6142                 }
6143         }
6144
6145         if (phydev->link != port->link || link_reconfigured) {
6146                 port->link = phydev->link;
6147
6148                 if (phydev->link) {
6149                         if (port->phy_interface == PHY_INTERFACE_MODE_RGMII ||
6150                             port->phy_interface == PHY_INTERFACE_MODE_RGMII_ID ||
6151                             port->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID ||
6152                             port->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID ||
6153                             port->phy_interface == PHY_INTERFACE_MODE_SGMII) {
6154                                 val = readl(port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6155                                 val |= (MVPP2_GMAC_FORCE_LINK_PASS |
6156                                         MVPP2_GMAC_FORCE_LINK_DOWN);
6157                                 writel(val, port->base + MVPP2_GMAC_AUTONEG_CONFIG);
6158                         }
6159
6160                         mvpp2_interrupts_enable(port);
6161                         mvpp2_port_enable(port);
6162
6163                         mvpp2_egress_enable(port);
6164                         mvpp2_ingress_enable(port);
6165                         netif_carrier_on(dev);
6166                         netif_tx_wake_all_queues(dev);
6167                 } else {
6168                         port->duplex = -1;
6169                         port->speed = 0;
6170
6171                         netif_tx_stop_all_queues(dev);
6172                         netif_carrier_off(dev);
6173                         mvpp2_ingress_disable(port);
6174                         mvpp2_egress_disable(port);
6175
6176                         mvpp2_port_disable(port);
6177                         mvpp2_interrupts_disable(port);
6178                 }
6179
6180                 phy_print_status(phydev);
6181         }
6182 }
6183
6184 static void mvpp2_timer_set(struct mvpp2_port_pcpu *port_pcpu)
6185 {
6186         ktime_t interval;
6187
6188         if (!port_pcpu->timer_scheduled) {
6189                 port_pcpu->timer_scheduled = true;
6190                 interval = MVPP2_TXDONE_HRTIMER_PERIOD_NS;
6191                 hrtimer_start(&port_pcpu->tx_done_timer, interval,
6192                               HRTIMER_MODE_REL_PINNED);
6193         }
6194 }
6195
6196 static void mvpp2_tx_proc_cb(unsigned long data)
6197 {
6198         struct net_device *dev = (struct net_device *)data;
6199         struct mvpp2_port *port = netdev_priv(dev);
6200         struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6201         unsigned int tx_todo, cause;
6202
6203         if (!netif_running(dev))
6204                 return;
6205         port_pcpu->timer_scheduled = false;
6206
6207         /* Process all the Tx queues */
6208         cause = (1 << port->ntxqs) - 1;
6209         tx_todo = mvpp2_tx_done(port, cause, smp_processor_id());
6210
6211         /* Set the timer in case not all the packets were processed */
6212         if (tx_todo)
6213                 mvpp2_timer_set(port_pcpu);
6214 }
6215
6216 static enum hrtimer_restart mvpp2_hr_timer_cb(struct hrtimer *timer)
6217 {
6218         struct mvpp2_port_pcpu *port_pcpu = container_of(timer,
6219                                                          struct mvpp2_port_pcpu,
6220                                                          tx_done_timer);
6221
6222         tasklet_schedule(&port_pcpu->tx_done_tasklet);
6223
6224         return HRTIMER_NORESTART;
6225 }
6226
6227 /* Main RX/TX processing routines */
6228
6229 /* Display more error info */
6230 static void mvpp2_rx_error(struct mvpp2_port *port,
6231                            struct mvpp2_rx_desc *rx_desc)
6232 {
6233         u32 status = mvpp2_rxdesc_status_get(port, rx_desc);
6234         size_t sz = mvpp2_rxdesc_size_get(port, rx_desc);
6235
6236         switch (status & MVPP2_RXD_ERR_CODE_MASK) {
6237         case MVPP2_RXD_ERR_CRC:
6238                 netdev_err(port->dev, "bad rx status %08x (crc error), size=%zu\n",
6239                            status, sz);
6240                 break;
6241         case MVPP2_RXD_ERR_OVERRUN:
6242                 netdev_err(port->dev, "bad rx status %08x (overrun error), size=%zu\n",
6243                            status, sz);
6244                 break;
6245         case MVPP2_RXD_ERR_RESOURCE:
6246                 netdev_err(port->dev, "bad rx status %08x (resource error), size=%zu\n",
6247                            status, sz);
6248                 break;
6249         }
6250 }
6251
6252 /* Handle RX checksum offload */
6253 static void mvpp2_rx_csum(struct mvpp2_port *port, u32 status,
6254                           struct sk_buff *skb)
6255 {
6256         if (((status & MVPP2_RXD_L3_IP4) &&
6257              !(status & MVPP2_RXD_IP4_HEADER_ERR)) ||
6258             (status & MVPP2_RXD_L3_IP6))
6259                 if (((status & MVPP2_RXD_L4_UDP) ||
6260                      (status & MVPP2_RXD_L4_TCP)) &&
6261                      (status & MVPP2_RXD_L4_CSUM_OK)) {
6262                         skb->csum = 0;
6263                         skb->ip_summed = CHECKSUM_UNNECESSARY;
6264                         return;
6265                 }
6266
6267         skb->ip_summed = CHECKSUM_NONE;
6268 }
6269
6270 /* Reuse skb if possible, or allocate a new skb and add it to BM pool */
6271 static int mvpp2_rx_refill(struct mvpp2_port *port,
6272                            struct mvpp2_bm_pool *bm_pool, int pool)
6273 {
6274         dma_addr_t dma_addr;
6275         phys_addr_t phys_addr;
6276         void *buf;
6277
6278         /* No recycle or too many buffers are in use, so allocate a new skb */
6279         buf = mvpp2_buf_alloc(port, bm_pool, &dma_addr, &phys_addr,
6280                               GFP_ATOMIC);
6281         if (!buf)
6282                 return -ENOMEM;
6283
6284         mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
6285
6286         return 0;
6287 }
6288
6289 /* Handle tx checksum */
6290 static u32 mvpp2_skb_tx_csum(struct mvpp2_port *port, struct sk_buff *skb)
6291 {
6292         if (skb->ip_summed == CHECKSUM_PARTIAL) {
6293                 int ip_hdr_len = 0;
6294                 u8 l4_proto;
6295
6296                 if (skb->protocol == htons(ETH_P_IP)) {
6297                         struct iphdr *ip4h = ip_hdr(skb);
6298
6299                         /* Calculate IPv4 checksum and L4 checksum */
6300                         ip_hdr_len = ip4h->ihl;
6301                         l4_proto = ip4h->protocol;
6302                 } else if (skb->protocol == htons(ETH_P_IPV6)) {
6303                         struct ipv6hdr *ip6h = ipv6_hdr(skb);
6304
6305                         /* Read l4_protocol from one of IPv6 extra headers */
6306                         if (skb_network_header_len(skb) > 0)
6307                                 ip_hdr_len = (skb_network_header_len(skb) >> 2);
6308                         l4_proto = ip6h->nexthdr;
6309                 } else {
6310                         return MVPP2_TXD_L4_CSUM_NOT;
6311                 }
6312
6313                 return mvpp2_txq_desc_csum(skb_network_offset(skb),
6314                                 skb->protocol, ip_hdr_len, l4_proto);
6315         }
6316
6317         return MVPP2_TXD_L4_CSUM_NOT | MVPP2_TXD_IP_CSUM_DISABLE;
6318 }
6319
6320 /* Main rx processing */
6321 static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
6322                     int rx_todo, struct mvpp2_rx_queue *rxq)
6323 {
6324         struct net_device *dev = port->dev;
6325         int rx_received;
6326         int rx_done = 0;
6327         u32 rcvd_pkts = 0;
6328         u32 rcvd_bytes = 0;
6329
6330         /* Get number of received packets and clamp the to-do */
6331         rx_received = mvpp2_rxq_received(port, rxq->id);
6332         if (rx_todo > rx_received)
6333                 rx_todo = rx_received;
6334
6335         while (rx_done < rx_todo) {
6336                 struct mvpp2_rx_desc *rx_desc = mvpp2_rxq_next_desc_get(rxq);
6337                 struct mvpp2_bm_pool *bm_pool;
6338                 struct sk_buff *skb;
6339                 unsigned int frag_size;
6340                 dma_addr_t dma_addr;
6341                 phys_addr_t phys_addr;
6342                 u32 rx_status;
6343                 int pool, rx_bytes, err;
6344                 void *data;
6345
6346                 rx_done++;
6347                 rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
6348                 rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
6349                 rx_bytes -= MVPP2_MH_SIZE;
6350                 dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
6351                 phys_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
6352                 data = (void *)phys_to_virt(phys_addr);
6353
6354                 pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
6355                         MVPP2_RXD_BM_POOL_ID_OFFS;
6356                 bm_pool = &port->priv->bm_pools[pool];
6357
6358                 /* In case of an error, release the requested buffer pointer
6359                  * to the Buffer Manager. This request process is controlled
6360                  * by the hardware, and the information about the buffer is
6361                  * comprised by the RX descriptor.
6362                  */
6363                 if (rx_status & MVPP2_RXD_ERR_SUMMARY) {
6364 err_drop_frame:
6365                         dev->stats.rx_errors++;
6366                         mvpp2_rx_error(port, rx_desc);
6367                         /* Return the buffer to the pool */
6368                         mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
6369                         continue;
6370                 }
6371
6372                 if (bm_pool->frag_size > PAGE_SIZE)
6373                         frag_size = 0;
6374                 else
6375                         frag_size = bm_pool->frag_size;
6376
6377                 skb = build_skb(data, frag_size);
6378                 if (!skb) {
6379                         netdev_warn(port->dev, "skb build failed\n");
6380                         goto err_drop_frame;
6381                 }
6382
6383                 err = mvpp2_rx_refill(port, bm_pool, pool);
6384                 if (err) {
6385                         netdev_err(port->dev, "failed to refill BM pools\n");
6386                         goto err_drop_frame;
6387                 }
6388
6389                 dma_unmap_single(dev->dev.parent, dma_addr,
6390                                  bm_pool->buf_size, DMA_FROM_DEVICE);
6391
6392                 rcvd_pkts++;
6393                 rcvd_bytes += rx_bytes;
6394
6395                 skb_reserve(skb, MVPP2_MH_SIZE + NET_SKB_PAD);
6396                 skb_put(skb, rx_bytes);
6397                 skb->protocol = eth_type_trans(skb, dev);
6398                 mvpp2_rx_csum(port, rx_status, skb);
6399
6400                 napi_gro_receive(napi, skb);
6401         }
6402
6403         if (rcvd_pkts) {
6404                 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6405
6406                 u64_stats_update_begin(&stats->syncp);
6407                 stats->rx_packets += rcvd_pkts;
6408                 stats->rx_bytes   += rcvd_bytes;
6409                 u64_stats_update_end(&stats->syncp);
6410         }
6411
6412         /* Update Rx queue management counters */
6413         wmb();
6414         mvpp2_rxq_status_update(port, rxq->id, rx_done, rx_done);
6415
6416         return rx_todo;
6417 }
6418
6419 static inline void
6420 tx_desc_unmap_put(struct mvpp2_port *port, struct mvpp2_tx_queue *txq,
6421                   struct mvpp2_tx_desc *desc)
6422 {
6423         struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6424
6425         dma_addr_t buf_dma_addr =
6426                 mvpp2_txdesc_dma_addr_get(port, desc);
6427         size_t buf_sz =
6428                 mvpp2_txdesc_size_get(port, desc);
6429         if (!IS_TSO_HEADER(txq_pcpu, buf_dma_addr))
6430                 dma_unmap_single(port->dev->dev.parent, buf_dma_addr,
6431                                  buf_sz, DMA_TO_DEVICE);
6432         mvpp2_txq_desc_put(txq);
6433 }
6434
6435 /* Handle tx fragmentation processing */
6436 static int mvpp2_tx_frag_process(struct mvpp2_port *port, struct sk_buff *skb,
6437                                  struct mvpp2_tx_queue *aggr_txq,
6438                                  struct mvpp2_tx_queue *txq)
6439 {
6440         struct mvpp2_txq_pcpu *txq_pcpu = this_cpu_ptr(txq->pcpu);
6441         struct mvpp2_tx_desc *tx_desc;
6442         int i;
6443         dma_addr_t buf_dma_addr;
6444
6445         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
6446                 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
6447                 void *addr = page_address(frag->page.p) + frag->page_offset;
6448
6449                 tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6450                 mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6451                 mvpp2_txdesc_size_set(port, tx_desc, frag->size);
6452
6453                 buf_dma_addr = dma_map_single(port->dev->dev.parent, addr,
6454                                                frag->size,
6455                                                DMA_TO_DEVICE);
6456                 if (dma_mapping_error(port->dev->dev.parent, buf_dma_addr)) {
6457                         mvpp2_txq_desc_put(txq);
6458                         goto cleanup;
6459                 }
6460
6461                 mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
6462
6463                 if (i == (skb_shinfo(skb)->nr_frags - 1)) {
6464                         /* Last descriptor */
6465                         mvpp2_txdesc_cmd_set(port, tx_desc,
6466                                              MVPP2_TXD_L_DESC);
6467                         mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6468                 } else {
6469                         /* Descriptor in the middle: Not First, Not Last */
6470                         mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6471                         mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6472                 }
6473         }
6474
6475         return 0;
6476 cleanup:
6477         /* Release all descriptors that were used to map fragments of
6478          * this packet, as well as the corresponding DMA mappings
6479          */
6480         for (i = i - 1; i >= 0; i--) {
6481                 tx_desc = txq->descs + i;
6482                 tx_desc_unmap_put(port, txq, tx_desc);
6483         }
6484
6485         return -ENOMEM;
6486 }
6487
6488 static inline void mvpp2_tso_put_hdr(struct sk_buff *skb,
6489                                      struct net_device *dev,
6490                                      struct mvpp2_tx_queue *txq,
6491                                      struct mvpp2_tx_queue *aggr_txq,
6492                                      struct mvpp2_txq_pcpu *txq_pcpu,
6493                                      int hdr_sz)
6494 {
6495         struct mvpp2_port *port = netdev_priv(dev);
6496         struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6497         dma_addr_t addr;
6498
6499         mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6500         mvpp2_txdesc_size_set(port, tx_desc, hdr_sz);
6501
6502         addr = txq_pcpu->tso_headers_dma +
6503                txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6504         mvpp2_txdesc_dma_addr_set(port, tx_desc, addr);
6505
6506         mvpp2_txdesc_cmd_set(port, tx_desc, mvpp2_skb_tx_csum(port, skb) |
6507                                             MVPP2_TXD_F_DESC |
6508                                             MVPP2_TXD_PADDING_DISABLE);
6509         mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6510 }
6511
6512 static inline int mvpp2_tso_put_data(struct sk_buff *skb,
6513                                      struct net_device *dev, struct tso_t *tso,
6514                                      struct mvpp2_tx_queue *txq,
6515                                      struct mvpp2_tx_queue *aggr_txq,
6516                                      struct mvpp2_txq_pcpu *txq_pcpu,
6517                                      int sz, bool left, bool last)
6518 {
6519         struct mvpp2_port *port = netdev_priv(dev);
6520         struct mvpp2_tx_desc *tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6521         dma_addr_t buf_dma_addr;
6522
6523         mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6524         mvpp2_txdesc_size_set(port, tx_desc, sz);
6525
6526         buf_dma_addr = dma_map_single(dev->dev.parent, tso->data, sz,
6527                                       DMA_TO_DEVICE);
6528         if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6529                 mvpp2_txq_desc_put(txq);
6530                 return -ENOMEM;
6531         }
6532
6533         mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
6534
6535         if (!left) {
6536                 mvpp2_txdesc_cmd_set(port, tx_desc, MVPP2_TXD_L_DESC);
6537                 if (last) {
6538                         mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6539                         return 0;
6540                 }
6541         } else {
6542                 mvpp2_txdesc_cmd_set(port, tx_desc, 0);
6543         }
6544
6545         mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6546         return 0;
6547 }
6548
6549 static int mvpp2_tx_tso(struct sk_buff *skb, struct net_device *dev,
6550                         struct mvpp2_tx_queue *txq,
6551                         struct mvpp2_tx_queue *aggr_txq,
6552                         struct mvpp2_txq_pcpu *txq_pcpu)
6553 {
6554         struct mvpp2_port *port = netdev_priv(dev);
6555         struct tso_t tso;
6556         int hdr_sz = skb_transport_offset(skb) + tcp_hdrlen(skb);
6557         int i, len, descs = 0;
6558
6559         /* Check number of available descriptors */
6560         if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq,
6561                                       tso_count_descs(skb)) ||
6562             mvpp2_txq_reserved_desc_num_proc(port->priv, txq, txq_pcpu,
6563                                              tso_count_descs(skb)))
6564                 return 0;
6565
6566         tso_start(skb, &tso);
6567         len = skb->len - hdr_sz;
6568         while (len > 0) {
6569                 int left = min_t(int, skb_shinfo(skb)->gso_size, len);
6570                 char *hdr = txq_pcpu->tso_headers +
6571                             txq_pcpu->txq_put_index * TSO_HEADER_SIZE;
6572
6573                 len -= left;
6574                 descs++;
6575
6576                 tso_build_hdr(skb, hdr, &tso, left, len == 0);
6577                 mvpp2_tso_put_hdr(skb, dev, txq, aggr_txq, txq_pcpu, hdr_sz);
6578
6579                 while (left > 0) {
6580                         int sz = min_t(int, tso.size, left);
6581                         left -= sz;
6582                         descs++;
6583
6584                         if (mvpp2_tso_put_data(skb, dev, &tso, txq, aggr_txq,
6585                                                txq_pcpu, sz, left, len == 0))
6586                                 goto release;
6587                         tso_build_data(skb, &tso, sz);
6588                 }
6589         }
6590
6591         return descs;
6592
6593 release:
6594         for (i = descs - 1; i >= 0; i--) {
6595                 struct mvpp2_tx_desc *tx_desc = txq->descs + i;
6596                 tx_desc_unmap_put(port, txq, tx_desc);
6597         }
6598         return 0;
6599 }
6600
6601 /* Main tx processing */
6602 static int mvpp2_tx(struct sk_buff *skb, struct net_device *dev)
6603 {
6604         struct mvpp2_port *port = netdev_priv(dev);
6605         struct mvpp2_tx_queue *txq, *aggr_txq;
6606         struct mvpp2_txq_pcpu *txq_pcpu;
6607         struct mvpp2_tx_desc *tx_desc;
6608         dma_addr_t buf_dma_addr;
6609         int frags = 0;
6610         u16 txq_id;
6611         u32 tx_cmd;
6612
6613         txq_id = skb_get_queue_mapping(skb);
6614         txq = port->txqs[txq_id];
6615         txq_pcpu = this_cpu_ptr(txq->pcpu);
6616         aggr_txq = &port->priv->aggr_txqs[smp_processor_id()];
6617
6618         if (skb_is_gso(skb)) {
6619                 frags = mvpp2_tx_tso(skb, dev, txq, aggr_txq, txq_pcpu);
6620                 goto out;
6621         }
6622         frags = skb_shinfo(skb)->nr_frags + 1;
6623
6624         /* Check number of available descriptors */
6625         if (mvpp2_aggr_desc_num_check(port->priv, aggr_txq, frags) ||
6626             mvpp2_txq_reserved_desc_num_proc(port->priv, txq,
6627                                              txq_pcpu, frags)) {
6628                 frags = 0;
6629                 goto out;
6630         }
6631
6632         /* Get a descriptor for the first part of the packet */
6633         tx_desc = mvpp2_txq_next_desc_get(aggr_txq);
6634         mvpp2_txdesc_txq_set(port, tx_desc, txq->id);
6635         mvpp2_txdesc_size_set(port, tx_desc, skb_headlen(skb));
6636
6637         buf_dma_addr = dma_map_single(dev->dev.parent, skb->data,
6638                                       skb_headlen(skb), DMA_TO_DEVICE);
6639         if (unlikely(dma_mapping_error(dev->dev.parent, buf_dma_addr))) {
6640                 mvpp2_txq_desc_put(txq);
6641                 frags = 0;
6642                 goto out;
6643         }
6644
6645         mvpp2_txdesc_dma_addr_set(port, tx_desc, buf_dma_addr);
6646
6647         tx_cmd = mvpp2_skb_tx_csum(port, skb);
6648
6649         if (frags == 1) {
6650                 /* First and Last descriptor */
6651                 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_L_DESC;
6652                 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6653                 mvpp2_txq_inc_put(port, txq_pcpu, skb, tx_desc);
6654         } else {
6655                 /* First but not Last */
6656                 tx_cmd |= MVPP2_TXD_F_DESC | MVPP2_TXD_PADDING_DISABLE;
6657                 mvpp2_txdesc_cmd_set(port, tx_desc, tx_cmd);
6658                 mvpp2_txq_inc_put(port, txq_pcpu, NULL, tx_desc);
6659
6660                 /* Continue with other skb fragments */
6661                 if (mvpp2_tx_frag_process(port, skb, aggr_txq, txq)) {
6662                         tx_desc_unmap_put(port, txq, tx_desc);
6663                         frags = 0;
6664                 }
6665         }
6666
6667 out:
6668         if (frags > 0) {
6669                 struct mvpp2_pcpu_stats *stats = this_cpu_ptr(port->stats);
6670                 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
6671
6672                 txq_pcpu->reserved_num -= frags;
6673                 txq_pcpu->count += frags;
6674                 aggr_txq->count += frags;
6675
6676                 /* Enable transmit */
6677                 wmb();
6678                 mvpp2_aggr_txq_pend_desc_add(port, frags);
6679
6680                 if (txq_pcpu->count >= txq_pcpu->stop_threshold)
6681                         netif_tx_stop_queue(nq);
6682
6683                 u64_stats_update_begin(&stats->syncp);
6684                 stats->tx_packets++;
6685                 stats->tx_bytes += skb->len;
6686                 u64_stats_update_end(&stats->syncp);
6687         } else {
6688                 dev->stats.tx_dropped++;
6689                 dev_kfree_skb_any(skb);
6690         }
6691
6692         /* Finalize TX processing */
6693         if (!port->has_tx_irqs && txq_pcpu->count >= txq->done_pkts_coal)
6694                 mvpp2_txq_done(port, txq, txq_pcpu);
6695
6696         /* Set the timer in case not all frags were processed */
6697         if (!port->has_tx_irqs && txq_pcpu->count <= frags &&
6698             txq_pcpu->count > 0) {
6699                 struct mvpp2_port_pcpu *port_pcpu = this_cpu_ptr(port->pcpu);
6700
6701                 mvpp2_timer_set(port_pcpu);
6702         }
6703
6704         return NETDEV_TX_OK;
6705 }
6706
6707 static inline void mvpp2_cause_error(struct net_device *dev, int cause)
6708 {
6709         if (cause & MVPP2_CAUSE_FCS_ERR_MASK)
6710                 netdev_err(dev, "FCS error\n");
6711         if (cause & MVPP2_CAUSE_RX_FIFO_OVERRUN_MASK)
6712                 netdev_err(dev, "rx fifo overrun error\n");
6713         if (cause & MVPP2_CAUSE_TX_FIFO_UNDERRUN_MASK)
6714                 netdev_err(dev, "tx fifo underrun error\n");
6715 }
6716
6717 static int mvpp2_poll(struct napi_struct *napi, int budget)
6718 {
6719         u32 cause_rx_tx, cause_rx, cause_tx, cause_misc;
6720         int rx_done = 0;
6721         struct mvpp2_port *port = netdev_priv(napi->dev);
6722         struct mvpp2_queue_vector *qv;
6723         int cpu = smp_processor_id();
6724
6725         qv = container_of(napi, struct mvpp2_queue_vector, napi);
6726
6727         /* Rx/Tx cause register
6728          *
6729          * Bits 0-15: each bit indicates received packets on the Rx queue
6730          * (bit 0 is for Rx queue 0).
6731          *
6732          * Bits 16-23: each bit indicates transmitted packets on the Tx queue
6733          * (bit 16 is for Tx queue 0).
6734          *
6735          * Each CPU has its own Rx/Tx cause register
6736          */
6737         cause_rx_tx = mvpp2_percpu_read(port->priv, qv->sw_thread_id,
6738                                         MVPP2_ISR_RX_TX_CAUSE_REG(port->id));
6739
6740         cause_misc = cause_rx_tx & MVPP2_CAUSE_MISC_SUM_MASK;
6741         if (cause_misc) {
6742                 mvpp2_cause_error(port->dev, cause_misc);
6743
6744                 /* Clear the cause register */
6745                 mvpp2_write(port->priv, MVPP2_ISR_MISC_CAUSE_REG, 0);
6746                 mvpp2_percpu_write(port->priv, cpu,
6747                                    MVPP2_ISR_RX_TX_CAUSE_REG(port->id),
6748                                    cause_rx_tx & ~MVPP2_CAUSE_MISC_SUM_MASK);
6749         }
6750
6751         cause_tx = cause_rx_tx & MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_MASK;
6752         if (cause_tx) {
6753                 cause_tx >>= MVPP2_CAUSE_TXQ_OCCUP_DESC_ALL_OFFSET;
6754                 mvpp2_tx_done(port, cause_tx, qv->sw_thread_id);
6755         }
6756
6757         /* Process RX packets */
6758         cause_rx = cause_rx_tx & MVPP2_CAUSE_RXQ_OCCUP_DESC_ALL_MASK;
6759         cause_rx <<= qv->first_rxq;
6760         cause_rx |= qv->pending_cause_rx;
6761         while (cause_rx && budget > 0) {
6762                 int count;
6763                 struct mvpp2_rx_queue *rxq;
6764
6765                 rxq = mvpp2_get_rx_queue(port, cause_rx);
6766                 if (!rxq)
6767                         break;
6768
6769                 count = mvpp2_rx(port, napi, budget, rxq);
6770                 rx_done += count;
6771                 budget -= count;
6772                 if (budget > 0) {
6773                         /* Clear the bit associated to this Rx queue
6774                          * so that next iteration will continue from
6775                          * the next Rx queue.
6776                          */
6777                         cause_rx &= ~(1 << rxq->logic_rxq);
6778                 }
6779         }
6780
6781         if (budget > 0) {
6782                 cause_rx = 0;
6783                 napi_complete_done(napi, rx_done);
6784
6785                 mvpp2_qvec_interrupt_enable(qv);
6786         }
6787         qv->pending_cause_rx = cause_rx;
6788         return rx_done;
6789 }
6790
6791 /* Set hw internals when starting port */
6792 static void mvpp2_start_dev(struct mvpp2_port *port)
6793 {
6794         struct net_device *ndev = port->dev;
6795         int i;
6796
6797         if (port->gop_id == 0 &&
6798             (port->phy_interface == PHY_INTERFACE_MODE_XAUI ||
6799              port->phy_interface == PHY_INTERFACE_MODE_10GKR))
6800                 mvpp2_xlg_max_rx_size_set(port);
6801         else
6802                 mvpp2_gmac_max_rx_size_set(port);
6803
6804         mvpp2_txp_max_tx_size_set(port);
6805
6806         for (i = 0; i < port->nqvecs; i++)
6807                 napi_enable(&port->qvecs[i].napi);
6808
6809         /* Enable interrupts on all CPUs */
6810         mvpp2_interrupts_enable(port);
6811
6812         if (port->priv->hw_version == MVPP22) {
6813                 mvpp22_comphy_init(port);
6814                 mvpp22_gop_init(port);
6815         }
6816
6817         mvpp2_port_mii_set(port);
6818         mvpp2_port_enable(port);
6819         if (ndev->phydev)
6820                 phy_start(ndev->phydev);
6821         netif_tx_start_all_queues(port->dev);
6822 }
6823
6824 /* Set hw internals when stopping port */
6825 static void mvpp2_stop_dev(struct mvpp2_port *port)
6826 {
6827         struct net_device *ndev = port->dev;
6828         int i;
6829
6830         /* Stop new packets from arriving to RXQs */
6831         mvpp2_ingress_disable(port);
6832
6833         mdelay(10);
6834
6835         /* Disable interrupts on all CPUs */
6836         mvpp2_interrupts_disable(port);
6837
6838         for (i = 0; i < port->nqvecs; i++)
6839                 napi_disable(&port->qvecs[i].napi);
6840
6841         netif_carrier_off(port->dev);
6842         netif_tx_stop_all_queues(port->dev);
6843
6844         mvpp2_egress_disable(port);
6845         mvpp2_port_disable(port);
6846         if (ndev->phydev)
6847                 phy_stop(ndev->phydev);
6848         phy_power_off(port->comphy);
6849 }
6850
6851 static int mvpp2_check_ringparam_valid(struct net_device *dev,
6852                                        struct ethtool_ringparam *ring)
6853 {
6854         u16 new_rx_pending = ring->rx_pending;
6855         u16 new_tx_pending = ring->tx_pending;
6856
6857         if (ring->rx_pending == 0 || ring->tx_pending == 0)
6858                 return -EINVAL;
6859
6860         if (ring->rx_pending > MVPP2_MAX_RXD)
6861                 new_rx_pending = MVPP2_MAX_RXD;
6862         else if (!IS_ALIGNED(ring->rx_pending, 16))
6863                 new_rx_pending = ALIGN(ring->rx_pending, 16);
6864
6865         if (ring->tx_pending > MVPP2_MAX_TXD)
6866                 new_tx_pending = MVPP2_MAX_TXD;
6867         else if (!IS_ALIGNED(ring->tx_pending, 32))
6868                 new_tx_pending = ALIGN(ring->tx_pending, 32);
6869
6870         if (ring->rx_pending != new_rx_pending) {
6871                 netdev_info(dev, "illegal Rx ring size value %d, round to %d\n",
6872                             ring->rx_pending, new_rx_pending);
6873                 ring->rx_pending = new_rx_pending;
6874         }
6875
6876         if (ring->tx_pending != new_tx_pending) {
6877                 netdev_info(dev, "illegal Tx ring size value %d, round to %d\n",
6878                             ring->tx_pending, new_tx_pending);
6879                 ring->tx_pending = new_tx_pending;
6880         }
6881
6882         return 0;
6883 }
6884
6885 static void mvpp21_get_mac_address(struct mvpp2_port *port, unsigned char *addr)
6886 {
6887         u32 mac_addr_l, mac_addr_m, mac_addr_h;
6888
6889         mac_addr_l = readl(port->base + MVPP2_GMAC_CTRL_1_REG);
6890         mac_addr_m = readl(port->priv->lms_base + MVPP2_SRC_ADDR_MIDDLE);
6891         mac_addr_h = readl(port->priv->lms_base + MVPP2_SRC_ADDR_HIGH);
6892         addr[0] = (mac_addr_h >> 24) & 0xFF;
6893         addr[1] = (mac_addr_h >> 16) & 0xFF;
6894         addr[2] = (mac_addr_h >> 8) & 0xFF;
6895         addr[3] = mac_addr_h & 0xFF;
6896         addr[4] = mac_addr_m & 0xFF;
6897         addr[5] = (mac_addr_l >> MVPP2_GMAC_SA_LOW_OFFS) & 0xFF;
6898 }
6899
6900 static int mvpp2_phy_connect(struct mvpp2_port *port)
6901 {
6902         struct phy_device *phy_dev;
6903
6904         /* No PHY is attached */
6905         if (!port->phy_node)
6906                 return 0;
6907
6908         phy_dev = of_phy_connect(port->dev, port->phy_node, mvpp2_link_event, 0,
6909                                  port->phy_interface);
6910         if (!phy_dev) {
6911                 netdev_err(port->dev, "cannot connect to phy\n");
6912                 return -ENODEV;
6913         }
6914         phy_dev->supported &= PHY_GBIT_FEATURES;
6915         phy_dev->advertising = phy_dev->supported;
6916
6917         port->link    = 0;
6918         port->duplex  = 0;
6919         port->speed   = 0;
6920
6921         return 0;
6922 }
6923
6924 static void mvpp2_phy_disconnect(struct mvpp2_port *port)
6925 {
6926         struct net_device *ndev = port->dev;
6927
6928         if (!ndev->phydev)
6929                 return;
6930
6931         phy_disconnect(ndev->phydev);
6932 }
6933
6934 static int mvpp2_irqs_init(struct mvpp2_port *port)
6935 {
6936         int err, i;
6937
6938         for (i = 0; i < port->nqvecs; i++) {
6939                 struct mvpp2_queue_vector *qv = port->qvecs + i;
6940
6941                 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6942                         irq_set_status_flags(qv->irq, IRQ_NO_BALANCING);
6943
6944                 err = request_irq(qv->irq, mvpp2_isr, 0, port->dev->name, qv);
6945                 if (err)
6946                         goto err;
6947
6948                 if (qv->type == MVPP2_QUEUE_VECTOR_PRIVATE)
6949                         irq_set_affinity_hint(qv->irq,
6950                                               cpumask_of(qv->sw_thread_id));
6951         }
6952
6953         return 0;
6954 err:
6955         for (i = 0; i < port->nqvecs; i++) {
6956                 struct mvpp2_queue_vector *qv = port->qvecs + i;
6957
6958                 irq_set_affinity_hint(qv->irq, NULL);
6959                 free_irq(qv->irq, qv);
6960         }
6961
6962         return err;
6963 }
6964
6965 static void mvpp2_irqs_deinit(struct mvpp2_port *port)
6966 {
6967         int i;
6968
6969         for (i = 0; i < port->nqvecs; i++) {
6970                 struct mvpp2_queue_vector *qv = port->qvecs + i;
6971
6972                 irq_set_affinity_hint(qv->irq, NULL);
6973                 irq_clear_status_flags(qv->irq, IRQ_NO_BALANCING);
6974                 free_irq(qv->irq, qv);
6975         }
6976 }
6977
6978 static void mvpp22_init_rss(struct mvpp2_port *port)
6979 {
6980         struct mvpp2 *priv = port->priv;
6981         int i;
6982
6983         /* Set the table width: replace the whole classifier Rx queue number
6984          * with the ones configured in RSS table entries.
6985          */
6986         mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_TABLE(0));
6987         mvpp2_write(priv, MVPP22_RSS_WIDTH, 8);
6988
6989         /* Loop through the classifier Rx Queues and map them to a RSS table.
6990          * Map them all to the first table (0) by default.
6991          */
6992         for (i = 0; i < MVPP2_CLS_RX_QUEUES; i++) {
6993                 mvpp2_write(priv, MVPP22_RSS_INDEX, MVPP22_RSS_INDEX_QUEUE(i));
6994                 mvpp2_write(priv, MVPP22_RSS_TABLE,
6995                             MVPP22_RSS_TABLE_POINTER(0));
6996         }
6997
6998         /* Configure the first table to evenly distribute the packets across
6999          * real Rx Queues. The table entries map a hash to an port Rx Queue.
7000          */
7001         for (i = 0; i < MVPP22_RSS_TABLE_ENTRIES; i++) {
7002                 u32 sel = MVPP22_RSS_INDEX_TABLE(0) |
7003                           MVPP22_RSS_INDEX_TABLE_ENTRY(i);
7004                 mvpp2_write(priv, MVPP22_RSS_INDEX, sel);
7005
7006                 mvpp2_write(priv, MVPP22_RSS_TABLE_ENTRY, i % port->nrxqs);
7007         }
7008
7009 }
7010
7011 static int mvpp2_open(struct net_device *dev)
7012 {
7013         struct mvpp2_port *port = netdev_priv(dev);
7014         struct mvpp2 *priv = port->priv;
7015         unsigned char mac_bcast[ETH_ALEN] = {
7016                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
7017         int err;
7018
7019         err = mvpp2_prs_mac_da_accept(port->priv, port->id, mac_bcast, true);
7020         if (err) {
7021                 netdev_err(dev, "mvpp2_prs_mac_da_accept BC failed\n");
7022                 return err;
7023         }
7024         err = mvpp2_prs_mac_da_accept(port->priv, port->id,
7025                                       dev->dev_addr, true);
7026         if (err) {
7027                 netdev_err(dev, "mvpp2_prs_mac_da_accept MC failed\n");
7028                 return err;
7029         }
7030         err = mvpp2_prs_tag_mode_set(port->priv, port->id, MVPP2_TAG_TYPE_MH);
7031         if (err) {
7032                 netdev_err(dev, "mvpp2_prs_tag_mode_set failed\n");
7033                 return err;
7034         }
7035         err = mvpp2_prs_def_flow(port);
7036         if (err) {
7037                 netdev_err(dev, "mvpp2_prs_def_flow failed\n");
7038                 return err;
7039         }
7040
7041         /* Allocate the Rx/Tx queues */
7042         err = mvpp2_setup_rxqs(port);
7043         if (err) {
7044                 netdev_err(port->dev, "cannot allocate Rx queues\n");
7045                 return err;
7046         }
7047
7048         err = mvpp2_setup_txqs(port);
7049         if (err) {
7050                 netdev_err(port->dev, "cannot allocate Tx queues\n");
7051                 goto err_cleanup_rxqs;
7052         }
7053
7054         err = mvpp2_irqs_init(port);
7055         if (err) {
7056                 netdev_err(port->dev, "cannot init IRQs\n");
7057                 goto err_cleanup_txqs;
7058         }
7059
7060         if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq) {
7061                 err = request_irq(port->link_irq, mvpp2_link_status_isr, 0,
7062                                   dev->name, port);
7063                 if (err) {
7064                         netdev_err(port->dev, "cannot request link IRQ %d\n",
7065                                    port->link_irq);
7066                         goto err_free_irq;
7067                 }
7068
7069                 mvpp22_gop_setup_irq(port);
7070         }
7071
7072         /* In default link is down */
7073         netif_carrier_off(port->dev);
7074
7075         err = mvpp2_phy_connect(port);
7076         if (err < 0)
7077                 goto err_free_link_irq;
7078
7079         /* Unmask interrupts on all CPUs */
7080         on_each_cpu(mvpp2_interrupts_unmask, port, 1);
7081         mvpp2_shared_interrupt_mask_unmask(port, false);
7082
7083         mvpp2_start_dev(port);
7084
7085         if (priv->hw_version == MVPP22)
7086                 mvpp22_init_rss(port);
7087
7088         /* Start hardware statistics gathering */
7089         queue_delayed_work(priv->stats_queue, &port->stats_work,
7090                            MVPP2_MIB_COUNTERS_STATS_DELAY);
7091
7092         return 0;
7093
7094 err_free_link_irq:
7095         if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7096                 free_irq(port->link_irq, port);
7097 err_free_irq:
7098         mvpp2_irqs_deinit(port);
7099 err_cleanup_txqs:
7100         mvpp2_cleanup_txqs(port);
7101 err_cleanup_rxqs:
7102         mvpp2_cleanup_rxqs(port);
7103         return err;
7104 }
7105
7106 static int mvpp2_stop(struct net_device *dev)
7107 {
7108         struct mvpp2_port *port = netdev_priv(dev);
7109         struct mvpp2_port_pcpu *port_pcpu;
7110         struct mvpp2 *priv = port->priv;
7111         int cpu;
7112
7113         mvpp2_stop_dev(port);
7114         mvpp2_phy_disconnect(port);
7115
7116         /* Mask interrupts on all CPUs */
7117         on_each_cpu(mvpp2_interrupts_mask, port, 1);
7118         mvpp2_shared_interrupt_mask_unmask(port, true);
7119
7120         if (priv->hw_version == MVPP22 && !port->phy_node && port->link_irq)
7121                 free_irq(port->link_irq, port);
7122
7123         mvpp2_irqs_deinit(port);
7124         if (!port->has_tx_irqs) {
7125                 for_each_present_cpu(cpu) {
7126                         port_pcpu = per_cpu_ptr(port->pcpu, cpu);
7127
7128                         hrtimer_cancel(&port_pcpu->tx_done_timer);
7129                         port_pcpu->timer_scheduled = false;
7130                         tasklet_kill(&port_pcpu->tx_done_tasklet);
7131                 }
7132         }
7133         mvpp2_cleanup_rxqs(port);
7134         mvpp2_cleanup_txqs(port);
7135
7136         cancel_delayed_work_sync(&port->stats_work);
7137
7138         return 0;
7139 }
7140
7141 static void mvpp2_set_rx_mode(struct net_device *dev)
7142 {
7143         struct mvpp2_port *port = netdev_priv(dev);
7144         struct mvpp2 *priv = port->priv;
7145         struct netdev_hw_addr *ha;
7146         int id = port->id;
7147         bool allmulti = dev->flags & IFF_ALLMULTI;
7148
7149         mvpp2_prs_mac_promisc_set(priv, id, dev->flags & IFF_PROMISC);
7150         mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_ALL, allmulti);
7151         mvpp2_prs_mac_multi_set(priv, id, MVPP2_PE_MAC_MC_IP6, allmulti);
7152
7153         /* Remove all port->id's mcast enries */
7154         mvpp2_prs_mcast_del_all(priv, id);
7155
7156         if (allmulti && !netdev_mc_empty(dev)) {
7157                 netdev_for_each_mc_addr(ha, dev)
7158                         mvpp2_prs_mac_da_accept(priv, id, ha->addr, true);
7159         }
7160 }
7161
7162 static int mvpp2_set_mac_address(struct net_device *dev, void *p)
7163 {
7164         struct mvpp2_port *port = netdev_priv(dev);
7165         const struct sockaddr *addr = p;
7166         int err;
7167
7168         if (!is_valid_ether_addr(addr->sa_data)) {
7169                 err = -EADDRNOTAVAIL;
7170                 goto log_error;
7171         }
7172
7173         if (!netif_running(dev)) {
7174                 err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7175                 if (!err)
7176                         return 0;
7177                 /* Reconfigure parser to accept the original MAC address */
7178                 err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7179                 if (err)
7180                         goto log_error;
7181         }
7182
7183         mvpp2_stop_dev(port);
7184
7185         err = mvpp2_prs_update_mac_da(dev, addr->sa_data);
7186         if (!err)
7187                 goto out_start;
7188
7189         /* Reconfigure parser accept the original MAC address */
7190         err = mvpp2_prs_update_mac_da(dev, dev->dev_addr);
7191         if (err)
7192                 goto log_error;
7193 out_start:
7194         mvpp2_start_dev(port);
7195         mvpp2_egress_enable(port);
7196         mvpp2_ingress_enable(port);
7197         return 0;
7198 log_error:
7199         netdev_err(dev, "failed to change MAC address\n");
7200         return err;
7201 }
7202
7203 static int mvpp2_change_mtu(struct net_device *dev, int mtu)
7204 {
7205         struct mvpp2_port *port = netdev_priv(dev);
7206         int err;
7207
7208         if (!IS_ALIGNED(MVPP2_RX_PKT_SIZE(mtu), 8)) {
7209                 netdev_info(dev, "illegal MTU value %d, round to %d\n", mtu,
7210                             ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8));
7211                 mtu = ALIGN(MVPP2_RX_PKT_SIZE(mtu), 8);
7212         }
7213
7214         if (!netif_running(dev)) {
7215                 err = mvpp2_bm_update_mtu(dev, mtu);
7216                 if (!err) {
7217                         port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
7218                         return 0;
7219                 }
7220
7221                 /* Reconfigure BM to the original MTU */
7222                 err = mvpp2_bm_update_mtu(dev, dev->mtu);
7223                 if (err)
7224                         goto log_error;
7225         }
7226
7227         mvpp2_stop_dev(port);
7228
7229         err = mvpp2_bm_update_mtu(dev, mtu);
7230         if (!err) {
7231                 port->pkt_size =  MVPP2_RX_PKT_SIZE(mtu);
7232                 goto out_start;
7233         }
7234
7235         /* Reconfigure BM to the original MTU */
7236         err = mvpp2_bm_update_mtu(dev, dev->mtu);
7237         if (err)
7238                 goto log_error;
7239
7240 out_start:
7241         mvpp2_start_dev(port);
7242         mvpp2_egress_enable(port);
7243         mvpp2_ingress_enable(port);
7244
7245         return 0;
7246 log_error:
7247         netdev_err(dev, "failed to change MTU\n");
7248         return err;
7249 }
7250
7251 static void
7252 mvpp2_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
7253 {
7254         struct mvpp2_port *port = netdev_priv(dev);
7255         unsigned int start;
7256         int cpu;
7257
7258         for_each_possible_cpu(cpu) {
7259                 struct mvpp2_pcpu_stats *cpu_stats;
7260                 u64 rx_packets;
7261                 u64 rx_bytes;
7262                 u64 tx_packets;
7263                 u64 tx_bytes;
7264
7265                 cpu_stats = per_cpu_ptr(port->stats, cpu);
7266                 do {
7267                         start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
7268                         rx_packets = cpu_stats->rx_packets;
7269                         rx_bytes   = cpu_stats->rx_bytes;
7270                         tx_packets = cpu_stats->tx_packets;
7271                         tx_bytes   = cpu_stats->tx_bytes;
7272                 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
7273
7274                 stats->rx_packets += rx_packets;
7275                 stats->rx_bytes   += rx_bytes;
7276                 stats->tx_packets += tx_packets;
7277                 stats->tx_bytes   += tx_bytes;
7278         }
7279
7280         stats->rx_errors        = dev->stats.rx_errors;
7281         stats->rx_dropped       = dev->stats.rx_dropped;
7282         stats->tx_dropped       = dev->stats.tx_dropped;
7283 }
7284
7285 static int mvpp2_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
7286 {
7287         int ret;
7288
7289         if (!dev->phydev)
7290                 return -ENOTSUPP;
7291
7292         ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
7293         if (!ret)
7294                 mvpp2_link_event(dev);
7295
7296         return ret;
7297 }
7298
7299 /* Ethtool methods */
7300
7301 /* Set interrupt coalescing for ethtools */
7302 static int mvpp2_ethtool_set_coalesce(struct net_device *dev,
7303                                       struct ethtool_coalesce *c)
7304 {
7305         struct mvpp2_port *port = netdev_priv(dev);
7306         int queue;
7307
7308         for (queue = 0; queue < port->nrxqs; queue++) {
7309                 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7310
7311                 rxq->time_coal = c->rx_coalesce_usecs;
7312                 rxq->pkts_coal = c->rx_max_coalesced_frames;
7313                 mvpp2_rx_pkts_coal_set(port, rxq);
7314                 mvpp2_rx_time_coal_set(port, rxq);
7315         }
7316
7317         if (port->has_tx_irqs) {
7318                 port->tx_time_coal = c->tx_coalesce_usecs;
7319                 mvpp2_tx_time_coal_set(port);
7320         }
7321
7322         for (queue = 0; queue < port->ntxqs; queue++) {
7323                 struct mvpp2_tx_queue *txq = port->txqs[queue];
7324
7325                 txq->done_pkts_coal = c->tx_max_coalesced_frames;
7326
7327                 if (port->has_tx_irqs)
7328                         mvpp2_tx_pkts_coal_set(port, txq);
7329         }
7330
7331         return 0;
7332 }
7333
7334 /* get coalescing for ethtools */
7335 static int mvpp2_ethtool_get_coalesce(struct net_device *dev,
7336                                       struct ethtool_coalesce *c)
7337 {
7338         struct mvpp2_port *port = netdev_priv(dev);
7339
7340         c->rx_coalesce_usecs        = port->rxqs[0]->time_coal;
7341         c->rx_max_coalesced_frames  = port->rxqs[0]->pkts_coal;
7342         c->tx_max_coalesced_frames =  port->txqs[0]->done_pkts_coal;
7343         return 0;
7344 }
7345
7346 static void mvpp2_ethtool_get_drvinfo(struct net_device *dev,
7347                                       struct ethtool_drvinfo *drvinfo)
7348 {
7349         strlcpy(drvinfo->driver, MVPP2_DRIVER_NAME,
7350                 sizeof(drvinfo->driver));
7351         strlcpy(drvinfo->version, MVPP2_DRIVER_VERSION,
7352                 sizeof(drvinfo->version));
7353         strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
7354                 sizeof(drvinfo->bus_info));
7355 }
7356
7357 static void mvpp2_ethtool_get_ringparam(struct net_device *dev,
7358                                         struct ethtool_ringparam *ring)
7359 {
7360         struct mvpp2_port *port = netdev_priv(dev);
7361
7362         ring->rx_max_pending = MVPP2_MAX_RXD;
7363         ring->tx_max_pending = MVPP2_MAX_TXD;
7364         ring->rx_pending = port->rx_ring_size;
7365         ring->tx_pending = port->tx_ring_size;
7366 }
7367
7368 static int mvpp2_ethtool_set_ringparam(struct net_device *dev,
7369                                        struct ethtool_ringparam *ring)
7370 {
7371         struct mvpp2_port *port = netdev_priv(dev);
7372         u16 prev_rx_ring_size = port->rx_ring_size;
7373         u16 prev_tx_ring_size = port->tx_ring_size;
7374         int err;
7375
7376         err = mvpp2_check_ringparam_valid(dev, ring);
7377         if (err)
7378                 return err;
7379
7380         if (!netif_running(dev)) {
7381                 port->rx_ring_size = ring->rx_pending;
7382                 port->tx_ring_size = ring->tx_pending;
7383                 return 0;
7384         }
7385
7386         /* The interface is running, so we have to force a
7387          * reallocation of the queues
7388          */
7389         mvpp2_stop_dev(port);
7390         mvpp2_cleanup_rxqs(port);
7391         mvpp2_cleanup_txqs(port);
7392
7393         port->rx_ring_size = ring->rx_pending;
7394         port->tx_ring_size = ring->tx_pending;
7395
7396         err = mvpp2_setup_rxqs(port);
7397         if (err) {
7398                 /* Reallocate Rx queues with the original ring size */
7399                 port->rx_ring_size = prev_rx_ring_size;
7400                 ring->rx_pending = prev_rx_ring_size;
7401                 err = mvpp2_setup_rxqs(port);
7402                 if (err)
7403                         goto err_out;
7404         }
7405         err = mvpp2_setup_txqs(port);
7406         if (err) {
7407                 /* Reallocate Tx queues with the original ring size */
7408                 port->tx_ring_size = prev_tx_ring_size;
7409                 ring->tx_pending = prev_tx_ring_size;
7410                 err = mvpp2_setup_txqs(port);
7411                 if (err)
7412                         goto err_clean_rxqs;
7413         }
7414
7415         mvpp2_start_dev(port);
7416         mvpp2_egress_enable(port);
7417         mvpp2_ingress_enable(port);
7418
7419         return 0;
7420
7421 err_clean_rxqs:
7422         mvpp2_cleanup_rxqs(port);
7423 err_out:
7424         netdev_err(dev, "failed to change ring parameters");
7425         return err;
7426 }
7427
7428 /* Device ops */
7429
7430 static const struct net_device_ops mvpp2_netdev_ops = {
7431         .ndo_open               = mvpp2_open,
7432         .ndo_stop               = mvpp2_stop,
7433         .ndo_start_xmit         = mvpp2_tx,
7434         .ndo_set_rx_mode        = mvpp2_set_rx_mode,
7435         .ndo_set_mac_address    = mvpp2_set_mac_address,
7436         .ndo_change_mtu         = mvpp2_change_mtu,
7437         .ndo_get_stats64        = mvpp2_get_stats64,
7438         .ndo_do_ioctl           = mvpp2_ioctl,
7439 };
7440
7441 static const struct ethtool_ops mvpp2_eth_tool_ops = {
7442         .nway_reset     = phy_ethtool_nway_reset,
7443         .get_link       = ethtool_op_get_link,
7444         .set_coalesce   = mvpp2_ethtool_set_coalesce,
7445         .get_coalesce   = mvpp2_ethtool_get_coalesce,
7446         .get_drvinfo    = mvpp2_ethtool_get_drvinfo,
7447         .get_ringparam  = mvpp2_ethtool_get_ringparam,
7448         .set_ringparam  = mvpp2_ethtool_set_ringparam,
7449         .get_strings    = mvpp2_ethtool_get_strings,
7450         .get_ethtool_stats = mvpp2_ethtool_get_stats,
7451         .get_sset_count = mvpp2_ethtool_get_sset_count,
7452         .get_link_ksettings = phy_ethtool_get_link_ksettings,
7453         .set_link_ksettings = phy_ethtool_set_link_ksettings,
7454 };
7455
7456 /* Used for PPv2.1, or PPv2.2 with the old Device Tree binding that
7457  * had a single IRQ defined per-port.
7458  */
7459 static int mvpp2_simple_queue_vectors_init(struct mvpp2_port *port,
7460                                            struct device_node *port_node)
7461 {
7462         struct mvpp2_queue_vector *v = &port->qvecs[0];
7463
7464         v->first_rxq = 0;
7465         v->nrxqs = port->nrxqs;
7466         v->type = MVPP2_QUEUE_VECTOR_SHARED;
7467         v->sw_thread_id = 0;
7468         v->sw_thread_mask = *cpumask_bits(cpu_online_mask);
7469         v->port = port;
7470         v->irq = irq_of_parse_and_map(port_node, 0);
7471         if (v->irq <= 0)
7472                 return -EINVAL;
7473         netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7474                        NAPI_POLL_WEIGHT);
7475
7476         port->nqvecs = 1;
7477
7478         return 0;
7479 }
7480
7481 static int mvpp2_multi_queue_vectors_init(struct mvpp2_port *port,
7482                                           struct device_node *port_node)
7483 {
7484         struct mvpp2_queue_vector *v;
7485         int i, ret;
7486
7487         port->nqvecs = num_possible_cpus();
7488         if (queue_mode == MVPP2_QDIST_SINGLE_MODE)
7489                 port->nqvecs += 1;
7490
7491         for (i = 0; i < port->nqvecs; i++) {
7492                 char irqname[16];
7493
7494                 v = port->qvecs + i;
7495
7496                 v->port = port;
7497                 v->type = MVPP2_QUEUE_VECTOR_PRIVATE;
7498                 v->sw_thread_id = i;
7499                 v->sw_thread_mask = BIT(i);
7500
7501                 snprintf(irqname, sizeof(irqname), "tx-cpu%d", i);
7502
7503                 if (queue_mode == MVPP2_QDIST_MULTI_MODE) {
7504                         v->first_rxq = i * MVPP2_DEFAULT_RXQ;
7505                         v->nrxqs = MVPP2_DEFAULT_RXQ;
7506                 } else if (queue_mode == MVPP2_QDIST_SINGLE_MODE &&
7507                            i == (port->nqvecs - 1)) {
7508                         v->first_rxq = 0;
7509                         v->nrxqs = port->nrxqs;
7510                         v->type = MVPP2_QUEUE_VECTOR_SHARED;
7511                         strncpy(irqname, "rx-shared", sizeof(irqname));
7512                 }
7513
7514                 v->irq = of_irq_get_byname(port_node, irqname);
7515                 if (v->irq <= 0) {
7516                         ret = -EINVAL;
7517                         goto err;
7518                 }
7519
7520                 netif_napi_add(port->dev, &v->napi, mvpp2_poll,
7521                                NAPI_POLL_WEIGHT);
7522         }
7523
7524         return 0;
7525
7526 err:
7527         for (i = 0; i < port->nqvecs; i++)
7528                 irq_dispose_mapping(port->qvecs[i].irq);
7529         return ret;
7530 }
7531
7532 static int mvpp2_queue_vectors_init(struct mvpp2_port *port,
7533                                     struct device_node *port_node)
7534 {
7535         if (port->has_tx_irqs)
7536                 return mvpp2_multi_queue_vectors_init(port, port_node);
7537         else
7538                 return mvpp2_simple_queue_vectors_init(port, port_node);
7539 }
7540
7541 static void mvpp2_queue_vectors_deinit(struct mvpp2_port *port)
7542 {
7543         int i;
7544
7545         for (i = 0; i < port->nqvecs; i++)
7546                 irq_dispose_mapping(port->qvecs[i].irq);
7547 }
7548
7549 /* Configure Rx queue group interrupt for this port */
7550 static void mvpp2_rx_irqs_setup(struct mvpp2_port *port)
7551 {
7552         struct mvpp2 *priv = port->priv;
7553         u32 val;
7554         int i;
7555
7556         if (priv->hw_version == MVPP21) {
7557                 mvpp2_write(priv, MVPP21_ISR_RXQ_GROUP_REG(port->id),
7558                             port->nrxqs);
7559                 return;
7560         }
7561
7562         /* Handle the more complicated PPv2.2 case */
7563         for (i = 0; i < port->nqvecs; i++) {
7564                 struct mvpp2_queue_vector *qv = port->qvecs + i;
7565
7566                 if (!qv->nrxqs)
7567                         continue;
7568
7569                 val = qv->sw_thread_id;
7570                 val |= port->id << MVPP22_ISR_RXQ_GROUP_INDEX_GROUP_OFFSET;
7571                 mvpp2_write(priv, MVPP22_ISR_RXQ_GROUP_INDEX_REG, val);
7572
7573                 val = qv->first_rxq;
7574                 val |= qv->nrxqs << MVPP22_ISR_RXQ_SUB_GROUP_SIZE_OFFSET;
7575                 mvpp2_write(priv, MVPP22_ISR_RXQ_SUB_GROUP_CONFIG_REG, val);
7576         }
7577 }
7578
7579 /* Initialize port HW */
7580 static int mvpp2_port_init(struct mvpp2_port *port)
7581 {
7582         struct device *dev = port->dev->dev.parent;
7583         struct mvpp2 *priv = port->priv;
7584         struct mvpp2_txq_pcpu *txq_pcpu;
7585         int queue, cpu, err;
7586
7587         /* Checks for hardware constraints */
7588         if (port->first_rxq + port->nrxqs >
7589             MVPP2_MAX_PORTS * priv->max_port_rxqs)
7590                 return -EINVAL;
7591
7592         if (port->nrxqs % 4 || (port->nrxqs > priv->max_port_rxqs) ||
7593             (port->ntxqs > MVPP2_MAX_TXQ))
7594                 return -EINVAL;
7595
7596         /* Disable port */
7597         mvpp2_egress_disable(port);
7598         mvpp2_port_disable(port);
7599
7600         port->tx_time_coal = MVPP2_TXDONE_COAL_USEC;
7601
7602         port->txqs = devm_kcalloc(dev, port->ntxqs, sizeof(*port->txqs),
7603                                   GFP_KERNEL);
7604         if (!port->txqs)
7605                 return -ENOMEM;
7606
7607         /* Associate physical Tx queues to this port and initialize.
7608          * The mapping is predefined.
7609          */
7610         for (queue = 0; queue < port->ntxqs; queue++) {
7611                 int queue_phy_id = mvpp2_txq_phys(port->id, queue);
7612                 struct mvpp2_tx_queue *txq;
7613
7614                 txq = devm_kzalloc(dev, sizeof(*txq), GFP_KERNEL);
7615                 if (!txq) {
7616                         err = -ENOMEM;
7617                         goto err_free_percpu;
7618                 }
7619
7620                 txq->pcpu = alloc_percpu(struct mvpp2_txq_pcpu);
7621                 if (!txq->pcpu) {
7622                         err = -ENOMEM;
7623                         goto err_free_percpu;
7624                 }
7625
7626                 txq->id = queue_phy_id;
7627                 txq->log_id = queue;
7628                 txq->done_pkts_coal = MVPP2_TXDONE_COAL_PKTS_THRESH;
7629                 for_each_present_cpu(cpu) {
7630                         txq_pcpu = per_cpu_ptr(txq->pcpu, cpu);
7631                         txq_pcpu->cpu = cpu;
7632                 }
7633
7634                 port->txqs[queue] = txq;
7635         }
7636
7637         port->rxqs = devm_kcalloc(dev, port->nrxqs, sizeof(*port->rxqs),
7638                                   GFP_KERNEL);
7639         if (!port->rxqs) {
7640                 err = -ENOMEM;
7641                 goto err_free_percpu;
7642         }
7643
7644         /* Allocate and initialize Rx queue for this port */
7645         for (queue = 0; queue < port->nrxqs; queue++) {
7646                 struct mvpp2_rx_queue *rxq;
7647
7648                 /* Map physical Rx queue to port's logical Rx queue */
7649                 rxq = devm_kzalloc(dev, sizeof(*rxq), GFP_KERNEL);
7650                 if (!rxq) {
7651                         err = -ENOMEM;
7652                         goto err_free_percpu;
7653                 }
7654                 /* Map this Rx queue to a physical queue */
7655                 rxq->id = port->first_rxq + queue;
7656                 rxq->port = port->id;
7657                 rxq->logic_rxq = queue;
7658
7659                 port->rxqs[queue] = rxq;
7660         }
7661
7662         mvpp2_rx_irqs_setup(port);
7663
7664         /* Create Rx descriptor rings */
7665         for (queue = 0; queue < port->nrxqs; queue++) {
7666                 struct mvpp2_rx_queue *rxq = port->rxqs[queue];
7667
7668                 rxq->size = port->rx_ring_size;
7669                 rxq->pkts_coal = MVPP2_RX_COAL_PKTS;
7670                 rxq->time_coal = MVPP2_RX_COAL_USEC;
7671         }
7672
7673         mvpp2_ingress_disable(port);
7674
7675         /* Port default configuration */
7676         mvpp2_defaults_set(port);
7677
7678         /* Port's classifier configuration */
7679         mvpp2_cls_oversize_rxq_set(port);
7680         mvpp2_cls_port_config(port);
7681
7682         /* Provide an initial Rx packet size */
7683         port->pkt_size = MVPP2_RX_PKT_SIZE(port->dev->mtu);
7684
7685         /* Initialize pools for swf */
7686         err = mvpp2_swf_bm_pool_init(port);
7687         if (err)
7688                 goto err_free_percpu;
7689
7690         return 0;
7691
7692 err_free_percpu:
7693         for (queue = 0; queue < port->ntxqs; queue++) {
7694                 if (!port->txqs[queue])
7695                         continue;
7696                 free_percpu(port->txqs[queue]->pcpu);
7697         }
7698         return err;
7699 }
7700
7701 /* Checks if the port DT description has the TX interrupts
7702  * described. On PPv2.1, there are no such interrupts. On PPv2.2,
7703  * there are available, but we need to keep support for old DTs.
7704  */
7705 static bool mvpp2_port_has_tx_irqs(struct mvpp2 *priv,
7706                                    struct device_node *port_node)
7707 {
7708         char *irqs[5] = { "rx-shared", "tx-cpu0", "tx-cpu1",
7709                           "tx-cpu2", "tx-cpu3" };
7710         int ret, i;
7711
7712         if (priv->hw_version == MVPP21)
7713                 return false;
7714
7715         for (i = 0; i < 5; i++) {
7716                 ret = of_property_match_string(port_node, "interrupt-names",
7717                                                irqs[i]);
7718                 if (ret < 0)
7719                         return false;
7720         }
7721
7722         return true;
7723 }
7724
7725 static void mvpp2_port_copy_mac_addr(struct net_device *dev, struct mvpp2 *priv,
7726                                      struct device_node *port_node,
7727                                      char **mac_from)
7728 {
7729         struct mvpp2_port *port = netdev_priv(dev);
7730         char hw_mac_addr[ETH_ALEN] = {0};
7731         const char *dt_mac_addr;
7732
7733         dt_mac_addr = of_get_mac_address(port_node);
7734         if (dt_mac_addr && is_valid_ether_addr(dt_mac_addr)) {
7735                 *mac_from = "device tree";
7736                 ether_addr_copy(dev->dev_addr, dt_mac_addr);
7737                 return;
7738         }
7739
7740         if (priv->hw_version == MVPP21) {
7741                 mvpp21_get_mac_address(port, hw_mac_addr);
7742                 if (is_valid_ether_addr(hw_mac_addr)) {
7743                         *mac_from = "hardware";
7744                         ether_addr_copy(dev->dev_addr, hw_mac_addr);
7745                         return;
7746                 }
7747         }
7748
7749         *mac_from = "random";
7750         eth_hw_addr_random(dev);
7751 }
7752
7753 /* Ports initialization */
7754 static int mvpp2_port_probe(struct platform_device *pdev,
7755                             struct device_node *port_node,
7756                             struct mvpp2 *priv, int index)
7757 {
7758         struct device_node *phy_node;
7759         struct phy *comphy;
7760         struct mvpp2_port *port;
7761         struct mvpp2_port_pcpu *port_pcpu;
7762         struct net_device *dev;
7763         struct resource *res;
7764         char *mac_from = "";
7765         unsigned int ntxqs, nrxqs;
7766         bool has_tx_irqs;
7767         u32 id;
7768         int features;
7769         int phy_mode;
7770         int err, i, cpu;
7771
7772         has_tx_irqs = mvpp2_port_has_tx_irqs(priv, port_node);
7773
7774         if (!has_tx_irqs)
7775                 queue_mode = MVPP2_QDIST_SINGLE_MODE;
7776
7777         ntxqs = MVPP2_MAX_TXQ;
7778         if (priv->hw_version == MVPP22 && queue_mode == MVPP2_QDIST_MULTI_MODE)
7779                 nrxqs = MVPP2_DEFAULT_RXQ * num_possible_cpus();
7780         else
7781                 nrxqs = MVPP2_DEFAULT_RXQ;
7782
7783         dev = alloc_etherdev_mqs(sizeof(*port), ntxqs, nrxqs);
7784         if (!dev)
7785                 return -ENOMEM;
7786
7787         phy_node = of_parse_phandle(port_node, "phy", 0);
7788         phy_mode = of_get_phy_mode(port_node);
7789         if (phy_mode < 0) {
7790                 dev_err(&pdev->dev, "incorrect phy mode\n");
7791                 err = phy_mode;
7792                 goto err_free_netdev;
7793         }
7794
7795         comphy = devm_of_phy_get(&pdev->dev, port_node, NULL);
7796         if (IS_ERR(comphy)) {
7797                 if (PTR_ERR(comphy) == -EPROBE_DEFER) {
7798                         err = -EPROBE_DEFER;
7799                         goto err_free_netdev;
7800                 }
7801                 comphy = NULL;
7802         }
7803
7804         if (of_property_read_u32(port_node, "port-id", &id)) {
7805                 err = -EINVAL;
7806                 dev_err(&pdev->dev, "missing port-id value\n");
7807                 goto err_free_netdev;
7808         }
7809
7810         dev->tx_queue_len = MVPP2_MAX_TXD;
7811         dev->watchdog_timeo = 5 * HZ;
7812         dev->netdev_ops = &mvpp2_netdev_ops;
7813         dev->ethtool_ops = &mvpp2_eth_tool_ops;
7814
7815         port = netdev_priv(dev);
7816         port->dev = dev;
7817         port->ntxqs = ntxqs;
7818         port->nrxqs = nrxqs;
7819         port->priv = priv;
7820         port->has_tx_irqs = has_tx_irqs;
7821
7822         err = mvpp2_queue_vectors_init(port, port_node);
7823         if (err)
7824                 goto err_free_netdev;
7825
7826         port->link_irq = of_irq_get_byname(port_node, "link");
7827         if (port->link_irq == -EPROBE_DEFER) {
7828                 err = -EPROBE_DEFER;
7829                 goto err_deinit_qvecs;
7830         }
7831         if (port->link_irq <= 0)
7832                 /* the link irq is optional */
7833                 port->link_irq = 0;
7834
7835         if (of_property_read_bool(port_node, "marvell,loopback"))
7836                 port->flags |= MVPP2_F_LOOPBACK;
7837
7838         port->id = id;
7839         if (priv->hw_version == MVPP21)
7840                 port->first_rxq = port->id * port->nrxqs;
7841         else
7842                 port->first_rxq = port->id * priv->max_port_rxqs;
7843
7844         port->phy_node = phy_node;
7845         port->phy_interface = phy_mode;
7846         port->comphy = comphy;
7847
7848         if (priv->hw_version == MVPP21) {
7849                 res = platform_get_resource(pdev, IORESOURCE_MEM, 2 + id);
7850                 port->base = devm_ioremap_resource(&pdev->dev, res);
7851                 if (IS_ERR(port->base)) {
7852                         err = PTR_ERR(port->base);
7853                         goto err_free_irq;
7854                 }
7855
7856                 port->stats_base = port->priv->lms_base +
7857                                    MVPP21_MIB_COUNTERS_OFFSET +
7858                                    port->gop_id * MVPP21_MIB_COUNTERS_PORT_SZ;
7859         } else {
7860                 if (of_property_read_u32(port_node, "gop-port-id",
7861                                          &port->gop_id)) {
7862                         err = -EINVAL;
7863                         dev_err(&pdev->dev, "missing gop-port-id value\n");
7864                         goto err_deinit_qvecs;
7865                 }
7866
7867                 port->base = priv->iface_base + MVPP22_GMAC_BASE(port->gop_id);
7868                 port->stats_base = port->priv->iface_base +
7869                                    MVPP22_MIB_COUNTERS_OFFSET +
7870                                    port->gop_id * MVPP22_MIB_COUNTERS_PORT_SZ;
7871         }
7872
7873         /* Alloc per-cpu and ethtool stats */
7874         port->stats = netdev_alloc_pcpu_stats(struct mvpp2_pcpu_stats);
7875         if (!port->stats) {
7876                 err = -ENOMEM;
7877                 goto err_free_irq;
7878         }
7879
7880         port->ethtool_stats = devm_kcalloc(&pdev->dev,
7881                                            ARRAY_SIZE(mvpp2_ethtool_regs),
7882                                            sizeof(u64), GFP_KERNEL);
7883         if (!port->ethtool_stats) {
7884                 err = -ENOMEM;
7885                 goto err_free_stats;
7886         }
7887
7888         mutex_init(&port->gather_stats_lock);
7889         INIT_DELAYED_WORK(&port->stats_work, mvpp2_gather_hw_statistics);
7890
7891         mvpp2_port_copy_mac_addr(dev, priv, port_node, &mac_from);
7892
7893         port->tx_ring_size = MVPP2_MAX_TXD;
7894         port->rx_ring_size = MVPP2_MAX_RXD;
7895         SET_NETDEV_DEV(dev, &pdev->dev);
7896
7897         err = mvpp2_port_init(port);
7898         if (err < 0) {
7899                 dev_err(&pdev->dev, "failed to init port %d\n", id);
7900                 goto err_free_stats;
7901         }
7902
7903         mvpp2_port_periodic_xon_disable(port);
7904
7905         if (priv->hw_version == MVPP21)
7906                 mvpp2_port_fc_adv_enable(port);
7907
7908         mvpp2_port_reset(port);
7909
7910         port->pcpu = alloc_percpu(struct mvpp2_port_pcpu);
7911         if (!port->pcpu) {
7912                 err = -ENOMEM;
7913                 goto err_free_txq_pcpu;
7914         }
7915
7916         if (!port->has_tx_irqs) {
7917                 for_each_present_cpu(cpu) {
7918                         port_pcpu = per_cpu_ptr(port->pcpu, cpu);
7919
7920                         hrtimer_init(&port_pcpu->tx_done_timer, CLOCK_MONOTONIC,
7921                                      HRTIMER_MODE_REL_PINNED);
7922                         port_pcpu->tx_done_timer.function = mvpp2_hr_timer_cb;
7923                         port_pcpu->timer_scheduled = false;
7924
7925                         tasklet_init(&port_pcpu->tx_done_tasklet,
7926                                      mvpp2_tx_proc_cb,
7927                                      (unsigned long)dev);
7928                 }
7929         }
7930
7931         features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
7932         dev->features = features | NETIF_F_RXCSUM;
7933         dev->hw_features |= features | NETIF_F_RXCSUM | NETIF_F_GRO;
7934         dev->vlan_features |= features;
7935         dev->gso_max_segs = MVPP2_MAX_TSO_SEGS;
7936
7937         /* MTU range: 68 - 9676 */
7938         dev->min_mtu = ETH_MIN_MTU;
7939         /* 9676 == 9700 - 20 and rounding to 8 */
7940         dev->max_mtu = 9676;
7941
7942         err = register_netdev(dev);
7943         if (err < 0) {
7944                 dev_err(&pdev->dev, "failed to register netdev\n");
7945                 goto err_free_port_pcpu;
7946         }
7947         netdev_info(dev, "Using %s mac address %pM\n", mac_from, dev->dev_addr);
7948
7949         priv->port_list[index] = port;
7950         return 0;
7951
7952 err_free_port_pcpu:
7953         free_percpu(port->pcpu);
7954 err_free_txq_pcpu:
7955         for (i = 0; i < port->ntxqs; i++)
7956                 free_percpu(port->txqs[i]->pcpu);
7957 err_free_stats:
7958         free_percpu(port->stats);
7959 err_free_irq:
7960         if (port->link_irq)
7961                 irq_dispose_mapping(port->link_irq);
7962 err_deinit_qvecs:
7963         mvpp2_queue_vectors_deinit(port);
7964 err_free_netdev:
7965         of_node_put(phy_node);
7966         free_netdev(dev);
7967         return err;
7968 }
7969
7970 /* Ports removal routine */
7971 static void mvpp2_port_remove(struct mvpp2_port *port)
7972 {
7973         int i;
7974
7975         unregister_netdev(port->dev);
7976         of_node_put(port->phy_node);
7977         free_percpu(port->pcpu);
7978         free_percpu(port->stats);
7979         for (i = 0; i < port->ntxqs; i++)
7980                 free_percpu(port->txqs[i]->pcpu);
7981         mvpp2_queue_vectors_deinit(port);
7982         if (port->link_irq)
7983                 irq_dispose_mapping(port->link_irq);
7984         free_netdev(port->dev);
7985 }
7986
7987 /* Initialize decoding windows */
7988 static void mvpp2_conf_mbus_windows(const struct mbus_dram_target_info *dram,
7989                                     struct mvpp2 *priv)
7990 {
7991         u32 win_enable;
7992         int i;
7993
7994         for (i = 0; i < 6; i++) {
7995                 mvpp2_write(priv, MVPP2_WIN_BASE(i), 0);
7996                 mvpp2_write(priv, MVPP2_WIN_SIZE(i), 0);
7997
7998                 if (i < 4)
7999                         mvpp2_write(priv, MVPP2_WIN_REMAP(i), 0);
8000         }
8001
8002         win_enable = 0;
8003
8004         for (i = 0; i < dram->num_cs; i++) {
8005                 const struct mbus_dram_window *cs = dram->cs + i;
8006
8007                 mvpp2_write(priv, MVPP2_WIN_BASE(i),
8008                             (cs->base & 0xffff0000) | (cs->mbus_attr << 8) |
8009                             dram->mbus_dram_target_id);
8010
8011                 mvpp2_write(priv, MVPP2_WIN_SIZE(i),
8012                             (cs->size - 1) & 0xffff0000);
8013
8014                 win_enable |= (1 << i);
8015         }
8016
8017         mvpp2_write(priv, MVPP2_BASE_ADDR_ENABLE, win_enable);
8018 }
8019
8020 /* Initialize Rx FIFO's */
8021 static void mvpp2_rx_fifo_init(struct mvpp2 *priv)
8022 {
8023         int port;
8024
8025         for (port = 0; port < MVPP2_MAX_PORTS; port++) {
8026                 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
8027                             MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
8028                 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
8029                             MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
8030         }
8031
8032         mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8033                     MVPP2_RX_FIFO_PORT_MIN_PKT);
8034         mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8035 }
8036
8037 static void mvpp22_rx_fifo_init(struct mvpp2 *priv)
8038 {
8039         int port;
8040
8041         /* The FIFO size parameters are set depending on the maximum speed a
8042          * given port can handle:
8043          * - Port 0: 10Gbps
8044          * - Port 1: 2.5Gbps
8045          * - Ports 2 and 3: 1Gbps
8046          */
8047
8048         mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(0),
8049                     MVPP2_RX_FIFO_PORT_DATA_SIZE_32KB);
8050         mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(0),
8051                     MVPP2_RX_FIFO_PORT_ATTR_SIZE_32KB);
8052
8053         mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(1),
8054                     MVPP2_RX_FIFO_PORT_DATA_SIZE_8KB);
8055         mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(1),
8056                     MVPP2_RX_FIFO_PORT_ATTR_SIZE_8KB);
8057
8058         for (port = 2; port < MVPP2_MAX_PORTS; port++) {
8059                 mvpp2_write(priv, MVPP2_RX_DATA_FIFO_SIZE_REG(port),
8060                             MVPP2_RX_FIFO_PORT_DATA_SIZE_4KB);
8061                 mvpp2_write(priv, MVPP2_RX_ATTR_FIFO_SIZE_REG(port),
8062                             MVPP2_RX_FIFO_PORT_ATTR_SIZE_4KB);
8063         }
8064
8065         mvpp2_write(priv, MVPP2_RX_MIN_PKT_SIZE_REG,
8066                     MVPP2_RX_FIFO_PORT_MIN_PKT);
8067         mvpp2_write(priv, MVPP2_RX_FIFO_INIT_REG, 0x1);
8068 }
8069
8070 /* Initialize Tx FIFO's */
8071 static void mvpp22_tx_fifo_init(struct mvpp2 *priv)
8072 {
8073         int port;
8074
8075         for (port = 0; port < MVPP2_MAX_PORTS; port++)
8076                 mvpp2_write(priv, MVPP22_TX_FIFO_SIZE_REG(port),
8077                             MVPP22_TX_FIFO_DATA_SIZE_3KB);
8078 }
8079
8080 static void mvpp2_axi_init(struct mvpp2 *priv)
8081 {
8082         u32 val, rdval, wrval;
8083
8084         mvpp2_write(priv, MVPP22_BM_ADDR_HIGH_RLS_REG, 0x0);
8085
8086         /* AXI Bridge Configuration */
8087
8088         rdval = MVPP22_AXI_CODE_CACHE_RD_CACHE
8089                 << MVPP22_AXI_ATTR_CACHE_OFFS;
8090         rdval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8091                 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8092
8093         wrval = MVPP22_AXI_CODE_CACHE_WR_CACHE
8094                 << MVPP22_AXI_ATTR_CACHE_OFFS;
8095         wrval |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8096                 << MVPP22_AXI_ATTR_DOMAIN_OFFS;
8097
8098         /* BM */
8099         mvpp2_write(priv, MVPP22_AXI_BM_WR_ATTR_REG, wrval);
8100         mvpp2_write(priv, MVPP22_AXI_BM_RD_ATTR_REG, rdval);
8101
8102         /* Descriptors */
8103         mvpp2_write(priv, MVPP22_AXI_AGGRQ_DESCR_RD_ATTR_REG, rdval);
8104         mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_WR_ATTR_REG, wrval);
8105         mvpp2_write(priv, MVPP22_AXI_TXQ_DESCR_RD_ATTR_REG, rdval);
8106         mvpp2_write(priv, MVPP22_AXI_RXQ_DESCR_WR_ATTR_REG, wrval);
8107
8108         /* Buffer Data */
8109         mvpp2_write(priv, MVPP22_AXI_TX_DATA_RD_ATTR_REG, rdval);
8110         mvpp2_write(priv, MVPP22_AXI_RX_DATA_WR_ATTR_REG, wrval);
8111
8112         val = MVPP22_AXI_CODE_CACHE_NON_CACHE
8113                 << MVPP22_AXI_CODE_CACHE_OFFS;
8114         val |= MVPP22_AXI_CODE_DOMAIN_SYSTEM
8115                 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8116         mvpp2_write(priv, MVPP22_AXI_RD_NORMAL_CODE_REG, val);
8117         mvpp2_write(priv, MVPP22_AXI_WR_NORMAL_CODE_REG, val);
8118
8119         val = MVPP22_AXI_CODE_CACHE_RD_CACHE
8120                 << MVPP22_AXI_CODE_CACHE_OFFS;
8121         val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8122                 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8123
8124         mvpp2_write(priv, MVPP22_AXI_RD_SNOOP_CODE_REG, val);
8125
8126         val = MVPP22_AXI_CODE_CACHE_WR_CACHE
8127                 << MVPP22_AXI_CODE_CACHE_OFFS;
8128         val |= MVPP22_AXI_CODE_DOMAIN_OUTER_DOM
8129                 << MVPP22_AXI_CODE_DOMAIN_OFFS;
8130
8131         mvpp2_write(priv, MVPP22_AXI_WR_SNOOP_CODE_REG, val);
8132 }
8133
8134 /* Initialize network controller common part HW */
8135 static int mvpp2_init(struct platform_device *pdev, struct mvpp2 *priv)
8136 {
8137         const struct mbus_dram_target_info *dram_target_info;
8138         int err, i;
8139         u32 val;
8140
8141         /* MBUS windows configuration */
8142         dram_target_info = mv_mbus_dram_info();
8143         if (dram_target_info)
8144                 mvpp2_conf_mbus_windows(dram_target_info, priv);
8145
8146         if (priv->hw_version == MVPP22)
8147                 mvpp2_axi_init(priv);
8148
8149         /* Disable HW PHY polling */
8150         if (priv->hw_version == MVPP21) {
8151                 val = readl(priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8152                 val |= MVPP2_PHY_AN_STOP_SMI0_MASK;
8153                 writel(val, priv->lms_base + MVPP2_PHY_AN_CFG0_REG);
8154         } else {
8155                 val = readl(priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8156                 val &= ~MVPP22_SMI_POLLING_EN;
8157                 writel(val, priv->iface_base + MVPP22_SMI_MISC_CFG_REG);
8158         }
8159
8160         /* Allocate and initialize aggregated TXQs */
8161         priv->aggr_txqs = devm_kcalloc(&pdev->dev, num_present_cpus(),
8162                                        sizeof(*priv->aggr_txqs),
8163                                        GFP_KERNEL);
8164         if (!priv->aggr_txqs)
8165                 return -ENOMEM;
8166
8167         for_each_present_cpu(i) {
8168                 priv->aggr_txqs[i].id = i;
8169                 priv->aggr_txqs[i].size = MVPP2_AGGR_TXQ_SIZE;
8170                 err = mvpp2_aggr_txq_init(pdev, &priv->aggr_txqs[i], i, priv);
8171                 if (err < 0)
8172                         return err;
8173         }
8174
8175         /* Fifo Init */
8176         if (priv->hw_version == MVPP21) {
8177                 mvpp2_rx_fifo_init(priv);
8178         } else {
8179                 mvpp22_rx_fifo_init(priv);
8180                 mvpp22_tx_fifo_init(priv);
8181         }
8182
8183         if (priv->hw_version == MVPP21)
8184                 writel(MVPP2_EXT_GLOBAL_CTRL_DEFAULT,
8185                        priv->lms_base + MVPP2_MNG_EXTENDED_GLOBAL_CTRL_REG);
8186
8187         /* Allow cache snoop when transmiting packets */
8188         mvpp2_write(priv, MVPP2_TX_SNOOP_REG, 0x1);
8189
8190         /* Buffer Manager initialization */
8191         err = mvpp2_bm_init(pdev, priv);
8192         if (err < 0)
8193                 return err;
8194
8195         /* Parser default initialization */
8196         err = mvpp2_prs_default_init(pdev, priv);
8197         if (err < 0)
8198                 return err;
8199
8200         /* Classifier default initialization */
8201         mvpp2_cls_init(priv);
8202
8203         return 0;
8204 }
8205
8206 static int mvpp2_probe(struct platform_device *pdev)
8207 {
8208         struct device_node *dn = pdev->dev.of_node;
8209         struct device_node *port_node;
8210         struct mvpp2 *priv;
8211         struct resource *res;
8212         void __iomem *base;
8213         int i;
8214         int err;
8215
8216         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
8217         if (!priv)
8218                 return -ENOMEM;
8219
8220         priv->hw_version =
8221                 (unsigned long)of_device_get_match_data(&pdev->dev);
8222
8223         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
8224         base = devm_ioremap_resource(&pdev->dev, res);
8225         if (IS_ERR(base))
8226                 return PTR_ERR(base);
8227
8228         if (priv->hw_version == MVPP21) {
8229                 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8230                 priv->lms_base = devm_ioremap_resource(&pdev->dev, res);
8231                 if (IS_ERR(priv->lms_base))
8232                         return PTR_ERR(priv->lms_base);
8233         } else {
8234                 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
8235                 priv->iface_base = devm_ioremap_resource(&pdev->dev, res);
8236                 if (IS_ERR(priv->iface_base))
8237                         return PTR_ERR(priv->iface_base);
8238
8239                 priv->sysctrl_base =
8240                         syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
8241                                                         "marvell,system-controller");
8242                 if (IS_ERR(priv->sysctrl_base))
8243                         /* The system controller regmap is optional for dt
8244                          * compatibility reasons. When not provided, the
8245                          * configuration of the GoP relies on the
8246                          * firmware/bootloader.
8247                          */
8248                         priv->sysctrl_base = NULL;
8249         }
8250
8251         for (i = 0; i < MVPP2_MAX_THREADS; i++) {
8252                 u32 addr_space_sz;
8253
8254                 addr_space_sz = (priv->hw_version == MVPP21 ?
8255                                  MVPP21_ADDR_SPACE_SZ : MVPP22_ADDR_SPACE_SZ);
8256                 priv->swth_base[i] = base + i * addr_space_sz;
8257         }
8258
8259         if (priv->hw_version == MVPP21)
8260                 priv->max_port_rxqs = 8;
8261         else
8262                 priv->max_port_rxqs = 32;
8263
8264         priv->pp_clk = devm_clk_get(&pdev->dev, "pp_clk");
8265         if (IS_ERR(priv->pp_clk))
8266                 return PTR_ERR(priv->pp_clk);
8267         err = clk_prepare_enable(priv->pp_clk);
8268         if (err < 0)
8269                 return err;
8270
8271         priv->gop_clk = devm_clk_get(&pdev->dev, "gop_clk");
8272         if (IS_ERR(priv->gop_clk)) {
8273                 err = PTR_ERR(priv->gop_clk);
8274                 goto err_pp_clk;
8275         }
8276         err = clk_prepare_enable(priv->gop_clk);
8277         if (err < 0)
8278                 goto err_pp_clk;
8279
8280         if (priv->hw_version == MVPP22) {
8281                 priv->mg_clk = devm_clk_get(&pdev->dev, "mg_clk");
8282                 if (IS_ERR(priv->mg_clk)) {
8283                         err = PTR_ERR(priv->mg_clk);
8284                         goto err_gop_clk;
8285                 }
8286
8287                 err = clk_prepare_enable(priv->mg_clk);
8288                 if (err < 0)
8289                         goto err_gop_clk;
8290
8291                 priv->axi_clk = devm_clk_get(&pdev->dev, "axi_clk");
8292                 if (IS_ERR(priv->axi_clk)) {
8293                         err = PTR_ERR(priv->axi_clk);
8294                         if (err == -EPROBE_DEFER)
8295                                 goto err_gop_clk;
8296                         priv->axi_clk = NULL;
8297                 } else {
8298                         err = clk_prepare_enable(priv->axi_clk);
8299                         if (err < 0)
8300                                 goto err_gop_clk;
8301                 }
8302         }
8303
8304         /* Get system's tclk rate */
8305         priv->tclk = clk_get_rate(priv->pp_clk);
8306
8307         if (priv->hw_version == MVPP22) {
8308                 err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(40));
8309                 if (err)
8310                         goto err_mg_clk;
8311                 /* Sadly, the BM pools all share the same register to
8312                  * store the high 32 bits of their address. So they
8313                  * must all have the same high 32 bits, which forces
8314                  * us to restrict coherent memory to DMA_BIT_MASK(32).
8315                  */
8316                 err = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8317                 if (err)
8318                         goto err_mg_clk;
8319         }
8320
8321         /* Initialize network controller */
8322         err = mvpp2_init(pdev, priv);
8323         if (err < 0) {
8324                 dev_err(&pdev->dev, "failed to initialize controller\n");
8325                 goto err_mg_clk;
8326         }
8327
8328         priv->port_count = of_get_available_child_count(dn);
8329         if (priv->port_count == 0) {
8330                 dev_err(&pdev->dev, "no ports enabled\n");
8331                 err = -ENODEV;
8332                 goto err_mg_clk;
8333         }
8334
8335         priv->port_list = devm_kcalloc(&pdev->dev, priv->port_count,
8336                                        sizeof(*priv->port_list),
8337                                        GFP_KERNEL);
8338         if (!priv->port_list) {
8339                 err = -ENOMEM;
8340                 goto err_mg_clk;
8341         }
8342
8343         /* Initialize ports */
8344         i = 0;
8345         for_each_available_child_of_node(dn, port_node) {
8346                 err = mvpp2_port_probe(pdev, port_node, priv, i);
8347                 if (err < 0)
8348                         goto err_mg_clk;
8349                 i++;
8350         }
8351
8352         /* Statistics must be gathered regularly because some of them (like
8353          * packets counters) are 32-bit registers and could overflow quite
8354          * quickly. For instance, a 10Gb link used at full bandwidth with the
8355          * smallest packets (64B) will overflow a 32-bit counter in less than
8356          * 30 seconds. Then, use a workqueue to fill 64-bit counters.
8357          */
8358         snprintf(priv->queue_name, sizeof(priv->queue_name),
8359                  "stats-wq-%s%s", netdev_name(priv->port_list[0]->dev),
8360                  priv->port_count > 1 ? "+" : "");
8361         priv->stats_queue = create_singlethread_workqueue(priv->queue_name);
8362         if (!priv->stats_queue) {
8363                 err = -ENOMEM;
8364                 goto err_mg_clk;
8365         }
8366
8367         platform_set_drvdata(pdev, priv);
8368         return 0;
8369
8370 err_mg_clk:
8371         clk_disable_unprepare(priv->axi_clk);
8372         if (priv->hw_version == MVPP22)
8373                 clk_disable_unprepare(priv->mg_clk);
8374 err_gop_clk:
8375         clk_disable_unprepare(priv->gop_clk);
8376 err_pp_clk:
8377         clk_disable_unprepare(priv->pp_clk);
8378         return err;
8379 }
8380
8381 static int mvpp2_remove(struct platform_device *pdev)
8382 {
8383         struct mvpp2 *priv = platform_get_drvdata(pdev);
8384         struct device_node *dn = pdev->dev.of_node;
8385         struct device_node *port_node;
8386         int i = 0;
8387
8388         flush_workqueue(priv->stats_queue);
8389         destroy_workqueue(priv->stats_queue);
8390
8391         for_each_available_child_of_node(dn, port_node) {
8392                 if (priv->port_list[i]) {
8393                         mutex_destroy(&priv->port_list[i]->gather_stats_lock);
8394                         mvpp2_port_remove(priv->port_list[i]);
8395                 }
8396                 i++;
8397         }
8398
8399         for (i = 0; i < MVPP2_BM_POOLS_NUM; i++) {
8400                 struct mvpp2_bm_pool *bm_pool = &priv->bm_pools[i];
8401
8402                 mvpp2_bm_pool_destroy(pdev, priv, bm_pool);
8403         }
8404
8405         for_each_present_cpu(i) {
8406                 struct mvpp2_tx_queue *aggr_txq = &priv->aggr_txqs[i];
8407
8408                 dma_free_coherent(&pdev->dev,
8409                                   MVPP2_AGGR_TXQ_SIZE * MVPP2_DESC_ALIGNED_SIZE,
8410                                   aggr_txq->descs,
8411                                   aggr_txq->descs_dma);
8412         }
8413
8414         clk_disable_unprepare(priv->axi_clk);
8415         clk_disable_unprepare(priv->mg_clk);
8416         clk_disable_unprepare(priv->pp_clk);
8417         clk_disable_unprepare(priv->gop_clk);
8418
8419         return 0;
8420 }
8421
8422 static const struct of_device_id mvpp2_match[] = {
8423         {
8424                 .compatible = "marvell,armada-375-pp2",
8425                 .data = (void *)MVPP21,
8426         },
8427         {
8428                 .compatible = "marvell,armada-7k-pp22",
8429                 .data = (void *)MVPP22,
8430         },
8431         { }
8432 };
8433 MODULE_DEVICE_TABLE(of, mvpp2_match);
8434
8435 static struct platform_driver mvpp2_driver = {
8436         .probe = mvpp2_probe,
8437         .remove = mvpp2_remove,
8438         .driver = {
8439                 .name = MVPP2_DRIVER_NAME,
8440                 .of_match_table = mvpp2_match,
8441         },
8442 };
8443
8444 module_platform_driver(mvpp2_driver);
8445
8446 MODULE_DESCRIPTION("Marvell PPv2 Ethernet Driver - www.marvell.com");
8447 MODULE_AUTHOR("Marcin Wojtas <mw@semihalf.com>");
8448 MODULE_LICENSE("GPL v2");