e1000_main.c
来自「是关于linux2.5.1的完全源码」· C语言 代码 · 共 2,106 行 · 第 1/4 页
C
2,106 行
/******************************************************************************* This software program is available to you under a choice of one of two licenses. You may choose to be licensed under either the GNU General Public License 2.0, June 1991, available at http://www.fsf.org/copyleft/gpl.html, or the Intel BSD + Patent License, the text of which follows: Recipient has requested a license and Intel Corporation ("Intel") is willing to grant a license for the software entitled Linux Base Driver for the Intel(R) PRO/1000 Family of Adapters (e1000) (the "Software") being provided by Intel Corporation. The following definitions apply to this license: "Licensed Patents" means patent claims licensable by Intel Corporation which are necessarily infringed by the use of sale of the Software alone or when combined with the operating system referred to below. "Recipient" means the party to whom Intel delivers this Software. "Licensee" means Recipient and those third parties that receive a license to any operating system available under the GNU General Public License 2.0 or later. Copyright (c) 1999 - 2002 Intel Corporation. All rights reserved. The license is provided to Recipient and Recipient's Licensees under the following terms. Redistribution and use in source and binary forms of the Software, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code of the Software may retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form of the Software may reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or materials provided with the distribution. Neither the name of Intel Corporation nor the names of its contributors shall be used to endorse or promote products derived from this Software without specific prior written permission. Intel hereby grants Recipient and Licensees a non-exclusive, worldwide, royalty-free patent license under Licensed Patents to make, use, sell, offer to sell, import and otherwise transfer the Software, if any, in source code and object code form. This license shall include changes to the Software that are error corrections or other minor changes to the Software that do not add functionality or features when the Software is incorporated in any version of an operating system that has been distributed under the GNU General Public License 2.0 or later. This patent license shall apply to the combination of the Software and any operating system licensed under the GNU General Public License 2.0 or later if, at the time Intel provides the Software to Recipient, such addition of the Software to the then publicly available versions of such operating systems available under the GNU General Public License 2.0 or later (whether in gold, beta or alpha form) causes such combination to be covered by the Licensed Patents. The patent license shall not apply to any other combinations which include the Software. NO hardware per se is licensed hereunder. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MECHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR IT CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; ANY LOSS OF USE; DATA, OR PROFITS; OR BUSINESS INTERUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*******************************************************************************/#define __E1000_MAIN__#include "e1000.h"char e1000_driver_name[] = "e1000";char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";char e1000_driver_version[] = "4.2.8";char e1000_copyright[] = "Copyright (c) 1999-2002 Intel Corporation.";/* e1000_pci_tbl - PCI Device ID Table * * Private driver_data field (last one) stores an index into e1000_strings * Wildcard entries (PCI_ANY_ID) should come last * Last entry must be all 0s * * { Vendor ID, Device ID, SubVendor ID, SubDevice ID, * Class, Class Mask, String Index } */static struct pci_device_id e1000_pci_tbl[] __devinitdata = { /* Intel(R) PRO/1000 Network Connection */ {0x8086, 0x1000, 0x8086, 0x1000, 0, 0, 0}, {0x8086, 0x1001, 0x8086, 0x1003, 0, 0, 0}, {0x8086, 0x1004, 0x8086, 0x1004, 0, 0, 0}, {0x8086, 0x1008, 0x8086, 0x1107, 0, 0, 0}, {0x8086, 0x1009, 0x8086, 0x1109, 0, 0, 0}, {0x8086, 0x100C, 0x8086, 0x1112, 0, 0, 0}, {0x8086, 0x100E, 0x8086, 0x001E, 0, 0, 0}, /* Compaq Gigabit Ethernet Server Adapter */ {0x8086, 0x1000, 0x0E11, PCI_ANY_ID, 0, 0, 1}, {0x8086, 0x1001, 0x0E11, PCI_ANY_ID, 0, 0, 1}, {0x8086, 0x1004, 0x0E11, PCI_ANY_ID, 0, 0, 1}, /* IBM Mobile, Desktop & Server Adapters */ {0x8086, 0x1000, 0x1014, PCI_ANY_ID, 0, 0, 2}, {0x8086, 0x1001, 0x1014, PCI_ANY_ID, 0, 0, 2}, {0x8086, 0x1004, 0x1014, PCI_ANY_ID, 0, 0, 2}, /* Generic */ {0x8086, 0x1000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {0x8086, 0x1001, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {0x8086, 0x1004, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {0x8086, 0x1008, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {0x8086, 0x1009, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {0x8086, 0x100C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {0x8086, 0x100D, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, {0x8086, 0x100E, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0}, /* required last entry */ {0,}};MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);static char *e1000_strings[] = { "Intel(R) PRO/1000 Network Connection", "Compaq Gigabit Ethernet Server Adapter", "IBM Mobile, Desktop & Server Adapters"};/* e1000_main.c Function Prototypes */int e1000_up(struct e1000_adapter *adapter);void e1000_down(struct e1000_adapter *adapter);void e1000_reset(struct e1000_adapter *adapter);static int e1000_init_module(void);static void e1000_exit_module(void);static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);static void e1000_remove(struct pci_dev *pdev);static void e1000_sw_init(struct e1000_adapter *adapter);static int e1000_open(struct net_device *netdev);static int e1000_close(struct net_device *netdev);static int e1000_setup_tx_resources(struct e1000_adapter *adapter);static int e1000_setup_rx_resources(struct e1000_adapter *adapter);static void e1000_configure_tx(struct e1000_adapter *adapter);static void e1000_configure_rx(struct e1000_adapter *adapter);static void e1000_setup_rctl(struct e1000_adapter *adapter);static void e1000_clean_tx_ring(struct e1000_adapter *adapter);static void e1000_clean_rx_ring(struct e1000_adapter *adapter);static void e1000_free_tx_resources(struct e1000_adapter *adapter);static void e1000_free_rx_resources(struct e1000_adapter *adapter);static void e1000_set_multi(struct net_device *netdev);static void e1000_update_phy_info(unsigned long data);static void e1000_watchdog(unsigned long data);static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);static void e1000_tx_timeout(struct net_device *dev);static struct net_device_stats * e1000_get_stats(struct net_device *netdev);static int e1000_change_mtu(struct net_device *netdev, int new_mtu);static int e1000_set_mac(struct net_device *netdev, void *p);static void e1000_update_stats(struct e1000_adapter *adapter);static inline void e1000_irq_disable(struct e1000_adapter *adapter);static inline void e1000_irq_enable(struct e1000_adapter *adapter);static void e1000_intr(int irq, void *data, struct pt_regs *regs);static void e1000_clean_tx_irq(struct e1000_adapter *adapter);static void e1000_clean_rx_irq(struct e1000_adapter *adapter);static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter);static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);static void e1000_enter_82542_rst(struct e1000_adapter *adapter);static void e1000_leave_82542_rst(struct e1000_adapter *adapter);static inline void e1000_rx_checksum(struct e1000_adapter *adapter, struct e1000_rx_desc *rx_desc, struct sk_buff *skb);void e1000_enable_WOL(struct e1000_adapter *adapter);#ifdef NETIF_F_HW_VLAN_TXstatic void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);static void e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);static void e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);#endif/* Exported from other modules */extern void e1000_check_options(struct e1000_adapter *adapter);extern void e1000_proc_dev_setup(struct e1000_adapter *adapter);extern void e1000_proc_dev_free(struct e1000_adapter *adapter);extern int e1000_ethtool_ioctl(struct net_device *netdev, struct ifreq *ifr);static struct pci_driver e1000_driver = { name: e1000_driver_name, id_table: e1000_pci_tbl, probe: e1000_probe, remove: __devexit_p(e1000_remove), /* Power Managment Hooks */ suspend: NULL, resume: NULL};MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");MODULE_LICENSE("Dual BSD/GPL");EXPORT_NO_SYMBOLS;/** * e1000_init_module - Driver Registration Routine * * e1000_init_module is the first routine called when the driver is * loaded. All it does is register with the PCI subsystem. **/static int __inite1000_init_module(void){ printk(KERN_INFO "%s - version %s\n", e1000_driver_string, e1000_driver_version); printk(KERN_INFO "%s\n", e1000_copyright); return pci_module_init(&e1000_driver);}module_init(e1000_init_module);/** * e1000_exit_module - Driver Exit Cleanup Routine * * e1000_exit_module is called just before the driver is removed * from memory. **/static void __exite1000_exit_module(void){ pci_unregister_driver(&e1000_driver);}module_exit(e1000_exit_module);inte1000_up(struct e1000_adapter *adapter){ struct net_device *netdev = adapter->netdev; if(request_irq(netdev->irq, &e1000_intr, SA_SHIRQ, netdev->name, netdev)) return -1; /* hardware has been reset, we need to reload some things */ e1000_set_multi(netdev); e1000_configure_tx(adapter); e1000_setup_rctl(adapter); e1000_configure_rx(adapter); e1000_alloc_rx_buffers(adapter); mod_timer(&adapter->watchdog_timer, jiffies); e1000_irq_enable(adapter); return 0;}voide1000_down(struct e1000_adapter *adapter){ struct net_device *netdev = adapter->netdev; e1000_irq_disable(adapter); free_irq(netdev->irq, netdev); del_timer_sync(&adapter->watchdog_timer); del_timer_sync(&adapter->phy_info_timer); adapter->link_speed = 0; adapter->link_duplex = 0; netif_carrier_off(netdev); netif_stop_queue(netdev); e1000_reset(adapter); e1000_clean_tx_ring(adapter); e1000_clean_rx_ring(adapter);}voide1000_reset(struct e1000_adapter *adapter){ /* Repartition Pba for greater than 9k mtu * To take effect CTRL.RST is required. */ if(adapter->rx_buffer_len > E1000_RXBUFFER_8192) E1000_WRITE_REG(&adapter->hw, PBA, E1000_JUMBO_PBA); else E1000_WRITE_REG(&adapter->hw, PBA, E1000_DEFAULT_PBA); adapter->hw.fc = adapter->hw.original_fc; e1000_reset_hw(&adapter->hw); e1000_init_hw(&adapter->hw); e1000_reset_adaptive(&adapter->hw); e1000_phy_get_info(&adapter->hw, &adapter->phy_info); e1000_enable_WOL(adapter);}/** * e1000_probe - Device Initialization Routine * @pdev: PCI device information struct * @ent: entry in e1000_pci_tbl * * Returns 0 on success, negative on failure * * e1000_probe initializes an adapter identified by a pci_dev structure. * The OS initialization, configuring of the adapter private structure, * and a hardware reset occur. **/static int __devinite1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent){ struct net_device *netdev; struct e1000_adapter *adapter; static int cards_found = 0; unsigned long mmio_start; int mmio_len; int pci_using_dac; int i; if((i = pci_enable_device(pdev))) return i; if(!(i = pci_set_dma_mask(pdev, (u64) 0xffffffffffffffff))) { pci_using_dac = 1; } else { if((i = pci_set_dma_mask(pdev, (u64) 0xffffffff))) { E1000_ERR("No usable DMA configuration, aborting\n"); return i; } pci_using_dac = 0; } if((i = pci_request_regions(pdev, e1000_driver_name))) return i; pci_set_master(pdev); netdev = alloc_etherdev(sizeof(struct e1000_adapter)); if(!netdev) goto err_alloc_etherdev; SET_MODULE_OWNER(netdev); pci_set_drvdata(pdev, netdev); adapter = netdev->priv; adapter->netdev = netdev; adapter->pdev = pdev; adapter->hw.back = adapter; mmio_start = pci_resource_start(pdev, BAR_0); mmio_len = pci_resource_len(pdev, BAR_0); adapter->hw.hw_addr = ioremap(mmio_start, mmio_len); if(!adapter->hw.hw_addr) goto err_ioremap; netdev->open = &e1000_open; netdev->stop = &e1000_close; netdev->hard_start_xmit = &e1000_xmit_frame; netdev->get_stats = &e1000_get_stats; netdev->set_multicast_list = &e1000_set_multi; netdev->set_mac_address = &e1000_set_mac; netdev->change_mtu = &e1000_change_mtu; netdev->do_ioctl = &e1000_ioctl; netdev->tx_timeout = &e1000_tx_timeout; netdev->watchdog_timeo = HZ;#ifdef NETIF_F_HW_VLAN_TX netdev->vlan_rx_register = e1000_vlan_rx_register; netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid; netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;#endif netdev->irq = pdev->irq; netdev->base_addr = mmio_start; adapter->bd_number = cards_found; adapter->id_string = e1000_strings[ent->driver_data]; /* setup the private structure */ e1000_sw_init(adapter); if(adapter->hw.mac_type >= e1000_82543) {#ifdef NETIF_F_HW_VLAN_TX netdev->features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX | NETIF_F_HW_VLAN_FILTER;#else netdev->features = NETIF_F_SG | NETIF_F_HW_CSUM;#endif } else { netdev->features = NETIF_F_SG; } if(pci_using_dac) netdev->features |= NETIF_F_HIGHDMA; /* make sure the EEPROM is good */ if(e1000_validate_eeprom_checksum(&adapter->hw) < 0) goto err_eeprom; /* copy the MAC address out of the EEPROM */ e1000_read_mac_addr(&adapter->hw); memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len); if(!is_valid_ether_addr(netdev->dev_addr)) goto err_eeprom; e1000_read_part_num(&adapter->hw, &(adapter->part_num)); e1000_get_bus_info(&adapter->hw); if((adapter->hw.mac_type == e1000_82544) && (adapter->hw.bus_type == e1000_bus_type_pcix)) adapter->max_data_per_txd = 4096; else adapter->max_data_per_txd = MAX_JUMBO_FRAME_SIZE; init_timer(&adapter->watchdog_timer); adapter->watchdog_timer.function = &e1000_watchdog; adapter->watchdog_timer.data = (unsigned long) adapter; init_timer(&adapter->phy_info_timer); adapter->phy_info_timer.function = &e1000_update_phy_info; adapter->phy_info_timer.data = (unsigned long) adapter; register_netdev(netdev); /* we're going to reset, so assume we have no link for now */ netif_carrier_off(netdev); netif_stop_queue(netdev); printk(KERN_INFO "%s: %s\n", netdev->name, adapter->id_string); e1000_check_options(adapter); e1000_proc_dev_setup(adapter); /* reset the hardware with the new settings */ e1000_reset(adapter); cards_found++; return 0;err_eeprom: iounmap(adapter->hw.hw_addr);err_ioremap: pci_release_regions(pdev); kfree(netdev);err_alloc_etherdev: return -ENOMEM;}/** * e1000_remove - Device Removal Routine * @pdev: PCI device information struct * * e1000_remove is called by the PCI subsystem to alert the driver * that it should release a PCI device. The could be caused by a * Hot-Plug event, or because the driver is going to be removed from * memory. **/static void __devexite1000_remove(struct pci_dev *pdev){ struct net_device *netdev = pci_get_drvdata(pdev); struct e1000_adapter *adapter = netdev->priv; unregister_netdev(netdev); e1000_phy_hw_reset(&adapter->hw); e1000_proc_dev_free(adapter); iounmap(adapter->hw.hw_addr); pci_release_regions(pdev); kfree(netdev);}/** * e1000_sw_init - Initialize general software structures (struct e1000_adapter) * @adapter: board private structure to initialize * * e1000_sw_init initializes the Adapter private data structure. * Fields are initialized based on PCI device information and * OS network device settings (MTU size). **/static void __devinite1000_sw_init(struct e1000_adapter *adapter){ struct e1000_hw *hw = &adapter->hw; struct net_device *netdev = adapter->netdev; struct pci_dev *pdev = adapter->pdev; /* PCI config space info */ uint16_t *vendor = &hw->vendor_id; uint16_t *device = &hw->device_id; uint16_t *subvendor = &hw->subsystem_vendor_id; uint16_t *subsystem = &hw->subsystem_id; uint8_t *revision = &hw->revision_id; pci_read_config_word(pdev, PCI_VENDOR_ID, vendor); pci_read_config_word(pdev, PCI_DEVICE_ID, device); pci_read_config_byte(pdev, PCI_REVISION_ID, revision); pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, subvendor); pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, subsystem); pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word); adapter->rx_buffer_len = E1000_RXBUFFER_2048; hw->max_frame_size = netdev->mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?