blob: 986aa902f7ed5d8ae2d27c51b4e11eebbea0c79f [file] [log] [blame]
Auke Kok9d5c8242008-01-24 02:22:38 -08001/*******************************************************************************
2
3 Intel(R) Gigabit Ethernet Linux driver
Alexander Duyck86d5d382009-02-06 23:23:12 +00004 Copyright(c) 2007-2009 Intel Corporation.
Auke Kok9d5c8242008-01-24 02:22:38 -08005
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21
22 Contact Information:
23 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
24 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
25
26*******************************************************************************/
27
28#include <linux/if_ether.h>
29#include <linux/delay.h>
30#include <linux/pci.h>
31#include <linux/netdevice.h>
32
33#include "e1000_mac.h"
34
35#include "igb.h"
36
37static s32 igb_set_default_fc(struct e1000_hw *hw);
38static s32 igb_set_fc_watermarks(struct e1000_hw *hw);
Auke Kok9d5c8242008-01-24 02:22:38 -080039
Auke Kok9d5c8242008-01-24 02:22:38 -080040/**
Jeff Kirsher733596b2008-06-27 10:59:59 -070041 * igb_get_bus_info_pcie - Get PCIe bus information
Auke Kok9d5c8242008-01-24 02:22:38 -080042 * @hw: pointer to the HW structure
43 *
44 * Determines and stores the system bus information for a particular
45 * network interface. The following bus information is determined and stored:
46 * bus speed, bus width, type (PCIe), and PCIe function.
47 **/
48s32 igb_get_bus_info_pcie(struct e1000_hw *hw)
49{
50 struct e1000_bus_info *bus = &hw->bus;
51 s32 ret_val;
Alexander Duyck5e8427e2008-12-10 01:09:53 -080052 u32 reg;
53 u16 pcie_link_status;
Auke Kok9d5c8242008-01-24 02:22:38 -080054
55 bus->type = e1000_bus_type_pci_express;
56 bus->speed = e1000_bus_speed_2500;
57
58 ret_val = igb_read_pcie_cap_reg(hw,
59 PCIE_LINK_STATUS,
60 &pcie_link_status);
61 if (ret_val)
62 bus->width = e1000_bus_width_unknown;
63 else
64 bus->width = (enum e1000_bus_width)((pcie_link_status &
65 PCIE_LINK_WIDTH_MASK) >>
66 PCIE_LINK_WIDTH_SHIFT);
67
Alexander Duyck5e8427e2008-12-10 01:09:53 -080068 reg = rd32(E1000_STATUS);
69 bus->func = (reg & E1000_STATUS_FUNC_MASK) >> E1000_STATUS_FUNC_SHIFT;
Auke Kok9d5c8242008-01-24 02:22:38 -080070
71 return 0;
72}
73
74/**
Jeff Kirsher733596b2008-06-27 10:59:59 -070075 * igb_clear_vfta - Clear VLAN filter table
Auke Kok9d5c8242008-01-24 02:22:38 -080076 * @hw: pointer to the HW structure
77 *
78 * Clears the register array which contains the VLAN filter table by
79 * setting all the values to 0.
80 **/
81void igb_clear_vfta(struct e1000_hw *hw)
82{
83 u32 offset;
84
85 for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
86 array_wr32(E1000_VFTA, offset, 0);
87 wrfl();
88 }
89}
90
91/**
Jeff Kirsher733596b2008-06-27 10:59:59 -070092 * igb_write_vfta - Write value to VLAN filter table
Auke Kok9d5c8242008-01-24 02:22:38 -080093 * @hw: pointer to the HW structure
94 * @offset: register offset in VLAN filter table
95 * @value: register value written to VLAN filter table
96 *
97 * Writes value at the given offset in the register array which stores
98 * the VLAN filter table.
99 **/
Alexander Duyckff6f63d2009-04-09 22:49:02 +0000100static void igb_write_vfta(struct e1000_hw *hw, u32 offset, u32 value)
Auke Kok9d5c8242008-01-24 02:22:38 -0800101{
102 array_wr32(E1000_VFTA, offset, value);
103 wrfl();
104}
105
106/**
Alexander Duyck5ac16652009-07-23 18:09:12 +0000107 * igb_init_rx_addrs - Initialize receive address's
108 * @hw: pointer to the HW structure
109 * @rar_count: receive address registers
110 *
111 * Setups the receive address registers by setting the base receive address
112 * register to the devices MAC address and clearing all the other receive
113 * address registers to 0.
114 **/
115void igb_init_rx_addrs(struct e1000_hw *hw, u16 rar_count)
116{
117 u32 i;
118 u8 mac_addr[ETH_ALEN] = {0};
119
120 /* Setup the receive address */
121 hw_dbg("Programming MAC Address into RAR[0]\n");
122
123 hw->mac.ops.rar_set(hw, hw->mac.addr, 0);
124
125 /* Zero out the other (rar_entry_count - 1) receive addresses */
126 hw_dbg("Clearing RAR[1-%u]\n", rar_count-1);
127 for (i = 1; i < rar_count; i++)
128 hw->mac.ops.rar_set(hw, mac_addr, i);
129}
130
131/**
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800132 * igb_vfta_set - enable or disable vlan in VLAN filter table
133 * @hw: pointer to the HW structure
134 * @vid: VLAN id to add or remove
135 * @add: if true add filter, if false remove
136 *
137 * Sets or clears a bit in the VLAN filter table array based on VLAN id
138 * and if we are adding or removing the filter
139 **/
Alexander Duyckcad6d052009-03-13 20:41:37 +0000140s32 igb_vfta_set(struct e1000_hw *hw, u32 vid, bool add)
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800141{
142 u32 index = (vid >> E1000_VFTA_ENTRY_SHIFT) & E1000_VFTA_ENTRY_MASK;
Alexander Duyck75f4f382009-03-13 20:41:55 +0000143 u32 mask = 1 << (vid & E1000_VFTA_ENTRY_BIT_SHIFT_MASK);
Alexander Duyckcad6d052009-03-13 20:41:37 +0000144 u32 vfta = array_rd32(E1000_VFTA, index);
145 s32 ret_val = 0;
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800146
Alexander Duyckcad6d052009-03-13 20:41:37 +0000147 /* bit was set/cleared before we started */
148 if ((!!(vfta & mask)) == add) {
149 ret_val = -E1000_ERR_CONFIG;
150 } else {
151 if (add)
152 vfta |= mask;
153 else
154 vfta &= ~mask;
155 }
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800156
157 igb_write_vfta(hw, index, vfta);
Alexander Duyckcad6d052009-03-13 20:41:37 +0000158
159 return ret_val;
Alexander Duyck4ae196d2009-02-19 20:40:07 -0800160}
161
162/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700163 * igb_check_alt_mac_addr - Check for alternate MAC addr
Auke Kok9d5c8242008-01-24 02:22:38 -0800164 * @hw: pointer to the HW structure
165 *
166 * Checks the nvm for an alternate MAC address. An alternate MAC address
167 * can be setup by pre-boot software and must be treated like a permanent
168 * address and must override the actual permanent MAC address. If an
169 * alternate MAC address is fopund it is saved in the hw struct and
170 * prgrammed into RAR0 and the cuntion returns success, otherwise the
171 * fucntion returns an error.
172 **/
173s32 igb_check_alt_mac_addr(struct e1000_hw *hw)
174{
175 u32 i;
176 s32 ret_val = 0;
177 u16 offset, nvm_alt_mac_addr_offset, nvm_data;
178 u8 alt_mac_addr[ETH_ALEN];
179
Alexander Duyck312c75a2009-02-06 23:17:47 +0000180 ret_val = hw->nvm.ops.read(hw, NVM_ALT_MAC_ADDR_PTR, 1,
Auke Kok9d5c8242008-01-24 02:22:38 -0800181 &nvm_alt_mac_addr_offset);
182 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700183 hw_dbg("NVM Read Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800184 goto out;
185 }
186
187 if (nvm_alt_mac_addr_offset == 0xFFFF) {
188 ret_val = -(E1000_NOT_IMPLEMENTED);
189 goto out;
190 }
191
192 if (hw->bus.func == E1000_FUNC_1)
193 nvm_alt_mac_addr_offset += ETH_ALEN/sizeof(u16);
194
195 for (i = 0; i < ETH_ALEN; i += 2) {
196 offset = nvm_alt_mac_addr_offset + (i >> 1);
Alexander Duyck312c75a2009-02-06 23:17:47 +0000197 ret_val = hw->nvm.ops.read(hw, offset, 1, &nvm_data);
Auke Kok9d5c8242008-01-24 02:22:38 -0800198 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700199 hw_dbg("NVM Read Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800200 goto out;
201 }
202
203 alt_mac_addr[i] = (u8)(nvm_data & 0xFF);
204 alt_mac_addr[i + 1] = (u8)(nvm_data >> 8);
205 }
206
207 /* if multicast bit is set, the alternate address will not be used */
208 if (alt_mac_addr[0] & 0x01) {
209 ret_val = -(E1000_NOT_IMPLEMENTED);
210 goto out;
211 }
212
213 for (i = 0; i < ETH_ALEN; i++)
214 hw->mac.addr[i] = hw->mac.perm_addr[i] = alt_mac_addr[i];
215
216 hw->mac.ops.rar_set(hw, hw->mac.perm_addr, 0);
217
218out:
219 return ret_val;
220}
221
222/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700223 * igb_rar_set - Set receive address register
Auke Kok9d5c8242008-01-24 02:22:38 -0800224 * @hw: pointer to the HW structure
225 * @addr: pointer to the receive address
226 * @index: receive address array register
227 *
228 * Sets the receive address array register at index to the address passed
229 * in by addr.
230 **/
231void igb_rar_set(struct e1000_hw *hw, u8 *addr, u32 index)
232{
233 u32 rar_low, rar_high;
234
235 /*
236 * HW expects these in little endian so we reverse the byte order
237 * from network order (big endian) to little endian
238 */
239 rar_low = ((u32) addr[0] |
240 ((u32) addr[1] << 8) |
241 ((u32) addr[2] << 16) | ((u32) addr[3] << 24));
242
243 rar_high = ((u32) addr[4] | ((u32) addr[5] << 8));
244
Alexander Duyck86757372009-02-06 23:21:51 +0000245 /* If MAC address zero, no need to set the AV bit */
246 if (rar_low || rar_high)
Auke Kok9d5c8242008-01-24 02:22:38 -0800247 rar_high |= E1000_RAH_AV;
248
Alexander Duyck5e8427e2008-12-10 01:09:53 -0800249 wr32(E1000_RAL(index), rar_low);
250 wr32(E1000_RAH(index), rar_high);
Auke Kok9d5c8242008-01-24 02:22:38 -0800251}
252
253/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700254 * igb_mta_set - Set multicast filter table address
Auke Kok9d5c8242008-01-24 02:22:38 -0800255 * @hw: pointer to the HW structure
256 * @hash_value: determines the MTA register and bit to set
257 *
258 * The multicast table address is a register array of 32-bit registers.
259 * The hash_value is used to determine what register the bit is in, the
260 * current value is read, the new bit is OR'd in and the new value is
261 * written back into the register.
262 **/
Alexander Duyck549bdd82008-08-04 15:00:06 -0700263void igb_mta_set(struct e1000_hw *hw, u32 hash_value)
Auke Kok9d5c8242008-01-24 02:22:38 -0800264{
265 u32 hash_bit, hash_reg, mta;
266
267 /*
268 * The MTA is a register array of 32-bit registers. It is
269 * treated like an array of (32*mta_reg_count) bits. We want to
270 * set bit BitArray[hash_value]. So we figure out what register
271 * the bit is in, read it, OR in the new bit, then write
272 * back the new value. The (hw->mac.mta_reg_count - 1) serves as a
273 * mask to bits 31:5 of the hash value which gives us the
274 * register we're modifying. The hash bit within that register
275 * is determined by the lower 5 bits of the hash value.
276 */
277 hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
278 hash_bit = hash_value & 0x1F;
279
280 mta = array_rd32(E1000_MTA, hash_reg);
281
282 mta |= (1 << hash_bit);
283
284 array_wr32(E1000_MTA, hash_reg, mta);
285 wrfl();
286}
287
288/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700289 * igb_hash_mc_addr - Generate a multicast hash value
Auke Kok9d5c8242008-01-24 02:22:38 -0800290 * @hw: pointer to the HW structure
291 * @mc_addr: pointer to a multicast address
292 *
293 * Generates a multicast address hash value which is used to determine
294 * the multicast filter table array address and new table value. See
295 * igb_mta_set()
296 **/
Alexander Duyck44c852e2009-09-17 14:52:29 +0000297static u32 igb_hash_mc_addr(struct e1000_hw *hw, u8 *mc_addr)
Auke Kok9d5c8242008-01-24 02:22:38 -0800298{
299 u32 hash_value, hash_mask;
300 u8 bit_shift = 0;
301
302 /* Register count multiplied by bits per register */
303 hash_mask = (hw->mac.mta_reg_count * 32) - 1;
304
305 /*
306 * For a mc_filter_type of 0, bit_shift is the number of left-shifts
307 * where 0xFF would still fall within the hash mask.
308 */
309 while (hash_mask >> bit_shift != 0xFF)
310 bit_shift++;
311
312 /*
313 * The portion of the address that is used for the hash table
314 * is determined by the mc_filter_type setting.
315 * The algorithm is such that there is a total of 8 bits of shifting.
316 * The bit_shift for a mc_filter_type of 0 represents the number of
317 * left-shifts where the MSB of mc_addr[5] would still fall within
318 * the hash_mask. Case 0 does this exactly. Since there are a total
319 * of 8 bits of shifting, then mc_addr[4] will shift right the
320 * remaining number of bits. Thus 8 - bit_shift. The rest of the
321 * cases are a variation of this algorithm...essentially raising the
322 * number of bits to shift mc_addr[5] left, while still keeping the
323 * 8-bit shifting total.
324 *
325 * For example, given the following Destination MAC Address and an
326 * mta register count of 128 (thus a 4096-bit vector and 0xFFF mask),
327 * we can see that the bit_shift for case 0 is 4. These are the hash
328 * values resulting from each mc_filter_type...
329 * [0] [1] [2] [3] [4] [5]
330 * 01 AA 00 12 34 56
331 * LSB MSB
332 *
333 * case 0: hash_value = ((0x34 >> 4) | (0x56 << 4)) & 0xFFF = 0x563
334 * case 1: hash_value = ((0x34 >> 3) | (0x56 << 5)) & 0xFFF = 0xAC6
335 * case 2: hash_value = ((0x34 >> 2) | (0x56 << 6)) & 0xFFF = 0x163
336 * case 3: hash_value = ((0x34 >> 0) | (0x56 << 8)) & 0xFFF = 0x634
337 */
338 switch (hw->mac.mc_filter_type) {
339 default:
340 case 0:
341 break;
342 case 1:
343 bit_shift += 1;
344 break;
345 case 2:
346 bit_shift += 2;
347 break;
348 case 3:
349 bit_shift += 4;
350 break;
351 }
352
353 hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
354 (((u16) mc_addr[5]) << bit_shift)));
355
356 return hash_value;
357}
358
359/**
Alexander Duyck44c852e2009-09-17 14:52:29 +0000360 * igb_update_mc_addr_list - Update Multicast addresses
361 * @hw: pointer to the HW structure
362 * @mc_addr_list: array of multicast addresses to program
363 * @mc_addr_count: number of multicast addresses to program
364 *
365 * Updates entire Multicast Table Array.
366 * The caller must have a packed mc_addr_list of multicast addresses.
367 **/
368void igb_update_mc_addr_list(struct e1000_hw *hw,
369 u8 *mc_addr_list, u32 mc_addr_count)
370{
371 u32 hash_value, hash_bit, hash_reg;
372 int i;
373
374 /* clear mta_shadow */
375 memset(&hw->mac.mta_shadow, 0, sizeof(hw->mac.mta_shadow));
376
377 /* update mta_shadow from mc_addr_list */
378 for (i = 0; (u32) i < mc_addr_count; i++) {
379 hash_value = igb_hash_mc_addr(hw, mc_addr_list);
380
381 hash_reg = (hash_value >> 5) & (hw->mac.mta_reg_count - 1);
382 hash_bit = hash_value & 0x1F;
383
384 hw->mac.mta_shadow[hash_reg] |= (1 << hash_bit);
385 mc_addr_list += (ETH_ALEN);
386 }
387
388 /* replace the entire MTA table */
389 for (i = hw->mac.mta_reg_count - 1; i >= 0; i--)
390 array_wr32(E1000_MTA, i, hw->mac.mta_shadow[i]);
391 wrfl();
392}
393
394/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700395 * igb_clear_hw_cntrs_base - Clear base hardware counters
Auke Kok9d5c8242008-01-24 02:22:38 -0800396 * @hw: pointer to the HW structure
397 *
398 * Clears the base hardware counters by reading the counter registers.
399 **/
400void igb_clear_hw_cntrs_base(struct e1000_hw *hw)
401{
Alexander Duyckcc9073b2009-10-05 06:31:25 +0000402 rd32(E1000_CRCERRS);
403 rd32(E1000_SYMERRS);
404 rd32(E1000_MPC);
405 rd32(E1000_SCC);
406 rd32(E1000_ECOL);
407 rd32(E1000_MCC);
408 rd32(E1000_LATECOL);
409 rd32(E1000_COLC);
410 rd32(E1000_DC);
411 rd32(E1000_SEC);
412 rd32(E1000_RLEC);
413 rd32(E1000_XONRXC);
414 rd32(E1000_XONTXC);
415 rd32(E1000_XOFFRXC);
416 rd32(E1000_XOFFTXC);
417 rd32(E1000_FCRUC);
418 rd32(E1000_GPRC);
419 rd32(E1000_BPRC);
420 rd32(E1000_MPRC);
421 rd32(E1000_GPTC);
422 rd32(E1000_GORCL);
423 rd32(E1000_GORCH);
424 rd32(E1000_GOTCL);
425 rd32(E1000_GOTCH);
426 rd32(E1000_RNBC);
427 rd32(E1000_RUC);
428 rd32(E1000_RFC);
429 rd32(E1000_ROC);
430 rd32(E1000_RJC);
431 rd32(E1000_TORL);
432 rd32(E1000_TORH);
433 rd32(E1000_TOTL);
434 rd32(E1000_TOTH);
435 rd32(E1000_TPR);
436 rd32(E1000_TPT);
437 rd32(E1000_MPTC);
438 rd32(E1000_BPTC);
Auke Kok9d5c8242008-01-24 02:22:38 -0800439}
440
441/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700442 * igb_check_for_copper_link - Check for link (Copper)
Auke Kok9d5c8242008-01-24 02:22:38 -0800443 * @hw: pointer to the HW structure
444 *
445 * Checks to see of the link status of the hardware has changed. If a
446 * change in link status has been detected, then we read the PHY registers
447 * to get the current speed/duplex if link exists.
448 **/
449s32 igb_check_for_copper_link(struct e1000_hw *hw)
450{
451 struct e1000_mac_info *mac = &hw->mac;
452 s32 ret_val;
453 bool link;
454
455 /*
456 * We only want to go out to the PHY registers to see if Auto-Neg
457 * has completed and/or if our link status has changed. The
458 * get_link_status flag is set upon receiving a Link Status
459 * Change or Rx Sequence Error interrupt.
460 */
461 if (!mac->get_link_status) {
462 ret_val = 0;
463 goto out;
464 }
465
466 /*
467 * First we want to see if the MII Status Register reports
468 * link. If so, then we want to get the current speed/duplex
469 * of the PHY.
470 */
471 ret_val = igb_phy_has_link(hw, 1, 0, &link);
472 if (ret_val)
473 goto out;
474
475 if (!link)
476 goto out; /* No link detected */
477
478 mac->get_link_status = false;
479
480 /*
481 * Check if there was DownShift, must be checked
482 * immediately after link-up
483 */
484 igb_check_downshift(hw);
485
486 /*
487 * If we are forcing speed/duplex, then we simply return since
488 * we have already determined whether we have link or not.
489 */
490 if (!mac->autoneg) {
491 ret_val = -E1000_ERR_CONFIG;
492 goto out;
493 }
494
495 /*
496 * Auto-Neg is enabled. Auto Speed Detection takes care
497 * of MAC speed/duplex configuration. So we only need to
498 * configure Collision Distance in the MAC.
499 */
500 igb_config_collision_dist(hw);
501
502 /*
503 * Configure Flow Control now that Auto-Neg has completed.
504 * First, we need to restore the desired flow control
505 * settings because we may have had to re-autoneg with a
506 * different link partner.
507 */
508 ret_val = igb_config_fc_after_link_up(hw);
509 if (ret_val)
Auke Kok652fff32008-06-27 11:00:18 -0700510 hw_dbg("Error configuring flow control\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800511
512out:
513 return ret_val;
514}
515
516/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700517 * igb_setup_link - Setup flow control and link settings
Auke Kok9d5c8242008-01-24 02:22:38 -0800518 * @hw: pointer to the HW structure
519 *
520 * Determines which flow control settings to use, then configures flow
521 * control. Calls the appropriate media-specific link configuration
522 * function. Assuming the adapter has a valid link partner, a valid link
523 * should be established. Assumes the hardware has previously been reset
524 * and the transmitter and receiver are not enabled.
525 **/
526s32 igb_setup_link(struct e1000_hw *hw)
527{
528 s32 ret_val = 0;
529
530 /*
531 * In the case of the phy reset being blocked, we already have a link.
532 * We do not need to set it up again.
533 */
534 if (igb_check_reset_block(hw))
535 goto out;
536
Alexander Duyck0cce1192009-07-23 18:10:24 +0000537 /*
538 * If requested flow control is set to default, set flow control
539 * based on the EEPROM flow control settings.
540 */
541 if (hw->fc.requested_mode == e1000_fc_default) {
542 ret_val = igb_set_default_fc(hw);
543 if (ret_val)
544 goto out;
545 }
Auke Kok9d5c8242008-01-24 02:22:38 -0800546
547 /*
548 * We want to save off the original Flow Control configuration just
549 * in case we get disconnected and then reconnected into a different
550 * hub or switch with different Flow Control capabilities.
551 */
Alexander Duyck0cce1192009-07-23 18:10:24 +0000552 hw->fc.current_mode = hw->fc.requested_mode;
Auke Kok9d5c8242008-01-24 02:22:38 -0800553
Alexander Duyck0cce1192009-07-23 18:10:24 +0000554 hw_dbg("After fix-ups FlowControl is now = %x\n", hw->fc.current_mode);
Auke Kok9d5c8242008-01-24 02:22:38 -0800555
556 /* Call the necessary media_type subroutine to configure the link. */
557 ret_val = hw->mac.ops.setup_physical_interface(hw);
558 if (ret_val)
559 goto out;
560
561 /*
562 * Initialize the flow control address, type, and PAUSE timer
563 * registers to their default values. This is done even if flow
564 * control is disabled, because it does not hurt anything to
565 * initialize these registers.
566 */
Auke Kok652fff32008-06-27 11:00:18 -0700567 hw_dbg("Initializing the Flow Control address, type and timer regs\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800568 wr32(E1000_FCT, FLOW_CONTROL_TYPE);
569 wr32(E1000_FCAH, FLOW_CONTROL_ADDRESS_HIGH);
570 wr32(E1000_FCAL, FLOW_CONTROL_ADDRESS_LOW);
571
572 wr32(E1000_FCTTV, hw->fc.pause_time);
573
574 ret_val = igb_set_fc_watermarks(hw);
575
576out:
577 return ret_val;
578}
579
580/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700581 * igb_config_collision_dist - Configure collision distance
Auke Kok9d5c8242008-01-24 02:22:38 -0800582 * @hw: pointer to the HW structure
583 *
584 * Configures the collision distance to the default value and is used
585 * during link setup. Currently no func pointer exists and all
586 * implementations are handled in the generic version of this function.
587 **/
588void igb_config_collision_dist(struct e1000_hw *hw)
589{
590 u32 tctl;
591
592 tctl = rd32(E1000_TCTL);
593
594 tctl &= ~E1000_TCTL_COLD;
595 tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
596
597 wr32(E1000_TCTL, tctl);
598 wrfl();
599}
600
601/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700602 * igb_set_fc_watermarks - Set flow control high/low watermarks
Auke Kok9d5c8242008-01-24 02:22:38 -0800603 * @hw: pointer to the HW structure
604 *
605 * Sets the flow control high/low threshold (watermark) registers. If
606 * flow control XON frame transmission is enabled, then set XON frame
607 * tansmission as well.
608 **/
609static s32 igb_set_fc_watermarks(struct e1000_hw *hw)
610{
611 s32 ret_val = 0;
612 u32 fcrtl = 0, fcrth = 0;
613
614 /*
615 * Set the flow control receive threshold registers. Normally,
616 * these registers will be set to a default threshold that may be
617 * adjusted later by the driver's runtime code. However, if the
618 * ability to transmit pause frames is not enabled, then these
619 * registers will be set to 0.
620 */
Alexander Duyck0cce1192009-07-23 18:10:24 +0000621 if (hw->fc.current_mode & e1000_fc_tx_pause) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800622 /*
623 * We need to set up the Receive Threshold high and low water
624 * marks as well as (optionally) enabling the transmission of
625 * XON frames.
626 */
627 fcrtl = hw->fc.low_water;
628 if (hw->fc.send_xon)
629 fcrtl |= E1000_FCRTL_XONE;
630
631 fcrth = hw->fc.high_water;
632 }
633 wr32(E1000_FCRTL, fcrtl);
634 wr32(E1000_FCRTH, fcrth);
635
636 return ret_val;
637}
638
639/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700640 * igb_set_default_fc - Set flow control default values
Auke Kok9d5c8242008-01-24 02:22:38 -0800641 * @hw: pointer to the HW structure
642 *
643 * Read the EEPROM for the default values for flow control and store the
644 * values.
645 **/
646static s32 igb_set_default_fc(struct e1000_hw *hw)
647{
648 s32 ret_val = 0;
649 u16 nvm_data;
650
651 /*
652 * Read and store word 0x0F of the EEPROM. This word contains bits
653 * that determine the hardware's default PAUSE (flow control) mode,
654 * a bit that determines whether the HW defaults to enabling or
655 * disabling auto-negotiation, and the direction of the
656 * SW defined pins. If there is no SW over-ride of the flow
657 * control setting, then the variable hw->fc will
658 * be initialized based on a value in the EEPROM.
659 */
Alexander Duyck312c75a2009-02-06 23:17:47 +0000660 ret_val = hw->nvm.ops.read(hw, NVM_INIT_CONTROL2_REG, 1, &nvm_data);
Auke Kok9d5c8242008-01-24 02:22:38 -0800661
662 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700663 hw_dbg("NVM Read Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800664 goto out;
665 }
666
667 if ((nvm_data & NVM_WORD0F_PAUSE_MASK) == 0)
Alexander Duyck0cce1192009-07-23 18:10:24 +0000668 hw->fc.requested_mode = e1000_fc_none;
Auke Kok9d5c8242008-01-24 02:22:38 -0800669 else if ((nvm_data & NVM_WORD0F_PAUSE_MASK) ==
670 NVM_WORD0F_ASM_DIR)
Alexander Duyck0cce1192009-07-23 18:10:24 +0000671 hw->fc.requested_mode = e1000_fc_tx_pause;
Auke Kok9d5c8242008-01-24 02:22:38 -0800672 else
Alexander Duyck0cce1192009-07-23 18:10:24 +0000673 hw->fc.requested_mode = e1000_fc_full;
Auke Kok9d5c8242008-01-24 02:22:38 -0800674
675out:
676 return ret_val;
677}
678
679/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700680 * igb_force_mac_fc - Force the MAC's flow control settings
Auke Kok9d5c8242008-01-24 02:22:38 -0800681 * @hw: pointer to the HW structure
682 *
683 * Force the MAC's flow control settings. Sets the TFCE and RFCE bits in the
684 * device control register to reflect the adapter settings. TFCE and RFCE
685 * need to be explicitly set by software when a copper PHY is used because
686 * autonegotiation is managed by the PHY rather than the MAC. Software must
687 * also configure these bits when link is forced on a fiber connection.
688 **/
689s32 igb_force_mac_fc(struct e1000_hw *hw)
690{
691 u32 ctrl;
692 s32 ret_val = 0;
693
694 ctrl = rd32(E1000_CTRL);
695
696 /*
697 * Because we didn't get link via the internal auto-negotiation
698 * mechanism (we either forced link or we got link via PHY
699 * auto-neg), we have to manually enable/disable transmit an
700 * receive flow control.
701 *
702 * The "Case" statement below enables/disable flow control
Alexander Duyck0cce1192009-07-23 18:10:24 +0000703 * according to the "hw->fc.current_mode" parameter.
Auke Kok9d5c8242008-01-24 02:22:38 -0800704 *
705 * The possible values of the "fc" parameter are:
706 * 0: Flow control is completely disabled
707 * 1: Rx flow control is enabled (we can receive pause
708 * frames but not send pause frames).
709 * 2: Tx flow control is enabled (we can send pause frames
710 * frames but we do not receive pause frames).
711 * 3: Both Rx and TX flow control (symmetric) is enabled.
712 * other: No other values should be possible at this point.
713 */
Alexander Duyck0cce1192009-07-23 18:10:24 +0000714 hw_dbg("hw->fc.current_mode = %u\n", hw->fc.current_mode);
Auke Kok9d5c8242008-01-24 02:22:38 -0800715
Alexander Duyck0cce1192009-07-23 18:10:24 +0000716 switch (hw->fc.current_mode) {
Auke Kok9d5c8242008-01-24 02:22:38 -0800717 case e1000_fc_none:
718 ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
719 break;
720 case e1000_fc_rx_pause:
721 ctrl &= (~E1000_CTRL_TFCE);
722 ctrl |= E1000_CTRL_RFCE;
723 break;
724 case e1000_fc_tx_pause:
725 ctrl &= (~E1000_CTRL_RFCE);
726 ctrl |= E1000_CTRL_TFCE;
727 break;
728 case e1000_fc_full:
729 ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
730 break;
731 default:
Auke Kok652fff32008-06-27 11:00:18 -0700732 hw_dbg("Flow control param set incorrectly\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800733 ret_val = -E1000_ERR_CONFIG;
734 goto out;
735 }
736
737 wr32(E1000_CTRL, ctrl);
738
739out:
740 return ret_val;
741}
742
743/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700744 * igb_config_fc_after_link_up - Configures flow control after link
Auke Kok9d5c8242008-01-24 02:22:38 -0800745 * @hw: pointer to the HW structure
746 *
747 * Checks the status of auto-negotiation after link up to ensure that the
748 * speed and duplex were not forced. If the link needed to be forced, then
749 * flow control needs to be forced also. If auto-negotiation is enabled
750 * and did not fail, then we configure flow control based on our link
751 * partner.
752 **/
753s32 igb_config_fc_after_link_up(struct e1000_hw *hw)
754{
755 struct e1000_mac_info *mac = &hw->mac;
756 s32 ret_val = 0;
757 u16 mii_status_reg, mii_nway_adv_reg, mii_nway_lp_ability_reg;
758 u16 speed, duplex;
759
760 /*
761 * Check for the case where we have fiber media and auto-neg failed
762 * so we had to force link. In this case, we need to force the
763 * configuration of the MAC to match the "fc" parameter.
764 */
765 if (mac->autoneg_failed) {
Alexander Duyckdcc3ae92009-07-23 18:07:20 +0000766 if (hw->phy.media_type == e1000_media_type_internal_serdes)
Auke Kok9d5c8242008-01-24 02:22:38 -0800767 ret_val = igb_force_mac_fc(hw);
768 } else {
769 if (hw->phy.media_type == e1000_media_type_copper)
770 ret_val = igb_force_mac_fc(hw);
771 }
772
773 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700774 hw_dbg("Error forcing flow control settings\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800775 goto out;
776 }
777
778 /*
779 * Check for the case where we have copper media and auto-neg is
780 * enabled. In this case, we need to check and see if Auto-Neg
781 * has completed, and if so, how the PHY and link partner has
782 * flow control configured.
783 */
784 if ((hw->phy.media_type == e1000_media_type_copper) && mac->autoneg) {
785 /*
786 * Read the MII Status Register and check to see if AutoNeg
787 * has completed. We read this twice because this reg has
788 * some "sticky" (latched) bits.
789 */
Alexander Duycka8d2a0c2009-02-06 23:17:26 +0000790 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS,
Auke Kok9d5c8242008-01-24 02:22:38 -0800791 &mii_status_reg);
792 if (ret_val)
793 goto out;
Alexander Duycka8d2a0c2009-02-06 23:17:26 +0000794 ret_val = hw->phy.ops.read_reg(hw, PHY_STATUS,
Auke Kok9d5c8242008-01-24 02:22:38 -0800795 &mii_status_reg);
796 if (ret_val)
797 goto out;
798
799 if (!(mii_status_reg & MII_SR_AUTONEG_COMPLETE)) {
Auke Kok652fff32008-06-27 11:00:18 -0700800 hw_dbg("Copper PHY and Auto Neg "
Auke Kok9d5c8242008-01-24 02:22:38 -0800801 "has not completed.\n");
802 goto out;
803 }
804
805 /*
806 * The AutoNeg process has completed, so we now need to
807 * read both the Auto Negotiation Advertisement
808 * Register (Address 4) and the Auto_Negotiation Base
809 * Page Ability Register (Address 5) to determine how
810 * flow control was negotiated.
811 */
Alexander Duycka8d2a0c2009-02-06 23:17:26 +0000812 ret_val = hw->phy.ops.read_reg(hw, PHY_AUTONEG_ADV,
Auke Kok9d5c8242008-01-24 02:22:38 -0800813 &mii_nway_adv_reg);
814 if (ret_val)
815 goto out;
Alexander Duycka8d2a0c2009-02-06 23:17:26 +0000816 ret_val = hw->phy.ops.read_reg(hw, PHY_LP_ABILITY,
Auke Kok9d5c8242008-01-24 02:22:38 -0800817 &mii_nway_lp_ability_reg);
818 if (ret_val)
819 goto out;
820
821 /*
822 * Two bits in the Auto Negotiation Advertisement Register
823 * (Address 4) and two bits in the Auto Negotiation Base
824 * Page Ability Register (Address 5) determine flow control
825 * for both the PHY and the link partner. The following
826 * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
827 * 1999, describes these PAUSE resolution bits and how flow
828 * control is determined based upon these settings.
829 * NOTE: DC = Don't Care
830 *
831 * LOCAL DEVICE | LINK PARTNER
832 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
833 *-------|---------|-------|---------|--------------------
834 * 0 | 0 | DC | DC | e1000_fc_none
835 * 0 | 1 | 0 | DC | e1000_fc_none
836 * 0 | 1 | 1 | 0 | e1000_fc_none
837 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
838 * 1 | 0 | 0 | DC | e1000_fc_none
839 * 1 | DC | 1 | DC | e1000_fc_full
840 * 1 | 1 | 0 | 0 | e1000_fc_none
841 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
842 *
843 * Are both PAUSE bits set to 1? If so, this implies
844 * Symmetric Flow Control is enabled at both ends. The
845 * ASM_DIR bits are irrelevant per the spec.
846 *
847 * For Symmetric Flow Control:
848 *
849 * LOCAL DEVICE | LINK PARTNER
850 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
851 *-------|---------|-------|---------|--------------------
852 * 1 | DC | 1 | DC | E1000_fc_full
853 *
854 */
855 if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
856 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
857 /*
858 * Now we need to check if the user selected RX ONLY
859 * of pause frames. In this case, we had to advertise
860 * FULL flow control because we could not advertise RX
861 * ONLY. Hence, we must now check to see if we need to
862 * turn OFF the TRANSMISSION of PAUSE frames.
863 */
Alexander Duyck0cce1192009-07-23 18:10:24 +0000864 if (hw->fc.requested_mode == e1000_fc_full) {
865 hw->fc.current_mode = e1000_fc_full;
Auke Kok652fff32008-06-27 11:00:18 -0700866 hw_dbg("Flow Control = FULL.\r\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800867 } else {
Alexander Duyck0cce1192009-07-23 18:10:24 +0000868 hw->fc.current_mode = e1000_fc_rx_pause;
Auke Kok652fff32008-06-27 11:00:18 -0700869 hw_dbg("Flow Control = "
870 "RX PAUSE frames only.\r\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800871 }
872 }
873 /*
874 * For receiving PAUSE frames ONLY.
875 *
876 * LOCAL DEVICE | LINK PARTNER
877 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
878 *-------|---------|-------|---------|--------------------
879 * 0 | 1 | 1 | 1 | e1000_fc_tx_pause
880 */
881 else if (!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
882 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
883 (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
884 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
Alexander Duyck0cce1192009-07-23 18:10:24 +0000885 hw->fc.current_mode = e1000_fc_tx_pause;
Auke Kok652fff32008-06-27 11:00:18 -0700886 hw_dbg("Flow Control = TX PAUSE frames only.\r\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800887 }
888 /*
889 * For transmitting PAUSE frames ONLY.
890 *
891 * LOCAL DEVICE | LINK PARTNER
892 * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
893 *-------|---------|-------|---------|--------------------
894 * 1 | 1 | 0 | 1 | e1000_fc_rx_pause
895 */
896 else if ((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
897 (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
898 !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
899 (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
Alexander Duyck0cce1192009-07-23 18:10:24 +0000900 hw->fc.current_mode = e1000_fc_rx_pause;
Auke Kok652fff32008-06-27 11:00:18 -0700901 hw_dbg("Flow Control = RX PAUSE frames only.\r\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800902 }
903 /*
904 * Per the IEEE spec, at this point flow control should be
905 * disabled. However, we want to consider that we could
906 * be connected to a legacy switch that doesn't advertise
907 * desired flow control, but can be forced on the link
908 * partner. So if we advertised no flow control, that is
909 * what we will resolve to. If we advertised some kind of
910 * receive capability (Rx Pause Only or Full Flow Control)
911 * and the link partner advertised none, we will configure
912 * ourselves to enable Rx Flow Control only. We can do
913 * this safely for two reasons: If the link partner really
914 * didn't want flow control enabled, and we enable Rx, no
915 * harm done since we won't be receiving any PAUSE frames
916 * anyway. If the intent on the link partner was to have
917 * flow control enabled, then by us enabling RX only, we
918 * can at least receive pause frames and process them.
919 * This is a good idea because in most cases, since we are
920 * predominantly a server NIC, more times than not we will
921 * be asked to delay transmission of packets than asking
922 * our link partner to pause transmission of frames.
923 */
Alexander Duyck0cce1192009-07-23 18:10:24 +0000924 else if ((hw->fc.requested_mode == e1000_fc_none ||
925 hw->fc.requested_mode == e1000_fc_tx_pause) ||
Auke Kok9d5c8242008-01-24 02:22:38 -0800926 hw->fc.strict_ieee) {
Alexander Duyck0cce1192009-07-23 18:10:24 +0000927 hw->fc.current_mode = e1000_fc_none;
Auke Kok652fff32008-06-27 11:00:18 -0700928 hw_dbg("Flow Control = NONE.\r\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800929 } else {
Alexander Duyck0cce1192009-07-23 18:10:24 +0000930 hw->fc.current_mode = e1000_fc_rx_pause;
Auke Kok652fff32008-06-27 11:00:18 -0700931 hw_dbg("Flow Control = RX PAUSE frames only.\r\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800932 }
933
934 /*
935 * Now we need to do one last check... If we auto-
936 * negotiated to HALF DUPLEX, flow control should not be
937 * enabled per IEEE 802.3 spec.
938 */
939 ret_val = hw->mac.ops.get_speed_and_duplex(hw, &speed, &duplex);
940 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700941 hw_dbg("Error getting link speed and duplex\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800942 goto out;
943 }
944
945 if (duplex == HALF_DUPLEX)
Alexander Duyck0cce1192009-07-23 18:10:24 +0000946 hw->fc.current_mode = e1000_fc_none;
Auke Kok9d5c8242008-01-24 02:22:38 -0800947
948 /*
949 * Now we call a subroutine to actually force the MAC
950 * controller to use the correct flow control settings.
951 */
952 ret_val = igb_force_mac_fc(hw);
953 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -0700954 hw_dbg("Error forcing flow control settings\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800955 goto out;
956 }
957 }
958
959out:
960 return ret_val;
961}
962
963/**
Jeff Kirsher733596b2008-06-27 10:59:59 -0700964 * igb_get_speed_and_duplex_copper - Retreive current speed/duplex
Auke Kok9d5c8242008-01-24 02:22:38 -0800965 * @hw: pointer to the HW structure
966 * @speed: stores the current speed
967 * @duplex: stores the current duplex
968 *
969 * Read the status register for the current speed/duplex and store the current
970 * speed and duplex for copper connections.
971 **/
972s32 igb_get_speed_and_duplex_copper(struct e1000_hw *hw, u16 *speed,
973 u16 *duplex)
974{
975 u32 status;
976
977 status = rd32(E1000_STATUS);
978 if (status & E1000_STATUS_SPEED_1000) {
979 *speed = SPEED_1000;
Auke Kok652fff32008-06-27 11:00:18 -0700980 hw_dbg("1000 Mbs, ");
Auke Kok9d5c8242008-01-24 02:22:38 -0800981 } else if (status & E1000_STATUS_SPEED_100) {
982 *speed = SPEED_100;
Auke Kok652fff32008-06-27 11:00:18 -0700983 hw_dbg("100 Mbs, ");
Auke Kok9d5c8242008-01-24 02:22:38 -0800984 } else {
985 *speed = SPEED_10;
Auke Kok652fff32008-06-27 11:00:18 -0700986 hw_dbg("10 Mbs, ");
Auke Kok9d5c8242008-01-24 02:22:38 -0800987 }
988
989 if (status & E1000_STATUS_FD) {
990 *duplex = FULL_DUPLEX;
Auke Kok652fff32008-06-27 11:00:18 -0700991 hw_dbg("Full Duplex\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800992 } else {
993 *duplex = HALF_DUPLEX;
Auke Kok652fff32008-06-27 11:00:18 -0700994 hw_dbg("Half Duplex\n");
Auke Kok9d5c8242008-01-24 02:22:38 -0800995 }
996
997 return 0;
998}
999
1000/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001001 * igb_get_hw_semaphore - Acquire hardware semaphore
Auke Kok9d5c8242008-01-24 02:22:38 -08001002 * @hw: pointer to the HW structure
1003 *
1004 * Acquire the HW semaphore to access the PHY or NVM
1005 **/
1006s32 igb_get_hw_semaphore(struct e1000_hw *hw)
1007{
1008 u32 swsm;
1009 s32 ret_val = 0;
1010 s32 timeout = hw->nvm.word_size + 1;
1011 s32 i = 0;
1012
1013 /* Get the SW semaphore */
1014 while (i < timeout) {
1015 swsm = rd32(E1000_SWSM);
1016 if (!(swsm & E1000_SWSM_SMBI))
1017 break;
1018
1019 udelay(50);
1020 i++;
1021 }
1022
1023 if (i == timeout) {
Auke Kok652fff32008-06-27 11:00:18 -07001024 hw_dbg("Driver can't access device - SMBI bit is set.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001025 ret_val = -E1000_ERR_NVM;
1026 goto out;
1027 }
1028
1029 /* Get the FW semaphore. */
1030 for (i = 0; i < timeout; i++) {
1031 swsm = rd32(E1000_SWSM);
1032 wr32(E1000_SWSM, swsm | E1000_SWSM_SWESMBI);
1033
1034 /* Semaphore acquired if bit latched */
1035 if (rd32(E1000_SWSM) & E1000_SWSM_SWESMBI)
1036 break;
1037
1038 udelay(50);
1039 }
1040
1041 if (i == timeout) {
1042 /* Release semaphores */
1043 igb_put_hw_semaphore(hw);
Auke Kok652fff32008-06-27 11:00:18 -07001044 hw_dbg("Driver can't access the NVM\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001045 ret_val = -E1000_ERR_NVM;
1046 goto out;
1047 }
1048
1049out:
1050 return ret_val;
1051}
1052
1053/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001054 * igb_put_hw_semaphore - Release hardware semaphore
Auke Kok9d5c8242008-01-24 02:22:38 -08001055 * @hw: pointer to the HW structure
1056 *
1057 * Release hardware semaphore used to access the PHY or NVM
1058 **/
1059void igb_put_hw_semaphore(struct e1000_hw *hw)
1060{
1061 u32 swsm;
1062
1063 swsm = rd32(E1000_SWSM);
1064
1065 swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
1066
1067 wr32(E1000_SWSM, swsm);
1068}
1069
1070/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001071 * igb_get_auto_rd_done - Check for auto read completion
Auke Kok9d5c8242008-01-24 02:22:38 -08001072 * @hw: pointer to the HW structure
1073 *
1074 * Check EEPROM for Auto Read done bit.
1075 **/
1076s32 igb_get_auto_rd_done(struct e1000_hw *hw)
1077{
1078 s32 i = 0;
1079 s32 ret_val = 0;
1080
1081
1082 while (i < AUTO_READ_DONE_TIMEOUT) {
1083 if (rd32(E1000_EECD) & E1000_EECD_AUTO_RD)
1084 break;
1085 msleep(1);
1086 i++;
1087 }
1088
1089 if (i == AUTO_READ_DONE_TIMEOUT) {
Auke Kok652fff32008-06-27 11:00:18 -07001090 hw_dbg("Auto read by HW from NVM has not completed.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001091 ret_val = -E1000_ERR_RESET;
1092 goto out;
1093 }
1094
1095out:
1096 return ret_val;
1097}
1098
1099/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001100 * igb_valid_led_default - Verify a valid default LED config
Auke Kok9d5c8242008-01-24 02:22:38 -08001101 * @hw: pointer to the HW structure
1102 * @data: pointer to the NVM (EEPROM)
1103 *
1104 * Read the EEPROM for the current default LED configuration. If the
1105 * LED configuration is not valid, set to a valid LED configuration.
1106 **/
1107static s32 igb_valid_led_default(struct e1000_hw *hw, u16 *data)
1108{
1109 s32 ret_val;
1110
Alexander Duyck312c75a2009-02-06 23:17:47 +00001111 ret_val = hw->nvm.ops.read(hw, NVM_ID_LED_SETTINGS, 1, data);
Auke Kok9d5c8242008-01-24 02:22:38 -08001112 if (ret_val) {
Auke Kok652fff32008-06-27 11:00:18 -07001113 hw_dbg("NVM Read Error\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001114 goto out;
1115 }
1116
Alexander Duyck099e1cb2009-07-23 18:07:40 +00001117 if (*data == ID_LED_RESERVED_0000 || *data == ID_LED_RESERVED_FFFF) {
1118 switch(hw->phy.media_type) {
1119 case e1000_media_type_internal_serdes:
1120 *data = ID_LED_DEFAULT_82575_SERDES;
1121 break;
1122 case e1000_media_type_copper:
1123 default:
1124 *data = ID_LED_DEFAULT;
1125 break;
1126 }
1127 }
Auke Kok9d5c8242008-01-24 02:22:38 -08001128out:
1129 return ret_val;
1130}
1131
1132/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001133 * igb_id_led_init -
Auke Kok9d5c8242008-01-24 02:22:38 -08001134 * @hw: pointer to the HW structure
1135 *
1136 **/
1137s32 igb_id_led_init(struct e1000_hw *hw)
1138{
1139 struct e1000_mac_info *mac = &hw->mac;
1140 s32 ret_val;
1141 const u32 ledctl_mask = 0x000000FF;
1142 const u32 ledctl_on = E1000_LEDCTL_MODE_LED_ON;
1143 const u32 ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
1144 u16 data, i, temp;
1145 const u16 led_mask = 0x0F;
1146
1147 ret_val = igb_valid_led_default(hw, &data);
1148 if (ret_val)
1149 goto out;
1150
1151 mac->ledctl_default = rd32(E1000_LEDCTL);
1152 mac->ledctl_mode1 = mac->ledctl_default;
1153 mac->ledctl_mode2 = mac->ledctl_default;
1154
1155 for (i = 0; i < 4; i++) {
1156 temp = (data >> (i << 2)) & led_mask;
1157 switch (temp) {
1158 case ID_LED_ON1_DEF2:
1159 case ID_LED_ON1_ON2:
1160 case ID_LED_ON1_OFF2:
1161 mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1162 mac->ledctl_mode1 |= ledctl_on << (i << 3);
1163 break;
1164 case ID_LED_OFF1_DEF2:
1165 case ID_LED_OFF1_ON2:
1166 case ID_LED_OFF1_OFF2:
1167 mac->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
1168 mac->ledctl_mode1 |= ledctl_off << (i << 3);
1169 break;
1170 default:
1171 /* Do nothing */
1172 break;
1173 }
1174 switch (temp) {
1175 case ID_LED_DEF1_ON2:
1176 case ID_LED_ON1_ON2:
1177 case ID_LED_OFF1_ON2:
1178 mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1179 mac->ledctl_mode2 |= ledctl_on << (i << 3);
1180 break;
1181 case ID_LED_DEF1_OFF2:
1182 case ID_LED_ON1_OFF2:
1183 case ID_LED_OFF1_OFF2:
1184 mac->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
1185 mac->ledctl_mode2 |= ledctl_off << (i << 3);
1186 break;
1187 default:
1188 /* Do nothing */
1189 break;
1190 }
1191 }
1192
1193out:
1194 return ret_val;
1195}
1196
1197/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001198 * igb_cleanup_led - Set LED config to default operation
Auke Kok9d5c8242008-01-24 02:22:38 -08001199 * @hw: pointer to the HW structure
1200 *
1201 * Remove the current LED configuration and set the LED configuration
1202 * to the default value, saved from the EEPROM.
1203 **/
1204s32 igb_cleanup_led(struct e1000_hw *hw)
1205{
1206 wr32(E1000_LEDCTL, hw->mac.ledctl_default);
1207 return 0;
1208}
1209
1210/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001211 * igb_blink_led - Blink LED
Auke Kok9d5c8242008-01-24 02:22:38 -08001212 * @hw: pointer to the HW structure
1213 *
1214 * Blink the led's which are set to be on.
1215 **/
1216s32 igb_blink_led(struct e1000_hw *hw)
1217{
1218 u32 ledctl_blink = 0;
1219 u32 i;
1220
Alexander Duyckdcc3ae92009-07-23 18:07:20 +00001221 /*
1222 * set the blink bit for each LED that's "on" (0x0E)
1223 * in ledctl_mode2
1224 */
1225 ledctl_blink = hw->mac.ledctl_mode2;
1226 for (i = 0; i < 4; i++)
1227 if (((hw->mac.ledctl_mode2 >> (i * 8)) & 0xFF) ==
1228 E1000_LEDCTL_MODE_LED_ON)
1229 ledctl_blink |= (E1000_LEDCTL_LED0_BLINK <<
1230 (i * 8));
Auke Kok9d5c8242008-01-24 02:22:38 -08001231
1232 wr32(E1000_LEDCTL, ledctl_blink);
1233
1234 return 0;
1235}
1236
1237/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001238 * igb_led_off - Turn LED off
Auke Kok9d5c8242008-01-24 02:22:38 -08001239 * @hw: pointer to the HW structure
1240 *
1241 * Turn LED off.
1242 **/
1243s32 igb_led_off(struct e1000_hw *hw)
1244{
Auke Kok9d5c8242008-01-24 02:22:38 -08001245 switch (hw->phy.media_type) {
Auke Kok9d5c8242008-01-24 02:22:38 -08001246 case e1000_media_type_copper:
1247 wr32(E1000_LEDCTL, hw->mac.ledctl_mode1);
1248 break;
1249 default:
1250 break;
1251 }
1252
1253 return 0;
1254}
1255
1256/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001257 * igb_disable_pcie_master - Disables PCI-express master access
Auke Kok9d5c8242008-01-24 02:22:38 -08001258 * @hw: pointer to the HW structure
1259 *
1260 * Returns 0 (0) if successful, else returns -10
1261 * (-E1000_ERR_MASTER_REQUESTS_PENDING) if master disable bit has not casued
1262 * the master requests to be disabled.
1263 *
1264 * Disables PCI-Express master access and verifies there are no pending
1265 * requests.
1266 **/
1267s32 igb_disable_pcie_master(struct e1000_hw *hw)
1268{
1269 u32 ctrl;
1270 s32 timeout = MASTER_DISABLE_TIMEOUT;
1271 s32 ret_val = 0;
1272
1273 if (hw->bus.type != e1000_bus_type_pci_express)
1274 goto out;
1275
1276 ctrl = rd32(E1000_CTRL);
1277 ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
1278 wr32(E1000_CTRL, ctrl);
1279
1280 while (timeout) {
1281 if (!(rd32(E1000_STATUS) &
1282 E1000_STATUS_GIO_MASTER_ENABLE))
1283 break;
1284 udelay(100);
1285 timeout--;
1286 }
1287
1288 if (!timeout) {
Auke Kok652fff32008-06-27 11:00:18 -07001289 hw_dbg("Master requests are pending.\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001290 ret_val = -E1000_ERR_MASTER_REQUESTS_PENDING;
1291 goto out;
1292 }
1293
1294out:
1295 return ret_val;
1296}
1297
1298/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001299 * igb_reset_adaptive - Reset Adaptive Interframe Spacing
Auke Kok9d5c8242008-01-24 02:22:38 -08001300 * @hw: pointer to the HW structure
1301 *
1302 * Reset the Adaptive Interframe Spacing throttle to default values.
1303 **/
1304void igb_reset_adaptive(struct e1000_hw *hw)
1305{
1306 struct e1000_mac_info *mac = &hw->mac;
1307
1308 if (!mac->adaptive_ifs) {
Auke Kok652fff32008-06-27 11:00:18 -07001309 hw_dbg("Not in Adaptive IFS mode!\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001310 goto out;
1311 }
1312
1313 if (!mac->ifs_params_forced) {
1314 mac->current_ifs_val = 0;
1315 mac->ifs_min_val = IFS_MIN;
1316 mac->ifs_max_val = IFS_MAX;
1317 mac->ifs_step_size = IFS_STEP;
1318 mac->ifs_ratio = IFS_RATIO;
1319 }
1320
1321 mac->in_ifs_mode = false;
1322 wr32(E1000_AIT, 0);
1323out:
1324 return;
1325}
1326
1327/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001328 * igb_update_adaptive - Update Adaptive Interframe Spacing
Auke Kok9d5c8242008-01-24 02:22:38 -08001329 * @hw: pointer to the HW structure
1330 *
1331 * Update the Adaptive Interframe Spacing Throttle value based on the
1332 * time between transmitted packets and time between collisions.
1333 **/
1334void igb_update_adaptive(struct e1000_hw *hw)
1335{
1336 struct e1000_mac_info *mac = &hw->mac;
1337
1338 if (!mac->adaptive_ifs) {
Auke Kok652fff32008-06-27 11:00:18 -07001339 hw_dbg("Not in Adaptive IFS mode!\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001340 goto out;
1341 }
1342
1343 if ((mac->collision_delta * mac->ifs_ratio) > mac->tx_packet_delta) {
1344 if (mac->tx_packet_delta > MIN_NUM_XMITS) {
1345 mac->in_ifs_mode = true;
1346 if (mac->current_ifs_val < mac->ifs_max_val) {
1347 if (!mac->current_ifs_val)
1348 mac->current_ifs_val = mac->ifs_min_val;
1349 else
1350 mac->current_ifs_val +=
1351 mac->ifs_step_size;
1352 wr32(E1000_AIT,
1353 mac->current_ifs_val);
1354 }
1355 }
1356 } else {
1357 if (mac->in_ifs_mode &&
1358 (mac->tx_packet_delta <= MIN_NUM_XMITS)) {
1359 mac->current_ifs_val = 0;
1360 mac->in_ifs_mode = false;
1361 wr32(E1000_AIT, 0);
1362 }
1363 }
1364out:
1365 return;
1366}
1367
1368/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001369 * igb_validate_mdi_setting - Verify MDI/MDIx settings
Auke Kok9d5c8242008-01-24 02:22:38 -08001370 * @hw: pointer to the HW structure
1371 *
1372 * Verify that when not using auto-negotitation that MDI/MDIx is correctly
1373 * set, which is forced to MDI mode only.
1374 **/
1375s32 igb_validate_mdi_setting(struct e1000_hw *hw)
1376{
1377 s32 ret_val = 0;
1378
1379 if (!hw->mac.autoneg && (hw->phy.mdix == 0 || hw->phy.mdix == 3)) {
Auke Kok652fff32008-06-27 11:00:18 -07001380 hw_dbg("Invalid MDI setting detected\n");
Auke Kok9d5c8242008-01-24 02:22:38 -08001381 hw->phy.mdix = 1;
1382 ret_val = -E1000_ERR_CONFIG;
1383 goto out;
1384 }
1385
1386out:
1387 return ret_val;
1388}
1389
1390/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001391 * igb_write_8bit_ctrl_reg - Write a 8bit CTRL register
Auke Kok9d5c8242008-01-24 02:22:38 -08001392 * @hw: pointer to the HW structure
1393 * @reg: 32bit register offset such as E1000_SCTL
1394 * @offset: register offset to write to
1395 * @data: data to write at register offset
1396 *
1397 * Writes an address/data control type register. There are several of these
1398 * and they all have the format address << 8 | data and bit 31 is polled for
1399 * completion.
1400 **/
1401s32 igb_write_8bit_ctrl_reg(struct e1000_hw *hw, u32 reg,
1402 u32 offset, u8 data)
1403{
1404 u32 i, regvalue = 0;
1405 s32 ret_val = 0;
1406
1407 /* Set up the address and data */
1408 regvalue = ((u32)data) | (offset << E1000_GEN_CTL_ADDRESS_SHIFT);
1409 wr32(reg, regvalue);
1410
1411 /* Poll the ready bit to see if the MDI read completed */
1412 for (i = 0; i < E1000_GEN_POLL_TIMEOUT; i++) {
1413 udelay(5);
1414 regvalue = rd32(reg);
1415 if (regvalue & E1000_GEN_CTL_READY)
1416 break;
1417 }
1418 if (!(regvalue & E1000_GEN_CTL_READY)) {
Auke Kok652fff32008-06-27 11:00:18 -07001419 hw_dbg("Reg %08x did not indicate ready\n", reg);
Auke Kok9d5c8242008-01-24 02:22:38 -08001420 ret_val = -E1000_ERR_PHY;
1421 goto out;
1422 }
1423
1424out:
1425 return ret_val;
1426}
1427
1428/**
Jeff Kirsher733596b2008-06-27 10:59:59 -07001429 * igb_enable_mng_pass_thru - Enable processing of ARP's
Auke Kok9d5c8242008-01-24 02:22:38 -08001430 * @hw: pointer to the HW structure
1431 *
1432 * Verifies the hardware needs to allow ARPs to be processed by the host.
1433 **/
1434bool igb_enable_mng_pass_thru(struct e1000_hw *hw)
1435{
1436 u32 manc;
1437 u32 fwsm, factps;
1438 bool ret_val = false;
1439
1440 if (!hw->mac.asf_firmware_present)
1441 goto out;
1442
1443 manc = rd32(E1000_MANC);
1444
1445 if (!(manc & E1000_MANC_RCV_TCO_EN) ||
1446 !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
1447 goto out;
1448
1449 if (hw->mac.arc_subsystem_valid) {
1450 fwsm = rd32(E1000_FWSM);
1451 factps = rd32(E1000_FACTPS);
1452
1453 if (!(factps & E1000_FACTPS_MNGCG) &&
1454 ((fwsm & E1000_FWSM_MODE_MASK) ==
1455 (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT))) {
1456 ret_val = true;
1457 goto out;
1458 }
1459 } else {
1460 if ((manc & E1000_MANC_SMBUS_EN) &&
1461 !(manc & E1000_MANC_ASF_EN)) {
1462 ret_val = true;
1463 goto out;
1464 }
1465 }
1466
1467out:
1468 return ret_val;
1469}