Skip to content

Commit 499dae1

Browse files
FROMLIST: PCI: Add pcie_link_is_active() to determine if the link is active
Add pcie_link_is_active() a common API to check if the PCIe link is active, replacing duplicate code in multiple locations. Signed-off-by: Shawn Anastasio <[email protected]> Signed-off-by: Timothy Pearson <[email protected]> Reviewed-by: Lukas Wunner <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Krishna Chaitanya Chundru <[email protected]>
1 parent fddebfa commit 499dae1

File tree

5 files changed

+31
-36
lines changed

5 files changed

+31
-36
lines changed

drivers/pci/hotplug/pciehp.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@ int pciehp_query_power_fault(struct controller *ctrl);
186186
int pciehp_card_present(struct controller *ctrl);
187187
int pciehp_card_present_or_link_active(struct controller *ctrl);
188188
int pciehp_check_link_status(struct controller *ctrl);
189-
int pciehp_check_link_active(struct controller *ctrl);
190189
bool pciehp_device_replaced(struct controller *ctrl);
191190
void pciehp_release_ctrl(struct controller *ctrl);
192191

drivers/pci/hotplug/pciehp_ctrl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
260260
/* Turn the slot on if it's occupied or link is up */
261261
mutex_lock(&ctrl->state_lock);
262262
present = pciehp_card_present(ctrl);
263-
link_active = pciehp_check_link_active(ctrl);
263+
link_active = pcie_link_is_active(ctrl->pcie->port);
264264
if (present <= 0 && link_active <= 0) {
265265
if (ctrl->state == BLINKINGON_STATE) {
266266
ctrl->state = OFF_STATE;

drivers/pci/hotplug/pciehp_hpc.c

Lines changed: 4 additions & 31 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)
@@ -614,8 +587,8 @@ static void pciehp_ignore_link_change(struct controller *ctrl,
614587
* Synthesize it to ensure that it is acted on.
615588
*/
616589
down_read_nested(&ctrl->reset_lock, ctrl->depth);
617-
if (!pciehp_check_link_active(ctrl) || pciehp_device_replaced(ctrl))
618-
pciehp_request(ctrl, ignored_events);
590+
if (!pcie_link_is_active(ctrl_dev(ctrl)) || pciehp_device_replaced(ctrl))
591+
pciehp_request(ctrl, PCI_EXP_SLTSTA_DLLSC);
619592
up_read(&ctrl->reset_lock);
620593
}
621594

@@ -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: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4868,7 +4868,6 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
48684868
return 0;
48694869

48704870
if (pcie_get_speed_cap(dev) <= PCIE_SPEED_5_0GT) {
4871-
u16 status;
48724871

48734872
pci_dbg(dev, "waiting %d ms for downstream link\n", delay);
48744873
msleep(delay);
@@ -4884,8 +4883,7 @@ int pci_bridge_wait_for_secondary_bus(struct pci_dev *dev, char *reset_type)
48844883
if (!dev->link_active_reporting)
48854884
return -ENOTTY;
48864885

4887-
pcie_capability_read_word(dev, PCI_EXP_LNKSTA, &status);
4888-
if (!(status & PCI_EXP_LNKSTA_DLLLA))
4886+
if (pcie_link_is_active(dev) <= 0)
48894887
return -ENOTTY;
48904888

48914889
return pci_dev_wait(child, reset_type,
@@ -6194,6 +6192,30 @@ void pcie_print_link_status(struct pci_dev *dev)
61946192
}
61956193
EXPORT_SYMBOL(pcie_print_link_status);
61966194

6195+
/**
6196+
* pcie_link_is_active() - Checks if the link is active or not
6197+
* @pdev: PCI device to query
6198+
*
6199+
* Check whether the downstream link is currently active. Note it is
6200+
* possible that the card is removed immediately after this so the
6201+
* caller may need to take it into account.
6202+
*
6203+
* Return: true if link is active, or -ENODEV if the config read fails.
6204+
*/
6205+
int pcie_link_is_active(struct pci_dev *pdev)
6206+
{
6207+
u16 lnk_status;
6208+
int ret;
6209+
6210+
ret = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
6211+
if (ret == PCIBIOS_DEVICE_NOT_FOUND || PCI_POSSIBLE_ERROR(lnk_status))
6212+
return -ENODEV;
6213+
6214+
pci_dbg(pdev, "lnk_status = %#06x\n", lnk_status);
6215+
return !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
6216+
}
6217+
EXPORT_SYMBOL(pcie_link_is_active);
6218+
61976219
/**
61986220
* pci_select_bars - Make BAR mask from the type of resource
61996221
* @dev: the PCI device for which BAR mask is made

drivers/pci/pci.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
320320
/* Functions for PCI Hotplug drivers to use */
321321
int pci_hp_add_bridge(struct pci_dev *dev);
322322
bool pci_hp_spurious_link_change(struct pci_dev *pdev);
323+
int pcie_link_is_active(struct pci_dev *dev);
323324

324325
#if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
325326
void pci_create_legacy_files(struct pci_bus *bus);

0 commit comments

Comments
 (0)