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