blob: 9e04786e3139905b0a905650d8e0a04a69c7dda0 [file] [log] [blame]
Florian Fainelli73181662017-01-30 09:48:43 -08001/*
2 * Broadcom Starfighter 2 DSA switch CFP support
3 *
4 * Copyright (C) 2016, Broadcom
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/list.h>
Florian Fainelli73181662017-01-30 09:48:43 -080013#include <linux/ethtool.h>
14#include <linux/if_ether.h>
15#include <linux/in.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020016#include <linux/netdevice.h>
17#include <net/dsa.h>
Florian Fainelli73181662017-01-30 09:48:43 -080018#include <linux/bitmap.h>
19
20#include "bcm_sf2.h"
21#include "bcm_sf2_regs.h"
22
Florian Fainelli5d80bcb2017-10-20 14:39:45 -070023struct cfp_udf_slice_layout {
24 u8 slices[UDFS_PER_SLICE];
Florian Fainelli73181662017-01-30 09:48:43 -080025 u32 mask_value;
Florian Fainelli5d80bcb2017-10-20 14:39:45 -070026 u32 base_offset;
Florian Fainelli73181662017-01-30 09:48:43 -080027};
28
Florian Fainelli5d80bcb2017-10-20 14:39:45 -070029struct cfp_udf_layout {
30 struct cfp_udf_slice_layout udfs[UDF_NUM_SLICES];
31};
32
33static const u8 zero_slice[UDFS_PER_SLICE] = { };
34
Florian Fainelli73181662017-01-30 09:48:43 -080035/* UDF slices layout for a TCPv4/UDPv4 specification */
36static const struct cfp_udf_layout udf_tcpip4_layout = {
Florian Fainelli5d80bcb2017-10-20 14:39:45 -070037 .udfs = {
38 [1] = {
39 .slices = {
40 /* End of L2, byte offset 12, src IP[0:15] */
41 CFG_UDF_EOL2 | 6,
42 /* End of L2, byte offset 14, src IP[16:31] */
43 CFG_UDF_EOL2 | 7,
44 /* End of L2, byte offset 16, dst IP[0:15] */
45 CFG_UDF_EOL2 | 8,
46 /* End of L2, byte offset 18, dst IP[16:31] */
47 CFG_UDF_EOL2 | 9,
48 /* End of L3, byte offset 0, src port */
49 CFG_UDF_EOL3 | 0,
50 /* End of L3, byte offset 2, dst port */
51 CFG_UDF_EOL3 | 1,
52 0, 0, 0
53 },
54 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
55 .base_offset = CORE_UDF_0_A_0_8_PORT_0 + UDF_SLICE_OFFSET,
56 },
Florian Fainelli73181662017-01-30 09:48:43 -080057 },
Florian Fainelli73181662017-01-30 09:48:43 -080058};
59
Florian Fainelliba0696c2017-10-20 14:39:47 -070060/* UDF slices layout for a TCPv6/UDPv6 specification */
61static const struct cfp_udf_layout udf_tcpip6_layout = {
62 .udfs = {
63 [0] = {
64 .slices = {
65 /* End of L2, byte offset 8, src IP[0:15] */
66 CFG_UDF_EOL2 | 4,
67 /* End of L2, byte offset 10, src IP[16:31] */
68 CFG_UDF_EOL2 | 5,
69 /* End of L2, byte offset 12, src IP[32:47] */
70 CFG_UDF_EOL2 | 6,
71 /* End of L2, byte offset 14, src IP[48:63] */
72 CFG_UDF_EOL2 | 7,
73 /* End of L2, byte offset 16, src IP[64:79] */
74 CFG_UDF_EOL2 | 8,
75 /* End of L2, byte offset 18, src IP[80:95] */
76 CFG_UDF_EOL2 | 9,
77 /* End of L2, byte offset 20, src IP[96:111] */
78 CFG_UDF_EOL2 | 10,
79 /* End of L2, byte offset 22, src IP[112:127] */
80 CFG_UDF_EOL2 | 11,
81 /* End of L3, byte offset 0, src port */
82 CFG_UDF_EOL3 | 0,
83 },
84 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
85 .base_offset = CORE_UDF_0_B_0_8_PORT_0,
86 },
87 [3] = {
88 .slices = {
89 /* End of L2, byte offset 24, dst IP[0:15] */
90 CFG_UDF_EOL2 | 12,
91 /* End of L2, byte offset 26, dst IP[16:31] */
92 CFG_UDF_EOL2 | 13,
93 /* End of L2, byte offset 28, dst IP[32:47] */
94 CFG_UDF_EOL2 | 14,
95 /* End of L2, byte offset 30, dst IP[48:63] */
96 CFG_UDF_EOL2 | 15,
97 /* End of L2, byte offset 32, dst IP[64:79] */
98 CFG_UDF_EOL2 | 16,
99 /* End of L2, byte offset 34, dst IP[80:95] */
100 CFG_UDF_EOL2 | 17,
101 /* End of L2, byte offset 36, dst IP[96:111] */
102 CFG_UDF_EOL2 | 18,
103 /* End of L2, byte offset 38, dst IP[112:127] */
104 CFG_UDF_EOL2 | 19,
105 /* End of L3, byte offset 2, dst port */
106 CFG_UDF_EOL3 | 1,
107 },
108 .mask_value = L3_FRAMING_MASK | IPPROTO_MASK | IP_FRAG,
109 .base_offset = CORE_UDF_0_D_0_11_PORT_0,
110 },
111 },
112};
113
Florian Fainelli73181662017-01-30 09:48:43 -0800114static inline unsigned int bcm_sf2_get_num_udf_slices(const u8 *layout)
115{
116 unsigned int i, count = 0;
117
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700118 for (i = 0; i < UDFS_PER_SLICE; i++) {
Florian Fainelli73181662017-01-30 09:48:43 -0800119 if (layout[i] != 0)
120 count++;
121 }
122
123 return count;
124}
125
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700126static inline u32 udf_upper_bits(unsigned int num_udf)
Florian Fainelli73181662017-01-30 09:48:43 -0800127{
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700128 return GENMASK(num_udf - 1, 0) >> (UDFS_PER_SLICE - 1);
129}
130
131static inline u32 udf_lower_bits(unsigned int num_udf)
132{
133 return (u8)GENMASK(num_udf - 1, 0);
134}
135
136static unsigned int bcm_sf2_get_slice_number(const struct cfp_udf_layout *l,
137 unsigned int start)
138{
139 const struct cfp_udf_slice_layout *slice_layout;
140 unsigned int slice_idx;
141
142 for (slice_idx = start; slice_idx < UDF_NUM_SLICES; slice_idx++) {
143 slice_layout = &l->udfs[slice_idx];
144 if (memcmp(slice_layout->slices, zero_slice,
145 sizeof(zero_slice)))
146 break;
147 }
148
149 return slice_idx;
150}
151
152static void bcm_sf2_cfp_udf_set(struct bcm_sf2_priv *priv,
153 const struct cfp_udf_layout *layout,
154 unsigned int slice_num)
155{
156 u32 offset = layout->udfs[slice_num].base_offset;
Florian Fainelli73181662017-01-30 09:48:43 -0800157 unsigned int i;
158
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700159 for (i = 0; i < UDFS_PER_SLICE; i++)
160 core_writel(priv, layout->udfs[slice_num].slices[i],
161 offset + i * 4);
Florian Fainelli73181662017-01-30 09:48:43 -0800162}
163
164static int bcm_sf2_cfp_op(struct bcm_sf2_priv *priv, unsigned int op)
165{
166 unsigned int timeout = 1000;
167 u32 reg;
168
169 reg = core_readl(priv, CORE_CFP_ACC);
170 reg &= ~(OP_SEL_MASK | RAM_SEL_MASK);
171 reg |= OP_STR_DONE | op;
172 core_writel(priv, reg, CORE_CFP_ACC);
173
174 do {
175 reg = core_readl(priv, CORE_CFP_ACC);
176 if (!(reg & OP_STR_DONE))
177 break;
178
179 cpu_relax();
180 } while (timeout--);
181
182 if (!timeout)
183 return -ETIMEDOUT;
184
185 return 0;
186}
187
188static inline void bcm_sf2_cfp_rule_addr_set(struct bcm_sf2_priv *priv,
189 unsigned int addr)
190{
191 u32 reg;
192
Florian Fainellidf191632017-08-30 12:39:33 -0700193 WARN_ON(addr >= priv->num_cfp_rules);
Florian Fainelli73181662017-01-30 09:48:43 -0800194
195 reg = core_readl(priv, CORE_CFP_ACC);
196 reg &= ~(XCESS_ADDR_MASK << XCESS_ADDR_SHIFT);
197 reg |= addr << XCESS_ADDR_SHIFT;
198 core_writel(priv, reg, CORE_CFP_ACC);
199}
200
201static inline unsigned int bcm_sf2_cfp_rule_size(struct bcm_sf2_priv *priv)
202{
203 /* Entry #0 is reserved */
Florian Fainellidf191632017-08-30 12:39:33 -0700204 return priv->num_cfp_rules - 1;
Florian Fainelli73181662017-01-30 09:48:43 -0800205}
206
Florian Fainelli33061452017-10-20 14:39:44 -0700207static int bcm_sf2_cfp_act_pol_set(struct bcm_sf2_priv *priv,
208 unsigned int rule_index,
209 unsigned int port_num,
Florian Fainelliba0696c2017-10-20 14:39:47 -0700210 unsigned int queue_num,
211 bool fwd_map_change)
Florian Fainelli73181662017-01-30 09:48:43 -0800212{
Florian Fainelli33061452017-10-20 14:39:44 -0700213 int ret;
214 u32 reg;
215
216 /* Replace ARL derived destination with DST_MAP derived, define
217 * which port and queue this should be forwarded to.
218 */
Florian Fainelliba0696c2017-10-20 14:39:47 -0700219 if (fwd_map_change)
220 reg = CHANGE_FWRD_MAP_IB_REP_ARL |
221 BIT(port_num + DST_MAP_IB_SHIFT) |
222 CHANGE_TC | queue_num << NEW_TC_SHIFT;
223 else
224 reg = 0;
Florian Fainelli33061452017-10-20 14:39:44 -0700225
226 core_writel(priv, reg, CORE_ACT_POL_DATA0);
227
228 /* Set classification ID that needs to be put in Broadcom tag */
Florian Fainelliba0696c2017-10-20 14:39:47 -0700229 core_writel(priv, rule_index << CHAIN_ID_SHIFT, CORE_ACT_POL_DATA1);
Florian Fainelli33061452017-10-20 14:39:44 -0700230
231 core_writel(priv, 0, CORE_ACT_POL_DATA2);
232
233 /* Configure policer RAM now */
234 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | ACT_POL_RAM);
235 if (ret) {
236 pr_err("Policer entry at %d failed\n", rule_index);
237 return ret;
238 }
239
240 /* Disable the policer */
241 core_writel(priv, POLICER_MODE_DISABLE, CORE_RATE_METER0);
242
243 /* Now the rate meter */
244 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | RATE_METER_RAM);
245 if (ret) {
246 pr_err("Meter entry at %d failed\n", rule_index);
247 return ret;
248 }
249
250 return 0;
251}
252
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700253static void bcm_sf2_cfp_slice_ipv4(struct bcm_sf2_priv *priv,
254 struct ethtool_tcpip4_spec *v4_spec,
255 unsigned int slice_num,
256 bool mask)
257{
258 u32 reg, offset;
259
260 /* C-Tag [31:24]
261 * UDF_n_A8 [23:8]
262 * UDF_n_A7 [7:0]
263 */
264 reg = 0;
265 if (mask)
266 offset = CORE_CFP_MASK_PORT(4);
267 else
268 offset = CORE_CFP_DATA_PORT(4);
269 core_writel(priv, reg, offset);
270
271 /* UDF_n_A7 [31:24]
272 * UDF_n_A6 [23:8]
273 * UDF_n_A5 [7:0]
274 */
275 reg = be16_to_cpu(v4_spec->pdst) >> 8;
276 if (mask)
277 offset = CORE_CFP_MASK_PORT(3);
278 else
279 offset = CORE_CFP_DATA_PORT(3);
280 core_writel(priv, reg, offset);
281
282 /* UDF_n_A5 [31:24]
283 * UDF_n_A4 [23:8]
284 * UDF_n_A3 [7:0]
285 */
286 reg = (be16_to_cpu(v4_spec->pdst) & 0xff) << 24 |
287 (u32)be16_to_cpu(v4_spec->psrc) << 8 |
288 (be32_to_cpu(v4_spec->ip4dst) & 0x0000ff00) >> 8;
289 if (mask)
290 offset = CORE_CFP_MASK_PORT(2);
291 else
292 offset = CORE_CFP_DATA_PORT(2);
293 core_writel(priv, reg, offset);
294
295 /* UDF_n_A3 [31:24]
296 * UDF_n_A2 [23:8]
297 * UDF_n_A1 [7:0]
298 */
299 reg = (u32)(be32_to_cpu(v4_spec->ip4dst) & 0xff) << 24 |
300 (u32)(be32_to_cpu(v4_spec->ip4dst) >> 16) << 8 |
301 (be32_to_cpu(v4_spec->ip4src) & 0x0000ff00) >> 8;
302 if (mask)
303 offset = CORE_CFP_MASK_PORT(1);
304 else
305 offset = CORE_CFP_DATA_PORT(1);
306 core_writel(priv, reg, offset);
307
308 /* UDF_n_A1 [31:24]
309 * UDF_n_A0 [23:8]
310 * Reserved [7:4]
311 * Slice ID [3:2]
312 * Slice valid [1:0]
313 */
314 reg = (u32)(be32_to_cpu(v4_spec->ip4src) & 0xff) << 24 |
315 (u32)(be32_to_cpu(v4_spec->ip4src) >> 16) << 8 |
316 SLICE_NUM(slice_num) | SLICE_VALID;
317 if (mask)
318 offset = CORE_CFP_MASK_PORT(0);
319 else
320 offset = CORE_CFP_DATA_PORT(0);
321 core_writel(priv, reg, offset);
322}
323
Florian Fainelli33061452017-10-20 14:39:44 -0700324static int bcm_sf2_cfp_ipv4_rule_set(struct bcm_sf2_priv *priv, int port,
325 unsigned int port_num,
326 unsigned int queue_num,
327 struct ethtool_rx_flow_spec *fs)
328{
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700329 struct ethtool_tcpip4_spec *v4_spec, *v4_m_spec;
Florian Fainelli73181662017-01-30 09:48:43 -0800330 const struct cfp_udf_layout *layout;
331 unsigned int slice_num, rule_index;
Florian Fainelli73181662017-01-30 09:48:43 -0800332 u8 ip_proto, ip_frag;
333 u8 num_udf;
334 u32 reg;
335 int ret;
336
Florian Fainelli73181662017-01-30 09:48:43 -0800337 switch (fs->flow_type & ~FLOW_EXT) {
338 case TCP_V4_FLOW:
339 ip_proto = IPPROTO_TCP;
340 v4_spec = &fs->h_u.tcp_ip4_spec;
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700341 v4_m_spec = &fs->m_u.tcp_ip4_spec;
Florian Fainelli73181662017-01-30 09:48:43 -0800342 break;
343 case UDP_V4_FLOW:
344 ip_proto = IPPROTO_UDP;
345 v4_spec = &fs->h_u.udp_ip4_spec;
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700346 v4_m_spec = &fs->m_u.udp_ip4_spec;
Florian Fainelli73181662017-01-30 09:48:43 -0800347 break;
348 default:
349 return -EINVAL;
350 }
351
Florian Fainelli33061452017-10-20 14:39:44 -0700352 ip_frag = be32_to_cpu(fs->m_ext.data[0]);
353
354 /* Locate the first rule available */
355 if (fs->location == RX_CLS_LOC_ANY)
356 rule_index = find_first_zero_bit(priv->cfp.used,
Florian Fainelli43a5e002018-05-15 16:01:23 -0700357 priv->num_cfp_rules);
Florian Fainelli33061452017-10-20 14:39:44 -0700358 else
359 rule_index = fs->location;
360
Florian Fainelli43a5e002018-05-15 16:01:23 -0700361 if (rule_index > bcm_sf2_cfp_rule_size(priv))
362 return -ENOSPC;
363
Florian Fainelli73181662017-01-30 09:48:43 -0800364 layout = &udf_tcpip4_layout;
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700365 /* We only use one UDF slice for now */
366 slice_num = bcm_sf2_get_slice_number(layout, 0);
367 if (slice_num == UDF_NUM_SLICES)
368 return -EINVAL;
369
370 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
Florian Fainelli73181662017-01-30 09:48:43 -0800371
372 /* Apply the UDF layout for this filter */
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700373 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
Florian Fainelli73181662017-01-30 09:48:43 -0800374
375 /* Apply to all packets received through this port */
376 core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
377
Florian Fainelli33061452017-10-20 14:39:44 -0700378 /* Source port map match */
379 core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
380
Florian Fainelli73181662017-01-30 09:48:43 -0800381 /* S-Tag status [31:30]
382 * C-Tag status [29:28]
383 * L2 framing [27:26]
384 * L3 framing [25:24]
385 * IP ToS [23:16]
386 * IP proto [15:08]
387 * IP Fragm [7]
388 * Non 1st frag [6]
389 * IP Authen [5]
390 * TTL range [4:3]
391 * PPPoE session [2]
392 * Reserved [1]
393 * UDF_Valid[8] [0]
394 */
Florian Fainelli39cdd342017-10-20 14:39:43 -0700395 core_writel(priv, v4_spec->tos << IPTOS_SHIFT |
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700396 ip_proto << IPPROTO_SHIFT | ip_frag << IP_FRAG_SHIFT |
397 udf_upper_bits(num_udf),
Florian Fainelli73181662017-01-30 09:48:43 -0800398 CORE_CFP_DATA_PORT(6));
399
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700400 /* Mask with the specific layout for IPv4 packets */
401 core_writel(priv, layout->udfs[slice_num].mask_value |
402 udf_upper_bits(num_udf), CORE_CFP_MASK_PORT(6));
403
Florian Fainelli73181662017-01-30 09:48:43 -0800404 /* UDF_Valid[7:0] [31:24]
405 * S-Tag [23:8]
406 * C-Tag [7:0]
407 */
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700408 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
Florian Fainelli73181662017-01-30 09:48:43 -0800409
Florian Fainelli73181662017-01-30 09:48:43 -0800410 /* Mask all but valid UDFs */
Florian Fainelli5d80bcb2017-10-20 14:39:45 -0700411 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
Florian Fainelli73181662017-01-30 09:48:43 -0800412
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700413 /* Program the match and the mask */
414 bcm_sf2_cfp_slice_ipv4(priv, v4_spec, slice_num, false);
415 bcm_sf2_cfp_slice_ipv4(priv, v4_m_spec, SLICE_NUM_MASK, true);
Florian Fainelli73181662017-01-30 09:48:43 -0800416
Florian Fainelli73181662017-01-30 09:48:43 -0800417 /* Insert into TCAM now */
418 bcm_sf2_cfp_rule_addr_set(priv, rule_index);
419
420 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
421 if (ret) {
422 pr_err("TCAM entry at addr %d failed\n", rule_index);
423 return ret;
424 }
425
Florian Fainelli33061452017-10-20 14:39:44 -0700426 /* Insert into Action and policer RAMs now */
Florian Fainelliba0696c2017-10-20 14:39:47 -0700427 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index, port_num,
428 queue_num, true);
Florian Fainelli33061452017-10-20 14:39:44 -0700429 if (ret)
Florian Fainelli73181662017-01-30 09:48:43 -0800430 return ret;
Florian Fainelli73181662017-01-30 09:48:43 -0800431
432 /* Turn on CFP for this rule now */
433 reg = core_readl(priv, CORE_CFP_CTL_REG);
434 reg |= BIT(port);
435 core_writel(priv, reg, CORE_CFP_CTL_REG);
436
437 /* Flag the rule as being used and return it */
438 set_bit(rule_index, priv->cfp.used);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700439 set_bit(rule_index, priv->cfp.unique);
Florian Fainelli73181662017-01-30 09:48:43 -0800440 fs->location = rule_index;
441
442 return 0;
443}
444
Florian Fainelliba0696c2017-10-20 14:39:47 -0700445static void bcm_sf2_cfp_slice_ipv6(struct bcm_sf2_priv *priv,
446 const __be32 *ip6_addr, const __be16 port,
Florian Fainellidd8eff62017-10-20 14:39:49 -0700447 unsigned int slice_num,
448 bool mask)
Florian Fainelliba0696c2017-10-20 14:39:47 -0700449{
Florian Fainellidd8eff62017-10-20 14:39:49 -0700450 u32 reg, tmp, val, offset;
Florian Fainelliba0696c2017-10-20 14:39:47 -0700451
452 /* C-Tag [31:24]
453 * UDF_n_B8 [23:8] (port)
454 * UDF_n_B7 (upper) [7:0] (addr[15:8])
455 */
456 reg = be32_to_cpu(ip6_addr[3]);
457 val = (u32)be16_to_cpu(port) << 8 | ((reg >> 8) & 0xff);
Florian Fainellidd8eff62017-10-20 14:39:49 -0700458 if (mask)
459 offset = CORE_CFP_MASK_PORT(4);
460 else
461 offset = CORE_CFP_DATA_PORT(4);
462 core_writel(priv, val, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700463
464 /* UDF_n_B7 (lower) [31:24] (addr[7:0])
465 * UDF_n_B6 [23:8] (addr[31:16])
466 * UDF_n_B5 (upper) [7:0] (addr[47:40])
467 */
468 tmp = be32_to_cpu(ip6_addr[2]);
469 val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
470 ((tmp >> 8) & 0xff);
Florian Fainellidd8eff62017-10-20 14:39:49 -0700471 if (mask)
472 offset = CORE_CFP_MASK_PORT(3);
473 else
474 offset = CORE_CFP_DATA_PORT(3);
475 core_writel(priv, val, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700476
477 /* UDF_n_B5 (lower) [31:24] (addr[39:32])
478 * UDF_n_B4 [23:8] (addr[63:48])
479 * UDF_n_B3 (upper) [7:0] (addr[79:72])
480 */
481 reg = be32_to_cpu(ip6_addr[1]);
482 val = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
483 ((reg >> 8) & 0xff);
Florian Fainellidd8eff62017-10-20 14:39:49 -0700484 if (mask)
485 offset = CORE_CFP_MASK_PORT(2);
486 else
487 offset = CORE_CFP_DATA_PORT(2);
488 core_writel(priv, val, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700489
490 /* UDF_n_B3 (lower) [31:24] (addr[71:64])
491 * UDF_n_B2 [23:8] (addr[95:80])
492 * UDF_n_B1 (upper) [7:0] (addr[111:104])
493 */
494 tmp = be32_to_cpu(ip6_addr[0]);
495 val = (u32)(reg & 0xff) << 24 | (u32)(reg >> 16) << 8 |
496 ((tmp >> 8) & 0xff);
Florian Fainellidd8eff62017-10-20 14:39:49 -0700497 if (mask)
498 offset = CORE_CFP_MASK_PORT(1);
499 else
500 offset = CORE_CFP_DATA_PORT(1);
501 core_writel(priv, val, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700502
503 /* UDF_n_B1 (lower) [31:24] (addr[103:96])
504 * UDF_n_B0 [23:8] (addr[127:112])
505 * Reserved [7:4]
506 * Slice ID [3:2]
507 * Slice valid [1:0]
508 */
509 reg = (u32)(tmp & 0xff) << 24 | (u32)(tmp >> 16) << 8 |
510 SLICE_NUM(slice_num) | SLICE_VALID;
Florian Fainellidd8eff62017-10-20 14:39:49 -0700511 if (mask)
512 offset = CORE_CFP_MASK_PORT(0);
513 else
514 offset = CORE_CFP_DATA_PORT(0);
515 core_writel(priv, reg, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700516}
517
518static int bcm_sf2_cfp_ipv6_rule_set(struct bcm_sf2_priv *priv, int port,
519 unsigned int port_num,
520 unsigned int queue_num,
521 struct ethtool_rx_flow_spec *fs)
522{
Florian Fainellidd8eff62017-10-20 14:39:49 -0700523 struct ethtool_tcpip6_spec *v6_spec, *v6_m_spec;
Florian Fainelliba0696c2017-10-20 14:39:47 -0700524 unsigned int slice_num, rule_index[2];
Florian Fainelliba0696c2017-10-20 14:39:47 -0700525 const struct cfp_udf_layout *layout;
526 u8 ip_proto, ip_frag;
527 int ret = 0;
528 u8 num_udf;
529 u32 reg;
530
531 switch (fs->flow_type & ~FLOW_EXT) {
532 case TCP_V6_FLOW:
533 ip_proto = IPPROTO_TCP;
534 v6_spec = &fs->h_u.tcp_ip6_spec;
Florian Fainellidd8eff62017-10-20 14:39:49 -0700535 v6_m_spec = &fs->m_u.tcp_ip6_spec;
Florian Fainelliba0696c2017-10-20 14:39:47 -0700536 break;
537 case UDP_V6_FLOW:
538 ip_proto = IPPROTO_UDP;
539 v6_spec = &fs->h_u.udp_ip6_spec;
Florian Fainellidd8eff62017-10-20 14:39:49 -0700540 v6_m_spec = &fs->m_u.udp_ip6_spec;
Florian Fainelliba0696c2017-10-20 14:39:47 -0700541 break;
542 default:
543 return -EINVAL;
544 }
545
546 ip_frag = be32_to_cpu(fs->m_ext.data[0]);
547
548 layout = &udf_tcpip6_layout;
549 slice_num = bcm_sf2_get_slice_number(layout, 0);
550 if (slice_num == UDF_NUM_SLICES)
551 return -EINVAL;
552
553 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
554
555 /* Negotiate two indexes, one for the second half which we are chained
556 * from, which is what we will return to user-space, and a second one
557 * which is used to store its first half. That first half does not
558 * allow any choice of placement, so it just needs to find the next
559 * available bit. We return the second half as fs->location because
560 * that helps with the rule lookup later on since the second half is
561 * chained from its first half, we can easily identify IPv6 CFP rules
562 * by looking whether they carry a CHAIN_ID.
563 *
564 * We also want the second half to have a lower rule_index than its
565 * first half because the HW search is by incrementing addresses.
566 */
567 if (fs->location == RX_CLS_LOC_ANY)
568 rule_index[0] = find_first_zero_bit(priv->cfp.used,
569 bcm_sf2_cfp_rule_size(priv));
570 else
571 rule_index[0] = fs->location;
572
573 /* Flag it as used (cleared on error path) such that we can immediately
574 * obtain a second one to chain from.
575 */
576 set_bit(rule_index[0], priv->cfp.used);
577
578 rule_index[1] = find_first_zero_bit(priv->cfp.used,
579 bcm_sf2_cfp_rule_size(priv));
580 if (rule_index[1] > bcm_sf2_cfp_rule_size(priv)) {
581 ret = -ENOSPC;
582 goto out_err;
583 }
584
585 /* Apply the UDF layout for this filter */
586 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
587
588 /* Apply to all packets received through this port */
589 core_writel(priv, BIT(port), CORE_CFP_DATA_PORT(7));
590
591 /* Source port map match */
592 core_writel(priv, 0xff, CORE_CFP_MASK_PORT(7));
593
594 /* S-Tag status [31:30]
595 * C-Tag status [29:28]
596 * L2 framing [27:26]
597 * L3 framing [25:24]
598 * IP ToS [23:16]
599 * IP proto [15:08]
600 * IP Fragm [7]
601 * Non 1st frag [6]
602 * IP Authen [5]
603 * TTL range [4:3]
604 * PPPoE session [2]
605 * Reserved [1]
606 * UDF_Valid[8] [0]
607 */
608 reg = 1 << L3_FRAMING_SHIFT | ip_proto << IPPROTO_SHIFT |
609 ip_frag << IP_FRAG_SHIFT | udf_upper_bits(num_udf);
610 core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
611
612 /* Mask with the specific layout for IPv6 packets including
613 * UDF_Valid[8]
614 */
615 reg = layout->udfs[slice_num].mask_value | udf_upper_bits(num_udf);
616 core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
617
618 /* UDF_Valid[7:0] [31:24]
619 * S-Tag [23:8]
620 * C-Tag [7:0]
621 */
622 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_DATA_PORT(5));
623
624 /* Mask all but valid UDFs */
625 core_writel(priv, udf_lower_bits(num_udf) << 24, CORE_CFP_MASK_PORT(5));
626
627 /* Slice the IPv6 source address and port */
Florian Fainellidd8eff62017-10-20 14:39:49 -0700628 bcm_sf2_cfp_slice_ipv6(priv, v6_spec->ip6src, v6_spec->psrc,
629 slice_num, false);
630 bcm_sf2_cfp_slice_ipv6(priv, v6_m_spec->ip6src, v6_m_spec->psrc,
Florian Fainelli6fef90c2017-11-30 10:45:26 -0800631 SLICE_NUM_MASK, true);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700632
633 /* Insert into TCAM now because we need to insert a second rule */
634 bcm_sf2_cfp_rule_addr_set(priv, rule_index[0]);
635
636 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
637 if (ret) {
638 pr_err("TCAM entry at addr %d failed\n", rule_index[0]);
639 goto out_err;
640 }
641
642 /* Insert into Action and policer RAMs now */
643 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[0], port_num,
644 queue_num, false);
645 if (ret)
646 goto out_err;
647
648 /* Now deal with the second slice to chain this rule */
649 slice_num = bcm_sf2_get_slice_number(layout, slice_num + 1);
650 if (slice_num == UDF_NUM_SLICES) {
651 ret = -EINVAL;
652 goto out_err;
653 }
654
655 num_udf = bcm_sf2_get_num_udf_slices(layout->udfs[slice_num].slices);
656
657 /* Apply the UDF layout for this filter */
658 bcm_sf2_cfp_udf_set(priv, layout, slice_num);
659
660 /* Chained rule, source port match is coming from the rule we are
661 * chained from.
662 */
663 core_writel(priv, 0, CORE_CFP_DATA_PORT(7));
664 core_writel(priv, 0, CORE_CFP_MASK_PORT(7));
665
666 /*
667 * CHAIN ID [31:24] chain to previous slice
668 * Reserved [23:20]
669 * UDF_Valid[11:8] [19:16]
670 * UDF_Valid[7:0] [15:8]
671 * UDF_n_D11 [7:0]
672 */
673 reg = rule_index[0] << 24 | udf_upper_bits(num_udf) << 16 |
674 udf_lower_bits(num_udf) << 8;
675 core_writel(priv, reg, CORE_CFP_DATA_PORT(6));
676
677 /* Mask all except chain ID, UDF Valid[8] and UDF Valid[7:0] */
678 reg = XCESS_ADDR_MASK << 24 | udf_upper_bits(num_udf) << 16 |
679 udf_lower_bits(num_udf) << 8;
680 core_writel(priv, reg, CORE_CFP_MASK_PORT(6));
681
682 /* Don't care */
683 core_writel(priv, 0, CORE_CFP_DATA_PORT(5));
684
685 /* Mask all */
686 core_writel(priv, 0, CORE_CFP_MASK_PORT(5));
687
Florian Fainellidd8eff62017-10-20 14:39:49 -0700688 bcm_sf2_cfp_slice_ipv6(priv, v6_spec->ip6dst, v6_spec->pdst, slice_num,
689 false);
690 bcm_sf2_cfp_slice_ipv6(priv, v6_m_spec->ip6dst, v6_m_spec->pdst,
691 SLICE_NUM_MASK, true);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700692
693 /* Insert into TCAM now */
694 bcm_sf2_cfp_rule_addr_set(priv, rule_index[1]);
695
696 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
697 if (ret) {
698 pr_err("TCAM entry at addr %d failed\n", rule_index[1]);
699 goto out_err;
700 }
701
702 /* Insert into Action and policer RAMs now, set chain ID to
703 * the one we are chained to
704 */
Florian Fainelli6fef90c2017-11-30 10:45:26 -0800705 ret = bcm_sf2_cfp_act_pol_set(priv, rule_index[1], port_num,
Florian Fainelliba0696c2017-10-20 14:39:47 -0700706 queue_num, true);
707 if (ret)
708 goto out_err;
709
710 /* Turn on CFP for this rule now */
711 reg = core_readl(priv, CORE_CFP_CTL_REG);
712 reg |= BIT(port);
713 core_writel(priv, reg, CORE_CFP_CTL_REG);
714
715 /* Flag the second half rule as being used now, return it as the
716 * location, and flag it as unique while dumping rules
717 */
718 set_bit(rule_index[1], priv->cfp.used);
719 set_bit(rule_index[1], priv->cfp.unique);
720 fs->location = rule_index[1];
721
722 return ret;
723
724out_err:
725 clear_bit(rule_index[0], priv->cfp.used);
726 return ret;
727}
728
Florian Fainelli33061452017-10-20 14:39:44 -0700729static int bcm_sf2_cfp_rule_set(struct dsa_switch *ds, int port,
730 struct ethtool_rx_flow_spec *fs)
731{
732 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
733 unsigned int queue_num, port_num;
Florian Fainelliba0696c2017-10-20 14:39:47 -0700734 int ret = -EINVAL;
Florian Fainelli33061452017-10-20 14:39:44 -0700735
736 /* Check for unsupported extensions */
737 if ((fs->flow_type & FLOW_EXT) && (fs->m_ext.vlan_etype ||
738 fs->m_ext.data[1]))
739 return -EINVAL;
740
741 if (fs->location != RX_CLS_LOC_ANY &&
742 test_bit(fs->location, priv->cfp.used))
743 return -EBUSY;
744
745 if (fs->location != RX_CLS_LOC_ANY &&
746 fs->location > bcm_sf2_cfp_rule_size(priv))
747 return -EINVAL;
748
749 /* We do not support discarding packets, check that the
750 * destination port is enabled and that we are within the
751 * number of ports supported by the switch
752 */
753 port_num = fs->ring_cookie / SF2_NUM_EGRESS_QUEUES;
754
755 if (fs->ring_cookie == RX_CLS_FLOW_DISC ||
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400756 !dsa_is_user_port(ds, port_num) ||
Florian Fainelli33061452017-10-20 14:39:44 -0700757 port_num >= priv->hw_params.num_ports)
758 return -EINVAL;
759 /*
760 * We have a small oddity where Port 6 just does not have a
761 * valid bit here (so we substract by one).
762 */
763 queue_num = fs->ring_cookie % SF2_NUM_EGRESS_QUEUES;
764 if (port_num >= 7)
765 port_num -= 1;
766
Florian Fainelliba0696c2017-10-20 14:39:47 -0700767 switch (fs->flow_type & ~FLOW_EXT) {
768 case TCP_V4_FLOW:
769 case UDP_V4_FLOW:
770 ret = bcm_sf2_cfp_ipv4_rule_set(priv, port, port_num,
771 queue_num, fs);
772 break;
773 case TCP_V6_FLOW:
774 case UDP_V6_FLOW:
775 ret = bcm_sf2_cfp_ipv6_rule_set(priv, port, port_num,
776 queue_num, fs);
777 break;
778 default:
779 break;
780 }
Florian Fainelli33061452017-10-20 14:39:44 -0700781
Florian Fainelliba0696c2017-10-20 14:39:47 -0700782 return ret;
Florian Fainelli33061452017-10-20 14:39:44 -0700783}
784
Florian Fainelliba0696c2017-10-20 14:39:47 -0700785static int bcm_sf2_cfp_rule_del_one(struct bcm_sf2_priv *priv, int port,
786 u32 loc, u32 *next_loc)
Florian Fainelli73181662017-01-30 09:48:43 -0800787{
788 int ret;
789 u32 reg;
790
791 /* Refuse deletion of unused rules, and the default reserved rule */
792 if (!test_bit(loc, priv->cfp.used) || loc == 0)
793 return -EINVAL;
794
795 /* Indicate which rule we want to read */
796 bcm_sf2_cfp_rule_addr_set(priv, loc);
797
798 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
799 if (ret)
800 return ret;
801
Florian Fainelliba0696c2017-10-20 14:39:47 -0700802 /* Check if this is possibly an IPv6 rule that would
803 * indicate we need to delete its companion rule
804 * as well
805 */
806 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
807 if (next_loc)
808 *next_loc = (reg >> 24) & CHAIN_ID_MASK;
809
Florian Fainelli73181662017-01-30 09:48:43 -0800810 /* Clear its valid bits */
811 reg = core_readl(priv, CORE_CFP_DATA_PORT(0));
812 reg &= ~SLICE_VALID;
813 core_writel(priv, reg, CORE_CFP_DATA_PORT(0));
814
815 /* Write back this entry into the TCAM now */
816 ret = bcm_sf2_cfp_op(priv, OP_SEL_WRITE | TCAM_SEL);
817 if (ret)
818 return ret;
819
820 clear_bit(loc, priv->cfp.used);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700821 clear_bit(loc, priv->cfp.unique);
Florian Fainelli73181662017-01-30 09:48:43 -0800822
823 return 0;
824}
825
Florian Fainelliba0696c2017-10-20 14:39:47 -0700826static int bcm_sf2_cfp_rule_del(struct bcm_sf2_priv *priv, int port,
827 u32 loc)
828{
829 u32 next_loc = 0;
830 int ret;
831
832 ret = bcm_sf2_cfp_rule_del_one(priv, port, loc, &next_loc);
833 if (ret)
834 return ret;
835
836 /* If this was an IPv6 rule, delete is companion rule too */
837 if (next_loc)
838 ret = bcm_sf2_cfp_rule_del_one(priv, port, next_loc, NULL);
839
840 return ret;
841}
842
Florian Fainelli73181662017-01-30 09:48:43 -0800843static void bcm_sf2_invert_masks(struct ethtool_rx_flow_spec *flow)
844{
845 unsigned int i;
846
847 for (i = 0; i < sizeof(flow->m_u); i++)
848 flow->m_u.hdata[i] ^= 0xff;
849
850 flow->m_ext.vlan_etype ^= cpu_to_be16(~0);
851 flow->m_ext.vlan_tci ^= cpu_to_be16(~0);
852 flow->m_ext.data[0] ^= cpu_to_be32(~0);
853 flow->m_ext.data[1] ^= cpu_to_be32(~0);
854}
855
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700856static int bcm_sf2_cfp_unslice_ipv4(struct bcm_sf2_priv *priv,
857 struct ethtool_tcpip4_spec *v4_spec,
858 bool mask)
859{
860 u32 reg, offset, ipv4;
861 u16 src_dst_port;
862
863 if (mask)
864 offset = CORE_CFP_MASK_PORT(3);
865 else
866 offset = CORE_CFP_DATA_PORT(3);
867
868 reg = core_readl(priv, offset);
869 /* src port [15:8] */
870 src_dst_port = reg << 8;
871
872 if (mask)
873 offset = CORE_CFP_MASK_PORT(2);
874 else
875 offset = CORE_CFP_DATA_PORT(2);
876
877 reg = core_readl(priv, offset);
878 /* src port [7:0] */
879 src_dst_port |= (reg >> 24);
880
881 v4_spec->pdst = cpu_to_be16(src_dst_port);
882 v4_spec->psrc = cpu_to_be16((u16)(reg >> 8));
883
884 /* IPv4 dst [15:8] */
885 ipv4 = (reg & 0xff) << 8;
886
887 if (mask)
888 offset = CORE_CFP_MASK_PORT(1);
889 else
890 offset = CORE_CFP_DATA_PORT(1);
891
892 reg = core_readl(priv, offset);
893 /* IPv4 dst [31:16] */
894 ipv4 |= ((reg >> 8) & 0xffff) << 16;
895 /* IPv4 dst [7:0] */
896 ipv4 |= (reg >> 24) & 0xff;
897 v4_spec->ip4dst = cpu_to_be32(ipv4);
898
899 /* IPv4 src [15:8] */
900 ipv4 = (reg & 0xff) << 8;
901
902 if (mask)
903 offset = CORE_CFP_MASK_PORT(0);
904 else
905 offset = CORE_CFP_DATA_PORT(0);
906 reg = core_readl(priv, offset);
907
908 /* Once the TCAM is programmed, the mask reflects the slice number
909 * being matched, don't bother checking it when reading back the
910 * mask spec
911 */
912 if (!mask && !(reg & SLICE_VALID))
913 return -EINVAL;
914
915 /* IPv4 src [7:0] */
916 ipv4 |= (reg >> 24) & 0xff;
917 /* IPv4 src [31:16] */
918 ipv4 |= ((reg >> 8) & 0xffff) << 16;
919 v4_spec->ip4src = cpu_to_be32(ipv4);
920
921 return 0;
922}
923
Florian Fainelli33061452017-10-20 14:39:44 -0700924static int bcm_sf2_cfp_ipv4_rule_get(struct bcm_sf2_priv *priv, int port,
Florian Fainelliba0696c2017-10-20 14:39:47 -0700925 struct ethtool_rx_flow_spec *fs)
Florian Fainelli33061452017-10-20 14:39:44 -0700926{
Florian Fainelliba0696c2017-10-20 14:39:47 -0700927 struct ethtool_tcpip4_spec *v4_spec = NULL, *v4_m_spec = NULL;
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700928 u32 reg;
929 int ret;
Florian Fainelli33061452017-10-20 14:39:44 -0700930
Florian Fainelliba0696c2017-10-20 14:39:47 -0700931 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
932
933 switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
934 case IPPROTO_TCP:
935 fs->flow_type = TCP_V4_FLOW;
936 v4_spec = &fs->h_u.tcp_ip4_spec;
937 v4_m_spec = &fs->m_u.tcp_ip4_spec;
938 break;
939 case IPPROTO_UDP:
940 fs->flow_type = UDP_V4_FLOW;
941 v4_spec = &fs->h_u.udp_ip4_spec;
942 v4_m_spec = &fs->m_u.udp_ip4_spec;
943 break;
944 default:
945 return -EINVAL;
946 }
947
948 fs->m_ext.data[0] = cpu_to_be32((reg >> IP_FRAG_SHIFT) & 1);
949 v4_spec->tos = (reg >> IPTOS_SHIFT) & IPTOS_MASK;
950
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700951 ret = bcm_sf2_cfp_unslice_ipv4(priv, v4_spec, false);
952 if (ret)
953 return ret;
Florian Fainelli33061452017-10-20 14:39:44 -0700954
Florian Fainellibc3fc44c2017-10-20 14:39:48 -0700955 return bcm_sf2_cfp_unslice_ipv4(priv, v4_m_spec, true);
Florian Fainelli33061452017-10-20 14:39:44 -0700956}
957
Florian Fainelliba0696c2017-10-20 14:39:47 -0700958static int bcm_sf2_cfp_unslice_ipv6(struct bcm_sf2_priv *priv,
959 __be32 *ip6_addr, __be16 *port,
Florian Fainellidd8eff62017-10-20 14:39:49 -0700960 bool mask)
Florian Fainelliba0696c2017-10-20 14:39:47 -0700961{
Florian Fainellidd8eff62017-10-20 14:39:49 -0700962 u32 reg, tmp, offset;
Florian Fainelliba0696c2017-10-20 14:39:47 -0700963
964 /* C-Tag [31:24]
965 * UDF_n_B8 [23:8] (port)
966 * UDF_n_B7 (upper) [7:0] (addr[15:8])
967 */
Florian Fainellidd8eff62017-10-20 14:39:49 -0700968 if (mask)
969 offset = CORE_CFP_MASK_PORT(4);
970 else
971 offset = CORE_CFP_DATA_PORT(4);
972 reg = core_readl(priv, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700973 *port = cpu_to_be32(reg) >> 8;
Florian Fainelliba0696c2017-10-20 14:39:47 -0700974 tmp = (u32)(reg & 0xff) << 8;
975
976 /* UDF_n_B7 (lower) [31:24] (addr[7:0])
977 * UDF_n_B6 [23:8] (addr[31:16])
978 * UDF_n_B5 (upper) [7:0] (addr[47:40])
979 */
Florian Fainellidd8eff62017-10-20 14:39:49 -0700980 if (mask)
981 offset = CORE_CFP_MASK_PORT(3);
982 else
983 offset = CORE_CFP_DATA_PORT(3);
984 reg = core_readl(priv, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700985 tmp |= (reg >> 24) & 0xff;
986 tmp |= (u32)((reg >> 8) << 16);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700987 ip6_addr[3] = cpu_to_be32(tmp);
988 tmp = (u32)(reg & 0xff) << 8;
989
990 /* UDF_n_B5 (lower) [31:24] (addr[39:32])
991 * UDF_n_B4 [23:8] (addr[63:48])
992 * UDF_n_B3 (upper) [7:0] (addr[79:72])
993 */
Florian Fainellidd8eff62017-10-20 14:39:49 -0700994 if (mask)
995 offset = CORE_CFP_MASK_PORT(2);
996 else
997 offset = CORE_CFP_DATA_PORT(2);
998 reg = core_readl(priv, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -0700999 tmp |= (reg >> 24) & 0xff;
1000 tmp |= (u32)((reg >> 8) << 16);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001001 ip6_addr[2] = cpu_to_be32(tmp);
1002 tmp = (u32)(reg & 0xff) << 8;
1003
1004 /* UDF_n_B3 (lower) [31:24] (addr[71:64])
1005 * UDF_n_B2 [23:8] (addr[95:80])
1006 * UDF_n_B1 (upper) [7:0] (addr[111:104])
1007 */
Florian Fainellidd8eff62017-10-20 14:39:49 -07001008 if (mask)
1009 offset = CORE_CFP_MASK_PORT(1);
1010 else
1011 offset = CORE_CFP_DATA_PORT(1);
1012 reg = core_readl(priv, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001013 tmp |= (reg >> 24) & 0xff;
1014 tmp |= (u32)((reg >> 8) << 16);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001015 ip6_addr[1] = cpu_to_be32(tmp);
1016 tmp = (u32)(reg & 0xff) << 8;
1017
1018 /* UDF_n_B1 (lower) [31:24] (addr[103:96])
1019 * UDF_n_B0 [23:8] (addr[127:112])
1020 * Reserved [7:4]
1021 * Slice ID [3:2]
1022 * Slice valid [1:0]
1023 */
Florian Fainellidd8eff62017-10-20 14:39:49 -07001024 if (mask)
1025 offset = CORE_CFP_MASK_PORT(0);
1026 else
1027 offset = CORE_CFP_DATA_PORT(0);
1028 reg = core_readl(priv, offset);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001029 tmp |= (reg >> 24) & 0xff;
1030 tmp |= (u32)((reg >> 8) << 16);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001031 ip6_addr[0] = cpu_to_be32(tmp);
1032
Florian Fainellidd8eff62017-10-20 14:39:49 -07001033 if (!mask && !(reg & SLICE_VALID))
Florian Fainelliba0696c2017-10-20 14:39:47 -07001034 return -EINVAL;
1035
1036 return 0;
1037}
1038
1039static int bcm_sf2_cfp_ipv6_rule_get(struct bcm_sf2_priv *priv, int port,
1040 struct ethtool_rx_flow_spec *fs,
1041 u32 next_loc)
1042{
1043 struct ethtool_tcpip6_spec *v6_spec = NULL, *v6_m_spec = NULL;
1044 u32 reg;
1045 int ret;
1046
1047 /* UDPv6 and TCPv6 both use ethtool_tcpip6_spec so we are fine
1048 * assuming tcp_ip6_spec here being an union.
1049 */
1050 v6_spec = &fs->h_u.tcp_ip6_spec;
1051 v6_m_spec = &fs->m_u.tcp_ip6_spec;
1052
1053 /* Read the second half first */
1054 ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6dst, &v6_spec->pdst,
Florian Fainellidd8eff62017-10-20 14:39:49 -07001055 false);
1056 if (ret)
1057 return ret;
1058
1059 ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6dst,
1060 &v6_m_spec->pdst, true);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001061 if (ret)
1062 return ret;
1063
1064 /* Read last to avoid next entry clobbering the results during search
1065 * operations. We would not have the port enabled for this rule, so
1066 * don't bother checking it.
1067 */
1068 (void)core_readl(priv, CORE_CFP_DATA_PORT(7));
1069
1070 /* The slice number is valid, so read the rule we are chained from now
1071 * which is our first half.
1072 */
1073 bcm_sf2_cfp_rule_addr_set(priv, next_loc);
1074 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
1075 if (ret)
1076 return ret;
1077
1078 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
1079
1080 switch ((reg & IPPROTO_MASK) >> IPPROTO_SHIFT) {
1081 case IPPROTO_TCP:
1082 fs->flow_type = TCP_V6_FLOW;
1083 break;
1084 case IPPROTO_UDP:
1085 fs->flow_type = UDP_V6_FLOW;
1086 break;
1087 default:
1088 return -EINVAL;
1089 }
1090
Florian Fainellidd8eff62017-10-20 14:39:49 -07001091 ret = bcm_sf2_cfp_unslice_ipv6(priv, v6_spec->ip6src, &v6_spec->psrc,
1092 false);
1093 if (ret)
1094 return ret;
1095
1096 return bcm_sf2_cfp_unslice_ipv6(priv, v6_m_spec->ip6src,
1097 &v6_m_spec->psrc, true);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001098}
1099
Florian Fainelli73181662017-01-30 09:48:43 -08001100static int bcm_sf2_cfp_rule_get(struct bcm_sf2_priv *priv, int port,
Florian Fainelli4daa70c2017-10-20 14:39:46 -07001101 struct ethtool_rxnfc *nfc)
Florian Fainelli73181662017-01-30 09:48:43 -08001102{
Florian Fainelliba0696c2017-10-20 14:39:47 -07001103 u32 reg, ipv4_or_chain_id;
Florian Fainelli73181662017-01-30 09:48:43 -08001104 unsigned int queue_num;
Florian Fainelli73181662017-01-30 09:48:43 -08001105 int ret;
1106
Florian Fainelli4daa70c2017-10-20 14:39:46 -07001107 bcm_sf2_cfp_rule_addr_set(priv, nfc->fs.location);
Florian Fainelli73181662017-01-30 09:48:43 -08001108
Florian Fainelli4daa70c2017-10-20 14:39:46 -07001109 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | ACT_POL_RAM);
1110 if (ret)
1111 return ret;
Florian Fainelli73181662017-01-30 09:48:43 -08001112
Florian Fainelli4daa70c2017-10-20 14:39:46 -07001113 reg = core_readl(priv, CORE_ACT_POL_DATA0);
Florian Fainelli73181662017-01-30 09:48:43 -08001114
Florian Fainelli4daa70c2017-10-20 14:39:46 -07001115 ret = bcm_sf2_cfp_op(priv, OP_SEL_READ | TCAM_SEL);
1116 if (ret)
1117 return ret;
Florian Fainelli73181662017-01-30 09:48:43 -08001118
1119 /* Extract the destination port */
1120 nfc->fs.ring_cookie = fls((reg >> DST_MAP_IB_SHIFT) &
1121 DST_MAP_IB_MASK) - 1;
1122
1123 /* There is no Port 6, so we compensate for that here */
1124 if (nfc->fs.ring_cookie >= 6)
1125 nfc->fs.ring_cookie++;
Florian Fainelli152b6fd2017-09-19 10:46:53 -07001126 nfc->fs.ring_cookie *= SF2_NUM_EGRESS_QUEUES;
Florian Fainelli73181662017-01-30 09:48:43 -08001127
1128 /* Extract the destination queue */
1129 queue_num = (reg >> NEW_TC_SHIFT) & NEW_TC_MASK;
1130 nfc->fs.ring_cookie += queue_num;
1131
Florian Fainelliba0696c2017-10-20 14:39:47 -07001132 /* Extract the L3_FRAMING or CHAIN_ID */
Florian Fainelli73181662017-01-30 09:48:43 -08001133 reg = core_readl(priv, CORE_CFP_DATA_PORT(6));
Florian Fainelli73181662017-01-30 09:48:43 -08001134
Florian Fainelliba0696c2017-10-20 14:39:47 -07001135 /* With IPv6 rules this would contain a non-zero chain ID since
1136 * we reserve entry 0 and it cannot be used. So if we read 0 here
1137 * this means an IPv4 rule.
1138 */
1139 ipv4_or_chain_id = (reg >> L3_FRAMING_SHIFT) & 0xff;
1140 if (ipv4_or_chain_id == 0)
1141 ret = bcm_sf2_cfp_ipv4_rule_get(priv, port, &nfc->fs);
1142 else
1143 ret = bcm_sf2_cfp_ipv6_rule_get(priv, port, &nfc->fs,
1144 ipv4_or_chain_id);
Florian Fainelli33061452017-10-20 14:39:44 -07001145 if (ret)
1146 return ret;
Florian Fainelli73181662017-01-30 09:48:43 -08001147
1148 /* Read last to avoid next entry clobbering the results during search
1149 * operations
1150 */
1151 reg = core_readl(priv, CORE_CFP_DATA_PORT(7));
1152 if (!(reg & 1 << port))
1153 return -EINVAL;
1154
1155 bcm_sf2_invert_masks(&nfc->fs);
1156
1157 /* Put the TCAM size here */
1158 nfc->data = bcm_sf2_cfp_rule_size(priv);
1159
1160 return 0;
1161}
1162
1163/* We implement the search doing a TCAM search operation */
1164static int bcm_sf2_cfp_rule_get_all(struct bcm_sf2_priv *priv,
1165 int port, struct ethtool_rxnfc *nfc,
1166 u32 *rule_locs)
1167{
1168 unsigned int index = 1, rules_cnt = 0;
Florian Fainelli73181662017-01-30 09:48:43 -08001169
Florian Fainelliba0696c2017-10-20 14:39:47 -07001170 for_each_set_bit_from(index, priv->cfp.unique, priv->num_cfp_rules) {
Florian Fainelli4daa70c2017-10-20 14:39:46 -07001171 rule_locs[rules_cnt] = index;
1172 rules_cnt++;
1173 }
Florian Fainelli73181662017-01-30 09:48:43 -08001174
1175 /* Put the TCAM size here */
1176 nfc->data = bcm_sf2_cfp_rule_size(priv);
1177 nfc->rule_cnt = rules_cnt;
1178
1179 return 0;
1180}
1181
1182int bcm_sf2_get_rxnfc(struct dsa_switch *ds, int port,
1183 struct ethtool_rxnfc *nfc, u32 *rule_locs)
1184{
1185 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1186 int ret = 0;
1187
1188 mutex_lock(&priv->cfp.lock);
1189
1190 switch (nfc->cmd) {
1191 case ETHTOOL_GRXCLSRLCNT:
1192 /* Subtract the default, unusable rule */
Florian Fainelliba0696c2017-10-20 14:39:47 -07001193 nfc->rule_cnt = bitmap_weight(priv->cfp.unique,
Florian Fainellidf191632017-08-30 12:39:33 -07001194 priv->num_cfp_rules) - 1;
Florian Fainelli73181662017-01-30 09:48:43 -08001195 /* We support specifying rule locations */
1196 nfc->data |= RX_CLS_LOC_SPECIAL;
1197 break;
1198 case ETHTOOL_GRXCLSRULE:
Florian Fainelli4daa70c2017-10-20 14:39:46 -07001199 ret = bcm_sf2_cfp_rule_get(priv, port, nfc);
Florian Fainelli73181662017-01-30 09:48:43 -08001200 break;
1201 case ETHTOOL_GRXCLSRLALL:
1202 ret = bcm_sf2_cfp_rule_get_all(priv, port, nfc, rule_locs);
1203 break;
1204 default:
1205 ret = -EOPNOTSUPP;
1206 break;
1207 }
1208
1209 mutex_unlock(&priv->cfp.lock);
1210
1211 return ret;
1212}
1213
1214int bcm_sf2_set_rxnfc(struct dsa_switch *ds, int port,
1215 struct ethtool_rxnfc *nfc)
1216{
1217 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
1218 int ret = 0;
1219
1220 mutex_lock(&priv->cfp.lock);
1221
1222 switch (nfc->cmd) {
1223 case ETHTOOL_SRXCLSRLINS:
1224 ret = bcm_sf2_cfp_rule_set(ds, port, &nfc->fs);
1225 break;
1226
1227 case ETHTOOL_SRXCLSRLDEL:
1228 ret = bcm_sf2_cfp_rule_del(priv, port, nfc->fs.location);
1229 break;
1230 default:
1231 ret = -EOPNOTSUPP;
1232 break;
1233 }
1234
1235 mutex_unlock(&priv->cfp.lock);
1236
1237 return ret;
1238}
1239
1240int bcm_sf2_cfp_rst(struct bcm_sf2_priv *priv)
1241{
1242 unsigned int timeout = 1000;
1243 u32 reg;
1244
1245 reg = core_readl(priv, CORE_CFP_ACC);
1246 reg |= TCAM_RESET;
1247 core_writel(priv, reg, CORE_CFP_ACC);
1248
1249 do {
1250 reg = core_readl(priv, CORE_CFP_ACC);
1251 if (!(reg & TCAM_RESET))
1252 break;
1253
1254 cpu_relax();
1255 } while (timeout--);
1256
1257 if (!timeout)
1258 return -ETIMEDOUT;
1259
1260 return 0;
1261}