]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/e1000/e1000_hw.c
5ee42c75adb160fabbdb7f025b19073f340a3c7f
[linux-2.6-omap-h63xx.git] / drivers / net / e1000 / e1000_hw.c
1 /*******************************************************************************
2
3   
4   Copyright(c) 1999 - 2005 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   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 /* e1000_hw.c
30  * Shared functions for accessing and configuring the MAC
31  */
32
33 #include "e1000_hw.h"
34
35 static int32_t e1000_set_phy_type(struct e1000_hw *hw);
36 static void e1000_phy_init_script(struct e1000_hw *hw);
37 static int32_t e1000_setup_copper_link(struct e1000_hw *hw);
38 static int32_t e1000_setup_fiber_serdes_link(struct e1000_hw *hw);
39 static int32_t e1000_adjust_serdes_amplitude(struct e1000_hw *hw);
40 static int32_t e1000_phy_force_speed_duplex(struct e1000_hw *hw);
41 static int32_t e1000_config_mac_to_phy(struct e1000_hw *hw);
42 static void e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
43 static void e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
44 static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data,
45                                      uint16_t count);
46 static uint16_t e1000_shift_in_mdi_bits(struct e1000_hw *hw);
47 static int32_t e1000_phy_reset_dsp(struct e1000_hw *hw);
48 static int32_t e1000_write_eeprom_spi(struct e1000_hw *hw, uint16_t offset,
49                                       uint16_t words, uint16_t *data);
50 static int32_t e1000_write_eeprom_microwire(struct e1000_hw *hw,
51                                             uint16_t offset, uint16_t words,
52                                             uint16_t *data);
53 static int32_t e1000_spi_eeprom_ready(struct e1000_hw *hw);
54 static void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
55 static void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
56 static void e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data,
57                                     uint16_t count);
58 static int32_t e1000_write_phy_reg_ex(struct e1000_hw *hw, uint32_t reg_addr,
59                                       uint16_t phy_data);
60 static int32_t e1000_read_phy_reg_ex(struct e1000_hw *hw,uint32_t reg_addr,
61                                      uint16_t *phy_data);
62 static uint16_t e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count);
63 static int32_t e1000_acquire_eeprom(struct e1000_hw *hw);
64 static void e1000_release_eeprom(struct e1000_hw *hw);
65 static void e1000_standby_eeprom(struct e1000_hw *hw);
66 static int32_t e1000_set_vco_speed(struct e1000_hw *hw);
67 static int32_t e1000_polarity_reversal_workaround(struct e1000_hw *hw);
68 static int32_t e1000_set_phy_mode(struct e1000_hw *hw);
69 static int32_t e1000_host_if_read_cookie(struct e1000_hw *hw, uint8_t *buffer);
70 static uint8_t e1000_calculate_mng_checksum(char *buffer, uint32_t length);
71 static uint8_t e1000_arc_subsystem_valid(struct e1000_hw *hw);
72 static int32_t e1000_check_downshift(struct e1000_hw *hw);
73 static int32_t e1000_check_polarity(struct e1000_hw *hw, uint16_t *polarity);
74 static void e1000_clear_hw_cntrs(struct e1000_hw *hw);
75 static void e1000_clear_vfta(struct e1000_hw *hw);
76 static int32_t e1000_commit_shadow_ram(struct e1000_hw *hw);
77 static int32_t e1000_config_dsp_after_link_change(struct e1000_hw *hw,
78                                                   boolean_t link_up);
79 static int32_t e1000_config_fc_after_link_up(struct e1000_hw *hw);
80 static int32_t e1000_detect_gig_phy(struct e1000_hw *hw);
81 static int32_t e1000_get_auto_rd_done(struct e1000_hw *hw);
82 static int32_t e1000_get_cable_length(struct e1000_hw *hw,
83                                       uint16_t *min_length,
84                                       uint16_t *max_length);
85 static int32_t e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw);
86 static int32_t e1000_get_phy_cfg_done(struct e1000_hw *hw);
87 static int32_t e1000_id_led_init(struct e1000_hw * hw);
88 static void e1000_init_rx_addrs(struct e1000_hw *hw);
89 static boolean_t e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw);
90 static int32_t e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd);
91 static void e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw);
92 static int32_t e1000_read_eeprom_eerd(struct e1000_hw *hw, uint16_t offset,
93                                       uint16_t words, uint16_t *data);
94 static int32_t e1000_set_d0_lplu_state(struct e1000_hw *hw, boolean_t active);
95 static int32_t e1000_set_d3_lplu_state(struct e1000_hw *hw, boolean_t active);
96 static int32_t e1000_wait_autoneg(struct e1000_hw *hw);
97
98 static void e1000_write_reg_io(struct e1000_hw *hw, uint32_t offset,
99                                uint32_t value);
100
101 #define E1000_WRITE_REG_IO(a, reg, val) \
102             e1000_write_reg_io((a), E1000_##reg, val)
103
104 /* IGP cable length table */
105 static const
106 uint16_t e1000_igp_cable_length_table[IGP01E1000_AGC_LENGTH_TABLE_SIZE] =
107     { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
108       5, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 25, 25, 25,
109       25, 25, 25, 25, 30, 30, 30, 30, 40, 40, 40, 40, 40, 40, 40, 40,
110       40, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 60, 60, 60,
111       60, 70, 70, 70, 70, 70, 70, 80, 80, 80, 80, 80, 80, 90, 90, 90,
112       90, 90, 90, 90, 90, 90, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
113       100, 100, 100, 100, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110,
114       110, 110, 110, 110, 110, 110, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120};
115
116 static const
117 uint16_t e1000_igp_2_cable_length_table[IGP02E1000_AGC_LENGTH_TABLE_SIZE] =
118     { 0, 0, 0, 0, 0, 0, 0, 0, 3, 5, 8, 11, 13, 16, 18, 21,
119       0, 0, 0, 3, 6, 10, 13, 16, 19, 23, 26, 29, 32, 35, 38, 41,
120       6, 10, 14, 18, 22, 26, 30, 33, 37, 41, 44, 48, 51, 54, 58, 61,
121       21, 26, 31, 35, 40, 44, 49, 53, 57, 61, 65, 68, 72, 75, 79, 82,
122       40, 45, 51, 56, 61, 66, 70, 75, 79, 83, 87, 91, 94, 98, 101, 104,
123       60, 66, 72, 77, 82, 87, 92, 96, 100, 104, 108, 111, 114, 117, 119, 121,
124       83, 89, 95, 100, 105, 109, 113, 116, 119, 122, 124,
125       104, 109, 114, 118, 121, 124};
126
127
128 /******************************************************************************
129  * Set the phy type member in the hw struct.
130  *
131  * hw - Struct containing variables accessed by shared code
132  *****************************************************************************/
133 int32_t
134 e1000_set_phy_type(struct e1000_hw *hw)
135 {
136     DEBUGFUNC("e1000_set_phy_type");
137
138     if(hw->mac_type == e1000_undefined)
139         return -E1000_ERR_PHY_TYPE;
140
141     switch(hw->phy_id) {
142     case M88E1000_E_PHY_ID:
143     case M88E1000_I_PHY_ID:
144     case M88E1011_I_PHY_ID:
145     case M88E1111_I_PHY_ID:
146         hw->phy_type = e1000_phy_m88;
147         break;
148     case IGP01E1000_I_PHY_ID:
149         if(hw->mac_type == e1000_82541 ||
150            hw->mac_type == e1000_82541_rev_2 ||
151            hw->mac_type == e1000_82547 ||
152            hw->mac_type == e1000_82547_rev_2) {
153             hw->phy_type = e1000_phy_igp;
154             break;
155         }
156         /* Fall Through */
157     default:
158         /* Should never have loaded on this device */
159         hw->phy_type = e1000_phy_undefined;
160         return -E1000_ERR_PHY_TYPE;
161     }
162
163     return E1000_SUCCESS;
164 }
165
166 /******************************************************************************
167  * IGP phy init script - initializes the GbE PHY
168  *
169  * hw - Struct containing variables accessed by shared code
170  *****************************************************************************/
171 static void
172 e1000_phy_init_script(struct e1000_hw *hw)
173 {
174     uint32_t ret_val;
175     uint16_t phy_saved_data;
176
177     DEBUGFUNC("e1000_phy_init_script");
178
179     if(hw->phy_init_script) {
180         msec_delay(20);
181
182         /* Save off the current value of register 0x2F5B to be restored at
183          * the end of this routine. */
184         ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
185
186         /* Disabled the PHY transmitter */
187         e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
188
189         msec_delay(20);
190
191         e1000_write_phy_reg(hw,0x0000,0x0140);
192
193         msec_delay(5);
194
195         switch(hw->mac_type) {
196         case e1000_82541:
197         case e1000_82547:
198             e1000_write_phy_reg(hw, 0x1F95, 0x0001);
199
200             e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
201
202             e1000_write_phy_reg(hw, 0x1F79, 0x0018);
203
204             e1000_write_phy_reg(hw, 0x1F30, 0x1600);
205
206             e1000_write_phy_reg(hw, 0x1F31, 0x0014);
207
208             e1000_write_phy_reg(hw, 0x1F32, 0x161C);
209
210             e1000_write_phy_reg(hw, 0x1F94, 0x0003);
211
212             e1000_write_phy_reg(hw, 0x1F96, 0x003F);
213
214             e1000_write_phy_reg(hw, 0x2010, 0x0008);
215             break;
216
217         case e1000_82541_rev_2:
218         case e1000_82547_rev_2:
219             e1000_write_phy_reg(hw, 0x1F73, 0x0099);
220             break;
221         default:
222             break;
223         }
224
225         e1000_write_phy_reg(hw, 0x0000, 0x3300);
226
227         msec_delay(20);
228
229         /* Now enable the transmitter */
230         e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
231
232         if(hw->mac_type == e1000_82547) {
233             uint16_t fused, fine, coarse;
234
235             /* Move to analog registers page */
236             e1000_read_phy_reg(hw, IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
237
238             if(!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
239                 e1000_read_phy_reg(hw, IGP01E1000_ANALOG_FUSE_STATUS, &fused);
240
241                 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
242                 coarse = fused & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
243
244                 if(coarse > IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
245                     coarse -= IGP01E1000_ANALOG_FUSE_COARSE_10;
246                     fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
247                 } else if(coarse == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
248                     fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
249
250                 fused = (fused & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
251                         (fine & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
252                         (coarse & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
253
254                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_CONTROL, fused);
255                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_BYPASS,
256                                     IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
257             }
258         }
259     }
260 }
261
262 /******************************************************************************
263  * Set the mac type member in the hw struct.
264  *
265  * hw - Struct containing variables accessed by shared code
266  *****************************************************************************/
267 int32_t
268 e1000_set_mac_type(struct e1000_hw *hw)
269 {
270     DEBUGFUNC("e1000_set_mac_type");
271
272     switch (hw->device_id) {
273     case E1000_DEV_ID_82542:
274         switch (hw->revision_id) {
275         case E1000_82542_2_0_REV_ID:
276             hw->mac_type = e1000_82542_rev2_0;
277             break;
278         case E1000_82542_2_1_REV_ID:
279             hw->mac_type = e1000_82542_rev2_1;
280             break;
281         default:
282             /* Invalid 82542 revision ID */
283             return -E1000_ERR_MAC_TYPE;
284         }
285         break;
286     case E1000_DEV_ID_82543GC_FIBER:
287     case E1000_DEV_ID_82543GC_COPPER:
288         hw->mac_type = e1000_82543;
289         break;
290     case E1000_DEV_ID_82544EI_COPPER:
291     case E1000_DEV_ID_82544EI_FIBER:
292     case E1000_DEV_ID_82544GC_COPPER:
293     case E1000_DEV_ID_82544GC_LOM:
294         hw->mac_type = e1000_82544;
295         break;
296     case E1000_DEV_ID_82540EM:
297     case E1000_DEV_ID_82540EM_LOM:
298     case E1000_DEV_ID_82540EP:
299     case E1000_DEV_ID_82540EP_LOM:
300     case E1000_DEV_ID_82540EP_LP:
301         hw->mac_type = e1000_82540;
302         break;
303     case E1000_DEV_ID_82545EM_COPPER:
304     case E1000_DEV_ID_82545EM_FIBER:
305         hw->mac_type = e1000_82545;
306         break;
307     case E1000_DEV_ID_82545GM_COPPER:
308     case E1000_DEV_ID_82545GM_FIBER:
309     case E1000_DEV_ID_82545GM_SERDES:
310         hw->mac_type = e1000_82545_rev_3;
311         break;
312     case E1000_DEV_ID_82546EB_COPPER:
313     case E1000_DEV_ID_82546EB_FIBER:
314     case E1000_DEV_ID_82546EB_QUAD_COPPER:
315         hw->mac_type = e1000_82546;
316         break;
317     case E1000_DEV_ID_82546GB_COPPER:
318     case E1000_DEV_ID_82546GB_FIBER:
319     case E1000_DEV_ID_82546GB_SERDES:
320     case E1000_DEV_ID_82546GB_PCIE:
321     case E1000_DEV_ID_82546GB_QUAD_COPPER:
322     case E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3:
323         hw->mac_type = e1000_82546_rev_3;
324         break;
325     case E1000_DEV_ID_82541EI:
326     case E1000_DEV_ID_82541EI_MOBILE:
327         hw->mac_type = e1000_82541;
328         break;
329     case E1000_DEV_ID_82541ER:
330     case E1000_DEV_ID_82541GI:
331     case E1000_DEV_ID_82541GI_LF:
332     case E1000_DEV_ID_82541GI_MOBILE:
333         hw->mac_type = e1000_82541_rev_2;
334         break;
335     case E1000_DEV_ID_82547EI:
336         hw->mac_type = e1000_82547;
337         break;
338     case E1000_DEV_ID_82547GI:
339         hw->mac_type = e1000_82547_rev_2;
340         break;
341     case E1000_DEV_ID_82571EB_COPPER:
342     case E1000_DEV_ID_82571EB_FIBER:
343     case E1000_DEV_ID_82571EB_SERDES:
344             hw->mac_type = e1000_82571;
345         break;
346     case E1000_DEV_ID_82572EI_COPPER:
347     case E1000_DEV_ID_82572EI_FIBER:
348     case E1000_DEV_ID_82572EI_SERDES:
349         hw->mac_type = e1000_82572;
350         break;
351     case E1000_DEV_ID_82573E:
352     case E1000_DEV_ID_82573E_IAMT:
353     case E1000_DEV_ID_82573L:
354         hw->mac_type = e1000_82573;
355         break;
356     default:
357         /* Should never have loaded on this device */
358         return -E1000_ERR_MAC_TYPE;
359     }
360
361     switch(hw->mac_type) {
362     case e1000_82571:
363     case e1000_82572:
364     case e1000_82573:
365         hw->eeprom_semaphore_present = TRUE;
366         /* fall through */
367     case e1000_82541:
368     case e1000_82547:
369     case e1000_82541_rev_2:
370     case e1000_82547_rev_2:
371         hw->asf_firmware_present = TRUE;
372         break;
373     default:
374         break;
375     }
376
377     return E1000_SUCCESS;
378 }
379
380 /*****************************************************************************
381  * Set media type and TBI compatibility.
382  *
383  * hw - Struct containing variables accessed by shared code
384  * **************************************************************************/
385 void
386 e1000_set_media_type(struct e1000_hw *hw)
387 {
388     uint32_t status;
389
390     DEBUGFUNC("e1000_set_media_type");
391
392     if(hw->mac_type != e1000_82543) {
393         /* tbi_compatibility is only valid on 82543 */
394         hw->tbi_compatibility_en = FALSE;
395     }
396
397     switch (hw->device_id) {
398     case E1000_DEV_ID_82545GM_SERDES:
399     case E1000_DEV_ID_82546GB_SERDES:
400     case E1000_DEV_ID_82571EB_SERDES:
401     case E1000_DEV_ID_82572EI_SERDES:
402         hw->media_type = e1000_media_type_internal_serdes;
403         break;
404     default:
405         switch (hw->mac_type) {
406         case e1000_82542_rev2_0:
407         case e1000_82542_rev2_1:
408             hw->media_type = e1000_media_type_fiber;
409             break;
410         case e1000_82573:
411             /* The STATUS_TBIMODE bit is reserved or reused for the this
412              * device.
413              */
414             hw->media_type = e1000_media_type_copper;
415             break;
416         default:
417             status = E1000_READ_REG(hw, STATUS);
418             if (status & E1000_STATUS_TBIMODE) {
419                 hw->media_type = e1000_media_type_fiber;
420                 /* tbi_compatibility not valid on fiber */
421                 hw->tbi_compatibility_en = FALSE;
422             } else {
423                 hw->media_type = e1000_media_type_copper;
424             }
425             break;
426         }
427     }
428 }
429
430 /******************************************************************************
431  * Reset the transmit and receive units; mask and clear all interrupts.
432  *
433  * hw - Struct containing variables accessed by shared code
434  *****************************************************************************/
435 int32_t
436 e1000_reset_hw(struct e1000_hw *hw)
437 {
438     uint32_t ctrl;
439     uint32_t ctrl_ext;
440     uint32_t icr;
441     uint32_t manc;
442     uint32_t led_ctrl;
443     uint32_t timeout;
444     uint32_t extcnf_ctrl;
445     int32_t ret_val;
446
447     DEBUGFUNC("e1000_reset_hw");
448
449     /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
450     if(hw->mac_type == e1000_82542_rev2_0) {
451         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
452         e1000_pci_clear_mwi(hw);
453     }
454
455     if(hw->bus_type == e1000_bus_type_pci_express) {
456         /* Prevent the PCI-E bus from sticking if there is no TLP connection
457          * on the last TLP read/write transaction when MAC is reset.
458          */
459         if(e1000_disable_pciex_master(hw) != E1000_SUCCESS) {
460             DEBUGOUT("PCI-E Master disable polling has failed.\n");
461         }
462     }
463
464     /* Clear interrupt mask to stop board from generating interrupts */
465     DEBUGOUT("Masking off all interrupts\n");
466     E1000_WRITE_REG(hw, IMC, 0xffffffff);
467
468     /* Disable the Transmit and Receive units.  Then delay to allow
469      * any pending transactions to complete before we hit the MAC with
470      * the global reset.
471      */
472     E1000_WRITE_REG(hw, RCTL, 0);
473     E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
474     E1000_WRITE_FLUSH(hw);
475
476     /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
477     hw->tbi_compatibility_on = FALSE;
478
479     /* Delay to allow any outstanding PCI transactions to complete before
480      * resetting the device
481      */
482     msec_delay(10);
483
484     ctrl = E1000_READ_REG(hw, CTRL);
485
486     /* Must reset the PHY before resetting the MAC */
487     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
488         E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_PHY_RST));
489         msec_delay(5);
490     }
491
492     /* Must acquire the MDIO ownership before MAC reset.
493      * Ownership defaults to firmware after a reset. */
494     if(hw->mac_type == e1000_82573) {
495         timeout = 10;
496
497         extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
498         extcnf_ctrl |= E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP;
499
500         do {
501             E1000_WRITE_REG(hw, EXTCNF_CTRL, extcnf_ctrl);
502             extcnf_ctrl = E1000_READ_REG(hw, EXTCNF_CTRL);
503
504             if(extcnf_ctrl & E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP)
505                 break;
506             else
507                 extcnf_ctrl |= E1000_EXTCNF_CTRL_MDIO_SW_OWNERSHIP;
508
509             msec_delay(2);
510             timeout--;
511         } while(timeout);
512     }
513
514     /* Issue a global reset to the MAC.  This will reset the chip's
515      * transmit, receive, DMA, and link units.  It will not effect
516      * the current PCI configuration.  The global reset bit is self-
517      * clearing, and should clear within a microsecond.
518      */
519     DEBUGOUT("Issuing a global reset to MAC\n");
520
521     switch(hw->mac_type) {
522         case e1000_82544:
523         case e1000_82540:
524         case e1000_82545:
525         case e1000_82546:
526         case e1000_82541:
527         case e1000_82541_rev_2:
528             /* These controllers can't ack the 64-bit write when issuing the
529              * reset, so use IO-mapping as a workaround to issue the reset */
530             E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
531             break;
532         case e1000_82545_rev_3:
533         case e1000_82546_rev_3:
534             /* Reset is performed on a shadow of the control register */
535             E1000_WRITE_REG(hw, CTRL_DUP, (ctrl | E1000_CTRL_RST));
536             break;
537         default:
538             E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
539             break;
540     }
541
542     /* After MAC reset, force reload of EEPROM to restore power-on settings to
543      * device.  Later controllers reload the EEPROM automatically, so just wait
544      * for reload to complete.
545      */
546     switch(hw->mac_type) {
547         case e1000_82542_rev2_0:
548         case e1000_82542_rev2_1:
549         case e1000_82543:
550         case e1000_82544:
551             /* Wait for reset to complete */
552             udelay(10);
553             ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
554             ctrl_ext |= E1000_CTRL_EXT_EE_RST;
555             E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
556             E1000_WRITE_FLUSH(hw);
557             /* Wait for EEPROM reload */
558             msec_delay(2);
559             break;
560         case e1000_82541:
561         case e1000_82541_rev_2:
562         case e1000_82547:
563         case e1000_82547_rev_2:
564             /* Wait for EEPROM reload */
565             msec_delay(20);
566             break;
567         case e1000_82573:
568             if (e1000_is_onboard_nvm_eeprom(hw) == FALSE) {
569                 udelay(10);
570                 ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
571                 ctrl_ext |= E1000_CTRL_EXT_EE_RST;
572                 E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
573                 E1000_WRITE_FLUSH(hw);
574             }
575             /* fall through */
576         case e1000_82571:
577         case e1000_82572:
578             ret_val = e1000_get_auto_rd_done(hw);
579             if(ret_val)
580                 /* We don't want to continue accessing MAC registers. */
581                 return ret_val;
582             break;
583         default:
584             /* Wait for EEPROM reload (it happens automatically) */
585             msec_delay(5);
586             break;
587     }
588
589     /* Disable HW ARPs on ASF enabled adapters */
590     if(hw->mac_type >= e1000_82540 && hw->mac_type <= e1000_82547_rev_2) {
591         manc = E1000_READ_REG(hw, MANC);
592         manc &= ~(E1000_MANC_ARP_EN);
593         E1000_WRITE_REG(hw, MANC, manc);
594     }
595
596     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
597         e1000_phy_init_script(hw);
598
599         /* Configure activity LED after PHY reset */
600         led_ctrl = E1000_READ_REG(hw, LEDCTL);
601         led_ctrl &= IGP_ACTIVITY_LED_MASK;
602         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
603         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
604     }
605
606     /* Clear interrupt mask to stop board from generating interrupts */
607     DEBUGOUT("Masking off all interrupts\n");
608     E1000_WRITE_REG(hw, IMC, 0xffffffff);
609
610     /* Clear any pending interrupt events. */
611     icr = E1000_READ_REG(hw, ICR);
612
613     /* If MWI was previously enabled, reenable it. */
614     if(hw->mac_type == e1000_82542_rev2_0) {
615         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
616             e1000_pci_set_mwi(hw);
617     }
618
619     return E1000_SUCCESS;
620 }
621
622 /******************************************************************************
623  * Performs basic configuration of the adapter.
624  *
625  * hw - Struct containing variables accessed by shared code
626  *
627  * Assumes that the controller has previously been reset and is in a
628  * post-reset uninitialized state. Initializes the receive address registers,
629  * multicast table, and VLAN filter table. Calls routines to setup link
630  * configuration and flow control settings. Clears all on-chip counters. Leaves
631  * the transmit and receive units disabled and uninitialized.
632  *****************************************************************************/
633 int32_t
634 e1000_init_hw(struct e1000_hw *hw)
635 {
636     uint32_t ctrl;
637     uint32_t i;
638     int32_t ret_val;
639     uint16_t pcix_cmd_word;
640     uint16_t pcix_stat_hi_word;
641     uint16_t cmd_mmrbc;
642     uint16_t stat_mmrbc;
643     uint32_t mta_size;
644     uint32_t ctrl_ext;
645
646     DEBUGFUNC("e1000_init_hw");
647
648     /* Initialize Identification LED */
649     ret_val = e1000_id_led_init(hw);
650     if(ret_val) {
651         DEBUGOUT("Error Initializing Identification LED\n");
652         return ret_val;
653     }
654
655     /* Set the media type and TBI compatibility */
656     e1000_set_media_type(hw);
657
658     /* Disabling VLAN filtering. */
659     DEBUGOUT("Initializing the IEEE VLAN\n");
660     if (hw->mac_type < e1000_82545_rev_3)
661         E1000_WRITE_REG(hw, VET, 0);
662     e1000_clear_vfta(hw);
663
664     /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
665     if(hw->mac_type == e1000_82542_rev2_0) {
666         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
667         e1000_pci_clear_mwi(hw);
668         E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
669         E1000_WRITE_FLUSH(hw);
670         msec_delay(5);
671     }
672
673     /* Setup the receive address. This involves initializing all of the Receive
674      * Address Registers (RARs 0 - 15).
675      */
676     e1000_init_rx_addrs(hw);
677
678     /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
679     if(hw->mac_type == e1000_82542_rev2_0) {
680         E1000_WRITE_REG(hw, RCTL, 0);
681         E1000_WRITE_FLUSH(hw);
682         msec_delay(1);
683         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
684             e1000_pci_set_mwi(hw);
685     }
686
687     /* Zero out the Multicast HASH table */
688     DEBUGOUT("Zeroing the MTA\n");
689     mta_size = E1000_MC_TBL_SIZE;
690     for(i = 0; i < mta_size; i++)
691         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
692
693     /* Set the PCI priority bit correctly in the CTRL register.  This
694      * determines if the adapter gives priority to receives, or if it
695      * gives equal priority to transmits and receives.  Valid only on
696      * 82542 and 82543 silicon.
697      */
698     if(hw->dma_fairness && hw->mac_type <= e1000_82543) {
699         ctrl = E1000_READ_REG(hw, CTRL);
700         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PRIOR);
701     }
702
703     switch(hw->mac_type) {
704     case e1000_82545_rev_3:
705     case e1000_82546_rev_3:
706         break;
707     default:
708         /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
709         if(hw->bus_type == e1000_bus_type_pcix) {
710             e1000_read_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd_word);
711             e1000_read_pci_cfg(hw, PCIX_STATUS_REGISTER_HI,
712                 &pcix_stat_hi_word);
713             cmd_mmrbc = (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
714                 PCIX_COMMAND_MMRBC_SHIFT;
715             stat_mmrbc = (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
716                 PCIX_STATUS_HI_MMRBC_SHIFT;
717             if(stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
718                 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
719             if(cmd_mmrbc > stat_mmrbc) {
720                 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
721                 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
722                 e1000_write_pci_cfg(hw, PCIX_COMMAND_REGISTER,
723                     &pcix_cmd_word);
724             }
725         }
726         break;
727     }
728
729     /* Call a subroutine to configure the link and setup flow control. */
730     ret_val = e1000_setup_link(hw);
731
732     /* Set the transmit descriptor write-back policy */
733     if(hw->mac_type > e1000_82544) {
734         ctrl = E1000_READ_REG(hw, TXDCTL);
735         ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
736         switch (hw->mac_type) {
737         default:
738             break;
739         case e1000_82571:
740         case e1000_82572:
741         case e1000_82573:
742             ctrl |= E1000_TXDCTL_COUNT_DESC;
743             break;
744         }
745         E1000_WRITE_REG(hw, TXDCTL, ctrl);
746     }
747
748     if (hw->mac_type == e1000_82573) {
749         e1000_enable_tx_pkt_filtering(hw); 
750     }
751
752     switch (hw->mac_type) {
753     default:
754         break;
755     case e1000_82571:
756     case e1000_82572:
757         ctrl = E1000_READ_REG(hw, TXDCTL1);
758         ctrl &= ~E1000_TXDCTL_WTHRESH;
759         ctrl |= E1000_TXDCTL_COUNT_DESC | E1000_TXDCTL_FULL_TX_DESC_WB;
760         ctrl |= (1 << 22);
761         E1000_WRITE_REG(hw, TXDCTL1, ctrl);
762         break;
763     }
764
765
766
767     if (hw->mac_type == e1000_82573) {
768         uint32_t gcr = E1000_READ_REG(hw, GCR);
769         gcr |= E1000_GCR_L1_ACT_WITHOUT_L0S_RX;
770         E1000_WRITE_REG(hw, GCR, gcr);
771     }
772
773     /* Clear all of the statistics registers (clear on read).  It is
774      * important that we do this after we have tried to establish link
775      * because the symbol error count will increment wildly if there
776      * is no link.
777      */
778     e1000_clear_hw_cntrs(hw);
779
780     if (hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER ||
781         hw->device_id == E1000_DEV_ID_82546GB_QUAD_COPPER_KSP3) {
782         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
783         /* Relaxed ordering must be disabled to avoid a parity
784          * error crash in a PCI slot. */
785         ctrl_ext |= E1000_CTRL_EXT_RO_DIS;
786         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
787     }
788
789     return ret_val;
790 }
791
792 /******************************************************************************
793  * Adjust SERDES output amplitude based on EEPROM setting.
794  *
795  * hw - Struct containing variables accessed by shared code.
796  *****************************************************************************/
797 static int32_t
798 e1000_adjust_serdes_amplitude(struct e1000_hw *hw)
799 {
800     uint16_t eeprom_data;
801     int32_t  ret_val;
802
803     DEBUGFUNC("e1000_adjust_serdes_amplitude");
804
805     if(hw->media_type != e1000_media_type_internal_serdes)
806         return E1000_SUCCESS;
807
808     switch(hw->mac_type) {
809     case e1000_82545_rev_3:
810     case e1000_82546_rev_3:
811         break;
812     default:
813         return E1000_SUCCESS;
814     }
815
816     ret_val = e1000_read_eeprom(hw, EEPROM_SERDES_AMPLITUDE, 1, &eeprom_data);
817     if (ret_val) {
818         return ret_val;
819     }
820
821     if(eeprom_data != EEPROM_RESERVED_WORD) {
822         /* Adjust SERDES output amplitude only. */
823         eeprom_data &= EEPROM_SERDES_AMPLITUDE_MASK; 
824         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_EXT_CTRL, eeprom_data);
825         if(ret_val)
826             return ret_val;
827     }
828
829     return E1000_SUCCESS;
830 }
831
832 /******************************************************************************
833  * Configures flow control and link settings.
834  *
835  * hw - Struct containing variables accessed by shared code
836  *
837  * Determines which flow control settings to use. Calls the apropriate media-
838  * specific link configuration function. Configures the flow control settings.
839  * Assuming the adapter has a valid link partner, a valid link should be
840  * established. Assumes the hardware has previously been reset and the
841  * transmitter and receiver are not enabled.
842  *****************************************************************************/
843 int32_t
844 e1000_setup_link(struct e1000_hw *hw)
845 {
846     uint32_t ctrl_ext;
847     int32_t ret_val;
848     uint16_t eeprom_data;
849
850     DEBUGFUNC("e1000_setup_link");
851
852     /* In the case of the phy reset being blocked, we already have a link.
853      * We do not have to set it up again. */
854     if (e1000_check_phy_reset_block(hw))
855         return E1000_SUCCESS;
856
857     /* Read and store word 0x0F of the EEPROM. This word contains bits
858      * that determine the hardware's default PAUSE (flow control) mode,
859      * a bit that determines whether the HW defaults to enabling or
860      * disabling auto-negotiation, and the direction of the
861      * SW defined pins. If there is no SW over-ride of the flow
862      * control setting, then the variable hw->fc will
863      * be initialized based on a value in the EEPROM.
864      */
865     if (hw->fc == e1000_fc_default) {
866         switch (hw->mac_type) {
867         case e1000_82573:
868             hw->fc = e1000_fc_full;
869             break;
870         default:
871             ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
872                                         1, &eeprom_data);
873             if (ret_val) {
874                 DEBUGOUT("EEPROM Read Error\n");
875                 return -E1000_ERR_EEPROM;
876             }
877             if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
878                 hw->fc = e1000_fc_none;
879             else if ((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
880                     EEPROM_WORD0F_ASM_DIR)
881                 hw->fc = e1000_fc_tx_pause;
882             else
883                 hw->fc = e1000_fc_full;
884             break;
885         }
886     }
887
888     /* We want to save off the original Flow Control configuration just
889      * in case we get disconnected and then reconnected into a different
890      * hub or switch with different Flow Control capabilities.
891      */
892     if(hw->mac_type == e1000_82542_rev2_0)
893         hw->fc &= (~e1000_fc_tx_pause);
894
895     if((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
896         hw->fc &= (~e1000_fc_rx_pause);
897
898     hw->original_fc = hw->fc;
899
900     DEBUGOUT1("After fix-ups FlowControl is now = %x\n", hw->fc);
901
902     /* Take the 4 bits from EEPROM word 0x0F that determine the initial
903      * polarity value for the SW controlled pins, and setup the
904      * Extended Device Control reg with that info.
905      * This is needed because one of the SW controlled pins is used for
906      * signal detection.  So this should be done before e1000_setup_pcs_link()
907      * or e1000_phy_setup() is called.
908      */
909     if (hw->mac_type == e1000_82543) {
910                 ret_val = e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG,
911                                                                         1, &eeprom_data);
912                 if (ret_val) {
913                         DEBUGOUT("EEPROM Read Error\n");
914                         return -E1000_ERR_EEPROM;
915                 }
916         ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
917                     SWDPIO__EXT_SHIFT);
918         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
919     }
920
921     /* Call the necessary subroutine to configure the link. */
922     ret_val = (hw->media_type == e1000_media_type_copper) ?
923               e1000_setup_copper_link(hw) :
924               e1000_setup_fiber_serdes_link(hw);
925
926     /* Initialize the flow control address, type, and PAUSE timer
927      * registers to their default values.  This is done even if flow
928      * control is disabled, because it does not hurt anything to
929      * initialize these registers.
930      */
931     DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
932
933     E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
934     E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
935     E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
936
937     E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
938
939     /* Set the flow control receive threshold registers.  Normally,
940      * these registers will be set to a default threshold that may be
941      * adjusted later by the driver's runtime code.  However, if the
942      * ability to transmit pause frames in not enabled, then these
943      * registers will be set to 0.
944      */
945     if(!(hw->fc & e1000_fc_tx_pause)) {
946         E1000_WRITE_REG(hw, FCRTL, 0);
947         E1000_WRITE_REG(hw, FCRTH, 0);
948     } else {
949         /* We need to set up the Receive Threshold high and low water marks
950          * as well as (optionally) enabling the transmission of XON frames.
951          */
952         if(hw->fc_send_xon) {
953             E1000_WRITE_REG(hw, FCRTL, (hw->fc_low_water | E1000_FCRTL_XONE));
954             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
955         } else {
956             E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
957             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
958         }
959     }
960     return ret_val;
961 }
962
963 /******************************************************************************
964  * Sets up link for a fiber based or serdes based adapter
965  *
966  * hw - Struct containing variables accessed by shared code
967  *
968  * Manipulates Physical Coding Sublayer functions in order to configure
969  * link. Assumes the hardware has been previously reset and the transmitter
970  * and receiver are not enabled.
971  *****************************************************************************/
972 static int32_t
973 e1000_setup_fiber_serdes_link(struct e1000_hw *hw)
974 {
975     uint32_t ctrl;
976     uint32_t status;
977     uint32_t txcw = 0;
978     uint32_t i;
979     uint32_t signal = 0;
980     int32_t ret_val;
981
982     DEBUGFUNC("e1000_setup_fiber_serdes_link");
983
984     /* On 82571 and 82572 Fiber connections, SerDes loopback mode persists
985      * until explicitly turned off or a power cycle is performed.  A read to
986      * the register does not indicate its status.  Therefore, we ensure
987      * loopback mode is disabled during initialization.
988      */
989     if (hw->mac_type == e1000_82571 || hw->mac_type == e1000_82572)
990         E1000_WRITE_REG(hw, SCTL, E1000_DISABLE_SERDES_LOOPBACK);
991
992     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
993      * set when the optics detect a signal. On older adapters, it will be
994      * cleared when there is a signal.  This applies to fiber media only.
995      * If we're on serdes media, adjust the output amplitude to value set in
996      * the EEPROM.
997      */
998     ctrl = E1000_READ_REG(hw, CTRL);
999     if(hw->media_type == e1000_media_type_fiber)
1000         signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
1001
1002     ret_val = e1000_adjust_serdes_amplitude(hw);
1003     if(ret_val)
1004         return ret_val;
1005
1006     /* Take the link out of reset */
1007     ctrl &= ~(E1000_CTRL_LRST);
1008
1009     /* Adjust VCO speed to improve BER performance */
1010     ret_val = e1000_set_vco_speed(hw);
1011     if(ret_val)
1012         return ret_val;
1013
1014     e1000_config_collision_dist(hw);
1015
1016     /* Check for a software override of the flow control settings, and setup
1017      * the device accordingly.  If auto-negotiation is enabled, then software
1018      * will have to set the "PAUSE" bits to the correct value in the Tranmsit
1019      * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
1020      * auto-negotiation is disabled, then software will have to manually
1021      * configure the two flow control enable bits in the CTRL register.
1022      *
1023      * The possible values of the "fc" parameter are:
1024      *      0:  Flow control is completely disabled
1025      *      1:  Rx flow control is enabled (we can receive pause frames, but
1026      *          not send pause frames).
1027      *      2:  Tx flow control is enabled (we can send pause frames but we do
1028      *          not support receiving pause frames).
1029      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1030      */
1031     switch (hw->fc) {
1032     case e1000_fc_none:
1033         /* Flow control is completely disabled by a software over-ride. */
1034         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
1035         break;
1036     case e1000_fc_rx_pause:
1037         /* RX Flow control is enabled and TX Flow control is disabled by a
1038          * software over-ride. Since there really isn't a way to advertise
1039          * that we are capable of RX Pause ONLY, we will advertise that we
1040          * support both symmetric and asymmetric RX PAUSE. Later, we will
1041          *  disable the adapter's ability to send PAUSE frames.
1042          */
1043         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
1044         break;
1045     case e1000_fc_tx_pause:
1046         /* TX Flow control is enabled, and RX Flow control is disabled, by a
1047          * software over-ride.
1048          */
1049         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
1050         break;
1051     case e1000_fc_full:
1052         /* Flow control (both RX and TX) is enabled by a software over-ride. */
1053         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
1054         break;
1055     default:
1056         DEBUGOUT("Flow control param set incorrectly\n");
1057         return -E1000_ERR_CONFIG;
1058         break;
1059     }
1060
1061     /* Since auto-negotiation is enabled, take the link out of reset (the link
1062      * will be in reset, because we previously reset the chip). This will
1063      * restart auto-negotiation.  If auto-neogtiation is successful then the
1064      * link-up status bit will be set and the flow control enable bits (RFCE
1065      * and TFCE) will be set according to their negotiated value.
1066      */
1067     DEBUGOUT("Auto-negotiation enabled\n");
1068
1069     E1000_WRITE_REG(hw, TXCW, txcw);
1070     E1000_WRITE_REG(hw, CTRL, ctrl);
1071     E1000_WRITE_FLUSH(hw);
1072
1073     hw->txcw = txcw;
1074     msec_delay(1);
1075
1076     /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
1077      * indication in the Device Status Register.  Time-out if a link isn't
1078      * seen in 500 milliseconds seconds (Auto-negotiation should complete in
1079      * less than 500 milliseconds even if the other end is doing it in SW).
1080      * For internal serdes, we just assume a signal is present, then poll.
1081      */
1082     if(hw->media_type == e1000_media_type_internal_serdes ||
1083        (E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
1084         DEBUGOUT("Looking for Link\n");
1085         for(i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
1086             msec_delay(10);
1087             status = E1000_READ_REG(hw, STATUS);
1088             if(status & E1000_STATUS_LU) break;
1089         }
1090         if(i == (LINK_UP_TIMEOUT / 10)) {
1091             DEBUGOUT("Never got a valid link from auto-neg!!!\n");
1092             hw->autoneg_failed = 1;
1093             /* AutoNeg failed to achieve a link, so we'll call
1094              * e1000_check_for_link. This routine will force the link up if
1095              * we detect a signal. This will allow us to communicate with
1096              * non-autonegotiating link partners.
1097              */
1098             ret_val = e1000_check_for_link(hw);
1099             if(ret_val) {
1100                 DEBUGOUT("Error while checking for link\n");
1101                 return ret_val;
1102             }
1103             hw->autoneg_failed = 0;
1104         } else {
1105             hw->autoneg_failed = 0;
1106             DEBUGOUT("Valid Link Found\n");
1107         }
1108     } else {
1109         DEBUGOUT("No Signal Detected\n");
1110     }
1111     return E1000_SUCCESS;
1112 }
1113
1114 /******************************************************************************
1115 * Make sure we have a valid PHY and change PHY mode before link setup.
1116 *
1117 * hw - Struct containing variables accessed by shared code
1118 ******************************************************************************/
1119 static int32_t
1120 e1000_copper_link_preconfig(struct e1000_hw *hw)
1121 {
1122     uint32_t ctrl;
1123     int32_t ret_val;
1124     uint16_t phy_data;
1125
1126     DEBUGFUNC("e1000_copper_link_preconfig");
1127
1128     ctrl = E1000_READ_REG(hw, CTRL);
1129     /* With 82543, we need to force speed and duplex on the MAC equal to what
1130      * the PHY speed and duplex configuration is. In addition, we need to
1131      * perform a hardware reset on the PHY to take it out of reset.
1132      */
1133     if(hw->mac_type > e1000_82543) {
1134         ctrl |= E1000_CTRL_SLU;
1135         ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1136         E1000_WRITE_REG(hw, CTRL, ctrl);
1137     } else {
1138         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
1139         E1000_WRITE_REG(hw, CTRL, ctrl);
1140         ret_val = e1000_phy_hw_reset(hw);
1141         if(ret_val)
1142             return ret_val;
1143     }
1144
1145     /* Make sure we have a valid PHY */
1146     ret_val = e1000_detect_gig_phy(hw);
1147     if(ret_val) {
1148         DEBUGOUT("Error, did not detect valid phy.\n");
1149         return ret_val;
1150     }
1151     DEBUGOUT1("Phy ID = %x \n", hw->phy_id);
1152
1153     /* Set PHY to class A mode (if necessary) */
1154     ret_val = e1000_set_phy_mode(hw);
1155     if(ret_val)
1156         return ret_val;
1157
1158     if((hw->mac_type == e1000_82545_rev_3) ||
1159        (hw->mac_type == e1000_82546_rev_3)) {
1160         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1161         phy_data |= 0x00000008;
1162         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1163     }
1164
1165     if(hw->mac_type <= e1000_82543 ||
1166        hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
1167        hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2)
1168         hw->phy_reset_disable = FALSE;
1169
1170    return E1000_SUCCESS;
1171 }
1172
1173
1174 /********************************************************************
1175 * Copper link setup for e1000_phy_igp series.
1176 *
1177 * hw - Struct containing variables accessed by shared code
1178 *********************************************************************/
1179 static int32_t
1180 e1000_copper_link_igp_setup(struct e1000_hw *hw)
1181 {
1182     uint32_t led_ctrl;
1183     int32_t ret_val;
1184     uint16_t phy_data;
1185
1186     DEBUGFUNC("e1000_copper_link_igp_setup");
1187
1188     if (hw->phy_reset_disable)
1189         return E1000_SUCCESS;
1190     
1191     ret_val = e1000_phy_reset(hw);
1192     if (ret_val) {
1193         DEBUGOUT("Error Resetting the PHY\n");
1194         return ret_val;
1195     }
1196
1197     /* Wait 10ms for MAC to configure PHY from eeprom settings */
1198     msec_delay(15);
1199
1200     /* Configure activity LED after PHY reset */
1201     led_ctrl = E1000_READ_REG(hw, LEDCTL);
1202     led_ctrl &= IGP_ACTIVITY_LED_MASK;
1203     led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
1204     E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
1205
1206     /* disable lplu d3 during driver init */
1207     ret_val = e1000_set_d3_lplu_state(hw, FALSE);
1208     if (ret_val) {
1209         DEBUGOUT("Error Disabling LPLU D3\n");
1210         return ret_val;
1211     }
1212
1213     /* disable lplu d0 during driver init */
1214     ret_val = e1000_set_d0_lplu_state(hw, FALSE);
1215     if (ret_val) {
1216         DEBUGOUT("Error Disabling LPLU D0\n");
1217         return ret_val;
1218     }
1219     /* Configure mdi-mdix settings */
1220     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1221     if (ret_val)
1222         return ret_val;
1223
1224     if ((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
1225         hw->dsp_config_state = e1000_dsp_config_disabled;
1226         /* Force MDI for earlier revs of the IGP PHY */
1227         phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX | IGP01E1000_PSCR_FORCE_MDI_MDIX);
1228         hw->mdix = 1;
1229
1230     } else {
1231         hw->dsp_config_state = e1000_dsp_config_enabled;
1232         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1233
1234         switch (hw->mdix) {
1235         case 1:
1236             phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1237             break;
1238         case 2:
1239             phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
1240             break;
1241         case 0:
1242         default:
1243             phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
1244             break;
1245         }
1246     }
1247     ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1248     if(ret_val)
1249         return ret_val;
1250
1251     /* set auto-master slave resolution settings */
1252     if(hw->autoneg) {
1253         e1000_ms_type phy_ms_setting = hw->master_slave;
1254
1255         if(hw->ffe_config_state == e1000_ffe_config_active)
1256             hw->ffe_config_state = e1000_ffe_config_enabled;
1257
1258         if(hw->dsp_config_state == e1000_dsp_config_activated)
1259             hw->dsp_config_state = e1000_dsp_config_enabled;
1260
1261         /* when autonegotiation advertisment is only 1000Mbps then we
1262           * should disable SmartSpeed and enable Auto MasterSlave
1263           * resolution as hardware default. */
1264         if(hw->autoneg_advertised == ADVERTISE_1000_FULL) {
1265             /* Disable SmartSpeed */
1266             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
1267             if(ret_val)
1268                 return ret_val;
1269             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1270             ret_val = e1000_write_phy_reg(hw,
1271                                                   IGP01E1000_PHY_PORT_CONFIG,
1272                                                   phy_data);
1273             if(ret_val)
1274                 return ret_val;
1275             /* Set auto Master/Slave resolution process */
1276             ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1277             if(ret_val)
1278                 return ret_val;
1279             phy_data &= ~CR_1000T_MS_ENABLE;
1280             ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1281             if(ret_val)
1282                 return ret_val;
1283         }
1284
1285         ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1286         if(ret_val)
1287             return ret_val;
1288
1289         /* load defaults for future use */
1290         hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
1291                                         ((phy_data & CR_1000T_MS_VALUE) ?
1292                                          e1000_ms_force_master :
1293                                          e1000_ms_force_slave) :
1294                                          e1000_ms_auto;
1295
1296         switch (phy_ms_setting) {
1297         case e1000_ms_force_master:
1298             phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
1299             break;
1300         case e1000_ms_force_slave:
1301             phy_data |= CR_1000T_MS_ENABLE;
1302             phy_data &= ~(CR_1000T_MS_VALUE);
1303             break;
1304         case e1000_ms_auto:
1305             phy_data &= ~CR_1000T_MS_ENABLE;
1306             default:
1307             break;
1308         }
1309         ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1310         if(ret_val)
1311             return ret_val;
1312     }
1313
1314     return E1000_SUCCESS;
1315 }
1316
1317
1318 /********************************************************************
1319 * Copper link setup for e1000_phy_m88 series.
1320 *
1321 * hw - Struct containing variables accessed by shared code
1322 *********************************************************************/
1323 static int32_t
1324 e1000_copper_link_mgp_setup(struct e1000_hw *hw)
1325 {
1326     int32_t ret_val;
1327     uint16_t phy_data;
1328
1329     DEBUGFUNC("e1000_copper_link_mgp_setup");
1330
1331     if(hw->phy_reset_disable)
1332         return E1000_SUCCESS;
1333     
1334     /* Enable CRS on TX. This must be set for half-duplex operation. */
1335     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1336     if(ret_val)
1337         return ret_val;
1338
1339     phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1340
1341     /* Options:
1342      *   MDI/MDI-X = 0 (default)
1343      *   0 - Auto for all speeds
1344      *   1 - MDI mode
1345      *   2 - MDI-X mode
1346      *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1347      */
1348     phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1349
1350     switch (hw->mdix) {
1351     case 1:
1352         phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1353         break;
1354     case 2:
1355         phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1356         break;
1357     case 3:
1358         phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1359         break;
1360     case 0:
1361     default:
1362         phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1363         break;
1364     }
1365
1366     /* Options:
1367      *   disable_polarity_correction = 0 (default)
1368      *       Automatic Correction for Reversed Cable Polarity
1369      *   0 - Disabled
1370      *   1 - Enabled
1371      */
1372     phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1373     if(hw->disable_polarity_correction == 1)
1374         phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1375         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1376         if(ret_val)
1377             return ret_val;
1378
1379     /* Force TX_CLK in the Extended PHY Specific Control Register
1380      * to 25MHz clock.
1381      */
1382     ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1383     if(ret_val)
1384         return ret_val;
1385
1386     phy_data |= M88E1000_EPSCR_TX_CLK_25;
1387
1388     if (hw->phy_revision < M88E1011_I_REV_4) {
1389         /* Configure Master and Slave downshift values */
1390         phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
1391                               M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
1392         phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
1393                              M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
1394         ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1395         if(ret_val)
1396             return ret_val;
1397     }
1398
1399     /* SW Reset the PHY so all changes take effect */
1400     ret_val = e1000_phy_reset(hw);
1401     if(ret_val) {
1402         DEBUGOUT("Error Resetting the PHY\n");
1403         return ret_val;
1404     }
1405
1406    return E1000_SUCCESS;
1407 }
1408
1409 /********************************************************************
1410 * Setup auto-negotiation and flow control advertisements,
1411 * and then perform auto-negotiation.
1412 *
1413 * hw - Struct containing variables accessed by shared code
1414 *********************************************************************/
1415 static int32_t
1416 e1000_copper_link_autoneg(struct e1000_hw *hw)
1417 {
1418     int32_t ret_val;
1419     uint16_t phy_data;
1420
1421     DEBUGFUNC("e1000_copper_link_autoneg");
1422
1423     /* Perform some bounds checking on the hw->autoneg_advertised
1424      * parameter.  If this variable is zero, then set it to the default.
1425      */
1426     hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
1427
1428     /* If autoneg_advertised is zero, we assume it was not defaulted
1429      * by the calling code so we set to advertise full capability.
1430      */
1431     if(hw->autoneg_advertised == 0)
1432         hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
1433
1434     DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
1435     ret_val = e1000_phy_setup_autoneg(hw);
1436     if(ret_val) {
1437         DEBUGOUT("Error Setting up Auto-Negotiation\n");
1438         return ret_val;
1439     }
1440     DEBUGOUT("Restarting Auto-Neg\n");
1441
1442     /* Restart auto-negotiation by setting the Auto Neg Enable bit and
1443      * the Auto Neg Restart bit in the PHY control register.
1444      */
1445     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
1446     if(ret_val)
1447         return ret_val;
1448
1449     phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
1450     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
1451     if(ret_val)
1452         return ret_val;
1453
1454     /* Does the user want to wait for Auto-Neg to complete here, or
1455      * check at a later time (for example, callback routine).
1456      */
1457     if(hw->wait_autoneg_complete) {
1458         ret_val = e1000_wait_autoneg(hw);
1459         if(ret_val) {
1460             DEBUGOUT("Error while waiting for autoneg to complete\n");
1461             return ret_val;
1462         }
1463     }
1464
1465     hw->get_link_status = TRUE;
1466
1467     return E1000_SUCCESS;
1468 }
1469
1470
1471 /******************************************************************************
1472 * Config the MAC and the PHY after link is up.
1473 *   1) Set up the MAC to the current PHY speed/duplex
1474 *      if we are on 82543.  If we
1475 *      are on newer silicon, we only need to configure
1476 *      collision distance in the Transmit Control Register.
1477 *   2) Set up flow control on the MAC to that established with
1478 *      the link partner.
1479 *   3) Config DSP to improve Gigabit link quality for some PHY revisions.    
1480 *
1481 * hw - Struct containing variables accessed by shared code
1482 ******************************************************************************/
1483 static int32_t
1484 e1000_copper_link_postconfig(struct e1000_hw *hw)
1485 {
1486     int32_t ret_val;
1487     DEBUGFUNC("e1000_copper_link_postconfig");
1488     
1489     if(hw->mac_type >= e1000_82544) {
1490         e1000_config_collision_dist(hw);
1491     } else {
1492         ret_val = e1000_config_mac_to_phy(hw);
1493         if(ret_val) {
1494             DEBUGOUT("Error configuring MAC to PHY settings\n");
1495             return ret_val;
1496         }
1497     }
1498     ret_val = e1000_config_fc_after_link_up(hw);
1499     if(ret_val) {
1500         DEBUGOUT("Error Configuring Flow Control\n");
1501         return ret_val;
1502     }
1503
1504     /* Config DSP to improve Giga link quality */
1505     if(hw->phy_type == e1000_phy_igp) {
1506         ret_val = e1000_config_dsp_after_link_change(hw, TRUE);
1507         if(ret_val) {
1508             DEBUGOUT("Error Configuring DSP after link up\n");
1509             return ret_val;
1510         }
1511     }
1512                 
1513     return E1000_SUCCESS;
1514 }
1515
1516 /******************************************************************************
1517 * Detects which PHY is present and setup the speed and duplex
1518 *
1519 * hw - Struct containing variables accessed by shared code
1520 ******************************************************************************/
1521 static int32_t
1522 e1000_setup_copper_link(struct e1000_hw *hw)
1523 {
1524     int32_t ret_val;
1525     uint16_t i;
1526     uint16_t phy_data;
1527
1528     DEBUGFUNC("e1000_setup_copper_link");
1529
1530     /* Check if it is a valid PHY and set PHY mode if necessary. */
1531     ret_val = e1000_copper_link_preconfig(hw);
1532     if(ret_val)
1533         return ret_val;
1534
1535     if (hw->phy_type == e1000_phy_igp ||
1536         hw->phy_type == e1000_phy_igp_2) {
1537         ret_val = e1000_copper_link_igp_setup(hw);
1538         if(ret_val)
1539             return ret_val;
1540     } else if (hw->phy_type == e1000_phy_m88) {
1541         ret_val = e1000_copper_link_mgp_setup(hw);
1542         if(ret_val)
1543             return ret_val;
1544     }
1545
1546     if(hw->autoneg) {
1547         /* Setup autoneg and flow control advertisement 
1548           * and perform autonegotiation */   
1549         ret_val = e1000_copper_link_autoneg(hw);
1550         if(ret_val)
1551             return ret_val;           
1552     } else {
1553         /* PHY will be set to 10H, 10F, 100H,or 100F
1554           * depending on value from forced_speed_duplex. */
1555         DEBUGOUT("Forcing speed and duplex\n");
1556         ret_val = e1000_phy_force_speed_duplex(hw);
1557         if(ret_val) {
1558             DEBUGOUT("Error Forcing Speed and Duplex\n");
1559             return ret_val;
1560         }
1561     }
1562
1563     /* Check link status. Wait up to 100 microseconds for link to become
1564      * valid.
1565      */
1566     for(i = 0; i < 10; i++) {
1567         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1568         if(ret_val)
1569             return ret_val;
1570         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1571         if(ret_val)
1572             return ret_val;
1573
1574         if(phy_data & MII_SR_LINK_STATUS) {
1575             /* Config the MAC and PHY after link is up */
1576             ret_val = e1000_copper_link_postconfig(hw);
1577             if(ret_val)
1578                 return ret_val;
1579             
1580             DEBUGOUT("Valid link established!!!\n");
1581             return E1000_SUCCESS;
1582         }
1583         udelay(10);
1584     }
1585
1586     DEBUGOUT("Unable to establish link!!!\n");
1587     return E1000_SUCCESS;
1588 }
1589
1590 /******************************************************************************
1591 * Configures PHY autoneg and flow control advertisement settings
1592 *
1593 * hw - Struct containing variables accessed by shared code
1594 ******************************************************************************/
1595 int32_t
1596 e1000_phy_setup_autoneg(struct e1000_hw *hw)
1597 {
1598     int32_t ret_val;
1599     uint16_t mii_autoneg_adv_reg;
1600     uint16_t mii_1000t_ctrl_reg;
1601
1602     DEBUGFUNC("e1000_phy_setup_autoneg");
1603
1604     /* Read the MII Auto-Neg Advertisement Register (Address 4). */
1605     ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
1606     if(ret_val)
1607         return ret_val;
1608
1609     /* Read the MII 1000Base-T Control Register (Address 9). */
1610     ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg);
1611     if(ret_val)
1612         return ret_val;
1613
1614     /* Need to parse both autoneg_advertised and fc and set up
1615      * the appropriate PHY registers.  First we will parse for
1616      * autoneg_advertised software override.  Since we can advertise
1617      * a plethora of combinations, we need to check each bit
1618      * individually.
1619      */
1620
1621     /* First we clear all the 10/100 mb speed bits in the Auto-Neg
1622      * Advertisement Register (Address 4) and the 1000 mb speed bits in
1623      * the  1000Base-T Control Register (Address 9).
1624      */
1625     mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
1626     mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
1627
1628     DEBUGOUT1("autoneg_advertised %x\n", hw->autoneg_advertised);
1629
1630     /* Do we want to advertise 10 Mb Half Duplex? */
1631     if(hw->autoneg_advertised & ADVERTISE_10_HALF) {
1632         DEBUGOUT("Advertise 10mb Half duplex\n");
1633         mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
1634     }
1635
1636     /* Do we want to advertise 10 Mb Full Duplex? */
1637     if(hw->autoneg_advertised & ADVERTISE_10_FULL) {
1638         DEBUGOUT("Advertise 10mb Full duplex\n");
1639         mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
1640     }
1641
1642     /* Do we want to advertise 100 Mb Half Duplex? */
1643     if(hw->autoneg_advertised & ADVERTISE_100_HALF) {
1644         DEBUGOUT("Advertise 100mb Half duplex\n");
1645         mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
1646     }
1647
1648     /* Do we want to advertise 100 Mb Full Duplex? */
1649     if(hw->autoneg_advertised & ADVERTISE_100_FULL) {
1650         DEBUGOUT("Advertise 100mb Full duplex\n");
1651         mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
1652     }
1653
1654     /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
1655     if(hw->autoneg_advertised & ADVERTISE_1000_HALF) {
1656         DEBUGOUT("Advertise 1000mb Half duplex requested, request denied!\n");
1657     }
1658
1659     /* Do we want to advertise 1000 Mb Full Duplex? */
1660     if(hw->autoneg_advertised & ADVERTISE_1000_FULL) {
1661         DEBUGOUT("Advertise 1000mb Full duplex\n");
1662         mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
1663     }
1664
1665     /* Check for a software override of the flow control settings, and
1666      * setup the PHY advertisement registers accordingly.  If
1667      * auto-negotiation is enabled, then software will have to set the
1668      * "PAUSE" bits to the correct value in the Auto-Negotiation
1669      * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
1670      *
1671      * The possible values of the "fc" parameter are:
1672      *      0:  Flow control is completely disabled
1673      *      1:  Rx flow control is enabled (we can receive pause frames
1674      *          but not send pause frames).
1675      *      2:  Tx flow control is enabled (we can send pause frames
1676      *          but we do not support receiving pause frames).
1677      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1678      *  other:  No software override.  The flow control configuration
1679      *          in the EEPROM is used.
1680      */
1681     switch (hw->fc) {
1682     case e1000_fc_none: /* 0 */
1683         /* Flow control (RX & TX) is completely disabled by a
1684          * software over-ride.
1685          */
1686         mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1687         break;
1688     case e1000_fc_rx_pause: /* 1 */
1689         /* RX Flow control is enabled, and TX Flow control is
1690          * disabled, by a software over-ride.
1691          */
1692         /* Since there really isn't a way to advertise that we are
1693          * capable of RX Pause ONLY, we will advertise that we
1694          * support both symmetric and asymmetric RX PAUSE.  Later
1695          * (in e1000_config_fc_after_link_up) we will disable the
1696          *hw's ability to send PAUSE frames.
1697          */
1698         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1699         break;
1700     case e1000_fc_tx_pause: /* 2 */
1701         /* TX Flow control is enabled, and RX Flow control is
1702          * disabled, by a software over-ride.
1703          */
1704         mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1705         mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1706         break;
1707     case e1000_fc_full: /* 3 */
1708         /* Flow control (both RX and TX) is enabled by a software
1709          * over-ride.
1710          */
1711         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1712         break;
1713     default:
1714         DEBUGOUT("Flow control param set incorrectly\n");
1715         return -E1000_ERR_CONFIG;
1716     }
1717
1718     ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1719     if(ret_val)
1720         return ret_val;
1721
1722     DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1723
1724     ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg);    
1725     if(ret_val)
1726         return ret_val;
1727
1728     return E1000_SUCCESS;
1729 }
1730
1731 /******************************************************************************
1732 * Force PHY speed and duplex settings to hw->forced_speed_duplex
1733 *
1734 * hw - Struct containing variables accessed by shared code
1735 ******************************************************************************/
1736 static int32_t
1737 e1000_phy_force_speed_duplex(struct e1000_hw *hw)
1738 {
1739     uint32_t ctrl;
1740     int32_t ret_val;
1741     uint16_t mii_ctrl_reg;
1742     uint16_t mii_status_reg;
1743     uint16_t phy_data;
1744     uint16_t i;
1745
1746     DEBUGFUNC("e1000_phy_force_speed_duplex");
1747
1748     /* Turn off Flow control if we are forcing speed and duplex. */
1749     hw->fc = e1000_fc_none;
1750
1751     DEBUGOUT1("hw->fc = %d\n", hw->fc);
1752
1753     /* Read the Device Control Register. */
1754     ctrl = E1000_READ_REG(hw, CTRL);
1755
1756     /* Set the bits to Force Speed and Duplex in the Device Ctrl Reg. */
1757     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1758     ctrl &= ~(DEVICE_SPEED_MASK);
1759
1760     /* Clear the Auto Speed Detect Enable bit. */
1761     ctrl &= ~E1000_CTRL_ASDE;
1762
1763     /* Read the MII Control Register. */
1764     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &mii_ctrl_reg);
1765     if(ret_val)
1766         return ret_val;
1767
1768     /* We need to disable autoneg in order to force link and duplex. */
1769
1770     mii_ctrl_reg &= ~MII_CR_AUTO_NEG_EN;
1771
1772     /* Are we forcing Full or Half Duplex? */
1773     if(hw->forced_speed_duplex == e1000_100_full ||
1774        hw->forced_speed_duplex == e1000_10_full) {
1775         /* We want to force full duplex so we SET the full duplex bits in the
1776          * Device and MII Control Registers.
1777          */
1778         ctrl |= E1000_CTRL_FD;
1779         mii_ctrl_reg |= MII_CR_FULL_DUPLEX;
1780         DEBUGOUT("Full Duplex\n");
1781     } else {
1782         /* We want to force half duplex so we CLEAR the full duplex bits in
1783          * the Device and MII Control Registers.
1784          */
1785         ctrl &= ~E1000_CTRL_FD;
1786         mii_ctrl_reg &= ~MII_CR_FULL_DUPLEX;
1787         DEBUGOUT("Half Duplex\n");
1788     }
1789
1790     /* Are we forcing 100Mbps??? */
1791     if(hw->forced_speed_duplex == e1000_100_full ||
1792        hw->forced_speed_duplex == e1000_100_half) {
1793         /* Set the 100Mb bit and turn off the 1000Mb and 10Mb bits. */
1794         ctrl |= E1000_CTRL_SPD_100;
1795         mii_ctrl_reg |= MII_CR_SPEED_100;
1796         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
1797         DEBUGOUT("Forcing 100mb ");
1798     } else {
1799         /* Set the 10Mb bit and turn off the 1000Mb and 100Mb bits. */
1800         ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1801         mii_ctrl_reg |= MII_CR_SPEED_10;
1802         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
1803         DEBUGOUT("Forcing 10mb ");
1804     }
1805
1806     e1000_config_collision_dist(hw);
1807
1808     /* Write the configured values back to the Device Control Reg. */
1809     E1000_WRITE_REG(hw, CTRL, ctrl);
1810
1811     if (hw->phy_type == e1000_phy_m88) {
1812         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1813         if(ret_val)
1814             return ret_val;
1815
1816         /* Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
1817          * forced whenever speed are duplex are forced.
1818          */
1819         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1820         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1821         if(ret_val)
1822             return ret_val;
1823
1824         DEBUGOUT1("M88E1000 PSCR: %x \n", phy_data);
1825
1826         /* Need to reset the PHY or these changes will be ignored */
1827         mii_ctrl_reg |= MII_CR_RESET;
1828     } else {
1829         /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1830          * forced whenever speed or duplex are forced.
1831          */
1832         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1833         if(ret_val)
1834             return ret_val;
1835
1836         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1837         phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1838
1839         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1840         if(ret_val)
1841             return ret_val;
1842     }
1843
1844     /* Write back the modified PHY MII control register. */
1845     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, mii_ctrl_reg);
1846     if(ret_val)
1847         return ret_val;
1848
1849     udelay(1);
1850
1851     /* The wait_autoneg_complete flag may be a little misleading here.
1852      * Since we are forcing speed and duplex, Auto-Neg is not enabled.
1853      * But we do want to delay for a period while forcing only so we
1854      * don't generate false No Link messages.  So we will wait here
1855      * only if the user has set wait_autoneg_complete to 1, which is
1856      * the default.
1857      */
1858     if(hw->wait_autoneg_complete) {
1859         /* We will wait for autoneg to complete. */
1860         DEBUGOUT("Waiting for forced speed/duplex link.\n");
1861         mii_status_reg = 0;
1862
1863         /* We will wait for autoneg to complete or 4.5 seconds to expire. */
1864         for(i = PHY_FORCE_TIME; i > 0; i--) {
1865             /* Read the MII Status Register and wait for Auto-Neg Complete bit
1866              * to be set.
1867              */
1868             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1869             if(ret_val)
1870                 return ret_val;
1871
1872             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1873             if(ret_val)
1874                 return ret_val;
1875
1876             if(mii_status_reg & MII_SR_LINK_STATUS) break;
1877             msec_delay(100);
1878         }
1879         if((i == 0) &&
1880            (hw->phy_type == e1000_phy_m88)) {
1881             /* We didn't get link.  Reset the DSP and wait again for link. */
1882             ret_val = e1000_phy_reset_dsp(hw);
1883             if(ret_val) {
1884                 DEBUGOUT("Error Resetting PHY DSP\n");
1885                 return ret_val;
1886             }
1887         }
1888         /* This loop will early-out if the link condition has been met.  */
1889         for(i = PHY_FORCE_TIME; i > 0; i--) {
1890             if(mii_status_reg & MII_SR_LINK_STATUS) break;
1891             msec_delay(100);
1892             /* Read the MII Status Register and wait for Auto-Neg Complete bit
1893              * to be set.
1894              */
1895             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1896             if(ret_val)
1897                 return ret_val;
1898
1899             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1900             if(ret_val)
1901                 return ret_val;
1902         }
1903     }
1904
1905     if (hw->phy_type == e1000_phy_m88) {
1906         /* Because we reset the PHY above, we need to re-force TX_CLK in the
1907          * Extended PHY Specific Control Register to 25MHz clock.  This value
1908          * defaults back to a 2.5MHz clock when the PHY is reset.
1909          */
1910         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1911         if(ret_val)
1912             return ret_val;
1913
1914         phy_data |= M88E1000_EPSCR_TX_CLK_25;
1915         ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1916         if(ret_val)
1917             return ret_val;
1918
1919         /* In addition, because of the s/w reset above, we need to enable CRS on
1920          * TX.  This must be set for both full and half duplex operation.
1921          */
1922         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1923         if(ret_val)
1924             return ret_val;
1925
1926         phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1927         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1928         if(ret_val)
1929             return ret_val;
1930
1931         if((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
1932            (!hw->autoneg) &&
1933            (hw->forced_speed_duplex == e1000_10_full ||
1934             hw->forced_speed_duplex == e1000_10_half)) {
1935             ret_val = e1000_polarity_reversal_workaround(hw);
1936             if(ret_val)
1937                 return ret_val;
1938         }
1939     }
1940     return E1000_SUCCESS;
1941 }
1942
1943 /******************************************************************************
1944 * Sets the collision distance in the Transmit Control register
1945 *
1946 * hw - Struct containing variables accessed by shared code
1947 *
1948 * Link should have been established previously. Reads the speed and duplex
1949 * information from the Device Status register.
1950 ******************************************************************************/
1951 void
1952 e1000_config_collision_dist(struct e1000_hw *hw)
1953 {
1954     uint32_t tctl, coll_dist;
1955
1956     DEBUGFUNC("e1000_config_collision_dist");
1957
1958     if (hw->mac_type < e1000_82543)
1959         coll_dist = E1000_COLLISION_DISTANCE_82542;
1960     else
1961         coll_dist = E1000_COLLISION_DISTANCE;
1962
1963     tctl = E1000_READ_REG(hw, TCTL);
1964
1965     tctl &= ~E1000_TCTL_COLD;
1966     tctl |= coll_dist << E1000_COLD_SHIFT;
1967
1968     E1000_WRITE_REG(hw, TCTL, tctl);
1969     E1000_WRITE_FLUSH(hw);
1970 }
1971
1972 /******************************************************************************
1973 * Sets MAC speed and duplex settings to reflect the those in the PHY
1974 *
1975 * hw - Struct containing variables accessed by shared code
1976 * mii_reg - data to write to the MII control register
1977 *
1978 * The contents of the PHY register containing the needed information need to
1979 * be passed in.
1980 ******************************************************************************/
1981 static int32_t
1982 e1000_config_mac_to_phy(struct e1000_hw *hw)
1983 {
1984     uint32_t ctrl;
1985     int32_t ret_val;
1986     uint16_t phy_data;
1987
1988     DEBUGFUNC("e1000_config_mac_to_phy");
1989
1990     /* 82544 or newer MAC, Auto Speed Detection takes care of 
1991     * MAC speed/duplex configuration.*/
1992     if (hw->mac_type >= e1000_82544)
1993         return E1000_SUCCESS;
1994
1995     /* Read the Device Control Register and set the bits to Force Speed
1996      * and Duplex.
1997      */
1998     ctrl = E1000_READ_REG(hw, CTRL);
1999     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
2000     ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
2001
2002     /* Set up duplex in the Device Control and Transmit Control
2003      * registers depending on negotiated values.
2004      */
2005     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
2006     if(ret_val)
2007         return ret_val;
2008
2009     if(phy_data & M88E1000_PSSR_DPLX) 
2010         ctrl |= E1000_CTRL_FD;
2011     else 
2012         ctrl &= ~E1000_CTRL_FD;
2013
2014     e1000_config_collision_dist(hw);
2015
2016     /* Set up speed in the Device Control register depending on
2017      * negotiated values.
2018      */
2019     if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
2020         ctrl |= E1000_CTRL_SPD_1000;
2021     else if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
2022         ctrl |= E1000_CTRL_SPD_100;
2023
2024     /* Write the configured values back to the Device Control Reg. */
2025     E1000_WRITE_REG(hw, CTRL, ctrl);
2026     return E1000_SUCCESS;
2027 }
2028
2029 /******************************************************************************
2030  * Forces the MAC's flow control settings.
2031  *
2032  * hw - Struct containing variables accessed by shared code
2033  *
2034  * Sets the TFCE and RFCE bits in the device control register to reflect
2035  * the adapter settings. TFCE and RFCE need to be explicitly set by
2036  * software when a Copper PHY is used because autonegotiation is managed
2037  * by the PHY rather than the MAC. Software must also configure these
2038  * bits when link is forced on a fiber connection.
2039  *****************************************************************************/
2040 int32_t
2041 e1000_force_mac_fc(struct e1000_hw *hw)
2042 {
2043     uint32_t ctrl;
2044
2045     DEBUGFUNC("e1000_force_mac_fc");
2046
2047     /* Get the current configuration of the Device Control Register */
2048     ctrl = E1000_READ_REG(hw, CTRL);
2049
2050     /* Because we didn't get link via the internal auto-negotiation
2051      * mechanism (we either forced link or we got link via PHY
2052      * auto-neg), we have to manually enable/disable transmit an
2053      * receive flow control.
2054      *
2055      * The "Case" statement below enables/disable flow control
2056      * according to the "hw->fc" parameter.
2057      *
2058      * The possible values of the "fc" parameter are:
2059      *      0:  Flow control is completely disabled
2060      *      1:  Rx flow control is enabled (we can receive pause
2061      *          frames but not send pause frames).
2062      *      2:  Tx flow control is enabled (we can send pause frames
2063      *          frames but we do not receive pause frames).
2064      *      3:  Both Rx and TX flow control (symmetric) is enabled.
2065      *  other:  No other values should be possible at this point.
2066      */
2067
2068     switch (hw->fc) {
2069     case e1000_fc_none:
2070         ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
2071         break;
2072     case e1000_fc_rx_pause:
2073         ctrl &= (~E1000_CTRL_TFCE);
2074         ctrl |= E1000_CTRL_RFCE;
2075         break;
2076     case e1000_fc_tx_pause:
2077         ctrl &= (~E1000_CTRL_RFCE);
2078         ctrl |= E1000_CTRL_TFCE;
2079         break;
2080     case e1000_fc_full:
2081         ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
2082         break;
2083     default:
2084         DEBUGOUT("Flow control param set incorrectly\n");
2085         return -E1000_ERR_CONFIG;
2086     }
2087
2088     /* Disable TX Flow Control for 82542 (rev 2.0) */
2089     if(hw->mac_type == e1000_82542_rev2_0)
2090         ctrl &= (~E1000_CTRL_TFCE);
2091
2092     E1000_WRITE_REG(hw, CTRL, ctrl);
2093     return E1000_SUCCESS;
2094 }
2095
2096 /******************************************************************************
2097  * Configures flow control settings after link is established
2098  *
2099  * hw - Struct containing variables accessed by shared code
2100  *
2101  * Should be called immediately after a valid link has been established.
2102  * Forces MAC flow control settings if link was forced. When in MII/GMII mode
2103  * and autonegotiation is enabled, the MAC flow control settings will be set
2104  * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
2105  * and RFCE bits will be automaticaly set to the negotiated flow control mode.
2106  *****************************************************************************/
2107 static int32_t
2108 e1000_config_fc_after_link_up(struct e1000_hw *hw)
2109 {
2110     int32_t ret_val;
2111     uint16_t mii_status_reg;
2112     uint16_t mii_nway_adv_reg;
2113     uint16_t mii_nway_lp_ability_reg;
2114     uint16_t speed;
2115     uint16_t duplex;
2116
2117     DEBUGFUNC("e1000_config_fc_after_link_up");
2118
2119     /* Check for the case where we have fiber media and auto-neg failed
2120      * so we had to force link.  In this case, we need to force the
2121      * configuration of the MAC to match the "fc" parameter.
2122      */
2123     if(((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed)) ||
2124        ((hw->media_type == e1000_media_type_internal_serdes) && (hw->autoneg_failed)) ||
2125        ((hw->media_type == e1000_media_type_copper) && (!hw->autoneg))) {
2126         ret_val = e1000_force_mac_fc(hw);
2127         if(ret_val) {
2128             DEBUGOUT("Error forcing flow control settings\n");
2129             return ret_val;
2130         }
2131     }
2132
2133     /* Check for the case where we have copper media and auto-neg is
2134      * enabled.  In this case, we need to check and see if Auto-Neg
2135      * has completed, and if so, how the PHY and link partner has
2136      * flow control configured.
2137      */
2138     if((hw->media_type == e1000_media_type_copper) && hw->autoneg) {
2139         /* Read the MII Status Register and check to see if AutoNeg
2140          * has completed.  We read this twice because this reg has
2141          * some "sticky" (latched) bits.
2142          */
2143         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2144         if(ret_val)
2145             return ret_val;
2146         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
2147         if(ret_val)
2148             return ret_val;
2149
2150         if(mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
2151             /* The AutoNeg process has completed, so we now need to
2152              * read both the Auto Negotiation Advertisement Register
2153              * (Address 4) and the Auto_Negotiation Base Page Ability
2154              * Register (Address 5) to determine how flow control was
2155              * negotiated.
2156              */
2157             ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
2158                                          &mii_nway_adv_reg);
2159             if(ret_val)
2160                 return ret_val;
2161             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY,
2162                                          &mii_nway_lp_ability_reg);
2163             if(ret_val)
2164                 return ret_val;
2165
2166             /* Two bits in the Auto Negotiation Advertisement Register
2167              * (Address 4) and two bits in the Auto Negotiation Base
2168              * Page Ability Register (Address 5) determine flow control
2169              * for both the PHY and the link partner.  The following
2170              * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
2171              * 1999, describes these PAUSE resolution bits and how flow
2172              * control is determined based upon these settings.
2173              * NOTE:  DC = Don't Care
2174              *
2175              *   LOCAL DEVICE  |   LINK PARTNER
2176              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
2177              *-------|---------|-------|---------|--------------------
2178              *   0   |    0    |  DC   |   DC    | e1000_fc_none
2179              *   0   |    1    |   0   |   DC    | e1000_fc_none
2180              *   0   |    1    |   1   |    0    | e1000_fc_none
2181              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
2182              *   1   |    0    |   0   |   DC    | e1000_fc_none
2183              *   1   |   DC    |   1   |   DC    | e1000_fc_full
2184              *   1   |    1    |   0   |    0    | e1000_fc_none
2185              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
2186              *
2187              */
2188             /* Are both PAUSE bits set to 1?  If so, this implies
2189              * Symmetric Flow Control is enabled at both ends.  The
2190              * ASM_DIR bits are irrelevant per the spec.
2191              *
2192              * For Symmetric Flow Control:
2193              *
2194              *   LOCAL DEVICE  |   LINK PARTNER
2195              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2196              *-------|---------|-------|---------|--------------------
2197              *   1   |   DC    |   1   |   DC    | e1000_fc_full
2198              *
2199              */
2200             if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2201                (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
2202                 /* Now we need to check if the user selected RX ONLY
2203                  * of pause frames.  In this case, we had to advertise
2204                  * FULL flow control because we could not advertise RX
2205                  * ONLY. Hence, we must now check to see if we need to
2206                  * turn OFF  the TRANSMISSION of PAUSE frames.
2207                  */
2208                 if(hw->original_fc == e1000_fc_full) {
2209                     hw->fc = e1000_fc_full;
2210                     DEBUGOUT("Flow Control = FULL.\r\n");
2211                 } else {
2212                     hw->fc = e1000_fc_rx_pause;
2213                     DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
2214                 }
2215             }
2216             /* For receiving PAUSE frames ONLY.
2217              *
2218              *   LOCAL DEVICE  |   LINK PARTNER
2219              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2220              *-------|---------|-------|---------|--------------------
2221              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
2222              *
2223              */
2224             else if(!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2225                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2226                     (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2227                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2228                 hw->fc = e1000_fc_tx_pause;
2229                 DEBUGOUT("Flow Control = TX PAUSE frames only.\r\n");
2230             }
2231             /* For transmitting PAUSE frames ONLY.
2232              *
2233              *   LOCAL DEVICE  |   LINK PARTNER
2234              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
2235              *-------|---------|-------|---------|--------------------
2236              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
2237              *
2238              */
2239             else if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
2240                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
2241                     !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
2242                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
2243                 hw->fc = e1000_fc_rx_pause;
2244                 DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
2245             }
2246             /* Per the IEEE spec, at this point flow control should be
2247              * disabled.  However, we want to consider that we could
2248              * be connected to a legacy switch that doesn't advertise
2249              * desired flow control, but can be forced on the link
2250              * partner.  So if we advertised no flow control, that is
2251              * what we will resolve to.  If we advertised some kind of
2252              * receive capability (Rx Pause Only or Full Flow Control)
2253              * and the link partner advertised none, we will configure
2254              * ourselves to enable Rx Flow Control only.  We can do
2255              * this safely for two reasons:  If the link partner really
2256              * didn't want flow control enabled, and we enable Rx, no
2257              * harm done since we won't be receiving any PAUSE frames
2258              * anyway.  If the intent on the link partner was to have
2259              * flow control enabled, then by us enabling RX only, we
2260              * can at least receive pause frames and process them.
2261              * This is a good idea because in most cases, since we are
2262              * predominantly a server NIC, more times than not we will
2263              * be asked to delay transmission of packets than asking
2264              * our link partner to pause transmission of frames.
2265              */
2266             else if((hw->original_fc == e1000_fc_none ||
2267                      hw->original_fc == e1000_fc_tx_pause) ||
2268                     hw->fc_strict_ieee) {
2269                 hw->fc = e1000_fc_none;
2270                 DEBUGOUT("Flow Control = NONE.\r\n");
2271             } else {
2272                 hw->fc = e1000_fc_rx_pause;
2273                 DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
2274             }
2275
2276             /* Now we need to do one last check...  If we auto-
2277              * negotiated to HALF DUPLEX, flow control should not be
2278              * enabled per IEEE 802.3 spec.
2279              */
2280             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
2281             if(ret_val) {
2282                 DEBUGOUT("Error getting link speed and duplex\n");
2283                 return ret_val;
2284             }
2285
2286             if(duplex == HALF_DUPLEX)
2287                 hw->fc = e1000_fc_none;
2288
2289             /* Now we call a subroutine to actually force the MAC
2290              * controller to use the correct flow control settings.
2291              */
2292             ret_val = e1000_force_mac_fc(hw);
2293             if(ret_val) {
2294                 DEBUGOUT("Error forcing flow control settings\n");
2295                 return ret_val;
2296             }
2297         } else {
2298             DEBUGOUT("Copper PHY and Auto Neg has not completed.\r\n");
2299         }
2300     }
2301     return E1000_SUCCESS;
2302 }
2303
2304 /******************************************************************************
2305  * Checks to see if the link status of the hardware has changed.
2306  *
2307  * hw - Struct containing variables accessed by shared code
2308  *
2309  * Called by any function that needs to check the link status of the adapter.
2310  *****************************************************************************/
2311 int32_t
2312 e1000_check_for_link(struct e1000_hw *hw)
2313 {
2314     uint32_t rxcw = 0;
2315     uint32_t ctrl;
2316     uint32_t status;
2317     uint32_t rctl;
2318     uint32_t icr;
2319     uint32_t signal = 0;
2320     int32_t ret_val;
2321     uint16_t phy_data;
2322
2323     DEBUGFUNC("e1000_check_for_link");
2324
2325     ctrl = E1000_READ_REG(hw, CTRL);
2326     status = E1000_READ_REG(hw, STATUS);
2327
2328     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
2329      * set when the optics detect a signal. On older adapters, it will be
2330      * cleared when there is a signal.  This applies to fiber media only.
2331      */
2332     if((hw->media_type == e1000_media_type_fiber) ||
2333        (hw->media_type == e1000_media_type_internal_serdes)) {
2334         rxcw = E1000_READ_REG(hw, RXCW);
2335
2336         if(hw->media_type == e1000_media_type_fiber) {
2337             signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
2338             if(status & E1000_STATUS_LU)
2339                 hw->get_link_status = FALSE;
2340         }
2341     }
2342
2343     /* If we have a copper PHY then we only want to go out to the PHY
2344      * registers to see if Auto-Neg has completed and/or if our link
2345      * status has changed.  The get_link_status flag will be set if we
2346      * receive a Link Status Change interrupt or we have Rx Sequence
2347      * Errors.
2348      */
2349     if((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
2350         /* First we want to see if the MII Status Register reports
2351          * link.  If so, then we want to get the current speed/duplex
2352          * of the PHY.
2353          * Read the register twice since the link bit is sticky.
2354          */
2355         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2356         if(ret_val)
2357             return ret_val;
2358         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2359         if(ret_val)
2360             return ret_val;
2361
2362         if(phy_data & MII_SR_LINK_STATUS) {
2363             hw->get_link_status = FALSE;
2364             /* Check if there was DownShift, must be checked immediately after
2365              * link-up */
2366             e1000_check_downshift(hw);
2367
2368             /* If we are on 82544 or 82543 silicon and speed/duplex
2369              * are forced to 10H or 10F, then we will implement the polarity
2370              * reversal workaround.  We disable interrupts first, and upon
2371              * returning, place the devices interrupt state to its previous
2372              * value except for the link status change interrupt which will
2373              * happen due to the execution of this workaround.
2374              */
2375
2376             if((hw->mac_type == e1000_82544 || hw->mac_type == e1000_82543) &&
2377                (!hw->autoneg) &&
2378                (hw->forced_speed_duplex == e1000_10_full ||
2379                 hw->forced_speed_duplex == e1000_10_half)) {
2380                 E1000_WRITE_REG(hw, IMC, 0xffffffff);
2381                 ret_val = e1000_polarity_reversal_workaround(hw);
2382                 icr = E1000_READ_REG(hw, ICR);
2383                 E1000_WRITE_REG(hw, ICS, (icr & ~E1000_ICS_LSC));
2384                 E1000_WRITE_REG(hw, IMS, IMS_ENABLE_MASK);
2385             }
2386
2387         } else {
2388             /* No link detected */
2389             e1000_config_dsp_after_link_change(hw, FALSE);
2390             return 0;
2391         }
2392
2393         /* If we are forcing speed/duplex, then we simply return since
2394          * we have already determined whether we have link or not.
2395          */
2396         if(!hw->autoneg) return -E1000_ERR_CONFIG;
2397
2398         /* optimize the dsp settings for the igp phy */
2399         e1000_config_dsp_after_link_change(hw, TRUE);
2400
2401         /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
2402          * have Si on board that is 82544 or newer, Auto
2403          * Speed Detection takes care of MAC speed/duplex
2404          * configuration.  So we only need to configure Collision
2405          * Distance in the MAC.  Otherwise, we need to force
2406          * speed/duplex on the MAC to the current PHY speed/duplex
2407          * settings.
2408          */
2409         if(hw->mac_type >= e1000_82544)
2410             e1000_config_collision_dist(hw);
2411         else {
2412             ret_val = e1000_config_mac_to_phy(hw);
2413             if(ret_val) {
2414                 DEBUGOUT("Error configuring MAC to PHY settings\n");
2415                 return ret_val;
2416             }
2417         }
2418
2419         /* Configure Flow Control now that Auto-Neg has completed. First, we
2420          * need to restore the desired flow control settings because we may
2421          * have had to re-autoneg with a different link partner.
2422          */
2423         ret_val = e1000_config_fc_after_link_up(hw);
2424         if(ret_val) {
2425             DEBUGOUT("Error configuring flow control\n");
2426             return ret_val;
2427         }
2428
2429         /* At this point we know that we are on copper and we have
2430          * auto-negotiated link.  These are conditions for checking the link
2431          * partner capability register.  We use the link speed to determine if
2432          * TBI compatibility needs to be turned on or off.  If the link is not
2433          * at gigabit speed, then TBI compatibility is not needed.  If we are
2434          * at gigabit speed, we turn on TBI compatibility.
2435          */
2436         if(hw->tbi_compatibility_en) {
2437             uint16_t speed, duplex;
2438             e1000_get_speed_and_duplex(hw, &speed, &duplex);
2439             if(speed != SPEED_1000) {
2440                 /* If link speed is not set to gigabit speed, we do not need
2441                  * to enable TBI compatibility.
2442                  */
2443                 if(hw->tbi_compatibility_on) {
2444                     /* If we previously were in the mode, turn it off. */
2445                     rctl = E1000_READ_REG(hw, RCTL);
2446                     rctl &= ~E1000_RCTL_SBP;
2447                     E1000_WRITE_REG(hw, RCTL, rctl);
2448                     hw->tbi_compatibility_on = FALSE;
2449                 }
2450             } else {
2451                 /* If TBI compatibility is was previously off, turn it on. For
2452                  * compatibility with a TBI link partner, we will store bad
2453                  * packets. Some frames have an additional byte on the end and
2454                  * will look like CRC errors to to the hardware.
2455                  */
2456                 if(!hw->tbi_compatibility_on) {
2457                     hw->tbi_compatibility_on = TRUE;
2458                     rctl = E1000_READ_REG(hw, RCTL);
2459                     rctl |= E1000_RCTL_SBP;
2460                     E1000_WRITE_REG(hw, RCTL, rctl);
2461                 }
2462             }
2463         }
2464     }
2465     /* If we don't have link (auto-negotiation failed or link partner cannot
2466      * auto-negotiate), the cable is plugged in (we have signal), and our
2467      * link partner is not trying to auto-negotiate with us (we are receiving
2468      * idles or data), we need to force link up. We also need to give
2469      * auto-negotiation time to complete, in case the cable was just plugged
2470      * in. The autoneg_failed flag does this.
2471      */
2472     else if((((hw->media_type == e1000_media_type_fiber) &&
2473               ((ctrl & E1000_CTRL_SWDPIN1) == signal)) ||
2474              (hw->media_type == e1000_media_type_internal_serdes)) &&
2475             (!(status & E1000_STATUS_LU)) &&
2476             (!(rxcw & E1000_RXCW_C))) {
2477         if(hw->autoneg_failed == 0) {
2478             hw->autoneg_failed = 1;
2479             return 0;
2480         }
2481         DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
2482
2483         /* Disable auto-negotiation in the TXCW register */
2484         E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
2485
2486         /* Force link-up and also force full-duplex. */
2487         ctrl = E1000_READ_REG(hw, CTRL);
2488         ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
2489         E1000_WRITE_REG(hw, CTRL, ctrl);
2490
2491         /* Configure Flow Control after forcing link up. */
2492         ret_val = e1000_config_fc_after_link_up(hw);
2493         if(ret_val) {
2494             DEBUGOUT("Error configuring flow control\n");
2495             return ret_val;
2496         }
2497     }
2498     /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
2499      * auto-negotiation in the TXCW register and disable forced link in the
2500      * Device Control register in an attempt to auto-negotiate with our link
2501      * partner.
2502      */
2503     else if(((hw->media_type == e1000_media_type_fiber) ||
2504              (hw->media_type == e1000_media_type_internal_serdes)) &&
2505             (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
2506         DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
2507         E1000_WRITE_REG(hw, TXCW, hw->txcw);
2508         E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
2509
2510         hw->serdes_link_down = FALSE;
2511     }
2512     /* If we force link for non-auto-negotiation switch, check link status
2513      * based on MAC synchronization for internal serdes media type.
2514      */
2515     else if((hw->media_type == e1000_media_type_internal_serdes) &&
2516             !(E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
2517         /* SYNCH bit and IV bit are sticky. */
2518         udelay(10);
2519         if(E1000_RXCW_SYNCH & E1000_READ_REG(hw, RXCW)) {
2520             if(!(rxcw & E1000_RXCW_IV)) {
2521                 hw->serdes_link_down = FALSE;
2522                 DEBUGOUT("SERDES: Link is up.\n");
2523             }
2524         } else {
2525             hw->serdes_link_down = TRUE;
2526             DEBUGOUT("SERDES: Link is down.\n");
2527         }
2528     }
2529     if((hw->media_type == e1000_media_type_internal_serdes) &&
2530        (E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
2531         hw->serdes_link_down = !(E1000_STATUS_LU & E1000_READ_REG(hw, STATUS));
2532     }
2533     return E1000_SUCCESS;
2534 }
2535
2536 /******************************************************************************
2537  * Detects the current speed and duplex settings of the hardware.
2538  *
2539  * hw - Struct containing variables accessed by shared code
2540  * speed - Speed of the connection
2541  * duplex - Duplex setting of the connection
2542  *****************************************************************************/
2543 int32_t
2544 e1000_get_speed_and_duplex(struct e1000_hw *hw,
2545                            uint16_t *speed,
2546                            uint16_t *duplex)
2547 {
2548     uint32_t status;
2549     int32_t ret_val;
2550     uint16_t phy_data;
2551
2552     DEBUGFUNC("e1000_get_speed_and_duplex");
2553
2554     if(hw->mac_type >= e1000_82543) {
2555         status = E1000_READ_REG(hw, STATUS);
2556         if(status & E1000_STATUS_SPEED_1000) {
2557             *speed = SPEED_1000;
2558             DEBUGOUT("1000 Mbs, ");
2559         } else if(status & E1000_STATUS_SPEED_100) {
2560             *speed = SPEED_100;
2561             DEBUGOUT("100 Mbs, ");
2562         } else {
2563             *speed = SPEED_10;
2564             DEBUGOUT("10 Mbs, ");
2565         }
2566
2567         if(status & E1000_STATUS_FD) {
2568             *duplex = FULL_DUPLEX;
2569             DEBUGOUT("Full Duplex\r\n");
2570         } else {
2571             *duplex = HALF_DUPLEX;
2572             DEBUGOUT(" Half Duplex\r\n");
2573         }
2574     } else {
2575         DEBUGOUT("1000 Mbs, Full Duplex\r\n");
2576         *speed = SPEED_1000;
2577         *duplex = FULL_DUPLEX;
2578     }
2579
2580     /* IGP01 PHY may advertise full duplex operation after speed downgrade even
2581      * if it is operating at half duplex.  Here we set the duplex settings to
2582      * match the duplex in the link partner's capabilities.
2583      */
2584     if(hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
2585         ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
2586         if(ret_val)
2587             return ret_val;
2588
2589         if(!(phy_data & NWAY_ER_LP_NWAY_CAPS))
2590             *duplex = HALF_DUPLEX;
2591         else {
2592             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_data);
2593             if(ret_val)
2594                 return ret_val;
2595             if((*speed == SPEED_100 && !(phy_data & NWAY_LPAR_100TX_FD_CAPS)) ||
2596                (*speed == SPEED_10 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
2597                 *duplex = HALF_DUPLEX;
2598         }
2599     }
2600
2601     return E1000_SUCCESS;
2602 }
2603
2604 /******************************************************************************
2605 * Blocks until autoneg completes or times out (~4.5 seconds)
2606 *
2607 * hw - Struct containing variables accessed by shared code
2608 ******************************************************************************/
2609 static int32_t
2610 e1000_wait_autoneg(struct e1000_hw *hw)
2611 {
2612     int32_t ret_val;
2613     uint16_t i;
2614     uint16_t phy_data;
2615
2616     DEBUGFUNC("e1000_wait_autoneg");
2617     DEBUGOUT("Waiting for Auto-Neg to complete.\n");
2618
2619     /* We will wait for autoneg to complete or 4.5 seconds to expire. */
2620     for(i = PHY_AUTO_NEG_TIME; i > 0; i--) {
2621         /* Read the MII Status Register and wait for Auto-Neg
2622          * Complete bit to be set.
2623          */
2624         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2625         if(ret_val)
2626             return ret_val;
2627         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2628         if(ret_val)
2629             return ret_val;
2630         if(phy_data & MII_SR_AUTONEG_COMPLETE) {
2631             return E1000_SUCCESS;
2632         }
2633         msec_delay(100);
2634     }
2635     return E1000_SUCCESS;
2636 }
2637
2638 /******************************************************************************
2639 * Raises the Management Data Clock
2640 *
2641 * hw - Struct containing variables accessed by shared code
2642 * ctrl - Device control register's current value
2643 ******************************************************************************/
2644 static void
2645 e1000_raise_mdi_clk(struct e1000_hw *hw,
2646                     uint32_t *ctrl)
2647 {
2648     /* Raise the clock input to the Management Data Clock (by setting the MDC
2649      * bit), and then delay 10 microseconds.
2650      */
2651     E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
2652     E1000_WRITE_FLUSH(hw);
2653     udelay(10);
2654 }
2655
2656 /******************************************************************************
2657 * Lowers the Management Data Clock
2658 *
2659 * hw - Struct containing variables accessed by shared code
2660 * ctrl - Device control register's current value
2661 ******************************************************************************/
2662 static void
2663 e1000_lower_mdi_clk(struct e1000_hw *hw,
2664                     uint32_t *ctrl)
2665 {
2666     /* Lower the clock input to the Management Data Clock (by clearing the MDC
2667      * bit), and then delay 10 microseconds.
2668      */
2669     E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
2670     E1000_WRITE_FLUSH(hw);
2671     udelay(10);
2672 }
2673
2674 /******************************************************************************
2675 * Shifts data bits out to the PHY
2676 *
2677 * hw - Struct containing variables accessed by shared code
2678 * data - Data to send out to the PHY
2679 * count - Number of bits to shift out
2680 *
2681 * Bits are shifted out in MSB to LSB order.
2682 ******************************************************************************/
2683 static void
2684 e1000_shift_out_mdi_bits(struct e1000_hw *hw,
2685                          uint32_t data,
2686                          uint16_t count)
2687 {
2688     uint32_t ctrl;
2689     uint32_t mask;
2690
2691     /* We need to shift "count" number of bits out to the PHY. So, the value
2692      * in the "data" parameter will be shifted out to the PHY one bit at a
2693      * time. In order to do this, "data" must be broken down into bits.
2694      */
2695     mask = 0x01;
2696     mask <<= (count - 1);
2697
2698     ctrl = E1000_READ_REG(hw, CTRL);
2699
2700     /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
2701     ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
2702
2703     while(mask) {
2704         /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
2705          * then raising and lowering the Management Data Clock. A "0" is
2706          * shifted out to the PHY by setting the MDIO bit to "0" and then
2707          * raising and lowering the clock.
2708          */
2709         if(data & mask) ctrl |= E1000_CTRL_MDIO;
2710         else ctrl &= ~E1000_CTRL_MDIO;
2711
2712         E1000_WRITE_REG(hw, CTRL, ctrl);
2713         E1000_WRITE_FLUSH(hw);
2714
2715         udelay(10);
2716
2717         e1000_raise_mdi_clk(hw, &ctrl);
2718         e1000_lower_mdi_clk(hw, &ctrl);
2719
2720         mask = mask >> 1;
2721     }
2722 }
2723
2724 /******************************************************************************
2725 * Shifts data bits in from the PHY
2726 *
2727 * hw - Struct containing variables accessed by shared code
2728 *
2729 * Bits are shifted in in MSB to LSB order.
2730 ******************************************************************************/
2731 static uint16_t
2732 e1000_shift_in_mdi_bits(struct e1000_hw *hw)
2733 {
2734     uint32_t ctrl;
2735     uint16_t data = 0;
2736     uint8_t i;
2737
2738     /* In order to read a register from the PHY, we need to shift in a total
2739      * of 18 bits from the PHY. The first two bit (turnaround) times are used
2740      * to avoid contention on the MDIO pin when a read operation is performed.
2741      * These two bits are ignored by us and thrown away. Bits are "shifted in"
2742      * by raising the input to the Management Data Clock (setting the MDC bit),
2743      * and then reading the value of the MDIO bit.
2744      */
2745     ctrl = E1000_READ_REG(hw, CTRL);
2746
2747     /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
2748     ctrl &= ~E1000_CTRL_MDIO_DIR;
2749     ctrl &= ~E1000_CTRL_MDIO;
2750
2751     E1000_WRITE_REG(hw, CTRL, ctrl);
2752     E1000_WRITE_FLUSH(hw);
2753
2754     /* Raise and Lower the clock before reading in the data. This accounts for
2755      * the turnaround bits. The first clock occurred when we clocked out the
2756      * last bit of the Register Address.
2757      */
2758     e1000_raise_mdi_clk(hw, &ctrl);
2759     e1000_lower_mdi_clk(hw, &ctrl);
2760
2761     for(data = 0, i = 0; i < 16; i++) {
2762         data = data << 1;
2763         e1000_raise_mdi_clk(hw, &ctrl);
2764         ctrl = E1000_READ_REG(hw, CTRL);
2765         /* Check to see if we shifted in a "1". */
2766         if(ctrl & E1000_CTRL_MDIO) data |= 1;
2767         e1000_lower_mdi_clk(hw, &ctrl);
2768     }
2769
2770     e1000_raise_mdi_clk(hw, &ctrl);
2771     e1000_lower_mdi_clk(hw, &ctrl);
2772
2773     return data;
2774 }
2775
2776 /*****************************************************************************
2777 * Reads the value from a PHY register, if the value is on a specific non zero
2778 * page, sets the page first.
2779 * hw - Struct containing variables accessed by shared code
2780 * reg_addr - address of the PHY register to read
2781 ******************************************************************************/
2782 int32_t
2783 e1000_read_phy_reg(struct e1000_hw *hw,
2784                    uint32_t reg_addr,
2785                    uint16_t *phy_data)
2786 {
2787     uint32_t ret_val;
2788
2789     DEBUGFUNC("e1000_read_phy_reg");
2790
2791     if((hw->phy_type == e1000_phy_igp || 
2792         hw->phy_type == e1000_phy_igp_2) &&
2793        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2794         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2795                                          (uint16_t)reg_addr);
2796         if(ret_val) {
2797             return ret_val;
2798         }
2799     }
2800
2801     ret_val = e1000_read_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
2802                                     phy_data);
2803
2804     return ret_val;
2805 }
2806
2807 int32_t
2808 e1000_read_phy_reg_ex(struct e1000_hw *hw,
2809                       uint32_t reg_addr,
2810                       uint16_t *phy_data)
2811 {
2812     uint32_t i;
2813     uint32_t mdic = 0;
2814     const uint32_t phy_addr = 1;
2815
2816     DEBUGFUNC("e1000_read_phy_reg_ex");
2817
2818     if(reg_addr > MAX_PHY_REG_ADDRESS) {
2819         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2820         return -E1000_ERR_PARAM;
2821     }
2822
2823     if(hw->mac_type > e1000_82543) {
2824         /* Set up Op-code, Phy Address, and register address in the MDI
2825          * Control register.  The MAC will take care of interfacing with the
2826          * PHY to retrieve the desired data.
2827          */
2828         mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
2829                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
2830                 (E1000_MDIC_OP_READ));
2831
2832         E1000_WRITE_REG(hw, MDIC, mdic);
2833
2834         /* Poll the ready bit to see if the MDI read completed */
2835         for(i = 0; i < 64; i++) {
2836             udelay(50);
2837             mdic = E1000_READ_REG(hw, MDIC);
2838             if(mdic & E1000_MDIC_READY) break;
2839         }
2840         if(!(mdic & E1000_MDIC_READY)) {
2841             DEBUGOUT("MDI Read did not complete\n");
2842             return -E1000_ERR_PHY;
2843         }
2844         if(mdic & E1000_MDIC_ERROR) {
2845             DEBUGOUT("MDI Error\n");
2846             return -E1000_ERR_PHY;
2847         }
2848         *phy_data = (uint16_t) mdic;
2849     } else {
2850         /* We must first send a preamble through the MDIO pin to signal the
2851          * beginning of an MII instruction.  This is done by sending 32
2852          * consecutive "1" bits.
2853          */
2854         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2855
2856         /* Now combine the next few fields that are required for a read
2857          * operation.  We use this method instead of calling the
2858          * e1000_shift_out_mdi_bits routine five different times. The format of
2859          * a MII read instruction consists of a shift out of 14 bits and is
2860          * defined as follows:
2861          *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
2862          * followed by a shift in of 18 bits.  This first two bits shifted in
2863          * are TurnAround bits used to avoid contention on the MDIO pin when a
2864          * READ operation is performed.  These two bits are thrown away
2865          * followed by a shift in of 16 bits which contains the desired data.
2866          */
2867         mdic = ((reg_addr) | (phy_addr << 5) |
2868                 (PHY_OP_READ << 10) | (PHY_SOF << 12));
2869
2870         e1000_shift_out_mdi_bits(hw, mdic, 14);
2871
2872         /* Now that we've shifted out the read command to the MII, we need to
2873          * "shift in" the 16-bit value (18 total bits) of the requested PHY
2874          * register address.
2875          */
2876         *phy_data = e1000_shift_in_mdi_bits(hw);
2877     }
2878     return E1000_SUCCESS;
2879 }
2880
2881 /******************************************************************************
2882 * Writes a value to a PHY register
2883 *
2884 * hw - Struct containing variables accessed by shared code
2885 * reg_addr - address of the PHY register to write
2886 * data - data to write to the PHY
2887 ******************************************************************************/
2888 int32_t
2889 e1000_write_phy_reg(struct e1000_hw *hw,
2890                     uint32_t reg_addr,
2891                     uint16_t phy_data)
2892 {
2893     uint32_t ret_val;
2894
2895     DEBUGFUNC("e1000_write_phy_reg");
2896
2897     if((hw->phy_type == e1000_phy_igp || 
2898         hw->phy_type == e1000_phy_igp_2) &&
2899        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2900         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2901                                          (uint16_t)reg_addr);
2902         if(ret_val) {
2903             return ret_val;
2904         }
2905     }
2906
2907     ret_val = e1000_write_phy_reg_ex(hw, MAX_PHY_REG_ADDRESS & reg_addr,
2908                                      phy_data);
2909
2910     return ret_val;
2911 }
2912
2913 int32_t
2914 e1000_write_phy_reg_ex(struct e1000_hw *hw,
2915                     uint32_t reg_addr,
2916                     uint16_t phy_data)
2917 {
2918     uint32_t i;
2919     uint32_t mdic = 0;
2920     const uint32_t phy_addr = 1;
2921
2922     DEBUGFUNC("e1000_write_phy_reg_ex");
2923
2924     if(reg_addr > MAX_PHY_REG_ADDRESS) {
2925         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2926         return -E1000_ERR_PARAM;
2927     }
2928
2929     if(hw->mac_type > e1000_82543) {
2930         /* Set up Op-code, Phy Address, register address, and data intended
2931          * for the PHY register in the MDI Control register.  The MAC will take
2932          * care of interfacing with the PHY to send the desired data.
2933          */
2934         mdic = (((uint32_t) phy_data) |
2935                 (reg_addr << E1000_MDIC_REG_SHIFT) |
2936                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
2937                 (E1000_MDIC_OP_WRITE));
2938
2939         E1000_WRITE_REG(hw, MDIC, mdic);
2940
2941         /* Poll the ready bit to see if the MDI read completed */
2942         for(i = 0; i < 640; i++) {
2943             udelay(5);
2944             mdic = E1000_READ_REG(hw, MDIC);
2945             if(mdic & E1000_MDIC_READY) break;
2946         }
2947         if(!(mdic & E1000_MDIC_READY)) {
2948             DEBUGOUT("MDI Write did not complete\n");
2949             return -E1000_ERR_PHY;
2950         }
2951     } else {
2952         /* We'll need to use the SW defined pins to shift the write command
2953          * out to the PHY. We first send a preamble to the PHY to signal the
2954          * beginning of the MII instruction.  This is done by sending 32
2955          * consecutive "1" bits.
2956          */
2957         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2958
2959         /* Now combine the remaining required fields that will indicate a
2960          * write operation. We use this method instead of calling the
2961          * e1000_shift_out_mdi_bits routine for each field in the command. The
2962          * format of a MII write instruction is as follows:
2963          * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
2964          */
2965         mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
2966                 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
2967         mdic <<= 16;
2968         mdic |= (uint32_t) phy_data;
2969
2970         e1000_shift_out_mdi_bits(hw, mdic, 32);
2971     }
2972
2973     return E1000_SUCCESS;
2974 }
2975
2976
2977 /******************************************************************************
2978 * Returns the PHY to the power-on reset state
2979 *
2980 * hw - Struct containing variables accessed by shared code
2981 ******************************************************************************/
2982 int32_t
2983 e1000_phy_hw_reset(struct e1000_hw *hw)
2984 {
2985     uint32_t ctrl, ctrl_ext;
2986     uint32_t led_ctrl;
2987     int32_t ret_val;
2988
2989     DEBUGFUNC("e1000_phy_hw_reset");
2990
2991     /* In the case of the phy reset being blocked, it's not an error, we
2992      * simply return success without performing the reset. */
2993     ret_val = e1000_check_phy_reset_block(hw);
2994     if (ret_val)
2995         return E1000_SUCCESS;
2996
2997     DEBUGOUT("Resetting Phy...\n");
2998
2999     if(hw->mac_type > e1000_82543) {
3000         /* Read the device control register and assert the E1000_CTRL_PHY_RST
3001          * bit. Then, take it out of reset.
3002          * For pre-e1000_82571 hardware, we delay for 10ms between the assert 
3003          * and deassert.  For e1000_82571 hardware and later, we instead delay
3004          * for 10ms after the deassertion.
3005          */
3006         ctrl = E1000_READ_REG(hw, CTRL);
3007         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
3008         E1000_WRITE_FLUSH(hw);
3009         
3010         if (hw->mac_type < e1000_82571) 
3011             msec_delay(10);
3012         else
3013             udelay(100);
3014         
3015         E1000_WRITE_REG(hw, CTRL, ctrl);
3016         E1000_WRITE_FLUSH(hw);
3017         
3018         if (hw->mac_type >= e1000_82571)
3019             msec_delay(10);
3020     } else {
3021         /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
3022          * bit to put the PHY into reset. Then, take it out of reset.
3023          */
3024         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
3025         ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
3026         ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
3027         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
3028         E1000_WRITE_FLUSH(hw);
3029         msec_delay(10);
3030         ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
3031         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
3032         E1000_WRITE_FLUSH(hw);
3033     }
3034     udelay(150);
3035
3036     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
3037         /* Configure activity LED after PHY reset */
3038         led_ctrl = E1000_READ_REG(hw, LEDCTL);
3039         led_ctrl &= IGP_ACTIVITY_LED_MASK;
3040         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
3041         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
3042     }
3043
3044     /* Wait for FW to finish PHY configuration. */
3045     ret_val = e1000_get_phy_cfg_done(hw);
3046
3047     return ret_val;
3048 }
3049
3050 /******************************************************************************
3051 * Resets the PHY
3052 *
3053 * hw - Struct containing variables accessed by shared code
3054 *
3055 * Sets bit 15 of the MII Control regiser
3056 ******************************************************************************/
3057 int32_t
3058 e1000_phy_reset(struct e1000_hw *hw)
3059 {
3060     int32_t ret_val;
3061     uint16_t phy_data;
3062
3063     DEBUGFUNC("e1000_phy_reset");
3064
3065     /* In the case of the phy reset being blocked, it's not an error, we
3066      * simply return success without performing the reset. */
3067     ret_val = e1000_check_phy_reset_block(hw);
3068     if (ret_val)
3069         return E1000_SUCCESS;
3070
3071     switch (hw->mac_type) {
3072     case e1000_82541_rev_2:
3073     case e1000_82571:
3074     case e1000_82572:
3075         ret_val = e1000_phy_hw_reset(hw);
3076         if(ret_val)
3077             return ret_val;
3078         break;
3079     default:
3080         ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
3081         if(ret_val)
3082             return ret_val;
3083
3084         phy_data |= MII_CR_RESET;
3085         ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
3086         if(ret_val)
3087             return ret_val;
3088
3089         udelay(1);
3090         break;
3091     }
3092
3093     if(hw->phy_type == e1000_phy_igp || hw->phy_type == e1000_phy_igp_2)
3094         e1000_phy_init_script(hw);
3095
3096     return E1000_SUCCESS;
3097 }
3098
3099 /******************************************************************************
3100 * Probes the expected PHY address for known PHY IDs
3101 *
3102 * hw - Struct containing variables accessed by shared code
3103 ******************************************************************************/
3104 static int32_t
3105 e1000_detect_gig_phy(struct e1000_hw *hw)
3106 {
3107     int32_t phy_init_status, ret_val;
3108     uint16_t phy_id_high, phy_id_low;
3109     boolean_t match = FALSE;
3110
3111     DEBUGFUNC("e1000_detect_gig_phy");
3112
3113     /* The 82571 firmware may still be configuring the PHY.  In this
3114      * case, we cannot access the PHY until the configuration is done.  So
3115      * we explicitly set the PHY values. */
3116     if(hw->mac_type == e1000_82571 ||
3117        hw->mac_type == e1000_82572) {
3118         hw->phy_id = IGP01E1000_I_PHY_ID;
3119         hw->phy_type = e1000_phy_igp_2;
3120         return E1000_SUCCESS;
3121     }
3122
3123     /* Read the PHY ID Registers to identify which PHY is onboard. */
3124     ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
3125     if(ret_val)
3126         return ret_val;
3127
3128     hw->phy_id = (uint32_t) (phy_id_high << 16);
3129     udelay(20);
3130     ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
3131     if(ret_val)
3132         return ret_val;
3133
3134     hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
3135     hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
3136
3137     switch(hw->mac_type) {
3138     case e1000_82543:
3139         if(hw->phy_id == M88E1000_E_PHY_ID) match = TRUE;
3140         break;
3141     case e1000_82544:
3142         if(hw->phy_id == M88E1000_I_PHY_ID) match = TRUE;
3143         break;
3144     case e1000_82540:
3145     case e1000_82545:
3146     case e1000_82545_rev_3:
3147     case e1000_82546:
3148     case e1000_82546_rev_3:
3149         if(hw->phy_id == M88E1011_I_PHY_ID) match = TRUE;
3150         break;
3151     case e1000_82541:
3152     case e1000_82541_rev_2:
3153     case e1000_82547:
3154     case e1000_82547_rev_2:
3155         if(hw->phy_id == IGP01E1000_I_PHY_ID) match = TRUE;
3156         break;
3157     case e1000_82573:
3158         if(hw->phy_id == M88E1111_I_PHY_ID) match = TRUE;
3159         break;
3160     default:
3161         DEBUGOUT1("Invalid MAC type %d\n", hw->mac_type);
3162         return -E1000_ERR_CONFIG;
3163     }
3164     phy_init_status = e1000_set_phy_type(hw);
3165
3166     if ((match) && (phy_init_status == E1000_SUCCESS)) {
3167         DEBUGOUT1("PHY ID 0x%X detected\n", hw->phy_id);
3168         return E1000_SUCCESS;
3169     }
3170     DEBUGOUT1("Invalid PHY ID 0x%X\n", hw->phy_id);
3171     return -E1000_ERR_PHY;
3172 }
3173
3174 /******************************************************************************
3175 * Resets the PHY's DSP
3176 *
3177 * hw - Struct containing variables accessed by shared code
3178 ******************************************************************************/
3179 static int32_t
3180 e1000_phy_reset_dsp(struct e1000_hw *hw)
3181 {
3182     int32_t ret_val;
3183     DEBUGFUNC("e1000_phy_reset_dsp");
3184
3185     do {
3186         ret_val = e1000_write_phy_reg(hw, 29, 0x001d);
3187         if(ret_val) break;
3188         ret_val = e1000_write_phy_reg(hw, 30, 0x00c1);
3189         if(ret_val) break;
3190         ret_val = e1000_write_phy_reg(hw, 30, 0x0000);
3191         if(ret_val) break;
3192         ret_val = E1000_SUCCESS;
3193     } while(0);
3194
3195     return ret_val;
3196 }
3197
3198 /******************************************************************************
3199 * Get PHY information from various PHY registers for igp PHY only.
3200 *
3201 * hw - Struct containing variables accessed by shared code
3202 * phy_info - PHY information structure
3203 ******************************************************************************/
3204 static int32_t
3205 e1000_phy_igp_get_info(struct e1000_hw *hw,
3206                        struct e1000_phy_info *phy_info)
3207 {
3208     int32_t ret_val;
3209     uint16_t phy_data, polarity, min_length, max_length, average;
3210
3211     DEBUGFUNC("e1000_phy_igp_get_info");
3212
3213     /* The downshift status is checked only once, after link is established,
3214      * and it stored in the hw->speed_downgraded parameter. */
3215     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3216
3217     /* IGP01E1000 does not need to support it. */
3218     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
3219
3220     /* IGP01E1000 always correct polarity reversal */
3221     phy_info->polarity_correction = e1000_polarity_reversal_enabled;
3222
3223     /* Check polarity status */
3224     ret_val = e1000_check_polarity(hw, &polarity);
3225     if(ret_val)
3226         return ret_val;
3227
3228     phy_info->cable_polarity = polarity;
3229
3230     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS, &phy_data);
3231     if(ret_val)
3232         return ret_val;
3233
3234     phy_info->mdix_mode = (phy_data & IGP01E1000_PSSR_MDIX) >>
3235                           IGP01E1000_PSSR_MDIX_SHIFT;
3236
3237     if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
3238        IGP01E1000_PSSR_SPEED_1000MBPS) {
3239         /* Local/Remote Receiver Information are only valid at 1000 Mbps */
3240         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3241         if(ret_val)
3242             return ret_val;
3243
3244         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3245                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
3246         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3247                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
3248
3249         /* Get cable length */
3250         ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
3251         if(ret_val)
3252             return ret_val;
3253
3254         /* Translate to old method */
3255         average = (max_length + min_length) / 2;
3256
3257         if(average <= e1000_igp_cable_length_50)
3258             phy_info->cable_length = e1000_cable_length_50;
3259         else if(average <= e1000_igp_cable_length_80)
3260             phy_info->cable_length = e1000_cable_length_50_80;
3261         else if(average <= e1000_igp_cable_length_110)
3262             phy_info->cable_length = e1000_cable_length_80_110;
3263         else if(average <= e1000_igp_cable_length_140)
3264             phy_info->cable_length = e1000_cable_length_110_140;
3265         else
3266             phy_info->cable_length = e1000_cable_length_140;
3267     }
3268
3269     return E1000_SUCCESS;
3270 }
3271
3272 /******************************************************************************
3273 * Get PHY information from various PHY registers fot m88 PHY only.
3274 *
3275 * hw - Struct containing variables accessed by shared code
3276 * phy_info - PHY information structure
3277 ******************************************************************************/
3278 static int32_t
3279 e1000_phy_m88_get_info(struct e1000_hw *hw,
3280                        struct e1000_phy_info *phy_info)
3281 {
3282     int32_t ret_val;
3283     uint16_t phy_data, polarity;
3284
3285     DEBUGFUNC("e1000_phy_m88_get_info");
3286
3287     /* The downshift status is checked only once, after link is established,
3288      * and it stored in the hw->speed_downgraded parameter. */
3289     phy_info->downshift = (e1000_downshift)hw->speed_downgraded;
3290
3291     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
3292     if(ret_val)
3293         return ret_val;
3294
3295     phy_info->extended_10bt_distance =
3296         (phy_data & M88E1000_PSCR_10BT_EXT_DIST_ENABLE) >>
3297         M88E1000_PSCR_10BT_EXT_DIST_ENABLE_SHIFT;
3298     phy_info->polarity_correction =
3299         (phy_data & M88E1000_PSCR_POLARITY_REVERSAL) >>
3300         M88E1000_PSCR_POLARITY_REVERSAL_SHIFT;
3301
3302     /* Check polarity status */
3303     ret_val = e1000_check_polarity(hw, &polarity);
3304     if(ret_val)
3305         return ret_val; 
3306     phy_info->cable_polarity = polarity;
3307
3308     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
3309     if(ret_val)
3310         return ret_val;
3311
3312     phy_info->mdix_mode = (phy_data & M88E1000_PSSR_MDIX) >>
3313                           M88E1000_PSSR_MDIX_SHIFT;
3314
3315     if ((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS) {
3316         /* Cable Length Estimation and Local/Remote Receiver Information
3317          * are only valid at 1000 Mbps.
3318          */
3319         phy_info->cable_length = ((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
3320                                   M88E1000_PSSR_CABLE_LENGTH_SHIFT);
3321
3322         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
3323         if(ret_val)
3324             return ret_val;
3325
3326         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
3327                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
3328
3329         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
3330                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
3331     }
3332
3333     return E1000_SUCCESS;
3334 }
3335
3336 /******************************************************************************
3337 * Get PHY information from various PHY registers
3338 *
3339 * hw - Struct containing variables accessed by shared code
3340 * phy_info - PHY information structure
3341 ******************************************************************************/
3342 int32_t
3343 e1000_phy_get_info(struct e1000_hw *hw,
3344                    struct e1000_phy_info *phy_info)
3345 {
3346     int32_t ret_val;
3347     uint16_t phy_data;
3348
3349     DEBUGFUNC("e1000_phy_get_info");
3350
3351     phy_info->cable_length = e1000_cable_length_undefined;
3352     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_undefined;
3353     phy_info->cable_polarity = e1000_rev_polarity_undefined;
3354     phy_info->downshift = e1000_downshift_undefined;
3355     phy_info->polarity_correction = e1000_polarity_reversal_undefined;
3356     phy_info->mdix_mode = e1000_auto_x_mode_undefined;
3357     phy_info->local_rx = e1000_1000t_rx_status_undefined;
3358     phy_info->remote_rx = e1000_1000t_rx_status_undefined;
3359
3360     if(hw->media_type != e1000_media_type_copper) {
3361         DEBUGOUT("PHY info is only valid for copper media\n");
3362         return -E1000_ERR_CONFIG;
3363     }
3364
3365     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3366     if(ret_val)
3367         return ret_val;
3368
3369     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
3370     if(ret_val)
3371         return ret_val;
3372
3373     if((phy_data & MII_SR_LINK_STATUS) != MII_SR_LINK_STATUS) {
3374         DEBUGOUT("PHY info is only valid if link is up\n");
3375         return -E1000_ERR_CONFIG;
3376     }
3377
3378     if(hw->phy_type == e1000_phy_igp ||
3379         hw->phy_type == e1000_phy_igp_2)
3380         return e1000_phy_igp_get_info(hw, phy_info);
3381     else
3382         return e1000_phy_m88_get_info(hw, phy_info);
3383 }
3384
3385 int32_t
3386 e1000_validate_mdi_setting(struct e1000_hw *hw)
3387 {
3388     DEBUGFUNC("e1000_validate_mdi_settings");
3389
3390     if(!hw->autoneg && (hw->mdix == 0 || hw->mdix == 3)) {
3391         DEBUGOUT("Invalid MDI setting detected\n");
3392         hw->mdix = 1;
3393         return -E1000_ERR_CONFIG;
3394     }
3395     return E1000_SUCCESS;
3396 }
3397
3398
3399 /******************************************************************************
3400  * Sets up eeprom variables in the hw struct.  Must be called after mac_type
3401  * is configured.
3402  *
3403  * hw - Struct containing variables accessed by shared code
3404  *****************************************************************************/
3405 int32_t
3406 e1000_init_eeprom_params(struct e1000_hw *hw)
3407 {
3408     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3409     uint32_t eecd = E1000_READ_REG(hw, EECD);
3410     int32_t ret_val = E1000_SUCCESS;
3411     uint16_t eeprom_size;
3412
3413     DEBUGFUNC("e1000_init_eeprom_params");
3414
3415     switch (hw->mac_type) {
3416     case e1000_82542_rev2_0:
3417     case e1000_82542_rev2_1:
3418     case e1000_82543:
3419     case e1000_82544:
3420         eeprom->type = e1000_eeprom_microwire;
3421         eeprom->word_size = 64;
3422         eeprom->opcode_bits = 3;
3423         eeprom->address_bits = 6;
3424         eeprom->delay_usec = 50;
3425         eeprom->use_eerd = FALSE;
3426         eeprom->use_eewr = FALSE;
3427         break;
3428     case e1000_82540:
3429     case e1000_82545:
3430     case e1000_82545_rev_3:
3431     case e1000_82546:
3432     case e1000_82546_rev_3:
3433         eeprom->type = e1000_eeprom_microwire;
3434         eeprom->opcode_bits = 3;
3435         eeprom->delay_usec = 50;
3436         if(eecd & E1000_EECD_SIZE) {
3437             eeprom->word_size = 256;
3438             eeprom->address_bits = 8;
3439         } else {
3440             eeprom->word_size = 64;
3441             eeprom->address_bits = 6;
3442         }
3443         eeprom->use_eerd = FALSE;
3444         eeprom->use_eewr = FALSE;
3445         break;
3446     case e1000_82541:
3447     case e1000_82541_rev_2:
3448     case e1000_82547:
3449     case e1000_82547_rev_2:
3450         if (eecd & E1000_EECD_TYPE) {
3451             eeprom->type = e1000_eeprom_spi;
3452             eeprom->opcode_bits = 8;
3453             eeprom->delay_usec = 1;
3454             if (eecd & E1000_EECD_ADDR_BITS) {
3455                 eeprom->page_size = 32;
3456                 eeprom->address_bits = 16;
3457             } else {
3458                 eeprom->page_size = 8;
3459                 eeprom->address_bits = 8;
3460             }
3461         } else {
3462             eeprom->type = e1000_eeprom_microwire;
3463             eeprom->opcode_bits = 3;
3464             eeprom->delay_usec = 50;
3465             if (eecd & E1000_EECD_ADDR_BITS) {
3466                 eeprom->word_size = 256;
3467                 eeprom->address_bits = 8;
3468             } else {
3469                 eeprom->word_size = 64;
3470                 eeprom->address_bits = 6;
3471             }
3472         }
3473         eeprom->use_eerd = FALSE;
3474         eeprom->use_eewr = FALSE;
3475         break;
3476     case e1000_82571:
3477     case e1000_82572:
3478         eeprom->type = e1000_eeprom_spi;
3479         eeprom->opcode_bits = 8;
3480         eeprom->delay_usec = 1;
3481         if (eecd & E1000_EECD_ADDR_BITS) {
3482             eeprom->page_size = 32;
3483             eeprom->address_bits = 16;
3484         } else {
3485             eeprom->page_size = 8;
3486             eeprom->address_bits = 8;
3487         }
3488         eeprom->use_eerd = FALSE;
3489         eeprom->use_eewr = FALSE;
3490         break;
3491     case e1000_82573:
3492         eeprom->type = e1000_eeprom_spi;
3493         eeprom->opcode_bits = 8;
3494         eeprom->delay_usec = 1;
3495         if (eecd & E1000_EECD_ADDR_BITS) {
3496             eeprom->page_size = 32;
3497             eeprom->address_bits = 16;
3498         } else {
3499             eeprom->page_size = 8;
3500             eeprom->address_bits = 8;
3501         }
3502         eeprom->use_eerd = TRUE;
3503         eeprom->use_eewr = TRUE;
3504         if(e1000_is_onboard_nvm_eeprom(hw) == FALSE) {
3505             eeprom->type = e1000_eeprom_flash;
3506             eeprom->word_size = 2048;
3507
3508             /* Ensure that the Autonomous FLASH update bit is cleared due to
3509              * Flash update issue on parts which use a FLASH for NVM. */
3510             eecd &= ~E1000_EECD_AUPDEN;
3511             E1000_WRITE_REG(hw, EECD, eecd);
3512         }
3513         break;
3514     default:
3515         break;
3516     }
3517
3518     if (eeprom->type == e1000_eeprom_spi) {
3519         /* eeprom_size will be an enum [0..8] that maps to eeprom sizes 128B to
3520          * 32KB (incremented by powers of 2).
3521          */
3522         if(hw->mac_type <= e1000_82547_rev_2) {
3523             /* Set to default value for initial eeprom read. */
3524             eeprom->word_size = 64;
3525             ret_val = e1000_read_eeprom(hw, EEPROM_CFG, 1, &eeprom_size);
3526             if(ret_val)
3527                 return ret_val;
3528             eeprom_size = (eeprom_size & EEPROM_SIZE_MASK) >> EEPROM_SIZE_SHIFT;
3529             /* 256B eeprom size was not supported in earlier hardware, so we
3530              * bump eeprom_size up one to ensure that "1" (which maps to 256B)
3531              * is never the result used in the shifting logic below. */
3532             if(eeprom_size)
3533                 eeprom_size++;
3534         } else {
3535             eeprom_size = (uint16_t)((eecd & E1000_EECD_SIZE_EX_MASK) >>
3536                           E1000_EECD_SIZE_EX_SHIFT);
3537         }
3538
3539         eeprom->word_size = 1 << (eeprom_size + EEPROM_WORD_SIZE_SHIFT);
3540     }
3541     return ret_val;
3542 }
3543
3544 /******************************************************************************
3545  * Raises the EEPROM's clock input.
3546  *
3547  * hw - Struct containing variables accessed by shared code
3548  * eecd - EECD's current value
3549  *****************************************************************************/
3550 static void
3551 e1000_raise_ee_clk(struct e1000_hw *hw,
3552                    uint32_t *eecd)
3553 {
3554     /* Raise the clock input to the EEPROM (by setting the SK bit), and then
3555      * wait <delay> microseconds.
3556      */
3557     *eecd = *eecd | E1000_EECD_SK;
3558     E1000_WRITE_REG(hw, EECD, *eecd);
3559     E1000_WRITE_FLUSH(hw);
3560     udelay(hw->eeprom.delay_usec);
3561 }
3562
3563 /******************************************************************************
3564  * Lowers the EEPROM's clock input.
3565  *
3566  * hw - Struct containing variables accessed by shared code
3567  * eecd - EECD's current value
3568  *****************************************************************************/
3569 static void
3570 e1000_lower_ee_clk(struct e1000_hw *hw,
3571                    uint32_t *eecd)
3572 {
3573     /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
3574      * wait 50 microseconds.
3575      */
3576     *eecd = *eecd & ~E1000_EECD_SK;
3577     E1000_WRITE_REG(hw, EECD, *eecd);
3578     E1000_WRITE_FLUSH(hw);
3579     udelay(hw->eeprom.delay_usec);
3580 }
3581
3582 /******************************************************************************
3583  * Shift data bits out to the EEPROM.
3584  *
3585  * hw - Struct containing variables accessed by shared code
3586  * data - data to send to the EEPROM
3587  * count - number of bits to shift out
3588  *****************************************************************************/
3589 static void
3590 e1000_shift_out_ee_bits(struct e1000_hw *hw,
3591                         uint16_t data,
3592                         uint16_t count)
3593 {
3594     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3595     uint32_t eecd;
3596     uint32_t mask;
3597
3598     /* We need to shift "count" bits out to the EEPROM. So, value in the
3599      * "data" parameter will be shifted out to the EEPROM one bit at a time.
3600      * In order to do this, "data" must be broken down into bits.
3601      */
3602     mask = 0x01 << (count - 1);
3603     eecd = E1000_READ_REG(hw, EECD);
3604     if (eeprom->type == e1000_eeprom_microwire) {
3605         eecd &= ~E1000_EECD_DO;
3606     } else if (eeprom->type == e1000_eeprom_spi) {
3607         eecd |= E1000_EECD_DO;
3608     }
3609     do {
3610         /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
3611          * and then raising and then lowering the clock (the SK bit controls
3612          * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
3613          * by setting "DI" to "0" and then raising and then lowering the clock.
3614          */
3615         eecd &= ~E1000_EECD_DI;
3616
3617         if(data & mask)
3618             eecd |= E1000_EECD_DI;
3619
3620         E1000_WRITE_REG(hw, EECD, eecd);
3621         E1000_WRITE_FLUSH(hw);
3622
3623         udelay(eeprom->delay_usec);
3624
3625         e1000_raise_ee_clk(hw, &eecd);
3626         e1000_lower_ee_clk(hw, &eecd);
3627
3628         mask = mask >> 1;
3629
3630     } while(mask);
3631
3632     /* We leave the "DI" bit set to "0" when we leave this routine. */
3633     eecd &= ~E1000_EECD_DI;
3634     E1000_WRITE_REG(hw, EECD, eecd);
3635 }
3636
3637 /******************************************************************************
3638  * Shift data bits in from the EEPROM
3639  *
3640  * hw - Struct containing variables accessed by shared code
3641  *****************************************************************************/
3642 static uint16_t
3643 e1000_shift_in_ee_bits(struct e1000_hw *hw,
3644                        uint16_t count)
3645 {
3646     uint32_t eecd;
3647     uint32_t i;
3648     uint16_t data;
3649
3650     /* In order to read a register from the EEPROM, we need to shift 'count'
3651      * bits in from the EEPROM. Bits are "shifted in" by raising the clock
3652      * input to the EEPROM (setting the SK bit), and then reading the value of
3653      * the "DO" bit.  During this "shifting in" process the "DI" bit should
3654      * always be clear.
3655      */
3656
3657     eecd = E1000_READ_REG(hw, EECD);
3658
3659     eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
3660     data = 0;
3661
3662     for(i = 0; i < count; i++) {
3663         data = data << 1;
3664         e1000_raise_ee_clk(hw, &eecd);
3665
3666         eecd = E1000_READ_REG(hw, EECD);
3667
3668         eecd &= ~(E1000_EECD_DI);
3669         if(eecd & E1000_EECD_DO)
3670             data |= 1;
3671
3672         e1000_lower_ee_clk(hw, &eecd);
3673     }
3674
3675     return data;
3676 }
3677
3678 /******************************************************************************
3679  * Prepares EEPROM for access
3680  *
3681  * hw - Struct containing variables accessed by shared code
3682  *
3683  * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
3684  * function should be called before issuing a command to the EEPROM.
3685  *****************************************************************************/
3686 static int32_t
3687 e1000_acquire_eeprom(struct e1000_hw *hw)
3688 {
3689     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3690     uint32_t eecd, i=0;
3691
3692     DEBUGFUNC("e1000_acquire_eeprom");
3693
3694     if(e1000_get_hw_eeprom_semaphore(hw))
3695         return -E1000_ERR_EEPROM;
3696
3697     eecd = E1000_READ_REG(hw, EECD);
3698
3699     if (hw->mac_type != e1000_82573) {
3700         /* Request EEPROM Access */
3701         if(hw->mac_type > e1000_82544) {
3702             eecd |= E1000_EECD_REQ;
3703             E1000_WRITE_REG(hw, EECD, eecd);
3704             eecd = E1000_READ_REG(hw, EECD);
3705             while((!(eecd & E1000_EECD_GNT)) &&
3706                   (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
3707                 i++;
3708                 udelay(5);
3709                 eecd = E1000_READ_REG(hw, EECD);
3710             }
3711             if(!(eecd & E1000_EECD_GNT)) {
3712                 eecd &= ~E1000_EECD_REQ;
3713                 E1000_WRITE_REG(hw, EECD, eecd);
3714                 DEBUGOUT("Could not acquire EEPROM grant\n");
3715                 e1000_put_hw_eeprom_semaphore(hw);
3716                 return -E1000_ERR_EEPROM;
3717             }
3718         }
3719     }
3720
3721     /* Setup EEPROM for Read/Write */
3722
3723     if (eeprom->type == e1000_eeprom_microwire) {
3724         /* Clear SK and DI */
3725         eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
3726         E1000_WRITE_REG(hw, EECD, eecd);
3727
3728         /* Set CS */
3729         eecd |= E1000_EECD_CS;
3730         E1000_WRITE_REG(hw, EECD, eecd);
3731     } else if (eeprom->type == e1000_eeprom_spi) {
3732         /* Clear SK and CS */
3733         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
3734         E1000_WRITE_REG(hw, EECD, eecd);
3735         udelay(1);
3736     }
3737
3738     return E1000_SUCCESS;
3739 }
3740
3741 /******************************************************************************
3742  * Returns EEPROM to a "standby" state
3743  *
3744  * hw - Struct containing variables accessed by shared code
3745  *****************************************************************************/
3746 static void
3747 e1000_standby_eeprom(struct e1000_hw *hw)
3748 {
3749     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3750     uint32_t eecd;
3751
3752     eecd = E1000_READ_REG(hw, EECD);
3753
3754     if(eeprom->type == e1000_eeprom_microwire) {
3755         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
3756         E1000_WRITE_REG(hw, EECD, eecd);
3757         E1000_WRITE_FLUSH(hw);
3758         udelay(eeprom->delay_usec);
3759
3760         /* Clock high */
3761         eecd |= E1000_EECD_SK;
3762         E1000_WRITE_REG(hw, EECD, eecd);
3763         E1000_WRITE_FLUSH(hw);
3764         udelay(eeprom->delay_usec);
3765
3766         /* Select EEPROM */
3767         eecd |= E1000_EECD_CS;
3768         E1000_WRITE_REG(hw, EECD, eecd);
3769         E1000_WRITE_FLUSH(hw);
3770         udelay(eeprom->delay_usec);
3771
3772         /* Clock low */
3773         eecd &= ~E1000_EECD_SK;
3774         E1000_WRITE_REG(hw, EECD, eecd);
3775         E1000_WRITE_FLUSH(hw);
3776         udelay(eeprom->delay_usec);
3777     } else if(eeprom->type == e1000_eeprom_spi) {
3778         /* Toggle CS to flush commands */
3779         eecd |= E1000_EECD_CS;
3780         E1000_WRITE_REG(hw, EECD, eecd);
3781         E1000_WRITE_FLUSH(hw);
3782         udelay(eeprom->delay_usec);
3783         eecd &= ~E1000_EECD_CS;
3784         E1000_WRITE_REG(hw, EECD, eecd);
3785         E1000_WRITE_FLUSH(hw);
3786         udelay(eeprom->delay_usec);
3787     }
3788 }
3789
3790 /******************************************************************************
3791  * Terminates a command by inverting the EEPROM's chip select pin
3792  *
3793  * hw - Struct containing variables accessed by shared code
3794  *****************************************************************************/
3795 static void
3796 e1000_release_eeprom(struct e1000_hw *hw)
3797 {
3798     uint32_t eecd;
3799
3800     DEBUGFUNC("e1000_release_eeprom");
3801
3802     eecd = E1000_READ_REG(hw, EECD);
3803
3804     if (hw->eeprom.type == e1000_eeprom_spi) {
3805         eecd |= E1000_EECD_CS;  /* Pull CS high */
3806         eecd &= ~E1000_EECD_SK; /* Lower SCK */
3807
3808         E1000_WRITE_REG(hw, EECD, eecd);
3809
3810         udelay(hw->eeprom.delay_usec);
3811     } else if(hw->eeprom.type == e1000_eeprom_microwire) {
3812         /* cleanup eeprom */
3813
3814         /* CS on Microwire is active-high */
3815         eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
3816
3817         E1000_WRITE_REG(hw, EECD, eecd);
3818
3819         /* Rising edge of clock */
3820         eecd |= E1000_EECD_SK;
3821         E1000_WRITE_REG(hw, EECD, eecd);
3822         E1000_WRITE_FLUSH(hw);
3823         udelay(hw->eeprom.delay_usec);
3824
3825         /* Falling edge of clock */
3826         eecd &= ~E1000_EECD_SK;
3827         E1000_WRITE_REG(hw, EECD, eecd);
3828         E1000_WRITE_FLUSH(hw);
3829         udelay(hw->eeprom.delay_usec);
3830     }
3831
3832     /* Stop requesting EEPROM access */
3833     if(hw->mac_type > e1000_82544) {
3834         eecd &= ~E1000_EECD_REQ;
3835         E1000_WRITE_REG(hw, EECD, eecd);
3836     }
3837
3838     e1000_put_hw_eeprom_semaphore(hw);
3839 }
3840
3841 /******************************************************************************
3842  * Reads a 16 bit word from the EEPROM.
3843  *
3844  * hw - Struct containing variables accessed by shared code
3845  *****************************************************************************/
3846 int32_t
3847 e1000_spi_eeprom_ready(struct e1000_hw *hw)
3848 {
3849     uint16_t retry_count = 0;
3850     uint8_t spi_stat_reg;
3851
3852     DEBUGFUNC("e1000_spi_eeprom_ready");
3853
3854     /* Read "Status Register" repeatedly until the LSB is cleared.  The
3855      * EEPROM will signal that the command has been completed by clearing
3856      * bit 0 of the internal status register.  If it's not cleared within
3857      * 5 milliseconds, then error out.
3858      */
3859     retry_count = 0;
3860     do {
3861         e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
3862                                 hw->eeprom.opcode_bits);
3863         spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
3864         if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
3865             break;
3866
3867         udelay(5);
3868         retry_count += 5;
3869
3870         e1000_standby_eeprom(hw);
3871     } while(retry_count < EEPROM_MAX_RETRY_SPI);
3872
3873     /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
3874      * only 0-5mSec on 5V devices)
3875      */
3876     if(retry_count >= EEPROM_MAX_RETRY_SPI) {
3877         DEBUGOUT("SPI EEPROM Status error\n");
3878         return -E1000_ERR_EEPROM;
3879     }
3880
3881     return E1000_SUCCESS;
3882 }
3883
3884 /******************************************************************************
3885  * Reads a 16 bit word from the EEPROM.
3886  *
3887  * hw - Struct containing variables accessed by shared code
3888  * offset - offset of  word in the EEPROM to read
3889  * data - word read from the EEPROM
3890  * words - number of words to read
3891  *****************************************************************************/
3892 int32_t
3893 e1000_read_eeprom(struct e1000_hw *hw,
3894                   uint16_t offset,
3895                   uint16_t words,
3896                   uint16_t *data)
3897 {
3898     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3899     uint32_t i = 0;
3900     int32_t ret_val;
3901
3902     DEBUGFUNC("e1000_read_eeprom");
3903
3904     /* A check for invalid values:  offset too large, too many words, and not
3905      * enough words.
3906      */
3907     if((offset >= eeprom->word_size) || (words > eeprom->word_size - offset) ||
3908        (words == 0)) {
3909         DEBUGOUT("\"words\" parameter out of bounds\n");
3910         return -E1000_ERR_EEPROM;
3911     }
3912
3913     /* FLASH reads without acquiring the semaphore are safe */
3914     if (e1000_is_onboard_nvm_eeprom(hw) == TRUE &&
3915     hw->eeprom.use_eerd == FALSE) {
3916         switch (hw->mac_type) {
3917         default:
3918             /* Prepare the EEPROM for reading  */
3919             if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
3920                 return -E1000_ERR_EEPROM;
3921             break;
3922         }
3923     }
3924
3925     if (eeprom->use_eerd == TRUE) {
3926         ret_val = e1000_read_eeprom_eerd(hw, offset, words, data);
3927         if ((e1000_is_onboard_nvm_eeprom(hw) == TRUE) ||
3928             (hw->mac_type != e1000_82573))
3929             e1000_release_eeprom(hw);
3930         return ret_val;
3931     }
3932
3933     if(eeprom->type == e1000_eeprom_spi) {
3934         uint16_t word_in;
3935         uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
3936
3937         if(e1000_spi_eeprom_ready(hw)) {
3938             e1000_release_eeprom(hw);
3939             return -E1000_ERR_EEPROM;
3940         }
3941
3942         e1000_standby_eeprom(hw);
3943
3944         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
3945         if((eeprom->address_bits == 8) && (offset >= 128))
3946             read_opcode |= EEPROM_A8_OPCODE_SPI;
3947
3948         /* Send the READ command (opcode + addr)  */
3949         e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
3950         e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2), eeprom->address_bits);
3951
3952         /* Read the data.  The address of the eeprom internally increments with
3953          * each byte (spi) being read, saving on the overhead of eeprom setup
3954          * and tear-down.  The address counter will roll over if reading beyond
3955          * the size of the eeprom, thus allowing the entire memory to be read
3956          * starting from any offset. */
3957         for (i = 0; i < words; i++) {
3958             word_in = e1000_shift_in_ee_bits(hw, 16);
3959             data[i] = (word_in >> 8) | (word_in << 8);
3960         }
3961     } else if(eeprom->type == e1000_eeprom_microwire) {
3962         for (i = 0; i < words; i++) {
3963             /* Send the READ command (opcode + addr)  */
3964             e1000_shift_out_ee_bits(hw, EEPROM_READ_OPCODE_MICROWIRE,
3965                                     eeprom->opcode_bits);
3966             e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
3967                                     eeprom->address_bits);
3968
3969             /* Read the data.  For microwire, each word requires the overhead
3970              * of eeprom setup and tear-down. */
3971             data[i] = e1000_shift_in_ee_bits(hw, 16);
3972             e1000_standby_eeprom(hw);
3973         }
3974     }
3975
3976     /* End this read operation */
3977     e1000_release_eeprom(hw);
3978
3979     return E1000_SUCCESS;
3980 }
3981
3982 /******************************************************************************
3983  * Reads a 16 bit word from the EEPROM using the EERD register.
3984  *
3985  * hw - Struct containing variables accessed by shared code
3986  * offset - offset of  word in the EEPROM to read
3987  * data - word read from the EEPROM
3988  * words - number of words to read
3989  *****************************************************************************/
3990 static int32_t
3991 e1000_read_eeprom_eerd(struct e1000_hw *hw,
3992                   uint16_t offset,
3993                   uint16_t words,
3994                   uint16_t *data)
3995 {
3996     uint32_t i, eerd = 0;
3997     int32_t error = 0;
3998
3999     for (i = 0; i < words; i++) {
4000         eerd = ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) +
4001                          E1000_EEPROM_RW_REG_START;
4002
4003         E1000_WRITE_REG(hw, EERD, eerd);
4004         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_READ);
4005         
4006         if(error) {
4007             break;
4008         }
4009         data[i] = (E1000_READ_REG(hw, EERD) >> E1000_EEPROM_RW_REG_DATA);
4010       
4011     }
4012     
4013     return error;
4014 }
4015
4016 /******************************************************************************
4017  * Writes a 16 bit word from the EEPROM using the EEWR register.
4018  *
4019  * hw - Struct containing variables accessed by shared code
4020  * offset - offset of  word in the EEPROM to read
4021  * data - word read from the EEPROM
4022  * words - number of words to read
4023  *****************************************************************************/
4024 static int32_t
4025 e1000_write_eeprom_eewr(struct e1000_hw *hw,
4026                    uint16_t offset,
4027                    uint16_t words,
4028                    uint16_t *data)
4029 {
4030     uint32_t    register_value = 0;
4031     uint32_t    i              = 0;
4032     int32_t     error          = 0;
4033
4034     for (i = 0; i < words; i++) {
4035         register_value = (data[i] << E1000_EEPROM_RW_REG_DATA) | 
4036                          ((offset+i) << E1000_EEPROM_RW_ADDR_SHIFT) | 
4037                          E1000_EEPROM_RW_REG_START;
4038
4039         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
4040         if(error) {
4041             break;
4042         }       
4043
4044         E1000_WRITE_REG(hw, EEWR, register_value);
4045         
4046         error = e1000_poll_eerd_eewr_done(hw, E1000_EEPROM_POLL_WRITE);
4047         
4048         if(error) {
4049             break;
4050         }       
4051     }
4052     
4053     return error;
4054 }
4055
4056 /******************************************************************************
4057  * Polls the status bit (bit 1) of the EERD to determine when the read is done.
4058  *
4059  * hw - Struct containing variables accessed by shared code
4060  *****************************************************************************/
4061 static int32_t
4062 e1000_poll_eerd_eewr_done(struct e1000_hw *hw, int eerd)
4063 {
4064     uint32_t attempts = 100000;
4065     uint32_t i, reg = 0;
4066     int32_t done = E1000_ERR_EEPROM;
4067
4068     for(i = 0; i < attempts; i++) {
4069         if(eerd == E1000_EEPROM_POLL_READ)
4070             reg = E1000_READ_REG(hw, EERD);
4071         else 
4072             reg = E1000_READ_REG(hw, EEWR);
4073
4074         if(reg & E1000_EEPROM_RW_REG_DONE) {
4075             done = E1000_SUCCESS;
4076             break;
4077         }
4078         udelay(5);
4079     }
4080
4081     return done;
4082 }
4083
4084 /***************************************************************************
4085 * Description:     Determines if the onboard NVM is FLASH or EEPROM.
4086 *
4087 * hw - Struct containing variables accessed by shared code
4088 ****************************************************************************/
4089 static boolean_t
4090 e1000_is_onboard_nvm_eeprom(struct e1000_hw *hw)
4091 {
4092     uint32_t eecd = 0;
4093
4094     if(hw->mac_type == e1000_82573) {
4095         eecd = E1000_READ_REG(hw, EECD);
4096
4097         /* Isolate bits 15 & 16 */
4098         eecd = ((eecd >> 15) & 0x03);
4099
4100         /* If both bits are set, device is Flash type */
4101         if(eecd == 0x03) {
4102             return FALSE;
4103         }
4104     }
4105     return TRUE;
4106 }
4107
4108 /******************************************************************************
4109  * Verifies that the EEPROM has a valid checksum
4110  *
4111  * hw - Struct containing variables accessed by shared code
4112  *
4113  * Reads the first 64 16 bit words of the EEPROM and sums the values read.
4114  * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
4115  * valid.
4116  *****************************************************************************/
4117 int32_t
4118 e1000_validate_eeprom_checksum(struct e1000_hw *hw)
4119 {
4120     uint16_t checksum = 0;
4121     uint16_t i, eeprom_data;
4122
4123     DEBUGFUNC("e1000_validate_eeprom_checksum");
4124
4125     if ((hw->mac_type == e1000_82573) &&
4126         (e1000_is_onboard_nvm_eeprom(hw) == FALSE)) {
4127         /* Check bit 4 of word 10h.  If it is 0, firmware is done updating
4128          * 10h-12h.  Checksum may need to be fixed. */
4129         e1000_read_eeprom(hw, 0x10, 1, &eeprom_data);
4130         if ((eeprom_data & 0x10) == 0) {
4131             /* Read 0x23 and check bit 15.  This bit is a 1 when the checksum
4132              * has already been fixed.  If the checksum is still wrong and this
4133              * bit is a 1, we need to return bad checksum.  Otherwise, we need
4134              * to set this bit to a 1 and update the checksum. */
4135             e1000_read_eeprom(hw, 0x23, 1, &eeprom_data);
4136             if ((eeprom_data & 0x8000) == 0) {
4137                 eeprom_data |= 0x8000;
4138                 e1000_write_eeprom(hw, 0x23, 1, &eeprom_data);
4139                 e1000_update_eeprom_checksum(hw);
4140             }
4141         }
4142     }
4143
4144     for(i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
4145         if(e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
4146             DEBUGOUT("EEPROM Read Error\n");
4147             return -E1000_ERR_EEPROM;
4148         }
4149         checksum += eeprom_data;
4150     }
4151
4152     if(checksum == (uint16_t) EEPROM_SUM)
4153         return E1000_SUCCESS;
4154     else {
4155         DEBUGOUT("EEPROM Checksum Invalid\n");
4156         return -E1000_ERR_EEPROM;
4157     }
4158 }
4159
4160 /******************************************************************************
4161  * Calculates the EEPROM checksum and writes it to the EEPROM
4162  *
4163  * hw - Struct containing variables accessed by shared code
4164  *
4165  * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
4166  * Writes the difference to word offset 63 of the EEPROM.
4167  *****************************************************************************/
4168 int32_t
4169 e1000_update_eeprom_checksum(struct e1000_hw *hw)
4170 {
4171     uint16_t checksum = 0;
4172     uint16_t i, eeprom_data;
4173
4174     DEBUGFUNC("e1000_update_eeprom_checksum");
4175
4176     for(i = 0; i < EEPROM_CHECKSUM_REG; i++) {
4177         if(e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
4178             DEBUGOUT("EEPROM Read Error\n");
4179             return -E1000_ERR_EEPROM;
4180         }
4181         checksum += eeprom_data;
4182     }
4183     checksum = (uint16_t) EEPROM_SUM - checksum;
4184     if(e1000_write_eeprom(hw, EEPROM_CHECKSUM_REG, 1, &checksum) < 0) {
4185         DEBUGOUT("EEPROM Write Error\n");
4186         return -E1000_ERR_EEPROM;
4187     } else if (hw->eeprom.type == e1000_eeprom_flash) {
4188         e1000_commit_shadow_ram(hw);
4189     }
4190     return E1000_SUCCESS;
4191 }
4192
4193 /******************************************************************************
4194  * Parent function for writing words to the different EEPROM types.
4195  *
4196  * hw - Struct containing variables accessed by shared code
4197  * offset - offset within the EEPROM to be written to
4198  * words - number of words to write
4199  * data - 16 bit word to be written to the EEPROM
4200  *
4201  * If e1000_update_eeprom_checksum is not called after this function, the
4202  * EEPROM will most likely contain an invalid checksum.
4203  *****************************************************************************/
4204 int32_t
4205 e1000_write_eeprom(struct e1000_hw *hw,
4206                    uint16_t offset,
4207                    uint16_t words,
4208                    uint16_t *data)
4209 {
4210     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4211     int32_t status = 0;
4212
4213     DEBUGFUNC("e1000_write_eeprom");
4214
4215     /* A check for invalid values:  offset too large, too many words, and not
4216      * enough words.
4217      */
4218     if((offset >= eeprom->word_size) || (words > eeprom->word_size - offset) ||
4219        (words == 0)) {
4220         DEBUGOUT("\"words\" parameter out of bounds\n");
4221         return -E1000_ERR_EEPROM;
4222     }
4223
4224     /* 82573 writes only through eewr */
4225     if(eeprom->use_eewr == TRUE)
4226         return e1000_write_eeprom_eewr(hw, offset, words, data);
4227
4228     /* Prepare the EEPROM for writing  */
4229     if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
4230         return -E1000_ERR_EEPROM;
4231
4232     if(eeprom->type == e1000_eeprom_microwire) {
4233         status = e1000_write_eeprom_microwire(hw, offset, words, data);
4234     } else {
4235         status = e1000_write_eeprom_spi(hw, offset, words, data);
4236         msec_delay(10);
4237     }
4238
4239     /* Done with writing */
4240     e1000_release_eeprom(hw);
4241
4242     return status;
4243 }
4244
4245 /******************************************************************************
4246  * Writes a 16 bit word to a given offset in an SPI EEPROM.
4247  *
4248  * hw - Struct containing variables accessed by shared code
4249  * offset - offset within the EEPROM to be written to
4250  * words - number of words to write
4251  * data - pointer to array of 8 bit words to be written to the EEPROM
4252  *
4253  *****************************************************************************/
4254 int32_t
4255 e1000_write_eeprom_spi(struct e1000_hw *hw,
4256                        uint16_t offset,
4257                        uint16_t words,
4258                        uint16_t *data)
4259 {
4260     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4261     uint16_t widx = 0;
4262
4263     DEBUGFUNC("e1000_write_eeprom_spi");
4264
4265     while (widx < words) {
4266         uint8_t write_opcode = EEPROM_WRITE_OPCODE_SPI;
4267
4268         if(e1000_spi_eeprom_ready(hw)) return -E1000_ERR_EEPROM;
4269
4270         e1000_standby_eeprom(hw);
4271
4272         /*  Send the WRITE ENABLE command (8 bit opcode )  */
4273         e1000_shift_out_ee_bits(hw, EEPROM_WREN_OPCODE_SPI,
4274                                     eeprom->opcode_bits);
4275
4276         e1000_standby_eeprom(hw);
4277
4278         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
4279         if((eeprom->address_bits == 8) && (offset >= 128))
4280             write_opcode |= EEPROM_A8_OPCODE_SPI;
4281
4282         /* Send the Write command (8-bit opcode + addr) */
4283         e1000_shift_out_ee_bits(hw, write_opcode, eeprom->opcode_bits);
4284
4285         e1000_shift_out_ee_bits(hw, (uint16_t)((offset + widx)*2),
4286                                 eeprom->address_bits);
4287
4288         /* Send the data */
4289
4290         /* Loop to allow for up to whole page write (32 bytes) of eeprom */
4291         while (widx < words) {
4292             uint16_t word_out = data[widx];
4293             word_out = (word_out >> 8) | (word_out << 8);
4294             e1000_shift_out_ee_bits(hw, word_out, 16);
4295             widx++;
4296
4297             /* Some larger eeprom sizes are capable of a 32-byte PAGE WRITE
4298              * operation, while the smaller eeproms are capable of an 8-byte
4299              * PAGE WRITE operation.  Break the inner loop to pass new address
4300              */
4301             if((((offset + widx)*2) % eeprom->page_size) == 0) {
4302                 e1000_standby_eeprom(hw);
4303                 break;
4304             }
4305         }
4306     }
4307
4308     return E1000_SUCCESS;
4309 }
4310
4311 /******************************************************************************
4312  * Writes a 16 bit word to a given offset in a Microwire EEPROM.
4313  *
4314  * hw - Struct containing variables accessed by shared code
4315  * offset - offset within the EEPROM to be written to
4316  * words - number of words to write
4317  * data - pointer to array of 16 bit words to be written to the EEPROM
4318  *
4319  *****************************************************************************/
4320 int32_t
4321 e1000_write_eeprom_microwire(struct e1000_hw *hw,
4322                              uint16_t offset,
4323                              uint16_t words,
4324                              uint16_t *data)
4325 {
4326     struct e1000_eeprom_info *eeprom = &hw->eeprom;
4327     uint32_t eecd;
4328     uint16_t words_written = 0;
4329     uint16_t i = 0;
4330
4331     DEBUGFUNC("e1000_write_eeprom_microwire");
4332
4333     /* Send the write enable command to the EEPROM (3-bit opcode plus
4334      * 6/8-bit dummy address beginning with 11).  It's less work to include
4335      * the 11 of the dummy address as part of the opcode than it is to shift
4336      * it over the correct number of bits for the address.  This puts the
4337      * EEPROM into write/erase mode.
4338      */
4339     e1000_shift_out_ee_bits(hw, EEPROM_EWEN_OPCODE_MICROWIRE,
4340                             (uint16_t)(eeprom->opcode_bits + 2));
4341
4342     e1000_shift_out_ee_bits(hw, 0, (uint16_t)(eeprom->address_bits - 2));
4343
4344     /* Prepare the EEPROM */
4345     e1000_standby_eeprom(hw);
4346
4347     while (words_written < words) {
4348         /* Send the Write command (3-bit opcode + addr) */
4349         e1000_shift_out_ee_bits(hw, EEPROM_WRITE_OPCODE_MICROWIRE,
4350                                 eeprom->opcode_bits);
4351
4352         e1000_shift_out_ee_bits(hw, (uint16_t)(offset + words_written),
4353                                 eeprom->address_bits);
4354
4355         /* Send the data */
4356         e1000_shift_out_ee_bits(hw, data[words_written], 16);
4357
4358         /* Toggle the CS line.  This in effect tells the EEPROM to execute
4359          * the previous command.
4360          */
4361         e1000_standby_eeprom(hw);
4362
4363         /* Read DO repeatedly until it is high (equal to '1').  The EEPROM will
4364          * signal that the command has been completed by raising the DO signal.
4365          * If DO does not go high in 10 milliseconds, then error out.
4366          */
4367         for(i = 0; i < 200; i++) {
4368             eecd = E1000_READ_REG(hw, EECD);
4369             if(eecd & E1000_EECD_DO) break;
4370             udelay(50);
4371         }
4372         if(i == 200) {
4373             DEBUGOUT("EEPROM Write did not complete\n");
4374             return -E1000_ERR_EEPROM;
4375         }
4376
4377         /* Recover from write */
4378         e1000_standby_eeprom(hw);
4379
4380         words_written++;
4381     }
4382
4383     /* Send the write disable command to the EEPROM (3-bit opcode plus
4384      * 6/8-bit dummy address beginning with 10).  It's less work to include
4385      * the 10 of the dummy address as part of the opcode than it is to shift
4386      * it over the correct number of bits for the address.  This takes the
4387      * EEPROM out of write/erase mode.
4388      */
4389     e1000_shift_out_ee_bits(hw, EEPROM_EWDS_OPCODE_MICROWIRE,
4390                             (uint16_t)(eeprom->opcode_bits + 2));
4391
4392     e1000_shift_out_ee_bits(hw, 0, (uint16_t)(eeprom->address_bits - 2));
4393
4394     return E1000_SUCCESS;
4395 }
4396
4397 /******************************************************************************
4398  * Flushes the cached eeprom to NVM. This is done by saving the modified values
4399  * in the eeprom cache and the non modified values in the currently active bank
4400  * to the new bank.
4401  *
4402  * hw - Struct containing variables accessed by shared code
4403  * offset - offset of  word in the EEPROM to read
4404  * data - word read from the EEPROM
4405  * words - number of words to read
4406  *****************************************************************************/
4407 static int32_t
4408 e1000_commit_shadow_ram(struct e1000_hw *hw)
4409 {
4410     uint32_t attempts = 100000;
4411     uint32_t eecd = 0;
4412     uint32_t flop = 0;
4413     uint32_t i = 0;
4414     int32_t error = E1000_SUCCESS;
4415
4416     /* The flop register will be used to determine if flash type is STM */
4417     flop = E1000_READ_REG(hw, FLOP);
4418
4419     if (hw->mac_type == e1000_82573) {
4420         for (i=0; i < attempts; i++) {
4421             eecd = E1000_READ_REG(hw, EECD);
4422             if ((eecd & E1000_EECD_FLUPD) == 0) {
4423                 break;
4424             }
4425             udelay(5);
4426         }
4427
4428         if (i == attempts) {
4429             return -E1000_ERR_EEPROM;
4430         }
4431
4432         /* If STM opcode located in bits 15:8 of flop, reset firmware */
4433         if ((flop & 0xFF00) == E1000_STM_OPCODE) {
4434             E1000_WRITE_REG(hw, HICR, E1000_HICR_FW_RESET);
4435         }
4436
4437         /* Perform the flash update */
4438         E1000_WRITE_REG(hw, EECD, eecd | E1000_EECD_FLUPD);
4439
4440         for (i=0; i < attempts; i++) {
4441             eecd = E1000_READ_REG(hw, EECD);
4442             if ((eecd & E1000_EECD_FLUPD) == 0) {
4443                 break;
4444             }
4445             udelay(5);
4446         }
4447
4448         if (i == attempts) {
4449             return -E1000_ERR_EEPROM;
4450         }
4451     }
4452
4453     return error;
4454 }
4455
4456 /******************************************************************************
4457  * Reads the adapter's part number from the EEPROM
4458  *
4459  * hw - Struct containing variables accessed by shared code
4460  * part_num - Adapter's part number
4461  *****************************************************************************/
4462 int32_t
4463 e1000_read_part_num(struct e1000_hw *hw,
4464                     uint32_t *part_num)
4465 {
4466     uint16_t offset = EEPROM_PBA_BYTE_1;
4467     uint16_t eeprom_data;
4468
4469     DEBUGFUNC("e1000_read_part_num");
4470
4471     /* Get word 0 from EEPROM */
4472     if(e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
4473         DEBUGOUT("EEPROM Read Error\n");
4474         return -E1000_ERR_EEPROM;
4475     }
4476     /* Save word 0 in upper half of part_num */
4477     *part_num = (uint32_t) (eeprom_data << 16);
4478
4479     /* Get word 1 from EEPROM */
4480     if(e1000_read_eeprom(hw, ++offset, 1, &eeprom_data) < 0) {
4481         DEBUGOUT("EEPROM Read Error\n");
4482         return -E1000_ERR_EEPROM;
4483     }
4484     /* Save word 1 in lower half of part_num */
4485     *part_num |= eeprom_data;
4486
4487     return E1000_SUCCESS;
4488 }
4489
4490 /******************************************************************************
4491  * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
4492  * second function of dual function devices
4493  *
4494  * hw - Struct containing variables accessed by shared code
4495  *****************************************************************************/
4496 int32_t
4497 e1000_read_mac_addr(struct e1000_hw * hw)
4498 {
4499     uint16_t offset;
4500     uint16_t eeprom_data, i;
4501
4502     DEBUGFUNC("e1000_read_mac_addr");
4503
4504     for(i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
4505         offset = i >> 1;
4506         if(e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
4507             DEBUGOUT("EEPROM Read Error\n");
4508             return -E1000_ERR_EEPROM;
4509         }
4510         hw->perm_mac_addr[i] = (uint8_t) (eeprom_data & 0x00FF);
4511         hw->perm_mac_addr[i+1] = (uint8_t) (eeprom_data >> 8);
4512     }
4513
4514     switch (hw->mac_type) {
4515     default:
4516         break;
4517     case e1000_82546:
4518     case e1000_82546_rev_3:
4519     case e1000_82571:
4520         if(E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1)
4521             hw->perm_mac_addr[5] ^= 0x01;
4522         break;
4523     }
4524
4525     for(i = 0; i < NODE_ADDRESS_SIZE; i++)
4526         hw->mac_addr[i] = hw->perm_mac_addr[i];
4527     return E1000_SUCCESS;
4528 }
4529
4530 /******************************************************************************
4531  * Initializes receive address filters.
4532  *
4533  * hw - Struct containing variables accessed by shared code
4534  *
4535  * Places the MAC address in receive address register 0 and clears the rest
4536  * of the receive addresss registers. Clears the multicast table. Assumes
4537  * the receiver is in reset when the routine is called.
4538  *****************************************************************************/
4539 static void
4540 e1000_init_rx_addrs(struct e1000_hw *hw)
4541 {
4542     uint32_t i;
4543     uint32_t rar_num;
4544
4545     DEBUGFUNC("e1000_init_rx_addrs");
4546
4547     /* Setup the receive address. */
4548     DEBUGOUT("Programming MAC Address into RAR[0]\n");
4549
4550     e1000_rar_set(hw, hw->mac_addr, 0);
4551
4552     rar_num = E1000_RAR_ENTRIES;
4553
4554     /* Reserve a spot for the Locally Administered Address to work around
4555      * an 82571 issue in which a reset on one port will reload the MAC on
4556      * the other port. */
4557     if ((hw->mac_type == e1000_82571) && (hw->laa_is_present == TRUE))
4558         rar_num -= 1;
4559     /* Zero out the other 15 receive addresses. */
4560     DEBUGOUT("Clearing RAR[1-15]\n");
4561     for(i = 1; i < rar_num; i++) {
4562         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
4563         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
4564     }
4565 }
4566
4567 #if 0
4568 /******************************************************************************
4569  * Updates the MAC's list of multicast addresses.
4570  *
4571  * hw - Struct containing variables accessed by shared code
4572  * mc_addr_list - the list of new multicast addresses
4573  * mc_addr_count - number of addresses
4574  * pad - number of bytes between addresses in the list
4575  * rar_used_count - offset where to start adding mc addresses into the RAR's
4576  *
4577  * The given list replaces any existing list. Clears the last 15 receive
4578  * address registers and the multicast table. Uses receive address registers
4579  * for the first 15 multicast addresses, and hashes the rest into the
4580  * multicast table.
4581  *****************************************************************************/
4582 void
4583 e1000_mc_addr_list_update(struct e1000_hw *hw,
4584                           uint8_t *mc_addr_list,
4585                           uint32_t mc_addr_count,
4586                           uint32_t pad,
4587                           uint32_t rar_used_count)
4588 {
4589     uint32_t hash_value;
4590     uint32_t i;
4591     uint32_t num_rar_entry;
4592     uint32_t num_mta_entry;
4593     
4594     DEBUGFUNC("e1000_mc_addr_list_update");
4595
4596     /* Set the new number of MC addresses that we are being requested to use. */
4597     hw->num_mc_addrs = mc_addr_count;
4598
4599     /* Clear RAR[1-15] */
4600     DEBUGOUT(" Clearing RAR[1-15]\n");
4601     num_rar_entry = E1000_RAR_ENTRIES;
4602     /* Reserve a spot for the Locally Administered Address to work around
4603      * an 82571 issue in which a reset on one port will reload the MAC on
4604      * the other port. */
4605     if ((hw->mac_type == e1000_82571) && (hw->laa_is_present == TRUE))
4606         num_rar_entry -= 1;
4607
4608     for(i = rar_used_count; i < num_rar_entry; i++) {
4609         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
4610         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
4611     }
4612
4613     /* Clear the MTA */
4614     DEBUGOUT(" Clearing MTA\n");
4615     num_mta_entry = E1000_NUM_MTA_REGISTERS;
4616     for(i = 0; i < num_mta_entry; i++) {
4617         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
4618     }
4619
4620     /* Add the new addresses */
4621     for(i = 0; i < mc_addr_count; i++) {
4622         DEBUGOUT(" Adding the multicast addresses:\n");
4623         DEBUGOUT7(" MC Addr #%d =%.2X %.2X %.2X %.2X %.2X %.2X\n", i,
4624                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad)],
4625                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 1],
4626                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 2],
4627                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 3],
4628                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 4],
4629                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 5]);
4630
4631         hash_value = e1000_hash_mc_addr(hw,
4632                                         mc_addr_list +
4633                                         (i * (ETH_LENGTH_OF_ADDRESS + pad)));
4634
4635         DEBUGOUT1(" Hash value = 0x%03X\n", hash_value);
4636
4637         /* Place this multicast address in the RAR if there is room, *
4638          * else put it in the MTA
4639          */
4640         if (rar_used_count < num_rar_entry) {
4641             e1000_rar_set(hw,
4642                           mc_addr_list + (i * (ETH_LENGTH_OF_ADDRESS + pad)),
4643                           rar_used_count);
4644             rar_used_count++;
4645         } else {
4646             e1000_mta_set(hw, hash_value);
4647         }
4648     }
4649     DEBUGOUT("MC Update Complete\n");
4650 }
4651 #endif  /*  0  */
4652
4653 /******************************************************************************
4654  * Hashes an address to determine its location in the multicast table
4655  *
4656  * hw - Struct containing variables accessed by shared code
4657  * mc_addr - the multicast address to hash
4658  *****************************************************************************/
4659 uint32_t
4660 e1000_hash_mc_addr(struct e1000_hw *hw,
4661                    uint8_t *mc_addr)
4662 {
4663     uint32_t hash_value = 0;
4664
4665     /* The portion of the address that is used for the hash table is
4666      * determined by the mc_filter_type setting.
4667      */
4668     switch (hw->mc_filter_type) {
4669     /* [0] [1] [2] [3] [4] [5]
4670      * 01  AA  00  12  34  56
4671      * LSB                 MSB
4672      */
4673     case 0:
4674         /* [47:36] i.e. 0x563 for above example address */
4675         hash_value = ((mc_addr[4] >> 4) | (((uint16_t) mc_addr[5]) << 4));
4676         break;
4677     case 1:
4678         /* [46:35] i.e. 0xAC6 for above example address */
4679         hash_value = ((mc_addr[4] >> 3) | (((uint16_t) mc_addr[5]) << 5));
4680         break;
4681     case 2:
4682         /* [45:34] i.e. 0x5D8 for above example address */
4683         hash_value = ((mc_addr[4] >> 2) | (((uint16_t) mc_addr[5]) << 6));
4684         break;
4685     case 3:
4686         /* [43:32] i.e. 0x634 for above example address */
4687         hash_value = ((mc_addr[4]) | (((uint16_t) mc_addr[5]) << 8));
4688         break;
4689     }
4690
4691     hash_value &= 0xFFF;
4692
4693     return hash_value;
4694 }
4695
4696 /******************************************************************************
4697  * Sets the bit in the multicast table corresponding to the hash value.
4698  *
4699  * hw - Struct containing variables accessed by shared code
4700  * hash_value - Multicast address hash value
4701  *****************************************************************************/
4702 void
4703 e1000_mta_set(struct e1000_hw *hw,
4704               uint32_t hash_value)
4705 {
4706     uint32_t hash_bit, hash_reg;
4707     uint32_t mta;
4708     uint32_t temp;
4709
4710     /* The MTA is a register array of 128 32-bit registers.
4711      * It is treated like an array of 4096 bits.  We want to set
4712      * bit BitArray[hash_value]. So we figure out what register
4713      * the bit is in, read it, OR in the new bit, then write
4714      * back the new value.  The register is determined by the
4715      * upper 7 bits of the hash value and the bit within that
4716      * register are determined by the lower 5 bits of the value.
4717      */
4718     hash_reg = (hash_value >> 5) & 0x7F;
4719     hash_bit = hash_value & 0x1F;
4720
4721     mta = E1000_READ_REG_ARRAY(hw, MTA, hash_reg);
4722
4723     mta |= (1 << hash_bit);
4724
4725     /* If we are on an 82544 and we are trying to write an odd offset
4726      * in the MTA, save off the previous entry before writing and
4727      * restore the old value after writing.
4728      */
4729     if((hw->mac_type == e1000_82544) && ((hash_reg & 0x1) == 1)) {
4730         temp = E1000_READ_REG_ARRAY(hw, MTA, (hash_reg - 1));
4731         E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
4732         E1000_WRITE_REG_ARRAY(hw, MTA, (hash_reg - 1), temp);
4733     } else {
4734         E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
4735     }
4736 }
4737
4738 /******************************************************************************
4739  * Puts an ethernet address into a receive address register.
4740  *
4741  * hw - Struct containing variables accessed by shared code
4742  * addr - Address to put into receive address register
4743  * index - Receive address register to write
4744  *****************************************************************************/
4745 void
4746 e1000_rar_set(struct e1000_hw *hw,
4747               uint8_t *addr,
4748               uint32_t index)
4749 {
4750     uint32_t rar_low, rar_high;
4751
4752     /* HW expects these in little endian so we reverse the byte order
4753      * from network order (big endian) to little endian
4754      */
4755     rar_low = ((uint32_t) addr[0] |
4756                ((uint32_t) addr[1] << 8) |
4757                ((uint32_t) addr[2] << 16) | ((uint32_t) addr[3] << 24));
4758     rar_high = ((uint32_t) addr[4] | ((uint32_t) addr[5] << 8));
4759
4760     /* Disable Rx and flush all Rx frames before enabling RSS to avoid Rx
4761      * unit hang.
4762      *
4763      * Description:
4764      * If there are any Rx frames queued up or otherwise present in the HW
4765      * before RSS is enabled, and then we enable RSS, the HW Rx unit will
4766      * hang.  To work around this issue, we have to disable receives and
4767      * flush out all Rx frames before we enable RSS. To do so, we modify we
4768      * redirect all Rx traffic to manageability and then reset the HW.
4769      * This flushes away Rx frames, and (since the redirections to
4770      * manageability persists across resets) keeps new ones from coming in
4771      * while we work.  Then, we clear the Address Valid AV bit for all MAC
4772      * addresses and undo the re-direction to manageability.
4773      * Now, frames are coming in again, but the MAC won't accept them, so
4774      * far so good.  We now proceed to initialize RSS (if necessary) and
4775      * configure the Rx unit.  Last, we re-enable the AV bits and continue
4776      * on our merry way.
4777      */
4778     switch (hw->mac_type) {
4779     case e1000_82571:
4780     case e1000_82572:
4781         if (hw->leave_av_bit_off == TRUE)
4782             break;
4783     default:
4784         /* Indicate to hardware the Address is Valid. */
4785         rar_high |= E1000_RAH_AV;
4786         break;
4787     }
4788
4789     E1000_WRITE_REG_ARRAY(hw, RA, (index << 1), rar_low);
4790     E1000_WRITE_REG_ARRAY(hw, RA, ((index << 1) + 1), rar_high);
4791 }
4792
4793 /******************************************************************************
4794  * Writes a value to the specified offset in the VLAN filter table.
4795  *
4796  * hw - Struct containing variables accessed by shared code
4797  * offset - Offset in VLAN filer table to write
4798  * value - Value to write into VLAN filter table
4799  *****************************************************************************/
4800 void
4801 e1000_write_vfta(struct e1000_hw *hw,
4802                  uint32_t offset,
4803                  uint32_t value)
4804 {
4805     uint32_t temp;
4806
4807     if((hw->mac_type == e1000_82544) && ((offset & 0x1) == 1)) {
4808         temp = E1000_READ_REG_ARRAY(hw, VFTA, (offset - 1));
4809         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
4810         E1000_WRITE_REG_ARRAY(hw, VFTA, (offset - 1), temp);
4811     } else {
4812         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
4813     }
4814 }
4815
4816 /******************************************************************************
4817  * Clears the VLAN filer table
4818  *
4819  * hw - Struct containing variables accessed by shared code
4820  *****************************************************************************/
4821 static void
4822 e1000_clear_vfta(struct e1000_hw *hw)
4823 {
4824     uint32_t offset;
4825     uint32_t vfta_value = 0;
4826     uint32_t vfta_offset = 0;
4827     uint32_t vfta_bit_in_reg = 0;
4828
4829     if (hw->mac_type == e1000_82573) {
4830         if (hw->mng_cookie.vlan_id != 0) {
4831             /* The VFTA is a 4096b bit-field, each identifying a single VLAN
4832              * ID.  The following operations determine which 32b entry
4833              * (i.e. offset) into the array we want to set the VLAN ID
4834              * (i.e. bit) of the manageability unit. */
4835             vfta_offset = (hw->mng_cookie.vlan_id >>
4836                            E1000_VFTA_ENTRY_SHIFT) &
4837                           E1000_VFTA_ENTRY_MASK;
4838             vfta_bit_in_reg = 1 << (hw->mng_cookie.vlan_id &
4839                                     E1000_VFTA_ENTRY_BIT_SHIFT_MASK);
4840         }
4841     }
4842     for (offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++) {
4843         /* If the offset we want to clear is the same offset of the
4844          * manageability VLAN ID, then clear all bits except that of the
4845          * manageability unit */
4846         vfta_value = (offset == vfta_offset) ? vfta_bit_in_reg : 0;
4847         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, vfta_value);
4848     }
4849 }
4850
4851 static int32_t
4852 e1000_id_led_init(struct e1000_hw * hw)
4853 {
4854     uint32_t ledctl;
4855     const uint32_t ledctl_mask = 0x000000FF;
4856     const uint32_t ledctl_on = E1000_LEDCTL_MODE_LED_ON;
4857     const uint32_t ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
4858     uint16_t eeprom_data, i, temp;
4859     const uint16_t led_mask = 0x0F;
4860
4861     DEBUGFUNC("e1000_id_led_init");
4862
4863     if(hw->mac_type < e1000_82540) {
4864         /* Nothing to do */
4865         return E1000_SUCCESS;
4866     }
4867
4868     ledctl = E1000_READ_REG(hw, LEDCTL);
4869     hw->ledctl_default = ledctl;
4870     hw->ledctl_mode1 = hw->ledctl_default;
4871     hw->ledctl_mode2 = hw->ledctl_default;
4872
4873     if(e1000_read_eeprom(hw, EEPROM_ID_LED_SETTINGS, 1, &eeprom_data) < 0) {
4874         DEBUGOUT("EEPROM Read Error\n");
4875         return -E1000_ERR_EEPROM;
4876     }
4877     if((eeprom_data== ID_LED_RESERVED_0000) ||
4878        (eeprom_data == ID_LED_RESERVED_FFFF)) eeprom_data = ID_LED_DEFAULT;
4879     for(i = 0; i < 4; i++) {
4880         temp = (eeprom_data >> (i << 2)) & led_mask;
4881         switch(temp) {
4882         case ID_LED_ON1_DEF2:
4883         case ID_LED_ON1_ON2:
4884         case ID_LED_ON1_OFF2:
4885             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
4886             hw->ledctl_mode1 |= ledctl_on << (i << 3);
4887             break;
4888         case ID_LED_OFF1_DEF2:
4889         case ID_LED_OFF1_ON2:
4890         case ID_LED_OFF1_OFF2:
4891             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
4892             hw->ledctl_mode1 |= ledctl_off << (i << 3);
4893             break;
4894         default:
4895             /* Do nothing */
4896             break;
4897         }
4898         switch(temp) {
4899         case ID_LED_DEF1_ON2:
4900         case ID_LED_ON1_ON2:
4901         case ID_LED_OFF1_ON2:
4902             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
4903             hw->ledctl_mode2 |= ledctl_on << (i << 3);
4904             break;
4905         case ID_LED_DEF1_OFF2:
4906         case ID_LED_ON1_OFF2:
4907         case ID_LED_OFF1_OFF2:
4908             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
4909             hw->ledctl_mode2 |= ledctl_off << (i << 3);
4910             break;
4911         default:
4912             /* Do nothing */
4913             break;
4914         }
4915     }
4916     return E1000_SUCCESS;
4917 }
4918
4919 /******************************************************************************
4920  * Prepares SW controlable LED for use and saves the current state of the LED.
4921  *
4922  * hw - Struct containing variables accessed by shared code
4923  *****************************************************************************/
4924 int32_t
4925 e1000_setup_led(struct e1000_hw *hw)
4926 {
4927     uint32_t ledctl;
4928     int32_t ret_val = E1000_SUCCESS;
4929
4930     DEBUGFUNC("e1000_setup_led");
4931
4932     switch(hw->mac_type) {
4933     case e1000_82542_rev2_0:
4934     case e1000_82542_rev2_1:
4935     case e1000_82543:
4936     case e1000_82544:
4937         /* No setup necessary */
4938         break;
4939     case e1000_82541:
4940     case e1000_82547:
4941     case e1000_82541_rev_2:
4942     case e1000_82547_rev_2:
4943         /* Turn off PHY Smart Power Down (if enabled) */
4944         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
4945                                      &hw->phy_spd_default);
4946         if(ret_val)
4947             return ret_val;
4948         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
4949                                       (uint16_t)(hw->phy_spd_default &
4950                                       ~IGP01E1000_GMII_SPD));
4951         if(ret_val)
4952             return ret_val;
4953         /* Fall Through */
4954     default:
4955         if(hw->media_type == e1000_media_type_fiber) {
4956             ledctl = E1000_READ_REG(hw, LEDCTL);
4957             /* Save current LEDCTL settings */
4958             hw->ledctl_default = ledctl;
4959             /* Turn off LED0 */
4960             ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
4961                         E1000_LEDCTL_LED0_BLINK |
4962                         E1000_LEDCTL_LED0_MODE_MASK);
4963             ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
4964                        E1000_LEDCTL_LED0_MODE_SHIFT);
4965             E1000_WRITE_REG(hw, LEDCTL, ledctl);
4966         } else if(hw->media_type == e1000_media_type_copper)
4967             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
4968         break;
4969     }
4970
4971     return E1000_SUCCESS;
4972 }
4973
4974 /******************************************************************************
4975  * Restores the saved state of the SW controlable LED.
4976  *
4977  * hw - Struct containing variables accessed by shared code
4978  *****************************************************************************/
4979 int32_t
4980 e1000_cleanup_led(struct e1000_hw *hw)
4981 {
4982     int32_t ret_val = E1000_SUCCESS;
4983
4984     DEBUGFUNC("e1000_cleanup_led");
4985
4986     switch(hw->mac_type) {
4987     case e1000_82542_rev2_0:
4988     case e1000_82542_rev2_1:
4989     case e1000_82543:
4990     case e1000_82544:
4991         /* No cleanup necessary */
4992         break;
4993     case e1000_82541:
4994     case e1000_82547:
4995     case e1000_82541_rev_2:
4996     case e1000_82547_rev_2:
4997         /* Turn on PHY Smart Power Down (if previously enabled) */
4998         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
4999                                       hw->phy_spd_default);
5000         if(ret_val)
5001             return ret_val;
5002         /* Fall Through */
5003     default:
5004         /* Restore LEDCTL settings */
5005         E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_default);
5006         break;
5007     }
5008
5009     return E1000_SUCCESS;
5010 }
5011
5012 /******************************************************************************
5013  * Turns on the software controllable LED
5014  *
5015  * hw - Struct containing variables accessed by shared code
5016  *****************************************************************************/
5017 int32_t
5018 e1000_led_on(struct e1000_hw *hw)
5019 {
5020     uint32_t ctrl = E1000_READ_REG(hw, CTRL);
5021
5022     DEBUGFUNC("e1000_led_on");
5023
5024     switch(hw->mac_type) {
5025     case e1000_82542_rev2_0:
5026     case e1000_82542_rev2_1:
5027     case e1000_82543:
5028         /* Set SW Defineable Pin 0 to turn on the LED */
5029         ctrl |= E1000_CTRL_SWDPIN0;
5030         ctrl |= E1000_CTRL_SWDPIO0;
5031         break;
5032     case e1000_82544:
5033         if(hw->media_type == e1000_media_type_fiber) {
5034             /* Set SW Defineable Pin 0 to turn on the LED */
5035             ctrl |= E1000_CTRL_SWDPIN0;
5036             ctrl |= E1000_CTRL_SWDPIO0;
5037         } else {
5038             /* Clear SW Defineable Pin 0 to turn on the LED */
5039             ctrl &= ~E1000_CTRL_SWDPIN0;
5040             ctrl |= E1000_CTRL_SWDPIO0;
5041         }
5042         break;
5043     default:
5044         if(hw->media_type == e1000_media_type_fiber) {
5045             /* Clear SW Defineable Pin 0 to turn on the LED */
5046             ctrl &= ~E1000_CTRL_SWDPIN0;
5047             ctrl |= E1000_CTRL_SWDPIO0;
5048         } else if(hw->media_type == e1000_media_type_copper) {
5049             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode2);
5050             return E1000_SUCCESS;
5051         }
5052         break;
5053     }
5054
5055     E1000_WRITE_REG(hw, CTRL, ctrl);
5056
5057     return E1000_SUCCESS;
5058 }
5059
5060 /******************************************************************************
5061  * Turns off the software controllable LED
5062  *
5063  * hw - Struct containing variables accessed by shared code
5064  *****************************************************************************/
5065 int32_t
5066 e1000_led_off(struct e1000_hw *hw)
5067 {
5068     uint32_t ctrl = E1000_READ_REG(hw, CTRL);
5069
5070     DEBUGFUNC("e1000_led_off");
5071
5072     switch(hw->mac_type) {
5073     case e1000_82542_rev2_0:
5074     case e1000_82542_rev2_1:
5075     case e1000_82543:
5076         /* Clear SW Defineable Pin 0 to turn off the LED */
5077         ctrl &= ~E1000_CTRL_SWDPIN0;
5078         ctrl |= E1000_CTRL_SWDPIO0;
5079         break;
5080     case e1000_82544:
5081         if(hw->media_type == e1000_media_type_fiber) {
5082             /* Clear SW Defineable Pin 0 to turn off the LED */
5083             ctrl &= ~E1000_CTRL_SWDPIN0;
5084             ctrl |= E1000_CTRL_SWDPIO0;
5085         } else {
5086             /* Set SW Defineable Pin 0 to turn off the LED */
5087             ctrl |= E1000_CTRL_SWDPIN0;
5088             ctrl |= E1000_CTRL_SWDPIO0;
5089         }
5090         break;
5091     default:
5092         if(hw->media_type == e1000_media_type_fiber) {
5093             /* Set SW Defineable Pin 0 to turn off the LED */
5094             ctrl |= E1000_CTRL_SWDPIN0;
5095             ctrl |= E1000_CTRL_SWDPIO0;
5096         } else if(hw->media_type == e1000_media_type_copper) {
5097             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
5098             return E1000_SUCCESS;
5099         }
5100         break;
5101     }
5102
5103     E1000_WRITE_REG(hw, CTRL, ctrl);
5104
5105     return E1000_SUCCESS;
5106 }
5107
5108 /******************************************************************************
5109  * Clears all hardware statistics counters.
5110  *
5111  * hw - Struct containing variables accessed by shared code
5112  *****************************************************************************/
5113 static void
5114 e1000_clear_hw_cntrs(struct e1000_hw *hw)
5115 {
5116     volatile uint32_t temp;
5117
5118     temp = E1000_READ_REG(hw, CRCERRS);
5119     temp = E1000_READ_REG(hw, SYMERRS);
5120     temp = E1000_READ_REG(hw, MPC);
5121     temp = E1000_READ_REG(hw, SCC);
5122     temp = E1000_READ_REG(hw, ECOL);
5123     temp = E1000_READ_REG(hw, MCC);
5124     temp = E1000_READ_REG(hw, LATECOL);
5125     temp = E1000_READ_REG(hw, COLC);
5126     temp = E1000_READ_REG(hw, DC);
5127     temp = E1000_READ_REG(hw, SEC);
5128     temp = E1000_READ_REG(hw, RLEC);
5129     temp = E1000_READ_REG(hw, XONRXC);
5130     temp = E1000_READ_REG(hw, XONTXC);
5131     temp = E1000_READ_REG(hw, XOFFRXC);
5132     temp = E1000_READ_REG(hw, XOFFTXC);
5133     temp = E1000_READ_REG(hw, FCRUC);
5134     temp = E1000_READ_REG(hw, PRC64);
5135     temp = E1000_READ_REG(hw, PRC127);
5136     temp = E1000_READ_REG(hw, PRC255);
5137     temp = E1000_READ_REG(hw, PRC511);
5138     temp = E1000_READ_REG(hw, PRC1023);
5139     temp = E1000_READ_REG(hw, PRC1522);
5140     temp = E1000_READ_REG(hw, GPRC);
5141     temp = E1000_READ_REG(hw, BPRC);
5142     temp = E1000_READ_REG(hw, MPRC);
5143     temp = E1000_READ_REG(hw, GPTC);
5144     temp = E1000_READ_REG(hw, GORCL);
5145     temp = E1000_READ_REG(hw, GORCH);
5146     temp = E1000_READ_REG(hw, GOTCL);
5147     temp = E1000_READ_REG(hw, GOTCH);
5148     temp = E1000_READ_REG(hw, RNBC);
5149     temp = E1000_READ_REG(hw, RUC);
5150     temp = E1000_READ_REG(hw, RFC);
5151     temp = E1000_READ_REG(hw, ROC);
5152     temp = E1000_READ_REG(hw, RJC);
5153     temp = E1000_READ_REG(hw, TORL);
5154     temp = E1000_READ_REG(hw, TORH);
5155     temp = E1000_READ_REG(hw, TOTL);
5156     temp = E1000_READ_REG(hw, TOTH);
5157     temp = E1000_READ_REG(hw, TPR);
5158     temp = E1000_READ_REG(hw, TPT);
5159     temp = E1000_READ_REG(hw, PTC64);
5160     temp = E1000_READ_REG(hw, PTC127);
5161     temp = E1000_READ_REG(hw, PTC255);
5162     temp = E1000_READ_REG(hw, PTC511);
5163     temp = E1000_READ_REG(hw, PTC1023);
5164     temp = E1000_READ_REG(hw, PTC1522);
5165     temp = E1000_READ_REG(hw, MPTC);
5166     temp = E1000_READ_REG(hw, BPTC);
5167
5168     if(hw->mac_type < e1000_82543) return;
5169
5170     temp = E1000_READ_REG(hw, ALGNERRC);
5171     temp = E1000_READ_REG(hw, RXERRC);
5172     temp = E1000_READ_REG(hw, TNCRS);
5173     temp = E1000_READ_REG(hw, CEXTERR);
5174     temp = E1000_READ_REG(hw, TSCTC);
5175     temp = E1000_READ_REG(hw, TSCTFC);
5176
5177     if(hw->mac_type <= e1000_82544) return;
5178
5179     temp = E1000_READ_REG(hw, MGTPRC);
5180     temp = E1000_READ_REG(hw, MGTPDC);
5181     temp = E1000_READ_REG(hw, MGTPTC);
5182
5183     if(hw->mac_type <= e1000_82547_rev_2) return;
5184
5185     temp = E1000_READ_REG(hw, IAC);
5186     temp = E1000_READ_REG(hw, ICRXOC);
5187     temp = E1000_READ_REG(hw, ICRXPTC);
5188     temp = E1000_READ_REG(hw, ICRXATC);
5189     temp = E1000_READ_REG(hw, ICTXPTC);
5190     temp = E1000_READ_REG(hw, ICTXATC);
5191     temp = E1000_READ_REG(hw, ICTXQEC);
5192     temp = E1000_READ_REG(hw, ICTXQMTC);
5193     temp = E1000_READ_REG(hw, ICRXDMTC);
5194 }
5195
5196 /******************************************************************************
5197  * Resets Adaptive IFS to its default state.
5198  *
5199  * hw - Struct containing variables accessed by shared code
5200  *
5201  * Call this after e1000_init_hw. You may override the IFS defaults by setting
5202  * hw->ifs_params_forced to TRUE. However, you must initialize hw->
5203  * current_ifs_val, ifs_min_val, ifs_max_val, ifs_step_size, and ifs_ratio
5204  * before calling this function.
5205  *****************************************************************************/
5206 void
5207 e1000_reset_adaptive(struct e1000_hw *hw)
5208 {
5209     DEBUGFUNC("e1000_reset_adaptive");
5210
5211     if(hw->adaptive_ifs) {
5212         if(!hw->ifs_params_forced) {
5213             hw->current_ifs_val = 0;
5214             hw->ifs_min_val = IFS_MIN;
5215             hw->ifs_max_val = IFS_MAX;
5216             hw->ifs_step_size = IFS_STEP;
5217             hw->ifs_ratio = IFS_RATIO;
5218         }
5219         hw->in_ifs_mode = FALSE;
5220         E1000_WRITE_REG(hw, AIT, 0);
5221     } else {
5222         DEBUGOUT("Not in Adaptive IFS mode!\n");
5223     }
5224 }
5225
5226 /******************************************************************************
5227  * Called during the callback/watchdog routine to update IFS value based on
5228  * the ratio of transmits to collisions.
5229  *
5230  * hw - Struct containing variables accessed by shared code
5231  * tx_packets - Number of transmits since last callback
5232  * total_collisions - Number of collisions since last callback
5233  *****************************************************************************/
5234 void
5235 e1000_update_adaptive(struct e1000_hw *hw)
5236 {
5237     DEBUGFUNC("e1000_update_adaptive");
5238
5239     if(hw->adaptive_ifs) {
5240         if((hw->collision_delta * hw->ifs_ratio) > hw->tx_packet_delta) {
5241             if(hw->tx_packet_delta > MIN_NUM_XMITS) {
5242                 hw->in_ifs_mode = TRUE;
5243                 if(hw->current_ifs_val < hw->ifs_max_val) {
5244                     if(hw->current_ifs_val == 0)
5245                         hw->current_ifs_val = hw->ifs_min_val;
5246                     else
5247                         hw->current_ifs_val += hw->ifs_step_size;
5248                     E1000_WRITE_REG(hw, AIT, hw->current_ifs_val);
5249                 }
5250             }
5251         } else {
5252             if(hw->in_ifs_mode && (hw->tx_packet_delta <= MIN_NUM_XMITS)) {
5253                 hw->current_ifs_val = 0;
5254                 hw->in_ifs_mode = FALSE;
5255                 E1000_WRITE_REG(hw, AIT, 0);
5256             }
5257         }
5258     } else {
5259         DEBUGOUT("Not in Adaptive IFS mode!\n");
5260     }
5261 }
5262
5263 /******************************************************************************
5264  * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
5265  *
5266  * hw - Struct containing variables accessed by shared code
5267  * frame_len - The length of the frame in question
5268  * mac_addr - The Ethernet destination address of the frame in question
5269  *****************************************************************************/
5270 void
5271 e1000_tbi_adjust_stats(struct e1000_hw *hw,
5272                        struct e1000_hw_stats *stats,
5273                        uint32_t frame_len,
5274                        uint8_t *mac_addr)
5275 {
5276     uint64_t carry_bit;
5277
5278     /* First adjust the frame length. */
5279     frame_len--;
5280     /* We need to adjust the statistics counters, since the hardware
5281      * counters overcount this packet as a CRC error and undercount
5282      * the packet as a good packet
5283      */
5284     /* This packet should not be counted as a CRC error.    */
5285     stats->crcerrs--;
5286     /* This packet does count as a Good Packet Received.    */
5287     stats->gprc++;
5288
5289     /* Adjust the Good Octets received counters             */
5290     carry_bit = 0x80000000 & stats->gorcl;
5291     stats->gorcl += frame_len;
5292     /* If the high bit of Gorcl (the low 32 bits of the Good Octets
5293      * Received Count) was one before the addition,
5294      * AND it is zero after, then we lost the carry out,
5295      * need to add one to Gorch (Good Octets Received Count High).
5296      * This could be simplified if all environments supported
5297      * 64-bit integers.
5298      */
5299     if(carry_bit && ((stats->gorcl & 0x80000000) == 0))
5300         stats->gorch++;
5301     /* Is this a broadcast or multicast?  Check broadcast first,
5302      * since the test for a multicast frame will test positive on
5303      * a broadcast frame.
5304      */
5305     if((mac_addr[0] == (uint8_t) 0xff) && (mac_addr[1] == (uint8_t) 0xff))
5306         /* Broadcast packet */
5307         stats->bprc++;
5308     else if(*mac_addr & 0x01)
5309         /* Multicast packet */
5310         stats->mprc++;
5311
5312     if(frame_len == hw->max_frame_size) {
5313         /* In this case, the hardware has overcounted the number of
5314          * oversize frames.
5315          */
5316         if(stats->roc > 0)
5317             stats->roc--;
5318     }
5319
5320     /* Adjust the bin counters when the extra byte put the frame in the
5321      * wrong bin. Remember that the frame_len was adjusted above.
5322      */
5323     if(frame_len == 64) {
5324         stats->prc64++;
5325         stats->prc127--;
5326     } else if(frame_len == 127) {
5327         stats->prc127++;
5328         stats->prc255--;
5329     } else if(frame_len == 255) {
5330         stats->prc255++;
5331         stats->prc511--;
5332     } else if(frame_len == 511) {
5333         stats->prc511++;
5334         stats->prc1023--;
5335     } else if(frame_len == 1023) {
5336         stats->prc1023++;
5337         stats->prc1522--;
5338     } else if(frame_len == 1522) {
5339         stats->prc1522++;
5340     }
5341 }
5342
5343 /******************************************************************************
5344  * Gets the current PCI bus type, speed, and width of the hardware
5345  *
5346  * hw - Struct containing variables accessed by shared code
5347  *****************************************************************************/
5348 void
5349 e1000_get_bus_info(struct e1000_hw *hw)
5350 {
5351     uint32_t status;
5352
5353     switch (hw->mac_type) {
5354     case e1000_82542_rev2_0:
5355     case e1000_82542_rev2_1:
5356         hw->bus_type = e1000_bus_type_unknown;
5357         hw->bus_speed = e1000_bus_speed_unknown;
5358         hw->bus_width = e1000_bus_width_unknown;
5359         break;
5360     case e1000_82572:
5361     case e1000_82573:
5362         hw->bus_type = e1000_bus_type_pci_express;
5363         hw->bus_speed = e1000_bus_speed_2500;
5364         hw->bus_width = e1000_bus_width_pciex_1;
5365         break;
5366     case e1000_82571:
5367         hw->bus_type = e1000_bus_type_pci_express;
5368         hw->bus_speed = e1000_bus_speed_2500;
5369         hw->bus_width = e1000_bus_width_pciex_4;
5370         break;
5371     default:
5372         status = E1000_READ_REG(hw, STATUS);
5373         hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
5374                        e1000_bus_type_pcix : e1000_bus_type_pci;
5375
5376         if(hw->device_id == E1000_DEV_ID_82546EB_QUAD_COPPER) {
5377             hw->bus_speed = (hw->bus_type == e1000_bus_type_pci) ?
5378                             e1000_bus_speed_66 : e1000_bus_speed_120;
5379         } else if(hw->bus_type == e1000_bus_type_pci) {
5380             hw->bus_speed = (status & E1000_STATUS_PCI66) ?
5381                             e1000_bus_speed_66 : e1000_bus_speed_33;
5382         } else {
5383             switch (status & E1000_STATUS_PCIX_SPEED) {
5384             case E1000_STATUS_PCIX_SPEED_66:
5385                 hw->bus_speed = e1000_bus_speed_66;
5386                 break;
5387             case E1000_STATUS_PCIX_SPEED_100:
5388                 hw->bus_speed = e1000_bus_speed_100;
5389                 break;
5390             case E1000_STATUS_PCIX_SPEED_133:
5391                 hw->bus_speed = e1000_bus_speed_133;
5392                 break;
5393             default:
5394                 hw->bus_speed = e1000_bus_speed_reserved;
5395                 break;
5396             }
5397         }
5398         hw->bus_width = (status & E1000_STATUS_BUS64) ?
5399                         e1000_bus_width_64 : e1000_bus_width_32;
5400         break;
5401     }
5402 }
5403
5404 #if 0
5405 /******************************************************************************
5406  * Reads a value from one of the devices registers using port I/O (as opposed
5407  * memory mapped I/O). Only 82544 and newer devices support port I/O.
5408  *
5409  * hw - Struct containing variables accessed by shared code
5410  * offset - offset to read from
5411  *****************************************************************************/
5412 uint32_t
5413 e1000_read_reg_io(struct e1000_hw *hw,
5414                   uint32_t offset)
5415 {
5416     unsigned long io_addr = hw->io_base;
5417     unsigned long io_data = hw->io_base + 4;
5418
5419     e1000_io_write(hw, io_addr, offset);
5420     return e1000_io_read(hw, io_data);
5421 }
5422 #endif  /*  0  */
5423
5424 /******************************************************************************
5425  * Writes a value to one of the devices registers using port I/O (as opposed to
5426  * memory mapped I/O). Only 82544 and newer devices support port I/O.
5427  *
5428  * hw - Struct containing variables accessed by shared code
5429  * offset - offset to write to
5430  * value - value to write
5431  *****************************************************************************/
5432 static void
5433 e1000_write_reg_io(struct e1000_hw *hw,
5434                    uint32_t offset,
5435                    uint32_t value)
5436 {
5437     unsigned long io_addr = hw->io_base;
5438     unsigned long io_data = hw->io_base + 4;
5439
5440     e1000_io_write(hw, io_addr, offset);
5441     e1000_io_write(hw, io_data, value);
5442 }
5443
5444
5445 /******************************************************************************
5446  * Estimates the cable length.
5447  *
5448  * hw - Struct containing variables accessed by shared code
5449  * min_length - The estimated minimum length
5450  * max_length - The estimated maximum length
5451  *
5452  * returns: - E1000_ERR_XXX
5453  *            E1000_SUCCESS
5454  *
5455  * This function always returns a ranged length (minimum & maximum).
5456  * So for M88 phy's, this function interprets the one value returned from the
5457  * register to the minimum and maximum range.
5458  * For IGP phy's, the function calculates the range by the AGC registers.
5459  *****************************************************************************/
5460 static int32_t
5461 e1000_get_cable_length(struct e1000_hw *hw,
5462                        uint16_t *min_length,
5463                        uint16_t *max_length)
5464 {
5465     int32_t ret_val;
5466     uint16_t agc_value = 0;
5467     uint16_t cur_agc, min_agc = IGP01E1000_AGC_LENGTH_TABLE_SIZE;
5468     uint16_t max_agc = 0;
5469     uint16_t i, phy_data;
5470     uint16_t cable_length;
5471
5472     DEBUGFUNC("e1000_get_cable_length");
5473
5474     *min_length = *max_length = 0;
5475
5476     /* Use old method for Phy older than IGP */
5477     if(hw->phy_type == e1000_phy_m88) {
5478
5479         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
5480                                      &phy_data);
5481         if(ret_val)
5482             return ret_val;
5483         cable_length = (phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
5484                        M88E1000_PSSR_CABLE_LENGTH_SHIFT;
5485
5486         /* Convert the enum value to ranged values */
5487         switch (cable_length) {
5488         case e1000_cable_length_50:
5489             *min_length = 0;
5490             *max_length = e1000_igp_cable_length_50;
5491             break;
5492         case e1000_cable_length_50_80:
5493             *min_length = e1000_igp_cable_length_50;
5494             *max_length = e1000_igp_cable_length_80;
5495             break;
5496         case e1000_cable_length_80_110:
5497             *min_length = e1000_igp_cable_length_80;
5498             *max_length = e1000_igp_cable_length_110;
5499             break;
5500         case e1000_cable_length_110_140:
5501             *min_length = e1000_igp_cable_length_110;
5502             *max_length = e1000_igp_cable_length_140;
5503             break;
5504         case e1000_cable_length_140:
5505             *min_length = e1000_igp_cable_length_140;
5506             *max_length = e1000_igp_cable_length_170;
5507             break;
5508         default:
5509             return -E1000_ERR_PHY;
5510             break;
5511         }
5512     } else if(hw->phy_type == e1000_phy_igp) { /* For IGP PHY */
5513         uint16_t agc_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
5514                                                          {IGP01E1000_PHY_AGC_A,
5515                                                           IGP01E1000_PHY_AGC_B,
5516                                                           IGP01E1000_PHY_AGC_C,
5517                                                           IGP01E1000_PHY_AGC_D};
5518         /* Read the AGC registers for all channels */
5519         for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
5520
5521             ret_val = e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
5522             if(ret_val)
5523                 return ret_val;
5524
5525             cur_agc = phy_data >> IGP01E1000_AGC_LENGTH_SHIFT;
5526
5527             /* Array bound check. */
5528             if((cur_agc >= IGP01E1000_AGC_LENGTH_TABLE_SIZE - 1) ||
5529                (cur_agc == 0))
5530                 return -E1000_ERR_PHY;
5531
5532             agc_value += cur_agc;
5533
5534             /* Update minimal AGC value. */
5535             if(min_agc > cur_agc)
5536                 min_agc = cur_agc;
5537         }
5538
5539         /* Remove the minimal AGC result for length < 50m */
5540         if(agc_value < IGP01E1000_PHY_CHANNEL_NUM * e1000_igp_cable_length_50) {
5541             agc_value -= min_agc;
5542
5543             /* Get the average length of the remaining 3 channels */
5544             agc_value /= (IGP01E1000_PHY_CHANNEL_NUM - 1);
5545         } else {
5546             /* Get the average length of all the 4 channels. */
5547             agc_value /= IGP01E1000_PHY_CHANNEL_NUM;
5548         }
5549
5550         /* Set the range of the calculated length. */
5551         *min_length = ((e1000_igp_cable_length_table[agc_value] -
5552                        IGP01E1000_AGC_RANGE) > 0) ?
5553                        (e1000_igp_cable_length_table[agc_value] -
5554                        IGP01E1000_AGC_RANGE) : 0;
5555         *max_length = e1000_igp_cable_length_table[agc_value] +
5556                       IGP01E1000_AGC_RANGE;
5557     } else if (hw->phy_type == e1000_phy_igp_2) {
5558         uint16_t agc_reg_array[IGP02E1000_PHY_CHANNEL_NUM] =
5559                                                          {IGP02E1000_PHY_AGC_A,
5560                                                           IGP02E1000_PHY_AGC_B,
5561                                                           IGP02E1000_PHY_AGC_C,
5562                                                           IGP02E1000_PHY_AGC_D};
5563         /* Read the AGC registers for all channels */
5564         for (i = 0; i < IGP02E1000_PHY_CHANNEL_NUM; i++) {
5565             ret_val = e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
5566             if (ret_val)
5567                 return ret_val;
5568
5569             /* Getting bits 15:9, which represent the combination of course and
5570              * fine gain values.  The result is a number that can be put into
5571              * the lookup table to obtain the approximate cable length. */
5572             cur_agc = (phy_data >> IGP02E1000_AGC_LENGTH_SHIFT) &
5573                       IGP02E1000_AGC_LENGTH_MASK;
5574
5575             /* Remove min & max AGC values from calculation. */
5576             if (e1000_igp_2_cable_length_table[min_agc] > e1000_igp_2_cable_length_table[cur_agc])
5577                 min_agc = cur_agc;
5578             if (e1000_igp_2_cable_length_table[max_agc] < e1000_igp_2_cable_length_table[cur_agc])
5579                 max_agc = cur_agc;
5580
5581             agc_value += e1000_igp_2_cable_length_table[cur_agc];
5582         }
5583
5584         agc_value -= (e1000_igp_2_cable_length_table[min_agc] + e1000_igp_2_cable_length_table[max_agc]);
5585         agc_value /= (IGP02E1000_PHY_CHANNEL_NUM - 2);
5586
5587         /* Calculate cable length with the error range of +/- 10 meters. */
5588         *min_length = ((agc_value - IGP02E1000_AGC_RANGE) > 0) ?
5589                        (agc_value - IGP02E1000_AGC_RANGE) : 0;
5590         *max_length = agc_value + IGP02E1000_AGC_RANGE;
5591     }
5592
5593     return E1000_SUCCESS;
5594 }
5595
5596 /******************************************************************************
5597  * Check the cable polarity
5598  *
5599  * hw - Struct containing variables accessed by shared code
5600  * polarity - output parameter : 0 - Polarity is not reversed
5601  *                               1 - Polarity is reversed.
5602  *
5603  * returns: - E1000_ERR_XXX
5604  *            E1000_SUCCESS
5605  *
5606  * For phy's older then IGP, this function simply reads the polarity bit in the
5607  * Phy Status register.  For IGP phy's, this bit is valid only if link speed is
5608  * 10 Mbps.  If the link speed is 100 Mbps there is no polarity so this bit will
5609  * return 0.  If the link speed is 1000 Mbps the polarity status is in the
5610  * IGP01E1000_PHY_PCS_INIT_REG.
5611  *****************************************************************************/
5612 static int32_t
5613 e1000_check_polarity(struct e1000_hw *hw,
5614                      uint16_t *polarity)
5615 {
5616     int32_t ret_val;
5617     uint16_t phy_data;
5618
5619     DEBUGFUNC("e1000_check_polarity");
5620
5621     if(hw->phy_type == e1000_phy_m88) {
5622         /* return the Polarity bit in the Status register. */
5623         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
5624                                      &phy_data);
5625         if(ret_val)
5626             return ret_val;
5627         *polarity = (phy_data & M88E1000_PSSR_REV_POLARITY) >>
5628                     M88E1000_PSSR_REV_POLARITY_SHIFT;
5629     } else if(hw->phy_type == e1000_phy_igp ||
5630               hw->phy_type == e1000_phy_igp_2) {
5631         /* Read the Status register to check the speed */
5632         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS,
5633                                      &phy_data);
5634         if(ret_val)
5635             return ret_val;
5636
5637         /* If speed is 1000 Mbps, must read the IGP01E1000_PHY_PCS_INIT_REG to
5638          * find the polarity status */
5639         if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
5640            IGP01E1000_PSSR_SPEED_1000MBPS) {
5641
5642             /* Read the GIG initialization PCS register (0x00B4) */
5643             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG,
5644                                          &phy_data);
5645             if(ret_val)
5646                 return ret_val;
5647
5648             /* Check the polarity bits */
5649             *polarity = (phy_data & IGP01E1000_PHY_POLARITY_MASK) ? 1 : 0;
5650         } else {
5651             /* For 10 Mbps, read the polarity bit in the status register. (for
5652              * 100 Mbps this bit is always 0) */
5653             *polarity = phy_data & IGP01E1000_PSSR_POLARITY_REVERSED;
5654         }
5655     }
5656     return E1000_SUCCESS;
5657 }
5658
5659 /******************************************************************************
5660  * Check if Downshift occured
5661  *
5662  * hw - Struct containing variables accessed by shared code
5663  * downshift - output parameter : 0 - No Downshift ocured.
5664  *                                1 - Downshift ocured.
5665  *
5666  * returns: - E1000_ERR_XXX
5667  *            E1000_SUCCESS 
5668  *
5669  * For phy's older then IGP, this function reads the Downshift bit in the Phy
5670  * Specific Status register.  For IGP phy's, it reads the Downgrade bit in the
5671  * Link Health register.  In IGP this bit is latched high, so the driver must
5672  * read it immediately after link is established.
5673  *****************************************************************************/
5674 static int32_t
5675 e1000_check_downshift(struct e1000_hw *hw)
5676 {
5677     int32_t ret_val;
5678     uint16_t phy_data;
5679
5680     DEBUGFUNC("e1000_check_downshift");
5681
5682     if(hw->phy_type == e1000_phy_igp || 
5683         hw->phy_type == e1000_phy_igp_2) {
5684         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_LINK_HEALTH,
5685                                      &phy_data);
5686         if(ret_val)
5687             return ret_val;
5688
5689         hw->speed_downgraded = (phy_data & IGP01E1000_PLHR_SS_DOWNGRADE) ? 1 : 0;
5690     } else if(hw->phy_type == e1000_phy_m88) {
5691         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
5692                                      &phy_data);
5693         if(ret_val)
5694             return ret_val;
5695
5696         hw->speed_downgraded = (phy_data & M88E1000_PSSR_DOWNSHIFT) >>
5697                                M88E1000_PSSR_DOWNSHIFT_SHIFT;
5698     }
5699
5700     return E1000_SUCCESS;
5701 }
5702
5703 /*****************************************************************************
5704  *
5705  * 82541_rev_2 & 82547_rev_2 have the capability to configure the DSP when a
5706  * gigabit link is achieved to improve link quality.
5707  *
5708  * hw: Struct containing variables accessed by shared code
5709  *
5710  * returns: - E1000_ERR_PHY if fail to read/write the PHY
5711  *            E1000_SUCCESS at any other case.
5712  *
5713  ****************************************************************************/
5714
5715 static int32_t
5716 e1000_config_dsp_after_link_change(struct e1000_hw *hw,
5717                                    boolean_t link_up)
5718 {
5719     int32_t ret_val;
5720     uint16_t phy_data, phy_saved_data, speed, duplex, i;
5721     uint16_t dsp_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
5722                                         {IGP01E1000_PHY_AGC_PARAM_A,
5723                                         IGP01E1000_PHY_AGC_PARAM_B,
5724                                         IGP01E1000_PHY_AGC_PARAM_C,
5725                                         IGP01E1000_PHY_AGC_PARAM_D};
5726     uint16_t min_length, max_length;
5727
5728     DEBUGFUNC("e1000_config_dsp_after_link_change");
5729
5730     if(hw->phy_type != e1000_phy_igp)
5731         return E1000_SUCCESS;
5732
5733     if(link_up) {
5734         ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
5735         if(ret_val) {
5736             DEBUGOUT("Error getting link speed and duplex\n");
5737             return ret_val;
5738         }
5739
5740         if(speed == SPEED_1000) {
5741
5742             e1000_get_cable_length(hw, &min_length, &max_length);
5743
5744             if((hw->dsp_config_state == e1000_dsp_config_enabled) &&
5745                 min_length >= e1000_igp_cable_length_50) {
5746
5747                 for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
5748                     ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i],
5749                                                  &phy_data);
5750                     if(ret_val)
5751                         return ret_val;
5752
5753                     phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
5754
5755                     ret_val = e1000_write_phy_reg(hw, dsp_reg_array[i],
5756                                                   phy_data);
5757                     if(ret_val)
5758                         return ret_val;
5759                 }
5760                 hw->dsp_config_state = e1000_dsp_config_activated;
5761             }
5762
5763             if((hw->ffe_config_state == e1000_ffe_config_enabled) &&
5764                (min_length < e1000_igp_cable_length_50)) {
5765
5766                 uint16_t ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_20;
5767                 uint32_t idle_errs = 0;
5768
5769                 /* clear previous idle error counts */
5770                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
5771                                              &phy_data);
5772                 if(ret_val)
5773                     return ret_val;
5774
5775                 for(i = 0; i < ffe_idle_err_timeout; i++) {
5776                     udelay(1000);
5777                     ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
5778                                                  &phy_data);
5779                     if(ret_val)
5780                         return ret_val;
5781
5782                     idle_errs += (phy_data & SR_1000T_IDLE_ERROR_CNT);
5783                     if(idle_errs > SR_1000T_PHY_EXCESSIVE_IDLE_ERR_COUNT) {
5784                         hw->ffe_config_state = e1000_ffe_config_active;
5785
5786                         ret_val = e1000_write_phy_reg(hw,
5787                                     IGP01E1000_PHY_DSP_FFE,
5788                                     IGP01E1000_PHY_DSP_FFE_CM_CP);
5789                         if(ret_val)
5790                             return ret_val;
5791                         break;
5792                     }
5793
5794                     if(idle_errs)
5795                         ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_100;
5796                 }
5797             }
5798         }
5799     } else {
5800         if(hw->dsp_config_state == e1000_dsp_config_activated) {
5801             /* Save off the current value of register 0x2F5B to be restored at
5802              * the end of the routines. */
5803             ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
5804
5805             if(ret_val)
5806                 return ret_val;
5807
5808             /* Disable the PHY transmitter */
5809             ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
5810
5811             if(ret_val)
5812                 return ret_val;
5813
5814             msec_delay_irq(20);
5815
5816             ret_val = e1000_write_phy_reg(hw, 0x0000,
5817                                           IGP01E1000_IEEE_FORCE_GIGA);
5818             if(ret_val)
5819                 return ret_val;
5820             for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
5821                 ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i], &phy_data);
5822                 if(ret_val)
5823                     return ret_val;
5824
5825                 phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
5826                 phy_data |=  IGP01E1000_PHY_EDAC_SIGN_EXT_9_BITS;
5827
5828                 ret_val = e1000_write_phy_reg(hw,dsp_reg_array[i], phy_data);
5829                 if(ret_val)
5830                     return ret_val;
5831             }
5832
5833             ret_val = e1000_write_phy_reg(hw, 0x0000,
5834                                           IGP01E1000_IEEE_RESTART_AUTONEG);
5835             if(ret_val)
5836                 return ret_val;
5837
5838             msec_delay_irq(20);
5839
5840             /* Now enable the transmitter */
5841             ret_val = e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
5842
5843             if(ret_val)
5844                 return ret_val;
5845
5846             hw->dsp_config_state = e1000_dsp_config_enabled;
5847         }
5848
5849         if(hw->ffe_config_state == e1000_ffe_config_active) {
5850             /* Save off the current value of register 0x2F5B to be restored at
5851              * the end of the routines. */
5852             ret_val = e1000_read_phy_reg(hw, 0x2F5B, &phy_saved_data);
5853
5854             if(ret_val)
5855                 return ret_val;
5856
5857             /* Disable the PHY transmitter */
5858             ret_val = e1000_write_phy_reg(hw, 0x2F5B, 0x0003);
5859
5860             if(ret_val)
5861                 return ret_val;
5862
5863             msec_delay_irq(20);
5864
5865             ret_val = e1000_write_phy_reg(hw, 0x0000,
5866                                           IGP01E1000_IEEE_FORCE_GIGA);
5867             if(ret_val)
5868                 return ret_val;
5869             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_DSP_FFE,
5870                                           IGP01E1000_PHY_DSP_FFE_DEFAULT);
5871             if(ret_val)
5872                 return ret_val;
5873
5874             ret_val = e1000_write_phy_reg(hw, 0x0000,
5875                                           IGP01E1000_IEEE_RESTART_AUTONEG);
5876             if(ret_val)
5877                 return ret_val;
5878
5879             msec_delay_irq(20);
5880
5881             /* Now enable the transmitter */
5882             ret_val = e1000_write_phy_reg(hw, 0x2F5B, phy_saved_data);
5883
5884             if(ret_val)
5885                 return ret_val;
5886
5887             hw->ffe_config_state = e1000_ffe_config_enabled;
5888         }
5889     }
5890     return E1000_SUCCESS;
5891 }
5892
5893 /*****************************************************************************
5894  * Set PHY to class A mode
5895  * Assumes the following operations will follow to enable the new class mode.
5896  *  1. Do a PHY soft reset
5897  *  2. Restart auto-negotiation or force link.
5898  *
5899  * hw - Struct containing variables accessed by shared code
5900  ****************************************************************************/
5901 static int32_t
5902 e1000_set_phy_mode(struct e1000_hw *hw)
5903 {
5904     int32_t ret_val;
5905     uint16_t eeprom_data;
5906
5907     DEBUGFUNC("e1000_set_phy_mode");
5908
5909     if((hw->mac_type == e1000_82545_rev_3) &&
5910        (hw->media_type == e1000_media_type_copper)) {
5911         ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD, 1, &eeprom_data);
5912         if(ret_val) {
5913             return ret_val;
5914         }
5915
5916         if((eeprom_data != EEPROM_RESERVED_WORD) &&
5917            (eeprom_data & EEPROM_PHY_CLASS_A)) {
5918             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x000B);
5919             if(ret_val)
5920                 return ret_val;
5921             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x8104);
5922             if(ret_val)
5923                 return ret_val;
5924
5925             hw->phy_reset_disable = FALSE;
5926         }
5927     }
5928
5929     return E1000_SUCCESS;
5930 }
5931
5932 /*****************************************************************************
5933  *
5934  * This function sets the lplu state according to the active flag.  When
5935  * activating lplu this function also disables smart speed and vise versa.
5936  * lplu will not be activated unless the device autonegotiation advertisment
5937  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
5938  * hw: Struct containing variables accessed by shared code
5939  * active - true to enable lplu false to disable lplu.
5940  *
5941  * returns: - E1000_ERR_PHY if fail to read/write the PHY
5942  *            E1000_SUCCESS at any other case.
5943  *
5944  ****************************************************************************/
5945
5946 static int32_t
5947 e1000_set_d3_lplu_state(struct e1000_hw *hw,
5948                         boolean_t active)
5949 {
5950     int32_t ret_val;
5951     uint16_t phy_data;
5952     DEBUGFUNC("e1000_set_d3_lplu_state");
5953
5954     if(hw->phy_type != e1000_phy_igp && hw->phy_type != e1000_phy_igp_2)
5955         return E1000_SUCCESS;
5956
5957     /* During driver activity LPLU should not be used or it will attain link
5958      * from the lowest speeds starting from 10Mbps. The capability is used for
5959      * Dx transitions and states */
5960     if(hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2) {
5961         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO, &phy_data);
5962         if(ret_val)
5963             return ret_val;
5964     } else {
5965         ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
5966         if(ret_val)
5967             return ret_val;
5968     }
5969
5970     if(!active) {
5971         if(hw->mac_type == e1000_82541_rev_2 ||
5972            hw->mac_type == e1000_82547_rev_2) {
5973             phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
5974             ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
5975             if(ret_val)
5976                 return ret_val;
5977         } else {
5978                 phy_data &= ~IGP02E1000_PM_D3_LPLU;
5979                 ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
5980                                               phy_data);
5981                 if (ret_val)
5982                     return ret_val;
5983         }
5984
5985         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
5986          * Dx states where the power conservation is most important.  During
5987          * driver activity we should enable SmartSpeed, so performance is
5988          * maintained. */
5989         if (hw->smart_speed == e1000_smart_speed_on) {
5990             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5991                                          &phy_data);
5992             if(ret_val)
5993                 return ret_val;
5994
5995             phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
5996             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
5997                                           phy_data);
5998             if(ret_val)
5999                 return ret_val;
6000         } else if (hw->smart_speed == e1000_smart_speed_off) {
6001             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6002                                          &phy_data);
6003             if (ret_val)
6004                 return ret_val;
6005
6006             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
6007             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6008                                           phy_data);
6009             if(ret_val)
6010                 return ret_val;
6011         }
6012
6013     } else if((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT) ||
6014               (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL ) ||
6015               (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
6016
6017         if(hw->mac_type == e1000_82541_rev_2 ||
6018            hw->mac_type == e1000_82547_rev_2) {
6019             phy_data |= IGP01E1000_GMII_FLEX_SPD;
6020             ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
6021             if(ret_val)
6022                 return ret_val;
6023         } else {
6024                 phy_data |= IGP02E1000_PM_D3_LPLU;
6025                 ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT,
6026                                               phy_data);
6027                 if (ret_val)
6028                     return ret_val;
6029         }
6030
6031         /* When LPLU is enabled we should disable SmartSpeed */
6032         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
6033         if(ret_val)
6034             return ret_val;
6035
6036         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
6037         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
6038         if(ret_val)
6039             return ret_val;
6040
6041     }
6042     return E1000_SUCCESS;
6043 }
6044
6045 /*****************************************************************************
6046  *
6047  * This function sets the lplu d0 state according to the active flag.  When
6048  * activating lplu this function also disables smart speed and vise versa.
6049  * lplu will not be activated unless the device autonegotiation advertisment
6050  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
6051  * hw: Struct containing variables accessed by shared code
6052  * active - true to enable lplu false to disable lplu.
6053  *
6054  * returns: - E1000_ERR_PHY if fail to read/write the PHY
6055  *            E1000_SUCCESS at any other case.
6056  *
6057  ****************************************************************************/
6058
6059 static int32_t
6060 e1000_set_d0_lplu_state(struct e1000_hw *hw,
6061                         boolean_t active)
6062 {
6063     int32_t ret_val;
6064     uint16_t phy_data;
6065     DEBUGFUNC("e1000_set_d0_lplu_state");
6066
6067     if(hw->mac_type <= e1000_82547_rev_2)
6068         return E1000_SUCCESS;
6069
6070         ret_val = e1000_read_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, &phy_data);
6071         if(ret_val)
6072             return ret_val;
6073
6074     if (!active) {
6075             phy_data &= ~IGP02E1000_PM_D0_LPLU;
6076             ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
6077             if (ret_val)
6078                 return ret_val;
6079
6080         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
6081          * Dx states where the power conservation is most important.  During
6082          * driver activity we should enable SmartSpeed, so performance is
6083          * maintained. */
6084         if (hw->smart_speed == e1000_smart_speed_on) {
6085             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6086                                          &phy_data);
6087             if(ret_val)
6088                 return ret_val;
6089
6090             phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
6091             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6092                                           phy_data);
6093             if(ret_val)
6094                 return ret_val;
6095         } else if (hw->smart_speed == e1000_smart_speed_off) {
6096             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6097                                          &phy_data);
6098             if (ret_val)
6099                 return ret_val;
6100
6101             phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
6102             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
6103                                           phy_data);
6104             if(ret_val)
6105                 return ret_val;
6106         }
6107
6108
6109     } else {
6110  
6111             phy_data |= IGP02E1000_PM_D0_LPLU;   
6112             ret_val = e1000_write_phy_reg(hw, IGP02E1000_PHY_POWER_MGMT, phy_data);
6113             if (ret_val)
6114                 return ret_val;
6115
6116         /* When LPLU is enabled we should disable SmartSpeed */
6117         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
6118         if(ret_val)
6119             return ret_val;
6120
6121         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
6122         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
6123         if(ret_val)
6124             return ret_val;
6125
6126     }
6127     return E1000_SUCCESS;
6128 }
6129
6130 /******************************************************************************
6131  * Change VCO speed register to improve Bit Error Rate performance of SERDES.
6132  *
6133  * hw - Struct containing variables accessed by shared code
6134  *****************************************************************************/
6135 static int32_t
6136 e1000_set_vco_speed(struct e1000_hw *hw)
6137 {
6138     int32_t  ret_val;
6139     uint16_t default_page = 0;
6140     uint16_t phy_data;
6141
6142     DEBUGFUNC("e1000_set_vco_speed");
6143
6144     switch(hw->mac_type) {
6145     case e1000_82545_rev_3:
6146     case e1000_82546_rev_3:
6147        break;
6148     default:
6149         return E1000_SUCCESS;
6150     }
6151
6152     /* Set PHY register 30, page 5, bit 8 to 0 */
6153
6154     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, &default_page);
6155     if(ret_val)
6156         return ret_val;
6157
6158     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0005);
6159     if(ret_val)
6160         return ret_val;
6161
6162     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
6163     if(ret_val)
6164         return ret_val;
6165
6166     phy_data &= ~M88E1000_PHY_VCO_REG_BIT8;
6167     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
6168     if(ret_val)
6169         return ret_val;
6170
6171     /* Set PHY register 30, page 4, bit 11 to 1 */
6172
6173     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0004);
6174     if(ret_val)
6175         return ret_val;
6176
6177     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
6178     if(ret_val)
6179         return ret_val;
6180
6181     phy_data |= M88E1000_PHY_VCO_REG_BIT11;
6182     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
6183     if(ret_val)
6184         return ret_val;
6185
6186     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, default_page);
6187     if(ret_val)
6188         return ret_val;
6189
6190     return E1000_SUCCESS;
6191 }
6192
6193
6194 /*****************************************************************************
6195  * This function reads the cookie from ARC ram.
6196  *
6197  * returns: - E1000_SUCCESS .
6198  ****************************************************************************/
6199 int32_t
6200 e1000_host_if_read_cookie(struct e1000_hw * hw, uint8_t *buffer)
6201 {
6202     uint8_t i;
6203     uint32_t offset = E1000_MNG_DHCP_COOKIE_OFFSET; 
6204     uint8_t length = E1000_MNG_DHCP_COOKIE_LENGTH;
6205
6206     length = (length >> 2);
6207     offset = (offset >> 2);
6208
6209     for (i = 0; i < length; i++) {
6210         *((uint32_t *) buffer + i) =
6211             E1000_READ_REG_ARRAY_DWORD(hw, HOST_IF, offset + i);
6212     }
6213     return E1000_SUCCESS;
6214 }
6215
6216
6217 /*****************************************************************************
6218  * This function checks whether the HOST IF is enabled for command operaton
6219  * and also checks whether the previous command is completed.
6220  * It busy waits in case of previous command is not completed.
6221  *
6222  * returns: - E1000_ERR_HOST_INTERFACE_COMMAND in case if is not ready or 
6223  *            timeout
6224  *          - E1000_SUCCESS for success.
6225  ****************************************************************************/
6226 static int32_t
6227 e1000_mng_enable_host_if(struct e1000_hw * hw)
6228 {
6229     uint32_t hicr;
6230     uint8_t i;
6231
6232     /* Check that the host interface is enabled. */
6233     hicr = E1000_READ_REG(hw, HICR);
6234     if ((hicr & E1000_HICR_EN) == 0) {
6235         DEBUGOUT("E1000_HOST_EN bit disabled.\n");
6236         return -E1000_ERR_HOST_INTERFACE_COMMAND;
6237     }
6238     /* check the previous command is completed */
6239     for (i = 0; i < E1000_MNG_DHCP_COMMAND_TIMEOUT; i++) {
6240         hicr = E1000_READ_REG(hw, HICR);
6241         if (!(hicr & E1000_HICR_C))
6242             break;
6243         msec_delay_irq(1);
6244     }
6245
6246     if (i == E1000_MNG_DHCP_COMMAND_TIMEOUT) { 
6247         DEBUGOUT("Previous command timeout failed .\n");
6248         return -E1000_ERR_HOST_INTERFACE_COMMAND;
6249     }
6250     return E1000_SUCCESS;
6251 }
6252
6253 /*****************************************************************************
6254  * This function writes the buffer content at the offset given on the host if.
6255  * It also does alignment considerations to do the writes in most efficient way.
6256  * Also fills up the sum of the buffer in *buffer parameter.
6257  *
6258  * returns  - E1000_SUCCESS for success.
6259  ****************************************************************************/
6260 static int32_t
6261 e1000_mng_host_if_write(struct e1000_hw * hw, uint8_t *buffer,
6262                         uint16_t length, uint16_t offset, uint8_t *sum)
6263 {
6264     uint8_t *tmp;
6265     uint8_t *bufptr = buffer;
6266     uint32_t data;
6267     uint16_t remaining, i, j, prev_bytes;
6268
6269     /* sum = only sum of the data and it is not checksum */
6270
6271     if (length == 0 || offset + length > E1000_HI_MAX_MNG_DATA_LENGTH) {
6272         return -E1000_ERR_PARAM;
6273     }
6274
6275     tmp = (uint8_t *)&data;
6276     prev_bytes = offset & 0x3;
6277     offset &= 0xFFFC;
6278     offset >>= 2;
6279
6280     if (prev_bytes) {
6281         data = E1000_READ_REG_ARRAY_DWORD(hw, HOST_IF, offset);
6282         for (j = prev_bytes; j < sizeof(uint32_t); j++) {
6283             *(tmp + j) = *bufptr++;
6284             *sum += *(tmp + j);
6285         }
6286         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset, data);
6287         length -= j - prev_bytes;
6288         offset++;
6289     }
6290
6291     remaining = length & 0x3;
6292     length -= remaining;
6293
6294     /* Calculate length in DWORDs */
6295     length >>= 2;
6296
6297     /* The device driver writes the relevant command block into the
6298      * ram area. */
6299     for (i = 0; i < length; i++) {
6300         for (j = 0; j < sizeof(uint32_t); j++) {
6301             *(tmp + j) = *bufptr++;
6302             *sum += *(tmp + j);
6303         }
6304
6305         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset + i, data);
6306     }
6307     if (remaining) {
6308         for (j = 0; j < sizeof(uint32_t); j++) {
6309             if (j < remaining)
6310                 *(tmp + j) = *bufptr++;
6311             else
6312                 *(tmp + j) = 0;
6313
6314             *sum += *(tmp + j);
6315         }
6316         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, offset + i, data);
6317     }
6318
6319     return E1000_SUCCESS;
6320 }
6321
6322
6323 /*****************************************************************************
6324  * This function writes the command header after does the checksum calculation.
6325  *
6326  * returns  - E1000_SUCCESS for success.
6327  ****************************************************************************/
6328 static int32_t
6329 e1000_mng_write_cmd_header(struct e1000_hw * hw,
6330                            struct e1000_host_mng_command_header * hdr)
6331 {
6332     uint16_t i;
6333     uint8_t sum;
6334     uint8_t *buffer;
6335
6336     /* Write the whole command header structure which includes sum of
6337      * the buffer */
6338
6339     uint16_t length = sizeof(struct e1000_host_mng_command_header);
6340
6341     sum = hdr->checksum;
6342     hdr->checksum = 0;
6343
6344     buffer = (uint8_t *) hdr;
6345     i = length;
6346     while(i--)
6347         sum += buffer[i];
6348
6349     hdr->checksum = 0 - sum;
6350
6351     length >>= 2;
6352     /* The device driver writes the relevant command block into the ram area. */
6353     for (i = 0; i < length; i++)
6354         E1000_WRITE_REG_ARRAY_DWORD(hw, HOST_IF, i, *((uint32_t *) hdr + i));
6355
6356     return E1000_SUCCESS;
6357 }
6358
6359
6360 /*****************************************************************************
6361  * This function indicates to ARC that a new command is pending which completes
6362  * one write operation by the driver.
6363  *
6364  * returns  - E1000_SUCCESS for success.
6365  ****************************************************************************/
6366 static int32_t
6367 e1000_mng_write_commit(
6368     struct e1000_hw * hw)
6369 {
6370     uint32_t hicr;
6371
6372     hicr = E1000_READ_REG(hw, HICR);
6373     /* Setting this bit tells the ARC that a new command is pending. */
6374     E1000_WRITE_REG(hw, HICR, hicr | E1000_HICR_C);
6375
6376     return E1000_SUCCESS;
6377 }
6378
6379
6380 /*****************************************************************************
6381  * This function checks the mode of the firmware.
6382  *
6383  * returns  - TRUE when the mode is IAMT or FALSE.
6384  ****************************************************************************/
6385 boolean_t
6386 e1000_check_mng_mode(
6387     struct e1000_hw *hw)
6388 {
6389     uint32_t fwsm;
6390
6391     fwsm = E1000_READ_REG(hw, FWSM);
6392
6393     if((fwsm & E1000_FWSM_MODE_MASK) ==
6394         (E1000_MNG_IAMT_MODE << E1000_FWSM_MODE_SHIFT))
6395         return TRUE;
6396
6397     return FALSE;
6398 }
6399
6400
6401 /*****************************************************************************
6402  * This function writes the dhcp info .
6403  ****************************************************************************/
6404 int32_t
6405 e1000_mng_write_dhcp_info(struct e1000_hw * hw, uint8_t *buffer,
6406                           uint16_t length)
6407 {
6408     int32_t ret_val;
6409     struct e1000_host_mng_command_header hdr;
6410
6411     hdr.command_id = E1000_MNG_DHCP_TX_PAYLOAD_CMD;
6412     hdr.command_length = length;
6413     hdr.reserved1 = 0;
6414     hdr.reserved2 = 0;
6415     hdr.checksum = 0;
6416
6417     ret_val = e1000_mng_enable_host_if(hw);
6418     if (ret_val == E1000_SUCCESS) {
6419         ret_val = e1000_mng_host_if_write(hw, buffer, length, sizeof(hdr),
6420                                           &(hdr.checksum));
6421         if (ret_val == E1000_SUCCESS) {
6422             ret_val = e1000_mng_write_cmd_header(hw, &hdr);
6423             if (ret_val == E1000_SUCCESS)
6424                 ret_val = e1000_mng_write_commit(hw);
6425         }
6426     }
6427     return ret_val;
6428 }
6429
6430
6431 /*****************************************************************************
6432  * This function calculates the checksum.
6433  *
6434  * returns  - checksum of buffer contents.
6435  ****************************************************************************/
6436 uint8_t
6437 e1000_calculate_mng_checksum(char *buffer, uint32_t length)
6438 {
6439     uint8_t sum = 0;
6440     uint32_t i;
6441
6442     if (!buffer)
6443         return 0;
6444
6445     for (i=0; i < length; i++)
6446         sum += buffer[i];
6447
6448     return (uint8_t) (0 - sum);
6449 }
6450
6451 /*****************************************************************************
6452  * This function checks whether tx pkt filtering needs to be enabled or not.
6453  *
6454  * returns  - TRUE for packet filtering or FALSE.
6455  ****************************************************************************/
6456 boolean_t
6457 e1000_enable_tx_pkt_filtering(struct e1000_hw *hw)
6458 {
6459     /* called in init as well as watchdog timer functions */
6460
6461     int32_t ret_val, checksum;
6462     boolean_t tx_filter = FALSE;
6463     struct e1000_host_mng_dhcp_cookie *hdr = &(hw->mng_cookie);
6464     uint8_t *buffer = (uint8_t *) &(hw->mng_cookie);
6465
6466     if (e1000_check_mng_mode(hw)) {
6467         ret_val = e1000_mng_enable_host_if(hw);
6468         if (ret_val == E1000_SUCCESS) {
6469             ret_val = e1000_host_if_read_cookie(hw, buffer);
6470             if (ret_val == E1000_SUCCESS) {
6471                 checksum = hdr->checksum;
6472                 hdr->checksum = 0;
6473                 if ((hdr->signature == E1000_IAMT_SIGNATURE) &&
6474                     checksum == e1000_calculate_mng_checksum((char *)buffer,
6475                                                E1000_MNG_DHCP_COOKIE_LENGTH)) {
6476                     if (hdr->status &
6477                         E1000_MNG_DHCP_COOKIE_STATUS_PARSING_SUPPORT)
6478                         tx_filter = TRUE;
6479                 } else
6480                     tx_filter = TRUE;
6481             } else
6482                 tx_filter = TRUE;
6483         }
6484     }
6485
6486     hw->tx_pkt_filtering = tx_filter;
6487     return tx_filter;
6488 }
6489
6490 /******************************************************************************
6491  * Verifies the hardware needs to allow ARPs to be processed by the host
6492  *
6493  * hw - Struct containing variables accessed by shared code
6494  *
6495  * returns: - TRUE/FALSE
6496  *
6497  *****************************************************************************/
6498 uint32_t
6499 e1000_enable_mng_pass_thru(struct e1000_hw *hw)
6500 {
6501     uint32_t manc;
6502     uint32_t fwsm, factps;
6503
6504     if (hw->asf_firmware_present) {
6505         manc = E1000_READ_REG(hw, MANC);
6506
6507         if (!(manc & E1000_MANC_RCV_TCO_EN) ||
6508             !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
6509             return FALSE;
6510         if (e1000_arc_subsystem_valid(hw) == TRUE) {
6511             fwsm = E1000_READ_REG(hw, FWSM);
6512             factps = E1000_READ_REG(hw, FACTPS);
6513
6514             if (((fwsm & E1000_FWSM_MODE_MASK) ==
6515                 (e1000_mng_mode_pt << E1000_FWSM_MODE_SHIFT)) &&
6516                 (factps & E1000_FACTPS_MNGCG))
6517                 return TRUE;
6518         } else
6519             if ((manc & E1000_MANC_SMBUS_EN) && !(manc & E1000_MANC_ASF_EN))
6520                 return TRUE;
6521     }
6522     return FALSE;
6523 }
6524
6525 static int32_t
6526 e1000_polarity_reversal_workaround(struct e1000_hw *hw)
6527 {
6528     int32_t ret_val;
6529     uint16_t mii_status_reg;
6530     uint16_t i;
6531
6532     /* Polarity reversal workaround for forced 10F/10H links. */
6533
6534     /* Disable the transmitter on the PHY */
6535
6536     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
6537     if(ret_val)
6538         return ret_val;
6539     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFFF);
6540     if(ret_val)
6541         return ret_val;
6542
6543     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
6544     if(ret_val)
6545         return ret_val;
6546
6547     /* This loop will early-out if the NO link condition has been met. */
6548     for(i = PHY_FORCE_TIME; i > 0; i--) {
6549         /* Read the MII Status Register and wait for Link Status bit
6550          * to be clear.
6551          */
6552
6553         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
6554         if(ret_val)
6555             return ret_val;
6556
6557         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
6558         if(ret_val)
6559             return ret_val;
6560
6561         if((mii_status_reg & ~MII_SR_LINK_STATUS) == 0) break;
6562         msec_delay_irq(100);
6563     }
6564
6565     /* Recommended delay time after link has been lost */
6566     msec_delay_irq(1000);
6567
6568     /* Now we will re-enable th transmitter on the PHY */
6569
6570     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0019);
6571     if(ret_val)
6572         return ret_val;
6573     msec_delay_irq(50);
6574     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFFF0);
6575     if(ret_val)
6576         return ret_val;
6577     msec_delay_irq(50);
6578     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0xFF00);
6579     if(ret_val)
6580         return ret_val;
6581     msec_delay_irq(50);
6582     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x0000);
6583     if(ret_val)
6584         return ret_val;
6585
6586     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0000);
6587     if(ret_val)
6588         return ret_val;
6589
6590     /* This loop will early-out if the link condition has been met. */
6591     for(i = PHY_FORCE_TIME; i > 0; i--) {
6592         /* Read the MII Status Register and wait for Link Status bit
6593          * to be set.
6594          */
6595
6596         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
6597         if(ret_val)
6598             return ret_val;
6599
6600         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
6601         if(ret_val)
6602             return ret_val;
6603
6604         if(mii_status_reg & MII_SR_LINK_STATUS) break;
6605         msec_delay_irq(100);
6606     }
6607     return E1000_SUCCESS;
6608 }
6609
6610 /***************************************************************************
6611  *
6612  * Disables PCI-Express master access.
6613  *
6614  * hw: Struct containing variables accessed by shared code
6615  *
6616  * returns: - none.
6617  *
6618  ***************************************************************************/
6619 static void
6620 e1000_set_pci_express_master_disable(struct e1000_hw *hw)
6621 {
6622     uint32_t ctrl;
6623
6624     DEBUGFUNC("e1000_set_pci_express_master_disable");
6625
6626     if (hw->bus_type != e1000_bus_type_pci_express)
6627         return;
6628
6629     ctrl = E1000_READ_REG(hw, CTRL);
6630     ctrl |= E1000_CTRL_GIO_MASTER_DISABLE;
6631     E1000_WRITE_REG(hw, CTRL, ctrl);
6632 }
6633
6634 #if 0
6635 /***************************************************************************
6636  *
6637  * Enables PCI-Express master access.
6638  *
6639  * hw: Struct containing variables accessed by shared code
6640  *
6641  * returns: - none.
6642  *
6643  ***************************************************************************/
6644 void
6645 e1000_enable_pciex_master(struct e1000_hw *hw)
6646 {
6647     uint32_t ctrl;
6648
6649     DEBUGFUNC("e1000_enable_pciex_master");
6650
6651     if (hw->bus_type != e1000_bus_type_pci_express)
6652         return;
6653
6654     ctrl = E1000_READ_REG(hw, CTRL);
6655     ctrl &= ~E1000_CTRL_GIO_MASTER_DISABLE;
6656     E1000_WRITE_REG(hw, CTRL, ctrl);
6657 }
6658 #endif  /*  0  */
6659
6660 /*******************************************************************************
6661  *
6662  * Disables PCI-Express master access and verifies there are no pending requests
6663  *
6664  * hw: Struct containing variables accessed by shared code
6665  *
6666  * returns: - E1000_ERR_MASTER_REQUESTS_PENDING if master disable bit hasn't
6667  *            caused the master requests to be disabled.
6668  *            E1000_SUCCESS master requests disabled.
6669  *
6670  ******************************************************************************/
6671 int32_t
6672 e1000_disable_pciex_master(struct e1000_hw *hw)
6673 {
6674     int32_t timeout = MASTER_DISABLE_TIMEOUT;   /* 80ms */
6675
6676     DEBUGFUNC("e1000_disable_pciex_master");
6677
6678     if (hw->bus_type != e1000_bus_type_pci_express)
6679         return E1000_SUCCESS;
6680
6681     e1000_set_pci_express_master_disable(hw);
6682
6683     while(timeout) {
6684         if(!(E1000_READ_REG(hw, STATUS) & E1000_STATUS_GIO_MASTER_ENABLE))
6685             break;
6686         else
6687             udelay(100);
6688         timeout--;
6689     }
6690
6691     if(!timeout) {
6692         DEBUGOUT("Master requests are pending.\n");
6693         return -E1000_ERR_MASTER_REQUESTS_PENDING;
6694     }
6695
6696     return E1000_SUCCESS;
6697 }
6698
6699 /*******************************************************************************
6700  *
6701  * Check for EEPROM Auto Read bit done.
6702  *
6703  * hw: Struct containing variables accessed by shared code
6704  *
6705  * returns: - E1000_ERR_RESET if fail to reset MAC
6706  *            E1000_SUCCESS at any other case.
6707  *
6708  ******************************************************************************/
6709 static int32_t
6710 e1000_get_auto_rd_done(struct e1000_hw *hw)
6711 {
6712     int32_t timeout = AUTO_READ_DONE_TIMEOUT;
6713
6714     DEBUGFUNC("e1000_get_auto_rd_done");
6715
6716     switch (hw->mac_type) {
6717     default:
6718         msec_delay(5);
6719         break;
6720     case e1000_82571:
6721     case e1000_82572:
6722     case e1000_82573:
6723         while(timeout) {
6724             if (E1000_READ_REG(hw, EECD) & E1000_EECD_AUTO_RD) break;
6725             else msec_delay(1);
6726             timeout--;
6727         }
6728
6729         if(!timeout) {
6730             DEBUGOUT("Auto read by HW from EEPROM has not completed.\n");
6731             return -E1000_ERR_RESET;
6732         }
6733         break;
6734     }
6735
6736     /* PHY configuration from NVM just starts after EECD_AUTO_RD sets to high.
6737      * Need to wait for PHY configuration completion before accessing NVM
6738      * and PHY. */
6739     if (hw->mac_type == e1000_82573)
6740         msec_delay(25);
6741
6742     return E1000_SUCCESS;
6743 }
6744
6745 /***************************************************************************
6746  * Checks if the PHY configuration is done
6747  *
6748  * hw: Struct containing variables accessed by shared code
6749  *
6750  * returns: - E1000_ERR_RESET if fail to reset MAC
6751  *            E1000_SUCCESS at any other case.
6752  *
6753  ***************************************************************************/
6754 static int32_t
6755 e1000_get_phy_cfg_done(struct e1000_hw *hw)
6756 {
6757     int32_t timeout = PHY_CFG_TIMEOUT;
6758     uint32_t cfg_mask = E1000_EEPROM_CFG_DONE;
6759
6760     DEBUGFUNC("e1000_get_phy_cfg_done");
6761
6762     switch (hw->mac_type) {
6763     default:
6764         msec_delay(10);
6765         break;
6766     case e1000_82571:
6767     case e1000_82572:
6768         while (timeout) {
6769             if (E1000_READ_REG(hw, EEMNGCTL) & cfg_mask)
6770                 break;
6771             else
6772                 msec_delay(1);
6773             timeout--;
6774         }
6775
6776         if (!timeout) {
6777             DEBUGOUT("MNG configuration cycle has not completed.\n");
6778             return -E1000_ERR_RESET;
6779         }
6780         break;
6781     }
6782
6783     return E1000_SUCCESS;
6784 }
6785
6786 /***************************************************************************
6787  *
6788  * Using the combination of SMBI and SWESMBI semaphore bits when resetting
6789  * adapter or Eeprom access.
6790  *
6791  * hw: Struct containing variables accessed by shared code
6792  *
6793  * returns: - E1000_ERR_EEPROM if fail to access EEPROM.
6794  *            E1000_SUCCESS at any other case.
6795  *
6796  ***************************************************************************/
6797 static int32_t
6798 e1000_get_hw_eeprom_semaphore(struct e1000_hw *hw)
6799 {
6800     int32_t timeout;
6801     uint32_t swsm;
6802
6803     DEBUGFUNC("e1000_get_hw_eeprom_semaphore");
6804
6805     if(!hw->eeprom_semaphore_present)
6806         return E1000_SUCCESS;
6807
6808
6809     /* Get the FW semaphore. */
6810     timeout = hw->eeprom.word_size + 1;
6811     while(timeout) {
6812         swsm = E1000_READ_REG(hw, SWSM);
6813         swsm |= E1000_SWSM_SWESMBI;
6814         E1000_WRITE_REG(hw, SWSM, swsm);
6815         /* if we managed to set the bit we got the semaphore. */
6816         swsm = E1000_READ_REG(hw, SWSM);
6817         if(swsm & E1000_SWSM_SWESMBI)
6818             break;
6819
6820         udelay(50);
6821         timeout--;
6822     }
6823
6824     if(!timeout) {
6825         /* Release semaphores */
6826         e1000_put_hw_eeprom_semaphore(hw);
6827         DEBUGOUT("Driver can't access the Eeprom - SWESMBI bit is set.\n");
6828         return -E1000_ERR_EEPROM;
6829     }
6830
6831     return E1000_SUCCESS;
6832 }
6833
6834 /***************************************************************************
6835  * This function clears HW semaphore bits.
6836  *
6837  * hw: Struct containing variables accessed by shared code
6838  *
6839  * returns: - None.
6840  *
6841  ***************************************************************************/
6842 static void
6843 e1000_put_hw_eeprom_semaphore(struct e1000_hw *hw)
6844 {
6845     uint32_t swsm;
6846
6847     DEBUGFUNC("e1000_put_hw_eeprom_semaphore");
6848
6849     if(!hw->eeprom_semaphore_present)
6850         return;
6851
6852     swsm = E1000_READ_REG(hw, SWSM);
6853         swsm &= ~(E1000_SWSM_SWESMBI);
6854     E1000_WRITE_REG(hw, SWSM, swsm);
6855 }
6856
6857 /******************************************************************************
6858  * Checks if PHY reset is blocked due to SOL/IDER session, for example.
6859  * Returning E1000_BLK_PHY_RESET isn't necessarily an error.  But it's up to
6860  * the caller to figure out how to deal with it.
6861  *
6862  * hw - Struct containing variables accessed by shared code
6863  *
6864  * returns: - E1000_BLK_PHY_RESET
6865  *            E1000_SUCCESS
6866  *
6867  *****************************************************************************/
6868 int32_t
6869 e1000_check_phy_reset_block(struct e1000_hw *hw)
6870 {
6871     uint32_t manc = 0;
6872
6873     if (hw->mac_type > e1000_82547_rev_2)
6874         manc = E1000_READ_REG(hw, MANC);
6875     return (manc & E1000_MANC_BLK_PHY_RST_ON_IDE) ?
6876             E1000_BLK_PHY_RESET : E1000_SUCCESS;
6877 }
6878
6879 static uint8_t
6880 e1000_arc_subsystem_valid(struct e1000_hw *hw)
6881 {
6882     uint32_t fwsm;
6883
6884     /* On 8257x silicon, registers in the range of 0x8800 - 0x8FFC
6885      * may not be provided a DMA clock when no manageability features are
6886      * enabled.  We do not want to perform any reads/writes to these registers
6887      * if this is the case.  We read FWSM to determine the manageability mode.
6888      */
6889     switch (hw->mac_type) {
6890     case e1000_82571:
6891     case e1000_82572:
6892     case e1000_82573:
6893         fwsm = E1000_READ_REG(hw, FWSM);
6894         if((fwsm & E1000_FWSM_MODE_MASK) != 0)
6895             return TRUE;
6896         break;
6897     default:
6898         break;
6899     }
6900     return FALSE;
6901 }
6902
6903
6904