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