4b9190d677fc8ecb2e9aefcc6189589f65436f69
[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 static void mlx5e_vxlan_add_port(struct mlx5e_priv *priv, u16 port);
40
41 void mlx5e_vxlan_init(struct mlx5e_priv *priv)
42 {
43         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
44
45         spin_lock_init(&vxlan_db->lock);
46         hash_init(vxlan_db->htable);
47
48         if (mlx5e_vxlan_allowed(priv->mdev))
49                 /* Hardware adds 4789 by default.
50                  * Lockless since we are the only hash table consumers, wq and TX are disabled.
51                  */
52                 mlx5e_vxlan_add_port(priv, 4789);
53 }
54
55 static inline u8 mlx5e_vxlan_max_udp_ports(struct mlx5_core_dev *mdev)
56 {
57         return MLX5_CAP_ETH(mdev, max_vxlan_udp_ports) ?: 4;
58 }
59
60 static int mlx5e_vxlan_core_add_port_cmd(struct mlx5_core_dev *mdev, u16 port)
61 {
62         u32 in[MLX5_ST_SZ_DW(add_vxlan_udp_dport_in)]   = {0};
63         u32 out[MLX5_ST_SZ_DW(add_vxlan_udp_dport_out)] = {0};
64
65         MLX5_SET(add_vxlan_udp_dport_in, in, opcode,
66                  MLX5_CMD_OP_ADD_VXLAN_UDP_DPORT);
67         MLX5_SET(add_vxlan_udp_dport_in, in, vxlan_udp_port, port);
68         return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
69 }
70
71 static int mlx5e_vxlan_core_del_port_cmd(struct mlx5_core_dev *mdev, u16 port)
72 {
73         u32 in[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_in)]   = {0};
74         u32 out[MLX5_ST_SZ_DW(delete_vxlan_udp_dport_out)] = {0};
75
76         MLX5_SET(delete_vxlan_udp_dport_in, in, opcode,
77                  MLX5_CMD_OP_DELETE_VXLAN_UDP_DPORT);
78         MLX5_SET(delete_vxlan_udp_dport_in, in, vxlan_udp_port, port);
79         return mlx5_cmd_exec(mdev, in, sizeof(in), out, sizeof(out));
80 }
81
82 static struct mlx5e_vxlan *mlx5e_vxlan_lookup_port_locked(struct mlx5e_priv *priv,
83                                                           u16 port)
84 {
85         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
86         struct mlx5e_vxlan    *vxlan;
87
88         hash_for_each_possible(vxlan_db->htable, vxlan, hlist, port) {
89                 if (vxlan->udp_port == port)
90                         return vxlan;
91         }
92
93         return NULL;
94 }
95
96 struct mlx5e_vxlan *mlx5e_vxlan_lookup_port(struct mlx5e_priv *priv, u16 port)
97 {
98         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
99         struct mlx5e_vxlan *vxlan;
100
101         spin_lock_bh(&vxlan_db->lock);
102         vxlan = mlx5e_vxlan_lookup_port_locked(priv, port);
103         spin_unlock_bh(&vxlan_db->lock);
104
105         return vxlan;
106 }
107
108 static void mlx5e_vxlan_add_port(struct mlx5e_priv *priv, u16 port)
109 {
110         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
111         struct mlx5e_vxlan *vxlan;
112
113         vxlan = mlx5e_vxlan_lookup_port(priv, port);
114         if (vxlan) {
115                 atomic_inc(&vxlan->refcount);
116                 return;
117         }
118
119         if (vxlan_db->num_ports >= mlx5e_vxlan_max_udp_ports(priv->mdev)) {
120                 netdev_info(priv->netdev,
121                             "UDP port (%d) not offloaded, max number of UDP ports (%d) are already offloaded\n",
122                             port, mlx5e_vxlan_max_udp_ports(priv->mdev));
123                 return;
124         }
125
126         if (mlx5e_vxlan_core_add_port_cmd(priv->mdev, port))
127                 return;
128
129         vxlan = kzalloc(sizeof(*vxlan), GFP_KERNEL);
130         if (!vxlan)
131                 goto err_delete_port;
132
133         vxlan->udp_port = port;
134         atomic_set(&vxlan->refcount, 1);
135
136         spin_lock_bh(&vxlan_db->lock);
137         hash_add(vxlan_db->htable, &vxlan->hlist, port);
138         spin_unlock_bh(&vxlan_db->lock);
139
140         vxlan_db->num_ports++;
141         return;
142
143 err_delete_port:
144         mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
145 }
146
147 static void mlx5e_vxlan_add_work(struct work_struct *work)
148 {
149         struct mlx5e_vxlan_work *vxlan_work =
150                 container_of(work, struct mlx5e_vxlan_work, work);
151         struct mlx5e_priv *priv = vxlan_work->priv;
152         u16 port = vxlan_work->port;
153
154         mutex_lock(&priv->state_lock);
155         mlx5e_vxlan_add_port(priv, port);
156         mutex_unlock(&priv->state_lock);
157
158         kfree(vxlan_work);
159 }
160
161 static void mlx5e_vxlan_del_work(struct work_struct *work)
162 {
163         struct mlx5e_vxlan_work *vxlan_work =
164                 container_of(work, struct mlx5e_vxlan_work, work);
165         struct mlx5e_priv *priv         = vxlan_work->priv;
166         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
167         u16 port = vxlan_work->port;
168         struct mlx5e_vxlan *vxlan;
169         bool remove = false;
170
171         mutex_lock(&priv->state_lock);
172         spin_lock_bh(&vxlan_db->lock);
173         vxlan = mlx5e_vxlan_lookup_port_locked(priv, port);
174         if (!vxlan)
175                 goto out_unlock;
176
177         if (atomic_dec_and_test(&vxlan->refcount)) {
178                 hash_del(&vxlan->hlist);
179                 remove = true;
180         }
181
182 out_unlock:
183         spin_unlock_bh(&vxlan_db->lock);
184
185         if (remove) {
186                 mlx5e_vxlan_core_del_port_cmd(priv->mdev, port);
187                 kfree(vxlan);
188                 vxlan_db->num_ports--;
189         }
190         mutex_unlock(&priv->state_lock);
191         kfree(vxlan_work);
192 }
193
194 void mlx5e_vxlan_queue_work(struct mlx5e_priv *priv, u16 port, int add)
195 {
196         struct mlx5e_vxlan_work *vxlan_work;
197
198         vxlan_work = kmalloc(sizeof(*vxlan_work), GFP_ATOMIC);
199         if (!vxlan_work)
200                 return;
201
202         if (add)
203                 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_add_work);
204         else
205                 INIT_WORK(&vxlan_work->work, mlx5e_vxlan_del_work);
206
207         vxlan_work->priv = priv;
208         vxlan_work->port = port;
209         queue_work(priv->wq, &vxlan_work->work);
210 }
211
212 void mlx5e_vxlan_cleanup(struct mlx5e_priv *priv)
213 {
214         struct mlx5e_vxlan_db *vxlan_db = &priv->vxlan;
215         struct mlx5e_vxlan *vxlan;
216         struct hlist_node *tmp;
217         int bkt;
218
219         /* Lockless since we are the only hash table consumers, wq and TX are disabled */
220         hash_for_each_safe(vxlan_db->htable, bkt, tmp, vxlan, hlist) {
221                 hash_del(&vxlan->hlist);
222                 mlx5e_vxlan_core_del_port_cmd(priv->mdev, vxlan->udp_port);
223                 kfree(vxlan);
224         }
225 }