Analysis of key functions of the driver of the Rtl8139too Nic in Linux
Source: Internet
Author: User
Linux Rtl8139too Nic Device Driver key function analysis-Linux general technology-Linux programming and kernel information, the following is a detailed reading. Static int _ devinit rtl8139_init_one (struct pci_dev * pdev, const struct pci_device_id * ent)
Function Description: The probe function pointer assigned to the pci_driver struct. It is used to Start chemical processing for the corresponding devices when the PCI core detects a pci_dev to be controlled.
Note: The main task of this function is to create and initialize the net_device structure, which is the abstraction of the NIC device.
Function process:
1. Call the alloc_etherdev function to create an Ethernet NIC device. It encapsulates the alloc_netdev function and provides the setup function in the kernel for setting general Ethernet attributes.
2. enable the current PCI device, including the establishment of functions such as wake-up and hot swapping.
3. Read the Base Address Register in the PCI configuration space. PCI local bus specification specifies that the PCI configuration space has six 32-bit registers that hold the base address (that is, the bar0-bar5 ). The 8139too NIC driver uses two of them to indicate the I/O space base address ing and memory space base address ing respectively.
4. Call the pci_request_regions function to apply for resources. This function actually calls request_region and request_mem_region, which are responsible for allocating I/O Ports and memory resources respectively. (Resources in the kernel are generally divided into four categories: I/O Ports, memory, DMA channels, and break-down. They are described by the resource structure, the difference is that each type of resources has a common base class object, which reflects the OO idea in kernel design. In fact, typical object-oriented ideas such as encapsulation, inheritance, and polymorphism are embodied in the kernel. Each type of resources is organized in a tree structure, and the source code is displayed. This tree structure is actually a combination of "parent-parent notation" and "child-brother notation ).
5. Set the dev-> base_addr field, depending on the driver's I/O space or memory space. If I/O space is used, I/O port addresses must also be mapped to memory addresses (this is a good practice); otherwise, re-map the memory address in the bar1 register in the PCI configuration space. The driver can access the memory area only after the re- ing. This involves adding page table items.
6. Reset the chip.
7. initialize some loose functions in the net_device structure, which are responsible for receiving and transmitting data packets.
Static int rtl8139_open (struct net_device * dev)
Function Description: applies for resources, starts hardware, and enables monitoring threads.
Function process:
1. Call the request_irq function to distribute the disconnection to the NIC device in a shared manner (ir1__shared). The interrupt number is stored in dev-> irq.
2. Apply for memory for the DMA device. Call the pci_alloc_consistent function. It returns two addresses. One is the virtual kernel address used by the device driver; the other is the BUS address used by the DMA controller (which is equivalent to a physical address in some architectures ). The physical memory of the device applied using this function must be consecutive, because the DMA Operation transfers physical contiguous data between the master memory and the FIFO cache of the network card. The pci_alloc_consistent function actually calls the _ get_free_pages function to obtain the Memory Page, and uses the vrit_to_phys function to convert the obtained Memory Page virtual address to the physical address.
3. initialize the ring buffer ..
4. Start the hardware. First reset the NIC device, then write the hardware address read from the EEPROM device into the corresponding register, and then assign the BUS address of the input/output DMA buffer obtained in step 3 to the corresponding register, finally, set the multicast list to enable all known interruptions.
5. Start the transmission queue and accept packets from the network layer.
6. Start the monitoring thread, which is responsible for processing when the transmission times out. This monitoring thread is implemented using a working queue. By using the schedule_delayed_work function, this thread function can be executed at a specified time point.
Function Description: copy the data obtained at the network layer to the DMA buffer.
Function process:
1. copy the data in the sk_buf structure to one of the four DMA output buffers to ensure that the package size is at least ETH_ZLEN.
2. Determine whether the four buffers have been used up. If yes, call the netif_stop_queue function to suspend the transport queue at the network layer. Note that after the sk_buf structure is copied, update the tp-> rx_curr pointer, which is an unsigned long type. Meanwhile, the driver also maintains the tp-> rx_dirty pointer. The two pointers are responsible for data synchronization in the four-way DMA output buffer zone. They all increase progressively. When the difference between the two is NUM_TX_DESC, it means that the four DMA output buffers have been filled and not yet transmitted, at this time, you need to stop the network layer and continue to send skb to the driver.
Note: In general, interruption means that the data in the FIFO has been sent or the DMA has copied the received data from the FIFO to the DMA buffer. More specifically, when early mode is disabled, interruption indicates that a complete data packet is sent to the network media or a complete data packet has been transferred to the memory buffer through DMA. To avoid frequent system interruptions caused by the receipt of a large number of small data packets, NAPI is provided in the Linux kernel, that is, when the reception interruption is disabled, receives data in Round Robin mode.
Function process:
1. Read the interrupt Status Register and check whether the interrupt belongs to the current Nic device. This step is required because it can be used in a shared manner.
2. if the receiving interrupt bit is set, call the netif_rx_schedule_prep function to add the dev-> poll method of the device object to the poll queue at the network layer. If the function is successfully executed, the corresponding bit of the interrupt Status Register is cleared (the method is to write 1 in the bit to be cleared), and then the _ netif_rx_schedule function is called on the device object, it will initiate a soft interrupt when necessary, so as to notify the network layer to begin to accept data packets (that is, call the dev-> poll function ).
3. If the transmission interruption is set to a bit, confirm the transmitted data packets. Specifically, the transmission information is obtained by reading the corresponding transmission Status Register. The chip Manual specifies that when the driver writes the data packet size to the Register at 12-bit lower, it must set its 13-bit value to 0, and when the DMA Operation is completed, this bit is automatically set to 1. In this case, it is a handshake operation with the DMA controller. It should be clearly pointed out that the operations on data packets transmitted to the driver at the network layer and placed in the DMA transmission buffer are the responsibility of the rtl8139_start_xmit function mentioned above, the interrupt processing function only confirms the sent data packets and records some information, such as the total number of sent data packets, the total number of sent data packets, and the total number of discarded data packets.
4. Steps 1, 2, and 3 should be protected by spin locks.
Static int rtl8139_poll (struct net_device * dev, int * budget)
Function Description: Copies the data in the DMA input buffer to the sk_buf structure and delivers the data to the network layer for further processing.
Note: This function is assigned to the poll field in the net_device structure. It starts from a soft interrupt and is continuously called until the data is processed. The entire process is protected by the input spin lock.
Function process:
1. Clear the corresponding bit of the interrupt Status Register.
2. Read the data packet from the input DMA buffer of rx_ring, apply for and initialize the sk_buf structure, and fill it with the data packet in rx_ring. Then call the netif_receive_skb function to deliver the sk_buf structure to the network layer. It should be noted that the driver maintains the processing sequence of multiple data packets in rx_ring by using the unsigned int variable cur_rx in the rtl8139_private structure. The status and length of the received packet are placed independently of the first 4 bytes of each packet.
3. update the CAPR (Current Address of Packet Read) Register. The Address must be in dual-font alignment. When calculating the cur_rx value, use the expression cur_rx = (cur_rx + rx_size + 4 + 3) &~ 3.
4. Repeat the operations in step 2 until all data packets in rx_ring are processed.
5. Use the _ netif_rx_complete function to clear the current device from the poll queue and enable the device to receive data interruptions. Due to the competition between the receiving interrupt and the _ netif_rx_complete function, a new reception interrupt may occur when the device is cleared from the poll queue, these two operations should be performed in the state of shielding local cpu interruptions.
Postscript:
Writing such an article is quite stressful for me. On the one hand, I am a newbie in the field of Linux device driver development; on the other hand, I just read the code for a few days to write a summary. More importantly, I am not very familiar with some of the content in the chip manual, for example, the use of early mode. Therefore, the errors in this article are inevitable. In addition, I have been surprised over the past few days that the 06-level master will change back to 2 and a half years, and I am not very happy with it. I would like to take this opportunity to comfort myself and my fellow students in the lab. Borrow party's Senior Brother's qq signature file: changes can be changed, and changes cannot be received.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service