知识点

  • mdio子模块属于总线型驱动

  • NAPI(New API)
    网卡数据处理API,用于提高网络处理效率。NAPI是中断和轮询poll的结合,数据量低时采用中断,数据量高时采用轮询

  • 通用phy驱动genphy。符合802.3,phy寄存器地址统一

流程梳理

以RK3399(Android7.1.2)为例
路径为: kernel/drivers/net/ethernet/stmicro/stmmac/

dts(“rockchip,rk3399-gmac”)
->rk_gmac_probe(dwmac-rk.c),gmac时钟、电源等配置
->stmmac_dvr_probe(stmmac_main.c)mac时钟使能、mac硬件初始化、net_device和NAPI绑定(netif_napi_add)、register_netdev()等等
->stmmac_mdio_register->mdiobus_register
->

mdiobus_register
  mdiobus_scan
    get_phy_device
      get_phy_id
      phy_device_create
    phy_device_register
      phy_scan_fixups

网卡驱动通过of_phy_connect来连接phy,也可以通过phy_find_first自动查找到总线上的第一个phy设备,然后调用phy_connect,PHY_READY

connect之后就会start,PHY_UP

phy状态机

应用层读写phy寄存器

未测试
前提条件: 驱动层MAC与MDIO总线已绑定
测试DEMO:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <linux/mii.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <linux/types.h>
#include <netinet/in.h>
#include <unistd.h>
 
//#include <QDebug>
 
 
int main(int argc, char *argv[])
{
    int sockfd;
    struct mii_ioctl_data *mii = NULL;
    struct ifreq ifr;
    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, "eth0", IFNAMSIZ - 1);
    sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
    ioctl(sockfd, SIOCGMIIPHY, &ifr);
    mii = (struct mii_ioctl_data*)&ifr.ifr_data;
 
    if(argc == 4)
    {
        strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
        sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
        ioctl(sockfd, SIOCGMIIPHY, &ifr);
        mii = (struct mii_ioctl_data*)&ifr.ifr_data;
 
        mii->phy_id    = (uint16_t)strtoul(argv[2], NULL, 0);
        mii->reg_num    = (uint16_t)strtoul(argv[3], NULL, 0);
 
        ioctl(sockfd, SIOCGMIIREG, &ifr);
 
        printf("read --- value : 0x%x", mii->val_out);
    }
    else if(argc == 5)
    {
        strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
        sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
        ioctl(sockfd, SIOCGMIIPHY, &ifr);
        mii = (struct mii_ioctl_data*)&ifr.ifr_data;
        mii->phy_id    = (uint16_t)strtoul(argv[2], NULL, 0);
        mii->reg_num    = (uint16_t)strtoul(argv[3], NULL, 0);
        mii->val_in     = (uint16_t)strtoul(argv[4], NULL, 0);
 
        ioctl(sockfd, SIOCSMIIREG, &ifr);
    }else{
       printf("mdio ethX phyId addr value\n");
    }
 
    close(sockfd);
 
    return 0;
}

使用:
写寄存器:./mdio ethX phyId addr value

  • ethX: 网卡,如eth0 eth1 。
  • phyId: phy的物理地址,一般0x00是广播地址.有些phy 的0x00不是广播地址,如marvell的88e1512的ID 只能是0x00 0x11,0x00不是88e1512的广播地址。
  • addr: phy手册的寄存器地址
  • value: phy地址要写入的值

读寄存器:./mdio ethX phyId addr

参考

https://blog.csdn.net/Zhu_Zhu_2009/article/details/105193423
https://blog.csdn.net/yafeixi/article/details/112958722