📄 natsemi.c
字号:
if (intr_status == 0) break; if (intr_status & (IntrRxDone | IntrRxErr | IntrRxIdle | IntrRxOverrun)) netdev_rx(dev); for (; np->cur_tx - np->dirty_tx > 0; np->dirty_tx++) { int entry = np->dirty_tx % TX_RING_SIZE; if (np->tx_ring[entry].cmd_status & cpu_to_le32(DescOwn)) break; if (np->tx_ring[entry].cmd_status & cpu_to_le32(0x08000000)) { np->stats.tx_packets++;#if LINUX_VERSION_CODE > 0x20127 np->stats.tx_bytes += np->tx_skbuff[entry]->len;#endif } else { /* Various Tx errors */ int tx_status = le32_to_cpu(np->tx_ring[entry].cmd_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_kfree_skb_irq(np->tx_skbuff[entry]); np->tx_skbuff[entry] = 0; } if (np->tx_full && np->cur_tx - np->dirty_tx < TX_QUEUE_LEN - 4) { /* The ring is no longer full, wake queue. */ np->tx_full = 0; netif_wake_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); break; } } while (1); if (debug > 3) printk(KERN_DEBUG "%s: exiting interrupt, status=%#4.4x.\n", dev->name, (int)readl(ioaddr + IntrStatus));#ifndef final_version /* Code that should never be run! Perhaps remove after testing.. */ { static int stopit = 10; if (!netif_running(dev) && --stopit < 0) { printk(KERN_ERR "%s: Emergency stop, looping startup interrupt.\n", dev->name); free_irq(irq, dev); } }#endif spin_unlock(&np->lock);}/* 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 (debug > 4) 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 (debug > 2) 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 < rx_copybreak && (skb = dev_alloc_skb(pkt_len + 2)) != NULL) { skb->dev = dev; skb_reserve(skb, 2); /* 16 byte align the IP header */#if HAS_IP_COPYSUM 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 { char *temp = skb_put(skb = np->rx_skbuff[entry], pkt_len); np->rx_skbuff[entry] = NULL;#ifndef final_version /* Remove after testing. */ if (le32desc_to_virt(np->rx_ring[entry].addr) != temp) printk(KERN_ERR "%s: Internal fault: The skbuff addresses " "do not match in netdev_rx: %p vs. %p / %p.\n", dev->name, le32desc_to_virt(np->rx_ring[entry].addr), skb->head, temp);#endif }#ifndef final_version /* Remove after testing. */ /* You will want this info for the initial debug. */ if (debug > 5) printk(KERN_DEBUG " Rx data %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:" "%2.2x %2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x %2.2x%2.2x " "%d.%d.%d.%d.\n", skb->data[0], skb->data[1], skb->data[2], skb->data[3], skb->data[4], skb->data[5], skb->data[6], skb->data[7], skb->data[8], skb->data[9], skb->data[10], skb->data[11], skb->data[12], skb->data[13], skb->data[14], skb->data[15], skb->data[16], skb->data[17]);#endif 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 the Rx ring buffers. */ for (; np->cur_rx - np->dirty_rx > 0; np->dirty_rx++) { struct sk_buff *skb; entry = np->dirty_rx % RX_RING_SIZE; if (np->rx_skbuff[entry] == NULL) { skb = dev_alloc_skb(np->rx_buf_sz); np->rx_skbuff[entry] = skb; if (skb == NULL) break; /* Better luck next round. */ skb->dev = dev; /* Mark as being used by this device. */ np->rx_ring[entry].addr = virt_to_le32desc(skb->tail); } np->rx_ring[entry].cmd_status = cpu_to_le32(np->rx_buf_sz); } /* Restart Rx engine if stopped. */ writel(RxOn, dev->base_addr + ChipCmd); return 0;}static void netdev_error(struct net_device *dev, int intr_status){ struct netdev_private *np = (struct netdev_private *)dev->priv; long ioaddr = dev->base_addr; if (intr_status & LinkChange) { printk(KERN_NOTICE "%s: Link changed: Autonegotiation advertising" " %4.4x partner %4.4x.\n", dev->name, (int)readl(ioaddr + 0x90), (int)readl(ioaddr + 0x94)); check_duplex(dev); } if (intr_status & StatsMax) { get_stats(dev); } if ((intr_status & ~(LinkChange|StatsMax|RxResetDone|TxResetDone|0x83ff)) && debug) printk(KERN_ERR "%s: Something Wicked happened! %4.4x.\n", dev->name, intr_status); /* Hmmmmm, it's not clear how to recover from PCI faults. */ if (intr_status & IntrPCIErr) { np->stats.tx_fifo_errors++; np->stats.rx_fifo_errors++; }}static struct net_device_stats *get_stats(struct net_device *dev){ long ioaddr = dev->base_addr; struct netdev_private *np = (struct netdev_private *)dev->priv; /* We should lock this segment of code for SMP eventually, although the vulnerability window is very small and statistics are non-critical. */ /* The chip only need report frame silently dropped. */ np->stats.rx_crc_errors += readl(ioaddr + RxCRCErrs); np->stats.rx_missed_errors += readl(ioaddr + RxMissed); return &np->stats;}/* The little-endian AUTODIN II ethernet CRC calculations. A big-endian version is also available. This is slow but compact code. Do not use this routine for bulk data, use a table-based routine instead. This is common code and should be moved to net/core/crc.c. Chips may use the upper or lower CRC bits, and may reverse and/or invert them. Select the endian-ness that results in minimal calculations.*/static unsigned const ethernet_polynomial_le = 0xedb88320U;static inline unsigned ether_crc_le(int length, unsigned char *data){ unsigned int crc = 0xffffffff; /* Initial value. */ while(--length >= 0) { unsigned char current_octet = *data++; int bit; for (bit = 8; --bit >= 0; current_octet >>= 1) { if ((crc ^ current_octet) & 1) { crc >>= 1; crc ^= ethernet_polynomial_le; } else crc >>= 1; } } return crc;}static void set_rx_mode(struct net_device *dev){ long ioaddr = dev->base_addr; struct netdev_private *np = (struct netdev_private *)dev->priv; u16 mc_filter[32]; /* Multicast hash filter */ u32 rx_mode; if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */ /* Unconditionally log net taps. */ printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n", dev->name); rx_mode = AcceptBroadcast | AcceptAllMulticast | AcceptAllPhys | AcceptMyPhys; } else if ((dev->mc_count > multicast_filter_limit) || (dev->flags & IFF_ALLMULTI)) { rx_mode = AcceptBroadcast | AcceptAllMulticast | AcceptMyPhys; } else { struct dev_mc_list *mclist; int i; memset(mc_filter, 0, sizeof(mc_filter)); for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count; i++, mclist = mclist->next) { set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff, mc_filter); } for (i = 0; i < 32; i++) { writew(0x200 + (i<<1), ioaddr + RxFilterAddr); writew(cpu_to_be16(mc_filter[i]), ioaddr + RxFilterData); } rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys; } writel(rx_mode | EnableFilter, ioaddr + RxFilterAddr); np->cur_rx_mode = rx_mode;}static int mii_ioctl(struct net_device *dev, struct ifreq *rq, int cmd){ u16 *data = (u16 *)&rq->ifr_data; switch(cmd) { case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */ data[0] = 1; /* Fall Through */ case SIOCDEVPRIVATE+1: /* Read the specified MII register. */ data[3] = mdio_read(dev, data[0] & 0x1f, data[1] & 0x1f); return 0; case SIOCDEVPRIVATE+2: /* Write the specified MII register */ if (!capable(CAP_NET_ADMIN)) return -EPERM; mdio_write(dev, data[0] & 0x1f, data[1] & 0x1f, data[2]); return 0; default: return -EOPNOTSUPP; }}static int netdev_close(struct net_device *dev){ long ioaddr = dev->base_addr; struct netdev_private *np = (struct netdev_private *)dev->priv; int i; netif_stop_queue(dev); if (debug > 1) { printk(KERN_DEBUG "%s: Shutting down ethercard, status was %4.4x " "Int %2.2x.\n", dev->name, (int)readl(ioaddr + ChipCmd), (int)readl(ioaddr + IntrStatus)); printk(KERN_DEBUG "%s: Queue pointers were Tx %d / %d, Rx %d / %d.\n", dev->name, np->cur_tx, np->dirty_tx, np->cur_rx, np->dirty_rx); } /* Disable interrupts using the mask. */ writel(0, ioaddr + IntrMask); writel(0, ioaddr + IntrEnable); writel(2, ioaddr + StatsCtrl); /* Freeze Stats */ /* Stop the chip's Tx and Rx processes. */ writel(RxOff | TxOff, ioaddr + ChipCmd); del_timer_sync(&np->timer);#ifdef __i386__ if (debug > 2) { printk("\n"KERN_DEBUG" Tx ring at %8.8x:\n", (int)virt_to_bus(np->tx_ring)); for (i = 0; i < TX_RING_SIZE; i++) printk(" #%d desc. %8.8x %8.8x.\n", i, np->tx_ring[i].cmd_status, np->tx_ring[i].addr); printk("\n"KERN_DEBUG " Rx ring %8.8x:\n", (int)virt_to_bus(np->rx_ring)); for (i = 0; i < RX_RING_SIZE; i++) { printk(KERN_DEBUG " #%d desc. %8.8x %8.8x\n", i, np->rx_ring[i].cmd_status, np->rx_ring[i].addr); } }#endif /* __i386__ debugging only */ free_irq(dev->irq, dev); /* Free all the skbuffs in the Rx queue. */ for (i = 0; i < RX_RING_SIZE; i++) { np->rx_ring[i].cmd_status = 0; np->rx_ring[i].addr = 0xBADF00D0; /* An invalid address. */ if (np->rx_skbuff[i]) {#if LINUX_VERSION_CODE < 0x20100 np->rx_skbuff[i]->free = 1;#endif dev_kfree_skb(np->rx_skbuff[i]); } np->rx_skbuff[i] = 0; } for (i = 0; i < TX_RING_SIZE; i++) { if (np->tx_skbuff[i]) dev_kfree_skb(np->tx_skbuff[i]); np->tx_skbuff[i] = 0; } /* Restore PME enable bit */ writel(np->SavedClkRun, ioaddr + ClkRun);#if 0 writel(0x0200, ioaddr + ChipConfig); /* Power down Xcvr. */#endif return 0;}static void __devexit natsemi_remove1 (struct pci_dev *pdev){ struct net_device *dev = pdev->driver_data; struct netdev_private *np = (struct netdev_private *)dev->priv; const int pcibar = 1; /* PCI base address register */ unregister_netdev (dev); release_mem_region(pci_resource_start(pdev, pcibar), np->iosize); iounmap ((char *) dev->base_addr); kfree (dev);}static struct pci_driver natsemi_driver = { name: "natsemi", id_table: natsemi_pci_tbl, probe: natsemi_probe1, remove: natsemi_remove1,};static int __init natsemi_init_mod (void){ if (debug > 1) printk(KERN_INFO "%s" KERN_INFO "%s" KERN_INFO "%s", version1, version2, version3); return pci_module_init (&natsemi_driver);}static void __exit natsemi_exit_mod (void){ pci_unregister_driver (&natsemi_driver);}module_init(natsemi_init_mod);module_exit(natsemi_exit_mod);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -