2 * Copyright (c) 2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/mlx5/fs.h>
35 #include "en/params.h"
36 #include "en/xsk/pool.h"
37 #include "en/fs_ethtool.h"
39 struct mlx5e_ethtool_table {
40 struct mlx5_flow_table *ft;
44 #define ETHTOOL_NUM_L3_L4_FTS 7
45 #define ETHTOOL_NUM_L2_FTS 4
47 struct mlx5e_ethtool_steering {
48 struct mlx5e_ethtool_table l3_l4_ft[ETHTOOL_NUM_L3_L4_FTS];
49 struct mlx5e_ethtool_table l2_ft[ETHTOOL_NUM_L2_FTS];
50 struct list_head rules;
54 static int flow_type_to_traffic_type(u32 flow_type);
56 static u32 flow_type_mask(u32 flow_type)
58 return flow_type & ~(FLOW_EXT | FLOW_MAC_EXT | FLOW_RSS);
61 struct mlx5e_ethtool_rule {
62 struct list_head list;
63 struct ethtool_rx_flow_spec flow_spec;
64 struct mlx5_flow_handle *rule;
65 struct mlx5e_ethtool_table *eth_ft;
66 struct mlx5e_rss *rss;
69 static void put_flow_table(struct mlx5e_ethtool_table *eth_ft)
71 if (!--eth_ft->num_rules) {
72 mlx5_destroy_flow_table(eth_ft->ft);
77 #define MLX5E_ETHTOOL_L3_L4_PRIO 0
78 #define MLX5E_ETHTOOL_L2_PRIO (MLX5E_ETHTOOL_L3_L4_PRIO + ETHTOOL_NUM_L3_L4_FTS)
79 #define MLX5E_ETHTOOL_NUM_ENTRIES 64000
80 #define MLX5E_ETHTOOL_NUM_GROUPS 10
81 static struct mlx5e_ethtool_table *get_flow_table(struct mlx5e_priv *priv,
82 struct ethtool_rx_flow_spec *fs,
85 struct mlx5e_ethtool_steering *ethtool = mlx5e_fs_get_ethtool(priv->fs);
86 struct mlx5_flow_table_attr ft_attr = {};
87 struct mlx5e_ethtool_table *eth_ft;
88 struct mlx5_flow_namespace *ns;
89 struct mlx5_flow_table *ft;
94 switch (flow_type_mask(fs->flow_type)) {
101 max_tuples = ETHTOOL_NUM_L3_L4_FTS;
102 prio = MLX5E_ETHTOOL_L3_L4_PRIO + (max_tuples - num_tuples);
103 eth_ft = ðtool->l3_l4_ft[prio];
106 max_tuples = ETHTOOL_NUM_L2_FTS;
107 prio = max_tuples - num_tuples;
108 eth_ft = ðtool->l2_ft[prio];
109 prio += MLX5E_ETHTOOL_L2_PRIO;
112 return ERR_PTR(-EINVAL);
119 ns = mlx5_get_flow_namespace(priv->mdev,
120 MLX5_FLOW_NAMESPACE_ETHTOOL);
122 return ERR_PTR(-EOPNOTSUPP);
124 table_size = min_t(u32, BIT(MLX5_CAP_FLOWTABLE(priv->mdev,
125 flow_table_properties_nic_receive.log_max_ft_size)),
126 MLX5E_ETHTOOL_NUM_ENTRIES);
129 ft_attr.max_fte = table_size;
130 ft_attr.autogroup.max_num_groups = MLX5E_ETHTOOL_NUM_GROUPS;
131 ft = mlx5_create_auto_grouped_flow_table(ns, &ft_attr);
139 static void mask_spec(u8 *mask, u8 *val, size_t size)
143 for (i = 0; i < size; i++, mask++, val++)
144 *((u8 *)val) = *((u8 *)mask) & *((u8 *)val);
147 #define MLX5E_FTE_SET(header_p, fld, v) \
148 MLX5_SET(fte_match_set_lyr_2_4, header_p, fld, v)
150 #define MLX5E_FTE_ADDR_OF(header_p, fld) \
151 MLX5_ADDR_OF(fte_match_set_lyr_2_4, header_p, fld)
154 set_ip4(void *headers_c, void *headers_v, __be32 ip4src_m,
155 __be32 ip4src_v, __be32 ip4dst_m, __be32 ip4dst_v)
158 memcpy(MLX5E_FTE_ADDR_OF(headers_v, src_ipv4_src_ipv6.ipv4_layout.ipv4),
159 &ip4src_v, sizeof(ip4src_v));
160 memcpy(MLX5E_FTE_ADDR_OF(headers_c, src_ipv4_src_ipv6.ipv4_layout.ipv4),
161 &ip4src_m, sizeof(ip4src_m));
164 memcpy(MLX5E_FTE_ADDR_OF(headers_v, dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
165 &ip4dst_v, sizeof(ip4dst_v));
166 memcpy(MLX5E_FTE_ADDR_OF(headers_c, dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
167 &ip4dst_m, sizeof(ip4dst_m));
170 MLX5E_FTE_SET(headers_c, ethertype, 0xffff);
171 MLX5E_FTE_SET(headers_v, ethertype, ETH_P_IP);
175 set_ip6(void *headers_c, void *headers_v, __be32 ip6src_m[4],
176 __be32 ip6src_v[4], __be32 ip6dst_m[4], __be32 ip6dst_v[4])
178 u8 ip6_sz = MLX5_FLD_SZ_BYTES(ipv6_layout, ipv6);
180 if (!ipv6_addr_any((struct in6_addr *)ip6src_m)) {
181 memcpy(MLX5E_FTE_ADDR_OF(headers_v, src_ipv4_src_ipv6.ipv6_layout.ipv6),
183 memcpy(MLX5E_FTE_ADDR_OF(headers_c, src_ipv4_src_ipv6.ipv6_layout.ipv6),
186 if (!ipv6_addr_any((struct in6_addr *)ip6dst_m)) {
187 memcpy(MLX5E_FTE_ADDR_OF(headers_v, dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
189 memcpy(MLX5E_FTE_ADDR_OF(headers_c, dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
193 MLX5E_FTE_SET(headers_c, ethertype, 0xffff);
194 MLX5E_FTE_SET(headers_v, ethertype, ETH_P_IPV6);
198 set_tcp(void *headers_c, void *headers_v, __be16 psrc_m, __be16 psrc_v,
199 __be16 pdst_m, __be16 pdst_v)
202 MLX5E_FTE_SET(headers_c, tcp_sport, ntohs(psrc_m));
203 MLX5E_FTE_SET(headers_v, tcp_sport, ntohs(psrc_v));
206 MLX5E_FTE_SET(headers_c, tcp_dport, ntohs(pdst_m));
207 MLX5E_FTE_SET(headers_v, tcp_dport, ntohs(pdst_v));
210 MLX5E_FTE_SET(headers_c, ip_protocol, 0xffff);
211 MLX5E_FTE_SET(headers_v, ip_protocol, IPPROTO_TCP);
215 set_udp(void *headers_c, void *headers_v, __be16 psrc_m, __be16 psrc_v,
216 __be16 pdst_m, __be16 pdst_v)
219 MLX5E_FTE_SET(headers_c, udp_sport, ntohs(psrc_m));
220 MLX5E_FTE_SET(headers_v, udp_sport, ntohs(psrc_v));
224 MLX5E_FTE_SET(headers_c, udp_dport, ntohs(pdst_m));
225 MLX5E_FTE_SET(headers_v, udp_dport, ntohs(pdst_v));
228 MLX5E_FTE_SET(headers_c, ip_protocol, 0xffff);
229 MLX5E_FTE_SET(headers_v, ip_protocol, IPPROTO_UDP);
233 parse_tcp4(void *headers_c, void *headers_v, struct ethtool_rx_flow_spec *fs)
235 struct ethtool_tcpip4_spec *l4_mask = &fs->m_u.tcp_ip4_spec;
236 struct ethtool_tcpip4_spec *l4_val = &fs->h_u.tcp_ip4_spec;
238 set_ip4(headers_c, headers_v, l4_mask->ip4src, l4_val->ip4src,
239 l4_mask->ip4dst, l4_val->ip4dst);
241 set_tcp(headers_c, headers_v, l4_mask->psrc, l4_val->psrc,
242 l4_mask->pdst, l4_val->pdst);
246 parse_udp4(void *headers_c, void *headers_v, struct ethtool_rx_flow_spec *fs)
248 struct ethtool_tcpip4_spec *l4_mask = &fs->m_u.udp_ip4_spec;
249 struct ethtool_tcpip4_spec *l4_val = &fs->h_u.udp_ip4_spec;
251 set_ip4(headers_c, headers_v, l4_mask->ip4src, l4_val->ip4src,
252 l4_mask->ip4dst, l4_val->ip4dst);
254 set_udp(headers_c, headers_v, l4_mask->psrc, l4_val->psrc,
255 l4_mask->pdst, l4_val->pdst);
259 parse_ip4(void *headers_c, void *headers_v, struct ethtool_rx_flow_spec *fs)
261 struct ethtool_usrip4_spec *l3_mask = &fs->m_u.usr_ip4_spec;
262 struct ethtool_usrip4_spec *l3_val = &fs->h_u.usr_ip4_spec;
264 set_ip4(headers_c, headers_v, l3_mask->ip4src, l3_val->ip4src,
265 l3_mask->ip4dst, l3_val->ip4dst);
267 if (l3_mask->proto) {
268 MLX5E_FTE_SET(headers_c, ip_protocol, l3_mask->proto);
269 MLX5E_FTE_SET(headers_v, ip_protocol, l3_val->proto);
274 parse_ip6(void *headers_c, void *headers_v, struct ethtool_rx_flow_spec *fs)
276 struct ethtool_usrip6_spec *l3_mask = &fs->m_u.usr_ip6_spec;
277 struct ethtool_usrip6_spec *l3_val = &fs->h_u.usr_ip6_spec;
279 set_ip6(headers_c, headers_v, l3_mask->ip6src,
280 l3_val->ip6src, l3_mask->ip6dst, l3_val->ip6dst);
282 if (l3_mask->l4_proto) {
283 MLX5E_FTE_SET(headers_c, ip_protocol, l3_mask->l4_proto);
284 MLX5E_FTE_SET(headers_v, ip_protocol, l3_val->l4_proto);
289 parse_tcp6(void *headers_c, void *headers_v, struct ethtool_rx_flow_spec *fs)
291 struct ethtool_tcpip6_spec *l4_mask = &fs->m_u.tcp_ip6_spec;
292 struct ethtool_tcpip6_spec *l4_val = &fs->h_u.tcp_ip6_spec;
294 set_ip6(headers_c, headers_v, l4_mask->ip6src,
295 l4_val->ip6src, l4_mask->ip6dst, l4_val->ip6dst);
297 set_tcp(headers_c, headers_v, l4_mask->psrc, l4_val->psrc,
298 l4_mask->pdst, l4_val->pdst);
302 parse_udp6(void *headers_c, void *headers_v, struct ethtool_rx_flow_spec *fs)
304 struct ethtool_tcpip6_spec *l4_mask = &fs->m_u.udp_ip6_spec;
305 struct ethtool_tcpip6_spec *l4_val = &fs->h_u.udp_ip6_spec;
307 set_ip6(headers_c, headers_v, l4_mask->ip6src,
308 l4_val->ip6src, l4_mask->ip6dst, l4_val->ip6dst);
310 set_udp(headers_c, headers_v, l4_mask->psrc, l4_val->psrc,
311 l4_mask->pdst, l4_val->pdst);
315 parse_ether(void *headers_c, void *headers_v, struct ethtool_rx_flow_spec *fs)
317 struct ethhdr *eth_mask = &fs->m_u.ether_spec;
318 struct ethhdr *eth_val = &fs->h_u.ether_spec;
320 mask_spec((u8 *)eth_mask, (u8 *)eth_val, sizeof(*eth_mask));
321 ether_addr_copy(MLX5E_FTE_ADDR_OF(headers_c, smac_47_16), eth_mask->h_source);
322 ether_addr_copy(MLX5E_FTE_ADDR_OF(headers_v, smac_47_16), eth_val->h_source);
323 ether_addr_copy(MLX5E_FTE_ADDR_OF(headers_c, dmac_47_16), eth_mask->h_dest);
324 ether_addr_copy(MLX5E_FTE_ADDR_OF(headers_v, dmac_47_16), eth_val->h_dest);
325 MLX5E_FTE_SET(headers_c, ethertype, ntohs(eth_mask->h_proto));
326 MLX5E_FTE_SET(headers_v, ethertype, ntohs(eth_val->h_proto));
330 set_cvlan(void *headers_c, void *headers_v, __be16 vlan_tci)
332 MLX5E_FTE_SET(headers_c, cvlan_tag, 1);
333 MLX5E_FTE_SET(headers_v, cvlan_tag, 1);
334 MLX5E_FTE_SET(headers_c, first_vid, 0xfff);
335 MLX5E_FTE_SET(headers_v, first_vid, ntohs(vlan_tci));
339 set_dmac(void *headers_c, void *headers_v,
340 unsigned char m_dest[ETH_ALEN], unsigned char v_dest[ETH_ALEN])
342 ether_addr_copy(MLX5E_FTE_ADDR_OF(headers_c, dmac_47_16), m_dest);
343 ether_addr_copy(MLX5E_FTE_ADDR_OF(headers_v, dmac_47_16), v_dest);
346 static int set_flow_attrs(u32 *match_c, u32 *match_v,
347 struct ethtool_rx_flow_spec *fs)
349 void *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
351 void *outer_headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
353 u32 flow_type = flow_type_mask(fs->flow_type);
357 parse_tcp4(outer_headers_c, outer_headers_v, fs);
360 parse_udp4(outer_headers_c, outer_headers_v, fs);
363 parse_ip4(outer_headers_c, outer_headers_v, fs);
366 parse_tcp6(outer_headers_c, outer_headers_v, fs);
369 parse_udp6(outer_headers_c, outer_headers_v, fs);
372 parse_ip6(outer_headers_c, outer_headers_v, fs);
375 parse_ether(outer_headers_c, outer_headers_v, fs);
381 if ((fs->flow_type & FLOW_EXT) &&
382 (fs->m_ext.vlan_tci & cpu_to_be16(VLAN_VID_MASK)))
383 set_cvlan(outer_headers_c, outer_headers_v, fs->h_ext.vlan_tci);
385 if (fs->flow_type & FLOW_MAC_EXT &&
386 !is_zero_ether_addr(fs->m_ext.h_dest)) {
387 mask_spec(fs->m_ext.h_dest, fs->h_ext.h_dest, ETH_ALEN);
388 set_dmac(outer_headers_c, outer_headers_v, fs->m_ext.h_dest,
395 static void add_rule_to_list(struct mlx5e_priv *priv,
396 struct mlx5e_ethtool_rule *rule)
398 struct mlx5e_ethtool_steering *ethtool = mlx5e_fs_get_ethtool(priv->fs);
399 struct list_head *head = ðtool->rules;
400 struct mlx5e_ethtool_rule *iter;
402 list_for_each_entry(iter, ðtool->rules, list) {
403 if (iter->flow_spec.location > rule->flow_spec.location)
407 ethtool->tot_num_rules++;
408 list_add(&rule->list, head);
411 static bool outer_header_zero(u32 *match_criteria)
413 int size = MLX5_FLD_SZ_BYTES(fte_match_param, outer_headers);
414 char *outer_headers_c = MLX5_ADDR_OF(fte_match_param, match_criteria,
417 return outer_headers_c[0] == 0 && !memcmp(outer_headers_c,
422 static int flow_get_tirn(struct mlx5e_priv *priv,
423 struct mlx5e_ethtool_rule *eth_rule,
424 struct ethtool_rx_flow_spec *fs,
425 u32 rss_context, u32 *tirn)
427 if (fs->flow_type & FLOW_RSS) {
428 struct mlx5e_packet_merge_param pkt_merge_param;
429 struct mlx5e_rss *rss;
434 rss = mlx5e_rx_res_rss_get(priv->rx_res, rss_context);
438 flow_type = flow_type_mask(fs->flow_type);
439 tt = flow_type_to_traffic_type(flow_type);
443 pkt_merge_param = priv->channels.params.packet_merge;
444 err = mlx5e_rss_obtain_tirn(rss, tt, &pkt_merge_param, false, tirn);
448 mlx5e_rss_refcnt_inc(eth_rule->rss);
450 *tirn = mlx5e_rx_res_get_tirn_direct(priv->rx_res, fs->ring_cookie);
456 static struct mlx5_flow_handle *
457 add_ethtool_flow_rule(struct mlx5e_priv *priv,
458 struct mlx5e_ethtool_rule *eth_rule,
459 struct mlx5_flow_table *ft,
460 struct ethtool_rx_flow_spec *fs, u32 rss_context)
462 struct mlx5_flow_act flow_act = { .flags = FLOW_ACT_NO_APPEND };
463 struct mlx5_flow_destination *dst = NULL;
464 struct mlx5_flow_handle *rule;
465 struct mlx5_flow_spec *spec;
468 spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
470 return ERR_PTR(-ENOMEM);
471 err = set_flow_attrs(spec->match_criteria, spec->match_value,
476 if (fs->ring_cookie == RX_CLS_FLOW_DISC) {
477 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_DROP;
479 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
485 err = flow_get_tirn(priv, eth_rule, fs, rss_context, &dst->tir_num);
489 dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
490 flow_act.action = MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
493 spec->match_criteria_enable = (!outer_header_zero(spec->match_criteria));
494 spec->flow_context.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG;
495 rule = mlx5_add_flow_rules(ft, spec, &flow_act, dst, dst ? 1 : 0);
498 netdev_err(priv->netdev, "%s: failed to add ethtool steering rule: %d\n",
505 return err ? ERR_PTR(err) : rule;
508 static void del_ethtool_rule(struct mlx5e_flow_steering *fs,
509 struct mlx5e_ethtool_rule *eth_rule)
511 struct mlx5e_ethtool_steering *ethtool = mlx5e_fs_get_ethtool(fs);
513 mlx5_del_flow_rules(eth_rule->rule);
515 mlx5e_rss_refcnt_dec(eth_rule->rss);
516 list_del(ð_rule->list);
517 ethtool->tot_num_rules--;
518 put_flow_table(eth_rule->eth_ft);
522 static struct mlx5e_ethtool_rule *find_ethtool_rule(struct mlx5e_priv *priv,
525 struct mlx5e_ethtool_steering *ethtool = mlx5e_fs_get_ethtool(priv->fs);
526 struct mlx5e_ethtool_rule *iter;
528 list_for_each_entry(iter, ðtool->rules, list) {
529 if (iter->flow_spec.location == location)
535 static struct mlx5e_ethtool_rule *get_ethtool_rule(struct mlx5e_priv *priv,
538 struct mlx5e_ethtool_rule *eth_rule;
540 eth_rule = find_ethtool_rule(priv, location);
542 del_ethtool_rule(priv->fs, eth_rule);
544 eth_rule = kzalloc(sizeof(*eth_rule), GFP_KERNEL);
546 return ERR_PTR(-ENOMEM);
548 add_rule_to_list(priv, eth_rule);
552 #define MAX_NUM_OF_ETHTOOL_RULES BIT(10)
554 #define all_ones(field) (field == (__force typeof(field))-1)
555 #define all_zeros_or_all_ones(field) \
556 ((field) == 0 || (field) == (__force typeof(field))-1)
558 static int validate_ethter(struct ethtool_rx_flow_spec *fs)
560 struct ethhdr *eth_mask = &fs->m_u.ether_spec;
563 if (!is_zero_ether_addr(eth_mask->h_dest))
565 if (!is_zero_ether_addr(eth_mask->h_source))
567 if (eth_mask->h_proto)
572 static int validate_tcpudp4(struct ethtool_rx_flow_spec *fs)
574 struct ethtool_tcpip4_spec *l4_mask = &fs->m_u.tcp_ip4_spec;
588 /* Flow is TCP/UDP */
592 static int validate_ip4(struct ethtool_rx_flow_spec *fs)
594 struct ethtool_usrip4_spec *l3_mask = &fs->m_u.usr_ip4_spec;
597 if (l3_mask->l4_4_bytes || l3_mask->tos ||
598 fs->h_u.usr_ip4_spec.ip_ver != ETH_RX_NFC_IP4)
610 static int validate_ip6(struct ethtool_rx_flow_spec *fs)
612 struct ethtool_usrip6_spec *l3_mask = &fs->m_u.usr_ip6_spec;
615 if (l3_mask->l4_4_bytes || l3_mask->tclass)
617 if (!ipv6_addr_any((struct in6_addr *)l3_mask->ip6src))
620 if (!ipv6_addr_any((struct in6_addr *)l3_mask->ip6dst))
622 if (l3_mask->l4_proto)
628 static int validate_tcpudp6(struct ethtool_rx_flow_spec *fs)
630 struct ethtool_tcpip6_spec *l4_mask = &fs->m_u.tcp_ip6_spec;
636 if (!ipv6_addr_any((struct in6_addr *)l4_mask->ip6src))
639 if (!ipv6_addr_any((struct in6_addr *)l4_mask->ip6dst))
646 /* Flow is TCP/UDP */
650 static int validate_vlan(struct ethtool_rx_flow_spec *fs)
652 if (fs->m_ext.vlan_etype ||
653 fs->m_ext.vlan_tci != cpu_to_be16(VLAN_VID_MASK))
656 if (fs->m_ext.vlan_tci &&
657 (be16_to_cpu(fs->h_ext.vlan_tci) >= VLAN_N_VID))
663 static int validate_flow(struct mlx5e_priv *priv,
664 struct ethtool_rx_flow_spec *fs)
669 if (fs->location >= MAX_NUM_OF_ETHTOOL_RULES)
672 if (fs->ring_cookie != RX_CLS_FLOW_DISC)
673 if (fs->ring_cookie >= priv->channels.params.num_channels)
676 switch (flow_type_mask(fs->flow_type)) {
678 num_tuples += validate_ethter(fs);
682 ret = validate_tcpudp4(fs);
688 ret = validate_ip4(fs);
695 ret = validate_tcpudp6(fs);
701 ret = validate_ip6(fs);
709 if ((fs->flow_type & FLOW_EXT)) {
710 ret = validate_vlan(fs);
716 if (fs->flow_type & FLOW_MAC_EXT &&
717 !is_zero_ether_addr(fs->m_ext.h_dest))
724 mlx5e_ethtool_flow_replace(struct mlx5e_priv *priv,
725 struct ethtool_rx_flow_spec *fs, u32 rss_context)
727 struct mlx5e_ethtool_table *eth_ft;
728 struct mlx5e_ethtool_rule *eth_rule;
729 struct mlx5_flow_handle *rule;
733 num_tuples = validate_flow(priv, fs);
734 if (num_tuples <= 0) {
735 netdev_warn(priv->netdev, "%s: flow is not valid %d\n",
736 __func__, num_tuples);
740 eth_ft = get_flow_table(priv, fs, num_tuples);
742 return PTR_ERR(eth_ft);
744 eth_rule = get_ethtool_rule(priv, fs->location);
745 if (IS_ERR(eth_rule)) {
746 put_flow_table(eth_ft);
747 return PTR_ERR(eth_rule);
750 eth_rule->flow_spec = *fs;
751 eth_rule->eth_ft = eth_ft;
753 rule = add_ethtool_flow_rule(priv, eth_rule, eth_ft->ft, fs, rss_context);
756 goto del_ethtool_rule;
759 eth_rule->rule = rule;
764 del_ethtool_rule(priv->fs, eth_rule);
770 mlx5e_ethtool_flow_remove(struct mlx5e_priv *priv, int location)
772 struct mlx5e_ethtool_rule *eth_rule;
775 if (location >= MAX_NUM_OF_ETHTOOL_RULES)
778 eth_rule = find_ethtool_rule(priv, location);
784 del_ethtool_rule(priv->fs, eth_rule);
790 mlx5e_ethtool_get_flow(struct mlx5e_priv *priv,
791 struct ethtool_rxnfc *info, int location)
793 struct mlx5e_ethtool_steering *ethtool = mlx5e_fs_get_ethtool(priv->fs);
794 struct mlx5e_ethtool_rule *eth_rule;
796 if (location < 0 || location >= MAX_NUM_OF_ETHTOOL_RULES)
799 list_for_each_entry(eth_rule, ðtool->rules, list) {
802 if (eth_rule->flow_spec.location != location)
806 info->fs = eth_rule->flow_spec;
809 index = mlx5e_rx_res_rss_index(priv->rx_res, eth_rule->rss);
812 info->rss_context = index;
820 mlx5e_ethtool_get_all_flows(struct mlx5e_priv *priv,
821 struct ethtool_rxnfc *info, u32 *rule_locs)
827 info->data = MAX_NUM_OF_ETHTOOL_RULES;
828 while ((!err || err == -ENOENT) && idx < info->rule_cnt) {
829 err = mlx5e_ethtool_get_flow(priv, NULL, location);
831 rule_locs[idx++] = location;
837 int mlx5e_ethtool_alloc(struct mlx5e_ethtool_steering **ethtool)
839 *ethtool = kvzalloc(sizeof(**ethtool), GFP_KERNEL);
845 void mlx5e_ethtool_free(struct mlx5e_ethtool_steering *ethtool)
850 void mlx5e_ethtool_cleanup_steering(struct mlx5e_flow_steering *fs)
852 struct mlx5e_ethtool_steering *ethtool = mlx5e_fs_get_ethtool(fs);
853 struct mlx5e_ethtool_rule *iter;
854 struct mlx5e_ethtool_rule *temp;
856 list_for_each_entry_safe(iter, temp, ðtool->rules, list)
857 del_ethtool_rule(fs, iter);
860 void mlx5e_ethtool_init_steering(struct mlx5e_flow_steering *fs)
862 struct mlx5e_ethtool_steering *ethtool = mlx5e_fs_get_ethtool(fs);
864 INIT_LIST_HEAD(ðtool->rules);
867 static int flow_type_to_traffic_type(u32 flow_type)
871 return MLX5_TT_IPV4_TCP;
873 return MLX5_TT_IPV6_TCP;
875 return MLX5_TT_IPV4_UDP;
877 return MLX5_TT_IPV6_UDP;
879 return MLX5_TT_IPV4_IPSEC_AH;
881 return MLX5_TT_IPV6_IPSEC_AH;
883 return MLX5_TT_IPV4_IPSEC_ESP;
885 return MLX5_TT_IPV6_IPSEC_ESP;
895 static int mlx5e_set_rss_hash_opt(struct mlx5e_priv *priv,
896 struct ethtool_rxnfc *nfc)
898 u8 rx_hash_field = 0;
904 if (nfc->flow_type & FLOW_RSS)
905 rss_idx = nfc->rss_context;
907 flow_type = flow_type_mask(nfc->flow_type);
908 tt = flow_type_to_traffic_type(flow_type);
912 /* RSS does not support anything other than hashing to queues
913 * on src IP, dest IP, TCP/UDP src port and TCP/UDP dest
916 if (flow_type != TCP_V4_FLOW &&
917 flow_type != TCP_V6_FLOW &&
918 flow_type != UDP_V4_FLOW &&
919 flow_type != UDP_V6_FLOW)
922 if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
923 RXH_L4_B_0_1 | RXH_L4_B_2_3))
926 if (nfc->data & RXH_IP_SRC)
927 rx_hash_field |= MLX5_HASH_FIELD_SEL_SRC_IP;
928 if (nfc->data & RXH_IP_DST)
929 rx_hash_field |= MLX5_HASH_FIELD_SEL_DST_IP;
930 if (nfc->data & RXH_L4_B_0_1)
931 rx_hash_field |= MLX5_HASH_FIELD_SEL_L4_SPORT;
932 if (nfc->data & RXH_L4_B_2_3)
933 rx_hash_field |= MLX5_HASH_FIELD_SEL_L4_DPORT;
935 mutex_lock(&priv->state_lock);
936 err = mlx5e_rx_res_rss_set_hash_fields(priv->rx_res, rss_idx, tt, rx_hash_field);
937 mutex_unlock(&priv->state_lock);
942 static int mlx5e_get_rss_hash_opt(struct mlx5e_priv *priv,
943 struct ethtool_rxnfc *nfc)
950 if (nfc->flow_type & FLOW_RSS)
951 rss_idx = nfc->rss_context;
953 flow_type = flow_type_mask(nfc->flow_type);
954 tt = flow_type_to_traffic_type(flow_type);
958 hash_field = mlx5e_rx_res_rss_get_hash_fields(priv->rx_res, rss_idx, tt);
964 if (hash_field & MLX5_HASH_FIELD_SEL_SRC_IP)
965 nfc->data |= RXH_IP_SRC;
966 if (hash_field & MLX5_HASH_FIELD_SEL_DST_IP)
967 nfc->data |= RXH_IP_DST;
968 if (hash_field & MLX5_HASH_FIELD_SEL_L4_SPORT)
969 nfc->data |= RXH_L4_B_0_1;
970 if (hash_field & MLX5_HASH_FIELD_SEL_L4_DPORT)
971 nfc->data |= RXH_L4_B_2_3;
976 int mlx5e_ethtool_set_rxnfc(struct mlx5e_priv *priv, struct ethtool_rxnfc *cmd)
981 case ETHTOOL_SRXCLSRLINS:
982 err = mlx5e_ethtool_flow_replace(priv, &cmd->fs, cmd->rss_context);
984 case ETHTOOL_SRXCLSRLDEL:
985 err = mlx5e_ethtool_flow_remove(priv, cmd->fs.location);
988 err = mlx5e_set_rss_hash_opt(priv, cmd);
998 int mlx5e_ethtool_get_rxnfc(struct mlx5e_priv *priv,
999 struct ethtool_rxnfc *info, u32 *rule_locs)
1001 struct mlx5e_ethtool_steering *ethtool = mlx5e_fs_get_ethtool(priv->fs);
1004 switch (info->cmd) {
1005 case ETHTOOL_GRXCLSRLCNT:
1006 info->rule_cnt = ethtool->tot_num_rules;
1008 case ETHTOOL_GRXCLSRULE:
1009 err = mlx5e_ethtool_get_flow(priv, info, info->fs.location);
1011 case ETHTOOL_GRXCLSRLALL:
1012 err = mlx5e_ethtool_get_all_flows(priv, info, rule_locs);
1015 err = mlx5e_get_rss_hash_opt(priv, info);