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 priv->flags &= ~MLX5_PRIV_FLAGS_DETACH;
307 for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
308 if (!priv->adev[i]) {
309 bool is_supported = false;
311 if (mlx5_adev_devices[i].is_supported)
312 is_supported = mlx5_adev_devices[i].is_supported(dev);
317 priv->adev[i] = add_adev(dev, i);
318 if (IS_ERR(priv->adev[i])) {
319 ret = PTR_ERR(priv->adev[i]);
320 priv->adev[i] = NULL;
323 adev = &priv->adev[i]->adev;
325 /* Pay attention that this is not PCI driver that
326 * mlx5_core_dev is connected, but auxiliary driver.
328 * Here we can race of module unload with devlink
329 * reload, but we don't need to take extra lock because
330 * we are holding global mlx5_intf_mutex.
332 if (!adev->dev.driver)
334 adrv = to_auxiliary_drv(adev->dev.driver);
337 ret = adrv->resume(adev);
340 mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
341 i, mlx5_adev_devices[i].suffix);
346 mutex_unlock(&mlx5_intf_mutex);
350 void mlx5_detach_device(struct mlx5_core_dev *dev)
352 struct mlx5_priv *priv = &dev->priv;
353 struct auxiliary_device *adev;
354 struct auxiliary_driver *adrv;
355 pm_message_t pm = {};
358 mutex_lock(&mlx5_intf_mutex);
359 for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
363 adev = &priv->adev[i]->adev;
364 /* Auxiliary driver was unbind manually through sysfs */
365 if (!adev->dev.driver)
368 adrv = to_auxiliary_drv(adev->dev.driver);
371 adrv->suspend(adev, pm);
376 del_adev(&priv->adev[i]->adev);
377 priv->adev[i] = NULL;
379 priv->flags |= MLX5_PRIV_FLAGS_DETACH;
380 mutex_unlock(&mlx5_intf_mutex);
383 int mlx5_register_device(struct mlx5_core_dev *dev)
387 mutex_lock(&mlx5_intf_mutex);
388 dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
389 ret = mlx5_rescan_drivers_locked(dev);
390 mutex_unlock(&mlx5_intf_mutex);
392 mlx5_unregister_device(dev);
397 void mlx5_unregister_device(struct mlx5_core_dev *dev)
399 mutex_lock(&mlx5_intf_mutex);
400 dev->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
401 mlx5_rescan_drivers_locked(dev);
402 mutex_unlock(&mlx5_intf_mutex);
405 static int add_drivers(struct mlx5_core_dev *dev)
407 struct mlx5_priv *priv = &dev->priv;
410 for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
411 bool is_supported = false;
416 if (mlx5_adev_devices[i].is_supported)
417 is_supported = mlx5_adev_devices[i].is_supported(dev);
422 priv->adev[i] = add_adev(dev, i);
423 if (IS_ERR(priv->adev[i])) {
424 mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
425 i, mlx5_adev_devices[i].suffix);
426 /* We continue to rescan drivers and leave to the caller
427 * to make decision if to release everything or continue.
429 ret = PTR_ERR(priv->adev[i]);
430 priv->adev[i] = NULL;
436 static void delete_drivers(struct mlx5_core_dev *dev)
438 struct mlx5_priv *priv = &dev->priv;
442 delete_all = priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
444 for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
445 bool is_supported = false;
450 if (mlx5_adev_devices[i].is_supported && !delete_all)
451 is_supported = mlx5_adev_devices[i].is_supported(dev);
456 del_adev(&priv->adev[i]->adev);
457 priv->adev[i] = NULL;
461 /* This function is used after mlx5_core_dev is reconfigured.
463 int mlx5_rescan_drivers_locked(struct mlx5_core_dev *dev)
465 struct mlx5_priv *priv = &dev->priv;
467 lockdep_assert_held(&mlx5_intf_mutex);
468 if (priv->flags & MLX5_PRIV_FLAGS_DETACH)
472 if (priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
475 return add_drivers(dev);
478 static u32 mlx5_gen_pci_id(const struct mlx5_core_dev *dev)
480 return (u32)((pci_domain_nr(dev->pdev->bus) << 16) |
481 (dev->pdev->bus->number << 8) |
482 PCI_SLOT(dev->pdev->devfn));
485 static int next_phys_dev(struct device *dev, const void *data)
487 struct mlx5_adev *madev = container_of(dev, struct mlx5_adev, adev.dev);
488 struct mlx5_core_dev *mdev = madev->mdev;
489 const struct mlx5_core_dev *curr = data;
491 if (!mlx5_core_is_pf(mdev))
497 if (mlx5_gen_pci_id(mdev) != mlx5_gen_pci_id(curr))
503 /* This function is called with two flows:
504 * 1. During initialization of mlx5_core_dev and we don't need to lock it.
505 * 2. During LAG configure stage and caller holds &mlx5_intf_mutex.
507 struct mlx5_core_dev *mlx5_get_next_phys_dev(struct mlx5_core_dev *dev)
509 struct auxiliary_device *adev;
510 struct mlx5_adev *madev;
512 if (!mlx5_core_is_pf(dev))
515 adev = auxiliary_find_device(NULL, dev, &next_phys_dev);
519 madev = container_of(adev, struct mlx5_adev, adev);
520 put_device(&adev->dev);
524 void mlx5_dev_list_lock(void)
526 mutex_lock(&mlx5_intf_mutex);
528 void mlx5_dev_list_unlock(void)
530 mutex_unlock(&mlx5_intf_mutex);
533 int mlx5_dev_list_trylock(void)
535 return mutex_trylock(&mlx5_intf_mutex);