Linux核外通过MDIO访问Phy

前言

调试以太网(MAC+PHY)网络问题,我们经常需要获取或配置PHY的寄存器,但又不想一次次去修改内核,在Linux应用层怎么简单方便的去操作PHY的寄存器呢?

众所周知,MAC+PHY的组合下,主要是通过MDIO接口去操作PHY的寄存器,所以也就变成了怎么去操作MDIO。

一般有以下方式:

  • 通过MDIO暴露到核外的接口,比如设备节点,或寄存器直接映射的内存地址。
  • 通过通用的ioctl访问接口。

以上都是要基于SoC的MAC驱动支持才行,特别是第一点,需要对Soc的MAC比较熟悉,而且因SoC厂家不同而差异很大。

这里重点看看第二种通用的方式。

使用ioctl接口访问

实现

主要是通过ioctl操作以下几个接口来实现对PHY寄存器的读写操作:

  • SIOCGMIIPHY:
    read register from the current PHY.
  • SIOCGMIIREG:
    read register from the specified PHY.
  • SIOCSMIIREG:
    set a register on the specified PHY.

原理

一般MAC驱动都需要实现struct net_device_ops 函数操作集,这个属于网络框架的一部分,这个操作集会有个ndo_do_ioctl接口,该接口就是用于实现应用层通过ioctl访问MAC。

这里以stmmac驱动来看看:

static const struct net_device_ops stmmac_netdev_ops = {
	.ndo_open = stmmac_open,
	.ndo_start_xmit = stmmac_xmit,
	.ndo_stop = stmmac_release,
	.ndo_change_mtu = stmmac_change_mtu,
	.ndo_fix_features = stmmac_fix_features,
	.ndo_set_features = stmmac_set_features,
	.ndo_set_rx_mode = stmmac_set_rx_mode,
	.ndo_tx_timeout = stmmac_tx_timeout,
	.ndo_do_ioctl = stmmac_ioctl,
	.ndo_setup_tc = stmmac_setup_tc,
	.ndo_select_queue = stmmac_select_queue,
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller = stmmac_poll_controller,
#endif
	.ndo_set_mac_address = stmmac_set_mac_address,
	.ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid,
	.ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid,
};
static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct stmmac_priv *priv = netdev_priv (dev);
	int ret = -EOPNOTSUPP;

	if (!netif_running(dev))
		return -EINVAL;

	switch (cmd) {
	case SIOCGMIIPHY:
	case SIOCGMIIREG:
	case SIOCSMIIREG:
		ret = phylink_mii_ioctl(priv->phylink, rq, cmd);
		break;
	case SIOCSHWTSTAMP:
		ret = stmmac_hwtstamp_set(dev, rq);
		break;
	case SIOCGHWTSTAMP:
		ret = stmmac_hwtstamp_get(dev, rq);
		break;
	default:
		break;
	}

	return ret;
}

stmmac_ioctl函数中,对SIOCGMIIPHYSIOCGMIIREGSIOCSMIIREG的处理都会调用到phylink_mii_ioctl,该接口是通用的 phylink 驱动中mii的ioctl接口。
phylink_mii_ioctl函数中,就会去调用相应的函数去进行读写操作,具体怎么读写这里就不再细化下去了。

int phylink_mii_ioctl(struct phylink *pl, struct ifreq *ifr, int cmd)
{
	struct mii_ioctl_data *mii = if_mii(ifr);
	int  ret;

	ASSERT_RTNL();

	if (pl->phydev) {
		/* PHYs only exist for MLO_AN_PHY and SGMII */
		switch (cmd) {
		case SIOCGMIIPHY:
			mii->phy_id = pl->phydev->mdio.addr;
			/* fall through */

		case SIOCGMIIREG:
			ret = phylink_phy_read(pl, mii->phy_id, mii->reg_num);
			if (ret >= 0) {
				mii->val_out = ret;
				ret = 0;
			}
			break;

		case SIOCSMIIREG:
			ret = phylink_phy_write(pl, mii->phy_id, mii->reg_num,
						mii->val_in);
			break;

		default:
			ret = phy_mii_ioctl(pl->phydev, ifr, cmd);
			break;
		}
	} else {
		switch (cmd) {
		case SIOCGMIIPHY:
			mii->phy_id = 0;
			/* fall through */

		case SIOCGMIIREG:
			ret = phylink_mii_read(pl, mii->phy_id, mii->reg_num);
			if (ret >= 0) {
				mii->val_out = ret;
				ret = 0;
			}
			break;

		case SIOCSMIIREG:
			ret = phylink_mii_write(pl, mii->phy_id, mii->reg_num,
						mii->val_in);
			break;

		default:
			ret = -EOPNOTSUPP;
			break;
		}
	}

	return ret;
}

应用例程

#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>


#define reteck(ret) \
if(ret < 0){ \
printf("%m! \"%s\" : line: %d\n", __func__, __LINE__); \
goto lab; \
}

#define help() \
printf("mdio:\n"); \
printf("read operation: mdio reg_addr\n"); \
printf("write operation: mdio reg_addr value\n"); \
printf("For example:\n"); \
printf("mdio eth0 1\n"); \
printf("mdio eth0 0 0x12\n\n"); \
exit(0);

int sockfd;

int main(int argc, char *argv[])
{
	if(argc == 1 || !strcmp(argv[1], "-h")){
		help();
	}

	struct mii_ioctl_data *mii = NULL;
	struct ifreq ifr;
	int ret;

	memset(&ifr, 0, sizeof(ifr));
	strncpy(ifr.ifr_name, argv[1], IFNAMSIZ - 1);
	sockfd = socket(PF_LOCAL, SOCK_DGRAM, 0);
	reteck(sockfd);

	//get phy address in smi bus
	ret = ioctl(sockfd, SIOCGMIIPHY, &ifr);
	reteck(ret);
	mii = (struct mii_ioctl_data*)&ifr.ifr_data;

	if(argc == 3){
		mii->reg_num = (uint16_t)strtoul(argv[2], NULL, 0);
		ret = ioctl(sockfd, SIOCGMIIREG, &ifr);
		reteck(ret);
		printf("read phy addr: 0x%x reg: 0x%x value : 0x%x\n\n", mii->phy_id, mii->reg_num, mii->val_out);

	}else if(argc == 4){
		mii->reg_num = (uint16_t)strtoul(argv[2], NULL, 0);
		mii->val_in = (uint16_t)strtoul(argv[3], NULL, 0);
		ret = ioctl(sockfd, SIOCSMIIREG, &ifr);
		reteck(ret);
		printf("write phy addr: 0x%x reg: 0x%x value : 0x%x\n\n", mii->phy_id, mii->reg_num, mii->val_in);
	}

lab:
	close(sockfd);

return 0;
}

参考