Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright (c) 2020, Intel Corporation. */
0003 
0004 #include <linux/vmalloc.h>
0005 
0006 #include "ice.h"
0007 #include "ice_lib.h"
0008 #include "ice_devlink.h"
0009 #include "ice_eswitch.h"
0010 #include "ice_fw_update.h"
0011 
0012 /* context for devlink info version reporting */
0013 struct ice_info_ctx {
0014     char buf[128];
0015     struct ice_orom_info pending_orom;
0016     struct ice_nvm_info pending_nvm;
0017     struct ice_netlist_info pending_netlist;
0018     struct ice_hw_dev_caps dev_caps;
0019 };
0020 
0021 /* The following functions are used to format specific strings for various
0022  * devlink info versions. The ctx parameter is used to provide the storage
0023  * buffer, as well as any ancillary information calculated when the info
0024  * request was made.
0025  *
0026  * If a version does not exist, for example when attempting to get the
0027  * inactive version of flash when there is no pending update, the function
0028  * should leave the buffer in the ctx structure empty.
0029  */
0030 
0031 static void ice_info_get_dsn(struct ice_pf *pf, struct ice_info_ctx *ctx)
0032 {
0033     u8 dsn[8];
0034 
0035     /* Copy the DSN into an array in Big Endian format */
0036     put_unaligned_be64(pci_get_dsn(pf->pdev), dsn);
0037 
0038     snprintf(ctx->buf, sizeof(ctx->buf), "%8phD", dsn);
0039 }
0040 
0041 static void ice_info_pba(struct ice_pf *pf, struct ice_info_ctx *ctx)
0042 {
0043     struct ice_hw *hw = &pf->hw;
0044     int status;
0045 
0046     status = ice_read_pba_string(hw, (u8 *)ctx->buf, sizeof(ctx->buf));
0047     if (status)
0048         /* We failed to locate the PBA, so just skip this entry */
0049         dev_dbg(ice_pf_to_dev(pf), "Failed to read Product Board Assembly string, status %d\n",
0050             status);
0051 }
0052 
0053 static void ice_info_fw_mgmt(struct ice_pf *pf, struct ice_info_ctx *ctx)
0054 {
0055     struct ice_hw *hw = &pf->hw;
0056 
0057     snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
0058          hw->fw_maj_ver, hw->fw_min_ver, hw->fw_patch);
0059 }
0060 
0061 static void ice_info_fw_api(struct ice_pf *pf, struct ice_info_ctx *ctx)
0062 {
0063     struct ice_hw *hw = &pf->hw;
0064 
0065     snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u", hw->api_maj_ver,
0066          hw->api_min_ver, hw->api_patch);
0067 }
0068 
0069 static void ice_info_fw_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
0070 {
0071     struct ice_hw *hw = &pf->hw;
0072 
0073     snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", hw->fw_build);
0074 }
0075 
0076 static void ice_info_orom_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
0077 {
0078     struct ice_orom_info *orom = &pf->hw.flash.orom;
0079 
0080     snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
0081          orom->major, orom->build, orom->patch);
0082 }
0083 
0084 static void
0085 ice_info_pending_orom_ver(struct ice_pf __always_unused *pf,
0086               struct ice_info_ctx *ctx)
0087 {
0088     struct ice_orom_info *orom = &ctx->pending_orom;
0089 
0090     if (ctx->dev_caps.common_cap.nvm_update_pending_orom)
0091         snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u",
0092              orom->major, orom->build, orom->patch);
0093 }
0094 
0095 static void ice_info_nvm_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
0096 {
0097     struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
0098 
0099     snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x", nvm->major, nvm->minor);
0100 }
0101 
0102 static void
0103 ice_info_pending_nvm_ver(struct ice_pf __always_unused *pf,
0104              struct ice_info_ctx *ctx)
0105 {
0106     struct ice_nvm_info *nvm = &ctx->pending_nvm;
0107 
0108     if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
0109         snprintf(ctx->buf, sizeof(ctx->buf), "%x.%02x",
0110              nvm->major, nvm->minor);
0111 }
0112 
0113 static void ice_info_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
0114 {
0115     struct ice_nvm_info *nvm = &pf->hw.flash.nvm;
0116 
0117     snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
0118 }
0119 
0120 static void
0121 ice_info_pending_eetrack(struct ice_pf *pf, struct ice_info_ctx *ctx)
0122 {
0123     struct ice_nvm_info *nvm = &ctx->pending_nvm;
0124 
0125     if (ctx->dev_caps.common_cap.nvm_update_pending_nvm)
0126         snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", nvm->eetrack);
0127 }
0128 
0129 static void ice_info_ddp_pkg_name(struct ice_pf *pf, struct ice_info_ctx *ctx)
0130 {
0131     struct ice_hw *hw = &pf->hw;
0132 
0133     snprintf(ctx->buf, sizeof(ctx->buf), "%s", hw->active_pkg_name);
0134 }
0135 
0136 static void
0137 ice_info_ddp_pkg_version(struct ice_pf *pf, struct ice_info_ctx *ctx)
0138 {
0139     struct ice_pkg_ver *pkg = &pf->hw.active_pkg_ver;
0140 
0141     snprintf(ctx->buf, sizeof(ctx->buf), "%u.%u.%u.%u",
0142          pkg->major, pkg->minor, pkg->update, pkg->draft);
0143 }
0144 
0145 static void
0146 ice_info_ddp_pkg_bundle_id(struct ice_pf *pf, struct ice_info_ctx *ctx)
0147 {
0148     snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", pf->hw.active_track_id);
0149 }
0150 
0151 static void ice_info_netlist_ver(struct ice_pf *pf, struct ice_info_ctx *ctx)
0152 {
0153     struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
0154 
0155     /* The netlist version fields are BCD formatted */
0156     snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
0157          netlist->major, netlist->minor,
0158          netlist->type >> 16, netlist->type & 0xFFFF,
0159          netlist->rev, netlist->cust_ver);
0160 }
0161 
0162 static void ice_info_netlist_build(struct ice_pf *pf, struct ice_info_ctx *ctx)
0163 {
0164     struct ice_netlist_info *netlist = &pf->hw.flash.netlist;
0165 
0166     snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
0167 }
0168 
0169 static void
0170 ice_info_pending_netlist_ver(struct ice_pf __always_unused *pf,
0171                  struct ice_info_ctx *ctx)
0172 {
0173     struct ice_netlist_info *netlist = &ctx->pending_netlist;
0174 
0175     /* The netlist version fields are BCD formatted */
0176     if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
0177         snprintf(ctx->buf, sizeof(ctx->buf), "%x.%x.%x-%x.%x.%x",
0178              netlist->major, netlist->minor,
0179              netlist->type >> 16, netlist->type & 0xFFFF,
0180              netlist->rev, netlist->cust_ver);
0181 }
0182 
0183 static void
0184 ice_info_pending_netlist_build(struct ice_pf __always_unused *pf,
0185                    struct ice_info_ctx *ctx)
0186 {
0187     struct ice_netlist_info *netlist = &ctx->pending_netlist;
0188 
0189     if (ctx->dev_caps.common_cap.nvm_update_pending_netlist)
0190         snprintf(ctx->buf, sizeof(ctx->buf), "0x%08x", netlist->hash);
0191 }
0192 
0193 #define fixed(key, getter) { ICE_VERSION_FIXED, key, getter, NULL }
0194 #define running(key, getter) { ICE_VERSION_RUNNING, key, getter, NULL }
0195 #define stored(key, getter, fallback) { ICE_VERSION_STORED, key, getter, fallback }
0196 
0197 /* The combined() macro inserts both the running entry as well as a stored
0198  * entry. The running entry will always report the version from the active
0199  * handler. The stored entry will first try the pending handler, and fallback
0200  * to the active handler if the pending function does not report a version.
0201  * The pending handler should check the status of a pending update for the
0202  * relevant flash component. It should only fill in the buffer in the case
0203  * where a valid pending version is available. This ensures that the related
0204  * stored and running versions remain in sync, and that stored versions are
0205  * correctly reported as expected.
0206  */
0207 #define combined(key, active, pending) \
0208     running(key, active), \
0209     stored(key, pending, active)
0210 
0211 enum ice_version_type {
0212     ICE_VERSION_FIXED,
0213     ICE_VERSION_RUNNING,
0214     ICE_VERSION_STORED,
0215 };
0216 
0217 static const struct ice_devlink_version {
0218     enum ice_version_type type;
0219     const char *key;
0220     void (*getter)(struct ice_pf *pf, struct ice_info_ctx *ctx);
0221     void (*fallback)(struct ice_pf *pf, struct ice_info_ctx *ctx);
0222 } ice_devlink_versions[] = {
0223     fixed(DEVLINK_INFO_VERSION_GENERIC_BOARD_ID, ice_info_pba),
0224     running(DEVLINK_INFO_VERSION_GENERIC_FW_MGMT, ice_info_fw_mgmt),
0225     running("fw.mgmt.api", ice_info_fw_api),
0226     running("fw.mgmt.build", ice_info_fw_build),
0227     combined(DEVLINK_INFO_VERSION_GENERIC_FW_UNDI, ice_info_orom_ver, ice_info_pending_orom_ver),
0228     combined("fw.psid.api", ice_info_nvm_ver, ice_info_pending_nvm_ver),
0229     combined(DEVLINK_INFO_VERSION_GENERIC_FW_BUNDLE_ID, ice_info_eetrack, ice_info_pending_eetrack),
0230     running("fw.app.name", ice_info_ddp_pkg_name),
0231     running(DEVLINK_INFO_VERSION_GENERIC_FW_APP, ice_info_ddp_pkg_version),
0232     running("fw.app.bundle_id", ice_info_ddp_pkg_bundle_id),
0233     combined("fw.netlist", ice_info_netlist_ver, ice_info_pending_netlist_ver),
0234     combined("fw.netlist.build", ice_info_netlist_build, ice_info_pending_netlist_build),
0235 };
0236 
0237 /**
0238  * ice_devlink_info_get - .info_get devlink handler
0239  * @devlink: devlink instance structure
0240  * @req: the devlink info request
0241  * @extack: extended netdev ack structure
0242  *
0243  * Callback for the devlink .info_get operation. Reports information about the
0244  * device.
0245  *
0246  * Return: zero on success or an error code on failure.
0247  */
0248 static int ice_devlink_info_get(struct devlink *devlink,
0249                 struct devlink_info_req *req,
0250                 struct netlink_ext_ack *extack)
0251 {
0252     struct ice_pf *pf = devlink_priv(devlink);
0253     struct device *dev = ice_pf_to_dev(pf);
0254     struct ice_hw *hw = &pf->hw;
0255     struct ice_info_ctx *ctx;
0256     size_t i;
0257     int err;
0258 
0259     err = ice_wait_for_reset(pf, 10 * HZ);
0260     if (err) {
0261         NL_SET_ERR_MSG_MOD(extack, "Device is busy resetting");
0262         return err;
0263     }
0264 
0265     ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
0266     if (!ctx)
0267         return -ENOMEM;
0268 
0269     /* discover capabilities first */
0270     err = ice_discover_dev_caps(hw, &ctx->dev_caps);
0271     if (err) {
0272         dev_dbg(dev, "Failed to discover device capabilities, status %d aq_err %s\n",
0273             err, ice_aq_str(hw->adminq.sq_last_status));
0274         NL_SET_ERR_MSG_MOD(extack, "Unable to discover device capabilities");
0275         goto out_free_ctx;
0276     }
0277 
0278     if (ctx->dev_caps.common_cap.nvm_update_pending_orom) {
0279         err = ice_get_inactive_orom_ver(hw, &ctx->pending_orom);
0280         if (err) {
0281             dev_dbg(dev, "Unable to read inactive Option ROM version data, status %d aq_err %s\n",
0282                 err, ice_aq_str(hw->adminq.sq_last_status));
0283 
0284             /* disable display of pending Option ROM */
0285             ctx->dev_caps.common_cap.nvm_update_pending_orom = false;
0286         }
0287     }
0288 
0289     if (ctx->dev_caps.common_cap.nvm_update_pending_nvm) {
0290         err = ice_get_inactive_nvm_ver(hw, &ctx->pending_nvm);
0291         if (err) {
0292             dev_dbg(dev, "Unable to read inactive NVM version data, status %d aq_err %s\n",
0293                 err, ice_aq_str(hw->adminq.sq_last_status));
0294 
0295             /* disable display of pending Option ROM */
0296             ctx->dev_caps.common_cap.nvm_update_pending_nvm = false;
0297         }
0298     }
0299 
0300     if (ctx->dev_caps.common_cap.nvm_update_pending_netlist) {
0301         err = ice_get_inactive_netlist_ver(hw, &ctx->pending_netlist);
0302         if (err) {
0303             dev_dbg(dev, "Unable to read inactive Netlist version data, status %d aq_err %s\n",
0304                 err, ice_aq_str(hw->adminq.sq_last_status));
0305 
0306             /* disable display of pending Option ROM */
0307             ctx->dev_caps.common_cap.nvm_update_pending_netlist = false;
0308         }
0309     }
0310 
0311     err = devlink_info_driver_name_put(req, KBUILD_MODNAME);
0312     if (err) {
0313         NL_SET_ERR_MSG_MOD(extack, "Unable to set driver name");
0314         goto out_free_ctx;
0315     }
0316 
0317     ice_info_get_dsn(pf, ctx);
0318 
0319     err = devlink_info_serial_number_put(req, ctx->buf);
0320     if (err) {
0321         NL_SET_ERR_MSG_MOD(extack, "Unable to set serial number");
0322         goto out_free_ctx;
0323     }
0324 
0325     for (i = 0; i < ARRAY_SIZE(ice_devlink_versions); i++) {
0326         enum ice_version_type type = ice_devlink_versions[i].type;
0327         const char *key = ice_devlink_versions[i].key;
0328 
0329         memset(ctx->buf, 0, sizeof(ctx->buf));
0330 
0331         ice_devlink_versions[i].getter(pf, ctx);
0332 
0333         /* If the default getter doesn't report a version, use the
0334          * fallback function. This is primarily useful in the case of
0335          * "stored" versions that want to report the same value as the
0336          * running version in the normal case of no pending update.
0337          */
0338         if (ctx->buf[0] == '\0' && ice_devlink_versions[i].fallback)
0339             ice_devlink_versions[i].fallback(pf, ctx);
0340 
0341         /* Do not report missing versions */
0342         if (ctx->buf[0] == '\0')
0343             continue;
0344 
0345         switch (type) {
0346         case ICE_VERSION_FIXED:
0347             err = devlink_info_version_fixed_put(req, key, ctx->buf);
0348             if (err) {
0349                 NL_SET_ERR_MSG_MOD(extack, "Unable to set fixed version");
0350                 goto out_free_ctx;
0351             }
0352             break;
0353         case ICE_VERSION_RUNNING:
0354             err = devlink_info_version_running_put(req, key, ctx->buf);
0355             if (err) {
0356                 NL_SET_ERR_MSG_MOD(extack, "Unable to set running version");
0357                 goto out_free_ctx;
0358             }
0359             break;
0360         case ICE_VERSION_STORED:
0361             err = devlink_info_version_stored_put(req, key, ctx->buf);
0362             if (err) {
0363                 NL_SET_ERR_MSG_MOD(extack, "Unable to set stored version");
0364                 goto out_free_ctx;
0365             }
0366             break;
0367         }
0368     }
0369 
0370 out_free_ctx:
0371     kfree(ctx);
0372     return err;
0373 }
0374 
0375 /**
0376  * ice_devlink_reload_empr_start - Start EMP reset to activate new firmware
0377  * @devlink: pointer to the devlink instance to reload
0378  * @netns_change: if true, the network namespace is changing
0379  * @action: the action to perform. Must be DEVLINK_RELOAD_ACTION_FW_ACTIVATE
0380  * @limit: limits on what reload should do, such as not resetting
0381  * @extack: netlink extended ACK structure
0382  *
0383  * Allow user to activate new Embedded Management Processor firmware by
0384  * issuing device specific EMP reset. Called in response to
0385  * a DEVLINK_CMD_RELOAD with the DEVLINK_RELOAD_ACTION_FW_ACTIVATE.
0386  *
0387  * Note that teardown and rebuild of the driver state happens automatically as
0388  * part of an interrupt and watchdog task. This is because all physical
0389  * functions on the device must be able to reset when an EMP reset occurs from
0390  * any source.
0391  */
0392 static int
0393 ice_devlink_reload_empr_start(struct devlink *devlink, bool netns_change,
0394                   enum devlink_reload_action action,
0395                   enum devlink_reload_limit limit,
0396                   struct netlink_ext_ack *extack)
0397 {
0398     struct ice_pf *pf = devlink_priv(devlink);
0399     struct device *dev = ice_pf_to_dev(pf);
0400     struct ice_hw *hw = &pf->hw;
0401     u8 pending;
0402     int err;
0403 
0404     err = ice_get_pending_updates(pf, &pending, extack);
0405     if (err)
0406         return err;
0407 
0408     /* pending is a bitmask of which flash banks have a pending update,
0409      * including the main NVM bank, the Option ROM bank, and the netlist
0410      * bank. If any of these bits are set, then there is a pending update
0411      * waiting to be activated.
0412      */
0413     if (!pending) {
0414         NL_SET_ERR_MSG_MOD(extack, "No pending firmware update");
0415         return -ECANCELED;
0416     }
0417 
0418     if (pf->fw_emp_reset_disabled) {
0419         NL_SET_ERR_MSG_MOD(extack, "EMP reset is not available. To activate firmware, a reboot or power cycle is needed");
0420         return -ECANCELED;
0421     }
0422 
0423     dev_dbg(dev, "Issuing device EMP reset to activate firmware\n");
0424 
0425     err = ice_aq_nvm_update_empr(hw);
0426     if (err) {
0427         dev_err(dev, "Failed to trigger EMP device reset to reload firmware, err %d aq_err %s\n",
0428             err, ice_aq_str(hw->adminq.sq_last_status));
0429         NL_SET_ERR_MSG_MOD(extack, "Failed to trigger EMP device reset to reload firmware");
0430         return err;
0431     }
0432 
0433     return 0;
0434 }
0435 
0436 /**
0437  * ice_devlink_reload_empr_finish - Wait for EMP reset to finish
0438  * @devlink: pointer to the devlink instance reloading
0439  * @action: the action requested
0440  * @limit: limits imposed by userspace, such as not resetting
0441  * @actions_performed: on return, indicate what actions actually performed
0442  * @extack: netlink extended ACK structure
0443  *
0444  * Wait for driver to finish rebuilding after EMP reset is completed. This
0445  * includes time to wait for both the actual device reset as well as the time
0446  * for the driver's rebuild to complete.
0447  */
0448 static int
0449 ice_devlink_reload_empr_finish(struct devlink *devlink,
0450                    enum devlink_reload_action action,
0451                    enum devlink_reload_limit limit,
0452                    u32 *actions_performed,
0453                    struct netlink_ext_ack *extack)
0454 {
0455     struct ice_pf *pf = devlink_priv(devlink);
0456     int err;
0457 
0458     *actions_performed = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE);
0459 
0460     err = ice_wait_for_reset(pf, 60 * HZ);
0461     if (err) {
0462         NL_SET_ERR_MSG_MOD(extack, "Device still resetting after 1 minute");
0463         return err;
0464     }
0465 
0466     return 0;
0467 }
0468 
0469 static const struct devlink_ops ice_devlink_ops = {
0470     .supported_flash_update_params = DEVLINK_SUPPORT_FLASH_UPDATE_OVERWRITE_MASK,
0471     .reload_actions = BIT(DEVLINK_RELOAD_ACTION_FW_ACTIVATE),
0472     /* The ice driver currently does not support driver reinit */
0473     .reload_down = ice_devlink_reload_empr_start,
0474     .reload_up = ice_devlink_reload_empr_finish,
0475     .eswitch_mode_get = ice_eswitch_mode_get,
0476     .eswitch_mode_set = ice_eswitch_mode_set,
0477     .info_get = ice_devlink_info_get,
0478     .flash_update = ice_devlink_flash_update,
0479 };
0480 
0481 static int
0482 ice_devlink_enable_roce_get(struct devlink *devlink, u32 id,
0483                 struct devlink_param_gset_ctx *ctx)
0484 {
0485     struct ice_pf *pf = devlink_priv(devlink);
0486 
0487     ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2 ? true : false;
0488 
0489     return 0;
0490 }
0491 
0492 static int
0493 ice_devlink_enable_roce_set(struct devlink *devlink, u32 id,
0494                 struct devlink_param_gset_ctx *ctx)
0495 {
0496     struct ice_pf *pf = devlink_priv(devlink);
0497     bool roce_ena = ctx->val.vbool;
0498     int ret;
0499 
0500     if (!roce_ena) {
0501         ice_unplug_aux_dev(pf);
0502         pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
0503         return 0;
0504     }
0505 
0506     pf->rdma_mode |= IIDC_RDMA_PROTOCOL_ROCEV2;
0507     ret = ice_plug_aux_dev(pf);
0508     if (ret)
0509         pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_ROCEV2;
0510 
0511     return ret;
0512 }
0513 
0514 static int
0515 ice_devlink_enable_roce_validate(struct devlink *devlink, u32 id,
0516                  union devlink_param_value val,
0517                  struct netlink_ext_ack *extack)
0518 {
0519     struct ice_pf *pf = devlink_priv(devlink);
0520 
0521     if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
0522         return -EOPNOTSUPP;
0523 
0524     if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP) {
0525         NL_SET_ERR_MSG_MOD(extack, "iWARP is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
0526         return -EOPNOTSUPP;
0527     }
0528 
0529     return 0;
0530 }
0531 
0532 static int
0533 ice_devlink_enable_iw_get(struct devlink *devlink, u32 id,
0534               struct devlink_param_gset_ctx *ctx)
0535 {
0536     struct ice_pf *pf = devlink_priv(devlink);
0537 
0538     ctx->val.vbool = pf->rdma_mode & IIDC_RDMA_PROTOCOL_IWARP;
0539 
0540     return 0;
0541 }
0542 
0543 static int
0544 ice_devlink_enable_iw_set(struct devlink *devlink, u32 id,
0545               struct devlink_param_gset_ctx *ctx)
0546 {
0547     struct ice_pf *pf = devlink_priv(devlink);
0548     bool iw_ena = ctx->val.vbool;
0549     int ret;
0550 
0551     if (!iw_ena) {
0552         ice_unplug_aux_dev(pf);
0553         pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
0554         return 0;
0555     }
0556 
0557     pf->rdma_mode |= IIDC_RDMA_PROTOCOL_IWARP;
0558     ret = ice_plug_aux_dev(pf);
0559     if (ret)
0560         pf->rdma_mode &= ~IIDC_RDMA_PROTOCOL_IWARP;
0561 
0562     return ret;
0563 }
0564 
0565 static int
0566 ice_devlink_enable_iw_validate(struct devlink *devlink, u32 id,
0567                    union devlink_param_value val,
0568                    struct netlink_ext_ack *extack)
0569 {
0570     struct ice_pf *pf = devlink_priv(devlink);
0571 
0572     if (!test_bit(ICE_FLAG_RDMA_ENA, pf->flags))
0573         return -EOPNOTSUPP;
0574 
0575     if (pf->rdma_mode & IIDC_RDMA_PROTOCOL_ROCEV2) {
0576         NL_SET_ERR_MSG_MOD(extack, "RoCEv2 is currently enabled. This device cannot enable iWARP and RoCEv2 simultaneously");
0577         return -EOPNOTSUPP;
0578     }
0579 
0580     return 0;
0581 }
0582 
0583 static const struct devlink_param ice_devlink_params[] = {
0584     DEVLINK_PARAM_GENERIC(ENABLE_ROCE, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
0585                   ice_devlink_enable_roce_get,
0586                   ice_devlink_enable_roce_set,
0587                   ice_devlink_enable_roce_validate),
0588     DEVLINK_PARAM_GENERIC(ENABLE_IWARP, BIT(DEVLINK_PARAM_CMODE_RUNTIME),
0589                   ice_devlink_enable_iw_get,
0590                   ice_devlink_enable_iw_set,
0591                   ice_devlink_enable_iw_validate),
0592 
0593 };
0594 
0595 static void ice_devlink_free(void *devlink_ptr)
0596 {
0597     devlink_free((struct devlink *)devlink_ptr);
0598 }
0599 
0600 /**
0601  * ice_allocate_pf - Allocate devlink and return PF structure pointer
0602  * @dev: the device to allocate for
0603  *
0604  * Allocate a devlink instance for this device and return the private area as
0605  * the PF structure. The devlink memory is kept track of through devres by
0606  * adding an action to remove it when unwinding.
0607  */
0608 struct ice_pf *ice_allocate_pf(struct device *dev)
0609 {
0610     struct devlink *devlink;
0611 
0612     devlink = devlink_alloc(&ice_devlink_ops, sizeof(struct ice_pf), dev);
0613     if (!devlink)
0614         return NULL;
0615 
0616     /* Add an action to teardown the devlink when unwinding the driver */
0617     if (devm_add_action_or_reset(dev, ice_devlink_free, devlink))
0618         return NULL;
0619 
0620     return devlink_priv(devlink);
0621 }
0622 
0623 /**
0624  * ice_devlink_register - Register devlink interface for this PF
0625  * @pf: the PF to register the devlink for.
0626  *
0627  * Register the devlink instance associated with this physical function.
0628  *
0629  * Return: zero on success or an error code on failure.
0630  */
0631 void ice_devlink_register(struct ice_pf *pf)
0632 {
0633     struct devlink *devlink = priv_to_devlink(pf);
0634 
0635     devlink_set_features(devlink, DEVLINK_F_RELOAD);
0636     devlink_register(devlink);
0637 }
0638 
0639 /**
0640  * ice_devlink_unregister - Unregister devlink resources for this PF.
0641  * @pf: the PF structure to cleanup
0642  *
0643  * Releases resources used by devlink and cleans up associated memory.
0644  */
0645 void ice_devlink_unregister(struct ice_pf *pf)
0646 {
0647     devlink_unregister(priv_to_devlink(pf));
0648 }
0649 
0650 /**
0651  * ice_devlink_set_switch_id - Set unique switch id based on pci dsn
0652  * @pf: the PF to create a devlink port for
0653  * @ppid: struct with switch id information
0654  */
0655 static void
0656 ice_devlink_set_switch_id(struct ice_pf *pf, struct netdev_phys_item_id *ppid)
0657 {
0658     struct pci_dev *pdev = pf->pdev;
0659     u64 id;
0660 
0661     id = pci_get_dsn(pdev);
0662 
0663     ppid->id_len = sizeof(id);
0664     put_unaligned_be64(id, &ppid->id);
0665 }
0666 
0667 int ice_devlink_register_params(struct ice_pf *pf)
0668 {
0669     struct devlink *devlink = priv_to_devlink(pf);
0670     union devlink_param_value value;
0671     int err;
0672 
0673     err = devlink_params_register(devlink, ice_devlink_params,
0674                       ARRAY_SIZE(ice_devlink_params));
0675     if (err)
0676         return err;
0677 
0678     value.vbool = false;
0679     devlink_param_driverinit_value_set(devlink,
0680                        DEVLINK_PARAM_GENERIC_ID_ENABLE_IWARP,
0681                        value);
0682 
0683     value.vbool = test_bit(ICE_FLAG_RDMA_ENA, pf->flags) ? true : false;
0684     devlink_param_driverinit_value_set(devlink,
0685                        DEVLINK_PARAM_GENERIC_ID_ENABLE_ROCE,
0686                        value);
0687 
0688     return 0;
0689 }
0690 
0691 void ice_devlink_unregister_params(struct ice_pf *pf)
0692 {
0693     devlink_params_unregister(priv_to_devlink(pf), ice_devlink_params,
0694                   ARRAY_SIZE(ice_devlink_params));
0695 }
0696 
0697 /**
0698  * ice_devlink_create_pf_port - Create a devlink port for this PF
0699  * @pf: the PF to create a devlink port for
0700  *
0701  * Create and register a devlink_port for this PF.
0702  *
0703  * Return: zero on success or an error code on failure.
0704  */
0705 int ice_devlink_create_pf_port(struct ice_pf *pf)
0706 {
0707     struct devlink_port_attrs attrs = {};
0708     struct devlink_port *devlink_port;
0709     struct devlink *devlink;
0710     struct ice_vsi *vsi;
0711     struct device *dev;
0712     int err;
0713 
0714     dev = ice_pf_to_dev(pf);
0715 
0716     devlink_port = &pf->devlink_port;
0717 
0718     vsi = ice_get_main_vsi(pf);
0719     if (!vsi)
0720         return -EIO;
0721 
0722     attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL;
0723     attrs.phys.port_number = pf->hw.bus.func;
0724 
0725     ice_devlink_set_switch_id(pf, &attrs.switch_id);
0726 
0727     devlink_port_attrs_set(devlink_port, &attrs);
0728     devlink = priv_to_devlink(pf);
0729 
0730     err = devlink_port_register(devlink, devlink_port, vsi->idx);
0731     if (err) {
0732         dev_err(dev, "Failed to create devlink port for PF %d, error %d\n",
0733             pf->hw.pf_id, err);
0734         return err;
0735     }
0736 
0737     return 0;
0738 }
0739 
0740 /**
0741  * ice_devlink_destroy_pf_port - Destroy the devlink_port for this PF
0742  * @pf: the PF to cleanup
0743  *
0744  * Unregisters the devlink_port structure associated with this PF.
0745  */
0746 void ice_devlink_destroy_pf_port(struct ice_pf *pf)
0747 {
0748     struct devlink_port *devlink_port;
0749 
0750     devlink_port = &pf->devlink_port;
0751 
0752     devlink_port_type_clear(devlink_port);
0753     devlink_port_unregister(devlink_port);
0754 }
0755 
0756 /**
0757  * ice_devlink_create_vf_port - Create a devlink port for this VF
0758  * @vf: the VF to create a port for
0759  *
0760  * Create and register a devlink_port for this VF.
0761  *
0762  * Return: zero on success or an error code on failure.
0763  */
0764 int ice_devlink_create_vf_port(struct ice_vf *vf)
0765 {
0766     struct devlink_port_attrs attrs = {};
0767     struct devlink_port *devlink_port;
0768     struct devlink *devlink;
0769     struct ice_vsi *vsi;
0770     struct device *dev;
0771     struct ice_pf *pf;
0772     int err;
0773 
0774     pf = vf->pf;
0775     dev = ice_pf_to_dev(pf);
0776     devlink_port = &vf->devlink_port;
0777 
0778     vsi = ice_get_vf_vsi(vf);
0779     if (!vsi)
0780         return -EINVAL;
0781 
0782     attrs.flavour = DEVLINK_PORT_FLAVOUR_PCI_VF;
0783     attrs.pci_vf.pf = pf->hw.bus.func;
0784     attrs.pci_vf.vf = vf->vf_id;
0785 
0786     ice_devlink_set_switch_id(pf, &attrs.switch_id);
0787 
0788     devlink_port_attrs_set(devlink_port, &attrs);
0789     devlink = priv_to_devlink(pf);
0790 
0791     err = devlink_port_register(devlink, devlink_port, vsi->idx);
0792     if (err) {
0793         dev_err(dev, "Failed to create devlink port for VF %d, error %d\n",
0794             vf->vf_id, err);
0795         return err;
0796     }
0797 
0798     return 0;
0799 }
0800 
0801 /**
0802  * ice_devlink_destroy_vf_port - Destroy the devlink_port for this VF
0803  * @vf: the VF to cleanup
0804  *
0805  * Unregisters the devlink_port structure associated with this VF.
0806  */
0807 void ice_devlink_destroy_vf_port(struct ice_vf *vf)
0808 {
0809     struct devlink_port *devlink_port;
0810 
0811     devlink_port = &vf->devlink_port;
0812 
0813     devlink_port_type_clear(devlink_port);
0814     devlink_port_unregister(devlink_port);
0815 }
0816 
0817 #define ICE_DEVLINK_READ_BLK_SIZE (1024 * 1024)
0818 
0819 /**
0820  * ice_devlink_nvm_snapshot - Capture a snapshot of the NVM flash contents
0821  * @devlink: the devlink instance
0822  * @ops: the devlink region being snapshotted
0823  * @extack: extended ACK response structure
0824  * @data: on exit points to snapshot data buffer
0825  *
0826  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
0827  * the nvm-flash devlink region. It captures a snapshot of the full NVM flash
0828  * contents, including both banks of flash. This snapshot can later be viewed
0829  * via the devlink-region interface.
0830  *
0831  * It captures the flash using the FLASH_ONLY bit set when reading via
0832  * firmware, so it does not read the current Shadow RAM contents. For that,
0833  * use the shadow-ram region.
0834  *
0835  * @returns zero on success, and updates the data pointer. Returns a non-zero
0836  * error code on failure.
0837  */
0838 static int ice_devlink_nvm_snapshot(struct devlink *devlink,
0839                     const struct devlink_region_ops *ops,
0840                     struct netlink_ext_ack *extack, u8 **data)
0841 {
0842     struct ice_pf *pf = devlink_priv(devlink);
0843     struct device *dev = ice_pf_to_dev(pf);
0844     struct ice_hw *hw = &pf->hw;
0845     u8 *nvm_data, *tmp, i;
0846     u32 nvm_size, left;
0847     s8 num_blks;
0848     int status;
0849 
0850     nvm_size = hw->flash.flash_size;
0851     nvm_data = vzalloc(nvm_size);
0852     if (!nvm_data)
0853         return -ENOMEM;
0854 
0855 
0856     num_blks = DIV_ROUND_UP(nvm_size, ICE_DEVLINK_READ_BLK_SIZE);
0857     tmp = nvm_data;
0858     left = nvm_size;
0859 
0860     /* Some systems take longer to read the NVM than others which causes the
0861      * FW to reclaim the NVM lock before the entire NVM has been read. Fix
0862      * this by breaking the reads of the NVM into smaller chunks that will
0863      * probably not take as long. This has some overhead since we are
0864      * increasing the number of AQ commands, but it should always work
0865      */
0866     for (i = 0; i < num_blks; i++) {
0867         u32 read_sz = min_t(u32, ICE_DEVLINK_READ_BLK_SIZE, left);
0868 
0869         status = ice_acquire_nvm(hw, ICE_RES_READ);
0870         if (status) {
0871             dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
0872                 status, hw->adminq.sq_last_status);
0873             NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
0874             vfree(nvm_data);
0875             return -EIO;
0876         }
0877 
0878         status = ice_read_flat_nvm(hw, i * ICE_DEVLINK_READ_BLK_SIZE,
0879                        &read_sz, tmp, false);
0880         if (status) {
0881             dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
0882                 read_sz, status, hw->adminq.sq_last_status);
0883             NL_SET_ERR_MSG_MOD(extack, "Failed to read NVM contents");
0884             ice_release_nvm(hw);
0885             vfree(nvm_data);
0886             return -EIO;
0887         }
0888         ice_release_nvm(hw);
0889 
0890         tmp += read_sz;
0891         left -= read_sz;
0892     }
0893 
0894     *data = nvm_data;
0895 
0896     return 0;
0897 }
0898 
0899 /**
0900  * ice_devlink_sram_snapshot - Capture a snapshot of the Shadow RAM contents
0901  * @devlink: the devlink instance
0902  * @ops: the devlink region being snapshotted
0903  * @extack: extended ACK response structure
0904  * @data: on exit points to snapshot data buffer
0905  *
0906  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
0907  * the shadow-ram devlink region. It captures a snapshot of the shadow ram
0908  * contents. This snapshot can later be viewed via the devlink-region
0909  * interface.
0910  *
0911  * @returns zero on success, and updates the data pointer. Returns a non-zero
0912  * error code on failure.
0913  */
0914 static int
0915 ice_devlink_sram_snapshot(struct devlink *devlink,
0916               const struct devlink_region_ops __always_unused *ops,
0917               struct netlink_ext_ack *extack, u8 **data)
0918 {
0919     struct ice_pf *pf = devlink_priv(devlink);
0920     struct device *dev = ice_pf_to_dev(pf);
0921     struct ice_hw *hw = &pf->hw;
0922     u8 *sram_data;
0923     u32 sram_size;
0924     int err;
0925 
0926     sram_size = hw->flash.sr_words * 2u;
0927     sram_data = vzalloc(sram_size);
0928     if (!sram_data)
0929         return -ENOMEM;
0930 
0931     err = ice_acquire_nvm(hw, ICE_RES_READ);
0932     if (err) {
0933         dev_dbg(dev, "ice_acquire_nvm failed, err %d aq_err %d\n",
0934             err, hw->adminq.sq_last_status);
0935         NL_SET_ERR_MSG_MOD(extack, "Failed to acquire NVM semaphore");
0936         vfree(sram_data);
0937         return err;
0938     }
0939 
0940     /* Read from the Shadow RAM, rather than directly from NVM */
0941     err = ice_read_flat_nvm(hw, 0, &sram_size, sram_data, true);
0942     if (err) {
0943         dev_dbg(dev, "ice_read_flat_nvm failed after reading %u bytes, err %d aq_err %d\n",
0944             sram_size, err, hw->adminq.sq_last_status);
0945         NL_SET_ERR_MSG_MOD(extack,
0946                    "Failed to read Shadow RAM contents");
0947         ice_release_nvm(hw);
0948         vfree(sram_data);
0949         return err;
0950     }
0951 
0952     ice_release_nvm(hw);
0953 
0954     *data = sram_data;
0955 
0956     return 0;
0957 }
0958 
0959 /**
0960  * ice_devlink_devcaps_snapshot - Capture snapshot of device capabilities
0961  * @devlink: the devlink instance
0962  * @ops: the devlink region being snapshotted
0963  * @extack: extended ACK response structure
0964  * @data: on exit points to snapshot data buffer
0965  *
0966  * This function is called in response to the DEVLINK_CMD_REGION_TRIGGER for
0967  * the device-caps devlink region. It captures a snapshot of the device
0968  * capabilities reported by firmware.
0969  *
0970  * @returns zero on success, and updates the data pointer. Returns a non-zero
0971  * error code on failure.
0972  */
0973 static int
0974 ice_devlink_devcaps_snapshot(struct devlink *devlink,
0975                  const struct devlink_region_ops *ops,
0976                  struct netlink_ext_ack *extack, u8 **data)
0977 {
0978     struct ice_pf *pf = devlink_priv(devlink);
0979     struct device *dev = ice_pf_to_dev(pf);
0980     struct ice_hw *hw = &pf->hw;
0981     void *devcaps;
0982     int status;
0983 
0984     devcaps = vzalloc(ICE_AQ_MAX_BUF_LEN);
0985     if (!devcaps)
0986         return -ENOMEM;
0987 
0988     status = ice_aq_list_caps(hw, devcaps, ICE_AQ_MAX_BUF_LEN, NULL,
0989                   ice_aqc_opc_list_dev_caps, NULL);
0990     if (status) {
0991         dev_dbg(dev, "ice_aq_list_caps: failed to read device capabilities, err %d aq_err %d\n",
0992             status, hw->adminq.sq_last_status);
0993         NL_SET_ERR_MSG_MOD(extack, "Failed to read device capabilities");
0994         vfree(devcaps);
0995         return status;
0996     }
0997 
0998     *data = (u8 *)devcaps;
0999 
1000     return 0;
1001 }
1002 
1003 static const struct devlink_region_ops ice_nvm_region_ops = {
1004     .name = "nvm-flash",
1005     .destructor = vfree,
1006     .snapshot = ice_devlink_nvm_snapshot,
1007 };
1008 
1009 static const struct devlink_region_ops ice_sram_region_ops = {
1010     .name = "shadow-ram",
1011     .destructor = vfree,
1012     .snapshot = ice_devlink_sram_snapshot,
1013 };
1014 
1015 static const struct devlink_region_ops ice_devcaps_region_ops = {
1016     .name = "device-caps",
1017     .destructor = vfree,
1018     .snapshot = ice_devlink_devcaps_snapshot,
1019 };
1020 
1021 /**
1022  * ice_devlink_init_regions - Initialize devlink regions
1023  * @pf: the PF device structure
1024  *
1025  * Create devlink regions used to enable access to dump the contents of the
1026  * flash memory on the device.
1027  */
1028 void ice_devlink_init_regions(struct ice_pf *pf)
1029 {
1030     struct devlink *devlink = priv_to_devlink(pf);
1031     struct device *dev = ice_pf_to_dev(pf);
1032     u64 nvm_size, sram_size;
1033 
1034     nvm_size = pf->hw.flash.flash_size;
1035     pf->nvm_region = devlink_region_create(devlink, &ice_nvm_region_ops, 1,
1036                            nvm_size);
1037     if (IS_ERR(pf->nvm_region)) {
1038         dev_err(dev, "failed to create NVM devlink region, err %ld\n",
1039             PTR_ERR(pf->nvm_region));
1040         pf->nvm_region = NULL;
1041     }
1042 
1043     sram_size = pf->hw.flash.sr_words * 2u;
1044     pf->sram_region = devlink_region_create(devlink, &ice_sram_region_ops,
1045                         1, sram_size);
1046     if (IS_ERR(pf->sram_region)) {
1047         dev_err(dev, "failed to create shadow-ram devlink region, err %ld\n",
1048             PTR_ERR(pf->sram_region));
1049         pf->sram_region = NULL;
1050     }
1051 
1052     pf->devcaps_region = devlink_region_create(devlink,
1053                            &ice_devcaps_region_ops, 10,
1054                            ICE_AQ_MAX_BUF_LEN);
1055     if (IS_ERR(pf->devcaps_region)) {
1056         dev_err(dev, "failed to create device-caps devlink region, err %ld\n",
1057             PTR_ERR(pf->devcaps_region));
1058         pf->devcaps_region = NULL;
1059     }
1060 }
1061 
1062 /**
1063  * ice_devlink_destroy_regions - Destroy devlink regions
1064  * @pf: the PF device structure
1065  *
1066  * Remove previously created regions for this PF.
1067  */
1068 void ice_devlink_destroy_regions(struct ice_pf *pf)
1069 {
1070     if (pf->nvm_region)
1071         devlink_region_destroy(pf->nvm_region);
1072 
1073     if (pf->sram_region)
1074         devlink_region_destroy(pf->sram_region);
1075 
1076     if (pf->devcaps_region)
1077         devlink_region_destroy(pf->devcaps_region);
1078 }