On Tue, 26 Oct 2021 12:06:03 +0300 Yishai Hadas <yishaih@xxxxxxxxxx> wrote: > +static int mlx5vf_pci_set_device_state(struct mlx5vf_pci_core_device *mvdev, > + u32 state) > +{ > + struct mlx5vf_pci_migration_info *vmig = &mvdev->vmig; > + u32 old_state = vmig->vfio_dev_state; > + u32 flipped_bits = old_state ^ state; > + int ret = 0; > + > + if (old_state == VFIO_DEVICE_STATE_ERROR || > + !VFIO_DEVICE_STATE_VALID(state) || > + (state & ~MLX5VF_SUPPORTED_DEVICE_STATES)) > + return -EINVAL; > + > + /* Running switches off */ > + if ((flipped_bits & VFIO_DEVICE_STATE_RUNNING) && > + !(state & VFIO_DEVICE_STATE_RUNNING)) { > + ret = mlx5vf_pci_quiesce_device(mvdev); > + if (ret) > + return ret; > + ret = mlx5vf_pci_freeze_device(mvdev); > + if (ret) { > + vmig->vfio_dev_state = VFIO_DEVICE_STATE_ERROR; > + return ret; Is it not possible for this to unwind, only entering the error state if unquiesce also fails? > + } > + } > + > + /* Saving switches on and not running */ > + if ((flipped_bits & > + (VFIO_DEVICE_STATE_RUNNING | VFIO_DEVICE_STATE_SAVING)) && > + ((state & (VFIO_DEVICE_STATE_RUNNING | > + VFIO_DEVICE_STATE_SAVING)) == VFIO_DEVICE_STATE_SAVING)) { Can't this be reduced to: if ((flipped_bits & ~VFIO_DEVICE_STATE_RESUMING) && (state == VFIO_DEVICE_STATE_SAVING)) { Maybe there's an argument for the original to be more invariant of TBD device_state bits? Thanks, Alex > + ret = mlx5vf_pci_save_device_data(mvdev); > + if (ret) > + return ret; > + } > + > + /* Resuming switches on */ > + if ((flipped_bits & VFIO_DEVICE_STATE_RESUMING) && > + (state & VFIO_DEVICE_STATE_RESUMING)) { > + mlx5vf_reset_mig_state(mvdev); > + ret = mlx5vf_pci_new_write_window(mvdev); > + if (ret) > + return ret; > + } > + > + /* Resuming switches off */ > + if ((flipped_bits & VFIO_DEVICE_STATE_RESUMING) && > + !(state & VFIO_DEVICE_STATE_RESUMING)) { > + /* deserialize state into the device */ > + ret = mlx5vf_load_state(mvdev); > + if (ret) { > + vmig->vfio_dev_state = VFIO_DEVICE_STATE_ERROR; > + return ret; > + } > + } > + > + /* Running switches on */ > + if ((flipped_bits & VFIO_DEVICE_STATE_RUNNING) && > + (state & VFIO_DEVICE_STATE_RUNNING)) { > + ret = mlx5vf_pci_unfreeze_device(mvdev); > + if (ret) > + return ret; > + ret = mlx5vf_pci_unquiesce_device(mvdev); > + if (ret) { > + vmig->vfio_dev_state = VFIO_DEVICE_STATE_ERROR; > + return ret; > + } > + } > + > + vmig->vfio_dev_state = state; > + return 0; > +}