1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2019 Mellanox Technologies */
3
4 #include <devlink.h>
5
6 #include "mlx5_core.h"
7 #include "fw_reset.h"
8 #include "fs_core.h"
9 #include "eswitch.h"
10
mlx5_devlink_flash_update(struct devlink * devlink,struct devlink_flash_update_params * params,struct netlink_ext_ack * extack)11 static int mlx5_devlink_flash_update(struct devlink *devlink,
12 struct devlink_flash_update_params *params,
13 struct netlink_ext_ack *extack)
14 {
15 struct mlx5_core_dev *dev = devlink_priv(devlink);
16 const struct firmware *fw;
17 int err;
18
19 err = request_firmware_direct(&fw, params->file_name, &dev->pdev->dev);
20 if (err)
21 return err;
22
23 err = mlx5_firmware_flash(dev, fw, extack);
24 release_firmware(fw);
25
26 return err;
27 }
28
mlx5_fw_ver_major(u32 version)29 static u8 mlx5_fw_ver_major(u32 version)
30 {
31 return (version >> 24) & 0xff;
32 }
33
mlx5_fw_ver_minor(u32 version)34 static u8 mlx5_fw_ver_minor(u32 version)
35 {
36 return (version >> 16) & 0xff;
37 }
38
mlx5_fw_ver_subminor(u32 version)39 static u16 mlx5_fw_ver_subminor(u32 version)
40 {
41 return version & 0xffff;
42 }
43
44 #define DEVLINK_FW_STRING_LEN 32
45
46 static int
mlx5_devlink_info_get(struct devlink * devlink,struct devlink_info_req * req,struct netlink_ext_ack * extack)47 mlx5_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
48 struct netlink_ext_ack *extack)
49 {
50 struct mlx5_core_dev *dev = devlink_priv(devlink);
51 char version_str[DEVLINK_FW_STRING_LEN];
52 u32 running_fw, stored_fw;
53 int err;
54
55 err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
56 if (err)
57 return err;
58
59 err = devlink_info_version_fixed_put(req, "fw.psid", dev->board_id);
60 if (err)
61 return err;
62
63 err = mlx5_fw_version_query(dev, &running_fw, &stored_fw);
64 if (err)
65 return err;
66
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);
71 if (err)
72 return err;
73
74 /* no pending version, return running (stored) version */
75 if (stored_fw == 0)
76 stored_fw = running_fw;
77
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);
82 if (err)
83 return err;
84
85 return 0;
86 }
87
mlx5_devlink_reload_fw_activate(struct devlink * devlink,struct netlink_ext_ack * extack)88 static int mlx5_devlink_reload_fw_activate(struct devlink *devlink, struct netlink_ext_ack *extack)
89 {
90 struct mlx5_core_dev *dev = devlink_priv(devlink);
91 u8 reset_level, reset_type, net_port_alive;
92 int err;
93
94 err = mlx5_fw_reset_query(dev, &reset_level, &reset_type);
95 if (err)
96 return err;
97 if (!(reset_level & MLX5_MFRL_REG_RESET_LEVEL3)) {
98 NL_SET_ERR_MSG_MOD(extack, "FW activate requires reboot");
99 return -EINVAL;
100 }
101
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);
104 if (err)
105 goto out;
106
107 err = mlx5_fw_reset_wait_reset_done(dev);
108 out:
109 if (err)
110 NL_SET_ERR_MSG_MOD(extack, "FW activate command failed");
111 return err;
112 }
113
mlx5_devlink_trigger_fw_live_patch(struct devlink * devlink,struct netlink_ext_ack * extack)114 static int mlx5_devlink_trigger_fw_live_patch(struct devlink *devlink,
115 struct netlink_ext_ack *extack)
116 {
117 struct mlx5_core_dev *dev = devlink_priv(devlink);
118 u8 reset_level;
119 int err;
120
121 err = mlx5_fw_reset_query(dev, &reset_level, NULL);
122 if (err)
123 return err;
124 if (!(reset_level & MLX5_MFRL_REG_RESET_LEVEL0)) {
125 NL_SET_ERR_MSG_MOD(extack,
126 "FW upgrade to the stored FW can't be done by FW live patching");
127 return -EINVAL;
128 }
129
130 return mlx5_fw_reset_set_live_patch(dev);
131 }
132
mlx5_devlink_reload_down(struct devlink * devlink,bool netns_change,enum devlink_reload_action action,enum devlink_reload_limit limit,struct netlink_ext_ack * extack)133 static int mlx5_devlink_reload_down(struct devlink *devlink, bool netns_change,
134 enum devlink_reload_action action,
135 enum devlink_reload_limit limit,
136 struct netlink_ext_ack *extack)
137 {
138 struct mlx5_core_dev *dev = devlink_priv(devlink);
139
140 if (mlx5_lag_is_active(dev)) {
141 NL_SET_ERR_MSG_MOD(extack, "reload is unsupported in Lag mode\n");
142 return -EOPNOTSUPP;
143 }
144
145 switch (action) {
146 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
147 mlx5_unload_one(dev, false);
148 return 0;
149 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
150 if (limit == DEVLINK_RELOAD_LIMIT_NO_RESET)
151 return mlx5_devlink_trigger_fw_live_patch(devlink, extack);
152 return mlx5_devlink_reload_fw_activate(devlink, extack);
153 default:
154 /* Unsupported action should not get to this function */
155 WARN_ON(1);
156 return -EOPNOTSUPP;
157 }
158 }
159
mlx5_devlink_reload_up(struct devlink * devlink,enum devlink_reload_action action,enum devlink_reload_limit limit,u32 * actions_performed,struct netlink_ext_ack * extack)160 static int mlx5_devlink_reload_up(struct devlink *devlink, enum devlink_reload_action action,
161 enum devlink_reload_limit limit, u32 *actions_performed,
162 struct netlink_ext_ack *extack)
163 {
164 struct mlx5_core_dev *dev = devlink_priv(devlink);
165
166 *actions_performed = BIT(action);
167 switch (action) {
168 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
169 return mlx5_load_one(dev, false);
170 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
171 if (limit == DEVLINK_RELOAD_LIMIT_NO_RESET)
172 break;
173 /* On fw_activate action, also driver is reloaded and reinit performed */
174 *actions_performed |= BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
175 return mlx5_load_one(dev, false);
176 default:
177 /* Unsupported action should not get to this function */
178 WARN_ON(1);
179 return -EOPNOTSUPP;
180 }
181
182 return 0;
183 }
184
185 static const struct devlink_ops mlx5_devlink_ops = {
186 #ifdef CONFIG_MLX5_ESWITCH
187 .eswitch_mode_set = mlx5_devlink_eswitch_mode_set,
188 .eswitch_mode_get = mlx5_devlink_eswitch_mode_get,
189 .eswitch_inline_mode_set = mlx5_devlink_eswitch_inline_mode_set,
190 .eswitch_inline_mode_get = mlx5_devlink_eswitch_inline_mode_get,
191 .eswitch_encap_mode_set = mlx5_devlink_eswitch_encap_mode_set,
192 .eswitch_encap_mode_get = mlx5_devlink_eswitch_encap_mode_get,
193 .port_function_hw_addr_get = mlx5_devlink_port_function_hw_addr_get,
194 .port_function_hw_addr_set = mlx5_devlink_port_function_hw_addr_set,
195 #endif
196 .flash_update = mlx5_devlink_flash_update,
197 .info_get = mlx5_devlink_info_get,
198 .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
199 BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
200 .reload_limits = BIT(DEVLINK_RELOAD_LIMIT_NO_RESET),
201 .reload_down = mlx5_devlink_reload_down,
202 .reload_up = mlx5_devlink_reload_up,
203 };
204
mlx5_devlink_alloc(void)205 struct devlink *mlx5_devlink_alloc(void)
206 {
207 return devlink_alloc(&mlx5_devlink_ops, sizeof(struct mlx5_core_dev));
208 }
209
mlx5_devlink_free(struct devlink * devlink)210 void mlx5_devlink_free(struct devlink *devlink)
211 {
212 devlink_free(devlink);
213 }
214
mlx5_devlink_fs_mode_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)215 static int mlx5_devlink_fs_mode_validate(struct devlink *devlink, u32 id,
216 union devlink_param_value val,
217 struct netlink_ext_ack *extack)
218 {
219 struct mlx5_core_dev *dev = devlink_priv(devlink);
220 char *value = val.vstr;
221 int err = 0;
222
223 if (!strcmp(value, "dmfs")) {
224 return 0;
225 } else if (!strcmp(value, "smfs")) {
226 u8 eswitch_mode;
227 bool smfs_cap;
228
229 eswitch_mode = mlx5_eswitch_mode(dev->priv.eswitch);
230 smfs_cap = mlx5_fs_dr_is_supported(dev);
231
232 if (!smfs_cap) {
233 err = -EOPNOTSUPP;
234 NL_SET_ERR_MSG_MOD(extack,
235 "Software managed steering is not supported by current device");
236 }
237
238 else if (eswitch_mode == MLX5_ESWITCH_OFFLOADS) {
239 NL_SET_ERR_MSG_MOD(extack,
240 "Software managed steering is not supported when eswitch offloads enabled.");
241 err = -EOPNOTSUPP;
242 }
243 } else {
244 NL_SET_ERR_MSG_MOD(extack,
245 "Bad parameter: supported values are [\"dmfs\", \"smfs\"]");
246 err = -EINVAL;
247 }
248
249 return err;
250 }
251
mlx5_devlink_fs_mode_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)252 static int mlx5_devlink_fs_mode_set(struct devlink *devlink, u32 id,
253 struct devlink_param_gset_ctx *ctx)
254 {
255 struct mlx5_core_dev *dev = devlink_priv(devlink);
256 enum mlx5_flow_steering_mode mode;
257
258 if (!strcmp(ctx->val.vstr, "smfs"))
259 mode = MLX5_FLOW_STEERING_MODE_SMFS;
260 else
261 mode = MLX5_FLOW_STEERING_MODE_DMFS;
262 dev->priv.steering->mode = mode;
263
264 return 0;
265 }
266
mlx5_devlink_fs_mode_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)267 static int mlx5_devlink_fs_mode_get(struct devlink *devlink, u32 id,
268 struct devlink_param_gset_ctx *ctx)
269 {
270 struct mlx5_core_dev *dev = devlink_priv(devlink);
271
272 if (dev->priv.steering->mode == MLX5_FLOW_STEERING_MODE_SMFS)
273 strcpy(ctx->val.vstr, "smfs");
274 else
275 strcpy(ctx->val.vstr, "dmfs");
276 return 0;
277 }
278
mlx5_devlink_enable_roce_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)279 static int mlx5_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
280 union devlink_param_value val,
281 struct netlink_ext_ack *extack)
282 {
283 struct mlx5_core_dev *dev = devlink_priv(devlink);
284 bool new_state = val.vbool;
285
286 if (new_state && !MLX5_CAP_GEN(dev, roce)) {
287 NL_SET_ERR_MSG_MOD(extack, "Device doesn't support RoCE");
288 return -EOPNOTSUPP;
289 }
290 if (mlx5_core_is_mp_slave(dev) || mlx5_lag_is_active(dev)) {
291 NL_SET_ERR_MSG_MOD(extack, "Multi port slave/Lag device can't configure RoCE");
292 return -EOPNOTSUPP;
293 }
294
295 return 0;
296 }
297
298 #ifdef CONFIG_MLX5_ESWITCH
mlx5_devlink_large_group_num_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)299 static int mlx5_devlink_large_group_num_validate(struct devlink *devlink, u32 id,
300 union devlink_param_value val,
301 struct netlink_ext_ack *extack)
302 {
303 int group_num = val.vu32;
304
305 if (group_num < 1 || group_num > 1024) {
306 NL_SET_ERR_MSG_MOD(extack,
307 "Unsupported group number, supported range is 1-1024");
308 return -EOPNOTSUPP;
309 }
310
311 return 0;
312 }
313 #endif
314
mlx5_devlink_enable_remote_dev_reset_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)315 static int mlx5_devlink_enable_remote_dev_reset_set(struct devlink *devlink, u32 id,
316 struct devlink_param_gset_ctx *ctx)
317 {
318 struct mlx5_core_dev *dev = devlink_priv(devlink);
319
320 mlx5_fw_reset_enable_remote_dev_reset_set(dev, ctx->val.vbool);
321 return 0;
322 }
323
mlx5_devlink_enable_remote_dev_reset_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)324 static int mlx5_devlink_enable_remote_dev_reset_get(struct devlink *devlink, u32 id,
325 struct devlink_param_gset_ctx *ctx)
326 {
327 struct mlx5_core_dev *dev = devlink_priv(devlink);
328
329 ctx->val.vbool = mlx5_fw_reset_enable_remote_dev_reset_get(dev);
330 return 0;
331 }
332
333 static const struct devlink_param mlx5_devlink_params[] = {
334 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
335 "flow_steering_mode", DEVLINK_PARAM_TYPE_STRING,
336 BIT(DEVLINK_PARAM_CMODE_RUNTIME),
337 mlx5_devlink_fs_mode_get, mlx5_devlink_fs_mode_set,
338 mlx5_devlink_fs_mode_validate),
339 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
340 NULL, NULL, mlx5_devlink_enable_roce_validate),
341 #ifdef CONFIG_MLX5_ESWITCH
342 DEVLINK_PARAM_DRIVER(MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
343 "fdb_large_groups", DEVLINK_PARAM_TYPE_U32,
344 BIT(DEVLINK_PARAM_CMODE_DRIVERINIT),
345 NULL, NULL,
346 mlx5_devlink_large_group_num_validate),
347 #endif
348 DEVLINK_PARAM_GENERIC(ENABLE_REMOTE_DEV_RESET, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
349 mlx5_devlink_enable_remote_dev_reset_get,
350 mlx5_devlink_enable_remote_dev_reset_set, NULL),
351 };
352
mlx5_devlink_set_params_init_values(struct devlink * devlink)353 static void mlx5_devlink_set_params_init_values(struct devlink *devlink)
354 {
355 struct mlx5_core_dev *dev = devlink_priv(devlink);
356 union devlink_param_value value;
357
358 if (dev->priv.steering->mode == MLX5_FLOW_STEERING_MODE_DMFS)
359 strcpy(value.vstr, "dmfs");
360 else
361 strcpy(value.vstr, "smfs");
362 devlink_param_driverinit_value_set(devlink,
363 MLX5_DEVLINK_PARAM_ID_FLOW_STEERING_MODE,
364 value);
365
366 value.vbool = MLX5_CAP_GEN(dev, roce);
367 devlink_param_driverinit_value_set(devlink,
368 DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
369 value);
370
371 #ifdef CONFIG_MLX5_ESWITCH
372 value.vu32 = ESW_OFFLOADS_DEFAULT_NUM_GROUPS;
373 devlink_param_driverinit_value_set(devlink,
374 MLX5_DEVLINK_PARAM_ID_ESW_LARGE_GROUP_NUM,
375 value);
376 #endif
377 }
378
mlx5_devlink_register(struct devlink * devlink,struct device * dev)379 int mlx5_devlink_register(struct devlink *devlink, struct device *dev)
380 {
381 int err;
382
383 err = devlink_register(devlink, dev);
384 if (err)
385 return err;
386
387 err = devlink_params_register(devlink, mlx5_devlink_params,
388 ARRAY_SIZE(mlx5_devlink_params));
389 if (err)
390 goto params_reg_err;
391 mlx5_devlink_set_params_init_values(devlink);
392 devlink_params_publish(devlink);
393 return 0;
394
395 params_reg_err:
396 devlink_unregister(devlink);
397 return err;
398 }
399
mlx5_devlink_unregister(struct devlink * devlink)400 void mlx5_devlink_unregister(struct devlink *devlink)
401 {
402 devlink_params_unpublish(devlink);
403 devlink_params_unregister(devlink, mlx5_devlink_params,
404 ARRAY_SIZE(mlx5_devlink_params));
405 devlink_unregister(devlink);
406 }
407