2 * Copyright (c) 2013-2015, 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/mlx5/driver.h>
34 #include <linux/mlx5/eswitch.h>
35 #include <linux/mlx5/mlx5_ifc_vdpa.h>
36 #include "mlx5_core.h"
38 /* intf dev list mutex */
39 static DEFINE_MUTEX(mlx5_intf_mutex);
40 static DEFINE_IDA(mlx5_adev_ida);
42 static bool is_eth_rep_supported(struct mlx5_core_dev *dev)
44 if (!IS_ENABLED(CONFIG_MLX5_ESWITCH))
47 if (!MLX5_ESWITCH_MANAGER(dev))
50 if (!is_mdev_switchdev_mode(dev))
56 static bool is_eth_supported(struct mlx5_core_dev *dev)
58 if (!IS_ENABLED(CONFIG_MLX5_CORE_EN))
61 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
64 if (!MLX5_CAP_GEN(dev, eth_net_offloads)) {
65 mlx5_core_warn(dev, "Missing eth_net_offloads capability\n");
69 if (!MLX5_CAP_GEN(dev, nic_flow_table)) {
70 mlx5_core_warn(dev, "Missing nic_flow_table capability\n");
74 if (!MLX5_CAP_ETH(dev, csum_cap)) {
75 mlx5_core_warn(dev, "Missing csum_cap capability\n");
79 if (!MLX5_CAP_ETH(dev, max_lso_cap)) {
80 mlx5_core_warn(dev, "Missing max_lso_cap capability\n");
84 if (!MLX5_CAP_ETH(dev, vlan_cap)) {
85 mlx5_core_warn(dev, "Missing vlan_cap capability\n");
89 if (!MLX5_CAP_ETH(dev, rss_ind_tbl_cap)) {
90 mlx5_core_warn(dev, "Missing rss_ind_tbl_cap capability\n");
94 if (MLX5_CAP_FLOWTABLE(dev,
95 flow_table_properties_nic_receive.max_ft_level) < 3) {
96 mlx5_core_warn(dev, "max_ft_level < 3\n");
100 if (!MLX5_CAP_ETH(dev, self_lb_en_modifiable))
101 mlx5_core_warn(dev, "Self loop back prevention is not supported\n");
102 if (!MLX5_CAP_GEN(dev, cq_moderation))
103 mlx5_core_warn(dev, "CQ moderation is not supported\n");
108 static bool is_vnet_supported(struct mlx5_core_dev *dev)
110 if (!IS_ENABLED(CONFIG_MLX5_VDPA_NET))
113 if (mlx5_core_is_pf(dev))
116 if (!(MLX5_CAP_GEN_64(dev, general_obj_types) &
117 MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q))
120 if (!(MLX5_CAP_DEV_VDPA_EMULATION(dev, event_mode) &
121 MLX5_VIRTIO_Q_EVENT_MODE_QP_MODE))
124 if (!MLX5_CAP_DEV_VDPA_EMULATION(dev, eth_frame_offload_type))
130 static bool is_ib_rep_supported(struct mlx5_core_dev *dev)
132 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
135 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
138 if (!is_eth_rep_supported(dev))
141 if (!MLX5_ESWITCH_MANAGER(dev))
144 if (!is_mdev_switchdev_mode(dev))
147 if (mlx5_core_mp_enabled(dev))
153 static bool is_mp_supported(struct mlx5_core_dev *dev)
155 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
158 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
161 if (is_ib_rep_supported(dev))
164 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
167 if (!mlx5_core_is_mp_slave(dev))
173 static bool is_ib_supported(struct mlx5_core_dev *dev)
175 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
178 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
181 if (is_ib_rep_supported(dev))
184 if (is_mp_supported(dev))
191 MLX5_INTERFACE_PROTOCOL_ETH,
192 MLX5_INTERFACE_PROTOCOL_ETH_REP,
194 MLX5_INTERFACE_PROTOCOL_IB,
195 MLX5_INTERFACE_PROTOCOL_IB_REP,
196 MLX5_INTERFACE_PROTOCOL_MPIB,
198 MLX5_INTERFACE_PROTOCOL_VNET,
201 static const struct mlx5_adev_device {
203 bool (*is_supported)(struct mlx5_core_dev *dev);
204 } mlx5_adev_devices[] = {
205 [MLX5_INTERFACE_PROTOCOL_VNET] = { .suffix = "vnet",
206 .is_supported = &is_vnet_supported },
207 [MLX5_INTERFACE_PROTOCOL_IB] = { .suffix = "rdma",
208 .is_supported = &is_ib_supported },
209 [MLX5_INTERFACE_PROTOCOL_ETH] = { .suffix = "eth",
210 .is_supported = &is_eth_supported },
211 [MLX5_INTERFACE_PROTOCOL_ETH_REP] = { .suffix = "eth-rep",
212 .is_supported = &is_eth_rep_supported },
213 [MLX5_INTERFACE_PROTOCOL_IB_REP] = { .suffix = "rdma-rep",
214 .is_supported = &is_ib_rep_supported },
215 [MLX5_INTERFACE_PROTOCOL_MPIB] = { .suffix = "multiport",
216 .is_supported = &is_mp_supported },
219 int mlx5_adev_idx_alloc(void)
221 return ida_alloc(&mlx5_adev_ida, GFP_KERNEL);
224 void mlx5_adev_idx_free(int idx)
226 ida_free(&mlx5_adev_ida, idx);
229 int mlx5_adev_init(struct mlx5_core_dev *dev)
231 struct mlx5_priv *priv = &dev->priv;
233 priv->adev = kcalloc(ARRAY_SIZE(mlx5_adev_devices),
234 sizeof(struct mlx5_adev *), GFP_KERNEL);
241 void mlx5_adev_cleanup(struct mlx5_core_dev *dev)
243 struct mlx5_priv *priv = &dev->priv;
248 static void adev_release(struct device *dev)
250 struct mlx5_adev *mlx5_adev =
251 container_of(dev, struct mlx5_adev, adev.dev);
252 struct mlx5_priv *priv = &mlx5_adev->mdev->priv;
253 int idx = mlx5_adev->idx;
256 priv->adev[idx] = NULL;
259 static struct mlx5_adev *add_adev(struct mlx5_core_dev *dev, int idx)
261 const char *suffix = mlx5_adev_devices[idx].suffix;
262 struct auxiliary_device *adev;
263 struct mlx5_adev *madev;
266 madev = kzalloc(sizeof(*madev), GFP_KERNEL);
268 return ERR_PTR(-ENOMEM);
271 adev->id = dev->priv.adev_idx;
273 adev->dev.parent = dev->device;
274 adev->dev.release = adev_release;
278 ret = auxiliary_device_init(adev);
284 ret = auxiliary_device_add(adev);
286 auxiliary_device_uninit(adev);
292 static void del_adev(struct auxiliary_device *adev)
294 auxiliary_device_delete(adev);
295 auxiliary_device_uninit(adev);
298 int mlx5_attach_device(struct mlx5_core_dev *dev)
300 struct mlx5_priv *priv = &dev->priv;
301 struct auxiliary_device *adev;
302 struct auxiliary_driver *adrv;
305 mutex_lock(&mlx5_intf_mutex);
306 for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
307 if (!priv->adev[i]) {
308 bool is_supported = false;
310 if (mlx5_adev_devices[i].is_supported)
311 is_supported = mlx5_adev_devices[i].is_supported(dev);
316 priv->adev[i] = add_adev(dev, i);
317 if (IS_ERR(priv->adev[i])) {
318 ret = PTR_ERR(priv->adev[i]);
319 priv->adev[i] = NULL;
322 adev = &priv->adev[i]->adev;
323 adrv = to_auxiliary_drv(adev->dev.driver);
326 ret = adrv->resume(adev);
329 mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
330 i, mlx5_adev_devices[i].suffix);
335 mutex_unlock(&mlx5_intf_mutex);
339 void mlx5_detach_device(struct mlx5_core_dev *dev)
341 struct mlx5_priv *priv = &dev->priv;
342 struct auxiliary_device *adev;
343 struct auxiliary_driver *adrv;
344 pm_message_t pm = {};
347 mutex_lock(&mlx5_intf_mutex);
348 for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
352 adev = &priv->adev[i]->adev;
353 adrv = to_auxiliary_drv(adev->dev.driver);
356 adrv->suspend(adev, pm);
360 del_adev(&priv->adev[i]->adev);
361 priv->adev[i] = NULL;
363 mutex_unlock(&mlx5_intf_mutex);
366 int mlx5_register_device(struct mlx5_core_dev *dev)
370 mutex_lock(&mlx5_intf_mutex);
371 dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
372 ret = mlx5_rescan_drivers_locked(dev);
373 mutex_unlock(&mlx5_intf_mutex);
375 mlx5_unregister_device(dev);
380 void mlx5_unregister_device(struct mlx5_core_dev *dev)
382 mutex_lock(&mlx5_intf_mutex);
383 dev->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
384 mlx5_rescan_drivers_locked(dev);
385 mutex_unlock(&mlx5_intf_mutex);
388 static int add_drivers(struct mlx5_core_dev *dev)
390 struct mlx5_priv *priv = &dev->priv;
393 for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
394 bool is_supported = false;
399 if (mlx5_adev_devices[i].is_supported)
400 is_supported = mlx5_adev_devices[i].is_supported(dev);
405 priv->adev[i] = add_adev(dev, i);
406 if (IS_ERR(priv->adev[i])) {
407 mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
408 i, mlx5_adev_devices[i].suffix);
409 /* We continue to rescan drivers and leave to the caller
410 * to make decision if to release everything or continue.
412 ret = PTR_ERR(priv->adev[i]);
413 priv->adev[i] = NULL;
419 static void delete_drivers(struct mlx5_core_dev *dev)
421 struct mlx5_priv *priv = &dev->priv;
425 delete_all = priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
427 for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
428 bool is_supported = false;
433 if (mlx5_adev_devices[i].is_supported && !delete_all)
434 is_supported = mlx5_adev_devices[i].is_supported(dev);
439 del_adev(&priv->adev[i]->adev);
440 priv->adev[i] = NULL;
444 /* This function is used after mlx5_core_dev is reconfigured.
446 int mlx5_rescan_drivers_locked(struct mlx5_core_dev *dev)
448 struct mlx5_priv *priv = &dev->priv;
450 lockdep_assert_held(&mlx5_intf_mutex);
453 if (priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
456 return add_drivers(dev);
459 static u32 mlx5_gen_pci_id(const struct mlx5_core_dev *dev)
461 return (u32)((pci_domain_nr(dev->pdev->bus) << 16) |
462 (dev->pdev->bus->number << 8) |
463 PCI_SLOT(dev->pdev->devfn));
466 static int next_phys_dev(struct device *dev, const void *data)
468 struct mlx5_adev *madev = container_of(dev, struct mlx5_adev, adev.dev);
469 struct mlx5_core_dev *mdev = madev->mdev;
470 const struct mlx5_core_dev *curr = data;
472 if (!mlx5_core_is_pf(mdev))
478 if (mlx5_gen_pci_id(mdev) != mlx5_gen_pci_id(curr))
484 /* This function is called with two flows:
485 * 1. During initialization of mlx5_core_dev and we don't need to lock it.
486 * 2. During LAG configure stage and caller holds &mlx5_intf_mutex.
488 struct mlx5_core_dev *mlx5_get_next_phys_dev(struct mlx5_core_dev *dev)
490 struct auxiliary_device *adev;
491 struct mlx5_adev *madev;
493 if (!mlx5_core_is_pf(dev))
496 adev = auxiliary_find_device(NULL, dev, &next_phys_dev);
500 madev = container_of(adev, struct mlx5_adev, adev);
501 put_device(&adev->dev);
505 void mlx5_dev_list_lock(void)
507 mutex_lock(&mlx5_intf_mutex);
509 void mlx5_dev_list_unlock(void)
511 mutex_unlock(&mlx5_intf_mutex);
514 int mlx5_dev_list_trylock(void)
516 return mutex_trylock(&mlx5_intf_mutex);