📄 natsemi.c
字号:
np->full_duplex = duplex; if (np->msg_level & NETIF_MSG_LINK) printk(KERN_INFO "%s: Setting %s-duplex based on negotiated link" " capability.\n", dev->name, duplex ? "full" : "half"); if (duplex) { np->rx_config |= 0x10000000; np->tx_config |= 0xC0000000; } else { np->rx_config &= ~0x10000000; np->tx_config &= ~0xC0000000; } writel(np->tx_config, ioaddr + TxConfig); writel(np->rx_config, ioaddr + RxConfig); }}static void netdev_timer(unsigned long data){ struct net_device *dev = (struct net_device *)data; struct netdev_private *np = (struct netdev_private *)dev->priv; long ioaddr = dev->base_addr; int next_tick = 10*HZ; if (np->msg_level & NETIF_MSG_TIMER) printk(KERN_DEBUG "%s: Driver monitor timer tick, status %8.8x.\n", dev->name, (int)readl(ioaddr + IntrStatus)); if (np->rx_q_empty) { /* Trigger an interrupt to refill. */ writel(SoftIntr, ioaddr + ChipCmd); } /* This will either have a small false-trigger window or will not catch tbusy incorrectly set when the queue is empty. */ if (netif_queue_paused(dev) && np->cur_tx - np->dirty_tx > 1 && (jiffies - dev->trans_start) > TX_TIMEOUT) { tx_timeout(dev); } check_duplex(dev); np->timer.expires = jiffies + next_tick; add_timer(&np->timer);}static void tx_timeout(struct net_device *dev){ struct netdev_private *np = (struct netdev_private *)dev->priv; long ioaddr = dev->base_addr; printk(KERN_WARNING "%s: Transmit timed out, status %8.8x," " resetting...\n", dev->name, (int)readl(ioaddr + TxRingPtr)); if (np->msg_level & NETIF_MSG_TX_ERR) { int i; printk(KERN_DEBUG " Rx ring %p: ", np->rx_ring); for (i = 0; i < RX_RING_SIZE; i++) printk(" %8.8x", (unsigned int)np->rx_ring[i].cmd_status); printk("\n"KERN_DEBUG" Tx ring %p: ", np->tx_ring); for (i = 0; i < TX_RING_SIZE; i++) printk(" %4.4x", np->tx_ring[i].cmd_status); printk("\n"); } /* Reinitialize the hardware here. */ /* Stop and restart the chip's Tx processes . */ /* Trigger an immediate transmit demand. */ dev->trans_start = jiffies; np->stats.tx_errors++; return;}/* Refill the Rx ring buffers, returning non-zero if not full. */static int rx_ring_fill(struct net_device *dev){ struct netdev_private *np = (struct netdev_private *)dev->priv; unsigned int entry; for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) { entry = np->dirty_rx % RX_RING_SIZE; if (np->rx_skbuff[entry] == NULL) { struct sk_buff *skb = dev_alloc_skb(np->rx_buf_sz); np->rx_skbuff[entry] = skb; if (skb == NULL) return 1; /* Better luck next time. */ skb->dev = dev; /* Mark as being used by this device. */ np->rx_ring[entry].buf_addr = virt_to_le32desc(skb->tail); } np->rx_ring[entry].cmd_status = cpu_to_le32(DescIntr | np->rx_buf_sz); } return 0;}/* Initialize the Rx and Tx rings, along with various 'dev' bits. */static void init_ring(struct net_device *dev){ struct netdev_private *np = (struct netdev_private *)dev->priv; int i; np->tx_full = 0; np->cur_rx = np->cur_tx = 0; np->dirty_rx = np->dirty_tx = 0; /* MAX(PKT_BUF_SZ, dev->mtu + 8); */ /* I know you _want_ to change this without understanding it. Don't. */ np->rx_buf_sz = (dev->mtu <= 1532 ? PKT_BUF_SZ : dev->mtu + 8); np->rx_head_desc = &np->rx_ring[0]; /* Initialize all Rx descriptors. */ for (i = 0; i < RX_RING_SIZE; i++) { np->rx_ring[i].next_desc = virt_to_le32desc(&np->rx_ring[i+1]); np->rx_ring[i].cmd_status = cpu_to_le32(DescOwn); np->rx_skbuff[i] = 0; } /* Mark the last entry as wrapping the ring. */ np->rx_ring[i-1].next_desc = virt_to_le32desc(&np->rx_ring[0]); for (i = 0; i < TX_RING_SIZE; i++) { np->tx_skbuff[i] = 0; np->tx_ring[i].next_desc = virt_to_le32desc(&np->tx_ring[i+1]); np->tx_ring[i].cmd_status = 0; } np->tx_ring[i-1].next_desc = virt_to_le32desc(&np->tx_ring[0]); /* Fill in the Rx buffers. Allocation failure just leaves a "negative" np->dirty_rx. */ np->dirty_rx = (unsigned int)(0 - RX_RING_SIZE); rx_ring_fill(dev); return;}static int start_tx(struct sk_buff *skb, struct net_device *dev){ struct netdev_private *np = (struct netdev_private *)dev->priv; unsigned int entry; /* Block a timer-based transmit from overlapping. This happens when packets are presumed lost, and we use this check the Tx status. */ if (netif_pause_tx_queue(dev) != 0) { /* This watchdog code is redundant with the media monitor timer. */ if (jiffies - dev->trans_start > TX_TIMEOUT) tx_timeout(dev); return 1; } /* Note: Ordering is important here, set the field with the "ownership" bit last, and only then increment cur_tx. No spinlock is needed for either Tx or Rx. */ /* Calculate the next Tx descriptor entry. */ entry = np->cur_tx % TX_RING_SIZE; np->tx_skbuff[entry] = skb; np->tx_ring[entry].buf_addr = virt_to_le32desc(skb->data); np->tx_ring[entry].cmd_status = cpu_to_le32(DescOwn|DescIntr | skb->len); np->cur_tx++; /* For some architectures explicitly flushing np->tx_ring,sizeof(tx_ring) and skb->data,skb->len improves performance. */ if (np->cur_tx - np->dirty_tx >= TX_QUEUE_LEN - 1) { np->tx_full = 1; /* Check for a just-cleared queue. */ if (np->cur_tx - (volatile unsigned int)np->dirty_tx < TX_QUEUE_LEN - 4) { np->tx_full = 0; netif_unpause_tx_queue(dev); } else netif_stop_tx_queue(dev); } else netif_unpause_tx_queue(dev); /* Typical path */ /* Wake the potentially-idle transmit channel. */ writel(TxOn, dev->base_addr + ChipCmd); dev->trans_start = jiffies; if (np->msg_level & NETIF_MSG_TX_QUEUED) { printk(KERN_DEBUG "%s: Transmit frame #%d queued in slot %d.\n", dev->name, np->cur_tx, entry); } return 0;}/* The interrupt handler does all of the Rx thread work and cleans up after the Tx thread. */static void intr_handler(int irq, void *dev_instance, struct pt_regs *rgs){ struct net_device *dev = (struct net_device *)dev_instance; struct netdev_private *np; long ioaddr; int boguscnt;#ifndef final_version /* Can never occur. */ if (dev == NULL) { printk (KERN_ERR "Netdev interrupt handler(): IRQ %d for unknown " "device.\n", irq); return; }#endif ioaddr = dev->base_addr; np = (struct netdev_private *)dev->priv; boguscnt = np->max_interrupt_work; do { u32 intr_status = readl(ioaddr + IntrStatus); if (intr_status == 0 || intr_status == 0xffffffff) break; /* Acknowledge all of the current interrupt sources ASAP. Nominally the read above accomplishes this, but... */ writel(intr_status & 0x001ffff, ioaddr + IntrStatus); if (np->msg_level & NETIF_MSG_INTR) printk(KERN_DEBUG "%s: Interrupt, status %8.8x.\n", dev->name, intr_status); if (intr_status & (IntrRxDone | IntrRxIntr)) { netdev_rx(dev); np->rx_q_empty = rx_ring_fill(dev); } if (intr_status & (IntrRxIdle | IntrDrv)) { unsigned int old_dirty_rx = np->dirty_rx; if (rx_ring_fill(dev) == 0) np->rx_q_empty = 0; /* Restart Rx engine iff we did add a buffer. */ if (np->dirty_rx != old_dirty_rx) writel(RxOn, dev->base_addr + ChipCmd); } for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) { int entry = np->dirty_tx % TX_RING_SIZE; int tx_status = le32_to_cpu(np->tx_ring[entry].cmd_status); if (tx_status & DescOwn) break; if (np->msg_level & NETIF_MSG_TX_DONE) printk(KERN_DEBUG "%s: Transmit done, Tx status %8.8x.\n", dev->name, tx_status); if (tx_status & 0x08000000) { np->stats.tx_packets++;#if LINUX_VERSION_CODE > 0x20127 np->stats.tx_bytes += np->tx_skbuff[entry]->len;#endif } else { /* Various Tx errors */ if (np->msg_level & NETIF_MSG_TX_ERR) printk(KERN_DEBUG "%s: Transmit error, Tx status %8.8x.\n", dev->name, tx_status); if (tx_status & 0x04010000) np->stats.tx_aborted_errors++; if (tx_status & 0x02000000) np->stats.tx_fifo_errors++; if (tx_status & 0x01000000) np->stats.tx_carrier_errors++; if (tx_status & 0x00200000) np->stats.tx_window_errors++; np->stats.tx_errors++; } /* Free the original skb. */ dev_free_skb_irq(np->tx_skbuff[entry]); np->tx_skbuff[entry] = 0; } /* Note the 4 slot hysteresis to mark the queue non-full. */ if (np->tx_full && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) { /* The ring is no longer full, allow new TX entries. */ np->tx_full = 0; netif_resume_tx_queue(dev); } /* Abnormal error summary/uncommon events handlers. */ if (intr_status & IntrAbnormalSummary) netdev_error(dev, intr_status); if (--boguscnt < 0) { printk(KERN_WARNING "%s: Too much work at interrupt, " "status=0x%4.4x.\n", dev->name, intr_status); np->restore_intr_enable = 1; break; } } while (1); if (np->msg_level & NETIF_MSG_INTR) printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n", dev->name, (int)readl(ioaddr + IntrStatus)); return;}/* This routine is logically part of the interrupt handler, but separated for clarity and better register allocation. */static int netdev_rx(struct net_device *dev){ struct netdev_private *np = (struct netdev_private *)dev->priv; int entry = np->cur_rx % RX_RING_SIZE; int boguscnt = np->dirty_rx + RX_RING_SIZE - np->cur_rx; s32 desc_status = le32_to_cpu(np->rx_head_desc->cmd_status); /* If the driver owns the next entry it's a new packet. Send it up. */ while (desc_status < 0) { /* e.g. & DescOwn */ if (np->msg_level & NETIF_MSG_RX_STATUS) printk(KERN_DEBUG " In netdev_rx() entry %d status was %8.8x.\n", entry, desc_status); if (--boguscnt < 0) break; if ((desc_status & (DescMore|DescPktOK|RxTooLong)) != DescPktOK) { if (desc_status & DescMore) { printk(KERN_WARNING "%s: Oversized(?) Ethernet frame spanned " "multiple buffers, entry %#x status %x.\n", dev->name, np->cur_rx, desc_status); np->stats.rx_length_errors++; } else { /* There was a error. */ if (np->msg_level & NETIF_MSG_RX_ERR) printk(KERN_DEBUG " netdev_rx() Rx error was %8.8x.\n", desc_status); np->stats.rx_errors++; if (desc_status & 0x06000000) np->stats.rx_over_errors++; if (desc_status & 0x00600000) np->stats.rx_length_errors++; if (desc_status & 0x00140000) np->stats.rx_frame_errors++; if (desc_status & 0x00080000) np->stats.rx_crc_errors++; } } else { struct sk_buff *skb; int pkt_len = (desc_status & 0x0fff) - 4; /* Omit CRC size. */ /* Check if the packet is long enough to accept without copying to a minimally-sized skbuff. */ if (pkt_len < np->rx_copybreak && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) { skb->dev = dev; skb_reserve(skb, 2); /* 16 byte align the IP header */#if defined(HAS_IP_COPYSUM) || (LINUX_VERSION_CODE >= 0x20100) eth_copy_and_sum(skb, np->rx_skbuff[entry]->tail, pkt_len, 0); skb_put(skb, pkt_len);#else memcpy(skb_put(skb, pkt_len), np->rx_skbuff[entry]->tail, pkt_len);#endif } else { skb_put(skb = np->rx_skbuff[entry], pkt_len); np->rx_skbuff[entry] = NULL; } skb->protocol = eth_type_trans(skb, dev); /* W/ hardware checksum: skb->ip_summed = CHECKSUM_UNNECESSARY; */ netif_rx(skb); dev->last_rx = jiffies; np->stats.rx_packets++;#if LINUX_VERSION_CODE > 0x20127 np->stats.rx_bytes += pkt_len;#endif } entry = (++np->cur_rx) % RX_RING_SIZE; np->rx_head_desc = &np->rx_ring[entry]; desc_status = le32_to_cpu(np->rx_head_desc->cmd_status); } /* Refill is now done in the main interrupt loop. */ return 0;}static void netdev_error(struct net_device *dev, int intr_status){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -