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