RTL8169(S)-32/64 网络接口芯片组编程指南
文章目录
RTL8169(S)-32/64 网络接口芯片组编程指南
RTL8169 是 Realtek 的下一代高性能网卡。该特定芯片组设计用于以 10/100/1000 Mbps 的速度运行。
识别
RTL8169 系列的基本接口在多款 Realtek 网卡中是通用的。这适用于以下来自 FreeBSD 的 re 驱动的 PCI Vendor 和 Device ID 的非完整列表:
- 10ec:8161
- 10ec:8168
- 10ec:8169
- 1259:c107
- 1737:1032
- 16ec:0116
基本启动
获取 MAC 地址
首先,我们需要获取 NIC 的物理 MAC 地址。通常我们应该通过 EEPROM 来访问 MAC 地址,但在这种情况下,我们将走捷径,从 MAC0-6 寄存器中获取地址。MAC 寄存器从偏移量 0x00 开始,应以 8 位为段进行访问。
示例:
char i; for (i = 0; i < 6; i++) { mac_address[i] = inportb(ioaddr + i); /*ioaddr is the base address obtainable from the PCI device configuration space.*/ }
复位芯片
有必要复位 RTL8169,以使寄存器处于已知的默认状态,并开始为芯片上电。我们需要向位于偏移量 0x37 的 Chip Command 寄存器发送复位命令。复位位位于偏移量 0x10,因此我们需要使用这些偏移量执行 'outportb',以告知芯片开始复位过程。一旦我们发送了该命令,芯片需要一些时间来完成操作,并且由于它正在修改寄存器,在其完成之前我们应该停止对寄存器的所有进一步写入/读取。操作完成后,Command Register 中的复位位将自行清零,这就是我们在继续设置芯片之前应该检查的内容。
示例:
outportb(ioaddr + 0x37, 0x10); /*set the Reset bit (0x10) to the Command Register (0x37)*/ while(inportb(ioaddr + 0x37) & 0x10) ;/*setting a timeout could be useful if the card is problematic*/
设置 Rx 描述符
这款新一代芯片引入的新特性之一是它完全基于描述符。该芯片支持最多 3 个唯一的描述符集,每个集有 1024 个可用描述符;1024 用于接收数据包,1024 用于正常传输,1024 用于高优先级传输数据包。不需要使用每个集的所有描述符,甚至不需要使用所有三个集。每个描述符宽 16 字节,包含一个 64 位的缓冲区地址(用于存储传输数据或告知接收过程将数据转储到何处)和 Command uint32_t。
Rx 描述符用于告知 NIC 在接收到数据包后将其放置在何处。由于这是一个异步操作,我们必须在启用数据包接收之前设置描述符,以便 NIC 知道将东西放在哪里。描述符的一个主要功能是告知 NIC 哪些描述符由操作系统(主机)拥有,哪些由 NIC 拥有。如果描述符由 NIC 拥有,NIC 就可以读取它并将其用于接收到的下一个数据包;如果不是,它要么跳过它,要么发送 RDU(Rx Descriptor Unavailable)中断并停止所有进一步的 Rx 操作。由于可能使用多个描述符,还有一个 EOR(End of Rx descriptor Ring)位,用于当 NIC 到达描述符末尾并需要循环回到描述符开头时。关于所有这些还有一个大的“陷阱”,描述符数组的开头必须在 256 字节边界上对齐。
描述符格式
| 偏移量 | 名称 | 描述 |
|---|---|---|
| dword 0 | Flags | 31 位=OWN(如果置位,卡拥有此描述符) 30 位=EOR(列表中的最后一个描述符) |
| dword 1 | VLAN | 未使用 |
| dword 2 | 缓冲区指针低 32 位 | |
| dword 3 | 缓冲区指针高 32 位 |
代码示例
struct Descriptor { uint32_t command; /* command/status uint32_t */ uint32_t vlan; /* currently unused */ uint32_t low_buf; /* low 32-bits of physical buffer address */ uint32_t high_buf; /* high 32-bits of physical buffer address */ }; struct Descriptor *Rx_Descriptors = (struct Descriptor *)0x100000; /* 1MB Base Address of Rx Descriptors */ void setup_rx_descriptors() { /* rx_buffer_len is the size (in bytes) that is reserved for incoming packets */ unsigned int OWN = 0x80000000, EOR = 0x40000000; /* Bit offsets */ int i; for(i = 0; i < num_of_rx_descriptors; i++) /* num_of_rx_descriptors can be up to 1024 */ { if(i == (num_of_rx_descriptors - 1)) /* Last descriptor? if so, set the EOR bit */ Rx_Descriptors[i].command = (OWN | EOR | (rx_buffer_len & 0x3FFF)); else Rx_Descriptors[i].command = (OWN | (rx_buffer_len & 0x3FFF)); /* VLAN adjustments are not part of this guide at the moment - leave as zeros for normal operation */ Rx_Descriptors[i].low_buf = (unsigned int)&packet_buffer_address; /* This is where the packet data will go */ /* If you are programming for a 64-bit OS, put the high memory location in the 'high_buf' descriptor area */ } }
配置 RxConfig 和 TxConfig
(解锁)锁定寄存器
为了使用一些关键寄存器,我们必须“解锁”它们以更改其状态,然后在完成后“锁定”它们,以防止在操作期间意外更改这些寄存器。9346CR 寄存器位于偏移量 0x50,用作这些关键寄存器的密钥。为了“解锁”它们,我们必须将卡设置为“Config register write enable”模式。这是通过设置两个 EEM(操作模式)位来完成的。一旦完成,就可以完成卡的设置。一旦配置完成且操作系统开始正常运行,应将 EEM 位设置为低,以将卡设置为“正常”模式并锁定配置寄存器。
TxConfig
Transmit Configuration 寄存器位于偏移量 0x40,包含诸如 DMA 阈值和回环状态等配置数据。我们主要应该关注的只有两个位:MXDMA 和 IFG 位。MXDMA 位用于设置传输突发的 DMA 传输阈值。我通常将其设置为无限制突发数据(一个完整数据包)。如果您通过主机操作系统进行了适当处理,可以将其设置得更低。IFG(Inter-Frame Gap)是每次数据包操作之间经过的时间量。这可以设置为标准值(96/960/9600ns),或者您可以以 8ns 为增量增加时间(原因未知)。
示例:
outportl(ioaddr + 0x40, 0x03000700); /* IFG: normal, MXDMA: unlimited */
RxConfig
Receive Configuration 寄存器位于偏移量 0x44,包含诸如 DMA 阈值、FIFO 阈值和数据包接收规则等配置数据。RXFTH 位用于设置 Rx Fifo Threshold。此阈值确定缓冲区中需要多少数据才能开始告知主机操作系统数据已就绪。我通常将其设置为无限制数据,因为这意味着一旦完整数据包进入卡上 FIFO 缓冲区,NIC 就会告知操作系统数据已就绪。MXDMA 基于为 Tx MXDMA 给出的相同解释。我将 MXDMA 设置为无限制。AB/AM/APM/AAP 位用于设置数据包过滤器。我通常将其设置为混杂模式,将前面的所有位都置位,以允许卡接收和处理物理到达 NIC 的任何数据包。
示例:
outportl(ioaddr + 0x44, 0x0000E70F) /* RXFTH: unlimited, MXDMA: unlimited, AAP: set (promisc. mode set) */
最大数据包大小
最大传输数据包大小
MTPS 寄存器位于偏移量 0xEC,用于设置可以从 RTL8169 传输的最大数据包大小。我们在此处可以提供的最大值是 0x3B。该值足以覆盖“巨型”数据包,并且仍然允许稳定的 FIFO 操作(无欠载)。如果需要,可以将此值设置得更低,但对于正常使用,最大值是可以的。
示例:
outportb(ioaddr + 0xEC, 0x3B); /* Maximum tx packet size */
接收数据包最大大小
RMS 寄存器位于偏移量 0xDA,用于设置可以接收到 NIC 中的最大数据包大小。如果设置为零,NIC 将不接受任何数据包(必须在正常操作之前设置!)。可以设置的最大大小是 0x3FFF(16k - 1),但如果接收到大于 8k 的数据包,会设置错误位,并且必须启动更长的过程来确定数据包是否确实良好。我将此值保持为 0x1FFF(8k - 1),因为很少接收到如此大的数据包,并且这也确保错误数据包确实是错误数据包,而不是误报。
示例:
outportw(ioaddr + 0xDA, 0x1FFF); /* Maximum rx packet size */
完整复位示例
这是一个关于如何复位 RTL8169 的简单示例,不包括自动协商(稍后解释)或 (G)MII 交互等内容。
示例:
struct Descriptor { uint32_t command; /* command/status uint32_t */ uint32_t vlan; /* currently unused */ uint32_t low_buf; /* low 32-bits of physical buffer address */ uint32_t high_buf; /* high 32-bits of physical buffer address */ }; /** * Note that this assumes 16*1024=16KB (4 pages) of physical memory at 1MB and 2MB is identity mapped to * the same linear address range */ struct Descriptor *Rx_Descriptors = (struct Descriptor *)0x100000; /* 1MB Base Address of Rx Descriptors */ struct Descriptor *Tx_Descriptors = (struct Descriptor *)0x200000; /* 2MB Base Address of Tx Descriptors */ unsigned long num_of_rx_descriptors = 1024, num_of_tx_descriptors = 1024; void setup_rx_descriptors() { /* rx_buffer_len is the size (in bytes) that is reserved for incoming packets */ unsigned int OWN = 0x80000000, EOR = 0x40000000; /* bit offsets */ int i; for(i = 0; i < num_of_rx_descriptors; i++) /* num_of_rx_descriptors can be up to 1024 */ { if(i == (num_of_rx_descriptors - 1)) /* Last descriptor? if so, set the EOR bit */ Rx_Descriptors[i].command = (OWN | EOR | (rx_buffer_len & 0x3FFF)); else Rx_Descriptors[i].command = (OWN | (rx_buffer_len & 0x3FFF)); /** packet_buffer_address is the *physical* address for the buffer */ Rx_Descriptors[i].low_buf = (unsigned int)&packet_buffer_address; Rx_Descriptors[i].high_buf = 0; /* If you are programming for a 64-bit OS, put the high memory location in the 'high_buf' descriptor area */ } } void reset() { unsigned int i; unsigned char mac_address[6]; outportb(ioaddr + 0x37, 0x10); /* Send the Reset bit to the Command register */ while(inportb(ioaddr + 0x37) & 0x10){} /* Wait for the chip to finish resetting */ for (i = 0; i < 6; i++) mac_address[i] = inportb(ioaddr + i); setup_rx_descriptors(); outportb(ioaddr + 0x50, 0xC0); /* Unlock config registers */ outportl(ioaddr + 0x44, 0x0000E70F); /* RxConfig = RXFTH: unlimited, MXDMA: unlimited, AAP: set (promisc. mode set) */ outportb(ioaddr + 0x37, 0x04); /* Enable Tx in the Command register, required before setting TxConfig */ outportl(ioaddr + 0x40, 0x03000700); /* TxConfig = IFG: normal, MXDMA: unlimited */ outportw(ioaddr + 0xDA, 0x1FFF); /* Max rx packet size */ outportb(ioaddr + 0xEC, 0x3B); /* max tx packet size */ /* offset 0x20 == Transmit Descriptor Start Address Register offset 0xE4 == Receive Descriptor Start Address Register Again, these are *physical* addresses. This code assumes physical==linear, this is typically not the case in real world kernels */ outportl(ioaddr + 0x20, (unsigned long)&Tx_Descriptors[0]; /* Tell the NIC where the first Tx descriptor is. NOTE: If writing a 64-bit address, split it into two 32-bit writes.*/ outportl(ioaddr + 0xE4, (unsigned long)&Rx_Descriptors[0]; /* Tell the NIC where the first Rx descriptor is. NOTE: If writing a 64-bit address, split it into two 32-bit writes.*/ outportb(ioaddr + 0x37, 0x0C); /* Enable Rx/Tx in the Command register */ outportb(ioaddr + 0x50, 0x00); /* Lock config registers */ }
另请参阅
文章
- RTL8139 - 相关的前代芯片组
外部链接
来源与翻译说明
本文根据原文快照翻译,保留原有代码、数值、技术限定及历史背景。原文中的命令与运行结果不代表本站已实际执行验证。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论