Skip to content

Commit 87e56ea

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 5364f3a commit 87e56ea

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
@@ -4919,7 +4919,6 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
49194919
return 0;
49204920

49214921
if (pcie_get_speed_cap(dev) <= PCIE_SPEED_5_0GT) {
4922-
u16 status;
49234922

49244923
pci_dbg(dev, "waiting %d ms for downstream link\n", delay);
49254924
msleep(delay);
@@ -4935,8 +4934,7 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
49354934
if (!dev->link_active_reporting)
49364935
return -ENOTTY;
49374936

4938-
pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &status);
4939-
if (!(status & PCI_EXP_LNKSTA_DLLLA))
4937+
if (!pcie_link_is_active(dev))
49404938
return -ENOTTY;
49414939

49424940
return pci_dev_wait(child, reset_type,
@@ -6241,6 +6239,28 @@ void pcie_print_link_status(struct pci_dev *dev)
62416239
}
62426240
EXPORT_SYMBOL(pcie_print_link_status);
62436241

6242+
/**
6243+
* pcie_link_is_active() - Checks if the link is active or not
6244+
* @pdev: PCI device to query
6245+
*
6246+
* Check whether the link is active or not.
6247+
*
6248+
* Return: true if link is active.
6249+
*/
6250+
bool pcie_link_is_active(struct pci_dev *pdev)
6251+
{
6252+
u16 lnk_status;
6253+
int ret;
6254+
6255+
ret = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
6256+
if (ret == PCIBIOS_DEVICE_NOT_FOUND || PCI_POSSIBLE_ERROR(lnk_status))
6257+
return false;
6258+
6259+
pci_dbg(pdev, "lnk_status = %x\n", lnk_status);
6260+
return !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
6261+
}
6262+
EXPORT_SYMBOL(pcie_link_is_active);
6263+
62446264
/**
62456265
* pci_select_bars - Make BAR mask from the type of resource
62466266
* @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
@@ -2016,6 +2016,7 @@ pci_release_mem_regions(struct pci_dev *pdev)
20162016
pci_select_bars(pdev, IORESOURCE_MEM));
20172017
}
20182018

2019+
bool pcie_link_is_active(struct pci_dev *dev);
20192020
#else /* CONFIG_PCI is not enabled */
20202021

20212022
static inline void pci_set_flags(int flags) { }
@@ -2164,6 +2165,9 @@ pci_alloc_irq_vectors(struct pci_dev *dev, unsigned int min_vecs,
21642165
{
21652166
return -ENOSPC;
21662167
}
2168+
2169+
static inline bool pcie_link_is_active(struct pci_dev *dev)
2170+
{ return false; }
21672171
#endif /* CONFIG_PCI */
21682172

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

0 commit comments

Comments
 (0)