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