Chinaunix首页 | 论坛 | 博客
  • 博客访问: 33992
  • 博文数量: 14
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2022-11-22 23:41
个人简介

将分享技术博文作为一种快乐,提升自己帮助他人

文章分类

全部博文(14)

文章存档

2023年(9)

2022年(5)

我的朋友

分类: LINUX

2022-12-13 07:41:49

熟悉Linux内核的同学都知道,设备和驱动都是挂在总线上的,一个驱动可以匹配多个设备,设备和驱动一旦匹配之后,就会调用驱动的probe函数对设备进行初始化。DPDK全称叫数据平面开发套件,它运行在用户态,里面也包含着许多设备驱动。那么DPDK中的总线、设备和驱动,又是使用什么模型呢?直接了当的说,DPDK中总线、设备和驱动的模型和Linux内核是一样的,加载方式也是类似的。下面主要介绍DPDK的总线注册、驱动注册和设备扫描挂载,以及设备和驱动匹配之后的设备探测。

总线注册

DPDK中有一个全局链表rte_bus_list,该链表默认初始化成空链表,结构和声明如下。

点击(此处)折叠或打开

  1. lib\eal\include\bus_driver.h
  2. struct rte_bus_list {
  3.     struct rte_bus *tqh_first; /* first element */
  4.     struct rte_bus **tqh_last; /* addr of last next element */
  5.     TRACEBUF
  6. }

  7. lib\eal\common\eal_common_bus.c
  8. static struct rte_bus_list rte_bus_list =
  9.     TAILQ_HEAD_INITIALIZER(rte_bus_list);
DPDK-22.11中支持pci、vdev、vmbux和auxiliary等8种总线。
这些总线都是通过RTE_REGISTER_BUS宏函数,将对应的bus插入到rte_bus_list链表中,相关定义如下所示。

点击(此处)折叠或打开

  1. lib\eal\common\eal_common_bus.c
  2. void
  3. rte_bus_register(struct rte_bus *bus)
  4. {
  5.     RTE_VERIFY(bus);
  6.     RTE_VERIFY(rte_bus_name(bus) && strlen(rte_bus_name(bus)));
  7.     /* A bus should mandatorily have the scan implemented */
  8.     RTE_VERIFY(bus->scan);
  9.     RTE_VERIFY(bus->probe);
  10.     RTE_VERIFY(bus->find_device);
  11.     /* Buses supporting driver plug also require unplug. */
  12.     RTE_VERIFY(!bus->plug || bus->unplug);

  13.     TAILQ_INSERT_TAIL(&rte_bus_list, bus, next);
  14.     RTE_LOG(DEBUG, EAL, "Registered [%s] bus.\n", rte_bus_name(bus));
  15. }

  16. lib\eal\include\bus_driver.h
  17. #define RTE_REGISTER_BUS(nm, bus) \
  18. RTE_INIT_PRIO(businitfn_ ##nm, BUS) \
  19. {\
  20.     (bus).name = RTE_STR(nm);\
  21.     rte_bus_register(&bus); \
  22. }
RTE_REGISTER_BUS中nm表示bus的名字,bus表示通用的struct rte_bus,该结构体定义如下:

点击(此处)折叠或打开

  1. struct rte_bus {
  2.     RTE_TAILQ_ENTRY(rte_bus) next; /**< Next bus object in linked list */
  3.     const char *name; /**< Name of the bus */
  4.     rte_bus_scan_t scan; /**< Scan for devices attached to bus */
  5.     rte_bus_probe_t probe; /**< Probe devices on bus */
  6.     rte_bus_find_device_t find_device; /**< Find a device on the bus */
  7.     rte_bus_plug_t plug; /**< Probe single device for drivers */
  8.     rte_bus_unplug_t unplug; /**< Remove single device from driver */
  9.     rte_bus_parse_t parse; /**< Parse a device name */
  10.     rte_bus_devargs_parse_t devargs_parse; /**< Parse bus devargs */
  11.     rte_dev_dma_map_t dma_map; /**< DMA map for device in the bus */
  12.     rte_dev_dma_unmap_t dma_unmap; /**< DMA unmap for device in the bus */
  13.     struct rte_bus_conf conf; /**< Bus configuration */
  14.     rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu class */
  15.     rte_dev_iterate_t dev_iterate; /**< Device iterator. */
  16.     rte_bus_hot_unplug_handler_t hot_unplug_handler;
  17.                 /**< handle hot-unplug failure on the bus */
  18.     rte_bus_sigbus_handler_t sigbus_handler;
  19.                     /**< handle sigbus error on the bus */
  20.     rte_bus_cleanup_t cleanup; /**< Cleanup devices on bus */
  21. };

该结构是对所有总线的一个抽象,但并不是各类总线都需要支持所有的钩子。但所有总线都会实现scan、probe和find_device钩子,如rte_bus_register()接口校验所示。对某类总线的抽象,除了rte_bus对象之外,一般都还会维护一个设备列表和驱动列表,如rte_pci_bus的结构如下:

点击(此处)折叠或打开

  1. struct rte_pci_bus {
  2.   struct rte_bus bus;               /**< Inherit the generic class */
  3.   RTE_TAILQ_HEAD(, rte_pci_device) device_list; /**< List of PCI devices */
  4.   RTE_TAILQ_HEAD(, rte_pci_driver) driver_list; /**< List of PCI drivers */
  5. };

  6. struct rte_pci_bus rte_pci_bus = {
  7.     .bus = {
  8.         .scan = rte_pci_scan,
  9.         .probe = pci_probe,
  10.         .cleanup = pci_cleanup,
  11.         .find_device = pci_find_device,
  12.         .plug = pci_plug,
  13.         .unplug = pci_unplug,
  14.         .parse = pci_parse,
  15.         .devargs_parse = rte_pci_devargs_parse,
  16.         .dma_map = pci_dma_map,
  17.         .dma_unmap = pci_dma_unmap,
  18.         .get_iommu_class = rte_pci_get_iommu_class,
  19.         .dev_iterate = rte_pci_dev_iterate,
  20.         .hot_unplug_handler = pci_hot_unplug_handler,
  21.         .sigbus_handler = pci_sigbus_handler,
  22.     },
  23.     .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
  24.     .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
  25. };

  26. RTE_REGISTER_BUS(pci, rte_pci_bus.bus);
另外,还需说明的是,各类总线的注册不是在rte_eal_init()中完成的,而是在执行DPDK主函数(main函数)前就注册好的。

驱动注册

DPDK中的设备驱动和Linux内核设备驱动也是类似的。各种总线定义了该总线上注册驱动的接口,如下图所示:
每个总线上的设备驱动,一般会在驱动主文件的末尾调用对应的钩子注册驱动,如vdev总线上的bonding和failsafe驱动,注册代码实现如下。

点击(此处)折叠或打开

  1. drivers\net\bonding\rte_eth_bond_pmd.c
  2. struct rte_vdev_driver pmd_bond_drv = {
  3.     .probe = bond_probe,
  4.     .remove = bond_remove,
  5. };
  6. RTE_PMD_REGISTER_VDEV(net_bonding, pmd_bond_drv);

  7. drivers\net\failsafe\failsafe.c
  8. static struct rte_vdev_driver failsafe_drv = {
  9.     .probe = rte_pmd_failsafe_probe,
  10.     .remove = rte_pmd_failsafe_remove,
  11. };

  12. RTE_PMD_REGISTER_VDEV(net_failsafe, failsafe_drv);
驱动的注册实现基本都是类似的,下面以鲲鹏920系列处理器上的内置PCI网卡驱动为例来说明。hns3驱动的rte_pci_driver中定义的id_table,每个硬件网卡设备的vendor_id和device_id是不同的,使得一个驱动可以支持多种设备。

点击(此处)折叠或打开

  1. static const struct rte_pci_id pci_id_hns3_map[] = {
  2.     { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_GE) },
  3.     { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE) },
  4.     { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_25GE_RDMA) },
  5.     { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_50GE_RDMA) },
  6.     { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_100G_RDMA_MACSEC) },
  7.     { RTE_PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, HNS3_DEV_ID_200G_RDMA) },
  8.     { .vendor_id = 0, }, /* sentinel */
  9. };

  10. static struct rte_pci_driver rte_hns3_pmd = {
  11.     .id_table = pci_id_hns3_map,
  12.     .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
  13.     .probe = eth_hns3_pci_probe,
  14.     .remove = eth_hns3_pci_remove,
  15. };

  16. RTE_PMD_REGISTER_PCI(net_hns3, rte_hns3_pmd);

rte_hns3_pmd是通过RTE_PMD_REGISTER_PCI()接口注册,并将rte_hns_pmd驱动挂接到rte_pci_bus下的驱动列表上。设备驱动的注册里面也是使用RTE_INIT宏定义constructor属性函数,其执行动作也在DPDK的主函数之前就完成的,但其设置了的优先级比总线注册的优先级低。因此,驱动注册完成于主函数执行前,总线注册之后。

设备挂载

设备挂载很多博文中都有介绍,但随着DPDK版本演进,中间过程有变化。本节主要基于刚发布的的DPDK-22.11版本来进行介绍。设备扫描挂载是在主函数中调用的rte_eal_init()中完成的。rte_eal_init()会调用rte_bus_scan(),该函数实现如下:

点击(此处)折叠或打开

  1. int
  2. rte_bus_scan(void)
  3. {
  4.     int ret;
  5.     struct rte_bus *bus = NULL;

  6.     TAILQ_FOREACH(bus, &rte_bus_list, next) {
  7.         ret = bus->scan();
  8.         if (ret)
  9.             RTE_LOG(ERR, EAL, "Scan for (%s) bus failed.\n",
  10.                 rte_bus_name(bus));
  11.     }

  12.     return 0;
  13. }
从该函数实现可以看出,eal层初始化时,会遍历全局总线链表,调用总线上的scan()接口扫描总线上的设备。下面以rte_pci_bus上的扫描过程为例进行介绍,该总线的scan()接口为rte_pci_scan(),该函数实现如下:

点击(此处)折叠或打开

  1. int
  2. rte_pci_scan(void)
  3. {
  4.     struct dirent *e;
  5.     DIR *dir;
  6.     char dirname[PATH_MAX];
  7.     struct rte_pci_addr addr;

  8.     /* for debug purposes, PCI can be disabled */
  9.     if (!rte_eal_has_pci())
  10.         return 0;

  11.     /* 打开/sys/bus/pci/devices/目录 */
  12.     dir = opendir(rte_pci_get_sysfs_path());
  13.     if (dir == NULL) {
  14.         RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
  15.             __func__, strerror(errno));
  16.         return -1;
  17.     }

  18.     /* 读取目录下所有的子目录,每个子目录为一个pci设备 */
  19.     while ((= readdir(dir)) != NULL) {
  20.         if (e->d_name[0] == '.')
  21.             continue;

  22.         /* 根据目录名,得到rte_pci_addr中的domain、bus、devid和function */
  23.         if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &addr) != 0)
  24.             continue;
  25.         
  26.         /*
  27.          * 如果不在白名单中设备则不会扫描,通过-b/-a控制,
  28.          * 如果都不指定则扫描被uio驱动接管的所有PCI设备。
  29.          */
  30.         if (rte_pci_ignore_device(&addr))
  31.             continue;

  32.         snprintf(dirname, sizeof(dirname), "%s/%s",
  33.                 rte_pci_get_sysfs_path(), e->d_name);
  34.         
  35.         /* 扫描子目录文件夹 */
  36.         if (pci_scan_one(dirname, &addr) < 0)
  37.             goto error;
  38.     }
  39.     closedir(dir);
  40.     return 0;

  41. error:
  42.     closedir(dir);
  43.     return -1;
  44. }
/sys/bus/pci/devices/目录中每个子目录文件如下图所示。注意该文件夹的顺序是随着BDF号(pci设备地址)的顺序从小到大依次排列的。
每个子设备文件夹下的文件包含了该设备下的许多信息,如下图所示。
而rte_pci_device的结构域段就是和上图中的文件是类似的,如下图所示:

pci_scan_one()函数就是解析设备文件夹中的每个文件信息,从而初始化申请的rte_pci_device,并将其挂载到rte_pci_bus总线上的设备列表上,具体解析代码实现细节如下:

点击(此处)折叠或打开

  1. static int
  2. pci_scan_one(const char *dirname, const struct rte_pci_addr *addr)
  3. {
  4.     char filename[PATH_MAX];
  5.     unsigned long tmp;
  6.     struct rte_pci_device *dev;
  7.     char driver[PATH_MAX];
  8.     int ret;

  9.     dev = malloc(sizeof(*dev));
  10.     if (dev == NULL)
  11.         return -1;

  12.     memset(dev, 0, sizeof(*dev));
  13.     dev->device.bus = &rte_pci_bus.bus;
  14.     dev->addr = *addr;

  15.     /* get vendor id */
  16.     snprintf(filename, sizeof(filename), "%s/vendor", dirname);
  17.     if (eal_parse_sysfs_value(filename, &tmp) < 0) {
  18.         pci_free(dev);
  19.         return -1;
  20.     }
  21.     dev->id.vendor_id = (uint16_t)tmp;

  22.     /* get device id */
  23.     snprintf(filename, sizeof(filename), "%s/device", dirname);
  24.     if (eal_parse_sysfs_value(filename, &tmp) < 0) {
  25.         pci_free(dev);
  26.         return -1;
  27.     }
  28.     dev->id.device_id = (uint16_t)tmp;

  29.     /* get subsystem_vendor id */
  30.     snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
  31.          dirname);
  32.     if (eal_parse_sysfs_value(filename, &tmp) < 0) {
  33.         pci_free(dev);
  34.         return -1;
  35.     }
  36.     dev->id.subsystem_vendor_id = (uint16_t)tmp;

  37.     /* get subsystem_device id */
  38.     snprintf(filename, sizeof(filename), "%s/subsystem_device",
  39.          dirname);
  40.     if (eal_parse_sysfs_value(filename, &tmp) < 0) {
  41.         pci_free(dev);
  42.         return -1;
  43.     }
  44.     dev->id.subsystem_device_id = (uint16_t)tmp;

  45.     /* get class_id */
  46.     snprintf(filename, sizeof(filename), "%s/class",
  47.          dirname);
  48.     if (eal_parse_sysfs_value(filename, &tmp) < 0) {
  49.         pci_free(dev);
  50.         return -1;
  51.     }
  52.     /* the least 24 bits are valid: class, subclass, program interface */
  53.     dev->id.class_id = (uint32_t)tmp & RTE_CLASS_ANY_ID;

  54.     /* get max_vfs */
  55.     dev->max_vfs = 0;
  56.     snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
  57.     if (!access(filename, F_OK) &&
  58.      eal_parse_sysfs_value(filename, &tmp) == 0)
  59.         dev->max_vfs = (uint16_t)tmp;
  60.     else {
  61.         /* for non igb_uio driver, need kernel version >= 3.*/
  62.         snprintf(filename, sizeof(filename),
  63.              "%s/sriov_numvfs", dirname);
  64.         if (!access(filename, F_OK) &&
  65.          eal_parse_sysfs_value(filename, &tmp) == 0)
  66.             dev->max_vfs = (uint16_t)tmp;
  67.     }

  68.     /* get numa node, default to 0 if not present */
  69.     snprintf(filename, sizeof(filename), "%s/numa_node", dirname);

  70.     if (access(filename, F_OK) == 0 &&
  71.      eal_parse_sysfs_value(filename, &tmp) == 0)
  72.         dev->device.numa_node = tmp;
  73.     else
  74.         dev->device.numa_node = SOCKET_ID_ANY;

  75.     pci_common_set(dev);

  76.     /* parse resources */
  77.     snprintf(filename, sizeof(filename), "%s/resource", dirname);
  78.     if (pci_parse_sysfs_resource(filename, dev) < 0) {
  79.         RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
  80.         pci_free(dev);
  81.         return -1;
  82.     }

  83.     /* parse driver */
  84.     snprintf(filename, sizeof(filename), "%s/driver", dirname);
  85.     ret = pci_get_kernel_driver_by_path(filename, driver, sizeof(driver));
  86.     if (ret < 0) {
  87.         RTE_LOG(ERR, EAL, "Fail to get kernel driver\n");
  88.         pci_free(dev);
  89.         return -1;
  90.     }

  91.     if (!ret) {
  92.         if (!strcmp(driver, "vfio-pci"))
  93.             dev->kdrv = RTE_PCI_KDRV_VFIO;
  94.         else if (!strcmp(driver, "igb_uio"))
  95.             dev->kdrv = RTE_PCI_KDRV_IGB_UIO;
  96.         else if (!strcmp(driver, "uio_pci_generic"))
  97.             dev->kdrv = RTE_PCI_KDRV_UIO_GENERIC;
  98.         else
  99.             dev->kdrv = RTE_PCI_KDRV_UNKNOWN;
  100.     } else {
  101.         pci_free(dev);
  102.         return 0;
  103.     }
  104.     /* device is valid, add in list (sorted) */
  105.     if (TAILQ_EMPTY(&rte_pci_bus.device_list)) {
  106.         /* 总线设备列表为空,直接将设备添加到总线上 */
  107.         rte_pci_add_device(dev);
  108.     } else {
  109.         struct rte_pci_device *dev2;
  110.         int ret;

  111.         TAILQ_FOREACH(dev2, &rte_pci_bus.device_list, next) {
  112.             ret = rte_pci_addr_cmp(&dev->addr, &dev2->addr);
  113.             if (ret > 0)
  114.                 continue;

  115.             if (ret < 0) {
  116.                 rte_pci_insert_device(dev2, dev);
  117.             } else { /* already registered */
  118.                 if (!rte_dev_is_probed(&dev2->device)) {
  119.                     dev2->kdrv = dev->kdrv;
  120.                     dev2->max_vfs = dev->max_vfs;
  121.                     dev2->id = dev->id;
  122.                     pci_common_set(dev2);
  123.                     memmove(dev2->mem_resource,
  124.                         dev->mem_resource,
  125.                         sizeof(dev->mem_resource));
  126.                 } else {
  127.                     /**
  128.                      * If device is plugged and driver is
  129.                      * probed already, (This happens when
  130.                      * we call rte_dev_probe which will
  131.                      * scan all device on the bus) we don't
  132.                      * need to do anything here unless...
  133.                      **/
  134.                     if (dev2->kdrv != dev->kdrv ||
  135.                         dev2->max_vfs != dev->max_vfs ||
  136.                         memcmp(&dev2->id, &dev->id, sizeof(dev2->id)))
  137.                         /*
  138.                          * This should not happens.
  139.                          * But it is still possible if
  140.                          * we unbind a device from
  141.                          * vfio or uio before hotplug
  142.                          * remove and rebind it with
  143.                          * a different configure.
  144.                          * So we just print out the
  145.                          * error as an alarm.
  146.                          */
  147.                         RTE_LOG(ERR, EAL, "Unexpected device scan at %s!\n",
  148.                             filename);
  149.                     else if (dev2->device.devargs !=
  150.                          dev->device.devargs) {
  151.                         rte_devargs_remove(dev2->device.devargs);
  152.                         pci_common_set(dev2);
  153.                     }
  154.                 }
  155.                 pci_free(dev);
  156.             }
  157.             return 0;
  158.         }
  159.         /* rte_pci_device初始化完成后,将设备添加到总线上 */
  160.         rte_pci_add_device(dev);
  161.     }

  162.     return 0;
  163. }
以上过程的具体细节在此不进行展开,主要提一下以下几点:1)driver指定了当前设备的驱动,DPDK支持三种托管设备到用户态的uio驱动,即vfio_pci、igb_uio和uio_pci_generic;2) struct rte_pci_id中的信息,后面会用它和驱动的id_table进行设备和驱动的匹配;3)  resource文件,该文件保存了PCI设备的Bar0-5的地址空间,其在pci_parse_sysfs_resource函数中被解析并保存到rte_pci_device中的mem_resource[]中。综上可以看出,设备就是按照以上方式将PCI设备按照BDF号从小到大的顺序将设备挂到rte_pci_bus总线上。 

设备探测

设备扫描完成后,在rte_eal_init()中的后面就调用rte_bus_probe()函数,遍历所有总线,调用总线的probe函数,代码如下:

点击(此处)折叠或打开

  1. int
  2. rte_bus_probe(void)
  3. {
  4.     int ret;
  5.     struct rte_bus *bus, *vbus = NULL;

  6.     TAILQ_FOREACH(bus, &rte_bus_list, next) {
  7.         if (!strcmp(rte_bus_name(bus), "vdev")) {
  8.             vbus = bus;
  9.             continue;
  10.         }

  11.         ret = bus->probe();
  12.         if (ret)
  13.             RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
  14.                 rte_bus_name(bus));
  15.     }

  16.     if (vbus) {
  17.         ret = vbus->probe();
  18.         if (ret)
  19.             RTE_LOG(ERR, EAL, "Bus (%s) probe failed.\n",
  20.                 rte_bus_name(vbus));
  21.     }

  22.     return 0;
  23. }
下面仍然以rte_pci_bus总线为例说明pci设备的探测过程。pci_probe()会按照PCI设备挂载顺序为该总线上的每个设备遍历总线上的所有驱动找到对应的驱动,代码实现如下:

点击(此处)折叠或打开

  1. static int
  2. pci_probe(void)
  3. {
  4.     struct rte_pci_device *dev = NULL;
  5.     size_t probed = 0, failed = 0;
  6.     int ret = 0;

  7.     FOREACH_DEVICE_ON_PCIBUS(dev) {
  8.         probed++;
  9.         /* 用该设备与所有驱动进行匹配 */
  10.         ret = pci_probe_all_drivers(dev);
  11.         if (ret < 0) {
  12.             if (ret != -EEXIST) {
  13.                 RTE_LOG(ERR, EAL, "Requested device "
  14.                     PCI_PRI_FMT " cannot be used\n",
  15.                     dev->addr.domain, dev->addr.bus,
  16.                     dev->addr.devid, dev->addr.function);
  17.                 rte_errno = errno;
  18.                 failed++;
  19.             }
  20.             ret = 0;
  21.         }
  22.     }

  23.     return (probed && probed == failed) ? -: 0;
  24. }
pci_probe_all_drivers中就会去遍历rte_pci_bus上的所有驱动,并调用rte_pci_probe_one_driver(dr, dev)进行设备和驱动的匹配。rte_pci_probe_one_driver的匹配细节如下代码:

点击(此处)折叠或打开

  1. static int
  2. rte_pci_probe_one_driver(struct rte_pci_driver *dr,
  3.              struct rte_pci_device *dev)
  4. {
  5.     int ret;
  6.     bool already_probed;
  7.     struct rte_pci_addr *loc;

  8.     if ((dr == NULL) || (dev == NULL))
  9.         return -EINVAL;

  10.     loc = &dev->addr;

  11.     /* 使用rte_pci_device中的rte_pci_id的vendor_id,device_id,
  12.      * subsystem_vendor_id,subsystem_device_id和class_id去和
  13.      * rte_pci_driver中的id table数组的每一个rte_pci_id进行匹配。
  14.      * 不匹配则直接返回 */
  15.     if (!rte_pci_match(dr, dev))
  16.         /* Match of device and driver failed */
  17.         return 1;

  18.     RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
  19.             loc->domain, loc->bus, loc->devid, loc->function,
  20.             dev->device.numa_node);

  21.     /* 设备被制定了-b,则返回,不对其进行初始化 */
  22.     if (dev->device.devargs != NULL &&
  23.         dev->device.devargs->policy == RTE_DEV_BLOCKED) {
  24.         RTE_LOG(INFO, EAL, " Device is blocked, not initializing\n");
  25.         return 1;
  26.     }

  27.     if (dev->device.numa_node < 0 && rte_socket_count() > 1)
  28.         RTE_LOG(INFO, EAL, "Device %s is not NUMA-aware\n", dev->name);

  29.     /* 检测设备是否已被探测过,如已被探测,而又不支持重复,则返回失败 */
  30.     already_probed = rte_dev_is_probed(&dev->device);
  31.     if (already_probed && !(dr->drv_flags & RTE_PCI_DRV_PROBE_AGAIN)) {
  32.         RTE_LOG(DEBUG, EAL, "Device %s is already probed\n",
  33.                 dev->device.name);
  34.         return -EEXIST;
  35.     }

  36.     RTE_LOG(DEBUG, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id,
  37.         dev->id.device_id, dr->driver.name);

  38.     if (!already_probed) {
  39.         enum rte_iova_mode dev_iova_mode;
  40.         enum rte_iova_mode iova_mode;
  41.         /* 通过使用的何种uio驱动,得到是iova还是pa模式 */
  42.         dev_iova_mode = pci_device_iova_mode(dr, dev);
  43.         iova_mode = rte_eal_iova_mode();
  44.         if (dev_iova_mode != RTE_IOVA_DC &&
  45.          dev_iova_mode != iova_mode) {
  46.             RTE_LOG(ERR, EAL, " Expecting '%s' IOVA mode but current mode is '%s', not initializing\n",
  47.                 dev_iova_mode == RTE_IOVA_PA ? "PA" : "VA",
  48.                 iova_mode == RTE_IOVA_PA ? "PA" : "VA");
  49.             return -EINVAL;
  50.         }

  51.         /* Allocate interrupt instance for pci device */
  52.         dev->intr_handle =
  53.             rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
  54.         if (dev->intr_handle == NULL) {
  55.             RTE_LOG(ERR, EAL,
  56.                 "Failed to create interrupt instance for %s\n",
  57.                 dev->device.name);
  58.             return -ENOMEM;
  59.         }

  60.         dev->vfio_req_intr_handle =
  61.             rte_intr_instance_alloc(RTE_INTR_INSTANCE_F_PRIVATE);
  62.         if (dev->vfio_req_intr_handle == NULL) {
  63.             rte_intr_instance_free(dev->intr_handle);
  64.             dev->intr_handle = NULL;
  65.             RTE_LOG(ERR, EAL,
  66.                 "Failed to create vfio req interrupt instance for %s\n",
  67.                 dev->device.name);
  68.             return -ENOMEM;
  69.         }

  70.         /*
  71.          * Reference driver structure.
  72.          * This needs to be before rte_pci_map_device(), as it enables
  73.          * to use driver flags for adjusting configuration.
  74.          */
  75.         dev->driver = dr;
  76.         /* 
  77.          * 驱动的drv_flag中有NEED_MAPPING才需要mapping设备的Bar空间。
  78.          * 一般是使用uio驱动托管设备到用户态的才需要用,
  79.          * 像dpaa2和mlx的驱动都不是通过这种方式托管的。
  80.          */
  81.         if (dev->driver->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
  82.             /*
  83.              * 该接口就会通过对应的uio驱动,mapping设备Bar空间得到Bar的
  84.              * 虚拟地址,保存在dev->mem_resource[i].addr中
  85.              */
  86.             ret = rte_pci_map_device(dev);
  87.             if (ret != 0) {
  88.                 dev->driver = NULL;
  89.                 rte_intr_instance_free(dev->vfio_req_intr_handle);
  90.                 dev->vfio_req_intr_handle = NULL;
  91.                 rte_intr_instance_free(dev->intr_handle);
  92.                 dev->intr_handle = NULL;
  93.                 return ret;
  94.             }
  95.         }
  96.     }

  97.     RTE_LOG(INFO, EAL, "Probe PCI driver: %s (%x:%x) device: "PCI_PRI_FMT" (socket %i)\n",
  98.             dr->driver.name, dev->id.vendor_id, dev->id.device_id,
  99.             loc->domain, loc->bus, loc->devid, loc->function,
  100.             dev->device.numa_node);
  101.     /* 调用驱动的probe()函数 */
  102.     ret = dr->probe(dr, dev);
  103.     if (already_probed)
  104.         return ret; /* no rollback if already succeeded earlier */
  105.     if (ret) {
  106.         dev->driver = NULL;
  107.         if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
  108.             /* Don't unmap if device is unsupported and
  109.              * driver needs mapped resources.
  110.              */
  111.             !(ret > 0 &&
  112.                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
  113.             rte_pci_unmap_device(dev);
  114.         rte_intr_instance_free(dev->vfio_req_intr_handle);
  115.         dev->vfio_req_intr_handle = NULL;
  116.         rte_intr_instance_free(dev->intr_handle);
  117.         dev->intr_handle = NULL;
  118.     } else {
  119.         /* 让设备的driver指针引用驱动的driver,方便获取驱动信息 */
  120.         dev->device.driver = &dr->driver;
  121.     }

  122.     return ret;
  123. }
rte_pci_match(dr, dev)就使用前面设备扫描过程中得到的设备pci_id信息和driver中的id_table进行匹配,若匹配则进行PCI设备Bar空间的映射,调用驱动的probe函数(115行)。
每个类设备驱动的probe函数执行也是类似的,下面以鲲鹏920系列处理器内置网卡hns3 PF驱动的probe过程做一个简要的介绍。hns3 PF驱动的probe函数实现如下:

点击(此处)折叠或打开

  1. static inline int
  2. rte_eth_dev_pci_generic_probe(struct rte_pci_device *pci_dev,
  3.     size_t private_data_size, eth_dev_pci_callback_t dev_init)
  4. {
  5.     struct rte_eth_dev *eth_dev;
  6.     int ret;
  7.     
  8.     /* 分配一个ethdev */
  9.     eth_dev = rte_eth_dev_pci_allocate(pci_dev, private_data_size);
  10.     if (!eth_dev)
  11.         return -ENOMEM;

  12.     if (*dev_init == NULL)
  13.         return -EINVAL;
  14.     
  15.     /* 调用驱动的dev_init函数,进行硬件设备初始化 */
  16.     ret = dev_init(eth_dev);
  17.     if (ret)
  18.         rte_eth_dev_release_port(eth_dev);
  19.     else
  20.         rte_eth_dev_probing_finish(eth_dev);

  21.     return ret;
  22. }

  23. static int
  24. eth_hns3_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
  25.          struct rte_pci_device *pci_dev)
  26. {
  27.     return rte_eth_dev_pci_generic_probe(pci_dev,
  28.                      sizeof(struct hns3_adapter),
  29.                      hns3_dev_init);
  30. }
对于网卡设备,上层应用看到的就是一个以太网设备(ethdev),上层应用都是通过ethdev对应的端口号,操作PCI网卡。该probe函数执行时,首先分配一个ethdev,然后调用驱动的dev_init()函数进行设备驱动中的默认初始化。特别指出的是,在hns3_dev_init()函数中会调用hns3_init_pf(),该接口会将PCI设备的Bar2地址作为硬件设备的io地址,如下所示:

点击(此处)折叠或打开

  1. static int
  2. hns3_init_pf(struct rte_eth_dev *eth_dev)
  3. {
  4.     struct hns3_adapter *hns = eth_dev->data->dev_private;
  5.     struct hns3_hw *hw = &hns->hw;
  6.     
  7.     ...
  8.     /* Get hardware io base address from pcie BAR2 IO space */
  9.     hw->io_base = pci_dev->mem_resource[2].addr;
  10.     ...
驱动通过该地址,就可在用户态直接读写设备寄存器。具体使用PCI设备的哪个Bar地址不固定,各厂商是有差异的。
驱动的probe函数执行完,则驱动和设备就探测成功了。但设备的初始化过程并没有完全完成,上层应用还需调用dev_configure接口下发配置,根据用户的配置重新配置硬件设备,再调用dev_start()接口,整个设备才全部初始化完成。

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