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 */ 14 struct 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 32 static 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 42 static 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 54 static 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 62 static 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 70 static 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 77 static 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 85 static void 86 ice_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 96 static 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 103 static void 104 ice_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 114 static 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 121 static void 122 ice_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 130 static 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 137 static void 138 ice_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 146 static void 147 ice_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 152 static 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 163 static 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 170 static void 171 ice_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 184 static void 185 ice_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 194 static 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 205 static 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 230 enum ice_version_type { 231 ICE_VERSION_FIXED, 232 ICE_VERSION_RUNNING, 233 ICE_VERSION_STORED, 234 }; 235 236 static 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 */ 269 static 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 385 out_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 */ 404 static int 405 ice_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 */ 449 static 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 */ 467 static int 468 ice_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 */ 511 static int 512 ice_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 */ 535 static 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 555 exit_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 */ 572 static 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); 595 exit_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 */ 609 static 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 */ 631 static 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 */ 663 static 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 */ 682 void 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 */ 709 static 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 */ 743 static 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 775 traverse_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 */ 788 int 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 806 static 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 */ 821 void 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 */ 839 static 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 */ 865 static 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 */ 891 static 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 */ 921 static 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 */ 948 static 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 955 static 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 973 static 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 1000 static 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 1015 static 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 1030 static 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 1045 static 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 1060 static 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 1075 static 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 1090 static 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 1105 static 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 1120 static 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 */ 1193 static int ice_devlink_reinit_up(struct ice_pf *pf) 1194 { 1195 struct ice_vsi *vsi = ice_get_main_vsi(pf); 1196 struct ice_vsi_cfg_params params; 1197 int err; 1198 1199 err = ice_init_dev(pf); 1200 if (err) 1201 return err; 1202 1203 params = ice_vsi_to_params(vsi); 1204 params.flags = ICE_VSI_FLAG_INIT; 1205 1206 rtnl_lock(); 1207 err = ice_vsi_cfg(vsi, ¶ms); 1208 rtnl_unlock(); 1209 if (err) 1210 goto err_vsi_cfg; 1211 1212 /* No need to take devl_lock, it's already taken by devlink API */ 1213 err = ice_load(pf); 1214 if (err) 1215 goto err_load; 1216 1217 return 0; 1218 1219 err_load: 1220 rtnl_lock(); 1221 ice_vsi_decfg(vsi); 1222 rtnl_unlock(); 1223 err_vsi_cfg: 1224 ice_deinit_dev(pf); 1225 return err; 1226 } 1227 1228 /** 1229 * ice_devlink_reload_up - do reload up after reinit 1230 * @devlink: pointer to the devlink instance reloading 1231 * @action: the action requested 1232 * @limit: limits imposed by userspace, such as not resetting 1233 * @actions_performed: on return, indicate what actions actually performed 1234 * @extack: netlink extended ACK structure 1235 */ 1236 static int 1237 ice_devlink_reload_up(struct devlink *devlink, 1238 enum devlink_reload_action action, 1239 enum devlink_reload_limit limit, 1240 u32 *actions_performed, 1241 struct netlink_ext_ack *extack) 1242 { 1243 struct ice_pf *pf = devlink_priv(devlink); 1244 1245 switch (action) { 1246 case DEVLINK_RELOAD_ACTION_DRIVER_REINIT: 1247 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT); 1248 return ice_devlink_reinit_up(pf); 1249 case DEVLINK_RELOAD_ACTION_FW_ACTIVATE: 1250 *actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE); 1251 return ice_devlink_reload_empr_finish(pf, extack); 1252 default: 1253 WARN_ON(1); 1254 return -EOPNOTSUPP; 1255 } 1256 } 1257 1258 static const struct devlink_ops ice_devlink_ops = { 1259 .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK, 1260 .reload_actions = BIT(DEVLINK_RELOAD_ACTION_DRIVER_REINIT) | 1261 BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE), 1262 .reload_down = ice_devlink_reload_down, 1263 .reload_up = ice_devlink_reload_up, 1264 .eswitch_mode_get = ice_eswitch_mode_get, 1265 .eswitch_mode_set = ice_eswitch_mode_set, 1266 .info_get = ice_devlink_info_get, 1267 .flash_update = ice_devlink_flash_update, 1268 1269 .rate_node_new = ice_devlink_rate_node_new, 1270 .rate_node_del = ice_devlink_rate_node_del, 1271 1272 .rate_leaf_tx_max_set = ice_devlink_rate_leaf_tx_max_set, 1273 .rate_leaf_tx_share_set = ice_devlink_rate_leaf_tx_share_set, 1274 .rate_leaf_tx_priority_set = ice_devlink_rate_leaf_tx_priority_set, 1275 .rate_leaf_tx_weight_set = ice_devlink_rate_leaf_tx_weight_set, 1276 1277 .rate_node_tx_max_set = ice_devlink_rate_node_tx_max_set, 1278 .rate_node_tx_share_set = ice_devlink_rate_node_tx_share_set, 1279 .rate_node_tx_priority_set = ice_devlink_rate_node_tx_priority_set, 1280 .rate_node_tx_weight_set = ice_devlink_rate_node_tx_weight_set, 1281 1282 .rate_leaf_parent_set = ice_devlink_set_parent, 1283 .rate_node_parent_set = ice_devlink_set_parent, 1284 }; 1285 1286 static int 1287 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id, 1288 struct devlink_param_gset_ctx *ctx) 1289 { 1290 struct ice_pf *pf = devlink_priv(devlink); 1291 1292 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false; 1293 1294 return 0; 1295 } 1296 1297 static int ice_devlink_enable_roce_set(struct devlink *devlink, u32 id, 1298 struct devlink_param_gset_ctx *ctx, 1299 struct netlink_ext_ack *extack) 1300 { 1301 struct ice_pf *pf = devlink_priv(devlink); 1302 bool roce_ena = ctx->val.vbool; 1303 int ret; 1304 1305 if (!roce_ena) { 1306 ice_unplug_aux_dev(pf); 1307 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2; 1308 return 0; 1309 } 1310 1311 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2; 1312 ret = ice_plug_aux_dev(pf); 1313 if (ret) 1314 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2; 1315 1316 return ret; 1317 } 1318 1319 static int 1320 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id, 1321 union devlink_param_value val, 1322 struct netlink_ext_ack *extack) 1323 { 1324 struct ice_pf *pf = devlink_priv(devlink); 1325 1326 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags)) 1327 return -EOPNOTSUPP; 1328 1329 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) { 1330 NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously"); 1331 return -EOPNOTSUPP; 1332 } 1333 1334 return 0; 1335 } 1336 1337 static int 1338 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id, 1339 struct devlink_param_gset_ctx *ctx) 1340 { 1341 struct ice_pf *pf = devlink_priv(devlink); 1342 1343 ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP; 1344 1345 return 0; 1346 } 1347 1348 static int ice_devlink_enable_iw_set(struct devlink *devlink, u32 id, 1349 struct devlink_param_gset_ctx *ctx, 1350 struct netlink_ext_ack *extack) 1351 { 1352 struct ice_pf *pf = devlink_priv(devlink); 1353 bool iw_ena = ctx->val.vbool; 1354 int ret; 1355 1356 if (!iw_ena) { 1357 ice_unplug_aux_dev(pf); 1358 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP; 1359 return 0; 1360 } 1361 1362 pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP; 1363 ret = ice_plug_aux_dev(pf); 1364 if (ret) 1365 pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP; 1366 1367 return ret; 1368 } 1369 1370 static int 1371 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id, 1372 union devlink_param_value val, 1373 struct netlink_ext_ack *extack) 1374 { 1375 struct ice_pf *pf = devlink_priv(devlink); 1376 1377 if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags)) 1378 return -EOPNOTSUPP; 1379 1380 if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) { 1381 NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously"); 1382 return -EOPNOTSUPP; 1383 } 1384 1385 return 0; 1386 } 1387 1388 enum ice_param_id { 1389 ICE_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX, 1390 ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS, 1391 }; 1392 1393 static const struct devlink_param ice_devlink_params[] = { 1394 DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME), 1395 ice_devlink_enable_roce_get, 1396 ice_devlink_enable_roce_set, 1397 ice_devlink_enable_roce_validate), 1398 DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME), 1399 ice_devlink_enable_iw_get, 1400 ice_devlink_enable_iw_set, 1401 ice_devlink_enable_iw_validate), 1402 DEVLINK_PARAM_DRIVER(ICE_DEVLINK_PARAM_ID_TX_SCHED_LAYERS, 1403 "tx_scheduling_layers", 1404 DEVLINK_PARAM_TYPE_U8, 1405 BIT(DEVLINK_PARAM_CMODE_PERMANENT), 1406 ice_devlink_tx_sched_layers_get, 1407 ice_devlink_tx_sched_layers_set, 1408 ice_devlink_tx_sched_layers_validate), 1409 }; 1410 1411 static void ice_devlink_free(void *devlink_ptr) 1412 { 1413 devlink_free((struct devlink *)devlink_ptr); 1414 } 1415 1416 /** 1417 * ice_allocate_pf - Allocate devlink and return PF structure pointer 1418 * @dev: the device to allocate for 1419 * 1420 * Allocate a devlink instance for this device and return the private area as 1421 * the PF structure. The devlink memory is kept track of through devres by 1422 * adding an action to remove it when unwinding. 1423 */ 1424 struct ice_pf *ice_allocate_pf(struct device *dev) 1425 { 1426 struct devlink *devlink; 1427 1428 devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev); 1429 if (!devlink) 1430 return NULL; 1431 1432 /* Add an action to teardown the devlink when unwinding the driver */ 1433 if (devm_add_action_or_reset(dev, ice_devlink_free, devlink)) 1434 return NULL; 1435 1436 return devlink_priv(devlink); 1437 } 1438 1439 /** 1440 * ice_devlink_register - Register devlink interface for this PF 1441 * @pf: the PF to register the devlink for. 1442 * 1443 * Register the devlink instance associated with this physical function. 1444 * 1445 * Return: zero on success or an error code on failure. 1446 */ 1447 void ice_devlink_register(struct ice_pf *pf) 1448 { 1449 struct devlink *devlink = priv_to_devlink(pf); 1450 1451 devl_register(devlink); 1452 } 1453 1454 /** 1455 * ice_devlink_unregister - Unregister devlink resources for this PF. 1456 * @pf: the PF structure to cleanup 1457 * 1458 * Releases resources used by devlink and cleans up associated memory. 1459 */ 1460 void ice_devlink_unregister(struct ice_pf *pf) 1461 { 1462 devl_unregister(priv_to_devlink(pf)); 1463 } 1464 1465 int ice_devlink_register_params(struct ice_pf *pf) 1466 { 1467 struct devlink *devlink = priv_to_devlink(pf); 1468 struct ice_hw *hw = &pf->hw; 1469 size_t params_size; 1470 1471 params_size = ARRAY_SIZE(ice_devlink_params); 1472 1473 if (!hw->func_caps.common_cap.tx_sched_topo_comp_mode_en) 1474 params_size--; 1475 1476 return devl_params_register(devlink, ice_devlink_params, 1477 params_size); 1478 } 1479 1480 void ice_devlink_unregister_params(struct ice_pf *pf) 1481 { 1482 devl_params_unregister(priv_to_devlink(pf), ice_devlink_params, 1483 ARRAY_SIZE(ice_devlink_params)); 1484 } 1485 1486 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024) 1487 1488 static const struct devlink_region_ops ice_nvm_region_ops; 1489 static const struct devlink_region_ops ice_sram_region_ops; 1490 1491 /** 1492 * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents 1493 * @devlink: the devlink instance 1494 * @ops: the devlink region to snapshot 1495 * @extack: extended ACK response structure 1496 * @data: on exit points to snapshot data buffer 1497 * 1498 * This function is called in response to a DEVLINK_CMD_REGION_NEW for either 1499 * the nvm-flash or shadow-ram region. 1500 * 1501 * It captures a snapshot of the NVM or Shadow RAM flash contents. This 1502 * snapshot can then later be viewed via the DEVLINK_CMD_REGION_READ netlink 1503 * interface. 1504 * 1505 * @returns zero on success, and updates the data pointer. Returns a non-zero 1506 * error code on failure. 1507 */ 1508 static int ice_devlink_nvm_snapshot(struct devlink *devlink, 1509 const struct devlink_region_ops *ops, 1510 struct netlink_ext_ack *extack, u8 **data) 1511 { 1512 struct ice_pf *pf = devlink_priv(devlink); 1513 struct device *dev = ice_pf_to_dev(pf); 1514 struct ice_hw *hw = &pf->hw; 1515 bool read_shadow_ram; 1516 u8 *nvm_data, *tmp, i; 1517 u32 nvm_size, left; 1518 s8 num_blks; 1519 int status; 1520 1521 if (ops == &ice_nvm_region_ops) { 1522 read_shadow_ram = false; 1523 nvm_size = hw->flash.flash_size; 1524 } else if (ops == &ice_sram_region_ops) { 1525 read_shadow_ram = true; 1526 nvm_size = hw->flash.sr_words * 2u; 1527 } else { 1528 NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function"); 1529 return -EOPNOTSUPP; 1530 } 1531 1532 nvm_data = vzalloc(nvm_size); 1533 if (!nvm_data) 1534 return -ENOMEM; 1535 1536 num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE); 1537 tmp = nvm_data; 1538 left = nvm_size; 1539 1540 /* Some systems take longer to read the NVM than others which causes the 1541 * FW to reclaim the NVM lock before the entire NVM has been read. Fix 1542 * this by breaking the reads of the NVM into smaller chunks that will 1543 * probably not take as long. This has some overhead since we are 1544 * increasing the number of AQ commands, but it should always work 1545 */ 1546 for (i = 0; i < num_blks; i++) { 1547 u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left); 1548 1549 status = ice_acquire_nvm(hw, ICE_RES_READ); 1550 if (status) { 1551 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n", 1552 status, hw->adminq.sq_last_status); 1553 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore"); 1554 vfree(nvm_data); 1555 return -EIO; 1556 } 1557 1558 status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE, 1559 &read_sz, tmp, read_shadow_ram); 1560 if (status) { 1561 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n", 1562 read_sz, status, hw->adminq.sq_last_status); 1563 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents"); 1564 ice_release_nvm(hw); 1565 vfree(nvm_data); 1566 return -EIO; 1567 } 1568 ice_release_nvm(hw); 1569 1570 tmp += read_sz; 1571 left -= read_sz; 1572 } 1573 1574 *data = nvm_data; 1575 1576 return 0; 1577 } 1578 1579 /** 1580 * ice_devlink_nvm_read - Read a portion of NVM flash contents 1581 * @devlink: the devlink instance 1582 * @ops: the devlink region to snapshot 1583 * @extack: extended ACK response structure 1584 * @offset: the offset to start at 1585 * @size: the amount to read 1586 * @data: the data buffer to read into 1587 * 1588 * This function is called in response to DEVLINK_CMD_REGION_READ to directly 1589 * read a section of the NVM contents. 1590 * 1591 * It reads from either the nvm-flash or shadow-ram region contents. 1592 * 1593 * @returns zero on success, and updates the data pointer. Returns a non-zero 1594 * error code on failure. 1595 */ 1596 static int ice_devlink_nvm_read(struct devlink *devlink, 1597 const struct devlink_region_ops *ops, 1598 struct netlink_ext_ack *extack, 1599 u64 offset, u32 size, u8 *data) 1600 { 1601 struct ice_pf *pf = devlink_priv(devlink); 1602 struct device *dev = ice_pf_to_dev(pf); 1603 struct ice_hw *hw = &pf->hw; 1604 bool read_shadow_ram; 1605 u64 nvm_size; 1606 int status; 1607 1608 if (ops == &ice_nvm_region_ops) { 1609 read_shadow_ram = false; 1610 nvm_size = hw->flash.flash_size; 1611 } else if (ops == &ice_sram_region_ops) { 1612 read_shadow_ram = true; 1613 nvm_size = hw->flash.sr_words * 2u; 1614 } else { 1615 NL_SET_ERR_MSG_MOD(extack, "Unexpected region in snapshot function"); 1616 return -EOPNOTSUPP; 1617 } 1618 1619 if (offset + size >= nvm_size) { 1620 NL_SET_ERR_MSG_MOD(extack, "Cannot read beyond the region size"); 1621 return -ERANGE; 1622 } 1623 1624 status = ice_acquire_nvm(hw, ICE_RES_READ); 1625 if (status) { 1626 dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n", 1627 status, hw->adminq.sq_last_status); 1628 NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore"); 1629 return -EIO; 1630 } 1631 1632 status = ice_read_flat_nvm(hw, (u32)offset, &size, data, 1633 read_shadow_ram); 1634 if (status) { 1635 dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n", 1636 size, status, hw->adminq.sq_last_status); 1637 NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents"); 1638 ice_release_nvm(hw); 1639 return -EIO; 1640 } 1641 ice_release_nvm(hw); 1642 1643 return 0; 1644 } 1645 1646 /** 1647 * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities 1648 * @devlink: the devlink instance 1649 * @ops: the devlink region being snapshotted 1650 * @extack: extended ACK response structure 1651 * @data: on exit points to snapshot data buffer 1652 * 1653 * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for 1654 * the device-caps devlink region. It captures a snapshot of the device 1655 * capabilities reported by firmware. 1656 * 1657 * @returns zero on success, and updates the data pointer. Returns a non-zero 1658 * error code on failure. 1659 */ 1660 static int 1661 ice_devlink_devcaps_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 void *devcaps; 1669 int status; 1670 1671 devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN); 1672 if (!devcaps) 1673 return -ENOMEM; 1674 1675 status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL, 1676 ice_aqc_opc_list_dev_caps, NULL); 1677 if (status) { 1678 dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n", 1679 status, hw->adminq.sq_last_status); 1680 NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities"); 1681 vfree(devcaps); 1682 return status; 1683 } 1684 1685 *data = (u8 *)devcaps; 1686 1687 return 0; 1688 } 1689 1690 static const struct devlink_region_ops ice_nvm_region_ops = { 1691 .name = "nvm-flash", 1692 .destructor = vfree, 1693 .snapshot = ice_devlink_nvm_snapshot, 1694 .read = ice_devlink_nvm_read, 1695 }; 1696 1697 static const struct devlink_region_ops ice_sram_region_ops = { 1698 .name = "shadow-ram", 1699 .destructor = vfree, 1700 .snapshot = ice_devlink_nvm_snapshot, 1701 .read = ice_devlink_nvm_read, 1702 }; 1703 1704 static const struct devlink_region_ops ice_devcaps_region_ops = { 1705 .name = "device-caps", 1706 .destructor = vfree, 1707 .snapshot = ice_devlink_devcaps_snapshot, 1708 }; 1709 1710 /** 1711 * ice_devlink_init_regions - Initialize devlink regions 1712 * @pf: the PF device structure 1713 * 1714 * Create devlink regions used to enable access to dump the contents of the 1715 * flash memory on the device. 1716 */ 1717 void ice_devlink_init_regions(struct ice_pf *pf) 1718 { 1719 struct devlink *devlink = priv_to_devlink(pf); 1720 struct device *dev = ice_pf_to_dev(pf); 1721 u64 nvm_size, sram_size; 1722 1723 nvm_size = pf->hw.flash.flash_size; 1724 pf->nvm_region = devl_region_create(devlink, &ice_nvm_region_ops, 1, 1725 nvm_size); 1726 if (IS_ERR(pf->nvm_region)) { 1727 dev_err(dev, "failed to create NVM devlink region, err %ld\n", 1728 PTR_ERR(pf->nvm_region)); 1729 pf->nvm_region = NULL; 1730 } 1731 1732 sram_size = pf->hw.flash.sr_words * 2u; 1733 pf->sram_region = devl_region_create(devlink, &ice_sram_region_ops, 1734 1, sram_size); 1735 if (IS_ERR(pf->sram_region)) { 1736 dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n", 1737 PTR_ERR(pf->sram_region)); 1738 pf->sram_region = NULL; 1739 } 1740 1741 pf->devcaps_region = devl_region_create(devlink, 1742 &ice_devcaps_region_ops, 10, 1743 ICE_AQ_MAX_BUF_LEN); 1744 if (IS_ERR(pf->devcaps_region)) { 1745 dev_err(dev, "failed to create device-caps devlink region, err %ld\n", 1746 PTR_ERR(pf->devcaps_region)); 1747 pf->devcaps_region = NULL; 1748 } 1749 } 1750 1751 /** 1752 * ice_devlink_destroy_regions - Destroy devlink regions 1753 * @pf: the PF device structure 1754 * 1755 * Remove previously created regions for this PF. 1756 */ 1757 void ice_devlink_destroy_regions(struct ice_pf *pf) 1758 { 1759 if (pf->nvm_region) 1760 devl_region_destroy(pf->nvm_region); 1761 1762 if (pf->sram_region) 1763 devl_region_destroy(pf->sram_region); 1764 1765 if (pf->devcaps_region) 1766 devl_region_destroy(pf->devcaps_region); 1767 } 1768