Chinaunix首页 | 论坛 | 博客
  • 博客访问: 560258
  • 博文数量: 252
  • 博客积分: 1068
  • 博客等级: 少尉
  • 技术积分: 1775
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-05 21:33
文章分类

全部博文(252)

文章存档

2013年(21)

2012年(231)

分类:

2012-06-10 10:39:09

        网络驱动是一种典型的PCI设备驱动,无论在嵌入式平台还是在PC领域,网络相关的项目开发有着比较广阔的前景,因此,分析当前Linux内核中网络设备的驱动,不但能了解网络相关的基本原理,而且可以借鉴Linux内核的先进的技术,将其应用到嵌入式或其他领域。本文以Linux内核中的rtl8139网络驱动为例,对网络驱动的源码进行了简单分析,并对其中涉及的相关概念和技术进行了简单的介绍。

一、PCI设备驱动模型

       rtl8139是典型的PCI设备,Linux内核的PCI核心驱动为PCI驱动开发者提供了方便的系统接口,极大地方便了PCI设备驱动的开发。

1 pci设备驱动相关的数据结构

  • pci驱动描述结构体
  1. struct pci_driver {
  2.     struct list_head node; /*用于连接入pci驱动列表*/
  3.     char *name; /*pci驱动的名称*/
  4.     const struct pci_device_id *id_table; /* must be non-NULL for probe to be called *//*该驱动支持的pci设备*/
  5.     int (*probe) (struct pci_dev *dev, const struct pci_device_id *id); /* New device inserted */
  6.     void (*remove) (struct pci_dev *dev); /* Device removed (NULL if not a hot-plug capable driver) */
  7.     int (*suspend) (struct pci_dev *dev, pm_message_t state); /* Device suspended */
  8.     int (*suspend_late) (struct pci_dev *dev, pm_message_t state);
  9.     int (*resume_early) (struct pci_dev *dev);
  10.     int (*resume) (struct pci_dev *dev); /* Device woken up */
  11.     void (*shutdown) (struct pci_dev *dev);
  12.     struct pci_error_handlers *err_handler;
  13.     struct device_driver driver;
  14.     struct pci_dynids dynids;
  15. };

pci设备描述结构体

  1. struct pci_dev{
  2.     struct list_head bus_list; /* node in per-bus list */
  3.     struct pci_bus *bus; /* bus this device is on */
  4.     struct pci_bus *subordinate; /* bus this device bridges to */

  5.     void *sysdata; /* hook for sys-specific extension */
  6.     struct proc_dir_entry *procent; /* device entry in /proc/bus/pci */
  7.     struct pci_slot *slot; /* Physical slot this device is in */

  8.     unsigned int devfn; /* encoded device & function index */
  9.     unsigned short vendor;
  10.     unsigned short device;
  11.     unsigned short subsystem_vendor;
  12.     unsigned short subsystem_device;
  13.     unsigned int class; /* 3 bytes: (base,sub,prog-if) */
  14.     u8 revision; /* PCI revision, low byte of class word */
  15.     u8 hdr_type; /* PCI header type (`multi' flag masked out) */
  16.     u8 pcie_cap; /* PCI-E capability offset */
  17.     u8 pcie_type; /* PCI-E device/port type */
  18.     u8 rom_base_reg; /* which config register controls the ROM */
  19.     u8 pin; /* which interrupt pin this device uses */

  20.     struct pci_driver *driver; /* which driver has allocated this device */
  21.     u64 dma_mask; /* Mask of the bits of bus address this
  22.                        device implements. Normally this is
  23.                        0xffffffff. You only need to change
  24.                        this if your device has broken DMA
  25.                        or supports 64-bit transfers. */

  26.     struct device_dma_parameters dma_parms;

  27.     pci_power_t current_state; /* Current operating state. In ACPI-speak,
  28.                        this is D0-D3, D0 being fully functional,
  29.                        and D3 being off. */
  30.     int pm_cap; /* PM capability offset in the
  31.                        configuration space */
  32.     unsigned int pme_support:5; /* Bitmask of states from which PME#
  33.                        can be generated */
  34.     unsigned int pme_interrupt:1;
  35.     unsigned int d1_support:1; /* Low power state D1 is supported */
  36.     unsigned int d2_support:1; /* Low power state D2 is supported */
  37.     unsigned int no_d1d2:1; /* Only allow D0 and D3 */
  38.     unsigned int wakeup_prepared:1;
  39.     unsigned int d3_delay; /* D3->D0 transition time in ms */

  40. #ifdef CONFIG_PCIEASPM
  41.     struct pcie_link_state *link_state; /* ASPM link state. */
  42. #endif

  43.     pci_channel_state_t error_state; /* current connectivity state */
  44.     struct device dev; /* Generic device interface */

  45.     int cfg_size; /* Size of configuration space */

  46.     /*
  47.      * Instead of touching interrupt line and base address registers
  48.      * directly, use the values stored here. They might be
  49.      */
  50.     unsigned int irq;
  51.     struct resource resource[DEVICE_COUNT_RESOURCE]; /* I/O and memory regions + expansion ROMs */
  52.     resource_size_t fw_addr[DEVICE_COUNT_RESOURCE]; /* FW-assigned addr */

  53.     /* These fields are used by common fixups */
  54.     unsigned int transparent:1; /* Transparent PCI bridge */
  55.     unsigned int multifunction:1;/* Part of multi-function device */
  56.     /* keep track of device state */
  57.     unsigned int is_added:1;
  58.     unsigned int is_busmaster:1; /* device is busmaster */
  59.     unsigned int no_msi:1; /* device may not use msi */
  60.     unsigned int block_ucfg_access:1; /* userspace config space access is blocked */
  61.     unsigned int broken_parity_status:1; /* Device generates false positive parity */
  62.     unsigned int irq_reroute_variant:2; /* device needs IRQ rerouting variant */
  63.     unsigned int msi_enabled:1;
  64.     unsigned int msix_enabled:1;
  65.     unsigned int ari_enabled:1; /* ARI forwarding */
  66.     unsigned int is_managed:1;
  67.     unsigned int is_pcie:1; /* Obsolete. Will be removed.
  68.                        Use pci_is_pcie() instead */
  69.     unsigned int needs_freset:1; /* Dev requires fundamental reset */
  70.     unsigned int state_saved:1;
  71.     unsigned int is_physfn:1;
  72.     unsigned int is_virtfn:1;
  73.     unsigned int reset_fn:1;
  74.     unsigned int is_hotplug_bridge:1;
  75.     unsigned int __aer_firmware_first_valid:1;
  76.     unsigned int __aer_firmware_first:1;
  77.     pci_dev_flags_t dev_flags;
  78.     atomic_t enable_cnt; /* pci_enable_device has been called */

  79.     u32 saved_config_space[16]; /* config space saved at suspend time */
  80.     struct hlist_head saved_cap_space;
  81.     struct bin_attribute *rom_attr; /* attribute descriptor for sysfs ROM entry */
  82.     int rom_attr_enabled; /* has display of the rom attribute been enabled? */
  83.     struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */
  84.     struct bin_attribute *res_attr_wc[DEVICE_COUNT_RESOURCE]; /* sysfs file for WC mapping of resources */
  85. #ifdef CONFIG_PCI_MSI
  86.     struct list_head msi_list;
  87. #endif
  88.     struct pci_vpd *vpd;
  89. #ifdef CONFIG_PCI_IOV
  90.     union {
  91.         struct pci_sriov *sriov; /* SR-IOV capability related */
  92.         struct pci_dev *physfn; /* the PF this VF is associated with */
  93.     };
  94.     struct pci_ats *ats; /* Address Translation Service */
  95. #endif
  96. }

    驱动开发者要想为某个PCI设备开发驱动就必须定义一个与当前PCI设备相对应的pci_driver数据结构,用来描述将要开发的pci驱动的相关信息,比如驱动的名称,当前驱动可以支持哪些设备,以及当前驱动支持的一些操作等,类似地,还需要有个结构体来表示PCI设备,描述PCI设备的硬件信息,如厂商ID,设备ID,以及各种资源等,详见注释。

二、PCI核心驱动API

Linux内核的PCI驱动为PCI设备驱动的开发提供了方便的结构,下面列举几个常用的接口:

pci_register_driver(struct pci_driver *drv)

功能:注册PCI驱动,参数为要注册的pci驱动的结构体。

下面来详细的分析以下这个函数,如此,才能更清楚的了解驱动和设备的匹配过程。

  1. pci_register_driver->driver_register(&drv->driver);->bus_add_driver->driver_attach->bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);

在这个过程中有涉及到一个更为抽象的结构体struct device_driver,它是pci_driver的更高级的抽象,即下层是pci_driver,其上是device_driver,这符合通常的程序设计逻辑,越往上层抽象级别越高,因为在操作系统看来,它并不需要知道具体是什么设备,所有的设备对操作系统来说都是相同的,即都用struct device_driver来表示。

在driver_register中先调用driver_find(drv->name, drv->bus),首先在相应的总线上查找drv->name的驱动是否已经被注册过,如果被注册过则返回,否则进行注册过程,即调用bus_add_driver(drv)。

int bus_add_driver(struct device_driver *drv)函数首先判断当前总线是否支持自动探测,如果执行则执行探测函数driver_attach(drv)。

  1. if (drv->bus->p->drivers_autoprobe) {
  2.         error = driver_attach(drv);
  3.         if (error)
  4.             goto out_unregister;
  5.     }int driver_attach(struct device_driver *drv)
  6. {
  7.     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  8. }

这个函数对PCI总线的上所有已经连接的PCI设备与当前的PCI驱动进程一次匹配的过程,即对每一个PCI设备都调用匹配函数__driver_attach。

  1. static int __driver_attach(struct device *dev, void *data)
  2. {
  3.     struct device_driver *drv = data;

  4.     /*
  5.      * Lock device and try to bind to it. We drop the error
  6.      * here and always return 0, because we need to keep trying
  7.      * to bind to devices and some drivers will return an error
  8.      * simply if it didn't support the device.
  9.      *
  10.      * driver_probe_device() will spit a warning if there
  11.      * is an error.
  12.      */

  13.     if (!driver_match_device(drv, dev))
  14.         return 0;

  15.     if (dev->parent) /* Needed for USB */
  16.         device_lock(dev->parent);
  17.     device_lock(dev);
  18.     if (!dev->driver)
  19.         driver_probe_device(drv, dev);
  20.     device_unlock(dev);
  21.     if (dev->parent)
  22.         device_unlock(dev->parent);

  23.     return 0;
  24. }

该函数首先判断总线提供的match函数是否为空,如果非空则执行总线提供的match函数,在rtl8139网络驱动中,match非空,参见代码:

  1. drv->driver.bus = &pci_bus_type;struct bus_type pci_bus_type = {
  2.     .name = "pci",
  3.     .match = pci_bus_match,
  4.     .uevent = pci_uevent,
  5.     .probe = pci_device_probe,
  6.     .remove = pci_device_remove,
  7.     .shutdown = pci_device_shutdown,
  8.     .dev_attrs = pci_dev_attrs,
  9.     .bus_attrs = pci_bus_attrs,
  10.     .pm = PCI_PM_OPS_PTR,
  11. };

这里将match函数即pci_bus_match,最后该函数调用到

  1. static inline const struct pci_device_id *
  2. pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev)
  3. {
  4.     if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&
  5.         (id->device == PCI_ANY_ID || id->device == dev->device) &&
  6.         (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&
  7.         (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&
  8.         !((id->class ^ dev->class) & id->class_mask))
  9.         return id;
  10.     return NULL;
  11. }

在这里进行了pci_driver和pci_dev的匹配,如果匹配成功,则返回pci_device_id。如果匹配不成功,而且当前设备还没有驱动,则调用driver_probe_device(drv,dev)。

  1. int driver_probe_device(struct device_driver *drv, struct device *dev)
  2. {
  3.     int ret = 0;

  4.     if (!device_is_registered(dev))
  5.         return -ENODEV;

  6.     pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  7.          drv->bus->name, __func__, dev_name(dev), drv->name);

  8.     pm_runtime_get_noresume(dev);
  9.     pm_runtime_barrier(dev);
  10.     ret = really_probe(dev, drv);
  11.     pm_runtime_put_sync(dev);

  12.     return ret;
  13. }

执行到这里说明,说明PCI总线没有提供match函数或者总线提供的match函数返回非空。还需要进行更深层次的探测,至少在总线提供的match函数中仅仅是进行了匹配,并没有将驱动和设备关联起来,这些操作就是在下面的函数中实现的。

  1. int driver_probe_device(struct device_driver *drv, struct device *dev)
  2. {
  3.     int ret = 0;

  4.     if (!device_is_registered(dev))
  5.         return -ENODEV;

  6.     pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  7.          drv->bus->name, __func__, dev_name(dev), drv->name);

  8.     pm_runtime_get_noresume(dev);
  9.     pm_runtime_barrier(dev);
  10.     ret = really_probe(dev, drv);
  11.     pm_runtime_put_sync(dev);

  12.     return ret;
  13. }

重点看really_probe函数:

 

  1. static int really_probe(struct device *dev, struct device_driver *drv)
  2. {
  3.     int ret = 0;

  4.     atomic_inc(&probe_count);
  5.     pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  6.          drv->bus->name, __func__, drv->name, dev_name(dev));
  7.     WARN_ON(!list_empty(&dev->devres_head));

  8.     dev->driver = drv;
  9.     if (driver_sysfs_add(dev)) {
  10.         printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  11.             __func__, dev_name(dev));
  12.         goto probe_failed;
  13.     }

  14.     if (dev->bus->probe) {
  15.         ret = dev->bus->probe(dev);
  16.         if (ret)
  17.             goto probe_failed;
  18.     } else if (drv->probe) {
  19.         ret = drv->probe(dev);
  20.         if (ret)
  21.             goto probe_failed;
  22.     }

  23.     driver_bound(dev);
  24.     ret = 1;
  25.     pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  26.          drv->bus->name, __func__, dev_name(dev), drv->name);
  27.     goto done;

  28. probe_failed:
  29.     devres_release_all(dev);
  30.     driver_sysfs_remove(dev);
  31.     dev->driver = NULL;

  32.     if (ret != -ENODEV && ret != -ENXIO) {
  33.         /* driver matched but the probe failed */
  34.         printk(KERN_WARNING
  35.                "%s: probe of %s failed with error %d\n",
  36.                drv->name, dev_name(dev), ret);
  37.     }
  38.     /*
  39.      * Ignore errors returned by ->probe so that the next driver can try
  40.      * its luck.
  41.      */
  42.     ret = 0;
  43. done:
  44.     atomic_dec(&probe_count);
  45.     wake_up(&probe_waitqueue);
  46.     return ret;
  47. }

 

在此函数中,首先将驱动和设备关联起来,即红色代码dev->driver = drv; 指明了当前设备的驱动。按照常规的程序设计思想,驱动和设备关联后是否还需要做一些其他工作才能是设备在相应的驱动下正常工作呢,这就是probe函数实现的功能了,很明显设备和驱动获取都需要做一些工作,因此这里分别留出设备和驱动的probe函数。其中设备的probe即设备所在总线的probe,这里暂且不去分析,因为与网络驱动关系不大,都是PCI总线相关的东西,重点来看驱动的probe,在前面提到的pci_driver结构体中,对于rtl8139驱动来说,其pci_driver结构体被初始化为:

  1. static struct pci_driver rtl8139_pci_driver = {
  2.     .name = DRV_NAME,
  3.     .id_table = rtl8139_pci_tbl,
  4.     .probe = rtl8139_init_one,
  5.     .remove = __devexit_p(rtl8139_remove_one),
  6. #ifdef CONFIG_PM
  7.     .suspend = rtl8139_suspend,
  8.     .resume = rtl8139_resume,
  9. #endif /* CONFIG_PM */
  10. };

这里即调用rtl8139_init_one,经过上面的逐层分析,我们从pci核心驱动一步一步的走到了rtl8139网络设备的驱动,豁然开朗了,以后看网络驱动的时候就不会感到开始的地方有点迷糊了。代码分析重在代码之间的过渡,如果衔接不好,很多地方都会产生疑问。在下一篇文章中,将会详细分析rtl8139网络驱动的代码。

阅读(580) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~