当前位置:   article > 正文

STMMAC驱动

stmmac

1. 注册平台驱动

1.1 设备树配置

  1. ethqos_hw: qcom,ethernet@20000 {
  2. compatible = "qcom,stmmac-ethqos";
  3. //以下属性都会在stmmac_probe_config_dt函数中进行解析
  4. snps,pbl = <32>;
  5. rx-fifo-depth = <16384>;
  6. tx-fifo-depth = <20480>;
  7. snps,mtl-rx-config = <&mtl_rx_setup>;
  8. snps,mtl-tx-config = <&mtl_tx_setup>;
  9. snps,reset = <&tlmm 79 GPIO_ACTIVE_HIGH>;
  10. qcom,phy-intr-redirect = <&tlmm 124 GPIO_ACTIVE_LOW>;
  11. gdsc_emac-supply = <&emac_gdsc>;
  12. snps,reset-delays-us = <0 11000 70000>;
  13. phy-mode = "rgmii"; //指定interface类型(比如rgmii或者rmii等)
  14. phy-handle = <&phy1>;
  15. mdio {//描述MDIO控制器驱动节点
  16. #address-cells = <1>;
  17. #size-cells = <0>;
  18. compatible = "snps,dwmac-mdio";
  19. phy1: ethernet-phy@3 {//描述控制器下挂PHY设备的节点
  20. compatible = "ethernet-phy-id002b.0b21", "ethernet-phy-ieee802.3-c22";
  21. reg = <3>;
  22. };
  23. };
  24. };

1.2 device和driver匹配

  1. static const struct of_device_id qcom_ethqos_match[] = {
  2. { .compatible = "qcom,qcs404-ethqos", .data = &emac_v2_3_0_por},
  3. { .compatible = "qcom,sdxprairie-ethqos", .data = &emac_v2_3_2_por},
  4. { .compatible = "qcom,stmmac-ethqos", },
  5. { .compatible = "qcom,emac-smmu-embedded", },
  6. { }
  7. };
  8. static struct platform_driver qcom_ethqos_driver = {
  9. .probe = qcom_ethqos_probe,
  10. .remove = qcom_ethqos_remove,
  11. .driver = {
  12. .name = DRV_NAME,
  13. .pm = &qcom_ethqos_pm_ops,
  14. .of_match_table = of_match_ptr(qcom_ethqos_match),
  15. },
  16. };
  17. static int __init qcom_ethqos_init_module(void)
  18. {
  19. ret = platform_driver_register(&qcom_ethqos_driver);
  20. return ret;
  21. }
  22. static int qcom_ethqos_probe(struct platform_device *pdev)
  23. {
  24. return _qcom_ethqos_probe(pdev);
  25. }
  26. static int _qcom_ethqos_probe(void *arg)
  27. {
  28. plat_dat = stmmac_probe_config_dt(pdev, &stmmac_res.mac); /* 解析设备树中网卡节点中的各种属性配置,比如phy-handle,max-speed等,同时初始化一些私有变量 */
  29. ret = stmmac_dvr_probe(&pdev->dev, plat_dat, &stmmac_res); /* 这个最后的probe函数是最关键的,其中调用了每个网卡驱动必会调用的alloc_etherdev创建net_device,之后一路初始化硬件、配置phy、最终调用register_netdev完成网卡驱动的注册 */
  30. }

2. 解析设备树中ethernet节点的各种属性

  1. struct plat_stmmacenet_data *
  2. stmmac_probe_config_dt(struct platform_device *pdev, const char **mac)
  3. {
  4. struct device_node *np = pdev->dev.of_node;
  5. struct plat_stmmacenet_data *plat; /* 用来存储ethernet device的所有配置信息 */
  6. struct stmmac_dma_cfg *dma_cfg;
  7. int rc;
  8. plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
  9. if (!plat)
  10. return ERR_PTR(-ENOMEM);
  11. *mac = of_get_mac_address(np); /* 从dts中解析mac-address属性,获得网卡的mac地址;mac-address属性的值,可以是hardcode在dts中,也可以从bootloader中传递过来 */
  12. if (IS_ERR(*mac)) {
  13. if (PTR_ERR(*mac) == -EPROBE_DEFER)
  14. return ERR_CAST(*mac);
  15. *mac = NULL;
  16. }
  17. plat->phy_interface = of_get_phy_mode(np); /* 一般是从dts中解析phy-mode属性,获得网卡和phy芯片之间通信的总线协议,一般是MII或者RMGII,在linux中有一张phy_modes的表格,枚举了所有可能的通信协议 */
  18. if (plat->phy_interface < 0)
  19. return ERR_PTR(plat->phy_interface);
  20. plat->interface = stmmac_of_get_mac_mode(np); /* 从dts中解析mac-mode属性,如果网卡和phy之间有协议转换设备(比如从RGMII转换到GMII),才会有该属性的配置 */
  21. if (plat->interface < 0)
  22. plat->interface = plat->phy_interface;
  23. /* Some wrapper drivers still rely on phy_node. Let's save it while
  24. * they are not converted to phylink. */
  25. plat->phy_node = of_parse_phandle(np, "phy-handle", 0); /*从dts中找到本网卡关联的phy节点*/
  26. /* PHYLINK automatically parses the phy-handle property */
  27. plat->phylink_node = np;
  28. /* Get max speed of operation from device tree */
  29. if (of_property_read_u32(np, "max-speed", &plat->max_speed))/*max-speed这个参数比较关键,dts配置的值会写到网卡的寄存器中,控制网卡的最高带宽 */
  30. plat->max_speed = -1;
  31. plat->bus_id = of_alias_get_id(np, "ethernet"); /*如果有多个ethernet,且在dts中配置有别名(alias),这里获得本ethernet节点在alias中定义的id号,方便与其他ethernet硬件做区分*/
  32. if (plat->bus_id < 0)
  33. plat->bus_id = 0;
  34. /* Default to phy auto-detection */
  35. plat->phy_addr = -1;
  36. /* Default to get clk_csr from stmmac_clk_crs_set(),
  37. * or get clk_csr from device tree.
  38. */
  39. plat->clk_csr = -1;
  40. of_property_read_u32(np, "clk_csr", &plat->clk_csr); /* csr是Control and Status Register的缩写,暂不清楚这个独立时钟的用途是什么 */
  41. /* "snps,phy-addr" is not a standard property. Mark it as deprecated
  42. * and warn of its use. Remove this when phy node support is added.
  43. */
  44. if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0) /*过时*/
  45. dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
  46. /* To Configure PHY by using all device-tree supported properties */
  47. rc = stmmac_dt_phy(plat, np, &pdev->dev); /*解析dts,如果ethernet节点中包含mdio子节点,则创建stmmac_mdio_bus_data结构体变量;mdio是以太网mac芯片与phy芯片交互的总线协议*/
  48. if (rc)
  49. return ERR_PTR(rc);
  50. of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size); /*获得mac控制器tx fifo的深度,单位是字节;snps,dwc-qos-ethernet-4.10支持256 byte, 512 byte, 1 KB, 2 KB, 4 KB, 8 KB, 16 KB, 32 KB, 64 KB, 128 KB, or 256 KB*/
  51. of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size); /*获得mac控制器rx fifo的深度,单位是字节;snps,dwc-qos-ethernet-4.10支持256 byte, 512 byte, 1 KB, 2 KB, 4 KB, 8 KB, 16 KB, 32 KB, 64 KB, 128 KB, or 256 KB*/
  52. plat->force_sf_dma_mode =
  53. of_property_read_bool(np, "snps,force_sf_dma_mode"); /* 从dts中获取属性值,以确定是否在tx和rx过程中强制使用 dma store and forward 模式;不使能该模式时,默认使用threshold模式 */
  54. plat->en_tx_lpi_clockgating =
  55. of_property_read_bool(np, "snps,en-tx-lpi-clockgating"); /*从dts中获取属性值,以确定是否在tx低功耗模式下使能gating mac tx clock;我理解该feature是指,当tx fifo无数据可发时,动态关闭tx的clock时钟,从而减低dynamic功耗*/
  56. /* Set the maxmtu to a default of JUMBO_LEN in case the
  57. * parameter is not present in the device tree.
  58. */
  59. plat->maxmtu = JUMBO_LEN; /* mac控制器支持的最大MTU(网络上传送的最大数据包),这里设置的“巨包”size是9000 */
  60. ......../*省去部分不关注的内容*/
  61. dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
  62. GFP_KERNEL); /*申请dma_cfg结构体内存,开始配置mac控制器的dma*/
  63. if (!dma_cfg) {
  64. stmmac_remove_config_dt(pdev, plat);
  65. return ERR_PTR(-ENOMEM);
  66. }
  67. plat->dma_cfg = dma_cfg;
  68. of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl); /*从dts中获取属性值,pbl全称是 programmable burst length, 可以是2、4或8 */
  69. if (!dma_cfg->pbl)
  70. dma_cfg->pbl = DEFAULT_DMA_PBL; /*如果dts中没配置,就用默认值,为8*/
  71. of_property_read_u32(np, "snps,txpbl", &dma_cfg->txpbl); /*tx pbl,配置了该值DMA tx会使用该值而不是pbl*/
  72. of_property_read_u32(np, "snps,rxpbl", &dma_cfg->rxpbl);/* rx pbl, 配置了该值DMA rx会使用该值而不是pbl */
  73. dma_cfg->pblx8 = !of_property_read_bool(np, "snps,no-pbl-x8");
  74. dma_cfg->aal = of_property_read_bool(np, "snps,aal"); /* 配置该值,表示地址对齐 */
  75. dma_cfg->fixed_burst = of_property_read_bool(np, "snps,fixed-burst");/* 配置该值,表示DMA传输为固定burst传输 */
  76. dma_cfg->mixed_burst = of_property_read_bool(np, "snps,mixed-burst");/* 配置该值,表示DMA传输为混合burst传输 */
  77. plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode"); /* 配置该值,DMA使用threshold模式 */
  78. if (plat->force_thresh_dma_mode) {
  79. plat->force_sf_dma_mode = 0; /* 若DMA使用threshold模式,则禁用store and fore模式 */
  80. dev_warn(&pdev->dev,
  81. "force_sf_dma_mode is ignored if force_thresh_dma_mode is set.\n");
  82. }
  83. of_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed); /*暂不理解,文档描述如下:Port selection speed that can be passed to the core when PCS is supported. For example, this is used in case of SGMII and MAC2MAC connection.*/
  84. plat->axi = stmmac_axi_setup(pdev); /*根据dts属性,配置mac控制器axi寄存器*/
  85. rc = stmmac_mtl_setup(pdev, plat); /* 根据dts属性,配置tx/rx queue相关寄存器,这里比较关键,后面详述 */
  86. if (rc) {
  87. stmmac_remove_config_dt(pdev, plat);
  88. return ERR_PTR(rc);
  89. }
  90. ........
  91. plat->pclk = devm_clk_get(&pdev->dev, "pclk"); /* 从dts中获得mac控制器的pclk */
  92. if (IS_ERR(plat->pclk)) {
  93. if (PTR_ERR(plat->pclk) == -EPROBE_DEFER)
  94. goto error_pclk_get;
  95. plat->pclk = NULL;
  96. }
  97. clk_prepare_enable(plat->pclk); /* 使能pclk */
  98. ........
  99. plat->stmmac_rst = devm_reset_control_get(&pdev->dev,
  100. STMMAC_RESOURCE_NAME);/* 从dts中获得mac控制器reset信号线的信息,以便之后做mac控制器所有寄存器的reset */
  101. if (IS_ERR(plat->stmmac_rst)) {
  102. if (PTR_ERR(plat->stmmac_rst) == -EPROBE_DEFER)
  103. goto error_hw_init;
  104. dev_info(&pdev->dev, "no reset control found\n");
  105. plat->stmmac_rst = NULL;
  106. }
  107. return plat;
  108. error_hw_init:
  109. clk_disable_unprepare(plat->pclk);
  110. error_pclk_get:
  111. clk_disable_unprepare(plat->stmmac_clk);
  112. return ERR_PTR(-EPROBE_DEFER);
  113. }
  114. static int stmmac_mtl_setup(struct platform_device *pdev,
  115. struct plat_stmmacenet_data *plat)
  116. {
  117. struct device_node *q_node;
  118. struct device_node *rx_node;
  119. struct device_node *tx_node;
  120. u8 queue = 0;
  121. int ret = 0;
  122. /* For backwards-compatibility with device trees that don't have any
  123. * snps,mtl-rx-config or snps,mtl-tx-config properties, we fall back
  124. * to one RX and TX queues each.
  125. */
  126. plat->rx_queues_to_use = 1;
  127. plat->tx_queues_to_use = 1;/* 如果dts中没有配置queue的信息,默认tx和rx queue都使用1个*/
  128. /* First Queue must always be in DCB mode. As MTL_QUEUE_DCB = 1 we need
  129. * to always set this, otherwise Queue will be classified as AVB
  130. * (because MTL_QUEUE_AVB = 0).
  131. */
  132. plat->rx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
  133. plat->tx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;/* 配置第一个queue的模式为DCB ;queue的模式有两种,分别是DCB和AVB */
  134. /*DCB模式 vs AVB模式:DCB(Data Center Bridge)模式常用于高吞吐场景, AVB(Audio Video Bridge)模式常用于局域网(LAN)中对时间敏感(time-sensitive)的业务。*/
  135. rx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-rx-config", 0);
  136. if (!rx_node)
  137. return ret;
  138. tx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-tx-config", 0);/* ethernet节点中需要有mtl-rx-config和mtl-tx-config两个节点,没有的话以下的配置也没必要展开了 */
  139. if (!tx_node) {
  140. of_node_put(rx_node);
  141. return ret;
  142. }
  143. /* Processing RX queues common config */
  144. if (of_property_read_u32(rx_node, "snps,rx-queues-to-use",
  145. &plat->rx_queues_to_use))/* 解析配置了多少个rx queue,如果没写,那就默认是1 */
  146. plat->rx_queues_to_use = 1;
  147. if (of_property_read_bool(rx_node, "snps,rx-sched-sp"))
  148. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
  149. else if (of_property_read_bool(rx_node, "snps,rx-sched-wsp"))
  150. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_WSP;
  151. else
  152. plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;/* 配置rx queue们采用什么调度算法 */
  153. /* Processing individual RX queue config */
  154. for_each_child_of_node(rx_node, q_node) {/* 遍历所有rx queue做配置*/
  155. if (queue >= plat->rx_queues_to_use)
  156. break;
  157. if (of_property_read_bool(q_node, "snps,dcb-algorithm"))
  158. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;/* 配置每个 rx queue使用哪种算法 */
  159. else if (of_property_read_bool(q_node, "snps,avb-algorithm"))
  160. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
  161. else
  162. plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  163. queue++;
  164. }
  165. /* Processing TX queues common config */
  166. if (of_property_read_u32(tx_node, "snps,tx-queues-to-use",
  167. &plat->tx_queues_to_use))
  168. plat->tx_queues_to_use = 1; /* 解析配置了多少个tx queue,如果没写,那就默认是1 */
  169. if (of_property_read_bool(tx_node, "snps,tx-sched-wrr"))
  170. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WRR;
  171. else if (of_property_read_bool(tx_node, "snps,tx-sched-wfq"))
  172. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WFQ;
  173. else if (of_property_read_bool(tx_node, "snps,tx-sched-dwrr"))
  174. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_DWRR;
  175. else if (of_property_read_bool(tx_node, "snps,tx-sched-sp"))
  176. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP;
  177. else
  178. plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP; /* 配置tx queue们采用什么调度算法 */
  179. queue = 0;
  180. /* Processing individual TX queue config */
  181. for_each_child_of_node(tx_node, q_node) { /* 遍历所有tx queue做配置*/
  182. if (queue >= plat->tx_queues_to_use)
  183. break;
  184. if (of_property_read_u32(q_node, "snps,weight",
  185. &plat->tx_queues_cfg[queue].weight))
  186. plat->tx_queues_cfg[queue].weight = 0x10 + queue;
  187. /* 配置每个 tx queue使用哪种算法 */
  188. if (of_property_read_bool(q_node, "snps,dcb-algorithm")) {
  189. plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
  190. } else if (of_property_read_bool(q_node,
  191. "snps,avb-algorithm")) {
  192. plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
  193. queue++;
  194. }
  195. return ret;
  196. }

3. register_netdev前的准备工作

  1. int stmmac_dvr_probe(struct device *device,
  2. struct plat_stmmacenet_data *plat_dat,
  3. struct stmmac_resources *res)
  4. {
  5. struct net_device *ndev = NULL;
  6. struct stmmac_priv *priv;
  7. u32 queue, rxq, maxq;
  8. int i, ret = 0;
  9. /* 申请以太网卡驱动关键的net_device结构体和私有结构体stmmac_priv的内存;net_device指代一个标准的以太网卡,这里之所以说是标准,是因为各家网卡驱动都会有自己特殊的结构体用来管理一些特有的属性,比如stmmac网卡驱动的特殊结构体是stmmac_priv,标准的网卡实例net_device是它的一个成员;该函数同时也为收发queue申请内存 */
  10. ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv),
  11. MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES);
  12. if (!ndev)
  13. return -ENOMEM;
  14. SET_NETDEV_DEV(ndev, device);/*设置sysfs中设备的父子关系,net_device结构体的parent是它对应的device结构体*/
  15. priv = netdev_priv(ndev);/* devm_alloc_etherdev_mqs申请内存时,net_device和stmmac_priv之间有一个偏移关系,这里可以直接通过net_device的内存地址找到stmmac_priv */
  16. priv->device = device;
  17. priv->dev = ndev;
  18. stmmac_set_ethtool_ops(ndev);/*为用户态工具ethtool访问网卡interface时设置相应的回调函数*/
  19. priv->pause = pause;
  20. priv->plat = plat_dat;
  21. priv->ioaddr = res->addr;
  22. priv->dev->base_addr = (unsigned long)res->addr;
  23. priv->dev->irq = res->irq;
  24. priv->wol_irq = res->wol_irq;
  25. priv->lpi_irq = res->lpi_irq;
  26. if (!IS_ERR_OR_NULL(res->mac))
  27. memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN);
  28. dev_set_drvdata(device, priv->dev);
  29. /* Verify driver arguments */
  30. stmmac_verify_args();
  31. /* Allocate workqueue */
  32. priv->wq = create_singlethread_workqueue("stmmac_wq");
  33. if (!priv->wq) {
  34. dev_err(priv->device, "failed to create workqueue\n");
  35. return -ENOMEM;
  36. }
  37. /*这里创建了初始化了一个workqueue,workqueue中只有一个work是stmamac_service_task,这是用来在网卡interface出错无法恢复时的一个重新初始化手段,后文详述*/
  38. INIT_WORK(&priv->service_task, stmmac_service_task);/*--->*/
  39. /* Override with kernel parameters if supplied XXX CRS XXX
  40. * this needs to have multiple instances
  41. */
  42. if ((phyaddr >= 0) && (phyaddr <= 31))
  43. priv->plat->phy_addr = phyaddr;
  44. if (priv->plat->stmmac_rst) {/*如果网卡配置了reset引脚,通过先assert(复位)再deassert(解复位)的方式的方式初始化网卡的寄存器状态 */
  45. ret = reset_control_assert(priv->plat->stmmac_rst);
  46. reset_control_deassert(priv->plat->stmmac_rst);
  47. /* Some reset controllers have only reset callback instead of
  48. * assert + deassert callbacks pair.
  49. */
  50. if (ret == -ENOTSUPP)
  51. reset_control_reset(priv->plat->stmmac_rst);
  52. }
  53. /* Init MAC and get the capabilities */
  54. ret = stmmac_hw_init(priv);/* 初始化MAC寄存器,同时根据MAC支持的特性做结构体配置 */
  55. if (ret)
  56. goto error_hw_init;
  57. stmmac_check_ether_addr(priv);/* 检查网卡MAC地址是否有效,如果是无效地址,则为网卡生成一个随机MAC地址 */
  58. /* Configure real RX and TX queues */
  59. netif_set_real_num_rx_queues(ndev, priv->plat->rx_queues_to_use);
  60. netif_set_real_num_tx_queues(ndev, priv->plat->tx_queues_to_use);/*根据配置了多少tx/rx queue初始化结构体*/
  61. ndev->netdev_ops = &stmmac_netdev_ops;/* 这里非常关键,为netdev_ops回调赋值,这里面包括了interface up/down时实际执行的函数,以及其他网卡interface操作时的重要回调,比如change mtu、ioctl等 */
  62. ndev->watchdog_timeo = msecs_to_jiffies(watchdog);/*为interface tx设置watchdog,如果超时则通过stmmac_service_task重启interface*/
  63. /* MTU range: 46 - hw-specific max */
  64. ndev->min_mtu = ETH_ZLEN - ETH_HLEN;/*设置mtu的最大/最小值*/
  65. if (priv->plat->has_xgmac)
  66. ndev->max_mtu = XGMAC_JUMBO_LEN;
  67. else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00))
  68. ndev->max_mtu = JUMBO_LEN;
  69. else
  70. ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN);
  71. /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu
  72. * as well as plat->maxmtu < ndev->min_mtu which is a invalid range.
  73. */
  74. if ((priv->plat->maxmtu < ndev->max_mtu) &&
  75. (priv->plat->maxmtu >= ndev->min_mtu))
  76. ndev->max_mtu = priv->plat->maxmtu;
  77. else if (priv->plat->maxmtu < ndev->min_mtu)
  78. dev_warn(priv->device,
  79. "%s: warning: maxmtu having invalid value (%d)\n",
  80. __func__, priv->plat->maxmtu);
  81. if (flow_ctrl)
  82. priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */
  83. /* Setup channels NAPI */
  84. maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use);
  85. for (queue = 0; queue < maxq; queue++) {
  86. struct stmmac_channel *ch = &priv->channel[queue];
  87. ch->priv_data = priv;
  88. ch->index = queue;
  89. if (queue < priv->plat->rx_queues_to_use) {
  90. netif_napi_add(ndev, &ch->rx_napi, stmmac_napi_poll_rx,
  91. NAPI_POLL_WEIGHT);/*为每条rx queue设置napi回调*/
  92. }
  93. if (queue < priv->plat->tx_queues_to_use) {
  94. netif_tx_napi_add(ndev, &ch->tx_napi,
  95. stmmac_napi_poll_tx,
  96. NAPI_POLL_WEIGHT);/*为每条tx queue设置napi回调*/
  97. }
  98. }
  99. if (priv->hw->pcs != STMMAC_PCS_RGMII &&
  100. priv->hw->pcs != STMMAC_PCS_TBI &&
  101. priv->hw->pcs != STMMAC_PCS_RTBI) {
  102. /* MDIO bus Registration */
  103. ret = stmmac_mdio_register(ndev);/*注册mdio相关结构体,以使MAC能与PHY通信--->*/
  104. if (ret < 0) {
  105. dev_err(priv->device,
  106. "%s: MDIO bus (id: %d) registration failed",
  107. __func__, priv->plat->bus_id);
  108. goto error_mdio_register;
  109. }
  110. }
  111. ret = stmmac_phy_setup(priv); /*注册phy实体-->*/
  112. if (ret) {
  113. netdev_err(ndev, "failed to setup phy (%d)\n", ret);
  114. goto error_phy_setup;
  115. }
  116. ret = register_netdev(ndev);/*注册mac实体*/
  117. if (ret) {
  118. dev_err(priv->device, "%s: ERROR %i registering the device\n",
  119. __func__, ret);
  120. goto error_netdev_register;
  121. }
  122. return ret;
  123. }
  124. stmmac_dvr_probe
  125. stmmac_phy_setup
  126. phylink_create
  127. phylink_parse_mode
  128. //判断设备树中是否有配置fixed-link或者managed节点,如果有则设置为MLO_AN_FIXED模式或者MLO_AN_INBAND模式,如果两者都没设置则返回0====》代表MLO_AN_PHY模式

4. 控制器平台驱动的probe函数

  1. /****************************************************************************************
  2. 直接调用CPU的MDIO控制器的方式的MDIO控制器驱动(probe函数中涉及PHY设备的创建和注册)
  3. ****************************************************************************************/
  4. # linux-4.9.225\drivers\net\ethernet\freescale\fsl_pq_mdio.c
  5. stmmac_dvr_probe
  6. |
  7. |--- stmmac_mdio_register(ndev)
  8. |--- struct mii_bus *new_bus
  9. |--- new_bus = mdiobus_alloc_size(sizeof(*priv)) //分配结构体
  10. |--- new_bus->name = "stmmac"
  11. |--- new_bus->read = &stmmac_mdio_read //总线的读接口
  12. |--- new_bus->write = &stmmac_mdio_write; //总线的写接口
  13. |
  14. |--- of_mdiobus_register(new_bus, np)//注册mii_bus设备,并通过设备树中控制器的子节点创建PHY设备
  15. | |--- mdio->phy_mask = ~0 //屏蔽所有PHY,防止自动探测。相反,设备树中列出的phy将在总线注册后填充
  16. | |--- mdio->dev.of_node = np
  17. | |--- mdiobus_register(mdio) //@注意@ 注册MDIO总线设备(注意是总线设备不是总线,因为总线也是一种设备。mdio_bus是在其他地方注册的,后面会讲到)
  18. | | |--- __mdiobus_register(bus, THIS_MODULE)
  19. | | | |--- bus->owner = owner
  20. | | | |--- bus->dev.parent = bus->parent
  21. | | | |--- bus->dev.class = &mdio_bus_class
  22. | | | |--- bus->dev.groups = NULL
  23. | | | |--- dev_set_name(&bus->dev, "%s", bus->id) //设置总线设备的名称
  24. | | | |--- device_register(&bus->dev) //注册总线设备
  25. | |
  26. | |--- for_each_available_child_of_node(np, child) //遍历这个平台设备的子节点并为每个phy注册一个phy_device
  27. | |--- addr = of_mdio_parse_addr(&mdio->dev, child) //从子节点的"reg"属性中获得PHY设备的地址
  28. | | |--- of_property_read_u32(np, "reg", &addr)
  29. | |--- if (addr < 0) //如果未获得子节点的"reg"属性,则在后面再启用扫描可能存在的PHY的,然后注册
  30. | | |--- scanphys = true
  31. | | |--- continue
  32. | |
  33. | |--- of_mdiobus_register_phy(mdio, child, addr) //创建并注册PHY设备
  34. | | |--- is_c45 = of_device_is_compatible(child,"ethernet-phy-ieee802.3-c45") //判断设备树中的PHY的属性是否指定45号条款
  35. | | |
  36. | | |--- if (!is_c45 && !of_get_phy_id(child, &phy_id)) //如果设备树中的PHY的属性未指定45号条款 且通过"ethernet-phy-id%4x.%4x"属性指定PHY的ID
  37. | | | |---phy_device_create(mdio, addr, phy_id, 0, NULL)
  38. | | |---else //我这里采用的是else分支
  39. | | | |---phy = get_phy_device(mdio, addr, is_c45) //在@bus上的@addr处读取PHY的ID寄存器,然后分配并返回表示它的phy_device
  40. | | | |--- get_phy_id(bus, addr, &phy_id, is_c45, &c45_ids) //通过mdio得到PHY的ID
  41. | | | |--- phy_device_create(bus, addr, phy_id, is_c45, &c45_ids) //创建PHY设备
  42. | | | |--- struct phy_device *dev
  43. | | | |--- struct mdio_device *mdiodev
  44. | | | |--- dev = kzalloc(sizeof(*dev), GFP_KERNEL)
  45. | | | |--- mdiodev = &dev->mdio //mdiodev是最新的内核引入,较老的版本没有这个结构
  46. | | | |--- mdiodev->dev.release = phy_device_release
  47. | | | |--- mdiodev->dev.parent = &bus->dev
  48. | | | |--- mdiodev->dev.bus = &mdio_bus_type //PHY设备和驱动都会挂在mdio_bus下,匹配时会调用对应的match函数 ---|
  49. | | | |--- mdiodev->bus = bus |
  50. | | | |--- mdiodev->pm_ops = MDIO_BUS_PHY_PM_OPS |
  51. | | | |--- mdiodev->bus_match = phy_bus_match //真正实现PHY设备和驱动匹配的函数<--------------------------------|
  52. | | | |--- mdiodev->addr = addr
  53. | | | |--- mdiodev->flags = MDIO_DEVICE_FLAG_PHY
  54. | | | |--- mdiodev->device_free = phy_mdio_device_free
  55. | | | |--- diodev->device_remove = phy_mdio_device_remove
  56. | | | |--- dev->speed = SPEED_UNKNOWN
  57. | | | |--- dev->duplex = DUPLEX_UNKNOWN
  58. | | | |--- dev->pause = 0
  59. | | | |--- dev->asym_pause = 0
  60. | | | |--- dev->link = 1
  61. | | | |--- dev->interface = PHY_INTERFACE_MODE_GMII
  62. | | | |--- dev->autoneg = AUTONEG_ENABLE //默认支持自协商
  63. | | | |--- dev->is_c45 = is_c45
  64. | | | |--- dev->phy_id = phy_id
  65. | | | |--- if (c45_ids)
  66. | | | | |--- dev->c45_ids = *c45_ids
  67. | | | |--- dev->irq = bus->irq[addr]
  68. | | | |--- dev_set_name(&mdiodev->dev, PHY_ID_FMT, bus->id, addr)
  69. | | | |--- dev->state = PHY_DOWN //指示PHY设备和驱动程序尚未准备就绪,在PHY驱动的probe函数中会更改为READY
  70. | | | |--- INIT_DELAYED_WORK(&dev->state_queue, phy_state_machine) //PHY的状态机(核心WORK)
  71. | | | |--- INIT_WORK(&dev->phy_queue, phy_change) //由phy_interrupt / timer调度以处理PHY状态的更改
  72. | | | |--- request_module(MDIO_MODULE_PREFIX MDIO_ID_FMT, MDIO_ID_ARGS(phy_id))//加载内核模块(这里没有细致研究过)
  73. | | | |--- device_initialize(&mdiodev->dev) //设备模型中的一些设备,主要是kset、kobject、ktype的设置
  74. | | |
  75. | | |--- irq_of_parse_and_map(child, 0) //将中断解析并映射到linux virq空间(未深入研究)
  76. | | |--- if (of_property_read_bool(child, "broken-turn-around"))//MDIO总线中的TA(Turnaround time)
  77. | | | |--- mdio->phy_ignore_ta_mask |= 1 << addr
  78. | | |
  79. | | |--- of_node_get(child)//将OF节点与设备结构相关联,以便以后查找
  80. | | |--- phy->mdio.dev.of_node = child
  81. | | |
  82. | | |--- phy_device_register(phy)//注册PHY设备
  83. | | | |--- mdiobus_register_device(&phydev->mdio) //注册到mdiodev->bus,其实笔者认为这是一个虚拟的注册,仅仅是根据PHY的地址在mdiodev->bus->mdio_map数组对应位置填充这个mdiodev
  84. | | | | |--- mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev // 方便通过mdiodev->bus统一管理和查找,以及关联bus的读写函数,方便PHY的功能配置
  85. | | | |
  86. | | | |--- device_add(&phydev->mdio.dev)//注册到linux设备模型框架中
  87. | |
  88. | |--- if (!scanphys) //如果从子节点的"reg"属性中获得PHY设备的地址,scanphys=false,这里就直接返回了,因为不需要再扫描了
  89. | | |--- return 0
  90. | |
  91. /******************************************************************************************************************
  92. 一般来说只要设备树种指定了PHY设备的"reg"属性,后面的流程可以自动忽略
  93. ******************************************************************************************************************
  94. | |--- for_each_available_child_of_node(np, child) //自动扫描具有空"reg"属性的PHY
  95. | |--- if (of_find_property(child, "reg", NULL)) //跳过具有reg属性集的PHY
  96. | | |--- continue
  97. | |
  98. | |--- for (addr = 0; addr < PHY_MAX_ADDR; addr++) //循环遍历扫描
  99. | |--- if (mdiobus_is_registered_device(mdio, addr)) //跳过已注册的PHY
  100. | | |--- continue
  101. | |
  102. | |--- dev_info(&mdio->dev, "scan phy %s at address %i\n", child->name, addr) //打印扫描的PHY,建议开发人员设置"reg"属性
  103. | |
  104. | |--- if (of_mdiobus_child_is_phy(child))
  105. | |--- of_mdiobus_register_phy(mdio, child, addr) //注册PHY设备
  106. |
  107. ******************************************************************************************************************/
  108. |--- ret = stmmac_phy_setup(priv); /*注册phy实体*/
  109. |--- ret = register_netdev(ndev);/*注册mac实体*/

5. PHY寄存器的读写

        在PHY设备的注册中(读PHY ID)、PHY的初始化、自协商、中断、状态、能力获取等流程中经常可以看到phy_read和phy_write两个函数(下一节要讲的PHY驱动),这两个函数的实现就依赖于控制器设备mii_bus的读写。

        PHY寄存器的读写函数phy_read和phy_write定义在linux-4.9.225\include\linux\phy.h中,如下:

  1. static inline int phy_read(struct phy_device *phydev, u32 regnum)
  2. {
  3. return mdiobus_read(phydev->mdio.bus, phydev->mdio.addr, regnum);
  4. }
  5. static inline int phy_write(struct phy_device *phydev, u32 regnum, u16 val)
  6. {
  7. return mdiobus_write(phydev->mdio.bus, phydev->mdio.addr, regnum, val);
  8. }

        其中mdiobus_read和mdiobus_write定义在linux-4.9.225\drivers\net\phy\mdio_bus.c中,如下:

  1. /**
  2. * mdiobus_read - Convenience function for reading a given MII mgmt register
  3. * @bus: the mii_bus struct
  4. * @addr: the phy address
  5. * @regnum: register number to read
  6. *
  7. * NOTE: MUST NOT be called from interrupt context,
  8. * because the bus read/write functions may wait for an interrupt
  9. * to conclude the operation.
  10. */
  11. int mdiobus_read(struct mii_bus *bus, int addr, u32 regnum)
  12. {
  13. int retval;
  14. BUG_ON(in_interrupt());
  15. mutex_lock(&bus->mdio_lock);
  16. retval = bus->read(bus, addr, regnum);
  17. mutex_unlock(&bus->mdio_lock);
  18. return retval;
  19. }
  20. /**
  21. * mdiobus_write - Convenience function for writing a given MII mgmt register
  22. * @bus: the mii_bus struct
  23. * @addr: the phy address
  24. * @regnum: register number to write
  25. * @val: value to write to @regnum
  26. *
  27. * NOTE: MUST NOT be called from interrupt context,
  28. * because the bus read/write functions may wait for an interrupt
  29. * to conclude the operation.
  30. */
  31. int mdiobus_write(struct mii_bus *bus, int addr, u32 regnum, u16 val)
  32. {
  33. int err;
  34. BUG_ON(in_interrupt());
  35. mutex_lock(&bus->mdio_lock);
  36. err = bus->write(bus, addr, regnum, val);
  37. mutex_unlock(&bus->mdio_lock);
  38. return err;
  39. }

        可以清楚的看到bus->read和bus->write读写接口在这里得到调用。

6. mdiodev的注册和获取

  1. stmmac_dvr_probe
  2. stmmac_mdio_register
  3. of_mdiobus_register
  4. mdiobus_register
  5. __mdiobus_register
  6. mdiobus_scan
  7. phy_device_register(phy)
  8. mdiobus_register_device((&phydev->mdio))
  9. mdiodev->bus->mdio_map[mdiodev->addr] = mdiodev
  10. for_each_available_child_of_node(np, child) //遍历这个平台设备的子节点并为每个phy注册一个phy_device
  11. addr = of_mdio_parse_addr(&mdio->dev, child) //从子节点的"reg"属性中获得PHY设备的地址
  12. mdiobus_get_phy
  13. bus->mdio_map[addr]
  14. container_of(mdiodev, struct phy_device, mdio) //获取phy_device

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/712612
推荐阅读
相关标签
  

闽ICP备14008679号