xref: /linux/drivers/net/dsa/ocelot/felix_vsc9959.c (revision 57f273adbcd44172cbe0bd10b8b7408dd255699f)
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Copyright 2017 Microsemi Corporation
3  * Copyright 2018-2019 NXP
4  */
5 #include <linux/fsl/enetc_mdio.h>
6 #include <soc/mscc/ocelot_qsys.h>
7 #include <soc/mscc/ocelot_vcap.h>
8 #include <soc/mscc/ocelot_ana.h>
9 #include <soc/mscc/ocelot_ptp.h>
10 #include <soc/mscc/ocelot_sys.h>
11 #include <net/tc_act/tc_gate.h>
12 #include <soc/mscc/ocelot.h>
13 #include <linux/dsa/ocelot.h>
14 #include <linux/pcs-lynx.h>
15 #include <net/pkt_sched.h>
16 #include <linux/iopoll.h>
17 #include <linux/mdio.h>
18 #include <linux/pci.h>
19 #include <linux/time.h>
20 #include "felix.h"
21 
22 #define VSC9959_NUM_PORTS		6
23 
24 #define VSC9959_TAS_GCL_ENTRY_MAX	63
25 #define VSC9959_TAS_MIN_GATE_LEN_NS	33
26 #define VSC9959_VCAP_POLICER_BASE	63
27 #define VSC9959_VCAP_POLICER_MAX	383
28 #define VSC9959_SWITCH_PCI_BAR		4
29 #define VSC9959_IMDIO_PCI_BAR		0
30 
31 #define VSC9959_PORT_MODE_SERDES	(OCELOT_PORT_MODE_SGMII | \
32 					 OCELOT_PORT_MODE_QSGMII | \
33 					 OCELOT_PORT_MODE_1000BASEX | \
34 					 OCELOT_PORT_MODE_2500BASEX | \
35 					 OCELOT_PORT_MODE_USXGMII)
36 
37 static const u32 vsc9959_port_modes[VSC9959_NUM_PORTS] = {
38 	VSC9959_PORT_MODE_SERDES,
39 	VSC9959_PORT_MODE_SERDES,
40 	VSC9959_PORT_MODE_SERDES,
41 	VSC9959_PORT_MODE_SERDES,
42 	OCELOT_PORT_MODE_INTERNAL,
43 	OCELOT_PORT_MODE_INTERNAL,
44 };
45 
46 static const u32 vsc9959_ana_regmap[] = {
47 	REG(ANA_ADVLEARN,			0x0089a0),
48 	REG(ANA_VLANMASK,			0x0089a4),
49 	REG_RESERVED(ANA_PORT_B_DOMAIN),
50 	REG(ANA_ANAGEFIL,			0x0089ac),
51 	REG(ANA_ANEVENTS,			0x0089b0),
52 	REG(ANA_STORMLIMIT_BURST,		0x0089b4),
53 	REG(ANA_STORMLIMIT_CFG,			0x0089b8),
54 	REG(ANA_ISOLATED_PORTS,			0x0089c8),
55 	REG(ANA_COMMUNITY_PORTS,		0x0089cc),
56 	REG(ANA_AUTOAGE,			0x0089d0),
57 	REG(ANA_MACTOPTIONS,			0x0089d4),
58 	REG(ANA_LEARNDISC,			0x0089d8),
59 	REG(ANA_AGENCTRL,			0x0089dc),
60 	REG(ANA_MIRRORPORTS,			0x0089e0),
61 	REG(ANA_EMIRRORPORTS,			0x0089e4),
62 	REG(ANA_FLOODING,			0x0089e8),
63 	REG(ANA_FLOODING_IPMC,			0x008a08),
64 	REG(ANA_SFLOW_CFG,			0x008a0c),
65 	REG(ANA_PORT_MODE,			0x008a28),
66 	REG(ANA_CUT_THRU_CFG,			0x008a48),
67 	REG(ANA_PGID_PGID,			0x008400),
68 	REG(ANA_TABLES_ANMOVED,			0x007f1c),
69 	REG(ANA_TABLES_MACHDATA,		0x007f20),
70 	REG(ANA_TABLES_MACLDATA,		0x007f24),
71 	REG(ANA_TABLES_STREAMDATA,		0x007f28),
72 	REG(ANA_TABLES_MACACCESS,		0x007f2c),
73 	REG(ANA_TABLES_MACTINDX,		0x007f30),
74 	REG(ANA_TABLES_VLANACCESS,		0x007f34),
75 	REG(ANA_TABLES_VLANTIDX,		0x007f38),
76 	REG(ANA_TABLES_ISDXACCESS,		0x007f3c),
77 	REG(ANA_TABLES_ISDXTIDX,		0x007f40),
78 	REG(ANA_TABLES_ENTRYLIM,		0x007f00),
79 	REG(ANA_TABLES_PTP_ID_HIGH,		0x007f44),
80 	REG(ANA_TABLES_PTP_ID_LOW,		0x007f48),
81 	REG(ANA_TABLES_STREAMACCESS,		0x007f4c),
82 	REG(ANA_TABLES_STREAMTIDX,		0x007f50),
83 	REG(ANA_TABLES_SEQ_HISTORY,		0x007f54),
84 	REG(ANA_TABLES_SEQ_MASK,		0x007f58),
85 	REG(ANA_TABLES_SFID_MASK,		0x007f5c),
86 	REG(ANA_TABLES_SFIDACCESS,		0x007f60),
87 	REG(ANA_TABLES_SFIDTIDX,		0x007f64),
88 	REG(ANA_MSTI_STATE,			0x008600),
89 	REG(ANA_OAM_UPM_LM_CNT,			0x008000),
90 	REG(ANA_SG_ACCESS_CTRL,			0x008a64),
91 	REG(ANA_SG_CONFIG_REG_1,		0x007fb0),
92 	REG(ANA_SG_CONFIG_REG_2,		0x007fb4),
93 	REG(ANA_SG_CONFIG_REG_3,		0x007fb8),
94 	REG(ANA_SG_CONFIG_REG_4,		0x007fbc),
95 	REG(ANA_SG_CONFIG_REG_5,		0x007fc0),
96 	REG(ANA_SG_GCL_GS_CONFIG,		0x007f80),
97 	REG(ANA_SG_GCL_TI_CONFIG,		0x007f90),
98 	REG(ANA_SG_STATUS_REG_1,		0x008980),
99 	REG(ANA_SG_STATUS_REG_2,		0x008984),
100 	REG(ANA_SG_STATUS_REG_3,		0x008988),
101 	REG(ANA_PORT_VLAN_CFG,			0x007800),
102 	REG(ANA_PORT_DROP_CFG,			0x007804),
103 	REG(ANA_PORT_QOS_CFG,			0x007808),
104 	REG(ANA_PORT_VCAP_CFG,			0x00780c),
105 	REG(ANA_PORT_VCAP_S1_KEY_CFG,		0x007810),
106 	REG(ANA_PORT_VCAP_S2_CFG,		0x00781c),
107 	REG(ANA_PORT_PCP_DEI_MAP,		0x007820),
108 	REG(ANA_PORT_CPU_FWD_CFG,		0x007860),
109 	REG(ANA_PORT_CPU_FWD_BPDU_CFG,		0x007864),
110 	REG(ANA_PORT_CPU_FWD_GARP_CFG,		0x007868),
111 	REG(ANA_PORT_CPU_FWD_CCM_CFG,		0x00786c),
112 	REG(ANA_PORT_PORT_CFG,			0x007870),
113 	REG(ANA_PORT_POL_CFG,			0x007874),
114 	REG(ANA_PORT_PTP_CFG,			0x007878),
115 	REG(ANA_PORT_PTP_DLY1_CFG,		0x00787c),
116 	REG(ANA_PORT_PTP_DLY2_CFG,		0x007880),
117 	REG(ANA_PORT_SFID_CFG,			0x007884),
118 	REG(ANA_PFC_PFC_CFG,			0x008800),
119 	REG_RESERVED(ANA_PFC_PFC_TIMER),
120 	REG_RESERVED(ANA_IPT_OAM_MEP_CFG),
121 	REG_RESERVED(ANA_IPT_IPT),
122 	REG_RESERVED(ANA_PPT_PPT),
123 	REG_RESERVED(ANA_FID_MAP_FID_MAP),
124 	REG(ANA_AGGR_CFG,			0x008a68),
125 	REG(ANA_CPUQ_CFG,			0x008a6c),
126 	REG_RESERVED(ANA_CPUQ_CFG2),
127 	REG(ANA_CPUQ_8021_CFG,			0x008a74),
128 	REG(ANA_DSCP_CFG,			0x008ab4),
129 	REG(ANA_DSCP_REWR_CFG,			0x008bb4),
130 	REG(ANA_VCAP_RNG_TYPE_CFG,		0x008bf4),
131 	REG(ANA_VCAP_RNG_VAL_CFG,		0x008c14),
132 	REG_RESERVED(ANA_VRAP_CFG),
133 	REG_RESERVED(ANA_VRAP_HDR_DATA),
134 	REG_RESERVED(ANA_VRAP_HDR_MASK),
135 	REG(ANA_DISCARD_CFG,			0x008c40),
136 	REG(ANA_FID_CFG,			0x008c44),
137 	REG(ANA_POL_PIR_CFG,			0x004000),
138 	REG(ANA_POL_CIR_CFG,			0x004004),
139 	REG(ANA_POL_MODE_CFG,			0x004008),
140 	REG(ANA_POL_PIR_STATE,			0x00400c),
141 	REG(ANA_POL_CIR_STATE,			0x004010),
142 	REG_RESERVED(ANA_POL_STATE),
143 	REG(ANA_POL_FLOWC,			0x008c48),
144 	REG(ANA_POL_HYST,			0x008cb4),
145 	REG_RESERVED(ANA_POL_MISC_CFG),
146 };
147 
148 static const u32 vsc9959_qs_regmap[] = {
149 	REG(QS_XTR_GRP_CFG,			0x000000),
150 	REG(QS_XTR_RD,				0x000008),
151 	REG(QS_XTR_FRM_PRUNING,			0x000010),
152 	REG(QS_XTR_FLUSH,			0x000018),
153 	REG(QS_XTR_DATA_PRESENT,		0x00001c),
154 	REG(QS_XTR_CFG,				0x000020),
155 	REG(QS_INJ_GRP_CFG,			0x000024),
156 	REG(QS_INJ_WR,				0x00002c),
157 	REG(QS_INJ_CTRL,			0x000034),
158 	REG(QS_INJ_STATUS,			0x00003c),
159 	REG(QS_INJ_ERR,				0x000040),
160 	REG_RESERVED(QS_INH_DBG),
161 };
162 
163 static const u32 vsc9959_vcap_regmap[] = {
164 	/* VCAP_CORE_CFG */
165 	REG(VCAP_CORE_UPDATE_CTRL,		0x000000),
166 	REG(VCAP_CORE_MV_CFG,			0x000004),
167 	/* VCAP_CORE_CACHE */
168 	REG(VCAP_CACHE_ENTRY_DAT,		0x000008),
169 	REG(VCAP_CACHE_MASK_DAT,		0x000108),
170 	REG(VCAP_CACHE_ACTION_DAT,		0x000208),
171 	REG(VCAP_CACHE_CNT_DAT,			0x000308),
172 	REG(VCAP_CACHE_TG_DAT,			0x000388),
173 	/* VCAP_CONST */
174 	REG(VCAP_CONST_VCAP_VER,		0x000398),
175 	REG(VCAP_CONST_ENTRY_WIDTH,		0x00039c),
176 	REG(VCAP_CONST_ENTRY_CNT,		0x0003a0),
177 	REG(VCAP_CONST_ENTRY_SWCNT,		0x0003a4),
178 	REG(VCAP_CONST_ENTRY_TG_WIDTH,		0x0003a8),
179 	REG(VCAP_CONST_ACTION_DEF_CNT,		0x0003ac),
180 	REG(VCAP_CONST_ACTION_WIDTH,		0x0003b0),
181 	REG(VCAP_CONST_CNT_WIDTH,		0x0003b4),
182 	REG(VCAP_CONST_CORE_CNT,		0x0003b8),
183 	REG(VCAP_CONST_IF_CNT,			0x0003bc),
184 };
185 
186 static const u32 vsc9959_qsys_regmap[] = {
187 	REG(QSYS_PORT_MODE,			0x00f460),
188 	REG(QSYS_SWITCH_PORT_MODE,		0x00f480),
189 	REG(QSYS_STAT_CNT_CFG,			0x00f49c),
190 	REG(QSYS_EEE_CFG,			0x00f4a0),
191 	REG(QSYS_EEE_THRES,			0x00f4b8),
192 	REG(QSYS_IGR_NO_SHARING,		0x00f4bc),
193 	REG(QSYS_EGR_NO_SHARING,		0x00f4c0),
194 	REG(QSYS_SW_STATUS,			0x00f4c4),
195 	REG(QSYS_EXT_CPU_CFG,			0x00f4e0),
196 	REG_RESERVED(QSYS_PAD_CFG),
197 	REG(QSYS_CPU_GROUP_MAP,			0x00f4e8),
198 	REG_RESERVED(QSYS_QMAP),
199 	REG_RESERVED(QSYS_ISDX_SGRP),
200 	REG_RESERVED(QSYS_TIMED_FRAME_ENTRY),
201 	REG(QSYS_TFRM_MISC,			0x00f50c),
202 	REG(QSYS_TFRM_PORT_DLY,			0x00f510),
203 	REG(QSYS_TFRM_TIMER_CFG_1,		0x00f514),
204 	REG(QSYS_TFRM_TIMER_CFG_2,		0x00f518),
205 	REG(QSYS_TFRM_TIMER_CFG_3,		0x00f51c),
206 	REG(QSYS_TFRM_TIMER_CFG_4,		0x00f520),
207 	REG(QSYS_TFRM_TIMER_CFG_5,		0x00f524),
208 	REG(QSYS_TFRM_TIMER_CFG_6,		0x00f528),
209 	REG(QSYS_TFRM_TIMER_CFG_7,		0x00f52c),
210 	REG(QSYS_TFRM_TIMER_CFG_8,		0x00f530),
211 	REG(QSYS_RED_PROFILE,			0x00f534),
212 	REG(QSYS_RES_QOS_MODE,			0x00f574),
213 	REG(QSYS_RES_CFG,			0x00c000),
214 	REG(QSYS_RES_STAT,			0x00c004),
215 	REG(QSYS_EGR_DROP_MODE,			0x00f578),
216 	REG(QSYS_EQ_CTRL,			0x00f57c),
217 	REG_RESERVED(QSYS_EVENTS_CORE),
218 	REG(QSYS_QMAXSDU_CFG_0,			0x00f584),
219 	REG(QSYS_QMAXSDU_CFG_1,			0x00f5a0),
220 	REG(QSYS_QMAXSDU_CFG_2,			0x00f5bc),
221 	REG(QSYS_QMAXSDU_CFG_3,			0x00f5d8),
222 	REG(QSYS_QMAXSDU_CFG_4,			0x00f5f4),
223 	REG(QSYS_QMAXSDU_CFG_5,			0x00f610),
224 	REG(QSYS_QMAXSDU_CFG_6,			0x00f62c),
225 	REG(QSYS_QMAXSDU_CFG_7,			0x00f648),
226 	REG(QSYS_PREEMPTION_CFG,		0x00f664),
227 	REG(QSYS_CIR_CFG,			0x000000),
228 	REG(QSYS_EIR_CFG,			0x000004),
229 	REG(QSYS_SE_CFG,			0x000008),
230 	REG(QSYS_SE_DWRR_CFG,			0x00000c),
231 	REG_RESERVED(QSYS_SE_CONNECT),
232 	REG(QSYS_SE_DLB_SENSE,			0x000040),
233 	REG(QSYS_CIR_STATE,			0x000044),
234 	REG(QSYS_EIR_STATE,			0x000048),
235 	REG_RESERVED(QSYS_SE_STATE),
236 	REG(QSYS_HSCH_MISC_CFG,			0x00f67c),
237 	REG(QSYS_TAG_CONFIG,			0x00f680),
238 	REG(QSYS_TAS_PARAM_CFG_CTRL,		0x00f698),
239 	REG(QSYS_PORT_MAX_SDU,			0x00f69c),
240 	REG(QSYS_PARAM_CFG_REG_1,		0x00f440),
241 	REG(QSYS_PARAM_CFG_REG_2,		0x00f444),
242 	REG(QSYS_PARAM_CFG_REG_3,		0x00f448),
243 	REG(QSYS_PARAM_CFG_REG_4,		0x00f44c),
244 	REG(QSYS_PARAM_CFG_REG_5,		0x00f450),
245 	REG(QSYS_GCL_CFG_REG_1,			0x00f454),
246 	REG(QSYS_GCL_CFG_REG_2,			0x00f458),
247 	REG(QSYS_PARAM_STATUS_REG_1,		0x00f400),
248 	REG(QSYS_PARAM_STATUS_REG_2,		0x00f404),
249 	REG(QSYS_PARAM_STATUS_REG_3,		0x00f408),
250 	REG(QSYS_PARAM_STATUS_REG_4,		0x00f40c),
251 	REG(QSYS_PARAM_STATUS_REG_5,		0x00f410),
252 	REG(QSYS_PARAM_STATUS_REG_6,		0x00f414),
253 	REG(QSYS_PARAM_STATUS_REG_7,		0x00f418),
254 	REG(QSYS_PARAM_STATUS_REG_8,		0x00f41c),
255 	REG(QSYS_PARAM_STATUS_REG_9,		0x00f420),
256 	REG(QSYS_GCL_STATUS_REG_1,		0x00f424),
257 	REG(QSYS_GCL_STATUS_REG_2,		0x00f428),
258 };
259 
260 static const u32 vsc9959_rew_regmap[] = {
261 	REG(REW_PORT_VLAN_CFG,			0x000000),
262 	REG(REW_TAG_CFG,			0x000004),
263 	REG(REW_PORT_CFG,			0x000008),
264 	REG(REW_DSCP_CFG,			0x00000c),
265 	REG(REW_PCP_DEI_QOS_MAP_CFG,		0x000010),
266 	REG(REW_PTP_CFG,			0x000050),
267 	REG(REW_PTP_DLY1_CFG,			0x000054),
268 	REG(REW_RED_TAG_CFG,			0x000058),
269 	REG(REW_DSCP_REMAP_DP1_CFG,		0x000410),
270 	REG(REW_DSCP_REMAP_CFG,			0x000510),
271 	REG_RESERVED(REW_STAT_CFG),
272 	REG_RESERVED(REW_REW_STICKY),
273 	REG_RESERVED(REW_PPT),
274 };
275 
276 static const u32 vsc9959_sys_regmap[] = {
277 	REG(SYS_COUNT_RX_OCTETS,		0x000000),
278 	REG(SYS_COUNT_RX_UNICAST,		0x000004),
279 	REG(SYS_COUNT_RX_MULTICAST,		0x000008),
280 	REG(SYS_COUNT_RX_BROADCAST,		0x00000c),
281 	REG(SYS_COUNT_RX_SHORTS,		0x000010),
282 	REG(SYS_COUNT_RX_FRAGMENTS,		0x000014),
283 	REG(SYS_COUNT_RX_JABBERS,		0x000018),
284 	REG(SYS_COUNT_RX_CRC_ALIGN_ERRS,	0x00001c),
285 	REG(SYS_COUNT_RX_SYM_ERRS,		0x000020),
286 	REG(SYS_COUNT_RX_64,			0x000024),
287 	REG(SYS_COUNT_RX_65_127,		0x000028),
288 	REG(SYS_COUNT_RX_128_255,		0x00002c),
289 	REG(SYS_COUNT_RX_256_511,		0x000030),
290 	REG(SYS_COUNT_RX_512_1023,		0x000034),
291 	REG(SYS_COUNT_RX_1024_1526,		0x000038),
292 	REG(SYS_COUNT_RX_1527_MAX,		0x00003c),
293 	REG(SYS_COUNT_RX_PAUSE,			0x000040),
294 	REG(SYS_COUNT_RX_CONTROL,		0x000044),
295 	REG(SYS_COUNT_RX_LONGS,			0x000048),
296 	REG(SYS_COUNT_RX_CLASSIFIED_DROPS,	0x00004c),
297 	REG(SYS_COUNT_RX_RED_PRIO_0,		0x000050),
298 	REG(SYS_COUNT_RX_RED_PRIO_1,		0x000054),
299 	REG(SYS_COUNT_RX_RED_PRIO_2,		0x000058),
300 	REG(SYS_COUNT_RX_RED_PRIO_3,		0x00005c),
301 	REG(SYS_COUNT_RX_RED_PRIO_4,		0x000060),
302 	REG(SYS_COUNT_RX_RED_PRIO_5,		0x000064),
303 	REG(SYS_COUNT_RX_RED_PRIO_6,		0x000068),
304 	REG(SYS_COUNT_RX_RED_PRIO_7,		0x00006c),
305 	REG(SYS_COUNT_RX_YELLOW_PRIO_0,		0x000070),
306 	REG(SYS_COUNT_RX_YELLOW_PRIO_1,		0x000074),
307 	REG(SYS_COUNT_RX_YELLOW_PRIO_2,		0x000078),
308 	REG(SYS_COUNT_RX_YELLOW_PRIO_3,		0x00007c),
309 	REG(SYS_COUNT_RX_YELLOW_PRIO_4,		0x000080),
310 	REG(SYS_COUNT_RX_YELLOW_PRIO_5,		0x000084),
311 	REG(SYS_COUNT_RX_YELLOW_PRIO_6,		0x000088),
312 	REG(SYS_COUNT_RX_YELLOW_PRIO_7,		0x00008c),
313 	REG(SYS_COUNT_RX_GREEN_PRIO_0,		0x000090),
314 	REG(SYS_COUNT_RX_GREEN_PRIO_1,		0x000094),
315 	REG(SYS_COUNT_RX_GREEN_PRIO_2,		0x000098),
316 	REG(SYS_COUNT_RX_GREEN_PRIO_3,		0x00009c),
317 	REG(SYS_COUNT_RX_GREEN_PRIO_4,		0x0000a0),
318 	REG(SYS_COUNT_RX_GREEN_PRIO_5,		0x0000a4),
319 	REG(SYS_COUNT_RX_GREEN_PRIO_6,		0x0000a8),
320 	REG(SYS_COUNT_RX_GREEN_PRIO_7,		0x0000ac),
321 	REG(SYS_COUNT_TX_OCTETS,		0x000200),
322 	REG(SYS_COUNT_TX_UNICAST,		0x000204),
323 	REG(SYS_COUNT_TX_MULTICAST,		0x000208),
324 	REG(SYS_COUNT_TX_BROADCAST,		0x00020c),
325 	REG(SYS_COUNT_TX_COLLISION,		0x000210),
326 	REG(SYS_COUNT_TX_DROPS,			0x000214),
327 	REG(SYS_COUNT_TX_PAUSE,			0x000218),
328 	REG(SYS_COUNT_TX_64,			0x00021c),
329 	REG(SYS_COUNT_TX_65_127,		0x000220),
330 	REG(SYS_COUNT_TX_128_255,		0x000224),
331 	REG(SYS_COUNT_TX_256_511,		0x000228),
332 	REG(SYS_COUNT_TX_512_1023,		0x00022c),
333 	REG(SYS_COUNT_TX_1024_1526,		0x000230),
334 	REG(SYS_COUNT_TX_1527_MAX,		0x000234),
335 	REG(SYS_COUNT_TX_YELLOW_PRIO_0,		0x000238),
336 	REG(SYS_COUNT_TX_YELLOW_PRIO_1,		0x00023c),
337 	REG(SYS_COUNT_TX_YELLOW_PRIO_2,		0x000240),
338 	REG(SYS_COUNT_TX_YELLOW_PRIO_3,		0x000244),
339 	REG(SYS_COUNT_TX_YELLOW_PRIO_4,		0x000248),
340 	REG(SYS_COUNT_TX_YELLOW_PRIO_5,		0x00024c),
341 	REG(SYS_COUNT_TX_YELLOW_PRIO_6,		0x000250),
342 	REG(SYS_COUNT_TX_YELLOW_PRIO_7,		0x000254),
343 	REG(SYS_COUNT_TX_GREEN_PRIO_0,		0x000258),
344 	REG(SYS_COUNT_TX_GREEN_PRIO_1,		0x00025c),
345 	REG(SYS_COUNT_TX_GREEN_PRIO_2,		0x000260),
346 	REG(SYS_COUNT_TX_GREEN_PRIO_3,		0x000264),
347 	REG(SYS_COUNT_TX_GREEN_PRIO_4,		0x000268),
348 	REG(SYS_COUNT_TX_GREEN_PRIO_5,		0x00026c),
349 	REG(SYS_COUNT_TX_GREEN_PRIO_6,		0x000270),
350 	REG(SYS_COUNT_TX_GREEN_PRIO_7,		0x000274),
351 	REG(SYS_COUNT_TX_AGED,			0x000278),
352 	REG(SYS_COUNT_DROP_LOCAL,		0x000400),
353 	REG(SYS_COUNT_DROP_TAIL,		0x000404),
354 	REG(SYS_COUNT_DROP_YELLOW_PRIO_0,	0x000408),
355 	REG(SYS_COUNT_DROP_YELLOW_PRIO_1,	0x00040c),
356 	REG(SYS_COUNT_DROP_YELLOW_PRIO_2,	0x000410),
357 	REG(SYS_COUNT_DROP_YELLOW_PRIO_3,	0x000414),
358 	REG(SYS_COUNT_DROP_YELLOW_PRIO_4,	0x000418),
359 	REG(SYS_COUNT_DROP_YELLOW_PRIO_5,	0x00041c),
360 	REG(SYS_COUNT_DROP_YELLOW_PRIO_6,	0x000420),
361 	REG(SYS_COUNT_DROP_YELLOW_PRIO_7,	0x000424),
362 	REG(SYS_COUNT_DROP_GREEN_PRIO_0,	0x000428),
363 	REG(SYS_COUNT_DROP_GREEN_PRIO_1,	0x00042c),
364 	REG(SYS_COUNT_DROP_GREEN_PRIO_2,	0x000430),
365 	REG(SYS_COUNT_DROP_GREEN_PRIO_3,	0x000434),
366 	REG(SYS_COUNT_DROP_GREEN_PRIO_4,	0x000438),
367 	REG(SYS_COUNT_DROP_GREEN_PRIO_5,	0x00043c),
368 	REG(SYS_COUNT_DROP_GREEN_PRIO_6,	0x000440),
369 	REG(SYS_COUNT_DROP_GREEN_PRIO_7,	0x000444),
370 	REG(SYS_COUNT_SF_MATCHING_FRAMES,	0x000800),
371 	REG(SYS_COUNT_SF_NOT_PASSING_FRAMES,	0x000804),
372 	REG(SYS_COUNT_SF_NOT_PASSING_SDU,	0x000808),
373 	REG(SYS_COUNT_SF_RED_FRAMES,		0x00080c),
374 	REG(SYS_RESET_CFG,			0x000e00),
375 	REG(SYS_SR_ETYPE_CFG,			0x000e04),
376 	REG(SYS_VLAN_ETYPE_CFG,			0x000e08),
377 	REG(SYS_PORT_MODE,			0x000e0c),
378 	REG(SYS_FRONT_PORT_MODE,		0x000e2c),
379 	REG(SYS_FRM_AGING,			0x000e44),
380 	REG(SYS_STAT_CFG,			0x000e48),
381 	REG(SYS_SW_STATUS,			0x000e4c),
382 	REG_RESERVED(SYS_MISC_CFG),
383 	REG(SYS_REW_MAC_HIGH_CFG,		0x000e6c),
384 	REG(SYS_REW_MAC_LOW_CFG,		0x000e84),
385 	REG(SYS_TIMESTAMP_OFFSET,		0x000e9c),
386 	REG(SYS_PAUSE_CFG,			0x000ea0),
387 	REG(SYS_PAUSE_TOT_CFG,			0x000ebc),
388 	REG(SYS_ATOP,				0x000ec0),
389 	REG(SYS_ATOP_TOT_CFG,			0x000edc),
390 	REG(SYS_MAC_FC_CFG,			0x000ee0),
391 	REG(SYS_MMGT,				0x000ef8),
392 	REG_RESERVED(SYS_MMGT_FAST),
393 	REG_RESERVED(SYS_EVENTS_DIF),
394 	REG_RESERVED(SYS_EVENTS_CORE),
395 	REG(SYS_PTP_STATUS,			0x000f14),
396 	REG(SYS_PTP_TXSTAMP,			0x000f18),
397 	REG(SYS_PTP_NXT,			0x000f1c),
398 	REG(SYS_PTP_CFG,			0x000f20),
399 	REG(SYS_RAM_INIT,			0x000f24),
400 	REG_RESERVED(SYS_CM_ADDR),
401 	REG_RESERVED(SYS_CM_DATA_WR),
402 	REG_RESERVED(SYS_CM_DATA_RD),
403 	REG_RESERVED(SYS_CM_OP),
404 	REG_RESERVED(SYS_CM_DATA),
405 };
406 
407 static const u32 vsc9959_ptp_regmap[] = {
408 	REG(PTP_PIN_CFG,			0x000000),
409 	REG(PTP_PIN_TOD_SEC_MSB,		0x000004),
410 	REG(PTP_PIN_TOD_SEC_LSB,		0x000008),
411 	REG(PTP_PIN_TOD_NSEC,			0x00000c),
412 	REG(PTP_PIN_WF_HIGH_PERIOD,		0x000014),
413 	REG(PTP_PIN_WF_LOW_PERIOD,		0x000018),
414 	REG(PTP_CFG_MISC,			0x0000a0),
415 	REG(PTP_CLK_CFG_ADJ_CFG,		0x0000a4),
416 	REG(PTP_CLK_CFG_ADJ_FREQ,		0x0000a8),
417 };
418 
419 static const u32 vsc9959_gcb_regmap[] = {
420 	REG(GCB_SOFT_RST,			0x000004),
421 };
422 
423 static const u32 vsc9959_dev_gmii_regmap[] = {
424 	REG(DEV_CLOCK_CFG,			0x0),
425 	REG(DEV_PORT_MISC,			0x4),
426 	REG(DEV_EVENTS,				0x8),
427 	REG(DEV_EEE_CFG,			0xc),
428 	REG(DEV_RX_PATH_DELAY,			0x10),
429 	REG(DEV_TX_PATH_DELAY,			0x14),
430 	REG(DEV_PTP_PREDICT_CFG,		0x18),
431 	REG(DEV_MAC_ENA_CFG,			0x1c),
432 	REG(DEV_MAC_MODE_CFG,			0x20),
433 	REG(DEV_MAC_MAXLEN_CFG,			0x24),
434 	REG(DEV_MAC_TAGS_CFG,			0x28),
435 	REG(DEV_MAC_ADV_CHK_CFG,		0x2c),
436 	REG(DEV_MAC_IFG_CFG,			0x30),
437 	REG(DEV_MAC_HDX_CFG,			0x34),
438 	REG(DEV_MAC_DBG_CFG,			0x38),
439 	REG(DEV_MAC_FC_MAC_LOW_CFG,		0x3c),
440 	REG(DEV_MAC_FC_MAC_HIGH_CFG,		0x40),
441 	REG(DEV_MAC_STICKY,			0x44),
442 	REG_RESERVED(PCS1G_CFG),
443 	REG_RESERVED(PCS1G_MODE_CFG),
444 	REG_RESERVED(PCS1G_SD_CFG),
445 	REG_RESERVED(PCS1G_ANEG_CFG),
446 	REG_RESERVED(PCS1G_ANEG_NP_CFG),
447 	REG_RESERVED(PCS1G_LB_CFG),
448 	REG_RESERVED(PCS1G_DBG_CFG),
449 	REG_RESERVED(PCS1G_CDET_CFG),
450 	REG_RESERVED(PCS1G_ANEG_STATUS),
451 	REG_RESERVED(PCS1G_ANEG_NP_STATUS),
452 	REG_RESERVED(PCS1G_LINK_STATUS),
453 	REG_RESERVED(PCS1G_LINK_DOWN_CNT),
454 	REG_RESERVED(PCS1G_STICKY),
455 	REG_RESERVED(PCS1G_DEBUG_STATUS),
456 	REG_RESERVED(PCS1G_LPI_CFG),
457 	REG_RESERVED(PCS1G_LPI_WAKE_ERROR_CNT),
458 	REG_RESERVED(PCS1G_LPI_STATUS),
459 	REG_RESERVED(PCS1G_TSTPAT_MODE_CFG),
460 	REG_RESERVED(PCS1G_TSTPAT_STATUS),
461 	REG_RESERVED(DEV_PCS_FX100_CFG),
462 	REG_RESERVED(DEV_PCS_FX100_STATUS),
463 };
464 
465 static const u32 *vsc9959_regmap[TARGET_MAX] = {
466 	[ANA]	= vsc9959_ana_regmap,
467 	[QS]	= vsc9959_qs_regmap,
468 	[QSYS]	= vsc9959_qsys_regmap,
469 	[REW]	= vsc9959_rew_regmap,
470 	[SYS]	= vsc9959_sys_regmap,
471 	[S0]	= vsc9959_vcap_regmap,
472 	[S1]	= vsc9959_vcap_regmap,
473 	[S2]	= vsc9959_vcap_regmap,
474 	[PTP]	= vsc9959_ptp_regmap,
475 	[GCB]	= vsc9959_gcb_regmap,
476 	[DEV_GMII] = vsc9959_dev_gmii_regmap,
477 };
478 
479 /* Addresses are relative to the PCI device's base address */
480 static const struct resource vsc9959_resources[] = {
481 	DEFINE_RES_MEM_NAMED(0x0010000, 0x0010000, "sys"),
482 	DEFINE_RES_MEM_NAMED(0x0030000, 0x0010000, "rew"),
483 	DEFINE_RES_MEM_NAMED(0x0040000, 0x0000400, "s0"),
484 	DEFINE_RES_MEM_NAMED(0x0050000, 0x0000400, "s1"),
485 	DEFINE_RES_MEM_NAMED(0x0060000, 0x0000400, "s2"),
486 	DEFINE_RES_MEM_NAMED(0x0070000, 0x0000200, "devcpu_gcb"),
487 	DEFINE_RES_MEM_NAMED(0x0080000, 0x0000100, "qs"),
488 	DEFINE_RES_MEM_NAMED(0x0090000, 0x00000cc, "ptp"),
489 	DEFINE_RES_MEM_NAMED(0x0100000, 0x0010000, "port0"),
490 	DEFINE_RES_MEM_NAMED(0x0110000, 0x0010000, "port1"),
491 	DEFINE_RES_MEM_NAMED(0x0120000, 0x0010000, "port2"),
492 	DEFINE_RES_MEM_NAMED(0x0130000, 0x0010000, "port3"),
493 	DEFINE_RES_MEM_NAMED(0x0140000, 0x0010000, "port4"),
494 	DEFINE_RES_MEM_NAMED(0x0150000, 0x0010000, "port5"),
495 	DEFINE_RES_MEM_NAMED(0x0200000, 0x0020000, "qsys"),
496 	DEFINE_RES_MEM_NAMED(0x0280000, 0x0010000, "ana"),
497 };
498 
499 static const char * const vsc9959_resource_names[TARGET_MAX] = {
500 	[SYS] = "sys",
501 	[REW] = "rew",
502 	[S0] = "s0",
503 	[S1] = "s1",
504 	[S2] = "s2",
505 	[GCB] = "devcpu_gcb",
506 	[QS] = "qs",
507 	[PTP] = "ptp",
508 	[QSYS] = "qsys",
509 	[ANA] = "ana",
510 };
511 
512 /* Port MAC 0 Internal MDIO bus through which the SerDes acting as an
513  * SGMII/QSGMII MAC PCS can be found.
514  */
515 static const struct resource vsc9959_imdio_res =
516 	DEFINE_RES_MEM_NAMED(0x8030, 0x8040, "imdio");
517 
518 static const struct reg_field vsc9959_regfields[REGFIELD_MAX] = {
519 	[ANA_ADVLEARN_VLAN_CHK] = REG_FIELD(ANA_ADVLEARN, 6, 6),
520 	[ANA_ADVLEARN_LEARN_MIRROR] = REG_FIELD(ANA_ADVLEARN, 0, 5),
521 	[ANA_ANEVENTS_FLOOD_DISCARD] = REG_FIELD(ANA_ANEVENTS, 30, 30),
522 	[ANA_ANEVENTS_AUTOAGE] = REG_FIELD(ANA_ANEVENTS, 26, 26),
523 	[ANA_ANEVENTS_STORM_DROP] = REG_FIELD(ANA_ANEVENTS, 24, 24),
524 	[ANA_ANEVENTS_LEARN_DROP] = REG_FIELD(ANA_ANEVENTS, 23, 23),
525 	[ANA_ANEVENTS_AGED_ENTRY] = REG_FIELD(ANA_ANEVENTS, 22, 22),
526 	[ANA_ANEVENTS_CPU_LEARN_FAILED] = REG_FIELD(ANA_ANEVENTS, 21, 21),
527 	[ANA_ANEVENTS_AUTO_LEARN_FAILED] = REG_FIELD(ANA_ANEVENTS, 20, 20),
528 	[ANA_ANEVENTS_LEARN_REMOVE] = REG_FIELD(ANA_ANEVENTS, 19, 19),
529 	[ANA_ANEVENTS_AUTO_LEARNED] = REG_FIELD(ANA_ANEVENTS, 18, 18),
530 	[ANA_ANEVENTS_AUTO_MOVED] = REG_FIELD(ANA_ANEVENTS, 17, 17),
531 	[ANA_ANEVENTS_CLASSIFIED_DROP] = REG_FIELD(ANA_ANEVENTS, 15, 15),
532 	[ANA_ANEVENTS_CLASSIFIED_COPY] = REG_FIELD(ANA_ANEVENTS, 14, 14),
533 	[ANA_ANEVENTS_VLAN_DISCARD] = REG_FIELD(ANA_ANEVENTS, 13, 13),
534 	[ANA_ANEVENTS_FWD_DISCARD] = REG_FIELD(ANA_ANEVENTS, 12, 12),
535 	[ANA_ANEVENTS_MULTICAST_FLOOD] = REG_FIELD(ANA_ANEVENTS, 11, 11),
536 	[ANA_ANEVENTS_UNICAST_FLOOD] = REG_FIELD(ANA_ANEVENTS, 10, 10),
537 	[ANA_ANEVENTS_DEST_KNOWN] = REG_FIELD(ANA_ANEVENTS, 9, 9),
538 	[ANA_ANEVENTS_BUCKET3_MATCH] = REG_FIELD(ANA_ANEVENTS, 8, 8),
539 	[ANA_ANEVENTS_BUCKET2_MATCH] = REG_FIELD(ANA_ANEVENTS, 7, 7),
540 	[ANA_ANEVENTS_BUCKET1_MATCH] = REG_FIELD(ANA_ANEVENTS, 6, 6),
541 	[ANA_ANEVENTS_BUCKET0_MATCH] = REG_FIELD(ANA_ANEVENTS, 5, 5),
542 	[ANA_ANEVENTS_CPU_OPERATION] = REG_FIELD(ANA_ANEVENTS, 4, 4),
543 	[ANA_ANEVENTS_DMAC_LOOKUP] = REG_FIELD(ANA_ANEVENTS, 3, 3),
544 	[ANA_ANEVENTS_SMAC_LOOKUP] = REG_FIELD(ANA_ANEVENTS, 2, 2),
545 	[ANA_ANEVENTS_SEQ_GEN_ERR_0] = REG_FIELD(ANA_ANEVENTS, 1, 1),
546 	[ANA_ANEVENTS_SEQ_GEN_ERR_1] = REG_FIELD(ANA_ANEVENTS, 0, 0),
547 	[ANA_TABLES_MACACCESS_B_DOM] = REG_FIELD(ANA_TABLES_MACACCESS, 16, 16),
548 	[ANA_TABLES_MACTINDX_BUCKET] = REG_FIELD(ANA_TABLES_MACTINDX, 11, 12),
549 	[ANA_TABLES_MACTINDX_M_INDEX] = REG_FIELD(ANA_TABLES_MACTINDX, 0, 10),
550 	[SYS_RESET_CFG_CORE_ENA] = REG_FIELD(SYS_RESET_CFG, 0, 0),
551 	[GCB_SOFT_RST_SWC_RST] = REG_FIELD(GCB_SOFT_RST, 0, 0),
552 	/* Replicated per number of ports (7), register size 4 per port */
553 	[QSYS_SWITCH_PORT_MODE_PORT_ENA] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 14, 14, 7, 4),
554 	[QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 11, 13, 7, 4),
555 	[QSYS_SWITCH_PORT_MODE_YEL_RSRVD] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 10, 10, 7, 4),
556 	[QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 9, 9, 7, 4),
557 	[QSYS_SWITCH_PORT_MODE_TX_PFC_ENA] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 1, 8, 7, 4),
558 	[QSYS_SWITCH_PORT_MODE_TX_PFC_MODE] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 0, 0, 7, 4),
559 	[SYS_PORT_MODE_DATA_WO_TS] = REG_FIELD_ID(SYS_PORT_MODE, 5, 6, 7, 4),
560 	[SYS_PORT_MODE_INCL_INJ_HDR] = REG_FIELD_ID(SYS_PORT_MODE, 3, 4, 7, 4),
561 	[SYS_PORT_MODE_INCL_XTR_HDR] = REG_FIELD_ID(SYS_PORT_MODE, 1, 2, 7, 4),
562 	[SYS_PORT_MODE_INCL_HDR_ERR] = REG_FIELD_ID(SYS_PORT_MODE, 0, 0, 7, 4),
563 	[SYS_PAUSE_CFG_PAUSE_START] = REG_FIELD_ID(SYS_PAUSE_CFG, 10, 18, 7, 4),
564 	[SYS_PAUSE_CFG_PAUSE_STOP] = REG_FIELD_ID(SYS_PAUSE_CFG, 1, 9, 7, 4),
565 	[SYS_PAUSE_CFG_PAUSE_ENA] = REG_FIELD_ID(SYS_PAUSE_CFG, 0, 1, 7, 4),
566 };
567 
568 static const struct ocelot_stat_layout vsc9959_stats_layout[OCELOT_NUM_STATS] = {
569 	OCELOT_COMMON_STATS,
570 };
571 
572 static const struct vcap_field vsc9959_vcap_es0_keys[] = {
573 	[VCAP_ES0_EGR_PORT]			= {  0,  3},
574 	[VCAP_ES0_IGR_PORT]			= {  3,  3},
575 	[VCAP_ES0_RSV]				= {  6,  2},
576 	[VCAP_ES0_L2_MC]			= {  8,  1},
577 	[VCAP_ES0_L2_BC]			= {  9,  1},
578 	[VCAP_ES0_VID]				= { 10, 12},
579 	[VCAP_ES0_DP]				= { 22,  1},
580 	[VCAP_ES0_PCP]				= { 23,  3},
581 };
582 
583 static const struct vcap_field vsc9959_vcap_es0_actions[] = {
584 	[VCAP_ES0_ACT_PUSH_OUTER_TAG]		= {  0,  2},
585 	[VCAP_ES0_ACT_PUSH_INNER_TAG]		= {  2,  1},
586 	[VCAP_ES0_ACT_TAG_A_TPID_SEL]		= {  3,  2},
587 	[VCAP_ES0_ACT_TAG_A_VID_SEL]		= {  5,  1},
588 	[VCAP_ES0_ACT_TAG_A_PCP_SEL]		= {  6,  2},
589 	[VCAP_ES0_ACT_TAG_A_DEI_SEL]		= {  8,  2},
590 	[VCAP_ES0_ACT_TAG_B_TPID_SEL]		= { 10,  2},
591 	[VCAP_ES0_ACT_TAG_B_VID_SEL]		= { 12,  1},
592 	[VCAP_ES0_ACT_TAG_B_PCP_SEL]		= { 13,  2},
593 	[VCAP_ES0_ACT_TAG_B_DEI_SEL]		= { 15,  2},
594 	[VCAP_ES0_ACT_VID_A_VAL]		= { 17, 12},
595 	[VCAP_ES0_ACT_PCP_A_VAL]		= { 29,  3},
596 	[VCAP_ES0_ACT_DEI_A_VAL]		= { 32,  1},
597 	[VCAP_ES0_ACT_VID_B_VAL]		= { 33, 12},
598 	[VCAP_ES0_ACT_PCP_B_VAL]		= { 45,  3},
599 	[VCAP_ES0_ACT_DEI_B_VAL]		= { 48,  1},
600 	[VCAP_ES0_ACT_RSV]			= { 49, 23},
601 	[VCAP_ES0_ACT_HIT_STICKY]		= { 72,  1},
602 };
603 
604 static const struct vcap_field vsc9959_vcap_is1_keys[] = {
605 	[VCAP_IS1_HK_TYPE]			= {  0,   1},
606 	[VCAP_IS1_HK_LOOKUP]			= {  1,   2},
607 	[VCAP_IS1_HK_IGR_PORT_MASK]		= {  3,   7},
608 	[VCAP_IS1_HK_RSV]			= { 10,   9},
609 	[VCAP_IS1_HK_OAM_Y1731]			= { 19,   1},
610 	[VCAP_IS1_HK_L2_MC]			= { 20,   1},
611 	[VCAP_IS1_HK_L2_BC]			= { 21,   1},
612 	[VCAP_IS1_HK_IP_MC]			= { 22,   1},
613 	[VCAP_IS1_HK_VLAN_TAGGED]		= { 23,   1},
614 	[VCAP_IS1_HK_VLAN_DBL_TAGGED]		= { 24,   1},
615 	[VCAP_IS1_HK_TPID]			= { 25,   1},
616 	[VCAP_IS1_HK_VID]			= { 26,  12},
617 	[VCAP_IS1_HK_DEI]			= { 38,   1},
618 	[VCAP_IS1_HK_PCP]			= { 39,   3},
619 	/* Specific Fields for IS1 Half Key S1_NORMAL */
620 	[VCAP_IS1_HK_L2_SMAC]			= { 42,  48},
621 	[VCAP_IS1_HK_ETYPE_LEN]			= { 90,   1},
622 	[VCAP_IS1_HK_ETYPE]			= { 91,  16},
623 	[VCAP_IS1_HK_IP_SNAP]			= {107,   1},
624 	[VCAP_IS1_HK_IP4]			= {108,   1},
625 	/* Layer-3 Information */
626 	[VCAP_IS1_HK_L3_FRAGMENT]		= {109,   1},
627 	[VCAP_IS1_HK_L3_FRAG_OFS_GT0]		= {110,   1},
628 	[VCAP_IS1_HK_L3_OPTIONS]		= {111,   1},
629 	[VCAP_IS1_HK_L3_DSCP]			= {112,   6},
630 	[VCAP_IS1_HK_L3_IP4_SIP]		= {118,  32},
631 	/* Layer-4 Information */
632 	[VCAP_IS1_HK_TCP_UDP]			= {150,   1},
633 	[VCAP_IS1_HK_TCP]			= {151,   1},
634 	[VCAP_IS1_HK_L4_SPORT]			= {152,  16},
635 	[VCAP_IS1_HK_L4_RNG]			= {168,   8},
636 	/* Specific Fields for IS1 Half Key S1_5TUPLE_IP4 */
637 	[VCAP_IS1_HK_IP4_INNER_TPID]            = { 42,   1},
638 	[VCAP_IS1_HK_IP4_INNER_VID]		= { 43,  12},
639 	[VCAP_IS1_HK_IP4_INNER_DEI]		= { 55,   1},
640 	[VCAP_IS1_HK_IP4_INNER_PCP]		= { 56,   3},
641 	[VCAP_IS1_HK_IP4_IP4]			= { 59,   1},
642 	[VCAP_IS1_HK_IP4_L3_FRAGMENT]		= { 60,   1},
643 	[VCAP_IS1_HK_IP4_L3_FRAG_OFS_GT0]	= { 61,   1},
644 	[VCAP_IS1_HK_IP4_L3_OPTIONS]		= { 62,   1},
645 	[VCAP_IS1_HK_IP4_L3_DSCP]		= { 63,   6},
646 	[VCAP_IS1_HK_IP4_L3_IP4_DIP]		= { 69,  32},
647 	[VCAP_IS1_HK_IP4_L3_IP4_SIP]		= {101,  32},
648 	[VCAP_IS1_HK_IP4_L3_PROTO]		= {133,   8},
649 	[VCAP_IS1_HK_IP4_TCP_UDP]		= {141,   1},
650 	[VCAP_IS1_HK_IP4_TCP]			= {142,   1},
651 	[VCAP_IS1_HK_IP4_L4_RNG]		= {143,   8},
652 	[VCAP_IS1_HK_IP4_IP_PAYLOAD_S1_5TUPLE]	= {151,  32},
653 };
654 
655 static const struct vcap_field vsc9959_vcap_is1_actions[] = {
656 	[VCAP_IS1_ACT_DSCP_ENA]			= {  0,  1},
657 	[VCAP_IS1_ACT_DSCP_VAL]			= {  1,  6},
658 	[VCAP_IS1_ACT_QOS_ENA]			= {  7,  1},
659 	[VCAP_IS1_ACT_QOS_VAL]			= {  8,  3},
660 	[VCAP_IS1_ACT_DP_ENA]			= { 11,  1},
661 	[VCAP_IS1_ACT_DP_VAL]			= { 12,  1},
662 	[VCAP_IS1_ACT_PAG_OVERRIDE_MASK]	= { 13,  8},
663 	[VCAP_IS1_ACT_PAG_VAL]			= { 21,  8},
664 	[VCAP_IS1_ACT_RSV]			= { 29,  9},
665 	/* The fields below are incorrectly shifted by 2 in the manual */
666 	[VCAP_IS1_ACT_VID_REPLACE_ENA]		= { 38,  1},
667 	[VCAP_IS1_ACT_VID_ADD_VAL]		= { 39, 12},
668 	[VCAP_IS1_ACT_FID_SEL]			= { 51,  2},
669 	[VCAP_IS1_ACT_FID_VAL]			= { 53, 13},
670 	[VCAP_IS1_ACT_PCP_DEI_ENA]		= { 66,  1},
671 	[VCAP_IS1_ACT_PCP_VAL]			= { 67,  3},
672 	[VCAP_IS1_ACT_DEI_VAL]			= { 70,  1},
673 	[VCAP_IS1_ACT_VLAN_POP_CNT_ENA]		= { 71,  1},
674 	[VCAP_IS1_ACT_VLAN_POP_CNT]		= { 72,  2},
675 	[VCAP_IS1_ACT_CUSTOM_ACE_TYPE_ENA]	= { 74,  4},
676 	[VCAP_IS1_ACT_HIT_STICKY]		= { 78,  1},
677 };
678 
679 static struct vcap_field vsc9959_vcap_is2_keys[] = {
680 	/* Common: 41 bits */
681 	[VCAP_IS2_TYPE]				= {  0,   4},
682 	[VCAP_IS2_HK_FIRST]			= {  4,   1},
683 	[VCAP_IS2_HK_PAG]			= {  5,   8},
684 	[VCAP_IS2_HK_IGR_PORT_MASK]		= { 13,   7},
685 	[VCAP_IS2_HK_RSV2]			= { 20,   1},
686 	[VCAP_IS2_HK_HOST_MATCH]		= { 21,   1},
687 	[VCAP_IS2_HK_L2_MC]			= { 22,   1},
688 	[VCAP_IS2_HK_L2_BC]			= { 23,   1},
689 	[VCAP_IS2_HK_VLAN_TAGGED]		= { 24,   1},
690 	[VCAP_IS2_HK_VID]			= { 25,  12},
691 	[VCAP_IS2_HK_DEI]			= { 37,   1},
692 	[VCAP_IS2_HK_PCP]			= { 38,   3},
693 	/* MAC_ETYPE / MAC_LLC / MAC_SNAP / OAM common */
694 	[VCAP_IS2_HK_L2_DMAC]			= { 41,  48},
695 	[VCAP_IS2_HK_L2_SMAC]			= { 89,  48},
696 	/* MAC_ETYPE (TYPE=000) */
697 	[VCAP_IS2_HK_MAC_ETYPE_ETYPE]		= {137,  16},
698 	[VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0]	= {153,  16},
699 	[VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1]	= {169,   8},
700 	[VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2]	= {177,   3},
701 	/* MAC_LLC (TYPE=001) */
702 	[VCAP_IS2_HK_MAC_LLC_L2_LLC]		= {137,  40},
703 	/* MAC_SNAP (TYPE=010) */
704 	[VCAP_IS2_HK_MAC_SNAP_L2_SNAP]		= {137,  40},
705 	/* MAC_ARP (TYPE=011) */
706 	[VCAP_IS2_HK_MAC_ARP_SMAC]		= { 41,  48},
707 	[VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK]	= { 89,   1},
708 	[VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK]	= { 90,   1},
709 	[VCAP_IS2_HK_MAC_ARP_LEN_OK]		= { 91,   1},
710 	[VCAP_IS2_HK_MAC_ARP_TARGET_MATCH]	= { 92,   1},
711 	[VCAP_IS2_HK_MAC_ARP_SENDER_MATCH]	= { 93,   1},
712 	[VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN]	= { 94,   1},
713 	[VCAP_IS2_HK_MAC_ARP_OPCODE]		= { 95,   2},
714 	[VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP]	= { 97,  32},
715 	[VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP]	= {129,  32},
716 	[VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP]	= {161,   1},
717 	/* IP4_TCP_UDP / IP4_OTHER common */
718 	[VCAP_IS2_HK_IP4]			= { 41,   1},
719 	[VCAP_IS2_HK_L3_FRAGMENT]		= { 42,   1},
720 	[VCAP_IS2_HK_L3_FRAG_OFS_GT0]		= { 43,   1},
721 	[VCAP_IS2_HK_L3_OPTIONS]		= { 44,   1},
722 	[VCAP_IS2_HK_IP4_L3_TTL_GT0]		= { 45,   1},
723 	[VCAP_IS2_HK_L3_TOS]			= { 46,   8},
724 	[VCAP_IS2_HK_L3_IP4_DIP]		= { 54,  32},
725 	[VCAP_IS2_HK_L3_IP4_SIP]		= { 86,  32},
726 	[VCAP_IS2_HK_DIP_EQ_SIP]		= {118,   1},
727 	/* IP4_TCP_UDP (TYPE=100) */
728 	[VCAP_IS2_HK_TCP]			= {119,   1},
729 	[VCAP_IS2_HK_L4_DPORT]			= {120,  16},
730 	[VCAP_IS2_HK_L4_SPORT]			= {136,  16},
731 	[VCAP_IS2_HK_L4_RNG]			= {152,   8},
732 	[VCAP_IS2_HK_L4_SPORT_EQ_DPORT]		= {160,   1},
733 	[VCAP_IS2_HK_L4_SEQUENCE_EQ0]		= {161,   1},
734 	[VCAP_IS2_HK_L4_FIN]			= {162,   1},
735 	[VCAP_IS2_HK_L4_SYN]			= {163,   1},
736 	[VCAP_IS2_HK_L4_RST]			= {164,   1},
737 	[VCAP_IS2_HK_L4_PSH]			= {165,   1},
738 	[VCAP_IS2_HK_L4_ACK]			= {166,   1},
739 	[VCAP_IS2_HK_L4_URG]			= {167,   1},
740 	[VCAP_IS2_HK_L4_1588_DOM]		= {168,   8},
741 	[VCAP_IS2_HK_L4_1588_VER]		= {176,   4},
742 	/* IP4_OTHER (TYPE=101) */
743 	[VCAP_IS2_HK_IP4_L3_PROTO]		= {119,   8},
744 	[VCAP_IS2_HK_L3_PAYLOAD]		= {127,  56},
745 	/* IP6_STD (TYPE=110) */
746 	[VCAP_IS2_HK_IP6_L3_TTL_GT0]		= { 41,   1},
747 	[VCAP_IS2_HK_L3_IP6_SIP]		= { 42, 128},
748 	[VCAP_IS2_HK_IP6_L3_PROTO]		= {170,   8},
749 	/* OAM (TYPE=111) */
750 	[VCAP_IS2_HK_OAM_MEL_FLAGS]		= {137,   7},
751 	[VCAP_IS2_HK_OAM_VER]			= {144,   5},
752 	[VCAP_IS2_HK_OAM_OPCODE]		= {149,   8},
753 	[VCAP_IS2_HK_OAM_FLAGS]			= {157,   8},
754 	[VCAP_IS2_HK_OAM_MEPID]			= {165,  16},
755 	[VCAP_IS2_HK_OAM_CCM_CNTS_EQ0]		= {181,   1},
756 	[VCAP_IS2_HK_OAM_IS_Y1731]		= {182,   1},
757 };
758 
759 static struct vcap_field vsc9959_vcap_is2_actions[] = {
760 	[VCAP_IS2_ACT_HIT_ME_ONCE]		= {  0,  1},
761 	[VCAP_IS2_ACT_CPU_COPY_ENA]		= {  1,  1},
762 	[VCAP_IS2_ACT_CPU_QU_NUM]		= {  2,  3},
763 	[VCAP_IS2_ACT_MASK_MODE]		= {  5,  2},
764 	[VCAP_IS2_ACT_MIRROR_ENA]		= {  7,  1},
765 	[VCAP_IS2_ACT_LRN_DIS]			= {  8,  1},
766 	[VCAP_IS2_ACT_POLICE_ENA]		= {  9,  1},
767 	[VCAP_IS2_ACT_POLICE_IDX]		= { 10,  9},
768 	[VCAP_IS2_ACT_POLICE_VCAP_ONLY]		= { 19,  1},
769 	[VCAP_IS2_ACT_PORT_MASK]		= { 20,  6},
770 	[VCAP_IS2_ACT_REW_OP]			= { 26,  9},
771 	[VCAP_IS2_ACT_SMAC_REPLACE_ENA]		= { 35,  1},
772 	[VCAP_IS2_ACT_RSV]			= { 36,  2},
773 	[VCAP_IS2_ACT_ACL_ID]			= { 38,  6},
774 	[VCAP_IS2_ACT_HIT_CNT]			= { 44, 32},
775 };
776 
777 static struct vcap_props vsc9959_vcap_props[] = {
778 	[VCAP_ES0] = {
779 		.action_type_width = 0,
780 		.action_table = {
781 			[ES0_ACTION_TYPE_NORMAL] = {
782 				.width = 72, /* HIT_STICKY not included */
783 				.count = 1,
784 			},
785 		},
786 		.target = S0,
787 		.keys = vsc9959_vcap_es0_keys,
788 		.actions = vsc9959_vcap_es0_actions,
789 	},
790 	[VCAP_IS1] = {
791 		.action_type_width = 0,
792 		.action_table = {
793 			[IS1_ACTION_TYPE_NORMAL] = {
794 				.width = 78, /* HIT_STICKY not included */
795 				.count = 4,
796 			},
797 		},
798 		.target = S1,
799 		.keys = vsc9959_vcap_is1_keys,
800 		.actions = vsc9959_vcap_is1_actions,
801 	},
802 	[VCAP_IS2] = {
803 		.action_type_width = 1,
804 		.action_table = {
805 			[IS2_ACTION_TYPE_NORMAL] = {
806 				.width = 44,
807 				.count = 2
808 			},
809 			[IS2_ACTION_TYPE_SMAC_SIP] = {
810 				.width = 6,
811 				.count = 4
812 			},
813 		},
814 		.target = S2,
815 		.keys = vsc9959_vcap_is2_keys,
816 		.actions = vsc9959_vcap_is2_actions,
817 	},
818 };
819 
820 static const struct ptp_clock_info vsc9959_ptp_caps = {
821 	.owner		= THIS_MODULE,
822 	.name		= "felix ptp",
823 	.max_adj	= 0x7fffffff,
824 	.n_alarm	= 0,
825 	.n_ext_ts	= 0,
826 	.n_per_out	= OCELOT_PTP_PINS_NUM,
827 	.n_pins		= OCELOT_PTP_PINS_NUM,
828 	.pps		= 0,
829 	.gettime64	= ocelot_ptp_gettime64,
830 	.settime64	= ocelot_ptp_settime64,
831 	.adjtime	= ocelot_ptp_adjtime,
832 	.adjfine	= ocelot_ptp_adjfine,
833 	.verify		= ocelot_ptp_verify,
834 	.enable		= ocelot_ptp_enable,
835 };
836 
837 #define VSC9959_INIT_TIMEOUT			50000
838 #define VSC9959_GCB_RST_SLEEP			100
839 #define VSC9959_SYS_RAMINIT_SLEEP		80
840 
841 static int vsc9959_gcb_soft_rst_status(struct ocelot *ocelot)
842 {
843 	int val;
844 
845 	ocelot_field_read(ocelot, GCB_SOFT_RST_SWC_RST, &val);
846 
847 	return val;
848 }
849 
850 static int vsc9959_sys_ram_init_status(struct ocelot *ocelot)
851 {
852 	return ocelot_read(ocelot, SYS_RAM_INIT);
853 }
854 
855 /* CORE_ENA is in SYS:SYSTEM:RESET_CFG
856  * RAM_INIT is in SYS:RAM_CTRL:RAM_INIT
857  */
858 static int vsc9959_reset(struct ocelot *ocelot)
859 {
860 	int val, err;
861 
862 	/* soft-reset the switch core */
863 	ocelot_field_write(ocelot, GCB_SOFT_RST_SWC_RST, 1);
864 
865 	err = readx_poll_timeout(vsc9959_gcb_soft_rst_status, ocelot, val, !val,
866 				 VSC9959_GCB_RST_SLEEP, VSC9959_INIT_TIMEOUT);
867 	if (err) {
868 		dev_err(ocelot->dev, "timeout: switch core reset\n");
869 		return err;
870 	}
871 
872 	/* initialize switch mem ~40us */
873 	ocelot_write(ocelot, SYS_RAM_INIT_RAM_INIT, SYS_RAM_INIT);
874 	err = readx_poll_timeout(vsc9959_sys_ram_init_status, ocelot, val, !val,
875 				 VSC9959_SYS_RAMINIT_SLEEP,
876 				 VSC9959_INIT_TIMEOUT);
877 	if (err) {
878 		dev_err(ocelot->dev, "timeout: switch sram init\n");
879 		return err;
880 	}
881 
882 	/* enable switch core */
883 	ocelot_field_write(ocelot, SYS_RESET_CFG_CORE_ENA, 1);
884 
885 	return 0;
886 }
887 
888 /* Watermark encode
889  * Bit 8:   Unit; 0:1, 1:16
890  * Bit 7-0: Value to be multiplied with unit
891  */
892 static u16 vsc9959_wm_enc(u16 value)
893 {
894 	WARN_ON(value >= 16 * BIT(8));
895 
896 	if (value >= BIT(8))
897 		return BIT(8) | (value / 16);
898 
899 	return value;
900 }
901 
902 static u16 vsc9959_wm_dec(u16 wm)
903 {
904 	WARN_ON(wm & ~GENMASK(8, 0));
905 
906 	if (wm & BIT(8))
907 		return (wm & GENMASK(7, 0)) * 16;
908 
909 	return wm;
910 }
911 
912 static void vsc9959_wm_stat(u32 val, u32 *inuse, u32 *maxuse)
913 {
914 	*inuse = (val & GENMASK(23, 12)) >> 12;
915 	*maxuse = val & GENMASK(11, 0);
916 }
917 
918 static int vsc9959_mdio_bus_alloc(struct ocelot *ocelot)
919 {
920 	struct pci_dev *pdev = to_pci_dev(ocelot->dev);
921 	struct felix *felix = ocelot_to_felix(ocelot);
922 	struct enetc_mdio_priv *mdio_priv;
923 	struct device *dev = ocelot->dev;
924 	resource_size_t imdio_base;
925 	void __iomem *imdio_regs;
926 	struct resource res;
927 	struct enetc_hw *hw;
928 	struct mii_bus *bus;
929 	int port;
930 	int rc;
931 
932 	felix->pcs = devm_kcalloc(dev, felix->info->num_ports,
933 				  sizeof(struct phylink_pcs *),
934 				  GFP_KERNEL);
935 	if (!felix->pcs) {
936 		dev_err(dev, "failed to allocate array for PCS PHYs\n");
937 		return -ENOMEM;
938 	}
939 
940 	imdio_base = pci_resource_start(pdev, VSC9959_IMDIO_PCI_BAR);
941 
942 	memcpy(&res, &vsc9959_imdio_res, sizeof(res));
943 	res.start += imdio_base;
944 	res.end += imdio_base;
945 
946 	imdio_regs = devm_ioremap_resource(dev, &res);
947 	if (IS_ERR(imdio_regs))
948 		return PTR_ERR(imdio_regs);
949 
950 	hw = enetc_hw_alloc(dev, imdio_regs);
951 	if (IS_ERR(hw)) {
952 		dev_err(dev, "failed to allocate ENETC HW structure\n");
953 		return PTR_ERR(hw);
954 	}
955 
956 	bus = mdiobus_alloc_size(sizeof(*mdio_priv));
957 	if (!bus)
958 		return -ENOMEM;
959 
960 	bus->name = "VSC9959 internal MDIO bus";
961 	bus->read = enetc_mdio_read;
962 	bus->write = enetc_mdio_write;
963 	bus->parent = dev;
964 	mdio_priv = bus->priv;
965 	mdio_priv->hw = hw;
966 	/* This gets added to imdio_regs, which already maps addresses
967 	 * starting with the proper offset.
968 	 */
969 	mdio_priv->mdio_base = 0;
970 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s-imdio", dev_name(dev));
971 
972 	/* Needed in order to initialize the bus mutex lock */
973 	rc = mdiobus_register(bus);
974 	if (rc < 0) {
975 		dev_err(dev, "failed to register MDIO bus\n");
976 		mdiobus_free(bus);
977 		return rc;
978 	}
979 
980 	felix->imdio = bus;
981 
982 	for (port = 0; port < felix->info->num_ports; port++) {
983 		struct ocelot_port *ocelot_port = ocelot->ports[port];
984 		struct phylink_pcs *phylink_pcs;
985 		struct mdio_device *mdio_device;
986 
987 		if (dsa_is_unused_port(felix->ds, port))
988 			continue;
989 
990 		if (ocelot_port->phy_mode == PHY_INTERFACE_MODE_INTERNAL)
991 			continue;
992 
993 		mdio_device = mdio_device_create(felix->imdio, port);
994 		if (IS_ERR(mdio_device))
995 			continue;
996 
997 		phylink_pcs = lynx_pcs_create(mdio_device);
998 		if (!phylink_pcs) {
999 			mdio_device_free(mdio_device);
1000 			continue;
1001 		}
1002 
1003 		felix->pcs[port] = phylink_pcs;
1004 
1005 		dev_info(dev, "Found PCS at internal MDIO address %d\n", port);
1006 	}
1007 
1008 	return 0;
1009 }
1010 
1011 static void vsc9959_mdio_bus_free(struct ocelot *ocelot)
1012 {
1013 	struct felix *felix = ocelot_to_felix(ocelot);
1014 	int port;
1015 
1016 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1017 		struct phylink_pcs *phylink_pcs = felix->pcs[port];
1018 		struct mdio_device *mdio_device;
1019 
1020 		if (!phylink_pcs)
1021 			continue;
1022 
1023 		mdio_device = lynx_get_mdio_device(phylink_pcs);
1024 		mdio_device_free(mdio_device);
1025 		lynx_pcs_destroy(phylink_pcs);
1026 	}
1027 	mdiobus_unregister(felix->imdio);
1028 	mdiobus_free(felix->imdio);
1029 }
1030 
1031 /* The switch considers any frame (regardless of size) as eligible for
1032  * transmission if the traffic class gate is open for at least 33 ns.
1033  * Overruns are prevented by cropping an interval at the end of the gate time
1034  * slot for which egress scheduling is blocked, but we need to still keep 33 ns
1035  * available for one packet to be transmitted, otherwise the port tc will hang.
1036  * This function returns the size of a gate interval that remains available for
1037  * setting the guard band, after reserving the space for one egress frame.
1038  */
1039 static u64 vsc9959_tas_remaining_gate_len_ps(u64 gate_len_ns)
1040 {
1041 	/* Gate always open */
1042 	if (gate_len_ns == U64_MAX)
1043 		return U64_MAX;
1044 
1045 	return (gate_len_ns - VSC9959_TAS_MIN_GATE_LEN_NS) * PSEC_PER_NSEC;
1046 }
1047 
1048 /* Extract shortest continuous gate open intervals in ns for each traffic class
1049  * of a cyclic tc-taprio schedule. If a gate is always open, the duration is
1050  * considered U64_MAX. If the gate is always closed, it is considered 0.
1051  */
1052 static void vsc9959_tas_min_gate_lengths(struct tc_taprio_qopt_offload *taprio,
1053 					 u64 min_gate_len[OCELOT_NUM_TC])
1054 {
1055 	struct tc_taprio_sched_entry *entry;
1056 	u64 gate_len[OCELOT_NUM_TC];
1057 	u8 gates_ever_opened = 0;
1058 	int tc, i, n;
1059 
1060 	/* Initialize arrays */
1061 	for (tc = 0; tc < OCELOT_NUM_TC; tc++) {
1062 		min_gate_len[tc] = U64_MAX;
1063 		gate_len[tc] = 0;
1064 	}
1065 
1066 	/* If we don't have taprio, consider all gates as permanently open */
1067 	if (!taprio)
1068 		return;
1069 
1070 	n = taprio->num_entries;
1071 
1072 	/* Walk through the gate list twice to determine the length
1073 	 * of consecutively open gates for a traffic class, including
1074 	 * open gates that wrap around. We are just interested in the
1075 	 * minimum window size, and this doesn't change what the
1076 	 * minimum is (if the gate never closes, min_gate_len will
1077 	 * remain U64_MAX).
1078 	 */
1079 	for (i = 0; i < 2 * n; i++) {
1080 		entry = &taprio->entries[i % n];
1081 
1082 		for (tc = 0; tc < OCELOT_NUM_TC; tc++) {
1083 			if (entry->gate_mask & BIT(tc)) {
1084 				gate_len[tc] += entry->interval;
1085 				gates_ever_opened |= BIT(tc);
1086 			} else {
1087 				/* Gate closes now, record a potential new
1088 				 * minimum and reinitialize length
1089 				 */
1090 				if (min_gate_len[tc] > gate_len[tc] &&
1091 				    gate_len[tc])
1092 					min_gate_len[tc] = gate_len[tc];
1093 				gate_len[tc] = 0;
1094 			}
1095 		}
1096 	}
1097 
1098 	/* min_gate_len[tc] actually tracks minimum *open* gate time, so for
1099 	 * permanently closed gates, min_gate_len[tc] will still be U64_MAX.
1100 	 * Therefore they are currently indistinguishable from permanently
1101 	 * open gates. Overwrite the gate len with 0 when we know they're
1102 	 * actually permanently closed, i.e. after the loop above.
1103 	 */
1104 	for (tc = 0; tc < OCELOT_NUM_TC; tc++)
1105 		if (!(gates_ever_opened & BIT(tc)))
1106 			min_gate_len[tc] = 0;
1107 }
1108 
1109 /* ocelot_write_rix is a macro that concatenates QSYS_MAXSDU_CFG_* with _RSZ,
1110  * so we need to spell out the register access to each traffic class in helper
1111  * functions, to simplify callers
1112  */
1113 static void vsc9959_port_qmaxsdu_set(struct ocelot *ocelot, int port, int tc,
1114 				     u32 max_sdu)
1115 {
1116 	switch (tc) {
1117 	case 0:
1118 		ocelot_write_rix(ocelot, max_sdu, QSYS_QMAXSDU_CFG_0,
1119 				 port);
1120 		break;
1121 	case 1:
1122 		ocelot_write_rix(ocelot, max_sdu, QSYS_QMAXSDU_CFG_1,
1123 				 port);
1124 		break;
1125 	case 2:
1126 		ocelot_write_rix(ocelot, max_sdu, QSYS_QMAXSDU_CFG_2,
1127 				 port);
1128 		break;
1129 	case 3:
1130 		ocelot_write_rix(ocelot, max_sdu, QSYS_QMAXSDU_CFG_3,
1131 				 port);
1132 		break;
1133 	case 4:
1134 		ocelot_write_rix(ocelot, max_sdu, QSYS_QMAXSDU_CFG_4,
1135 				 port);
1136 		break;
1137 	case 5:
1138 		ocelot_write_rix(ocelot, max_sdu, QSYS_QMAXSDU_CFG_5,
1139 				 port);
1140 		break;
1141 	case 6:
1142 		ocelot_write_rix(ocelot, max_sdu, QSYS_QMAXSDU_CFG_6,
1143 				 port);
1144 		break;
1145 	case 7:
1146 		ocelot_write_rix(ocelot, max_sdu, QSYS_QMAXSDU_CFG_7,
1147 				 port);
1148 		break;
1149 	}
1150 }
1151 
1152 static u32 vsc9959_port_qmaxsdu_get(struct ocelot *ocelot, int port, int tc)
1153 {
1154 	switch (tc) {
1155 	case 0: return ocelot_read_rix(ocelot, QSYS_QMAXSDU_CFG_0, port);
1156 	case 1: return ocelot_read_rix(ocelot, QSYS_QMAXSDU_CFG_1, port);
1157 	case 2: return ocelot_read_rix(ocelot, QSYS_QMAXSDU_CFG_2, port);
1158 	case 3: return ocelot_read_rix(ocelot, QSYS_QMAXSDU_CFG_3, port);
1159 	case 4: return ocelot_read_rix(ocelot, QSYS_QMAXSDU_CFG_4, port);
1160 	case 5: return ocelot_read_rix(ocelot, QSYS_QMAXSDU_CFG_5, port);
1161 	case 6: return ocelot_read_rix(ocelot, QSYS_QMAXSDU_CFG_6, port);
1162 	case 7: return ocelot_read_rix(ocelot, QSYS_QMAXSDU_CFG_7, port);
1163 	default:
1164 		return 0;
1165 	}
1166 }
1167 
1168 static u32 vsc9959_tas_tc_max_sdu(struct tc_taprio_qopt_offload *taprio, int tc)
1169 {
1170 	if (!taprio || !taprio->max_sdu[tc])
1171 		return 0;
1172 
1173 	return taprio->max_sdu[tc] + ETH_HLEN + 2 * VLAN_HLEN + ETH_FCS_LEN;
1174 }
1175 
1176 /* Update QSYS_PORT_MAX_SDU to make sure the static guard bands added by the
1177  * switch (see the ALWAYS_GUARD_BAND_SCH_Q comment) are correct at all MTU
1178  * values (the default value is 1518). Also, for traffic class windows smaller
1179  * than one MTU sized frame, update QSYS_QMAXSDU_CFG to enable oversized frame
1180  * dropping, such that these won't hang the port, as they will never be sent.
1181  */
1182 static void vsc9959_tas_guard_bands_update(struct ocelot *ocelot, int port)
1183 {
1184 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1185 	struct tc_taprio_qopt_offload *taprio;
1186 	u64 min_gate_len[OCELOT_NUM_TC];
1187 	int speed, picos_per_byte;
1188 	u64 needed_bit_time_ps;
1189 	u32 val, maxlen;
1190 	u8 tas_speed;
1191 	int tc;
1192 
1193 	lockdep_assert_held(&ocelot->tas_lock);
1194 
1195 	taprio = ocelot_port->taprio;
1196 
1197 	val = ocelot_read_rix(ocelot, QSYS_TAG_CONFIG, port);
1198 	tas_speed = QSYS_TAG_CONFIG_LINK_SPEED_X(val);
1199 
1200 	switch (tas_speed) {
1201 	case OCELOT_SPEED_10:
1202 		speed = SPEED_10;
1203 		break;
1204 	case OCELOT_SPEED_100:
1205 		speed = SPEED_100;
1206 		break;
1207 	case OCELOT_SPEED_1000:
1208 		speed = SPEED_1000;
1209 		break;
1210 	case OCELOT_SPEED_2500:
1211 		speed = SPEED_2500;
1212 		break;
1213 	default:
1214 		return;
1215 	}
1216 
1217 	picos_per_byte = (USEC_PER_SEC * 8) / speed;
1218 
1219 	val = ocelot_port_readl(ocelot_port, DEV_MAC_MAXLEN_CFG);
1220 	/* MAXLEN_CFG accounts automatically for VLAN. We need to include it
1221 	 * manually in the bit time calculation, plus the preamble and SFD.
1222 	 */
1223 	maxlen = val + 2 * VLAN_HLEN;
1224 	/* Consider the standard Ethernet overhead of 8 octets preamble+SFD,
1225 	 * 4 octets FCS, 12 octets IFG.
1226 	 */
1227 	needed_bit_time_ps = (maxlen + 24) * picos_per_byte;
1228 
1229 	dev_dbg(ocelot->dev,
1230 		"port %d: max frame size %d needs %llu ps at speed %d\n",
1231 		port, maxlen, needed_bit_time_ps, speed);
1232 
1233 	vsc9959_tas_min_gate_lengths(taprio, min_gate_len);
1234 
1235 	mutex_lock(&ocelot->fwd_domain_lock);
1236 
1237 	for (tc = 0; tc < OCELOT_NUM_TC; tc++) {
1238 		u32 requested_max_sdu = vsc9959_tas_tc_max_sdu(taprio, tc);
1239 		u64 remaining_gate_len_ps;
1240 		u32 max_sdu;
1241 
1242 		remaining_gate_len_ps =
1243 			vsc9959_tas_remaining_gate_len_ps(min_gate_len[tc]);
1244 
1245 		if (remaining_gate_len_ps > needed_bit_time_ps) {
1246 			/* Setting QMAXSDU_CFG to 0 disables oversized frame
1247 			 * dropping.
1248 			 */
1249 			max_sdu = requested_max_sdu;
1250 			dev_dbg(ocelot->dev,
1251 				"port %d tc %d min gate len %llu"
1252 				", sending all frames\n",
1253 				port, tc, min_gate_len[tc]);
1254 		} else {
1255 			/* If traffic class doesn't support a full MTU sized
1256 			 * frame, make sure to enable oversize frame dropping
1257 			 * for frames larger than the smallest that would fit.
1258 			 *
1259 			 * However, the exact same register, QSYS_QMAXSDU_CFG_*,
1260 			 * controls not only oversized frame dropping, but also
1261 			 * per-tc static guard band lengths, so it reduces the
1262 			 * useful gate interval length. Therefore, be careful
1263 			 * to calculate a guard band (and therefore max_sdu)
1264 			 * that still leaves 33 ns available in the time slot.
1265 			 */
1266 			max_sdu = div_u64(remaining_gate_len_ps, picos_per_byte);
1267 			/* A TC gate may be completely closed, which is a
1268 			 * special case where all packets are oversized.
1269 			 * Any limit smaller than 64 octets accomplishes this
1270 			 */
1271 			if (!max_sdu)
1272 				max_sdu = 1;
1273 			/* Take L1 overhead into account, but just don't allow
1274 			 * max_sdu to go negative or to 0. Here we use 20
1275 			 * because QSYS_MAXSDU_CFG_* already counts the 4 FCS
1276 			 * octets as part of packet size.
1277 			 */
1278 			if (max_sdu > 20)
1279 				max_sdu -= 20;
1280 
1281 			if (requested_max_sdu && requested_max_sdu < max_sdu)
1282 				max_sdu = requested_max_sdu;
1283 
1284 			dev_info(ocelot->dev,
1285 				 "port %d tc %d min gate length %llu"
1286 				 " ns not enough for max frame size %d at %d"
1287 				 " Mbps, dropping frames over %d"
1288 				 " octets including FCS\n",
1289 				 port, tc, min_gate_len[tc], maxlen, speed,
1290 				 max_sdu);
1291 		}
1292 
1293 		vsc9959_port_qmaxsdu_set(ocelot, port, tc, max_sdu);
1294 	}
1295 
1296 	ocelot_write_rix(ocelot, maxlen, QSYS_PORT_MAX_SDU, port);
1297 
1298 	ocelot->ops->cut_through_fwd(ocelot);
1299 
1300 	mutex_unlock(&ocelot->fwd_domain_lock);
1301 }
1302 
1303 static void vsc9959_sched_speed_set(struct ocelot *ocelot, int port,
1304 				    u32 speed)
1305 {
1306 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1307 	u8 tas_speed;
1308 
1309 	switch (speed) {
1310 	case SPEED_10:
1311 		tas_speed = OCELOT_SPEED_10;
1312 		break;
1313 	case SPEED_100:
1314 		tas_speed = OCELOT_SPEED_100;
1315 		break;
1316 	case SPEED_1000:
1317 		tas_speed = OCELOT_SPEED_1000;
1318 		break;
1319 	case SPEED_2500:
1320 		tas_speed = OCELOT_SPEED_2500;
1321 		break;
1322 	default:
1323 		tas_speed = OCELOT_SPEED_1000;
1324 		break;
1325 	}
1326 
1327 	mutex_lock(&ocelot->tas_lock);
1328 
1329 	ocelot_rmw_rix(ocelot,
1330 		       QSYS_TAG_CONFIG_LINK_SPEED(tas_speed),
1331 		       QSYS_TAG_CONFIG_LINK_SPEED_M,
1332 		       QSYS_TAG_CONFIG, port);
1333 
1334 	if (ocelot_port->taprio)
1335 		vsc9959_tas_guard_bands_update(ocelot, port);
1336 
1337 	mutex_unlock(&ocelot->tas_lock);
1338 }
1339 
1340 static void vsc9959_new_base_time(struct ocelot *ocelot, ktime_t base_time,
1341 				  u64 cycle_time,
1342 				  struct timespec64 *new_base_ts)
1343 {
1344 	struct timespec64 ts;
1345 	ktime_t new_base_time;
1346 	ktime_t current_time;
1347 
1348 	ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1349 	current_time = timespec64_to_ktime(ts);
1350 	new_base_time = base_time;
1351 
1352 	if (base_time < current_time) {
1353 		u64 nr_of_cycles = current_time - base_time;
1354 
1355 		do_div(nr_of_cycles, cycle_time);
1356 		new_base_time += cycle_time * (nr_of_cycles + 1);
1357 	}
1358 
1359 	*new_base_ts = ktime_to_timespec64(new_base_time);
1360 }
1361 
1362 static u32 vsc9959_tas_read_cfg_status(struct ocelot *ocelot)
1363 {
1364 	return ocelot_read(ocelot, QSYS_TAS_PARAM_CFG_CTRL);
1365 }
1366 
1367 static void vsc9959_tas_gcl_set(struct ocelot *ocelot, const u32 gcl_ix,
1368 				struct tc_taprio_sched_entry *entry)
1369 {
1370 	ocelot_write(ocelot,
1371 		     QSYS_GCL_CFG_REG_1_GCL_ENTRY_NUM(gcl_ix) |
1372 		     QSYS_GCL_CFG_REG_1_GATE_STATE(entry->gate_mask),
1373 		     QSYS_GCL_CFG_REG_1);
1374 	ocelot_write(ocelot, entry->interval, QSYS_GCL_CFG_REG_2);
1375 }
1376 
1377 static int vsc9959_qos_port_tas_set(struct ocelot *ocelot, int port,
1378 				    struct tc_taprio_qopt_offload *taprio)
1379 {
1380 	struct ocelot_port *ocelot_port = ocelot->ports[port];
1381 	struct timespec64 base_ts;
1382 	int ret, i;
1383 	u32 val;
1384 
1385 	mutex_lock(&ocelot->tas_lock);
1386 
1387 	if (!taprio->enable) {
1388 		ocelot_rmw_rix(ocelot, 0, QSYS_TAG_CONFIG_ENABLE,
1389 			       QSYS_TAG_CONFIG, port);
1390 
1391 		taprio_offload_free(ocelot_port->taprio);
1392 		ocelot_port->taprio = NULL;
1393 
1394 		vsc9959_tas_guard_bands_update(ocelot, port);
1395 
1396 		mutex_unlock(&ocelot->tas_lock);
1397 		return 0;
1398 	}
1399 
1400 	if (taprio->cycle_time > NSEC_PER_SEC ||
1401 	    taprio->cycle_time_extension >= NSEC_PER_SEC) {
1402 		ret = -EINVAL;
1403 		goto err;
1404 	}
1405 
1406 	if (taprio->num_entries > VSC9959_TAS_GCL_ENTRY_MAX) {
1407 		ret = -ERANGE;
1408 		goto err;
1409 	}
1410 
1411 	/* Enable guard band. The switch will schedule frames without taking
1412 	 * their length into account. Thus we'll always need to enable the
1413 	 * guard band which reserves the time of a maximum sized frame at the
1414 	 * end of the time window.
1415 	 *
1416 	 * Although the ALWAYS_GUARD_BAND_SCH_Q bit is global for all ports, we
1417 	 * need to set PORT_NUM, because subsequent writes to PARAM_CFG_REG_n
1418 	 * operate on the port number.
1419 	 */
1420 	ocelot_rmw(ocelot, QSYS_TAS_PARAM_CFG_CTRL_PORT_NUM(port) |
1421 		   QSYS_TAS_PARAM_CFG_CTRL_ALWAYS_GUARD_BAND_SCH_Q,
1422 		   QSYS_TAS_PARAM_CFG_CTRL_PORT_NUM_M |
1423 		   QSYS_TAS_PARAM_CFG_CTRL_ALWAYS_GUARD_BAND_SCH_Q,
1424 		   QSYS_TAS_PARAM_CFG_CTRL);
1425 
1426 	/* Hardware errata -  Admin config could not be overwritten if
1427 	 * config is pending, need reset the TAS module
1428 	 */
1429 	val = ocelot_read(ocelot, QSYS_PARAM_STATUS_REG_8);
1430 	if (val & QSYS_PARAM_STATUS_REG_8_CONFIG_PENDING) {
1431 		ret = -EBUSY;
1432 		goto err;
1433 	}
1434 
1435 	ocelot_rmw_rix(ocelot,
1436 		       QSYS_TAG_CONFIG_ENABLE |
1437 		       QSYS_TAG_CONFIG_INIT_GATE_STATE(0xFF) |
1438 		       QSYS_TAG_CONFIG_SCH_TRAFFIC_QUEUES(0xFF),
1439 		       QSYS_TAG_CONFIG_ENABLE |
1440 		       QSYS_TAG_CONFIG_INIT_GATE_STATE_M |
1441 		       QSYS_TAG_CONFIG_SCH_TRAFFIC_QUEUES_M,
1442 		       QSYS_TAG_CONFIG, port);
1443 
1444 	vsc9959_new_base_time(ocelot, taprio->base_time,
1445 			      taprio->cycle_time, &base_ts);
1446 	ocelot_write(ocelot, base_ts.tv_nsec, QSYS_PARAM_CFG_REG_1);
1447 	ocelot_write(ocelot, lower_32_bits(base_ts.tv_sec), QSYS_PARAM_CFG_REG_2);
1448 	val = upper_32_bits(base_ts.tv_sec);
1449 	ocelot_write(ocelot,
1450 		     QSYS_PARAM_CFG_REG_3_BASE_TIME_SEC_MSB(val) |
1451 		     QSYS_PARAM_CFG_REG_3_LIST_LENGTH(taprio->num_entries),
1452 		     QSYS_PARAM_CFG_REG_3);
1453 	ocelot_write(ocelot, taprio->cycle_time, QSYS_PARAM_CFG_REG_4);
1454 	ocelot_write(ocelot, taprio->cycle_time_extension, QSYS_PARAM_CFG_REG_5);
1455 
1456 	for (i = 0; i < taprio->num_entries; i++)
1457 		vsc9959_tas_gcl_set(ocelot, i, &taprio->entries[i]);
1458 
1459 	ocelot_rmw(ocelot, QSYS_TAS_PARAM_CFG_CTRL_CONFIG_CHANGE,
1460 		   QSYS_TAS_PARAM_CFG_CTRL_CONFIG_CHANGE,
1461 		   QSYS_TAS_PARAM_CFG_CTRL);
1462 
1463 	ret = readx_poll_timeout(vsc9959_tas_read_cfg_status, ocelot, val,
1464 				 !(val & QSYS_TAS_PARAM_CFG_CTRL_CONFIG_CHANGE),
1465 				 10, 100000);
1466 	if (ret)
1467 		goto err;
1468 
1469 	ocelot_port->taprio = taprio_offload_get(taprio);
1470 	vsc9959_tas_guard_bands_update(ocelot, port);
1471 
1472 err:
1473 	mutex_unlock(&ocelot->tas_lock);
1474 
1475 	return ret;
1476 }
1477 
1478 static void vsc9959_tas_clock_adjust(struct ocelot *ocelot)
1479 {
1480 	struct tc_taprio_qopt_offload *taprio;
1481 	struct ocelot_port *ocelot_port;
1482 	struct timespec64 base_ts;
1483 	int port;
1484 	u32 val;
1485 
1486 	mutex_lock(&ocelot->tas_lock);
1487 
1488 	for (port = 0; port < ocelot->num_phys_ports; port++) {
1489 		ocelot_port = ocelot->ports[port];
1490 		taprio = ocelot_port->taprio;
1491 		if (!taprio)
1492 			continue;
1493 
1494 		ocelot_rmw(ocelot,
1495 			   QSYS_TAS_PARAM_CFG_CTRL_PORT_NUM(port),
1496 			   QSYS_TAS_PARAM_CFG_CTRL_PORT_NUM_M,
1497 			   QSYS_TAS_PARAM_CFG_CTRL);
1498 
1499 		/* Disable time-aware shaper */
1500 		ocelot_rmw_rix(ocelot, 0, QSYS_TAG_CONFIG_ENABLE,
1501 			       QSYS_TAG_CONFIG, port);
1502 
1503 		vsc9959_new_base_time(ocelot, taprio->base_time,
1504 				      taprio->cycle_time, &base_ts);
1505 
1506 		ocelot_write(ocelot, base_ts.tv_nsec, QSYS_PARAM_CFG_REG_1);
1507 		ocelot_write(ocelot, lower_32_bits(base_ts.tv_sec),
1508 			     QSYS_PARAM_CFG_REG_2);
1509 		val = upper_32_bits(base_ts.tv_sec);
1510 		ocelot_rmw(ocelot,
1511 			   QSYS_PARAM_CFG_REG_3_BASE_TIME_SEC_MSB(val),
1512 			   QSYS_PARAM_CFG_REG_3_BASE_TIME_SEC_MSB_M,
1513 			   QSYS_PARAM_CFG_REG_3);
1514 
1515 		ocelot_rmw(ocelot, QSYS_TAS_PARAM_CFG_CTRL_CONFIG_CHANGE,
1516 			   QSYS_TAS_PARAM_CFG_CTRL_CONFIG_CHANGE,
1517 			   QSYS_TAS_PARAM_CFG_CTRL);
1518 
1519 		/* Re-enable time-aware shaper */
1520 		ocelot_rmw_rix(ocelot, QSYS_TAG_CONFIG_ENABLE,
1521 			       QSYS_TAG_CONFIG_ENABLE,
1522 			       QSYS_TAG_CONFIG, port);
1523 	}
1524 	mutex_unlock(&ocelot->tas_lock);
1525 }
1526 
1527 static int vsc9959_qos_port_cbs_set(struct dsa_switch *ds, int port,
1528 				    struct tc_cbs_qopt_offload *cbs_qopt)
1529 {
1530 	struct ocelot *ocelot = ds->priv;
1531 	int port_ix = port * 8 + cbs_qopt->queue;
1532 	u32 rate, burst;
1533 
1534 	if (cbs_qopt->queue >= ds->num_tx_queues)
1535 		return -EINVAL;
1536 
1537 	if (!cbs_qopt->enable) {
1538 		ocelot_write_gix(ocelot, QSYS_CIR_CFG_CIR_RATE(0) |
1539 				 QSYS_CIR_CFG_CIR_BURST(0),
1540 				 QSYS_CIR_CFG, port_ix);
1541 
1542 		ocelot_rmw_gix(ocelot, 0, QSYS_SE_CFG_SE_AVB_ENA,
1543 			       QSYS_SE_CFG, port_ix);
1544 
1545 		return 0;
1546 	}
1547 
1548 	/* Rate unit is 100 kbps */
1549 	rate = DIV_ROUND_UP(cbs_qopt->idleslope, 100);
1550 	/* Avoid using zero rate */
1551 	rate = clamp_t(u32, rate, 1, GENMASK(14, 0));
1552 	/* Burst unit is 4kB */
1553 	burst = DIV_ROUND_UP(cbs_qopt->hicredit, 4096);
1554 	/* Avoid using zero burst size */
1555 	burst = clamp_t(u32, burst, 1, GENMASK(5, 0));
1556 	ocelot_write_gix(ocelot,
1557 			 QSYS_CIR_CFG_CIR_RATE(rate) |
1558 			 QSYS_CIR_CFG_CIR_BURST(burst),
1559 			 QSYS_CIR_CFG,
1560 			 port_ix);
1561 
1562 	ocelot_rmw_gix(ocelot,
1563 		       QSYS_SE_CFG_SE_FRM_MODE(0) |
1564 		       QSYS_SE_CFG_SE_AVB_ENA,
1565 		       QSYS_SE_CFG_SE_AVB_ENA |
1566 		       QSYS_SE_CFG_SE_FRM_MODE_M,
1567 		       QSYS_SE_CFG,
1568 		       port_ix);
1569 
1570 	return 0;
1571 }
1572 
1573 static int vsc9959_qos_query_caps(struct tc_query_caps_base *base)
1574 {
1575 	switch (base->type) {
1576 	case TC_SETUP_QDISC_TAPRIO: {
1577 		struct tc_taprio_caps *caps = base->caps;
1578 
1579 		caps->supports_queue_max_sdu = true;
1580 
1581 		return 0;
1582 	}
1583 	default:
1584 		return -EOPNOTSUPP;
1585 	}
1586 }
1587 
1588 static int vsc9959_port_setup_tc(struct dsa_switch *ds, int port,
1589 				 enum tc_setup_type type,
1590 				 void *type_data)
1591 {
1592 	struct ocelot *ocelot = ds->priv;
1593 
1594 	switch (type) {
1595 	case TC_QUERY_CAPS:
1596 		return vsc9959_qos_query_caps(type_data);
1597 	case TC_SETUP_QDISC_TAPRIO:
1598 		return vsc9959_qos_port_tas_set(ocelot, port, type_data);
1599 	case TC_SETUP_QDISC_CBS:
1600 		return vsc9959_qos_port_cbs_set(ds, port, type_data);
1601 	default:
1602 		return -EOPNOTSUPP;
1603 	}
1604 }
1605 
1606 #define VSC9959_PSFP_SFID_MAX			175
1607 #define VSC9959_PSFP_GATE_ID_MAX		183
1608 #define VSC9959_PSFP_POLICER_BASE		63
1609 #define VSC9959_PSFP_POLICER_MAX		383
1610 #define VSC9959_PSFP_GATE_LIST_NUM		4
1611 #define VSC9959_PSFP_GATE_CYCLETIME_MIN		5000
1612 
1613 struct felix_stream {
1614 	struct list_head list;
1615 	unsigned long id;
1616 	bool dummy;
1617 	int ports;
1618 	int port;
1619 	u8 dmac[ETH_ALEN];
1620 	u16 vid;
1621 	s8 prio;
1622 	u8 sfid_valid;
1623 	u8 ssid_valid;
1624 	u32 sfid;
1625 	u32 ssid;
1626 };
1627 
1628 struct felix_stream_filter_counters {
1629 	u64 match;
1630 	u64 not_pass_gate;
1631 	u64 not_pass_sdu;
1632 	u64 red;
1633 };
1634 
1635 struct felix_stream_filter {
1636 	struct felix_stream_filter_counters stats;
1637 	struct list_head list;
1638 	refcount_t refcount;
1639 	u32 index;
1640 	u8 enable;
1641 	int portmask;
1642 	u8 sg_valid;
1643 	u32 sgid;
1644 	u8 fm_valid;
1645 	u32 fmid;
1646 	u8 prio_valid;
1647 	u8 prio;
1648 	u32 maxsdu;
1649 };
1650 
1651 struct felix_stream_gate {
1652 	u32 index;
1653 	u8 enable;
1654 	u8 ipv_valid;
1655 	u8 init_ipv;
1656 	u64 basetime;
1657 	u64 cycletime;
1658 	u64 cycletime_ext;
1659 	u32 num_entries;
1660 	struct action_gate_entry entries[];
1661 };
1662 
1663 struct felix_stream_gate_entry {
1664 	struct list_head list;
1665 	refcount_t refcount;
1666 	u32 index;
1667 };
1668 
1669 static int vsc9959_stream_identify(struct flow_cls_offload *f,
1670 				   struct felix_stream *stream)
1671 {
1672 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1673 	struct flow_dissector *dissector = rule->match.dissector;
1674 
1675 	if (dissector->used_keys &
1676 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1677 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
1678 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
1679 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS)))
1680 		return -EOPNOTSUPP;
1681 
1682 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1683 		struct flow_match_eth_addrs match;
1684 
1685 		flow_rule_match_eth_addrs(rule, &match);
1686 		ether_addr_copy(stream->dmac, match.key->dst);
1687 		if (!is_zero_ether_addr(match.mask->src))
1688 			return -EOPNOTSUPP;
1689 	} else {
1690 		return -EOPNOTSUPP;
1691 	}
1692 
1693 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
1694 		struct flow_match_vlan match;
1695 
1696 		flow_rule_match_vlan(rule, &match);
1697 		if (match.mask->vlan_priority)
1698 			stream->prio = match.key->vlan_priority;
1699 		else
1700 			stream->prio = -1;
1701 
1702 		if (!match.mask->vlan_id)
1703 			return -EOPNOTSUPP;
1704 		stream->vid = match.key->vlan_id;
1705 	} else {
1706 		return -EOPNOTSUPP;
1707 	}
1708 
1709 	stream->id = f->cookie;
1710 
1711 	return 0;
1712 }
1713 
1714 static int vsc9959_mact_stream_set(struct ocelot *ocelot,
1715 				   struct felix_stream *stream,
1716 				   struct netlink_ext_ack *extack)
1717 {
1718 	enum macaccess_entry_type type;
1719 	int ret, sfid, ssid;
1720 	u32 vid, dst_idx;
1721 	u8 mac[ETH_ALEN];
1722 
1723 	ether_addr_copy(mac, stream->dmac);
1724 	vid = stream->vid;
1725 
1726 	/* Stream identification desn't support to add a stream with non
1727 	 * existent MAC (The MAC entry has not been learned in MAC table).
1728 	 */
1729 	ret = ocelot_mact_lookup(ocelot, &dst_idx, mac, vid, &type);
1730 	if (ret) {
1731 		if (extack)
1732 			NL_SET_ERR_MSG_MOD(extack, "Stream is not learned in MAC table");
1733 		return -EOPNOTSUPP;
1734 	}
1735 
1736 	if ((stream->sfid_valid || stream->ssid_valid) &&
1737 	    type == ENTRYTYPE_NORMAL)
1738 		type = ENTRYTYPE_LOCKED;
1739 
1740 	sfid = stream->sfid_valid ? stream->sfid : -1;
1741 	ssid = stream->ssid_valid ? stream->ssid : -1;
1742 
1743 	ret = ocelot_mact_learn_streamdata(ocelot, dst_idx, mac, vid, type,
1744 					   sfid, ssid);
1745 
1746 	return ret;
1747 }
1748 
1749 static struct felix_stream *
1750 vsc9959_stream_table_lookup(struct list_head *stream_list,
1751 			    struct felix_stream *stream)
1752 {
1753 	struct felix_stream *tmp;
1754 
1755 	list_for_each_entry(tmp, stream_list, list)
1756 		if (ether_addr_equal(tmp->dmac, stream->dmac) &&
1757 		    tmp->vid == stream->vid)
1758 			return tmp;
1759 
1760 	return NULL;
1761 }
1762 
1763 static int vsc9959_stream_table_add(struct ocelot *ocelot,
1764 				    struct list_head *stream_list,
1765 				    struct felix_stream *stream,
1766 				    struct netlink_ext_ack *extack)
1767 {
1768 	struct felix_stream *stream_entry;
1769 	int ret;
1770 
1771 	stream_entry = kmemdup(stream, sizeof(*stream_entry), GFP_KERNEL);
1772 	if (!stream_entry)
1773 		return -ENOMEM;
1774 
1775 	if (!stream->dummy) {
1776 		ret = vsc9959_mact_stream_set(ocelot, stream_entry, extack);
1777 		if (ret) {
1778 			kfree(stream_entry);
1779 			return ret;
1780 		}
1781 	}
1782 
1783 	list_add_tail(&stream_entry->list, stream_list);
1784 
1785 	return 0;
1786 }
1787 
1788 static struct felix_stream *
1789 vsc9959_stream_table_get(struct list_head *stream_list, unsigned long id)
1790 {
1791 	struct felix_stream *tmp;
1792 
1793 	list_for_each_entry(tmp, stream_list, list)
1794 		if (tmp->id == id)
1795 			return tmp;
1796 
1797 	return NULL;
1798 }
1799 
1800 static void vsc9959_stream_table_del(struct ocelot *ocelot,
1801 				     struct felix_stream *stream)
1802 {
1803 	if (!stream->dummy)
1804 		vsc9959_mact_stream_set(ocelot, stream, NULL);
1805 
1806 	list_del(&stream->list);
1807 	kfree(stream);
1808 }
1809 
1810 static u32 vsc9959_sfi_access_status(struct ocelot *ocelot)
1811 {
1812 	return ocelot_read(ocelot, ANA_TABLES_SFIDACCESS);
1813 }
1814 
1815 static int vsc9959_psfp_sfi_set(struct ocelot *ocelot,
1816 				struct felix_stream_filter *sfi)
1817 {
1818 	u32 val;
1819 
1820 	if (sfi->index > VSC9959_PSFP_SFID_MAX)
1821 		return -EINVAL;
1822 
1823 	if (!sfi->enable) {
1824 		ocelot_write(ocelot, ANA_TABLES_SFIDTIDX_SFID_INDEX(sfi->index),
1825 			     ANA_TABLES_SFIDTIDX);
1826 
1827 		val = ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(SFIDACCESS_CMD_WRITE);
1828 		ocelot_write(ocelot, val, ANA_TABLES_SFIDACCESS);
1829 
1830 		return readx_poll_timeout(vsc9959_sfi_access_status, ocelot, val,
1831 					  (!ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(val)),
1832 					  10, 100000);
1833 	}
1834 
1835 	if (sfi->sgid > VSC9959_PSFP_GATE_ID_MAX ||
1836 	    sfi->fmid > VSC9959_PSFP_POLICER_MAX)
1837 		return -EINVAL;
1838 
1839 	ocelot_write(ocelot,
1840 		     (sfi->sg_valid ? ANA_TABLES_SFIDTIDX_SGID_VALID : 0) |
1841 		     ANA_TABLES_SFIDTIDX_SGID(sfi->sgid) |
1842 		     (sfi->fm_valid ? ANA_TABLES_SFIDTIDX_POL_ENA : 0) |
1843 		     ANA_TABLES_SFIDTIDX_POL_IDX(sfi->fmid) |
1844 		     ANA_TABLES_SFIDTIDX_SFID_INDEX(sfi->index),
1845 		     ANA_TABLES_SFIDTIDX);
1846 
1847 	ocelot_write(ocelot,
1848 		     (sfi->prio_valid ? ANA_TABLES_SFIDACCESS_IGR_PRIO_MATCH_ENA : 0) |
1849 		     ANA_TABLES_SFIDACCESS_IGR_PRIO(sfi->prio) |
1850 		     ANA_TABLES_SFIDACCESS_MAX_SDU_LEN(sfi->maxsdu) |
1851 		     ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(SFIDACCESS_CMD_WRITE),
1852 		     ANA_TABLES_SFIDACCESS);
1853 
1854 	return readx_poll_timeout(vsc9959_sfi_access_status, ocelot, val,
1855 				  (!ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(val)),
1856 				  10, 100000);
1857 }
1858 
1859 static int vsc9959_psfp_sfidmask_set(struct ocelot *ocelot, u32 sfid, int ports)
1860 {
1861 	u32 val;
1862 
1863 	ocelot_rmw(ocelot,
1864 		   ANA_TABLES_SFIDTIDX_SFID_INDEX(sfid),
1865 		   ANA_TABLES_SFIDTIDX_SFID_INDEX_M,
1866 		   ANA_TABLES_SFIDTIDX);
1867 
1868 	ocelot_write(ocelot,
1869 		     ANA_TABLES_SFID_MASK_IGR_PORT_MASK(ports) |
1870 		     ANA_TABLES_SFID_MASK_IGR_SRCPORT_MATCH_ENA,
1871 		     ANA_TABLES_SFID_MASK);
1872 
1873 	ocelot_rmw(ocelot,
1874 		   ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(SFIDACCESS_CMD_WRITE),
1875 		   ANA_TABLES_SFIDACCESS_SFID_TBL_CMD_M,
1876 		   ANA_TABLES_SFIDACCESS);
1877 
1878 	return readx_poll_timeout(vsc9959_sfi_access_status, ocelot, val,
1879 				  (!ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(val)),
1880 				  10, 100000);
1881 }
1882 
1883 static int vsc9959_psfp_sfi_list_add(struct ocelot *ocelot,
1884 				     struct felix_stream_filter *sfi,
1885 				     struct list_head *pos)
1886 {
1887 	struct felix_stream_filter *sfi_entry;
1888 	int ret;
1889 
1890 	sfi_entry = kmemdup(sfi, sizeof(*sfi_entry), GFP_KERNEL);
1891 	if (!sfi_entry)
1892 		return -ENOMEM;
1893 
1894 	refcount_set(&sfi_entry->refcount, 1);
1895 
1896 	ret = vsc9959_psfp_sfi_set(ocelot, sfi_entry);
1897 	if (ret) {
1898 		kfree(sfi_entry);
1899 		return ret;
1900 	}
1901 
1902 	vsc9959_psfp_sfidmask_set(ocelot, sfi->index, sfi->portmask);
1903 
1904 	list_add(&sfi_entry->list, pos);
1905 
1906 	return 0;
1907 }
1908 
1909 static int vsc9959_psfp_sfi_table_add(struct ocelot *ocelot,
1910 				      struct felix_stream_filter *sfi)
1911 {
1912 	struct list_head *pos, *q, *last;
1913 	struct felix_stream_filter *tmp;
1914 	struct ocelot_psfp_list *psfp;
1915 	u32 insert = 0;
1916 
1917 	psfp = &ocelot->psfp;
1918 	last = &psfp->sfi_list;
1919 
1920 	list_for_each_safe(pos, q, &psfp->sfi_list) {
1921 		tmp = list_entry(pos, struct felix_stream_filter, list);
1922 		if (sfi->sg_valid == tmp->sg_valid &&
1923 		    sfi->fm_valid == tmp->fm_valid &&
1924 		    sfi->portmask == tmp->portmask &&
1925 		    tmp->sgid == sfi->sgid &&
1926 		    tmp->fmid == sfi->fmid) {
1927 			sfi->index = tmp->index;
1928 			refcount_inc(&tmp->refcount);
1929 			return 0;
1930 		}
1931 		/* Make sure that the index is increasing in order. */
1932 		if (tmp->index == insert) {
1933 			last = pos;
1934 			insert++;
1935 		}
1936 	}
1937 	sfi->index = insert;
1938 
1939 	return vsc9959_psfp_sfi_list_add(ocelot, sfi, last);
1940 }
1941 
1942 static int vsc9959_psfp_sfi_table_add2(struct ocelot *ocelot,
1943 				       struct felix_stream_filter *sfi,
1944 				       struct felix_stream_filter *sfi2)
1945 {
1946 	struct felix_stream_filter *tmp;
1947 	struct list_head *pos, *q, *last;
1948 	struct ocelot_psfp_list *psfp;
1949 	u32 insert = 0;
1950 	int ret;
1951 
1952 	psfp = &ocelot->psfp;
1953 	last = &psfp->sfi_list;
1954 
1955 	list_for_each_safe(pos, q, &psfp->sfi_list) {
1956 		tmp = list_entry(pos, struct felix_stream_filter, list);
1957 		/* Make sure that the index is increasing in order. */
1958 		if (tmp->index >= insert + 2)
1959 			break;
1960 
1961 		insert = tmp->index + 1;
1962 		last = pos;
1963 	}
1964 	sfi->index = insert;
1965 
1966 	ret = vsc9959_psfp_sfi_list_add(ocelot, sfi, last);
1967 	if (ret)
1968 		return ret;
1969 
1970 	sfi2->index = insert + 1;
1971 
1972 	return vsc9959_psfp_sfi_list_add(ocelot, sfi2, last->next);
1973 }
1974 
1975 static struct felix_stream_filter *
1976 vsc9959_psfp_sfi_table_get(struct list_head *sfi_list, u32 index)
1977 {
1978 	struct felix_stream_filter *tmp;
1979 
1980 	list_for_each_entry(tmp, sfi_list, list)
1981 		if (tmp->index == index)
1982 			return tmp;
1983 
1984 	return NULL;
1985 }
1986 
1987 static void vsc9959_psfp_sfi_table_del(struct ocelot *ocelot, u32 index)
1988 {
1989 	struct felix_stream_filter *tmp, *n;
1990 	struct ocelot_psfp_list *psfp;
1991 	u8 z;
1992 
1993 	psfp = &ocelot->psfp;
1994 
1995 	list_for_each_entry_safe(tmp, n, &psfp->sfi_list, list)
1996 		if (tmp->index == index) {
1997 			z = refcount_dec_and_test(&tmp->refcount);
1998 			if (z) {
1999 				tmp->enable = 0;
2000 				vsc9959_psfp_sfi_set(ocelot, tmp);
2001 				list_del(&tmp->list);
2002 				kfree(tmp);
2003 			}
2004 			break;
2005 		}
2006 }
2007 
2008 static void vsc9959_psfp_parse_gate(const struct flow_action_entry *entry,
2009 				    struct felix_stream_gate *sgi)
2010 {
2011 	sgi->index = entry->hw_index;
2012 	sgi->ipv_valid = (entry->gate.prio < 0) ? 0 : 1;
2013 	sgi->init_ipv = (sgi->ipv_valid) ? entry->gate.prio : 0;
2014 	sgi->basetime = entry->gate.basetime;
2015 	sgi->cycletime = entry->gate.cycletime;
2016 	sgi->num_entries = entry->gate.num_entries;
2017 	sgi->enable = 1;
2018 
2019 	memcpy(sgi->entries, entry->gate.entries,
2020 	       entry->gate.num_entries * sizeof(struct action_gate_entry));
2021 }
2022 
2023 static u32 vsc9959_sgi_cfg_status(struct ocelot *ocelot)
2024 {
2025 	return ocelot_read(ocelot, ANA_SG_ACCESS_CTRL);
2026 }
2027 
2028 static int vsc9959_psfp_sgi_set(struct ocelot *ocelot,
2029 				struct felix_stream_gate *sgi)
2030 {
2031 	struct action_gate_entry *e;
2032 	struct timespec64 base_ts;
2033 	u32 interval_sum = 0;
2034 	u32 val;
2035 	int i;
2036 
2037 	if (sgi->index > VSC9959_PSFP_GATE_ID_MAX)
2038 		return -EINVAL;
2039 
2040 	ocelot_write(ocelot, ANA_SG_ACCESS_CTRL_SGID(sgi->index),
2041 		     ANA_SG_ACCESS_CTRL);
2042 
2043 	if (!sgi->enable) {
2044 		ocelot_rmw(ocelot, ANA_SG_CONFIG_REG_3_INIT_GATE_STATE,
2045 			   ANA_SG_CONFIG_REG_3_INIT_GATE_STATE |
2046 			   ANA_SG_CONFIG_REG_3_GATE_ENABLE,
2047 			   ANA_SG_CONFIG_REG_3);
2048 
2049 		return 0;
2050 	}
2051 
2052 	if (sgi->cycletime < VSC9959_PSFP_GATE_CYCLETIME_MIN ||
2053 	    sgi->cycletime > NSEC_PER_SEC)
2054 		return -EINVAL;
2055 
2056 	if (sgi->num_entries > VSC9959_PSFP_GATE_LIST_NUM)
2057 		return -EINVAL;
2058 
2059 	vsc9959_new_base_time(ocelot, sgi->basetime, sgi->cycletime, &base_ts);
2060 	ocelot_write(ocelot, base_ts.tv_nsec, ANA_SG_CONFIG_REG_1);
2061 	val = lower_32_bits(base_ts.tv_sec);
2062 	ocelot_write(ocelot, val, ANA_SG_CONFIG_REG_2);
2063 
2064 	val = upper_32_bits(base_ts.tv_sec);
2065 	ocelot_write(ocelot,
2066 		     (sgi->ipv_valid ? ANA_SG_CONFIG_REG_3_IPV_VALID : 0) |
2067 		     ANA_SG_CONFIG_REG_3_INIT_IPV(sgi->init_ipv) |
2068 		     ANA_SG_CONFIG_REG_3_GATE_ENABLE |
2069 		     ANA_SG_CONFIG_REG_3_LIST_LENGTH(sgi->num_entries) |
2070 		     ANA_SG_CONFIG_REG_3_INIT_GATE_STATE |
2071 		     ANA_SG_CONFIG_REG_3_BASE_TIME_SEC_MSB(val),
2072 		     ANA_SG_CONFIG_REG_3);
2073 
2074 	ocelot_write(ocelot, sgi->cycletime, ANA_SG_CONFIG_REG_4);
2075 
2076 	e = sgi->entries;
2077 	for (i = 0; i < sgi->num_entries; i++) {
2078 		u32 ips = (e[i].ipv < 0) ? 0 : (e[i].ipv + 8);
2079 
2080 		ocelot_write_rix(ocelot, ANA_SG_GCL_GS_CONFIG_IPS(ips) |
2081 				 (e[i].gate_state ?
2082 				  ANA_SG_GCL_GS_CONFIG_GATE_STATE : 0),
2083 				 ANA_SG_GCL_GS_CONFIG, i);
2084 
2085 		interval_sum += e[i].interval;
2086 		ocelot_write_rix(ocelot, interval_sum, ANA_SG_GCL_TI_CONFIG, i);
2087 	}
2088 
2089 	ocelot_rmw(ocelot, ANA_SG_ACCESS_CTRL_CONFIG_CHANGE,
2090 		   ANA_SG_ACCESS_CTRL_CONFIG_CHANGE,
2091 		   ANA_SG_ACCESS_CTRL);
2092 
2093 	return readx_poll_timeout(vsc9959_sgi_cfg_status, ocelot, val,
2094 				  (!(ANA_SG_ACCESS_CTRL_CONFIG_CHANGE & val)),
2095 				  10, 100000);
2096 }
2097 
2098 static int vsc9959_psfp_sgi_table_add(struct ocelot *ocelot,
2099 				      struct felix_stream_gate *sgi)
2100 {
2101 	struct felix_stream_gate_entry *tmp;
2102 	struct ocelot_psfp_list *psfp;
2103 	int ret;
2104 
2105 	psfp = &ocelot->psfp;
2106 
2107 	list_for_each_entry(tmp, &psfp->sgi_list, list)
2108 		if (tmp->index == sgi->index) {
2109 			refcount_inc(&tmp->refcount);
2110 			return 0;
2111 		}
2112 
2113 	tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
2114 	if (!tmp)
2115 		return -ENOMEM;
2116 
2117 	ret = vsc9959_psfp_sgi_set(ocelot, sgi);
2118 	if (ret) {
2119 		kfree(tmp);
2120 		return ret;
2121 	}
2122 
2123 	tmp->index = sgi->index;
2124 	refcount_set(&tmp->refcount, 1);
2125 	list_add_tail(&tmp->list, &psfp->sgi_list);
2126 
2127 	return 0;
2128 }
2129 
2130 static void vsc9959_psfp_sgi_table_del(struct ocelot *ocelot,
2131 				       u32 index)
2132 {
2133 	struct felix_stream_gate_entry *tmp, *n;
2134 	struct felix_stream_gate sgi = {0};
2135 	struct ocelot_psfp_list *psfp;
2136 	u8 z;
2137 
2138 	psfp = &ocelot->psfp;
2139 
2140 	list_for_each_entry_safe(tmp, n, &psfp->sgi_list, list)
2141 		if (tmp->index == index) {
2142 			z = refcount_dec_and_test(&tmp->refcount);
2143 			if (z) {
2144 				sgi.index = index;
2145 				sgi.enable = 0;
2146 				vsc9959_psfp_sgi_set(ocelot, &sgi);
2147 				list_del(&tmp->list);
2148 				kfree(tmp);
2149 			}
2150 			break;
2151 		}
2152 }
2153 
2154 static int vsc9959_psfp_filter_add(struct ocelot *ocelot, int port,
2155 				   struct flow_cls_offload *f)
2156 {
2157 	struct netlink_ext_ack *extack = f->common.extack;
2158 	struct felix_stream_filter old_sfi, *sfi_entry;
2159 	struct felix_stream_filter sfi = {0};
2160 	const struct flow_action_entry *a;
2161 	struct felix_stream *stream_entry;
2162 	struct felix_stream stream = {0};
2163 	struct felix_stream_gate *sgi;
2164 	struct ocelot_psfp_list *psfp;
2165 	struct ocelot_policer pol;
2166 	int ret, i, size;
2167 	u64 rate, burst;
2168 	u32 index;
2169 
2170 	psfp = &ocelot->psfp;
2171 
2172 	ret = vsc9959_stream_identify(f, &stream);
2173 	if (ret) {
2174 		NL_SET_ERR_MSG_MOD(extack, "Only can match on VID, PCP, and dest MAC");
2175 		return ret;
2176 	}
2177 
2178 	mutex_lock(&psfp->lock);
2179 
2180 	flow_action_for_each(i, a, &f->rule->action) {
2181 		switch (a->id) {
2182 		case FLOW_ACTION_GATE:
2183 			size = struct_size(sgi, entries, a->gate.num_entries);
2184 			sgi = kzalloc(size, GFP_KERNEL);
2185 			if (!sgi) {
2186 				ret = -ENOMEM;
2187 				goto err;
2188 			}
2189 			vsc9959_psfp_parse_gate(a, sgi);
2190 			ret = vsc9959_psfp_sgi_table_add(ocelot, sgi);
2191 			if (ret) {
2192 				kfree(sgi);
2193 				goto err;
2194 			}
2195 			sfi.sg_valid = 1;
2196 			sfi.sgid = sgi->index;
2197 			kfree(sgi);
2198 			break;
2199 		case FLOW_ACTION_POLICE:
2200 			index = a->hw_index + VSC9959_PSFP_POLICER_BASE;
2201 			if (index > VSC9959_PSFP_POLICER_MAX) {
2202 				ret = -EINVAL;
2203 				goto err;
2204 			}
2205 
2206 			rate = a->police.rate_bytes_ps;
2207 			burst = rate * PSCHED_NS2TICKS(a->police.burst);
2208 			pol = (struct ocelot_policer) {
2209 				.burst = div_u64(burst, PSCHED_TICKS_PER_SEC),
2210 				.rate = div_u64(rate, 1000) * 8,
2211 			};
2212 			ret = ocelot_vcap_policer_add(ocelot, index, &pol);
2213 			if (ret)
2214 				goto err;
2215 
2216 			sfi.fm_valid = 1;
2217 			sfi.fmid = index;
2218 			sfi.maxsdu = a->police.mtu;
2219 			break;
2220 		default:
2221 			mutex_unlock(&psfp->lock);
2222 			return -EOPNOTSUPP;
2223 		}
2224 	}
2225 
2226 	stream.ports = BIT(port);
2227 	stream.port = port;
2228 
2229 	sfi.portmask = stream.ports;
2230 	sfi.prio_valid = (stream.prio < 0 ? 0 : 1);
2231 	sfi.prio = (sfi.prio_valid ? stream.prio : 0);
2232 	sfi.enable = 1;
2233 
2234 	/* Check if stream is set. */
2235 	stream_entry = vsc9959_stream_table_lookup(&psfp->stream_list, &stream);
2236 	if (stream_entry) {
2237 		if (stream_entry->ports & BIT(port)) {
2238 			NL_SET_ERR_MSG_MOD(extack,
2239 					   "The stream is added on this port");
2240 			ret = -EEXIST;
2241 			goto err;
2242 		}
2243 
2244 		if (stream_entry->ports != BIT(stream_entry->port)) {
2245 			NL_SET_ERR_MSG_MOD(extack,
2246 					   "The stream is added on two ports");
2247 			ret = -EEXIST;
2248 			goto err;
2249 		}
2250 
2251 		stream_entry->ports |= BIT(port);
2252 		stream.ports = stream_entry->ports;
2253 
2254 		sfi_entry = vsc9959_psfp_sfi_table_get(&psfp->sfi_list,
2255 						       stream_entry->sfid);
2256 		memcpy(&old_sfi, sfi_entry, sizeof(old_sfi));
2257 
2258 		vsc9959_psfp_sfi_table_del(ocelot, stream_entry->sfid);
2259 
2260 		old_sfi.portmask = stream_entry->ports;
2261 		sfi.portmask = stream.ports;
2262 
2263 		if (stream_entry->port > port) {
2264 			ret = vsc9959_psfp_sfi_table_add2(ocelot, &sfi,
2265 							  &old_sfi);
2266 			stream_entry->dummy = true;
2267 		} else {
2268 			ret = vsc9959_psfp_sfi_table_add2(ocelot, &old_sfi,
2269 							  &sfi);
2270 			stream.dummy = true;
2271 		}
2272 		if (ret)
2273 			goto err;
2274 
2275 		stream_entry->sfid = old_sfi.index;
2276 	} else {
2277 		ret = vsc9959_psfp_sfi_table_add(ocelot, &sfi);
2278 		if (ret)
2279 			goto err;
2280 	}
2281 
2282 	stream.sfid = sfi.index;
2283 	stream.sfid_valid = 1;
2284 	ret = vsc9959_stream_table_add(ocelot, &psfp->stream_list,
2285 				       &stream, extack);
2286 	if (ret) {
2287 		vsc9959_psfp_sfi_table_del(ocelot, stream.sfid);
2288 		goto err;
2289 	}
2290 
2291 	mutex_unlock(&psfp->lock);
2292 
2293 	return 0;
2294 
2295 err:
2296 	if (sfi.sg_valid)
2297 		vsc9959_psfp_sgi_table_del(ocelot, sfi.sgid);
2298 
2299 	if (sfi.fm_valid)
2300 		ocelot_vcap_policer_del(ocelot, sfi.fmid);
2301 
2302 	mutex_unlock(&psfp->lock);
2303 
2304 	return ret;
2305 }
2306 
2307 static int vsc9959_psfp_filter_del(struct ocelot *ocelot,
2308 				   struct flow_cls_offload *f)
2309 {
2310 	struct felix_stream *stream, tmp, *stream_entry;
2311 	struct ocelot_psfp_list *psfp = &ocelot->psfp;
2312 	static struct felix_stream_filter *sfi;
2313 
2314 	mutex_lock(&psfp->lock);
2315 
2316 	stream = vsc9959_stream_table_get(&psfp->stream_list, f->cookie);
2317 	if (!stream) {
2318 		mutex_unlock(&psfp->lock);
2319 		return -ENOMEM;
2320 	}
2321 
2322 	sfi = vsc9959_psfp_sfi_table_get(&psfp->sfi_list, stream->sfid);
2323 	if (!sfi) {
2324 		mutex_unlock(&psfp->lock);
2325 		return -ENOMEM;
2326 	}
2327 
2328 	if (sfi->sg_valid)
2329 		vsc9959_psfp_sgi_table_del(ocelot, sfi->sgid);
2330 
2331 	if (sfi->fm_valid)
2332 		ocelot_vcap_policer_del(ocelot, sfi->fmid);
2333 
2334 	vsc9959_psfp_sfi_table_del(ocelot, stream->sfid);
2335 
2336 	memcpy(&tmp, stream, sizeof(tmp));
2337 
2338 	stream->sfid_valid = 0;
2339 	vsc9959_stream_table_del(ocelot, stream);
2340 
2341 	stream_entry = vsc9959_stream_table_lookup(&psfp->stream_list, &tmp);
2342 	if (stream_entry) {
2343 		stream_entry->ports = BIT(stream_entry->port);
2344 		if (stream_entry->dummy) {
2345 			stream_entry->dummy = false;
2346 			vsc9959_mact_stream_set(ocelot, stream_entry, NULL);
2347 		}
2348 		vsc9959_psfp_sfidmask_set(ocelot, stream_entry->sfid,
2349 					  stream_entry->ports);
2350 	}
2351 
2352 	mutex_unlock(&psfp->lock);
2353 
2354 	return 0;
2355 }
2356 
2357 static void vsc9959_update_sfid_stats(struct ocelot *ocelot,
2358 				      struct felix_stream_filter *sfi)
2359 {
2360 	struct felix_stream_filter_counters *s = &sfi->stats;
2361 	u32 match, not_pass_gate, not_pass_sdu, red;
2362 	u32 sfid = sfi->index;
2363 
2364 	lockdep_assert_held(&ocelot->stat_view_lock);
2365 
2366 	ocelot_rmw(ocelot, SYS_STAT_CFG_STAT_VIEW(sfid),
2367 		   SYS_STAT_CFG_STAT_VIEW_M,
2368 		   SYS_STAT_CFG);
2369 
2370 	match = ocelot_read(ocelot, SYS_COUNT_SF_MATCHING_FRAMES);
2371 	not_pass_gate = ocelot_read(ocelot, SYS_COUNT_SF_NOT_PASSING_FRAMES);
2372 	not_pass_sdu = ocelot_read(ocelot, SYS_COUNT_SF_NOT_PASSING_SDU);
2373 	red = ocelot_read(ocelot, SYS_COUNT_SF_RED_FRAMES);
2374 
2375 	/* Clear the PSFP counter. */
2376 	ocelot_write(ocelot,
2377 		     SYS_STAT_CFG_STAT_VIEW(sfid) |
2378 		     SYS_STAT_CFG_STAT_CLEAR_SHOT(0x10),
2379 		     SYS_STAT_CFG);
2380 
2381 	s->match += match;
2382 	s->not_pass_gate += not_pass_gate;
2383 	s->not_pass_sdu += not_pass_sdu;
2384 	s->red += red;
2385 }
2386 
2387 /* Caller must hold &ocelot->stat_view_lock */
2388 static void vsc9959_update_stats(struct ocelot *ocelot)
2389 {
2390 	struct ocelot_psfp_list *psfp = &ocelot->psfp;
2391 	struct felix_stream_filter *sfi;
2392 
2393 	mutex_lock(&psfp->lock);
2394 
2395 	list_for_each_entry(sfi, &psfp->sfi_list, list)
2396 		vsc9959_update_sfid_stats(ocelot, sfi);
2397 
2398 	mutex_unlock(&psfp->lock);
2399 }
2400 
2401 static int vsc9959_psfp_stats_get(struct ocelot *ocelot,
2402 				  struct flow_cls_offload *f,
2403 				  struct flow_stats *stats)
2404 {
2405 	struct ocelot_psfp_list *psfp = &ocelot->psfp;
2406 	struct felix_stream_filter_counters *s;
2407 	static struct felix_stream_filter *sfi;
2408 	struct felix_stream *stream;
2409 
2410 	stream = vsc9959_stream_table_get(&psfp->stream_list, f->cookie);
2411 	if (!stream)
2412 		return -ENOMEM;
2413 
2414 	sfi = vsc9959_psfp_sfi_table_get(&psfp->sfi_list, stream->sfid);
2415 	if (!sfi)
2416 		return -EINVAL;
2417 
2418 	mutex_lock(&ocelot->stat_view_lock);
2419 
2420 	vsc9959_update_sfid_stats(ocelot, sfi);
2421 
2422 	s = &sfi->stats;
2423 	stats->pkts = s->match;
2424 	stats->drops = s->not_pass_gate + s->not_pass_sdu + s->red;
2425 
2426 	memset(s, 0, sizeof(*s));
2427 
2428 	mutex_unlock(&ocelot->stat_view_lock);
2429 
2430 	return 0;
2431 }
2432 
2433 static void vsc9959_psfp_init(struct ocelot *ocelot)
2434 {
2435 	struct ocelot_psfp_list *psfp = &ocelot->psfp;
2436 
2437 	INIT_LIST_HEAD(&psfp->stream_list);
2438 	INIT_LIST_HEAD(&psfp->sfi_list);
2439 	INIT_LIST_HEAD(&psfp->sgi_list);
2440 	mutex_init(&psfp->lock);
2441 }
2442 
2443 /* When using cut-through forwarding and the egress port runs at a higher data
2444  * rate than the ingress port, the packet currently under transmission would
2445  * suffer an underrun since it would be transmitted faster than it is received.
2446  * The Felix switch implementation of cut-through forwarding does not check in
2447  * hardware whether this condition is satisfied or not, so we must restrict the
2448  * list of ports that have cut-through forwarding enabled on egress to only be
2449  * the ports operating at the lowest link speed within their respective
2450  * forwarding domain.
2451  */
2452 static void vsc9959_cut_through_fwd(struct ocelot *ocelot)
2453 {
2454 	struct felix *felix = ocelot_to_felix(ocelot);
2455 	struct dsa_switch *ds = felix->ds;
2456 	int tc, port, other_port;
2457 
2458 	lockdep_assert_held(&ocelot->fwd_domain_lock);
2459 
2460 	for (port = 0; port < ocelot->num_phys_ports; port++) {
2461 		struct ocelot_port *ocelot_port = ocelot->ports[port];
2462 		int min_speed = ocelot_port->speed;
2463 		unsigned long mask = 0;
2464 		u32 tmp, val = 0;
2465 
2466 		/* Disable cut-through on ports that are down */
2467 		if (ocelot_port->speed <= 0)
2468 			goto set;
2469 
2470 		if (dsa_is_cpu_port(ds, port)) {
2471 			/* Ocelot switches forward from the NPI port towards
2472 			 * any port, regardless of it being in the NPI port's
2473 			 * forwarding domain or not.
2474 			 */
2475 			mask = dsa_user_ports(ds);
2476 		} else {
2477 			mask = ocelot_get_bridge_fwd_mask(ocelot, port);
2478 			mask &= ~BIT(port);
2479 			if (ocelot->npi >= 0)
2480 				mask |= BIT(ocelot->npi);
2481 			else
2482 				mask |= ocelot_port_assigned_dsa_8021q_cpu_mask(ocelot,
2483 										port);
2484 		}
2485 
2486 		/* Calculate the minimum link speed, among the ports that are
2487 		 * up, of this source port's forwarding domain.
2488 		 */
2489 		for_each_set_bit(other_port, &mask, ocelot->num_phys_ports) {
2490 			struct ocelot_port *other_ocelot_port;
2491 
2492 			other_ocelot_port = ocelot->ports[other_port];
2493 			if (other_ocelot_port->speed <= 0)
2494 				continue;
2495 
2496 			if (min_speed > other_ocelot_port->speed)
2497 				min_speed = other_ocelot_port->speed;
2498 		}
2499 
2500 		/* Enable cut-through forwarding for all traffic classes that
2501 		 * don't have oversized dropping enabled, since this check is
2502 		 * bypassed in cut-through mode.
2503 		 */
2504 		if (ocelot_port->speed == min_speed) {
2505 			val = GENMASK(7, 0);
2506 
2507 			for (tc = 0; tc < OCELOT_NUM_TC; tc++)
2508 				if (vsc9959_port_qmaxsdu_get(ocelot, port, tc))
2509 					val &= ~BIT(tc);
2510 		}
2511 
2512 set:
2513 		tmp = ocelot_read_rix(ocelot, ANA_CUT_THRU_CFG, port);
2514 		if (tmp == val)
2515 			continue;
2516 
2517 		dev_dbg(ocelot->dev,
2518 			"port %d fwd mask 0x%lx speed %d min_speed %d, %s cut-through forwarding on TC mask 0x%x\n",
2519 			port, mask, ocelot_port->speed, min_speed,
2520 			val ? "enabling" : "disabling", val);
2521 
2522 		ocelot_write_rix(ocelot, val, ANA_CUT_THRU_CFG, port);
2523 	}
2524 }
2525 
2526 static const struct ocelot_ops vsc9959_ops = {
2527 	.reset			= vsc9959_reset,
2528 	.wm_enc			= vsc9959_wm_enc,
2529 	.wm_dec			= vsc9959_wm_dec,
2530 	.wm_stat		= vsc9959_wm_stat,
2531 	.port_to_netdev		= felix_port_to_netdev,
2532 	.netdev_to_port		= felix_netdev_to_port,
2533 	.psfp_init		= vsc9959_psfp_init,
2534 	.psfp_filter_add	= vsc9959_psfp_filter_add,
2535 	.psfp_filter_del	= vsc9959_psfp_filter_del,
2536 	.psfp_stats_get		= vsc9959_psfp_stats_get,
2537 	.cut_through_fwd	= vsc9959_cut_through_fwd,
2538 	.tas_clock_adjust	= vsc9959_tas_clock_adjust,
2539 	.update_stats		= vsc9959_update_stats,
2540 };
2541 
2542 static const struct felix_info felix_info_vsc9959 = {
2543 	.resources		= vsc9959_resources,
2544 	.num_resources		= ARRAY_SIZE(vsc9959_resources),
2545 	.resource_names		= vsc9959_resource_names,
2546 	.regfields		= vsc9959_regfields,
2547 	.map			= vsc9959_regmap,
2548 	.ops			= &vsc9959_ops,
2549 	.stats_layout		= vsc9959_stats_layout,
2550 	.vcap			= vsc9959_vcap_props,
2551 	.vcap_pol_base		= VSC9959_VCAP_POLICER_BASE,
2552 	.vcap_pol_max		= VSC9959_VCAP_POLICER_MAX,
2553 	.vcap_pol_base2		= 0,
2554 	.vcap_pol_max2		= 0,
2555 	.num_mact_rows		= 2048,
2556 	.num_ports		= VSC9959_NUM_PORTS,
2557 	.num_tx_queues		= OCELOT_NUM_TC,
2558 	.quirk_no_xtr_irq	= true,
2559 	.ptp_caps		= &vsc9959_ptp_caps,
2560 	.mdio_bus_alloc		= vsc9959_mdio_bus_alloc,
2561 	.mdio_bus_free		= vsc9959_mdio_bus_free,
2562 	.port_modes		= vsc9959_port_modes,
2563 	.port_setup_tc		= vsc9959_port_setup_tc,
2564 	.port_sched_speed_set	= vsc9959_sched_speed_set,
2565 	.tas_guard_bands_update	= vsc9959_tas_guard_bands_update,
2566 };
2567 
2568 static irqreturn_t felix_irq_handler(int irq, void *data)
2569 {
2570 	struct ocelot *ocelot = (struct ocelot *)data;
2571 
2572 	/* The INTB interrupt is used for both PTP TX timestamp interrupt
2573 	 * and preemption status change interrupt on each port.
2574 	 *
2575 	 * - Get txtstamp if have
2576 	 * - TODO: handle preemption. Without handling it, driver may get
2577 	 *   interrupt storm.
2578 	 */
2579 
2580 	ocelot_get_txtstamp(ocelot);
2581 
2582 	return IRQ_HANDLED;
2583 }
2584 
2585 static int felix_pci_probe(struct pci_dev *pdev,
2586 			   const struct pci_device_id *id)
2587 {
2588 	struct dsa_switch *ds;
2589 	struct ocelot *ocelot;
2590 	struct felix *felix;
2591 	int err;
2592 
2593 	if (pdev->dev.of_node && !of_device_is_available(pdev->dev.of_node)) {
2594 		dev_info(&pdev->dev, "device is disabled, skipping\n");
2595 		return -ENODEV;
2596 	}
2597 
2598 	err = pci_enable_device(pdev);
2599 	if (err) {
2600 		dev_err(&pdev->dev, "device enable failed\n");
2601 		goto err_pci_enable;
2602 	}
2603 
2604 	felix = kzalloc(sizeof(struct felix), GFP_KERNEL);
2605 	if (!felix) {
2606 		err = -ENOMEM;
2607 		dev_err(&pdev->dev, "Failed to allocate driver memory\n");
2608 		goto err_alloc_felix;
2609 	}
2610 
2611 	pci_set_drvdata(pdev, felix);
2612 	ocelot = &felix->ocelot;
2613 	ocelot->dev = &pdev->dev;
2614 	ocelot->num_flooding_pgids = OCELOT_NUM_TC;
2615 	felix->info = &felix_info_vsc9959;
2616 	felix->switch_base = pci_resource_start(pdev, VSC9959_SWITCH_PCI_BAR);
2617 
2618 	pci_set_master(pdev);
2619 
2620 	err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL,
2621 					&felix_irq_handler, IRQF_ONESHOT,
2622 					"felix-intb", ocelot);
2623 	if (err) {
2624 		dev_err(&pdev->dev, "Failed to request irq\n");
2625 		goto err_alloc_irq;
2626 	}
2627 
2628 	ocelot->ptp = 1;
2629 
2630 	ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL);
2631 	if (!ds) {
2632 		err = -ENOMEM;
2633 		dev_err(&pdev->dev, "Failed to allocate DSA switch\n");
2634 		goto err_alloc_ds;
2635 	}
2636 
2637 	ds->dev = &pdev->dev;
2638 	ds->num_ports = felix->info->num_ports;
2639 	ds->num_tx_queues = felix->info->num_tx_queues;
2640 	ds->ops = &felix_switch_ops;
2641 	ds->priv = ocelot;
2642 	felix->ds = ds;
2643 	felix->tag_proto = DSA_TAG_PROTO_OCELOT;
2644 
2645 	err = dsa_register_switch(ds);
2646 	if (err) {
2647 		dev_err_probe(&pdev->dev, err, "Failed to register DSA switch\n");
2648 		goto err_register_ds;
2649 	}
2650 
2651 	return 0;
2652 
2653 err_register_ds:
2654 	kfree(ds);
2655 err_alloc_ds:
2656 err_alloc_irq:
2657 	kfree(felix);
2658 err_alloc_felix:
2659 	pci_disable_device(pdev);
2660 err_pci_enable:
2661 	return err;
2662 }
2663 
2664 static void felix_pci_remove(struct pci_dev *pdev)
2665 {
2666 	struct felix *felix = pci_get_drvdata(pdev);
2667 
2668 	if (!felix)
2669 		return;
2670 
2671 	dsa_unregister_switch(felix->ds);
2672 
2673 	kfree(felix->ds);
2674 	kfree(felix);
2675 
2676 	pci_disable_device(pdev);
2677 }
2678 
2679 static void felix_pci_shutdown(struct pci_dev *pdev)
2680 {
2681 	struct felix *felix = pci_get_drvdata(pdev);
2682 
2683 	if (!felix)
2684 		return;
2685 
2686 	dsa_switch_shutdown(felix->ds);
2687 
2688 	pci_set_drvdata(pdev, NULL);
2689 }
2690 
2691 static struct pci_device_id felix_ids[] = {
2692 	{
2693 		/* NXP LS1028A */
2694 		PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0xEEF0),
2695 	},
2696 	{ 0, }
2697 };
2698 MODULE_DEVICE_TABLE(pci, felix_ids);
2699 
2700 static struct pci_driver felix_vsc9959_pci_driver = {
2701 	.name		= "mscc_felix",
2702 	.id_table	= felix_ids,
2703 	.probe		= felix_pci_probe,
2704 	.remove		= felix_pci_remove,
2705 	.shutdown	= felix_pci_shutdown,
2706 };
2707 module_pci_driver(felix_vsc9959_pci_driver);
2708 
2709 MODULE_DESCRIPTION("Felix Switch driver");
2710 MODULE_LICENSE("GPL v2");
2711