From: Valentine Fatiev <valentinef@nvidia.com>
Subject: [PATCH] BACKPORT: drivers/net/ethernet/mellanox/mlxfw/mlxfw_fsm.c

Change-Id: Ic61f5544ac79f28bc3fbc2be494765eca8f0470c
---
 .../net/ethernet/mellanox/mlxfw/mlxfw_fsm.c   | 96 +++++++++++++++----
 1 file changed, 76 insertions(+), 20 deletions(-)

--- a/drivers/net/ethernet/mellanox/mlxfw/mlxfw_fsm.c
+++ b/drivers/net/ethernet/mellanox/mlxfw/mlxfw_fsm.c
@@ -50,31 +50,41 @@ static int mlxfw_fsm_state_err(struct ml
 	case MLXFW_FSM_STATE_ERR_ERROR:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "general error", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_REJECTED_DIGEST_ERR:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "component hash mismatch", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_REJECTED_NOT_APPLICABLE:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "component not applicable", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_REJECTED_UNKNOWN_KEY:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "unknown key", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_REJECTED_AUTH_FAILED:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "authentication failed", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_REJECTED_UNSIGNED:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "component was not signed", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_REJECTED_KEY_NOT_APPLICABLE:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "key not applicable", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_REJECTED_BAD_FORMAT:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "bad format", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_BLOCKED_PENDING_RESET:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "pending reset", err);
 		break;
+
 	case MLXFW_FSM_STATE_ERR_OK:
+
 	case MLXFW_FSM_STATE_ERR_MAX:
 		MLXFW_ERR_MSG(mlxfw_dev, extack, "unknown error", err);
 		break;
@@ -84,8 +94,9 @@ static int mlxfw_fsm_state_err(struct ml
 };
 
 static int mlxfw_fsm_state_wait(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
-				enum mlxfw_fsm_state fsm_state,
-				struct netlink_ext_ack *extack)
+				enum mlxfw_fsm_state fsm_state
+				, struct netlink_ext_ack *extack
+				)
 {
 	enum mlxfw_fsm_state_err fsm_state_err;
 	enum mlxfw_fsm_state curr_fsm_state;
@@ -102,7 +113,9 @@ retry:
 	}
 
 	if (fsm_state_err != MLXFW_FSM_STATE_ERR_OK)
-		return mlxfw_fsm_state_err(mlxfw_dev, extack, fsm_state_err);
+		return mlxfw_fsm_state_err(mlxfw_dev,
+			       	extack, 
+				fsm_state_err);
 
 	if (curr_fsm_state != fsm_state) {
 		if (--times == 0) {
@@ -118,7 +131,8 @@ retry:
 
 static int
 mlxfw_fsm_reactivate_err(struct mlxfw_dev *mlxfw_dev,
-			 struct netlink_ext_ack *extack, u8 err)
+			 struct netlink_ext_ack *extack, 
+			 u8 err)
 {
 	enum mlxfw_fsm_reactivate_status status;
 
@@ -133,31 +147,41 @@ mlxfw_fsm_reactivate_err(struct mlxfw_de
 	case MLXFW_FSM_REACTIVATE_STATUS_BUSY:
 		MLXFW_REACT_ERR("busy", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_PROHIBITED_FW_VER_ERR:
 		MLXFW_REACT_ERR("prohibited fw ver", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_COPY_FAILED:
 		MLXFW_REACT_ERR("first page copy failed", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_ERASE_FAILED:
 		MLXFW_REACT_ERR("first page erase failed", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_FIRST_PAGE_RESTORE_FAILED:
 		MLXFW_REACT_ERR("first page restore failed", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_CANDIDATE_FW_DEACTIVATION_FAILED:
 		MLXFW_REACT_ERR("candidate fw deactivation failed", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_ERR_DEVICE_RESET_REQUIRED:
 		MLXFW_REACT_ERR("device reset required", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_ERR_FW_PROGRAMMING_NEEDED:
 		MLXFW_REACT_ERR("fw programming needed", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_FW_ALREADY_ACTIVATED:
 		MLXFW_REACT_ERR("fw already activated", err);
 		break;
+
 	case MLXFW_FSM_REACTIVATE_STATUS_OK:
+
 	case MLXFW_FSM_REACTIVATE_STATUS_MAX:
 		MLXFW_REACT_ERR("unexpected error", err);
 		break;
@@ -191,15 +215,24 @@ static int mlxfw_fsm_reactivate(struct m
 	    status == MLXFW_FSM_REACTIVATE_STATUS_FW_ALREADY_ACTIVATED)
 		return 0;
 
-	return mlxfw_fsm_reactivate_err(mlxfw_dev, extack, status);
+	return mlxfw_fsm_reactivate_err(mlxfw_dev,
+		       	extack, 
+			status);
 }
 
 static void mlxfw_status_notify(struct mlxfw_dev *mlxfw_dev,
 				const char *msg, const char *comp_name,
 				u32 done_bytes, u32 total_bytes)
 {
+#ifdef HAVE_DEVLINK_FLASH_UPDATE_STATUS_NOTIFY
 	devlink_flash_update_status_notify(mlxfw_dev->devlink, msg, comp_name,
 					   done_bytes, total_bytes);
+#else
+       if (!mlxfw_dev->ops->status_notify)
+              return;
+       mlxfw_dev->ops->status_notify(mlxfw_dev, msg, comp_name,
+                                  done_bytes, total_bytes);
+#endif
 }
 
 #define MLXFW_ALIGN_DOWN(x, align_bits) ((x) & ~((1 << (align_bits)) - 1))
@@ -209,8 +242,9 @@ static void mlxfw_status_notify(struct m
 static int mlxfw_flash_component(struct mlxfw_dev *mlxfw_dev,
 				 u32 fwhandle,
 				 struct mlxfw_mfa2_component *comp,
-				 bool reactivate_supp,
-				 struct netlink_ext_ack *extack)
+				 bool reactivate_supp
+				 ,struct netlink_ext_ack *extack
+				 )
 {
 	u16 comp_max_write_size;
 	u8 comp_align_bits;
@@ -239,7 +273,6 @@ static int mlxfw_flash_component(struct
 
 	comp_max_write_size = MLXFW_ALIGN_DOWN(comp_max_write_size,
 					       comp_align_bits);
-
 	mlxfw_dbg(mlxfw_dev, "Component update\n");
 	mlxfw_status_notify(mlxfw_dev, "Updating component", comp_name, 0, 0);
 	err = mlxfw_dev->ops->fsm_component_update(mlxfw_dev, fwhandle,
@@ -257,10 +290,11 @@ static int mlxfw_flash_component(struct
 	}
 
 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
-				   MLXFW_FSM_STATE_DOWNLOAD, extack);
+				   MLXFW_FSM_STATE_DOWNLOAD
+				   , extack
+				   );
 	if (err)
 		goto err_out;
-
 	mlxfw_dbg(mlxfw_dev, "Component download\n");
 	mlxfw_status_notify(mlxfw_dev, "Downloading component",
 			    comp_name, 0, comp->data_size);
@@ -294,7 +328,9 @@ static int mlxfw_flash_component(struct
 	}
 
 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
-				   MLXFW_FSM_STATE_LOCKED, extack);
+				   MLXFW_FSM_STATE_LOCKED
+				   , extack
+				   );
 	if (err)
 		goto err_out;
 	return 0;
@@ -306,8 +342,9 @@ err_out:
 
 static int mlxfw_flash_components(struct mlxfw_dev *mlxfw_dev, u32 fwhandle,
 				  struct mlxfw_mfa2_file *mfa2_file,
-				  bool reactivate_supp,
-				  struct netlink_ext_ack *extack)
+				  bool reactivate_supp
+				  , struct netlink_ext_ack *extack
+				  )
 {
 	u32 component_count;
 	int err;
@@ -336,6 +373,7 @@ static int mlxfw_flash_components(struct
 
 		mlxfw_info(mlxfw_dev, "Flashing component type %d\n",
 			   comp->index);
+
 		err = mlxfw_flash_component(mlxfw_dev, fwhandle, comp,
 					    reactivate_supp, extack);
 		mlxfw_mfa2_file_component_put(comp);
@@ -346,8 +384,9 @@ static int mlxfw_flash_components(struct
 }
 
 int mlxfw_firmware_flash(struct mlxfw_dev *mlxfw_dev,
-			 const struct firmware *firmware,
-			 struct netlink_ext_ack *extack)
+			 const struct firmware *firmware
+			 , struct netlink_ext_ack *extack
+			 )
 {
 	struct mlxfw_mfa2_file *mfa2_file;
 	bool reactivate_supp = true;
@@ -379,21 +418,29 @@ int mlxfw_firmware_flash(struct mlxfw_de
 	}
 
 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
-				   MLXFW_FSM_STATE_LOCKED, extack);
+				   MLXFW_FSM_STATE_LOCKED
+				   , extack
+				   );
 	if (err)
 		goto err_state_wait_idle_to_locked;
 
-	err = mlxfw_fsm_reactivate(mlxfw_dev, extack, &reactivate_supp);
+	err = mlxfw_fsm_reactivate(mlxfw_dev, 
+			extack, 
+			&reactivate_supp);
 	if (err)
 		goto err_fsm_reactivate;
 
 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
-				   MLXFW_FSM_STATE_LOCKED, extack);
+				   MLXFW_FSM_STATE_LOCKED
+				   , extack
+				   );
 	if (err)
 		goto err_state_wait_reactivate_to_locked;
 
 	err = mlxfw_flash_components(mlxfw_dev, fwhandle, mfa2_file,
-				     reactivate_supp, extack);
+				     reactivate_supp
+				     , extack
+				     );
 	if (err)
 		goto err_flash_components;
 
@@ -407,7 +454,9 @@ int mlxfw_firmware_flash(struct mlxfw_de
 	}
 
 	err = mlxfw_fsm_state_wait(mlxfw_dev, fwhandle,
-				   MLXFW_FSM_STATE_LOCKED, extack);
+				   MLXFW_FSM_STATE_LOCKED
+				   , extack
+				   );
 	if (err)
 		goto err_state_wait_activate_to_locked;
 
@@ -417,6 +466,10 @@ int mlxfw_firmware_flash(struct mlxfw_de
 	mlxfw_info(mlxfw_dev, "Firmware flash done\n");
 	mlxfw_status_notify(mlxfw_dev, "Firmware flash done", NULL, 0, 0);
 	mlxfw_mfa2_file_fini(mfa2_file);
+
+#ifdef HAVE_DEVLINK_FLASH_UPDATE_END_NOTIFY
+	devlink_flash_update_end_notify(mlxfw_dev->devlink);
+#endif
 	return 0;
 
 err_state_wait_activate_to_locked:
@@ -428,6 +481,9 @@ err_state_wait_idle_to_locked:
 	mlxfw_dev->ops->fsm_release(mlxfw_dev, fwhandle);
 err_fsm_lock:
 	mlxfw_mfa2_file_fini(mfa2_file);
+#ifdef HAVE_DEVLINK_FLASH_UPDATE_END_NOTIFY
+	devlink_flash_update_end_notify(mlxfw_dev->devlink);
+#endif
 	return err;
 }
 EXPORT_SYMBOL(mlxfw_firmware_flash);
