Merge tag 'scsi-misc' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / vxlan.c
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
39 void 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
47 static int mlx5e_vxlan_core_add_port_cmd(struct mlx5_core_dev *mdev, u16 port)
48 {
49         u32 in[MLX5_ST_SZ_DW(add_vxlan_udp_dport_in)]   = {0};
50         u32 out[MLX5_ST_SZ_DW(add_vxlan_udp_dport_out)] = {0};
51
52         MLX5_SET(add_vxlan_udp_dport_in, in, opcode,
53                  MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT);
54         MLX5_SET(add_vxlan_udp_dport_in, in, vxlan_udp_port, port);
55         return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
56 }
57
58 static int mlx5e_vxlan_core_del_port_cmd(struct mlx5_core_dev *mdev, u16 port)
59 {
60         u32 in[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_in)]   = {0};
61         u32 out[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_out)] = {0};
62
63         MLX5_SET(delete_vxlan_udp_dport_in, in, opcode,
64                  MLX5_CMD_OP_DELETE_VXLAN_UDP_DPORT);
65         MLX5_SET(delete_vxlan_udp_dport_in, in, vxlan_udp_port, port);
66         return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
67 }
68
69 struct mlx5e_vxlan *mlx5e_vxlan_lookup_port(struct mlx5e_priv *priv, u16 port)
70 {
71         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
72         struct mlx5e_vxlan *vxlan;
73
74         spin_lock_bh(&vxlan_db->lock);
75         vxlan = radix_tree_lookup(&vxlan_db->tree, port);
76         spin_unlock_bh(&vxlan_db->lock);
77
78         return vxlan;
79 }
80
81 static void mlx5e_vxlan_add_port(struct work_struct *work)
82 {
83         struct mlx5e_vxlan_work *vxlan_work =
84                 container_of(work, struct mlx5e_vxlan_work, work);
85         struct mlx5e_priv *priv = vxlan_work->priv;
86         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
87         u16 port = vxlan_work->port;
88         struct mlx5e_vxlan *vxlan;
89         int err;
90
91         mutex_lock(&priv->state_lock);
92         vxlan = mlx5e_vxlan_lookup_port(priv, port);
93         if (vxlan) {
94                 atomic_inc(&vxlan->refcount);
95                 goto free_work;
96         }
97
98         if (mlx5e_vxlan_core_add_port_cmd(priv->mdev, port))
99                 goto free_work;
100
101         vxlan = kzalloc(sizeof(*vxlan), GFP_KERNEL);
102         if (!vxlan)
103                 goto err_delete_port;
104
105         vxlan->udp_port = port;
106         atomic_set(&vxlan->refcount, 1);
107
108         spin_lock_bh(&vxlan_db->lock);
109         err = radix_tree_insert(&vxlan_db->tree, vxlan->udp_port, vxlan);
110         spin_unlock_bh(&vxlan_db->lock);
111         if (err)
112                 goto err_free;
113
114         goto free_work;
115
116 err_free:
117         kfree(vxlan);
118 err_delete_port:
119         mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
120 free_work:
121         mutex_unlock(&priv->state_lock);
122         kfree(vxlan_work);
123 }
124
125 static void mlx5e_vxlan_del_port(struct work_struct *work)
126 {
127         struct mlx5e_vxlan_work *vxlan_work =
128                 container_of(work, struct mlx5e_vxlan_work, work);
129         struct mlx5e_priv *priv         = vxlan_work->priv;
130         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
131         u16 port = vxlan_work->port;
132         struct mlx5e_vxlan *vxlan;
133         bool remove = false;
134
135         mutex_lock(&priv->state_lock);
136         spin_lock_bh(&vxlan_db->lock);
137         vxlan = radix_tree_lookup(&vxlan_db->tree, port);
138         if (!vxlan)
139                 goto out_unlock;
140
141         if (atomic_dec_and_test(&vxlan->refcount)) {
142                 radix_tree_delete(&vxlan_db->tree, port);
143                 remove = true;
144         }
145
146 out_unlock:
147         spin_unlock_bh(&vxlan_db->lock);
148
149         if (remove) {
150                 mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
151                 kfree(vxlan);
152         }
153         mutex_unlock(&priv->state_lock);
154         kfree(vxlan_work);
155 }
156
157 void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, sa_family_t sa_family,
158                             u16 port, int add)
159 {
160         struct mlx5e_vxlan_work *vxlan_work;
161
162         vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
163         if (!vxlan_work)
164                 return;
165
166         if (add)
167                 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_port);
168         else
169                 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_port);
170
171         vxlan_work->priv = priv;
172         vxlan_work->port = port;
173         vxlan_work->sa_family = sa_family;
174         queue_work(priv->wq, &vxlan_work->work);
175 }
176
177 void mlx5e_vxlan_cleanup(struct mlx5e_priv *priv)
178 {
179         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
180         struct mlx5e_vxlan *vxlan;
181         unsigned int port = 0;
182
183         /* Lockless since we are the only radix-tree consumers, wq is disabled */
184         while (radix_tree_gang_lookup(&vxlan_db->tree, (void **)&vxlan, port, 1)) {
185                 port = vxlan->udp_port;
186                 radix_tree_delete(&vxlan_db->tree, port);
187                 mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
188                 kfree(vxlan);
189         }
190 }