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