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