PageRenderTime 73ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/net/e1000/e1000_main.c

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