|
| 1 | +// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB |
| 2 | +// Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. |
| 3 | + |
| 4 | +#include "en.h" |
| 5 | +#include "pcie_cong_event.h" |
| 6 | + |
| 7 | +struct mlx5e_pcie_cong_thresh { |
| 8 | + u16 inbound_high; |
| 9 | + u16 inbound_low; |
| 10 | + u16 outbound_high; |
| 11 | + u16 outbound_low; |
| 12 | +}; |
| 13 | + |
| 14 | +struct mlx5e_pcie_cong_event { |
| 15 | + u64 obj_id; |
| 16 | + |
| 17 | + struct mlx5e_priv *priv; |
| 18 | +}; |
| 19 | + |
| 20 | +/* In units of 0.01 % */ |
| 21 | +static const struct mlx5e_pcie_cong_thresh default_thresh_config = { |
| 22 | + .inbound_high = 9000, |
| 23 | + .inbound_low = 7500, |
| 24 | + .outbound_high = 9000, |
| 25 | + .outbound_low = 7500, |
| 26 | +}; |
| 27 | + |
| 28 | +static int |
| 29 | +mlx5_cmd_pcie_cong_event_set(struct mlx5_core_dev *dev, |
| 30 | + const struct mlx5e_pcie_cong_thresh *config, |
| 31 | + u64 *obj_id) |
| 32 | +{ |
| 33 | + u32 in[MLX5_ST_SZ_DW(pcie_cong_event_cmd_in)] = {}; |
| 34 | + u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)]; |
| 35 | + void *cong_obj; |
| 36 | + void *hdr; |
| 37 | + int err; |
| 38 | + |
| 39 | + hdr = MLX5_ADDR_OF(pcie_cong_event_cmd_in, in, hdr); |
| 40 | + cong_obj = MLX5_ADDR_OF(pcie_cong_event_cmd_in, in, cong_obj); |
| 41 | + |
| 42 | + MLX5_SET(general_obj_in_cmd_hdr, hdr, opcode, |
| 43 | + MLX5_CMD_OP_CREATE_GENERAL_OBJECT); |
| 44 | + |
| 45 | + MLX5_SET(general_obj_in_cmd_hdr, hdr, obj_type, |
| 46 | + MLX5_GENERAL_OBJECT_TYPES_PCIE_CONG_EVENT); |
| 47 | + |
| 48 | + MLX5_SET(pcie_cong_event_obj, cong_obj, inbound_event_en, 1); |
| 49 | + MLX5_SET(pcie_cong_event_obj, cong_obj, outbound_event_en, 1); |
| 50 | + |
| 51 | + MLX5_SET(pcie_cong_event_obj, cong_obj, |
| 52 | + inbound_cong_high_threshold, config->inbound_high); |
| 53 | + MLX5_SET(pcie_cong_event_obj, cong_obj, |
| 54 | + inbound_cong_low_threshold, config->inbound_low); |
| 55 | + |
| 56 | + MLX5_SET(pcie_cong_event_obj, cong_obj, |
| 57 | + outbound_cong_high_threshold, config->outbound_high); |
| 58 | + MLX5_SET(pcie_cong_event_obj, cong_obj, |
| 59 | + outbound_cong_low_threshold, config->outbound_low); |
| 60 | + |
| 61 | + err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); |
| 62 | + if (err) |
| 63 | + return err; |
| 64 | + |
| 65 | + *obj_id = MLX5_GET(general_obj_out_cmd_hdr, out, obj_id); |
| 66 | + |
| 67 | + mlx5_core_dbg(dev, "PCIe congestion event (obj_id=%llu) created. Config: in: [%u, %u], out: [%u, %u]\n", |
| 68 | + *obj_id, |
| 69 | + config->inbound_high, config->inbound_low, |
| 70 | + config->outbound_high, config->outbound_low); |
| 71 | + |
| 72 | + return 0; |
| 73 | +} |
| 74 | + |
| 75 | +static int mlx5_cmd_pcie_cong_event_destroy(struct mlx5_core_dev *dev, |
| 76 | + u64 obj_id) |
| 77 | +{ |
| 78 | + u32 in[MLX5_ST_SZ_DW(pcie_cong_event_cmd_in)] = {}; |
| 79 | + u32 out[MLX5_ST_SZ_DW(general_obj_out_cmd_hdr)]; |
| 80 | + void *hdr; |
| 81 | + |
| 82 | + hdr = MLX5_ADDR_OF(pcie_cong_event_cmd_in, in, hdr); |
| 83 | + MLX5_SET(general_obj_in_cmd_hdr, hdr, opcode, |
| 84 | + MLX5_CMD_OP_DESTROY_GENERAL_OBJECT); |
| 85 | + MLX5_SET(general_obj_in_cmd_hdr, hdr, obj_type, |
| 86 | + MLX5_GENERAL_OBJECT_TYPES_PCIE_CONG_EVENT); |
| 87 | + MLX5_SET(general_obj_in_cmd_hdr, hdr, obj_id, obj_id); |
| 88 | + |
| 89 | + return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out)); |
| 90 | +} |
| 91 | + |
| 92 | +int mlx5e_pcie_cong_event_init(struct mlx5e_priv *priv) |
| 93 | +{ |
| 94 | + struct mlx5e_pcie_cong_event *cong_event; |
| 95 | + struct mlx5_core_dev *mdev = priv->mdev; |
| 96 | + int err; |
| 97 | + |
| 98 | + if (!mlx5_pcie_cong_event_supported(mdev)) |
| 99 | + return 0; |
| 100 | + |
| 101 | + cong_event = kvzalloc_node(sizeof(*cong_event), GFP_KERNEL, |
| 102 | + mdev->priv.numa_node); |
| 103 | + if (!cong_event) |
| 104 | + return -ENOMEM; |
| 105 | + |
| 106 | + cong_event->priv = priv; |
| 107 | + |
| 108 | + err = mlx5_cmd_pcie_cong_event_set(mdev, &default_thresh_config, |
| 109 | + &cong_event->obj_id); |
| 110 | + if (err) { |
| 111 | + mlx5_core_warn(mdev, "Error creating a PCIe congestion event object\n"); |
| 112 | + goto err_free; |
| 113 | + } |
| 114 | + |
| 115 | + priv->cong_event = cong_event; |
| 116 | + |
| 117 | + return 0; |
| 118 | + |
| 119 | +err_free: |
| 120 | + kvfree(cong_event); |
| 121 | + |
| 122 | + return err; |
| 123 | +} |
| 124 | + |
| 125 | +void mlx5e_pcie_cong_event_cleanup(struct mlx5e_priv *priv) |
| 126 | +{ |
| 127 | + struct mlx5e_pcie_cong_event *cong_event = priv->cong_event; |
| 128 | + struct mlx5_core_dev *mdev = priv->mdev; |
| 129 | + |
| 130 | + if (!cong_event) |
| 131 | + return; |
| 132 | + |
| 133 | + priv->cong_event = NULL; |
| 134 | + |
| 135 | + if (mlx5_cmd_pcie_cong_event_destroy(mdev, cong_event->obj_id)) |
| 136 | + mlx5_core_warn(mdev, "Error destroying PCIe congestion event (obj_id=%llu)\n", |
| 137 | + cong_event->obj_id); |
| 138 | + |
| 139 | + kvfree(cong_event); |
| 140 | +} |
0 commit comments