Merge branch 'upstream-fixes' of master.kernel.org:/pub/scm/linux/kernel/git/jgarzik...
[GitHub/LineageOS/android_kernel_motorola_exynos9610.git] / drivers / net / e1000 / e1000_main.c
1 /*******************************************************************************
2
3
4 Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2 of the License, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59
18 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 The full GNU General Public License is included in this distribution in the
21 file called LICENSE.
22
23 Contact Information:
24 Linux NICS <linux.nics@intel.com>
25 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 #include "e1000.h"
30
31 /* Change Log
32 * 7.0.33 3-Feb-2006
33 * o Added another fix for the pass false carrier bit
34 * 7.0.32 24-Jan-2006
35 * o Need to rebuild with noew version number for the pass false carrier
36 * fix in e1000_hw.c
37 * 7.0.30 18-Jan-2006
38 * o fixup for tso workaround to disable it for pci-x
39 * o fix mem leak on 82542
40 * o fixes for 10 Mb/s connections and incorrect stats
41 * 7.0.28 01/06/2006
42 * o hardware workaround to only set "speed mode" bit for 1G link.
43 * 7.0.26 12/23/2005
44 * o wake on lan support modified for device ID 10B5
45 * o fix dhcp + vlan issue not making it to the iAMT firmware
46 * 7.0.24 12/9/2005
47 * o New hardware support for the Gigabit NIC embedded in the south bridge
48 * o Fixes to the recycling logic (skb->tail) from IBM LTC
49 * 6.3.9 12/16/2005
50 * o incorporate fix for recycled skbs from IBM LTC
51 * 6.3.7 11/18/2005
52 * o Honor eeprom setting for enabling/disabling Wake On Lan
53 * 6.3.5 11/17/2005
54 * o Fix memory leak in rx ring handling for PCI Express adapters
55 * 6.3.4 11/8/05
56 * o Patch from Jesper Juhl to remove redundant NULL checks for kfree
57 * 6.3.2 9/20/05
58 * o Render logic that sets/resets DRV_LOAD as inline functions to
59 * avoid code replication. If f/w is AMT then set DRV_LOAD only when
60 * network interface is open.
61 * o Handle DRV_LOAD set/reset in cases where AMT uses VLANs.
62 * o Adjust PBA partioning for Jumbo frames using MTU size and not
63 * rx_buffer_len
64 * 6.3.1 9/19/05
65 * o Use adapter->tx_timeout_factor in Tx Hung Detect logic
66 * (e1000_clean_tx_irq)
67 * o Support for 8086:10B5 device (Quad Port)
68 */
69
70 char e1000_driver_name[] = "e1000";
71 static char e1000_driver_string[] = "Intel(R) PRO/1000 Network Driver";
72 #ifndef CONFIG_E1000_NAPI
73 #define DRIVERNAPI
74 #else
75 #define DRIVERNAPI "-NAPI"
76 #endif
77 #define DRV_VERSION "7.0.33-k2"DRIVERNAPI
78 char e1000_driver_version[] = DRV_VERSION;
79 static char e1000_copyright[] = "Copyright (c) 1999-2005 Intel Corporation.";
80
81 /* e1000_pci_tbl - PCI Device ID Table
82 *
83 * Last entry must be all 0s
84 *
85 * Macro expands to...
86 * {PCI_DEVICE(PCI_VENDOR_ID_INTEL, device_id)}
87 */
88 static struct pci_device_id e1000_pci_tbl[] = {
89 INTEL_E1000_ETHERNET_DEVICE(0x1000),
90 INTEL_E1000_ETHERNET_DEVICE(0x1001),
91 INTEL_E1000_ETHERNET_DEVICE(0x1004),
92 INTEL_E1000_ETHERNET_DEVICE(0x1008),
93 INTEL_E1000_ETHERNET_DEVICE(0x1009),
94 INTEL_E1000_ETHERNET_DEVICE(0x100C),
95 INTEL_E1000_ETHERNET_DEVICE(0x100D),
96 INTEL_E1000_ETHERNET_DEVICE(0x100E),
97 INTEL_E1000_ETHERNET_DEVICE(0x100F),
98 INTEL_E1000_ETHERNET_DEVICE(0x1010),
99 INTEL_E1000_ETHERNET_DEVICE(0x1011),
100 INTEL_E1000_ETHERNET_DEVICE(0x1012),
101 INTEL_E1000_ETHERNET_DEVICE(0x1013),
102 INTEL_E1000_ETHERNET_DEVICE(0x1014),
103 INTEL_E1000_ETHERNET_DEVICE(0x1015),
104 INTEL_E1000_ETHERNET_DEVICE(0x1016),
105 INTEL_E1000_ETHERNET_DEVICE(0x1017),
106 INTEL_E1000_ETHERNET_DEVICE(0x1018),
107 INTEL_E1000_ETHERNET_DEVICE(0x1019),
108 INTEL_E1000_ETHERNET_DEVICE(0x101A),
109 INTEL_E1000_ETHERNET_DEVICE(0x101D),
110 INTEL_E1000_ETHERNET_DEVICE(0x101E),
111 INTEL_E1000_ETHERNET_DEVICE(0x1026),
112 INTEL_E1000_ETHERNET_DEVICE(0x1027),
113 INTEL_E1000_ETHERNET_DEVICE(0x1028),
114 INTEL_E1000_ETHERNET_DEVICE(0x105E),
115 INTEL_E1000_ETHERNET_DEVICE(0x105F),
116 INTEL_E1000_ETHERNET_DEVICE(0x1060),
117 INTEL_E1000_ETHERNET_DEVICE(0x1075),
118 INTEL_E1000_ETHERNET_DEVICE(0x1076),
119 INTEL_E1000_ETHERNET_DEVICE(0x1077),
120 INTEL_E1000_ETHERNET_DEVICE(0x1078),
121 INTEL_E1000_ETHERNET_DEVICE(0x1079),
122 INTEL_E1000_ETHERNET_DEVICE(0x107A),
123 INTEL_E1000_ETHERNET_DEVICE(0x107B),
124 INTEL_E1000_ETHERNET_DEVICE(0x107C),
125 INTEL_E1000_ETHERNET_DEVICE(0x107D),
126 INTEL_E1000_ETHERNET_DEVICE(0x107E),
127 INTEL_E1000_ETHERNET_DEVICE(0x107F),
128 INTEL_E1000_ETHERNET_DEVICE(0x108A),
129 INTEL_E1000_ETHERNET_DEVICE(0x108B),
130 INTEL_E1000_ETHERNET_DEVICE(0x108C),
131 INTEL_E1000_ETHERNET_DEVICE(0x1096),
132 INTEL_E1000_ETHERNET_DEVICE(0x1098),
133 INTEL_E1000_ETHERNET_DEVICE(0x1099),
134 INTEL_E1000_ETHERNET_DEVICE(0x109A),
135 INTEL_E1000_ETHERNET_DEVICE(0x10B5),
136 INTEL_E1000_ETHERNET_DEVICE(0x10B9),
137 /* required last entry */
138 {0,}
139 };
140
141 MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
142
143 static int e1000_setup_tx_resources(struct e1000_adapter *adapter,
144 struct e1000_tx_ring *txdr);
145 static int e1000_setup_rx_resources(struct e1000_adapter *adapter,
146 struct e1000_rx_ring *rxdr);
147 static void e1000_free_tx_resources(struct e1000_adapter *adapter,
148 struct e1000_tx_ring *tx_ring);
149 static void e1000_free_rx_resources(struct e1000_adapter *adapter,
150 struct e1000_rx_ring *rx_ring);
151
152 /* Local Function Prototypes */
153
154 static int e1000_init_module(void);
155 static void e1000_exit_module(void);
156 static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent);
157 static void __devexit e1000_remove(struct pci_dev *pdev);
158 static int e1000_alloc_queues(struct e1000_adapter *adapter);
159 static int e1000_sw_init(struct e1000_adapter *adapter);
160 static int e1000_open(struct net_device *netdev);
161 static int e1000_close(struct net_device *netdev);
162 static void e1000_configure_tx(struct e1000_adapter *adapter);
163 static void e1000_configure_rx(struct e1000_adapter *adapter);
164 static void e1000_setup_rctl(struct e1000_adapter *adapter);
165 static void e1000_clean_all_tx_rings(struct e1000_adapter *adapter);
166 static void e1000_clean_all_rx_rings(struct e1000_adapter *adapter);
167 static void e1000_clean_tx_ring(struct e1000_adapter *adapter,
168 struct e1000_tx_ring *tx_ring);
169 static void e1000_clean_rx_ring(struct e1000_adapter *adapter,
170 struct e1000_rx_ring *rx_ring);
171 static void e1000_set_multi(struct net_device *netdev);
172 static void e1000_update_phy_info(unsigned long data);
173 static void e1000_watchdog(unsigned long data);
174 static void e1000_watchdog_task(struct e1000_adapter *adapter);
175 static void e1000_82547_tx_fifo_stall(unsigned long data);
176 static int e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev);
177 static struct net_device_stats * e1000_get_stats(struct net_device *netdev);
178 static int e1000_change_mtu(struct net_device *netdev, int new_mtu);
179 static int e1000_set_mac(struct net_device *netdev, void *p);
180 static irqreturn_t e1000_intr(int irq, void *data, struct pt_regs *regs);
181 static boolean_t e1000_clean_tx_irq(struct e1000_adapter *adapter,
182 struct e1000_tx_ring *tx_ring);
183 #ifdef CONFIG_E1000_NAPI
184 static int e1000_clean(struct net_device *poll_dev, int *budget);
185 static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
186 struct e1000_rx_ring *rx_ring,
187 int *work_done, int work_to_do);
188 static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
189 struct e1000_rx_ring *rx_ring,
190 int *work_done, int work_to_do);
191 #else
192 static boolean_t e1000_clean_rx_irq(struct e1000_adapter *adapter,
193 struct e1000_rx_ring *rx_ring);
194 static boolean_t e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
195 struct e1000_rx_ring *rx_ring);
196 #endif
197 static void e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
198 struct e1000_rx_ring *rx_ring,
199 int cleaned_count);
200 static void e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
201 struct e1000_rx_ring *rx_ring,
202 int cleaned_count);
203 static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd);
204 static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
205 int cmd);
206 static void e1000_enter_82542_rst(struct e1000_adapter *adapter);
207 static void e1000_leave_82542_rst(struct e1000_adapter *adapter);
208 static void e1000_tx_timeout(struct net_device *dev);
209 static void e1000_reset_task(struct net_device *dev);
210 static void e1000_smartspeed(struct e1000_adapter *adapter);
211 static inline int e1000_82547_fifo_workaround(struct e1000_adapter *adapter,
212 struct sk_buff *skb);
213
214 static void e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp);
215 static void e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid);
216 static void e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid);
217 static void e1000_restore_vlan(struct e1000_adapter *adapter);
218
219 #ifdef CONFIG_PM
220 static int e1000_suspend(struct pci_dev *pdev, pm_message_t state);
221 static int e1000_resume(struct pci_dev *pdev);
222 #endif
223 static void e1000_shutdown(struct pci_dev *pdev);
224
225 #ifdef CONFIG_NET_POLL_CONTROLLER
226 /* for netdump / net console */
227 static void e1000_netpoll (struct net_device *netdev);
228 #endif
229
230
231 static struct pci_driver e1000_driver = {
232 .name = e1000_driver_name,
233 .id_table = e1000_pci_tbl,
234 .probe = e1000_probe,
235 .remove = __devexit_p(e1000_remove),
236 /* Power Managment Hooks */
237 #ifdef CONFIG_PM
238 .suspend = e1000_suspend,
239 .resume = e1000_resume,
240 #endif
241 .shutdown = e1000_shutdown
242 };
243
244 MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
245 MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
246 MODULE_LICENSE("GPL");
247 MODULE_VERSION(DRV_VERSION);
248
249 static int debug = NETIF_MSG_DRV | NETIF_MSG_PROBE;
250 module_param(debug, int, 0);
251 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
252
253 /**
254 * e1000_init_module - Driver Registration Routine
255 *
256 * e1000_init_module is the first routine called when the driver is
257 * loaded. All it does is register with the PCI subsystem.
258 **/
259
260 static int __init
261 e1000_init_module(void)
262 {
263 int ret;
264 printk(KERN_INFO "%s - version %s\n",
265 e1000_driver_string, e1000_driver_version);
266
267 printk(KERN_INFO "%s\n", e1000_copyright);
268
269 ret = pci_module_init(&e1000_driver);
270
271 return ret;
272 }
273
274 module_init(e1000_init_module);
275
276 /**
277 * e1000_exit_module - Driver Exit Cleanup Routine
278 *
279 * e1000_exit_module is called just before the driver is removed
280 * from memory.
281 **/
282
283 static void __exit
284 e1000_exit_module(void)
285 {
286 pci_unregister_driver(&e1000_driver);
287 }
288
289 module_exit(e1000_exit_module);
290
291 /**
292 * e1000_irq_disable - Mask off interrupt generation on the NIC
293 * @adapter: board private structure
294 **/
295
296 static inline void
297 e1000_irq_disable(struct e1000_adapter *adapter)
298 {
299 atomic_inc(&adapter->irq_sem);
300 E1000_WRITE_REG(&adapter->hw, IMC, ~0);
301 E1000_WRITE_FLUSH(&adapter->hw);
302 synchronize_irq(adapter->pdev->irq);
303 }
304
305 /**
306 * e1000_irq_enable - Enable default interrupt generation settings
307 * @adapter: board private structure
308 **/
309
310 static inline void
311 e1000_irq_enable(struct e1000_adapter *adapter)
312 {
313 if (likely(atomic_dec_and_test(&adapter->irq_sem))) {
314 E1000_WRITE_REG(&adapter->hw, IMS, IMS_ENABLE_MASK);
315 E1000_WRITE_FLUSH(&adapter->hw);
316 }
317 }
318
319 static void
320 e1000_update_mng_vlan(struct e1000_adapter *adapter)
321 {
322 struct net_device *netdev = adapter->netdev;
323 uint16_t vid = adapter->hw.mng_cookie.vlan_id;
324 uint16_t old_vid = adapter->mng_vlan_id;
325 if (adapter->vlgrp) {
326 if (!adapter->vlgrp->vlan_devices[vid]) {
327 if (adapter->hw.mng_cookie.status &
328 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) {
329 e1000_vlan_rx_add_vid(netdev, vid);
330 adapter->mng_vlan_id = vid;
331 } else
332 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
333
334 if ((old_vid != (uint16_t)E1000_MNG_VLAN_NONE) &&
335 (vid != old_vid) &&
336 !adapter->vlgrp->vlan_devices[old_vid])
337 e1000_vlan_rx_kill_vid(netdev, old_vid);
338 } else
339 adapter->mng_vlan_id = vid;
340 }
341 }
342
343 /**
344 * e1000_release_hw_control - release control of the h/w to f/w
345 * @adapter: address of board private structure
346 *
347 * e1000_release_hw_control resets {CTRL_EXT|FWSM}:DRV_LOAD bit.
348 * For ASF and Pass Through versions of f/w this means that the
349 * driver is no longer loaded. For AMT version (only with 82573) i
350 * of the f/w this means that the netowrk i/f is closed.
351 *
352 **/
353
354 static inline void
355 e1000_release_hw_control(struct e1000_adapter *adapter)
356 {
357 uint32_t ctrl_ext;
358 uint32_t swsm;
359
360 /* Let firmware taken over control of h/w */
361 switch (adapter->hw.mac_type) {
362 case e1000_82571:
363 case e1000_82572:
364 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
365 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
366 ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
367 break;
368 case e1000_82573:
369 swsm = E1000_READ_REG(&adapter->hw, SWSM);
370 E1000_WRITE_REG(&adapter->hw, SWSM,
371 swsm & ~E1000_SWSM_DRV_LOAD);
372 default:
373 break;
374 }
375 }
376
377 /**
378 * e1000_get_hw_control - get control of the h/w from f/w
379 * @adapter: address of board private structure
380 *
381 * e1000_get_hw_control sets {CTRL_EXT|FWSM}:DRV_LOAD bit.
382 * For ASF and Pass Through versions of f/w this means that
383 * the driver is loaded. For AMT version (only with 82573)
384 * of the f/w this means that the netowrk i/f is open.
385 *
386 **/
387
388 static inline void
389 e1000_get_hw_control(struct e1000_adapter *adapter)
390 {
391 uint32_t ctrl_ext;
392 uint32_t swsm;
393 /* Let firmware know the driver has taken over */
394 switch (adapter->hw.mac_type) {
395 case e1000_82571:
396 case e1000_82572:
397 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
398 E1000_WRITE_REG(&adapter->hw, CTRL_EXT,
399 ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
400 break;
401 case e1000_82573:
402 swsm = E1000_READ_REG(&adapter->hw, SWSM);
403 E1000_WRITE_REG(&adapter->hw, SWSM,
404 swsm | E1000_SWSM_DRV_LOAD);
405 break;
406 default:
407 break;
408 }
409 }
410
411 int
412 e1000_up(struct e1000_adapter *adapter)
413 {
414 struct net_device *netdev = adapter->netdev;
415 int i, err;
416
417 /* hardware has been reset, we need to reload some things */
418
419 /* Reset the PHY if it was previously powered down */
420 if (adapter->hw.media_type == e1000_media_type_copper) {
421 uint16_t mii_reg;
422 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
423 if (mii_reg & MII_CR_POWER_DOWN)
424 e1000_phy_reset(&adapter->hw);
425 }
426
427 e1000_set_multi(netdev);
428
429 e1000_restore_vlan(adapter);
430
431 e1000_configure_tx(adapter);
432 e1000_setup_rctl(adapter);
433 e1000_configure_rx(adapter);
434 /* call E1000_DESC_UNUSED which always leaves
435 * at least 1 descriptor unused to make sure
436 * next_to_use != next_to_clean */
437 for (i = 0; i < adapter->num_rx_queues; i++) {
438 struct e1000_rx_ring *ring = &adapter->rx_ring[i];
439 adapter->alloc_rx_buf(adapter, ring,
440 E1000_DESC_UNUSED(ring));
441 }
442
443 #ifdef CONFIG_PCI_MSI
444 if (adapter->hw.mac_type > e1000_82547_rev_2) {
445 adapter->have_msi = TRUE;
446 if ((err = pci_enable_msi(adapter->pdev))) {
447 DPRINTK(PROBE, ERR,
448 "Unable to allocate MSI interrupt Error: %d\n", err);
449 adapter->have_msi = FALSE;
450 }
451 }
452 #endif
453 if ((err = request_irq(adapter->pdev->irq, &e1000_intr,
454 SA_SHIRQ | SA_SAMPLE_RANDOM,
455 netdev->name, netdev))) {
456 DPRINTK(PROBE, ERR,
457 "Unable to allocate interrupt Error: %d\n", err);
458 return err;
459 }
460
461 adapter->tx_queue_len = netdev->tx_queue_len;
462
463 mod_timer(&adapter->watchdog_timer, jiffies);
464
465 #ifdef CONFIG_E1000_NAPI
466 netif_poll_enable(netdev);
467 #endif
468 e1000_irq_enable(adapter);
469
470 return 0;
471 }
472
473 void
474 e1000_down(struct e1000_adapter *adapter)
475 {
476 struct net_device *netdev = adapter->netdev;
477 boolean_t mng_mode_enabled = (adapter->hw.mac_type >= e1000_82571) &&
478 e1000_check_mng_mode(&adapter->hw);
479
480 e1000_irq_disable(adapter);
481
482 free_irq(adapter->pdev->irq, netdev);
483 #ifdef CONFIG_PCI_MSI
484 if (adapter->hw.mac_type > e1000_82547_rev_2 &&
485 adapter->have_msi == TRUE)
486 pci_disable_msi(adapter->pdev);
487 #endif
488 del_timer_sync(&adapter->tx_fifo_stall_timer);
489 del_timer_sync(&adapter->watchdog_timer);
490 del_timer_sync(&adapter->phy_info_timer);
491
492 #ifdef CONFIG_E1000_NAPI
493 netif_poll_disable(netdev);
494 #endif
495 netdev->tx_queue_len = adapter->tx_queue_len;
496 adapter->link_speed = 0;
497 adapter->link_duplex = 0;
498 netif_carrier_off(netdev);
499 netif_stop_queue(netdev);
500
501 e1000_reset(adapter);
502 e1000_clean_all_tx_rings(adapter);
503 e1000_clean_all_rx_rings(adapter);
504
505 /* Power down the PHY so no link is implied when interface is down *
506 * The PHY cannot be powered down if any of the following is TRUE *
507 * (a) WoL is enabled
508 * (b) AMT is active
509 * (c) SoL/IDER session is active */
510 if (!adapter->wol && adapter->hw.mac_type >= e1000_82540 &&
511 adapter->hw.media_type == e1000_media_type_copper &&
512 !(E1000_READ_REG(&adapter->hw, MANC) & E1000_MANC_SMBUS_EN) &&
513 !mng_mode_enabled &&
514 !e1000_check_phy_reset_block(&adapter->hw)) {
515 uint16_t mii_reg;
516 e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &mii_reg);
517 mii_reg |= MII_CR_POWER_DOWN;
518 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, mii_reg);
519 mdelay(1);
520 }
521 }
522
523 void
524 e1000_reset(struct e1000_adapter *adapter)
525 {
526 uint32_t pba, manc;
527 uint16_t fc_high_water_mark = E1000_FC_HIGH_DIFF;
528
529 /* Repartition Pba for greater than 9k mtu
530 * To take effect CTRL.RST is required.
531 */
532
533 switch (adapter->hw.mac_type) {
534 case e1000_82547:
535 case e1000_82547_rev_2:
536 pba = E1000_PBA_30K;
537 break;
538 case e1000_82571:
539 case e1000_82572:
540 case e1000_80003es2lan:
541 pba = E1000_PBA_38K;
542 break;
543 case e1000_82573:
544 pba = E1000_PBA_12K;
545 break;
546 default:
547 pba = E1000_PBA_48K;
548 break;
549 }
550
551 if ((adapter->hw.mac_type != e1000_82573) &&
552 (adapter->netdev->mtu > E1000_RXBUFFER_8192))
553 pba -= 8; /* allocate more FIFO for Tx */
554
555
556 if (adapter->hw.mac_type == e1000_82547) {
557 adapter->tx_fifo_head = 0;
558 adapter->tx_head_addr = pba << E1000_TX_HEAD_ADDR_SHIFT;
559 adapter->tx_fifo_size =
560 (E1000_PBA_40K - pba) << E1000_PBA_BYTES_SHIFT;
561 atomic_set(&adapter->tx_fifo_stall, 0);
562 }
563
564 E1000_WRITE_REG(&adapter->hw, PBA, pba);
565
566 /* flow control settings */
567 /* Set the FC high water mark to 90% of the FIFO size.
568 * Required to clear last 3 LSB */
569 fc_high_water_mark = ((pba * 9216)/10) & 0xFFF8;
570
571 adapter->hw.fc_high_water = fc_high_water_mark;
572 adapter->hw.fc_low_water = fc_high_water_mark - 8;
573 if (adapter->hw.mac_type == e1000_80003es2lan)
574 adapter->hw.fc_pause_time = 0xFFFF;
575 else
576 adapter->hw.fc_pause_time = E1000_FC_PAUSE_TIME;
577 adapter->hw.fc_send_xon = 1;
578 adapter->hw.fc = adapter->hw.original_fc;
579
580 /* Allow time for pending master requests to run */
581 e1000_reset_hw(&adapter->hw);
582 if (adapter->hw.mac_type >= e1000_82544)
583 E1000_WRITE_REG(&adapter->hw, WUC, 0);
584 if (e1000_init_hw(&adapter->hw))
585 DPRINTK(PROBE, ERR, "Hardware Error\n");
586 e1000_update_mng_vlan(adapter);
587 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
588 E1000_WRITE_REG(&adapter->hw, VET, ETHERNET_IEEE_VLAN_TYPE);
589
590 e1000_reset_adaptive(&adapter->hw);
591 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
592 if (adapter->en_mng_pt) {
593 manc = E1000_READ_REG(&adapter->hw, MANC);
594 manc |= (E1000_MANC_ARP_EN | E1000_MANC_EN_MNG2HOST);
595 E1000_WRITE_REG(&adapter->hw, MANC, manc);
596 }
597 }
598
599 /**
600 * e1000_probe - Device Initialization Routine
601 * @pdev: PCI device information struct
602 * @ent: entry in e1000_pci_tbl
603 *
604 * Returns 0 on success, negative on failure
605 *
606 * e1000_probe initializes an adapter identified by a pci_dev structure.
607 * The OS initialization, configuring of the adapter private structure,
608 * and a hardware reset occur.
609 **/
610
611 static int __devinit
612 e1000_probe(struct pci_dev *pdev,
613 const struct pci_device_id *ent)
614 {
615 struct net_device *netdev;
616 struct e1000_adapter *adapter;
617 unsigned long mmio_start, mmio_len;
618
619 static int cards_found = 0;
620 static int e1000_ksp3_port_a = 0; /* global ksp3 port a indication */
621 int i, err, pci_using_dac;
622 uint16_t eeprom_data;
623 uint16_t eeprom_apme_mask = E1000_EEPROM_APME;
624 if ((err = pci_enable_device(pdev)))
625 return err;
626
627 if (!(err = pci_set_dma_mask(pdev, DMA_64BIT_MASK))) {
628 pci_using_dac = 1;
629 } else {
630 if ((err = pci_set_dma_mask(pdev, DMA_32BIT_MASK))) {
631 E1000_ERR("No usable DMA configuration, aborting\n");
632 return err;
633 }
634 pci_using_dac = 0;
635 }
636
637 if ((err = pci_request_regions(pdev, e1000_driver_name)))
638 return err;
639
640 pci_set_master(pdev);
641
642 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
643 if (!netdev) {
644 err = -ENOMEM;
645 goto err_alloc_etherdev;
646 }
647
648 SET_MODULE_OWNER(netdev);
649 SET_NETDEV_DEV(netdev, &pdev->dev);
650
651 pci_set_drvdata(pdev, netdev);
652 adapter = netdev_priv(netdev);
653 adapter->netdev = netdev;
654 adapter->pdev = pdev;
655 adapter->hw.back = adapter;
656 adapter->msg_enable = (1 << debug) - 1;
657
658 mmio_start = pci_resource_start(pdev, BAR_0);
659 mmio_len = pci_resource_len(pdev, BAR_0);
660
661 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
662 if (!adapter->hw.hw_addr) {
663 err = -EIO;
664 goto err_ioremap;
665 }
666
667 for (i = BAR_1; i <= BAR_5; i++) {
668 if (pci_resource_len(pdev, i) == 0)
669 continue;
670 if (pci_resource_flags(pdev, i) & IORESOURCE_IO) {
671 adapter->hw.io_base = pci_resource_start(pdev, i);
672 break;
673 }
674 }
675
676 netdev->open = &e1000_open;
677 netdev->stop = &e1000_close;
678 netdev->hard_start_xmit = &e1000_xmit_frame;
679 netdev->get_stats = &e1000_get_stats;
680 netdev->set_multicast_list = &e1000_set_multi;
681 netdev->set_mac_address = &e1000_set_mac;
682 netdev->change_mtu = &e1000_change_mtu;
683 netdev->do_ioctl = &e1000_ioctl;
684 e1000_set_ethtool_ops(netdev);
685 netdev->tx_timeout = &e1000_tx_timeout;
686 netdev->watchdog_timeo = 5 * HZ;
687 #ifdef CONFIG_E1000_NAPI
688 netdev->poll = &e1000_clean;
689 netdev->weight = 64;
690 #endif
691 netdev->vlan_rx_register = e1000_vlan_rx_register;
692 netdev->vlan_rx_add_vid = e1000_vlan_rx_add_vid;
693 netdev->vlan_rx_kill_vid = e1000_vlan_rx_kill_vid;
694 #ifdef CONFIG_NET_POLL_CONTROLLER
695 netdev->poll_controller = e1000_netpoll;
696 #endif
697 strcpy(netdev->name, pci_name(pdev));
698
699 netdev->mem_start = mmio_start;
700 netdev->mem_end = mmio_start + mmio_len;
701 netdev->base_addr = adapter->hw.io_base;
702
703 adapter->bd_number = cards_found;
704
705 /* setup the private structure */
706
707 if ((err = e1000_sw_init(adapter)))
708 goto err_sw_init;
709
710 if ((err = e1000_check_phy_reset_block(&adapter->hw)))
711 DPRINTK(PROBE, INFO, "PHY reset is blocked due to SOL/IDER session.\n");
712
713 /* if ksp3, indicate if it's port a being setup */
714 if (pdev->device == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3 &&
715 e1000_ksp3_port_a == 0)
716 adapter->ksp3_port_a = 1;
717 e1000_ksp3_port_a++;
718 /* Reset for multiple KP3 adapters */
719 if (e1000_ksp3_port_a == 4)
720 e1000_ksp3_port_a = 0;
721
722 if (adapter->hw.mac_type >= e1000_82543) {
723 netdev->features = NETIF_F_SG |
724 NETIF_F_HW_CSUM |
725 NETIF_F_HW_VLAN_TX |
726 NETIF_F_HW_VLAN_RX |
727 NETIF_F_HW_VLAN_FILTER;
728 }
729
730 #ifdef NETIF_F_TSO
731 if ((adapter->hw.mac_type >= e1000_82544) &&
732 (adapter->hw.mac_type != e1000_82547))
733 netdev->features |= NETIF_F_TSO;
734
735 #ifdef NETIF_F_TSO_IPV6
736 if (adapter->hw.mac_type > e1000_82547_rev_2)
737 netdev->features |= NETIF_F_TSO_IPV6;
738 #endif
739 #endif
740 if (pci_using_dac)
741 netdev->features |= NETIF_F_HIGHDMA;
742
743 /* hard_start_xmit is safe against parallel locking */
744 netdev->features |= NETIF_F_LLTX;
745
746 adapter->en_mng_pt = e1000_enable_mng_pass_thru(&adapter->hw);
747
748 /* before reading the EEPROM, reset the controller to
749 * put the device in a known good starting state */
750
751 e1000_reset_hw(&adapter->hw);
752
753 /* make sure the EEPROM is good */
754
755 if (e1000_validate_eeprom_checksum(&adapter->hw) < 0) {
756 DPRINTK(PROBE, ERR, "The EEPROM Checksum Is Not Valid\n");
757 err = -EIO;
758 goto err_eeprom;
759 }
760
761 /* copy the MAC address out of the EEPROM */
762
763 if (e1000_read_mac_addr(&adapter->hw))
764 DPRINTK(PROBE, ERR, "EEPROM Read Error\n");
765 memcpy(netdev->dev_addr, adapter->hw.mac_addr, netdev->addr_len);
766 memcpy(netdev->perm_addr, adapter->hw.mac_addr, netdev->addr_len);
767
768 if (!is_valid_ether_addr(netdev->perm_addr)) {
769 DPRINTK(PROBE, ERR, "Invalid MAC Address\n");
770 err = -EIO;
771 goto err_eeprom;
772 }
773
774 e1000_read_part_num(&adapter->hw, &(adapter->part_num));
775
776 e1000_get_bus_info(&adapter->hw);
777
778 init_timer(&adapter->tx_fifo_stall_timer);
779 adapter->tx_fifo_stall_timer.function = &e1000_82547_tx_fifo_stall;
780 adapter->tx_fifo_stall_timer.data = (unsigned long) adapter;
781
782 init_timer(&adapter->watchdog_timer);
783 adapter->watchdog_timer.function = &e1000_watchdog;
784 adapter->watchdog_timer.data = (unsigned long) adapter;
785
786 INIT_WORK(&adapter->watchdog_task,
787 (void (*)(void *))e1000_watchdog_task, adapter);
788
789 init_timer(&adapter->phy_info_timer);
790 adapter->phy_info_timer.function = &e1000_update_phy_info;
791 adapter->phy_info_timer.data = (unsigned long) adapter;
792
793 INIT_WORK(&adapter->reset_task,
794 (void (*)(void *))e1000_reset_task, netdev);
795
796 /* we're going to reset, so assume we have no link for now */
797
798 netif_carrier_off(netdev);
799 netif_stop_queue(netdev);
800
801 e1000_check_options(adapter);
802
803 /* Initial Wake on LAN setting
804 * If APM wake is enabled in the EEPROM,
805 * enable the ACPI Magic Packet filter
806 */
807
808 switch (adapter->hw.mac_type) {
809 case e1000_82542_rev2_0:
810 case e1000_82542_rev2_1:
811 case e1000_82543:
812 break;
813 case e1000_82544:
814 e1000_read_eeprom(&adapter->hw,
815 EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data);
816 eeprom_apme_mask = E1000_EEPROM_82544_APM;
817 break;
818 case e1000_82546:
819 case e1000_82546_rev_3:
820 case e1000_82571:
821 case e1000_80003es2lan:
822 if (E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_FUNC_1){
823 e1000_read_eeprom(&adapter->hw,
824 EEPROM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
825 break;
826 }
827 /* Fall Through */
828 default:
829 e1000_read_eeprom(&adapter->hw,
830 EEPROM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
831 break;
832 }
833 if (eeprom_data & eeprom_apme_mask)
834 adapter->wol |= E1000_WUFC_MAG;
835
836 /* print bus type/speed/width info */
837 {
838 struct e1000_hw *hw = &adapter->hw;
839 DPRINTK(PROBE, INFO, "(PCI%s:%s:%s) ",
840 ((hw->bus_type == e1000_bus_type_pcix) ? "-X" :
841 (hw->bus_type == e1000_bus_type_pci_express ? " Express":"")),
842 ((hw->bus_speed == e1000_bus_speed_2500) ? "2.5Gb/s" :
843 (hw->bus_speed == e1000_bus_speed_133) ? "133MHz" :
844 (hw->bus_speed == e1000_bus_speed_120) ? "120MHz" :
845 (hw->bus_speed == e1000_bus_speed_100) ? "100MHz" :
846 (hw->bus_speed == e1000_bus_speed_66) ? "66MHz" : "33MHz"),
847 ((hw->bus_width == e1000_bus_width_64) ? "64-bit" :
848 (hw->bus_width == e1000_bus_width_pciex_4) ? "Width x4" :
849 (hw->bus_width == e1000_bus_width_pciex_1) ? "Width x1" :
850 "32-bit"));
851 }
852
853 for (i = 0; i < 6; i++)
854 printk("%2.2x%c", netdev->dev_addr[i], i == 5 ? '\n' : ':');
855
856 /* reset the hardware with the new settings */
857 e1000_reset(adapter);
858
859 /* If the controller is 82573 and f/w is AMT, do not set
860 * DRV_LOAD until the interface is up. For all other cases,
861 * let the f/w know that the h/w is now under the control
862 * of the driver. */
863 if (adapter->hw.mac_type != e1000_82573 ||
864 !e1000_check_mng_mode(&adapter->hw))
865 e1000_get_hw_control(adapter);
866
867 strcpy(netdev->name, "eth%d");
868 if ((err = register_netdev(netdev)))
869 goto err_register;
870
871 DPRINTK(PROBE, INFO, "Intel(R) PRO/1000 Network Connection\n");
872
873 cards_found++;
874 return 0;
875
876 err_register:
877 err_sw_init:
878 err_eeprom:
879 iounmap(adapter->hw.hw_addr);
880 err_ioremap:
881 free_netdev(netdev);
882 err_alloc_etherdev:
883 pci_release_regions(pdev);
884 return err;
885 }
886
887 /**
888 * e1000_remove - Device Removal Routine
889 * @pdev: PCI device information struct
890 *
891 * e1000_remove is called by the PCI subsystem to alert the driver
892 * that it should release a PCI device. The could be caused by a
893 * Hot-Plug event, or because the driver is going to be removed from
894 * memory.
895 **/
896
897 static void __devexit
898 e1000_remove(struct pci_dev *pdev)
899 {
900 struct net_device *netdev = pci_get_drvdata(pdev);
901 struct e1000_adapter *adapter = netdev_priv(netdev);
902 uint32_t manc;
903 #ifdef CONFIG_E1000_NAPI
904 int i;
905 #endif
906
907 flush_scheduled_work();
908
909 if (adapter->hw.mac_type >= e1000_82540 &&
910 adapter->hw.media_type == e1000_media_type_copper) {
911 manc = E1000_READ_REG(&adapter->hw, MANC);
912 if (manc & E1000_MANC_SMBUS_EN) {
913 manc |= E1000_MANC_ARP_EN;
914 E1000_WRITE_REG(&adapter->hw, MANC, manc);
915 }
916 }
917
918 /* Release control of h/w to f/w. If f/w is AMT enabled, this
919 * would have already happened in close and is redundant. */
920 e1000_release_hw_control(adapter);
921
922 unregister_netdev(netdev);
923 #ifdef CONFIG_E1000_NAPI
924 for (i = 0; i < adapter->num_rx_queues; i++)
925 dev_put(&adapter->polling_netdev[i]);
926 #endif
927
928 if (!e1000_check_phy_reset_block(&adapter->hw))
929 e1000_phy_hw_reset(&adapter->hw);
930
931 kfree(adapter->tx_ring);
932 kfree(adapter->rx_ring);
933 #ifdef CONFIG_E1000_NAPI
934 kfree(adapter->polling_netdev);
935 #endif
936
937 iounmap(adapter->hw.hw_addr);
938 pci_release_regions(pdev);
939
940 free_netdev(netdev);
941
942 pci_disable_device(pdev);
943 }
944
945 /**
946 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
947 * @adapter: board private structure to initialize
948 *
949 * e1000_sw_init initializes the Adapter private data structure.
950 * Fields are initialized based on PCI device information and
951 * OS network device settings (MTU size).
952 **/
953
954 static int __devinit
955 e1000_sw_init(struct e1000_adapter *adapter)
956 {
957 struct e1000_hw *hw = &adapter->hw;
958 struct net_device *netdev = adapter->netdev;
959 struct pci_dev *pdev = adapter->pdev;
960 #ifdef CONFIG_E1000_NAPI
961 int i;
962 #endif
963
964 /* PCI config space info */
965
966 hw->vendor_id = pdev->vendor;
967 hw->device_id = pdev->device;
968 hw->subsystem_vendor_id = pdev->subsystem_vendor;
969 hw->subsystem_id = pdev->subsystem_device;
970
971 pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
972
973 pci_read_config_word(pdev, PCI_COMMAND, &hw->pci_cmd_word);
974
975 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
976 adapter->rx_ps_bsize0 = E1000_RXBUFFER_256;
977 hw->max_frame_size = netdev->mtu +
978 ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
979 hw->min_frame_size = MINIMUM_ETHERNET_FRAME_SIZE;
980
981 /* identify the MAC */
982
983 if (e1000_set_mac_type(hw)) {
984 DPRINTK(PROBE, ERR, "Unknown MAC Type\n");
985 return -EIO;
986 }
987
988 /* initialize eeprom parameters */
989
990 if (e1000_init_eeprom_params(hw)) {
991 E1000_ERR("EEPROM initialization failed\n");
992 return -EIO;
993 }
994
995 switch (hw->mac_type) {
996 default:
997 break;
998 case e1000_82541:
999 case e1000_82547:
1000 case e1000_82541_rev_2:
1001 case e1000_82547_rev_2:
1002 hw->phy_init_script = 1;
1003 break;
1004 }
1005
1006 e1000_set_media_type(hw);
1007
1008 hw->wait_autoneg_complete = FALSE;
1009 hw->tbi_compatibility_en = TRUE;
1010 hw->adaptive_ifs = TRUE;
1011
1012 /* Copper options */
1013
1014 if (hw->media_type == e1000_media_type_copper) {
1015 hw->mdix = AUTO_ALL_MODES;
1016 hw->disable_polarity_correction = FALSE;
1017 hw->master_slave = E1000_MASTER_SLAVE;
1018 }
1019
1020 adapter->num_tx_queues = 1;
1021 adapter->num_rx_queues = 1;
1022
1023 if (e1000_alloc_queues(adapter)) {
1024 DPRINTK(PROBE, ERR, "Unable to allocate memory for queues\n");
1025 return -ENOMEM;
1026 }
1027
1028 #ifdef CONFIG_E1000_NAPI
1029 for (i = 0; i < adapter->num_rx_queues; i++) {
1030 adapter->polling_netdev[i].priv = adapter;
1031 adapter->polling_netdev[i].poll = &e1000_clean;
1032 adapter->polling_netdev[i].weight = 64;
1033 dev_hold(&adapter->polling_netdev[i]);
1034 set_bit(__LINK_STATE_START, &adapter->polling_netdev[i].state);
1035 }
1036 spin_lock_init(&adapter->tx_queue_lock);
1037 #endif
1038
1039 atomic_set(&adapter->irq_sem, 1);
1040 spin_lock_init(&adapter->stats_lock);
1041
1042 return 0;
1043 }
1044
1045 /**
1046 * e1000_alloc_queues - Allocate memory for all rings
1047 * @adapter: board private structure to initialize
1048 *
1049 * We allocate one ring per queue at run-time since we don't know the
1050 * number of queues at compile-time. The polling_netdev array is
1051 * intended for Multiqueue, but should work fine with a single queue.
1052 **/
1053
1054 static int __devinit
1055 e1000_alloc_queues(struct e1000_adapter *adapter)
1056 {
1057 int size;
1058
1059 size = sizeof(struct e1000_tx_ring) * adapter->num_tx_queues;
1060 adapter->tx_ring = kmalloc(size, GFP_KERNEL);
1061 if (!adapter->tx_ring)
1062 return -ENOMEM;
1063 memset(adapter->tx_ring, 0, size);
1064
1065 size = sizeof(struct e1000_rx_ring) * adapter->num_rx_queues;
1066 adapter->rx_ring = kmalloc(size, GFP_KERNEL);
1067 if (!adapter->rx_ring) {
1068 kfree(adapter->tx_ring);
1069 return -ENOMEM;
1070 }
1071 memset(adapter->rx_ring, 0, size);
1072
1073 #ifdef CONFIG_E1000_NAPI
1074 size = sizeof(struct net_device) * adapter->num_rx_queues;
1075 adapter->polling_netdev = kmalloc(size, GFP_KERNEL);
1076 if (!adapter->polling_netdev) {
1077 kfree(adapter->tx_ring);
1078 kfree(adapter->rx_ring);
1079 return -ENOMEM;
1080 }
1081 memset(adapter->polling_netdev, 0, size);
1082 #endif
1083
1084 return E1000_SUCCESS;
1085 }
1086
1087 /**
1088 * e1000_open - Called when a network interface is made active
1089 * @netdev: network interface device structure
1090 *
1091 * Returns 0 on success, negative value on failure
1092 *
1093 * The open entry point is called when a network interface is made
1094 * active by the system (IFF_UP). At this point all resources needed
1095 * for transmit and receive operations are allocated, the interrupt
1096 * handler is registered with the OS, the watchdog timer is started,
1097 * and the stack is notified that the interface is ready.
1098 **/
1099
1100 static int
1101 e1000_open(struct net_device *netdev)
1102 {
1103 struct e1000_adapter *adapter = netdev_priv(netdev);
1104 int err;
1105
1106 /* allocate transmit descriptors */
1107
1108 if ((err = e1000_setup_all_tx_resources(adapter)))
1109 goto err_setup_tx;
1110
1111 /* allocate receive descriptors */
1112
1113 if ((err = e1000_setup_all_rx_resources(adapter)))
1114 goto err_setup_rx;
1115
1116 if ((err = e1000_up(adapter)))
1117 goto err_up;
1118 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
1119 if ((adapter->hw.mng_cookie.status &
1120 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1121 e1000_update_mng_vlan(adapter);
1122 }
1123
1124 /* If AMT is enabled, let the firmware know that the network
1125 * interface is now open */
1126 if (adapter->hw.mac_type == e1000_82573 &&
1127 e1000_check_mng_mode(&adapter->hw))
1128 e1000_get_hw_control(adapter);
1129
1130 return E1000_SUCCESS;
1131
1132 err_up:
1133 e1000_free_all_rx_resources(adapter);
1134 err_setup_rx:
1135 e1000_free_all_tx_resources(adapter);
1136 err_setup_tx:
1137 e1000_reset(adapter);
1138
1139 return err;
1140 }
1141
1142 /**
1143 * e1000_close - Disables a network interface
1144 * @netdev: network interface device structure
1145 *
1146 * Returns 0, this is not allowed to fail
1147 *
1148 * The close entry point is called when an interface is de-activated
1149 * by the OS. The hardware is still under the drivers control, but
1150 * needs to be disabled. A global MAC reset is issued to stop the
1151 * hardware, and all transmit and receive resources are freed.
1152 **/
1153
1154 static int
1155 e1000_close(struct net_device *netdev)
1156 {
1157 struct e1000_adapter *adapter = netdev_priv(netdev);
1158
1159 e1000_down(adapter);
1160
1161 e1000_free_all_tx_resources(adapter);
1162 e1000_free_all_rx_resources(adapter);
1163
1164 if ((adapter->hw.mng_cookie.status &
1165 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) {
1166 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
1167 }
1168
1169 /* If AMT is enabled, let the firmware know that the network
1170 * interface is now closed */
1171 if (adapter->hw.mac_type == e1000_82573 &&
1172 e1000_check_mng_mode(&adapter->hw))
1173 e1000_release_hw_control(adapter);
1174
1175 return 0;
1176 }
1177
1178 /**
1179 * e1000_check_64k_bound - check that memory doesn't cross 64kB boundary
1180 * @adapter: address of board private structure
1181 * @start: address of beginning of memory
1182 * @len: length of memory
1183 **/
1184 static inline boolean_t
1185 e1000_check_64k_bound(struct e1000_adapter *adapter,
1186 void *start, unsigned long len)
1187 {
1188 unsigned long begin = (unsigned long) start;
1189 unsigned long end = begin + len;
1190
1191 /* First rev 82545 and 82546 need to not allow any memory
1192 * write location to cross 64k boundary due to errata 23 */
1193 if (adapter->hw.mac_type == e1000_82545 ||
1194 adapter->hw.mac_type == e1000_82546) {
1195 return ((begin ^ (end - 1)) >> 16) != 0 ? FALSE : TRUE;
1196 }
1197
1198 return TRUE;
1199 }
1200
1201 /**
1202 * e1000_setup_tx_resources - allocate Tx resources (Descriptors)
1203 * @adapter: board private structure
1204 * @txdr: tx descriptor ring (for a specific queue) to setup
1205 *
1206 * Return 0 on success, negative on failure
1207 **/
1208
1209 static int
1210 e1000_setup_tx_resources(struct e1000_adapter *adapter,
1211 struct e1000_tx_ring *txdr)
1212 {
1213 struct pci_dev *pdev = adapter->pdev;
1214 int size;
1215
1216 size = sizeof(struct e1000_buffer) * txdr->count;
1217
1218 txdr->buffer_info = vmalloc_node(size, pcibus_to_node(pdev->bus));
1219 if (!txdr->buffer_info) {
1220 DPRINTK(PROBE, ERR,
1221 "Unable to allocate memory for the transmit descriptor ring\n");
1222 return -ENOMEM;
1223 }
1224 memset(txdr->buffer_info, 0, size);
1225
1226 /* round up to nearest 4K */
1227
1228 txdr->size = txdr->count * sizeof(struct e1000_tx_desc);
1229 E1000_ROUNDUP(txdr->size, 4096);
1230
1231 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1232 if (!txdr->desc) {
1233 setup_tx_desc_die:
1234 vfree(txdr->buffer_info);
1235 DPRINTK(PROBE, ERR,
1236 "Unable to allocate memory for the transmit descriptor ring\n");
1237 return -ENOMEM;
1238 }
1239
1240 /* Fix for errata 23, can't cross 64kB boundary */
1241 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1242 void *olddesc = txdr->desc;
1243 dma_addr_t olddma = txdr->dma;
1244 DPRINTK(TX_ERR, ERR, "txdr align check failed: %u bytes "
1245 "at %p\n", txdr->size, txdr->desc);
1246 /* Try again, without freeing the previous */
1247 txdr->desc = pci_alloc_consistent(pdev, txdr->size, &txdr->dma);
1248 /* Failed allocation, critical failure */
1249 if (!txdr->desc) {
1250 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1251 goto setup_tx_desc_die;
1252 }
1253
1254 if (!e1000_check_64k_bound(adapter, txdr->desc, txdr->size)) {
1255 /* give up */
1256 pci_free_consistent(pdev, txdr->size, txdr->desc,
1257 txdr->dma);
1258 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1259 DPRINTK(PROBE, ERR,
1260 "Unable to allocate aligned memory "
1261 "for the transmit descriptor ring\n");
1262 vfree(txdr->buffer_info);
1263 return -ENOMEM;
1264 } else {
1265 /* Free old allocation, new allocation was successful */
1266 pci_free_consistent(pdev, txdr->size, olddesc, olddma);
1267 }
1268 }
1269 memset(txdr->desc, 0, txdr->size);
1270
1271 txdr->next_to_use = 0;
1272 txdr->next_to_clean = 0;
1273 spin_lock_init(&txdr->tx_lock);
1274
1275 return 0;
1276 }
1277
1278 /**
1279 * e1000_setup_all_tx_resources - wrapper to allocate Tx resources
1280 * (Descriptors) for all queues
1281 * @adapter: board private structure
1282 *
1283 * If this function returns with an error, then it's possible one or
1284 * more of the rings is populated (while the rest are not). It is the
1285 * callers duty to clean those orphaned rings.
1286 *
1287 * Return 0 on success, negative on failure
1288 **/
1289
1290 int
1291 e1000_setup_all_tx_resources(struct e1000_adapter *adapter)
1292 {
1293 int i, err = 0;
1294
1295 for (i = 0; i < adapter->num_tx_queues; i++) {
1296 err = e1000_setup_tx_resources(adapter, &adapter->tx_ring[i]);
1297 if (err) {
1298 DPRINTK(PROBE, ERR,
1299 "Allocation for Tx Queue %u failed\n", i);
1300 break;
1301 }
1302 }
1303
1304 return err;
1305 }
1306
1307 /**
1308 * e1000_configure_tx - Configure 8254x Transmit Unit after Reset
1309 * @adapter: board private structure
1310 *
1311 * Configure the Tx unit of the MAC after a reset.
1312 **/
1313
1314 static void
1315 e1000_configure_tx(struct e1000_adapter *adapter)
1316 {
1317 uint64_t tdba;
1318 struct e1000_hw *hw = &adapter->hw;
1319 uint32_t tdlen, tctl, tipg, tarc;
1320 uint32_t ipgr1, ipgr2;
1321
1322 /* Setup the HW Tx Head and Tail descriptor pointers */
1323
1324 switch (adapter->num_tx_queues) {
1325 case 1:
1326 default:
1327 tdba = adapter->tx_ring[0].dma;
1328 tdlen = adapter->tx_ring[0].count *
1329 sizeof(struct e1000_tx_desc);
1330 E1000_WRITE_REG(hw, TDBAL, (tdba & 0x00000000ffffffffULL));
1331 E1000_WRITE_REG(hw, TDBAH, (tdba >> 32));
1332 E1000_WRITE_REG(hw, TDLEN, tdlen);
1333 E1000_WRITE_REG(hw, TDH, 0);
1334 E1000_WRITE_REG(hw, TDT, 0);
1335 adapter->tx_ring[0].tdh = E1000_TDH;
1336 adapter->tx_ring[0].tdt = E1000_TDT;
1337 break;
1338 }
1339
1340 /* Set the default values for the Tx Inter Packet Gap timer */
1341
1342 if (hw->media_type == e1000_media_type_fiber ||
1343 hw->media_type == e1000_media_type_internal_serdes)
1344 tipg = DEFAULT_82543_TIPG_IPGT_FIBER;
1345 else
1346 tipg = DEFAULT_82543_TIPG_IPGT_COPPER;
1347
1348 switch (hw->mac_type) {
1349 case e1000_82542_rev2_0:
1350 case e1000_82542_rev2_1:
1351 tipg = DEFAULT_82542_TIPG_IPGT;
1352 ipgr1 = DEFAULT_82542_TIPG_IPGR1;
1353 ipgr2 = DEFAULT_82542_TIPG_IPGR2;
1354 break;
1355 case e1000_80003es2lan:
1356 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1357 ipgr2 = DEFAULT_80003ES2LAN_TIPG_IPGR2;
1358 break;
1359 default:
1360 ipgr1 = DEFAULT_82543_TIPG_IPGR1;
1361 ipgr2 = DEFAULT_82543_TIPG_IPGR2;
1362 break;
1363 }
1364 tipg |= ipgr1 << E1000_TIPG_IPGR1_SHIFT;
1365 tipg |= ipgr2 << E1000_TIPG_IPGR2_SHIFT;
1366 E1000_WRITE_REG(hw, TIPG, tipg);
1367
1368 /* Set the Tx Interrupt Delay register */
1369
1370 E1000_WRITE_REG(hw, TIDV, adapter->tx_int_delay);
1371 if (hw->mac_type >= e1000_82540)
1372 E1000_WRITE_REG(hw, TADV, adapter->tx_abs_int_delay);
1373
1374 /* Program the Transmit Control Register */
1375
1376 tctl = E1000_READ_REG(hw, TCTL);
1377
1378 tctl &= ~E1000_TCTL_CT;
1379 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
1380 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
1381
1382 #ifdef DISABLE_MULR
1383 /* disable Multiple Reads for debugging */
1384 tctl &= ~E1000_TCTL_MULR;
1385 #endif
1386
1387 if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572) {
1388 tarc = E1000_READ_REG(hw, TARC0);
1389 tarc |= ((1 << 25) | (1 << 21));
1390 E1000_WRITE_REG(hw, TARC0, tarc);
1391 tarc = E1000_READ_REG(hw, TARC1);
1392 tarc |= (1 << 25);
1393 if (tctl & E1000_TCTL_MULR)
1394 tarc &= ~(1 << 28);
1395 else
1396 tarc |= (1 << 28);
1397 E1000_WRITE_REG(hw, TARC1, tarc);
1398 } else if (hw->mac_type == e1000_80003es2lan) {
1399 tarc = E1000_READ_REG(hw, TARC0);
1400 tarc |= 1;
1401 if (hw->media_type == e1000_media_type_internal_serdes)
1402 tarc |= (1 << 20);
1403 E1000_WRITE_REG(hw, TARC0, tarc);
1404 tarc = E1000_READ_REG(hw, TARC1);
1405 tarc |= 1;
1406 E1000_WRITE_REG(hw, TARC1, tarc);
1407 }
1408
1409 e1000_config_collision_dist(hw);
1410
1411 /* Setup Transmit Descriptor Settings for eop descriptor */
1412 adapter->txd_cmd = E1000_TXD_CMD_IDE | E1000_TXD_CMD_EOP |
1413 E1000_TXD_CMD_IFCS;
1414
1415 if (hw->mac_type < e1000_82543)
1416 adapter->txd_cmd |= E1000_TXD_CMD_RPS;
1417 else
1418 adapter->txd_cmd |= E1000_TXD_CMD_RS;
1419
1420 /* Cache if we're 82544 running in PCI-X because we'll
1421 * need this to apply a workaround later in the send path. */
1422 if (hw->mac_type == e1000_82544 &&
1423 hw->bus_type == e1000_bus_type_pcix)
1424 adapter->pcix_82544 = 1;
1425
1426 E1000_WRITE_REG(hw, TCTL, tctl);
1427
1428 }
1429
1430 /**
1431 * e1000_setup_rx_resources - allocate Rx resources (Descriptors)
1432 * @adapter: board private structure
1433 * @rxdr: rx descriptor ring (for a specific queue) to setup
1434 *
1435 * Returns 0 on success, negative on failure
1436 **/
1437
1438 static int
1439 e1000_setup_rx_resources(struct e1000_adapter *adapter,
1440 struct e1000_rx_ring *rxdr)
1441 {
1442 struct pci_dev *pdev = adapter->pdev;
1443 int size, desc_len;
1444
1445 size = sizeof(struct e1000_buffer) * rxdr->count;
1446 rxdr->buffer_info = vmalloc_node(size, pcibus_to_node(pdev->bus));
1447 if (!rxdr->buffer_info) {
1448 DPRINTK(PROBE, ERR,
1449 "Unable to allocate memory for the receive descriptor ring\n");
1450 return -ENOMEM;
1451 }
1452 memset(rxdr->buffer_info, 0, size);
1453
1454 size = sizeof(struct e1000_ps_page) * rxdr->count;
1455 rxdr->ps_page = kmalloc(size, GFP_KERNEL);
1456 if (!rxdr->ps_page) {
1457 vfree(rxdr->buffer_info);
1458 DPRINTK(PROBE, ERR,
1459 "Unable to allocate memory for the receive descriptor ring\n");
1460 return -ENOMEM;
1461 }
1462 memset(rxdr->ps_page, 0, size);
1463
1464 size = sizeof(struct e1000_ps_page_dma) * rxdr->count;
1465 rxdr->ps_page_dma = kmalloc(size, GFP_KERNEL);
1466 if (!rxdr->ps_page_dma) {
1467 vfree(rxdr->buffer_info);
1468 kfree(rxdr->ps_page);
1469 DPRINTK(PROBE, ERR,
1470 "Unable to allocate memory for the receive descriptor ring\n");
1471 return -ENOMEM;
1472 }
1473 memset(rxdr->ps_page_dma, 0, size);
1474
1475 if (adapter->hw.mac_type <= e1000_82547_rev_2)
1476 desc_len = sizeof(struct e1000_rx_desc);
1477 else
1478 desc_len = sizeof(union e1000_rx_desc_packet_split);
1479
1480 /* Round up to nearest 4K */
1481
1482 rxdr->size = rxdr->count * desc_len;
1483 E1000_ROUNDUP(rxdr->size, 4096);
1484
1485 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1486
1487 if (!rxdr->desc) {
1488 DPRINTK(PROBE, ERR,
1489 "Unable to allocate memory for the receive descriptor ring\n");
1490 setup_rx_desc_die:
1491 vfree(rxdr->buffer_info);
1492 kfree(rxdr->ps_page);
1493 kfree(rxdr->ps_page_dma);
1494 return -ENOMEM;
1495 }
1496
1497 /* Fix for errata 23, can't cross 64kB boundary */
1498 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1499 void *olddesc = rxdr->desc;
1500 dma_addr_t olddma = rxdr->dma;
1501 DPRINTK(RX_ERR, ERR, "rxdr align check failed: %u bytes "
1502 "at %p\n", rxdr->size, rxdr->desc);
1503 /* Try again, without freeing the previous */
1504 rxdr->desc = pci_alloc_consistent(pdev, rxdr->size, &rxdr->dma);
1505 /* Failed allocation, critical failure */
1506 if (!rxdr->desc) {
1507 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1508 DPRINTK(PROBE, ERR,
1509 "Unable to allocate memory "
1510 "for the receive descriptor ring\n");
1511 goto setup_rx_desc_die;
1512 }
1513
1514 if (!e1000_check_64k_bound(adapter, rxdr->desc, rxdr->size)) {
1515 /* give up */
1516 pci_free_consistent(pdev, rxdr->size, rxdr->desc,
1517 rxdr->dma);
1518 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1519 DPRINTK(PROBE, ERR,
1520 "Unable to allocate aligned memory "
1521 "for the receive descriptor ring\n");
1522 goto setup_rx_desc_die;
1523 } else {
1524 /* Free old allocation, new allocation was successful */
1525 pci_free_consistent(pdev, rxdr->size, olddesc, olddma);
1526 }
1527 }
1528 memset(rxdr->desc, 0, rxdr->size);
1529
1530 rxdr->next_to_clean = 0;
1531 rxdr->next_to_use = 0;
1532
1533 return 0;
1534 }
1535
1536 /**
1537 * e1000_setup_all_rx_resources - wrapper to allocate Rx resources
1538 * (Descriptors) for all queues
1539 * @adapter: board private structure
1540 *
1541 * If this function returns with an error, then it's possible one or
1542 * more of the rings is populated (while the rest are not). It is the
1543 * callers duty to clean those orphaned rings.
1544 *
1545 * Return 0 on success, negative on failure
1546 **/
1547
1548 int
1549 e1000_setup_all_rx_resources(struct e1000_adapter *adapter)
1550 {
1551 int i, err = 0;
1552
1553 for (i = 0; i < adapter->num_rx_queues; i++) {
1554 err = e1000_setup_rx_resources(adapter, &adapter->rx_ring[i]);
1555 if (err) {
1556 DPRINTK(PROBE, ERR,
1557 "Allocation for Rx Queue %u failed\n", i);
1558 break;
1559 }
1560 }
1561
1562 return err;
1563 }
1564
1565 /**
1566 * e1000_setup_rctl - configure the receive control registers
1567 * @adapter: Board private structure
1568 **/
1569 #define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
1570 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
1571 static void
1572 e1000_setup_rctl(struct e1000_adapter *adapter)
1573 {
1574 uint32_t rctl, rfctl;
1575 uint32_t psrctl = 0;
1576 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
1577 uint32_t pages = 0;
1578 #endif
1579
1580 rctl = E1000_READ_REG(&adapter->hw, RCTL);
1581
1582 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
1583
1584 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
1585 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
1586 (adapter->hw.mc_filter_type << E1000_RCTL_MO_SHIFT);
1587
1588 if (adapter->hw.mac_type > e1000_82543)
1589 rctl |= E1000_RCTL_SECRC;
1590
1591 if (adapter->hw.tbi_compatibility_on == 1)
1592 rctl |= E1000_RCTL_SBP;
1593 else
1594 rctl &= ~E1000_RCTL_SBP;
1595
1596 if (adapter->netdev->mtu <= ETH_DATA_LEN)
1597 rctl &= ~E1000_RCTL_LPE;
1598 else
1599 rctl |= E1000_RCTL_LPE;
1600
1601 /* Setup buffer sizes */
1602 if (adapter->hw.mac_type >= e1000_82571) {
1603 /* We can now specify buffers in 1K increments.
1604 * BSIZE and BSEX are ignored in this case. */
1605 rctl |= adapter->rx_buffer_len << 0x11;
1606 } else {
1607 rctl &= ~E1000_RCTL_SZ_4096;
1608 rctl |= E1000_RCTL_BSEX;
1609 switch (adapter->rx_buffer_len) {
1610 case E1000_RXBUFFER_2048:
1611 default:
1612 rctl |= E1000_RCTL_SZ_2048;
1613 rctl &= ~E1000_RCTL_BSEX;
1614 break;
1615 case E1000_RXBUFFER_4096:
1616 rctl |= E1000_RCTL_SZ_4096;
1617 break;
1618 case E1000_RXBUFFER_8192:
1619 rctl |= E1000_RCTL_SZ_8192;
1620 break;
1621 case E1000_RXBUFFER_16384:
1622 rctl |= E1000_RCTL_SZ_16384;
1623 break;
1624 }
1625 }
1626
1627 #ifndef CONFIG_E1000_DISABLE_PACKET_SPLIT
1628 /* 82571 and greater support packet-split where the protocol
1629 * header is placed in skb->data and the packet data is
1630 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
1631 * In the case of a non-split, skb->data is linearly filled,
1632 * followed by the page buffers. Therefore, skb->data is
1633 * sized to hold the largest protocol header.
1634 */
1635 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
1636 if ((adapter->hw.mac_type > e1000_82547_rev_2) && (pages <= 3) &&
1637 PAGE_SIZE <= 16384)
1638 adapter->rx_ps_pages = pages;
1639 else
1640 adapter->rx_ps_pages = 0;
1641 #endif
1642 if (adapter->rx_ps_pages) {
1643 /* Configure extra packet-split registers */
1644 rfctl = E1000_READ_REG(&adapter->hw, RFCTL);
1645 rfctl |= E1000_RFCTL_EXTEN;
1646 /* disable IPv6 packet split support */
1647 rfctl |= E1000_RFCTL_IPV6_DIS;
1648 E1000_WRITE_REG(&adapter->hw, RFCTL, rfctl);
1649
1650 rctl |= E1000_RCTL_DTYP_PS | E1000_RCTL_SECRC;
1651
1652 psrctl |= adapter->rx_ps_bsize0 >>
1653 E1000_PSRCTL_BSIZE0_SHIFT;
1654
1655 switch (adapter->rx_ps_pages) {
1656 case 3:
1657 psrctl |= PAGE_SIZE <<
1658 E1000_PSRCTL_BSIZE3_SHIFT;
1659 case 2:
1660 psrctl |= PAGE_SIZE <<
1661 E1000_PSRCTL_BSIZE2_SHIFT;
1662 case 1:
1663 psrctl |= PAGE_SIZE >>
1664 E1000_PSRCTL_BSIZE1_SHIFT;
1665 break;
1666 }
1667
1668 E1000_WRITE_REG(&adapter->hw, PSRCTL, psrctl);
1669 }
1670
1671 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
1672 }
1673
1674 /**
1675 * e1000_configure_rx - Configure 8254x Receive Unit after Reset
1676 * @adapter: board private structure
1677 *
1678 * Configure the Rx unit of the MAC after a reset.
1679 **/
1680
1681 static void
1682 e1000_configure_rx(struct e1000_adapter *adapter)
1683 {
1684 uint64_t rdba;
1685 struct e1000_hw *hw = &adapter->hw;
1686 uint32_t rdlen, rctl, rxcsum, ctrl_ext;
1687
1688 if (adapter->rx_ps_pages) {
1689 /* this is a 32 byte descriptor */
1690 rdlen = adapter->rx_ring[0].count *
1691 sizeof(union e1000_rx_desc_packet_split);
1692 adapter->clean_rx = e1000_clean_rx_irq_ps;
1693 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
1694 } else {
1695 rdlen = adapter->rx_ring[0].count *
1696 sizeof(struct e1000_rx_desc);
1697 adapter->clean_rx = e1000_clean_rx_irq;
1698 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
1699 }
1700
1701 /* disable receives while setting up the descriptors */
1702 rctl = E1000_READ_REG(hw, RCTL);
1703 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
1704
1705 /* set the Receive Delay Timer Register */
1706 E1000_WRITE_REG(hw, RDTR, adapter->rx_int_delay);
1707
1708 if (hw->mac_type >= e1000_82540) {
1709 E1000_WRITE_REG(hw, RADV, adapter->rx_abs_int_delay);
1710 if (adapter->itr > 1)
1711 E1000_WRITE_REG(hw, ITR,
1712 1000000000 / (adapter->itr * 256));
1713 }
1714
1715 if (hw->mac_type >= e1000_82571) {
1716 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
1717 /* Reset delay timers after every interrupt */
1718 ctrl_ext |= E1000_CTRL_EXT_CANC;
1719 #ifdef CONFIG_E1000_NAPI
1720 /* Auto-Mask interrupts upon ICR read. */
1721 ctrl_ext |= E1000_CTRL_EXT_IAME;
1722 #endif
1723 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1724 E1000_WRITE_REG(hw, IAM, ~0);
1725 E1000_WRITE_FLUSH(hw);
1726 }
1727
1728 /* Setup the HW Rx Head and Tail Descriptor Pointers and
1729 * the Base and Length of the Rx Descriptor Ring */
1730 switch (adapter->num_rx_queues) {
1731 case 1:
1732 default:
1733 rdba = adapter->rx_ring[0].dma;
1734 E1000_WRITE_REG(hw, RDBAL, (rdba & 0x00000000ffffffffULL));
1735 E1000_WRITE_REG(hw, RDBAH, (rdba >> 32));
1736 E1000_WRITE_REG(hw, RDLEN, rdlen);
1737 E1000_WRITE_REG(hw, RDH, 0);
1738 E1000_WRITE_REG(hw, RDT, 0);
1739 adapter->rx_ring[0].rdh = E1000_RDH;
1740 adapter->rx_ring[0].rdt = E1000_RDT;
1741 break;
1742 }
1743
1744 /* Enable 82543 Receive Checksum Offload for TCP and UDP */
1745 if (hw->mac_type >= e1000_82543) {
1746 rxcsum = E1000_READ_REG(hw, RXCSUM);
1747 if (adapter->rx_csum == TRUE) {
1748 rxcsum |= E1000_RXCSUM_TUOFL;
1749
1750 /* Enable 82571 IPv4 payload checksum for UDP fragments
1751 * Must be used in conjunction with packet-split. */
1752 if ((hw->mac_type >= e1000_82571) &&
1753 (adapter->rx_ps_pages)) {
1754 rxcsum |= E1000_RXCSUM_IPPCSE;
1755 }
1756 } else {
1757 rxcsum &= ~E1000_RXCSUM_TUOFL;
1758 /* don't need to clear IPPCSE as it defaults to 0 */
1759 }
1760 E1000_WRITE_REG(hw, RXCSUM, rxcsum);
1761 }
1762
1763 if (hw->mac_type == e1000_82573)
1764 E1000_WRITE_REG(hw, ERT, 0x0100);
1765
1766 /* Enable Receives */
1767 E1000_WRITE_REG(hw, RCTL, rctl);
1768 }
1769
1770 /**
1771 * e1000_free_tx_resources - Free Tx Resources per Queue
1772 * @adapter: board private structure
1773 * @tx_ring: Tx descriptor ring for a specific queue
1774 *
1775 * Free all transmit software resources
1776 **/
1777
1778 static void
1779 e1000_free_tx_resources(struct e1000_adapter *adapter,
1780 struct e1000_tx_ring *tx_ring)
1781 {
1782 struct pci_dev *pdev = adapter->pdev;
1783
1784 e1000_clean_tx_ring(adapter, tx_ring);
1785
1786 vfree(tx_ring->buffer_info);
1787 tx_ring->buffer_info = NULL;
1788
1789 pci_free_consistent(pdev, tx_ring->size, tx_ring->desc, tx_ring->dma);
1790
1791 tx_ring->desc = NULL;
1792 }
1793
1794 /**
1795 * e1000_free_all_tx_resources - Free Tx Resources for All Queues
1796 * @adapter: board private structure
1797 *
1798 * Free all transmit software resources
1799 **/
1800
1801 void
1802 e1000_free_all_tx_resources(struct e1000_adapter *adapter)
1803 {
1804 int i;
1805
1806 for (i = 0; i < adapter->num_tx_queues; i++)
1807 e1000_free_tx_resources(adapter, &adapter->tx_ring[i]);
1808 }
1809
1810 static inline void
1811 e1000_unmap_and_free_tx_resource(struct e1000_adapter *adapter,
1812 struct e1000_buffer *buffer_info)
1813 {
1814 if (buffer_info->dma) {
1815 pci_unmap_page(adapter->pdev,
1816 buffer_info->dma,
1817 buffer_info->length,
1818 PCI_DMA_TODEVICE);
1819 }
1820 if (buffer_info->skb)
1821 dev_kfree_skb_any(buffer_info->skb);
1822 memset(buffer_info, 0, sizeof(struct e1000_buffer));
1823 }
1824
1825 /**
1826 * e1000_clean_tx_ring - Free Tx Buffers
1827 * @adapter: board private structure
1828 * @tx_ring: ring to be cleaned
1829 **/
1830
1831 static void
1832 e1000_clean_tx_ring(struct e1000_adapter *adapter,
1833 struct e1000_tx_ring *tx_ring)
1834 {
1835 struct e1000_buffer *buffer_info;
1836 unsigned long size;
1837 unsigned int i;
1838
1839 /* Free all the Tx ring sk_buffs */
1840
1841 for (i = 0; i < tx_ring->count; i++) {
1842 buffer_info = &tx_ring->buffer_info[i];
1843 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
1844 }
1845
1846 size = sizeof(struct e1000_buffer) * tx_ring->count;
1847 memset(tx_ring->buffer_info, 0, size);
1848
1849 /* Zero out the descriptor ring */
1850
1851 memset(tx_ring->desc, 0, tx_ring->size);
1852
1853 tx_ring->next_to_use = 0;
1854 tx_ring->next_to_clean = 0;
1855 tx_ring->last_tx_tso = 0;
1856
1857 writel(0, adapter->hw.hw_addr + tx_ring->tdh);
1858 writel(0, adapter->hw.hw_addr + tx_ring->tdt);
1859 }
1860
1861 /**
1862 * e1000_clean_all_tx_rings - Free Tx Buffers for all queues
1863 * @adapter: board private structure
1864 **/
1865
1866 static void
1867 e1000_clean_all_tx_rings(struct e1000_adapter *adapter)
1868 {
1869 int i;
1870
1871 for (i = 0; i < adapter->num_tx_queues; i++)
1872 e1000_clean_tx_ring(adapter, &adapter->tx_ring[i]);
1873 }
1874
1875 /**
1876 * e1000_free_rx_resources - Free Rx Resources
1877 * @adapter: board private structure
1878 * @rx_ring: ring to clean the resources from
1879 *
1880 * Free all receive software resources
1881 **/
1882
1883 static void
1884 e1000_free_rx_resources(struct e1000_adapter *adapter,
1885 struct e1000_rx_ring *rx_ring)
1886 {
1887 struct pci_dev *pdev = adapter->pdev;
1888
1889 e1000_clean_rx_ring(adapter, rx_ring);
1890
1891 vfree(rx_ring->buffer_info);
1892 rx_ring->buffer_info = NULL;
1893 kfree(rx_ring->ps_page);
1894 rx_ring->ps_page = NULL;
1895 kfree(rx_ring->ps_page_dma);
1896 rx_ring->ps_page_dma = NULL;
1897
1898 pci_free_consistent(pdev, rx_ring->size, rx_ring->desc, rx_ring->dma);
1899
1900 rx_ring->desc = NULL;
1901 }
1902
1903 /**
1904 * e1000_free_all_rx_resources - Free Rx Resources for All Queues
1905 * @adapter: board private structure
1906 *
1907 * Free all receive software resources
1908 **/
1909
1910 void
1911 e1000_free_all_rx_resources(struct e1000_adapter *adapter)
1912 {
1913 int i;
1914
1915 for (i = 0; i < adapter->num_rx_queues; i++)
1916 e1000_free_rx_resources(adapter, &adapter->rx_ring[i]);
1917 }
1918
1919 /**
1920 * e1000_clean_rx_ring - Free Rx Buffers per Queue
1921 * @adapter: board private structure
1922 * @rx_ring: ring to free buffers from
1923 **/
1924
1925 static void
1926 e1000_clean_rx_ring(struct e1000_adapter *adapter,
1927 struct e1000_rx_ring *rx_ring)
1928 {
1929 struct e1000_buffer *buffer_info;
1930 struct e1000_ps_page *ps_page;
1931 struct e1000_ps_page_dma *ps_page_dma;
1932 struct pci_dev *pdev = adapter->pdev;
1933 unsigned long size;
1934 unsigned int i, j;
1935
1936 /* Free all the Rx ring sk_buffs */
1937 for (i = 0; i < rx_ring->count; i++) {
1938 buffer_info = &rx_ring->buffer_info[i];
1939 if (buffer_info->skb) {
1940 pci_unmap_single(pdev,
1941 buffer_info->dma,
1942 buffer_info->length,
1943 PCI_DMA_FROMDEVICE);
1944
1945 dev_kfree_skb(buffer_info->skb);
1946 buffer_info->skb = NULL;
1947 }
1948 ps_page = &rx_ring->ps_page[i];
1949 ps_page_dma = &rx_ring->ps_page_dma[i];
1950 for (j = 0; j < adapter->rx_ps_pages; j++) {
1951 if (!ps_page->ps_page[j]) break;
1952 pci_unmap_page(pdev,
1953 ps_page_dma->ps_page_dma[j],
1954 PAGE_SIZE, PCI_DMA_FROMDEVICE);
1955 ps_page_dma->ps_page_dma[j] = 0;
1956 put_page(ps_page->ps_page[j]);
1957 ps_page->ps_page[j] = NULL;
1958 }
1959 }
1960
1961 size = sizeof(struct e1000_buffer) * rx_ring->count;
1962 memset(rx_ring->buffer_info, 0, size);
1963 size = sizeof(struct e1000_ps_page) * rx_ring->count;
1964 memset(rx_ring->ps_page, 0, size);
1965 size = sizeof(struct e1000_ps_page_dma) * rx_ring->count;
1966 memset(rx_ring->ps_page_dma, 0, size);
1967
1968 /* Zero out the descriptor ring */
1969
1970 memset(rx_ring->desc, 0, rx_ring->size);
1971
1972 rx_ring->next_to_clean = 0;
1973 rx_ring->next_to_use = 0;
1974
1975 writel(0, adapter->hw.hw_addr + rx_ring->rdh);
1976 writel(0, adapter->hw.hw_addr + rx_ring->rdt);
1977 }
1978
1979 /**
1980 * e1000_clean_all_rx_rings - Free Rx Buffers for all queues
1981 * @adapter: board private structure
1982 **/
1983
1984 static void
1985 e1000_clean_all_rx_rings(struct e1000_adapter *adapter)
1986 {
1987 int i;
1988
1989 for (i = 0; i < adapter->num_rx_queues; i++)
1990 e1000_clean_rx_ring(adapter, &adapter->rx_ring[i]);
1991 }
1992
1993 /* The 82542 2.0 (revision 2) needs to have the receive unit in reset
1994 * and memory write and invalidate disabled for certain operations
1995 */
1996 static void
1997 e1000_enter_82542_rst(struct e1000_adapter *adapter)
1998 {
1999 struct net_device *netdev = adapter->netdev;
2000 uint32_t rctl;
2001
2002 e1000_pci_clear_mwi(&adapter->hw);
2003
2004 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2005 rctl |= E1000_RCTL_RST;
2006 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2007 E1000_WRITE_FLUSH(&adapter->hw);
2008 mdelay(5);
2009
2010 if (netif_running(netdev))
2011 e1000_clean_all_rx_rings(adapter);
2012 }
2013
2014 static void
2015 e1000_leave_82542_rst(struct e1000_adapter *adapter)
2016 {
2017 struct net_device *netdev = adapter->netdev;
2018 uint32_t rctl;
2019
2020 rctl = E1000_READ_REG(&adapter->hw, RCTL);
2021 rctl &= ~E1000_RCTL_RST;
2022 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
2023 E1000_WRITE_FLUSH(&adapter->hw);
2024 mdelay(5);
2025
2026 if (adapter->hw.pci_cmd_word & PCI_COMMAND_INVALIDATE)
2027 e1000_pci_set_mwi(&adapter->hw);
2028
2029 if (netif_running(netdev)) {
2030 /* No need to loop, because 82542 supports only 1 queue */
2031 struct e1000_rx_ring *ring = &adapter->rx_ring[0];
2032 e1000_configure_rx(adapter);
2033 adapter->alloc_rx_buf(adapter, ring, E1000_DESC_UNUSED(ring));
2034 }
2035 }
2036
2037 /**
2038 * e1000_set_mac - Change the Ethernet Address of the NIC
2039 * @netdev: network interface device structure
2040 * @p: pointer to an address structure
2041 *
2042 * Returns 0 on success, negative on failure
2043 **/
2044
2045 static int
2046 e1000_set_mac(struct net_device *netdev, void *p)
2047 {
2048 struct e1000_adapter *adapter = netdev_priv(netdev);
2049 struct sockaddr *addr = p;
2050
2051 if (!is_valid_ether_addr(addr->sa_data))
2052 return -EADDRNOTAVAIL;
2053
2054 /* 82542 2.0 needs to be in reset to write receive address registers */
2055
2056 if (adapter->hw.mac_type == e1000_82542_rev2_0)
2057 e1000_enter_82542_rst(adapter);
2058
2059 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
2060 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
2061
2062 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2063
2064 /* With 82571 controllers, LAA may be overwritten (with the default)
2065 * due to controller reset from the other port. */
2066 if (adapter->hw.mac_type == e1000_82571) {
2067 /* activate the work around */
2068 adapter->hw.laa_is_present = 1;
2069
2070 /* Hold a copy of the LAA in RAR[14] This is done so that
2071 * between the time RAR[0] gets clobbered and the time it
2072 * gets fixed (in e1000_watchdog), the actual LAA is in one
2073 * of the RARs and no incoming packets directed to this port
2074 * are dropped. Eventaully the LAA will be in RAR[0] and
2075 * RAR[14] */
2076 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr,
2077 E1000_RAR_ENTRIES - 1);
2078 }
2079
2080 if (adapter->hw.mac_type == e1000_82542_rev2_0)
2081 e1000_leave_82542_rst(adapter);
2082
2083 return 0;
2084 }
2085
2086 /**
2087 * e1000_set_multi - Multicast and Promiscuous mode set
2088 * @netdev: network interface device structure
2089 *
2090 * The set_multi entry point is called whenever the multicast address
2091 * list or the network interface flags are updated. This routine is
2092 * responsible for configuring the hardware for proper multicast,
2093 * promiscuous mode, and all-multi behavior.
2094 **/
2095
2096 static void
2097 e1000_set_multi(struct net_device *netdev)
2098 {
2099 struct e1000_adapter *adapter = netdev_priv(netdev);
2100 struct e1000_hw *hw = &adapter->hw;
2101 struct dev_mc_list *mc_ptr;
2102 uint32_t rctl;
2103 uint32_t hash_value;
2104 int i, rar_entries = E1000_RAR_ENTRIES;
2105
2106 /* reserve RAR[14] for LAA over-write work-around */
2107 if (adapter->hw.mac_type == e1000_82571)
2108 rar_entries--;
2109
2110 /* Check for Promiscuous and All Multicast modes */
2111
2112 rctl = E1000_READ_REG(hw, RCTL);
2113
2114 if (netdev->flags & IFF_PROMISC) {
2115 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
2116 } else if (netdev->flags & IFF_ALLMULTI) {
2117 rctl |= E1000_RCTL_MPE;
2118 rctl &= ~E1000_RCTL_UPE;
2119 } else {
2120 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
2121 }
2122
2123 E1000_WRITE_REG(hw, RCTL, rctl);
2124
2125 /* 82542 2.0 needs to be in reset to write receive address registers */
2126
2127 if (hw->mac_type == e1000_82542_rev2_0)
2128 e1000_enter_82542_rst(adapter);
2129
2130 /* load the first 14 multicast address into the exact filters 1-14
2131 * RAR 0 is used for the station MAC adddress
2132 * if there are not 14 addresses, go ahead and clear the filters
2133 * -- with 82571 controllers only 0-13 entries are filled here
2134 */
2135 mc_ptr = netdev->mc_list;
2136
2137 for (i = 1; i < rar_entries; i++) {
2138 if (mc_ptr) {
2139 e1000_rar_set(hw, mc_ptr->dmi_addr, i);
2140 mc_ptr = mc_ptr->next;
2141 } else {
2142 E1000_WRITE_REG_ARRAY(hw, RA, i << 1, 0);
2143 E1000_WRITE_REG_ARRAY(hw, RA, (i << 1) + 1, 0);
2144 }
2145 }
2146
2147 /* clear the old settings from the multicast hash table */
2148
2149 for (i = 0; i < E1000_NUM_MTA_REGISTERS; i++)
2150 E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
2151
2152 /* load any remaining addresses into the hash table */
2153
2154 for (; mc_ptr; mc_ptr = mc_ptr->next) {
2155 hash_value = e1000_hash_mc_addr(hw, mc_ptr->dmi_addr);
2156 e1000_mta_set(hw, hash_value);
2157 }
2158
2159 if (hw->mac_type == e1000_82542_rev2_0)
2160 e1000_leave_82542_rst(adapter);
2161 }
2162
2163 /* Need to wait a few seconds after link up to get diagnostic information from
2164 * the phy */
2165
2166 static void
2167 e1000_update_phy_info(unsigned long data)
2168 {
2169 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2170 e1000_phy_get_info(&adapter->hw, &adapter->phy_info);
2171 }
2172
2173 /**
2174 * e1000_82547_tx_fifo_stall - Timer Call-back
2175 * @data: pointer to adapter cast into an unsigned long
2176 **/
2177
2178 static void
2179 e1000_82547_tx_fifo_stall(unsigned long data)
2180 {
2181 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2182 struct net_device *netdev = adapter->netdev;
2183 uint32_t tctl;
2184
2185 if (atomic_read(&adapter->tx_fifo_stall)) {
2186 if ((E1000_READ_REG(&adapter->hw, TDT) ==
2187 E1000_READ_REG(&adapter->hw, TDH)) &&
2188 (E1000_READ_REG(&adapter->hw, TDFT) ==
2189 E1000_READ_REG(&adapter->hw, TDFH)) &&
2190 (E1000_READ_REG(&adapter->hw, TDFTS) ==
2191 E1000_READ_REG(&adapter->hw, TDFHS))) {
2192 tctl = E1000_READ_REG(&adapter->hw, TCTL);
2193 E1000_WRITE_REG(&adapter->hw, TCTL,
2194 tctl & ~E1000_TCTL_EN);
2195 E1000_WRITE_REG(&adapter->hw, TDFT,
2196 adapter->tx_head_addr);
2197 E1000_WRITE_REG(&adapter->hw, TDFH,
2198 adapter->tx_head_addr);
2199 E1000_WRITE_REG(&adapter->hw, TDFTS,
2200 adapter->tx_head_addr);
2201 E1000_WRITE_REG(&adapter->hw, TDFHS,
2202 adapter->tx_head_addr);
2203 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2204 E1000_WRITE_FLUSH(&adapter->hw);
2205
2206 adapter->tx_fifo_head = 0;
2207 atomic_set(&adapter->tx_fifo_stall, 0);
2208 netif_wake_queue(netdev);
2209 } else {
2210 mod_timer(&adapter->tx_fifo_stall_timer, jiffies + 1);
2211 }
2212 }
2213 }
2214
2215 /**
2216 * e1000_watchdog - Timer Call-back
2217 * @data: pointer to adapter cast into an unsigned long
2218 **/
2219 static void
2220 e1000_watchdog(unsigned long data)
2221 {
2222 struct e1000_adapter *adapter = (struct e1000_adapter *) data;
2223
2224 /* Do the rest outside of interrupt context */
2225 schedule_work(&adapter->watchdog_task);
2226 }
2227
2228 static void
2229 e1000_watchdog_task(struct e1000_adapter *adapter)
2230 {
2231 struct net_device *netdev = adapter->netdev;
2232 struct e1000_tx_ring *txdr = adapter->tx_ring;
2233 uint32_t link, tctl;
2234
2235 e1000_check_for_link(&adapter->hw);
2236 if (adapter->hw.mac_type == e1000_82573) {
2237 e1000_enable_tx_pkt_filtering(&adapter->hw);
2238 if (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id)
2239 e1000_update_mng_vlan(adapter);
2240 }
2241
2242 if ((adapter->hw.media_type == e1000_media_type_internal_serdes) &&
2243 !(E1000_READ_REG(&adapter->hw, TXCW) & E1000_TXCW_ANE))
2244 link = !adapter->hw.serdes_link_down;
2245 else
2246 link = E1000_READ_REG(&adapter->hw, STATUS) & E1000_STATUS_LU;
2247
2248 if (link) {
2249 if (!netif_carrier_ok(netdev)) {
2250 e1000_get_speed_and_duplex(&adapter->hw,
2251 &adapter->link_speed,
2252 &adapter->link_duplex);
2253
2254 DPRINTK(LINK, INFO, "NIC Link is Up %d Mbps %s\n",
2255 adapter->link_speed,
2256 adapter->link_duplex == FULL_DUPLEX ?
2257 "Full Duplex" : "Half Duplex");
2258
2259 /* tweak tx_queue_len according to speed/duplex
2260 * and adjust the timeout factor */
2261 netdev->tx_queue_len = adapter->tx_queue_len;
2262 adapter->tx_timeout_factor = 1;
2263 adapter->txb2b = 1;
2264 switch (adapter->link_speed) {
2265 case SPEED_10:
2266 adapter->txb2b = 0;
2267 netdev->tx_queue_len = 10;
2268 adapter->tx_timeout_factor = 8;
2269 break;
2270 case SPEED_100:
2271 adapter->txb2b = 0;
2272 netdev->tx_queue_len = 100;
2273 /* maybe add some timeout factor ? */
2274 break;
2275 }
2276
2277 if ((adapter->hw.mac_type == e1000_82571 ||
2278 adapter->hw.mac_type == e1000_82572) &&
2279 adapter->txb2b == 0) {
2280 #define SPEED_MODE_BIT (1 << 21)
2281 uint32_t tarc0;
2282 tarc0 = E1000_READ_REG(&adapter->hw, TARC0);
2283 tarc0 &= ~SPEED_MODE_BIT;
2284 E1000_WRITE_REG(&adapter->hw, TARC0, tarc0);
2285 }
2286
2287 #ifdef NETIF_F_TSO
2288 /* disable TSO for pcie and 10/100 speeds, to avoid
2289 * some hardware issues */
2290 if (!adapter->tso_force &&
2291 adapter->hw.bus_type == e1000_bus_type_pci_express){
2292 switch (adapter->link_speed) {
2293 case SPEED_10:
2294 case SPEED_100:
2295 DPRINTK(PROBE,INFO,
2296 "10/100 speed: disabling TSO\n");
2297 netdev->features &= ~NETIF_F_TSO;
2298 break;
2299 case SPEED_1000:
2300 netdev->features |= NETIF_F_TSO;
2301 break;
2302 default:
2303 /* oops */
2304 break;
2305 }
2306 }
2307 #endif
2308
2309 /* enable transmits in the hardware, need to do this
2310 * after setting TARC0 */
2311 tctl = E1000_READ_REG(&adapter->hw, TCTL);
2312 tctl |= E1000_TCTL_EN;
2313 E1000_WRITE_REG(&adapter->hw, TCTL, tctl);
2314
2315 netif_carrier_on(netdev);
2316 netif_wake_queue(netdev);
2317 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2318 adapter->smartspeed = 0;
2319 }
2320 } else {
2321 if (netif_carrier_ok(netdev)) {
2322 adapter->link_speed = 0;
2323 adapter->link_duplex = 0;
2324 DPRINTK(LINK, INFO, "NIC Link is Down\n");
2325 netif_carrier_off(netdev);
2326 netif_stop_queue(netdev);
2327 mod_timer(&adapter->phy_info_timer, jiffies + 2 * HZ);
2328
2329 /* 80003ES2LAN workaround--
2330 * For packet buffer work-around on link down event;
2331 * disable receives in the ISR and
2332 * reset device here in the watchdog
2333 */
2334 if (adapter->hw.mac_type == e1000_80003es2lan) {
2335 /* reset device */
2336 schedule_work(&adapter->reset_task);
2337 }
2338 }
2339
2340 e1000_smartspeed(adapter);
2341 }
2342
2343 e1000_update_stats(adapter);
2344
2345 adapter->hw.tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
2346 adapter->tpt_old = adapter->stats.tpt;
2347 adapter->hw.collision_delta = adapter->stats.colc - adapter->colc_old;
2348 adapter->colc_old = adapter->stats.colc;
2349
2350 adapter->gorcl = adapter->stats.gorcl - adapter->gorcl_old;
2351 adapter->gorcl_old = adapter->stats.gorcl;
2352 adapter->gotcl = adapter->stats.gotcl - adapter->gotcl_old;
2353 adapter->gotcl_old = adapter->stats.gotcl;
2354
2355 e1000_update_adaptive(&adapter->hw);
2356
2357 if (!netif_carrier_ok(netdev)) {
2358 if (E1000_DESC_UNUSED(txdr) + 1 < txdr->count) {
2359 /* We've lost link, so the controller stops DMA,
2360 * but we've got queued Tx work that's never going
2361 * to get done, so reset controller to flush Tx.
2362 * (Do the reset outside of interrupt context). */
2363 adapter->tx_timeout_count++;
2364 schedule_work(&adapter->reset_task);
2365 }
2366 }
2367
2368 /* Dynamic mode for Interrupt Throttle Rate (ITR) */
2369 if (adapter->hw.mac_type >= e1000_82540 && adapter->itr == 1) {
2370 /* Symmetric Tx/Rx gets a reduced ITR=2000; Total
2371 * asymmetrical Tx or Rx gets ITR=8000; everyone
2372 * else is between 2000-8000. */
2373 uint32_t goc = (adapter->gotcl + adapter->gorcl) / 10000;
2374 uint32_t dif = (adapter->gotcl > adapter->gorcl ?
2375 adapter->gotcl - adapter->gorcl :
2376 adapter->gorcl - adapter->gotcl) / 10000;
2377 uint32_t itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
2378 E1000_WRITE_REG(&adapter->hw, ITR, 1000000000 / (itr * 256));
2379 }
2380
2381 /* Cause software interrupt to ensure rx ring is cleaned */
2382 E1000_WRITE_REG(&adapter->hw, ICS, E1000_ICS_RXDMT0);
2383
2384 /* Force detection of hung controller every watchdog period */
2385 adapter->detect_tx_hung = TRUE;
2386
2387 /* With 82571 controllers, LAA may be overwritten due to controller
2388 * reset from the other port. Set the appropriate LAA in RAR[0] */
2389 if (adapter->hw.mac_type == e1000_82571 && adapter->hw.laa_is_present)
2390 e1000_rar_set(&adapter->hw, adapter->hw.mac_addr, 0);
2391
2392 /* Reset the timer */
2393 mod_timer(&adapter->watchdog_timer, jiffies + 2 * HZ);
2394 }
2395
2396 #define E1000_TX_FLAGS_CSUM 0x00000001
2397 #define E1000_TX_FLAGS_VLAN 0x00000002
2398 #define E1000_TX_FLAGS_TSO 0x00000004
2399 #define E1000_TX_FLAGS_IPV4 0x00000008
2400 #define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
2401 #define E1000_TX_FLAGS_VLAN_SHIFT 16
2402
2403 static inline int
2404 e1000_tso(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2405 struct sk_buff *skb)
2406 {
2407 #ifdef NETIF_F_TSO
2408 struct e1000_context_desc *context_desc;
2409 struct e1000_buffer *buffer_info;
2410 unsigned int i;
2411 uint32_t cmd_length = 0;
2412 uint16_t ipcse = 0, tucse, mss;
2413 uint8_t ipcss, ipcso, tucss, tucso, hdr_len;
2414 int err;
2415
2416 if (skb_shinfo(skb)->tso_size) {
2417 if (skb_header_cloned(skb)) {
2418 err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2419 if (err)
2420 return err;
2421 }
2422
2423 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2424 mss = skb_shinfo(skb)->tso_size;
2425 if (skb->protocol == ntohs(ETH_P_IP)) {
2426 skb->nh.iph->tot_len = 0;
2427 skb->nh.iph->check = 0;
2428 skb->h.th->check =
2429 ~csum_tcpudp_magic(skb->nh.iph->saddr,
2430 skb->nh.iph->daddr,
2431 0,
2432 IPPROTO_TCP,
2433 0);
2434 cmd_length = E1000_TXD_CMD_IP;
2435 ipcse = skb->h.raw - skb->data - 1;
2436 #ifdef NETIF_F_TSO_IPV6
2437 } else if (skb->protocol == ntohs(ETH_P_IPV6)) {
2438 skb->nh.ipv6h->payload_len = 0;
2439 skb->h.th->check =
2440 ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
2441 &skb->nh.ipv6h->daddr,
2442 0,
2443 IPPROTO_TCP,
2444 0);
2445 ipcse = 0;
2446 #endif
2447 }
2448 ipcss = skb->nh.raw - skb->data;
2449 ipcso = (void *)&(skb->nh.iph->check) - (void *)skb->data;
2450 tucss = skb->h.raw - skb->data;
2451 tucso = (void *)&(skb->h.th->check) - (void *)skb->data;
2452 tucse = 0;
2453
2454 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
2455 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
2456
2457 i = tx_ring->next_to_use;
2458 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2459 buffer_info = &tx_ring->buffer_info[i];
2460
2461 context_desc->lower_setup.ip_fields.ipcss = ipcss;
2462 context_desc->lower_setup.ip_fields.ipcso = ipcso;
2463 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
2464 context_desc->upper_setup.tcp_fields.tucss = tucss;
2465 context_desc->upper_setup.tcp_fields.tucso = tucso;
2466 context_desc->upper_setup.tcp_fields.tucse = cpu_to_le16(tucse);
2467 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
2468 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
2469 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
2470
2471 buffer_info->time_stamp = jiffies;
2472
2473 if (++i == tx_ring->count) i = 0;
2474 tx_ring->next_to_use = i;
2475
2476 return TRUE;
2477 }
2478 #endif
2479
2480 return FALSE;
2481 }
2482
2483 static inline boolean_t
2484 e1000_tx_csum(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2485 struct sk_buff *skb)
2486 {
2487 struct e1000_context_desc *context_desc;
2488 struct e1000_buffer *buffer_info;
2489 unsigned int i;
2490 uint8_t css;
2491
2492 if (likely(skb->ip_summed == CHECKSUM_HW)) {
2493 css = skb->h.raw - skb->data;
2494
2495 i = tx_ring->next_to_use;
2496 buffer_info = &tx_ring->buffer_info[i];
2497 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
2498
2499 context_desc->upper_setup.tcp_fields.tucss = css;
2500 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum;
2501 context_desc->upper_setup.tcp_fields.tucse = 0;
2502 context_desc->tcp_seg_setup.data = 0;
2503 context_desc->cmd_and_length = cpu_to_le32(E1000_TXD_CMD_DEXT);
2504
2505 buffer_info->time_stamp = jiffies;
2506
2507 if (unlikely(++i == tx_ring->count)) i = 0;
2508 tx_ring->next_to_use = i;
2509
2510 return TRUE;
2511 }
2512
2513 return FALSE;
2514 }
2515
2516 #define E1000_MAX_TXD_PWR 12
2517 #define E1000_MAX_DATA_PER_TXD (1<<E1000_MAX_TXD_PWR)
2518
2519 static inline int
2520 e1000_tx_map(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2521 struct sk_buff *skb, unsigned int first, unsigned int max_per_txd,
2522 unsigned int nr_frags, unsigned int mss)
2523 {
2524 struct e1000_buffer *buffer_info;
2525 unsigned int len = skb->len;
2526 unsigned int offset = 0, size, count = 0, i;
2527 unsigned int f;
2528 len -= skb->data_len;
2529
2530 i = tx_ring->next_to_use;
2531
2532 while (len) {
2533 buffer_info = &tx_ring->buffer_info[i];
2534 size = min(len, max_per_txd);
2535 #ifdef NETIF_F_TSO
2536 /* Workaround for Controller erratum --
2537 * descriptor for non-tso packet in a linear SKB that follows a
2538 * tso gets written back prematurely before the data is fully
2539 * DMA'd to the controller */
2540 if (!skb->data_len && tx_ring->last_tx_tso &&
2541 !skb_shinfo(skb)->tso_size) {
2542 tx_ring->last_tx_tso = 0;
2543 size -= 4;
2544 }
2545
2546 /* Workaround for premature desc write-backs
2547 * in TSO mode. Append 4-byte sentinel desc */
2548 if (unlikely(mss && !nr_frags && size == len && size > 8))
2549 size -= 4;
2550 #endif
2551 /* work-around for errata 10 and it applies
2552 * to all controllers in PCI-X mode
2553 * The fix is to make sure that the first descriptor of a
2554 * packet is smaller than 2048 - 16 - 16 (or 2016) bytes
2555 */
2556 if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
2557 (size > 2015) && count == 0))
2558 size = 2015;
2559
2560 /* Workaround for potential 82544 hang in PCI-X. Avoid
2561 * terminating buffers within evenly-aligned dwords. */
2562 if (unlikely(adapter->pcix_82544 &&
2563 !((unsigned long)(skb->data + offset + size - 1) & 4) &&
2564 size > 4))
2565 size -= 4;
2566
2567 buffer_info->length = size;
2568 buffer_info->dma =
2569 pci_map_single(adapter->pdev,
2570 skb->data + offset,
2571 size,
2572 PCI_DMA_TODEVICE);
2573 buffer_info->time_stamp = jiffies;
2574
2575 len -= size;
2576 offset += size;
2577 count++;
2578 if (unlikely(++i == tx_ring->count)) i = 0;
2579 }
2580
2581 for (f = 0; f < nr_frags; f++) {
2582 struct skb_frag_struct *frag;
2583
2584 frag = &skb_shinfo(skb)->frags[f];
2585 len = frag->size;
2586 offset = frag->page_offset;
2587
2588 while (len) {
2589 buffer_info = &tx_ring->buffer_info[i];
2590 size = min(len, max_per_txd);
2591 #ifdef NETIF_F_TSO
2592 /* Workaround for premature desc write-backs
2593 * in TSO mode. Append 4-byte sentinel desc */
2594 if (unlikely(mss && f == (nr_frags-1) && size == len && size > 8))
2595 size -= 4;
2596 #endif
2597 /* Workaround for potential 82544 hang in PCI-X.
2598 * Avoid terminating buffers within evenly-aligned
2599 * dwords. */
2600 if (unlikely(adapter->pcix_82544 &&
2601 !((unsigned long)(frag->page+offset+size-1) & 4) &&
2602 size > 4))
2603 size -= 4;
2604
2605 buffer_info->length = size;
2606 buffer_info->dma =
2607 pci_map_page(adapter->pdev,
2608 frag->page,
2609 offset,
2610 size,
2611 PCI_DMA_TODEVICE);
2612 buffer_info->time_stamp = jiffies;
2613
2614 len -= size;
2615 offset += size;
2616 count++;
2617 if (unlikely(++i == tx_ring->count)) i = 0;
2618 }
2619 }
2620
2621 i = (i == 0) ? tx_ring->count - 1 : i - 1;
2622 tx_ring->buffer_info[i].skb = skb;
2623 tx_ring->buffer_info[first].next_to_watch = i;
2624
2625 return count;
2626 }
2627
2628 static inline void
2629 e1000_tx_queue(struct e1000_adapter *adapter, struct e1000_tx_ring *tx_ring,
2630 int tx_flags, int count)
2631 {
2632 struct e1000_tx_desc *tx_desc = NULL;
2633 struct e1000_buffer *buffer_info;
2634 uint32_t txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
2635 unsigned int i;
2636
2637 if (likely(tx_flags & E1000_TX_FLAGS_TSO)) {
2638 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
2639 E1000_TXD_CMD_TSE;
2640 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2641
2642 if (likely(tx_flags & E1000_TX_FLAGS_IPV4))
2643 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
2644 }
2645
2646 if (likely(tx_flags & E1000_TX_FLAGS_CSUM)) {
2647 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
2648 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
2649 }
2650
2651 if (unlikely(tx_flags & E1000_TX_FLAGS_VLAN)) {
2652 txd_lower |= E1000_TXD_CMD_VLE;
2653 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
2654 }
2655
2656 i = tx_ring->next_to_use;
2657
2658 while (count--) {
2659 buffer_info = &tx_ring->buffer_info[i];
2660 tx_desc = E1000_TX_DESC(*tx_ring, i);
2661 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
2662 tx_desc->lower.data =
2663 cpu_to_le32(txd_lower | buffer_info->length);
2664 tx_desc->upper.data = cpu_to_le32(txd_upper);
2665 if (unlikely(++i == tx_ring->count)) i = 0;
2666 }
2667
2668 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
2669
2670 /* Force memory writes to complete before letting h/w
2671 * know there are new descriptors to fetch. (Only
2672 * applicable for weak-ordered memory model archs,
2673 * such as IA-64). */
2674 wmb();
2675
2676 tx_ring->next_to_use = i;
2677 writel(i, adapter->hw.hw_addr + tx_ring->tdt);
2678 }
2679
2680 /**
2681 * 82547 workaround to avoid controller hang in half-duplex environment.
2682 * The workaround is to avoid queuing a large packet that would span
2683 * the internal Tx FIFO ring boundary by notifying the stack to resend
2684 * the packet at a later time. This gives the Tx FIFO an opportunity to
2685 * flush all packets. When that occurs, we reset the Tx FIFO pointers
2686 * to the beginning of the Tx FIFO.
2687 **/
2688
2689 #define E1000_FIFO_HDR 0x10
2690 #define E1000_82547_PAD_LEN 0x3E0
2691
2692 static inline int
2693 e1000_82547_fifo_workaround(struct e1000_adapter *adapter, struct sk_buff *skb)
2694 {
2695 uint32_t fifo_space = adapter->tx_fifo_size - adapter->tx_fifo_head;
2696 uint32_t skb_fifo_len = skb->len + E1000_FIFO_HDR;
2697
2698 E1000_ROUNDUP(skb_fifo_len, E1000_FIFO_HDR);
2699
2700 if (adapter->link_duplex != HALF_DUPLEX)
2701 goto no_fifo_stall_required;
2702
2703 if (atomic_read(&adapter->tx_fifo_stall))
2704 return 1;
2705
2706 if (skb_fifo_len >= (E1000_82547_PAD_LEN + fifo_space)) {
2707 atomic_set(&adapter->tx_fifo_stall, 1);
2708 return 1;
2709 }
2710
2711 no_fifo_stall_required:
2712 adapter->tx_fifo_head += skb_fifo_len;
2713 if (adapter->tx_fifo_head >= adapter->tx_fifo_size)
2714 adapter->tx_fifo_head -= adapter->tx_fifo_size;
2715 return 0;
2716 }
2717
2718 #define MINIMUM_DHCP_PACKET_SIZE 282
2719 static inline int
2720 e1000_transfer_dhcp_info(struct e1000_adapter *adapter, struct sk_buff *skb)
2721 {
2722 struct e1000_hw *hw = &adapter->hw;
2723 uint16_t length, offset;
2724 if (vlan_tx_tag_present(skb)) {
2725 if (!((vlan_tx_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
2726 ( adapter->hw.mng_cookie.status &
2727 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT)) )
2728 return 0;
2729 }
2730 if (skb->len > MINIMUM_DHCP_PACKET_SIZE) {
2731 struct ethhdr *eth = (struct ethhdr *) skb->data;
2732 if ((htons(ETH_P_IP) == eth->h_proto)) {
2733 const struct iphdr *ip =
2734 (struct iphdr *)((uint8_t *)skb->data+14);
2735 if (IPPROTO_UDP == ip->protocol) {
2736 struct udphdr *udp =
2737 (struct udphdr *)((uint8_t *)ip +
2738 (ip->ihl << 2));
2739 if (ntohs(udp->dest) == 67) {
2740 offset = (uint8_t *)udp + 8 - skb->data;
2741 length = skb->len - offset;
2742
2743 return e1000_mng_write_dhcp_info(hw,
2744 (uint8_t *)udp + 8,
2745 length);
2746 }
2747 }
2748 }
2749 }
2750 return 0;
2751 }
2752
2753 #define TXD_USE_COUNT(S, X) (((S) >> (X)) + 1 )
2754 static int
2755 e1000_xmit_frame(struct sk_buff *skb, struct net_device *netdev)
2756 {
2757 struct e1000_adapter *adapter = netdev_priv(netdev);
2758 struct e1000_tx_ring *tx_ring;
2759 unsigned int first, max_per_txd = E1000_MAX_DATA_PER_TXD;
2760 unsigned int max_txd_pwr = E1000_MAX_TXD_PWR;
2761 unsigned int tx_flags = 0;
2762 unsigned int len = skb->len;
2763 unsigned long flags;
2764 unsigned int nr_frags = 0;
2765 unsigned int mss = 0;
2766 int count = 0;
2767 int tso;
2768 unsigned int f;
2769 len -= skb->data_len;
2770
2771 tx_ring = adapter->tx_ring;
2772
2773 if (unlikely(skb->len <= 0)) {
2774 dev_kfree_skb_any(skb);
2775 return NETDEV_TX_OK;
2776 }
2777
2778 #ifdef NETIF_F_TSO
2779 mss = skb_shinfo(skb)->tso_size;
2780 /* The controller does a simple calculation to
2781 * make sure there is enough room in the FIFO before
2782 * initiating the DMA for each buffer. The calc is:
2783 * 4 = ceil(buffer len/mss). To make sure we don't
2784 * overrun the FIFO, adjust the max buffer len if mss
2785 * drops. */
2786 if (mss) {
2787 uint8_t hdr_len;
2788 max_per_txd = min(mss << 2, max_per_txd);
2789 max_txd_pwr = fls(max_per_txd) - 1;
2790
2791 /* TSO Workaround for 82571/2/3 Controllers -- if skb->data
2792 * points to just header, pull a few bytes of payload from
2793 * frags into skb->data */
2794 hdr_len = ((skb->h.raw - skb->data) + (skb->h.th->doff << 2));
2795 if (skb->data_len && (hdr_len == (skb->len - skb->data_len))) {
2796 switch (adapter->hw.mac_type) {
2797 unsigned int pull_size;
2798 case e1000_82571:
2799 case e1000_82572:
2800 case e1000_82573:
2801 pull_size = min((unsigned int)4, skb->data_len);
2802 if (!__pskb_pull_tail(skb, pull_size)) {
2803 printk(KERN_ERR
2804 "__pskb_pull_tail failed.\n");
2805 dev_kfree_skb_any(skb);
2806 return NETDEV_TX_OK;
2807 }
2808 len = skb->len - skb->data_len;
2809 break;
2810 default:
2811 /* do nothing */
2812 break;
2813 }
2814 }
2815 }
2816
2817 /* reserve a descriptor for the offload context */
2818 if ((mss) || (skb->ip_summed == CHECKSUM_HW))
2819 count++;
2820 count++;
2821 #else
2822 if (skb->ip_summed == CHECKSUM_HW)
2823 count++;
2824 #endif
2825
2826 #ifdef NETIF_F_TSO
2827 /* Controller Erratum workaround */
2828 if (!skb->data_len && tx_ring->last_tx_tso &&
2829 !skb_shinfo(skb)->tso_size)
2830 count++;
2831 #endif
2832
2833 count += TXD_USE_COUNT(len, max_txd_pwr);
2834
2835 if (adapter->pcix_82544)
2836 count++;
2837
2838 /* work-around for errata 10 and it applies to all controllers
2839 * in PCI-X mode, so add one more descriptor to the count
2840 */
2841 if (unlikely((adapter->hw.bus_type == e1000_bus_type_pcix) &&
2842 (len > 2015)))
2843 count++;
2844
2845 nr_frags = skb_shinfo(skb)->nr_frags;
2846 for (f = 0; f < nr_frags; f++)
2847 count += TXD_USE_COUNT(skb_shinfo(skb)->frags[f].size,
2848 max_txd_pwr);
2849 if (adapter->pcix_82544)
2850 count += nr_frags;
2851
2852
2853 if (adapter->hw.tx_pkt_filtering &&
2854 (adapter->hw.mac_type == e1000_82573))
2855 e1000_transfer_dhcp_info(adapter, skb);
2856
2857 local_irq_save(flags);
2858 if (!spin_trylock(&tx_ring->tx_lock)) {
2859 /* Collision - tell upper layer to requeue */
2860 local_irq_restore(flags);
2861 return NETDEV_TX_LOCKED;
2862 }
2863
2864 /* need: count + 2 desc gap to keep tail from touching
2865 * head, otherwise try next time */
2866 if (unlikely(E1000_DESC_UNUSED(tx_ring) < count + 2)) {
2867 netif_stop_queue(netdev);
2868 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
2869 return NETDEV_TX_BUSY;
2870 }
2871
2872 if (unlikely(adapter->hw.mac_type == e1000_82547)) {
2873 if (unlikely(e1000_82547_fifo_workaround(adapter, skb))) {
2874 netif_stop_queue(netdev);
2875 mod_timer(&adapter->tx_fifo_stall_timer, jiffies);
2876 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
2877 return NETDEV_TX_BUSY;
2878 }
2879 }
2880
2881 if (unlikely(adapter->vlgrp && vlan_tx_tag_present(skb))) {
2882 tx_flags |= E1000_TX_FLAGS_VLAN;
2883 tx_flags |= (vlan_tx_tag_get(skb) << E1000_TX_FLAGS_VLAN_SHIFT);
2884 }
2885
2886 first = tx_ring->next_to_use;
2887
2888 tso = e1000_tso(adapter, tx_ring, skb);
2889 if (tso < 0) {
2890 dev_kfree_skb_any(skb);
2891 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
2892 return NETDEV_TX_OK;
2893 }
2894
2895 if (likely(tso)) {
2896 tx_ring->last_tx_tso = 1;
2897 tx_flags |= E1000_TX_FLAGS_TSO;
2898 } else if (likely(e1000_tx_csum(adapter, tx_ring, skb)))
2899 tx_flags |= E1000_TX_FLAGS_CSUM;
2900
2901 /* Old method was to assume IPv4 packet by default if TSO was enabled.
2902 * 82571 hardware supports TSO capabilities for IPv6 as well...
2903 * no longer assume, we must. */
2904 if (likely(skb->protocol == ntohs(ETH_P_IP)))
2905 tx_flags |= E1000_TX_FLAGS_IPV4;
2906
2907 e1000_tx_queue(adapter, tx_ring, tx_flags,
2908 e1000_tx_map(adapter, tx_ring, skb, first,
2909 max_per_txd, nr_frags, mss));
2910
2911 netdev->trans_start = jiffies;
2912
2913 /* Make sure there is space in the ring for the next send. */
2914 if (unlikely(E1000_DESC_UNUSED(tx_ring) < MAX_SKB_FRAGS + 2))
2915 netif_stop_queue(netdev);
2916
2917 spin_unlock_irqrestore(&tx_ring->tx_lock, flags);
2918 return NETDEV_TX_OK;
2919 }
2920
2921 /**
2922 * e1000_tx_timeout - Respond to a Tx Hang
2923 * @netdev: network interface device structure
2924 **/
2925
2926 static void
2927 e1000_tx_timeout(struct net_device *netdev)
2928 {
2929 struct e1000_adapter *adapter = netdev_priv(netdev);
2930
2931 /* Do the reset outside of interrupt context */
2932 adapter->tx_timeout_count++;
2933 schedule_work(&adapter->reset_task);
2934 }
2935
2936 static void
2937 e1000_reset_task(struct net_device *netdev)
2938 {
2939 struct e1000_adapter *adapter = netdev_priv(netdev);
2940
2941 e1000_down(adapter);
2942 e1000_up(adapter);
2943 }
2944
2945 /**
2946 * e1000_get_stats - Get System Network Statistics
2947 * @netdev: network interface device structure
2948 *
2949 * Returns the address of the device statistics structure.
2950 * The statistics are actually updated from the timer callback.
2951 **/
2952
2953 static struct net_device_stats *
2954 e1000_get_stats(struct net_device *netdev)
2955 {
2956 struct e1000_adapter *adapter = netdev_priv(netdev);
2957
2958 /* only return the current stats */
2959 return &adapter->net_stats;
2960 }
2961
2962 /**
2963 * e1000_change_mtu - Change the Maximum Transfer Unit
2964 * @netdev: network interface device structure
2965 * @new_mtu: new value for maximum frame size
2966 *
2967 * Returns 0 on success, negative on failure
2968 **/
2969
2970 static int
2971 e1000_change_mtu(struct net_device *netdev, int new_mtu)
2972 {
2973 struct e1000_adapter *adapter = netdev_priv(netdev);
2974 int max_frame = new_mtu + ENET_HEADER_SIZE + ETHERNET_FCS_SIZE;
2975 uint16_t eeprom_data = 0;
2976
2977 if ((max_frame < MINIMUM_ETHERNET_FRAME_SIZE) ||
2978 (max_frame > MAX_JUMBO_FRAME_SIZE)) {
2979 DPRINTK(PROBE, ERR, "Invalid MTU setting\n");
2980 return -EINVAL;
2981 }
2982
2983 /* Adapter-specific max frame size limits. */
2984 switch (adapter->hw.mac_type) {
2985 case e1000_82542_rev2_0:
2986 case e1000_82542_rev2_1:
2987 if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
2988 DPRINTK(PROBE, ERR, "Jumbo Frames not supported.\n");
2989 return -EINVAL;
2990 }
2991 break;
2992 case e1000_82573:
2993 /* only enable jumbo frames if ASPM is disabled completely
2994 * this means both bits must be zero in 0x1A bits 3:2 */
2995 e1000_read_eeprom(&adapter->hw, EEPROM_INIT_3GIO_3, 1,
2996 &eeprom_data);
2997 if (eeprom_data & EEPROM_WORD1A_ASPM_MASK) {
2998 if (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE) {
2999 DPRINTK(PROBE, ERR,
3000 "Jumbo Frames not supported.\n");
3001 return -EINVAL;
3002 }
3003 break;
3004 }
3005 /* fall through to get support */
3006 case e1000_82571:
3007 case e1000_82572:
3008 case e1000_80003es2lan:
3009 #define MAX_STD_JUMBO_FRAME_SIZE 9234
3010 if (max_frame > MAX_STD_JUMBO_FRAME_SIZE) {
3011 DPRINTK(PROBE, ERR, "MTU > 9216 not supported.\n");
3012 return -EINVAL;
3013 }
3014 break;
3015 default:
3016 /* Capable of supporting up to MAX_JUMBO_FRAME_SIZE limit. */
3017 break;
3018 }
3019
3020
3021 if (adapter->hw.mac_type > e1000_82547_rev_2) {
3022 adapter->rx_buffer_len = max_frame;
3023 E1000_ROUNDUP(adapter->rx_buffer_len, 1024);
3024 } else {
3025 if(unlikely((adapter->hw.mac_type < e1000_82543) &&
3026 (max_frame > MAXIMUM_ETHERNET_FRAME_SIZE))) {
3027 DPRINTK(PROBE, ERR, "Jumbo Frames not supported "
3028 "on 82542\n");
3029 return -EINVAL;
3030 } else {
3031 if(max_frame <= E1000_RXBUFFER_2048)
3032 adapter->rx_buffer_len = E1000_RXBUFFER_2048;
3033 else if(max_frame <= E1000_RXBUFFER_4096)
3034 adapter->rx_buffer_len = E1000_RXBUFFER_4096;
3035 else if(max_frame <= E1000_RXBUFFER_8192)
3036 adapter->rx_buffer_len = E1000_RXBUFFER_8192;
3037 else if(max_frame <= E1000_RXBUFFER_16384)
3038 adapter->rx_buffer_len = E1000_RXBUFFER_16384;
3039 }
3040 }
3041
3042 netdev->mtu = new_mtu;
3043
3044 if (netif_running(netdev)) {
3045 e1000_down(adapter);
3046 e1000_up(adapter);
3047 }
3048
3049 adapter->hw.max_frame_size = max_frame;
3050
3051 return 0;
3052 }
3053
3054 /**
3055 * e1000_update_stats - Update the board statistics counters
3056 * @adapter: board private structure
3057 **/
3058
3059 void
3060 e1000_update_stats(struct e1000_adapter *adapter)
3061 {
3062 struct e1000_hw *hw = &adapter->hw;
3063 unsigned long flags;
3064 uint16_t phy_tmp;
3065
3066 #define PHY_IDLE_ERROR_COUNT_MASK 0x00FF
3067
3068 spin_lock_irqsave(&adapter->stats_lock, flags);
3069
3070 /* these counters are modified from e1000_adjust_tbi_stats,
3071 * called from the interrupt context, so they must only
3072 * be written while holding adapter->stats_lock
3073 */
3074
3075 adapter->stats.crcerrs += E1000_READ_REG(hw, CRCERRS);
3076 adapter->stats.gprc += E1000_READ_REG(hw, GPRC);
3077 adapter->stats.gorcl += E1000_READ_REG(hw, GORCL);
3078 adapter->stats.gorch += E1000_READ_REG(hw, GORCH);
3079 adapter->stats.bprc += E1000_READ_REG(hw, BPRC);
3080 adapter->stats.mprc += E1000_READ_REG(hw, MPRC);
3081 adapter->stats.roc += E1000_READ_REG(hw, ROC);
3082 adapter->stats.prc64 += E1000_READ_REG(hw, PRC64);
3083 adapter->stats.prc127 += E1000_READ_REG(hw, PRC127);
3084 adapter->stats.prc255 += E1000_READ_REG(hw, PRC255);
3085 adapter->stats.prc511 += E1000_READ_REG(hw, PRC511);
3086 adapter->stats.prc1023 += E1000_READ_REG(hw, PRC1023);
3087 adapter->stats.prc1522 += E1000_READ_REG(hw, PRC1522);
3088
3089 adapter->stats.symerrs += E1000_READ_REG(hw, SYMERRS);
3090 adapter->stats.mpc += E1000_READ_REG(hw, MPC);
3091 adapter->stats.scc += E1000_READ_REG(hw, SCC);
3092 adapter->stats.ecol += E1000_READ_REG(hw, ECOL);
3093 adapter->stats.mcc += E1000_READ_REG(hw, MCC);
3094 adapter->stats.latecol += E1000_READ_REG(hw, LATECOL);
3095 adapter->stats.dc += E1000_READ_REG(hw, DC);
3096 adapter->stats.sec += E1000_READ_REG(hw, SEC);
3097 adapter->stats.rlec += E1000_READ_REG(hw, RLEC);
3098 adapter->stats.xonrxc += E1000_READ_REG(hw, XONRXC);
3099 adapter->stats.xontxc += E1000_READ_REG(hw, XONTXC);
3100 adapter->stats.xoffrxc += E1000_READ_REG(hw, XOFFRXC);
3101 adapter->stats.xofftxc += E1000_READ_REG(hw, XOFFTXC);
3102 adapter->stats.fcruc += E1000_READ_REG(hw, FCRUC);
3103 adapter->stats.gptc += E1000_READ_REG(hw, GPTC);
3104 adapter->stats.gotcl += E1000_READ_REG(hw, GOTCL);
3105 adapter->stats.gotch += E1000_READ_REG(hw, GOTCH);
3106 adapter->stats.rnbc += E1000_READ_REG(hw, RNBC);
3107 adapter->stats.ruc += E1000_READ_REG(hw, RUC);
3108 adapter->stats.rfc += E1000_READ_REG(hw, RFC);
3109 adapter->stats.rjc += E1000_READ_REG(hw, RJC);
3110 adapter->stats.torl += E1000_READ_REG(hw, TORL);
3111 adapter->stats.torh += E1000_READ_REG(hw, TORH);
3112 adapter->stats.totl += E1000_READ_REG(hw, TOTL);
3113 adapter->stats.toth += E1000_READ_REG(hw, TOTH);
3114 adapter->stats.tpr += E1000_READ_REG(hw, TPR);
3115 adapter->stats.ptc64 += E1000_READ_REG(hw, PTC64);
3116 adapter->stats.ptc127 += E1000_READ_REG(hw, PTC127);
3117 adapter->stats.ptc255 += E1000_READ_REG(hw, PTC255);
3118 adapter->stats.ptc511 += E1000_READ_REG(hw, PTC511);
3119 adapter->stats.ptc1023 += E1000_READ_REG(hw, PTC1023);
3120 adapter->stats.ptc1522 += E1000_READ_REG(hw, PTC1522);
3121 adapter->stats.mptc += E1000_READ_REG(hw, MPTC);
3122 adapter->stats.bptc += E1000_READ_REG(hw, BPTC);
3123
3124 /* used for adaptive IFS */
3125
3126 hw->tx_packet_delta = E1000_READ_REG(hw, TPT);
3127 adapter->stats.tpt += hw->tx_packet_delta;
3128 hw->collision_delta = E1000_READ_REG(hw, COLC);
3129 adapter->stats.colc += hw->collision_delta;
3130
3131 if (hw->mac_type >= e1000_82543) {
3132 adapter->stats.algnerrc += E1000_READ_REG(hw, ALGNERRC);
3133 adapter->stats.rxerrc += E1000_READ_REG(hw, RXERRC);
3134 adapter->stats.tncrs += E1000_READ_REG(hw, TNCRS);
3135 adapter->stats.cexterr += E1000_READ_REG(hw, CEXTERR);
3136 adapter->stats.tsctc += E1000_READ_REG(hw, TSCTC);
3137 adapter->stats.tsctfc += E1000_READ_REG(hw, TSCTFC);
3138 }
3139 if (hw->mac_type > e1000_82547_rev_2) {
3140 adapter->stats.iac += E1000_READ_REG(hw, IAC);
3141 adapter->stats.icrxoc += E1000_READ_REG(hw, ICRXOC);
3142 adapter->stats.icrxptc += E1000_READ_REG(hw, ICRXPTC);
3143 adapter->stats.icrxatc += E1000_READ_REG(hw, ICRXATC);
3144 adapter->stats.ictxptc += E1000_READ_REG(hw, ICTXPTC);
3145 adapter->stats.ictxatc += E1000_READ_REG(hw, ICTXATC);
3146 adapter->stats.ictxqec += E1000_READ_REG(hw, ICTXQEC);
3147 adapter->stats.ictxqmtc += E1000_READ_REG(hw, ICTXQMTC);
3148 adapter->stats.icrxdmtc += E1000_READ_REG(hw, ICRXDMTC);
3149 }
3150
3151 /* Fill out the OS statistics structure */
3152
3153 adapter->net_stats.rx_packets = adapter->stats.gprc;
3154 adapter->net_stats.tx_packets = adapter->stats.gptc;
3155 adapter->net_stats.rx_bytes = adapter->stats.gorcl;
3156 adapter->net_stats.tx_bytes = adapter->stats.gotcl;
3157 adapter->net_stats.multicast = adapter->stats.mprc;
3158 adapter->net_stats.collisions = adapter->stats.colc;
3159
3160 /* Rx Errors */
3161
3162 /* RLEC on some newer hardware can be incorrect so build
3163 * our own version based on RUC and ROC */
3164 adapter->net_stats.rx_errors = adapter->stats.rxerrc +
3165 adapter->stats.crcerrs + adapter->stats.algnerrc +
3166 adapter->stats.ruc + adapter->stats.roc +
3167 adapter->stats.cexterr;
3168 adapter->net_stats.rx_dropped = 0;
3169 adapter->net_stats.rx_length_errors = adapter->stats.ruc +
3170 adapter->stats.roc;
3171 adapter->net_stats.rx_crc_errors = adapter->stats.crcerrs;
3172 adapter->net_stats.rx_frame_errors = adapter->stats.algnerrc;
3173 adapter->net_stats.rx_missed_errors = adapter->stats.mpc;
3174
3175 /* Tx Errors */
3176
3177 adapter->net_stats.tx_errors = adapter->stats.ecol +
3178 adapter->stats.latecol;
3179 adapter->net_stats.tx_aborted_errors = adapter->stats.ecol;
3180 adapter->net_stats.tx_window_errors = adapter->stats.latecol;
3181 adapter->net_stats.tx_carrier_errors = adapter->stats.tncrs;
3182
3183 /* Tx Dropped needs to be maintained elsewhere */
3184
3185 /* Phy Stats */
3186
3187 if (hw->media_type == e1000_media_type_copper) {
3188 if ((adapter->link_speed == SPEED_1000) &&
3189 (!e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_tmp))) {
3190 phy_tmp &= PHY_IDLE_ERROR_COUNT_MASK;
3191 adapter->phy_stats.idle_errors += phy_tmp;
3192 }
3193
3194 if ((hw->mac_type <= e1000_82546) &&
3195 (hw->phy_type == e1000_phy_m88) &&
3196 !e1000_read_phy_reg(hw, M88E1000_RX_ERR_CNTR, &phy_tmp))
3197 adapter->phy_stats.receive_errors += phy_tmp;
3198 }
3199
3200 spin_unlock_irqrestore(&adapter->stats_lock, flags);
3201 }
3202
3203 /**
3204 * e1000_intr - Interrupt Handler
3205 * @irq: interrupt number
3206 * @data: pointer to a network interface device structure
3207 * @pt_regs: CPU registers structure
3208 **/
3209
3210 static irqreturn_t
3211 e1000_intr(int irq, void *data, struct pt_regs *regs)
3212 {
3213 struct net_device *netdev = data;
3214 struct e1000_adapter *adapter = netdev_priv(netdev);
3215 struct e1000_hw *hw = &adapter->hw;
3216 uint32_t rctl, icr = E1000_READ_REG(hw, ICR);
3217 #ifndef CONFIG_E1000_NAPI
3218 int i;
3219 #else
3220 /* Interrupt Auto-Mask...upon reading ICR,
3221 * interrupts are masked. No need for the
3222 * IMC write, but it does mean we should
3223 * account for it ASAP. */
3224 if (likely(hw->mac_type >= e1000_82571))
3225 atomic_inc(&adapter->irq_sem);
3226 #endif
3227
3228 if (unlikely(!icr)) {
3229 #ifdef CONFIG_E1000_NAPI
3230 if (hw->mac_type >= e1000_82571)
3231 e1000_irq_enable(adapter);
3232 #endif
3233 return IRQ_NONE; /* Not our interrupt */
3234 }
3235
3236 if (unlikely(icr & (E1000_ICR_RXSEQ | E1000_ICR_LSC))) {
3237 hw->get_link_status = 1;
3238 /* 80003ES2LAN workaround--
3239 * For packet buffer work-around on link down event;
3240 * disable receives here in the ISR and
3241 * reset adapter in watchdog
3242 */
3243 if (netif_carrier_ok(netdev) &&
3244 (adapter->hw.mac_type == e1000_80003es2lan)) {
3245 /* disable receives */
3246 rctl = E1000_READ_REG(hw, RCTL);
3247 E1000_WRITE_REG(hw, RCTL, rctl & ~E1000_RCTL_EN);
3248 }
3249 mod_timer(&adapter->watchdog_timer, jiffies);
3250 }
3251
3252 #ifdef CONFIG_E1000_NAPI
3253 if (unlikely(hw->mac_type < e1000_82571)) {
3254 atomic_inc(&adapter->irq_sem);
3255 E1000_WRITE_REG(hw, IMC, ~0);
3256 E1000_WRITE_FLUSH(hw);
3257 }
3258 if (likely(netif_rx_schedule_prep(&adapter->polling_netdev[0])))
3259 __netif_rx_schedule(&adapter->polling_netdev[0]);
3260 else
3261 e1000_irq_enable(adapter);
3262 #else
3263 /* Writing IMC and IMS is needed for 82547.
3264 * Due to Hub Link bus being occupied, an interrupt
3265 * de-assertion message is not able to be sent.
3266 * When an interrupt assertion message is generated later,
3267 * two messages are re-ordered and sent out.
3268 * That causes APIC to think 82547 is in de-assertion
3269 * state, while 82547 is in assertion state, resulting
3270 * in dead lock. Writing IMC forces 82547 into
3271 * de-assertion state.
3272 */
3273 if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2) {
3274 atomic_inc(&adapter->irq_sem);
3275 E1000_WRITE_REG(hw, IMC, ~0);
3276 }
3277
3278 for (i = 0; i < E1000_MAX_INTR; i++)
3279 if (unlikely(!adapter->clean_rx(adapter, adapter->rx_ring) &
3280 !e1000_clean_tx_irq(adapter, adapter->tx_ring)))
3281 break;
3282
3283 if (hw->mac_type == e1000_82547 || hw->mac_type == e1000_82547_rev_2)
3284 e1000_irq_enable(adapter);
3285
3286 #endif
3287
3288 return IRQ_HANDLED;
3289 }
3290
3291 #ifdef CONFIG_E1000_NAPI
3292 /**
3293 * e1000_clean - NAPI Rx polling callback
3294 * @adapter: board private structure
3295 **/
3296
3297 static int
3298 e1000_clean(struct net_device *poll_dev, int *budget)
3299 {
3300 struct e1000_adapter *adapter;
3301 int work_to_do = min(*budget, poll_dev->quota);
3302 int tx_cleaned = 0, i = 0, work_done = 0;
3303
3304 /* Must NOT use netdev_priv macro here. */
3305 adapter = poll_dev->priv;
3306
3307 /* Keep link state information with original netdev */
3308 if (!netif_carrier_ok(adapter->netdev))
3309 goto quit_polling;
3310
3311 while (poll_dev != &adapter->polling_netdev[i]) {
3312 i++;
3313 BUG_ON(i == adapter->num_rx_queues);
3314 }
3315
3316 if (likely(adapter->num_tx_queues == 1)) {
3317 /* e1000_clean is called per-cpu. This lock protects
3318 * tx_ring[0] from being cleaned by multiple cpus
3319 * simultaneously. A failure obtaining the lock means
3320 * tx_ring[0] is currently being cleaned anyway. */
3321 if (spin_trylock(&adapter->tx_queue_lock)) {
3322 tx_cleaned = e1000_clean_tx_irq(adapter,
3323 &adapter->tx_ring[0]);
3324 spin_unlock(&adapter->tx_queue_lock);
3325 }
3326 } else
3327 tx_cleaned = e1000_clean_tx_irq(adapter, &adapter->tx_ring[i]);
3328
3329 adapter->clean_rx(adapter, &adapter->rx_ring[i],
3330 &work_done, work_to_do);
3331
3332 *budget -= work_done;
3333 poll_dev->quota -= work_done;
3334
3335 /* If no Tx and not enough Rx work done, exit the polling mode */
3336 if ((!tx_cleaned && (work_done == 0)) ||
3337 !netif_running(adapter->netdev)) {
3338 quit_polling:
3339 netif_rx_complete(poll_dev);
3340 e1000_irq_enable(adapter);
3341 return 0;
3342 }
3343
3344 return 1;
3345 }
3346
3347 #endif
3348 /**
3349 * e1000_clean_tx_irq - Reclaim resources after transmit completes
3350 * @adapter: board private structure
3351 **/
3352
3353 static boolean_t
3354 e1000_clean_tx_irq(struct e1000_adapter *adapter,
3355 struct e1000_tx_ring *tx_ring)
3356 {
3357 struct net_device *netdev = adapter->netdev;
3358 struct e1000_tx_desc *tx_desc, *eop_desc;
3359 struct e1000_buffer *buffer_info;
3360 unsigned int i, eop;
3361 #ifdef CONFIG_E1000_NAPI
3362 unsigned int count = 0;
3363 #endif
3364 boolean_t cleaned = FALSE;
3365
3366 i = tx_ring->next_to_clean;
3367 eop = tx_ring->buffer_info[i].next_to_watch;
3368 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3369
3370 while (eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) {
3371 for (cleaned = FALSE; !cleaned; ) {
3372 tx_desc = E1000_TX_DESC(*tx_ring, i);
3373 buffer_info = &tx_ring->buffer_info[i];
3374 cleaned = (i == eop);
3375
3376 e1000_unmap_and_free_tx_resource(adapter, buffer_info);
3377 memset(tx_desc, 0, sizeof(struct e1000_tx_desc));
3378
3379 if (unlikely(++i == tx_ring->count)) i = 0;
3380 }
3381
3382
3383 eop = tx_ring->buffer_info[i].next_to_watch;
3384 eop_desc = E1000_TX_DESC(*tx_ring, eop);
3385 #ifdef CONFIG_E1000_NAPI
3386 #define E1000_TX_WEIGHT 64
3387 /* weight of a sort for tx, to avoid endless transmit cleanup */
3388 if (count++ == E1000_TX_WEIGHT) break;
3389 #endif
3390 }
3391
3392 tx_ring->next_to_clean = i;
3393
3394 spin_lock(&tx_ring->tx_lock);
3395
3396 if (unlikely(cleaned && netif_queue_stopped(netdev) &&
3397 netif_carrier_ok(netdev)))
3398 netif_wake_queue(netdev);
3399
3400 spin_unlock(&tx_ring->tx_lock);
3401
3402 if (adapter->detect_tx_hung) {
3403 /* Detect a transmit hang in hardware, this serializes the
3404 * check with the clearing of time_stamp and movement of i */
3405 adapter->detect_tx_hung = FALSE;
3406 if (tx_ring->buffer_info[eop].dma &&
3407 time_after(jiffies, tx_ring->buffer_info[eop].time_stamp +
3408 (adapter->tx_timeout_factor * HZ))
3409 && !(E1000_READ_REG(&adapter->hw, STATUS) &
3410 E1000_STATUS_TXOFF)) {
3411
3412 /* detected Tx unit hang */
3413 DPRINTK(DRV, ERR, "Detected Tx Unit Hang\n"
3414 " Tx Queue <%lu>\n"
3415 " TDH <%x>\n"
3416 " TDT <%x>\n"
3417 " next_to_use <%x>\n"
3418 " next_to_clean <%x>\n"
3419 "buffer_info[next_to_clean]\n"
3420 " time_stamp <%lx>\n"
3421 " next_to_watch <%x>\n"
3422 " jiffies <%lx>\n"
3423 " next_to_watch.status <%x>\n",
3424 (unsigned long)((tx_ring - adapter->tx_ring) /
3425 sizeof(struct e1000_tx_ring)),
3426 readl(adapter->hw.hw_addr + tx_ring->tdh),
3427 readl(adapter->hw.hw_addr + tx_ring->tdt),
3428 tx_ring->next_to_use,
3429 tx_ring->next_to_clean,
3430 tx_ring->buffer_info[eop].time_stamp,
3431 eop,
3432 jiffies,
3433 eop_desc->upper.fields.status);
3434 netif_stop_queue(netdev);
3435 }
3436 }
3437 return cleaned;
3438 }
3439
3440 /**
3441 * e1000_rx_checksum - Receive Checksum Offload for 82543
3442 * @adapter: board private structure
3443 * @status_err: receive descriptor status and error fields
3444 * @csum: receive descriptor csum field
3445 * @sk_buff: socket buffer with received data
3446 **/
3447
3448 static inline void
3449 e1000_rx_checksum(struct e1000_adapter *adapter,
3450 uint32_t status_err, uint32_t csum,
3451 struct sk_buff *skb)
3452 {
3453 uint16_t status = (uint16_t)status_err;
3454 uint8_t errors = (uint8_t)(status_err >> 24);
3455 skb->ip_summed = CHECKSUM_NONE;
3456
3457 /* 82543 or newer only */
3458 if (unlikely(adapter->hw.mac_type < e1000_82543)) return;
3459 /* Ignore Checksum bit is set */
3460 if (unlikely(status & E1000_RXD_STAT_IXSM)) return;
3461 /* TCP/UDP checksum error bit is set */
3462 if (unlikely(errors & E1000_RXD_ERR_TCPE)) {
3463 /* let the stack verify checksum errors */
3464 adapter->hw_csum_err++;
3465 return;
3466 }
3467 /* TCP/UDP Checksum has not been calculated */
3468 if (adapter->hw.mac_type <= e1000_82547_rev_2) {
3469 if (!(status & E1000_RXD_STAT_TCPCS))
3470 return;
3471 } else {
3472 if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
3473 return;
3474 }
3475 /* It must be a TCP or UDP packet with a valid checksum */
3476 if (likely(status & E1000_RXD_STAT_TCPCS)) {
3477 /* TCP checksum is good */
3478 skb->ip_summed = CHECKSUM_UNNECESSARY;
3479 } else if (adapter->hw.mac_type > e1000_82547_rev_2) {
3480 /* IP fragment with UDP payload */
3481 /* Hardware complements the payload checksum, so we undo it
3482 * and then put the value in host order for further stack use.
3483 */
3484 csum = ntohl(csum ^ 0xFFFF);
3485 skb->csum = csum;
3486 skb->ip_summed = CHECKSUM_HW;
3487 }
3488 adapter->hw_csum_good++;
3489 }
3490
3491 /**
3492 * e1000_clean_rx_irq - Send received data up the network stack; legacy
3493 * @adapter: board private structure
3494 **/
3495
3496 static boolean_t
3497 #ifdef CONFIG_E1000_NAPI
3498 e1000_clean_rx_irq(struct e1000_adapter *adapter,
3499 struct e1000_rx_ring *rx_ring,
3500 int *work_done, int work_to_do)
3501 #else
3502 e1000_clean_rx_irq(struct e1000_adapter *adapter,
3503 struct e1000_rx_ring *rx_ring)
3504 #endif
3505 {
3506 struct net_device *netdev = adapter->netdev;
3507 struct pci_dev *pdev = adapter->pdev;
3508 struct e1000_rx_desc *rx_desc, *next_rxd;
3509 struct e1000_buffer *buffer_info, *next_buffer;
3510 unsigned long flags;
3511 uint32_t length;
3512 uint8_t last_byte;
3513 unsigned int i;
3514 int cleaned_count = 0;
3515 boolean_t cleaned = FALSE;
3516
3517 i = rx_ring->next_to_clean;
3518 rx_desc = E1000_RX_DESC(*rx_ring, i);
3519 buffer_info = &rx_ring->buffer_info[i];
3520
3521 while (rx_desc->status & E1000_RXD_STAT_DD) {
3522 struct sk_buff *skb;
3523 u8 status;
3524 #ifdef CONFIG_E1000_NAPI
3525 if (*work_done >= work_to_do)
3526 break;
3527 (*work_done)++;
3528 #endif
3529 status = rx_desc->status;
3530 skb = buffer_info->skb;
3531 buffer_info->skb = NULL;
3532
3533 prefetch(skb->data - NET_IP_ALIGN);
3534
3535 if (++i == rx_ring->count) i = 0;
3536 next_rxd = E1000_RX_DESC(*rx_ring, i);
3537 prefetch(next_rxd);
3538
3539 next_buffer = &rx_ring->buffer_info[i];
3540
3541 cleaned = TRUE;
3542 cleaned_count++;
3543 pci_unmap_single(pdev,
3544 buffer_info->dma,
3545 buffer_info->length,
3546 PCI_DMA_FROMDEVICE);
3547
3548 length = le16_to_cpu(rx_desc->length);
3549
3550 if (unlikely(!(status & E1000_RXD_STAT_EOP))) {
3551 /* All receives must fit into a single buffer */
3552 E1000_DBG("%s: Receive packet consumed multiple"
3553 " buffers\n", netdev->name);
3554 dev_kfree_skb_irq(skb);
3555 goto next_desc;
3556 }
3557
3558 if (unlikely(rx_desc->errors & E1000_RXD_ERR_FRAME_ERR_MASK)) {
3559 last_byte = *(skb->data + length - 1);
3560 if (TBI_ACCEPT(&adapter->hw, status,
3561 rx_desc->errors, length, last_byte)) {
3562 spin_lock_irqsave(&adapter->stats_lock, flags);
3563 e1000_tbi_adjust_stats(&adapter->hw,
3564 &adapter->stats,
3565 length, skb->data);
3566 spin_unlock_irqrestore(&adapter->stats_lock,
3567 flags);
3568 length--;
3569 } else {
3570 dev_kfree_skb_irq(skb);
3571 goto next_desc;
3572 }
3573 }
3574
3575 /* code added for copybreak, this should improve
3576 * performance for small packets with large amounts
3577 * of reassembly being done in the stack */
3578 #define E1000_CB_LENGTH 256
3579 if (length < E1000_CB_LENGTH) {
3580 struct sk_buff *new_skb =
3581 dev_alloc_skb(length + NET_IP_ALIGN);
3582 if (new_skb) {
3583 skb_reserve(new_skb, NET_IP_ALIGN);
3584 new_skb->dev = netdev;
3585 memcpy(new_skb->data - NET_IP_ALIGN,
3586 skb->data - NET_IP_ALIGN,
3587 length + NET_IP_ALIGN);
3588 /* save the skb in buffer_info as good */
3589 buffer_info->skb = skb;
3590 skb = new_skb;
3591 skb_put(skb, length);
3592 }
3593 } else
3594 skb_put(skb, length);
3595
3596 /* end copybreak code */
3597
3598 /* Receive Checksum Offload */
3599 e1000_rx_checksum(adapter,
3600 (uint32_t)(status) |
3601 ((uint32_t)(rx_desc->errors) << 24),
3602 le16_to_cpu(rx_desc->csum), skb);
3603
3604 skb->protocol = eth_type_trans(skb, netdev);
3605 #ifdef CONFIG_E1000_NAPI
3606 if (unlikely(adapter->vlgrp &&
3607 (status & E1000_RXD_STAT_VP))) {
3608 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
3609 le16_to_cpu(rx_desc->special) &
3610 E1000_RXD_SPC_VLAN_MASK);
3611 } else {
3612 netif_receive_skb(skb);
3613 }
3614 #else /* CONFIG_E1000_NAPI */
3615 if (unlikely(adapter->vlgrp &&
3616 (status & E1000_RXD_STAT_VP))) {
3617 vlan_hwaccel_rx(skb, adapter->vlgrp,
3618 le16_to_cpu(rx_desc->special) &
3619 E1000_RXD_SPC_VLAN_MASK);
3620 } else {
3621 netif_rx(skb);
3622 }
3623 #endif /* CONFIG_E1000_NAPI */
3624 netdev->last_rx = jiffies;
3625
3626 next_desc:
3627 rx_desc->status = 0;
3628
3629 /* return some buffers to hardware, one at a time is too slow */
3630 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3631 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3632 cleaned_count = 0;
3633 }
3634
3635 /* use prefetched values */
3636 rx_desc = next_rxd;
3637 buffer_info = next_buffer;
3638 }
3639 rx_ring->next_to_clean = i;
3640
3641 cleaned_count = E1000_DESC_UNUSED(rx_ring);
3642 if (cleaned_count)
3643 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3644
3645 return cleaned;
3646 }
3647
3648 /**
3649 * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
3650 * @adapter: board private structure
3651 **/
3652
3653 static boolean_t
3654 #ifdef CONFIG_E1000_NAPI
3655 e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3656 struct e1000_rx_ring *rx_ring,
3657 int *work_done, int work_to_do)
3658 #else
3659 e1000_clean_rx_irq_ps(struct e1000_adapter *adapter,
3660 struct e1000_rx_ring *rx_ring)
3661 #endif
3662 {
3663 union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
3664 struct net_device *netdev = adapter->netdev;
3665 struct pci_dev *pdev = adapter->pdev;
3666 struct e1000_buffer *buffer_info, *next_buffer;
3667 struct e1000_ps_page *ps_page;
3668 struct e1000_ps_page_dma *ps_page_dma;
3669 struct sk_buff *skb;
3670 unsigned int i, j;
3671 uint32_t length, staterr;
3672 int cleaned_count = 0;
3673 boolean_t cleaned = FALSE;
3674
3675 i = rx_ring->next_to_clean;
3676 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
3677 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
3678
3679 while (staterr & E1000_RXD_STAT_DD) {
3680 buffer_info = &rx_ring->buffer_info[i];
3681 ps_page = &rx_ring->ps_page[i];
3682 ps_page_dma = &rx_ring->ps_page_dma[i];
3683 #ifdef CONFIG_E1000_NAPI
3684 if (unlikely(*work_done >= work_to_do))
3685 break;
3686 (*work_done)++;
3687 #endif
3688 skb = buffer_info->skb;
3689
3690 /* in the packet split case this is header only */
3691 prefetch(skb->data - NET_IP_ALIGN);
3692
3693 if (++i == rx_ring->count) i = 0;
3694 next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
3695 prefetch(next_rxd);
3696
3697 next_buffer = &rx_ring->buffer_info[i];
3698
3699 cleaned = TRUE;
3700 cleaned_count++;
3701 pci_unmap_single(pdev, buffer_info->dma,
3702 buffer_info->length,
3703 PCI_DMA_FROMDEVICE);
3704
3705 if (unlikely(!(staterr & E1000_RXD_STAT_EOP))) {
3706 E1000_DBG("%s: Packet Split buffers didn't pick up"
3707 " the full packet\n", netdev->name);
3708 dev_kfree_skb_irq(skb);
3709 goto next_desc;
3710 }
3711
3712 if (unlikely(staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK)) {
3713 dev_kfree_skb_irq(skb);
3714 goto next_desc;
3715 }
3716
3717 length = le16_to_cpu(rx_desc->wb.middle.length0);
3718
3719 if (unlikely(!length)) {
3720 E1000_DBG("%s: Last part of the packet spanning"
3721 " multiple descriptors\n", netdev->name);
3722 dev_kfree_skb_irq(skb);
3723 goto next_desc;
3724 }
3725
3726 /* Good Receive */
3727 skb_put(skb, length);
3728
3729 {
3730 /* this looks ugly, but it seems compiler issues make it
3731 more efficient than reusing j */
3732 int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
3733
3734 /* page alloc/put takes too long and effects small packet
3735 * throughput, so unsplit small packets and save the alloc/put*/
3736 if (l1 && ((length + l1) < E1000_CB_LENGTH)) {
3737 u8 *vaddr;
3738 /* there is no documentation about how to call
3739 * kmap_atomic, so we can't hold the mapping
3740 * very long */
3741 pci_dma_sync_single_for_cpu(pdev,
3742 ps_page_dma->ps_page_dma[0],
3743 PAGE_SIZE,
3744 PCI_DMA_FROMDEVICE);
3745 vaddr = kmap_atomic(ps_page->ps_page[0],
3746 KM_SKB_DATA_SOFTIRQ);
3747 memcpy(skb->tail, vaddr, l1);
3748 kunmap_atomic(vaddr, KM_SKB_DATA_SOFTIRQ);
3749 pci_dma_sync_single_for_device(pdev,
3750 ps_page_dma->ps_page_dma[0],
3751 PAGE_SIZE, PCI_DMA_FROMDEVICE);
3752 skb_put(skb, l1);
3753 length += l1;
3754 goto copydone;
3755 } /* if */
3756 }
3757
3758 for (j = 0; j < adapter->rx_ps_pages; j++) {
3759 if (!(length= le16_to_cpu(rx_desc->wb.upper.length[j])))
3760 break;
3761 pci_unmap_page(pdev, ps_page_dma->ps_page_dma[j],
3762 PAGE_SIZE, PCI_DMA_FROMDEVICE);
3763 ps_page_dma->ps_page_dma[j] = 0;
3764 skb_fill_page_desc(skb, j, ps_page->ps_page[j], 0,
3765 length);
3766 ps_page->ps_page[j] = NULL;
3767 skb->len += length;
3768 skb->data_len += length;
3769 skb->truesize += length;
3770 }
3771
3772 copydone:
3773 e1000_rx_checksum(adapter, staterr,
3774 le16_to_cpu(rx_desc->wb.lower.hi_dword.csum_ip.csum), skb);
3775 skb->protocol = eth_type_trans(skb, netdev);
3776
3777 if (likely(rx_desc->wb.upper.header_status &
3778 cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP)))
3779 adapter->rx_hdr_split++;
3780 #ifdef CONFIG_E1000_NAPI
3781 if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
3782 vlan_hwaccel_receive_skb(skb, adapter->vlgrp,
3783 le16_to_cpu(rx_desc->wb.middle.vlan) &
3784 E1000_RXD_SPC_VLAN_MASK);
3785 } else {
3786 netif_receive_skb(skb);
3787 }
3788 #else /* CONFIG_E1000_NAPI */
3789 if (unlikely(adapter->vlgrp && (staterr & E1000_RXD_STAT_VP))) {
3790 vlan_hwaccel_rx(skb, adapter->vlgrp,
3791 le16_to_cpu(rx_desc->wb.middle.vlan) &
3792 E1000_RXD_SPC_VLAN_MASK);
3793 } else {
3794 netif_rx(skb);
3795 }
3796 #endif /* CONFIG_E1000_NAPI */
3797 netdev->last_rx = jiffies;
3798
3799 next_desc:
3800 rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
3801 buffer_info->skb = NULL;
3802
3803 /* return some buffers to hardware, one at a time is too slow */
3804 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
3805 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3806 cleaned_count = 0;
3807 }
3808
3809 /* use prefetched values */
3810 rx_desc = next_rxd;
3811 buffer_info = next_buffer;
3812
3813 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
3814 }
3815 rx_ring->next_to_clean = i;
3816
3817 cleaned_count = E1000_DESC_UNUSED(rx_ring);
3818 if (cleaned_count)
3819 adapter->alloc_rx_buf(adapter, rx_ring, cleaned_count);
3820
3821 return cleaned;
3822 }
3823
3824 /**
3825 * e1000_alloc_rx_buffers - Replace used receive buffers; legacy & extended
3826 * @adapter: address of board private structure
3827 **/
3828
3829 static void
3830 e1000_alloc_rx_buffers(struct e1000_adapter *adapter,
3831 struct e1000_rx_ring *rx_ring,
3832 int cleaned_count)
3833 {
3834 struct net_device *netdev = adapter->netdev;
3835 struct pci_dev *pdev = adapter->pdev;
3836 struct e1000_rx_desc *rx_desc;
3837 struct e1000_buffer *buffer_info;
3838 struct sk_buff *skb;
3839 unsigned int i;
3840 unsigned int bufsz = adapter->rx_buffer_len + NET_IP_ALIGN;
3841
3842 i = rx_ring->next_to_use;
3843 buffer_info = &rx_ring->buffer_info[i];
3844
3845 while (cleaned_count--) {
3846 if (!(skb = buffer_info->skb))
3847 skb = dev_alloc_skb(bufsz);
3848 else {
3849 skb_trim(skb, 0);
3850 goto map_skb;
3851 }
3852
3853 if (unlikely(!skb)) {
3854 /* Better luck next round */
3855 adapter->alloc_rx_buff_failed++;
3856 break;
3857 }
3858
3859 /* Fix for errata 23, can't cross 64kB boundary */
3860 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
3861 struct sk_buff *oldskb = skb;
3862 DPRINTK(RX_ERR, ERR, "skb align check failed: %u bytes "
3863 "at %p\n", bufsz, skb->data);
3864 /* Try again, without freeing the previous */
3865 skb = dev_alloc_skb(bufsz);
3866 /* Failed allocation, critical failure */
3867 if (!skb) {
3868 dev_kfree_skb(oldskb);
3869 break;
3870 }
3871
3872 if (!e1000_check_64k_bound(adapter, skb->data, bufsz)) {
3873 /* give up */
3874 dev_kfree_skb(skb);
3875 dev_kfree_skb(oldskb);
3876 break; /* while !buffer_info->skb */
3877 } else {
3878 /* Use new allocation */
3879 dev_kfree_skb(oldskb);
3880 }
3881 }
3882 /* Make buffer alignment 2 beyond a 16 byte boundary
3883 * this will result in a 16 byte aligned IP header after
3884 * the 14 byte MAC header is removed
3885 */
3886 skb_reserve(skb, NET_IP_ALIGN);
3887
3888 skb->dev = netdev;
3889
3890 buffer_info->skb = skb;
3891 buffer_info->length = adapter->rx_buffer_len;
3892 map_skb:
3893 buffer_info->dma = pci_map_single(pdev,
3894 skb->data,
3895 adapter->rx_buffer_len,
3896 PCI_DMA_FROMDEVICE);
3897
3898 /* Fix for errata 23, can't cross 64kB boundary */
3899 if (!e1000_check_64k_bound(adapter,
3900 (void *)(unsigned long)buffer_info->dma,
3901 adapter->rx_buffer_len)) {
3902 DPRINTK(RX_ERR, ERR,
3903 "dma align check failed: %u bytes at %p\n",
3904 adapter->rx_buffer_len,
3905 (void *)(unsigned long)buffer_info->dma);
3906 dev_kfree_skb(skb);
3907 buffer_info->skb = NULL;
3908
3909 pci_unmap_single(pdev, buffer_info->dma,
3910 adapter->rx_buffer_len,
3911 PCI_DMA_FROMDEVICE);
3912
3913 break; /* while !buffer_info->skb */
3914 }
3915 rx_desc = E1000_RX_DESC(*rx_ring, i);
3916 rx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
3917
3918 if (unlikely(++i == rx_ring->count))
3919 i = 0;
3920 buffer_info = &rx_ring->buffer_info[i];
3921 }
3922
3923 if (likely(rx_ring->next_to_use != i)) {
3924 rx_ring->next_to_use = i;
3925 if (unlikely(i-- == 0))
3926 i = (rx_ring->count - 1);
3927
3928 /* Force memory writes to complete before letting h/w
3929 * know there are new descriptors to fetch. (Only
3930 * applicable for weak-ordered memory model archs,
3931 * such as IA-64). */
3932 wmb();
3933 writel(i, adapter->hw.hw_addr + rx_ring->rdt);
3934 }
3935 }
3936
3937 /**
3938 * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
3939 * @adapter: address of board private structure
3940 **/
3941
3942 static void
3943 e1000_alloc_rx_buffers_ps(struct e1000_adapter *adapter,
3944 struct e1000_rx_ring *rx_ring,
3945 int cleaned_count)
3946 {
3947 struct net_device *netdev = adapter->netdev;
3948 struct pci_dev *pdev = adapter->pdev;
3949 union e1000_rx_desc_packet_split *rx_desc;
3950 struct e1000_buffer *buffer_info;
3951 struct e1000_ps_page *ps_page;
3952 struct e1000_ps_page_dma *ps_page_dma;
3953 struct sk_buff *skb;
3954 unsigned int i, j;
3955
3956 i = rx_ring->next_to_use;
3957 buffer_info = &rx_ring->buffer_info[i];
3958 ps_page = &rx_ring->ps_page[i];
3959 ps_page_dma = &rx_ring->ps_page_dma[i];
3960
3961 while (cleaned_count--) {
3962 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
3963
3964 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
3965 if (j < adapter->rx_ps_pages) {
3966 if (likely(!ps_page->ps_page[j])) {
3967 ps_page->ps_page[j] =
3968 alloc_page(GFP_ATOMIC);
3969 if (unlikely(!ps_page->ps_page[j])) {
3970 adapter->alloc_rx_buff_failed++;
3971 goto no_buffers;
3972 }
3973 ps_page_dma->ps_page_dma[j] =
3974 pci_map_page(pdev,
3975 ps_page->ps_page[j],
3976 0, PAGE_SIZE,
3977 PCI_DMA_FROMDEVICE);
3978 }
3979 /* Refresh the desc even if buffer_addrs didn't
3980 * change because each write-back erases
3981 * this info.
3982 */
3983 rx_desc->read.buffer_addr[j+1] =
3984 cpu_to_le64(ps_page_dma->ps_page_dma[j]);
3985 } else
3986 rx_desc->read.buffer_addr[j+1] = ~0;
3987 }
3988
3989 skb = dev_alloc_skb(adapter->rx_ps_bsize0 + NET_IP_ALIGN);
3990
3991 if (unlikely(!skb)) {
3992 adapter->alloc_rx_buff_failed++;
3993 break;
3994 }
3995
3996 /* Make buffer alignment 2 beyond a 16 byte boundary
3997 * this will result in a 16 byte aligned IP header after
3998 * the 14 byte MAC header is removed
3999 */
4000 skb_reserve(skb, NET_IP_ALIGN);
4001
4002 skb->dev = netdev;
4003
4004 buffer_info->skb = skb;
4005 buffer_info->length = adapter->rx_ps_bsize0;
4006 buffer_info->dma = pci_map_single(pdev, skb->data,
4007 adapter->rx_ps_bsize0,
4008 PCI_DMA_FROMDEVICE);
4009
4010 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
4011
4012 if (unlikely(++i == rx_ring->count)) i = 0;
4013 buffer_info = &rx_ring->buffer_info[i];
4014 ps_page = &rx_ring->ps_page[i];
4015 ps_page_dma = &rx_ring->ps_page_dma[i];
4016 }
4017
4018 no_buffers:
4019 if (likely(rx_ring->next_to_use != i)) {
4020 rx_ring->next_to_use = i;
4021 if (unlikely(i-- == 0)) i = (rx_ring->count - 1);
4022
4023 /* Force memory writes to complete before letting h/w
4024 * know there are new descriptors to fetch. (Only
4025 * applicable for weak-ordered memory model archs,
4026 * such as IA-64). */
4027 wmb();
4028 /* Hardware increments by 16 bytes, but packet split
4029 * descriptors are 32 bytes...so we increment tail
4030 * twice as much.
4031 */
4032 writel(i<<1, adapter->hw.hw_addr + rx_ring->rdt);
4033 }
4034 }
4035
4036 /**
4037 * e1000_smartspeed - Workaround for SmartSpeed on 82541 and 82547 controllers.
4038 * @adapter:
4039 **/
4040
4041 static void
4042 e1000_smartspeed(struct e1000_adapter *adapter)
4043 {
4044 uint16_t phy_status;
4045 uint16_t phy_ctrl;
4046
4047 if ((adapter->hw.phy_type != e1000_phy_igp) || !adapter->hw.autoneg ||
4048 !(adapter->hw.autoneg_advertised & ADVERTISE_1000_FULL))
4049 return;
4050
4051 if (adapter->smartspeed == 0) {
4052 /* If Master/Slave config fault is asserted twice,
4053 * we assume back-to-back */
4054 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
4055 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4056 e1000_read_phy_reg(&adapter->hw, PHY_1000T_STATUS, &phy_status);
4057 if (!(phy_status & SR_1000T_MS_CONFIG_FAULT)) return;
4058 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
4059 if (phy_ctrl & CR_1000T_MS_ENABLE) {
4060 phy_ctrl &= ~CR_1000T_MS_ENABLE;
4061 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL,
4062 phy_ctrl);
4063 adapter->smartspeed++;
4064 if (!e1000_phy_setup_autoneg(&adapter->hw) &&
4065 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL,
4066 &phy_ctrl)) {
4067 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4068 MII_CR_RESTART_AUTO_NEG);
4069 e1000_write_phy_reg(&adapter->hw, PHY_CTRL,
4070 phy_ctrl);
4071 }
4072 }
4073 return;
4074 } else if (adapter->smartspeed == E1000_SMARTSPEED_DOWNSHIFT) {
4075 /* If still no link, perhaps using 2/3 pair cable */
4076 e1000_read_phy_reg(&adapter->hw, PHY_1000T_CTRL, &phy_ctrl);
4077 phy_ctrl |= CR_1000T_MS_ENABLE;
4078 e1000_write_phy_reg(&adapter->hw, PHY_1000T_CTRL, phy_ctrl);
4079 if (!e1000_phy_setup_autoneg(&adapter->hw) &&
4080 !e1000_read_phy_reg(&adapter->hw, PHY_CTRL, &phy_ctrl)) {
4081 phy_ctrl |= (MII_CR_AUTO_NEG_EN |
4082 MII_CR_RESTART_AUTO_NEG);
4083 e1000_write_phy_reg(&adapter->hw, PHY_CTRL, phy_ctrl);
4084 }
4085 }
4086 /* Restart process after E1000_SMARTSPEED_MAX iterations */
4087 if (adapter->smartspeed++ == E1000_SMARTSPEED_MAX)
4088 adapter->smartspeed = 0;
4089 }
4090
4091 /**
4092 * e1000_ioctl -
4093 * @netdev:
4094 * @ifreq:
4095 * @cmd:
4096 **/
4097
4098 static int
4099 e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4100 {
4101 switch (cmd) {
4102 case SIOCGMIIPHY:
4103 case SIOCGMIIREG:
4104 case SIOCSMIIREG:
4105 return e1000_mii_ioctl(netdev, ifr, cmd);
4106 default:
4107 return -EOPNOTSUPP;
4108 }
4109 }
4110
4111 /**
4112 * e1000_mii_ioctl -
4113 * @netdev:
4114 * @ifreq:
4115 * @cmd:
4116 **/
4117
4118 static int
4119 e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
4120 {
4121 struct e1000_adapter *adapter = netdev_priv(netdev);
4122 struct mii_ioctl_data *data = if_mii(ifr);
4123 int retval;
4124 uint16_t mii_reg;
4125 uint16_t spddplx;
4126 unsigned long flags;
4127
4128 if (adapter->hw.media_type != e1000_media_type_copper)
4129 return -EOPNOTSUPP;
4130
4131 switch (cmd) {
4132 case SIOCGMIIPHY:
4133 data->phy_id = adapter->hw.phy_addr;
4134 break;
4135 case SIOCGMIIREG:
4136 if (!capable(CAP_NET_ADMIN))
4137 return -EPERM;
4138 spin_lock_irqsave(&adapter->stats_lock, flags);
4139 if (e1000_read_phy_reg(&adapter->hw, data->reg_num & 0x1F,
4140 &data->val_out)) {
4141 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4142 return -EIO;
4143 }
4144 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4145 break;
4146 case SIOCSMIIREG:
4147 if (!capable(CAP_NET_ADMIN))
4148 return -EPERM;
4149 if (data->reg_num & ~(0x1F))
4150 return -EFAULT;
4151 mii_reg = data->val_in;
4152 spin_lock_irqsave(&adapter->stats_lock, flags);
4153 if (e1000_write_phy_reg(&adapter->hw, data->reg_num,
4154 mii_reg)) {
4155 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4156 return -EIO;
4157 }
4158 if (adapter->hw.phy_type == e1000_media_type_copper) {
4159 switch (data->reg_num) {
4160 case PHY_CTRL:
4161 if (mii_reg & MII_CR_POWER_DOWN)
4162 break;
4163 if (mii_reg & MII_CR_AUTO_NEG_EN) {
4164 adapter->hw.autoneg = 1;
4165 adapter->hw.autoneg_advertised = 0x2F;
4166 } else {
4167 if (mii_reg & 0x40)
4168 spddplx = SPEED_1000;
4169 else if (mii_reg & 0x2000)
4170 spddplx = SPEED_100;
4171 else
4172 spddplx = SPEED_10;
4173 spddplx += (mii_reg & 0x100)
4174 ? DUPLEX_FULL :
4175 DUPLEX_HALF;
4176 retval = e1000_set_spd_dplx(adapter,
4177 spddplx);
4178 if (retval) {
4179 spin_unlock_irqrestore(
4180 &adapter->stats_lock,
4181 flags);
4182 return retval;
4183 }
4184 }
4185 if (netif_running(adapter->netdev)) {
4186 e1000_down(adapter);
4187 e1000_up(adapter);
4188 } else
4189 e1000_reset(adapter);
4190 break;
4191 case M88E1000_PHY_SPEC_CTRL:
4192 case M88E1000_EXT_PHY_SPEC_CTRL:
4193 if (e1000_phy_reset(&adapter->hw)) {
4194 spin_unlock_irqrestore(
4195 &adapter->stats_lock, flags);
4196 return -EIO;
4197 }
4198 break;
4199 }
4200 } else {
4201 switch (data->reg_num) {
4202 case PHY_CTRL:
4203 if (mii_reg & MII_CR_POWER_DOWN)
4204 break;
4205 if (netif_running(adapter->netdev)) {
4206 e1000_down(adapter);
4207 e1000_up(adapter);
4208 } else
4209 e1000_reset(adapter);
4210 break;
4211 }
4212 }
4213 spin_unlock_irqrestore(&adapter->stats_lock, flags);
4214 break;
4215 default:
4216 return -EOPNOTSUPP;
4217 }
4218 return E1000_SUCCESS;
4219 }
4220
4221 void
4222 e1000_pci_set_mwi(struct e1000_hw *hw)
4223 {
4224 struct e1000_adapter *adapter = hw->back;
4225 int ret_val = pci_set_mwi(adapter->pdev);
4226
4227 if (ret_val)
4228 DPRINTK(PROBE, ERR, "Error in setting MWI\n");
4229 }
4230
4231 void
4232 e1000_pci_clear_mwi(struct e1000_hw *hw)
4233 {
4234 struct e1000_adapter *adapter = hw->back;
4235
4236 pci_clear_mwi(adapter->pdev);
4237 }
4238
4239 void
4240 e1000_read_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4241 {
4242 struct e1000_adapter *adapter = hw->back;
4243
4244 pci_read_config_word(adapter->pdev, reg, value);
4245 }
4246
4247 void
4248 e1000_write_pci_cfg(struct e1000_hw *hw, uint32_t reg, uint16_t *value)
4249 {
4250 struct e1000_adapter *adapter = hw->back;
4251
4252 pci_write_config_word(adapter->pdev, reg, *value);
4253 }
4254
4255 uint32_t
4256 e1000_io_read(struct e1000_hw *hw, unsigned long port)
4257 {
4258 return inl(port);
4259 }
4260
4261 void
4262 e1000_io_write(struct e1000_hw *hw, unsigned long port, uint32_t value)
4263 {
4264 outl(value, port);
4265 }
4266
4267 static void
4268 e1000_vlan_rx_register(struct net_device *netdev, struct vlan_group *grp)
4269 {
4270 struct e1000_adapter *adapter = netdev_priv(netdev);
4271 uint32_t ctrl, rctl;
4272
4273 e1000_irq_disable(adapter);
4274 adapter->vlgrp = grp;
4275
4276 if (grp) {
4277 /* enable VLAN tag insert/strip */
4278 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4279 ctrl |= E1000_CTRL_VME;
4280 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4281
4282 /* enable VLAN receive filtering */
4283 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4284 rctl |= E1000_RCTL_VFE;
4285 rctl &= ~E1000_RCTL_CFIEN;
4286 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4287 e1000_update_mng_vlan(adapter);
4288 } else {
4289 /* disable VLAN tag insert/strip */
4290 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4291 ctrl &= ~E1000_CTRL_VME;
4292 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4293
4294 /* disable VLAN filtering */
4295 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4296 rctl &= ~E1000_RCTL_VFE;
4297 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4298 if (adapter->mng_vlan_id != (uint16_t)E1000_MNG_VLAN_NONE) {
4299 e1000_vlan_rx_kill_vid(netdev, adapter->mng_vlan_id);
4300 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4301 }
4302 }
4303
4304 e1000_irq_enable(adapter);
4305 }
4306
4307 static void
4308 e1000_vlan_rx_add_vid(struct net_device *netdev, uint16_t vid)
4309 {
4310 struct e1000_adapter *adapter = netdev_priv(netdev);
4311 uint32_t vfta, index;
4312
4313 if ((adapter->hw.mng_cookie.status &
4314 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4315 (vid == adapter->mng_vlan_id))
4316 return;
4317 /* add VID to filter table */
4318 index = (vid >> 5) & 0x7F;
4319 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4320 vfta |= (1 << (vid & 0x1F));
4321 e1000_write_vfta(&adapter->hw, index, vfta);
4322 }
4323
4324 static void
4325 e1000_vlan_rx_kill_vid(struct net_device *netdev, uint16_t vid)
4326 {
4327 struct e1000_adapter *adapter = netdev_priv(netdev);
4328 uint32_t vfta, index;
4329
4330 e1000_irq_disable(adapter);
4331
4332 if (adapter->vlgrp)
4333 adapter->vlgrp->vlan_devices[vid] = NULL;
4334
4335 e1000_irq_enable(adapter);
4336
4337 if ((adapter->hw.mng_cookie.status &
4338 E1000_MNG_DHCP_COOKIE_STATUS_VLAN_SUPPORT) &&
4339 (vid == adapter->mng_vlan_id)) {
4340 /* release control to f/w */
4341 e1000_release_hw_control(adapter);
4342 return;
4343 }
4344
4345 /* remove VID from filter table */
4346 index = (vid >> 5) & 0x7F;
4347 vfta = E1000_READ_REG_ARRAY(&adapter->hw, VFTA, index);
4348 vfta &= ~(1 << (vid & 0x1F));
4349 e1000_write_vfta(&adapter->hw, index, vfta);
4350 }
4351
4352 static void
4353 e1000_restore_vlan(struct e1000_adapter *adapter)
4354 {
4355 e1000_vlan_rx_register(adapter->netdev, adapter->vlgrp);
4356
4357 if (adapter->vlgrp) {
4358 uint16_t vid;
4359 for (vid = 0; vid < VLAN_GROUP_ARRAY_LEN; vid++) {
4360 if (!adapter->vlgrp->vlan_devices[vid])
4361 continue;
4362 e1000_vlan_rx_add_vid(adapter->netdev, vid);
4363 }
4364 }
4365 }
4366
4367 int
4368 e1000_set_spd_dplx(struct e1000_adapter *adapter, uint16_t spddplx)
4369 {
4370 adapter->hw.autoneg = 0;
4371
4372 /* Fiber NICs only allow 1000 gbps Full duplex */
4373 if ((adapter->hw.media_type == e1000_media_type_fiber) &&
4374 spddplx != (SPEED_1000 + DUPLEX_FULL)) {
4375 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4376 return -EINVAL;
4377 }
4378
4379 switch (spddplx) {
4380 case SPEED_10 + DUPLEX_HALF:
4381 adapter->hw.forced_speed_duplex = e1000_10_half;
4382 break;
4383 case SPEED_10 + DUPLEX_FULL:
4384 adapter->hw.forced_speed_duplex = e1000_10_full;
4385 break;
4386 case SPEED_100 + DUPLEX_HALF:
4387 adapter->hw.forced_speed_duplex = e1000_100_half;
4388 break;
4389 case SPEED_100 + DUPLEX_FULL:
4390 adapter->hw.forced_speed_duplex = e1000_100_full;
4391 break;
4392 case SPEED_1000 + DUPLEX_FULL:
4393 adapter->hw.autoneg = 1;
4394 adapter->hw.autoneg_advertised = ADVERTISE_1000_FULL;
4395 break;
4396 case SPEED_1000 + DUPLEX_HALF: /* not supported */
4397 default:
4398 DPRINTK(PROBE, ERR, "Unsupported Speed/Duplex configuration\n");
4399 return -EINVAL;
4400 }
4401 return 0;
4402 }
4403
4404 #ifdef CONFIG_PM
4405 /* Save/restore 16 or 64 dwords of PCI config space depending on which
4406 * bus we're on (PCI(X) vs. PCI-E)
4407 */
4408 #define PCIE_CONFIG_SPACE_LEN 256
4409 #define PCI_CONFIG_SPACE_LEN 64
4410 static int
4411 e1000_pci_save_state(struct e1000_adapter *adapter)
4412 {
4413 struct pci_dev *dev = adapter->pdev;
4414 int size;
4415 int i;
4416
4417 if (adapter->hw.mac_type >= e1000_82571)
4418 size = PCIE_CONFIG_SPACE_LEN;
4419 else
4420 size = PCI_CONFIG_SPACE_LEN;
4421
4422 WARN_ON(adapter->config_space != NULL);
4423
4424 adapter->config_space = kmalloc(size, GFP_KERNEL);
4425 if (!adapter->config_space) {
4426 DPRINTK(PROBE, ERR, "unable to allocate %d bytes\n", size);
4427 return -ENOMEM;
4428 }
4429 for (i = 0; i < (size / 4); i++)
4430 pci_read_config_dword(dev, i * 4, &adapter->config_space[i]);
4431 return 0;
4432 }
4433
4434 static void
4435 e1000_pci_restore_state(struct e1000_adapter *adapter)
4436 {
4437 struct pci_dev *dev = adapter->pdev;
4438 int size;
4439 int i;
4440
4441 if (adapter->config_space == NULL)
4442 return;
4443
4444 if (adapter->hw.mac_type >= e1000_82571)
4445 size = PCIE_CONFIG_SPACE_LEN;
4446 else
4447 size = PCI_CONFIG_SPACE_LEN;
4448 for (i = 0; i < (size / 4); i++)
4449 pci_write_config_dword(dev, i * 4, adapter->config_space[i]);
4450 kfree(adapter->config_space);
4451 adapter->config_space = NULL;
4452 return;
4453 }
4454 #endif /* CONFIG_PM */
4455
4456 static int
4457 e1000_suspend(struct pci_dev *pdev, pm_message_t state)
4458 {
4459 struct net_device *netdev = pci_get_drvdata(pdev);
4460 struct e1000_adapter *adapter = netdev_priv(netdev);
4461 uint32_t ctrl, ctrl_ext, rctl, manc, status;
4462 uint32_t wufc = adapter->wol;
4463 int retval = 0;
4464
4465 netif_device_detach(netdev);
4466
4467 if (netif_running(netdev))
4468 e1000_down(adapter);
4469
4470 #ifdef CONFIG_PM
4471 /* Implement our own version of pci_save_state(pdev) because pci-
4472 * express adapters have 256-byte config spaces. */
4473 retval = e1000_pci_save_state(adapter);
4474 if (retval)
4475 return retval;
4476 #endif
4477
4478 status = E1000_READ_REG(&adapter->hw, STATUS);
4479 if (status & E1000_STATUS_LU)
4480 wufc &= ~E1000_WUFC_LNKC;
4481
4482 if (wufc) {
4483 e1000_setup_rctl(adapter);
4484 e1000_set_multi(netdev);
4485
4486 /* turn on all-multi mode if wake on multicast is enabled */
4487 if (adapter->wol & E1000_WUFC_MC) {
4488 rctl = E1000_READ_REG(&adapter->hw, RCTL);
4489 rctl |= E1000_RCTL_MPE;
4490 E1000_WRITE_REG(&adapter->hw, RCTL, rctl);
4491 }
4492
4493 if (adapter->hw.mac_type >= e1000_82540) {
4494 ctrl = E1000_READ_REG(&adapter->hw, CTRL);
4495 /* advertise wake from D3Cold */
4496 #define E1000_CTRL_ADVD3WUC 0x00100000
4497 /* phy power management enable */
4498 #define E1000_CTRL_EN_PHY_PWR_MGMT 0x00200000
4499 ctrl |= E1000_CTRL_ADVD3WUC |
4500 E1000_CTRL_EN_PHY_PWR_MGMT;
4501 E1000_WRITE_REG(&adapter->hw, CTRL, ctrl);
4502 }
4503
4504 if (adapter->hw.media_type == e1000_media_type_fiber ||
4505 adapter->hw.media_type == e1000_media_type_internal_serdes) {
4506 /* keep the laser running in D3 */
4507 ctrl_ext = E1000_READ_REG(&adapter->hw, CTRL_EXT);
4508 ctrl_ext |= E1000_CTRL_EXT_SDP7_DATA;
4509 E1000_WRITE_REG(&adapter->hw, CTRL_EXT, ctrl_ext);
4510 }
4511
4512 /* Allow time for pending master requests to run */
4513 e1000_disable_pciex_master(&adapter->hw);
4514
4515 E1000_WRITE_REG(&adapter->hw, WUC, E1000_WUC_PME_EN);
4516 E1000_WRITE_REG(&adapter->hw, WUFC, wufc);
4517 retval = pci_enable_wake(pdev, PCI_D3hot, 1);
4518 if (retval)
4519 DPRINTK(PROBE, ERR, "Error enabling D3 wake\n");
4520 retval = pci_enable_wake(pdev, PCI_D3cold, 1);
4521 if (retval)
4522 DPRINTK(PROBE, ERR, "Error enabling D3 cold wake\n");
4523 } else {
4524 E1000_WRITE_REG(&adapter->hw, WUC, 0);
4525 E1000_WRITE_REG(&adapter->hw, WUFC, 0);
4526 retval = pci_enable_wake(pdev, PCI_D3hot, 0);
4527 if (retval)
4528 DPRINTK(PROBE, ERR, "Error enabling D3 wake\n");
4529 retval = pci_enable_wake(pdev, PCI_D3cold, 0);
4530 if (retval)
4531 DPRINTK(PROBE, ERR, "Error enabling D3 cold wake\n");
4532 }
4533
4534 if (adapter->hw.mac_type >= e1000_82540 &&
4535 adapter->hw.media_type == e1000_media_type_copper) {
4536 manc = E1000_READ_REG(&adapter->hw, MANC);
4537 if (manc & E1000_MANC_SMBUS_EN) {
4538 manc |= E1000_MANC_ARP_EN;
4539 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4540 retval = pci_enable_wake(pdev, PCI_D3hot, 1);
4541 if (retval)
4542 DPRINTK(PROBE, ERR, "Error enabling D3 wake\n");
4543 retval = pci_enable_wake(pdev, PCI_D3cold, 1);
4544 if (retval)
4545 DPRINTK(PROBE, ERR,
4546 "Error enabling D3 cold wake\n");
4547 }
4548 }
4549
4550 /* Release control of h/w to f/w. If f/w is AMT enabled, this
4551 * would have already happened in close and is redundant. */
4552 e1000_release_hw_control(adapter);
4553
4554 pci_disable_device(pdev);
4555
4556 retval = pci_set_power_state(pdev, pci_choose_state(pdev, state));
4557 if (retval)
4558 DPRINTK(PROBE, ERR, "Error in setting power state\n");
4559
4560 return 0;
4561 }
4562
4563 #ifdef CONFIG_PM
4564 static int
4565 e1000_resume(struct pci_dev *pdev)
4566 {
4567 struct net_device *netdev = pci_get_drvdata(pdev);
4568 struct e1000_adapter *adapter = netdev_priv(netdev);
4569 int retval;
4570 uint32_t manc, ret_val;
4571
4572 retval = pci_set_power_state(pdev, PCI_D0);
4573 if (retval)
4574 DPRINTK(PROBE, ERR, "Error in setting power state\n");
4575 e1000_pci_restore_state(adapter);
4576 ret_val = pci_enable_device(pdev);
4577 pci_set_master(pdev);
4578
4579 retval = pci_enable_wake(pdev, PCI_D3hot, 0);
4580 if (retval)
4581 DPRINTK(PROBE, ERR, "Error enabling D3 wake\n");
4582 retval = pci_enable_wake(pdev, PCI_D3cold, 0);
4583 if (retval)
4584 DPRINTK(PROBE, ERR, "Error enabling D3 cold wake\n");
4585
4586 e1000_reset(adapter);
4587 E1000_WRITE_REG(&adapter->hw, WUS, ~0);
4588
4589 if (netif_running(netdev))
4590 e1000_up(adapter);
4591
4592 netif_device_attach(netdev);
4593
4594 if (adapter->hw.mac_type >= e1000_82540 &&
4595 adapter->hw.media_type == e1000_media_type_copper) {
4596 manc = E1000_READ_REG(&adapter->hw, MANC);
4597 manc &= ~(E1000_MANC_ARP_EN);
4598 E1000_WRITE_REG(&adapter->hw, MANC, manc);
4599 }
4600
4601 /* If the controller is 82573 and f/w is AMT, do not set
4602 * DRV_LOAD until the interface is up. For all other cases,
4603 * let the f/w know that the h/w is now under the control
4604 * of the driver. */
4605 if (adapter->hw.mac_type != e1000_82573 ||
4606 !e1000_check_mng_mode(&adapter->hw))
4607 e1000_get_hw_control(adapter);
4608
4609 return 0;
4610 }
4611 #endif
4612
4613 static void e1000_shutdown(struct pci_dev *pdev)
4614 {
4615 e1000_suspend(pdev, PMSG_SUSPEND);
4616 }
4617
4618 #ifdef CONFIG_NET_POLL_CONTROLLER
4619 /*
4620 * Polling 'interrupt' - used by things like netconsole to send skbs
4621 * without having to re-enable interrupts. It's not called while
4622 * the interrupt routine is executing.
4623 */
4624 static void
4625 e1000_netpoll(struct net_device *netdev)
4626 {
4627 struct e1000_adapter *adapter = netdev_priv(netdev);
4628 disable_irq(adapter->pdev->irq);
4629 e1000_intr(adapter->pdev->irq, netdev, NULL);
4630 e1000_clean_tx_irq(adapter, adapter->tx_ring);
4631 #ifndef CONFIG_E1000_NAPI
4632 adapter->clean_rx(adapter, adapter->rx_ring);
4633 #endif
4634 enable_irq(adapter->pdev->irq);
4635 }
4636 #endif
4637
4638 /* e1000_main.c */