How does Linux connect many network cards with the same driver?

Recently, I am studying the linux network driver, and I wonder what if I have many network cards of the same type on my board as the kernel of their drive? Does the kernel need to load the same driver many times? I think this is impossible, insmod will not do this, since I can make all the same cards at the same time?

considers

+6
source share
2 answers

The state of each card (I / O address, IRQ, ...) is stored in a driver-specific structure, which is transmitted (directly or indirectly) to each driver entry point, which can thus distinguish cards, Thus, the same code can manage different cards (which means that yes, the kernel stores only one instance of the driver module, regardless of the number of devices it controls).

For example, look at drivers/video/backlight/platform_lcd.c , which is a very simple LCD power driver. It contains a structure called platform_lcd that is private to this file and retains the state of the LCD (regardless of whether the power is on and paused). One instance of this structure is allocated in the probe function via kzalloc , that is, one per LCD device, and stored in the platform device representing the LCD using platform_set_drvdata . The instance allocated for this device is then returned at the beginning of all other driver functions so that it knows which instance it is working on:

 struct platform_lcd *plcd = to_our_lcd(lcd); 

to_our_lcd expands to lcd_get_data , which itself extends to dev_get_drvdata (an analog of the dev_get_drvdata platform) if you look at include/linux/lcd.h Then the function can know what the state of the device was called for.

This is a very simple example, and the platform_lcd driver does not directly control any device (this is deferred to the function pointer in the platform data), but adds hardware parameters (IRQ, I / O base, etc.), and you will learn how 99% of drivers work on Linux.

+10
source

The driver code is loaded only once, but it allocates a separate context structure for each card. You will usually see a struct pci_driver with a .probe function .probe . The probe function is called once for each card using the PCI support code and calls alloc_etherdev to distribute the network interface with space for any private context that it needs.

+4
source

Source: https://habr.com/ru/post/903687/


All Articles