PCI ASPM: cleanup clkpm checks

In the current ASPM implementation, callers of pcie_set_clock_pm() check
Clock PM capability of the link or current Clock PM state of the link.
This check should be done in pcie_set_clock_pm() itself.

This patch moves those checks into pcie_set_clock_pm(). It also
introduces pcie_set_clkpm_nocheck() that is equivalent to old
pcie_set_clock_pm(), for the caller who wants to change Clocl PM state
regardless of the Clock PM capability or current Clock PM state. In
addition, this patch changes the function name from
pcie_set_clock_pm() to pcie_set_clkpm() for consistency.

Acked-by: Shaohua Li <shaohua.li@intel.com>
Signed-off-by: Kenji Kaneshige <kaneshige.kenji@jp.fujitsu.com>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
diff --git a/drivers/pci/pcie/aspm.c b/drivers/pci/pcie/aspm.c
index 694ba56..0d0d3e6 100644
--- a/drivers/pci/pcie/aspm.c
+++ b/drivers/pci/pcie/aspm.c
@@ -105,7 +105,7 @@
 	return 0;
 }
 
-static void pcie_set_clock_pm(struct pcie_link_state *link, int enable)
+static void pcie_set_clkpm_nocheck(struct pcie_link_state *link, int enable)
 {
 	int pos;
 	u16 reg16;
@@ -126,6 +126,17 @@
 	link->clkpm_enabled = !!enable;
 }
 
+static void pcie_set_clkpm(struct pcie_link_state *link, int enable)
+{
+	/* Don't enable Clock PM if the link is not Clock PM capable */
+	if (!link->clkpm_capable && enable)
+		return;
+	/* Need nothing if the specified equals to current state */
+	if (link->clkpm_enabled == enable)
+		return;
+	pcie_set_clkpm_nocheck(link, enable);
+}
+
 static void pcie_clkpm_cap_init(struct pcie_link_state *link, int blacklist)
 {
 	int pos, capable = 1, enabled = 1;
@@ -660,7 +671,7 @@
 
 	/* Setup initial Clock PM state */
 	state = (link->clkpm_capable) ? policy_to_clkpm_state(link) : 0;
-	pcie_set_clock_pm(link, state);
+	pcie_set_clkpm(link, state);
 unlock:
 	mutex_unlock(&aspm_lock);
 out:
@@ -738,12 +749,11 @@
 	mutex_lock(&aspm_lock);
 	link_state = parent->link_state;
 	link_state->aspm_support &= ~state;
-	if (state & PCIE_LINK_STATE_CLKPM)
-		link_state->clkpm_capable = 0;
-
 	__pcie_aspm_configure_link_state(link_state, link_state->aspm_enabled);
-	if (!link_state->clkpm_capable && link_state->clkpm_enabled)
-		pcie_set_clock_pm(link_state, 0);
+	if (state & PCIE_LINK_STATE_CLKPM) {
+		link_state->clkpm_capable = 0;
+		pcie_set_clkpm(link_state, 0);
+	}
 	mutex_unlock(&aspm_lock);
 	up_read(&pci_bus_sem);
 }
@@ -768,11 +778,7 @@
 	list_for_each_entry(link_state, &link_list, sibling) {
 		__pcie_aspm_configure_link_state(link_state,
 			policy_to_aspm_state(link_state));
-		if (link_state->clkpm_capable &&
-		    link_state->clkpm_enabled != policy_to_clkpm_state(link_state))
-			pcie_set_clock_pm(link_state,
-					  policy_to_clkpm_state(link_state));
-
+		pcie_set_clkpm(link_state, policy_to_clkpm_state(link_state));
 	}
 	mutex_unlock(&aspm_lock);
 	up_read(&pci_bus_sem);
@@ -839,7 +845,7 @@
 		const char *buf,
 		size_t n)
 {
-	struct pci_dev *pci_device = to_pci_dev(dev);
+	struct pci_dev *pdev = to_pci_dev(dev);
 	int state;
 
 	if (n < 1)
@@ -848,7 +854,7 @@
 
 	down_read(&pci_bus_sem);
 	mutex_lock(&aspm_lock);
-	pcie_set_clock_pm(pci_device->link_state, !!state);
+	pcie_set_clkpm_nocheck(pdev->link_state, !!state);
 	mutex_unlock(&aspm_lock);
 	up_read(&pci_bus_sem);