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 bool mlx5_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_eth_enabled(struct mlx5_core_dev *dev)
110 union devlink_param_value val;
113 err = devlink_param_driverinit_value_get(priv_to_devlink(dev),
114 DEVLINK_PARAM_GENERIC_ID_ENABLE_ETH,
116 return err ? false : val.vbool;
119 bool mlx5_vnet_supported(struct mlx5_core_dev *dev)
121 if (!IS_ENABLED(CONFIG_MLX5_VDPA_NET))
124 if (mlx5_core_is_pf(dev))
127 if (!(MLX5_CAP_GEN_64(dev, general_obj_types) &
128 MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q))
131 if (!(MLX5_CAP_DEV_VDPA_EMULATION(dev, event_mode) &
132 MLX5_VIRTIO_Q_EVENT_MODE_QP_MODE))
135 if (!MLX5_CAP_DEV_VDPA_EMULATION(dev, eth_frame_offload_type))
141 static bool is_vnet_enabled(struct mlx5_core_dev *dev)
143 union devlink_param_value val;
146 err = devlink_param_driverinit_value_get(priv_to_devlink(dev),
147 DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
149 return err ? false : val.vbool;
152 static bool is_ib_rep_supported(struct mlx5_core_dev *dev)
154 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
157 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
160 if (!is_eth_rep_supported(dev))
163 if (!MLX5_ESWITCH_MANAGER(dev))
166 if (!is_mdev_switchdev_mode(dev))
169 if (mlx5_core_mp_enabled(dev))
175 static bool is_mp_supported(struct mlx5_core_dev *dev)
177 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
180 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
183 if (is_ib_rep_supported(dev))
186 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
189 if (!mlx5_core_is_mp_slave(dev))
195 bool mlx5_rdma_supported(struct mlx5_core_dev *dev)
197 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
200 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
203 if (is_ib_rep_supported(dev))
206 if (is_mp_supported(dev))
212 static bool is_ib_enabled(struct mlx5_core_dev *dev)
214 union devlink_param_value val;
217 err = devlink_param_driverinit_value_get(priv_to_devlink(dev),
218 DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
220 return err ? false : val.vbool;
224 MLX5_INTERFACE_PROTOCOL_ETH,
225 MLX5_INTERFACE_PROTOCOL_ETH_REP,
227 MLX5_INTERFACE_PROTOCOL_IB,
228 MLX5_INTERFACE_PROTOCOL_IB_REP,
229 MLX5_INTERFACE_PROTOCOL_MPIB,
231 MLX5_INTERFACE_PROTOCOL_VNET,
234 static const struct mlx5_adev_device {
236 bool (*is_supported)(struct mlx5_core_dev *dev);
237 bool (*is_enabled)(struct mlx5_core_dev *dev);
238 } mlx5_adev_devices[] = {
239 [MLX5_INTERFACE_PROTOCOL_VNET] = { .suffix = "vnet",
240 .is_supported = &mlx5_vnet_supported,
241 .is_enabled = &is_vnet_enabled },
242 [MLX5_INTERFACE_PROTOCOL_IB] = { .suffix = "rdma",
243 .is_supported = &mlx5_rdma_supported,
244 .is_enabled = &is_ib_enabled },
245 [MLX5_INTERFACE_PROTOCOL_ETH] = { .suffix = "eth",
246 .is_supported = &mlx5_eth_supported,
247 .is_enabled = &is_eth_enabled },
248 [MLX5_INTERFACE_PROTOCOL_ETH_REP] = { .suffix = "eth-rep",
249 .is_supported = &is_eth_rep_supported },
250 [MLX5_INTERFACE_PROTOCOL_IB_REP] = { .suffix = "rdma-rep",
251 .is_supported = &is_ib_rep_supported },
252 [MLX5_INTERFACE_PROTOCOL_MPIB] = { .suffix = "multiport",
253 .is_supported = &is_mp_supported },
256 int mlx5_adev_idx_alloc(void)
258 return ida_alloc(&mlx5_adev_ida, GFP_KERNEL);
261 void mlx5_adev_idx_free(int idx)
263 ida_free(&mlx5_adev_ida, idx);
266 int mlx5_adev_init(struct mlx5_core_dev *dev)
268 struct mlx5_priv *priv = &dev->priv;
270 priv->adev = kcalloc(ARRAY_SIZE(mlx5_adev_devices),
271 sizeof(struct mlx5_adev *), GFP_KERNEL);
278 void mlx5_adev_cleanup(struct mlx5_core_dev *dev)
280 struct mlx5_priv *priv = &dev->priv;
285 static void adev_release(struct device *dev)
287 struct mlx5_adev *mlx5_adev =
288 container_of(dev, struct mlx5_adev, adev.dev);
289 struct mlx5_priv *priv = &mlx5_adev->mdev->priv;
290 int idx = mlx5_adev->idx;
293 priv->adev[idx] = NULL;
296 static struct mlx5_adev *add_adev(struct mlx5_core_dev *dev, int idx)
298 const char *suffix = mlx5_adev_devices[idx].suffix;
299 struct auxiliary_device *adev;
300 struct mlx5_adev *madev;
303 madev = kzalloc(sizeof(*madev), GFP_KERNEL);
305 return ERR_PTR(-ENOMEM);
308 adev->id = dev->priv.adev_idx;
310 adev->dev.parent = dev->device;
311 adev->dev.release = adev_release;
315 ret = auxiliary_device_init(adev);
321 ret = auxiliary_device_add(adev);
323 auxiliary_device_uninit(adev);
329 static void del_adev(struct auxiliary_device *adev)
331 auxiliary_device_delete(adev);
332 auxiliary_device_uninit(adev);
335 int mlx5_attach_device(struct mlx5_core_dev *dev)
337 struct mlx5_priv *priv = &dev->priv;
338 struct auxiliary_device *adev;
339 struct auxiliary_driver *adrv;
342 mutex_lock(&mlx5_intf_mutex);
343 priv->flags &= ~MLX5_PRIV_FLAGS_DETACH;
344 for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
345 if (!priv->adev[i]) {
346 bool is_supported = false;
348 if (mlx5_adev_devices[i].is_enabled) {
351 enabled = mlx5_adev_devices[i].is_enabled(dev);
356 if (mlx5_adev_devices[i].is_supported)
357 is_supported = mlx5_adev_devices[i].is_supported(dev);
362 priv->adev[i] = add_adev(dev, i);
363 if (IS_ERR(priv->adev[i])) {
364 ret = PTR_ERR(priv->adev[i]);
365 priv->adev[i] = NULL;
368 adev = &priv->adev[i]->adev;
370 /* Pay attention that this is not PCI driver that
371 * mlx5_core_dev is connected, but auxiliary driver.
373 * Here we can race of module unload with devlink
374 * reload, but we don't need to take extra lock because
375 * we are holding global mlx5_intf_mutex.
377 if (!adev->dev.driver)
379 adrv = to_auxiliary_drv(adev->dev.driver);
382 ret = adrv->resume(adev);
385 mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
386 i, mlx5_adev_devices[i].suffix);
391 mutex_unlock(&mlx5_intf_mutex);
395 void mlx5_detach_device(struct mlx5_core_dev *dev)
397 struct mlx5_priv *priv = &dev->priv;
398 struct auxiliary_device *adev;
399 struct auxiliary_driver *adrv;
400 pm_message_t pm = {};
403 mutex_lock(&mlx5_intf_mutex);
404 for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
408 if (mlx5_adev_devices[i].is_enabled) {
411 enabled = mlx5_adev_devices[i].is_enabled(dev);
416 adev = &priv->adev[i]->adev;
417 /* Auxiliary driver was unbind manually through sysfs */
418 if (!adev->dev.driver)
421 adrv = to_auxiliary_drv(adev->dev.driver);
424 adrv->suspend(adev, pm);
429 del_adev(&priv->adev[i]->adev);
430 priv->adev[i] = NULL;
432 priv->flags |= MLX5_PRIV_FLAGS_DETACH;
433 mutex_unlock(&mlx5_intf_mutex);
436 int mlx5_register_device(struct mlx5_core_dev *dev)
440 mutex_lock(&mlx5_intf_mutex);
441 dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
442 ret = mlx5_rescan_drivers_locked(dev);
443 mutex_unlock(&mlx5_intf_mutex);
445 mlx5_unregister_device(dev);
450 void mlx5_unregister_device(struct mlx5_core_dev *dev)
452 mutex_lock(&mlx5_intf_mutex);
453 dev->priv.flags = MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
454 mlx5_rescan_drivers_locked(dev);
455 mutex_unlock(&mlx5_intf_mutex);
458 static int add_drivers(struct mlx5_core_dev *dev)
460 struct mlx5_priv *priv = &dev->priv;
463 for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
464 bool is_supported = false;
469 if (mlx5_adev_devices[i].is_supported)
470 is_supported = mlx5_adev_devices[i].is_supported(dev);
475 priv->adev[i] = add_adev(dev, i);
476 if (IS_ERR(priv->adev[i])) {
477 mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
478 i, mlx5_adev_devices[i].suffix);
479 /* We continue to rescan drivers and leave to the caller
480 * to make decision if to release everything or continue.
482 ret = PTR_ERR(priv->adev[i]);
483 priv->adev[i] = NULL;
489 static void delete_drivers(struct mlx5_core_dev *dev)
491 struct mlx5_priv *priv = &dev->priv;
495 delete_all = priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
497 for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
498 bool is_supported = false;
503 if (mlx5_adev_devices[i].is_enabled) {
506 enabled = mlx5_adev_devices[i].is_enabled(dev);
511 if (mlx5_adev_devices[i].is_supported && !delete_all)
512 is_supported = mlx5_adev_devices[i].is_supported(dev);
518 del_adev(&priv->adev[i]->adev);
519 priv->adev[i] = NULL;
523 /* This function is used after mlx5_core_dev is reconfigured.
525 int mlx5_rescan_drivers_locked(struct mlx5_core_dev *dev)
527 struct mlx5_priv *priv = &dev->priv;
529 lockdep_assert_held(&mlx5_intf_mutex);
530 if (priv->flags & MLX5_PRIV_FLAGS_DETACH)
534 if (priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
537 return add_drivers(dev);
540 static u32 mlx5_gen_pci_id(const struct mlx5_core_dev *dev)
542 return (u32)((pci_domain_nr(dev->pdev->bus) << 16) |
543 (dev->pdev->bus->number << 8) |
544 PCI_SLOT(dev->pdev->devfn));
547 static int next_phys_dev(struct device *dev, const void *data)
549 struct mlx5_adev *madev = container_of(dev, struct mlx5_adev, adev.dev);
550 struct mlx5_core_dev *mdev = madev->mdev;
551 const struct mlx5_core_dev *curr = data;
553 if (!mlx5_core_is_pf(mdev))
559 if (mlx5_gen_pci_id(mdev) != mlx5_gen_pci_id(curr))
565 /* Must be called with intf_mutex held */
566 struct mlx5_core_dev *mlx5_get_next_phys_dev(struct mlx5_core_dev *dev)
568 struct auxiliary_device *adev;
569 struct mlx5_adev *madev;
571 if (!mlx5_core_is_pf(dev))
574 adev = auxiliary_find_device(NULL, dev, &next_phys_dev);
578 madev = container_of(adev, struct mlx5_adev, adev);
579 put_device(&adev->dev);
583 void mlx5_dev_list_lock(void)
585 mutex_lock(&mlx5_intf_mutex);
587 void mlx5_dev_list_unlock(void)
589 mutex_unlock(&mlx5_intf_mutex);
592 int mlx5_dev_list_trylock(void)
594 return mutex_trylock(&mlx5_intf_mutex);