e1000/base: update copyright to 2014
[dpdk.git] / lib / librte_pmd_e1000 / e1000 / e1000_mac.c
1 /*******************************************************************************
2
3 Copyright (c) 2001-2014, Intel Corporation
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8
9  1. Redistributions of source code must retain the above copyright notice,
10     this list of conditions and the following disclaimer.
11
12  2. Redistributions in binary form must reproduce the above copyright
13     notice, this list of conditions and the following disclaimer in the
14     documentation and/or other materials provided with the distribution.
15
16  3. Neither the name of the Intel Corporation nor the names of its
17     contributors may be used to endorse or promote products derived from
18     this software without specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
31
32 ***************************************************************************/
33
34 #include "e1000_api.h"
35
36 STATIC s32 e1000_validate_mdi_setting_generic(struct e1000_hw *hw);
37 STATIC void e1000_set_lan_id_multi_port_pcie(struct e1000_hw *hw);
38 STATIC void e1000_config_collision_dist_generic(struct e1000_hw *hw);
39 STATIC void e1000_rar_set_generic(struct e1000_hw *hw, u8 *addr, u32 index);
40
41 /**
42  *  e1000_init_mac_ops_generic - Initialize MAC function pointers
43  *  @hw: pointer to the HW structure
44  *
45  *  Setups up the function pointers to no-op functions
46  **/
47 void e1000_init_mac_ops_generic(struct e1000_hw *hw)
48 {
49         struct e1000_mac_info *mac = &hw->mac;
50         DEBUGFUNC("e1000_init_mac_ops_generic");
51
52         /* General Setup */
53         mac->ops.init_params = e1000_null_ops_generic;
54         mac->ops.init_hw = e1000_null_ops_generic;
55         mac->ops.reset_hw = e1000_null_ops_generic;
56         mac->ops.setup_physical_interface = e1000_null_ops_generic;
57         mac->ops.get_bus_info = e1000_null_ops_generic;
58         mac->ops.set_lan_id = e1000_set_lan_id_multi_port_pcie;
59         mac->ops.read_mac_addr = e1000_read_mac_addr_generic;
60         mac->ops.config_collision_dist = e1000_config_collision_dist_generic;
61         mac->ops.clear_hw_cntrs = e1000_null_mac_generic;
62         /* LED */
63         mac->ops.cleanup_led = e1000_null_ops_generic;
64         mac->ops.setup_led = e1000_null_ops_generic;
65         mac->ops.blink_led = e1000_null_ops_generic;
66         mac->ops.led_on = e1000_null_ops_generic;
67         mac->ops.led_off = e1000_null_ops_generic;
68         /* LINK */
69         mac->ops.setup_link = e1000_null_ops_generic;
70         mac->ops.get_link_up_info = e1000_null_link_info;
71         mac->ops.check_for_link = e1000_null_ops_generic;
72         /* Management */
73         mac->ops.check_mng_mode = e1000_null_mng_mode;
74         /* VLAN, MC, etc. */
75         mac->ops.update_mc_addr_list = e1000_null_update_mc;
76         mac->ops.clear_vfta = e1000_null_mac_generic;
77         mac->ops.write_vfta = e1000_null_write_vfta;
78         mac->ops.rar_set = e1000_rar_set_generic;
79         mac->ops.validate_mdi_setting = e1000_validate_mdi_setting_generic;
80 }
81
82 /**
83  *  e1000_null_ops_generic - No-op function, returns 0
84  *  @hw: pointer to the HW structure
85  **/
86 s32 e1000_null_ops_generic(struct e1000_hw E1000_UNUSEDARG *hw)
87 {
88         DEBUGFUNC("e1000_null_ops_generic");
89         UNREFERENCED_1PARAMETER(hw);
90         return E1000_SUCCESS;
91 }
92
93 /**
94  *  e1000_null_mac_generic - No-op function, return void
95  *  @hw: pointer to the HW structure
96  **/
97 void e1000_null_mac_generic(struct e1000_hw E1000_UNUSEDARG *hw)
98 {
99         DEBUGFUNC("e1000_null_mac_generic");
100         UNREFERENCED_1PARAMETER(hw);
101         return;
102 }
103
104 /**
105  *  e1000_null_link_info - No-op function, return 0
106  *  @hw: pointer to the HW structure
107  **/
108 s32 e1000_null_link_info(struct e1000_hw E1000_UNUSEDARG *hw,
109                          u16 E1000_UNUSEDARG *s, u16 E1000_UNUSEDARG *d)
110 {
111         DEBUGFUNC("e1000_null_link_info");
112         UNREFERENCED_3PARAMETER(hw, s, d);
113         return E1000_SUCCESS;
114 }
115
116 /**
117  *  e1000_null_mng_mode - No-op function, return false
118  *  @hw: pointer to the HW structure
119  **/
120 bool e1000_null_mng_mode(struct e1000_hw E1000_UNUSEDARG *hw)
121 {
122         DEBUGFUNC("e1000_null_mng_mode");
123         UNREFERENCED_1PARAMETER(hw);
124         return false;
125 }
126
127 /**
128  *  e1000_null_update_mc - No-op function, return void
129  *  @hw: pointer to the HW structure
130  **/
131 void e1000_null_update_mc(struct e1000_hw E1000_UNUSEDARG *hw,
132                           u8 E1000_UNUSEDARG *h, u32 E1000_UNUSEDARG a)
133 {
134         DEBUGFUNC("e1000_null_update_mc");
135         UNREFERENCED_3PARAMETER(hw, h, a);
136         return;
137 }
138
139 /**
140  *  e1000_null_write_vfta - No-op function, return void
141  *  @hw: pointer to the HW structure
142  **/
143 void e1000_null_write_vfta(struct e1000_hw E1000_UNUSEDARG *hw,
144                            u32 E1000_UNUSEDARG a, u32 E1000_UNUSEDARG b)
145 {
146         DEBUGFUNC("e1000_null_write_vfta");
147         UNREFERENCED_3PARAMETER(hw, a, b);
148         return;
149 }
150
151 /**
152  *  e1000_null_rar_set - No-op function, return void
153  *  @hw: pointer to the HW structure
154  **/
155 void e1000_null_rar_set(struct e1000_hw E1000_UNUSEDARG *hw,
156                         u8 E1000_UNUSEDARG *h, u32 E1000_UNUSEDARG a)
157 {
158         DEBUGFUNC("e1000_null_rar_set");
159         UNREFERENCED_3PARAMETER(hw, h, a);
160         return;
161 }
162
163 /**
164  *  e1000_get_bus_info_pci_generic - Get PCI(x) bus information
165  *  @hw: pointer to the HW structure
166  *
167  *  Determines and stores the system bus information for a particular
168  *  network interface.  The following bus information is determined and stored:
169  *  bus speed, bus width, type (PCI/PCIx), and PCI(-x) function.
170  **/
171 s32 e1000_get_bus_info_pci_generic(struct e1000_hw *hw)
172 {
173         struct e1000_mac_info *mac = &hw->mac;
174         struct e1000_bus_info *bus = &hw->bus;
175         u32 status = E1000_READ_REG(hw, E1000_STATUS);
176         s32 ret_val = E1000_SUCCESS;
177
178         DEBUGFUNC("e1000_get_bus_info_pci_generic");
179
180         /* PCI or PCI-X? */
181         bus->type = (status & E1000_STATUS_PCIX_MODE)
182                         ? e1000_bus_type_pcix
183                         : e1000_bus_type_pci;
184
185         /* Bus speed */
186         if (bus->type == e1000_bus_type_pci) {
187                 bus->speed = (status & E1000_STATUS_PCI66)
188                              ? e1000_bus_speed_66
189                              : e1000_bus_speed_33;
190         } else {
191                 switch (status & E1000_STATUS_PCIX_SPEED) {
192                 case E1000_STATUS_PCIX_SPEED_66:
193                         bus->speed = e1000_bus_speed_66;
194                         break;
195                 case E1000_STATUS_PCIX_SPEED_100:
196                         bus->speed = e1000_bus_speed_100;
197                         break;
198                 case E1000_STATUS_PCIX_SPEED_133:
199                         bus->speed = e1000_bus_speed_133;
200                         break;
201                 default:
202                         bus->speed = e1000_bus_speed_reserved;
203                         break;
204                 }
205         }
206
207         /* Bus width */
208         bus->width = (status & E1000_STATUS_BUS64)
209                      ? e1000_bus_width_64
210                      : e1000_bus_width_32;
211
212         /* Which PCI(-X) function? */
213         mac->ops.set_lan_id(hw);
214
215         return ret_val;
216 }
217
218 /**
219  *  e1000_get_bus_info_pcie_generic - Get PCIe bus information
220  *  @hw: pointer to the HW structure
221  *
222  *  Determines and stores the system bus information for a particular
223  *  network interface.  The following bus information is determined and stored:
224  *  bus speed, bus width, type (PCIe), and PCIe function.
225  **/
226 s32 e1000_get_bus_info_pcie_generic(struct e1000_hw *hw)
227 {
228         struct e1000_mac_info *mac = &hw->mac;
229         struct e1000_bus_info *bus = &hw->bus;
230         s32 ret_val;
231         u16 pcie_link_status;
232
233         DEBUGFUNC("e1000_get_bus_info_pcie_generic");
234
235         bus->type = e1000_bus_type_pci_express;
236
237         ret_val = e1000_read_pcie_cap_reg(hw, PCIE_LINK_STATUS,
238                                           &pcie_link_status);
239         if (ret_val) {
240                 bus->width = e1000_bus_width_unknown;
241                 bus->speed = e1000_bus_speed_unknown;
242         } else {
243                 switch (pcie_link_status & PCIE_LINK_SPEED_MASK) {
244                 case PCIE_LINK_SPEED_2500:
245                         bus->speed = e1000_bus_speed_2500;
246                         break;
247                 case PCIE_LINK_SPEED_5000:
248                         bus->speed = e1000_bus_speed_5000;
249                         break;
250                 default:
251                         bus->speed = e1000_bus_speed_unknown;
252                         break;
253                 }
254
255                 bus->width = (enum e1000_bus_width)((pcie_link_status &
256                               PCIE_LINK_WIDTH_MASK) >> PCIE_LINK_WIDTH_SHIFT);
257         }
258
259         mac->ops.set_lan_id(hw);
260
261         return E1000_SUCCESS;
262 }
263
264 /**
265  *  e1000_set_lan_id_multi_port_pcie - Set LAN id for PCIe multiple port devices
266  *
267  *  @hw: pointer to the HW structure
268  *
269  *  Determines the LAN function id by reading memory-mapped registers
270  *  and swaps the port value if requested.
271  **/
272 STATIC void e1000_set_lan_id_multi_port_pcie(struct e1000_hw *hw)
273 {
274         struct e1000_bus_info *bus = &hw->bus;
275         u32 reg;
276
277         /* The status register reports the correct function number
278          * for the device regardless of function swap state.
279          */
280         reg = E1000_READ_REG(hw, E1000_STATUS);
281         bus->func = (reg & E1000_STATUS_FUNC_MASK) >> E1000_STATUS_FUNC_SHIFT;
282 }
283
284 /**
285  *  e1000_set_lan_id_multi_port_pci - Set LAN id for PCI multiple port devices
286  *  @hw: pointer to the HW structure
287  *
288  *  Determines the LAN function id by reading PCI config space.
289  **/
290 void e1000_set_lan_id_multi_port_pci(struct e1000_hw *hw)
291 {
292         struct e1000_bus_info *bus = &hw->bus;
293         u16 pci_header_type;
294         u32 status;
295
296         e1000_read_pci_cfg(hw, PCI_HEADER_TYPE_REGISTER, &pci_header_type);
297         if (pci_header_type & PCI_HEADER_TYPE_MULTIFUNC) {
298                 status = E1000_READ_REG(hw, E1000_STATUS);
299                 bus->func = (status & E1000_STATUS_FUNC_MASK)
300                             >> E1000_STATUS_FUNC_SHIFT;
301         } else {
302                 bus->func = 0;
303         }
304 }
305
306 /**
307  *  e1000_set_lan_id_single_port - Set LAN id for a single port device
308  *  @hw: pointer to the HW structure
309  *
310  *  Sets the LAN function id to zero for a single port device.
311  **/
312 void e1000_set_lan_id_single_port(struct e1000_hw *hw)
313 {
314         struct e1000_bus_info *bus = &hw->bus;
315
316         bus->func = 0;
317 }
318
319 /**
320  *  e1000_clear_vfta_generic - Clear VLAN filter table
321  *  @hw: pointer to the HW structure
322  *
323  *  Clears the register array which contains the VLAN filter table by
324  *  setting all the values to 0.
325  **/
326 void e1000_clear_vfta_generic(struct e1000_hw *hw)
327 {
328         u32 offset;
329
330         DEBUGFUNC("e1000_clear_vfta_generic");
331
332         for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
333                 E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, 0);
334                 E1000_WRITE_FLUSH(hw);
335         }
336 }
337
338 /**
339  *  e1000_write_vfta_generic - Write value to VLAN filter table
340  *  @hw: pointer to the HW structure
341  *  @offset: register offset in VLAN filter table
342  *  @value: register value written to VLAN filter table
343  *
344  *  Writes value at the given offset in the register array which stores
345  *  the VLAN filter table.
346  **/
347 void e1000_write_vfta_generic(struct e1000_hw *hw, u32 offset, u32 value)
348 {
349         DEBUGFUNC("e1000_write_vfta_generic");
350
351         E1000_WRITE_REG_ARRAY(hw, E1000_VFTA, offset, value);
352         E1000_WRITE_FLUSH(hw);
353 }
354
355 /**
356  *  e1000_init_rx_addrs_generic - Initialize receive address's
357  *  @hw: pointer to the HW structure
358  *  @rar_count: receive address registers
359  *
360  *  Setup the receive address registers by setting the base receive address
361  *  register to the devices MAC address and clearing all the other receive
362  *  address registers to 0.
363  **/
364 void e1000_init_rx_addrs_generic(struct e1000_hw *hw, u16 rar_count)
365 {
366         u32 i;
367         u8 mac_addr[ETH_ADDR_LEN] = {0};
368
369         DEBUGFUNC("e1000_init_rx_addrs_generic");
370
371         /* Setup the receive address */
372         DEBUGOUT("Programming MAC Address into RAR[0]\n");
373
374         hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
375
376         /* Zero out the other (rar_entry_count - 1) receive addresses */
377         DEBUGOUT1("Clearing RAR[1-%u]\n", rar_count-1);
378         for (i = 1; i < rar_count; i++)
379                 hw->mac.ops.rar_set(hw, mac_addr, i);
380 }
381
382 /**
383  *  e1000_check_alt_mac_addr_generic - Check for alternate MAC addr
384  *  @hw: pointer to the HW structure
385  *
386  *  Checks the nvm for an alternate MAC address.  An alternate MAC address
387  *  can be setup by pre-boot software and must be treated like a permanent
388  *  address and must override the actual permanent MAC address. If an
389  *  alternate MAC address is found it is programmed into RAR0, replacing
390  *  the permanent address that was installed into RAR0 by the Si on reset.
391  *  This function will return SUCCESS unless it encounters an error while
392  *  reading the EEPROM.
393  **/
394 s32 e1000_check_alt_mac_addr_generic(struct e1000_hw *hw)
395 {
396         u32 i;
397         s32 ret_val;
398         u16 offset, nvm_alt_mac_addr_offset, nvm_data;
399         u8 alt_mac_addr[ETH_ADDR_LEN];
400
401         DEBUGFUNC("e1000_check_alt_mac_addr_generic");
402
403         ret_val = hw->nvm.ops.read(hw, NVM_COMPAT, 1, &nvm_data);
404         if (ret_val)
405                 return ret_val;
406
407         /* not supported on older hardware or 82573 */
408         if ((hw->mac.type < e1000_82571) || (hw->mac.type == e1000_82573))
409                 return E1000_SUCCESS;
410
411         /* Alternate MAC address is handled by the option ROM for 82580
412          * and newer. SW support not required.
413          */
414         if (hw->mac.type >= e1000_82580)
415                 return E1000_SUCCESS;
416
417         ret_val = hw->nvm.ops.read(hw, NVM_ALT_MAC_ADDR_PTR, 1,
418                                    &nvm_alt_mac_addr_offset);
419         if (ret_val) {
420                 DEBUGOUT("NVM Read Error\n");
421                 return ret_val;
422         }
423
424         if ((nvm_alt_mac_addr_offset == 0xFFFF) ||
425             (nvm_alt_mac_addr_offset == 0x0000))
426                 /* There is no Alternate MAC Address */
427                 return E1000_SUCCESS;
428
429         if (hw->bus.func == E1000_FUNC_1)
430                 nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN1;
431         if (hw->bus.func == E1000_FUNC_2)
432                 nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN2;
433
434         if (hw->bus.func == E1000_FUNC_3)
435                 nvm_alt_mac_addr_offset += E1000_ALT_MAC_ADDRESS_OFFSET_LAN3;
436         for (i = 0; i < ETH_ADDR_LEN; i += 2) {
437                 offset = nvm_alt_mac_addr_offset + (i >> 1);
438                 ret_val = hw->nvm.ops.read(hw, offset, 1, &nvm_data);
439                 if (ret_val) {
440                         DEBUGOUT("NVM Read Error\n");
441                         return ret_val;
442                 }
443
444                 alt_mac_addr[i] = (u8)(nvm_data & 0xFF);
445                 alt_mac_addr[i + 1] = (u8)(nvm_data >> 8);
446         }
447
448         /* if multicast bit is set, the alternate address will not be used */
449         if (alt_mac_addr[0] & 0x01) {
450                 DEBUGOUT("Ignoring Alternate Mac Address with MC bit set\n");
451                 return E1000_SUCCESS;
452         }
453
454         /* We have a valid alternate MAC address, and we want to treat it the
455          * same as the normal permanent MAC address stored by the HW into the
456          * RAR. Do this by mapping this address into RAR0.
457          */
458         hw->mac.ops.rar_set(hw, alt_mac_addr, 0);
459
460         return E1000_SUCCESS;
461 }
462
463 /**
464  *  e1000_rar_set_generic - Set receive address register
465  *  @hw: pointer to the HW structure
466  *  @addr: pointer to the receive address
467  *  @index: receive address array register
468  *
469  *  Sets the receive address array register at index to the address passed
470  *  in by addr.
471  **/
472 STATIC void e1000_rar_set_generic(struct e1000_hw *hw, u8 *addr, u32 index)
473 {
474         u32 rar_low, rar_high;
475
476         DEBUGFUNC("e1000_rar_set_generic");
477
478         /* HW expects these in little endian so we reverse the byte order
479          * from network order (big endian) to little endian
480          */
481         rar_low = ((u32) addr[0] | ((u32) addr[1] << 8) |
482                    ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
483
484         rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
485
486         /* If MAC address zero, no need to set the AV bit */
487         if (rar_low || rar_high)
488                 rar_high |= E1000_RAH_AV;
489
490         /* Some bridges will combine consecutive 32-bit writes into
491          * a single burst write, which will malfunction on some parts.
492          * The flushes avoid this.
493          */
494         E1000_WRITE_REG(hw, E1000_RAL(index), rar_low);
495         E1000_WRITE_FLUSH(hw);
496         E1000_WRITE_REG(hw, E1000_RAH(index), rar_high);
497         E1000_WRITE_FLUSH(hw);
498 }
499
500 /**
501  *  e1000_hash_mc_addr_generic - Generate a multicast hash value
502  *  @hw: pointer to the HW structure
503  *  @mc_addr: pointer to a multicast address
504  *
505  *  Generates a multicast address hash value which is used to determine
506  *  the multicast filter table array address and new table value.
507  **/
508 u32 e1000_hash_mc_addr_generic(struct e1000_hw *hw, u8 *mc_addr)
509 {
510         u32 hash_value, hash_mask;
511         u8 bit_shift = 0;
512
513         DEBUGFUNC("e1000_hash_mc_addr_generic");
514
515         /* Register count multiplied by bits per register */
516         hash_mask = (hw->mac.mta_reg_count * 32) - 1;
517
518         /* For a mc_filter_type of 0, bit_shift is the number of left-shifts
519          * where 0xFF would still fall within the hash mask.
520          */
521         while (hash_mask >> bit_shift != 0xFF)
522                 bit_shift++;
523
524         /* The portion of the address that is used for the hash table
525          * is determined by the mc_filter_type setting.
526          * The algorithm is such that there is a total of 8 bits of shifting.
527          * The bit_shift for a mc_filter_type of 0 represents the number of
528          * left-shifts where the MSB of mc_addr[5] would still fall within
529          * the hash_mask.  Case 0 does this exactly.  Since there are a total
530          * of 8 bits of shifting, then mc_addr[4] will shift right the
531          * remaining number of bits. Thus 8 - bit_shift.  The rest of the
532          * cases are a variation of this algorithm...essentially raising the
533          * number of bits to shift mc_addr[5] left, while still keeping the
534          * 8-bit shifting total.
535          *
536          * For example, given the following Destination MAC Address and an
537          * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
538          * we can see that the bit_shift for case 0 is 4.  These are the hash
539          * values resulting from each mc_filter_type...
540          * [0] [1] [2] [3] [4] [5]
541          * 01  AA  00  12  34  56
542          * LSB           MSB
543          *
544          * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
545          * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
546          * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
547          * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
548          */
549         switch (hw->mac.mc_filter_type) {
550         default:
551         case 0:
552                 break;
553         case 1:
554                 bit_shift += 1;
555                 break;
556         case 2:
557                 bit_shift += 2;
558                 break;
559         case 3:
560                 bit_shift += 4;
561                 break;
562         }
563
564         hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
565                                   (((u16) mc_addr[5]) << bit_shift)));
566
567         return hash_value;
568 }
569
570 /**
571  *  e1000_update_mc_addr_list_generic - Update Multicast addresses
572  *  @hw: pointer to the HW structure
573  *  @mc_addr_list: array of multicast addresses to program
574  *  @mc_addr_count: number of multicast addresses to program
575  *
576  *  Updates entire Multicast Table Array.
577  *  The caller must have a packed mc_addr_list of multicast addresses.
578  **/
579 void e1000_update_mc_addr_list_generic(struct e1000_hw *hw,
580                                        u8 *mc_addr_list, u32 mc_addr_count)
581 {
582         u32 hash_value, hash_bit, hash_reg;
583         int i;
584
585         DEBUGFUNC("e1000_update_mc_addr_list_generic");
586
587         /* clear mta_shadow */
588         memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
589
590         /* update mta_shadow from mc_addr_list */
591         for (i = 0; (u32) i < mc_addr_count; i++) {
592                 hash_value = e1000_hash_mc_addr_generic(hw, mc_addr_list);
593
594                 hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
595                 hash_bit = hash_value & 0x1F;
596
597                 hw->mac.mta_shadow[hash_reg] |= (1 << hash_bit);
598                 mc_addr_list += (ETH_ADDR_LEN);
599         }
600
601         /* replace the entire MTA table */
602         for (i = hw->mac.mta_reg_count - 1; i >= 0; i--)
603                 E1000_WRITE_REG_ARRAY(hw, E1000_MTA, i, hw->mac.mta_shadow[i]);
604         E1000_WRITE_FLUSH(hw);
605 }
606
607 /**
608  *  e1000_pcix_mmrbc_workaround_generic - Fix incorrect MMRBC value
609  *  @hw: pointer to the HW structure
610  *
611  *  In certain situations, a system BIOS may report that the PCIx maximum
612  *  memory read byte count (MMRBC) value is higher than than the actual
613  *  value. We check the PCIx command register with the current PCIx status
614  *  register.
615  **/
616 void e1000_pcix_mmrbc_workaround_generic(struct e1000_hw *hw)
617 {
618         u16 cmd_mmrbc;
619         u16 pcix_cmd;
620         u16 pcix_stat_hi_word;
621         u16 stat_mmrbc;
622
623         DEBUGFUNC("e1000_pcix_mmrbc_workaround_generic");
624
625         /* Workaround for PCI-X issue when BIOS sets MMRBC incorrectly */
626         if (hw->bus.type != e1000_bus_type_pcix)
627                 return;
628
629         e1000_read_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd);
630         e1000_read_pci_cfg(hw, PCIX_STATUS_REGISTER_HI, &pcix_stat_hi_word);
631         cmd_mmrbc = (pcix_cmd & PCIX_COMMAND_MMRBC_MASK) >>
632                      PCIX_COMMAND_MMRBC_SHIFT;
633         stat_mmrbc = (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
634                       PCIX_STATUS_HI_MMRBC_SHIFT;
635         if (stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
636                 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
637         if (cmd_mmrbc > stat_mmrbc) {
638                 pcix_cmd &= ~PCIX_COMMAND_MMRBC_MASK;
639                 pcix_cmd |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
640                 e1000_write_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd);
641         }
642 }
643
644 /**
645  *  e1000_clear_hw_cntrs_base_generic - Clear base hardware counters
646  *  @hw: pointer to the HW structure
647  *
648  *  Clears the base hardware counters by reading the counter registers.
649  **/
650 void e1000_clear_hw_cntrs_base_generic(struct e1000_hw *hw)
651 {
652         DEBUGFUNC("e1000_clear_hw_cntrs_base_generic");
653
654         E1000_READ_REG(hw, E1000_CRCERRS);
655         E1000_READ_REG(hw, E1000_SYMERRS);
656         E1000_READ_REG(hw, E1000_MPC);
657         E1000_READ_REG(hw, E1000_SCC);
658         E1000_READ_REG(hw, E1000_ECOL);
659         E1000_READ_REG(hw, E1000_MCC);
660         E1000_READ_REG(hw, E1000_LATECOL);
661         E1000_READ_REG(hw, E1000_COLC);
662         E1000_READ_REG(hw, E1000_DC);
663         E1000_READ_REG(hw, E1000_SEC);
664         E1000_READ_REG(hw, E1000_RLEC);
665         E1000_READ_REG(hw, E1000_XONRXC);
666         E1000_READ_REG(hw, E1000_XONTXC);
667         E1000_READ_REG(hw, E1000_XOFFRXC);
668         E1000_READ_REG(hw, E1000_XOFFTXC);
669         E1000_READ_REG(hw, E1000_FCRUC);
670         E1000_READ_REG(hw, E1000_GPRC);
671         E1000_READ_REG(hw, E1000_BPRC);
672         E1000_READ_REG(hw, E1000_MPRC);
673         E1000_READ_REG(hw, E1000_GPTC);
674         E1000_READ_REG(hw, E1000_GORCL);
675         E1000_READ_REG(hw, E1000_GORCH);
676         E1000_READ_REG(hw, E1000_GOTCL);
677         E1000_READ_REG(hw, E1000_GOTCH);
678         E1000_READ_REG(hw, E1000_RNBC);
679         E1000_READ_REG(hw, E1000_RUC);
680         E1000_READ_REG(hw, E1000_RFC);
681         E1000_READ_REG(hw, E1000_ROC);
682         E1000_READ_REG(hw, E1000_RJC);
683         E1000_READ_REG(hw, E1000_TORL);
684         E1000_READ_REG(hw, E1000_TORH);
685         E1000_READ_REG(hw, E1000_TOTL);
686         E1000_READ_REG(hw, E1000_TOTH);
687         E1000_READ_REG(hw, E1000_TPR);
688         E1000_READ_REG(hw, E1000_TPT);
689         E1000_READ_REG(hw, E1000_MPTC);
690         E1000_READ_REG(hw, E1000_BPTC);
691 }
692
693 /**
694  *  e1000_check_for_copper_link_generic - Check for link (Copper)
695  *  @hw: pointer to the HW structure
696  *
697  *  Checks to see of the link status of the hardware has changed.  If a
698  *  change in link status has been detected, then we read the PHY registers
699  *  to get the current speed/duplex if link exists.
700  **/
701 s32 e1000_check_for_copper_link_generic(struct e1000_hw *hw)
702 {
703         struct e1000_mac_info *mac = &hw->mac;
704         s32 ret_val;
705         bool link;
706
707         DEBUGFUNC("e1000_check_for_copper_link");
708
709         /* We only want to go out to the PHY registers to see if Auto-Neg
710          * has completed and/or if our link status has changed.  The
711          * get_link_status flag is set upon receiving a Link Status
712          * Change or Rx Sequence Error interrupt.
713          */
714         if (!mac->get_link_status)
715                 return E1000_SUCCESS;
716
717         /* First we want to see if the MII Status Register reports
718          * link.  If so, then we want to get the current speed/duplex
719          * of the PHY.
720          */
721         ret_val = e1000_phy_has_link_generic(hw, 1, 0, &link);
722         if (ret_val)
723                 return ret_val;
724
725         if (!link)
726                 return E1000_SUCCESS; /* No link detected */
727
728         mac->get_link_status = false;
729
730         /* Check if there was DownShift, must be checked
731          * immediately after link-up
732          */
733         e1000_check_downshift_generic(hw);
734
735         /* If we are forcing speed/duplex, then we simply return since
736          * we have already determined whether we have link or not.
737          */
738         if (!mac->autoneg)
739                 return -E1000_ERR_CONFIG;
740
741         /* Auto-Neg is enabled.  Auto Speed Detection takes care
742          * of MAC speed/duplex configuration.  So we only need to
743          * configure Collision Distance in the MAC.
744          */
745         mac->ops.config_collision_dist(hw);
746
747         /* Configure Flow Control now that Auto-Neg has completed.
748          * First, we need to restore the desired flow control
749          * settings because we may have had to re-autoneg with a
750          * different link partner.
751          */
752         ret_val = e1000_config_fc_after_link_up_generic(hw);
753         if (ret_val)
754                 DEBUGOUT("Error configuring flow control\n");
755
756         return ret_val;
757 }
758
759 /**
760  *  e1000_check_for_fiber_link_generic - Check for link (Fiber)
761  *  @hw: pointer to the HW structure
762  *
763  *  Checks for link up on the hardware.  If link is not up and we have
764  *  a signal, then we need to force link up.
765  **/
766 s32 e1000_check_for_fiber_link_generic(struct e1000_hw *hw)
767 {
768         struct e1000_mac_info *mac = &hw->mac;
769         u32 rxcw;
770         u32 ctrl;
771         u32 status;
772         s32 ret_val;
773
774         DEBUGFUNC("e1000_check_for_fiber_link_generic");
775
776         ctrl = E1000_READ_REG(hw, E1000_CTRL);
777         status = E1000_READ_REG(hw, E1000_STATUS);
778         rxcw = E1000_READ_REG(hw, E1000_RXCW);
779
780         /* If we don't have link (auto-negotiation failed or link partner
781          * cannot auto-negotiate), the cable is plugged in (we have signal),
782          * and our link partner is not trying to auto-negotiate with us (we
783          * are receiving idles or data), we need to force link up. We also
784          * need to give auto-negotiation time to complete, in case the cable
785          * was just plugged in. The autoneg_failed flag does this.
786          */
787         /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
788         if ((ctrl & E1000_CTRL_SWDPIN1) && !(status & E1000_STATUS_LU) &&
789             !(rxcw & E1000_RXCW_C)) {
790                 if (!mac->autoneg_failed) {
791                         mac->autoneg_failed = true;
792                         return E1000_SUCCESS;
793                 }
794                 DEBUGOUT("NOT Rx'ing /C/, disable AutoNeg and force link.\n");
795
796                 /* Disable auto-negotiation in the TXCW register */
797                 E1000_WRITE_REG(hw, E1000_TXCW, (mac->txcw & ~E1000_TXCW_ANE));
798
799                 /* Force link-up and also force full-duplex. */
800                 ctrl = E1000_READ_REG(hw, E1000_CTRL);
801                 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
802                 E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
803
804                 /* Configure Flow Control after forcing link up. */
805                 ret_val = e1000_config_fc_after_link_up_generic(hw);
806                 if (ret_val) {
807                         DEBUGOUT("Error configuring flow control\n");
808                         return ret_val;
809                 }
810         } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
811                 /* If we are forcing link and we are receiving /C/ ordered
812                  * sets, re-enable auto-negotiation in the TXCW register
813                  * and disable forced link in the Device Control register
814                  * in an attempt to auto-negotiate with our link partner.
815                  */
816                 DEBUGOUT("Rx'ing /C/, enable AutoNeg and stop forcing link.\n");
817                 E1000_WRITE_REG(hw, E1000_TXCW, mac->txcw);
818                 E1000_WRITE_REG(hw, E1000_CTRL, (ctrl & ~E1000_CTRL_SLU));
819
820                 mac->serdes_has_link = true;
821         }
822
823         return E1000_SUCCESS;
824 }
825
826 /**
827  *  e1000_check_for_serdes_link_generic - Check for link (Serdes)
828  *  @hw: pointer to the HW structure
829  *
830  *  Checks for link up on the hardware.  If link is not up and we have
831  *  a signal, then we need to force link up.
832  **/
833 s32 e1000_check_for_serdes_link_generic(struct e1000_hw *hw)
834 {
835         struct e1000_mac_info *mac = &hw->mac;
836         u32 rxcw;
837         u32 ctrl;
838         u32 status;
839         s32 ret_val;
840
841         DEBUGFUNC("e1000_check_for_serdes_link_generic");
842
843         ctrl = E1000_READ_REG(hw, E1000_CTRL);
844         status = E1000_READ_REG(hw, E1000_STATUS);
845         rxcw = E1000_READ_REG(hw, E1000_RXCW);
846
847         /* If we don't have link (auto-negotiation failed or link partner
848          * cannot auto-negotiate), and our link partner is not trying to
849          * auto-negotiate with us (we are receiving idles or data),
850          * we need to force link up. We also need to give auto-negotiation
851          * time to complete.
852          */
853         /* (ctrl & E1000_CTRL_SWDPIN1) == 1 == have signal */
854         if (!(status & E1000_STATUS_LU) && !(rxcw & E1000_RXCW_C)) {
855                 if (!mac->autoneg_failed) {
856                         mac->autoneg_failed = true;
857                         return E1000_SUCCESS;
858                 }
859                 DEBUGOUT("NOT Rx'ing /C/, disable AutoNeg and force link.\n");
860
861                 /* Disable auto-negotiation in the TXCW register */
862                 E1000_WRITE_REG(hw, E1000_TXCW, (mac->txcw & ~E1000_TXCW_ANE));
863
864                 /* Force link-up and also force full-duplex. */
865                 ctrl = E1000_READ_REG(hw, E1000_CTRL);
866                 ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
867                 E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
868
869                 /* Configure Flow Control after forcing link up. */
870                 ret_val = e1000_config_fc_after_link_up_generic(hw);
871                 if (ret_val) {
872                         DEBUGOUT("Error configuring flow control\n");
873                         return ret_val;
874                 }
875         } else if ((ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
876                 /* If we are forcing link and we are receiving /C/ ordered
877                  * sets, re-enable auto-negotiation in the TXCW register
878                  * and disable forced link in the Device Control register
879                  * in an attempt to auto-negotiate with our link partner.
880                  */
881                 DEBUGOUT("Rx'ing /C/, enable AutoNeg and stop forcing link.\n");
882                 E1000_WRITE_REG(hw, E1000_TXCW, mac->txcw);
883                 E1000_WRITE_REG(hw, E1000_CTRL, (ctrl & ~E1000_CTRL_SLU));
884
885                 mac->serdes_has_link = true;
886         } else if (!(E1000_TXCW_ANE & E1000_READ_REG(hw, E1000_TXCW))) {
887                 /* If we force link for non-auto-negotiation switch, check
888                  * link status based on MAC synchronization for internal
889                  * serdes media type.
890                  */
891                 /* SYNCH bit and IV bit are sticky. */
892                 usec_delay(10);
893                 rxcw = E1000_READ_REG(hw, E1000_RXCW);
894                 if (rxcw & E1000_RXCW_SYNCH) {
895                         if (!(rxcw & E1000_RXCW_IV)) {
896                                 mac->serdes_has_link = true;
897                                 DEBUGOUT("SERDES: Link up - forced.\n");
898                         }
899                 } else {
900                         mac->serdes_has_link = false;
901                         DEBUGOUT("SERDES: Link down - force failed.\n");
902                 }
903         }
904
905         if (E1000_TXCW_ANE & E1000_READ_REG(hw, E1000_TXCW)) {
906                 status = E1000_READ_REG(hw, E1000_STATUS);
907                 if (status & E1000_STATUS_LU) {
908                         /* SYNCH bit and IV bit are sticky, so reread rxcw. */
909                         usec_delay(10);
910                         rxcw = E1000_READ_REG(hw, E1000_RXCW);
911                         if (rxcw & E1000_RXCW_SYNCH) {
912                                 if (!(rxcw & E1000_RXCW_IV)) {
913                                         mac->serdes_has_link = true;
914                                         DEBUGOUT("SERDES: Link up - autoneg completed successfully.\n");
915                                 } else {
916                                         mac->serdes_has_link = false;
917                                         DEBUGOUT("SERDES: Link down - invalid codewords detected in autoneg.\n");
918                                 }
919                         } else {
920                                 mac->serdes_has_link = false;
921                                 DEBUGOUT("SERDES: Link down - no sync.\n");
922                         }
923                 } else {
924                         mac->serdes_has_link = false;
925                         DEBUGOUT("SERDES: Link down - autoneg failed\n");
926                 }
927         }
928
929         return E1000_SUCCESS;
930 }
931
932 /**
933  *  e1000_set_default_fc_generic - Set flow control default values
934  *  @hw: pointer to the HW structure
935  *
936  *  Read the EEPROM for the default values for flow control and store the
937  *  values.
938  **/
939 s32 e1000_set_default_fc_generic(struct e1000_hw *hw)
940 {
941         s32 ret_val;
942         u16 nvm_data;
943         u16 nvm_offset = 0;
944
945         DEBUGFUNC("e1000_set_default_fc_generic");
946
947         /* Read and store word 0x0F of the EEPROM. This word contains bits
948          * that determine the hardware's default PAUSE (flow control) mode,
949          * a bit that determines whether the HW defaults to enabling or
950          * disabling auto-negotiation, and the direction of the
951          * SW defined pins. If there is no SW over-ride of the flow
952          * control setting, then the variable hw->fc will
953          * be initialized based on a value in the EEPROM.
954          */
955         if (hw->mac.type == e1000_i350) {
956                 nvm_offset = NVM_82580_LAN_FUNC_OFFSET(hw->bus.func);
957                 ret_val = hw->nvm.ops.read(hw,
958                                            NVM_INIT_CONTROL2_REG +
959                                            nvm_offset,
960                                            1, &nvm_data);
961         } else {
962                 ret_val = hw->nvm.ops.read(hw,
963                                            NVM_INIT_CONTROL2_REG,
964                                            1, &nvm_data);
965         }
966
967
968         if (ret_val) {
969                 DEBUGOUT("NVM Read Error\n");
970                 return ret_val;
971         }
972
973         if (!(nvm_data & NVM_WORD0F_PAUSE_MASK))
974                 hw->fc.requested_mode = e1000_fc_none;
975         else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) ==
976                  NVM_WORD0F_ASM_DIR)
977                 hw->fc.requested_mode = e1000_fc_tx_pause;
978         else
979                 hw->fc.requested_mode = e1000_fc_full;
980
981         return E1000_SUCCESS;
982 }
983
984 /**
985  *  e1000_setup_link_generic - Setup flow control and link settings
986  *  @hw: pointer to the HW structure
987  *
988  *  Determines which flow control settings to use, then configures flow
989  *  control.  Calls the appropriate media-specific link configuration
990  *  function.  Assuming the adapter has a valid link partner, a valid link
991  *  should be established.  Assumes the hardware has previously been reset
992  *  and the transmitter and receiver are not enabled.
993  **/
994 s32 e1000_setup_link_generic(struct e1000_hw *hw)
995 {
996         s32 ret_val;
997
998         DEBUGFUNC("e1000_setup_link_generic");
999
1000         /* In the case of the phy reset being blocked, we already have a link.
1001          * We do not need to set it up again.
1002          */
1003         if (hw->phy.ops.check_reset_block && hw->phy.ops.check_reset_block(hw))
1004                 return E1000_SUCCESS;
1005
1006         /* If requested flow control is set to default, set flow control
1007          * based on the EEPROM flow control settings.
1008          */
1009         if (hw->fc.requested_mode == e1000_fc_default) {
1010                 ret_val = e1000_set_default_fc_generic(hw);
1011                 if (ret_val)
1012                         return ret_val;
1013         }
1014
1015         /* Save off the requested flow control mode for use later.  Depending
1016          * on the link partner's capabilities, we may or may not use this mode.
1017          */
1018         hw->fc.current_mode = hw->fc.requested_mode;
1019
1020         DEBUGOUT1("After fix-ups FlowControl is now = %x\n",
1021                 hw->fc.current_mode);
1022
1023         /* Call the necessary media_type subroutine to configure the link. */
1024         ret_val = hw->mac.ops.setup_physical_interface(hw);
1025         if (ret_val)
1026                 return ret_val;
1027
1028         /* Initialize the flow control address, type, and PAUSE timer
1029          * registers to their default values.  This is done even if flow
1030          * control is disabled, because it does not hurt anything to
1031          * initialize these registers.
1032          */
1033         DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
1034         E1000_WRITE_REG(hw, E1000_FCT, FLOW_CONTROL_TYPE);
1035         E1000_WRITE_REG(hw, E1000_FCAH, FLOW_CONTROL_ADDRESS_HIGH);
1036         E1000_WRITE_REG(hw, E1000_FCAL, FLOW_CONTROL_ADDRESS_LOW);
1037
1038         E1000_WRITE_REG(hw, E1000_FCTTV, hw->fc.pause_time);
1039
1040         return e1000_set_fc_watermarks_generic(hw);
1041 }
1042
1043 /**
1044  *  e1000_commit_fc_settings_generic - Configure flow control
1045  *  @hw: pointer to the HW structure
1046  *
1047  *  Write the flow control settings to the Transmit Config Word Register (TXCW)
1048  *  base on the flow control settings in e1000_mac_info.
1049  **/
1050 s32 e1000_commit_fc_settings_generic(struct e1000_hw *hw)
1051 {
1052         struct e1000_mac_info *mac = &hw->mac;
1053         u32 txcw;
1054
1055         DEBUGFUNC("e1000_commit_fc_settings_generic");
1056
1057         /* Check for a software override of the flow control settings, and
1058          * setup the device accordingly.  If auto-negotiation is enabled, then
1059          * software will have to set the "PAUSE" bits to the correct value in
1060          * the Transmit Config Word Register (TXCW) and re-start auto-
1061          * negotiation.  However, if auto-negotiation is disabled, then
1062          * software will have to manually configure the two flow control enable
1063          * bits in the CTRL register.
1064          *
1065          * The possible values of the "fc" parameter are:
1066          *      0:  Flow control is completely disabled
1067          *      1:  Rx flow control is enabled (we can receive pause frames,
1068          *          but not send pause frames).
1069          *      2:  Tx flow control is enabled (we can send pause frames but we
1070          *          do not support receiving pause frames).
1071          *      3:  Both Rx and Tx flow control (symmetric) are enabled.
1072          */
1073         switch (hw->fc.current_mode) {
1074         case e1000_fc_none:
1075                 /* Flow control completely disabled by a software over-ride. */
1076                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
1077                 break;
1078         case e1000_fc_rx_pause:
1079                 /* Rx Flow control is enabled and Tx Flow control is disabled
1080                  * by a software over-ride. Since there really isn't a way to
1081                  * advertise that we are capable of Rx Pause ONLY, we will
1082                  * advertise that we support both symmetric and asymmetric Rx
1083                  * PAUSE.  Later, we will disable the adapter's ability to send
1084                  * PAUSE frames.
1085                  */
1086                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
1087                 break;
1088         case e1000_fc_tx_pause:
1089                 /* Tx Flow control is enabled, and Rx Flow control is disabled,
1090                  * by a software over-ride.
1091                  */
1092                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
1093                 break;
1094         case e1000_fc_full:
1095                 /* Flow control (both Rx and Tx) is enabled by a software
1096                  * over-ride.
1097                  */
1098                 txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
1099                 break;
1100         default:
1101                 DEBUGOUT("Flow control param set incorrectly\n");
1102                 return -E1000_ERR_CONFIG;
1103                 break;
1104         }
1105
1106         E1000_WRITE_REG(hw, E1000_TXCW, txcw);
1107         mac->txcw = txcw;
1108
1109         return E1000_SUCCESS;
1110 }
1111
1112 /**
1113  *  e1000_poll_fiber_serdes_link_generic - Poll for link up
1114  *  @hw: pointer to the HW structure
1115  *
1116  *  Polls for link up by reading the status register, if link fails to come
1117  *  up with auto-negotiation, then the link is forced if a signal is detected.
1118  **/
1119 s32 e1000_poll_fiber_serdes_link_generic(struct e1000_hw *hw)
1120 {
1121         struct e1000_mac_info *mac = &hw->mac;
1122         u32 i, status;
1123         s32 ret_val;
1124
1125         DEBUGFUNC("e1000_poll_fiber_serdes_link_generic");
1126
1127         /* If we have a signal (the cable is plugged in, or assumed true for
1128          * serdes media) then poll for a "Link-Up" indication in the Device
1129          * Status Register.  Time-out if a link isn't seen in 500 milliseconds
1130          * seconds (Auto-negotiation should complete in less than 500
1131          * milliseconds even if the other end is doing it in SW).
1132          */
1133         for (i = 0; i < FIBER_LINK_UP_LIMIT; i++) {
1134                 msec_delay(10);
1135                 status = E1000_READ_REG(hw, E1000_STATUS);
1136                 if (status & E1000_STATUS_LU)
1137                         break;
1138         }
1139         if (i == FIBER_LINK_UP_LIMIT) {
1140                 DEBUGOUT("Never got a valid link from auto-neg!!!\n");
1141                 mac->autoneg_failed = true;
1142                 /* AutoNeg failed to achieve a link, so we'll call
1143                  * mac->check_for_link. This routine will force the
1144                  * link up if we detect a signal. This will allow us to
1145                  * communicate with non-autonegotiating link partners.
1146                  */
1147                 ret_val = mac->ops.check_for_link(hw);
1148                 if (ret_val) {
1149                         DEBUGOUT("Error while checking for link\n");
1150                         return ret_val;
1151                 }
1152                 mac->autoneg_failed = false;
1153         } else {
1154                 mac->autoneg_failed = false;
1155                 DEBUGOUT("Valid Link Found\n");
1156         }
1157
1158         return E1000_SUCCESS;
1159 }
1160
1161 /**
1162  *  e1000_setup_fiber_serdes_link_generic - Setup link for fiber/serdes
1163  *  @hw: pointer to the HW structure
1164  *
1165  *  Configures collision distance and flow control for fiber and serdes
1166  *  links.  Upon successful setup, poll for link.
1167  **/
1168 s32 e1000_setup_fiber_serdes_link_generic(struct e1000_hw *hw)
1169 {
1170         u32 ctrl;
1171         s32 ret_val;
1172
1173         DEBUGFUNC("e1000_setup_fiber_serdes_link_generic");
1174
1175         ctrl = E1000_READ_REG(hw, E1000_CTRL);
1176
1177         /* Take the link out of reset */
1178         ctrl &= ~E1000_CTRL_LRST;
1179
1180         hw->mac.ops.config_collision_dist(hw);
1181
1182         ret_val = e1000_commit_fc_settings_generic(hw);
1183         if (ret_val)
1184                 return ret_val;
1185
1186         /* Since auto-negotiation is enabled, take the link out of reset (the
1187          * link will be in reset, because we previously reset the chip). This
1188          * will restart auto-negotiation.  If auto-negotiation is successful
1189          * then the link-up status bit will be set and the flow control enable
1190          * bits (RFCE and TFCE) will be set according to their negotiated value.
1191          */
1192         DEBUGOUT("Auto-negotiation enabled\n");
1193
1194         E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
1195         E1000_WRITE_FLUSH(hw);
1196         msec_delay(1);
1197
1198         /* For these adapters, the SW definable pin 1 is set when the optics
1199          * detect a signal.  If we have a signal, then poll for a "Link-Up"
1200          * indication.
1201          */
1202         if (hw->phy.media_type == e1000_media_type_internal_serdes ||
1203             (E1000_READ_REG(hw, E1000_CTRL) & E1000_CTRL_SWDPIN1)) {
1204                 ret_val = e1000_poll_fiber_serdes_link_generic(hw);
1205         } else {
1206                 DEBUGOUT("No signal detected\n");
1207         }
1208
1209         return ret_val;
1210 }
1211
1212 /**
1213  *  e1000_config_collision_dist_generic - Configure collision distance
1214  *  @hw: pointer to the HW structure
1215  *
1216  *  Configures the collision distance to the default value and is used
1217  *  during link setup.
1218  **/
1219 STATIC void e1000_config_collision_dist_generic(struct e1000_hw *hw)
1220 {
1221         u32 tctl;
1222
1223         DEBUGFUNC("e1000_config_collision_dist_generic");
1224
1225         tctl = E1000_READ_REG(hw, E1000_TCTL);
1226
1227         tctl &= ~E1000_TCTL_COLD;
1228         tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1229
1230         E1000_WRITE_REG(hw, E1000_TCTL, tctl);
1231         E1000_WRITE_FLUSH(hw);
1232 }
1233
1234 /**
1235  *  e1000_set_fc_watermarks_generic - Set flow control high/low watermarks
1236  *  @hw: pointer to the HW structure
1237  *
1238  *  Sets the flow control high/low threshold (watermark) registers.  If
1239  *  flow control XON frame transmission is enabled, then set XON frame
1240  *  transmission as well.
1241  **/
1242 s32 e1000_set_fc_watermarks_generic(struct e1000_hw *hw)
1243 {
1244         u32 fcrtl = 0, fcrth = 0;
1245
1246         DEBUGFUNC("e1000_set_fc_watermarks_generic");
1247
1248         /* Set the flow control receive threshold registers.  Normally,
1249          * these registers will be set to a default threshold that may be
1250          * adjusted later by the driver's runtime code.  However, if the
1251          * ability to transmit pause frames is not enabled, then these
1252          * registers will be set to 0.
1253          */
1254         if (hw->fc.current_mode & e1000_fc_tx_pause) {
1255                 /* We need to set up the Receive Threshold high and low water
1256                  * marks as well as (optionally) enabling the transmission of
1257                  * XON frames.
1258                  */
1259                 fcrtl = hw->fc.low_water;
1260                 if (hw->fc.send_xon)
1261                         fcrtl |= E1000_FCRTL_XONE;
1262
1263                 fcrth = hw->fc.high_water;
1264         }
1265         E1000_WRITE_REG(hw, E1000_FCRTL, fcrtl);
1266         E1000_WRITE_REG(hw, E1000_FCRTH, fcrth);
1267
1268         return E1000_SUCCESS;
1269 }
1270
1271 /**
1272  *  e1000_force_mac_fc_generic - Force the MAC's flow control settings
1273  *  @hw: pointer to the HW structure
1274  *
1275  *  Force the MAC's flow control settings.  Sets the TFCE and RFCE bits in the
1276  *  device control register to reflect the adapter settings.  TFCE and RFCE
1277  *  need to be explicitly set by software when a copper PHY is used because
1278  *  autonegotiation is managed by the PHY rather than the MAC.  Software must
1279  *  also configure these bits when link is forced on a fiber connection.
1280  **/
1281 s32 e1000_force_mac_fc_generic(struct e1000_hw *hw)
1282 {
1283         u32 ctrl;
1284
1285         DEBUGFUNC("e1000_force_mac_fc_generic");
1286
1287         ctrl = E1000_READ_REG(hw, E1000_CTRL);
1288
1289         /* Because we didn't get link via the internal auto-negotiation
1290          * mechanism (we either forced link or we got link via PHY
1291          * auto-neg), we have to manually enable/disable transmit an
1292          * receive flow control.
1293          *
1294          * The "Case" statement below enables/disable flow control
1295          * according to the "hw->fc.current_mode" parameter.
1296          *
1297          * The possible values of the "fc" parameter are:
1298          *      0:  Flow control is completely disabled
1299          *      1:  Rx flow control is enabled (we can receive pause
1300          *          frames but not send pause frames).
1301          *      2:  Tx flow control is enabled (we can send pause frames
1302          *          frames but we do not receive pause frames).
1303          *      3:  Both Rx and Tx flow control (symmetric) is enabled.
1304          *  other:  No other values should be possible at this point.
1305          */
1306         DEBUGOUT1("hw->fc.current_mode = %u\n", hw->fc.current_mode);
1307
1308         switch (hw->fc.current_mode) {
1309         case e1000_fc_none:
1310                 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
1311                 break;
1312         case e1000_fc_rx_pause:
1313                 ctrl &= (~E1000_CTRL_TFCE);
1314                 ctrl |= E1000_CTRL_RFCE;
1315                 break;
1316         case e1000_fc_tx_pause:
1317                 ctrl &= (~E1000_CTRL_RFCE);
1318                 ctrl |= E1000_CTRL_TFCE;
1319                 break;
1320         case e1000_fc_full:
1321                 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
1322                 break;
1323         default:
1324                 DEBUGOUT("Flow control param set incorrectly\n");
1325                 return -E1000_ERR_CONFIG;
1326         }
1327
1328         E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
1329
1330         return E1000_SUCCESS;
1331 }
1332
1333 /**
1334  *  e1000_config_fc_after_link_up_generic - Configures flow control after link
1335  *  @hw: pointer to the HW structure
1336  *
1337  *  Checks the status of auto-negotiation after link up to ensure that the
1338  *  speed and duplex were not forced.  If the link needed to be forced, then
1339  *  flow control needs to be forced also.  If auto-negotiation is enabled
1340  *  and did not fail, then we configure flow control based on our link
1341  *  partner.
1342  **/
1343 s32 e1000_config_fc_after_link_up_generic(struct e1000_hw *hw)
1344 {
1345         struct e1000_mac_info *mac = &hw->mac;
1346         s32 ret_val = E1000_SUCCESS;
1347         u32 pcs_status_reg, pcs_adv_reg, pcs_lp_ability_reg, pcs_ctrl_reg;
1348         u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
1349         u16 speed, duplex;
1350
1351         DEBUGFUNC("e1000_config_fc_after_link_up_generic");
1352
1353         /* Check for the case where we have fiber media and auto-neg failed
1354          * so we had to force link.  In this case, we need to force the
1355          * configuration of the MAC to match the "fc" parameter.
1356          */
1357         if (mac->autoneg_failed) {
1358                 if (hw->phy.media_type == e1000_media_type_fiber ||
1359                     hw->phy.media_type == e1000_media_type_internal_serdes)
1360                         ret_val = e1000_force_mac_fc_generic(hw);
1361         } else {
1362                 if (hw->phy.media_type == e1000_media_type_copper)
1363                         ret_val = e1000_force_mac_fc_generic(hw);
1364         }
1365
1366         if (ret_val) {
1367                 DEBUGOUT("Error forcing flow control settings\n");
1368                 return ret_val;
1369         }
1370
1371         /* Check for the case where we have copper media and auto-neg is
1372          * enabled.  In this case, we need to check and see if Auto-Neg
1373          * has completed, and if so, how the PHY and link partner has
1374          * flow control configured.
1375          */
1376         if ((hw->phy.media_type == e1000_media_type_copper) && mac->autoneg) {
1377                 /* Read the MII Status Register and check to see if AutoNeg
1378                  * has completed.  We read this twice because this reg has
1379                  * some "sticky" (latched) bits.
1380                  */
1381                 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &mii_status_reg);
1382                 if (ret_val)
1383                         return ret_val;
1384                 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS, &mii_status_reg);
1385                 if (ret_val)
1386                         return ret_val;
1387
1388                 if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
1389                         DEBUGOUT("Copper PHY and Auto Neg has not completed.\n");
1390                         return ret_val;
1391                 }
1392
1393                 /* The AutoNeg process has completed, so we now need to
1394                  * read both the Auto Negotiation Advertisement
1395                  * Register (Address 4) and the Auto_Negotiation Base
1396                  * Page Ability Register (Address 5) to determine how
1397                  * flow control was negotiated.
1398                  */
1399                 ret_val = hw->phy.ops.read_reg(hw, PHY_AUTONEG_ADV,
1400                                                &mii_nway_adv_reg);
1401                 if (ret_val)
1402                         return ret_val;
1403                 ret_val = hw->phy.ops.read_reg(hw, PHY_LP_ABILITY,
1404                                                &mii_nway_lp_ability_reg);
1405                 if (ret_val)
1406                         return ret_val;
1407
1408                 /* Two bits in the Auto Negotiation Advertisement Register
1409                  * (Address 4) and two bits in the Auto Negotiation Base
1410                  * Page Ability Register (Address 5) determine flow control
1411                  * for both the PHY and the link partner.  The following
1412                  * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
1413                  * 1999, describes these PAUSE resolution bits and how flow
1414                  * control is determined based upon these settings.
1415                  * NOTE:  DC = Don't Care
1416                  *
1417                  *   LOCAL DEVICE  |   LINK PARTNER
1418                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
1419                  *-------|---------|-------|---------|--------------------
1420                  *   0   |    0    |  DC   |   DC    | e1000_fc_none
1421                  *   0   |    1    |   0   |   DC    | e1000_fc_none
1422                  *   0   |    1    |   1   |    0    | e1000_fc_none
1423                  *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1424                  *   1   |    0    |   0   |   DC    | e1000_fc_none
1425                  *   1   |   DC    |   1   |   DC    | e1000_fc_full
1426                  *   1   |    1    |   0   |    0    | e1000_fc_none
1427                  *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1428                  *
1429                  * Are both PAUSE bits set to 1?  If so, this implies
1430                  * Symmetric Flow Control is enabled at both ends.  The
1431                  * ASM_DIR bits are irrelevant per the spec.
1432                  *
1433                  * For Symmetric Flow Control:
1434                  *
1435                  *   LOCAL DEVICE  |   LINK PARTNER
1436                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1437                  *-------|---------|-------|---------|--------------------
1438                  *   1   |   DC    |   1   |   DC    | E1000_fc_full
1439                  *
1440                  */
1441                 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1442                     (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
1443                         /* Now we need to check if the user selected Rx ONLY
1444                          * of pause frames.  In this case, we had to advertise
1445                          * FULL flow control because we could not advertise Rx
1446                          * ONLY. Hence, we must now check to see if we need to
1447                          * turn OFF the TRANSMISSION of PAUSE frames.
1448                          */
1449                         if (hw->fc.requested_mode == e1000_fc_full) {
1450                                 hw->fc.current_mode = e1000_fc_full;
1451                                 DEBUGOUT("Flow Control = FULL.\n");
1452                         } else {
1453                                 hw->fc.current_mode = e1000_fc_rx_pause;
1454                                 DEBUGOUT("Flow Control = Rx PAUSE frames only.\n");
1455                         }
1456                 }
1457                 /* For receiving PAUSE frames ONLY.
1458                  *
1459                  *   LOCAL DEVICE  |   LINK PARTNER
1460                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1461                  *-------|---------|-------|---------|--------------------
1462                  *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1463                  */
1464                 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1465                           (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1466                           (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1467                           (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1468                         hw->fc.current_mode = e1000_fc_tx_pause;
1469                         DEBUGOUT("Flow Control = Tx PAUSE frames only.\n");
1470                 }
1471                 /* For transmitting PAUSE frames ONLY.
1472                  *
1473                  *   LOCAL DEVICE  |   LINK PARTNER
1474                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1475                  *-------|---------|-------|---------|--------------------
1476                  *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1477                  */
1478                 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1479                          (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1480                          !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1481                          (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1482                         hw->fc.current_mode = e1000_fc_rx_pause;
1483                         DEBUGOUT("Flow Control = Rx PAUSE frames only.\n");
1484                 } else {
1485                         /* Per the IEEE spec, at this point flow control
1486                          * should be disabled.
1487                          */
1488                         hw->fc.current_mode = e1000_fc_none;
1489                         DEBUGOUT("Flow Control = NONE.\n");
1490                 }
1491
1492                 /* Now we need to do one last check...  If we auto-
1493                  * negotiated to HALF DUPLEX, flow control should not be
1494                  * enabled per IEEE 802.3 spec.
1495                  */
1496                 ret_val = mac->ops.get_link_up_info(hw, &speed, &duplex);
1497                 if (ret_val) {
1498                         DEBUGOUT("Error getting link speed and duplex\n");
1499                         return ret_val;
1500                 }
1501
1502                 if (duplex == HALF_DUPLEX)
1503                         hw->fc.current_mode = e1000_fc_none;
1504
1505                 /* Now we call a subroutine to actually force the MAC
1506                  * controller to use the correct flow control settings.
1507                  */
1508                 ret_val = e1000_force_mac_fc_generic(hw);
1509                 if (ret_val) {
1510                         DEBUGOUT("Error forcing flow control settings\n");
1511                         return ret_val;
1512                 }
1513         }
1514
1515         /* Check for the case where we have SerDes media and auto-neg is
1516          * enabled.  In this case, we need to check and see if Auto-Neg
1517          * has completed, and if so, how the PHY and link partner has
1518          * flow control configured.
1519          */
1520         if ((hw->phy.media_type == e1000_media_type_internal_serdes) &&
1521             mac->autoneg) {
1522                 /* Read the PCS_LSTS and check to see if AutoNeg
1523                  * has completed.
1524                  */
1525                 pcs_status_reg = E1000_READ_REG(hw, E1000_PCS_LSTAT);
1526
1527                 if (!(pcs_status_reg & E1000_PCS_LSTS_AN_COMPLETE)) {
1528                         DEBUGOUT("PCS Auto Neg has not completed.\n");
1529                         return ret_val;
1530                 }
1531
1532                 /* The AutoNeg process has completed, so we now need to
1533                  * read both the Auto Negotiation Advertisement
1534                  * Register (PCS_ANADV) and the Auto_Negotiation Base
1535                  * Page Ability Register (PCS_LPAB) to determine how
1536                  * flow control was negotiated.
1537                  */
1538                 pcs_adv_reg = E1000_READ_REG(hw, E1000_PCS_ANADV);
1539                 pcs_lp_ability_reg = E1000_READ_REG(hw, E1000_PCS_LPAB);
1540
1541                 /* Two bits in the Auto Negotiation Advertisement Register
1542                  * (PCS_ANADV) and two bits in the Auto Negotiation Base
1543                  * Page Ability Register (PCS_LPAB) determine flow control
1544                  * for both the PHY and the link partner.  The following
1545                  * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
1546                  * 1999, describes these PAUSE resolution bits and how flow
1547                  * control is determined based upon these settings.
1548                  * NOTE:  DC = Don't Care
1549                  *
1550                  *   LOCAL DEVICE  |   LINK PARTNER
1551                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
1552                  *-------|---------|-------|---------|--------------------
1553                  *   0   |    0    |  DC   |   DC    | e1000_fc_none
1554                  *   0   |    1    |   0   |   DC    | e1000_fc_none
1555                  *   0   |    1    |   1   |    0    | e1000_fc_none
1556                  *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1557                  *   1   |    0    |   0   |   DC    | e1000_fc_none
1558                  *   1   |   DC    |   1   |   DC    | e1000_fc_full
1559                  *   1   |    1    |   0   |    0    | e1000_fc_none
1560                  *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1561                  *
1562                  * Are both PAUSE bits set to 1?  If so, this implies
1563                  * Symmetric Flow Control is enabled at both ends.  The
1564                  * ASM_DIR bits are irrelevant per the spec.
1565                  *
1566                  * For Symmetric Flow Control:
1567                  *
1568                  *   LOCAL DEVICE  |   LINK PARTNER
1569                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1570                  *-------|---------|-------|---------|--------------------
1571                  *   1   |   DC    |   1   |   DC    | e1000_fc_full
1572                  *
1573                  */
1574                 if ((pcs_adv_reg & E1000_TXCW_PAUSE) &&
1575                     (pcs_lp_ability_reg & E1000_TXCW_PAUSE)) {
1576                         /* Now we need to check if the user selected Rx ONLY
1577                          * of pause frames.  In this case, we had to advertise
1578                          * FULL flow control because we could not advertise Rx
1579                          * ONLY. Hence, we must now check to see if we need to
1580                          * turn OFF the TRANSMISSION of PAUSE frames.
1581                          */
1582                         if (hw->fc.requested_mode == e1000_fc_full) {
1583                                 hw->fc.current_mode = e1000_fc_full;
1584                                 DEBUGOUT("Flow Control = FULL.\n");
1585                         } else {
1586                                 hw->fc.current_mode = e1000_fc_rx_pause;
1587                                 DEBUGOUT("Flow Control = Rx PAUSE frames only.\n");
1588                         }
1589                 }
1590                 /* For receiving PAUSE frames ONLY.
1591                  *
1592                  *   LOCAL DEVICE  |   LINK PARTNER
1593                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1594                  *-------|---------|-------|---------|--------------------
1595                  *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1596                  */
1597                 else if (!(pcs_adv_reg & E1000_TXCW_PAUSE) &&
1598                           (pcs_adv_reg & E1000_TXCW_ASM_DIR) &&
1599                           (pcs_lp_ability_reg & E1000_TXCW_PAUSE) &&
1600                           (pcs_lp_ability_reg & E1000_TXCW_ASM_DIR)) {
1601                         hw->fc.current_mode = e1000_fc_tx_pause;
1602                         DEBUGOUT("Flow Control = Tx PAUSE frames only.\n");
1603                 }
1604                 /* For transmitting PAUSE frames ONLY.
1605                  *
1606                  *   LOCAL DEVICE  |   LINK PARTNER
1607                  * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1608                  *-------|---------|-------|---------|--------------------
1609                  *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1610                  */
1611                 else if ((pcs_adv_reg & E1000_TXCW_PAUSE) &&
1612                          (pcs_adv_reg & E1000_TXCW_ASM_DIR) &&
1613                          !(pcs_lp_ability_reg & E1000_TXCW_PAUSE) &&
1614                          (pcs_lp_ability_reg & E1000_TXCW_ASM_DIR)) {
1615                         hw->fc.current_mode = e1000_fc_rx_pause;
1616                         DEBUGOUT("Flow Control = Rx PAUSE frames only.\n");
1617                 } else {
1618                         /* Per the IEEE spec, at this point flow control
1619                          * should be disabled.
1620                          */
1621                         hw->fc.current_mode = e1000_fc_none;
1622                         DEBUGOUT("Flow Control = NONE.\n");
1623                 }
1624
1625                 /* Now we call a subroutine to actually force the MAC
1626                  * controller to use the correct flow control settings.
1627                  */
1628                 pcs_ctrl_reg = E1000_READ_REG(hw, E1000_PCS_LCTL);
1629                 pcs_ctrl_reg |= E1000_PCS_LCTL_FORCE_FCTRL;
1630                 E1000_WRITE_REG(hw, E1000_PCS_LCTL, pcs_ctrl_reg);
1631
1632                 ret_val = e1000_force_mac_fc_generic(hw);
1633                 if (ret_val) {
1634                         DEBUGOUT("Error forcing flow control settings\n");
1635                         return ret_val;
1636                 }
1637         }
1638
1639         return E1000_SUCCESS;
1640 }
1641
1642 /**
1643  *  e1000_get_speed_and_duplex_copper_generic - Retrieve current speed/duplex
1644  *  @hw: pointer to the HW structure
1645  *  @speed: stores the current speed
1646  *  @duplex: stores the current duplex
1647  *
1648  *  Read the status register for the current speed/duplex and store the current
1649  *  speed and duplex for copper connections.
1650  **/
1651 s32 e1000_get_speed_and_duplex_copper_generic(struct e1000_hw *hw, u16 *speed,
1652                                               u16 *duplex)
1653 {
1654         u32 status;
1655
1656         DEBUGFUNC("e1000_get_speed_and_duplex_copper_generic");
1657
1658         status = E1000_READ_REG(hw, E1000_STATUS);
1659         if (status & E1000_STATUS_SPEED_1000) {
1660                 *speed = SPEED_1000;
1661                 DEBUGOUT("1000 Mbs, ");
1662         } else if (status & E1000_STATUS_SPEED_100) {
1663                 *speed = SPEED_100;
1664                 DEBUGOUT("100 Mbs, ");
1665         } else {
1666                 *speed = SPEED_10;
1667                 DEBUGOUT("10 Mbs, ");
1668         }
1669
1670         if (status & E1000_STATUS_FD) {
1671                 *duplex = FULL_DUPLEX;
1672                 DEBUGOUT("Full Duplex\n");
1673         } else {
1674                 *duplex = HALF_DUPLEX;
1675                 DEBUGOUT("Half Duplex\n");
1676         }
1677
1678         return E1000_SUCCESS;
1679 }
1680
1681 /**
1682  *  e1000_get_speed_and_duplex_fiber_generic - Retrieve current speed/duplex
1683  *  @hw: pointer to the HW structure
1684  *  @speed: stores the current speed
1685  *  @duplex: stores the current duplex
1686  *
1687  *  Sets the speed and duplex to gigabit full duplex (the only possible option)
1688  *  for fiber/serdes links.
1689  **/
1690 s32 e1000_get_speed_and_duplex_fiber_serdes_generic(struct e1000_hw E1000_UNUSEDARG *hw,
1691                                                     u16 *speed, u16 *duplex)
1692 {
1693         DEBUGFUNC("e1000_get_speed_and_duplex_fiber_serdes_generic");
1694         UNREFERENCED_1PARAMETER(hw);
1695
1696         *speed = SPEED_1000;
1697         *duplex = FULL_DUPLEX;
1698
1699         return E1000_SUCCESS;
1700 }
1701
1702 /**
1703  *  e1000_get_hw_semaphore_generic - Acquire hardware semaphore
1704  *  @hw: pointer to the HW structure
1705  *
1706  *  Acquire the HW semaphore to access the PHY or NVM
1707  **/
1708 s32 e1000_get_hw_semaphore_generic(struct e1000_hw *hw)
1709 {
1710         u32 swsm;
1711         s32 timeout = hw->nvm.word_size + 1;
1712         s32 i = 0;
1713
1714         DEBUGFUNC("e1000_get_hw_semaphore_generic");
1715
1716         /* Get the SW semaphore */
1717         while (i < timeout) {
1718                 swsm = E1000_READ_REG(hw, E1000_SWSM);
1719                 if (!(swsm & E1000_SWSM_SMBI))
1720                         break;
1721
1722                 usec_delay(50);
1723                 i++;
1724         }
1725
1726         if (i == timeout) {
1727                 DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
1728                 return -E1000_ERR_NVM;
1729         }
1730
1731         /* Get the FW semaphore. */
1732         for (i = 0; i < timeout; i++) {
1733                 swsm = E1000_READ_REG(hw, E1000_SWSM);
1734                 E1000_WRITE_REG(hw, E1000_SWSM, swsm | E1000_SWSM_SWESMBI);
1735
1736                 /* Semaphore acquired if bit latched */
1737                 if (E1000_READ_REG(hw, E1000_SWSM) & E1000_SWSM_SWESMBI)
1738                         break;
1739
1740                 usec_delay(50);
1741         }
1742
1743         if (i == timeout) {
1744                 /* Release semaphores */
1745                 e1000_put_hw_semaphore_generic(hw);
1746                 DEBUGOUT("Driver can't access the NVM\n");
1747                 return -E1000_ERR_NVM;
1748         }
1749
1750         return E1000_SUCCESS;
1751 }
1752
1753 /**
1754  *  e1000_put_hw_semaphore_generic - Release hardware semaphore
1755  *  @hw: pointer to the HW structure
1756  *
1757  *  Release hardware semaphore used to access the PHY or NVM
1758  **/
1759 void e1000_put_hw_semaphore_generic(struct e1000_hw *hw)
1760 {
1761         u32 swsm;
1762
1763         DEBUGFUNC("e1000_put_hw_semaphore_generic");
1764
1765         swsm = E1000_READ_REG(hw, E1000_SWSM);
1766
1767         swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1768
1769         E1000_WRITE_REG(hw, E1000_SWSM, swsm);
1770 }
1771
1772 /**
1773  *  e1000_get_auto_rd_done_generic - Check for auto read completion
1774  *  @hw: pointer to the HW structure
1775  *
1776  *  Check EEPROM for Auto Read done bit.
1777  **/
1778 s32 e1000_get_auto_rd_done_generic(struct e1000_hw *hw)
1779 {
1780         s32 i = 0;
1781
1782         DEBUGFUNC("e1000_get_auto_rd_done_generic");
1783
1784         while (i < AUTO_READ_DONE_TIMEOUT) {
1785                 if (E1000_READ_REG(hw, E1000_EECD) & E1000_EECD_AUTO_RD)
1786                         break;
1787                 msec_delay(1);
1788                 i++;
1789         }
1790
1791         if (i == AUTO_READ_DONE_TIMEOUT) {
1792                 DEBUGOUT("Auto read by HW from NVM has not completed.\n");
1793                 return -E1000_ERR_RESET;
1794         }
1795
1796         return E1000_SUCCESS;
1797 }
1798
1799 /**
1800  *  e1000_valid_led_default_generic - Verify a valid default LED config
1801  *  @hw: pointer to the HW structure
1802  *  @data: pointer to the NVM (EEPROM)
1803  *
1804  *  Read the EEPROM for the current default LED configuration.  If the
1805  *  LED configuration is not valid, set to a valid LED configuration.
1806  **/
1807 s32 e1000_valid_led_default_generic(struct e1000_hw *hw, u16 *data)
1808 {
1809         s32 ret_val;
1810
1811         DEBUGFUNC("e1000_valid_led_default_generic");
1812
1813         ret_val = hw->nvm.ops.read(hw, NVM_ID_LED_SETTINGS, 1, data);
1814         if (ret_val) {
1815                 DEBUGOUT("NVM Read Error\n");
1816                 return ret_val;
1817         }
1818
1819         if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF)
1820                 *data = ID_LED_DEFAULT;
1821
1822         return E1000_SUCCESS;
1823 }
1824
1825 /**
1826  *  e1000_id_led_init_generic -
1827  *  @hw: pointer to the HW structure
1828  *
1829  **/
1830 s32 e1000_id_led_init_generic(struct e1000_hw *hw)
1831 {
1832         struct e1000_mac_info *mac = &hw->mac;
1833         s32 ret_val;
1834         const u32 ledctl_mask = 0x000000FF;
1835         const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
1836         const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
1837         u16 data, i, temp;
1838         const u16 led_mask = 0x0F;
1839
1840         DEBUGFUNC("e1000_id_led_init_generic");
1841
1842         ret_val = hw->nvm.ops.valid_led_default(hw, &data);
1843         if (ret_val)
1844                 return ret_val;
1845
1846         mac->ledctl_default = E1000_READ_REG(hw, E1000_LEDCTL);
1847         mac->ledctl_mode1 = mac->ledctl_default;
1848         mac->ledctl_mode2 = mac->ledctl_default;
1849
1850         for (i = 0; i < 4; i++) {
1851                 temp = (data >> (i << 2)) & led_mask;
1852                 switch (temp) {
1853                 case ID_LED_ON1_DEF2:
1854                 case ID_LED_ON1_ON2:
1855                 case ID_LED_ON1_OFF2:
1856                         mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1857                         mac->ledctl_mode1 |= ledctl_on << (i << 3);
1858                         break;
1859                 case ID_LED_OFF1_DEF2:
1860                 case ID_LED_OFF1_ON2:
1861                 case ID_LED_OFF1_OFF2:
1862                         mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1863                         mac->ledctl_mode1 |= ledctl_off << (i << 3);
1864                         break;
1865                 default:
1866                         /* Do nothing */
1867                         break;
1868                 }
1869                 switch (temp) {
1870                 case ID_LED_DEF1_ON2:
1871                 case ID_LED_ON1_ON2:
1872                 case ID_LED_OFF1_ON2:
1873                         mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1874                         mac->ledctl_mode2 |= ledctl_on << (i << 3);
1875                         break;
1876                 case ID_LED_DEF1_OFF2:
1877                 case ID_LED_ON1_OFF2:
1878                 case ID_LED_OFF1_OFF2:
1879                         mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1880                         mac->ledctl_mode2 |= ledctl_off << (i << 3);
1881                         break;
1882                 default:
1883                         /* Do nothing */
1884                         break;
1885                 }
1886         }
1887
1888         return E1000_SUCCESS;
1889 }
1890
1891 /**
1892  *  e1000_setup_led_generic - Configures SW controllable LED
1893  *  @hw: pointer to the HW structure
1894  *
1895  *  This prepares the SW controllable LED for use and saves the current state
1896  *  of the LED so it can be later restored.
1897  **/
1898 s32 e1000_setup_led_generic(struct e1000_hw *hw)
1899 {
1900         u32 ledctl;
1901
1902         DEBUGFUNC("e1000_setup_led_generic");
1903
1904         if (hw->mac.ops.setup_led != e1000_setup_led_generic)
1905                 return -E1000_ERR_CONFIG;
1906
1907         if (hw->phy.media_type == e1000_media_type_fiber) {
1908                 ledctl = E1000_READ_REG(hw, E1000_LEDCTL);
1909                 hw->mac.ledctl_default = ledctl;
1910                 /* Turn off LED0 */
1911                 ledctl &= ~(E1000_LEDCTL_LED0_IVRT | E1000_LEDCTL_LED0_BLINK |
1912                             E1000_LEDCTL_LED0_MODE_MASK);
1913                 ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
1914                            E1000_LEDCTL_LED0_MODE_SHIFT);
1915                 E1000_WRITE_REG(hw, E1000_LEDCTL, ledctl);
1916         } else if (hw->phy.media_type == e1000_media_type_copper) {
1917                 E1000_WRITE_REG(hw, E1000_LEDCTL, hw->mac.ledctl_mode1);
1918         }
1919
1920         return E1000_SUCCESS;
1921 }
1922
1923 /**
1924  *  e1000_cleanup_led_generic - Set LED config to default operation
1925  *  @hw: pointer to the HW structure
1926  *
1927  *  Remove the current LED configuration and set the LED configuration
1928  *  to the default value, saved from the EEPROM.
1929  **/
1930 s32 e1000_cleanup_led_generic(struct e1000_hw *hw)
1931 {
1932         DEBUGFUNC("e1000_cleanup_led_generic");
1933
1934         E1000_WRITE_REG(hw, E1000_LEDCTL, hw->mac.ledctl_default);
1935         return E1000_SUCCESS;
1936 }
1937
1938 /**
1939  *  e1000_blink_led_generic - Blink LED
1940  *  @hw: pointer to the HW structure
1941  *
1942  *  Blink the LEDs which are set to be on.
1943  **/
1944 s32 e1000_blink_led_generic(struct e1000_hw *hw)
1945 {
1946         u32 ledctl_blink = 0;
1947         u32 i;
1948
1949         DEBUGFUNC("e1000_blink_led_generic");
1950
1951         if (hw->phy.media_type == e1000_media_type_fiber) {
1952                 /* always blink LED0 for PCI-E fiber */
1953                 ledctl_blink = E1000_LEDCTL_LED0_BLINK |
1954                      (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
1955         } else {
1956                 /* Set the blink bit for each LED that's "on" (0x0E)
1957                  * (or "off" if inverted) in ledctl_mode2.  The blink
1958                  * logic in hardware only works when mode is set to "on"
1959                  * so it must be changed accordingly when the mode is
1960                  * "off" and inverted.
1961                  */
1962                 ledctl_blink = hw->mac.ledctl_mode2;
1963                 for (i = 0; i < 32; i += 8) {
1964                         u32 mode = (hw->mac.ledctl_mode2 >> i) &
1965                             E1000_LEDCTL_LED0_MODE_MASK;
1966                         u32 led_default = hw->mac.ledctl_default >> i;
1967
1968                         if ((!(led_default & E1000_LEDCTL_LED0_IVRT) &&
1969                              (mode == E1000_LEDCTL_MODE_LED_ON)) ||
1970                             ((led_default & E1000_LEDCTL_LED0_IVRT) &&
1971                              (mode == E1000_LEDCTL_MODE_LED_OFF))) {
1972                                 ledctl_blink &=
1973                                     ~(E1000_LEDCTL_LED0_MODE_MASK << i);
1974                                 ledctl_blink |= (E1000_LEDCTL_LED0_BLINK |
1975                                                  E1000_LEDCTL_MODE_LED_ON) << i;
1976                         }
1977                 }
1978         }
1979
1980         E1000_WRITE_REG(hw, E1000_LEDCTL, ledctl_blink);
1981
1982         return E1000_SUCCESS;
1983 }
1984
1985 /**
1986  *  e1000_led_on_generic - Turn LED on
1987  *  @hw: pointer to the HW structure
1988  *
1989  *  Turn LED on.
1990  **/
1991 s32 e1000_led_on_generic(struct e1000_hw *hw)
1992 {
1993         u32 ctrl;
1994
1995         DEBUGFUNC("e1000_led_on_generic");
1996
1997         switch (hw->phy.media_type) {
1998         case e1000_media_type_fiber:
1999                 ctrl = E1000_READ_REG(hw, E1000_CTRL);
2000                 ctrl &= ~E1000_CTRL_SWDPIN0;
2001                 ctrl |= E1000_CTRL_SWDPIO0;
2002                 E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
2003                 break;
2004         case e1000_media_type_copper:
2005                 E1000_WRITE_REG(hw, E1000_LEDCTL, hw->mac.ledctl_mode2);
2006                 break;
2007         default:
2008                 break;
2009         }
2010
2011         return E1000_SUCCESS;
2012 }
2013
2014 /**
2015  *  e1000_led_off_generic - Turn LED off
2016  *  @hw: pointer to the HW structure
2017  *
2018  *  Turn LED off.
2019  **/
2020 s32 e1000_led_off_generic(struct e1000_hw *hw)
2021 {
2022         u32 ctrl;
2023
2024         DEBUGFUNC("e1000_led_off_generic");
2025
2026         switch (hw->phy.media_type) {
2027         case e1000_media_type_fiber:
2028                 ctrl = E1000_READ_REG(hw, E1000_CTRL);
2029                 ctrl |= E1000_CTRL_SWDPIN0;
2030                 ctrl |= E1000_CTRL_SWDPIO0;
2031                 E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
2032                 break;
2033         case e1000_media_type_copper:
2034                 E1000_WRITE_REG(hw, E1000_LEDCTL, hw->mac.ledctl_mode1);
2035                 break;
2036         default:
2037                 break;
2038         }
2039
2040         return E1000_SUCCESS;
2041 }
2042
2043 /**
2044  *  e1000_set_pcie_no_snoop_generic - Set PCI-express capabilities
2045  *  @hw: pointer to the HW structure
2046  *  @no_snoop: bitmap of snoop events
2047  *
2048  *  Set the PCI-express register to snoop for events enabled in 'no_snoop'.
2049  **/
2050 void e1000_set_pcie_no_snoop_generic(struct e1000_hw *hw, u32 no_snoop)
2051 {
2052         u32 gcr;
2053
2054         DEBUGFUNC("e1000_set_pcie_no_snoop_generic");
2055
2056         if (hw->bus.type != e1000_bus_type_pci_express)
2057                 return;
2058
2059         if (no_snoop) {
2060                 gcr = E1000_READ_REG(hw, E1000_GCR);
2061                 gcr &= ~(PCIE_NO_SNOOP_ALL);
2062                 gcr |= no_snoop;
2063                 E1000_WRITE_REG(hw, E1000_GCR, gcr);
2064         }
2065 }
2066
2067 /**
2068  *  e1000_disable_pcie_master_generic - Disables PCI-express master access
2069  *  @hw: pointer to the HW structure
2070  *
2071  *  Returns E1000_SUCCESS if successful, else returns -10
2072  *  (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not caused
2073  *  the master requests to be disabled.
2074  *
2075  *  Disables PCI-Express master access and verifies there are no pending
2076  *  requests.
2077  **/
2078 s32 e1000_disable_pcie_master_generic(struct e1000_hw *hw)
2079 {
2080         u32 ctrl;
2081         s32 timeout = MASTER_DISABLE_TIMEOUT;
2082
2083         DEBUGFUNC("e1000_disable_pcie_master_generic");
2084
2085         if (hw->bus.type != e1000_bus_type_pci_express)
2086                 return E1000_SUCCESS;
2087
2088         ctrl = E1000_READ_REG(hw, E1000_CTRL);
2089         ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
2090         E1000_WRITE_REG(hw, E1000_CTRL, ctrl);
2091
2092         while (timeout) {
2093                 if (!(E1000_READ_REG(hw, E1000_STATUS) &
2094                       E1000_STATUS_GIO_MASTER_ENABLE) ||
2095                                 E1000_REMOVED(hw->hw_addr))
2096                         break;
2097                 usec_delay(100);
2098                 timeout--;
2099         }
2100
2101         if (!timeout) {
2102                 DEBUGOUT("Master requests are pending.\n");
2103                 return -E1000_ERR_MASTER_REQUESTS_PENDING;
2104         }
2105
2106         return E1000_SUCCESS;
2107 }
2108
2109 /**
2110  *  e1000_reset_adaptive_generic - Reset Adaptive Interframe Spacing
2111  *  @hw: pointer to the HW structure
2112  *
2113  *  Reset the Adaptive Interframe Spacing throttle to default values.
2114  **/
2115 void e1000_reset_adaptive_generic(struct e1000_hw *hw)
2116 {
2117         struct e1000_mac_info *mac = &hw->mac;
2118
2119         DEBUGFUNC("e1000_reset_adaptive_generic");
2120
2121         if (!mac->adaptive_ifs) {
2122                 DEBUGOUT("Not in Adaptive IFS mode!\n");
2123                 return;
2124         }
2125
2126         mac->current_ifs_val = 0;
2127         mac->ifs_min_val = IFS_MIN;
2128         mac->ifs_max_val = IFS_MAX;
2129         mac->ifs_step_size = IFS_STEP;
2130         mac->ifs_ratio = IFS_RATIO;
2131
2132         mac->in_ifs_mode = false;
2133         E1000_WRITE_REG(hw, E1000_AIT, 0);
2134 }
2135
2136 /**
2137  *  e1000_update_adaptive_generic - Update Adaptive Interframe Spacing
2138  *  @hw: pointer to the HW structure
2139  *
2140  *  Update the Adaptive Interframe Spacing Throttle value based on the
2141  *  time between transmitted packets and time between collisions.
2142  **/
2143 void e1000_update_adaptive_generic(struct e1000_hw *hw)
2144 {
2145         struct e1000_mac_info *mac = &hw->mac;
2146
2147         DEBUGFUNC("e1000_update_adaptive_generic");
2148
2149         if (!mac->adaptive_ifs) {
2150                 DEBUGOUT("Not in Adaptive IFS mode!\n");
2151                 return;
2152         }
2153
2154         if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
2155                 if (mac->tx_packet_delta > MIN_NUM_XMITS) {
2156                         mac->in_ifs_mode = true;
2157                         if (mac->current_ifs_val < mac->ifs_max_val) {
2158                                 if (!mac->current_ifs_val)
2159                                         mac->current_ifs_val = mac->ifs_min_val;
2160                                 else
2161                                         mac->current_ifs_val +=
2162                                                 mac->ifs_step_size;
2163                                 E1000_WRITE_REG(hw, E1000_AIT,
2164                                                 mac->current_ifs_val);
2165                         }
2166                 }
2167         } else {
2168                 if (mac->in_ifs_mode &&
2169                     (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
2170                         mac->current_ifs_val = 0;
2171                         mac->in_ifs_mode = false;
2172                         E1000_WRITE_REG(hw, E1000_AIT, 0);
2173                 }
2174         }
2175 }
2176
2177 /**
2178  *  e1000_validate_mdi_setting_generic - Verify MDI/MDIx settings
2179  *  @hw: pointer to the HW structure
2180  *
2181  *  Verify that when not using auto-negotiation that MDI/MDIx is correctly
2182  *  set, which is forced to MDI mode only.
2183  **/
2184 STATIC s32 e1000_validate_mdi_setting_generic(struct e1000_hw *hw)
2185 {
2186         DEBUGFUNC("e1000_validate_mdi_setting_generic");
2187
2188         if (!hw->mac.autoneg && (hw->phy.mdix == 0 || hw->phy.mdix == 3)) {
2189                 DEBUGOUT("Invalid MDI setting detected\n");
2190                 hw->phy.mdix = 1;
2191                 return -E1000_ERR_CONFIG;
2192         }
2193
2194         return E1000_SUCCESS;
2195 }
2196
2197 /**
2198  *  e1000_validate_mdi_setting_crossover_generic - Verify MDI/MDIx settings
2199  *  @hw: pointer to the HW structure
2200  *
2201  *  Validate the MDI/MDIx setting, allowing for auto-crossover during forced
2202  *  operation.
2203  **/
2204 s32 e1000_validate_mdi_setting_crossover_generic(struct e1000_hw E1000_UNUSEDARG *hw)
2205 {
2206         DEBUGFUNC("e1000_validate_mdi_setting_crossover_generic");
2207         UNREFERENCED_1PARAMETER(hw);
2208
2209         return E1000_SUCCESS;
2210 }
2211
2212 /**
2213  *  e1000_write_8bit_ctrl_reg_generic - Write a 8bit CTRL register
2214  *  @hw: pointer to the HW structure
2215  *  @reg: 32bit register offset such as E1000_SCTL
2216  *  @offset: register offset to write to
2217  *  @data: data to write at register offset
2218  *
2219  *  Writes an address/data control type register.  There are several of these
2220  *  and they all have the format address << 8 | data and bit 31 is polled for
2221  *  completion.
2222  **/
2223 s32 e1000_write_8bit_ctrl_reg_generic(struct e1000_hw *hw, u32 reg,
2224                                       u32 offset, u8 data)
2225 {
2226         u32 i, regvalue = 0;
2227
2228         DEBUGFUNC("e1000_write_8bit_ctrl_reg_generic");
2229
2230         /* Set up the address and data */
2231         regvalue = ((u32)data) | (offset << E1000_GEN_CTL_ADDRESS_SHIFT);
2232         E1000_WRITE_REG(hw, reg, regvalue);
2233
2234         /* Poll the ready bit to see if the MDI read completed */
2235         for (i = 0; i < E1000_GEN_POLL_TIMEOUT; i++) {
2236                 usec_delay(5);
2237                 regvalue = E1000_READ_REG(hw, reg);
2238                 if (regvalue & E1000_GEN_CTL_READY)
2239                         break;
2240         }
2241         if (!(regvalue & E1000_GEN_CTL_READY)) {
2242                 DEBUGOUT1("Reg %08x did not indicate ready\n", reg);
2243                 return -E1000_ERR_PHY;
2244         }
2245
2246         return E1000_SUCCESS;
2247 }