2 * Copyright (c) 2016, Mellanox Technologies, Ltd. 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/kernel.h>
34 #include <linux/module.h>
35 #include <linux/refcount.h>
36 #include <linux/mlx5/driver.h>
37 #include <net/vxlan.h>
38 #include "mlx5_core.h"
42 struct mlx5_core_dev *mdev;
43 /* max_num_ports is usuallly 4, 16 buckets is more than enough */
44 DECLARE_HASHTABLE(htable, 4);
45 struct mutex sync_lock; /* sync add/del port HW operations */
48 struct mlx5_vxlan_port {
49 struct hlist_node hlist;
53 static int mlx5_vxlan_core_add_port_cmd(struct mlx5_core_dev *mdev, u16 port)
55 u32 in[MLX5_ST_SZ_DW(add_vxlan_udp_dport_in)] = {};
57 MLX5_SET(add_vxlan_udp_dport_in, in, opcode,
58 MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT);
59 MLX5_SET(add_vxlan_udp_dport_in, in, vxlan_udp_port, port);
60 return mlx5_cmd_exec_in(mdev, add_vxlan_udp_dport, in);
63 static int mlx5_vxlan_core_del_port_cmd(struct mlx5_core_dev *mdev, u16 port)
65 u32 in[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_in)] = {};
67 MLX5_SET(delete_vxlan_udp_dport_in, in, opcode,
68 MLX5_CMD_OP_DELETE_VXLAN_UDP_DPORT);
69 MLX5_SET(delete_vxlan_udp_dport_in, in, vxlan_udp_port, port);
70 return mlx5_cmd_exec_in(mdev, delete_vxlan_udp_dport, in);
73 bool mlx5_vxlan_lookup_port(struct mlx5_vxlan *vxlan, u16 port)
75 struct mlx5_vxlan_port *vxlanp;
78 if (!mlx5_vxlan_allowed(vxlan))
82 hash_for_each_possible_rcu(vxlan->htable, vxlanp, hlist, port)
83 if (vxlanp->udp_port == port) {
92 static struct mlx5_vxlan_port *vxlan_lookup_port(struct mlx5_vxlan *vxlan, u16 port)
94 struct mlx5_vxlan_port *vxlanp;
96 hash_for_each_possible(vxlan->htable, vxlanp, hlist, port)
97 if (vxlanp->udp_port == port)
102 int mlx5_vxlan_add_port(struct mlx5_vxlan *vxlan, u16 port)
104 struct mlx5_vxlan_port *vxlanp;
107 vxlanp = kzalloc(sizeof(*vxlanp), GFP_KERNEL);
110 vxlanp->udp_port = port;
112 ret = mlx5_vxlan_core_add_port_cmd(vxlan->mdev, port);
118 mutex_lock(&vxlan->sync_lock);
119 hash_add_rcu(vxlan->htable, &vxlanp->hlist, port);
120 mutex_unlock(&vxlan->sync_lock);
125 int mlx5_vxlan_del_port(struct mlx5_vxlan *vxlan, u16 port)
127 struct mlx5_vxlan_port *vxlanp;
130 mutex_lock(&vxlan->sync_lock);
132 vxlanp = vxlan_lookup_port(vxlan, port);
133 if (WARN_ON(!vxlanp)) {
138 hash_del_rcu(&vxlanp->hlist);
140 mlx5_vxlan_core_del_port_cmd(vxlan->mdev, port);
144 mutex_unlock(&vxlan->sync_lock);
148 struct mlx5_vxlan *mlx5_vxlan_create(struct mlx5_core_dev *mdev)
150 struct mlx5_vxlan *vxlan;
152 if (!MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan) || !mlx5_core_is_pf(mdev))
153 return ERR_PTR(-ENOTSUPP);
155 vxlan = kzalloc(sizeof(*vxlan), GFP_KERNEL);
157 return ERR_PTR(-ENOMEM);
160 mutex_init(&vxlan->sync_lock);
161 hash_init(vxlan->htable);
163 /* Hardware adds 4789 (IANA_VXLAN_UDP_PORT) by default */
164 mlx5_vxlan_add_port(vxlan, IANA_VXLAN_UDP_PORT);
169 void mlx5_vxlan_destroy(struct mlx5_vxlan *vxlan)
171 if (!mlx5_vxlan_allowed(vxlan))
174 mlx5_vxlan_del_port(vxlan, IANA_VXLAN_UDP_PORT);
175 WARN_ON(!hash_empty(vxlan->htable));
180 void mlx5_vxlan_reset_to_default(struct mlx5_vxlan *vxlan)
182 struct mlx5_vxlan_port *vxlanp;
183 struct hlist_node *tmp;
186 if (!mlx5_vxlan_allowed(vxlan))
189 hash_for_each_safe(vxlan->htable, bkt, tmp, vxlanp, hlist) {
190 /* Don't delete default UDP port added by the HW.
191 * Remove only user configured ports
193 if (vxlanp->udp_port == IANA_VXLAN_UDP_PORT)
195 mlx5_vxlan_del_port(vxlan, vxlanp->udp_port);