[PATCH v8 4/6] PCI/ERR: simplify function pci_dev_set_io_state() with if

From: Ethan Zhao
Date: Wed Oct 07 2020 - 07:33:57 EST


No function change.

Signed-off-by: Ethan Zhao <haifeng.zhao@xxxxxxxxx>
---
Changes:
v8: based on Bjorn's code and truth table, simplify the logic of
function pci_dev_set_io_state(), no function change.

drivers/pci/pci.h | 54 ++++++++++++++++++++---------------------------
1 file changed, 23 insertions(+), 31 deletions(-)

diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index 455b32187abd..bceb3f108744 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -359,39 +359,31 @@ struct pci_sriov {
static inline bool pci_dev_set_io_state(struct pci_dev *dev,
pci_channel_state_t new)
{
- bool changed = false;
-
device_lock_assert(&dev->dev);
- switch (new) {
- case pci_channel_io_perm_failure:
- switch (dev->error_state) {
- case pci_channel_io_frozen:
- case pci_channel_io_normal:
- case pci_channel_io_perm_failure:
- changed = true;
- break;
- }
- break;
- case pci_channel_io_frozen:
- switch (dev->error_state) {
- case pci_channel_io_frozen:
- case pci_channel_io_normal:
- changed = true;
- break;
- }
- break;
- case pci_channel_io_normal:
- switch (dev->error_state) {
- case pci_channel_io_frozen:
- case pci_channel_io_normal:
- changed = true;
- break;
- }
- break;
+/*
+ * Truth table:
+ * requested new state
+ * current ------------------------------------------
+ * state normal frozen perm_failure
+ * ------------ + ------------- ------------- ------------
+ * normal | normal frozen perm_failure
+ * frozen | normal frozen perm_failure
+ * perm_failure | perm_failure* perm_failure* perm_failure
+ */
+
+ /* Can always put a device in perm_failure state */
+ if (new == pci_channel_io_perm_failure) {
+ dev->error_state = pci_channel_io_perm_failure;
+ return true;
}
- if (changed)
- dev->error_state = new;
- return changed;
+
+ /* If already in perm_failure, can't set to normal or frozen */
+ if (dev->error_state == pci_channel_io_perm_failure)
+ return false;
+
+ /* Can always change normal to frozen or vice versa */
+ dev->error_state = new;
+ return true;
}

static inline int pci_dev_set_disconnected(struct pci_dev *dev, void *unused)
--
2.18.4