1 /*
2 * Copyright (c) 2017, Mellanox Technologies. 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/hash.h>
34 #include "ipoib.h"
35
36 #define MLX5I_MAX_LOG_PKEY_SUP 7
37
38 struct qpn_to_netdev {
39 struct net_device *netdev;
40 struct hlist_node hlist;
41 u32 underlay_qpn;
42 };
43
44 struct mlx5i_pkey_qpn_ht {
45 struct hlist_head buckets[1 << MLX5I_MAX_LOG_PKEY_SUP];
46 spinlock_t ht_lock; /* Synchronise with NAPI */
47 };
48
mlx5i_pkey_qpn_ht_init(struct net_device * netdev)49 int mlx5i_pkey_qpn_ht_init(struct net_device *netdev)
50 {
51 struct mlx5i_priv *ipriv = netdev_priv(netdev);
52 struct mlx5i_pkey_qpn_ht *qpn_htbl;
53
54 qpn_htbl = kzalloc(sizeof(*qpn_htbl), GFP_KERNEL);
55 if (!qpn_htbl)
56 return -ENOMEM;
57
58 ipriv->qpn_htbl = qpn_htbl;
59 spin_lock_init(&qpn_htbl->ht_lock);
60
61 return 0;
62 }
63
mlx5i_pkey_qpn_ht_cleanup(struct net_device * netdev)64 void mlx5i_pkey_qpn_ht_cleanup(struct net_device *netdev)
65 {
66 struct mlx5i_priv *ipriv = netdev_priv(netdev);
67
68 kfree(ipriv->qpn_htbl);
69 }
70
mlx5i_find_qpn_to_netdev_node(struct hlist_head * buckets,u32 qpn)71 static struct qpn_to_netdev *mlx5i_find_qpn_to_netdev_node(struct hlist_head *buckets,
72 u32 qpn)
73 {
74 struct hlist_head *h = &buckets[hash_32(qpn, MLX5I_MAX_LOG_PKEY_SUP)];
75 struct qpn_to_netdev *node;
76
77 hlist_for_each_entry(node, h, hlist) {
78 if (node->underlay_qpn == qpn)
79 return node;
80 }
81
82 return NULL;
83 }
84
mlx5i_pkey_add_qpn(struct net_device * netdev,u32 qpn)85 int mlx5i_pkey_add_qpn(struct net_device *netdev, u32 qpn)
86 {
87 struct mlx5i_priv *ipriv = netdev_priv(netdev);
88 struct mlx5i_pkey_qpn_ht *ht = ipriv->qpn_htbl;
89 u8 key = hash_32(qpn, MLX5I_MAX_LOG_PKEY_SUP);
90 struct qpn_to_netdev *new_node;
91
92 new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
93 if (!new_node)
94 return -ENOMEM;
95
96 new_node->netdev = netdev;
97 new_node->underlay_qpn = qpn;
98 spin_lock_bh(&ht->ht_lock);
99 hlist_add_head(&new_node->hlist, &ht->buckets[key]);
100 spin_unlock_bh(&ht->ht_lock);
101
102 return 0;
103 }
104
mlx5i_pkey_del_qpn(struct net_device * netdev,u32 qpn)105 int mlx5i_pkey_del_qpn(struct net_device *netdev, u32 qpn)
106 {
107 struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
108 struct mlx5i_priv *ipriv = epriv->ppriv;
109 struct mlx5i_pkey_qpn_ht *ht = ipriv->qpn_htbl;
110 struct qpn_to_netdev *node;
111
112 node = mlx5i_find_qpn_to_netdev_node(ht->buckets, qpn);
113 if (!node) {
114 mlx5_core_warn(epriv->mdev, "QPN to netdev delete from HT failed\n");
115 return -EINVAL;
116 }
117
118 spin_lock_bh(&ht->ht_lock);
119 hlist_del_init(&node->hlist);
120 spin_unlock_bh(&ht->ht_lock);
121 kfree(node);
122
123 return 0;
124 }
125
mlx5i_pkey_get_netdev(struct net_device * netdev,u32 qpn)126 struct net_device *mlx5i_pkey_get_netdev(struct net_device *netdev, u32 qpn)
127 {
128 struct mlx5i_priv *ipriv = netdev_priv(netdev);
129 struct qpn_to_netdev *node;
130
131 node = mlx5i_find_qpn_to_netdev_node(ipriv->qpn_htbl->buckets, qpn);
132 if (!node)
133 return NULL;
134
135 return node->netdev;
136 }
137
138 static int mlx5i_pkey_open(struct net_device *netdev);
139 static int mlx5i_pkey_close(struct net_device *netdev);
140 static int mlx5i_pkey_dev_init(struct net_device *dev);
141 static void mlx5i_pkey_dev_cleanup(struct net_device *netdev);
142 static int mlx5i_pkey_change_mtu(struct net_device *netdev, int new_mtu);
143 static int mlx5i_pkey_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
144
145 static const struct net_device_ops mlx5i_pkey_netdev_ops = {
146 .ndo_open = mlx5i_pkey_open,
147 .ndo_stop = mlx5i_pkey_close,
148 .ndo_init = mlx5i_pkey_dev_init,
149 .ndo_get_stats64 = mlx5i_get_stats,
150 .ndo_uninit = mlx5i_pkey_dev_cleanup,
151 .ndo_change_mtu = mlx5i_pkey_change_mtu,
152 .ndo_eth_ioctl = mlx5i_pkey_ioctl,
153 };
154
155 /* Child NDOs */
mlx5i_pkey_dev_init(struct net_device * dev)156 static int mlx5i_pkey_dev_init(struct net_device *dev)
157 {
158 struct mlx5e_priv *priv = mlx5i_epriv(dev);
159 struct mlx5i_priv *ipriv, *parent_ipriv;
160 struct net_device *parent_dev;
161
162 ipriv = priv->ppriv;
163
164 /* Link to parent */
165 parent_dev = mlx5i_parent_get(dev);
166 if (!parent_dev) {
167 mlx5_core_warn(priv->mdev, "failed to get parent device\n");
168 return -EINVAL;
169 }
170
171 if (dev->num_rx_queues < parent_dev->real_num_rx_queues) {
172 mlx5_core_warn(priv->mdev,
173 "failed to create child device with rx queues [%d] less than parent's [%d]\n",
174 dev->num_rx_queues,
175 parent_dev->real_num_rx_queues);
176 mlx5i_parent_put(dev);
177 return -EINVAL;
178 }
179
180 /* Get QPN to netdevice hash table from parent */
181 parent_ipriv = netdev_priv(parent_dev);
182 ipriv->qpn_htbl = parent_ipriv->qpn_htbl;
183
184 return mlx5i_dev_init(dev);
185 }
186
mlx5i_pkey_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)187 static int mlx5i_pkey_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
188 {
189 return mlx5i_ioctl(dev, ifr, cmd);
190 }
191
mlx5i_pkey_dev_cleanup(struct net_device * netdev)192 static void mlx5i_pkey_dev_cleanup(struct net_device *netdev)
193 {
194 mlx5i_parent_put(netdev);
195 return mlx5i_dev_cleanup(netdev);
196 }
197
mlx5i_pkey_open(struct net_device * netdev)198 static int mlx5i_pkey_open(struct net_device *netdev)
199 {
200 struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
201 struct mlx5i_priv *ipriv = epriv->ppriv;
202 struct mlx5_core_dev *mdev = epriv->mdev;
203 int err;
204
205 mutex_lock(&epriv->state_lock);
206
207 set_bit(MLX5E_STATE_OPENED, &epriv->state);
208
209 err = mlx5i_init_underlay_qp(epriv);
210 if (err) {
211 mlx5_core_warn(mdev, "prepare child underlay qp state failed, %d\n", err);
212 goto err_release_lock;
213 }
214
215 err = mlx5_fs_add_rx_underlay_qpn(mdev, ipriv->qpn);
216 if (err) {
217 mlx5_core_warn(mdev, "attach child underlay qp to ft failed, %d\n", err);
218 goto err_unint_underlay_qp;
219 }
220
221 err = mlx5i_create_tis(mdev, ipriv->qpn, &epriv->tisn[0][0]);
222 if (err) {
223 mlx5_core_warn(mdev, "create child tis failed, %d\n", err);
224 goto err_remove_rx_uderlay_qp;
225 }
226
227 err = mlx5e_open_channels(epriv, &epriv->channels);
228 if (err) {
229 mlx5_core_warn(mdev, "opening child channels failed, %d\n", err);
230 goto err_clear_state_opened_flag;
231 }
232 epriv->profile->update_rx(epriv);
233 mlx5e_activate_priv_channels(epriv);
234 mutex_unlock(&epriv->state_lock);
235
236 return 0;
237
238 err_clear_state_opened_flag:
239 mlx5e_destroy_tis(mdev, epriv->tisn[0][0]);
240 err_remove_rx_uderlay_qp:
241 mlx5_fs_remove_rx_underlay_qpn(mdev, ipriv->qpn);
242 err_unint_underlay_qp:
243 mlx5i_uninit_underlay_qp(epriv);
244 err_release_lock:
245 clear_bit(MLX5E_STATE_OPENED, &epriv->state);
246 mutex_unlock(&epriv->state_lock);
247 return err;
248 }
249
mlx5i_pkey_close(struct net_device * netdev)250 static int mlx5i_pkey_close(struct net_device *netdev)
251 {
252 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
253 struct mlx5i_priv *ipriv = priv->ppriv;
254 struct mlx5_core_dev *mdev = priv->mdev;
255
256 mutex_lock(&priv->state_lock);
257
258 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
259 goto unlock;
260
261 clear_bit(MLX5E_STATE_OPENED, &priv->state);
262
263 netif_carrier_off(priv->netdev);
264 mlx5_fs_remove_rx_underlay_qpn(mdev, ipriv->qpn);
265 mlx5i_uninit_underlay_qp(priv);
266 mlx5e_deactivate_priv_channels(priv);
267 mlx5e_close_channels(&priv->channels);
268 mlx5e_destroy_tis(mdev, priv->tisn[0][0]);
269 unlock:
270 mutex_unlock(&priv->state_lock);
271 return 0;
272 }
273
mlx5i_pkey_change_mtu(struct net_device * netdev,int new_mtu)274 static int mlx5i_pkey_change_mtu(struct net_device *netdev, int new_mtu)
275 {
276 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
277
278 mutex_lock(&priv->state_lock);
279 netdev->mtu = new_mtu;
280 mutex_unlock(&priv->state_lock);
281
282 return 0;
283 }
284
285 /* Called directly after IPoIB netdevice was created to initialize SW structs */
mlx5i_pkey_init(struct mlx5_core_dev * mdev,struct net_device * netdev)286 static int mlx5i_pkey_init(struct mlx5_core_dev *mdev,
287 struct net_device *netdev)
288 {
289 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
290 int err;
291
292 err = mlx5i_init(mdev, netdev);
293 if (err)
294 return err;
295
296 /* Override parent ndo */
297 netdev->netdev_ops = &mlx5i_pkey_netdev_ops;
298
299 /* Set child limited ethtool support */
300 netdev->ethtool_ops = &mlx5i_pkey_ethtool_ops;
301
302 /* Use dummy rqs */
303 priv->channels.params.log_rq_mtu_frames = MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE;
304
305 return 0;
306 }
307
308 /* Called directly before IPoIB netdevice is destroyed to cleanup SW structs */
mlx5i_pkey_cleanup(struct mlx5e_priv * priv)309 static void mlx5i_pkey_cleanup(struct mlx5e_priv *priv)
310 {
311 mlx5i_cleanup(priv);
312 }
313
mlx5i_pkey_init_tx(struct mlx5e_priv * priv)314 static int mlx5i_pkey_init_tx(struct mlx5e_priv *priv)
315 {
316 int err;
317
318 err = mlx5i_create_underlay_qp(priv);
319 if (err)
320 mlx5_core_warn(priv->mdev, "create child underlay QP failed, %d\n", err);
321
322 return err;
323 }
324
mlx5i_pkey_cleanup_tx(struct mlx5e_priv * priv)325 static void mlx5i_pkey_cleanup_tx(struct mlx5e_priv *priv)
326 {
327 struct mlx5i_priv *ipriv = priv->ppriv;
328
329 mlx5i_destroy_underlay_qp(priv->mdev, ipriv->qpn);
330 }
331
mlx5i_pkey_init_rx(struct mlx5e_priv * priv)332 static int mlx5i_pkey_init_rx(struct mlx5e_priv *priv)
333 {
334 /* Since the rx resources are shared between child and parent, the
335 * parent interface is taking care of rx resource allocation and init
336 */
337 return 0;
338 }
339
mlx5i_pkey_cleanup_rx(struct mlx5e_priv * priv)340 static void mlx5i_pkey_cleanup_rx(struct mlx5e_priv *priv)
341 {
342 /* Since the rx resources are shared between child and parent, the
343 * parent interface is taking care of rx resource free and de-init
344 */
345 }
346
347 static const struct mlx5e_profile mlx5i_pkey_nic_profile = {
348 .init = mlx5i_pkey_init,
349 .cleanup = mlx5i_pkey_cleanup,
350 .init_tx = mlx5i_pkey_init_tx,
351 .cleanup_tx = mlx5i_pkey_cleanup_tx,
352 .init_rx = mlx5i_pkey_init_rx,
353 .cleanup_rx = mlx5i_pkey_cleanup_rx,
354 .enable = NULL,
355 .disable = NULL,
356 .update_rx = mlx5i_update_nic_rx,
357 .update_stats = NULL,
358 .rx_handlers = &mlx5i_rx_handlers,
359 .max_tc = MLX5I_MAX_NUM_TC,
360 };
361
mlx5i_pkey_get_profile(void)362 const struct mlx5e_profile *mlx5i_pkey_get_profile(void)
363 {
364 return &mlx5i_pkey_nic_profile;
365 }
366