1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies */
11 static int mlx5_devlink_flash_update(struct devlink *devlink,
12 struct devlink_flash_update_params *params,
13 struct netlink_ext_ack *extack)
15 struct mlx5_core_dev *dev = devlink_priv(devlink);
16 const struct firmware *fw;
19 err = request_firmware_direct(&fw, params->file_name, &dev->pdev->dev);
23 err = 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_fw_activate(struct devlink *devlink, struct netlink_ext_ack *extack)
90 struct mlx5_core_dev *dev = devlink_priv(devlink);
91 u8 reset_level, reset_type, net_port_alive;
94 err = mlx5_fw_reset_query(dev, &reset_level, &reset_type);
97 if (!(reset_level & MLX5_MFRL_REG_RESET_LEVEL3)) {
98 NL_SET_ERR_MSG_MOD(extack, "FW activate requires reboot");
102 net_port_alive = !!(reset_type & MLX5_MFRL_REG_RESET_TYPE_NET_PORT_ALIVE);
103 err = mlx5_fw_reset_set_reset_sync(dev, net_port_alive);
107 err = mlx5_fw_reset_wait_reset_done(dev);
110 NL_SET_ERR_MSG_MOD(extack, "FW activate command failed");
114 static int mlx5_devlink_reload_down(struct devlink *devlink, bool netns_change,
115 enum devlink_reload_action action,
116 enum devlink_reload_limit limit,
117 struct netlink_ext_ack *extack)
119 struct mlx5_core_dev *dev = devlink_priv(devlink);
122 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
123 mlx5_unload_one(dev, false);
125 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
126 return mlx5_devlink_reload_fw_activate(devlink, extack);
128 /* Unsupported action should not get to this function */
134 static int mlx5_devlink_reload_up(struct devlink *devlink, enum devlink_reload_action action,
135 enum devlink_reload_limit limit, u32 *actions_performed,
136 struct netlink_ext_ack *extack)
138 struct mlx5_core_dev *dev = devlink_priv(devlink);
140 *actions_performed = BIT(action);
142 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
143 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
144 /* On fw_activate action, also driver is reloaded and reinit performed */
145 *actions_performed |= BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
146 return mlx5_load_one(dev, false);
148 /* Unsupported action should not get to this function */
156 static const struct devlink_ops mlx5_devlink_ops = {
157 #ifdef CONFIG_MLX5_ESWITCH
158 .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
159 .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
160 .eswitch_inline_mode_set = mlx5_devlink_eswitch_inline_mode_set,
161 .eswitch_inline_mode_get = mlx5_devlink_eswitch_inline_mode_get,
162 .eswitch_encap_mode_set = mlx5_devlink_eswitch_encap_mode_set,
163 .eswitch_encap_mode_get = mlx5_devlink_eswitch_encap_mode_get,
164 .port_function_hw_addr_get = mlx5_devlink_port_function_hw_addr_get,
165 .port_function_hw_addr_set = mlx5_devlink_port_function_hw_addr_set,
167 .flash_update = mlx5_devlink_flash_update,
168 .info_get = mlx5_devlink_info_get,
169 .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
170 BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
171 .reload_down = mlx5_devlink_reload_down,
172 .reload_up = mlx5_devlink_reload_up,
175 struct devlink *mlx5_devlink_alloc(void)
177 return devlink_alloc(&mlx5_devlink_ops, sizeof(struct mlx5_core_dev));
180 void mlx5_devlink_free(struct devlink *devlink)
182 devlink_free(devlink);
185 static int mlx5_devlink_fs_mode_validate(struct devlink *devlink, u32 id,
186 union devlink_param_value val,
187 struct netlink_ext_ack *extack)
189 struct mlx5_core_dev *dev = devlink_priv(devlink);
190 char *value = val.vstr;
193 if (!strcmp(value, "dmfs")) {
195 } else if (!strcmp(value, "smfs")) {
199 eswitch_mode = mlx5_eswitch_mode(dev->priv.eswitch);
200 smfs_cap = mlx5_fs_dr_is_supported(dev);
204 NL_SET_ERR_MSG_MOD(extack,
205 "Software managed steering is not supported by current device");
208 else if (eswitch_mode == MLX5_ESWITCH_OFFLOADS) {
209 NL_SET_ERR_MSG_MOD(extack,
210 "Software managed steering is not supported when eswitch offloads enabled.");
214 NL_SET_ERR_MSG_MOD(extack,
215 "Bad parameter: supported values are [\"dmfs\", \"smfs\"]");
222 static int mlx5_devlink_fs_mode_set(struct devlink *devlink, u32 id,
223 struct devlink_param_gset_ctx *ctx)
225 struct mlx5_core_dev *dev = devlink_priv(devlink);
226 enum mlx5_flow_steering_mode mode;
228 if (!strcmp(ctx->val.vstr, "smfs"))
229 mode = MLX5_FLOW_STEERING_MODE_SMFS;
231 mode = MLX5_FLOW_STEERING_MODE_DMFS;
232 dev->priv.steering->mode = mode;
237 static int mlx5_devlink_fs_mode_get(struct devlink *devlink, u32 id,
238 struct devlink_param_gset_ctx *ctx)
240 struct mlx5_core_dev *dev = devlink_priv(devlink);
242 if (dev->priv.steering->mode == MLX5_FLOW_STEERING_MODE_SMFS)
243 strcpy(ctx->val.vstr, "smfs");
245 strcpy(ctx->val.vstr, "dmfs");
249 static int mlx5_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
250 union devlink_param_value val,
251 struct netlink_ext_ack *extack)
253 struct mlx5_core_dev *dev = devlink_priv(devlink);
254 bool new_state = val.vbool;
256 if (new_state && !MLX5_CAP_GEN(dev, roce)) {
257 NL_SET_ERR_MSG_MOD(extack, "Device doesn't support RoCE");
264 #ifdef CONFIG_MLX5_ESWITCH
265 static int mlx5_devlink_large_group_num_validate(struct devlink *devlink, u32 id,
266 union devlink_param_value val,
267 struct netlink_ext_ack *extack)
269 int group_num = val.vu32;
271 if (group_num < 1 || group_num > 1024) {
272 NL_SET_ERR_MSG_MOD(extack,
273 "Unsupported group number, supported range is 1-1024");
281 static const struct devlink_param mlx5_devlink_params[] = {
282 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
283 "flow_steering_mode", DEVLINK_PARAM_TYPE_STRING,
284 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
285 mlx5_devlink_fs_mode_get, mlx5_devlink_fs_mode_set,
286 mlx5_devlink_fs_mode_validate),
287 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
288 NULL, NULL, mlx5_devlink_enable_roce_validate),
289 #ifdef CONFIG_MLX5_ESWITCH
290 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
291 "fdb_large_groups", DEVLINK_PARAM_TYPE_U32,
292 BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
294 mlx5_devlink_large_group_num_validate),
298 static void mlx5_devlink_set_params_init_values(struct devlink *devlink)
300 struct mlx5_core_dev *dev = devlink_priv(devlink);
301 union devlink_param_value value;
303 if (dev->priv.steering->mode == MLX5_FLOW_STEERING_MODE_DMFS)
304 strcpy(value.vstr, "dmfs");
306 strcpy(value.vstr, "smfs");
307 devlink_param_driverinit_value_set(devlink,
308 MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
311 value.vbool = MLX5_CAP_GEN(dev, roce);
312 devlink_param_driverinit_value_set(devlink,
313 DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
316 #ifdef CONFIG_MLX5_ESWITCH
317 value.vu32 = ESW_OFFLOADS_DEFAULT_NUM_GROUPS;
318 devlink_param_driverinit_value_set(devlink,
319 MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
324 int mlx5_devlink_register(struct devlink *devlink, struct device *dev)
328 err = devlink_register(devlink, dev);
332 err = devlink_params_register(devlink, mlx5_devlink_params,
333 ARRAY_SIZE(mlx5_devlink_params));
336 mlx5_devlink_set_params_init_values(devlink);
337 devlink_params_publish(devlink);
341 devlink_unregister(devlink);
345 void mlx5_devlink_unregister(struct devlink *devlink)
347 devlink_params_unregister(devlink, mlx5_devlink_params,
348 ARRAY_SIZE(mlx5_devlink_params));
349 devlink_unregister(devlink);