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