1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies */
10 static int mlx5_devlink_flash_update(struct devlink *devlink,
11 const char *file_name,
12 const char *component,
13 struct netlink_ext_ack *extack)
15 struct mlx5_core_dev *dev = devlink_priv(devlink);
16 const struct firmware *fw;
22 err = request_firmware_direct(&fw, file_name, &dev->pdev->dev);
26 return mlx5_firmware_flash(dev, fw, extack);
29 static u8 mlx5_fw_ver_major(u32 version)
31 return (version >> 24) & 0xff;
34 static u8 mlx5_fw_ver_minor(u32 version)
36 return (version >> 16) & 0xff;
39 static u16 mlx5_fw_ver_subminor(u32 version)
41 return version & 0xffff;
44 #define DEVLINK_FW_STRING_LEN 32
47 mlx5_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
48 struct netlink_ext_ack *extack)
50 struct mlx5_core_dev *dev = devlink_priv(devlink);
51 char version_str[DEVLINK_FW_STRING_LEN];
52 u32 running_fw, stored_fw;
55 err = devlink_info_driver_name_put(req, DRIVER_NAME);
59 err = devlink_info_version_fixed_put(req, "fw.psid", dev->board_id);
63 err = mlx5_fw_version_query(dev, &running_fw, &stored_fw);
67 snprintf(version_str, sizeof(version_str), "%d.%d.%04d",
68 mlx5_fw_ver_major(running_fw), mlx5_fw_ver_minor(running_fw),
69 mlx5_fw_ver_subminor(running_fw));
70 err = devlink_info_version_running_put(req, "fw.version", version_str);
74 /* no pending version, return running (stored) version */
76 stored_fw = running_fw;
78 snprintf(version_str, sizeof(version_str), "%d.%d.%04d",
79 mlx5_fw_ver_major(stored_fw), mlx5_fw_ver_minor(stored_fw),
80 mlx5_fw_ver_subminor(stored_fw));
81 err = devlink_info_version_stored_put(req, "fw.version", version_str);
88 static int mlx5_devlink_reload_down(struct devlink *devlink, bool netns_change,
89 struct netlink_ext_ack *extack)
91 struct mlx5_core_dev *dev = devlink_priv(devlink);
93 return mlx5_unload_one(dev, false);
96 static int mlx5_devlink_reload_up(struct devlink *devlink,
97 struct netlink_ext_ack *extack)
99 struct mlx5_core_dev *dev = devlink_priv(devlink);
101 return mlx5_load_one(dev, false);
104 static const struct devlink_ops mlx5_devlink_ops = {
105 #ifdef CONFIG_MLX5_ESWITCH
106 .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
107 .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
108 .eswitch_inline_mode_set = mlx5_devlink_eswitch_inline_mode_set,
109 .eswitch_inline_mode_get = mlx5_devlink_eswitch_inline_mode_get,
110 .eswitch_encap_mode_set = mlx5_devlink_eswitch_encap_mode_set,
111 .eswitch_encap_mode_get = mlx5_devlink_eswitch_encap_mode_get,
113 .flash_update = mlx5_devlink_flash_update,
114 .info_get = mlx5_devlink_info_get,
115 .reload_down = mlx5_devlink_reload_down,
116 .reload_up = mlx5_devlink_reload_up,
119 struct devlink *mlx5_devlink_alloc(void)
121 return devlink_alloc(&mlx5_devlink_ops, sizeof(struct mlx5_core_dev));
124 void mlx5_devlink_free(struct devlink *devlink)
126 devlink_free(devlink);
129 static int mlx5_devlink_fs_mode_validate(struct devlink *devlink, u32 id,
130 union devlink_param_value val,
131 struct netlink_ext_ack *extack)
133 struct mlx5_core_dev *dev = devlink_priv(devlink);
134 char *value = val.vstr;
137 if (!strcmp(value, "dmfs")) {
139 } else if (!strcmp(value, "smfs")) {
143 eswitch_mode = mlx5_eswitch_mode(dev->priv.eswitch);
144 smfs_cap = mlx5_fs_dr_is_supported(dev);
148 NL_SET_ERR_MSG_MOD(extack,
149 "Software managed steering is not supported by current device");
152 else if (eswitch_mode == MLX5_ESWITCH_OFFLOADS) {
153 NL_SET_ERR_MSG_MOD(extack,
154 "Software managed steering is not supported when eswitch offloads enabled.");
158 NL_SET_ERR_MSG_MOD(extack,
159 "Bad parameter: supported values are [\"dmfs\", \"smfs\"]");
166 static int mlx5_devlink_fs_mode_set(struct devlink *devlink, u32 id,
167 struct devlink_param_gset_ctx *ctx)
169 struct mlx5_core_dev *dev = devlink_priv(devlink);
170 enum mlx5_flow_steering_mode mode;
172 if (!strcmp(ctx->val.vstr, "smfs"))
173 mode = MLX5_FLOW_STEERING_MODE_SMFS;
175 mode = MLX5_FLOW_STEERING_MODE_DMFS;
176 dev->priv.steering->mode = mode;
181 static int mlx5_devlink_fs_mode_get(struct devlink *devlink, u32 id,
182 struct devlink_param_gset_ctx *ctx)
184 struct mlx5_core_dev *dev = devlink_priv(devlink);
186 if (dev->priv.steering->mode == MLX5_FLOW_STEERING_MODE_SMFS)
187 strcpy(ctx->val.vstr, "smfs");
189 strcpy(ctx->val.vstr, "dmfs");
193 enum mlx5_devlink_param_id {
194 MLX5_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
195 MLX5_DEVLINK_PARAM_FLOW_STEERING_MODE,
198 static int mlx5_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
199 union devlink_param_value val,
200 struct netlink_ext_ack *extack)
202 struct mlx5_core_dev *dev = devlink_priv(devlink);
203 bool new_state = val.vbool;
205 if (new_state && !MLX5_CAP_GEN(dev, roce)) {
206 NL_SET_ERR_MSG_MOD(extack, "Device doesn't support RoCE");
213 static const struct devlink_param mlx5_devlink_params[] = {
214 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_FLOW_STEERING_MODE,
215 "flow_steering_mode", DEVLINK_PARAM_TYPE_STRING,
216 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
217 mlx5_devlink_fs_mode_get, mlx5_devlink_fs_mode_set,
218 mlx5_devlink_fs_mode_validate),
219 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
220 NULL, NULL, mlx5_devlink_enable_roce_validate),
223 static void mlx5_devlink_set_params_init_values(struct devlink *devlink)
225 struct mlx5_core_dev *dev = devlink_priv(devlink);
226 union devlink_param_value value;
228 if (dev->priv.steering->mode == MLX5_FLOW_STEERING_MODE_DMFS)
229 strcpy(value.vstr, "dmfs");
231 strcpy(value.vstr, "smfs");
232 devlink_param_driverinit_value_set(devlink,
233 MLX5_DEVLINK_PARAM_FLOW_STEERING_MODE,
236 value.vbool = MLX5_CAP_GEN(dev, roce);
237 devlink_param_driverinit_value_set(devlink,
238 DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
242 int mlx5_devlink_register(struct devlink *devlink, struct device *dev)
246 err = devlink_register(devlink, dev);
250 err = devlink_params_register(devlink, mlx5_devlink_params,
251 ARRAY_SIZE(mlx5_devlink_params));
254 mlx5_devlink_set_params_init_values(devlink);
255 devlink_params_publish(devlink);
256 devlink_reload_enable(devlink);
260 devlink_unregister(devlink);
264 void mlx5_devlink_unregister(struct devlink *devlink)
266 devlink_reload_disable(devlink);
267 devlink_params_unregister(devlink, mlx5_devlink_params,
268 ARRAY_SIZE(mlx5_devlink_params));
269 devlink_unregister(devlink);