Skip to content

Commit e5fcd75

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 a041ed7 commit e5fcd75

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
@@ -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) <= 0)
49404938
return -ENOTTY;
49414939

49424940
return pci_dev_wait(child, reset_type,
@@ -6241,6 +6239,30 @@ 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 downstream link is currently active. Note it is
6247+
* possible that the card is removed immediately after this so the
6248+
* caller may need to take it into account.
6249+
*
6250+
* Return: true if link is active, or -ENODEV if the config read fails.
6251+
*/
6252+
int pcie_link_is_active(struct pci_dev *pdev)
6253+
{
6254+
u16 lnk_status;
6255+
int ret;
6256+
6257+
ret = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
6258+
if (ret == PCIBIOS_DEVICE_NOT_FOUND || PCI_POSSIBLE_ERROR(lnk_status))
6259+
return -ENODEV;
6260+
6261+
pci_dbg(pdev, "lnk_status = %#06x\n", lnk_status);
6262+
return !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
6263+
}
6264+
EXPORT_SYMBOL(pcie_link_is_active);
6265+
62446266
/**
62456267
* pci_select_bars - Make BAR mask from the type of resource
62466268
* @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
@@ -228,6 +228,7 @@ static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
228228
/* Functions for PCI Hotplug drivers to use */
229229
int pci_hp_add_bridge(struct pci_dev *dev);
230230
bool pci_hp_spurious_link_change(struct pci_dev *pdev);
231+
int pcie_link_is_active(struct pci_dev *dev);
231232

232233
#if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
233234
void pci_create_legacy_files(struct pci_bus *bus);

0 commit comments

Comments
 (0)