2 * Copyright (c) 2017, Mellanox Technologies. 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/hash.h>
36 #define MLX5I_MAX_LOG_PKEY_SUP 7
38 struct qpn_to_netdev {
39 struct net_device *netdev;
40 struct hlist_node hlist;
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 */
49 int mlx5i_pkey_qpn_ht_init(struct net_device *netdev)
51 struct mlx5i_priv *ipriv = netdev_priv(netdev);
52 struct mlx5i_pkey_qpn_ht *qpn_htbl;
54 qpn_htbl = kzalloc(sizeof(*qpn_htbl), GFP_KERNEL);
58 ipriv->qpn_htbl = qpn_htbl;
59 spin_lock_init(&qpn_htbl->ht_lock);
64 void mlx5i_pkey_qpn_ht_cleanup(struct net_device *netdev)
66 struct mlx5i_priv *ipriv = netdev_priv(netdev);
68 kfree(ipriv->qpn_htbl);
71 static struct qpn_to_netdev *mlx5i_find_qpn_to_netdev_node(struct hlist_head *buckets,
74 struct hlist_head *h = &buckets[hash_32(qpn, MLX5I_MAX_LOG_PKEY_SUP)];
75 struct qpn_to_netdev *node;
77 hlist_for_each_entry(node, h, hlist) {
78 if (node->underlay_qpn == qpn)
85 int mlx5i_pkey_add_qpn(struct net_device *netdev, u32 qpn)
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;
92 new_node = kzalloc(sizeof(*new_node), GFP_KERNEL);
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);
105 int mlx5i_pkey_del_qpn(struct net_device *netdev, u32 qpn)
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;
112 node = mlx5i_find_qpn_to_netdev_node(ht->buckets, qpn);
114 mlx5_core_warn(epriv->mdev, "QPN to netdev delete from HT failed\n");
118 spin_lock_bh(&ht->ht_lock);
119 hlist_del_init(&node->hlist);
120 spin_unlock_bh(&ht->ht_lock);
126 struct net_device *mlx5i_pkey_get_netdev(struct net_device *netdev, u32 qpn)
128 struct mlx5i_priv *ipriv = netdev_priv(netdev);
129 struct qpn_to_netdev *node;
131 node = mlx5i_find_qpn_to_netdev_node(ipriv->qpn_htbl->buckets, qpn);
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);
144 static const struct net_device_ops mlx5i_pkey_netdev_ops = {
145 .ndo_open = mlx5i_pkey_open,
146 .ndo_stop = mlx5i_pkey_close,
147 .ndo_init = mlx5i_pkey_dev_init,
148 .ndo_uninit = mlx5i_pkey_dev_cleanup,
149 .ndo_change_mtu = mlx5i_pkey_change_mtu,
153 static int mlx5i_pkey_dev_init(struct net_device *dev)
155 struct mlx5e_priv *priv = mlx5i_epriv(dev);
156 struct mlx5i_priv *ipriv, *parent_ipriv;
157 struct net_device *parent_dev;
162 /* Get QPN to netdevice hash table from parent */
163 parent_ifindex = dev->netdev_ops->ndo_get_iflink(dev);
164 parent_dev = dev_get_by_index(dev_net(dev), parent_ifindex);
166 mlx5_core_warn(priv->mdev, "failed to get parent device\n");
170 parent_ipriv = netdev_priv(parent_dev);
171 ipriv->qpn_htbl = parent_ipriv->qpn_htbl;
174 return mlx5i_dev_init(dev);
177 static void mlx5i_pkey_dev_cleanup(struct net_device *netdev)
179 return mlx5i_dev_cleanup(netdev);
182 static int mlx5i_pkey_open(struct net_device *netdev)
184 struct mlx5e_priv *epriv = mlx5i_epriv(netdev);
185 struct mlx5i_priv *ipriv = epriv->ppriv;
186 struct mlx5_core_dev *mdev = epriv->mdev;
189 mutex_lock(&epriv->state_lock);
191 set_bit(MLX5E_STATE_OPENED, &epriv->state);
193 err = mlx5i_init_underlay_qp(epriv);
195 mlx5_core_warn(mdev, "prepare child underlay qp state failed, %d\n", err);
196 goto err_release_lock;
199 err = mlx5_fs_add_rx_underlay_qpn(mdev, ipriv->qp.qpn);
201 mlx5_core_warn(mdev, "attach child underlay qp to ft failed, %d\n", err);
202 goto err_unint_underlay_qp;
205 err = mlx5e_create_tis(mdev, 0 /* tc */, ipriv->qp.qpn, &epriv->tisn[0]);
207 mlx5_core_warn(mdev, "create child tis failed, %d\n", err);
208 goto err_remove_rx_uderlay_qp;
211 err = mlx5e_open_channels(epriv, &epriv->channels);
213 mlx5_core_warn(mdev, "opening child channels failed, %d\n", err);
214 goto err_clear_state_opened_flag;
216 mlx5e_refresh_tirs(epriv, false);
217 mlx5e_activate_priv_channels(epriv);
218 mutex_unlock(&epriv->state_lock);
222 err_clear_state_opened_flag:
223 mlx5e_destroy_tis(mdev, epriv->tisn[0]);
224 err_remove_rx_uderlay_qp:
225 mlx5_fs_remove_rx_underlay_qpn(mdev, ipriv->qp.qpn);
226 err_unint_underlay_qp:
227 mlx5i_uninit_underlay_qp(epriv);
229 clear_bit(MLX5E_STATE_OPENED, &epriv->state);
230 mutex_unlock(&epriv->state_lock);
234 static int mlx5i_pkey_close(struct net_device *netdev)
236 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
237 struct mlx5i_priv *ipriv = priv->ppriv;
238 struct mlx5_core_dev *mdev = priv->mdev;
240 mutex_lock(&priv->state_lock);
242 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
245 clear_bit(MLX5E_STATE_OPENED, &priv->state);
247 netif_carrier_off(priv->netdev);
248 mlx5_fs_remove_rx_underlay_qpn(mdev, ipriv->qp.qpn);
249 mlx5i_uninit_underlay_qp(priv);
250 mlx5e_deactivate_priv_channels(priv);
251 mlx5e_close_channels(&priv->channels);
252 mlx5e_destroy_tis(mdev, priv->tisn[0]);
254 mutex_unlock(&priv->state_lock);
258 static int mlx5i_pkey_change_mtu(struct net_device *netdev, int new_mtu)
260 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
262 mutex_lock(&priv->state_lock);
263 netdev->mtu = new_mtu;
264 mutex_unlock(&priv->state_lock);
269 /* Called directly after IPoIB netdevice was created to initialize SW structs */
270 static void mlx5i_pkey_init(struct mlx5_core_dev *mdev,
271 struct net_device *netdev,
272 const struct mlx5e_profile *profile,
275 struct mlx5e_priv *priv = mlx5i_epriv(netdev);
277 mlx5i_init(mdev, netdev, profile, ppriv);
279 /* Override parent ndo */
280 netdev->netdev_ops = &mlx5i_pkey_netdev_ops;
282 /* Set child limited ethtool support */
283 netdev->ethtool_ops = &mlx5i_pkey_ethtool_ops;
286 priv->channels.params.log_rq_size = MLX5E_PARAMS_MINIMUM_LOG_RQ_SIZE;
289 /* Called directly before IPoIB netdevice is destroyed to cleanup SW structs */
290 static void mlx5i_pkey_cleanup(struct mlx5e_priv *priv)
295 static int mlx5i_pkey_init_tx(struct mlx5e_priv *priv)
297 struct mlx5i_priv *ipriv = priv->ppriv;
300 err = mlx5i_create_underlay_qp(priv->mdev, &ipriv->qp);
302 mlx5_core_warn(priv->mdev, "create child underlay QP failed, %d\n", err);
309 static void mlx5i_pkey_cleanup_tx(struct mlx5e_priv *priv)
311 struct mlx5i_priv *ipriv = priv->ppriv;
313 mlx5i_destroy_underlay_qp(priv->mdev, &ipriv->qp);
316 static int mlx5i_pkey_init_rx(struct mlx5e_priv *priv)
318 /* Since the rx resources are shared between child and parent, the
319 * parent interface is taking care of rx resource allocation and init
324 static void mlx5i_pkey_cleanup_rx(struct mlx5e_priv *priv)
326 /* Since the rx resources are shared between child and parent, the
327 * parent interface is taking care of rx resource free and de-init
331 static const struct mlx5e_profile mlx5i_pkey_nic_profile = {
332 .init = mlx5i_pkey_init,
333 .cleanup = mlx5i_pkey_cleanup,
334 .init_tx = mlx5i_pkey_init_tx,
335 .cleanup_tx = mlx5i_pkey_cleanup_tx,
336 .init_rx = mlx5i_pkey_init_rx,
337 .cleanup_rx = mlx5i_pkey_cleanup_rx,
340 .update_stats = NULL,
341 .max_nch = mlx5e_get_max_num_channels,
342 .rx_handlers.handle_rx_cqe = mlx5i_handle_rx_cqe,
343 .rx_handlers.handle_rx_cqe_mpwqe = NULL, /* Not supported */
344 .max_tc = MLX5I_MAX_NUM_TC,
347 const struct mlx5e_profile *mlx5i_pkey_get_profile(void)
349 return &mlx5i_pkey_nic_profile;