Merge tag 'mfd-fixes-4.7' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/mfd
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / vxlan.c
CommitLineData
b3f63c3d
MF
1/*
2 * Copyright (c) 2016, Mellanox Technologies, Ltd. All rights reserved.
3 *
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:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
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.
22 *
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
30 * SOFTWARE.
31 */
32
33#include <linux/kernel.h>
34#include <linux/module.h>
35#include <linux/mlx5/driver.h>
36#include "mlx5_core.h"
37#include "vxlan.h"
38
39void mlx5e_vxlan_init(struct mlx5e_priv *priv)
40{
41 struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
42
43 spin_lock_init(&vxlan_db->lock);
44 INIT_RADIX_TREE(&vxlan_db->tree, GFP_ATOMIC);
45}
46
47static int mlx5e_vxlan_core_add_port_cmd(struct mlx5_core_dev *mdev, u16 port)
48{
49 struct mlx5_outbox_hdr *hdr;
50 int err;
51
52 u32 in[MLX5_ST_SZ_DW(add_vxlan_udp_dport_in)];
53 u32 out[MLX5_ST_SZ_DW(add_vxlan_udp_dport_out)];
54
55 memset(in, 0, sizeof(in));
56 memset(out, 0, sizeof(out));
57
58 MLX5_SET(add_vxlan_udp_dport_in, in, opcode,
59 MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT);
60 MLX5_SET(add_vxlan_udp_dport_in, in, vxlan_udp_port, port);
61
62 err = mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
63 if (err)
64 return err;
65
66 hdr = (struct mlx5_outbox_hdr *)out;
67 return hdr->status ? -ENOMEM : 0;
68}
69
70static int mlx5e_vxlan_core_del_port_cmd(struct mlx5_core_dev *mdev, u16 port)
71{
72 u32 in[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_in)];
73 u32 out[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_out)];
74
75 memset(&in, 0, sizeof(in));
76 memset(&out, 0, sizeof(out));
77
78 MLX5_SET(delete_vxlan_udp_dport_in, in, opcode,
79 MLX5_CMD_OP_DELETE_VXLAN_UDP_DPORT);
80 MLX5_SET(delete_vxlan_udp_dport_in, in, vxlan_udp_port, port);
81
82 return mlx5_cmd_exec_check_status(mdev, in, sizeof(in), out,
83 sizeof(out));
84}
85
86struct mlx5e_vxlan *mlx5e_vxlan_lookup_port(struct mlx5e_priv *priv, u16 port)
87{
88 struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
89 struct mlx5e_vxlan *vxlan;
90
91 spin_lock(&vxlan_db->lock);
92 vxlan = radix_tree_lookup(&vxlan_db->tree, port);
93 spin_unlock(&vxlan_db->lock);
94
95 return vxlan;
96}
97
d8cf2dda 98static void mlx5e_vxlan_add_port(struct work_struct *work)
b3f63c3d 99{
d8cf2dda
MF
100 struct mlx5e_vxlan_work *vxlan_work =
101 container_of(work, struct mlx5e_vxlan_work, work);
102 struct mlx5e_priv *priv = vxlan_work->priv;
b3f63c3d 103 struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
d8cf2dda 104 u16 port = vxlan_work->port;
b3f63c3d
MF
105 struct mlx5e_vxlan *vxlan;
106 int err;
107
9ceec359
MF
108 if (mlx5e_vxlan_lookup_port(priv, port))
109 goto free_work;
110
d8cf2dda
MF
111 if (mlx5e_vxlan_core_add_port_cmd(priv->mdev, port))
112 goto free_work;
b3f63c3d
MF
113
114 vxlan = kzalloc(sizeof(*vxlan), GFP_KERNEL);
d8cf2dda 115 if (!vxlan)
b3f63c3d 116 goto err_delete_port;
b3f63c3d
MF
117
118 vxlan->udp_port = port;
119
120 spin_lock_irq(&vxlan_db->lock);
121 err = radix_tree_insert(&vxlan_db->tree, vxlan->udp_port, vxlan);
122 spin_unlock_irq(&vxlan_db->lock);
123 if (err)
124 goto err_free;
125
d8cf2dda 126 goto free_work;
b3f63c3d
MF
127
128err_free:
129 kfree(vxlan);
130err_delete_port:
131 mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
d8cf2dda
MF
132free_work:
133 kfree(vxlan_work);
b3f63c3d
MF
134}
135
136static void __mlx5e_vxlan_core_del_port(struct mlx5e_priv *priv, u16 port)
137{
138 struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
139 struct mlx5e_vxlan *vxlan;
140
141 spin_lock_irq(&vxlan_db->lock);
142 vxlan = radix_tree_delete(&vxlan_db->tree, port);
143 spin_unlock_irq(&vxlan_db->lock);
144
145 if (!vxlan)
146 return;
147
148 mlx5e_vxlan_core_del_port_cmd(priv->mdev, vxlan->udp_port);
149
150 kfree(vxlan);
151}
152
d8cf2dda 153static void mlx5e_vxlan_del_port(struct work_struct *work)
b3f63c3d 154{
d8cf2dda
MF
155 struct mlx5e_vxlan_work *vxlan_work =
156 container_of(work, struct mlx5e_vxlan_work, work);
157 struct mlx5e_priv *priv = vxlan_work->priv;
158 u16 port = vxlan_work->port;
b3f63c3d
MF
159
160 __mlx5e_vxlan_core_del_port(priv, port);
d8cf2dda
MF
161
162 kfree(vxlan_work);
163}
164
165void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, sa_family_t sa_family,
166 u16 port, int add)
167{
168 struct mlx5e_vxlan_work *vxlan_work;
169
170 vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
171 if (!vxlan_work)
172 return;
173
174 if (add)
175 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_port);
176 else
177 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_port);
178
179 vxlan_work->priv = priv;
180 vxlan_work->port = port;
181 vxlan_work->sa_family = sa_family;
182 queue_work(priv->wq, &vxlan_work->work);
b3f63c3d
MF
183}
184
185void mlx5e_vxlan_cleanup(struct mlx5e_priv *priv)
186{
187 struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
188 struct mlx5e_vxlan *vxlan;
189 unsigned int port = 0;
190
191 spin_lock_irq(&vxlan_db->lock);
192 while (radix_tree_gang_lookup(&vxlan_db->tree, (void **)&vxlan, port, 1)) {
193 port = vxlan->udp_port;
194 spin_unlock_irq(&vxlan_db->lock);
195 __mlx5e_vxlan_core_del_port(priv, (u16)port);
196 spin_lock_irq(&vxlan_db->lock);
197 }
198 spin_unlock_irq(&vxlan_db->lock);
199}