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