devices/e1000/e1000_main-2.6.26-ethercat.c
changeset 1360 afcea72f0040
child 2050 a3e59f2a8589
equal deleted inserted replaced
1359:a3d54cccba48 1360:afcea72f0040
       
     1 /*******************************************************************************
       
     2 
       
     3   Intel PRO/1000 Linux driver
       
     4   Copyright(c) 1999 - 2006 Intel Corporation.
       
     5 
       
     6   This program is free software; you can redistribute it and/or modify it
       
     7   under the terms and conditions of the GNU General Public License,
       
     8   version 2, as published by the Free Software Foundation.
       
     9 
       
    10   This program is distributed in the hope it will be useful, but WITHOUT
       
    11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
       
    13   more details.
       
    14 
       
    15   You should have received a copy of the GNU General Public License along with
       
    16   this program; if not, write to the Free Software Foundation, Inc.,
       
    17   51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
       
    18 
       
    19   The full GNU General Public License is included in this distribution in
       
    20   the file called "COPYING".
       
    21 
       
    22   Contact Information:
       
    23   Linux NICS <linux.nics@intel.com>
       
    24   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
       
    25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
       
    26 
       
    27   vim: noexpandtab
       
    28 
       
    29 *******************************************************************************/
       
    30 
       
    31 #include "e1000-2.6.26-ethercat.h"
       
    32 #include <net/ip6_checksum.h>
       
    33 
       
    34 char e1000_driver_name[] = "ec_e1000";
       
    35 static char e1000_driver_string[] = "EtherCAT Intel(R) PRO/1000 Network Driver";
       
    36 #ifndef CONFIG_E1000_NAPI
       
    37 #define DRIVERNAPI
       
    38 #else
       
    39 #define DRIVERNAPI "-NAPI"
       
    40 #endif
       
    41 #define DRV_VERSION "7.3.20-k2"DRIVERNAPI
       
    42 const char e1000_driver_version[] = DRV_VERSION;
       
    43 static const char e1000_copyright[] = "Copyright (c) 1999-2006 Intel Corporation.";
       
    44 
       
    45 /* e1000_pci_tbl - PCI Device ID Table
       
    46  *
       
    47  * Last entry must be all 0s
       
    48  *
       
    49  * Macro expands to...
       
    50  *   {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
       
    51  */
       
    52 #ifdef CONFIG_E1000E_ENABLED
       
    53   #define PCIE(x) 
       
    54 #else
       
    55   #define PCIE(x) x,
       
    56 #endif
       
    57 
       
    58 static struct pci_device_id e1000_pci_tbl[] = {
       
    59 	INTEL_E1000_ETHERNET_DEVICE(0x1000),
       
    60 	INTEL_E1000_ETHERNET_DEVICE(0x1001),
       
    61 	INTEL_E1000_ETHERNET_DEVICE(0x1004),
       
    62 	INTEL_E1000_ETHERNET_DEVICE(0x1008),
       
    63 	INTEL_E1000_ETHERNET_DEVICE(0x1009),
       
    64 	INTEL_E1000_ETHERNET_DEVICE(0x100C),
       
    65 	INTEL_E1000_ETHERNET_DEVICE(0x100D),
       
    66 	INTEL_E1000_ETHERNET_DEVICE(0x100E),
       
    67 	INTEL_E1000_ETHERNET_DEVICE(0x100F),
       
    68 	INTEL_E1000_ETHERNET_DEVICE(0x1010),
       
    69 	INTEL_E1000_ETHERNET_DEVICE(0x1011),
       
    70 	INTEL_E1000_ETHERNET_DEVICE(0x1012),
       
    71 	INTEL_E1000_ETHERNET_DEVICE(0x1013),
       
    72 	INTEL_E1000_ETHERNET_DEVICE(0x1014),
       
    73 	INTEL_E1000_ETHERNET_DEVICE(0x1015),
       
    74 	INTEL_E1000_ETHERNET_DEVICE(0x1016),
       
    75 	INTEL_E1000_ETHERNET_DEVICE(0x1017),
       
    76 	INTEL_E1000_ETHERNET_DEVICE(0x1018),
       
    77 	INTEL_E1000_ETHERNET_DEVICE(0x1019),
       
    78 	INTEL_E1000_ETHERNET_DEVICE(0x101A),
       
    79 	INTEL_E1000_ETHERNET_DEVICE(0x101D),
       
    80 	INTEL_E1000_ETHERNET_DEVICE(0x101E),
       
    81 	INTEL_E1000_ETHERNET_DEVICE(0x1026),
       
    82 	INTEL_E1000_ETHERNET_DEVICE(0x1027),
       
    83 	INTEL_E1000_ETHERNET_DEVICE(0x1028),
       
    84 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x1049))
       
    85 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x104A))
       
    86 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x104B))
       
    87 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x104C))
       
    88 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x104D))
       
    89 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x105E))
       
    90 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x105F))
       
    91 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x1060))
       
    92 	INTEL_E1000_ETHERNET_DEVICE(0x1075),
       
    93 	INTEL_E1000_ETHERNET_DEVICE(0x1076),
       
    94 	INTEL_E1000_ETHERNET_DEVICE(0x1077),
       
    95 	INTEL_E1000_ETHERNET_DEVICE(0x1078),
       
    96 	INTEL_E1000_ETHERNET_DEVICE(0x1079),
       
    97 	INTEL_E1000_ETHERNET_DEVICE(0x107A),
       
    98 	INTEL_E1000_ETHERNET_DEVICE(0x107B),
       
    99 	INTEL_E1000_ETHERNET_DEVICE(0x107C),
       
   100 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x107D))
       
   101 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x107E))
       
   102 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x107F))
       
   103 	INTEL_E1000_ETHERNET_DEVICE(0x108A),
       
   104 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x108B))
       
   105 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x108C))
       
   106 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x1096))
       
   107 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x1098))
       
   108 	INTEL_E1000_ETHERNET_DEVICE(0x1099),
       
   109 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x109A))
       
   110 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10A4))
       
   111 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10A5))
       
   112 	INTEL_E1000_ETHERNET_DEVICE(0x10B5),
       
   113 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10B9))
       
   114 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10BA))
       
   115 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10BB))
       
   116 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10BC))
       
   117 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10C4))
       
   118 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10C5))
       
   119 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10D5))
       
   120 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10D9))
       
   121 PCIE(	INTEL_E1000_ETHERNET_DEVICE(0x10DA))
       
   122 	/* required last entry */
       
   123 	{0,}
       
   124 };
       
   125 
       
   126 // do not auto-load driver
       
   127 // MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
       
   128 
       
   129 int e1000_up(struct e1000_adapter *adapter);
       
   130 void e1000_down(struct e1000_adapter *adapter);
       
   131 void e1000_reinit_locked(struct e1000_adapter *adapter);
       
   132 void e1000_reset(struct e1000_adapter *adapter);
       
   133 int e1000_set_spd_dplx(struct e1000_adapter *adapter, u16 spddplx);
       
   134 int e1000_setup_all_tx_resources(struct e1000_adapter *adapter);
       
   135 int e1000_setup_all_rx_resources(struct e1000_adapter *adapter);
       
   136 void e1000_free_all_tx_resources(struct e1000_adapter *adapter);
       
   137 void e1000_free_all_rx_resources(struct e1000_adapter *adapter);
       
   138 static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
       
   139                              struct e1000_tx_ring *txdr);
       
   140 static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
       
   141                              struct e1000_rx_ring *rxdr);
       
   142 static void e1000_free_tx_resources(struct e1000_adapter *adapter,
       
   143                              struct e1000_tx_ring *tx_ring);
       
   144 static void e1000_free_rx_resources(struct e1000_adapter *adapter,
       
   145                              struct e1000_rx_ring *rx_ring);
       
   146 void e1000_update_stats(struct e1000_adapter *adapter);
       
   147 
       
   148 static int e1000_init_module(void);
       
   149 static void e1000_exit_module(void);
       
   150 static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
       
   151 static void __devexit e1000_remove(struct pci_dev *pdev);
       
   152 static int e1000_alloc_queues(struct e1000_adapter *adapter);
       
   153 static int e1000_sw_init(struct e1000_adapter *adapter);
       
   154 static int e1000_open(struct net_device *netdev);
       
   155 static int e1000_close(struct net_device *netdev);
       
   156 static void e1000_configure_tx(struct e1000_adapter *adapter);
       
   157 static void e1000_configure_rx(struct e1000_adapter *adapter);
       
   158 static void e1000_setup_rctl(struct e1000_adapter *adapter);
       
   159 static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
       
   160 static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
       
   161 static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
       
   162                                 struct e1000_tx_ring *tx_ring);
       
   163 static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
       
   164                                 struct e1000_rx_ring *rx_ring);
       
   165 static void e1000_set_rx_mode(struct net_device *netdev);
       
   166 static void e1000_update_phy_info(unsigned long data);
       
   167 static void e1000_watchdog(unsigned long data);
       
   168 static void e1000_82547_tx_fifo_stall(unsigned long data);
       
   169 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
       
   170 static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
       
   171 static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
       
   172 static int e1000_set_mac(struct net_device *netdev, void *p);
       
   173 void ec_poll(struct net_device *);
       
   174 static irqreturn_t e1000_intr(int irq, void *data);
       
   175 static irqreturn_t e1000_intr_msi(int irq, void *data);
       
   176 static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
       
   177 			       struct e1000_tx_ring *tx_ring);
       
   178 #ifdef CONFIG_E1000_NAPI
       
   179 static int e1000_clean(struct napi_struct *napi, int budget);
       
   180 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
       
   181 			       struct e1000_rx_ring *rx_ring,
       
   182 			       int *work_done, int work_to_do);
       
   183 static bool e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
       
   184 				  struct e1000_rx_ring *rx_ring,
       
   185 				  int *work_done, int work_to_do);
       
   186 #else
       
   187 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
       
   188 			       struct e1000_rx_ring *rx_ring);
       
   189 static bool e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
       
   190 				  struct e1000_rx_ring *rx_ring);
       
   191 #endif
       
   192 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
       
   193                                    struct e1000_rx_ring *rx_ring,
       
   194 				   int cleaned_count);
       
   195 static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
       
   196                                       struct e1000_rx_ring *rx_ring,
       
   197 				      int cleaned_count);
       
   198 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
       
   199 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
       
   200 			   int cmd);
       
   201 static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
       
   202 static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
       
   203 static void e1000_tx_timeout(struct net_device *dev);
       
   204 static void e1000_reset_task(struct work_struct *work);
       
   205 static void e1000_smartspeed(struct e1000_adapter *adapter);
       
   206 static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
       
   207                                        struct sk_buff *skb);
       
   208 
       
   209 static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
       
   210 static void e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid);
       
   211 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid);
       
   212 static void e1000_restore_vlan(struct e1000_adapter *adapter);
       
   213 
       
   214 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
       
   215 #ifdef CONFIG_PM
       
   216 static int e1000_resume(struct pci_dev *pdev);
       
   217 #endif
       
   218 static void e1000_shutdown(struct pci_dev *pdev);
       
   219 
       
   220 #ifdef CONFIG_NET_POLL_CONTROLLER
       
   221 /* for netdump / net console */
       
   222 static void e1000_netpoll (struct net_device *netdev);
       
   223 #endif
       
   224 
       
   225 #define COPYBREAK_DEFAULT 256
       
   226 static unsigned int copybreak __read_mostly = COPYBREAK_DEFAULT;
       
   227 module_param(copybreak, uint, 0644);
       
   228 MODULE_PARM_DESC(copybreak,
       
   229 	"Maximum size of packet that is copied to a new buffer on receive");
       
   230 
       
   231 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
       
   232                      pci_channel_state_t state);
       
   233 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev);
       
   234 static void e1000_io_resume(struct pci_dev *pdev);
       
   235 
       
   236 static struct pci_error_handlers e1000_err_handler = {
       
   237 	.error_detected = e1000_io_error_detected,
       
   238 	.slot_reset = e1000_io_slot_reset,
       
   239 	.resume = e1000_io_resume,
       
   240 };
       
   241 
       
   242 static struct pci_driver e1000_driver = {
       
   243 	.name     = e1000_driver_name,
       
   244 	.id_table = e1000_pci_tbl,
       
   245 	.probe    = e1000_probe,
       
   246 	.remove   = __devexit_p(e1000_remove),
       
   247 #ifdef CONFIG_PM
       
   248 	/* Power Managment Hooks */
       
   249 	.suspend  = e1000_suspend,
       
   250 	.resume   = e1000_resume,
       
   251 #endif
       
   252 	.shutdown = e1000_shutdown,
       
   253 	.err_handler = &e1000_err_handler
       
   254 };
       
   255 
       
   256 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
       
   257 MODULE_DESCRIPTION("EtherCAT-capable Intel(R) PRO/1000 Network Driver");
       
   258 MODULE_LICENSE("GPL");
       
   259 MODULE_VERSION(DRV_VERSION);
       
   260 
       
   261 static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
       
   262 module_param(debug, int, 0);
       
   263 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
       
   264 
       
   265 /**
       
   266  * e1000_init_module - Driver Registration Routine
       
   267  *
       
   268  * e1000_init_module is the first routine called when the driver is
       
   269  * loaded. All it does is register with the PCI subsystem.
       
   270  **/
       
   271 
       
   272 static int __init
       
   273 e1000_init_module(void)
       
   274 {
       
   275 	int ret;
       
   276 	printk(KERN_INFO "%s - version %s\n",
       
   277 	       e1000_driver_string, e1000_driver_version);
       
   278 
       
   279 	printk(KERN_INFO "%s\n", e1000_copyright);
       
   280 
       
   281 	ret = pci_register_driver(&e1000_driver);
       
   282 	if (copybreak != COPYBREAK_DEFAULT) {
       
   283 		if (copybreak == 0)
       
   284 			printk(KERN_INFO "e1000: copybreak disabled\n");
       
   285 		else
       
   286 			printk(KERN_INFO "e1000: copybreak enabled for "
       
   287 			       "packets <= %u bytes\n", copybreak);
       
   288 	}
       
   289 	return ret;
       
   290 }
       
   291 
       
   292 module_init(e1000_init_module);
       
   293 
       
   294 /**
       
   295  * e1000_exit_module - Driver Exit Cleanup Routine
       
   296  *
       
   297  * e1000_exit_module is called just before the driver is removed
       
   298  * from memory.
       
   299  **/
       
   300 
       
   301 static void __exit
       
   302 e1000_exit_module(void)
       
   303 {
       
   304 	pci_unregister_driver(&e1000_driver);
       
   305 }
       
   306 
       
   307 module_exit(e1000_exit_module);
       
   308 
       
   309 static int e1000_request_irq(struct e1000_adapter *adapter)
       
   310 {
       
   311 	struct net_device *netdev = adapter->netdev;
       
   312 	irq_handler_t handler = e1000_intr;
       
   313 	int irq_flags = IRQF_SHARED;
       
   314 	int err;
       
   315 
       
   316 	if (adapter->ecdev)
       
   317 		return 0;
       
   318 
       
   319 	if (adapter->hw.mac_type >= e1000_82571) {
       
   320 		adapter->have_msi = !pci_enable_msi(adapter->pdev);
       
   321 		if (adapter->have_msi) {
       
   322 			handler = e1000_intr_msi;
       
   323 			irq_flags = 0;
       
   324 		}
       
   325 	}
       
   326 
       
   327 	err = request_irq(adapter->pdev->irq, handler, irq_flags, netdev->name,
       
   328 	                  netdev);
       
   329 	if (err) {
       
   330 		if (adapter->have_msi)
       
   331 			pci_disable_msi(adapter->pdev);
       
   332 		DPRINTK(PROBE, ERR,
       
   333 		        "Unable to allocate interrupt Error: %d\n", err);
       
   334 	}
       
   335 
       
   336 	return err;
       
   337 }
       
   338 
       
   339 static void e1000_free_irq(struct e1000_adapter *adapter)
       
   340 {
       
   341 	struct net_device *netdev = adapter->netdev;
       
   342 
       
   343 	if (adapter->ecdev)
       
   344 		return;
       
   345 
       
   346 	free_irq(adapter->pdev->irq, netdev);
       
   347 
       
   348 	if (adapter->have_msi)
       
   349 		pci_disable_msi(adapter->pdev);
       
   350 }
       
   351 
       
   352 /**
       
   353  * e1000_irq_disable - Mask off interrupt generation on the NIC
       
   354  * @adapter: board private structure
       
   355  **/
       
   356 
       
   357 static void
       
   358 e1000_irq_disable(struct e1000_adapter *adapter)
       
   359 {
       
   360 	if (adapter->ecdev)
       
   361 		return;
       
   362 
       
   363 	E1000_WRITE_REG(&adapter->hw, IMC, ~0);
       
   364 	E1000_WRITE_FLUSH(&adapter->hw);
       
   365 	synchronize_irq(adapter->pdev->irq);
       
   366 }
       
   367 
       
   368 /**
       
   369  * e1000_irq_enable - Enable default interrupt generation settings
       
   370  * @adapter: board private structure
       
   371  **/
       
   372 
       
   373 static void
       
   374 e1000_irq_enable(struct e1000_adapter *adapter)
       
   375 {
       
   376 	if (adapter->ecdev)
       
   377 		return;
       
   378 
       
   379 	E1000_WRITE_REG(&adapter->hw, IMS, IMS_ENABLE_MASK);
       
   380 	E1000_WRITE_FLUSH(&adapter->hw);
       
   381 }
       
   382 
       
   383 static void
       
   384 e1000_update_mng_vlan(struct e1000_adapter *adapter)
       
   385 {
       
   386 	struct net_device *netdev = adapter->netdev;
       
   387 	u16 vid = adapter->hw.mng_cookie.vlan_id;
       
   388 	u16 old_vid = adapter->mng_vlan_id;
       
   389 	if (adapter->vlgrp) {
       
   390 		if (!vlan_group_get_device(adapter->vlgrp, vid)) {
       
   391 			if (adapter->hw.mng_cookie.status &
       
   392 				E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
       
   393 				e1000_vlan_rx_add_vid(netdev, vid);
       
   394 				adapter->mng_vlan_id = vid;
       
   395 			} else
       
   396 				adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
       
   397 
       
   398 			if ((old_vid != (u16)E1000_MNG_VLAN_NONE) &&
       
   399 					(vid != old_vid) &&
       
   400 			    !vlan_group_get_device(adapter->vlgrp, old_vid))
       
   401 				e1000_vlan_rx_kill_vid(netdev, old_vid);
       
   402 		} else
       
   403 			adapter->mng_vlan_id = vid;
       
   404 	}
       
   405 }
       
   406 
       
   407 /**
       
   408  * e1000_release_hw_control - release control of the h/w to f/w
       
   409  * @adapter: address of board private structure
       
   410  *
       
   411  * e1000_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
       
   412  * For ASF and Pass Through versions of f/w this means that the
       
   413  * driver is no longer loaded. For AMT version (only with 82573) i
       
   414  * of the f/w this means that the network i/f is closed.
       
   415  *
       
   416  **/
       
   417 
       
   418 static void
       
   419 e1000_release_hw_control(struct e1000_adapter *adapter)
       
   420 {
       
   421 	u32 ctrl_ext;
       
   422 	u32 swsm;
       
   423 
       
   424 	/* Let firmware taken over control of h/w */
       
   425 	switch (adapter->hw.mac_type) {
       
   426 	case e1000_82573:
       
   427 		swsm = E1000_READ_REG(&adapter->hw, SWSM);
       
   428 		E1000_WRITE_REG(&adapter->hw, SWSM,
       
   429 				swsm & ~E1000_SWSM_DRV_LOAD);
       
   430 		break;
       
   431 	case e1000_82571:
       
   432 	case e1000_82572:
       
   433 	case e1000_80003es2lan:
       
   434 	case e1000_ich8lan:
       
   435 		ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
       
   436 		E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
       
   437 				ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
       
   438 		break;
       
   439 	default:
       
   440 		break;
       
   441 	}
       
   442 }
       
   443 
       
   444 /**
       
   445  * e1000_get_hw_control - get control of the h/w from f/w
       
   446  * @adapter: address of board private structure
       
   447  *
       
   448  * e1000_get_hw_control sets {CTRL_EXT|FWSM}:DRV_LOAD bit.
       
   449  * For ASF and Pass Through versions of f/w this means that
       
   450  * the driver is loaded. For AMT version (only with 82573)
       
   451  * of the f/w this means that the network i/f is open.
       
   452  *
       
   453  **/
       
   454 
       
   455 static void
       
   456 e1000_get_hw_control(struct e1000_adapter *adapter)
       
   457 {
       
   458 	u32 ctrl_ext;
       
   459 	u32 swsm;
       
   460 
       
   461 	/* Let firmware know the driver has taken over */
       
   462 	switch (adapter->hw.mac_type) {
       
   463 	case e1000_82573:
       
   464 		swsm = E1000_READ_REG(&adapter->hw, SWSM);
       
   465 		E1000_WRITE_REG(&adapter->hw, SWSM,
       
   466 				swsm | E1000_SWSM_DRV_LOAD);
       
   467 		break;
       
   468 	case e1000_82571:
       
   469 	case e1000_82572:
       
   470 	case e1000_80003es2lan:
       
   471 	case e1000_ich8lan:
       
   472 		ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
       
   473 		E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
       
   474 				ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
       
   475 		break;
       
   476 	default:
       
   477 		break;
       
   478 	}
       
   479 }
       
   480 
       
   481 static void
       
   482 e1000_init_manageability(struct e1000_adapter *adapter)
       
   483 {
       
   484 	if (adapter->en_mng_pt) {
       
   485 		u32 manc = E1000_READ_REG(&adapter->hw, MANC);
       
   486 
       
   487 		/* disable hardware interception of ARP */
       
   488 		manc &= ~(E1000_MANC_ARP_EN);
       
   489 
       
   490 		/* enable receiving management packets to the host */
       
   491 		/* this will probably generate destination unreachable messages
       
   492 		 * from the host OS, but the packets will be handled on SMBUS */
       
   493 		if (adapter->hw.has_manc2h) {
       
   494 			u32 manc2h = E1000_READ_REG(&adapter->hw, MANC2H);
       
   495 
       
   496 			manc |= E1000_MANC_EN_MNG2HOST;
       
   497 #define E1000_MNG2HOST_PORT_623 (1 << 5)
       
   498 #define E1000_MNG2HOST_PORT_664 (1 << 6)
       
   499 			manc2h |= E1000_MNG2HOST_PORT_623;
       
   500 			manc2h |= E1000_MNG2HOST_PORT_664;
       
   501 			E1000_WRITE_REG(&adapter->hw, MANC2H, manc2h);
       
   502 		}
       
   503 
       
   504 		E1000_WRITE_REG(&adapter->hw, MANC, manc);
       
   505 	}
       
   506 }
       
   507 
       
   508 static void
       
   509 e1000_release_manageability(struct e1000_adapter *adapter)
       
   510 {
       
   511 	if (adapter->en_mng_pt) {
       
   512 		u32 manc = E1000_READ_REG(&adapter->hw, MANC);
       
   513 
       
   514 		/* re-enable hardware interception of ARP */
       
   515 		manc |= E1000_MANC_ARP_EN;
       
   516 
       
   517 		if (adapter->hw.has_manc2h)
       
   518 			manc &= ~E1000_MANC_EN_MNG2HOST;
       
   519 
       
   520 		/* don't explicitly have to mess with MANC2H since
       
   521 		 * MANC has an enable disable that gates MANC2H */
       
   522 
       
   523 		E1000_WRITE_REG(&adapter->hw, MANC, manc);
       
   524 	}
       
   525 }
       
   526 
       
   527 /**
       
   528  * e1000_configure - configure the hardware for RX and TX
       
   529  * @adapter = private board structure
       
   530  **/
       
   531 static void e1000_configure(struct e1000_adapter *adapter)
       
   532 {
       
   533 	struct net_device *netdev = adapter->netdev;
       
   534 	int i;
       
   535 
       
   536 	e1000_set_rx_mode(netdev);
       
   537 
       
   538 	e1000_restore_vlan(adapter);
       
   539 	e1000_init_manageability(adapter);
       
   540 
       
   541 	e1000_configure_tx(adapter);
       
   542 	e1000_setup_rctl(adapter);
       
   543 	e1000_configure_rx(adapter);
       
   544 	/* call E1000_DESC_UNUSED which always leaves
       
   545 	 * at least 1 descriptor unused to make sure
       
   546 	 * next_to_use != next_to_clean */
       
   547 	for (i = 0; i < adapter->num_rx_queues; i++) {
       
   548 		struct e1000_rx_ring *ring = &adapter->rx_ring[i];
       
   549 		if (adapter->ecdev) {
       
   550 			/* fill rx ring completely! */
       
   551 			adapter->alloc_rx_buf(adapter, ring, ring->count);
       
   552 		} else {
       
   553             /* this one leaves the last ring element unallocated! */
       
   554 			adapter->alloc_rx_buf(adapter, ring,
       
   555 					E1000_DESC_UNUSED(ring));
       
   556 		}
       
   557 	}
       
   558 
       
   559 	adapter->tx_queue_len = netdev->tx_queue_len;
       
   560 }
       
   561 
       
   562 int e1000_up(struct e1000_adapter *adapter)
       
   563 {
       
   564 	/* hardware has been reset, we need to reload some things */
       
   565 	e1000_configure(adapter);
       
   566 
       
   567 	clear_bit(__E1000_DOWN, &adapter->flags);
       
   568 
       
   569 	if (!adapter->ecdev) {
       
   570 #ifdef CONFIG_E1000_NAPI
       
   571 		napi_enable(&adapter->napi);
       
   572 #endif
       
   573 		e1000_irq_enable(adapter);
       
   574 	}
       
   575 
       
   576 	/* fire a link change interrupt to start the watchdog */
       
   577 	if (!adapter->ecdev)
       
   578 		E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_LSC);
       
   579 	return 0;
       
   580 }
       
   581 
       
   582 /**
       
   583  * e1000_power_up_phy - restore link in case the phy was powered down
       
   584  * @adapter: address of board private structure
       
   585  *
       
   586  * The phy may be powered down to save power and turn off link when the
       
   587  * driver is unloaded and wake on lan is not enabled (among others)
       
   588  * *** this routine MUST be followed by a call to e1000_reset ***
       
   589  *
       
   590  **/
       
   591 
       
   592 void e1000_power_up_phy(struct e1000_adapter *adapter)
       
   593 {
       
   594 	u16 mii_reg = 0;
       
   595 
       
   596 	/* Just clear the power down bit to wake the phy back up */
       
   597 	if (adapter->hw.media_type == e1000_media_type_copper) {
       
   598 		/* according to the manual, the phy will retain its
       
   599 		 * settings across a power-down/up cycle */
       
   600 		e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
       
   601 		mii_reg &= ~MII_CR_POWER_DOWN;
       
   602 		e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
       
   603 	}
       
   604 }
       
   605 
       
   606 static void e1000_power_down_phy(struct e1000_adapter *adapter)
       
   607 {
       
   608 	/* Power down the PHY so no link is implied when interface is down *
       
   609 	 * The PHY cannot be powered down if any of the following is true *
       
   610 	 * (a) WoL is enabled
       
   611 	 * (b) AMT is active
       
   612 	 * (c) SoL/IDER session is active */
       
   613 	if (!adapter->wol && adapter->hw.mac_type >= e1000_82540 &&
       
   614 	   adapter->hw.media_type == e1000_media_type_copper) {
       
   615 		u16 mii_reg = 0;
       
   616 
       
   617 		switch (adapter->hw.mac_type) {
       
   618 		case e1000_82540:
       
   619 		case e1000_82545:
       
   620 		case e1000_82545_rev_3:
       
   621 		case e1000_82546:
       
   622 		case e1000_82546_rev_3:
       
   623 		case e1000_82541:
       
   624 		case e1000_82541_rev_2:
       
   625 		case e1000_82547:
       
   626 		case e1000_82547_rev_2:
       
   627 			if (E1000_READ_REG(&adapter->hw, MANC) &
       
   628 			    E1000_MANC_SMBUS_EN)
       
   629 				goto out;
       
   630 			break;
       
   631 		case e1000_82571:
       
   632 		case e1000_82572:
       
   633 		case e1000_82573:
       
   634 		case e1000_80003es2lan:
       
   635 		case e1000_ich8lan:
       
   636 			if (e1000_check_mng_mode(&adapter->hw) ||
       
   637 			    e1000_check_phy_reset_block(&adapter->hw))
       
   638 				goto out;
       
   639 			break;
       
   640 		default:
       
   641 			goto out;
       
   642 		}
       
   643 		e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
       
   644 		mii_reg |= MII_CR_POWER_DOWN;
       
   645 		e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
       
   646 		mdelay(1);
       
   647 	}
       
   648 out:
       
   649 	return;
       
   650 }
       
   651 
       
   652 void
       
   653 e1000_down(struct e1000_adapter *adapter)
       
   654 {
       
   655 	struct net_device *netdev = adapter->netdev;
       
   656 
       
   657 	/* signal that we're down so the interrupt handler does not
       
   658 	 * reschedule our watchdog timer */
       
   659 	set_bit(__E1000_DOWN, &adapter->flags);
       
   660 
       
   661 	if (!adapter->ecdev) {
       
   662 #ifdef CONFIG_E1000_NAPI
       
   663 		napi_disable(&adapter->napi);
       
   664 #endif
       
   665 		e1000_irq_disable(adapter);
       
   666 
       
   667 		del_timer_sync(&adapter->tx_fifo_stall_timer);
       
   668 		del_timer_sync(&adapter->watchdog_timer);
       
   669 		del_timer_sync(&adapter->phy_info_timer);
       
   670 	}
       
   671 
       
   672 	netdev->tx_queue_len = adapter->tx_queue_len;
       
   673 	adapter->link_speed = 0;
       
   674 	adapter->link_duplex = 0;
       
   675 	
       
   676 	if (!adapter->ecdev) {
       
   677 		netif_carrier_off(netdev);
       
   678 		netif_stop_queue(netdev);
       
   679 	}
       
   680 
       
   681 	e1000_reset(adapter);
       
   682 	e1000_clean_all_tx_rings(adapter);
       
   683 	e1000_clean_all_rx_rings(adapter);
       
   684 }
       
   685 
       
   686 void
       
   687 e1000_reinit_locked(struct e1000_adapter *adapter)
       
   688 {
       
   689 	WARN_ON(in_interrupt());
       
   690 	while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
       
   691 		msleep(1);
       
   692 	e1000_down(adapter);
       
   693 	e1000_up(adapter);
       
   694 	clear_bit(__E1000_RESETTING, &adapter->flags);
       
   695 }
       
   696 
       
   697 void
       
   698 e1000_reset(struct e1000_adapter *adapter)
       
   699 {
       
   700 	u32 pba = 0, tx_space, min_tx_space, min_rx_space;
       
   701 	u16 fc_high_water_mark = E1000_FC_HIGH_DIFF;
       
   702 	bool legacy_pba_adjust = false;
       
   703 
       
   704 	/* Repartition Pba for greater than 9k mtu
       
   705 	 * To take effect CTRL.RST is required.
       
   706 	 */
       
   707 
       
   708 	switch (adapter->hw.mac_type) {
       
   709 	case e1000_82542_rev2_0:
       
   710 	case e1000_82542_rev2_1:
       
   711 	case e1000_82543:
       
   712 	case e1000_82544:
       
   713 	case e1000_82540:
       
   714 	case e1000_82541:
       
   715 	case e1000_82541_rev_2:
       
   716 		legacy_pba_adjust = true;
       
   717 		pba = E1000_PBA_48K;
       
   718 		break;
       
   719 	case e1000_82545:
       
   720 	case e1000_82545_rev_3:
       
   721 	case e1000_82546:
       
   722 	case e1000_82546_rev_3:
       
   723 		pba = E1000_PBA_48K;
       
   724 		break;
       
   725 	case e1000_82547:
       
   726 	case e1000_82547_rev_2:
       
   727 		legacy_pba_adjust = true;
       
   728 		pba = E1000_PBA_30K;
       
   729 		break;
       
   730 	case e1000_82571:
       
   731 	case e1000_82572:
       
   732 	case e1000_80003es2lan:
       
   733 		pba = E1000_PBA_38K;
       
   734 		break;
       
   735 	case e1000_82573:
       
   736 		pba = E1000_PBA_20K;
       
   737 		break;
       
   738 	case e1000_ich8lan:
       
   739 		pba = E1000_PBA_8K;
       
   740 	case e1000_undefined:
       
   741 	case e1000_num_macs:
       
   742 		break;
       
   743 	}
       
   744 
       
   745 	if (legacy_pba_adjust) {
       
   746 		if (adapter->netdev->mtu > E1000_RXBUFFER_8192)
       
   747 			pba -= 8; /* allocate more FIFO for Tx */
       
   748 
       
   749 		if (adapter->hw.mac_type == e1000_82547) {
       
   750 			adapter->tx_fifo_head = 0;
       
   751 			adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
       
   752 			adapter->tx_fifo_size =
       
   753 				(E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
       
   754 			atomic_set(&adapter->tx_fifo_stall, 0);
       
   755 		}
       
   756 	} else if (adapter->hw.max_frame_size > MAXIMUM_ETHERNET_FRAME_SIZE) {
       
   757 		/* adjust PBA for jumbo frames */
       
   758 		E1000_WRITE_REG(&adapter->hw, PBA, pba);
       
   759 
       
   760 		/* To maintain wire speed transmits, the Tx FIFO should be
       
   761 		 * large enough to accomodate two full transmit packets,
       
   762 		 * rounded up to the next 1KB and expressed in KB.  Likewise,
       
   763 		 * the Rx FIFO should be large enough to accomodate at least
       
   764 		 * one full receive packet and is similarly rounded up and
       
   765 		 * expressed in KB. */
       
   766 		pba = E1000_READ_REG(&adapter->hw, PBA);
       
   767 		/* upper 16 bits has Tx packet buffer allocation size in KB */
       
   768 		tx_space = pba >> 16;
       
   769 		/* lower 16 bits has Rx packet buffer allocation size in KB */
       
   770 		pba &= 0xffff;
       
   771 		/* don't include ethernet FCS because hardware appends/strips */
       
   772 		min_rx_space = adapter->netdev->mtu + ENET_HEADER_SIZE +
       
   773 		               VLAN_TAG_SIZE;
       
   774 		min_tx_space = min_rx_space;
       
   775 		min_tx_space *= 2;
       
   776 		min_tx_space = ALIGN(min_tx_space, 1024);
       
   777 		min_tx_space >>= 10;
       
   778 		min_rx_space = ALIGN(min_rx_space, 1024);
       
   779 		min_rx_space >>= 10;
       
   780 
       
   781 		/* If current Tx allocation is less than the min Tx FIFO size,
       
   782 		 * and the min Tx FIFO size is less than the current Rx FIFO
       
   783 		 * allocation, take space away from current Rx allocation */
       
   784 		if (tx_space < min_tx_space &&
       
   785 		    ((min_tx_space - tx_space) < pba)) {
       
   786 			pba = pba - (min_tx_space - tx_space);
       
   787 
       
   788 			/* PCI/PCIx hardware has PBA alignment constraints */
       
   789 			switch (adapter->hw.mac_type) {
       
   790 			case e1000_82545 ... e1000_82546_rev_3:
       
   791 				pba &= ~(E1000_PBA_8K - 1);
       
   792 				break;
       
   793 			default:
       
   794 				break;
       
   795 			}
       
   796 
       
   797 			/* if short on rx space, rx wins and must trump tx
       
   798 			 * adjustment or use Early Receive if available */
       
   799 			if (pba < min_rx_space) {
       
   800 				switch (adapter->hw.mac_type) {
       
   801 				case e1000_82573:
       
   802 					/* ERT enabled in e1000_configure_rx */
       
   803 					break;
       
   804 				default:
       
   805 					pba = min_rx_space;
       
   806 					break;
       
   807 				}
       
   808 			}
       
   809 		}
       
   810 	}
       
   811 
       
   812 	E1000_WRITE_REG(&adapter->hw, PBA, pba);
       
   813 
       
   814 	/* flow control settings */
       
   815 	/* Set the FC high water mark to 90% of the FIFO size.
       
   816 	 * Required to clear last 3 LSB */
       
   817 	fc_high_water_mark = ((pba * 9216)/10) & 0xFFF8;
       
   818 	/* We can't use 90% on small FIFOs because the remainder
       
   819 	 * would be less than 1 full frame.  In this case, we size
       
   820 	 * it to allow at least a full frame above the high water
       
   821 	 *  mark. */
       
   822 	if (pba < E1000_PBA_16K)
       
   823 		fc_high_water_mark = (pba * 1024) - 1600;
       
   824 
       
   825 	adapter->hw.fc_high_water = fc_high_water_mark;
       
   826 	adapter->hw.fc_low_water = fc_high_water_mark - 8;
       
   827 	if (adapter->hw.mac_type == e1000_80003es2lan)
       
   828 		adapter->hw.fc_pause_time = 0xFFFF;
       
   829 	else
       
   830 		adapter->hw.fc_pause_time = E1000_FC_PAUSE_TIME;
       
   831 	adapter->hw.fc_send_xon = 1;
       
   832 	adapter->hw.fc = adapter->hw.original_fc;
       
   833 
       
   834 	/* Allow time for pending master requests to run */
       
   835 	e1000_reset_hw(&adapter->hw);
       
   836 	if (adapter->hw.mac_type >= e1000_82544)
       
   837 		E1000_WRITE_REG(&adapter->hw, WUC, 0);
       
   838 
       
   839 	if (e1000_init_hw(&adapter->hw))
       
   840 		DPRINTK(PROBE, ERR, "Hardware Error\n");
       
   841 	e1000_update_mng_vlan(adapter);
       
   842 
       
   843 	/* if (adapter->hwflags & HWFLAGS_PHY_PWR_BIT) { */
       
   844 	if (adapter->hw.mac_type >= e1000_82544 &&
       
   845 	    adapter->hw.mac_type <= e1000_82547_rev_2 &&
       
   846 	    adapter->hw.autoneg == 1 &&
       
   847 	    adapter->hw.autoneg_advertised == ADVERTISE_1000_FULL) {
       
   848 		u32 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
       
   849 		/* clear phy power management bit if we are in gig only mode,
       
   850 		 * which if enabled will attempt negotiation to 100Mb, which
       
   851 		 * can cause a loss of link at power off or driver unload */
       
   852 		ctrl &= ~E1000_CTRL_SWDPIN3;
       
   853 		E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
       
   854 	}
       
   855 
       
   856 	/* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
       
   857 	E1000_WRITE_REG(&adapter->hw, VET, ETHERNET_IEEE_VLAN_TYPE);
       
   858 
       
   859 	e1000_reset_adaptive(&adapter->hw);
       
   860 	e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
       
   861 
       
   862 	if (!adapter->smart_power_down &&
       
   863 	    (adapter->hw.mac_type == e1000_82571 ||
       
   864 	     adapter->hw.mac_type == e1000_82572)) {
       
   865 		u16 phy_data = 0;
       
   866 		/* speed up time to link by disabling smart power down, ignore
       
   867 		 * the return value of this function because there is nothing
       
   868 		 * different we would do if it failed */
       
   869 		e1000_read_phy_reg(&adapter->hw, IGP02E1000_PHY_POWER_MGMT,
       
   870 		                   &phy_data);
       
   871 		phy_data &= ~IGP02E1000_PM_SPD;
       
   872 		e1000_write_phy_reg(&adapter->hw, IGP02E1000_PHY_POWER_MGMT,
       
   873 		                    phy_data);
       
   874 	}
       
   875 
       
   876 	e1000_release_manageability(adapter);
       
   877 }
       
   878 
       
   879 /**
       
   880  *  Dump the eeprom for users having checksum issues
       
   881  **/
       
   882 static void e1000_dump_eeprom(struct e1000_adapter *adapter)
       
   883 {
       
   884 	struct net_device *netdev = adapter->netdev;
       
   885 	struct ethtool_eeprom eeprom;
       
   886 	const struct ethtool_ops *ops = netdev->ethtool_ops;
       
   887 	u8 *data;
       
   888 	int i;
       
   889 	u16 csum_old, csum_new = 0;
       
   890 
       
   891 	eeprom.len = ops->get_eeprom_len(netdev);
       
   892 	eeprom.offset = 0;
       
   893 
       
   894 	data = kmalloc(eeprom.len, GFP_KERNEL);
       
   895 	if (!data) {
       
   896 		printk(KERN_ERR "Unable to allocate memory to dump EEPROM"
       
   897 		       " data\n");
       
   898 		return;
       
   899 	}
       
   900 
       
   901 	ops->get_eeprom(netdev, &eeprom, data);
       
   902 
       
   903 	csum_old = (data[EEPROM_CHECKSUM_REG * 2]) +
       
   904 		   (data[EEPROM_CHECKSUM_REG * 2 + 1] << 8);
       
   905 	for (i = 0; i < EEPROM_CHECKSUM_REG * 2; i += 2)
       
   906 		csum_new += data[i] + (data[i + 1] << 8);
       
   907 	csum_new = EEPROM_SUM - csum_new;
       
   908 
       
   909 	printk(KERN_ERR "/*********************/\n");
       
   910 	printk(KERN_ERR "Current EEPROM Checksum : 0x%04x\n", csum_old);
       
   911 	printk(KERN_ERR "Calculated              : 0x%04x\n", csum_new);
       
   912 
       
   913 	printk(KERN_ERR "Offset    Values\n");
       
   914 	printk(KERN_ERR "========  ======\n");
       
   915 	print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, data, 128, 0);
       
   916 
       
   917 	printk(KERN_ERR "Include this output when contacting your support "
       
   918 	       "provider.\n");
       
   919 	printk(KERN_ERR "This is not a software error! Something bad "
       
   920 	       "happened to your hardware or\n");
       
   921 	printk(KERN_ERR "EEPROM image. Ignoring this "
       
   922 	       "problem could result in further problems,\n");
       
   923 	printk(KERN_ERR "possibly loss of data, corruption or system hangs!\n");
       
   924 	printk(KERN_ERR "The MAC Address will be reset to 00:00:00:00:00:00, "
       
   925 	       "which is invalid\n");
       
   926 	printk(KERN_ERR "and requires you to set the proper MAC "
       
   927 	       "address manually before continuing\n");
       
   928 	printk(KERN_ERR "to enable this network device.\n");
       
   929 	printk(KERN_ERR "Please inspect the EEPROM dump and report the issue "
       
   930 	       "to your hardware vendor\n");
       
   931 	printk(KERN_ERR "or Intel Customer Support: linux-nics@intel.com\n");
       
   932 	printk(KERN_ERR "/*********************/\n");
       
   933 
       
   934 	kfree(data);
       
   935 }
       
   936 
       
   937 /**
       
   938  * e1000_probe - Device Initialization Routine
       
   939  * @pdev: PCI device information struct
       
   940  * @ent: entry in e1000_pci_tbl
       
   941  *
       
   942  * Returns 0 on success, negative on failure
       
   943  *
       
   944  * e1000_probe initializes an adapter identified by a pci_dev structure.
       
   945  * The OS initialization, configuring of the adapter private structure,
       
   946  * and a hardware reset occur.
       
   947  **/
       
   948 
       
   949 static int __devinit
       
   950 e1000_probe(struct pci_dev *pdev,
       
   951             const struct pci_device_id *ent)
       
   952 {
       
   953 	struct net_device *netdev;
       
   954 	struct e1000_adapter *adapter;
       
   955 
       
   956 	static int cards_found = 0;
       
   957 	static int global_quad_port_a = 0; /* global ksp3 port a indication */
       
   958 	int i, err, pci_using_dac;
       
   959 	u16 eeprom_data = 0;
       
   960 	u16 eeprom_apme_mask = E1000_EEPROM_APME;
       
   961 	DECLARE_MAC_BUF(mac);
       
   962 
       
   963 	if ((err = pci_enable_device(pdev)))
       
   964 		return err;
       
   965 
       
   966 	if (!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK)) &&
       
   967 	    !(err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK))) {
       
   968 		pci_using_dac = 1;
       
   969 	} else {
       
   970 		if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK)) &&
       
   971 		    (err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK))) {
       
   972 			E1000_ERR("No usable DMA configuration, aborting\n");
       
   973 			goto err_dma;
       
   974 		}
       
   975 		pci_using_dac = 0;
       
   976 	}
       
   977 
       
   978 	if ((err = pci_request_regions(pdev, e1000_driver_name)))
       
   979 		goto err_pci_reg;
       
   980 
       
   981 	pci_set_master(pdev);
       
   982 
       
   983 	err = -ENOMEM;
       
   984 	netdev = alloc_etherdev(sizeof(struct e1000_adapter));
       
   985 	if (!netdev)
       
   986 		goto err_alloc_etherdev;
       
   987 
       
   988 	SET_NETDEV_DEV(netdev, &pdev->dev);
       
   989 
       
   990 	pci_set_drvdata(pdev, netdev);
       
   991 	adapter = netdev_priv(netdev);
       
   992 	adapter->netdev = netdev;
       
   993 	adapter->pdev = pdev;
       
   994 	adapter->hw.back = adapter;
       
   995 	adapter->msg_enable = (1 << debug) - 1;
       
   996 
       
   997 	err = -EIO;
       
   998 	adapter->hw.hw_addr = ioremap(pci_resource_start(pdev, BAR_0),
       
   999 				      pci_resource_len(pdev, BAR_0));
       
  1000 	if (!adapter->hw.hw_addr)
       
  1001 		goto err_ioremap;
       
  1002 
       
  1003 	for (i = BAR_1; i <= BAR_5; i++) {
       
  1004 		if (pci_resource_len(pdev, i) == 0)
       
  1005 			continue;
       
  1006 		if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
       
  1007 			adapter->hw.io_base = pci_resource_start(pdev, i);
       
  1008 			break;
       
  1009 		}
       
  1010 	}
       
  1011 
       
  1012 	netdev->open = &e1000_open;
       
  1013 	netdev->stop = &e1000_close;
       
  1014 	netdev->hard_start_xmit = &e1000_xmit_frame;
       
  1015 	netdev->get_stats = &e1000_get_stats;
       
  1016 	netdev->set_rx_mode = &e1000_set_rx_mode;
       
  1017 	netdev->set_mac_address = &e1000_set_mac;
       
  1018 	netdev->change_mtu = &e1000_change_mtu;
       
  1019 	netdev->do_ioctl = &e1000_ioctl;
       
  1020 	e1000_set_ethtool_ops(netdev);
       
  1021 	netdev->tx_timeout = &e1000_tx_timeout;
       
  1022 	netdev->watchdog_timeo = 5 * HZ;
       
  1023 #ifdef CONFIG_E1000_NAPI
       
  1024 	netif_napi_add(netdev, &adapter->napi, e1000_clean, 64);
       
  1025 #endif
       
  1026 	netdev->vlan_rx_register = e1000_vlan_rx_register;
       
  1027 	netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
       
  1028 	netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
       
  1029 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  1030 	netdev->poll_controller = e1000_netpoll;
       
  1031 #endif
       
  1032 	strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
       
  1033 
       
  1034 	adapter->bd_number = cards_found;
       
  1035 
       
  1036 	/* setup the private structure */
       
  1037 
       
  1038 	if ((err = e1000_sw_init(adapter)))
       
  1039 		goto err_sw_init;
       
  1040 
       
  1041 	err = -EIO;
       
  1042 	/* Flash BAR mapping must happen after e1000_sw_init
       
  1043 	 * because it depends on mac_type */
       
  1044 	if ((adapter->hw.mac_type == e1000_ich8lan) &&
       
  1045 	   (pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
       
  1046 		adapter->hw.flash_address =
       
  1047 			ioremap(pci_resource_start(pdev, 1),
       
  1048 				pci_resource_len(pdev, 1));
       
  1049 		if (!adapter->hw.flash_address)
       
  1050 			goto err_flashmap;
       
  1051 	}
       
  1052 
       
  1053 	if (e1000_check_phy_reset_block(&adapter->hw))
       
  1054 		DPRINTK(PROBE, INFO, "PHY reset is blocked due to SOL/IDER session.\n");
       
  1055 
       
  1056 	if (adapter->hw.mac_type >= e1000_82543) {
       
  1057 		netdev->features = NETIF_F_SG |
       
  1058 				   NETIF_F_HW_CSUM |
       
  1059 				   NETIF_F_HW_VLAN_TX |
       
  1060 				   NETIF_F_HW_VLAN_RX |
       
  1061 				   NETIF_F_HW_VLAN_FILTER;
       
  1062 		if (adapter->hw.mac_type == e1000_ich8lan)
       
  1063 			netdev->features &= ~NETIF_F_HW_VLAN_FILTER;
       
  1064 	}
       
  1065 
       
  1066 	if ((adapter->hw.mac_type >= e1000_82544) &&
       
  1067 	   (adapter->hw.mac_type != e1000_82547))
       
  1068 		netdev->features |= NETIF_F_TSO;
       
  1069 
       
  1070 	if (adapter->hw.mac_type > e1000_82547_rev_2)
       
  1071 		netdev->features |= NETIF_F_TSO6;
       
  1072 	if (pci_using_dac)
       
  1073 		netdev->features |= NETIF_F_HIGHDMA;
       
  1074 
       
  1075 	netdev->features |= NETIF_F_LLTX;
       
  1076 
       
  1077 	adapter->en_mng_pt = e1000_enable_mng_pass_thru(&adapter->hw);
       
  1078 
       
  1079 	/* initialize eeprom parameters */
       
  1080 	if (e1000_init_eeprom_params(&adapter->hw)) {
       
  1081 		E1000_ERR("EEPROM initialization failed\n");
       
  1082 		goto err_eeprom;
       
  1083 	}
       
  1084 
       
  1085 	/* before reading the EEPROM, reset the controller to
       
  1086 	 * put the device in a known good starting state */
       
  1087 
       
  1088 	e1000_reset_hw(&adapter->hw);
       
  1089 
       
  1090 	/* make sure the EEPROM is good */
       
  1091 	if (e1000_validate_eeprom_checksum(&adapter->hw) < 0) {
       
  1092 		DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
       
  1093 		e1000_dump_eeprom(adapter);
       
  1094 		/*
       
  1095 		 * set MAC address to all zeroes to invalidate and temporary
       
  1096 		 * disable this device for the user. This blocks regular
       
  1097 		 * traffic while still permitting ethtool ioctls from reaching
       
  1098 		 * the hardware as well as allowing the user to run the
       
  1099 		 * interface after manually setting a hw addr using
       
  1100 		 * `ip set address`
       
  1101 		 */
       
  1102 		memset(adapter->hw.mac_addr, 0, netdev->addr_len);
       
  1103 	} else {
       
  1104 		/* copy the MAC address out of the EEPROM */
       
  1105 		if (e1000_read_mac_addr(&adapter->hw))
       
  1106 			DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
       
  1107 	}
       
  1108 	/* don't block initalization here due to bad MAC address */
       
  1109 	memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
       
  1110 	memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
       
  1111 
       
  1112 	if (!is_valid_ether_addr(netdev->perm_addr))
       
  1113 		DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
       
  1114 
       
  1115 	e1000_get_bus_info(&adapter->hw);
       
  1116 
       
  1117 	init_timer(&adapter->tx_fifo_stall_timer);
       
  1118 	adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
       
  1119 	adapter->tx_fifo_stall_timer.data = (unsigned long) adapter;
       
  1120 
       
  1121 	init_timer(&adapter->watchdog_timer);
       
  1122 	adapter->watchdog_timer.function = &e1000_watchdog;
       
  1123 	adapter->watchdog_timer.data = (unsigned long) adapter;
       
  1124 
       
  1125 	init_timer(&adapter->phy_info_timer);
       
  1126 	adapter->phy_info_timer.function = &e1000_update_phy_info;
       
  1127 	adapter->phy_info_timer.data = (unsigned long) adapter;
       
  1128 
       
  1129 	INIT_WORK(&adapter->reset_task, e1000_reset_task);
       
  1130 
       
  1131 	e1000_check_options(adapter);
       
  1132 
       
  1133 	/* Initial Wake on LAN setting
       
  1134 	 * If APM wake is enabled in the EEPROM,
       
  1135 	 * enable the ACPI Magic Packet filter
       
  1136 	 */
       
  1137 
       
  1138 	switch (adapter->hw.mac_type) {
       
  1139 	case e1000_82542_rev2_0:
       
  1140 	case e1000_82542_rev2_1:
       
  1141 	case e1000_82543:
       
  1142 		break;
       
  1143 	case e1000_82544:
       
  1144 		e1000_read_eeprom(&adapter->hw,
       
  1145 			EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
       
  1146 		eeprom_apme_mask = E1000_EEPROM_82544_APM;
       
  1147 		break;
       
  1148 	case e1000_ich8lan:
       
  1149 		e1000_read_eeprom(&adapter->hw,
       
  1150 			EEPROM_INIT_CONTROL1_REG, 1, &eeprom_data);
       
  1151 		eeprom_apme_mask = E1000_EEPROM_ICH8_APME;
       
  1152 		break;
       
  1153 	case e1000_82546:
       
  1154 	case e1000_82546_rev_3:
       
  1155 	case e1000_82571:
       
  1156 	case e1000_80003es2lan:
       
  1157 		if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1){
       
  1158 			e1000_read_eeprom(&adapter->hw,
       
  1159 				EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
       
  1160 			break;
       
  1161 		}
       
  1162 		/* Fall Through */
       
  1163 	default:
       
  1164 		e1000_read_eeprom(&adapter->hw,
       
  1165 			EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
       
  1166 		break;
       
  1167 	}
       
  1168 	if (eeprom_data & eeprom_apme_mask)
       
  1169 		adapter->eeprom_wol |= E1000_WUFC_MAG;
       
  1170 
       
  1171 	/* now that we have the eeprom settings, apply the special cases
       
  1172 	 * where the eeprom may be wrong or the board simply won't support
       
  1173 	 * wake on lan on a particular port */
       
  1174 	switch (pdev->device) {
       
  1175 	case E1000_DEV_ID_82546GB_PCIE:
       
  1176 		adapter->eeprom_wol = 0;
       
  1177 		break;
       
  1178 	case E1000_DEV_ID_82546EB_FIBER:
       
  1179 	case E1000_DEV_ID_82546GB_FIBER:
       
  1180 	case E1000_DEV_ID_82571EB_FIBER:
       
  1181 		/* Wake events only supported on port A for dual fiber
       
  1182 		 * regardless of eeprom setting */
       
  1183 		if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1)
       
  1184 			adapter->eeprom_wol = 0;
       
  1185 		break;
       
  1186 	case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
       
  1187 	case E1000_DEV_ID_82571EB_QUAD_COPPER:
       
  1188 	case E1000_DEV_ID_82571EB_QUAD_FIBER:
       
  1189 	case E1000_DEV_ID_82571EB_QUAD_COPPER_LOWPROFILE:
       
  1190 	case E1000_DEV_ID_82571PT_QUAD_COPPER:
       
  1191 		/* if quad port adapter, disable WoL on all but port A */
       
  1192 		if (global_quad_port_a != 0)
       
  1193 			adapter->eeprom_wol = 0;
       
  1194 		else
       
  1195 			adapter->quad_port_a = 1;
       
  1196 		/* Reset for multiple quad port adapters */
       
  1197 		if (++global_quad_port_a == 4)
       
  1198 			global_quad_port_a = 0;
       
  1199 		break;
       
  1200 	}
       
  1201 
       
  1202 	/* initialize the wol settings based on the eeprom settings */
       
  1203 	adapter->wol = adapter->eeprom_wol;
       
  1204 
       
  1205 	/* print bus type/speed/width info */
       
  1206 	{
       
  1207 	struct e1000_hw *hw = &adapter->hw;
       
  1208 	DPRINTK(PROBE, INFO, "(PCI%s:%s:%s) ",
       
  1209 		((hw->bus_type == e1000_bus_type_pcix) ? "-X" :
       
  1210 		 (hw->bus_type == e1000_bus_type_pci_express ? " Express":"")),
       
  1211 		((hw->bus_speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
       
  1212 		 (hw->bus_speed == e1000_bus_speed_133) ? "133MHz" :
       
  1213 		 (hw->bus_speed == e1000_bus_speed_120) ? "120MHz" :
       
  1214 		 (hw->bus_speed == e1000_bus_speed_100) ? "100MHz" :
       
  1215 		 (hw->bus_speed == e1000_bus_speed_66) ? "66MHz" : "33MHz"),
       
  1216 		((hw->bus_width == e1000_bus_width_64) ? "64-bit" :
       
  1217 		 (hw->bus_width == e1000_bus_width_pciex_4) ? "Width x4" :
       
  1218 		 (hw->bus_width == e1000_bus_width_pciex_1) ? "Width x1" :
       
  1219 		 "32-bit"));
       
  1220 	}
       
  1221 
       
  1222 	printk("%s\n", print_mac(mac, netdev->dev_addr));
       
  1223 
       
  1224 	if (adapter->hw.bus_type == e1000_bus_type_pci_express) {
       
  1225 		DPRINTK(PROBE, WARNING, "This device (id %04x:%04x) will no "
       
  1226 			"longer be supported by this driver in the future.\n",
       
  1227 			pdev->vendor, pdev->device);
       
  1228 		DPRINTK(PROBE, WARNING, "please use the \"e1000e\" "
       
  1229 			"driver instead.\n");
       
  1230 	}
       
  1231 
       
  1232 	/* reset the hardware with the new settings */
       
  1233 	e1000_reset(adapter);
       
  1234 
       
  1235 	/* If the controller is 82573 and f/w is AMT, do not set
       
  1236 	 * DRV_LOAD until the interface is up.  For all other cases,
       
  1237 	 * let the f/w know that the h/w is now under the control
       
  1238 	 * of the driver. */
       
  1239 	if (adapter->hw.mac_type != e1000_82573 ||
       
  1240 	    !e1000_check_mng_mode(&adapter->hw))
       
  1241 		e1000_get_hw_control(adapter);
       
  1242 
       
  1243 	// offer device to EtherCAT master module
       
  1244 	adapter->ecdev = ecdev_offer(netdev, ec_poll, THIS_MODULE);
       
  1245 	if (adapter->ecdev) {
       
  1246 		if (ecdev_open(adapter->ecdev)) {
       
  1247 			ecdev_withdraw(adapter->ecdev);
       
  1248 			goto err_register;
       
  1249 		}
       
  1250 	} else {
       
  1251 		/* tell the stack to leave us alone until e1000_open() is called */
       
  1252 		netif_carrier_off(netdev);
       
  1253 		netif_stop_queue(netdev);
       
  1254 
       
  1255 		strcpy(netdev->name, "eth%d");
       
  1256 		if ((err = register_netdev(netdev)))
       
  1257 			goto err_register;
       
  1258 	}
       
  1259 
       
  1260 	DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
       
  1261 
       
  1262 	cards_found++;
       
  1263 	return 0;
       
  1264 
       
  1265 err_register:
       
  1266 	e1000_release_hw_control(adapter);
       
  1267 err_eeprom:
       
  1268 	if (!e1000_check_phy_reset_block(&adapter->hw))
       
  1269 		e1000_phy_hw_reset(&adapter->hw);
       
  1270 
       
  1271 	if (adapter->hw.flash_address)
       
  1272 		iounmap(adapter->hw.flash_address);
       
  1273 err_flashmap:
       
  1274 #ifdef CONFIG_E1000_NAPI
       
  1275 	for (i = 0; i < adapter->num_rx_queues; i++)
       
  1276 		dev_put(&adapter->polling_netdev[i]);
       
  1277 #endif
       
  1278 
       
  1279 	kfree(adapter->tx_ring);
       
  1280 	kfree(adapter->rx_ring);
       
  1281 #ifdef CONFIG_E1000_NAPI
       
  1282 	kfree(adapter->polling_netdev);
       
  1283 #endif
       
  1284 err_sw_init:
       
  1285 	iounmap(adapter->hw.hw_addr);
       
  1286 err_ioremap:
       
  1287 	free_netdev(netdev);
       
  1288 err_alloc_etherdev:
       
  1289 	pci_release_regions(pdev);
       
  1290 err_pci_reg:
       
  1291 err_dma:
       
  1292 	pci_disable_device(pdev);
       
  1293 	return err;
       
  1294 }
       
  1295 
       
  1296 /**
       
  1297  * e1000_remove - Device Removal Routine
       
  1298  * @pdev: PCI device information struct
       
  1299  *
       
  1300  * e1000_remove is called by the PCI subsystem to alert the driver
       
  1301  * that it should release a PCI device.  The could be caused by a
       
  1302  * Hot-Plug event, or because the driver is going to be removed from
       
  1303  * memory.
       
  1304  **/
       
  1305 
       
  1306 static void __devexit
       
  1307 e1000_remove(struct pci_dev *pdev)
       
  1308 {
       
  1309 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  1310 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  1311 #ifdef CONFIG_E1000_NAPI
       
  1312 	int i;
       
  1313 #endif
       
  1314 
       
  1315 	cancel_work_sync(&adapter->reset_task);
       
  1316 
       
  1317 	e1000_release_manageability(adapter);
       
  1318 
       
  1319 	/* Release control of h/w to f/w.  If f/w is AMT enabled, this
       
  1320 	 * would have already happened in close and is redundant. */
       
  1321 	e1000_release_hw_control(adapter);
       
  1322 
       
  1323 #ifdef CONFIG_E1000_NAPI
       
  1324 	for (i = 0; i < adapter->num_rx_queues; i++)
       
  1325 		dev_put(&adapter->polling_netdev[i]);
       
  1326 #endif
       
  1327 
       
  1328 	if (adapter->ecdev) {
       
  1329 		ecdev_close(adapter->ecdev);
       
  1330 		ecdev_withdraw(adapter->ecdev);
       
  1331 	} else {
       
  1332 		unregister_netdev(netdev);
       
  1333 	}
       
  1334 
       
  1335 	if (!e1000_check_phy_reset_block(&adapter->hw))
       
  1336 		e1000_phy_hw_reset(&adapter->hw);
       
  1337 
       
  1338 	kfree(adapter->tx_ring);
       
  1339 	kfree(adapter->rx_ring);
       
  1340 #ifdef CONFIG_E1000_NAPI
       
  1341 	kfree(adapter->polling_netdev);
       
  1342 #endif
       
  1343 
       
  1344 	iounmap(adapter->hw.hw_addr);
       
  1345 	if (adapter->hw.flash_address)
       
  1346 		iounmap(adapter->hw.flash_address);
       
  1347 	pci_release_regions(pdev);
       
  1348 
       
  1349 	free_netdev(netdev);
       
  1350 
       
  1351 	pci_disable_device(pdev);
       
  1352 }
       
  1353 
       
  1354 /**
       
  1355  * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
       
  1356  * @adapter: board private structure to initialize
       
  1357  *
       
  1358  * e1000_sw_init initializes the Adapter private data structure.
       
  1359  * Fields are initialized based on PCI device information and
       
  1360  * OS network device settings (MTU size).
       
  1361  **/
       
  1362 
       
  1363 static int __devinit
       
  1364 e1000_sw_init(struct e1000_adapter *adapter)
       
  1365 {
       
  1366 	struct e1000_hw *hw = &adapter->hw;
       
  1367 	struct net_device *netdev = adapter->netdev;
       
  1368 	struct pci_dev *pdev = adapter->pdev;
       
  1369 #ifdef CONFIG_E1000_NAPI
       
  1370 	int i;
       
  1371 #endif
       
  1372 
       
  1373 	/* PCI config space info */
       
  1374 
       
  1375 	hw->vendor_id = pdev->vendor;
       
  1376 	hw->device_id = pdev->device;
       
  1377 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
       
  1378 	hw->subsystem_id = pdev->subsystem_device;
       
  1379 	hw->revision_id = pdev->revision;
       
  1380 
       
  1381 	pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
       
  1382 
       
  1383 	adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
       
  1384 	adapter->rx_ps_bsize0 = E1000_RXBUFFER_128;
       
  1385 	hw->max_frame_size = netdev->mtu +
       
  1386 			     ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
       
  1387 	hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
       
  1388 
       
  1389 	/* identify the MAC */
       
  1390 
       
  1391 	if (e1000_set_mac_type(hw)) {
       
  1392 		DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
       
  1393 		return -EIO;
       
  1394 	}
       
  1395 
       
  1396 	switch (hw->mac_type) {
       
  1397 	default:
       
  1398 		break;
       
  1399 	case e1000_82541:
       
  1400 	case e1000_82547:
       
  1401 	case e1000_82541_rev_2:
       
  1402 	case e1000_82547_rev_2:
       
  1403 		hw->phy_init_script = 1;
       
  1404 		break;
       
  1405 	}
       
  1406 
       
  1407 	e1000_set_media_type(hw);
       
  1408 
       
  1409 	hw->wait_autoneg_complete = false;
       
  1410 	hw->tbi_compatibility_en = true;
       
  1411 	hw->adaptive_ifs = true;
       
  1412 
       
  1413 	/* Copper options */
       
  1414 
       
  1415 	if (hw->media_type == e1000_media_type_copper) {
       
  1416 		hw->mdix = AUTO_ALL_MODES;
       
  1417 		hw->disable_polarity_correction = false;
       
  1418 		hw->master_slave = E1000_MASTER_SLAVE;
       
  1419 	}
       
  1420 
       
  1421 	adapter->num_tx_queues = 1;
       
  1422 	adapter->num_rx_queues = 1;
       
  1423 
       
  1424 	if (e1000_alloc_queues(adapter)) {
       
  1425 		DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
       
  1426 		return -ENOMEM;
       
  1427 	}
       
  1428 
       
  1429 #ifdef CONFIG_E1000_NAPI
       
  1430 	for (i = 0; i < adapter->num_rx_queues; i++) {
       
  1431 		adapter->polling_netdev[i].priv = adapter;
       
  1432 		dev_hold(&adapter->polling_netdev[i]);
       
  1433 		set_bit(__LINK_STATE_START, &adapter->polling_netdev[i].state);
       
  1434 	}
       
  1435 	spin_lock_init(&adapter->tx_queue_lock);
       
  1436 #endif
       
  1437 
       
  1438 	/* Explicitly disable IRQ since the NIC can be in any state. */
       
  1439 	e1000_irq_disable(adapter);
       
  1440 
       
  1441 	spin_lock_init(&adapter->stats_lock);
       
  1442 
       
  1443 	set_bit(__E1000_DOWN, &adapter->flags);
       
  1444 
       
  1445 	return 0;
       
  1446 }
       
  1447 
       
  1448 /**
       
  1449  * e1000_alloc_queues - Allocate memory for all rings
       
  1450  * @adapter: board private structure to initialize
       
  1451  *
       
  1452  * We allocate one ring per queue at run-time since we don't know the
       
  1453  * number of queues at compile-time.  The polling_netdev array is
       
  1454  * intended for Multiqueue, but should work fine with a single queue.
       
  1455  **/
       
  1456 
       
  1457 static int __devinit
       
  1458 e1000_alloc_queues(struct e1000_adapter *adapter)
       
  1459 {
       
  1460 	adapter->tx_ring = kcalloc(adapter->num_tx_queues,
       
  1461 	                           sizeof(struct e1000_tx_ring), GFP_KERNEL);
       
  1462 	if (!adapter->tx_ring)
       
  1463 		return -ENOMEM;
       
  1464 
       
  1465 	adapter->rx_ring = kcalloc(adapter->num_rx_queues,
       
  1466 	                           sizeof(struct e1000_rx_ring), GFP_KERNEL);
       
  1467 	if (!adapter->rx_ring) {
       
  1468 		kfree(adapter->tx_ring);
       
  1469 		return -ENOMEM;
       
  1470 	}
       
  1471 
       
  1472 #ifdef CONFIG_E1000_NAPI
       
  1473 	adapter->polling_netdev = kcalloc(adapter->num_rx_queues,
       
  1474 	                                  sizeof(struct net_device),
       
  1475 	                                  GFP_KERNEL);
       
  1476 	if (!adapter->polling_netdev) {
       
  1477 		kfree(adapter->tx_ring);
       
  1478 		kfree(adapter->rx_ring);
       
  1479 		return -ENOMEM;
       
  1480 	}
       
  1481 #endif
       
  1482 
       
  1483 	return E1000_SUCCESS;
       
  1484 }
       
  1485 
       
  1486 /**
       
  1487  * e1000_open - Called when a network interface is made active
       
  1488  * @netdev: network interface device structure
       
  1489  *
       
  1490  * Returns 0 on success, negative value on failure
       
  1491  *
       
  1492  * The open entry point is called when a network interface is made
       
  1493  * active by the system (IFF_UP).  At this point all resources needed
       
  1494  * for transmit and receive operations are allocated, the interrupt
       
  1495  * handler is registered with the OS, the watchdog timer is started,
       
  1496  * and the stack is notified that the interface is ready.
       
  1497  **/
       
  1498 
       
  1499 static int
       
  1500 e1000_open(struct net_device *netdev)
       
  1501 {
       
  1502 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  1503 	int err;
       
  1504 
       
  1505 	/* disallow open during test */
       
  1506 	if (test_bit(__E1000_TESTING, &adapter->flags))
       
  1507 		return -EBUSY;
       
  1508 
       
  1509 	/* allocate transmit descriptors */
       
  1510 	err = e1000_setup_all_tx_resources(adapter);
       
  1511 	if (err)
       
  1512 		goto err_setup_tx;
       
  1513 
       
  1514 	/* allocate receive descriptors */
       
  1515 	err = e1000_setup_all_rx_resources(adapter);
       
  1516 	if (err)
       
  1517 		goto err_setup_rx;
       
  1518 
       
  1519 	e1000_power_up_phy(adapter);
       
  1520 
       
  1521 	adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
       
  1522 	if ((adapter->hw.mng_cookie.status &
       
  1523 			  E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
       
  1524 		e1000_update_mng_vlan(adapter);
       
  1525 	}
       
  1526 
       
  1527 	/* If AMT is enabled, let the firmware know that the network
       
  1528 	 * interface is now open */
       
  1529 	if (adapter->hw.mac_type == e1000_82573 &&
       
  1530 	    e1000_check_mng_mode(&adapter->hw))
       
  1531 		e1000_get_hw_control(adapter);
       
  1532 
       
  1533 	/* before we allocate an interrupt, we must be ready to handle it.
       
  1534 	 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
       
  1535 	 * as soon as we call pci_request_irq, so we have to setup our
       
  1536 	 * clean_rx handler before we do so.  */
       
  1537 	e1000_configure(adapter);
       
  1538 
       
  1539 	err = e1000_request_irq(adapter);
       
  1540 	if (err)
       
  1541 		goto err_req_irq;
       
  1542 
       
  1543 	/* From here on the code is the same as e1000_up() */
       
  1544 	clear_bit(__E1000_DOWN, &adapter->flags);
       
  1545 
       
  1546 #ifdef CONFIG_E1000_NAPI
       
  1547 	napi_enable(&adapter->napi);
       
  1548 #endif
       
  1549 
       
  1550 	e1000_irq_enable(adapter);
       
  1551 
       
  1552 	/* fire a link status change interrupt to start the watchdog */
       
  1553 	E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_LSC);
       
  1554 
       
  1555 	return E1000_SUCCESS;
       
  1556 
       
  1557 err_req_irq:
       
  1558 	e1000_release_hw_control(adapter);
       
  1559 	e1000_power_down_phy(adapter);
       
  1560 	e1000_free_all_rx_resources(adapter);
       
  1561 err_setup_rx:
       
  1562 	e1000_free_all_tx_resources(adapter);
       
  1563 err_setup_tx:
       
  1564 	e1000_reset(adapter);
       
  1565 
       
  1566 	return err;
       
  1567 }
       
  1568 
       
  1569 /**
       
  1570  * e1000_close - Disables a network interface
       
  1571  * @netdev: network interface device structure
       
  1572  *
       
  1573  * Returns 0, this is not allowed to fail
       
  1574  *
       
  1575  * The close entry point is called when an interface is de-activated
       
  1576  * by the OS.  The hardware is still under the drivers control, but
       
  1577  * needs to be disabled.  A global MAC reset is issued to stop the
       
  1578  * hardware, and all transmit and receive resources are freed.
       
  1579  **/
       
  1580 
       
  1581 static int
       
  1582 e1000_close(struct net_device *netdev)
       
  1583 {
       
  1584 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  1585 
       
  1586 	WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
       
  1587 	e1000_down(adapter);
       
  1588 	e1000_power_down_phy(adapter);
       
  1589 	e1000_free_irq(adapter);
       
  1590 
       
  1591 	e1000_free_all_tx_resources(adapter);
       
  1592 	e1000_free_all_rx_resources(adapter);
       
  1593 
       
  1594 	/* kill manageability vlan ID if supported, but not if a vlan with
       
  1595 	 * the same ID is registered on the host OS (let 8021q kill it) */
       
  1596 	if ((adapter->hw.mng_cookie.status &
       
  1597 			  E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
       
  1598 	     !(adapter->vlgrp &&
       
  1599 	       vlan_group_get_device(adapter->vlgrp, adapter->mng_vlan_id))) {
       
  1600 		e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
       
  1601 	}
       
  1602 
       
  1603 	/* If AMT is enabled, let the firmware know that the network
       
  1604 	 * interface is now closed */
       
  1605 	if (adapter->hw.mac_type == e1000_82573 &&
       
  1606 	    e1000_check_mng_mode(&adapter->hw))
       
  1607 		e1000_release_hw_control(adapter);
       
  1608 
       
  1609 	return 0;
       
  1610 }
       
  1611 
       
  1612 /**
       
  1613  * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
       
  1614  * @adapter: address of board private structure
       
  1615  * @start: address of beginning of memory
       
  1616  * @len: length of memory
       
  1617  **/
       
  1618 static bool
       
  1619 e1000_check_64k_bound(struct e1000_adapter *adapter,
       
  1620 		      void *start, unsigned long len)
       
  1621 {
       
  1622 	unsigned long begin = (unsigned long) start;
       
  1623 	unsigned long end = begin + len;
       
  1624 
       
  1625 	/* First rev 82545 and 82546 need to not allow any memory
       
  1626 	 * write location to cross 64k boundary due to errata 23 */
       
  1627 	if (adapter->hw.mac_type == e1000_82545 ||
       
  1628 	    adapter->hw.mac_type == e1000_82546) {
       
  1629 		return ((begin ^ (end - 1)) >> 16) != 0 ? false : true;
       
  1630 	}
       
  1631 
       
  1632 	return true;
       
  1633 }
       
  1634 
       
  1635 /**
       
  1636  * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
       
  1637  * @adapter: board private structure
       
  1638  * @txdr:    tx descriptor ring (for a specific queue) to setup
       
  1639  *
       
  1640  * Return 0 on success, negative on failure
       
  1641  **/
       
  1642 
       
  1643 static int
       
  1644 e1000_setup_tx_resources(struct e1000_adapter *adapter,
       
  1645                          struct e1000_tx_ring *txdr)
       
  1646 {
       
  1647 	struct pci_dev *pdev = adapter->pdev;
       
  1648 	int size;
       
  1649 
       
  1650 	size = sizeof(struct e1000_buffer) * txdr->count;
       
  1651 	txdr->buffer_info = vmalloc(size);
       
  1652 	if (!txdr->buffer_info) {
       
  1653 		DPRINTK(PROBE, ERR,
       
  1654 		"Unable to allocate memory for the transmit descriptor ring\n");
       
  1655 		return -ENOMEM;
       
  1656 	}
       
  1657 	memset(txdr->buffer_info, 0, size);
       
  1658 
       
  1659 	/* round up to nearest 4K */
       
  1660 
       
  1661 	txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
       
  1662 	txdr->size = ALIGN(txdr->size, 4096);
       
  1663 
       
  1664 	txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
       
  1665 	if (!txdr->desc) {
       
  1666 setup_tx_desc_die:
       
  1667 		vfree(txdr->buffer_info);
       
  1668 		DPRINTK(PROBE, ERR,
       
  1669 		"Unable to allocate memory for the transmit descriptor ring\n");
       
  1670 		return -ENOMEM;
       
  1671 	}
       
  1672 
       
  1673 	/* Fix for errata 23, can't cross 64kB boundary */
       
  1674 	if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
       
  1675 		void *olddesc = txdr->desc;
       
  1676 		dma_addr_t olddma = txdr->dma;
       
  1677 		DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "
       
  1678 				     "at %p\n", txdr->size, txdr->desc);
       
  1679 		/* Try again, without freeing the previous */
       
  1680 		txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
       
  1681 		/* Failed allocation, critical failure */
       
  1682 		if (!txdr->desc) {
       
  1683 			pci_free_consistent(pdev, txdr->size, olddesc, olddma);
       
  1684 			goto setup_tx_desc_die;
       
  1685 		}
       
  1686 
       
  1687 		if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
       
  1688 			/* give up */
       
  1689 			pci_free_consistent(pdev, txdr->size, txdr->desc,
       
  1690 					    txdr->dma);
       
  1691 			pci_free_consistent(pdev, txdr->size, olddesc, olddma);
       
  1692 			DPRINTK(PROBE, ERR,
       
  1693 				"Unable to allocate aligned memory "
       
  1694 				"for the transmit descriptor ring\n");
       
  1695 			vfree(txdr->buffer_info);
       
  1696 			return -ENOMEM;
       
  1697 		} else {
       
  1698 			/* Free old allocation, new allocation was successful */
       
  1699 			pci_free_consistent(pdev, txdr->size, olddesc, olddma);
       
  1700 		}
       
  1701 	}
       
  1702 	memset(txdr->desc, 0, txdr->size);
       
  1703 
       
  1704 	txdr->next_to_use = 0;
       
  1705 	txdr->next_to_clean = 0;
       
  1706 	spin_lock_init(&txdr->tx_lock);
       
  1707 
       
  1708 	return 0;
       
  1709 }
       
  1710 
       
  1711 /**
       
  1712  * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
       
  1713  * 				  (Descriptors) for all queues
       
  1714  * @adapter: board private structure
       
  1715  *
       
  1716  * Return 0 on success, negative on failure
       
  1717  **/
       
  1718 
       
  1719 int
       
  1720 e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
       
  1721 {
       
  1722 	int i, err = 0;
       
  1723 
       
  1724 	for (i = 0; i < adapter->num_tx_queues; i++) {
       
  1725 		err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
       
  1726 		if (err) {
       
  1727 			DPRINTK(PROBE, ERR,
       
  1728 				"Allocation for Tx Queue %u failed\n", i);
       
  1729 			for (i-- ; i >= 0; i--)
       
  1730 				e1000_free_tx_resources(adapter,
       
  1731 							&adapter->tx_ring[i]);
       
  1732 			break;
       
  1733 		}
       
  1734 	}
       
  1735 
       
  1736 	return err;
       
  1737 }
       
  1738 
       
  1739 /**
       
  1740  * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
       
  1741  * @adapter: board private structure
       
  1742  *
       
  1743  * Configure the Tx unit of the MAC after a reset.
       
  1744  **/
       
  1745 
       
  1746 static void
       
  1747 e1000_configure_tx(struct e1000_adapter *adapter)
       
  1748 {
       
  1749 	u64 tdba;
       
  1750 	struct e1000_hw *hw = &adapter->hw;
       
  1751 	u32 tdlen, tctl, tipg, tarc;
       
  1752 	u32 ipgr1, ipgr2;
       
  1753 
       
  1754 	/* Setup the HW Tx Head and Tail descriptor pointers */
       
  1755 
       
  1756 	switch (adapter->num_tx_queues) {
       
  1757 	case 1:
       
  1758 	default:
       
  1759 		tdba = adapter->tx_ring[0].dma;
       
  1760 		tdlen = adapter->tx_ring[0].count *
       
  1761 			sizeof(struct e1000_tx_desc);
       
  1762 		E1000_WRITE_REG(hw, TDLEN, tdlen);
       
  1763 		E1000_WRITE_REG(hw, TDBAH, (tdba >> 32));
       
  1764 		E1000_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
       
  1765 		E1000_WRITE_REG(hw, TDT, 0);
       
  1766 		E1000_WRITE_REG(hw, TDH, 0);
       
  1767 		adapter->tx_ring[0].tdh = ((hw->mac_type >= e1000_82543) ? E1000_TDH : E1000_82542_TDH);
       
  1768 		adapter->tx_ring[0].tdt = ((hw->mac_type >= e1000_82543) ? E1000_TDT : E1000_82542_TDT);
       
  1769 		break;
       
  1770 	}
       
  1771 
       
  1772 	/* Set the default values for the Tx Inter Packet Gap timer */
       
  1773 	if (adapter->hw.mac_type <= e1000_82547_rev_2 &&
       
  1774 	    (hw->media_type == e1000_media_type_fiber ||
       
  1775 	     hw->media_type == e1000_media_type_internal_serdes))
       
  1776 		tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
       
  1777 	else
       
  1778 		tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
       
  1779 
       
  1780 	switch (hw->mac_type) {
       
  1781 	case e1000_82542_rev2_0:
       
  1782 	case e1000_82542_rev2_1:
       
  1783 		tipg = DEFAULT_82542_TIPG_IPGT;
       
  1784 		ipgr1 = DEFAULT_82542_TIPG_IPGR1;
       
  1785 		ipgr2 = DEFAULT_82542_TIPG_IPGR2;
       
  1786 		break;
       
  1787 	case e1000_80003es2lan:
       
  1788 		ipgr1 = DEFAULT_82543_TIPG_IPGR1;
       
  1789 		ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
       
  1790 		break;
       
  1791 	default:
       
  1792 		ipgr1 = DEFAULT_82543_TIPG_IPGR1;
       
  1793 		ipgr2 = DEFAULT_82543_TIPG_IPGR2;
       
  1794 		break;
       
  1795 	}
       
  1796 	tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
       
  1797 	tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
       
  1798 	E1000_WRITE_REG(hw, TIPG, tipg);
       
  1799 
       
  1800 	/* Set the Tx Interrupt Delay register */
       
  1801 
       
  1802 	E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
       
  1803 	if (hw->mac_type >= e1000_82540)
       
  1804 		E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);
       
  1805 
       
  1806 	/* Program the Transmit Control Register */
       
  1807 
       
  1808 	tctl = E1000_READ_REG(hw, TCTL);
       
  1809 	tctl &= ~E1000_TCTL_CT;
       
  1810 	tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
       
  1811 		(E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
       
  1812 
       
  1813 	if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
       
  1814 		tarc = E1000_READ_REG(hw, TARC0);
       
  1815 		/* set the speed mode bit, we'll clear it if we're not at
       
  1816 		 * gigabit link later */
       
  1817 		tarc |= (1 << 21);
       
  1818 		E1000_WRITE_REG(hw, TARC0, tarc);
       
  1819 	} else if (hw->mac_type == e1000_80003es2lan) {
       
  1820 		tarc = E1000_READ_REG(hw, TARC0);
       
  1821 		tarc |= 1;
       
  1822 		E1000_WRITE_REG(hw, TARC0, tarc);
       
  1823 		tarc = E1000_READ_REG(hw, TARC1);
       
  1824 		tarc |= 1;
       
  1825 		E1000_WRITE_REG(hw, TARC1, tarc);
       
  1826 	}
       
  1827 
       
  1828 	e1000_config_collision_dist(hw);
       
  1829 
       
  1830 	/* Setup Transmit Descriptor Settings for eop descriptor */
       
  1831 	adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
       
  1832 
       
  1833 	/* only set IDE if we are delaying interrupts using the timers */
       
  1834 	if (adapter->tx_int_delay)
       
  1835 		adapter->txd_cmd |= E1000_TXD_CMD_IDE;
       
  1836 
       
  1837 	if (hw->mac_type < e1000_82543)
       
  1838 		adapter->txd_cmd |= E1000_TXD_CMD_RPS;
       
  1839 	else
       
  1840 		adapter->txd_cmd |= E1000_TXD_CMD_RS;
       
  1841 
       
  1842 	/* Cache if we're 82544 running in PCI-X because we'll
       
  1843 	 * need this to apply a workaround later in the send path. */
       
  1844 	if (hw->mac_type == e1000_82544 &&
       
  1845 	    hw->bus_type == e1000_bus_type_pcix)
       
  1846 		adapter->pcix_82544 = 1;
       
  1847 
       
  1848 	E1000_WRITE_REG(hw, TCTL, tctl);
       
  1849 
       
  1850 }
       
  1851 
       
  1852 /**
       
  1853  * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
       
  1854  * @adapter: board private structure
       
  1855  * @rxdr:    rx descriptor ring (for a specific queue) to setup
       
  1856  *
       
  1857  * Returns 0 on success, negative on failure
       
  1858  **/
       
  1859 
       
  1860 static int
       
  1861 e1000_setup_rx_resources(struct e1000_adapter *adapter,
       
  1862                          struct e1000_rx_ring *rxdr)
       
  1863 {
       
  1864 	struct pci_dev *pdev = adapter->pdev;
       
  1865 	int size, desc_len;
       
  1866 
       
  1867 	size = sizeof(struct e1000_buffer) * rxdr->count;
       
  1868 	rxdr->buffer_info = vmalloc(size);
       
  1869 	if (!rxdr->buffer_info) {
       
  1870 		DPRINTK(PROBE, ERR,
       
  1871 		"Unable to allocate memory for the receive descriptor ring\n");
       
  1872 		return -ENOMEM;
       
  1873 	}
       
  1874 	memset(rxdr->buffer_info, 0, size);
       
  1875 
       
  1876 	rxdr->ps_page = kcalloc(rxdr->count, sizeof(struct e1000_ps_page),
       
  1877 	                        GFP_KERNEL);
       
  1878 	if (!rxdr->ps_page) {
       
  1879 		vfree(rxdr->buffer_info);
       
  1880 		DPRINTK(PROBE, ERR,
       
  1881 		"Unable to allocate memory for the receive descriptor ring\n");
       
  1882 		return -ENOMEM;
       
  1883 	}
       
  1884 
       
  1885 	rxdr->ps_page_dma = kcalloc(rxdr->count,
       
  1886 	                            sizeof(struct e1000_ps_page_dma),
       
  1887 	                            GFP_KERNEL);
       
  1888 	if (!rxdr->ps_page_dma) {
       
  1889 		vfree(rxdr->buffer_info);
       
  1890 		kfree(rxdr->ps_page);
       
  1891 		DPRINTK(PROBE, ERR,
       
  1892 		"Unable to allocate memory for the receive descriptor ring\n");
       
  1893 		return -ENOMEM;
       
  1894 	}
       
  1895 
       
  1896 	if (adapter->hw.mac_type <= e1000_82547_rev_2)
       
  1897 		desc_len = sizeof(struct e1000_rx_desc);
       
  1898 	else
       
  1899 		desc_len = sizeof(union e1000_rx_desc_packet_split);
       
  1900 
       
  1901 	/* Round up to nearest 4K */
       
  1902 
       
  1903 	rxdr->size = rxdr->count * desc_len;
       
  1904 	rxdr->size = ALIGN(rxdr->size, 4096);
       
  1905 
       
  1906 	rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
       
  1907 
       
  1908 	if (!rxdr->desc) {
       
  1909 		DPRINTK(PROBE, ERR,
       
  1910 		"Unable to allocate memory for the receive descriptor ring\n");
       
  1911 setup_rx_desc_die:
       
  1912 		vfree(rxdr->buffer_info);
       
  1913 		kfree(rxdr->ps_page);
       
  1914 		kfree(rxdr->ps_page_dma);
       
  1915 		return -ENOMEM;
       
  1916 	}
       
  1917 
       
  1918 	/* Fix for errata 23, can't cross 64kB boundary */
       
  1919 	if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
       
  1920 		void *olddesc = rxdr->desc;
       
  1921 		dma_addr_t olddma = rxdr->dma;
       
  1922 		DPRINTK(RX_ERR, ERR, "rxdr align check failed: %u bytes "
       
  1923 				     "at %p\n", rxdr->size, rxdr->desc);
       
  1924 		/* Try again, without freeing the previous */
       
  1925 		rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
       
  1926 		/* Failed allocation, critical failure */
       
  1927 		if (!rxdr->desc) {
       
  1928 			pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
       
  1929 			DPRINTK(PROBE, ERR,
       
  1930 				"Unable to allocate memory "
       
  1931 				"for the receive descriptor ring\n");
       
  1932 			goto setup_rx_desc_die;
       
  1933 		}
       
  1934 
       
  1935 		if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
       
  1936 			/* give up */
       
  1937 			pci_free_consistent(pdev, rxdr->size, rxdr->desc,
       
  1938 					    rxdr->dma);
       
  1939 			pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
       
  1940 			DPRINTK(PROBE, ERR,
       
  1941 				"Unable to allocate aligned memory "
       
  1942 				"for the receive descriptor ring\n");
       
  1943 			goto setup_rx_desc_die;
       
  1944 		} else {
       
  1945 			/* Free old allocation, new allocation was successful */
       
  1946 			pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
       
  1947 		}
       
  1948 	}
       
  1949 	memset(rxdr->desc, 0, rxdr->size);
       
  1950 
       
  1951 	rxdr->next_to_clean = 0;
       
  1952 	rxdr->next_to_use = 0;
       
  1953 
       
  1954 	return 0;
       
  1955 }
       
  1956 
       
  1957 /**
       
  1958  * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
       
  1959  * 				  (Descriptors) for all queues
       
  1960  * @adapter: board private structure
       
  1961  *
       
  1962  * Return 0 on success, negative on failure
       
  1963  **/
       
  1964 
       
  1965 int
       
  1966 e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
       
  1967 {
       
  1968 	int i, err = 0;
       
  1969 
       
  1970 	for (i = 0; i < adapter->num_rx_queues; i++) {
       
  1971 		err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
       
  1972 		if (err) {
       
  1973 			DPRINTK(PROBE, ERR,
       
  1974 				"Allocation for Rx Queue %u failed\n", i);
       
  1975 			for (i-- ; i >= 0; i--)
       
  1976 				e1000_free_rx_resources(adapter,
       
  1977 							&adapter->rx_ring[i]);
       
  1978 			break;
       
  1979 		}
       
  1980 	}
       
  1981 
       
  1982 	return err;
       
  1983 }
       
  1984 
       
  1985 /**
       
  1986  * e1000_setup_rctl - configure the receive control registers
       
  1987  * @adapter: Board private structure
       
  1988  **/
       
  1989 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
       
  1990 			(((S) & (PAGE_SIZE - 1)) ? 1 : 0))
       
  1991 static void
       
  1992 e1000_setup_rctl(struct e1000_adapter *adapter)
       
  1993 {
       
  1994 	u32 rctl, rfctl;
       
  1995 	u32 psrctl = 0;
       
  1996 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
       
  1997 	u32 pages = 0;
       
  1998 #endif
       
  1999 
       
  2000 	rctl = E1000_READ_REG(&adapter->hw, RCTL);
       
  2001 
       
  2002 	rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
       
  2003 
       
  2004 	rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
       
  2005 		E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
       
  2006 		(adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
       
  2007 
       
  2008 	if (adapter->hw.tbi_compatibility_on == 1)
       
  2009 		rctl |= E1000_RCTL_SBP;
       
  2010 	else
       
  2011 		rctl &= ~E1000_RCTL_SBP;
       
  2012 
       
  2013 	if (adapter->netdev->mtu <= ETH_DATA_LEN)
       
  2014 		rctl &= ~E1000_RCTL_LPE;
       
  2015 	else
       
  2016 		rctl |= E1000_RCTL_LPE;
       
  2017 
       
  2018 	/* Setup buffer sizes */
       
  2019 	rctl &= ~E1000_RCTL_SZ_4096;
       
  2020 	rctl |= E1000_RCTL_BSEX;
       
  2021 	switch (adapter->rx_buffer_len) {
       
  2022 		case E1000_RXBUFFER_256:
       
  2023 			rctl |= E1000_RCTL_SZ_256;
       
  2024 			rctl &= ~E1000_RCTL_BSEX;
       
  2025 			break;
       
  2026 		case E1000_RXBUFFER_512:
       
  2027 			rctl |= E1000_RCTL_SZ_512;
       
  2028 			rctl &= ~E1000_RCTL_BSEX;
       
  2029 			break;
       
  2030 		case E1000_RXBUFFER_1024:
       
  2031 			rctl |= E1000_RCTL_SZ_1024;
       
  2032 			rctl &= ~E1000_RCTL_BSEX;
       
  2033 			break;
       
  2034 		case E1000_RXBUFFER_2048:
       
  2035 		default:
       
  2036 			rctl |= E1000_RCTL_SZ_2048;
       
  2037 			rctl &= ~E1000_RCTL_BSEX;
       
  2038 			break;
       
  2039 		case E1000_RXBUFFER_4096:
       
  2040 			rctl |= E1000_RCTL_SZ_4096;
       
  2041 			break;
       
  2042 		case E1000_RXBUFFER_8192:
       
  2043 			rctl |= E1000_RCTL_SZ_8192;
       
  2044 			break;
       
  2045 		case E1000_RXBUFFER_16384:
       
  2046 			rctl |= E1000_RCTL_SZ_16384;
       
  2047 			break;
       
  2048 	}
       
  2049 
       
  2050 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
       
  2051 	/* 82571 and greater support packet-split where the protocol
       
  2052 	 * header is placed in skb->data and the packet data is
       
  2053 	 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
       
  2054 	 * In the case of a non-split, skb->data is linearly filled,
       
  2055 	 * followed by the page buffers.  Therefore, skb->data is
       
  2056 	 * sized to hold the largest protocol header.
       
  2057 	 */
       
  2058 	/* allocations using alloc_page take too long for regular MTU
       
  2059 	 * so only enable packet split for jumbo frames */
       
  2060 	pages = PAGE_USE_COUNT(adapter->netdev->mtu);
       
  2061 	if ((adapter->hw.mac_type >= e1000_82571) && (pages <= 3) &&
       
  2062 	    PAGE_SIZE <= 16384 && (rctl & E1000_RCTL_LPE))
       
  2063 		adapter->rx_ps_pages = pages;
       
  2064 	else
       
  2065 		adapter->rx_ps_pages = 0;
       
  2066 #endif
       
  2067 	if (adapter->rx_ps_pages) {
       
  2068 		/* Configure extra packet-split registers */
       
  2069 		rfctl = E1000_READ_REG(&adapter->hw, RFCTL);
       
  2070 		rfctl |= E1000_RFCTL_EXTEN;
       
  2071 		/* disable packet split support for IPv6 extension headers,
       
  2072 		 * because some malformed IPv6 headers can hang the RX */
       
  2073 		rfctl |= (E1000_RFCTL_IPV6_EX_DIS |
       
  2074 		          E1000_RFCTL_NEW_IPV6_EXT_DIS);
       
  2075 
       
  2076 		E1000_WRITE_REG(&adapter->hw, RFCTL, rfctl);
       
  2077 
       
  2078 		rctl |= E1000_RCTL_DTYP_PS;
       
  2079 
       
  2080 		psrctl |= adapter->rx_ps_bsize0 >>
       
  2081 			E1000_PSRCTL_BSIZE0_SHIFT;
       
  2082 
       
  2083 		switch (adapter->rx_ps_pages) {
       
  2084 		case 3:
       
  2085 			psrctl |= PAGE_SIZE <<
       
  2086 				E1000_PSRCTL_BSIZE3_SHIFT;
       
  2087 		case 2:
       
  2088 			psrctl |= PAGE_SIZE <<
       
  2089 				E1000_PSRCTL_BSIZE2_SHIFT;
       
  2090 		case 1:
       
  2091 			psrctl |= PAGE_SIZE >>
       
  2092 				E1000_PSRCTL_BSIZE1_SHIFT;
       
  2093 			break;
       
  2094 		}
       
  2095 
       
  2096 		E1000_WRITE_REG(&adapter->hw, PSRCTL, psrctl);
       
  2097 	}
       
  2098 
       
  2099 	E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
       
  2100 }
       
  2101 
       
  2102 /**
       
  2103  * e1000_configure_rx - Configure 8254x Receive Unit after Reset
       
  2104  * @adapter: board private structure
       
  2105  *
       
  2106  * Configure the Rx unit of the MAC after a reset.
       
  2107  **/
       
  2108 
       
  2109 static void
       
  2110 e1000_configure_rx(struct e1000_adapter *adapter)
       
  2111 {
       
  2112 	u64 rdba;
       
  2113 	struct e1000_hw *hw = &adapter->hw;
       
  2114 	u32 rdlen, rctl, rxcsum, ctrl_ext;
       
  2115 
       
  2116 	if (adapter->rx_ps_pages) {
       
  2117 		/* this is a 32 byte descriptor */
       
  2118 		rdlen = adapter->rx_ring[0].count *
       
  2119 			sizeof(union e1000_rx_desc_packet_split);
       
  2120 		adapter->clean_rx = e1000_clean_rx_irq_ps;
       
  2121 		adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
       
  2122 	} else {
       
  2123 		rdlen = adapter->rx_ring[0].count *
       
  2124 			sizeof(struct e1000_rx_desc);
       
  2125 		adapter->clean_rx = e1000_clean_rx_irq;
       
  2126 		adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
       
  2127 	}
       
  2128 
       
  2129 	/* disable receives while setting up the descriptors */
       
  2130 	rctl = E1000_READ_REG(hw, RCTL);
       
  2131 	E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
       
  2132 
       
  2133 	/* set the Receive Delay Timer Register */
       
  2134 	E1000_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
       
  2135 
       
  2136 	if (hw->mac_type >= e1000_82540) {
       
  2137 		E1000_WRITE_REG(hw, RADV, adapter->rx_abs_int_delay);
       
  2138 		if (adapter->itr_setting != 0)
       
  2139 			E1000_WRITE_REG(hw, ITR,
       
  2140 				1000000000 / (adapter->itr * 256));
       
  2141 	}
       
  2142 
       
  2143 	if (hw->mac_type >= e1000_82571) {
       
  2144 		ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
       
  2145 		/* Reset delay timers after every interrupt */
       
  2146 		ctrl_ext |= E1000_CTRL_EXT_INT_TIMER_CLR;
       
  2147 #ifdef CONFIG_E1000_NAPI
       
  2148 		/* Auto-Mask interrupts upon ICR access */
       
  2149 		ctrl_ext |= E1000_CTRL_EXT_IAME;
       
  2150 		E1000_WRITE_REG(hw, IAM, 0xffffffff);
       
  2151 #endif
       
  2152 		E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
       
  2153 		E1000_WRITE_FLUSH(hw);
       
  2154 	}
       
  2155 
       
  2156 	/* Setup the HW Rx Head and Tail Descriptor Pointers and
       
  2157 	 * the Base and Length of the Rx Descriptor Ring */
       
  2158 	switch (adapter->num_rx_queues) {
       
  2159 	case 1:
       
  2160 	default:
       
  2161 		rdba = adapter->rx_ring[0].dma;
       
  2162 		E1000_WRITE_REG(hw, RDLEN, rdlen);
       
  2163 		E1000_WRITE_REG(hw, RDBAH, (rdba >> 32));
       
  2164 		E1000_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
       
  2165 		E1000_WRITE_REG(hw, RDT, 0);
       
  2166 		E1000_WRITE_REG(hw, RDH, 0);
       
  2167 		adapter->rx_ring[0].rdh = ((hw->mac_type >= e1000_82543) ? E1000_RDH : E1000_82542_RDH);
       
  2168 		adapter->rx_ring[0].rdt = ((hw->mac_type >= e1000_82543) ? E1000_RDT : E1000_82542_RDT);
       
  2169 		break;
       
  2170 	}
       
  2171 
       
  2172 	/* Enable 82543 Receive Checksum Offload for TCP and UDP */
       
  2173 	if (hw->mac_type >= e1000_82543) {
       
  2174 		rxcsum = E1000_READ_REG(hw, RXCSUM);
       
  2175 		if (adapter->rx_csum) {
       
  2176 			rxcsum |= E1000_RXCSUM_TUOFL;
       
  2177 
       
  2178 			/* Enable 82571 IPv4 payload checksum for UDP fragments
       
  2179 			 * Must be used in conjunction with packet-split. */
       
  2180 			if ((hw->mac_type >= e1000_82571) &&
       
  2181 			    (adapter->rx_ps_pages)) {
       
  2182 				rxcsum |= E1000_RXCSUM_IPPCSE;
       
  2183 			}
       
  2184 		} else {
       
  2185 			rxcsum &= ~E1000_RXCSUM_TUOFL;
       
  2186 			/* don't need to clear IPPCSE as it defaults to 0 */
       
  2187 		}
       
  2188 		E1000_WRITE_REG(hw, RXCSUM, rxcsum);
       
  2189 	}
       
  2190 
       
  2191 	/* enable early receives on 82573, only takes effect if using > 2048
       
  2192 	 * byte total frame size.  for example only for jumbo frames */
       
  2193 #define E1000_ERT_2048 0x100
       
  2194 	if (hw->mac_type == e1000_82573)
       
  2195 		E1000_WRITE_REG(hw, ERT, E1000_ERT_2048);
       
  2196 
       
  2197 	/* Enable Receives */
       
  2198 	E1000_WRITE_REG(hw, RCTL, rctl);
       
  2199 }
       
  2200 
       
  2201 /**
       
  2202  * e1000_free_tx_resources - Free Tx Resources per Queue
       
  2203  * @adapter: board private structure
       
  2204  * @tx_ring: Tx descriptor ring for a specific queue
       
  2205  *
       
  2206  * Free all transmit software resources
       
  2207  **/
       
  2208 
       
  2209 static void
       
  2210 e1000_free_tx_resources(struct e1000_adapter *adapter,
       
  2211                         struct e1000_tx_ring *tx_ring)
       
  2212 {
       
  2213 	struct pci_dev *pdev = adapter->pdev;
       
  2214 
       
  2215 	e1000_clean_tx_ring(adapter, tx_ring);
       
  2216 
       
  2217 	vfree(tx_ring->buffer_info);
       
  2218 	tx_ring->buffer_info = NULL;
       
  2219 
       
  2220 	pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
       
  2221 
       
  2222 	tx_ring->desc = NULL;
       
  2223 }
       
  2224 
       
  2225 /**
       
  2226  * e1000_free_all_tx_resources - Free Tx Resources for All Queues
       
  2227  * @adapter: board private structure
       
  2228  *
       
  2229  * Free all transmit software resources
       
  2230  **/
       
  2231 
       
  2232 void
       
  2233 e1000_free_all_tx_resources(struct e1000_adapter *adapter)
       
  2234 {
       
  2235 	int i;
       
  2236 
       
  2237 	for (i = 0; i < adapter->num_tx_queues; i++)
       
  2238 		e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
       
  2239 }
       
  2240 
       
  2241 static void
       
  2242 e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
       
  2243 			struct e1000_buffer *buffer_info)
       
  2244 {
       
  2245 	if (adapter->ecdev)
       
  2246 		return;
       
  2247 
       
  2248 	if (buffer_info->dma) {
       
  2249 		pci_unmap_page(adapter->pdev,
       
  2250 				buffer_info->dma,
       
  2251 				buffer_info->length,
       
  2252 				PCI_DMA_TODEVICE);
       
  2253 		buffer_info->dma = 0;
       
  2254 	}
       
  2255 	if (buffer_info->skb) {
       
  2256 		dev_kfree_skb_any(buffer_info->skb);
       
  2257 		buffer_info->skb = NULL;
       
  2258 	}
       
  2259 	/* buffer_info must be completely set up in the transmit path */
       
  2260 }
       
  2261 
       
  2262 /**
       
  2263  * e1000_clean_tx_ring - Free Tx Buffers
       
  2264  * @adapter: board private structure
       
  2265  * @tx_ring: ring to be cleaned
       
  2266  **/
       
  2267 
       
  2268 static void
       
  2269 e1000_clean_tx_ring(struct e1000_adapter *adapter,
       
  2270                     struct e1000_tx_ring *tx_ring)
       
  2271 {
       
  2272 	struct e1000_buffer *buffer_info;
       
  2273 	unsigned long size;
       
  2274 	unsigned int i;
       
  2275 
       
  2276 	/* Free all the Tx ring sk_buffs */
       
  2277 
       
  2278 	for (i = 0; i < tx_ring->count; i++) {
       
  2279 		buffer_info = &tx_ring->buffer_info[i];
       
  2280 		e1000_unmap_and_free_tx_resource(adapter, buffer_info);
       
  2281 	}
       
  2282 
       
  2283 	size = sizeof(struct e1000_buffer) * tx_ring->count;
       
  2284 	memset(tx_ring->buffer_info, 0, size);
       
  2285 
       
  2286 	/* Zero out the descriptor ring */
       
  2287 
       
  2288 	memset(tx_ring->desc, 0, tx_ring->size);
       
  2289 
       
  2290 	tx_ring->next_to_use = 0;
       
  2291 	tx_ring->next_to_clean = 0;
       
  2292 	tx_ring->last_tx_tso = 0;
       
  2293 
       
  2294 	writel(0, adapter->hw.hw_addr + tx_ring->tdh);
       
  2295 	writel(0, adapter->hw.hw_addr + tx_ring->tdt);
       
  2296 }
       
  2297 
       
  2298 /**
       
  2299  * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
       
  2300  * @adapter: board private structure
       
  2301  **/
       
  2302 
       
  2303 static void
       
  2304 e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
       
  2305 {
       
  2306 	int i;
       
  2307 
       
  2308 	for (i = 0; i < adapter->num_tx_queues; i++)
       
  2309 		e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
       
  2310 }
       
  2311 
       
  2312 /**
       
  2313  * e1000_free_rx_resources - Free Rx Resources
       
  2314  * @adapter: board private structure
       
  2315  * @rx_ring: ring to clean the resources from
       
  2316  *
       
  2317  * Free all receive software resources
       
  2318  **/
       
  2319 
       
  2320 static void
       
  2321 e1000_free_rx_resources(struct e1000_adapter *adapter,
       
  2322                         struct e1000_rx_ring *rx_ring)
       
  2323 {
       
  2324 	struct pci_dev *pdev = adapter->pdev;
       
  2325 
       
  2326 	e1000_clean_rx_ring(adapter, rx_ring);
       
  2327 
       
  2328 	vfree(rx_ring->buffer_info);
       
  2329 	rx_ring->buffer_info = NULL;
       
  2330 	kfree(rx_ring->ps_page);
       
  2331 	rx_ring->ps_page = NULL;
       
  2332 	kfree(rx_ring->ps_page_dma);
       
  2333 	rx_ring->ps_page_dma = NULL;
       
  2334 
       
  2335 	pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
       
  2336 
       
  2337 	rx_ring->desc = NULL;
       
  2338 }
       
  2339 
       
  2340 /**
       
  2341  * e1000_free_all_rx_resources - Free Rx Resources for All Queues
       
  2342  * @adapter: board private structure
       
  2343  *
       
  2344  * Free all receive software resources
       
  2345  **/
       
  2346 
       
  2347 void
       
  2348 e1000_free_all_rx_resources(struct e1000_adapter *adapter)
       
  2349 {
       
  2350 	int i;
       
  2351 
       
  2352 	for (i = 0; i < adapter->num_rx_queues; i++)
       
  2353 		e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
       
  2354 }
       
  2355 
       
  2356 /**
       
  2357  * e1000_clean_rx_ring - Free Rx Buffers per Queue
       
  2358  * @adapter: board private structure
       
  2359  * @rx_ring: ring to free buffers from
       
  2360  **/
       
  2361 
       
  2362 static void
       
  2363 e1000_clean_rx_ring(struct e1000_adapter *adapter,
       
  2364                     struct e1000_rx_ring *rx_ring)
       
  2365 {
       
  2366 	struct e1000_buffer *buffer_info;
       
  2367 	struct e1000_ps_page *ps_page;
       
  2368 	struct e1000_ps_page_dma *ps_page_dma;
       
  2369 	struct pci_dev *pdev = adapter->pdev;
       
  2370 	unsigned long size;
       
  2371 	unsigned int i, j;
       
  2372 
       
  2373 	/* Free all the Rx ring sk_buffs */
       
  2374 	for (i = 0; i < rx_ring->count; i++) {
       
  2375 		buffer_info = &rx_ring->buffer_info[i];
       
  2376 		if (buffer_info->skb) {
       
  2377 			pci_unmap_single(pdev,
       
  2378 					 buffer_info->dma,
       
  2379 					 buffer_info->length,
       
  2380 					 PCI_DMA_FROMDEVICE);
       
  2381 
       
  2382 			dev_kfree_skb(buffer_info->skb);
       
  2383 			buffer_info->skb = NULL;
       
  2384 		}
       
  2385 		ps_page = &rx_ring->ps_page[i];
       
  2386 		ps_page_dma = &rx_ring->ps_page_dma[i];
       
  2387 		for (j = 0; j < adapter->rx_ps_pages; j++) {
       
  2388 			if (!ps_page->ps_page[j]) break;
       
  2389 			pci_unmap_page(pdev,
       
  2390 				       ps_page_dma->ps_page_dma[j],
       
  2391 				       PAGE_SIZE, PCI_DMA_FROMDEVICE);
       
  2392 			ps_page_dma->ps_page_dma[j] = 0;
       
  2393 			put_page(ps_page->ps_page[j]);
       
  2394 			ps_page->ps_page[j] = NULL;
       
  2395 		}
       
  2396 	}
       
  2397 
       
  2398 	size = sizeof(struct e1000_buffer) * rx_ring->count;
       
  2399 	memset(rx_ring->buffer_info, 0, size);
       
  2400 	size = sizeof(struct e1000_ps_page) * rx_ring->count;
       
  2401 	memset(rx_ring->ps_page, 0, size);
       
  2402 	size = sizeof(struct e1000_ps_page_dma) * rx_ring->count;
       
  2403 	memset(rx_ring->ps_page_dma, 0, size);
       
  2404 
       
  2405 	/* Zero out the descriptor ring */
       
  2406 
       
  2407 	memset(rx_ring->desc, 0, rx_ring->size);
       
  2408 
       
  2409 	rx_ring->next_to_clean = 0;
       
  2410 	rx_ring->next_to_use = 0;
       
  2411 
       
  2412 	writel(0, adapter->hw.hw_addr + rx_ring->rdh);
       
  2413 	writel(0, adapter->hw.hw_addr + rx_ring->rdt);
       
  2414 }
       
  2415 
       
  2416 /**
       
  2417  * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
       
  2418  * @adapter: board private structure
       
  2419  **/
       
  2420 
       
  2421 static void
       
  2422 e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
       
  2423 {
       
  2424 	int i;
       
  2425 
       
  2426 	for (i = 0; i < adapter->num_rx_queues; i++)
       
  2427 		e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
       
  2428 }
       
  2429 
       
  2430 /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
       
  2431  * and memory write and invalidate disabled for certain operations
       
  2432  */
       
  2433 static void
       
  2434 e1000_enter_82542_rst(struct e1000_adapter *adapter)
       
  2435 {
       
  2436 	struct net_device *netdev = adapter->netdev;
       
  2437 	u32 rctl;
       
  2438 
       
  2439 	e1000_pci_clear_mwi(&adapter->hw);
       
  2440 
       
  2441 	rctl = E1000_READ_REG(&adapter->hw, RCTL);
       
  2442 	rctl |= E1000_RCTL_RST;
       
  2443 	E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
       
  2444 	E1000_WRITE_FLUSH(&adapter->hw);
       
  2445 	mdelay(5);
       
  2446 
       
  2447 	if (!adapter->ecdev && netif_running(netdev))
       
  2448 		e1000_clean_all_rx_rings(adapter);
       
  2449 }
       
  2450 
       
  2451 static void
       
  2452 e1000_leave_82542_rst(struct e1000_adapter *adapter)
       
  2453 {
       
  2454 	struct net_device *netdev = adapter->netdev;
       
  2455 	u32 rctl;
       
  2456 
       
  2457 	rctl = E1000_READ_REG(&adapter->hw, RCTL);
       
  2458 	rctl &= ~E1000_RCTL_RST;
       
  2459 	E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
       
  2460 	E1000_WRITE_FLUSH(&adapter->hw);
       
  2461 	mdelay(5);
       
  2462 
       
  2463 	if (adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
       
  2464 		e1000_pci_set_mwi(&adapter->hw);
       
  2465 
       
  2466 	if (!adapter->netdev && netif_running(netdev)) {
       
  2467 		/* No need to loop, because 82542 supports only 1 queue */
       
  2468 		struct e1000_rx_ring *ring = &adapter->rx_ring[0];
       
  2469 		e1000_configure_rx(adapter);
       
  2470 		if (adapter->ecdev) { 
       
  2471 			/* fill rx ring completely! */
       
  2472 			adapter->alloc_rx_buf(adapter, ring, ring->count);
       
  2473 		} else {
       
  2474             /* this one leaves the last ring element unallocated! */
       
  2475 			adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
       
  2476 		}
       
  2477 
       
  2478 	}
       
  2479 }
       
  2480 
       
  2481 /**
       
  2482  * e1000_set_mac - Change the Ethernet Address of the NIC
       
  2483  * @netdev: network interface device structure
       
  2484  * @p: pointer to an address structure
       
  2485  *
       
  2486  * Returns 0 on success, negative on failure
       
  2487  **/
       
  2488 
       
  2489 static int
       
  2490 e1000_set_mac(struct net_device *netdev, void *p)
       
  2491 {
       
  2492 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  2493 	struct sockaddr *addr = p;
       
  2494 
       
  2495 	if (!is_valid_ether_addr(addr->sa_data))
       
  2496 		return -EADDRNOTAVAIL;
       
  2497 
       
  2498 	/* 82542 2.0 needs to be in reset to write receive address registers */
       
  2499 
       
  2500 	if (adapter->hw.mac_type == e1000_82542_rev2_0)
       
  2501 		e1000_enter_82542_rst(adapter);
       
  2502 
       
  2503 	memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
       
  2504 	memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
       
  2505 
       
  2506 	e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
       
  2507 
       
  2508 	/* With 82571 controllers, LAA may be overwritten (with the default)
       
  2509 	 * due to controller reset from the other port. */
       
  2510 	if (adapter->hw.mac_type == e1000_82571) {
       
  2511 		/* activate the work around */
       
  2512 		adapter->hw.laa_is_present = 1;
       
  2513 
       
  2514 		/* Hold a copy of the LAA in RAR[14] This is done so that
       
  2515 		 * between the time RAR[0] gets clobbered  and the time it
       
  2516 		 * gets fixed (in e1000_watchdog), the actual LAA is in one
       
  2517 		 * of the RARs and no incoming packets directed to this port
       
  2518 		 * are dropped. Eventaully the LAA will be in RAR[0] and
       
  2519 		 * RAR[14] */
       
  2520 		e1000_rar_set(&adapter->hw, adapter->hw.mac_addr,
       
  2521 					E1000_RAR_ENTRIES - 1);
       
  2522 	}
       
  2523 
       
  2524 	if (adapter->hw.mac_type == e1000_82542_rev2_0)
       
  2525 		e1000_leave_82542_rst(adapter);
       
  2526 
       
  2527 	return 0;
       
  2528 }
       
  2529 
       
  2530 /**
       
  2531  * e1000_set_rx_mode - Secondary Unicast, Multicast and Promiscuous mode set
       
  2532  * @netdev: network interface device structure
       
  2533  *
       
  2534  * The set_rx_mode entry point is called whenever the unicast or multicast
       
  2535  * address lists or the network interface flags are updated. This routine is
       
  2536  * responsible for configuring the hardware for proper unicast, multicast,
       
  2537  * promiscuous mode, and all-multi behavior.
       
  2538  **/
       
  2539 
       
  2540 static void
       
  2541 e1000_set_rx_mode(struct net_device *netdev)
       
  2542 {
       
  2543 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  2544 	struct e1000_hw *hw = &adapter->hw;
       
  2545 	struct dev_addr_list *uc_ptr;
       
  2546 	struct dev_addr_list *mc_ptr;
       
  2547 	u32 rctl;
       
  2548 	u32 hash_value;
       
  2549 	int i, rar_entries = E1000_RAR_ENTRIES;
       
  2550 	int mta_reg_count = (hw->mac_type == e1000_ich8lan) ?
       
  2551 				E1000_NUM_MTA_REGISTERS_ICH8LAN :
       
  2552 				E1000_NUM_MTA_REGISTERS;
       
  2553 
       
  2554 	if (adapter->hw.mac_type == e1000_ich8lan)
       
  2555 		rar_entries = E1000_RAR_ENTRIES_ICH8LAN;
       
  2556 
       
  2557 	/* reserve RAR[14] for LAA over-write work-around */
       
  2558 	if (adapter->hw.mac_type == e1000_82571)
       
  2559 		rar_entries--;
       
  2560 
       
  2561 	/* Check for Promiscuous and All Multicast modes */
       
  2562 
       
  2563 	rctl = E1000_READ_REG(hw, RCTL);
       
  2564 
       
  2565 	if (netdev->flags & IFF_PROMISC) {
       
  2566 		rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
       
  2567 	} else if (netdev->flags & IFF_ALLMULTI) {
       
  2568 		rctl |= E1000_RCTL_MPE;
       
  2569 	} else {
       
  2570 		rctl &= ~E1000_RCTL_MPE;
       
  2571 	}
       
  2572 
       
  2573 	uc_ptr = NULL;
       
  2574 	if (netdev->uc_count > rar_entries - 1) {
       
  2575 		rctl |= E1000_RCTL_UPE;
       
  2576 	} else if (!(netdev->flags & IFF_PROMISC)) {
       
  2577 		rctl &= ~E1000_RCTL_UPE;
       
  2578 		uc_ptr = netdev->uc_list;
       
  2579 	}
       
  2580 
       
  2581 	E1000_WRITE_REG(hw, RCTL, rctl);
       
  2582 
       
  2583 	/* 82542 2.0 needs to be in reset to write receive address registers */
       
  2584 
       
  2585 	if (hw->mac_type == e1000_82542_rev2_0)
       
  2586 		e1000_enter_82542_rst(adapter);
       
  2587 
       
  2588 	/* load the first 14 addresses into the exact filters 1-14. Unicast
       
  2589 	 * addresses take precedence to avoid disabling unicast filtering
       
  2590 	 * when possible.
       
  2591 	 *
       
  2592 	 * RAR 0 is used for the station MAC adddress
       
  2593 	 * if there are not 14 addresses, go ahead and clear the filters
       
  2594 	 * -- with 82571 controllers only 0-13 entries are filled here
       
  2595 	 */
       
  2596 	mc_ptr = netdev->mc_list;
       
  2597 
       
  2598 	for (i = 1; i < rar_entries; i++) {
       
  2599 		if (uc_ptr) {
       
  2600 			e1000_rar_set(hw, uc_ptr->da_addr, i);
       
  2601 			uc_ptr = uc_ptr->next;
       
  2602 		} else if (mc_ptr) {
       
  2603 			e1000_rar_set(hw, mc_ptr->da_addr, i);
       
  2604 			mc_ptr = mc_ptr->next;
       
  2605 		} else {
       
  2606 			E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
       
  2607 			E1000_WRITE_FLUSH(hw);
       
  2608 			E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
       
  2609 			E1000_WRITE_FLUSH(hw);
       
  2610 		}
       
  2611 	}
       
  2612 	WARN_ON(uc_ptr != NULL);
       
  2613 
       
  2614 	/* clear the old settings from the multicast hash table */
       
  2615 
       
  2616 	for (i = 0; i < mta_reg_count; i++) {
       
  2617 		E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
       
  2618 		E1000_WRITE_FLUSH(hw);
       
  2619 	}
       
  2620 
       
  2621 	/* load any remaining addresses into the hash table */
       
  2622 
       
  2623 	for (; mc_ptr; mc_ptr = mc_ptr->next) {
       
  2624 		hash_value = e1000_hash_mc_addr(hw, mc_ptr->da_addr);
       
  2625 		e1000_mta_set(hw, hash_value);
       
  2626 	}
       
  2627 
       
  2628 	if (hw->mac_type == e1000_82542_rev2_0)
       
  2629 		e1000_leave_82542_rst(adapter);
       
  2630 }
       
  2631 
       
  2632 /* Need to wait a few seconds after link up to get diagnostic information from
       
  2633  * the phy */
       
  2634 
       
  2635 static void
       
  2636 e1000_update_phy_info(unsigned long data)
       
  2637 {
       
  2638 	struct e1000_adapter *adapter = (struct e1000_adapter *) data;
       
  2639 	e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
       
  2640 }
       
  2641 
       
  2642 /**
       
  2643  * e1000_82547_tx_fifo_stall - Timer Call-back
       
  2644  * @data: pointer to adapter cast into an unsigned long
       
  2645  **/
       
  2646 
       
  2647 static void
       
  2648 e1000_82547_tx_fifo_stall(unsigned long data)
       
  2649 {
       
  2650 	struct e1000_adapter *adapter = (struct e1000_adapter *) data;
       
  2651 	struct net_device *netdev = adapter->netdev;
       
  2652 	u32 tctl;
       
  2653 
       
  2654 	if (atomic_read(&adapter->tx_fifo_stall)) {
       
  2655 		if ((E1000_READ_REG(&adapter->hw, TDT) ==
       
  2656 		    E1000_READ_REG(&adapter->hw, TDH)) &&
       
  2657 		   (E1000_READ_REG(&adapter->hw, TDFT) ==
       
  2658 		    E1000_READ_REG(&adapter->hw, TDFH)) &&
       
  2659 		   (E1000_READ_REG(&adapter->hw, TDFTS) ==
       
  2660 		    E1000_READ_REG(&adapter->hw, TDFHS))) {
       
  2661 			tctl = E1000_READ_REG(&adapter->hw, TCTL);
       
  2662 			E1000_WRITE_REG(&adapter->hw, TCTL,
       
  2663 					tctl & ~E1000_TCTL_EN);
       
  2664 			E1000_WRITE_REG(&adapter->hw, TDFT,
       
  2665 					adapter->tx_head_addr);
       
  2666 			E1000_WRITE_REG(&adapter->hw, TDFH,
       
  2667 					adapter->tx_head_addr);
       
  2668 			E1000_WRITE_REG(&adapter->hw, TDFTS,
       
  2669 					adapter->tx_head_addr);
       
  2670 			E1000_WRITE_REG(&adapter->hw, TDFHS,
       
  2671 					adapter->tx_head_addr);
       
  2672 			E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
       
  2673 			E1000_WRITE_FLUSH(&adapter->hw);
       
  2674 
       
  2675 			adapter->tx_fifo_head = 0;
       
  2676 			atomic_set(&adapter->tx_fifo_stall, 0);
       
  2677 			if (!adapter->ecdev) netif_wake_queue(netdev);
       
  2678 		} else {
       
  2679 			if (!adapter->ecdev)
       
  2680 				mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
       
  2681 		}
       
  2682 	}
       
  2683 }
       
  2684 
       
  2685 /**
       
  2686  * e1000_watchdog - Timer Call-back
       
  2687  * @data: pointer to adapter cast into an unsigned long
       
  2688  **/
       
  2689 static void
       
  2690 e1000_watchdog(unsigned long data)
       
  2691 {
       
  2692 	struct e1000_adapter *adapter = (struct e1000_adapter *) data;
       
  2693 	struct net_device *netdev = adapter->netdev;
       
  2694 	struct e1000_tx_ring *txdr = adapter->tx_ring;
       
  2695 	u32 link, tctl;
       
  2696 	s32 ret_val;
       
  2697 
       
  2698 	ret_val = e1000_check_for_link(&adapter->hw);
       
  2699 	if ((ret_val == E1000_ERR_PHY) &&
       
  2700 	    (adapter->hw.phy_type == e1000_phy_igp_3) &&
       
  2701 	    (E1000_READ_REG(&adapter->hw, CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
       
  2702 		/* See e1000_kumeran_lock_loss_workaround() */
       
  2703 		DPRINTK(LINK, INFO,
       
  2704 			"Gigabit has been disabled, downgrading speed\n");
       
  2705 	}
       
  2706 
       
  2707 	if (adapter->hw.mac_type == e1000_82573) {
       
  2708 		e1000_enable_tx_pkt_filtering(&adapter->hw);
       
  2709 		if (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id)
       
  2710 			e1000_update_mng_vlan(adapter);
       
  2711 	}
       
  2712 
       
  2713 	if ((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
       
  2714 	   !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
       
  2715 		link = !adapter->hw.serdes_link_down;
       
  2716 	else
       
  2717 		link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
       
  2718 
       
  2719 	if (link) {
       
  2720 		if ((adapter->ecdev && !ecdev_get_link(adapter->ecdev))
       
  2721 				|| (!adapter->ecdev && !netif_carrier_ok(netdev))) {
       
  2722 			u32 ctrl;
       
  2723 			bool txb2b = true;
       
  2724 			e1000_get_speed_and_duplex(&adapter->hw,
       
  2725 			                           &adapter->link_speed,
       
  2726 			                           &adapter->link_duplex);
       
  2727 
       
  2728 			ctrl = E1000_READ_REG(&adapter->hw, CTRL);
       
  2729 			DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s, "
       
  2730 			        "Flow Control: %s\n",
       
  2731 			        adapter->link_speed,
       
  2732 			        adapter->link_duplex == FULL_DUPLEX ?
       
  2733 			        "Full Duplex" : "Half Duplex",
       
  2734 			        ((ctrl & E1000_CTRL_TFCE) && (ctrl &
       
  2735 			        E1000_CTRL_RFCE)) ? "RX/TX" : ((ctrl &
       
  2736 			        E1000_CTRL_RFCE) ? "RX" : ((ctrl &
       
  2737 			        E1000_CTRL_TFCE) ? "TX" : "None" )));
       
  2738 
       
  2739 			/* tweak tx_queue_len according to speed/duplex
       
  2740 			 * and adjust the timeout factor */
       
  2741 			netdev->tx_queue_len = adapter->tx_queue_len;
       
  2742 			adapter->tx_timeout_factor = 1;
       
  2743 			switch (adapter->link_speed) {
       
  2744 			case SPEED_10:
       
  2745 				txb2b = false;
       
  2746 				netdev->tx_queue_len = 10;
       
  2747 				adapter->tx_timeout_factor = 8;
       
  2748 				break;
       
  2749 			case SPEED_100:
       
  2750 				txb2b = false;
       
  2751 				netdev->tx_queue_len = 100;
       
  2752 				/* maybe add some timeout factor ? */
       
  2753 				break;
       
  2754 			}
       
  2755 
       
  2756 			if ((adapter->hw.mac_type == e1000_82571 ||
       
  2757 			     adapter->hw.mac_type == e1000_82572) &&
       
  2758 			    !txb2b) {
       
  2759 				u32 tarc0;
       
  2760 				tarc0 = E1000_READ_REG(&adapter->hw, TARC0);
       
  2761 				tarc0 &= ~(1 << 21);
       
  2762 				E1000_WRITE_REG(&adapter->hw, TARC0, tarc0);
       
  2763 			}
       
  2764 
       
  2765 			/* disable TSO for pcie and 10/100 speeds, to avoid
       
  2766 			 * some hardware issues */
       
  2767 			if (!adapter->tso_force &&
       
  2768 			    adapter->hw.bus_type == e1000_bus_type_pci_express){
       
  2769 				switch (adapter->link_speed) {
       
  2770 				case SPEED_10:
       
  2771 				case SPEED_100:
       
  2772 					DPRINTK(PROBE,INFO,
       
  2773 				        "10/100 speed: disabling TSO\n");
       
  2774 					netdev->features &= ~NETIF_F_TSO;
       
  2775 					netdev->features &= ~NETIF_F_TSO6;
       
  2776 					break;
       
  2777 				case SPEED_1000:
       
  2778 					netdev->features |= NETIF_F_TSO;
       
  2779 					netdev->features |= NETIF_F_TSO6;
       
  2780 					break;
       
  2781 				default:
       
  2782 					/* oops */
       
  2783 					break;
       
  2784 				}
       
  2785 			}
       
  2786 
       
  2787 			/* enable transmits in the hardware, need to do this
       
  2788 			 * after setting TARC0 */
       
  2789 			tctl = E1000_READ_REG(&adapter->hw, TCTL);
       
  2790 			tctl |= E1000_TCTL_EN;
       
  2791 			E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
       
  2792 
       
  2793 			if (adapter->ecdev) {
       
  2794 				ecdev_set_link(adapter->ecdev, 1);
       
  2795 			} else {
       
  2796 				netif_carrier_on(netdev);
       
  2797 				netif_wake_queue(netdev);
       
  2798 				mod_timer(&adapter->phy_info_timer, round_jiffies(jiffies + 2 * HZ));
       
  2799 			}
       
  2800 			adapter->smartspeed = 0;
       
  2801 		} else {
       
  2802 			/* make sure the receive unit is started */
       
  2803 			if (adapter->hw.rx_needs_kicking) {
       
  2804 				struct e1000_hw *hw = &adapter->hw;
       
  2805 				u32 rctl = E1000_READ_REG(hw, RCTL);
       
  2806 				E1000_WRITE_REG(hw, RCTL, rctl | E1000_RCTL_EN);
       
  2807 			}
       
  2808 		}
       
  2809 	} else {
       
  2810 		if ((adapter->ecdev && ecdev_get_link(adapter->ecdev))
       
  2811 				|| (!adapter->ecdev && netif_carrier_ok(netdev))) {
       
  2812 			adapter->link_speed = 0;
       
  2813 			adapter->link_duplex = 0;
       
  2814 			DPRINTK(LINK, INFO, "NIC Link is Down\n");
       
  2815 			if (adapter->ecdev) {
       
  2816 				ecdev_set_link(adapter->ecdev, 0);
       
  2817 			} else {
       
  2818 				netif_carrier_off(netdev);
       
  2819 				netif_stop_queue(netdev);
       
  2820 				mod_timer(&adapter->phy_info_timer, round_jiffies(jiffies + 2 * HZ));
       
  2821 			}
       
  2822 
       
  2823 			/* 80003ES2LAN workaround--
       
  2824 			 * For packet buffer work-around on link down event;
       
  2825 			 * disable receives in the ISR and
       
  2826 			 * reset device here in the watchdog
       
  2827 			 */
       
  2828 			if (adapter->hw.mac_type == e1000_80003es2lan)
       
  2829 				/* reset device */
       
  2830 				schedule_work(&adapter->reset_task);
       
  2831 		}
       
  2832 
       
  2833 		e1000_smartspeed(adapter);
       
  2834 	}
       
  2835 
       
  2836 	e1000_update_stats(adapter);
       
  2837 
       
  2838 	adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
       
  2839 	adapter->tpt_old = adapter->stats.tpt;
       
  2840 	adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
       
  2841 	adapter->colc_old = adapter->stats.colc;
       
  2842 
       
  2843 	adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
       
  2844 	adapter->gorcl_old = adapter->stats.gorcl;
       
  2845 	adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
       
  2846 	adapter->gotcl_old = adapter->stats.gotcl;
       
  2847 
       
  2848 	e1000_update_adaptive(&adapter->hw);
       
  2849 
       
  2850 	if (!adapter->ecdev && !netif_carrier_ok(netdev)) {
       
  2851 		if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
       
  2852 			/* We've lost link, so the controller stops DMA,
       
  2853 			 * but we've got queued Tx work that's never going
       
  2854 			 * to get done, so reset controller to flush Tx.
       
  2855 			 * (Do the reset outside of interrupt context). */
       
  2856 			adapter->tx_timeout_count++;
       
  2857 			schedule_work(&adapter->reset_task);
       
  2858 		}
       
  2859 	}
       
  2860 
       
  2861 	/* Cause software interrupt to ensure rx ring is cleaned */
       
  2862 	E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
       
  2863 
       
  2864 	/* Force detection of hung controller every watchdog period */
       
  2865 	if (!adapter->ecdev) adapter->detect_tx_hung = true;
       
  2866 
       
  2867 	/* With 82571 controllers, LAA may be overwritten due to controller
       
  2868 	 * reset from the other port. Set the appropriate LAA in RAR[0] */
       
  2869 	if (adapter->hw.mac_type == e1000_82571 && adapter->hw.laa_is_present)
       
  2870 		e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
       
  2871 
       
  2872 	/* Reset the timer */
       
  2873 	if (!adapter->ecdev)
       
  2874 		mod_timer(&adapter->watchdog_timer, round_jiffies(jiffies + 2 * HZ));
       
  2875 }
       
  2876 
       
  2877 enum latency_range {
       
  2878 	lowest_latency = 0,
       
  2879 	low_latency = 1,
       
  2880 	bulk_latency = 2,
       
  2881 	latency_invalid = 255
       
  2882 };
       
  2883 
       
  2884 /**
       
  2885  * e1000_update_itr - update the dynamic ITR value based on statistics
       
  2886  *      Stores a new ITR value based on packets and byte
       
  2887  *      counts during the last interrupt.  The advantage of per interrupt
       
  2888  *      computation is faster updates and more accurate ITR for the current
       
  2889  *      traffic pattern.  Constants in this function were computed
       
  2890  *      based on theoretical maximum wire speed and thresholds were set based
       
  2891  *      on testing data as well as attempting to minimize response time
       
  2892  *      while increasing bulk throughput.
       
  2893  *      this functionality is controlled by the InterruptThrottleRate module
       
  2894  *      parameter (see e1000_param.c)
       
  2895  * @adapter: pointer to adapter
       
  2896  * @itr_setting: current adapter->itr
       
  2897  * @packets: the number of packets during this measurement interval
       
  2898  * @bytes: the number of bytes during this measurement interval
       
  2899  **/
       
  2900 static unsigned int e1000_update_itr(struct e1000_adapter *adapter,
       
  2901                                    u16 itr_setting,
       
  2902                                    int packets,
       
  2903                                    int bytes)
       
  2904 {
       
  2905 	unsigned int retval = itr_setting;
       
  2906 	struct e1000_hw *hw = &adapter->hw;
       
  2907 
       
  2908 	if (unlikely(hw->mac_type < e1000_82540))
       
  2909 		goto update_itr_done;
       
  2910 
       
  2911 	if (packets == 0)
       
  2912 		goto update_itr_done;
       
  2913 
       
  2914 	switch (itr_setting) {
       
  2915 	case lowest_latency:
       
  2916 		/* jumbo frames get bulk treatment*/
       
  2917 		if (bytes/packets > 8000)
       
  2918 			retval = bulk_latency;
       
  2919 		else if ((packets < 5) && (bytes > 512))
       
  2920 			retval = low_latency;
       
  2921 		break;
       
  2922 	case low_latency:  /* 50 usec aka 20000 ints/s */
       
  2923 		if (bytes > 10000) {
       
  2924 			/* jumbo frames need bulk latency setting */
       
  2925 			if (bytes/packets > 8000)
       
  2926 				retval = bulk_latency;
       
  2927 			else if ((packets < 10) || ((bytes/packets) > 1200))
       
  2928 				retval = bulk_latency;
       
  2929 			else if ((packets > 35))
       
  2930 				retval = lowest_latency;
       
  2931 		} else if (bytes/packets > 2000)
       
  2932 			retval = bulk_latency;
       
  2933 		else if (packets <= 2 && bytes < 512)
       
  2934 			retval = lowest_latency;
       
  2935 		break;
       
  2936 	case bulk_latency: /* 250 usec aka 4000 ints/s */
       
  2937 		if (bytes > 25000) {
       
  2938 			if (packets > 35)
       
  2939 				retval = low_latency;
       
  2940 		} else if (bytes < 6000) {
       
  2941 			retval = low_latency;
       
  2942 		}
       
  2943 		break;
       
  2944 	}
       
  2945 
       
  2946 update_itr_done:
       
  2947 	return retval;
       
  2948 }
       
  2949 
       
  2950 static void e1000_set_itr(struct e1000_adapter *adapter)
       
  2951 {
       
  2952 	struct e1000_hw *hw = &adapter->hw;
       
  2953 	u16 current_itr;
       
  2954 	u32 new_itr = adapter->itr;
       
  2955 
       
  2956 	if (unlikely(hw->mac_type < e1000_82540))
       
  2957 		return;
       
  2958 
       
  2959 	/* for non-gigabit speeds, just fix the interrupt rate at 4000 */
       
  2960 	if (unlikely(adapter->link_speed != SPEED_1000)) {
       
  2961 		current_itr = 0;
       
  2962 		new_itr = 4000;
       
  2963 		goto set_itr_now;
       
  2964 	}
       
  2965 
       
  2966 	adapter->tx_itr = e1000_update_itr(adapter,
       
  2967 	                            adapter->tx_itr,
       
  2968 	                            adapter->total_tx_packets,
       
  2969 	                            adapter->total_tx_bytes);
       
  2970 	/* conservative mode (itr 3) eliminates the lowest_latency setting */
       
  2971 	if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
       
  2972 		adapter->tx_itr = low_latency;
       
  2973 
       
  2974 	adapter->rx_itr = e1000_update_itr(adapter,
       
  2975 	                            adapter->rx_itr,
       
  2976 	                            adapter->total_rx_packets,
       
  2977 	                            adapter->total_rx_bytes);
       
  2978 	/* conservative mode (itr 3) eliminates the lowest_latency setting */
       
  2979 	if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
       
  2980 		adapter->rx_itr = low_latency;
       
  2981 
       
  2982 	current_itr = max(adapter->rx_itr, adapter->tx_itr);
       
  2983 
       
  2984 	switch (current_itr) {
       
  2985 	/* counts and packets in update_itr are dependent on these numbers */
       
  2986 	case lowest_latency:
       
  2987 		new_itr = 70000;
       
  2988 		break;
       
  2989 	case low_latency:
       
  2990 		new_itr = 20000; /* aka hwitr = ~200 */
       
  2991 		break;
       
  2992 	case bulk_latency:
       
  2993 		new_itr = 4000;
       
  2994 		break;
       
  2995 	default:
       
  2996 		break;
       
  2997 	}
       
  2998 
       
  2999 set_itr_now:
       
  3000 	if (new_itr != adapter->itr) {
       
  3001 		/* this attempts to bias the interrupt rate towards Bulk
       
  3002 		 * by adding intermediate steps when interrupt rate is
       
  3003 		 * increasing */
       
  3004 		new_itr = new_itr > adapter->itr ?
       
  3005 		             min(adapter->itr + (new_itr >> 2), new_itr) :
       
  3006 		             new_itr;
       
  3007 		adapter->itr = new_itr;
       
  3008 		E1000_WRITE_REG(hw, ITR, 1000000000 / (new_itr * 256));
       
  3009 	}
       
  3010 
       
  3011 	return;
       
  3012 }
       
  3013 
       
  3014 #define E1000_TX_FLAGS_CSUM		0x00000001
       
  3015 #define E1000_TX_FLAGS_VLAN		0x00000002
       
  3016 #define E1000_TX_FLAGS_TSO		0x00000004
       
  3017 #define E1000_TX_FLAGS_IPV4		0x00000008
       
  3018 #define E1000_TX_FLAGS_VLAN_MASK	0xffff0000
       
  3019 #define E1000_TX_FLAGS_VLAN_SHIFT	16
       
  3020 
       
  3021 static int
       
  3022 e1000_tso(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
       
  3023           struct sk_buff *skb)
       
  3024 {
       
  3025 	struct e1000_context_desc *context_desc;
       
  3026 	struct e1000_buffer *buffer_info;
       
  3027 	unsigned int i;
       
  3028 	u32 cmd_length = 0;
       
  3029 	u16 ipcse = 0, tucse, mss;
       
  3030 	u8 ipcss, ipcso, tucss, tucso, hdr_len;
       
  3031 	int err;
       
  3032 
       
  3033 	if (skb_is_gso(skb)) {
       
  3034 		if (skb_header_cloned(skb)) {
       
  3035 			err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
       
  3036 			if (err)
       
  3037 				return err;
       
  3038 		}
       
  3039 
       
  3040 		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
       
  3041 		mss = skb_shinfo(skb)->gso_size;
       
  3042 		if (skb->protocol == htons(ETH_P_IP)) {
       
  3043 			struct iphdr *iph = ip_hdr(skb);
       
  3044 			iph->tot_len = 0;
       
  3045 			iph->check = 0;
       
  3046 			tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
       
  3047 								 iph->daddr, 0,
       
  3048 								 IPPROTO_TCP,
       
  3049 								 0);
       
  3050 			cmd_length = E1000_TXD_CMD_IP;
       
  3051 			ipcse = skb_transport_offset(skb) - 1;
       
  3052 		} else if (skb->protocol == htons(ETH_P_IPV6)) {
       
  3053 			ipv6_hdr(skb)->payload_len = 0;
       
  3054 			tcp_hdr(skb)->check =
       
  3055 				~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
       
  3056 						 &ipv6_hdr(skb)->daddr,
       
  3057 						 0, IPPROTO_TCP, 0);
       
  3058 			ipcse = 0;
       
  3059 		}
       
  3060 		ipcss = skb_network_offset(skb);
       
  3061 		ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
       
  3062 		tucss = skb_transport_offset(skb);
       
  3063 		tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
       
  3064 		tucse = 0;
       
  3065 
       
  3066 		cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
       
  3067 			       E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
       
  3068 
       
  3069 		i = tx_ring->next_to_use;
       
  3070 		context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
       
  3071 		buffer_info = &tx_ring->buffer_info[i];
       
  3072 
       
  3073 		context_desc->lower_setup.ip_fields.ipcss  = ipcss;
       
  3074 		context_desc->lower_setup.ip_fields.ipcso  = ipcso;
       
  3075 		context_desc->lower_setup.ip_fields.ipcse  = cpu_to_le16(ipcse);
       
  3076 		context_desc->upper_setup.tcp_fields.tucss = tucss;
       
  3077 		context_desc->upper_setup.tcp_fields.tucso = tucso;
       
  3078 		context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
       
  3079 		context_desc->tcp_seg_setup.fields.mss     = cpu_to_le16(mss);
       
  3080 		context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
       
  3081 		context_desc->cmd_and_length = cpu_to_le32(cmd_length);
       
  3082 
       
  3083 		buffer_info->time_stamp = jiffies;
       
  3084 		buffer_info->next_to_watch = i;
       
  3085 
       
  3086 		if (++i == tx_ring->count) i = 0;
       
  3087 		tx_ring->next_to_use = i;
       
  3088 
       
  3089 		return true;
       
  3090 	}
       
  3091 	return false;
       
  3092 }
       
  3093 
       
  3094 static bool
       
  3095 e1000_tx_csum(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
       
  3096               struct sk_buff *skb)
       
  3097 {
       
  3098 	struct e1000_context_desc *context_desc;
       
  3099 	struct e1000_buffer *buffer_info;
       
  3100 	unsigned int i;
       
  3101 	u8 css;
       
  3102 
       
  3103 	if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) {
       
  3104 		css = skb_transport_offset(skb);
       
  3105 
       
  3106 		i = tx_ring->next_to_use;
       
  3107 		buffer_info = &tx_ring->buffer_info[i];
       
  3108 		context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
       
  3109 
       
  3110 		context_desc->lower_setup.ip_config = 0;
       
  3111 		context_desc->upper_setup.tcp_fields.tucss = css;
       
  3112 		context_desc->upper_setup.tcp_fields.tucso =
       
  3113 			css + skb->csum_offset;
       
  3114 		context_desc->upper_setup.tcp_fields.tucse = 0;
       
  3115 		context_desc->tcp_seg_setup.data = 0;
       
  3116 		context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
       
  3117 
       
  3118 		buffer_info->time_stamp = jiffies;
       
  3119 		buffer_info->next_to_watch = i;
       
  3120 
       
  3121 		if (unlikely(++i == tx_ring->count)) i = 0;
       
  3122 		tx_ring->next_to_use = i;
       
  3123 
       
  3124 		return true;
       
  3125 	}
       
  3126 
       
  3127 	return false;
       
  3128 }
       
  3129 
       
  3130 #define E1000_MAX_TXD_PWR	12
       
  3131 #define E1000_MAX_DATA_PER_TXD	(1<<E1000_MAX_TXD_PWR)
       
  3132 
       
  3133 static int
       
  3134 e1000_tx_map(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
       
  3135              struct sk_buff *skb, unsigned int first, unsigned int max_per_txd,
       
  3136              unsigned int nr_frags, unsigned int mss)
       
  3137 {
       
  3138 	struct e1000_buffer *buffer_info;
       
  3139 	unsigned int len = skb->len;
       
  3140 	unsigned int offset = 0, size, count = 0, i;
       
  3141 	unsigned int f;
       
  3142 	len -= skb->data_len;
       
  3143 
       
  3144 	i = tx_ring->next_to_use;
       
  3145 
       
  3146 	while (len) {
       
  3147 		buffer_info = &tx_ring->buffer_info[i];
       
  3148 		size = min(len, max_per_txd);
       
  3149 		/* Workaround for Controller erratum --
       
  3150 		 * descriptor for non-tso packet in a linear SKB that follows a
       
  3151 		 * tso gets written back prematurely before the data is fully
       
  3152 		 * DMA'd to the controller */
       
  3153 		if (!skb->data_len && tx_ring->last_tx_tso &&
       
  3154 		    !skb_is_gso(skb)) {
       
  3155 			tx_ring->last_tx_tso = 0;
       
  3156 			size -= 4;
       
  3157 		}
       
  3158 
       
  3159 		/* Workaround for premature desc write-backs
       
  3160 		 * in TSO mode.  Append 4-byte sentinel desc */
       
  3161 		if (unlikely(mss && !nr_frags && size == len && size > 8))
       
  3162 			size -= 4;
       
  3163 		/* work-around for errata 10 and it applies
       
  3164 		 * to all controllers in PCI-X mode
       
  3165 		 * The fix is to make sure that the first descriptor of a
       
  3166 		 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
       
  3167 		 */
       
  3168 		if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
       
  3169 		                (size > 2015) && count == 0))
       
  3170 		        size = 2015;
       
  3171 
       
  3172 		/* Workaround for potential 82544 hang in PCI-X.  Avoid
       
  3173 		 * terminating buffers within evenly-aligned dwords. */
       
  3174 		if (unlikely(adapter->pcix_82544 &&
       
  3175 		   !((unsigned long)(skb->data + offset + size - 1) & 4) &&
       
  3176 		   size > 4))
       
  3177 			size -= 4;
       
  3178 
       
  3179 		buffer_info->length = size;
       
  3180 		buffer_info->dma =
       
  3181 			pci_map_single(adapter->pdev,
       
  3182 				skb->data + offset,
       
  3183 				size,
       
  3184 				PCI_DMA_TODEVICE);
       
  3185 		buffer_info->time_stamp = jiffies;
       
  3186 		buffer_info->next_to_watch = i;
       
  3187 
       
  3188 		len -= size;
       
  3189 		offset += size;
       
  3190 		count++;
       
  3191 		if (unlikely(++i == tx_ring->count)) i = 0;
       
  3192 	}
       
  3193 
       
  3194 	for (f = 0; f < nr_frags; f++) {
       
  3195 		struct skb_frag_struct *frag;
       
  3196 
       
  3197 		frag = &skb_shinfo(skb)->frags[f];
       
  3198 		len = frag->size;
       
  3199 		offset = frag->page_offset;
       
  3200 
       
  3201 		while (len) {
       
  3202 			buffer_info = &tx_ring->buffer_info[i];
       
  3203 			size = min(len, max_per_txd);
       
  3204 			/* Workaround for premature desc write-backs
       
  3205 			 * in TSO mode.  Append 4-byte sentinel desc */
       
  3206 			if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
       
  3207 				size -= 4;
       
  3208 			/* Workaround for potential 82544 hang in PCI-X.
       
  3209 			 * Avoid terminating buffers within evenly-aligned
       
  3210 			 * dwords. */
       
  3211 			if (unlikely(adapter->pcix_82544 &&
       
  3212 			   !((unsigned long)(frag->page+offset+size-1) & 4) &&
       
  3213 			   size > 4))
       
  3214 				size -= 4;
       
  3215 
       
  3216 			buffer_info->length = size;
       
  3217 			buffer_info->dma =
       
  3218 				pci_map_page(adapter->pdev,
       
  3219 					frag->page,
       
  3220 					offset,
       
  3221 					size,
       
  3222 					PCI_DMA_TODEVICE);
       
  3223 			buffer_info->time_stamp = jiffies;
       
  3224 			buffer_info->next_to_watch = i;
       
  3225 
       
  3226 			len -= size;
       
  3227 			offset += size;
       
  3228 			count++;
       
  3229 			if (unlikely(++i == tx_ring->count)) i = 0;
       
  3230 		}
       
  3231 	}
       
  3232 
       
  3233 	i = (i == 0) ? tx_ring->count - 1 : i - 1;
       
  3234 	tx_ring->buffer_info[i].skb = skb;
       
  3235 	tx_ring->buffer_info[first].next_to_watch = i;
       
  3236 
       
  3237 	return count;
       
  3238 }
       
  3239 
       
  3240 static void
       
  3241 e1000_tx_queue(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
       
  3242                int tx_flags, int count)
       
  3243 {
       
  3244 	struct e1000_tx_desc *tx_desc = NULL;
       
  3245 	struct e1000_buffer *buffer_info;
       
  3246 	u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
       
  3247 	unsigned int i;
       
  3248 
       
  3249 	if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
       
  3250 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
       
  3251 		             E1000_TXD_CMD_TSE;
       
  3252 		txd_upper |= E1000_TXD_POPTS_TXSM << 8;
       
  3253 
       
  3254 		if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
       
  3255 			txd_upper |= E1000_TXD_POPTS_IXSM << 8;
       
  3256 	}
       
  3257 
       
  3258 	if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
       
  3259 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
       
  3260 		txd_upper |= E1000_TXD_POPTS_TXSM << 8;
       
  3261 	}
       
  3262 
       
  3263 	if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
       
  3264 		txd_lower |= E1000_TXD_CMD_VLE;
       
  3265 		txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
       
  3266 	}
       
  3267 
       
  3268 	i = tx_ring->next_to_use;
       
  3269 
       
  3270 	while (count--) {
       
  3271 		buffer_info = &tx_ring->buffer_info[i];
       
  3272 		tx_desc = E1000_TX_DESC(*tx_ring, i);
       
  3273 		tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
       
  3274 		tx_desc->lower.data =
       
  3275 			cpu_to_le32(txd_lower | buffer_info->length);
       
  3276 		tx_desc->upper.data = cpu_to_le32(txd_upper);
       
  3277 		if (unlikely(++i == tx_ring->count)) i = 0;
       
  3278 	}
       
  3279 
       
  3280 	tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
       
  3281 
       
  3282 	/* Force memory writes to complete before letting h/w
       
  3283 	 * know there are new descriptors to fetch.  (Only
       
  3284 	 * applicable for weak-ordered memory model archs,
       
  3285 	 * such as IA-64). */
       
  3286 	wmb();
       
  3287 
       
  3288 	tx_ring->next_to_use = i;
       
  3289 	writel(i, adapter->hw.hw_addr + tx_ring->tdt);
       
  3290 	/* we need this if more than one processor can write to our tail
       
  3291 	 * at a time, it syncronizes IO on IA64/Altix systems */
       
  3292 	mmiowb();
       
  3293 }
       
  3294 
       
  3295 /**
       
  3296  * 82547 workaround to avoid controller hang in half-duplex environment.
       
  3297  * The workaround is to avoid queuing a large packet that would span
       
  3298  * the internal Tx FIFO ring boundary by notifying the stack to resend
       
  3299  * the packet at a later time.  This gives the Tx FIFO an opportunity to
       
  3300  * flush all packets.  When that occurs, we reset the Tx FIFO pointers
       
  3301  * to the beginning of the Tx FIFO.
       
  3302  **/
       
  3303 
       
  3304 #define E1000_FIFO_HDR			0x10
       
  3305 #define E1000_82547_PAD_LEN		0x3E0
       
  3306 
       
  3307 static int
       
  3308 e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
       
  3309 {
       
  3310 	u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
       
  3311 	u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
       
  3312 
       
  3313 	skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
       
  3314 
       
  3315 	if (adapter->link_duplex != HALF_DUPLEX)
       
  3316 		goto no_fifo_stall_required;
       
  3317 
       
  3318 	if (atomic_read(&adapter->tx_fifo_stall))
       
  3319 		return 1;
       
  3320 
       
  3321 	if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
       
  3322 		atomic_set(&adapter->tx_fifo_stall, 1);
       
  3323 		return 1;
       
  3324 	}
       
  3325 
       
  3326 no_fifo_stall_required:
       
  3327 	adapter->tx_fifo_head += skb_fifo_len;
       
  3328 	if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
       
  3329 		adapter->tx_fifo_head -= adapter->tx_fifo_size;
       
  3330 	return 0;
       
  3331 }
       
  3332 
       
  3333 #define MINIMUM_DHCP_PACKET_SIZE 282
       
  3334 static int
       
  3335 e1000_transfer_dhcp_info(struct e1000_adapter *adapter, struct sk_buff *skb)
       
  3336 {
       
  3337 	struct e1000_hw *hw =  &adapter->hw;
       
  3338 	u16 length, offset;
       
  3339 	if (vlan_tx_tag_present(skb)) {
       
  3340 		if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
       
  3341 			( adapter->hw.mng_cookie.status &
       
  3342 			  E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) )
       
  3343 			return 0;
       
  3344 	}
       
  3345 	if (skb->len > MINIMUM_DHCP_PACKET_SIZE) {
       
  3346 		struct ethhdr *eth = (struct ethhdr *) skb->data;
       
  3347 		if ((htons(ETH_P_IP) == eth->h_proto)) {
       
  3348 			const struct iphdr *ip =
       
  3349 				(struct iphdr *)((u8 *)skb->data+14);
       
  3350 			if (IPPROTO_UDP == ip->protocol) {
       
  3351 				struct udphdr *udp =
       
  3352 					(struct udphdr *)((u8 *)ip +
       
  3353 						(ip->ihl << 2));
       
  3354 				if (ntohs(udp->dest) == 67) {
       
  3355 					offset = (u8 *)udp + 8 - skb->data;
       
  3356 					length = skb->len - offset;
       
  3357 
       
  3358 					return e1000_mng_write_dhcp_info(hw,
       
  3359 							(u8 *)udp + 8,
       
  3360 							length);
       
  3361 				}
       
  3362 			}
       
  3363 		}
       
  3364 	}
       
  3365 	return 0;
       
  3366 }
       
  3367 
       
  3368 static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
       
  3369 {
       
  3370 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3371 	struct e1000_tx_ring *tx_ring = adapter->tx_ring;
       
  3372 
       
  3373 	netif_stop_queue(netdev);
       
  3374 	/* Herbert's original patch had:
       
  3375 	 *  smp_mb__after_netif_stop_queue();
       
  3376 	 * but since that doesn't exist yet, just open code it. */
       
  3377 	smp_mb();
       
  3378 
       
  3379 	/* We need to check again in a case another CPU has just
       
  3380 	 * made room available. */
       
  3381 	if (likely(E1000_DESC_UNUSED(tx_ring) < size))
       
  3382 		return -EBUSY;
       
  3383 
       
  3384 	/* A reprieve! */
       
  3385 	netif_start_queue(netdev);
       
  3386 	++adapter->restart_queue;
       
  3387 	return 0;
       
  3388 }
       
  3389 
       
  3390 static int e1000_maybe_stop_tx(struct net_device *netdev,
       
  3391                                struct e1000_tx_ring *tx_ring, int size)
       
  3392 {
       
  3393 	if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
       
  3394 		return 0;
       
  3395 	return __e1000_maybe_stop_tx(netdev, size);
       
  3396 }
       
  3397 
       
  3398 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
       
  3399 static int
       
  3400 e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
       
  3401 {
       
  3402 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3403 	struct e1000_tx_ring *tx_ring;
       
  3404 	unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
       
  3405 	unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
       
  3406 	unsigned int tx_flags = 0;
       
  3407 	unsigned int len = skb->len - skb->data_len;
       
  3408 	unsigned long flags = 0;
       
  3409 	unsigned int nr_frags = 0;
       
  3410 	unsigned int mss = 0;
       
  3411 	int count = 0;
       
  3412 	int tso;
       
  3413 	unsigned int f;
       
  3414 
       
  3415 	/* This goes back to the question of how to logically map a tx queue
       
  3416 	 * to a flow.  Right now, performance is impacted slightly negatively
       
  3417 	 * if using multiple tx queues.  If the stack breaks away from a
       
  3418 	 * single qdisc implementation, we can look at this again. */
       
  3419 	tx_ring = adapter->tx_ring;
       
  3420 
       
  3421 	if (unlikely(skb->len <= 0)) {
       
  3422 		if (!adapter->ecdev)
       
  3423 			dev_kfree_skb_any(skb);
       
  3424 		return NETDEV_TX_OK;
       
  3425 	}
       
  3426 
       
  3427 	/* 82571 and newer doesn't need the workaround that limited descriptor
       
  3428 	 * length to 4kB */
       
  3429 	if (adapter->hw.mac_type >= e1000_82571)
       
  3430 		max_per_txd = 8192;
       
  3431 
       
  3432 	mss = skb_shinfo(skb)->gso_size;
       
  3433 	/* The controller does a simple calculation to
       
  3434 	 * make sure there is enough room in the FIFO before
       
  3435 	 * initiating the DMA for each buffer.  The calc is:
       
  3436 	 * 4 = ceil(buffer len/mss).  To make sure we don't
       
  3437 	 * overrun the FIFO, adjust the max buffer len if mss
       
  3438 	 * drops. */
       
  3439 	if (mss) {
       
  3440 		u8 hdr_len;
       
  3441 		max_per_txd = min(mss << 2, max_per_txd);
       
  3442 		max_txd_pwr = fls(max_per_txd) - 1;
       
  3443 
       
  3444 		/* TSO Workaround for 82571/2/3 Controllers -- if skb->data
       
  3445 		* points to just header, pull a few bytes of payload from
       
  3446 		* frags into skb->data */
       
  3447 		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
       
  3448 		if (skb->data_len && hdr_len == len) {
       
  3449 			switch (adapter->hw.mac_type) {
       
  3450 				unsigned int pull_size;
       
  3451 			case e1000_82544:
       
  3452 				/* Make sure we have room to chop off 4 bytes,
       
  3453 				 * and that the end alignment will work out to
       
  3454 				 * this hardware's requirements
       
  3455 				 * NOTE: this is a TSO only workaround
       
  3456 				 * if end byte alignment not correct move us
       
  3457 				 * into the next dword */
       
  3458 				if ((unsigned long)(skb_tail_pointer(skb) - 1) & 4)
       
  3459 					break;
       
  3460 				/* fall through */
       
  3461 			case e1000_82571:
       
  3462 			case e1000_82572:
       
  3463 			case e1000_82573:
       
  3464 			case e1000_ich8lan:
       
  3465 				pull_size = min((unsigned int)4, skb->data_len);
       
  3466 				if (!__pskb_pull_tail(skb, pull_size)) {
       
  3467 					DPRINTK(DRV, ERR,
       
  3468 						"__pskb_pull_tail failed.\n");
       
  3469 					dev_kfree_skb_any(skb);
       
  3470 					return NETDEV_TX_OK;
       
  3471 				}
       
  3472 				len = skb->len - skb->data_len;
       
  3473 				break;
       
  3474 			default:
       
  3475 				/* do nothing */
       
  3476 				break;
       
  3477 			}
       
  3478 		}
       
  3479 	}
       
  3480 
       
  3481 	/* reserve a descriptor for the offload context */
       
  3482 	if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
       
  3483 		count++;
       
  3484 	count++;
       
  3485 
       
  3486 	/* Controller Erratum workaround */
       
  3487 	if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
       
  3488 		count++;
       
  3489 
       
  3490 	count += TXD_USE_COUNT(len, max_txd_pwr);
       
  3491 
       
  3492 	if (adapter->pcix_82544)
       
  3493 		count++;
       
  3494 
       
  3495 	/* work-around for errata 10 and it applies to all controllers
       
  3496 	 * in PCI-X mode, so add one more descriptor to the count
       
  3497 	 */
       
  3498 	if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
       
  3499 			(len > 2015)))
       
  3500 		count++;
       
  3501 
       
  3502 	nr_frags = skb_shinfo(skb)->nr_frags;
       
  3503 	for (f = 0; f < nr_frags; f++)
       
  3504 		count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
       
  3505 				       max_txd_pwr);
       
  3506 	if (adapter->pcix_82544)
       
  3507 		count += nr_frags;
       
  3508 
       
  3509 
       
  3510 	if (adapter->hw.tx_pkt_filtering &&
       
  3511 	    (adapter->hw.mac_type == e1000_82573))
       
  3512 		e1000_transfer_dhcp_info(adapter, skb);
       
  3513 
       
  3514 	if (!adapter->ecdev &&
       
  3515 			!spin_trylock_irqsave(&tx_ring->tx_lock, flags)) {
       
  3516 		/* Collision - tell upper layer to requeue */
       
  3517 		return NETDEV_TX_LOCKED;
       
  3518 	}
       
  3519 
       
  3520 	/* need: count + 2 desc gap to keep tail from touching
       
  3521 	 * head, otherwise try next time */
       
  3522 	if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2))) {
       
  3523 		if (!adapter->ecdev) {
       
  3524 			spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
       
  3525 		}
       
  3526 		return NETDEV_TX_BUSY;
       
  3527 	}
       
  3528 
       
  3529 	if (unlikely(adapter->hw.mac_type == e1000_82547)) {
       
  3530 		if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
       
  3531 			if (!adapter->ecdev) {
       
  3532 				netif_stop_queue(netdev);
       
  3533 				mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
       
  3534 				spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
       
  3535 			}
       
  3536 			return NETDEV_TX_BUSY;
       
  3537 		}
       
  3538 	}
       
  3539 
       
  3540 	if (unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
       
  3541 		tx_flags |= E1000_TX_FLAGS_VLAN;
       
  3542 		tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
       
  3543 	}
       
  3544 
       
  3545 	first = tx_ring->next_to_use;
       
  3546 
       
  3547 	tso = e1000_tso(adapter, tx_ring, skb);
       
  3548 	if (tso < 0) {
       
  3549 		if (!adapter->ecdev) {
       
  3550 			dev_kfree_skb_any(skb);
       
  3551 			spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
       
  3552 		}
       
  3553 		return NETDEV_TX_OK;
       
  3554 	}
       
  3555 
       
  3556 	if (likely(tso)) {
       
  3557 		tx_ring->last_tx_tso = 1;
       
  3558 		tx_flags |= E1000_TX_FLAGS_TSO;
       
  3559 	} else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
       
  3560 		tx_flags |= E1000_TX_FLAGS_CSUM;
       
  3561 
       
  3562 	/* Old method was to assume IPv4 packet by default if TSO was enabled.
       
  3563 	 * 82571 hardware supports TSO capabilities for IPv6 as well...
       
  3564 	 * no longer assume, we must. */
       
  3565 	if (likely(skb->protocol == htons(ETH_P_IP)))
       
  3566 		tx_flags |= E1000_TX_FLAGS_IPV4;
       
  3567 
       
  3568 	e1000_tx_queue(adapter, tx_ring, tx_flags,
       
  3569 	               e1000_tx_map(adapter, tx_ring, skb, first,
       
  3570 	                            max_per_txd, nr_frags, mss));
       
  3571 
       
  3572 	netdev->trans_start = jiffies;
       
  3573 
       
  3574 	if (!adapter->ecdev) {
       
  3575 		/* Make sure there is space in the ring for the next send. */
       
  3576 		e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
       
  3577 
       
  3578 		spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
       
  3579 	}
       
  3580 	return NETDEV_TX_OK;
       
  3581 }
       
  3582 
       
  3583 /**
       
  3584  * e1000_tx_timeout - Respond to a Tx Hang
       
  3585  * @netdev: network interface device structure
       
  3586  **/
       
  3587 
       
  3588 static void
       
  3589 e1000_tx_timeout(struct net_device *netdev)
       
  3590 {
       
  3591 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3592 
       
  3593 	/* Do the reset outside of interrupt context */
       
  3594 	adapter->tx_timeout_count++;
       
  3595 	schedule_work(&adapter->reset_task);
       
  3596 }
       
  3597 
       
  3598 static void
       
  3599 e1000_reset_task(struct work_struct *work)
       
  3600 {
       
  3601 	struct e1000_adapter *adapter =
       
  3602 		container_of(work, struct e1000_adapter, reset_task);
       
  3603 
       
  3604 	e1000_reinit_locked(adapter);
       
  3605 }
       
  3606 
       
  3607 /**
       
  3608  * e1000_get_stats - Get System Network Statistics
       
  3609  * @netdev: network interface device structure
       
  3610  *
       
  3611  * Returns the address of the device statistics structure.
       
  3612  * The statistics are actually updated from the timer callback.
       
  3613  **/
       
  3614 
       
  3615 static struct net_device_stats *
       
  3616 e1000_get_stats(struct net_device *netdev)
       
  3617 {
       
  3618 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3619 
       
  3620 	/* only return the current stats */
       
  3621 	return &adapter->net_stats;
       
  3622 }
       
  3623 
       
  3624 /**
       
  3625  * e1000_change_mtu - Change the Maximum Transfer Unit
       
  3626  * @netdev: network interface device structure
       
  3627  * @new_mtu: new value for maximum frame size
       
  3628  *
       
  3629  * Returns 0 on success, negative on failure
       
  3630  **/
       
  3631 
       
  3632 static int
       
  3633 e1000_change_mtu(struct net_device *netdev, int new_mtu)
       
  3634 {
       
  3635 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3636 	int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
       
  3637 	u16 eeprom_data = 0;
       
  3638 
       
  3639 	if (adapter->ecdev)
       
  3640 		return -EBUSY;
       
  3641 
       
  3642 	if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
       
  3643 	    (max_frame > MAX_JUMBO_FRAME_SIZE)) {
       
  3644 		DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
       
  3645 		return -EINVAL;
       
  3646 	}
       
  3647 
       
  3648 	/* Adapter-specific max frame size limits. */
       
  3649 	switch (adapter->hw.mac_type) {
       
  3650 	case e1000_undefined ... e1000_82542_rev2_1:
       
  3651 	case e1000_ich8lan:
       
  3652 		if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
       
  3653 			DPRINTK(PROBE, ERR, "Jumbo Frames not supported.\n");
       
  3654 			return -EINVAL;
       
  3655 		}
       
  3656 		break;
       
  3657 	case e1000_82573:
       
  3658 		/* Jumbo Frames not supported if:
       
  3659 		 * - this is not an 82573L device
       
  3660 		 * - ASPM is enabled in any way (0x1A bits 3:2) */
       
  3661 		e1000_read_eeprom(&adapter->hw, EEPROM_INIT_3GIO_3, 1,
       
  3662 		                  &eeprom_data);
       
  3663 		if ((adapter->hw.device_id != E1000_DEV_ID_82573L) ||
       
  3664 		    (eeprom_data & EEPROM_WORD1A_ASPM_MASK)) {
       
  3665 			if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
       
  3666 				DPRINTK(PROBE, ERR,
       
  3667 			            	"Jumbo Frames not supported.\n");
       
  3668 				return -EINVAL;
       
  3669 			}
       
  3670 			break;
       
  3671 		}
       
  3672 		/* ERT will be enabled later to enable wire speed receives */
       
  3673 
       
  3674 		/* fall through to get support */
       
  3675 	case e1000_82571:
       
  3676 	case e1000_82572:
       
  3677 	case e1000_80003es2lan:
       
  3678 #define MAX_STD_JUMBO_FRAME_SIZE 9234
       
  3679 		if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
       
  3680 			DPRINTK(PROBE, ERR, "MTU > 9216 not supported.\n");
       
  3681 			return -EINVAL;
       
  3682 		}
       
  3683 		break;
       
  3684 	default:
       
  3685 		/* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
       
  3686 		break;
       
  3687 	}
       
  3688 
       
  3689 	/* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
       
  3690 	 * means we reserve 2 more, this pushes us to allocate from the next
       
  3691 	 * larger slab size
       
  3692 	 * i.e. RXBUFFER_2048 --> size-4096 slab */
       
  3693 
       
  3694 	if (max_frame <= E1000_RXBUFFER_256)
       
  3695 		adapter->rx_buffer_len = E1000_RXBUFFER_256;
       
  3696 	else if (max_frame <= E1000_RXBUFFER_512)
       
  3697 		adapter->rx_buffer_len = E1000_RXBUFFER_512;
       
  3698 	else if (max_frame <= E1000_RXBUFFER_1024)
       
  3699 		adapter->rx_buffer_len = E1000_RXBUFFER_1024;
       
  3700 	else if (max_frame <= E1000_RXBUFFER_2048)
       
  3701 		adapter->rx_buffer_len = E1000_RXBUFFER_2048;
       
  3702 	else if (max_frame <= E1000_RXBUFFER_4096)
       
  3703 		adapter->rx_buffer_len = E1000_RXBUFFER_4096;
       
  3704 	else if (max_frame <= E1000_RXBUFFER_8192)
       
  3705 		adapter->rx_buffer_len = E1000_RXBUFFER_8192;
       
  3706 	else if (max_frame <= E1000_RXBUFFER_16384)
       
  3707 		adapter->rx_buffer_len = E1000_RXBUFFER_16384;
       
  3708 
       
  3709 	/* adjust allocation if LPE protects us, and we aren't using SBP */
       
  3710 	if (!adapter->hw.tbi_compatibility_on &&
       
  3711 	    ((max_frame == MAXIMUM_ETHERNET_FRAME_SIZE) ||
       
  3712 	     (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
       
  3713 		adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
       
  3714 
       
  3715 	netdev->mtu = new_mtu;
       
  3716 	adapter->hw.max_frame_size = max_frame;
       
  3717 
       
  3718 	if (netif_running(netdev))
       
  3719 		e1000_reinit_locked(adapter);
       
  3720 
       
  3721 	return 0;
       
  3722 }
       
  3723 
       
  3724 /**
       
  3725  * e1000_update_stats - Update the board statistics counters
       
  3726  * @adapter: board private structure
       
  3727  **/
       
  3728 
       
  3729 void
       
  3730 e1000_update_stats(struct e1000_adapter *adapter)
       
  3731 {
       
  3732 	struct e1000_hw *hw = &adapter->hw;
       
  3733 	struct pci_dev *pdev = adapter->pdev;
       
  3734 	unsigned long flags = 0;
       
  3735 	u16 phy_tmp;
       
  3736 
       
  3737 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
       
  3738 
       
  3739 	/*
       
  3740 	 * Prevent stats update while adapter is being reset, or if the pci
       
  3741 	 * connection is down.
       
  3742 	 */
       
  3743 	if (adapter->link_speed == 0)
       
  3744 		return;
       
  3745 	if (pci_channel_offline(pdev))
       
  3746 		return;
       
  3747 
       
  3748 	if (!adapter->ecdev)
       
  3749 		spin_lock_irqsave(&adapter->stats_lock, flags);
       
  3750 
       
  3751 	/* these counters are modified from e1000_tbi_adjust_stats,
       
  3752 	 * called from the interrupt context, so they must only
       
  3753 	 * be written while holding adapter->stats_lock
       
  3754 	 */
       
  3755 
       
  3756 	adapter->stats.crcerrs += E1000_READ_REG(hw, CRCERRS);
       
  3757 	adapter->stats.gprc += E1000_READ_REG(hw, GPRC);
       
  3758 	adapter->stats.gorcl += E1000_READ_REG(hw, GORCL);
       
  3759 	adapter->stats.gorch += E1000_READ_REG(hw, GORCH);
       
  3760 	adapter->stats.bprc += E1000_READ_REG(hw, BPRC);
       
  3761 	adapter->stats.mprc += E1000_READ_REG(hw, MPRC);
       
  3762 	adapter->stats.roc += E1000_READ_REG(hw, ROC);
       
  3763 
       
  3764 	if (adapter->hw.mac_type != e1000_ich8lan) {
       
  3765 		adapter->stats.prc64 += E1000_READ_REG(hw, PRC64);
       
  3766 		adapter->stats.prc127 += E1000_READ_REG(hw, PRC127);
       
  3767 		adapter->stats.prc255 += E1000_READ_REG(hw, PRC255);
       
  3768 		adapter->stats.prc511 += E1000_READ_REG(hw, PRC511);
       
  3769 		adapter->stats.prc1023 += E1000_READ_REG(hw, PRC1023);
       
  3770 		adapter->stats.prc1522 += E1000_READ_REG(hw, PRC1522);
       
  3771 	}
       
  3772 
       
  3773 	adapter->stats.symerrs += E1000_READ_REG(hw, SYMERRS);
       
  3774 	adapter->stats.mpc += E1000_READ_REG(hw, MPC);
       
  3775 	adapter->stats.scc += E1000_READ_REG(hw, SCC);
       
  3776 	adapter->stats.ecol += E1000_READ_REG(hw, ECOL);
       
  3777 	adapter->stats.mcc += E1000_READ_REG(hw, MCC);
       
  3778 	adapter->stats.latecol += E1000_READ_REG(hw, LATECOL);
       
  3779 	adapter->stats.dc += E1000_READ_REG(hw, DC);
       
  3780 	adapter->stats.sec += E1000_READ_REG(hw, SEC);
       
  3781 	adapter->stats.rlec += E1000_READ_REG(hw, RLEC);
       
  3782 	adapter->stats.xonrxc += E1000_READ_REG(hw, XONRXC);
       
  3783 	adapter->stats.xontxc += E1000_READ_REG(hw, XONTXC);
       
  3784 	adapter->stats.xoffrxc += E1000_READ_REG(hw, XOFFRXC);
       
  3785 	adapter->stats.xofftxc += E1000_READ_REG(hw, XOFFTXC);
       
  3786 	adapter->stats.fcruc += E1000_READ_REG(hw, FCRUC);
       
  3787 	adapter->stats.gptc += E1000_READ_REG(hw, GPTC);
       
  3788 	adapter->stats.gotcl += E1000_READ_REG(hw, GOTCL);
       
  3789 	adapter->stats.gotch += E1000_READ_REG(hw, GOTCH);
       
  3790 	adapter->stats.rnbc += E1000_READ_REG(hw, RNBC);
       
  3791 	adapter->stats.ruc += E1000_READ_REG(hw, RUC);
       
  3792 	adapter->stats.rfc += E1000_READ_REG(hw, RFC);
       
  3793 	adapter->stats.rjc += E1000_READ_REG(hw, RJC);
       
  3794 	adapter->stats.torl += E1000_READ_REG(hw, TORL);
       
  3795 	adapter->stats.torh += E1000_READ_REG(hw, TORH);
       
  3796 	adapter->stats.totl += E1000_READ_REG(hw, TOTL);
       
  3797 	adapter->stats.toth += E1000_READ_REG(hw, TOTH);
       
  3798 	adapter->stats.tpr += E1000_READ_REG(hw, TPR);
       
  3799 
       
  3800 	if (adapter->hw.mac_type != e1000_ich8lan) {
       
  3801 		adapter->stats.ptc64 += E1000_READ_REG(hw, PTC64);
       
  3802 		adapter->stats.ptc127 += E1000_READ_REG(hw, PTC127);
       
  3803 		adapter->stats.ptc255 += E1000_READ_REG(hw, PTC255);
       
  3804 		adapter->stats.ptc511 += E1000_READ_REG(hw, PTC511);
       
  3805 		adapter->stats.ptc1023 += E1000_READ_REG(hw, PTC1023);
       
  3806 		adapter->stats.ptc1522 += E1000_READ_REG(hw, PTC1522);
       
  3807 	}
       
  3808 
       
  3809 	adapter->stats.mptc += E1000_READ_REG(hw, MPTC);
       
  3810 	adapter->stats.bptc += E1000_READ_REG(hw, BPTC);
       
  3811 
       
  3812 	/* used for adaptive IFS */
       
  3813 
       
  3814 	hw->tx_packet_delta = E1000_READ_REG(hw, TPT);
       
  3815 	adapter->stats.tpt += hw->tx_packet_delta;
       
  3816 	hw->collision_delta = E1000_READ_REG(hw, COLC);
       
  3817 	adapter->stats.colc += hw->collision_delta;
       
  3818 
       
  3819 	if (hw->mac_type >= e1000_82543) {
       
  3820 		adapter->stats.algnerrc += E1000_READ_REG(hw, ALGNERRC);
       
  3821 		adapter->stats.rxerrc += E1000_READ_REG(hw, RXERRC);
       
  3822 		adapter->stats.tncrs += E1000_READ_REG(hw, TNCRS);
       
  3823 		adapter->stats.cexterr += E1000_READ_REG(hw, CEXTERR);
       
  3824 		adapter->stats.tsctc += E1000_READ_REG(hw, TSCTC);
       
  3825 		adapter->stats.tsctfc += E1000_READ_REG(hw, TSCTFC);
       
  3826 	}
       
  3827 	if (hw->mac_type > e1000_82547_rev_2) {
       
  3828 		adapter->stats.iac += E1000_READ_REG(hw, IAC);
       
  3829 		adapter->stats.icrxoc += E1000_READ_REG(hw, ICRXOC);
       
  3830 
       
  3831 		if (adapter->hw.mac_type != e1000_ich8lan) {
       
  3832 			adapter->stats.icrxptc += E1000_READ_REG(hw, ICRXPTC);
       
  3833 			adapter->stats.icrxatc += E1000_READ_REG(hw, ICRXATC);
       
  3834 			adapter->stats.ictxptc += E1000_READ_REG(hw, ICTXPTC);
       
  3835 			adapter->stats.ictxatc += E1000_READ_REG(hw, ICTXATC);
       
  3836 			adapter->stats.ictxqec += E1000_READ_REG(hw, ICTXQEC);
       
  3837 			adapter->stats.ictxqmtc += E1000_READ_REG(hw, ICTXQMTC);
       
  3838 			adapter->stats.icrxdmtc += E1000_READ_REG(hw, ICRXDMTC);
       
  3839 		}
       
  3840 	}
       
  3841 
       
  3842 	/* Fill out the OS statistics structure */
       
  3843 	adapter->net_stats.multicast = adapter->stats.mprc;
       
  3844 	adapter->net_stats.collisions = adapter->stats.colc;
       
  3845 
       
  3846 	/* Rx Errors */
       
  3847 
       
  3848 	/* RLEC on some newer hardware can be incorrect so build
       
  3849 	* our own version based on RUC and ROC */
       
  3850 	adapter->net_stats.rx_errors = adapter->stats.rxerrc +
       
  3851 		adapter->stats.crcerrs + adapter->stats.algnerrc +
       
  3852 		adapter->stats.ruc + adapter->stats.roc +
       
  3853 		adapter->stats.cexterr;
       
  3854 	adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
       
  3855 	adapter->net_stats.rx_length_errors = adapter->stats.rlerrc;
       
  3856 	adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
       
  3857 	adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
       
  3858 	adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
       
  3859 
       
  3860 	/* Tx Errors */
       
  3861 	adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
       
  3862 	adapter->net_stats.tx_errors = adapter->stats.txerrc;
       
  3863 	adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
       
  3864 	adapter->net_stats.tx_window_errors = adapter->stats.latecol;
       
  3865 	adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
       
  3866 	if (adapter->hw.bad_tx_carr_stats_fd &&
       
  3867 	    adapter->link_duplex == FULL_DUPLEX) {
       
  3868 		adapter->net_stats.tx_carrier_errors = 0;
       
  3869 		adapter->stats.tncrs = 0;
       
  3870 	}
       
  3871 
       
  3872 	/* Tx Dropped needs to be maintained elsewhere */
       
  3873 
       
  3874 	/* Phy Stats */
       
  3875 	if (hw->media_type == e1000_media_type_copper) {
       
  3876 		if ((adapter->link_speed == SPEED_1000) &&
       
  3877 		   (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
       
  3878 			phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
       
  3879 			adapter->phy_stats.idle_errors += phy_tmp;
       
  3880 		}
       
  3881 
       
  3882 		if ((hw->mac_type <= e1000_82546) &&
       
  3883 		   (hw->phy_type == e1000_phy_m88) &&
       
  3884 		   !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
       
  3885 			adapter->phy_stats.receive_errors += phy_tmp;
       
  3886 	}
       
  3887 
       
  3888 	/* Management Stats */
       
  3889 	if (adapter->hw.has_smbus) {
       
  3890 		adapter->stats.mgptc += E1000_READ_REG(hw, MGTPTC);
       
  3891 		adapter->stats.mgprc += E1000_READ_REG(hw, MGTPRC);
       
  3892 		adapter->stats.mgpdc += E1000_READ_REG(hw, MGTPDC);
       
  3893 	}
       
  3894 
       
  3895 	if (!adapter->ecdev)
       
  3896 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  3897 }
       
  3898 
       
  3899 void ec_poll(struct net_device *netdev)
       
  3900 {
       
  3901     struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3902 
       
  3903     if (jiffies - adapter->ec_watchdog_jiffies >= 2 * HZ) {
       
  3904         e1000_watchdog((unsigned long) adapter);
       
  3905         adapter->ec_watchdog_jiffies = jiffies;
       
  3906     }
       
  3907 
       
  3908 #ifdef CONFIG_PCI_MSI
       
  3909 	e1000_intr_msi(0, netdev);
       
  3910 #else
       
  3911     e1000_intr(0, netdev);
       
  3912 #endif
       
  3913 }
       
  3914 
       
  3915 /**
       
  3916  * e1000_intr_msi - Interrupt Handler
       
  3917  * @irq: interrupt number
       
  3918  * @data: pointer to a network interface device structure
       
  3919  **/
       
  3920 
       
  3921 static irqreturn_t
       
  3922 e1000_intr_msi(int irq, void *data)
       
  3923 {
       
  3924 	struct net_device *netdev = data;
       
  3925 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3926 	struct e1000_hw *hw = &adapter->hw;
       
  3927 	
       
  3928 	int i;
       
  3929 
       
  3930 	if (adapter->ecdev) {
       
  3931 #ifdef CONFIG_E1000_NAPI
       
  3932 		int ec_work_done = 0;
       
  3933 #endif
       
  3934 		for (i = 0; i < E1000_MAX_INTR; i++) {
       
  3935 #ifdef CONFIG_E1000_NAPI
       
  3936 			if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring,
       
  3937                             &ec_work_done, 100) &
       
  3938 						!e1000_clean_tx_irq(adapter, adapter->tx_ring))) {
       
  3939 #else
       
  3940 			if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
       
  3941 						!e1000_clean_tx_irq(adapter, adapter->tx_ring))) {
       
  3942 #endif
       
  3943 				break;
       
  3944 			}
       
  3945 		}
       
  3946 	} else {
       
  3947 		u32 icr = E1000_READ_REG(hw, ICR);
       
  3948 
       
  3949 		/* in NAPI mode read ICR disables interrupts using IAM */
       
  3950 
       
  3951 		if (icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC)) {
       
  3952 			hw->get_link_status = 1;
       
  3953 			/* 80003ES2LAN workaround-- For packet buffer work-around on
       
  3954 			 * link down event; disable receives here in the ISR and reset
       
  3955 			 * adapter in watchdog */
       
  3956 			if (netif_carrier_ok(netdev) &&
       
  3957 			    (adapter->hw.mac_type == e1000_80003es2lan)) {
       
  3958 				/* disable receives */
       
  3959 				u32 rctl = E1000_READ_REG(hw, RCTL);
       
  3960 				E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
       
  3961 			}
       
  3962 			/* guard against interrupt when we're going down */
       
  3963 			if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  3964 				mod_timer(&adapter->watchdog_timer, jiffies + 1);
       
  3965 		}
       
  3966 
       
  3967 #ifdef CONFIG_E1000_NAPI
       
  3968 		if (likely(netif_rx_schedule_prep(netdev, &adapter->napi))) {
       
  3969 			adapter->total_tx_bytes = 0;
       
  3970 			adapter->total_tx_packets = 0;
       
  3971 			adapter->total_rx_bytes = 0;
       
  3972 			adapter->total_rx_packets = 0;
       
  3973 			__netif_rx_schedule(netdev, &adapter->napi);
       
  3974 		} else
       
  3975 			e1000_irq_enable(adapter);
       
  3976 #else
       
  3977 		adapter->total_tx_bytes = 0;
       
  3978 		adapter->total_rx_bytes = 0;
       
  3979 		adapter->total_tx_packets = 0;
       
  3980 		adapter->total_rx_packets = 0;
       
  3981 
       
  3982 		for (i = 0; i < E1000_MAX_INTR; i++)
       
  3983 			if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
       
  3984 			   !e1000_clean_tx_irq(adapter, adapter->tx_ring)))
       
  3985 				break;
       
  3986 
       
  3987 		if (likely(adapter->itr_setting & 3))
       
  3988 			e1000_set_itr(adapter);
       
  3989 #endif
       
  3990 	}
       
  3991 
       
  3992 	return IRQ_HANDLED;
       
  3993 }
       
  3994 
       
  3995 /**
       
  3996  * e1000_intr - Interrupt Handler
       
  3997  * @irq: interrupt number
       
  3998  * @data: pointer to a network interface device structure
       
  3999  **/
       
  4000 
       
  4001 static irqreturn_t
       
  4002 e1000_intr(int irq, void *data)
       
  4003 {
       
  4004 	struct net_device *netdev = data;
       
  4005 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  4006 	struct e1000_hw *hw = &adapter->hw;
       
  4007 	u32 rctl, icr = E1000_READ_REG(hw, ICR);
       
  4008 	int i;
       
  4009 	if (unlikely(!icr))
       
  4010 		return IRQ_NONE;  /* Not our interrupt */
       
  4011 
       
  4012 #ifdef CONFIG_E1000_NAPI
       
  4013 	/* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
       
  4014 	 * not set, then the adapter didn't send an interrupt */
       
  4015 	if (unlikely(hw->mac_type >= e1000_82571 &&
       
  4016 	             !(icr & E1000_ICR_INT_ASSERTED)))
       
  4017 		return IRQ_NONE;
       
  4018 
       
  4019 	/* Interrupt Auto-Mask...upon reading ICR, interrupts are masked.  No
       
  4020 	 * need for the IMC write */
       
  4021 #endif
       
  4022 
       
  4023 	if (!adapter->ecdev && unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
       
  4024 		hw->get_link_status = 1;
       
  4025 		/* 80003ES2LAN workaround--
       
  4026 		 * For packet buffer work-around on link down event;
       
  4027 		 * disable receives here in the ISR and
       
  4028 		 * reset adapter in watchdog
       
  4029 		 */
       
  4030 		if (netif_carrier_ok(netdev) &&
       
  4031 		    (adapter->hw.mac_type == e1000_80003es2lan)) {
       
  4032 			/* disable receives */
       
  4033 			rctl = E1000_READ_REG(hw, RCTL);
       
  4034 			E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
       
  4035 		}
       
  4036 		/* guard against interrupt when we're going down */
       
  4037 		if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  4038 			mod_timer(&adapter->watchdog_timer, jiffies + 1);
       
  4039 	}
       
  4040 
       
  4041 	if (adapter->ecdev) {
       
  4042 #ifdef CONFIG_E1000_NAPI
       
  4043 			int ec_work_done = 0;
       
  4044 #endif
       
  4045 			for (i = 0; i < E1000_MAX_INTR; i++) {
       
  4046 #ifdef CONFIG_E1000_NAPI
       
  4047 				if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring,
       
  4048 		                    &ec_work_done, 100) &
       
  4049 							!e1000_clean_tx_irq(adapter, adapter->tx_ring))) {
       
  4050 #else
       
  4051 				if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
       
  4052 							!e1000_clean_tx_irq(adapter, adapter->tx_ring))) {
       
  4053 #endif
       
  4054 					break;
       
  4055 				}
       
  4056 			}
       
  4057 	} else {
       
  4058 #ifdef CONFIG_E1000_NAPI
       
  4059 		if (unlikely(hw->mac_type < e1000_82571)) {
       
  4060 			/* disable interrupts, without the synchronize_irq bit */
       
  4061 			E1000_WRITE_REG(hw, IMC, ~0);
       
  4062 			E1000_WRITE_FLUSH(hw);
       
  4063 		}
       
  4064 		if (likely(netif_rx_schedule_prep(netdev, &adapter->napi))) {
       
  4065 			adapter->total_tx_bytes = 0;
       
  4066 			adapter->total_tx_packets = 0;
       
  4067 			adapter->total_rx_bytes = 0;
       
  4068 			adapter->total_rx_packets = 0;
       
  4069 			__netif_rx_schedule(netdev, &adapter->napi);
       
  4070 		} else
       
  4071 			/* this really should not happen! if it does it is basically a
       
  4072 			 * bug, but not a hard error, so enable ints and continue */
       
  4073 			e1000_irq_enable(adapter);
       
  4074 #else
       
  4075 		/* Writing IMC and IMS is needed for 82547.
       
  4076 		 * Due to Hub Link bus being occupied, an interrupt
       
  4077 		 * de-assertion message is not able to be sent.
       
  4078 		 * When an interrupt assertion message is generated later,
       
  4079 		 * two messages are re-ordered and sent out.
       
  4080 		 * That causes APIC to think 82547 is in de-assertion
       
  4081 		 * state, while 82547 is in assertion state, resulting
       
  4082 		 * in dead lock. Writing IMC forces 82547 into
       
  4083 		 * de-assertion state.
       
  4084 		 */
       
  4085 		if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
       
  4086 			E1000_WRITE_REG(hw, IMC, ~0);
       
  4087 
       
  4088 		adapter->total_tx_bytes = 0;
       
  4089 		adapter->total_rx_bytes = 0;
       
  4090 		adapter->total_tx_packets = 0;
       
  4091 		adapter->total_rx_packets = 0;
       
  4092 
       
  4093 		for (i = 0; i < E1000_MAX_INTR; i++)
       
  4094 			if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
       
  4095 			   !e1000_clean_tx_irq(adapter, adapter->tx_ring)))
       
  4096 				break;
       
  4097 
       
  4098 		if (likely(adapter->itr_setting & 3))
       
  4099 			e1000_set_itr(adapter);
       
  4100 
       
  4101 		if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
       
  4102 			e1000_irq_enable(adapter);
       
  4103 
       
  4104 #endif
       
  4105 	}
       
  4106 	
       
  4107 	return IRQ_HANDLED;
       
  4108 }
       
  4109 
       
  4110 #ifdef CONFIG_E1000_NAPI
       
  4111 /**
       
  4112  * e1000_clean - NAPI Rx polling callback
       
  4113  * @adapter: board private structure
       
  4114  **/
       
  4115 
       
  4116 static int
       
  4117 e1000_clean(struct napi_struct *napi, int budget) // EtherCAT: never called
       
  4118 {
       
  4119 	struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter, napi);
       
  4120 	struct net_device *poll_dev = adapter->netdev;
       
  4121 	int tx_cleaned = 0, work_done = 0;
       
  4122 
       
  4123 	/* Must NOT use netdev_priv macro here. */
       
  4124 	adapter = poll_dev->priv;
       
  4125 
       
  4126 	/* e1000_clean is called per-cpu.  This lock protects
       
  4127 	 * tx_ring[0] from being cleaned by multiple cpus
       
  4128 	 * simultaneously.  A failure obtaining the lock means
       
  4129 	 * tx_ring[0] is currently being cleaned anyway. */
       
  4130 	if (spin_trylock(&adapter->tx_queue_lock)) {
       
  4131 		tx_cleaned = e1000_clean_tx_irq(adapter,
       
  4132 						&adapter->tx_ring[0]);
       
  4133 		spin_unlock(&adapter->tx_queue_lock);
       
  4134 	}
       
  4135 
       
  4136 	adapter->clean_rx(adapter, &adapter->rx_ring[0],
       
  4137 	                  &work_done, budget);
       
  4138 
       
  4139 	if (tx_cleaned)
       
  4140 		work_done = budget;
       
  4141 
       
  4142 	/* If budget not fully consumed, exit the polling mode */
       
  4143 	if (work_done < budget) {
       
  4144 		if (likely(adapter->itr_setting & 3))
       
  4145 			e1000_set_itr(adapter);
       
  4146 		netif_rx_complete(poll_dev, napi);
       
  4147 		e1000_irq_enable(adapter);
       
  4148 	}
       
  4149 
       
  4150 	return work_done;
       
  4151 }
       
  4152 
       
  4153 #endif
       
  4154 /**
       
  4155  * e1000_clean_tx_irq - Reclaim resources after transmit completes
       
  4156  * @adapter: board private structure
       
  4157  **/
       
  4158 
       
  4159 static bool
       
  4160 e1000_clean_tx_irq(struct e1000_adapter *adapter,
       
  4161                    struct e1000_tx_ring *tx_ring)
       
  4162 {
       
  4163 	struct net_device *netdev = adapter->netdev;
       
  4164 	struct e1000_tx_desc *tx_desc, *eop_desc;
       
  4165 	struct e1000_buffer *buffer_info;
       
  4166 	unsigned int i, eop;
       
  4167 #ifdef CONFIG_E1000_NAPI
       
  4168 	unsigned int count = 0;
       
  4169 #endif
       
  4170 	bool cleaned = false;
       
  4171 	unsigned int total_tx_bytes=0, total_tx_packets=0;
       
  4172 
       
  4173 	i = tx_ring->next_to_clean;
       
  4174 	eop = tx_ring->buffer_info[i].next_to_watch;
       
  4175 	eop_desc = E1000_TX_DESC(*tx_ring, eop);
       
  4176 
       
  4177 	while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
       
  4178 		for (cleaned = false; !cleaned; ) {
       
  4179 			tx_desc = E1000_TX_DESC(*tx_ring, i);
       
  4180 			buffer_info = &tx_ring->buffer_info[i];
       
  4181 			cleaned = (i == eop);
       
  4182 
       
  4183 			if (cleaned) {
       
  4184 				struct sk_buff *skb = buffer_info->skb;
       
  4185 				unsigned int segs, bytecount;
       
  4186 				segs = skb_shinfo(skb)->gso_segs ?: 1;
       
  4187 				/* multiply data chunks by size of headers */
       
  4188 				bytecount = ((segs - 1) * skb_headlen(skb)) +
       
  4189 				            skb->len;
       
  4190 				total_tx_packets += segs;
       
  4191 				total_tx_bytes += bytecount;
       
  4192 			}
       
  4193 			e1000_unmap_and_free_tx_resource(adapter, buffer_info);
       
  4194 			tx_desc->upper.data = 0;
       
  4195 
       
  4196 			if (unlikely(++i == tx_ring->count)) i = 0;
       
  4197 		}
       
  4198 
       
  4199 		eop = tx_ring->buffer_info[i].next_to_watch;
       
  4200 		eop_desc = E1000_TX_DESC(*tx_ring, eop);
       
  4201 #ifdef CONFIG_E1000_NAPI
       
  4202 #define E1000_TX_WEIGHT 64
       
  4203 		/* weight of a sort for tx, to avoid endless transmit cleanup */
       
  4204 		if (count++ == E1000_TX_WEIGHT) break;
       
  4205 #endif
       
  4206 	}
       
  4207 
       
  4208 	tx_ring->next_to_clean = i;
       
  4209 
       
  4210 #define TX_WAKE_THRESHOLD 32
       
  4211 	if (!adapter->ecdev && unlikely(cleaned && netif_carrier_ok(netdev) &&
       
  4212 		     E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
       
  4213 		/* Make sure that anybody stopping the queue after this
       
  4214 		 * sees the new next_to_clean.
       
  4215 		 */
       
  4216 		smp_mb();
       
  4217 		if (netif_queue_stopped(netdev)) {
       
  4218 			netif_wake_queue(netdev);
       
  4219 			++adapter->restart_queue;
       
  4220 		}
       
  4221 	}
       
  4222 
       
  4223 	if (!adapter->ecdev && adapter->detect_tx_hung) {
       
  4224 		/* Detect a transmit hang in hardware, this serializes the
       
  4225 		 * check with the clearing of time_stamp and movement of i */
       
  4226 		adapter->detect_tx_hung = false;
       
  4227 		if (tx_ring->buffer_info[eop].dma &&
       
  4228 		    time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
       
  4229 		               (adapter->tx_timeout_factor * HZ))
       
  4230 		    && !(E1000_READ_REG(&adapter->hw, STATUS) &
       
  4231 		         E1000_STATUS_TXOFF)) {
       
  4232 
       
  4233 			/* detected Tx unit hang */
       
  4234 			DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
       
  4235 					"  Tx Queue             <%lu>\n"
       
  4236 					"  TDH                  <%x>\n"
       
  4237 					"  TDT                  <%x>\n"
       
  4238 					"  next_to_use          <%x>\n"
       
  4239 					"  next_to_clean        <%x>\n"
       
  4240 					"buffer_info[next_to_clean]\n"
       
  4241 					"  time_stamp           <%lx>\n"
       
  4242 					"  next_to_watch        <%x>\n"
       
  4243 					"  jiffies              <%lx>\n"
       
  4244 					"  next_to_watch.status <%x>\n",
       
  4245 				(unsigned long)((tx_ring - adapter->tx_ring) /
       
  4246 					sizeof(struct e1000_tx_ring)),
       
  4247 				readl(adapter->hw.hw_addr + tx_ring->tdh),
       
  4248 				readl(adapter->hw.hw_addr + tx_ring->tdt),
       
  4249 				tx_ring->next_to_use,
       
  4250 				tx_ring->next_to_clean,
       
  4251 				tx_ring->buffer_info[eop].time_stamp,
       
  4252 				eop,
       
  4253 				jiffies,
       
  4254 				eop_desc->upper.fields.status);
       
  4255 			netif_stop_queue(netdev);
       
  4256 		}
       
  4257 	}
       
  4258 	adapter->total_tx_bytes += total_tx_bytes;
       
  4259 	adapter->total_tx_packets += total_tx_packets;
       
  4260 	adapter->net_stats.tx_bytes += total_tx_bytes;
       
  4261 	adapter->net_stats.tx_packets += total_tx_packets;
       
  4262 	return cleaned;
       
  4263 }
       
  4264 
       
  4265 /**
       
  4266  * e1000_rx_checksum - Receive Checksum Offload for 82543
       
  4267  * @adapter:     board private structure
       
  4268  * @status_err:  receive descriptor status and error fields
       
  4269  * @csum:        receive descriptor csum field
       
  4270  * @sk_buff:     socket buffer with received data
       
  4271  **/
       
  4272 
       
  4273 static void
       
  4274 e1000_rx_checksum(struct e1000_adapter *adapter,
       
  4275 		  u32 status_err, u32 csum,
       
  4276 		  struct sk_buff *skb)
       
  4277 {
       
  4278 	u16 status = (u16)status_err;
       
  4279 	u8 errors = (u8)(status_err >> 24);
       
  4280 	skb->ip_summed = CHECKSUM_NONE;
       
  4281 
       
  4282 	/* 82543 or newer only */
       
  4283 	if (unlikely(adapter->hw.mac_type < e1000_82543)) return;
       
  4284 	/* Ignore Checksum bit is set */
       
  4285 	if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
       
  4286 	/* TCP/UDP checksum error bit is set */
       
  4287 	if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
       
  4288 		/* let the stack verify checksum errors */
       
  4289 		adapter->hw_csum_err++;
       
  4290 		return;
       
  4291 	}
       
  4292 	/* TCP/UDP Checksum has not been calculated */
       
  4293 	if (adapter->hw.mac_type <= e1000_82547_rev_2) {
       
  4294 		if (!(status & E1000_RXD_STAT_TCPCS))
       
  4295 			return;
       
  4296 	} else {
       
  4297 		if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
       
  4298 			return;
       
  4299 	}
       
  4300 	/* It must be a TCP or UDP packet with a valid checksum */
       
  4301 	if (likely(status & E1000_RXD_STAT_TCPCS)) {
       
  4302 		/* TCP checksum is good */
       
  4303 		skb->ip_summed = CHECKSUM_UNNECESSARY;
       
  4304 	} else if (adapter->hw.mac_type > e1000_82547_rev_2) {
       
  4305 		/* IP fragment with UDP payload */
       
  4306 		/* Hardware complements the payload checksum, so we undo it
       
  4307 		 * and then put the value in host order for further stack use.
       
  4308 		 */
       
  4309 		__sum16 sum = (__force __sum16)htons(csum);
       
  4310 		skb->csum = csum_unfold(~sum);
       
  4311 		skb->ip_summed = CHECKSUM_COMPLETE;
       
  4312 	}
       
  4313 	adapter->hw_csum_good++;
       
  4314 }
       
  4315 
       
  4316 /**
       
  4317  * e1000_clean_rx_irq - Send received data up the network stack; legacy
       
  4318  * @adapter: board private structure
       
  4319  **/
       
  4320 
       
  4321 static bool
       
  4322 #ifdef CONFIG_E1000_NAPI
       
  4323 e1000_clean_rx_irq(struct e1000_adapter *adapter,
       
  4324                    struct e1000_rx_ring *rx_ring,
       
  4325                    int *work_done, int work_to_do)
       
  4326 #else
       
  4327 e1000_clean_rx_irq(struct e1000_adapter *adapter,
       
  4328                    struct e1000_rx_ring *rx_ring)
       
  4329 #endif
       
  4330 {
       
  4331 	struct net_device *netdev = adapter->netdev;
       
  4332 	struct pci_dev *pdev = adapter->pdev;
       
  4333 	struct e1000_rx_desc *rx_desc, *next_rxd;
       
  4334 	struct e1000_buffer *buffer_info, *next_buffer;
       
  4335 	unsigned long flags;
       
  4336 	u32 length;
       
  4337 	u8 last_byte;
       
  4338 	unsigned int i;
       
  4339 	int cleaned_count = 0;
       
  4340 	bool cleaned = false;
       
  4341 	unsigned int total_rx_bytes=0, total_rx_packets=0;
       
  4342 
       
  4343 	i = rx_ring->next_to_clean;
       
  4344 	rx_desc = E1000_RX_DESC(*rx_ring, i);
       
  4345 	buffer_info = &rx_ring->buffer_info[i];
       
  4346 
       
  4347 	while (rx_desc->status & E1000_RXD_STAT_DD) {
       
  4348 		struct sk_buff *skb;
       
  4349 		u8 status;
       
  4350 
       
  4351 #ifdef CONFIG_E1000_NAPI
       
  4352 		if (*work_done >= work_to_do)
       
  4353 			break;
       
  4354 		(*work_done)++;
       
  4355 #endif
       
  4356 		status = rx_desc->status;
       
  4357 		skb = buffer_info->skb;
       
  4358 		if (!adapter->ecdev) buffer_info->skb = NULL;
       
  4359 
       
  4360 		prefetch(skb->data - NET_IP_ALIGN);
       
  4361 
       
  4362 		if (++i == rx_ring->count) i = 0;
       
  4363 		next_rxd = E1000_RX_DESC(*rx_ring, i);
       
  4364 		prefetch(next_rxd);
       
  4365 
       
  4366 		next_buffer = &rx_ring->buffer_info[i];
       
  4367 
       
  4368 		cleaned = true;
       
  4369 		cleaned_count++;
       
  4370 		pci_unmap_single(pdev,
       
  4371 		                 buffer_info->dma,
       
  4372 		                 buffer_info->length,
       
  4373 		                 PCI_DMA_FROMDEVICE);
       
  4374 
       
  4375 		length = le16_to_cpu(rx_desc->length);
       
  4376 
       
  4377 		if (unlikely(!(status & E1000_RXD_STAT_EOP))) {
       
  4378 			/* All receives must fit into a single buffer */
       
  4379 			E1000_DBG("%s: Receive packet consumed multiple"
       
  4380 				  " buffers\n", netdev->name);
       
  4381 			/* recycle */
       
  4382 			buffer_info->skb = skb;
       
  4383 			goto next_desc;
       
  4384 		}
       
  4385 
       
  4386 		if (!adapter->ecdev &&
       
  4387                 unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
       
  4388 			last_byte = *(skb->data + length - 1);
       
  4389 			if (TBI_ACCEPT(&adapter->hw, status,
       
  4390 			              rx_desc->errors, length, last_byte)) {
       
  4391 				spin_lock_irqsave(&adapter->stats_lock, flags);
       
  4392 				e1000_tbi_adjust_stats(&adapter->hw,
       
  4393 				                       &adapter->stats,
       
  4394 				                       length, skb->data);
       
  4395 				spin_unlock_irqrestore(&adapter->stats_lock,
       
  4396 				                       flags);
       
  4397 				length--;
       
  4398 			} else {
       
  4399 				/* recycle */
       
  4400 				buffer_info->skb = skb;
       
  4401 				goto next_desc;
       
  4402 			}
       
  4403 		}
       
  4404 
       
  4405 		/* adjust length to remove Ethernet CRC, this must be
       
  4406 		 * done after the TBI_ACCEPT workaround above */
       
  4407 		length -= 4;
       
  4408 
       
  4409 		/* probably a little skewed due to removing CRC */
       
  4410 		total_rx_bytes += length;
       
  4411 		total_rx_packets++;
       
  4412 
       
  4413 		/* code added for copybreak, this should improve
       
  4414 		 * performance for small packets with large amounts
       
  4415 		 * of reassembly being done in the stack */
       
  4416 		if (!adapter->ecdev && length < copybreak) {
       
  4417 			struct sk_buff *new_skb =
       
  4418 			    netdev_alloc_skb(netdev, length + NET_IP_ALIGN);
       
  4419 			if (new_skb) {
       
  4420 				skb_reserve(new_skb, NET_IP_ALIGN);
       
  4421 				skb_copy_to_linear_data_offset(new_skb,
       
  4422 							       -NET_IP_ALIGN,
       
  4423 							       (skb->data -
       
  4424 							        NET_IP_ALIGN),
       
  4425 							       (length +
       
  4426 							        NET_IP_ALIGN));
       
  4427 				/* save the skb in buffer_info as good */
       
  4428 				buffer_info->skb = skb;
       
  4429 				skb = new_skb;
       
  4430 			}
       
  4431 			/* else just continue with the old one */
       
  4432 		}
       
  4433 		/* end copybreak code */
       
  4434 		skb_put(skb, length);
       
  4435 
       
  4436 		/* Receive Checksum Offload */
       
  4437 		e1000_rx_checksum(adapter,
       
  4438 				  (u32)(status) |
       
  4439 				  ((u32)(rx_desc->errors) << 24),
       
  4440 				  le16_to_cpu(rx_desc->csum), skb);
       
  4441 
       
  4442 		if (adapter->ecdev) {
       
  4443 			ecdev_receive(adapter->ecdev, skb->data, length);
       
  4444 
       
  4445 			// No need to detect link status as
       
  4446 			// long as frames are received: Reset watchdog.
       
  4447 			adapter->ec_watchdog_jiffies = jiffies;
       
  4448 		} else {
       
  4449 			skb->protocol = eth_type_trans(skb, netdev);
       
  4450 #ifdef CONFIG_E1000_NAPI
       
  4451 			if (unlikely(adapter->vlgrp &&
       
  4452 			   	 (status & E1000_RXD_STAT_VP))) {
       
  4453 				vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
       
  4454 							 le16_to_cpu(rx_desc->special) &
       
  4455 							 E1000_RXD_SPC_VLAN_MASK);
       
  4456 			} else {
       
  4457 				netif_receive_skb(skb);
       
  4458 			}
       
  4459 #else /* CONFIG_E1000_NAPI */
       
  4460 			if (unlikely(adapter->vlgrp &&
       
  4461 				    (status & E1000_RXD_STAT_VP))) {
       
  4462 				vlan_hwaccel_rx(skb, adapter->vlgrp,
       
  4463 						le16_to_cpu(rx_desc->special) &
       
  4464 						E1000_RXD_SPC_VLAN_MASK);
       
  4465 			} else {
       
  4466 				netif_rx(skb);
       
  4467 			}
       
  4468 #endif /* CONFIG_E1000_NAPI */
       
  4469 		}
       
  4470 		netdev->last_rx = jiffies;
       
  4471 
       
  4472 next_desc:
       
  4473 		rx_desc->status = 0;
       
  4474 
       
  4475 		/* return some buffers to hardware, one at a time is too slow */
       
  4476 		if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
       
  4477 			adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
       
  4478 			cleaned_count = 0;
       
  4479 		}
       
  4480 
       
  4481 		/* use prefetched values */
       
  4482 		rx_desc = next_rxd;
       
  4483 		buffer_info = next_buffer;
       
  4484 	}
       
  4485 	rx_ring->next_to_clean = i;
       
  4486 
       
  4487 	cleaned_count = E1000_DESC_UNUSED(rx_ring);
       
  4488 	if (cleaned_count)
       
  4489 		adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
       
  4490 
       
  4491 	adapter->total_rx_packets += total_rx_packets;
       
  4492 	adapter->total_rx_bytes += total_rx_bytes;
       
  4493 	adapter->net_stats.rx_bytes += total_rx_bytes;
       
  4494 	adapter->net_stats.rx_packets += total_rx_packets;
       
  4495 	return cleaned;
       
  4496 }
       
  4497 
       
  4498 /**
       
  4499  * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
       
  4500  * @adapter: board private structure
       
  4501  **/
       
  4502 
       
  4503 static bool
       
  4504 #ifdef CONFIG_E1000_NAPI
       
  4505 e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
       
  4506                       struct e1000_rx_ring *rx_ring,
       
  4507                       int *work_done, int work_to_do)
       
  4508 #else
       
  4509 e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
       
  4510                       struct e1000_rx_ring *rx_ring)
       
  4511 #endif
       
  4512 {
       
  4513 	union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
       
  4514 	struct net_device *netdev = adapter->netdev;
       
  4515 	struct pci_dev *pdev = adapter->pdev;
       
  4516 	struct e1000_buffer *buffer_info, *next_buffer;
       
  4517 	struct e1000_ps_page *ps_page;
       
  4518 	struct e1000_ps_page_dma *ps_page_dma;
       
  4519 	struct sk_buff *skb;
       
  4520 	unsigned int i, j;
       
  4521 	u32 length, staterr;
       
  4522 	int cleaned_count = 0;
       
  4523 	bool cleaned = false;
       
  4524 	unsigned int total_rx_bytes=0, total_rx_packets=0;
       
  4525 
       
  4526 	i = rx_ring->next_to_clean;
       
  4527 	rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
       
  4528 	staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
       
  4529 	buffer_info = &rx_ring->buffer_info[i];
       
  4530 
       
  4531 	while (staterr & E1000_RXD_STAT_DD) {
       
  4532 		ps_page = &rx_ring->ps_page[i];
       
  4533 		ps_page_dma = &rx_ring->ps_page_dma[i];
       
  4534 #ifdef CONFIG_E1000_NAPI
       
  4535 		if (unlikely(*work_done >= work_to_do))
       
  4536 			break;
       
  4537 		(*work_done)++;
       
  4538 #endif
       
  4539 		skb = buffer_info->skb;
       
  4540 
       
  4541 		/* in the packet split case this is header only */
       
  4542 		prefetch(skb->data - NET_IP_ALIGN);
       
  4543 
       
  4544 		if (++i == rx_ring->count) i = 0;
       
  4545 		next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
       
  4546 		prefetch(next_rxd);
       
  4547 
       
  4548 		next_buffer = &rx_ring->buffer_info[i];
       
  4549 
       
  4550 		cleaned = true;
       
  4551 		cleaned_count++;
       
  4552 		pci_unmap_single(pdev, buffer_info->dma,
       
  4553 				 buffer_info->length,
       
  4554 				 PCI_DMA_FROMDEVICE);
       
  4555 
       
  4556 		if (unlikely(!(staterr & E1000_RXD_STAT_EOP))) {
       
  4557 			E1000_DBG("%s: Packet Split buffers didn't pick up"
       
  4558 				  " the full packet\n", netdev->name);
       
  4559 			if (!adapter->ecdev) dev_kfree_skb_irq(skb);
       
  4560 			goto next_desc;
       
  4561 		}
       
  4562 
       
  4563 		if (unlikely(staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK)) {
       
  4564 			if (!adapter->ecdev) dev_kfree_skb_irq(skb);
       
  4565 			goto next_desc;
       
  4566 		}
       
  4567 
       
  4568 		length = le16_to_cpu(rx_desc->wb.middle.length0);
       
  4569 
       
  4570 		if (unlikely(!length)) {
       
  4571 			E1000_DBG("%s: Last part of the packet spanning"
       
  4572 				  " multiple descriptors\n", netdev->name);
       
  4573 			if (!adapter->ecdev) dev_kfree_skb_irq(skb);
       
  4574 			goto next_desc;
       
  4575 		}
       
  4576 
       
  4577 		/* Good Receive */
       
  4578 		skb_put(skb, length);
       
  4579 
       
  4580 		{
       
  4581 		/* this looks ugly, but it seems compiler issues make it
       
  4582 		   more efficient than reusing j */
       
  4583 		int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
       
  4584 
       
  4585 		/* page alloc/put takes too long and effects small packet
       
  4586 		 * throughput, so unsplit small packets and save the alloc/put*/
       
  4587 		if (l1 && (l1 <= copybreak) && ((length + l1) <= adapter->rx_ps_bsize0)) {
       
  4588 			u8 *vaddr;
       
  4589 			/* there is no documentation about how to call
       
  4590 			 * kmap_atomic, so we can't hold the mapping
       
  4591 			 * very long */
       
  4592 			pci_dma_sync_single_for_cpu(pdev,
       
  4593 				ps_page_dma->ps_page_dma[0],
       
  4594 				PAGE_SIZE,
       
  4595 				PCI_DMA_FROMDEVICE);
       
  4596 			vaddr = kmap_atomic(ps_page->ps_page[0],
       
  4597 			                    KM_SKB_DATA_SOFTIRQ);
       
  4598 			memcpy(skb_tail_pointer(skb), vaddr, l1);
       
  4599 			kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
       
  4600 			pci_dma_sync_single_for_device(pdev,
       
  4601 				ps_page_dma->ps_page_dma[0],
       
  4602 				PAGE_SIZE, PCI_DMA_FROMDEVICE);
       
  4603 			/* remove the CRC */
       
  4604 			l1 -= 4;
       
  4605 			skb_put(skb, l1);
       
  4606 			goto copydone;
       
  4607 		} /* if */
       
  4608 		}
       
  4609 
       
  4610 		for (j = 0; j < adapter->rx_ps_pages; j++) {
       
  4611 			if (!(length= le16_to_cpu(rx_desc->wb.upper.length[j])))
       
  4612 				break;
       
  4613 			pci_unmap_page(pdev, ps_page_dma->ps_page_dma[j],
       
  4614 					PAGE_SIZE, PCI_DMA_FROMDEVICE);
       
  4615 			ps_page_dma->ps_page_dma[j] = 0;
       
  4616 			skb_fill_page_desc(skb, j, ps_page->ps_page[j], 0,
       
  4617 			                   length);
       
  4618 			ps_page->ps_page[j] = NULL;
       
  4619 			skb->len += length;
       
  4620 			skb->data_len += length;
       
  4621 			skb->truesize += length;
       
  4622 		}
       
  4623 
       
  4624 		/* strip the ethernet crc, problem is we're using pages now so
       
  4625 		 * this whole operation can get a little cpu intensive */
       
  4626 		pskb_trim(skb, skb->len - 4);
       
  4627 
       
  4628 copydone:
       
  4629 		total_rx_bytes += skb->len;
       
  4630 		total_rx_packets++;
       
  4631 
       
  4632 		e1000_rx_checksum(adapter, staterr,
       
  4633 				  le16_to_cpu(rx_desc->wb.lower.hi_dword.csum_ip.csum), skb);
       
  4634 
       
  4635 		if (likely(rx_desc->wb.upper.header_status &
       
  4636 			   cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP)))
       
  4637 			adapter->rx_hdr_split++;
       
  4638 		if (adapter->ecdev) {
       
  4639 			ecdev_receive(adapter->ecdev, skb->data, length);
       
  4640 		} else {
       
  4641 			skb->protocol = eth_type_trans(skb, netdev);
       
  4642 #ifdef CONFIG_E1000_NAPI
       
  4643 			if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
       
  4644 				vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
       
  4645 					le16_to_cpu(rx_desc->wb.middle.vlan) &
       
  4646 					E1000_RXD_SPC_VLAN_MASK);
       
  4647 			} else {
       
  4648 				netif_receive_skb(skb);
       
  4649 			}
       
  4650 #else /* CONFIG_E1000_NAPI */
       
  4651 			if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
       
  4652 				vlan_hwaccel_rx(skb, adapter->vlgrp,
       
  4653 					le16_to_cpu(rx_desc->wb.middle.vlan) &
       
  4654 					E1000_RXD_SPC_VLAN_MASK);
       
  4655 			} else {
       
  4656 				netif_rx(skb);
       
  4657 			}
       
  4658 #endif /* CONFIG_E1000_NAPI */
       
  4659 		}
       
  4660 		netdev->last_rx = jiffies;
       
  4661 
       
  4662 next_desc:
       
  4663 		rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
       
  4664 		if (!adapter->ecdev) buffer_info->skb = NULL;
       
  4665 
       
  4666 		/* return some buffers to hardware, one at a time is too slow */
       
  4667 		if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
       
  4668 			adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
       
  4669 			cleaned_count = 0;
       
  4670 		}
       
  4671 
       
  4672 		/* use prefetched values */
       
  4673 		rx_desc = next_rxd;
       
  4674 		buffer_info = next_buffer;
       
  4675 
       
  4676 		staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
       
  4677 	}
       
  4678 	rx_ring->next_to_clean = i;
       
  4679 
       
  4680 	cleaned_count = E1000_DESC_UNUSED(rx_ring);
       
  4681 	if (cleaned_count)
       
  4682 		adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
       
  4683 
       
  4684 	adapter->total_rx_packets += total_rx_packets;
       
  4685 	adapter->total_rx_bytes += total_rx_bytes;
       
  4686 	adapter->net_stats.rx_bytes += total_rx_bytes;
       
  4687 	adapter->net_stats.rx_packets += total_rx_packets;
       
  4688 	return cleaned;
       
  4689 }
       
  4690 
       
  4691 /**
       
  4692  * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
       
  4693  * @adapter: address of board private structure
       
  4694  **/
       
  4695 
       
  4696 static void
       
  4697 e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
       
  4698                        struct e1000_rx_ring *rx_ring,
       
  4699 		       int cleaned_count)
       
  4700 {
       
  4701 	struct net_device *netdev = adapter->netdev;
       
  4702 	struct pci_dev *pdev = adapter->pdev;
       
  4703 	struct e1000_rx_desc *rx_desc;
       
  4704 	struct e1000_buffer *buffer_info;
       
  4705 	struct sk_buff *skb;
       
  4706 	unsigned int i;
       
  4707 	unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
       
  4708 
       
  4709 	i = rx_ring->next_to_use;
       
  4710 	buffer_info = &rx_ring->buffer_info[i];
       
  4711 
       
  4712 	while (cleaned_count--) {
       
  4713 		skb = buffer_info->skb;
       
  4714 		if (skb) {
       
  4715 			skb_trim(skb, 0);
       
  4716 			goto map_skb;
       
  4717 		}
       
  4718 
       
  4719 		skb = netdev_alloc_skb(netdev, bufsz);
       
  4720 		if (unlikely(!skb)) {
       
  4721 			/* Better luck next round */
       
  4722 			adapter->alloc_rx_buff_failed++;
       
  4723 			break;
       
  4724 		}
       
  4725 
       
  4726 		/* Fix for errata 23, can't cross 64kB boundary */
       
  4727 		if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
       
  4728 			struct sk_buff *oldskb = skb;
       
  4729 			DPRINTK(RX_ERR, ERR, "skb align check failed: %u bytes "
       
  4730 					     "at %p\n", bufsz, skb->data);
       
  4731 			/* Try again, without freeing the previous */
       
  4732 			skb = netdev_alloc_skb(netdev, bufsz);
       
  4733 			/* Failed allocation, critical failure */
       
  4734 			if (!skb) {
       
  4735 				dev_kfree_skb(oldskb);
       
  4736 				break;
       
  4737 			}
       
  4738 
       
  4739 			if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
       
  4740 				/* give up */
       
  4741 				dev_kfree_skb(skb);
       
  4742 				dev_kfree_skb(oldskb);
       
  4743 				break; /* while !buffer_info->skb */
       
  4744 			}
       
  4745 
       
  4746 			/* Use new allocation */
       
  4747 			dev_kfree_skb(oldskb);
       
  4748 		}
       
  4749 		/* Make buffer alignment 2 beyond a 16 byte boundary
       
  4750 		 * this will result in a 16 byte aligned IP header after
       
  4751 		 * the 14 byte MAC header is removed
       
  4752 		 */
       
  4753 		skb_reserve(skb, NET_IP_ALIGN);
       
  4754 
       
  4755 		buffer_info->skb = skb;
       
  4756 		buffer_info->length = adapter->rx_buffer_len;
       
  4757 map_skb:
       
  4758 		buffer_info->dma = pci_map_single(pdev,
       
  4759 						  skb->data,
       
  4760 						  adapter->rx_buffer_len,
       
  4761 						  PCI_DMA_FROMDEVICE);
       
  4762 
       
  4763 		/* Fix for errata 23, can't cross 64kB boundary */
       
  4764 		if (!e1000_check_64k_bound(adapter,
       
  4765 					(void *)(unsigned long)buffer_info->dma,
       
  4766 					adapter->rx_buffer_len)) {
       
  4767 			DPRINTK(RX_ERR, ERR,
       
  4768 				"dma align check failed: %u bytes at %p\n",
       
  4769 				adapter->rx_buffer_len,
       
  4770 				(void *)(unsigned long)buffer_info->dma);
       
  4771 			if (!adapter->ecdev) {
       
  4772 				dev_kfree_skb(skb);
       
  4773 				buffer_info->skb = NULL;
       
  4774 			}
       
  4775 
       
  4776 			pci_unmap_single(pdev, buffer_info->dma,
       
  4777 					 adapter->rx_buffer_len,
       
  4778 					 PCI_DMA_FROMDEVICE);
       
  4779 
       
  4780 			break; /* while !buffer_info->skb */
       
  4781 		}
       
  4782 		rx_desc = E1000_RX_DESC(*rx_ring, i);
       
  4783 		rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
       
  4784 
       
  4785 		if (unlikely(++i == rx_ring->count))
       
  4786 			i = 0;
       
  4787 		buffer_info = &rx_ring->buffer_info[i];
       
  4788 	}
       
  4789 
       
  4790 	if (likely(rx_ring->next_to_use != i)) {
       
  4791 		rx_ring->next_to_use = i;
       
  4792 		if (unlikely(i-- == 0))
       
  4793 			i = (rx_ring->count - 1);
       
  4794 
       
  4795 		/* Force memory writes to complete before letting h/w
       
  4796 		 * know there are new descriptors to fetch.  (Only
       
  4797 		 * applicable for weak-ordered memory model archs,
       
  4798 		 * such as IA-64). */
       
  4799 		wmb();
       
  4800 		writel(i, adapter->hw.hw_addr + rx_ring->rdt);
       
  4801 	}
       
  4802 }
       
  4803 
       
  4804 /**
       
  4805  * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
       
  4806  * @adapter: address of board private structure
       
  4807  **/
       
  4808 
       
  4809 static void
       
  4810 e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
       
  4811                           struct e1000_rx_ring *rx_ring,
       
  4812 			  int cleaned_count)
       
  4813 {
       
  4814 	struct net_device *netdev = adapter->netdev;
       
  4815 	struct pci_dev *pdev = adapter->pdev;
       
  4816 	union e1000_rx_desc_packet_split *rx_desc;
       
  4817 	struct e1000_buffer *buffer_info;
       
  4818 	struct e1000_ps_page *ps_page;
       
  4819 	struct e1000_ps_page_dma *ps_page_dma;
       
  4820 	struct sk_buff *skb;
       
  4821 	unsigned int i, j;
       
  4822 
       
  4823 	i = rx_ring->next_to_use;
       
  4824 	buffer_info = &rx_ring->buffer_info[i];
       
  4825 	ps_page = &rx_ring->ps_page[i];
       
  4826 	ps_page_dma = &rx_ring->ps_page_dma[i];
       
  4827 
       
  4828 	while (cleaned_count--) {
       
  4829 		rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
       
  4830 
       
  4831 		for (j = 0; j < PS_PAGE_BUFFERS; j++) {
       
  4832 			if (j < adapter->rx_ps_pages) {
       
  4833 				if (likely(!ps_page->ps_page[j])) {
       
  4834 					ps_page->ps_page[j] =
       
  4835 						alloc_page(GFP_ATOMIC);
       
  4836 					if (unlikely(!ps_page->ps_page[j])) {
       
  4837 						adapter->alloc_rx_buff_failed++;
       
  4838 						goto no_buffers;
       
  4839 					}
       
  4840 					ps_page_dma->ps_page_dma[j] =
       
  4841 						pci_map_page(pdev,
       
  4842 							    ps_page->ps_page[j],
       
  4843 							    0, PAGE_SIZE,
       
  4844 							    PCI_DMA_FROMDEVICE);
       
  4845 				}
       
  4846 				/* Refresh the desc even if buffer_addrs didn't
       
  4847 				 * change because each write-back erases
       
  4848 				 * this info.
       
  4849 				 */
       
  4850 				rx_desc->read.buffer_addr[j+1] =
       
  4851 				     cpu_to_le64(ps_page_dma->ps_page_dma[j]);
       
  4852 			} else
       
  4853 				rx_desc->read.buffer_addr[j+1] = ~cpu_to_le64(0);
       
  4854 		}
       
  4855 
       
  4856 		skb = netdev_alloc_skb(netdev,
       
  4857 		                       adapter->rx_ps_bsize0 + NET_IP_ALIGN);
       
  4858 
       
  4859 		if (unlikely(!skb)) {
       
  4860 			adapter->alloc_rx_buff_failed++;
       
  4861 			break;
       
  4862 		}
       
  4863 
       
  4864 		/* Make buffer alignment 2 beyond a 16 byte boundary
       
  4865 		 * this will result in a 16 byte aligned IP header after
       
  4866 		 * the 14 byte MAC header is removed
       
  4867 		 */
       
  4868 		skb_reserve(skb, NET_IP_ALIGN);
       
  4869 
       
  4870 		buffer_info->skb = skb;
       
  4871 		buffer_info->length = adapter->rx_ps_bsize0;
       
  4872 		buffer_info->dma = pci_map_single(pdev, skb->data,
       
  4873 						  adapter->rx_ps_bsize0,
       
  4874 						  PCI_DMA_FROMDEVICE);
       
  4875 
       
  4876 		rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
       
  4877 
       
  4878 		if (unlikely(++i == rx_ring->count)) i = 0;
       
  4879 		buffer_info = &rx_ring->buffer_info[i];
       
  4880 		ps_page = &rx_ring->ps_page[i];
       
  4881 		ps_page_dma = &rx_ring->ps_page_dma[i];
       
  4882 	}
       
  4883 
       
  4884 no_buffers:
       
  4885 	if (likely(rx_ring->next_to_use != i)) {
       
  4886 		rx_ring->next_to_use = i;
       
  4887 		if (unlikely(i-- == 0)) i = (rx_ring->count - 1);
       
  4888 
       
  4889 		/* Force memory writes to complete before letting h/w
       
  4890 		 * know there are new descriptors to fetch.  (Only
       
  4891 		 * applicable for weak-ordered memory model archs,
       
  4892 		 * such as IA-64). */
       
  4893 		wmb();
       
  4894 		/* Hardware increments by 16 bytes, but packet split
       
  4895 		 * descriptors are 32 bytes...so we increment tail
       
  4896 		 * twice as much.
       
  4897 		 */
       
  4898 		writel(i<<1, adapter->hw.hw_addr + rx_ring->rdt);
       
  4899 	}
       
  4900 }
       
  4901 
       
  4902 /**
       
  4903  * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
       
  4904  * @adapter:
       
  4905  **/
       
  4906 
       
  4907 static void
       
  4908 e1000_smartspeed(struct e1000_adapter *adapter)
       
  4909 {
       
  4910 	u16 phy_status;
       
  4911 	u16 phy_ctrl;
       
  4912 
       
  4913 	if ((adapter->hw.phy_type != e1000_phy_igp) || !adapter->hw.autoneg ||
       
  4914 	   !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
       
  4915 		return;
       
  4916 
       
  4917 	if (adapter->smartspeed == 0) {
       
  4918 		/* If Master/Slave config fault is asserted twice,
       
  4919 		 * we assume back-to-back */
       
  4920 		e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
       
  4921 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
       
  4922 		e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
       
  4923 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
       
  4924 		e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
       
  4925 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
       
  4926 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
       
  4927 			e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL,
       
  4928 					    phy_ctrl);
       
  4929 			adapter->smartspeed++;
       
  4930 			if (!e1000_phy_setup_autoneg(&adapter->hw) &&
       
  4931 			   !e1000_read_phy_reg(&adapter->hw, PHY_CTRL,
       
  4932 				   	       &phy_ctrl)) {
       
  4933 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
       
  4934 					     MII_CR_RESTART_AUTO_NEG);
       
  4935 				e1000_write_phy_reg(&adapter->hw, PHY_CTRL,
       
  4936 						    phy_ctrl);
       
  4937 			}
       
  4938 		}
       
  4939 		return;
       
  4940 	} else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
       
  4941 		/* If still no link, perhaps using 2/3 pair cable */
       
  4942 		e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
       
  4943 		phy_ctrl |= CR_1000T_MS_ENABLE;
       
  4944 		e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_ctrl);
       
  4945 		if (!e1000_phy_setup_autoneg(&adapter->hw) &&
       
  4946 		   !e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_ctrl)) {
       
  4947 			phy_ctrl |= (MII_CR_AUTO_NEG_EN |
       
  4948 				     MII_CR_RESTART_AUTO_NEG);
       
  4949 			e1000_write_phy_reg(&adapter->hw, PHY_CTRL, phy_ctrl);
       
  4950 		}
       
  4951 	}
       
  4952 	/* Restart process after E1000_SMARTSPEED_MAX iterations */
       
  4953 	if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
       
  4954 		adapter->smartspeed = 0;
       
  4955 }
       
  4956 
       
  4957 /**
       
  4958  * e1000_ioctl -
       
  4959  * @netdev:
       
  4960  * @ifreq:
       
  4961  * @cmd:
       
  4962  **/
       
  4963 
       
  4964 static int
       
  4965 e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
       
  4966 {
       
  4967 	switch (cmd) {
       
  4968 	case SIOCGMIIPHY:
       
  4969 	case SIOCGMIIREG:
       
  4970 	case SIOCSMIIREG:
       
  4971 		return e1000_mii_ioctl(netdev, ifr, cmd);
       
  4972 	default:
       
  4973 		return -EOPNOTSUPP;
       
  4974 	}
       
  4975 }
       
  4976 
       
  4977 /**
       
  4978  * e1000_mii_ioctl -
       
  4979  * @netdev:
       
  4980  * @ifreq:
       
  4981  * @cmd:
       
  4982  **/
       
  4983 
       
  4984 static int
       
  4985 e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
       
  4986 {
       
  4987 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  4988 	struct mii_ioctl_data *data = if_mii(ifr);
       
  4989 	int retval;
       
  4990 	u16 mii_reg;
       
  4991 	u16 spddplx;
       
  4992 	unsigned long flags;
       
  4993 
       
  4994 	if (adapter->hw.media_type != e1000_media_type_copper)
       
  4995 		return -EOPNOTSUPP;
       
  4996 
       
  4997 	switch (cmd) {
       
  4998 	case SIOCGMIIPHY:
       
  4999 		data->phy_id = adapter->hw.phy_addr;
       
  5000 		break;
       
  5001 	case SIOCGMIIREG:
       
  5002 		if (adapter->ecdev || !capable(CAP_NET_ADMIN))
       
  5003 			return -EPERM;
       
  5004 		spin_lock_irqsave(&adapter->stats_lock, flags);
       
  5005 		if (e1000_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
       
  5006 				   &data->val_out)) {
       
  5007 			spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  5008 			return -EIO;
       
  5009 		}
       
  5010 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  5011 		break;
       
  5012 	case SIOCSMIIREG:
       
  5013 		if (adapter->ecdev || !capable(CAP_NET_ADMIN))
       
  5014 			return -EPERM;
       
  5015 		if (data->reg_num & ~(0x1F))
       
  5016 			return -EFAULT;
       
  5017 		mii_reg = data->val_in;
       
  5018 		spin_lock_irqsave(&adapter->stats_lock, flags);
       
  5019 		if (e1000_write_phy_reg(&adapter->hw, data->reg_num,
       
  5020 					mii_reg)) {
       
  5021 			spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  5022 			return -EIO;
       
  5023 		}
       
  5024 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  5025 		if (adapter->hw.media_type == e1000_media_type_copper) {
       
  5026 			switch (data->reg_num) {
       
  5027 			case PHY_CTRL:
       
  5028 				if (mii_reg & MII_CR_POWER_DOWN)
       
  5029 					break;
       
  5030 				if (mii_reg & MII_CR_AUTO_NEG_EN) {
       
  5031 					adapter->hw.autoneg = 1;
       
  5032 					adapter->hw.autoneg_advertised = 0x2F;
       
  5033 				} else {
       
  5034 					if (mii_reg & 0x40)
       
  5035 						spddplx = SPEED_1000;
       
  5036 					else if (mii_reg & 0x2000)
       
  5037 						spddplx = SPEED_100;
       
  5038 					else
       
  5039 						spddplx = SPEED_10;
       
  5040 					spddplx += (mii_reg & 0x100)
       
  5041 						   ? DUPLEX_FULL :
       
  5042 						   DUPLEX_HALF;
       
  5043 					retval = e1000_set_spd_dplx(adapter,
       
  5044 								    spddplx);
       
  5045 					if (retval)
       
  5046 						return retval;
       
  5047 				}
       
  5048 				if (netif_running(adapter->netdev))
       
  5049 					e1000_reinit_locked(adapter);
       
  5050 				else
       
  5051 					e1000_reset(adapter);
       
  5052 				break;
       
  5053 			case M88E1000_PHY_SPEC_CTRL:
       
  5054 			case M88E1000_EXT_PHY_SPEC_CTRL:
       
  5055 				if (e1000_phy_reset(&adapter->hw))
       
  5056 					return -EIO;
       
  5057 				break;
       
  5058 			}
       
  5059 		} else {
       
  5060 			switch (data->reg_num) {
       
  5061 			case PHY_CTRL:
       
  5062 				if (mii_reg & MII_CR_POWER_DOWN)
       
  5063 					break;
       
  5064 				if (netif_running(adapter->netdev))
       
  5065 					e1000_reinit_locked(adapter);
       
  5066 				else
       
  5067 					e1000_reset(adapter);
       
  5068 				break;
       
  5069 			}
       
  5070 		}
       
  5071 		break;
       
  5072 	default:
       
  5073 		return -EOPNOTSUPP;
       
  5074 	}
       
  5075 	return E1000_SUCCESS;
       
  5076 }
       
  5077 
       
  5078 void
       
  5079 e1000_pci_set_mwi(struct e1000_hw *hw)
       
  5080 {
       
  5081 	struct e1000_adapter *adapter = hw->back;
       
  5082 	int ret_val = pci_set_mwi(adapter->pdev);
       
  5083 
       
  5084 	if (ret_val)
       
  5085 		DPRINTK(PROBE, ERR, "Error in setting MWI\n");
       
  5086 }
       
  5087 
       
  5088 void
       
  5089 e1000_pci_clear_mwi(struct e1000_hw *hw)
       
  5090 {
       
  5091 	struct e1000_adapter *adapter = hw->back;
       
  5092 
       
  5093 	pci_clear_mwi(adapter->pdev);
       
  5094 }
       
  5095 
       
  5096 int
       
  5097 e1000_pcix_get_mmrbc(struct e1000_hw *hw)
       
  5098 {
       
  5099 	struct e1000_adapter *adapter = hw->back;
       
  5100 	return pcix_get_mmrbc(adapter->pdev);
       
  5101 }
       
  5102 
       
  5103 void
       
  5104 e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
       
  5105 {
       
  5106 	struct e1000_adapter *adapter = hw->back;
       
  5107 	pcix_set_mmrbc(adapter->pdev, mmrbc);
       
  5108 }
       
  5109 
       
  5110 s32
       
  5111 e1000_read_pcie_cap_reg(struct e1000_hw *hw, u32 reg, u16 *value)
       
  5112 {
       
  5113     struct e1000_adapter *adapter = hw->back;
       
  5114     u16 cap_offset;
       
  5115 
       
  5116     cap_offset = pci_find_capability(adapter->pdev, PCI_CAP_ID_EXP);
       
  5117     if (!cap_offset)
       
  5118         return -E1000_ERR_CONFIG;
       
  5119 
       
  5120     pci_read_config_word(adapter->pdev, cap_offset + reg, value);
       
  5121 
       
  5122     return E1000_SUCCESS;
       
  5123 }
       
  5124 
       
  5125 void
       
  5126 e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
       
  5127 {
       
  5128 	outl(value, port);
       
  5129 }
       
  5130 
       
  5131 static void
       
  5132 e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
       
  5133 {
       
  5134 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5135 	u32 ctrl, rctl;
       
  5136 
       
  5137 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  5138 		e1000_irq_disable(adapter);
       
  5139 	adapter->vlgrp = grp;
       
  5140 
       
  5141 	if (grp) {
       
  5142 		/* enable VLAN tag insert/strip */
       
  5143 		ctrl = E1000_READ_REG(&adapter->hw, CTRL);
       
  5144 		ctrl |= E1000_CTRL_VME;
       
  5145 		E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
       
  5146 
       
  5147 		if (adapter->hw.mac_type != e1000_ich8lan) {
       
  5148 			/* enable VLAN receive filtering */
       
  5149 			rctl = E1000_READ_REG(&adapter->hw, RCTL);
       
  5150 			rctl |= E1000_RCTL_VFE;
       
  5151 			rctl &= ~E1000_RCTL_CFIEN;
       
  5152 			E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
       
  5153 			e1000_update_mng_vlan(adapter);
       
  5154 		}
       
  5155 	} else {
       
  5156 		/* disable VLAN tag insert/strip */
       
  5157 		ctrl = E1000_READ_REG(&adapter->hw, CTRL);
       
  5158 		ctrl &= ~E1000_CTRL_VME;
       
  5159 		E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
       
  5160 
       
  5161 		if (adapter->hw.mac_type != e1000_ich8lan) {
       
  5162 			/* disable VLAN filtering */
       
  5163 			rctl = E1000_READ_REG(&adapter->hw, RCTL);
       
  5164 			rctl &= ~E1000_RCTL_VFE;
       
  5165 			E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
       
  5166 			if (adapter->mng_vlan_id !=
       
  5167 			    (u16)E1000_MNG_VLAN_NONE) {
       
  5168 				e1000_vlan_rx_kill_vid(netdev,
       
  5169 				                       adapter->mng_vlan_id);
       
  5170 				adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
       
  5171 			}
       
  5172 		}
       
  5173 	}
       
  5174 
       
  5175 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  5176 		e1000_irq_enable(adapter);
       
  5177 }
       
  5178 
       
  5179 static void
       
  5180 e1000_vlan_rx_add_vid(struct net_device *netdev, u16 vid)
       
  5181 {
       
  5182 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5183 	u32 vfta, index;
       
  5184 
       
  5185 	if ((adapter->hw.mng_cookie.status &
       
  5186 	     E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
       
  5187 	    (vid == adapter->mng_vlan_id))
       
  5188 		return;
       
  5189 	/* add VID to filter table */
       
  5190 	index = (vid >> 5) & 0x7F;
       
  5191 	vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
       
  5192 	vfta |= (1 << (vid & 0x1F));
       
  5193 	e1000_write_vfta(&adapter->hw, index, vfta);
       
  5194 }
       
  5195 
       
  5196 static void
       
  5197 e1000_vlan_rx_kill_vid(struct net_device *netdev, u16 vid)
       
  5198 {
       
  5199 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5200 	u32 vfta, index;
       
  5201 
       
  5202 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  5203 		e1000_irq_disable(adapter);
       
  5204 	vlan_group_set_device(adapter->vlgrp, vid, NULL);
       
  5205 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  5206 		e1000_irq_enable(adapter);
       
  5207 
       
  5208 	if ((adapter->hw.mng_cookie.status &
       
  5209 	     E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
       
  5210 	    (vid == adapter->mng_vlan_id)) {
       
  5211 		/* release control to f/w */
       
  5212 		e1000_release_hw_control(adapter);
       
  5213 		return;
       
  5214 	}
       
  5215 
       
  5216 	/* remove VID from filter table */
       
  5217 	index = (vid >> 5) & 0x7F;
       
  5218 	vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
       
  5219 	vfta &= ~(1 << (vid & 0x1F));
       
  5220 	e1000_write_vfta(&adapter->hw, index, vfta);
       
  5221 }
       
  5222 
       
  5223 static void
       
  5224 e1000_restore_vlan(struct e1000_adapter *adapter)
       
  5225 {
       
  5226 	e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
       
  5227 
       
  5228 	if (adapter->vlgrp) {
       
  5229 		u16 vid;
       
  5230 		for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
       
  5231 			if (!vlan_group_get_device(adapter->vlgrp, vid))
       
  5232 				continue;
       
  5233 			e1000_vlan_rx_add_vid(adapter->netdev, vid);
       
  5234 		}
       
  5235 	}
       
  5236 }
       
  5237 
       
  5238 int
       
  5239 e1000_set_spd_dplx(struct e1000_adapter *adapter, u16 spddplx)
       
  5240 {
       
  5241 	adapter->hw.autoneg = 0;
       
  5242 
       
  5243 	/* Fiber NICs only allow 1000 gbps Full duplex */
       
  5244 	if ((adapter->hw.media_type == e1000_media_type_fiber) &&
       
  5245 		spddplx != (SPEED_1000 + DUPLEX_FULL)) {
       
  5246 		DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
       
  5247 		return -EINVAL;
       
  5248 	}
       
  5249 
       
  5250 	switch (spddplx) {
       
  5251 	case SPEED_10 + DUPLEX_HALF:
       
  5252 		adapter->hw.forced_speed_duplex = e1000_10_half;
       
  5253 		break;
       
  5254 	case SPEED_10 + DUPLEX_FULL:
       
  5255 		adapter->hw.forced_speed_duplex = e1000_10_full;
       
  5256 		break;
       
  5257 	case SPEED_100 + DUPLEX_HALF:
       
  5258 		adapter->hw.forced_speed_duplex = e1000_100_half;
       
  5259 		break;
       
  5260 	case SPEED_100 + DUPLEX_FULL:
       
  5261 		adapter->hw.forced_speed_duplex = e1000_100_full;
       
  5262 		break;
       
  5263 	case SPEED_1000 + DUPLEX_FULL:
       
  5264 		adapter->hw.autoneg = 1;
       
  5265 		adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
       
  5266 		break;
       
  5267 	case SPEED_1000 + DUPLEX_HALF: /* not supported */
       
  5268 	default:
       
  5269 		DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
       
  5270 		return -EINVAL;
       
  5271 	}
       
  5272 	return 0;
       
  5273 }
       
  5274 
       
  5275 static int
       
  5276 e1000_suspend(struct pci_dev *pdev, pm_message_t state)
       
  5277 {
       
  5278 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5279 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5280 	u32 ctrl, ctrl_ext, rctl, status;
       
  5281 	u32 wufc = adapter->wol;
       
  5282 #ifdef CONFIG_PM
       
  5283 	int retval = 0;
       
  5284 #endif
       
  5285 
       
  5286 	if (adapter->ecdev)
       
  5287 		return -EBUSY;
       
  5288 
       
  5289 	netif_device_detach(netdev);
       
  5290 
       
  5291 	if (netif_running(netdev)) {
       
  5292 		WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
       
  5293 		e1000_down(adapter);
       
  5294 	}
       
  5295 
       
  5296 #ifdef CONFIG_PM
       
  5297 	retval = pci_save_state(pdev);
       
  5298 	if (retval)
       
  5299 		return retval;
       
  5300 #endif
       
  5301 
       
  5302 	status = E1000_READ_REG(&adapter->hw, STATUS);
       
  5303 	if (status & E1000_STATUS_LU)
       
  5304 		wufc &= ~E1000_WUFC_LNKC;
       
  5305 
       
  5306 	if (wufc) {
       
  5307 		e1000_setup_rctl(adapter);
       
  5308 		e1000_set_rx_mode(netdev);
       
  5309 
       
  5310 		/* turn on all-multi mode if wake on multicast is enabled */
       
  5311 		if (wufc & E1000_WUFC_MC) {
       
  5312 			rctl = E1000_READ_REG(&adapter->hw, RCTL);
       
  5313 			rctl |= E1000_RCTL_MPE;
       
  5314 			E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
       
  5315 		}
       
  5316 
       
  5317 		if (adapter->hw.mac_type >= e1000_82540) {
       
  5318 			ctrl = E1000_READ_REG(&adapter->hw, CTRL);
       
  5319 			/* advertise wake from D3Cold */
       
  5320 			#define E1000_CTRL_ADVD3WUC 0x00100000
       
  5321 			/* phy power management enable */
       
  5322 			#define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
       
  5323 			ctrl |= E1000_CTRL_ADVD3WUC |
       
  5324 				E1000_CTRL_EN_PHY_PWR_MGMT;
       
  5325 			E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
       
  5326 		}
       
  5327 
       
  5328 		if (adapter->hw.media_type == e1000_media_type_fiber ||
       
  5329 		   adapter->hw.media_type == e1000_media_type_internal_serdes) {
       
  5330 			/* keep the laser running in D3 */
       
  5331 			ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
       
  5332 			ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
       
  5333 			E1000_WRITE_REG(&adapter->hw, CTRL_EXT, ctrl_ext);
       
  5334 		}
       
  5335 
       
  5336 		/* Allow time for pending master requests to run */
       
  5337 		e1000_disable_pciex_master(&adapter->hw);
       
  5338 
       
  5339 		E1000_WRITE_REG(&adapter->hw, WUC, E1000_WUC_PME_EN);
       
  5340 		E1000_WRITE_REG(&adapter->hw, WUFC, wufc);
       
  5341 		pci_enable_wake(pdev, PCI_D3hot, 1);
       
  5342 		pci_enable_wake(pdev, PCI_D3cold, 1);
       
  5343 	} else {
       
  5344 		E1000_WRITE_REG(&adapter->hw, WUC, 0);
       
  5345 		E1000_WRITE_REG(&adapter->hw, WUFC, 0);
       
  5346 		pci_enable_wake(pdev, PCI_D3hot, 0);
       
  5347 		pci_enable_wake(pdev, PCI_D3cold, 0);
       
  5348 	}
       
  5349 
       
  5350 	e1000_release_manageability(adapter);
       
  5351 
       
  5352 	/* make sure adapter isn't asleep if manageability is enabled */
       
  5353 	if (adapter->en_mng_pt) {
       
  5354 		pci_enable_wake(pdev, PCI_D3hot, 1);
       
  5355 		pci_enable_wake(pdev, PCI_D3cold, 1);
       
  5356 	}
       
  5357 
       
  5358 	if (adapter->hw.phy_type == e1000_phy_igp_3)
       
  5359 		e1000_phy_powerdown_workaround(&adapter->hw);
       
  5360 
       
  5361 	if (netif_running(netdev))
       
  5362 		e1000_free_irq(adapter);
       
  5363 
       
  5364 	/* Release control of h/w to f/w.  If f/w is AMT enabled, this
       
  5365 	 * would have already happened in close and is redundant. */
       
  5366 	e1000_release_hw_control(adapter);
       
  5367 
       
  5368 	pci_disable_device(pdev);
       
  5369 
       
  5370 	pci_set_power_state(pdev, pci_choose_state(pdev, state));
       
  5371 
       
  5372 	return 0;
       
  5373 }
       
  5374 
       
  5375 #ifdef CONFIG_PM
       
  5376 static int
       
  5377 e1000_resume(struct pci_dev *pdev)
       
  5378 {
       
  5379 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5380 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5381 	u32 err;
       
  5382 
       
  5383 	if (adapter->ecdev)
       
  5384 		return -EBUSY;
       
  5385 
       
  5386 	pci_set_power_state(pdev, PCI_D0);
       
  5387 	pci_restore_state(pdev);
       
  5388 	if ((err = pci_enable_device(pdev))) {
       
  5389 		printk(KERN_ERR "e1000: Cannot enable PCI device from suspend\n");
       
  5390 		return err;
       
  5391 	}
       
  5392 	pci_set_master(pdev);
       
  5393 
       
  5394 	pci_enable_wake(pdev, PCI_D3hot, 0);
       
  5395 	pci_enable_wake(pdev, PCI_D3cold, 0);
       
  5396 
       
  5397 	if (netif_running(netdev) && (err = e1000_request_irq(adapter)))
       
  5398 		return err;
       
  5399 
       
  5400 	e1000_power_up_phy(adapter);
       
  5401 	e1000_reset(adapter);
       
  5402 	E1000_WRITE_REG(&adapter->hw, WUS, ~0);
       
  5403 
       
  5404 	e1000_init_manageability(adapter);
       
  5405 
       
  5406 	if (netif_running(netdev))
       
  5407 		e1000_up(adapter);
       
  5408 
       
  5409 	if (!adapter->ecdev) netif_device_attach(netdev);
       
  5410 
       
  5411 	/* If the controller is 82573 and f/w is AMT, do not set
       
  5412 	 * DRV_LOAD until the interface is up.  For all other cases,
       
  5413 	 * let the f/w know that the h/w is now under the control
       
  5414 	 * of the driver. */
       
  5415 	if (adapter->hw.mac_type != e1000_82573 ||
       
  5416 	    !e1000_check_mng_mode(&adapter->hw))
       
  5417 		e1000_get_hw_control(adapter);
       
  5418 
       
  5419 	return 0;
       
  5420 }
       
  5421 #endif
       
  5422 
       
  5423 static void e1000_shutdown(struct pci_dev *pdev)
       
  5424 {
       
  5425 	e1000_suspend(pdev, PMSG_SUSPEND);
       
  5426 }
       
  5427 
       
  5428 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  5429 /*
       
  5430  * Polling 'interrupt' - used by things like netconsole to send skbs
       
  5431  * without having to re-enable interrupts. It's not called while
       
  5432  * the interrupt routine is executing.
       
  5433  */
       
  5434 static void
       
  5435 e1000_netpoll(struct net_device *netdev)
       
  5436 {
       
  5437 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5438 
       
  5439 	disable_irq(adapter->pdev->irq);
       
  5440 	e1000_intr(adapter->pdev->irq, netdev);
       
  5441 	e1000_clean_tx_irq(adapter, adapter->tx_ring);
       
  5442 #ifndef CONFIG_E1000_NAPI
       
  5443 	adapter->clean_rx(adapter, adapter->rx_ring);
       
  5444 #endif
       
  5445 	enable_irq(adapter->pdev->irq);
       
  5446 }
       
  5447 #endif
       
  5448 
       
  5449 /**
       
  5450  * e1000_io_error_detected - called when PCI error is detected
       
  5451  * @pdev: Pointer to PCI device
       
  5452  * @state: The current pci conneection state
       
  5453  *
       
  5454  * This function is called after a PCI bus error affecting
       
  5455  * this device has been detected.
       
  5456  */
       
  5457 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
       
  5458 {
       
  5459 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5460 	struct e1000_adapter *adapter = netdev->priv;
       
  5461 
       
  5462 	netif_device_detach(netdev);
       
  5463 
       
  5464 	if (netif_running(netdev))
       
  5465 		e1000_down(adapter);
       
  5466 	pci_disable_device(pdev);
       
  5467 
       
  5468 	/* Request a slot slot reset. */
       
  5469 	return PCI_ERS_RESULT_NEED_RESET;
       
  5470 }
       
  5471 
       
  5472 /**
       
  5473  * e1000_io_slot_reset - called after the pci bus has been reset.
       
  5474  * @pdev: Pointer to PCI device
       
  5475  *
       
  5476  * Restart the card from scratch, as if from a cold-boot. Implementation
       
  5477  * resembles the first-half of the e1000_resume routine.
       
  5478  */
       
  5479 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
       
  5480 {
       
  5481 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5482 	struct e1000_adapter *adapter = netdev->priv;
       
  5483 
       
  5484 	if (pci_enable_device(pdev)) {
       
  5485 		printk(KERN_ERR "e1000: Cannot re-enable PCI device after reset.\n");
       
  5486 		return PCI_ERS_RESULT_DISCONNECT;
       
  5487 	}
       
  5488 	pci_set_master(pdev);
       
  5489 
       
  5490 	pci_enable_wake(pdev, PCI_D3hot, 0);
       
  5491 	pci_enable_wake(pdev, PCI_D3cold, 0);
       
  5492 
       
  5493 	e1000_reset(adapter);
       
  5494 	E1000_WRITE_REG(&adapter->hw, WUS, ~0);
       
  5495 
       
  5496 	return PCI_ERS_RESULT_RECOVERED;
       
  5497 }
       
  5498 
       
  5499 /**
       
  5500  * e1000_io_resume - called when traffic can start flowing again.
       
  5501  * @pdev: Pointer to PCI device
       
  5502  *
       
  5503  * This callback is called when the error recovery driver tells us that
       
  5504  * its OK to resume normal operation. Implementation resembles the
       
  5505  * second-half of the e1000_resume routine.
       
  5506  */
       
  5507 static void e1000_io_resume(struct pci_dev *pdev)
       
  5508 {
       
  5509 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5510 	struct e1000_adapter *adapter = netdev->priv;
       
  5511 
       
  5512 	e1000_init_manageability(adapter);
       
  5513 
       
  5514 	if (netif_running(netdev)) {
       
  5515 		if (e1000_up(adapter)) {
       
  5516 			printk("e1000: can't bring device back up after reset\n");
       
  5517 			return;
       
  5518 		}
       
  5519 	}
       
  5520 
       
  5521 	netif_device_attach(netdev);
       
  5522 
       
  5523 	/* If the controller is 82573 and f/w is AMT, do not set
       
  5524 	 * DRV_LOAD until the interface is up.  For all other cases,
       
  5525 	 * let the f/w know that the h/w is now under the control
       
  5526 	 * of the driver. */
       
  5527 	if (adapter->hw.mac_type != e1000_82573 ||
       
  5528 	    !e1000_check_mng_mode(&adapter->hw))
       
  5529 		e1000_get_hw_control(adapter);
       
  5530 
       
  5531 }
       
  5532 
       
  5533 /* e1000_main.c */