DM9000是开发板经采用的网络芯片,是一种高度集成而且功耗很低的高速网络控制器,可以和CPU直连,支持10/100M以太网连接,芯片内部自带16K SARM(3KB用来发送,13KB用来接收).
1.模块初始化
staTIc struct platform_driver dm9000_driver = {
.driver = {
.name = "dm9000",
.owner = THIS_MODULE,
},
.probe = dm9000_probe,
.remove = __devexit_p(dm9000_drv_remove),
.suspend = dm9000_drv_suspend,
.resume = dm9000_drv_resume,
};
staTIc int __init
dm9000_init(void)
{
printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);
return platform_driver_register(&dm9000_driver);
}
staTIc struct platform_driver dm9000_driver = {.driver= {.name = "dm9000",.owner = THIS_MODULE,},.probe = dm9000_probe,.remove = __devexit_p(dm9000_drv_remove),.suspend = dm9000_drv_suspend,.resume = dm9000_drv_resume,};staTIc int __initdm9000_init(void){printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);return platform_driver_register(&dm9000_driver);}
模块初始化完成了基于platfrom平台的DM9000网卡驱动的注册,当DM9000网卡找到其对应的能处理的platform设备后调用probe函数。
2.DM9000网卡初始化
在probe函数中完成了对DM9000网卡的初始化
DM9000的特性:DM9000地址信号和数据信号复用使用CMD引脚区分它们(CMD为低是读写DM900地址寄存器,CMD为高时读写DM9000数据寄存器),访问DM9000内部寄存器时,先将CMD置低,写DM900地址寄存器,然后将CMD置高,读写DM9000数据寄存器。
static int __devinit
dm9000_probe(struct platform_device *pdev)
{
struct dm9000_plat_data *pdata = pdev->dev.platform_data;
struct board_info *db; /* Point a board information structure */
struct net_device *ndev;
const unsigned char *mac_src;
int ret = 0;
int iosize;
int i;
u32 id_val;
/* Init network device */
//申请net_device结构
ndev = alloc_etherdev(sizeof(struct board_info));
if (!ndev) {
dev_err(&pdev->dev, "could not allocate device.\n");
return -ENOMEM;
}
//将net_device的parent指针指向platform_device对象,表示该设备挂载platform设备上。
SET_NETDEV_DEV(ndev, &pdev->dev);
dev_dbg(&pdev->dev, "dm9000_probe()\n");
/* setup board info structure */
//获取net_device私有数据结构指针
db = netdev_priv(ndev);
memset(db, 0, sizeof(*db));
//设置相关设备
db->dev = &pdev->dev;
db->ndevndev = ndev;
spin_lock_init(&db->lock);
mutex_init(&db->addr_lock);
INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);
//获取平台设备资源。包括DM9000地址寄存器地址,DM9000数据寄存器地址,和DM900所占用的中断号
db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
if (db->addr_res == NULL || db->data_res == NULL ||
db->irq_res == NULL) {
dev_err(db->dev, "insufficient resources\n");
ret = -ENOENT;
goto out;
}
//申请地址寄存器IO内存区域并映射
iosize = res_size(db->addr_res);
db->addr_req = request_mem_region(db->addr_res->start, iosize,
pdev->name);
if (db->addr_req == NULL) {
dev_err(db->dev, "cannot claim address reg area\n");
ret = -EIO;
goto out;
}
db->io_addr = ioremap(db->addr_res->start, iosize);
if (db->io_addr == NULL) {
dev_err(db->dev, "failed to ioremap address reg\n");
ret = -EINVAL;
goto out;
}
//申请数据寄存器IO内存区域并映射
iosize = res_size(db->data_res);
db->data_req = request_mem_region(db->data_res->start, iosize,
pdev->name);
if (db->data_req == NULL) {
dev_err(db->dev, "cannot claim data reg area\n");
ret = -EIO;
goto out;
}
db->io_data = ioremap(db->data_res->start, iosize);
if (db->io_data == NULL) {
dev_err(db->dev, "failed to ioremap data reg\n");
ret = -EINVAL;
goto out;
}
/* fill in parameters for net-dev structure */
ndev->base_addr = (unsigned long)db->io_addr;
ndev->irq = db->irq_res->start;
//设置数据位宽
/* ensure at least we have a default set of IO routines */
dm9000_set_io(db, iosize);
/* check to see if anything is being over-ridden */
if (pdata != NULL) {
/* check to see if the driver wants to over-ride the
* default IO width */
if (pdata->flags & DM9000_PLATF_8BITONLY)
dm9000_set_io(db, 1);
if (pdata->flags & DM9000_PLATF_16BITONLY)
dm9000_set_io(db, 2);
if (pdata->flags & DM9000_PLATF_32BITONLY)
dm9000_set_io(db, 4);
/* check to see if there are any IO routine
* over-rides */
if (pdata->inblk != NULL)
db->inblk = pdata->inblk;
if (pdata->outblk != NULL)
db->outblk = pdata->outblk;
if (pdata->dumpblk != NULL)
db->dumpblk = pdata->dumpblk;
db->flags = pdata->flags;
}
#ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
db->flags |= DM9000_PLATF_SIMPLE_PHY;
#endif
//复位网卡芯片
dm9000_reset(db);
//读取设备ID,判断是否是驱动能够处理的网卡芯片
/* try multiple times, DM9000 sometimes gets the read wrong */
for (i = 0; i < 8; i++) {
id_val = ior(db, DM9000_VIDL);
id_val |= (u32)ior(db, DM9000_VIDH) << 8;
id_val |= (u32)ior(db, DM9000_PIDL) << 16;
id_val |= (u32)ior(db, DM9000_PIDH) << 24;
if (id_val == DM9000_ID)
break;
dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
}
if (id_val != DM9000_ID) {
dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
ret = -ENODEV;
goto out;
}
/* Identify what type of DM9000 we are working on */
id_val = ior(db, DM9000_CHIPR);
dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);
switch (id_val) {
case CHIPR_DM9000A:
db->type = TYPE_DM9000A;
break;
case CHIPR_DM9000B:
db->type = TYPE_DM9000B;
break;
default:
dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
db->type = TYPE_DM9000E;
}
/* from this point we assume that we have found a DM9000 */
/* driver system function */
ether_setup(ndev);
//设置网卡芯片的接口函数
ndev->open = &dm9000_open;
ndev->hard_start_xmit = &dm9000_start_xmit;
ndev->tx_timeout = &dm9000_timeout;
ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
ndev->stop = &dm9000_stop;
ndev->set_multicast_list = &dm9000_hash_table;
ndev->ethtool_ops = &dm9000_ethtool_ops;
ndev->do_ioctl = &dm9000_ioctl;
#ifdef CONFIG_NET_POLL_CONTROLLER
ndev->poll_controller = &dm9000_poll_controller;
#endif
db->msg_enable = NETIF_MSG_LINK;
db->mii.phy_id_mask = 0x1f;
db->mii.reg_num_mask = 0x1f;
db->mii.force_media = 0;
db->mii.full_duplex = 0;
db->mii.dev = ndev;
db->mii.mdio_read = dm9000_phy_read;
db->mii.mdio_write = dm9000_phy_write;
mac_src = "eeprom";
//从EEPROM中读取MAC地址填充dev_addr
/* try reading the node address from the attached EEPROM */
for (i = 0; i < 6; i += 2)
dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);
if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
mac_src = "platform data";
memcpy(ndev->dev_addr, pdata->dev_addr, 6);
}
if (!is_valid_ether_addr(ndev->dev_addr)) {
/* try reading from mac */
mac_src = "chip";
for (i = 0; i < 6; i++)
ndev->dev_addr[i] = ior(db, i+DM9000_PAR);
}
if (!is_valid_ether_addr(ndev->dev_addr))
dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
"set using ifconfig\n", ndev->name);
//设置平台设备驱动的dev成员为ndev。
platform_set_drvdata(pdev, ndev);
//注册网络设备驱动
ret = register_netdev(ndev);
if (ret == 0)
printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
ndev->name, dm9000_type_to_char(db->type),
db->io_addr, db->io_data, ndev->irq,
ndev->dev_addr, mac_src);
return 0;
out:
dev_err(db->dev, "not found (%d).\n", ret);
dm9000_release_board(pdev, db);
free_netdev(ndev);
return ret;
}
static int __devinitdm9000_probe(struct platform_device *pdev){struct dm9000_plat_data *pdata = pdev->dev.platform_data;struct board_info *db;/* Point a board information structure */struct net_device *ndev;const unsigned char *mac_src;int ret = 0;int iosize;int i;u32 id_val;/* Init network device *///申请net_device结构ndev = alloc_etherdev(sizeof(struct board_info));if (!ndev) {dev_err(&pdev->dev, "could not allocate device.\n");return -ENOMEM;}//将net_device的parent指针指向platform_device对象,表示该设备挂载platform设备上。SET_NETDEV_DEV(ndev, &pdev->dev);dev_dbg(&pdev->dev, "dm9000_probe()\n");/* setup board info structure *///获取net_device私有数据结构指针db = netdev_priv(ndev);memset(db, 0, sizeof(*db));//设置相关设备db->dev = &pdev->dev;db->ndev = ndev;spin_lock_init(&db->lock);mutex_init(&db->addr_lock);INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);//获取平台设备资源。包括DM9000地址寄存器地址,DM9000数据寄存器地址,和DM900所占用的中断号db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);db->irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);if (db->addr_res == NULL || db->data_res == NULL ||db->irq_res == NULL) {dev_err(db->dev, "insufficient resources\n");ret = -ENOENT;goto out;}//申请地址寄存器IO内存区域并映射iosize = res_size(db->addr_res);db->addr_req = request_mem_region(db->addr_res->start, iosize,pdev->name);if (db->addr_req == NULL) {dev_err(db->dev, "cannot claim address reg area\n");ret = -EIO;goto out;}db->io_addr = ioremap(db->addr_res->start, iosize);if (db->io_addr == NULL) {dev_err(db->dev, "failed to ioremap address reg\n");ret = -EINVAL;goto out;}//申请数据寄存器IO内存区域并映射iosize = res_size(db->data_res);db->data_req = request_mem_region(db->data_res->start, iosize,pdev->name);if (db->data_req == NULL) {dev_err(db->dev, "cannot claim data reg area\n");ret = -EIO;goto out;}db->io_data = ioremap(db->data_res->start, iosize);if (db->io_data == NULL) {dev_err(db->dev, "failed to ioremap data reg\n");ret = -EINVAL;goto out;}/* fill in parameters for net-dev structure */ndev->base_addr = (unsigned long)db->io_addr;ndev->irq= db->irq_res->start;//设置数据位宽/* ensure at least we have a default set of IO routines */dm9000_set_io(db, iosize);/* check to see if anything is being over-ridden */if (pdata != NULL) {/* check to see if the driver wants to over-ride the* default IO width */if (pdata->flags & DM9000_PLATF_8BITONLY)dm9000_set_io(db, 1);if (pdata->flags & DM9000_PLATF_16BITONLY)dm9000_set_io(db, 2);if (pdata->flags & DM9000_PLATF_32BITONLY)dm9000_set_io(db, 4);/* check to see if there are any IO routine* over-rides */if (pdata->inblk != NULL)db->inblk = pdata->inblk;if (pdata->outblk != NULL)db->outblk = pdata->outblk;if (pdata->dumpblk != NULL)db->dumpblk = pdata->dumpblk;db->flags = pdata->flags;}#ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLLdb->flags |= DM9000_PLATF_SIMPLE_PHY;#endif//复位网卡芯片dm9000_reset(db);//读取设备ID,判断是否是驱动能够处理的网卡芯片/* try multiple times, DM9000 sometimes gets the read wrong */for (i = 0; i < 8; i++) {id_val = ior(db, DM9000_VIDL);id_val |= (u32)ior(db, DM9000_VIDH) << 8;id_val |= (u32)ior(db, DM9000_PIDL) << 16;id_val |= (u32)ior(db, DM9000_PIDH) << 24;if (id_val == DM9000_ID)break;dev_err(db->dev, "read wrong id 0x%08x\n", id_val);}if (id_val != DM9000_ID) {dev_err(db->dev, "wrong id: 0x%08x\n", id_val);ret = -ENODEV;goto out;}/* Identify what type of DM9000 we are working on */id_val = ior(db, DM9000_CHIPR);dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);switch (id_val) {case CHIPR_DM9000A:db->type = TYPE_DM9000A;break;case CHIPR_DM9000B:db->type = TYPE_DM9000B;break;default:dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);db->type = TYPE_DM9000E;}/* from this point we assume that we have found a DM9000 *//* driver system function */ether_setup(ndev);//设置网卡芯片的接口函数ndev->open = &dm9000_open;ndev->hard_start_xmit = &dm9000_start_xmit;ndev->tx_timeout = &dm9000_timeout;ndev->watchdog_timeo = msecs_to_jiffies(watchdog);ndev->stop = &dm9000_stop;ndev->set_multicast_list = &dm9000_hash_table;ndev->ethtool_ops = &dm9000_ethtool_ops;ndev->do_ioctl = &dm9000_ioctl;#ifdef CONFIG_NET_POLL_CONTROLLERndev->poll_controller = &dm9000_poll_controller;#endifdb->msg_enable = NETIF_MSG_LINK;db->mii.phy_id_mask = 0x1f;db->mii.reg_num_mask = 0x1f;db->mii.force_media = 0;db->mii.full_duplex = 0;db->mii.dev = ndev;db->mii.mdio_read = dm9000_phy_read;db->mii.mdio_write = dm9000_phy_write;mac_src = "eeprom";//从EEPROM中读取MAC地址填充dev_addr/* try reading the node address from the attached EEPROM */for (i = 0; i < 6; i += 2)dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {mac_src = "platform data";memcpy(ndev->dev_addr, pdata->dev_addr, 6);}if (!is_valid_ether_addr(ndev->dev_addr)) {/* try reading from mac */mac_src = "chip";for (i = 0; i < 6; i++)ndev->dev_addr[i] = ior(db, i+DM9000_PAR);}if (!is_valid_ether_addr(ndev->dev_addr))dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please ""set using ifconfig\n", ndev->name);//设置平台设备驱动的dev成员为ndev。platform_set_drvdata(pdev, ndev);//注册网络设备驱动ret = register_netdev(ndev);if (ret == 0)printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",ndev->name, dm9000_type_to_char(db->type),db->io_addr, db->io_data, ndev->irq,ndev->dev_addr, mac_src);return 0;out:dev_err(db->dev, "not found (%d).\n", ret);dm9000_release_board(pdev, db);free_netdev(ndev);return ret;}
我们在来看看读写网卡寄存器所用的ior和iow
static u8
ior(board_info_t * db, int reg)
{
writeb(reg, db->io_addr);
return readb(db->io_data);
}
static void
iow(board_info_t * db, int reg, int value)
{
writeb(reg, db->io_addr);
writeb(value, db->io_data);
}
static u8ior(board_info_t * db, int reg){writeb(reg, db->io_addr);return readb(db->io_data);}static voidiow(board_info_t * db, int reg, int value){writeb(reg, db->io_addr);writeb(value, db->io_data);}
可以看得出是先将要访问的寄存器地址写入到地址寄存器,然后在将数据写入到数据寄存器。地址。
3.打开网卡
在linux终端下使用ifconfig命令时调用net_device的open函数打开网卡设备
static int
dm9000_open(struct net_device *dev)
{
board_info_t *db = netdev_priv(dev);
unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;
if (netif_msg_ifup(db))
dev_dbg(db->dev, "enabling %s\n", dev->name);
/* If there is no IRQ type specified, default to something that
* may work, and tell the user that this is a problem */
if (irqflags == IRQF_TRIGGER_NONE)
dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");
irqflags |= IRQF_SHARED;
//申请中断
if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))
return -EAGAIN;
/* Initialize DM9000 board */
//复位网卡芯片
dm9000_reset(db);
//初始化网卡(相关寄存器设置)
dm9000_init_dm9000(dev);
/* Init driver variable */
db->dbug_cnt = 0;
mii_check_media(&db->mii, netif_msg_link(db), 1);
//打开发送队列
netif_start_queue(dev);
//调度发送队列开始工作
dm9000_schedule_poll(db);
return 0;
}
static intdm9000_open(struct net_device *dev){board_info_t *db = netdev_priv(dev);unsigned long irqflags = db->irq_res->flags & IRQF_TRIGGER_MASK;if (netif_msg_ifup(db))dev_dbg(db->dev, "enabling %s\n", dev->name);/* If there is no IRQ type specified, default to something that* may work, and tell the user that this is a problem */if (irqflags == IRQF_TRIGGER_NONE)dev_warn(db->dev, "WARNING: no IRQ resource flags set.\n");irqflags |= IRQF_SHARED;//申请中断if (request_irq(dev->irq, &dm9000_interrupt, irqflags, dev->name, dev))return -EAGAIN;/* Initialize DM9000 board *///复位网卡芯片dm9000_reset(db);//初始化网卡(相关寄存器设置)dm9000_init_dm9000(dev);/* Init driver variable */db->dbug_cnt = 0;mii_check_media(&db->mii, netif_msg_link(db), 1);//打开发送队列netif_start_queue(dev);//调度发送队列开始工作dm9000_schedule_poll(db);return 0;}
4.数据发送
下面说一下DM9000A中的存储部分,DM9000A内部有一个4K Dword SRAM,其中3KB是作为发送,16KB作为接收,如下图所示。其中0x0000~0x0BFF是传说中的TX buffer(TX buffer中只能存放两个包),0x0C00~0x3FFF是RX buffer。因此在写内存 *** 作时,当IMR的第7位被设置,如果到达了地址的结尾比如到了3KB,则回卷到0。相似的方式,在读 *** 作中,当IMR的第7位被设置如果到达了地址的结尾比如16K,则回卷到0x0C00。
DM9000的TX RAM可以同时放两个包,可以第9行代码中看出如果大于TXRAM中的包大于2则返回,DM9000会先发送第一个包,然后再发第二个包。
static int
dm9000_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
unsigned long flags;
board_info_t *db = netdev_priv(dev);
dm9000_dbg(db, 3, "%s:\n", __func__);
//如果TX RAM中的包大于2个包则返回
if (db->tx_pkt_cnt > 1)
return 1;
spin_lock_irqsave(&db->lock, flags);
*MWCMD是Memory data write command with address increment Register(F8H)
*将要访问的TXRAM地址写入地址寄存器。
/* Move data to DM9000 TX RAM */
writeb(DM9000_MWCMD, db->io_addr);
//拷贝数据到TXRAM
(db->outblk)(db->io_data, skb->data, skb->len);
dev->stats.tx_bytes += skb->len;
db->tx_pkt_cnt++;//增加数据包计数,这个值会在发送完成中断时进行自减
如果是第一个包则直接发送
/* TX control: First packet immediately send, second packet queue */
if (db->tx_pkt_cnt == 1) {
/* Set TX length to DM9000 */
/*把数据的长度填到TXPLL(发送包长度低字节)和TXPLH(发送包长度高字节)中*/
iow(db, DM9000_TXPLL, skb->len);
iow(db, DM9000_TXPLH, skb->len >> 8);
/*置发送控制寄存器(TX Control Register)的发送请求位TXREQ(Auto clears after sending completely),这样就可以发送出去了*/
/*
*记下此时的时间,这里起一个时间戳的作用,之后的超时会用到。如果当前的系统时间超过设备的trans_start时间
*至少一个超时周期,网络层将最终调用驱动程序的tx_timeout。那个这个"一个超时周期"又是什么呢?这个是我们在
*probe函数中设置的,ndev->watchdog_timeo = msecs_to_jiffies(watchdog);
*/
dev->trans_start = jiffies; /* save the time stamp */
} else {
//如果是第二个包,则暂时不发送,等待第一个包发送完成时tx_pkt_cnt减为1的时候再发送。
/* Second packet */
db->queue_pkt_len = skb->len;
netif_stop_queue(dev);//停止发送队列
}
spin_unlock_irqrestore(&db->lock, flags);
/* free this SKB */
dev_kfree_skb(skb);
return 0;
}
static intdm9000_start_xmit(struct sk_buff *skb, struct net_device *dev){unsigned long flags;board_info_t *db = netdev_priv(dev);dm9000_dbg(db, 3, "%s:\n", __func__);//如果TX RAM中的包大于2个包则返回if (db->tx_pkt_cnt > 1)return 1;spin_lock_irqsave(&db->lock, flags);*MWCMD是Memory data write command with address increment Register(F8H)*将要访问的TXRAM地址写入地址寄存器。/* Move data to DM9000 TX RAM */writeb(DM9000_MWCMD, db->io_addr);//拷贝数据到TXRAM(db->outblk)(db->io_data, skb->data, skb->len);dev->stats.tx_bytes += skb->len;db->tx_pkt_cnt++;//增加数据包计数,这个值会在发送完成中断时进行自减如果是第一个包则直接发送/* TX control: First packet immediately send, second packet queue */if (db->tx_pkt_cnt == 1) {/* Set TX length to DM9000 *//*把数据的长度填到TXPLL(发送包长度低字节)和TXPLH(发送包长度高字节)中*/iow(db, DM9000_TXPLL, skb->len);iow(db, DM9000_TXPLH, skb->len >> 8);/*置发送控制寄存器(TX Control Register)的发送请求位TXREQ(Auto clears after sending completely),这样就可以发送出去了*//**记下此时的时间,这里起一个时间戳的作用,之后的超时会用到。如果当前的系统时间超过设备的trans_start时间*至少一个超时周期,网络层将最终调用驱动程序的tx_timeout。那个这个"一个超时周期"又是什么呢?这个是我们在*probe函数中设置的,ndev->watchdog_timeo = msecs_to_jiffies(watchdog);*/dev->trans_start = jiffies;/* save the time stamp */} else {//如果是第二个包,则暂时不发送,等待第一个包发送完成时tx_pkt_cnt减为1的时候再发送。/* Second packet */db->queue_pkt_len = skb->len;netif_stop_queue(dev);//停止发送队列}spin_unlock_irqrestore(&db->lock, flags);/* free this SKB */dev_kfree_skb(skb);return 0;}
4.中断
static irqreturn_t dm9000_interrupt(intirq, void *dev_id)
{
structnet_device *dev = dev_id;
board_info_t*db = netdev_priv(dev);
intint_status;
unsignedlong flags;
u8reg_save;
dm9000_dbg(db,3, "entering %s\n", __func__);
/*A real interrupt coming */
//禁止所用中断
/*holders of db->lock must always block IRQs */
spin_lock_irqsave(&db->lock,flags);
//保存寄存器地址
/*Save previous register address */
reg_save= readb(db->io_addr);
//禁止DM9000的所有中断
/*Disable all interrupts */
iow(db,DM9000_IMR, IMR_PAR);
/*Got DM9000 interrupt status */
//获取中断状态寄存器的值
int_status= ior(db, DM9000_ISR); /* Got ISR */
iow(db,DM9000_ISR, int_status); /* Clear ISRstatus */
if(netif_msg_intr(db))
dev_dbg(db->dev,"interrupt status %02x\n", int_status);
/*Received the coming packet */
//如果是读取中断,则开始读取
if(int_status & ISR_PRS)
dm9000_rx(dev);
/*Trnasmit Interrupt check */
//是发送完成中断则处理发送完成后的事情
if(int_status & ISR_PTS)
dm9000_tx_done(dev,db);
if(db->type != TYPE_DM9000E) {
if(int_status & ISR_LNKCHNG) {
/*fire a link-change request */
schedule_delayed_work(&db->phy_poll,1);
}
}
/*Re-enable interrupt mask */
//重新打开DM9000的内部中断
iow(db,DM9000_IMR, db->imr_all);
/*Restore previous register address */
//恢复寄存器的值
writeb(reg_save,db->io_addr);
//重新允许所有中断
spin_unlock_irqrestore(&db->lock,flags);
returnIRQ_HANDLED;
}
static irqreturn_t dm9000_interrupt(intirq, void *dev_id){structnet_device *dev = dev_id;board_info_t*db = netdev_priv(dev);intint_status;unsignedlong flags;u8reg_save;dm9000_dbg(db,3, "entering %s\n", __func__);/*A real interrupt coming *///禁止所用中断/*holders of db->lock must always block IRQs */spin_lock_irqsave(&db->lock,flags);//保存寄存器地址/*Save previous register address */reg_save= readb(db->io_addr);//禁止DM9000的所有中断/*Disable all interrupts */iow(db,DM9000_IMR, IMR_PAR);/*Got DM9000 interrupt status *///获取中断状态寄存器的值int_status= ior(db, DM9000_ISR); /* Got ISR */iow(db,DM9000_ISR, int_status); /* Clear ISRstatus */if(netif_msg_intr(db))dev_dbg(db->dev,"interrupt status %02x\n", int_status);/*Received the coming packet *///如果是读取中断,则开始读取if(int_status & ISR_PRS)dm9000_rx(dev);/*Trnasmit Interrupt check *///是发送完成中断则处理发送完成后的事情if(int_status & ISR_PTS)dm9000_tx_done(dev,db);if(db->type != TYPE_DM9000E) {if(int_status & ISR_LNKCHNG) {/*fire a link-change request */schedule_delayed_work(&db->phy_poll,1);}}/*Re-enable interrupt mask *///重新打开DM9000的内部中断iow(db,DM9000_IMR, db->imr_all);/*Restore previous register address *///恢复寄存器的值writeb(reg_save,db->io_addr);//重新允许所有中断spin_unlock_irqrestore(&db->lock,flags);returnIRQ_HANDLED;}
5.接收数据
static void
dm9000_rx(struct net_device *dev)
{
board_info_t *db = netdev_priv(dev);
struct dm9000_rxhdr rxhdr;
struct sk_buff *skb;
u8 rxbyte, *rdptr;
bool GoodPacket;
int RxLen;
/* Check packet ready or not */
do {
ior(db, DM9000_MRCMDX); /* Dummy read */
//获取接收数据的长度
/* Get most updated data */
rxbyte = readb(db->io_data);
//检查设备接收状态
/* Status check: this byte must be 0 or 1 */
if (rxbyte > DM9000_PKT_RDY) {
dev_warn(db->dev, "status check fail: %d\n", rxbyte);
iow(db, DM9000_RCR, 0x00); /* Stop Device */
iow(db, DM9000_ISR, IMR_PAR); /* Stop INT request */
return;
}
if (rxbyte != DM9000_PKT_RDY)
return;
/* A packet ready now & Get status/length */
GoodPacket = true;
writeb(DM9000_MRCMD, db->io_addr);
(db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));
RxLen = le16_to_cpu(rxhdr.RxLen);
if (netif_msg_rx_status(db))
dev_dbg(db->dev, "RX: status %02x, length %04x\n",
rxhdr.RxStatus, RxLen);
/* Packet Status check */
if (RxLen < 0x40) {
GoodPacket = false;
if (netif_msg_rx_err(db))
dev_dbg(db->dev, "RX: Bad Packet (runt)\n");
}
if (RxLen > DM9000_PKT_MAX) {
dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);
}
/* rxhdr.RxStatus is identical to RSR register. */
if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |
RSR_PLE | RSR_RWTO |
RSR_LCS | RSR_RF)) {
GoodPacket = false;
if (rxhdr.RxStatus & RSR_FOE) {
if (netif_msg_rx_err(db))
dev_dbg(db->dev, "fifo error\n");
dev->stats.rx_fifo_errors++;
}
if (rxhdr.RxStatus & RSR_CE) {
if (netif_msg_rx_err(db))
dev_dbg(db->dev, "crc error\n");
dev->stats.rx_crc_errors++;
}
if (rxhdr.RxStatus & RSR_RF) {
if (netif_msg_rx_err(db))
dev_dbg(db->dev, "length error\n");
dev->stats.rx_length_errors++;
}
}
/* Move data from DM9000 */
//如果接收正确,开始接收
if (GoodPacket
&& ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {
skb_reserve(skb, 2);
rdptr = (u8 *) skb_put(skb, RxLen - 4);//获取skb的数据指针
/* Read received packet from RX SRAM */
(db->inblk)(db->io_data, rdptr, RxLen);//读取数据
dev->stats.rx_bytes += RxLen;
/* Pass to upper layer */
skb->protocol = eth_type_trans(skb, dev);
netif_rx(skb);//将接收到的skb交给协议层
dev->stats.rx_packets++;
} else {
/* need to dump the packet's data */
(db->dumpblk)(db->io_data, RxLen);
}
} while (rxbyte == DM9000_PKT_RDY);
}
static voiddm9000_rx(struct net_device *dev){board_info_t *db = netdev_priv(dev);struct dm9000_rxhdr rxhdr;struct sk_buff *skb;u8 rxbyte, *rdptr;bool GoodPacket;int RxLen;/* Check packet ready or not */do {ior(db, DM9000_MRCMDX);/* Dummy read *///获取接收数据的长度/* Get most updated data */rxbyte = readb(db->io_data);//检查设备接收状态/* Status check: this byte must be 0 or 1 */if (rxbyte > DM9000_PKT_RDY) {dev_warn(db->dev, "status check fail: %d\n", rxbyte);iow(db, DM9000_RCR, 0x00);/* Stop Device */iow(db, DM9000_ISR, IMR_PAR);/* Stop INT request */return;}if (rxbyte != DM9000_PKT_RDY)return;/* A packet ready now & Get status/length */GoodPacket = true;writeb(DM9000_MRCMD, db->io_addr);(db->inblk)(db->io_data, &rxhdr, sizeof(rxhdr));RxLen = le16_to_cpu(rxhdr.RxLen);if (netif_msg_rx_status(db))dev_dbg(db->dev, "RX: status %02x, length %04x\n",rxhdr.RxStatus, RxLen);/* Packet Status check */if (RxLen < 0x40) {GoodPacket = false;if (netif_msg_rx_err(db))dev_dbg(db->dev, "RX: Bad Packet (runt)\n");}if (RxLen > DM9000_PKT_MAX) {dev_dbg(db->dev, "RST: RX Len:%x\n", RxLen);}/* rxhdr.RxStatus is identical to RSR register. */if (rxhdr.RxStatus & (RSR_FOE | RSR_CE | RSR_AE |RSR_PLE | RSR_RWTO |RSR_LCS | RSR_RF)) {GoodPacket = false;if (rxhdr.RxStatus & RSR_FOE) {if (netif_msg_rx_err(db))dev_dbg(db->dev, "fifo error\n");dev->stats.rx_fifo_errors++;}if (rxhdr.RxStatus & RSR_CE) {if (netif_msg_rx_err(db))dev_dbg(db->dev, "crc error\n");dev->stats.rx_crc_errors++;}if (rxhdr.RxStatus & RSR_RF) {if (netif_msg_rx_err(db))dev_dbg(db->dev, "length error\n");dev->stats.rx_length_errors++;}}/* Move data from DM9000 *///如果接收正确,开始接收if (GoodPacket&& ((skb = dev_alloc_skb(RxLen + 4)) != NULL)) {skb_reserve(skb, 2);rdptr = (u8 *) skb_put(skb, RxLen - 4);//获取skb的数据指针/* Read received packet from RX SRAM */(db->inblk)(db->io_data, rdptr, RxLen);//读取数据dev->stats.rx_bytes += RxLen;/* Pass to upper layer */skb->protocol = eth_type_trans(skb, dev);netif_rx(skb);//将接收到的skb交给协议层dev->stats.rx_packets++;} else {/* need to dump the packet's data */(db->dumpblk)(db->io_data, RxLen);}} while (rxbyte == DM9000_PKT_RDY);}
6.发送完成
static void dm9000_tx_done(struct net_device *dev, board_info_t *db)
{
int tx_status = ior(db, DM9000_NSR); /* Got TX status */
if (tx_status & (NSR_TX2END | NSR_TX1END)) {
/* One packet sent complete */
//将数据包计数减1
db->tx_pkt_cnt--;
dev->stats.tx_packets++;
if (netif_msg_tx_done(db))
dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);
/* Queue packet check & send */
//如果数据包数量依然大于0,说明是TX RAM中的第二个包,再次启动发送,将TX RAM中第二个包发送出去
if (db->tx_pkt_cnt > 0) {
/*把数据的长度填到TXPLL(发送包长度低字节)和TXPLH(发送包长度高字节)中*/
iow(db, DM9000_TXPLL, skb->len);
iow(db, DM9000_TXPLH, skb->len >> 8);
/*置发送控制寄存器(TX Control Register)的发送请求位TXREQ(Auto clears after sending completely),这样就可以发送出去了*/
dev->trans_start = jiffies;
}
netif_wake_queue(dev);//唤醒发送队列
}
}
static void dm9000_tx_done(struct net_device *dev, board_info_t *db){int tx_status = ior(db, DM9000_NSR);/* Got TX status */if (tx_status & (NSR_TX2END | NSR_TX1END)) {/* One packet sent complete *///将数据包计数减1db->tx_pkt_cnt--;dev->stats.tx_packets++;if (netif_msg_tx_done(db))dev_dbg(db->dev, "tx done, NSR %02x\n", tx_status);/* Queue packet check & send *///如果数据包数量依然大于0,说明是TX RAM中的第二个包,再次启动发送,将TX RAM中第二个包发送出去if (db->tx_pkt_cnt > 0) {/*把数据的长度填到TXPLL(发送包长度低字节)和TXPLH(发送包长度高字节)中*/iow(db, DM9000_TXPLL, skb->len);iow(db, DM9000_TXPLH, skb->len >> 8);/*置发送控制寄存器(TX Control Register)的发送请求位TXREQ(Auto clears after sending completely),这样就可以发送出去了*/dev->trans_start = jiffies;}netif_wake_queue(dev);//唤醒发送队列}}
7.超时处理
static void dm9000_timeout(struct net_device *dev)
{
board_info_t *db = netdev_priv(dev);
u8 reg_save;
unsigned long flags;
/* Save previous register address */
reg_save = readb(db->io_addr);
spin_lock_irqsave(&db->lock, flags);
//停止发送队列并复位DM9000网卡
netif_stop_queue(dev);
dm9000_reset(db);
dm9000_init_dm9000(dev);
/* We can accept TX packets again */
//重新发送
dev->trans_start = jiffies;
netif_wake_queue(dev);
/* Restore previous register address */
writeb(reg_save, db->io_addr);
spin_unlock_irqrestore(&db->lock, flags);
}
static void dm9000_timeout(struct net_device *dev){board_info_t *db = netdev_priv(dev);u8 reg_save;unsigned long flags;/* Save previous register address */reg_save = readb(db->io_addr);spin_lock_irqsave(&db->lock, flags);//停止发送队列并复位DM9000网卡netif_stop_queue(dev);dm9000_reset(db);dm9000_init_dm9000(dev);/* We can accept TX packets again *///重新发送dev->trans_start = jiffies;netif_wake_queue(dev);/* Restore previous register address */writeb(reg_save, db->io_addr);spin_unlock_irqrestore(&db->lock, flags);}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)