devices/e1000/e1000_main-3.16-ethercat.c
branchstable-1.5
changeset 2588 792892ab4806
child 2598 19ff84bbbcb3
equal deleted inserted replaced
2587:afd76ee3aa87 2588:792892ab4806
       
     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.16-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 
       
  2767 	if (skb_is_gso(skb)) {
       
  2768 		int err;
       
  2769 
       
  2770 		err = skb_cow_head(skb, 0);
       
  2771 		if (err < 0)
       
  2772 			return err;
       
  2773 
       
  2774 		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
       
  2775 		mss = skb_shinfo(skb)->gso_size;
       
  2776 		if (skb->protocol == htons(ETH_P_IP)) {
       
  2777 			struct iphdr *iph = ip_hdr(skb);
       
  2778 			iph->tot_len = 0;
       
  2779 			iph->check = 0;
       
  2780 			tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr,
       
  2781 								 iph->daddr, 0,
       
  2782 								 IPPROTO_TCP,
       
  2783 								 0);
       
  2784 			cmd_length = E1000_TXD_CMD_IP;
       
  2785 			ipcse = skb_transport_offset(skb) - 1;
       
  2786 		} else if (skb->protocol == htons(ETH_P_IPV6)) {
       
  2787 			ipv6_hdr(skb)->payload_len = 0;
       
  2788 			tcp_hdr(skb)->check =
       
  2789 				~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
       
  2790 						 &ipv6_hdr(skb)->daddr,
       
  2791 						 0, IPPROTO_TCP, 0);
       
  2792 			ipcse = 0;
       
  2793 		}
       
  2794 		ipcss = skb_network_offset(skb);
       
  2795 		ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
       
  2796 		tucss = skb_transport_offset(skb);
       
  2797 		tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
       
  2798 		tucse = 0;
       
  2799 
       
  2800 		cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
       
  2801 			       E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
       
  2802 
       
  2803 		i = tx_ring->next_to_use;
       
  2804 		context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
       
  2805 		buffer_info = &tx_ring->buffer_info[i];
       
  2806 
       
  2807 		context_desc->lower_setup.ip_fields.ipcss  = ipcss;
       
  2808 		context_desc->lower_setup.ip_fields.ipcso  = ipcso;
       
  2809 		context_desc->lower_setup.ip_fields.ipcse  = cpu_to_le16(ipcse);
       
  2810 		context_desc->upper_setup.tcp_fields.tucss = tucss;
       
  2811 		context_desc->upper_setup.tcp_fields.tucso = tucso;
       
  2812 		context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
       
  2813 		context_desc->tcp_seg_setup.fields.mss     = cpu_to_le16(mss);
       
  2814 		context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
       
  2815 		context_desc->cmd_and_length = cpu_to_le32(cmd_length);
       
  2816 
       
  2817 		buffer_info->time_stamp = jiffies;
       
  2818 		buffer_info->next_to_watch = i;
       
  2819 
       
  2820 		if (++i == tx_ring->count) i = 0;
       
  2821 		tx_ring->next_to_use = i;
       
  2822 
       
  2823 		return true;
       
  2824 	}
       
  2825 	return false;
       
  2826 }
       
  2827 
       
  2828 static bool e1000_tx_csum(struct e1000_adapter *adapter,
       
  2829 			  struct e1000_tx_ring *tx_ring, struct sk_buff *skb)
       
  2830 {
       
  2831 	struct e1000_context_desc *context_desc;
       
  2832 	struct e1000_buffer *buffer_info;
       
  2833 	unsigned int i;
       
  2834 	u8 css;
       
  2835 	u32 cmd_len = E1000_TXD_CMD_DEXT;
       
  2836 
       
  2837 	if (skb->ip_summed != CHECKSUM_PARTIAL)
       
  2838 		return false;
       
  2839 
       
  2840 	switch (skb->protocol) {
       
  2841 	case cpu_to_be16(ETH_P_IP):
       
  2842 		if (ip_hdr(skb)->protocol == IPPROTO_TCP)
       
  2843 			cmd_len |= E1000_TXD_CMD_TCP;
       
  2844 		break;
       
  2845 	case cpu_to_be16(ETH_P_IPV6):
       
  2846 		/* XXX not handling all IPV6 headers */
       
  2847 		if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
       
  2848 			cmd_len |= E1000_TXD_CMD_TCP;
       
  2849 		break;
       
  2850 	default:
       
  2851 		if (unlikely(net_ratelimit()))
       
  2852 			e_warn(drv, "checksum_partial proto=%x!\n",
       
  2853 			       skb->protocol);
       
  2854 		break;
       
  2855 	}
       
  2856 
       
  2857 	css = skb_checksum_start_offset(skb);
       
  2858 
       
  2859 	i = tx_ring->next_to_use;
       
  2860 	buffer_info = &tx_ring->buffer_info[i];
       
  2861 	context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
       
  2862 
       
  2863 	context_desc->lower_setup.ip_config = 0;
       
  2864 	context_desc->upper_setup.tcp_fields.tucss = css;
       
  2865 	context_desc->upper_setup.tcp_fields.tucso =
       
  2866 		css + skb->csum_offset;
       
  2867 	context_desc->upper_setup.tcp_fields.tucse = 0;
       
  2868 	context_desc->tcp_seg_setup.data = 0;
       
  2869 	context_desc->cmd_and_length = cpu_to_le32(cmd_len);
       
  2870 
       
  2871 	buffer_info->time_stamp = jiffies;
       
  2872 	buffer_info->next_to_watch = i;
       
  2873 
       
  2874 	if (unlikely(++i == tx_ring->count)) i = 0;
       
  2875 	tx_ring->next_to_use = i;
       
  2876 
       
  2877 	return true;
       
  2878 }
       
  2879 
       
  2880 #define E1000_MAX_TXD_PWR	12
       
  2881 #define E1000_MAX_DATA_PER_TXD	(1<<E1000_MAX_TXD_PWR)
       
  2882 
       
  2883 static int e1000_tx_map(struct e1000_adapter *adapter,
       
  2884 			struct e1000_tx_ring *tx_ring,
       
  2885 			struct sk_buff *skb, unsigned int first,
       
  2886 			unsigned int max_per_txd, unsigned int nr_frags,
       
  2887 			unsigned int mss)
       
  2888 {
       
  2889 	struct e1000_hw *hw = &adapter->hw;
       
  2890 	struct pci_dev *pdev = adapter->pdev;
       
  2891 	struct e1000_buffer *buffer_info;
       
  2892 	unsigned int len = skb_headlen(skb);
       
  2893 	unsigned int offset = 0, size, count = 0, i;
       
  2894 	unsigned int f, bytecount, segs;
       
  2895 
       
  2896 	i = tx_ring->next_to_use;
       
  2897 
       
  2898 	while (len) {
       
  2899 		buffer_info = &tx_ring->buffer_info[i];
       
  2900 		size = min(len, max_per_txd);
       
  2901 		/* Workaround for Controller erratum --
       
  2902 		 * descriptor for non-tso packet in a linear SKB that follows a
       
  2903 		 * tso gets written back prematurely before the data is fully
       
  2904 		 * DMA'd to the controller
       
  2905 		 */
       
  2906 		if (!skb->data_len && tx_ring->last_tx_tso &&
       
  2907 		    !skb_is_gso(skb)) {
       
  2908 			tx_ring->last_tx_tso = false;
       
  2909 			size -= 4;
       
  2910 		}
       
  2911 
       
  2912 		/* Workaround for premature desc write-backs
       
  2913 		 * in TSO mode.  Append 4-byte sentinel desc
       
  2914 		 */
       
  2915 		if (unlikely(mss && !nr_frags && size == len && size > 8))
       
  2916 			size -= 4;
       
  2917 		/* work-around for errata 10 and it applies
       
  2918 		 * to all controllers in PCI-X mode
       
  2919 		 * The fix is to make sure that the first descriptor of a
       
  2920 		 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
       
  2921 		 */
       
  2922 		if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
       
  2923 		                (size > 2015) && count == 0))
       
  2924 		        size = 2015;
       
  2925 
       
  2926 		/* Workaround for potential 82544 hang in PCI-X.  Avoid
       
  2927 		 * terminating buffers within evenly-aligned dwords.
       
  2928 		 */
       
  2929 		if (unlikely(adapter->pcix_82544 &&
       
  2930 		   !((unsigned long)(skb->data + offset + size - 1) & 4) &&
       
  2931 		   size > 4))
       
  2932 			size -= 4;
       
  2933 
       
  2934 		buffer_info->length = size;
       
  2935 		/* set time_stamp *before* dma to help avoid a possible race */
       
  2936 		buffer_info->time_stamp = jiffies;
       
  2937 		buffer_info->mapped_as_page = false;
       
  2938 		buffer_info->dma = dma_map_single(&pdev->dev,
       
  2939 						  skb->data + offset,
       
  2940 						  size, DMA_TO_DEVICE);
       
  2941 		if (dma_mapping_error(&pdev->dev, buffer_info->dma))
       
  2942 			goto dma_error;
       
  2943 		buffer_info->next_to_watch = i;
       
  2944 
       
  2945 		len -= size;
       
  2946 		offset += size;
       
  2947 		count++;
       
  2948 		if (len) {
       
  2949 			i++;
       
  2950 			if (unlikely(i == tx_ring->count))
       
  2951 				i = 0;
       
  2952 		}
       
  2953 	}
       
  2954 
       
  2955 	for (f = 0; f < nr_frags; f++) {
       
  2956 		const struct skb_frag_struct *frag;
       
  2957 
       
  2958 		frag = &skb_shinfo(skb)->frags[f];
       
  2959 		len = skb_frag_size(frag);
       
  2960 		offset = 0;
       
  2961 
       
  2962 		while (len) {
       
  2963 			unsigned long bufend;
       
  2964 			i++;
       
  2965 			if (unlikely(i == tx_ring->count))
       
  2966 				i = 0;
       
  2967 
       
  2968 			buffer_info = &tx_ring->buffer_info[i];
       
  2969 			size = min(len, max_per_txd);
       
  2970 			/* Workaround for premature desc write-backs
       
  2971 			 * in TSO mode.  Append 4-byte sentinel desc
       
  2972 			 */
       
  2973 			if (unlikely(mss && f == (nr_frags-1) &&
       
  2974 			    size == len && size > 8))
       
  2975 				size -= 4;
       
  2976 			/* Workaround for potential 82544 hang in PCI-X.
       
  2977 			 * Avoid terminating buffers within evenly-aligned
       
  2978 			 * dwords.
       
  2979 			 */
       
  2980 			bufend = (unsigned long)
       
  2981 				page_to_phys(skb_frag_page(frag));
       
  2982 			bufend += offset + size - 1;
       
  2983 			if (unlikely(adapter->pcix_82544 &&
       
  2984 				     !(bufend & 4) &&
       
  2985 				     size > 4))
       
  2986 				size -= 4;
       
  2987 
       
  2988 			buffer_info->length = size;
       
  2989 			buffer_info->time_stamp = jiffies;
       
  2990 			buffer_info->mapped_as_page = true;
       
  2991 			buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
       
  2992 						offset, size, DMA_TO_DEVICE);
       
  2993 			if (dma_mapping_error(&pdev->dev, buffer_info->dma))
       
  2994 				goto dma_error;
       
  2995 			buffer_info->next_to_watch = i;
       
  2996 
       
  2997 			len -= size;
       
  2998 			offset += size;
       
  2999 			count++;
       
  3000 		}
       
  3001 	}
       
  3002 
       
  3003 	segs = skb_shinfo(skb)->gso_segs ?: 1;
       
  3004 	/* multiply data chunks by size of headers */
       
  3005 	bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
       
  3006 
       
  3007 	tx_ring->buffer_info[i].skb = skb;
       
  3008 	tx_ring->buffer_info[i].segs = segs;
       
  3009 	tx_ring->buffer_info[i].bytecount = bytecount;
       
  3010 	tx_ring->buffer_info[first].next_to_watch = i;
       
  3011 
       
  3012 	return count;
       
  3013 
       
  3014 dma_error:
       
  3015 	dev_err(&pdev->dev, "TX DMA map failed\n");
       
  3016 	buffer_info->dma = 0;
       
  3017 	if (count)
       
  3018 		count--;
       
  3019 
       
  3020 	while (count--) {
       
  3021 		if (i==0)
       
  3022 			i += tx_ring->count;
       
  3023 		i--;
       
  3024 		buffer_info = &tx_ring->buffer_info[i];
       
  3025 		e1000_unmap_and_free_tx_resource(adapter, buffer_info);
       
  3026 	}
       
  3027 
       
  3028 	return 0;
       
  3029 }
       
  3030 
       
  3031 static void e1000_tx_queue(struct e1000_adapter *adapter,
       
  3032 			   struct e1000_tx_ring *tx_ring, int tx_flags,
       
  3033 			   int count)
       
  3034 {
       
  3035 	struct e1000_hw *hw = &adapter->hw;
       
  3036 	struct e1000_tx_desc *tx_desc = NULL;
       
  3037 	struct e1000_buffer *buffer_info;
       
  3038 	u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
       
  3039 	unsigned int i;
       
  3040 
       
  3041 	if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
       
  3042 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
       
  3043 			     E1000_TXD_CMD_TSE;
       
  3044 		txd_upper |= E1000_TXD_POPTS_TXSM << 8;
       
  3045 
       
  3046 		if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
       
  3047 			txd_upper |= E1000_TXD_POPTS_IXSM << 8;
       
  3048 	}
       
  3049 
       
  3050 	if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
       
  3051 		txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
       
  3052 		txd_upper |= E1000_TXD_POPTS_TXSM << 8;
       
  3053 	}
       
  3054 
       
  3055 	if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
       
  3056 		txd_lower |= E1000_TXD_CMD_VLE;
       
  3057 		txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
       
  3058 	}
       
  3059 
       
  3060 	if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
       
  3061 		txd_lower &= ~(E1000_TXD_CMD_IFCS);
       
  3062 
       
  3063 	i = tx_ring->next_to_use;
       
  3064 
       
  3065 	while (count--) {
       
  3066 		buffer_info = &tx_ring->buffer_info[i];
       
  3067 		tx_desc = E1000_TX_DESC(*tx_ring, i);
       
  3068 		tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
       
  3069 		tx_desc->lower.data =
       
  3070 			cpu_to_le32(txd_lower | buffer_info->length);
       
  3071 		tx_desc->upper.data = cpu_to_le32(txd_upper);
       
  3072 		if (unlikely(++i == tx_ring->count)) i = 0;
       
  3073 	}
       
  3074 
       
  3075 	tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
       
  3076 
       
  3077 	/* txd_cmd re-enables FCS, so we'll re-disable it here as desired. */
       
  3078 	if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
       
  3079 		tx_desc->lower.data &= ~(cpu_to_le32(E1000_TXD_CMD_IFCS));
       
  3080 
       
  3081 	/* Force memory writes to complete before letting h/w
       
  3082 	 * know there are new descriptors to fetch.  (Only
       
  3083 	 * applicable for weak-ordered memory model archs,
       
  3084 	 * such as IA-64).
       
  3085 	 */
       
  3086 	wmb();
       
  3087 
       
  3088 	tx_ring->next_to_use = i;
       
  3089 	writel(i, hw->hw_addr + tx_ring->tdt);
       
  3090 	/* we need this if more than one processor can write to our tail
       
  3091 	 * at a time, it synchronizes IO on IA64/Altix systems
       
  3092 	 */
       
  3093 	mmiowb();
       
  3094 }
       
  3095 
       
  3096 /* 82547 workaround to avoid controller hang in half-duplex environment.
       
  3097  * The workaround is to avoid queuing a large packet that would span
       
  3098  * the internal Tx FIFO ring boundary by notifying the stack to resend
       
  3099  * the packet at a later time.  This gives the Tx FIFO an opportunity to
       
  3100  * flush all packets.  When that occurs, we reset the Tx FIFO pointers
       
  3101  * to the beginning of the Tx FIFO.
       
  3102  */
       
  3103 
       
  3104 #define E1000_FIFO_HDR			0x10
       
  3105 #define E1000_82547_PAD_LEN		0x3E0
       
  3106 
       
  3107 static int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
       
  3108 				       struct sk_buff *skb)
       
  3109 {
       
  3110 	u32 fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
       
  3111 	u32 skb_fifo_len = skb->len + E1000_FIFO_HDR;
       
  3112 
       
  3113 	skb_fifo_len = ALIGN(skb_fifo_len, E1000_FIFO_HDR);
       
  3114 
       
  3115 	if (adapter->link_duplex != HALF_DUPLEX)
       
  3116 		goto no_fifo_stall_required;
       
  3117 
       
  3118 	if (atomic_read(&adapter->tx_fifo_stall))
       
  3119 		return 1;
       
  3120 
       
  3121 	if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
       
  3122 		atomic_set(&adapter->tx_fifo_stall, 1);
       
  3123 		return 1;
       
  3124 	}
       
  3125 
       
  3126 no_fifo_stall_required:
       
  3127 	adapter->tx_fifo_head += skb_fifo_len;
       
  3128 	if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
       
  3129 		adapter->tx_fifo_head -= adapter->tx_fifo_size;
       
  3130 	return 0;
       
  3131 }
       
  3132 
       
  3133 static int __e1000_maybe_stop_tx(struct net_device *netdev, int size)
       
  3134 {
       
  3135 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3136 	struct e1000_tx_ring *tx_ring = adapter->tx_ring;
       
  3137 
       
  3138 	if (adapter->ecdev) {
       
  3139 		return -EBUSY;
       
  3140 	}
       
  3141 
       
  3142 	netif_stop_queue(netdev);
       
  3143 	/* Herbert's original patch had:
       
  3144 	 *  smp_mb__after_netif_stop_queue();
       
  3145 	 * but since that doesn't exist yet, just open code it.
       
  3146 	 */
       
  3147 	smp_mb();
       
  3148 
       
  3149 	/* We need to check again in a case another CPU has just
       
  3150 	 * made room available.
       
  3151 	 */
       
  3152 	if (likely(E1000_DESC_UNUSED(tx_ring) < size))
       
  3153 		return -EBUSY;
       
  3154 
       
  3155 	/* A reprieve! */
       
  3156 	netif_start_queue(netdev);
       
  3157 	++adapter->restart_queue;
       
  3158 	return 0;
       
  3159 }
       
  3160 
       
  3161 static int e1000_maybe_stop_tx(struct net_device *netdev,
       
  3162 			       struct e1000_tx_ring *tx_ring, int size)
       
  3163 {
       
  3164 	if (likely(E1000_DESC_UNUSED(tx_ring) >= size))
       
  3165 		return 0;
       
  3166 	return __e1000_maybe_stop_tx(netdev, size);
       
  3167 }
       
  3168 
       
  3169 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
       
  3170 static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
       
  3171 				    struct net_device *netdev)
       
  3172 {
       
  3173 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3174 	struct e1000_hw *hw = &adapter->hw;
       
  3175 	struct e1000_tx_ring *tx_ring;
       
  3176 	unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
       
  3177 	unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
       
  3178 	unsigned int tx_flags = 0;
       
  3179 	unsigned int len = skb_headlen(skb);
       
  3180 	unsigned int nr_frags;
       
  3181 	unsigned int mss;
       
  3182 	int count = 0;
       
  3183 	int tso;
       
  3184 	unsigned int f;
       
  3185 
       
  3186 	/* This goes back to the question of how to logically map a Tx queue
       
  3187 	 * to a flow.  Right now, performance is impacted slightly negatively
       
  3188 	 * if using multiple Tx queues.  If the stack breaks away from a
       
  3189 	 * single qdisc implementation, we can look at this again.
       
  3190 	 */
       
  3191 	tx_ring = adapter->tx_ring;
       
  3192 
       
  3193 	/* On PCI/PCI-X HW, if packet size is less than ETH_ZLEN,
       
  3194 	 * packets may get corrupted during padding by HW.
       
  3195 	 * To WA this issue, pad all small packets manually.
       
  3196 	 */
       
  3197 	if (skb->len < ETH_ZLEN) {
       
  3198 		if (skb_pad(skb, ETH_ZLEN - skb->len))
       
  3199 			return NETDEV_TX_OK;
       
  3200 		skb->len = ETH_ZLEN;
       
  3201 		skb_set_tail_pointer(skb, ETH_ZLEN);
       
  3202 	}
       
  3203 
       
  3204 	mss = skb_shinfo(skb)->gso_size;
       
  3205 	/* The controller does a simple calculation to
       
  3206 	 * make sure there is enough room in the FIFO before
       
  3207 	 * initiating the DMA for each buffer.  The calc is:
       
  3208 	 * 4 = ceil(buffer len/mss).  To make sure we don't
       
  3209 	 * overrun the FIFO, adjust the max buffer len if mss
       
  3210 	 * drops.
       
  3211 	 */
       
  3212 	if (mss) {
       
  3213 		u8 hdr_len;
       
  3214 		max_per_txd = min(mss << 2, max_per_txd);
       
  3215 		max_txd_pwr = fls(max_per_txd) - 1;
       
  3216 
       
  3217 		hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
       
  3218 		if (skb->data_len && hdr_len == len) {
       
  3219 			switch (hw->mac_type) {
       
  3220 				unsigned int pull_size;
       
  3221 			case e1000_82544:
       
  3222 				/* Make sure we have room to chop off 4 bytes,
       
  3223 				 * and that the end alignment will work out to
       
  3224 				 * this hardware's requirements
       
  3225 				 * NOTE: this is a TSO only workaround
       
  3226 				 * if end byte alignment not correct move us
       
  3227 				 * into the next dword
       
  3228 				 */
       
  3229 				if ((unsigned long)(skb_tail_pointer(skb) - 1)
       
  3230 				    & 4)
       
  3231 					break;
       
  3232 				/* fall through */
       
  3233 				pull_size = min((unsigned int)4, skb->data_len);
       
  3234 				if (!__pskb_pull_tail(skb, pull_size)) {
       
  3235 					e_err(drv, "__pskb_pull_tail "
       
  3236 					      "failed.\n");
       
  3237 					if (!adapter->ecdev) {
       
  3238 						dev_kfree_skb_any(skb);
       
  3239 					}
       
  3240 					return NETDEV_TX_OK;
       
  3241 				}
       
  3242 				len = skb_headlen(skb);
       
  3243 				break;
       
  3244 			default:
       
  3245 				/* do nothing */
       
  3246 				break;
       
  3247 			}
       
  3248 		}
       
  3249 	}
       
  3250 
       
  3251 	/* reserve a descriptor for the offload context */
       
  3252 	if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
       
  3253 		count++;
       
  3254 	count++;
       
  3255 
       
  3256 	/* Controller Erratum workaround */
       
  3257 	if (!skb->data_len && tx_ring->last_tx_tso && !skb_is_gso(skb))
       
  3258 		count++;
       
  3259 
       
  3260 	count += TXD_USE_COUNT(len, max_txd_pwr);
       
  3261 
       
  3262 	if (adapter->pcix_82544)
       
  3263 		count++;
       
  3264 
       
  3265 	/* work-around for errata 10 and it applies to all controllers
       
  3266 	 * in PCI-X mode, so add one more descriptor to the count
       
  3267 	 */
       
  3268 	if (unlikely((hw->bus_type == e1000_bus_type_pcix) &&
       
  3269 			(len > 2015)))
       
  3270 		count++;
       
  3271 
       
  3272 	nr_frags = skb_shinfo(skb)->nr_frags;
       
  3273 	for (f = 0; f < nr_frags; f++)
       
  3274 		count += TXD_USE_COUNT(skb_frag_size(&skb_shinfo(skb)->frags[f]),
       
  3275 				       max_txd_pwr);
       
  3276 	if (adapter->pcix_82544)
       
  3277 		count += nr_frags;
       
  3278 
       
  3279 	/* need: count + 2 desc gap to keep tail from touching
       
  3280 	 * head, otherwise try next time
       
  3281 	 */
       
  3282 	if (unlikely(e1000_maybe_stop_tx(netdev, tx_ring, count + 2)))
       
  3283 		return NETDEV_TX_BUSY;
       
  3284 
       
  3285 	if (unlikely((hw->mac_type == e1000_82547) &&
       
  3286 		     (e1000_82547_fifo_workaround(adapter, skb)))) {
       
  3287 		if (!adapter->ecdev) {
       
  3288 			netif_stop_queue(netdev);
       
  3289 			if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  3290 				schedule_delayed_work(&adapter->fifo_stall_task, 1);
       
  3291 		}
       
  3292 		return NETDEV_TX_BUSY;
       
  3293 	}
       
  3294 
       
  3295 	if (vlan_tx_tag_present(skb)) {
       
  3296 		tx_flags |= E1000_TX_FLAGS_VLAN;
       
  3297 		tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
       
  3298 	}
       
  3299 
       
  3300 	first = tx_ring->next_to_use;
       
  3301 
       
  3302 	tso = e1000_tso(adapter, tx_ring, skb);
       
  3303 	if (tso < 0) {
       
  3304 		if (!adapter->ecdev) {
       
  3305 			dev_kfree_skb_any(skb);
       
  3306 		}
       
  3307 		return NETDEV_TX_OK;
       
  3308 	}
       
  3309 
       
  3310 	if (likely(tso)) {
       
  3311 		if (likely(hw->mac_type != e1000_82544))
       
  3312 			tx_ring->last_tx_tso = true;
       
  3313 		tx_flags |= E1000_TX_FLAGS_TSO;
       
  3314 	} else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
       
  3315 		tx_flags |= E1000_TX_FLAGS_CSUM;
       
  3316 
       
  3317 	if (likely(skb->protocol == htons(ETH_P_IP)))
       
  3318 		tx_flags |= E1000_TX_FLAGS_IPV4;
       
  3319 
       
  3320 	if (unlikely(skb->no_fcs))
       
  3321 		tx_flags |= E1000_TX_FLAGS_NO_FCS;
       
  3322 
       
  3323 	count = e1000_tx_map(adapter, tx_ring, skb, first, max_per_txd,
       
  3324 			     nr_frags, mss);
       
  3325 
       
  3326 	if (count) {
       
  3327 		netdev_sent_queue(netdev, skb->len);
       
  3328 		skb_tx_timestamp(skb);
       
  3329 
       
  3330 		e1000_tx_queue(adapter, tx_ring, tx_flags, count);
       
  3331 		if (!adapter->ecdev) {
       
  3332 			/* Make sure there is space in the ring for the next send. */
       
  3333 			e1000_maybe_stop_tx(netdev, tx_ring, MAX_SKB_FRAGS + 2);
       
  3334 		}
       
  3335 
       
  3336 	} else {
       
  3337 		if (!adapter->ecdev) {
       
  3338 			dev_kfree_skb_any(skb);
       
  3339 		}
       
  3340 		tx_ring->buffer_info[first].time_stamp = 0;
       
  3341 		tx_ring->next_to_use = first;
       
  3342 	}
       
  3343 
       
  3344 	return NETDEV_TX_OK;
       
  3345 }
       
  3346 
       
  3347 #define NUM_REGS 38 /* 1 based count */
       
  3348 static void e1000_regdump(struct e1000_adapter *adapter)
       
  3349 {
       
  3350 	struct e1000_hw *hw = &adapter->hw;
       
  3351 	u32 regs[NUM_REGS];
       
  3352 	u32 *regs_buff = regs;
       
  3353 	int i = 0;
       
  3354 
       
  3355 	static const char * const reg_name[] = {
       
  3356 		"CTRL",  "STATUS",
       
  3357 		"RCTL", "RDLEN", "RDH", "RDT", "RDTR",
       
  3358 		"TCTL", "TDBAL", "TDBAH", "TDLEN", "TDH", "TDT",
       
  3359 		"TIDV", "TXDCTL", "TADV", "TARC0",
       
  3360 		"TDBAL1", "TDBAH1", "TDLEN1", "TDH1", "TDT1",
       
  3361 		"TXDCTL1", "TARC1",
       
  3362 		"CTRL_EXT", "ERT", "RDBAL", "RDBAH",
       
  3363 		"TDFH", "TDFT", "TDFHS", "TDFTS", "TDFPC",
       
  3364 		"RDFH", "RDFT", "RDFHS", "RDFTS", "RDFPC"
       
  3365 	};
       
  3366 
       
  3367 	regs_buff[0]  = er32(CTRL);
       
  3368 	regs_buff[1]  = er32(STATUS);
       
  3369 
       
  3370 	regs_buff[2]  = er32(RCTL);
       
  3371 	regs_buff[3]  = er32(RDLEN);
       
  3372 	regs_buff[4]  = er32(RDH);
       
  3373 	regs_buff[5]  = er32(RDT);
       
  3374 	regs_buff[6]  = er32(RDTR);
       
  3375 
       
  3376 	regs_buff[7]  = er32(TCTL);
       
  3377 	regs_buff[8]  = er32(TDBAL);
       
  3378 	regs_buff[9]  = er32(TDBAH);
       
  3379 	regs_buff[10] = er32(TDLEN);
       
  3380 	regs_buff[11] = er32(TDH);
       
  3381 	regs_buff[12] = er32(TDT);
       
  3382 	regs_buff[13] = er32(TIDV);
       
  3383 	regs_buff[14] = er32(TXDCTL);
       
  3384 	regs_buff[15] = er32(TADV);
       
  3385 	regs_buff[16] = er32(TARC0);
       
  3386 
       
  3387 	regs_buff[17] = er32(TDBAL1);
       
  3388 	regs_buff[18] = er32(TDBAH1);
       
  3389 	regs_buff[19] = er32(TDLEN1);
       
  3390 	regs_buff[20] = er32(TDH1);
       
  3391 	regs_buff[21] = er32(TDT1);
       
  3392 	regs_buff[22] = er32(TXDCTL1);
       
  3393 	regs_buff[23] = er32(TARC1);
       
  3394 	regs_buff[24] = er32(CTRL_EXT);
       
  3395 	regs_buff[25] = er32(ERT);
       
  3396 	regs_buff[26] = er32(RDBAL0);
       
  3397 	regs_buff[27] = er32(RDBAH0);
       
  3398 	regs_buff[28] = er32(TDFH);
       
  3399 	regs_buff[29] = er32(TDFT);
       
  3400 	regs_buff[30] = er32(TDFHS);
       
  3401 	regs_buff[31] = er32(TDFTS);
       
  3402 	regs_buff[32] = er32(TDFPC);
       
  3403 	regs_buff[33] = er32(RDFH);
       
  3404 	regs_buff[34] = er32(RDFT);
       
  3405 	regs_buff[35] = er32(RDFHS);
       
  3406 	regs_buff[36] = er32(RDFTS);
       
  3407 	regs_buff[37] = er32(RDFPC);
       
  3408 
       
  3409 	pr_info("Register dump\n");
       
  3410 	for (i = 0; i < NUM_REGS; i++)
       
  3411 		pr_info("%-15s  %08x\n", reg_name[i], regs_buff[i]);
       
  3412 }
       
  3413 
       
  3414 /*
       
  3415  * e1000_dump: Print registers, tx ring and rx ring
       
  3416  */
       
  3417 static void e1000_dump(struct e1000_adapter *adapter)
       
  3418 {
       
  3419 	/* this code doesn't handle multiple rings */
       
  3420 	struct e1000_tx_ring *tx_ring = adapter->tx_ring;
       
  3421 	struct e1000_rx_ring *rx_ring = adapter->rx_ring;
       
  3422 	int i;
       
  3423 
       
  3424 	if (!netif_msg_hw(adapter))
       
  3425 		return;
       
  3426 
       
  3427 	/* Print Registers */
       
  3428 	e1000_regdump(adapter);
       
  3429 
       
  3430 	/* transmit dump */
       
  3431 	pr_info("TX Desc ring0 dump\n");
       
  3432 
       
  3433 	/* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
       
  3434 	 *
       
  3435 	 * Legacy Transmit Descriptor
       
  3436 	 *   +--------------------------------------------------------------+
       
  3437 	 * 0 |         Buffer Address [63:0] (Reserved on Write Back)       |
       
  3438 	 *   +--------------------------------------------------------------+
       
  3439 	 * 8 | Special  |    CSS     | Status |  CMD    |  CSO   |  Length  |
       
  3440 	 *   +--------------------------------------------------------------+
       
  3441 	 *   63       48 47        36 35    32 31     24 23    16 15        0
       
  3442 	 *
       
  3443 	 * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
       
  3444 	 *   63      48 47    40 39       32 31             16 15    8 7      0
       
  3445 	 *   +----------------------------------------------------------------+
       
  3446 	 * 0 |  TUCSE  | TUCS0  |   TUCSS   |     IPCSE       | IPCS0 | IPCSS |
       
  3447 	 *   +----------------------------------------------------------------+
       
  3448 	 * 8 |   MSS   | HDRLEN | RSV | STA | TUCMD | DTYP |      PAYLEN      |
       
  3449 	 *   +----------------------------------------------------------------+
       
  3450 	 *   63      48 47    40 39 36 35 32 31   24 23  20 19                0
       
  3451 	 *
       
  3452 	 * Extended Data Descriptor (DTYP=0x1)
       
  3453 	 *   +----------------------------------------------------------------+
       
  3454 	 * 0 |                     Buffer Address [63:0]                      |
       
  3455 	 *   +----------------------------------------------------------------+
       
  3456 	 * 8 | VLAN tag |  POPTS  | Rsvd | Status | Command | DTYP |  DTALEN  |
       
  3457 	 *   +----------------------------------------------------------------+
       
  3458 	 *   63       48 47     40 39  36 35    32 31     24 23  20 19        0
       
  3459 	 */
       
  3460 	pr_info("Tc[desc]     [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma       ] leng  ntw timestmp         bi->skb\n");
       
  3461 	pr_info("Td[desc]     [address 63:0  ] [VlaPoRSCm1Dlen] [bi->dma       ] leng  ntw timestmp         bi->skb\n");
       
  3462 
       
  3463 	if (!netif_msg_tx_done(adapter))
       
  3464 		goto rx_ring_summary;
       
  3465 
       
  3466 	for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
       
  3467 		struct e1000_tx_desc *tx_desc = E1000_TX_DESC(*tx_ring, i);
       
  3468 		struct e1000_buffer *buffer_info = &tx_ring->buffer_info[i];
       
  3469 		struct my_u { __le64 a; __le64 b; };
       
  3470 		struct my_u *u = (struct my_u *)tx_desc;
       
  3471 		const char *type;
       
  3472 
       
  3473 		if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
       
  3474 			type = "NTC/U";
       
  3475 		else if (i == tx_ring->next_to_use)
       
  3476 			type = "NTU";
       
  3477 		else if (i == tx_ring->next_to_clean)
       
  3478 			type = "NTC";
       
  3479 		else
       
  3480 			type = "";
       
  3481 
       
  3482 		pr_info("T%c[0x%03X]    %016llX %016llX %016llX %04X  %3X %016llX %p %s\n",
       
  3483 			((le64_to_cpu(u->b) & (1<<20)) ? 'd' : 'c'), i,
       
  3484 			le64_to_cpu(u->a), le64_to_cpu(u->b),
       
  3485 			(u64)buffer_info->dma, buffer_info->length,
       
  3486 			buffer_info->next_to_watch,
       
  3487 			(u64)buffer_info->time_stamp, buffer_info->skb, type);
       
  3488 	}
       
  3489 
       
  3490 rx_ring_summary:
       
  3491 	/* receive dump */
       
  3492 	pr_info("\nRX Desc ring dump\n");
       
  3493 
       
  3494 	/* Legacy Receive Descriptor Format
       
  3495 	 *
       
  3496 	 * +-----------------------------------------------------+
       
  3497 	 * |                Buffer Address [63:0]                |
       
  3498 	 * +-----------------------------------------------------+
       
  3499 	 * | VLAN Tag | Errors | Status 0 | Packet csum | Length |
       
  3500 	 * +-----------------------------------------------------+
       
  3501 	 * 63       48 47    40 39      32 31         16 15      0
       
  3502 	 */
       
  3503 	pr_info("R[desc]      [address 63:0  ] [vl er S cks ln] [bi->dma       ] [bi->skb]\n");
       
  3504 
       
  3505 	if (!netif_msg_rx_status(adapter))
       
  3506 		goto exit;
       
  3507 
       
  3508 	for (i = 0; rx_ring->desc && (i < rx_ring->count); i++) {
       
  3509 		struct e1000_rx_desc *rx_desc = E1000_RX_DESC(*rx_ring, i);
       
  3510 		struct e1000_buffer *buffer_info = &rx_ring->buffer_info[i];
       
  3511 		struct my_u { __le64 a; __le64 b; };
       
  3512 		struct my_u *u = (struct my_u *)rx_desc;
       
  3513 		const char *type;
       
  3514 
       
  3515 		if (i == rx_ring->next_to_use)
       
  3516 			type = "NTU";
       
  3517 		else if (i == rx_ring->next_to_clean)
       
  3518 			type = "NTC";
       
  3519 		else
       
  3520 			type = "";
       
  3521 
       
  3522 		pr_info("R[0x%03X]     %016llX %016llX %016llX %p %s\n",
       
  3523 			i, le64_to_cpu(u->a), le64_to_cpu(u->b),
       
  3524 			(u64)buffer_info->dma, buffer_info->skb, type);
       
  3525 	} /* for */
       
  3526 
       
  3527 	/* dump the descriptor caches */
       
  3528 	/* rx */
       
  3529 	pr_info("Rx descriptor cache in 64bit format\n");
       
  3530 	for (i = 0x6000; i <= 0x63FF ; i += 0x10) {
       
  3531 		pr_info("R%04X: %08X|%08X %08X|%08X\n",
       
  3532 			i,
       
  3533 			readl(adapter->hw.hw_addr + i+4),
       
  3534 			readl(adapter->hw.hw_addr + i),
       
  3535 			readl(adapter->hw.hw_addr + i+12),
       
  3536 			readl(adapter->hw.hw_addr + i+8));
       
  3537 	}
       
  3538 	/* tx */
       
  3539 	pr_info("Tx descriptor cache in 64bit format\n");
       
  3540 	for (i = 0x7000; i <= 0x73FF ; i += 0x10) {
       
  3541 		pr_info("T%04X: %08X|%08X %08X|%08X\n",
       
  3542 			i,
       
  3543 			readl(adapter->hw.hw_addr + i+4),
       
  3544 			readl(adapter->hw.hw_addr + i),
       
  3545 			readl(adapter->hw.hw_addr + i+12),
       
  3546 			readl(adapter->hw.hw_addr + i+8));
       
  3547 	}
       
  3548 exit:
       
  3549 	return;
       
  3550 }
       
  3551 
       
  3552 /**
       
  3553  * e1000_tx_timeout - Respond to a Tx Hang
       
  3554  * @netdev: network interface device structure
       
  3555  **/
       
  3556 static void e1000_tx_timeout(struct net_device *netdev)
       
  3557 {
       
  3558 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3559 
       
  3560 	/* Do the reset outside of interrupt context */
       
  3561 	adapter->tx_timeout_count++;
       
  3562 	schedule_work(&adapter->reset_task);
       
  3563 }
       
  3564 
       
  3565 static void e1000_reset_task(struct work_struct *work)
       
  3566 {
       
  3567 	struct e1000_adapter *adapter =
       
  3568 		container_of(work, struct e1000_adapter, reset_task);
       
  3569 
       
  3570 	e_err(drv, "Reset adapter\n");
       
  3571 	e1000_reinit_locked(adapter);
       
  3572 }
       
  3573 
       
  3574 /**
       
  3575  * e1000_get_stats - Get System Network Statistics
       
  3576  * @netdev: network interface device structure
       
  3577  *
       
  3578  * Returns the address of the device statistics structure.
       
  3579  * The statistics are actually updated from the watchdog.
       
  3580  **/
       
  3581 static struct net_device_stats *e1000_get_stats(struct net_device *netdev)
       
  3582 {
       
  3583 	/* only return the current stats */
       
  3584 	return &netdev->stats;
       
  3585 }
       
  3586 
       
  3587 /**
       
  3588  * e1000_change_mtu - Change the Maximum Transfer Unit
       
  3589  * @netdev: network interface device structure
       
  3590  * @new_mtu: new value for maximum frame size
       
  3591  *
       
  3592  * Returns 0 on success, negative on failure
       
  3593  **/
       
  3594 static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
       
  3595 {
       
  3596 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3597 	struct e1000_hw *hw = &adapter->hw;
       
  3598 	int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
       
  3599 
       
  3600 	if (adapter->ecdev) {
       
  3601 		return -EBUSY;
       
  3602 	}
       
  3603 
       
  3604 	if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
       
  3605 	    (max_frame > MAX_JUMBO_FRAME_SIZE)) {
       
  3606 		e_err(probe, "Invalid MTU setting\n");
       
  3607 		return -EINVAL;
       
  3608 	}
       
  3609 
       
  3610 	/* Adapter-specific max frame size limits. */
       
  3611 	switch (hw->mac_type) {
       
  3612 	case e1000_undefined ... e1000_82542_rev2_1:
       
  3613 		if (max_frame > (ETH_FRAME_LEN + ETH_FCS_LEN)) {
       
  3614 			e_err(probe, "Jumbo Frames not supported.\n");
       
  3615 			return -EINVAL;
       
  3616 		}
       
  3617 		break;
       
  3618 	default:
       
  3619 		/* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
       
  3620 		break;
       
  3621 	}
       
  3622 
       
  3623 	while (test_and_set_bit(__E1000_RESETTING, &adapter->flags))
       
  3624 		msleep(1);
       
  3625 	/* e1000_down has a dependency on max_frame_size */
       
  3626 	hw->max_frame_size = max_frame;
       
  3627 	if (netif_running(netdev))
       
  3628 		e1000_down(adapter);
       
  3629 
       
  3630 	/* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
       
  3631 	 * means we reserve 2 more, this pushes us to allocate from the next
       
  3632 	 * larger slab size.
       
  3633 	 * i.e. RXBUFFER_2048 --> size-4096 slab
       
  3634 	 * however with the new *_jumbo_rx* routines, jumbo receives will use
       
  3635 	 * fragmented skbs
       
  3636 	 */
       
  3637 
       
  3638 	if (max_frame <= E1000_RXBUFFER_2048)
       
  3639 		adapter->rx_buffer_len = E1000_RXBUFFER_2048;
       
  3640 	else
       
  3641 #if (PAGE_SIZE >= E1000_RXBUFFER_16384)
       
  3642 		adapter->rx_buffer_len = E1000_RXBUFFER_16384;
       
  3643 #elif (PAGE_SIZE >= E1000_RXBUFFER_4096)
       
  3644 		adapter->rx_buffer_len = PAGE_SIZE;
       
  3645 #endif
       
  3646 
       
  3647 	/* adjust allocation if LPE protects us, and we aren't using SBP */
       
  3648 	if (!hw->tbi_compatibility_on &&
       
  3649 	    ((max_frame == (ETH_FRAME_LEN + ETH_FCS_LEN)) ||
       
  3650 	     (max_frame == MAXIMUM_ETHERNET_VLAN_SIZE)))
       
  3651 		adapter->rx_buffer_len = MAXIMUM_ETHERNET_VLAN_SIZE;
       
  3652 
       
  3653 	pr_info("%s changing MTU from %d to %d\n",
       
  3654 		netdev->name, netdev->mtu, new_mtu);
       
  3655 	netdev->mtu = new_mtu;
       
  3656 
       
  3657 	if (netif_running(netdev))
       
  3658 		e1000_up(adapter);
       
  3659 	else
       
  3660 		e1000_reset(adapter);
       
  3661 
       
  3662 	clear_bit(__E1000_RESETTING, &adapter->flags);
       
  3663 
       
  3664 	return 0;
       
  3665 }
       
  3666 
       
  3667 /**
       
  3668  * e1000_update_stats - Update the board statistics counters
       
  3669  * @adapter: board private structure
       
  3670  **/
       
  3671 void e1000_update_stats(struct e1000_adapter *adapter)
       
  3672 {
       
  3673 	struct net_device *netdev = adapter->netdev;
       
  3674 	struct e1000_hw *hw = &adapter->hw;
       
  3675 	struct pci_dev *pdev = adapter->pdev;
       
  3676 	unsigned long flags = 0;
       
  3677 	u16 phy_tmp;
       
  3678 
       
  3679 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
       
  3680 
       
  3681 	/* Prevent stats update while adapter is being reset, or if the pci
       
  3682 	 * connection is down.
       
  3683 	 */
       
  3684 	if (adapter->link_speed == 0)
       
  3685 		return;
       
  3686 	if (pci_channel_offline(pdev))
       
  3687 		return;
       
  3688 
       
  3689 	if (!adapter->ecdev) {
       
  3690 		spin_lock_irqsave(&adapter->stats_lock, flags);
       
  3691 	}
       
  3692 
       
  3693 	/* these counters are modified from e1000_tbi_adjust_stats,
       
  3694 	 * called from the interrupt context, so they must only
       
  3695 	 * be written while holding adapter->stats_lock
       
  3696 	 */
       
  3697 
       
  3698 	adapter->stats.crcerrs += er32(CRCERRS);
       
  3699 	adapter->stats.gprc += er32(GPRC);
       
  3700 	adapter->stats.gorcl += er32(GORCL);
       
  3701 	adapter->stats.gorch += er32(GORCH);
       
  3702 	adapter->stats.bprc += er32(BPRC);
       
  3703 	adapter->stats.mprc += er32(MPRC);
       
  3704 	adapter->stats.roc += er32(ROC);
       
  3705 
       
  3706 	adapter->stats.prc64 += er32(PRC64);
       
  3707 	adapter->stats.prc127 += er32(PRC127);
       
  3708 	adapter->stats.prc255 += er32(PRC255);
       
  3709 	adapter->stats.prc511 += er32(PRC511);
       
  3710 	adapter->stats.prc1023 += er32(PRC1023);
       
  3711 	adapter->stats.prc1522 += er32(PRC1522);
       
  3712 
       
  3713 	adapter->stats.symerrs += er32(SYMERRS);
       
  3714 	adapter->stats.mpc += er32(MPC);
       
  3715 	adapter->stats.scc += er32(SCC);
       
  3716 	adapter->stats.ecol += er32(ECOL);
       
  3717 	adapter->stats.mcc += er32(MCC);
       
  3718 	adapter->stats.latecol += er32(LATECOL);
       
  3719 	adapter->stats.dc += er32(DC);
       
  3720 	adapter->stats.sec += er32(SEC);
       
  3721 	adapter->stats.rlec += er32(RLEC);
       
  3722 	adapter->stats.xonrxc += er32(XONRXC);
       
  3723 	adapter->stats.xontxc += er32(XONTXC);
       
  3724 	adapter->stats.xoffrxc += er32(XOFFRXC);
       
  3725 	adapter->stats.xofftxc += er32(XOFFTXC);
       
  3726 	adapter->stats.fcruc += er32(FCRUC);
       
  3727 	adapter->stats.gptc += er32(GPTC);
       
  3728 	adapter->stats.gotcl += er32(GOTCL);
       
  3729 	adapter->stats.gotch += er32(GOTCH);
       
  3730 	adapter->stats.rnbc += er32(RNBC);
       
  3731 	adapter->stats.ruc += er32(RUC);
       
  3732 	adapter->stats.rfc += er32(RFC);
       
  3733 	adapter->stats.rjc += er32(RJC);
       
  3734 	adapter->stats.torl += er32(TORL);
       
  3735 	adapter->stats.torh += er32(TORH);
       
  3736 	adapter->stats.totl += er32(TOTL);
       
  3737 	adapter->stats.toth += er32(TOTH);
       
  3738 	adapter->stats.tpr += er32(TPR);
       
  3739 
       
  3740 	adapter->stats.ptc64 += er32(PTC64);
       
  3741 	adapter->stats.ptc127 += er32(PTC127);
       
  3742 	adapter->stats.ptc255 += er32(PTC255);
       
  3743 	adapter->stats.ptc511 += er32(PTC511);
       
  3744 	adapter->stats.ptc1023 += er32(PTC1023);
       
  3745 	adapter->stats.ptc1522 += er32(PTC1522);
       
  3746 
       
  3747 	adapter->stats.mptc += er32(MPTC);
       
  3748 	adapter->stats.bptc += er32(BPTC);
       
  3749 
       
  3750 	/* used for adaptive IFS */
       
  3751 
       
  3752 	hw->tx_packet_delta = er32(TPT);
       
  3753 	adapter->stats.tpt += hw->tx_packet_delta;
       
  3754 	hw->collision_delta = er32(COLC);
       
  3755 	adapter->stats.colc += hw->collision_delta;
       
  3756 
       
  3757 	if (hw->mac_type >= e1000_82543) {
       
  3758 		adapter->stats.algnerrc += er32(ALGNERRC);
       
  3759 		adapter->stats.rxerrc += er32(RXERRC);
       
  3760 		adapter->stats.tncrs += er32(TNCRS);
       
  3761 		adapter->stats.cexterr += er32(CEXTERR);
       
  3762 		adapter->stats.tsctc += er32(TSCTC);
       
  3763 		adapter->stats.tsctfc += er32(TSCTFC);
       
  3764 	}
       
  3765 
       
  3766 	/* Fill out the OS statistics structure */
       
  3767 	netdev->stats.multicast = adapter->stats.mprc;
       
  3768 	netdev->stats.collisions = adapter->stats.colc;
       
  3769 
       
  3770 	/* Rx Errors */
       
  3771 
       
  3772 	/* RLEC on some newer hardware can be incorrect so build
       
  3773 	 * our own version based on RUC and ROC
       
  3774 	 */
       
  3775 	netdev->stats.rx_errors = adapter->stats.rxerrc +
       
  3776 		adapter->stats.crcerrs + adapter->stats.algnerrc +
       
  3777 		adapter->stats.ruc + adapter->stats.roc +
       
  3778 		adapter->stats.cexterr;
       
  3779 	adapter->stats.rlerrc = adapter->stats.ruc + adapter->stats.roc;
       
  3780 	netdev->stats.rx_length_errors = adapter->stats.rlerrc;
       
  3781 	netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
       
  3782 	netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
       
  3783 	netdev->stats.rx_missed_errors = adapter->stats.mpc;
       
  3784 
       
  3785 	/* Tx Errors */
       
  3786 	adapter->stats.txerrc = adapter->stats.ecol + adapter->stats.latecol;
       
  3787 	netdev->stats.tx_errors = adapter->stats.txerrc;
       
  3788 	netdev->stats.tx_aborted_errors = adapter->stats.ecol;
       
  3789 	netdev->stats.tx_window_errors = adapter->stats.latecol;
       
  3790 	netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
       
  3791 	if (hw->bad_tx_carr_stats_fd &&
       
  3792 	    adapter->link_duplex == FULL_DUPLEX) {
       
  3793 		netdev->stats.tx_carrier_errors = 0;
       
  3794 		adapter->stats.tncrs = 0;
       
  3795 	}
       
  3796 
       
  3797 	/* Tx Dropped needs to be maintained elsewhere */
       
  3798 
       
  3799 	/* Phy Stats */
       
  3800 	if (hw->media_type == e1000_media_type_copper) {
       
  3801 		if ((adapter->link_speed == SPEED_1000) &&
       
  3802 		   (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
       
  3803 			phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
       
  3804 			adapter->phy_stats.idle_errors += phy_tmp;
       
  3805 		}
       
  3806 
       
  3807 		if ((hw->mac_type <= e1000_82546) &&
       
  3808 		   (hw->phy_type == e1000_phy_m88) &&
       
  3809 		   !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
       
  3810 			adapter->phy_stats.receive_errors += phy_tmp;
       
  3811 	}
       
  3812 
       
  3813 	/* Management Stats */
       
  3814 	if (hw->has_smbus) {
       
  3815 		adapter->stats.mgptc += er32(MGTPTC);
       
  3816 		adapter->stats.mgprc += er32(MGTPRC);
       
  3817 		adapter->stats.mgpdc += er32(MGTPDC);
       
  3818 	}
       
  3819 
       
  3820 	if (!adapter->ecdev) {
       
  3821 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  3822 	}
       
  3823 }
       
  3824 
       
  3825 void ec_poll(struct net_device *netdev)
       
  3826 {
       
  3827 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3828 	if (jiffies - adapter->ec_watchdog_jiffies >= 2 * HZ) {
       
  3829 		e1000_watchdog(&adapter->watchdog_task.work);
       
  3830 		adapter->ec_watchdog_jiffies = jiffies;
       
  3831 	}
       
  3832 
       
  3833 	e1000_intr(0, netdev);
       
  3834 }
       
  3835 
       
  3836 /**
       
  3837  * e1000_intr - Interrupt Handler
       
  3838  * @irq: interrupt number
       
  3839  * @data: pointer to a network interface device structure
       
  3840  **/
       
  3841 static irqreturn_t e1000_intr(int irq, void *data)
       
  3842 {
       
  3843 	struct net_device *netdev = data;
       
  3844 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  3845 	struct e1000_hw *hw = &adapter->hw;
       
  3846 	u32 icr = er32(ICR);
       
  3847 
       
  3848 	if (unlikely((!icr)))
       
  3849 		return IRQ_NONE;  /* Not our interrupt */
       
  3850 
       
  3851 	/* we might have caused the interrupt, but the above
       
  3852 	 * read cleared it, and just in case the driver is
       
  3853 	 * down there is nothing to do so return handled
       
  3854 	 */
       
  3855 	if (unlikely(test_bit(__E1000_DOWN, &adapter->flags)))
       
  3856 		return IRQ_HANDLED;
       
  3857 
       
  3858 	if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
       
  3859 		hw->get_link_status = 1;
       
  3860 		/* guard against interrupt when we're going down */
       
  3861 		if (!adapter->ecdev && !test_bit(__E1000_DOWN, &adapter->flags))
       
  3862 			schedule_delayed_work(&adapter->watchdog_task, 1);
       
  3863 	}
       
  3864 
       
  3865 	if (adapter->ecdev) {
       
  3866 		int i, ec_work_done = 0;
       
  3867 		for (i = 0; i < E1000_MAX_INTR; i++) {
       
  3868 			if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring,
       
  3869 							&ec_work_done, 100) &&
       
  3870 						!e1000_clean_tx_irq(adapter, adapter->tx_ring))) {
       
  3871 				break;
       
  3872 			}
       
  3873 		}
       
  3874  	} else {
       
  3875 		/* disable interrupts, without the synchronize_irq bit */
       
  3876 		ew32(IMC, ~0);
       
  3877 		E1000_WRITE_FLUSH();
       
  3878 
       
  3879 		if (likely(napi_schedule_prep(&adapter->napi))) {
       
  3880 			adapter->total_tx_bytes = 0;
       
  3881 			adapter->total_tx_packets = 0;
       
  3882 			adapter->total_rx_bytes = 0;
       
  3883 			adapter->total_rx_packets = 0;
       
  3884 			__napi_schedule(&adapter->napi);
       
  3885 		} else {
       
  3886 			/* this really should not happen! if it does it is basically a
       
  3887 			 * bug, but not a hard error, so enable ints and continue
       
  3888 			 */
       
  3889 			if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  3890 				e1000_irq_enable(adapter);
       
  3891 		}
       
  3892 	}
       
  3893 
       
  3894 	return IRQ_HANDLED;
       
  3895 }
       
  3896 
       
  3897 /**
       
  3898  * e1000_clean - NAPI Rx polling callback
       
  3899  * @adapter: board private structure
       
  3900  * EtherCAT: never called
       
  3901  **/
       
  3902 static int e1000_clean(struct napi_struct *napi, int budget)
       
  3903 {
       
  3904 	struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter,
       
  3905 						     napi);
       
  3906 	int tx_clean_complete = 0, work_done = 0;
       
  3907 
       
  3908 	tx_clean_complete = e1000_clean_tx_irq(adapter, &adapter->tx_ring[0]);
       
  3909 
       
  3910 	adapter->clean_rx(adapter, &adapter->rx_ring[0], &work_done, budget);
       
  3911 
       
  3912 	if (!tx_clean_complete)
       
  3913 		work_done = budget;
       
  3914 
       
  3915 	/* If budget not fully consumed, exit the polling mode */
       
  3916 	if (work_done < budget) {
       
  3917 		if (likely(adapter->itr_setting & 3))
       
  3918 			e1000_set_itr(adapter);
       
  3919 		napi_complete(napi);
       
  3920 		if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  3921 			e1000_irq_enable(adapter);
       
  3922 	}
       
  3923 
       
  3924 	return work_done;
       
  3925 }
       
  3926 
       
  3927 /**
       
  3928  * e1000_clean_tx_irq - Reclaim resources after transmit completes
       
  3929  * @adapter: board private structure
       
  3930  **/
       
  3931 static bool e1000_clean_tx_irq(struct e1000_adapter *adapter,
       
  3932 			       struct e1000_tx_ring *tx_ring)
       
  3933 {
       
  3934 	struct e1000_hw *hw = &adapter->hw;
       
  3935 	struct net_device *netdev = adapter->netdev;
       
  3936 	struct e1000_tx_desc *tx_desc, *eop_desc;
       
  3937 	struct e1000_buffer *buffer_info;
       
  3938 	unsigned int i, eop;
       
  3939 	unsigned int count = 0;
       
  3940 	unsigned int total_tx_bytes=0, total_tx_packets=0;
       
  3941 	unsigned int bytes_compl = 0, pkts_compl = 0;
       
  3942 
       
  3943 	i = tx_ring->next_to_clean;
       
  3944 	eop = tx_ring->buffer_info[i].next_to_watch;
       
  3945 	eop_desc = E1000_TX_DESC(*tx_ring, eop);
       
  3946 
       
  3947 	while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
       
  3948 	       (count < tx_ring->count)) {
       
  3949 		bool cleaned = false;
       
  3950 		rmb();	/* read buffer_info after eop_desc */
       
  3951 		for ( ; !cleaned; count++) {
       
  3952 			tx_desc = E1000_TX_DESC(*tx_ring, i);
       
  3953 			buffer_info = &tx_ring->buffer_info[i];
       
  3954 			cleaned = (i == eop);
       
  3955 
       
  3956 			if (cleaned) {
       
  3957 				total_tx_packets += buffer_info->segs;
       
  3958 				total_tx_bytes += buffer_info->bytecount;
       
  3959 				if (buffer_info->skb) {
       
  3960 					bytes_compl += buffer_info->skb->len;
       
  3961 					pkts_compl++;
       
  3962 				}
       
  3963 
       
  3964 			}
       
  3965 			e1000_unmap_and_free_tx_resource(adapter, buffer_info);
       
  3966 			tx_desc->upper.data = 0;
       
  3967 
       
  3968 			if (unlikely(++i == tx_ring->count)) i = 0;
       
  3969 		}
       
  3970 
       
  3971 		eop = tx_ring->buffer_info[i].next_to_watch;
       
  3972 		eop_desc = E1000_TX_DESC(*tx_ring, eop);
       
  3973 	}
       
  3974 
       
  3975 	tx_ring->next_to_clean = i;
       
  3976 
       
  3977 	netdev_completed_queue(netdev, pkts_compl, bytes_compl);
       
  3978 
       
  3979 #define TX_WAKE_THRESHOLD 32
       
  3980 	if (!adapter->ecdev && unlikely(count && netif_carrier_ok(netdev) &&
       
  3981 		     E1000_DESC_UNUSED(tx_ring) >= TX_WAKE_THRESHOLD)) {
       
  3982 		/* Make sure that anybody stopping the queue after this
       
  3983 		 * sees the new next_to_clean.
       
  3984 		 */
       
  3985 		smp_mb();
       
  3986 
       
  3987 		if (netif_queue_stopped(netdev) &&
       
  3988 		    !(test_bit(__E1000_DOWN, &adapter->flags))) {
       
  3989 			netif_wake_queue(netdev);
       
  3990 			++adapter->restart_queue;
       
  3991 		}
       
  3992 	}
       
  3993 
       
  3994 	if (!adapter->ecdev && adapter->detect_tx_hung) {
       
  3995 		/* Detect a transmit hang in hardware, this serializes the
       
  3996 		 * check with the clearing of time_stamp and movement of i
       
  3997 		 */
       
  3998 		adapter->detect_tx_hung = false;
       
  3999 		if (tx_ring->buffer_info[eop].time_stamp &&
       
  4000 		    time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
       
  4001 			       (adapter->tx_timeout_factor * HZ)) &&
       
  4002 		    !(er32(STATUS) & E1000_STATUS_TXOFF)) {
       
  4003 
       
  4004 			/* detected Tx unit hang */
       
  4005 			e_err(drv, "Detected Tx Unit Hang\n"
       
  4006 			      "  Tx Queue             <%lu>\n"
       
  4007 			      "  TDH                  <%x>\n"
       
  4008 			      "  TDT                  <%x>\n"
       
  4009 			      "  next_to_use          <%x>\n"
       
  4010 			      "  next_to_clean        <%x>\n"
       
  4011 			      "buffer_info[next_to_clean]\n"
       
  4012 			      "  time_stamp           <%lx>\n"
       
  4013 			      "  next_to_watch        <%x>\n"
       
  4014 			      "  jiffies              <%lx>\n"
       
  4015 			      "  next_to_watch.status <%x>\n",
       
  4016 				(unsigned long)(tx_ring - adapter->tx_ring),
       
  4017 				readl(hw->hw_addr + tx_ring->tdh),
       
  4018 				readl(hw->hw_addr + tx_ring->tdt),
       
  4019 				tx_ring->next_to_use,
       
  4020 				tx_ring->next_to_clean,
       
  4021 				tx_ring->buffer_info[eop].time_stamp,
       
  4022 				eop,
       
  4023 				jiffies,
       
  4024 				eop_desc->upper.fields.status);
       
  4025 			e1000_dump(adapter);
       
  4026 			netif_stop_queue(netdev);
       
  4027 		}
       
  4028 	}
       
  4029 	adapter->total_tx_bytes += total_tx_bytes;
       
  4030 	adapter->total_tx_packets += total_tx_packets;
       
  4031 	netdev->stats.tx_bytes += total_tx_bytes;
       
  4032 	netdev->stats.tx_packets += total_tx_packets;
       
  4033 	return count < tx_ring->count;
       
  4034 }
       
  4035 
       
  4036 /**
       
  4037  * e1000_rx_checksum - Receive Checksum Offload for 82543
       
  4038  * @adapter:     board private structure
       
  4039  * @status_err:  receive descriptor status and error fields
       
  4040  * @csum:        receive descriptor csum field
       
  4041  * @sk_buff:     socket buffer with received data
       
  4042  **/
       
  4043 static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
       
  4044 			      u32 csum, struct sk_buff *skb)
       
  4045 {
       
  4046 	struct e1000_hw *hw = &adapter->hw;
       
  4047 	u16 status = (u16)status_err;
       
  4048 	u8 errors = (u8)(status_err >> 24);
       
  4049 
       
  4050 	skb_checksum_none_assert(skb);
       
  4051 
       
  4052 	/* 82543 or newer only */
       
  4053 	if (unlikely(hw->mac_type < e1000_82543)) return;
       
  4054 	/* Ignore Checksum bit is set */
       
  4055 	if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
       
  4056 	/* TCP/UDP checksum error bit is set */
       
  4057 	if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
       
  4058 		/* let the stack verify checksum errors */
       
  4059 		adapter->hw_csum_err++;
       
  4060 		return;
       
  4061 	}
       
  4062 	/* TCP/UDP Checksum has not been calculated */
       
  4063 	if (!(status & E1000_RXD_STAT_TCPCS))
       
  4064 		return;
       
  4065 
       
  4066 	/* It must be a TCP or UDP packet with a valid checksum */
       
  4067 	if (likely(status & E1000_RXD_STAT_TCPCS)) {
       
  4068 		/* TCP checksum is good */
       
  4069 		skb->ip_summed = CHECKSUM_UNNECESSARY;
       
  4070 	}
       
  4071 	adapter->hw_csum_good++;
       
  4072 }
       
  4073 
       
  4074 /**
       
  4075  * e1000_consume_page - helper function
       
  4076  **/
       
  4077 static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
       
  4078 			       u16 length)
       
  4079 {
       
  4080 	bi->page = NULL;
       
  4081 	skb->len += length;
       
  4082 	skb->data_len += length;
       
  4083 	skb->truesize += PAGE_SIZE;
       
  4084 }
       
  4085 
       
  4086 /**
       
  4087  * e1000_receive_skb - helper function to handle rx indications
       
  4088  * @adapter: board private structure
       
  4089  * @status: descriptor status field as written by hardware
       
  4090  * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
       
  4091  * @skb: pointer to sk_buff to be indicated to stack
       
  4092  */
       
  4093 static void e1000_receive_skb(struct e1000_adapter *adapter, u8 status,
       
  4094 			      __le16 vlan, struct sk_buff *skb)
       
  4095 {
       
  4096 	skb->protocol = eth_type_trans(skb, adapter->netdev);
       
  4097 
       
  4098 	if (status & E1000_RXD_STAT_VP) {
       
  4099 		u16 vid = le16_to_cpu(vlan) & E1000_RXD_SPC_VLAN_MASK;
       
  4100 
       
  4101 		__vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vid);
       
  4102 	}
       
  4103 	napi_gro_receive(&adapter->napi, skb);
       
  4104 }
       
  4105 
       
  4106 /**
       
  4107  * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
       
  4108  * @adapter: board private structure
       
  4109  * @rx_ring: ring to clean
       
  4110  * @work_done: amount of napi work completed this call
       
  4111  * @work_to_do: max amount of work allowed for this call to do
       
  4112  *
       
  4113  * the return value indicates whether actual cleaning was done, there
       
  4114  * is no guarantee that everything was cleaned
       
  4115  */
       
  4116 static bool e1000_clean_jumbo_rx_irq(struct e1000_adapter *adapter,
       
  4117 				     struct e1000_rx_ring *rx_ring,
       
  4118 				     int *work_done, int work_to_do)
       
  4119 {
       
  4120 	struct e1000_hw *hw = &adapter->hw;
       
  4121 	struct net_device *netdev = adapter->netdev;
       
  4122 	struct pci_dev *pdev = adapter->pdev;
       
  4123 	struct e1000_rx_desc *rx_desc, *next_rxd;
       
  4124 	struct e1000_buffer *buffer_info, *next_buffer;
       
  4125 	unsigned long irq_flags;
       
  4126 	u32 length;
       
  4127 	unsigned int i;
       
  4128 	int cleaned_count = 0;
       
  4129 	bool cleaned = false;
       
  4130 	unsigned int total_rx_bytes=0, total_rx_packets=0;
       
  4131 
       
  4132 	i = rx_ring->next_to_clean;
       
  4133 	rx_desc = E1000_RX_DESC(*rx_ring, i);
       
  4134 	buffer_info = &rx_ring->buffer_info[i];
       
  4135 
       
  4136 	while (rx_desc->status & E1000_RXD_STAT_DD) {
       
  4137 		struct sk_buff *skb;
       
  4138 		u8 status;
       
  4139 
       
  4140 		if (*work_done >= work_to_do)
       
  4141 			break;
       
  4142 		(*work_done)++;
       
  4143 		rmb(); /* read descriptor and rx_buffer_info after status DD */
       
  4144 
       
  4145 		status = rx_desc->status;
       
  4146 		skb = buffer_info->skb;
       
  4147 		if (!adapter->ecdev) {
       
  4148 			buffer_info->skb = NULL;
       
  4149 		}
       
  4150 
       
  4151 		if (++i == rx_ring->count) i = 0;
       
  4152 		next_rxd = E1000_RX_DESC(*rx_ring, i);
       
  4153 		prefetch(next_rxd);
       
  4154 
       
  4155 		next_buffer = &rx_ring->buffer_info[i];
       
  4156 
       
  4157 		cleaned = true;
       
  4158 		cleaned_count++;
       
  4159 		dma_unmap_page(&pdev->dev, buffer_info->dma,
       
  4160 			       buffer_info->length, DMA_FROM_DEVICE);
       
  4161 		buffer_info->dma = 0;
       
  4162 
       
  4163 		length = le16_to_cpu(rx_desc->length);
       
  4164 
       
  4165 		/* errors is only valid for DD + EOP descriptors */
       
  4166 		if (!adapter->ecdev &&
       
  4167 		    unlikely((status & E1000_RXD_STAT_EOP) &&
       
  4168 		    (rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK))) {
       
  4169 			u8 *mapped;
       
  4170 			u8 last_byte;
       
  4171 
       
  4172 			mapped = page_address(buffer_info->page);
       
  4173 			last_byte = *(mapped + length - 1);
       
  4174 			if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
       
  4175 				       last_byte)) {
       
  4176 				spin_lock_irqsave(&adapter->stats_lock,
       
  4177 						  irq_flags);
       
  4178 				e1000_tbi_adjust_stats(hw, &adapter->stats,
       
  4179 						       length, mapped);
       
  4180 				spin_unlock_irqrestore(&adapter->stats_lock,
       
  4181 						       irq_flags);
       
  4182 				length--;
       
  4183 			} else {
       
  4184 				if (netdev->features & NETIF_F_RXALL)
       
  4185 					goto process_skb;
       
  4186 				/* recycle both page and skb */
       
  4187 				buffer_info->skb = skb;
       
  4188 				/* an error means any chain goes out the window
       
  4189 				 * too
       
  4190 				 */
       
  4191 				if (!adapter->ecdev && rx_ring->rx_skb_top)
       
  4192 					dev_kfree_skb(rx_ring->rx_skb_top);
       
  4193 				rx_ring->rx_skb_top = NULL;
       
  4194 				goto next_desc;
       
  4195 			}
       
  4196 		}
       
  4197 
       
  4198 #define rxtop rx_ring->rx_skb_top
       
  4199 process_skb:
       
  4200 		if (!(status & E1000_RXD_STAT_EOP)) {
       
  4201 			/* this descriptor is only the beginning (or middle) */
       
  4202 			if (!rxtop) {
       
  4203 				/* this is the beginning of a chain */
       
  4204 				rxtop = skb;
       
  4205 				skb_fill_page_desc(rxtop, 0, buffer_info->page,
       
  4206 						   0, length);
       
  4207 			} else {
       
  4208 				/* this is the middle of a chain */
       
  4209 				skb_fill_page_desc(rxtop,
       
  4210 				    skb_shinfo(rxtop)->nr_frags,
       
  4211 				    buffer_info->page, 0, length);
       
  4212 				/* re-use the skb, only consumed the page */
       
  4213 				buffer_info->skb = skb;
       
  4214 			}
       
  4215 			e1000_consume_page(buffer_info, rxtop, length);
       
  4216 			goto next_desc;
       
  4217 		} else {
       
  4218 			if (rxtop) {
       
  4219 				/* end of the chain */
       
  4220 				skb_fill_page_desc(rxtop,
       
  4221 				    skb_shinfo(rxtop)->nr_frags,
       
  4222 				    buffer_info->page, 0, length);
       
  4223 				/* re-use the current skb, we only consumed the
       
  4224 				 * page
       
  4225 				 */
       
  4226 				buffer_info->skb = skb;
       
  4227 				skb = rxtop;
       
  4228 				rxtop = NULL;
       
  4229 				e1000_consume_page(buffer_info, skb, length);
       
  4230 			} else {
       
  4231 				/* no chain, got EOP, this buf is the packet
       
  4232 				 * copybreak to save the put_page/alloc_page
       
  4233 				 */
       
  4234 				if (length <= copybreak &&
       
  4235 				    skb_tailroom(skb) >= length) {
       
  4236 					u8 *vaddr;
       
  4237 					vaddr = kmap_atomic(buffer_info->page);
       
  4238 					memcpy(skb_tail_pointer(skb), vaddr,
       
  4239 					       length);
       
  4240 					kunmap_atomic(vaddr);
       
  4241 					/* re-use the page, so don't erase
       
  4242 					 * buffer_info->page
       
  4243 					 */
       
  4244 					skb_put(skb, length);
       
  4245 				} else {
       
  4246 					skb_fill_page_desc(skb, 0,
       
  4247 							   buffer_info->page, 0,
       
  4248 							   length);
       
  4249 					e1000_consume_page(buffer_info, skb,
       
  4250 							   length);
       
  4251 				}
       
  4252 			}
       
  4253 		}
       
  4254 
       
  4255 		/* Receive Checksum Offload XXX recompute due to CRC strip? */
       
  4256 		e1000_rx_checksum(adapter,
       
  4257 				  (u32)(status) |
       
  4258 				  ((u32)(rx_desc->errors) << 24),
       
  4259 				  le16_to_cpu(rx_desc->csum), skb);
       
  4260 
       
  4261 		total_rx_bytes += (skb->len - 4); /* don't count FCS */
       
  4262 		if (likely(!(netdev->features & NETIF_F_RXFCS)))
       
  4263 			pskb_trim(skb, skb->len - 4);
       
  4264 		total_rx_packets++;
       
  4265 
       
  4266 		/* eth type trans needs skb->data to point to something */
       
  4267 		if (!pskb_may_pull(skb, ETH_HLEN)) {
       
  4268 			e_err(drv, "pskb_may_pull failed.\n");
       
  4269 			if (!adapter->ecdev) {
       
  4270 				dev_kfree_skb(skb);
       
  4271 			}
       
  4272 			goto next_desc;
       
  4273 		}
       
  4274 
       
  4275 		if (adapter->ecdev) {
       
  4276 			ecdev_receive(adapter->ecdev, skb->data, length);
       
  4277 
       
  4278 			// No need to detect link status as
       
  4279 			// long as frames are received: Reset watchdog.
       
  4280 			adapter->ec_watchdog_jiffies = jiffies;
       
  4281 		} else {
       
  4282 			e1000_receive_skb(adapter, status, rx_desc->special, skb);
       
  4283 		}
       
  4284 
       
  4285 next_desc:
       
  4286 		rx_desc->status = 0;
       
  4287 
       
  4288 		/* return some buffers to hardware, one at a time is too slow */
       
  4289 		if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
       
  4290 			adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
       
  4291 			cleaned_count = 0;
       
  4292 		}
       
  4293 
       
  4294 		/* use prefetched values */
       
  4295 		rx_desc = next_rxd;
       
  4296 		buffer_info = next_buffer;
       
  4297 	}
       
  4298 	rx_ring->next_to_clean = i;
       
  4299 
       
  4300 	cleaned_count = E1000_DESC_UNUSED(rx_ring);
       
  4301 	if (cleaned_count)
       
  4302 		adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
       
  4303 
       
  4304 	adapter->total_rx_packets += total_rx_packets;
       
  4305 	adapter->total_rx_bytes += total_rx_bytes;
       
  4306 	netdev->stats.rx_bytes += total_rx_bytes;
       
  4307 	netdev->stats.rx_packets += total_rx_packets;
       
  4308 	return cleaned;
       
  4309 }
       
  4310 
       
  4311 /* this should improve performance for small packets with large amounts
       
  4312  * of reassembly being done in the stack
       
  4313  */
       
  4314 static void e1000_check_copybreak(struct net_device *netdev,
       
  4315 				 struct e1000_buffer *buffer_info,
       
  4316 				 u32 length, struct sk_buff **skb)
       
  4317 {
       
  4318 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  4319 	struct sk_buff *new_skb;
       
  4320 
       
  4321 	if (adapter->ecdev || length > copybreak)
       
  4322 		return;
       
  4323 
       
  4324 	new_skb = netdev_alloc_skb_ip_align(netdev, length);
       
  4325 	if (!new_skb)
       
  4326 		return;
       
  4327 
       
  4328 	skb_copy_to_linear_data_offset(new_skb, -NET_IP_ALIGN,
       
  4329 				       (*skb)->data - NET_IP_ALIGN,
       
  4330 				       length + NET_IP_ALIGN);
       
  4331 	/* save the skb in buffer_info as good */
       
  4332 	buffer_info->skb = *skb;
       
  4333 	*skb = new_skb;
       
  4334 }
       
  4335 
       
  4336 /**
       
  4337  * e1000_clean_rx_irq - Send received data up the network stack; legacy
       
  4338  * @adapter: board private structure
       
  4339  * @rx_ring: ring to clean
       
  4340  * @work_done: amount of napi work completed this call
       
  4341  * @work_to_do: max amount of work allowed for this call to do
       
  4342  */
       
  4343 static bool e1000_clean_rx_irq(struct e1000_adapter *adapter,
       
  4344 			       struct e1000_rx_ring *rx_ring,
       
  4345 			       int *work_done, int work_to_do)
       
  4346 {
       
  4347 	struct e1000_hw *hw = &adapter->hw;
       
  4348 	struct net_device *netdev = adapter->netdev;
       
  4349 	struct pci_dev *pdev = adapter->pdev;
       
  4350 	struct e1000_rx_desc *rx_desc, *next_rxd;
       
  4351 	struct e1000_buffer *buffer_info, *next_buffer;
       
  4352 	unsigned long flags;
       
  4353 	u32 length;
       
  4354 	unsigned int i;
       
  4355 	int cleaned_count = 0;
       
  4356 	bool cleaned = false;
       
  4357 	unsigned int total_rx_bytes=0, total_rx_packets=0;
       
  4358 
       
  4359 	i = rx_ring->next_to_clean;
       
  4360 	rx_desc = E1000_RX_DESC(*rx_ring, i);
       
  4361 	buffer_info = &rx_ring->buffer_info[i];
       
  4362 
       
  4363 	while (rx_desc->status & E1000_RXD_STAT_DD) {
       
  4364 		struct sk_buff *skb;
       
  4365 		u8 status;
       
  4366 
       
  4367 		if (*work_done >= work_to_do)
       
  4368 			break;
       
  4369 		(*work_done)++;
       
  4370 		rmb(); /* read descriptor and rx_buffer_info after status DD */
       
  4371 
       
  4372 		status = rx_desc->status;
       
  4373 		skb = buffer_info->skb;
       
  4374 		if (!adapter->ecdev) {
       
  4375 			buffer_info->skb = NULL;
       
  4376 		}
       
  4377 
       
  4378 		prefetch(skb->data - NET_IP_ALIGN);
       
  4379 
       
  4380 		if (++i == rx_ring->count) i = 0;
       
  4381 		next_rxd = E1000_RX_DESC(*rx_ring, i);
       
  4382 		prefetch(next_rxd);
       
  4383 
       
  4384 		next_buffer = &rx_ring->buffer_info[i];
       
  4385 
       
  4386 		cleaned = true;
       
  4387 		cleaned_count++;
       
  4388 		dma_unmap_single(&pdev->dev, buffer_info->dma,
       
  4389 				 buffer_info->length, DMA_FROM_DEVICE);
       
  4390 		buffer_info->dma = 0;
       
  4391 
       
  4392 		length = le16_to_cpu(rx_desc->length);
       
  4393 		/* !EOP means multiple descriptors were used to store a single
       
  4394 		 * packet, if thats the case we need to toss it.  In fact, we
       
  4395 		 * to toss every packet with the EOP bit clear and the next
       
  4396 		 * frame that _does_ have the EOP bit set, as it is by
       
  4397 		 * definition only a frame fragment
       
  4398 		 */
       
  4399 		if (unlikely(!(status & E1000_RXD_STAT_EOP)))
       
  4400 			adapter->discarding = true;
       
  4401 
       
  4402 		if (adapter->discarding) {
       
  4403 			/* All receives must fit into a single buffer */
       
  4404 			e_dbg("Receive packet consumed multiple buffers\n");
       
  4405 			/* recycle */
       
  4406 			buffer_info->skb = skb;
       
  4407 			if (status & E1000_RXD_STAT_EOP)
       
  4408 				adapter->discarding = false;
       
  4409 			goto next_desc;
       
  4410 		}
       
  4411 
       
  4412 		if (!adapter->ecdev &&
       
  4413 		    unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
       
  4414 			u8 last_byte = *(skb->data + length - 1);
       
  4415 			if (TBI_ACCEPT(hw, status, rx_desc->errors, length,
       
  4416 				       last_byte)) {
       
  4417 				spin_lock_irqsave(&adapter->stats_lock, flags);
       
  4418 				e1000_tbi_adjust_stats(hw, &adapter->stats,
       
  4419 						       length, skb->data);
       
  4420 				spin_unlock_irqrestore(&adapter->stats_lock,
       
  4421 						       flags);
       
  4422 				length--;
       
  4423 			} else {
       
  4424 				if (netdev->features & NETIF_F_RXALL)
       
  4425 					goto process_skb;
       
  4426 				/* recycle */
       
  4427 				buffer_info->skb = skb;
       
  4428 				goto next_desc;
       
  4429 			}
       
  4430 		}
       
  4431 
       
  4432 process_skb:
       
  4433 		total_rx_bytes += (length - 4); /* don't count FCS */
       
  4434 		total_rx_packets++;
       
  4435 
       
  4436 		if (likely(!(netdev->features & NETIF_F_RXFCS)))
       
  4437 			/* adjust length to remove Ethernet CRC, this must be
       
  4438 			 * done after the TBI_ACCEPT workaround above
       
  4439 			 */
       
  4440 			length -= 4;
       
  4441 
       
  4442 		e1000_check_copybreak(netdev, buffer_info, length, &skb);
       
  4443 
       
  4444 		skb_put(skb, length);
       
  4445 
       
  4446 		/* Receive Checksum Offload */
       
  4447 		e1000_rx_checksum(adapter,
       
  4448 				  (u32)(status) |
       
  4449 				  ((u32)(rx_desc->errors) << 24),
       
  4450 				  le16_to_cpu(rx_desc->csum), skb);
       
  4451 
       
  4452 		if (adapter->ecdev) {
       
  4453 			ecdev_receive(adapter->ecdev, skb->data, length);
       
  4454 
       
  4455 			// No need to detect link status as
       
  4456 			// long as frames are received: Reset watchdog.
       
  4457 			adapter->ec_watchdog_jiffies = jiffies;
       
  4458 		} else {
       
  4459 			e1000_receive_skb(adapter, status, rx_desc->special, skb);
       
  4460 		}
       
  4461 
       
  4462 next_desc:
       
  4463 		rx_desc->status = 0;
       
  4464 
       
  4465 		/* return some buffers to hardware, one at a time is too slow */
       
  4466 		if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
       
  4467 			adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
       
  4468 			cleaned_count = 0;
       
  4469 		}
       
  4470 
       
  4471 		/* use prefetched values */
       
  4472 		rx_desc = next_rxd;
       
  4473 		buffer_info = next_buffer;
       
  4474 	}
       
  4475 	rx_ring->next_to_clean = i;
       
  4476 
       
  4477 	cleaned_count = E1000_DESC_UNUSED(rx_ring);
       
  4478 	if (cleaned_count)
       
  4479 		adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
       
  4480 
       
  4481 	adapter->total_rx_packets += total_rx_packets;
       
  4482 	adapter->total_rx_bytes += total_rx_bytes;
       
  4483 	netdev->stats.rx_bytes += total_rx_bytes;
       
  4484 	netdev->stats.rx_packets += total_rx_packets;
       
  4485 	return cleaned;
       
  4486 }
       
  4487 
       
  4488 /**
       
  4489  * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
       
  4490  * @adapter: address of board private structure
       
  4491  * @rx_ring: pointer to receive ring structure
       
  4492  * @cleaned_count: number of buffers to allocate this pass
       
  4493  **/
       
  4494 static void
       
  4495 e1000_alloc_jumbo_rx_buffers(struct e1000_adapter *adapter,
       
  4496 			     struct e1000_rx_ring *rx_ring, int cleaned_count)
       
  4497 {
       
  4498 	struct net_device *netdev = adapter->netdev;
       
  4499 	struct pci_dev *pdev = adapter->pdev;
       
  4500 	struct e1000_rx_desc *rx_desc;
       
  4501 	struct e1000_buffer *buffer_info;
       
  4502 	struct sk_buff *skb;
       
  4503 	unsigned int i;
       
  4504 	unsigned int bufsz = 256 - 16 /*for skb_reserve */ ;
       
  4505 
       
  4506 	i = rx_ring->next_to_use;
       
  4507 	buffer_info = &rx_ring->buffer_info[i];
       
  4508 
       
  4509 	while (cleaned_count--) {
       
  4510 		skb = buffer_info->skb;
       
  4511 		if (skb) {
       
  4512 			skb_trim(skb, 0);
       
  4513 			goto check_page;
       
  4514 		}
       
  4515 
       
  4516 		skb = netdev_alloc_skb_ip_align(netdev, bufsz);
       
  4517 		if (unlikely(!skb)) {
       
  4518 			/* Better luck next round */
       
  4519 			adapter->alloc_rx_buff_failed++;
       
  4520 			break;
       
  4521 		}
       
  4522 
       
  4523 		buffer_info->skb = skb;
       
  4524 		buffer_info->length = adapter->rx_buffer_len;
       
  4525 check_page:
       
  4526 		/* allocate a new page if necessary */
       
  4527 		if (!buffer_info->page) {
       
  4528 			buffer_info->page = alloc_page(GFP_ATOMIC);
       
  4529 			if (unlikely(!buffer_info->page)) {
       
  4530 				adapter->alloc_rx_buff_failed++;
       
  4531 				break;
       
  4532 			}
       
  4533 		}
       
  4534 
       
  4535 		if (!buffer_info->dma) {
       
  4536 			buffer_info->dma = dma_map_page(&pdev->dev,
       
  4537 							buffer_info->page, 0,
       
  4538 							buffer_info->length,
       
  4539 							DMA_FROM_DEVICE);
       
  4540 			if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
       
  4541 				put_page(buffer_info->page);
       
  4542 				dev_kfree_skb(skb);
       
  4543 				buffer_info->page = NULL;
       
  4544 				buffer_info->skb = NULL;
       
  4545 				buffer_info->dma = 0;
       
  4546 				adapter->alloc_rx_buff_failed++;
       
  4547 				break; /* while !buffer_info->skb */
       
  4548 			}
       
  4549 		}
       
  4550 
       
  4551 		rx_desc = E1000_RX_DESC(*rx_ring, i);
       
  4552 		rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
       
  4553 
       
  4554 		if (unlikely(++i == rx_ring->count))
       
  4555 			i = 0;
       
  4556 		buffer_info = &rx_ring->buffer_info[i];
       
  4557 	}
       
  4558 
       
  4559 	if (likely(rx_ring->next_to_use != i)) {
       
  4560 		rx_ring->next_to_use = i;
       
  4561 		if (unlikely(i-- == 0))
       
  4562 			i = (rx_ring->count - 1);
       
  4563 
       
  4564 		/* Force memory writes to complete before letting h/w
       
  4565 		 * know there are new descriptors to fetch.  (Only
       
  4566 		 * applicable for weak-ordered memory model archs,
       
  4567 		 * such as IA-64).
       
  4568 		 */
       
  4569 		wmb();
       
  4570 		writel(i, adapter->hw.hw_addr + rx_ring->rdt);
       
  4571 	}
       
  4572 }
       
  4573 
       
  4574 /**
       
  4575  * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
       
  4576  * @adapter: address of board private structure
       
  4577  **/
       
  4578 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
       
  4579 				   struct e1000_rx_ring *rx_ring,
       
  4580 				   int cleaned_count)
       
  4581 {
       
  4582 	struct e1000_hw *hw = &adapter->hw;
       
  4583 	struct net_device *netdev = adapter->netdev;
       
  4584 	struct pci_dev *pdev = adapter->pdev;
       
  4585 	struct e1000_rx_desc *rx_desc;
       
  4586 	struct e1000_buffer *buffer_info;
       
  4587 	struct sk_buff *skb;
       
  4588 	unsigned int i;
       
  4589 	unsigned int bufsz = adapter->rx_buffer_len;
       
  4590 
       
  4591 	i = rx_ring->next_to_use;
       
  4592 	buffer_info = &rx_ring->buffer_info[i];
       
  4593 
       
  4594 	while (cleaned_count--) {
       
  4595 		skb = buffer_info->skb;
       
  4596 		if (skb) {
       
  4597 			skb_trim(skb, 0);
       
  4598 			goto map_skb;
       
  4599 		}
       
  4600 
       
  4601 		skb = netdev_alloc_skb_ip_align(netdev, bufsz);
       
  4602 		if (unlikely(!skb)) {
       
  4603 			/* Better luck next round */
       
  4604 			adapter->alloc_rx_buff_failed++;
       
  4605 			break;
       
  4606 		}
       
  4607 
       
  4608 		/* Fix for errata 23, can't cross 64kB boundary */
       
  4609 		if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
       
  4610 			struct sk_buff *oldskb = skb;
       
  4611 			e_err(rx_err, "skb align check failed: %u bytes at "
       
  4612 			      "%p\n", bufsz, skb->data);
       
  4613 			/* Try again, without freeing the previous */
       
  4614 			skb = netdev_alloc_skb_ip_align(netdev, bufsz);
       
  4615 			/* Failed allocation, critical failure */
       
  4616 			if (!skb) {
       
  4617 				dev_kfree_skb(oldskb);
       
  4618 				adapter->alloc_rx_buff_failed++;
       
  4619 				break;
       
  4620 			}
       
  4621 
       
  4622 			if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
       
  4623 				/* give up */
       
  4624 				dev_kfree_skb(skb);
       
  4625 				dev_kfree_skb(oldskb);
       
  4626 				adapter->alloc_rx_buff_failed++;
       
  4627 				break; /* while !buffer_info->skb */
       
  4628 			}
       
  4629 
       
  4630 			/* Use new allocation */
       
  4631 			dev_kfree_skb(oldskb);
       
  4632 		}
       
  4633 		buffer_info->skb = skb;
       
  4634 		buffer_info->length = adapter->rx_buffer_len;
       
  4635 map_skb:
       
  4636 		buffer_info->dma = dma_map_single(&pdev->dev,
       
  4637 						  skb->data,
       
  4638 						  buffer_info->length,
       
  4639 						  DMA_FROM_DEVICE);
       
  4640 		if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
       
  4641 			dev_kfree_skb(skb);
       
  4642 			buffer_info->skb = NULL;
       
  4643 			buffer_info->dma = 0;
       
  4644 			adapter->alloc_rx_buff_failed++;
       
  4645 			break; /* while !buffer_info->skb */
       
  4646 		}
       
  4647 
       
  4648 		/* XXX if it was allocated cleanly it will never map to a
       
  4649 		 * boundary crossing
       
  4650 		 */
       
  4651 
       
  4652 		/* Fix for errata 23, can't cross 64kB boundary */
       
  4653 		if (!e1000_check_64k_bound(adapter,
       
  4654 					(void *)(unsigned long)buffer_info->dma,
       
  4655 					adapter->rx_buffer_len)) {
       
  4656 			e_err(rx_err, "dma align check failed: %u bytes at "
       
  4657 			      "%p\n", adapter->rx_buffer_len,
       
  4658 			      (void *)(unsigned long)buffer_info->dma);
       
  4659 			dev_kfree_skb(skb);
       
  4660 			buffer_info->skb = NULL;
       
  4661 
       
  4662 			dma_unmap_single(&pdev->dev, buffer_info->dma,
       
  4663 					 adapter->rx_buffer_len,
       
  4664 					 DMA_FROM_DEVICE);
       
  4665 			buffer_info->dma = 0;
       
  4666 
       
  4667 			adapter->alloc_rx_buff_failed++;
       
  4668 			break; /* while !buffer_info->skb */
       
  4669 		}
       
  4670 		rx_desc = E1000_RX_DESC(*rx_ring, i);
       
  4671 		rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
       
  4672 
       
  4673 		if (unlikely(++i == rx_ring->count))
       
  4674 			i = 0;
       
  4675 		buffer_info = &rx_ring->buffer_info[i];
       
  4676 	}
       
  4677 
       
  4678 	if (likely(rx_ring->next_to_use != i)) {
       
  4679 		rx_ring->next_to_use = i;
       
  4680 		if (unlikely(i-- == 0))
       
  4681 			i = (rx_ring->count - 1);
       
  4682 
       
  4683 		/* Force memory writes to complete before letting h/w
       
  4684 		 * know there are new descriptors to fetch.  (Only
       
  4685 		 * applicable for weak-ordered memory model archs,
       
  4686 		 * such as IA-64).
       
  4687 		 */
       
  4688 		wmb();
       
  4689 		writel(i, hw->hw_addr + rx_ring->rdt);
       
  4690 	}
       
  4691 }
       
  4692 
       
  4693 /**
       
  4694  * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
       
  4695  * @adapter:
       
  4696  **/
       
  4697 static void e1000_smartspeed(struct e1000_adapter *adapter)
       
  4698 {
       
  4699 	struct e1000_hw *hw = &adapter->hw;
       
  4700 	u16 phy_status;
       
  4701 	u16 phy_ctrl;
       
  4702 
       
  4703 	if ((hw->phy_type != e1000_phy_igp) || !hw->autoneg ||
       
  4704 	   !(hw->autoneg_advertised & ADVERTISE_1000_FULL))
       
  4705 		return;
       
  4706 
       
  4707 	if (adapter->smartspeed == 0) {
       
  4708 		/* If Master/Slave config fault is asserted twice,
       
  4709 		 * we assume back-to-back
       
  4710 		 */
       
  4711 		e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
       
  4712 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
       
  4713 		e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_status);
       
  4714 		if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
       
  4715 		e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
       
  4716 		if (phy_ctrl & CR_1000T_MS_ENABLE) {
       
  4717 			phy_ctrl &= ~CR_1000T_MS_ENABLE;
       
  4718 			e1000_write_phy_reg(hw, PHY_1000T_CTRL,
       
  4719 					    phy_ctrl);
       
  4720 			adapter->smartspeed++;
       
  4721 			if (!e1000_phy_setup_autoneg(hw) &&
       
  4722 			   !e1000_read_phy_reg(hw, PHY_CTRL,
       
  4723 					       &phy_ctrl)) {
       
  4724 				phy_ctrl |= (MII_CR_AUTO_NEG_EN |
       
  4725 					     MII_CR_RESTART_AUTO_NEG);
       
  4726 				e1000_write_phy_reg(hw, PHY_CTRL,
       
  4727 						    phy_ctrl);
       
  4728 			}
       
  4729 		}
       
  4730 		return;
       
  4731 	} else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
       
  4732 		/* If still no link, perhaps using 2/3 pair cable */
       
  4733 		e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_ctrl);
       
  4734 		phy_ctrl |= CR_1000T_MS_ENABLE;
       
  4735 		e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_ctrl);
       
  4736 		if (!e1000_phy_setup_autoneg(hw) &&
       
  4737 		   !e1000_read_phy_reg(hw, PHY_CTRL, &phy_ctrl)) {
       
  4738 			phy_ctrl |= (MII_CR_AUTO_NEG_EN |
       
  4739 				     MII_CR_RESTART_AUTO_NEG);
       
  4740 			e1000_write_phy_reg(hw, PHY_CTRL, phy_ctrl);
       
  4741 		}
       
  4742 	}
       
  4743 	/* Restart process after E1000_SMARTSPEED_MAX iterations */
       
  4744 	if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
       
  4745 		adapter->smartspeed = 0;
       
  4746 }
       
  4747 
       
  4748 /**
       
  4749  * e1000_ioctl -
       
  4750  * @netdev:
       
  4751  * @ifreq:
       
  4752  * @cmd:
       
  4753  **/
       
  4754 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
       
  4755 {
       
  4756 	switch (cmd) {
       
  4757 	case SIOCGMIIPHY:
       
  4758 	case SIOCGMIIREG:
       
  4759 	case SIOCSMIIREG:
       
  4760 		return e1000_mii_ioctl(netdev, ifr, cmd);
       
  4761 	default:
       
  4762 		return -EOPNOTSUPP;
       
  4763 	}
       
  4764 }
       
  4765 
       
  4766 /**
       
  4767  * e1000_mii_ioctl -
       
  4768  * @netdev:
       
  4769  * @ifreq:
       
  4770  * @cmd:
       
  4771  **/
       
  4772 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
       
  4773 			   int cmd)
       
  4774 {
       
  4775 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  4776 	struct e1000_hw *hw = &adapter->hw;
       
  4777 	struct mii_ioctl_data *data = if_mii(ifr);
       
  4778 	int retval;
       
  4779 	u16 mii_reg;
       
  4780 	unsigned long flags;
       
  4781 
       
  4782 	if (hw->media_type != e1000_media_type_copper)
       
  4783 		return -EOPNOTSUPP;
       
  4784 
       
  4785 	switch (cmd) {
       
  4786 	case SIOCGMIIPHY:
       
  4787 		data->phy_id = hw->phy_addr;
       
  4788 		break;
       
  4789 	case SIOCGMIIREG:
       
  4790 		if (adapter->ecdev) {
       
  4791 			return -EPERM;
       
  4792 		}
       
  4793 		spin_lock_irqsave(&adapter->stats_lock, flags);
       
  4794 		if (e1000_read_phy_reg(hw, data->reg_num & 0x1F,
       
  4795 				   &data->val_out)) {
       
  4796 			spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  4797 			return -EIO;
       
  4798 		}
       
  4799 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  4800 		break;
       
  4801 	case SIOCSMIIREG:
       
  4802 		if (adapter->ecdev) {
       
  4803 			return -EPERM;
       
  4804 		}
       
  4805 		if (data->reg_num & ~(0x1F))
       
  4806 			return -EFAULT;
       
  4807 		mii_reg = data->val_in;
       
  4808 		spin_lock_irqsave(&adapter->stats_lock, flags);
       
  4809 		if (e1000_write_phy_reg(hw, data->reg_num,
       
  4810 					mii_reg)) {
       
  4811 			spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  4812 			return -EIO;
       
  4813 		}
       
  4814 		spin_unlock_irqrestore(&adapter->stats_lock, flags);
       
  4815 		if (hw->media_type == e1000_media_type_copper) {
       
  4816 			switch (data->reg_num) {
       
  4817 			case PHY_CTRL:
       
  4818 				if (mii_reg & MII_CR_POWER_DOWN)
       
  4819 					break;
       
  4820 				if (mii_reg & MII_CR_AUTO_NEG_EN) {
       
  4821 					hw->autoneg = 1;
       
  4822 					hw->autoneg_advertised = 0x2F;
       
  4823 				} else {
       
  4824 					u32 speed;
       
  4825 					if (mii_reg & 0x40)
       
  4826 						speed = SPEED_1000;
       
  4827 					else if (mii_reg & 0x2000)
       
  4828 						speed = SPEED_100;
       
  4829 					else
       
  4830 						speed = SPEED_10;
       
  4831 					retval = e1000_set_spd_dplx(
       
  4832 						adapter, speed,
       
  4833 						((mii_reg & 0x100)
       
  4834 						 ? DUPLEX_FULL :
       
  4835 						 DUPLEX_HALF));
       
  4836 					if (retval)
       
  4837 						return retval;
       
  4838 				}
       
  4839 				if (netif_running(adapter->netdev))
       
  4840 					e1000_reinit_locked(adapter);
       
  4841 				else
       
  4842 					e1000_reset(adapter);
       
  4843 				break;
       
  4844 			case M88E1000_PHY_SPEC_CTRL:
       
  4845 			case M88E1000_EXT_PHY_SPEC_CTRL:
       
  4846 				if (e1000_phy_reset(hw))
       
  4847 					return -EIO;
       
  4848 				break;
       
  4849 			}
       
  4850 		} else {
       
  4851 			switch (data->reg_num) {
       
  4852 			case PHY_CTRL:
       
  4853 				if (mii_reg & MII_CR_POWER_DOWN)
       
  4854 					break;
       
  4855 				if (netif_running(adapter->netdev))
       
  4856 					e1000_reinit_locked(adapter);
       
  4857 				else
       
  4858 					e1000_reset(adapter);
       
  4859 				break;
       
  4860 			}
       
  4861 		}
       
  4862 		break;
       
  4863 	default:
       
  4864 		return -EOPNOTSUPP;
       
  4865 	}
       
  4866 	return E1000_SUCCESS;
       
  4867 }
       
  4868 
       
  4869 void e1000_pci_set_mwi(struct e1000_hw *hw)
       
  4870 {
       
  4871 	struct e1000_adapter *adapter = hw->back;
       
  4872 	int ret_val = pci_set_mwi(adapter->pdev);
       
  4873 
       
  4874 	if (ret_val)
       
  4875 		e_err(probe, "Error in setting MWI\n");
       
  4876 }
       
  4877 
       
  4878 void e1000_pci_clear_mwi(struct e1000_hw *hw)
       
  4879 {
       
  4880 	struct e1000_adapter *adapter = hw->back;
       
  4881 
       
  4882 	pci_clear_mwi(adapter->pdev);
       
  4883 }
       
  4884 
       
  4885 int e1000_pcix_get_mmrbc(struct e1000_hw *hw)
       
  4886 {
       
  4887 	struct e1000_adapter *adapter = hw->back;
       
  4888 	return pcix_get_mmrbc(adapter->pdev);
       
  4889 }
       
  4890 
       
  4891 void e1000_pcix_set_mmrbc(struct e1000_hw *hw, int mmrbc)
       
  4892 {
       
  4893 	struct e1000_adapter *adapter = hw->back;
       
  4894 	pcix_set_mmrbc(adapter->pdev, mmrbc);
       
  4895 }
       
  4896 
       
  4897 void e1000_io_write(struct e1000_hw *hw, unsigned long port, u32 value)
       
  4898 {
       
  4899 	outl(value, port);
       
  4900 }
       
  4901 
       
  4902 static bool e1000_vlan_used(struct e1000_adapter *adapter)
       
  4903 {
       
  4904 	u16 vid;
       
  4905 
       
  4906 	for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
       
  4907 		return true;
       
  4908 	return false;
       
  4909 }
       
  4910 
       
  4911 static void __e1000_vlan_mode(struct e1000_adapter *adapter,
       
  4912 			      netdev_features_t features)
       
  4913 {
       
  4914 	struct e1000_hw *hw = &adapter->hw;
       
  4915 	u32 ctrl;
       
  4916 
       
  4917 	ctrl = er32(CTRL);
       
  4918 	if (features & NETIF_F_HW_VLAN_CTAG_RX) {
       
  4919 		/* enable VLAN tag insert/strip */
       
  4920 		ctrl |= E1000_CTRL_VME;
       
  4921 	} else {
       
  4922 		/* disable VLAN tag insert/strip */
       
  4923 		ctrl &= ~E1000_CTRL_VME;
       
  4924 	}
       
  4925 	ew32(CTRL, ctrl);
       
  4926 }
       
  4927 static void e1000_vlan_filter_on_off(struct e1000_adapter *adapter,
       
  4928 				     bool filter_on)
       
  4929 {
       
  4930 	struct e1000_hw *hw = &adapter->hw;
       
  4931 	u32 rctl;
       
  4932 
       
  4933 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  4934 		e1000_irq_disable(adapter);
       
  4935 
       
  4936 	__e1000_vlan_mode(adapter, adapter->netdev->features);
       
  4937 	if (filter_on) {
       
  4938 		/* enable VLAN receive filtering */
       
  4939 		rctl = er32(RCTL);
       
  4940 		rctl &= ~E1000_RCTL_CFIEN;
       
  4941 		if (!(adapter->netdev->flags & IFF_PROMISC))
       
  4942 			rctl |= E1000_RCTL_VFE;
       
  4943 		ew32(RCTL, rctl);
       
  4944 		e1000_update_mng_vlan(adapter);
       
  4945 	} else {
       
  4946 		/* disable VLAN receive filtering */
       
  4947 		rctl = er32(RCTL);
       
  4948 		rctl &= ~E1000_RCTL_VFE;
       
  4949 		ew32(RCTL, rctl);
       
  4950 	}
       
  4951 
       
  4952 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  4953 		e1000_irq_enable(adapter);
       
  4954 }
       
  4955 
       
  4956 static void e1000_vlan_mode(struct net_device *netdev,
       
  4957 			    netdev_features_t features)
       
  4958 {
       
  4959 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  4960 
       
  4961 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  4962 		e1000_irq_disable(adapter);
       
  4963 
       
  4964 	__e1000_vlan_mode(adapter, features);
       
  4965 
       
  4966 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  4967 		e1000_irq_enable(adapter);
       
  4968 }
       
  4969 
       
  4970 static int e1000_vlan_rx_add_vid(struct net_device *netdev,
       
  4971 				 __be16 proto, u16 vid)
       
  4972 {
       
  4973 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  4974 	struct e1000_hw *hw = &adapter->hw;
       
  4975 	u32 vfta, index;
       
  4976 
       
  4977 	if ((hw->mng_cookie.status &
       
  4978 	     E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
       
  4979 	    (vid == adapter->mng_vlan_id))
       
  4980 		return 0;
       
  4981 
       
  4982 	if (!e1000_vlan_used(adapter))
       
  4983 		e1000_vlan_filter_on_off(adapter, true);
       
  4984 
       
  4985 	/* add VID to filter table */
       
  4986 	index = (vid >> 5) & 0x7F;
       
  4987 	vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
       
  4988 	vfta |= (1 << (vid & 0x1F));
       
  4989 	e1000_write_vfta(hw, index, vfta);
       
  4990 
       
  4991 	set_bit(vid, adapter->active_vlans);
       
  4992 
       
  4993 	return 0;
       
  4994 }
       
  4995 
       
  4996 static int e1000_vlan_rx_kill_vid(struct net_device *netdev,
       
  4997 				  __be16 proto, u16 vid)
       
  4998 {
       
  4999 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5000 	struct e1000_hw *hw = &adapter->hw;
       
  5001 	u32 vfta, index;
       
  5002 
       
  5003 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  5004 		e1000_irq_disable(adapter);
       
  5005 	if (!test_bit(__E1000_DOWN, &adapter->flags))
       
  5006 		e1000_irq_enable(adapter);
       
  5007 
       
  5008 	/* remove VID from filter table */
       
  5009 	index = (vid >> 5) & 0x7F;
       
  5010 	vfta = E1000_READ_REG_ARRAY(hw, VFTA, index);
       
  5011 	vfta &= ~(1 << (vid & 0x1F));
       
  5012 	e1000_write_vfta(hw, index, vfta);
       
  5013 
       
  5014 	clear_bit(vid, adapter->active_vlans);
       
  5015 
       
  5016 	if (!e1000_vlan_used(adapter))
       
  5017 		e1000_vlan_filter_on_off(adapter, false);
       
  5018 
       
  5019 	return 0;
       
  5020 }
       
  5021 
       
  5022 static void e1000_restore_vlan(struct e1000_adapter *adapter)
       
  5023 {
       
  5024 	u16 vid;
       
  5025 
       
  5026 	if (!e1000_vlan_used(adapter))
       
  5027 		return;
       
  5028 
       
  5029 	e1000_vlan_filter_on_off(adapter, true);
       
  5030 	for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
       
  5031 		e1000_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
       
  5032 }
       
  5033 
       
  5034 int e1000_set_spd_dplx(struct e1000_adapter *adapter, u32 spd, u8 dplx)
       
  5035 {
       
  5036 	struct e1000_hw *hw = &adapter->hw;
       
  5037 
       
  5038 	hw->autoneg = 0;
       
  5039 
       
  5040 	/* Make sure dplx is at most 1 bit and lsb of speed is not set
       
  5041 	 * for the switch() below to work
       
  5042 	 */
       
  5043 	if ((spd & 1) || (dplx & ~1))
       
  5044 		goto err_inval;
       
  5045 
       
  5046 	/* Fiber NICs only allow 1000 gbps Full duplex */
       
  5047 	if ((hw->media_type == e1000_media_type_fiber) &&
       
  5048 	    spd != SPEED_1000 &&
       
  5049 	    dplx != DUPLEX_FULL)
       
  5050 		goto err_inval;
       
  5051 
       
  5052 	switch (spd + dplx) {
       
  5053 	case SPEED_10 + DUPLEX_HALF:
       
  5054 		hw->forced_speed_duplex = e1000_10_half;
       
  5055 		break;
       
  5056 	case SPEED_10 + DUPLEX_FULL:
       
  5057 		hw->forced_speed_duplex = e1000_10_full;
       
  5058 		break;
       
  5059 	case SPEED_100 + DUPLEX_HALF:
       
  5060 		hw->forced_speed_duplex = e1000_100_half;
       
  5061 		break;
       
  5062 	case SPEED_100 + DUPLEX_FULL:
       
  5063 		hw->forced_speed_duplex = e1000_100_full;
       
  5064 		break;
       
  5065 	case SPEED_1000 + DUPLEX_FULL:
       
  5066 		hw->autoneg = 1;
       
  5067 		hw->autoneg_advertised = ADVERTISE_1000_FULL;
       
  5068 		break;
       
  5069 	case SPEED_1000 + DUPLEX_HALF: /* not supported */
       
  5070 	default:
       
  5071 		goto err_inval;
       
  5072 	}
       
  5073 
       
  5074 	/* clear MDI, MDI(-X) override is only allowed when autoneg enabled */
       
  5075 	hw->mdix = AUTO_ALL_MODES;
       
  5076 
       
  5077 	return 0;
       
  5078 
       
  5079 err_inval:
       
  5080 	e_err(probe, "Unsupported Speed/Duplex configuration\n");
       
  5081 	return -EINVAL;
       
  5082 }
       
  5083 
       
  5084 static int __e1000_shutdown(struct pci_dev *pdev, bool *enable_wake)
       
  5085 {
       
  5086 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5087 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5088 	struct e1000_hw *hw = &adapter->hw;
       
  5089 	u32 ctrl, ctrl_ext, rctl, status;
       
  5090 	u32 wufc = adapter->wol;
       
  5091 #ifdef CONFIG_PM
       
  5092 	int retval = 0;
       
  5093 #endif
       
  5094 
       
  5095 	if (adapter->ecdev) {
       
  5096 		return -EBUSY;
       
  5097 	}
       
  5098 
       
  5099 	netif_device_detach(netdev);
       
  5100 
       
  5101 	if (netif_running(netdev)) {
       
  5102 		int count = E1000_CHECK_RESET_COUNT;
       
  5103 
       
  5104 		while (test_bit(__E1000_RESETTING, &adapter->flags) && count--)
       
  5105 			usleep_range(10000, 20000);
       
  5106 
       
  5107 		WARN_ON(test_bit(__E1000_RESETTING, &adapter->flags));
       
  5108 		e1000_down(adapter);
       
  5109 	}
       
  5110 
       
  5111 #ifdef CONFIG_PM
       
  5112 	retval = pci_save_state(pdev);
       
  5113 	if (retval)
       
  5114 		return retval;
       
  5115 #endif
       
  5116 
       
  5117 	status = er32(STATUS);
       
  5118 	if (status & E1000_STATUS_LU)
       
  5119 		wufc &= ~E1000_WUFC_LNKC;
       
  5120 
       
  5121 	if (wufc) {
       
  5122 		e1000_setup_rctl(adapter);
       
  5123 		e1000_set_rx_mode(netdev);
       
  5124 
       
  5125 		rctl = er32(RCTL);
       
  5126 
       
  5127 		/* turn on all-multi mode if wake on multicast is enabled */
       
  5128 		if (wufc & E1000_WUFC_MC)
       
  5129 			rctl |= E1000_RCTL_MPE;
       
  5130 
       
  5131 		/* enable receives in the hardware */
       
  5132 		ew32(RCTL, rctl | E1000_RCTL_EN);
       
  5133 
       
  5134 		if (hw->mac_type >= e1000_82540) {
       
  5135 			ctrl = er32(CTRL);
       
  5136 			/* advertise wake from D3Cold */
       
  5137 			#define E1000_CTRL_ADVD3WUC 0x00100000
       
  5138 			/* phy power management enable */
       
  5139 			#define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
       
  5140 			ctrl |= E1000_CTRL_ADVD3WUC |
       
  5141 				E1000_CTRL_EN_PHY_PWR_MGMT;
       
  5142 			ew32(CTRL, ctrl);
       
  5143 		}
       
  5144 
       
  5145 		if (hw->media_type == e1000_media_type_fiber ||
       
  5146 		    hw->media_type == e1000_media_type_internal_serdes) {
       
  5147 			/* keep the laser running in D3 */
       
  5148 			ctrl_ext = er32(CTRL_EXT);
       
  5149 			ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
       
  5150 			ew32(CTRL_EXT, ctrl_ext);
       
  5151 		}
       
  5152 
       
  5153 		ew32(WUC, E1000_WUC_PME_EN);
       
  5154 		ew32(WUFC, wufc);
       
  5155 	} else {
       
  5156 		ew32(WUC, 0);
       
  5157 		ew32(WUFC, 0);
       
  5158 	}
       
  5159 
       
  5160 	e1000_release_manageability(adapter);
       
  5161 
       
  5162 	*enable_wake = !!wufc;
       
  5163 
       
  5164 	/* make sure adapter isn't asleep if manageability is enabled */
       
  5165 	if (adapter->en_mng_pt)
       
  5166 		*enable_wake = true;
       
  5167 
       
  5168 	if (netif_running(netdev))
       
  5169 		e1000_free_irq(adapter);
       
  5170 
       
  5171 	pci_disable_device(pdev);
       
  5172 
       
  5173 	return 0;
       
  5174 }
       
  5175 
       
  5176 #ifdef CONFIG_PM
       
  5177 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state)
       
  5178 {
       
  5179 	int retval;
       
  5180 	bool wake;
       
  5181 
       
  5182 	retval = __e1000_shutdown(pdev, &wake);
       
  5183 	if (retval)
       
  5184 		return retval;
       
  5185 
       
  5186 	if (wake) {
       
  5187 		pci_prepare_to_sleep(pdev);
       
  5188 	} else {
       
  5189 		pci_wake_from_d3(pdev, false);
       
  5190 		pci_set_power_state(pdev, PCI_D3hot);
       
  5191 	}
       
  5192 
       
  5193 	return 0;
       
  5194 }
       
  5195 
       
  5196 static int e1000_resume(struct pci_dev *pdev)
       
  5197 {
       
  5198 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5199 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5200 	struct e1000_hw *hw = &adapter->hw;
       
  5201 	u32 err;
       
  5202 
       
  5203 	if (adapter->ecdev) {
       
  5204 		return -EBUSY;
       
  5205 	}
       
  5206 
       
  5207 	pci_set_power_state(pdev, PCI_D0);
       
  5208 	pci_restore_state(pdev);
       
  5209 	pci_save_state(pdev);
       
  5210 
       
  5211 	if (adapter->need_ioport)
       
  5212 		err = pci_enable_device(pdev);
       
  5213 	else
       
  5214 		err = pci_enable_device_mem(pdev);
       
  5215 	if (err) {
       
  5216 		pr_err("Cannot enable PCI device from suspend\n");
       
  5217 		return err;
       
  5218 	}
       
  5219 	pci_set_master(pdev);
       
  5220 
       
  5221 	pci_enable_wake(pdev, PCI_D3hot, 0);
       
  5222 	pci_enable_wake(pdev, PCI_D3cold, 0);
       
  5223 
       
  5224 	if (netif_running(netdev)) {
       
  5225 		err = e1000_request_irq(adapter);
       
  5226 		if (err)
       
  5227 			return err;
       
  5228 	}
       
  5229 
       
  5230 	e1000_power_up_phy(adapter);
       
  5231 	e1000_reset(adapter);
       
  5232 	ew32(WUS, ~0);
       
  5233 
       
  5234 	e1000_init_manageability(adapter);
       
  5235 
       
  5236 	if (netif_running(netdev))
       
  5237 		e1000_up(adapter);
       
  5238 
       
  5239 	if (!adapter->ecdev) {
       
  5240 		netif_device_attach(netdev);
       
  5241 	}
       
  5242 
       
  5243 	return 0;
       
  5244 }
       
  5245 #endif
       
  5246 
       
  5247 static void e1000_shutdown(struct pci_dev *pdev)
       
  5248 {
       
  5249 	bool wake;
       
  5250 
       
  5251 	__e1000_shutdown(pdev, &wake);
       
  5252 
       
  5253 	if (system_state == SYSTEM_POWER_OFF) {
       
  5254 		pci_wake_from_d3(pdev, wake);
       
  5255 		pci_set_power_state(pdev, PCI_D3hot);
       
  5256 	}
       
  5257 }
       
  5258 
       
  5259 #ifdef CONFIG_NET_POLL_CONTROLLER
       
  5260 /* Polling 'interrupt' - used by things like netconsole to send skbs
       
  5261  * without having to re-enable interrupts. It's not called while
       
  5262  * the interrupt routine is executing.
       
  5263  */
       
  5264 static void e1000_netpoll(struct net_device *netdev)
       
  5265 {
       
  5266 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5267 
       
  5268 	disable_irq(adapter->pdev->irq);
       
  5269 	e1000_intr(adapter->pdev->irq, netdev);
       
  5270 	enable_irq(adapter->pdev->irq);
       
  5271 }
       
  5272 #endif
       
  5273 
       
  5274 /**
       
  5275  * e1000_io_error_detected - called when PCI error is detected
       
  5276  * @pdev: Pointer to PCI device
       
  5277  * @state: The current pci connection state
       
  5278  *
       
  5279  * This function is called after a PCI bus error affecting
       
  5280  * this device has been detected.
       
  5281  */
       
  5282 static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
       
  5283 						pci_channel_state_t state)
       
  5284 {
       
  5285 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5286 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5287 
       
  5288 	netif_device_detach(netdev);
       
  5289 
       
  5290 	if (state == pci_channel_io_perm_failure)
       
  5291 		return PCI_ERS_RESULT_DISCONNECT;
       
  5292 
       
  5293 	if (netif_running(netdev))
       
  5294 		e1000_down(adapter);
       
  5295 	pci_disable_device(pdev);
       
  5296 
       
  5297 	/* Request a slot slot reset. */
       
  5298 	return PCI_ERS_RESULT_NEED_RESET;
       
  5299 }
       
  5300 
       
  5301 /**
       
  5302  * e1000_io_slot_reset - called after the pci bus has been reset.
       
  5303  * @pdev: Pointer to PCI device
       
  5304  *
       
  5305  * Restart the card from scratch, as if from a cold-boot. Implementation
       
  5306  * resembles the first-half of the e1000_resume routine.
       
  5307  */
       
  5308 static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
       
  5309 {
       
  5310 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5311 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5312 	struct e1000_hw *hw = &adapter->hw;
       
  5313 	int err;
       
  5314 
       
  5315 	if (adapter->need_ioport)
       
  5316 		err = pci_enable_device(pdev);
       
  5317 	else
       
  5318 		err = pci_enable_device_mem(pdev);
       
  5319 	if (err) {
       
  5320 		pr_err("Cannot re-enable PCI device after reset.\n");
       
  5321 		return PCI_ERS_RESULT_DISCONNECT;
       
  5322 	}
       
  5323 	pci_set_master(pdev);
       
  5324 
       
  5325 	pci_enable_wake(pdev, PCI_D3hot, 0);
       
  5326 	pci_enable_wake(pdev, PCI_D3cold, 0);
       
  5327 
       
  5328 	e1000_reset(adapter);
       
  5329 	ew32(WUS, ~0);
       
  5330 
       
  5331 	return PCI_ERS_RESULT_RECOVERED;
       
  5332 }
       
  5333 
       
  5334 /**
       
  5335  * e1000_io_resume - called when traffic can start flowing again.
       
  5336  * @pdev: Pointer to PCI device
       
  5337  *
       
  5338  * This callback is called when the error recovery driver tells us that
       
  5339  * its OK to resume normal operation. Implementation resembles the
       
  5340  * second-half of the e1000_resume routine.
       
  5341  */
       
  5342 static void e1000_io_resume(struct pci_dev *pdev)
       
  5343 {
       
  5344 	struct net_device *netdev = pci_get_drvdata(pdev);
       
  5345 	struct e1000_adapter *adapter = netdev_priv(netdev);
       
  5346 
       
  5347 	e1000_init_manageability(adapter);
       
  5348 
       
  5349 	if (netif_running(netdev)) {
       
  5350 		if (e1000_up(adapter)) {
       
  5351 			pr_info("can't bring device back up after reset\n");
       
  5352 			return;
       
  5353 		}
       
  5354 	}
       
  5355 
       
  5356 	netif_device_attach(netdev);
       
  5357 }
       
  5358 
       
  5359 /* e1000_main.c */