转载:linux+arm 网卡故障调试:ethtool&phy寄存器读写

来源:互联网 发布:mac book air能吃鸡吗 编辑:程序博客网 时间:2024/06/03 15:58

两个工具一个是ethtool工具,一个是源码编译的可以读phy寄存器的工具phyreg。两者结合使用,事半功倍。


ethtool 可以查看和设置网卡的工作状态,比如查看设置网卡的链接 自协商,10/100/1000M,双工半双工的工作状态。


ethtool的交叉编译工具,见个人博客文章:http://blog.csdn.net/vc66vcc/article/details/52398864


phyreg 可以读写phy的任意寄存器。相关源码如下

[cpp] view plain copy
print?
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <linux/mii.h>  
  5. #include <sys/types.h>  
  6. #include <sys/socket.h>  
  7. #include <sys/ioctl.h>  
  8. #include <net/if.h>  
  9. #include <linux/sockios.h>  
  10. #include <linux/types.h>  
  11. #include <netinet/in.h>  
  12.   
  13.   
  14. #define reteck(ret)     \  
  15.         if(ret < 0){    \  
  16.             printf(”%m! \”%s\” : line: %d\n”funcLINE);   \  
  17.             goto lab;   \  
  18.         }  
  19.   
  20. #define help() \  
  21.     printf(”mdio:\n”);                  \  
  22.     printf(”read operation: mdio reg_addr\n”);          \  
  23.     printf(”write operation: mdio reg_addr value\n”);    \  
  24.     printf(”For example:\n”);            \  
  25.     printf(”mdio eth0 1\n”);             \  
  26.     printf(”mdio eth0 0 0x12\n\n”);      \  
  27.     exit(0);  
  28.   
  29. int sockfd;  
  30.   
  31. int main(int argc, char argv[]){  
  32.           
  33.     if(argc == 1 || !strcmp(argv[1], “-h”)){  
  34.         help();  
  35.     }  
  36.       
  37.     struct mii_ioctl_data *mii = NULL;  
  38.     struct ifreq ifr;  
  39.     int ret;  
  40.   
  41.     memset(&ifr, 0, sizeof(ifr));  
  42.     strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);  
  43.   
  44.     sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);  
  45.     reteck(sockfd);  
  46.   
  47.     //get phy address in smi bus  
  48.     ret = ioctl(sockfd, SIOCGMIIPHY, &ifr);  
  49.     reteck(ret);  
  50.   
  51.     mii = (struct mii_ioctl_data)&ifr.ifr_data;  
  52.   
  53.     if(argc == 3){  
  54.   
  55.         mii->reg_num    = (uint16_t)strtoul(argv[2], NULL, 0);  
  56.           
  57.         ret = ioctl(sockfd, SIOCGMIIREG, &ifr);  
  58.         reteck(ret);  
  59.       
  60.         printf(”read phy addr: 0x%x  reg: 0x%x   value : 0x%x\n\n”, mii->phy_id, mii->reg_num, mii->val_out);  
  61.     }else if(argc == 4){  
  62.   
  63.         mii->reg_num    = (uint16_t)strtoul(argv[2], NULL, 0);  
  64.         mii->val_in     = (uint16_t)strtoul(argv[3], NULL, 0);  
  65.   
  66.         ret = ioctl(sockfd, SIOCSMIIREG, &ifr);  
  67.         reteck(ret);  
  68.   
  69.         printf(”write phy addr: 0x%x  reg: 0x%x  value : 0x%x\n\n”, mii->phy_id, mii->reg_num, mii->val_in);  
  70.     }  
  71.   
  72. lab:  
  73.     close(sockfd);  
  74.     return 0;  
  75. }  

阅读全文
0 0