1,首先从模块加载函数module_init(fec_enet_module_init);

static int __init fec_enet_module_init(void)

{

struct net_device *dev;

int i, j, err;

DECLARE_MAC_BUF(mac);

printk("FEC ENET Version 0.2\n");

for (i = 0; (i < FEC_MAX_PORTS); i++) {

dev = alloc_etherdev(sizeof(struct fec_enet_private));//申请一个网络设备其格式是fec_enet_private

if (!dev)

return -ENOMEM;

err = fec_enet_init(dev);

if (err) {

free_netdev(dev);

continue;

}

/

net_dev_array[i]=dev;

/

if (register_netdev(dev) != 0) {

/* XXX: missing cleanup here */

free_netdev(dev);

return -EIO;

}

printk("%s: ethernet %s\n",

dev->name, print_mac(mac, dev->dev_addr));

}

return 0;

}

2,关键结构体fec_enet_private定义了网络设备所用到的所有私有资源

struct fec_enet_private {

/* Hardware registers of the FEC device */

volatile fec_t*hwp;

struct net_device *netdev;

/* The saved address of a sent-in-place packet/buffer, for skfree(). */

unsigned char *tx_bounce[TX_RING_SIZE];

structsk_buff* tx_skbuff[TX_RING_SIZE];

ushortskb_cur;

ushortskb_dirty;

/* CPM dual port RAM relative addresses.

*/

cbd_t*rx_bd_base;/* Address of Rx and Tx buffers. */

cbd_t*tx_bd_base;

cbd_t*cur_rx, *cur_tx;/* The next free ring entry */

cbd_t*dirty_tx;/* The ring entries to be free()ed. */

uinttx_full;

spinlock_t lock;

uintphy_id;

uintphy_id_done;

uintphy_status;

uintphy_speed;

phy_info_t const*phy;

struct work_struct phy_task;

volatile fec_t*phy_hwp;

uintsequence_done;

uintmii_phy_task_queued;

uintphy_addr;

intindex;

intopened;

intlink;

intold_link;

intfull_duplex;

#ifdef CONFIG_M5445X

intspeed_10;

struct timer_list phy_timer;

unsigned long sTime;

unsigned long rxCount;

unsigned long bktCount;

unsigned long mTime;

unsigned long pktCount;

unsigned long bpkt_state;//for broadcast packet reject state machine

unsigned long bpkt_flag;//flag for reject broadcast packet

unsigned long pTime;

unsigned long bpkt_msk;

unsigned long tLimit;

unsigned long tCount;

unsigned long pkt_acount;//the all packet count in current slot

unsigned long bpkt_rcount;//the boradcast packet count in current slot

unsigned long bpkt_rlimit;//the broadcast packet receive limit

unsigned long pCount;//the total packet count

unsigned long bCount;//the total broadcast packet count

unsigned long flux_aCount;//byte count for all packet

unsigned long flux_aCount_st; //sticky of byte count for all packet

unsigned long flux_bCount;//byte count for broadcast packet

unsigned long flux_bCount_st; //sticky of byte count for broadcast packet

unsigned long flux_dCount[32];

unsigned long flux_rTime[32];

unsigned long flux_index;

unsigned long flux_wCount;

unsigned long aflux;//flux for total packet

unsigned long bflux;//flux for broadcast packet

#endif

};

//其中重要的是两个自旋锁,其中一个是发送用的,一个网络设备net_device,两个sk_buff指针一个发送一个接收,还有两个地址发送接收的buffer地址,一个计时器等等。。

socket缓冲描述结构体,包含控制状态信息以及数据长度,缓冲地址。

typedef struct bufdesc {

unsigned shortcbd_sc;/* Control and status info */

unsigned shortcbd_datlen;/* Data length */

unsigned longcbd_bufaddr;/* Buffer address */

} cbd_t;

Fec结构体定义了faste thernet control有用的寄存器具体看各成员列表:

typedef struct fec {

unsigned longfec_reserved0;

unsigned longfec_ievent;/* Interrupt event reg */

unsigned longfec_imask;/* Interrupt mask reg */

unsigned longfec_reserved1;

unsigned longfec_r_des_active;/* Receive descriptor reg */

unsigned longfec_x_des_active;/* Transmit descriptor reg */

unsigned longfec_reserved2[3];

unsigned longfec_ecntrl;/* Ethernet control reg */

unsigned longfec_reserved3[6];

unsigned longfec_mii_data;/* MII manage frame reg */

unsigned longfec_mii_speed;/* MII speed control reg */

unsigned longfec_reserved4[7];

unsigned longfec_mib_ctrlstat;/* MIB control/status reg */

unsigned longfec_reserved5[7];

unsigned longfec_r_cntrl;/* Receive control reg */

unsigned longfec_reserved6[15];

unsigned longfec_x_cntrl;/* Transmit Control reg */

unsigned longfec_reserved7[7];

unsigned longfec_addr_low;/* Low 32bits MAC address */

unsigned longfec_addr_high;/* High 16bits MAC address */

unsigned longfec_opd;/* Opcode + Pause duration */

unsigned longfec_reserved8[10];

unsigned longfec_hash_table_high;/* High 32bits hash table */

unsigned longfec_hash_table_low;/* Low 32bits hash table */

unsigned longfec_grp_hash_table_high;/* High 32bits hash table */

unsigned longfec_grp_hash_table_low;/* Low 32bits hash table */

unsigned longfec_reserved9[7];

unsigned longfec_x_wmrk;/* FIFO transmit water mark */

unsigned longfec_reserved10;

unsigned longfec_r_bound;/* FIFO receive bound reg */

unsigned longfec_r_fstart;/* FIFO receive start reg */

unsigned longfec_reserved11[11];

unsigned longfec_r_des_start;/* Receive descriptor ring */

unsigned longfec_x_des_start;/* Transmit descriptor ring */

unsigned longfec_r_buff_size;/* Maximum receive buff size */

unsigned longfec_reserved12[29]; //

unsigned longfec_mib_ram[64];//for fec mib message buffer

} fec_t;

3网络设备初始化,各个步骤:

具体实现代码如下:

/* Initialize the FEC Ethernet.

*/

/*

* XXX:We need to clean up on failure exits here.

*/

int __init fec_enet_init(struct net_device *dev)

{

struct fec_enet_private *fep = netdev_priv(dev);

unsigned longmem_addr;

volatile cbd_t*bdp;

cbd_t*cbd_base;

volatile fec_t*fecp;

int i, j;

static intindex = 0;

/* Only allow us to be probed once. */

if (index >= FEC_MAX_PORTS)

return -ENXIO;

/* Allocate memory for buffer descriptors.

*/

mem_addr = __get_free_page(GFP_DMA);//申请一个页内存,通过方式DMA

if (mem_addr == 0) {

printk("FEC: allocate descriptor memory failed?\n");

return -ENOMEM;

}

/* Create an Ethernet device instance.

*/

fecp = (volatile fec_t *) fec_hw[index];//FEC设备的基地址灰色为添加的便于观看,实际这里没有,这里看地址应该是0xfc034000 MCF_MBAR=0

/*

* Define the fixed address of the FEC hardware.

*/

static unsigned int fec_hw[] = {

#if defined(CONFIG_M5272)

(MCF_MBAR + 0x840),

#elif defined(CONFIG_M527x)

(MCF_MBAR + 0x1000),

(MCF_MBAR + 0x1800),

#elif defined(CONFIG_M523x) || defined(CONFIG_M528x)

(MCF_MBAR + 0x1000),

#elif defined(CONFIG_M520x)

(MCF_MBAR+0x30000),

#elif defined(CONFIG_M532x)

(MCF_MBAR+0xfc030000),

#elif defined(CONFIG_M5445X)

(MCF_MBAR+0xfc030000),

#if defined(CONFIG_FEC2)

(MCF_MBAR+0xfc034000),

#endif

fep->index = index;

fep->hwp = fecp;

fep->netdev = dev;

#ifdef CONFIG_FEC_SHARED_PHY

fep->phy_hwp = (volatile fec_t *) fec_hw[index & ~1];

#else

fep->phy_hwp = fecp;

#endif

/* Whack a reset.We should wait for this.

*/

fecp->fec_ecntrl = 1;//复位网络设备

udelay(10);

//

fecp->fec_mib_ctrlstat|=FEC_MIB_EN_MSK;clear mib counter

for(i=0;i<64;i++)

{

fecp->fec_mib_ram[i]=0;

}

/

/* Set the Ethernet address.If using multiple Enets on the 8xx,

* this needs some work to get unique addresses.

*

* This is our default MAC address unless the user changes

* it via eth_mac_addr (our dev->set_mac_addr handler).

*/

fec_get_mac(dev);

cbd_base = (cbd_t *)mem_addr;

/* XXX: missing check for allocation failure */

fec_uncache(mem_addr);

/* Set receive and transmit descriptor base.

*/

fep->rx_bd_base = cbd_base;

fep->tx_bd_base = cbd_base + RX_RING_SIZE;

fep->dirty_tx = fep->cur_tx = fep->tx_bd_base;

fep->cur_rx = fep->rx_bd_base;

fep->skb_cur = fep->skb_dirty = 0;

/* Initialize the receive buffer descriptors.

*/

bdp = fep->rx_bd_base;

for (i=0; i

/* Allocate a page.

*/

mem_addr = __get_free_page(GFP_DMA);//再次申请内存

/* XXX: missing check for allocation failure */

fec_uncache(mem_addr);

/* Initialize the BD for every fragment in the page.

*/

for (j=0; j

bdp->cbd_sc = BD_ENET_RX_EMPTY;

bdp->cbd_bufaddr = __pa(mem_addr);//虚拟地址到物理地址的转换

mem_addr += FEC_ENET_RX_FRSIZE;

bdp++;

}

}

/* Set the last buffer to wrap.

*/

bdp--;

bdp->cbd_sc |= BD_SC_WRAP;

/* ...and the same for transmmit.

*/

bdp = fep->tx_bd_base;

for (i=0, j=FEC_ENET_TX_FRPPG; i

if (j >= FEC_ENET_TX_FRPPG) {

mem_addr = __get_free_page(GFP_DMA);

j = 1;

} else {

mem_addr += FEC_ENET_TX_FRSIZE;

j++;

}

fep->tx_bounce[i] = (unsigned char *) mem_addr;

/* Initialize the BD for every fragment in the page.

*/

bdp->cbd_sc = 0;

bdp->cbd_bufaddr = 0;

bdp++;

}

/* Set the last buffer to wrap.

*/

bdp--;

bdp->cbd_sc |= BD_SC_WRAP;

/* Set receive and transmit descriptor base.

*/

fecp->fec_r_des_start = __pa((uint)(fep->rx_bd_base));

fecp->fec_x_des_start = __pa((uint)(fep->tx_bd_base));

/* Install our interrupt handlers. This varies depending on

* the architecture.

*/

fec_request_intrs(dev);//注册设备中断

fecp->fec_grp_hash_table_high = 0;

fecp->fec_grp_hash_table_low = 0;

fecp->fec_r_buff_size = PKT_MAXBLR_SIZE;

fecp->fec_ecntrl = 2;

fecp->fec_r_des_active = 0;

#ifndef CONFIG_M5272

fecp->fec_hash_table_high = 0;

fecp->fec_hash_table_low = 0;

#endif//设置一些寄存器

dev->base_addr = (unsigned long)fecp;将寄存器地址赋值给网络设备基地址

/* The FEC Ethernet specific entries in the device structure. */

dev->open = fec_enet_open;

dev->hard_start_xmit = fec_enet_start_xmit;//该函数特殊,是上层协议调用的函数

dev->tx_timeout = fec_timeout;

dev->watchdog_timeo = TX_TIMEOUT;

dev->stop = fec_enet_close;

dev->set_multicast_list = set_multicast_list;

for (i=0; i

mii_cmds[i].mii_next = &mii_cmds[i+1];

mii_free = mii_cmds;

/* setup MII interface */

fec_set_mii(dev, fep);//设置为mii方式

/* Clear and enable interrupts */

fecp->fec_ievent = 0xffc00000;

fecp->fec_imask = (FEC_ENET_TXF | FEC_ENET_TXB |

FEC_ENET_RXF | FEC_ENET_RXB | FEC_ENET_MII);

/* Queue up command to detect the PHY and initialize the

* remainder of the interface.

*/

fep->phy_id_done = 0;

#ifndef CONFIG_FEC_SHARED_PHY

fep->phy_addr = 0;

#else

fep->phy_addr = fep->index;

#endif

mii_queue(dev, mk_mii_read(MII_REG_PHYIR1), mii_discover_phy);

index++;

/

fecp->fec_mib_ctrlstat&=~FEC_MIB_EN_MSK;//enable fec mib counter,add by pjy on 2012-2-27

/***************************************************************************************************************/

return 0;

}

4申请中断函数实现

static void __inline__ fec_request_intrs(struct net_device *dev)

{

struct fec_enet_private *fep;

int b;

static const struct idesc {

char *name;

unsigned short irq;

} *idp, id[] = {

{ "fec(TXF)", 36 },

{ "fec(TXB)", 37 },

{ "fec(TXFIFO)", 38 },

{ "fec(TXCR)", 39 },

{ "fec(RXF)", 40 },

{ "fec(RXB)", 41 },

{ "fec(MII)", 42 },

{ "fec(LC)", 43 },

{ "fec(HBERR)", 44 },

{ "fec(GRA)", 45 },

{ "fec(EBERR)", 46 },

{ "fec(BABT)", 47 },

{ "fec(BABR)", 48 },

{ NULL },

};//中断名及中断号

fep = netdev_priv(dev);

b = (fep->index) ? 77 : 64;

/* Setup interrupt handlers. */

for (idp = id; idp->name; idp++) {

if (request_irq(b+idp->irq,fec_enet_interrupt, IRQF_DISABLED,//申请中断,中断处理函数为fec_enet_interrupt

idp->name, dev) != 0)

printk(KERN_ERR "FEC: Could not alloc %s IRQ(%d)!\n",

idp->name, b+idp->irq);

}

#if 1 //,we use MII

if (fep->index) {/* Configure FEC1 to MII */

MCF_GPIO_PAR_FEC = (MCF_GPIO_PAR_FEC &

MCF_GPIO_PAR_FEC_FEC1_MASK) |MCF_GPIO_PAR_FEC_FEC1_MII;}

else {/* Configure FEC0 to MII */

MCF_GPIO_PAR_FEC = (MCF_GPIO_PAR_FEC &

MCF_GPIO_PAR_FEC_FEC0_MASK) |MCF_GPIO_PAR_FEC_FEC0_MII;}

#else // we use MII

if (fep->index) {

/* Configure RMII */

MCF_GPIO_PAR_FEC = (MCF_GPIO_PAR_FEC &

MCF_GPIO_PAR_FEC_FEC1_MASK) |

MCF_GPIO_PAR_FEC_FEC1_RMII_GPIO;

} else {

/* Configure RMII */

MCF_GPIO_PAR_FEC = (MCF_GPIO_PAR_FEC &

MCF_GPIO_PAR_FEC_FEC0_MASK) |

MCF_GPIO_PAR_FEC_FEC0_RMII_GPIO;

}

#endif

/* Set up gpio outputs for MII lines on FEC0 */

MCF_GPIO_PAR_FECI2C |= (0 |

MCF_GPIO_PAR_FECI2C_MDIO0_MDIO0 |

MCF_GPIO_PAR_FECI2C_MDC0_MDC0);

}

中断处理函数实现:

static irqreturn_t

fec_enet_interrupt(int irq, void * dev_id)

{

structnet_device *dev = dev_id;

//structnet_device *pdev;

volatile fec_t*fecp;

uintint_events;

int handled = 0;

int i;

/

fecp = (volatile fec_t*)dev->base_addr;

/* Get the interrupt events that caused us to be here.

*/

//fecp->fec_imask &= ~FEC_ENET_RXF; //disable rx int

while ((int_events = fecp->fec_ievent) != 0) {

//fecp->fec_ievent = int_events;//

/* Handle receive event in its own function.

*/

if (int_events & FEC_ENET_RXF) {

handled = 1;

fec_enet_rx(dev);//接收处理

}

fecp->fec_ievent = int_events;//

//fecp->fec_imask |=FEC_ENET_RXF; //enable rx int

/* Transmit OK, or non-fatal error. Update the buffer

descriptors. FEC handles all errors, we just discover

them as part of the transmit process.

*/

if (int_events & FEC_ENET_TXF) {

handled = 1;

fec_enet_tx(dev);//发送处理

}

if (int_events & FEC_ENET_MII) {

handled = 1;

fec_enet_mii(dev);//mii处理

}

}

return IRQ_RETVAL(handled);

}

5上层接口函数含义:open stop等:

dev->open = fec_enet_open;//打开网络设备函数

dev->hard_start_xmit = fec_enet_start_xmit;//该函数特殊,是上层协议调用的函数

dev->tx_timeout = fec_timeout;发送超时函数处理

dev->watchdog_timeo = TX_TIMEOUT;超时时间设置

dev->stop = fec_enet_close;//关闭网络设备

dev->set_multicast_list = set_multicast_list;

6发送函数:

7接收函数:

待续。。。

linux网卡的fec功能,网络控制器驱动程序学习记录fec(1)相关推荐

  1. linux内核 rps/rfs功能详细测试分析,学习Linux-4.12内核网路协议栈(2.1)——接口层加快传输速率的特性...

    前面花了好多篇幅,终于将网络协议栈的初始化相关的内容介绍完了,也就是说完成前面的那些步骤以后,网络协议栈具备了数据包的收发功能.在网络接口层,它只负责数据包的接收与发送,而不关注数据包在网络层的类型是 ...

  2. 网络安全等级保护学习记录--工作流程、技术要求、管理要求、工作步骤

    1.工作流程 各阶段实施过程中,可参考的规范性标准依据如下: 系统定级阶段:<信息系统安全保护等级定级指南> 安全保护:<信息系统安全等级保护基本要求>.<信息系统安全等 ...

  3. 内存映射和多功能LED控制器驱动芯片/带按键扫描的LED驱动芯片(IC)-VK16K33A/B/C,内置RC振荡器,最大16seg和8com

    品牌:永嘉微电/VINKA ­工程服务,技术支持 型号:VK16K33A/B/C 封装:SOP28/24/20 年份:新年份 概述: VK16K33是一个内存映射和多功能LED控制器驱动程序.VK16 ...

  4. HCIE学习记录——数通网络基础

    HCIE学习记录 数通网络基础 HCIE学习记录 前言 一.什么是网络? 1.通信的五要素 二.简单网络设备 1.交换机 1.1.广播域 2.路由器 总结 前言 HCIE数通方向学习记录. 一.什么是 ...

  5. linux网卡驱动程序分析

    linux网卡驱动程序分析 学习应该是一个先把问题简单化,在把问题复杂化的过程.一开始就着手处理复杂的问题,难免让人 有心惊胆颤,捉襟见肘的感觉.读Linux网卡驱动也是一样.那长长的源码夹杂着那些我 ...

  6. Linux网卡驱动程序编写

    Linux网卡驱动程序编写 [摘自 LinuxAID] 工作需要写了我们公司一块网卡的Linux驱动程序.经历一个从无到有的过程,深感技术交流的重要.Linux作为挑战微软垄断的强有力武器,日益受到大 ...

  7. linux 网卡的驱动程序,Linux网卡驱动程序代码

    广告 100%的CPU性能,计算能力不会降低!选择最主流的云服务器来满足各种业务需求,有数百种流行的云产品和8888元起价套餐,可帮助行业恢复工作! 获取网卡信息的代码示例. 通过命令获取arp(地址 ...

  8. linux 网卡驱动编译安装包,linux网卡驱动程序的编译与安装

    一般来说,目前新版的Linux预设可以支持的网络卡芯片组数量已经很完备了,很多网络卡芯片都已经被支持, 例如RLT 8139芯片( RealTek 8139 )的网络卡所以使用者可以很轻易的设定好他们 ...

  9. linux网卡驱动开发视频,Linux下网卡驱动程序的开发.doc

    Linux下网卡驱动程序的开发 论文题目:Linux下网卡驱动程序的开发 专 业: 年 级: 学生学号: 学生姓名: 指导教师: 完成时间: Linux下网卡驱动程序的开发 八年经验 专业指导毕业设计 ...

最新文章

  1. python标准库介绍——4 string模块详解
  2. 实现网页中增加刷新按钮、链接的方法 搜集
  3. Nacos 2.0 性能提升十倍,贡献者 80% 以上来自阿里之外
  4. 视觉SLAM学习--相机成像模型及标定
  5. 黑马程序员_java基础笔记(06)...集合
  6. sql server 并发_并发问题– SQL Server中的理论和实验
  7. python安装详细步骤mac_Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
  8. hibernate环境搭建
  9. Office 365 Connectors 的使用与自定义开发
  10. day6面向对象--继承、多态
  11. 华章教育pdf计算机,AB-PLC华章培训全集.pdf
  12. Roaring Bitmaps结构原理
  13. 计算机网络复习03——数据链路层
  14. Android Provision 的作用
  15. 在线EXCEL编辑器-Luckysheet
  16. nginx端口映射配置(Windows)
  17. Java使用POI导出Excel .xlsx提示文件格式或文件扩展名有误
  18. MybatisPlus中and和or的使用
  19. 苹果开发者账号修改公司名称的过程
  20. 如何让你爱的人爱上你

热门文章

  1. JVM——内存与垃圾回收
  2. H3C 三层交换机策略路由配置方法
  3. 自 2017 年发表以来被引用次数最多的论文合集——大数据篇
  4. 金融时间序列Note 1 —— 平稳性,相关系数,自相关函数(ACF)及其平稳性检验
  5. 网站图片优化技巧助关键词快速提升
  6. 怎样设置计算机u盘启动程序,设置u盘启动|教您怎么设置电脑U盘启动
  7. 多核实时调度—任务分配启发式算法解读FF,NF,BF,WF
  8. ES7-12内容详解
  9. 阿昌带你一起学习若依之【项目结构、登录实现】 -若依开源项目
  10. 制作简单的响应式HTML邮件