vserver 1.9.3
[linux-2.6.git] / drivers / net / e1000 / e1000_hw.c
1 /*******************************************************************************
2
3   
4   Copyright(c) 1999 - 2004 Intel Corporation. All rights reserved.
5   
6   This program is free software; you can redistribute it and/or modify it 
7   under the terms of the GNU General Public License as published by the Free 
8   Software Foundation; either version 2 of the License, or (at your option) 
9   any later version.
10   
11   This program is distributed in the hope that it will be useful, but WITHOUT 
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for 
14   more details.
15   
16   You should have received a copy of the GNU General Public License along with
17   this program; if not, write to the Free Software Foundation, Inc., 59 
18   Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19   
20   The full GNU General Public License is included in this distribution in the
21   file called LICENSE.
22   
23   Contact Information:
24   Linux NICS <linux.nics@intel.com>
25   Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
26
27 *******************************************************************************/
28
29 /* e1000_hw.c
30  * Shared functions for accessing and configuring the MAC
31  */
32
33 #include "e1000_hw.h"
34
35 static int32_t e1000_set_phy_type(struct e1000_hw *hw);
36 static void e1000_phy_init_script(struct e1000_hw *hw);
37 static int32_t e1000_setup_copper_link(struct e1000_hw *hw);
38 static int32_t e1000_setup_fiber_serdes_link(struct e1000_hw *hw);
39 static int32_t e1000_adjust_serdes_amplitude(struct e1000_hw *hw);
40 static int32_t e1000_phy_force_speed_duplex(struct e1000_hw *hw);
41 static int32_t e1000_config_mac_to_phy(struct e1000_hw *hw);
42 static void e1000_raise_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
43 static void e1000_lower_mdi_clk(struct e1000_hw *hw, uint32_t *ctrl);
44 static void e1000_shift_out_mdi_bits(struct e1000_hw *hw, uint32_t data,
45                                      uint16_t count);
46 static uint16_t e1000_shift_in_mdi_bits(struct e1000_hw *hw);
47 static int32_t e1000_phy_reset_dsp(struct e1000_hw *hw);
48 static int32_t e1000_write_eeprom_spi(struct e1000_hw *hw, uint16_t offset,
49                                       uint16_t words, uint16_t *data);
50 static int32_t e1000_write_eeprom_microwire(struct e1000_hw *hw,
51                                             uint16_t offset, uint16_t words,
52                                             uint16_t *data);
53 static int32_t e1000_spi_eeprom_ready(struct e1000_hw *hw);
54 static void e1000_raise_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
55 static void e1000_lower_ee_clk(struct e1000_hw *hw, uint32_t *eecd);
56 static void e1000_shift_out_ee_bits(struct e1000_hw *hw, uint16_t data,
57                                     uint16_t count);
58 static int32_t e1000_write_phy_reg_ex(struct e1000_hw *hw, uint32_t reg_addr,
59                                       uint16_t phy_data);
60 static int32_t e1000_read_phy_reg_ex(struct e1000_hw *hw,uint32_t reg_addr,
61                                      uint16_t *phy_data);
62 static uint16_t e1000_shift_in_ee_bits(struct e1000_hw *hw, uint16_t count);
63 static int32_t e1000_acquire_eeprom(struct e1000_hw *hw);
64 static void e1000_release_eeprom(struct e1000_hw *hw);
65 static void e1000_standby_eeprom(struct e1000_hw *hw);
66 static int32_t e1000_id_led_init(struct e1000_hw * hw);
67 static int32_t e1000_set_vco_speed(struct e1000_hw *hw);
68 static int32_t e1000_set_phy_mode(struct e1000_hw *hw);
69
70 /* IGP cable length table */
71 static const
72 uint16_t e1000_igp_cable_length_table[IGP01E1000_AGC_LENGTH_TABLE_SIZE] =
73     { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
74       5, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 25, 25, 25,
75       25, 25, 25, 25, 30, 30, 30, 30, 40, 40, 40, 40, 40, 40, 40, 40,
76       40, 50, 50, 50, 50, 50, 50, 50, 60, 60, 60, 60, 60, 60, 60, 60,
77       60, 70, 70, 70, 70, 70, 70, 80, 80, 80, 80, 80, 80, 90, 90, 90,
78       90, 90, 90, 90, 90, 90, 100, 100, 100, 100, 100, 100, 100, 100, 100, 100,
79       100, 100, 100, 100, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110, 110,
80       110, 110, 110, 110, 110, 110, 120, 120, 120, 120, 120, 120, 120, 120, 120, 120};
81
82
83 /******************************************************************************
84  * Set the phy type member in the hw struct.
85  *
86  * hw - Struct containing variables accessed by shared code
87  *****************************************************************************/
88 int32_t
89 e1000_set_phy_type(struct e1000_hw *hw)
90 {
91     DEBUGFUNC("e1000_set_phy_type");
92
93     switch(hw->phy_id) {
94     case M88E1000_E_PHY_ID:
95     case M88E1000_I_PHY_ID:
96     case M88E1011_I_PHY_ID:
97         hw->phy_type = e1000_phy_m88;
98         break;
99     case IGP01E1000_I_PHY_ID:
100         if(hw->mac_type == e1000_82541 ||
101            hw->mac_type == e1000_82541_rev_2 ||
102            hw->mac_type == e1000_82547 ||
103            hw->mac_type == e1000_82547_rev_2) {
104             hw->phy_type = e1000_phy_igp;
105             break;
106         }
107         /* Fall Through */
108     default:
109         /* Should never have loaded on this device */
110         hw->phy_type = e1000_phy_undefined;
111         return -E1000_ERR_PHY_TYPE;
112     }
113
114     return E1000_SUCCESS;
115 }
116
117 /******************************************************************************
118  * IGP phy init script - initializes the GbE PHY
119  *
120  * hw - Struct containing variables accessed by shared code
121  *****************************************************************************/
122 static void
123 e1000_phy_init_script(struct e1000_hw *hw)
124 {
125     DEBUGFUNC("e1000_phy_init_script");
126
127     if(hw->phy_init_script) {
128         msec_delay(20);
129
130         e1000_write_phy_reg(hw,0x0000,0x0140);
131
132         msec_delay(5);
133
134         if(hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547) {
135             e1000_write_phy_reg(hw, 0x1F95, 0x0001);
136
137             e1000_write_phy_reg(hw, 0x1F71, 0xBD21);
138
139             e1000_write_phy_reg(hw, 0x1F79, 0x0018);
140
141             e1000_write_phy_reg(hw, 0x1F30, 0x1600);
142
143             e1000_write_phy_reg(hw, 0x1F31, 0x0014);
144
145             e1000_write_phy_reg(hw, 0x1F32, 0x161C);
146
147             e1000_write_phy_reg(hw, 0x1F94, 0x0003);
148
149             e1000_write_phy_reg(hw, 0x1F96, 0x003F);
150
151             e1000_write_phy_reg(hw, 0x2010, 0x0008);
152         } else {
153             e1000_write_phy_reg(hw, 0x1F73, 0x0099);
154         }
155
156         e1000_write_phy_reg(hw, 0x0000, 0x3300);
157
158         if(hw->mac_type == e1000_82547) {
159             uint16_t fused, fine, coarse;
160
161             /* Move to analog registers page */
162             e1000_read_phy_reg(hw, IGP01E1000_ANALOG_SPARE_FUSE_STATUS, &fused);
163
164             if(!(fused & IGP01E1000_ANALOG_SPARE_FUSE_ENABLED)) {
165                 e1000_read_phy_reg(hw, IGP01E1000_ANALOG_FUSE_STATUS, &fused);
166
167                 fine = fused & IGP01E1000_ANALOG_FUSE_FINE_MASK;
168                 coarse = fused & IGP01E1000_ANALOG_FUSE_COARSE_MASK;
169
170                 if(coarse > IGP01E1000_ANALOG_FUSE_COARSE_THRESH) {
171                     coarse -= IGP01E1000_ANALOG_FUSE_COARSE_10;
172                     fine -= IGP01E1000_ANALOG_FUSE_FINE_1;
173                 } else if(coarse == IGP01E1000_ANALOG_FUSE_COARSE_THRESH)
174                     fine -= IGP01E1000_ANALOG_FUSE_FINE_10;
175
176                 fused = (fused & IGP01E1000_ANALOG_FUSE_POLY_MASK) |
177                         (fine & IGP01E1000_ANALOG_FUSE_FINE_MASK) |
178                         (coarse & IGP01E1000_ANALOG_FUSE_COARSE_MASK);
179
180                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_CONTROL, fused);
181                 e1000_write_phy_reg(hw, IGP01E1000_ANALOG_FUSE_BYPASS,
182                                     IGP01E1000_ANALOG_FUSE_ENABLE_SW_CONTROL);
183             }
184         }
185     }
186 }
187
188 /******************************************************************************
189  * Set the mac type member in the hw struct.
190  *
191  * hw - Struct containing variables accessed by shared code
192  *****************************************************************************/
193 int32_t
194 e1000_set_mac_type(struct e1000_hw *hw)
195 {
196     DEBUGFUNC("e1000_set_mac_type");
197
198     switch (hw->device_id) {
199     case E1000_DEV_ID_82542:
200         switch (hw->revision_id) {
201         case E1000_82542_2_0_REV_ID:
202             hw->mac_type = e1000_82542_rev2_0;
203             break;
204         case E1000_82542_2_1_REV_ID:
205             hw->mac_type = e1000_82542_rev2_1;
206             break;
207         default:
208             /* Invalid 82542 revision ID */
209             return -E1000_ERR_MAC_TYPE;
210         }
211         break;
212     case E1000_DEV_ID_82543GC_FIBER:
213     case E1000_DEV_ID_82543GC_COPPER:
214         hw->mac_type = e1000_82543;
215         break;
216     case E1000_DEV_ID_82544EI_COPPER:
217     case E1000_DEV_ID_82544EI_FIBER:
218     case E1000_DEV_ID_82544GC_COPPER:
219     case E1000_DEV_ID_82544GC_LOM:
220         hw->mac_type = e1000_82544;
221         break;
222     case E1000_DEV_ID_82540EM:
223     case E1000_DEV_ID_82540EM_LOM:
224     case E1000_DEV_ID_82540EP:
225     case E1000_DEV_ID_82540EP_LOM:
226     case E1000_DEV_ID_82540EP_LP:
227         hw->mac_type = e1000_82540;
228         break;
229     case E1000_DEV_ID_82545EM_COPPER:
230     case E1000_DEV_ID_82545EM_FIBER:
231         hw->mac_type = e1000_82545;
232         break;
233     case E1000_DEV_ID_82545GM_COPPER:
234     case E1000_DEV_ID_82545GM_FIBER:
235     case E1000_DEV_ID_82545GM_SERDES:
236         hw->mac_type = e1000_82545_rev_3;
237         break;
238     case E1000_DEV_ID_82546EB_COPPER:
239     case E1000_DEV_ID_82546EB_FIBER:
240     case E1000_DEV_ID_82546EB_QUAD_COPPER:
241         hw->mac_type = e1000_82546;
242         break;
243     case E1000_DEV_ID_82546GB_COPPER:
244     case E1000_DEV_ID_82546GB_FIBER:
245     case E1000_DEV_ID_82546GB_SERDES:
246         hw->mac_type = e1000_82546_rev_3;
247         break;
248     case E1000_DEV_ID_82541EI:
249     case E1000_DEV_ID_82541EI_MOBILE:
250         hw->mac_type = e1000_82541;
251         break;
252     case E1000_DEV_ID_82541ER:
253     case E1000_DEV_ID_82541GI:
254     case E1000_DEV_ID_82541GI_LF:
255     case E1000_DEV_ID_82541GI_MOBILE:
256         hw->mac_type = e1000_82541_rev_2;
257         break;
258     case E1000_DEV_ID_82547EI:
259         hw->mac_type = e1000_82547;
260         break;
261     case E1000_DEV_ID_82547GI:
262         hw->mac_type = e1000_82547_rev_2;
263         break;
264     default:
265         /* Should never have loaded on this device */
266         return -E1000_ERR_MAC_TYPE;
267     }
268
269     switch(hw->mac_type) {
270     case e1000_82541:
271     case e1000_82547:
272     case e1000_82541_rev_2:
273     case e1000_82547_rev_2:
274         hw->asf_firmware_present = TRUE;
275         break;
276     default:
277         break;
278     }
279
280     return E1000_SUCCESS;
281 }
282
283 /*****************************************************************************
284  * Set media type and TBI compatibility.
285  *
286  * hw - Struct containing variables accessed by shared code
287  * **************************************************************************/
288 void
289 e1000_set_media_type(struct e1000_hw *hw)
290 {
291     uint32_t status;
292
293     DEBUGFUNC("e1000_set_media_type");
294
295     if(hw->mac_type != e1000_82543) {
296         /* tbi_compatibility is only valid on 82543 */
297         hw->tbi_compatibility_en = FALSE;
298     }
299
300     switch (hw->device_id) {
301     case E1000_DEV_ID_82545GM_SERDES:
302     case E1000_DEV_ID_82546GB_SERDES:
303         hw->media_type = e1000_media_type_internal_serdes;
304         break;
305     default:
306         if(hw->mac_type >= e1000_82543) {
307             status = E1000_READ_REG(hw, STATUS);
308             if(status & E1000_STATUS_TBIMODE) {
309                 hw->media_type = e1000_media_type_fiber;
310                 /* tbi_compatibility not valid on fiber */
311                 hw->tbi_compatibility_en = FALSE;
312             } else {
313                 hw->media_type = e1000_media_type_copper;
314             }
315         } else {
316             /* This is an 82542 (fiber only) */
317             hw->media_type = e1000_media_type_fiber;
318         }
319     }
320 }
321
322 /******************************************************************************
323  * Reset the transmit and receive units; mask and clear all interrupts.
324  *
325  * hw - Struct containing variables accessed by shared code
326  *****************************************************************************/
327 int32_t
328 e1000_reset_hw(struct e1000_hw *hw)
329 {
330     uint32_t ctrl;
331     uint32_t ctrl_ext;
332     uint32_t icr;
333     uint32_t manc;
334     uint32_t led_ctrl;
335
336     DEBUGFUNC("e1000_reset_hw");
337
338     /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
339     if(hw->mac_type == e1000_82542_rev2_0) {
340         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
341         e1000_pci_clear_mwi(hw);
342     }
343
344     /* Clear interrupt mask to stop board from generating interrupts */
345     DEBUGOUT("Masking off all interrupts\n");
346     E1000_WRITE_REG(hw, IMC, 0xffffffff);
347
348     /* Disable the Transmit and Receive units.  Then delay to allow
349      * any pending transactions to complete before we hit the MAC with
350      * the global reset.
351      */
352     E1000_WRITE_REG(hw, RCTL, 0);
353     E1000_WRITE_REG(hw, TCTL, E1000_TCTL_PSP);
354     E1000_WRITE_FLUSH(hw);
355
356     /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
357     hw->tbi_compatibility_on = FALSE;
358
359     /* Delay to allow any outstanding PCI transactions to complete before
360      * resetting the device
361      */
362     msec_delay(10);
363
364     ctrl = E1000_READ_REG(hw, CTRL);
365
366     /* Must reset the PHY before resetting the MAC */
367     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
368         E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_PHY_RST));
369         msec_delay(5);
370     }
371
372     /* Issue a global reset to the MAC.  This will reset the chip's
373      * transmit, receive, DMA, and link units.  It will not effect
374      * the current PCI configuration.  The global reset bit is self-
375      * clearing, and should clear within a microsecond.
376      */
377     DEBUGOUT("Issuing a global reset to MAC\n");
378
379     switch(hw->mac_type) {
380         case e1000_82544:
381         case e1000_82540:
382         case e1000_82545:
383         case e1000_82546:
384         case e1000_82541:
385         case e1000_82541_rev_2:
386             /* These controllers can't ack the 64-bit write when issuing the
387              * reset, so use IO-mapping as a workaround to issue the reset */
388             E1000_WRITE_REG_IO(hw, CTRL, (ctrl | E1000_CTRL_RST));
389             break;
390         case e1000_82545_rev_3:
391         case e1000_82546_rev_3:
392             /* Reset is performed on a shadow of the control register */
393             E1000_WRITE_REG(hw, CTRL_DUP, (ctrl | E1000_CTRL_RST));
394             break;
395         default:
396             E1000_WRITE_REG(hw, CTRL, (ctrl | E1000_CTRL_RST));
397             break;
398     }
399
400     /* After MAC reset, force reload of EEPROM to restore power-on settings to
401      * device.  Later controllers reload the EEPROM automatically, so just wait
402      * for reload to complete.
403      */
404     switch(hw->mac_type) {
405         case e1000_82542_rev2_0:
406         case e1000_82542_rev2_1:
407         case e1000_82543:
408         case e1000_82544:
409             /* Wait for reset to complete */
410             udelay(10);
411             ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
412             ctrl_ext |= E1000_CTRL_EXT_EE_RST;
413             E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
414             E1000_WRITE_FLUSH(hw);
415             /* Wait for EEPROM reload */
416             msec_delay(2);
417             break;
418         case e1000_82541:
419         case e1000_82541_rev_2:
420         case e1000_82547:
421         case e1000_82547_rev_2:
422             /* Wait for EEPROM reload */
423             msec_delay(20);
424             break;
425         default:
426             /* Wait for EEPROM reload (it happens automatically) */
427             msec_delay(5);
428             break;
429     }
430
431     /* Disable HW ARPs on ASF enabled adapters */
432     if(hw->mac_type >= e1000_82540) {
433         manc = E1000_READ_REG(hw, MANC);
434         manc &= ~(E1000_MANC_ARP_EN);
435         E1000_WRITE_REG(hw, MANC, manc);
436     }
437
438     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
439         e1000_phy_init_script(hw);
440
441         /* Configure activity LED after PHY reset */
442         led_ctrl = E1000_READ_REG(hw, LEDCTL);
443         led_ctrl &= IGP_ACTIVITY_LED_MASK;
444         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
445         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
446     }
447
448     /* Clear interrupt mask to stop board from generating interrupts */
449     DEBUGOUT("Masking off all interrupts\n");
450     E1000_WRITE_REG(hw, IMC, 0xffffffff);
451
452     /* Clear any pending interrupt events. */
453     icr = E1000_READ_REG(hw, ICR);
454
455     /* If MWI was previously enabled, reenable it. */
456     if(hw->mac_type == e1000_82542_rev2_0) {
457         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
458             e1000_pci_set_mwi(hw);
459     }
460
461     return E1000_SUCCESS;
462 }
463
464 /******************************************************************************
465  * Performs basic configuration of the adapter.
466  *
467  * hw - Struct containing variables accessed by shared code
468  *
469  * Assumes that the controller has previously been reset and is in a
470  * post-reset uninitialized state. Initializes the receive address registers,
471  * multicast table, and VLAN filter table. Calls routines to setup link
472  * configuration and flow control settings. Clears all on-chip counters. Leaves
473  * the transmit and receive units disabled and uninitialized.
474  *****************************************************************************/
475 int32_t
476 e1000_init_hw(struct e1000_hw *hw)
477 {
478     uint32_t ctrl;
479     uint32_t i;
480     int32_t ret_val;
481     uint16_t pcix_cmd_word;
482     uint16_t pcix_stat_hi_word;
483     uint16_t cmd_mmrbc;
484     uint16_t stat_mmrbc;
485     DEBUGFUNC("e1000_init_hw");
486
487     /* Initialize Identification LED */
488     ret_val = e1000_id_led_init(hw);
489     if(ret_val) {
490         DEBUGOUT("Error Initializing Identification LED\n");
491         return ret_val;
492     }
493
494     /* Set the media type and TBI compatibility */
495     e1000_set_media_type(hw);
496
497     /* Disabling VLAN filtering. */
498     DEBUGOUT("Initializing the IEEE VLAN\n");
499     E1000_WRITE_REG(hw, VET, 0);
500
501     e1000_clear_vfta(hw);
502
503     /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
504     if(hw->mac_type == e1000_82542_rev2_0) {
505         DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
506         e1000_pci_clear_mwi(hw);
507         E1000_WRITE_REG(hw, RCTL, E1000_RCTL_RST);
508         E1000_WRITE_FLUSH(hw);
509         msec_delay(5);
510     }
511
512     /* Setup the receive address. This involves initializing all of the Receive
513      * Address Registers (RARs 0 - 15).
514      */
515     e1000_init_rx_addrs(hw);
516
517     /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
518     if(hw->mac_type == e1000_82542_rev2_0) {
519         E1000_WRITE_REG(hw, RCTL, 0);
520         E1000_WRITE_FLUSH(hw);
521         msec_delay(1);
522         if(hw->pci_cmd_word & CMD_MEM_WRT_INVALIDATE)
523             e1000_pci_set_mwi(hw);
524     }
525
526     /* Zero out the Multicast HASH table */
527     DEBUGOUT("Zeroing the MTA\n");
528     for(i = 0; i < E1000_MC_TBL_SIZE; i++)
529         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
530
531     /* Set the PCI priority bit correctly in the CTRL register.  This
532      * determines if the adapter gives priority to receives, or if it
533      * gives equal priority to transmits and receives.
534      */
535     if(hw->dma_fairness) {
536         ctrl = E1000_READ_REG(hw, CTRL);
537         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PRIOR);
538     }
539
540     switch(hw->mac_type) {
541     case e1000_82545_rev_3:
542     case e1000_82546_rev_3:
543         break;
544     default:
545         /* Workaround for PCI-X problem when BIOS sets MMRBC incorrectly. */
546         if(hw->bus_type == e1000_bus_type_pcix) {
547             e1000_read_pci_cfg(hw, PCIX_COMMAND_REGISTER, &pcix_cmd_word);
548             e1000_read_pci_cfg(hw, PCIX_STATUS_REGISTER_HI,
549                 &pcix_stat_hi_word);
550             cmd_mmrbc = (pcix_cmd_word & PCIX_COMMAND_MMRBC_MASK) >>
551                 PCIX_COMMAND_MMRBC_SHIFT;
552             stat_mmrbc = (pcix_stat_hi_word & PCIX_STATUS_HI_MMRBC_MASK) >>
553                 PCIX_STATUS_HI_MMRBC_SHIFT;
554             if(stat_mmrbc == PCIX_STATUS_HI_MMRBC_4K)
555                 stat_mmrbc = PCIX_STATUS_HI_MMRBC_2K;
556             if(cmd_mmrbc > stat_mmrbc) {
557                 pcix_cmd_word &= ~PCIX_COMMAND_MMRBC_MASK;
558                 pcix_cmd_word |= stat_mmrbc << PCIX_COMMAND_MMRBC_SHIFT;
559                 e1000_write_pci_cfg(hw, PCIX_COMMAND_REGISTER,
560                     &pcix_cmd_word);
561             }
562         }
563         break;
564     }
565
566     /* Call a subroutine to configure the link and setup flow control. */
567     ret_val = e1000_setup_link(hw);
568
569     /* Set the transmit descriptor write-back policy */
570     if(hw->mac_type > e1000_82544) {
571         ctrl = E1000_READ_REG(hw, TXDCTL);
572         ctrl = (ctrl & ~E1000_TXDCTL_WTHRESH) | E1000_TXDCTL_FULL_TX_DESC_WB;
573         E1000_WRITE_REG(hw, TXDCTL, ctrl);
574     }
575
576     /* Clear all of the statistics registers (clear on read).  It is
577      * important that we do this after we have tried to establish link
578      * because the symbol error count will increment wildly if there
579      * is no link.
580      */
581     e1000_clear_hw_cntrs(hw);
582
583     return ret_val;
584 }
585
586 /******************************************************************************
587  * Adjust SERDES output amplitude based on EEPROM setting.
588  *
589  * hw - Struct containing variables accessed by shared code.
590  *****************************************************************************/
591 static int32_t
592 e1000_adjust_serdes_amplitude(struct e1000_hw *hw)
593 {
594     uint16_t eeprom_data;
595     int32_t  ret_val;
596
597     DEBUGFUNC("e1000_adjust_serdes_amplitude");
598
599     if(hw->media_type != e1000_media_type_internal_serdes)
600         return E1000_SUCCESS;
601
602     switch(hw->mac_type) {
603     case e1000_82545_rev_3:
604     case e1000_82546_rev_3:
605         break;
606     default:
607         return E1000_SUCCESS;
608     }
609
610     ret_val = e1000_read_eeprom(hw, EEPROM_SERDES_AMPLITUDE, 1, &eeprom_data);
611     if (ret_val) {
612         return ret_val;
613     }
614
615     if(eeprom_data != EEPROM_RESERVED_WORD) {
616         /* Adjust SERDES output amplitude only. */
617         eeprom_data &= EEPROM_SERDES_AMPLITUDE_MASK; 
618         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_EXT_CTRL, eeprom_data);
619         if(ret_val)
620             return ret_val;
621     }
622
623     return E1000_SUCCESS;
624 }
625
626 /******************************************************************************
627  * Configures flow control and link settings.
628  *
629  * hw - Struct containing variables accessed by shared code
630  *
631  * Determines which flow control settings to use. Calls the apropriate media-
632  * specific link configuration function. Configures the flow control settings.
633  * Assuming the adapter has a valid link partner, a valid link should be
634  * established. Assumes the hardware has previously been reset and the
635  * transmitter and receiver are not enabled.
636  *****************************************************************************/
637 int32_t
638 e1000_setup_link(struct e1000_hw *hw)
639 {
640     uint32_t ctrl_ext;
641     int32_t ret_val;
642     uint16_t eeprom_data;
643
644     DEBUGFUNC("e1000_setup_link");
645
646     /* Read and store word 0x0F of the EEPROM. This word contains bits
647      * that determine the hardware's default PAUSE (flow control) mode,
648      * a bit that determines whether the HW defaults to enabling or
649      * disabling auto-negotiation, and the direction of the
650      * SW defined pins. If there is no SW over-ride of the flow
651      * control setting, then the variable hw->fc will
652      * be initialized based on a value in the EEPROM.
653      */
654     if(e1000_read_eeprom(hw, EEPROM_INIT_CONTROL2_REG, 1, &eeprom_data) < 0) {
655         DEBUGOUT("EEPROM Read Error\n");
656         return -E1000_ERR_EEPROM;
657     }
658
659     if(hw->fc == e1000_fc_default) {
660         if((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) == 0)
661             hw->fc = e1000_fc_none;
662         else if((eeprom_data & EEPROM_WORD0F_PAUSE_MASK) ==
663                 EEPROM_WORD0F_ASM_DIR)
664             hw->fc = e1000_fc_tx_pause;
665         else
666             hw->fc = e1000_fc_full;
667     }
668
669     /* We want to save off the original Flow Control configuration just
670      * in case we get disconnected and then reconnected into a different
671      * hub or switch with different Flow Control capabilities.
672      */
673     if(hw->mac_type == e1000_82542_rev2_0)
674         hw->fc &= (~e1000_fc_tx_pause);
675
676     if((hw->mac_type < e1000_82543) && (hw->report_tx_early == 1))
677         hw->fc &= (~e1000_fc_rx_pause);
678
679     hw->original_fc = hw->fc;
680
681     DEBUGOUT1("After fix-ups FlowControl is now = %x\n", hw->fc);
682
683     /* Take the 4 bits from EEPROM word 0x0F that determine the initial
684      * polarity value for the SW controlled pins, and setup the
685      * Extended Device Control reg with that info.
686      * This is needed because one of the SW controlled pins is used for
687      * signal detection.  So this should be done before e1000_setup_pcs_link()
688      * or e1000_phy_setup() is called.
689      */
690     if(hw->mac_type == e1000_82543) {
691         ctrl_ext = ((eeprom_data & EEPROM_WORD0F_SWPDIO_EXT) <<
692                     SWDPIO__EXT_SHIFT);
693         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
694     }
695
696     /* Call the necessary subroutine to configure the link. */
697     ret_val = (hw->media_type == e1000_media_type_copper) ?
698               e1000_setup_copper_link(hw) :
699               e1000_setup_fiber_serdes_link(hw);
700
701     /* Initialize the flow control address, type, and PAUSE timer
702      * registers to their default values.  This is done even if flow
703      * control is disabled, because it does not hurt anything to
704      * initialize these registers.
705      */
706     DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");
707
708     E1000_WRITE_REG(hw, FCAL, FLOW_CONTROL_ADDRESS_LOW);
709     E1000_WRITE_REG(hw, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
710     E1000_WRITE_REG(hw, FCT, FLOW_CONTROL_TYPE);
711     E1000_WRITE_REG(hw, FCTTV, hw->fc_pause_time);
712
713     /* Set the flow control receive threshold registers.  Normally,
714      * these registers will be set to a default threshold that may be
715      * adjusted later by the driver's runtime code.  However, if the
716      * ability to transmit pause frames in not enabled, then these
717      * registers will be set to 0.
718      */
719     if(!(hw->fc & e1000_fc_tx_pause)) {
720         E1000_WRITE_REG(hw, FCRTL, 0);
721         E1000_WRITE_REG(hw, FCRTH, 0);
722     } else {
723         /* We need to set up the Receive Threshold high and low water marks
724          * as well as (optionally) enabling the transmission of XON frames.
725          */
726         if(hw->fc_send_xon) {
727             E1000_WRITE_REG(hw, FCRTL, (hw->fc_low_water | E1000_FCRTL_XONE));
728             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
729         } else {
730             E1000_WRITE_REG(hw, FCRTL, hw->fc_low_water);
731             E1000_WRITE_REG(hw, FCRTH, hw->fc_high_water);
732         }
733     }
734     return ret_val;
735 }
736
737 /******************************************************************************
738  * Sets up link for a fiber based or serdes based adapter
739  *
740  * hw - Struct containing variables accessed by shared code
741  *
742  * Manipulates Physical Coding Sublayer functions in order to configure
743  * link. Assumes the hardware has been previously reset and the transmitter
744  * and receiver are not enabled.
745  *****************************************************************************/
746 static int32_t
747 e1000_setup_fiber_serdes_link(struct e1000_hw *hw)
748 {
749     uint32_t ctrl;
750     uint32_t status;
751     uint32_t txcw = 0;
752     uint32_t i;
753     uint32_t signal = 0;
754     int32_t ret_val;
755
756     DEBUGFUNC("e1000_setup_fiber_serdes_link");
757
758     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
759      * set when the optics detect a signal. On older adapters, it will be
760      * cleared when there is a signal.  This applies to fiber media only.
761      * If we're on serdes media, adjust the output amplitude to value set in
762      * the EEPROM.
763      */
764     ctrl = E1000_READ_REG(hw, CTRL);
765     if(hw->media_type == e1000_media_type_fiber)
766         signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
767
768     ret_val = e1000_adjust_serdes_amplitude(hw);
769     if(ret_val)
770         return ret_val;
771
772     /* Take the link out of reset */
773     ctrl &= ~(E1000_CTRL_LRST);
774
775     /* Adjust VCO speed to improve BER performance */
776     ret_val = e1000_set_vco_speed(hw);
777     if(ret_val)
778         return ret_val;
779
780     e1000_config_collision_dist(hw);
781
782     /* Check for a software override of the flow control settings, and setup
783      * the device accordingly.  If auto-negotiation is enabled, then software
784      * will have to set the "PAUSE" bits to the correct value in the Tranmsit
785      * Config Word Register (TXCW) and re-start auto-negotiation.  However, if
786      * auto-negotiation is disabled, then software will have to manually
787      * configure the two flow control enable bits in the CTRL register.
788      *
789      * The possible values of the "fc" parameter are:
790      *      0:  Flow control is completely disabled
791      *      1:  Rx flow control is enabled (we can receive pause frames, but
792      *          not send pause frames).
793      *      2:  Tx flow control is enabled (we can send pause frames but we do
794      *          not support receiving pause frames).
795      *      3:  Both Rx and TX flow control (symmetric) are enabled.
796      */
797     switch (hw->fc) {
798     case e1000_fc_none:
799         /* Flow control is completely disabled by a software over-ride. */
800         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD);
801         break;
802     case e1000_fc_rx_pause:
803         /* RX Flow control is enabled and TX Flow control is disabled by a
804          * software over-ride. Since there really isn't a way to advertise
805          * that we are capable of RX Pause ONLY, we will advertise that we
806          * support both symmetric and asymmetric RX PAUSE. Later, we will
807          *  disable the adapter's ability to send PAUSE frames.
808          */
809         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
810         break;
811     case e1000_fc_tx_pause:
812         /* TX Flow control is enabled, and RX Flow control is disabled, by a
813          * software over-ride.
814          */
815         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
816         break;
817     case e1000_fc_full:
818         /* Flow control (both RX and TX) is enabled by a software over-ride. */
819         txcw = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
820         break;
821     default:
822         DEBUGOUT("Flow control param set incorrectly\n");
823         return -E1000_ERR_CONFIG;
824         break;
825     }
826
827     /* Since auto-negotiation is enabled, take the link out of reset (the link
828      * will be in reset, because we previously reset the chip). This will
829      * restart auto-negotiation.  If auto-neogtiation is successful then the
830      * link-up status bit will be set and the flow control enable bits (RFCE
831      * and TFCE) will be set according to their negotiated value.
832      */
833     DEBUGOUT("Auto-negotiation enabled\n");
834
835     E1000_WRITE_REG(hw, TXCW, txcw);
836     E1000_WRITE_REG(hw, CTRL, ctrl);
837     E1000_WRITE_FLUSH(hw);
838
839     hw->txcw = txcw;
840     msec_delay(1);
841
842     /* If we have a signal (the cable is plugged in) then poll for a "Link-Up"
843      * indication in the Device Status Register.  Time-out if a link isn't
844      * seen in 500 milliseconds seconds (Auto-negotiation should complete in
845      * less than 500 milliseconds even if the other end is doing it in SW).
846      * For internal serdes, we just assume a signal is present, then poll.
847      */
848     if(hw->media_type == e1000_media_type_internal_serdes ||
849        (E1000_READ_REG(hw, CTRL) & E1000_CTRL_SWDPIN1) == signal) {
850         DEBUGOUT("Looking for Link\n");
851         for(i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
852             msec_delay(10);
853             status = E1000_READ_REG(hw, STATUS);
854             if(status & E1000_STATUS_LU) break;
855         }
856         if(i == (LINK_UP_TIMEOUT / 10)) {
857             DEBUGOUT("Never got a valid link from auto-neg!!!\n");
858             hw->autoneg_failed = 1;
859             /* AutoNeg failed to achieve a link, so we'll call
860              * e1000_check_for_link. This routine will force the link up if
861              * we detect a signal. This will allow us to communicate with
862              * non-autonegotiating link partners.
863              */
864             ret_val = e1000_check_for_link(hw);
865             if(ret_val) {
866                 DEBUGOUT("Error while checking for link\n");
867                 return ret_val;
868             }
869             hw->autoneg_failed = 0;
870         } else {
871             hw->autoneg_failed = 0;
872             DEBUGOUT("Valid Link Found\n");
873         }
874     } else {
875         DEBUGOUT("No Signal Detected\n");
876     }
877     return E1000_SUCCESS;
878 }
879
880 /******************************************************************************
881 * Detects which PHY is present and the speed and duplex
882 *
883 * hw - Struct containing variables accessed by shared code
884 ******************************************************************************/
885 static int32_t
886 e1000_setup_copper_link(struct e1000_hw *hw)
887 {
888     uint32_t ctrl;
889     uint32_t led_ctrl;
890     int32_t ret_val;
891     uint16_t i;
892     uint16_t phy_data;
893
894     DEBUGFUNC("e1000_setup_copper_link");
895
896     ctrl = E1000_READ_REG(hw, CTRL);
897     /* With 82543, we need to force speed and duplex on the MAC equal to what
898      * the PHY speed and duplex configuration is. In addition, we need to
899      * perform a hardware reset on the PHY to take it out of reset.
900      */
901     if(hw->mac_type > e1000_82543) {
902         ctrl |= E1000_CTRL_SLU;
903         ctrl &= ~(E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
904         E1000_WRITE_REG(hw, CTRL, ctrl);
905     } else {
906         ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX | E1000_CTRL_SLU);
907         E1000_WRITE_REG(hw, CTRL, ctrl);
908         e1000_phy_hw_reset(hw);
909     }
910
911     /* Make sure we have a valid PHY */
912     ret_val = e1000_detect_gig_phy(hw);
913     if(ret_val) {
914         DEBUGOUT("Error, did not detect valid phy.\n");
915         return ret_val;
916     }
917     DEBUGOUT1("Phy ID = %x \n", hw->phy_id);
918
919     /* Set PHY to class A mode (if necessary) */
920     ret_val = e1000_set_phy_mode(hw);
921     if(ret_val)
922         return ret_val;
923
924     if((hw->mac_type == e1000_82545_rev_3) ||
925        (hw->mac_type == e1000_82546_rev_3)) {
926         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
927         phy_data |= 0x00000008;
928         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
929     }
930
931     if(hw->mac_type <= e1000_82543 ||
932        hw->mac_type == e1000_82541 || hw->mac_type == e1000_82547 ||
933        hw->mac_type == e1000_82541_rev_2 || hw->mac_type == e1000_82547_rev_2)
934         hw->phy_reset_disable = FALSE;
935
936     if(!hw->phy_reset_disable) {
937         if (hw->phy_type == e1000_phy_igp) {
938
939             ret_val = e1000_phy_reset(hw);
940             if(ret_val) {
941                 DEBUGOUT("Error Resetting the PHY\n");
942                 return ret_val;
943             }
944
945             /* Wait 10ms for MAC to configure PHY from eeprom settings */
946             msec_delay(15);
947
948             /* Configure activity LED after PHY reset */
949             led_ctrl = E1000_READ_REG(hw, LEDCTL);
950             led_ctrl &= IGP_ACTIVITY_LED_MASK;
951             led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
952             E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
953
954             /* disable lplu d3 during driver init */
955             ret_val = e1000_set_d3_lplu_state(hw, FALSE);
956             if(ret_val) {
957                 DEBUGOUT("Error Disabling LPLU D3\n");
958                 return ret_val;
959             }
960
961             /* Configure mdi-mdix settings */
962             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL,
963                                          &phy_data);
964             if(ret_val)
965                 return ret_val;
966
967             if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
968                 hw->dsp_config_state = e1000_dsp_config_disabled;
969                 /* Force MDI for IGP B-0 PHY */
970                 phy_data &= ~(IGP01E1000_PSCR_AUTO_MDIX |
971                               IGP01E1000_PSCR_FORCE_MDI_MDIX);
972                 hw->mdix = 1;
973
974             } else {
975                 hw->dsp_config_state = e1000_dsp_config_enabled;
976                 phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
977
978                 switch (hw->mdix) {
979                 case 1:
980                     phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
981                     break;
982                 case 2:
983                     phy_data |= IGP01E1000_PSCR_FORCE_MDI_MDIX;
984                     break;
985                 case 0:
986                 default:
987                     phy_data |= IGP01E1000_PSCR_AUTO_MDIX;
988                     break;
989                 }
990             }
991             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL,
992                                           phy_data);
993             if(ret_val)
994                 return ret_val;
995
996             /* set auto-master slave resolution settings */
997             if(hw->autoneg) {
998                 e1000_ms_type phy_ms_setting = hw->master_slave;
999
1000                 if(hw->ffe_config_state == e1000_ffe_config_active)
1001                     hw->ffe_config_state = e1000_ffe_config_enabled;
1002
1003                 if(hw->dsp_config_state == e1000_dsp_config_activated)
1004                     hw->dsp_config_state = e1000_dsp_config_enabled;
1005
1006                 /* when autonegotiation advertisment is only 1000Mbps then we
1007                  * should disable SmartSpeed and enable Auto MasterSlave
1008                  * resolution as hardware default. */
1009                 if(hw->autoneg_advertised == ADVERTISE_1000_FULL) {
1010                     /* Disable SmartSpeed */
1011                     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG,
1012                                                  &phy_data);
1013                     if(ret_val)
1014                         return ret_val;
1015                     phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
1016                     ret_val = e1000_write_phy_reg(hw,
1017                                                   IGP01E1000_PHY_PORT_CONFIG,
1018                                                   phy_data);
1019                     if(ret_val)
1020                         return ret_val;
1021                     /* Set auto Master/Slave resolution process */
1022                     ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1023                     if(ret_val)
1024                         return ret_val;
1025                     phy_data &= ~CR_1000T_MS_ENABLE;
1026                     ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1027                     if(ret_val)
1028                         return ret_val;
1029                 }
1030
1031                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &phy_data);
1032                 if(ret_val)
1033                     return ret_val;
1034
1035                 /* load defaults for future use */
1036                 hw->original_master_slave = (phy_data & CR_1000T_MS_ENABLE) ?
1037                                             ((phy_data & CR_1000T_MS_VALUE) ?
1038                                              e1000_ms_force_master :
1039                                              e1000_ms_force_slave) :
1040                                              e1000_ms_auto;
1041
1042                 switch (phy_ms_setting) {
1043                 case e1000_ms_force_master:
1044                     phy_data |= (CR_1000T_MS_ENABLE | CR_1000T_MS_VALUE);
1045                     break;
1046                 case e1000_ms_force_slave:
1047                     phy_data |= CR_1000T_MS_ENABLE;
1048                     phy_data &= ~(CR_1000T_MS_VALUE);
1049                     break;
1050                 case e1000_ms_auto:
1051                     phy_data &= ~CR_1000T_MS_ENABLE;
1052                 default:
1053                     break;
1054                 }
1055                 ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, phy_data);
1056                 if(ret_val)
1057                     return ret_val;
1058             }
1059         } else {
1060             /* Enable CRS on TX. This must be set for half-duplex operation. */
1061             ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
1062                                          &phy_data);
1063             if(ret_val)
1064                 return ret_val;
1065
1066             phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1067
1068             /* Options:
1069              *   MDI/MDI-X = 0 (default)
1070              *   0 - Auto for all speeds
1071              *   1 - MDI mode
1072              *   2 - MDI-X mode
1073              *   3 - Auto for 1000Base-T only (MDI-X for 10/100Base-T modes)
1074              */
1075             phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1076
1077             switch (hw->mdix) {
1078             case 1:
1079                 phy_data |= M88E1000_PSCR_MDI_MANUAL_MODE;
1080                 break;
1081             case 2:
1082                 phy_data |= M88E1000_PSCR_MDIX_MANUAL_MODE;
1083                 break;
1084             case 3:
1085                 phy_data |= M88E1000_PSCR_AUTO_X_1000T;
1086                 break;
1087             case 0:
1088             default:
1089                 phy_data |= M88E1000_PSCR_AUTO_X_MODE;
1090                 break;
1091             }
1092
1093             /* Options:
1094              *   disable_polarity_correction = 0 (default)
1095              *       Automatic Correction for Reversed Cable Polarity
1096              *   0 - Disabled
1097              *   1 - Enabled
1098              */
1099             phy_data &= ~M88E1000_PSCR_POLARITY_REVERSAL;
1100             if(hw->disable_polarity_correction == 1)
1101                 phy_data |= M88E1000_PSCR_POLARITY_REVERSAL;
1102             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL,
1103                                           phy_data);
1104             if(ret_val)
1105                 return ret_val;
1106
1107             /* Force TX_CLK in the Extended PHY Specific Control Register
1108              * to 25MHz clock.
1109              */
1110             ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1111                                          &phy_data);
1112             if(ret_val)
1113                 return ret_val;
1114
1115             phy_data |= M88E1000_EPSCR_TX_CLK_25;
1116
1117             if (hw->phy_revision < M88E1011_I_REV_4) {
1118                 /* Configure Master and Slave downshift values */
1119                 phy_data &= ~(M88E1000_EPSCR_MASTER_DOWNSHIFT_MASK |
1120                               M88E1000_EPSCR_SLAVE_DOWNSHIFT_MASK);
1121                 phy_data |= (M88E1000_EPSCR_MASTER_DOWNSHIFT_1X |
1122                              M88E1000_EPSCR_SLAVE_DOWNSHIFT_1X);
1123                 ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL,
1124                                               phy_data);
1125                 if(ret_val)
1126                     return ret_val;
1127             }
1128
1129             /* SW Reset the PHY so all changes take effect */
1130             ret_val = e1000_phy_reset(hw);
1131             if(ret_val) {
1132                 DEBUGOUT("Error Resetting the PHY\n");
1133                 return ret_val;
1134             }
1135         }
1136
1137         /* Options:
1138          *   autoneg = 1 (default)
1139          *      PHY will advertise value(s) parsed from
1140          *      autoneg_advertised and fc
1141          *   autoneg = 0
1142          *      PHY will be set to 10H, 10F, 100H, or 100F
1143          *      depending on value parsed from forced_speed_duplex.
1144          */
1145
1146         /* Is autoneg enabled?  This is enabled by default or by software
1147          * override.  If so, call e1000_phy_setup_autoneg routine to parse the
1148          * autoneg_advertised and fc options. If autoneg is NOT enabled, then
1149          * the user should have provided a speed/duplex override.  If so, then
1150          * call e1000_phy_force_speed_duplex to parse and set this up.
1151          */
1152         if(hw->autoneg) {
1153             /* Perform some bounds checking on the hw->autoneg_advertised
1154              * parameter.  If this variable is zero, then set it to the default.
1155              */
1156             hw->autoneg_advertised &= AUTONEG_ADVERTISE_SPEED_DEFAULT;
1157
1158             /* If autoneg_advertised is zero, we assume it was not defaulted
1159              * by the calling code so we set to advertise full capability.
1160              */
1161             if(hw->autoneg_advertised == 0)
1162                 hw->autoneg_advertised = AUTONEG_ADVERTISE_SPEED_DEFAULT;
1163
1164             DEBUGOUT("Reconfiguring auto-neg advertisement params\n");
1165             ret_val = e1000_phy_setup_autoneg(hw);
1166             if(ret_val) {
1167                 DEBUGOUT("Error Setting up Auto-Negotiation\n");
1168                 return ret_val;
1169             }
1170             DEBUGOUT("Restarting Auto-Neg\n");
1171
1172             /* Restart auto-negotiation by setting the Auto Neg Enable bit and
1173              * the Auto Neg Restart bit in the PHY control register.
1174              */
1175             ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
1176             if(ret_val)
1177                 return ret_val;
1178
1179             phy_data |= (MII_CR_AUTO_NEG_EN | MII_CR_RESTART_AUTO_NEG);
1180             ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
1181             if(ret_val)
1182                 return ret_val;
1183
1184             /* Does the user want to wait for Auto-Neg to complete here, or
1185              * check at a later time (for example, callback routine).
1186              */
1187             if(hw->wait_autoneg_complete) {
1188                 ret_val = e1000_wait_autoneg(hw);
1189                 if(ret_val) {
1190                     DEBUGOUT("Error while waiting for autoneg to complete\n");
1191                     return ret_val;
1192                 }
1193             }
1194             hw->get_link_status = TRUE;
1195         } else {
1196             DEBUGOUT("Forcing speed and duplex\n");
1197             ret_val = e1000_phy_force_speed_duplex(hw);
1198             if(ret_val) {
1199                 DEBUGOUT("Error Forcing Speed and Duplex\n");
1200                 return ret_val;
1201             }
1202         }
1203     } /* !hw->phy_reset_disable */
1204
1205     /* Check link status. Wait up to 100 microseconds for link to become
1206      * valid.
1207      */
1208     for(i = 0; i < 10; i++) {
1209         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1210         if(ret_val)
1211             return ret_val;
1212         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
1213         if(ret_val)
1214             return ret_val;
1215
1216         if(phy_data & MII_SR_LINK_STATUS) {
1217             /* We have link, so we need to finish the config process:
1218              *   1) Set up the MAC to the current PHY speed/duplex
1219              *      if we are on 82543.  If we
1220              *      are on newer silicon, we only need to configure
1221              *      collision distance in the Transmit Control Register.
1222              *   2) Set up flow control on the MAC to that established with
1223              *      the link partner.
1224              */
1225             if(hw->mac_type >= e1000_82544) {
1226                 e1000_config_collision_dist(hw);
1227             } else {
1228                 ret_val = e1000_config_mac_to_phy(hw);
1229                 if(ret_val) {
1230                     DEBUGOUT("Error configuring MAC to PHY settings\n");
1231                     return ret_val;
1232                 }
1233             }
1234             ret_val = e1000_config_fc_after_link_up(hw);
1235             if(ret_val) {
1236                 DEBUGOUT("Error Configuring Flow Control\n");
1237                 return ret_val;
1238             }
1239             DEBUGOUT("Valid link established!!!\n");
1240
1241             if(hw->phy_type == e1000_phy_igp) {
1242                 ret_val = e1000_config_dsp_after_link_change(hw, TRUE);
1243                 if(ret_val) {
1244                     DEBUGOUT("Error Configuring DSP after link up\n");
1245                     return ret_val;
1246                 }
1247             }
1248             DEBUGOUT("Valid link established!!!\n");
1249             return E1000_SUCCESS;
1250         }
1251         udelay(10);
1252     }
1253
1254     DEBUGOUT("Unable to establish link!!!\n");
1255     return E1000_SUCCESS;
1256 }
1257
1258 /******************************************************************************
1259 * Configures PHY autoneg and flow control advertisement settings
1260 *
1261 * hw - Struct containing variables accessed by shared code
1262 ******************************************************************************/
1263 int32_t
1264 e1000_phy_setup_autoneg(struct e1000_hw *hw)
1265 {
1266     int32_t ret_val;
1267     uint16_t mii_autoneg_adv_reg;
1268     uint16_t mii_1000t_ctrl_reg;
1269
1270     DEBUGFUNC("e1000_phy_setup_autoneg");
1271
1272     /* Read the MII Auto-Neg Advertisement Register (Address 4). */
1273     ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV, &mii_autoneg_adv_reg);
1274     if(ret_val)
1275         return ret_val;
1276
1277     /* Read the MII 1000Base-T Control Register (Address 9). */
1278     ret_val = e1000_read_phy_reg(hw, PHY_1000T_CTRL, &mii_1000t_ctrl_reg);
1279     if(ret_val)
1280         return ret_val;
1281
1282     /* Need to parse both autoneg_advertised and fc and set up
1283      * the appropriate PHY registers.  First we will parse for
1284      * autoneg_advertised software override.  Since we can advertise
1285      * a plethora of combinations, we need to check each bit
1286      * individually.
1287      */
1288
1289     /* First we clear all the 10/100 mb speed bits in the Auto-Neg
1290      * Advertisement Register (Address 4) and the 1000 mb speed bits in
1291      * the  1000Base-T Control Register (Address 9).
1292      */
1293     mii_autoneg_adv_reg &= ~REG4_SPEED_MASK;
1294     mii_1000t_ctrl_reg &= ~REG9_SPEED_MASK;
1295
1296     DEBUGOUT1("autoneg_advertised %x\n", hw->autoneg_advertised);
1297
1298     /* Do we want to advertise 10 Mb Half Duplex? */
1299     if(hw->autoneg_advertised & ADVERTISE_10_HALF) {
1300         DEBUGOUT("Advertise 10mb Half duplex\n");
1301         mii_autoneg_adv_reg |= NWAY_AR_10T_HD_CAPS;
1302     }
1303
1304     /* Do we want to advertise 10 Mb Full Duplex? */
1305     if(hw->autoneg_advertised & ADVERTISE_10_FULL) {
1306         DEBUGOUT("Advertise 10mb Full duplex\n");
1307         mii_autoneg_adv_reg |= NWAY_AR_10T_FD_CAPS;
1308     }
1309
1310     /* Do we want to advertise 100 Mb Half Duplex? */
1311     if(hw->autoneg_advertised & ADVERTISE_100_HALF) {
1312         DEBUGOUT("Advertise 100mb Half duplex\n");
1313         mii_autoneg_adv_reg |= NWAY_AR_100TX_HD_CAPS;
1314     }
1315
1316     /* Do we want to advertise 100 Mb Full Duplex? */
1317     if(hw->autoneg_advertised & ADVERTISE_100_FULL) {
1318         DEBUGOUT("Advertise 100mb Full duplex\n");
1319         mii_autoneg_adv_reg |= NWAY_AR_100TX_FD_CAPS;
1320     }
1321
1322     /* We do not allow the Phy to advertise 1000 Mb Half Duplex */
1323     if(hw->autoneg_advertised & ADVERTISE_1000_HALF) {
1324         DEBUGOUT("Advertise 1000mb Half duplex requested, request denied!\n");
1325     }
1326
1327     /* Do we want to advertise 1000 Mb Full Duplex? */
1328     if(hw->autoneg_advertised & ADVERTISE_1000_FULL) {
1329         DEBUGOUT("Advertise 1000mb Full duplex\n");
1330         mii_1000t_ctrl_reg |= CR_1000T_FD_CAPS;
1331     }
1332
1333     /* Check for a software override of the flow control settings, and
1334      * setup the PHY advertisement registers accordingly.  If
1335      * auto-negotiation is enabled, then software will have to set the
1336      * "PAUSE" bits to the correct value in the Auto-Negotiation
1337      * Advertisement Register (PHY_AUTONEG_ADV) and re-start auto-negotiation.
1338      *
1339      * The possible values of the "fc" parameter are:
1340      *      0:  Flow control is completely disabled
1341      *      1:  Rx flow control is enabled (we can receive pause frames
1342      *          but not send pause frames).
1343      *      2:  Tx flow control is enabled (we can send pause frames
1344      *          but we do not support receiving pause frames).
1345      *      3:  Both Rx and TX flow control (symmetric) are enabled.
1346      *  other:  No software override.  The flow control configuration
1347      *          in the EEPROM is used.
1348      */
1349     switch (hw->fc) {
1350     case e1000_fc_none: /* 0 */
1351         /* Flow control (RX & TX) is completely disabled by a
1352          * software over-ride.
1353          */
1354         mii_autoneg_adv_reg &= ~(NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1355         break;
1356     case e1000_fc_rx_pause: /* 1 */
1357         /* RX Flow control is enabled, and TX Flow control is
1358          * disabled, by a software over-ride.
1359          */
1360         /* Since there really isn't a way to advertise that we are
1361          * capable of RX Pause ONLY, we will advertise that we
1362          * support both symmetric and asymmetric RX PAUSE.  Later
1363          * (in e1000_config_fc_after_link_up) we will disable the
1364          *hw's ability to send PAUSE frames.
1365          */
1366         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1367         break;
1368     case e1000_fc_tx_pause: /* 2 */
1369         /* TX Flow control is enabled, and RX Flow control is
1370          * disabled, by a software over-ride.
1371          */
1372         mii_autoneg_adv_reg |= NWAY_AR_ASM_DIR;
1373         mii_autoneg_adv_reg &= ~NWAY_AR_PAUSE;
1374         break;
1375     case e1000_fc_full: /* 3 */
1376         /* Flow control (both RX and TX) is enabled by a software
1377          * over-ride.
1378          */
1379         mii_autoneg_adv_reg |= (NWAY_AR_ASM_DIR | NWAY_AR_PAUSE);
1380         break;
1381     default:
1382         DEBUGOUT("Flow control param set incorrectly\n");
1383         return -E1000_ERR_CONFIG;
1384     }
1385
1386     ret_val = e1000_write_phy_reg(hw, PHY_AUTONEG_ADV, mii_autoneg_adv_reg);
1387     if(ret_val)
1388         return ret_val;
1389
1390     DEBUGOUT1("Auto-Neg Advertising %x\n", mii_autoneg_adv_reg);
1391
1392     ret_val = e1000_write_phy_reg(hw, PHY_1000T_CTRL, mii_1000t_ctrl_reg);
1393     if(ret_val)
1394         return ret_val;
1395
1396     return E1000_SUCCESS;
1397 }
1398
1399 /******************************************************************************
1400 * Force PHY speed and duplex settings to hw->forced_speed_duplex
1401 *
1402 * hw - Struct containing variables accessed by shared code
1403 ******************************************************************************/
1404 static int32_t
1405 e1000_phy_force_speed_duplex(struct e1000_hw *hw)
1406 {
1407     uint32_t ctrl;
1408     int32_t ret_val;
1409     uint16_t mii_ctrl_reg;
1410     uint16_t mii_status_reg;
1411     uint16_t phy_data;
1412     uint16_t i;
1413
1414     DEBUGFUNC("e1000_phy_force_speed_duplex");
1415
1416     /* Turn off Flow control if we are forcing speed and duplex. */
1417     hw->fc = e1000_fc_none;
1418
1419     DEBUGOUT1("hw->fc = %d\n", hw->fc);
1420
1421     /* Read the Device Control Register. */
1422     ctrl = E1000_READ_REG(hw, CTRL);
1423
1424     /* Set the bits to Force Speed and Duplex in the Device Ctrl Reg. */
1425     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1426     ctrl &= ~(DEVICE_SPEED_MASK);
1427
1428     /* Clear the Auto Speed Detect Enable bit. */
1429     ctrl &= ~E1000_CTRL_ASDE;
1430
1431     /* Read the MII Control Register. */
1432     ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &mii_ctrl_reg);
1433     if(ret_val)
1434         return ret_val;
1435
1436     /* We need to disable autoneg in order to force link and duplex. */
1437
1438     mii_ctrl_reg &= ~MII_CR_AUTO_NEG_EN;
1439
1440     /* Are we forcing Full or Half Duplex? */
1441     if(hw->forced_speed_duplex == e1000_100_full ||
1442        hw->forced_speed_duplex == e1000_10_full) {
1443         /* We want to force full duplex so we SET the full duplex bits in the
1444          * Device and MII Control Registers.
1445          */
1446         ctrl |= E1000_CTRL_FD;
1447         mii_ctrl_reg |= MII_CR_FULL_DUPLEX;
1448         DEBUGOUT("Full Duplex\n");
1449     } else {
1450         /* We want to force half duplex so we CLEAR the full duplex bits in
1451          * the Device and MII Control Registers.
1452          */
1453         ctrl &= ~E1000_CTRL_FD;
1454         mii_ctrl_reg &= ~MII_CR_FULL_DUPLEX;
1455         DEBUGOUT("Half Duplex\n");
1456     }
1457
1458     /* Are we forcing 100Mbps??? */
1459     if(hw->forced_speed_duplex == e1000_100_full ||
1460        hw->forced_speed_duplex == e1000_100_half) {
1461         /* Set the 100Mb bit and turn off the 1000Mb and 10Mb bits. */
1462         ctrl |= E1000_CTRL_SPD_100;
1463         mii_ctrl_reg |= MII_CR_SPEED_100;
1464         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_10);
1465         DEBUGOUT("Forcing 100mb ");
1466     } else {
1467         /* Set the 10Mb bit and turn off the 1000Mb and 100Mb bits. */
1468         ctrl &= ~(E1000_CTRL_SPD_1000 | E1000_CTRL_SPD_100);
1469         mii_ctrl_reg |= MII_CR_SPEED_10;
1470         mii_ctrl_reg &= ~(MII_CR_SPEED_1000 | MII_CR_SPEED_100);
1471         DEBUGOUT("Forcing 10mb ");
1472     }
1473
1474     e1000_config_collision_dist(hw);
1475
1476     /* Write the configured values back to the Device Control Reg. */
1477     E1000_WRITE_REG(hw, CTRL, ctrl);
1478
1479     if (hw->phy_type == e1000_phy_m88) {
1480         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1481         if(ret_val)
1482             return ret_val;
1483
1484         /* Clear Auto-Crossover to force MDI manually. M88E1000 requires MDI
1485          * forced whenever speed are duplex are forced.
1486          */
1487         phy_data &= ~M88E1000_PSCR_AUTO_X_MODE;
1488         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1489         if(ret_val)
1490             return ret_val;
1491
1492         DEBUGOUT1("M88E1000 PSCR: %x \n", phy_data);
1493
1494         /* Need to reset the PHY or these changes will be ignored */
1495         mii_ctrl_reg |= MII_CR_RESET;
1496     } else {
1497         /* Clear Auto-Crossover to force MDI manually.  IGP requires MDI
1498          * forced whenever speed or duplex are forced.
1499          */
1500         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, &phy_data);
1501         if(ret_val)
1502             return ret_val;
1503
1504         phy_data &= ~IGP01E1000_PSCR_AUTO_MDIX;
1505         phy_data &= ~IGP01E1000_PSCR_FORCE_MDI_MDIX;
1506
1507         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CTRL, phy_data);
1508         if(ret_val)
1509             return ret_val;
1510     }
1511
1512     /* Write back the modified PHY MII control register. */
1513     ret_val = e1000_write_phy_reg(hw, PHY_CTRL, mii_ctrl_reg);
1514     if(ret_val)
1515         return ret_val;
1516
1517     udelay(1);
1518
1519     /* The wait_autoneg_complete flag may be a little misleading here.
1520      * Since we are forcing speed and duplex, Auto-Neg is not enabled.
1521      * But we do want to delay for a period while forcing only so we
1522      * don't generate false No Link messages.  So we will wait here
1523      * only if the user has set wait_autoneg_complete to 1, which is
1524      * the default.
1525      */
1526     if(hw->wait_autoneg_complete) {
1527         /* We will wait for autoneg to complete. */
1528         DEBUGOUT("Waiting for forced speed/duplex link.\n");
1529         mii_status_reg = 0;
1530
1531         /* We will wait for autoneg to complete or 4.5 seconds to expire. */
1532         for(i = PHY_FORCE_TIME; i > 0; i--) {
1533             /* Read the MII Status Register and wait for Auto-Neg Complete bit
1534              * to be set.
1535              */
1536             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1537             if(ret_val)
1538                 return ret_val;
1539
1540             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1541             if(ret_val)
1542                 return ret_val;
1543
1544             if(mii_status_reg & MII_SR_LINK_STATUS) break;
1545             msec_delay(100);
1546         }
1547         if((i == 0) && (hw->phy_type == e1000_phy_m88)) {
1548             /* We didn't get link.  Reset the DSP and wait again for link. */
1549             ret_val = e1000_phy_reset_dsp(hw);
1550             if(ret_val) {
1551                 DEBUGOUT("Error Resetting PHY DSP\n");
1552                 return ret_val;
1553             }
1554         }
1555         /* This loop will early-out if the link condition has been met.  */
1556         for(i = PHY_FORCE_TIME; i > 0; i--) {
1557             if(mii_status_reg & MII_SR_LINK_STATUS) break;
1558             msec_delay(100);
1559             /* Read the MII Status Register and wait for Auto-Neg Complete bit
1560              * to be set.
1561              */
1562             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1563             if(ret_val)
1564                 return ret_val;
1565
1566             ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1567             if(ret_val)
1568                 return ret_val;
1569         }
1570     }
1571
1572     if (hw->phy_type == e1000_phy_m88) {
1573         /* Because we reset the PHY above, we need to re-force TX_CLK in the
1574          * Extended PHY Specific Control Register to 25MHz clock.  This value
1575          * defaults back to a 2.5MHz clock when the PHY is reset.
1576          */
1577         ret_val = e1000_read_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, &phy_data);
1578         if(ret_val)
1579             return ret_val;
1580
1581         phy_data |= M88E1000_EPSCR_TX_CLK_25;
1582         ret_val = e1000_write_phy_reg(hw, M88E1000_EXT_PHY_SPEC_CTRL, phy_data);
1583         if(ret_val)
1584             return ret_val;
1585
1586         /* In addition, because of the s/w reset above, we need to enable CRS on
1587          * TX.  This must be set for both full and half duplex operation.
1588          */
1589         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
1590         if(ret_val)
1591             return ret_val;
1592
1593         phy_data |= M88E1000_PSCR_ASSERT_CRS_ON_TX;
1594         ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, phy_data);
1595         if(ret_val)
1596             return ret_val;
1597     }
1598     return E1000_SUCCESS;
1599 }
1600
1601 /******************************************************************************
1602 * Sets the collision distance in the Transmit Control register
1603 *
1604 * hw - Struct containing variables accessed by shared code
1605 *
1606 * Link should have been established previously. Reads the speed and duplex
1607 * information from the Device Status register.
1608 ******************************************************************************/
1609 void
1610 e1000_config_collision_dist(struct e1000_hw *hw)
1611 {
1612     uint32_t tctl;
1613
1614     DEBUGFUNC("e1000_config_collision_dist");
1615
1616     tctl = E1000_READ_REG(hw, TCTL);
1617
1618     tctl &= ~E1000_TCTL_COLD;
1619     tctl |= E1000_COLLISION_DISTANCE << E1000_COLD_SHIFT;
1620
1621     E1000_WRITE_REG(hw, TCTL, tctl);
1622     E1000_WRITE_FLUSH(hw);
1623 }
1624
1625 /******************************************************************************
1626 * Sets MAC speed and duplex settings to reflect the those in the PHY
1627 *
1628 * hw - Struct containing variables accessed by shared code
1629 * mii_reg - data to write to the MII control register
1630 *
1631 * The contents of the PHY register containing the needed information need to
1632 * be passed in.
1633 ******************************************************************************/
1634 static int32_t
1635 e1000_config_mac_to_phy(struct e1000_hw *hw)
1636 {
1637     uint32_t ctrl;
1638     int32_t ret_val;
1639     uint16_t phy_data;
1640
1641     DEBUGFUNC("e1000_config_mac_to_phy");
1642
1643     /* Read the Device Control Register and set the bits to Force Speed
1644      * and Duplex.
1645      */
1646     ctrl = E1000_READ_REG(hw, CTRL);
1647     ctrl |= (E1000_CTRL_FRCSPD | E1000_CTRL_FRCDPX);
1648     ctrl &= ~(E1000_CTRL_SPD_SEL | E1000_CTRL_ILOS);
1649
1650     /* Set up duplex in the Device Control and Transmit Control
1651      * registers depending on negotiated values.
1652      */
1653     if (hw->phy_type == e1000_phy_igp) {
1654         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS,
1655                                      &phy_data);
1656         if(ret_val)
1657             return ret_val;
1658
1659         if(phy_data & IGP01E1000_PSSR_FULL_DUPLEX) ctrl |= E1000_CTRL_FD;
1660         else ctrl &= ~E1000_CTRL_FD;
1661
1662         e1000_config_collision_dist(hw);
1663
1664         /* Set up speed in the Device Control register depending on
1665          * negotiated values.
1666          */
1667         if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
1668            IGP01E1000_PSSR_SPEED_1000MBPS)
1669             ctrl |= E1000_CTRL_SPD_1000;
1670         else if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
1671                 IGP01E1000_PSSR_SPEED_100MBPS)
1672             ctrl |= E1000_CTRL_SPD_100;
1673     } else {
1674         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
1675                                      &phy_data);
1676         if(ret_val)
1677             return ret_val;
1678
1679         if(phy_data & M88E1000_PSSR_DPLX) ctrl |= E1000_CTRL_FD;
1680         else ctrl &= ~E1000_CTRL_FD;
1681
1682         e1000_config_collision_dist(hw);
1683
1684         /* Set up speed in the Device Control register depending on
1685          * negotiated values.
1686          */
1687         if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_1000MBS)
1688             ctrl |= E1000_CTRL_SPD_1000;
1689         else if((phy_data & M88E1000_PSSR_SPEED) == M88E1000_PSSR_100MBS)
1690             ctrl |= E1000_CTRL_SPD_100;
1691     }
1692     /* Write the configured values back to the Device Control Reg. */
1693     E1000_WRITE_REG(hw, CTRL, ctrl);
1694     return E1000_SUCCESS;
1695 }
1696
1697 /******************************************************************************
1698  * Forces the MAC's flow control settings.
1699  *
1700  * hw - Struct containing variables accessed by shared code
1701  *
1702  * Sets the TFCE and RFCE bits in the device control register to reflect
1703  * the adapter settings. TFCE and RFCE need to be explicitly set by
1704  * software when a Copper PHY is used because autonegotiation is managed
1705  * by the PHY rather than the MAC. Software must also configure these
1706  * bits when link is forced on a fiber connection.
1707  *****************************************************************************/
1708 int32_t
1709 e1000_force_mac_fc(struct e1000_hw *hw)
1710 {
1711     uint32_t ctrl;
1712
1713     DEBUGFUNC("e1000_force_mac_fc");
1714
1715     /* Get the current configuration of the Device Control Register */
1716     ctrl = E1000_READ_REG(hw, CTRL);
1717
1718     /* Because we didn't get link via the internal auto-negotiation
1719      * mechanism (we either forced link or we got link via PHY
1720      * auto-neg), we have to manually enable/disable transmit an
1721      * receive flow control.
1722      *
1723      * The "Case" statement below enables/disable flow control
1724      * according to the "hw->fc" parameter.
1725      *
1726      * The possible values of the "fc" parameter are:
1727      *      0:  Flow control is completely disabled
1728      *      1:  Rx flow control is enabled (we can receive pause
1729      *          frames but not send pause frames).
1730      *      2:  Tx flow control is enabled (we can send pause frames
1731      *          frames but we do not receive pause frames).
1732      *      3:  Both Rx and TX flow control (symmetric) is enabled.
1733      *  other:  No other values should be possible at this point.
1734      */
1735
1736     switch (hw->fc) {
1737     case e1000_fc_none:
1738         ctrl &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
1739         break;
1740     case e1000_fc_rx_pause:
1741         ctrl &= (~E1000_CTRL_TFCE);
1742         ctrl |= E1000_CTRL_RFCE;
1743         break;
1744     case e1000_fc_tx_pause:
1745         ctrl &= (~E1000_CTRL_RFCE);
1746         ctrl |= E1000_CTRL_TFCE;
1747         break;
1748     case e1000_fc_full:
1749         ctrl |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
1750         break;
1751     default:
1752         DEBUGOUT("Flow control param set incorrectly\n");
1753         return -E1000_ERR_CONFIG;
1754     }
1755
1756     /* Disable TX Flow Control for 82542 (rev 2.0) */
1757     if(hw->mac_type == e1000_82542_rev2_0)
1758         ctrl &= (~E1000_CTRL_TFCE);
1759
1760     E1000_WRITE_REG(hw, CTRL, ctrl);
1761     return E1000_SUCCESS;
1762 }
1763
1764 /******************************************************************************
1765  * Configures flow control settings after link is established
1766  *
1767  * hw - Struct containing variables accessed by shared code
1768  *
1769  * Should be called immediately after a valid link has been established.
1770  * Forces MAC flow control settings if link was forced. When in MII/GMII mode
1771  * and autonegotiation is enabled, the MAC flow control settings will be set
1772  * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
1773  * and RFCE bits will be automaticaly set to the negotiated flow control mode.
1774  *****************************************************************************/
1775 int32_t
1776 e1000_config_fc_after_link_up(struct e1000_hw *hw)
1777 {
1778     int32_t ret_val;
1779     uint16_t mii_status_reg;
1780     uint16_t mii_nway_adv_reg;
1781     uint16_t mii_nway_lp_ability_reg;
1782     uint16_t speed;
1783     uint16_t duplex;
1784
1785     DEBUGFUNC("e1000_config_fc_after_link_up");
1786
1787     /* Check for the case where we have fiber media and auto-neg failed
1788      * so we had to force link.  In this case, we need to force the
1789      * configuration of the MAC to match the "fc" parameter.
1790      */
1791     if(((hw->media_type == e1000_media_type_fiber) && (hw->autoneg_failed)) ||
1792        ((hw->media_type == e1000_media_type_internal_serdes) && (hw->autoneg_failed)) ||
1793        ((hw->media_type == e1000_media_type_copper) && (!hw->autoneg))) {
1794         ret_val = e1000_force_mac_fc(hw);
1795         if(ret_val) {
1796             DEBUGOUT("Error forcing flow control settings\n");
1797             return ret_val;
1798         }
1799     }
1800
1801     /* Check for the case where we have copper media and auto-neg is
1802      * enabled.  In this case, we need to check and see if Auto-Neg
1803      * has completed, and if so, how the PHY and link partner has
1804      * flow control configured.
1805      */
1806     if((hw->media_type == e1000_media_type_copper) && hw->autoneg) {
1807         /* Read the MII Status Register and check to see if AutoNeg
1808          * has completed.  We read this twice because this reg has
1809          * some "sticky" (latched) bits.
1810          */
1811         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1812         if(ret_val)
1813             return ret_val;
1814         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &mii_status_reg);
1815         if(ret_val)
1816             return ret_val;
1817
1818         if(mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
1819             /* The AutoNeg process has completed, so we now need to
1820              * read both the Auto Negotiation Advertisement Register
1821              * (Address 4) and the Auto_Negotiation Base Page Ability
1822              * Register (Address 5) to determine how flow control was
1823              * negotiated.
1824              */
1825             ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_ADV,
1826                                          &mii_nway_adv_reg);
1827             if(ret_val)
1828                 return ret_val;
1829             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY,
1830                                          &mii_nway_lp_ability_reg);
1831             if(ret_val)
1832                 return ret_val;
1833
1834             /* Two bits in the Auto Negotiation Advertisement Register
1835              * (Address 4) and two bits in the Auto Negotiation Base
1836              * Page Ability Register (Address 5) determine flow control
1837              * for both the PHY and the link partner.  The following
1838              * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
1839              * 1999, describes these PAUSE resolution bits and how flow
1840              * control is determined based upon these settings.
1841              * NOTE:  DC = Don't Care
1842              *
1843              *   LOCAL DEVICE  |   LINK PARTNER
1844              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
1845              *-------|---------|-------|---------|--------------------
1846              *   0   |    0    |  DC   |   DC    | e1000_fc_none
1847              *   0   |    1    |   0   |   DC    | e1000_fc_none
1848              *   0   |    1    |   1   |    0    | e1000_fc_none
1849              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1850              *   1   |    0    |   0   |   DC    | e1000_fc_none
1851              *   1   |   DC    |   1   |   DC    | e1000_fc_full
1852              *   1   |    1    |   0   |    0    | e1000_fc_none
1853              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1854              *
1855              */
1856             /* Are both PAUSE bits set to 1?  If so, this implies
1857              * Symmetric Flow Control is enabled at both ends.  The
1858              * ASM_DIR bits are irrelevant per the spec.
1859              *
1860              * For Symmetric Flow Control:
1861              *
1862              *   LOCAL DEVICE  |   LINK PARTNER
1863              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1864              *-------|---------|-------|---------|--------------------
1865              *   1   |   DC    |   1   |   DC    | e1000_fc_full
1866              *
1867              */
1868             if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1869                (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
1870                 /* Now we need to check if the user selected RX ONLY
1871                  * of pause frames.  In this case, we had to advertise
1872                  * FULL flow control because we could not advertise RX
1873                  * ONLY. Hence, we must now check to see if we need to
1874                  * turn OFF  the TRANSMISSION of PAUSE frames.
1875                  */
1876                 if(hw->original_fc == e1000_fc_full) {
1877                     hw->fc = e1000_fc_full;
1878                     DEBUGOUT("Flow Control = FULL.\r\n");
1879                 } else {
1880                     hw->fc = e1000_fc_rx_pause;
1881                     DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
1882                 }
1883             }
1884             /* For receiving PAUSE frames ONLY.
1885              *
1886              *   LOCAL DEVICE  |   LINK PARTNER
1887              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1888              *-------|---------|-------|---------|--------------------
1889              *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
1890              *
1891              */
1892             else if(!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1893                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1894                     (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1895                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1896                 hw->fc = e1000_fc_tx_pause;
1897                 DEBUGOUT("Flow Control = TX PAUSE frames only.\r\n");
1898             }
1899             /* For transmitting PAUSE frames ONLY.
1900              *
1901              *   LOCAL DEVICE  |   LINK PARTNER
1902              * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
1903              *-------|---------|-------|---------|--------------------
1904              *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
1905              *
1906              */
1907             else if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
1908                     (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
1909                     !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
1910                     (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
1911                 hw->fc = e1000_fc_rx_pause;
1912                 DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
1913             }
1914             /* Per the IEEE spec, at this point flow control should be
1915              * disabled.  However, we want to consider that we could
1916              * be connected to a legacy switch that doesn't advertise
1917              * desired flow control, but can be forced on the link
1918              * partner.  So if we advertised no flow control, that is
1919              * what we will resolve to.  If we advertised some kind of
1920              * receive capability (Rx Pause Only or Full Flow Control)
1921              * and the link partner advertised none, we will configure
1922              * ourselves to enable Rx Flow Control only.  We can do
1923              * this safely for two reasons:  If the link partner really
1924              * didn't want flow control enabled, and we enable Rx, no
1925              * harm done since we won't be receiving any PAUSE frames
1926              * anyway.  If the intent on the link partner was to have
1927              * flow control enabled, then by us enabling RX only, we
1928              * can at least receive pause frames and process them.
1929              * This is a good idea because in most cases, since we are
1930              * predominantly a server NIC, more times than not we will
1931              * be asked to delay transmission of packets than asking
1932              * our link partner to pause transmission of frames.
1933              */
1934             else if((hw->original_fc == e1000_fc_none ||
1935                      hw->original_fc == e1000_fc_tx_pause) ||
1936                     hw->fc_strict_ieee) {
1937                 hw->fc = e1000_fc_none;
1938                 DEBUGOUT("Flow Control = NONE.\r\n");
1939             } else {
1940                 hw->fc = e1000_fc_rx_pause;
1941                 DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
1942             }
1943
1944             /* Now we need to do one last check...  If we auto-
1945              * negotiated to HALF DUPLEX, flow control should not be
1946              * enabled per IEEE 802.3 spec.
1947              */
1948             ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
1949             if(ret_val) {
1950                 DEBUGOUT("Error getting link speed and duplex\n");
1951                 return ret_val;
1952             }
1953
1954             if(duplex == HALF_DUPLEX)
1955                 hw->fc = e1000_fc_none;
1956
1957             /* Now we call a subroutine to actually force the MAC
1958              * controller to use the correct flow control settings.
1959              */
1960             ret_val = e1000_force_mac_fc(hw);
1961             if(ret_val) {
1962                 DEBUGOUT("Error forcing flow control settings\n");
1963                 return ret_val;
1964             }
1965         } else {
1966             DEBUGOUT("Copper PHY and Auto Neg has not completed.\r\n");
1967         }
1968     }
1969     return E1000_SUCCESS;
1970 }
1971
1972 /******************************************************************************
1973  * Checks to see if the link status of the hardware has changed.
1974  *
1975  * hw - Struct containing variables accessed by shared code
1976  *
1977  * Called by any function that needs to check the link status of the adapter.
1978  *****************************************************************************/
1979 int32_t
1980 e1000_check_for_link(struct e1000_hw *hw)
1981 {
1982     uint32_t rxcw = 0;
1983     uint32_t ctrl;
1984     uint32_t status;
1985     uint32_t rctl;
1986     uint32_t signal = 0;
1987     int32_t ret_val;
1988     uint16_t phy_data;
1989
1990     DEBUGFUNC("e1000_check_for_link");
1991
1992     ctrl = E1000_READ_REG(hw, CTRL);
1993     status = E1000_READ_REG(hw, STATUS);
1994
1995     /* On adapters with a MAC newer than 82544, SW Defineable pin 1 will be
1996      * set when the optics detect a signal. On older adapters, it will be
1997      * cleared when there is a signal.  This applies to fiber media only.
1998      */
1999     if((hw->media_type == e1000_media_type_fiber) ||
2000        (hw->media_type == e1000_media_type_internal_serdes)) {
2001         rxcw = E1000_READ_REG(hw, RXCW);
2002
2003         if(hw->media_type == e1000_media_type_fiber) {
2004             signal = (hw->mac_type > e1000_82544) ? E1000_CTRL_SWDPIN1 : 0;
2005             if(status & E1000_STATUS_LU)
2006                 hw->get_link_status = FALSE;
2007         }
2008     }
2009
2010     /* If we have a copper PHY then we only want to go out to the PHY
2011      * registers to see if Auto-Neg has completed and/or if our link
2012      * status has changed.  The get_link_status flag will be set if we
2013      * receive a Link Status Change interrupt or we have Rx Sequence
2014      * Errors.
2015      */
2016     if((hw->media_type == e1000_media_type_copper) && hw->get_link_status) {
2017         /* First we want to see if the MII Status Register reports
2018          * link.  If so, then we want to get the current speed/duplex
2019          * of the PHY.
2020          * Read the register twice since the link bit is sticky.
2021          */
2022         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2023         if(ret_val)
2024             return ret_val;
2025         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2026         if(ret_val)
2027             return ret_val;
2028
2029         if(phy_data & MII_SR_LINK_STATUS) {
2030             hw->get_link_status = FALSE;
2031             /* Check if there was DownShift, must be checked immediately after
2032              * link-up */
2033             e1000_check_downshift(hw);
2034
2035         } else {
2036             /* No link detected */
2037             e1000_config_dsp_after_link_change(hw, FALSE);
2038             return 0;
2039         }
2040
2041         /* If we are forcing speed/duplex, then we simply return since
2042          * we have already determined whether we have link or not.
2043          */
2044         if(!hw->autoneg) return -E1000_ERR_CONFIG;
2045
2046         /* optimize the dsp settings for the igp phy */
2047         e1000_config_dsp_after_link_change(hw, TRUE);
2048
2049         /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
2050          * have Si on board that is 82544 or newer, Auto
2051          * Speed Detection takes care of MAC speed/duplex
2052          * configuration.  So we only need to configure Collision
2053          * Distance in the MAC.  Otherwise, we need to force
2054          * speed/duplex on the MAC to the current PHY speed/duplex
2055          * settings.
2056          */
2057         if(hw->mac_type >= e1000_82544)
2058             e1000_config_collision_dist(hw);
2059         else {
2060             ret_val = e1000_config_mac_to_phy(hw);
2061             if(ret_val) {
2062                 DEBUGOUT("Error configuring MAC to PHY settings\n");
2063                 return ret_val;
2064             }
2065         }
2066
2067         /* Configure Flow Control now that Auto-Neg has completed. First, we
2068          * need to restore the desired flow control settings because we may
2069          * have had to re-autoneg with a different link partner.
2070          */
2071         ret_val = e1000_config_fc_after_link_up(hw);
2072         if(ret_val) {
2073             DEBUGOUT("Error configuring flow control\n");
2074             return ret_val;
2075         }
2076
2077         /* At this point we know that we are on copper and we have
2078          * auto-negotiated link.  These are conditions for checking the link
2079          * partner capability register.  We use the link speed to determine if
2080          * TBI compatibility needs to be turned on or off.  If the link is not
2081          * at gigabit speed, then TBI compatibility is not needed.  If we are
2082          * at gigabit speed, we turn on TBI compatibility.
2083          */
2084         if(hw->tbi_compatibility_en) {
2085             uint16_t speed, duplex;
2086             e1000_get_speed_and_duplex(hw, &speed, &duplex);
2087             if(speed != SPEED_1000) {
2088                 /* If link speed is not set to gigabit speed, we do not need
2089                  * to enable TBI compatibility.
2090                  */
2091                 if(hw->tbi_compatibility_on) {
2092                     /* If we previously were in the mode, turn it off. */
2093                     rctl = E1000_READ_REG(hw, RCTL);
2094                     rctl &= ~E1000_RCTL_SBP;
2095                     E1000_WRITE_REG(hw, RCTL, rctl);
2096                     hw->tbi_compatibility_on = FALSE;
2097                 }
2098             } else {
2099                 /* If TBI compatibility is was previously off, turn it on. For
2100                  * compatibility with a TBI link partner, we will store bad
2101                  * packets. Some frames have an additional byte on the end and
2102                  * will look like CRC errors to to the hardware.
2103                  */
2104                 if(!hw->tbi_compatibility_on) {
2105                     hw->tbi_compatibility_on = TRUE;
2106                     rctl = E1000_READ_REG(hw, RCTL);
2107                     rctl |= E1000_RCTL_SBP;
2108                     E1000_WRITE_REG(hw, RCTL, rctl);
2109                 }
2110             }
2111         }
2112     }
2113     /* If we don't have link (auto-negotiation failed or link partner cannot
2114      * auto-negotiate), the cable is plugged in (we have signal), and our
2115      * link partner is not trying to auto-negotiate with us (we are receiving
2116      * idles or data), we need to force link up. We also need to give
2117      * auto-negotiation time to complete, in case the cable was just plugged
2118      * in. The autoneg_failed flag does this.
2119      */
2120     else if((((hw->media_type == e1000_media_type_fiber) &&
2121               ((ctrl & E1000_CTRL_SWDPIN1) == signal)) ||
2122              (hw->media_type == e1000_media_type_internal_serdes)) &&
2123             (!(status & E1000_STATUS_LU)) &&
2124             (!(rxcw & E1000_RXCW_C))) {
2125         if(hw->autoneg_failed == 0) {
2126             hw->autoneg_failed = 1;
2127             return 0;
2128         }
2129         DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");
2130
2131         /* Disable auto-negotiation in the TXCW register */
2132         E1000_WRITE_REG(hw, TXCW, (hw->txcw & ~E1000_TXCW_ANE));
2133
2134         /* Force link-up and also force full-duplex. */
2135         ctrl = E1000_READ_REG(hw, CTRL);
2136         ctrl |= (E1000_CTRL_SLU | E1000_CTRL_FD);
2137         E1000_WRITE_REG(hw, CTRL, ctrl);
2138
2139         /* Configure Flow Control after forcing link up. */
2140         ret_val = e1000_config_fc_after_link_up(hw);
2141         if(ret_val) {
2142             DEBUGOUT("Error configuring flow control\n");
2143             return ret_val;
2144         }
2145     }
2146     /* If we are forcing link and we are receiving /C/ ordered sets, re-enable
2147      * auto-negotiation in the TXCW register and disable forced link in the
2148      * Device Control register in an attempt to auto-negotiate with our link
2149      * partner.
2150      */
2151     else if(((hw->media_type == e1000_media_type_fiber) ||
2152              (hw->media_type == e1000_media_type_internal_serdes)) &&
2153             (ctrl & E1000_CTRL_SLU) && (rxcw & E1000_RXCW_C)) {
2154         DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\r\n");
2155         E1000_WRITE_REG(hw, TXCW, hw->txcw);
2156         E1000_WRITE_REG(hw, CTRL, (ctrl & ~E1000_CTRL_SLU));
2157
2158         hw->serdes_link_down = FALSE;
2159     }
2160     /* If we force link for non-auto-negotiation switch, check link status
2161      * based on MAC synchronization for internal serdes media type.
2162      */
2163     else if((hw->media_type == e1000_media_type_internal_serdes) &&
2164             !(E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
2165         /* SYNCH bit and IV bit are sticky. */
2166         udelay(10);
2167         if(E1000_RXCW_SYNCH & E1000_READ_REG(hw, RXCW)) {
2168             if(!(rxcw & E1000_RXCW_IV)) {
2169                 hw->serdes_link_down = FALSE;
2170                 DEBUGOUT("SERDES: Link is up.\n");
2171             }
2172         } else {
2173             hw->serdes_link_down = TRUE;
2174             DEBUGOUT("SERDES: Link is down.\n");
2175         }
2176     }
2177     if((hw->media_type == e1000_media_type_internal_serdes) &&
2178        (E1000_TXCW_ANE & E1000_READ_REG(hw, TXCW))) {
2179         hw->serdes_link_down = !(E1000_STATUS_LU & E1000_READ_REG(hw, STATUS));
2180     }
2181     return E1000_SUCCESS;
2182 }
2183
2184 /******************************************************************************
2185  * Detects the current speed and duplex settings of the hardware.
2186  *
2187  * hw - Struct containing variables accessed by shared code
2188  * speed - Speed of the connection
2189  * duplex - Duplex setting of the connection
2190  *****************************************************************************/
2191 int32_t
2192 e1000_get_speed_and_duplex(struct e1000_hw *hw,
2193                            uint16_t *speed,
2194                            uint16_t *duplex)
2195 {
2196     uint32_t status;
2197     int32_t ret_val;
2198     uint16_t phy_data;
2199
2200     DEBUGFUNC("e1000_get_speed_and_duplex");
2201
2202     if(hw->mac_type >= e1000_82543) {
2203         status = E1000_READ_REG(hw, STATUS);
2204         if(status & E1000_STATUS_SPEED_1000) {
2205             *speed = SPEED_1000;
2206             DEBUGOUT("1000 Mbs, ");
2207         } else if(status & E1000_STATUS_SPEED_100) {
2208             *speed = SPEED_100;
2209             DEBUGOUT("100 Mbs, ");
2210         } else {
2211             *speed = SPEED_10;
2212             DEBUGOUT("10 Mbs, ");
2213         }
2214
2215         if(status & E1000_STATUS_FD) {
2216             *duplex = FULL_DUPLEX;
2217             DEBUGOUT("Full Duplex\r\n");
2218         } else {
2219             *duplex = HALF_DUPLEX;
2220             DEBUGOUT(" Half Duplex\r\n");
2221         }
2222     } else {
2223         DEBUGOUT("1000 Mbs, Full Duplex\r\n");
2224         *speed = SPEED_1000;
2225         *duplex = FULL_DUPLEX;
2226     }
2227
2228     /* IGP01 PHY may advertise full duplex operation after speed downgrade even
2229      * if it is operating at half duplex.  Here we set the duplex settings to
2230      * match the duplex in the link partner's capabilities.
2231      */
2232     if(hw->phy_type == e1000_phy_igp && hw->speed_downgraded) {
2233         ret_val = e1000_read_phy_reg(hw, PHY_AUTONEG_EXP, &phy_data);
2234         if(ret_val)
2235             return ret_val;
2236
2237         if(!(phy_data & NWAY_ER_LP_NWAY_CAPS))
2238             *duplex = HALF_DUPLEX;
2239         else {
2240             ret_val = e1000_read_phy_reg(hw, PHY_LP_ABILITY, &phy_data);
2241             if(ret_val)
2242                 return ret_val;
2243             if((*speed == SPEED_100 && !(phy_data & NWAY_LPAR_100TX_FD_CAPS)) ||
2244                (*speed == SPEED_10 && !(phy_data & NWAY_LPAR_10T_FD_CAPS)))
2245                 *duplex = HALF_DUPLEX;
2246         }
2247     }
2248
2249     return E1000_SUCCESS;
2250 }
2251
2252 /******************************************************************************
2253 * Blocks until autoneg completes or times out (~4.5 seconds)
2254 *
2255 * hw - Struct containing variables accessed by shared code
2256 ******************************************************************************/
2257 int32_t
2258 e1000_wait_autoneg(struct e1000_hw *hw)
2259 {
2260     int32_t ret_val;
2261     uint16_t i;
2262     uint16_t phy_data;
2263
2264     DEBUGFUNC("e1000_wait_autoneg");
2265     DEBUGOUT("Waiting for Auto-Neg to complete.\n");
2266
2267     /* We will wait for autoneg to complete or 4.5 seconds to expire. */
2268     for(i = PHY_AUTO_NEG_TIME; i > 0; i--) {
2269         /* Read the MII Status Register and wait for Auto-Neg
2270          * Complete bit to be set.
2271          */
2272         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2273         if(ret_val)
2274             return ret_val;
2275         ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2276         if(ret_val)
2277             return ret_val;
2278         if(phy_data & MII_SR_AUTONEG_COMPLETE) {
2279             return E1000_SUCCESS;
2280         }
2281         msec_delay(100);
2282     }
2283     return E1000_SUCCESS;
2284 }
2285
2286 /******************************************************************************
2287 * Raises the Management Data Clock
2288 *
2289 * hw - Struct containing variables accessed by shared code
2290 * ctrl - Device control register's current value
2291 ******************************************************************************/
2292 static void
2293 e1000_raise_mdi_clk(struct e1000_hw *hw,
2294                     uint32_t *ctrl)
2295 {
2296     /* Raise the clock input to the Management Data Clock (by setting the MDC
2297      * bit), and then delay 10 microseconds.
2298      */
2299     E1000_WRITE_REG(hw, CTRL, (*ctrl | E1000_CTRL_MDC));
2300     E1000_WRITE_FLUSH(hw);
2301     udelay(10);
2302 }
2303
2304 /******************************************************************************
2305 * Lowers the Management Data Clock
2306 *
2307 * hw - Struct containing variables accessed by shared code
2308 * ctrl - Device control register's current value
2309 ******************************************************************************/
2310 static void
2311 e1000_lower_mdi_clk(struct e1000_hw *hw,
2312                     uint32_t *ctrl)
2313 {
2314     /* Lower the clock input to the Management Data Clock (by clearing the MDC
2315      * bit), and then delay 10 microseconds.
2316      */
2317     E1000_WRITE_REG(hw, CTRL, (*ctrl & ~E1000_CTRL_MDC));
2318     E1000_WRITE_FLUSH(hw);
2319     udelay(10);
2320 }
2321
2322 /******************************************************************************
2323 * Shifts data bits out to the PHY
2324 *
2325 * hw - Struct containing variables accessed by shared code
2326 * data - Data to send out to the PHY
2327 * count - Number of bits to shift out
2328 *
2329 * Bits are shifted out in MSB to LSB order.
2330 ******************************************************************************/
2331 static void
2332 e1000_shift_out_mdi_bits(struct e1000_hw *hw,
2333                          uint32_t data,
2334                          uint16_t count)
2335 {
2336     uint32_t ctrl;
2337     uint32_t mask;
2338
2339     /* We need to shift "count" number of bits out to the PHY. So, the value
2340      * in the "data" parameter will be shifted out to the PHY one bit at a
2341      * time. In order to do this, "data" must be broken down into bits.
2342      */
2343     mask = 0x01;
2344     mask <<= (count - 1);
2345
2346     ctrl = E1000_READ_REG(hw, CTRL);
2347
2348     /* Set MDIO_DIR and MDC_DIR direction bits to be used as output pins. */
2349     ctrl |= (E1000_CTRL_MDIO_DIR | E1000_CTRL_MDC_DIR);
2350
2351     while(mask) {
2352         /* A "1" is shifted out to the PHY by setting the MDIO bit to "1" and
2353          * then raising and lowering the Management Data Clock. A "0" is
2354          * shifted out to the PHY by setting the MDIO bit to "0" and then
2355          * raising and lowering the clock.
2356          */
2357         if(data & mask) ctrl |= E1000_CTRL_MDIO;
2358         else ctrl &= ~E1000_CTRL_MDIO;
2359
2360         E1000_WRITE_REG(hw, CTRL, ctrl);
2361         E1000_WRITE_FLUSH(hw);
2362
2363         udelay(10);
2364
2365         e1000_raise_mdi_clk(hw, &ctrl);
2366         e1000_lower_mdi_clk(hw, &ctrl);
2367
2368         mask = mask >> 1;
2369     }
2370 }
2371
2372 /******************************************************************************
2373 * Shifts data bits in from the PHY
2374 *
2375 * hw - Struct containing variables accessed by shared code
2376 *
2377 * Bits are shifted in in MSB to LSB order.
2378 ******************************************************************************/
2379 static uint16_t
2380 e1000_shift_in_mdi_bits(struct e1000_hw *hw)
2381 {
2382     uint32_t ctrl;
2383     uint16_t data = 0;
2384     uint8_t i;
2385
2386     /* In order to read a register from the PHY, we need to shift in a total
2387      * of 18 bits from the PHY. The first two bit (turnaround) times are used
2388      * to avoid contention on the MDIO pin when a read operation is performed.
2389      * These two bits are ignored by us and thrown away. Bits are "shifted in"
2390      * by raising the input to the Management Data Clock (setting the MDC bit),
2391      * and then reading the value of the MDIO bit.
2392      */
2393     ctrl = E1000_READ_REG(hw, CTRL);
2394
2395     /* Clear MDIO_DIR (SWDPIO1) to indicate this bit is to be used as input. */
2396     ctrl &= ~E1000_CTRL_MDIO_DIR;
2397     ctrl &= ~E1000_CTRL_MDIO;
2398
2399     E1000_WRITE_REG(hw, CTRL, ctrl);
2400     E1000_WRITE_FLUSH(hw);
2401
2402     /* Raise and Lower the clock before reading in the data. This accounts for
2403      * the turnaround bits. The first clock occurred when we clocked out the
2404      * last bit of the Register Address.
2405      */
2406     e1000_raise_mdi_clk(hw, &ctrl);
2407     e1000_lower_mdi_clk(hw, &ctrl);
2408
2409     for(data = 0, i = 0; i < 16; i++) {
2410         data = data << 1;
2411         e1000_raise_mdi_clk(hw, &ctrl);
2412         ctrl = E1000_READ_REG(hw, CTRL);
2413         /* Check to see if we shifted in a "1". */
2414         if(ctrl & E1000_CTRL_MDIO) data |= 1;
2415         e1000_lower_mdi_clk(hw, &ctrl);
2416     }
2417
2418     e1000_raise_mdi_clk(hw, &ctrl);
2419     e1000_lower_mdi_clk(hw, &ctrl);
2420
2421     return data;
2422 }
2423
2424 /*****************************************************************************
2425 * Reads the value from a PHY register, if the value is on a specific non zero
2426 * page, sets the page first.
2427 * hw - Struct containing variables accessed by shared code
2428 * reg_addr - address of the PHY register to read
2429 ******************************************************************************/
2430 int32_t
2431 e1000_read_phy_reg(struct e1000_hw *hw,
2432                    uint32_t reg_addr,
2433                    uint16_t *phy_data)
2434 {
2435     uint32_t ret_val;
2436
2437     DEBUGFUNC("e1000_read_phy_reg");
2438
2439     if(hw->phy_type == e1000_phy_igp &&
2440        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2441         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2442                                          (uint16_t)reg_addr);
2443         if(ret_val)
2444             return ret_val;
2445     }
2446
2447     ret_val = e1000_read_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT & reg_addr,
2448                                     phy_data);
2449
2450     return ret_val;
2451 }
2452
2453 int32_t
2454 e1000_read_phy_reg_ex(struct e1000_hw *hw,
2455                       uint32_t reg_addr,
2456                       uint16_t *phy_data)
2457 {
2458     uint32_t i;
2459     uint32_t mdic = 0;
2460     const uint32_t phy_addr = 1;
2461
2462     DEBUGFUNC("e1000_read_phy_reg_ex");
2463
2464     if(reg_addr > MAX_PHY_REG_ADDRESS) {
2465         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2466         return -E1000_ERR_PARAM;
2467     }
2468
2469     if(hw->mac_type > e1000_82543) {
2470         /* Set up Op-code, Phy Address, and register address in the MDI
2471          * Control register.  The MAC will take care of interfacing with the
2472          * PHY to retrieve the desired data.
2473          */
2474         mdic = ((reg_addr << E1000_MDIC_REG_SHIFT) |
2475                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
2476                 (E1000_MDIC_OP_READ));
2477
2478         E1000_WRITE_REG(hw, MDIC, mdic);
2479
2480         /* Poll the ready bit to see if the MDI read completed */
2481         for(i = 0; i < 64; i++) {
2482             udelay(50);
2483             mdic = E1000_READ_REG(hw, MDIC);
2484             if(mdic & E1000_MDIC_READY) break;
2485         }
2486         if(!(mdic & E1000_MDIC_READY)) {
2487             DEBUGOUT("MDI Read did not complete\n");
2488             return -E1000_ERR_PHY;
2489         }
2490         if(mdic & E1000_MDIC_ERROR) {
2491             DEBUGOUT("MDI Error\n");
2492             return -E1000_ERR_PHY;
2493         }
2494         *phy_data = (uint16_t) mdic;
2495     } else {
2496         /* We must first send a preamble through the MDIO pin to signal the
2497          * beginning of an MII instruction.  This is done by sending 32
2498          * consecutive "1" bits.
2499          */
2500         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2501
2502         /* Now combine the next few fields that are required for a read
2503          * operation.  We use this method instead of calling the
2504          * e1000_shift_out_mdi_bits routine five different times. The format of
2505          * a MII read instruction consists of a shift out of 14 bits and is
2506          * defined as follows:
2507          *    <Preamble><SOF><Op Code><Phy Addr><Reg Addr>
2508          * followed by a shift in of 18 bits.  This first two bits shifted in
2509          * are TurnAround bits used to avoid contention on the MDIO pin when a
2510          * READ operation is performed.  These two bits are thrown away
2511          * followed by a shift in of 16 bits which contains the desired data.
2512          */
2513         mdic = ((reg_addr) | (phy_addr << 5) |
2514                 (PHY_OP_READ << 10) | (PHY_SOF << 12));
2515
2516         e1000_shift_out_mdi_bits(hw, mdic, 14);
2517
2518         /* Now that we've shifted out the read command to the MII, we need to
2519          * "shift in" the 16-bit value (18 total bits) of the requested PHY
2520          * register address.
2521          */
2522         *phy_data = e1000_shift_in_mdi_bits(hw);
2523     }
2524     return E1000_SUCCESS;
2525 }
2526
2527 /******************************************************************************
2528 * Writes a value to a PHY register
2529 *
2530 * hw - Struct containing variables accessed by shared code
2531 * reg_addr - address of the PHY register to write
2532 * data - data to write to the PHY
2533 ******************************************************************************/
2534 int32_t
2535 e1000_write_phy_reg(struct e1000_hw *hw,
2536                     uint32_t reg_addr,
2537                     uint16_t phy_data)
2538 {
2539     uint32_t ret_val;
2540
2541     DEBUGFUNC("e1000_write_phy_reg");
2542
2543     if(hw->phy_type == e1000_phy_igp &&
2544        (reg_addr > MAX_PHY_MULTI_PAGE_REG)) {
2545         ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT,
2546                                          (uint16_t)reg_addr);
2547         if(ret_val)
2548             return ret_val;
2549     }
2550
2551     ret_val = e1000_write_phy_reg_ex(hw, IGP01E1000_PHY_PAGE_SELECT & reg_addr,
2552                                      phy_data);
2553
2554     return ret_val;
2555 }
2556
2557 int32_t
2558 e1000_write_phy_reg_ex(struct e1000_hw *hw,
2559                     uint32_t reg_addr,
2560                     uint16_t phy_data)
2561 {
2562     uint32_t i;
2563     uint32_t mdic = 0;
2564     const uint32_t phy_addr = 1;
2565
2566     DEBUGFUNC("e1000_write_phy_reg_ex");
2567
2568     if(reg_addr > MAX_PHY_REG_ADDRESS) {
2569         DEBUGOUT1("PHY Address %d is out of range\n", reg_addr);
2570         return -E1000_ERR_PARAM;
2571     }
2572
2573     if(hw->mac_type > e1000_82543) {
2574         /* Set up Op-code, Phy Address, register address, and data intended
2575          * for the PHY register in the MDI Control register.  The MAC will take
2576          * care of interfacing with the PHY to send the desired data.
2577          */
2578         mdic = (((uint32_t) phy_data) |
2579                 (reg_addr << E1000_MDIC_REG_SHIFT) |
2580                 (phy_addr << E1000_MDIC_PHY_SHIFT) |
2581                 (E1000_MDIC_OP_WRITE));
2582
2583         E1000_WRITE_REG(hw, MDIC, mdic);
2584
2585         /* Poll the ready bit to see if the MDI read completed */
2586         for(i = 0; i < 640; i++) {
2587             udelay(5);
2588             mdic = E1000_READ_REG(hw, MDIC);
2589             if(mdic & E1000_MDIC_READY) break;
2590         }
2591         if(!(mdic & E1000_MDIC_READY)) {
2592             DEBUGOUT("MDI Write did not complete\n");
2593             return -E1000_ERR_PHY;
2594         }
2595     } else {
2596         /* We'll need to use the SW defined pins to shift the write command
2597          * out to the PHY. We first send a preamble to the PHY to signal the
2598          * beginning of the MII instruction.  This is done by sending 32
2599          * consecutive "1" bits.
2600          */
2601         e1000_shift_out_mdi_bits(hw, PHY_PREAMBLE, PHY_PREAMBLE_SIZE);
2602
2603         /* Now combine the remaining required fields that will indicate a
2604          * write operation. We use this method instead of calling the
2605          * e1000_shift_out_mdi_bits routine for each field in the command. The
2606          * format of a MII write instruction is as follows:
2607          * <Preamble><SOF><Op Code><Phy Addr><Reg Addr><Turnaround><Data>.
2608          */
2609         mdic = ((PHY_TURNAROUND) | (reg_addr << 2) | (phy_addr << 7) |
2610                 (PHY_OP_WRITE << 12) | (PHY_SOF << 14));
2611         mdic <<= 16;
2612         mdic |= (uint32_t) phy_data;
2613
2614         e1000_shift_out_mdi_bits(hw, mdic, 32);
2615     }
2616
2617     return E1000_SUCCESS;
2618 }
2619
2620 /******************************************************************************
2621 * Returns the PHY to the power-on reset state
2622 *
2623 * hw - Struct containing variables accessed by shared code
2624 ******************************************************************************/
2625 void
2626 e1000_phy_hw_reset(struct e1000_hw *hw)
2627 {
2628     uint32_t ctrl, ctrl_ext;
2629     uint32_t led_ctrl;
2630
2631     DEBUGFUNC("e1000_phy_hw_reset");
2632
2633     DEBUGOUT("Resetting Phy...\n");
2634
2635     if(hw->mac_type > e1000_82543) {
2636         /* Read the device control register and assert the E1000_CTRL_PHY_RST
2637          * bit. Then, take it out of reset.
2638          */
2639         ctrl = E1000_READ_REG(hw, CTRL);
2640         E1000_WRITE_REG(hw, CTRL, ctrl | E1000_CTRL_PHY_RST);
2641         E1000_WRITE_FLUSH(hw);
2642         msec_delay(10);
2643         E1000_WRITE_REG(hw, CTRL, ctrl);
2644         E1000_WRITE_FLUSH(hw);
2645     } else {
2646         /* Read the Extended Device Control Register, assert the PHY_RESET_DIR
2647          * bit to put the PHY into reset. Then, take it out of reset.
2648          */
2649         ctrl_ext = E1000_READ_REG(hw, CTRL_EXT);
2650         ctrl_ext |= E1000_CTRL_EXT_SDP4_DIR;
2651         ctrl_ext &= ~E1000_CTRL_EXT_SDP4_DATA;
2652         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2653         E1000_WRITE_FLUSH(hw);
2654         msec_delay(10);
2655         ctrl_ext |= E1000_CTRL_EXT_SDP4_DATA;
2656         E1000_WRITE_REG(hw, CTRL_EXT, ctrl_ext);
2657         E1000_WRITE_FLUSH(hw);
2658     }
2659     udelay(150);
2660
2661     if((hw->mac_type == e1000_82541) || (hw->mac_type == e1000_82547)) {
2662         /* Configure activity LED after PHY reset */
2663         led_ctrl = E1000_READ_REG(hw, LEDCTL);
2664         led_ctrl &= IGP_ACTIVITY_LED_MASK;
2665         led_ctrl |= (IGP_ACTIVITY_LED_ENABLE | IGP_LED3_MODE);
2666         E1000_WRITE_REG(hw, LEDCTL, led_ctrl);
2667     }
2668 }
2669
2670 /******************************************************************************
2671 * Resets the PHY
2672 *
2673 * hw - Struct containing variables accessed by shared code
2674 *
2675 * Sets bit 15 of the MII Control regiser
2676 ******************************************************************************/
2677 int32_t
2678 e1000_phy_reset(struct e1000_hw *hw)
2679 {
2680     int32_t ret_val;
2681     uint16_t phy_data;
2682
2683     DEBUGFUNC("e1000_phy_reset");
2684
2685     if(hw->mac_type != e1000_82541_rev_2) {
2686         ret_val = e1000_read_phy_reg(hw, PHY_CTRL, &phy_data);
2687         if(ret_val)
2688             return ret_val;
2689
2690         phy_data |= MII_CR_RESET;
2691         ret_val = e1000_write_phy_reg(hw, PHY_CTRL, phy_data);
2692         if(ret_val)
2693             return ret_val;
2694
2695         udelay(1);
2696     } else e1000_phy_hw_reset(hw);
2697
2698     if(hw->phy_type == e1000_phy_igp)
2699         e1000_phy_init_script(hw);
2700
2701     return E1000_SUCCESS;
2702 }
2703
2704 /******************************************************************************
2705 * Probes the expected PHY address for known PHY IDs
2706 *
2707 * hw - Struct containing variables accessed by shared code
2708 ******************************************************************************/
2709 int32_t
2710 e1000_detect_gig_phy(struct e1000_hw *hw)
2711 {
2712     int32_t phy_init_status, ret_val;
2713     uint16_t phy_id_high, phy_id_low;
2714     boolean_t match = FALSE;
2715
2716     DEBUGFUNC("e1000_detect_gig_phy");
2717
2718     /* Read the PHY ID Registers to identify which PHY is onboard. */
2719     ret_val = e1000_read_phy_reg(hw, PHY_ID1, &phy_id_high);
2720     if(ret_val)
2721         return ret_val;
2722
2723     hw->phy_id = (uint32_t) (phy_id_high << 16);
2724     udelay(20);
2725     ret_val = e1000_read_phy_reg(hw, PHY_ID2, &phy_id_low);
2726     if(ret_val)
2727         return ret_val;
2728
2729     hw->phy_id |= (uint32_t) (phy_id_low & PHY_REVISION_MASK);
2730     hw->phy_revision = (uint32_t) phy_id_low & ~PHY_REVISION_MASK;
2731
2732     switch(hw->mac_type) {
2733     case e1000_82543:
2734         if(hw->phy_id == M88E1000_E_PHY_ID) match = TRUE;
2735         break;
2736     case e1000_82544:
2737         if(hw->phy_id == M88E1000_I_PHY_ID) match = TRUE;
2738         break;
2739     case e1000_82540:
2740     case e1000_82545:
2741     case e1000_82545_rev_3:
2742     case e1000_82546:
2743     case e1000_82546_rev_3:
2744         if(hw->phy_id == M88E1011_I_PHY_ID) match = TRUE;
2745         break;
2746     case e1000_82541:
2747     case e1000_82541_rev_2:
2748     case e1000_82547:
2749     case e1000_82547_rev_2:
2750         if(hw->phy_id == IGP01E1000_I_PHY_ID) match = TRUE;
2751         break;
2752     default:
2753         DEBUGOUT1("Invalid MAC type %d\n", hw->mac_type);
2754         return -E1000_ERR_CONFIG;
2755     }
2756     phy_init_status = e1000_set_phy_type(hw);
2757
2758     if ((match) && (phy_init_status == E1000_SUCCESS)) {
2759         DEBUGOUT1("PHY ID 0x%X detected\n", hw->phy_id);
2760         return E1000_SUCCESS;
2761     }
2762     DEBUGOUT1("Invalid PHY ID 0x%X\n", hw->phy_id);
2763     return -E1000_ERR_PHY;
2764 }
2765
2766 /******************************************************************************
2767 * Resets the PHY's DSP
2768 *
2769 * hw - Struct containing variables accessed by shared code
2770 ******************************************************************************/
2771 static int32_t
2772 e1000_phy_reset_dsp(struct e1000_hw *hw)
2773 {
2774     int32_t ret_val;
2775     DEBUGFUNC("e1000_phy_reset_dsp");
2776
2777     do {
2778         ret_val = e1000_write_phy_reg(hw, 29, 0x001d);
2779         if(ret_val) break;
2780         ret_val = e1000_write_phy_reg(hw, 30, 0x00c1);
2781         if(ret_val) break;
2782         ret_val = e1000_write_phy_reg(hw, 30, 0x0000);
2783         if(ret_val) break;
2784         ret_val = E1000_SUCCESS;
2785     } while(0);
2786
2787     return ret_val;
2788 }
2789
2790 /******************************************************************************
2791 * Get PHY information from various PHY registers for igp PHY only.
2792 *
2793 * hw - Struct containing variables accessed by shared code
2794 * phy_info - PHY information structure
2795 ******************************************************************************/
2796 int32_t
2797 e1000_phy_igp_get_info(struct e1000_hw *hw,
2798                        struct e1000_phy_info *phy_info)
2799 {
2800     int32_t ret_val;
2801     uint16_t phy_data, polarity, min_length, max_length, average;
2802
2803     DEBUGFUNC("e1000_phy_igp_get_info");
2804
2805     /* The downshift status is checked only once, after link is established,
2806      * and it stored in the hw->speed_downgraded parameter. */
2807     phy_info->downshift = hw->speed_downgraded;
2808
2809     /* IGP01E1000 does not need to support it. */
2810     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_normal;
2811
2812     /* IGP01E1000 always correct polarity reversal */
2813     phy_info->polarity_correction = e1000_polarity_reversal_enabled;
2814
2815     /* Check polarity status */
2816     ret_val = e1000_check_polarity(hw, &polarity);
2817     if(ret_val)
2818         return ret_val;
2819
2820     phy_info->cable_polarity = polarity;
2821
2822     ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS, &phy_data);
2823     if(ret_val)
2824         return ret_val;
2825
2826     phy_info->mdix_mode = (phy_data & IGP01E1000_PSSR_MDIX) >>
2827                           IGP01E1000_PSSR_MDIX_SHIFT;
2828
2829     if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
2830        IGP01E1000_PSSR_SPEED_1000MBPS) {
2831         /* Local/Remote Receiver Information are only valid at 1000 Mbps */
2832         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
2833         if(ret_val)
2834             return ret_val;
2835
2836         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
2837                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
2838         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
2839                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
2840
2841         /* Get cable length */
2842         ret_val = e1000_get_cable_length(hw, &min_length, &max_length);
2843         if(ret_val)
2844             return ret_val;
2845
2846         /* transalte to old method */
2847         average = (max_length + min_length) / 2;
2848
2849         if(average <= e1000_igp_cable_length_50)
2850             phy_info->cable_length = e1000_cable_length_50;
2851         else if(average <= e1000_igp_cable_length_80)
2852             phy_info->cable_length = e1000_cable_length_50_80;
2853         else if(average <= e1000_igp_cable_length_110)
2854             phy_info->cable_length = e1000_cable_length_80_110;
2855         else if(average <= e1000_igp_cable_length_140)
2856             phy_info->cable_length = e1000_cable_length_110_140;
2857         else
2858             phy_info->cable_length = e1000_cable_length_140;
2859     }
2860
2861     return E1000_SUCCESS;
2862 }
2863
2864 /******************************************************************************
2865 * Get PHY information from various PHY registers fot m88 PHY only.
2866 *
2867 * hw - Struct containing variables accessed by shared code
2868 * phy_info - PHY information structure
2869 ******************************************************************************/
2870 int32_t
2871 e1000_phy_m88_get_info(struct e1000_hw *hw,
2872                        struct e1000_phy_info *phy_info)
2873 {
2874     int32_t ret_val;
2875     uint16_t phy_data, polarity;
2876
2877     DEBUGFUNC("e1000_phy_m88_get_info");
2878
2879     /* The downshift status is checked only once, after link is established,
2880      * and it stored in the hw->speed_downgraded parameter. */
2881     phy_info->downshift = hw->speed_downgraded;
2882
2883     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_CTRL, &phy_data);
2884     if(ret_val)
2885         return ret_val;
2886
2887     phy_info->extended_10bt_distance =
2888         (phy_data & M88E1000_PSCR_10BT_EXT_DIST_ENABLE) >>
2889         M88E1000_PSCR_10BT_EXT_DIST_ENABLE_SHIFT;
2890     phy_info->polarity_correction =
2891         (phy_data & M88E1000_PSCR_POLARITY_REVERSAL) >>
2892         M88E1000_PSCR_POLARITY_REVERSAL_SHIFT;
2893
2894     /* Check polarity status */
2895     ret_val = e1000_check_polarity(hw, &polarity);
2896     if(ret_val)
2897         return ret_val;
2898
2899     phy_info->cable_polarity = polarity;
2900
2901     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS, &phy_data);
2902     if(ret_val)
2903         return ret_val;
2904
2905     phy_info->mdix_mode = (phy_data & M88E1000_PSSR_MDIX) >>
2906                           M88E1000_PSSR_MDIX_SHIFT;
2907
2908     if(phy_data & M88E1000_PSSR_1000MBS) {
2909         /* Cable Length Estimation and Local/Remote Receiver Informatoion
2910          * are only valid at 1000 Mbps
2911          */
2912         phy_info->cable_length = ((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
2913                                   M88E1000_PSSR_CABLE_LENGTH_SHIFT);
2914
2915         ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS, &phy_data);
2916         if(ret_val)
2917             return ret_val;
2918
2919         phy_info->local_rx = (phy_data & SR_1000T_LOCAL_RX_STATUS) >>
2920                              SR_1000T_LOCAL_RX_STATUS_SHIFT;
2921
2922         phy_info->remote_rx = (phy_data & SR_1000T_REMOTE_RX_STATUS) >>
2923                               SR_1000T_REMOTE_RX_STATUS_SHIFT;
2924     }
2925
2926     return E1000_SUCCESS;
2927 }
2928
2929 /******************************************************************************
2930 * Get PHY information from various PHY registers
2931 *
2932 * hw - Struct containing variables accessed by shared code
2933 * phy_info - PHY information structure
2934 ******************************************************************************/
2935 int32_t
2936 e1000_phy_get_info(struct e1000_hw *hw,
2937                    struct e1000_phy_info *phy_info)
2938 {
2939     int32_t ret_val;
2940     uint16_t phy_data;
2941
2942     DEBUGFUNC("e1000_phy_get_info");
2943
2944     phy_info->cable_length = e1000_cable_length_undefined;
2945     phy_info->extended_10bt_distance = e1000_10bt_ext_dist_enable_undefined;
2946     phy_info->cable_polarity = e1000_rev_polarity_undefined;
2947     phy_info->downshift = e1000_downshift_undefined;
2948     phy_info->polarity_correction = e1000_polarity_reversal_undefined;
2949     phy_info->mdix_mode = e1000_auto_x_mode_undefined;
2950     phy_info->local_rx = e1000_1000t_rx_status_undefined;
2951     phy_info->remote_rx = e1000_1000t_rx_status_undefined;
2952
2953     if(hw->media_type != e1000_media_type_copper) {
2954         DEBUGOUT("PHY info is only valid for copper media\n");
2955         return -E1000_ERR_CONFIG;
2956     }
2957
2958     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2959     if(ret_val)
2960         return ret_val;
2961
2962     ret_val = e1000_read_phy_reg(hw, PHY_STATUS, &phy_data);
2963     if(ret_val)
2964         return ret_val;
2965
2966     if((phy_data & MII_SR_LINK_STATUS) != MII_SR_LINK_STATUS) {
2967         DEBUGOUT("PHY info is only valid if link is up\n");
2968         return -E1000_ERR_CONFIG;
2969     }
2970
2971     if(hw->phy_type == e1000_phy_igp)
2972         return e1000_phy_igp_get_info(hw, phy_info);
2973     else
2974         return e1000_phy_m88_get_info(hw, phy_info);
2975 }
2976
2977 int32_t
2978 e1000_validate_mdi_setting(struct e1000_hw *hw)
2979 {
2980     DEBUGFUNC("e1000_validate_mdi_settings");
2981
2982     if(!hw->autoneg && (hw->mdix == 0 || hw->mdix == 3)) {
2983         DEBUGOUT("Invalid MDI setting detected\n");
2984         hw->mdix = 1;
2985         return -E1000_ERR_CONFIG;
2986     }
2987     return E1000_SUCCESS;
2988 }
2989
2990
2991 /******************************************************************************
2992  * Sets up eeprom variables in the hw struct.  Must be called after mac_type
2993  * is configured.
2994  *
2995  * hw - Struct containing variables accessed by shared code
2996  *****************************************************************************/
2997 void
2998 e1000_init_eeprom_params(struct e1000_hw *hw)
2999 {
3000     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3001     uint32_t eecd = E1000_READ_REG(hw, EECD);
3002     uint16_t eeprom_size;
3003
3004     DEBUGFUNC("e1000_init_eeprom_params");
3005
3006     switch (hw->mac_type) {
3007     case e1000_82542_rev2_0:
3008     case e1000_82542_rev2_1:
3009     case e1000_82543:
3010     case e1000_82544:
3011         eeprom->type = e1000_eeprom_microwire;
3012         eeprom->word_size = 64;
3013         eeprom->opcode_bits = 3;
3014         eeprom->address_bits = 6;
3015         eeprom->delay_usec = 50;
3016         break;
3017     case e1000_82540:
3018     case e1000_82545:
3019     case e1000_82545_rev_3:
3020     case e1000_82546:
3021     case e1000_82546_rev_3:
3022         eeprom->type = e1000_eeprom_microwire;
3023         eeprom->opcode_bits = 3;
3024         eeprom->delay_usec = 50;
3025         if(eecd & E1000_EECD_SIZE) {
3026             eeprom->word_size = 256;
3027             eeprom->address_bits = 8;
3028         } else {
3029             eeprom->word_size = 64;
3030             eeprom->address_bits = 6;
3031         }
3032         break;
3033     case e1000_82541:
3034     case e1000_82541_rev_2:
3035     case e1000_82547:
3036     case e1000_82547_rev_2:
3037         if (eecd & E1000_EECD_TYPE) {
3038             eeprom->type = e1000_eeprom_spi;
3039             eeprom->opcode_bits = 8;
3040             eeprom->delay_usec = 1;
3041             if (eecd & E1000_EECD_ADDR_BITS) {
3042                 eeprom->page_size = 32;
3043                 eeprom->address_bits = 16;
3044             } else {
3045                 eeprom->page_size = 8;
3046                 eeprom->address_bits = 8;
3047             }
3048         } else {
3049             eeprom->type = e1000_eeprom_microwire;
3050             eeprom->opcode_bits = 3;
3051             eeprom->delay_usec = 50;
3052             if (eecd & E1000_EECD_ADDR_BITS) {
3053                 eeprom->word_size = 256;
3054                 eeprom->address_bits = 8;
3055             } else {
3056                 eeprom->word_size = 64;
3057                 eeprom->address_bits = 6;
3058             }
3059         }
3060         break;
3061     default:
3062         break;
3063     }
3064
3065     if (eeprom->type == e1000_eeprom_spi) {
3066         eeprom->word_size = 64;
3067         if (e1000_read_eeprom(hw, EEPROM_CFG, 1, &eeprom_size) == 0) {
3068             eeprom_size &= EEPROM_SIZE_MASK;
3069
3070             switch (eeprom_size) {
3071             case EEPROM_SIZE_16KB:
3072                 eeprom->word_size = 8192;
3073                 break;
3074             case EEPROM_SIZE_8KB:
3075                 eeprom->word_size = 4096;
3076                 break;
3077             case EEPROM_SIZE_4KB:
3078                 eeprom->word_size = 2048;
3079                 break;
3080             case EEPROM_SIZE_2KB:
3081                 eeprom->word_size = 1024;
3082                 break;
3083             case EEPROM_SIZE_1KB:
3084                 eeprom->word_size = 512;
3085                 break;
3086             case EEPROM_SIZE_512B:
3087                 eeprom->word_size = 256;
3088                 break;
3089             case EEPROM_SIZE_128B:
3090             default:
3091                 eeprom->word_size = 64;
3092                 break;
3093             }
3094         }
3095     }
3096 }
3097
3098 /******************************************************************************
3099  * Raises the EEPROM's clock input.
3100  *
3101  * hw - Struct containing variables accessed by shared code
3102  * eecd - EECD's current value
3103  *****************************************************************************/
3104 static void
3105 e1000_raise_ee_clk(struct e1000_hw *hw,
3106                    uint32_t *eecd)
3107 {
3108     /* Raise the clock input to the EEPROM (by setting the SK bit), and then
3109      * wait <delay> microseconds.
3110      */
3111     *eecd = *eecd | E1000_EECD_SK;
3112     E1000_WRITE_REG(hw, EECD, *eecd);
3113     E1000_WRITE_FLUSH(hw);
3114     udelay(hw->eeprom.delay_usec);
3115 }
3116
3117 /******************************************************************************
3118  * Lowers the EEPROM's clock input.
3119  *
3120  * hw - Struct containing variables accessed by shared code
3121  * eecd - EECD's current value
3122  *****************************************************************************/
3123 static void
3124 e1000_lower_ee_clk(struct e1000_hw *hw,
3125                    uint32_t *eecd)
3126 {
3127     /* Lower the clock input to the EEPROM (by clearing the SK bit), and then
3128      * wait 50 microseconds.
3129      */
3130     *eecd = *eecd & ~E1000_EECD_SK;
3131     E1000_WRITE_REG(hw, EECD, *eecd);
3132     E1000_WRITE_FLUSH(hw);
3133     udelay(hw->eeprom.delay_usec);
3134 }
3135
3136 /******************************************************************************
3137  * Shift data bits out to the EEPROM.
3138  *
3139  * hw - Struct containing variables accessed by shared code
3140  * data - data to send to the EEPROM
3141  * count - number of bits to shift out
3142  *****************************************************************************/
3143 static void
3144 e1000_shift_out_ee_bits(struct e1000_hw *hw,
3145                         uint16_t data,
3146                         uint16_t count)
3147 {
3148     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3149     uint32_t eecd;
3150     uint32_t mask;
3151
3152     /* We need to shift "count" bits out to the EEPROM. So, value in the
3153      * "data" parameter will be shifted out to the EEPROM one bit at a time.
3154      * In order to do this, "data" must be broken down into bits.
3155      */
3156     mask = 0x01 << (count - 1);
3157     eecd = E1000_READ_REG(hw, EECD);
3158     if (eeprom->type == e1000_eeprom_microwire) {
3159         eecd &= ~E1000_EECD_DO;
3160     } else if (eeprom->type == e1000_eeprom_spi) {
3161         eecd |= E1000_EECD_DO;
3162     }
3163     do {
3164         /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
3165          * and then raising and then lowering the clock (the SK bit controls
3166          * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
3167          * by setting "DI" to "0" and then raising and then lowering the clock.
3168          */
3169         eecd &= ~E1000_EECD_DI;
3170
3171         if(data & mask)
3172             eecd |= E1000_EECD_DI;
3173
3174         E1000_WRITE_REG(hw, EECD, eecd);
3175         E1000_WRITE_FLUSH(hw);
3176
3177         udelay(eeprom->delay_usec);
3178
3179         e1000_raise_ee_clk(hw, &eecd);
3180         e1000_lower_ee_clk(hw, &eecd);
3181
3182         mask = mask >> 1;
3183
3184     } while(mask);
3185
3186     /* We leave the "DI" bit set to "0" when we leave this routine. */
3187     eecd &= ~E1000_EECD_DI;
3188     E1000_WRITE_REG(hw, EECD, eecd);
3189 }
3190
3191 /******************************************************************************
3192  * Shift data bits in from the EEPROM
3193  *
3194  * hw - Struct containing variables accessed by shared code
3195  *****************************************************************************/
3196 static uint16_t
3197 e1000_shift_in_ee_bits(struct e1000_hw *hw,
3198                        uint16_t count)
3199 {
3200     uint32_t eecd;
3201     uint32_t i;
3202     uint16_t data;
3203
3204     /* In order to read a register from the EEPROM, we need to shift 'count'
3205      * bits in from the EEPROM. Bits are "shifted in" by raising the clock
3206      * input to the EEPROM (setting the SK bit), and then reading the value of
3207      * the "DO" bit.  During this "shifting in" process the "DI" bit should
3208      * always be clear.
3209      */
3210
3211     eecd = E1000_READ_REG(hw, EECD);
3212
3213     eecd &= ~(E1000_EECD_DO | E1000_EECD_DI);
3214     data = 0;
3215
3216     for(i = 0; i < count; i++) {
3217         data = data << 1;
3218         e1000_raise_ee_clk(hw, &eecd);
3219
3220         eecd = E1000_READ_REG(hw, EECD);
3221
3222         eecd &= ~(E1000_EECD_DI);
3223         if(eecd & E1000_EECD_DO)
3224             data |= 1;
3225
3226         e1000_lower_ee_clk(hw, &eecd);
3227     }
3228
3229     return data;
3230 }
3231
3232 /******************************************************************************
3233  * Prepares EEPROM for access
3234  *
3235  * hw - Struct containing variables accessed by shared code
3236  *
3237  * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This
3238  * function should be called before issuing a command to the EEPROM.
3239  *****************************************************************************/
3240 static int32_t
3241 e1000_acquire_eeprom(struct e1000_hw *hw)
3242 {
3243     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3244     uint32_t eecd, i=0;
3245
3246     DEBUGFUNC("e1000_acquire_eeprom");
3247
3248     eecd = E1000_READ_REG(hw, EECD);
3249
3250     /* Request EEPROM Access */
3251     if(hw->mac_type > e1000_82544) {
3252         eecd |= E1000_EECD_REQ;
3253         E1000_WRITE_REG(hw, EECD, eecd);
3254         eecd = E1000_READ_REG(hw, EECD);
3255         while((!(eecd & E1000_EECD_GNT)) &&
3256               (i < E1000_EEPROM_GRANT_ATTEMPTS)) {
3257             i++;
3258             udelay(5);
3259             eecd = E1000_READ_REG(hw, EECD);
3260         }
3261         if(!(eecd & E1000_EECD_GNT)) {
3262             eecd &= ~E1000_EECD_REQ;
3263             E1000_WRITE_REG(hw, EECD, eecd);
3264             DEBUGOUT("Could not acquire EEPROM grant\n");
3265             return -E1000_ERR_EEPROM;
3266         }
3267     }
3268
3269     /* Setup EEPROM for Read/Write */
3270
3271     if (eeprom->type == e1000_eeprom_microwire) {
3272         /* Clear SK and DI */
3273         eecd &= ~(E1000_EECD_DI | E1000_EECD_SK);
3274         E1000_WRITE_REG(hw, EECD, eecd);
3275
3276         /* Set CS */
3277         eecd |= E1000_EECD_CS;
3278         E1000_WRITE_REG(hw, EECD, eecd);
3279     } else if (eeprom->type == e1000_eeprom_spi) {
3280         /* Clear SK and CS */
3281         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
3282         E1000_WRITE_REG(hw, EECD, eecd);
3283         udelay(1);
3284     }
3285
3286     return E1000_SUCCESS;
3287 }
3288
3289 /******************************************************************************
3290  * Returns EEPROM to a "standby" state
3291  *
3292  * hw - Struct containing variables accessed by shared code
3293  *****************************************************************************/
3294 static void
3295 e1000_standby_eeprom(struct e1000_hw *hw)
3296 {
3297     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3298     uint32_t eecd;
3299
3300     eecd = E1000_READ_REG(hw, EECD);
3301
3302     if(eeprom->type == e1000_eeprom_microwire) {
3303         eecd &= ~(E1000_EECD_CS | E1000_EECD_SK);
3304         E1000_WRITE_REG(hw, EECD, eecd);
3305         E1000_WRITE_FLUSH(hw);
3306         udelay(eeprom->delay_usec);
3307
3308         /* Clock high */
3309         eecd |= E1000_EECD_SK;
3310         E1000_WRITE_REG(hw, EECD, eecd);
3311         E1000_WRITE_FLUSH(hw);
3312         udelay(eeprom->delay_usec);
3313
3314         /* Select EEPROM */
3315         eecd |= E1000_EECD_CS;
3316         E1000_WRITE_REG(hw, EECD, eecd);
3317         E1000_WRITE_FLUSH(hw);
3318         udelay(eeprom->delay_usec);
3319
3320         /* Clock low */
3321         eecd &= ~E1000_EECD_SK;
3322         E1000_WRITE_REG(hw, EECD, eecd);
3323         E1000_WRITE_FLUSH(hw);
3324         udelay(eeprom->delay_usec);
3325     } else if(eeprom->type == e1000_eeprom_spi) {
3326         /* Toggle CS to flush commands */
3327         eecd |= E1000_EECD_CS;
3328         E1000_WRITE_REG(hw, EECD, eecd);
3329         E1000_WRITE_FLUSH(hw);
3330         udelay(eeprom->delay_usec);
3331         eecd &= ~E1000_EECD_CS;
3332         E1000_WRITE_REG(hw, EECD, eecd);
3333         E1000_WRITE_FLUSH(hw);
3334         udelay(eeprom->delay_usec);
3335     }
3336 }
3337
3338 /******************************************************************************
3339  * Terminates a command by inverting the EEPROM's chip select pin
3340  *
3341  * hw - Struct containing variables accessed by shared code
3342  *****************************************************************************/
3343 static void
3344 e1000_release_eeprom(struct e1000_hw *hw)
3345 {
3346     uint32_t eecd;
3347
3348     DEBUGFUNC("e1000_release_eeprom");
3349
3350     eecd = E1000_READ_REG(hw, EECD);
3351
3352     if (hw->eeprom.type == e1000_eeprom_spi) {
3353         eecd |= E1000_EECD_CS;  /* Pull CS high */
3354         eecd &= ~E1000_EECD_SK; /* Lower SCK */
3355
3356         E1000_WRITE_REG(hw, EECD, eecd);
3357
3358         udelay(hw->eeprom.delay_usec);
3359     } else if(hw->eeprom.type == e1000_eeprom_microwire) {
3360         /* cleanup eeprom */
3361
3362         /* CS on Microwire is active-high */
3363         eecd &= ~(E1000_EECD_CS | E1000_EECD_DI);
3364
3365         E1000_WRITE_REG(hw, EECD, eecd);
3366
3367         /* Rising edge of clock */
3368         eecd |= E1000_EECD_SK;
3369         E1000_WRITE_REG(hw, EECD, eecd);
3370         E1000_WRITE_FLUSH(hw);
3371         udelay(hw->eeprom.delay_usec);
3372
3373         /* Falling edge of clock */
3374         eecd &= ~E1000_EECD_SK;
3375         E1000_WRITE_REG(hw, EECD, eecd);
3376         E1000_WRITE_FLUSH(hw);
3377         udelay(hw->eeprom.delay_usec);
3378     }
3379
3380     /* Stop requesting EEPROM access */
3381     if(hw->mac_type > e1000_82544) {
3382         eecd &= ~E1000_EECD_REQ;
3383         E1000_WRITE_REG(hw, EECD, eecd);
3384     }
3385 }
3386
3387 /******************************************************************************
3388  * Reads a 16 bit word from the EEPROM.
3389  *
3390  * hw - Struct containing variables accessed by shared code
3391  *****************************************************************************/
3392 int32_t
3393 e1000_spi_eeprom_ready(struct e1000_hw *hw)
3394 {
3395     uint16_t retry_count = 0;
3396     uint8_t spi_stat_reg;
3397
3398     DEBUGFUNC("e1000_spi_eeprom_ready");
3399
3400     /* Read "Status Register" repeatedly until the LSB is cleared.  The
3401      * EEPROM will signal that the command has been completed by clearing
3402      * bit 0 of the internal status register.  If it's not cleared within
3403      * 5 milliseconds, then error out.
3404      */
3405     retry_count = 0;
3406     do {
3407         e1000_shift_out_ee_bits(hw, EEPROM_RDSR_OPCODE_SPI,
3408                                 hw->eeprom.opcode_bits);
3409         spi_stat_reg = (uint8_t)e1000_shift_in_ee_bits(hw, 8);
3410         if (!(spi_stat_reg & EEPROM_STATUS_RDY_SPI))
3411             break;
3412
3413         udelay(5);
3414         retry_count += 5;
3415
3416         e1000_standby_eeprom(hw);
3417     } while(retry_count < EEPROM_MAX_RETRY_SPI);
3418
3419     /* ATMEL SPI write time could vary from 0-20mSec on 3.3V devices (and
3420      * only 0-5mSec on 5V devices)
3421      */
3422     if(retry_count >= EEPROM_MAX_RETRY_SPI) {
3423         DEBUGOUT("SPI EEPROM Status error\n");
3424         return -E1000_ERR_EEPROM;
3425     }
3426
3427     return E1000_SUCCESS;
3428 }
3429
3430 /******************************************************************************
3431  * Reads a 16 bit word from the EEPROM.
3432  *
3433  * hw - Struct containing variables accessed by shared code
3434  * offset - offset of  word in the EEPROM to read
3435  * data - word read from the EEPROM
3436  * words - number of words to read
3437  *****************************************************************************/
3438 int32_t
3439 e1000_read_eeprom(struct e1000_hw *hw,
3440                   uint16_t offset,
3441                   uint16_t words,
3442                   uint16_t *data)
3443 {
3444     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3445     uint32_t i = 0;
3446
3447     DEBUGFUNC("e1000_read_eeprom");
3448     /* A check for invalid values:  offset too large, too many words, and not
3449      * enough words.
3450      */
3451     if((offset > eeprom->word_size) || (words > eeprom->word_size - offset) ||
3452        (words == 0)) {
3453         DEBUGOUT("\"words\" parameter out of bounds\n");
3454         return -E1000_ERR_EEPROM;
3455     }
3456
3457     /* Prepare the EEPROM for reading  */
3458     if(e1000_acquire_eeprom(hw) != E1000_SUCCESS)
3459         return -E1000_ERR_EEPROM;
3460
3461     if(eeprom->type == e1000_eeprom_spi) {
3462         uint16_t word_in;
3463         uint8_t read_opcode = EEPROM_READ_OPCODE_SPI;
3464
3465         if(e1000_spi_eeprom_ready(hw)) {
3466             e1000_release_eeprom(hw);
3467             return -E1000_ERR_EEPROM;
3468         }
3469
3470         e1000_standby_eeprom(hw);
3471
3472         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
3473         if((eeprom->address_bits == 8) && (offset >= 128))
3474             read_opcode |= EEPROM_A8_OPCODE_SPI;
3475
3476         /* Send the READ command (opcode + addr)  */
3477         e1000_shift_out_ee_bits(hw, read_opcode, eeprom->opcode_bits);
3478         e1000_shift_out_ee_bits(hw, (uint16_t)(offset*2), eeprom->address_bits);
3479
3480         /* Read the data.  The address of the eeprom internally increments with
3481          * each byte (spi) being read, saving on the overhead of eeprom setup
3482          * and tear-down.  The address counter will roll over if reading beyond
3483          * the size of the eeprom, thus allowing the entire memory to be read
3484          * starting from any offset. */
3485         for (i = 0; i < words; i++) {
3486             word_in = e1000_shift_in_ee_bits(hw, 16);
3487             data[i] = (word_in >> 8) | (word_in << 8);
3488         }
3489     } else if(eeprom->type == e1000_eeprom_microwire) {
3490         for (i = 0; i < words; i++) {
3491             /* Send the READ command (opcode + addr)  */
3492             e1000_shift_out_ee_bits(hw, EEPROM_READ_OPCODE_MICROWIRE,
3493                                     eeprom->opcode_bits);
3494             e1000_shift_out_ee_bits(hw, (uint16_t)(offset + i),
3495                                     eeprom->address_bits);
3496
3497             /* Read the data.  For microwire, each word requires the overhead
3498              * of eeprom setup and tear-down. */
3499             data[i] = e1000_shift_in_ee_bits(hw, 16);
3500             e1000_standby_eeprom(hw);
3501         }
3502     }
3503
3504     /* End this read operation */
3505     e1000_release_eeprom(hw);
3506
3507     return E1000_SUCCESS;
3508 }
3509
3510 /******************************************************************************
3511  * Verifies that the EEPROM has a valid checksum
3512  *
3513  * hw - Struct containing variables accessed by shared code
3514  *
3515  * Reads the first 64 16 bit words of the EEPROM and sums the values read.
3516  * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
3517  * valid.
3518  *****************************************************************************/
3519 int32_t
3520 e1000_validate_eeprom_checksum(struct e1000_hw *hw)
3521 {
3522     uint16_t checksum = 0;
3523     uint16_t i, eeprom_data;
3524
3525     DEBUGFUNC("e1000_validate_eeprom_checksum");
3526
3527     for(i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++) {
3528         if(e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
3529             DEBUGOUT("EEPROM Read Error\n");
3530             return -E1000_ERR_EEPROM;
3531         }
3532         checksum += eeprom_data;
3533     }
3534
3535     if(checksum == (uint16_t) EEPROM_SUM)
3536         return E1000_SUCCESS;
3537     else {
3538         DEBUGOUT("EEPROM Checksum Invalid\n");
3539         return -E1000_ERR_EEPROM;
3540     }
3541 }
3542
3543 /******************************************************************************
3544  * Calculates the EEPROM checksum and writes it to the EEPROM
3545  *
3546  * hw - Struct containing variables accessed by shared code
3547  *
3548  * Sums the first 63 16 bit words of the EEPROM. Subtracts the sum from 0xBABA.
3549  * Writes the difference to word offset 63 of the EEPROM.
3550  *****************************************************************************/
3551 int32_t
3552 e1000_update_eeprom_checksum(struct e1000_hw *hw)
3553 {
3554     uint16_t checksum = 0;
3555     uint16_t i, eeprom_data;
3556
3557     DEBUGFUNC("e1000_update_eeprom_checksum");
3558
3559     for(i = 0; i < EEPROM_CHECKSUM_REG; i++) {
3560         if(e1000_read_eeprom(hw, i, 1, &eeprom_data) < 0) {
3561             DEBUGOUT("EEPROM Read Error\n");
3562             return -E1000_ERR_EEPROM;
3563         }
3564         checksum += eeprom_data;
3565     }
3566     checksum = (uint16_t) EEPROM_SUM - checksum;
3567     if(e1000_write_eeprom(hw, EEPROM_CHECKSUM_REG, 1, &checksum) < 0) {
3568         DEBUGOUT("EEPROM Write Error\n");
3569         return -E1000_ERR_EEPROM;
3570     }
3571     return E1000_SUCCESS;
3572 }
3573
3574 /******************************************************************************
3575  * Parent function for writing words to the different EEPROM types.
3576  *
3577  * hw - Struct containing variables accessed by shared code
3578  * offset - offset within the EEPROM to be written to
3579  * words - number of words to write
3580  * data - 16 bit word to be written to the EEPROM
3581  *
3582  * If e1000_update_eeprom_checksum is not called after this function, the
3583  * EEPROM will most likely contain an invalid checksum.
3584  *****************************************************************************/
3585 int32_t
3586 e1000_write_eeprom(struct e1000_hw *hw,
3587                    uint16_t offset,
3588                    uint16_t words,
3589                    uint16_t *data)
3590 {
3591     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3592     int32_t status = 0;
3593
3594     DEBUGFUNC("e1000_write_eeprom");
3595
3596     /* A check for invalid values:  offset too large, too many words, and not
3597      * enough words.
3598      */
3599     if((offset > eeprom->word_size) || (words > eeprom->word_size - offset) ||
3600        (words == 0)) {
3601         DEBUGOUT("\"words\" parameter out of bounds\n");
3602         return -E1000_ERR_EEPROM;
3603     }
3604
3605     /* Prepare the EEPROM for writing  */
3606     if (e1000_acquire_eeprom(hw) != E1000_SUCCESS)
3607         return -E1000_ERR_EEPROM;
3608
3609     if(eeprom->type == e1000_eeprom_microwire) {
3610         status = e1000_write_eeprom_microwire(hw, offset, words, data);
3611     } else {
3612         status = e1000_write_eeprom_spi(hw, offset, words, data);
3613         msec_delay(10);
3614     }
3615
3616     /* Done with writing */
3617     e1000_release_eeprom(hw);
3618
3619     return status;
3620 }
3621
3622 /******************************************************************************
3623  * Writes a 16 bit word to a given offset in an SPI EEPROM.
3624  *
3625  * hw - Struct containing variables accessed by shared code
3626  * offset - offset within the EEPROM to be written to
3627  * words - number of words to write
3628  * data - pointer to array of 8 bit words to be written to the EEPROM
3629  *
3630  *****************************************************************************/
3631 int32_t
3632 e1000_write_eeprom_spi(struct e1000_hw *hw,
3633                        uint16_t offset,
3634                        uint16_t words,
3635                        uint16_t *data)
3636 {
3637     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3638     uint16_t widx = 0;
3639
3640     DEBUGFUNC("e1000_write_eeprom_spi");
3641
3642     while (widx < words) {
3643         uint8_t write_opcode = EEPROM_WRITE_OPCODE_SPI;
3644
3645         if(e1000_spi_eeprom_ready(hw)) return -E1000_ERR_EEPROM;
3646
3647         e1000_standby_eeprom(hw);
3648
3649         /*  Send the WRITE ENABLE command (8 bit opcode )  */
3650         e1000_shift_out_ee_bits(hw, EEPROM_WREN_OPCODE_SPI,
3651                                     eeprom->opcode_bits);
3652
3653         e1000_standby_eeprom(hw);
3654
3655         /* Some SPI eeproms use the 8th address bit embedded in the opcode */
3656         if((eeprom->address_bits == 8) && (offset >= 128))
3657             write_opcode |= EEPROM_A8_OPCODE_SPI;
3658
3659         /* Send the Write command (8-bit opcode + addr) */
3660         e1000_shift_out_ee_bits(hw, write_opcode, eeprom->opcode_bits);
3661
3662         e1000_shift_out_ee_bits(hw, (uint16_t)((offset + widx)*2),
3663                                 eeprom->address_bits);
3664
3665         /* Send the data */
3666
3667         /* Loop to allow for up to whole page write (32 bytes) of eeprom */
3668         while (widx < words) {
3669             uint16_t word_out = data[widx];
3670             word_out = (word_out >> 8) | (word_out << 8);
3671             e1000_shift_out_ee_bits(hw, word_out, 16);
3672             widx++;
3673
3674             /* Some larger eeprom sizes are capable of a 32-byte PAGE WRITE
3675              * operation, while the smaller eeproms are capable of an 8-byte
3676              * PAGE WRITE operation.  Break the inner loop to pass new address
3677              */
3678             if((((offset + widx)*2) % eeprom->page_size) == 0) {
3679                 e1000_standby_eeprom(hw);
3680                 break;
3681             }
3682         }
3683     }
3684
3685     return E1000_SUCCESS;
3686 }
3687
3688 /******************************************************************************
3689  * Writes a 16 bit word to a given offset in a Microwire EEPROM.
3690  *
3691  * hw - Struct containing variables accessed by shared code
3692  * offset - offset within the EEPROM to be written to
3693  * words - number of words to write
3694  * data - pointer to array of 16 bit words to be written to the EEPROM
3695  *
3696  *****************************************************************************/
3697 int32_t
3698 e1000_write_eeprom_microwire(struct e1000_hw *hw,
3699                              uint16_t offset,
3700                              uint16_t words,
3701                              uint16_t *data)
3702 {
3703     struct e1000_eeprom_info *eeprom = &hw->eeprom;
3704     uint32_t eecd;
3705     uint16_t words_written = 0;
3706     uint16_t i = 0;
3707
3708     DEBUGFUNC("e1000_write_eeprom_microwire");
3709
3710     /* Send the write enable command to the EEPROM (3-bit opcode plus
3711      * 6/8-bit dummy address beginning with 11).  It's less work to include
3712      * the 11 of the dummy address as part of the opcode than it is to shift
3713      * it over the correct number of bits for the address.  This puts the
3714      * EEPROM into write/erase mode.
3715      */
3716     e1000_shift_out_ee_bits(hw, EEPROM_EWEN_OPCODE_MICROWIRE,
3717                             (uint16_t)(eeprom->opcode_bits + 2));
3718
3719     e1000_shift_out_ee_bits(hw, 0, (uint16_t)(eeprom->address_bits - 2));
3720
3721     /* Prepare the EEPROM */
3722     e1000_standby_eeprom(hw);
3723
3724     while (words_written < words) {
3725         /* Send the Write command (3-bit opcode + addr) */
3726         e1000_shift_out_ee_bits(hw, EEPROM_WRITE_OPCODE_MICROWIRE,
3727                                 eeprom->opcode_bits);
3728
3729         e1000_shift_out_ee_bits(hw, (uint16_t)(offset + words_written),
3730                                 eeprom->address_bits);
3731
3732         /* Send the data */
3733         e1000_shift_out_ee_bits(hw, data[words_written], 16);
3734
3735         /* Toggle the CS line.  This in effect tells the EEPROM to execute
3736          * the previous command.
3737          */
3738         e1000_standby_eeprom(hw);
3739
3740         /* Read DO repeatedly until it is high (equal to '1').  The EEPROM will
3741          * signal that the command has been completed by raising the DO signal.
3742          * If DO does not go high in 10 milliseconds, then error out.
3743          */
3744         for(i = 0; i < 200; i++) {
3745             eecd = E1000_READ_REG(hw, EECD);
3746             if(eecd & E1000_EECD_DO) break;
3747             udelay(50);
3748         }
3749         if(i == 200) {
3750             DEBUGOUT("EEPROM Write did not complete\n");
3751             return -E1000_ERR_EEPROM;
3752         }
3753
3754         /* Recover from write */
3755         e1000_standby_eeprom(hw);
3756
3757         words_written++;
3758     }
3759
3760     /* Send the write disable command to the EEPROM (3-bit opcode plus
3761      * 6/8-bit dummy address beginning with 10).  It's less work to include
3762      * the 10 of the dummy address as part of the opcode than it is to shift
3763      * it over the correct number of bits for the address.  This takes the
3764      * EEPROM out of write/erase mode.
3765      */
3766     e1000_shift_out_ee_bits(hw, EEPROM_EWDS_OPCODE_MICROWIRE,
3767                             (uint16_t)(eeprom->opcode_bits + 2));
3768
3769     e1000_shift_out_ee_bits(hw, 0, (uint16_t)(eeprom->address_bits - 2));
3770
3771     return E1000_SUCCESS;
3772 }
3773
3774 /******************************************************************************
3775  * Reads the adapter's part number from the EEPROM
3776  *
3777  * hw - Struct containing variables accessed by shared code
3778  * part_num - Adapter's part number
3779  *****************************************************************************/
3780 int32_t
3781 e1000_read_part_num(struct e1000_hw *hw,
3782                     uint32_t *part_num)
3783 {
3784     uint16_t offset = EEPROM_PBA_BYTE_1;
3785     uint16_t eeprom_data;
3786
3787     DEBUGFUNC("e1000_read_part_num");
3788
3789     /* Get word 0 from EEPROM */
3790     if(e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
3791         DEBUGOUT("EEPROM Read Error\n");
3792         return -E1000_ERR_EEPROM;
3793     }
3794     /* Save word 0 in upper half of part_num */
3795     *part_num = (uint32_t) (eeprom_data << 16);
3796
3797     /* Get word 1 from EEPROM */
3798     if(e1000_read_eeprom(hw, ++offset, 1, &eeprom_data) < 0) {
3799         DEBUGOUT("EEPROM Read Error\n");
3800         return -E1000_ERR_EEPROM;
3801     }
3802     /* Save word 1 in lower half of part_num */
3803     *part_num |= eeprom_data;
3804
3805     return E1000_SUCCESS;
3806 }
3807
3808 /******************************************************************************
3809  * Reads the adapter's MAC address from the EEPROM and inverts the LSB for the
3810  * second function of dual function devices
3811  *
3812  * hw - Struct containing variables accessed by shared code
3813  *****************************************************************************/
3814 int32_t
3815 e1000_read_mac_addr(struct e1000_hw * hw)
3816 {
3817     uint16_t offset;
3818     uint16_t eeprom_data, i;
3819
3820     DEBUGFUNC("e1000_read_mac_addr");
3821
3822     for(i = 0; i < NODE_ADDRESS_SIZE; i += 2) {
3823         offset = i >> 1;
3824         if(e1000_read_eeprom(hw, offset, 1, &eeprom_data) < 0) {
3825             DEBUGOUT("EEPROM Read Error\n");
3826             return -E1000_ERR_EEPROM;
3827         }
3828         hw->perm_mac_addr[i] = (uint8_t) (eeprom_data & 0x00FF);
3829         hw->perm_mac_addr[i+1] = (uint8_t) (eeprom_data >> 8);
3830     }
3831     if(((hw->mac_type == e1000_82546) || (hw->mac_type == e1000_82546_rev_3)) &&
3832        (E1000_READ_REG(hw, STATUS) & E1000_STATUS_FUNC_1))
3833             hw->perm_mac_addr[5] ^= 0x01;
3834
3835     for(i = 0; i < NODE_ADDRESS_SIZE; i++)
3836         hw->mac_addr[i] = hw->perm_mac_addr[i];
3837     return E1000_SUCCESS;
3838 }
3839
3840 /******************************************************************************
3841  * Initializes receive address filters.
3842  *
3843  * hw - Struct containing variables accessed by shared code
3844  *
3845  * Places the MAC address in receive address register 0 and clears the rest
3846  * of the receive addresss registers. Clears the multicast table. Assumes
3847  * the receiver is in reset when the routine is called.
3848  *****************************************************************************/
3849 void
3850 e1000_init_rx_addrs(struct e1000_hw *hw)
3851 {
3852     uint32_t i;
3853
3854     DEBUGFUNC("e1000_init_rx_addrs");
3855
3856     /* Setup the receive address. */
3857     DEBUGOUT("Programming MAC Address into RAR[0]\n");
3858
3859     e1000_rar_set(hw, hw->mac_addr, 0);
3860
3861     /* Zero out the other 15 receive addresses. */
3862     DEBUGOUT("Clearing RAR[1-15]\n");
3863     for(i = 1; i < E1000_RAR_ENTRIES; i++) {
3864         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
3865         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
3866     }
3867 }
3868
3869 /******************************************************************************
3870  * Updates the MAC's list of multicast addresses.
3871  *
3872  * hw - Struct containing variables accessed by shared code
3873  * mc_addr_list - the list of new multicast addresses
3874  * mc_addr_count - number of addresses
3875  * pad - number of bytes between addresses in the list
3876  * rar_used_count - offset where to start adding mc addresses into the RAR's
3877  *
3878  * The given list replaces any existing list. Clears the last 15 receive
3879  * address registers and the multicast table. Uses receive address registers
3880  * for the first 15 multicast addresses, and hashes the rest into the
3881  * multicast table.
3882  *****************************************************************************/
3883 void
3884 e1000_mc_addr_list_update(struct e1000_hw *hw,
3885                           uint8_t *mc_addr_list,
3886                           uint32_t mc_addr_count,
3887                           uint32_t pad,
3888                           uint32_t rar_used_count)
3889 {
3890     uint32_t hash_value;
3891     uint32_t i;
3892
3893     DEBUGFUNC("e1000_mc_addr_list_update");
3894
3895     /* Set the new number of MC addresses that we are being requested to use. */
3896     hw->num_mc_addrs = mc_addr_count;
3897
3898     /* Clear RAR[1-15] */
3899     DEBUGOUT(" Clearing RAR[1-15]\n");
3900     for(i = rar_used_count; i < E1000_RAR_ENTRIES; i++) {
3901         E1000_WRITE_REG_ARRAY(hw, RA, (i << 1), 0);
3902         E1000_WRITE_REG_ARRAY(hw, RA, ((i << 1) + 1), 0);
3903     }
3904
3905     /* Clear the MTA */
3906     DEBUGOUT(" Clearing MTA\n");
3907     for(i = 0; i < E1000_NUM_MTA_REGISTERS; i++) {
3908         E1000_WRITE_REG_ARRAY(hw, MTA, i, 0);
3909     }
3910
3911     /* Add the new addresses */
3912     for(i = 0; i < mc_addr_count; i++) {
3913         DEBUGOUT(" Adding the multicast addresses:\n");
3914         DEBUGOUT7(" MC Addr #%d =%.2X %.2X %.2X %.2X %.2X %.2X\n", i,
3915                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad)],
3916                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 1],
3917                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 2],
3918                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 3],
3919                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 4],
3920                   mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 5]);
3921
3922         hash_value = e1000_hash_mc_addr(hw,
3923                                         mc_addr_list +
3924                                         (i * (ETH_LENGTH_OF_ADDRESS + pad)));
3925
3926         DEBUGOUT1(" Hash value = 0x%03X\n", hash_value);
3927
3928         /* Place this multicast address in the RAR if there is room, *
3929          * else put it in the MTA
3930          */
3931         if(rar_used_count < E1000_RAR_ENTRIES) {
3932             e1000_rar_set(hw,
3933                           mc_addr_list + (i * (ETH_LENGTH_OF_ADDRESS + pad)),
3934                           rar_used_count);
3935             rar_used_count++;
3936         } else {
3937             e1000_mta_set(hw, hash_value);
3938         }
3939     }
3940     DEBUGOUT("MC Update Complete\n");
3941 }
3942
3943 /******************************************************************************
3944  * Hashes an address to determine its location in the multicast table
3945  *
3946  * hw - Struct containing variables accessed by shared code
3947  * mc_addr - the multicast address to hash
3948  *****************************************************************************/
3949 uint32_t
3950 e1000_hash_mc_addr(struct e1000_hw *hw,
3951                    uint8_t *mc_addr)
3952 {
3953     uint32_t hash_value = 0;
3954
3955     /* The portion of the address that is used for the hash table is
3956      * determined by the mc_filter_type setting.
3957      */
3958     switch (hw->mc_filter_type) {
3959     /* [0] [1] [2] [3] [4] [5]
3960      * 01  AA  00  12  34  56
3961      * LSB                 MSB
3962      */
3963     case 0:
3964         /* [47:36] i.e. 0x563 for above example address */
3965         hash_value = ((mc_addr[4] >> 4) | (((uint16_t) mc_addr[5]) << 4));
3966         break;
3967     case 1:
3968         /* [46:35] i.e. 0xAC6 for above example address */
3969         hash_value = ((mc_addr[4] >> 3) | (((uint16_t) mc_addr[5]) << 5));
3970         break;
3971     case 2:
3972         /* [45:34] i.e. 0x5D8 for above example address */
3973         hash_value = ((mc_addr[4] >> 2) | (((uint16_t) mc_addr[5]) << 6));
3974         break;
3975     case 3:
3976         /* [43:32] i.e. 0x634 for above example address */
3977         hash_value = ((mc_addr[4]) | (((uint16_t) mc_addr[5]) << 8));
3978         break;
3979     }
3980
3981     hash_value &= 0xFFF;
3982     return hash_value;
3983 }
3984
3985 /******************************************************************************
3986  * Sets the bit in the multicast table corresponding to the hash value.
3987  *
3988  * hw - Struct containing variables accessed by shared code
3989  * hash_value - Multicast address hash value
3990  *****************************************************************************/
3991 void
3992 e1000_mta_set(struct e1000_hw *hw,
3993               uint32_t hash_value)
3994 {
3995     uint32_t hash_bit, hash_reg;
3996     uint32_t mta;
3997     uint32_t temp;
3998
3999     /* The MTA is a register array of 128 32-bit registers.
4000      * It is treated like an array of 4096 bits.  We want to set
4001      * bit BitArray[hash_value]. So we figure out what register
4002      * the bit is in, read it, OR in the new bit, then write
4003      * back the new value.  The register is determined by the
4004      * upper 7 bits of the hash value and the bit within that
4005      * register are determined by the lower 5 bits of the value.
4006      */
4007     hash_reg = (hash_value >> 5) & 0x7F;
4008     hash_bit = hash_value & 0x1F;
4009
4010     mta = E1000_READ_REG_ARRAY(hw, MTA, hash_reg);
4011
4012     mta |= (1 << hash_bit);
4013
4014     /* If we are on an 82544 and we are trying to write an odd offset
4015      * in the MTA, save off the previous entry before writing and
4016      * restore the old value after writing.
4017      */
4018     if((hw->mac_type == e1000_82544) && ((hash_reg & 0x1) == 1)) {
4019         temp = E1000_READ_REG_ARRAY(hw, MTA, (hash_reg - 1));
4020         E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
4021         E1000_WRITE_REG_ARRAY(hw, MTA, (hash_reg - 1), temp);
4022     } else {
4023         E1000_WRITE_REG_ARRAY(hw, MTA, hash_reg, mta);
4024     }
4025 }
4026
4027 /******************************************************************************
4028  * Puts an ethernet address into a receive address register.
4029  *
4030  * hw - Struct containing variables accessed by shared code
4031  * addr - Address to put into receive address register
4032  * index - Receive address register to write
4033  *****************************************************************************/
4034 void
4035 e1000_rar_set(struct e1000_hw *hw,
4036               uint8_t *addr,
4037               uint32_t index)
4038 {
4039     uint32_t rar_low, rar_high;
4040
4041     /* HW expects these in little endian so we reverse the byte order
4042      * from network order (big endian) to little endian
4043      */
4044     rar_low = ((uint32_t) addr[0] |
4045                ((uint32_t) addr[1] << 8) |
4046                ((uint32_t) addr[2] << 16) | ((uint32_t) addr[3] << 24));
4047
4048     rar_high = ((uint32_t) addr[4] | ((uint32_t) addr[5] << 8) | E1000_RAH_AV);
4049
4050     E1000_WRITE_REG_ARRAY(hw, RA, (index << 1), rar_low);
4051     E1000_WRITE_REG_ARRAY(hw, RA, ((index << 1) + 1), rar_high);
4052 }
4053
4054 /******************************************************************************
4055  * Writes a value to the specified offset in the VLAN filter table.
4056  *
4057  * hw - Struct containing variables accessed by shared code
4058  * offset - Offset in VLAN filer table to write
4059  * value - Value to write into VLAN filter table
4060  *****************************************************************************/
4061 void
4062 e1000_write_vfta(struct e1000_hw *hw,
4063                  uint32_t offset,
4064                  uint32_t value)
4065 {
4066     uint32_t temp;
4067
4068     if((hw->mac_type == e1000_82544) && ((offset & 0x1) == 1)) {
4069         temp = E1000_READ_REG_ARRAY(hw, VFTA, (offset - 1));
4070         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
4071         E1000_WRITE_REG_ARRAY(hw, VFTA, (offset - 1), temp);
4072     } else {
4073         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, value);
4074     }
4075 }
4076
4077 /******************************************************************************
4078  * Clears the VLAN filer table
4079  *
4080  * hw - Struct containing variables accessed by shared code
4081  *****************************************************************************/
4082 void
4083 e1000_clear_vfta(struct e1000_hw *hw)
4084 {
4085     uint32_t offset;
4086
4087     for(offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
4088         E1000_WRITE_REG_ARRAY(hw, VFTA, offset, 0);
4089 }
4090
4091 static int32_t
4092 e1000_id_led_init(struct e1000_hw * hw)
4093 {
4094     uint32_t ledctl;
4095     const uint32_t ledctl_mask = 0x000000FF;
4096     const uint32_t ledctl_on = E1000_LEDCTL_MODE_LED_ON;
4097     const uint32_t ledctl_off = E1000_LEDCTL_MODE_LED_OFF;
4098     uint16_t eeprom_data, i, temp;
4099     const uint16_t led_mask = 0x0F;
4100
4101     DEBUGFUNC("e1000_id_led_init");
4102
4103     if(hw->mac_type < e1000_82540) {
4104         /* Nothing to do */
4105         return E1000_SUCCESS;
4106     }
4107
4108     ledctl = E1000_READ_REG(hw, LEDCTL);
4109     hw->ledctl_default = ledctl;
4110     hw->ledctl_mode1 = hw->ledctl_default;
4111     hw->ledctl_mode2 = hw->ledctl_default;
4112
4113     if(e1000_read_eeprom(hw, EEPROM_ID_LED_SETTINGS, 1, &eeprom_data) < 0) {
4114         DEBUGOUT("EEPROM Read Error\n");
4115         return -E1000_ERR_EEPROM;
4116     }
4117     if((eeprom_data== ID_LED_RESERVED_0000) ||
4118        (eeprom_data == ID_LED_RESERVED_FFFF)) eeprom_data = ID_LED_DEFAULT;
4119     for(i = 0; i < 4; i++) {
4120         temp = (eeprom_data >> (i << 2)) & led_mask;
4121         switch(temp) {
4122         case ID_LED_ON1_DEF2:
4123         case ID_LED_ON1_ON2:
4124         case ID_LED_ON1_OFF2:
4125             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
4126             hw->ledctl_mode1 |= ledctl_on << (i << 3);
4127             break;
4128         case ID_LED_OFF1_DEF2:
4129         case ID_LED_OFF1_ON2:
4130         case ID_LED_OFF1_OFF2:
4131             hw->ledctl_mode1 &= ~(ledctl_mask << (i << 3));
4132             hw->ledctl_mode1 |= ledctl_off << (i << 3);
4133             break;
4134         default:
4135             /* Do nothing */
4136             break;
4137         }
4138         switch(temp) {
4139         case ID_LED_DEF1_ON2:
4140         case ID_LED_ON1_ON2:
4141         case ID_LED_OFF1_ON2:
4142             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
4143             hw->ledctl_mode2 |= ledctl_on << (i << 3);
4144             break;
4145         case ID_LED_DEF1_OFF2:
4146         case ID_LED_ON1_OFF2:
4147         case ID_LED_OFF1_OFF2:
4148             hw->ledctl_mode2 &= ~(ledctl_mask << (i << 3));
4149             hw->ledctl_mode2 |= ledctl_off << (i << 3);
4150             break;
4151         default:
4152             /* Do nothing */
4153             break;
4154         }
4155     }
4156     return E1000_SUCCESS;
4157 }
4158
4159 /******************************************************************************
4160  * Prepares SW controlable LED for use and saves the current state of the LED.
4161  *
4162  * hw - Struct containing variables accessed by shared code
4163  *****************************************************************************/
4164 int32_t
4165 e1000_setup_led(struct e1000_hw *hw)
4166 {
4167     uint32_t ledctl;
4168     int32_t ret_val = E1000_SUCCESS;
4169
4170     DEBUGFUNC("e1000_setup_led");
4171
4172     switch(hw->mac_type) {
4173     case e1000_82542_rev2_0:
4174     case e1000_82542_rev2_1:
4175     case e1000_82543:
4176     case e1000_82544:
4177         /* No setup necessary */
4178         break;
4179     case e1000_82541:
4180     case e1000_82547:
4181     case e1000_82541_rev_2:
4182     case e1000_82547_rev_2:
4183         /* Turn off PHY Smart Power Down (if enabled) */
4184         ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO,
4185                                      &hw->phy_spd_default);
4186         if(ret_val)
4187             return ret_val;
4188         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
4189                                       (uint16_t)(hw->phy_spd_default &
4190                                       ~IGP01E1000_GMII_SPD));
4191         if(ret_val)
4192             return ret_val;
4193         /* Fall Through */
4194     default:
4195         if(hw->media_type == e1000_media_type_fiber) {
4196             ledctl = E1000_READ_REG(hw, LEDCTL);
4197             /* Save current LEDCTL settings */
4198             hw->ledctl_default = ledctl;
4199             /* Turn off LED0 */
4200             ledctl &= ~(E1000_LEDCTL_LED0_IVRT |
4201                         E1000_LEDCTL_LED0_BLINK |
4202                         E1000_LEDCTL_LED0_MODE_MASK);
4203             ledctl |= (E1000_LEDCTL_MODE_LED_OFF <<
4204                        E1000_LEDCTL_LED0_MODE_SHIFT);
4205             E1000_WRITE_REG(hw, LEDCTL, ledctl);
4206         } else if(hw->media_type == e1000_media_type_copper)
4207             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
4208         break;
4209     }
4210
4211     return E1000_SUCCESS;
4212 }
4213
4214 /******************************************************************************
4215  * Restores the saved state of the SW controlable LED.
4216  *
4217  * hw - Struct containing variables accessed by shared code
4218  *****************************************************************************/
4219 int32_t
4220 e1000_cleanup_led(struct e1000_hw *hw)
4221 {
4222     int32_t ret_val = E1000_SUCCESS;
4223
4224     DEBUGFUNC("e1000_cleanup_led");
4225
4226     switch(hw->mac_type) {
4227     case e1000_82542_rev2_0:
4228     case e1000_82542_rev2_1:
4229     case e1000_82543:
4230     case e1000_82544:
4231         /* No cleanup necessary */
4232         break;
4233     case e1000_82541:
4234     case e1000_82547:
4235     case e1000_82541_rev_2:
4236     case e1000_82547_rev_2:
4237         /* Turn on PHY Smart Power Down (if previously enabled) */
4238         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO,
4239                                       hw->phy_spd_default);
4240         if(ret_val)
4241             return ret_val;
4242         /* Fall Through */
4243     default:
4244         /* Restore LEDCTL settings */
4245         E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_default);
4246         break;
4247     }
4248
4249     return E1000_SUCCESS;
4250 }
4251
4252 /******************************************************************************
4253  * Turns on the software controllable LED
4254  *
4255  * hw - Struct containing variables accessed by shared code
4256  *****************************************************************************/
4257 int32_t
4258 e1000_led_on(struct e1000_hw *hw)
4259 {
4260     uint32_t ctrl = E1000_READ_REG(hw, CTRL);
4261
4262     DEBUGFUNC("e1000_led_on");
4263
4264     switch(hw->mac_type) {
4265     case e1000_82542_rev2_0:
4266     case e1000_82542_rev2_1:
4267     case e1000_82543:
4268         /* Set SW Defineable Pin 0 to turn on the LED */
4269         ctrl |= E1000_CTRL_SWDPIN0;
4270         ctrl |= E1000_CTRL_SWDPIO0;
4271         break;
4272     case e1000_82544:
4273         if(hw->media_type == e1000_media_type_fiber) {
4274             /* Set SW Defineable Pin 0 to turn on the LED */
4275             ctrl |= E1000_CTRL_SWDPIN0;
4276             ctrl |= E1000_CTRL_SWDPIO0;
4277         } else {
4278             /* Clear SW Defineable Pin 0 to turn on the LED */
4279             ctrl &= ~E1000_CTRL_SWDPIN0;
4280             ctrl |= E1000_CTRL_SWDPIO0;
4281         }
4282         break;
4283     default:
4284         if(hw->media_type == e1000_media_type_fiber) {
4285             /* Clear SW Defineable Pin 0 to turn on the LED */
4286             ctrl &= ~E1000_CTRL_SWDPIN0;
4287             ctrl |= E1000_CTRL_SWDPIO0;
4288         } else if(hw->media_type == e1000_media_type_copper) {
4289             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode2);
4290             return E1000_SUCCESS;
4291         }
4292         break;
4293     }
4294
4295     E1000_WRITE_REG(hw, CTRL, ctrl);
4296
4297     return E1000_SUCCESS;
4298 }
4299
4300 /******************************************************************************
4301  * Turns off the software controllable LED
4302  *
4303  * hw - Struct containing variables accessed by shared code
4304  *****************************************************************************/
4305 int32_t
4306 e1000_led_off(struct e1000_hw *hw)
4307 {
4308     uint32_t ctrl = E1000_READ_REG(hw, CTRL);
4309
4310     DEBUGFUNC("e1000_led_off");
4311
4312     switch(hw->mac_type) {
4313     case e1000_82542_rev2_0:
4314     case e1000_82542_rev2_1:
4315     case e1000_82543:
4316         /* Clear SW Defineable Pin 0 to turn off the LED */
4317         ctrl &= ~E1000_CTRL_SWDPIN0;
4318         ctrl |= E1000_CTRL_SWDPIO0;
4319         break;
4320     case e1000_82544:
4321         if(hw->media_type == e1000_media_type_fiber) {
4322             /* Clear SW Defineable Pin 0 to turn off the LED */
4323             ctrl &= ~E1000_CTRL_SWDPIN0;
4324             ctrl |= E1000_CTRL_SWDPIO0;
4325         } else {
4326             /* Set SW Defineable Pin 0 to turn off the LED */
4327             ctrl |= E1000_CTRL_SWDPIN0;
4328             ctrl |= E1000_CTRL_SWDPIO0;
4329         }
4330         break;
4331     default:
4332         if(hw->media_type == e1000_media_type_fiber) {
4333             /* Set SW Defineable Pin 0 to turn off the LED */
4334             ctrl |= E1000_CTRL_SWDPIN0;
4335             ctrl |= E1000_CTRL_SWDPIO0;
4336         } else if(hw->media_type == e1000_media_type_copper) {
4337             E1000_WRITE_REG(hw, LEDCTL, hw->ledctl_mode1);
4338             return E1000_SUCCESS;
4339         }
4340         break;
4341     }
4342
4343     E1000_WRITE_REG(hw, CTRL, ctrl);
4344
4345     return E1000_SUCCESS;
4346 }
4347
4348 /******************************************************************************
4349  * Clears all hardware statistics counters.
4350  *
4351  * hw - Struct containing variables accessed by shared code
4352  *****************************************************************************/
4353 void
4354 e1000_clear_hw_cntrs(struct e1000_hw *hw)
4355 {
4356     volatile uint32_t temp;
4357
4358     temp = E1000_READ_REG(hw, CRCERRS);
4359     temp = E1000_READ_REG(hw, SYMERRS);
4360     temp = E1000_READ_REG(hw, MPC);
4361     temp = E1000_READ_REG(hw, SCC);
4362     temp = E1000_READ_REG(hw, ECOL);
4363     temp = E1000_READ_REG(hw, MCC);
4364     temp = E1000_READ_REG(hw, LATECOL);
4365     temp = E1000_READ_REG(hw, COLC);
4366     temp = E1000_READ_REG(hw, DC);
4367     temp = E1000_READ_REG(hw, SEC);
4368     temp = E1000_READ_REG(hw, RLEC);
4369     temp = E1000_READ_REG(hw, XONRXC);
4370     temp = E1000_READ_REG(hw, XONTXC);
4371     temp = E1000_READ_REG(hw, XOFFRXC);
4372     temp = E1000_READ_REG(hw, XOFFTXC);
4373     temp = E1000_READ_REG(hw, FCRUC);
4374     temp = E1000_READ_REG(hw, PRC64);
4375     temp = E1000_READ_REG(hw, PRC127);
4376     temp = E1000_READ_REG(hw, PRC255);
4377     temp = E1000_READ_REG(hw, PRC511);
4378     temp = E1000_READ_REG(hw, PRC1023);
4379     temp = E1000_READ_REG(hw, PRC1522);
4380     temp = E1000_READ_REG(hw, GPRC);
4381     temp = E1000_READ_REG(hw, BPRC);
4382     temp = E1000_READ_REG(hw, MPRC);
4383     temp = E1000_READ_REG(hw, GPTC);
4384     temp = E1000_READ_REG(hw, GORCL);
4385     temp = E1000_READ_REG(hw, GORCH);
4386     temp = E1000_READ_REG(hw, GOTCL);
4387     temp = E1000_READ_REG(hw, GOTCH);
4388     temp = E1000_READ_REG(hw, RNBC);
4389     temp = E1000_READ_REG(hw, RUC);
4390     temp = E1000_READ_REG(hw, RFC);
4391     temp = E1000_READ_REG(hw, ROC);
4392     temp = E1000_READ_REG(hw, RJC);
4393     temp = E1000_READ_REG(hw, TORL);
4394     temp = E1000_READ_REG(hw, TORH);
4395     temp = E1000_READ_REG(hw, TOTL);
4396     temp = E1000_READ_REG(hw, TOTH);
4397     temp = E1000_READ_REG(hw, TPR);
4398     temp = E1000_READ_REG(hw, TPT);
4399     temp = E1000_READ_REG(hw, PTC64);
4400     temp = E1000_READ_REG(hw, PTC127);
4401     temp = E1000_READ_REG(hw, PTC255);
4402     temp = E1000_READ_REG(hw, PTC511);
4403     temp = E1000_READ_REG(hw, PTC1023);
4404     temp = E1000_READ_REG(hw, PTC1522);
4405     temp = E1000_READ_REG(hw, MPTC);
4406     temp = E1000_READ_REG(hw, BPTC);
4407
4408     if(hw->mac_type < e1000_82543) return;
4409
4410     temp = E1000_READ_REG(hw, ALGNERRC);
4411     temp = E1000_READ_REG(hw, RXERRC);
4412     temp = E1000_READ_REG(hw, TNCRS);
4413     temp = E1000_READ_REG(hw, CEXTERR);
4414     temp = E1000_READ_REG(hw, TSCTC);
4415     temp = E1000_READ_REG(hw, TSCTFC);
4416
4417     if(hw->mac_type <= e1000_82544) return;
4418
4419     temp = E1000_READ_REG(hw, MGTPRC);
4420     temp = E1000_READ_REG(hw, MGTPDC);
4421     temp = E1000_READ_REG(hw, MGTPTC);
4422 }
4423
4424 /******************************************************************************
4425  * Resets Adaptive IFS to its default state.
4426  *
4427  * hw - Struct containing variables accessed by shared code
4428  *
4429  * Call this after e1000_init_hw. You may override the IFS defaults by setting
4430  * hw->ifs_params_forced to TRUE. However, you must initialize hw->
4431  * current_ifs_val, ifs_min_val, ifs_max_val, ifs_step_size, and ifs_ratio
4432  * before calling this function.
4433  *****************************************************************************/
4434 void
4435 e1000_reset_adaptive(struct e1000_hw *hw)
4436 {
4437     DEBUGFUNC("e1000_reset_adaptive");
4438
4439     if(hw->adaptive_ifs) {
4440         if(!hw->ifs_params_forced) {
4441             hw->current_ifs_val = 0;
4442             hw->ifs_min_val = IFS_MIN;
4443             hw->ifs_max_val = IFS_MAX;
4444             hw->ifs_step_size = IFS_STEP;
4445             hw->ifs_ratio = IFS_RATIO;
4446         }
4447         hw->in_ifs_mode = FALSE;
4448         E1000_WRITE_REG(hw, AIT, 0);
4449     } else {
4450         DEBUGOUT("Not in Adaptive IFS mode!\n");
4451     }
4452 }
4453
4454 /******************************************************************************
4455  * Called during the callback/watchdog routine to update IFS value based on
4456  * the ratio of transmits to collisions.
4457  *
4458  * hw - Struct containing variables accessed by shared code
4459  * tx_packets - Number of transmits since last callback
4460  * total_collisions - Number of collisions since last callback
4461  *****************************************************************************/
4462 void
4463 e1000_update_adaptive(struct e1000_hw *hw)
4464 {
4465     DEBUGFUNC("e1000_update_adaptive");
4466
4467     if(hw->adaptive_ifs) {
4468         if((hw->collision_delta * hw->ifs_ratio) > hw->tx_packet_delta) {
4469             if(hw->tx_packet_delta > MIN_NUM_XMITS) {
4470                 hw->in_ifs_mode = TRUE;
4471                 if(hw->current_ifs_val < hw->ifs_max_val) {
4472                     if(hw->current_ifs_val == 0)
4473                         hw->current_ifs_val = hw->ifs_min_val;
4474                     else
4475                         hw->current_ifs_val += hw->ifs_step_size;
4476                     E1000_WRITE_REG(hw, AIT, hw->current_ifs_val);
4477                 }
4478             }
4479         } else {
4480             if(hw->in_ifs_mode && (hw->tx_packet_delta <= MIN_NUM_XMITS)) {
4481                 hw->current_ifs_val = 0;
4482                 hw->in_ifs_mode = FALSE;
4483                 E1000_WRITE_REG(hw, AIT, 0);
4484             }
4485         }
4486     } else {
4487         DEBUGOUT("Not in Adaptive IFS mode!\n");
4488     }
4489 }
4490
4491 /******************************************************************************
4492  * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
4493  *
4494  * hw - Struct containing variables accessed by shared code
4495  * frame_len - The length of the frame in question
4496  * mac_addr - The Ethernet destination address of the frame in question
4497  *****************************************************************************/
4498 void
4499 e1000_tbi_adjust_stats(struct e1000_hw *hw,
4500                        struct e1000_hw_stats *stats,
4501                        uint32_t frame_len,
4502                        uint8_t *mac_addr)
4503 {
4504     uint64_t carry_bit;
4505
4506     /* First adjust the frame length. */
4507     frame_len--;
4508     /* We need to adjust the statistics counters, since the hardware
4509      * counters overcount this packet as a CRC error and undercount
4510      * the packet as a good packet
4511      */
4512     /* This packet should not be counted as a CRC error.    */
4513     stats->crcerrs--;
4514     /* This packet does count as a Good Packet Received.    */
4515     stats->gprc++;
4516
4517     /* Adjust the Good Octets received counters             */
4518     carry_bit = 0x80000000 & stats->gorcl;
4519     stats->gorcl += frame_len;
4520     /* If the high bit of Gorcl (the low 32 bits of the Good Octets
4521      * Received Count) was one before the addition,
4522      * AND it is zero after, then we lost the carry out,
4523      * need to add one to Gorch (Good Octets Received Count High).
4524      * This could be simplified if all environments supported
4525      * 64-bit integers.
4526      */
4527     if(carry_bit && ((stats->gorcl & 0x80000000) == 0))
4528         stats->gorch++;
4529     /* Is this a broadcast or multicast?  Check broadcast first,
4530      * since the test for a multicast frame will test positive on
4531      * a broadcast frame.
4532      */
4533     if((mac_addr[0] == (uint8_t) 0xff) && (mac_addr[1] == (uint8_t) 0xff))
4534         /* Broadcast packet */
4535         stats->bprc++;
4536     else if(*mac_addr & 0x01)
4537         /* Multicast packet */
4538         stats->mprc++;
4539
4540     if(frame_len == hw->max_frame_size) {
4541         /* In this case, the hardware has overcounted the number of
4542          * oversize frames.
4543          */
4544         if(stats->roc > 0)
4545             stats->roc--;
4546     }
4547
4548     /* Adjust the bin counters when the extra byte put the frame in the
4549      * wrong bin. Remember that the frame_len was adjusted above.
4550      */
4551     if(frame_len == 64) {
4552         stats->prc64++;
4553         stats->prc127--;
4554     } else if(frame_len == 127) {
4555         stats->prc127++;
4556         stats->prc255--;
4557     } else if(frame_len == 255) {
4558         stats->prc255++;
4559         stats->prc511--;
4560     } else if(frame_len == 511) {
4561         stats->prc511++;
4562         stats->prc1023--;
4563     } else if(frame_len == 1023) {
4564         stats->prc1023++;
4565         stats->prc1522--;
4566     } else if(frame_len == 1522) {
4567         stats->prc1522++;
4568     }
4569 }
4570
4571 /******************************************************************************
4572  * Gets the current PCI bus type, speed, and width of the hardware
4573  *
4574  * hw - Struct containing variables accessed by shared code
4575  *****************************************************************************/
4576 void
4577 e1000_get_bus_info(struct e1000_hw *hw)
4578 {
4579     uint32_t status;
4580
4581     if(hw->mac_type < e1000_82543) {
4582         hw->bus_type = e1000_bus_type_unknown;
4583         hw->bus_speed = e1000_bus_speed_unknown;
4584         hw->bus_width = e1000_bus_width_unknown;
4585         return;
4586     }
4587
4588     status = E1000_READ_REG(hw, STATUS);
4589     hw->bus_type = (status & E1000_STATUS_PCIX_MODE) ?
4590                    e1000_bus_type_pcix : e1000_bus_type_pci;
4591
4592     if(hw->device_id == E1000_DEV_ID_82546EB_QUAD_COPPER) {
4593         hw->bus_speed = (hw->bus_type == e1000_bus_type_pci) ?
4594                         e1000_bus_speed_66 : e1000_bus_speed_120;
4595     } else if(hw->bus_type == e1000_bus_type_pci) {
4596         hw->bus_speed = (status & E1000_STATUS_PCI66) ?
4597                         e1000_bus_speed_66 : e1000_bus_speed_33;
4598     } else {
4599         switch (status & E1000_STATUS_PCIX_SPEED) {
4600         case E1000_STATUS_PCIX_SPEED_66:
4601             hw->bus_speed = e1000_bus_speed_66;
4602             break;
4603         case E1000_STATUS_PCIX_SPEED_100:
4604             hw->bus_speed = e1000_bus_speed_100;
4605             break;
4606         case E1000_STATUS_PCIX_SPEED_133:
4607             hw->bus_speed = e1000_bus_speed_133;
4608             break;
4609         default:
4610             hw->bus_speed = e1000_bus_speed_reserved;
4611             break;
4612         }
4613     }
4614     hw->bus_width = (status & E1000_STATUS_BUS64) ?
4615                     e1000_bus_width_64 : e1000_bus_width_32;
4616 }
4617 /******************************************************************************
4618  * Reads a value from one of the devices registers using port I/O (as opposed
4619  * memory mapped I/O). Only 82544 and newer devices support port I/O.
4620  *
4621  * hw - Struct containing variables accessed by shared code
4622  * offset - offset to read from
4623  *****************************************************************************/
4624 uint32_t
4625 e1000_read_reg_io(struct e1000_hw *hw,
4626                   uint32_t offset)
4627 {
4628     unsigned long io_addr = hw->io_base;
4629     unsigned long io_data = hw->io_base + 4;
4630
4631     e1000_io_write(hw, io_addr, offset);
4632     return e1000_io_read(hw, io_data);
4633 }
4634
4635 /******************************************************************************
4636  * Writes a value to one of the devices registers using port I/O (as opposed to
4637  * memory mapped I/O). Only 82544 and newer devices support port I/O.
4638  *
4639  * hw - Struct containing variables accessed by shared code
4640  * offset - offset to write to
4641  * value - value to write
4642  *****************************************************************************/
4643 void
4644 e1000_write_reg_io(struct e1000_hw *hw,
4645                    uint32_t offset,
4646                    uint32_t value)
4647 {
4648     unsigned long io_addr = hw->io_base;
4649     unsigned long io_data = hw->io_base + 4;
4650
4651     e1000_io_write(hw, io_addr, offset);
4652     e1000_io_write(hw, io_data, value);
4653 }
4654
4655
4656 /******************************************************************************
4657  * Estimates the cable length.
4658  *
4659  * hw - Struct containing variables accessed by shared code
4660  * min_length - The estimated minimum length
4661  * max_length - The estimated maximum length
4662  *
4663  * returns: - E1000_ERR_XXX
4664  *            E1000_SUCCESS
4665  *
4666  * This function always returns a ranged length (minimum & maximum).
4667  * So for M88 phy's, this function interprets the one value returned from the
4668  * register to the minimum and maximum range.
4669  * For IGP phy's, the function calculates the range by the AGC registers.
4670  *****************************************************************************/
4671 int32_t
4672 e1000_get_cable_length(struct e1000_hw *hw,
4673                        uint16_t *min_length,
4674                        uint16_t *max_length)
4675 {
4676     int32_t ret_val;
4677     uint16_t agc_value = 0;
4678     uint16_t cur_agc, min_agc = IGP01E1000_AGC_LENGTH_TABLE_SIZE;
4679     uint16_t i, phy_data;
4680
4681     DEBUGFUNC("e1000_get_cable_length");
4682
4683     *min_length = *max_length = 0;
4684
4685     /* Use old method for Phy older than IGP */
4686     if(hw->phy_type == e1000_phy_m88) {
4687         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
4688                                      &phy_data);
4689         if(ret_val)
4690             return ret_val;
4691
4692         /* Convert the enum value to ranged values */
4693         switch((phy_data & M88E1000_PSSR_CABLE_LENGTH) >>
4694                M88E1000_PSSR_CABLE_LENGTH_SHIFT) {
4695         case e1000_cable_length_50:
4696             *min_length = 0;
4697             *max_length = e1000_igp_cable_length_50;
4698             break;
4699         case e1000_cable_length_50_80:
4700             *min_length = e1000_igp_cable_length_50;
4701             *max_length = e1000_igp_cable_length_80;
4702             break;
4703         case e1000_cable_length_80_110:
4704             *min_length = e1000_igp_cable_length_80;
4705             *max_length = e1000_igp_cable_length_110;
4706             break;
4707         case e1000_cable_length_110_140:
4708             *min_length = e1000_igp_cable_length_110;
4709             *max_length = e1000_igp_cable_length_140;
4710             break;
4711         case e1000_cable_length_140:
4712             *min_length = e1000_igp_cable_length_140;
4713             *max_length = e1000_igp_cable_length_170;
4714             break;
4715         default:
4716             return -E1000_ERR_PHY;
4717             break;
4718         }
4719     } else if(hw->phy_type == e1000_phy_igp) { /* For IGP PHY */
4720         uint16_t agc_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
4721                                                          {IGP01E1000_PHY_AGC_A,
4722                                                           IGP01E1000_PHY_AGC_B,
4723                                                           IGP01E1000_PHY_AGC_C,
4724                                                           IGP01E1000_PHY_AGC_D};
4725         /* Read the AGC registers for all channels */
4726         for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
4727
4728             ret_val = e1000_read_phy_reg(hw, agc_reg_array[i], &phy_data);
4729             if(ret_val)
4730                 return ret_val;
4731
4732             cur_agc = phy_data >> IGP01E1000_AGC_LENGTH_SHIFT;
4733
4734             /* Array bound check. */
4735             if((cur_agc >= IGP01E1000_AGC_LENGTH_TABLE_SIZE - 1) ||
4736                (cur_agc == 0))
4737                 return -E1000_ERR_PHY;
4738
4739             agc_value += cur_agc;
4740
4741             /* Update minimal AGC value. */
4742             if(min_agc > cur_agc)
4743                 min_agc = cur_agc;
4744         }
4745
4746         /* Remove the minimal AGC result for length < 50m */
4747         if(agc_value < IGP01E1000_PHY_CHANNEL_NUM * e1000_igp_cable_length_50) {
4748             agc_value -= min_agc;
4749
4750             /* Get the average length of the remaining 3 channels */
4751             agc_value /= (IGP01E1000_PHY_CHANNEL_NUM - 1);
4752         } else {
4753             /* Get the average length of all the 4 channels. */
4754             agc_value /= IGP01E1000_PHY_CHANNEL_NUM;
4755         }
4756
4757         /* Set the range of the calculated length. */
4758         *min_length = ((e1000_igp_cable_length_table[agc_value] -
4759                        IGP01E1000_AGC_RANGE) > 0) ?
4760                        (e1000_igp_cable_length_table[agc_value] -
4761                        IGP01E1000_AGC_RANGE) : 0;
4762         *max_length = e1000_igp_cable_length_table[agc_value] +
4763                       IGP01E1000_AGC_RANGE;
4764     }
4765
4766     return E1000_SUCCESS;
4767 }
4768
4769 /******************************************************************************
4770  * Check the cable polarity
4771  *
4772  * hw - Struct containing variables accessed by shared code
4773  * polarity - output parameter : 0 - Polarity is not reversed
4774  *                               1 - Polarity is reversed.
4775  *
4776  * returns: - E1000_ERR_XXX
4777  *            E1000_SUCCESS
4778  *
4779  * For phy's older then IGP, this function simply reads the polarity bit in the
4780  * Phy Status register.  For IGP phy's, this bit is valid only if link speed is
4781  * 10 Mbps.  If the link speed is 100 Mbps there is no polarity so this bit will
4782  * return 0.  If the link speed is 1000 Mbps the polarity status is in the
4783  * IGP01E1000_PHY_PCS_INIT_REG.
4784  *****************************************************************************/
4785 int32_t
4786 e1000_check_polarity(struct e1000_hw *hw,
4787                      uint16_t *polarity)
4788 {
4789     int32_t ret_val;
4790     uint16_t phy_data;
4791
4792     DEBUGFUNC("e1000_check_polarity");
4793
4794     if(hw->phy_type == e1000_phy_m88) {
4795         /* return the Polarity bit in the Status register. */
4796         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
4797                                      &phy_data);
4798         if(ret_val)
4799             return ret_val;
4800         *polarity = (phy_data & M88E1000_PSSR_REV_POLARITY) >>
4801                     M88E1000_PSSR_REV_POLARITY_SHIFT;
4802     } else if(hw->phy_type == e1000_phy_igp) {
4803         /* Read the Status register to check the speed */
4804         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_STATUS,
4805                                      &phy_data);
4806         if(ret_val)
4807             return ret_val;
4808
4809         /* If speed is 1000 Mbps, must read the IGP01E1000_PHY_PCS_INIT_REG to
4810          * find the polarity status */
4811         if((phy_data & IGP01E1000_PSSR_SPEED_MASK) ==
4812            IGP01E1000_PSSR_SPEED_1000MBPS) {
4813
4814             /* Read the GIG initialization PCS register (0x00B4) */
4815             ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PCS_INIT_REG,
4816                                          &phy_data);
4817             if(ret_val)
4818                 return ret_val;
4819
4820             /* Check the polarity bits */
4821             *polarity = (phy_data & IGP01E1000_PHY_POLARITY_MASK) ? 1 : 0;
4822         } else {
4823             /* For 10 Mbps, read the polarity bit in the status register. (for
4824              * 100 Mbps this bit is always 0) */
4825             *polarity = phy_data & IGP01E1000_PSSR_POLARITY_REVERSED;
4826         }
4827     }
4828     return E1000_SUCCESS;
4829 }
4830
4831 /******************************************************************************
4832  * Check if Downshift occured
4833  *
4834  * hw - Struct containing variables accessed by shared code
4835  * downshift - output parameter : 0 - No Downshift ocured.
4836  *                                1 - Downshift ocured.
4837  *
4838  * returns: - E1000_ERR_XXX
4839  *            E1000_SUCCESS 
4840  *
4841  * For phy's older then IGP, this function reads the Downshift bit in the Phy
4842  * Specific Status register.  For IGP phy's, it reads the Downgrade bit in the
4843  * Link Health register.  In IGP this bit is latched high, so the driver must
4844  * read it immediately after link is established.
4845  *****************************************************************************/
4846 int32_t
4847 e1000_check_downshift(struct e1000_hw *hw)
4848 {
4849     int32_t ret_val;
4850     uint16_t phy_data;
4851
4852     DEBUGFUNC("e1000_check_downshift");
4853
4854     if(hw->phy_type == e1000_phy_igp) {
4855         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_LINK_HEALTH,
4856                                      &phy_data);
4857         if(ret_val)
4858             return ret_val;
4859
4860         hw->speed_downgraded = (phy_data & IGP01E1000_PLHR_SS_DOWNGRADE) ? 1 : 0;
4861     }
4862     else if(hw->phy_type == e1000_phy_m88) {
4863         ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_SPEC_STATUS,
4864                                      &phy_data);
4865         if(ret_val)
4866             return ret_val;
4867
4868         hw->speed_downgraded = (phy_data & M88E1000_PSSR_DOWNSHIFT) >>
4869                                M88E1000_PSSR_DOWNSHIFT_SHIFT;
4870     }
4871     return E1000_SUCCESS;
4872 }
4873
4874 /*****************************************************************************
4875  *
4876  * 82541_rev_2 & 82547_rev_2 have the capability to configure the DSP when a
4877  * gigabit link is achieved to improve link quality.
4878  *
4879  * hw: Struct containing variables accessed by shared code
4880  *
4881  * returns: - E1000_ERR_PHY if fail to read/write the PHY
4882  *            E1000_SUCCESS at any other case.
4883  *
4884  ****************************************************************************/
4885
4886 int32_t
4887 e1000_config_dsp_after_link_change(struct e1000_hw *hw,
4888                                    boolean_t link_up)
4889 {
4890     int32_t ret_val;
4891     uint16_t phy_data, speed, duplex, i;
4892     uint16_t dsp_reg_array[IGP01E1000_PHY_CHANNEL_NUM] =
4893                                         {IGP01E1000_PHY_AGC_PARAM_A,
4894                                         IGP01E1000_PHY_AGC_PARAM_B,
4895                                         IGP01E1000_PHY_AGC_PARAM_C,
4896                                         IGP01E1000_PHY_AGC_PARAM_D};
4897     uint16_t min_length, max_length;
4898
4899     DEBUGFUNC("e1000_config_dsp_after_link_change");
4900
4901     if(hw->phy_type != e1000_phy_igp)
4902         return E1000_SUCCESS;
4903
4904     if(link_up) {
4905         ret_val = e1000_get_speed_and_duplex(hw, &speed, &duplex);
4906         if(ret_val) {
4907             DEBUGOUT("Error getting link speed and duplex\n");
4908             return ret_val;
4909         }
4910
4911         if(speed == SPEED_1000) {
4912
4913             e1000_get_cable_length(hw, &min_length, &max_length);
4914
4915             if((hw->dsp_config_state == e1000_dsp_config_enabled) &&
4916                 min_length >= e1000_igp_cable_length_50) {
4917
4918                 for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
4919                     ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i],
4920                                                  &phy_data);
4921                     if(ret_val)
4922                         return ret_val;
4923
4924                     phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
4925
4926                     ret_val = e1000_write_phy_reg(hw, dsp_reg_array[i],
4927                                                   phy_data);
4928                     if(ret_val)
4929                         return ret_val;
4930                 }
4931                 hw->dsp_config_state = e1000_dsp_config_activated;
4932             }
4933
4934             if((hw->ffe_config_state == e1000_ffe_config_enabled) &&
4935                (min_length < e1000_igp_cable_length_50)) {
4936
4937                 uint16_t ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_20;
4938                 uint32_t idle_errs = 0;
4939
4940                 /* clear previous idle error counts */
4941                 ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
4942                                              &phy_data);
4943                 if(ret_val)
4944                     return ret_val;
4945
4946                 for(i = 0; i < ffe_idle_err_timeout; i++) {
4947                     udelay(1000);
4948                     ret_val = e1000_read_phy_reg(hw, PHY_1000T_STATUS,
4949                                                  &phy_data);
4950                     if(ret_val)
4951                         return ret_val;
4952
4953                     idle_errs += (phy_data & SR_1000T_IDLE_ERROR_CNT);
4954                     if(idle_errs > SR_1000T_PHY_EXCESSIVE_IDLE_ERR_COUNT) {
4955                         hw->ffe_config_state = e1000_ffe_config_active;
4956
4957                         ret_val = e1000_write_phy_reg(hw,
4958                                     IGP01E1000_PHY_DSP_FFE,
4959                                     IGP01E1000_PHY_DSP_FFE_CM_CP);
4960                         if(ret_val)
4961                             return ret_val;
4962                         break;
4963                     }
4964
4965                     if(idle_errs)
4966                         ffe_idle_err_timeout = FFE_IDLE_ERR_COUNT_TIMEOUT_100;
4967                 }
4968             }
4969         }
4970     } else {
4971         if(hw->dsp_config_state == e1000_dsp_config_activated) {
4972             ret_val = e1000_write_phy_reg(hw, 0x0000,
4973                                           IGP01E1000_IEEE_FORCE_GIGA);
4974             if(ret_val)
4975                 return ret_val;
4976             for(i = 0; i < IGP01E1000_PHY_CHANNEL_NUM; i++) {
4977                 ret_val = e1000_read_phy_reg(hw, dsp_reg_array[i], &phy_data);
4978                 if(ret_val)
4979                     return ret_val;
4980
4981                 phy_data &= ~IGP01E1000_PHY_EDAC_MU_INDEX;
4982                 phy_data |=  IGP01E1000_PHY_EDAC_SIGN_EXT_9_BITS;
4983
4984                 ret_val = e1000_write_phy_reg(hw,dsp_reg_array[i], phy_data);
4985                 if(ret_val)
4986                     return ret_val;
4987             }
4988
4989             ret_val = e1000_write_phy_reg(hw, 0x0000,
4990                                           IGP01E1000_IEEE_RESTART_AUTONEG);
4991             if(ret_val)
4992                 return ret_val;
4993
4994             hw->dsp_config_state = e1000_dsp_config_enabled;
4995         }
4996
4997         if(hw->ffe_config_state == e1000_ffe_config_active) {
4998             ret_val = e1000_write_phy_reg(hw, 0x0000,
4999                                           IGP01E1000_IEEE_FORCE_GIGA);
5000             if(ret_val)
5001                 return ret_val;
5002             ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_DSP_FFE,
5003                                           IGP01E1000_PHY_DSP_FFE_DEFAULT);
5004             if(ret_val)
5005                 return ret_val;
5006
5007             ret_val = e1000_write_phy_reg(hw, 0x0000,
5008                                           IGP01E1000_IEEE_RESTART_AUTONEG);
5009             if(ret_val)
5010                 return ret_val;
5011             hw->ffe_config_state = e1000_ffe_config_enabled;
5012         }
5013     }
5014     return E1000_SUCCESS;
5015 }
5016
5017 /*****************************************************************************
5018  * Set PHY to class A mode
5019  * Assumes the following operations will follow to enable the new class mode.
5020  *  1. Do a PHY soft reset
5021  *  2. Restart auto-negotiation or force link.
5022  *
5023  * hw - Struct containing variables accessed by shared code
5024  ****************************************************************************/
5025 static int32_t
5026 e1000_set_phy_mode(struct e1000_hw *hw)
5027 {
5028     int32_t ret_val;
5029     uint16_t eeprom_data;
5030
5031     DEBUGFUNC("e1000_set_phy_mode");
5032
5033     if((hw->mac_type == e1000_82545_rev_3) &&
5034        (hw->media_type == e1000_media_type_copper)) {
5035         ret_val = e1000_read_eeprom(hw, EEPROM_PHY_CLASS_WORD, 1, &eeprom_data);
5036         if(ret_val) {
5037             return ret_val;
5038         }
5039
5040         if((eeprom_data != EEPROM_RESERVED_WORD) &&
5041            (eeprom_data & EEPROM_PHY_CLASS_A)) {
5042             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x000B);
5043             if(ret_val)
5044                 return ret_val;
5045             ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, 0x8104);
5046             if(ret_val)
5047                 return ret_val;
5048
5049             hw->phy_reset_disable = FALSE;
5050         }
5051     }
5052
5053     return E1000_SUCCESS;
5054 }
5055
5056 /*****************************************************************************
5057  *
5058  * This function sets the lplu state according to the active flag.  When
5059  * activating lplu this function also disables smart speed and vise versa.
5060  * lplu will not be activated unless the device autonegotiation advertisment
5061  * meets standards of either 10 or 10/100 or 10/100/1000 at all duplexes.
5062  * hw: Struct containing variables accessed by shared code
5063  * active - true to enable lplu false to disable lplu.
5064  *
5065  * returns: - E1000_ERR_PHY if fail to read/write the PHY
5066  *            E1000_SUCCESS at any other case.
5067  *
5068  ****************************************************************************/
5069
5070 int32_t
5071 e1000_set_d3_lplu_state(struct e1000_hw *hw,
5072                         boolean_t active)
5073 {
5074     int32_t ret_val;
5075     uint16_t phy_data;
5076     DEBUGFUNC("e1000_set_d3_lplu_state");
5077
5078     if(!((hw->mac_type == e1000_82541_rev_2) ||
5079          (hw->mac_type == e1000_82547_rev_2)))
5080         return E1000_SUCCESS;
5081
5082     /* During driver activity LPLU should not be used or it will attain link
5083      * from the lowest speeds starting from 10Mbps. The capability is used for
5084      * Dx transitions and states */
5085     ret_val = e1000_read_phy_reg(hw, IGP01E1000_GMII_FIFO, &phy_data);
5086     if(ret_val)
5087         return ret_val;
5088
5089     if(!active) {
5090         phy_data &= ~IGP01E1000_GMII_FLEX_SPD;
5091         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
5092         if(ret_val)
5093             return ret_val;
5094
5095         /* LPLU and SmartSpeed are mutually exclusive.  LPLU is used during
5096          * Dx states where the power conservation is most important.  During
5097          * driver activity we should enable SmartSpeed, so performance is
5098          * maintained. */
5099         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
5100         if(ret_val)
5101             return ret_val;
5102
5103         phy_data |= IGP01E1000_PSCFR_SMART_SPEED;
5104         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
5105         if(ret_val)
5106             return ret_val;
5107
5108     } else if((hw->autoneg_advertised == AUTONEG_ADVERTISE_SPEED_DEFAULT) ||
5109               (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_ALL ) ||
5110               (hw->autoneg_advertised == AUTONEG_ADVERTISE_10_100_ALL)) {
5111
5112         phy_data |= IGP01E1000_GMII_FLEX_SPD;
5113         ret_val = e1000_write_phy_reg(hw, IGP01E1000_GMII_FIFO, phy_data);
5114         if(ret_val)
5115             return ret_val;
5116
5117         /* When LPLU is enabled we should disable SmartSpeed */
5118         ret_val = e1000_read_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, &phy_data);
5119         if(ret_val)
5120             return ret_val;
5121
5122         phy_data &= ~IGP01E1000_PSCFR_SMART_SPEED;
5123         ret_val = e1000_write_phy_reg(hw, IGP01E1000_PHY_PORT_CONFIG, phy_data);
5124         if(ret_val)
5125             return ret_val;
5126
5127     }
5128     return E1000_SUCCESS;
5129 }
5130
5131 /******************************************************************************
5132  * Change VCO speed register to improve Bit Error Rate performance of SERDES.
5133  *
5134  * hw - Struct containing variables accessed by shared code
5135  *****************************************************************************/
5136 static int32_t
5137 e1000_set_vco_speed(struct e1000_hw *hw)
5138 {
5139     int32_t  ret_val;
5140     uint16_t default_page = 0;
5141     uint16_t phy_data;
5142
5143     DEBUGFUNC("e1000_set_vco_speed");
5144
5145     switch(hw->mac_type) {
5146     case e1000_82545_rev_3:
5147     case e1000_82546_rev_3:
5148        break;
5149     default:
5150         return E1000_SUCCESS;
5151     }
5152
5153     /* Set PHY register 30, page 5, bit 8 to 0 */
5154
5155     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, &default_page);
5156     if(ret_val)
5157         return ret_val;
5158
5159     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0005);
5160     if(ret_val)
5161         return ret_val;
5162
5163     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
5164     if(ret_val)
5165         return ret_val;
5166
5167     phy_data &= ~M88E1000_PHY_VCO_REG_BIT8;
5168     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
5169     if(ret_val)
5170         return ret_val;
5171
5172     /* Set PHY register 30, page 4, bit 11 to 1 */
5173
5174     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, 0x0004);
5175     if(ret_val)
5176         return ret_val;
5177
5178     ret_val = e1000_read_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, &phy_data);
5179     if(ret_val)
5180         return ret_val;
5181
5182     phy_data |= M88E1000_PHY_VCO_REG_BIT11;
5183     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_GEN_CONTROL, phy_data);
5184     if(ret_val)
5185         return ret_val;
5186
5187     ret_val = e1000_write_phy_reg(hw, M88E1000_PHY_PAGE_SELECT, default_page);
5188     if(ret_val)
5189         return ret_val;
5190
5191     return E1000_SUCCESS;
5192 }
5193
5194 /******************************************************************************
5195  * Verifies the hardware needs to allow ARPs to be processed by the host
5196  *
5197  * hw - Struct containing variables accessed by shared code
5198  *
5199  * returns: - TRUE/FALSE
5200  *
5201  *****************************************************************************/
5202 uint32_t
5203 e1000_enable_mng_pass_thru(struct e1000_hw *hw)
5204 {
5205     uint32_t manc;
5206
5207     if (hw->asf_firmware_present) {
5208         manc = E1000_READ_REG(hw, MANC);
5209
5210         if (!(manc & E1000_MANC_RCV_TCO_EN) ||
5211             !(manc & E1000_MANC_EN_MAC_ADDR_FILTER))
5212             return FALSE;
5213         if ((manc & E1000_MANC_SMBUS_EN) && !(manc & E1000_MANC_ASF_EN))
5214             return TRUE;
5215     }
5216     return FALSE;
5217 }
5218