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