blob: 110d6c403bdd2aa7b23395272bf9d677a0b1049e [file] [log] [blame]
Vladimir Oltean56051942019-11-14 17:03:30 +02001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/* Copyright 2017 Microsemi Corporation
Vladimir Oltean3c9cfb52021-09-17 14:17:35 +03003 * Copyright 2018-2019 NXP
Vladimir Oltean56051942019-11-14 17:03:30 +02004 */
Vladimir Olteanbdeced72020-01-06 03:34:17 +02005#include <linux/fsl/enetc_mdio.h>
Xiaoliang Yangde143c02020-05-13 10:25:09 +08006#include <soc/mscc/ocelot_qsys.h>
Vladimir Oltean07d985e2020-02-29 16:31:14 +02007#include <soc/mscc/ocelot_vcap.h>
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08008#include <soc/mscc/ocelot_ana.h>
Xiaoliang Yangde143c02020-05-13 10:25:09 +08009#include <soc/mscc/ocelot_ptp.h>
Vladimir Oltean56051942019-11-14 17:03:30 +020010#include <soc/mscc/ocelot_sys.h>
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +080011#include <net/tc_act/tc_gate.h>
Vladimir Oltean56051942019-11-14 17:03:30 +020012#include <soc/mscc/ocelot.h>
Vladimir Oltean40d3f292021-02-14 00:37:56 +020013#include <linux/dsa/ocelot.h>
Ioana Ciornei588d0552020-08-30 11:34:02 +030014#include <linux/pcs-lynx.h>
Xiaoliang Yangde143c02020-05-13 10:25:09 +080015#include <net/pkt_sched.h>
Vladimir Oltean56051942019-11-14 17:03:30 +020016#include <linux/iopoll.h>
Michael Walle16659b82020-07-20 00:03:34 +020017#include <linux/mdio.h>
Vladimir Oltean56051942019-11-14 17:03:30 +020018#include <linux/pci.h>
19#include "felix.h"
20
Xiaoliang Yangde143c02020-05-13 10:25:09 +080021#define VSC9959_TAS_GCL_ENTRY_MAX 63
Xiaoliang Yang77043c32021-11-18 18:12:02 +080022#define VSC9959_VCAP_POLICER_BASE 63
23#define VSC9959_VCAP_POLICER_MAX 383
Colin Fosterc9910482021-12-07 09:00:27 -080024#define VSC9959_SWITCH_PCI_BAR 4
25#define VSC9959_IMDIO_PCI_BAR 0
Xiaoliang Yangde143c02020-05-13 10:25:09 +080026
Vladimir Oltean56051942019-11-14 17:03:30 +020027static const u32 vsc9959_ana_regmap[] = {
28 REG(ANA_ADVLEARN, 0x0089a0),
29 REG(ANA_VLANMASK, 0x0089a4),
30 REG_RESERVED(ANA_PORT_B_DOMAIN),
31 REG(ANA_ANAGEFIL, 0x0089ac),
32 REG(ANA_ANEVENTS, 0x0089b0),
33 REG(ANA_STORMLIMIT_BURST, 0x0089b4),
34 REG(ANA_STORMLIMIT_CFG, 0x0089b8),
35 REG(ANA_ISOLATED_PORTS, 0x0089c8),
36 REG(ANA_COMMUNITY_PORTS, 0x0089cc),
37 REG(ANA_AUTOAGE, 0x0089d0),
38 REG(ANA_MACTOPTIONS, 0x0089d4),
39 REG(ANA_LEARNDISC, 0x0089d8),
40 REG(ANA_AGENCTRL, 0x0089dc),
41 REG(ANA_MIRRORPORTS, 0x0089e0),
42 REG(ANA_EMIRRORPORTS, 0x0089e4),
43 REG(ANA_FLOODING, 0x0089e8),
44 REG(ANA_FLOODING_IPMC, 0x008a08),
45 REG(ANA_SFLOW_CFG, 0x008a0c),
46 REG(ANA_PORT_MODE, 0x008a28),
47 REG(ANA_CUT_THRU_CFG, 0x008a48),
48 REG(ANA_PGID_PGID, 0x008400),
49 REG(ANA_TABLES_ANMOVED, 0x007f1c),
50 REG(ANA_TABLES_MACHDATA, 0x007f20),
51 REG(ANA_TABLES_MACLDATA, 0x007f24),
52 REG(ANA_TABLES_STREAMDATA, 0x007f28),
53 REG(ANA_TABLES_MACACCESS, 0x007f2c),
54 REG(ANA_TABLES_MACTINDX, 0x007f30),
55 REG(ANA_TABLES_VLANACCESS, 0x007f34),
56 REG(ANA_TABLES_VLANTIDX, 0x007f38),
57 REG(ANA_TABLES_ISDXACCESS, 0x007f3c),
58 REG(ANA_TABLES_ISDXTIDX, 0x007f40),
59 REG(ANA_TABLES_ENTRYLIM, 0x007f00),
60 REG(ANA_TABLES_PTP_ID_HIGH, 0x007f44),
61 REG(ANA_TABLES_PTP_ID_LOW, 0x007f48),
62 REG(ANA_TABLES_STREAMACCESS, 0x007f4c),
63 REG(ANA_TABLES_STREAMTIDX, 0x007f50),
64 REG(ANA_TABLES_SEQ_HISTORY, 0x007f54),
65 REG(ANA_TABLES_SEQ_MASK, 0x007f58),
66 REG(ANA_TABLES_SFID_MASK, 0x007f5c),
67 REG(ANA_TABLES_SFIDACCESS, 0x007f60),
68 REG(ANA_TABLES_SFIDTIDX, 0x007f64),
69 REG(ANA_MSTI_STATE, 0x008600),
70 REG(ANA_OAM_UPM_LM_CNT, 0x008000),
71 REG(ANA_SG_ACCESS_CTRL, 0x008a64),
72 REG(ANA_SG_CONFIG_REG_1, 0x007fb0),
73 REG(ANA_SG_CONFIG_REG_2, 0x007fb4),
74 REG(ANA_SG_CONFIG_REG_3, 0x007fb8),
75 REG(ANA_SG_CONFIG_REG_4, 0x007fbc),
76 REG(ANA_SG_CONFIG_REG_5, 0x007fc0),
77 REG(ANA_SG_GCL_GS_CONFIG, 0x007f80),
78 REG(ANA_SG_GCL_TI_CONFIG, 0x007f90),
79 REG(ANA_SG_STATUS_REG_1, 0x008980),
80 REG(ANA_SG_STATUS_REG_2, 0x008984),
81 REG(ANA_SG_STATUS_REG_3, 0x008988),
82 REG(ANA_PORT_VLAN_CFG, 0x007800),
83 REG(ANA_PORT_DROP_CFG, 0x007804),
84 REG(ANA_PORT_QOS_CFG, 0x007808),
85 REG(ANA_PORT_VCAP_CFG, 0x00780c),
86 REG(ANA_PORT_VCAP_S1_KEY_CFG, 0x007810),
87 REG(ANA_PORT_VCAP_S2_CFG, 0x00781c),
88 REG(ANA_PORT_PCP_DEI_MAP, 0x007820),
89 REG(ANA_PORT_CPU_FWD_CFG, 0x007860),
90 REG(ANA_PORT_CPU_FWD_BPDU_CFG, 0x007864),
91 REG(ANA_PORT_CPU_FWD_GARP_CFG, 0x007868),
92 REG(ANA_PORT_CPU_FWD_CCM_CFG, 0x00786c),
93 REG(ANA_PORT_PORT_CFG, 0x007870),
94 REG(ANA_PORT_POL_CFG, 0x007874),
95 REG(ANA_PORT_PTP_CFG, 0x007878),
96 REG(ANA_PORT_PTP_DLY1_CFG, 0x00787c),
97 REG(ANA_PORT_PTP_DLY2_CFG, 0x007880),
98 REG(ANA_PORT_SFID_CFG, 0x007884),
99 REG(ANA_PFC_PFC_CFG, 0x008800),
100 REG_RESERVED(ANA_PFC_PFC_TIMER),
101 REG_RESERVED(ANA_IPT_OAM_MEP_CFG),
102 REG_RESERVED(ANA_IPT_IPT),
103 REG_RESERVED(ANA_PPT_PPT),
104 REG_RESERVED(ANA_FID_MAP_FID_MAP),
105 REG(ANA_AGGR_CFG, 0x008a68),
106 REG(ANA_CPUQ_CFG, 0x008a6c),
107 REG_RESERVED(ANA_CPUQ_CFG2),
108 REG(ANA_CPUQ_8021_CFG, 0x008a74),
109 REG(ANA_DSCP_CFG, 0x008ab4),
110 REG(ANA_DSCP_REWR_CFG, 0x008bb4),
111 REG(ANA_VCAP_RNG_TYPE_CFG, 0x008bf4),
112 REG(ANA_VCAP_RNG_VAL_CFG, 0x008c14),
113 REG_RESERVED(ANA_VRAP_CFG),
114 REG_RESERVED(ANA_VRAP_HDR_DATA),
115 REG_RESERVED(ANA_VRAP_HDR_MASK),
116 REG(ANA_DISCARD_CFG, 0x008c40),
117 REG(ANA_FID_CFG, 0x008c44),
118 REG(ANA_POL_PIR_CFG, 0x004000),
119 REG(ANA_POL_CIR_CFG, 0x004004),
120 REG(ANA_POL_MODE_CFG, 0x004008),
121 REG(ANA_POL_PIR_STATE, 0x00400c),
122 REG(ANA_POL_CIR_STATE, 0x004010),
123 REG_RESERVED(ANA_POL_STATE),
124 REG(ANA_POL_FLOWC, 0x008c48),
125 REG(ANA_POL_HYST, 0x008cb4),
126 REG_RESERVED(ANA_POL_MISC_CFG),
127};
128
129static const u32 vsc9959_qs_regmap[] = {
130 REG(QS_XTR_GRP_CFG, 0x000000),
131 REG(QS_XTR_RD, 0x000008),
132 REG(QS_XTR_FRM_PRUNING, 0x000010),
133 REG(QS_XTR_FLUSH, 0x000018),
134 REG(QS_XTR_DATA_PRESENT, 0x00001c),
135 REG(QS_XTR_CFG, 0x000020),
136 REG(QS_INJ_GRP_CFG, 0x000024),
137 REG(QS_INJ_WR, 0x00002c),
138 REG(QS_INJ_CTRL, 0x000034),
139 REG(QS_INJ_STATUS, 0x00003c),
140 REG(QS_INJ_ERR, 0x000040),
141 REG_RESERVED(QS_INH_DBG),
142};
143
Vladimir Olteanc1c39932020-09-30 01:27:23 +0300144static const u32 vsc9959_vcap_regmap[] = {
145 /* VCAP_CORE_CFG */
146 REG(VCAP_CORE_UPDATE_CTRL, 0x000000),
147 REG(VCAP_CORE_MV_CFG, 0x000004),
148 /* VCAP_CORE_CACHE */
149 REG(VCAP_CACHE_ENTRY_DAT, 0x000008),
150 REG(VCAP_CACHE_MASK_DAT, 0x000108),
151 REG(VCAP_CACHE_ACTION_DAT, 0x000208),
152 REG(VCAP_CACHE_CNT_DAT, 0x000308),
153 REG(VCAP_CACHE_TG_DAT, 0x000388),
Vladimir Oltean20968052020-09-30 01:27:26 +0300154 /* VCAP_CONST */
155 REG(VCAP_CONST_VCAP_VER, 0x000398),
156 REG(VCAP_CONST_ENTRY_WIDTH, 0x00039c),
157 REG(VCAP_CONST_ENTRY_CNT, 0x0003a0),
158 REG(VCAP_CONST_ENTRY_SWCNT, 0x0003a4),
159 REG(VCAP_CONST_ENTRY_TG_WIDTH, 0x0003a8),
160 REG(VCAP_CONST_ACTION_DEF_CNT, 0x0003ac),
161 REG(VCAP_CONST_ACTION_WIDTH, 0x0003b0),
162 REG(VCAP_CONST_CNT_WIDTH, 0x0003b4),
163 REG(VCAP_CONST_CORE_CNT, 0x0003b8),
164 REG(VCAP_CONST_IF_CNT, 0x0003bc),
Vladimir Oltean56051942019-11-14 17:03:30 +0200165};
166
167static const u32 vsc9959_qsys_regmap[] = {
168 REG(QSYS_PORT_MODE, 0x00f460),
169 REG(QSYS_SWITCH_PORT_MODE, 0x00f480),
170 REG(QSYS_STAT_CNT_CFG, 0x00f49c),
171 REG(QSYS_EEE_CFG, 0x00f4a0),
172 REG(QSYS_EEE_THRES, 0x00f4b8),
173 REG(QSYS_IGR_NO_SHARING, 0x00f4bc),
174 REG(QSYS_EGR_NO_SHARING, 0x00f4c0),
175 REG(QSYS_SW_STATUS, 0x00f4c4),
176 REG(QSYS_EXT_CPU_CFG, 0x00f4e0),
177 REG_RESERVED(QSYS_PAD_CFG),
178 REG(QSYS_CPU_GROUP_MAP, 0x00f4e8),
179 REG_RESERVED(QSYS_QMAP),
180 REG_RESERVED(QSYS_ISDX_SGRP),
181 REG_RESERVED(QSYS_TIMED_FRAME_ENTRY),
182 REG(QSYS_TFRM_MISC, 0x00f50c),
183 REG(QSYS_TFRM_PORT_DLY, 0x00f510),
184 REG(QSYS_TFRM_TIMER_CFG_1, 0x00f514),
185 REG(QSYS_TFRM_TIMER_CFG_2, 0x00f518),
186 REG(QSYS_TFRM_TIMER_CFG_3, 0x00f51c),
187 REG(QSYS_TFRM_TIMER_CFG_4, 0x00f520),
188 REG(QSYS_TFRM_TIMER_CFG_5, 0x00f524),
189 REG(QSYS_TFRM_TIMER_CFG_6, 0x00f528),
190 REG(QSYS_TFRM_TIMER_CFG_7, 0x00f52c),
191 REG(QSYS_TFRM_TIMER_CFG_8, 0x00f530),
192 REG(QSYS_RED_PROFILE, 0x00f534),
193 REG(QSYS_RES_QOS_MODE, 0x00f574),
194 REG(QSYS_RES_CFG, 0x00c000),
195 REG(QSYS_RES_STAT, 0x00c004),
196 REG(QSYS_EGR_DROP_MODE, 0x00f578),
197 REG(QSYS_EQ_CTRL, 0x00f57c),
198 REG_RESERVED(QSYS_EVENTS_CORE),
199 REG(QSYS_QMAXSDU_CFG_0, 0x00f584),
200 REG(QSYS_QMAXSDU_CFG_1, 0x00f5a0),
201 REG(QSYS_QMAXSDU_CFG_2, 0x00f5bc),
202 REG(QSYS_QMAXSDU_CFG_3, 0x00f5d8),
203 REG(QSYS_QMAXSDU_CFG_4, 0x00f5f4),
204 REG(QSYS_QMAXSDU_CFG_5, 0x00f610),
205 REG(QSYS_QMAXSDU_CFG_6, 0x00f62c),
206 REG(QSYS_QMAXSDU_CFG_7, 0x00f648),
207 REG(QSYS_PREEMPTION_CFG, 0x00f664),
Xiaoliang Yang0fbabf82020-05-13 10:25:10 +0800208 REG(QSYS_CIR_CFG, 0x000000),
Vladimir Oltean56051942019-11-14 17:03:30 +0200209 REG(QSYS_EIR_CFG, 0x000004),
210 REG(QSYS_SE_CFG, 0x000008),
211 REG(QSYS_SE_DWRR_CFG, 0x00000c),
212 REG_RESERVED(QSYS_SE_CONNECT),
213 REG(QSYS_SE_DLB_SENSE, 0x000040),
214 REG(QSYS_CIR_STATE, 0x000044),
215 REG(QSYS_EIR_STATE, 0x000048),
216 REG_RESERVED(QSYS_SE_STATE),
217 REG(QSYS_HSCH_MISC_CFG, 0x00f67c),
218 REG(QSYS_TAG_CONFIG, 0x00f680),
219 REG(QSYS_TAS_PARAM_CFG_CTRL, 0x00f698),
220 REG(QSYS_PORT_MAX_SDU, 0x00f69c),
221 REG(QSYS_PARAM_CFG_REG_1, 0x00f440),
222 REG(QSYS_PARAM_CFG_REG_2, 0x00f444),
223 REG(QSYS_PARAM_CFG_REG_3, 0x00f448),
224 REG(QSYS_PARAM_CFG_REG_4, 0x00f44c),
225 REG(QSYS_PARAM_CFG_REG_5, 0x00f450),
226 REG(QSYS_GCL_CFG_REG_1, 0x00f454),
227 REG(QSYS_GCL_CFG_REG_2, 0x00f458),
228 REG(QSYS_PARAM_STATUS_REG_1, 0x00f400),
229 REG(QSYS_PARAM_STATUS_REG_2, 0x00f404),
230 REG(QSYS_PARAM_STATUS_REG_3, 0x00f408),
231 REG(QSYS_PARAM_STATUS_REG_4, 0x00f40c),
232 REG(QSYS_PARAM_STATUS_REG_5, 0x00f410),
233 REG(QSYS_PARAM_STATUS_REG_6, 0x00f414),
234 REG(QSYS_PARAM_STATUS_REG_7, 0x00f418),
235 REG(QSYS_PARAM_STATUS_REG_8, 0x00f41c),
236 REG(QSYS_PARAM_STATUS_REG_9, 0x00f420),
237 REG(QSYS_GCL_STATUS_REG_1, 0x00f424),
238 REG(QSYS_GCL_STATUS_REG_2, 0x00f428),
239};
240
241static const u32 vsc9959_rew_regmap[] = {
242 REG(REW_PORT_VLAN_CFG, 0x000000),
243 REG(REW_TAG_CFG, 0x000004),
244 REG(REW_PORT_CFG, 0x000008),
245 REG(REW_DSCP_CFG, 0x00000c),
246 REG(REW_PCP_DEI_QOS_MAP_CFG, 0x000010),
247 REG(REW_PTP_CFG, 0x000050),
248 REG(REW_PTP_DLY1_CFG, 0x000054),
249 REG(REW_RED_TAG_CFG, 0x000058),
250 REG(REW_DSCP_REMAP_DP1_CFG, 0x000410),
251 REG(REW_DSCP_REMAP_CFG, 0x000510),
252 REG_RESERVED(REW_STAT_CFG),
253 REG_RESERVED(REW_REW_STICKY),
254 REG_RESERVED(REW_PPT),
255};
256
257static const u32 vsc9959_sys_regmap[] = {
258 REG(SYS_COUNT_RX_OCTETS, 0x000000),
259 REG(SYS_COUNT_RX_MULTICAST, 0x000008),
260 REG(SYS_COUNT_RX_SHORTS, 0x000010),
261 REG(SYS_COUNT_RX_FRAGMENTS, 0x000014),
262 REG(SYS_COUNT_RX_JABBERS, 0x000018),
263 REG(SYS_COUNT_RX_64, 0x000024),
264 REG(SYS_COUNT_RX_65_127, 0x000028),
265 REG(SYS_COUNT_RX_128_255, 0x00002c),
266 REG(SYS_COUNT_RX_256_1023, 0x000030),
267 REG(SYS_COUNT_RX_1024_1526, 0x000034),
268 REG(SYS_COUNT_RX_1527_MAX, 0x000038),
269 REG(SYS_COUNT_RX_LONGS, 0x000044),
270 REG(SYS_COUNT_TX_OCTETS, 0x000200),
271 REG(SYS_COUNT_TX_COLLISION, 0x000210),
272 REG(SYS_COUNT_TX_DROPS, 0x000214),
273 REG(SYS_COUNT_TX_64, 0x00021c),
274 REG(SYS_COUNT_TX_65_127, 0x000220),
275 REG(SYS_COUNT_TX_128_511, 0x000224),
276 REG(SYS_COUNT_TX_512_1023, 0x000228),
277 REG(SYS_COUNT_TX_1024_1526, 0x00022c),
278 REG(SYS_COUNT_TX_1527_MAX, 0x000230),
279 REG(SYS_COUNT_TX_AGING, 0x000278),
280 REG(SYS_RESET_CFG, 0x000e00),
281 REG(SYS_SR_ETYPE_CFG, 0x000e04),
282 REG(SYS_VLAN_ETYPE_CFG, 0x000e08),
283 REG(SYS_PORT_MODE, 0x000e0c),
284 REG(SYS_FRONT_PORT_MODE, 0x000e2c),
285 REG(SYS_FRM_AGING, 0x000e44),
286 REG(SYS_STAT_CFG, 0x000e48),
287 REG(SYS_SW_STATUS, 0x000e4c),
288 REG_RESERVED(SYS_MISC_CFG),
289 REG(SYS_REW_MAC_HIGH_CFG, 0x000e6c),
290 REG(SYS_REW_MAC_LOW_CFG, 0x000e84),
291 REG(SYS_TIMESTAMP_OFFSET, 0x000e9c),
292 REG(SYS_PAUSE_CFG, 0x000ea0),
293 REG(SYS_PAUSE_TOT_CFG, 0x000ebc),
294 REG(SYS_ATOP, 0x000ec0),
295 REG(SYS_ATOP_TOT_CFG, 0x000edc),
296 REG(SYS_MAC_FC_CFG, 0x000ee0),
297 REG(SYS_MMGT, 0x000ef8),
298 REG_RESERVED(SYS_MMGT_FAST),
299 REG_RESERVED(SYS_EVENTS_DIF),
300 REG_RESERVED(SYS_EVENTS_CORE),
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +0800301 REG(SYS_CNT, 0x000000),
Vladimir Oltean56051942019-11-14 17:03:30 +0200302 REG(SYS_PTP_STATUS, 0x000f14),
303 REG(SYS_PTP_TXSTAMP, 0x000f18),
304 REG(SYS_PTP_NXT, 0x000f1c),
305 REG(SYS_PTP_CFG, 0x000f20),
306 REG(SYS_RAM_INIT, 0x000f24),
307 REG_RESERVED(SYS_CM_ADDR),
308 REG_RESERVED(SYS_CM_DATA_WR),
309 REG_RESERVED(SYS_CM_DATA_RD),
310 REG_RESERVED(SYS_CM_OP),
311 REG_RESERVED(SYS_CM_DATA),
312};
313
Yangbo Lu5df66c42019-11-20 16:23:17 +0800314static const u32 vsc9959_ptp_regmap[] = {
Vladimir Olteanf8320ec2020-09-18 13:57:49 +0300315 REG(PTP_PIN_CFG, 0x000000),
316 REG(PTP_PIN_TOD_SEC_MSB, 0x000004),
317 REG(PTP_PIN_TOD_SEC_LSB, 0x000008),
318 REG(PTP_PIN_TOD_NSEC, 0x00000c),
319 REG(PTP_PIN_WF_HIGH_PERIOD, 0x000014),
320 REG(PTP_PIN_WF_LOW_PERIOD, 0x000018),
321 REG(PTP_CFG_MISC, 0x0000a0),
322 REG(PTP_CLK_CFG_ADJ_CFG, 0x0000a4),
323 REG(PTP_CLK_CFG_ADJ_FREQ, 0x0000a8),
Yangbo Lu5df66c42019-11-20 16:23:17 +0800324};
325
Vladimir Oltean56051942019-11-14 17:03:30 +0200326static const u32 vsc9959_gcb_regmap[] = {
327 REG(GCB_SOFT_RST, 0x000004),
328};
329
Vladimir Oltean91c724c2020-07-13 19:57:01 +0300330static const u32 vsc9959_dev_gmii_regmap[] = {
331 REG(DEV_CLOCK_CFG, 0x0),
332 REG(DEV_PORT_MISC, 0x4),
333 REG(DEV_EVENTS, 0x8),
334 REG(DEV_EEE_CFG, 0xc),
335 REG(DEV_RX_PATH_DELAY, 0x10),
336 REG(DEV_TX_PATH_DELAY, 0x14),
337 REG(DEV_PTP_PREDICT_CFG, 0x18),
338 REG(DEV_MAC_ENA_CFG, 0x1c),
339 REG(DEV_MAC_MODE_CFG, 0x20),
340 REG(DEV_MAC_MAXLEN_CFG, 0x24),
341 REG(DEV_MAC_TAGS_CFG, 0x28),
342 REG(DEV_MAC_ADV_CHK_CFG, 0x2c),
343 REG(DEV_MAC_IFG_CFG, 0x30),
344 REG(DEV_MAC_HDX_CFG, 0x34),
345 REG(DEV_MAC_DBG_CFG, 0x38),
346 REG(DEV_MAC_FC_MAC_LOW_CFG, 0x3c),
347 REG(DEV_MAC_FC_MAC_HIGH_CFG, 0x40),
348 REG(DEV_MAC_STICKY, 0x44),
349 REG_RESERVED(PCS1G_CFG),
350 REG_RESERVED(PCS1G_MODE_CFG),
351 REG_RESERVED(PCS1G_SD_CFG),
352 REG_RESERVED(PCS1G_ANEG_CFG),
353 REG_RESERVED(PCS1G_ANEG_NP_CFG),
354 REG_RESERVED(PCS1G_LB_CFG),
355 REG_RESERVED(PCS1G_DBG_CFG),
356 REG_RESERVED(PCS1G_CDET_CFG),
357 REG_RESERVED(PCS1G_ANEG_STATUS),
358 REG_RESERVED(PCS1G_ANEG_NP_STATUS),
359 REG_RESERVED(PCS1G_LINK_STATUS),
360 REG_RESERVED(PCS1G_LINK_DOWN_CNT),
361 REG_RESERVED(PCS1G_STICKY),
362 REG_RESERVED(PCS1G_DEBUG_STATUS),
363 REG_RESERVED(PCS1G_LPI_CFG),
364 REG_RESERVED(PCS1G_LPI_WAKE_ERROR_CNT),
365 REG_RESERVED(PCS1G_LPI_STATUS),
366 REG_RESERVED(PCS1G_TSTPAT_MODE_CFG),
367 REG_RESERVED(PCS1G_TSTPAT_STATUS),
368 REG_RESERVED(DEV_PCS_FX100_CFG),
369 REG_RESERVED(DEV_PCS_FX100_STATUS),
370};
371
372static const u32 *vsc9959_regmap[TARGET_MAX] = {
Vladimir Oltean56051942019-11-14 17:03:30 +0200373 [ANA] = vsc9959_ana_regmap,
374 [QS] = vsc9959_qs_regmap,
375 [QSYS] = vsc9959_qsys_regmap,
376 [REW] = vsc9959_rew_regmap,
377 [SYS] = vsc9959_sys_regmap,
Vladimir Olteane3aea292020-09-30 01:27:25 +0300378 [S0] = vsc9959_vcap_regmap,
Vladimir Olteana61e3652020-09-30 01:27:24 +0300379 [S1] = vsc9959_vcap_regmap,
Vladimir Olteanc1c39932020-09-30 01:27:23 +0300380 [S2] = vsc9959_vcap_regmap,
Yangbo Lu5df66c42019-11-20 16:23:17 +0800381 [PTP] = vsc9959_ptp_regmap,
Vladimir Oltean56051942019-11-14 17:03:30 +0200382 [GCB] = vsc9959_gcb_regmap,
Vladimir Oltean91c724c2020-07-13 19:57:01 +0300383 [DEV_GMII] = vsc9959_dev_gmii_regmap,
Vladimir Oltean56051942019-11-14 17:03:30 +0200384};
385
Claudiu Manoilb4024c92020-05-22 11:54:34 +0300386/* Addresses are relative to the PCI device's base address */
Vladimir Oltean91c724c2020-07-13 19:57:01 +0300387static const struct resource vsc9959_target_io_res[TARGET_MAX] = {
Vladimir Oltean56051942019-11-14 17:03:30 +0200388 [ANA] = {
389 .start = 0x0280000,
390 .end = 0x028ffff,
391 .name = "ana",
392 },
393 [QS] = {
394 .start = 0x0080000,
395 .end = 0x00800ff,
396 .name = "qs",
397 },
398 [QSYS] = {
399 .start = 0x0200000,
400 .end = 0x021ffff,
401 .name = "qsys",
402 },
403 [REW] = {
404 .start = 0x0030000,
405 .end = 0x003ffff,
406 .name = "rew",
407 },
408 [SYS] = {
409 .start = 0x0010000,
410 .end = 0x001ffff,
411 .name = "sys",
412 },
Vladimir Olteane3aea292020-09-30 01:27:25 +0300413 [S0] = {
414 .start = 0x0040000,
415 .end = 0x00403ff,
416 .name = "s0",
417 },
Vladimir Olteana61e3652020-09-30 01:27:24 +0300418 [S1] = {
419 .start = 0x0050000,
420 .end = 0x00503ff,
421 .name = "s1",
422 },
Vladimir Oltean56051942019-11-14 17:03:30 +0200423 [S2] = {
424 .start = 0x0060000,
425 .end = 0x00603ff,
426 .name = "s2",
427 },
Yangbo Lu5df66c42019-11-20 16:23:17 +0800428 [PTP] = {
429 .start = 0x0090000,
430 .end = 0x00900cb,
431 .name = "ptp",
432 },
Vladimir Oltean56051942019-11-14 17:03:30 +0200433 [GCB] = {
434 .start = 0x0070000,
435 .end = 0x00701ff,
436 .name = "devcpu_gcb",
437 },
438};
439
Claudiu Manoilb4024c92020-05-22 11:54:34 +0300440static const struct resource vsc9959_port_io_res[] = {
Vladimir Oltean56051942019-11-14 17:03:30 +0200441 {
442 .start = 0x0100000,
443 .end = 0x010ffff,
444 .name = "port0",
445 },
446 {
447 .start = 0x0110000,
448 .end = 0x011ffff,
449 .name = "port1",
450 },
451 {
452 .start = 0x0120000,
453 .end = 0x012ffff,
454 .name = "port2",
455 },
456 {
457 .start = 0x0130000,
458 .end = 0x013ffff,
459 .name = "port3",
460 },
461 {
462 .start = 0x0140000,
463 .end = 0x014ffff,
464 .name = "port4",
465 },
466 {
467 .start = 0x0150000,
468 .end = 0x015ffff,
469 .name = "port5",
470 },
471};
472
Vladimir Olteanbdeced72020-01-06 03:34:17 +0200473/* Port MAC 0 Internal MDIO bus through which the SerDes acting as an
474 * SGMII/QSGMII MAC PCS can be found.
475 */
Claudiu Manoilb4024c92020-05-22 11:54:34 +0300476static const struct resource vsc9959_imdio_res = {
Vladimir Olteanbdeced72020-01-06 03:34:17 +0200477 .start = 0x8030,
478 .end = 0x8040,
479 .name = "imdio",
480};
481
Maxim Kochetkov27896582020-07-13 19:57:02 +0300482static const struct reg_field vsc9959_regfields[REGFIELD_MAX] = {
Vladimir Oltean56051942019-11-14 17:03:30 +0200483 [ANA_ADVLEARN_VLAN_CHK] = REG_FIELD(ANA_ADVLEARN, 6, 6),
484 [ANA_ADVLEARN_LEARN_MIRROR] = REG_FIELD(ANA_ADVLEARN, 0, 5),
485 [ANA_ANEVENTS_FLOOD_DISCARD] = REG_FIELD(ANA_ANEVENTS, 30, 30),
486 [ANA_ANEVENTS_AUTOAGE] = REG_FIELD(ANA_ANEVENTS, 26, 26),
487 [ANA_ANEVENTS_STORM_DROP] = REG_FIELD(ANA_ANEVENTS, 24, 24),
488 [ANA_ANEVENTS_LEARN_DROP] = REG_FIELD(ANA_ANEVENTS, 23, 23),
489 [ANA_ANEVENTS_AGED_ENTRY] = REG_FIELD(ANA_ANEVENTS, 22, 22),
490 [ANA_ANEVENTS_CPU_LEARN_FAILED] = REG_FIELD(ANA_ANEVENTS, 21, 21),
491 [ANA_ANEVENTS_AUTO_LEARN_FAILED] = REG_FIELD(ANA_ANEVENTS, 20, 20),
492 [ANA_ANEVENTS_LEARN_REMOVE] = REG_FIELD(ANA_ANEVENTS, 19, 19),
493 [ANA_ANEVENTS_AUTO_LEARNED] = REG_FIELD(ANA_ANEVENTS, 18, 18),
494 [ANA_ANEVENTS_AUTO_MOVED] = REG_FIELD(ANA_ANEVENTS, 17, 17),
495 [ANA_ANEVENTS_CLASSIFIED_DROP] = REG_FIELD(ANA_ANEVENTS, 15, 15),
496 [ANA_ANEVENTS_CLASSIFIED_COPY] = REG_FIELD(ANA_ANEVENTS, 14, 14),
497 [ANA_ANEVENTS_VLAN_DISCARD] = REG_FIELD(ANA_ANEVENTS, 13, 13),
498 [ANA_ANEVENTS_FWD_DISCARD] = REG_FIELD(ANA_ANEVENTS, 12, 12),
499 [ANA_ANEVENTS_MULTICAST_FLOOD] = REG_FIELD(ANA_ANEVENTS, 11, 11),
500 [ANA_ANEVENTS_UNICAST_FLOOD] = REG_FIELD(ANA_ANEVENTS, 10, 10),
501 [ANA_ANEVENTS_DEST_KNOWN] = REG_FIELD(ANA_ANEVENTS, 9, 9),
502 [ANA_ANEVENTS_BUCKET3_MATCH] = REG_FIELD(ANA_ANEVENTS, 8, 8),
503 [ANA_ANEVENTS_BUCKET2_MATCH] = REG_FIELD(ANA_ANEVENTS, 7, 7),
504 [ANA_ANEVENTS_BUCKET1_MATCH] = REG_FIELD(ANA_ANEVENTS, 6, 6),
505 [ANA_ANEVENTS_BUCKET0_MATCH] = REG_FIELD(ANA_ANEVENTS, 5, 5),
506 [ANA_ANEVENTS_CPU_OPERATION] = REG_FIELD(ANA_ANEVENTS, 4, 4),
507 [ANA_ANEVENTS_DMAC_LOOKUP] = REG_FIELD(ANA_ANEVENTS, 3, 3),
508 [ANA_ANEVENTS_SMAC_LOOKUP] = REG_FIELD(ANA_ANEVENTS, 2, 2),
509 [ANA_ANEVENTS_SEQ_GEN_ERR_0] = REG_FIELD(ANA_ANEVENTS, 1, 1),
510 [ANA_ANEVENTS_SEQ_GEN_ERR_1] = REG_FIELD(ANA_ANEVENTS, 0, 0),
511 [ANA_TABLES_MACACCESS_B_DOM] = REG_FIELD(ANA_TABLES_MACACCESS, 16, 16),
512 [ANA_TABLES_MACTINDX_BUCKET] = REG_FIELD(ANA_TABLES_MACTINDX, 11, 12),
513 [ANA_TABLES_MACTINDX_M_INDEX] = REG_FIELD(ANA_TABLES_MACTINDX, 0, 10),
514 [SYS_RESET_CFG_CORE_ENA] = REG_FIELD(SYS_RESET_CFG, 0, 0),
515 [GCB_SOFT_RST_SWC_RST] = REG_FIELD(GCB_SOFT_RST, 0, 0),
Vladimir Oltean886e1382020-07-13 19:57:03 +0300516 /* Replicated per number of ports (7), register size 4 per port */
517 [QSYS_SWITCH_PORT_MODE_PORT_ENA] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 14, 14, 7, 4),
518 [QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 11, 13, 7, 4),
519 [QSYS_SWITCH_PORT_MODE_YEL_RSRVD] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 10, 10, 7, 4),
520 [QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 9, 9, 7, 4),
521 [QSYS_SWITCH_PORT_MODE_TX_PFC_ENA] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 1, 8, 7, 4),
522 [QSYS_SWITCH_PORT_MODE_TX_PFC_MODE] = REG_FIELD_ID(QSYS_SWITCH_PORT_MODE, 0, 0, 7, 4),
523 [SYS_PORT_MODE_DATA_WO_TS] = REG_FIELD_ID(SYS_PORT_MODE, 5, 6, 7, 4),
524 [SYS_PORT_MODE_INCL_INJ_HDR] = REG_FIELD_ID(SYS_PORT_MODE, 3, 4, 7, 4),
525 [SYS_PORT_MODE_INCL_XTR_HDR] = REG_FIELD_ID(SYS_PORT_MODE, 1, 2, 7, 4),
526 [SYS_PORT_MODE_INCL_HDR_ERR] = REG_FIELD_ID(SYS_PORT_MODE, 0, 0, 7, 4),
Maxim Kochetkov541132f2020-07-13 19:57:07 +0300527 [SYS_PAUSE_CFG_PAUSE_START] = REG_FIELD_ID(SYS_PAUSE_CFG, 10, 18, 7, 4),
528 [SYS_PAUSE_CFG_PAUSE_STOP] = REG_FIELD_ID(SYS_PAUSE_CFG, 1, 9, 7, 4),
529 [SYS_PAUSE_CFG_PAUSE_ENA] = REG_FIELD_ID(SYS_PAUSE_CFG, 0, 1, 7, 4),
Vladimir Oltean56051942019-11-14 17:03:30 +0200530};
531
532static const struct ocelot_stat_layout vsc9959_stats_layout[] = {
533 { .offset = 0x00, .name = "rx_octets", },
534 { .offset = 0x01, .name = "rx_unicast", },
535 { .offset = 0x02, .name = "rx_multicast", },
536 { .offset = 0x03, .name = "rx_broadcast", },
537 { .offset = 0x04, .name = "rx_shorts", },
538 { .offset = 0x05, .name = "rx_fragments", },
539 { .offset = 0x06, .name = "rx_jabbers", },
540 { .offset = 0x07, .name = "rx_crc_align_errs", },
541 { .offset = 0x08, .name = "rx_sym_errs", },
542 { .offset = 0x09, .name = "rx_frames_below_65_octets", },
543 { .offset = 0x0A, .name = "rx_frames_65_to_127_octets", },
544 { .offset = 0x0B, .name = "rx_frames_128_to_255_octets", },
545 { .offset = 0x0C, .name = "rx_frames_256_to_511_octets", },
546 { .offset = 0x0D, .name = "rx_frames_512_to_1023_octets", },
547 { .offset = 0x0E, .name = "rx_frames_1024_to_1526_octets", },
548 { .offset = 0x0F, .name = "rx_frames_over_1526_octets", },
549 { .offset = 0x10, .name = "rx_pause", },
550 { .offset = 0x11, .name = "rx_control", },
551 { .offset = 0x12, .name = "rx_longs", },
552 { .offset = 0x13, .name = "rx_classified_drops", },
553 { .offset = 0x14, .name = "rx_red_prio_0", },
554 { .offset = 0x15, .name = "rx_red_prio_1", },
555 { .offset = 0x16, .name = "rx_red_prio_2", },
556 { .offset = 0x17, .name = "rx_red_prio_3", },
557 { .offset = 0x18, .name = "rx_red_prio_4", },
558 { .offset = 0x19, .name = "rx_red_prio_5", },
559 { .offset = 0x1A, .name = "rx_red_prio_6", },
560 { .offset = 0x1B, .name = "rx_red_prio_7", },
561 { .offset = 0x1C, .name = "rx_yellow_prio_0", },
562 { .offset = 0x1D, .name = "rx_yellow_prio_1", },
563 { .offset = 0x1E, .name = "rx_yellow_prio_2", },
564 { .offset = 0x1F, .name = "rx_yellow_prio_3", },
565 { .offset = 0x20, .name = "rx_yellow_prio_4", },
566 { .offset = 0x21, .name = "rx_yellow_prio_5", },
567 { .offset = 0x22, .name = "rx_yellow_prio_6", },
568 { .offset = 0x23, .name = "rx_yellow_prio_7", },
569 { .offset = 0x24, .name = "rx_green_prio_0", },
570 { .offset = 0x25, .name = "rx_green_prio_1", },
571 { .offset = 0x26, .name = "rx_green_prio_2", },
572 { .offset = 0x27, .name = "rx_green_prio_3", },
573 { .offset = 0x28, .name = "rx_green_prio_4", },
574 { .offset = 0x29, .name = "rx_green_prio_5", },
575 { .offset = 0x2A, .name = "rx_green_prio_6", },
576 { .offset = 0x2B, .name = "rx_green_prio_7", },
577 { .offset = 0x80, .name = "tx_octets", },
578 { .offset = 0x81, .name = "tx_unicast", },
579 { .offset = 0x82, .name = "tx_multicast", },
580 { .offset = 0x83, .name = "tx_broadcast", },
581 { .offset = 0x84, .name = "tx_collision", },
582 { .offset = 0x85, .name = "tx_drops", },
583 { .offset = 0x86, .name = "tx_pause", },
584 { .offset = 0x87, .name = "tx_frames_below_65_octets", },
585 { .offset = 0x88, .name = "tx_frames_65_to_127_octets", },
586 { .offset = 0x89, .name = "tx_frames_128_255_octets", },
587 { .offset = 0x8B, .name = "tx_frames_256_511_octets", },
588 { .offset = 0x8C, .name = "tx_frames_1024_1526_octets", },
589 { .offset = 0x8D, .name = "tx_frames_over_1526_octets", },
590 { .offset = 0x8E, .name = "tx_yellow_prio_0", },
591 { .offset = 0x8F, .name = "tx_yellow_prio_1", },
592 { .offset = 0x90, .name = "tx_yellow_prio_2", },
593 { .offset = 0x91, .name = "tx_yellow_prio_3", },
594 { .offset = 0x92, .name = "tx_yellow_prio_4", },
595 { .offset = 0x93, .name = "tx_yellow_prio_5", },
596 { .offset = 0x94, .name = "tx_yellow_prio_6", },
597 { .offset = 0x95, .name = "tx_yellow_prio_7", },
598 { .offset = 0x96, .name = "tx_green_prio_0", },
599 { .offset = 0x97, .name = "tx_green_prio_1", },
600 { .offset = 0x98, .name = "tx_green_prio_2", },
601 { .offset = 0x99, .name = "tx_green_prio_3", },
602 { .offset = 0x9A, .name = "tx_green_prio_4", },
603 { .offset = 0x9B, .name = "tx_green_prio_5", },
604 { .offset = 0x9C, .name = "tx_green_prio_6", },
605 { .offset = 0x9D, .name = "tx_green_prio_7", },
606 { .offset = 0x9E, .name = "tx_aged", },
607 { .offset = 0x100, .name = "drop_local", },
608 { .offset = 0x101, .name = "drop_tail", },
609 { .offset = 0x102, .name = "drop_yellow_prio_0", },
610 { .offset = 0x103, .name = "drop_yellow_prio_1", },
611 { .offset = 0x104, .name = "drop_yellow_prio_2", },
612 { .offset = 0x105, .name = "drop_yellow_prio_3", },
613 { .offset = 0x106, .name = "drop_yellow_prio_4", },
614 { .offset = 0x107, .name = "drop_yellow_prio_5", },
615 { .offset = 0x108, .name = "drop_yellow_prio_6", },
616 { .offset = 0x109, .name = "drop_yellow_prio_7", },
617 { .offset = 0x10A, .name = "drop_green_prio_0", },
618 { .offset = 0x10B, .name = "drop_green_prio_1", },
619 { .offset = 0x10C, .name = "drop_green_prio_2", },
620 { .offset = 0x10D, .name = "drop_green_prio_3", },
621 { .offset = 0x10E, .name = "drop_green_prio_4", },
622 { .offset = 0x10F, .name = "drop_green_prio_5", },
623 { .offset = 0x110, .name = "drop_green_prio_6", },
624 { .offset = 0x111, .name = "drop_green_prio_7", },
625};
626
Vladimir Olteane3aea292020-09-30 01:27:25 +0300627static const struct vcap_field vsc9959_vcap_es0_keys[] = {
628 [VCAP_ES0_EGR_PORT] = { 0, 3},
629 [VCAP_ES0_IGR_PORT] = { 3, 3},
630 [VCAP_ES0_RSV] = { 6, 2},
631 [VCAP_ES0_L2_MC] = { 8, 1},
632 [VCAP_ES0_L2_BC] = { 9, 1},
633 [VCAP_ES0_VID] = { 10, 12},
634 [VCAP_ES0_DP] = { 22, 1},
635 [VCAP_ES0_PCP] = { 23, 3},
636};
637
638static const struct vcap_field vsc9959_vcap_es0_actions[] = {
639 [VCAP_ES0_ACT_PUSH_OUTER_TAG] = { 0, 2},
640 [VCAP_ES0_ACT_PUSH_INNER_TAG] = { 2, 1},
641 [VCAP_ES0_ACT_TAG_A_TPID_SEL] = { 3, 2},
642 [VCAP_ES0_ACT_TAG_A_VID_SEL] = { 5, 1},
643 [VCAP_ES0_ACT_TAG_A_PCP_SEL] = { 6, 2},
644 [VCAP_ES0_ACT_TAG_A_DEI_SEL] = { 8, 2},
645 [VCAP_ES0_ACT_TAG_B_TPID_SEL] = { 10, 2},
646 [VCAP_ES0_ACT_TAG_B_VID_SEL] = { 12, 1},
647 [VCAP_ES0_ACT_TAG_B_PCP_SEL] = { 13, 2},
648 [VCAP_ES0_ACT_TAG_B_DEI_SEL] = { 15, 2},
649 [VCAP_ES0_ACT_VID_A_VAL] = { 17, 12},
650 [VCAP_ES0_ACT_PCP_A_VAL] = { 29, 3},
651 [VCAP_ES0_ACT_DEI_A_VAL] = { 32, 1},
652 [VCAP_ES0_ACT_VID_B_VAL] = { 33, 12},
653 [VCAP_ES0_ACT_PCP_B_VAL] = { 45, 3},
654 [VCAP_ES0_ACT_DEI_B_VAL] = { 48, 1},
655 [VCAP_ES0_ACT_RSV] = { 49, 23},
656 [VCAP_ES0_ACT_HIT_STICKY] = { 72, 1},
657};
658
Vladimir Olteana61e3652020-09-30 01:27:24 +0300659static const struct vcap_field vsc9959_vcap_is1_keys[] = {
660 [VCAP_IS1_HK_TYPE] = { 0, 1},
661 [VCAP_IS1_HK_LOOKUP] = { 1, 2},
662 [VCAP_IS1_HK_IGR_PORT_MASK] = { 3, 7},
663 [VCAP_IS1_HK_RSV] = { 10, 9},
664 [VCAP_IS1_HK_OAM_Y1731] = { 19, 1},
665 [VCAP_IS1_HK_L2_MC] = { 20, 1},
666 [VCAP_IS1_HK_L2_BC] = { 21, 1},
667 [VCAP_IS1_HK_IP_MC] = { 22, 1},
668 [VCAP_IS1_HK_VLAN_TAGGED] = { 23, 1},
669 [VCAP_IS1_HK_VLAN_DBL_TAGGED] = { 24, 1},
670 [VCAP_IS1_HK_TPID] = { 25, 1},
671 [VCAP_IS1_HK_VID] = { 26, 12},
672 [VCAP_IS1_HK_DEI] = { 38, 1},
673 [VCAP_IS1_HK_PCP] = { 39, 3},
674 /* Specific Fields for IS1 Half Key S1_NORMAL */
675 [VCAP_IS1_HK_L2_SMAC] = { 42, 48},
676 [VCAP_IS1_HK_ETYPE_LEN] = { 90, 1},
677 [VCAP_IS1_HK_ETYPE] = { 91, 16},
678 [VCAP_IS1_HK_IP_SNAP] = {107, 1},
679 [VCAP_IS1_HK_IP4] = {108, 1},
680 /* Layer-3 Information */
681 [VCAP_IS1_HK_L3_FRAGMENT] = {109, 1},
682 [VCAP_IS1_HK_L3_FRAG_OFS_GT0] = {110, 1},
683 [VCAP_IS1_HK_L3_OPTIONS] = {111, 1},
684 [VCAP_IS1_HK_L3_DSCP] = {112, 6},
685 [VCAP_IS1_HK_L3_IP4_SIP] = {118, 32},
686 /* Layer-4 Information */
687 [VCAP_IS1_HK_TCP_UDP] = {150, 1},
688 [VCAP_IS1_HK_TCP] = {151, 1},
689 [VCAP_IS1_HK_L4_SPORT] = {152, 16},
690 [VCAP_IS1_HK_L4_RNG] = {168, 8},
691 /* Specific Fields for IS1 Half Key S1_5TUPLE_IP4 */
692 [VCAP_IS1_HK_IP4_INNER_TPID] = { 42, 1},
693 [VCAP_IS1_HK_IP4_INNER_VID] = { 43, 12},
694 [VCAP_IS1_HK_IP4_INNER_DEI] = { 55, 1},
695 [VCAP_IS1_HK_IP4_INNER_PCP] = { 56, 3},
696 [VCAP_IS1_HK_IP4_IP4] = { 59, 1},
697 [VCAP_IS1_HK_IP4_L3_FRAGMENT] = { 60, 1},
698 [VCAP_IS1_HK_IP4_L3_FRAG_OFS_GT0] = { 61, 1},
699 [VCAP_IS1_HK_IP4_L3_OPTIONS] = { 62, 1},
700 [VCAP_IS1_HK_IP4_L3_DSCP] = { 63, 6},
701 [VCAP_IS1_HK_IP4_L3_IP4_DIP] = { 69, 32},
702 [VCAP_IS1_HK_IP4_L3_IP4_SIP] = {101, 32},
703 [VCAP_IS1_HK_IP4_L3_PROTO] = {133, 8},
704 [VCAP_IS1_HK_IP4_TCP_UDP] = {141, 1},
705 [VCAP_IS1_HK_IP4_TCP] = {142, 1},
706 [VCAP_IS1_HK_IP4_L4_RNG] = {143, 8},
707 [VCAP_IS1_HK_IP4_IP_PAYLOAD_S1_5TUPLE] = {151, 32},
708};
709
710static const struct vcap_field vsc9959_vcap_is1_actions[] = {
711 [VCAP_IS1_ACT_DSCP_ENA] = { 0, 1},
712 [VCAP_IS1_ACT_DSCP_VAL] = { 1, 6},
713 [VCAP_IS1_ACT_QOS_ENA] = { 7, 1},
714 [VCAP_IS1_ACT_QOS_VAL] = { 8, 3},
715 [VCAP_IS1_ACT_DP_ENA] = { 11, 1},
716 [VCAP_IS1_ACT_DP_VAL] = { 12, 1},
717 [VCAP_IS1_ACT_PAG_OVERRIDE_MASK] = { 13, 8},
718 [VCAP_IS1_ACT_PAG_VAL] = { 21, 8},
719 [VCAP_IS1_ACT_RSV] = { 29, 9},
Xiaoliang Yang75944fd2020-10-02 15:02:23 +0300720 /* The fields below are incorrectly shifted by 2 in the manual */
Vladimir Olteana61e3652020-09-30 01:27:24 +0300721 [VCAP_IS1_ACT_VID_REPLACE_ENA] = { 38, 1},
722 [VCAP_IS1_ACT_VID_ADD_VAL] = { 39, 12},
723 [VCAP_IS1_ACT_FID_SEL] = { 51, 2},
724 [VCAP_IS1_ACT_FID_VAL] = { 53, 13},
725 [VCAP_IS1_ACT_PCP_DEI_ENA] = { 66, 1},
726 [VCAP_IS1_ACT_PCP_VAL] = { 67, 3},
727 [VCAP_IS1_ACT_DEI_VAL] = { 70, 1},
728 [VCAP_IS1_ACT_VLAN_POP_CNT_ENA] = { 71, 1},
729 [VCAP_IS1_ACT_VLAN_POP_CNT] = { 72, 2},
730 [VCAP_IS1_ACT_CUSTOM_ACE_TYPE_ENA] = { 74, 4},
731 [VCAP_IS1_ACT_HIT_STICKY] = { 78, 1},
732};
733
Vladimir Oltean3ab4ceb2020-06-20 18:43:36 +0300734static struct vcap_field vsc9959_vcap_is2_keys[] = {
Vladimir Oltean07d985e2020-02-29 16:31:14 +0200735 /* Common: 41 bits */
736 [VCAP_IS2_TYPE] = { 0, 4},
737 [VCAP_IS2_HK_FIRST] = { 4, 1},
738 [VCAP_IS2_HK_PAG] = { 5, 8},
739 [VCAP_IS2_HK_IGR_PORT_MASK] = { 13, 7},
740 [VCAP_IS2_HK_RSV2] = { 20, 1},
741 [VCAP_IS2_HK_HOST_MATCH] = { 21, 1},
742 [VCAP_IS2_HK_L2_MC] = { 22, 1},
743 [VCAP_IS2_HK_L2_BC] = { 23, 1},
744 [VCAP_IS2_HK_VLAN_TAGGED] = { 24, 1},
745 [VCAP_IS2_HK_VID] = { 25, 12},
746 [VCAP_IS2_HK_DEI] = { 37, 1},
747 [VCAP_IS2_HK_PCP] = { 38, 3},
748 /* MAC_ETYPE / MAC_LLC / MAC_SNAP / OAM common */
749 [VCAP_IS2_HK_L2_DMAC] = { 41, 48},
750 [VCAP_IS2_HK_L2_SMAC] = { 89, 48},
751 /* MAC_ETYPE (TYPE=000) */
752 [VCAP_IS2_HK_MAC_ETYPE_ETYPE] = {137, 16},
753 [VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0] = {153, 16},
754 [VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1] = {169, 8},
755 [VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2] = {177, 3},
756 /* MAC_LLC (TYPE=001) */
757 [VCAP_IS2_HK_MAC_LLC_L2_LLC] = {137, 40},
758 /* MAC_SNAP (TYPE=010) */
759 [VCAP_IS2_HK_MAC_SNAP_L2_SNAP] = {137, 40},
760 /* MAC_ARP (TYPE=011) */
761 [VCAP_IS2_HK_MAC_ARP_SMAC] = { 41, 48},
762 [VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK] = { 89, 1},
763 [VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK] = { 90, 1},
764 [VCAP_IS2_HK_MAC_ARP_LEN_OK] = { 91, 1},
765 [VCAP_IS2_HK_MAC_ARP_TARGET_MATCH] = { 92, 1},
766 [VCAP_IS2_HK_MAC_ARP_SENDER_MATCH] = { 93, 1},
767 [VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN] = { 94, 1},
768 [VCAP_IS2_HK_MAC_ARP_OPCODE] = { 95, 2},
769 [VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP] = { 97, 32},
770 [VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP] = {129, 32},
771 [VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP] = {161, 1},
772 /* IP4_TCP_UDP / IP4_OTHER common */
773 [VCAP_IS2_HK_IP4] = { 41, 1},
774 [VCAP_IS2_HK_L3_FRAGMENT] = { 42, 1},
775 [VCAP_IS2_HK_L3_FRAG_OFS_GT0] = { 43, 1},
776 [VCAP_IS2_HK_L3_OPTIONS] = { 44, 1},
777 [VCAP_IS2_HK_IP4_L3_TTL_GT0] = { 45, 1},
778 [VCAP_IS2_HK_L3_TOS] = { 46, 8},
779 [VCAP_IS2_HK_L3_IP4_DIP] = { 54, 32},
780 [VCAP_IS2_HK_L3_IP4_SIP] = { 86, 32},
781 [VCAP_IS2_HK_DIP_EQ_SIP] = {118, 1},
782 /* IP4_TCP_UDP (TYPE=100) */
783 [VCAP_IS2_HK_TCP] = {119, 1},
Xiaoliang Yang8b9e03c2020-09-22 01:56:36 +0300784 [VCAP_IS2_HK_L4_DPORT] = {120, 16},
785 [VCAP_IS2_HK_L4_SPORT] = {136, 16},
Vladimir Oltean07d985e2020-02-29 16:31:14 +0200786 [VCAP_IS2_HK_L4_RNG] = {152, 8},
787 [VCAP_IS2_HK_L4_SPORT_EQ_DPORT] = {160, 1},
788 [VCAP_IS2_HK_L4_SEQUENCE_EQ0] = {161, 1},
Xiaoliang Yang8b9e03c2020-09-22 01:56:36 +0300789 [VCAP_IS2_HK_L4_FIN] = {162, 1},
790 [VCAP_IS2_HK_L4_SYN] = {163, 1},
791 [VCAP_IS2_HK_L4_RST] = {164, 1},
792 [VCAP_IS2_HK_L4_PSH] = {165, 1},
793 [VCAP_IS2_HK_L4_ACK] = {166, 1},
794 [VCAP_IS2_HK_L4_URG] = {167, 1},
Vladimir Oltean07d985e2020-02-29 16:31:14 +0200795 [VCAP_IS2_HK_L4_1588_DOM] = {168, 8},
796 [VCAP_IS2_HK_L4_1588_VER] = {176, 4},
797 /* IP4_OTHER (TYPE=101) */
798 [VCAP_IS2_HK_IP4_L3_PROTO] = {119, 8},
799 [VCAP_IS2_HK_L3_PAYLOAD] = {127, 56},
800 /* IP6_STD (TYPE=110) */
801 [VCAP_IS2_HK_IP6_L3_TTL_GT0] = { 41, 1},
802 [VCAP_IS2_HK_L3_IP6_SIP] = { 42, 128},
803 [VCAP_IS2_HK_IP6_L3_PROTO] = {170, 8},
804 /* OAM (TYPE=111) */
805 [VCAP_IS2_HK_OAM_MEL_FLAGS] = {137, 7},
806 [VCAP_IS2_HK_OAM_VER] = {144, 5},
807 [VCAP_IS2_HK_OAM_OPCODE] = {149, 8},
808 [VCAP_IS2_HK_OAM_FLAGS] = {157, 8},
809 [VCAP_IS2_HK_OAM_MEPID] = {165, 16},
810 [VCAP_IS2_HK_OAM_CCM_CNTS_EQ0] = {181, 1},
811 [VCAP_IS2_HK_OAM_IS_Y1731] = {182, 1},
812};
813
Vladimir Oltean3ab4ceb2020-06-20 18:43:36 +0300814static struct vcap_field vsc9959_vcap_is2_actions[] = {
Vladimir Oltean07d985e2020-02-29 16:31:14 +0200815 [VCAP_IS2_ACT_HIT_ME_ONCE] = { 0, 1},
816 [VCAP_IS2_ACT_CPU_COPY_ENA] = { 1, 1},
817 [VCAP_IS2_ACT_CPU_QU_NUM] = { 2, 3},
818 [VCAP_IS2_ACT_MASK_MODE] = { 5, 2},
819 [VCAP_IS2_ACT_MIRROR_ENA] = { 7, 1},
820 [VCAP_IS2_ACT_LRN_DIS] = { 8, 1},
821 [VCAP_IS2_ACT_POLICE_ENA] = { 9, 1},
822 [VCAP_IS2_ACT_POLICE_IDX] = { 10, 9},
823 [VCAP_IS2_ACT_POLICE_VCAP_ONLY] = { 19, 1},
Vladimir Oltean460e9852020-09-29 14:20:24 +0300824 [VCAP_IS2_ACT_PORT_MASK] = { 20, 6},
825 [VCAP_IS2_ACT_REW_OP] = { 26, 9},
826 [VCAP_IS2_ACT_SMAC_REPLACE_ENA] = { 35, 1},
827 [VCAP_IS2_ACT_RSV] = { 36, 2},
828 [VCAP_IS2_ACT_ACL_ID] = { 38, 6},
829 [VCAP_IS2_ACT_HIT_CNT] = { 44, 32},
Vladimir Oltean07d985e2020-02-29 16:31:14 +0200830};
831
Vladimir Oltean20968052020-09-30 01:27:26 +0300832static struct vcap_props vsc9959_vcap_props[] = {
Vladimir Olteane3aea292020-09-30 01:27:25 +0300833 [VCAP_ES0] = {
834 .action_type_width = 0,
835 .action_table = {
836 [ES0_ACTION_TYPE_NORMAL] = {
837 .width = 72, /* HIT_STICKY not included */
838 .count = 1,
839 },
840 },
841 .target = S0,
842 .keys = vsc9959_vcap_es0_keys,
843 .actions = vsc9959_vcap_es0_actions,
844 },
Vladimir Olteana61e3652020-09-30 01:27:24 +0300845 [VCAP_IS1] = {
846 .action_type_width = 0,
847 .action_table = {
848 [IS1_ACTION_TYPE_NORMAL] = {
849 .width = 78, /* HIT_STICKY not included */
850 .count = 4,
851 },
852 },
853 .target = S1,
854 .keys = vsc9959_vcap_is1_keys,
855 .actions = vsc9959_vcap_is1_actions,
856 },
Vladimir Oltean07d985e2020-02-29 16:31:14 +0200857 [VCAP_IS2] = {
Vladimir Oltean07d985e2020-02-29 16:31:14 +0200858 .action_type_width = 1,
859 .action_table = {
860 [IS2_ACTION_TYPE_NORMAL] = {
861 .width = 44,
862 .count = 2
863 },
864 [IS2_ACTION_TYPE_SMAC_SIP] = {
865 .width = 6,
866 .count = 4
867 },
868 },
Vladimir Olteanc1c39932020-09-30 01:27:23 +0300869 .target = S2,
870 .keys = vsc9959_vcap_is2_keys,
871 .actions = vsc9959_vcap_is2_actions,
Vladimir Oltean07d985e2020-02-29 16:31:14 +0200872 },
873};
874
Vladimir Oltean2ac7c6c2020-09-18 13:57:52 +0300875static const struct ptp_clock_info vsc9959_ptp_caps = {
876 .owner = THIS_MODULE,
877 .name = "felix ptp",
878 .max_adj = 0x7fffffff,
879 .n_alarm = 0,
880 .n_ext_ts = 0,
881 .n_per_out = OCELOT_PTP_PINS_NUM,
882 .n_pins = OCELOT_PTP_PINS_NUM,
883 .pps = 0,
884 .gettime64 = ocelot_ptp_gettime64,
885 .settime64 = ocelot_ptp_settime64,
886 .adjtime = ocelot_ptp_adjtime,
887 .adjfine = ocelot_ptp_adjfine,
888 .verify = ocelot_ptp_verify,
889 .enable = ocelot_ptp_enable,
890};
891
Vladimir Oltean56051942019-11-14 17:03:30 +0200892#define VSC9959_INIT_TIMEOUT 50000
893#define VSC9959_GCB_RST_SLEEP 100
894#define VSC9959_SYS_RAMINIT_SLEEP 80
895
896static int vsc9959_gcb_soft_rst_status(struct ocelot *ocelot)
897{
898 int val;
899
Vladimir Oltean75cea9c2020-09-18 13:57:43 +0300900 ocelot_field_read(ocelot, GCB_SOFT_RST_SWC_RST, &val);
Vladimir Oltean56051942019-11-14 17:03:30 +0200901
902 return val;
903}
904
905static int vsc9959_sys_ram_init_status(struct ocelot *ocelot)
906{
907 return ocelot_read(ocelot, SYS_RAM_INIT);
908}
909
Vladimir Olteanc129fc52020-09-18 13:57:46 +0300910/* CORE_ENA is in SYS:SYSTEM:RESET_CFG
911 * RAM_INIT is in SYS:RAM_CTRL:RAM_INIT
912 */
Vladimir Oltean56051942019-11-14 17:03:30 +0200913static int vsc9959_reset(struct ocelot *ocelot)
914{
915 int val, err;
916
917 /* soft-reset the switch core */
Vladimir Oltean75cea9c2020-09-18 13:57:43 +0300918 ocelot_field_write(ocelot, GCB_SOFT_RST_SWC_RST, 1);
Vladimir Oltean56051942019-11-14 17:03:30 +0200919
920 err = readx_poll_timeout(vsc9959_gcb_soft_rst_status, ocelot, val, !val,
921 VSC9959_GCB_RST_SLEEP, VSC9959_INIT_TIMEOUT);
922 if (err) {
923 dev_err(ocelot->dev, "timeout: switch core reset\n");
924 return err;
925 }
926
927 /* initialize switch mem ~40us */
928 ocelot_write(ocelot, SYS_RAM_INIT_RAM_INIT, SYS_RAM_INIT);
929 err = readx_poll_timeout(vsc9959_sys_ram_init_status, ocelot, val, !val,
930 VSC9959_SYS_RAMINIT_SLEEP,
931 VSC9959_INIT_TIMEOUT);
932 if (err) {
933 dev_err(ocelot->dev, "timeout: switch sram init\n");
934 return err;
935 }
936
937 /* enable switch core */
Vladimir Oltean75cea9c2020-09-18 13:57:43 +0300938 ocelot_field_write(ocelot, SYS_RESET_CFG_CORE_ENA, 1);
Vladimir Oltean56051942019-11-14 17:03:30 +0200939
940 return 0;
941}
942
Vladimir Oltean375e1312020-07-13 19:57:09 +0300943static void vsc9959_phylink_validate(struct ocelot *ocelot, int port,
944 unsigned long *supported,
945 struct phylink_link_state *state)
946{
947 struct ocelot_port *ocelot_port = ocelot->ports[port];
948 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
949
950 if (state->interface != PHY_INTERFACE_MODE_NA &&
951 state->interface != ocelot_port->phy_mode) {
Sean Anderson49730562021-10-22 18:41:04 -0400952 linkmode_zero(supported);
Vladimir Oltean375e1312020-07-13 19:57:09 +0300953 return;
954 }
955
956 phylink_set_port_modes(mask);
957 phylink_set(mask, Autoneg);
958 phylink_set(mask, Pause);
959 phylink_set(mask, Asym_Pause);
960 phylink_set(mask, 10baseT_Half);
961 phylink_set(mask, 10baseT_Full);
962 phylink_set(mask, 100baseT_Half);
963 phylink_set(mask, 100baseT_Full);
964 phylink_set(mask, 1000baseT_Half);
965 phylink_set(mask, 1000baseT_Full);
966
967 if (state->interface == PHY_INTERFACE_MODE_INTERNAL ||
968 state->interface == PHY_INTERFACE_MODE_2500BASEX ||
969 state->interface == PHY_INTERFACE_MODE_USXGMII) {
970 phylink_set(mask, 2500baseT_Full);
971 phylink_set(mask, 2500baseX_Full);
972 }
973
Sean Anderson49730562021-10-22 18:41:04 -0400974 linkmode_and(supported, supported, mask);
975 linkmode_and(state->advertising, state->advertising, mask);
Vladimir Oltean375e1312020-07-13 19:57:09 +0300976}
977
Vladimir Olteanbdeced72020-01-06 03:34:17 +0200978static int vsc9959_prevalidate_phy_mode(struct ocelot *ocelot, int port,
979 phy_interface_t phy_mode)
980{
981 switch (phy_mode) {
Vladimir Oltean28a134f2020-02-24 14:15:33 +0200982 case PHY_INTERFACE_MODE_INTERNAL:
Vladimir Olteanbdeced72020-01-06 03:34:17 +0200983 if (port != 4 && port != 5)
984 return -ENOTSUPP;
985 return 0;
986 case PHY_INTERFACE_MODE_SGMII:
987 case PHY_INTERFACE_MODE_QSGMII:
988 case PHY_INTERFACE_MODE_USXGMII:
989 case PHY_INTERFACE_MODE_2500BASEX:
990 /* Not supported on internal to-CPU ports */
991 if (port == 4 || port == 5)
992 return -ENOTSUPP;
993 return 0;
994 default:
995 return -ENOTSUPP;
996 }
997}
998
Maxim Kochetkovaa92d832020-07-13 19:57:08 +0300999/* Watermark encode
1000 * Bit 8: Unit; 0:1, 1:16
1001 * Bit 7-0: Value to be multiplied with unit
1002 */
1003static u16 vsc9959_wm_enc(u16 value)
1004{
Vladimir Oltean01326492020-10-05 12:09:12 +03001005 WARN_ON(value >= 16 * BIT(8));
1006
Maxim Kochetkovaa92d832020-07-13 19:57:08 +03001007 if (value >= BIT(8))
1008 return BIT(8) | (value / 16);
1009
1010 return value;
1011}
1012
Vladimir Oltean703b7622021-01-15 04:11:12 +02001013static u16 vsc9959_wm_dec(u16 wm)
1014{
1015 WARN_ON(wm & ~GENMASK(8, 0));
1016
1017 if (wm & BIT(8))
1018 return (wm & GENMASK(7, 0)) * 16;
1019
1020 return wm;
1021}
1022
1023static void vsc9959_wm_stat(u32 val, u32 *inuse, u32 *maxuse)
1024{
1025 *inuse = (val & GENMASK(23, 12)) >> 12;
1026 *maxuse = val & GENMASK(11, 0);
1027}
1028
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001029static int vsc9959_mdio_bus_alloc(struct ocelot *ocelot)
1030{
1031 struct felix *felix = ocelot_to_felix(ocelot);
1032 struct enetc_mdio_priv *mdio_priv;
1033 struct device *dev = ocelot->dev;
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001034 void __iomem *imdio_regs;
Claudiu Manoilb4024c92020-05-22 11:54:34 +03001035 struct resource res;
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001036 struct enetc_hw *hw;
1037 struct mii_bus *bus;
1038 int port;
1039 int rc;
1040
1041 felix->pcs = devm_kcalloc(dev, felix->info->num_ports,
Ioana Ciornei588d0552020-08-30 11:34:02 +03001042 sizeof(struct lynx_pcs *),
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001043 GFP_KERNEL);
1044 if (!felix->pcs) {
1045 dev_err(dev, "failed to allocate array for PCS PHYs\n");
1046 return -ENOMEM;
1047 }
1048
Claudiu Manoilb4024c92020-05-22 11:54:34 +03001049 memcpy(&res, felix->info->imdio_res, sizeof(res));
1050 res.flags = IORESOURCE_MEM;
Vladimir Oltean375e1312020-07-13 19:57:09 +03001051 res.start += felix->imdio_base;
1052 res.end += felix->imdio_base;
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001053
Claudiu Manoilb4024c92020-05-22 11:54:34 +03001054 imdio_regs = devm_ioremap_resource(dev, &res);
Guobin Huanga180be72021-03-29 09:54:05 +08001055 if (IS_ERR(imdio_regs))
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001056 return PTR_ERR(imdio_regs);
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001057
1058 hw = enetc_hw_alloc(dev, imdio_regs);
1059 if (IS_ERR(hw)) {
1060 dev_err(dev, "failed to allocate ENETC HW structure\n");
1061 return PTR_ERR(hw);
1062 }
1063
1064 bus = devm_mdiobus_alloc_size(dev, sizeof(*mdio_priv));
1065 if (!bus)
1066 return -ENOMEM;
1067
1068 bus->name = "VSC9959 internal MDIO bus";
1069 bus->read = enetc_mdio_read;
1070 bus->write = enetc_mdio_write;
1071 bus->parent = dev;
1072 mdio_priv = bus->priv;
1073 mdio_priv->hw = hw;
1074 /* This gets added to imdio_regs, which already maps addresses
1075 * starting with the proper offset.
1076 */
1077 mdio_priv->mdio_base = 0;
1078 snprintf(bus->id, MII_BUS_ID_SIZE, "%s-imdio", dev_name(dev));
1079
1080 /* Needed in order to initialize the bus mutex lock */
1081 rc = mdiobus_register(bus);
1082 if (rc < 0) {
1083 dev_err(dev, "failed to register MDIO bus\n");
1084 return rc;
1085 }
1086
1087 felix->imdio = bus;
1088
1089 for (port = 0; port < felix->info->num_ports; port++) {
1090 struct ocelot_port *ocelot_port = ocelot->ports[port];
Ioana Ciornei588d0552020-08-30 11:34:02 +03001091 struct mdio_device *pcs;
1092 struct lynx_pcs *lynx;
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001093
Ioana Ciornei588d0552020-08-30 11:34:02 +03001094 if (dsa_is_unused_port(felix->ds, port))
1095 continue;
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001096
Ioana Ciornei588d0552020-08-30 11:34:02 +03001097 if (ocelot_port->phy_mode == PHY_INTERFACE_MODE_INTERNAL)
1098 continue;
1099
1100 pcs = mdio_device_create(felix->imdio, port);
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001101 if (IS_ERR(pcs))
1102 continue;
1103
Ioana Ciornei588d0552020-08-30 11:34:02 +03001104 lynx = lynx_pcs_create(pcs);
1105 if (!lynx) {
1106 mdio_device_free(pcs);
1107 continue;
1108 }
1109
1110 felix->pcs[port] = lynx;
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001111
1112 dev_info(dev, "Found PCS at internal MDIO address %d\n", port);
1113 }
1114
1115 return 0;
1116}
1117
Vladimir Olteanccfdbab2020-09-18 13:57:50 +03001118static void vsc9959_mdio_bus_free(struct ocelot *ocelot)
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001119{
1120 struct felix *felix = ocelot_to_felix(ocelot);
1121 int port;
1122
1123 for (port = 0; port < ocelot->num_phys_ports; port++) {
Ioana Ciornei588d0552020-08-30 11:34:02 +03001124 struct lynx_pcs *pcs = felix->pcs[port];
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001125
1126 if (!pcs)
1127 continue;
1128
Ioana Ciornei588d0552020-08-30 11:34:02 +03001129 mdio_device_free(pcs->mdio);
1130 lynx_pcs_destroy(pcs);
Vladimir Olteanbdeced72020-01-06 03:34:17 +02001131 }
1132 mdiobus_unregister(felix->imdio);
1133}
1134
Xiaoliang Yangde143c02020-05-13 10:25:09 +08001135static void vsc9959_sched_speed_set(struct ocelot *ocelot, int port,
1136 u32 speed)
1137{
Xiaoliang Yangdba1e462020-09-24 09:57:46 +08001138 u8 tas_speed;
1139
1140 switch (speed) {
1141 case SPEED_10:
1142 tas_speed = OCELOT_SPEED_10;
1143 break;
1144 case SPEED_100:
1145 tas_speed = OCELOT_SPEED_100;
1146 break;
1147 case SPEED_1000:
1148 tas_speed = OCELOT_SPEED_1000;
1149 break;
1150 case SPEED_2500:
1151 tas_speed = OCELOT_SPEED_2500;
1152 break;
1153 default:
1154 tas_speed = OCELOT_SPEED_1000;
1155 break;
1156 }
1157
Xiaoliang Yangde143c02020-05-13 10:25:09 +08001158 ocelot_rmw_rix(ocelot,
Xiaoliang Yangdba1e462020-09-24 09:57:46 +08001159 QSYS_TAG_CONFIG_LINK_SPEED(tas_speed),
Xiaoliang Yangde143c02020-05-13 10:25:09 +08001160 QSYS_TAG_CONFIG_LINK_SPEED_M,
1161 QSYS_TAG_CONFIG, port);
1162}
1163
1164static void vsc9959_new_base_time(struct ocelot *ocelot, ktime_t base_time,
1165 u64 cycle_time,
1166 struct timespec64 *new_base_ts)
1167{
1168 struct timespec64 ts;
1169 ktime_t new_base_time;
1170 ktime_t current_time;
1171
1172 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
1173 current_time = timespec64_to_ktime(ts);
1174 new_base_time = base_time;
1175
1176 if (base_time < current_time) {
1177 u64 nr_of_cycles = current_time - base_time;
1178
1179 do_div(nr_of_cycles, cycle_time);
1180 new_base_time += cycle_time * (nr_of_cycles + 1);
1181 }
1182
1183 *new_base_ts = ktime_to_timespec64(new_base_time);
1184}
1185
1186static u32 vsc9959_tas_read_cfg_status(struct ocelot *ocelot)
1187{
1188 return ocelot_read(ocelot, QSYS_TAS_PARAM_CFG_CTRL);
1189}
1190
1191static void vsc9959_tas_gcl_set(struct ocelot *ocelot, const u32 gcl_ix,
1192 struct tc_taprio_sched_entry *entry)
1193{
1194 ocelot_write(ocelot,
1195 QSYS_GCL_CFG_REG_1_GCL_ENTRY_NUM(gcl_ix) |
1196 QSYS_GCL_CFG_REG_1_GATE_STATE(entry->gate_mask),
1197 QSYS_GCL_CFG_REG_1);
1198 ocelot_write(ocelot, entry->interval, QSYS_GCL_CFG_REG_2);
1199}
1200
1201static int vsc9959_qos_port_tas_set(struct ocelot *ocelot, int port,
1202 struct tc_taprio_qopt_offload *taprio)
1203{
1204 struct timespec64 base_ts;
1205 int ret, i;
1206 u32 val;
1207
1208 if (!taprio->enable) {
1209 ocelot_rmw_rix(ocelot,
1210 QSYS_TAG_CONFIG_INIT_GATE_STATE(0xFF),
1211 QSYS_TAG_CONFIG_ENABLE |
1212 QSYS_TAG_CONFIG_INIT_GATE_STATE_M,
1213 QSYS_TAG_CONFIG, port);
1214
1215 return 0;
1216 }
1217
1218 if (taprio->cycle_time > NSEC_PER_SEC ||
1219 taprio->cycle_time_extension >= NSEC_PER_SEC)
1220 return -EINVAL;
1221
1222 if (taprio->num_entries > VSC9959_TAS_GCL_ENTRY_MAX)
1223 return -ERANGE;
1224
Michael Walle297c4de2021-05-10 13:07:08 +02001225 /* Enable guard band. The switch will schedule frames without taking
1226 * their length into account. Thus we'll always need to enable the
1227 * guard band which reserves the time of a maximum sized frame at the
1228 * end of the time window.
1229 *
1230 * Although the ALWAYS_GUARD_BAND_SCH_Q bit is global for all ports, we
1231 * need to set PORT_NUM, because subsequent writes to PARAM_CFG_REG_n
1232 * operate on the port number.
Xiaoliang Yang316bcff2021-04-19 18:25:30 +08001233 */
Michael Walle297c4de2021-05-10 13:07:08 +02001234 ocelot_rmw(ocelot, QSYS_TAS_PARAM_CFG_CTRL_PORT_NUM(port) |
1235 QSYS_TAS_PARAM_CFG_CTRL_ALWAYS_GUARD_BAND_SCH_Q,
Xiaoliang Yangde143c02020-05-13 10:25:09 +08001236 QSYS_TAS_PARAM_CFG_CTRL_PORT_NUM_M |
1237 QSYS_TAS_PARAM_CFG_CTRL_ALWAYS_GUARD_BAND_SCH_Q,
1238 QSYS_TAS_PARAM_CFG_CTRL);
1239
1240 /* Hardware errata - Admin config could not be overwritten if
1241 * config is pending, need reset the TAS module
1242 */
1243 val = ocelot_read(ocelot, QSYS_PARAM_STATUS_REG_8);
1244 if (val & QSYS_PARAM_STATUS_REG_8_CONFIG_PENDING)
1245 return -EBUSY;
1246
1247 ocelot_rmw_rix(ocelot,
1248 QSYS_TAG_CONFIG_ENABLE |
1249 QSYS_TAG_CONFIG_INIT_GATE_STATE(0xFF) |
1250 QSYS_TAG_CONFIG_SCH_TRAFFIC_QUEUES(0xFF),
1251 QSYS_TAG_CONFIG_ENABLE |
1252 QSYS_TAG_CONFIG_INIT_GATE_STATE_M |
1253 QSYS_TAG_CONFIG_SCH_TRAFFIC_QUEUES_M,
1254 QSYS_TAG_CONFIG, port);
1255
1256 vsc9959_new_base_time(ocelot, taprio->base_time,
1257 taprio->cycle_time, &base_ts);
1258 ocelot_write(ocelot, base_ts.tv_nsec, QSYS_PARAM_CFG_REG_1);
1259 ocelot_write(ocelot, lower_32_bits(base_ts.tv_sec), QSYS_PARAM_CFG_REG_2);
1260 val = upper_32_bits(base_ts.tv_sec);
1261 ocelot_write(ocelot,
1262 QSYS_PARAM_CFG_REG_3_BASE_TIME_SEC_MSB(val) |
1263 QSYS_PARAM_CFG_REG_3_LIST_LENGTH(taprio->num_entries),
1264 QSYS_PARAM_CFG_REG_3);
1265 ocelot_write(ocelot, taprio->cycle_time, QSYS_PARAM_CFG_REG_4);
1266 ocelot_write(ocelot, taprio->cycle_time_extension, QSYS_PARAM_CFG_REG_5);
1267
1268 for (i = 0; i < taprio->num_entries; i++)
1269 vsc9959_tas_gcl_set(ocelot, i, &taprio->entries[i]);
1270
1271 ocelot_rmw(ocelot, QSYS_TAS_PARAM_CFG_CTRL_CONFIG_CHANGE,
1272 QSYS_TAS_PARAM_CFG_CTRL_CONFIG_CHANGE,
1273 QSYS_TAS_PARAM_CFG_CTRL);
1274
1275 ret = readx_poll_timeout(vsc9959_tas_read_cfg_status, ocelot, val,
1276 !(val & QSYS_TAS_PARAM_CFG_CTRL_CONFIG_CHANGE),
1277 10, 100000);
1278
1279 return ret;
1280}
1281
Xiaoliang Yang0fbabf82020-05-13 10:25:10 +08001282static int vsc9959_qos_port_cbs_set(struct dsa_switch *ds, int port,
1283 struct tc_cbs_qopt_offload *cbs_qopt)
1284{
1285 struct ocelot *ocelot = ds->priv;
1286 int port_ix = port * 8 + cbs_qopt->queue;
1287 u32 rate, burst;
1288
1289 if (cbs_qopt->queue >= ds->num_tx_queues)
1290 return -EINVAL;
1291
1292 if (!cbs_qopt->enable) {
1293 ocelot_write_gix(ocelot, QSYS_CIR_CFG_CIR_RATE(0) |
1294 QSYS_CIR_CFG_CIR_BURST(0),
1295 QSYS_CIR_CFG, port_ix);
1296
1297 ocelot_rmw_gix(ocelot, 0, QSYS_SE_CFG_SE_AVB_ENA,
1298 QSYS_SE_CFG, port_ix);
1299
1300 return 0;
1301 }
1302
1303 /* Rate unit is 100 kbps */
1304 rate = DIV_ROUND_UP(cbs_qopt->idleslope, 100);
1305 /* Avoid using zero rate */
1306 rate = clamp_t(u32, rate, 1, GENMASK(14, 0));
1307 /* Burst unit is 4kB */
1308 burst = DIV_ROUND_UP(cbs_qopt->hicredit, 4096);
1309 /* Avoid using zero burst size */
Colin Ian Kingb014d042020-05-14 19:33:02 +01001310 burst = clamp_t(u32, burst, 1, GENMASK(5, 0));
Xiaoliang Yang0fbabf82020-05-13 10:25:10 +08001311 ocelot_write_gix(ocelot,
1312 QSYS_CIR_CFG_CIR_RATE(rate) |
1313 QSYS_CIR_CFG_CIR_BURST(burst),
1314 QSYS_CIR_CFG,
1315 port_ix);
1316
1317 ocelot_rmw_gix(ocelot,
1318 QSYS_SE_CFG_SE_FRM_MODE(0) |
1319 QSYS_SE_CFG_SE_AVB_ENA,
1320 QSYS_SE_CFG_SE_AVB_ENA |
1321 QSYS_SE_CFG_SE_FRM_MODE_M,
1322 QSYS_SE_CFG,
1323 port_ix);
1324
1325 return 0;
1326}
1327
Xiaoliang Yangde143c02020-05-13 10:25:09 +08001328static int vsc9959_port_setup_tc(struct dsa_switch *ds, int port,
1329 enum tc_setup_type type,
1330 void *type_data)
1331{
1332 struct ocelot *ocelot = ds->priv;
1333
1334 switch (type) {
1335 case TC_SETUP_QDISC_TAPRIO:
1336 return vsc9959_qos_port_tas_set(ocelot, port, type_data);
Xiaoliang Yang0fbabf82020-05-13 10:25:10 +08001337 case TC_SETUP_QDISC_CBS:
1338 return vsc9959_qos_port_cbs_set(ds, port, type_data);
Xiaoliang Yangde143c02020-05-13 10:25:09 +08001339 default:
1340 return -EOPNOTSUPP;
1341 }
1342}
1343
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001344#define VSC9959_PSFP_SFID_MAX 175
1345#define VSC9959_PSFP_GATE_ID_MAX 183
Xiaoliang Yang76c13ed2021-11-18 18:12:03 +08001346#define VSC9959_PSFP_POLICER_BASE 63
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001347#define VSC9959_PSFP_POLICER_MAX 383
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001348#define VSC9959_PSFP_GATE_LIST_NUM 4
1349#define VSC9959_PSFP_GATE_CYCLETIME_MIN 5000
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001350
1351struct felix_stream {
1352 struct list_head list;
1353 unsigned long id;
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001354 bool dummy;
1355 int ports;
1356 int port;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001357 u8 dmac[ETH_ALEN];
1358 u16 vid;
1359 s8 prio;
1360 u8 sfid_valid;
1361 u8 ssid_valid;
1362 u32 sfid;
1363 u32 ssid;
1364};
1365
1366struct felix_stream_filter {
1367 struct list_head list;
1368 refcount_t refcount;
1369 u32 index;
1370 u8 enable;
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001371 int portmask;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001372 u8 sg_valid;
1373 u32 sgid;
1374 u8 fm_valid;
1375 u32 fmid;
1376 u8 prio_valid;
1377 u8 prio;
1378 u32 maxsdu;
1379};
1380
1381struct felix_stream_filter_counters {
1382 u32 match;
1383 u32 not_pass_gate;
1384 u32 not_pass_sdu;
1385 u32 red;
1386};
1387
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001388struct felix_stream_gate {
1389 u32 index;
1390 u8 enable;
1391 u8 ipv_valid;
1392 u8 init_ipv;
1393 u64 basetime;
1394 u64 cycletime;
1395 u64 cycletime_ext;
1396 u32 num_entries;
kernel test robotdcad856fe52021-11-27 19:03:20 +01001397 struct action_gate_entry entries[];
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001398};
1399
1400struct felix_stream_gate_entry {
1401 struct list_head list;
1402 refcount_t refcount;
1403 u32 index;
1404};
1405
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001406static int vsc9959_stream_identify(struct flow_cls_offload *f,
1407 struct felix_stream *stream)
1408{
1409 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
1410 struct flow_dissector *dissector = rule->match.dissector;
1411
1412 if (dissector->used_keys &
1413 ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
1414 BIT(FLOW_DISSECTOR_KEY_BASIC) |
1415 BIT(FLOW_DISSECTOR_KEY_VLAN) |
1416 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS)))
1417 return -EOPNOTSUPP;
1418
1419 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
1420 struct flow_match_eth_addrs match;
1421
1422 flow_rule_match_eth_addrs(rule, &match);
1423 ether_addr_copy(stream->dmac, match.key->dst);
1424 if (!is_zero_ether_addr(match.mask->src))
1425 return -EOPNOTSUPP;
1426 } else {
1427 return -EOPNOTSUPP;
1428 }
1429
1430 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
1431 struct flow_match_vlan match;
1432
1433 flow_rule_match_vlan(rule, &match);
1434 if (match.mask->vlan_priority)
1435 stream->prio = match.key->vlan_priority;
1436 else
1437 stream->prio = -1;
1438
1439 if (!match.mask->vlan_id)
1440 return -EOPNOTSUPP;
1441 stream->vid = match.key->vlan_id;
1442 } else {
1443 return -EOPNOTSUPP;
1444 }
1445
1446 stream->id = f->cookie;
1447
1448 return 0;
1449}
1450
1451static int vsc9959_mact_stream_set(struct ocelot *ocelot,
1452 struct felix_stream *stream,
1453 struct netlink_ext_ack *extack)
1454{
1455 enum macaccess_entry_type type;
1456 int ret, sfid, ssid;
1457 u32 vid, dst_idx;
1458 u8 mac[ETH_ALEN];
1459
1460 ether_addr_copy(mac, stream->dmac);
1461 vid = stream->vid;
1462
1463 /* Stream identification desn't support to add a stream with non
1464 * existent MAC (The MAC entry has not been learned in MAC table).
1465 */
1466 ret = ocelot_mact_lookup(ocelot, &dst_idx, mac, vid, &type);
1467 if (ret) {
1468 if (extack)
1469 NL_SET_ERR_MSG_MOD(extack, "Stream is not learned in MAC table");
1470 return -EOPNOTSUPP;
1471 }
1472
1473 if ((stream->sfid_valid || stream->ssid_valid) &&
1474 type == ENTRYTYPE_NORMAL)
1475 type = ENTRYTYPE_LOCKED;
1476
1477 sfid = stream->sfid_valid ? stream->sfid : -1;
1478 ssid = stream->ssid_valid ? stream->ssid : -1;
1479
1480 ret = ocelot_mact_learn_streamdata(ocelot, dst_idx, mac, vid, type,
1481 sfid, ssid);
1482
1483 return ret;
1484}
1485
1486static struct felix_stream *
1487vsc9959_stream_table_lookup(struct list_head *stream_list,
1488 struct felix_stream *stream)
1489{
1490 struct felix_stream *tmp;
1491
1492 list_for_each_entry(tmp, stream_list, list)
1493 if (ether_addr_equal(tmp->dmac, stream->dmac) &&
1494 tmp->vid == stream->vid)
1495 return tmp;
1496
1497 return NULL;
1498}
1499
1500static int vsc9959_stream_table_add(struct ocelot *ocelot,
1501 struct list_head *stream_list,
1502 struct felix_stream *stream,
1503 struct netlink_ext_ack *extack)
1504{
1505 struct felix_stream *stream_entry;
1506 int ret;
1507
Yihao Hane44aecc2021-12-06 22:44:18 -08001508 stream_entry = kmemdup(stream, sizeof(*stream_entry), GFP_KERNEL);
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001509 if (!stream_entry)
1510 return -ENOMEM;
1511
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001512 if (!stream->dummy) {
1513 ret = vsc9959_mact_stream_set(ocelot, stream_entry, extack);
1514 if (ret) {
1515 kfree(stream_entry);
1516 return ret;
1517 }
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001518 }
1519
1520 list_add_tail(&stream_entry->list, stream_list);
1521
1522 return 0;
1523}
1524
1525static struct felix_stream *
1526vsc9959_stream_table_get(struct list_head *stream_list, unsigned long id)
1527{
1528 struct felix_stream *tmp;
1529
1530 list_for_each_entry(tmp, stream_list, list)
1531 if (tmp->id == id)
1532 return tmp;
1533
1534 return NULL;
1535}
1536
1537static void vsc9959_stream_table_del(struct ocelot *ocelot,
1538 struct felix_stream *stream)
1539{
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001540 if (!stream->dummy)
1541 vsc9959_mact_stream_set(ocelot, stream, NULL);
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001542
1543 list_del(&stream->list);
1544 kfree(stream);
1545}
1546
1547static u32 vsc9959_sfi_access_status(struct ocelot *ocelot)
1548{
1549 return ocelot_read(ocelot, ANA_TABLES_SFIDACCESS);
1550}
1551
1552static int vsc9959_psfp_sfi_set(struct ocelot *ocelot,
1553 struct felix_stream_filter *sfi)
1554{
1555 u32 val;
1556
1557 if (sfi->index > VSC9959_PSFP_SFID_MAX)
1558 return -EINVAL;
1559
1560 if (!sfi->enable) {
1561 ocelot_write(ocelot, ANA_TABLES_SFIDTIDX_SFID_INDEX(sfi->index),
1562 ANA_TABLES_SFIDTIDX);
1563
1564 val = ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(SFIDACCESS_CMD_WRITE);
1565 ocelot_write(ocelot, val, ANA_TABLES_SFIDACCESS);
1566
1567 return readx_poll_timeout(vsc9959_sfi_access_status, ocelot, val,
1568 (!ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(val)),
1569 10, 100000);
1570 }
1571
1572 if (sfi->sgid > VSC9959_PSFP_GATE_ID_MAX ||
1573 sfi->fmid > VSC9959_PSFP_POLICER_MAX)
1574 return -EINVAL;
1575
1576 ocelot_write(ocelot,
1577 (sfi->sg_valid ? ANA_TABLES_SFIDTIDX_SGID_VALID : 0) |
1578 ANA_TABLES_SFIDTIDX_SGID(sfi->sgid) |
1579 (sfi->fm_valid ? ANA_TABLES_SFIDTIDX_POL_ENA : 0) |
1580 ANA_TABLES_SFIDTIDX_POL_IDX(sfi->fmid) |
1581 ANA_TABLES_SFIDTIDX_SFID_INDEX(sfi->index),
1582 ANA_TABLES_SFIDTIDX);
1583
1584 ocelot_write(ocelot,
1585 (sfi->prio_valid ? ANA_TABLES_SFIDACCESS_IGR_PRIO_MATCH_ENA : 0) |
1586 ANA_TABLES_SFIDACCESS_IGR_PRIO(sfi->prio) |
1587 ANA_TABLES_SFIDACCESS_MAX_SDU_LEN(sfi->maxsdu) |
1588 ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(SFIDACCESS_CMD_WRITE),
1589 ANA_TABLES_SFIDACCESS);
1590
1591 return readx_poll_timeout(vsc9959_sfi_access_status, ocelot, val,
1592 (!ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(val)),
1593 10, 100000);
1594}
1595
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001596static int vsc9959_psfp_sfidmask_set(struct ocelot *ocelot, u32 sfid, int ports)
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001597{
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001598 u32 val;
1599
1600 ocelot_rmw(ocelot,
1601 ANA_TABLES_SFIDTIDX_SFID_INDEX(sfid),
1602 ANA_TABLES_SFIDTIDX_SFID_INDEX_M,
1603 ANA_TABLES_SFIDTIDX);
1604
1605 ocelot_write(ocelot,
1606 ANA_TABLES_SFID_MASK_IGR_PORT_MASK(ports) |
1607 ANA_TABLES_SFID_MASK_IGR_SRCPORT_MATCH_ENA,
1608 ANA_TABLES_SFID_MASK);
1609
1610 ocelot_rmw(ocelot,
1611 ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(SFIDACCESS_CMD_WRITE),
1612 ANA_TABLES_SFIDACCESS_SFID_TBL_CMD_M,
1613 ANA_TABLES_SFIDACCESS);
1614
1615 return readx_poll_timeout(vsc9959_sfi_access_status, ocelot, val,
1616 (!ANA_TABLES_SFIDACCESS_SFID_TBL_CMD(val)),
1617 10, 100000);
1618}
1619
1620static int vsc9959_psfp_sfi_list_add(struct ocelot *ocelot,
1621 struct felix_stream_filter *sfi,
1622 struct list_head *pos)
1623{
1624 struct felix_stream_filter *sfi_entry;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001625 int ret;
1626
Yihao Hane44aecc2021-12-06 22:44:18 -08001627 sfi_entry = kmemdup(sfi, sizeof(*sfi_entry), GFP_KERNEL);
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001628 if (!sfi_entry)
1629 return -ENOMEM;
1630
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001631 refcount_set(&sfi_entry->refcount, 1);
1632
1633 ret = vsc9959_psfp_sfi_set(ocelot, sfi_entry);
1634 if (ret) {
1635 kfree(sfi_entry);
1636 return ret;
1637 }
1638
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001639 vsc9959_psfp_sfidmask_set(ocelot, sfi->index, sfi->portmask);
1640
1641 list_add(&sfi_entry->list, pos);
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001642
1643 return 0;
1644}
1645
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001646static int vsc9959_psfp_sfi_table_add(struct ocelot *ocelot,
1647 struct felix_stream_filter *sfi)
1648{
1649 struct list_head *pos, *q, *last;
1650 struct felix_stream_filter *tmp;
1651 struct ocelot_psfp_list *psfp;
1652 u32 insert = 0;
1653
1654 psfp = &ocelot->psfp;
1655 last = &psfp->sfi_list;
1656
1657 list_for_each_safe(pos, q, &psfp->sfi_list) {
1658 tmp = list_entry(pos, struct felix_stream_filter, list);
1659 if (sfi->sg_valid == tmp->sg_valid &&
1660 sfi->fm_valid == tmp->fm_valid &&
1661 sfi->portmask == tmp->portmask &&
1662 tmp->sgid == sfi->sgid &&
1663 tmp->fmid == sfi->fmid) {
1664 sfi->index = tmp->index;
1665 refcount_inc(&tmp->refcount);
1666 return 0;
1667 }
1668 /* Make sure that the index is increasing in order. */
1669 if (tmp->index == insert) {
1670 last = pos;
1671 insert++;
1672 }
1673 }
1674 sfi->index = insert;
1675
1676 return vsc9959_psfp_sfi_list_add(ocelot, sfi, last);
1677}
1678
1679static int vsc9959_psfp_sfi_table_add2(struct ocelot *ocelot,
1680 struct felix_stream_filter *sfi,
1681 struct felix_stream_filter *sfi2)
1682{
1683 struct felix_stream_filter *tmp;
1684 struct list_head *pos, *q, *last;
1685 struct ocelot_psfp_list *psfp;
1686 u32 insert = 0;
1687 int ret;
1688
1689 psfp = &ocelot->psfp;
1690 last = &psfp->sfi_list;
1691
1692 list_for_each_safe(pos, q, &psfp->sfi_list) {
1693 tmp = list_entry(pos, struct felix_stream_filter, list);
1694 /* Make sure that the index is increasing in order. */
1695 if (tmp->index >= insert + 2)
1696 break;
1697
1698 insert = tmp->index + 1;
1699 last = pos;
1700 }
1701 sfi->index = insert;
1702
1703 ret = vsc9959_psfp_sfi_list_add(ocelot, sfi, last);
1704 if (ret)
1705 return ret;
1706
1707 sfi2->index = insert + 1;
1708
1709 return vsc9959_psfp_sfi_list_add(ocelot, sfi2, last->next);
1710}
1711
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001712static struct felix_stream_filter *
1713vsc9959_psfp_sfi_table_get(struct list_head *sfi_list, u32 index)
1714{
1715 struct felix_stream_filter *tmp;
1716
1717 list_for_each_entry(tmp, sfi_list, list)
1718 if (tmp->index == index)
1719 return tmp;
1720
1721 return NULL;
1722}
1723
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001724static void vsc9959_psfp_sfi_table_del(struct ocelot *ocelot, u32 index)
1725{
1726 struct felix_stream_filter *tmp, *n;
1727 struct ocelot_psfp_list *psfp;
1728 u8 z;
1729
1730 psfp = &ocelot->psfp;
1731
1732 list_for_each_entry_safe(tmp, n, &psfp->sfi_list, list)
1733 if (tmp->index == index) {
1734 z = refcount_dec_and_test(&tmp->refcount);
1735 if (z) {
1736 tmp->enable = 0;
1737 vsc9959_psfp_sfi_set(ocelot, tmp);
1738 list_del(&tmp->list);
1739 kfree(tmp);
1740 }
1741 break;
1742 }
1743}
1744
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001745static void vsc9959_psfp_parse_gate(const struct flow_action_entry *entry,
1746 struct felix_stream_gate *sgi)
1747{
1748 sgi->index = entry->gate.index;
1749 sgi->ipv_valid = (entry->gate.prio < 0) ? 0 : 1;
1750 sgi->init_ipv = (sgi->ipv_valid) ? entry->gate.prio : 0;
1751 sgi->basetime = entry->gate.basetime;
1752 sgi->cycletime = entry->gate.cycletime;
1753 sgi->num_entries = entry->gate.num_entries;
1754 sgi->enable = 1;
1755
1756 memcpy(sgi->entries, entry->gate.entries,
1757 entry->gate.num_entries * sizeof(struct action_gate_entry));
1758}
1759
1760static u32 vsc9959_sgi_cfg_status(struct ocelot *ocelot)
1761{
1762 return ocelot_read(ocelot, ANA_SG_ACCESS_CTRL);
1763}
1764
1765static int vsc9959_psfp_sgi_set(struct ocelot *ocelot,
1766 struct felix_stream_gate *sgi)
1767{
1768 struct action_gate_entry *e;
1769 struct timespec64 base_ts;
1770 u32 interval_sum = 0;
1771 u32 val;
1772 int i;
1773
1774 if (sgi->index > VSC9959_PSFP_GATE_ID_MAX)
1775 return -EINVAL;
1776
1777 ocelot_write(ocelot, ANA_SG_ACCESS_CTRL_SGID(sgi->index),
1778 ANA_SG_ACCESS_CTRL);
1779
1780 if (!sgi->enable) {
1781 ocelot_rmw(ocelot, ANA_SG_CONFIG_REG_3_INIT_GATE_STATE,
1782 ANA_SG_CONFIG_REG_3_INIT_GATE_STATE |
1783 ANA_SG_CONFIG_REG_3_GATE_ENABLE,
1784 ANA_SG_CONFIG_REG_3);
1785
1786 return 0;
1787 }
1788
1789 if (sgi->cycletime < VSC9959_PSFP_GATE_CYCLETIME_MIN ||
1790 sgi->cycletime > NSEC_PER_SEC)
1791 return -EINVAL;
1792
1793 if (sgi->num_entries > VSC9959_PSFP_GATE_LIST_NUM)
1794 return -EINVAL;
1795
1796 vsc9959_new_base_time(ocelot, sgi->basetime, sgi->cycletime, &base_ts);
1797 ocelot_write(ocelot, base_ts.tv_nsec, ANA_SG_CONFIG_REG_1);
1798 val = lower_32_bits(base_ts.tv_sec);
1799 ocelot_write(ocelot, val, ANA_SG_CONFIG_REG_2);
1800
1801 val = upper_32_bits(base_ts.tv_sec);
1802 ocelot_write(ocelot,
1803 (sgi->ipv_valid ? ANA_SG_CONFIG_REG_3_IPV_VALID : 0) |
1804 ANA_SG_CONFIG_REG_3_INIT_IPV(sgi->init_ipv) |
1805 ANA_SG_CONFIG_REG_3_GATE_ENABLE |
1806 ANA_SG_CONFIG_REG_3_LIST_LENGTH(sgi->num_entries) |
1807 ANA_SG_CONFIG_REG_3_INIT_GATE_STATE |
1808 ANA_SG_CONFIG_REG_3_BASE_TIME_SEC_MSB(val),
1809 ANA_SG_CONFIG_REG_3);
1810
1811 ocelot_write(ocelot, sgi->cycletime, ANA_SG_CONFIG_REG_4);
1812
1813 e = sgi->entries;
1814 for (i = 0; i < sgi->num_entries; i++) {
1815 u32 ips = (e[i].ipv < 0) ? 0 : (e[i].ipv + 8);
1816
1817 ocelot_write_rix(ocelot, ANA_SG_GCL_GS_CONFIG_IPS(ips) |
1818 (e[i].gate_state ?
1819 ANA_SG_GCL_GS_CONFIG_GATE_STATE : 0),
1820 ANA_SG_GCL_GS_CONFIG, i);
1821
1822 interval_sum += e[i].interval;
1823 ocelot_write_rix(ocelot, interval_sum, ANA_SG_GCL_TI_CONFIG, i);
1824 }
1825
1826 ocelot_rmw(ocelot, ANA_SG_ACCESS_CTRL_CONFIG_CHANGE,
1827 ANA_SG_ACCESS_CTRL_CONFIG_CHANGE,
1828 ANA_SG_ACCESS_CTRL);
1829
1830 return readx_poll_timeout(vsc9959_sgi_cfg_status, ocelot, val,
1831 (!(ANA_SG_ACCESS_CTRL_CONFIG_CHANGE & val)),
1832 10, 100000);
1833}
1834
1835static int vsc9959_psfp_sgi_table_add(struct ocelot *ocelot,
1836 struct felix_stream_gate *sgi)
1837{
1838 struct felix_stream_gate_entry *tmp;
1839 struct ocelot_psfp_list *psfp;
1840 int ret;
1841
1842 psfp = &ocelot->psfp;
1843
1844 list_for_each_entry(tmp, &psfp->sgi_list, list)
1845 if (tmp->index == sgi->index) {
1846 refcount_inc(&tmp->refcount);
1847 return 0;
1848 }
1849
1850 tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
1851 if (!tmp)
1852 return -ENOMEM;
1853
1854 ret = vsc9959_psfp_sgi_set(ocelot, sgi);
1855 if (ret) {
1856 kfree(tmp);
1857 return ret;
1858 }
1859
1860 tmp->index = sgi->index;
1861 refcount_set(&tmp->refcount, 1);
1862 list_add_tail(&tmp->list, &psfp->sgi_list);
1863
1864 return 0;
1865}
1866
1867static void vsc9959_psfp_sgi_table_del(struct ocelot *ocelot,
1868 u32 index)
1869{
1870 struct felix_stream_gate_entry *tmp, *n;
1871 struct felix_stream_gate sgi = {0};
1872 struct ocelot_psfp_list *psfp;
1873 u8 z;
1874
1875 psfp = &ocelot->psfp;
1876
1877 list_for_each_entry_safe(tmp, n, &psfp->sgi_list, list)
1878 if (tmp->index == index) {
1879 z = refcount_dec_and_test(&tmp->refcount);
1880 if (z) {
1881 sgi.index = index;
1882 sgi.enable = 0;
1883 vsc9959_psfp_sgi_set(ocelot, &sgi);
1884 list_del(&tmp->list);
1885 kfree(tmp);
1886 }
1887 break;
1888 }
1889}
1890
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001891static void vsc9959_psfp_counters_get(struct ocelot *ocelot, u32 index,
1892 struct felix_stream_filter_counters *counters)
1893{
1894 ocelot_rmw(ocelot, SYS_STAT_CFG_STAT_VIEW(index),
1895 SYS_STAT_CFG_STAT_VIEW_M,
1896 SYS_STAT_CFG);
1897
1898 counters->match = ocelot_read_gix(ocelot, SYS_CNT, 0x200);
1899 counters->not_pass_gate = ocelot_read_gix(ocelot, SYS_CNT, 0x201);
1900 counters->not_pass_sdu = ocelot_read_gix(ocelot, SYS_CNT, 0x202);
1901 counters->red = ocelot_read_gix(ocelot, SYS_CNT, 0x203);
1902
1903 /* Clear the PSFP counter. */
1904 ocelot_write(ocelot,
1905 SYS_STAT_CFG_STAT_VIEW(index) |
1906 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x10),
1907 SYS_STAT_CFG);
1908}
1909
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001910static int vsc9959_psfp_filter_add(struct ocelot *ocelot, int port,
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001911 struct flow_cls_offload *f)
1912{
1913 struct netlink_ext_ack *extack = f->common.extack;
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001914 struct felix_stream_filter old_sfi, *sfi_entry;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001915 struct felix_stream_filter sfi = {0};
1916 const struct flow_action_entry *a;
1917 struct felix_stream *stream_entry;
1918 struct felix_stream stream = {0};
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001919 struct felix_stream_gate *sgi;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001920 struct ocelot_psfp_list *psfp;
Xiaoliang Yang76c13ed2021-11-18 18:12:03 +08001921 struct ocelot_policer pol;
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001922 int ret, i, size;
Xiaoliang Yang76c13ed2021-11-18 18:12:03 +08001923 u64 rate, burst;
1924 u32 index;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001925
1926 psfp = &ocelot->psfp;
1927
1928 ret = vsc9959_stream_identify(f, &stream);
1929 if (ret) {
1930 NL_SET_ERR_MSG_MOD(extack, "Only can match on VID, PCP, and dest MAC");
1931 return ret;
1932 }
1933
1934 flow_action_for_each(i, a, &f->rule->action) {
1935 switch (a->id) {
1936 case FLOW_ACTION_GATE:
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001937 size = struct_size(sgi, entries, a->gate.num_entries);
1938 sgi = kzalloc(size, GFP_KERNEL);
1939 vsc9959_psfp_parse_gate(a, sgi);
1940 ret = vsc9959_psfp_sgi_table_add(ocelot, sgi);
1941 if (ret) {
1942 kfree(sgi);
Xiaoliang Yang76c13ed2021-11-18 18:12:03 +08001943 goto err;
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08001944 }
1945 sfi.sg_valid = 1;
1946 sfi.sgid = sgi->index;
1947 kfree(sgi);
1948 break;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001949 case FLOW_ACTION_POLICE:
Xiaoliang Yang76c13ed2021-11-18 18:12:03 +08001950 index = a->police.index + VSC9959_PSFP_POLICER_BASE;
1951 if (index > VSC9959_PSFP_POLICER_MAX) {
1952 ret = -EINVAL;
1953 goto err;
1954 }
1955
1956 rate = a->police.rate_bytes_ps;
1957 burst = rate * PSCHED_NS2TICKS(a->police.burst);
1958 pol = (struct ocelot_policer) {
1959 .burst = div_u64(burst, PSCHED_TICKS_PER_SEC),
1960 .rate = div_u64(rate, 1000) * 8,
1961 };
1962 ret = ocelot_vcap_policer_add(ocelot, index, &pol);
1963 if (ret)
1964 goto err;
1965
1966 sfi.fm_valid = 1;
1967 sfi.fmid = index;
1968 sfi.maxsdu = a->police.mtu;
1969 break;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001970 default:
1971 return -EOPNOTSUPP;
1972 }
1973 }
1974
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001975 stream.ports = BIT(port);
1976 stream.port = port;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001977
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001978 sfi.portmask = stream.ports;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08001979 sfi.prio_valid = (stream.prio < 0 ? 0 : 1);
1980 sfi.prio = (sfi.prio_valid ? stream.prio : 0);
1981 sfi.enable = 1;
1982
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08001983 /* Check if stream is set. */
1984 stream_entry = vsc9959_stream_table_lookup(&psfp->stream_list, &stream);
1985 if (stream_entry) {
1986 if (stream_entry->ports & BIT(port)) {
1987 NL_SET_ERR_MSG_MOD(extack,
1988 "The stream is added on this port");
1989 ret = -EEXIST;
1990 goto err;
1991 }
1992
1993 if (stream_entry->ports != BIT(stream_entry->port)) {
1994 NL_SET_ERR_MSG_MOD(extack,
1995 "The stream is added on two ports");
1996 ret = -EEXIST;
1997 goto err;
1998 }
1999
2000 stream_entry->ports |= BIT(port);
2001 stream.ports = stream_entry->ports;
2002
2003 sfi_entry = vsc9959_psfp_sfi_table_get(&psfp->sfi_list,
2004 stream_entry->sfid);
2005 memcpy(&old_sfi, sfi_entry, sizeof(old_sfi));
2006
2007 vsc9959_psfp_sfi_table_del(ocelot, stream_entry->sfid);
2008
2009 old_sfi.portmask = stream_entry->ports;
2010 sfi.portmask = stream.ports;
2011
2012 if (stream_entry->port > port) {
2013 ret = vsc9959_psfp_sfi_table_add2(ocelot, &sfi,
2014 &old_sfi);
2015 stream_entry->dummy = true;
2016 } else {
2017 ret = vsc9959_psfp_sfi_table_add2(ocelot, &old_sfi,
2018 &sfi);
2019 stream.dummy = true;
2020 }
2021 if (ret)
2022 goto err;
2023
2024 stream_entry->sfid = old_sfi.index;
2025 } else {
2026 ret = vsc9959_psfp_sfi_table_add(ocelot, &sfi);
2027 if (ret)
2028 goto err;
2029 }
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002030
2031 stream.sfid = sfi.index;
2032 stream.sfid_valid = 1;
2033 ret = vsc9959_stream_table_add(ocelot, &psfp->stream_list,
2034 &stream, extack);
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08002035 if (ret) {
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002036 vsc9959_psfp_sfi_table_del(ocelot, stream.sfid);
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08002037 goto err;
2038 }
2039
2040 return 0;
2041
2042err:
2043 if (sfi.sg_valid)
2044 vsc9959_psfp_sgi_table_del(ocelot, sfi.sgid);
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002045
Xiaoliang Yang76c13ed2021-11-18 18:12:03 +08002046 if (sfi.fm_valid)
2047 ocelot_vcap_policer_del(ocelot, sfi.fmid);
2048
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002049 return ret;
2050}
2051
2052static int vsc9959_psfp_filter_del(struct ocelot *ocelot,
2053 struct flow_cls_offload *f)
2054{
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08002055 struct felix_stream *stream, tmp, *stream_entry;
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08002056 static struct felix_stream_filter *sfi;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002057 struct ocelot_psfp_list *psfp;
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002058
2059 psfp = &ocelot->psfp;
2060
2061 stream = vsc9959_stream_table_get(&psfp->stream_list, f->cookie);
2062 if (!stream)
2063 return -ENOMEM;
2064
Xiaoliang Yang23ae3a72021-11-18 18:12:01 +08002065 sfi = vsc9959_psfp_sfi_table_get(&psfp->sfi_list, stream->sfid);
2066 if (!sfi)
2067 return -ENOMEM;
2068
2069 if (sfi->sg_valid)
2070 vsc9959_psfp_sgi_table_del(ocelot, sfi->sgid);
2071
Xiaoliang Yang76c13ed2021-11-18 18:12:03 +08002072 if (sfi->fm_valid)
2073 ocelot_vcap_policer_del(ocelot, sfi->fmid);
2074
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002075 vsc9959_psfp_sfi_table_del(ocelot, stream->sfid);
2076
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08002077 memcpy(&tmp, stream, sizeof(tmp));
2078
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002079 stream->sfid_valid = 0;
2080 vsc9959_stream_table_del(ocelot, stream);
2081
Xiaoliang Yanga7e13ed2021-11-18 18:12:04 +08002082 stream_entry = vsc9959_stream_table_lookup(&psfp->stream_list, &tmp);
2083 if (stream_entry) {
2084 stream_entry->ports = BIT(stream_entry->port);
2085 if (stream_entry->dummy) {
2086 stream_entry->dummy = false;
2087 vsc9959_mact_stream_set(ocelot, stream_entry, NULL);
2088 }
2089 vsc9959_psfp_sfidmask_set(ocelot, stream_entry->sfid,
2090 stream_entry->ports);
2091 }
2092
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002093 return 0;
2094}
2095
2096static int vsc9959_psfp_stats_get(struct ocelot *ocelot,
2097 struct flow_cls_offload *f,
2098 struct flow_stats *stats)
2099{
2100 struct felix_stream_filter_counters counters;
2101 struct ocelot_psfp_list *psfp;
2102 struct felix_stream *stream;
2103
2104 psfp = &ocelot->psfp;
2105 stream = vsc9959_stream_table_get(&psfp->stream_list, f->cookie);
2106 if (!stream)
2107 return -ENOMEM;
2108
2109 vsc9959_psfp_counters_get(ocelot, stream->sfid, &counters);
2110
2111 stats->pkts = counters.match;
2112 stats->drops = counters.not_pass_gate + counters.not_pass_sdu +
2113 counters.red;
2114
2115 return 0;
2116}
2117
2118static void vsc9959_psfp_init(struct ocelot *ocelot)
2119{
2120 struct ocelot_psfp_list *psfp = &ocelot->psfp;
2121
2122 INIT_LIST_HEAD(&psfp->stream_list);
2123 INIT_LIST_HEAD(&psfp->sfi_list);
2124 INIT_LIST_HEAD(&psfp->sgi_list);
2125}
2126
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002127/* When using cut-through forwarding and the egress port runs at a higher data
2128 * rate than the ingress port, the packet currently under transmission would
2129 * suffer an underrun since it would be transmitted faster than it is received.
2130 * The Felix switch implementation of cut-through forwarding does not check in
2131 * hardware whether this condition is satisfied or not, so we must restrict the
2132 * list of ports that have cut-through forwarding enabled on egress to only be
2133 * the ports operating at the lowest link speed within their respective
2134 * forwarding domain.
2135 */
2136static void vsc9959_cut_through_fwd(struct ocelot *ocelot)
2137{
2138 struct felix *felix = ocelot_to_felix(ocelot);
2139 struct dsa_switch *ds = felix->ds;
2140 int port, other_port;
2141
2142 lockdep_assert_held(&ocelot->fwd_domain_lock);
2143
2144 for (port = 0; port < ocelot->num_phys_ports; port++) {
2145 struct ocelot_port *ocelot_port = ocelot->ports[port];
2146 int min_speed = ocelot_port->speed;
2147 unsigned long mask = 0;
2148 u32 tmp, val = 0;
2149
2150 /* Disable cut-through on ports that are down */
2151 if (ocelot_port->speed <= 0)
2152 goto set;
2153
2154 if (dsa_is_cpu_port(ds, port)) {
2155 /* Ocelot switches forward from the NPI port towards
2156 * any port, regardless of it being in the NPI port's
2157 * forwarding domain or not.
2158 */
2159 mask = dsa_user_ports(ds);
2160 } else {
2161 mask = ocelot_get_bridge_fwd_mask(ocelot, port);
2162 mask &= ~BIT(port);
2163 if (ocelot->npi >= 0)
2164 mask |= BIT(ocelot->npi);
2165 else
2166 mask |= ocelot_get_dsa_8021q_cpu_mask(ocelot);
2167 }
2168
2169 /* Calculate the minimum link speed, among the ports that are
2170 * up, of this source port's forwarding domain.
2171 */
2172 for_each_set_bit(other_port, &mask, ocelot->num_phys_ports) {
2173 struct ocelot_port *other_ocelot_port;
2174
2175 other_ocelot_port = ocelot->ports[other_port];
2176 if (other_ocelot_port->speed <= 0)
2177 continue;
2178
2179 if (min_speed > other_ocelot_port->speed)
2180 min_speed = other_ocelot_port->speed;
2181 }
2182
2183 /* Enable cut-through forwarding for all traffic classes. */
2184 if (ocelot_port->speed == min_speed)
2185 val = GENMASK(7, 0);
2186
2187set:
2188 tmp = ocelot_read_rix(ocelot, ANA_CUT_THRU_CFG, port);
2189 if (tmp == val)
2190 continue;
2191
2192 dev_dbg(ocelot->dev,
2193 "port %d fwd mask 0x%lx speed %d min_speed %d, %s cut-through forwarding\n",
2194 port, mask, ocelot_port->speed, min_speed,
2195 val ? "enabling" : "disabling");
2196
2197 ocelot_write_rix(ocelot, val, ANA_CUT_THRU_CFG, port);
2198 }
2199}
2200
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002201static const struct ocelot_ops vsc9959_ops = {
2202 .reset = vsc9959_reset,
2203 .wm_enc = vsc9959_wm_enc,
2204 .wm_dec = vsc9959_wm_dec,
2205 .wm_stat = vsc9959_wm_stat,
2206 .port_to_netdev = felix_port_to_netdev,
2207 .netdev_to_port = felix_netdev_to_port,
2208 .psfp_init = vsc9959_psfp_init,
2209 .psfp_filter_add = vsc9959_psfp_filter_add,
2210 .psfp_filter_del = vsc9959_psfp_filter_del,
2211 .psfp_stats_get = vsc9959_psfp_stats_get,
Vladimir Oltean8abe1972021-11-25 14:58:08 +02002212 .cut_through_fwd = vsc9959_cut_through_fwd,
Xiaoliang Yang7d4b5642021-11-18 18:12:00 +08002213};
2214
Vladimir Oltean375e1312020-07-13 19:57:09 +03002215static const struct felix_info felix_info_vsc9959 = {
Vladimir Oltean56051942019-11-14 17:03:30 +02002216 .target_io_res = vsc9959_target_io_res,
2217 .port_io_res = vsc9959_port_io_res,
Vladimir Olteanbdeced72020-01-06 03:34:17 +02002218 .imdio_res = &vsc9959_imdio_res,
Vladimir Oltean56051942019-11-14 17:03:30 +02002219 .regfields = vsc9959_regfields,
2220 .map = vsc9959_regmap,
2221 .ops = &vsc9959_ops,
2222 .stats_layout = vsc9959_stats_layout,
2223 .num_stats = ARRAY_SIZE(vsc9959_stats_layout),
Vladimir Oltean07d985e2020-02-29 16:31:14 +02002224 .vcap = vsc9959_vcap_props,
Xiaoliang Yang77043c32021-11-18 18:12:02 +08002225 .vcap_pol_base = VSC9959_VCAP_POLICER_BASE,
2226 .vcap_pol_max = VSC9959_VCAP_POLICER_MAX,
2227 .vcap_pol_base2 = 0,
2228 .vcap_pol_max2 = 0,
Vladimir Oltean21ce7f32020-05-04 01:20:26 +03002229 .num_mact_rows = 2048,
Vladimir Oltean56051942019-11-14 17:03:30 +02002230 .num_ports = 6,
Vladimir Oltean70d39a62021-01-15 04:11:16 +02002231 .num_tx_queues = OCELOT_NUM_TC,
Vladimir Olteanc8c0ba42021-02-14 00:38:00 +02002232 .quirk_no_xtr_irq = true,
Vladimir Oltean2ac7c6c2020-09-18 13:57:52 +03002233 .ptp_caps = &vsc9959_ptp_caps,
Vladimir Olteanbdeced72020-01-06 03:34:17 +02002234 .mdio_bus_alloc = vsc9959_mdio_bus_alloc,
2235 .mdio_bus_free = vsc9959_mdio_bus_free,
Vladimir Oltean375e1312020-07-13 19:57:09 +03002236 .phylink_validate = vsc9959_phylink_validate,
Vladimir Olteanbdeced72020-01-06 03:34:17 +02002237 .prevalidate_phy_mode = vsc9959_prevalidate_phy_mode,
Vladimir Olteanf8320ec2020-09-18 13:57:49 +03002238 .port_setup_tc = vsc9959_port_setup_tc,
2239 .port_sched_speed_set = vsc9959_sched_speed_set,
Colin Foster242bd0c2021-12-07 09:00:29 -08002240 .init_regmap = ocelot_regmap_init,
Vladimir Oltean56051942019-11-14 17:03:30 +02002241};
Vladimir Oltean375e1312020-07-13 19:57:09 +03002242
2243static irqreturn_t felix_irq_handler(int irq, void *data)
2244{
2245 struct ocelot *ocelot = (struct ocelot *)data;
2246
2247 /* The INTB interrupt is used for both PTP TX timestamp interrupt
2248 * and preemption status change interrupt on each port.
2249 *
2250 * - Get txtstamp if have
2251 * - TODO: handle preemption. Without handling it, driver may get
2252 * interrupt storm.
2253 */
2254
2255 ocelot_get_txtstamp(ocelot);
2256
2257 return IRQ_HANDLED;
2258}
2259
2260static int felix_pci_probe(struct pci_dev *pdev,
2261 const struct pci_device_id *id)
2262{
2263 struct dsa_switch *ds;
2264 struct ocelot *ocelot;
2265 struct felix *felix;
2266 int err;
2267
2268 if (pdev->dev.of_node && !of_device_is_available(pdev->dev.of_node)) {
2269 dev_info(&pdev->dev, "device is disabled, skipping\n");
2270 return -ENODEV;
2271 }
2272
2273 err = pci_enable_device(pdev);
2274 if (err) {
2275 dev_err(&pdev->dev, "device enable failed\n");
2276 goto err_pci_enable;
2277 }
2278
Vladimir Oltean375e1312020-07-13 19:57:09 +03002279 felix = kzalloc(sizeof(struct felix), GFP_KERNEL);
2280 if (!felix) {
2281 err = -ENOMEM;
2282 dev_err(&pdev->dev, "Failed to allocate driver memory\n");
2283 goto err_alloc_felix;
2284 }
2285
2286 pci_set_drvdata(pdev, felix);
2287 ocelot = &felix->ocelot;
2288 ocelot->dev = &pdev->dev;
Vladimir Oltean70d39a62021-01-15 04:11:16 +02002289 ocelot->num_flooding_pgids = OCELOT_NUM_TC;
Vladimir Oltean375e1312020-07-13 19:57:09 +03002290 felix->info = &felix_info_vsc9959;
Colin Fosterc9910482021-12-07 09:00:27 -08002291 felix->switch_base = pci_resource_start(pdev, VSC9959_SWITCH_PCI_BAR);
2292 felix->imdio_base = pci_resource_start(pdev, VSC9959_IMDIO_PCI_BAR);
Vladimir Oltean375e1312020-07-13 19:57:09 +03002293
2294 pci_set_master(pdev);
2295
2296 err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL,
2297 &felix_irq_handler, IRQF_ONESHOT,
2298 "felix-intb", ocelot);
2299 if (err) {
2300 dev_err(&pdev->dev, "Failed to request irq\n");
2301 goto err_alloc_irq;
2302 }
2303
2304 ocelot->ptp = 1;
2305
2306 ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL);
2307 if (!ds) {
2308 err = -ENOMEM;
2309 dev_err(&pdev->dev, "Failed to allocate DSA switch\n");
2310 goto err_alloc_ds;
2311 }
2312
2313 ds->dev = &pdev->dev;
2314 ds->num_ports = felix->info->num_ports;
2315 ds->num_tx_queues = felix->info->num_tx_queues;
2316 ds->ops = &felix_switch_ops;
2317 ds->priv = ocelot;
2318 felix->ds = ds;
Vladimir Olteanadb3dcc2021-01-29 03:00:07 +02002319 felix->tag_proto = DSA_TAG_PROTO_OCELOT;
Vladimir Oltean375e1312020-07-13 19:57:09 +03002320
2321 err = dsa_register_switch(ds);
2322 if (err) {
2323 dev_err(&pdev->dev, "Failed to register DSA switch: %d\n", err);
2324 goto err_register_ds;
2325 }
2326
2327 return 0;
2328
2329err_register_ds:
2330 kfree(ds);
2331err_alloc_ds:
2332err_alloc_irq:
Vladimir Oltean375e1312020-07-13 19:57:09 +03002333 kfree(felix);
Vladimir Oltean537e2b82021-01-09 22:34:15 +02002334err_alloc_felix:
Vladimir Oltean375e1312020-07-13 19:57:09 +03002335 pci_disable_device(pdev);
2336err_pci_enable:
2337 return err;
2338}
2339
2340static void felix_pci_remove(struct pci_dev *pdev)
2341{
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002342 struct felix *felix = pci_get_drvdata(pdev);
Vladimir Oltean375e1312020-07-13 19:57:09 +03002343
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002344 if (!felix)
2345 return;
Vladimir Oltean375e1312020-07-13 19:57:09 +03002346
2347 dsa_unregister_switch(felix->ds);
2348
2349 kfree(felix->ds);
2350 kfree(felix);
2351
2352 pci_disable_device(pdev);
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002353
2354 pci_set_drvdata(pdev, NULL);
2355}
2356
2357static void felix_pci_shutdown(struct pci_dev *pdev)
2358{
2359 struct felix *felix = pci_get_drvdata(pdev);
2360
2361 if (!felix)
2362 return;
2363
2364 dsa_switch_shutdown(felix->ds);
2365
2366 pci_set_drvdata(pdev, NULL);
Vladimir Oltean375e1312020-07-13 19:57:09 +03002367}
2368
2369static struct pci_device_id felix_ids[] = {
2370 {
2371 /* NXP LS1028A */
2372 PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, 0xEEF0),
2373 },
2374 { 0, }
2375};
2376MODULE_DEVICE_TABLE(pci, felix_ids);
2377
Vladimir Olteand60bc622020-09-18 13:57:53 +03002378static struct pci_driver felix_vsc9959_pci_driver = {
Vladimir Oltean375e1312020-07-13 19:57:09 +03002379 .name = "mscc_felix",
2380 .id_table = felix_ids,
2381 .probe = felix_pci_probe,
2382 .remove = felix_pci_remove,
Vladimir Oltean0650bf52021-09-17 16:34:33 +03002383 .shutdown = felix_pci_shutdown,
Vladimir Oltean375e1312020-07-13 19:57:09 +03002384};
Vladimir Olteand60bc622020-09-18 13:57:53 +03002385module_pci_driver(felix_vsc9959_pci_driver);
2386
2387MODULE_DESCRIPTION("Felix Switch driver");
2388MODULE_LICENSE("GPL v2");