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