1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (c) 2020, Intel Corporation. */
3
4 #include <linux/vmalloc.h>
5
6 #include "ice.h"
7 #include "ice_lib.h"
8 #include "ice_devlink.h"
9 #include "ice_eswitch.h"
10 #include "ice_fw_update.h"
11 #include "ice_dcb_lib.h"
12
13 static int ice_active_port_option = -1;
14
15 /* context for devlink info version reporting */
16 struct ice_info_ctx {
17 char buf[128];
18 struct ice_orom_info pending_orom;
19 struct ice_nvm_info pending_nvm;
20 struct ice_netlist_info pending_netlist;
21 struct ice_hw_dev_caps dev_caps;
22 };
23
24 /* The following functions are used to format specific strings for various
25 * devlink info versions. The ctx parameter is used to provide the storage
26 * buffer, as well as any ancillary information calculated when the info
27 * request was made.
28 *
29 * If a version does not exist, for example when attempting to get the
30 * inactive version of flash when there is no pending update, the function
31 * should leave the buffer in the ctx structure empty.
32 */
33
ice_info_get_dsn(struct ice_pf * pf,struct ice_info_ctx * ctx)34 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
35 {
36 u8 dsn[8];
37
38 /* Copy the DSN into an array in Big Endian format */
39 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
40
41 snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
42 }
43
ice_info_pba(struct ice_pf * pf,struct ice_info_ctx * ctx)44 static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
45 {
46 struct ice_hw *hw = &pf->hw;
47 int status;
48
49 status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
50 if (status)
51 /* We failed to locate the PBA, so just skip this entry */
52 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %d\n",
53 status);
54 }
55
ice_info_fw_mgmt(struct ice_pf * pf,struct ice_info_ctx * ctx)56 static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
57 {
58 struct ice_hw *hw = &pf->hw;
59
60 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
61 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
62 }
63
ice_info_fw_api(struct ice_pf * pf,struct ice_info_ctx * ctx)64 static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
65 {
66 struct ice_hw *hw = &pf->hw;
67
68 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
69 hw->api_min_ver, hw->api_patch);
70 }
71
ice_info_fw_build(struct ice_pf * pf,struct ice_info_ctx * ctx)72 static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
73 {
74 struct ice_hw *hw = &pf->hw;
75
76 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
77 }
78
ice_info_orom_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)79 static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
80 {
81 struct ice_orom_info *orom = &pf->hw.flash.orom;
82
83 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
84 orom->major, orom->build, orom->patch);
85 }
86
87 static void
ice_info_pending_orom_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)88 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
89 struct ice_info_ctx *ctx)
90 {
91 struct ice_orom_info *orom = &ctx->pending_orom;
92
93 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
94 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
95 orom->major, orom->build, orom->patch);
96 }
97
ice_info_nvm_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)98 static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
99 {
100 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
101
102 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
103 }
104
105 static void
ice_info_pending_nvm_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)106 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
107 struct ice_info_ctx *ctx)
108 {
109 struct ice_nvm_info *nvm = &ctx->pending_nvm;
110
111 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
112 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
113 nvm->major, nvm->minor);
114 }
115
ice_info_eetrack(struct ice_pf * pf,struct ice_info_ctx * ctx)116 static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
117 {
118 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
119
120 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
121 }
122
123 static void
ice_info_pending_eetrack(struct ice_pf * pf,struct ice_info_ctx * ctx)124 ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
125 {
126 struct ice_nvm_info *nvm = &ctx->pending_nvm;
127
128 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
129 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
130 }
131
ice_info_ddp_pkg_name(struct ice_pf * pf,struct ice_info_ctx * ctx)132 static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
133 {
134 struct ice_hw *hw = &pf->hw;
135
136 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
137 }
138
139 static void
ice_info_ddp_pkg_version(struct ice_pf * pf,struct ice_info_ctx * ctx)140 ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
141 {
142 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
143
144 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
145 pkg->major, pkg->minor, pkg->update, pkg->draft);
146 }
147
148 static void
ice_info_ddp_pkg_bundle_id(struct ice_pf * pf,struct ice_info_ctx * ctx)149 ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
150 {
151 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
152 }
153
ice_info_netlist_ver(struct ice_pf * pf,struct ice_info_ctx * ctx)154 static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
155 {
156 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
157
158 /* The netlist version fields are BCD formatted */
159 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
160 netlist->major, netlist->minor,
161 netlist->type >> 16, netlist->type & 0xFFFF,
162 netlist->rev, netlist->cust_ver);
163 }
164
ice_info_netlist_build(struct ice_pf * pf,struct ice_info_ctx * ctx)165 static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
166 {
167 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
168
169 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
170 }
171
172 static void
ice_info_pending_netlist_ver(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)173 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
174 struct ice_info_ctx *ctx)
175 {
176 struct ice_netlist_info *netlist = &ctx->pending_netlist;
177
178 /* The netlist version fields are BCD formatted */
179 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
180 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
181 netlist->major, netlist->minor,
182 netlist->type >> 16, netlist->type & 0xFFFF,
183 netlist->rev, netlist->cust_ver);
184 }
185
186 static void
ice_info_pending_netlist_build(struct ice_pf __always_unused * pf,struct ice_info_ctx * ctx)187 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
188 struct ice_info_ctx *ctx)
189 {
190 struct ice_netlist_info *netlist = &ctx->pending_netlist;
191
192 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
193 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
194 }
195
196 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
197 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
198 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
199
200 /* The combined() macro inserts both the running entry as well as a stored
201 * entry. The running entry will always report the version from the active
202 * handler. The stored entry will first try the pending handler, and fallback
203 * to the active handler if the pending function does not report a version.
204 * The pending handler should check the status of a pending update for the
205 * relevant flash component. It should only fill in the buffer in the case
206 * where a valid pending version is available. This ensures that the related
207 * stored and running versions remain in sync, and that stored versions are
208 * correctly reported as expected.
209 */
210 #define combined(key, active, pending) \
211 running(key, active), \
212 stored(key, pending, active)
213
214 enum ice_version_type {
215 ICE_VERSION_FIXED,
216 ICE_VERSION_RUNNING,
217 ICE_VERSION_STORED,
218 };
219
220 static const struct ice_devlink_version {
221 enum ice_version_type type;
222 const char *key;
223 void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
224 void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
225 } ice_devlink_versions[] = {
226 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
227 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
228 running("fw.mgmt.api", ice_info_fw_api),
229 running("fw.mgmt.build", ice_info_fw_build),
230 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
231 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
232 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
233 running("fw.app.name", ice_info_ddp_pkg_name),
234 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
235 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
236 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
237 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
238 };
239
240 /**
241 * ice_devlink_info_get - .info_get devlink handler
242 * @devlink: devlink instance structure
243 * @req: the devlink info request
244 * @extack: extended netdev ack structure
245 *
246 * Callback for the devlink .info_get operation. Reports information about the
247 * device.
248 *
249 * Return: zero on success or an error code on failure.
250 */
ice_devlink_info_get(struct devlink * devlink,struct devlink_info_req * req,struct netlink_ext_ack * extack)251 static int ice_devlink_info_get(struct devlink *devlink,
252 struct devlink_info_req *req,
253 struct netlink_ext_ack *extack)
254 {
255 struct ice_pf *pf = devlink_priv(devlink);
256 struct device *dev = ice_pf_to_dev(pf);
257 struct ice_hw *hw = &pf->hw;
258 struct ice_info_ctx *ctx;
259 size_t i;
260 int err;
261
262 err = ice_wait_for_reset(pf, 10 * HZ);
263 if (err) {
264 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
265 return err;
266 }
267
268 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
269 if (!ctx)
270 return -ENOMEM;
271
272 /* discover capabilities first */
273 err = ice_discover_dev_caps(hw, &ctx->dev_caps);
274 if (err) {
275 dev_dbg(dev, "Failed to discover device capabilities, status %d aq_err %s\n",
276 err, ice_aq_str(hw->adminq.sq_last_status));
277 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
278 goto out_free_ctx;
279 }
280
281 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
282 err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
283 if (err) {
284 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %d aq_err %s\n",
285 err, ice_aq_str(hw->adminq.sq_last_status));
286
287 /* disable display of pending Option ROM */
288 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
289 }
290 }
291
292 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
293 err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
294 if (err) {
295 dev_dbg(dev, "Unable to read inactive NVM version data, status %d aq_err %s\n",
296 err, ice_aq_str(hw->adminq.sq_last_status));
297
298 /* disable display of pending Option ROM */
299 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
300 }
301 }
302
303 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
304 err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
305 if (err) {
306 dev_dbg(dev, "Unable to read inactive Netlist version data, status %d aq_err %s\n",
307 err, ice_aq_str(hw->adminq.sq_last_status));
308
309 /* disable display of pending Option ROM */
310 ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
311 }
312 }
313
314 ice_info_get_dsn(pf, ctx);
315
316 err = devlink_info_serial_number_put(req, ctx->buf);
317 if (err) {
318 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
319 goto out_free_ctx;
320 }
321
322 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
323 enum ice_version_type type = ice_devlink_versions[i].type;
324 const char *key = ice_devlink_versions[i].key;
325
326 memset(ctx->buf, 0, sizeof(ctx->buf));
327
328 ice_devlink_versions[i].getter(pf, ctx);
329
330 /* If the default getter doesn't report a version, use the
331 * fallback function. This is primarily useful in the case of
332 * "stored" versions that want to report the same value as the
333 * running version in the normal case of no pending update.
334 */
335 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
336 ice_devlink_versions[i].fallback(pf, ctx);
337
338 /* Do not report missing versions */
339 if (ctx->buf[0] == '\0')
340 continue;
341
342 switch (type) {
343 case ICE_VERSION_FIXED:
344 err = devlink_info_version_fixed_put(req, key, ctx->buf);
345 if (err) {
346 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
347 goto out_free_ctx;
348 }
349 break;
350 case ICE_VERSION_RUNNING:
351 err = devlink_info_version_running_put(req, key, ctx->buf);
352 if (err) {
353 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
354 goto out_free_ctx;
355 }
356 break;
357 case ICE_VERSION_STORED:
358 err = devlink_info_version_stored_put(req, key, ctx->buf);
359 if (err) {
360 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
361 goto out_free_ctx;
362 }
363 break;
364 }
365 }
366
367 out_free_ctx:
368 kfree(ctx);
369 return err;
370 }
371
372 /**
373 * ice_devlink_reload_empr_start - Start EMP reset to activate new firmware
374 * @pf: pointer to the pf instance
375 * @extack: netlink extended ACK structure
376 *
377 * Allow user to activate new Embedded Management Processor firmware by
378 * issuing device specific EMP reset. Called in response to
379 * a DEVLINK_CMD_RELOAD with the DEVLINK_RELOAD_ACTION_FW_ACTIVATE.
380 *
381 * Note that teardown and rebuild of the driver state happens automatically as
382 * part of an interrupt and watchdog task. This is because all physical
383 * functions on the device must be able to reset when an EMP reset occurs from
384 * any source.
385 */
386 static int
ice_devlink_reload_empr_start(struct ice_pf * pf,struct netlink_ext_ack * extack)387 ice_devlink_reload_empr_start(struct ice_pf *pf,
388 struct netlink_ext_ack *extack)
389 {
390 struct device *dev = ice_pf_to_dev(pf);
391 struct ice_hw *hw = &pf->hw;
392 u8 pending;
393 int err;
394
395 err = ice_get_pending_updates(pf, &pending, extack);
396 if (err)
397 return err;
398
399 /* pending is a bitmask of which flash banks have a pending update,
400 * including the main NVM bank, the Option ROM bank, and the netlist
401 * bank. If any of these bits are set, then there is a pending update
402 * waiting to be activated.
403 */
404 if (!pending) {
405 NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
406 return -ECANCELED;
407 }
408
409 if (pf->fw_emp_reset_disabled) {
410 NL_SET_ERR_MSG_MOD(extack, "EMP reset is not available. To activate firmware, a reboot or power cycle is needed");
411 return -ECANCELED;
412 }
413
414 dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
415
416 err = ice_aq_nvm_update_empr(hw);
417 if (err) {
418 dev_err(dev, "Failed to trigger EMP device reset to reload firmware, err %d aq_err %s\n",
419 err, ice_aq_str(hw->adminq.sq_last_status));
420 NL_SET_ERR_MSG_MOD(extack, "Failed to trigger EMP device reset to reload firmware");
421 return err;
422 }
423
424 return 0;
425 }
426
427 /**
428 * ice_devlink_reload_down - prepare for reload
429 * @devlink: pointer to the devlink instance to reload
430 * @netns_change: if true, the network namespace is changing
431 * @action: the action to perform
432 * @limit: limits on what reload should do, such as not resetting
433 * @extack: netlink extended ACK structure
434 */
435 static int
ice_devlink_reload_down(struct devlink * devlink,bool netns_change,enum devlink_reload_action action,enum devlink_reload_limit limit,struct netlink_ext_ack * extack)436 ice_devlink_reload_down(struct devlink *devlink, bool netns_change,
437 enum devlink_reload_action action,
438 enum devlink_reload_limit limit,
439 struct netlink_ext_ack *extack)
440 {
441 struct ice_pf *pf = devlink_priv(devlink);
442
443 switch (action) {
444 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
445 if (ice_is_eswitch_mode_switchdev(pf)) {
446 NL_SET_ERR_MSG_MOD(extack,
447 "Go to legacy mode before doing reinit\n");
448 return -EOPNOTSUPP;
449 }
450 if (ice_is_adq_active(pf)) {
451 NL_SET_ERR_MSG_MOD(extack,
452 "Turn off ADQ before doing reinit\n");
453 return -EOPNOTSUPP;
454 }
455 if (ice_has_vfs(pf)) {
456 NL_SET_ERR_MSG_MOD(extack,
457 "Remove all VFs before doing reinit\n");
458 return -EOPNOTSUPP;
459 }
460 ice_unload(pf);
461 return 0;
462 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
463 return ice_devlink_reload_empr_start(pf, extack);
464 default:
465 WARN_ON(1);
466 return -EOPNOTSUPP;
467 }
468 }
469
470 /**
471 * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
472 * @pf: pointer to the pf instance
473 * @extack: netlink extended ACK structure
474 *
475 * Wait for driver to finish rebuilding after EMP reset is completed. This
476 * includes time to wait for both the actual device reset as well as the time
477 * for the driver's rebuild to complete.
478 */
479 static int
ice_devlink_reload_empr_finish(struct ice_pf * pf,struct netlink_ext_ack * extack)480 ice_devlink_reload_empr_finish(struct ice_pf *pf,
481 struct netlink_ext_ack *extack)
482 {
483 int err;
484
485 err = ice_wait_for_reset(pf, 60 * HZ);
486 if (err) {
487 NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
488 return err;
489 }
490
491 return 0;
492 }
493
494 /**
495 * ice_devlink_port_opt_speed_str - convert speed to a string
496 * @speed: speed value
497 */
ice_devlink_port_opt_speed_str(u8 speed)498 static const char *ice_devlink_port_opt_speed_str(u8 speed)
499 {
500 switch (speed & ICE_AQC_PORT_OPT_MAX_LANE_M) {
501 case ICE_AQC_PORT_OPT_MAX_LANE_100M:
502 return "0.1";
503 case ICE_AQC_PORT_OPT_MAX_LANE_1G:
504 return "1";
505 case ICE_AQC_PORT_OPT_MAX_LANE_2500M:
506 return "2.5";
507 case ICE_AQC_PORT_OPT_MAX_LANE_5G:
508 return "5";
509 case ICE_AQC_PORT_OPT_MAX_LANE_10G:
510 return "10";
511 case ICE_AQC_PORT_OPT_MAX_LANE_25G:
512 return "25";
513 case ICE_AQC_PORT_OPT_MAX_LANE_50G:
514 return "50";
515 case ICE_AQC_PORT_OPT_MAX_LANE_100G:
516 return "100";
517 }
518
519 return "-";
520 }
521
522 #define ICE_PORT_OPT_DESC_LEN 50
523 /**
524 * ice_devlink_port_options_print - Print available port split options
525 * @pf: the PF to print split port options
526 *
527 * Prints a table with available port split options and max port speeds
528 */
ice_devlink_port_options_print(struct ice_pf * pf)529 static void ice_devlink_port_options_print(struct ice_pf *pf)
530 {
531 u8 i, j, options_count, cnt, speed, pending_idx, active_idx;
532 struct ice_aqc_get_port_options_elem *options, *opt;
533 struct device *dev = ice_pf_to_dev(pf);
534 bool active_valid, pending_valid;
535 char desc[ICE_PORT_OPT_DESC_LEN];
536 const char *str;
537 int status;
538
539 options = kcalloc(ICE_AQC_PORT_OPT_MAX * ICE_MAX_PORT_PER_PCI_DEV,
540 sizeof(*options), GFP_KERNEL);
541 if (!options)
542 return;
543
544 for (i = 0; i < ICE_MAX_PORT_PER_PCI_DEV; i++) {
545 opt = options + i * ICE_AQC_PORT_OPT_MAX;
546 options_count = ICE_AQC_PORT_OPT_MAX;
547 active_valid = 0;
548
549 status = ice_aq_get_port_options(&pf->hw, opt, &options_count,
550 i, true, &active_idx,
551 &active_valid, &pending_idx,
552 &pending_valid);
553 if (status) {
554 dev_dbg(dev, "Couldn't read port option for port %d, err %d\n",
555 i, status);
556 goto err;
557 }
558 }
559
560 dev_dbg(dev, "Available port split options and max port speeds (Gbps):\n");
561 dev_dbg(dev, "Status Split Quad 0 Quad 1\n");
562 dev_dbg(dev, " count L0 L1 L2 L3 L4 L5 L6 L7\n");
563
564 for (i = 0; i < options_count; i++) {
565 cnt = 0;
566
567 if (i == ice_active_port_option)
568 str = "Active";
569 else if ((i == pending_idx) && pending_valid)
570 str = "Pending";
571 else
572 str = "";
573
574 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
575 "%-8s", str);
576
577 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
578 "%-6u", options[i].pmd);
579
580 for (j = 0; j < ICE_MAX_PORT_PER_PCI_DEV; ++j) {
581 speed = options[i + j * ICE_AQC_PORT_OPT_MAX].max_lane_speed;
582 str = ice_devlink_port_opt_speed_str(speed);
583 cnt += snprintf(&desc[cnt], ICE_PORT_OPT_DESC_LEN - cnt,
584 "%3s ", str);
585 }
586
587 dev_dbg(dev, "%s\n", desc);
588 }
589
590 err:
591 kfree(options);
592 }
593
594 /**
595 * ice_devlink_aq_set_port_option - Send set port option admin queue command
596 * @pf: the PF to print split port options
597 * @option_idx: selected port option
598 * @extack: extended netdev ack structure
599 *
600 * Sends set port option admin queue command with selected port option and
601 * calls NVM write activate.
602 */
603 static int
ice_devlink_aq_set_port_option(struct ice_pf * pf,u8 option_idx,struct netlink_ext_ack * extack)604 ice_devlink_aq_set_port_option(struct ice_pf *pf, u8 option_idx,
605 struct netlink_ext_ack *extack)
606 {
607 struct device *dev = ice_pf_to_dev(pf);
608 int status;
609
610 status = ice_aq_set_port_option(&pf->hw, 0, true, option_idx);
611 if (status) {
612 dev_dbg(dev, "ice_aq_set_port_option, err %d aq_err %d\n",
613 status, pf->hw.adminq.sq_last_status);
614 NL_SET_ERR_MSG_MOD(extack, "Port split request failed");
615 return -EIO;
616 }
617
618 status = ice_acquire_nvm(&pf->hw, ICE_RES_WRITE);
619 if (status) {
620 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
621 status, pf->hw.adminq.sq_last_status);
622 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
623 return -EIO;
624 }
625
626 status = ice_nvm_write_activate(&pf->hw, ICE_AQC_NVM_ACTIV_REQ_EMPR, NULL);
627 if (status) {
628 dev_dbg(dev, "ice_nvm_write_activate failed, err %d aq_err %d\n",
629 status, pf->hw.adminq.sq_last_status);
630 NL_SET_ERR_MSG_MOD(extack, "Port split request failed to save data");
631 ice_release_nvm(&pf->hw);
632 return -EIO;
633 }
634
635 ice_release_nvm(&pf->hw);
636
637 NL_SET_ERR_MSG_MOD(extack, "Reboot required to finish port split");
638 return 0;
639 }
640
641 /**
642 * ice_devlink_port_split - .port_split devlink handler
643 * @devlink: devlink instance structure
644 * @port: devlink port structure
645 * @count: number of ports to split to
646 * @extack: extended netdev ack structure
647 *
648 * Callback for the devlink .port_split operation.
649 *
650 * Unfortunately, the devlink expression of available options is limited
651 * to just a number, so search for an FW port option which supports
652 * the specified number. As there could be multiple FW port options with
653 * the same port split count, allow switching between them. When the same
654 * port split count request is issued again, switch to the next FW port
655 * option with the same port split count.
656 *
657 * Return: zero on success or an error code on failure.
658 */
659 static int
ice_devlink_port_split(struct devlink * devlink,struct devlink_port * port,unsigned int count,struct netlink_ext_ack * extack)660 ice_devlink_port_split(struct devlink *devlink, struct devlink_port *port,
661 unsigned int count, struct netlink_ext_ack *extack)
662 {
663 struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
664 u8 i, j, active_idx, pending_idx, new_option;
665 struct ice_pf *pf = devlink_priv(devlink);
666 u8 option_count = ICE_AQC_PORT_OPT_MAX;
667 struct device *dev = ice_pf_to_dev(pf);
668 bool active_valid, pending_valid;
669 int status;
670
671 status = ice_aq_get_port_options(&pf->hw, options, &option_count,
672 0, true, &active_idx, &active_valid,
673 &pending_idx, &pending_valid);
674 if (status) {
675 dev_dbg(dev, "Couldn't read port split options, err = %d\n",
676 status);
677 NL_SET_ERR_MSG_MOD(extack, "Failed to get available port split options");
678 return -EIO;
679 }
680
681 new_option = ICE_AQC_PORT_OPT_MAX;
682 active_idx = pending_valid ? pending_idx : active_idx;
683 for (i = 1; i <= option_count; i++) {
684 /* In order to allow switching between FW port options with
685 * the same port split count, search for a new option starting
686 * from the active/pending option (with array wrap around).
687 */
688 j = (active_idx + i) % option_count;
689
690 if (count == options[j].pmd) {
691 new_option = j;
692 break;
693 }
694 }
695
696 if (new_option == active_idx) {
697 dev_dbg(dev, "request to split: count: %u is already set and there are no other options\n",
698 count);
699 NL_SET_ERR_MSG_MOD(extack, "Requested split count is already set");
700 ice_devlink_port_options_print(pf);
701 return -EINVAL;
702 }
703
704 if (new_option == ICE_AQC_PORT_OPT_MAX) {
705 dev_dbg(dev, "request to split: count: %u not found\n", count);
706 NL_SET_ERR_MSG_MOD(extack, "Port split requested unsupported port config");
707 ice_devlink_port_options_print(pf);
708 return -EINVAL;
709 }
710
711 status = ice_devlink_aq_set_port_option(pf, new_option, extack);
712 if (status)
713 return status;
714
715 ice_devlink_port_options_print(pf);
716
717 return 0;
718 }
719
720 /**
721 * ice_devlink_port_unsplit - .port_unsplit devlink handler
722 * @devlink: devlink instance structure
723 * @port: devlink port structure
724 * @extack: extended netdev ack structure
725 *
726 * Callback for the devlink .port_unsplit operation.
727 * Calls ice_devlink_port_split with split count set to 1.
728 * There could be no FW option available with split count 1.
729 *
730 * Return: zero on success or an error code on failure.
731 */
732 static int
ice_devlink_port_unsplit(struct devlink * devlink,struct devlink_port * port,struct netlink_ext_ack * extack)733 ice_devlink_port_unsplit(struct devlink *devlink, struct devlink_port *port,
734 struct netlink_ext_ack *extack)
735 {
736 return ice_devlink_port_split(devlink, port, 1, extack);
737 }
738
739 /**
740 * ice_tear_down_devlink_rate_tree - removes devlink-rate exported tree
741 * @pf: pf struct
742 *
743 * This function tears down tree exported during VF's creation.
744 */
ice_tear_down_devlink_rate_tree(struct ice_pf * pf)745 void ice_tear_down_devlink_rate_tree(struct ice_pf *pf)
746 {
747 struct devlink *devlink;
748 struct ice_vf *vf;
749 unsigned int bkt;
750
751 devlink = priv_to_devlink(pf);
752
753 devl_lock(devlink);
754 mutex_lock(&pf->vfs.table_lock);
755 ice_for_each_vf(pf, bkt, vf) {
756 if (vf->devlink_port.devlink_rate)
757 devl_rate_leaf_destroy(&vf->devlink_port);
758 }
759 mutex_unlock(&pf->vfs.table_lock);
760
761 devl_rate_nodes_destroy(devlink);
762 devl_unlock(devlink);
763 }
764
765 /**
766 * ice_enable_custom_tx - try to enable custom Tx feature
767 * @pf: pf struct
768 *
769 * This function tries to enable custom Tx feature,
770 * it's not possible to enable it, if DCB or ADQ is active.
771 */
ice_enable_custom_tx(struct ice_pf * pf)772 static bool ice_enable_custom_tx(struct ice_pf *pf)
773 {
774 struct ice_port_info *pi = ice_get_main_vsi(pf)->port_info;
775 struct device *dev = ice_pf_to_dev(pf);
776
777 if (pi->is_custom_tx_enabled)
778 /* already enabled, return true */
779 return true;
780
781 if (ice_is_adq_active(pf)) {
782 dev_err(dev, "ADQ active, can't modify Tx scheduler tree\n");
783 return false;
784 }
785
786 if (ice_is_dcb_active(pf)) {
787 dev_err(dev, "DCB active, can't modify Tx scheduler tree\n");
788 return false;
789 }
790
791 pi->is_custom_tx_enabled = true;
792
793 return true;
794 }
795
796 /**
797 * ice_traverse_tx_tree - traverse Tx scheduler tree
798 * @devlink: devlink struct
799 * @node: current node, used for recursion
800 * @tc_node: tc_node struct, that is treated as a root
801 * @pf: pf struct
802 *
803 * This function traverses Tx scheduler tree and exports
804 * entire structure to the devlink-rate.
805 */
ice_traverse_tx_tree(struct devlink * devlink,struct ice_sched_node * node,struct ice_sched_node * tc_node,struct ice_pf * pf)806 static void ice_traverse_tx_tree(struct devlink *devlink, struct ice_sched_node *node,
807 struct ice_sched_node *tc_node, struct ice_pf *pf)
808 {
809 struct devlink_rate *rate_node = NULL;
810 struct ice_vf *vf;
811 int i;
812
813 if (node->parent == tc_node) {
814 /* create root node */
815 rate_node = devl_rate_node_create(devlink, node, node->name, NULL);
816 } else if (node->vsi_handle &&
817 pf->vsi[node->vsi_handle]->vf) {
818 vf = pf->vsi[node->vsi_handle]->vf;
819 if (!vf->devlink_port.devlink_rate)
820 /* leaf nodes doesn't have children
821 * so we don't set rate_node
822 */
823 devl_rate_leaf_create(&vf->devlink_port, node,
824 node->parent->rate_node);
825 } else if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF &&
826 node->parent->rate_node) {
827 rate_node = devl_rate_node_create(devlink, node, node->name,
828 node->parent->rate_node);
829 }
830
831 if (rate_node && !IS_ERR(rate_node))
832 node->rate_node = rate_node;
833
834 for (i = 0; i < node->num_children; i++)
835 ice_traverse_tx_tree(devlink, node->children[i], tc_node, pf);
836 }
837
838 /**
839 * ice_devlink_rate_init_tx_topology - export Tx scheduler tree to devlink rate
840 * @devlink: devlink struct
841 * @vsi: main vsi struct
842 *
843 * This function finds a root node, then calls ice_traverse_tx tree, which
844 * traverses the tree and exports it's contents to devlink rate.
845 */
ice_devlink_rate_init_tx_topology(struct devlink * devlink,struct ice_vsi * vsi)846 int ice_devlink_rate_init_tx_topology(struct devlink *devlink, struct ice_vsi *vsi)
847 {
848 struct ice_port_info *pi = vsi->port_info;
849 struct ice_sched_node *tc_node;
850 struct ice_pf *pf = vsi->back;
851 int i;
852
853 tc_node = pi->root->children[0];
854 mutex_lock(&pi->sched_lock);
855 devl_lock(devlink);
856 for (i = 0; i < tc_node->num_children; i++)
857 ice_traverse_tx_tree(devlink, tc_node->children[i], tc_node, pf);
858 devl_unlock(devlink);
859 mutex_unlock(&pi->sched_lock);
860
861 return 0;
862 }
863
864 /**
865 * ice_set_object_tx_share - sets node scheduling parameter
866 * @pi: devlink struct instance
867 * @node: node struct instance
868 * @bw: bandwidth in bytes per second
869 * @extack: extended netdev ack structure
870 *
871 * This function sets ICE_MIN_BW scheduling BW limit.
872 */
ice_set_object_tx_share(struct ice_port_info * pi,struct ice_sched_node * node,u64 bw,struct netlink_ext_ack * extack)873 static int ice_set_object_tx_share(struct ice_port_info *pi, struct ice_sched_node *node,
874 u64 bw, struct netlink_ext_ack *extack)
875 {
876 int status;
877
878 mutex_lock(&pi->sched_lock);
879 /* converts bytes per second to kilo bits per second */
880 node->tx_share = div_u64(bw, 125);
881 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW, node->tx_share);
882 mutex_unlock(&pi->sched_lock);
883
884 if (status)
885 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_share");
886
887 return status;
888 }
889
890 /**
891 * ice_set_object_tx_max - sets node scheduling parameter
892 * @pi: devlink struct instance
893 * @node: node struct instance
894 * @bw: bandwidth in bytes per second
895 * @extack: extended netdev ack structure
896 *
897 * This function sets ICE_MAX_BW scheduling BW limit.
898 */
ice_set_object_tx_max(struct ice_port_info * pi,struct ice_sched_node * node,u64 bw,struct netlink_ext_ack * extack)899 static int ice_set_object_tx_max(struct ice_port_info *pi, struct ice_sched_node *node,
900 u64 bw, struct netlink_ext_ack *extack)
901 {
902 int status;
903
904 mutex_lock(&pi->sched_lock);
905 /* converts bytes per second value to kilo bits per second */
906 node->tx_max = div_u64(bw, 125);
907 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW, node->tx_max);
908 mutex_unlock(&pi->sched_lock);
909
910 if (status)
911 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_max");
912
913 return status;
914 }
915
916 /**
917 * ice_set_object_tx_priority - sets node scheduling parameter
918 * @pi: devlink struct instance
919 * @node: node struct instance
920 * @priority: value representing priority for strict priority arbitration
921 * @extack: extended netdev ack structure
922 *
923 * This function sets priority of node among siblings.
924 */
ice_set_object_tx_priority(struct ice_port_info * pi,struct ice_sched_node * node,u32 priority,struct netlink_ext_ack * extack)925 static int ice_set_object_tx_priority(struct ice_port_info *pi, struct ice_sched_node *node,
926 u32 priority, struct netlink_ext_ack *extack)
927 {
928 int status;
929
930 if (priority >= 8) {
931 NL_SET_ERR_MSG_MOD(extack, "Priority should be less than 8");
932 return -EINVAL;
933 }
934
935 mutex_lock(&pi->sched_lock);
936 node->tx_priority = priority;
937 status = ice_sched_set_node_priority(pi, node, node->tx_priority);
938 mutex_unlock(&pi->sched_lock);
939
940 if (status)
941 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_priority");
942
943 return status;
944 }
945
946 /**
947 * ice_set_object_tx_weight - sets node scheduling parameter
948 * @pi: devlink struct instance
949 * @node: node struct instance
950 * @weight: value represeting relative weight for WFQ arbitration
951 * @extack: extended netdev ack structure
952 *
953 * This function sets node weight for WFQ algorithm.
954 */
ice_set_object_tx_weight(struct ice_port_info * pi,struct ice_sched_node * node,u32 weight,struct netlink_ext_ack * extack)955 static int ice_set_object_tx_weight(struct ice_port_info *pi, struct ice_sched_node *node,
956 u32 weight, struct netlink_ext_ack *extack)
957 {
958 int status;
959
960 if (weight > 200 || weight < 1) {
961 NL_SET_ERR_MSG_MOD(extack, "Weight must be between 1 and 200");
962 return -EINVAL;
963 }
964
965 mutex_lock(&pi->sched_lock);
966 node->tx_weight = weight;
967 status = ice_sched_set_node_weight(pi, node, node->tx_weight);
968 mutex_unlock(&pi->sched_lock);
969
970 if (status)
971 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_weight");
972
973 return status;
974 }
975
976 /**
977 * ice_get_pi_from_dev_rate - get port info from devlink_rate
978 * @rate_node: devlink struct instance
979 *
980 * This function returns corresponding port_info struct of devlink_rate
981 */
ice_get_pi_from_dev_rate(struct devlink_rate * rate_node)982 static struct ice_port_info *ice_get_pi_from_dev_rate(struct devlink_rate *rate_node)
983 {
984 struct ice_pf *pf = devlink_priv(rate_node->devlink);
985
986 return ice_get_main_vsi(pf)->port_info;
987 }
988
ice_devlink_rate_node_new(struct devlink_rate * rate_node,void ** priv,struct netlink_ext_ack * extack)989 static int ice_devlink_rate_node_new(struct devlink_rate *rate_node, void **priv,
990 struct netlink_ext_ack *extack)
991 {
992 struct ice_sched_node *node;
993 struct ice_port_info *pi;
994
995 pi = ice_get_pi_from_dev_rate(rate_node);
996
997 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
998 return -EBUSY;
999
1000 /* preallocate memory for ice_sched_node */
1001 node = devm_kzalloc(ice_hw_to_dev(pi->hw), sizeof(*node), GFP_KERNEL);
1002 if (!node)
1003 return -ENOMEM;
1004
1005 *priv = node;
1006
1007 return 0;
1008 }
1009
ice_devlink_rate_node_del(struct devlink_rate * rate_node,void * priv,struct netlink_ext_ack * extack)1010 static int ice_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,
1011 struct netlink_ext_ack *extack)
1012 {
1013 struct ice_sched_node *node, *tc_node;
1014 struct ice_port_info *pi;
1015
1016 pi = ice_get_pi_from_dev_rate(rate_node);
1017 tc_node = pi->root->children[0];
1018 node = priv;
1019
1020 if (!rate_node->parent || !node || tc_node == node || !extack)
1021 return 0;
1022
1023 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1024 return -EBUSY;
1025
1026 /* can't allow to delete a node with children */
1027 if (node->num_children)
1028 return -EINVAL;
1029
1030 mutex_lock(&pi->sched_lock);
1031 ice_free_sched_node(pi, node);
1032 mutex_unlock(&pi->sched_lock);
1033
1034 return 0;
1035 }
1036
ice_devlink_rate_leaf_tx_max_set(struct devlink_rate * rate_leaf,void * priv,u64 tx_max,struct netlink_ext_ack * extack)1037 static int ice_devlink_rate_leaf_tx_max_set(struct devlink_rate *rate_leaf, void *priv,
1038 u64 tx_max, struct netlink_ext_ack *extack)
1039 {
1040 struct ice_sched_node *node = priv;
1041
1042 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1043 return -EBUSY;
1044
1045 if (!node)
1046 return 0;
1047
1048 return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_leaf),
1049 node, tx_max, extack);
1050 }
1051
ice_devlink_rate_leaf_tx_share_set(struct devlink_rate * rate_leaf,void * priv,u64 tx_share,struct netlink_ext_ack * extack)1052 static int ice_devlink_rate_leaf_tx_share_set(struct devlink_rate *rate_leaf, void *priv,
1053 u64 tx_share, struct netlink_ext_ack *extack)
1054 {
1055 struct ice_sched_node *node = priv;
1056
1057 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1058 return -EBUSY;
1059
1060 if (!node)
1061 return 0;
1062
1063 return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_leaf), node,
1064 tx_share, extack);
1065 }
1066
ice_devlink_rate_leaf_tx_priority_set(struct devlink_rate * rate_leaf,void * priv,u32 tx_priority,struct netlink_ext_ack * extack)1067 static int ice_devlink_rate_leaf_tx_priority_set(struct devlink_rate *rate_leaf, void *priv,
1068 u32 tx_priority, struct netlink_ext_ack *extack)
1069 {
1070 struct ice_sched_node *node = priv;
1071
1072 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1073 return -EBUSY;
1074
1075 if (!node)
1076 return 0;
1077
1078 return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_leaf), node,
1079 tx_priority, extack);
1080 }
1081
ice_devlink_rate_leaf_tx_weight_set(struct devlink_rate * rate_leaf,void * priv,u32 tx_weight,struct netlink_ext_ack * extack)1082 static int ice_devlink_rate_leaf_tx_weight_set(struct devlink_rate *rate_leaf, void *priv,
1083 u32 tx_weight, struct netlink_ext_ack *extack)
1084 {
1085 struct ice_sched_node *node = priv;
1086
1087 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1088 return -EBUSY;
1089
1090 if (!node)
1091 return 0;
1092
1093 return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_leaf), node,
1094 tx_weight, extack);
1095 }
1096
ice_devlink_rate_node_tx_max_set(struct devlink_rate * rate_node,void * priv,u64 tx_max,struct netlink_ext_ack * extack)1097 static int ice_devlink_rate_node_tx_max_set(struct devlink_rate *rate_node, void *priv,
1098 u64 tx_max, struct netlink_ext_ack *extack)
1099 {
1100 struct ice_sched_node *node = priv;
1101
1102 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1103 return -EBUSY;
1104
1105 if (!node)
1106 return 0;
1107
1108 return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_node),
1109 node, tx_max, extack);
1110 }
1111
ice_devlink_rate_node_tx_share_set(struct devlink_rate * rate_node,void * priv,u64 tx_share,struct netlink_ext_ack * extack)1112 static int ice_devlink_rate_node_tx_share_set(struct devlink_rate *rate_node, void *priv,
1113 u64 tx_share, struct netlink_ext_ack *extack)
1114 {
1115 struct ice_sched_node *node = priv;
1116
1117 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1118 return -EBUSY;
1119
1120 if (!node)
1121 return 0;
1122
1123 return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_node),
1124 node, tx_share, extack);
1125 }
1126
ice_devlink_rate_node_tx_priority_set(struct devlink_rate * rate_node,void * priv,u32 tx_priority,struct netlink_ext_ack * extack)1127 static int ice_devlink_rate_node_tx_priority_set(struct devlink_rate *rate_node, void *priv,
1128 u32 tx_priority, struct netlink_ext_ack *extack)
1129 {
1130 struct ice_sched_node *node = priv;
1131
1132 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1133 return -EBUSY;
1134
1135 if (!node)
1136 return 0;
1137
1138 return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_node),
1139 node, tx_priority, extack);
1140 }
1141
ice_devlink_rate_node_tx_weight_set(struct devlink_rate * rate_node,void * priv,u32 tx_weight,struct netlink_ext_ack * extack)1142 static int ice_devlink_rate_node_tx_weight_set(struct devlink_rate *rate_node, void *priv,
1143 u32 tx_weight, struct netlink_ext_ack *extack)
1144 {
1145 struct ice_sched_node *node = priv;
1146
1147 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1148 return -EBUSY;
1149
1150 if (!node)
1151 return 0;
1152
1153 return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_node),
1154 node, tx_weight, extack);
1155 }
1156
ice_devlink_set_parent(struct devlink_rate * devlink_rate,struct devlink_rate * parent,void * priv,void * parent_priv,struct netlink_ext_ack * extack)1157 static int ice_devlink_set_parent(struct devlink_rate *devlink_rate,
1158 struct devlink_rate *parent,
1159 void *priv, void *parent_priv,
1160 struct netlink_ext_ack *extack)
1161 {
1162 struct ice_port_info *pi = ice_get_pi_from_dev_rate(devlink_rate);
1163 struct ice_sched_node *tc_node, *node, *parent_node;
1164 u16 num_nodes_added;
1165 u32 first_node_teid;
1166 u32 node_teid;
1167 int status;
1168
1169 tc_node = pi->root->children[0];
1170 node = priv;
1171
1172 if (!extack)
1173 return 0;
1174
1175 if (!ice_enable_custom_tx(devlink_priv(devlink_rate->devlink)))
1176 return -EBUSY;
1177
1178 if (!parent) {
1179 if (!node || tc_node == node || node->num_children)
1180 return -EINVAL;
1181
1182 mutex_lock(&pi->sched_lock);
1183 ice_free_sched_node(pi, node);
1184 mutex_unlock(&pi->sched_lock);
1185
1186 return 0;
1187 }
1188
1189 parent_node = parent_priv;
1190
1191 /* if the node doesn't exist, create it */
1192 if (!node->parent) {
1193 mutex_lock(&pi->sched_lock);
1194 status = ice_sched_add_elems(pi, tc_node, parent_node,
1195 parent_node->tx_sched_layer + 1,
1196 1, &num_nodes_added, &first_node_teid,
1197 &node);
1198 mutex_unlock(&pi->sched_lock);
1199
1200 if (status) {
1201 NL_SET_ERR_MSG_MOD(extack, "Can't add a new node");
1202 return status;
1203 }
1204
1205 if (devlink_rate->tx_share)
1206 ice_set_object_tx_share(pi, node, devlink_rate->tx_share, extack);
1207 if (devlink_rate->tx_max)
1208 ice_set_object_tx_max(pi, node, devlink_rate->tx_max, extack);
1209 if (devlink_rate->tx_priority)
1210 ice_set_object_tx_priority(pi, node, devlink_rate->tx_priority, extack);
1211 if (devlink_rate->tx_weight)
1212 ice_set_object_tx_weight(pi, node, devlink_rate->tx_weight, extack);
1213 } else {
1214 node_teid = le32_to_cpu(node->info.node_teid);
1215 mutex_lock(&pi->sched_lock);
1216 status = ice_sched_move_nodes(pi, parent_node, 1, &node_teid);
1217 mutex_unlock(&pi->sched_lock);
1218
1219 if (status)
1220 NL_SET_ERR_MSG_MOD(extack, "Can't move existing node to a new parent");
1221 }
1222
1223 return status;
1224 }
1225
1226 /**
1227 * ice_devlink_reload_up - do reload up after reinit
1228 * @devlink: pointer to the devlink instance reloading
1229 * @action: the action requested
1230 * @limit: limits imposed by userspace, such as not resetting
1231 * @actions_performed: on return, indicate what actions actually performed
1232 * @extack: netlink extended ACK structure
1233 */
1234 static int
ice_devlink_reload_up(struct devlink * devlink,enum devlink_reload_action action,enum devlink_reload_limit limit,u32 * actions_performed,struct netlink_ext_ack * extack)1235 ice_devlink_reload_up(struct devlink *devlink,
1236 enum devlink_reload_action action,
1237 enum devlink_reload_limit limit,
1238 u32 *actions_performed,
1239 struct netlink_ext_ack *extack)
1240 {
1241 struct ice_pf *pf = devlink_priv(devlink);
1242
1243 switch (action) {
1244 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
1245 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT);
1246 return ice_load(pf);
1247 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
1248 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
1249 return ice_devlink_reload_empr_finish(pf, extack);
1250 default:
1251 WARN_ON(1);
1252 return -EOPNOTSUPP;
1253 }
1254 }
1255
1256 static const struct devlink_ops ice_devlink_ops = {
1257 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
1258 .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) |
1259 BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
1260 .reload_down = ice_devlink_reload_down,
1261 .reload_up = ice_devlink_reload_up,
1262 .eswitch_mode_get = ice_eswitch_mode_get,
1263 .eswitch_mode_set = ice_eswitch_mode_set,
1264 .info_get = ice_devlink_info_get,
1265 .flash_update = ice_devlink_flash_update,
1266
1267 .rate_node_new = ice_devlink_rate_node_new,
1268 .rate_node_del = ice_devlink_rate_node_del,
1269
1270 .rate_leaf_tx_max_set = ice_devlink_rate_leaf_tx_max_set,
1271 .rate_leaf_tx_share_set = ice_devlink_rate_leaf_tx_share_set,
1272 .rate_leaf_tx_priority_set = ice_devlink_rate_leaf_tx_priority_set,
1273 .rate_leaf_tx_weight_set = ice_devlink_rate_leaf_tx_weight_set,
1274
1275 .rate_node_tx_max_set = ice_devlink_rate_node_tx_max_set,
1276 .rate_node_tx_share_set = ice_devlink_rate_node_tx_share_set,
1277 .rate_node_tx_priority_set = ice_devlink_rate_node_tx_priority_set,
1278 .rate_node_tx_weight_set = ice_devlink_rate_node_tx_weight_set,
1279
1280 .rate_leaf_parent_set = ice_devlink_set_parent,
1281 .rate_node_parent_set = ice_devlink_set_parent,
1282 };
1283
1284 static int
ice_devlink_enable_roce_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)1285 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
1286 struct devlink_param_gset_ctx *ctx)
1287 {
1288 struct ice_pf *pf = devlink_priv(devlink);
1289
1290 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
1291
1292 return 0;
1293 }
1294
1295 static int
ice_devlink_enable_roce_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)1296 ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
1297 struct devlink_param_gset_ctx *ctx)
1298 {
1299 struct ice_pf *pf = devlink_priv(devlink);
1300 bool roce_ena = ctx->val.vbool;
1301 int ret;
1302
1303 if (!roce_ena) {
1304 ice_unplug_aux_dev(pf);
1305 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1306 return 0;
1307 }
1308
1309 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
1310 ret = ice_plug_aux_dev(pf);
1311 if (ret)
1312 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
1313
1314 return ret;
1315 }
1316
1317 static int
ice_devlink_enable_roce_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)1318 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
1319 union devlink_param_value val,
1320 struct netlink_ext_ack *extack)
1321 {
1322 struct ice_pf *pf = devlink_priv(devlink);
1323
1324 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1325 return -EOPNOTSUPP;
1326
1327 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
1328 NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1329 return -EOPNOTSUPP;
1330 }
1331
1332 return 0;
1333 }
1334
1335 static int
ice_devlink_enable_iw_get(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)1336 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
1337 struct devlink_param_gset_ctx *ctx)
1338 {
1339 struct ice_pf *pf = devlink_priv(devlink);
1340
1341 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
1342
1343 return 0;
1344 }
1345
1346 static int
ice_devlink_enable_iw_set(struct devlink * devlink,u32 id,struct devlink_param_gset_ctx * ctx)1347 ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
1348 struct devlink_param_gset_ctx *ctx)
1349 {
1350 struct ice_pf *pf = devlink_priv(devlink);
1351 bool iw_ena = ctx->val.vbool;
1352 int ret;
1353
1354 if (!iw_ena) {
1355 ice_unplug_aux_dev(pf);
1356 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1357 return 0;
1358 }
1359
1360 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
1361 ret = ice_plug_aux_dev(pf);
1362 if (ret)
1363 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
1364
1365 return ret;
1366 }
1367
1368 static int
ice_devlink_enable_iw_validate(struct devlink * devlink,u32 id,union devlink_param_value val,struct netlink_ext_ack * extack)1369 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
1370 union devlink_param_value val,
1371 struct netlink_ext_ack *extack)
1372 {
1373 struct ice_pf *pf = devlink_priv(devlink);
1374
1375 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
1376 return -EOPNOTSUPP;
1377
1378 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
1379 NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
1380 return -EOPNOTSUPP;
1381 }
1382
1383 return 0;
1384 }
1385
1386 static const struct devlink_param ice_devlink_params[] = {
1387 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1388 ice_devlink_enable_roce_get,
1389 ice_devlink_enable_roce_set,
1390 ice_devlink_enable_roce_validate),
1391 DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1392 ice_devlink_enable_iw_get,
1393 ice_devlink_enable_iw_set,
1394 ice_devlink_enable_iw_validate),
1395
1396 };
1397
ice_devlink_free(void * devlink_ptr)1398 static void ice_devlink_free(void *devlink_ptr)
1399 {
1400 devlink_free((struct devlink *)devlink_ptr);
1401 }
1402
1403 /**
1404 * ice_allocate_pf - Allocate devlink and return PF structure pointer
1405 * @dev: the device to allocate for
1406 *
1407 * Allocate a devlink instance for this device and return the private area as
1408 * the PF structure. The devlink memory is kept track of through devres by
1409 * adding an action to remove it when unwinding.
1410 */
ice_allocate_pf(struct device * dev)1411 struct ice_pf *ice_allocate_pf(struct device *dev)
1412 {
1413 struct devlink *devlink;
1414
1415 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
1416 if (!devlink)
1417 return NULL;
1418
1419 /* Add an action to teardown the devlink when unwinding the driver */
1420 if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
1421 return NULL;
1422
1423 return devlink_priv(devlink);
1424 }
1425
1426 /**
1427 * ice_devlink_register - Register devlink interface for this PF
1428 * @pf: the PF to register the devlink for.
1429 *
1430 * Register the devlink instance associated with this physical function.
1431 *
1432 * Return: zero on success or an error code on failure.
1433 */
ice_devlink_register(struct ice_pf * pf)1434 void ice_devlink_register(struct ice_pf *pf)
1435 {
1436 struct devlink *devlink = priv_to_devlink(pf);
1437
1438 devlink_register(devlink);
1439 }
1440
1441 /**
1442 * ice_devlink_unregister - Unregister devlink resources for this PF.
1443 * @pf: the PF structure to cleanup
1444 *
1445 * Releases resources used by devlink and cleans up associated memory.
1446 */
ice_devlink_unregister(struct ice_pf * pf)1447 void ice_devlink_unregister(struct ice_pf *pf)
1448 {
1449 devlink_unregister(priv_to_devlink(pf));
1450 }
1451
1452 /**
1453 * ice_devlink_set_switch_id - Set unique switch id based on pci dsn
1454 * @pf: the PF to create a devlink port for
1455 * @ppid: struct with switch id information
1456 */
1457 static void
ice_devlink_set_switch_id(struct ice_pf * pf,struct netdev_phys_item_id * ppid)1458 ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid)
1459 {
1460 struct pci_dev *pdev = pf->pdev;
1461 u64 id;
1462
1463 id = pci_get_dsn(pdev);
1464
1465 ppid->id_len = sizeof(id);
1466 put_unaligned_be64(id, &ppid->id);
1467 }
1468
ice_devlink_register_params(struct ice_pf * pf)1469 int ice_devlink_register_params(struct ice_pf *pf)
1470 {
1471 struct devlink *devlink = priv_to_devlink(pf);
1472
1473 return devlink_params_register(devlink, ice_devlink_params,
1474 ARRAY_SIZE(ice_devlink_params));
1475 }
1476
ice_devlink_unregister_params(struct ice_pf * pf)1477 void ice_devlink_unregister_params(struct ice_pf *pf)
1478 {
1479 devlink_params_unregister(priv_to_devlink(pf), ice_devlink_params,
1480 ARRAY_SIZE(ice_devlink_params));
1481 }
1482
1483 /**
1484 * ice_devlink_set_port_split_options - Set port split options
1485 * @pf: the PF to set port split options
1486 * @attrs: devlink attributes
1487 *
1488 * Sets devlink port split options based on available FW port options
1489 */
1490 static void
ice_devlink_set_port_split_options(struct ice_pf * pf,struct devlink_port_attrs * attrs)1491 ice_devlink_set_port_split_options(struct ice_pf *pf,
1492 struct devlink_port_attrs *attrs)
1493 {
1494 struct ice_aqc_get_port_options_elem options[ICE_AQC_PORT_OPT_MAX];
1495 u8 i, active_idx, pending_idx, option_count = ICE_AQC_PORT_OPT_MAX;
1496 bool active_valid, pending_valid;
1497 int status;
1498
1499 status = ice_aq_get_port_options(&pf->hw, options, &option_count,
1500 0, true, &active_idx, &active_valid,
1501 &pending_idx, &pending_valid);
1502 if (status) {
1503 dev_dbg(ice_pf_to_dev(pf), "Couldn't read port split options, err = %d\n",
1504 status);
1505 return;
1506 }
1507
1508 /* find the biggest available port split count */
1509 for (i = 0; i < option_count; i++)
1510 attrs->lanes = max_t(int, attrs->lanes, options[i].pmd);
1511
1512 attrs->splittable = attrs->lanes ? 1 : 0;
1513 ice_active_port_option = active_idx;
1514 }
1515
1516 static const struct devlink_port_ops ice_devlink_port_ops = {
1517 .port_split = ice_devlink_port_split,
1518 .port_unsplit = ice_devlink_port_unsplit,
1519 };
1520
1521 /**
1522 * ice_devlink_create_pf_port - Create a devlink port for this PF
1523 * @pf: the PF to create a devlink port for
1524 *
1525 * Create and register a devlink_port for this PF.
1526 *
1527 * Return: zero on success or an error code on failure.
1528 */
ice_devlink_create_pf_port(struct ice_pf * pf)1529 int ice_devlink_create_pf_port(struct ice_pf *pf)
1530 {
1531 struct devlink_port_attrs attrs = {};
1532 struct devlink_port *devlink_port;
1533 struct devlink *devlink;
1534 struct ice_vsi *vsi;
1535 struct device *dev;
1536 int err;
1537
1538 dev = ice_pf_to_dev(pf);
1539
1540 devlink_port = &pf->devlink_port;
1541
1542 vsi = ice_get_main_vsi(pf);
1543 if (!vsi)
1544 return -EIO;
1545
1546 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
1547 attrs.phys.port_number = pf->hw.bus.func;
1548
1549 /* As FW supports only port split options for whole device,
1550 * set port split options only for first PF.
1551 */
1552 if (pf->hw.pf_id == 0)
1553 ice_devlink_set_port_split_options(pf, &attrs);
1554
1555 ice_devlink_set_switch_id(pf, &attrs.switch_id);
1556
1557 devlink_port_attrs_set(devlink_port, &attrs);
1558 devlink = priv_to_devlink(pf);
1559
1560 err = devlink_port_register_with_ops(devlink, devlink_port, vsi->idx,
1561 &ice_devlink_port_ops);
1562 if (err) {
1563 dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
1564 pf->hw.pf_id, err);
1565 return err;
1566 }
1567
1568 return 0;
1569 }
1570
1571 /**
1572 * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
1573 * @pf: the PF to cleanup
1574 *
1575 * Unregisters the devlink_port structure associated with this PF.
1576 */
ice_devlink_destroy_pf_port(struct ice_pf * pf)1577 void ice_devlink_destroy_pf_port(struct ice_pf *pf)
1578 {
1579 devlink_port_unregister(&pf->devlink_port);
1580 }
1581
1582 /**
1583 * ice_devlink_create_vf_port - Create a devlink port for this VF
1584 * @vf: the VF to create a port for
1585 *
1586 * Create and register a devlink_port for this VF.
1587 *
1588 * Return: zero on success or an error code on failure.
1589 */
ice_devlink_create_vf_port(struct ice_vf * vf)1590 int ice_devlink_create_vf_port(struct ice_vf *vf)
1591 {
1592 struct devlink_port_attrs attrs = {};
1593 struct devlink_port *devlink_port;
1594 struct devlink *devlink;
1595 struct ice_vsi *vsi;
1596 struct device *dev;
1597 struct ice_pf *pf;
1598 int err;
1599
1600 pf = vf->pf;
1601 dev = ice_pf_to_dev(pf);
1602 devlink_port = &vf->devlink_port;
1603
1604 vsi = ice_get_vf_vsi(vf);
1605 if (!vsi)
1606 return -EINVAL;
1607
1608 attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
1609 attrs.pci_vf.pf = pf->hw.bus.func;
1610 attrs.pci_vf.vf = vf->vf_id;
1611
1612 ice_devlink_set_switch_id(pf, &attrs.switch_id);
1613
1614 devlink_port_attrs_set(devlink_port, &attrs);
1615 devlink = priv_to_devlink(pf);
1616
1617 err = devlink_port_register(devlink, devlink_port, vsi->idx);
1618 if (err) {
1619 dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
1620 vf->vf_id, err);
1621 return err;
1622 }
1623
1624 return 0;
1625 }
1626
1627 /**
1628 * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
1629 * @vf: the VF to cleanup
1630 *
1631 * Unregisters the devlink_port structure associated with this VF.
1632 */
ice_devlink_destroy_vf_port(struct ice_vf * vf)1633 void ice_devlink_destroy_vf_port(struct ice_vf *vf)
1634 {
1635 devl_rate_leaf_destroy(&vf->devlink_port);
1636 devlink_port_unregister(&vf->devlink_port);
1637 }
1638
1639 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
1640
1641 static const struct devlink_region_ops ice_nvm_region_ops;
1642 static const struct devlink_region_ops ice_sram_region_ops;
1643
1644 /**
1645 * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
1646 * @devlink: the devlink instance
1647 * @ops: the devlink region to snapshot
1648 * @extack: extended ACK response structure
1649 * @data: on exit points to snapshot data buffer
1650 *
1651 * This function is called in response to a DEVLINK_CMD_REGION_NEW for either
1652 * the nvm-flash or shadow-ram region.
1653 *
1654 * It captures a snapshot of the NVM or Shadow RAM flash contents. This
1655 * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink
1656 * interface.
1657 *
1658 * @returns zero on success, and updates the data pointer. Returns a non-zero
1659 * error code on failure.
1660 */
ice_devlink_nvm_snapshot(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)1661 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
1662 const struct devlink_region_ops *ops,
1663 struct netlink_ext_ack *extack, u8 **data)
1664 {
1665 struct ice_pf *pf = devlink_priv(devlink);
1666 struct device *dev = ice_pf_to_dev(pf);
1667 struct ice_hw *hw = &pf->hw;
1668 bool read_shadow_ram;
1669 u8 *nvm_data, *tmp, i;
1670 u32 nvm_size, left;
1671 s8 num_blks;
1672 int status;
1673
1674 if (ops == &ice_nvm_region_ops) {
1675 read_shadow_ram = false;
1676 nvm_size = hw->flash.flash_size;
1677 } else if (ops == &ice_sram_region_ops) {
1678 read_shadow_ram = true;
1679 nvm_size = hw->flash.sr_words * 2u;
1680 } else {
1681 NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1682 return -EOPNOTSUPP;
1683 }
1684
1685 nvm_data = vzalloc(nvm_size);
1686 if (!nvm_data)
1687 return -ENOMEM;
1688
1689 num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
1690 tmp = nvm_data;
1691 left = nvm_size;
1692
1693 /* Some systems take longer to read the NVM than others which causes the
1694 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
1695 * this by breaking the reads of the NVM into smaller chunks that will
1696 * probably not take as long. This has some overhead since we are
1697 * increasing the number of AQ commands, but it should always work
1698 */
1699 for (i = 0; i < num_blks; i++) {
1700 u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
1701
1702 status = ice_acquire_nvm(hw, ICE_RES_READ);
1703 if (status) {
1704 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1705 status, hw->adminq.sq_last_status);
1706 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1707 vfree(nvm_data);
1708 return -EIO;
1709 }
1710
1711 status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
1712 &read_sz, tmp, read_shadow_ram);
1713 if (status) {
1714 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1715 read_sz, status, hw->adminq.sq_last_status);
1716 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1717 ice_release_nvm(hw);
1718 vfree(nvm_data);
1719 return -EIO;
1720 }
1721 ice_release_nvm(hw);
1722
1723 tmp += read_sz;
1724 left -= read_sz;
1725 }
1726
1727 *data = nvm_data;
1728
1729 return 0;
1730 }
1731
1732 /**
1733 * ice_devlink_nvm_read - Read a portion of NVM flash contents
1734 * @devlink: the devlink instance
1735 * @ops: the devlink region to snapshot
1736 * @extack: extended ACK response structure
1737 * @offset: the offset to start at
1738 * @size: the amount to read
1739 * @data: the data buffer to read into
1740 *
1741 * This function is called in response to DEVLINK_CMD_REGION_READ to directly
1742 * read a section of the NVM contents.
1743 *
1744 * It reads from either the nvm-flash or shadow-ram region contents.
1745 *
1746 * @returns zero on success, and updates the data pointer. Returns a non-zero
1747 * error code on failure.
1748 */
ice_devlink_nvm_read(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u64 offset,u32 size,u8 * data)1749 static int ice_devlink_nvm_read(struct devlink *devlink,
1750 const struct devlink_region_ops *ops,
1751 struct netlink_ext_ack *extack,
1752 u64 offset, u32 size, u8 *data)
1753 {
1754 struct ice_pf *pf = devlink_priv(devlink);
1755 struct device *dev = ice_pf_to_dev(pf);
1756 struct ice_hw *hw = &pf->hw;
1757 bool read_shadow_ram;
1758 u64 nvm_size;
1759 int status;
1760
1761 if (ops == &ice_nvm_region_ops) {
1762 read_shadow_ram = false;
1763 nvm_size = hw->flash.flash_size;
1764 } else if (ops == &ice_sram_region_ops) {
1765 read_shadow_ram = true;
1766 nvm_size = hw->flash.sr_words * 2u;
1767 } else {
1768 NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1769 return -EOPNOTSUPP;
1770 }
1771
1772 if (offset + size >= nvm_size) {
1773 NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size");
1774 return -ERANGE;
1775 }
1776
1777 status = ice_acquire_nvm(hw, ICE_RES_READ);
1778 if (status) {
1779 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1780 status, hw->adminq.sq_last_status);
1781 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1782 return -EIO;
1783 }
1784
1785 status = ice_read_flat_nvm(hw, (u32)offset, &size, data,
1786 read_shadow_ram);
1787 if (status) {
1788 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1789 size, status, hw->adminq.sq_last_status);
1790 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1791 ice_release_nvm(hw);
1792 return -EIO;
1793 }
1794 ice_release_nvm(hw);
1795
1796 return 0;
1797 }
1798
1799 /**
1800 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
1801 * @devlink: the devlink instance
1802 * @ops: the devlink region being snapshotted
1803 * @extack: extended ACK response structure
1804 * @data: on exit points to snapshot data buffer
1805 *
1806 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1807 * the device-caps devlink region. It captures a snapshot of the device
1808 * capabilities reported by firmware.
1809 *
1810 * @returns zero on success, and updates the data pointer. Returns a non-zero
1811 * error code on failure.
1812 */
1813 static int
ice_devlink_devcaps_snapshot(struct devlink * devlink,const struct devlink_region_ops * ops,struct netlink_ext_ack * extack,u8 ** data)1814 ice_devlink_devcaps_snapshot(struct devlink *devlink,
1815 const struct devlink_region_ops *ops,
1816 struct netlink_ext_ack *extack, u8 **data)
1817 {
1818 struct ice_pf *pf = devlink_priv(devlink);
1819 struct device *dev = ice_pf_to_dev(pf);
1820 struct ice_hw *hw = &pf->hw;
1821 void *devcaps;
1822 int status;
1823
1824 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
1825 if (!devcaps)
1826 return -ENOMEM;
1827
1828 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
1829 ice_aqc_opc_list_dev_caps, NULL);
1830 if (status) {
1831 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
1832 status, hw->adminq.sq_last_status);
1833 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
1834 vfree(devcaps);
1835 return status;
1836 }
1837
1838 *data = (u8 *)devcaps;
1839
1840 return 0;
1841 }
1842
1843 static const struct devlink_region_ops ice_nvm_region_ops = {
1844 .name = "nvm-flash",
1845 .destructor = vfree,
1846 .snapshot = ice_devlink_nvm_snapshot,
1847 .read = ice_devlink_nvm_read,
1848 };
1849
1850 static const struct devlink_region_ops ice_sram_region_ops = {
1851 .name = "shadow-ram",
1852 .destructor = vfree,
1853 .snapshot = ice_devlink_nvm_snapshot,
1854 .read = ice_devlink_nvm_read,
1855 };
1856
1857 static const struct devlink_region_ops ice_devcaps_region_ops = {
1858 .name = "device-caps",
1859 .destructor = vfree,
1860 .snapshot = ice_devlink_devcaps_snapshot,
1861 };
1862
1863 /**
1864 * ice_devlink_init_regions - Initialize devlink regions
1865 * @pf: the PF device structure
1866 *
1867 * Create devlink regions used to enable access to dump the contents of the
1868 * flash memory on the device.
1869 */
ice_devlink_init_regions(struct ice_pf * pf)1870 void ice_devlink_init_regions(struct ice_pf *pf)
1871 {
1872 struct devlink *devlink = priv_to_devlink(pf);
1873 struct device *dev = ice_pf_to_dev(pf);
1874 u64 nvm_size, sram_size;
1875
1876 nvm_size = pf->hw.flash.flash_size;
1877 pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
1878 nvm_size);
1879 if (IS_ERR(pf->nvm_region)) {
1880 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1881 PTR_ERR(pf->nvm_region));
1882 pf->nvm_region = NULL;
1883 }
1884
1885 sram_size = pf->hw.flash.sr_words * 2u;
1886 pf->sram_region = devlink_region_create(devlink, &ice_sram_region_ops,
1887 1, sram_size);
1888 if (IS_ERR(pf->sram_region)) {
1889 dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1890 PTR_ERR(pf->sram_region));
1891 pf->sram_region = NULL;
1892 }
1893
1894 pf->devcaps_region = devlink_region_create(devlink,
1895 &ice_devcaps_region_ops, 10,
1896 ICE_AQ_MAX_BUF_LEN);
1897 if (IS_ERR(pf->devcaps_region)) {
1898 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1899 PTR_ERR(pf->devcaps_region));
1900 pf->devcaps_region = NULL;
1901 }
1902 }
1903
1904 /**
1905 * ice_devlink_destroy_regions - Destroy devlink regions
1906 * @pf: the PF device structure
1907 *
1908 * Remove previously created regions for this PF.
1909 */
ice_devlink_destroy_regions(struct ice_pf * pf)1910 void ice_devlink_destroy_regions(struct ice_pf *pf)
1911 {
1912 if (pf->nvm_region)
1913 devlink_region_destroy(pf->nvm_region);
1914
1915 if (pf->sram_region)
1916 devlink_region_destroy(pf->sram_region);
1917
1918 if (pf->devcaps_region)
1919 devlink_region_destroy(pf->devcaps_region);
1920 }
1921