Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
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 "devlink.h"
9#include "ice_eswitch.h"
10#include "ice_fw_update.h"
11#include "ice_dcb_lib.h"
12
13/* context for devlink info version reporting */
14struct ice_info_ctx {
15 char buf[128];
16 struct ice_orom_info pending_orom;
17 struct ice_nvm_info pending_nvm;
18 struct ice_netlist_info pending_netlist;
19 struct ice_hw_dev_caps dev_caps;
20};
21
22/* The following functions are used to format specific strings for various
23 * devlink info versions. The ctx parameter is used to provide the storage
24 * buffer, as well as any ancillary information calculated when the info
25 * request was made.
26 *
27 * If a version does not exist, for example when attempting to get the
28 * inactive version of flash when there is no pending update, the function
29 * should leave the buffer in the ctx structure empty.
30 */
31
32static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
33{
34 u8 dsn[8];
35
36 /* Copy the DSN into an array in Big Endian format */
37 put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
38
39 snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
40}
41
42static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
43{
44 struct ice_hw *hw = &pf->hw;
45 int status;
46
47 status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
48 if (status)
49 /* We failed to locate the PBA, so just skip this entry */
50 dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %d\n",
51 status);
52}
53
54static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
55{
56 struct ice_hw *hw = &pf->hw;
57
58 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
59 hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
60}
61
62static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
63{
64 struct ice_hw *hw = &pf->hw;
65
66 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
67 hw->api_min_ver, hw->api_patch);
68}
69
70static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
71{
72 struct ice_hw *hw = &pf->hw;
73
74 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
75}
76
77static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
78{
79 struct ice_orom_info *orom = &pf->hw.flash.orom;
80
81 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
82 orom->major, orom->build, orom->patch);
83}
84
85static void
86ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
87 struct ice_info_ctx *ctx)
88{
89 struct ice_orom_info *orom = &ctx->pending_orom;
90
91 if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
92 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
93 orom->major, orom->build, orom->patch);
94}
95
96static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
97{
98 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
99
100 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
101}
102
103static void
104ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
105 struct ice_info_ctx *ctx)
106{
107 struct ice_nvm_info *nvm = &ctx->pending_nvm;
108
109 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
110 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
111 nvm->major, nvm->minor);
112}
113
114static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
115{
116 struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
117
118 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
119}
120
121static void
122ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
123{
124 struct ice_nvm_info *nvm = &ctx->pending_nvm;
125
126 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
127 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
128}
129
130static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
131{
132 struct ice_hw *hw = &pf->hw;
133
134 snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
135}
136
137static void
138ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
139{
140 struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
141
142 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
143 pkg->major, pkg->minor, pkg->update, pkg->draft);
144}
145
146static void
147ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
148{
149 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
150}
151
152static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
153{
154 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
155
156 /* The netlist version fields are BCD formatted */
157 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
158 netlist->major, netlist->minor,
159 netlist->type >> 16, netlist->type & 0xFFFF,
160 netlist->rev, netlist->cust_ver);
161}
162
163static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
164{
165 struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
166
167 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
168}
169
170static void
171ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
172 struct ice_info_ctx *ctx)
173{
174 struct ice_netlist_info *netlist = &ctx->pending_netlist;
175
176 /* The netlist version fields are BCD formatted */
177 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
178 snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
179 netlist->major, netlist->minor,
180 netlist->type >> 16, netlist->type & 0xFFFF,
181 netlist->rev, netlist->cust_ver);
182}
183
184static void
185ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
186 struct ice_info_ctx *ctx)
187{
188 struct ice_netlist_info *netlist = &ctx->pending_netlist;
189
190 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
191 snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
192}
193
194static void ice_info_cgu_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
195{
196 u32 id, cfg_ver, fw_ver;
197
198 if (!ice_is_feature_supported(pf, ICE_F_CGU))
199 return;
200 if (ice_aq_get_cgu_info(&pf->hw, &id, &cfg_ver, &fw_ver))
201 return;
202 snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", id, cfg_ver, fw_ver);
203}
204
205static void ice_info_cgu_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
206{
207 if (!ice_is_feature_supported(pf, ICE_F_CGU))
208 return;
209 snprintf(ctx->buf, sizeof(ctx->buf), "%u", pf->hw.cgu_part_number);
210}
211
212#define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
213#define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
214#define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
215
216/* The combined() macro inserts both the running entry as well as a stored
217 * entry. The running entry will always report the version from the active
218 * handler. The stored entry will first try the pending handler, and fallback
219 * to the active handler if the pending function does not report a version.
220 * The pending handler should check the status of a pending update for the
221 * relevant flash component. It should only fill in the buffer in the case
222 * where a valid pending version is available. This ensures that the related
223 * stored and running versions remain in sync, and that stored versions are
224 * correctly reported as expected.
225 */
226#define combined(key, active, pending) \
227 running(key, active), \
228 stored(key, pending, active)
229
230enum ice_version_type {
231 ICE_VERSION_FIXED,
232 ICE_VERSION_RUNNING,
233 ICE_VERSION_STORED,
234};
235
236static const struct ice_devlink_version {
237 enum ice_version_type type;
238 const char *key;
239 void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
240 void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
241} ice_devlink_versions[] = {
242 fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
243 running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
244 running("fw.mgmt.api", ice_info_fw_api),
245 running("fw.mgmt.build", ice_info_fw_build),
246 combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
247 combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
248 combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
249 running("fw.app.name", ice_info_ddp_pkg_name),
250 running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
251 running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
252 combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
253 combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
254 fixed("cgu.id", ice_info_cgu_id),
255 running("fw.cgu", ice_info_cgu_fw_build),
256};
257
258/**
259 * ice_devlink_info_get - .info_get devlink handler
260 * @devlink: devlink instance structure
261 * @req: the devlink info request
262 * @extack: extended netdev ack structure
263 *
264 * Callback for the devlink .info_get operation. Reports information about the
265 * device.
266 *
267 * Return: zero on success or an error code on failure.
268 */
269static int ice_devlink_info_get(struct devlink *devlink,
270 struct devlink_info_req *req,
271 struct netlink_ext_ack *extack)
272{
273 struct ice_pf *pf = devlink_priv(devlink);
274 struct device *dev = ice_pf_to_dev(pf);
275 struct ice_hw *hw = &pf->hw;
276 struct ice_info_ctx *ctx;
277 size_t i;
278 int err;
279
280 err = ice_wait_for_reset(pf, 10 * HZ);
281 if (err) {
282 NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
283 return err;
284 }
285
286 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
287 if (!ctx)
288 return -ENOMEM;
289
290 /* discover capabilities first */
291 err = ice_discover_dev_caps(hw, &ctx->dev_caps);
292 if (err) {
293 dev_dbg(dev, "Failed to discover device capabilities, status %d aq_err %s\n",
294 err, ice_aq_str(hw->adminq.sq_last_status));
295 NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
296 goto out_free_ctx;
297 }
298
299 if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
300 err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
301 if (err) {
302 dev_dbg(dev, "Unable to read inactive Option ROM version data, status %d aq_err %s\n",
303 err, ice_aq_str(hw->adminq.sq_last_status));
304
305 /* disable display of pending Option ROM */
306 ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
307 }
308 }
309
310 if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
311 err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
312 if (err) {
313 dev_dbg(dev, "Unable to read inactive NVM version data, status %d aq_err %s\n",
314 err, ice_aq_str(hw->adminq.sq_last_status));
315
316 /* disable display of pending Option ROM */
317 ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
318 }
319 }
320
321 if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
322 err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
323 if (err) {
324 dev_dbg(dev, "Unable to read inactive Netlist version data, status %d aq_err %s\n",
325 err, ice_aq_str(hw->adminq.sq_last_status));
326
327 /* disable display of pending Option ROM */
328 ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
329 }
330 }
331
332 ice_info_get_dsn(pf, ctx);
333
334 err = devlink_info_serial_number_put(req, ctx->buf);
335 if (err) {
336 NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
337 goto out_free_ctx;
338 }
339
340 for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
341 enum ice_version_type type = ice_devlink_versions[i].type;
342 const char *key = ice_devlink_versions[i].key;
343
344 memset(ctx->buf, 0, sizeof(ctx->buf));
345
346 ice_devlink_versions[i].getter(pf, ctx);
347
348 /* If the default getter doesn't report a version, use the
349 * fallback function. This is primarily useful in the case of
350 * "stored" versions that want to report the same value as the
351 * running version in the normal case of no pending update.
352 */
353 if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
354 ice_devlink_versions[i].fallback(pf, ctx);
355
356 /* Do not report missing versions */
357 if (ctx->buf[0] == '\0')
358 continue;
359
360 switch (type) {
361 case ICE_VERSION_FIXED:
362 err = devlink_info_version_fixed_put(req, key, ctx->buf);
363 if (err) {
364 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
365 goto out_free_ctx;
366 }
367 break;
368 case ICE_VERSION_RUNNING:
369 err = devlink_info_version_running_put(req, key, ctx->buf);
370 if (err) {
371 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
372 goto out_free_ctx;
373 }
374 break;
375 case ICE_VERSION_STORED:
376 err = devlink_info_version_stored_put(req, key, ctx->buf);
377 if (err) {
378 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
379 goto out_free_ctx;
380 }
381 break;
382 }
383 }
384
385out_free_ctx:
386 kfree(ctx);
387 return err;
388}
389
390/**
391 * ice_devlink_reload_empr_start - Start EMP reset to activate new firmware
392 * @pf: pointer to the pf instance
393 * @extack: netlink extended ACK structure
394 *
395 * Allow user to activate new Embedded Management Processor firmware by
396 * issuing device specific EMP reset. Called in response to
397 * a DEVLINK_CMD_RELOAD with the DEVLINK_RELOAD_ACTION_FW_ACTIVATE.
398 *
399 * Note that teardown and rebuild of the driver state happens automatically as
400 * part of an interrupt and watchdog task. This is because all physical
401 * functions on the device must be able to reset when an EMP reset occurs from
402 * any source.
403 */
404static int
405ice_devlink_reload_empr_start(struct ice_pf *pf,
406 struct netlink_ext_ack *extack)
407{
408 struct device *dev = ice_pf_to_dev(pf);
409 struct ice_hw *hw = &pf->hw;
410 u8 pending;
411 int err;
412
413 err = ice_get_pending_updates(pf, &pending, extack);
414 if (err)
415 return err;
416
417 /* pending is a bitmask of which flash banks have a pending update,
418 * including the main NVM bank, the Option ROM bank, and the netlist
419 * bank. If any of these bits are set, then there is a pending update
420 * waiting to be activated.
421 */
422 if (!pending) {
423 NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
424 return -ECANCELED;
425 }
426
427 if (pf->fw_emp_reset_disabled) {
428 NL_SET_ERR_MSG_MOD(extack, "EMP reset is not available. To activate firmware, a reboot or power cycle is needed");
429 return -ECANCELED;
430 }
431
432 dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
433
434 err = ice_aq_nvm_update_empr(hw);
435 if (err) {
436 dev_err(dev, "Failed to trigger EMP device reset to reload firmware, err %d aq_err %s\n",
437 err, ice_aq_str(hw->adminq.sq_last_status));
438 NL_SET_ERR_MSG_MOD(extack, "Failed to trigger EMP device reset to reload firmware");
439 return err;
440 }
441
442 return 0;
443}
444
445/**
446 * ice_devlink_reinit_down - unload given PF
447 * @pf: pointer to the PF struct
448 */
449static void ice_devlink_reinit_down(struct ice_pf *pf)
450{
451 /* No need to take devl_lock, it's already taken by devlink API */
452 ice_unload(pf);
453 rtnl_lock();
454 ice_vsi_decfg(ice_get_main_vsi(pf));
455 rtnl_unlock();
456 ice_deinit_dev(pf);
457}
458
459/**
460 * ice_devlink_reload_down - prepare for reload
461 * @devlink: pointer to the devlink instance to reload
462 * @netns_change: if true, the network namespace is changing
463 * @action: the action to perform
464 * @limit: limits on what reload should do, such as not resetting
465 * @extack: netlink extended ACK structure
466 */
467static int
468ice_devlink_reload_down(struct devlink *devlink, bool netns_change,
469 enum devlink_reload_action action,
470 enum devlink_reload_limit limit,
471 struct netlink_ext_ack *extack)
472{
473 struct ice_pf *pf = devlink_priv(devlink);
474
475 switch (action) {
476 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT:
477 if (ice_is_eswitch_mode_switchdev(pf)) {
478 NL_SET_ERR_MSG_MOD(extack,
479 "Go to legacy mode before doing reinit");
480 return -EOPNOTSUPP;
481 }
482 if (ice_is_adq_active(pf)) {
483 NL_SET_ERR_MSG_MOD(extack,
484 "Turn off ADQ before doing reinit");
485 return -EOPNOTSUPP;
486 }
487 if (ice_has_vfs(pf)) {
488 NL_SET_ERR_MSG_MOD(extack,
489 "Remove all VFs before doing reinit");
490 return -EOPNOTSUPP;
491 }
492 ice_devlink_reinit_down(pf);
493 return 0;
494 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE:
495 return ice_devlink_reload_empr_start(pf, extack);
496 default:
497 WARN_ON(1);
498 return -EOPNOTSUPP;
499 }
500}
501
502/**
503 * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
504 * @pf: pointer to the pf instance
505 * @extack: netlink extended ACK structure
506 *
507 * Wait for driver to finish rebuilding after EMP reset is completed. This
508 * includes time to wait for both the actual device reset as well as the time
509 * for the driver's rebuild to complete.
510 */
511static int
512ice_devlink_reload_empr_finish(struct ice_pf *pf,
513 struct netlink_ext_ack *extack)
514{
515 int err;
516
517 err = ice_wait_for_reset(pf, 60 * HZ);
518 if (err) {
519 NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
520 return err;
521 }
522
523 return 0;
524}
525
526/**
527 * ice_get_tx_topo_user_sel - Read user's choice from flash
528 * @pf: pointer to pf structure
529 * @layers: value read from flash will be saved here
530 *
531 * Reads user's preference for Tx Scheduler Topology Tree from PFA TLV.
532 *
533 * Return: zero when read was successful, negative values otherwise.
534 */
535static int ice_get_tx_topo_user_sel(struct ice_pf *pf, uint8_t *layers)
536{
537 struct ice_aqc_nvm_tx_topo_user_sel usr_sel = {};
538 struct ice_hw *hw = &pf->hw;
539 int err;
540
541 err = ice_acquire_nvm(hw, ICE_RES_READ);
542 if (err)
543 return err;
544
545 err = ice_aq_read_nvm(hw, ICE_AQC_NVM_TX_TOPO_MOD_ID, 0,
546 sizeof(usr_sel), &usr_sel, true, true, NULL);
547 if (err)
548 goto exit_release_res;
549
550 if (usr_sel.data & ICE_AQC_NVM_TX_TOPO_USER_SEL)
551 *layers = ICE_SCHED_5_LAYERS;
552 else
553 *layers = ICE_SCHED_9_LAYERS;
554
555exit_release_res:
556 ice_release_nvm(hw);
557
558 return err;
559}
560
561/**
562 * ice_update_tx_topo_user_sel - Save user's preference in flash
563 * @pf: pointer to pf structure
564 * @layers: value to be saved in flash
565 *
566 * Variable "layers" defines user's preference about number of layers in Tx
567 * Scheduler Topology Tree. This choice should be stored in PFA TLV field
568 * and be picked up by driver, next time during init.
569 *
570 * Return: zero when save was successful, negative values otherwise.
571 */
572static int ice_update_tx_topo_user_sel(struct ice_pf *pf, int layers)
573{
574 struct ice_aqc_nvm_tx_topo_user_sel usr_sel = {};
575 struct ice_hw *hw = &pf->hw;
576 int err;
577
578 err = ice_acquire_nvm(hw, ICE_RES_WRITE);
579 if (err)
580 return err;
581
582 err = ice_aq_read_nvm(hw, ICE_AQC_NVM_TX_TOPO_MOD_ID, 0,
583 sizeof(usr_sel), &usr_sel, true, true, NULL);
584 if (err)
585 goto exit_release_res;
586
587 if (layers == ICE_SCHED_5_LAYERS)
588 usr_sel.data |= ICE_AQC_NVM_TX_TOPO_USER_SEL;
589 else
590 usr_sel.data &= ~ICE_AQC_NVM_TX_TOPO_USER_SEL;
591
592 err = ice_write_one_nvm_block(pf, ICE_AQC_NVM_TX_TOPO_MOD_ID, 2,
593 sizeof(usr_sel.data), &usr_sel.data,
594 true, NULL, NULL);
595exit_release_res:
596 ice_release_nvm(hw);
597
598 return err;
599}
600
601/**
602 * ice_devlink_tx_sched_layers_get - Get tx_scheduling_layers parameter
603 * @devlink: pointer to the devlink instance
604 * @id: the parameter ID to set
605 * @ctx: context to store the parameter value
606 *
607 * Return: zero on success and negative value on failure.
608 */
609static int ice_devlink_tx_sched_layers_get(struct devlink *devlink, u32 id,
610 struct devlink_param_gset_ctx *ctx)
611{
612 struct ice_pf *pf = devlink_priv(devlink);
613 int err;
614
615 err = ice_get_tx_topo_user_sel(pf, &ctx->val.vu8);
616 if (err)
617 return err;
618
619 return 0;
620}
621
622/**
623 * ice_devlink_tx_sched_layers_set - Set tx_scheduling_layers parameter
624 * @devlink: pointer to the devlink instance
625 * @id: the parameter ID to set
626 * @ctx: context to get the parameter value
627 * @extack: netlink extended ACK structure
628 *
629 * Return: zero on success and negative value on failure.
630 */
631static int ice_devlink_tx_sched_layers_set(struct devlink *devlink, u32 id,
632 struct devlink_param_gset_ctx *ctx,
633 struct netlink_ext_ack *extack)
634{
635 struct ice_pf *pf = devlink_priv(devlink);
636 int err;
637
638 err = ice_update_tx_topo_user_sel(pf, ctx->val.vu8);
639 if (err)
640 return err;
641
642 NL_SET_ERR_MSG_MOD(extack,
643 "Tx scheduling layers have been changed on this device. You must do the PCI slot powercycle for the change to take effect.");
644
645 return 0;
646}
647
648/**
649 * ice_devlink_tx_sched_layers_validate - Validate passed tx_scheduling_layers
650 * parameter value
651 * @devlink: unused pointer to devlink instance
652 * @id: the parameter ID to validate
653 * @val: value to validate
654 * @extack: netlink extended ACK structure
655 *
656 * Supported values are:
657 * - 5 - five layers Tx Scheduler Topology Tree
658 * - 9 - nine layers Tx Scheduler Topology Tree
659 *
660 * Return: zero when passed parameter value is supported. Negative value on
661 * error.
662 */
663static int ice_devlink_tx_sched_layers_validate(struct devlink *devlink, u32 id,
664 union devlink_param_value val,
665 struct netlink_ext_ack *extack)
666{
667 if (val.vu8 != ICE_SCHED_5_LAYERS && val.vu8 != ICE_SCHED_9_LAYERS) {
668 NL_SET_ERR_MSG_MOD(extack,
669 "Wrong number of tx scheduler layers provided.");
670 return -EINVAL;
671 }
672
673 return 0;
674}
675
676/**
677 * ice_tear_down_devlink_rate_tree - removes devlink-rate exported tree
678 * @pf: pf struct
679 *
680 * This function tears down tree exported during VF's creation.
681 */
682void ice_tear_down_devlink_rate_tree(struct ice_pf *pf)
683{
684 struct devlink *devlink;
685 struct ice_vf *vf;
686 unsigned int bkt;
687
688 devlink = priv_to_devlink(pf);
689
690 devl_lock(devlink);
691 mutex_lock(&pf->vfs.table_lock);
692 ice_for_each_vf(pf, bkt, vf) {
693 if (vf->devlink_port.devlink_rate)
694 devl_rate_leaf_destroy(&vf->devlink_port);
695 }
696 mutex_unlock(&pf->vfs.table_lock);
697
698 devl_rate_nodes_destroy(devlink);
699 devl_unlock(devlink);
700}
701
702/**
703 * ice_enable_custom_tx - try to enable custom Tx feature
704 * @pf: pf struct
705 *
706 * This function tries to enable custom Tx feature,
707 * it's not possible to enable it, if DCB or ADQ is active.
708 */
709static bool ice_enable_custom_tx(struct ice_pf *pf)
710{
711 struct ice_port_info *pi = ice_get_main_vsi(pf)->port_info;
712 struct device *dev = ice_pf_to_dev(pf);
713
714 if (pi->is_custom_tx_enabled)
715 /* already enabled, return true */
716 return true;
717
718 if (ice_is_adq_active(pf)) {
719 dev_err(dev, "ADQ active, can't modify Tx scheduler tree\n");
720 return false;
721 }
722
723 if (ice_is_dcb_active(pf)) {
724 dev_err(dev, "DCB active, can't modify Tx scheduler tree\n");
725 return false;
726 }
727
728 pi->is_custom_tx_enabled = true;
729
730 return true;
731}
732
733/**
734 * ice_traverse_tx_tree - traverse Tx scheduler tree
735 * @devlink: devlink struct
736 * @node: current node, used for recursion
737 * @tc_node: tc_node struct, that is treated as a root
738 * @pf: pf struct
739 *
740 * This function traverses Tx scheduler tree and exports
741 * entire structure to the devlink-rate.
742 */
743static void ice_traverse_tx_tree(struct devlink *devlink, struct ice_sched_node *node,
744 struct ice_sched_node *tc_node, struct ice_pf *pf)
745{
746 struct devlink_rate *rate_node = NULL;
747 struct ice_vf *vf;
748 int i;
749
750 if (node->rate_node)
751 /* already added, skip to the next */
752 goto traverse_children;
753
754 if (node->parent == tc_node) {
755 /* create root node */
756 rate_node = devl_rate_node_create(devlink, node, node->name, NULL);
757 } else if (node->vsi_handle &&
758 pf->vsi[node->vsi_handle]->vf) {
759 vf = pf->vsi[node->vsi_handle]->vf;
760 if (!vf->devlink_port.devlink_rate)
761 /* leaf nodes doesn't have children
762 * so we don't set rate_node
763 */
764 devl_rate_leaf_create(&vf->devlink_port, node,
765 node->parent->rate_node);
766 } else if (node->info.data.elem_type != ICE_AQC_ELEM_TYPE_LEAF &&
767 node->parent->rate_node) {
768 rate_node = devl_rate_node_create(devlink, node, node->name,
769 node->parent->rate_node);
770 }
771
772 if (rate_node && !IS_ERR(rate_node))
773 node->rate_node = rate_node;
774
775traverse_children:
776 for (i = 0; i < node->num_children; i++)
777 ice_traverse_tx_tree(devlink, node->children[i], tc_node, pf);
778}
779
780/**
781 * ice_devlink_rate_init_tx_topology - export Tx scheduler tree to devlink rate
782 * @devlink: devlink struct
783 * @vsi: main vsi struct
784 *
785 * This function finds a root node, then calls ice_traverse_tx tree, which
786 * traverses the tree and exports it's contents to devlink rate.
787 */
788int ice_devlink_rate_init_tx_topology(struct devlink *devlink, struct ice_vsi *vsi)
789{
790 struct ice_port_info *pi = vsi->port_info;
791 struct ice_sched_node *tc_node;
792 struct ice_pf *pf = vsi->back;
793 int i;
794
795 tc_node = pi->root->children[0];
796 mutex_lock(&pi->sched_lock);
797 devl_lock(devlink);
798 for (i = 0; i < tc_node->num_children; i++)
799 ice_traverse_tx_tree(devlink, tc_node->children[i], tc_node, pf);
800 devl_unlock(devlink);
801 mutex_unlock(&pi->sched_lock);
802
803 return 0;
804}
805
806static void ice_clear_rate_nodes(struct ice_sched_node *node)
807{
808 node->rate_node = NULL;
809
810 for (int i = 0; i < node->num_children; i++)
811 ice_clear_rate_nodes(node->children[i]);
812}
813
814/**
815 * ice_devlink_rate_clear_tx_topology - clear node->rate_node
816 * @vsi: main vsi struct
817 *
818 * Clear rate_node to cleanup creation of Tx topology.
819 *
820 */
821void ice_devlink_rate_clear_tx_topology(struct ice_vsi *vsi)
822{
823 struct ice_port_info *pi = vsi->port_info;
824
825 mutex_lock(&pi->sched_lock);
826 ice_clear_rate_nodes(pi->root->children[0]);
827 mutex_unlock(&pi->sched_lock);
828}
829
830/**
831 * ice_set_object_tx_share - sets node scheduling parameter
832 * @pi: devlink struct instance
833 * @node: node struct instance
834 * @bw: bandwidth in bytes per second
835 * @extack: extended netdev ack structure
836 *
837 * This function sets ICE_MIN_BW scheduling BW limit.
838 */
839static int ice_set_object_tx_share(struct ice_port_info *pi, struct ice_sched_node *node,
840 u64 bw, struct netlink_ext_ack *extack)
841{
842 int status;
843
844 mutex_lock(&pi->sched_lock);
845 /* converts bytes per second to kilo bits per second */
846 node->tx_share = div_u64(bw, 125);
847 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MIN_BW, node->tx_share);
848 mutex_unlock(&pi->sched_lock);
849
850 if (status)
851 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_share");
852
853 return status;
854}
855
856/**
857 * ice_set_object_tx_max - sets node scheduling parameter
858 * @pi: devlink struct instance
859 * @node: node struct instance
860 * @bw: bandwidth in bytes per second
861 * @extack: extended netdev ack structure
862 *
863 * This function sets ICE_MAX_BW scheduling BW limit.
864 */
865static int ice_set_object_tx_max(struct ice_port_info *pi, struct ice_sched_node *node,
866 u64 bw, struct netlink_ext_ack *extack)
867{
868 int status;
869
870 mutex_lock(&pi->sched_lock);
871 /* converts bytes per second value to kilo bits per second */
872 node->tx_max = div_u64(bw, 125);
873 status = ice_sched_set_node_bw_lmt(pi, node, ICE_MAX_BW, node->tx_max);
874 mutex_unlock(&pi->sched_lock);
875
876 if (status)
877 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_max");
878
879 return status;
880}
881
882/**
883 * ice_set_object_tx_priority - sets node scheduling parameter
884 * @pi: devlink struct instance
885 * @node: node struct instance
886 * @priority: value representing priority for strict priority arbitration
887 * @extack: extended netdev ack structure
888 *
889 * This function sets priority of node among siblings.
890 */
891static int ice_set_object_tx_priority(struct ice_port_info *pi, struct ice_sched_node *node,
892 u32 priority, struct netlink_ext_ack *extack)
893{
894 int status;
895
896 if (priority >= 8) {
897 NL_SET_ERR_MSG_MOD(extack, "Priority should be less than 8");
898 return -EINVAL;
899 }
900
901 mutex_lock(&pi->sched_lock);
902 node->tx_priority = priority;
903 status = ice_sched_set_node_priority(pi, node, node->tx_priority);
904 mutex_unlock(&pi->sched_lock);
905
906 if (status)
907 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_priority");
908
909 return status;
910}
911
912/**
913 * ice_set_object_tx_weight - sets node scheduling parameter
914 * @pi: devlink struct instance
915 * @node: node struct instance
916 * @weight: value represeting relative weight for WFQ arbitration
917 * @extack: extended netdev ack structure
918 *
919 * This function sets node weight for WFQ algorithm.
920 */
921static int ice_set_object_tx_weight(struct ice_port_info *pi, struct ice_sched_node *node,
922 u32 weight, struct netlink_ext_ack *extack)
923{
924 int status;
925
926 if (weight > 200 || weight < 1) {
927 NL_SET_ERR_MSG_MOD(extack, "Weight must be between 1 and 200");
928 return -EINVAL;
929 }
930
931 mutex_lock(&pi->sched_lock);
932 node->tx_weight = weight;
933 status = ice_sched_set_node_weight(pi, node, node->tx_weight);
934 mutex_unlock(&pi->sched_lock);
935
936 if (status)
937 NL_SET_ERR_MSG_MOD(extack, "Can't set scheduling node tx_weight");
938
939 return status;
940}
941
942/**
943 * ice_get_pi_from_dev_rate - get port info from devlink_rate
944 * @rate_node: devlink struct instance
945 *
946 * This function returns corresponding port_info struct of devlink_rate
947 */
948static struct ice_port_info *ice_get_pi_from_dev_rate(struct devlink_rate *rate_node)
949{
950 struct ice_pf *pf = devlink_priv(rate_node->devlink);
951
952 return ice_get_main_vsi(pf)->port_info;
953}
954
955static int ice_devlink_rate_node_new(struct devlink_rate *rate_node, void **priv,
956 struct netlink_ext_ack *extack)
957{
958 struct ice_sched_node *node;
959 struct ice_port_info *pi;
960
961 pi = ice_get_pi_from_dev_rate(rate_node);
962
963 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
964 return -EBUSY;
965
966 /* preallocate memory for ice_sched_node */
967 node = devm_kzalloc(ice_hw_to_dev(pi->hw), sizeof(*node), GFP_KERNEL);
968 *priv = node;
969
970 return 0;
971}
972
973static int ice_devlink_rate_node_del(struct devlink_rate *rate_node, void *priv,
974 struct netlink_ext_ack *extack)
975{
976 struct ice_sched_node *node, *tc_node;
977 struct ice_port_info *pi;
978
979 pi = ice_get_pi_from_dev_rate(rate_node);
980 tc_node = pi->root->children[0];
981 node = priv;
982
983 if (!rate_node->parent || !node || tc_node == node || !extack)
984 return 0;
985
986 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
987 return -EBUSY;
988
989 /* can't allow to delete a node with children */
990 if (node->num_children)
991 return -EINVAL;
992
993 mutex_lock(&pi->sched_lock);
994 ice_free_sched_node(pi, node);
995 mutex_unlock(&pi->sched_lock);
996
997 return 0;
998}
999
1000static int ice_devlink_rate_leaf_tx_max_set(struct devlink_rate *rate_leaf, void *priv,
1001 u64 tx_max, struct netlink_ext_ack *extack)
1002{
1003 struct ice_sched_node *node = priv;
1004
1005 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1006 return -EBUSY;
1007
1008 if (!node)
1009 return 0;
1010
1011 return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_leaf),
1012 node, tx_max, extack);
1013}
1014
1015static int ice_devlink_rate_leaf_tx_share_set(struct devlink_rate *rate_leaf, void *priv,
1016 u64 tx_share, struct netlink_ext_ack *extack)
1017{
1018 struct ice_sched_node *node = priv;
1019
1020 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1021 return -EBUSY;
1022
1023 if (!node)
1024 return 0;
1025
1026 return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_leaf), node,
1027 tx_share, extack);
1028}
1029
1030static int ice_devlink_rate_leaf_tx_priority_set(struct devlink_rate *rate_leaf, void *priv,
1031 u32 tx_priority, struct netlink_ext_ack *extack)
1032{
1033 struct ice_sched_node *node = priv;
1034
1035 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1036 return -EBUSY;
1037
1038 if (!node)
1039 return 0;
1040
1041 return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_leaf), node,
1042 tx_priority, extack);
1043}
1044
1045static int ice_devlink_rate_leaf_tx_weight_set(struct devlink_rate *rate_leaf, void *priv,
1046 u32 tx_weight, struct netlink_ext_ack *extack)
1047{
1048 struct ice_sched_node *node = priv;
1049
1050 if (!ice_enable_custom_tx(devlink_priv(rate_leaf->devlink)))
1051 return -EBUSY;
1052
1053 if (!node)
1054 return 0;
1055
1056 return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_leaf), node,
1057 tx_weight, extack);
1058}
1059
1060static int ice_devlink_rate_node_tx_max_set(struct devlink_rate *rate_node, void *priv,
1061 u64 tx_max, struct netlink_ext_ack *extack)
1062{
1063 struct ice_sched_node *node = priv;
1064
1065 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1066 return -EBUSY;
1067
1068 if (!node)
1069 return 0;
1070
1071 return ice_set_object_tx_max(ice_get_pi_from_dev_rate(rate_node),
1072 node, tx_max, extack);
1073}
1074
1075static int ice_devlink_rate_node_tx_share_set(struct devlink_rate *rate_node, void *priv,
1076 u64 tx_share, struct netlink_ext_ack *extack)
1077{
1078 struct ice_sched_node *node = priv;
1079
1080 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1081 return -EBUSY;
1082
1083 if (!node)
1084 return 0;
1085
1086 return ice_set_object_tx_share(ice_get_pi_from_dev_rate(rate_node),
1087 node, tx_share, extack);
1088}
1089
1090static int ice_devlink_rate_node_tx_priority_set(struct devlink_rate *rate_node, void *priv,
1091 u32 tx_priority, struct netlink_ext_ack *extack)
1092{
1093 struct ice_sched_node *node = priv;
1094
1095 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1096 return -EBUSY;
1097
1098 if (!node)
1099 return 0;
1100
1101 return ice_set_object_tx_priority(ice_get_pi_from_dev_rate(rate_node),
1102 node, tx_priority, extack);
1103}
1104
1105static int ice_devlink_rate_node_tx_weight_set(struct devlink_rate *rate_node, void *priv,
1106 u32 tx_weight, struct netlink_ext_ack *extack)
1107{
1108 struct ice_sched_node *node = priv;
1109
1110 if (!ice_enable_custom_tx(devlink_priv(rate_node->devlink)))
1111 return -EBUSY;
1112
1113 if (!node)
1114 return 0;
1115
1116 return ice_set_object_tx_weight(ice_get_pi_from_dev_rate(rate_node),
1117 node, tx_weight, extack);
1118}
1119
1120static int ice_devlink_set_parent(struct devlink_rate *devlink_rate,
1121 struct devlink_rate *parent,
1122 void *priv, void *parent_priv,
1123 struct netlink_ext_ack *extack)
1124{
1125 struct ice_port_info *pi = ice_get_pi_from_dev_rate(devlink_rate);
1126 struct ice_sched_node *tc_node, *node, *parent_node;
1127 u16 num_nodes_added;
1128 u32 first_node_teid;
1129 u32 node_teid;
1130 int status;
1131
1132 tc_node = pi->root->children[0];
1133 node = priv;
1134
1135 if (!extack)
1136 return 0;
1137
1138 if (!ice_enable_custom_tx(devlink_priv(devlink_rate->devlink)))
1139 return -EBUSY;
1140
1141 if (!parent) {
1142 if (!node || tc_node == node || node->num_children)
1143 return -EINVAL;
1144
1145 mutex_lock(&pi->sched_lock);
1146 ice_free_sched_node(pi, node);
1147 mutex_unlock(&pi->sched_lock);
1148
1149 return 0;
1150 }
1151
1152 parent_node = parent_priv;
1153
1154 /* if the node doesn't exist, create it */
1155 if (!node->parent) {
1156 mutex_lock(&pi->sched_lock);
1157 status = ice_sched_add_elems(pi, tc_node, parent_node,
1158 parent_node->tx_sched_layer + 1,
1159 1, &num_nodes_added, &first_node_teid,
1160 &node);
1161 mutex_unlock(&pi->sched_lock);
1162
1163 if (status) {
1164 NL_SET_ERR_MSG_MOD(extack, "Can't add a new node");
1165 return status;
1166 }
1167
1168 if (devlink_rate->tx_share)
1169 ice_set_object_tx_share(pi, node, devlink_rate->tx_share, extack);
1170 if (devlink_rate->tx_max)
1171 ice_set_object_tx_max(pi, node, devlink_rate->tx_max, extack);
1172 if (devlink_rate->tx_priority)
1173 ice_set_object_tx_priority(pi, node, devlink_rate->tx_priority, extack);
1174 if (devlink_rate->tx_weight)
1175 ice_set_object_tx_weight(pi, node, devlink_rate->tx_weight, extack);
1176 } else {
1177 node_teid = le32_to_cpu(node->info.node_teid);
1178 mutex_lock(&pi->sched_lock);
1179 status = ice_sched_move_nodes(pi, parent_node, 1, &node_teid);
1180 mutex_unlock(&pi->sched_lock);
1181
1182 if (status)
1183 NL_SET_ERR_MSG_MOD(extack, "Can't move existing node to a new parent");
1184 }
1185
1186 return status;
1187}
1188
1189/**
1190 * ice_devlink_reinit_up - do reinit of the given PF
1191 * @pf: pointer to the PF struct
1192 */
1193static int ice_devlink_reinit_up(struct ice_pf *pf)
1194{
1195 struct ice_vsi *vsi = ice_get_main_vsi(pf);
1196 int err;
1197
1198 err = ice_init_dev(pf);
1199 if (err)
1200 return err;
1201
1202 vsi->flags = ICE_VSI_FLAG_INIT;
1203
1204 rtnl_lock();
1205 err = ice_vsi_cfg(vsi);
1206 rtnl_unlock();
1207 if (err)
1208 goto err_vsi_cfg;
1209
1210 /* No need to take devl_lock, it's already taken by devlink API */
1211 err = ice_load(pf);
1212 if (err)
1213 goto err_load;
1214
1215 return 0;
1216
1217err_load:
1218 rtnl_lock();
1219 ice_vsi_decfg(vsi);
1220 rtnl_unlock();
1221err_vsi_cfg:
1222 ice_deinit_dev(pf);
1223 return err;
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 */
1234static int
1235ice_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_devlink_reinit_up(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
1256static 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
1284static int
1285ice_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
1295static int ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
1296 struct devlink_param_gset_ctx *ctx,
1297 struct netlink_ext_ack *extack)
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
1317static int
1318ice_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
1335static int
1336ice_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
1346static int ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
1347 struct devlink_param_gset_ctx *ctx,
1348 struct netlink_ext_ack *extack)
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
1368static int
1369ice_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
1386enum ice_param_id {
1387 ICE_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX,
1388 ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS,
1389};
1390
1391static const struct devlink_param ice_dvl_rdma_params[] = {
1392 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1393 ice_devlink_enable_roce_get,
1394 ice_devlink_enable_roce_set,
1395 ice_devlink_enable_roce_validate),
1396 DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
1397 ice_devlink_enable_iw_get,
1398 ice_devlink_enable_iw_set,
1399 ice_devlink_enable_iw_validate),
1400};
1401
1402static const struct devlink_param ice_dvl_sched_params[] = {
1403 DEVLINK_PARAM_DRIVER(ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS,
1404 "tx_scheduling_layers",
1405 DEVLINK_PARAM_TYPE_U8,
1406 BIT(DEVLINK_PARAM_CMODE_PERMANENT),
1407 ice_devlink_tx_sched_layers_get,
1408 ice_devlink_tx_sched_layers_set,
1409 ice_devlink_tx_sched_layers_validate),
1410};
1411
1412static void ice_devlink_free(void *devlink_ptr)
1413{
1414 devlink_free((struct devlink *)devlink_ptr);
1415}
1416
1417/**
1418 * ice_allocate_pf - Allocate devlink and return PF structure pointer
1419 * @dev: the device to allocate for
1420 *
1421 * Allocate a devlink instance for this device and return the private area as
1422 * the PF structure. The devlink memory is kept track of through devres by
1423 * adding an action to remove it when unwinding.
1424 */
1425struct ice_pf *ice_allocate_pf(struct device *dev)
1426{
1427 struct devlink *devlink;
1428
1429 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
1430 if (!devlink)
1431 return NULL;
1432
1433 /* Add an action to teardown the devlink when unwinding the driver */
1434 if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
1435 return NULL;
1436
1437 return devlink_priv(devlink);
1438}
1439
1440/**
1441 * ice_devlink_register - Register devlink interface for this PF
1442 * @pf: the PF to register the devlink for.
1443 *
1444 * Register the devlink instance associated with this physical function.
1445 *
1446 * Return: zero on success or an error code on failure.
1447 */
1448void ice_devlink_register(struct ice_pf *pf)
1449{
1450 struct devlink *devlink = priv_to_devlink(pf);
1451
1452 devl_register(devlink);
1453}
1454
1455/**
1456 * ice_devlink_unregister - Unregister devlink resources for this PF.
1457 * @pf: the PF structure to cleanup
1458 *
1459 * Releases resources used by devlink and cleans up associated memory.
1460 */
1461void ice_devlink_unregister(struct ice_pf *pf)
1462{
1463 devl_unregister(priv_to_devlink(pf));
1464}
1465
1466int ice_devlink_register_params(struct ice_pf *pf)
1467{
1468 struct devlink *devlink = priv_to_devlink(pf);
1469 struct ice_hw *hw = &pf->hw;
1470 int status;
1471
1472 status = devl_params_register(devlink, ice_dvl_rdma_params,
1473 ARRAY_SIZE(ice_dvl_rdma_params));
1474 if (status)
1475 return status;
1476
1477 if (hw->func_caps.common_cap.tx_sched_topo_comp_mode_en)
1478 status = devl_params_register(devlink, ice_dvl_sched_params,
1479 ARRAY_SIZE(ice_dvl_sched_params));
1480
1481 return status;
1482}
1483
1484void ice_devlink_unregister_params(struct ice_pf *pf)
1485{
1486 struct devlink *devlink = priv_to_devlink(pf);
1487 struct ice_hw *hw = &pf->hw;
1488
1489 devl_params_unregister(devlink, ice_dvl_rdma_params,
1490 ARRAY_SIZE(ice_dvl_rdma_params));
1491
1492 if (hw->func_caps.common_cap.tx_sched_topo_comp_mode_en)
1493 devl_params_unregister(devlink, ice_dvl_sched_params,
1494 ARRAY_SIZE(ice_dvl_sched_params));
1495}
1496
1497#define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
1498
1499static const struct devlink_region_ops ice_nvm_region_ops;
1500static const struct devlink_region_ops ice_sram_region_ops;
1501
1502/**
1503 * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
1504 * @devlink: the devlink instance
1505 * @ops: the devlink region to snapshot
1506 * @extack: extended ACK response structure
1507 * @data: on exit points to snapshot data buffer
1508 *
1509 * This function is called in response to a DEVLINK_CMD_REGION_NEW for either
1510 * the nvm-flash or shadow-ram region.
1511 *
1512 * It captures a snapshot of the NVM or Shadow RAM flash contents. This
1513 * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink
1514 * interface.
1515 *
1516 * @returns zero on success, and updates the data pointer. Returns a non-zero
1517 * error code on failure.
1518 */
1519static int ice_devlink_nvm_snapshot(struct devlink *devlink,
1520 const struct devlink_region_ops *ops,
1521 struct netlink_ext_ack *extack, u8 **data)
1522{
1523 struct ice_pf *pf = devlink_priv(devlink);
1524 struct device *dev = ice_pf_to_dev(pf);
1525 struct ice_hw *hw = &pf->hw;
1526 bool read_shadow_ram;
1527 u8 *nvm_data, *tmp, i;
1528 u32 nvm_size, left;
1529 s8 num_blks;
1530 int status;
1531
1532 if (ops == &ice_nvm_region_ops) {
1533 read_shadow_ram = false;
1534 nvm_size = hw->flash.flash_size;
1535 } else if (ops == &ice_sram_region_ops) {
1536 read_shadow_ram = true;
1537 nvm_size = hw->flash.sr_words * 2u;
1538 } else {
1539 NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1540 return -EOPNOTSUPP;
1541 }
1542
1543 nvm_data = vzalloc(nvm_size);
1544 if (!nvm_data)
1545 return -ENOMEM;
1546
1547 num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
1548 tmp = nvm_data;
1549 left = nvm_size;
1550
1551 /* Some systems take longer to read the NVM than others which causes the
1552 * FW to reclaim the NVM lock before the entire NVM has been read. Fix
1553 * this by breaking the reads of the NVM into smaller chunks that will
1554 * probably not take as long. This has some overhead since we are
1555 * increasing the number of AQ commands, but it should always work
1556 */
1557 for (i = 0; i < num_blks; i++) {
1558 u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
1559
1560 status = ice_acquire_nvm(hw, ICE_RES_READ);
1561 if (status) {
1562 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1563 status, hw->adminq.sq_last_status);
1564 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1565 vfree(nvm_data);
1566 return -EIO;
1567 }
1568
1569 status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
1570 &read_sz, tmp, read_shadow_ram);
1571 if (status) {
1572 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1573 read_sz, status, hw->adminq.sq_last_status);
1574 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1575 ice_release_nvm(hw);
1576 vfree(nvm_data);
1577 return -EIO;
1578 }
1579 ice_release_nvm(hw);
1580
1581 tmp += read_sz;
1582 left -= read_sz;
1583 }
1584
1585 *data = nvm_data;
1586
1587 return 0;
1588}
1589
1590/**
1591 * ice_devlink_nvm_read - Read a portion of NVM flash contents
1592 * @devlink: the devlink instance
1593 * @ops: the devlink region to snapshot
1594 * @extack: extended ACK response structure
1595 * @offset: the offset to start at
1596 * @size: the amount to read
1597 * @data: the data buffer to read into
1598 *
1599 * This function is called in response to DEVLINK_CMD_REGION_READ to directly
1600 * read a section of the NVM contents.
1601 *
1602 * It reads from either the nvm-flash or shadow-ram region contents.
1603 *
1604 * @returns zero on success, and updates the data pointer. Returns a non-zero
1605 * error code on failure.
1606 */
1607static int ice_devlink_nvm_read(struct devlink *devlink,
1608 const struct devlink_region_ops *ops,
1609 struct netlink_ext_ack *extack,
1610 u64 offset, u32 size, u8 *data)
1611{
1612 struct ice_pf *pf = devlink_priv(devlink);
1613 struct device *dev = ice_pf_to_dev(pf);
1614 struct ice_hw *hw = &pf->hw;
1615 bool read_shadow_ram;
1616 u64 nvm_size;
1617 int status;
1618
1619 if (ops == &ice_nvm_region_ops) {
1620 read_shadow_ram = false;
1621 nvm_size = hw->flash.flash_size;
1622 } else if (ops == &ice_sram_region_ops) {
1623 read_shadow_ram = true;
1624 nvm_size = hw->flash.sr_words * 2u;
1625 } else {
1626 NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function");
1627 return -EOPNOTSUPP;
1628 }
1629
1630 if (offset + size >= nvm_size) {
1631 NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size");
1632 return -ERANGE;
1633 }
1634
1635 status = ice_acquire_nvm(hw, ICE_RES_READ);
1636 if (status) {
1637 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
1638 status, hw->adminq.sq_last_status);
1639 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
1640 return -EIO;
1641 }
1642
1643 status = ice_read_flat_nvm(hw, (u32)offset, &size, data,
1644 read_shadow_ram);
1645 if (status) {
1646 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
1647 size, status, hw->adminq.sq_last_status);
1648 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
1649 ice_release_nvm(hw);
1650 return -EIO;
1651 }
1652 ice_release_nvm(hw);
1653
1654 return 0;
1655}
1656
1657/**
1658 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
1659 * @devlink: the devlink instance
1660 * @ops: the devlink region being snapshotted
1661 * @extack: extended ACK response structure
1662 * @data: on exit points to snapshot data buffer
1663 *
1664 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
1665 * the device-caps devlink region. It captures a snapshot of the device
1666 * capabilities reported by firmware.
1667 *
1668 * @returns zero on success, and updates the data pointer. Returns a non-zero
1669 * error code on failure.
1670 */
1671static int
1672ice_devlink_devcaps_snapshot(struct devlink *devlink,
1673 const struct devlink_region_ops *ops,
1674 struct netlink_ext_ack *extack, u8 **data)
1675{
1676 struct ice_pf *pf = devlink_priv(devlink);
1677 struct device *dev = ice_pf_to_dev(pf);
1678 struct ice_hw *hw = &pf->hw;
1679 void *devcaps;
1680 int status;
1681
1682 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
1683 if (!devcaps)
1684 return -ENOMEM;
1685
1686 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
1687 ice_aqc_opc_list_dev_caps, NULL);
1688 if (status) {
1689 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
1690 status, hw->adminq.sq_last_status);
1691 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
1692 vfree(devcaps);
1693 return status;
1694 }
1695
1696 *data = (u8 *)devcaps;
1697
1698 return 0;
1699}
1700
1701static const struct devlink_region_ops ice_nvm_region_ops = {
1702 .name = "nvm-flash",
1703 .destructor = vfree,
1704 .snapshot = ice_devlink_nvm_snapshot,
1705 .read = ice_devlink_nvm_read,
1706};
1707
1708static const struct devlink_region_ops ice_sram_region_ops = {
1709 .name = "shadow-ram",
1710 .destructor = vfree,
1711 .snapshot = ice_devlink_nvm_snapshot,
1712 .read = ice_devlink_nvm_read,
1713};
1714
1715static const struct devlink_region_ops ice_devcaps_region_ops = {
1716 .name = "device-caps",
1717 .destructor = vfree,
1718 .snapshot = ice_devlink_devcaps_snapshot,
1719};
1720
1721/**
1722 * ice_devlink_init_regions - Initialize devlink regions
1723 * @pf: the PF device structure
1724 *
1725 * Create devlink regions used to enable access to dump the contents of the
1726 * flash memory on the device.
1727 */
1728void ice_devlink_init_regions(struct ice_pf *pf)
1729{
1730 struct devlink *devlink = priv_to_devlink(pf);
1731 struct device *dev = ice_pf_to_dev(pf);
1732 u64 nvm_size, sram_size;
1733
1734 nvm_size = pf->hw.flash.flash_size;
1735 pf->nvm_region = devl_region_create(devlink, &ice_nvm_region_ops, 1,
1736 nvm_size);
1737 if (IS_ERR(pf->nvm_region)) {
1738 dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1739 PTR_ERR(pf->nvm_region));
1740 pf->nvm_region = NULL;
1741 }
1742
1743 sram_size = pf->hw.flash.sr_words * 2u;
1744 pf->sram_region = devl_region_create(devlink, &ice_sram_region_ops,
1745 1, sram_size);
1746 if (IS_ERR(pf->sram_region)) {
1747 dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1748 PTR_ERR(pf->sram_region));
1749 pf->sram_region = NULL;
1750 }
1751
1752 pf->devcaps_region = devl_region_create(devlink,
1753 &ice_devcaps_region_ops, 10,
1754 ICE_AQ_MAX_BUF_LEN);
1755 if (IS_ERR(pf->devcaps_region)) {
1756 dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1757 PTR_ERR(pf->devcaps_region));
1758 pf->devcaps_region = NULL;
1759 }
1760}
1761
1762/**
1763 * ice_devlink_destroy_regions - Destroy devlink regions
1764 * @pf: the PF device structure
1765 *
1766 * Remove previously created regions for this PF.
1767 */
1768void ice_devlink_destroy_regions(struct ice_pf *pf)
1769{
1770 if (pf->nvm_region)
1771 devl_region_destroy(pf->nvm_region);
1772
1773 if (pf->sram_region)
1774 devl_region_destroy(pf->sram_region);
1775
1776 if (pf->devcaps_region)
1777 devl_region_destroy(pf->devcaps_region);
1778}