Merge branch 'perf-fixes-for-linus-2' of git://git.kernel.org/pub/scm/linux/kernel...
[sfrench/cifs-2.6.git] / drivers / net / igb / e1000_82575.c
1 /*******************************************************************************
2
3   Intel(R) Gigabit Ethernet Linux driver
4   Copyright(c) 2007-2009 Intel Corporation.
5
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 /* e1000_82575
29  * e1000_82576
30  */
31
32 #include <linux/types.h>
33 #include <linux/slab.h>
34 #include <linux/if_ether.h>
35
36 #include "e1000_mac.h"
37 #include "e1000_82575.h"
38
39 static s32  igb_get_invariants_82575(struct e1000_hw *);
40 static s32  igb_acquire_phy_82575(struct e1000_hw *);
41 static void igb_release_phy_82575(struct e1000_hw *);
42 static s32  igb_acquire_nvm_82575(struct e1000_hw *);
43 static void igb_release_nvm_82575(struct e1000_hw *);
44 static s32  igb_check_for_link_82575(struct e1000_hw *);
45 static s32  igb_get_cfg_done_82575(struct e1000_hw *);
46 static s32  igb_init_hw_82575(struct e1000_hw *);
47 static s32  igb_phy_hw_reset_sgmii_82575(struct e1000_hw *);
48 static s32  igb_read_phy_reg_sgmii_82575(struct e1000_hw *, u32, u16 *);
49 static s32  igb_read_phy_reg_82580(struct e1000_hw *, u32, u16 *);
50 static s32  igb_write_phy_reg_82580(struct e1000_hw *, u32, u16);
51 static s32  igb_reset_hw_82575(struct e1000_hw *);
52 static s32  igb_reset_hw_82580(struct e1000_hw *);
53 static s32  igb_set_d0_lplu_state_82575(struct e1000_hw *, bool);
54 static s32  igb_setup_copper_link_82575(struct e1000_hw *);
55 static s32  igb_setup_serdes_link_82575(struct e1000_hw *);
56 static s32  igb_write_phy_reg_sgmii_82575(struct e1000_hw *, u32, u16);
57 static void igb_clear_hw_cntrs_82575(struct e1000_hw *);
58 static s32  igb_acquire_swfw_sync_82575(struct e1000_hw *, u16);
59 static s32  igb_get_pcs_speed_and_duplex_82575(struct e1000_hw *, u16 *,
60                                                  u16 *);
61 static s32  igb_get_phy_id_82575(struct e1000_hw *);
62 static void igb_release_swfw_sync_82575(struct e1000_hw *, u16);
63 static bool igb_sgmii_active_82575(struct e1000_hw *);
64 static s32  igb_reset_init_script_82575(struct e1000_hw *);
65 static s32  igb_read_mac_addr_82575(struct e1000_hw *);
66 static s32  igb_set_pcie_completion_timeout(struct e1000_hw *hw);
67
68 static const u16 e1000_82580_rxpbs_table[] =
69         { 36, 72, 144, 1, 2, 4, 8, 16,
70           35, 70, 140 };
71 #define E1000_82580_RXPBS_TABLE_SIZE \
72         (sizeof(e1000_82580_rxpbs_table)/sizeof(u16))
73
74 static s32 igb_get_invariants_82575(struct e1000_hw *hw)
75 {
76         struct e1000_phy_info *phy = &hw->phy;
77         struct e1000_nvm_info *nvm = &hw->nvm;
78         struct e1000_mac_info *mac = &hw->mac;
79         struct e1000_dev_spec_82575 * dev_spec = &hw->dev_spec._82575;
80         u32 eecd;
81         s32 ret_val;
82         u16 size;
83         u32 ctrl_ext = 0;
84
85         switch (hw->device_id) {
86         case E1000_DEV_ID_82575EB_COPPER:
87         case E1000_DEV_ID_82575EB_FIBER_SERDES:
88         case E1000_DEV_ID_82575GB_QUAD_COPPER:
89                 mac->type = e1000_82575;
90                 break;
91         case E1000_DEV_ID_82576:
92         case E1000_DEV_ID_82576_NS:
93         case E1000_DEV_ID_82576_NS_SERDES:
94         case E1000_DEV_ID_82576_FIBER:
95         case E1000_DEV_ID_82576_SERDES:
96         case E1000_DEV_ID_82576_QUAD_COPPER:
97         case E1000_DEV_ID_82576_SERDES_QUAD:
98                 mac->type = e1000_82576;
99                 break;
100         case E1000_DEV_ID_82580_COPPER:
101         case E1000_DEV_ID_82580_FIBER:
102         case E1000_DEV_ID_82580_SERDES:
103         case E1000_DEV_ID_82580_SGMII:
104         case E1000_DEV_ID_82580_COPPER_DUAL:
105                 mac->type = e1000_82580;
106                 break;
107         default:
108                 return -E1000_ERR_MAC_INIT;
109                 break;
110         }
111
112         /* Set media type */
113         /*
114          * The 82575 uses bits 22:23 for link mode. The mode can be changed
115          * based on the EEPROM. We cannot rely upon device ID. There
116          * is no distinguishable difference between fiber and internal
117          * SerDes mode on the 82575. There can be an external PHY attached
118          * on the SGMII interface. For this, we'll set sgmii_active to true.
119          */
120         phy->media_type = e1000_media_type_copper;
121         dev_spec->sgmii_active = false;
122
123         ctrl_ext = rd32(E1000_CTRL_EXT);
124         switch (ctrl_ext & E1000_CTRL_EXT_LINK_MODE_MASK) {
125         case E1000_CTRL_EXT_LINK_MODE_SGMII:
126                 dev_spec->sgmii_active = true;
127                 ctrl_ext |= E1000_CTRL_I2C_ENA;
128                 break;
129         case E1000_CTRL_EXT_LINK_MODE_1000BASE_KX:
130         case E1000_CTRL_EXT_LINK_MODE_PCIE_SERDES:
131                 hw->phy.media_type = e1000_media_type_internal_serdes;
132                 ctrl_ext |= E1000_CTRL_I2C_ENA;
133                 break;
134         default:
135                 ctrl_ext &= ~E1000_CTRL_I2C_ENA;
136                 break;
137         }
138
139         wr32(E1000_CTRL_EXT, ctrl_ext);
140
141         /*
142          * if using i2c make certain the MDICNFG register is cleared to prevent
143          * communications from being misrouted to the mdic registers
144          */
145         if ((ctrl_ext & E1000_CTRL_I2C_ENA) && (hw->mac.type == e1000_82580))
146                 wr32(E1000_MDICNFG, 0);
147
148         /* Set mta register count */
149         mac->mta_reg_count = 128;
150         /* Set rar entry count */
151         mac->rar_entry_count = E1000_RAR_ENTRIES_82575;
152         if (mac->type == e1000_82576)
153                 mac->rar_entry_count = E1000_RAR_ENTRIES_82576;
154         if (mac->type == e1000_82580)
155                 mac->rar_entry_count = E1000_RAR_ENTRIES_82580;
156         /* reset */
157         if (mac->type == e1000_82580)
158                 mac->ops.reset_hw = igb_reset_hw_82580;
159         else
160                 mac->ops.reset_hw = igb_reset_hw_82575;
161         /* Set if part includes ASF firmware */
162         mac->asf_firmware_present = true;
163         /* Set if manageability features are enabled. */
164         mac->arc_subsystem_valid =
165                 (rd32(E1000_FWSM) & E1000_FWSM_MODE_MASK)
166                         ? true : false;
167
168         /* physical interface link setup */
169         mac->ops.setup_physical_interface =
170                 (hw->phy.media_type == e1000_media_type_copper)
171                         ? igb_setup_copper_link_82575
172                         : igb_setup_serdes_link_82575;
173
174         /* NVM initialization */
175         eecd = rd32(E1000_EECD);
176
177         nvm->opcode_bits        = 8;
178         nvm->delay_usec         = 1;
179         switch (nvm->override) {
180         case e1000_nvm_override_spi_large:
181                 nvm->page_size    = 32;
182                 nvm->address_bits = 16;
183                 break;
184         case e1000_nvm_override_spi_small:
185                 nvm->page_size    = 8;
186                 nvm->address_bits = 8;
187                 break;
188         default:
189                 nvm->page_size    = eecd & E1000_EECD_ADDR_BITS ? 32 : 8;
190                 nvm->address_bits = eecd & E1000_EECD_ADDR_BITS ? 16 : 8;
191                 break;
192         }
193
194         nvm->type = e1000_nvm_eeprom_spi;
195
196         size = (u16)((eecd & E1000_EECD_SIZE_EX_MASK) >>
197                      E1000_EECD_SIZE_EX_SHIFT);
198
199         /*
200          * Added to a constant, "size" becomes the left-shift value
201          * for setting word_size.
202          */
203         size += NVM_WORD_SIZE_BASE_SHIFT;
204
205         /* EEPROM access above 16k is unsupported */
206         if (size > 14)
207                 size = 14;
208         nvm->word_size = 1 << size;
209
210         /* if 82576 then initialize mailbox parameters */
211         if (mac->type == e1000_82576)
212                 igb_init_mbx_params_pf(hw);
213
214         /* setup PHY parameters */
215         if (phy->media_type != e1000_media_type_copper) {
216                 phy->type = e1000_phy_none;
217                 return 0;
218         }
219
220         phy->autoneg_mask        = AUTONEG_ADVERTISE_SPEED_DEFAULT;
221         phy->reset_delay_us      = 100;
222
223         /* PHY function pointers */
224         if (igb_sgmii_active_82575(hw)) {
225                 phy->ops.reset              = igb_phy_hw_reset_sgmii_82575;
226                 phy->ops.read_reg           = igb_read_phy_reg_sgmii_82575;
227                 phy->ops.write_reg          = igb_write_phy_reg_sgmii_82575;
228         } else if (hw->mac.type == e1000_82580) {
229                 phy->ops.reset              = igb_phy_hw_reset;
230                 phy->ops.read_reg           = igb_read_phy_reg_82580;
231                 phy->ops.write_reg          = igb_write_phy_reg_82580;
232         } else {
233                 phy->ops.reset              = igb_phy_hw_reset;
234                 phy->ops.read_reg           = igb_read_phy_reg_igp;
235                 phy->ops.write_reg          = igb_write_phy_reg_igp;
236         }
237
238         /* set lan id */
239         hw->bus.func = (rd32(E1000_STATUS) & E1000_STATUS_FUNC_MASK) >>
240                        E1000_STATUS_FUNC_SHIFT;
241
242         /* Set phy->phy_addr and phy->id. */
243         ret_val = igb_get_phy_id_82575(hw);
244         if (ret_val)
245                 return ret_val;
246
247         /* Verify phy id and set remaining function pointers */
248         switch (phy->id) {
249         case M88E1111_I_PHY_ID:
250                 phy->type                   = e1000_phy_m88;
251                 phy->ops.get_phy_info       = igb_get_phy_info_m88;
252                 phy->ops.get_cable_length   = igb_get_cable_length_m88;
253                 phy->ops.force_speed_duplex = igb_phy_force_speed_duplex_m88;
254                 break;
255         case IGP03E1000_E_PHY_ID:
256                 phy->type                   = e1000_phy_igp_3;
257                 phy->ops.get_phy_info       = igb_get_phy_info_igp;
258                 phy->ops.get_cable_length   = igb_get_cable_length_igp_2;
259                 phy->ops.force_speed_duplex = igb_phy_force_speed_duplex_igp;
260                 phy->ops.set_d0_lplu_state  = igb_set_d0_lplu_state_82575;
261                 phy->ops.set_d3_lplu_state  = igb_set_d3_lplu_state;
262                 break;
263         case I82580_I_PHY_ID:
264                 phy->type                   = e1000_phy_82580;
265                 phy->ops.force_speed_duplex = igb_phy_force_speed_duplex_82580;
266                 phy->ops.get_cable_length   = igb_get_cable_length_82580;
267                 phy->ops.get_phy_info       = igb_get_phy_info_82580;
268                 break;
269         default:
270                 return -E1000_ERR_PHY;
271         }
272
273         return 0;
274 }
275
276 /**
277  *  igb_acquire_phy_82575 - Acquire rights to access PHY
278  *  @hw: pointer to the HW structure
279  *
280  *  Acquire access rights to the correct PHY.  This is a
281  *  function pointer entry point called by the api module.
282  **/
283 static s32 igb_acquire_phy_82575(struct e1000_hw *hw)
284 {
285         u16 mask = E1000_SWFW_PHY0_SM;
286
287         if (hw->bus.func == E1000_FUNC_1)
288                 mask = E1000_SWFW_PHY1_SM;
289
290         return igb_acquire_swfw_sync_82575(hw, mask);
291 }
292
293 /**
294  *  igb_release_phy_82575 - Release rights to access PHY
295  *  @hw: pointer to the HW structure
296  *
297  *  A wrapper to release access rights to the correct PHY.  This is a
298  *  function pointer entry point called by the api module.
299  **/
300 static void igb_release_phy_82575(struct e1000_hw *hw)
301 {
302         u16 mask = E1000_SWFW_PHY0_SM;
303
304         if (hw->bus.func == E1000_FUNC_1)
305                 mask = E1000_SWFW_PHY1_SM;
306
307         igb_release_swfw_sync_82575(hw, mask);
308 }
309
310 /**
311  *  igb_read_phy_reg_sgmii_82575 - Read PHY register using sgmii
312  *  @hw: pointer to the HW structure
313  *  @offset: register offset to be read
314  *  @data: pointer to the read data
315  *
316  *  Reads the PHY register at offset using the serial gigabit media independent
317  *  interface and stores the retrieved information in data.
318  **/
319 static s32 igb_read_phy_reg_sgmii_82575(struct e1000_hw *hw, u32 offset,
320                                           u16 *data)
321 {
322         s32 ret_val = -E1000_ERR_PARAM;
323
324         if (offset > E1000_MAX_SGMII_PHY_REG_ADDR) {
325                 hw_dbg("PHY Address %u is out of range\n", offset);
326                 goto out;
327         }
328
329         ret_val = hw->phy.ops.acquire(hw);
330         if (ret_val)
331                 goto out;
332
333         ret_val = igb_read_phy_reg_i2c(hw, offset, data);
334
335         hw->phy.ops.release(hw);
336
337 out:
338         return ret_val;
339 }
340
341 /**
342  *  igb_write_phy_reg_sgmii_82575 - Write PHY register using sgmii
343  *  @hw: pointer to the HW structure
344  *  @offset: register offset to write to
345  *  @data: data to write at register offset
346  *
347  *  Writes the data to PHY register at the offset using the serial gigabit
348  *  media independent interface.
349  **/
350 static s32 igb_write_phy_reg_sgmii_82575(struct e1000_hw *hw, u32 offset,
351                                            u16 data)
352 {
353         s32 ret_val = -E1000_ERR_PARAM;
354
355
356         if (offset > E1000_MAX_SGMII_PHY_REG_ADDR) {
357                 hw_dbg("PHY Address %d is out of range\n", offset);
358                 goto out;
359         }
360
361         ret_val = hw->phy.ops.acquire(hw);
362         if (ret_val)
363                 goto out;
364
365         ret_val = igb_write_phy_reg_i2c(hw, offset, data);
366
367         hw->phy.ops.release(hw);
368
369 out:
370         return ret_val;
371 }
372
373 /**
374  *  igb_get_phy_id_82575 - Retrieve PHY addr and id
375  *  @hw: pointer to the HW structure
376  *
377  *  Retrieves the PHY address and ID for both PHY's which do and do not use
378  *  sgmi interface.
379  **/
380 static s32 igb_get_phy_id_82575(struct e1000_hw *hw)
381 {
382         struct e1000_phy_info *phy = &hw->phy;
383         s32  ret_val = 0;
384         u16 phy_id;
385         u32 ctrl_ext;
386
387         /*
388          * For SGMII PHYs, we try the list of possible addresses until
389          * we find one that works.  For non-SGMII PHYs
390          * (e.g. integrated copper PHYs), an address of 1 should
391          * work.  The result of this function should mean phy->phy_addr
392          * and phy->id are set correctly.
393          */
394         if (!(igb_sgmii_active_82575(hw))) {
395                 phy->addr = 1;
396                 ret_val = igb_get_phy_id(hw);
397                 goto out;
398         }
399
400         /* Power on sgmii phy if it is disabled */
401         ctrl_ext = rd32(E1000_CTRL_EXT);
402         wr32(E1000_CTRL_EXT, ctrl_ext & ~E1000_CTRL_EXT_SDP3_DATA);
403         wrfl();
404         msleep(300);
405
406         /*
407          * The address field in the I2CCMD register is 3 bits and 0 is invalid.
408          * Therefore, we need to test 1-7
409          */
410         for (phy->addr = 1; phy->addr < 8; phy->addr++) {
411                 ret_val = igb_read_phy_reg_sgmii_82575(hw, PHY_ID1, &phy_id);
412                 if (ret_val == 0) {
413                         hw_dbg("Vendor ID 0x%08X read at address %u\n",
414                                phy_id, phy->addr);
415                         /*
416                          * At the time of this writing, The M88 part is
417                          * the only supported SGMII PHY product.
418                          */
419                         if (phy_id == M88_VENDOR)
420                                 break;
421                 } else {
422                         hw_dbg("PHY address %u was unreadable\n", phy->addr);
423                 }
424         }
425
426         /* A valid PHY type couldn't be found. */
427         if (phy->addr == 8) {
428                 phy->addr = 0;
429                 ret_val = -E1000_ERR_PHY;
430                 goto out;
431         } else {
432                 ret_val = igb_get_phy_id(hw);
433         }
434
435         /* restore previous sfp cage power state */
436         wr32(E1000_CTRL_EXT, ctrl_ext);
437
438 out:
439         return ret_val;
440 }
441
442 /**
443  *  igb_phy_hw_reset_sgmii_82575 - Performs a PHY reset
444  *  @hw: pointer to the HW structure
445  *
446  *  Resets the PHY using the serial gigabit media independent interface.
447  **/
448 static s32 igb_phy_hw_reset_sgmii_82575(struct e1000_hw *hw)
449 {
450         s32 ret_val;
451
452         /*
453          * This isn't a true "hard" reset, but is the only reset
454          * available to us at this time.
455          */
456
457         hw_dbg("Soft resetting SGMII attached PHY...\n");
458
459         /*
460          * SFP documentation requires the following to configure the SPF module
461          * to work on SGMII.  No further documentation is given.
462          */
463         ret_val = hw->phy.ops.write_reg(hw, 0x1B, 0x8084);
464         if (ret_val)
465                 goto out;
466
467         ret_val = igb_phy_sw_reset(hw);
468
469 out:
470         return ret_val;
471 }
472
473 /**
474  *  igb_set_d0_lplu_state_82575 - Set Low Power Linkup D0 state
475  *  @hw: pointer to the HW structure
476  *  @active: true to enable LPLU, false to disable
477  *
478  *  Sets the LPLU D0 state according to the active flag.  When
479  *  activating LPLU this function also disables smart speed
480  *  and vice versa.  LPLU will not be activated unless the
481  *  device autonegotiation advertisement meets standards of
482  *  either 10 or 10/100 or 10/100/1000 at all duplexes.
483  *  This is a function pointer entry point only called by
484  *  PHY setup routines.
485  **/
486 static s32 igb_set_d0_lplu_state_82575(struct e1000_hw *hw, bool active)
487 {
488         struct e1000_phy_info *phy = &hw->phy;
489         s32 ret_val;
490         u16 data;
491
492         ret_val = phy->ops.read_reg(hw, IGP02E1000_PHY_POWER_MGMT, &data);
493         if (ret_val)
494                 goto out;
495
496         if (active) {
497                 data |= IGP02E1000_PM_D0_LPLU;
498                 ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
499                                                  data);
500                 if (ret_val)
501                         goto out;
502
503                 /* When LPLU is enabled, we should disable SmartSpeed */
504                 ret_val = phy->ops.read_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
505                                                 &data);
506                 data &= ~IGP01E1000_PSCFR_SMART_SPEED;
507                 ret_val = phy->ops.write_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
508                                                  data);
509                 if (ret_val)
510                         goto out;
511         } else {
512                 data &= ~IGP02E1000_PM_D0_LPLU;
513                 ret_val = phy->ops.write_reg(hw, IGP02E1000_PHY_POWER_MGMT,
514                                                  data);
515                 /*
516                  * LPLU and SmartSpeed are mutually exclusive.  LPLU is used
517                  * during Dx states where the power conservation is most
518                  * important.  During driver activity we should enable
519                  * SmartSpeed, so performance is maintained.
520                  */
521                 if (phy->smart_speed == e1000_smart_speed_on) {
522                         ret_val = phy->ops.read_reg(hw,
523                                         IGP01E1000_PHY_PORT_CONFIG, &data);
524                         if (ret_val)
525                                 goto out;
526
527                         data |= IGP01E1000_PSCFR_SMART_SPEED;
528                         ret_val = phy->ops.write_reg(hw,
529                                         IGP01E1000_PHY_PORT_CONFIG, data);
530                         if (ret_val)
531                                 goto out;
532                 } else if (phy->smart_speed == e1000_smart_speed_off) {
533                         ret_val = phy->ops.read_reg(hw,
534                                         IGP01E1000_PHY_PORT_CONFIG, &data);
535                         if (ret_val)
536                                 goto out;
537
538                         data &= ~IGP01E1000_PSCFR_SMART_SPEED;
539                         ret_val = phy->ops.write_reg(hw,
540                                         IGP01E1000_PHY_PORT_CONFIG, data);
541                         if (ret_val)
542                                 goto out;
543                 }
544         }
545
546 out:
547         return ret_val;
548 }
549
550 /**
551  *  igb_acquire_nvm_82575 - Request for access to EEPROM
552  *  @hw: pointer to the HW structure
553  *
554  *  Acquire the necessary semaphores for exclusive access to the EEPROM.
555  *  Set the EEPROM access request bit and wait for EEPROM access grant bit.
556  *  Return successful if access grant bit set, else clear the request for
557  *  EEPROM access and return -E1000_ERR_NVM (-1).
558  **/
559 static s32 igb_acquire_nvm_82575(struct e1000_hw *hw)
560 {
561         s32 ret_val;
562
563         ret_val = igb_acquire_swfw_sync_82575(hw, E1000_SWFW_EEP_SM);
564         if (ret_val)
565                 goto out;
566
567         ret_val = igb_acquire_nvm(hw);
568
569         if (ret_val)
570                 igb_release_swfw_sync_82575(hw, E1000_SWFW_EEP_SM);
571
572 out:
573         return ret_val;
574 }
575
576 /**
577  *  igb_release_nvm_82575 - Release exclusive access to EEPROM
578  *  @hw: pointer to the HW structure
579  *
580  *  Stop any current commands to the EEPROM and clear the EEPROM request bit,
581  *  then release the semaphores acquired.
582  **/
583 static void igb_release_nvm_82575(struct e1000_hw *hw)
584 {
585         igb_release_nvm(hw);
586         igb_release_swfw_sync_82575(hw, E1000_SWFW_EEP_SM);
587 }
588
589 /**
590  *  igb_acquire_swfw_sync_82575 - Acquire SW/FW semaphore
591  *  @hw: pointer to the HW structure
592  *  @mask: specifies which semaphore to acquire
593  *
594  *  Acquire the SW/FW semaphore to access the PHY or NVM.  The mask
595  *  will also specify which port we're acquiring the lock for.
596  **/
597 static s32 igb_acquire_swfw_sync_82575(struct e1000_hw *hw, u16 mask)
598 {
599         u32 swfw_sync;
600         u32 swmask = mask;
601         u32 fwmask = mask << 16;
602         s32 ret_val = 0;
603         s32 i = 0, timeout = 200; /* FIXME: find real value to use here */
604
605         while (i < timeout) {
606                 if (igb_get_hw_semaphore(hw)) {
607                         ret_val = -E1000_ERR_SWFW_SYNC;
608                         goto out;
609                 }
610
611                 swfw_sync = rd32(E1000_SW_FW_SYNC);
612                 if (!(swfw_sync & (fwmask | swmask)))
613                         break;
614
615                 /*
616                  * Firmware currently using resource (fwmask)
617                  * or other software thread using resource (swmask)
618                  */
619                 igb_put_hw_semaphore(hw);
620                 mdelay(5);
621                 i++;
622         }
623
624         if (i == timeout) {
625                 hw_dbg("Driver can't access resource, SW_FW_SYNC timeout.\n");
626                 ret_val = -E1000_ERR_SWFW_SYNC;
627                 goto out;
628         }
629
630         swfw_sync |= swmask;
631         wr32(E1000_SW_FW_SYNC, swfw_sync);
632
633         igb_put_hw_semaphore(hw);
634
635 out:
636         return ret_val;
637 }
638
639 /**
640  *  igb_release_swfw_sync_82575 - Release SW/FW semaphore
641  *  @hw: pointer to the HW structure
642  *  @mask: specifies which semaphore to acquire
643  *
644  *  Release the SW/FW semaphore used to access the PHY or NVM.  The mask
645  *  will also specify which port we're releasing the lock for.
646  **/
647 static void igb_release_swfw_sync_82575(struct e1000_hw *hw, u16 mask)
648 {
649         u32 swfw_sync;
650
651         while (igb_get_hw_semaphore(hw) != 0);
652         /* Empty */
653
654         swfw_sync = rd32(E1000_SW_FW_SYNC);
655         swfw_sync &= ~mask;
656         wr32(E1000_SW_FW_SYNC, swfw_sync);
657
658         igb_put_hw_semaphore(hw);
659 }
660
661 /**
662  *  igb_get_cfg_done_82575 - Read config done bit
663  *  @hw: pointer to the HW structure
664  *
665  *  Read the management control register for the config done bit for
666  *  completion status.  NOTE: silicon which is EEPROM-less will fail trying
667  *  to read the config done bit, so an error is *ONLY* logged and returns
668  *  0.  If we were to return with error, EEPROM-less silicon
669  *  would not be able to be reset or change link.
670  **/
671 static s32 igb_get_cfg_done_82575(struct e1000_hw *hw)
672 {
673         s32 timeout = PHY_CFG_TIMEOUT;
674         s32 ret_val = 0;
675         u32 mask = E1000_NVM_CFG_DONE_PORT_0;
676
677         if (hw->bus.func == 1)
678                 mask = E1000_NVM_CFG_DONE_PORT_1;
679         else if (hw->bus.func == E1000_FUNC_2)
680                 mask = E1000_NVM_CFG_DONE_PORT_2;
681         else if (hw->bus.func == E1000_FUNC_3)
682                 mask = E1000_NVM_CFG_DONE_PORT_3;
683
684         while (timeout) {
685                 if (rd32(E1000_EEMNGCTL) & mask)
686                         break;
687                 msleep(1);
688                 timeout--;
689         }
690         if (!timeout)
691                 hw_dbg("MNG configuration cycle has not completed.\n");
692
693         /* If EEPROM is not marked present, init the PHY manually */
694         if (((rd32(E1000_EECD) & E1000_EECD_PRES) == 0) &&
695             (hw->phy.type == e1000_phy_igp_3))
696                 igb_phy_init_script_igp3(hw);
697
698         return ret_val;
699 }
700
701 /**
702  *  igb_check_for_link_82575 - Check for link
703  *  @hw: pointer to the HW structure
704  *
705  *  If sgmii is enabled, then use the pcs register to determine link, otherwise
706  *  use the generic interface for determining link.
707  **/
708 static s32 igb_check_for_link_82575(struct e1000_hw *hw)
709 {
710         s32 ret_val;
711         u16 speed, duplex;
712
713         if (hw->phy.media_type != e1000_media_type_copper) {
714                 ret_val = igb_get_pcs_speed_and_duplex_82575(hw, &speed,
715                                                              &duplex);
716                 /*
717                  * Use this flag to determine if link needs to be checked or
718                  * not.  If  we have link clear the flag so that we do not
719                  * continue to check for link.
720                  */
721                 hw->mac.get_link_status = !hw->mac.serdes_has_link;
722         } else {
723                 ret_val = igb_check_for_copper_link(hw);
724         }
725
726         return ret_val;
727 }
728
729 /**
730  *  igb_get_pcs_speed_and_duplex_82575 - Retrieve current speed/duplex
731  *  @hw: pointer to the HW structure
732  *  @speed: stores the current speed
733  *  @duplex: stores the current duplex
734  *
735  *  Using the physical coding sub-layer (PCS), retrieve the current speed and
736  *  duplex, then store the values in the pointers provided.
737  **/
738 static s32 igb_get_pcs_speed_and_duplex_82575(struct e1000_hw *hw, u16 *speed,
739                                                 u16 *duplex)
740 {
741         struct e1000_mac_info *mac = &hw->mac;
742         u32 pcs;
743
744         /* Set up defaults for the return values of this function */
745         mac->serdes_has_link = false;
746         *speed = 0;
747         *duplex = 0;
748
749         /*
750          * Read the PCS Status register for link state. For non-copper mode,
751          * the status register is not accurate. The PCS status register is
752          * used instead.
753          */
754         pcs = rd32(E1000_PCS_LSTAT);
755
756         /*
757          * The link up bit determines when link is up on autoneg. The sync ok
758          * gets set once both sides sync up and agree upon link. Stable link
759          * can be determined by checking for both link up and link sync ok
760          */
761         if ((pcs & E1000_PCS_LSTS_LINK_OK) && (pcs & E1000_PCS_LSTS_SYNK_OK)) {
762                 mac->serdes_has_link = true;
763
764                 /* Detect and store PCS speed */
765                 if (pcs & E1000_PCS_LSTS_SPEED_1000) {
766                         *speed = SPEED_1000;
767                 } else if (pcs & E1000_PCS_LSTS_SPEED_100) {
768                         *speed = SPEED_100;
769                 } else {
770                         *speed = SPEED_10;
771                 }
772
773                 /* Detect and store PCS duplex */
774                 if (pcs & E1000_PCS_LSTS_DUPLEX_FULL) {
775                         *duplex = FULL_DUPLEX;
776                 } else {
777                         *duplex = HALF_DUPLEX;
778                 }
779         }
780
781         return 0;
782 }
783
784 /**
785  *  igb_shutdown_serdes_link_82575 - Remove link during power down
786  *  @hw: pointer to the HW structure
787  *
788  *  In the case of fiber serdes, shut down optics and PCS on driver unload
789  *  when management pass thru is not enabled.
790  **/
791 void igb_shutdown_serdes_link_82575(struct e1000_hw *hw)
792 {
793         u32 reg;
794         u16 eeprom_data = 0;
795
796         if (hw->phy.media_type != e1000_media_type_internal_serdes ||
797             igb_sgmii_active_82575(hw))
798                 return;
799
800         if (hw->bus.func == E1000_FUNC_0)
801                 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A, 1, &eeprom_data);
802         else if (hw->mac.type == e1000_82580)
803                 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_A +
804                                  NVM_82580_LAN_FUNC_OFFSET(hw->bus.func), 1,
805                                  &eeprom_data);
806         else if (hw->bus.func == E1000_FUNC_1)
807                 hw->nvm.ops.read(hw, NVM_INIT_CONTROL3_PORT_B, 1, &eeprom_data);
808
809         /*
810          * If APM is not enabled in the EEPROM and management interface is
811          * not enabled, then power down.
812          */
813         if (!(eeprom_data & E1000_NVM_APME_82575) &&
814             !igb_enable_mng_pass_thru(hw)) {
815                 /* Disable PCS to turn off link */
816                 reg = rd32(E1000_PCS_CFG0);
817                 reg &= ~E1000_PCS_CFG_PCS_EN;
818                 wr32(E1000_PCS_CFG0, reg);
819
820                 /* shutdown the laser */
821                 reg = rd32(E1000_CTRL_EXT);
822                 reg |= E1000_CTRL_EXT_SDP3_DATA;
823                 wr32(E1000_CTRL_EXT, reg);
824
825                 /* flush the write to verify completion */
826                 wrfl();
827                 msleep(1);
828         }
829
830         return;
831 }
832
833 /**
834  *  igb_reset_hw_82575 - Reset hardware
835  *  @hw: pointer to the HW structure
836  *
837  *  This resets the hardware into a known state.  This is a
838  *  function pointer entry point called by the api module.
839  **/
840 static s32 igb_reset_hw_82575(struct e1000_hw *hw)
841 {
842         u32 ctrl, icr;
843         s32 ret_val;
844
845         /*
846          * Prevent the PCI-E bus from sticking if there is no TLP connection
847          * on the last TLP read/write transaction when MAC is reset.
848          */
849         ret_val = igb_disable_pcie_master(hw);
850         if (ret_val)
851                 hw_dbg("PCI-E Master disable polling has failed.\n");
852
853         /* set the completion timeout for interface */
854         ret_val = igb_set_pcie_completion_timeout(hw);
855         if (ret_val) {
856                 hw_dbg("PCI-E Set completion timeout has failed.\n");
857         }
858
859         hw_dbg("Masking off all interrupts\n");
860         wr32(E1000_IMC, 0xffffffff);
861
862         wr32(E1000_RCTL, 0);
863         wr32(E1000_TCTL, E1000_TCTL_PSP);
864         wrfl();
865
866         msleep(10);
867
868         ctrl = rd32(E1000_CTRL);
869
870         hw_dbg("Issuing a global reset to MAC\n");
871         wr32(E1000_CTRL, ctrl | E1000_CTRL_RST);
872
873         ret_val = igb_get_auto_rd_done(hw);
874         if (ret_val) {
875                 /*
876                  * When auto config read does not complete, do not
877                  * return with an error. This can happen in situations
878                  * where there is no eeprom and prevents getting link.
879                  */
880                 hw_dbg("Auto Read Done did not complete\n");
881         }
882
883         /* If EEPROM is not present, run manual init scripts */
884         if ((rd32(E1000_EECD) & E1000_EECD_PRES) == 0)
885                 igb_reset_init_script_82575(hw);
886
887         /* Clear any pending interrupt events. */
888         wr32(E1000_IMC, 0xffffffff);
889         icr = rd32(E1000_ICR);
890
891         /* Install any alternate MAC address into RAR0 */
892         ret_val = igb_check_alt_mac_addr(hw);
893
894         return ret_val;
895 }
896
897 /**
898  *  igb_init_hw_82575 - Initialize hardware
899  *  @hw: pointer to the HW structure
900  *
901  *  This inits the hardware readying it for operation.
902  **/
903 static s32 igb_init_hw_82575(struct e1000_hw *hw)
904 {
905         struct e1000_mac_info *mac = &hw->mac;
906         s32 ret_val;
907         u16 i, rar_count = mac->rar_entry_count;
908
909         /* Initialize identification LED */
910         ret_val = igb_id_led_init(hw);
911         if (ret_val) {
912                 hw_dbg("Error initializing identification LED\n");
913                 /* This is not fatal and we should not stop init due to this */
914         }
915
916         /* Disabling VLAN filtering */
917         hw_dbg("Initializing the IEEE VLAN\n");
918         igb_clear_vfta(hw);
919
920         /* Setup the receive address */
921         igb_init_rx_addrs(hw, rar_count);
922
923         /* Zero out the Multicast HASH table */
924         hw_dbg("Zeroing the MTA\n");
925         for (i = 0; i < mac->mta_reg_count; i++)
926                 array_wr32(E1000_MTA, i, 0);
927
928         /* Zero out the Unicast HASH table */
929         hw_dbg("Zeroing the UTA\n");
930         for (i = 0; i < mac->uta_reg_count; i++)
931                 array_wr32(E1000_UTA, i, 0);
932
933         /* Setup link and flow control */
934         ret_val = igb_setup_link(hw);
935
936         /*
937          * Clear all of the statistics registers (clear on read).  It is
938          * important that we do this after we have tried to establish link
939          * because the symbol error count will increment wildly if there
940          * is no link.
941          */
942         igb_clear_hw_cntrs_82575(hw);
943
944         return ret_val;
945 }
946
947 /**
948  *  igb_setup_copper_link_82575 - Configure copper link settings
949  *  @hw: pointer to the HW structure
950  *
951  *  Configures the link for auto-neg or forced speed and duplex.  Then we check
952  *  for link, once link is established calls to configure collision distance
953  *  and flow control are called.
954  **/
955 static s32 igb_setup_copper_link_82575(struct e1000_hw *hw)
956 {
957         u32 ctrl;
958         s32  ret_val;
959
960         ctrl = rd32(E1000_CTRL);
961         ctrl |= E1000_CTRL_SLU;
962         ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
963         wr32(E1000_CTRL, ctrl);
964
965         ret_val = igb_setup_serdes_link_82575(hw);
966         if (ret_val)
967                 goto out;
968
969         if (igb_sgmii_active_82575(hw) && !hw->phy.reset_disable) {
970                 /* allow time for SFP cage time to power up phy */
971                 msleep(300);
972
973                 ret_val = hw->phy.ops.reset(hw);
974                 if (ret_val) {
975                         hw_dbg("Error resetting the PHY.\n");
976                         goto out;
977                 }
978         }
979         switch (hw->phy.type) {
980         case e1000_phy_m88:
981                 ret_val = igb_copper_link_setup_m88(hw);
982                 break;
983         case e1000_phy_igp_3:
984                 ret_val = igb_copper_link_setup_igp(hw);
985                 break;
986         case e1000_phy_82580:
987                 ret_val = igb_copper_link_setup_82580(hw);
988                 break;
989         default:
990                 ret_val = -E1000_ERR_PHY;
991                 break;
992         }
993
994         if (ret_val)
995                 goto out;
996
997         ret_val = igb_setup_copper_link(hw);
998 out:
999         return ret_val;
1000 }
1001
1002 /**
1003  *  igb_setup_serdes_link_82575 - Setup link for serdes
1004  *  @hw: pointer to the HW structure
1005  *
1006  *  Configure the physical coding sub-layer (PCS) link.  The PCS link is
1007  *  used on copper connections where the serialized gigabit media independent
1008  *  interface (sgmii), or serdes fiber is being used.  Configures the link
1009  *  for auto-negotiation or forces speed/duplex.
1010  **/
1011 static s32 igb_setup_serdes_link_82575(struct e1000_hw *hw)
1012 {
1013         u32 ctrl_ext, ctrl_reg, reg;
1014         bool pcs_autoneg;
1015
1016         if ((hw->phy.media_type != e1000_media_type_internal_serdes) &&
1017             !igb_sgmii_active_82575(hw))
1018                 return 0;
1019
1020         /*
1021          * On the 82575, SerDes loopback mode persists until it is
1022          * explicitly turned off or a power cycle is performed.  A read to
1023          * the register does not indicate its status.  Therefore, we ensure
1024          * loopback mode is disabled during initialization.
1025          */
1026         wr32(E1000_SCTL, E1000_SCTL_DISABLE_SERDES_LOOPBACK);
1027
1028         /* power on the sfp cage if present */
1029         ctrl_ext = rd32(E1000_CTRL_EXT);
1030         ctrl_ext &= ~E1000_CTRL_EXT_SDP3_DATA;
1031         wr32(E1000_CTRL_EXT, ctrl_ext);
1032
1033         ctrl_reg = rd32(E1000_CTRL);
1034         ctrl_reg |= E1000_CTRL_SLU;
1035
1036         if (hw->mac.type == e1000_82575 || hw->mac.type == e1000_82576) {
1037                 /* set both sw defined pins */
1038                 ctrl_reg |= E1000_CTRL_SWDPIN0 | E1000_CTRL_SWDPIN1;
1039
1040                 /* Set switch control to serdes energy detect */
1041                 reg = rd32(E1000_CONNSW);
1042                 reg |= E1000_CONNSW_ENRGSRC;
1043                 wr32(E1000_CONNSW, reg);
1044         }
1045
1046         reg = rd32(E1000_PCS_LCTL);
1047
1048         /* default pcs_autoneg to the same setting as mac autoneg */
1049         pcs_autoneg = hw->mac.autoneg;
1050
1051         switch (ctrl_ext & E1000_CTRL_EXT_LINK_MODE_MASK) {
1052         case E1000_CTRL_EXT_LINK_MODE_SGMII:
1053                 /* sgmii mode lets the phy handle forcing speed/duplex */
1054                 pcs_autoneg = true;
1055                 /* autoneg time out should be disabled for SGMII mode */
1056                 reg &= ~(E1000_PCS_LCTL_AN_TIMEOUT);
1057                 break;
1058         case E1000_CTRL_EXT_LINK_MODE_1000BASE_KX:
1059                 /* disable PCS autoneg and support parallel detect only */
1060                 pcs_autoneg = false;
1061         default:
1062                 /*
1063                  * non-SGMII modes only supports a speed of 1000/Full for the
1064                  * link so it is best to just force the MAC and let the pcs
1065                  * link either autoneg or be forced to 1000/Full
1066                  */
1067                 ctrl_reg |= E1000_CTRL_SPD_1000 | E1000_CTRL_FRCSPD |
1068                             E1000_CTRL_FD | E1000_CTRL_FRCDPX;
1069
1070                 /* set speed of 1000/Full if speed/duplex is forced */
1071                 reg |= E1000_PCS_LCTL_FSV_1000 | E1000_PCS_LCTL_FDV_FULL;
1072                 break;
1073         }
1074
1075         wr32(E1000_CTRL, ctrl_reg);
1076
1077         /*
1078          * New SerDes mode allows for forcing speed or autonegotiating speed
1079          * at 1gb. Autoneg should be default set by most drivers. This is the
1080          * mode that will be compatible with older link partners and switches.
1081          * However, both are supported by the hardware and some drivers/tools.
1082          */
1083         reg &= ~(E1000_PCS_LCTL_AN_ENABLE | E1000_PCS_LCTL_FLV_LINK_UP |
1084                 E1000_PCS_LCTL_FSD | E1000_PCS_LCTL_FORCE_LINK);
1085
1086         /*
1087          * We force flow control to prevent the CTRL register values from being
1088          * overwritten by the autonegotiated flow control values
1089          */
1090         reg |= E1000_PCS_LCTL_FORCE_FCTRL;
1091
1092         if (pcs_autoneg) {
1093                 /* Set PCS register for autoneg */
1094                 reg |= E1000_PCS_LCTL_AN_ENABLE | /* Enable Autoneg */
1095                        E1000_PCS_LCTL_AN_RESTART; /* Restart autoneg */
1096                 hw_dbg("Configuring Autoneg:PCS_LCTL=0x%08X\n", reg);
1097         } else {
1098                 /* Set PCS register for forced link */
1099                 reg |= E1000_PCS_LCTL_FSD;        /* Force Speed */
1100
1101                 hw_dbg("Configuring Forced Link:PCS_LCTL=0x%08X\n", reg);
1102         }
1103
1104         wr32(E1000_PCS_LCTL, reg);
1105
1106         if (!igb_sgmii_active_82575(hw))
1107                 igb_force_mac_fc(hw);
1108
1109         return 0;
1110 }
1111
1112 /**
1113  *  igb_sgmii_active_82575 - Return sgmii state
1114  *  @hw: pointer to the HW structure
1115  *
1116  *  82575 silicon has a serialized gigabit media independent interface (sgmii)
1117  *  which can be enabled for use in the embedded applications.  Simply
1118  *  return the current state of the sgmii interface.
1119  **/
1120 static bool igb_sgmii_active_82575(struct e1000_hw *hw)
1121 {
1122         struct e1000_dev_spec_82575 *dev_spec = &hw->dev_spec._82575;
1123         return dev_spec->sgmii_active;
1124 }
1125
1126 /**
1127  *  igb_reset_init_script_82575 - Inits HW defaults after reset
1128  *  @hw: pointer to the HW structure
1129  *
1130  *  Inits recommended HW defaults after a reset when there is no EEPROM
1131  *  detected. This is only for the 82575.
1132  **/
1133 static s32 igb_reset_init_script_82575(struct e1000_hw *hw)
1134 {
1135         if (hw->mac.type == e1000_82575) {
1136                 hw_dbg("Running reset init script for 82575\n");
1137                 /* SerDes configuration via SERDESCTRL */
1138                 igb_write_8bit_ctrl_reg(hw, E1000_SCTL, 0x00, 0x0C);
1139                 igb_write_8bit_ctrl_reg(hw, E1000_SCTL, 0x01, 0x78);
1140                 igb_write_8bit_ctrl_reg(hw, E1000_SCTL, 0x1B, 0x23);
1141                 igb_write_8bit_ctrl_reg(hw, E1000_SCTL, 0x23, 0x15);
1142
1143                 /* CCM configuration via CCMCTL register */
1144                 igb_write_8bit_ctrl_reg(hw, E1000_CCMCTL, 0x14, 0x00);
1145                 igb_write_8bit_ctrl_reg(hw, E1000_CCMCTL, 0x10, 0x00);
1146
1147                 /* PCIe lanes configuration */
1148                 igb_write_8bit_ctrl_reg(hw, E1000_GIOCTL, 0x00, 0xEC);
1149                 igb_write_8bit_ctrl_reg(hw, E1000_GIOCTL, 0x61, 0xDF);
1150                 igb_write_8bit_ctrl_reg(hw, E1000_GIOCTL, 0x34, 0x05);
1151                 igb_write_8bit_ctrl_reg(hw, E1000_GIOCTL, 0x2F, 0x81);
1152
1153                 /* PCIe PLL Configuration */
1154                 igb_write_8bit_ctrl_reg(hw, E1000_SCCTL, 0x02, 0x47);
1155                 igb_write_8bit_ctrl_reg(hw, E1000_SCCTL, 0x14, 0x00);
1156                 igb_write_8bit_ctrl_reg(hw, E1000_SCCTL, 0x10, 0x00);
1157         }
1158
1159         return 0;
1160 }
1161
1162 /**
1163  *  igb_read_mac_addr_82575 - Read device MAC address
1164  *  @hw: pointer to the HW structure
1165  **/
1166 static s32 igb_read_mac_addr_82575(struct e1000_hw *hw)
1167 {
1168         s32 ret_val = 0;
1169
1170         /*
1171          * If there's an alternate MAC address place it in RAR0
1172          * so that it will override the Si installed default perm
1173          * address.
1174          */
1175         ret_val = igb_check_alt_mac_addr(hw);
1176         if (ret_val)
1177                 goto out;
1178
1179         ret_val = igb_read_mac_addr(hw);
1180
1181 out:
1182         return ret_val;
1183 }
1184
1185 /**
1186  *  igb_clear_hw_cntrs_82575 - Clear device specific hardware counters
1187  *  @hw: pointer to the HW structure
1188  *
1189  *  Clears the hardware counters by reading the counter registers.
1190  **/
1191 static void igb_clear_hw_cntrs_82575(struct e1000_hw *hw)
1192 {
1193         igb_clear_hw_cntrs_base(hw);
1194
1195         rd32(E1000_PRC64);
1196         rd32(E1000_PRC127);
1197         rd32(E1000_PRC255);
1198         rd32(E1000_PRC511);
1199         rd32(E1000_PRC1023);
1200         rd32(E1000_PRC1522);
1201         rd32(E1000_PTC64);
1202         rd32(E1000_PTC127);
1203         rd32(E1000_PTC255);
1204         rd32(E1000_PTC511);
1205         rd32(E1000_PTC1023);
1206         rd32(E1000_PTC1522);
1207
1208         rd32(E1000_ALGNERRC);
1209         rd32(E1000_RXERRC);
1210         rd32(E1000_TNCRS);
1211         rd32(E1000_CEXTERR);
1212         rd32(E1000_TSCTC);
1213         rd32(E1000_TSCTFC);
1214
1215         rd32(E1000_MGTPRC);
1216         rd32(E1000_MGTPDC);
1217         rd32(E1000_MGTPTC);
1218
1219         rd32(E1000_IAC);
1220         rd32(E1000_ICRXOC);
1221
1222         rd32(E1000_ICRXPTC);
1223         rd32(E1000_ICRXATC);
1224         rd32(E1000_ICTXPTC);
1225         rd32(E1000_ICTXATC);
1226         rd32(E1000_ICTXQEC);
1227         rd32(E1000_ICTXQMTC);
1228         rd32(E1000_ICRXDMTC);
1229
1230         rd32(E1000_CBTMPC);
1231         rd32(E1000_HTDPMC);
1232         rd32(E1000_CBRMPC);
1233         rd32(E1000_RPTHC);
1234         rd32(E1000_HGPTC);
1235         rd32(E1000_HTCBDPC);
1236         rd32(E1000_HGORCL);
1237         rd32(E1000_HGORCH);
1238         rd32(E1000_HGOTCL);
1239         rd32(E1000_HGOTCH);
1240         rd32(E1000_LENERRS);
1241
1242         /* This register should not be read in copper configurations */
1243         if (hw->phy.media_type == e1000_media_type_internal_serdes ||
1244             igb_sgmii_active_82575(hw))
1245                 rd32(E1000_SCVPC);
1246 }
1247
1248 /**
1249  *  igb_rx_fifo_flush_82575 - Clean rx fifo after RX enable
1250  *  @hw: pointer to the HW structure
1251  *
1252  *  After rx enable if managability is enabled then there is likely some
1253  *  bad data at the start of the fifo and possibly in the DMA fifo.  This
1254  *  function clears the fifos and flushes any packets that came in as rx was
1255  *  being enabled.
1256  **/
1257 void igb_rx_fifo_flush_82575(struct e1000_hw *hw)
1258 {
1259         u32 rctl, rlpml, rxdctl[4], rfctl, temp_rctl, rx_enabled;
1260         int i, ms_wait;
1261
1262         if (hw->mac.type != e1000_82575 ||
1263             !(rd32(E1000_MANC) & E1000_MANC_RCV_TCO_EN))
1264                 return;
1265
1266         /* Disable all RX queues */
1267         for (i = 0; i < 4; i++) {
1268                 rxdctl[i] = rd32(E1000_RXDCTL(i));
1269                 wr32(E1000_RXDCTL(i),
1270                      rxdctl[i] & ~E1000_RXDCTL_QUEUE_ENABLE);
1271         }
1272         /* Poll all queues to verify they have shut down */
1273         for (ms_wait = 0; ms_wait < 10; ms_wait++) {
1274                 msleep(1);
1275                 rx_enabled = 0;
1276                 for (i = 0; i < 4; i++)
1277                         rx_enabled |= rd32(E1000_RXDCTL(i));
1278                 if (!(rx_enabled & E1000_RXDCTL_QUEUE_ENABLE))
1279                         break;
1280         }
1281
1282         if (ms_wait == 10)
1283                 hw_dbg("Queue disable timed out after 10ms\n");
1284
1285         /* Clear RLPML, RCTL.SBP, RFCTL.LEF, and set RCTL.LPE so that all
1286          * incoming packets are rejected.  Set enable and wait 2ms so that
1287          * any packet that was coming in as RCTL.EN was set is flushed
1288          */
1289         rfctl = rd32(E1000_RFCTL);
1290         wr32(E1000_RFCTL, rfctl & ~E1000_RFCTL_LEF);
1291
1292         rlpml = rd32(E1000_RLPML);
1293         wr32(E1000_RLPML, 0);
1294
1295         rctl = rd32(E1000_RCTL);
1296         temp_rctl = rctl & ~(E1000_RCTL_EN | E1000_RCTL_SBP);
1297         temp_rctl |= E1000_RCTL_LPE;
1298
1299         wr32(E1000_RCTL, temp_rctl);
1300         wr32(E1000_RCTL, temp_rctl | E1000_RCTL_EN);
1301         wrfl();
1302         msleep(2);
1303
1304         /* Enable RX queues that were previously enabled and restore our
1305          * previous state
1306          */
1307         for (i = 0; i < 4; i++)
1308                 wr32(E1000_RXDCTL(i), rxdctl[i]);
1309         wr32(E1000_RCTL, rctl);
1310         wrfl();
1311
1312         wr32(E1000_RLPML, rlpml);
1313         wr32(E1000_RFCTL, rfctl);
1314
1315         /* Flush receive errors generated by workaround */
1316         rd32(E1000_ROC);
1317         rd32(E1000_RNBC);
1318         rd32(E1000_MPC);
1319 }
1320
1321 /**
1322  *  igb_set_pcie_completion_timeout - set pci-e completion timeout
1323  *  @hw: pointer to the HW structure
1324  *
1325  *  The defaults for 82575 and 82576 should be in the range of 50us to 50ms,
1326  *  however the hardware default for these parts is 500us to 1ms which is less
1327  *  than the 10ms recommended by the pci-e spec.  To address this we need to
1328  *  increase the value to either 10ms to 200ms for capability version 1 config,
1329  *  or 16ms to 55ms for version 2.
1330  **/
1331 static s32 igb_set_pcie_completion_timeout(struct e1000_hw *hw)
1332 {
1333         u32 gcr = rd32(E1000_GCR);
1334         s32 ret_val = 0;
1335         u16 pcie_devctl2;
1336
1337         /* only take action if timeout value is defaulted to 0 */
1338         if (gcr & E1000_GCR_CMPL_TMOUT_MASK)
1339                 goto out;
1340
1341         /*
1342          * if capababilities version is type 1 we can write the
1343          * timeout of 10ms to 200ms through the GCR register
1344          */
1345         if (!(gcr & E1000_GCR_CAP_VER2)) {
1346                 gcr |= E1000_GCR_CMPL_TMOUT_10ms;
1347                 goto out;
1348         }
1349
1350         /*
1351          * for version 2 capabilities we need to write the config space
1352          * directly in order to set the completion timeout value for
1353          * 16ms to 55ms
1354          */
1355         ret_val = igb_read_pcie_cap_reg(hw, PCIE_DEVICE_CONTROL2,
1356                                         &pcie_devctl2);
1357         if (ret_val)
1358                 goto out;
1359
1360         pcie_devctl2 |= PCIE_DEVICE_CONTROL2_16ms;
1361
1362         ret_val = igb_write_pcie_cap_reg(hw, PCIE_DEVICE_CONTROL2,
1363                                          &pcie_devctl2);
1364 out:
1365         /* disable completion timeout resend */
1366         gcr &= ~E1000_GCR_CMPL_TMOUT_RESEND;
1367
1368         wr32(E1000_GCR, gcr);
1369         return ret_val;
1370 }
1371
1372 /**
1373  *  igb_vmdq_set_loopback_pf - enable or disable vmdq loopback
1374  *  @hw: pointer to the hardware struct
1375  *  @enable: state to enter, either enabled or disabled
1376  *
1377  *  enables/disables L2 switch loopback functionality.
1378  **/
1379 void igb_vmdq_set_loopback_pf(struct e1000_hw *hw, bool enable)
1380 {
1381         u32 dtxswc = rd32(E1000_DTXSWC);
1382
1383         if (enable)
1384                 dtxswc |= E1000_DTXSWC_VMDQ_LOOPBACK_EN;
1385         else
1386                 dtxswc &= ~E1000_DTXSWC_VMDQ_LOOPBACK_EN;
1387
1388         wr32(E1000_DTXSWC, dtxswc);
1389 }
1390
1391 /**
1392  *  igb_vmdq_set_replication_pf - enable or disable vmdq replication
1393  *  @hw: pointer to the hardware struct
1394  *  @enable: state to enter, either enabled or disabled
1395  *
1396  *  enables/disables replication of packets across multiple pools.
1397  **/
1398 void igb_vmdq_set_replication_pf(struct e1000_hw *hw, bool enable)
1399 {
1400         u32 vt_ctl = rd32(E1000_VT_CTL);
1401
1402         if (enable)
1403                 vt_ctl |= E1000_VT_CTL_VM_REPL_EN;
1404         else
1405                 vt_ctl &= ~E1000_VT_CTL_VM_REPL_EN;
1406
1407         wr32(E1000_VT_CTL, vt_ctl);
1408 }
1409
1410 /**
1411  *  igb_read_phy_reg_82580 - Read 82580 MDI control register
1412  *  @hw: pointer to the HW structure
1413  *  @offset: register offset to be read
1414  *  @data: pointer to the read data
1415  *
1416  *  Reads the MDI control register in the PHY at offset and stores the
1417  *  information read to data.
1418  **/
1419 static s32 igb_read_phy_reg_82580(struct e1000_hw *hw, u32 offset, u16 *data)
1420 {
1421         u32 mdicnfg = 0;
1422         s32 ret_val;
1423
1424
1425         ret_val = hw->phy.ops.acquire(hw);
1426         if (ret_val)
1427                 goto out;
1428
1429         /*
1430          * We config the phy address in MDICNFG register now. Same bits
1431          * as before. The values in MDIC can be written but will be
1432          * ignored. This allows us to call the old function after
1433          * configuring the PHY address in the new register
1434          */
1435         mdicnfg = (hw->phy.addr << E1000_MDIC_PHY_SHIFT);
1436         wr32(E1000_MDICNFG, mdicnfg);
1437
1438         ret_val = igb_read_phy_reg_mdic(hw, offset, data);
1439
1440         hw->phy.ops.release(hw);
1441
1442 out:
1443         return ret_val;
1444 }
1445
1446 /**
1447  *  igb_write_phy_reg_82580 - Write 82580 MDI control register
1448  *  @hw: pointer to the HW structure
1449  *  @offset: register offset to write to
1450  *  @data: data to write to register at offset
1451  *
1452  *  Writes data to MDI control register in the PHY at offset.
1453  **/
1454 static s32 igb_write_phy_reg_82580(struct e1000_hw *hw, u32 offset, u16 data)
1455 {
1456         u32 mdicnfg = 0;
1457         s32 ret_val;
1458
1459
1460         ret_val = hw->phy.ops.acquire(hw);
1461         if (ret_val)
1462                 goto out;
1463
1464         /*
1465          * We config the phy address in MDICNFG register now. Same bits
1466          * as before. The values in MDIC can be written but will be
1467          * ignored. This allows us to call the old function after
1468          * configuring the PHY address in the new register
1469          */
1470         mdicnfg = (hw->phy.addr << E1000_MDIC_PHY_SHIFT);
1471         wr32(E1000_MDICNFG, mdicnfg);
1472
1473         ret_val = igb_write_phy_reg_mdic(hw, offset, data);
1474
1475         hw->phy.ops.release(hw);
1476
1477 out:
1478         return ret_val;
1479 }
1480
1481 /**
1482  *  igb_reset_hw_82580 - Reset hardware
1483  *  @hw: pointer to the HW structure
1484  *
1485  *  This resets function or entire device (all ports, etc.)
1486  *  to a known state.
1487  **/
1488 static s32 igb_reset_hw_82580(struct e1000_hw *hw)
1489 {
1490         s32 ret_val = 0;
1491         /* BH SW mailbox bit in SW_FW_SYNC */
1492         u16 swmbsw_mask = E1000_SW_SYNCH_MB;
1493         u32 ctrl, icr;
1494         bool global_device_reset = hw->dev_spec._82575.global_device_reset;
1495
1496
1497         hw->dev_spec._82575.global_device_reset = false;
1498
1499         /* Get current control state. */
1500         ctrl = rd32(E1000_CTRL);
1501
1502         /*
1503          * Prevent the PCI-E bus from sticking if there is no TLP connection
1504          * on the last TLP read/write transaction when MAC is reset.
1505          */
1506         ret_val = igb_disable_pcie_master(hw);
1507         if (ret_val)
1508                 hw_dbg("PCI-E Master disable polling has failed.\n");
1509
1510         hw_dbg("Masking off all interrupts\n");
1511         wr32(E1000_IMC, 0xffffffff);
1512         wr32(E1000_RCTL, 0);
1513         wr32(E1000_TCTL, E1000_TCTL_PSP);
1514         wrfl();
1515
1516         msleep(10);
1517
1518         /* Determine whether or not a global dev reset is requested */
1519         if (global_device_reset &&
1520                 igb_acquire_swfw_sync_82575(hw, swmbsw_mask))
1521                         global_device_reset = false;
1522
1523         if (global_device_reset &&
1524                 !(rd32(E1000_STATUS) & E1000_STAT_DEV_RST_SET))
1525                 ctrl |= E1000_CTRL_DEV_RST;
1526         else
1527                 ctrl |= E1000_CTRL_RST;
1528
1529         wr32(E1000_CTRL, ctrl);
1530
1531         /* Add delay to insure DEV_RST has time to complete */
1532         if (global_device_reset)
1533                 msleep(5);
1534
1535         ret_val = igb_get_auto_rd_done(hw);
1536         if (ret_val) {
1537                 /*
1538                  * When auto config read does not complete, do not
1539                  * return with an error. This can happen in situations
1540                  * where there is no eeprom and prevents getting link.
1541                  */
1542                 hw_dbg("Auto Read Done did not complete\n");
1543         }
1544
1545         /* If EEPROM is not present, run manual init scripts */
1546         if ((rd32(E1000_EECD) & E1000_EECD_PRES) == 0)
1547                 igb_reset_init_script_82575(hw);
1548
1549         /* clear global device reset status bit */
1550         wr32(E1000_STATUS, E1000_STAT_DEV_RST_SET);
1551
1552         /* Clear any pending interrupt events. */
1553         wr32(E1000_IMC, 0xffffffff);
1554         icr = rd32(E1000_ICR);
1555
1556         /* Install any alternate MAC address into RAR0 */
1557         ret_val = igb_check_alt_mac_addr(hw);
1558
1559         /* Release semaphore */
1560         if (global_device_reset)
1561                 igb_release_swfw_sync_82575(hw, swmbsw_mask);
1562
1563         return ret_val;
1564 }
1565
1566 /**
1567  *  igb_rxpbs_adjust_82580 - adjust RXPBS value to reflect actual RX PBA size
1568  *  @data: data received by reading RXPBS register
1569  *
1570  *  The 82580 uses a table based approach for packet buffer allocation sizes.
1571  *  This function converts the retrieved value into the correct table value
1572  *     0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7
1573  *  0x0 36  72 144   1   2   4   8  16
1574  *  0x8 35  70 140 rsv rsv rsv rsv rsv
1575  */
1576 u16 igb_rxpbs_adjust_82580(u32 data)
1577 {
1578         u16 ret_val = 0;
1579
1580         if (data < E1000_82580_RXPBS_TABLE_SIZE)
1581                 ret_val = e1000_82580_rxpbs_table[data];
1582
1583         return ret_val;
1584 }
1585
1586 static struct e1000_mac_operations e1000_mac_ops_82575 = {
1587         .init_hw              = igb_init_hw_82575,
1588         .check_for_link       = igb_check_for_link_82575,
1589         .rar_set              = igb_rar_set,
1590         .read_mac_addr        = igb_read_mac_addr_82575,
1591         .get_speed_and_duplex = igb_get_speed_and_duplex_copper,
1592 };
1593
1594 static struct e1000_phy_operations e1000_phy_ops_82575 = {
1595         .acquire              = igb_acquire_phy_82575,
1596         .get_cfg_done         = igb_get_cfg_done_82575,
1597         .release              = igb_release_phy_82575,
1598 };
1599
1600 static struct e1000_nvm_operations e1000_nvm_ops_82575 = {
1601         .acquire              = igb_acquire_nvm_82575,
1602         .read                 = igb_read_nvm_eerd,
1603         .release              = igb_release_nvm_82575,
1604         .write                = igb_write_nvm_spi,
1605 };
1606
1607 const struct e1000_info e1000_82575_info = {
1608         .get_invariants = igb_get_invariants_82575,
1609         .mac_ops = &e1000_mac_ops_82575,
1610         .phy_ops = &e1000_phy_ops_82575,
1611         .nvm_ops = &e1000_nvm_ops_82575,
1612 };
1613