Merge branch 'upstream-fixes' of git://lost.foo-projects.org/~ahkok/git/netdev-2...
[linux-2.6-microblaze.git] / drivers / net / e1000 / e1000_hw.c
1 /*******************************************************************************
2
3   
4   Copyright(c) 1999 - 2006 Intel Corporation. All rights reserved.
5   
6   This program is free software; you can redistribute it and/or modify it 
7   under the terms of the GNU General Public License as published by the Free 
8   Software Foundation; either version 2 of the License, or (at your option) 
9   any later version.
10   
11   This program is distributed in the hope that it will be useful, but WITHOUT 
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
14   more details.
15   
16   You should have received a copy of the GNU General Public License along with
17   this program; if not, write to the Free Software Foundation, Inc., 59 
18   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19   
20   The full GNU General Public License is included in this distribution in the
21   file called LICENSE.
22   
23   Contact Information:
24   Linux NICS <linux.nics@intel.com>
25   e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
26   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27
28 *******************************************************************************/
29
30 /* e1000_hw.c
31  * Shared functions for accessing and configuring the MAC
32  */
33
34 #include "e1000_hw.h"
35
36 static int32_t e1000_set_phy_type(struct e1000_hw *hw);
37 static void e1000_phy_init_script(struct e1000_hw *hw);
38 static int32_t e1000_setup_copper_link(struct e1000_hw *hw);
39 static int32_t e1000_setup_fiber_serdes_link(struct e1000_hw *hw);
40 static int32_t e1000_adjust_serdes_amplitude(struct e1000_hw *hw);
41 static int32_t e1000_phy_force_speed_duplex(struct e1000_hw *hw);
42 static int32_t e1000_config_mac_to_phy(struct e1000_hw *hw);
43 static void e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
44 static void e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
45 static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data,
46                                      uint16_t count);
47 static uint16_t e1000_shift_in_mdi_bits(struct e1000_hw *hw);
48 static int32_t e1000_phy_reset_dsp(struct e1000_hw *hw);
49 static int32_t e1000_write_eeprom_spi(struct e1000_hw *hw, uint16_t offset,
50                                       uint16_t words, uint16_t *data);
51 static int32_t e1000_write_eeprom_microwire(struct e1000_hw *hw,
52                                             uint16_t offset, uint16_t words,
53                                             uint16_t *data);
54 static int32_t e1000_spi_eeprom_ready(struct e1000_hw *hw);
55 static void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
56 static void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
57 static void e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data,
58                                     uint16_t count);
59 static int32_t e1000_write_phy_reg_ex(struct e1000_hw *hw, uint32_t reg_addr,
60                                       uint16_t phy_data);
61 static int32_t e1000_read_phy_reg_ex(struct e1000_hw *hw,uint32_t reg_addr,
62                                      uint16_t *phy_data);
63 static uint16_t e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count);
64 static int32_t e1000_acquire_eeprom(struct e1000_hw *hw);
65 static void e1000_release_eeprom(struct e1000_hw *hw);
66 static void e1000_standby_eeprom(struct e1000_hw *hw);
67 static int32_t e1000_set_vco_speed(struct e1000_hw *hw);
68 static int32_t e1000_polarity_reversal_workaround(struct e1000_hw *hw);
69 static int32_t e1000_set_phy_mode(struct e1000_hw *hw);
70 static int32_t e1000_host_if_read_cookie(struct e1000_hw *hw, uint8_t *buffer);
71 static uint8_t e1000_calculate_mng_checksum(char *buffer, uint32_t length);
72 static uint8_t e1000_arc_subsystem_valid(struct e1000_hw *hw);
73 static int32_t e1000_check_downshift(struct e1000_hw *hw);
74 static int32_t e1000_check_polarity(struct e1000_hw *hw, uint16_t *polarity);
75 static void e1000_clear_hw_cntrs(struct e1000_hw *hw);
76 static void e1000_clear_vfta(struct e1000_hw *hw);
77 static int32_t e1000_commit_shadow_ram(struct e1000_hw *hw);
78 static int32_t e1000_config_dsp_after_link_change(struct e1000_hw *hw,
79                                                   boolean_t link_up);
80 static int32_t e1000_config_fc_after_link_up(struct e1000_hw *hw);
81 static int32_t e1000_detect_gig_phy(struct e1000_hw *hw);
82 static int32_t e1000_get_auto_rd_done(struct e1000_hw *hw);
83 static int32_t e1000_get_cable_length(struct e1000_hw *hw,
84                                       uint16_t *min_length,
85                                       uint16_t *max_length);
86 static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
87 static int32_t e1000_get_phy_cfg_done(struct e1000_hw *hw);
88 static int32_t e1000_id_led_init(struct e1000_hw * hw);
89 static void e1000_init_rx_addrs(struct e1000_hw *hw);
90 static boolean_t e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw);
91 static int32_t e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd);
92 static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
93 static int32_t e1000_read_eeprom_eerd(struct e1000_hw *hw, uint16_t offset,
94                                       uint16_t words, uint16_t *data);
95 static int32_t e1000_set_d0_lplu_state(struct e1000_hw *hw, boolean_t active);
96 static int32_t e1000_set_d3_lplu_state(struct e1000_hw *hw, boolean_t active);
97 static int32_t e1000_wait_autoneg(struct e1000_hw *hw);
98
99 static void e1000_write_reg_io(struct e1000_hw *hw, uint32_t offset,
100                                uint32_t value);
101
102 #define E1000_WRITE_REG_IO(a, reg, val) \
103             e1000_write_reg_io((a), E1000_##reg, val)
104 static int32_t e1000_configure_kmrn_for_10_100(struct e1000_hw *hw,
105                                                uint16_t duplex);
106 static int32_t e1000_configure_kmrn_for_1000(struct e1000_hw *hw);
107
108 static int32_t e1000_erase_ich8_4k_segment(struct e1000_hw *hw,
109                                            uint32_t segment);
110 static int32_t e1000_get_software_flag(struct e1000_hw *hw);
111 static int32_t e1000_get_software_semaphore(struct e1000_hw *hw);
112 static int32_t e1000_init_lcd_from_nvm(struct e1000_hw *hw);
113 static int32_t e1000_kumeran_lock_loss_workaround(struct e1000_hw *hw);
114 static int32_t e1000_read_eeprom_ich8(struct e1000_hw *hw, uint16_t offset,
115                                       uint16_t words, uint16_t *data);
116 static int32_t e1000_read_ich8_byte(struct e1000_hw *hw, uint32_t index,
117                                     uint8_t* data);
118 static int32_t e1000_read_ich8_word(struct e1000_hw *hw, uint32_t index,
119                                     uint16_t *data);
120 static int32_t e1000_read_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr,
121                                    uint16_t *data);
122 static void e1000_release_software_flag(struct e1000_hw *hw);
123 static void e1000_release_software_semaphore(struct e1000_hw *hw);
124 static int32_t e1000_set_pci_ex_no_snoop(struct e1000_hw *hw,
125                                          uint32_t no_snoop);
126 static int32_t e1000_verify_write_ich8_byte(struct e1000_hw *hw,
127                                             uint32_t index, uint8_t byte);
128 static int32_t e1000_write_eeprom_ich8(struct e1000_hw *hw, uint16_t offset,
129                                        uint16_t words, uint16_t *data);
130 static int32_t e1000_write_ich8_byte(struct e1000_hw *hw, uint32_t index,
131                                      uint8_t data);
132 static int32_t e1000_write_kmrn_reg(struct e1000_hw *hw, uint32_t reg_addr,
133                                     uint16_t data);
134
135 /* IGP cable length table */
136 static const
137 uint16_t e1000_igp_cable_length_table[IGP01E1000_AGC_LENGTH_TABLE_SIZE] =
138     { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
139       5, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 25, 25, 25,
140       25, 25, 25, 25, 30, 30, 30, 30, 40, 40, 40, 40, 40, 40, 40, 40,
141       40, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 60, 60, 60,
142       60, 70, 70, 70, 70, 70, 70, 80, 80, 80, 80, 80, 80, 90, 90, 90,
143       90, 90, 90, 90, 90, 90, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
144       100, 100, 100, 100, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110,
145       110, 110, 110, 110, 110, 110, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120};
146
147 static const
148 uint16_t e1000_igp_2_cable_length_table[IGP02E1000_AGC_LENGTH_TABLE_SIZE] =
149     { 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21,
150       0, 0, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41,
151       6, 10, 14, 18, 22, 26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61,
152       21, 26, 31, 35, 40, 44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82,
153       40, 45, 51, 56, 61, 66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104,
154       60, 66, 72, 77, 82, 87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121,
155       83, 89, 95, 100, 105, 109, 113, 116, 119, 122, 124,
156       104, 109, 114, 118, 121, 124};
157
158
159 /******************************************************************************
160  * Set the phy type member in the hw struct.
161  *
162  * hw - Struct containing variables accessed by shared code
163  *****************************************************************************/
164 int32_t
165 e1000_set_phy_type(struct e1000_hw *hw)
166 {
167     DEBUGFUNC("e1000_set_phy_type");
168
169     if(hw->mac_type == e1000_undefined)
170         return -E1000_ERR_PHY_TYPE;
171
172     switch(hw->phy_id) {
173     case M88E1000_E_PHY_ID:
174     case M88E1000_I_PHY_ID:
175     case M88E1011_I_PHY_ID:
176     case M88E1111_I_PHY_ID:
177         hw->phy_type = e1000_phy_m88;
178         break;
179     case IGP01E1000_I_PHY_ID:
180         if(hw->mac_type == e1000_82541 ||
181            hw->mac_type == e1000_82541_rev_2 ||
182            hw->mac_type == e1000_82547 ||
183            hw->mac_type == e1000_82547_rev_2) {
184             hw->phy_type = e1000_phy_igp;
185             break;
186         }
187     case IGP03E1000_E_PHY_ID:
188         hw->phy_type = e1000_phy_igp_3;
189         break;
190     case IFE_E_PHY_ID:
191     case IFE_PLUS_E_PHY_ID:
192     case IFE_C_E_PHY_ID:
193         hw->phy_type = e1000_phy_ife;
194         break;
195     case GG82563_E_PHY_ID:
196         if (hw->mac_type == e1000_80003es2lan) {
197             hw->phy_type = e1000_phy_gg82563;
198             break;
199         }
200         /* Fall Through */
201     default:
202         /* Should never have loaded on this device */
203         hw->phy_type = e1000_phy_undefined;
204         return -E1000_ERR_PHY_TYPE;
205     }
206
207     return E1000_SUCCESS;
208 }
209
210 /******************************************************************************
211  * IGP phy init script - initializes the GbE PHY
212  *
213  * hw - Struct containing variables accessed by shared code
214  *****************************************************************************/
215 static void
216 e1000_phy_init_script(struct e1000_hw *hw)
217 {
218     uint32_t ret_val;
219     uint16_t phy_saved_data;
220
221     DEBUGFUNC("e1000_phy_init_script");
222
223     if(hw->phy_init_script) {
224         msec_delay(20);
225
226         /* Save off the current value of register 0x2F5B to be restored at
227          * the end of this routine. */
228         ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
229
230         /* Disabled the PHY transmitter */
231         e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
232
233         msec_delay(20);
234
235         e1000_write_phy_reg(hw,0x0000,0x0140);
236
237         msec_delay(5);
238
239         switch(hw->mac_type) {
240         case e1000_82541:
241         case e1000_82547:
242             e1000_write_phy_reg(hw, 0x1F95, 0x0001);
243
244             e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
245
246             e1000_write_phy_reg(hw, 0x1F79, 0x0018);
247
248             e1000_write_phy_reg(hw, 0x1F30, 0x1600);
249
250             e1000_write_phy_reg(hw, 0x1F31, 0x0014);
251
252             e1000_write_phy_reg(hw, 0x1F32, 0x161C);
253
254             e1000_write_phy_reg(hw, 0x1F94, 0x0003);
255
256             e1000_write_phy_reg(hw, 0x1F96, 0x003F);
257
258             e1000_write_phy_reg(hw, 0x2010, 0x0008);
259             break;
260
261         case e1000_82541_rev_2:
262         case e1000_82547_rev_2:
263             e1000_write_phy_reg(hw, 0x1F73, 0x0099);
264             break;
265         default:
266             break;
267         }
268
269         e1000_write_phy_reg(hw, 0x0000, 0x3300);
270
271         msec_delay(20);
272
273         /* Now enable the transmitter */
274         e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
275
276         if(hw->mac_type == e1000_82547) {
277             uint16_t fused, fine, coarse;
278
279             /* Move to analog registers page */
280             e1000_read_phy_reg(hw, IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
281
282             if(!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
283                 e1000_read_phy_reg(hw, IGP01E1000_ANALOG_FUSE_STATUS, &fused);
284
285                 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
286                 coarse = fused & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
287
288                 if(coarse > IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
289                     coarse -= IGP01E1000_ANALOG_FUSE_COARSE_10;
290                     fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
291                 } else if(coarse == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
292                     fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
293
294                 fused = (fused & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
295                         (fine & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
296                         (coarse & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
297
298                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_CONTROL, fused);
299                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_BYPASS,
300                                     IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
301             }
302         }
303     }
304 }
305
306 /******************************************************************************
307  * Set the mac type member in the hw struct.
308  *
309  * hw - Struct containing variables accessed by shared code
310  *****************************************************************************/
311 int32_t
312 e1000_set_mac_type(struct e1000_hw *hw)
313 {
314     DEBUGFUNC("e1000_set_mac_type");
315
316     switch (hw->device_id) {
317     case E1000_DEV_ID_82542:
318         switch (hw->revision_id) {
319         case E1000_82542_2_0_REV_ID:
320             hw->mac_type = e1000_82542_rev2_0;
321             break;
322         case E1000_82542_2_1_REV_ID:
323             hw->mac_type = e1000_82542_rev2_1;
324             break;
325         default:
326             /* Invalid 82542 revision ID */
327             return -E1000_ERR_MAC_TYPE;
328         }
329         break;
330     case E1000_DEV_ID_82543GC_FIBER:
331     case E1000_DEV_ID_82543GC_COPPER:
332         hw->mac_type = e1000_82543;
333         break;
334     case E1000_DEV_ID_82544EI_COPPER:
335     case E1000_DEV_ID_82544EI_FIBER:
336     case E1000_DEV_ID_82544GC_COPPER:
337     case E1000_DEV_ID_82544GC_LOM:
338         hw->mac_type = e1000_82544;
339         break;
340     case E1000_DEV_ID_82540EM:
341     case E1000_DEV_ID_82540EM_LOM:
342     case E1000_DEV_ID_82540EP:
343     case E1000_DEV_ID_82540EP_LOM:
344     case E1000_DEV_ID_82540EP_LP:
345         hw->mac_type = e1000_82540;
346         break;
347     case E1000_DEV_ID_82545EM_COPPER:
348     case E1000_DEV_ID_82545EM_FIBER:
349         hw->mac_type = e1000_82545;
350         break;
351     case E1000_DEV_ID_82545GM_COPPER:
352     case E1000_DEV_ID_82545GM_FIBER:
353     case E1000_DEV_ID_82545GM_SERDES:
354         hw->mac_type = e1000_82545_rev_3;
355         break;
356     case E1000_DEV_ID_82546EB_COPPER:
357     case E1000_DEV_ID_82546EB_FIBER:
358     case E1000_DEV_ID_82546EB_QUAD_COPPER:
359         hw->mac_type = e1000_82546;
360         break;
361     case E1000_DEV_ID_82546GB_COPPER:
362     case E1000_DEV_ID_82546GB_FIBER:
363     case E1000_DEV_ID_82546GB_SERDES:
364     case E1000_DEV_ID_82546GB_PCIE:
365     case E1000_DEV_ID_82546GB_QUAD_COPPER:
366     case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
367         hw->mac_type = e1000_82546_rev_3;
368         break;
369     case E1000_DEV_ID_82541EI:
370     case E1000_DEV_ID_82541EI_MOBILE:
371     case E1000_DEV_ID_82541ER_LOM:
372         hw->mac_type = e1000_82541;
373         break;
374     case E1000_DEV_ID_82541ER:
375     case E1000_DEV_ID_82541GI:
376     case E1000_DEV_ID_82541GI_LF:
377     case E1000_DEV_ID_82541GI_MOBILE:
378         hw->mac_type = e1000_82541_rev_2;
379         break;
380     case E1000_DEV_ID_82547EI:
381     case E1000_DEV_ID_82547EI_MOBILE:
382         hw->mac_type = e1000_82547;
383         break;
384     case E1000_DEV_ID_82547GI:
385         hw->mac_type = e1000_82547_rev_2;
386         break;
387     case E1000_DEV_ID_82571EB_COPPER:
388     case E1000_DEV_ID_82571EB_FIBER:
389     case E1000_DEV_ID_82571EB_SERDES:
390             hw->mac_type = e1000_82571;
391         break;
392     case E1000_DEV_ID_82572EI_COPPER:
393     case E1000_DEV_ID_82572EI_FIBER:
394     case E1000_DEV_ID_82572EI_SERDES:
395     case E1000_DEV_ID_82572EI:
396         hw->mac_type = e1000_82572;
397         break;
398     case E1000_DEV_ID_82573E:
399     case E1000_DEV_ID_82573E_IAMT:
400     case E1000_DEV_ID_82573L:
401         hw->mac_type = e1000_82573;
402         break;
403     case E1000_DEV_ID_80003ES2LAN_COPPER_SPT:
404     case E1000_DEV_ID_80003ES2LAN_SERDES_SPT:
405     case E1000_DEV_ID_80003ES2LAN_COPPER_DPT:
406     case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
407         hw->mac_type = e1000_80003es2lan;
408         break;
409     case E1000_DEV_ID_ICH8_IGP_M_AMT:
410     case E1000_DEV_ID_ICH8_IGP_AMT:
411     case E1000_DEV_ID_ICH8_IGP_C:
412     case E1000_DEV_ID_ICH8_IFE:
413     case E1000_DEV_ID_ICH8_IGP_M:
414         hw->mac_type = e1000_ich8lan;
415         break;
416     default:
417         /* Should never have loaded on this device */
418         return -E1000_ERR_MAC_TYPE;
419     }
420
421     switch(hw->mac_type) {
422     case e1000_ich8lan:
423         hw->swfwhw_semaphore_present = TRUE;
424         hw->asf_firmware_present = TRUE;
425         break;
426     case e1000_80003es2lan:
427         hw->swfw_sync_present = TRUE;
428         /* fall through */
429     case e1000_82571:
430     case e1000_82572:
431     case e1000_82573:
432         hw->eeprom_semaphore_present = TRUE;
433         /* fall through */
434     case e1000_82541:
435     case e1000_82547:
436     case e1000_82541_rev_2:
437     case e1000_82547_rev_2:
438         hw->asf_firmware_present = TRUE;
439         break;
440     default:
441         break;
442     }
443
444     return E1000_SUCCESS;
445 }
446
447 /*****************************************************************************
448  * Set media type and TBI compatibility.
449  *
450  * hw - Struct containing variables accessed by shared code
451  * **************************************************************************/
452 void
453 e1000_set_media_type(struct e1000_hw *hw)
454 {
455     uint32_t status;
456
457     DEBUGFUNC("e1000_set_media_type");
458
459     if(hw->mac_type != e1000_82543) {
460         /* tbi_compatibility is only valid on 82543 */
461         hw->tbi_compatibility_en = FALSE;
462     }
463
464     switch (hw->device_id) {
465     case E1000_DEV_ID_82545GM_SERDES:
466     case E1000_DEV_ID_82546GB_SERDES:
467     case E1000_DEV_ID_82571EB_SERDES:
468     case E1000_DEV_ID_82572EI_SERDES:
469     case E1000_DEV_ID_80003ES2LAN_SERDES_DPT:
470         hw->media_type = e1000_media_type_internal_serdes;
471         break;
472     default:
473         switch (hw->mac_type) {
474         case e1000_82542_rev2_0:
475         case e1000_82542_rev2_1:
476             hw->media_type = e1000_media_type_fiber;
477             break;
478         case e1000_ich8lan:
479         case e1000_82573:
480             /* The STATUS_TBIMODE bit is reserved or reused for the this
481              * device.
482              */
483             hw->media_type = e1000_media_type_copper;
484             break;
485         default:
486             status = E1000_READ_REG(hw, STATUS);
487             if (status & E1000_STATUS_TBIMODE) {
488                 hw->media_type = e1000_media_type_fiber;
489                 /* tbi_compatibility not valid on fiber */
490                 hw->tbi_compatibility_en = FALSE;
491             } else {
492                 hw->media_type = e1000_media_type_copper;
493             }
494             break;
495         }
496     }
497 }
498
499 /******************************************************************************
500  * Reset the transmit and receive units; mask and clear all interrupts.
501  *
502  * hw - Struct containing variables accessed by shared code
503  *****************************************************************************/
504 int32_t
505 e1000_reset_hw(struct e1000_hw *hw)
506 {
507     uint32_t ctrl;
508     uint32_t ctrl_ext;
509     uint32_t icr;
510     uint32_t manc;
511     uint32_t led_ctrl;
512     uint32_t timeout;
513     uint32_t extcnf_ctrl;
514     int32_t ret_val;
515
516     DEBUGFUNC("e1000_reset_hw");
517
518     /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
519     if(hw->mac_type == e1000_82542_rev2_0) {
520         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
521         e1000_pci_clear_mwi(hw);
522     }
523
524     if(hw->bus_type == e1000_bus_type_pci_express) {
525         /* Prevent the PCI-E bus from sticking if there is no TLP connection
526          * on the last TLP read/write transaction when MAC is reset.
527          */
528         if(e1000_disable_pciex_master(hw) != E1000_SUCCESS) {
529             DEBUGOUT("PCI-E Master disable polling has failed.\n");
530         }
531     }
532
533     /* Clear interrupt mask to stop board from generating interrupts */
534     DEBUGOUT("Masking off all interrupts\n");
535     E1000_WRITE_REG(hw, IMC, 0xffffffff);
536
537     /* Disable the Transmit and Receive units.  Then delay to allow
538      * any pending transactions to complete before we hit the MAC with
539      * the global reset.
540      */
541     E1000_WRITE_REG(hw, RCTL, 0);
542     E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
543     E1000_WRITE_FLUSH(hw);
544
545     /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
546     hw->tbi_compatibility_on = FALSE;
547
548     /* Delay to allow any outstanding PCI transactions to complete before
549      * resetting the device
550      */
551     msec_delay(10);
552
553     ctrl = E1000_READ_REG(hw, CTRL);
554
555     /* Must reset the PHY before resetting the MAC */
556     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
557         E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_PHY_RST));
558         msec_delay(5);
559     }
560
561     /* Must acquire the MDIO ownership before MAC reset.
562      * Ownership defaults to firmware after a reset. */
563     if(hw->mac_type == e1000_82573) {
564         timeout = 10;
565
566         extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
567         extcnf_ctrl |= E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP;
568
569         do {
570             E1000_WRITE_REG(hw, EXTCNF_CTRL, extcnf_ctrl);
571             extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
572
573             if(extcnf_ctrl & E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP)
574                 break;
575             else
576                 extcnf_ctrl |= E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP;
577
578             msec_delay(2);
579             timeout--;
580         } while(timeout);
581     }
582
583     /* Workaround for ICH8 bit corruption issue in FIFO memory */
584     if (hw->mac_type == e1000_ich8lan) {
585         /* Set Tx and Rx buffer allocation to 8k apiece. */
586         E1000_WRITE_REG(hw, PBA, E1000_PBA_8K);
587         /* Set Packet Buffer Size to 16k. */
588         E1000_WRITE_REG(hw, PBS, E1000_PBS_16K);
589     }
590
591     /* Issue a global reset to the MAC.  This will reset the chip's
592      * transmit, receive, DMA, and link units.  It will not effect
593      * the current PCI configuration.  The global reset bit is self-
594      * clearing, and should clear within a microsecond.
595      */
596     DEBUGOUT("Issuing a global reset to MAC\n");
597
598     switch(hw->mac_type) {
599         case e1000_82544:
600         case e1000_82540:
601         case e1000_82545:
602         case e1000_82546:
603         case e1000_82541:
604         case e1000_82541_rev_2:
605             /* These controllers can't ack the 64-bit write when issuing the
606              * reset, so use IO-mapping as a workaround to issue the reset */
607             E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
608             break;
609         case e1000_82545_rev_3:
610         case e1000_82546_rev_3:
611             /* Reset is performed on a shadow of the control register */
612             E1000_WRITE_REG(hw, CTRL_DUP, (ctrl | E1000_CTRL_RST));
613             break;
614         case e1000_ich8lan:
615             if (!hw->phy_reset_disable &&
616                 e1000_check_phy_reset_block(hw) == E1000_SUCCESS) {
617                 /* e1000_ich8lan PHY HW reset requires MAC CORE reset
618                  * at the same time to make sure the interface between
619                  * MAC and the external PHY is reset.
620                  */
621                 ctrl |= E1000_CTRL_PHY_RST;
622             }
623
624             e1000_get_software_flag(hw);
625             E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
626             msec_delay(5);
627             break;
628         default:
629             E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
630             break;
631     }
632
633     /* After MAC reset, force reload of EEPROM to restore power-on settings to
634      * device.  Later controllers reload the EEPROM automatically, so just wait
635      * for reload to complete.
636      */
637     switch(hw->mac_type) {
638         case e1000_82542_rev2_0:
639         case e1000_82542_rev2_1:
640         case e1000_82543:
641         case e1000_82544:
642             /* Wait for reset to complete */
643             udelay(10);
644             ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
645             ctrl_ext |= E1000_CTRL_EXT_EE_RST;
646             E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
647             E1000_WRITE_FLUSH(hw);
648             /* Wait for EEPROM reload */
649             msec_delay(2);
650             break;
651         case e1000_82541:
652         case e1000_82541_rev_2:
653         case e1000_82547:
654         case e1000_82547_rev_2:
655             /* Wait for EEPROM reload */
656             msec_delay(20);
657             break;
658         case e1000_82573:
659             if (e1000_is_onboard_nvm_eeprom(hw) == FALSE) {
660                 udelay(10);
661                 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
662                 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
663                 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
664                 E1000_WRITE_FLUSH(hw);
665             }
666             /* fall through */
667         case e1000_82571:
668         case e1000_82572:
669         case e1000_ich8lan:
670         case e1000_80003es2lan:
671             ret_val = e1000_get_auto_rd_done(hw);
672             if(ret_val)
673                 /* We don't want to continue accessing MAC registers. */
674                 return ret_val;
675             break;
676         default:
677             /* Wait for EEPROM reload (it happens automatically) */
678             msec_delay(5);
679             break;
680     }
681
682     /* Disable HW ARPs on ASF enabled adapters */
683     if(hw->mac_type >= e1000_82540 && hw->mac_type <= e1000_82547_rev_2) {
684         manc = E1000_READ_REG(hw, MANC);
685         manc &= ~(E1000_MANC_ARP_EN);
686         E1000_WRITE_REG(hw, MANC, manc);
687     }
688
689     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
690         e1000_phy_init_script(hw);
691
692         /* Configure activity LED after PHY reset */
693         led_ctrl = E1000_READ_REG(hw, LEDCTL);
694         led_ctrl &= IGP_ACTIVITY_LED_MASK;
695         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
696         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
697     }
698
699     /* Clear interrupt mask to stop board from generating interrupts */
700     DEBUGOUT("Masking off all interrupts\n");
701     E1000_WRITE_REG(hw, IMC, 0xffffffff);
702
703     /* Clear any pending interrupt events. */
704     icr = E1000_READ_REG(hw, ICR);
705
706     /* If MWI was previously enabled, reenable it. */
707     if(hw->mac_type == e1000_82542_rev2_0) {
708         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
709             e1000_pci_set_mwi(hw);
710     }
711
712     if (hw->mac_type == e1000_ich8lan) {
713         uint32_t kab = E1000_READ_REG(hw, KABGTXD);
714         kab |= E1000_KABGTXD_BGSQLBIAS;
715         E1000_WRITE_REG(hw, KABGTXD, kab);
716     }
717
718     return E1000_SUCCESS;
719 }
720
721 /******************************************************************************
722  * Performs basic configuration of the adapter.
723  *
724  * hw - Struct containing variables accessed by shared code
725  *
726  * Assumes that the controller has previously been reset and is in a
727  * post-reset uninitialized state. Initializes the receive address registers,
728  * multicast table, and VLAN filter table. Calls routines to setup link
729  * configuration and flow control settings. Clears all on-chip counters. Leaves
730  * the transmit and receive units disabled and uninitialized.
731  *****************************************************************************/
732 int32_t
733 e1000_init_hw(struct e1000_hw *hw)
734 {
735     uint32_t ctrl;
736     uint32_t i;
737     int32_t ret_val;
738     uint16_t pcix_cmd_word;
739     uint16_t pcix_stat_hi_word;
740     uint16_t cmd_mmrbc;
741     uint16_t stat_mmrbc;
742     uint32_t mta_size;
743     uint32_t reg_data;
744     uint32_t ctrl_ext;
745
746     DEBUGFUNC("e1000_init_hw");
747
748     /* force full DMA clock frequency for 10/100 on ICH8 A0-B0 */
749     if (hw->mac_type == e1000_ich8lan) {
750         reg_data = E1000_READ_REG(hw, TARC0);
751         reg_data |= 0x30000000;
752         E1000_WRITE_REG(hw, TARC0, reg_data);
753
754         reg_data = E1000_READ_REG(hw, STATUS);
755         reg_data &= ~0x80000000;
756         E1000_WRITE_REG(hw, STATUS, reg_data);
757     }
758
759     /* Initialize Identification LED */
760     ret_val = e1000_id_led_init(hw);
761     if(ret_val) {
762         DEBUGOUT("Error Initializing Identification LED\n");
763         return ret_val;
764     }
765
766     /* Set the media type and TBI compatibility */
767     e1000_set_media_type(hw);
768
769     /* Disabling VLAN filtering. */
770     DEBUGOUT("Initializing the IEEE VLAN\n");
771     /* VET hardcoded to standard value and VFTA removed in ICH8 LAN */
772     if (hw->mac_type != e1000_ich8lan) {
773         if (hw->mac_type < e1000_82545_rev_3)
774             E1000_WRITE_REG(hw, VET, 0);
775         e1000_clear_vfta(hw);
776     }
777
778     /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
779     if(hw->mac_type == e1000_82542_rev2_0) {
780         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
781         e1000_pci_clear_mwi(hw);
782         E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
783         E1000_WRITE_FLUSH(hw);
784         msec_delay(5);
785     }
786
787     /* Setup the receive address. This involves initializing all of the Receive
788      * Address Registers (RARs 0 - 15).
789      */
790     e1000_init_rx_addrs(hw);
791
792     /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
793     if(hw->mac_type == e1000_82542_rev2_0) {
794         E1000_WRITE_REG(hw, RCTL, 0);
795         E1000_WRITE_FLUSH(hw);
796         msec_delay(1);
797         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
798             e1000_pci_set_mwi(hw);
799     }
800
801     /* Zero out the Multicast HASH table */
802     DEBUGOUT("Zeroing the MTA\n");
803     mta_size = E1000_MC_TBL_SIZE;
804     if (hw->mac_type == e1000_ich8lan)
805         mta_size = E1000_MC_TBL_SIZE_ICH8LAN;
806     for(i = 0; i < mta_size; i++) {
807         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
808         /* use write flush to prevent Memory Write Block (MWB) from
809          * occuring when accessing our register space */
810         E1000_WRITE_FLUSH(hw);
811     }
812
813     /* Set the PCI priority bit correctly in the CTRL register.  This
814      * determines if the adapter gives priority to receives, or if it
815      * gives equal priority to transmits and receives.  Valid only on
816      * 82542 and 82543 silicon.
817      */
818     if(hw->dma_fairness && hw->mac_type <= e1000_82543) {
819         ctrl = E1000_READ_REG(hw, CTRL);
820         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PRIOR);
821     }
822
823     switch(hw->mac_type) {
824     case e1000_82545_rev_3:
825     case e1000_82546_rev_3:
826         break;
827     default:
828         /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
829         if(hw->bus_type == e1000_bus_type_pcix) {
830             e1000_read_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd_word);
831             e1000_read_pci_cfg(hw, PCIX_STATUS_REGISTER_HI,
832                 &pcix_stat_hi_word);
833             cmd_mmrbc = (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
834                 PCIX_COMMAND_MMRBC_SHIFT;
835             stat_mmrbc = (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
836                 PCIX_STATUS_HI_MMRBC_SHIFT;
837             if(stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
838                 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
839             if(cmd_mmrbc > stat_mmrbc) {
840                 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
841                 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
842                 e1000_write_pci_cfg(hw, PCIX_COMMAND_REGISTER,
843                     &pcix_cmd_word);
844             }
845         }
846         break;
847     }
848
849     /* More time needed for PHY to initialize */
850     if (hw->mac_type == e1000_ich8lan)
851         msec_delay(15);
852
853     /* Call a subroutine to configure the link and setup flow control. */
854     ret_val = e1000_setup_link(hw);
855
856     /* Set the transmit descriptor write-back policy */
857     if(hw->mac_type > e1000_82544) {
858         ctrl = E1000_READ_REG(hw, TXDCTL);
859         ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
860         switch (hw->mac_type) {
861         default:
862             break;
863         case e1000_82571:
864         case e1000_82572:
865         case e1000_82573:
866         case e1000_ich8lan:
867         case e1000_80003es2lan:
868             ctrl |= E1000_TXDCTL_COUNT_DESC;
869             break;
870         }
871         E1000_WRITE_REG(hw, TXDCTL, ctrl);
872     }
873
874     if (hw->mac_type == e1000_82573) {
875         e1000_enable_tx_pkt_filtering(hw);
876     }
877
878     switch (hw->mac_type) {
879     default:
880         break;
881     case e1000_80003es2lan:
882         /* Enable retransmit on late collisions */
883         reg_data = E1000_READ_REG(hw, TCTL);
884         reg_data |= E1000_TCTL_RTLC;
885         E1000_WRITE_REG(hw, TCTL, reg_data);
886
887         /* Configure Gigabit Carry Extend Padding */
888         reg_data = E1000_READ_REG(hw, TCTL_EXT);
889         reg_data &= ~E1000_TCTL_EXT_GCEX_MASK;
890         reg_data |= DEFAULT_80003ES2LAN_TCTL_EXT_GCEX;
891         E1000_WRITE_REG(hw, TCTL_EXT, reg_data);
892
893         /* Configure Transmit Inter-Packet Gap */
894         reg_data = E1000_READ_REG(hw, TIPG);
895         reg_data &= ~E1000_TIPG_IPGT_MASK;
896         reg_data |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
897         E1000_WRITE_REG(hw, TIPG, reg_data);
898
899         reg_data = E1000_READ_REG_ARRAY(hw, FFLT, 0x0001);
900         reg_data &= ~0x00100000;
901         E1000_WRITE_REG_ARRAY(hw, FFLT, 0x0001, reg_data);
902         /* Fall through */
903     case e1000_82571:
904     case e1000_82572:
905     case e1000_ich8lan:
906         ctrl = E1000_READ_REG(hw, TXDCTL1);
907         ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
908         if(hw->mac_type >= e1000_82571)
909             ctrl |= E1000_TXDCTL_COUNT_DESC;
910         E1000_WRITE_REG(hw, TXDCTL1, ctrl);
911         break;
912     }
913
914
915
916     if (hw->mac_type == e1000_82573) {
917         uint32_t gcr = E1000_READ_REG(hw, GCR);
918         gcr |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
919         E1000_WRITE_REG(hw, GCR, gcr);
920     }
921
922     /* Clear all of the statistics registers (clear on read).  It is
923      * important that we do this after we have tried to establish link
924      * because the symbol error count will increment wildly if there
925      * is no link.
926      */
927     e1000_clear_hw_cntrs(hw);
928
929     /* ICH8 No-snoop bits are opposite polarity.
930      * Set to snoop by default after reset. */
931     if (hw->mac_type == e1000_ich8lan)
932         e1000_set_pci_ex_no_snoop(hw, PCI_EX_82566_SNOOP_ALL);
933
934     if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
935         hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
936         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
937         /* Relaxed ordering must be disabled to avoid a parity
938          * error crash in a PCI slot. */
939         ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
940         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
941     }
942
943     return ret_val;
944 }
945
946 /******************************************************************************
947  * Adjust SERDES output amplitude based on EEPROM setting.
948  *
949  * hw - Struct containing variables accessed by shared code.
950  *****************************************************************************/
951 static int32_t
952 e1000_adjust_serdes_amplitude(struct e1000_hw *hw)
953 {
954     uint16_t eeprom_data;
955     int32_t  ret_val;
956
957     DEBUGFUNC("e1000_adjust_serdes_amplitude");
958
959     if(hw->media_type != e1000_media_type_internal_serdes)
960         return E1000_SUCCESS;
961
962     switch(hw->mac_type) {
963     case e1000_82545_rev_3:
964     case e1000_82546_rev_3:
965         break;
966     default:
967         return E1000_SUCCESS;
968     }
969
970     ret_val = e1000_read_eeprom(hw, EEPROM_SERDES_AMPLITUDE, 1, &eeprom_data);
971     if (ret_val) {
972         return ret_val;
973     }
974
975     if(eeprom_data != EEPROM_RESERVED_WORD) {
976         /* Adjust SERDES output amplitude only. */
977         eeprom_data &= EEPROM_SERDES_AMPLITUDE_MASK;
978         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_EXT_CTRL, eeprom_data);
979         if(ret_val)
980             return ret_val;
981     }
982
983     return E1000_SUCCESS;
984 }
985
986 /******************************************************************************
987  * Configures flow control and link settings.
988  *
989  * hw - Struct containing variables accessed by shared code
990  *
991  * Determines which flow control settings to use. Calls the apropriate media-
992  * specific link configuration function. Configures the flow control settings.
993  * Assuming the adapter has a valid link partner, a valid link should be
994  * established. Assumes the hardware has previously been reset and the
995  * transmitter and receiver are not enabled.
996  *****************************************************************************/
997 int32_t
998 e1000_setup_link(struct e1000_hw *hw)
999 {
1000     uint32_t ctrl_ext;
1001     int32_t ret_val;
1002     uint16_t eeprom_data;
1003
1004     DEBUGFUNC("e1000_setup_link");
1005
1006     /* In the case of the phy reset being blocked, we already have a link.
1007      * We do not have to set it up again. */
1008     if (e1000_check_phy_reset_block(hw))
1009         return E1000_SUCCESS;
1010
1011     /* Read and store word 0x0F of the EEPROM. This word contains bits
1012      * that determine the hardware's default PAUSE (flow control) mode,
1013      * a bit that determines whether the HW defaults to enabling or
1014      * disabling auto-negotiation, and the direction of the
1015      * SW defined pins. If there is no SW over-ride of the flow
1016      * control setting, then the variable hw->fc will
1017      * be initialized based on a value in the EEPROM.
1018      */
1019     if (hw->fc == e1000_fc_default) {
1020         switch (hw->mac_type) {
1021         case e1000_ich8lan:
1022         case e1000_82573:
1023             hw->fc = e1000_fc_full;
1024             break;
1025         default:
1026             ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
1027                                         1, &eeprom_data);
1028             if (ret_val) {
1029                 DEBUGOUT("EEPROM Read Error\n");
1030                 return -E1000_ERR_EEPROM;
1031             }
1032             if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
1033                 hw->fc = e1000_fc_none;
1034             else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
1035                     EEPROM_WORD0F_ASM_DIR)
1036                 hw->fc = e1000_fc_tx_pause;
1037             else
1038                 hw->fc = e1000_fc_full;
1039             break;
1040         }
1041     }
1042
1043     /* We want to save off the original Flow Control configuration just
1044      * in case we get disconnected and then reconnected into a different
1045      * hub or switch with different Flow Control capabilities.
1046      */
1047     if(hw->mac_type == e1000_82542_rev2_0)
1048         hw->fc &= (~e1000_fc_tx_pause);
1049
1050     if((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
1051         hw->fc &= (~e1000_fc_rx_pause);
1052
1053     hw->original_fc = hw->fc;
1054
1055     DEBUGOUT1("After fix-ups FlowControl is now = %x\n", hw->fc);
1056
1057     /* Take the 4 bits from EEPROM word 0x0F that determine the initial
1058      * polarity value for the SW controlled pins, and setup the
1059      * Extended Device Control reg with that info.
1060      * This is needed because one of the SW controlled pins is used for
1061      * signal detection.  So this should be done before e1000_setup_pcs_link()
1062      * or e1000_phy_setup() is called.
1063      */
1064     if (hw->mac_type == e1000_82543) {
1065                 ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
1066                                                                         1, &eeprom_data);
1067                 if (ret_val) {
1068                         DEBUGOUT("EEPROM Read Error\n");
1069                         return -E1000_ERR_EEPROM;
1070                 }
1071         ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
1072                     SWDPIO__EXT_SHIFT);
1073         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
1074     }
1075
1076     /* Call the necessary subroutine to configure the link. */
1077     ret_val = (hw->media_type == e1000_media_type_copper) ?
1078               e1000_setup_copper_link(hw) :
1079               e1000_setup_fiber_serdes_link(hw);
1080
1081     /* Initialize the flow control address, type, and PAUSE timer
1082      * registers to their default values.  This is done even if flow
1083      * control is disabled, because it does not hurt anything to
1084      * initialize these registers.
1085      */
1086     DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
1087
1088     /* FCAL/H and FCT are hardcoded to standard values in e1000_ich8lan. */
1089     if (hw->mac_type != e1000_ich8lan) {
1090         E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
1091         E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
1092         E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
1093     }
1094
1095     E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
1096
1097     /* Set the flow control receive threshold registers.  Normally,
1098      * these registers will be set to a default threshold that may be
1099      * adjusted later by the driver's runtime code.  However, if the
1100      * ability to transmit pause frames in not enabled, then these
1101      * registers will be set to 0.
1102      */
1103     if(!(hw->fc & e1000_fc_tx_pause)) {
1104         E1000_WRITE_REG(hw, FCRTL, 0);
1105         E1000_WRITE_REG(hw, FCRTH, 0);
1106     } else {
1107         /* We need to set up the Receive Threshold high and low water marks
1108          * as well as (optionally) enabling the transmission of XON frames.
1109          */
1110         if(hw->fc_send_xon) {
1111             E1000_WRITE_REG(hw, FCRTL, (hw->fc_low_water | E1000_FCRTL_XONE));
1112             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
1113         } else {
1114             E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
1115             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
1116         }
1117     }
1118     return ret_val;
1119 }
1120
1121 /******************************************************************************
1122  * Sets up link for a fiber based or serdes based adapter
1123  *
1124  * hw - Struct containing variables accessed by shared code
1125  *
1126  * Manipulates Physical Coding Sublayer functions in order to configure
1127  * link. Assumes the hardware has been previously reset and the transmitter
1128  * and receiver are not enabled.
1129  *****************************************************************************/
1130 static int32_t
1131 e1000_setup_fiber_serdes_link(struct e1000_hw *hw)
1132 {
1133     uint32_t ctrl;
1134     uint32_t status;
1135     uint32_t txcw = 0;
1136     uint32_t i;
1137     uint32_t signal = 0;
1138     int32_t ret_val;
1139
1140     DEBUGFUNC("e1000_setup_fiber_serdes_link");
1141
1142     /* On 82571 and 82572 Fiber connections, SerDes loopback mode persists
1143      * until explicitly turned off or a power cycle is performed.  A read to
1144      * the register does not indicate its status.  Therefore, we ensure
1145      * loopback mode is disabled during initialization.
1146      */
1147     if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572)
1148         E1000_WRITE_REG(hw, SCTL, E1000_DISABLE_SERDES_LOOPBACK);
1149
1150     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
1151      * set when the optics detect a signal. On older adapters, it will be
1152      * cleared when there is a signal.  This applies to fiber media only.
1153      * If we're on serdes media, adjust the output amplitude to value set in
1154      * the EEPROM.
1155      */
1156     ctrl = E1000_READ_REG(hw, CTRL);
1157     if(hw->media_type == e1000_media_type_fiber)
1158         signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
1159
1160     ret_val = e1000_adjust_serdes_amplitude(hw);
1161     if(ret_val)
1162         return ret_val;
1163
1164     /* Take the link out of reset */
1165     ctrl &= ~(E1000_CTRL_LRST);
1166
1167     /* Adjust VCO speed to improve BER performance */
1168     ret_val = e1000_set_vco_speed(hw);
1169     if(ret_val)
1170         return ret_val;
1171
1172     e1000_config_collision_dist(hw);
1173
1174     /* Check for a software override of the flow control settings, and setup
1175      * the device accordingly.  If auto-negotiation is enabled, then software
1176      * will have to set the "PAUSE" bits to the correct value in the Tranmsit
1177      * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
1178      * auto-negotiation is disabled, then software will have to manually
1179      * configure the two flow control enable bits in the CTRL register.
1180      *
1181      * The possible values of the "fc" parameter are:
1182      *      0:  Flow control is completely disabled
1183      *      1:  Rx flow control is enabled (we can receive pause frames, but
1184      *          not send pause frames).
1185      *      2:  Tx flow control is enabled (we can send pause frames but we do
1186      *          not support receiving pause frames).
1187      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1188      */
1189     switch (hw->fc) {
1190     case e1000_fc_none:
1191         /* Flow control is completely disabled by a software over-ride. */
1192         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
1193         break;
1194     case e1000_fc_rx_pause:
1195         /* RX Flow control is enabled and TX Flow control is disabled by a
1196          * software over-ride. Since there really isn't a way to advertise
1197          * that we are capable of RX Pause ONLY, we will advertise that we
1198          * support both symmetric and asymmetric RX PAUSE. Later, we will
1199          *  disable the adapter's ability to send PAUSE frames.
1200          */
1201         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
1202         break;
1203     case e1000_fc_tx_pause:
1204         /* TX Flow control is enabled, and RX Flow control is disabled, by a
1205          * software over-ride.
1206          */
1207         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
1208         break;
1209     case e1000_fc_full:
1210         /* Flow control (both RX and TX) is enabled by a software over-ride. */
1211         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
1212         break;
1213     default:
1214         DEBUGOUT("Flow control param set incorrectly\n");
1215         return -E1000_ERR_CONFIG;
1216         break;
1217     }
1218
1219     /* Since auto-negotiation is enabled, take the link out of reset (the link
1220      * will be in reset, because we previously reset the chip). This will
1221      * restart auto-negotiation.  If auto-neogtiation is successful then the
1222      * link-up status bit will be set and the flow control enable bits (RFCE
1223      * and TFCE) will be set according to their negotiated value.
1224      */
1225     DEBUGOUT("Auto-negotiation enabled\n");
1226
1227     E1000_WRITE_REG(hw, TXCW, txcw);
1228     E1000_WRITE_REG(hw, CTRL, ctrl);
1229     E1000_WRITE_FLUSH(hw);
1230
1231     hw->txcw = txcw;
1232     msec_delay(1);
1233
1234     /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
1235      * indication in the Device Status Register.  Time-out if a link isn't
1236      * seen in 500 milliseconds seconds (Auto-negotiation should complete in
1237      * less than 500 milliseconds even if the other end is doing it in SW).
1238      * For internal serdes, we just assume a signal is present, then poll.
1239      */
1240     if(hw->media_type == e1000_media_type_internal_serdes ||
1241        (E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
1242         DEBUGOUT("Looking for Link\n");
1243         for(i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
1244             msec_delay(10);
1245             status = E1000_READ_REG(hw, STATUS);
1246             if(status & E1000_STATUS_LU) break;
1247         }
1248         if(i == (LINK_UP_TIMEOUT / 10)) {
1249             DEBUGOUT("Never got a valid link from auto-neg!!!\n");
1250             hw->autoneg_failed = 1;
1251             /* AutoNeg failed to achieve a link, so we'll call
1252              * e1000_check_for_link. This routine will force the link up if
1253              * we detect a signal. This will allow us to communicate with
1254              * non-autonegotiating link partners.
1255              */
1256             ret_val = e1000_check_for_link(hw);
1257             if(ret_val) {
1258                 DEBUGOUT("Error while checking for link\n");
1259                 return ret_val;
1260             }
1261             hw->autoneg_failed = 0;
1262         } else {
1263             hw->autoneg_failed = 0;
1264             DEBUGOUT("Valid Link Found\n");
1265         }
1266     } else {
1267         DEBUGOUT("No Signal Detected\n");
1268     }
1269     return E1000_SUCCESS;
1270 }
1271
1272 /******************************************************************************
1273 * Make sure we have a valid PHY and change PHY mode before link setup.
1274 *
1275 * hw - Struct containing variables accessed by shared code
1276 ******************************************************************************/
1277 static int32_t
1278 e1000_copper_link_preconfig(struct e1000_hw *hw)
1279 {
1280     uint32_t ctrl;
1281     int32_t ret_val;
1282     uint16_t phy_data;
1283
1284     DEBUGFUNC("e1000_copper_link_preconfig");
1285
1286     ctrl = E1000_READ_REG(hw, CTRL);
1287     /* With 82543, we need to force speed and duplex on the MAC equal to what
1288      * the PHY speed and duplex configuration is. In addition, we need to
1289      * perform a hardware reset on the PHY to take it out of reset.
1290      */
1291     if(hw->mac_type > e1000_82543) {
1292         ctrl |= E1000_CTRL_SLU;
1293         ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1294         E1000_WRITE_REG(hw, CTRL, ctrl);
1295     } else {
1296         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
1297         E1000_WRITE_REG(hw, CTRL, ctrl);
1298         ret_val = e1000_phy_hw_reset(hw);
1299         if(ret_val)
1300             return ret_val;
1301     }
1302
1303     /* Make sure we have a valid PHY */
1304     ret_val = e1000_detect_gig_phy(hw);
1305     if(ret_val) {
1306         DEBUGOUT("Error, did not detect valid phy.\n");
1307         return ret_val;
1308     }
1309     DEBUGOUT1("Phy ID = %x \n", hw->phy_id);
1310
1311     /* Set PHY to class A mode (if necessary) */
1312     ret_val = e1000_set_phy_mode(hw);
1313     if(ret_val)
1314         return ret_val;
1315
1316     if((hw->mac_type == e1000_82545_rev_3) ||
1317        (hw->mac_type == e1000_82546_rev_3)) {
1318         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1319         phy_data |= 0x00000008;
1320         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1321     }
1322
1323     if(hw->mac_type <= e1000_82543 ||
1324        hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
1325        hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2)
1326         hw->phy_reset_disable = FALSE;
1327
1328    return E1000_SUCCESS;
1329 }
1330
1331
1332 /********************************************************************
1333 * Copper link setup for e1000_phy_igp series.
1334 *
1335 * hw - Struct containing variables accessed by shared code
1336 *********************************************************************/
1337 static int32_t
1338 e1000_copper_link_igp_setup(struct e1000_hw *hw)
1339 {
1340     uint32_t led_ctrl;
1341     int32_t ret_val;
1342     uint16_t phy_data;
1343
1344     DEBUGFUNC("e1000_copper_link_igp_setup");
1345
1346     if (hw->phy_reset_disable)
1347         return E1000_SUCCESS;
1348
1349     ret_val = e1000_phy_reset(hw);
1350     if (ret_val) {
1351         DEBUGOUT("Error Resetting the PHY\n");
1352         return ret_val;
1353     }
1354
1355     /* Wait 10ms for MAC to configure PHY from eeprom settings */
1356     msec_delay(15);
1357     if (hw->mac_type != e1000_ich8lan) {
1358     /* Configure activity LED after PHY reset */
1359     led_ctrl = E1000_READ_REG(hw, LEDCTL);
1360     led_ctrl &= IGP_ACTIVITY_LED_MASK;
1361     led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
1362     E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
1363     }
1364
1365     /* The NVM settings will configure LPLU in D3 for IGP2 and IGP3 PHYs */
1366     if (hw->phy_type == e1000_phy_igp) {
1367         /* disable lplu d3 during driver init */
1368         ret_val = e1000_set_d3_lplu_state(hw, FALSE);
1369         if (ret_val) {
1370             DEBUGOUT("Error Disabling LPLU D3\n");
1371             return ret_val;
1372         }
1373     }
1374
1375     /* disable lplu d0 during driver init */
1376     ret_val = e1000_set_d0_lplu_state(hw, FALSE);
1377     if (ret_val) {
1378         DEBUGOUT("Error Disabling LPLU D0\n");
1379         return ret_val;
1380     }
1381     /* Configure mdi-mdix settings */
1382     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1383     if (ret_val)
1384         return ret_val;
1385
1386     if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
1387         hw->dsp_config_state = e1000_dsp_config_disabled;
1388         /* Force MDI for earlier revs of the IGP PHY */
1389         phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX | IGP01E1000_PSCR_FORCE_MDI_MDIX);
1390         hw->mdix = 1;
1391
1392     } else {
1393         hw->dsp_config_state = e1000_dsp_config_enabled;
1394         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1395
1396         switch (hw->mdix) {
1397         case 1:
1398             phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1399             break;
1400         case 2:
1401             phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
1402             break;
1403         case 0:
1404         default:
1405             phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
1406             break;
1407         }
1408     }
1409     ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1410     if(ret_val)
1411         return ret_val;
1412
1413     /* set auto-master slave resolution settings */
1414     if(hw->autoneg) {
1415         e1000_ms_type phy_ms_setting = hw->master_slave;
1416
1417         if(hw->ffe_config_state == e1000_ffe_config_active)
1418             hw->ffe_config_state = e1000_ffe_config_enabled;
1419
1420         if(hw->dsp_config_state == e1000_dsp_config_activated)
1421             hw->dsp_config_state = e1000_dsp_config_enabled;
1422
1423         /* when autonegotiation advertisment is only 1000Mbps then we
1424           * should disable SmartSpeed and enable Auto MasterSlave
1425           * resolution as hardware default. */
1426         if(hw->autoneg_advertised == ADVERTISE_1000_FULL) {
1427             /* Disable SmartSpeed */
1428             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
1429             if(ret_val)
1430                 return ret_val;
1431             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1432             ret_val = e1000_write_phy_reg(hw,
1433                                                   IGP01E1000_PHY_PORT_CONFIG,
1434                                                   phy_data);
1435             if(ret_val)
1436                 return ret_val;
1437             /* Set auto Master/Slave resolution process */
1438             ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1439             if(ret_val)
1440                 return ret_val;
1441             phy_data &= ~CR_1000T_MS_ENABLE;
1442             ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1443             if(ret_val)
1444                 return ret_val;
1445         }
1446
1447         ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1448         if(ret_val)
1449             return ret_val;
1450
1451         /* load defaults for future use */
1452         hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
1453                                         ((phy_data & CR_1000T_MS_VALUE) ?
1454                                          e1000_ms_force_master :
1455                                          e1000_ms_force_slave) :
1456                                          e1000_ms_auto;
1457
1458         switch (phy_ms_setting) {
1459         case e1000_ms_force_master:
1460             phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
1461             break;
1462         case e1000_ms_force_slave:
1463             phy_data |= CR_1000T_MS_ENABLE;
1464             phy_data &= ~(CR_1000T_MS_VALUE);
1465             break;
1466         case e1000_ms_auto:
1467             phy_data &= ~CR_1000T_MS_ENABLE;
1468             default:
1469             break;
1470         }
1471         ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1472         if(ret_val)
1473             return ret_val;
1474     }
1475
1476     return E1000_SUCCESS;
1477 }
1478
1479 /********************************************************************
1480 * Copper link setup for e1000_phy_gg82563 series.
1481 *
1482 * hw - Struct containing variables accessed by shared code
1483 *********************************************************************/
1484 static int32_t
1485 e1000_copper_link_ggp_setup(struct e1000_hw *hw)
1486 {
1487     int32_t ret_val;
1488     uint16_t phy_data;
1489     uint32_t reg_data;
1490
1491     DEBUGFUNC("e1000_copper_link_ggp_setup");
1492
1493     if(!hw->phy_reset_disable) {
1494
1495         /* Enable CRS on TX for half-duplex operation. */
1496         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
1497                                      &phy_data);
1498         if(ret_val)
1499             return ret_val;
1500
1501         phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
1502         /* Use 25MHz for both link down and 1000BASE-T for Tx clock */
1503         phy_data |= GG82563_MSCR_TX_CLK_1000MBPS_25MHZ;
1504
1505         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL,
1506                                       phy_data);
1507         if(ret_val)
1508             return ret_val;
1509
1510         /* Options:
1511          *   MDI/MDI-X = 0 (default)
1512          *   0 - Auto for all speeds
1513          *   1 - MDI mode
1514          *   2 - MDI-X mode
1515          *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1516          */
1517         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_SPEC_CTRL, &phy_data);
1518         if(ret_val)
1519             return ret_val;
1520
1521         phy_data &= ~GG82563_PSCR_CROSSOVER_MODE_MASK;
1522
1523         switch (hw->mdix) {
1524         case 1:
1525             phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDI;
1526             break;
1527         case 2:
1528             phy_data |= GG82563_PSCR_CROSSOVER_MODE_MDIX;
1529             break;
1530         case 0:
1531         default:
1532             phy_data |= GG82563_PSCR_CROSSOVER_MODE_AUTO;
1533             break;
1534         }
1535
1536         /* Options:
1537          *   disable_polarity_correction = 0 (default)
1538          *       Automatic Correction for Reversed Cable Polarity
1539          *   0 - Disabled
1540          *   1 - Enabled
1541          */
1542         phy_data &= ~GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
1543         if(hw->disable_polarity_correction == 1)
1544             phy_data |= GG82563_PSCR_POLARITY_REVERSAL_DISABLE;
1545         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_SPEC_CTRL, phy_data);
1546
1547         if(ret_val)
1548             return ret_val;
1549
1550         /* SW Reset the PHY so all changes take effect */
1551         ret_val = e1000_phy_reset(hw);
1552         if (ret_val) {
1553             DEBUGOUT("Error Resetting the PHY\n");
1554             return ret_val;
1555         }
1556     } /* phy_reset_disable */
1557
1558     if (hw->mac_type == e1000_80003es2lan) {
1559         /* Bypass RX and TX FIFO's */
1560         ret_val = e1000_write_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_FIFO_CTRL,
1561                                        E1000_KUMCTRLSTA_FIFO_CTRL_RX_BYPASS |
1562                                        E1000_KUMCTRLSTA_FIFO_CTRL_TX_BYPASS);
1563         if (ret_val)
1564             return ret_val;
1565
1566         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_SPEC_CTRL_2, &phy_data);
1567         if (ret_val)
1568             return ret_val;
1569
1570         phy_data &= ~GG82563_PSCR2_REVERSE_AUTO_NEG;
1571         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_SPEC_CTRL_2, phy_data);
1572
1573         if (ret_val)
1574             return ret_val;
1575
1576         reg_data = E1000_READ_REG(hw, CTRL_EXT);
1577         reg_data &= ~(E1000_CTRL_EXT_LINK_MODE_MASK);
1578         E1000_WRITE_REG(hw, CTRL_EXT, reg_data);
1579
1580         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_PWR_MGMT_CTRL,
1581                                           &phy_data);
1582         if (ret_val)
1583             return ret_val;
1584
1585         /* Do not init these registers when the HW is in IAMT mode, since the
1586          * firmware will have already initialized them.  We only initialize
1587          * them if the HW is not in IAMT mode.
1588          */
1589         if (e1000_check_mng_mode(hw) == FALSE) {
1590             /* Enable Electrical Idle on the PHY */
1591             phy_data |= GG82563_PMCR_ENABLE_ELECTRICAL_IDLE;
1592             ret_val = e1000_write_phy_reg(hw, GG82563_PHY_PWR_MGMT_CTRL,
1593                                           phy_data);
1594             if (ret_val)
1595                 return ret_val;
1596
1597             ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
1598                                          &phy_data);
1599             if (ret_val)
1600                 return ret_val;
1601
1602             phy_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
1603
1604             ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL,
1605                                           phy_data);
1606             if (ret_val)
1607                 return ret_val;
1608         }
1609
1610         /* Workaround: Disable padding in Kumeran interface in the MAC
1611          * and in the PHY to avoid CRC errors.
1612          */
1613         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_INBAND_CTRL,
1614                                      &phy_data);
1615         if (ret_val)
1616             return ret_val;
1617         phy_data |= GG82563_ICR_DIS_PADDING;
1618         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_INBAND_CTRL,
1619                                       phy_data);
1620         if (ret_val)
1621             return ret_val;
1622     }
1623
1624     return E1000_SUCCESS;
1625 }
1626
1627 /********************************************************************
1628 * Copper link setup for e1000_phy_m88 series.
1629 *
1630 * hw - Struct containing variables accessed by shared code
1631 *********************************************************************/
1632 static int32_t
1633 e1000_copper_link_mgp_setup(struct e1000_hw *hw)
1634 {
1635     int32_t ret_val;
1636     uint16_t phy_data;
1637
1638     DEBUGFUNC("e1000_copper_link_mgp_setup");
1639
1640     if(hw->phy_reset_disable)
1641         return E1000_SUCCESS;
1642
1643     /* Enable CRS on TX. This must be set for half-duplex operation. */
1644     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1645     if(ret_val)
1646         return ret_val;
1647
1648     phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1649
1650     /* Options:
1651      *   MDI/MDI-X = 0 (default)
1652      *   0 - Auto for all speeds
1653      *   1 - MDI mode
1654      *   2 - MDI-X mode
1655      *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1656      */
1657     phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1658
1659     switch (hw->mdix) {
1660     case 1:
1661         phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1662         break;
1663     case 2:
1664         phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1665         break;
1666     case 3:
1667         phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1668         break;
1669     case 0:
1670     default:
1671         phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1672         break;
1673     }
1674
1675     /* Options:
1676      *   disable_polarity_correction = 0 (default)
1677      *       Automatic Correction for Reversed Cable Polarity
1678      *   0 - Disabled
1679      *   1 - Enabled
1680      */
1681     phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1682     if(hw->disable_polarity_correction == 1)
1683         phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1684     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1685     if (ret_val)
1686         return ret_val;
1687
1688     if (hw->phy_revision < M88E1011_I_REV_4) {
1689         /* Force TX_CLK in the Extended PHY Specific Control Register
1690          * to 25MHz clock.
1691          */
1692         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1693         if (ret_val)
1694             return ret_val;
1695
1696         phy_data |= M88E1000_EPSCR_TX_CLK_25;
1697
1698         if ((hw->phy_revision == E1000_REVISION_2) &&
1699             (hw->phy_id == M88E1111_I_PHY_ID)) {
1700             /* Vidalia Phy, set the downshift counter to 5x */
1701             phy_data &= ~(M88EC018_EPSCR_DOWNSHIFT_COUNTER_MASK);
1702             phy_data |= M88EC018_EPSCR_DOWNSHIFT_COUNTER_5X;
1703             ret_val = e1000_write_phy_reg(hw,
1704                                         M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1705             if (ret_val)
1706                 return ret_val;
1707         } else {
1708             /* Configure Master and Slave downshift values */
1709             phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
1710                               M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
1711             phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
1712                              M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
1713             ret_val = e1000_write_phy_reg(hw,
1714                                         M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1715             if (ret_val)
1716                return ret_val;
1717         }
1718     }
1719
1720     /* SW Reset the PHY so all changes take effect */
1721     ret_val = e1000_phy_reset(hw);
1722     if(ret_val) {
1723         DEBUGOUT("Error Resetting the PHY\n");
1724         return ret_val;
1725     }
1726
1727    return E1000_SUCCESS;
1728 }
1729
1730 /********************************************************************
1731 * Setup auto-negotiation and flow control advertisements,
1732 * and then perform auto-negotiation.
1733 *
1734 * hw - Struct containing variables accessed by shared code
1735 *********************************************************************/
1736 static int32_t
1737 e1000_copper_link_autoneg(struct e1000_hw *hw)
1738 {
1739     int32_t ret_val;
1740     uint16_t phy_data;
1741
1742     DEBUGFUNC("e1000_copper_link_autoneg");
1743
1744     /* Perform some bounds checking on the hw->autoneg_advertised
1745      * parameter.  If this variable is zero, then set it to the default.
1746      */
1747     hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
1748
1749     /* If autoneg_advertised is zero, we assume it was not defaulted
1750      * by the calling code so we set to advertise full capability.
1751      */
1752     if(hw->autoneg_advertised == 0)
1753         hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
1754
1755     /* IFE phy only supports 10/100 */
1756     if (hw->phy_type == e1000_phy_ife)
1757         hw->autoneg_advertised &= AUTONEG_ADVERTISE_10_100_ALL;
1758
1759     DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
1760     ret_val = e1000_phy_setup_autoneg(hw);
1761     if(ret_val) {
1762         DEBUGOUT("Error Setting up Auto-Negotiation\n");
1763         return ret_val;
1764     }
1765     DEBUGOUT("Restarting Auto-Neg\n");
1766
1767     /* Restart auto-negotiation by setting the Auto Neg Enable bit and
1768      * the Auto Neg Restart bit in the PHY control register.
1769      */
1770     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
1771     if(ret_val)
1772         return ret_val;
1773
1774     phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
1775     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
1776     if(ret_val)
1777         return ret_val;
1778
1779     /* Does the user want to wait for Auto-Neg to complete here, or
1780      * check at a later time (for example, callback routine).
1781      */
1782     if(hw->wait_autoneg_complete) {
1783         ret_val = e1000_wait_autoneg(hw);
1784         if(ret_val) {
1785             DEBUGOUT("Error while waiting for autoneg to complete\n");
1786             return ret_val;
1787         }
1788     }
1789
1790     hw->get_link_status = TRUE;
1791
1792     return E1000_SUCCESS;
1793 }
1794
1795
1796 /******************************************************************************
1797 * Config the MAC and the PHY after link is up.
1798 *   1) Set up the MAC to the current PHY speed/duplex
1799 *      if we are on 82543.  If we
1800 *      are on newer silicon, we only need to configure
1801 *      collision distance in the Transmit Control Register.
1802 *   2) Set up flow control on the MAC to that established with
1803 *      the link partner.
1804 *   3) Config DSP to improve Gigabit link quality for some PHY revisions.
1805 *
1806 * hw - Struct containing variables accessed by shared code
1807 ******************************************************************************/
1808 static int32_t
1809 e1000_copper_link_postconfig(struct e1000_hw *hw)
1810 {
1811     int32_t ret_val;
1812     DEBUGFUNC("e1000_copper_link_postconfig");
1813
1814     if(hw->mac_type >= e1000_82544) {
1815         e1000_config_collision_dist(hw);
1816     } else {
1817         ret_val = e1000_config_mac_to_phy(hw);
1818         if(ret_val) {
1819             DEBUGOUT("Error configuring MAC to PHY settings\n");
1820             return ret_val;
1821         }
1822     }
1823     ret_val = e1000_config_fc_after_link_up(hw);
1824     if(ret_val) {
1825         DEBUGOUT("Error Configuring Flow Control\n");
1826         return ret_val;
1827     }
1828
1829     /* Config DSP to improve Giga link quality */
1830     if(hw->phy_type == e1000_phy_igp) {
1831         ret_val = e1000_config_dsp_after_link_change(hw, TRUE);
1832         if(ret_val) {
1833             DEBUGOUT("Error Configuring DSP after link up\n");
1834             return ret_val;
1835         }
1836     }
1837
1838     return E1000_SUCCESS;
1839 }
1840
1841 /******************************************************************************
1842 * Detects which PHY is present and setup the speed and duplex
1843 *
1844 * hw - Struct containing variables accessed by shared code
1845 ******************************************************************************/
1846 static int32_t
1847 e1000_setup_copper_link(struct e1000_hw *hw)
1848 {
1849     int32_t ret_val;
1850     uint16_t i;
1851     uint16_t phy_data;
1852     uint16_t reg_data;
1853
1854     DEBUGFUNC("e1000_setup_copper_link");
1855
1856     switch (hw->mac_type) {
1857     case e1000_80003es2lan:
1858     case e1000_ich8lan:
1859         /* Set the mac to wait the maximum time between each
1860          * iteration and increase the max iterations when
1861          * polling the phy; this fixes erroneous timeouts at 10Mbps. */
1862         ret_val = e1000_write_kmrn_reg(hw, GG82563_REG(0x34, 4), 0xFFFF);
1863         if (ret_val)
1864             return ret_val;
1865         ret_val = e1000_read_kmrn_reg(hw, GG82563_REG(0x34, 9), &reg_data);
1866         if (ret_val)
1867             return ret_val;
1868         reg_data |= 0x3F;
1869         ret_val = e1000_write_kmrn_reg(hw, GG82563_REG(0x34, 9), reg_data);
1870         if (ret_val)
1871             return ret_val;
1872     default:
1873         break;
1874     }
1875
1876     /* Check if it is a valid PHY and set PHY mode if necessary. */
1877     ret_val = e1000_copper_link_preconfig(hw);
1878     if(ret_val)
1879         return ret_val;
1880
1881     switch (hw->mac_type) {
1882     case e1000_80003es2lan:
1883         /* Kumeran registers are written-only */
1884         reg_data = E1000_KUMCTRLSTA_INB_CTRL_LINK_STATUS_TX_TIMEOUT_DEFAULT;
1885         reg_data |= E1000_KUMCTRLSTA_INB_CTRL_DIS_PADDING;
1886         ret_val = e1000_write_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_INB_CTRL,
1887                                        reg_data);
1888         if (ret_val)
1889             return ret_val;
1890         break;
1891     default:
1892         break;
1893     }
1894
1895     if (hw->phy_type == e1000_phy_igp ||
1896         hw->phy_type == e1000_phy_igp_3 ||
1897         hw->phy_type == e1000_phy_igp_2) {
1898         ret_val = e1000_copper_link_igp_setup(hw);
1899         if(ret_val)
1900             return ret_val;
1901     } else if (hw->phy_type == e1000_phy_m88) {
1902         ret_val = e1000_copper_link_mgp_setup(hw);
1903         if(ret_val)
1904             return ret_val;
1905     } else if (hw->phy_type == e1000_phy_gg82563) {
1906         ret_val = e1000_copper_link_ggp_setup(hw);
1907         if(ret_val)
1908             return ret_val;
1909     }
1910
1911     if(hw->autoneg) {
1912         /* Setup autoneg and flow control advertisement
1913           * and perform autonegotiation */
1914         ret_val = e1000_copper_link_autoneg(hw);
1915         if(ret_val)
1916             return ret_val;
1917     } else {
1918         /* PHY will be set to 10H, 10F, 100H,or 100F
1919           * depending on value from forced_speed_duplex. */
1920         DEBUGOUT("Forcing speed and duplex\n");
1921         ret_val = e1000_phy_force_speed_duplex(hw);
1922         if(ret_val) {
1923             DEBUGOUT("Error Forcing Speed and Duplex\n");
1924             return ret_val;
1925         }
1926     }
1927
1928     /* Check link status. Wait up to 100 microseconds for link to become
1929      * valid.
1930      */
1931     for(i = 0; i < 10; i++) {
1932         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1933         if(ret_val)
1934             return ret_val;
1935         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1936         if(ret_val)
1937             return ret_val;
1938
1939         if(phy_data & MII_SR_LINK_STATUS) {
1940             /* Config the MAC and PHY after link is up */
1941             ret_val = e1000_copper_link_postconfig(hw);
1942             if(ret_val)
1943                 return ret_val;
1944
1945             DEBUGOUT("Valid link established!!!\n");
1946             return E1000_SUCCESS;
1947         }
1948         udelay(10);
1949     }
1950
1951     DEBUGOUT("Unable to establish link!!!\n");
1952     return E1000_SUCCESS;
1953 }
1954
1955 /******************************************************************************
1956 * Configure the MAC-to-PHY interface for 10/100Mbps
1957 *
1958 * hw - Struct containing variables accessed by shared code
1959 ******************************************************************************/
1960 static int32_t
1961 e1000_configure_kmrn_for_10_100(struct e1000_hw *hw, uint16_t duplex)
1962 {
1963     int32_t ret_val = E1000_SUCCESS;
1964     uint32_t tipg;
1965     uint16_t reg_data;
1966
1967     DEBUGFUNC("e1000_configure_kmrn_for_10_100");
1968
1969     reg_data = E1000_KUMCTRLSTA_HD_CTRL_10_100_DEFAULT;
1970     ret_val = e1000_write_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_HD_CTRL,
1971                                    reg_data);
1972     if (ret_val)
1973         return ret_val;
1974
1975     /* Configure Transmit Inter-Packet Gap */
1976     tipg = E1000_READ_REG(hw, TIPG);
1977     tipg &= ~E1000_TIPG_IPGT_MASK;
1978     tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_10_100;
1979     E1000_WRITE_REG(hw, TIPG, tipg);
1980
1981     ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
1982
1983     if (ret_val)
1984         return ret_val;
1985
1986     if (duplex == HALF_DUPLEX)
1987         reg_data |= GG82563_KMCR_PASS_FALSE_CARRIER;
1988     else
1989         reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
1990
1991     ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
1992
1993     return ret_val;
1994 }
1995
1996 static int32_t
1997 e1000_configure_kmrn_for_1000(struct e1000_hw *hw)
1998 {
1999     int32_t ret_val = E1000_SUCCESS;
2000     uint16_t reg_data;
2001     uint32_t tipg;
2002
2003     DEBUGFUNC("e1000_configure_kmrn_for_1000");
2004
2005     reg_data = E1000_KUMCTRLSTA_HD_CTRL_1000_DEFAULT;
2006     ret_val = e1000_write_kmrn_reg(hw, E1000_KUMCTRLSTA_OFFSET_HD_CTRL,
2007                                    reg_data);
2008     if (ret_val)
2009         return ret_val;
2010
2011     /* Configure Transmit Inter-Packet Gap */
2012     tipg = E1000_READ_REG(hw, TIPG);
2013     tipg &= ~E1000_TIPG_IPGT_MASK;
2014     tipg |= DEFAULT_80003ES2LAN_TIPG_IPGT_1000;
2015     E1000_WRITE_REG(hw, TIPG, tipg);
2016
2017     ret_val = e1000_read_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, &reg_data);
2018
2019     if (ret_val)
2020         return ret_val;
2021
2022     reg_data &= ~GG82563_KMCR_PASS_FALSE_CARRIER;
2023     ret_val = e1000_write_phy_reg(hw, GG82563_PHY_KMRN_MODE_CTRL, reg_data);
2024
2025     return ret_val;
2026 }
2027
2028 /******************************************************************************
2029 * Configures PHY autoneg and flow control advertisement settings
2030 *
2031 * hw - Struct containing variables accessed by shared code
2032 ******************************************************************************/
2033 int32_t
2034 e1000_phy_setup_autoneg(struct e1000_hw *hw)
2035 {
2036     int32_t ret_val;
2037     uint16_t mii_autoneg_adv_reg;
2038     uint16_t mii_1000t_ctrl_reg;
2039
2040     DEBUGFUNC("e1000_phy_setup_autoneg");
2041
2042     /* Read the MII Auto-Neg Advertisement Register (Address 4). */
2043     ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
2044     if(ret_val)
2045         return ret_val;
2046
2047     if (hw->phy_type != e1000_phy_ife) {
2048         /* Read the MII 1000Base-T Control Register (Address 9). */
2049         ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg);
2050         if (ret_val)
2051             return ret_val;
2052     } else
2053         mii_1000t_ctrl_reg=0;
2054
2055     /* Need to parse both autoneg_advertised and fc and set up
2056      * the appropriate PHY registers.  First we will parse for
2057      * autoneg_advertised software override.  Since we can advertise
2058      * a plethora of combinations, we need to check each bit
2059      * individually.
2060      */
2061
2062     /* First we clear all the 10/100 mb speed bits in the Auto-Neg
2063      * Advertisement Register (Address 4) and the 1000 mb speed bits in
2064      * the  1000Base-T Control Register (Address 9).
2065      */
2066     mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
2067     mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
2068
2069     DEBUGOUT1("autoneg_advertised %x\n", hw->autoneg_advertised);
2070
2071     /* Do we want to advertise 10 Mb Half Duplex? */
2072     if(hw->autoneg_advertised & ADVERTISE_10_HALF) {
2073         DEBUGOUT("Advertise 10mb Half duplex\n");
2074         mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
2075     }
2076
2077     /* Do we want to advertise 10 Mb Full Duplex? */
2078     if(hw->autoneg_advertised & ADVERTISE_10_FULL) {
2079         DEBUGOUT("Advertise 10mb Full duplex\n");
2080         mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
2081     }
2082
2083     /* Do we want to advertise 100 Mb Half Duplex? */
2084     if(hw->autoneg_advertised & ADVERTISE_100_HALF) {
2085         DEBUGOUT("Advertise 100mb Half duplex\n");
2086         mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
2087     }
2088
2089     /* Do we want to advertise 100 Mb Full Duplex? */
2090     if(hw->autoneg_advertised & ADVERTISE_100_FULL) {
2091         DEBUGOUT("Advertise 100mb Full duplex\n");
2092         mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
2093     }
2094
2095     /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
2096     if(hw->autoneg_advertised & ADVERTISE_1000_HALF) {
2097         DEBUGOUT("Advertise 1000mb Half duplex requested, request denied!\n");
2098     }
2099
2100     /* Do we want to advertise 1000 Mb Full Duplex? */
2101     if(hw->autoneg_advertised & ADVERTISE_1000_FULL) {
2102         DEBUGOUT("Advertise 1000mb Full duplex\n");
2103         mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
2104         if (hw->phy_type == e1000_phy_ife) {
2105             DEBUGOUT("e1000_phy_ife is a 10/100 PHY. Gigabit speed is not supported.\n");
2106         }
2107     }
2108
2109     /* Check for a software override of the flow control settings, and
2110      * setup the PHY advertisement registers accordingly.  If
2111      * auto-negotiation is enabled, then software will have to set the
2112      * "PAUSE" bits to the correct value in the Auto-Negotiation
2113      * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
2114      *
2115      * The possible values of the "fc" parameter are:
2116      *      0:  Flow control is completely disabled
2117      *      1:  Rx flow control is enabled (we can receive pause frames
2118      *          but not send pause frames).
2119      *      2:  Tx flow control is enabled (we can send pause frames
2120      *          but we do not support receiving pause frames).
2121      *      3:  Both Rx and TX flow control (symmetric) are enabled.
2122      *  other:  No software override.  The flow control configuration
2123      *          in the EEPROM is used.
2124      */
2125     switch (hw->fc) {
2126     case e1000_fc_none: /* 0 */
2127         /* Flow control (RX & TX) is completely disabled by a
2128          * software over-ride.
2129          */
2130         mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
2131         break;
2132     case e1000_fc_rx_pause: /* 1 */
2133         /* RX Flow control is enabled, and TX Flow control is
2134          * disabled, by a software over-ride.
2135          */
2136         /* Since there really isn't a way to advertise that we are
2137          * capable of RX Pause ONLY, we will advertise that we
2138          * support both symmetric and asymmetric RX PAUSE.  Later
2139          * (in e1000_config_fc_after_link_up) we will disable the
2140          *hw's ability to send PAUSE frames.
2141          */
2142         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
2143         break;
2144     case e1000_fc_tx_pause: /* 2 */
2145         /* TX Flow control is enabled, and RX Flow control is
2146          * disabled, by a software over-ride.
2147          */
2148         mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
2149         mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
2150         break;
2151     case e1000_fc_full: /* 3 */
2152         /* Flow control (both RX and TX) is enabled by a software
2153          * over-ride.
2154          */
2155         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
2156         break;
2157     default:
2158         DEBUGOUT("Flow control param set incorrectly\n");
2159         return -E1000_ERR_CONFIG;
2160     }
2161
2162     ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
2163     if(ret_val)
2164         return ret_val;
2165
2166     DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
2167
2168     if (hw->phy_type != e1000_phy_ife) {
2169         ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg);
2170         if (ret_val)
2171             return ret_val;
2172     }
2173
2174     return E1000_SUCCESS;
2175 }
2176
2177 /******************************************************************************
2178 * Force PHY speed and duplex settings to hw->forced_speed_duplex
2179 *
2180 * hw - Struct containing variables accessed by shared code
2181 ******************************************************************************/
2182 static int32_t
2183 e1000_phy_force_speed_duplex(struct e1000_hw *hw)
2184 {
2185     uint32_t ctrl;
2186     int32_t ret_val;
2187     uint16_t mii_ctrl_reg;
2188     uint16_t mii_status_reg;
2189     uint16_t phy_data;
2190     uint16_t i;
2191
2192     DEBUGFUNC("e1000_phy_force_speed_duplex");
2193
2194     /* Turn off Flow control if we are forcing speed and duplex. */
2195     hw->fc = e1000_fc_none;
2196
2197     DEBUGOUT1("hw->fc = %d\n", hw->fc);
2198
2199     /* Read the Device Control Register. */
2200     ctrl = E1000_READ_REG(hw, CTRL);
2201
2202     /* Set the bits to Force Speed and Duplex in the Device Ctrl Reg. */
2203     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2204     ctrl &= ~(DEVICE_SPEED_MASK);
2205
2206     /* Clear the Auto Speed Detect Enable bit. */
2207     ctrl &= ~E1000_CTRL_ASDE;
2208
2209     /* Read the MII Control Register. */
2210     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &mii_ctrl_reg);
2211     if(ret_val)
2212         return ret_val;
2213
2214     /* We need to disable autoneg in order to force link and duplex. */
2215
2216     mii_ctrl_reg &= ~MII_CR_AUTO_NEG_EN;
2217
2218     /* Are we forcing Full or Half Duplex? */
2219     if(hw->forced_speed_duplex == e1000_100_full ||
2220        hw->forced_speed_duplex == e1000_10_full) {
2221         /* We want to force full duplex so we SET the full duplex bits in the
2222          * Device and MII Control Registers.
2223          */
2224         ctrl |= E1000_CTRL_FD;
2225         mii_ctrl_reg |= MII_CR_FULL_DUPLEX;
2226         DEBUGOUT("Full Duplex\n");
2227     } else {
2228         /* We want to force half duplex so we CLEAR the full duplex bits in
2229          * the Device and MII Control Registers.
2230          */
2231         ctrl &= ~E1000_CTRL_FD;
2232         mii_ctrl_reg &= ~MII_CR_FULL_DUPLEX;
2233         DEBUGOUT("Half Duplex\n");
2234     }
2235
2236     /* Are we forcing 100Mbps??? */
2237     if(hw->forced_speed_duplex == e1000_100_full ||
2238        hw->forced_speed_duplex == e1000_100_half) {
2239         /* Set the 100Mb bit and turn off the 1000Mb and 10Mb bits. */
2240         ctrl |= E1000_CTRL_SPD_100;
2241         mii_ctrl_reg |= MII_CR_SPEED_100;
2242         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
2243         DEBUGOUT("Forcing 100mb ");
2244     } else {
2245         /* Set the 10Mb bit and turn off the 1000Mb and 100Mb bits. */
2246         ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
2247         mii_ctrl_reg |= MII_CR_SPEED_10;
2248         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
2249         DEBUGOUT("Forcing 10mb ");
2250     }
2251
2252     e1000_config_collision_dist(hw);
2253
2254     /* Write the configured values back to the Device Control Reg. */
2255     E1000_WRITE_REG(hw, CTRL, ctrl);
2256
2257     if ((hw->phy_type == e1000_phy_m88) ||
2258         (hw->phy_type == e1000_phy_gg82563)) {
2259         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
2260         if(ret_val)
2261             return ret_val;
2262
2263         /* Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
2264          * forced whenever speed are duplex are forced.
2265          */
2266         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
2267         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
2268         if(ret_val)
2269             return ret_val;
2270
2271         DEBUGOUT1("M88E1000 PSCR: %x \n", phy_data);
2272
2273         /* Need to reset the PHY or these changes will be ignored */
2274         mii_ctrl_reg |= MII_CR_RESET;
2275     /* Disable MDI-X support for 10/100 */
2276     } else if (hw->phy_type == e1000_phy_ife) {
2277         ret_val = e1000_read_phy_reg(hw, IFE_PHY_MDIX_CONTROL, &phy_data);
2278         if (ret_val)
2279             return ret_val;
2280
2281         phy_data &= ~IFE_PMC_AUTO_MDIX;
2282         phy_data &= ~IFE_PMC_FORCE_MDIX;
2283
2284         ret_val = e1000_write_phy_reg(hw, IFE_PHY_MDIX_CONTROL, phy_data);
2285         if (ret_val)
2286             return ret_val;
2287     } else {
2288         /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
2289          * forced whenever speed or duplex are forced.
2290          */
2291         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
2292         if(ret_val)
2293             return ret_val;
2294
2295         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
2296         phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
2297
2298         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
2299         if(ret_val)
2300             return ret_val;
2301     }
2302
2303     /* Write back the modified PHY MII control register. */
2304     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, mii_ctrl_reg);
2305     if(ret_val)
2306         return ret_val;
2307
2308     udelay(1);
2309
2310     /* The wait_autoneg_complete flag may be a little misleading here.
2311      * Since we are forcing speed and duplex, Auto-Neg is not enabled.
2312      * But we do want to delay for a period while forcing only so we
2313      * don't generate false No Link messages.  So we will wait here
2314      * only if the user has set wait_autoneg_complete to 1, which is
2315      * the default.
2316      */
2317     if(hw->wait_autoneg_complete) {
2318         /* We will wait for autoneg to complete. */
2319         DEBUGOUT("Waiting for forced speed/duplex link.\n");
2320         mii_status_reg = 0;
2321
2322         /* We will wait for autoneg to complete or 4.5 seconds to expire. */
2323         for(i = PHY_FORCE_TIME; i > 0; i--) {
2324             /* Read the MII Status Register and wait for Auto-Neg Complete bit
2325              * to be set.
2326              */
2327             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2328             if(ret_val)
2329                 return ret_val;
2330
2331             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2332             if(ret_val)
2333                 return ret_val;
2334
2335             if(mii_status_reg & MII_SR_LINK_STATUS) break;
2336             msec_delay(100);
2337         }
2338         if((i == 0) &&
2339            ((hw->phy_type == e1000_phy_m88) ||
2340             (hw->phy_type == e1000_phy_gg82563))) {
2341             /* We didn't get link.  Reset the DSP and wait again for link. */
2342             ret_val = e1000_phy_reset_dsp(hw);
2343             if(ret_val) {
2344                 DEBUGOUT("Error Resetting PHY DSP\n");
2345                 return ret_val;
2346             }
2347         }
2348         /* This loop will early-out if the link condition has been met.  */
2349         for(i = PHY_FORCE_TIME; i > 0; i--) {
2350             if(mii_status_reg & MII_SR_LINK_STATUS) break;
2351             msec_delay(100);
2352             /* Read the MII Status Register and wait for Auto-Neg Complete bit
2353              * to be set.
2354              */
2355             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2356             if(ret_val)
2357                 return ret_val;
2358
2359             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2360             if(ret_val)
2361                 return ret_val;
2362         }
2363     }
2364
2365     if (hw->phy_type == e1000_phy_m88) {
2366         /* Because we reset the PHY above, we need to re-force TX_CLK in the
2367          * Extended PHY Specific Control Register to 25MHz clock.  This value
2368          * defaults back to a 2.5MHz clock when the PHY is reset.
2369          */
2370         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
2371         if(ret_val)
2372             return ret_val;
2373
2374         phy_data |= M88E1000_EPSCR_TX_CLK_25;
2375         ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
2376         if(ret_val)
2377             return ret_val;
2378
2379         /* In addition, because of the s/w reset above, we need to enable CRS on
2380          * TX.  This must be set for both full and half duplex operation.
2381          */
2382         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
2383         if(ret_val)
2384             return ret_val;
2385
2386         phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
2387         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
2388         if(ret_val)
2389             return ret_val;
2390
2391         if((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
2392            (!hw->autoneg) &&
2393            (hw->forced_speed_duplex == e1000_10_full ||
2394             hw->forced_speed_duplex == e1000_10_half)) {
2395             ret_val = e1000_polarity_reversal_workaround(hw);
2396             if(ret_val)
2397                 return ret_val;
2398         }
2399     } else if (hw->phy_type == e1000_phy_gg82563) {
2400         /* The TX_CLK of the Extended PHY Specific Control Register defaults
2401          * to 2.5MHz on a reset.  We need to re-force it back to 25MHz, if
2402          * we're not in a forced 10/duplex configuration. */
2403         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL, &phy_data);
2404         if (ret_val)
2405             return ret_val;
2406
2407         phy_data &= ~GG82563_MSCR_TX_CLK_MASK;
2408         if ((hw->forced_speed_duplex == e1000_10_full) ||
2409             (hw->forced_speed_duplex == e1000_10_half))
2410             phy_data |= GG82563_MSCR_TX_CLK_10MBPS_2_5MHZ;
2411         else
2412             phy_data |= GG82563_MSCR_TX_CLK_100MBPS_25MHZ;
2413
2414         /* Also due to the reset, we need to enable CRS on Tx. */
2415         phy_data |= GG82563_MSCR_ASSERT_CRS_ON_TX;
2416
2417         ret_val = e1000_write_phy_reg(hw, GG82563_PHY_MAC_SPEC_CTRL, phy_data);
2418         if (ret_val)
2419             return ret_val;
2420     }
2421     return E1000_SUCCESS;
2422 }
2423
2424 /******************************************************************************
2425 * Sets the collision distance in the Transmit Control register
2426 *
2427 * hw - Struct containing variables accessed by shared code
2428 *
2429 * Link should have been established previously. Reads the speed and duplex
2430 * information from the Device Status register.
2431 ******************************************************************************/
2432 void
2433 e1000_config_collision_dist(struct e1000_hw *hw)
2434 {
2435     uint32_t tctl, coll_dist;
2436
2437     DEBUGFUNC("e1000_config_collision_dist");
2438
2439     if (hw->mac_type < e1000_82543)
2440         coll_dist = E1000_COLLISION_DISTANCE_82542;
2441     else
2442         coll_dist = E1000_COLLISION_DISTANCE;
2443
2444     tctl = E1000_READ_REG(hw, TCTL);
2445
2446     tctl &= ~E1000_TCTL_COLD;
2447     tctl |= coll_dist << E1000_COLD_SHIFT;
2448
2449     E1000_WRITE_REG(hw, TCTL, tctl);
2450     E1000_WRITE_FLUSH(hw);
2451 }
2452
2453 /******************************************************************************
2454 * Sets MAC speed and duplex settings to reflect the those in the PHY
2455 *
2456 * hw - Struct containing variables accessed by shared code
2457 * mii_reg - data to write to the MII control register
2458 *
2459 * The contents of the PHY register containing the needed information need to
2460 * be passed in.
2461 ******************************************************************************/
2462 static int32_t
2463 e1000_config_mac_to_phy(struct e1000_hw *hw)
2464 {
2465     uint32_t ctrl;
2466     int32_t ret_val;
2467     uint16_t phy_data;
2468
2469     DEBUGFUNC("e1000_config_mac_to_phy");
2470
2471     /* 82544 or newer MAC, Auto Speed Detection takes care of
2472     * MAC speed/duplex configuration.*/
2473     if (hw->mac_type >= e1000_82544)
2474         return E1000_SUCCESS;
2475
2476     /* Read the Device Control Register and set the bits to Force Speed
2477      * and Duplex.
2478      */
2479     ctrl = E1000_READ_REG(hw, CTRL);
2480     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2481     ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
2482
2483     /* Set up duplex in the Device Control and Transmit Control
2484      * registers depending on negotiated values.
2485      */
2486     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
2487     if(ret_val)
2488         return ret_val;
2489
2490     if(phy_data & M88E1000_PSSR_DPLX)
2491         ctrl |= E1000_CTRL_FD;
2492     else
2493         ctrl &= ~E1000_CTRL_FD;
2494
2495     e1000_config_collision_dist(hw);
2496
2497     /* Set up speed in the Device Control register depending on
2498      * negotiated values.
2499      */
2500     if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
2501         ctrl |= E1000_CTRL_SPD_1000;
2502     else if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
2503         ctrl |= E1000_CTRL_SPD_100;
2504
2505     /* Write the configured values back to the Device Control Reg. */
2506     E1000_WRITE_REG(hw, CTRL, ctrl);
2507     return E1000_SUCCESS;
2508 }
2509
2510 /******************************************************************************
2511  * Forces the MAC's flow control settings.
2512  *
2513  * hw - Struct containing variables accessed by shared code
2514  *
2515  * Sets the TFCE and RFCE bits in the device control register to reflect
2516  * the adapter settings. TFCE and RFCE need to be explicitly set by
2517  * software when a Copper PHY is used because autonegotiation is managed
2518  * by the PHY rather than the MAC. Software must also configure these
2519  * bits when link is forced on a fiber connection.
2520  *****************************************************************************/
2521 int32_t
2522 e1000_force_mac_fc(struct e1000_hw *hw)
2523 {
2524     uint32_t ctrl;
2525
2526     DEBUGFUNC("e1000_force_mac_fc");
2527
2528     /* Get the current configuration of the Device Control Register */
2529     ctrl = E1000_READ_REG(hw, CTRL);
2530
2531     /* Because we didn't get link via the internal auto-negotiation
2532      * mechanism (we either forced link or we got link via PHY
2533      * auto-neg), we have to manually enable/disable transmit an
2534      * receive flow control.
2535      *
2536      * The "Case" statement below enables/disable flow control
2537      * according to the "hw->fc" parameter.
2538      *
2539      * The possible values of the "fc" parameter are:
2540      *      0:  Flow control is completely disabled
2541      *      1:  Rx flow control is enabled (we can receive pause
2542      *          frames but not send pause frames).
2543      *      2:  Tx flow control is enabled (we can send pause frames
2544      *          frames but we do not receive pause frames).
2545      *      3:  Both Rx and TX flow control (symmetric) is enabled.
2546      *  other:  No other values should be possible at this point.
2547      */
2548
2549     switch (hw->fc) {
2550     case e1000_fc_none:
2551         ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
2552         break;
2553     case e1000_fc_rx_pause:
2554         ctrl &= (~E1000_CTRL_TFCE);
2555         ctrl |= E1000_CTRL_RFCE;
2556         break;
2557     case e1000_fc_tx_pause:
2558         ctrl &= (~E1000_CTRL_RFCE);
2559         ctrl |= E1000_CTRL_TFCE;
2560         break;
2561     case e1000_fc_full:
2562         ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
2563         break;
2564     default:
2565         DEBUGOUT("Flow control param set incorrectly\n");
2566         return -E1000_ERR_CONFIG;
2567     }
2568
2569     /* Disable TX Flow Control for 82542 (rev 2.0) */
2570     if(hw->mac_type == e1000_82542_rev2_0)
2571         ctrl &= (~E1000_CTRL_TFCE);
2572
2573     E1000_WRITE_REG(hw, CTRL, ctrl);
2574     return E1000_SUCCESS;
2575 }
2576
2577 /******************************************************************************
2578  * Configures flow control settings after link is established
2579  *
2580  * hw - Struct containing variables accessed by shared code
2581  *
2582  * Should be called immediately after a valid link has been established.
2583  * Forces MAC flow control settings if link was forced. When in MII/GMII mode
2584  * and autonegotiation is enabled, the MAC flow control settings will be set
2585  * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
2586  * and RFCE bits will be automaticaly set to the negotiated flow control mode.
2587  *****************************************************************************/
2588 static int32_t
2589 e1000_config_fc_after_link_up(struct e1000_hw *hw)
2590 {
2591     int32_t ret_val;
2592     uint16_t mii_status_reg;
2593     uint16_t mii_nway_adv_reg;
2594     uint16_t mii_nway_lp_ability_reg;
2595     uint16_t speed;
2596     uint16_t duplex;
2597
2598     DEBUGFUNC("e1000_config_fc_after_link_up");
2599
2600     /* Check for the case where we have fiber media and auto-neg failed
2601      * so we had to force link.  In this case, we need to force the
2602      * configuration of the MAC to match the "fc" parameter.
2603      */
2604     if(((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed)) ||
2605        ((hw->media_type == e1000_media_type_internal_serdes) && (hw->autoneg_failed)) ||
2606        ((hw->media_type == e1000_media_type_copper) && (!hw->autoneg))) {
2607         ret_val = e1000_force_mac_fc(hw);
2608         if(ret_val) {
2609             DEBUGOUT("Error forcing flow control settings\n");
2610             return ret_val;
2611         }
2612     }
2613
2614     /* Check for the case where we have copper media and auto-neg is
2615      * enabled.  In this case, we need to check and see if Auto-Neg
2616      * has completed, and if so, how the PHY and link partner has
2617      * flow control configured.
2618      */
2619     if((hw->media_type == e1000_media_type_copper) && hw->autoneg) {
2620         /* Read the MII Status Register and check to see if AutoNeg
2621          * has completed.  We read this twice because this reg has
2622          * some "sticky" (latched) bits.
2623          */
2624         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2625         if(ret_val)
2626             return ret_val;
2627         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2628         if(ret_val)
2629             return ret_val;
2630
2631         if(mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
2632             /* The AutoNeg process has completed, so we now need to
2633              * read both the Auto Negotiation Advertisement Register
2634              * (Address 4) and the Auto_Negotiation Base Page Ability
2635              * Register (Address 5) to determine how flow control was
2636              * negotiated.
2637              */
2638             ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
2639                                          &mii_nway_adv_reg);
2640             if(ret_val)
2641                 return ret_val;
2642             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY,
2643                                          &mii_nway_lp_ability_reg);
2644             if(ret_val)
2645                 return ret_val;
2646
2647             /* Two bits in the Auto Negotiation Advertisement Register
2648              * (Address 4) and two bits in the Auto Negotiation Base
2649              * Page Ability Register (Address 5) determine flow control
2650              * for both the PHY and the link partner.  The following
2651              * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
2652              * 1999, describes these PAUSE resolution bits and how flow
2653              * control is determined based upon these settings.
2654              * NOTE:  DC = Don't Care
2655              *
2656              *   LOCAL DEVICE  |   LINK PARTNER
2657              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
2658              *-------|---------|-------|---------|--------------------
2659              *   0   |    0    |  DC   |   DC    | e1000_fc_none
2660              *   0   |    1    |   0   |   DC    | e1000_fc_none
2661              *   0   |    1    |   1   |    0    | e1000_fc_none
2662              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
2663              *   1   |    0    |   0   |   DC    | e1000_fc_none
2664              *   1   |   DC    |   1   |   DC    | e1000_fc_full
2665              *   1   |    1    |   0   |    0    | e1000_fc_none
2666              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
2667              *
2668              */
2669             /* Are both PAUSE bits set to 1?  If so, this implies
2670              * Symmetric Flow Control is enabled at both ends.  The
2671              * ASM_DIR bits are irrelevant per the spec.
2672              *
2673              * For Symmetric Flow Control:
2674              *
2675              *   LOCAL DEVICE  |   LINK PARTNER
2676              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2677              *-------|---------|-------|---------|--------------------
2678              *   1   |   DC    |   1   |   DC    | e1000_fc_full
2679              *
2680              */
2681             if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2682                (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
2683                 /* Now we need to check if the user selected RX ONLY
2684                  * of pause frames.  In this case, we had to advertise
2685                  * FULL flow control because we could not advertise RX
2686                  * ONLY. Hence, we must now check to see if we need to
2687                  * turn OFF  the TRANSMISSION of PAUSE frames.
2688                  */
2689                 if(hw->original_fc == e1000_fc_full) {
2690                     hw->fc = e1000_fc_full;
2691                     DEBUGOUT("Flow Control = FULL.\n");
2692                 } else {
2693                     hw->fc = e1000_fc_rx_pause;
2694                     DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2695                 }
2696             }
2697             /* For receiving PAUSE frames ONLY.
2698              *
2699              *   LOCAL DEVICE  |   LINK PARTNER
2700              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2701              *-------|---------|-------|---------|--------------------
2702              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
2703              *
2704              */
2705             else if(!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2706                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2707                     (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2708                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2709                 hw->fc = e1000_fc_tx_pause;
2710                 DEBUGOUT("Flow Control = TX PAUSE frames only.\n");
2711             }
2712             /* For transmitting PAUSE frames ONLY.
2713              *
2714              *   LOCAL DEVICE  |   LINK PARTNER
2715              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2716              *-------|---------|-------|---------|--------------------
2717              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
2718              *
2719              */
2720             else if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2721                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2722                     !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2723                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2724                 hw->fc = e1000_fc_rx_pause;
2725                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2726             }
2727             /* Per the IEEE spec, at this point flow control should be
2728              * disabled.  However, we want to consider that we could
2729              * be connected to a legacy switch that doesn't advertise
2730              * desired flow control, but can be forced on the link
2731              * partner.  So if we advertised no flow control, that is
2732              * what we will resolve to.  If we advertised some kind of
2733              * receive capability (Rx Pause Only or Full Flow Control)
2734              * and the link partner advertised none, we will configure
2735              * ourselves to enable Rx Flow Control only.  We can do
2736              * this safely for two reasons:  If the link partner really
2737              * didn't want flow control enabled, and we enable Rx, no
2738              * harm done since we won't be receiving any PAUSE frames
2739              * anyway.  If the intent on the link partner was to have
2740              * flow control enabled, then by us enabling RX only, we
2741              * can at least receive pause frames and process them.
2742              * This is a good idea because in most cases, since we are
2743              * predominantly a server NIC, more times than not we will
2744              * be asked to delay transmission of packets than asking
2745              * our link partner to pause transmission of frames.
2746              */
2747             else if((hw->original_fc == e1000_fc_none ||
2748                      hw->original_fc == e1000_fc_tx_pause) ||
2749                     hw->fc_strict_ieee) {
2750                 hw->fc = e1000_fc_none;
2751                 DEBUGOUT("Flow Control = NONE.\n");
2752             } else {
2753                 hw->fc = e1000_fc_rx_pause;
2754                 DEBUGOUT("Flow Control = RX PAUSE frames only.\n");
2755             }
2756
2757             /* Now we need to do one last check...  If we auto-
2758              * negotiated to HALF DUPLEX, flow control should not be
2759              * enabled per IEEE 802.3 spec.
2760              */
2761             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
2762             if(ret_val) {
2763                 DEBUGOUT("Error getting link speed and duplex\n");
2764                 return ret_val;
2765             }
2766
2767             if(duplex == HALF_DUPLEX)
2768                 hw->fc = e1000_fc_none;
2769
2770             /* Now we call a subroutine to actually force the MAC
2771              * controller to use the correct flow control settings.
2772              */
2773             ret_val = e1000_force_mac_fc(hw);
2774             if(ret_val) {
2775                 DEBUGOUT("Error forcing flow control settings\n");
2776                 return ret_val;
2777             }
2778         } else {
2779             DEBUGOUT("Copper PHY and Auto Neg has not completed.\n");
2780         }
2781     }
2782     return E1000_SUCCESS;
2783 }
2784
2785 /******************************************************************************
2786  * Checks to see if the link status of the hardware has changed.
2787  *
2788  * hw - Struct containing variables accessed by shared code
2789  *
2790  * Called by any function that needs to check the link status of the adapter.
2791  *****************************************************************************/
2792 int32_t
2793 e1000_check_for_link(struct e1000_hw *hw)
2794 {
2795     uint32_t rxcw = 0;
2796     uint32_t ctrl;
2797     uint32_t status;
2798     uint32_t rctl;
2799     uint32_t icr;
2800     uint32_t signal = 0;
2801     int32_t ret_val;
2802     uint16_t phy_data;
2803
2804     DEBUGFUNC("e1000_check_for_link");
2805
2806     ctrl = E1000_READ_REG(hw, CTRL);
2807     status = E1000_READ_REG(hw, STATUS);
2808
2809     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
2810      * set when the optics detect a signal. On older adapters, it will be
2811      * cleared when there is a signal.  This applies to fiber media only.
2812      */
2813     if((hw->media_type == e1000_media_type_fiber) ||
2814        (hw->media_type == e1000_media_type_internal_serdes)) {
2815         rxcw = E1000_READ_REG(hw, RXCW);
2816
2817         if(hw->media_type == e1000_media_type_fiber) {
2818             signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
2819             if(status & E1000_STATUS_LU)
2820                 hw->get_link_status = FALSE;
2821         }
2822     }
2823
2824     /* If we have a copper PHY then we only want to go out to the PHY
2825      * registers to see if Auto-Neg has completed and/or if our link
2826      * status has changed.  The get_link_status flag will be set if we
2827      * receive a Link Status Change interrupt or we have Rx Sequence
2828      * Errors.
2829      */
2830     if((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
2831         /* First we want to see if the MII Status Register reports
2832          * link.  If so, then we want to get the current speed/duplex
2833          * of the PHY.
2834          * Read the register twice since the link bit is sticky.
2835          */
2836         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2837         if(ret_val)
2838             return ret_val;
2839         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2840         if(ret_val)
2841             return ret_val;
2842
2843         if(phy_data & MII_SR_LINK_STATUS) {
2844             hw->get_link_status = FALSE;
2845             /* Check if there was DownShift, must be checked immediately after
2846              * link-up */
2847             e1000_check_downshift(hw);
2848
2849             /* If we are on 82544 or 82543 silicon and speed/duplex
2850              * are forced to 10H or 10F, then we will implement the polarity
2851              * reversal workaround.  We disable interrupts first, and upon
2852              * returning, place the devices interrupt state to its previous
2853              * value except for the link status change interrupt which will
2854              * happen due to the execution of this workaround.
2855              */
2856
2857             if((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
2858                (!hw->autoneg) &&
2859                (hw->forced_speed_duplex == e1000_10_full ||
2860                 hw->forced_speed_duplex == e1000_10_half)) {
2861                 E1000_WRITE_REG(hw, IMC, 0xffffffff);
2862                 ret_val = e1000_polarity_reversal_workaround(hw);
2863                 icr = E1000_READ_REG(hw, ICR);
2864                 E1000_WRITE_REG(hw, ICS, (icr & ~E1000_ICS_LSC));
2865                 E1000_WRITE_REG(hw, IMS, IMS_ENABLE_MASK);
2866             }
2867
2868         } else {
2869             /* No link detected */
2870             e1000_config_dsp_after_link_change(hw, FALSE);
2871             return 0;
2872         }
2873
2874         /* If we are forcing speed/duplex, then we simply return since
2875          * we have already determined whether we have link or not.
2876          */
2877         if(!hw->autoneg) return -E1000_ERR_CONFIG;
2878
2879         /* optimize the dsp settings for the igp phy */
2880         e1000_config_dsp_after_link_change(hw, TRUE);
2881
2882         /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
2883          * have Si on board that is 82544 or newer, Auto
2884          * Speed Detection takes care of MAC speed/duplex
2885          * configuration.  So we only need to configure Collision
2886          * Distance in the MAC.  Otherwise, we need to force
2887          * speed/duplex on the MAC to the current PHY speed/duplex
2888          * settings.
2889          */
2890         if(hw->mac_type >= e1000_82544)
2891             e1000_config_collision_dist(hw);
2892         else {
2893             ret_val = e1000_config_mac_to_phy(hw);
2894             if(ret_val) {
2895                 DEBUGOUT("Error configuring MAC to PHY settings\n");
2896                 return ret_val;
2897             }
2898         }
2899
2900         /* Configure Flow Control now that Auto-Neg has completed. First, we
2901          * need to restore the desired flow control settings because we may
2902          * have had to re-autoneg with a different link partner.
2903          */
2904         ret_val = e1000_config_fc_after_link_up(hw);
2905         if(ret_val) {
2906             DEBUGOUT("Error configuring flow control\n");
2907             return ret_val;
2908         }
2909
2910         /* At this point we know that we are on copper and we have
2911          * auto-negotiated link.  These are conditions for checking the link
2912          * partner capability register.  We use the link speed to determine if
2913          * TBI compatibility needs to be turned on or off.  If the link is not
2914          * at gigabit speed, then TBI compatibility is not needed.  If we are
2915          * at gigabit speed, we turn on TBI compatibility.
2916          */
2917         if(hw->tbi_compatibility_en) {
2918             uint16_t speed, duplex;
2919             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
2920             if (ret_val) {
2921                 DEBUGOUT("Error getting link speed and duplex\n");
2922                 return ret_val;
2923             }
2924             if (speed != SPEED_1000) {
2925                 /* If link speed is not set to gigabit speed, we do not need
2926                  * to enable TBI compatibility.
2927                  */
2928                 if(hw->tbi_compatibility_on) {
2929                     /* If we previously were in the mode, turn it off. */
2930                     rctl = E1000_READ_REG(hw, RCTL);
2931                     rctl &= ~E1000_RCTL_SBP;
2932                     E1000_WRITE_REG(hw, RCTL, rctl);
2933                     hw->tbi_compatibility_on = FALSE;
2934                 }
2935             } else {
2936                 /* If TBI compatibility is was previously off, turn it on. For
2937                  * compatibility with a TBI link partner, we will store bad
2938                  * packets. Some frames have an additional byte on the end and
2939                  * will look like CRC errors to to the hardware.
2940                  */
2941                 if(!hw->tbi_compatibility_on) {
2942                     hw->tbi_compatibility_on = TRUE;
2943                     rctl = E1000_READ_REG(hw, RCTL);
2944                     rctl |= E1000_RCTL_SBP;
2945                     E1000_WRITE_REG(hw, RCTL, rctl);
2946                 }
2947             }
2948         }
2949     }
2950     /* If we don't have link (auto-negotiation failed or link partner cannot
2951      * auto-negotiate), the cable is plugged in (we have signal), and our
2952      * link partner is not trying to auto-negotiate with us (we are receiving
2953      * idles or data), we need to force link up. We also need to give
2954      * auto-negotiation time to complete, in case the cable was just plugged
2955      * in. The autoneg_failed flag does this.
2956      */
2957     else if((((hw->media_type == e1000_media_type_fiber) &&
2958               ((ctrl & E1000_CTRL_SWDPIN1) == signal)) ||
2959              (hw->media_type == e1000_media_type_internal_serdes)) &&
2960             (!(status & E1000_STATUS_LU)) &&
2961             (!(rxcw & E1000_RXCW_C))) {
2962         if(hw->autoneg_failed == 0) {
2963             hw->autoneg_failed = 1;
2964             return 0;
2965         }
2966         DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\n");
2967
2968         /* Disable auto-negotiation in the TXCW register */
2969         E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
2970
2971         /* Force link-up and also force full-duplex. */
2972         ctrl = E1000_READ_REG(hw, CTRL);
2973         ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
2974         E1000_WRITE_REG(hw, CTRL, ctrl);
2975
2976         /* Configure Flow Control after forcing link up. */
2977         ret_val = e1000_config_fc_after_link_up(hw);
2978         if(ret_val) {
2979             DEBUGOUT("Error configuring flow control\n");
2980             return ret_val;
2981         }
2982     }
2983     /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
2984      * auto-negotiation in the TXCW register and disable forced link in the
2985      * Device Control register in an attempt to auto-negotiate with our link
2986      * partner.
2987      */
2988     else if(((hw->media_type == e1000_media_type_fiber) ||
2989              (hw->media_type == e1000_media_type_internal_serdes)) &&
2990             (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
2991         DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\n");
2992         E1000_WRITE_REG(hw, TXCW, hw->txcw);
2993         E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
2994
2995         hw->serdes_link_down = FALSE;
2996     }
2997     /* If we force link for non-auto-negotiation switch, check link status
2998      * based on MAC synchronization for internal serdes media type.
2999      */
3000     else if((hw->media_type == e1000_media_type_internal_serdes) &&
3001             !(E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
3002         /* SYNCH bit and IV bit are sticky. */
3003         udelay(10);
3004         if(E1000_RXCW_SYNCH & E1000_READ_REG(hw, RXCW)) {
3005             if(!(rxcw & E1000_RXCW_IV)) {
3006                 hw->serdes_link_down = FALSE;
3007                 DEBUGOUT("SERDES: Link is up.\n");
3008             }
3009         } else {
3010             hw->serdes_link_down = TRUE;
3011             DEBUGOUT("SERDES: Link is down.\n");
3012         }
3013     }
3014     if((hw->media_type == e1000_media_type_internal_serdes) &&
3015        (E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
3016         hw->serdes_link_down = !(E1000_STATUS_LU & E1000_READ_REG(hw, STATUS));
3017     }
3018     return E1000_SUCCESS;
3019 }
3020
3021 /******************************************************************************
3022  * Detects the current speed and duplex settings of the hardware.
3023  *
3024  * hw - Struct containing variables accessed by shared code
3025  * speed - Speed of the connection
3026  * duplex - Duplex setting of the connection
3027  *****************************************************************************/
3028 int32_t
3029 e1000_get_speed_and_duplex(struct e1000_hw *hw,
3030                            uint16_t *speed,
3031                            uint16_t *duplex)
3032 {
3033     uint32_t status;
3034     int32_t ret_val;
3035     uint16_t phy_data;
3036
3037     DEBUGFUNC("e1000_get_speed_and_duplex");
3038
3039     if(hw->mac_type >= e1000_82543) {
3040         status = E1000_READ_REG(hw, STATUS);
3041         if(status & E1000_STATUS_SPEED_1000) {
3042             *speed = SPEED_1000;
3043             DEBUGOUT("1000 Mbs, ");
3044         } else if(status & E1000_STATUS_SPEED_100) {
3045             *speed = SPEED_100;
3046             DEBUGOUT("100 Mbs, ");
3047         } else {
3048             *speed = SPEED_10;
3049             DEBUGOUT("10 Mbs, ");
3050         }
3051
3052         if(status & E1000_STATUS_FD) {
3053             *duplex = FULL_DUPLEX;
3054             DEBUGOUT("Full Duplex\n");
3055         } else {
3056             *duplex = HALF_DUPLEX;
3057             DEBUGOUT(" Half Duplex\n");
3058         }
3059     } else {
3060         DEBUGOUT("1000 Mbs, Full Duplex\n");
3061         *speed = SPEED_1000;
3062         *duplex = FULL_DUPLEX;
3063     }
3064
3065     /* IGP01 PHY may advertise full duplex operation after speed downgrade even
3066      * if it is operating at half duplex.  Here we set the duplex settings to
3067      * match the duplex in the link partner's capabilities.
3068      */
3069     if(hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
3070         ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
3071         if(ret_val)
3072             return ret_val;
3073
3074         if(!(phy_data & NWAY_ER_LP_NWAY_CAPS))
3075             *duplex = HALF_DUPLEX;
3076         else {
3077             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_data);
3078             if(ret_val)
3079                 return ret_val;
3080             if((*speed == SPEED_100 && !(phy_data & NWAY_LPAR_100TX_FD_CAPS)) ||
3081                (*speed == SPEED_10 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
3082                 *duplex = HALF_DUPLEX;
3083         }
3084     }
3085
3086     if ((hw->mac_type == e1000_80003es2lan) &&
3087         (hw->media_type == e1000_media_type_copper)) {
3088         if (*speed == SPEED_1000)
3089             ret_val = e1000_configure_kmrn_for_1000(hw);
3090         else
3091             ret_val = e1000_configure_kmrn_for_10_100(hw, *duplex);
3092         if (ret_val)
3093             return ret_val;
3094     }
3095
3096     if ((hw->phy_type == e1000_phy_igp_3) && (*speed == SPEED_1000)) {
3097         ret_val = e1000_kumeran_lock_loss_workaround(hw);
3098         if (ret_val)
3099             return ret_val;
3100     }
3101
3102     return E1000_SUCCESS;
3103 }
3104
3105 /******************************************************************************
3106 * Blocks until autoneg completes or times out (~4.5 seconds)
3107 *
3108 * hw - Struct containing variables accessed by shared code
3109 ******************************************************************************/
3110 static int32_t
3111 e1000_wait_autoneg(struct e1000_hw *hw)
3112 {
3113     int32_t ret_val;
3114     uint16_t i;
3115     uint16_t phy_data;
3116
3117     DEBUGFUNC("e1000_wait_autoneg");
3118     DEBUGOUT("Waiting for Auto-Neg to complete.\n");
3119
3120     /* We will wait for autoneg to complete or 4.5 seconds to expire. */
3121     for(i = PHY_AUTO_NEG_TIME; i > 0; i--) {
3122         /* Read the MII Status Register and wait for Auto-Neg
3123          * Complete bit to be set.
3124          */
3125         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3126         if(ret_val)
3127             return ret_val;
3128         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3129         if(ret_val)
3130             return ret_val;
3131         if(phy_data & MII_SR_AUTONEG_COMPLETE) {
3132             return E1000_SUCCESS;
3133         }
3134         msec_delay(100);
3135     }
3136     return E1000_SUCCESS;
3137 }
3138
3139 /******************************************************************************
3140 * Raises the Management Data Clock
3141 *
3142 * hw - Struct containing variables accessed by shared code
3143 * ctrl - Device control register's current value
3144 ******************************************************************************/
3145 static void
3146 e1000_raise_mdi_clk(struct e1000_hw *hw,
3147                     uint32_t *ctrl)
3148 {
3149     /* Raise the clock input to the Management Data Clock (by setting the MDC
3150      * bit), and then delay 10 microseconds.
3151      */
3152     E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
3153     E1000_WRITE_FLUSH(hw);
3154     udelay(10);
3155 }
3156
3157 /******************************************************************************
3158 * Lowers the Management Data Clock
3159 *
3160 * hw - Struct containing variables accessed by shared code
3161 * ctrl - Device control register's current value
3162 ******************************************************************************/
3163 static void
3164 e1000_lower_mdi_clk(struct e1000_hw *hw,
3165                     uint32_t *ctrl)
3166 {
3167     /* Lower the clock input to the Management Data Clock (by clearing the MDC
3168      * bit), and then delay 10 microseconds.
3169      */
3170     E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
3171     E1000_WRITE_FLUSH(hw);
3172     udelay(10);
3173 }
3174
3175 /******************************************************************************
3176 * Shifts data bits out to the PHY
3177 *
3178 * hw - Struct containing variables accessed by shared code
3179 * data - Data to send out to the PHY
3180 * count - Number of bits to shift out
3181 *
3182 * Bits are shifted out in MSB to LSB order.
3183 ******************************************************************************/
3184 static void
3185 e1000_shift_out_mdi_bits(struct e1000_hw *hw,
3186                          uint32_t data,
3187                          uint16_t count)
3188 {
3189     uint32_t ctrl;
3190     uint32_t mask;
3191
3192     /* We need to shift "count" number of bits out to the PHY. So, the value
3193      * in the "data" parameter will be shifted out to the PHY one bit at a
3194      * time. In order to do this, "data" must be broken down into bits.
3195      */
3196     mask = 0x01;
3197     mask <<= (count - 1);
3198
3199     ctrl = E1000_READ_REG(hw, CTRL);
3200
3201     /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
3202     ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
3203
3204     while(mask) {
3205         /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
3206          * then raising and lowering the Management Data Clock. A "0" is
3207          * shifted out to the PHY by setting the MDIO bit to "0" and then
3208          * raising and lowering the clock.
3209          */
3210         if(data & mask) ctrl |= E1000_CTRL_MDIO;
3211         else ctrl &= ~E1000_CTRL_MDIO;
3212
3213         E1000_WRITE_REG(hw, CTRL, ctrl);
3214         E1000_WRITE_FLUSH(hw);
3215
3216         udelay(10);
3217
3218         e1000_raise_mdi_clk(hw, &ctrl);
3219         e1000_lower_mdi_clk(hw, &ctrl);
3220
3221         mask = mask >> 1;
3222     }
3223 }
3224
3225 /******************************************************************************
3226 * Shifts data bits in from the PHY
3227 *
3228 * hw - Struct containing variables accessed by shared code
3229 *
3230 * Bits are shifted in in MSB to LSB order.
3231 ******************************************************************************/
3232 static uint16_t
3233 e1000_shift_in_mdi_bits(struct e1000_hw *hw)
3234 {
3235     uint32_t ctrl;
3236     uint16_t data = 0;
3237     uint8_t i;
3238
3239     /* In order to read a register from the PHY, we need to shift in a total
3240      * of 18 bits from the PHY. The first two bit (turnaround) times are used
3241      * to avoid contention on the MDIO pin when a read operation is performed.
3242      * These two bits are ignored by us and thrown away. Bits are "shifted in"
3243      * by raising the input to the Management Data Clock (setting the MDC bit),
3244      * and then reading the value of the MDIO bit.
3245      */
3246     ctrl = E1000_READ_REG(hw, CTRL);
3247
3248     /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
3249     ctrl &= ~E1000_CTRL_MDIO_DIR;
3250     ctrl &= ~E1000_CTRL_MDIO;
3251
3252     E1000_WRITE_REG(hw, CTRL, ctrl);
3253     E1000_WRITE_FLUSH(hw);
3254
3255     /* Raise and Lower the clock before reading in the data. This accounts for
3256      * the turnaround bits. The first clock occurred when we clocked out the
3257      * last bit of the Register Address.
3258      */
3259     e1000_raise_mdi_clk(hw, &ctrl);
3260     e1000_lower_mdi_clk(hw, &ctrl);
3261
3262     for(data = 0, i = 0; i < 16; i++) {
3263         data = data << 1;
3264         e1000_raise_mdi_clk(hw, &ctrl);
3265         ctrl = E1000_READ_REG(hw, CTRL);
3266         /* Check to see if we shifted in a "1". */
3267         if(ctrl & E1000_CTRL_MDIO) data |= 1;
3268         e1000_lower_mdi_clk(hw, &ctrl);
3269     }
3270
3271     e1000_raise_mdi_clk(hw, &ctrl);
3272     e1000_lower_mdi_clk(hw, &ctrl);
3273
3274     return data;
3275 }
3276
3277 static int32_t
3278 e1000_swfw_sync_acquire(struct e1000_hw *hw, uint16_t mask)
3279 {
3280     uint32_t swfw_sync = 0;
3281     uint32_t swmask = mask;
3282     uint32_t fwmask = mask << 16;
3283     int32_t timeout = 200;
3284
3285     DEBUGFUNC("e1000_swfw_sync_acquire");
3286
3287     if (hw->swfwhw_semaphore_present)
3288         return e1000_get_software_flag(hw);
3289
3290     if (!hw->swfw_sync_present)
3291         return e1000_get_hw_eeprom_semaphore(hw);
3292
3293     while(timeout) {
3294             if (e1000_get_hw_eeprom_semaphore(hw))
3295                 return -E1000_ERR_SWFW_SYNC;
3296
3297             swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
3298             if (!(swfw_sync & (fwmask | swmask))) {
3299                 break;
3300             }
3301
3302             /* firmware currently using resource (fwmask) */
3303             /* or other software thread currently using resource (swmask) */
3304             e1000_put_hw_eeprom_semaphore(hw);
3305             msec_delay_irq(5);
3306             timeout--;
3307     }
3308
3309     if (!timeout) {
3310         DEBUGOUT("Driver can't access resource, SW_FW_SYNC timeout.\n");
3311         return -E1000_ERR_SWFW_SYNC;
3312     }
3313
3314     swfw_sync |= swmask;
3315     E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
3316
3317     e1000_put_hw_eeprom_semaphore(hw);
3318     return E1000_SUCCESS;
3319 }
3320
3321 static void
3322 e1000_swfw_sync_release(struct e1000_hw *hw, uint16_t mask)
3323 {
3324     uint32_t swfw_sync;
3325     uint32_t swmask = mask;
3326
3327     DEBUGFUNC("e1000_swfw_sync_release");
3328
3329     if (hw->swfwhw_semaphore_present) {
3330         e1000_release_software_flag(hw);
3331         return;
3332     }
3333
3334     if (!hw->swfw_sync_present) {
3335         e1000_put_hw_eeprom_semaphore(hw);
3336         return;
3337     }
3338
3339     /* if (e1000_get_hw_eeprom_semaphore(hw))
3340      *    return -E1000_ERR_SWFW_SYNC; */
3341     while (e1000_get_hw_eeprom_semaphore(hw) != E1000_SUCCESS);
3342         /* empty */
3343
3344     swfw_sync = E1000_READ_REG(hw, SW_FW_SYNC);
3345     swfw_sync &= ~swmask;
3346     E1000_WRITE_REG(hw, SW_FW_SYNC, swfw_sync);
3347
3348     e1000_put_hw_eeprom_semaphore(hw);
3349 }
3350
3351 /*****************************************************************************
3352 * Reads the value from a PHY register, if the value is on a specific non zero
3353 * page, sets the page first.
3354 * hw - Struct containing variables accessed by shared code
3355 * reg_addr - address of the PHY register to read
3356 ******************************************************************************/
3357 int32_t
3358 e1000_read_phy_reg(struct e1000_hw *hw,
3359                    uint32_t reg_addr,
3360                    uint16_t *phy_data)
3361 {
3362     uint32_t ret_val;
3363     uint16_t swfw;
3364
3365     DEBUGFUNC("e1000_read_phy_reg");
3366
3367     if ((hw->mac_type == e1000_80003es2lan) &&
3368         (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3369         swfw = E1000_SWFW_PHY1_SM;
3370     } else {
3371         swfw = E1000_SWFW_PHY0_SM;
3372     }
3373     if (e1000_swfw_sync_acquire(hw, swfw))
3374         return -E1000_ERR_SWFW_SYNC;
3375
3376     if ((hw->phy_type == e1000_phy_igp ||
3377         hw->phy_type == e1000_phy_igp_3 ||
3378         hw->phy_type == e1000_phy_igp_2) &&
3379        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
3380         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
3381                                          (uint16_t)reg_addr);
3382         if(ret_val) {
3383             e1000_swfw_sync_release(hw, swfw);
3384             return ret_val;
3385         }
3386     } else if (hw->phy_type == e1000_phy_gg82563) {
3387         if (((reg_addr & MAX_PHY_REG_ADDRESS) > MAX_PHY_MULTI_PAGE_REG) ||
3388             (hw->mac_type == e1000_80003es2lan)) {
3389             /* Select Configuration Page */
3390             if ((reg_addr & MAX_PHY_REG_ADDRESS) < GG82563_MIN_ALT_REG) {
3391                 ret_val = e1000_write_phy_reg_ex(hw, GG82563_PHY_PAGE_SELECT,
3392                           (uint16_t)((uint16_t)reg_addr >> GG82563_PAGE_SHIFT));
3393             } else {
3394                 /* Use Alternative Page Select register to access
3395                  * registers 30 and 31
3396                  */
3397                 ret_val = e1000_write_phy_reg_ex(hw,
3398                                                  GG82563_PHY_PAGE_SELECT_ALT,
3399                           (uint16_t)((uint16_t)reg_addr >> GG82563_PAGE_SHIFT));
3400             }
3401
3402             if (ret_val) {
3403                 e1000_swfw_sync_release(hw, swfw);
3404                 return ret_val;
3405             }
3406         }
3407     }
3408
3409     ret_val = e1000_read_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
3410                                     phy_data);
3411
3412     e1000_swfw_sync_release(hw, swfw);
3413     return ret_val;
3414 }
3415
3416 int32_t
3417 e1000_read_phy_reg_ex(struct e1000_hw *hw,
3418                       uint32_t reg_addr,
3419                       uint16_t *phy_data)
3420 {
3421     uint32_t i;
3422     uint32_t mdic = 0;
3423     const uint32_t phy_addr = 1;
3424
3425     DEBUGFUNC("e1000_read_phy_reg_ex");
3426
3427     if(reg_addr > MAX_PHY_REG_ADDRESS) {
3428         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
3429         return -E1000_ERR_PARAM;
3430     }
3431
3432     if(hw->mac_type > e1000_82543) {
3433         /* Set up Op-code, Phy Address, and register address in the MDI
3434          * Control register.  The MAC will take care of interfacing with the
3435          * PHY to retrieve the desired data.
3436          */
3437         mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
3438                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
3439                 (E1000_MDIC_OP_READ));
3440
3441         E1000_WRITE_REG(hw, MDIC, mdic);
3442
3443         /* Poll the ready bit to see if the MDI read completed */
3444         for(i = 0; i < 64; i++) {
3445             udelay(50);
3446             mdic = E1000_READ_REG(hw, MDIC);
3447             if(mdic & E1000_MDIC_READY) break;
3448         }
3449         if(!(mdic & E1000_MDIC_READY)) {
3450             DEBUGOUT("MDI Read did not complete\n");
3451             return -E1000_ERR_PHY;
3452         }
3453         if(mdic & E1000_MDIC_ERROR) {
3454             DEBUGOUT("MDI Error\n");
3455             return -E1000_ERR_PHY;
3456         }
3457         *phy_data = (uint16_t) mdic;
3458     } else {
3459         /* We must first send a preamble through the MDIO pin to signal the
3460          * beginning of an MII instruction.  This is done by sending 32
3461          * consecutive "1" bits.
3462          */
3463         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
3464
3465         /* Now combine the next few fields that are required for a read
3466          * operation.  We use this method instead of calling the
3467          * e1000_shift_out_mdi_bits routine five different times. The format of
3468          * a MII read instruction consists of a shift out of 14 bits and is
3469          * defined as follows:
3470          *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
3471          * followed by a shift in of 18 bits.  This first two bits shifted in
3472          * are TurnAround bits used to avoid contention on the MDIO pin when a
3473          * READ operation is performed.  These two bits are thrown away
3474          * followed by a shift in of 16 bits which contains the desired data.
3475          */
3476         mdic = ((reg_addr) | (phy_addr << 5) |
3477                 (PHY_OP_READ << 10) | (PHY_SOF << 12));
3478
3479         e1000_shift_out_mdi_bits(hw, mdic, 14);
3480
3481         /* Now that we've shifted out the read command to the MII, we need to
3482          * "shift in" the 16-bit value (18 total bits) of the requested PHY
3483          * register address.
3484          */
3485         *phy_data = e1000_shift_in_mdi_bits(hw);
3486     }
3487     return E1000_SUCCESS;
3488 }
3489
3490 /******************************************************************************
3491 * Writes a value to a PHY register
3492 *
3493 * hw - Struct containing variables accessed by shared code
3494 * reg_addr - address of the PHY register to write
3495 * data - data to write to the PHY
3496 ******************************************************************************/
3497 int32_t
3498 e1000_write_phy_reg(struct e1000_hw *hw,
3499                     uint32_t reg_addr,
3500                     uint16_t phy_data)
3501 {
3502     uint32_t ret_val;
3503     uint16_t swfw;
3504
3505     DEBUGFUNC("e1000_write_phy_reg");
3506
3507     if ((hw->mac_type == e1000_80003es2lan) &&
3508         (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3509         swfw = E1000_SWFW_PHY1_SM;
3510     } else {
3511         swfw = E1000_SWFW_PHY0_SM;
3512     }
3513     if (e1000_swfw_sync_acquire(hw, swfw))
3514         return -E1000_ERR_SWFW_SYNC;
3515
3516     if ((hw->phy_type == e1000_phy_igp ||
3517         hw->phy_type == e1000_phy_igp_3 ||
3518         hw->phy_type == e1000_phy_igp_2) &&
3519        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
3520         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
3521                                          (uint16_t)reg_addr);
3522         if(ret_val) {
3523             e1000_swfw_sync_release(hw, swfw);
3524             return ret_val;
3525         }
3526     } else if (hw->phy_type == e1000_phy_gg82563) {
3527         if (((reg_addr & MAX_PHY_REG_ADDRESS) > MAX_PHY_MULTI_PAGE_REG) ||
3528             (hw->mac_type == e1000_80003es2lan)) {
3529             /* Select Configuration Page */
3530             if ((reg_addr & MAX_PHY_REG_ADDRESS) < GG82563_MIN_ALT_REG) {
3531                 ret_val = e1000_write_phy_reg_ex(hw, GG82563_PHY_PAGE_SELECT,
3532                           (uint16_t)((uint16_t)reg_addr >> GG82563_PAGE_SHIFT));
3533             } else {
3534                 /* Use Alternative Page Select register to access
3535                  * registers 30 and 31
3536                  */
3537                 ret_val = e1000_write_phy_reg_ex(hw,
3538                                                  GG82563_PHY_PAGE_SELECT_ALT,
3539                           (uint16_t)((uint16_t)reg_addr >> GG82563_PAGE_SHIFT));
3540             }
3541
3542             if (ret_val) {
3543                 e1000_swfw_sync_release(hw, swfw);
3544                 return ret_val;
3545             }
3546         }
3547     }
3548
3549     ret_val = e1000_write_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
3550                                      phy_data);
3551
3552     e1000_swfw_sync_release(hw, swfw);
3553     return ret_val;
3554 }
3555
3556 int32_t
3557 e1000_write_phy_reg_ex(struct e1000_hw *hw,
3558                     uint32_t reg_addr,
3559                     uint16_t phy_data)
3560 {
3561     uint32_t i;
3562     uint32_t mdic = 0;
3563     const uint32_t phy_addr = 1;
3564
3565     DEBUGFUNC("e1000_write_phy_reg_ex");
3566
3567     if(reg_addr > MAX_PHY_REG_ADDRESS) {
3568         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
3569         return -E1000_ERR_PARAM;
3570     }
3571
3572     if(hw->mac_type > e1000_82543) {
3573         /* Set up Op-code, Phy Address, register address, and data intended
3574          * for the PHY register in the MDI Control register.  The MAC will take
3575          * care of interfacing with the PHY to send the desired data.
3576          */
3577         mdic = (((uint32_t) phy_data) |
3578                 (reg_addr << E1000_MDIC_REG_SHIFT) |
3579                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
3580                 (E1000_MDIC_OP_WRITE));
3581
3582         E1000_WRITE_REG(hw, MDIC, mdic);
3583
3584         /* Poll the ready bit to see if the MDI read completed */
3585         for(i = 0; i < 640; i++) {
3586             udelay(5);
3587             mdic = E1000_READ_REG(hw, MDIC);
3588             if(mdic & E1000_MDIC_READY) break;
3589         }
3590         if(!(mdic & E1000_MDIC_READY)) {
3591             DEBUGOUT("MDI Write did not complete\n");
3592             return -E1000_ERR_PHY;
3593         }
3594     } else {
3595         /* We'll need to use the SW defined pins to shift the write command
3596          * out to the PHY. We first send a preamble to the PHY to signal the
3597          * beginning of the MII instruction.  This is done by sending 32
3598          * consecutive "1" bits.
3599          */
3600         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
3601
3602         /* Now combine the remaining required fields that will indicate a
3603          * write operation. We use this method instead of calling the
3604          * e1000_shift_out_mdi_bits routine for each field in the command. The
3605          * format of a MII write instruction is as follows:
3606          * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
3607          */
3608         mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
3609                 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
3610         mdic <<= 16;
3611         mdic |= (uint32_t) phy_data;
3612
3613         e1000_shift_out_mdi_bits(hw, mdic, 32);
3614     }
3615
3616     return E1000_SUCCESS;
3617 }
3618
3619 static int32_t
3620 e1000_read_kmrn_reg(struct e1000_hw *hw,
3621                     uint32_t reg_addr,
3622                     uint16_t *data)
3623 {
3624     uint32_t reg_val;
3625     uint16_t swfw;
3626     DEBUGFUNC("e1000_read_kmrn_reg");
3627
3628     if ((hw->mac_type == e1000_80003es2lan) &&
3629         (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3630         swfw = E1000_SWFW_PHY1_SM;
3631     } else {
3632         swfw = E1000_SWFW_PHY0_SM;
3633     }
3634     if (e1000_swfw_sync_acquire(hw, swfw))
3635         return -E1000_ERR_SWFW_SYNC;
3636
3637     /* Write register address */
3638     reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
3639               E1000_KUMCTRLSTA_OFFSET) |
3640               E1000_KUMCTRLSTA_REN;
3641     E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
3642     udelay(2);
3643
3644     /* Read the data returned */
3645     reg_val = E1000_READ_REG(hw, KUMCTRLSTA);
3646     *data = (uint16_t)reg_val;
3647
3648     e1000_swfw_sync_release(hw, swfw);
3649     return E1000_SUCCESS;
3650 }
3651
3652 static int32_t
3653 e1000_write_kmrn_reg(struct e1000_hw *hw,
3654                      uint32_t reg_addr,
3655                      uint16_t data)
3656 {
3657     uint32_t reg_val;
3658     uint16_t swfw;
3659     DEBUGFUNC("e1000_write_kmrn_reg");
3660
3661     if ((hw->mac_type == e1000_80003es2lan) &&
3662         (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3663         swfw = E1000_SWFW_PHY1_SM;
3664     } else {
3665         swfw = E1000_SWFW_PHY0_SM;
3666     }
3667     if (e1000_swfw_sync_acquire(hw, swfw))
3668         return -E1000_ERR_SWFW_SYNC;
3669
3670     reg_val = ((reg_addr << E1000_KUMCTRLSTA_OFFSET_SHIFT) &
3671               E1000_KUMCTRLSTA_OFFSET) | data;
3672     E1000_WRITE_REG(hw, KUMCTRLSTA, reg_val);
3673     udelay(2);
3674
3675     e1000_swfw_sync_release(hw, swfw);
3676     return E1000_SUCCESS;
3677 }
3678
3679 /******************************************************************************
3680 * Returns the PHY to the power-on reset state
3681 *
3682 * hw - Struct containing variables accessed by shared code
3683 ******************************************************************************/
3684 int32_t
3685 e1000_phy_hw_reset(struct e1000_hw *hw)
3686 {
3687     uint32_t ctrl, ctrl_ext;
3688     uint32_t led_ctrl;
3689     int32_t ret_val;
3690     uint16_t swfw;
3691
3692     DEBUGFUNC("e1000_phy_hw_reset");
3693
3694     /* In the case of the phy reset being blocked, it's not an error, we
3695      * simply return success without performing the reset. */
3696     ret_val = e1000_check_phy_reset_block(hw);
3697     if (ret_val)
3698         return E1000_SUCCESS;
3699
3700     DEBUGOUT("Resetting Phy...\n");
3701
3702     if(hw->mac_type > e1000_82543) {
3703         if ((hw->mac_type == e1000_80003es2lan) &&
3704             (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)) {
3705             swfw = E1000_SWFW_PHY1_SM;
3706         } else {
3707             swfw = E1000_SWFW_PHY0_SM;
3708         }
3709         if (e1000_swfw_sync_acquire(hw, swfw)) {
3710             e1000_release_software_semaphore(hw);
3711             return -E1000_ERR_SWFW_SYNC;
3712         }
3713         /* Read the device control register and assert the E1000_CTRL_PHY_RST
3714          * bit. Then, take it out of reset.
3715          * For pre-e1000_82571 hardware, we delay for 10ms between the assert
3716          * and deassert.  For e1000_82571 hardware and later, we instead delay
3717          * for 50us between and 10ms after the deassertion.
3718          */
3719         ctrl = E1000_READ_REG(hw, CTRL);
3720         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
3721         E1000_WRITE_FLUSH(hw);
3722
3723         if (hw->mac_type < e1000_82571)
3724             msec_delay(10);
3725         else
3726             udelay(100);
3727
3728         E1000_WRITE_REG(hw, CTRL, ctrl);
3729         E1000_WRITE_FLUSH(hw);
3730
3731         if (hw->mac_type >= e1000_82571)
3732             msec_delay_irq(10);
3733         e1000_swfw_sync_release(hw, swfw);
3734     } else {
3735         /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
3736          * bit to put the PHY into reset. Then, take it out of reset.
3737          */
3738         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
3739         ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
3740         ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
3741         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
3742         E1000_WRITE_FLUSH(hw);
3743         msec_delay(10);
3744         ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
3745         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
3746         E1000_WRITE_FLUSH(hw);
3747     }
3748     udelay(150);
3749
3750     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
3751         /* Configure activity LED after PHY reset */
3752         led_ctrl = E1000_READ_REG(hw, LEDCTL);
3753         led_ctrl &= IGP_ACTIVITY_LED_MASK;
3754         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
3755         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
3756     }
3757
3758     /* Wait for FW to finish PHY configuration. */
3759     ret_val = e1000_get_phy_cfg_done(hw);
3760     e1000_release_software_semaphore(hw);
3761
3762         if ((hw->mac_type == e1000_ich8lan) &&
3763             (hw->phy_type == e1000_phy_igp_3)) {
3764             ret_val = e1000_init_lcd_from_nvm(hw);
3765             if (ret_val)
3766                 return ret_val;
3767         }
3768     return ret_val;
3769 }
3770
3771 /******************************************************************************
3772 * Resets the PHY
3773 *
3774 * hw - Struct containing variables accessed by shared code
3775 *
3776 * Sets bit 15 of the MII Control regiser
3777 ******************************************************************************/
3778 int32_t
3779 e1000_phy_reset(struct e1000_hw *hw)
3780 {
3781     int32_t ret_val;
3782     uint16_t phy_data;
3783
3784     DEBUGFUNC("e1000_phy_reset");
3785
3786     /* In the case of the phy reset being blocked, it's not an error, we
3787      * simply return success without performing the reset. */
3788     ret_val = e1000_check_phy_reset_block(hw);
3789     if (ret_val)
3790         return E1000_SUCCESS;
3791
3792     switch (hw->mac_type) {
3793     case e1000_82541_rev_2:
3794     case e1000_82571:
3795     case e1000_82572:
3796     case e1000_ich8lan:
3797         ret_val = e1000_phy_hw_reset(hw);
3798         if(ret_val)
3799             return ret_val;
3800
3801         break;
3802     default:
3803         ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3804         if(ret_val)
3805             return ret_val;
3806
3807         phy_data |= MII_CR_RESET;
3808         ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3809         if(ret_val)
3810             return ret_val;
3811
3812         udelay(1);
3813         break;
3814     }
3815
3816     if(hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
3817         e1000_phy_init_script(hw);
3818
3819     return E1000_SUCCESS;
3820 }
3821
3822 /******************************************************************************
3823 * Work-around for 82566 power-down: on D3 entry-
3824 * 1) disable gigabit link
3825 * 2) write VR power-down enable
3826 * 3) read it back
3827 * if successful continue, else issue LCD reset and repeat
3828 *
3829 * hw - struct containing variables accessed by shared code
3830 ******************************************************************************/
3831 void
3832 e1000_phy_powerdown_workaround(struct e1000_hw *hw)
3833 {
3834     int32_t reg;
3835     uint16_t phy_data;
3836     int32_t retry = 0;
3837
3838     DEBUGFUNC("e1000_phy_powerdown_workaround");
3839
3840     if (hw->phy_type != e1000_phy_igp_3)
3841         return;
3842
3843     do {
3844         /* Disable link */
3845         reg = E1000_READ_REG(hw, PHY_CTRL);
3846         E1000_WRITE_REG(hw, PHY_CTRL, reg | E1000_PHY_CTRL_GBE_DISABLE |
3847                         E1000_PHY_CTRL_NOND0A_GBE_DISABLE);
3848
3849         /* Write VR power-down enable */
3850         e1000_read_phy_reg(hw, IGP3_VR_CTRL, &phy_data);
3851         e1000_write_phy_reg(hw, IGP3_VR_CTRL, phy_data |
3852                             IGP3_VR_CTRL_MODE_SHUT);
3853
3854         /* Read it back and test */
3855         e1000_read_phy_reg(hw, IGP3_VR_CTRL, &phy_data);
3856         if ((phy_data & IGP3_VR_CTRL_MODE_SHUT) || retry)
3857             break;
3858
3859         /* Issue PHY reset and repeat at most one more time */
3860         reg = E1000_READ_REG(hw, CTRL);
3861         E1000_WRITE_REG(hw, CTRL, reg | E1000_CTRL_PHY_RST);
3862         retry++;
3863     } while (retry);
3864
3865     return;
3866
3867 }
3868
3869 /******************************************************************************
3870 * Work-around for 82566 Kumeran PCS lock loss:
3871 * On link status change (i.e. PCI reset, speed change) and link is up and
3872 * speed is gigabit-
3873 * 0) if workaround is optionally disabled do nothing
3874 * 1) wait 1ms for Kumeran link to come up
3875 * 2) check Kumeran Diagnostic register PCS lock loss bit
3876 * 3) if not set the link is locked (all is good), otherwise...
3877 * 4) reset the PHY
3878 * 5) repeat up to 10 times
3879 * Note: this is only called for IGP3 copper when speed is 1gb.
3880 *
3881 * hw - struct containing variables accessed by shared code
3882 ******************************************************************************/
3883 static int32_t
3884 e1000_kumeran_lock_loss_workaround(struct e1000_hw *hw)
3885 {
3886     int32_t ret_val;
3887     int32_t reg;
3888     int32_t cnt;
3889     uint16_t phy_data;
3890
3891     if (hw->kmrn_lock_loss_workaround_disabled)
3892         return E1000_SUCCESS;
3893
3894     /* Make sure link is up before proceeding. If not just return.
3895      * Attempting this while link is negotiating fouls up link
3896      * stability */
3897     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3898     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3899
3900     if (phy_data & MII_SR_LINK_STATUS) {
3901         for (cnt = 0; cnt < 10; cnt++) {
3902             /* read once to clear */
3903             ret_val = e1000_read_phy_reg(hw, IGP3_KMRN_DIAG, &phy_data);
3904             if (ret_val)
3905                 return ret_val;
3906             /* and again to get new status */
3907             ret_val = e1000_read_phy_reg(hw, IGP3_KMRN_DIAG, &phy_data);
3908             if (ret_val)
3909                 return ret_val;
3910
3911             /* check for PCS lock */
3912             if (!(phy_data & IGP3_KMRN_DIAG_PCS_LOCK_LOSS))
3913                 return E1000_SUCCESS;
3914
3915             /* Issue PHY reset */
3916             e1000_phy_hw_reset(hw);
3917             msec_delay_irq(5);
3918         }
3919         /* Disable GigE link negotiation */
3920         reg = E1000_READ_REG(hw, PHY_CTRL);
3921         E1000_WRITE_REG(hw, PHY_CTRL, reg | E1000_PHY_CTRL_GBE_DISABLE |
3922                         E1000_PHY_CTRL_NOND0A_GBE_DISABLE);
3923
3924         /* unable to acquire PCS lock */
3925         return E1000_ERR_PHY;
3926     }
3927
3928     return E1000_SUCCESS;
3929 }
3930
3931 /******************************************************************************
3932 * Probes the expected PHY address for known PHY IDs
3933 *
3934 * hw - Struct containing variables accessed by shared code
3935 ******************************************************************************/
3936 int32_t
3937 e1000_detect_gig_phy(struct e1000_hw *hw)
3938 {
3939     int32_t phy_init_status, ret_val;
3940     uint16_t phy_id_high, phy_id_low;
3941     boolean_t match = FALSE;
3942
3943     DEBUGFUNC("e1000_detect_gig_phy");
3944
3945     /* The 82571 firmware may still be configuring the PHY.  In this
3946      * case, we cannot access the PHY until the configuration is done.  So
3947      * we explicitly set the PHY values. */
3948     if (hw->mac_type == e1000_82571 ||
3949         hw->mac_type == e1000_82572) {
3950         hw->phy_id = IGP01E1000_I_PHY_ID;
3951         hw->phy_type = e1000_phy_igp_2;
3952         return E1000_SUCCESS;
3953     }
3954
3955     /* ESB-2 PHY reads require e1000_phy_gg82563 to be set because of a work-
3956      * around that forces PHY page 0 to be set or the reads fail.  The rest of
3957      * the code in this routine uses e1000_read_phy_reg to read the PHY ID.
3958      * So for ESB-2 we need to have this set so our reads won't fail.  If the
3959      * attached PHY is not a e1000_phy_gg82563, the routines below will figure
3960      * this out as well. */
3961     if (hw->mac_type == e1000_80003es2lan)
3962         hw->phy_type = e1000_phy_gg82563;
3963
3964     /* Read the PHY ID Registers to identify which PHY is onboard. */
3965     ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
3966     if (ret_val)
3967         return ret_val;
3968
3969     hw->phy_id = (uint32_t) (phy_id_high << 16);
3970     udelay(20);
3971     ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
3972     if(ret_val)
3973         return ret_val;
3974
3975     hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
3976     hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
3977
3978     switch(hw->mac_type) {
3979     case e1000_82543:
3980         if(hw->phy_id == M88E1000_E_PHY_ID) match = TRUE;
3981         break;
3982     case e1000_82544:
3983         if(hw->phy_id == M88E1000_I_PHY_ID) match = TRUE;
3984         break;
3985     case e1000_82540:
3986     case e1000_82545:
3987     case e1000_82545_rev_3:
3988     case e1000_82546:
3989     case e1000_82546_rev_3:
3990         if(hw->phy_id == M88E1011_I_PHY_ID) match = TRUE;
3991         break;
3992     case e1000_82541:
3993     case e1000_82541_rev_2:
3994     case e1000_82547:
3995     case e1000_82547_rev_2:
3996         if(hw->phy_id == IGP01E1000_I_PHY_ID) match = TRUE;
3997         break;
3998     case e1000_82573:
3999         if(hw->phy_id == M88E1111_I_PHY_ID) match = TRUE;
4000         break;
4001     case e1000_80003es2lan:
4002         if (hw->phy_id == GG82563_E_PHY_ID) match = TRUE;
4003         break;
4004     case e1000_ich8lan:
4005         if (hw->phy_id == IGP03E1000_E_PHY_ID) match = TRUE;
4006         if (hw->phy_id == IFE_E_PHY_ID) match = TRUE;
4007         if (hw->phy_id == IFE_PLUS_E_PHY_ID) match = TRUE;
4008         if (hw->phy_id == IFE_C_E_PHY_ID) match = TRUE;
4009         break;
4010     default:
4011         DEBUGOUT1("Invalid MAC type %d\n", hw->mac_type);
4012         return -E1000_ERR_CONFIG;
4013     }
4014     phy_init_status = e1000_set_phy_type(hw);
4015
4016     if ((match) && (phy_init_status == E1000_SUCCESS)) {
4017         DEBUGOUT1("PHY ID 0x%X detected\n", hw->phy_id);
4018         return E1000_SUCCESS;
4019     }
4020     DEBUGOUT1("Invalid PHY ID 0x%X\n", hw->phy_id);
4021     return -E1000_ERR_PHY;
4022 }
4023
4024 /******************************************************************************
4025 * Resets the PHY's DSP
4026 *
4027 * hw - Struct containing variables accessed by shared code
4028 ******************************************************************************/
4029 static int32_t
4030 e1000_phy_reset_dsp(struct e1000_hw *hw)
4031 {
4032     int32_t ret_val;
4033     DEBUGFUNC("e1000_phy_reset_dsp");
4034
4035     do {
4036         if (hw->phy_type != e1000_phy_gg82563) {
4037             ret_val = e1000_write_phy_reg(hw, 29, 0x001d);
4038             if(ret_val) break;
4039         }
4040         ret_val = e1000_write_phy_reg(hw, 30, 0x00c1);
4041         if(ret_val) break;
4042         ret_val = e1000_write_phy_reg(hw, 30, 0x0000);
4043         if(ret_val) break;
4044         ret_val = E1000_SUCCESS;
4045     } while(0);
4046
4047     return ret_val;
4048 }
4049
4050 /******************************************************************************
4051 * Get PHY information from various PHY registers for igp PHY only.
4052 *
4053 * hw - Struct containing variables accessed by shared code
4054 * phy_info - PHY information structure
4055 ******************************************************************************/
4056 static int32_t
4057 e1000_phy_igp_get_info(struct e1000_hw *hw,
4058                        struct e1000_phy_info *phy_info)
4059 {
4060     int32_t ret_val;
4061     uint16_t phy_data, polarity, min_length, max_length, average;
4062
4063     DEBUGFUNC("e1000_phy_igp_get_info");
4064
4065     /* The downshift status is checked only once, after link is established,
4066      * and it stored in the hw->speed_downgraded parameter. */
4067     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
4068
4069     /* IGP01E1000 does not need to support it. */
4070     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
4071
4072     /* IGP01E1000 always correct polarity reversal */
4073     phy_info->polarity_correction = e1000_polarity_reversal_enabled;
4074
4075     /* Check polarity status */
4076     ret_val = e1000_check_polarity(hw, &polarity);
4077     if(ret_val)
4078         return ret_val;
4079
4080     phy_info->cable_polarity = polarity;
4081
4082     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS, &phy_data);
4083     if(ret_val)
4084         return ret_val;
4085
4086     phy_info->mdix_mode = (phy_data & IGP01E1000_PSSR_MDIX) >>
4087                           IGP01E1000_PSSR_MDIX_SHIFT;
4088
4089     if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
4090        IGP01E1000_PSSR_SPEED_1000MBPS) {
4091         /* Local/Remote Receiver Information are only valid at 1000 Mbps */
4092         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
4093         if(ret_val)
4094             return ret_val;
4095
4096         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
4097                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
4098         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
4099                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
4100
4101         /* Get cable length */
4102         ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
4103         if(ret_val)
4104             return ret_val;
4105
4106         /* Translate to old method */
4107         average = (max_length + min_length) / 2;
4108
4109         if(average <= e1000_igp_cable_length_50)
4110             phy_info->cable_length = e1000_cable_length_50;
4111         else if(average <= e1000_igp_cable_length_80)
4112             phy_info->cable_length = e1000_cable_length_50_80;
4113         else if(average <= e1000_igp_cable_length_110)
4114             phy_info->cable_length = e1000_cable_length_80_110;
4115         else if(average <= e1000_igp_cable_length_140)
4116             phy_info->cable_length = e1000_cable_length_110_140;
4117         else
4118             phy_info->cable_length = e1000_cable_length_140;
4119     }
4120
4121     return E1000_SUCCESS;
4122 }
4123
4124 /******************************************************************************
4125 * Get PHY information from various PHY registers for ife PHY only.
4126 *
4127 * hw - Struct containing variables accessed by shared code
4128 * phy_info - PHY information structure
4129 ******************************************************************************/
4130 static int32_t
4131 e1000_phy_ife_get_info(struct e1000_hw *hw,
4132                        struct e1000_phy_info *phy_info)
4133 {
4134     int32_t ret_val;
4135     uint16_t phy_data, polarity;
4136
4137     DEBUGFUNC("e1000_phy_ife_get_info");
4138
4139     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
4140     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
4141
4142     ret_val = e1000_read_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, &phy_data);
4143     if (ret_val)
4144         return ret_val;
4145     phy_info->polarity_correction =
4146                         (phy_data & IFE_PSC_AUTO_POLARITY_DISABLE) >>
4147                         IFE_PSC_AUTO_POLARITY_DISABLE_SHIFT;
4148
4149     if (phy_info->polarity_correction == e1000_polarity_reversal_enabled) {
4150         ret_val = e1000_check_polarity(hw, &polarity);
4151         if (ret_val)
4152             return ret_val;
4153     } else {
4154         /* Polarity is forced. */
4155         polarity = (phy_data & IFE_PSC_FORCE_POLARITY) >>
4156                        IFE_PSC_FORCE_POLARITY_SHIFT;
4157     }
4158     phy_info->cable_polarity = polarity;
4159
4160     ret_val = e1000_read_phy_reg(hw, IFE_PHY_MDIX_CONTROL, &phy_data);
4161     if (ret_val)
4162         return ret_val;
4163
4164     phy_info->mdix_mode =
4165                      (phy_data & (IFE_PMC_AUTO_MDIX | IFE_PMC_FORCE_MDIX)) >>
4166                      IFE_PMC_MDIX_MODE_SHIFT;
4167
4168     return E1000_SUCCESS;
4169 }
4170
4171 /******************************************************************************
4172 * Get PHY information from various PHY registers fot m88 PHY only.
4173 *
4174 * hw - Struct containing variables accessed by shared code
4175 * phy_info - PHY information structure
4176 ******************************************************************************/
4177 static int32_t
4178 e1000_phy_m88_get_info(struct e1000_hw *hw,
4179                        struct e1000_phy_info *phy_info)
4180 {
4181     int32_t ret_val;
4182     uint16_t phy_data, polarity;
4183
4184     DEBUGFUNC("e1000_phy_m88_get_info");
4185
4186     /* The downshift status is checked only once, after link is established,
4187      * and it stored in the hw->speed_downgraded parameter. */
4188     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
4189
4190     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
4191     if(ret_val)
4192         return ret_val;
4193
4194     phy_info->extended_10bt_distance =
4195         (phy_data & M88E1000_PSCR_10BT_EXT_DIST_ENABLE) >>
4196         M88E1000_PSCR_10BT_EXT_DIST_ENABLE_SHIFT;
4197     phy_info->polarity_correction =
4198         (phy_data & M88E1000_PSCR_POLARITY_REVERSAL) >>
4199         M88E1000_PSCR_POLARITY_REVERSAL_SHIFT;
4200
4201     /* Check polarity status */
4202     ret_val = e1000_check_polarity(hw, &polarity);
4203     if(ret_val)
4204         return ret_val;
4205     phy_info->cable_polarity = polarity;
4206
4207     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
4208     if(ret_val)
4209         return ret_val;
4210
4211     phy_info->mdix_mode = (phy_data & M88E1000_PSSR_MDIX) >>
4212                           M88E1000_PSSR_MDIX_SHIFT;
4213
4214     if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
4215         /* Cable Length Estimation and Local/Remote Receiver Information
4216          * are only valid at 1000 Mbps.
4217          */
4218         if (hw->phy_type != e1000_phy_gg82563) {
4219             phy_info->cable_length = ((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
4220                                       M88E1000_PSSR_CABLE_LENGTH_SHIFT);
4221         } else {
4222             ret_val = e1000_read_phy_reg(hw, GG82563_PHY_DSP_DISTANCE,
4223                                          &phy_data);
4224             if (ret_val)
4225                 return ret_val;
4226
4227             phy_info->cable_length = phy_data & GG82563_DSPD_CABLE_LENGTH;
4228         }
4229
4230         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
4231         if(ret_val)
4232             return ret_val;
4233
4234         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
4235                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
4236
4237         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
4238                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
4239     }
4240
4241     return E1000_SUCCESS;
4242 }
4243
4244 /******************************************************************************
4245 * Get PHY information from various PHY registers
4246 *
4247 * hw - Struct containing variables accessed by shared code
4248 * phy_info - PHY information structure
4249 ******************************************************************************/
4250 int32_t
4251 e1000_phy_get_info(struct e1000_hw *hw,
4252                    struct e1000_phy_info *phy_info)
4253 {
4254     int32_t ret_val;
4255     uint16_t phy_data;
4256
4257     DEBUGFUNC("e1000_phy_get_info");
4258
4259     phy_info->cable_length = e1000_cable_length_undefined;
4260     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_undefined;
4261     phy_info->cable_polarity = e1000_rev_polarity_undefined;
4262     phy_info->downshift = e1000_downshift_undefined;
4263     phy_info->polarity_correction = e1000_polarity_reversal_undefined;
4264     phy_info->mdix_mode = e1000_auto_x_mode_undefined;
4265     phy_info->local_rx = e1000_1000t_rx_status_undefined;
4266     phy_info->remote_rx = e1000_1000t_rx_status_undefined;
4267
4268     if(hw->media_type != e1000_media_type_copper) {
4269         DEBUGOUT("PHY info is only valid for copper media\n");
4270         return -E1000_ERR_CONFIG;
4271     }
4272
4273     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
4274     if(ret_val)
4275         return ret_val;
4276
4277     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
4278     if(ret_val)
4279         return ret_val;
4280
4281     if((phy_data & MII_SR_LINK_STATUS) != MII_SR_LINK_STATUS) {
4282         DEBUGOUT("PHY info is only valid if link is up\n");
4283         return -E1000_ERR_CONFIG;
4284     }
4285
4286     if (hw->phy_type == e1000_phy_igp ||
4287         hw->phy_type == e1000_phy_igp_3 ||
4288         hw->phy_type == e1000_phy_igp_2)
4289         return e1000_phy_igp_get_info(hw, phy_info);
4290     else if (hw->phy_type == e1000_phy_ife)
4291         return e1000_phy_ife_get_info(hw, phy_info);
4292     else
4293         return e1000_phy_m88_get_info(hw, phy_info);
4294 }
4295
4296 int32_t
4297 e1000_validate_mdi_setting(struct e1000_hw *hw)
4298 {
4299     DEBUGFUNC("e1000_validate_mdi_settings");
4300
4301     if(!hw->autoneg && (hw->mdix == 0 || hw->mdix == 3)) {
4302         DEBUGOUT("Invalid MDI setting detected\n");
4303         hw->mdix = 1;
4304         return -E1000_ERR_CONFIG;
4305     }
4306     return E1000_SUCCESS;
4307 }
4308
4309
4310 /******************************************************************************
4311  * Sets up eeprom variables in the hw struct.  Must be called after mac_type
4312  * is configured.  Additionally, if this is ICH8, the flash controller GbE
4313  * registers must be mapped, or this will crash.
4314  *
4315  * hw - Struct containing variables accessed by shared code
4316  *****************************************************************************/
4317 int32_t
4318 e1000_init_eeprom_params(struct e1000_hw *hw)
4319 {
4320     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4321     uint32_t eecd = E1000_READ_REG(hw, EECD);
4322     int32_t ret_val = E1000_SUCCESS;
4323     uint16_t eeprom_size;
4324
4325     DEBUGFUNC("e1000_init_eeprom_params");
4326
4327     switch (hw->mac_type) {
4328     case e1000_82542_rev2_0:
4329     case e1000_82542_rev2_1:
4330     case e1000_82543:
4331     case e1000_82544:
4332         eeprom->type = e1000_eeprom_microwire;
4333         eeprom->word_size = 64;
4334         eeprom->opcode_bits = 3;
4335         eeprom->address_bits = 6;
4336         eeprom->delay_usec = 50;
4337         eeprom->use_eerd = FALSE;
4338         eeprom->use_eewr = FALSE;
4339         break;
4340     case e1000_82540:
4341     case e1000_82545:
4342     case e1000_82545_rev_3:
4343     case e1000_82546:
4344     case e1000_82546_rev_3:
4345         eeprom->type = e1000_eeprom_microwire;
4346         eeprom->opcode_bits = 3;
4347         eeprom->delay_usec = 50;
4348         if(eecd & E1000_EECD_SIZE) {
4349             eeprom->word_size = 256;
4350             eeprom->address_bits = 8;
4351         } else {
4352             eeprom->word_size = 64;
4353             eeprom->address_bits = 6;
4354         }
4355         eeprom->use_eerd = FALSE;
4356         eeprom->use_eewr = FALSE;
4357         break;
4358     case e1000_82541:
4359     case e1000_82541_rev_2:
4360     case e1000_82547:
4361     case e1000_82547_rev_2:
4362         if (eecd & E1000_EECD_TYPE) {
4363             eeprom->type = e1000_eeprom_spi;
4364             eeprom->opcode_bits = 8;
4365             eeprom->delay_usec = 1;
4366             if (eecd & E1000_EECD_ADDR_BITS) {
4367                 eeprom->page_size = 32;
4368                 eeprom->address_bits = 16;
4369             } else {
4370                 eeprom->page_size = 8;
4371                 eeprom->address_bits = 8;
4372             }
4373         } else {
4374             eeprom->type = e1000_eeprom_microwire;
4375             eeprom->opcode_bits = 3;
4376             eeprom->delay_usec = 50;
4377             if (eecd & E1000_EECD_ADDR_BITS) {
4378                 eeprom->word_size = 256;
4379                 eeprom->address_bits = 8;
4380             } else {
4381                 eeprom->word_size = 64;
4382                 eeprom->address_bits = 6;
4383             }
4384         }
4385         eeprom->use_eerd = FALSE;
4386         eeprom->use_eewr = FALSE;
4387         break;
4388     case e1000_82571:
4389     case e1000_82572:
4390         eeprom->type = e1000_eeprom_spi;
4391         eeprom->opcode_bits = 8;
4392         eeprom->delay_usec = 1;
4393         if (eecd & E1000_EECD_ADDR_BITS) {
4394             eeprom->page_size = 32;
4395             eeprom->address_bits = 16;
4396         } else {
4397             eeprom->page_size = 8;
4398             eeprom->address_bits = 8;
4399         }
4400         eeprom->use_eerd = FALSE;
4401         eeprom->use_eewr = FALSE;
4402         break;
4403     case e1000_82573:
4404         eeprom->type = e1000_eeprom_spi;
4405         eeprom->opcode_bits = 8;
4406         eeprom->delay_usec = 1;
4407         if (eecd & E1000_EECD_ADDR_BITS) {
4408             eeprom->page_size = 32;
4409             eeprom->address_bits = 16;
4410         } else {
4411             eeprom->page_size = 8;
4412             eeprom->address_bits = 8;
4413         }
4414         eeprom->use_eerd = TRUE;
4415         eeprom->use_eewr = TRUE;
4416         if(e1000_is_onboard_nvm_eeprom(hw) == FALSE) {
4417             eeprom->type = e1000_eeprom_flash;
4418             eeprom->word_size = 2048;
4419
4420             /* Ensure that the Autonomous FLASH update bit is cleared due to
4421              * Flash update issue on parts which use a FLASH for NVM. */
4422             eecd &= ~E1000_EECD_AUPDEN;
4423             E1000_WRITE_REG(hw, EECD, eecd);
4424         }
4425         break;
4426     case e1000_80003es2lan:
4427         eeprom->type = e1000_eeprom_spi;
4428         eeprom->opcode_bits = 8;
4429         eeprom->delay_usec = 1;
4430         if (eecd & E1000_EECD_ADDR_BITS) {
4431             eeprom->page_size = 32;
4432             eeprom->address_bits = 16;
4433         } else {
4434             eeprom->page_size = 8;
4435             eeprom->address_bits = 8;
4436         }
4437         eeprom->use_eerd = TRUE;
4438         eeprom->use_eewr = FALSE;
4439         break;
4440     case e1000_ich8lan:
4441     {
4442         int32_t  i = 0;
4443         uint32_t flash_size = E1000_READ_ICH8_REG(hw, ICH8_FLASH_GFPREG);
4444
4445         eeprom->type = e1000_eeprom_ich8;
4446         eeprom->use_eerd = FALSE;
4447         eeprom->use_eewr = FALSE;
4448         eeprom->word_size = E1000_SHADOW_RAM_WORDS;
4449
4450         /* Zero the shadow RAM structure. But don't load it from NVM
4451          * so as to save time for driver init */
4452         if (hw->eeprom_shadow_ram != NULL) {
4453             for (i = 0; i < E1000_SHADOW_RAM_WORDS; i++) {
4454                 hw->eeprom_shadow_ram[i].modified = FALSE;
4455                 hw->eeprom_shadow_ram[i].eeprom_word = 0xFFFF;
4456             }
4457         }
4458
4459         hw->flash_base_addr = (flash_size & ICH8_GFPREG_BASE_MASK) *
4460                               ICH8_FLASH_SECTOR_SIZE;
4461
4462         hw->flash_bank_size = ((flash_size >> 16) & ICH8_GFPREG_BASE_MASK) + 1;
4463         hw->flash_bank_size -= (flash_size & ICH8_GFPREG_BASE_MASK);
4464         hw->flash_bank_size *= ICH8_FLASH_SECTOR_SIZE;
4465         hw->flash_bank_size /= 2 * sizeof(uint16_t);
4466
4467         break;
4468     }
4469     default:
4470         break;
4471     }
4472
4473     if (eeprom->type == e1000_eeprom_spi) {
4474         /* eeprom_size will be an enum [0..8] that maps to eeprom sizes 128B to
4475          * 32KB (incremented by powers of 2).
4476          */
4477         if(hw->mac_type <= e1000_82547_rev_2) {
4478             /* Set to default value for initial eeprom read. */
4479             eeprom->word_size = 64;
4480             ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1, &eeprom_size);
4481             if(ret_val)
4482                 return ret_val;
4483             eeprom_size = (eeprom_size & EEPROM_SIZE_MASK) >> EEPROM_SIZE_SHIFT;
4484             /* 256B eeprom size was not supported in earlier hardware, so we
4485              * bump eeprom_size up one to ensure that "1" (which maps to 256B)
4486              * is never the result used in the shifting logic below. */
4487             if(eeprom_size)
4488                 eeprom_size++;
4489         } else {
4490             eeprom_size = (uint16_t)((eecd & E1000_EECD_SIZE_EX_MASK) >>
4491                           E1000_EECD_SIZE_EX_SHIFT);
4492         }
4493
4494         eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
4495     }
4496     return ret_val;
4497 }
4498
4499 /******************************************************************************
4500  * Raises the EEPROM's clock input.
4501  *
4502  * hw - Struct containing variables accessed by shared code
4503  * eecd - EECD's current value
4504  *****************************************************************************/
4505 static void
4506 e1000_raise_ee_clk(struct e1000_hw *hw,
4507                    uint32_t *eecd)
4508 {
4509     /* Raise the clock input to the EEPROM (by setting the SK bit), and then
4510      * wait <delay> microseconds.
4511      */
4512     *eecd = *eecd | E1000_EECD_SK;
4513     E1000_WRITE_REG(hw, EECD, *eecd);
4514     E1000_WRITE_FLUSH(hw);
4515     udelay(hw->eeprom.delay_usec);
4516 }
4517
4518 /******************************************************************************
4519  * Lowers the EEPROM's clock input.
4520  *
4521  * hw - Struct containing variables accessed by shared code
4522  * eecd - EECD's current value
4523  *****************************************************************************/
4524 static void
4525 e1000_lower_ee_clk(struct e1000_hw *hw,
4526                    uint32_t *eecd)
4527 {
4528     /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
4529      * wait 50 microseconds.
4530      */
4531     *eecd = *eecd & ~E1000_EECD_SK;
4532     E1000_WRITE_REG(hw, EECD, *eecd);
4533     E1000_WRITE_FLUSH(hw);
4534     udelay(hw->eeprom.delay_usec);
4535 }
4536
4537 /******************************************************************************
4538  * Shift data bits out to the EEPROM.
4539  *
4540  * hw - Struct containing variables accessed by shared code
4541  * data - data to send to the EEPROM
4542  * count - number of bits to shift out
4543  *****************************************************************************/
4544 static void
4545 e1000_shift_out_ee_bits(struct e1000_hw *hw,
4546                         uint16_t data,
4547                         uint16_t count)
4548 {
4549     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4550     uint32_t eecd;
4551     uint32_t mask;
4552
4553     /* We need to shift "count" bits out to the EEPROM. So, value in the
4554      * "data" parameter will be shifted out to the EEPROM one bit at a time.
4555      * In order to do this, "data" must be broken down into bits.
4556      */
4557     mask = 0x01 << (count - 1);
4558     eecd = E1000_READ_REG(hw, EECD);
4559     if (eeprom->type == e1000_eeprom_microwire) {
4560         eecd &= ~E1000_EECD_DO;
4561     } else if (eeprom->type == e1000_eeprom_spi) {
4562         eecd |= E1000_EECD_DO;
4563     }
4564     do {
4565         /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
4566          * and then raising and then lowering the clock (the SK bit controls
4567          * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
4568          * by setting "DI" to "0" and then raising and then lowering the clock.
4569          */
4570         eecd &= ~E1000_EECD_DI;
4571
4572         if(data & mask)
4573             eecd |= E1000_EECD_DI;
4574
4575         E1000_WRITE_REG(hw, EECD, eecd);
4576         E1000_WRITE_FLUSH(hw);
4577
4578         udelay(eeprom->delay_usec);
4579
4580         e1000_raise_ee_clk(hw, &eecd);
4581         e1000_lower_ee_clk(hw, &eecd);
4582
4583         mask = mask >> 1;
4584
4585     } while(mask);
4586
4587     /* We leave the "DI" bit set to "0" when we leave this routine. */
4588     eecd &= ~E1000_EECD_DI;
4589     E1000_WRITE_REG(hw, EECD, eecd);
4590 }
4591
4592 /******************************************************************************
4593  * Shift data bits in from the EEPROM
4594  *
4595  * hw - Struct containing variables accessed by shared code
4596  *****************************************************************************/
4597 static uint16_t
4598 e1000_shift_in_ee_bits(struct e1000_hw *hw,
4599                        uint16_t count)
4600 {
4601     uint32_t eecd;
4602     uint32_t i;
4603     uint16_t data;
4604
4605     /* In order to read a register from the EEPROM, we need to shift 'count'
4606      * bits in from the EEPROM. Bits are "shifted in" by raising the clock
4607      * input to the EEPROM (setting the SK bit), and then reading the value of
4608      * the "DO" bit.  During this "shifting in" process the "DI" bit should
4609      * always be clear.
4610      */
4611
4612     eecd = E1000_READ_REG(hw, EECD);
4613
4614     eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
4615     data = 0;
4616
4617     for(i = 0; i < count; i++) {
4618         data = data << 1;
4619         e1000_raise_ee_clk(hw, &eecd);
4620
4621         eecd = E1000_READ_REG(hw, EECD);
4622
4623         eecd &= ~(E1000_EECD_DI);
4624         if(eecd & E1000_EECD_DO)
4625             data |= 1;
4626
4627         e1000_lower_ee_clk(hw, &eecd);
4628     }
4629
4630     return data;
4631 }
4632
4633 /******************************************************************************
4634  * Prepares EEPROM for access
4635  *
4636  * hw - Struct containing variables accessed by shared code
4637  *
4638  * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
4639  * function should be called before issuing a command to the EEPROM.
4640  *****************************************************************************/
4641 static int32_t
4642 e1000_acquire_eeprom(struct e1000_hw *hw)
4643 {
4644     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4645     uint32_t eecd, i=0;
4646
4647     DEBUGFUNC("e1000_acquire_eeprom");
4648
4649     if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
4650         return -E1000_ERR_SWFW_SYNC;
4651     eecd = E1000_READ_REG(hw, EECD);
4652
4653     if (hw->mac_type != e1000_82573) {
4654         /* Request EEPROM Access */
4655         if(hw->mac_type > e1000_82544) {
4656             eecd |= E1000_EECD_REQ;
4657             E1000_WRITE_REG(hw, EECD, eecd);
4658             eecd = E1000_READ_REG(hw, EECD);
4659             while((!(eecd & E1000_EECD_GNT)) &&
4660                   (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
4661                 i++;
4662                 udelay(5);
4663                 eecd = E1000_READ_REG(hw, EECD);
4664             }
4665             if(!(eecd & E1000_EECD_GNT)) {
4666                 eecd &= ~E1000_EECD_REQ;
4667                 E1000_WRITE_REG(hw, EECD, eecd);
4668                 DEBUGOUT("Could not acquire EEPROM grant\n");
4669                 e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
4670                 return -E1000_ERR_EEPROM;
4671             }
4672         }
4673     }
4674
4675     /* Setup EEPROM for Read/Write */
4676
4677     if (eeprom->type == e1000_eeprom_microwire) {
4678         /* Clear SK and DI */
4679         eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
4680         E1000_WRITE_REG(hw, EECD, eecd);
4681
4682         /* Set CS */
4683         eecd |= E1000_EECD_CS;
4684         E1000_WRITE_REG(hw, EECD, eecd);
4685     } else if (eeprom->type == e1000_eeprom_spi) {
4686         /* Clear SK and CS */
4687         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
4688         E1000_WRITE_REG(hw, EECD, eecd);
4689         udelay(1);
4690     }
4691
4692     return E1000_SUCCESS;
4693 }
4694
4695 /******************************************************************************
4696  * Returns EEPROM to a "standby" state
4697  *
4698  * hw - Struct containing variables accessed by shared code
4699  *****************************************************************************/
4700 static void
4701 e1000_standby_eeprom(struct e1000_hw *hw)
4702 {
4703     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4704     uint32_t eecd;
4705
4706     eecd = E1000_READ_REG(hw, EECD);
4707
4708     if(eeprom->type == e1000_eeprom_microwire) {
4709         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
4710         E1000_WRITE_REG(hw, EECD, eecd);
4711         E1000_WRITE_FLUSH(hw);
4712         udelay(eeprom->delay_usec);
4713
4714         /* Clock high */
4715         eecd |= E1000_EECD_SK;
4716         E1000_WRITE_REG(hw, EECD, eecd);
4717         E1000_WRITE_FLUSH(hw);
4718         udelay(eeprom->delay_usec);
4719
4720         /* Select EEPROM */
4721         eecd |= E1000_EECD_CS;
4722         E1000_WRITE_REG(hw, EECD, eecd);
4723         E1000_WRITE_FLUSH(hw);
4724         udelay(eeprom->delay_usec);
4725
4726         /* Clock low */
4727         eecd &= ~E1000_EECD_SK;
4728         E1000_WRITE_REG(hw, EECD, eecd);
4729         E1000_WRITE_FLUSH(hw);
4730         udelay(eeprom->delay_usec);
4731     } else if(eeprom->type == e1000_eeprom_spi) {
4732         /* Toggle CS to flush commands */
4733         eecd |= E1000_EECD_CS;
4734         E1000_WRITE_REG(hw, EECD, eecd);
4735         E1000_WRITE_FLUSH(hw);
4736         udelay(eeprom->delay_usec);
4737         eecd &= ~E1000_EECD_CS;
4738         E1000_WRITE_REG(hw, EECD, eecd);
4739         E1000_WRITE_FLUSH(hw);
4740         udelay(eeprom->delay_usec);
4741     }
4742 }
4743
4744 /******************************************************************************
4745  * Terminates a command by inverting the EEPROM's chip select pin
4746  *
4747  * hw - Struct containing variables accessed by shared code
4748  *****************************************************************************/
4749 static void
4750 e1000_release_eeprom(struct e1000_hw *hw)
4751 {
4752     uint32_t eecd;
4753
4754     DEBUGFUNC("e1000_release_eeprom");
4755
4756     eecd = E1000_READ_REG(hw, EECD);
4757
4758     if (hw->eeprom.type == e1000_eeprom_spi) {
4759         eecd |= E1000_EECD_CS;  /* Pull CS high */
4760         eecd &= ~E1000_EECD_SK; /* Lower SCK */
4761
4762         E1000_WRITE_REG(hw, EECD, eecd);
4763
4764         udelay(hw->eeprom.delay_usec);
4765     } else if(hw->eeprom.type == e1000_eeprom_microwire) {
4766         /* cleanup eeprom */
4767
4768         /* CS on Microwire is active-high */
4769         eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
4770
4771         E1000_WRITE_REG(hw, EECD, eecd);
4772
4773         /* Rising edge of clock */
4774         eecd |= E1000_EECD_SK;
4775         E1000_WRITE_REG(hw, EECD, eecd);
4776         E1000_WRITE_FLUSH(hw);
4777         udelay(hw->eeprom.delay_usec);
4778
4779         /* Falling edge of clock */
4780         eecd &= ~E1000_EECD_SK;
4781         E1000_WRITE_REG(hw, EECD, eecd);
4782         E1000_WRITE_FLUSH(hw);
4783         udelay(hw->eeprom.delay_usec);
4784     }
4785
4786     /* Stop requesting EEPROM access */
4787     if(hw->mac_type > e1000_82544) {
4788         eecd &= ~E1000_EECD_REQ;
4789         E1000_WRITE_REG(hw, EECD, eecd);
4790     }
4791
4792     e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
4793 }
4794
4795 /******************************************************************************
4796  * Reads a 16 bit word from the EEPROM.
4797  *
4798  * hw - Struct containing variables accessed by shared code
4799  *****************************************************************************/
4800 int32_t
4801 e1000_spi_eeprom_ready(struct e1000_hw *hw)
4802 {
4803     uint16_t retry_count = 0;
4804     uint8_t spi_stat_reg;
4805
4806     DEBUGFUNC("e1000_spi_eeprom_ready");
4807
4808     /* Read "Status Register" repeatedly until the LSB is cleared.  The
4809      * EEPROM will signal that the command has been completed by clearing
4810      * bit 0 of the internal status register.  If it's not cleared within
4811      * 5 milliseconds, then error out.
4812      */
4813     retry_count = 0;
4814     do {
4815         e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
4816                                 hw->eeprom.opcode_bits);
4817         spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
4818         if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
4819             break;
4820
4821         udelay(5);
4822         retry_count += 5;
4823
4824         e1000_standby_eeprom(hw);
4825     } while(retry_count < EEPROM_MAX_RETRY_SPI);
4826
4827     /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
4828      * only 0-5mSec on 5V devices)
4829      */
4830     if(retry_count >= EEPROM_MAX_RETRY_SPI) {
4831         DEBUGOUT("SPI EEPROM Status error\n");
4832         return -E1000_ERR_EEPROM;
4833     }
4834
4835     return E1000_SUCCESS;
4836 }
4837
4838 /******************************************************************************
4839  * Reads a 16 bit word from the EEPROM.
4840  *
4841  * hw - Struct containing variables accessed by shared code
4842  * offset - offset of  word in the EEPROM to read
4843  * data - word read from the EEPROM
4844  * words - number of words to read
4845  *****************************************************************************/
4846 int32_t
4847 e1000_read_eeprom(struct e1000_hw *hw,
4848                   uint16_t offset,
4849                   uint16_t words,
4850                   uint16_t *data)
4851 {
4852     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4853     uint32_t i = 0;
4854     int32_t ret_val;
4855
4856     DEBUGFUNC("e1000_read_eeprom");
4857
4858     /* A check for invalid values:  offset too large, too many words, and not
4859      * enough words.
4860      */
4861     if((offset >= eeprom->word_size) || (words > eeprom->word_size - offset) ||
4862        (words == 0)) {
4863         DEBUGOUT("\"words\" parameter out of bounds\n");
4864         return -E1000_ERR_EEPROM;
4865     }
4866
4867     /* FLASH reads without acquiring the semaphore are safe */
4868     if (e1000_is_onboard_nvm_eeprom(hw) == TRUE &&
4869     hw->eeprom.use_eerd == FALSE) {
4870         switch (hw->mac_type) {
4871         case e1000_80003es2lan:
4872             break;
4873         default:
4874             /* Prepare the EEPROM for reading  */
4875             if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
4876                 return -E1000_ERR_EEPROM;
4877             break;
4878         }
4879     }
4880
4881     if (eeprom->use_eerd == TRUE) {
4882         ret_val = e1000_read_eeprom_eerd(hw, offset, words, data);
4883         if ((e1000_is_onboard_nvm_eeprom(hw) == TRUE) ||
4884             (hw->mac_type != e1000_82573))
4885             e1000_release_eeprom(hw);
4886         return ret_val;
4887     }
4888
4889     if (eeprom->type == e1000_eeprom_ich8)
4890         return e1000_read_eeprom_ich8(hw, offset, words, data);
4891
4892     if (eeprom->type == e1000_eeprom_spi) {
4893         uint16_t word_in;
4894         uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
4895
4896         if(e1000_spi_eeprom_ready(hw)) {
4897             e1000_release_eeprom(hw);
4898             return -E1000_ERR_EEPROM;
4899         }
4900
4901         e1000_standby_eeprom(hw);
4902
4903         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
4904         if((eeprom->address_bits == 8) && (offset >= 128))
4905             read_opcode |= EEPROM_A8_OPCODE_SPI;
4906
4907         /* Send the READ command (opcode + addr)  */
4908         e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
4909         e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2), eeprom->address_bits);
4910
4911         /* Read the data.  The address of the eeprom internally increments with
4912          * each byte (spi) being read, saving on the overhead of eeprom setup
4913          * and tear-down.  The address counter will roll over if reading beyond
4914          * the size of the eeprom, thus allowing the entire memory to be read
4915          * starting from any offset. */
4916         for (i = 0; i < words; i++) {
4917             word_in = e1000_shift_in_ee_bits(hw, 16);
4918             data[i] = (word_in >> 8) | (word_in << 8);
4919         }
4920     } else if(eeprom->type == e1000_eeprom_microwire) {
4921         for (i = 0; i < words; i++) {
4922             /* Send the READ command (opcode + addr)  */
4923             e1000_shift_out_ee_bits(hw, EEPROM_READ_OPCODE_MICROWIRE,
4924                                     eeprom->opcode_bits);
4925             e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
4926                                     eeprom->address_bits);
4927
4928             /* Read the data.  For microwire, each word requires the overhead
4929              * of eeprom setup and tear-down. */
4930             data[i] = e1000_shift_in_ee_bits(hw, 16);
4931             e1000_standby_eeprom(hw);
4932         }
4933     }
4934
4935     /* End this read operation */
4936     e1000_release_eeprom(hw);
4937
4938     return E1000_SUCCESS;
4939 }
4940
4941 /******************************************************************************
4942  * Reads a 16 bit word from the EEPROM using the EERD register.
4943  *
4944  * hw - Struct containing variables accessed by shared code
4945  * offset - offset of  word in the EEPROM to read
4946  * data - word read from the EEPROM
4947  * words - number of words to read
4948  *****************************************************************************/
4949 static int32_t
4950 e1000_read_eeprom_eerd(struct e1000_hw *hw,
4951                   uint16_t offset,
4952                   uint16_t words,
4953                   uint16_t *data)
4954 {
4955     uint32_t i, eerd = 0;
4956     int32_t error = 0;
4957
4958     for (i = 0; i < words; i++) {
4959         eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
4960                          E1000_EEPROM_RW_REG_START;
4961
4962         E1000_WRITE_REG(hw, EERD, eerd);
4963         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
4964
4965         if(error) {
4966             break;
4967         }
4968         data[i] = (E1000_READ_REG(hw, EERD) >> E1000_EEPROM_RW_REG_DATA);
4969
4970     }
4971
4972     return error;
4973 }
4974
4975 /******************************************************************************
4976  * Writes a 16 bit word from the EEPROM using the EEWR register.
4977  *
4978  * hw - Struct containing variables accessed by shared code
4979  * offset - offset of  word in the EEPROM to read
4980  * data - word read from the EEPROM
4981  * words - number of words to read
4982  *****************************************************************************/
4983 static int32_t
4984 e1000_write_eeprom_eewr(struct e1000_hw *hw,
4985                    uint16_t offset,
4986                    uint16_t words,
4987                    uint16_t *data)
4988 {
4989     uint32_t    register_value = 0;
4990     uint32_t    i              = 0;
4991     int32_t     error          = 0;
4992
4993     if (e1000_swfw_sync_acquire(hw, E1000_SWFW_EEP_SM))
4994         return -E1000_ERR_SWFW_SYNC;
4995
4996     for (i = 0; i < words; i++) {
4997         register_value = (data[i] << E1000_EEPROM_RW_REG_DATA) |
4998                          ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) |
4999                          E1000_EEPROM_RW_REG_START;
5000
5001         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
5002         if(error) {
5003             break;
5004         }
5005
5006         E1000_WRITE_REG(hw, EEWR, register_value);
5007
5008         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
5009
5010         if(error) {
5011             break;
5012         }
5013     }
5014
5015     e1000_swfw_sync_release(hw, E1000_SWFW_EEP_SM);
5016     return error;
5017 }
5018
5019 /******************************************************************************
5020  * Polls the status bit (bit 1) of the EERD to determine when the read is done.
5021  *
5022  * hw - Struct containing variables accessed by shared code
5023  *****************************************************************************/
5024 static int32_t
5025 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
5026 {
5027     uint32_t attempts = 100000;
5028     uint32_t i, reg = 0;
5029     int32_t done = E1000_ERR_EEPROM;
5030
5031     for(i = 0; i < attempts; i++) {
5032         if(eerd == E1000_EEPROM_POLL_READ)
5033             reg = E1000_READ_REG(hw, EERD);
5034         else
5035             reg = E1000_READ_REG(hw, EEWR);
5036
5037         if(reg & E1000_EEPROM_RW_REG_DONE) {
5038             done = E1000_SUCCESS;
5039             break;
5040         }
5041         udelay(5);
5042     }
5043
5044     return done;
5045 }
5046
5047 /***************************************************************************
5048 * Description:     Determines if the onboard NVM is FLASH or EEPROM.
5049 *
5050 * hw - Struct containing variables accessed by shared code
5051 ****************************************************************************/
5052 static boolean_t
5053 e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
5054 {
5055     uint32_t eecd = 0;
5056
5057     DEBUGFUNC("e1000_is_onboard_nvm_eeprom");
5058
5059     if (hw->mac_type == e1000_ich8lan)
5060         return FALSE;
5061
5062     if (hw->mac_type == e1000_82573) {
5063         eecd = E1000_READ_REG(hw, EECD);
5064
5065         /* Isolate bits 15 & 16 */
5066         eecd = ((eecd >> 15) & 0x03);
5067
5068         /* If both bits are set, device is Flash type */
5069         if(eecd == 0x03) {
5070             return FALSE;
5071         }
5072     }
5073     return TRUE;
5074 }
5075
5076 /******************************************************************************
5077  * Verifies that the EEPROM has a valid checksum
5078  *
5079  * hw - Struct containing variables accessed by shared code
5080  *
5081  * Reads the first 64 16 bit words of the EEPROM and sums the values read.
5082  * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
5083  * valid.
5084  *****************************************************************************/
5085 int32_t
5086 e1000_validate_eeprom_checksum(struct e1000_hw *hw)
5087 {
5088     uint16_t checksum = 0;
5089     uint16_t i, eeprom_data;
5090
5091     DEBUGFUNC("e1000_validate_eeprom_checksum");
5092
5093     if ((hw->mac_type == e1000_82573) &&
5094         (e1000_is_onboard_nvm_eeprom(hw) == FALSE)) {
5095         /* Check bit 4 of word 10h.  If it is 0, firmware is done updating
5096          * 10h-12h.  Checksum may need to be fixed. */
5097         e1000_read_eeprom(hw, 0x10, 1, &eeprom_data);
5098         if ((eeprom_data & 0x10) == 0) {
5099             /* Read 0x23 and check bit 15.  This bit is a 1 when the checksum
5100              * has already been fixed.  If the checksum is still wrong and this
5101              * bit is a 1, we need to return bad checksum.  Otherwise, we need
5102              * to set this bit to a 1 and update the checksum. */
5103             e1000_read_eeprom(hw, 0x23, 1, &eeprom_data);
5104             if ((eeprom_data & 0x8000) == 0) {
5105                 eeprom_data |= 0x8000;
5106                 e1000_write_eeprom(hw, 0x23, 1, &eeprom_data);
5107                 e1000_update_eeprom_checksum(hw);
5108             }
5109         }
5110     }
5111
5112     if (hw->mac_type == e1000_ich8lan) {
5113         /* Drivers must allocate the shadow ram structure for the
5114          * EEPROM checksum to be updated.  Otherwise, this bit as well
5115          * as the checksum must both be set correctly for this
5116          * validation to pass.
5117          */
5118         e1000_read_eeprom(hw, 0x19, 1, &eeprom_data);
5119         if ((eeprom_data & 0x40) == 0) {
5120             eeprom_data |= 0x40;
5121             e1000_write_eeprom(hw, 0x19, 1, &eeprom_data);
5122             e1000_update_eeprom_checksum(hw);
5123         }
5124     }
5125
5126     for (i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
5127         if (e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
5128             DEBUGOUT("EEPROM Read Error\n");
5129             return -E1000_ERR_EEPROM;
5130         }
5131         checksum += eeprom_data;
5132     }
5133
5134     if(checksum == (uint16_t) EEPROM_SUM)
5135         return E1000_SUCCESS;
5136     else {
5137         DEBUGOUT("EEPROM Checksum Invalid\n");
5138         return -E1000_ERR_EEPROM;
5139     }
5140 }
5141
5142 /******************************************************************************
5143  * Calculates the EEPROM checksum and writes it to the EEPROM
5144  *
5145  * hw - Struct containing variables accessed by shared code
5146  *
5147  * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
5148  * Writes the difference to word offset 63 of the EEPROM.
5149  *****************************************************************************/
5150 int32_t
5151 e1000_update_eeprom_checksum(struct e1000_hw *hw)
5152 {
5153     uint32_t ctrl_ext;
5154     uint16_t checksum = 0;
5155     uint16_t i, eeprom_data;
5156
5157     DEBUGFUNC("e1000_update_eeprom_checksum");
5158
5159     for(i = 0; i < EEPROM_CHECKSUM_REG; i++) {
5160         if(e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
5161             DEBUGOUT("EEPROM Read Error\n");
5162             return -E1000_ERR_EEPROM;
5163         }
5164         checksum += eeprom_data;
5165     }
5166     checksum = (uint16_t) EEPROM_SUM - checksum;
5167     if(e1000_write_eeprom(hw, EEPROM_CHECKSUM_REG, 1, &checksum) < 0) {
5168         DEBUGOUT("EEPROM Write Error\n");
5169         return -E1000_ERR_EEPROM;
5170     } else if (hw->eeprom.type == e1000_eeprom_flash) {
5171         e1000_commit_shadow_ram(hw);
5172     } else if (hw->eeprom.type == e1000_eeprom_ich8) {
5173         e1000_commit_shadow_ram(hw);
5174         /* Reload the EEPROM, or else modifications will not appear
5175          * until after next adapter reset. */
5176         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
5177         ctrl_ext |= E1000_CTRL_EXT_EE_RST;
5178         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
5179         msec_delay(10);
5180     }
5181     return E1000_SUCCESS;
5182 }
5183
5184 /******************************************************************************
5185  * Parent function for writing words to the different EEPROM types.
5186  *
5187  * hw - Struct containing variables accessed by shared code
5188  * offset - offset within the EEPROM to be written to
5189  * words - number of words to write
5190  * data - 16 bit word to be written to the EEPROM
5191  *
5192  * If e1000_update_eeprom_checksum is not called after this function, the
5193  * EEPROM will most likely contain an invalid checksum.
5194  *****************************************************************************/
5195 int32_t
5196 e1000_write_eeprom(struct e1000_hw *hw,
5197                    uint16_t offset,
5198                    uint16_t words,
5199                    uint16_t *data)
5200 {
5201     struct e1000_eeprom_info *eeprom = &hw->eeprom;
5202     int32_t status = 0;
5203
5204     DEBUGFUNC("e1000_write_eeprom");
5205
5206     /* A check for invalid values:  offset too large, too many words, and not
5207      * enough words.
5208      */
5209     if((offset >= eeprom->word_size) || (words > eeprom->word_size - offset) ||
5210        (words == 0)) {
5211         DEBUGOUT("\"words\" parameter out of bounds\n");
5212         return -E1000_ERR_EEPROM;
5213     }
5214
5215     /* 82573 writes only through eewr */
5216     if(eeprom->use_eewr == TRUE)
5217         return e1000_write_eeprom_eewr(hw, offset, words, data);
5218
5219     if (eeprom->type == e1000_eeprom_ich8)
5220         return e1000_write_eeprom_ich8(hw, offset, words, data);
5221
5222     /* Prepare the EEPROM for writing  */
5223     if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
5224         return -E1000_ERR_EEPROM;
5225
5226     if(eeprom->type == e1000_eeprom_microwire) {
5227         status = e1000_write_eeprom_microwire(hw, offset, words, data);
5228     } else {
5229         status = e1000_write_eeprom_spi(hw, offset, words, data);
5230         msec_delay(10);
5231     }
5232
5233     /* Done with writing */
5234     e1000_release_eeprom(hw);
5235
5236     return status;
5237 }
5238
5239 /******************************************************************************
5240  * Writes a 16 bit word to a given offset in an SPI EEPROM.
5241  *
5242  * hw - Struct containing variables accessed by shared code
5243  * offset - offset within the EEPROM to be written to
5244  * words - number of words to write
5245  * data - pointer to array of 8 bit words to be written to the EEPROM
5246  *
5247  *****************************************************************************/
5248 int32_t
5249 e1000_write_eeprom_spi(struct e1000_hw *hw,
5250                        uint16_t offset,
5251                        uint16_t words,
5252                        uint16_t *data)
5253 {
5254     struct e1000_eeprom_info *eeprom = &hw->eeprom;
5255     uint16_t widx = 0;
5256
5257     DEBUGFUNC("e1000_write_eeprom_spi");
5258
5259     while (widx < words) {
5260         uint8_t write_opcode = EEPROM_WRITE_OPCODE_SPI;
5261
5262         if(e1000_spi_eeprom_ready(hw)) return -E1000_ERR_EEPROM;
5263
5264         e1000_standby_eeprom(hw);
5265
5266         /*  Send the WRITE ENABLE command (8 bit opcode )  */
5267         e1000_shift_out_ee_bits(hw, EEPROM_WREN_OPCODE_SPI,
5268                                     eeprom->opcode_bits);
5269
5270         e1000_standby_eeprom(hw);
5271
5272         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
5273         if((eeprom->address_bits == 8) && (offset >= 128))
5274             write_opcode |= EEPROM_A8_OPCODE_SPI;
5275
5276         /* Send the Write command (8-bit opcode + addr) */
5277         e1000_shift_out_ee_bits(hw, write_opcode, eeprom->opcode_bits);
5278
5279         e1000_shift_out_ee_bits(hw, (uint16_t)((offset + widx)*2),
5280                                 eeprom->address_bits);
5281
5282         /* Send the data */
5283
5284         /* Loop to allow for up to whole page write (32 bytes) of eeprom */
5285         while (widx < words) {
5286             uint16_t word_out = data[widx];
5287             word_out = (word_out >> 8) | (word_out << 8);
5288             e1000_shift_out_ee_bits(hw, word_out, 16);
5289             widx++;
5290
5291             /* Some larger eeprom sizes are capable of a 32-byte PAGE WRITE
5292              * operation, while the smaller eeproms are capable of an 8-byte
5293              * PAGE WRITE operation.  Break the inner loop to pass new address
5294              */
5295             if((((offset + widx)*2) % eeprom->page_size) == 0) {
5296                 e1000_standby_eeprom(hw);
5297                 break;
5298             }
5299         }
5300     }
5301
5302     return E1000_SUCCESS;
5303 }
5304
5305 /******************************************************************************
5306  * Writes a 16 bit word to a given offset in a Microwire EEPROM.
5307  *
5308  * hw - Struct containing variables accessed by shared code
5309  * offset - offset within the EEPROM to be written to
5310  * words - number of words to write
5311  * data - pointer to array of 16 bit words to be written to the EEPROM
5312  *
5313  *****************************************************************************/
5314 int32_t
5315 e1000_write_eeprom_microwire(struct e1000_hw *hw,
5316                              uint16_t offset,
5317                              uint16_t words,
5318                              uint16_t *data)
5319 {
5320     struct e1000_eeprom_info *eeprom = &hw->eeprom;
5321     uint32_t eecd;
5322     uint16_t words_written = 0;
5323     uint16_t i = 0;
5324
5325     DEBUGFUNC("e1000_write_eeprom_microwire");
5326
5327     /* Send the write enable command to the EEPROM (3-bit opcode plus
5328      * 6/8-bit dummy address beginning with 11).  It's less work to include
5329      * the 11 of the dummy address as part of the opcode than it is to shift
5330      * it over the correct number of bits for the address.  This puts the
5331      * EEPROM into write/erase mode.
5332      */
5333     e1000_shift_out_ee_bits(hw, EEPROM_EWEN_OPCODE_MICROWIRE,
5334                             (uint16_t)(eeprom->opcode_bits + 2));
5335
5336     e1000_shift_out_ee_bits(hw, 0, (uint16_t)(eeprom->address_bits - 2));
5337
5338     /* Prepare the EEPROM */
5339     e1000_standby_eeprom(hw);
5340
5341     while (words_written < words) {
5342         /* Send the Write command (3-bit opcode + addr) */
5343         e1000_shift_out_ee_bits(hw, EEPROM_WRITE_OPCODE_MICROWIRE,
5344                                 eeprom->opcode_bits);
5345
5346         e1000_shift_out_ee_bits(hw, (uint16_t)(offset + words_written),
5347                                 eeprom->address_bits);
5348
5349         /* Send the data */
5350         e1000_shift_out_ee_bits(hw, data[words_written], 16);
5351
5352         /* Toggle the CS line.  This in effect tells the EEPROM to execute
5353          * the previous command.
5354          */
5355         e1000_standby_eeprom(hw);
5356
5357         /* Read DO repeatedly until it is high (equal to '1').  The EEPROM will
5358          * signal that the command has been completed by raising the DO signal.
5359          * If DO does not go high in 10 milliseconds, then error out.
5360          */
5361         for(i = 0; i < 200; i++) {
5362             eecd = E1000_READ_REG(hw, EECD);
5363             if(eecd & E1000_EECD_DO) break;
5364             udelay(50);
5365         }
5366         if(i == 200) {
5367             DEBUGOUT("EEPROM Write did not complete\n");
5368             return -E1000_ERR_EEPROM;
5369         }
5370
5371         /* Recover from write */
5372         e1000_standby_eeprom(hw);
5373
5374         words_written++;
5375     }
5376
5377     /* Send the write disable command to the EEPROM (3-bit opcode plus
5378      * 6/8-bit dummy address beginning with 10).  It's less work to include
5379      * the 10 of the dummy address as part of the opcode than it is to shift
5380      * it over the correct number of bits for the address.  This takes the
5381      * EEPROM out of write/erase mode.
5382      */
5383     e1000_shift_out_ee_bits(hw, EEPROM_EWDS_OPCODE_MICROWIRE,
5384                             (uint16_t)(eeprom->opcode_bits + 2));
5385
5386     e1000_shift_out_ee_bits(hw, 0, (uint16_t)(eeprom->address_bits - 2));
5387
5388     return E1000_SUCCESS;
5389 }
5390
5391 /******************************************************************************
5392  * Flushes the cached eeprom to NVM. This is done by saving the modified values
5393  * in the eeprom cache and the non modified values in the currently active bank
5394  * to the new bank.
5395  *
5396  * hw - Struct containing variables accessed by shared code
5397  * offset - offset of  word in the EEPROM to read
5398  * data - word read from the EEPROM
5399  * words - number of words to read
5400  *****************************************************************************/
5401 static int32_t
5402 e1000_commit_shadow_ram(struct e1000_hw *hw)
5403 {
5404     uint32_t attempts = 100000;
5405     uint32_t eecd = 0;
5406     uint32_t flop = 0;
5407     uint32_t i = 0;
5408     int32_t error = E1000_SUCCESS;
5409     uint32_t old_bank_offset = 0;
5410     uint32_t new_bank_offset = 0;
5411     uint32_t sector_retries = 0;
5412     uint8_t low_byte = 0;
5413     uint8_t high_byte = 0;
5414     uint8_t temp_byte = 0;
5415     boolean_t sector_write_failed = FALSE;
5416
5417     if (hw->mac_type == e1000_82573) {
5418         /* The flop register will be used to determine if flash type is STM */
5419         flop = E1000_READ_REG(hw, FLOP);
5420         for (i=0; i < attempts; i++) {
5421             eecd = E1000_READ_REG(hw, EECD);
5422             if ((eecd & E1000_EECD_FLUPD) == 0) {
5423                 break;
5424             }
5425             udelay(5);
5426         }
5427
5428         if (i == attempts) {
5429             return -E1000_ERR_EEPROM;
5430         }
5431
5432         /* If STM opcode located in bits 15:8 of flop, reset firmware */
5433         if ((flop & 0xFF00) == E1000_STM_OPCODE) {
5434             E1000_WRITE_REG(hw, HICR, E1000_HICR_FW_RESET);
5435         }
5436
5437         /* Perform the flash update */
5438         E1000_WRITE_REG(hw, EECD, eecd | E1000_EECD_FLUPD);
5439
5440         for (i=0; i < attempts; i++) {
5441             eecd = E1000_READ_REG(hw, EECD);
5442             if ((eecd & E1000_EECD_FLUPD) == 0) {
5443                 break;
5444             }
5445             udelay(5);
5446         }
5447
5448         if (i == attempts) {
5449             return -E1000_ERR_EEPROM;
5450         }
5451     }
5452
5453     if (hw->mac_type == e1000_ich8lan && hw->eeprom_shadow_ram != NULL) {
5454         /* We're writing to the opposite bank so if we're on bank 1,
5455          * write to bank 0 etc.  We also need to erase the segment that
5456          * is going to be written */
5457         if (!(E1000_READ_REG(hw, EECD) & E1000_EECD_SEC1VAL)) {
5458             new_bank_offset = hw->flash_bank_size * 2;
5459             old_bank_offset = 0;
5460             e1000_erase_ich8_4k_segment(hw, 1);
5461         } else {
5462             old_bank_offset = hw->flash_bank_size * 2;
5463             new_bank_offset = 0;
5464             e1000_erase_ich8_4k_segment(hw, 0);
5465         }
5466
5467         do {
5468             sector_write_failed = FALSE;
5469             /* Loop for every byte in the shadow RAM,
5470              * which is in units of words. */
5471             for (i = 0; i < E1000_SHADOW_RAM_WORDS; i++) {
5472                 /* Determine whether to write the value stored
5473                  * in the other NVM bank or a modified value stored
5474                  * in the shadow RAM */
5475                 if (hw->eeprom_shadow_ram[i].modified == TRUE) {
5476                     low_byte = (uint8_t)hw->eeprom_shadow_ram[i].eeprom_word;
5477                     e1000_read_ich8_byte(hw, (i << 1) + old_bank_offset,
5478                                          &temp_byte);
5479                     udelay(100);
5480                     error = e1000_verify_write_ich8_byte(hw,
5481                                                  (i << 1) + new_bank_offset,
5482                                                  low_byte);
5483                     if (error != E1000_SUCCESS)
5484                         sector_write_failed = TRUE;
5485                     high_byte =
5486                         (uint8_t)(hw->eeprom_shadow_ram[i].eeprom_word >> 8);
5487                     e1000_read_ich8_byte(hw, (i << 1) + old_bank_offset + 1,
5488                                          &temp_byte);
5489                     udelay(100);
5490                 } else {
5491                     e1000_read_ich8_byte(hw, (i << 1) + old_bank_offset,
5492                                          &low_byte);
5493                     udelay(100);
5494                     error = e1000_verify_write_ich8_byte(hw,
5495                                  (i << 1) + new_bank_offset, low_byte);
5496                     if (error != E1000_SUCCESS)
5497                         sector_write_failed = TRUE;
5498                     e1000_read_ich8_byte(hw, (i << 1) + old_bank_offset + 1,
5499                                          &high_byte);
5500                 }
5501
5502                 /* If the word is 0x13, then make sure the signature bits
5503                  * (15:14) are 11b until the commit has completed.
5504                  * This will allow us to write 10b which indicates the
5505                  * signature is valid.  We want to do this after the write
5506                  * has completed so that we don't mark the segment valid
5507                  * while the write is still in progress */
5508                 if (i == E1000_ICH8_NVM_SIG_WORD)
5509                     high_byte = E1000_ICH8_NVM_SIG_MASK | high_byte;
5510
5511                 error = e1000_verify_write_ich8_byte(hw,
5512                              (i << 1) + new_bank_offset + 1, high_byte);
5513                 if (error != E1000_SUCCESS)
5514                     sector_write_failed = TRUE;
5515
5516                 if (sector_write_failed == FALSE) {
5517                     /* Clear the now not used entry in the cache */
5518                     hw->eeprom_shadow_ram[i].modified = FALSE;
5519                     hw->eeprom_shadow_ram[i].eeprom_word = 0xFFFF;
5520                 }
5521             }
5522
5523             /* Don't bother writing the segment valid bits if sector
5524              * programming failed. */
5525             if (sector_write_failed == FALSE) {
5526                 /* Finally validate the new segment by setting bit 15:14
5527                  * to 10b in word 0x13 , this can be done without an
5528                  * erase as well since these bits are 11 to start with
5529                  * and we need to change bit 14 to 0b */
5530                 e1000_read_ich8_byte(hw,
5531                     E1000_ICH8_NVM_SIG_WORD * 2 + 1 + new_bank_offset,
5532                     &high_byte);
5533                 high_byte &= 0xBF;
5534                 error = e1000_verify_write_ich8_byte(hw,
5535                             E1000_ICH8_NVM_SIG_WORD * 2 + 1 + new_bank_offset,
5536                             high_byte);
5537                 if (error != E1000_SUCCESS)
5538                     sector_write_failed = TRUE;
5539
5540                 /* And invalidate the previously valid segment by setting
5541                  * its signature word (0x13) high_byte to 0b. This can be
5542                  * done without an erase because flash erase sets all bits
5543                  * to 1's. We can write 1's to 0's without an erase */
5544                 error = e1000_verify_write_ich8_byte(hw,
5545                             E1000_ICH8_NVM_SIG_WORD * 2 + 1 + old_bank_offset,
5546                             0);
5547                 if (error != E1000_SUCCESS)
5548                     sector_write_failed = TRUE;
5549             }
5550         } while (++sector_retries < 10 && sector_write_failed == TRUE);
5551     }
5552
5553     return error;
5554 }
5555
5556 /******************************************************************************
5557  * Reads the adapter's part number from the EEPROM
5558  *
5559  * hw - Struct containing variables accessed by shared code
5560  * part_num - Adapter's part number
5561  *****************************************************************************/
5562 int32_t
5563 e1000_read_part_num(struct e1000_hw *hw,
5564                     uint32_t *part_num)
5565 {
5566     uint16_t offset = EEPROM_PBA_BYTE_1;
5567     uint16_t eeprom_data;
5568
5569     DEBUGFUNC("e1000_read_part_num");
5570
5571     /* Get word 0 from EEPROM */
5572     if(e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
5573         DEBUGOUT("EEPROM Read Error\n");
5574         return -E1000_ERR_EEPROM;
5575     }
5576     /* Save word 0 in upper half of part_num */
5577     *part_num = (uint32_t) (eeprom_data << 16);
5578
5579     /* Get word 1 from EEPROM */
5580     if(e1000_read_eeprom(hw, ++offset, 1, &eeprom_data) < 0) {
5581         DEBUGOUT("EEPROM Read Error\n");
5582         return -E1000_ERR_EEPROM;
5583     }
5584     /* Save word 1 in lower half of part_num */
5585     *part_num |= eeprom_data;
5586
5587     return E1000_SUCCESS;
5588 }
5589
5590 /******************************************************************************
5591  * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
5592  * second function of dual function devices
5593  *
5594  * hw - Struct containing variables accessed by shared code
5595  *****************************************************************************/
5596 int32_t
5597 e1000_read_mac_addr(struct e1000_hw * hw)
5598 {
5599     uint16_t offset;
5600     uint16_t eeprom_data, i;
5601
5602     DEBUGFUNC("e1000_read_mac_addr");
5603
5604     for(i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
5605         offset = i >> 1;
5606         if(e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
5607             DEBUGOUT("EEPROM Read Error\n");
5608             return -E1000_ERR_EEPROM;
5609         }
5610         hw->perm_mac_addr[i] = (uint8_t) (eeprom_data & 0x00FF);
5611         hw->perm_mac_addr[i+1] = (uint8_t) (eeprom_data >> 8);
5612     }
5613
5614     switch (hw->mac_type) {
5615     default:
5616         break;
5617     case e1000_82546:
5618     case e1000_82546_rev_3:
5619     case e1000_82571:
5620     case e1000_80003es2lan:
5621         if(E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
5622             hw->perm_mac_addr[5] ^= 0x01;
5623         break;
5624     }
5625
5626     for(i = 0; i < NODE_ADDRESS_SIZE; i++)
5627         hw->mac_addr[i] = hw->perm_mac_addr[i];
5628     return E1000_SUCCESS;
5629 }
5630
5631 /******************************************************************************
5632  * Initializes receive address filters.
5633  *
5634  * hw - Struct containing variables accessed by shared code
5635  *
5636  * Places the MAC address in receive address register 0 and clears the rest
5637  * of the receive addresss registers. Clears the multicast table. Assumes
5638  * the receiver is in reset when the routine is called.
5639  *****************************************************************************/
5640 static void
5641 e1000_init_rx_addrs(struct e1000_hw *hw)
5642 {
5643     uint32_t i;
5644     uint32_t rar_num;
5645
5646     DEBUGFUNC("e1000_init_rx_addrs");
5647
5648     /* Setup the receive address. */
5649     DEBUGOUT("Programming MAC Address into RAR[0]\n");
5650
5651     e1000_rar_set(hw, hw->mac_addr, 0);
5652
5653     rar_num = E1000_RAR_ENTRIES;
5654
5655     /* Reserve a spot for the Locally Administered Address to work around
5656      * an 82571 issue in which a reset on one port will reload the MAC on
5657      * the other port. */
5658     if ((hw->mac_type == e1000_82571) && (hw->laa_is_present == TRUE))
5659         rar_num -= 1;
5660     if (hw->mac_type == e1000_ich8lan)
5661         rar_num = E1000_RAR_ENTRIES_ICH8LAN;
5662
5663     /* Zero out the other 15 receive addresses. */
5664     DEBUGOUT("Clearing RAR[1-15]\n");
5665     for(i = 1; i < rar_num; i++) {
5666         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
5667         E1000_WRITE_FLUSH(hw);
5668         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
5669         E1000_WRITE_FLUSH(hw);
5670     }
5671 }
5672
5673 /******************************************************************************
5674  * Updates the MAC's list of multicast addresses.
5675  *
5676  * hw - Struct containing variables accessed by shared code
5677  * mc_addr_list - the list of new multicast addresses
5678  * mc_addr_count - number of addresses
5679  * pad - number of bytes between addresses in the list
5680  * rar_used_count - offset where to start adding mc addresses into the RAR's
5681  *
5682  * The given list replaces any existing list. Clears the last 15 receive
5683  * address registers and the multicast table. Uses receive address registers
5684  * for the first 15 multicast addresses, and hashes the rest into the
5685  * multicast table.
5686  *****************************************************************************/
5687 #if 0
5688 void
5689 e1000_mc_addr_list_update(struct e1000_hw *hw,
5690                           uint8_t *mc_addr_list,
5691                           uint32_t mc_addr_count,
5692                           uint32_t pad,
5693                           uint32_t rar_used_count)
5694 {
5695     uint32_t hash_value;
5696     uint32_t i;
5697     uint32_t num_rar_entry;
5698     uint32_t num_mta_entry;
5699
5700     DEBUGFUNC("e1000_mc_addr_list_update");
5701
5702     /* Set the new number of MC addresses that we are being requested to use. */
5703     hw->num_mc_addrs = mc_addr_count;
5704
5705     /* Clear RAR[1-15] */
5706     DEBUGOUT(" Clearing RAR[1-15]\n");
5707     num_rar_entry = E1000_RAR_ENTRIES;
5708     if (hw->mac_type == e1000_ich8lan)
5709         num_rar_entry = E1000_RAR_ENTRIES_ICH8LAN;
5710     /* Reserve a spot for the Locally Administered Address to work around
5711      * an 82571 issue in which a reset on one port will reload the MAC on
5712      * the other port. */
5713     if ((hw->mac_type == e1000_82571) && (hw->laa_is_present == TRUE))
5714         num_rar_entry -= 1;
5715
5716     for(i = rar_used_count; i < num_rar_entry; i++) {
5717         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
5718         E1000_WRITE_FLUSH(hw);
5719         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
5720         E1000_WRITE_FLUSH(hw);
5721     }
5722
5723     /* Clear the MTA */
5724     DEBUGOUT(" Clearing MTA\n");
5725     num_mta_entry = E1000_NUM_MTA_REGISTERS;
5726     if (hw->mac_type == e1000_ich8lan)
5727         num_mta_entry = E1000_NUM_MTA_REGISTERS_ICH8LAN;
5728     for(i = 0; i < num_mta_entry; i++) {
5729         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
5730         E1000_WRITE_FLUSH(hw);
5731     }
5732
5733     /* Add the new addresses */
5734     for(i = 0; i < mc_addr_count; i++) {
5735         DEBUGOUT(" Adding the multicast addresses:\n");
5736         DEBUGOUT7(" MC Addr #%d =%.2X %.2X %.2X %.2X %.2X %.2X\n", i,
5737                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad)],
5738                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 1],
5739                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 2],
5740                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 3],
5741                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 4],
5742                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 5]);
5743
5744         hash_value = e1000_hash_mc_addr(hw,
5745                                         mc_addr_list +
5746                                         (i * (ETH_LENGTH_OF_ADDRESS + pad)));
5747
5748         DEBUGOUT1(" Hash value = 0x%03X\n", hash_value);
5749
5750         /* Place this multicast address in the RAR if there is room, *
5751          * else put it in the MTA
5752          */
5753         if (rar_used_count < num_rar_entry) {
5754             e1000_rar_set(hw,
5755                           mc_addr_list + (i * (ETH_LENGTH_OF_ADDRESS + pad)),
5756                           rar_used_count);
5757             rar_used_count++;
5758         } else {
5759             e1000_mta_set(hw, hash_value);
5760         }
5761     }
5762     DEBUGOUT("MC Update Complete\n");
5763 }
5764 #endif  /*  0  */
5765
5766 /******************************************************************************
5767  * Hashes an address to determine its location in the multicast table
5768  *
5769  * hw - Struct containing variables accessed by shared code
5770  * mc_addr - the multicast address to hash
5771  *****************************************************************************/
5772 uint32_t
5773 e1000_hash_mc_addr(struct e1000_hw *hw,
5774                    uint8_t *mc_addr)
5775 {
5776     uint32_t hash_value = 0;
5777
5778     /* The portion of the address that is used for the hash table is
5779      * determined by the mc_filter_type setting.
5780      */
5781     switch (hw->mc_filter_type) {
5782     /* [0] [1] [2] [3] [4] [5]
5783      * 01  AA  00  12  34  56
5784      * LSB                 MSB
5785      */
5786     case 0:
5787         if (hw->mac_type == e1000_ich8lan) {
5788             /* [47:38] i.e. 0x158 for above example address */
5789             hash_value = ((mc_addr[4] >> 6) | (((uint16_t) mc_addr[5]) << 2));
5790         } else {
5791             /* [47:36] i.e. 0x563 for above example address */
5792             hash_value = ((mc_addr[4] >> 4) | (((uint16_t) mc_addr[5]) << 4));
5793         }
5794         break;
5795     case 1:
5796         if (hw->mac_type == e1000_ich8lan) {
5797             /* [46:37] i.e. 0x2B1 for above example address */
5798             hash_value = ((mc_addr[4] >> 5) | (((uint16_t) mc_addr[5]) << 3));
5799         } else {
5800             /* [46:35] i.e. 0xAC6 for above example address */
5801             hash_value = ((mc_addr[4] >> 3) | (((uint16_t) mc_addr[5]) << 5));
5802         }
5803         break;
5804     case 2:
5805         if (hw->mac_type == e1000_ich8lan) {
5806             /*[45:36] i.e. 0x163 for above example address */
5807             hash_value = ((mc_addr[4] >> 4) | (((uint16_t) mc_addr[5]) << 4));
5808         } else {
5809             /* [45:34] i.e. 0x5D8 for above example address */
5810             hash_value = ((mc_addr[4] >> 2) | (((uint16_t) mc_addr[5]) << 6));
5811         }
5812         break;
5813     case 3:
5814         if (hw->mac_type == e1000_ich8lan) {
5815             /* [43:34] i.e. 0x18D for above example address */
5816             hash_value = ((mc_addr[4] >> 2) | (((uint16_t) mc_addr[5]) << 6));
5817         } else {
5818             /* [43:32] i.e. 0x634 for above example address */
5819             hash_value = ((mc_addr[4]) | (((uint16_t) mc_addr[5]) << 8));
5820         }
5821         break;
5822     }
5823
5824     hash_value &= 0xFFF;
5825     if (hw->mac_type == e1000_ich8lan)
5826         hash_value &= 0x3FF;
5827
5828     return hash_value;
5829 }
5830
5831 /******************************************************************************
5832  * Sets the bit in the multicast table corresponding to the hash value.
5833  *
5834  * hw - Struct containing variables accessed by shared code
5835  * hash_value - Multicast address hash value
5836  *****************************************************************************/
5837 void
5838 e1000_mta_set(struct e1000_hw *hw,
5839               uint32_t hash_value)
5840 {
5841     uint32_t hash_bit, hash_reg;
5842     uint32_t mta;
5843     uint32_t temp;
5844
5845     /* The MTA is a register array of 128 32-bit registers.
5846      * It is treated like an array of 4096 bits.  We want to set
5847      * bit BitArray[hash_value]. So we figure out what register
5848      * the bit is in, read it, OR in the new bit, then write
5849      * back the new value.  The register is determined by the
5850      * upper 7 bits of the hash value and the bit within that
5851      * register are determined by the lower 5 bits of the value.
5852      */
5853     hash_reg = (hash_value >> 5) & 0x7F;
5854     if (hw->mac_type == e1000_ich8lan)
5855         hash_reg &= 0x1F;
5856     hash_bit = hash_value & 0x1F;
5857
5858     mta = E1000_READ_REG_ARRAY(hw, MTA, hash_reg);
5859
5860     mta |= (1 << hash_bit);
5861
5862     /* If we are on an 82544 and we are trying to write an odd offset
5863      * in the MTA, save off the previous entry before writing and
5864      * restore the old value after writing.
5865      */
5866     if((hw->mac_type == e1000_82544) && ((hash_reg & 0x1) == 1)) {
5867         temp = E1000_READ_REG_ARRAY(hw, MTA, (hash_reg - 1));
5868         E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
5869         E1000_WRITE_FLUSH(hw);
5870         E1000_WRITE_REG_ARRAY(hw, MTA, (hash_reg - 1), temp);
5871         E1000_WRITE_FLUSH(hw);
5872     } else {
5873         E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
5874         E1000_WRITE_FLUSH(hw);
5875     }
5876 }
5877
5878 /******************************************************************************
5879  * Puts an ethernet address into a receive address register.
5880  *
5881  * hw - Struct containing variables accessed by shared code
5882  * addr - Address to put into receive address register
5883  * index - Receive address register to write
5884  *****************************************************************************/
5885 void
5886 e1000_rar_set(struct e1000_hw *hw,
5887               uint8_t *addr,
5888               uint32_t index)
5889 {
5890     uint32_t rar_low, rar_high;
5891
5892     /* HW expects these in little endian so we reverse the byte order
5893      * from network order (big endian) to little endian
5894      */
5895     rar_low = ((uint32_t) addr[0] |
5896                ((uint32_t) addr[1] << 8) |
5897                ((uint32_t) addr[2] << 16) | ((uint32_t) addr[3] << 24));
5898     rar_high = ((uint32_t) addr[4] | ((uint32_t) addr[5] << 8));
5899
5900     /* Disable Rx and flush all Rx frames before enabling RSS to avoid Rx
5901      * unit hang.
5902      *
5903      * Description:
5904      * If there are any Rx frames queued up or otherwise present in the HW
5905      * before RSS is enabled, and then we enable RSS, the HW Rx unit will
5906      * hang.  To work around this issue, we have to disable receives and
5907      * flush out all Rx frames before we enable RSS. To do so, we modify we
5908      * redirect all Rx traffic to manageability and then reset the HW.
5909      * This flushes away Rx frames, and (since the redirections to
5910      * manageability persists across resets) keeps new ones from coming in
5911      * while we work.  Then, we clear the Address Valid AV bit for all MAC
5912      * addresses and undo the re-direction to manageability.
5913      * Now, frames are coming in again, but the MAC won't accept them, so
5914      * far so good.  We now proceed to initialize RSS (if necessary) and
5915      * configure the Rx unit.  Last, we re-enable the AV bits and continue
5916      * on our merry way.
5917      */
5918     switch (hw->mac_type) {
5919     case e1000_82571:
5920     case e1000_82572:
5921     case e1000_80003es2lan:
5922         if (hw->leave_av_bit_off == TRUE)
5923             break;
5924     default:
5925         /* Indicate to hardware the Address is Valid. */
5926         rar_high |= E1000_RAH_AV;
5927         break;
5928     }
5929
5930     E1000_WRITE_REG_ARRAY(hw, RA, (index << 1), rar_low);
5931     E1000_WRITE_FLUSH(hw);
5932     E1000_WRITE_REG_ARRAY(hw, RA, ((index << 1) + 1), rar_high);
5933     E1000_WRITE_FLUSH(hw);
5934 }
5935
5936 /******************************************************************************
5937  * Writes a value to the specified offset in the VLAN filter table.
5938  *
5939  * hw - Struct containing variables accessed by shared code
5940  * offset - Offset in VLAN filer table to write
5941  * value - Value to write into VLAN filter table
5942  *****************************************************************************/
5943 void
5944 e1000_write_vfta(struct e1000_hw *hw,
5945                  uint32_t offset,
5946                  uint32_t value)
5947 {
5948     uint32_t temp;
5949
5950     if (hw->mac_type == e1000_ich8lan)
5951         return;
5952
5953     if ((hw->mac_type == e1000_82544) && ((offset & 0x1) == 1)) {
5954         temp = E1000_READ_REG_ARRAY(hw, VFTA, (offset - 1));
5955         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
5956         E1000_WRITE_FLUSH(hw);
5957         E1000_WRITE_REG_ARRAY(hw, VFTA, (offset - 1), temp);
5958         E1000_WRITE_FLUSH(hw);
5959     } else {
5960         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
5961         E1000_WRITE_FLUSH(hw);
5962     }
5963 }
5964
5965 /******************************************************************************
5966  * Clears the VLAN filer table
5967  *
5968  * hw - Struct containing variables accessed by shared code
5969  *****************************************************************************/
5970 static void
5971 e1000_clear_vfta(struct e1000_hw *hw)
5972 {
5973     uint32_t offset;
5974     uint32_t vfta_value = 0;
5975     uint32_t vfta_offset = 0;
5976     uint32_t vfta_bit_in_reg = 0;
5977
5978     if (hw->mac_type == e1000_ich8lan)
5979         return;
5980
5981     if (hw->mac_type == e1000_82573) {
5982         if (hw->mng_cookie.vlan_id != 0) {
5983             /* The VFTA is a 4096b bit-field, each identifying a single VLAN
5984              * ID.  The following operations determine which 32b entry
5985              * (i.e. offset) into the array we want to set the VLAN ID
5986              * (i.e. bit) of the manageability unit. */
5987             vfta_offset = (hw->mng_cookie.vlan_id >>
5988                            E1000_VFTA_ENTRY_SHIFT) &
5989                           E1000_VFTA_ENTRY_MASK;
5990             vfta_bit_in_reg = 1 << (hw->mng_cookie.vlan_id &
5991                                     E1000_VFTA_ENTRY_BIT_SHIFT_MASK);
5992         }
5993     }
5994     for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
5995         /* If the offset we want to clear is the same offset of the
5996          * manageability VLAN ID, then clear all bits except that of the
5997          * manageability unit */
5998         vfta_value = (offset == vfta_offset) ? vfta_bit_in_reg : 0;
5999         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, vfta_value);
6000         E1000_WRITE_FLUSH(hw);
6001     }
6002 }
6003
6004 static int32_t
6005 e1000_id_led_init(struct e1000_hw * hw)
6006 {
6007     uint32_t ledctl;
6008     const uint32_t ledctl_mask = 0x000000FF;
6009     const uint32_t ledctl_on = E1000_LEDCTL_MODE_LED_ON;
6010     const uint32_t ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
6011     uint16_t eeprom_data, i, temp;
6012     const uint16_t led_mask = 0x0F;
6013
6014     DEBUGFUNC("e1000_id_led_init");
6015
6016     if(hw->mac_type < e1000_82540) {
6017         /* Nothing to do */
6018         return E1000_SUCCESS;
6019     }
6020
6021     ledctl = E1000_READ_REG(hw, LEDCTL);
6022     hw->ledctl_default = ledctl;
6023     hw->ledctl_mode1 = hw->ledctl_default;
6024     hw->ledctl_mode2 = hw->ledctl_default;
6025
6026     if(e1000_read_eeprom(hw, EEPROM_ID_LED_SETTINGS, 1, &eeprom_data) < 0) {
6027         DEBUGOUT("EEPROM Read Error\n");
6028         return -E1000_ERR_EEPROM;
6029     }
6030
6031     if ((hw->mac_type == e1000_82573) &&
6032         (eeprom_data == ID_LED_RESERVED_82573))
6033         eeprom_data = ID_LED_DEFAULT_82573;
6034     else if ((eeprom_data == ID_LED_RESERVED_0000) ||
6035             (eeprom_data == ID_LED_RESERVED_FFFF)) {
6036         if (hw->mac_type == e1000_ich8lan)
6037             eeprom_data = ID_LED_DEFAULT_ICH8LAN;
6038         else
6039             eeprom_data = ID_LED_DEFAULT;
6040     }
6041     for (i = 0; i < 4; i++) {
6042         temp = (eeprom_data >> (i << 2)) & led_mask;
6043         switch(temp) {
6044         case ID_LED_ON1_DEF2:
6045         case ID_LED_ON1_ON2:
6046         case ID_LED_ON1_OFF2:
6047             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
6048             hw->ledctl_mode1 |= ledctl_on << (i << 3);
6049             break;
6050         case ID_LED_OFF1_DEF2:
6051         case ID_LED_OFF1_ON2:
6052         case ID_LED_OFF1_OFF2:
6053             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
6054             hw->ledctl_mode1 |= ledctl_off << (i << 3);
6055             break;
6056         default:
6057             /* Do nothing */
6058             break;
6059         }
6060         switch(temp) {
6061         case ID_LED_DEF1_ON2:
6062         case ID_LED_ON1_ON2:
6063         case ID_LED_OFF1_ON2:
6064             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
6065             hw->ledctl_mode2 |= ledctl_on << (i << 3);
6066             break;
6067         case ID_LED_DEF1_OFF2:
6068         case ID_LED_ON1_OFF2:
6069         case ID_LED_OFF1_OFF2:
6070             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
6071             hw->ledctl_mode2 |= ledctl_off << (i << 3);
6072             break;
6073         default:
6074             /* Do nothing */
6075             break;
6076         }
6077     }
6078     return E1000_SUCCESS;
6079 }
6080
6081 /******************************************************************************
6082  * Prepares SW controlable LED for use and saves the current state of the LED.
6083  *
6084  * hw - Struct containing variables accessed by shared code
6085  *****************************************************************************/
6086 int32_t
6087 e1000_setup_led(struct e1000_hw *hw)
6088 {
6089     uint32_t ledctl;
6090     int32_t ret_val = E1000_SUCCESS;
6091
6092     DEBUGFUNC("e1000_setup_led");
6093
6094     switch(hw->mac_type) {
6095     case e1000_82542_rev2_0:
6096     case e1000_82542_rev2_1:
6097     case e1000_82543:
6098     case e1000_82544:
6099         /* No setup necessary */
6100         break;
6101     case e1000_82541:
6102     case e1000_82547:
6103     case e1000_82541_rev_2:
6104     case e1000_82547_rev_2:
6105         /* Turn off PHY Smart Power Down (if enabled) */
6106         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
6107                                      &hw->phy_spd_default);
6108         if(ret_val)
6109             return ret_val;
6110         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
6111                                       (uint16_t)(hw->phy_spd_default &
6112                                       ~IGP01E1000_GMII_SPD));
6113         if(ret_val)
6114             return ret_val;
6115         /* Fall Through */
6116     default:
6117         if(hw->media_type == e1000_media_type_fiber) {
6118             ledctl = E1000_READ_REG(hw, LEDCTL);
6119             /* Save current LEDCTL settings */
6120             hw->ledctl_default = ledctl;
6121             /* Turn off LED0 */
6122             ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
6123                         E1000_LEDCTL_LED0_BLINK |
6124                         E1000_LEDCTL_LED0_MODE_MASK);
6125             ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
6126                        E1000_LEDCTL_LED0_MODE_SHIFT);
6127             E1000_WRITE_REG(hw, LEDCTL, ledctl);
6128         } else if(hw->media_type == e1000_media_type_copper)
6129             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
6130         break;
6131     }
6132
6133     return E1000_SUCCESS;
6134 }
6135
6136 /******************************************************************************
6137  * Used on 82571 and later Si that has LED blink bits.
6138  * Callers must use their own timer and should have already called
6139  * e1000_id_led_init()
6140  * Call e1000_cleanup led() to stop blinking
6141  *
6142  * hw - Struct containing variables accessed by shared code
6143  *****************************************************************************/
6144 int32_t
6145 e1000_blink_led_start(struct e1000_hw *hw)
6146 {
6147     int16_t  i;
6148     uint32_t ledctl_blink = 0;
6149
6150     DEBUGFUNC("e1000_id_led_blink_on");
6151
6152     if (hw->mac_type < e1000_82571) {
6153         /* Nothing to do */
6154         return E1000_SUCCESS;
6155     }
6156     if (hw->media_type == e1000_media_type_fiber) {
6157         /* always blink LED0 for PCI-E fiber */
6158         ledctl_blink = E1000_LEDCTL_LED0_BLINK |
6159                      (E1000_LEDCTL_MODE_LED_ON << E1000_LEDCTL_LED0_MODE_SHIFT);
6160     } else {
6161         /* set the blink bit for each LED that's "on" (0x0E) in ledctl_mode2 */
6162         ledctl_blink = hw->ledctl_mode2;
6163         for (i=0; i < 4; i++)
6164             if (((hw->ledctl_mode2 >> (i * 8)) & 0xFF) ==
6165                 E1000_LEDCTL_MODE_LED_ON)
6166                 ledctl_blink |= (E1000_LEDCTL_LED0_BLINK << (i * 8));
6167     }
6168
6169     E1000_WRITE_REG(hw, LEDCTL, ledctl_blink);
6170
6171     return E1000_SUCCESS;
6172 }
6173
6174 /******************************************************************************
6175  * Restores the saved state of the SW controlable LED.
6176  *
6177  * hw - Struct containing variables accessed by shared code
6178  *****************************************************************************/
6179 int32_t
6180 e1000_cleanup_led(struct e1000_hw *hw)
6181 {
6182     int32_t ret_val = E1000_SUCCESS;
6183
6184     DEBUGFUNC("e1000_cleanup_led");
6185
6186     switch(hw->mac_type) {
6187     case e1000_82542_rev2_0:
6188     case e1000_82542_rev2_1:
6189     case e1000_82543:
6190     case e1000_82544:
6191         /* No cleanup necessary */
6192         break;
6193     case e1000_82541:
6194     case e1000_82547:
6195     case e1000_82541_rev_2:
6196     case e1000_82547_rev_2:
6197         /* Turn on PHY Smart Power Down (if previously enabled) */
6198         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
6199                                       hw->phy_spd_default);
6200         if(ret_val)
6201             return ret_val;
6202         /* Fall Through */
6203     default:
6204         if (hw->phy_type == e1000_phy_ife) {
6205             e1000_write_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL_LED, 0);
6206             break;
6207         }
6208         /* Restore LEDCTL settings */
6209         E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_default);
6210         break;
6211     }
6212
6213     return E1000_SUCCESS;
6214 }
6215
6216 /******************************************************************************
6217  * Turns on the software controllable LED
6218  *
6219  * hw - Struct containing variables accessed by shared code
6220  *****************************************************************************/
6221 int32_t
6222 e1000_led_on(struct e1000_hw *hw)
6223 {
6224     uint32_t ctrl = E1000_READ_REG(hw, CTRL);
6225
6226     DEBUGFUNC("e1000_led_on");
6227
6228     switch(hw->mac_type) {
6229     case e1000_82542_rev2_0:
6230     case e1000_82542_rev2_1:
6231     case e1000_82543:
6232         /* Set SW Defineable Pin 0 to turn on the LED */
6233         ctrl |= E1000_CTRL_SWDPIN0;
6234         ctrl |= E1000_CTRL_SWDPIO0;
6235         break;
6236     case e1000_82544:
6237         if(hw->media_type == e1000_media_type_fiber) {
6238             /* Set SW Defineable Pin 0 to turn on the LED */
6239             ctrl |= E1000_CTRL_SWDPIN0;
6240             ctrl |= E1000_CTRL_SWDPIO0;
6241         } else {
6242             /* Clear SW Defineable Pin 0 to turn on the LED */
6243             ctrl &= ~E1000_CTRL_SWDPIN0;
6244             ctrl |= E1000_CTRL_SWDPIO0;
6245         }
6246         break;
6247     default:
6248         if(hw->media_type == e1000_media_type_fiber) {
6249             /* Clear SW Defineable Pin 0 to turn on the LED */
6250             ctrl &= ~E1000_CTRL_SWDPIN0;
6251             ctrl |= E1000_CTRL_SWDPIO0;
6252         } else if (hw->phy_type == e1000_phy_ife) {
6253             e1000_write_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL_LED,
6254                  (IFE_PSCL_PROBE_MODE | IFE_PSCL_PROBE_LEDS_ON));
6255         } else if (hw->media_type == e1000_media_type_copper) {
6256             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode2);
6257             return E1000_SUCCESS;
6258         }
6259         break;
6260     }
6261
6262     E1000_WRITE_REG(hw, CTRL, ctrl);
6263
6264     return E1000_SUCCESS;
6265 }
6266
6267 /******************************************************************************
6268  * Turns off the software controllable LED
6269  *
6270  * hw - Struct containing variables accessed by shared code
6271  *****************************************************************************/
6272 int32_t
6273 e1000_led_off(struct e1000_hw *hw)
6274 {
6275     uint32_t ctrl = E1000_READ_REG(hw, CTRL);
6276
6277     DEBUGFUNC("e1000_led_off");
6278
6279     switch(hw->mac_type) {
6280     case e1000_82542_rev2_0:
6281     case e1000_82542_rev2_1:
6282     case e1000_82543:
6283         /* Clear SW Defineable Pin 0 to turn off the LED */
6284         ctrl &= ~E1000_CTRL_SWDPIN0;
6285         ctrl |= E1000_CTRL_SWDPIO0;
6286         break;
6287     case e1000_82544:
6288         if(hw->media_type == e1000_media_type_fiber) {
6289             /* Clear SW Defineable Pin 0 to turn off the LED */
6290             ctrl &= ~E1000_CTRL_SWDPIN0;
6291             ctrl |= E1000_CTRL_SWDPIO0;
6292         } else {
6293             /* Set SW Defineable Pin 0 to turn off the LED */
6294             ctrl |= E1000_CTRL_SWDPIN0;
6295             ctrl |= E1000_CTRL_SWDPIO0;
6296         }
6297         break;
6298     default:
6299         if(hw->media_type == e1000_media_type_fiber) {
6300             /* Set SW Defineable Pin 0 to turn off the LED */
6301             ctrl |= E1000_CTRL_SWDPIN0;
6302             ctrl |= E1000_CTRL_SWDPIO0;
6303         } else if (hw->phy_type == e1000_phy_ife) {
6304             e1000_write_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL_LED,
6305                  (IFE_PSCL_PROBE_MODE | IFE_PSCL_PROBE_LEDS_OFF));
6306         } else if (hw->media_type == e1000_media_type_copper) {
6307             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
6308             return E1000_SUCCESS;
6309         }
6310         break;
6311     }
6312
6313     E1000_WRITE_REG(hw, CTRL, ctrl);
6314
6315     return E1000_SUCCESS;
6316 }
6317
6318 /******************************************************************************
6319  * Clears all hardware statistics counters.
6320  *
6321  * hw - Struct containing variables accessed by shared code
6322  *****************************************************************************/
6323 static void
6324 e1000_clear_hw_cntrs(struct e1000_hw *hw)
6325 {
6326     volatile uint32_t temp;
6327
6328     temp = E1000_READ_REG(hw, CRCERRS);
6329     temp = E1000_READ_REG(hw, SYMERRS);
6330     temp = E1000_READ_REG(hw, MPC);
6331     temp = E1000_READ_REG(hw, SCC);
6332     temp = E1000_READ_REG(hw, ECOL);
6333     temp = E1000_READ_REG(hw, MCC);
6334     temp = E1000_READ_REG(hw, LATECOL);
6335     temp = E1000_READ_REG(hw, COLC);
6336     temp = E1000_READ_REG(hw, DC);
6337     temp = E1000_READ_REG(hw, SEC);
6338     temp = E1000_READ_REG(hw, RLEC);
6339     temp = E1000_READ_REG(hw, XONRXC);
6340     temp = E1000_READ_REG(hw, XONTXC);
6341     temp = E1000_READ_REG(hw, XOFFRXC);
6342     temp = E1000_READ_REG(hw, XOFFTXC);
6343     temp = E1000_READ_REG(hw, FCRUC);
6344
6345     if (hw->mac_type != e1000_ich8lan) {
6346     temp = E1000_READ_REG(hw, PRC64);
6347     temp = E1000_READ_REG(hw, PRC127);
6348     temp = E1000_READ_REG(hw, PRC255);
6349     temp = E1000_READ_REG(hw, PRC511);
6350     temp = E1000_READ_REG(hw, PRC1023);
6351     temp = E1000_READ_REG(hw, PRC1522);
6352     }
6353
6354     temp = E1000_READ_REG(hw, GPRC);
6355     temp = E1000_READ_REG(hw, BPRC);
6356     temp = E1000_READ_REG(hw, MPRC);
6357     temp = E1000_READ_REG(hw, GPTC);
6358     temp = E1000_READ_REG(hw, GORCL);
6359     temp = E1000_READ_REG(hw, GORCH);
6360     temp = E1000_READ_REG(hw, GOTCL);
6361     temp = E1000_READ_REG(hw, GOTCH);
6362     temp = E1000_READ_REG(hw, RNBC);
6363     temp = E1000_READ_REG(hw, RUC);
6364     temp = E1000_READ_REG(hw, RFC);
6365     temp = E1000_READ_REG(hw, ROC);
6366     temp = E1000_READ_REG(hw, RJC);
6367     temp = E1000_READ_REG(hw, TORL);
6368     temp = E1000_READ_REG(hw, TORH);
6369     temp = E1000_READ_REG(hw, TOTL);
6370     temp = E1000_READ_REG(hw, TOTH);
6371     temp = E1000_READ_REG(hw, TPR);
6372     temp = E1000_READ_REG(hw, TPT);
6373
6374     if (hw->mac_type != e1000_ich8lan) {
6375     temp = E1000_READ_REG(hw, PTC64);
6376     temp = E1000_READ_REG(hw, PTC127);
6377     temp = E1000_READ_REG(hw, PTC255);
6378     temp = E1000_READ_REG(hw, PTC511);
6379     temp = E1000_READ_REG(hw, PTC1023);
6380     temp = E1000_READ_REG(hw, PTC1522);
6381     }
6382
6383     temp = E1000_READ_REG(hw, MPTC);
6384     temp = E1000_READ_REG(hw, BPTC);
6385
6386     if(hw->mac_type < e1000_82543) return;
6387
6388     temp = E1000_READ_REG(hw, ALGNERRC);
6389     temp = E1000_READ_REG(hw, RXERRC);
6390     temp = E1000_READ_REG(hw, TNCRS);
6391     temp = E1000_READ_REG(hw, CEXTERR);
6392     temp = E1000_READ_REG(hw, TSCTC);
6393     temp = E1000_READ_REG(hw, TSCTFC);
6394
6395     if(hw->mac_type <= e1000_82544) return;
6396
6397     temp = E1000_READ_REG(hw, MGTPRC);
6398     temp = E1000_READ_REG(hw, MGTPDC);
6399     temp = E1000_READ_REG(hw, MGTPTC);
6400
6401     if(hw->mac_type <= e1000_82547_rev_2) return;
6402
6403     temp = E1000_READ_REG(hw, IAC);
6404     temp = E1000_READ_REG(hw, ICRXOC);
6405
6406     if (hw->mac_type == e1000_ich8lan) return;
6407
6408     temp = E1000_READ_REG(hw, ICRXPTC);
6409     temp = E1000_READ_REG(hw, ICRXATC);
6410     temp = E1000_READ_REG(hw, ICTXPTC);
6411     temp = E1000_READ_REG(hw, ICTXATC);
6412     temp = E1000_READ_REG(hw, ICTXQEC);
6413     temp = E1000_READ_REG(hw, ICTXQMTC);
6414     temp = E1000_READ_REG(hw, ICRXDMTC);
6415 }
6416
6417 /******************************************************************************
6418  * Resets Adaptive IFS to its default state.
6419  *
6420  * hw - Struct containing variables accessed by shared code
6421  *
6422  * Call this after e1000_init_hw. You may override the IFS defaults by setting
6423  * hw->ifs_params_forced to TRUE. However, you must initialize hw->
6424  * current_ifs_val, ifs_min_val, ifs_max_val, ifs_step_size, and ifs_ratio
6425  * before calling this function.
6426  *****************************************************************************/
6427 void
6428 e1000_reset_adaptive(struct e1000_hw *hw)
6429 {
6430     DEBUGFUNC("e1000_reset_adaptive");
6431
6432     if(hw->adaptive_ifs) {
6433         if(!hw->ifs_params_forced) {
6434             hw->current_ifs_val = 0;
6435             hw->ifs_min_val = IFS_MIN;
6436             hw->ifs_max_val = IFS_MAX;
6437             hw->ifs_step_size = IFS_STEP;
6438             hw->ifs_ratio = IFS_RATIO;
6439         }
6440         hw->in_ifs_mode = FALSE;
6441         E1000_WRITE_REG(hw, AIT, 0);
6442     } else {
6443         DEBUGOUT("Not in Adaptive IFS mode!\n");
6444     }
6445 }
6446
6447 /******************************************************************************
6448  * Called during the callback/watchdog routine to update IFS value based on
6449  * the ratio of transmits to collisions.
6450  *
6451  * hw - Struct containing variables accessed by shared code
6452  * tx_packets - Number of transmits since last callback
6453  * total_collisions - Number of collisions since last callback
6454  *****************************************************************************/
6455 void
6456 e1000_update_adaptive(struct e1000_hw *hw)
6457 {
6458     DEBUGFUNC("e1000_update_adaptive");
6459
6460     if(hw->adaptive_ifs) {
6461         if((hw->collision_delta * hw->ifs_ratio) > hw->tx_packet_delta) {
6462             if(hw->tx_packet_delta > MIN_NUM_XMITS) {
6463                 hw->in_ifs_mode = TRUE;
6464                 if(hw->current_ifs_val < hw->ifs_max_val) {
6465                     if(hw->current_ifs_val == 0)
6466                         hw->current_ifs_val = hw->ifs_min_val;
6467                     else
6468                         hw->current_ifs_val += hw->ifs_step_size;
6469                     E1000_WRITE_REG(hw, AIT, hw->current_ifs_val);
6470                 }
6471             }
6472         } else {
6473             if(hw->in_ifs_mode && (hw->tx_packet_delta <= MIN_NUM_XMITS)) {
6474                 hw->current_ifs_val = 0;
6475                 hw->in_ifs_mode = FALSE;
6476                 E1000_WRITE_REG(hw, AIT, 0);
6477             }
6478         }
6479     } else {
6480         DEBUGOUT("Not in Adaptive IFS mode!\n");
6481     }
6482 }
6483
6484 /******************************************************************************
6485  * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
6486  *
6487  * hw - Struct containing variables accessed by shared code
6488  * frame_len - The length of the frame in question
6489  * mac_addr - The Ethernet destination address of the frame in question
6490  *****************************************************************************/
6491 void
6492 e1000_tbi_adjust_stats(struct e1000_hw *hw,
6493                        struct e1000_hw_stats *stats,
6494                        uint32_t frame_len,
6495                        uint8_t *mac_addr)
6496 {
6497     uint64_t carry_bit;
6498
6499     /* First adjust the frame length. */
6500     frame_len--;
6501     /* We need to adjust the statistics counters, since the hardware
6502      * counters overcount this packet as a CRC error and undercount
6503      * the packet as a good packet
6504      */
6505     /* This packet should not be counted as a CRC error.    */
6506     stats->crcerrs--;
6507     /* This packet does count as a Good Packet Received.    */
6508     stats->gprc++;
6509
6510     /* Adjust the Good Octets received counters             */
6511     carry_bit = 0x80000000 & stats->gorcl;
6512     stats->gorcl += frame_len;
6513     /* If the high bit of Gorcl (the low 32 bits of the Good Octets
6514      * Received Count) was one before the addition,
6515      * AND it is zero after, then we lost the carry out,
6516      * need to add one to Gorch (Good Octets Received Count High).
6517      * This could be simplified if all environments supported
6518      * 64-bit integers.
6519      */
6520     if(carry_bit && ((stats->gorcl & 0x80000000) == 0))
6521         stats->gorch++;
6522     /* Is this a broadcast or multicast?  Check broadcast first,
6523      * since the test for a multicast frame will test positive on
6524      * a broadcast frame.
6525      */
6526     if((mac_addr[0] == (uint8_t) 0xff) && (mac_addr[1] == (uint8_t) 0xff))
6527         /* Broadcast packet */
6528         stats->bprc++;
6529     else if(*mac_addr & 0x01)
6530         /* Multicast packet */
6531         stats->mprc++;
6532
6533     if(frame_len == hw->max_frame_size) {
6534         /* In this case, the hardware has overcounted the number of
6535          * oversize frames.
6536          */
6537         if(stats->roc > 0)
6538             stats->roc--;
6539     }
6540
6541     /* Adjust the bin counters when the extra byte put the frame in the
6542      * wrong bin. Remember that the frame_len was adjusted above.
6543      */
6544     if(frame_len == 64) {
6545         stats->prc64++;
6546         stats->prc127--;
6547     } else if(frame_len == 127) {
6548         stats->prc127++;
6549         stats->prc255--;
6550     } else if(frame_len == 255) {
6551         stats->prc255++;
6552         stats->prc511--;
6553     } else if(frame_len == 511) {
6554         stats->prc511++;
6555         stats->prc1023--;
6556     } else if(frame_len == 1023) {
6557         stats->prc1023++;
6558         stats->prc1522--;
6559     } else if(frame_len == 1522) {
6560         stats->prc1522++;
6561     }
6562 }
6563
6564 /******************************************************************************
6565  * Gets the current PCI bus type, speed, and width of the hardware
6566  *
6567  * hw - Struct containing variables accessed by shared code
6568  *****************************************************************************/
6569 void
6570 e1000_get_bus_info(struct e1000_hw *hw)
6571 {
6572     uint32_t status;
6573
6574     switch (hw->mac_type) {
6575     case e1000_82542_rev2_0:
6576     case e1000_82542_rev2_1:
6577         hw->bus_type = e1000_bus_type_unknown;
6578         hw->bus_speed = e1000_bus_speed_unknown;
6579         hw->bus_width = e1000_bus_width_unknown;
6580         break;
6581     case e1000_82572:
6582     case e1000_82573:
6583         hw->bus_type = e1000_bus_type_pci_express;
6584         hw->bus_speed = e1000_bus_speed_2500;
6585         hw->bus_width = e1000_bus_width_pciex_1;
6586         break;
6587     case e1000_82571:
6588     case e1000_ich8lan:
6589     case e1000_80003es2lan:
6590         hw->bus_type = e1000_bus_type_pci_express;
6591         hw->bus_speed = e1000_bus_speed_2500;
6592         hw->bus_width = e1000_bus_width_pciex_4;
6593         break;
6594     default:
6595         status = E1000_READ_REG(hw, STATUS);
6596         hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
6597                        e1000_bus_type_pcix : e1000_bus_type_pci;
6598
6599         if(hw->device_id == E1000_DEV_ID_82546EB_QUAD_COPPER) {
6600             hw->bus_speed = (hw->bus_type == e1000_bus_type_pci) ?
6601                             e1000_bus_speed_66 : e1000_bus_speed_120;
6602         } else if(hw->bus_type == e1000_bus_type_pci) {
6603             hw->bus_speed = (status & E1000_STATUS_PCI66) ?
6604                             e1000_bus_speed_66 : e1000_bus_speed_33;
6605         } else {
6606             switch (status & E1000_STATUS_PCIX_SPEED) {
6607             case E1000_STATUS_PCIX_SPEED_66:
6608                 hw->bus_speed = e1000_bus_speed_66;
6609                 break;
6610             case E1000_STATUS_PCIX_SPEED_100:
6611                 hw->bus_speed = e1000_bus_speed_100;
6612                 break;
6613             case E1000_STATUS_PCIX_SPEED_133:
6614                 hw->bus_speed = e1000_bus_speed_133;
6615                 break;
6616             default:
6617                 hw->bus_speed = e1000_bus_speed_reserved;
6618                 break;
6619             }
6620         }
6621         hw->bus_width = (status & E1000_STATUS_BUS64) ?
6622                         e1000_bus_width_64 : e1000_bus_width_32;
6623         break;
6624     }
6625 }
6626 /******************************************************************************
6627  * Reads a value from one of the devices registers using port I/O (as opposed
6628  * memory mapped I/O). Only 82544 and newer devices support port I/O.
6629  *
6630  * hw - Struct containing variables accessed by shared code
6631  * offset - offset to read from
6632  *****************************************************************************/
6633 #if 0
6634 uint32_t
6635 e1000_read_reg_io(struct e1000_hw *hw,
6636                   uint32_t offset)
6637 {
6638     unsigned long io_addr = hw->io_base;
6639     unsigned long io_data = hw->io_base + 4;
6640
6641     e1000_io_write(hw, io_addr, offset);
6642     return e1000_io_read(hw, io_data);
6643 }
6644 #endif  /*  0  */
6645
6646 /******************************************************************************
6647  * Writes a value to one of the devices registers using port I/O (as opposed to
6648  * memory mapped I/O). Only 82544 and newer devices support port I/O.
6649  *
6650  * hw - Struct containing variables accessed by shared code
6651  * offset - offset to write to
6652  * value - value to write
6653  *****************************************************************************/
6654 static void
6655 e1000_write_reg_io(struct e1000_hw *hw,
6656                    uint32_t offset,
6657                    uint32_t value)
6658 {
6659     unsigned long io_addr = hw->io_base;
6660     unsigned long io_data = hw->io_base + 4;
6661
6662     e1000_io_write(hw, io_addr, offset);
6663     e1000_io_write(hw, io_data, value);
6664 }
6665
6666
6667 /******************************************************************************
6668  * Estimates the cable length.
6669  *
6670  * hw - Struct containing variables accessed by shared code
6671  * min_length - The estimated minimum length
6672  * max_length - The estimated maximum length
6673  *
6674  * returns: - E1000_ERR_XXX
6675  *            E1000_SUCCESS
6676  *
6677  * This function always returns a ranged length (minimum & maximum).
6678  * So for M88 phy's, this function interprets the one value returned from the
6679  * register to the minimum and maximum range.
6680  * For IGP phy's, the function calculates the range by the AGC registers.
6681  *****************************************************************************/
6682 static int32_t
6683 e1000_get_cable_length(struct e1000_hw *hw,
6684                        uint16_t *min_length,
6685                        uint16_t *max_length)
6686 {
6687     int32_t ret_val;
6688     uint16_t agc_value = 0;
6689     uint16_t i, phy_data;
6690     uint16_t cable_length;
6691
6692     DEBUGFUNC("e1000_get_cable_length");
6693
6694     *min_length = *max_length = 0;
6695
6696     /* Use old method for Phy older than IGP */
6697     if(hw->phy_type == e1000_phy_m88) {
6698
6699         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
6700                                      &phy_data);
6701         if(ret_val)
6702             return ret_val;
6703         cable_length = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
6704                        M88E1000_PSSR_CABLE_LENGTH_SHIFT;
6705
6706         /* Convert the enum value to ranged values */
6707         switch (cable_length) {
6708         case e1000_cable_length_50:
6709             *min_length = 0;
6710             *max_length = e1000_igp_cable_length_50;
6711             break;
6712         case e1000_cable_length_50_80:
6713             *min_length = e1000_igp_cable_length_50;
6714             *max_length = e1000_igp_cable_length_80;
6715             break;
6716         case e1000_cable_length_80_110:
6717             *min_length = e1000_igp_cable_length_80;
6718             *max_length = e1000_igp_cable_length_110;
6719             break;
6720         case e1000_cable_length_110_140:
6721             *min_length = e1000_igp_cable_length_110;
6722             *max_length = e1000_igp_cable_length_140;
6723             break;
6724         case e1000_cable_length_140:
6725             *min_length = e1000_igp_cable_length_140;
6726             *max_length = e1000_igp_cable_length_170;
6727             break;
6728         default:
6729             return -E1000_ERR_PHY;
6730             break;
6731         }
6732     } else if (hw->phy_type == e1000_phy_gg82563) {
6733         ret_val = e1000_read_phy_reg(hw, GG82563_PHY_DSP_DISTANCE,
6734                                      &phy_data);
6735         if (ret_val)
6736             return ret_val;
6737         cable_length = phy_data & GG82563_DSPD_CABLE_LENGTH;
6738
6739         switch (cable_length) {
6740         case e1000_gg_cable_length_60:
6741             *min_length = 0;
6742             *max_length = e1000_igp_cable_length_60;
6743             break;
6744         case e1000_gg_cable_length_60_115:
6745             *min_length = e1000_igp_cable_length_60;
6746             *max_length = e1000_igp_cable_length_115;
6747             break;
6748         case e1000_gg_cable_length_115_150:
6749             *min_length = e1000_igp_cable_length_115;
6750             *max_length = e1000_igp_cable_length_150;
6751             break;
6752         case e1000_gg_cable_length_150:
6753             *min_length = e1000_igp_cable_length_150;
6754             *max_length = e1000_igp_cable_length_180;
6755             break;
6756         default:
6757             return -E1000_ERR_PHY;
6758             break;
6759         }
6760     } else if(hw->phy_type == e1000_phy_igp) { /* For IGP PHY */
6761         uint16_t cur_agc_value;
6762         uint16_t min_agc_value = IGP01E1000_AGC_LENGTH_TABLE_SIZE;
6763         uint16_t agc_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
6764                                                          {IGP01E1000_PHY_AGC_A,
6765                                                           IGP01E1000_PHY_AGC_B,
6766                                                           IGP01E1000_PHY_AGC_C,
6767                                                           IGP01E1000_PHY_AGC_D};
6768         /* Read the AGC registers for all channels */
6769         for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
6770
6771             ret_val = e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
6772             if(ret_val)
6773                 return ret_val;
6774
6775             cur_agc_value = phy_data >> IGP01E1000_AGC_LENGTH_SHIFT;
6776
6777             /* Value bound check. */
6778             if ((cur_agc_value >= IGP01E1000_AGC_LENGTH_TABLE_SIZE - 1) ||
6779                 (cur_agc_value == 0))
6780                 return -E1000_ERR_PHY;
6781
6782             agc_value += cur_agc_value;
6783
6784             /* Update minimal AGC value. */
6785             if (min_agc_value > cur_agc_value)
6786                 min_agc_value = cur_agc_value;
6787         }
6788
6789         /* Remove the minimal AGC result for length < 50m */
6790         if (agc_value < IGP01E1000_PHY_CHANNEL_NUM * e1000_igp_cable_length_50) {
6791             agc_value -= min_agc_value;
6792
6793             /* Get the average length of the remaining 3 channels */
6794             agc_value /= (IGP01E1000_PHY_CHANNEL_NUM - 1);
6795         } else {
6796             /* Get the average length of all the 4 channels. */
6797             agc_value /= IGP01E1000_PHY_CHANNEL_NUM;
6798         }
6799
6800         /* Set the range of the calculated length. */
6801         *min_length = ((e1000_igp_cable_length_table[agc_value] -
6802                        IGP01E1000_AGC_RANGE) > 0) ?
6803                        (e1000_igp_cable_length_table[agc_value] -
6804                        IGP01E1000_AGC_RANGE) : 0;
6805         *max_length = e1000_igp_cable_length_table[agc_value] +
6806                       IGP01E1000_AGC_RANGE;
6807     } else if (hw->phy_type == e1000_phy_igp_2 ||
6808                hw->phy_type == e1000_phy_igp_3) {
6809         uint16_t cur_agc_index, max_agc_index = 0;
6810         uint16_t min_agc_index = IGP02E1000_AGC_LENGTH_TABLE_SIZE - 1;
6811         uint16_t agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] =
6812                                                          {IGP02E1000_PHY_AGC_A,
6813                                                           IGP02E1000_PHY_AGC_B,
6814                                                           IGP02E1000_PHY_AGC_C,
6815                                                           IGP02E1000_PHY_AGC_D};
6816         /* Read the AGC registers for all channels */
6817         for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
6818             ret_val = e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
6819             if (ret_val)
6820                 return ret_val;
6821
6822             /* Getting bits 15:9, which represent the combination of course and
6823              * fine gain values.  The result is a number that can be put into
6824              * the lookup table to obtain the approximate cable length. */
6825             cur_agc_index = (phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
6826                             IGP02E1000_AGC_LENGTH_MASK;
6827
6828             /* Array index bound check. */
6829             if ((cur_agc_index >= IGP02E1000_AGC_LENGTH_TABLE_SIZE) ||
6830                 (cur_agc_index == 0))
6831                 return -E1000_ERR_PHY;
6832
6833             /* Remove min & max AGC values from calculation. */
6834             if (e1000_igp_2_cable_length_table[min_agc_index] >
6835                 e1000_igp_2_cable_length_table[cur_agc_index])
6836                 min_agc_index = cur_agc_index;
6837             if (e1000_igp_2_cable_length_table[max_agc_index] <
6838                 e1000_igp_2_cable_length_table[cur_agc_index])
6839                 max_agc_index = cur_agc_index;
6840
6841             agc_value += e1000_igp_2_cable_length_table[cur_agc_index];
6842         }
6843
6844         agc_value -= (e1000_igp_2_cable_length_table[min_agc_index] +
6845                       e1000_igp_2_cable_length_table[max_agc_index]);
6846         agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
6847
6848         /* Calculate cable length with the error range of +/- 10 meters. */
6849         *min_length = ((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
6850                        (agc_value - IGP02E1000_AGC_RANGE) : 0;
6851         *max_length = agc_value + IGP02E1000_AGC_RANGE;
6852     }
6853
6854     return E1000_SUCCESS;
6855 }
6856
6857 /******************************************************************************
6858  * Check the cable polarity
6859  *
6860  * hw - Struct containing variables accessed by shared code
6861  * polarity - output parameter : 0 - Polarity is not reversed
6862  *                               1 - Polarity is reversed.
6863  *
6864  * returns: - E1000_ERR_XXX
6865  *            E1000_SUCCESS
6866  *
6867  * For phy's older then IGP, this function simply reads the polarity bit in the
6868  * Phy Status register.  For IGP phy's, this bit is valid only if link speed is
6869  * 10 Mbps.  If the link speed is 100 Mbps there is no polarity so this bit will
6870  * return 0.  If the link speed is 1000 Mbps the polarity status is in the
6871  * IGP01E1000_PHY_PCS_INIT_REG.
6872  *****************************************************************************/
6873 static int32_t
6874 e1000_check_polarity(struct e1000_hw *hw,
6875                      uint16_t *polarity)
6876 {
6877     int32_t ret_val;
6878     uint16_t phy_data;
6879
6880     DEBUGFUNC("e1000_check_polarity");
6881
6882     if ((hw->phy_type == e1000_phy_m88) ||
6883         (hw->phy_type == e1000_phy_gg82563)) {
6884         /* return the Polarity bit in the Status register. */
6885         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
6886                                      &phy_data);
6887         if(ret_val)
6888             return ret_val;
6889         *polarity = (phy_data & M88E1000_PSSR_REV_POLARITY) >>
6890                     M88E1000_PSSR_REV_POLARITY_SHIFT;
6891     } else if (hw->phy_type == e1000_phy_igp ||
6892               hw->phy_type == e1000_phy_igp_3 ||
6893               hw->phy_type == e1000_phy_igp_2) {
6894         /* Read the Status register to check the speed */
6895         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS,
6896                                      &phy_data);
6897         if(ret_val)
6898             return ret_val;
6899
6900         /* If speed is 1000 Mbps, must read the IGP01E1000_PHY_PCS_INIT_REG to
6901          * find the polarity status */
6902         if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
6903            IGP01E1000_PSSR_SPEED_1000MBPS) {
6904
6905             /* Read the GIG initialization PCS register (0x00B4) */
6906             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG,
6907                                          &phy_data);
6908             if(ret_val)
6909                 return ret_val;
6910
6911             /* Check the polarity bits */
6912             *polarity = (phy_data & IGP01E1000_PHY_POLARITY_MASK) ? 1 : 0;
6913         } else {
6914             /* For 10 Mbps, read the polarity bit in the status register. (for
6915              * 100 Mbps this bit is always 0) */
6916             *polarity = phy_data & IGP01E1000_PSSR_POLARITY_REVERSED;
6917         }
6918     } else if (hw->phy_type == e1000_phy_ife) {
6919         ret_val = e1000_read_phy_reg(hw, IFE_PHY_EXTENDED_STATUS_CONTROL,
6920                                      &phy_data);
6921         if (ret_val)
6922             return ret_val;
6923         *polarity = (phy_data & IFE_PESC_POLARITY_REVERSED) >>
6924                            IFE_PESC_POLARITY_REVERSED_SHIFT;
6925     }
6926     return E1000_SUCCESS;
6927 }
6928
6929 /******************************************************************************
6930  * Check if Downshift occured
6931  *
6932  * hw - Struct containing variables accessed by shared code
6933  * downshift - output parameter : 0 - No Downshift ocured.
6934  *                                1 - Downshift ocured.
6935  *
6936  * returns: - E1000_ERR_XXX
6937  *            E1000_SUCCESS
6938  *
6939  * For phy's older then IGP, this function reads the Downshift bit in the Phy
6940  * Specific Status register.  For IGP phy's, it reads the Downgrade bit in the
6941  * Link Health register.  In IGP this bit is latched high, so the driver must
6942  * read it immediately after link is established.
6943  *****************************************************************************/
6944 static int32_t
6945 e1000_check_downshift(struct e1000_hw *hw)
6946 {
6947     int32_t ret_val;
6948     uint16_t phy_data;
6949
6950     DEBUGFUNC("e1000_check_downshift");
6951
6952     if (hw->phy_type == e1000_phy_igp ||
6953         hw->phy_type == e1000_phy_igp_3 ||
6954         hw->phy_type == e1000_phy_igp_2) {
6955         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_LINK_HEALTH,
6956                                      &phy_data);
6957         if(ret_val)
6958             return ret_val;
6959
6960         hw->speed_downgraded = (phy_data & IGP01E1000_PLHR_SS_DOWNGRADE) ? 1 : 0;
6961     } else if ((hw->phy_type == e1000_phy_m88) ||
6962                (hw->phy_type == e1000_phy_gg82563)) {
6963         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
6964                                      &phy_data);
6965         if(ret_val)
6966             return ret_val;
6967
6968         hw->speed_downgraded = (phy_data & M88E1000_PSSR_DOWNSHIFT) >>
6969                                M88E1000_PSSR_DOWNSHIFT_SHIFT;
6970     } else if (hw->phy_type == e1000_phy_ife) {
6971         /* e1000_phy_ife supports 10/100 speed only */
6972         hw->speed_downgraded = FALSE;
6973     }
6974
6975     return E1000_SUCCESS;
6976 }
6977
6978 /*****************************************************************************
6979  *
6980  * 82541_rev_2 & 82547_rev_2 have the capability to configure the DSP when a
6981  * gigabit link is achieved to improve link quality.
6982  *
6983  * hw: Struct containing variables accessed by shared code
6984  *
6985  * returns: - E1000_ERR_PHY if fail to read/write the PHY
6986  *            E1000_SUCCESS at any other case.
6987  *
6988  ****************************************************************************/
6989
6990 static int32_t
6991 e1000_config_dsp_after_link_change(struct e1000_hw *hw,
6992                                    boolean_t link_up)
6993 {
6994     int32_t ret_val;
6995     uint16_t phy_data, phy_saved_data, speed, duplex, i;
6996     uint16_t dsp_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
6997                                         {IGP01E1000_PHY_AGC_PARAM_A,
6998                                         IGP01E1000_PHY_AGC_PARAM_B,
6999                                         IGP01E1000_PHY_AGC_PARAM_C,
7000                                         IGP01E1000_PHY_AGC_PARAM_D};
7001     uint16_t min_length, max_length;
7002
7003     DEBUGFUNC("e1000_config_dsp_after_link_change");
7004
7005     if(hw->phy_type != e1000_phy_igp)
7006         return E1000_SUCCESS;
7007
7008     if(link_up) {
7009         ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
7010         if(ret_val) {
7011             DEBUGOUT("Error getting link speed and duplex\n");
7012             return ret_val;
7013         }
7014
7015         if(speed == SPEED_1000) {
7016
7017             ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
7018             if (ret_val)
7019                 return ret_val;
7020
7021             if((hw->dsp_config_state == e1000_dsp_config_enabled) &&
7022                 min_length >= e1000_igp_cable_length_50) {
7023
7024                 for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
7025                     ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i],
7026                                                  &phy_data);
7027                     if(ret_val)
7028                         return ret_val;
7029
7030                     phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
7031
7032                     ret_val = e1000_write_phy_reg(hw, dsp_reg_array[i],
7033                                                   phy_data);
7034                     if(ret_val)
7035                         return ret_val;
7036                 }
7037                 hw->dsp_config_state = e1000_dsp_config_activated;
7038             }
7039
7040             if((hw->ffe_config_state == e1000_ffe_config_enabled) &&
7041                (min_length < e1000_igp_cable_length_50)) {
7042
7043                 uint16_t ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_20;
7044                 uint32_t idle_errs = 0;
7045
7046                 /* clear previous idle error counts */
7047                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
7048                                              &phy_data);
7049                 if(ret_val)
7050                     return ret_val;
7051
7052                 for(i = 0; i < ffe_idle_err_timeout; i++) {
7053                     udelay(1000);
7054                     ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
7055                                                  &phy_data);
7056                     if(ret_val)
7057                         return ret_val;
7058
7059                     idle_errs += (phy_data & SR_1000T_IDLE_ERROR_CNT);
7060                     if(idle_errs > SR_1000T_PHY_EXCESSIVE_IDLE_ERR_COUNT) {
7061                         hw->ffe_config_state = e1000_ffe_config_active;
7062
7063                         ret_val = e1000_write_phy_reg(hw,
7064                                     IGP01E1000_PHY_DSP_FFE,
7065                                     IGP01E1000_PHY_DSP_FFE_CM_CP);
7066                         if(ret_val)
7067                             return ret_val;
7068                         break;
7069                     }
7070
7071                     if(idle_errs)
7072                         ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_100;
7073                 }
7074             }
7075         }
7076     } else {
7077         if(hw->dsp_config_state == e1000_dsp_config_activated) {
7078             /* Save off the current value of register 0x2F5B to be restored at
7079              * the end of the routines. */
7080             ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
7081
7082             if(ret_val)
7083                 return ret_val;
7084
7085             /* Disable the PHY transmitter */
7086             ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
7087
7088             if(ret_val)
7089                 return ret_val;
7090
7091             msec_delay_irq(20);
7092
7093             ret_val = e1000_write_phy_reg(hw, 0x0000,
7094                                           IGP01E1000_IEEE_FORCE_GIGA);
7095             if(ret_val)
7096                 return ret_val;
7097             for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
7098                 ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i], &phy_data);
7099                 if(ret_val)
7100                     return ret_val;
7101
7102                 phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
7103                 phy_data |=  IGP01E1000_PHY_EDAC_SIGN_EXT_9_BITS;
7104
7105                 ret_val = e1000_write_phy_reg(hw,dsp_reg_array[i], phy_data);
7106                 if(ret_val)
7107                     return ret_val;
7108             }
7109
7110             ret_val = e1000_write_phy_reg(hw, 0x0000,
7111                                           IGP01E1000_IEEE_RESTART_AUTONEG);
7112             if(ret_val)
7113                 return ret_val;
7114
7115             msec_delay_irq(20);
7116
7117             /* Now enable the transmitter */
7118             ret_val = e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
7119
7120             if(ret_val)
7121                 return ret_val;
7122
7123             hw->dsp_config_state = e1000_dsp_config_enabled;
7124         }
7125
7126         if(hw->ffe_config_state == e1000_ffe_config_active) {
7127             /* Save off the current value of register 0x2F5B to be restored at
7128              * the end of the routines. */
7129             ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
7130
7131             if(ret_val)
7132                 return ret_val;
7133
7134             /* Disable the PHY transmitter */
7135             ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
7136
7137             if(ret_val)
7138                 return ret_val;
7139
7140             msec_delay_irq(20);
7141
7142             ret_val = e1000_write_phy_reg(hw, 0x0000,
7143                                           IGP01E1000_IEEE_FORCE_GIGA);
7144             if(ret_val)
7145                 return ret_val;
7146             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_DSP_FFE,
7147                                           IGP01E1000_PHY_DSP_FFE_DEFAULT);
7148             if(ret_val)
7149                 return ret_val;
7150
7151             ret_val = e1000_write_phy_reg(hw, 0x0000,
7152                                           IGP01E1000_IEEE_RESTART_AUTONEG);
7153             if(ret_val)
7154                 return ret_val;
7155
7156             msec_delay_irq(20);
7157
7158             /* Now enable the transmitter */
7159             ret_val = e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
7160
7161             if(ret_val)
7162                 return ret_val;
7163
7164             hw->ffe_config_state = e1000_ffe_config_enabled;
7165         }
7166     }
7167     return E1000_SUCCESS;
7168 }
7169
7170 /*****************************************************************************
7171  * Set PHY to class A mode
7172  * Assumes the following operations will follow to enable the new class mode.
7173  *  1. Do a PHY soft reset
7174  *  2. Restart auto-negotiation or force link.
7175  *
7176  * hw - Struct containing variables accessed by shared code
7177  ****************************************************************************/
7178 static int32_t
7179 e1000_set_phy_mode(struct e1000_hw *hw)
7180 {
7181     int32_t ret_val;
7182     uint16_t eeprom_data;
7183
7184     DEBUGFUNC("e1000_set_phy_mode");
7185
7186     if((hw->mac_type == e1000_82545_rev_3) &&
7187        (hw->media_type == e1000_media_type_copper)) {
7188         ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD, 1, &eeprom_data);
7189         if(ret_val) {
7190             return ret_val;
7191         }
7192
7193         if((eeprom_data != EEPROM_RESERVED_WORD) &&
7194            (eeprom_data & EEPROM_PHY_CLASS_A)) {
7195             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x000B);
7196             if(ret_val)
7197                 return ret_val;
7198             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x8104);
7199             if(ret_val)
7200                 return ret_val;
7201
7202             hw->phy_reset_disable = FALSE;
7203         }
7204     }
7205
7206     return E1000_SUCCESS;
7207 }
7208
7209 /*****************************************************************************
7210  *
7211  * This function sets the lplu state according to the active flag.  When
7212  * activating lplu this function also disables smart speed and vise versa.
7213  * lplu will not be activated unless the device autonegotiation advertisment
7214  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
7215  * hw: Struct containing variables accessed by shared code
7216  * active - true to enable lplu false to disable lplu.
7217  *
7218  * returns: - E1000_ERR_PHY if fail to read/write the PHY
7219  *            E1000_SUCCESS at any other case.
7220  *
7221  ****************************************************************************/
7222
7223 static int32_t
7224 e1000_set_d3_lplu_state(struct e1000_hw *hw,
7225                         boolean_t active)
7226 {
7227     uint32_t phy_ctrl = 0;
7228     int32_t ret_val;
7229     uint16_t phy_data;
7230     DEBUGFUNC("e1000_set_d3_lplu_state");
7231
7232     if (hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2
7233         && hw->phy_type != e1000_phy_igp_3)
7234         return E1000_SUCCESS;
7235
7236     /* During driver activity LPLU should not be used or it will attain link
7237      * from the lowest speeds starting from 10Mbps. The capability is used for
7238      * Dx transitions and states */
7239     if (hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2) {
7240         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO, &phy_data);
7241         if (ret_val)
7242             return ret_val;
7243     } else if (hw->mac_type == e1000_ich8lan) {
7244         /* MAC writes into PHY register based on the state transition
7245          * and start auto-negotiation. SW driver can overwrite the settings
7246          * in CSR PHY power control E1000_PHY_CTRL register. */
7247         phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
7248     } else {
7249         ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
7250         if(ret_val)
7251             return ret_val;
7252     }
7253
7254     if(!active) {
7255         if(hw->mac_type == e1000_82541_rev_2 ||
7256            hw->mac_type == e1000_82547_rev_2) {
7257             phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
7258             ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
7259             if(ret_val)
7260                 return ret_val;
7261         } else {
7262             if (hw->mac_type == e1000_ich8lan) {
7263                 phy_ctrl &= ~E1000_PHY_CTRL_NOND0A_LPLU;
7264                 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
7265             } else {
7266                 phy_data &= ~IGP02E1000_PM_D3_LPLU;
7267                 ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
7268                                               phy_data);
7269                 if (ret_val)
7270                     return ret_val;
7271             }
7272         }
7273
7274         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
7275          * Dx states where the power conservation is most important.  During
7276          * driver activity we should enable SmartSpeed, so performance is
7277          * maintained. */
7278         if (hw->smart_speed == e1000_smart_speed_on) {
7279             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
7280                                          &phy_data);
7281             if(ret_val)
7282                 return ret_val;
7283
7284             phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
7285             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
7286                                           phy_data);
7287             if(ret_val)
7288                 return ret_val;
7289         } else if (hw->smart_speed == e1000_smart_speed_off) {
7290             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
7291                                          &phy_data);
7292             if (ret_val)
7293                 return ret_val;
7294
7295             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
7296             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
7297                                           phy_data);
7298             if(ret_val)
7299                 return ret_val;
7300         }
7301
7302     } else if((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT) ||
7303               (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL ) ||
7304               (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
7305
7306         if(hw->mac_type == e1000_82541_rev_2 ||
7307             hw->mac_type == e1000_82547_rev_2) {
7308             phy_data |= IGP01E1000_GMII_FLEX_SPD;
7309             ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
7310             if(ret_val)
7311                 return ret_val;
7312         } else {
7313             if (hw->mac_type == e1000_ich8lan) {
7314                 phy_ctrl |= E1000_PHY_CTRL_NOND0A_LPLU;
7315                 E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
7316             } else {
7317                 phy_data |= IGP02E1000_PM_D3_LPLU;
7318                 ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
7319                                               phy_data);
7320                 if (ret_val)
7321                     return ret_val;
7322             }
7323         }
7324
7325         /* When LPLU is enabled we should disable SmartSpeed */
7326         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
7327         if(ret_val)
7328             return ret_val;
7329
7330         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
7331         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
7332         if(ret_val)
7333             return ret_val;
7334
7335     }
7336     return E1000_SUCCESS;
7337 }
7338
7339 /*****************************************************************************
7340  *
7341  * This function sets the lplu d0 state according to the active flag.  When
7342  * activating lplu this function also disables smart speed and vise versa.
7343  * lplu will not be activated unless the device autonegotiation advertisment
7344  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
7345  * hw: Struct containing variables accessed by shared code
7346  * active - true to enable lplu false to disable lplu.
7347  *
7348  * returns: - E1000_ERR_PHY if fail to read/write the PHY
7349  *            E1000_SUCCESS at any other case.
7350  *
7351  ****************************************************************************/
7352
7353 static int32_t
7354 e1000_set_d0_lplu_state(struct e1000_hw *hw,
7355                         boolean_t active)
7356 {
7357     uint32_t phy_ctrl = 0;
7358     int32_t ret_val;
7359     uint16_t phy_data;
7360     DEBUGFUNC("e1000_set_d0_lplu_state");
7361
7362     if(hw->mac_type <= e1000_82547_rev_2)
7363         return E1000_SUCCESS;
7364
7365     if (hw->mac_type == e1000_ich8lan) {
7366         phy_ctrl = E1000_READ_REG(hw, PHY_CTRL);
7367     } else {
7368         ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
7369         if(ret_val)
7370             return ret_val;
7371     }
7372
7373     if (!active) {
7374         if (hw->mac_type == e1000_ich8lan) {
7375             phy_ctrl &= ~E1000_PHY_CTRL_D0A_LPLU;
7376             E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
7377         } else {
7378             phy_data &= ~IGP02E1000_PM_D0_LPLU;
7379             ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
7380             if (ret_val)
7381                 return ret_val;
7382         }
7383
7384         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
7385          * Dx states where the power conservation is most important.  During
7386          * driver activity we should enable SmartSpeed, so performance is
7387          * maintained. */
7388         if (hw->smart_speed == e1000_smart_speed_on) {
7389             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
7390                                          &phy_data);
7391             if(ret_val)
7392                 return ret_val;
7393
7394             phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
7395             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
7396                                           phy_data);
7397             if(ret_val)
7398                 return ret_val;
7399         } else if (hw->smart_speed == e1000_smart_speed_off) {
7400             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
7401                                          &phy_data);
7402             if (ret_val)
7403                 return ret_val;
7404
7405             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
7406             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
7407                                           phy_data);
7408             if(ret_val)
7409                 return ret_val;
7410         }
7411
7412
7413     } else {
7414
7415         if (hw->mac_type == e1000_ich8lan) {
7416             phy_ctrl |= E1000_PHY_CTRL_D0A_LPLU;
7417             E1000_WRITE_REG(hw, PHY_CTRL, phy_ctrl);
7418         } else {
7419             phy_data |= IGP02E1000_PM_D0_LPLU;
7420             ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
7421             if (ret_val)
7422                 return ret_val;
7423         }
7424
7425         /* When LPLU is enabled we should disable SmartSpeed */
7426         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
7427         if(ret_val)
7428             return ret_val;
7429
7430         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
7431         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
7432         if(ret_val)
7433             return ret_val;
7434
7435     }
7436     return E1000_SUCCESS;
7437 }
7438
7439 /******************************************************************************
7440  * Change VCO speed register to improve Bit Error Rate performance of SERDES.
7441  *
7442  * hw - Struct containing variables accessed by shared code
7443  *****************************************************************************/
7444 static int32_t
7445 e1000_set_vco_speed(struct e1000_hw *hw)
7446 {
7447     int32_t  ret_val;
7448     uint16_t default_page = 0;
7449     uint16_t phy_data;
7450
7451     DEBUGFUNC("e1000_set_vco_speed");
7452
7453     switch(hw->mac_type) {
7454     case e1000_82545_rev_3:
7455     case e1000_82546_rev_3:
7456        break;
7457     default:
7458         return E1000_SUCCESS;
7459     }
7460
7461     /* Set PHY register 30, page 5, bit 8 to 0 */
7462
7463     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, &default_page);
7464     if(ret_val)
7465         return ret_val;
7466
7467     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0005);
7468     if(ret_val)
7469         return ret_val;
7470
7471     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
7472     if(ret_val)
7473         return ret_val;
7474
7475     phy_data &= ~M88E1000_PHY_VCO_REG_BIT8;
7476     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
7477     if(ret_val)
7478         return ret_val;
7479
7480     /* Set PHY register 30, page 4, bit 11 to 1 */
7481
7482     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0004);
7483     if(ret_val)
7484         return ret_val;
7485
7486     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
7487     if(ret_val)
7488         return ret_val;
7489
7490     phy_data |= M88E1000_PHY_VCO_REG_BIT11;
7491     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
7492     if(ret_val)
7493         return ret_val;
7494
7495     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, default_page);
7496     if(ret_val)
7497         return ret_val;
7498
7499     return E1000_SUCCESS;
7500 }
7501
7502
7503 /*****************************************************************************
7504  * This function reads the cookie from ARC ram.
7505  *
7506  * returns: - E1000_SUCCESS .
7507  ****************************************************************************/
7508 int32_t
7509 e1000_host_if_read_cookie(struct e1000_hw * hw, uint8_t *buffer)
7510 {
7511     uint8_t i;
7512     uint32_t offset = E1000_MNG_DHCP_COOKIE_OFFSET;
7513     uint8_t length = E1000_MNG_DHCP_COOKIE_LENGTH;
7514
7515     length = (length >> 2);
7516     offset = (offset >> 2);
7517
7518     for (i = 0; i < length; i++) {
7519         *((uint32_t *) buffer + i) =
7520             E1000_READ_REG_ARRAY_DWORD(hw, HOST_IF, offset + i);
7521     }
7522     return E1000_SUCCESS;
7523 }
7524
7525
7526 /*****************************************************************************
7527  * This function checks whether the HOST IF is enabled for command operaton
7528  * and also checks whether the previous command is completed.
7529  * It busy waits in case of previous command is not completed.
7530  *
7531  * returns: - E1000_ERR_HOST_INTERFACE_COMMAND in case if is not ready or
7532  *            timeout
7533  *          - E1000_SUCCESS for success.
7534  ****************************************************************************/
7535 static int32_t
7536 e1000_mng_enable_host_if(struct e1000_hw * hw)
7537 {
7538     uint32_t hicr;
7539     uint8_t i;
7540
7541     /* Check that the host interface is enabled. */
7542     hicr = E1000_READ_REG(hw, HICR);
7543     if ((hicr & E1000_HICR_EN) == 0) {
7544         DEBUGOUT("E1000_HOST_EN bit disabled.\n");
7545         return -E1000_ERR_HOST_INTERFACE_COMMAND;
7546     }
7547     /* check the previous command is completed */
7548     for (i = 0; i < E1000_MNG_DHCP_COMMAND_TIMEOUT; i++) {
7549         hicr = E1000_READ_REG(hw, HICR);
7550         if (!(hicr & E1000_HICR_C))
7551             break;
7552         msec_delay_irq(1);
7553     }
7554
7555     if (i == E1000_MNG_DHCP_COMMAND_TIMEOUT) {
7556         DEBUGOUT("Previous command timeout failed .\n");
7557         return -E1000_ERR_HOST_INTERFACE_COMMAND;
7558     }
7559     return E1000_SUCCESS;
7560 }
7561
7562 /*****************************************************************************
7563  * This function writes the buffer content at the offset given on the host if.
7564  * It also does alignment considerations to do the writes in most efficient way.
7565  * Also fills up the sum of the buffer in *buffer parameter.
7566  *
7567  * returns  - E1000_SUCCESS for success.
7568  ****************************************************************************/
7569 static int32_t
7570 e1000_mng_host_if_write(struct e1000_hw * hw, uint8_t *buffer,
7571                         uint16_t length, uint16_t offset, uint8_t *sum)
7572 {
7573     uint8_t *tmp;
7574     uint8_t *bufptr = buffer;
7575     uint32_t data;
7576     uint16_t remaining, i, j, prev_bytes;
7577
7578     /* sum = only sum of the data and it is not checksum */
7579
7580     if (length == 0 || offset + length > E1000_HI_MAX_MNG_DATA_LENGTH) {
7581         return -E1000_ERR_PARAM;
7582     }
7583
7584     tmp = (uint8_t *)&data;
7585     prev_bytes = offset & 0x3;
7586     offset &= 0xFFFC;
7587     offset >>= 2;
7588
7589     if (prev_bytes) {
7590         data = E1000_READ_REG_ARRAY_DWORD(hw, HOST_IF, offset);
7591         for (j = prev_bytes; j < sizeof(uint32_t); j++) {
7592             *(tmp + j) = *bufptr++;
7593             *sum += *(tmp + j);
7594         }
7595         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset, data);
7596         length -= j - prev_bytes;
7597         offset++;
7598     }
7599
7600     remaining = length & 0x3;
7601     length -= remaining;
7602
7603     /* Calculate length in DWORDs */
7604     length >>= 2;
7605
7606     /* The device driver writes the relevant command block into the
7607      * ram area. */
7608     for (i = 0; i < length; i++) {
7609         for (j = 0; j < sizeof(uint32_t); j++) {
7610             *(tmp + j) = *bufptr++;
7611             *sum += *(tmp + j);
7612         }
7613
7614         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset + i, data);
7615     }
7616     if (remaining) {
7617         for (j = 0; j < sizeof(uint32_t); j++) {
7618             if (j < remaining)
7619                 *(tmp + j) = *bufptr++;
7620             else
7621                 *(tmp + j) = 0;
7622
7623             *sum += *(tmp + j);
7624         }
7625         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset + i, data);
7626     }
7627
7628     return E1000_SUCCESS;
7629 }
7630
7631
7632 /*****************************************************************************
7633  * This function writes the command header after does the checksum calculation.
7634  *
7635  * returns  - E1000_SUCCESS for success.
7636  ****************************************************************************/
7637 static int32_t
7638 e1000_mng_write_cmd_header(struct e1000_hw * hw,
7639                            struct e1000_host_mng_command_header * hdr)
7640 {
7641     uint16_t i;
7642     uint8_t sum;
7643     uint8_t *buffer;
7644
7645     /* Write the whole command header structure which includes sum of
7646      * the buffer */
7647
7648     uint16_t length = sizeof(struct e1000_host_mng_command_header);
7649
7650     sum = hdr->checksum;
7651     hdr->checksum = 0;
7652
7653     buffer = (uint8_t *) hdr;
7654     i = length;
7655     while(i--)
7656         sum += buffer[i];
7657
7658     hdr->checksum = 0 - sum;
7659
7660     length >>= 2;
7661     /* The device driver writes the relevant command block into the ram area. */
7662     for (i = 0; i < length; i++) {
7663         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, i, *((uint32_t *) hdr + i));
7664         E1000_WRITE_FLUSH(hw);
7665     }
7666
7667     return E1000_SUCCESS;
7668 }
7669
7670
7671 /*****************************************************************************
7672  * This function indicates to ARC that a new command is pending which completes
7673  * one write operation by the driver.
7674  *
7675  * returns  - E1000_SUCCESS for success.
7676  ****************************************************************************/
7677 static int32_t
7678 e1000_mng_write_commit(
7679     struct e1000_hw * hw)
7680 {
7681     uint32_t hicr;
7682
7683     hicr = E1000_READ_REG(hw, HICR);
7684     /* Setting this bit tells the ARC that a new command is pending. */
7685     E1000_WRITE_REG(hw, HICR, hicr | E1000_HICR_C);
7686
7687     return E1000_SUCCESS;
7688 }
7689
7690
7691 /*****************************************************************************
7692  * This function checks the mode of the firmware.
7693  *
7694  * returns  - TRUE when the mode is IAMT or FALSE.
7695  ****************************************************************************/
7696 boolean_t
7697 e1000_check_mng_mode(struct e1000_hw *hw)
7698 {
7699     uint32_t fwsm;
7700
7701     fwsm = E1000_READ_REG(hw, FWSM);
7702
7703     if (hw->mac_type == e1000_ich8lan) {
7704         if ((fwsm & E1000_FWSM_MODE_MASK) ==
7705             (E1000_MNG_ICH_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
7706             return TRUE;
7707     } else if ((fwsm & E1000_FWSM_MODE_MASK) ==
7708                (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
7709         return TRUE;
7710
7711     return FALSE;
7712 }
7713
7714
7715 /*****************************************************************************
7716  * This function writes the dhcp info .
7717  ****************************************************************************/
7718 int32_t
7719 e1000_mng_write_dhcp_info(struct e1000_hw * hw, uint8_t *buffer,
7720                           uint16_t length)
7721 {
7722     int32_t ret_val;
7723     struct e1000_host_mng_command_header hdr;
7724
7725     hdr.command_id = E1000_MNG_DHCP_TX_PAYLOAD_CMD;
7726     hdr.command_length = length;
7727     hdr.reserved1 = 0;
7728     hdr.reserved2 = 0;
7729     hdr.checksum = 0;
7730
7731     ret_val = e1000_mng_enable_host_if(hw);
7732     if (ret_val == E1000_SUCCESS) {
7733         ret_val = e1000_mng_host_if_write(hw, buffer, length, sizeof(hdr),
7734                                           &(hdr.checksum));
7735         if (ret_val == E1000_SUCCESS) {
7736             ret_val = e1000_mng_write_cmd_header(hw, &hdr);
7737             if (ret_val == E1000_SUCCESS)
7738                 ret_val = e1000_mng_write_commit(hw);
7739         }
7740     }
7741     return ret_val;
7742 }
7743
7744
7745 /*****************************************************************************
7746  * This function calculates the checksum.
7747  *
7748  * returns  - checksum of buffer contents.
7749  ****************************************************************************/
7750 uint8_t
7751 e1000_calculate_mng_checksum(char *buffer, uint32_t length)
7752 {
7753     uint8_t sum = 0;
7754     uint32_t i;
7755
7756     if (!buffer)
7757         return 0;
7758
7759     for (i=0; i < length; i++)
7760         sum += buffer[i];
7761
7762     return (uint8_t) (0 - sum);
7763 }
7764
7765 /*****************************************************************************
7766  * This function checks whether tx pkt filtering needs to be enabled or not.
7767  *
7768  * returns  - TRUE for packet filtering or FALSE.
7769  ****************************************************************************/
7770 boolean_t
7771 e1000_enable_tx_pkt_filtering(struct e1000_hw *hw)
7772 {
7773     /* called in init as well as watchdog timer functions */
7774
7775     int32_t ret_val, checksum;
7776     boolean_t tx_filter = FALSE;
7777     struct e1000_host_mng_dhcp_cookie *hdr = &(hw->mng_cookie);
7778     uint8_t *buffer = (uint8_t *) &(hw->mng_cookie);
7779
7780     if (e1000_check_mng_mode(hw)) {
7781         ret_val = e1000_mng_enable_host_if(hw);
7782         if (ret_val == E1000_SUCCESS) {
7783             ret_val = e1000_host_if_read_cookie(hw, buffer);
7784             if (ret_val == E1000_SUCCESS) {
7785                 checksum = hdr->checksum;
7786                 hdr->checksum = 0;
7787                 if ((hdr->signature == E1000_IAMT_SIGNATURE) &&
7788                     checksum == e1000_calculate_mng_checksum((char *)buffer,
7789                                                E1000_MNG_DHCP_COOKIE_LENGTH)) {
7790                     if (hdr->status &
7791                         E1000_MNG_DHCP_COOKIE_STATUS_PARSING_SUPPORT)
7792                         tx_filter = TRUE;
7793                 } else
7794                     tx_filter = TRUE;
7795             } else
7796                 tx_filter = TRUE;
7797         }
7798     }
7799
7800     hw->tx_pkt_filtering = tx_filter;
7801     return tx_filter;
7802 }
7803
7804 /******************************************************************************
7805  * Verifies the hardware needs to allow ARPs to be processed by the host
7806  *
7807  * hw - Struct containing variables accessed by shared code
7808  *
7809  * returns: - TRUE/FALSE
7810  *
7811  *****************************************************************************/
7812 uint32_t
7813 e1000_enable_mng_pass_thru(struct e1000_hw *hw)
7814 {
7815     uint32_t manc;
7816     uint32_t fwsm, factps;
7817
7818     if (hw->asf_firmware_present) {
7819         manc = E1000_READ_REG(hw, MANC);
7820
7821         if (!(manc & E1000_MANC_RCV_TCO_EN) ||
7822             !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
7823             return FALSE;
7824         if (e1000_arc_subsystem_valid(hw) == TRUE) {
7825             fwsm = E1000_READ_REG(hw, FWSM);
7826             factps = E1000_READ_REG(hw, FACTPS);
7827
7828             if (((fwsm & E1000_FWSM_MODE_MASK) ==
7829                 (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT)) &&
7830                 (factps & E1000_FACTPS_MNGCG))
7831                 return TRUE;
7832         } else
7833             if ((manc & E1000_MANC_SMBUS_EN) && !(manc & E1000_MANC_ASF_EN))
7834                 return TRUE;
7835     }
7836     return FALSE;
7837 }
7838
7839 static int32_t
7840 e1000_polarity_reversal_workaround(struct e1000_hw *hw)
7841 {
7842     int32_t ret_val;
7843     uint16_t mii_status_reg;
7844     uint16_t i;
7845
7846     /* Polarity reversal workaround for forced 10F/10H links. */
7847
7848     /* Disable the transmitter on the PHY */
7849
7850     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
7851     if(ret_val)
7852         return ret_val;
7853     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFFF);
7854     if(ret_val)
7855         return ret_val;
7856
7857     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
7858     if(ret_val)
7859         return ret_val;
7860
7861     /* This loop will early-out if the NO link condition has been met. */
7862     for(i = PHY_FORCE_TIME; i > 0; i--) {
7863         /* Read the MII Status Register and wait for Link Status bit
7864          * to be clear.
7865          */
7866
7867         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
7868         if(ret_val)
7869             return ret_val;
7870
7871         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
7872         if(ret_val)
7873             return ret_val;
7874
7875         if((mii_status_reg & ~MII_SR_LINK_STATUS) == 0) break;
7876         msec_delay_irq(100);
7877     }
7878
7879     /* Recommended delay time after link has been lost */
7880     msec_delay_irq(1000);
7881
7882     /* Now we will re-enable th transmitter on the PHY */
7883
7884     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
7885     if(ret_val)
7886         return ret_val;
7887     msec_delay_irq(50);
7888     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFF0);
7889     if(ret_val)
7890         return ret_val;
7891     msec_delay_irq(50);
7892     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFF00);
7893     if(ret_val)
7894         return ret_val;
7895     msec_delay_irq(50);
7896     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x0000);
7897     if(ret_val)
7898         return ret_val;
7899
7900     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
7901     if(ret_val)
7902         return ret_val;
7903
7904     /* This loop will early-out if the link condition has been met. */
7905     for(i = PHY_FORCE_TIME; i > 0; i--) {
7906         /* Read the MII Status Register and wait for Link Status bit
7907          * to be set.
7908          */
7909
7910         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
7911         if(ret_val)
7912             return ret_val;
7913
7914         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
7915         if(ret_val)
7916             return ret_val;
7917
7918         if(mii_status_reg & MII_SR_LINK_STATUS) break;
7919         msec_delay_irq(100);
7920     }
7921     return E1000_SUCCESS;
7922 }
7923
7924 /***************************************************************************
7925  *
7926  * Disables PCI-Express master access.
7927  *
7928  * hw: Struct containing variables accessed by shared code
7929  *
7930  * returns: - none.
7931  *
7932  ***************************************************************************/
7933 static void
7934 e1000_set_pci_express_master_disable(struct e1000_hw *hw)
7935 {
7936     uint32_t ctrl;
7937
7938     DEBUGFUNC("e1000_set_pci_express_master_disable");
7939
7940     if (hw->bus_type != e1000_bus_type_pci_express)
7941         return;
7942
7943     ctrl = E1000_READ_REG(hw, CTRL);
7944     ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
7945     E1000_WRITE_REG(hw, CTRL, ctrl);
7946 }
7947
7948 /***************************************************************************
7949  *
7950  * Enables PCI-Express master access.
7951  *
7952  * hw: Struct containing variables accessed by shared code
7953  *
7954  * returns: - none.
7955  *
7956  ***************************************************************************/
7957 #if 0
7958 void
7959 e1000_enable_pciex_master(struct e1000_hw *hw)
7960 {
7961     uint32_t ctrl;
7962
7963     DEBUGFUNC("e1000_enable_pciex_master");
7964
7965     if (hw->bus_type != e1000_bus_type_pci_express)
7966         return;
7967
7968     ctrl = E1000_READ_REG(hw, CTRL);
7969     ctrl &= ~E1000_CTRL_GIO_MASTER_DISABLE;
7970     E1000_WRITE_REG(hw, CTRL, ctrl);
7971 }
7972 #endif  /*  0  */
7973
7974 /*******************************************************************************
7975  *
7976  * Disables PCI-Express master access and verifies there are no pending requests
7977  *
7978  * hw: Struct containing variables accessed by shared code
7979  *
7980  * returns: - E1000_ERR_MASTER_REQUESTS_PENDING if master disable bit hasn't
7981  *            caused the master requests to be disabled.
7982  *            E1000_SUCCESS master requests disabled.
7983  *
7984  ******************************************************************************/
7985 int32_t
7986 e1000_disable_pciex_master(struct e1000_hw *hw)
7987 {
7988     int32_t timeout = MASTER_DISABLE_TIMEOUT;   /* 80ms */
7989
7990     DEBUGFUNC("e1000_disable_pciex_master");
7991
7992     if (hw->bus_type != e1000_bus_type_pci_express)
7993         return E1000_SUCCESS;
7994
7995     e1000_set_pci_express_master_disable(hw);
7996
7997     while(timeout) {
7998         if(!(E1000_READ_REG(hw, STATUS) & E1000_STATUS_GIO_MASTER_ENABLE))
7999             break;
8000         else
8001             udelay(100);
8002         timeout--;
8003     }
8004
8005     if(!timeout) {
8006         DEBUGOUT("Master requests are pending.\n");
8007         return -E1000_ERR_MASTER_REQUESTS_PENDING;
8008     }
8009
8010     return E1000_SUCCESS;
8011 }
8012
8013 /*******************************************************************************
8014  *
8015  * Check for EEPROM Auto Read bit done.
8016  *
8017  * hw: Struct containing variables accessed by shared code
8018  *
8019  * returns: - E1000_ERR_RESET if fail to reset MAC
8020  *            E1000_SUCCESS at any other case.
8021  *
8022  ******************************************************************************/
8023 static int32_t
8024 e1000_get_auto_rd_done(struct e1000_hw *hw)
8025 {
8026     int32_t timeout = AUTO_READ_DONE_TIMEOUT;
8027
8028     DEBUGFUNC("e1000_get_auto_rd_done");
8029
8030     switch (hw->mac_type) {
8031     default:
8032         msec_delay(5);
8033         break;
8034     case e1000_82571:
8035     case e1000_82572:
8036     case e1000_82573:
8037     case e1000_80003es2lan:
8038     case e1000_ich8lan:
8039         while (timeout) {
8040             if (E1000_READ_REG(hw, EECD) & E1000_EECD_AUTO_RD)
8041                 break;
8042             else msec_delay(1);
8043             timeout--;
8044         }
8045
8046         if(!timeout) {
8047             DEBUGOUT("Auto read by HW from EEPROM has not completed.\n");
8048             return -E1000_ERR_RESET;
8049         }
8050         break;
8051     }
8052
8053     /* PHY configuration from NVM just starts after EECD_AUTO_RD sets to high.
8054      * Need to wait for PHY configuration completion before accessing NVM
8055      * and PHY. */
8056     if (hw->mac_type == e1000_82573)
8057         msec_delay(25);
8058
8059     return E1000_SUCCESS;
8060 }
8061
8062 /***************************************************************************
8063  * Checks if the PHY configuration is done
8064  *
8065  * hw: Struct containing variables accessed by shared code
8066  *
8067  * returns: - E1000_ERR_RESET if fail to reset MAC
8068  *            E1000_SUCCESS at any other case.
8069  *
8070  ***************************************************************************/
8071 static int32_t
8072 e1000_get_phy_cfg_done(struct e1000_hw *hw)
8073 {
8074     int32_t timeout = PHY_CFG_TIMEOUT;
8075     uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
8076
8077     DEBUGFUNC("e1000_get_phy_cfg_done");
8078
8079     switch (hw->mac_type) {
8080     default:
8081         msec_delay_irq(10);
8082         break;
8083     case e1000_80003es2lan:
8084         /* Separate *_CFG_DONE_* bit for each port */
8085         if (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
8086             cfg_mask = E1000_EEPROM_CFG_DONE_PORT_1;
8087         /* Fall Through */
8088     case e1000_82571:
8089     case e1000_82572:
8090         while (timeout) {
8091             if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
8092                 break;
8093             else
8094                 msec_delay(1);
8095             timeout--;
8096         }
8097
8098         if (!timeout) {
8099             DEBUGOUT("MNG configuration cycle has not completed.\n");
8100             return -E1000_ERR_RESET;
8101         }
8102         break;
8103     }
8104
8105     return E1000_SUCCESS;
8106 }
8107
8108 /***************************************************************************
8109  *
8110  * Using the combination of SMBI and SWESMBI semaphore bits when resetting
8111  * adapter or Eeprom access.
8112  *
8113  * hw: Struct containing variables accessed by shared code
8114  *
8115  * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
8116  *            E1000_SUCCESS at any other case.
8117  *
8118  ***************************************************************************/
8119 static int32_t
8120 e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
8121 {
8122     int32_t timeout;
8123     uint32_t swsm;
8124
8125     DEBUGFUNC("e1000_get_hw_eeprom_semaphore");
8126
8127     if(!hw->eeprom_semaphore_present)
8128         return E1000_SUCCESS;
8129
8130     if (hw->mac_type == e1000_80003es2lan) {
8131         /* Get the SW semaphore. */
8132         if (e1000_get_software_semaphore(hw) != E1000_SUCCESS)
8133             return -E1000_ERR_EEPROM;
8134     }
8135
8136     /* Get the FW semaphore. */
8137     timeout = hw->eeprom.word_size + 1;
8138     while(timeout) {
8139         swsm = E1000_READ_REG(hw, SWSM);
8140         swsm |= E1000_SWSM_SWESMBI;
8141         E1000_WRITE_REG(hw, SWSM, swsm);
8142         /* if we managed to set the bit we got the semaphore. */
8143         swsm = E1000_READ_REG(hw, SWSM);
8144         if(swsm & E1000_SWSM_SWESMBI)
8145             break;
8146
8147         udelay(50);
8148         timeout--;
8149     }
8150
8151     if(!timeout) {
8152         /* Release semaphores */
8153         e1000_put_hw_eeprom_semaphore(hw);
8154         DEBUGOUT("Driver can't access the Eeprom - SWESMBI bit is set.\n");
8155         return -E1000_ERR_EEPROM;
8156     }
8157
8158     return E1000_SUCCESS;
8159 }
8160
8161 /***************************************************************************
8162  * This function clears HW semaphore bits.
8163  *
8164  * hw: Struct containing variables accessed by shared code
8165  *
8166  * returns: - None.
8167  *
8168  ***************************************************************************/
8169 static void
8170 e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
8171 {
8172     uint32_t swsm;
8173
8174     DEBUGFUNC("e1000_put_hw_eeprom_semaphore");
8175
8176     if(!hw->eeprom_semaphore_present)
8177         return;
8178
8179     swsm = E1000_READ_REG(hw, SWSM);
8180     if (hw->mac_type == e1000_80003es2lan) {
8181         /* Release both semaphores. */
8182         swsm &= ~(E1000_SWSM_SMBI | E1000_SWSM_SWESMBI);
8183     } else
8184         swsm &= ~(E1000_SWSM_SWESMBI);
8185     E1000_WRITE_REG(hw, SWSM, swsm);
8186 }
8187
8188 /***************************************************************************
8189  *
8190  * Obtaining software semaphore bit (SMBI) before resetting PHY.
8191  *
8192  * hw: Struct containing variables accessed by shared code
8193  *
8194  * returns: - E1000_ERR_RESET if fail to obtain semaphore.
8195  *            E1000_SUCCESS at any other case.
8196  *
8197  ***************************************************************************/
8198 static int32_t
8199 e1000_get_software_semaphore(struct e1000_hw *hw)
8200 {
8201     int32_t timeout = hw->eeprom.word_size + 1;
8202     uint32_t swsm;
8203
8204     DEBUGFUNC("e1000_get_software_semaphore");
8205
8206     if (hw->mac_type != e1000_80003es2lan)
8207         return E1000_SUCCESS;
8208
8209     while(timeout) {
8210         swsm = E1000_READ_REG(hw, SWSM);
8211         /* If SMBI bit cleared, it is now set and we hold the semaphore */
8212         if(!(swsm & E1000_SWSM_SMBI))
8213             break;
8214         msec_delay_irq(1);
8215         timeout--;
8216     }
8217
8218     if(!timeout) {
8219         DEBUGOUT("Driver can't access device - SMBI bit is set.\n");
8220         return -E1000_ERR_RESET;
8221     }
8222
8223     return E1000_SUCCESS;
8224 }
8225
8226 /***************************************************************************
8227  *
8228  * Release semaphore bit (SMBI).
8229  *
8230  * hw: Struct containing variables accessed by shared code
8231  *
8232  ***************************************************************************/
8233 static void
8234 e1000_release_software_semaphore(struct e1000_hw *hw)
8235 {
8236     uint32_t swsm;
8237
8238     DEBUGFUNC("e1000_release_software_semaphore");
8239
8240     if (hw->mac_type != e1000_80003es2lan)
8241         return;
8242
8243     swsm = E1000_READ_REG(hw, SWSM);
8244     /* Release the SW semaphores.*/
8245     swsm &= ~E1000_SWSM_SMBI;
8246     E1000_WRITE_REG(hw, SWSM, swsm);
8247 }
8248
8249 /******************************************************************************
8250  * Checks if PHY reset is blocked due to SOL/IDER session, for example.
8251  * Returning E1000_BLK_PHY_RESET isn't necessarily an error.  But it's up to
8252  * the caller to figure out how to deal with it.
8253  *
8254  * hw - Struct containing variables accessed by shared code
8255  *
8256  * returns: - E1000_BLK_PHY_RESET
8257  *            E1000_SUCCESS
8258  *
8259  *****************************************************************************/
8260 int32_t
8261 e1000_check_phy_reset_block(struct e1000_hw *hw)
8262 {
8263     uint32_t manc = 0;
8264     uint32_t fwsm = 0;
8265
8266     if (hw->mac_type == e1000_ich8lan) {
8267         fwsm = E1000_READ_REG(hw, FWSM);
8268         return (fwsm & E1000_FWSM_RSPCIPHY) ? E1000_SUCCESS
8269                                             : E1000_BLK_PHY_RESET;
8270     }
8271
8272     if (hw->mac_type > e1000_82547_rev_2)
8273         manc = E1000_READ_REG(hw, MANC);
8274     return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
8275             E1000_BLK_PHY_RESET : E1000_SUCCESS;
8276 }
8277
8278 static uint8_t
8279 e1000_arc_subsystem_valid(struct e1000_hw *hw)
8280 {
8281     uint32_t fwsm;
8282
8283     /* On 8257x silicon, registers in the range of 0x8800 - 0x8FFC
8284      * may not be provided a DMA clock when no manageability features are
8285      * enabled.  We do not want to perform any reads/writes to these registers
8286      * if this is the case.  We read FWSM to determine the manageability mode.
8287      */
8288     switch (hw->mac_type) {
8289     case e1000_82571:
8290     case e1000_82572:
8291     case e1000_82573:
8292     case e1000_80003es2lan:
8293         fwsm = E1000_READ_REG(hw, FWSM);
8294         if((fwsm & E1000_FWSM_MODE_MASK) != 0)
8295             return TRUE;
8296         break;
8297     case e1000_ich8lan:
8298         return TRUE;
8299     default:
8300         break;
8301     }
8302     return FALSE;
8303 }
8304
8305
8306 /******************************************************************************
8307  * Configure PCI-Ex no-snoop
8308  *
8309  * hw - Struct containing variables accessed by shared code.
8310  * no_snoop - Bitmap of no-snoop events.
8311  *
8312  * returns: E1000_SUCCESS
8313  *
8314  *****************************************************************************/
8315 static int32_t
8316 e1000_set_pci_ex_no_snoop(struct e1000_hw *hw, uint32_t no_snoop)
8317 {
8318     uint32_t gcr_reg = 0;
8319
8320     DEBUGFUNC("e1000_set_pci_ex_no_snoop");
8321
8322     if (hw->bus_type == e1000_bus_type_unknown)
8323         e1000_get_bus_info(hw);
8324
8325     if (hw->bus_type != e1000_bus_type_pci_express)
8326         return E1000_SUCCESS;
8327
8328     if (no_snoop) {
8329         gcr_reg = E1000_READ_REG(hw, GCR);
8330         gcr_reg &= ~(PCI_EX_NO_SNOOP_ALL);
8331         gcr_reg |= no_snoop;
8332         E1000_WRITE_REG(hw, GCR, gcr_reg);
8333     }
8334     if (hw->mac_type == e1000_ich8lan) {
8335         uint32_t ctrl_ext;
8336
8337         E1000_WRITE_REG(hw, GCR, PCI_EX_82566_SNOOP_ALL);
8338
8339         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
8340         ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
8341         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
8342     }
8343
8344     return E1000_SUCCESS;
8345 }
8346
8347 /***************************************************************************
8348  *
8349  * Get software semaphore FLAG bit (SWFLAG).
8350  * SWFLAG is used to synchronize the access to all shared resource between
8351  * SW, FW and HW.
8352  *
8353  * hw: Struct containing variables accessed by shared code
8354  *
8355  ***************************************************************************/
8356 static int32_t
8357 e1000_get_software_flag(struct e1000_hw *hw)
8358 {
8359     int32_t timeout = PHY_CFG_TIMEOUT;
8360     uint32_t extcnf_ctrl;
8361
8362     DEBUGFUNC("e1000_get_software_flag");
8363
8364     if (hw->mac_type == e1000_ich8lan) {
8365         while (timeout) {
8366             extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
8367             extcnf_ctrl |= E1000_EXTCNF_CTRL_SWFLAG;
8368             E1000_WRITE_REG(hw, EXTCNF_CTRL, extcnf_ctrl);
8369
8370             extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
8371             if (extcnf_ctrl & E1000_EXTCNF_CTRL_SWFLAG)
8372                 break;
8373             msec_delay_irq(1);
8374             timeout--;
8375         }
8376
8377         if (!timeout) {
8378             DEBUGOUT("FW or HW locks the resource too long.\n");
8379             return -E1000_ERR_CONFIG;
8380         }
8381     }
8382
8383     return E1000_SUCCESS;
8384 }
8385
8386 /***************************************************************************
8387  *
8388  * Release software semaphore FLAG bit (SWFLAG).
8389  * SWFLAG is used to synchronize the access to all shared resource between
8390  * SW, FW and HW.
8391  *
8392  * hw: Struct containing variables accessed by shared code
8393  *
8394  ***************************************************************************/
8395 static void
8396 e1000_release_software_flag(struct e1000_hw *hw)
8397 {
8398     uint32_t extcnf_ctrl;
8399
8400     DEBUGFUNC("e1000_release_software_flag");
8401
8402     if (hw->mac_type == e1000_ich8lan) {
8403         extcnf_ctrl= E1000_READ_REG(hw, EXTCNF_CTRL);
8404         extcnf_ctrl &= ~E1000_EXTCNF_CTRL_SWFLAG;
8405         E1000_WRITE_REG(hw, EXTCNF_CTRL, extcnf_ctrl);
8406     }
8407
8408     return;
8409 }
8410
8411 /***************************************************************************
8412  *
8413  * Disable dynamic power down mode in ife PHY.
8414  * It can be used to workaround band-gap problem.
8415  *
8416  * hw: Struct containing variables accessed by shared code
8417  *
8418  ***************************************************************************/
8419 #if 0
8420 int32_t
8421 e1000_ife_disable_dynamic_power_down(struct e1000_hw *hw)
8422 {
8423     uint16_t phy_data;
8424     int32_t ret_val = E1000_SUCCESS;
8425
8426     DEBUGFUNC("e1000_ife_disable_dynamic_power_down");
8427
8428     if (hw->phy_type == e1000_phy_ife) {
8429         ret_val = e1000_read_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, &phy_data);
8430         if (ret_val)
8431             return ret_val;
8432
8433         phy_data |=  IFE_PSC_DISABLE_DYNAMIC_POWER_DOWN;
8434         ret_val = e1000_write_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, phy_data);
8435     }
8436
8437     return ret_val;
8438 }
8439 #endif  /*  0  */
8440
8441 /***************************************************************************
8442  *
8443  * Enable dynamic power down mode in ife PHY.
8444  * It can be used to workaround band-gap problem.
8445  *
8446  * hw: Struct containing variables accessed by shared code
8447  *
8448  ***************************************************************************/
8449 #if 0
8450 int32_t
8451 e1000_ife_enable_dynamic_power_down(struct e1000_hw *hw)
8452 {
8453     uint16_t phy_data;
8454     int32_t ret_val = E1000_SUCCESS;
8455
8456     DEBUGFUNC("e1000_ife_enable_dynamic_power_down");
8457
8458     if (hw->phy_type == e1000_phy_ife) {
8459         ret_val = e1000_read_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, &phy_data);
8460         if (ret_val)
8461             return ret_val;
8462
8463         phy_data &=  ~IFE_PSC_DISABLE_DYNAMIC_POWER_DOWN;
8464         ret_val = e1000_write_phy_reg(hw, IFE_PHY_SPECIAL_CONTROL, phy_data);
8465     }
8466
8467     return ret_val;
8468 }
8469 #endif  /*  0  */
8470
8471 /******************************************************************************
8472  * Reads a 16 bit word or words from the EEPROM using the ICH8's flash access
8473  * register.
8474  *
8475  * hw - Struct containing variables accessed by shared code
8476  * offset - offset of word in the EEPROM to read
8477  * data - word read from the EEPROM
8478  * words - number of words to read
8479  *****************************************************************************/
8480 static int32_t
8481 e1000_read_eeprom_ich8(struct e1000_hw *hw, uint16_t offset, uint16_t words,
8482                        uint16_t *data)
8483 {
8484     int32_t  error = E1000_SUCCESS;
8485     uint32_t flash_bank = 0;
8486     uint32_t act_offset = 0;
8487     uint32_t bank_offset = 0;
8488     uint16_t word = 0;
8489     uint16_t i = 0;
8490
8491     /* We need to know which is the valid flash bank.  In the event
8492      * that we didn't allocate eeprom_shadow_ram, we may not be
8493      * managing flash_bank.  So it cannot be trusted and needs
8494      * to be updated with each read.
8495      */
8496     /* Value of bit 22 corresponds to the flash bank we're on. */
8497     flash_bank = (E1000_READ_REG(hw, EECD) & E1000_EECD_SEC1VAL) ? 1 : 0;
8498
8499     /* Adjust offset appropriately if we're on bank 1 - adjust for word size */
8500     bank_offset = flash_bank * (hw->flash_bank_size * 2);
8501
8502     error = e1000_get_software_flag(hw);
8503     if (error != E1000_SUCCESS)
8504         return error;
8505
8506     for (i = 0; i < words; i++) {
8507         if (hw->eeprom_shadow_ram != NULL &&
8508             hw->eeprom_shadow_ram[offset+i].modified == TRUE) {
8509             data[i] = hw->eeprom_shadow_ram[offset+i].eeprom_word;
8510         } else {
8511             /* The NVM part needs a byte offset, hence * 2 */
8512             act_offset = bank_offset + ((offset + i) * 2);
8513             error = e1000_read_ich8_word(hw, act_offset, &word);
8514             if (error != E1000_SUCCESS)
8515                 break;
8516             data[i] = word;
8517         }
8518     }
8519
8520     e1000_release_software_flag(hw);
8521
8522     return error;
8523 }
8524
8525 /******************************************************************************
8526  * Writes a 16 bit word or words to the EEPROM using the ICH8's flash access
8527  * register.  Actually, writes are written to the shadow ram cache in the hw
8528  * structure hw->e1000_shadow_ram.  e1000_commit_shadow_ram flushes this to
8529  * the NVM, which occurs when the NVM checksum is updated.
8530  *
8531  * hw - Struct containing variables accessed by shared code
8532  * offset - offset of word in the EEPROM to write
8533  * words - number of words to write
8534  * data - words to write to the EEPROM
8535  *****************************************************************************/
8536 static int32_t
8537 e1000_write_eeprom_ich8(struct e1000_hw *hw, uint16_t offset, uint16_t words,
8538                         uint16_t *data)
8539 {
8540     uint32_t i = 0;
8541     int32_t error = E1000_SUCCESS;
8542
8543     error = e1000_get_software_flag(hw);
8544     if (error != E1000_SUCCESS)
8545         return error;
8546
8547     /* A driver can write to the NVM only if it has eeprom_shadow_ram
8548      * allocated.  Subsequent reads to the modified words are read from
8549      * this cached structure as well.  Writes will only go into this
8550      * cached structure unless it's followed by a call to
8551      * e1000_update_eeprom_checksum() where it will commit the changes
8552      * and clear the "modified" field.
8553      */
8554     if (hw->eeprom_shadow_ram != NULL) {
8555         for (i = 0; i < words; i++) {
8556             if ((offset + i) < E1000_SHADOW_RAM_WORDS) {
8557                 hw->eeprom_shadow_ram[offset+i].modified = TRUE;
8558                 hw->eeprom_shadow_ram[offset+i].eeprom_word = data[i];
8559             } else {
8560                 error = -E1000_ERR_EEPROM;
8561                 break;
8562             }
8563         }
8564     } else {
8565         /* Drivers have the option to not allocate eeprom_shadow_ram as long
8566          * as they don't perform any NVM writes.  An attempt in doing so
8567          * will result in this error.
8568          */
8569         error = -E1000_ERR_EEPROM;
8570     }
8571
8572     e1000_release_software_flag(hw);
8573
8574     return error;
8575 }
8576
8577 /******************************************************************************
8578  * This function does initial flash setup so that a new read/write/erase cycle
8579  * can be started.
8580  *
8581  * hw - The pointer to the hw structure
8582  ****************************************************************************/
8583 static int32_t
8584 e1000_ich8_cycle_init(struct e1000_hw *hw)
8585 {
8586     union ich8_hws_flash_status hsfsts;
8587     int32_t error = E1000_ERR_EEPROM;
8588     int32_t i     = 0;
8589
8590     DEBUGFUNC("e1000_ich8_cycle_init");
8591
8592     hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8593
8594     /* May be check the Flash Des Valid bit in Hw status */
8595     if (hsfsts.hsf_status.fldesvalid == 0) {
8596         DEBUGOUT("Flash descriptor invalid.  SW Sequencing must be used.");
8597         return error;
8598     }
8599
8600     /* Clear FCERR in Hw status by writing 1 */
8601     /* Clear DAEL in Hw status by writing a 1 */
8602     hsfsts.hsf_status.flcerr = 1;
8603     hsfsts.hsf_status.dael = 1;
8604
8605     E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFSTS, hsfsts.regval);
8606
8607     /* Either we should have a hardware SPI cycle in progress bit to check
8608      * against, in order to start a new cycle or FDONE bit should be changed
8609      * in the hardware so that it is 1 after harware reset, which can then be
8610      * used as an indication whether a cycle is in progress or has been
8611      * completed .. we should also have some software semaphore mechanism to
8612      * guard FDONE or the cycle in progress bit so that two threads access to
8613      * those bits can be sequentiallized or a way so that 2 threads dont
8614      * start the cycle at the same time */
8615
8616     if (hsfsts.hsf_status.flcinprog == 0) {
8617         /* There is no cycle running at present, so we can start a cycle */
8618         /* Begin by setting Flash Cycle Done. */
8619         hsfsts.hsf_status.flcdone = 1;
8620         E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFSTS, hsfsts.regval);
8621         error = E1000_SUCCESS;
8622     } else {
8623         /* otherwise poll for sometime so the current cycle has a chance
8624          * to end before giving up. */
8625         for (i = 0; i < ICH8_FLASH_COMMAND_TIMEOUT; i++) {
8626             hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8627             if (hsfsts.hsf_status.flcinprog == 0) {
8628                 error = E1000_SUCCESS;
8629                 break;
8630             }
8631             udelay(1);
8632         }
8633         if (error == E1000_SUCCESS) {
8634             /* Successful in waiting for previous cycle to timeout,
8635              * now set the Flash Cycle Done. */
8636             hsfsts.hsf_status.flcdone = 1;
8637             E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFSTS, hsfsts.regval);
8638         } else {
8639             DEBUGOUT("Flash controller busy, cannot get access");
8640         }
8641     }
8642     return error;
8643 }
8644
8645 /******************************************************************************
8646  * This function starts a flash cycle and waits for its completion
8647  *
8648  * hw - The pointer to the hw structure
8649  ****************************************************************************/
8650 static int32_t
8651 e1000_ich8_flash_cycle(struct e1000_hw *hw, uint32_t timeout)
8652 {
8653     union ich8_hws_flash_ctrl hsflctl;
8654     union ich8_hws_flash_status hsfsts;
8655     int32_t error = E1000_ERR_EEPROM;
8656     uint32_t i = 0;
8657
8658     /* Start a cycle by writing 1 in Flash Cycle Go in Hw Flash Control */
8659     hsflctl.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFCTL);
8660     hsflctl.hsf_ctrl.flcgo = 1;
8661     E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFCTL, hsflctl.regval);
8662
8663     /* wait till FDONE bit is set to 1 */
8664     do {
8665         hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8666         if (hsfsts.hsf_status.flcdone == 1)
8667             break;
8668         udelay(1);
8669         i++;
8670     } while (i < timeout);
8671     if (hsfsts.hsf_status.flcdone == 1 && hsfsts.hsf_status.flcerr == 0) {
8672         error = E1000_SUCCESS;
8673     }
8674     return error;
8675 }
8676
8677 /******************************************************************************
8678  * Reads a byte or word from the NVM using the ICH8 flash access registers.
8679  *
8680  * hw - The pointer to the hw structure
8681  * index - The index of the byte or word to read.
8682  * size - Size of data to read, 1=byte 2=word
8683  * data - Pointer to the word to store the value read.
8684  *****************************************************************************/
8685 static int32_t
8686 e1000_read_ich8_data(struct e1000_hw *hw, uint32_t index,
8687                      uint32_t size, uint16_t* data)
8688 {
8689     union ich8_hws_flash_status hsfsts;
8690     union ich8_hws_flash_ctrl hsflctl;
8691     uint32_t flash_linear_address;
8692     uint32_t flash_data = 0;
8693     int32_t error = -E1000_ERR_EEPROM;
8694     int32_t count = 0;
8695
8696     DEBUGFUNC("e1000_read_ich8_data");
8697
8698     if (size < 1  || size > 2 || data == 0x0 ||
8699         index > ICH8_FLASH_LINEAR_ADDR_MASK)
8700         return error;
8701
8702     flash_linear_address = (ICH8_FLASH_LINEAR_ADDR_MASK & index) +
8703                            hw->flash_base_addr;
8704
8705     do {
8706         udelay(1);
8707         /* Steps */
8708         error = e1000_ich8_cycle_init(hw);
8709         if (error != E1000_SUCCESS)
8710             break;
8711
8712         hsflctl.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFCTL);
8713         /* 0b/1b corresponds to 1 or 2 byte size, respectively. */
8714         hsflctl.hsf_ctrl.fldbcount = size - 1;
8715         hsflctl.hsf_ctrl.flcycle = ICH8_CYCLE_READ;
8716         E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFCTL, hsflctl.regval);
8717
8718         /* Write the last 24 bits of index into Flash Linear address field in
8719          * Flash Address */
8720         /* TODO: TBD maybe check the index against the size of flash */
8721
8722         E1000_WRITE_ICH8_REG(hw, ICH8_FLASH_FADDR, flash_linear_address);
8723
8724         error = e1000_ich8_flash_cycle(hw, ICH8_FLASH_COMMAND_TIMEOUT);
8725
8726         /* Check if FCERR is set to 1, if set to 1, clear it and try the whole
8727          * sequence a few more times, else read in (shift in) the Flash Data0,
8728          * the order is least significant byte first msb to lsb */
8729         if (error == E1000_SUCCESS) {
8730             flash_data = E1000_READ_ICH8_REG(hw, ICH8_FLASH_FDATA0);
8731             if (size == 1) {
8732                 *data = (uint8_t)(flash_data & 0x000000FF);
8733             } else if (size == 2) {
8734                 *data = (uint16_t)(flash_data & 0x0000FFFF);
8735             }
8736             break;
8737         } else {
8738             /* If we've gotten here, then things are probably completely hosed,
8739              * but if the error condition is detected, it won't hurt to give
8740              * it another try...ICH8_FLASH_CYCLE_REPEAT_COUNT times.
8741              */
8742             hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8743             if (hsfsts.hsf_status.flcerr == 1) {
8744                 /* Repeat for some time before giving up. */
8745                 continue;
8746             } else if (hsfsts.hsf_status.flcdone == 0) {
8747                 DEBUGOUT("Timeout error - flash cycle did not complete.");
8748                 break;
8749             }
8750         }
8751     } while (count++ < ICH8_FLASH_CYCLE_REPEAT_COUNT);
8752
8753     return error;
8754 }
8755
8756 /******************************************************************************
8757  * Writes One /two bytes to the NVM using the ICH8 flash access registers.
8758  *
8759  * hw - The pointer to the hw structure
8760  * index - The index of the byte/word to read.
8761  * size - Size of data to read, 1=byte 2=word
8762  * data - The byte(s) to write to the NVM.
8763  *****************************************************************************/
8764 static int32_t
8765 e1000_write_ich8_data(struct e1000_hw *hw, uint32_t index, uint32_t size,
8766                       uint16_t data)
8767 {
8768     union ich8_hws_flash_status hsfsts;
8769     union ich8_hws_flash_ctrl hsflctl;
8770     uint32_t flash_linear_address;
8771     uint32_t flash_data = 0;
8772     int32_t error = -E1000_ERR_EEPROM;
8773     int32_t count = 0;
8774
8775     DEBUGFUNC("e1000_write_ich8_data");
8776
8777     if (size < 1  || size > 2 || data > size * 0xff ||
8778         index > ICH8_FLASH_LINEAR_ADDR_MASK)
8779         return error;
8780
8781     flash_linear_address = (ICH8_FLASH_LINEAR_ADDR_MASK & index) +
8782                            hw->flash_base_addr;
8783
8784     do {
8785         udelay(1);
8786         /* Steps */
8787         error = e1000_ich8_cycle_init(hw);
8788         if (error != E1000_SUCCESS)
8789             break;
8790
8791         hsflctl.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFCTL);
8792         /* 0b/1b corresponds to 1 or 2 byte size, respectively. */
8793         hsflctl.hsf_ctrl.fldbcount = size -1;
8794         hsflctl.hsf_ctrl.flcycle = ICH8_CYCLE_WRITE;
8795         E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFCTL, hsflctl.regval);
8796
8797         /* Write the last 24 bits of index into Flash Linear address field in
8798          * Flash Address */
8799         E1000_WRITE_ICH8_REG(hw, ICH8_FLASH_FADDR, flash_linear_address);
8800
8801         if (size == 1)
8802             flash_data = (uint32_t)data & 0x00FF;
8803         else
8804             flash_data = (uint32_t)data;
8805
8806         E1000_WRITE_ICH8_REG(hw, ICH8_FLASH_FDATA0, flash_data);
8807
8808         /* check if FCERR is set to 1 , if set to 1, clear it and try the whole
8809          * sequence a few more times else done */
8810         error = e1000_ich8_flash_cycle(hw, ICH8_FLASH_COMMAND_TIMEOUT);
8811         if (error == E1000_SUCCESS) {
8812             break;
8813         } else {
8814             /* If we're here, then things are most likely completely hosed,
8815              * but if the error condition is detected, it won't hurt to give
8816              * it another try...ICH8_FLASH_CYCLE_REPEAT_COUNT times.
8817              */
8818             hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8819             if (hsfsts.hsf_status.flcerr == 1) {
8820                 /* Repeat for some time before giving up. */
8821                 continue;
8822             } else if (hsfsts.hsf_status.flcdone == 0) {
8823                 DEBUGOUT("Timeout error - flash cycle did not complete.");
8824                 break;
8825             }
8826         }
8827     } while (count++ < ICH8_FLASH_CYCLE_REPEAT_COUNT);
8828
8829     return error;
8830 }
8831
8832 /******************************************************************************
8833  * Reads a single byte from the NVM using the ICH8 flash access registers.
8834  *
8835  * hw - pointer to e1000_hw structure
8836  * index - The index of the byte to read.
8837  * data - Pointer to a byte to store the value read.
8838  *****************************************************************************/
8839 static int32_t
8840 e1000_read_ich8_byte(struct e1000_hw *hw, uint32_t index, uint8_t* data)
8841 {
8842     int32_t status = E1000_SUCCESS;
8843     uint16_t word = 0;
8844
8845     status = e1000_read_ich8_data(hw, index, 1, &word);
8846     if (status == E1000_SUCCESS) {
8847         *data = (uint8_t)word;
8848     }
8849
8850     return status;
8851 }
8852
8853 /******************************************************************************
8854  * Writes a single byte to the NVM using the ICH8 flash access registers.
8855  * Performs verification by reading back the value and then going through
8856  * a retry algorithm before giving up.
8857  *
8858  * hw - pointer to e1000_hw structure
8859  * index - The index of the byte to write.
8860  * byte - The byte to write to the NVM.
8861  *****************************************************************************/
8862 static int32_t
8863 e1000_verify_write_ich8_byte(struct e1000_hw *hw, uint32_t index, uint8_t byte)
8864 {
8865     int32_t error = E1000_SUCCESS;
8866     int32_t program_retries;
8867     uint8_t temp_byte;
8868
8869     e1000_write_ich8_byte(hw, index, byte);
8870     udelay(100);
8871
8872     for (program_retries = 0; program_retries < 100; program_retries++) {
8873         e1000_read_ich8_byte(hw, index, &temp_byte);
8874         if (temp_byte == byte)
8875             break;
8876         udelay(10);
8877         e1000_write_ich8_byte(hw, index, byte);
8878         udelay(100);
8879     }
8880     if (program_retries == 100)
8881         error = E1000_ERR_EEPROM;
8882
8883     return error;
8884 }
8885
8886 /******************************************************************************
8887  * Writes a single byte to the NVM using the ICH8 flash access registers.
8888  *
8889  * hw - pointer to e1000_hw structure
8890  * index - The index of the byte to read.
8891  * data - The byte to write to the NVM.
8892  *****************************************************************************/
8893 static int32_t
8894 e1000_write_ich8_byte(struct e1000_hw *hw, uint32_t index, uint8_t data)
8895 {
8896     int32_t status = E1000_SUCCESS;
8897     uint16_t word = (uint16_t)data;
8898
8899     status = e1000_write_ich8_data(hw, index, 1, word);
8900
8901     return status;
8902 }
8903
8904 /******************************************************************************
8905  * Reads a word from the NVM using the ICH8 flash access registers.
8906  *
8907  * hw - pointer to e1000_hw structure
8908  * index - The starting byte index of the word to read.
8909  * data - Pointer to a word to store the value read.
8910  *****************************************************************************/
8911 static int32_t
8912 e1000_read_ich8_word(struct e1000_hw *hw, uint32_t index, uint16_t *data)
8913 {
8914     int32_t status = E1000_SUCCESS;
8915     status = e1000_read_ich8_data(hw, index, 2, data);
8916     return status;
8917 }
8918
8919 /******************************************************************************
8920  * Writes a word to the NVM using the ICH8 flash access registers.
8921  *
8922  * hw - pointer to e1000_hw structure
8923  * index - The starting byte index of the word to read.
8924  * data - The word to write to the NVM.
8925  *****************************************************************************/
8926 #if 0
8927 int32_t
8928 e1000_write_ich8_word(struct e1000_hw *hw, uint32_t index, uint16_t data)
8929 {
8930     int32_t status = E1000_SUCCESS;
8931     status = e1000_write_ich8_data(hw, index, 2, data);
8932     return status;
8933 }
8934 #endif  /*  0  */
8935
8936 /******************************************************************************
8937  * Erases the bank specified. Each bank is a 4k block. Segments are 0 based.
8938  * segment N is 4096 * N + flash_reg_addr.
8939  *
8940  * hw - pointer to e1000_hw structure
8941  * segment - 0 for first segment, 1 for second segment, etc.
8942  *****************************************************************************/
8943 static int32_t
8944 e1000_erase_ich8_4k_segment(struct e1000_hw *hw, uint32_t segment)
8945 {
8946     union ich8_hws_flash_status hsfsts;
8947     union ich8_hws_flash_ctrl hsflctl;
8948     uint32_t flash_linear_address;
8949     int32_t  count = 0;
8950     int32_t  error = E1000_ERR_EEPROM;
8951     int32_t  iteration, seg_size;
8952     int32_t  sector_size;
8953     int32_t  j = 0;
8954     int32_t  error_flag = 0;
8955
8956     hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
8957
8958     /* Determine HW Sector size: Read BERASE bits of Hw flash Status register */
8959     /* 00: The Hw sector is 256 bytes, hence we need to erase 16
8960      *     consecutive sectors.  The start index for the nth Hw sector can be
8961      *     calculated as = segment * 4096 + n * 256
8962      * 01: The Hw sector is 4K bytes, hence we need to erase 1 sector.
8963      *     The start index for the nth Hw sector can be calculated
8964      *     as = segment * 4096
8965      * 10: Error condition
8966      * 11: The Hw sector size is much bigger than the size asked to
8967      *     erase...error condition */
8968     if (hsfsts.hsf_status.berasesz == 0x0) {
8969         /* Hw sector size 256 */
8970         sector_size = seg_size = ICH8_FLASH_SEG_SIZE_256;
8971         iteration = ICH8_FLASH_SECTOR_SIZE / ICH8_FLASH_SEG_SIZE_256;
8972     } else if (hsfsts.hsf_status.berasesz == 0x1) {
8973         sector_size = seg_size = ICH8_FLASH_SEG_SIZE_4K;
8974         iteration = 1;
8975     } else if (hsfsts.hsf_status.berasesz == 0x3) {
8976         sector_size = seg_size = ICH8_FLASH_SEG_SIZE_64K;
8977         iteration = 1;
8978     } else {
8979         return error;
8980     }
8981
8982     for (j = 0; j < iteration ; j++) {
8983         do {
8984             count++;
8985             /* Steps */
8986             error = e1000_ich8_cycle_init(hw);
8987             if (error != E1000_SUCCESS) {
8988                 error_flag = 1;
8989                 break;
8990             }
8991
8992             /* Write a value 11 (block Erase) in Flash Cycle field in Hw flash
8993              * Control */
8994             hsflctl.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFCTL);
8995             hsflctl.hsf_ctrl.flcycle = ICH8_CYCLE_ERASE;
8996             E1000_WRITE_ICH8_REG16(hw, ICH8_FLASH_HSFCTL, hsflctl.regval);
8997
8998             /* Write the last 24 bits of an index within the block into Flash
8999              * Linear address field in Flash Address.  This probably needs to
9000              * be calculated here based off the on-chip segment size and the
9001              * software segment size assumed (4K) */
9002             /* TBD */
9003             flash_linear_address = segment * sector_size + j * seg_size;
9004             flash_linear_address &= ICH8_FLASH_LINEAR_ADDR_MASK;
9005             flash_linear_address += hw->flash_base_addr;
9006
9007             E1000_WRITE_ICH8_REG(hw, ICH8_FLASH_FADDR, flash_linear_address);
9008
9009             error = e1000_ich8_flash_cycle(hw, 1000000);
9010             /* Check if FCERR is set to 1.  If 1, clear it and try the whole
9011              * sequence a few more times else Done */
9012             if (error == E1000_SUCCESS) {
9013                 break;
9014             } else {
9015                 hsfsts.regval = E1000_READ_ICH8_REG16(hw, ICH8_FLASH_HSFSTS);
9016                 if (hsfsts.hsf_status.flcerr == 1) {
9017                     /* repeat for some time before giving up */
9018                     continue;
9019                 } else if (hsfsts.hsf_status.flcdone == 0) {
9020                     error_flag = 1;
9021                     break;
9022                 }
9023             }
9024         } while ((count < ICH8_FLASH_CYCLE_REPEAT_COUNT) && !error_flag);
9025         if (error_flag == 1)
9026             break;
9027     }
9028     if (error_flag != 1)
9029         error = E1000_SUCCESS;
9030     return error;
9031 }
9032
9033 /******************************************************************************
9034  *
9035  * Reverse duplex setting without breaking the link.
9036  *
9037  * hw: Struct containing variables accessed by shared code
9038  *
9039  *****************************************************************************/
9040 #if 0
9041 int32_t
9042 e1000_duplex_reversal(struct e1000_hw *hw)
9043 {
9044     int32_t ret_val;
9045     uint16_t phy_data;
9046
9047     if (hw->phy_type != e1000_phy_igp_3)
9048         return E1000_SUCCESS;
9049
9050     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
9051     if (ret_val)
9052         return ret_val;
9053
9054     phy_data ^= MII_CR_FULL_DUPLEX;
9055
9056     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
9057     if (ret_val)
9058         return ret_val;
9059
9060     ret_val = e1000_read_phy_reg(hw, IGP3E1000_PHY_MISC_CTRL, &phy_data);
9061     if (ret_val)
9062         return ret_val;
9063
9064     phy_data |= IGP3_PHY_MISC_DUPLEX_MANUAL_SET;
9065     ret_val = e1000_write_phy_reg(hw, IGP3E1000_PHY_MISC_CTRL, phy_data);
9066
9067     return ret_val;
9068 }
9069 #endif  /*  0  */
9070
9071 static int32_t
9072 e1000_init_lcd_from_nvm_config_region(struct e1000_hw *hw,
9073                                       uint32_t cnf_base_addr, uint32_t cnf_size)
9074 {
9075     uint32_t ret_val = E1000_SUCCESS;
9076     uint16_t word_addr, reg_data, reg_addr;
9077     uint16_t i;
9078
9079     /* cnf_base_addr is in DWORD */
9080     word_addr = (uint16_t)(cnf_base_addr << 1);
9081
9082     /* cnf_size is returned in size of dwords */
9083     for (i = 0; i < cnf_size; i++) {
9084         ret_val = e1000_read_eeprom(hw, (word_addr + i*2), 1, &reg_data);
9085         if (ret_val)
9086             return ret_val;
9087
9088         ret_val = e1000_read_eeprom(hw, (word_addr + i*2 + 1), 1, &reg_addr);
9089         if (ret_val)
9090             return ret_val;
9091
9092         ret_val = e1000_get_software_flag(hw);
9093         if (ret_val != E1000_SUCCESS)
9094             return ret_val;
9095
9096         ret_val = e1000_write_phy_reg_ex(hw, (uint32_t)reg_addr, reg_data);
9097
9098         e1000_release_software_flag(hw);
9099     }
9100
9101     return ret_val;
9102 }
9103
9104
9105 static int32_t
9106 e1000_init_lcd_from_nvm(struct e1000_hw *hw)
9107 {
9108     uint32_t reg_data, cnf_base_addr, cnf_size, ret_val, loop;
9109
9110     if (hw->phy_type != e1000_phy_igp_3)
9111           return E1000_SUCCESS;
9112
9113     /* Check if SW needs configure the PHY */
9114     reg_data = E1000_READ_REG(hw, FEXTNVM);
9115     if (!(reg_data & FEXTNVM_SW_CONFIG))
9116         return E1000_SUCCESS;
9117
9118     /* Wait for basic configuration completes before proceeding*/
9119     loop = 0;
9120     do {
9121         reg_data = E1000_READ_REG(hw, STATUS) & E1000_STATUS_LAN_INIT_DONE;
9122         udelay(100);
9123         loop++;
9124     } while ((!reg_data) && (loop < 50));
9125
9126     /* Clear the Init Done bit for the next init event */
9127     reg_data = E1000_READ_REG(hw, STATUS);
9128     reg_data &= ~E1000_STATUS_LAN_INIT_DONE;
9129     E1000_WRITE_REG(hw, STATUS, reg_data);
9130
9131     /* Make sure HW does not configure LCD from PHY extended configuration
9132        before SW configuration */
9133     reg_data = E1000_READ_REG(hw, EXTCNF_CTRL);
9134     if ((reg_data & E1000_EXTCNF_CTRL_LCD_WRITE_ENABLE) == 0x0000) {
9135         reg_data = E1000_READ_REG(hw, EXTCNF_SIZE);
9136         cnf_size = reg_data & E1000_EXTCNF_SIZE_EXT_PCIE_LENGTH;
9137         cnf_size >>= 16;
9138         if (cnf_size) {
9139             reg_data = E1000_READ_REG(hw, EXTCNF_CTRL);
9140             cnf_base_addr = reg_data & E1000_EXTCNF_CTRL_EXT_CNF_POINTER;
9141             /* cnf_base_addr is in DWORD */
9142             cnf_base_addr >>= 16;
9143
9144             /* Configure LCD from extended configuration region. */
9145             ret_val = e1000_init_lcd_from_nvm_config_region(hw, cnf_base_addr,
9146                                                             cnf_size);
9147             if (ret_val)
9148                 return ret_val;
9149         }
9150     }
9151
9152     return E1000_SUCCESS;
9153 }
9154
9155
9156