linux重新扫描pci总线

设备PCI总线上有块FPGA,在没有加载代码前,不会被识别。
加载完代码后需要重新扫描PCI总线,识别到FPGA设备。

1. linux对pci rescan支持
<drivers/pci/pci-driver.c>
注册总线pci_bus_type,关注成员dev_attr
static int __init pci_driver_init(void)
{
 return bus_register(&pci_bus_type);
}
postcore_initcall(pci_driver_init);

struct bus_type pci_bus_type = {
 .name  = "pci",
 .match  = pci_bus_match,
 .uevent  = pci_uevent,
 .probe  = pci_device_probe,
 .remove  = pci_device_remove,
 .shutdown = pci_device_shutdown,
 .dev_attrs = pci_dev_attrs,
 .bus_attrs = pci_bus_attrs,
 .pm  = PCI_PM_OPS_PTR,
};

<drivers/pci/pci-sysfs.c>
pci_dev_attrs关注成员rescan
struct device_attribute pci_dev_attrs[] = {
 __ATTR_RO(resource),
 __ATTR_RO(vendor),
 __ATTR_RO(device),
 __ATTR_RO(subsystem_vendor),
 __ATTR_RO(subsystem_device),
 __ATTR_RO(class),
 __ATTR_RO(irq),
 __ATTR_RO(local_cpus),
 __ATTR_RO(local_cpulist),
 __ATTR_RO(modalias),
#ifdef CONFIG_NUMA
 __ATTR_RO(numa_node),
#endif
 __ATTR_RO(dma_mask_bits),
 __ATTR_RO(consistent_dma_mask_bits),
 __ATTR(enable, 0600, is_enabled_show, is_enabled_store),
 __ATTR(broken_parity_status,(S_IRUGO|S_IWUSR),
  broken_parity_status_show,broken_parity_status_store),
 __ATTR(msi_bus, 0644, msi_bus_show, msi_bus_store),
#ifdef CONFIG_HOTPLUG
 __ATTR(remove, (S_IWUSR|S_IWGRP), NULL, remove_store),
 __ATTR(rescan, (S_IWUSR|S_IWGRP), NULL, dev_rescan_store),
#endif
 __ATTR_NULL,
};

rescan写操作对应处理函数
static ssize_t dev_rescan_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
{
 unsigned long val;
 struct pci_dev *pdev = to_pci_dev(dev);

 if (strict_strtoul(buf, 0, &val) < 0)
  return -EINVAL;

 if (val) {
  mutex_lock(&pci_remove_rescan_mutex);
  pci_rescan_bus(pdev->bus);
  mutex_unlock(&pci_remove_rescan_mutex);
 }
 return count;
}

<drivers/pci/probe.c>
重新扫描选定PCI总线,配置新设备和向系统添加新设备
unsigned int __ref pci_rescan_bus(struct pci_bus *bus)
{
 unsigned int max;
 struct pci_dev *dev;

 max = pci_scan_child_bus(bus);

 down_read(&pci_bus_sem);
 list_for_each_entry(dev, &bus->devices, bus_list)
  if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
      dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
   if (dev->subordinate)
    pci_bus_size_bridges(dev->subordinate);
 up_read(&pci_bus_sem);

 pci_bus_assign_resources(bus);
 pci_enable_bridges(bus);
 pci_bus_add_devices(bus);

 return max;
}
EXPORT_SYMBOL_GPL(pci_rescan_bus);

2. 应用
echo 1 > /sys/bus/pci/devices/0000:00:1e.0/rescan

  • 0
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值