linux如何用同一个驱动程序驱动多个网卡?
我最近在学习linux网络驱动,我想知道如果我的板上有很多相同类型的网卡,内核如何驱动它们?内核是否需要多次加载同一个驱动程序?我认为这是不可能的,insmod不会这样做,那么我怎样才能让所有同类卡同时工作呢?
问候
I am learning linux network driver recently, and I wonder that if I have many network cards in same type on my board, how does the kernel drive them? Does the kernel need to load the same driver many times? I think it's not possible, insmod won't do that, so how can I make all same kind cards work at same time?
regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每张卡的状态(I/O 地址、IRQ 等)都存储在特定于驱动程序的结构中,该结构被(直接或间接)传递到驱动程序的每个入口点,从而可以通过这种方式区分卡。这样,相同的代码就可以控制不同的卡(这意味着,是的,内核只保留驱动程序模块的一个实例,无论它控制的设备数量有多少)。
例如,看一下
drivers/video/backlight/platform_lcd.c
,这是一个非常简单的 LCD 电源驱动程序。它包含一个名为platform_lcd
的结构,该结构对此文件私有,并存储 LCD 的状态(是否已通电,是否已挂起)。该结构的一个实例通过kzalloc
在驱动程序的probe
函数中分配 - 即每个 LCD 设备一个 - 并使用存储到表示 LCD 的平台设备中代码>platform_set_drvdata
。然后,在所有其他驱动程序函数的开头取回已为此设备分配的实例,以便它知道它正在处理哪个实例:to_our_lcd
扩展为lcd_get_data
如果您查看include/linux/lcd.h
,它本身会扩展为dev_get_drvdata
(platform_set_drvdata 的对应部分)。然后,该函数可以知道已调用的设备的状态。这是一个非常简单的示例,
platform_lcd
驱动程序并不直接控制任何设备(这被推迟到平台数据中的函数指针),而是添加特定于硬件的参数(IRQ、I/O) base 等),您就可以了解 Linux 中 99% 的驱动程序是如何工作的。The state of every card (I/O addresses, IRQs, ...) is stored into a driver-specific structure that is passed (directly or indirectly) to every entry point of the driver which can this way differenciate the cards. That way the very same code can control different cards (which means that yes, the kernel only keeps one instance of a driver's module no matter the number of devices it controls).
For instance, have a look at
drivers/video/backlight/platform_lcd.c
, which is a very simple LCD power driver. It contains a structure calledplatform_lcd
that is private to this file and stores the state of the LCD (whether it is powered, and whether it is suspended). One instance of this structure is allocated in theprobe
function of the driver throughkzalloc
- that is, one per LCD device - and stored into the platform device representing the LCD usingplatform_set_drvdata
. The instance that has been allocated for this device is then fetched back at the beginning of all other driver functions so that it knows which instance it is working on:to_our_lcd
expands tolcd_get_data
which itself expands todev_get_drvdata
(a counterpart of platform_set_drvdata) if you look atinclude/linux/lcd.h
. The function can then know the state of the device is has been invoked for.This is a very simple example, and the
platform_lcd
driver does not directly control any device (this is deferred to a function pointer in the platform data), but add hardware-specific parameters (IRQ, I/O base, etc.) and you get how 99% of the drivers in Linux work.驱动程序代码仅加载一次,但它为每个卡分配一个单独的上下文结构。通常,您会看到带有
.probe
函数指针的struct pci_driver
。 PCI 支持代码为每个卡调用一次探测函数,并调用alloc_etherdev 为网络接口分配其所需的任何私有上下文的空间。The driver code is only loaded once, but it allocates a separate context structure for each card. Typically you will see a
struct pci_driver
with a.probe
function pointer. The probe function is called once for each card by the PCI support code, and it callsalloc_etherdev
to allocate a network interface with space for whatever private context it needs.