Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
0002 /* Copyright (c) 2017-2019 Mellanox Technologies. All rights reserved */
0003 
0004 #define pr_fmt(fmt) "mlxfw: " fmt
0005 
0006 #include <linux/kernel.h>
0007 #include <linux/module.h>
0008 #include <linux/delay.h>
0009 
0010 #include "mlxfw.h"
0011 #include "mlxfw_mfa2.h"
0012 
0013 #define MLXFW_FSM_STATE_WAIT_CYCLE_MS 200
0014 #define MLXFW_FSM_STATE_WAIT_TIMEOUT_MS 30000
0015 #define MLXFW_FSM_STATE_WAIT_ROUNDS \
0016     (MLXFW_FSM_STATE_WAIT_TIMEOUT_MS / MLXFW_FSM_STATE_WAIT_CYCLE_MS)
0017 #define MLXFW_FSM_MAX_COMPONENT_SIZE (10 * (1 << 20))
0018 
0019 static const int mlxfw_fsm_state_errno[] = {
0020     [MLXFW_FSM_STATE_ERR_ERROR] = -EIO,
0021     [MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR] = -EBADMSG,
0022     [MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE] = -ENOENT,
0023     [MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY] = -ENOKEY,
0024     [MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED] = -EACCES,
0025     [MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED] = -EKEYREVOKED,
0026     [MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE] = -EKEYREJECTED,
0027     [MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT] = -ENOEXEC,
0028     [MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET] = -EBUSY,
0029     [MLXFW_FSM_STATE_ERR_MAX] = -EINVAL
0030 };
0031 
0032 #define MLXFW_ERR_PRFX "Firmware flash failed: "
0033 #define MLXFW_ERR_MSG(fwdev, extack, msg, err) do { \
0034     mlxfw_err(fwdev, "%s, err (%d)\n", MLXFW_ERR_PRFX msg, err); \
0035     NL_SET_ERR_MSG_MOD(extack, MLXFW_ERR_PRFX msg); \
0036 } while (0)
0037 
0038 static int mlxfw_fsm_state_err(struct mlxfw_dev *mlxfw_dev,
0039                    struct netlink_ext_ack *extack,
0040                    enum mlxfw_fsm_state_err err)
0041 {
0042     enum mlxfw_fsm_state_err fsm_state_err;
0043 
0044     fsm_state_err = min_t(enum mlxfw_fsm_state_err, err,
0045                   MLXFW_FSM_STATE_ERR_MAX);
0046 
0047     switch (fsm_state_err) {
0048     case MLXFW_FSM_STATE_ERR_ERROR:
0049         MLXFW_ERR_MSG(mlxfw_dev, extack, "general error", err);
0050         break;
0051     case MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR:
0052         MLXFW_ERR_MSG(mlxfw_dev, extack, "component hash mismatch", err);
0053         break;
0054     case MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE:
0055         MLXFW_ERR_MSG(mlxfw_dev, extack, "component not applicable", err);
0056         break;
0057     case MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY:
0058         MLXFW_ERR_MSG(mlxfw_dev, extack, "unknown key", err);
0059         break;
0060     case MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED:
0061         MLXFW_ERR_MSG(mlxfw_dev, extack, "authentication failed", err);
0062         break;
0063     case MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED:
0064         MLXFW_ERR_MSG(mlxfw_dev, extack, "component was not signed", err);
0065         break;
0066     case MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE:
0067         MLXFW_ERR_MSG(mlxfw_dev, extack, "key not applicable", err);
0068         break;
0069     case MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT:
0070         MLXFW_ERR_MSG(mlxfw_dev, extack, "bad format", err);
0071         break;
0072     case MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET:
0073         MLXFW_ERR_MSG(mlxfw_dev, extack, "pending reset", err);
0074         break;
0075     case MLXFW_FSM_STATE_ERR_OK:
0076     case MLXFW_FSM_STATE_ERR_MAX:
0077         MLXFW_ERR_MSG(mlxfw_dev, extack, "unknown error", err);
0078         break;
0079     }
0080 
0081     return mlxfw_fsm_state_errno[fsm_state_err];
0082 };
0083 
0084 static int mlxfw_fsm_state_wait(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
0085                 enum mlxfw_fsm_state fsm_state,
0086                 struct netlink_ext_ack *extack)
0087 {
0088     enum mlxfw_fsm_state_err fsm_state_err;
0089     enum mlxfw_fsm_state curr_fsm_state;
0090     int times;
0091     int err;
0092 
0093     times = MLXFW_FSM_STATE_WAIT_ROUNDS;
0094 retry:
0095     err = mlxfw_dev->ops->fsm_query_state(mlxfw_dev, fwhandle,
0096                           &curr_fsm_state, &fsm_state_err);
0097     if (err) {
0098         MLXFW_ERR_MSG(mlxfw_dev, extack, "FSM state query failed", err);
0099         return err;
0100     }
0101 
0102     if (fsm_state_err != MLXFW_FSM_STATE_ERR_OK)
0103         return mlxfw_fsm_state_err(mlxfw_dev, extack, fsm_state_err);
0104 
0105     if (curr_fsm_state != fsm_state) {
0106         if (--times == 0) {
0107             MLXFW_ERR_MSG(mlxfw_dev, extack,
0108                       "Timeout reached on FSM state change", -ETIMEDOUT);
0109             return -ETIMEDOUT;
0110         }
0111         msleep(MLXFW_FSM_STATE_WAIT_CYCLE_MS);
0112         goto retry;
0113     }
0114     return 0;
0115 }
0116 
0117 static int
0118 mlxfw_fsm_reactivate_err(struct mlxfw_dev *mlxfw_dev,
0119              struct netlink_ext_ack *extack, u8 err)
0120 {
0121     enum mlxfw_fsm_reactivate_status status;
0122 
0123 #define MXFW_REACT_PRFX "Reactivate FSM: "
0124 #define MLXFW_REACT_ERR(msg, err) \
0125     MLXFW_ERR_MSG(mlxfw_dev, extack, MXFW_REACT_PRFX msg, err)
0126 
0127     status = min_t(enum mlxfw_fsm_reactivate_status, err,
0128                MLXFW_FSM_REACTIVATE_STATUS_MAX);
0129 
0130     switch (status) {
0131     case MLXFW_FSM_REACTIVATE_STATUS_BUSY:
0132         MLXFW_REACT_ERR("busy", err);
0133         break;
0134     case MLXFW_FSM_REACTIVATE_STATUS_PROHIBITED_FW_VER_ERR:
0135         MLXFW_REACT_ERR("prohibited fw ver", err);
0136         break;
0137     case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_COPY_FAILED:
0138         MLXFW_REACT_ERR("first page copy failed", err);
0139         break;
0140     case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_ERASE_FAILED:
0141         MLXFW_REACT_ERR("first page erase failed", err);
0142         break;
0143     case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_RESTORE_FAILED:
0144         MLXFW_REACT_ERR("first page restore failed", err);
0145         break;
0146     case MLXFW_FSM_REACTIVATE_STATUS_CANDIDATE_FW_DEACTIVATION_FAILED:
0147         MLXFW_REACT_ERR("candidate fw deactivation failed", err);
0148         break;
0149     case MLXFW_FSM_REACTIVATE_STATUS_ERR_DEVICE_RESET_REQUIRED:
0150         MLXFW_REACT_ERR("device reset required", err);
0151         break;
0152     case MLXFW_FSM_REACTIVATE_STATUS_ERR_FW_PROGRAMMING_NEEDED:
0153         MLXFW_REACT_ERR("fw programming needed", err);
0154         break;
0155     case MLXFW_FSM_REACTIVATE_STATUS_FW_ALREADY_ACTIVATED:
0156         MLXFW_REACT_ERR("fw already activated", err);
0157         break;
0158     case MLXFW_FSM_REACTIVATE_STATUS_OK:
0159     case MLXFW_FSM_REACTIVATE_STATUS_MAX:
0160         MLXFW_REACT_ERR("unexpected error", err);
0161         break;
0162     }
0163     return -EREMOTEIO;
0164 };
0165 
0166 static int mlxfw_fsm_reactivate(struct mlxfw_dev *mlxfw_dev,
0167                 struct netlink_ext_ack *extack,
0168                 bool *supported)
0169 {
0170     u8 status;
0171     int err;
0172 
0173     if (!mlxfw_dev->ops->fsm_reactivate)
0174         return 0;
0175 
0176     err = mlxfw_dev->ops->fsm_reactivate(mlxfw_dev, &status);
0177     if (err == -EOPNOTSUPP) {
0178         *supported = false;
0179         return 0;
0180     }
0181 
0182     if (err) {
0183         MLXFW_ERR_MSG(mlxfw_dev, extack,
0184                   "Could not reactivate firmware flash", err);
0185         return err;
0186     }
0187 
0188     if (status == MLXFW_FSM_REACTIVATE_STATUS_OK ||
0189         status == MLXFW_FSM_REACTIVATE_STATUS_FW_ALREADY_ACTIVATED)
0190         return 0;
0191 
0192     return mlxfw_fsm_reactivate_err(mlxfw_dev, extack, status);
0193 }
0194 
0195 static void mlxfw_status_notify(struct mlxfw_dev *mlxfw_dev,
0196                 const char *msg, const char *comp_name,
0197                 u32 done_bytes, u32 total_bytes)
0198 {
0199     devlink_flash_update_status_notify(mlxfw_dev->devlink, msg, comp_name,
0200                        done_bytes, total_bytes);
0201 }
0202 
0203 #define MLXFW_ALIGN_DOWN(x, align_bits) ((x) & ~((1 << (align_bits)) - 1))
0204 #define MLXFW_ALIGN_UP(x, align_bits) \
0205         MLXFW_ALIGN_DOWN((x) + ((1 << (align_bits)) - 1), (align_bits))
0206 
0207 static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
0208                  u32 fwhandle,
0209                  struct mlxfw_mfa2_component *comp,
0210                  bool reactivate_supp,
0211                  struct netlink_ext_ack *extack)
0212 {
0213     u16 comp_max_write_size;
0214     u8 comp_align_bits;
0215     u32 comp_max_size;
0216     char comp_name[8];
0217     u16 block_size;
0218     u8 *block_ptr;
0219     u32 offset;
0220     int err;
0221 
0222     sprintf(comp_name, "%u", comp->index);
0223 
0224     err = mlxfw_dev->ops->component_query(mlxfw_dev, comp->index,
0225                           &comp_max_size, &comp_align_bits,
0226                           &comp_max_write_size);
0227     if (err) {
0228         MLXFW_ERR_MSG(mlxfw_dev, extack, "FSM component query failed", err);
0229         return err;
0230     }
0231 
0232     comp_max_size = min_t(u32, comp_max_size, MLXFW_FSM_MAX_COMPONENT_SIZE);
0233     if (comp->data_size > comp_max_size) {
0234         MLXFW_ERR_MSG(mlxfw_dev, extack,
0235                   "Component size is bigger than limit", -EINVAL);
0236         return -EINVAL;
0237     }
0238 
0239     comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
0240                            comp_align_bits);
0241 
0242     mlxfw_dbg(mlxfw_dev, "Component update\n");
0243     mlxfw_status_notify(mlxfw_dev, "Updating component", comp_name, 0, 0);
0244     err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
0245                            comp->index,
0246                            comp->data_size);
0247     if (err) {
0248         if (!reactivate_supp)
0249             MLXFW_ERR_MSG(mlxfw_dev, extack,
0250                       "FSM component update failed, FW reactivate is not supported",
0251                       err);
0252         else
0253             MLXFW_ERR_MSG(mlxfw_dev, extack,
0254                       "FSM component update failed", err);
0255         return err;
0256     }
0257 
0258     err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
0259                    MLXFW_FSM_STATE_DOWNLOAD, extack);
0260     if (err)
0261         goto err_out;
0262 
0263     mlxfw_dbg(mlxfw_dev, "Component download\n");
0264     mlxfw_status_notify(mlxfw_dev, "Downloading component",
0265                 comp_name, 0, comp->data_size);
0266     for (offset = 0;
0267          offset < MLXFW_ALIGN_UP(comp->data_size, comp_align_bits);
0268          offset += comp_max_write_size) {
0269         block_ptr = comp->data + offset;
0270         block_size = (u16) min_t(u32, comp->data_size - offset,
0271                      comp_max_write_size);
0272         err = mlxfw_dev->ops->fsm_block_download(mlxfw_dev, fwhandle,
0273                              block_ptr, block_size,
0274                              offset);
0275         if (err) {
0276             MLXFW_ERR_MSG(mlxfw_dev, extack,
0277                       "Component download failed", err);
0278             goto err_out;
0279         }
0280         mlxfw_status_notify(mlxfw_dev, "Downloading component",
0281                     comp_name, offset + block_size,
0282                     comp->data_size);
0283     }
0284 
0285     mlxfw_dbg(mlxfw_dev, "Component verify\n");
0286     mlxfw_status_notify(mlxfw_dev, "Verifying component", comp_name, 0, 0);
0287     err = mlxfw_dev->ops->fsm_component_verify(mlxfw_dev, fwhandle,
0288                            comp->index);
0289     if (err) {
0290         MLXFW_ERR_MSG(mlxfw_dev, extack,
0291                   "FSM component verify failed", err);
0292         goto err_out;
0293     }
0294 
0295     err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
0296                    MLXFW_FSM_STATE_LOCKED, extack);
0297     if (err)
0298         goto err_out;
0299     return 0;
0300 
0301 err_out:
0302     mlxfw_dev->ops->fsm_cancel(mlxfw_dev, fwhandle);
0303     return err;
0304 }
0305 
0306 static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
0307                   struct mlxfw_mfa2_file *mfa2_file,
0308                   bool reactivate_supp,
0309                   struct netlink_ext_ack *extack)
0310 {
0311     u32 component_count;
0312     int err;
0313     int i;
0314 
0315     err = mlxfw_mfa2_file_component_count(mfa2_file, mlxfw_dev->psid,
0316                           mlxfw_dev->psid_size,
0317                           &component_count);
0318     if (err) {
0319         MLXFW_ERR_MSG(mlxfw_dev, extack,
0320                   "Could not find device PSID in MFA2 file", err);
0321         return err;
0322     }
0323 
0324     for (i = 0; i < component_count; i++) {
0325         struct mlxfw_mfa2_component *comp;
0326 
0327         comp = mlxfw_mfa2_file_component_get(mfa2_file, mlxfw_dev->psid,
0328                              mlxfw_dev->psid_size, i);
0329         if (IS_ERR(comp)) {
0330             err = PTR_ERR(comp);
0331             MLXFW_ERR_MSG(mlxfw_dev, extack,
0332                       "Failed to get MFA2 component", err);
0333             return err;
0334         }
0335 
0336         mlxfw_info(mlxfw_dev, "Flashing component type %d\n",
0337                comp->index);
0338         err = mlxfw_flash_component(mlxfw_dev, fwhandle, comp,
0339                         reactivate_supp, extack);
0340         mlxfw_mfa2_file_component_put(comp);
0341         if (err)
0342             return err;
0343     }
0344     return 0;
0345 }
0346 
0347 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
0348              const struct firmware *firmware,
0349              struct netlink_ext_ack *extack)
0350 {
0351     struct mlxfw_mfa2_file *mfa2_file;
0352     bool reactivate_supp = true;
0353     u32 fwhandle;
0354     int err;
0355 
0356     if (!mlxfw_mfa2_check(firmware)) {
0357         MLXFW_ERR_MSG(mlxfw_dev, extack,
0358                   "Firmware file is not MFA2", -EINVAL);
0359         return -EINVAL;
0360     }
0361 
0362     mfa2_file = mlxfw_mfa2_file_init(firmware);
0363     if (IS_ERR(mfa2_file)) {
0364         err = PTR_ERR(mfa2_file);
0365         MLXFW_ERR_MSG(mlxfw_dev, extack,
0366                   "Failed to initialize MFA2 firmware file", err);
0367         return err;
0368     }
0369 
0370     mlxfw_info(mlxfw_dev, "Initialize firmware flash process\n");
0371     mlxfw_status_notify(mlxfw_dev, "Initializing firmware flash process",
0372                 NULL, 0, 0);
0373     err = mlxfw_dev->ops->fsm_lock(mlxfw_dev, &fwhandle);
0374     if (err) {
0375         MLXFW_ERR_MSG(mlxfw_dev, extack,
0376                   "Could not lock the firmware FSM", err);
0377         goto err_fsm_lock;
0378     }
0379 
0380     err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
0381                    MLXFW_FSM_STATE_LOCKED, extack);
0382     if (err)
0383         goto err_state_wait_idle_to_locked;
0384 
0385     err = mlxfw_fsm_reactivate(mlxfw_dev, extack, &reactivate_supp);
0386     if (err)
0387         goto err_fsm_reactivate;
0388 
0389     err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
0390                    MLXFW_FSM_STATE_LOCKED, extack);
0391     if (err)
0392         goto err_state_wait_reactivate_to_locked;
0393 
0394     err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file,
0395                      reactivate_supp, extack);
0396     if (err)
0397         goto err_flash_components;
0398 
0399     mlxfw_dbg(mlxfw_dev, "Activate image\n");
0400     mlxfw_status_notify(mlxfw_dev, "Activating image", NULL, 0, 0);
0401     err = mlxfw_dev->ops->fsm_activate(mlxfw_dev, fwhandle);
0402     if (err) {
0403         MLXFW_ERR_MSG(mlxfw_dev, extack,
0404                   "Could not activate the downloaded image", err);
0405         goto err_fsm_activate;
0406     }
0407 
0408     err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
0409                    MLXFW_FSM_STATE_LOCKED, extack);
0410     if (err)
0411         goto err_state_wait_activate_to_locked;
0412 
0413     mlxfw_dbg(mlxfw_dev, "Handle release\n");
0414     mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
0415 
0416     mlxfw_info(mlxfw_dev, "Firmware flash done\n");
0417     mlxfw_status_notify(mlxfw_dev, "Firmware flash done", NULL, 0, 0);
0418     mlxfw_mfa2_file_fini(mfa2_file);
0419     return 0;
0420 
0421 err_state_wait_activate_to_locked:
0422 err_fsm_activate:
0423 err_flash_components:
0424 err_state_wait_reactivate_to_locked:
0425 err_fsm_reactivate:
0426 err_state_wait_idle_to_locked:
0427     mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
0428 err_fsm_lock:
0429     mlxfw_mfa2_file_fini(mfa2_file);
0430     return err;
0431 }
0432 EXPORT_SYMBOL(mlxfw_firmware_flash);
0433 
0434 MODULE_LICENSE("Dual BSD/GPL");
0435 MODULE_AUTHOR("Yotam Gigi <yotamg@mellanox.com>");
0436 MODULE_DESCRIPTION("Mellanox firmware flash lib");