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 <linux/mlx5/vport.h>
37 #include "mlx5_core.h"
41 static DEFINE_IDA(mlx5_adev_ida);
43 static bool is_eth_rep_supported(struct mlx5_core_dev *dev)
45 if (!IS_ENABLED(CONFIG_MLX5_ESWITCH))
48 if (!MLX5_ESWITCH_MANAGER(dev))
51 if (!is_mdev_switchdev_mode(dev))
57 bool mlx5_eth_supported(struct mlx5_core_dev *dev)
59 if (!IS_ENABLED(CONFIG_MLX5_CORE_EN))
62 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
65 if (!MLX5_CAP_GEN(dev, eth_net_offloads)) {
66 mlx5_core_warn(dev, "Missing eth_net_offloads capability\n");
70 if (!MLX5_CAP_GEN(dev, nic_flow_table)) {
71 mlx5_core_warn(dev, "Missing nic_flow_table capability\n");
75 if (!MLX5_CAP_ETH(dev, csum_cap)) {
76 mlx5_core_warn(dev, "Missing csum_cap capability\n");
80 if (!MLX5_CAP_ETH(dev, max_lso_cap)) {
81 mlx5_core_warn(dev, "Missing max_lso_cap capability\n");
85 if (!MLX5_CAP_ETH(dev, vlan_cap)) {
86 mlx5_core_warn(dev, "Missing vlan_cap capability\n");
90 if (!MLX5_CAP_ETH(dev, rss_ind_tbl_cap)) {
91 mlx5_core_warn(dev, "Missing rss_ind_tbl_cap capability\n");
95 if (MLX5_CAP_FLOWTABLE(dev,
96 flow_table_properties_nic_receive.max_ft_level) < 3) {
97 mlx5_core_warn(dev, "max_ft_level < 3\n");
101 if (!MLX5_CAP_ETH(dev, self_lb_en_modifiable))
102 mlx5_core_warn(dev, "Self loop back prevention is not supported\n");
103 if (!MLX5_CAP_GEN(dev, cq_moderation))
104 mlx5_core_warn(dev, "CQ moderation is not supported\n");
109 bool mlx5_vnet_supported(struct mlx5_core_dev *dev)
111 if (!IS_ENABLED(CONFIG_MLX5_VDPA_NET))
114 if (mlx5_core_is_pf(dev))
117 if (!(MLX5_CAP_GEN_64(dev, general_obj_types) &
118 MLX5_GENERAL_OBJ_TYPES_CAP_VIRTIO_NET_Q))
121 if (!(MLX5_CAP_DEV_VDPA_EMULATION(dev, event_mode) &
122 MLX5_VIRTIO_Q_EVENT_MODE_QP_MODE))
125 if (!MLX5_CAP_DEV_VDPA_EMULATION(dev, eth_frame_offload_type))
131 static bool is_vnet_enabled(struct mlx5_core_dev *dev)
133 union devlink_param_value val;
136 err = devl_param_driverinit_value_get(priv_to_devlink(dev),
137 DEVLINK_PARAM_GENERIC_ID_ENABLE_VNET,
139 return err ? false : val.vbool;
142 static bool is_ib_rep_supported(struct mlx5_core_dev *dev)
144 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
147 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
150 if (!is_eth_rep_supported(dev))
153 if (mlx5_core_mp_enabled(dev))
159 static bool is_mp_supported(struct mlx5_core_dev *dev)
161 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
164 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
167 if (is_ib_rep_supported(dev))
170 if (MLX5_CAP_GEN(dev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
173 if (!mlx5_core_is_mp_slave(dev))
179 bool mlx5_rdma_supported(struct mlx5_core_dev *dev)
181 if (!IS_ENABLED(CONFIG_MLX5_INFINIBAND))
184 if (dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_IB_ADEV)
187 if (is_ib_rep_supported(dev))
190 if (is_mp_supported(dev))
196 static bool is_ib_enabled(struct mlx5_core_dev *dev)
198 union devlink_param_value val;
201 err = devl_param_driverinit_value_get(priv_to_devlink(dev),
202 DEVLINK_PARAM_GENERIC_ID_ENABLE_RDMA,
204 return err ? false : val.vbool;
207 static bool is_dpll_supported(struct mlx5_core_dev *dev)
209 if (!IS_ENABLED(CONFIG_MLX5_DPLL))
212 if (!MLX5_CAP_MCAM_REG2(dev, synce_registers)) {
213 mlx5_core_dbg(dev, "Missing SyncE capability\n");
221 MLX5_INTERFACE_PROTOCOL_ETH,
222 MLX5_INTERFACE_PROTOCOL_ETH_REP,
224 MLX5_INTERFACE_PROTOCOL_IB,
225 MLX5_INTERFACE_PROTOCOL_IB_REP,
226 MLX5_INTERFACE_PROTOCOL_MPIB,
228 MLX5_INTERFACE_PROTOCOL_VNET,
230 MLX5_INTERFACE_PROTOCOL_DPLL,
233 static const struct mlx5_adev_device {
235 bool (*is_supported)(struct mlx5_core_dev *dev);
236 bool (*is_enabled)(struct mlx5_core_dev *dev);
237 } mlx5_adev_devices[] = {
238 [MLX5_INTERFACE_PROTOCOL_VNET] = { .suffix = "vnet",
239 .is_supported = &mlx5_vnet_supported,
240 .is_enabled = &is_vnet_enabled },
241 [MLX5_INTERFACE_PROTOCOL_IB] = { .suffix = "rdma",
242 .is_supported = &mlx5_rdma_supported,
243 .is_enabled = &is_ib_enabled },
244 [MLX5_INTERFACE_PROTOCOL_ETH] = { .suffix = "eth",
245 .is_supported = &mlx5_eth_supported,
246 .is_enabled = &mlx5_core_is_eth_enabled },
247 [MLX5_INTERFACE_PROTOCOL_ETH_REP] = { .suffix = "eth-rep",
248 .is_supported = &is_eth_rep_supported },
249 [MLX5_INTERFACE_PROTOCOL_IB_REP] = { .suffix = "rdma-rep",
250 .is_supported = &is_ib_rep_supported },
251 [MLX5_INTERFACE_PROTOCOL_MPIB] = { .suffix = "multiport",
252 .is_supported = &is_mp_supported },
253 [MLX5_INTERFACE_PROTOCOL_DPLL] = { .suffix = "dpll",
254 .is_supported = &is_dpll_supported },
257 int mlx5_adev_idx_alloc(void)
259 return ida_alloc(&mlx5_adev_ida, GFP_KERNEL);
262 void mlx5_adev_idx_free(int idx)
264 ida_free(&mlx5_adev_ida, idx);
267 int mlx5_adev_init(struct mlx5_core_dev *dev)
269 struct mlx5_priv *priv = &dev->priv;
271 priv->adev = kcalloc(ARRAY_SIZE(mlx5_adev_devices),
272 sizeof(struct mlx5_adev *), GFP_KERNEL);
279 void mlx5_adev_cleanup(struct mlx5_core_dev *dev)
281 struct mlx5_priv *priv = &dev->priv;
286 static void adev_release(struct device *dev)
288 struct mlx5_adev *mlx5_adev =
289 container_of(dev, struct mlx5_adev, adev.dev);
290 struct mlx5_priv *priv = &mlx5_adev->mdev->priv;
291 int idx = mlx5_adev->idx;
294 priv->adev[idx] = NULL;
297 static struct mlx5_adev *add_adev(struct mlx5_core_dev *dev, int idx)
299 const char *suffix = mlx5_adev_devices[idx].suffix;
300 struct auxiliary_device *adev;
301 struct mlx5_adev *madev;
304 madev = kzalloc(sizeof(*madev), GFP_KERNEL);
306 return ERR_PTR(-ENOMEM);
309 adev->id = dev->priv.adev_idx;
311 adev->dev.parent = dev->device;
312 adev->dev.release = adev_release;
316 ret = auxiliary_device_init(adev);
322 ret = auxiliary_device_add(adev);
324 auxiliary_device_uninit(adev);
330 static void del_adev(struct auxiliary_device *adev)
332 auxiliary_device_delete(adev);
333 auxiliary_device_uninit(adev);
336 void mlx5_dev_set_lightweight(struct mlx5_core_dev *dev)
338 mlx5_devcom_comp_lock(dev->priv.hca_devcom_comp);
339 dev->priv.flags |= MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
340 mlx5_devcom_comp_unlock(dev->priv.hca_devcom_comp);
343 bool mlx5_dev_is_lightweight(struct mlx5_core_dev *dev)
345 return dev->priv.flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
348 int mlx5_attach_device(struct mlx5_core_dev *dev)
350 struct mlx5_priv *priv = &dev->priv;
351 struct auxiliary_device *adev;
352 const struct auxiliary_driver *adrv;
355 devl_assert_locked(priv_to_devlink(dev));
356 mlx5_devcom_comp_lock(dev->priv.hca_devcom_comp);
357 priv->flags &= ~MLX5_PRIV_FLAGS_DETACH;
358 for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
359 if (!priv->adev[i]) {
360 bool is_supported = false;
362 if (mlx5_adev_devices[i].is_enabled) {
365 enabled = mlx5_adev_devices[i].is_enabled(dev);
370 if (mlx5_adev_devices[i].is_supported)
371 is_supported = mlx5_adev_devices[i].is_supported(dev);
376 priv->adev[i] = add_adev(dev, i);
377 if (IS_ERR(priv->adev[i])) {
378 ret = PTR_ERR(priv->adev[i]);
379 priv->adev[i] = NULL;
382 adev = &priv->adev[i]->adev;
384 /* Pay attention that this is not PCI driver that
385 * mlx5_core_dev is connected, but auxiliary driver.
387 if (!adev->dev.driver)
389 adrv = to_auxiliary_drv(adev->dev.driver);
392 ret = adrv->resume(adev);
395 mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
396 i, mlx5_adev_devices[i].suffix);
401 mlx5_devcom_comp_unlock(dev->priv.hca_devcom_comp);
405 void mlx5_detach_device(struct mlx5_core_dev *dev, bool suspend)
407 struct mlx5_priv *priv = &dev->priv;
408 struct auxiliary_device *adev;
409 const struct auxiliary_driver *adrv;
410 pm_message_t pm = {};
413 devl_assert_locked(priv_to_devlink(dev));
414 mlx5_devcom_comp_lock(dev->priv.hca_devcom_comp);
415 for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
419 if (mlx5_adev_devices[i].is_enabled) {
422 enabled = mlx5_adev_devices[i].is_enabled(dev);
427 adev = &priv->adev[i]->adev;
428 /* Auxiliary driver was unbind manually through sysfs */
429 if (!adev->dev.driver)
432 adrv = to_auxiliary_drv(adev->dev.driver);
434 if (adrv->suspend && suspend) {
435 adrv->suspend(adev, pm);
440 del_adev(&priv->adev[i]->adev);
441 priv->adev[i] = NULL;
443 priv->flags |= MLX5_PRIV_FLAGS_DETACH;
444 mlx5_devcom_comp_unlock(dev->priv.hca_devcom_comp);
447 int mlx5_register_device(struct mlx5_core_dev *dev)
451 devl_assert_locked(priv_to_devlink(dev));
452 mlx5_devcom_comp_lock(dev->priv.hca_devcom_comp);
453 dev->priv.flags &= ~MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
454 ret = mlx5_rescan_drivers_locked(dev);
455 mlx5_devcom_comp_unlock(dev->priv.hca_devcom_comp);
457 mlx5_unregister_device(dev);
462 void mlx5_unregister_device(struct mlx5_core_dev *dev)
464 devl_assert_locked(priv_to_devlink(dev));
465 mlx5_devcom_comp_lock(dev->priv.hca_devcom_comp);
466 dev->priv.flags = MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
467 mlx5_rescan_drivers_locked(dev);
468 mlx5_devcom_comp_unlock(dev->priv.hca_devcom_comp);
471 static int add_drivers(struct mlx5_core_dev *dev)
473 struct mlx5_priv *priv = &dev->priv;
476 for (i = 0; i < ARRAY_SIZE(mlx5_adev_devices); i++) {
477 bool is_supported = false;
482 if (mlx5_adev_devices[i].is_enabled &&
483 !(mlx5_adev_devices[i].is_enabled(dev)))
486 if (mlx5_adev_devices[i].is_supported)
487 is_supported = mlx5_adev_devices[i].is_supported(dev);
492 priv->adev[i] = add_adev(dev, i);
493 if (IS_ERR(priv->adev[i])) {
494 mlx5_core_warn(dev, "Device[%d] (%s) failed to load\n",
495 i, mlx5_adev_devices[i].suffix);
496 /* We continue to rescan drivers and leave to the caller
497 * to make decision if to release everything or continue.
499 ret = PTR_ERR(priv->adev[i]);
500 priv->adev[i] = NULL;
506 static void delete_drivers(struct mlx5_core_dev *dev)
508 struct mlx5_priv *priv = &dev->priv;
512 delete_all = priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV;
514 for (i = ARRAY_SIZE(mlx5_adev_devices) - 1; i >= 0; i--) {
515 bool is_supported = false;
520 if (mlx5_adev_devices[i].is_enabled) {
523 enabled = mlx5_adev_devices[i].is_enabled(dev);
528 if (mlx5_adev_devices[i].is_supported && !delete_all)
529 is_supported = mlx5_adev_devices[i].is_supported(dev);
535 del_adev(&priv->adev[i]->adev);
536 priv->adev[i] = NULL;
540 /* This function is used after mlx5_core_dev is reconfigured.
542 int mlx5_rescan_drivers_locked(struct mlx5_core_dev *dev)
544 struct mlx5_priv *priv = &dev->priv;
546 if (priv->flags & MLX5_PRIV_FLAGS_DETACH)
550 if (priv->flags & MLX5_PRIV_FLAGS_DISABLE_ALL_ADEV)
553 return add_drivers(dev);
556 bool mlx5_same_hw_devs(struct mlx5_core_dev *dev, struct mlx5_core_dev *peer_dev)
558 u64 fsystem_guid, psystem_guid;
560 fsystem_guid = mlx5_query_nic_system_image_guid(dev);
561 psystem_guid = mlx5_query_nic_system_image_guid(peer_dev);
563 return (fsystem_guid && psystem_guid && fsystem_guid == psystem_guid);