blob: 2c34d45354fe9322d939471cf22cca4fc136a47d [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 1999 - 2018 Intel Corporation. */
3
4#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5
6#include <linux/module.h>
7#include <linux/types.h>
8#include <linux/init.h>
9#include <linux/pci.h>
10#include <linux/vmalloc.h>
11#include <linux/pagemap.h>
12#include <linux/delay.h>
13#include <linux/netdevice.h>
14#include <linux/interrupt.h>
15#include <linux/tcp.h>
16#include <linux/ipv6.h>
17#include <linux/slab.h>
18#include <net/checksum.h>
19#include <net/ip6_checksum.h>
20#include <linux/ethtool.h>
21#include <linux/if_vlan.h>
22#include <linux/cpu.h>
23#include <linux/smp.h>
24#include <linux/pm_qos.h>
25#include <linux/pm_runtime.h>
26#include <linux/aer.h>
27#include <linux/prefetch.h>
28
29#include "e1000.h"
30
31#define DRV_EXTRAVERSION "-k"
32
33#define DRV_VERSION "3.2.6" DRV_EXTRAVERSION
34char e1000e_driver_name[] = "e1000e";
35const char e1000e_driver_version[] = DRV_VERSION;
36
37#define DEFAULT_MSG_ENABLE (NETIF_MSG_DRV|NETIF_MSG_PROBE|NETIF_MSG_LINK)
38static int debug = -1;
39module_param(debug, int, 0);
40MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
41
42static const struct e1000_info *e1000_info_tbl[] = {
43 [board_82571] = &e1000_82571_info,
44 [board_82572] = &e1000_82572_info,
45 [board_82573] = &e1000_82573_info,
46 [board_82574] = &e1000_82574_info,
47 [board_82583] = &e1000_82583_info,
48 [board_80003es2lan] = &e1000_es2_info,
49 [board_ich8lan] = &e1000_ich8_info,
50 [board_ich9lan] = &e1000_ich9_info,
51 [board_ich10lan] = &e1000_ich10_info,
52 [board_pchlan] = &e1000_pch_info,
53 [board_pch2lan] = &e1000_pch2_info,
54 [board_pch_lpt] = &e1000_pch_lpt_info,
55 [board_pch_spt] = &e1000_pch_spt_info,
56 [board_pch_cnp] = &e1000_pch_cnp_info,
57};
58
59struct e1000_reg_info {
60 u32 ofs;
61 char *name;
62};
63
64static const struct e1000_reg_info e1000_reg_info_tbl[] = {
65 /* General Registers */
66 {E1000_CTRL, "CTRL"},
67 {E1000_STATUS, "STATUS"},
68 {E1000_CTRL_EXT, "CTRL_EXT"},
69
70 /* Interrupt Registers */
71 {E1000_ICR, "ICR"},
72
73 /* Rx Registers */
74 {E1000_RCTL, "RCTL"},
75 {E1000_RDLEN(0), "RDLEN"},
76 {E1000_RDH(0), "RDH"},
77 {E1000_RDT(0), "RDT"},
78 {E1000_RDTR, "RDTR"},
79 {E1000_RXDCTL(0), "RXDCTL"},
80 {E1000_ERT, "ERT"},
81 {E1000_RDBAL(0), "RDBAL"},
82 {E1000_RDBAH(0), "RDBAH"},
83 {E1000_RDFH, "RDFH"},
84 {E1000_RDFT, "RDFT"},
85 {E1000_RDFHS, "RDFHS"},
86 {E1000_RDFTS, "RDFTS"},
87 {E1000_RDFPC, "RDFPC"},
88
89 /* Tx Registers */
90 {E1000_TCTL, "TCTL"},
91 {E1000_TDBAL(0), "TDBAL"},
92 {E1000_TDBAH(0), "TDBAH"},
93 {E1000_TDLEN(0), "TDLEN"},
94 {E1000_TDH(0), "TDH"},
95 {E1000_TDT(0), "TDT"},
96 {E1000_TIDV, "TIDV"},
97 {E1000_TXDCTL(0), "TXDCTL"},
98 {E1000_TADV, "TADV"},
99 {E1000_TARC(0), "TARC"},
100 {E1000_TDFH, "TDFH"},
101 {E1000_TDFT, "TDFT"},
102 {E1000_TDFHS, "TDFHS"},
103 {E1000_TDFTS, "TDFTS"},
104 {E1000_TDFPC, "TDFPC"},
105
106 /* List Terminator */
107 {0, NULL}
108};
109
110/**
111 * __ew32_prepare - prepare to write to MAC CSR register on certain parts
112 * @hw: pointer to the HW structure
113 *
114 * When updating the MAC CSR registers, the Manageability Engine (ME) could
115 * be accessing the registers at the same time. Normally, this is handled in
116 * h/w by an arbiter but on some parts there is a bug that acknowledges Host
117 * accesses later than it should which could result in the register to have
118 * an incorrect value. Workaround this by checking the FWSM register which
119 * has bit 24 set while ME is accessing MAC CSR registers, wait if it is set
120 * and try again a number of times.
121 **/
122static void __ew32_prepare(struct e1000_hw *hw)
123{
124 s32 i = E1000_ICH_FWSM_PCIM2PCI_COUNT;
125
126 while ((er32(FWSM) & E1000_ICH_FWSM_PCIM2PCI) && --i)
127 udelay(50);
128}
129
130void __ew32(struct e1000_hw *hw, unsigned long reg, u32 val)
131{
132 if (hw->adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
133 __ew32_prepare(hw);
134
135 writel(val, hw->hw_addr + reg);
136}
137
138/**
139 * e1000_regdump - register printout routine
140 * @hw: pointer to the HW structure
141 * @reginfo: pointer to the register info table
142 **/
143static void e1000_regdump(struct e1000_hw *hw, struct e1000_reg_info *reginfo)
144{
145 int n = 0;
146 char rname[16];
147 u32 regs[8];
148
149 switch (reginfo->ofs) {
150 case E1000_RXDCTL(0):
151 for (n = 0; n < 2; n++)
152 regs[n] = __er32(hw, E1000_RXDCTL(n));
153 break;
154 case E1000_TXDCTL(0):
155 for (n = 0; n < 2; n++)
156 regs[n] = __er32(hw, E1000_TXDCTL(n));
157 break;
158 case E1000_TARC(0):
159 for (n = 0; n < 2; n++)
160 regs[n] = __er32(hw, E1000_TARC(n));
161 break;
162 default:
163 pr_info("%-15s %08x\n",
164 reginfo->name, __er32(hw, reginfo->ofs));
165 return;
166 }
167
168 snprintf(rname, 16, "%s%s", reginfo->name, "[0-1]");
169 pr_info("%-15s %08x %08x\n", rname, regs[0], regs[1]);
170}
171
172static void e1000e_dump_ps_pages(struct e1000_adapter *adapter,
173 struct e1000_buffer *bi)
174{
175 int i;
176 struct e1000_ps_page *ps_page;
177
178 for (i = 0; i < adapter->rx_ps_pages; i++) {
179 ps_page = &bi->ps_pages[i];
180
181 if (ps_page->page) {
182 pr_info("packet dump for ps_page %d:\n", i);
183 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS,
184 16, 1, page_address(ps_page->page),
185 PAGE_SIZE, true);
186 }
187 }
188}
189
190/**
191 * e1000e_dump - Print registers, Tx-ring and Rx-ring
192 * @adapter: board private structure
193 **/
194static void e1000e_dump(struct e1000_adapter *adapter)
195{
196 struct net_device *netdev = adapter->netdev;
197 struct e1000_hw *hw = &adapter->hw;
198 struct e1000_reg_info *reginfo;
199 struct e1000_ring *tx_ring = adapter->tx_ring;
200 struct e1000_tx_desc *tx_desc;
201 struct my_u0 {
202 __le64 a;
203 __le64 b;
204 } *u0;
205 struct e1000_buffer *buffer_info;
206 struct e1000_ring *rx_ring = adapter->rx_ring;
207 union e1000_rx_desc_packet_split *rx_desc_ps;
208 union e1000_rx_desc_extended *rx_desc;
209 struct my_u1 {
210 __le64 a;
211 __le64 b;
212 __le64 c;
213 __le64 d;
214 } *u1;
215 u32 staterr;
216 int i = 0;
217
218 if (!netif_msg_hw(adapter))
219 return;
220
221 /* Print netdevice Info */
222 if (netdev) {
223 dev_info(&adapter->pdev->dev, "Net device Info\n");
224 pr_info("Device Name state trans_start\n");
225 pr_info("%-15s %016lX %016lX\n", netdev->name,
226 netdev->state, dev_trans_start(netdev));
227 }
228
229 /* Print Registers */
230 dev_info(&adapter->pdev->dev, "Register Dump\n");
231 pr_info(" Register Name Value\n");
232 for (reginfo = (struct e1000_reg_info *)e1000_reg_info_tbl;
233 reginfo->name; reginfo++) {
234 e1000_regdump(hw, reginfo);
235 }
236
237 /* Print Tx Ring Summary */
238 if (!netdev || !netif_running(netdev))
239 return;
240
241 dev_info(&adapter->pdev->dev, "Tx Ring Summary\n");
242 pr_info("Queue [NTU] [NTC] [bi(ntc)->dma ] leng ntw timestamp\n");
243 buffer_info = &tx_ring->buffer_info[tx_ring->next_to_clean];
244 pr_info(" %5d %5X %5X %016llX %04X %3X %016llX\n",
245 0, tx_ring->next_to_use, tx_ring->next_to_clean,
246 (unsigned long long)buffer_info->dma,
247 buffer_info->length,
248 buffer_info->next_to_watch,
249 (unsigned long long)buffer_info->time_stamp);
250
251 /* Print Tx Ring */
252 if (!netif_msg_tx_done(adapter))
253 goto rx_ring_summary;
254
255 dev_info(&adapter->pdev->dev, "Tx Ring Dump\n");
256
257 /* Transmit Descriptor Formats - DEXT[29] is 0 (Legacy) or 1 (Extended)
258 *
259 * Legacy Transmit Descriptor
260 * +--------------------------------------------------------------+
261 * 0 | Buffer Address [63:0] (Reserved on Write Back) |
262 * +--------------------------------------------------------------+
263 * 8 | Special | CSS | Status | CMD | CSO | Length |
264 * +--------------------------------------------------------------+
265 * 63 48 47 36 35 32 31 24 23 16 15 0
266 *
267 * Extended Context Descriptor (DTYP=0x0) for TSO or checksum offload
268 * 63 48 47 40 39 32 31 16 15 8 7 0
269 * +----------------------------------------------------------------+
270 * 0 | TUCSE | TUCS0 | TUCSS | IPCSE | IPCS0 | IPCSS |
271 * +----------------------------------------------------------------+
272 * 8 | MSS | HDRLEN | RSV | STA | TUCMD | DTYP | PAYLEN |
273 * +----------------------------------------------------------------+
274 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
275 *
276 * Extended Data Descriptor (DTYP=0x1)
277 * +----------------------------------------------------------------+
278 * 0 | Buffer Address [63:0] |
279 * +----------------------------------------------------------------+
280 * 8 | VLAN tag | POPTS | Rsvd | Status | Command | DTYP | DTALEN |
281 * +----------------------------------------------------------------+
282 * 63 48 47 40 39 36 35 32 31 24 23 20 19 0
283 */
284 pr_info("Tl[desc] [address 63:0 ] [SpeCssSCmCsLen] [bi->dma ] leng ntw timestamp bi->skb <-- Legacy format\n");
285 pr_info("Tc[desc] [Ce CoCsIpceCoS] [MssHlRSCm0Plen] [bi->dma ] leng ntw timestamp bi->skb <-- Ext Context format\n");
286 pr_info("Td[desc] [address 63:0 ] [VlaPoRSCm1Dlen] [bi->dma ] leng ntw timestamp bi->skb <-- Ext Data format\n");
287 for (i = 0; tx_ring->desc && (i < tx_ring->count); i++) {
288 const char *next_desc;
289 tx_desc = E1000_TX_DESC(*tx_ring, i);
290 buffer_info = &tx_ring->buffer_info[i];
291 u0 = (struct my_u0 *)tx_desc;
292 if (i == tx_ring->next_to_use && i == tx_ring->next_to_clean)
293 next_desc = " NTC/U";
294 else if (i == tx_ring->next_to_use)
295 next_desc = " NTU";
296 else if (i == tx_ring->next_to_clean)
297 next_desc = " NTC";
298 else
299 next_desc = "";
300 pr_info("T%c[0x%03X] %016llX %016llX %016llX %04X %3X %016llX %p%s\n",
301 (!(le64_to_cpu(u0->b) & BIT(29)) ? 'l' :
302 ((le64_to_cpu(u0->b) & BIT(20)) ? 'd' : 'c')),
303 i,
304 (unsigned long long)le64_to_cpu(u0->a),
305 (unsigned long long)le64_to_cpu(u0->b),
306 (unsigned long long)buffer_info->dma,
307 buffer_info->length, buffer_info->next_to_watch,
308 (unsigned long long)buffer_info->time_stamp,
309 buffer_info->skb, next_desc);
310
311 if (netif_msg_pktdata(adapter) && buffer_info->skb)
312 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS,
313 16, 1, buffer_info->skb->data,
314 buffer_info->skb->len, true);
315 }
316
317 /* Print Rx Ring Summary */
318rx_ring_summary:
319 dev_info(&adapter->pdev->dev, "Rx Ring Summary\n");
320 pr_info("Queue [NTU] [NTC]\n");
321 pr_info(" %5d %5X %5X\n",
322 0, rx_ring->next_to_use, rx_ring->next_to_clean);
323
324 /* Print Rx Ring */
325 if (!netif_msg_rx_status(adapter))
326 return;
327
328 dev_info(&adapter->pdev->dev, "Rx Ring Dump\n");
329 switch (adapter->rx_ps_pages) {
330 case 1:
331 case 2:
332 case 3:
333 /* [Extended] Packet Split Receive Descriptor Format
334 *
335 * +-----------------------------------------------------+
336 * 0 | Buffer Address 0 [63:0] |
337 * +-----------------------------------------------------+
338 * 8 | Buffer Address 1 [63:0] |
339 * +-----------------------------------------------------+
340 * 16 | Buffer Address 2 [63:0] |
341 * +-----------------------------------------------------+
342 * 24 | Buffer Address 3 [63:0] |
343 * +-----------------------------------------------------+
344 */
345 pr_info("R [desc] [buffer 0 63:0 ] [buffer 1 63:0 ] [buffer 2 63:0 ] [buffer 3 63:0 ] [bi->dma ] [bi->skb] <-- Ext Pkt Split format\n");
346 /* [Extended] Receive Descriptor (Write-Back) Format
347 *
348 * 63 48 47 32 31 13 12 8 7 4 3 0
349 * +------------------------------------------------------+
350 * 0 | Packet | IP | Rsvd | MRQ | Rsvd | MRQ RSS |
351 * | Checksum | Ident | | Queue | | Type |
352 * +------------------------------------------------------+
353 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
354 * +------------------------------------------------------+
355 * 63 48 47 32 31 20 19 0
356 */
357 pr_info("RWB[desc] [ck ipid mrqhsh] [vl l0 ee es] [ l3 l2 l1 hs] [reserved ] ---------------- [bi->skb] <-- Ext Rx Write-Back format\n");
358 for (i = 0; i < rx_ring->count; i++) {
359 const char *next_desc;
360 buffer_info = &rx_ring->buffer_info[i];
361 rx_desc_ps = E1000_RX_DESC_PS(*rx_ring, i);
362 u1 = (struct my_u1 *)rx_desc_ps;
363 staterr =
364 le32_to_cpu(rx_desc_ps->wb.middle.status_error);
365
366 if (i == rx_ring->next_to_use)
367 next_desc = " NTU";
368 else if (i == rx_ring->next_to_clean)
369 next_desc = " NTC";
370 else
371 next_desc = "";
372
373 if (staterr & E1000_RXD_STAT_DD) {
374 /* Descriptor Done */
375 pr_info("%s[0x%03X] %016llX %016llX %016llX %016llX ---------------- %p%s\n",
376 "RWB", i,
377 (unsigned long long)le64_to_cpu(u1->a),
378 (unsigned long long)le64_to_cpu(u1->b),
379 (unsigned long long)le64_to_cpu(u1->c),
380 (unsigned long long)le64_to_cpu(u1->d),
381 buffer_info->skb, next_desc);
382 } else {
383 pr_info("%s[0x%03X] %016llX %016llX %016llX %016llX %016llX %p%s\n",
384 "R ", i,
385 (unsigned long long)le64_to_cpu(u1->a),
386 (unsigned long long)le64_to_cpu(u1->b),
387 (unsigned long long)le64_to_cpu(u1->c),
388 (unsigned long long)le64_to_cpu(u1->d),
389 (unsigned long long)buffer_info->dma,
390 buffer_info->skb, next_desc);
391
392 if (netif_msg_pktdata(adapter))
393 e1000e_dump_ps_pages(adapter,
394 buffer_info);
395 }
396 }
397 break;
398 default:
399 case 0:
400 /* Extended Receive Descriptor (Read) Format
401 *
402 * +-----------------------------------------------------+
403 * 0 | Buffer Address [63:0] |
404 * +-----------------------------------------------------+
405 * 8 | Reserved |
406 * +-----------------------------------------------------+
407 */
408 pr_info("R [desc] [buf addr 63:0 ] [reserved 63:0 ] [bi->dma ] [bi->skb] <-- Ext (Read) format\n");
409 /* Extended Receive Descriptor (Write-Back) Format
410 *
411 * 63 48 47 32 31 24 23 4 3 0
412 * +------------------------------------------------------+
413 * | RSS Hash | | | |
414 * 0 +-------------------+ Rsvd | Reserved | MRQ RSS |
415 * | Packet | IP | | | Type |
416 * | Checksum | Ident | | | |
417 * +------------------------------------------------------+
418 * 8 | VLAN Tag | Length | Extended Error | Extended Status |
419 * +------------------------------------------------------+
420 * 63 48 47 32 31 20 19 0
421 */
422 pr_info("RWB[desc] [cs ipid mrq] [vt ln xe xs] [bi->skb] <-- Ext (Write-Back) format\n");
423
424 for (i = 0; i < rx_ring->count; i++) {
425 const char *next_desc;
426
427 buffer_info = &rx_ring->buffer_info[i];
428 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
429 u1 = (struct my_u1 *)rx_desc;
430 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
431
432 if (i == rx_ring->next_to_use)
433 next_desc = " NTU";
434 else if (i == rx_ring->next_to_clean)
435 next_desc = " NTC";
436 else
437 next_desc = "";
438
439 if (staterr & E1000_RXD_STAT_DD) {
440 /* Descriptor Done */
441 pr_info("%s[0x%03X] %016llX %016llX ---------------- %p%s\n",
442 "RWB", i,
443 (unsigned long long)le64_to_cpu(u1->a),
444 (unsigned long long)le64_to_cpu(u1->b),
445 buffer_info->skb, next_desc);
446 } else {
447 pr_info("%s[0x%03X] %016llX %016llX %016llX %p%s\n",
448 "R ", i,
449 (unsigned long long)le64_to_cpu(u1->a),
450 (unsigned long long)le64_to_cpu(u1->b),
451 (unsigned long long)buffer_info->dma,
452 buffer_info->skb, next_desc);
453
454 if (netif_msg_pktdata(adapter) &&
455 buffer_info->skb)
456 print_hex_dump(KERN_INFO, "",
457 DUMP_PREFIX_ADDRESS, 16,
458 1,
459 buffer_info->skb->data,
460 adapter->rx_buffer_len,
461 true);
462 }
463 }
464 }
465}
466
467/**
468 * e1000_desc_unused - calculate if we have unused descriptors
469 **/
470static int e1000_desc_unused(struct e1000_ring *ring)
471{
472 if (ring->next_to_clean > ring->next_to_use)
473 return ring->next_to_clean - ring->next_to_use - 1;
474
475 return ring->count + ring->next_to_clean - ring->next_to_use - 1;
476}
477
478/**
479 * e1000e_systim_to_hwtstamp - convert system time value to hw time stamp
480 * @adapter: board private structure
481 * @hwtstamps: time stamp structure to update
482 * @systim: unsigned 64bit system time value.
483 *
484 * Convert the system time value stored in the RX/TXSTMP registers into a
485 * hwtstamp which can be used by the upper level time stamping functions.
486 *
487 * The 'systim_lock' spinlock is used to protect the consistency of the
488 * system time value. This is needed because reading the 64 bit time
489 * value involves reading two 32 bit registers. The first read latches the
490 * value.
491 **/
492static void e1000e_systim_to_hwtstamp(struct e1000_adapter *adapter,
493 struct skb_shared_hwtstamps *hwtstamps,
494 u64 systim)
495{
496 u64 ns;
497 unsigned long flags;
498
499 spin_lock_irqsave(&adapter->systim_lock, flags);
500 ns = timecounter_cyc2time(&adapter->tc, systim);
501 spin_unlock_irqrestore(&adapter->systim_lock, flags);
502
503 memset(hwtstamps, 0, sizeof(*hwtstamps));
504 hwtstamps->hwtstamp = ns_to_ktime(ns);
505}
506
507/**
508 * e1000e_rx_hwtstamp - utility function which checks for Rx time stamp
509 * @adapter: board private structure
510 * @status: descriptor extended error and status field
511 * @skb: particular skb to include time stamp
512 *
513 * If the time stamp is valid, convert it into the timecounter ns value
514 * and store that result into the shhwtstamps structure which is passed
515 * up the network stack.
516 **/
517static void e1000e_rx_hwtstamp(struct e1000_adapter *adapter, u32 status,
518 struct sk_buff *skb)
519{
520 struct e1000_hw *hw = &adapter->hw;
521 u64 rxstmp;
522
523 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP) ||
524 !(status & E1000_RXDEXT_STATERR_TST) ||
525 !(er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
526 return;
527
528 /* The Rx time stamp registers contain the time stamp. No other
529 * received packet will be time stamped until the Rx time stamp
530 * registers are read. Because only one packet can be time stamped
531 * at a time, the register values must belong to this packet and
532 * therefore none of the other additional attributes need to be
533 * compared.
534 */
535 rxstmp = (u64)er32(RXSTMPL);
536 rxstmp |= (u64)er32(RXSTMPH) << 32;
537 e1000e_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), rxstmp);
538
539 adapter->flags2 &= ~FLAG2_CHECK_RX_HWTSTAMP;
540}
541
542/**
543 * e1000_receive_skb - helper function to handle Rx indications
544 * @adapter: board private structure
545 * @staterr: descriptor extended error and status field as written by hardware
546 * @vlan: descriptor vlan field as written by hardware (no le/be conversion)
547 * @skb: pointer to sk_buff to be indicated to stack
548 **/
549static void e1000_receive_skb(struct e1000_adapter *adapter,
550 struct net_device *netdev, struct sk_buff *skb,
551 u32 staterr, __le16 vlan)
552{
553 u16 tag = le16_to_cpu(vlan);
554
555 e1000e_rx_hwtstamp(adapter, staterr, skb);
556
557 skb->protocol = eth_type_trans(skb, netdev);
558
559 if (staterr & E1000_RXD_STAT_VP)
560 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), tag);
561
562 napi_gro_receive(&adapter->napi, skb);
563}
564
565/**
566 * e1000_rx_checksum - Receive Checksum Offload
567 * @adapter: board private structure
568 * @status_err: receive descriptor status and error fields
569 * @csum: receive descriptor csum field
570 * @sk_buff: socket buffer with received data
571 **/
572static void e1000_rx_checksum(struct e1000_adapter *adapter, u32 status_err,
573 struct sk_buff *skb)
574{
575 u16 status = (u16)status_err;
576 u8 errors = (u8)(status_err >> 24);
577
578 skb_checksum_none_assert(skb);
579
580 /* Rx checksum disabled */
581 if (!(adapter->netdev->features & NETIF_F_RXCSUM))
582 return;
583
584 /* Ignore Checksum bit is set */
585 if (status & E1000_RXD_STAT_IXSM)
586 return;
587
588 /* TCP/UDP checksum error bit or IP checksum error bit is set */
589 if (errors & (E1000_RXD_ERR_TCPE | E1000_RXD_ERR_IPE)) {
590 /* let the stack verify checksum errors */
591 adapter->hw_csum_err++;
592 return;
593 }
594
595 /* TCP/UDP Checksum has not been calculated */
596 if (!(status & (E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS)))
597 return;
598
599 /* It must be a TCP or UDP packet with a valid checksum */
600 skb->ip_summed = CHECKSUM_UNNECESSARY;
601 adapter->hw_csum_good++;
602}
603
604static void e1000e_update_rdt_wa(struct e1000_ring *rx_ring, unsigned int i)
605{
606 struct e1000_adapter *adapter = rx_ring->adapter;
607 struct e1000_hw *hw = &adapter->hw;
608
609 __ew32_prepare(hw);
610 writel(i, rx_ring->tail);
611
612 if (unlikely(i != readl(rx_ring->tail))) {
613 u32 rctl = er32(RCTL);
614
615 ew32(RCTL, rctl & ~E1000_RCTL_EN);
616 e_err("ME firmware caused invalid RDT - resetting\n");
617 schedule_work(&adapter->reset_task);
618 }
619}
620
621static void e1000e_update_tdt_wa(struct e1000_ring *tx_ring, unsigned int i)
622{
623 struct e1000_adapter *adapter = tx_ring->adapter;
624 struct e1000_hw *hw = &adapter->hw;
625
626 __ew32_prepare(hw);
627 writel(i, tx_ring->tail);
628
629 if (unlikely(i != readl(tx_ring->tail))) {
630 u32 tctl = er32(TCTL);
631
632 ew32(TCTL, tctl & ~E1000_TCTL_EN);
633 e_err("ME firmware caused invalid TDT - resetting\n");
634 schedule_work(&adapter->reset_task);
635 }
636}
637
638/**
639 * e1000_alloc_rx_buffers - Replace used receive buffers
640 * @rx_ring: Rx descriptor ring
641 **/
642static void e1000_alloc_rx_buffers(struct e1000_ring *rx_ring,
643 int cleaned_count, gfp_t gfp)
644{
645 struct e1000_adapter *adapter = rx_ring->adapter;
646 struct net_device *netdev = adapter->netdev;
647 struct pci_dev *pdev = adapter->pdev;
648 union e1000_rx_desc_extended *rx_desc;
649 struct e1000_buffer *buffer_info;
650 struct sk_buff *skb;
651 unsigned int i;
652 unsigned int bufsz = adapter->rx_buffer_len;
653
654 i = rx_ring->next_to_use;
655 buffer_info = &rx_ring->buffer_info[i];
656
657 while (cleaned_count--) {
658 skb = buffer_info->skb;
659 if (skb) {
660 skb_trim(skb, 0);
661 goto map_skb;
662 }
663
664 skb = __netdev_alloc_skb_ip_align(netdev, bufsz, gfp);
665 if (!skb) {
666 /* Better luck next round */
667 adapter->alloc_rx_buff_failed++;
668 break;
669 }
670
671 buffer_info->skb = skb;
672map_skb:
673 buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
674 adapter->rx_buffer_len,
675 DMA_FROM_DEVICE);
676 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
677 dev_err(&pdev->dev, "Rx DMA map failed\n");
678 adapter->rx_dma_failed++;
679 break;
680 }
681
682 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
683 rx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
684
685 if (unlikely(!(i & (E1000_RX_BUFFER_WRITE - 1)))) {
686 /* Force memory writes to complete before letting h/w
687 * know there are new descriptors to fetch. (Only
688 * applicable for weak-ordered memory model archs,
689 * such as IA-64).
690 */
691 wmb();
692 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
693 e1000e_update_rdt_wa(rx_ring, i);
694 else
695 writel(i, rx_ring->tail);
696 }
697 i++;
698 if (i == rx_ring->count)
699 i = 0;
700 buffer_info = &rx_ring->buffer_info[i];
701 }
702
703 rx_ring->next_to_use = i;
704}
705
706/**
707 * e1000_alloc_rx_buffers_ps - Replace used receive buffers; packet split
708 * @rx_ring: Rx descriptor ring
709 **/
710static void e1000_alloc_rx_buffers_ps(struct e1000_ring *rx_ring,
711 int cleaned_count, gfp_t gfp)
712{
713 struct e1000_adapter *adapter = rx_ring->adapter;
714 struct net_device *netdev = adapter->netdev;
715 struct pci_dev *pdev = adapter->pdev;
716 union e1000_rx_desc_packet_split *rx_desc;
717 struct e1000_buffer *buffer_info;
718 struct e1000_ps_page *ps_page;
719 struct sk_buff *skb;
720 unsigned int i, j;
721
722 i = rx_ring->next_to_use;
723 buffer_info = &rx_ring->buffer_info[i];
724
725 while (cleaned_count--) {
726 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
727
728 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
729 ps_page = &buffer_info->ps_pages[j];
730 if (j >= adapter->rx_ps_pages) {
731 /* all unused desc entries get hw null ptr */
732 rx_desc->read.buffer_addr[j + 1] =
733 ~cpu_to_le64(0);
734 continue;
735 }
736 if (!ps_page->page) {
737 ps_page->page = alloc_page(gfp);
738 if (!ps_page->page) {
739 adapter->alloc_rx_buff_failed++;
740 goto no_buffers;
741 }
742 ps_page->dma = dma_map_page(&pdev->dev,
743 ps_page->page,
744 0, PAGE_SIZE,
745 DMA_FROM_DEVICE);
746 if (dma_mapping_error(&pdev->dev,
747 ps_page->dma)) {
748 dev_err(&adapter->pdev->dev,
749 "Rx DMA page map failed\n");
750 adapter->rx_dma_failed++;
751 goto no_buffers;
752 }
753 }
754 /* Refresh the desc even if buffer_addrs
755 * didn't change because each write-back
756 * erases this info.
757 */
758 rx_desc->read.buffer_addr[j + 1] =
759 cpu_to_le64(ps_page->dma);
760 }
761
762 skb = __netdev_alloc_skb_ip_align(netdev, adapter->rx_ps_bsize0,
763 gfp);
764
765 if (!skb) {
766 adapter->alloc_rx_buff_failed++;
767 break;
768 }
769
770 buffer_info->skb = skb;
771 buffer_info->dma = dma_map_single(&pdev->dev, skb->data,
772 adapter->rx_ps_bsize0,
773 DMA_FROM_DEVICE);
774 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
775 dev_err(&pdev->dev, "Rx DMA map failed\n");
776 adapter->rx_dma_failed++;
777 /* cleanup skb */
778 dev_kfree_skb_any(skb);
779 buffer_info->skb = NULL;
780 break;
781 }
782
783 rx_desc->read.buffer_addr[0] = cpu_to_le64(buffer_info->dma);
784
785 if (unlikely(!(i & (E1000_RX_BUFFER_WRITE - 1)))) {
786 /* Force memory writes to complete before letting h/w
787 * know there are new descriptors to fetch. (Only
788 * applicable for weak-ordered memory model archs,
789 * such as IA-64).
790 */
791 wmb();
792 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
793 e1000e_update_rdt_wa(rx_ring, i << 1);
794 else
795 writel(i << 1, rx_ring->tail);
796 }
797
798 i++;
799 if (i == rx_ring->count)
800 i = 0;
801 buffer_info = &rx_ring->buffer_info[i];
802 }
803
804no_buffers:
805 rx_ring->next_to_use = i;
806}
807
808/**
809 * e1000_alloc_jumbo_rx_buffers - Replace used jumbo receive buffers
810 * @rx_ring: Rx descriptor ring
811 * @cleaned_count: number of buffers to allocate this pass
812 **/
813
814static void e1000_alloc_jumbo_rx_buffers(struct e1000_ring *rx_ring,
815 int cleaned_count, gfp_t gfp)
816{
817 struct e1000_adapter *adapter = rx_ring->adapter;
818 struct net_device *netdev = adapter->netdev;
819 struct pci_dev *pdev = adapter->pdev;
820 union e1000_rx_desc_extended *rx_desc;
821 struct e1000_buffer *buffer_info;
822 struct sk_buff *skb;
823 unsigned int i;
824 unsigned int bufsz = 256 - 16; /* for skb_reserve */
825
826 i = rx_ring->next_to_use;
827 buffer_info = &rx_ring->buffer_info[i];
828
829 while (cleaned_count--) {
830 skb = buffer_info->skb;
831 if (skb) {
832 skb_trim(skb, 0);
833 goto check_page;
834 }
835
836 skb = __netdev_alloc_skb_ip_align(netdev, bufsz, gfp);
837 if (unlikely(!skb)) {
838 /* Better luck next round */
839 adapter->alloc_rx_buff_failed++;
840 break;
841 }
842
843 buffer_info->skb = skb;
844check_page:
845 /* allocate a new page if necessary */
846 if (!buffer_info->page) {
847 buffer_info->page = alloc_page(gfp);
848 if (unlikely(!buffer_info->page)) {
849 adapter->alloc_rx_buff_failed++;
850 break;
851 }
852 }
853
854 if (!buffer_info->dma) {
855 buffer_info->dma = dma_map_page(&pdev->dev,
856 buffer_info->page, 0,
857 PAGE_SIZE,
858 DMA_FROM_DEVICE);
859 if (dma_mapping_error(&pdev->dev, buffer_info->dma)) {
860 adapter->alloc_rx_buff_failed++;
861 break;
862 }
863 }
864
865 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
866 rx_desc->read.buffer_addr = cpu_to_le64(buffer_info->dma);
867
868 if (unlikely(++i == rx_ring->count))
869 i = 0;
870 buffer_info = &rx_ring->buffer_info[i];
871 }
872
873 if (likely(rx_ring->next_to_use != i)) {
874 rx_ring->next_to_use = i;
875 if (unlikely(i-- == 0))
876 i = (rx_ring->count - 1);
877
878 /* Force memory writes to complete before letting h/w
879 * know there are new descriptors to fetch. (Only
880 * applicable for weak-ordered memory model archs,
881 * such as IA-64).
882 */
883 wmb();
884 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
885 e1000e_update_rdt_wa(rx_ring, i);
886 else
887 writel(i, rx_ring->tail);
888 }
889}
890
891static inline void e1000_rx_hash(struct net_device *netdev, __le32 rss,
892 struct sk_buff *skb)
893{
894 if (netdev->features & NETIF_F_RXHASH)
895 skb_set_hash(skb, le32_to_cpu(rss), PKT_HASH_TYPE_L3);
896}
897
898/**
899 * e1000_clean_rx_irq - Send received data up the network stack
900 * @rx_ring: Rx descriptor ring
901 *
902 * the return value indicates whether actual cleaning was done, there
903 * is no guarantee that everything was cleaned
904 **/
905static bool e1000_clean_rx_irq(struct e1000_ring *rx_ring, int *work_done,
906 int work_to_do)
907{
908 struct e1000_adapter *adapter = rx_ring->adapter;
909 struct net_device *netdev = adapter->netdev;
910 struct pci_dev *pdev = adapter->pdev;
911 struct e1000_hw *hw = &adapter->hw;
912 union e1000_rx_desc_extended *rx_desc, *next_rxd;
913 struct e1000_buffer *buffer_info, *next_buffer;
914 u32 length, staterr;
915 unsigned int i;
916 int cleaned_count = 0;
917 bool cleaned = false;
918 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
919
920 i = rx_ring->next_to_clean;
921 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
922 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
923 buffer_info = &rx_ring->buffer_info[i];
924
925 while (staterr & E1000_RXD_STAT_DD) {
926 struct sk_buff *skb;
927
928 if (*work_done >= work_to_do)
929 break;
930 (*work_done)++;
931 dma_rmb(); /* read descriptor and rx_buffer_info after status DD */
932
933 skb = buffer_info->skb;
934 buffer_info->skb = NULL;
935
936 prefetch(skb->data - NET_IP_ALIGN);
937
938 i++;
939 if (i == rx_ring->count)
940 i = 0;
941 next_rxd = E1000_RX_DESC_EXT(*rx_ring, i);
942 prefetch(next_rxd);
943
944 next_buffer = &rx_ring->buffer_info[i];
945
946 cleaned = true;
947 cleaned_count++;
948 dma_unmap_single(&pdev->dev, buffer_info->dma,
949 adapter->rx_buffer_len, DMA_FROM_DEVICE);
950 buffer_info->dma = 0;
951
952 length = le16_to_cpu(rx_desc->wb.upper.length);
953
954 /* !EOP means multiple descriptors were used to store a single
955 * packet, if that's the case we need to toss it. In fact, we
956 * need to toss every packet with the EOP bit clear and the
957 * next frame that _does_ have the EOP bit set, as it is by
958 * definition only a frame fragment
959 */
960 if (unlikely(!(staterr & E1000_RXD_STAT_EOP)))
961 adapter->flags2 |= FLAG2_IS_DISCARDING;
962
963 if (adapter->flags2 & FLAG2_IS_DISCARDING) {
964 /* All receives must fit into a single buffer */
965 e_dbg("Receive packet consumed multiple buffers\n");
966 /* recycle */
967 buffer_info->skb = skb;
968 if (staterr & E1000_RXD_STAT_EOP)
969 adapter->flags2 &= ~FLAG2_IS_DISCARDING;
970 goto next_desc;
971 }
972
973 if (unlikely((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
974 !(netdev->features & NETIF_F_RXALL))) {
975 /* recycle */
976 buffer_info->skb = skb;
977 goto next_desc;
978 }
979
980 /* adjust length to remove Ethernet CRC */
981 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
982 /* If configured to store CRC, don't subtract FCS,
983 * but keep the FCS bytes out of the total_rx_bytes
984 * counter
985 */
986 if (netdev->features & NETIF_F_RXFCS)
987 total_rx_bytes -= 4;
988 else
989 length -= 4;
990 }
991
992 total_rx_bytes += length;
993 total_rx_packets++;
994
995 /* code added for copybreak, this should improve
996 * performance for small packets with large amounts
997 * of reassembly being done in the stack
998 */
999 if (length < copybreak) {
1000 struct sk_buff *new_skb =
1001 napi_alloc_skb(&adapter->napi, length);
1002 if (new_skb) {
1003 skb_copy_to_linear_data_offset(new_skb,
1004 -NET_IP_ALIGN,
1005 (skb->data -
1006 NET_IP_ALIGN),
1007 (length +
1008 NET_IP_ALIGN));
1009 /* save the skb in buffer_info as good */
1010 buffer_info->skb = skb;
1011 skb = new_skb;
1012 }
1013 /* else just continue with the old one */
1014 }
1015 /* end copybreak code */
1016 skb_put(skb, length);
1017
1018 /* Receive Checksum Offload */
1019 e1000_rx_checksum(adapter, staterr, skb);
1020
1021 e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
1022
1023 e1000_receive_skb(adapter, netdev, skb, staterr,
1024 rx_desc->wb.upper.vlan);
1025
1026next_desc:
1027 rx_desc->wb.upper.status_error &= cpu_to_le32(~0xFF);
1028
1029 /* return some buffers to hardware, one at a time is too slow */
1030 if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
1031 adapter->alloc_rx_buf(rx_ring, cleaned_count,
1032 GFP_ATOMIC);
1033 cleaned_count = 0;
1034 }
1035
1036 /* use prefetched values */
1037 rx_desc = next_rxd;
1038 buffer_info = next_buffer;
1039
1040 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1041 }
1042 rx_ring->next_to_clean = i;
1043
1044 cleaned_count = e1000_desc_unused(rx_ring);
1045 if (cleaned_count)
1046 adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
1047
1048 adapter->total_rx_bytes += total_rx_bytes;
1049 adapter->total_rx_packets += total_rx_packets;
1050 return cleaned;
1051}
1052
1053static void e1000_put_txbuf(struct e1000_ring *tx_ring,
1054 struct e1000_buffer *buffer_info,
1055 bool drop)
1056{
1057 struct e1000_adapter *adapter = tx_ring->adapter;
1058
1059 if (buffer_info->dma) {
1060 if (buffer_info->mapped_as_page)
1061 dma_unmap_page(&adapter->pdev->dev, buffer_info->dma,
1062 buffer_info->length, DMA_TO_DEVICE);
1063 else
1064 dma_unmap_single(&adapter->pdev->dev, buffer_info->dma,
1065 buffer_info->length, DMA_TO_DEVICE);
1066 buffer_info->dma = 0;
1067 }
1068 if (buffer_info->skb) {
1069 if (drop)
1070 dev_kfree_skb_any(buffer_info->skb);
1071 else
1072 dev_consume_skb_any(buffer_info->skb);
1073 buffer_info->skb = NULL;
1074 }
1075 buffer_info->time_stamp = 0;
1076}
1077
1078static void e1000_print_hw_hang(struct work_struct *work)
1079{
1080 struct e1000_adapter *adapter = container_of(work,
1081 struct e1000_adapter,
1082 print_hang_task);
1083 struct net_device *netdev = adapter->netdev;
1084 struct e1000_ring *tx_ring = adapter->tx_ring;
1085 unsigned int i = tx_ring->next_to_clean;
1086 unsigned int eop = tx_ring->buffer_info[i].next_to_watch;
1087 struct e1000_tx_desc *eop_desc = E1000_TX_DESC(*tx_ring, eop);
1088 struct e1000_hw *hw = &adapter->hw;
1089 u16 phy_status, phy_1000t_status, phy_ext_status;
1090 u16 pci_status;
1091
1092 if (test_bit(__E1000_DOWN, &adapter->state))
1093 return;
1094
1095 if (!adapter->tx_hang_recheck && (adapter->flags2 & FLAG2_DMA_BURST)) {
1096 /* May be block on write-back, flush and detect again
1097 * flush pending descriptor writebacks to memory
1098 */
1099 ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
1100 /* execute the writes immediately */
1101 e1e_flush();
1102 /* Due to rare timing issues, write to TIDV again to ensure
1103 * the write is successful
1104 */
1105 ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
1106 /* execute the writes immediately */
1107 e1e_flush();
1108 adapter->tx_hang_recheck = true;
1109 return;
1110 }
1111 adapter->tx_hang_recheck = false;
1112
1113 if (er32(TDH(0)) == er32(TDT(0))) {
1114 e_dbg("false hang detected, ignoring\n");
1115 return;
1116 }
1117
1118 /* Real hang detected */
1119 netif_stop_queue(netdev);
1120
1121 e1e_rphy(hw, MII_BMSR, &phy_status);
1122 e1e_rphy(hw, MII_STAT1000, &phy_1000t_status);
1123 e1e_rphy(hw, MII_ESTATUS, &phy_ext_status);
1124
1125 pci_read_config_word(adapter->pdev, PCI_STATUS, &pci_status);
1126
1127 /* detected Hardware unit hang */
1128 e_err("Detected Hardware Unit Hang:\n"
1129 " TDH <%x>\n"
1130 " TDT <%x>\n"
1131 " next_to_use <%x>\n"
1132 " next_to_clean <%x>\n"
1133 "buffer_info[next_to_clean]:\n"
1134 " time_stamp <%lx>\n"
1135 " next_to_watch <%x>\n"
1136 " jiffies <%lx>\n"
1137 " next_to_watch.status <%x>\n"
1138 "MAC Status <%x>\n"
1139 "PHY Status <%x>\n"
1140 "PHY 1000BASE-T Status <%x>\n"
1141 "PHY Extended Status <%x>\n"
1142 "PCI Status <%x>\n",
1143 readl(tx_ring->head), readl(tx_ring->tail), tx_ring->next_to_use,
1144 tx_ring->next_to_clean, tx_ring->buffer_info[eop].time_stamp,
1145 eop, jiffies, eop_desc->upper.fields.status, er32(STATUS),
1146 phy_status, phy_1000t_status, phy_ext_status, pci_status);
1147
1148 e1000e_dump(adapter);
1149
1150 /* Suggest workaround for known h/w issue */
1151 if ((hw->mac.type == e1000_pchlan) && (er32(CTRL) & E1000_CTRL_TFCE))
1152 e_err("Try turning off Tx pause (flow control) via ethtool\n");
1153}
1154
1155/**
1156 * e1000e_tx_hwtstamp_work - check for Tx time stamp
1157 * @work: pointer to work struct
1158 *
1159 * This work function polls the TSYNCTXCTL valid bit to determine when a
1160 * timestamp has been taken for the current stored skb. The timestamp must
1161 * be for this skb because only one such packet is allowed in the queue.
1162 */
1163static void e1000e_tx_hwtstamp_work(struct work_struct *work)
1164{
1165 struct e1000_adapter *adapter = container_of(work, struct e1000_adapter,
1166 tx_hwtstamp_work);
1167 struct e1000_hw *hw = &adapter->hw;
1168
1169 if (er32(TSYNCTXCTL) & E1000_TSYNCTXCTL_VALID) {
1170 struct sk_buff *skb = adapter->tx_hwtstamp_skb;
1171 struct skb_shared_hwtstamps shhwtstamps;
1172 u64 txstmp;
1173
1174 txstmp = er32(TXSTMPL);
1175 txstmp |= (u64)er32(TXSTMPH) << 32;
1176
1177 e1000e_systim_to_hwtstamp(adapter, &shhwtstamps, txstmp);
1178
1179 /* Clear the global tx_hwtstamp_skb pointer and force writes
1180 * prior to notifying the stack of a Tx timestamp.
1181 */
1182 adapter->tx_hwtstamp_skb = NULL;
1183 wmb(); /* force write prior to skb_tstamp_tx */
1184
1185 skb_tstamp_tx(skb, &shhwtstamps);
1186 dev_consume_skb_any(skb);
1187 } else if (time_after(jiffies, adapter->tx_hwtstamp_start
1188 + adapter->tx_timeout_factor * HZ)) {
1189 dev_kfree_skb_any(adapter->tx_hwtstamp_skb);
1190 adapter->tx_hwtstamp_skb = NULL;
1191 adapter->tx_hwtstamp_timeouts++;
1192 e_warn("clearing Tx timestamp hang\n");
1193 } else {
1194 /* reschedule to check later */
1195 schedule_work(&adapter->tx_hwtstamp_work);
1196 }
1197}
1198
1199/**
1200 * e1000_clean_tx_irq - Reclaim resources after transmit completes
1201 * @tx_ring: Tx descriptor ring
1202 *
1203 * the return value indicates whether actual cleaning was done, there
1204 * is no guarantee that everything was cleaned
1205 **/
1206static bool e1000_clean_tx_irq(struct e1000_ring *tx_ring)
1207{
1208 struct e1000_adapter *adapter = tx_ring->adapter;
1209 struct net_device *netdev = adapter->netdev;
1210 struct e1000_hw *hw = &adapter->hw;
1211 struct e1000_tx_desc *tx_desc, *eop_desc;
1212 struct e1000_buffer *buffer_info;
1213 unsigned int i, eop;
1214 unsigned int count = 0;
1215 unsigned int total_tx_bytes = 0, total_tx_packets = 0;
1216 unsigned int bytes_compl = 0, pkts_compl = 0;
1217
1218 i = tx_ring->next_to_clean;
1219 eop = tx_ring->buffer_info[i].next_to_watch;
1220 eop_desc = E1000_TX_DESC(*tx_ring, eop);
1221
1222 while ((eop_desc->upper.data & cpu_to_le32(E1000_TXD_STAT_DD)) &&
1223 (count < tx_ring->count)) {
1224 bool cleaned = false;
1225
1226 dma_rmb(); /* read buffer_info after eop_desc */
1227 for (; !cleaned; count++) {
1228 tx_desc = E1000_TX_DESC(*tx_ring, i);
1229 buffer_info = &tx_ring->buffer_info[i];
1230 cleaned = (i == eop);
1231
1232 if (cleaned) {
1233 total_tx_packets += buffer_info->segs;
1234 total_tx_bytes += buffer_info->bytecount;
1235 if (buffer_info->skb) {
1236 bytes_compl += buffer_info->skb->len;
1237 pkts_compl++;
1238 }
1239 }
1240
1241 e1000_put_txbuf(tx_ring, buffer_info, false);
1242 tx_desc->upper.data = 0;
1243
1244 i++;
1245 if (i == tx_ring->count)
1246 i = 0;
1247 }
1248
1249 if (i == tx_ring->next_to_use)
1250 break;
1251 eop = tx_ring->buffer_info[i].next_to_watch;
1252 eop_desc = E1000_TX_DESC(*tx_ring, eop);
1253 }
1254
1255 tx_ring->next_to_clean = i;
1256
1257 netdev_completed_queue(netdev, pkts_compl, bytes_compl);
1258
1259#define TX_WAKE_THRESHOLD 32
1260 if (count && netif_carrier_ok(netdev) &&
1261 e1000_desc_unused(tx_ring) >= TX_WAKE_THRESHOLD) {
1262 /* Make sure that anybody stopping the queue after this
1263 * sees the new next_to_clean.
1264 */
1265 smp_mb();
1266
1267 if (netif_queue_stopped(netdev) &&
1268 !(test_bit(__E1000_DOWN, &adapter->state))) {
1269 netif_wake_queue(netdev);
1270 ++adapter->restart_queue;
1271 }
1272 }
1273
1274 if (adapter->detect_tx_hung) {
1275 /* Detect a transmit hang in hardware, this serializes the
1276 * check with the clearing of time_stamp and movement of i
1277 */
1278 adapter->detect_tx_hung = false;
1279 if (tx_ring->buffer_info[i].time_stamp &&
1280 time_after(jiffies, tx_ring->buffer_info[i].time_stamp
1281 + (adapter->tx_timeout_factor * HZ)) &&
1282 !(er32(STATUS) & E1000_STATUS_TXOFF))
1283 schedule_work(&adapter->print_hang_task);
1284 else
1285 adapter->tx_hang_recheck = false;
1286 }
1287 adapter->total_tx_bytes += total_tx_bytes;
1288 adapter->total_tx_packets += total_tx_packets;
1289 return count < tx_ring->count;
1290}
1291
1292/**
1293 * e1000_clean_rx_irq_ps - Send received data up the network stack; packet split
1294 * @rx_ring: Rx descriptor ring
1295 *
1296 * the return value indicates whether actual cleaning was done, there
1297 * is no guarantee that everything was cleaned
1298 **/
1299static bool e1000_clean_rx_irq_ps(struct e1000_ring *rx_ring, int *work_done,
1300 int work_to_do)
1301{
1302 struct e1000_adapter *adapter = rx_ring->adapter;
1303 struct e1000_hw *hw = &adapter->hw;
1304 union e1000_rx_desc_packet_split *rx_desc, *next_rxd;
1305 struct net_device *netdev = adapter->netdev;
1306 struct pci_dev *pdev = adapter->pdev;
1307 struct e1000_buffer *buffer_info, *next_buffer;
1308 struct e1000_ps_page *ps_page;
1309 struct sk_buff *skb;
1310 unsigned int i, j;
1311 u32 length, staterr;
1312 int cleaned_count = 0;
1313 bool cleaned = false;
1314 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1315
1316 i = rx_ring->next_to_clean;
1317 rx_desc = E1000_RX_DESC_PS(*rx_ring, i);
1318 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
1319 buffer_info = &rx_ring->buffer_info[i];
1320
1321 while (staterr & E1000_RXD_STAT_DD) {
1322 if (*work_done >= work_to_do)
1323 break;
1324 (*work_done)++;
1325 skb = buffer_info->skb;
1326 dma_rmb(); /* read descriptor and rx_buffer_info after status DD */
1327
1328 /* in the packet split case this is header only */
1329 prefetch(skb->data - NET_IP_ALIGN);
1330
1331 i++;
1332 if (i == rx_ring->count)
1333 i = 0;
1334 next_rxd = E1000_RX_DESC_PS(*rx_ring, i);
1335 prefetch(next_rxd);
1336
1337 next_buffer = &rx_ring->buffer_info[i];
1338
1339 cleaned = true;
1340 cleaned_count++;
1341 dma_unmap_single(&pdev->dev, buffer_info->dma,
1342 adapter->rx_ps_bsize0, DMA_FROM_DEVICE);
1343 buffer_info->dma = 0;
1344
1345 /* see !EOP comment in other Rx routine */
1346 if (!(staterr & E1000_RXD_STAT_EOP))
1347 adapter->flags2 |= FLAG2_IS_DISCARDING;
1348
1349 if (adapter->flags2 & FLAG2_IS_DISCARDING) {
1350 e_dbg("Packet Split buffers didn't pick up the full packet\n");
1351 dev_kfree_skb_irq(skb);
1352 if (staterr & E1000_RXD_STAT_EOP)
1353 adapter->flags2 &= ~FLAG2_IS_DISCARDING;
1354 goto next_desc;
1355 }
1356
1357 if (unlikely((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
1358 !(netdev->features & NETIF_F_RXALL))) {
1359 dev_kfree_skb_irq(skb);
1360 goto next_desc;
1361 }
1362
1363 length = le16_to_cpu(rx_desc->wb.middle.length0);
1364
1365 if (!length) {
1366 e_dbg("Last part of the packet spanning multiple descriptors\n");
1367 dev_kfree_skb_irq(skb);
1368 goto next_desc;
1369 }
1370
1371 /* Good Receive */
1372 skb_put(skb, length);
1373
1374 {
1375 /* this looks ugly, but it seems compiler issues make
1376 * it more efficient than reusing j
1377 */
1378 int l1 = le16_to_cpu(rx_desc->wb.upper.length[0]);
1379
1380 /* page alloc/put takes too long and effects small
1381 * packet throughput, so unsplit small packets and
1382 * save the alloc/put only valid in softirq (napi)
1383 * context to call kmap_*
1384 */
1385 if (l1 && (l1 <= copybreak) &&
1386 ((length + l1) <= adapter->rx_ps_bsize0)) {
1387 u8 *vaddr;
1388
1389 ps_page = &buffer_info->ps_pages[0];
1390
1391 /* there is no documentation about how to call
1392 * kmap_atomic, so we can't hold the mapping
1393 * very long
1394 */
1395 dma_sync_single_for_cpu(&pdev->dev,
1396 ps_page->dma,
1397 PAGE_SIZE,
1398 DMA_FROM_DEVICE);
1399 vaddr = kmap_atomic(ps_page->page);
1400 memcpy(skb_tail_pointer(skb), vaddr, l1);
1401 kunmap_atomic(vaddr);
1402 dma_sync_single_for_device(&pdev->dev,
1403 ps_page->dma,
1404 PAGE_SIZE,
1405 DMA_FROM_DEVICE);
1406
1407 /* remove the CRC */
1408 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
1409 if (!(netdev->features & NETIF_F_RXFCS))
1410 l1 -= 4;
1411 }
1412
1413 skb_put(skb, l1);
1414 goto copydone;
1415 } /* if */
1416 }
1417
1418 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
1419 length = le16_to_cpu(rx_desc->wb.upper.length[j]);
1420 if (!length)
1421 break;
1422
1423 ps_page = &buffer_info->ps_pages[j];
1424 dma_unmap_page(&pdev->dev, ps_page->dma, PAGE_SIZE,
1425 DMA_FROM_DEVICE);
1426 ps_page->dma = 0;
1427 skb_fill_page_desc(skb, j, ps_page->page, 0, length);
1428 ps_page->page = NULL;
1429 skb->len += length;
1430 skb->data_len += length;
1431 skb->truesize += PAGE_SIZE;
1432 }
1433
1434 /* strip the ethernet crc, problem is we're using pages now so
1435 * this whole operation can get a little cpu intensive
1436 */
1437 if (!(adapter->flags2 & FLAG2_CRC_STRIPPING)) {
1438 if (!(netdev->features & NETIF_F_RXFCS))
1439 pskb_trim(skb, skb->len - 4);
1440 }
1441
1442copydone:
1443 total_rx_bytes += skb->len;
1444 total_rx_packets++;
1445
1446 e1000_rx_checksum(adapter, staterr, skb);
1447
1448 e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
1449
1450 if (rx_desc->wb.upper.header_status &
1451 cpu_to_le16(E1000_RXDPS_HDRSTAT_HDRSP))
1452 adapter->rx_hdr_split++;
1453
1454 e1000_receive_skb(adapter, netdev, skb, staterr,
1455 rx_desc->wb.middle.vlan);
1456
1457next_desc:
1458 rx_desc->wb.middle.status_error &= cpu_to_le32(~0xFF);
1459 buffer_info->skb = NULL;
1460
1461 /* return some buffers to hardware, one at a time is too slow */
1462 if (cleaned_count >= E1000_RX_BUFFER_WRITE) {
1463 adapter->alloc_rx_buf(rx_ring, cleaned_count,
1464 GFP_ATOMIC);
1465 cleaned_count = 0;
1466 }
1467
1468 /* use prefetched values */
1469 rx_desc = next_rxd;
1470 buffer_info = next_buffer;
1471
1472 staterr = le32_to_cpu(rx_desc->wb.middle.status_error);
1473 }
1474 rx_ring->next_to_clean = i;
1475
1476 cleaned_count = e1000_desc_unused(rx_ring);
1477 if (cleaned_count)
1478 adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
1479
1480 adapter->total_rx_bytes += total_rx_bytes;
1481 adapter->total_rx_packets += total_rx_packets;
1482 return cleaned;
1483}
1484
1485/**
1486 * e1000_consume_page - helper function
1487 **/
1488static void e1000_consume_page(struct e1000_buffer *bi, struct sk_buff *skb,
1489 u16 length)
1490{
1491 bi->page = NULL;
1492 skb->len += length;
1493 skb->data_len += length;
1494 skb->truesize += PAGE_SIZE;
1495}
1496
1497/**
1498 * e1000_clean_jumbo_rx_irq - Send received data up the network stack; legacy
1499 * @adapter: board private structure
1500 *
1501 * the return value indicates whether actual cleaning was done, there
1502 * is no guarantee that everything was cleaned
1503 **/
1504static bool e1000_clean_jumbo_rx_irq(struct e1000_ring *rx_ring, int *work_done,
1505 int work_to_do)
1506{
1507 struct e1000_adapter *adapter = rx_ring->adapter;
1508 struct net_device *netdev = adapter->netdev;
1509 struct pci_dev *pdev = adapter->pdev;
1510 union e1000_rx_desc_extended *rx_desc, *next_rxd;
1511 struct e1000_buffer *buffer_info, *next_buffer;
1512 u32 length, staterr;
1513 unsigned int i;
1514 int cleaned_count = 0;
1515 bool cleaned = false;
1516 unsigned int total_rx_bytes = 0, total_rx_packets = 0;
1517 struct skb_shared_info *shinfo;
1518
1519 i = rx_ring->next_to_clean;
1520 rx_desc = E1000_RX_DESC_EXT(*rx_ring, i);
1521 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1522 buffer_info = &rx_ring->buffer_info[i];
1523
1524 while (staterr & E1000_RXD_STAT_DD) {
1525 struct sk_buff *skb;
1526
1527 if (*work_done >= work_to_do)
1528 break;
1529 (*work_done)++;
1530 dma_rmb(); /* read descriptor and rx_buffer_info after status DD */
1531
1532 skb = buffer_info->skb;
1533 buffer_info->skb = NULL;
1534
1535 ++i;
1536 if (i == rx_ring->count)
1537 i = 0;
1538 next_rxd = E1000_RX_DESC_EXT(*rx_ring, i);
1539 prefetch(next_rxd);
1540
1541 next_buffer = &rx_ring->buffer_info[i];
1542
1543 cleaned = true;
1544 cleaned_count++;
1545 dma_unmap_page(&pdev->dev, buffer_info->dma, PAGE_SIZE,
1546 DMA_FROM_DEVICE);
1547 buffer_info->dma = 0;
1548
1549 length = le16_to_cpu(rx_desc->wb.upper.length);
1550
1551 /* errors is only valid for DD + EOP descriptors */
1552 if (unlikely((staterr & E1000_RXD_STAT_EOP) &&
1553 ((staterr & E1000_RXDEXT_ERR_FRAME_ERR_MASK) &&
1554 !(netdev->features & NETIF_F_RXALL)))) {
1555 /* recycle both page and skb */
1556 buffer_info->skb = skb;
1557 /* an error means any chain goes out the window too */
1558 if (rx_ring->rx_skb_top)
1559 dev_kfree_skb_irq(rx_ring->rx_skb_top);
1560 rx_ring->rx_skb_top = NULL;
1561 goto next_desc;
1562 }
1563#define rxtop (rx_ring->rx_skb_top)
1564 if (!(staterr & E1000_RXD_STAT_EOP)) {
1565 /* this descriptor is only the beginning (or middle) */
1566 if (!rxtop) {
1567 /* this is the beginning of a chain */
1568 rxtop = skb;
1569 skb_fill_page_desc(rxtop, 0, buffer_info->page,
1570 0, length);
1571 } else {
1572 /* this is the middle of a chain */
1573 shinfo = skb_shinfo(rxtop);
1574 skb_fill_page_desc(rxtop, shinfo->nr_frags,
1575 buffer_info->page, 0,
1576 length);
1577 /* re-use the skb, only consumed the page */
1578 buffer_info->skb = skb;
1579 }
1580 e1000_consume_page(buffer_info, rxtop, length);
1581 goto next_desc;
1582 } else {
1583 if (rxtop) {
1584 /* end of the chain */
1585 shinfo = skb_shinfo(rxtop);
1586 skb_fill_page_desc(rxtop, shinfo->nr_frags,
1587 buffer_info->page, 0,
1588 length);
1589 /* re-use the current skb, we only consumed the
1590 * page
1591 */
1592 buffer_info->skb = skb;
1593 skb = rxtop;
1594 rxtop = NULL;
1595 e1000_consume_page(buffer_info, skb, length);
1596 } else {
1597 /* no chain, got EOP, this buf is the packet
1598 * copybreak to save the put_page/alloc_page
1599 */
1600 if (length <= copybreak &&
1601 skb_tailroom(skb) >= length) {
1602 u8 *vaddr;
1603 vaddr = kmap_atomic(buffer_info->page);
1604 memcpy(skb_tail_pointer(skb), vaddr,
1605 length);
1606 kunmap_atomic(vaddr);
1607 /* re-use the page, so don't erase
1608 * buffer_info->page
1609 */
1610 skb_put(skb, length);
1611 } else {
1612 skb_fill_page_desc(skb, 0,
1613 buffer_info->page, 0,
1614 length);
1615 e1000_consume_page(buffer_info, skb,
1616 length);
1617 }
1618 }
1619 }
1620
1621 /* Receive Checksum Offload */
1622 e1000_rx_checksum(adapter, staterr, skb);
1623
1624 e1000_rx_hash(netdev, rx_desc->wb.lower.hi_dword.rss, skb);
1625
1626 /* probably a little skewed due to removing CRC */
1627 total_rx_bytes += skb->len;
1628 total_rx_packets++;
1629
1630 /* eth type trans needs skb->data to point to something */
1631 if (!pskb_may_pull(skb, ETH_HLEN)) {
1632 e_err("pskb_may_pull failed.\n");
1633 dev_kfree_skb_irq(skb);
1634 goto next_desc;
1635 }
1636
1637 e1000_receive_skb(adapter, netdev, skb, staterr,
1638 rx_desc->wb.upper.vlan);
1639
1640next_desc:
1641 rx_desc->wb.upper.status_error &= cpu_to_le32(~0xFF);
1642
1643 /* return some buffers to hardware, one at a time is too slow */
1644 if (unlikely(cleaned_count >= E1000_RX_BUFFER_WRITE)) {
1645 adapter->alloc_rx_buf(rx_ring, cleaned_count,
1646 GFP_ATOMIC);
1647 cleaned_count = 0;
1648 }
1649
1650 /* use prefetched values */
1651 rx_desc = next_rxd;
1652 buffer_info = next_buffer;
1653
1654 staterr = le32_to_cpu(rx_desc->wb.upper.status_error);
1655 }
1656 rx_ring->next_to_clean = i;
1657
1658 cleaned_count = e1000_desc_unused(rx_ring);
1659 if (cleaned_count)
1660 adapter->alloc_rx_buf(rx_ring, cleaned_count, GFP_ATOMIC);
1661
1662 adapter->total_rx_bytes += total_rx_bytes;
1663 adapter->total_rx_packets += total_rx_packets;
1664 return cleaned;
1665}
1666
1667/**
1668 * e1000_clean_rx_ring - Free Rx Buffers per Queue
1669 * @rx_ring: Rx descriptor ring
1670 **/
1671static void e1000_clean_rx_ring(struct e1000_ring *rx_ring)
1672{
1673 struct e1000_adapter *adapter = rx_ring->adapter;
1674 struct e1000_buffer *buffer_info;
1675 struct e1000_ps_page *ps_page;
1676 struct pci_dev *pdev = adapter->pdev;
1677 unsigned int i, j;
1678
1679 /* Free all the Rx ring sk_buffs */
1680 for (i = 0; i < rx_ring->count; i++) {
1681 buffer_info = &rx_ring->buffer_info[i];
1682 if (buffer_info->dma) {
1683 if (adapter->clean_rx == e1000_clean_rx_irq)
1684 dma_unmap_single(&pdev->dev, buffer_info->dma,
1685 adapter->rx_buffer_len,
1686 DMA_FROM_DEVICE);
1687 else if (adapter->clean_rx == e1000_clean_jumbo_rx_irq)
1688 dma_unmap_page(&pdev->dev, buffer_info->dma,
1689 PAGE_SIZE, DMA_FROM_DEVICE);
1690 else if (adapter->clean_rx == e1000_clean_rx_irq_ps)
1691 dma_unmap_single(&pdev->dev, buffer_info->dma,
1692 adapter->rx_ps_bsize0,
1693 DMA_FROM_DEVICE);
1694 buffer_info->dma = 0;
1695 }
1696
1697 if (buffer_info->page) {
1698 put_page(buffer_info->page);
1699 buffer_info->page = NULL;
1700 }
1701
1702 if (buffer_info->skb) {
1703 dev_kfree_skb(buffer_info->skb);
1704 buffer_info->skb = NULL;
1705 }
1706
1707 for (j = 0; j < PS_PAGE_BUFFERS; j++) {
1708 ps_page = &buffer_info->ps_pages[j];
1709 if (!ps_page->page)
1710 break;
1711 dma_unmap_page(&pdev->dev, ps_page->dma, PAGE_SIZE,
1712 DMA_FROM_DEVICE);
1713 ps_page->dma = 0;
1714 put_page(ps_page->page);
1715 ps_page->page = NULL;
1716 }
1717 }
1718
1719 /* there also may be some cached data from a chained receive */
1720 if (rx_ring->rx_skb_top) {
1721 dev_kfree_skb(rx_ring->rx_skb_top);
1722 rx_ring->rx_skb_top = NULL;
1723 }
1724
1725 /* Zero out the descriptor ring */
1726 memset(rx_ring->desc, 0, rx_ring->size);
1727
1728 rx_ring->next_to_clean = 0;
1729 rx_ring->next_to_use = 0;
1730 adapter->flags2 &= ~FLAG2_IS_DISCARDING;
1731}
1732
1733static void e1000e_downshift_workaround(struct work_struct *work)
1734{
1735 struct e1000_adapter *adapter = container_of(work,
1736 struct e1000_adapter,
1737 downshift_task);
1738
1739 if (test_bit(__E1000_DOWN, &adapter->state))
1740 return;
1741
1742 e1000e_gig_downshift_workaround_ich8lan(&adapter->hw);
1743}
1744
1745/**
1746 * e1000_intr_msi - Interrupt Handler
1747 * @irq: interrupt number
1748 * @data: pointer to a network interface device structure
1749 **/
1750static irqreturn_t e1000_intr_msi(int __always_unused irq, void *data)
1751{
1752 struct net_device *netdev = data;
1753 struct e1000_adapter *adapter = netdev_priv(netdev);
1754 struct e1000_hw *hw = &adapter->hw;
1755 u32 icr = er32(ICR);
1756
1757 /* read ICR disables interrupts using IAM */
1758 if (icr & E1000_ICR_LSC) {
1759 hw->mac.get_link_status = true;
1760 /* ICH8 workaround-- Call gig speed drop workaround on cable
1761 * disconnect (LSC) before accessing any PHY registers
1762 */
1763 if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
1764 (!(er32(STATUS) & E1000_STATUS_LU)))
1765 schedule_work(&adapter->downshift_task);
1766
1767 /* 80003ES2LAN workaround-- For packet buffer work-around on
1768 * link down event; disable receives here in the ISR and reset
1769 * adapter in watchdog
1770 */
1771 if (netif_carrier_ok(netdev) &&
1772 adapter->flags & FLAG_RX_NEEDS_RESTART) {
1773 /* disable receives */
1774 u32 rctl = er32(RCTL);
1775
1776 ew32(RCTL, rctl & ~E1000_RCTL_EN);
1777 adapter->flags |= FLAG_RESTART_NOW;
1778 }
1779 /* guard against interrupt when we're going down */
1780 if (!test_bit(__E1000_DOWN, &adapter->state))
1781 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1782 }
1783
1784 /* Reset on uncorrectable ECC error */
1785 if ((icr & E1000_ICR_ECCER) && (hw->mac.type >= e1000_pch_lpt)) {
1786 u32 pbeccsts = er32(PBECCSTS);
1787
1788 adapter->corr_errors +=
1789 pbeccsts & E1000_PBECCSTS_CORR_ERR_CNT_MASK;
1790 adapter->uncorr_errors +=
1791 (pbeccsts & E1000_PBECCSTS_UNCORR_ERR_CNT_MASK) >>
1792 E1000_PBECCSTS_UNCORR_ERR_CNT_SHIFT;
1793
1794 /* Do the reset outside of interrupt context */
1795 schedule_work(&adapter->reset_task);
1796
1797 /* return immediately since reset is imminent */
1798 return IRQ_HANDLED;
1799 }
1800
1801 if (napi_schedule_prep(&adapter->napi)) {
1802 adapter->total_tx_bytes = 0;
1803 adapter->total_tx_packets = 0;
1804 adapter->total_rx_bytes = 0;
1805 adapter->total_rx_packets = 0;
1806 __napi_schedule(&adapter->napi);
1807 }
1808
1809 return IRQ_HANDLED;
1810}
1811
1812/**
1813 * e1000_intr - Interrupt Handler
1814 * @irq: interrupt number
1815 * @data: pointer to a network interface device structure
1816 **/
1817static irqreturn_t e1000_intr(int __always_unused irq, void *data)
1818{
1819 struct net_device *netdev = data;
1820 struct e1000_adapter *adapter = netdev_priv(netdev);
1821 struct e1000_hw *hw = &adapter->hw;
1822 u32 rctl, icr = er32(ICR);
1823
1824 if (!icr || test_bit(__E1000_DOWN, &adapter->state))
1825 return IRQ_NONE; /* Not our interrupt */
1826
1827 /* IMS will not auto-mask if INT_ASSERTED is not set, and if it is
1828 * not set, then the adapter didn't send an interrupt
1829 */
1830 if (!(icr & E1000_ICR_INT_ASSERTED))
1831 return IRQ_NONE;
1832
1833 /* Interrupt Auto-Mask...upon reading ICR,
1834 * interrupts are masked. No need for the
1835 * IMC write
1836 */
1837
1838 if (icr & E1000_ICR_LSC) {
1839 hw->mac.get_link_status = true;
1840 /* ICH8 workaround-- Call gig speed drop workaround on cable
1841 * disconnect (LSC) before accessing any PHY registers
1842 */
1843 if ((adapter->flags & FLAG_LSC_GIG_SPEED_DROP) &&
1844 (!(er32(STATUS) & E1000_STATUS_LU)))
1845 schedule_work(&adapter->downshift_task);
1846
1847 /* 80003ES2LAN workaround--
1848 * For packet buffer work-around on link down event;
1849 * disable receives here in the ISR and
1850 * reset adapter in watchdog
1851 */
1852 if (netif_carrier_ok(netdev) &&
1853 (adapter->flags & FLAG_RX_NEEDS_RESTART)) {
1854 /* disable receives */
1855 rctl = er32(RCTL);
1856 ew32(RCTL, rctl & ~E1000_RCTL_EN);
1857 adapter->flags |= FLAG_RESTART_NOW;
1858 }
1859 /* guard against interrupt when we're going down */
1860 if (!test_bit(__E1000_DOWN, &adapter->state))
1861 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1862 }
1863
1864 /* Reset on uncorrectable ECC error */
1865 if ((icr & E1000_ICR_ECCER) && (hw->mac.type >= e1000_pch_lpt)) {
1866 u32 pbeccsts = er32(PBECCSTS);
1867
1868 adapter->corr_errors +=
1869 pbeccsts & E1000_PBECCSTS_CORR_ERR_CNT_MASK;
1870 adapter->uncorr_errors +=
1871 (pbeccsts & E1000_PBECCSTS_UNCORR_ERR_CNT_MASK) >>
1872 E1000_PBECCSTS_UNCORR_ERR_CNT_SHIFT;
1873
1874 /* Do the reset outside of interrupt context */
1875 schedule_work(&adapter->reset_task);
1876
1877 /* return immediately since reset is imminent */
1878 return IRQ_HANDLED;
1879 }
1880
1881 if (napi_schedule_prep(&adapter->napi)) {
1882 adapter->total_tx_bytes = 0;
1883 adapter->total_tx_packets = 0;
1884 adapter->total_rx_bytes = 0;
1885 adapter->total_rx_packets = 0;
1886 __napi_schedule(&adapter->napi);
1887 }
1888
1889 return IRQ_HANDLED;
1890}
1891
1892static irqreturn_t e1000_msix_other(int __always_unused irq, void *data)
1893{
1894 struct net_device *netdev = data;
1895 struct e1000_adapter *adapter = netdev_priv(netdev);
1896 struct e1000_hw *hw = &adapter->hw;
1897 u32 icr = er32(ICR);
1898
1899 if (icr & adapter->eiac_mask)
1900 ew32(ICS, (icr & adapter->eiac_mask));
1901
1902 if (icr & E1000_ICR_LSC) {
1903 hw->mac.get_link_status = true;
1904 /* guard against interrupt when we're going down */
1905 if (!test_bit(__E1000_DOWN, &adapter->state))
1906 mod_timer(&adapter->watchdog_timer, jiffies + 1);
1907 }
1908
1909 if (!test_bit(__E1000_DOWN, &adapter->state))
1910 ew32(IMS, E1000_IMS_OTHER | IMS_OTHER_MASK);
1911
1912 return IRQ_HANDLED;
1913}
1914
1915static irqreturn_t e1000_intr_msix_tx(int __always_unused irq, void *data)
1916{
1917 struct net_device *netdev = data;
1918 struct e1000_adapter *adapter = netdev_priv(netdev);
1919 struct e1000_hw *hw = &adapter->hw;
1920 struct e1000_ring *tx_ring = adapter->tx_ring;
1921
1922 adapter->total_tx_bytes = 0;
1923 adapter->total_tx_packets = 0;
1924
1925 if (!e1000_clean_tx_irq(tx_ring))
1926 /* Ring was not completely cleaned, so fire another interrupt */
1927 ew32(ICS, tx_ring->ims_val);
1928
1929 if (!test_bit(__E1000_DOWN, &adapter->state))
1930 ew32(IMS, adapter->tx_ring->ims_val);
1931
1932 return IRQ_HANDLED;
1933}
1934
1935static irqreturn_t e1000_intr_msix_rx(int __always_unused irq, void *data)
1936{
1937 struct net_device *netdev = data;
1938 struct e1000_adapter *adapter = netdev_priv(netdev);
1939 struct e1000_ring *rx_ring = adapter->rx_ring;
1940
1941 /* Write the ITR value calculated at the end of the
1942 * previous interrupt.
1943 */
1944 if (rx_ring->set_itr) {
1945 u32 itr = rx_ring->itr_val ?
1946 1000000000 / (rx_ring->itr_val * 256) : 0;
1947
1948 writel(itr, rx_ring->itr_register);
1949 rx_ring->set_itr = 0;
1950 }
1951
1952 if (napi_schedule_prep(&adapter->napi)) {
1953 adapter->total_rx_bytes = 0;
1954 adapter->total_rx_packets = 0;
1955 __napi_schedule(&adapter->napi);
1956 }
1957 return IRQ_HANDLED;
1958}
1959
1960/**
1961 * e1000_configure_msix - Configure MSI-X hardware
1962 *
1963 * e1000_configure_msix sets up the hardware to properly
1964 * generate MSI-X interrupts.
1965 **/
1966static void e1000_configure_msix(struct e1000_adapter *adapter)
1967{
1968 struct e1000_hw *hw = &adapter->hw;
1969 struct e1000_ring *rx_ring = adapter->rx_ring;
1970 struct e1000_ring *tx_ring = adapter->tx_ring;
1971 int vector = 0;
1972 u32 ctrl_ext, ivar = 0;
1973
1974 adapter->eiac_mask = 0;
1975
1976 /* Workaround issue with spurious interrupts on 82574 in MSI-X mode */
1977 if (hw->mac.type == e1000_82574) {
1978 u32 rfctl = er32(RFCTL);
1979
1980 rfctl |= E1000_RFCTL_ACK_DIS;
1981 ew32(RFCTL, rfctl);
1982 }
1983
1984 /* Configure Rx vector */
1985 rx_ring->ims_val = E1000_IMS_RXQ0;
1986 adapter->eiac_mask |= rx_ring->ims_val;
1987 if (rx_ring->itr_val)
1988 writel(1000000000 / (rx_ring->itr_val * 256),
1989 rx_ring->itr_register);
1990 else
1991 writel(1, rx_ring->itr_register);
1992 ivar = E1000_IVAR_INT_ALLOC_VALID | vector;
1993
1994 /* Configure Tx vector */
1995 tx_ring->ims_val = E1000_IMS_TXQ0;
1996 vector++;
1997 if (tx_ring->itr_val)
1998 writel(1000000000 / (tx_ring->itr_val * 256),
1999 tx_ring->itr_register);
2000 else
2001 writel(1, tx_ring->itr_register);
2002 adapter->eiac_mask |= tx_ring->ims_val;
2003 ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 8);
2004
2005 /* set vector for Other Causes, e.g. link changes */
2006 vector++;
2007 ivar |= ((E1000_IVAR_INT_ALLOC_VALID | vector) << 16);
2008 if (rx_ring->itr_val)
2009 writel(1000000000 / (rx_ring->itr_val * 256),
2010 hw->hw_addr + E1000_EITR_82574(vector));
2011 else
2012 writel(1, hw->hw_addr + E1000_EITR_82574(vector));
2013
2014 /* Cause Tx interrupts on every write back */
2015 ivar |= BIT(31);
2016
2017 ew32(IVAR, ivar);
2018
2019 /* enable MSI-X PBA support */
2020 ctrl_ext = er32(CTRL_EXT) & ~E1000_CTRL_EXT_IAME;
2021 ctrl_ext |= E1000_CTRL_EXT_PBA_CLR | E1000_CTRL_EXT_EIAME;
2022 ew32(CTRL_EXT, ctrl_ext);
2023 e1e_flush();
2024}
2025
2026void e1000e_reset_interrupt_capability(struct e1000_adapter *adapter)
2027{
2028 if (adapter->msix_entries) {
2029 pci_disable_msix(adapter->pdev);
2030 kfree(adapter->msix_entries);
2031 adapter->msix_entries = NULL;
2032 } else if (adapter->flags & FLAG_MSI_ENABLED) {
2033 pci_disable_msi(adapter->pdev);
2034 adapter->flags &= ~FLAG_MSI_ENABLED;
2035 }
2036}
2037
2038/**
2039 * e1000e_set_interrupt_capability - set MSI or MSI-X if supported
2040 *
2041 * Attempt to configure interrupts using the best available
2042 * capabilities of the hardware and kernel.
2043 **/
2044void e1000e_set_interrupt_capability(struct e1000_adapter *adapter)
2045{
2046 int err;
2047 int i;
2048
2049 switch (adapter->int_mode) {
2050 case E1000E_INT_MODE_MSIX:
2051 if (adapter->flags & FLAG_HAS_MSIX) {
2052 adapter->num_vectors = 3; /* RxQ0, TxQ0 and other */
2053 adapter->msix_entries = kcalloc(adapter->num_vectors,
2054 sizeof(struct
2055 msix_entry),
2056 GFP_KERNEL);
2057 if (adapter->msix_entries) {
2058 struct e1000_adapter *a = adapter;
2059
2060 for (i = 0; i < adapter->num_vectors; i++)
2061 adapter->msix_entries[i].entry = i;
2062
2063 err = pci_enable_msix_range(a->pdev,
2064 a->msix_entries,
2065 a->num_vectors,
2066 a->num_vectors);
2067 if (err > 0)
2068 return;
2069 }
2070 /* MSI-X failed, so fall through and try MSI */
2071 e_err("Failed to initialize MSI-X interrupts. Falling back to MSI interrupts.\n");
2072 e1000e_reset_interrupt_capability(adapter);
2073 }
2074 adapter->int_mode = E1000E_INT_MODE_MSI;
2075 /* Fall through */
2076 case E1000E_INT_MODE_MSI:
2077 if (!pci_enable_msi(adapter->pdev)) {
2078 adapter->flags |= FLAG_MSI_ENABLED;
2079 } else {
2080 adapter->int_mode = E1000E_INT_MODE_LEGACY;
2081 e_err("Failed to initialize MSI interrupts. Falling back to legacy interrupts.\n");
2082 }
2083 /* Fall through */
2084 case E1000E_INT_MODE_LEGACY:
2085 /* Don't do anything; this is the system default */
2086 break;
2087 }
2088
2089 /* store the number of vectors being used */
2090 adapter->num_vectors = 1;
2091}
2092
2093/**
2094 * e1000_request_msix - Initialize MSI-X interrupts
2095 *
2096 * e1000_request_msix allocates MSI-X vectors and requests interrupts from the
2097 * kernel.
2098 **/
2099static int e1000_request_msix(struct e1000_adapter *adapter)
2100{
2101 struct net_device *netdev = adapter->netdev;
2102 int err = 0, vector = 0;
2103
2104 if (strlen(netdev->name) < (IFNAMSIZ - 5))
2105 snprintf(adapter->rx_ring->name,
2106 sizeof(adapter->rx_ring->name) - 1,
2107 "%.14s-rx-0", netdev->name);
2108 else
2109 memcpy(adapter->rx_ring->name, netdev->name, IFNAMSIZ);
2110 err = request_irq(adapter->msix_entries[vector].vector,
2111 e1000_intr_msix_rx, 0, adapter->rx_ring->name,
2112 netdev);
2113 if (err)
2114 return err;
2115 adapter->rx_ring->itr_register = adapter->hw.hw_addr +
2116 E1000_EITR_82574(vector);
2117 adapter->rx_ring->itr_val = adapter->itr;
2118 vector++;
2119
2120 if (strlen(netdev->name) < (IFNAMSIZ - 5))
2121 snprintf(adapter->tx_ring->name,
2122 sizeof(adapter->tx_ring->name) - 1,
2123 "%.14s-tx-0", netdev->name);
2124 else
2125 memcpy(adapter->tx_ring->name, netdev->name, IFNAMSIZ);
2126 err = request_irq(adapter->msix_entries[vector].vector,
2127 e1000_intr_msix_tx, 0, adapter->tx_ring->name,
2128 netdev);
2129 if (err)
2130 return err;
2131 adapter->tx_ring->itr_register = adapter->hw.hw_addr +
2132 E1000_EITR_82574(vector);
2133 adapter->tx_ring->itr_val = adapter->itr;
2134 vector++;
2135
2136 err = request_irq(adapter->msix_entries[vector].vector,
2137 e1000_msix_other, 0, netdev->name, netdev);
2138 if (err)
2139 return err;
2140
2141 e1000_configure_msix(adapter);
2142
2143 return 0;
2144}
2145
2146/**
2147 * e1000_request_irq - initialize interrupts
2148 *
2149 * Attempts to configure interrupts using the best available
2150 * capabilities of the hardware and kernel.
2151 **/
2152static int e1000_request_irq(struct e1000_adapter *adapter)
2153{
2154 struct net_device *netdev = adapter->netdev;
2155 int err;
2156
2157 if (adapter->msix_entries) {
2158 err = e1000_request_msix(adapter);
2159 if (!err)
2160 return err;
2161 /* fall back to MSI */
2162 e1000e_reset_interrupt_capability(adapter);
2163 adapter->int_mode = E1000E_INT_MODE_MSI;
2164 e1000e_set_interrupt_capability(adapter);
2165 }
2166 if (adapter->flags & FLAG_MSI_ENABLED) {
2167 err = request_irq(adapter->pdev->irq, e1000_intr_msi, 0,
2168 netdev->name, netdev);
2169 if (!err)
2170 return err;
2171
2172 /* fall back to legacy interrupt */
2173 e1000e_reset_interrupt_capability(adapter);
2174 adapter->int_mode = E1000E_INT_MODE_LEGACY;
2175 }
2176
2177 err = request_irq(adapter->pdev->irq, e1000_intr, IRQF_SHARED,
2178 netdev->name, netdev);
2179 if (err)
2180 e_err("Unable to allocate interrupt, Error: %d\n", err);
2181
2182 return err;
2183}
2184
2185static void e1000_free_irq(struct e1000_adapter *adapter)
2186{
2187 struct net_device *netdev = adapter->netdev;
2188
2189 if (adapter->msix_entries) {
2190 int vector = 0;
2191
2192 free_irq(adapter->msix_entries[vector].vector, netdev);
2193 vector++;
2194
2195 free_irq(adapter->msix_entries[vector].vector, netdev);
2196 vector++;
2197
2198 /* Other Causes interrupt vector */
2199 free_irq(adapter->msix_entries[vector].vector, netdev);
2200 return;
2201 }
2202
2203 free_irq(adapter->pdev->irq, netdev);
2204}
2205
2206/**
2207 * e1000_irq_disable - Mask off interrupt generation on the NIC
2208 **/
2209static void e1000_irq_disable(struct e1000_adapter *adapter)
2210{
2211 struct e1000_hw *hw = &adapter->hw;
2212
2213 ew32(IMC, ~0);
2214 if (adapter->msix_entries)
2215 ew32(EIAC_82574, 0);
2216 e1e_flush();
2217
2218 if (adapter->msix_entries) {
2219 int i;
2220
2221 for (i = 0; i < adapter->num_vectors; i++)
2222 synchronize_irq(adapter->msix_entries[i].vector);
2223 } else {
2224 synchronize_irq(adapter->pdev->irq);
2225 }
2226}
2227
2228/**
2229 * e1000_irq_enable - Enable default interrupt generation settings
2230 **/
2231static void e1000_irq_enable(struct e1000_adapter *adapter)
2232{
2233 struct e1000_hw *hw = &adapter->hw;
2234
2235 if (adapter->msix_entries) {
2236 ew32(EIAC_82574, adapter->eiac_mask & E1000_EIAC_MASK_82574);
2237 ew32(IMS, adapter->eiac_mask | E1000_IMS_OTHER |
2238 IMS_OTHER_MASK);
2239 } else if (hw->mac.type >= e1000_pch_lpt) {
2240 ew32(IMS, IMS_ENABLE_MASK | E1000_IMS_ECCER);
2241 } else {
2242 ew32(IMS, IMS_ENABLE_MASK);
2243 }
2244 e1e_flush();
2245}
2246
2247/**
2248 * e1000e_get_hw_control - get control of the h/w from f/w
2249 * @adapter: address of board private structure
2250 *
2251 * e1000e_get_hw_control sets {CTRL_EXT|SWSM}:DRV_LOAD bit.
2252 * For ASF and Pass Through versions of f/w this means that
2253 * the driver is loaded. For AMT version (only with 82573)
2254 * of the f/w this means that the network i/f is open.
2255 **/
2256void e1000e_get_hw_control(struct e1000_adapter *adapter)
2257{
2258 struct e1000_hw *hw = &adapter->hw;
2259 u32 ctrl_ext;
2260 u32 swsm;
2261
2262 /* Let firmware know the driver has taken over */
2263 if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
2264 swsm = er32(SWSM);
2265 ew32(SWSM, swsm | E1000_SWSM_DRV_LOAD);
2266 } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
2267 ctrl_ext = er32(CTRL_EXT);
2268 ew32(CTRL_EXT, ctrl_ext | E1000_CTRL_EXT_DRV_LOAD);
2269 }
2270}
2271
2272/**
2273 * e1000e_release_hw_control - release control of the h/w to f/w
2274 * @adapter: address of board private structure
2275 *
2276 * e1000e_release_hw_control resets {CTRL_EXT|SWSM}:DRV_LOAD bit.
2277 * For ASF and Pass Through versions of f/w this means that the
2278 * driver is no longer loaded. For AMT version (only with 82573) i
2279 * of the f/w this means that the network i/f is closed.
2280 *
2281 **/
2282void e1000e_release_hw_control(struct e1000_adapter *adapter)
2283{
2284 struct e1000_hw *hw = &adapter->hw;
2285 u32 ctrl_ext;
2286 u32 swsm;
2287
2288 /* Let firmware taken over control of h/w */
2289 if (adapter->flags & FLAG_HAS_SWSM_ON_LOAD) {
2290 swsm = er32(SWSM);
2291 ew32(SWSM, swsm & ~E1000_SWSM_DRV_LOAD);
2292 } else if (adapter->flags & FLAG_HAS_CTRLEXT_ON_LOAD) {
2293 ctrl_ext = er32(CTRL_EXT);
2294 ew32(CTRL_EXT, ctrl_ext & ~E1000_CTRL_EXT_DRV_LOAD);
2295 }
2296}
2297
2298/**
2299 * e1000_alloc_ring_dma - allocate memory for a ring structure
2300 **/
2301static int e1000_alloc_ring_dma(struct e1000_adapter *adapter,
2302 struct e1000_ring *ring)
2303{
2304 struct pci_dev *pdev = adapter->pdev;
2305
2306 ring->desc = dma_alloc_coherent(&pdev->dev, ring->size, &ring->dma,
2307 GFP_KERNEL);
2308 if (!ring->desc)
2309 return -ENOMEM;
2310
2311 return 0;
2312}
2313
2314/**
2315 * e1000e_setup_tx_resources - allocate Tx resources (Descriptors)
2316 * @tx_ring: Tx descriptor ring
2317 *
2318 * Return 0 on success, negative on failure
2319 **/
2320int e1000e_setup_tx_resources(struct e1000_ring *tx_ring)
2321{
2322 struct e1000_adapter *adapter = tx_ring->adapter;
2323 int err = -ENOMEM, size;
2324
2325 size = sizeof(struct e1000_buffer) * tx_ring->count;
2326 tx_ring->buffer_info = vzalloc(size);
2327 if (!tx_ring->buffer_info)
2328 goto err;
2329
2330 /* round up to nearest 4K */
2331 tx_ring->size = tx_ring->count * sizeof(struct e1000_tx_desc);
2332 tx_ring->size = ALIGN(tx_ring->size, 4096);
2333
2334 err = e1000_alloc_ring_dma(adapter, tx_ring);
2335 if (err)
2336 goto err;
2337
2338 tx_ring->next_to_use = 0;
2339 tx_ring->next_to_clean = 0;
2340
2341 return 0;
2342err:
2343 vfree(tx_ring->buffer_info);
2344 e_err("Unable to allocate memory for the transmit descriptor ring\n");
2345 return err;
2346}
2347
2348/**
2349 * e1000e_setup_rx_resources - allocate Rx resources (Descriptors)
2350 * @rx_ring: Rx descriptor ring
2351 *
2352 * Returns 0 on success, negative on failure
2353 **/
2354int e1000e_setup_rx_resources(struct e1000_ring *rx_ring)
2355{
2356 struct e1000_adapter *adapter = rx_ring->adapter;
2357 struct e1000_buffer *buffer_info;
2358 int i, size, desc_len, err = -ENOMEM;
2359
2360 size = sizeof(struct e1000_buffer) * rx_ring->count;
2361 rx_ring->buffer_info = vzalloc(size);
2362 if (!rx_ring->buffer_info)
2363 goto err;
2364
2365 for (i = 0; i < rx_ring->count; i++) {
2366 buffer_info = &rx_ring->buffer_info[i];
2367 buffer_info->ps_pages = kcalloc(PS_PAGE_BUFFERS,
2368 sizeof(struct e1000_ps_page),
2369 GFP_KERNEL);
2370 if (!buffer_info->ps_pages)
2371 goto err_pages;
2372 }
2373
2374 desc_len = sizeof(union e1000_rx_desc_packet_split);
2375
2376 /* Round up to nearest 4K */
2377 rx_ring->size = rx_ring->count * desc_len;
2378 rx_ring->size = ALIGN(rx_ring->size, 4096);
2379
2380 err = e1000_alloc_ring_dma(adapter, rx_ring);
2381 if (err)
2382 goto err_pages;
2383
2384 rx_ring->next_to_clean = 0;
2385 rx_ring->next_to_use = 0;
2386 rx_ring->rx_skb_top = NULL;
2387
2388 return 0;
2389
2390err_pages:
2391 for (i = 0; i < rx_ring->count; i++) {
2392 buffer_info = &rx_ring->buffer_info[i];
2393 kfree(buffer_info->ps_pages);
2394 }
2395err:
2396 vfree(rx_ring->buffer_info);
2397 e_err("Unable to allocate memory for the receive descriptor ring\n");
2398 return err;
2399}
2400
2401/**
2402 * e1000_clean_tx_ring - Free Tx Buffers
2403 * @tx_ring: Tx descriptor ring
2404 **/
2405static void e1000_clean_tx_ring(struct e1000_ring *tx_ring)
2406{
2407 struct e1000_adapter *adapter = tx_ring->adapter;
2408 struct e1000_buffer *buffer_info;
2409 unsigned long size;
2410 unsigned int i;
2411
2412 for (i = 0; i < tx_ring->count; i++) {
2413 buffer_info = &tx_ring->buffer_info[i];
2414 e1000_put_txbuf(tx_ring, buffer_info, false);
2415 }
2416
2417 netdev_reset_queue(adapter->netdev);
2418 size = sizeof(struct e1000_buffer) * tx_ring->count;
2419 memset(tx_ring->buffer_info, 0, size);
2420
2421 memset(tx_ring->desc, 0, tx_ring->size);
2422
2423 tx_ring->next_to_use = 0;
2424 tx_ring->next_to_clean = 0;
2425}
2426
2427/**
2428 * e1000e_free_tx_resources - Free Tx Resources per Queue
2429 * @tx_ring: Tx descriptor ring
2430 *
2431 * Free all transmit software resources
2432 **/
2433void e1000e_free_tx_resources(struct e1000_ring *tx_ring)
2434{
2435 struct e1000_adapter *adapter = tx_ring->adapter;
2436 struct pci_dev *pdev = adapter->pdev;
2437
2438 e1000_clean_tx_ring(tx_ring);
2439
2440 vfree(tx_ring->buffer_info);
2441 tx_ring->buffer_info = NULL;
2442
2443 dma_free_coherent(&pdev->dev, tx_ring->size, tx_ring->desc,
2444 tx_ring->dma);
2445 tx_ring->desc = NULL;
2446}
2447
2448/**
2449 * e1000e_free_rx_resources - Free Rx Resources
2450 * @rx_ring: Rx descriptor ring
2451 *
2452 * Free all receive software resources
2453 **/
2454void e1000e_free_rx_resources(struct e1000_ring *rx_ring)
2455{
2456 struct e1000_adapter *adapter = rx_ring->adapter;
2457 struct pci_dev *pdev = adapter->pdev;
2458 int i;
2459
2460 e1000_clean_rx_ring(rx_ring);
2461
2462 for (i = 0; i < rx_ring->count; i++)
2463 kfree(rx_ring->buffer_info[i].ps_pages);
2464
2465 vfree(rx_ring->buffer_info);
2466 rx_ring->buffer_info = NULL;
2467
2468 dma_free_coherent(&pdev->dev, rx_ring->size, rx_ring->desc,
2469 rx_ring->dma);
2470 rx_ring->desc = NULL;
2471}
2472
2473/**
2474 * e1000_update_itr - update the dynamic ITR value based on statistics
2475 * @adapter: pointer to adapter
2476 * @itr_setting: current adapter->itr
2477 * @packets: the number of packets during this measurement interval
2478 * @bytes: the number of bytes during this measurement interval
2479 *
2480 * Stores a new ITR value based on packets and byte
2481 * counts during the last interrupt. The advantage of per interrupt
2482 * computation is faster updates and more accurate ITR for the current
2483 * traffic pattern. Constants in this function were computed
2484 * based on theoretical maximum wire speed and thresholds were set based
2485 * on testing data as well as attempting to minimize response time
2486 * while increasing bulk throughput. This functionality is controlled
2487 * by the InterruptThrottleRate module parameter.
2488 **/
2489static unsigned int e1000_update_itr(u16 itr_setting, int packets, int bytes)
2490{
2491 unsigned int retval = itr_setting;
2492
2493 if (packets == 0)
2494 return itr_setting;
2495
2496 switch (itr_setting) {
2497 case lowest_latency:
2498 /* handle TSO and jumbo frames */
2499 if (bytes / packets > 8000)
2500 retval = bulk_latency;
2501 else if ((packets < 5) && (bytes > 512))
2502 retval = low_latency;
2503 break;
2504 case low_latency: /* 50 usec aka 20000 ints/s */
2505 if (bytes > 10000) {
2506 /* this if handles the TSO accounting */
2507 if (bytes / packets > 8000)
2508 retval = bulk_latency;
2509 else if ((packets < 10) || ((bytes / packets) > 1200))
2510 retval = bulk_latency;
2511 else if ((packets > 35))
2512 retval = lowest_latency;
2513 } else if (bytes / packets > 2000) {
2514 retval = bulk_latency;
2515 } else if (packets <= 2 && bytes < 512) {
2516 retval = lowest_latency;
2517 }
2518 break;
2519 case bulk_latency: /* 250 usec aka 4000 ints/s */
2520 if (bytes > 25000) {
2521 if (packets > 35)
2522 retval = low_latency;
2523 } else if (bytes < 6000) {
2524 retval = low_latency;
2525 }
2526 break;
2527 }
2528
2529 return retval;
2530}
2531
2532static void e1000_set_itr(struct e1000_adapter *adapter)
2533{
2534 u16 current_itr;
2535 u32 new_itr = adapter->itr;
2536
2537 /* for non-gigabit speeds, just fix the interrupt rate at 4000 */
2538 if (adapter->link_speed != SPEED_1000) {
2539 current_itr = 0;
2540 new_itr = 4000;
2541 goto set_itr_now;
2542 }
2543
2544 if (adapter->flags2 & FLAG2_DISABLE_AIM) {
2545 new_itr = 0;
2546 goto set_itr_now;
2547 }
2548
2549 adapter->tx_itr = e1000_update_itr(adapter->tx_itr,
2550 adapter->total_tx_packets,
2551 adapter->total_tx_bytes);
2552 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2553 if (adapter->itr_setting == 3 && adapter->tx_itr == lowest_latency)
2554 adapter->tx_itr = low_latency;
2555
2556 adapter->rx_itr = e1000_update_itr(adapter->rx_itr,
2557 adapter->total_rx_packets,
2558 adapter->total_rx_bytes);
2559 /* conservative mode (itr 3) eliminates the lowest_latency setting */
2560 if (adapter->itr_setting == 3 && adapter->rx_itr == lowest_latency)
2561 adapter->rx_itr = low_latency;
2562
2563 current_itr = max(adapter->rx_itr, adapter->tx_itr);
2564
2565 /* counts and packets in update_itr are dependent on these numbers */
2566 switch (current_itr) {
2567 case lowest_latency:
2568 new_itr = 70000;
2569 break;
2570 case low_latency:
2571 new_itr = 20000; /* aka hwitr = ~200 */
2572 break;
2573 case bulk_latency:
2574 new_itr = 4000;
2575 break;
2576 default:
2577 break;
2578 }
2579
2580set_itr_now:
2581 if (new_itr != adapter->itr) {
2582 /* this attempts to bias the interrupt rate towards Bulk
2583 * by adding intermediate steps when interrupt rate is
2584 * increasing
2585 */
2586 new_itr = new_itr > adapter->itr ?
2587 min(adapter->itr + (new_itr >> 2), new_itr) : new_itr;
2588 adapter->itr = new_itr;
2589 adapter->rx_ring->itr_val = new_itr;
2590 if (adapter->msix_entries)
2591 adapter->rx_ring->set_itr = 1;
2592 else
2593 e1000e_write_itr(adapter, new_itr);
2594 }
2595}
2596
2597/**
2598 * e1000e_write_itr - write the ITR value to the appropriate registers
2599 * @adapter: address of board private structure
2600 * @itr: new ITR value to program
2601 *
2602 * e1000e_write_itr determines if the adapter is in MSI-X mode
2603 * and, if so, writes the EITR registers with the ITR value.
2604 * Otherwise, it writes the ITR value into the ITR register.
2605 **/
2606void e1000e_write_itr(struct e1000_adapter *adapter, u32 itr)
2607{
2608 struct e1000_hw *hw = &adapter->hw;
2609 u32 new_itr = itr ? 1000000000 / (itr * 256) : 0;
2610
2611 if (adapter->msix_entries) {
2612 int vector;
2613
2614 for (vector = 0; vector < adapter->num_vectors; vector++)
2615 writel(new_itr, hw->hw_addr + E1000_EITR_82574(vector));
2616 } else {
2617 ew32(ITR, new_itr);
2618 }
2619}
2620
2621/**
2622 * e1000_alloc_queues - Allocate memory for all rings
2623 * @adapter: board private structure to initialize
2624 **/
2625static int e1000_alloc_queues(struct e1000_adapter *adapter)
2626{
2627 int size = sizeof(struct e1000_ring);
2628
2629 adapter->tx_ring = kzalloc(size, GFP_KERNEL);
2630 if (!adapter->tx_ring)
2631 goto err;
2632 adapter->tx_ring->count = adapter->tx_ring_count;
2633 adapter->tx_ring->adapter = adapter;
2634
2635 adapter->rx_ring = kzalloc(size, GFP_KERNEL);
2636 if (!adapter->rx_ring)
2637 goto err;
2638 adapter->rx_ring->count = adapter->rx_ring_count;
2639 adapter->rx_ring->adapter = adapter;
2640
2641 return 0;
2642err:
2643 e_err("Unable to allocate memory for queues\n");
2644 kfree(adapter->rx_ring);
2645 kfree(adapter->tx_ring);
2646 return -ENOMEM;
2647}
2648
2649/**
2650 * e1000e_poll - NAPI Rx polling callback
2651 * @napi: struct associated with this polling callback
2652 * @budget: number of packets driver is allowed to process this poll
2653 **/
2654static int e1000e_poll(struct napi_struct *napi, int budget)
2655{
2656 struct e1000_adapter *adapter = container_of(napi, struct e1000_adapter,
2657 napi);
2658 struct e1000_hw *hw = &adapter->hw;
2659 struct net_device *poll_dev = adapter->netdev;
2660 int tx_cleaned = 1, work_done = 0;
2661
2662 adapter = netdev_priv(poll_dev);
2663
2664 if (!adapter->msix_entries ||
2665 (adapter->rx_ring->ims_val & adapter->tx_ring->ims_val))
2666 tx_cleaned = e1000_clean_tx_irq(adapter->tx_ring);
2667
2668 adapter->clean_rx(adapter->rx_ring, &work_done, budget);
2669
2670 if (!tx_cleaned || work_done == budget)
2671 return budget;
2672
2673 /* Exit the polling mode, but don't re-enable interrupts if stack might
2674 * poll us due to busy-polling
2675 */
2676 if (likely(napi_complete_done(napi, work_done))) {
2677 if (adapter->itr_setting & 3)
2678 e1000_set_itr(adapter);
2679 if (!test_bit(__E1000_DOWN, &adapter->state)) {
2680 if (adapter->msix_entries)
2681 ew32(IMS, adapter->rx_ring->ims_val);
2682 else
2683 e1000_irq_enable(adapter);
2684 }
2685 }
2686
2687 return work_done;
2688}
2689
2690static int e1000_vlan_rx_add_vid(struct net_device *netdev,
2691 __always_unused __be16 proto, u16 vid)
2692{
2693 struct e1000_adapter *adapter = netdev_priv(netdev);
2694 struct e1000_hw *hw = &adapter->hw;
2695 u32 vfta, index;
2696
2697 /* don't update vlan cookie if already programmed */
2698 if ((adapter->hw.mng_cookie.status &
2699 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2700 (vid == adapter->mng_vlan_id))
2701 return 0;
2702
2703 /* add VID to filter table */
2704 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2705 index = (vid >> 5) & 0x7F;
2706 vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
2707 vfta |= BIT((vid & 0x1F));
2708 hw->mac.ops.write_vfta(hw, index, vfta);
2709 }
2710
2711 set_bit(vid, adapter->active_vlans);
2712
2713 return 0;
2714}
2715
2716static int e1000_vlan_rx_kill_vid(struct net_device *netdev,
2717 __always_unused __be16 proto, u16 vid)
2718{
2719 struct e1000_adapter *adapter = netdev_priv(netdev);
2720 struct e1000_hw *hw = &adapter->hw;
2721 u32 vfta, index;
2722
2723 if ((adapter->hw.mng_cookie.status &
2724 E1000_MNG_DHCP_COOKIE_STATUS_VLAN) &&
2725 (vid == adapter->mng_vlan_id)) {
2726 /* release control to f/w */
2727 e1000e_release_hw_control(adapter);
2728 return 0;
2729 }
2730
2731 /* remove VID from filter table */
2732 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2733 index = (vid >> 5) & 0x7F;
2734 vfta = E1000_READ_REG_ARRAY(hw, E1000_VFTA, index);
2735 vfta &= ~BIT((vid & 0x1F));
2736 hw->mac.ops.write_vfta(hw, index, vfta);
2737 }
2738
2739 clear_bit(vid, adapter->active_vlans);
2740
2741 return 0;
2742}
2743
2744/**
2745 * e1000e_vlan_filter_disable - helper to disable hw VLAN filtering
2746 * @adapter: board private structure to initialize
2747 **/
2748static void e1000e_vlan_filter_disable(struct e1000_adapter *adapter)
2749{
2750 struct net_device *netdev = adapter->netdev;
2751 struct e1000_hw *hw = &adapter->hw;
2752 u32 rctl;
2753
2754 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2755 /* disable VLAN receive filtering */
2756 rctl = er32(RCTL);
2757 rctl &= ~(E1000_RCTL_VFE | E1000_RCTL_CFIEN);
2758 ew32(RCTL, rctl);
2759
2760 if (adapter->mng_vlan_id != (u16)E1000_MNG_VLAN_NONE) {
2761 e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
2762 adapter->mng_vlan_id);
2763 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
2764 }
2765 }
2766}
2767
2768/**
2769 * e1000e_vlan_filter_enable - helper to enable HW VLAN filtering
2770 * @adapter: board private structure to initialize
2771 **/
2772static void e1000e_vlan_filter_enable(struct e1000_adapter *adapter)
2773{
2774 struct e1000_hw *hw = &adapter->hw;
2775 u32 rctl;
2776
2777 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER) {
2778 /* enable VLAN receive filtering */
2779 rctl = er32(RCTL);
2780 rctl |= E1000_RCTL_VFE;
2781 rctl &= ~E1000_RCTL_CFIEN;
2782 ew32(RCTL, rctl);
2783 }
2784}
2785
2786/**
2787 * e1000e_vlan_strip_disable - helper to disable HW VLAN stripping
2788 * @adapter: board private structure to initialize
2789 **/
2790static void e1000e_vlan_strip_disable(struct e1000_adapter *adapter)
2791{
2792 struct e1000_hw *hw = &adapter->hw;
2793 u32 ctrl;
2794
2795 /* disable VLAN tag insert/strip */
2796 ctrl = er32(CTRL);
2797 ctrl &= ~E1000_CTRL_VME;
2798 ew32(CTRL, ctrl);
2799}
2800
2801/**
2802 * e1000e_vlan_strip_enable - helper to enable HW VLAN stripping
2803 * @adapter: board private structure to initialize
2804 **/
2805static void e1000e_vlan_strip_enable(struct e1000_adapter *adapter)
2806{
2807 struct e1000_hw *hw = &adapter->hw;
2808 u32 ctrl;
2809
2810 /* enable VLAN tag insert/strip */
2811 ctrl = er32(CTRL);
2812 ctrl |= E1000_CTRL_VME;
2813 ew32(CTRL, ctrl);
2814}
2815
2816static void e1000_update_mng_vlan(struct e1000_adapter *adapter)
2817{
2818 struct net_device *netdev = adapter->netdev;
2819 u16 vid = adapter->hw.mng_cookie.vlan_id;
2820 u16 old_vid = adapter->mng_vlan_id;
2821
2822 if (adapter->hw.mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN) {
2823 e1000_vlan_rx_add_vid(netdev, htons(ETH_P_8021Q), vid);
2824 adapter->mng_vlan_id = vid;
2825 }
2826
2827 if ((old_vid != (u16)E1000_MNG_VLAN_NONE) && (vid != old_vid))
2828 e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q), old_vid);
2829}
2830
2831static void e1000_restore_vlan(struct e1000_adapter *adapter)
2832{
2833 u16 vid;
2834
2835 e1000_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), 0);
2836
2837 for_each_set_bit(vid, adapter->active_vlans, VLAN_N_VID)
2838 e1000_vlan_rx_add_vid(adapter->netdev, htons(ETH_P_8021Q), vid);
2839}
2840
2841static void e1000_init_manageability_pt(struct e1000_adapter *adapter)
2842{
2843 struct e1000_hw *hw = &adapter->hw;
2844 u32 manc, manc2h, mdef, i, j;
2845
2846 if (!(adapter->flags & FLAG_MNG_PT_ENABLED))
2847 return;
2848
2849 manc = er32(MANC);
2850
2851 /* enable receiving management packets to the host. this will probably
2852 * generate destination unreachable messages from the host OS, but
2853 * the packets will be handled on SMBUS
2854 */
2855 manc |= E1000_MANC_EN_MNG2HOST;
2856 manc2h = er32(MANC2H);
2857
2858 switch (hw->mac.type) {
2859 default:
2860 manc2h |= (E1000_MANC2H_PORT_623 | E1000_MANC2H_PORT_664);
2861 break;
2862 case e1000_82574:
2863 case e1000_82583:
2864 /* Check if IPMI pass-through decision filter already exists;
2865 * if so, enable it.
2866 */
2867 for (i = 0, j = 0; i < 8; i++) {
2868 mdef = er32(MDEF(i));
2869
2870 /* Ignore filters with anything other than IPMI ports */
2871 if (mdef & ~(E1000_MDEF_PORT_623 | E1000_MDEF_PORT_664))
2872 continue;
2873
2874 /* Enable this decision filter in MANC2H */
2875 if (mdef)
2876 manc2h |= BIT(i);
2877
2878 j |= mdef;
2879 }
2880
2881 if (j == (E1000_MDEF_PORT_623 | E1000_MDEF_PORT_664))
2882 break;
2883
2884 /* Create new decision filter in an empty filter */
2885 for (i = 0, j = 0; i < 8; i++)
2886 if (er32(MDEF(i)) == 0) {
2887 ew32(MDEF(i), (E1000_MDEF_PORT_623 |
2888 E1000_MDEF_PORT_664));
2889 manc2h |= BIT(1);
2890 j++;
2891 break;
2892 }
2893
2894 if (!j)
2895 e_warn("Unable to create IPMI pass-through filter\n");
2896 break;
2897 }
2898
2899 ew32(MANC2H, manc2h);
2900 ew32(MANC, manc);
2901}
2902
2903/**
2904 * e1000_configure_tx - Configure Transmit Unit after Reset
2905 * @adapter: board private structure
2906 *
2907 * Configure the Tx unit of the MAC after a reset.
2908 **/
2909static void e1000_configure_tx(struct e1000_adapter *adapter)
2910{
2911 struct e1000_hw *hw = &adapter->hw;
2912 struct e1000_ring *tx_ring = adapter->tx_ring;
2913 u64 tdba;
2914 u32 tdlen, tctl, tarc;
2915
2916 /* Setup the HW Tx Head and Tail descriptor pointers */
2917 tdba = tx_ring->dma;
2918 tdlen = tx_ring->count * sizeof(struct e1000_tx_desc);
2919 ew32(TDBAL(0), (tdba & DMA_BIT_MASK(32)));
2920 ew32(TDBAH(0), (tdba >> 32));
2921 ew32(TDLEN(0), tdlen);
2922 ew32(TDH(0), 0);
2923 ew32(TDT(0), 0);
2924 tx_ring->head = adapter->hw.hw_addr + E1000_TDH(0);
2925 tx_ring->tail = adapter->hw.hw_addr + E1000_TDT(0);
2926
2927 writel(0, tx_ring->head);
2928 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
2929 e1000e_update_tdt_wa(tx_ring, 0);
2930 else
2931 writel(0, tx_ring->tail);
2932
2933 /* Set the Tx Interrupt Delay register */
2934 ew32(TIDV, adapter->tx_int_delay);
2935 /* Tx irq moderation */
2936 ew32(TADV, adapter->tx_abs_int_delay);
2937
2938 if (adapter->flags2 & FLAG2_DMA_BURST) {
2939 u32 txdctl = er32(TXDCTL(0));
2940
2941 txdctl &= ~(E1000_TXDCTL_PTHRESH | E1000_TXDCTL_HTHRESH |
2942 E1000_TXDCTL_WTHRESH);
2943 /* set up some performance related parameters to encourage the
2944 * hardware to use the bus more efficiently in bursts, depends
2945 * on the tx_int_delay to be enabled,
2946 * wthresh = 1 ==> burst write is disabled to avoid Tx stalls
2947 * hthresh = 1 ==> prefetch when one or more available
2948 * pthresh = 0x1f ==> prefetch if internal cache 31 or less
2949 * BEWARE: this seems to work but should be considered first if
2950 * there are Tx hangs or other Tx related bugs
2951 */
2952 txdctl |= E1000_TXDCTL_DMA_BURST_ENABLE;
2953 ew32(TXDCTL(0), txdctl);
2954 }
2955 /* erratum work around: set txdctl the same for both queues */
2956 ew32(TXDCTL(1), er32(TXDCTL(0)));
2957
2958 /* Program the Transmit Control Register */
2959 tctl = er32(TCTL);
2960 tctl &= ~E1000_TCTL_CT;
2961 tctl |= E1000_TCTL_PSP | E1000_TCTL_RTLC |
2962 (E1000_COLLISION_THRESHOLD << E1000_CT_SHIFT);
2963
2964 if (adapter->flags & FLAG_TARC_SPEED_MODE_BIT) {
2965 tarc = er32(TARC(0));
2966 /* set the speed mode bit, we'll clear it if we're not at
2967 * gigabit link later
2968 */
2969#define SPEED_MODE_BIT BIT(21)
2970 tarc |= SPEED_MODE_BIT;
2971 ew32(TARC(0), tarc);
2972 }
2973
2974 /* errata: program both queues to unweighted RR */
2975 if (adapter->flags & FLAG_TARC_SET_BIT_ZERO) {
2976 tarc = er32(TARC(0));
2977 tarc |= 1;
2978 ew32(TARC(0), tarc);
2979 tarc = er32(TARC(1));
2980 tarc |= 1;
2981 ew32(TARC(1), tarc);
2982 }
2983
2984 /* Setup Transmit Descriptor Settings for eop descriptor */
2985 adapter->txd_cmd = E1000_TXD_CMD_EOP | E1000_TXD_CMD_IFCS;
2986
2987 /* only set IDE if we are delaying interrupts using the timers */
2988 if (adapter->tx_int_delay)
2989 adapter->txd_cmd |= E1000_TXD_CMD_IDE;
2990
2991 /* enable Report Status bit */
2992 adapter->txd_cmd |= E1000_TXD_CMD_RS;
2993
2994 ew32(TCTL, tctl);
2995
2996 hw->mac.ops.config_collision_dist(hw);
2997
2998 /* SPT and KBL Si errata workaround to avoid data corruption */
2999 if (hw->mac.type == e1000_pch_spt) {
3000 u32 reg_val;
3001
3002 reg_val = er32(IOSFPC);
3003 reg_val |= E1000_RCTL_RDMTS_HEX;
3004 ew32(IOSFPC, reg_val);
3005
3006 reg_val = er32(TARC(0));
3007 /* SPT and KBL Si errata workaround to avoid Tx hang.
3008 * Dropping the number of outstanding requests from
3009 * 3 to 2 in order to avoid a buffer overrun.
3010 */
3011 reg_val &= ~E1000_TARC0_CB_MULTIQ_3_REQ;
3012 reg_val |= E1000_TARC0_CB_MULTIQ_2_REQ;
3013 ew32(TARC(0), reg_val);
3014 }
3015}
3016
3017/**
3018 * e1000_setup_rctl - configure the receive control registers
3019 * @adapter: Board private structure
3020 **/
3021#define PAGE_USE_COUNT(S) (((S) >> PAGE_SHIFT) + \
3022 (((S) & (PAGE_SIZE - 1)) ? 1 : 0))
3023static void e1000_setup_rctl(struct e1000_adapter *adapter)
3024{
3025 struct e1000_hw *hw = &adapter->hw;
3026 u32 rctl, rfctl;
3027 u32 pages = 0;
3028
3029 /* Workaround Si errata on PCHx - configure jumbo frame flow.
3030 * If jumbo frames not set, program related MAC/PHY registers
3031 * to h/w defaults
3032 */
3033 if (hw->mac.type >= e1000_pch2lan) {
3034 s32 ret_val;
3035
3036 if (adapter->netdev->mtu > ETH_DATA_LEN)
3037 ret_val = e1000_lv_jumbo_workaround_ich8lan(hw, true);
3038 else
3039 ret_val = e1000_lv_jumbo_workaround_ich8lan(hw, false);
3040
3041 if (ret_val)
3042 e_dbg("failed to enable|disable jumbo frame workaround mode\n");
3043 }
3044
3045 /* Program MC offset vector base */
3046 rctl = er32(RCTL);
3047 rctl &= ~(3 << E1000_RCTL_MO_SHIFT);
3048 rctl |= E1000_RCTL_EN | E1000_RCTL_BAM |
3049 E1000_RCTL_LBM_NO | E1000_RCTL_RDMTS_HALF |
3050 (adapter->hw.mac.mc_filter_type << E1000_RCTL_MO_SHIFT);
3051
3052 /* Do not Store bad packets */
3053 rctl &= ~E1000_RCTL_SBP;
3054
3055 /* Enable Long Packet receive */
3056 if (adapter->netdev->mtu <= ETH_DATA_LEN)
3057 rctl &= ~E1000_RCTL_LPE;
3058 else
3059 rctl |= E1000_RCTL_LPE;
3060
3061 /* Some systems expect that the CRC is included in SMBUS traffic. The
3062 * hardware strips the CRC before sending to both SMBUS (BMC) and to
3063 * host memory when this is enabled
3064 */
3065 if (adapter->flags2 & FLAG2_CRC_STRIPPING)
3066 rctl |= E1000_RCTL_SECRC;
3067
3068 /* Workaround Si errata on 82577 PHY - configure IPG for jumbos */
3069 if ((hw->phy.type == e1000_phy_82577) && (rctl & E1000_RCTL_LPE)) {
3070 u16 phy_data;
3071
3072 e1e_rphy(hw, PHY_REG(770, 26), &phy_data);
3073 phy_data &= 0xfff8;
3074 phy_data |= BIT(2);
3075 e1e_wphy(hw, PHY_REG(770, 26), phy_data);
3076
3077 e1e_rphy(hw, 22, &phy_data);
3078 phy_data &= 0x0fff;
3079 phy_data |= BIT(14);
3080 e1e_wphy(hw, 0x10, 0x2823);
3081 e1e_wphy(hw, 0x11, 0x0003);
3082 e1e_wphy(hw, 22, phy_data);
3083 }
3084
3085 /* Setup buffer sizes */
3086 rctl &= ~E1000_RCTL_SZ_4096;
3087 rctl |= E1000_RCTL_BSEX;
3088 switch (adapter->rx_buffer_len) {
3089 case 2048:
3090 default:
3091 rctl |= E1000_RCTL_SZ_2048;
3092 rctl &= ~E1000_RCTL_BSEX;
3093 break;
3094 case 4096:
3095 rctl |= E1000_RCTL_SZ_4096;
3096 break;
3097 case 8192:
3098 rctl |= E1000_RCTL_SZ_8192;
3099 break;
3100 case 16384:
3101 rctl |= E1000_RCTL_SZ_16384;
3102 break;
3103 }
3104
3105 /* Enable Extended Status in all Receive Descriptors */
3106 rfctl = er32(RFCTL);
3107 rfctl |= E1000_RFCTL_EXTEN;
3108 ew32(RFCTL, rfctl);
3109
3110 /* 82571 and greater support packet-split where the protocol
3111 * header is placed in skb->data and the packet data is
3112 * placed in pages hanging off of skb_shinfo(skb)->nr_frags.
3113 * In the case of a non-split, skb->data is linearly filled,
3114 * followed by the page buffers. Therefore, skb->data is
3115 * sized to hold the largest protocol header.
3116 *
3117 * allocations using alloc_page take too long for regular MTU
3118 * so only enable packet split for jumbo frames
3119 *
3120 * Using pages when the page size is greater than 16k wastes
3121 * a lot of memory, since we allocate 3 pages at all times
3122 * per packet.
3123 */
3124 pages = PAGE_USE_COUNT(adapter->netdev->mtu);
3125 if ((pages <= 3) && (PAGE_SIZE <= 16384) && (rctl & E1000_RCTL_LPE))
3126 adapter->rx_ps_pages = pages;
3127 else
3128 adapter->rx_ps_pages = 0;
3129
3130 if (adapter->rx_ps_pages) {
3131 u32 psrctl = 0;
3132
3133 /* Enable Packet split descriptors */
3134 rctl |= E1000_RCTL_DTYP_PS;
3135
3136 psrctl |= adapter->rx_ps_bsize0 >> E1000_PSRCTL_BSIZE0_SHIFT;
3137
3138 switch (adapter->rx_ps_pages) {
3139 case 3:
3140 psrctl |= PAGE_SIZE << E1000_PSRCTL_BSIZE3_SHIFT;
3141 /* fall-through */
3142 case 2:
3143 psrctl |= PAGE_SIZE << E1000_PSRCTL_BSIZE2_SHIFT;
3144 /* fall-through */
3145 case 1:
3146 psrctl |= PAGE_SIZE >> E1000_PSRCTL_BSIZE1_SHIFT;
3147 break;
3148 }
3149
3150 ew32(PSRCTL, psrctl);
3151 }
3152
3153 /* This is useful for sniffing bad packets. */
3154 if (adapter->netdev->features & NETIF_F_RXALL) {
3155 /* UPE and MPE will be handled by normal PROMISC logic
3156 * in e1000e_set_rx_mode
3157 */
3158 rctl |= (E1000_RCTL_SBP | /* Receive bad packets */
3159 E1000_RCTL_BAM | /* RX All Bcast Pkts */
3160 E1000_RCTL_PMCF); /* RX All MAC Ctrl Pkts */
3161
3162 rctl &= ~(E1000_RCTL_VFE | /* Disable VLAN filter */
3163 E1000_RCTL_DPF | /* Allow filtered pause */
3164 E1000_RCTL_CFIEN); /* Dis VLAN CFIEN Filter */
3165 /* Do not mess with E1000_CTRL_VME, it affects transmit as well,
3166 * and that breaks VLANs.
3167 */
3168 }
3169
3170 ew32(RCTL, rctl);
3171 /* just started the receive unit, no need to restart */
3172 adapter->flags &= ~FLAG_RESTART_NOW;
3173}
3174
3175/**
3176 * e1000_configure_rx - Configure Receive Unit after Reset
3177 * @adapter: board private structure
3178 *
3179 * Configure the Rx unit of the MAC after a reset.
3180 **/
3181static void e1000_configure_rx(struct e1000_adapter *adapter)
3182{
3183 struct e1000_hw *hw = &adapter->hw;
3184 struct e1000_ring *rx_ring = adapter->rx_ring;
3185 u64 rdba;
3186 u32 rdlen, rctl, rxcsum, ctrl_ext;
3187
3188 if (adapter->rx_ps_pages) {
3189 /* this is a 32 byte descriptor */
3190 rdlen = rx_ring->count *
3191 sizeof(union e1000_rx_desc_packet_split);
3192 adapter->clean_rx = e1000_clean_rx_irq_ps;
3193 adapter->alloc_rx_buf = e1000_alloc_rx_buffers_ps;
3194 } else if (adapter->netdev->mtu > ETH_FRAME_LEN + ETH_FCS_LEN) {
3195 rdlen = rx_ring->count * sizeof(union e1000_rx_desc_extended);
3196 adapter->clean_rx = e1000_clean_jumbo_rx_irq;
3197 adapter->alloc_rx_buf = e1000_alloc_jumbo_rx_buffers;
3198 } else {
3199 rdlen = rx_ring->count * sizeof(union e1000_rx_desc_extended);
3200 adapter->clean_rx = e1000_clean_rx_irq;
3201 adapter->alloc_rx_buf = e1000_alloc_rx_buffers;
3202 }
3203
3204 /* disable receives while setting up the descriptors */
3205 rctl = er32(RCTL);
3206 if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
3207 ew32(RCTL, rctl & ~E1000_RCTL_EN);
3208 e1e_flush();
3209 usleep_range(10000, 11000);
3210
3211 if (adapter->flags2 & FLAG2_DMA_BURST) {
3212 /* set the writeback threshold (only takes effect if the RDTR
3213 * is set). set GRAN=1 and write back up to 0x4 worth, and
3214 * enable prefetching of 0x20 Rx descriptors
3215 * granularity = 01
3216 * wthresh = 04,
3217 * hthresh = 04,
3218 * pthresh = 0x20
3219 */
3220 ew32(RXDCTL(0), E1000_RXDCTL_DMA_BURST_ENABLE);
3221 ew32(RXDCTL(1), E1000_RXDCTL_DMA_BURST_ENABLE);
3222 }
3223
3224 /* set the Receive Delay Timer Register */
3225 ew32(RDTR, adapter->rx_int_delay);
3226
3227 /* irq moderation */
3228 ew32(RADV, adapter->rx_abs_int_delay);
3229 if ((adapter->itr_setting != 0) && (adapter->itr != 0))
3230 e1000e_write_itr(adapter, adapter->itr);
3231
3232 ctrl_ext = er32(CTRL_EXT);
3233 /* Auto-Mask interrupts upon ICR access */
3234 ctrl_ext |= E1000_CTRL_EXT_IAME;
3235 ew32(IAM, 0xffffffff);
3236 ew32(CTRL_EXT, ctrl_ext);
3237 e1e_flush();
3238
3239 /* Setup the HW Rx Head and Tail Descriptor Pointers and
3240 * the Base and Length of the Rx Descriptor Ring
3241 */
3242 rdba = rx_ring->dma;
3243 ew32(RDBAL(0), (rdba & DMA_BIT_MASK(32)));
3244 ew32(RDBAH(0), (rdba >> 32));
3245 ew32(RDLEN(0), rdlen);
3246 ew32(RDH(0), 0);
3247 ew32(RDT(0), 0);
3248 rx_ring->head = adapter->hw.hw_addr + E1000_RDH(0);
3249 rx_ring->tail = adapter->hw.hw_addr + E1000_RDT(0);
3250
3251 writel(0, rx_ring->head);
3252 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
3253 e1000e_update_rdt_wa(rx_ring, 0);
3254 else
3255 writel(0, rx_ring->tail);
3256
3257 /* Enable Receive Checksum Offload for TCP and UDP */
3258 rxcsum = er32(RXCSUM);
3259 if (adapter->netdev->features & NETIF_F_RXCSUM)
3260 rxcsum |= E1000_RXCSUM_TUOFL;
3261 else
3262 rxcsum &= ~E1000_RXCSUM_TUOFL;
3263 ew32(RXCSUM, rxcsum);
3264
3265 /* With jumbo frames, excessive C-state transition latencies result
3266 * in dropped transactions.
3267 */
3268 if (adapter->netdev->mtu > ETH_DATA_LEN) {
3269 u32 lat =
3270 ((er32(PBA) & E1000_PBA_RXA_MASK) * 1024 -
3271 adapter->max_frame_size) * 8 / 1000;
3272
3273 if (adapter->flags & FLAG_IS_ICH) {
3274 u32 rxdctl = er32(RXDCTL(0));
3275
3276 ew32(RXDCTL(0), rxdctl | 0x3 | BIT(8));
3277 }
3278
3279 dev_info(&adapter->pdev->dev,
3280 "Some CPU C-states have been disabled in order to enable jumbo frames\n");
3281 pm_qos_update_request(&adapter->pm_qos_req, lat);
3282 } else {
3283 pm_qos_update_request(&adapter->pm_qos_req,
3284 PM_QOS_DEFAULT_VALUE);
3285 }
3286
3287 /* Enable Receives */
3288 ew32(RCTL, rctl);
3289}
3290
3291/**
3292 * e1000e_write_mc_addr_list - write multicast addresses to MTA
3293 * @netdev: network interface device structure
3294 *
3295 * Writes multicast address list to the MTA hash table.
3296 * Returns: -ENOMEM on failure
3297 * 0 on no addresses written
3298 * X on writing X addresses to MTA
3299 */
3300static int e1000e_write_mc_addr_list(struct net_device *netdev)
3301{
3302 struct e1000_adapter *adapter = netdev_priv(netdev);
3303 struct e1000_hw *hw = &adapter->hw;
3304 struct netdev_hw_addr *ha;
3305 u8 *mta_list;
3306 int i;
3307
3308 if (netdev_mc_empty(netdev)) {
3309 /* nothing to program, so clear mc list */
3310 hw->mac.ops.update_mc_addr_list(hw, NULL, 0);
3311 return 0;
3312 }
3313
3314 mta_list = kcalloc(netdev_mc_count(netdev), ETH_ALEN, GFP_ATOMIC);
3315 if (!mta_list)
3316 return -ENOMEM;
3317
3318 /* update_mc_addr_list expects a packed array of only addresses. */
3319 i = 0;
3320 netdev_for_each_mc_addr(ha, netdev)
3321 memcpy(mta_list + (i++ * ETH_ALEN), ha->addr, ETH_ALEN);
3322
3323 hw->mac.ops.update_mc_addr_list(hw, mta_list, i);
3324 kfree(mta_list);
3325
3326 return netdev_mc_count(netdev);
3327}
3328
3329/**
3330 * e1000e_write_uc_addr_list - write unicast addresses to RAR table
3331 * @netdev: network interface device structure
3332 *
3333 * Writes unicast address list to the RAR table.
3334 * Returns: -ENOMEM on failure/insufficient address space
3335 * 0 on no addresses written
3336 * X on writing X addresses to the RAR table
3337 **/
3338static int e1000e_write_uc_addr_list(struct net_device *netdev)
3339{
3340 struct e1000_adapter *adapter = netdev_priv(netdev);
3341 struct e1000_hw *hw = &adapter->hw;
3342 unsigned int rar_entries;
3343 int count = 0;
3344
3345 rar_entries = hw->mac.ops.rar_get_count(hw);
3346
3347 /* save a rar entry for our hardware address */
3348 rar_entries--;
3349
3350 /* save a rar entry for the LAA workaround */
3351 if (adapter->flags & FLAG_RESET_OVERWRITES_LAA)
3352 rar_entries--;
3353
3354 /* return ENOMEM indicating insufficient memory for addresses */
3355 if (netdev_uc_count(netdev) > rar_entries)
3356 return -ENOMEM;
3357
3358 if (!netdev_uc_empty(netdev) && rar_entries) {
3359 struct netdev_hw_addr *ha;
3360
3361 /* write the addresses in reverse order to avoid write
3362 * combining
3363 */
3364 netdev_for_each_uc_addr(ha, netdev) {
3365 int ret_val;
3366
3367 if (!rar_entries)
3368 break;
3369 ret_val = hw->mac.ops.rar_set(hw, ha->addr, rar_entries--);
3370 if (ret_val < 0)
3371 return -ENOMEM;
3372 count++;
3373 }
3374 }
3375
3376 /* zero out the remaining RAR entries not used above */
3377 for (; rar_entries > 0; rar_entries--) {
3378 ew32(RAH(rar_entries), 0);
3379 ew32(RAL(rar_entries), 0);
3380 }
3381 e1e_flush();
3382
3383 return count;
3384}
3385
3386/**
3387 * e1000e_set_rx_mode - secondary unicast, Multicast and Promiscuous mode set
3388 * @netdev: network interface device structure
3389 *
3390 * The ndo_set_rx_mode entry point is called whenever the unicast or multicast
3391 * address list or the network interface flags are updated. This routine is
3392 * responsible for configuring the hardware for proper unicast, multicast,
3393 * promiscuous mode, and all-multi behavior.
3394 **/
3395static void e1000e_set_rx_mode(struct net_device *netdev)
3396{
3397 struct e1000_adapter *adapter = netdev_priv(netdev);
3398 struct e1000_hw *hw = &adapter->hw;
3399 u32 rctl;
3400
3401 if (pm_runtime_suspended(netdev->dev.parent))
3402 return;
3403
3404 /* Check for Promiscuous and All Multicast modes */
3405 rctl = er32(RCTL);
3406
3407 /* clear the affected bits */
3408 rctl &= ~(E1000_RCTL_UPE | E1000_RCTL_MPE);
3409
3410 if (netdev->flags & IFF_PROMISC) {
3411 rctl |= (E1000_RCTL_UPE | E1000_RCTL_MPE);
3412 /* Do not hardware filter VLANs in promisc mode */
3413 e1000e_vlan_filter_disable(adapter);
3414 } else {
3415 int count;
3416
3417 if (netdev->flags & IFF_ALLMULTI) {
3418 rctl |= E1000_RCTL_MPE;
3419 } else {
3420 /* Write addresses to the MTA, if the attempt fails
3421 * then we should just turn on promiscuous mode so
3422 * that we can at least receive multicast traffic
3423 */
3424 count = e1000e_write_mc_addr_list(netdev);
3425 if (count < 0)
3426 rctl |= E1000_RCTL_MPE;
3427 }
3428 e1000e_vlan_filter_enable(adapter);
3429 /* Write addresses to available RAR registers, if there is not
3430 * sufficient space to store all the addresses then enable
3431 * unicast promiscuous mode
3432 */
3433 count = e1000e_write_uc_addr_list(netdev);
3434 if (count < 0)
3435 rctl |= E1000_RCTL_UPE;
3436 }
3437
3438 ew32(RCTL, rctl);
3439
3440 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3441 e1000e_vlan_strip_enable(adapter);
3442 else
3443 e1000e_vlan_strip_disable(adapter);
3444}
3445
3446static void e1000e_setup_rss_hash(struct e1000_adapter *adapter)
3447{
3448 struct e1000_hw *hw = &adapter->hw;
3449 u32 mrqc, rxcsum;
3450 u32 rss_key[10];
3451 int i;
3452
3453 netdev_rss_key_fill(rss_key, sizeof(rss_key));
3454 for (i = 0; i < 10; i++)
3455 ew32(RSSRK(i), rss_key[i]);
3456
3457 /* Direct all traffic to queue 0 */
3458 for (i = 0; i < 32; i++)
3459 ew32(RETA(i), 0);
3460
3461 /* Disable raw packet checksumming so that RSS hash is placed in
3462 * descriptor on writeback.
3463 */
3464 rxcsum = er32(RXCSUM);
3465 rxcsum |= E1000_RXCSUM_PCSD;
3466
3467 ew32(RXCSUM, rxcsum);
3468
3469 mrqc = (E1000_MRQC_RSS_FIELD_IPV4 |
3470 E1000_MRQC_RSS_FIELD_IPV4_TCP |
3471 E1000_MRQC_RSS_FIELD_IPV6 |
3472 E1000_MRQC_RSS_FIELD_IPV6_TCP |
3473 E1000_MRQC_RSS_FIELD_IPV6_TCP_EX);
3474
3475 ew32(MRQC, mrqc);
3476}
3477
3478/**
3479 * e1000e_get_base_timinca - get default SYSTIM time increment attributes
3480 * @adapter: board private structure
3481 * @timinca: pointer to returned time increment attributes
3482 *
3483 * Get attributes for incrementing the System Time Register SYSTIML/H at
3484 * the default base frequency, and set the cyclecounter shift value.
3485 **/
3486s32 e1000e_get_base_timinca(struct e1000_adapter *adapter, u32 *timinca)
3487{
3488 struct e1000_hw *hw = &adapter->hw;
3489 u32 incvalue, incperiod, shift;
3490
3491 /* Make sure clock is enabled on I217/I218/I219 before checking
3492 * the frequency
3493 */
3494 if ((hw->mac.type >= e1000_pch_lpt) &&
3495 !(er32(TSYNCTXCTL) & E1000_TSYNCTXCTL_ENABLED) &&
3496 !(er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_ENABLED)) {
3497 u32 fextnvm7 = er32(FEXTNVM7);
3498
3499 if (!(fextnvm7 & BIT(0))) {
3500 ew32(FEXTNVM7, fextnvm7 | BIT(0));
3501 e1e_flush();
3502 }
3503 }
3504
3505 switch (hw->mac.type) {
3506 case e1000_pch2lan:
3507 /* Stable 96MHz frequency */
3508 incperiod = INCPERIOD_96MHZ;
3509 incvalue = INCVALUE_96MHZ;
3510 shift = INCVALUE_SHIFT_96MHZ;
3511 adapter->cc.shift = shift + INCPERIOD_SHIFT_96MHZ;
3512 break;
3513 case e1000_pch_lpt:
3514 if (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI) {
3515 /* Stable 96MHz frequency */
3516 incperiod = INCPERIOD_96MHZ;
3517 incvalue = INCVALUE_96MHZ;
3518 shift = INCVALUE_SHIFT_96MHZ;
3519 adapter->cc.shift = shift + INCPERIOD_SHIFT_96MHZ;
3520 } else {
3521 /* Stable 25MHz frequency */
3522 incperiod = INCPERIOD_25MHZ;
3523 incvalue = INCVALUE_25MHZ;
3524 shift = INCVALUE_SHIFT_25MHZ;
3525 adapter->cc.shift = shift;
3526 }
3527 break;
3528 case e1000_pch_spt:
3529 /* Stable 24MHz frequency */
3530 incperiod = INCPERIOD_24MHZ;
3531 incvalue = INCVALUE_24MHZ;
3532 shift = INCVALUE_SHIFT_24MHZ;
3533 adapter->cc.shift = shift;
3534 break;
3535 case e1000_pch_cnp:
3536 if (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_SYSCFI) {
3537 /* Stable 24MHz frequency */
3538 incperiod = INCPERIOD_24MHZ;
3539 incvalue = INCVALUE_24MHZ;
3540 shift = INCVALUE_SHIFT_24MHZ;
3541 adapter->cc.shift = shift;
3542 } else {
3543 /* Stable 38400KHz frequency */
3544 incperiod = INCPERIOD_38400KHZ;
3545 incvalue = INCVALUE_38400KHZ;
3546 shift = INCVALUE_SHIFT_38400KHZ;
3547 adapter->cc.shift = shift;
3548 }
3549 break;
3550 case e1000_82574:
3551 case e1000_82583:
3552 /* Stable 25MHz frequency */
3553 incperiod = INCPERIOD_25MHZ;
3554 incvalue = INCVALUE_25MHZ;
3555 shift = INCVALUE_SHIFT_25MHZ;
3556 adapter->cc.shift = shift;
3557 break;
3558 default:
3559 return -EINVAL;
3560 }
3561
3562 *timinca = ((incperiod << E1000_TIMINCA_INCPERIOD_SHIFT) |
3563 ((incvalue << shift) & E1000_TIMINCA_INCVALUE_MASK));
3564
3565 return 0;
3566}
3567
3568/**
3569 * e1000e_config_hwtstamp - configure the hwtstamp registers and enable/disable
3570 * @adapter: board private structure
3571 *
3572 * Outgoing time stamping can be enabled and disabled. Play nice and
3573 * disable it when requested, although it shouldn't cause any overhead
3574 * when no packet needs it. At most one packet in the queue may be
3575 * marked for time stamping, otherwise it would be impossible to tell
3576 * for sure to which packet the hardware time stamp belongs.
3577 *
3578 * Incoming time stamping has to be configured via the hardware filters.
3579 * Not all combinations are supported, in particular event type has to be
3580 * specified. Matching the kind of event packet is not supported, with the
3581 * exception of "all V2 events regardless of level 2 or 4".
3582 **/
3583static int e1000e_config_hwtstamp(struct e1000_adapter *adapter,
3584 struct hwtstamp_config *config)
3585{
3586 struct e1000_hw *hw = &adapter->hw;
3587 u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
3588 u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
3589 u32 rxmtrl = 0;
3590 u16 rxudp = 0;
3591 bool is_l4 = false;
3592 bool is_l2 = false;
3593 u32 regval;
3594
3595 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
3596 return -EINVAL;
3597
3598 /* flags reserved for future extensions - must be zero */
3599 if (config->flags)
3600 return -EINVAL;
3601
3602 switch (config->tx_type) {
3603 case HWTSTAMP_TX_OFF:
3604 tsync_tx_ctl = 0;
3605 break;
3606 case HWTSTAMP_TX_ON:
3607 break;
3608 default:
3609 return -ERANGE;
3610 }
3611
3612 switch (config->rx_filter) {
3613 case HWTSTAMP_FILTER_NONE:
3614 tsync_rx_ctl = 0;
3615 break;
3616 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
3617 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
3618 rxmtrl = E1000_RXMTRL_PTP_V1_SYNC_MESSAGE;
3619 is_l4 = true;
3620 break;
3621 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
3622 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
3623 rxmtrl = E1000_RXMTRL_PTP_V1_DELAY_REQ_MESSAGE;
3624 is_l4 = true;
3625 break;
3626 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
3627 /* Also time stamps V2 L2 Path Delay Request/Response */
3628 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_V2;
3629 rxmtrl = E1000_RXMTRL_PTP_V2_SYNC_MESSAGE;
3630 is_l2 = true;
3631 break;
3632 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
3633 /* Also time stamps V2 L2 Path Delay Request/Response. */
3634 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_V2;
3635 rxmtrl = E1000_RXMTRL_PTP_V2_DELAY_REQ_MESSAGE;
3636 is_l2 = true;
3637 break;
3638 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
3639 /* Hardware cannot filter just V2 L4 Sync messages;
3640 * fall-through to V2 (both L2 and L4) Sync.
3641 */
3642 case HWTSTAMP_FILTER_PTP_V2_SYNC:
3643 /* Also time stamps V2 Path Delay Request/Response. */
3644 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
3645 rxmtrl = E1000_RXMTRL_PTP_V2_SYNC_MESSAGE;
3646 is_l2 = true;
3647 is_l4 = true;
3648 break;
3649 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
3650 /* Hardware cannot filter just V2 L4 Delay Request messages;
3651 * fall-through to V2 (both L2 and L4) Delay Request.
3652 */
3653 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
3654 /* Also time stamps V2 Path Delay Request/Response. */
3655 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L2_L4_V2;
3656 rxmtrl = E1000_RXMTRL_PTP_V2_DELAY_REQ_MESSAGE;
3657 is_l2 = true;
3658 is_l4 = true;
3659 break;
3660 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
3661 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
3662 /* Hardware cannot filter just V2 L4 or L2 Event messages;
3663 * fall-through to all V2 (both L2 and L4) Events.
3664 */
3665 case HWTSTAMP_FILTER_PTP_V2_EVENT:
3666 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
3667 config->rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
3668 is_l2 = true;
3669 is_l4 = true;
3670 break;
3671 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
3672 /* For V1, the hardware can only filter Sync messages or
3673 * Delay Request messages but not both so fall-through to
3674 * time stamp all packets.
3675 */
3676 case HWTSTAMP_FILTER_NTP_ALL:
3677 case HWTSTAMP_FILTER_ALL:
3678 is_l2 = true;
3679 is_l4 = true;
3680 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
3681 config->rx_filter = HWTSTAMP_FILTER_ALL;
3682 break;
3683 default:
3684 return -ERANGE;
3685 }
3686
3687 adapter->hwtstamp_config = *config;
3688
3689 /* enable/disable Tx h/w time stamping */
3690 regval = er32(TSYNCTXCTL);
3691 regval &= ~E1000_TSYNCTXCTL_ENABLED;
3692 regval |= tsync_tx_ctl;
3693 ew32(TSYNCTXCTL, regval);
3694 if ((er32(TSYNCTXCTL) & E1000_TSYNCTXCTL_ENABLED) !=
3695 (regval & E1000_TSYNCTXCTL_ENABLED)) {
3696 e_err("Timesync Tx Control register not set as expected\n");
3697 return -EAGAIN;
3698 }
3699
3700 /* enable/disable Rx h/w time stamping */
3701 regval = er32(TSYNCRXCTL);
3702 regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
3703 regval |= tsync_rx_ctl;
3704 ew32(TSYNCRXCTL, regval);
3705 if ((er32(TSYNCRXCTL) & (E1000_TSYNCRXCTL_ENABLED |
3706 E1000_TSYNCRXCTL_TYPE_MASK)) !=
3707 (regval & (E1000_TSYNCRXCTL_ENABLED |
3708 E1000_TSYNCRXCTL_TYPE_MASK))) {
3709 e_err("Timesync Rx Control register not set as expected\n");
3710 return -EAGAIN;
3711 }
3712
3713 /* L2: define ethertype filter for time stamped packets */
3714 if (is_l2)
3715 rxmtrl |= ETH_P_1588;
3716
3717 /* define which PTP packets get time stamped */
3718 ew32(RXMTRL, rxmtrl);
3719
3720 /* Filter by destination port */
3721 if (is_l4) {
3722 rxudp = PTP_EV_PORT;
3723 cpu_to_be16s(&rxudp);
3724 }
3725 ew32(RXUDP, rxudp);
3726
3727 e1e_flush();
3728
3729 /* Clear TSYNCRXCTL_VALID & TSYNCTXCTL_VALID bit */
3730 er32(RXSTMPH);
3731 er32(TXSTMPH);
3732
3733 return 0;
3734}
3735
3736/**
3737 * e1000_configure - configure the hardware for Rx and Tx
3738 * @adapter: private board structure
3739 **/
3740static void e1000_configure(struct e1000_adapter *adapter)
3741{
3742 struct e1000_ring *rx_ring = adapter->rx_ring;
3743
3744 e1000e_set_rx_mode(adapter->netdev);
3745
3746 e1000_restore_vlan(adapter);
3747 e1000_init_manageability_pt(adapter);
3748
3749 e1000_configure_tx(adapter);
3750
3751 if (adapter->netdev->features & NETIF_F_RXHASH)
3752 e1000e_setup_rss_hash(adapter);
3753 e1000_setup_rctl(adapter);
3754 e1000_configure_rx(adapter);
3755 adapter->alloc_rx_buf(rx_ring, e1000_desc_unused(rx_ring), GFP_KERNEL);
3756}
3757
3758/**
3759 * e1000e_power_up_phy - restore link in case the phy was powered down
3760 * @adapter: address of board private structure
3761 *
3762 * The phy may be powered down to save power and turn off link when the
3763 * driver is unloaded and wake on lan is not enabled (among others)
3764 * *** this routine MUST be followed by a call to e1000e_reset ***
3765 **/
3766void e1000e_power_up_phy(struct e1000_adapter *adapter)
3767{
3768 if (adapter->hw.phy.ops.power_up)
3769 adapter->hw.phy.ops.power_up(&adapter->hw);
3770
3771 adapter->hw.mac.ops.setup_link(&adapter->hw);
3772}
3773
3774/**
3775 * e1000_power_down_phy - Power down the PHY
3776 *
3777 * Power down the PHY so no link is implied when interface is down.
3778 * The PHY cannot be powered down if management or WoL is active.
3779 */
3780static void e1000_power_down_phy(struct e1000_adapter *adapter)
3781{
3782 if (adapter->hw.phy.ops.power_down)
3783 adapter->hw.phy.ops.power_down(&adapter->hw);
3784}
3785
3786/**
3787 * e1000_flush_tx_ring - remove all descriptors from the tx_ring
3788 *
3789 * We want to clear all pending descriptors from the TX ring.
3790 * zeroing happens when the HW reads the regs. We assign the ring itself as
3791 * the data of the next descriptor. We don't care about the data we are about
3792 * to reset the HW.
3793 */
3794static void e1000_flush_tx_ring(struct e1000_adapter *adapter)
3795{
3796 struct e1000_hw *hw = &adapter->hw;
3797 struct e1000_ring *tx_ring = adapter->tx_ring;
3798 struct e1000_tx_desc *tx_desc = NULL;
3799 u32 tdt, tctl, txd_lower = E1000_TXD_CMD_IFCS;
3800 u16 size = 512;
3801
3802 tctl = er32(TCTL);
3803 ew32(TCTL, tctl | E1000_TCTL_EN);
3804 tdt = er32(TDT(0));
3805 BUG_ON(tdt != tx_ring->next_to_use);
3806 tx_desc = E1000_TX_DESC(*tx_ring, tx_ring->next_to_use);
3807 tx_desc->buffer_addr = tx_ring->dma;
3808
3809 tx_desc->lower.data = cpu_to_le32(txd_lower | size);
3810 tx_desc->upper.data = 0;
3811 /* flush descriptors to memory before notifying the HW */
3812 wmb();
3813 tx_ring->next_to_use++;
3814 if (tx_ring->next_to_use == tx_ring->count)
3815 tx_ring->next_to_use = 0;
3816 ew32(TDT(0), tx_ring->next_to_use);
3817 usleep_range(200, 250);
3818}
3819
3820/**
3821 * e1000_flush_rx_ring - remove all descriptors from the rx_ring
3822 *
3823 * Mark all descriptors in the RX ring as consumed and disable the rx ring
3824 */
3825static void e1000_flush_rx_ring(struct e1000_adapter *adapter)
3826{
3827 u32 rctl, rxdctl;
3828 struct e1000_hw *hw = &adapter->hw;
3829
3830 rctl = er32(RCTL);
3831 ew32(RCTL, rctl & ~E1000_RCTL_EN);
3832 e1e_flush();
3833 usleep_range(100, 150);
3834
3835 rxdctl = er32(RXDCTL(0));
3836 /* zero the lower 14 bits (prefetch and host thresholds) */
3837 rxdctl &= 0xffffc000;
3838
3839 /* update thresholds: prefetch threshold to 31, host threshold to 1
3840 * and make sure the granularity is "descriptors" and not "cache lines"
3841 */
3842 rxdctl |= (0x1F | BIT(8) | E1000_RXDCTL_THRESH_UNIT_DESC);
3843
3844 ew32(RXDCTL(0), rxdctl);
3845 /* momentarily enable the RX ring for the changes to take effect */
3846 ew32(RCTL, rctl | E1000_RCTL_EN);
3847 e1e_flush();
3848 usleep_range(100, 150);
3849 ew32(RCTL, rctl & ~E1000_RCTL_EN);
3850}
3851
3852/**
3853 * e1000_flush_desc_rings - remove all descriptors from the descriptor rings
3854 *
3855 * In i219, the descriptor rings must be emptied before resetting the HW
3856 * or before changing the device state to D3 during runtime (runtime PM).
3857 *
3858 * Failure to do this will cause the HW to enter a unit hang state which can
3859 * only be released by PCI reset on the device
3860 *
3861 */
3862
3863static void e1000_flush_desc_rings(struct e1000_adapter *adapter)
3864{
3865 u16 hang_state;
3866 u32 fext_nvm11, tdlen;
3867 struct e1000_hw *hw = &adapter->hw;
3868
3869 /* First, disable MULR fix in FEXTNVM11 */
3870 fext_nvm11 = er32(FEXTNVM11);
3871 fext_nvm11 |= E1000_FEXTNVM11_DISABLE_MULR_FIX;
3872 ew32(FEXTNVM11, fext_nvm11);
3873 /* do nothing if we're not in faulty state, or if the queue is empty */
3874 tdlen = er32(TDLEN(0));
3875 pci_read_config_word(adapter->pdev, PCICFG_DESC_RING_STATUS,
3876 &hang_state);
3877 if (!(hang_state & FLUSH_DESC_REQUIRED) || !tdlen)
3878 return;
3879 e1000_flush_tx_ring(adapter);
3880 /* recheck, maybe the fault is caused by the rx ring */
3881 pci_read_config_word(adapter->pdev, PCICFG_DESC_RING_STATUS,
3882 &hang_state);
3883 if (hang_state & FLUSH_DESC_REQUIRED)
3884 e1000_flush_rx_ring(adapter);
3885}
3886
3887/**
3888 * e1000e_systim_reset - reset the timesync registers after a hardware reset
3889 * @adapter: board private structure
3890 *
3891 * When the MAC is reset, all hardware bits for timesync will be reset to the
3892 * default values. This function will restore the settings last in place.
3893 * Since the clock SYSTIME registers are reset, we will simply restore the
3894 * cyclecounter to the kernel real clock time.
3895 **/
3896static void e1000e_systim_reset(struct e1000_adapter *adapter)
3897{
3898 struct ptp_clock_info *info = &adapter->ptp_clock_info;
3899 struct e1000_hw *hw = &adapter->hw;
3900 unsigned long flags;
3901 u32 timinca;
3902 s32 ret_val;
3903
3904 if (!(adapter->flags & FLAG_HAS_HW_TIMESTAMP))
3905 return;
3906
3907 if (info->adjfreq) {
3908 /* restore the previous ptp frequency delta */
3909 ret_val = info->adjfreq(info, adapter->ptp_delta);
3910 } else {
3911 /* set the default base frequency if no adjustment possible */
3912 ret_val = e1000e_get_base_timinca(adapter, &timinca);
3913 if (!ret_val)
3914 ew32(TIMINCA, timinca);
3915 }
3916
3917 if (ret_val) {
3918 dev_warn(&adapter->pdev->dev,
3919 "Failed to restore TIMINCA clock rate delta: %d\n",
3920 ret_val);
3921 return;
3922 }
3923
3924 /* reset the systim ns time counter */
3925 spin_lock_irqsave(&adapter->systim_lock, flags);
3926 timecounter_init(&adapter->tc, &adapter->cc,
3927 ktime_to_ns(ktime_get_real()));
3928 spin_unlock_irqrestore(&adapter->systim_lock, flags);
3929
3930 /* restore the previous hwtstamp configuration settings */
3931 e1000e_config_hwtstamp(adapter, &adapter->hwtstamp_config);
3932}
3933
3934/**
3935 * e1000e_reset - bring the hardware into a known good state
3936 *
3937 * This function boots the hardware and enables some settings that
3938 * require a configuration cycle of the hardware - those cannot be
3939 * set/changed during runtime. After reset the device needs to be
3940 * properly configured for Rx, Tx etc.
3941 */
3942void e1000e_reset(struct e1000_adapter *adapter)
3943{
3944 struct e1000_mac_info *mac = &adapter->hw.mac;
3945 struct e1000_fc_info *fc = &adapter->hw.fc;
3946 struct e1000_hw *hw = &adapter->hw;
3947 u32 tx_space, min_tx_space, min_rx_space;
3948 u32 pba = adapter->pba;
3949 u16 hwm;
3950
3951 /* reset Packet Buffer Allocation to default */
3952 ew32(PBA, pba);
3953
3954 if (adapter->max_frame_size > (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)) {
3955 /* To maintain wire speed transmits, the Tx FIFO should be
3956 * large enough to accommodate two full transmit packets,
3957 * rounded up to the next 1KB and expressed in KB. Likewise,
3958 * the Rx FIFO should be large enough to accommodate at least
3959 * one full receive packet and is similarly rounded up and
3960 * expressed in KB.
3961 */
3962 pba = er32(PBA);
3963 /* upper 16 bits has Tx packet buffer allocation size in KB */
3964 tx_space = pba >> 16;
3965 /* lower 16 bits has Rx packet buffer allocation size in KB */
3966 pba &= 0xffff;
3967 /* the Tx fifo also stores 16 bytes of information about the Tx
3968 * but don't include ethernet FCS because hardware appends it
3969 */
3970 min_tx_space = (adapter->max_frame_size +
3971 sizeof(struct e1000_tx_desc) - ETH_FCS_LEN) * 2;
3972 min_tx_space = ALIGN(min_tx_space, 1024);
3973 min_tx_space >>= 10;
3974 /* software strips receive CRC, so leave room for it */
3975 min_rx_space = adapter->max_frame_size;
3976 min_rx_space = ALIGN(min_rx_space, 1024);
3977 min_rx_space >>= 10;
3978
3979 /* If current Tx allocation is less than the min Tx FIFO size,
3980 * and the min Tx FIFO size is less than the current Rx FIFO
3981 * allocation, take space away from current Rx allocation
3982 */
3983 if ((tx_space < min_tx_space) &&
3984 ((min_tx_space - tx_space) < pba)) {
3985 pba -= min_tx_space - tx_space;
3986
3987 /* if short on Rx space, Rx wins and must trump Tx
3988 * adjustment
3989 */
3990 if (pba < min_rx_space)
3991 pba = min_rx_space;
3992 }
3993
3994 ew32(PBA, pba);
3995 }
3996
3997 /* flow control settings
3998 *
3999 * The high water mark must be low enough to fit one full frame
4000 * (or the size used for early receive) above it in the Rx FIFO.
4001 * Set it to the lower of:
4002 * - 90% of the Rx FIFO size, and
4003 * - the full Rx FIFO size minus one full frame
4004 */
4005 if (adapter->flags & FLAG_DISABLE_FC_PAUSE_TIME)
4006 fc->pause_time = 0xFFFF;
4007 else
4008 fc->pause_time = E1000_FC_PAUSE_TIME;
4009 fc->send_xon = true;
4010 fc->current_mode = fc->requested_mode;
4011
4012 switch (hw->mac.type) {
4013 case e1000_ich9lan:
4014 case e1000_ich10lan:
4015 if (adapter->netdev->mtu > ETH_DATA_LEN) {
4016 pba = 14;
4017 ew32(PBA, pba);
4018 fc->high_water = 0x2800;
4019 fc->low_water = fc->high_water - 8;
4020 break;
4021 }
4022 /* fall-through */
4023 default:
4024 hwm = min(((pba << 10) * 9 / 10),
4025 ((pba << 10) - adapter->max_frame_size));
4026
4027 fc->high_water = hwm & E1000_FCRTH_RTH; /* 8-byte granularity */
4028 fc->low_water = fc->high_water - 8;
4029 break;
4030 case e1000_pchlan:
4031 /* Workaround PCH LOM adapter hangs with certain network
4032 * loads. If hangs persist, try disabling Tx flow control.
4033 */
4034 if (adapter->netdev->mtu > ETH_DATA_LEN) {
4035 fc->high_water = 0x3500;
4036 fc->low_water = 0x1500;
4037 } else {
4038 fc->high_water = 0x5000;
4039 fc->low_water = 0x3000;
4040 }
4041 fc->refresh_time = 0x1000;
4042 break;
4043 case e1000_pch2lan:
4044 case e1000_pch_lpt:
4045 case e1000_pch_spt:
4046 case e1000_pch_cnp:
4047 fc->refresh_time = 0xFFFF;
4048 fc->pause_time = 0xFFFF;
4049
4050 if (adapter->netdev->mtu <= ETH_DATA_LEN) {
4051 fc->high_water = 0x05C20;
4052 fc->low_water = 0x05048;
4053 break;
4054 }
4055
4056 pba = 14;
4057 ew32(PBA, pba);
4058 fc->high_water = ((pba << 10) * 9 / 10) & E1000_FCRTH_RTH;
4059 fc->low_water = ((pba << 10) * 8 / 10) & E1000_FCRTL_RTL;
4060 break;
4061 }
4062
4063 /* Alignment of Tx data is on an arbitrary byte boundary with the
4064 * maximum size per Tx descriptor limited only to the transmit
4065 * allocation of the packet buffer minus 96 bytes with an upper
4066 * limit of 24KB due to receive synchronization limitations.
4067 */
4068 adapter->tx_fifo_limit = min_t(u32, ((er32(PBA) >> 16) << 10) - 96,
4069 24 << 10);
4070
4071 /* Disable Adaptive Interrupt Moderation if 2 full packets cannot
4072 * fit in receive buffer.
4073 */
4074 if (adapter->itr_setting & 0x3) {
4075 if ((adapter->max_frame_size * 2) > (pba << 10)) {
4076 if (!(adapter->flags2 & FLAG2_DISABLE_AIM)) {
4077 dev_info(&adapter->pdev->dev,
4078 "Interrupt Throttle Rate off\n");
4079 adapter->flags2 |= FLAG2_DISABLE_AIM;
4080 e1000e_write_itr(adapter, 0);
4081 }
4082 } else if (adapter->flags2 & FLAG2_DISABLE_AIM) {
4083 dev_info(&adapter->pdev->dev,
4084 "Interrupt Throttle Rate on\n");
4085 adapter->flags2 &= ~FLAG2_DISABLE_AIM;
4086 adapter->itr = 20000;
4087 e1000e_write_itr(adapter, adapter->itr);
4088 }
4089 }
4090
4091 if (hw->mac.type >= e1000_pch_spt)
4092 e1000_flush_desc_rings(adapter);
4093 /* Allow time for pending master requests to run */
4094 mac->ops.reset_hw(hw);
4095
4096 /* For parts with AMT enabled, let the firmware know
4097 * that the network interface is in control
4098 */
4099 if (adapter->flags & FLAG_HAS_AMT)
4100 e1000e_get_hw_control(adapter);
4101
4102 ew32(WUC, 0);
4103
4104 if (mac->ops.init_hw(hw))
4105 e_err("Hardware Error\n");
4106
4107 e1000_update_mng_vlan(adapter);
4108
4109 /* Enable h/w to recognize an 802.1Q VLAN Ethernet packet */
4110 ew32(VET, ETH_P_8021Q);
4111
4112 e1000e_reset_adaptive(hw);
4113
4114 /* restore systim and hwtstamp settings */
4115 e1000e_systim_reset(adapter);
4116
4117 /* Set EEE advertisement as appropriate */
4118 if (adapter->flags2 & FLAG2_HAS_EEE) {
4119 s32 ret_val;
4120 u16 adv_addr;
4121
4122 switch (hw->phy.type) {
4123 case e1000_phy_82579:
4124 adv_addr = I82579_EEE_ADVERTISEMENT;
4125 break;
4126 case e1000_phy_i217:
4127 adv_addr = I217_EEE_ADVERTISEMENT;
4128 break;
4129 default:
4130 dev_err(&adapter->pdev->dev,
4131 "Invalid PHY type setting EEE advertisement\n");
4132 return;
4133 }
4134
4135 ret_val = hw->phy.ops.acquire(hw);
4136 if (ret_val) {
4137 dev_err(&adapter->pdev->dev,
4138 "EEE advertisement - unable to acquire PHY\n");
4139 return;
4140 }
4141
4142 e1000_write_emi_reg_locked(hw, adv_addr,
4143 hw->dev_spec.ich8lan.eee_disable ?
4144 0 : adapter->eee_advert);
4145
4146 hw->phy.ops.release(hw);
4147 }
4148
4149 if (!netif_running(adapter->netdev) &&
4150 !test_bit(__E1000_TESTING, &adapter->state))
4151 e1000_power_down_phy(adapter);
4152
4153 e1000_get_phy_info(hw);
4154
4155 if ((adapter->flags & FLAG_HAS_SMART_POWER_DOWN) &&
4156 !(adapter->flags & FLAG_SMART_POWER_DOWN)) {
4157 u16 phy_data = 0;
4158 /* speed up time to link by disabling smart power down, ignore
4159 * the return value of this function because there is nothing
4160 * different we would do if it failed
4161 */
4162 e1e_rphy(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
4163 phy_data &= ~IGP02E1000_PM_SPD;
4164 e1e_wphy(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
4165 }
4166 if (hw->mac.type >= e1000_pch_spt && adapter->int_mode == 0) {
4167 u32 reg;
4168
4169 /* Fextnvm7 @ 0xe4[2] = 1 */
4170 reg = er32(FEXTNVM7);
4171 reg |= E1000_FEXTNVM7_SIDE_CLK_UNGATE;
4172 ew32(FEXTNVM7, reg);
4173 /* Fextnvm9 @ 0x5bb4[13:12] = 11 */
4174 reg = er32(FEXTNVM9);
4175 reg |= E1000_FEXTNVM9_IOSFSB_CLKGATE_DIS |
4176 E1000_FEXTNVM9_IOSFSB_CLKREQ_DIS;
4177 ew32(FEXTNVM9, reg);
4178 }
4179
4180}
4181
4182/**
4183 * e1000e_trigger_lsc - trigger an LSC interrupt
4184 * @adapter:
4185 *
4186 * Fire a link status change interrupt to start the watchdog.
4187 **/
4188static void e1000e_trigger_lsc(struct e1000_adapter *adapter)
4189{
4190 struct e1000_hw *hw = &adapter->hw;
4191
4192 if (adapter->msix_entries)
4193 ew32(ICS, E1000_ICS_LSC | E1000_ICS_OTHER);
4194 else
4195 ew32(ICS, E1000_ICS_LSC);
4196}
4197
4198void e1000e_up(struct e1000_adapter *adapter)
4199{
4200 /* hardware has been reset, we need to reload some things */
4201 e1000_configure(adapter);
4202
4203 clear_bit(__E1000_DOWN, &adapter->state);
4204
4205 if (adapter->msix_entries)
4206 e1000_configure_msix(adapter);
4207 e1000_irq_enable(adapter);
4208
4209 /* Tx queue started by watchdog timer when link is up */
4210
4211 e1000e_trigger_lsc(adapter);
4212}
4213
4214static void e1000e_flush_descriptors(struct e1000_adapter *adapter)
4215{
4216 struct e1000_hw *hw = &adapter->hw;
4217
4218 if (!(adapter->flags2 & FLAG2_DMA_BURST))
4219 return;
4220
4221 /* flush pending descriptor writebacks to memory */
4222 ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
4223 ew32(RDTR, adapter->rx_int_delay | E1000_RDTR_FPD);
4224
4225 /* execute the writes immediately */
4226 e1e_flush();
4227
4228 /* due to rare timing issues, write to TIDV/RDTR again to ensure the
4229 * write is successful
4230 */
4231 ew32(TIDV, adapter->tx_int_delay | E1000_TIDV_FPD);
4232 ew32(RDTR, adapter->rx_int_delay | E1000_RDTR_FPD);
4233
4234 /* execute the writes immediately */
4235 e1e_flush();
4236}
4237
4238static void e1000e_update_stats(struct e1000_adapter *adapter);
4239
4240/**
4241 * e1000e_down - quiesce the device and optionally reset the hardware
4242 * @adapter: board private structure
4243 * @reset: boolean flag to reset the hardware or not
4244 */
4245void e1000e_down(struct e1000_adapter *adapter, bool reset)
4246{
4247 struct net_device *netdev = adapter->netdev;
4248 struct e1000_hw *hw = &adapter->hw;
4249 u32 tctl, rctl;
4250
4251 /* signal that we're down so the interrupt handler does not
4252 * reschedule our watchdog timer
4253 */
4254 set_bit(__E1000_DOWN, &adapter->state);
4255
4256 netif_carrier_off(netdev);
4257
4258 /* disable receives in the hardware */
4259 rctl = er32(RCTL);
4260 if (!(adapter->flags2 & FLAG2_NO_DISABLE_RX))
4261 ew32(RCTL, rctl & ~E1000_RCTL_EN);
4262 /* flush and sleep below */
4263
4264 netif_stop_queue(netdev);
4265
4266 /* disable transmits in the hardware */
4267 tctl = er32(TCTL);
4268 tctl &= ~E1000_TCTL_EN;
4269 ew32(TCTL, tctl);
4270
4271 /* flush both disables and wait for them to finish */
4272 e1e_flush();
4273 usleep_range(10000, 11000);
4274
4275 e1000_irq_disable(adapter);
4276
4277 napi_synchronize(&adapter->napi);
4278
4279 del_timer_sync(&adapter->watchdog_timer);
4280 del_timer_sync(&adapter->phy_info_timer);
4281
4282 spin_lock(&adapter->stats64_lock);
4283 e1000e_update_stats(adapter);
4284 spin_unlock(&adapter->stats64_lock);
4285
4286 e1000e_flush_descriptors(adapter);
4287
4288 adapter->link_speed = 0;
4289 adapter->link_duplex = 0;
4290
4291 /* Disable Si errata workaround on PCHx for jumbo frame flow */
4292 if ((hw->mac.type >= e1000_pch2lan) &&
4293 (adapter->netdev->mtu > ETH_DATA_LEN) &&
4294 e1000_lv_jumbo_workaround_ich8lan(hw, false))
4295 e_dbg("failed to disable jumbo frame workaround mode\n");
4296
4297 if (!pci_channel_offline(adapter->pdev)) {
4298 if (reset)
4299 e1000e_reset(adapter);
4300 else if (hw->mac.type >= e1000_pch_spt)
4301 e1000_flush_desc_rings(adapter);
4302 }
4303 e1000_clean_tx_ring(adapter->tx_ring);
4304 e1000_clean_rx_ring(adapter->rx_ring);
4305}
4306
4307void e1000e_reinit_locked(struct e1000_adapter *adapter)
4308{
4309 might_sleep();
4310 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
4311 usleep_range(1000, 1100);
4312 e1000e_down(adapter, true);
4313 e1000e_up(adapter);
4314 clear_bit(__E1000_RESETTING, &adapter->state);
4315}
4316
4317/**
4318 * e1000e_sanitize_systim - sanitize raw cycle counter reads
4319 * @hw: pointer to the HW structure
4320 * @systim: PHC time value read, sanitized and returned
4321 * @sts: structure to hold system time before and after reading SYSTIML,
4322 * may be NULL
4323 *
4324 * Errata for 82574/82583 possible bad bits read from SYSTIMH/L:
4325 * check to see that the time is incrementing at a reasonable
4326 * rate and is a multiple of incvalue.
4327 **/
4328static u64 e1000e_sanitize_systim(struct e1000_hw *hw, u64 systim,
4329 struct ptp_system_timestamp *sts)
4330{
4331 u64 time_delta, rem, temp;
4332 u64 systim_next;
4333 u32 incvalue;
4334 int i;
4335
4336 incvalue = er32(TIMINCA) & E1000_TIMINCA_INCVALUE_MASK;
4337 for (i = 0; i < E1000_MAX_82574_SYSTIM_REREADS; i++) {
4338 /* latch SYSTIMH on read of SYSTIML */
4339 ptp_read_system_prets(sts);
4340 systim_next = (u64)er32(SYSTIML);
4341 ptp_read_system_postts(sts);
4342 systim_next |= (u64)er32(SYSTIMH) << 32;
4343
4344 time_delta = systim_next - systim;
4345 temp = time_delta;
4346 /* VMWare users have seen incvalue of zero, don't div / 0 */
4347 rem = incvalue ? do_div(temp, incvalue) : (time_delta != 0);
4348
4349 systim = systim_next;
4350
4351 if ((time_delta < E1000_82574_SYSTIM_EPSILON) && (rem == 0))
4352 break;
4353 }
4354
4355 return systim;
4356}
4357
4358/**
4359 * e1000e_read_systim - read SYSTIM register
4360 * @adapter: board private structure
4361 * @sts: structure which will contain system time before and after reading
4362 * SYSTIML, may be NULL
4363 **/
4364u64 e1000e_read_systim(struct e1000_adapter *adapter,
4365 struct ptp_system_timestamp *sts)
4366{
4367 struct e1000_hw *hw = &adapter->hw;
4368 u32 systimel, systimel_2, systimeh;
4369 u64 systim;
4370 /* SYSTIMH latching upon SYSTIML read does not work well.
4371 * This means that if SYSTIML overflows after we read it but before
4372 * we read SYSTIMH, the value of SYSTIMH has been incremented and we
4373 * will experience a huge non linear increment in the systime value
4374 * to fix that we test for overflow and if true, we re-read systime.
4375 */
4376 ptp_read_system_prets(sts);
4377 systimel = er32(SYSTIML);
4378 ptp_read_system_postts(sts);
4379 systimeh = er32(SYSTIMH);
4380 /* Is systimel is so large that overflow is possible? */
4381 if (systimel >= (u32)0xffffffff - E1000_TIMINCA_INCVALUE_MASK) {
4382 ptp_read_system_prets(sts);
4383 systimel_2 = er32(SYSTIML);
4384 ptp_read_system_postts(sts);
4385 if (systimel > systimel_2) {
4386 /* There was an overflow, read again SYSTIMH, and use
4387 * systimel_2
4388 */
4389 systimeh = er32(SYSTIMH);
4390 systimel = systimel_2;
4391 }
4392 }
4393 systim = (u64)systimel;
4394 systim |= (u64)systimeh << 32;
4395
4396 if (adapter->flags2 & FLAG2_CHECK_SYSTIM_OVERFLOW)
4397 systim = e1000e_sanitize_systim(hw, systim, sts);
4398
4399 return systim;
4400}
4401
4402/**
4403 * e1000e_cyclecounter_read - read raw cycle counter (used by time counter)
4404 * @cc: cyclecounter structure
4405 **/
4406static u64 e1000e_cyclecounter_read(const struct cyclecounter *cc)
4407{
4408 struct e1000_adapter *adapter = container_of(cc, struct e1000_adapter,
4409 cc);
4410
4411 return e1000e_read_systim(adapter, NULL);
4412}
4413
4414/**
4415 * e1000_sw_init - Initialize general software structures (struct e1000_adapter)
4416 * @adapter: board private structure to initialize
4417 *
4418 * e1000_sw_init initializes the Adapter private data structure.
4419 * Fields are initialized based on PCI device information and
4420 * OS network device settings (MTU size).
4421 **/
4422static int e1000_sw_init(struct e1000_adapter *adapter)
4423{
4424 struct net_device *netdev = adapter->netdev;
4425
4426 adapter->rx_buffer_len = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
4427 adapter->rx_ps_bsize0 = 128;
4428 adapter->max_frame_size = netdev->mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
4429 adapter->min_frame_size = ETH_ZLEN + ETH_FCS_LEN;
4430 adapter->tx_ring_count = E1000_DEFAULT_TXD;
4431 adapter->rx_ring_count = E1000_DEFAULT_RXD;
4432
4433 spin_lock_init(&adapter->stats64_lock);
4434
4435 e1000e_set_interrupt_capability(adapter);
4436
4437 if (e1000_alloc_queues(adapter))
4438 return -ENOMEM;
4439
4440 /* Setup hardware time stamping cyclecounter */
4441 if (adapter->flags & FLAG_HAS_HW_TIMESTAMP) {
4442 adapter->cc.read = e1000e_cyclecounter_read;
4443 adapter->cc.mask = CYCLECOUNTER_MASK(64);
4444 adapter->cc.mult = 1;
4445 /* cc.shift set in e1000e_get_base_tininca() */
4446
4447 spin_lock_init(&adapter->systim_lock);
4448 INIT_WORK(&adapter->tx_hwtstamp_work, e1000e_tx_hwtstamp_work);
4449 }
4450
4451 /* Explicitly disable IRQ since the NIC can be in any state. */
4452 e1000_irq_disable(adapter);
4453
4454 set_bit(__E1000_DOWN, &adapter->state);
4455 return 0;
4456}
4457
4458/**
4459 * e1000_intr_msi_test - Interrupt Handler
4460 * @irq: interrupt number
4461 * @data: pointer to a network interface device structure
4462 **/
4463static irqreturn_t e1000_intr_msi_test(int __always_unused irq, void *data)
4464{
4465 struct net_device *netdev = data;
4466 struct e1000_adapter *adapter = netdev_priv(netdev);
4467 struct e1000_hw *hw = &adapter->hw;
4468 u32 icr = er32(ICR);
4469
4470 e_dbg("icr is %08X\n", icr);
4471 if (icr & E1000_ICR_RXSEQ) {
4472 adapter->flags &= ~FLAG_MSI_TEST_FAILED;
4473 /* Force memory writes to complete before acknowledging the
4474 * interrupt is handled.
4475 */
4476 wmb();
4477 }
4478
4479 return IRQ_HANDLED;
4480}
4481
4482/**
4483 * e1000_test_msi_interrupt - Returns 0 for successful test
4484 * @adapter: board private struct
4485 *
4486 * code flow taken from tg3.c
4487 **/
4488static int e1000_test_msi_interrupt(struct e1000_adapter *adapter)
4489{
4490 struct net_device *netdev = adapter->netdev;
4491 struct e1000_hw *hw = &adapter->hw;
4492 int err;
4493
4494 /* poll_enable hasn't been called yet, so don't need disable */
4495 /* clear any pending events */
4496 er32(ICR);
4497
4498 /* free the real vector and request a test handler */
4499 e1000_free_irq(adapter);
4500 e1000e_reset_interrupt_capability(adapter);
4501
4502 /* Assume that the test fails, if it succeeds then the test
4503 * MSI irq handler will unset this flag
4504 */
4505 adapter->flags |= FLAG_MSI_TEST_FAILED;
4506
4507 err = pci_enable_msi(adapter->pdev);
4508 if (err)
4509 goto msi_test_failed;
4510
4511 err = request_irq(adapter->pdev->irq, e1000_intr_msi_test, 0,
4512 netdev->name, netdev);
4513 if (err) {
4514 pci_disable_msi(adapter->pdev);
4515 goto msi_test_failed;
4516 }
4517
4518 /* Force memory writes to complete before enabling and firing an
4519 * interrupt.
4520 */
4521 wmb();
4522
4523 e1000_irq_enable(adapter);
4524
4525 /* fire an unusual interrupt on the test handler */
4526 ew32(ICS, E1000_ICS_RXSEQ);
4527 e1e_flush();
4528 msleep(100);
4529
4530 e1000_irq_disable(adapter);
4531
4532 rmb(); /* read flags after interrupt has been fired */
4533
4534 if (adapter->flags & FLAG_MSI_TEST_FAILED) {
4535 adapter->int_mode = E1000E_INT_MODE_LEGACY;
4536 e_info("MSI interrupt test failed, using legacy interrupt.\n");
4537 } else {
4538 e_dbg("MSI interrupt test succeeded!\n");
4539 }
4540
4541 free_irq(adapter->pdev->irq, netdev);
4542 pci_disable_msi(adapter->pdev);
4543
4544msi_test_failed:
4545 e1000e_set_interrupt_capability(adapter);
4546 return e1000_request_irq(adapter);
4547}
4548
4549/**
4550 * e1000_test_msi - Returns 0 if MSI test succeeds or INTx mode is restored
4551 * @adapter: board private struct
4552 *
4553 * code flow taken from tg3.c, called with e1000 interrupts disabled.
4554 **/
4555static int e1000_test_msi(struct e1000_adapter *adapter)
4556{
4557 int err;
4558 u16 pci_cmd;
4559
4560 if (!(adapter->flags & FLAG_MSI_ENABLED))
4561 return 0;
4562
4563 /* disable SERR in case the MSI write causes a master abort */
4564 pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
4565 if (pci_cmd & PCI_COMMAND_SERR)
4566 pci_write_config_word(adapter->pdev, PCI_COMMAND,
4567 pci_cmd & ~PCI_COMMAND_SERR);
4568
4569 err = e1000_test_msi_interrupt(adapter);
4570
4571 /* re-enable SERR */
4572 if (pci_cmd & PCI_COMMAND_SERR) {
4573 pci_read_config_word(adapter->pdev, PCI_COMMAND, &pci_cmd);
4574 pci_cmd |= PCI_COMMAND_SERR;
4575 pci_write_config_word(adapter->pdev, PCI_COMMAND, pci_cmd);
4576 }
4577
4578 return err;
4579}
4580
4581/**
4582 * e1000e_open - Called when a network interface is made active
4583 * @netdev: network interface device structure
4584 *
4585 * Returns 0 on success, negative value on failure
4586 *
4587 * The open entry point is called when a network interface is made
4588 * active by the system (IFF_UP). At this point all resources needed
4589 * for transmit and receive operations are allocated, the interrupt
4590 * handler is registered with the OS, the watchdog timer is started,
4591 * and the stack is notified that the interface is ready.
4592 **/
4593int e1000e_open(struct net_device *netdev)
4594{
4595 struct e1000_adapter *adapter = netdev_priv(netdev);
4596 struct e1000_hw *hw = &adapter->hw;
4597 struct pci_dev *pdev = adapter->pdev;
4598 int err;
4599
4600 /* disallow open during test */
4601 if (test_bit(__E1000_TESTING, &adapter->state))
4602 return -EBUSY;
4603
4604 pm_runtime_get_sync(&pdev->dev);
4605
4606 netif_carrier_off(netdev);
4607 netif_stop_queue(netdev);
4608
4609 /* allocate transmit descriptors */
4610 err = e1000e_setup_tx_resources(adapter->tx_ring);
4611 if (err)
4612 goto err_setup_tx;
4613
4614 /* allocate receive descriptors */
4615 err = e1000e_setup_rx_resources(adapter->rx_ring);
4616 if (err)
4617 goto err_setup_rx;
4618
4619 /* If AMT is enabled, let the firmware know that the network
4620 * interface is now open and reset the part to a known state.
4621 */
4622 if (adapter->flags & FLAG_HAS_AMT) {
4623 e1000e_get_hw_control(adapter);
4624 e1000e_reset(adapter);
4625 }
4626
4627 e1000e_power_up_phy(adapter);
4628
4629 adapter->mng_vlan_id = E1000_MNG_VLAN_NONE;
4630 if ((adapter->hw.mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN))
4631 e1000_update_mng_vlan(adapter);
4632
4633 /* DMA latency requirement to workaround jumbo issue */
4634 pm_qos_add_request(&adapter->pm_qos_req, PM_QOS_CPU_DMA_LATENCY,
4635 PM_QOS_DEFAULT_VALUE);
4636
4637 /* before we allocate an interrupt, we must be ready to handle it.
4638 * Setting DEBUG_SHIRQ in the kernel makes it fire an interrupt
4639 * as soon as we call pci_request_irq, so we have to setup our
4640 * clean_rx handler before we do so.
4641 */
4642 e1000_configure(adapter);
4643
4644 err = e1000_request_irq(adapter);
4645 if (err)
4646 goto err_req_irq;
4647
4648 /* Work around PCIe errata with MSI interrupts causing some chipsets to
4649 * ignore e1000e MSI messages, which means we need to test our MSI
4650 * interrupt now
4651 */
4652 if (adapter->int_mode != E1000E_INT_MODE_LEGACY) {
4653 err = e1000_test_msi(adapter);
4654 if (err) {
4655 e_err("Interrupt allocation failed\n");
4656 goto err_req_irq;
4657 }
4658 }
4659
4660 /* From here on the code is the same as e1000e_up() */
4661 clear_bit(__E1000_DOWN, &adapter->state);
4662
4663 napi_enable(&adapter->napi);
4664
4665 e1000_irq_enable(adapter);
4666
4667 adapter->tx_hang_recheck = false;
4668
4669 hw->mac.get_link_status = true;
4670 pm_runtime_put(&pdev->dev);
4671
4672 e1000e_trigger_lsc(adapter);
4673
4674 return 0;
4675
4676err_req_irq:
4677 pm_qos_remove_request(&adapter->pm_qos_req);
4678 e1000e_release_hw_control(adapter);
4679 e1000_power_down_phy(adapter);
4680 e1000e_free_rx_resources(adapter->rx_ring);
4681err_setup_rx:
4682 e1000e_free_tx_resources(adapter->tx_ring);
4683err_setup_tx:
4684 e1000e_reset(adapter);
4685 pm_runtime_put_sync(&pdev->dev);
4686
4687 return err;
4688}
4689
4690/**
4691 * e1000e_close - Disables a network interface
4692 * @netdev: network interface device structure
4693 *
4694 * Returns 0, this is not allowed to fail
4695 *
4696 * The close entry point is called when an interface is de-activated
4697 * by the OS. The hardware is still under the drivers control, but
4698 * needs to be disabled. A global MAC reset is issued to stop the
4699 * hardware, and all transmit and receive resources are freed.
4700 **/
4701int e1000e_close(struct net_device *netdev)
4702{
4703 struct e1000_adapter *adapter = netdev_priv(netdev);
4704 struct pci_dev *pdev = adapter->pdev;
4705 int count = E1000_CHECK_RESET_COUNT;
4706
4707 while (test_bit(__E1000_RESETTING, &adapter->state) && count--)
4708 usleep_range(10000, 11000);
4709
4710 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
4711
4712 pm_runtime_get_sync(&pdev->dev);
4713
4714 if (netif_device_present(netdev)) {
4715 e1000e_down(adapter, true);
4716 e1000_free_irq(adapter);
4717
4718 /* Link status message must follow this format */
4719 pr_info("%s NIC Link is Down\n", netdev->name);
4720 }
4721
4722 napi_disable(&adapter->napi);
4723
4724 e1000e_free_tx_resources(adapter->tx_ring);
4725 e1000e_free_rx_resources(adapter->rx_ring);
4726
4727 /* kill manageability vlan ID if supported, but not if a vlan with
4728 * the same ID is registered on the host OS (let 8021q kill it)
4729 */
4730 if (adapter->hw.mng_cookie.status & E1000_MNG_DHCP_COOKIE_STATUS_VLAN)
4731 e1000_vlan_rx_kill_vid(netdev, htons(ETH_P_8021Q),
4732 adapter->mng_vlan_id);
4733
4734 /* If AMT is enabled, let the firmware know that the network
4735 * interface is now closed
4736 */
4737 if ((adapter->flags & FLAG_HAS_AMT) &&
4738 !test_bit(__E1000_TESTING, &adapter->state))
4739 e1000e_release_hw_control(adapter);
4740
4741 pm_qos_remove_request(&adapter->pm_qos_req);
4742
4743 pm_runtime_put_sync(&pdev->dev);
4744
4745 return 0;
4746}
4747
4748/**
4749 * e1000_set_mac - Change the Ethernet Address of the NIC
4750 * @netdev: network interface device structure
4751 * @p: pointer to an address structure
4752 *
4753 * Returns 0 on success, negative on failure
4754 **/
4755static int e1000_set_mac(struct net_device *netdev, void *p)
4756{
4757 struct e1000_adapter *adapter = netdev_priv(netdev);
4758 struct e1000_hw *hw = &adapter->hw;
4759 struct sockaddr *addr = p;
4760
4761 if (!is_valid_ether_addr(addr->sa_data))
4762 return -EADDRNOTAVAIL;
4763
4764 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
4765 memcpy(adapter->hw.mac.addr, addr->sa_data, netdev->addr_len);
4766
4767 hw->mac.ops.rar_set(&adapter->hw, adapter->hw.mac.addr, 0);
4768
4769 if (adapter->flags & FLAG_RESET_OVERWRITES_LAA) {
4770 /* activate the work around */
4771 e1000e_set_laa_state_82571(&adapter->hw, 1);
4772
4773 /* Hold a copy of the LAA in RAR[14] This is done so that
4774 * between the time RAR[0] gets clobbered and the time it
4775 * gets fixed (in e1000_watchdog), the actual LAA is in one
4776 * of the RARs and no incoming packets directed to this port
4777 * are dropped. Eventually the LAA will be in RAR[0] and
4778 * RAR[14]
4779 */
4780 hw->mac.ops.rar_set(&adapter->hw, adapter->hw.mac.addr,
4781 adapter->hw.mac.rar_entry_count - 1);
4782 }
4783
4784 return 0;
4785}
4786
4787/**
4788 * e1000e_update_phy_task - work thread to update phy
4789 * @work: pointer to our work struct
4790 *
4791 * this worker thread exists because we must acquire a
4792 * semaphore to read the phy, which we could msleep while
4793 * waiting for it, and we can't msleep in a timer.
4794 **/
4795static void e1000e_update_phy_task(struct work_struct *work)
4796{
4797 struct e1000_adapter *adapter = container_of(work,
4798 struct e1000_adapter,
4799 update_phy_task);
4800 struct e1000_hw *hw = &adapter->hw;
4801
4802 if (test_bit(__E1000_DOWN, &adapter->state))
4803 return;
4804
4805 e1000_get_phy_info(hw);
4806
4807 /* Enable EEE on 82579 after link up */
4808 if (hw->phy.type >= e1000_phy_82579)
4809 e1000_set_eee_pchlan(hw);
4810}
4811
4812/**
4813 * e1000_update_phy_info - timre call-back to update PHY info
4814 * @data: pointer to adapter cast into an unsigned long
4815 *
4816 * Need to wait a few seconds after link up to get diagnostic information from
4817 * the phy
4818 **/
4819static void e1000_update_phy_info(struct timer_list *t)
4820{
4821 struct e1000_adapter *adapter = from_timer(adapter, t, phy_info_timer);
4822
4823 if (test_bit(__E1000_DOWN, &adapter->state))
4824 return;
4825
4826 schedule_work(&adapter->update_phy_task);
4827}
4828
4829/**
4830 * e1000e_update_phy_stats - Update the PHY statistics counters
4831 * @adapter: board private structure
4832 *
4833 * Read/clear the upper 16-bit PHY registers and read/accumulate lower
4834 **/
4835static void e1000e_update_phy_stats(struct e1000_adapter *adapter)
4836{
4837 struct e1000_hw *hw = &adapter->hw;
4838 s32 ret_val;
4839 u16 phy_data;
4840
4841 ret_val = hw->phy.ops.acquire(hw);
4842 if (ret_val)
4843 return;
4844
4845 /* A page set is expensive so check if already on desired page.
4846 * If not, set to the page with the PHY status registers.
4847 */
4848 hw->phy.addr = 1;
4849 ret_val = e1000e_read_phy_reg_mdic(hw, IGP01E1000_PHY_PAGE_SELECT,
4850 &phy_data);
4851 if (ret_val)
4852 goto release;
4853 if (phy_data != (HV_STATS_PAGE << IGP_PAGE_SHIFT)) {
4854 ret_val = hw->phy.ops.set_page(hw,
4855 HV_STATS_PAGE << IGP_PAGE_SHIFT);
4856 if (ret_val)
4857 goto release;
4858 }
4859
4860 /* Single Collision Count */
4861 hw->phy.ops.read_reg_page(hw, HV_SCC_UPPER, &phy_data);
4862 ret_val = hw->phy.ops.read_reg_page(hw, HV_SCC_LOWER, &phy_data);
4863 if (!ret_val)
4864 adapter->stats.scc += phy_data;
4865
4866 /* Excessive Collision Count */
4867 hw->phy.ops.read_reg_page(hw, HV_ECOL_UPPER, &phy_data);
4868 ret_val = hw->phy.ops.read_reg_page(hw, HV_ECOL_LOWER, &phy_data);
4869 if (!ret_val)
4870 adapter->stats.ecol += phy_data;
4871
4872 /* Multiple Collision Count */
4873 hw->phy.ops.read_reg_page(hw, HV_MCC_UPPER, &phy_data);
4874 ret_val = hw->phy.ops.read_reg_page(hw, HV_MCC_LOWER, &phy_data);
4875 if (!ret_val)
4876 adapter->stats.mcc += phy_data;
4877
4878 /* Late Collision Count */
4879 hw->phy.ops.read_reg_page(hw, HV_LATECOL_UPPER, &phy_data);
4880 ret_val = hw->phy.ops.read_reg_page(hw, HV_LATECOL_LOWER, &phy_data);
4881 if (!ret_val)
4882 adapter->stats.latecol += phy_data;
4883
4884 /* Collision Count - also used for adaptive IFS */
4885 hw->phy.ops.read_reg_page(hw, HV_COLC_UPPER, &phy_data);
4886 ret_val = hw->phy.ops.read_reg_page(hw, HV_COLC_LOWER, &phy_data);
4887 if (!ret_val)
4888 hw->mac.collision_delta = phy_data;
4889
4890 /* Defer Count */
4891 hw->phy.ops.read_reg_page(hw, HV_DC_UPPER, &phy_data);
4892 ret_val = hw->phy.ops.read_reg_page(hw, HV_DC_LOWER, &phy_data);
4893 if (!ret_val)
4894 adapter->stats.dc += phy_data;
4895
4896 /* Transmit with no CRS */
4897 hw->phy.ops.read_reg_page(hw, HV_TNCRS_UPPER, &phy_data);
4898 ret_val = hw->phy.ops.read_reg_page(hw, HV_TNCRS_LOWER, &phy_data);
4899 if (!ret_val)
4900 adapter->stats.tncrs += phy_data;
4901
4902release:
4903 hw->phy.ops.release(hw);
4904}
4905
4906/**
4907 * e1000e_update_stats - Update the board statistics counters
4908 * @adapter: board private structure
4909 **/
4910static void e1000e_update_stats(struct e1000_adapter *adapter)
4911{
4912 struct net_device *netdev = adapter->netdev;
4913 struct e1000_hw *hw = &adapter->hw;
4914 struct pci_dev *pdev = adapter->pdev;
4915
4916 /* Prevent stats update while adapter is being reset, or if the pci
4917 * connection is down.
4918 */
4919 if (adapter->link_speed == 0)
4920 return;
4921 if (pci_channel_offline(pdev))
4922 return;
4923
4924 adapter->stats.crcerrs += er32(CRCERRS);
4925 adapter->stats.gprc += er32(GPRC);
4926 adapter->stats.gorc += er32(GORCL);
4927 er32(GORCH); /* Clear gorc */
4928 adapter->stats.bprc += er32(BPRC);
4929 adapter->stats.mprc += er32(MPRC);
4930 adapter->stats.roc += er32(ROC);
4931
4932 adapter->stats.mpc += er32(MPC);
4933
4934 /* Half-duplex statistics */
4935 if (adapter->link_duplex == HALF_DUPLEX) {
4936 if (adapter->flags2 & FLAG2_HAS_PHY_STATS) {
4937 e1000e_update_phy_stats(adapter);
4938 } else {
4939 adapter->stats.scc += er32(SCC);
4940 adapter->stats.ecol += er32(ECOL);
4941 adapter->stats.mcc += er32(MCC);
4942 adapter->stats.latecol += er32(LATECOL);
4943 adapter->stats.dc += er32(DC);
4944
4945 hw->mac.collision_delta = er32(COLC);
4946
4947 if ((hw->mac.type != e1000_82574) &&
4948 (hw->mac.type != e1000_82583))
4949 adapter->stats.tncrs += er32(TNCRS);
4950 }
4951 adapter->stats.colc += hw->mac.collision_delta;
4952 }
4953
4954 adapter->stats.xonrxc += er32(XONRXC);
4955 adapter->stats.xontxc += er32(XONTXC);
4956 adapter->stats.xoffrxc += er32(XOFFRXC);
4957 adapter->stats.xofftxc += er32(XOFFTXC);
4958 adapter->stats.gptc += er32(GPTC);
4959 adapter->stats.gotc += er32(GOTCL);
4960 er32(GOTCH); /* Clear gotc */
4961 adapter->stats.rnbc += er32(RNBC);
4962 adapter->stats.ruc += er32(RUC);
4963
4964 adapter->stats.mptc += er32(MPTC);
4965 adapter->stats.bptc += er32(BPTC);
4966
4967 /* used for adaptive IFS */
4968
4969 hw->mac.tx_packet_delta = er32(TPT);
4970 adapter->stats.tpt += hw->mac.tx_packet_delta;
4971
4972 adapter->stats.algnerrc += er32(ALGNERRC);
4973 adapter->stats.rxerrc += er32(RXERRC);
4974 adapter->stats.cexterr += er32(CEXTERR);
4975 adapter->stats.tsctc += er32(TSCTC);
4976 adapter->stats.tsctfc += er32(TSCTFC);
4977
4978 /* Fill out the OS statistics structure */
4979 netdev->stats.multicast = adapter->stats.mprc;
4980 netdev->stats.collisions = adapter->stats.colc;
4981
4982 /* Rx Errors */
4983
4984 /* RLEC on some newer hardware can be incorrect so build
4985 * our own version based on RUC and ROC
4986 */
4987 netdev->stats.rx_errors = adapter->stats.rxerrc +
4988 adapter->stats.crcerrs + adapter->stats.algnerrc +
4989 adapter->stats.ruc + adapter->stats.roc + adapter->stats.cexterr;
4990 netdev->stats.rx_length_errors = adapter->stats.ruc +
4991 adapter->stats.roc;
4992 netdev->stats.rx_crc_errors = adapter->stats.crcerrs;
4993 netdev->stats.rx_frame_errors = adapter->stats.algnerrc;
4994 netdev->stats.rx_missed_errors = adapter->stats.mpc;
4995
4996 /* Tx Errors */
4997 netdev->stats.tx_errors = adapter->stats.ecol + adapter->stats.latecol;
4998 netdev->stats.tx_aborted_errors = adapter->stats.ecol;
4999 netdev->stats.tx_window_errors = adapter->stats.latecol;
5000 netdev->stats.tx_carrier_errors = adapter->stats.tncrs;
5001
5002 /* Tx Dropped needs to be maintained elsewhere */
5003
5004 /* Management Stats */
5005 adapter->stats.mgptc += er32(MGTPTC);
5006 adapter->stats.mgprc += er32(MGTPRC);
5007 adapter->stats.mgpdc += er32(MGTPDC);
5008
5009 /* Correctable ECC Errors */
5010 if (hw->mac.type >= e1000_pch_lpt) {
5011 u32 pbeccsts = er32(PBECCSTS);
5012
5013 adapter->corr_errors +=
5014 pbeccsts & E1000_PBECCSTS_CORR_ERR_CNT_MASK;
5015 adapter->uncorr_errors +=
5016 (pbeccsts & E1000_PBECCSTS_UNCORR_ERR_CNT_MASK) >>
5017 E1000_PBECCSTS_UNCORR_ERR_CNT_SHIFT;
5018 }
5019}
5020
5021/**
5022 * e1000_phy_read_status - Update the PHY register status snapshot
5023 * @adapter: board private structure
5024 **/
5025static void e1000_phy_read_status(struct e1000_adapter *adapter)
5026{
5027 struct e1000_hw *hw = &adapter->hw;
5028 struct e1000_phy_regs *phy = &adapter->phy_regs;
5029
5030 if (!pm_runtime_suspended((&adapter->pdev->dev)->parent) &&
5031 (er32(STATUS) & E1000_STATUS_LU) &&
5032 (adapter->hw.phy.media_type == e1000_media_type_copper)) {
5033 int ret_val;
5034
5035 ret_val = e1e_rphy(hw, MII_BMCR, &phy->bmcr);
5036 ret_val |= e1e_rphy(hw, MII_BMSR, &phy->bmsr);
5037 ret_val |= e1e_rphy(hw, MII_ADVERTISE, &phy->advertise);
5038 ret_val |= e1e_rphy(hw, MII_LPA, &phy->lpa);
5039 ret_val |= e1e_rphy(hw, MII_EXPANSION, &phy->expansion);
5040 ret_val |= e1e_rphy(hw, MII_CTRL1000, &phy->ctrl1000);
5041 ret_val |= e1e_rphy(hw, MII_STAT1000, &phy->stat1000);
5042 ret_val |= e1e_rphy(hw, MII_ESTATUS, &phy->estatus);
5043 if (ret_val)
5044 e_warn("Error reading PHY register\n");
5045 } else {
5046 /* Do not read PHY registers if link is not up
5047 * Set values to typical power-on defaults
5048 */
5049 phy->bmcr = (BMCR_SPEED1000 | BMCR_ANENABLE | BMCR_FULLDPLX);
5050 phy->bmsr = (BMSR_100FULL | BMSR_100HALF | BMSR_10FULL |
5051 BMSR_10HALF | BMSR_ESTATEN | BMSR_ANEGCAPABLE |
5052 BMSR_ERCAP);
5053 phy->advertise = (ADVERTISE_PAUSE_ASYM | ADVERTISE_PAUSE_CAP |
5054 ADVERTISE_ALL | ADVERTISE_CSMA);
5055 phy->lpa = 0;
5056 phy->expansion = EXPANSION_ENABLENPAGE;
5057 phy->ctrl1000 = ADVERTISE_1000FULL;
5058 phy->stat1000 = 0;
5059 phy->estatus = (ESTATUS_1000_TFULL | ESTATUS_1000_THALF);
5060 }
5061}
5062
5063static void e1000_print_link_info(struct e1000_adapter *adapter)
5064{
5065 struct e1000_hw *hw = &adapter->hw;
5066 u32 ctrl = er32(CTRL);
5067
5068 /* Link status message must follow this format for user tools */
5069 pr_info("%s NIC Link is Up %d Mbps %s Duplex, Flow Control: %s\n",
5070 adapter->netdev->name, adapter->link_speed,
5071 adapter->link_duplex == FULL_DUPLEX ? "Full" : "Half",
5072 (ctrl & E1000_CTRL_TFCE) && (ctrl & E1000_CTRL_RFCE) ? "Rx/Tx" :
5073 (ctrl & E1000_CTRL_RFCE) ? "Rx" :
5074 (ctrl & E1000_CTRL_TFCE) ? "Tx" : "None");
5075}
5076
5077static bool e1000e_has_link(struct e1000_adapter *adapter)
5078{
5079 struct e1000_hw *hw = &adapter->hw;
5080 bool link_active = false;
5081 s32 ret_val = 0;
5082
5083 /* get_link_status is set on LSC (link status) interrupt or
5084 * Rx sequence error interrupt. get_link_status will stay
5085 * true until the check_for_link establishes link
5086 * for copper adapters ONLY
5087 */
5088 switch (hw->phy.media_type) {
5089 case e1000_media_type_copper:
5090 if (hw->mac.get_link_status) {
5091 ret_val = hw->mac.ops.check_for_link(hw);
5092 link_active = !hw->mac.get_link_status;
5093 } else {
5094 link_active = true;
5095 }
5096 break;
5097 case e1000_media_type_fiber:
5098 ret_val = hw->mac.ops.check_for_link(hw);
5099 link_active = !!(er32(STATUS) & E1000_STATUS_LU);
5100 break;
5101 case e1000_media_type_internal_serdes:
5102 ret_val = hw->mac.ops.check_for_link(hw);
5103 link_active = hw->mac.serdes_has_link;
5104 break;
5105 default:
5106 case e1000_media_type_unknown:
5107 break;
5108 }
5109
5110 if ((ret_val == -E1000_ERR_PHY) && (hw->phy.type == e1000_phy_igp_3) &&
5111 (er32(CTRL) & E1000_PHY_CTRL_GBE_DISABLE)) {
5112 /* See e1000_kmrn_lock_loss_workaround_ich8lan() */
5113 e_info("Gigabit has been disabled, downgrading speed\n");
5114 }
5115
5116 return link_active;
5117}
5118
5119static void e1000e_enable_receives(struct e1000_adapter *adapter)
5120{
5121 /* make sure the receive unit is started */
5122 if ((adapter->flags & FLAG_RX_NEEDS_RESTART) &&
5123 (adapter->flags & FLAG_RESTART_NOW)) {
5124 struct e1000_hw *hw = &adapter->hw;
5125 u32 rctl = er32(RCTL);
5126
5127 ew32(RCTL, rctl | E1000_RCTL_EN);
5128 adapter->flags &= ~FLAG_RESTART_NOW;
5129 }
5130}
5131
5132static void e1000e_check_82574_phy_workaround(struct e1000_adapter *adapter)
5133{
5134 struct e1000_hw *hw = &adapter->hw;
5135
5136 /* With 82574 controllers, PHY needs to be checked periodically
5137 * for hung state and reset, if two calls return true
5138 */
5139 if (e1000_check_phy_82574(hw))
5140 adapter->phy_hang_count++;
5141 else
5142 adapter->phy_hang_count = 0;
5143
5144 if (adapter->phy_hang_count > 1) {
5145 adapter->phy_hang_count = 0;
5146 e_dbg("PHY appears hung - resetting\n");
5147 schedule_work(&adapter->reset_task);
5148 }
5149}
5150
5151/**
5152 * e1000_watchdog - Timer Call-back
5153 * @data: pointer to adapter cast into an unsigned long
5154 **/
5155static void e1000_watchdog(struct timer_list *t)
5156{
5157 struct e1000_adapter *adapter = from_timer(adapter, t, watchdog_timer);
5158
5159 /* Do the rest outside of interrupt context */
5160 schedule_work(&adapter->watchdog_task);
5161
5162 /* TODO: make this use queue_delayed_work() */
5163}
5164
5165static void e1000_watchdog_task(struct work_struct *work)
5166{
5167 struct e1000_adapter *adapter = container_of(work,
5168 struct e1000_adapter,
5169 watchdog_task);
5170 struct net_device *netdev = adapter->netdev;
5171 struct e1000_mac_info *mac = &adapter->hw.mac;
5172 struct e1000_phy_info *phy = &adapter->hw.phy;
5173 struct e1000_ring *tx_ring = adapter->tx_ring;
5174 u32 dmoff_exit_timeout = 100, tries = 0;
5175 struct e1000_hw *hw = &adapter->hw;
5176 u32 link, tctl, pcim_state;
5177
5178 if (test_bit(__E1000_DOWN, &adapter->state))
5179 return;
5180
5181 link = e1000e_has_link(adapter);
5182 if ((netif_carrier_ok(netdev)) && link) {
5183 /* Cancel scheduled suspend requests. */
5184 pm_runtime_resume(netdev->dev.parent);
5185
5186 e1000e_enable_receives(adapter);
5187 goto link_up;
5188 }
5189
5190 if ((e1000e_enable_tx_pkt_filtering(hw)) &&
5191 (adapter->mng_vlan_id != adapter->hw.mng_cookie.vlan_id))
5192 e1000_update_mng_vlan(adapter);
5193
5194 if (link) {
5195 if (!netif_carrier_ok(netdev)) {
5196 bool txb2b = true;
5197
5198 /* Cancel scheduled suspend requests. */
5199 pm_runtime_resume(netdev->dev.parent);
5200
5201 /* Checking if MAC is in DMoff state*/
5202 if (er32(FWSM) & E1000_ICH_FWSM_FW_VALID) {
5203 pcim_state = er32(STATUS);
5204 while (pcim_state & E1000_STATUS_PCIM_STATE) {
5205 if (tries++ == dmoff_exit_timeout) {
5206 e_dbg("Error in exiting dmoff\n");
5207 break;
5208 }
5209 usleep_range(10000, 20000);
5210 pcim_state = er32(STATUS);
5211
5212 /* Checking if MAC exited DMoff state */
5213 if (!(pcim_state & E1000_STATUS_PCIM_STATE))
5214 e1000_phy_hw_reset(&adapter->hw);
5215 }
5216 }
5217
5218 /* update snapshot of PHY registers on LSC */
5219 e1000_phy_read_status(adapter);
5220 mac->ops.get_link_up_info(&adapter->hw,
5221 &adapter->link_speed,
5222 &adapter->link_duplex);
5223 e1000_print_link_info(adapter);
5224
5225 /* check if SmartSpeed worked */
5226 e1000e_check_downshift(hw);
5227 if (phy->speed_downgraded)
5228 netdev_warn(netdev,
5229 "Link Speed was downgraded by SmartSpeed\n");
5230
5231 /* On supported PHYs, check for duplex mismatch only
5232 * if link has autonegotiated at 10/100 half
5233 */
5234 if ((hw->phy.type == e1000_phy_igp_3 ||
5235 hw->phy.type == e1000_phy_bm) &&
5236 hw->mac.autoneg &&
5237 (adapter->link_speed == SPEED_10 ||
5238 adapter->link_speed == SPEED_100) &&
5239 (adapter->link_duplex == HALF_DUPLEX)) {
5240 u16 autoneg_exp;
5241
5242 e1e_rphy(hw, MII_EXPANSION, &autoneg_exp);
5243
5244 if (!(autoneg_exp & EXPANSION_NWAY))
5245 e_info("Autonegotiated half duplex but link partner cannot autoneg. Try forcing full duplex if link gets many collisions.\n");
5246 }
5247
5248 /* adjust timeout factor according to speed/duplex */
5249 adapter->tx_timeout_factor = 1;
5250 switch (adapter->link_speed) {
5251 case SPEED_10:
5252 txb2b = false;
5253 adapter->tx_timeout_factor = 16;
5254 break;
5255 case SPEED_100:
5256 txb2b = false;
5257 adapter->tx_timeout_factor = 10;
5258 break;
5259 }
5260
5261 /* workaround: re-program speed mode bit after
5262 * link-up event
5263 */
5264 if ((adapter->flags & FLAG_TARC_SPEED_MODE_BIT) &&
5265 !txb2b) {
5266 u32 tarc0;
5267
5268 tarc0 = er32(TARC(0));
5269 tarc0 &= ~SPEED_MODE_BIT;
5270 ew32(TARC(0), tarc0);
5271 }
5272
5273 /* enable transmits in the hardware, need to do this
5274 * after setting TARC(0)
5275 */
5276 tctl = er32(TCTL);
5277 tctl |= E1000_TCTL_EN;
5278 ew32(TCTL, tctl);
5279
5280 /* Perform any post-link-up configuration before
5281 * reporting link up.
5282 */
5283 if (phy->ops.cfg_on_link_up)
5284 phy->ops.cfg_on_link_up(hw);
5285
5286 netif_wake_queue(netdev);
5287 netif_carrier_on(netdev);
5288
5289 if (!test_bit(__E1000_DOWN, &adapter->state))
5290 mod_timer(&adapter->phy_info_timer,
5291 round_jiffies(jiffies + 2 * HZ));
5292 }
5293 } else {
5294 if (netif_carrier_ok(netdev)) {
5295 adapter->link_speed = 0;
5296 adapter->link_duplex = 0;
5297 /* Link status message must follow this format */
5298 pr_info("%s NIC Link is Down\n", adapter->netdev->name);
5299 netif_carrier_off(netdev);
5300 netif_stop_queue(netdev);
5301 if (!test_bit(__E1000_DOWN, &adapter->state))
5302 mod_timer(&adapter->phy_info_timer,
5303 round_jiffies(jiffies + 2 * HZ));
5304
5305 /* 8000ES2LAN requires a Rx packet buffer work-around
5306 * on link down event; reset the controller to flush
5307 * the Rx packet buffer.
5308 */
5309 if (adapter->flags & FLAG_RX_NEEDS_RESTART)
5310 adapter->flags |= FLAG_RESTART_NOW;
5311 else
5312 pm_schedule_suspend(netdev->dev.parent,
5313 LINK_TIMEOUT);
5314 }
5315 }
5316
5317link_up:
5318 spin_lock(&adapter->stats64_lock);
5319 e1000e_update_stats(adapter);
5320
5321 mac->tx_packet_delta = adapter->stats.tpt - adapter->tpt_old;
5322 adapter->tpt_old = adapter->stats.tpt;
5323 mac->collision_delta = adapter->stats.colc - adapter->colc_old;
5324 adapter->colc_old = adapter->stats.colc;
5325
5326 adapter->gorc = adapter->stats.gorc - adapter->gorc_old;
5327 adapter->gorc_old = adapter->stats.gorc;
5328 adapter->gotc = adapter->stats.gotc - adapter->gotc_old;
5329 adapter->gotc_old = adapter->stats.gotc;
5330 spin_unlock(&adapter->stats64_lock);
5331
5332 /* If the link is lost the controller stops DMA, but
5333 * if there is queued Tx work it cannot be done. So
5334 * reset the controller to flush the Tx packet buffers.
5335 */
5336 if (!netif_carrier_ok(netdev) &&
5337 (e1000_desc_unused(tx_ring) + 1 < tx_ring->count))
5338 adapter->flags |= FLAG_RESTART_NOW;
5339
5340 /* If reset is necessary, do it outside of interrupt context. */
5341 if (adapter->flags & FLAG_RESTART_NOW) {
5342 schedule_work(&adapter->reset_task);
5343 /* return immediately since reset is imminent */
5344 return;
5345 }
5346
5347 e1000e_update_adaptive(&adapter->hw);
5348
5349 /* Simple mode for Interrupt Throttle Rate (ITR) */
5350 if (adapter->itr_setting == 4) {
5351 /* Symmetric Tx/Rx gets a reduced ITR=2000;
5352 * Total asymmetrical Tx or Rx gets ITR=8000;
5353 * everyone else is between 2000-8000.
5354 */
5355 u32 goc = (adapter->gotc + adapter->gorc) / 10000;
5356 u32 dif = (adapter->gotc > adapter->gorc ?
5357 adapter->gotc - adapter->gorc :
5358 adapter->gorc - adapter->gotc) / 10000;
5359 u32 itr = goc > 0 ? (dif * 6000 / goc + 2000) : 8000;
5360
5361 e1000e_write_itr(adapter, itr);
5362 }
5363
5364 /* Cause software interrupt to ensure Rx ring is cleaned */
5365 if (adapter->msix_entries)
5366 ew32(ICS, adapter->rx_ring->ims_val);
5367 else
5368 ew32(ICS, E1000_ICS_RXDMT0);
5369
5370 /* flush pending descriptors to memory before detecting Tx hang */
5371 e1000e_flush_descriptors(adapter);
5372
5373 /* Force detection of hung controller every watchdog period */
5374 adapter->detect_tx_hung = true;
5375
5376 /* With 82571 controllers, LAA may be overwritten due to controller
5377 * reset from the other port. Set the appropriate LAA in RAR[0]
5378 */
5379 if (e1000e_get_laa_state_82571(hw))
5380 hw->mac.ops.rar_set(hw, adapter->hw.mac.addr, 0);
5381
5382 if (adapter->flags2 & FLAG2_CHECK_PHY_HANG)
5383 e1000e_check_82574_phy_workaround(adapter);
5384
5385 /* Clear valid timestamp stuck in RXSTMPL/H due to a Rx error */
5386 if (adapter->hwtstamp_config.rx_filter != HWTSTAMP_FILTER_NONE) {
5387 if ((adapter->flags2 & FLAG2_CHECK_RX_HWTSTAMP) &&
5388 (er32(TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID)) {
5389 er32(RXSTMPH);
5390 adapter->rx_hwtstamp_cleared++;
5391 } else {
5392 adapter->flags2 |= FLAG2_CHECK_RX_HWTSTAMP;
5393 }
5394 }
5395
5396 /* Reset the timer */
5397 if (!test_bit(__E1000_DOWN, &adapter->state))
5398 mod_timer(&adapter->watchdog_timer,
5399 round_jiffies(jiffies + 2 * HZ));
5400}
5401
5402#define E1000_TX_FLAGS_CSUM 0x00000001
5403#define E1000_TX_FLAGS_VLAN 0x00000002
5404#define E1000_TX_FLAGS_TSO 0x00000004
5405#define E1000_TX_FLAGS_IPV4 0x00000008
5406#define E1000_TX_FLAGS_NO_FCS 0x00000010
5407#define E1000_TX_FLAGS_HWTSTAMP 0x00000020
5408#define E1000_TX_FLAGS_VLAN_MASK 0xffff0000
5409#define E1000_TX_FLAGS_VLAN_SHIFT 16
5410
5411static int e1000_tso(struct e1000_ring *tx_ring, struct sk_buff *skb,
5412 __be16 protocol)
5413{
5414 struct e1000_context_desc *context_desc;
5415 struct e1000_buffer *buffer_info;
5416 unsigned int i;
5417 u32 cmd_length = 0;
5418 u16 ipcse = 0, mss;
5419 u8 ipcss, ipcso, tucss, tucso, hdr_len;
5420 int err;
5421
5422 if (!skb_is_gso(skb))
5423 return 0;
5424
5425 err = skb_cow_head(skb, 0);
5426 if (err < 0)
5427 return err;
5428
5429 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
5430 mss = skb_shinfo(skb)->gso_size;
5431 if (protocol == htons(ETH_P_IP)) {
5432 struct iphdr *iph = ip_hdr(skb);
5433 iph->tot_len = 0;
5434 iph->check = 0;
5435 tcp_hdr(skb)->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
5436 0, IPPROTO_TCP, 0);
5437 cmd_length = E1000_TXD_CMD_IP;
5438 ipcse = skb_transport_offset(skb) - 1;
5439 } else if (skb_is_gso_v6(skb)) {
5440 ipv6_hdr(skb)->payload_len = 0;
5441 tcp_hdr(skb)->check = ~csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
5442 &ipv6_hdr(skb)->daddr,
5443 0, IPPROTO_TCP, 0);
5444 ipcse = 0;
5445 }
5446 ipcss = skb_network_offset(skb);
5447 ipcso = (void *)&(ip_hdr(skb)->check) - (void *)skb->data;
5448 tucss = skb_transport_offset(skb);
5449 tucso = (void *)&(tcp_hdr(skb)->check) - (void *)skb->data;
5450
5451 cmd_length |= (E1000_TXD_CMD_DEXT | E1000_TXD_CMD_TSE |
5452 E1000_TXD_CMD_TCP | (skb->len - (hdr_len)));
5453
5454 i = tx_ring->next_to_use;
5455 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
5456 buffer_info = &tx_ring->buffer_info[i];
5457
5458 context_desc->lower_setup.ip_fields.ipcss = ipcss;
5459 context_desc->lower_setup.ip_fields.ipcso = ipcso;
5460 context_desc->lower_setup.ip_fields.ipcse = cpu_to_le16(ipcse);
5461 context_desc->upper_setup.tcp_fields.tucss = tucss;
5462 context_desc->upper_setup.tcp_fields.tucso = tucso;
5463 context_desc->upper_setup.tcp_fields.tucse = 0;
5464 context_desc->tcp_seg_setup.fields.mss = cpu_to_le16(mss);
5465 context_desc->tcp_seg_setup.fields.hdr_len = hdr_len;
5466 context_desc->cmd_and_length = cpu_to_le32(cmd_length);
5467
5468 buffer_info->time_stamp = jiffies;
5469 buffer_info->next_to_watch = i;
5470
5471 i++;
5472 if (i == tx_ring->count)
5473 i = 0;
5474 tx_ring->next_to_use = i;
5475
5476 return 1;
5477}
5478
5479static bool e1000_tx_csum(struct e1000_ring *tx_ring, struct sk_buff *skb,
5480 __be16 protocol)
5481{
5482 struct e1000_adapter *adapter = tx_ring->adapter;
5483 struct e1000_context_desc *context_desc;
5484 struct e1000_buffer *buffer_info;
5485 unsigned int i;
5486 u8 css;
5487 u32 cmd_len = E1000_TXD_CMD_DEXT;
5488
5489 if (skb->ip_summed != CHECKSUM_PARTIAL)
5490 return false;
5491
5492 switch (protocol) {
5493 case cpu_to_be16(ETH_P_IP):
5494 if (ip_hdr(skb)->protocol == IPPROTO_TCP)
5495 cmd_len |= E1000_TXD_CMD_TCP;
5496 break;
5497 case cpu_to_be16(ETH_P_IPV6):
5498 /* XXX not handling all IPV6 headers */
5499 if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
5500 cmd_len |= E1000_TXD_CMD_TCP;
5501 break;
5502 default:
5503 if (unlikely(net_ratelimit()))
5504 e_warn("checksum_partial proto=%x!\n",
5505 be16_to_cpu(protocol));
5506 break;
5507 }
5508
5509 css = skb_checksum_start_offset(skb);
5510
5511 i = tx_ring->next_to_use;
5512 buffer_info = &tx_ring->buffer_info[i];
5513 context_desc = E1000_CONTEXT_DESC(*tx_ring, i);
5514
5515 context_desc->lower_setup.ip_config = 0;
5516 context_desc->upper_setup.tcp_fields.tucss = css;
5517 context_desc->upper_setup.tcp_fields.tucso = css + skb->csum_offset;
5518 context_desc->upper_setup.tcp_fields.tucse = 0;
5519 context_desc->tcp_seg_setup.data = 0;
5520 context_desc->cmd_and_length = cpu_to_le32(cmd_len);
5521
5522 buffer_info->time_stamp = jiffies;
5523 buffer_info->next_to_watch = i;
5524
5525 i++;
5526 if (i == tx_ring->count)
5527 i = 0;
5528 tx_ring->next_to_use = i;
5529
5530 return true;
5531}
5532
5533static int e1000_tx_map(struct e1000_ring *tx_ring, struct sk_buff *skb,
5534 unsigned int first, unsigned int max_per_txd,
5535 unsigned int nr_frags)
5536{
5537 struct e1000_adapter *adapter = tx_ring->adapter;
5538 struct pci_dev *pdev = adapter->pdev;
5539 struct e1000_buffer *buffer_info;
5540 unsigned int len = skb_headlen(skb);
5541 unsigned int offset = 0, size, count = 0, i;
5542 unsigned int f, bytecount, segs;
5543
5544 i = tx_ring->next_to_use;
5545
5546 while (len) {
5547 buffer_info = &tx_ring->buffer_info[i];
5548 size = min(len, max_per_txd);
5549
5550 buffer_info->length = size;
5551 buffer_info->time_stamp = jiffies;
5552 buffer_info->next_to_watch = i;
5553 buffer_info->dma = dma_map_single(&pdev->dev,
5554 skb->data + offset,
5555 size, DMA_TO_DEVICE);
5556 buffer_info->mapped_as_page = false;
5557 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
5558 goto dma_error;
5559
5560 len -= size;
5561 offset += size;
5562 count++;
5563
5564 if (len) {
5565 i++;
5566 if (i == tx_ring->count)
5567 i = 0;
5568 }
5569 }
5570
5571 for (f = 0; f < nr_frags; f++) {
5572 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
5573
5574 len = skb_frag_size(frag);
5575 offset = 0;
5576
5577 while (len) {
5578 i++;
5579 if (i == tx_ring->count)
5580 i = 0;
5581
5582 buffer_info = &tx_ring->buffer_info[i];
5583 size = min(len, max_per_txd);
5584
5585 buffer_info->length = size;
5586 buffer_info->time_stamp = jiffies;
5587 buffer_info->next_to_watch = i;
5588 buffer_info->dma = skb_frag_dma_map(&pdev->dev, frag,
5589 offset, size,
5590 DMA_TO_DEVICE);
5591 buffer_info->mapped_as_page = true;
5592 if (dma_mapping_error(&pdev->dev, buffer_info->dma))
5593 goto dma_error;
5594
5595 len -= size;
5596 offset += size;
5597 count++;
5598 }
5599 }
5600
5601 segs = skb_shinfo(skb)->gso_segs ? : 1;
5602 /* multiply data chunks by size of headers */
5603 bytecount = ((segs - 1) * skb_headlen(skb)) + skb->len;
5604
5605 tx_ring->buffer_info[i].skb = skb;
5606 tx_ring->buffer_info[i].segs = segs;
5607 tx_ring->buffer_info[i].bytecount = bytecount;
5608 tx_ring->buffer_info[first].next_to_watch = i;
5609
5610 return count;
5611
5612dma_error:
5613 dev_err(&pdev->dev, "Tx DMA map failed\n");
5614 buffer_info->dma = 0;
5615 if (count)
5616 count--;
5617
5618 while (count--) {
5619 if (i == 0)
5620 i += tx_ring->count;
5621 i--;
5622 buffer_info = &tx_ring->buffer_info[i];
5623 e1000_put_txbuf(tx_ring, buffer_info, true);
5624 }
5625
5626 return 0;
5627}
5628
5629static void e1000_tx_queue(struct e1000_ring *tx_ring, int tx_flags, int count)
5630{
5631 struct e1000_adapter *adapter = tx_ring->adapter;
5632 struct e1000_tx_desc *tx_desc = NULL;
5633 struct e1000_buffer *buffer_info;
5634 u32 txd_upper = 0, txd_lower = E1000_TXD_CMD_IFCS;
5635 unsigned int i;
5636
5637 if (tx_flags & E1000_TX_FLAGS_TSO) {
5638 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D |
5639 E1000_TXD_CMD_TSE;
5640 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
5641
5642 if (tx_flags & E1000_TX_FLAGS_IPV4)
5643 txd_upper |= E1000_TXD_POPTS_IXSM << 8;
5644 }
5645
5646 if (tx_flags & E1000_TX_FLAGS_CSUM) {
5647 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
5648 txd_upper |= E1000_TXD_POPTS_TXSM << 8;
5649 }
5650
5651 if (tx_flags & E1000_TX_FLAGS_VLAN) {
5652 txd_lower |= E1000_TXD_CMD_VLE;
5653 txd_upper |= (tx_flags & E1000_TX_FLAGS_VLAN_MASK);
5654 }
5655
5656 if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
5657 txd_lower &= ~(E1000_TXD_CMD_IFCS);
5658
5659 if (unlikely(tx_flags & E1000_TX_FLAGS_HWTSTAMP)) {
5660 txd_lower |= E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D;
5661 txd_upper |= E1000_TXD_EXTCMD_TSTAMP;
5662 }
5663
5664 i = tx_ring->next_to_use;
5665
5666 do {
5667 buffer_info = &tx_ring->buffer_info[i];
5668 tx_desc = E1000_TX_DESC(*tx_ring, i);
5669 tx_desc->buffer_addr = cpu_to_le64(buffer_info->dma);
5670 tx_desc->lower.data = cpu_to_le32(txd_lower |
5671 buffer_info->length);
5672 tx_desc->upper.data = cpu_to_le32(txd_upper);
5673
5674 i++;
5675 if (i == tx_ring->count)
5676 i = 0;
5677 } while (--count > 0);
5678
5679 tx_desc->lower.data |= cpu_to_le32(adapter->txd_cmd);
5680
5681 /* txd_cmd re-enables FCS, so we'll re-disable it here as desired. */
5682 if (unlikely(tx_flags & E1000_TX_FLAGS_NO_FCS))
5683 tx_desc->lower.data &= ~(cpu_to_le32(E1000_TXD_CMD_IFCS));
5684
5685 /* Force memory writes to complete before letting h/w
5686 * know there are new descriptors to fetch. (Only
5687 * applicable for weak-ordered memory model archs,
5688 * such as IA-64).
5689 */
5690 wmb();
5691
5692 tx_ring->next_to_use = i;
5693}
5694
5695#define MINIMUM_DHCP_PACKET_SIZE 282
5696static int e1000_transfer_dhcp_info(struct e1000_adapter *adapter,
5697 struct sk_buff *skb)
5698{
5699 struct e1000_hw *hw = &adapter->hw;
5700 u16 length, offset;
5701
5702 if (skb_vlan_tag_present(skb) &&
5703 !((skb_vlan_tag_get(skb) == adapter->hw.mng_cookie.vlan_id) &&
5704 (adapter->hw.mng_cookie.status &
5705 E1000_MNG_DHCP_COOKIE_STATUS_VLAN)))
5706 return 0;
5707
5708 if (skb->len <= MINIMUM_DHCP_PACKET_SIZE)
5709 return 0;
5710
5711 if (((struct ethhdr *)skb->data)->h_proto != htons(ETH_P_IP))
5712 return 0;
5713
5714 {
5715 const struct iphdr *ip = (struct iphdr *)((u8 *)skb->data + 14);
5716 struct udphdr *udp;
5717
5718 if (ip->protocol != IPPROTO_UDP)
5719 return 0;
5720
5721 udp = (struct udphdr *)((u8 *)ip + (ip->ihl << 2));
5722 if (ntohs(udp->dest) != 67)
5723 return 0;
5724
5725 offset = (u8 *)udp + 8 - skb->data;
5726 length = skb->len - offset;
5727 return e1000e_mng_write_dhcp_info(hw, (u8 *)udp + 8, length);
5728 }
5729
5730 return 0;
5731}
5732
5733static int __e1000_maybe_stop_tx(struct e1000_ring *tx_ring, int size)
5734{
5735 struct e1000_adapter *adapter = tx_ring->adapter;
5736
5737 netif_stop_queue(adapter->netdev);
5738 /* Herbert's original patch had:
5739 * smp_mb__after_netif_stop_queue();
5740 * but since that doesn't exist yet, just open code it.
5741 */
5742 smp_mb();
5743
5744 /* We need to check again in a case another CPU has just
5745 * made room available.
5746 */
5747 if (e1000_desc_unused(tx_ring) < size)
5748 return -EBUSY;
5749
5750 /* A reprieve! */
5751 netif_start_queue(adapter->netdev);
5752 ++adapter->restart_queue;
5753 return 0;
5754}
5755
5756static int e1000_maybe_stop_tx(struct e1000_ring *tx_ring, int size)
5757{
5758 BUG_ON(size > tx_ring->count);
5759
5760 if (e1000_desc_unused(tx_ring) >= size)
5761 return 0;
5762 return __e1000_maybe_stop_tx(tx_ring, size);
5763}
5764
5765static netdev_tx_t e1000_xmit_frame(struct sk_buff *skb,
5766 struct net_device *netdev)
5767{
5768 struct e1000_adapter *adapter = netdev_priv(netdev);
5769 struct e1000_ring *tx_ring = adapter->tx_ring;
5770 unsigned int first;
5771 unsigned int tx_flags = 0;
5772 unsigned int len = skb_headlen(skb);
5773 unsigned int nr_frags;
5774 unsigned int mss;
5775 int count = 0;
5776 int tso;
5777 unsigned int f;
5778 __be16 protocol = vlan_get_protocol(skb);
5779
5780 if (test_bit(__E1000_DOWN, &adapter->state)) {
5781 dev_kfree_skb_any(skb);
5782 return NETDEV_TX_OK;
5783 }
5784
5785 if (skb->len <= 0) {
5786 dev_kfree_skb_any(skb);
5787 return NETDEV_TX_OK;
5788 }
5789
5790 /* The minimum packet size with TCTL.PSP set is 17 bytes so
5791 * pad skb in order to meet this minimum size requirement
5792 */
5793 if (skb_put_padto(skb, 17))
5794 return NETDEV_TX_OK;
5795
5796 mss = skb_shinfo(skb)->gso_size;
5797 if (mss) {
5798 u8 hdr_len;
5799
5800 /* TSO Workaround for 82571/2/3 Controllers -- if skb->data
5801 * points to just header, pull a few bytes of payload from
5802 * frags into skb->data
5803 */
5804 hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
5805 /* we do this workaround for ES2LAN, but it is un-necessary,
5806 * avoiding it could save a lot of cycles
5807 */
5808 if (skb->data_len && (hdr_len == len)) {
5809 unsigned int pull_size;
5810
5811 pull_size = min_t(unsigned int, 4, skb->data_len);
5812 if (!__pskb_pull_tail(skb, pull_size)) {
5813 e_err("__pskb_pull_tail failed.\n");
5814 dev_kfree_skb_any(skb);
5815 return NETDEV_TX_OK;
5816 }
5817 len = skb_headlen(skb);
5818 }
5819 }
5820
5821 /* reserve a descriptor for the offload context */
5822 if ((mss) || (skb->ip_summed == CHECKSUM_PARTIAL))
5823 count++;
5824 count++;
5825
5826 count += DIV_ROUND_UP(len, adapter->tx_fifo_limit);
5827
5828 nr_frags = skb_shinfo(skb)->nr_frags;
5829 for (f = 0; f < nr_frags; f++)
5830 count += DIV_ROUND_UP(skb_frag_size(&skb_shinfo(skb)->frags[f]),
5831 adapter->tx_fifo_limit);
5832
5833 if (adapter->hw.mac.tx_pkt_filtering)
5834 e1000_transfer_dhcp_info(adapter, skb);
5835
5836 /* need: count + 2 desc gap to keep tail from touching
5837 * head, otherwise try next time
5838 */
5839 if (e1000_maybe_stop_tx(tx_ring, count + 2))
5840 return NETDEV_TX_BUSY;
5841
5842 if (skb_vlan_tag_present(skb)) {
5843 tx_flags |= E1000_TX_FLAGS_VLAN;
5844 tx_flags |= (skb_vlan_tag_get(skb) <<
5845 E1000_TX_FLAGS_VLAN_SHIFT);
5846 }
5847
5848 first = tx_ring->next_to_use;
5849
5850 tso = e1000_tso(tx_ring, skb, protocol);
5851 if (tso < 0) {
5852 dev_kfree_skb_any(skb);
5853 return NETDEV_TX_OK;
5854 }
5855
5856 if (tso)
5857 tx_flags |= E1000_TX_FLAGS_TSO;
5858 else if (e1000_tx_csum(tx_ring, skb, protocol))
5859 tx_flags |= E1000_TX_FLAGS_CSUM;
5860
5861 /* Old method was to assume IPv4 packet by default if TSO was enabled.
5862 * 82571 hardware supports TSO capabilities for IPv6 as well...
5863 * no longer assume, we must.
5864 */
5865 if (protocol == htons(ETH_P_IP))
5866 tx_flags |= E1000_TX_FLAGS_IPV4;
5867
5868 if (unlikely(skb->no_fcs))
5869 tx_flags |= E1000_TX_FLAGS_NO_FCS;
5870
5871 /* if count is 0 then mapping error has occurred */
5872 count = e1000_tx_map(tx_ring, skb, first, adapter->tx_fifo_limit,
5873 nr_frags);
5874 if (count) {
5875 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
5876 (adapter->flags & FLAG_HAS_HW_TIMESTAMP)) {
5877 if (!adapter->tx_hwtstamp_skb) {
5878 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
5879 tx_flags |= E1000_TX_FLAGS_HWTSTAMP;
5880 adapter->tx_hwtstamp_skb = skb_get(skb);
5881 adapter->tx_hwtstamp_start = jiffies;
5882 schedule_work(&adapter->tx_hwtstamp_work);
5883 } else {
5884 adapter->tx_hwtstamp_skipped++;
5885 }
5886 }
5887
5888 skb_tx_timestamp(skb);
5889
5890 netdev_sent_queue(netdev, skb->len);
5891 e1000_tx_queue(tx_ring, tx_flags, count);
5892 /* Make sure there is space in the ring for the next send. */
5893 e1000_maybe_stop_tx(tx_ring,
5894 ((MAX_SKB_FRAGS + 1) *
5895 DIV_ROUND_UP(PAGE_SIZE,
5896 adapter->tx_fifo_limit) + 4));
5897
5898 if (!netdev_xmit_more() ||
5899 netif_xmit_stopped(netdev_get_tx_queue(netdev, 0))) {
5900 if (adapter->flags2 & FLAG2_PCIM2PCI_ARBITER_WA)
5901 e1000e_update_tdt_wa(tx_ring,
5902 tx_ring->next_to_use);
5903 else
5904 writel(tx_ring->next_to_use, tx_ring->tail);
5905 }
5906 } else {
5907 dev_kfree_skb_any(skb);
5908 tx_ring->buffer_info[first].time_stamp = 0;
5909 tx_ring->next_to_use = first;
5910 }
5911
5912 return NETDEV_TX_OK;
5913}
5914
5915/**
5916 * e1000_tx_timeout - Respond to a Tx Hang
5917 * @netdev: network interface device structure
5918 **/
5919static void e1000_tx_timeout(struct net_device *netdev)
5920{
5921 struct e1000_adapter *adapter = netdev_priv(netdev);
5922
5923 /* Do the reset outside of interrupt context */
5924 adapter->tx_timeout_count++;
5925 schedule_work(&adapter->reset_task);
5926}
5927
5928static void e1000_reset_task(struct work_struct *work)
5929{
5930 struct e1000_adapter *adapter;
5931 adapter = container_of(work, struct e1000_adapter, reset_task);
5932
5933 rtnl_lock();
5934 /* don't run the task if already down */
5935 if (test_bit(__E1000_DOWN, &adapter->state)) {
5936 rtnl_unlock();
5937 return;
5938 }
5939
5940 if (!(adapter->flags & FLAG_RESTART_NOW)) {
5941 e1000e_dump(adapter);
5942 e_err("Reset adapter unexpectedly\n");
5943 }
5944 e1000e_reinit_locked(adapter);
5945 rtnl_unlock();
5946}
5947
5948/**
5949 * e1000_get_stats64 - Get System Network Statistics
5950 * @netdev: network interface device structure
5951 * @stats: rtnl_link_stats64 pointer
5952 *
5953 * Returns the address of the device statistics structure.
5954 **/
5955void e1000e_get_stats64(struct net_device *netdev,
5956 struct rtnl_link_stats64 *stats)
5957{
5958 struct e1000_adapter *adapter = netdev_priv(netdev);
5959
5960 spin_lock(&adapter->stats64_lock);
5961 e1000e_update_stats(adapter);
5962 /* Fill out the OS statistics structure */
5963 stats->rx_bytes = adapter->stats.gorc;
5964 stats->rx_packets = adapter->stats.gprc;
5965 stats->tx_bytes = adapter->stats.gotc;
5966 stats->tx_packets = adapter->stats.gptc;
5967 stats->multicast = adapter->stats.mprc;
5968 stats->collisions = adapter->stats.colc;
5969
5970 /* Rx Errors */
5971
5972 /* RLEC on some newer hardware can be incorrect so build
5973 * our own version based on RUC and ROC
5974 */
5975 stats->rx_errors = adapter->stats.rxerrc +
5976 adapter->stats.crcerrs + adapter->stats.algnerrc +
5977 adapter->stats.ruc + adapter->stats.roc + adapter->stats.cexterr;
5978 stats->rx_length_errors = adapter->stats.ruc + adapter->stats.roc;
5979 stats->rx_crc_errors = adapter->stats.crcerrs;
5980 stats->rx_frame_errors = adapter->stats.algnerrc;
5981 stats->rx_missed_errors = adapter->stats.mpc;
5982
5983 /* Tx Errors */
5984 stats->tx_errors = adapter->stats.ecol + adapter->stats.latecol;
5985 stats->tx_aborted_errors = adapter->stats.ecol;
5986 stats->tx_window_errors = adapter->stats.latecol;
5987 stats->tx_carrier_errors = adapter->stats.tncrs;
5988
5989 /* Tx Dropped needs to be maintained elsewhere */
5990
5991 spin_unlock(&adapter->stats64_lock);
5992}
5993
5994/**
5995 * e1000_change_mtu - Change the Maximum Transfer Unit
5996 * @netdev: network interface device structure
5997 * @new_mtu: new value for maximum frame size
5998 *
5999 * Returns 0 on success, negative on failure
6000 **/
6001static int e1000_change_mtu(struct net_device *netdev, int new_mtu)
6002{
6003 struct e1000_adapter *adapter = netdev_priv(netdev);
6004 int max_frame = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
6005
6006 /* Jumbo frame support */
6007 if ((new_mtu > ETH_DATA_LEN) &&
6008 !(adapter->flags & FLAG_HAS_JUMBO_FRAMES)) {
6009 e_err("Jumbo Frames not supported.\n");
6010 return -EINVAL;
6011 }
6012
6013 /* Jumbo frame workaround on 82579 and newer requires CRC be stripped */
6014 if ((adapter->hw.mac.type >= e1000_pch2lan) &&
6015 !(adapter->flags2 & FLAG2_CRC_STRIPPING) &&
6016 (new_mtu > ETH_DATA_LEN)) {
6017 e_err("Jumbo Frames not supported on this device when CRC stripping is disabled.\n");
6018 return -EINVAL;
6019 }
6020
6021 while (test_and_set_bit(__E1000_RESETTING, &adapter->state))
6022 usleep_range(1000, 1100);
6023 /* e1000e_down -> e1000e_reset dependent on max_frame_size & mtu */
6024 adapter->max_frame_size = max_frame;
6025 e_info("changing MTU from %d to %d\n", netdev->mtu, new_mtu);
6026 netdev->mtu = new_mtu;
6027
6028 pm_runtime_get_sync(netdev->dev.parent);
6029
6030 if (netif_running(netdev))
6031 e1000e_down(adapter, true);
6032
6033 /* NOTE: netdev_alloc_skb reserves 16 bytes, and typically NET_IP_ALIGN
6034 * means we reserve 2 more, this pushes us to allocate from the next
6035 * larger slab size.
6036 * i.e. RXBUFFER_2048 --> size-4096 slab
6037 * However with the new *_jumbo_rx* routines, jumbo receives will use
6038 * fragmented skbs
6039 */
6040
6041 if (max_frame <= 2048)
6042 adapter->rx_buffer_len = 2048;
6043 else
6044 adapter->rx_buffer_len = 4096;
6045
6046 /* adjust allocation if LPE protects us, and we aren't using SBP */
6047 if (max_frame <= (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN))
6048 adapter->rx_buffer_len = VLAN_ETH_FRAME_LEN + ETH_FCS_LEN;
6049
6050 if (netif_running(netdev))
6051 e1000e_up(adapter);
6052 else
6053 e1000e_reset(adapter);
6054
6055 pm_runtime_put_sync(netdev->dev.parent);
6056
6057 clear_bit(__E1000_RESETTING, &adapter->state);
6058
6059 return 0;
6060}
6061
6062static int e1000_mii_ioctl(struct net_device *netdev, struct ifreq *ifr,
6063 int cmd)
6064{
6065 struct e1000_adapter *adapter = netdev_priv(netdev);
6066 struct mii_ioctl_data *data = if_mii(ifr);
6067
6068 if (adapter->hw.phy.media_type != e1000_media_type_copper)
6069 return -EOPNOTSUPP;
6070
6071 switch (cmd) {
6072 case SIOCGMIIPHY:
6073 data->phy_id = adapter->hw.phy.addr;
6074 break;
6075 case SIOCGMIIREG:
6076 e1000_phy_read_status(adapter);
6077
6078 switch (data->reg_num & 0x1F) {
6079 case MII_BMCR:
6080 data->val_out = adapter->phy_regs.bmcr;
6081 break;
6082 case MII_BMSR:
6083 data->val_out = adapter->phy_regs.bmsr;
6084 break;
6085 case MII_PHYSID1:
6086 data->val_out = (adapter->hw.phy.id >> 16);
6087 break;
6088 case MII_PHYSID2:
6089 data->val_out = (adapter->hw.phy.id & 0xFFFF);
6090 break;
6091 case MII_ADVERTISE:
6092 data->val_out = adapter->phy_regs.advertise;
6093 break;
6094 case MII_LPA:
6095 data->val_out = adapter->phy_regs.lpa;
6096 break;
6097 case MII_EXPANSION:
6098 data->val_out = adapter->phy_regs.expansion;
6099 break;
6100 case MII_CTRL1000:
6101 data->val_out = adapter->phy_regs.ctrl1000;
6102 break;
6103 case MII_STAT1000:
6104 data->val_out = adapter->phy_regs.stat1000;
6105 break;
6106 case MII_ESTATUS:
6107 data->val_out = adapter->phy_regs.estatus;
6108 break;
6109 default:
6110 return -EIO;
6111 }
6112 break;
6113 case SIOCSMIIREG:
6114 default:
6115 return -EOPNOTSUPP;
6116 }
6117 return 0;
6118}
6119
6120/**
6121 * e1000e_hwtstamp_ioctl - control hardware time stamping
6122 * @netdev: network interface device structure
6123 * @ifreq: interface request
6124 *
6125 * Outgoing time stamping can be enabled and disabled. Play nice and
6126 * disable it when requested, although it shouldn't cause any overhead
6127 * when no packet needs it. At most one packet in the queue may be
6128 * marked for time stamping, otherwise it would be impossible to tell
6129 * for sure to which packet the hardware time stamp belongs.
6130 *
6131 * Incoming time stamping has to be configured via the hardware filters.
6132 * Not all combinations are supported, in particular event type has to be
6133 * specified. Matching the kind of event packet is not supported, with the
6134 * exception of "all V2 events regardless of level 2 or 4".
6135 **/
6136static int e1000e_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
6137{
6138 struct e1000_adapter *adapter = netdev_priv(netdev);
6139 struct hwtstamp_config config;
6140 int ret_val;
6141
6142 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
6143 return -EFAULT;
6144
6145 ret_val = e1000e_config_hwtstamp(adapter, &config);
6146 if (ret_val)
6147 return ret_val;
6148
6149 switch (config.rx_filter) {
6150 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
6151 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
6152 case HWTSTAMP_FILTER_PTP_V2_SYNC:
6153 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
6154 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
6155 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
6156 /* With V2 type filters which specify a Sync or Delay Request,
6157 * Path Delay Request/Response messages are also time stamped
6158 * by hardware so notify the caller the requested packets plus
6159 * some others are time stamped.
6160 */
6161 config.rx_filter = HWTSTAMP_FILTER_SOME;
6162 break;
6163 default:
6164 break;
6165 }
6166
6167 return copy_to_user(ifr->ifr_data, &config,
6168 sizeof(config)) ? -EFAULT : 0;
6169}
6170
6171static int e1000e_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
6172{
6173 struct e1000_adapter *adapter = netdev_priv(netdev);
6174
6175 return copy_to_user(ifr->ifr_data, &adapter->hwtstamp_config,
6176 sizeof(adapter->hwtstamp_config)) ? -EFAULT : 0;
6177}
6178
6179static int e1000_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
6180{
6181 switch (cmd) {
6182 case SIOCGMIIPHY:
6183 case SIOCGMIIREG:
6184 case SIOCSMIIREG:
6185 return e1000_mii_ioctl(netdev, ifr, cmd);
6186 case SIOCSHWTSTAMP:
6187 return e1000e_hwtstamp_set(netdev, ifr);
6188 case SIOCGHWTSTAMP:
6189 return e1000e_hwtstamp_get(netdev, ifr);
6190 default:
6191 return -EOPNOTSUPP;
6192 }
6193}
6194
6195static int e1000_init_phy_wakeup(struct e1000_adapter *adapter, u32 wufc)
6196{
6197 struct e1000_hw *hw = &adapter->hw;
6198 u32 i, mac_reg, wuc;
6199 u16 phy_reg, wuc_enable;
6200 int retval;
6201
6202 /* copy MAC RARs to PHY RARs */
6203 e1000_copy_rx_addrs_to_phy_ich8lan(hw);
6204
6205 retval = hw->phy.ops.acquire(hw);
6206 if (retval) {
6207 e_err("Could not acquire PHY\n");
6208 return retval;
6209 }
6210
6211 /* Enable access to wakeup registers on and set page to BM_WUC_PAGE */
6212 retval = e1000_enable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
6213 if (retval)
6214 goto release;
6215
6216 /* copy MAC MTA to PHY MTA - only needed for pchlan */
6217 for (i = 0; i < adapter->hw.mac.mta_reg_count; i++) {
6218 mac_reg = E1000_READ_REG_ARRAY(hw, E1000_MTA, i);
6219 hw->phy.ops.write_reg_page(hw, BM_MTA(i),
6220 (u16)(mac_reg & 0xFFFF));
6221 hw->phy.ops.write_reg_page(hw, BM_MTA(i) + 1,
6222 (u16)((mac_reg >> 16) & 0xFFFF));
6223 }
6224
6225 /* configure PHY Rx Control register */
6226 hw->phy.ops.read_reg_page(&adapter->hw, BM_RCTL, &phy_reg);
6227 mac_reg = er32(RCTL);
6228 if (mac_reg & E1000_RCTL_UPE)
6229 phy_reg |= BM_RCTL_UPE;
6230 if (mac_reg & E1000_RCTL_MPE)
6231 phy_reg |= BM_RCTL_MPE;
6232 phy_reg &= ~(BM_RCTL_MO_MASK);
6233 if (mac_reg & E1000_RCTL_MO_3)
6234 phy_reg |= (((mac_reg & E1000_RCTL_MO_3) >> E1000_RCTL_MO_SHIFT)
6235 << BM_RCTL_MO_SHIFT);
6236 if (mac_reg & E1000_RCTL_BAM)
6237 phy_reg |= BM_RCTL_BAM;
6238 if (mac_reg & E1000_RCTL_PMCF)
6239 phy_reg |= BM_RCTL_PMCF;
6240 mac_reg = er32(CTRL);
6241 if (mac_reg & E1000_CTRL_RFCE)
6242 phy_reg |= BM_RCTL_RFCE;
6243 hw->phy.ops.write_reg_page(&adapter->hw, BM_RCTL, phy_reg);
6244
6245 wuc = E1000_WUC_PME_EN;
6246 if (wufc & (E1000_WUFC_MAG | E1000_WUFC_LNKC))
6247 wuc |= E1000_WUC_APME;
6248
6249 /* enable PHY wakeup in MAC register */
6250 ew32(WUFC, wufc);
6251 ew32(WUC, (E1000_WUC_PHY_WAKE | E1000_WUC_APMPME |
6252 E1000_WUC_PME_STATUS | wuc));
6253
6254 /* configure and enable PHY wakeup in PHY registers */
6255 hw->phy.ops.write_reg_page(&adapter->hw, BM_WUFC, wufc);
6256 hw->phy.ops.write_reg_page(&adapter->hw, BM_WUC, wuc);
6257
6258 /* activate PHY wakeup */
6259 wuc_enable |= BM_WUC_ENABLE_BIT | BM_WUC_HOST_WU_BIT;
6260 retval = e1000_disable_phy_wakeup_reg_access_bm(hw, &wuc_enable);
6261 if (retval)
6262 e_err("Could not set PHY Host Wakeup bit\n");
6263release:
6264 hw->phy.ops.release(hw);
6265
6266 return retval;
6267}
6268
6269static void e1000e_flush_lpic(struct pci_dev *pdev)
6270{
6271 struct net_device *netdev = pci_get_drvdata(pdev);
6272 struct e1000_adapter *adapter = netdev_priv(netdev);
6273 struct e1000_hw *hw = &adapter->hw;
6274 u32 ret_val;
6275
6276 pm_runtime_get_sync(netdev->dev.parent);
6277
6278 ret_val = hw->phy.ops.acquire(hw);
6279 if (ret_val)
6280 goto fl_out;
6281
6282 pr_info("EEE TX LPI TIMER: %08X\n",
6283 er32(LPIC) >> E1000_LPIC_LPIET_SHIFT);
6284
6285 hw->phy.ops.release(hw);
6286
6287fl_out:
6288 pm_runtime_put_sync(netdev->dev.parent);
6289}
6290
6291static int e1000e_pm_freeze(struct device *dev)
6292{
6293 struct net_device *netdev = dev_get_drvdata(dev);
6294 struct e1000_adapter *adapter = netdev_priv(netdev);
6295 bool present;
6296
6297 rtnl_lock();
6298
6299 present = netif_device_present(netdev);
6300 netif_device_detach(netdev);
6301
6302 if (present && netif_running(netdev)) {
6303 int count = E1000_CHECK_RESET_COUNT;
6304
6305 while (test_bit(__E1000_RESETTING, &adapter->state) && count--)
6306 usleep_range(10000, 11000);
6307
6308 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
6309
6310 /* Quiesce the device without resetting the hardware */
6311 e1000e_down(adapter, false);
6312 e1000_free_irq(adapter);
6313 }
6314 rtnl_unlock();
6315
6316 e1000e_reset_interrupt_capability(adapter);
6317
6318 /* Allow time for pending master requests to run */
6319 e1000e_disable_pcie_master(&adapter->hw);
6320
6321 return 0;
6322}
6323
6324static int __e1000_shutdown(struct pci_dev *pdev, bool runtime)
6325{
6326 struct net_device *netdev = pci_get_drvdata(pdev);
6327 struct e1000_adapter *adapter = netdev_priv(netdev);
6328 struct e1000_hw *hw = &adapter->hw;
6329 u32 ctrl, ctrl_ext, rctl, status, wufc;
6330 int retval = 0;
6331
6332 /* Runtime suspend should only enable wakeup for link changes */
6333 if (runtime)
6334 wufc = E1000_WUFC_LNKC;
6335 else if (device_may_wakeup(&pdev->dev))
6336 wufc = adapter->wol;
6337 else
6338 wufc = 0;
6339
6340 status = er32(STATUS);
6341 if (status & E1000_STATUS_LU)
6342 wufc &= ~E1000_WUFC_LNKC;
6343
6344 if (wufc) {
6345 e1000_setup_rctl(adapter);
6346 e1000e_set_rx_mode(netdev);
6347
6348 /* turn on all-multi mode if wake on multicast is enabled */
6349 if (wufc & E1000_WUFC_MC) {
6350 rctl = er32(RCTL);
6351 rctl |= E1000_RCTL_MPE;
6352 ew32(RCTL, rctl);
6353 }
6354
6355 ctrl = er32(CTRL);
6356 ctrl |= E1000_CTRL_ADVD3WUC;
6357 if (!(adapter->flags2 & FLAG2_HAS_PHY_WAKEUP))
6358 ctrl |= E1000_CTRL_EN_PHY_PWR_MGMT;
6359 ew32(CTRL, ctrl);
6360
6361 if (adapter->hw.phy.media_type == e1000_media_type_fiber ||
6362 adapter->hw.phy.media_type ==
6363 e1000_media_type_internal_serdes) {
6364 /* keep the laser running in D3 */
6365 ctrl_ext = er32(CTRL_EXT);
6366 ctrl_ext |= E1000_CTRL_EXT_SDP3_DATA;
6367 ew32(CTRL_EXT, ctrl_ext);
6368 }
6369
6370 if (!runtime)
6371 e1000e_power_up_phy(adapter);
6372
6373 if (adapter->flags & FLAG_IS_ICH)
6374 e1000_suspend_workarounds_ich8lan(&adapter->hw);
6375
6376 if (adapter->flags2 & FLAG2_HAS_PHY_WAKEUP) {
6377 /* enable wakeup by the PHY */
6378 retval = e1000_init_phy_wakeup(adapter, wufc);
6379 if (retval)
6380 return retval;
6381 } else {
6382 /* enable wakeup by the MAC */
6383 ew32(WUFC, wufc);
6384 ew32(WUC, E1000_WUC_PME_EN);
6385 }
6386 } else {
6387 ew32(WUC, 0);
6388 ew32(WUFC, 0);
6389
6390 e1000_power_down_phy(adapter);
6391 }
6392
6393 if (adapter->hw.phy.type == e1000_phy_igp_3) {
6394 e1000e_igp3_phy_powerdown_workaround_ich8lan(&adapter->hw);
6395 } else if (hw->mac.type >= e1000_pch_lpt) {
6396 if (wufc && !(wufc & (E1000_WUFC_EX | E1000_WUFC_MC | E1000_WUFC_BC)))
6397 /* ULP does not support wake from unicast, multicast
6398 * or broadcast.
6399 */
6400 retval = e1000_enable_ulp_lpt_lp(hw, !runtime);
6401
6402 if (retval)
6403 return retval;
6404 }
6405
6406 /* Ensure that the appropriate bits are set in LPI_CTRL
6407 * for EEE in Sx
6408 */
6409 if ((hw->phy.type >= e1000_phy_i217) &&
6410 adapter->eee_advert && hw->dev_spec.ich8lan.eee_lp_ability) {
6411 u16 lpi_ctrl = 0;
6412
6413 retval = hw->phy.ops.acquire(hw);
6414 if (!retval) {
6415 retval = e1e_rphy_locked(hw, I82579_LPI_CTRL,
6416 &lpi_ctrl);
6417 if (!retval) {
6418 if (adapter->eee_advert &
6419 hw->dev_spec.ich8lan.eee_lp_ability &
6420 I82579_EEE_100_SUPPORTED)
6421 lpi_ctrl |= I82579_LPI_CTRL_100_ENABLE;
6422 if (adapter->eee_advert &
6423 hw->dev_spec.ich8lan.eee_lp_ability &
6424 I82579_EEE_1000_SUPPORTED)
6425 lpi_ctrl |= I82579_LPI_CTRL_1000_ENABLE;
6426
6427 retval = e1e_wphy_locked(hw, I82579_LPI_CTRL,
6428 lpi_ctrl);
6429 }
6430 }
6431 hw->phy.ops.release(hw);
6432 }
6433
6434 /* Release control of h/w to f/w. If f/w is AMT enabled, this
6435 * would have already happened in close and is redundant.
6436 */
6437 e1000e_release_hw_control(adapter);
6438
6439 pci_clear_master(pdev);
6440
6441 /* The pci-e switch on some quad port adapters will report a
6442 * correctable error when the MAC transitions from D0 to D3. To
6443 * prevent this we need to mask off the correctable errors on the
6444 * downstream port of the pci-e switch.
6445 *
6446 * We don't have the associated upstream bridge while assigning
6447 * the PCI device into guest. For example, the KVM on power is
6448 * one of the cases.
6449 */
6450 if (adapter->flags & FLAG_IS_QUAD_PORT) {
6451 struct pci_dev *us_dev = pdev->bus->self;
6452 u16 devctl;
6453
6454 if (!us_dev)
6455 return 0;
6456
6457 pcie_capability_read_word(us_dev, PCI_EXP_DEVCTL, &devctl);
6458 pcie_capability_write_word(us_dev, PCI_EXP_DEVCTL,
6459 (devctl & ~PCI_EXP_DEVCTL_CERE));
6460
6461 pci_save_state(pdev);
6462 pci_prepare_to_sleep(pdev);
6463
6464 pcie_capability_write_word(us_dev, PCI_EXP_DEVCTL, devctl);
6465 }
6466
6467 return 0;
6468}
6469
6470/**
6471 * __e1000e_disable_aspm - Disable ASPM states
6472 * @pdev: pointer to PCI device struct
6473 * @state: bit-mask of ASPM states to disable
6474 * @locked: indication if this context holds pci_bus_sem locked.
6475 *
6476 * Some devices *must* have certain ASPM states disabled per hardware errata.
6477 **/
6478static void __e1000e_disable_aspm(struct pci_dev *pdev, u16 state, int locked)
6479{
6480 struct pci_dev *parent = pdev->bus->self;
6481 u16 aspm_dis_mask = 0;
6482 u16 pdev_aspmc, parent_aspmc;
6483
6484 switch (state) {
6485 case PCIE_LINK_STATE_L0S:
6486 case PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1:
6487 aspm_dis_mask |= PCI_EXP_LNKCTL_ASPM_L0S;
6488 /* fall-through - can't have L1 without L0s */
6489 case PCIE_LINK_STATE_L1:
6490 aspm_dis_mask |= PCI_EXP_LNKCTL_ASPM_L1;
6491 break;
6492 default:
6493 return;
6494 }
6495
6496 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &pdev_aspmc);
6497 pdev_aspmc &= PCI_EXP_LNKCTL_ASPMC;
6498
6499 if (parent) {
6500 pcie_capability_read_word(parent, PCI_EXP_LNKCTL,
6501 &parent_aspmc);
6502 parent_aspmc &= PCI_EXP_LNKCTL_ASPMC;
6503 }
6504
6505 /* Nothing to do if the ASPM states to be disabled already are */
6506 if (!(pdev_aspmc & aspm_dis_mask) &&
6507 (!parent || !(parent_aspmc & aspm_dis_mask)))
6508 return;
6509
6510 dev_info(&pdev->dev, "Disabling ASPM %s %s\n",
6511 (aspm_dis_mask & pdev_aspmc & PCI_EXP_LNKCTL_ASPM_L0S) ?
6512 "L0s" : "",
6513 (aspm_dis_mask & pdev_aspmc & PCI_EXP_LNKCTL_ASPM_L1) ?
6514 "L1" : "");
6515
6516#ifdef CONFIG_PCIEASPM
6517 if (locked)
6518 pci_disable_link_state_locked(pdev, state);
6519 else
6520 pci_disable_link_state(pdev, state);
6521
6522 /* Double-check ASPM control. If not disabled by the above, the
6523 * BIOS is preventing that from happening (or CONFIG_PCIEASPM is
6524 * not enabled); override by writing PCI config space directly.
6525 */
6526 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &pdev_aspmc);
6527 pdev_aspmc &= PCI_EXP_LNKCTL_ASPMC;
6528
6529 if (!(aspm_dis_mask & pdev_aspmc))
6530 return;
6531#endif
6532
6533 /* Both device and parent should have the same ASPM setting.
6534 * Disable ASPM in downstream component first and then upstream.
6535 */
6536 pcie_capability_clear_word(pdev, PCI_EXP_LNKCTL, aspm_dis_mask);
6537
6538 if (parent)
6539 pcie_capability_clear_word(parent, PCI_EXP_LNKCTL,
6540 aspm_dis_mask);
6541}
6542
6543/**
6544 * e1000e_disable_aspm - Disable ASPM states.
6545 * @pdev: pointer to PCI device struct
6546 * @state: bit-mask of ASPM states to disable
6547 *
6548 * This function acquires the pci_bus_sem!
6549 * Some devices *must* have certain ASPM states disabled per hardware errata.
6550 **/
6551static void e1000e_disable_aspm(struct pci_dev *pdev, u16 state)
6552{
6553 __e1000e_disable_aspm(pdev, state, 0);
6554}
6555
6556/**
6557 * e1000e_disable_aspm_locked Disable ASPM states.
6558 * @pdev: pointer to PCI device struct
6559 * @state: bit-mask of ASPM states to disable
6560 *
6561 * This function must be called with pci_bus_sem acquired!
6562 * Some devices *must* have certain ASPM states disabled per hardware errata.
6563 **/
6564static void e1000e_disable_aspm_locked(struct pci_dev *pdev, u16 state)
6565{
6566 __e1000e_disable_aspm(pdev, state, 1);
6567}
6568
6569static int e1000e_pm_thaw(struct device *dev)
6570{
6571 struct net_device *netdev = dev_get_drvdata(dev);
6572 struct e1000_adapter *adapter = netdev_priv(netdev);
6573 int rc = 0;
6574
6575 e1000e_set_interrupt_capability(adapter);
6576
6577 rtnl_lock();
6578 if (netif_running(netdev)) {
6579 rc = e1000_request_irq(adapter);
6580 if (rc)
6581 goto err_irq;
6582
6583 e1000e_up(adapter);
6584 }
6585
6586 netif_device_attach(netdev);
6587err_irq:
6588 rtnl_unlock();
6589
6590 return rc;
6591}
6592
6593#ifdef CONFIG_PM
6594static int __e1000_resume(struct pci_dev *pdev)
6595{
6596 struct net_device *netdev = pci_get_drvdata(pdev);
6597 struct e1000_adapter *adapter = netdev_priv(netdev);
6598 struct e1000_hw *hw = &adapter->hw;
6599 u16 aspm_disable_flag = 0;
6600
6601 if (adapter->flags2 & FLAG2_DISABLE_ASPM_L0S)
6602 aspm_disable_flag = PCIE_LINK_STATE_L0S;
6603 if (adapter->flags2 & FLAG2_DISABLE_ASPM_L1)
6604 aspm_disable_flag |= PCIE_LINK_STATE_L1;
6605 if (aspm_disable_flag)
6606 e1000e_disable_aspm(pdev, aspm_disable_flag);
6607
6608 pci_set_master(pdev);
6609
6610 if (hw->mac.type >= e1000_pch2lan)
6611 e1000_resume_workarounds_pchlan(&adapter->hw);
6612
6613 e1000e_power_up_phy(adapter);
6614
6615 /* report the system wakeup cause from S3/S4 */
6616 if (adapter->flags2 & FLAG2_HAS_PHY_WAKEUP) {
6617 u16 phy_data;
6618
6619 e1e_rphy(&adapter->hw, BM_WUS, &phy_data);
6620 if (phy_data) {
6621 e_info("PHY Wakeup cause - %s\n",
6622 phy_data & E1000_WUS_EX ? "Unicast Packet" :
6623 phy_data & E1000_WUS_MC ? "Multicast Packet" :
6624 phy_data & E1000_WUS_BC ? "Broadcast Packet" :
6625 phy_data & E1000_WUS_MAG ? "Magic Packet" :
6626 phy_data & E1000_WUS_LNKC ?
6627 "Link Status Change" : "other");
6628 }
6629 e1e_wphy(&adapter->hw, BM_WUS, ~0);
6630 } else {
6631 u32 wus = er32(WUS);
6632
6633 if (wus) {
6634 e_info("MAC Wakeup cause - %s\n",
6635 wus & E1000_WUS_EX ? "Unicast Packet" :
6636 wus & E1000_WUS_MC ? "Multicast Packet" :
6637 wus & E1000_WUS_BC ? "Broadcast Packet" :
6638 wus & E1000_WUS_MAG ? "Magic Packet" :
6639 wus & E1000_WUS_LNKC ? "Link Status Change" :
6640 "other");
6641 }
6642 ew32(WUS, ~0);
6643 }
6644
6645 e1000e_reset(adapter);
6646
6647 e1000_init_manageability_pt(adapter);
6648
6649 /* If the controller has AMT, do not set DRV_LOAD until the interface
6650 * is up. For all other cases, let the f/w know that the h/w is now
6651 * under the control of the driver.
6652 */
6653 if (!(adapter->flags & FLAG_HAS_AMT))
6654 e1000e_get_hw_control(adapter);
6655
6656 return 0;
6657}
6658
6659#ifdef CONFIG_PM_SLEEP
6660static int e1000e_pm_suspend(struct device *dev)
6661{
6662 struct pci_dev *pdev = to_pci_dev(dev);
6663 int rc;
6664
6665 e1000e_flush_lpic(pdev);
6666
6667 e1000e_pm_freeze(dev);
6668
6669 rc = __e1000_shutdown(pdev, false);
6670 if (rc)
6671 e1000e_pm_thaw(dev);
6672
6673 return rc;
6674}
6675
6676static int e1000e_pm_resume(struct device *dev)
6677{
6678 struct pci_dev *pdev = to_pci_dev(dev);
6679 int rc;
6680
6681 rc = __e1000_resume(pdev);
6682 if (rc)
6683 return rc;
6684
6685 return e1000e_pm_thaw(dev);
6686}
6687#endif /* CONFIG_PM_SLEEP */
6688
6689static int e1000e_pm_runtime_idle(struct device *dev)
6690{
6691 struct net_device *netdev = dev_get_drvdata(dev);
6692 struct e1000_adapter *adapter = netdev_priv(netdev);
6693 u16 eee_lp;
6694
6695 eee_lp = adapter->hw.dev_spec.ich8lan.eee_lp_ability;
6696
6697 if (!e1000e_has_link(adapter)) {
6698 adapter->hw.dev_spec.ich8lan.eee_lp_ability = eee_lp;
6699 pm_schedule_suspend(dev, 5 * MSEC_PER_SEC);
6700 }
6701
6702 return -EBUSY;
6703}
6704
6705static int e1000e_pm_runtime_resume(struct device *dev)
6706{
6707 struct pci_dev *pdev = to_pci_dev(dev);
6708 struct net_device *netdev = pci_get_drvdata(pdev);
6709 struct e1000_adapter *adapter = netdev_priv(netdev);
6710 int rc;
6711
6712 rc = __e1000_resume(pdev);
6713 if (rc)
6714 return rc;
6715
6716 if (netdev->flags & IFF_UP)
6717 e1000e_up(adapter);
6718
6719 return rc;
6720}
6721
6722static int e1000e_pm_runtime_suspend(struct device *dev)
6723{
6724 struct pci_dev *pdev = to_pci_dev(dev);
6725 struct net_device *netdev = pci_get_drvdata(pdev);
6726 struct e1000_adapter *adapter = netdev_priv(netdev);
6727
6728 if (netdev->flags & IFF_UP) {
6729 int count = E1000_CHECK_RESET_COUNT;
6730
6731 while (test_bit(__E1000_RESETTING, &adapter->state) && count--)
6732 usleep_range(10000, 11000);
6733
6734 WARN_ON(test_bit(__E1000_RESETTING, &adapter->state));
6735
6736 /* Down the device without resetting the hardware */
6737 e1000e_down(adapter, false);
6738 }
6739
6740 if (__e1000_shutdown(pdev, true)) {
6741 e1000e_pm_runtime_resume(dev);
6742 return -EBUSY;
6743 }
6744
6745 return 0;
6746}
6747#endif /* CONFIG_PM */
6748
6749static void e1000_shutdown(struct pci_dev *pdev)
6750{
6751 e1000e_flush_lpic(pdev);
6752
6753 e1000e_pm_freeze(&pdev->dev);
6754
6755 __e1000_shutdown(pdev, false);
6756}
6757
6758#ifdef CONFIG_NET_POLL_CONTROLLER
6759
6760static irqreturn_t e1000_intr_msix(int __always_unused irq, void *data)
6761{
6762 struct net_device *netdev = data;
6763 struct e1000_adapter *adapter = netdev_priv(netdev);
6764
6765 if (adapter->msix_entries) {
6766 int vector, msix_irq;
6767
6768 vector = 0;
6769 msix_irq = adapter->msix_entries[vector].vector;
6770 if (disable_hardirq(msix_irq))
6771 e1000_intr_msix_rx(msix_irq, netdev);
6772 enable_irq(msix_irq);
6773
6774 vector++;
6775 msix_irq = adapter->msix_entries[vector].vector;
6776 if (disable_hardirq(msix_irq))
6777 e1000_intr_msix_tx(msix_irq, netdev);
6778 enable_irq(msix_irq);
6779
6780 vector++;
6781 msix_irq = adapter->msix_entries[vector].vector;
6782 if (disable_hardirq(msix_irq))
6783 e1000_msix_other(msix_irq, netdev);
6784 enable_irq(msix_irq);
6785 }
6786
6787 return IRQ_HANDLED;
6788}
6789
6790/**
6791 * e1000_netpoll
6792 * @netdev: network interface device structure
6793 *
6794 * Polling 'interrupt' - used by things like netconsole to send skbs
6795 * without having to re-enable interrupts. It's not called while
6796 * the interrupt routine is executing.
6797 */
6798static void e1000_netpoll(struct net_device *netdev)
6799{
6800 struct e1000_adapter *adapter = netdev_priv(netdev);
6801
6802 switch (adapter->int_mode) {
6803 case E1000E_INT_MODE_MSIX:
6804 e1000_intr_msix(adapter->pdev->irq, netdev);
6805 break;
6806 case E1000E_INT_MODE_MSI:
6807 if (disable_hardirq(adapter->pdev->irq))
6808 e1000_intr_msi(adapter->pdev->irq, netdev);
6809 enable_irq(adapter->pdev->irq);
6810 break;
6811 default: /* E1000E_INT_MODE_LEGACY */
6812 if (disable_hardirq(adapter->pdev->irq))
6813 e1000_intr(adapter->pdev->irq, netdev);
6814 enable_irq(adapter->pdev->irq);
6815 break;
6816 }
6817}
6818#endif
6819
6820/**
6821 * e1000_io_error_detected - called when PCI error is detected
6822 * @pdev: Pointer to PCI device
6823 * @state: The current pci connection state
6824 *
6825 * This function is called after a PCI bus error affecting
6826 * this device has been detected.
6827 */
6828static pci_ers_result_t e1000_io_error_detected(struct pci_dev *pdev,
6829 pci_channel_state_t state)
6830{
6831 e1000e_pm_freeze(&pdev->dev);
6832
6833 if (state == pci_channel_io_perm_failure)
6834 return PCI_ERS_RESULT_DISCONNECT;
6835
6836 pci_disable_device(pdev);
6837
6838 /* Request a slot slot reset. */
6839 return PCI_ERS_RESULT_NEED_RESET;
6840}
6841
6842/**
6843 * e1000_io_slot_reset - called after the pci bus has been reset.
6844 * @pdev: Pointer to PCI device
6845 *
6846 * Restart the card from scratch, as if from a cold-boot. Implementation
6847 * resembles the first-half of the e1000e_pm_resume routine.
6848 */
6849static pci_ers_result_t e1000_io_slot_reset(struct pci_dev *pdev)
6850{
6851 struct net_device *netdev = pci_get_drvdata(pdev);
6852 struct e1000_adapter *adapter = netdev_priv(netdev);
6853 struct e1000_hw *hw = &adapter->hw;
6854 u16 aspm_disable_flag = 0;
6855 int err;
6856 pci_ers_result_t result;
6857
6858 if (adapter->flags2 & FLAG2_DISABLE_ASPM_L0S)
6859 aspm_disable_flag = PCIE_LINK_STATE_L0S;
6860 if (adapter->flags2 & FLAG2_DISABLE_ASPM_L1)
6861 aspm_disable_flag |= PCIE_LINK_STATE_L1;
6862 if (aspm_disable_flag)
6863 e1000e_disable_aspm_locked(pdev, aspm_disable_flag);
6864
6865 err = pci_enable_device_mem(pdev);
6866 if (err) {
6867 dev_err(&pdev->dev,
6868 "Cannot re-enable PCI device after reset.\n");
6869 result = PCI_ERS_RESULT_DISCONNECT;
6870 } else {
6871 pdev->state_saved = true;
6872 pci_restore_state(pdev);
6873 pci_set_master(pdev);
6874
6875 pci_enable_wake(pdev, PCI_D3hot, 0);
6876 pci_enable_wake(pdev, PCI_D3cold, 0);
6877
6878 e1000e_reset(adapter);
6879 ew32(WUS, ~0);
6880 result = PCI_ERS_RESULT_RECOVERED;
6881 }
6882
6883 return result;
6884}
6885
6886/**
6887 * e1000_io_resume - called when traffic can start flowing again.
6888 * @pdev: Pointer to PCI device
6889 *
6890 * This callback is called when the error recovery driver tells us that
6891 * its OK to resume normal operation. Implementation resembles the
6892 * second-half of the e1000e_pm_resume routine.
6893 */
6894static void e1000_io_resume(struct pci_dev *pdev)
6895{
6896 struct net_device *netdev = pci_get_drvdata(pdev);
6897 struct e1000_adapter *adapter = netdev_priv(netdev);
6898
6899 e1000_init_manageability_pt(adapter);
6900
6901 e1000e_pm_thaw(&pdev->dev);
6902
6903 /* If the controller has AMT, do not set DRV_LOAD until the interface
6904 * is up. For all other cases, let the f/w know that the h/w is now
6905 * under the control of the driver.
6906 */
6907 if (!(adapter->flags & FLAG_HAS_AMT))
6908 e1000e_get_hw_control(adapter);
6909}
6910
6911static void e1000_print_device_info(struct e1000_adapter *adapter)
6912{
6913 struct e1000_hw *hw = &adapter->hw;
6914 struct net_device *netdev = adapter->netdev;
6915 u32 ret_val;
6916 u8 pba_str[E1000_PBANUM_LENGTH];
6917
6918 /* print bus type/speed/width info */
6919 e_info("(PCI Express:2.5GT/s:%s) %pM\n",
6920 /* bus width */
6921 ((hw->bus.width == e1000_bus_width_pcie_x4) ? "Width x4" :
6922 "Width x1"),
6923 /* MAC address */
6924 netdev->dev_addr);
6925 e_info("Intel(R) PRO/%s Network Connection\n",
6926 (hw->phy.type == e1000_phy_ife) ? "10/100" : "1000");
6927 ret_val = e1000_read_pba_string_generic(hw, pba_str,
6928 E1000_PBANUM_LENGTH);
6929 if (ret_val)
6930 strlcpy((char *)pba_str, "Unknown", sizeof(pba_str));
6931 e_info("MAC: %d, PHY: %d, PBA No: %s\n",
6932 hw->mac.type, hw->phy.type, pba_str);
6933}
6934
6935static void e1000_eeprom_checks(struct e1000_adapter *adapter)
6936{
6937 struct e1000_hw *hw = &adapter->hw;
6938 int ret_val;
6939 u16 buf = 0;
6940
6941 if (hw->mac.type != e1000_82573)
6942 return;
6943
6944 ret_val = e1000_read_nvm(hw, NVM_INIT_CONTROL2_REG, 1, &buf);
6945 le16_to_cpus(&buf);
6946 if (!ret_val && (!(buf & BIT(0)))) {
6947 /* Deep Smart Power Down (DSPD) */
6948 dev_warn(&adapter->pdev->dev,
6949 "Warning: detected DSPD enabled in EEPROM\n");
6950 }
6951}
6952
6953static netdev_features_t e1000_fix_features(struct net_device *netdev,
6954 netdev_features_t features)
6955{
6956 struct e1000_adapter *adapter = netdev_priv(netdev);
6957 struct e1000_hw *hw = &adapter->hw;
6958
6959 /* Jumbo frame workaround on 82579 and newer requires CRC be stripped */
6960 if ((hw->mac.type >= e1000_pch2lan) && (netdev->mtu > ETH_DATA_LEN))
6961 features &= ~NETIF_F_RXFCS;
6962
6963 /* Since there is no support for separate Rx/Tx vlan accel
6964 * enable/disable make sure Tx flag is always in same state as Rx.
6965 */
6966 if (features & NETIF_F_HW_VLAN_CTAG_RX)
6967 features |= NETIF_F_HW_VLAN_CTAG_TX;
6968 else
6969 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
6970
6971 return features;
6972}
6973
6974static int e1000_set_features(struct net_device *netdev,
6975 netdev_features_t features)
6976{
6977 struct e1000_adapter *adapter = netdev_priv(netdev);
6978 netdev_features_t changed = features ^ netdev->features;
6979
6980 if (changed & (NETIF_F_TSO | NETIF_F_TSO6))
6981 adapter->flags |= FLAG_TSO_FORCE;
6982
6983 if (!(changed & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
6984 NETIF_F_RXCSUM | NETIF_F_RXHASH | NETIF_F_RXFCS |
6985 NETIF_F_RXALL)))
6986 return 0;
6987
6988 if (changed & NETIF_F_RXFCS) {
6989 if (features & NETIF_F_RXFCS) {
6990 adapter->flags2 &= ~FLAG2_CRC_STRIPPING;
6991 } else {
6992 /* We need to take it back to defaults, which might mean
6993 * stripping is still disabled at the adapter level.
6994 */
6995 if (adapter->flags2 & FLAG2_DFLT_CRC_STRIPPING)
6996 adapter->flags2 |= FLAG2_CRC_STRIPPING;
6997 else
6998 adapter->flags2 &= ~FLAG2_CRC_STRIPPING;
6999 }
7000 }
7001
7002 netdev->features = features;
7003
7004 if (netif_running(netdev))
7005 e1000e_reinit_locked(adapter);
7006 else
7007 e1000e_reset(adapter);
7008
7009 return 1;
7010}
7011
7012static const struct net_device_ops e1000e_netdev_ops = {
7013 .ndo_open = e1000e_open,
7014 .ndo_stop = e1000e_close,
7015 .ndo_start_xmit = e1000_xmit_frame,
7016 .ndo_get_stats64 = e1000e_get_stats64,
7017 .ndo_set_rx_mode = e1000e_set_rx_mode,
7018 .ndo_set_mac_address = e1000_set_mac,
7019 .ndo_change_mtu = e1000_change_mtu,
7020 .ndo_do_ioctl = e1000_ioctl,
7021 .ndo_tx_timeout = e1000_tx_timeout,
7022 .ndo_validate_addr = eth_validate_addr,
7023
7024 .ndo_vlan_rx_add_vid = e1000_vlan_rx_add_vid,
7025 .ndo_vlan_rx_kill_vid = e1000_vlan_rx_kill_vid,
7026#ifdef CONFIG_NET_POLL_CONTROLLER
7027 .ndo_poll_controller = e1000_netpoll,
7028#endif
7029 .ndo_set_features = e1000_set_features,
7030 .ndo_fix_features = e1000_fix_features,
7031 .ndo_features_check = passthru_features_check,
7032};
7033
7034/**
7035 * e1000_probe - Device Initialization Routine
7036 * @pdev: PCI device information struct
7037 * @ent: entry in e1000_pci_tbl
7038 *
7039 * Returns 0 on success, negative on failure
7040 *
7041 * e1000_probe initializes an adapter identified by a pci_dev structure.
7042 * The OS initialization, configuring of the adapter private structure,
7043 * and a hardware reset occur.
7044 **/
7045static int e1000_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
7046{
7047 struct net_device *netdev;
7048 struct e1000_adapter *adapter;
7049 struct e1000_hw *hw;
7050 const struct e1000_info *ei = e1000_info_tbl[ent->driver_data];
7051 resource_size_t mmio_start, mmio_len;
7052 resource_size_t flash_start, flash_len;
7053 static int cards_found;
7054 u16 aspm_disable_flag = 0;
7055 int bars, i, err, pci_using_dac;
7056 u16 eeprom_data = 0;
7057 u16 eeprom_apme_mask = E1000_EEPROM_APME;
7058 s32 ret_val = 0;
7059
7060 if (ei->flags2 & FLAG2_DISABLE_ASPM_L0S)
7061 aspm_disable_flag = PCIE_LINK_STATE_L0S;
7062 if (ei->flags2 & FLAG2_DISABLE_ASPM_L1)
7063 aspm_disable_flag |= PCIE_LINK_STATE_L1;
7064 if (aspm_disable_flag)
7065 e1000e_disable_aspm(pdev, aspm_disable_flag);
7066
7067 err = pci_enable_device_mem(pdev);
7068 if (err)
7069 return err;
7070
7071 pci_using_dac = 0;
7072 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
7073 if (!err) {
7074 pci_using_dac = 1;
7075 } else {
7076 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
7077 if (err) {
7078 dev_err(&pdev->dev,
7079 "No usable DMA configuration, aborting\n");
7080 goto err_dma;
7081 }
7082 }
7083
7084 bars = pci_select_bars(pdev, IORESOURCE_MEM);
7085 err = pci_request_selected_regions_exclusive(pdev, bars,
7086 e1000e_driver_name);
7087 if (err)
7088 goto err_pci_reg;
7089
7090 /* AER (Advanced Error Reporting) hooks */
7091 pci_enable_pcie_error_reporting(pdev);
7092
7093 pci_set_master(pdev);
7094 /* PCI config space info */
7095 err = pci_save_state(pdev);
7096 if (err)
7097 goto err_alloc_etherdev;
7098
7099 err = -ENOMEM;
7100 netdev = alloc_etherdev(sizeof(struct e1000_adapter));
7101 if (!netdev)
7102 goto err_alloc_etherdev;
7103
7104 SET_NETDEV_DEV(netdev, &pdev->dev);
7105
7106 netdev->irq = pdev->irq;
7107
7108 pci_set_drvdata(pdev, netdev);
7109 adapter = netdev_priv(netdev);
7110 hw = &adapter->hw;
7111 adapter->netdev = netdev;
7112 adapter->pdev = pdev;
7113 adapter->ei = ei;
7114 adapter->pba = ei->pba;
7115 adapter->flags = ei->flags;
7116 adapter->flags2 = ei->flags2;
7117 adapter->hw.adapter = adapter;
7118 adapter->hw.mac.type = ei->mac;
7119 adapter->max_hw_frame_size = ei->max_hw_frame_size;
7120 adapter->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);
7121
7122 mmio_start = pci_resource_start(pdev, 0);
7123 mmio_len = pci_resource_len(pdev, 0);
7124
7125 err = -EIO;
7126 adapter->hw.hw_addr = ioremap(mmio_start, mmio_len);
7127 if (!adapter->hw.hw_addr)
7128 goto err_ioremap;
7129
7130 if ((adapter->flags & FLAG_HAS_FLASH) &&
7131 (pci_resource_flags(pdev, 1) & IORESOURCE_MEM) &&
7132 (hw->mac.type < e1000_pch_spt)) {
7133 flash_start = pci_resource_start(pdev, 1);
7134 flash_len = pci_resource_len(pdev, 1);
7135 adapter->hw.flash_address = ioremap(flash_start, flash_len);
7136 if (!adapter->hw.flash_address)
7137 goto err_flashmap;
7138 }
7139
7140 /* Set default EEE advertisement */
7141 if (adapter->flags2 & FLAG2_HAS_EEE)
7142 adapter->eee_advert = MDIO_EEE_100TX | MDIO_EEE_1000T;
7143
7144 /* construct the net_device struct */
7145 netdev->netdev_ops = &e1000e_netdev_ops;
7146 e1000e_set_ethtool_ops(netdev);
7147 netdev->watchdog_timeo = 5 * HZ;
7148 netif_napi_add(netdev, &adapter->napi, e1000e_poll, 64);
7149 strlcpy(netdev->name, pci_name(pdev), sizeof(netdev->name));
7150
7151 netdev->mem_start = mmio_start;
7152 netdev->mem_end = mmio_start + mmio_len;
7153
7154 adapter->bd_number = cards_found++;
7155
7156 e1000e_check_options(adapter);
7157
7158 /* setup adapter struct */
7159 err = e1000_sw_init(adapter);
7160 if (err)
7161 goto err_sw_init;
7162
7163 memcpy(&hw->mac.ops, ei->mac_ops, sizeof(hw->mac.ops));
7164 memcpy(&hw->nvm.ops, ei->nvm_ops, sizeof(hw->nvm.ops));
7165 memcpy(&hw->phy.ops, ei->phy_ops, sizeof(hw->phy.ops));
7166
7167 err = ei->get_variants(adapter);
7168 if (err)
7169 goto err_hw_init;
7170
7171 if ((adapter->flags & FLAG_IS_ICH) &&
7172 (adapter->flags & FLAG_READ_ONLY_NVM) &&
7173 (hw->mac.type < e1000_pch_spt))
7174 e1000e_write_protect_nvm_ich8lan(&adapter->hw);
7175
7176 hw->mac.ops.get_bus_info(&adapter->hw);
7177
7178 adapter->hw.phy.autoneg_wait_to_complete = 0;
7179
7180 /* Copper options */
7181 if (adapter->hw.phy.media_type == e1000_media_type_copper) {
7182 adapter->hw.phy.mdix = AUTO_ALL_MODES;
7183 adapter->hw.phy.disable_polarity_correction = 0;
7184 adapter->hw.phy.ms_type = e1000_ms_hw_default;
7185 }
7186
7187 if (hw->phy.ops.check_reset_block && hw->phy.ops.check_reset_block(hw))
7188 dev_info(&pdev->dev,
7189 "PHY reset is blocked due to SOL/IDER session.\n");
7190
7191 /* Set initial default active device features */
7192 netdev->features = (NETIF_F_SG |
7193 NETIF_F_HW_VLAN_CTAG_RX |
7194 NETIF_F_HW_VLAN_CTAG_TX |
7195 NETIF_F_TSO |
7196 NETIF_F_TSO6 |
7197 NETIF_F_RXHASH |
7198 NETIF_F_RXCSUM |
7199 NETIF_F_HW_CSUM);
7200
7201 /* disable TSO for pcie and 10/100 speeds to avoid
7202 * some hardware issues and for i219 to fix transfer
7203 * speed being capped at 60%
7204 */
7205 if (!(adapter->flags & FLAG_TSO_FORCE)) {
7206 switch (adapter->link_speed) {
7207 case SPEED_10:
7208 case SPEED_100:
7209 e_info("10/100 speed: disabling TSO\n");
7210 netdev->features &= ~NETIF_F_TSO;
7211 netdev->features &= ~NETIF_F_TSO6;
7212 break;
7213 case SPEED_1000:
7214 netdev->features |= NETIF_F_TSO;
7215 netdev->features |= NETIF_F_TSO6;
7216 break;
7217 default:
7218 /* oops */
7219 break;
7220 }
7221 if (hw->mac.type == e1000_pch_spt) {
7222 netdev->features &= ~NETIF_F_TSO;
7223 netdev->features &= ~NETIF_F_TSO6;
7224 }
7225 }
7226
7227 /* Set user-changeable features (subset of all device features) */
7228 netdev->hw_features = netdev->features;
7229 netdev->hw_features |= NETIF_F_RXFCS;
7230 netdev->priv_flags |= IFF_SUPP_NOFCS;
7231 netdev->hw_features |= NETIF_F_RXALL;
7232
7233 if (adapter->flags & FLAG_HAS_HW_VLAN_FILTER)
7234 netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
7235
7236 netdev->vlan_features |= (NETIF_F_SG |
7237 NETIF_F_TSO |
7238 NETIF_F_TSO6 |
7239 NETIF_F_HW_CSUM);
7240
7241 netdev->priv_flags |= IFF_UNICAST_FLT;
7242
7243 if (pci_using_dac) {
7244 netdev->features |= NETIF_F_HIGHDMA;
7245 netdev->vlan_features |= NETIF_F_HIGHDMA;
7246 }
7247
7248 /* MTU range: 68 - max_hw_frame_size */
7249 netdev->min_mtu = ETH_MIN_MTU;
7250 netdev->max_mtu = adapter->max_hw_frame_size -
7251 (VLAN_ETH_HLEN + ETH_FCS_LEN);
7252
7253 if (e1000e_enable_mng_pass_thru(&adapter->hw))
7254 adapter->flags |= FLAG_MNG_PT_ENABLED;
7255
7256 /* before reading the NVM, reset the controller to
7257 * put the device in a known good starting state
7258 */
7259 adapter->hw.mac.ops.reset_hw(&adapter->hw);
7260
7261 /* systems with ASPM and others may see the checksum fail on the first
7262 * attempt. Let's give it a few tries
7263 */
7264 for (i = 0;; i++) {
7265 if (e1000_validate_nvm_checksum(&adapter->hw) >= 0)
7266 break;
7267 if (i == 2) {
7268 dev_err(&pdev->dev, "The NVM Checksum Is Not Valid\n");
7269 err = -EIO;
7270 goto err_eeprom;
7271 }
7272 }
7273
7274 e1000_eeprom_checks(adapter);
7275
7276 /* copy the MAC address */
7277 if (e1000e_read_mac_addr(&adapter->hw))
7278 dev_err(&pdev->dev,
7279 "NVM Read Error while reading MAC address\n");
7280
7281 memcpy(netdev->dev_addr, adapter->hw.mac.addr, netdev->addr_len);
7282
7283 if (!is_valid_ether_addr(netdev->dev_addr)) {
7284 dev_err(&pdev->dev, "Invalid MAC Address: %pM\n",
7285 netdev->dev_addr);
7286 err = -EIO;
7287 goto err_eeprom;
7288 }
7289
7290 timer_setup(&adapter->watchdog_timer, e1000_watchdog, 0);
7291 timer_setup(&adapter->phy_info_timer, e1000_update_phy_info, 0);
7292
7293 INIT_WORK(&adapter->reset_task, e1000_reset_task);
7294 INIT_WORK(&adapter->watchdog_task, e1000_watchdog_task);
7295 INIT_WORK(&adapter->downshift_task, e1000e_downshift_workaround);
7296 INIT_WORK(&adapter->update_phy_task, e1000e_update_phy_task);
7297 INIT_WORK(&adapter->print_hang_task, e1000_print_hw_hang);
7298
7299 /* Initialize link parameters. User can change them with ethtool */
7300 adapter->hw.mac.autoneg = 1;
7301 adapter->fc_autoneg = true;
7302 adapter->hw.fc.requested_mode = e1000_fc_default;
7303 adapter->hw.fc.current_mode = e1000_fc_default;
7304 adapter->hw.phy.autoneg_advertised = 0x2f;
7305
7306 /* Initial Wake on LAN setting - If APM wake is enabled in
7307 * the EEPROM, enable the ACPI Magic Packet filter
7308 */
7309 if (adapter->flags & FLAG_APME_IN_WUC) {
7310 /* APME bit in EEPROM is mapped to WUC.APME */
7311 eeprom_data = er32(WUC);
7312 eeprom_apme_mask = E1000_WUC_APME;
7313 if ((hw->mac.type > e1000_ich10lan) &&
7314 (eeprom_data & E1000_WUC_PHY_WAKE))
7315 adapter->flags2 |= FLAG2_HAS_PHY_WAKEUP;
7316 } else if (adapter->flags & FLAG_APME_IN_CTRL3) {
7317 if (adapter->flags & FLAG_APME_CHECK_PORT_B &&
7318 (adapter->hw.bus.func == 1))
7319 ret_val = e1000_read_nvm(&adapter->hw,
7320 NVM_INIT_CONTROL3_PORT_B,
7321 1, &eeprom_data);
7322 else
7323 ret_val = e1000_read_nvm(&adapter->hw,
7324 NVM_INIT_CONTROL3_PORT_A,
7325 1, &eeprom_data);
7326 }
7327
7328 /* fetch WoL from EEPROM */
7329 if (ret_val)
7330 e_dbg("NVM read error getting WoL initial values: %d\n", ret_val);
7331 else if (eeprom_data & eeprom_apme_mask)
7332 adapter->eeprom_wol |= E1000_WUFC_MAG;
7333
7334 /* now that we have the eeprom settings, apply the special cases
7335 * where the eeprom may be wrong or the board simply won't support
7336 * wake on lan on a particular port
7337 */
7338 if (!(adapter->flags & FLAG_HAS_WOL))
7339 adapter->eeprom_wol = 0;
7340
7341 /* initialize the wol settings based on the eeprom settings */
7342 adapter->wol = adapter->eeprom_wol;
7343
7344 /* make sure adapter isn't asleep if manageability is enabled */
7345 if (adapter->wol || (adapter->flags & FLAG_MNG_PT_ENABLED) ||
7346 (hw->mac.ops.check_mng_mode(hw)))
7347 device_wakeup_enable(&pdev->dev);
7348
7349 /* save off EEPROM version number */
7350 ret_val = e1000_read_nvm(&adapter->hw, 5, 1, &adapter->eeprom_vers);
7351
7352 if (ret_val) {
7353 e_dbg("NVM read error getting EEPROM version: %d\n", ret_val);
7354 adapter->eeprom_vers = 0;
7355 }
7356
7357 /* init PTP hardware clock */
7358 e1000e_ptp_init(adapter);
7359
7360 /* reset the hardware with the new settings */
7361 e1000e_reset(adapter);
7362
7363 /* If the controller has AMT, do not set DRV_LOAD until the interface
7364 * is up. For all other cases, let the f/w know that the h/w is now
7365 * under the control of the driver.
7366 */
7367 if (!(adapter->flags & FLAG_HAS_AMT))
7368 e1000e_get_hw_control(adapter);
7369
7370 strlcpy(netdev->name, "eth%d", sizeof(netdev->name));
7371 err = register_netdev(netdev);
7372 if (err)
7373 goto err_register;
7374
7375 /* carrier off reporting is important to ethtool even BEFORE open */
7376 netif_carrier_off(netdev);
7377
7378 e1000_print_device_info(adapter);
7379
7380 dev_pm_set_driver_flags(&pdev->dev, DPM_FLAG_NEVER_SKIP);
7381
7382 if (pci_dev_run_wake(pdev) && hw->mac.type < e1000_pch_cnp)
7383 pm_runtime_put_noidle(&pdev->dev);
7384
7385 return 0;
7386
7387err_register:
7388 if (!(adapter->flags & FLAG_HAS_AMT))
7389 e1000e_release_hw_control(adapter);
7390err_eeprom:
7391 if (hw->phy.ops.check_reset_block && !hw->phy.ops.check_reset_block(hw))
7392 e1000_phy_hw_reset(&adapter->hw);
7393err_hw_init:
7394 kfree(adapter->tx_ring);
7395 kfree(adapter->rx_ring);
7396err_sw_init:
7397 if ((adapter->hw.flash_address) && (hw->mac.type < e1000_pch_spt))
7398 iounmap(adapter->hw.flash_address);
7399 e1000e_reset_interrupt_capability(adapter);
7400err_flashmap:
7401 iounmap(adapter->hw.hw_addr);
7402err_ioremap:
7403 free_netdev(netdev);
7404err_alloc_etherdev:
7405 pci_disable_pcie_error_reporting(pdev);
7406 pci_release_mem_regions(pdev);
7407err_pci_reg:
7408err_dma:
7409 pci_disable_device(pdev);
7410 return err;
7411}
7412
7413/**
7414 * e1000_remove - Device Removal Routine
7415 * @pdev: PCI device information struct
7416 *
7417 * e1000_remove is called by the PCI subsystem to alert the driver
7418 * that it should release a PCI device. The could be caused by a
7419 * Hot-Plug event, or because the driver is going to be removed from
7420 * memory.
7421 **/
7422static void e1000_remove(struct pci_dev *pdev)
7423{
7424 struct net_device *netdev = pci_get_drvdata(pdev);
7425 struct e1000_adapter *adapter = netdev_priv(netdev);
7426
7427 e1000e_ptp_remove(adapter);
7428
7429 /* The timers may be rescheduled, so explicitly disable them
7430 * from being rescheduled.
7431 */
7432 set_bit(__E1000_DOWN, &adapter->state);
7433 del_timer_sync(&adapter->watchdog_timer);
7434 del_timer_sync(&adapter->phy_info_timer);
7435
7436 cancel_work_sync(&adapter->reset_task);
7437 cancel_work_sync(&adapter->watchdog_task);
7438 cancel_work_sync(&adapter->downshift_task);
7439 cancel_work_sync(&adapter->update_phy_task);
7440 cancel_work_sync(&adapter->print_hang_task);
7441
7442 if (adapter->flags & FLAG_HAS_HW_TIMESTAMP) {
7443 cancel_work_sync(&adapter->tx_hwtstamp_work);
7444 if (adapter->tx_hwtstamp_skb) {
7445 dev_consume_skb_any(adapter->tx_hwtstamp_skb);
7446 adapter->tx_hwtstamp_skb = NULL;
7447 }
7448 }
7449
7450 unregister_netdev(netdev);
7451
7452 if (pci_dev_run_wake(pdev))
7453 pm_runtime_get_noresume(&pdev->dev);
7454
7455 /* Release control of h/w to f/w. If f/w is AMT enabled, this
7456 * would have already happened in close and is redundant.
7457 */
7458 e1000e_release_hw_control(adapter);
7459
7460 e1000e_reset_interrupt_capability(adapter);
7461 kfree(adapter->tx_ring);
7462 kfree(adapter->rx_ring);
7463
7464 iounmap(adapter->hw.hw_addr);
7465 if ((adapter->hw.flash_address) &&
7466 (adapter->hw.mac.type < e1000_pch_spt))
7467 iounmap(adapter->hw.flash_address);
7468 pci_release_mem_regions(pdev);
7469
7470 free_netdev(netdev);
7471
7472 /* AER disable */
7473 pci_disable_pcie_error_reporting(pdev);
7474
7475 pci_disable_device(pdev);
7476}
7477
7478/* PCI Error Recovery (ERS) */
7479static const struct pci_error_handlers e1000_err_handler = {
7480 .error_detected = e1000_io_error_detected,
7481 .slot_reset = e1000_io_slot_reset,
7482 .resume = e1000_io_resume,
7483};
7484
7485static const struct pci_device_id e1000_pci_tbl[] = {
7486 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_COPPER), board_82571 },
7487 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_FIBER), board_82571 },
7488 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER), board_82571 },
7489 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_COPPER_LP),
7490 board_82571 },
7491 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_QUAD_FIBER), board_82571 },
7492 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES), board_82571 },
7493 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_DUAL), board_82571 },
7494 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571EB_SERDES_QUAD), board_82571 },
7495 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82571PT_QUAD_COPPER), board_82571 },
7496
7497 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI), board_82572 },
7498 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_COPPER), board_82572 },
7499 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_FIBER), board_82572 },
7500 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82572EI_SERDES), board_82572 },
7501
7502 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E), board_82573 },
7503 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573E_IAMT), board_82573 },
7504 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82573L), board_82573 },
7505
7506 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82574L), board_82574 },
7507 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82574LA), board_82574 },
7508 { PCI_VDEVICE(INTEL, E1000_DEV_ID_82583V), board_82583 },
7509
7510 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_DPT),
7511 board_80003es2lan },
7512 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_COPPER_SPT),
7513 board_80003es2lan },
7514 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_DPT),
7515 board_80003es2lan },
7516 { PCI_VDEVICE(INTEL, E1000_DEV_ID_80003ES2LAN_SERDES_SPT),
7517 board_80003es2lan },
7518
7519 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE), board_ich8lan },
7520 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_G), board_ich8lan },
7521 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IFE_GT), board_ich8lan },
7522 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_AMT), board_ich8lan },
7523 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_C), board_ich8lan },
7524 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M), board_ich8lan },
7525 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_IGP_M_AMT), board_ich8lan },
7526 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH8_82567V_3), board_ich8lan },
7527
7528 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE), board_ich9lan },
7529 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_G), board_ich9lan },
7530 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IFE_GT), board_ich9lan },
7531 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_AMT), board_ich9lan },
7532 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_C), board_ich9lan },
7533 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_BM), board_ich9lan },
7534 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M), board_ich9lan },
7535 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_AMT), board_ich9lan },
7536 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH9_IGP_M_V), board_ich9lan },
7537
7538 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LM), board_ich9lan },
7539 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_LF), board_ich9lan },
7540 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_R_BM_V), board_ich9lan },
7541
7542 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LM), board_ich10lan },
7543 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_LF), board_ich10lan },
7544 { PCI_VDEVICE(INTEL, E1000_DEV_ID_ICH10_D_BM_V), board_ich10lan },
7545
7546 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_M_HV_LM), board_pchlan },
7547 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_M_HV_LC), board_pchlan },
7548 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_D_HV_DM), board_pchlan },
7549 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_D_HV_DC), board_pchlan },
7550
7551 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH2_LV_LM), board_pch2lan },
7552 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH2_LV_V), board_pch2lan },
7553
7554 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LPT_I217_LM), board_pch_lpt },
7555 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LPT_I217_V), board_pch_lpt },
7556 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LPTLP_I218_LM), board_pch_lpt },
7557 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LPTLP_I218_V), board_pch_lpt },
7558 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_I218_LM2), board_pch_lpt },
7559 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_I218_V2), board_pch_lpt },
7560 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_I218_LM3), board_pch_lpt },
7561 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_I218_V3), board_pch_lpt },
7562 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_LM), board_pch_spt },
7563 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_V), board_pch_spt },
7564 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_LM2), board_pch_spt },
7565 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_V2), board_pch_spt },
7566 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_LBG_I219_LM3), board_pch_spt },
7567 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_LM4), board_pch_spt },
7568 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_V4), board_pch_spt },
7569 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_LM5), board_pch_spt },
7570 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_SPT_I219_V5), board_pch_spt },
7571 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CNP_I219_LM6), board_pch_cnp },
7572 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CNP_I219_V6), board_pch_cnp },
7573 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CNP_I219_LM7), board_pch_cnp },
7574 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CNP_I219_V7), board_pch_cnp },
7575 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ICP_I219_LM8), board_pch_cnp },
7576 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ICP_I219_V8), board_pch_cnp },
7577 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ICP_I219_LM9), board_pch_cnp },
7578 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_ICP_I219_V9), board_pch_cnp },
7579 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_LM10), board_pch_cnp },
7580 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_V10), board_pch_cnp },
7581 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_LM11), board_pch_cnp },
7582 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_V11), board_pch_cnp },
7583 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_LM12), board_pch_spt },
7584 { PCI_VDEVICE(INTEL, E1000_DEV_ID_PCH_CMP_I219_V12), board_pch_spt },
7585
7586 { 0, 0, 0, 0, 0, 0, 0 } /* terminate list */
7587};
7588MODULE_DEVICE_TABLE(pci, e1000_pci_tbl);
7589
7590static const struct dev_pm_ops e1000_pm_ops = {
7591#ifdef CONFIG_PM_SLEEP
7592 .suspend = e1000e_pm_suspend,
7593 .resume = e1000e_pm_resume,
7594 .freeze = e1000e_pm_freeze,
7595 .thaw = e1000e_pm_thaw,
7596 .poweroff = e1000e_pm_suspend,
7597 .restore = e1000e_pm_resume,
7598#endif
7599 SET_RUNTIME_PM_OPS(e1000e_pm_runtime_suspend, e1000e_pm_runtime_resume,
7600 e1000e_pm_runtime_idle)
7601};
7602
7603/* PCI Device API Driver */
7604static struct pci_driver e1000_driver = {
7605 .name = e1000e_driver_name,
7606 .id_table = e1000_pci_tbl,
7607 .probe = e1000_probe,
7608 .remove = e1000_remove,
7609 .driver = {
7610 .pm = &e1000_pm_ops,
7611 },
7612 .shutdown = e1000_shutdown,
7613 .err_handler = &e1000_err_handler
7614};
7615
7616/**
7617 * e1000_init_module - Driver Registration Routine
7618 *
7619 * e1000_init_module is the first routine called when the driver is
7620 * loaded. All it does is register with the PCI subsystem.
7621 **/
7622static int __init e1000_init_module(void)
7623{
7624 pr_info("Intel(R) PRO/1000 Network Driver - %s\n",
7625 e1000e_driver_version);
7626 pr_info("Copyright(c) 1999 - 2015 Intel Corporation.\n");
7627
7628 return pci_register_driver(&e1000_driver);
7629}
7630module_init(e1000_init_module);
7631
7632/**
7633 * e1000_exit_module - Driver Exit Cleanup Routine
7634 *
7635 * e1000_exit_module is called just before the driver is removed
7636 * from memory.
7637 **/
7638static void __exit e1000_exit_module(void)
7639{
7640 pci_unregister_driver(&e1000_driver);
7641}
7642module_exit(e1000_exit_module);
7643
7644MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
7645MODULE_DESCRIPTION("Intel(R) PRO/1000 Network Driver");
7646MODULE_LICENSE("GPL v2");
7647MODULE_VERSION(DRV_VERSION);
7648
7649/* netdev.c */