Skip to content

Commit 2c9bc88

Browse files
FROMLIST: PCI: PCI: Add pcie_link_is_active() to determine if the PCIe link is active
Introduce a common API to check if the PCIe link is active, replacing duplicate code in multiple locations. Link: https://lore.kernel.org/lkml/[email protected]/ Signed-off-by: Krishna Chaitanya Chundru <[email protected]> Reviewed-by: Lukas Wunner <[email protected]>
1 parent 69d8388 commit 2c9bc88

File tree

4 files changed

+33
-35
lines changed

4 files changed

+33
-35
lines changed

drivers/pci/hotplug/pciehp_ctrl.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,8 @@ void pciehp_handle_disable_request(struct controller *ctrl)
230230

231231
void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
232232
{
233-
int present, link_active;
233+
bool link_active;
234+
int present;
234235

235236
/*
236237
* If the slot is on and presence or link has changed, turn it off.
@@ -260,8 +261,8 @@ void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
260261
/* Turn the slot on if it's occupied or link is up */
261262
mutex_lock(&ctrl->state_lock);
262263
present = pciehp_card_present(ctrl);
263-
link_active = pciehp_check_link_active(ctrl);
264-
if (present <= 0 && link_active <= 0) {
264+
link_active = pcie_link_is_active(ctrl->pcie->port);
265+
if (present <= 0 && !link_active) {
265266
if (ctrl->state == BLINKINGON_STATE) {
266267
ctrl->state = OFF_STATE;
267268
cancel_delayed_work(&ctrl->button_work);

drivers/pci/hotplug/pciehp_hpc.c

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -221,33 +221,6 @@ static void pcie_write_cmd_nowait(struct controller *ctrl, u16 cmd, u16 mask)
221221
pcie_do_write_cmd(ctrl, cmd, mask, false);
222222
}
223223

224-
/**
225-
* pciehp_check_link_active() - Is the link active
226-
* @ctrl: PCIe hotplug controller
227-
*
228-
* Check whether the downstream link is currently active. Note it is
229-
* possible that the card is removed immediately after this so the
230-
* caller may need to take it into account.
231-
*
232-
* If the hotplug controller itself is not available anymore returns
233-
* %-ENODEV.
234-
*/
235-
int pciehp_check_link_active(struct controller *ctrl)
236-
{
237-
struct pci_dev *pdev = ctrl_dev(ctrl);
238-
u16 lnk_status;
239-
int ret;
240-
241-
ret = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
242-
if (ret == PCIBIOS_DEVICE_NOT_FOUND || PCI_POSSIBLE_ERROR(lnk_status))
243-
return -ENODEV;
244-
245-
ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
246-
ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
247-
248-
return ret;
249-
}
250-
251224
static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
252225
{
253226
u32 l;
@@ -467,7 +440,7 @@ int pciehp_card_present_or_link_active(struct controller *ctrl)
467440
if (ret)
468441
return ret;
469442

470-
return pciehp_check_link_active(ctrl);
443+
return pcie_link_is_active(ctrl_dev(ctrl));
471444
}
472445

473446
int pciehp_query_power_fault(struct controller *ctrl)
@@ -921,7 +894,7 @@ int pciehp_slot_reset(struct pcie_device *dev)
921894
pcie_capability_write_word(dev->port, PCI_EXP_SLTSTA,
922895
PCI_EXP_SLTSTA_DLLSC);
923896

924-
if (!pciehp_check_link_active(ctrl))
897+
if (!pcie_link_is_active(ctrl_dev(ctrl)))
925898
pciehp_request(ctrl, PCI_EXP_SLTSTA_DLLSC);
926899

927900
return 0;

drivers/pci/pci.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4909,7 +4909,6 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
49094909
return 0;
49104910

49114911
if (pcie_get_speed_cap(dev) <= PCIE_SPEED_5_0GT) {
4912-
u16 status;
49134912

49144913
pci_dbg(dev, "waiting %d ms for downstream link\n", delay);
49154914
msleep(delay);
@@ -4925,8 +4924,7 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
49254924
if (!dev->link_active_reporting)
49264925
return -ENOTTY;
49274926

4928-
pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &status);
4929-
if (!(status & PCI_EXP_LNKSTA_DLLLA))
4927+
if (!pcie_link_is_active(dev))
49304928
return -ENOTTY;
49314929

49324930
return pci_dev_wait(child, reset_type,
@@ -6231,6 +6229,28 @@ void pcie_print_link_status(struct pci_dev *dev)
62316229
}
62326230
EXPORT_SYMBOL(pcie_print_link_status);
62336231

6232+
/**
6233+
* pcie_link_is_active() - Checks if the link is active or not
6234+
* @pdev: PCI device to query
6235+
*
6236+
* Check whether the link is active or not.
6237+
*
6238+
* Return: true if link is active.
6239+
*/
6240+
bool pcie_link_is_active(struct pci_dev *pdev)
6241+
{
6242+
u16 lnk_status;
6243+
int ret;
6244+
6245+
ret = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
6246+
if (ret == PCIBIOS_DEVICE_NOT_FOUND || PCI_POSSIBLE_ERROR(lnk_status))
6247+
return false;
6248+
6249+
pci_dbg(pdev, "lnk_status = %x\n", lnk_status);
6250+
return !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
6251+
}
6252+
EXPORT_SYMBOL(pcie_link_is_active);
6253+
62346254
/**
62356255
* pci_select_bars - Make BAR mask from the type of resource
62366256
* @dev: the PCI device for which BAR mask is made

include/linux/pci.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,7 @@ pci_release_mem_regions(struct pci_dev *pdev)
19951995
pci_select_bars(pdev, IORESOURCE_MEM));
19961996
}
19971997

1998+
bool pcie_link_is_active(struct pci_dev *dev);
19981999
#else /* CONFIG_PCI is not enabled */
19992000

20002001
static inline void pci_set_flags(int flags) { }
@@ -2143,6 +2144,9 @@ pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
21432144
{
21442145
return -ENOSPC;
21452146
}
2147+
2148+
static inline bool pcie_link_is_active(struct pci_dev *dev)
2149+
{ return false; }
21462150
#endif /* CONFIG_PCI */
21472151

21482152
/* Include architecture-dependent settings and functions */

0 commit comments

Comments
 (0)