Linux preempt-rt

Check our new training course

Real-Time Linux with PREEMPT_RT

Check our new training course
with Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
/*******************************************************************************

  This software program is available to you under a choice of one of two
  licenses. You may choose to be licensed under either the GNU General Public
  License (GPL) Version 2, June 1991, available at
  http://www.fsf.org/copyleft/gpl.html, or the Intel BSD + Patent License, the
  text of which follows:
  
  Recipient has requested a license and Intel Corporation ("Intel") is willing
  to grant a license for the software entitled Linux Base Driver for the
  Intel(R) PRO/1000 Family of Adapters (e1000) (the "Software") being provided
  by Intel Corporation. The following definitions apply to this license:
  
  "Licensed Patents" means patent claims licensable by Intel Corporation which
  are necessarily infringed by the use of sale of the Software alone or when
  combined with the operating system referred to below.
  
  "Recipient" means the party to whom Intel delivers this Software.
  
  "Licensee" means Recipient and those third parties that receive a license to
  any operating system available under the GNU Public License version 2.0 or
  later.
  
  Copyright (c) 1999 - 2002 Intel Corporation.
  All rights reserved.
  
  The license is provided to Recipient and Recipient's Licensees under the
  following terms.
  
  Redistribution and use in source and binary forms of the Software, with or
  without modification, are permitted provided that the following conditions
  are met:
  
  Redistributions of source code of the Software may retain the above
  copyright notice, this list of conditions and the following disclaimer.
  
  Redistributions in binary form of the Software may reproduce the above
  copyright notice, this list of conditions and the following disclaimer in
  the documentation and/or materials provided with the distribution.
  
  Neither the name of Intel Corporation nor the names of its contributors
  shall be used to endorse or promote products derived from this Software
  without specific prior written permission.
  
  Intel hereby grants Recipient and Licensees a non-exclusive, worldwide,
  royalty-free patent license under Licensed Patents to make, use, sell, offer
  to sell, import and otherwise transfer the Software, if any, in source code
  and object code form. This license shall include changes to the Software
  that are error corrections or other minor changes to the Software that do
  not add functionality or features when the Software is incorporated in any
  version of an operating system that has been distributed under the GNU
  General Public License 2.0 or later. This patent license shall apply to the
  combination of the Software and any operating system licensed under the GNU
  Public License version 2.0 or later if, at the time Intel provides the
  Software to Recipient, such addition of the Software to the then publicly
  available versions of such operating systems available under the GNU Public
  License version 2.0 or later (whether in gold, beta or alpha form) causes
  such combination to be covered by the Licensed Patents. The patent license
  shall not apply to any other combinations which include the Software. NO
  hardware per se is licensed hereunder.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MECHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR IT CONTRIBUTORS BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  ANY LOSS OF USE; DATA, OR PROFITS; OR BUSINESS INTERUPTION) HOWEVER CAUSED
  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

/* e1000_mac.c
 * Shared functions for accessing and configuring the MAC
 */

#include "e1000_mac.h"
#include "e1000_phy.h"

/******************************************************************************
 * Raises the EEPROM's clock input.
 *
 * shared - Struct containing variables accessed by shared code
 * eecd_reg - EECD's current value
 *****************************************************************************/
static void
e1000_raise_clock(struct e1000_shared_adapter *shared,
                  uint32_t *eecd_reg)
{
    /* Raise the clock input to the EEPROM (by setting the SK bit), and then
     * wait 50 microseconds.
     */
    *eecd_reg = *eecd_reg | E1000_EECD_SK;
    E1000_WRITE_REG(shared, EECD, *eecd_reg);
    usec_delay(50);
    return;
}

/******************************************************************************
 * Lowers the EEPROM's clock input.
 *
 * shared - Struct containing variables accessed by shared code 
 * eecd_reg - EECD's current value
 *****************************************************************************/
static void
e1000_lower_clock(struct e1000_shared_adapter *shared,
                  uint32_t *eecd_reg)
{
    /* Lower the clock input to the EEPROM (by clearing the SK bit), and then 
     * wait 50 microseconds. 
     */
    *eecd_reg = *eecd_reg & ~E1000_EECD_SK;
    E1000_WRITE_REG(shared, EECD, *eecd_reg);
    usec_delay(50);
    return;
}

/******************************************************************************
 * Shift data bits out to the EEPROM.
 *
 * shared - Struct containing variables accessed by shared code
 * data - data to send to the EEPROM
 * count - number of bits to shift out
 *****************************************************************************/
static void
e1000_shift_out_bits(struct e1000_shared_adapter *shared,
                     uint16_t data,
                     uint16_t count)
{
    uint32_t eecd_reg;
    uint32_t mask;

    /* We need to shift "count" bits out to the EEPROM. So, value in the
     * "data" parameter will be shifted out to the EEPROM one bit at a time.
     * In order to do this, "data" must be broken down into bits. 
     */
    mask = 0x01 << (count - 1);
    eecd_reg = E1000_READ_REG(shared, EECD);
    eecd_reg &= ~(E1000_EECD_DO | E1000_EECD_DI);
    do {
        /* A "1" is shifted out to the EEPROM by setting bit "DI" to a "1",
         * and then raising and then lowering the clock (the SK bit controls
         * the clock input to the EEPROM).  A "0" is shifted out to the EEPROM
         * by setting "DI" to "0" and then raising and then lowering the clock.
         */
        eecd_reg &= ~E1000_EECD_DI;

        if(data & mask)
            eecd_reg |= E1000_EECD_DI;

        E1000_WRITE_REG(shared, EECD, eecd_reg);

        usec_delay(50);

        e1000_raise_clock(shared, &eecd_reg);
        e1000_lower_clock(shared, &eecd_reg);

        mask = mask >> 1;

    } while(mask);

    /* We leave the "DI" bit set to "0" when we leave this routine. */
    eecd_reg &= ~E1000_EECD_DI;
    E1000_WRITE_REG(shared, EECD, eecd_reg);
    return;
}

/******************************************************************************
 * Shift data bits in from the EEPROM
 *
 * shared - Struct containing variables accessed by shared code
 *****************************************************************************/
static uint16_t
e1000_shift_in_bits(struct e1000_shared_adapter *shared)
{
    uint32_t eecd_reg;
    uint32_t i;
    uint16_t data;

    /* In order to read a register from the EEPROM, we need to shift 16 bits 
     * in from the EEPROM. Bits are "shifted in" by raising the clock input to
     * the EEPROM (setting the SK bit), and then reading the value of the "DO"
     * bit.  During this "shifting in" process the "DI" bit should always be 
     * clear..
     */

    eecd_reg = E1000_READ_REG(shared, EECD);

    eecd_reg &= ~(E1000_EECD_DO | E1000_EECD_DI);
    data = 0;

    for(i = 0; i < 16; i++) {
        data = data << 1;
        e1000_raise_clock(shared, &eecd_reg);

        eecd_reg = E1000_READ_REG(shared, EECD);

        eecd_reg &= ~(E1000_EECD_DI);
        if(eecd_reg & E1000_EECD_DO)
            data |= 1;

        e1000_lower_clock(shared, &eecd_reg);
    }

    return data;
}

/******************************************************************************
 * Prepares EEPROM for access
 *
 * shared - Struct containing variables accessed by shared code
 *
 * Lowers EEPROM clock. Clears input pin. Sets the chip select pin. This 
 * function should be called before issuing a command to the EEPROM.
 *****************************************************************************/
static void
e1000_setup_eeprom(struct e1000_shared_adapter *shared)
{
    uint32_t eecd_reg;

    eecd_reg = E1000_READ_REG(shared, EECD);

    /* Clear SK and DI */
    eecd_reg &= ~(E1000_EECD_SK | E1000_EECD_DI);
    E1000_WRITE_REG(shared, EECD, eecd_reg);

    /* Set CS */
    eecd_reg |= E1000_EECD_CS;
    E1000_WRITE_REG(shared, EECD, eecd_reg);
    return;
}

/******************************************************************************
 * Returns EEPROM to a "standby" state
 * 
 * shared - Struct containing variables accessed by shared code
 *****************************************************************************/
static void
e1000_standby_eeprom(struct e1000_shared_adapter *shared)
{
    uint32_t eecd_reg;

    eecd_reg = E1000_READ_REG(shared, EECD);

    /* Deselct EEPROM */
    eecd_reg &= ~(E1000_EECD_CS | E1000_EECD_SK);
    E1000_WRITE_REG(shared, EECD, eecd_reg);
    usec_delay(50);

    /* Clock high */
    eecd_reg |= E1000_EECD_SK;
    E1000_WRITE_REG(shared, EECD, eecd_reg);
    usec_delay(50);

    /* Select EEPROM */
    eecd_reg |= E1000_EECD_CS;
    E1000_WRITE_REG(shared, EECD, eecd_reg);
    usec_delay(50);

    /* Clock low */
    eecd_reg &= ~E1000_EECD_SK;
    E1000_WRITE_REG(shared, EECD, eecd_reg);
    usec_delay(50);
    return;
}


/******************************************************************************
 * Forces the MAC's flow control settings.
 * 
 * shared - Struct containing variables accessed by shared code
 *
 * Sets the TFCE and RFCE bits in the device control register to reflect
 * the adapter settings. TFCE and RFCE need to be explicitly set by
 * software when a Copper PHY is used because autonegotiation is managed
 * by the PHY rather than the MAC. Software must also configure these
 * bits when link is forced on a fiber connection.
 *****************************************************************************/
static void
e1000_force_mac_fc(struct e1000_shared_adapter *shared)
{
    uint32_t ctrl_reg;

    DEBUGFUNC("e1000_force_mac_fc");

    /* Get the current configuration of the Device Control Register */
    ctrl_reg = E1000_READ_REG(shared, CTRL);

    /* Because we didn't get link via the internal auto-negotiation
     * mechanism (we either forced link or we got link via PHY
     * auto-neg), we have to manually enable/disable transmit an
     * receive flow control.
     *
     * The "Case" statement below enables/disable flow control
     * according to the "shared->fc" parameter.
     *
     * The possible values of the "fc" parameter are:
     *      0:  Flow control is completely disabled
     *      1:  Rx flow control is enabled (we can receive pause
     *          frames but not send pause frames).
     *      2:  Tx flow control is enabled (we can send pause frames
     *          frames but we do not receive pause frames).
     *      3:  Both Rx and TX flow control (symmetric) is enabled.
     *  other:  No other values should be possible at this point.
     */

    switch (shared->fc) {
    case e1000_fc_none:
        ctrl_reg &= (~(E1000_CTRL_TFCE | E1000_CTRL_RFCE));
        break;
    case e1000_fc_rx_pause:
        ctrl_reg &= (~E1000_CTRL_TFCE);
        ctrl_reg |= E1000_CTRL_RFCE;
        break;
    case e1000_fc_tx_pause:
        ctrl_reg &= (~E1000_CTRL_RFCE);
        ctrl_reg |= E1000_CTRL_TFCE;
        break;
    case e1000_fc_full:
        ctrl_reg |= (E1000_CTRL_TFCE | E1000_CTRL_RFCE);
        break;
    default:
        DEBUGOUT("Flow control param set incorrectly\n");
        ASSERT(0);
        break;
    }

    /* Disable TX Flow Control for 82542 (rev 2.0) */
    if(shared->mac_type == e1000_82542_rev2_0)
        ctrl_reg &= (~E1000_CTRL_TFCE);


    E1000_WRITE_REG(shared, CTRL, ctrl_reg);
    return;
}

/******************************************************************************
 * Reset the transmit and receive units; mask and clear all interrupts.
 *
 * shared - Struct containing variables accessed by shared code
 *****************************************************************************/
void
e1000_adapter_stop(struct e1000_shared_adapter *shared)
{
    uint32_t ctrl_reg;
    uint32_t ctrl_ext_reg;
    uint32_t icr_reg;
    uint16_t pci_cmd_word;

    DEBUGFUNC("e1000_shared_adapter_stop");

    /* If we are stopped or resetting exit gracefully and wait to be
     * started again before accessing the hardware.
     */
    if(shared->adapter_stopped) {
        DEBUGOUT("Exiting because the adapter is already stopped!!!\n");
        return;
    }

    /* Set the Adapter Stopped flag so other driver functions stop
     * touching the Hardware.
     */
    shared->adapter_stopped = TRUE;

    /* For 82542 (rev 2.0), disable MWI before issuing a device reset */
    if(shared->mac_type == e1000_82542_rev2_0) {
        if(shared->pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
            DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");

            pci_cmd_word = shared->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE;

            e1000_write_pci_cfg(shared, PCI_COMMAND_REGISTER, &pci_cmd_word);
        }
    }

    /* Clear interrupt mask to stop board from generating interrupts */
    DEBUGOUT("Masking off all interrupts\n");
    E1000_WRITE_REG(shared, IMC, 0xffffffff);

    /* Disable the Transmit and Receive units.  Then delay to allow
     * any pending transactions to complete before we hit the MAC with
     * the global reset.
     */
    E1000_WRITE_REG(shared, RCTL, 0);
    E1000_WRITE_REG(shared, TCTL, E1000_TCTL_PSP);

    /* The tbi_compatibility_on Flag must be cleared when Rctl is cleared. */
    shared->tbi_compatibility_on = FALSE;

    msec_delay(10);

    /* Issue a global reset to the MAC.  This will reset the chip's
     * transmit, receive, DMA, and link units.  It will not effect
     * the current PCI configuration.  The global reset bit is self-
     * clearing, and should clear within a microsecond.
     */
    DEBUGOUT("Issuing a global reset to MAC\n");
    ctrl_reg = E1000_READ_REG(shared, CTRL);
    E1000_WRITE_REG(shared, CTRL, (ctrl_reg | E1000_CTRL_RST));

    /* Delay a few ms just to allow the reset to complete */
    msec_delay(10);

#if DBG
    /* Make sure the self-clearing global reset bit did self clear */
    ctrl_reg = E1000_READ_REG(shared, CTRL);

    ASSERT(!(ctrl_reg & E1000_CTRL_RST));
#endif

    /* Force a reload from the EEPROM */
    ctrl_ext_reg = E1000_READ_REG(shared, CTRL_EXT);
    ctrl_ext_reg |= E1000_CTRL_EXT_EE_RST;
    E1000_WRITE_REG(shared, CTRL_EXT, ctrl_ext_reg);
    msec_delay(2);
    
    /* Clear interrupt mask to stop board from generating interrupts */
    DEBUGOUT("Masking off all interrupts\n");
    E1000_WRITE_REG(shared, IMC, 0xffffffff);

    /* Clear any pending interrupt events. */
    icr_reg = E1000_READ_REG(shared, ICR);

    /* If MWI was previously enabled, reenable it. */
    if(shared->mac_type == e1000_82542_rev2_0) {
        if(shared->pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
            e1000_write_pci_cfg(shared,
                                PCI_COMMAND_REGISTER, &shared->pci_cmd_word);
        }
    }
    return;
}

/******************************************************************************
 * Performs basic configuration of the adapter.
 *
 * shared - Struct containing variables accessed by shared code
 * 
 * Assumes that the controller has previously been reset and is in a 
 * post-reset uninitialized state. Initializes the receive address registers,
 * multicast table, and VLAN filter table. Calls routines to setup link
 * configuration and flow control settings. Clears all on-chip counters. Leaves
 * the transmit and receive units disabled and uninitialized.
 *****************************************************************************/
boolean_t
e1000_init_hw(struct e1000_shared_adapter *shared)
{
    uint32_t status_reg;
    uint32_t i;
    uint16_t pci_cmd_word;
    boolean_t status;

    DEBUGFUNC("e1000_init_hw");

    /* Set the Media Type and exit with error if it is not valid. */
    if(shared->mac_type != e1000_82543) {
        /* tbi_compatibility is only valid on 82543 */
        shared->tbi_compatibility_en = FALSE;
    }

    if(shared->mac_type >= e1000_82543) {
        status_reg = E1000_READ_REG(shared, STATUS);
        if(status_reg & E1000_STATUS_TBIMODE) {
            shared->media_type = e1000_media_type_fiber;
            /* tbi_compatibility not valid on fiber */
            shared->tbi_compatibility_en = FALSE;
        } else {
            shared->media_type = e1000_media_type_copper;
        }
    } else {
        /* This is an 82542 (fiber only) */
        shared->media_type = e1000_media_type_fiber;
    }

    /* Disabling VLAN filtering. */
    DEBUGOUT("Initializing the IEEE VLAN\n");
    E1000_WRITE_REG(shared, VET, 0);

    e1000_clear_vfta(shared);

    /* For 82542 (rev 2.0), disable MWI and put the receiver into reset */
    if(shared->mac_type == e1000_82542_rev2_0) {
        if(shared->pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
            DEBUGOUT("Disabling MWI on 82542 rev 2.0\n");
            pci_cmd_word = shared->pci_cmd_word & ~CMD_MEM_WRT_INVALIDATE;
            e1000_write_pci_cfg(shared, PCI_COMMAND_REGISTER, &pci_cmd_word);
        }
        E1000_WRITE_REG(shared, RCTL, E1000_RCTL_RST);

        msec_delay(5);
    }

    /* Setup the receive address. This involves initializing all of the Receive
     * Address Registers (RARs 0 - 15).
     */
    e1000_init_rx_addrs(shared);

    /* For 82542 (rev 2.0), take the receiver out of reset and enable MWI */
    if(shared->mac_type == e1000_82542_rev2_0) {
        E1000_WRITE_REG(shared, RCTL, 0);

        msec_delay(1);

        if(shared->pci_cmd_word & CMD_MEM_WRT_INVALIDATE) {
            e1000_write_pci_cfg(shared,
                                PCI_COMMAND_REGISTER, &shared->pci_cmd_word);
        }
    }

    /* Zero out the Multicast HASH table */
    DEBUGOUT("Zeroing the MTA\n");
    for(i = 0; i < E1000_MC_TBL_SIZE; i++)
        E1000_WRITE_REG_ARRAY(shared, MTA, i, 0);

    /* Call a subroutine to configure the link and setup flow control. */
    status = e1000_setup_fc_and_link(shared);

    /* Clear all of the statistics registers (clear on read).  It is
     * important that we do this after we have tried to establish link
     * because the symbol error count will increment wildly if there
     * is no link.
     */
    e1000_clear_hw_cntrs(shared);

    return (status);
}

/******************************************************************************
 * Initializes receive address filters.
 *
 * shared - Struct containing variables accessed by shared code 
 *
 * Places the MAC address in receive address register 0 and clears the rest
 * of the receive addresss registers. Clears the multicast table. Assumes
 * the receiver is in reset when the routine is called.
 *****************************************************************************/
void
e1000_init_rx_addrs(struct e1000_shared_adapter *shared)
{
    uint32_t i;
    uint32_t addr_low;
    uint32_t addr_high;

    DEBUGFUNC("e1000_init_rx_addrs");

    /* Setup the receive address. */
    DEBUGOUT("Programming MAC Address into RAR[0]\n");
    addr_low = (shared->mac_addr[0] |
                (shared->mac_addr[1] << 8) |
                (shared->mac_addr[2] << 16) | (shared->mac_addr[3] << 24));

    addr_high = (shared->mac_addr[4] |
                 (shared->mac_addr[5] << 8) | E1000_RAH_AV);

    E1000_WRITE_REG_ARRAY(shared, RA, 0, addr_low);
    E1000_WRITE_REG_ARRAY(shared, RA, 1, addr_high);

    /* Zero out the other 15 receive addresses. */
    DEBUGOUT("Clearing RAR[1-15]\n");
    for(i = 1; i < E1000_RAR_ENTRIES; i++) {
        E1000_WRITE_REG_ARRAY(shared, RA, (i << 1), 0);
        E1000_WRITE_REG_ARRAY(shared, RA, ((i << 1) + 1), 0);
    }

    return;
}

/******************************************************************************
 * Updates the MAC's list of multicast addresses.
 *
 * shared - Struct containing variables accessed by shared code
 * mc_addr_list - the list of new multicast addresses
 * mc_addr_count - number of addresses
 * pad - number of bytes between addresses in the list
 *
 * The given list replaces any existing list. Clears the last 15 receive
 * address registers and the multicast table. Uses receive address registers
 * for the first 15 multicast addresses, and hashes the rest into the 
 * multicast table.
 *****************************************************************************/
void
e1000_mc_addr_list_update(struct e1000_shared_adapter *shared,
                          uint8_t *mc_addr_list,
                          uint32_t mc_addr_count,
                          uint32_t pad)
{
    uint32_t hash_value;
    uint32_t i;
    uint32_t rar_used_count = 1;        /* RAR[0] is used for our MAC address */

    DEBUGFUNC("e1000_mc_addr_list_update");

    /* Set the new number of MC addresses that we are being requested to use. */
    shared->num_mc_addrs = mc_addr_count;

    /* Clear RAR[1-15] */
    DEBUGOUT(" Clearing RAR[1-15]\n");
    for(i = rar_used_count; i < E1000_RAR_ENTRIES; i++) {
        E1000_WRITE_REG_ARRAY(shared, RA, (i << 1), 0);
        E1000_WRITE_REG_ARRAY(shared, RA, ((i << 1) + 1), 0);
    }

    /* Clear the MTA */
    DEBUGOUT(" Clearing MTA\n");
    for(i = 0; i < E1000_NUM_MTA_REGISTERS; i++) {
        E1000_WRITE_REG_ARRAY(shared, MTA, i, 0);
    }

    /* Add the new addresses */
    for(i = 0; i < mc_addr_count; i++) {
        DEBUGOUT(" Adding the multicast addresses:\n");
        DEBUGOUT7(" MC Addr #%d =%.2X %.2X %.2X %.2X %.2X %.2X\n", i,
                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad)],
                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 1],
                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 2],
                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 3],
                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 4],
                  mc_addr_list[i * (ETH_LENGTH_OF_ADDRESS + pad) + 5]);

        hash_value = e1000_hash_mc_addr(shared,
                                        mc_addr_list +
                                        (i * (ETH_LENGTH_OF_ADDRESS + pad)));

        DEBUGOUT1(" Hash value = 0x%03X\n", hash_value);

        /* Place this multicast address in the RAR if there is room, *
         * else put it in the MTA            
         */
        if(rar_used_count < E1000_RAR_ENTRIES) {
            e1000_rar_set(shared,
                          mc_addr_list + (i * (ETH_LENGTH_OF_ADDRESS + pad)),
                          rar_used_count);
            rar_used_count++;
        } else {
            e1000_mta_set(shared, hash_value);
        }
    }

    DEBUGOUT("MC Update Complete\n");
    return;
}

/******************************************************************************
 * Hashes an address to determine its location in the multicast table
 *
 * shared - Struct containing variables accessed by shared code
 * mc_addr - the multicast address to hash 
 *****************************************************************************/
uint32_t
e1000_hash_mc_addr(struct e1000_shared_adapter *shared,
                   uint8_t *mc_addr)
{
    uint32_t hash_value = 0;

    /* The portion of the address that is used for the hash table is
     * determined by the mc_filter_type setting.  
     */
    switch (shared->mc_filter_type) {
        /* [0] [1] [2] [3] [4] [5]
            * 01  AA  00  12  34  56
            * LSB                 MSB - According to H/W docs */
    case 0:
        /* [47:36] i.e. 0x563 for above example address */
        hash_value = ((mc_addr[4] >> 4) | (((uint16_t) mc_addr[5]) << 4));
        break;
    case 1:                   /* [46:35] i.e. 0xAC6 for above example address */
        hash_value = ((mc_addr[4] >> 3) | (((uint16_t) mc_addr[5]) << 5));
        break;
    case 2:                   /* [45:34] i.e. 0x5D8 for above example address */
        hash_value = ((mc_addr[4] >> 2) | (((uint16_t) mc_addr[5]) << 6));
        break;
    case 3:                   /* [43:32] i.e. 0x634 for above example address */
        hash_value = ((mc_addr[4]) | (((uint16_t) mc_addr[5]) << 8));
        break;
    }

    hash_value &= 0xFFF;
    return (hash_value);
}

/******************************************************************************
 * Sets the bit in the multicast table corresponding to the hash value.
 *
 * shared - Struct containing variables accessed by shared code
 * hash_value - Multicast address hash value
 *****************************************************************************/
void
e1000_mta_set(struct e1000_shared_adapter *shared,
              uint32_t hash_value)
{
    uint32_t hash_bit, hash_reg;
    uint32_t mta_reg;
    uint32_t temp;

    /* The MTA is a register array of 128 32-bit registers.  
     * It is treated like an array of 4096 bits.  We want to set 
     * bit BitArray[hash_value]. So we figure out what register
     * the bit is in, read it, OR in the new bit, then write
     * back the new value.  The register is determined by the 
     * upper 7 bits of the hash value and the bit within that 
     * register are determined by the lower 5 bits of the value.
     */
    hash_reg = (hash_value >> 5) & 0x7F;
    hash_bit = hash_value & 0x1F;

    mta_reg = E1000_READ_REG_ARRAY(shared, MTA, hash_reg);

    mta_reg |= (1 << hash_bit);

    /* If we are on an 82544 and we are trying to write an odd offset
     * in the MTA, save off the previous entry before writing and
     * restore the old value after writing.
     */
    if((shared->mac_type == e1000_82544) && ((hash_reg & 0x1) == 1)) {
        temp = E1000_READ_REG_ARRAY(shared, MTA, (hash_reg - 1));
        E1000_WRITE_REG_ARRAY(shared, MTA, hash_reg, mta_reg);
        E1000_WRITE_REG_ARRAY(shared, MTA, (hash_reg - 1), temp);
    } else {
        E1000_WRITE_REG_ARRAY(shared, MTA, hash_reg, mta_reg);
    }
    return;
}

/******************************************************************************
 * Puts an ethernet address into a receive address register.
 *
 * shared - Struct containing variables accessed by shared code
 * addr - Address to put into receive address register
 * index - Receive address register to write
 *****************************************************************************/
void
e1000_rar_set(struct e1000_shared_adapter *shared,
              uint8_t *addr,
              uint32_t index)
{
    uint32_t rar_low, rar_high;

    /* HW expects these in little endian so we reverse the byte order
     * from network order (big endian) to little endian              
     */
    rar_low = ((uint32_t) addr[0] |
               ((uint32_t) addr[1] << 8) |
               ((uint32_t) addr[2] << 16) | ((uint32_t) addr[3] << 24));

    rar_high = ((uint32_t) addr[4] | ((uint32_t) addr[5] << 8) | E1000_RAH_AV);

    E1000_WRITE_REG_ARRAY(shared, RA, (index << 1), rar_low);
    E1000_WRITE_REG_ARRAY(shared, RA, ((index << 1) + 1), rar_high);
    return;
}

/******************************************************************************
 * Writes a value to the specified offset in the VLAN filter table.
 *
 * shared - Struct containing variables accessed by shared code
 * offset - Offset in VLAN filer table to write
 * value - Value to write into VLAN filter table
 *****************************************************************************/
void
e1000_write_vfta(struct e1000_shared_adapter *shared,
                 uint32_t offset,
                 uint32_t value)
{
    uint32_t temp;

    if((shared->mac_type == e1000_82544) && ((offset & 0x1) == 1)) {
        temp = E1000_READ_REG_ARRAY(shared, VFTA, (offset - 1));
        E1000_WRITE_REG_ARRAY(shared, VFTA, offset, value);
        E1000_WRITE_REG_ARRAY(shared, VFTA, (offset - 1), temp);
    } else {
        E1000_WRITE_REG_ARRAY(shared, VFTA, offset, value);
    }
    return;
}

/******************************************************************************
 * Clears the VLAN filer table
 *
 * shared - Struct containing variables accessed by shared code
 *****************************************************************************/
void
e1000_clear_vfta(struct e1000_shared_adapter *shared)
{
    uint32_t offset;

    for(offset = 0; offset < E1000_VLAN_FILTER_TBL_SIZE; offset++)
        E1000_WRITE_REG_ARRAY(shared, VFTA, offset, 0);
    return;
}

/******************************************************************************
 * Configures flow control and link settings.
 * 
 * shared - Struct containing variables accessed by shared code
 * 
 * Determines which flow control settings to use. Calls the apropriate media-
 * specific link configuration function. Configures the flow control settings.
 * Assuming the adapter has a valid link partner, a valid link should be
 * established. Assumes the hardware has previously been reset and the 
 * transmitter and receiver are not enabled.
 *****************************************************************************/
boolean_t
e1000_setup_fc_and_link(struct e1000_shared_adapter *shared)
{
    uint32_t ctrl_reg;
    uint32_t eecd_reg;
    uint32_t ctrl_ext_reg;
    boolean_t status = TRUE;

    DEBUGFUNC("e1000_setup_fc_and_link");

    /* Read the SWDPIO bits and the ILOS bit out of word 0x0A in the
     * EEPROM.  Store these bits in a variable that we will later write
     * to the Device Control Register (CTRL).
     */
    eecd_reg = e1000_read_eeprom(shared, EEPROM_INIT_CONTROL1_REG);

    ctrl_reg =
        (((eecd_reg & EEPROM_WORD0A_SWDPIO) << SWDPIO_SHIFT) |
         ((eecd_reg & EEPROM_WORD0A_ILOS) << ILOS_SHIFT));

    /* Set the PCI priority bit correctly in the CTRL register.  This
     * determines if the adapter gives priority to receives, or if it
     * gives equal priority to transmits and receives.
     */
    if(shared->dma_fairness)
        ctrl_reg |= E1000_CTRL_PRIOR;

    /* Read and store word 0x0F of the EEPROM. This word contains bits
     * that determine the hardware's default PAUSE (flow control) mode,
     * a bit that determines whether the HW defaults to enabling or
     * disabling auto-negotiation, and the direction of the
     * SW defined pins. If there is no SW over-ride of the flow
     * control setting, then the variable shared->fc will
     * be initialized based on a value in the EEPROM.
     */
    eecd_reg = e1000_read_eeprom(shared, EEPROM_INIT_CONTROL2_REG);

    if(shared->fc > e1000_fc_full) {
        if((eecd_reg & EEPROM_WORD0F_PAUSE_MASK) == 0)
            shared->fc = e1000_fc_none;
        else if((eecd_reg & EEPROM_WORD0F_PAUSE_MASK) == EEPROM_WORD0F_ASM_DIR)
            shared->fc = e1000_fc_tx_pause;
        else
            shared->fc = e1000_fc_full;
    }

    /* We want to save off the original Flow Control configuration just
     * in case we get disconnected and then reconnected into a different
     * hub or switch with different Flow Control capabilities.
     */
    shared->original_fc = shared->fc;

    if(shared->mac_type == e1000_82542_rev2_0)
        shared->fc &= (~e1000_fc_tx_pause);

    if((shared->mac_type < e1000_82543) && (shared->report_tx_early == 1))
        shared->fc &= (~e1000_fc_rx_pause);

    DEBUGOUT1("After fix-ups FlowControl is now = %x\n", shared->fc);

    /* Take the 4 bits from EEPROM word 0x0F that determine the initial
     * polarity value for the SW controlled pins, and setup the
     * Extended Device Control reg with that info.
     * This is needed because one of the SW controlled pins is used for
     * signal detection.  So this should be done before e1000_setup_pcs_link()
     * or e1000_phy_setup() is called.
     */
    if(shared->mac_type == e1000_82543) {
        ctrl_ext_reg = ((eecd_reg & EEPROM_WORD0F_SWPDIO_EXT)
                        << SWDPIO__EXT_SHIFT);
        E1000_WRITE_REG(shared, CTRL_EXT, ctrl_ext_reg);
    }

    /* Call the necessary subroutine to configure the link. */
    if(shared->media_type == e1000_media_type_fiber)
        status = e1000_setup_pcs_link(shared, ctrl_reg);
    else
        status = e1000_phy_setup(shared, ctrl_reg);

    /* Initialize the flow control address, type, and PAUSE timer
     * registers to their default values.  This is done even if flow
     * control is disabled, because it does not hurt anything to
     * initialize these registers.
     */
    DEBUGOUT("Initializing the Flow Control address, type and timer regs\n");

    E1000_WRITE_REG(shared, FCAL, FLOW_CONTROL_ADDRESS_LOW);
    E1000_WRITE_REG(shared, FCAH, FLOW_CONTROL_ADDRESS_HIGH);
    E1000_WRITE_REG(shared, FCT, FLOW_CONTROL_TYPE);
    E1000_WRITE_REG(shared, FCTTV, shared->fc_pause_time);

    /* Set the flow control receive threshold registers.  Normally,
     * these registers will be set to a default threshold that may be
     * adjusted later by the driver's runtime code.  However, if the
     * ability to transmit pause frames in not enabled, then these
     * registers will be set to 0. 
     */
    if(!(shared->fc & e1000_fc_tx_pause)) {
        E1000_WRITE_REG(shared, FCRTL, 0);
        E1000_WRITE_REG(shared, FCRTH, 0);
    } else {
        /* We need to set up the Receive Threshold high and low water marks
         * as well as (optionally) enabling the transmission of XON frames.
         */
        if(shared->fc_send_xon) {
            E1000_WRITE_REG(shared, FCRTL,
                            (shared->fc_low_water | E1000_FCRTL_XONE));
            E1000_WRITE_REG(shared, FCRTH, shared->fc_high_water);
        } else {
            E1000_WRITE_REG(shared, FCRTL, shared->fc_low_water);
            E1000_WRITE_REG(shared, FCRTH, shared->fc_high_water);
        }
    }
    return (status);
}

/******************************************************************************
 * Sets up link for a fiber based adapter
 *
 * shared - Struct containing variables accessed by shared code
 * ctrl_reg - Current value of the device control register
 *
 * Manipulates Physical Coding Sublayer functions in order to configure
 * link. Assumes the hardware has been previously reset and the transmitter
 * and receiver are not enabled.
 *****************************************************************************/
boolean_t
e1000_setup_pcs_link(struct e1000_shared_adapter *shared,
                     uint32_t ctrl_reg)
{
    uint32_t status_reg;
    uint32_t tctl_reg;
    uint32_t txcw_reg = 0;
    uint32_t i;

    DEBUGFUNC("e1000_setup_pcs_link");

    /* Setup the collsion distance.  Since this is configuring the
     * TBI it is assumed that we are in Full Duplex.
     */
    tctl_reg = E1000_READ_REG(shared, TCTL);
    i = E1000_FDX_COLLISION_DISTANCE;
    i <<= E1000_COLD_SHIFT;
    tctl_reg |= i;
    E1000_WRITE_REG(shared, TCTL, tctl_reg);

    /* Check for a software override of the flow control settings, and
     * setup the device accordingly.  If auto-negotiation is enabled,
     * then software will have to set the "PAUSE" bits to the correct
     * value in the Tranmsit Config Word Register (TXCW) and re-start
     * auto-negotiation.  However, if auto-negotiation is disabled,
     * then software will have to manually configure the two flow
     * control enable bits in the CTRL register.
     *
     * The possible values of the "fc" parameter are:
     *      0:  Flow control is completely disabled
     *      1:  Rx flow control is enabled (we can receive pause frames
     *          but not send pause frames).
     *      2:  Tx flow control is enabled (we can send pause frames
     *          but we do not support receiving pause frames).
     *      3:  Both Rx and TX flow control (symmetric) are enabled.
     *  other:  No software override.  The flow control configuration
     *          in the EEPROM is used.
     */
    switch (shared->fc) {
    case e1000_fc_none:        /* 0 */
        /* Flow control (RX & TX) is completely disabled by a
         * software over-ride.
         */
        txcw_reg = (E1000_TXCW_ANE | E1000_TXCW_FD);
        break;
    case e1000_fc_rx_pause:    /* 1 */
        /* RX Flow control is enabled, and TX Flow control is
         * disabled, by a software over-ride.
         */
        /* Since there really isn't a way to advertise that we are
         * capable of RX Pause ONLY, we will advertise that we
         * support both symmetric and asymmetric RX PAUSE.  Later
         * we will disable the adapter's ability to send PAUSE
         * frames.
         */
        txcw_reg = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
        break;
    case e1000_fc_tx_pause:    /* 2 */
        /* TX Flow control is enabled, and RX Flow control is
         * disabled, by a software over-ride.
         */
        txcw_reg = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_ASM_DIR);
        break;
    case e1000_fc_full:        /* 3 */
        /* Flow control (both RX and TX) is enabled by a software
         * over-ride.
         */
        txcw_reg = (E1000_TXCW_ANE | E1000_TXCW_FD | E1000_TXCW_PAUSE_MASK);
        break;
    default:
        /* We should never get here.  The value should be 0-3. */
        DEBUGOUT("Flow control param set incorrectly\n");
        ASSERT(0);
        break;
    }

    /* Since auto-negotiation is enabled, take the link out of reset.
     * (the link will be in reset, because we previously reset the
     * chip). This will restart auto-negotiation.  If auto-neogtiation
     * is successful then the link-up status bit will be set and the
     * flow control enable bits (RFCE and TFCE) will be set according
     * to their negotiated value.
     */
    DEBUGOUT("Auto-negotiation enabled\n");

    E1000_WRITE_REG(shared, TXCW, txcw_reg);
    E1000_WRITE_REG(shared, CTRL, ctrl_reg);

    shared->txcw_reg = txcw_reg;
    msec_delay(1);

    /* If we have a signal then poll for a "Link-Up" indication in the
     * Device Status Register.   Time-out if a link isn't seen in 500
     * milliseconds seconds (Auto-negotiation should complete in less
     * than 500 milliseconds even if the other end is doing it in SW).
     */
    if(!(E1000_READ_REG(shared, CTRL) & E1000_CTRL_SWDPIN1)) {

        DEBUGOUT("Looking for Link\n");
        for(i = 0; i < (LINK_UP_TIMEOUT / 10); i++) {
            msec_delay(10);
            status_reg = E1000_READ_REG(shared, STATUS);
            if(status_reg & E1000_STATUS_LU)
                break;
        }

        if(i == (LINK_UP_TIMEOUT / 10)) {
            /* AutoNeg failed to achieve a link, so we'll call the
             * "CheckForLink" routine.  This routine will force the link
             * up if we have "signal-detect".  This will allow us to
             * communicate with non-autonegotiating link partners.
             */
            DEBUGOUT("Never got a valid link from auto-neg!!!\n");

            shared->autoneg_failed = 1;
            e1000_check_for_link(shared);
            shared->autoneg_failed = 0;
        } else {
            shared->autoneg_failed = 0;
            DEBUGOUT("Valid Link Found\n");
        }
    } else {
        DEBUGOUT("No Signal Detected\n");
    }

    return (TRUE);
}

/******************************************************************************
 * Configures flow control settings after link is established
 * 
 * shared - Struct containing variables accessed by shared code
 *
 * Should be called immediately after a valid link has been established.
 * Forces MAC flow control settings if link was forced. When in MII/GMII mode
 * and autonegotiation is enabled, the MAC flow control settings will be set
 * based on the flow control negotiated by the PHY. In TBI mode, the TFCE
 * and RFCE bits will be automaticaly set to the negotiated flow control mode.
 *****************************************************************************/
void
e1000_config_fc_after_link_up(struct e1000_shared_adapter *shared)
{
    uint16_t mii_status_reg;
    uint16_t mii_nway_adv_reg;
    uint16_t mii_nway_lp_ability_reg;
    uint16_t speed;
    uint16_t duplex;

    DEBUGFUNC("e1000_config_fc_after_link_up");

    /* Check for the case where we have fiber media and auto-neg failed
     * so we had to force link.  In this case, we need to force the
     * configuration of the MAC to match the "fc" parameter.
     */
    if(((shared->media_type == e1000_media_type_fiber)
        && (shared->autoneg_failed))
       || ((shared->media_type == e1000_media_type_copper)
           && (!shared->autoneg))) {
        e1000_force_mac_fc(shared);
    }

    /* Check for the case where we have copper media and auto-neg is
     * enabled.  In this case, we need to check and see if Auto-Neg
     * has completed, and if so, how the PHY and link partner has
     * flow control configured.
     */
    if((shared->media_type == e1000_media_type_copper) && shared->autoneg) {
        /* Read the MII Status Register and check to see if AutoNeg
         * has completed.  We read this twice because this reg has
         * some "sticky" (latched) bits.
         */
        mii_status_reg = e1000_read_phy_reg(shared, PHY_STATUS);
        mii_status_reg = e1000_read_phy_reg(shared, PHY_STATUS);

        if(mii_status_reg & MII_SR_AUTONEG_COMPLETE) {
            /* The AutoNeg process has completed, so we now need to
             * read both the Auto Negotiation Advertisement Register
             * (Address 4) and the Auto_Negotiation Base Page Ability
             * Register (Address 5) to determine how flow control was
             * negotiated.
             */
            mii_nway_adv_reg = e1000_read_phy_reg(shared,
                                                  PHY_AUTONEG_ADV);
            mii_nway_lp_ability_reg = e1000_read_phy_reg(shared,
                                                         PHY_LP_ABILITY);

            /* Two bits in the Auto Negotiation Advertisement Register
             * (Address 4) and two bits in the Auto Negotiation Base
             * Page Ability Register (Address 5) determine flow control
             * for both the PHY and the link partner.  The following
             * table, taken out of the IEEE 802.3ab/D6.0 dated March 25,
             * 1999, describes these PAUSE resolution bits and how flow
             * control is determined based upon these settings.
             * NOTE:  DC = Don't Care
             *
             *   LOCAL DEVICE  |   LINK PARTNER
             * PAUSE | ASM_DIR | PAUSE | ASM_DIR | NIC Resolution
             *-------|---------|-------|---------|--------------------
             *   0   |    0    |  DC   |   DC    | e1000_fc_none
             *   0   |    1    |   0   |   DC    | e1000_fc_none
             *   0   |    1    |   1   |    0    | e1000_fc_none
             *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
             *   1   |    0    |   0   |   DC    | e1000_fc_none
             *   1   |   DC    |   1   |   DC    | e1000_fc_full
             *   1   |    1    |   0   |    0    | e1000_fc_none
             *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
             *
             */
            /* Are both PAUSE bits set to 1?  If so, this implies
             * Symmetric Flow Control is enabled at both ends.  The
             * ASM_DIR bits are irrelevant per the spec.
             *
             * For Symmetric Flow Control:
             *
             *   LOCAL DEVICE  |   LINK PARTNER
             * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
             *-------|---------|-------|---------|--------------------
             *   1   |   DC    |   1   |   DC    | e1000_fc_full
             *
             */
            if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
               (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE)) {
                /* Now we need to check if the user selected RX ONLY
                 * of pause frames.  In this case, we had to advertise
                 * FULL flow control because we could not advertise RX
                 * ONLY. Hence, we must now check to see if we need to
                 * turn OFF  the TRANSMISSION of PAUSE frames.
                 */
                if(shared->original_fc == e1000_fc_full) {
                    shared->fc = e1000_fc_full;
                    DEBUGOUT("Flow Control = FULL.\r\n");
                } else {
                    shared->fc = e1000_fc_rx_pause;
                    DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
                }
            }
            /* For receiving PAUSE frames ONLY.
             *
             *   LOCAL DEVICE  |   LINK PARTNER
             * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
             *-------|---------|-------|---------|--------------------
             *   0   |    1    |   1   |    1    | e1000_fc_tx_pause
             *
             */
            else if(!(mii_nway_adv_reg & NWAY_AR_PAUSE) &&
                    (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
                    (mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
                    (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
                shared->fc = e1000_fc_tx_pause;
                DEBUGOUT("Flow Control = TX PAUSE frames only.\r\n");
            }
            /* For transmitting PAUSE frames ONLY.
             *
             *   LOCAL DEVICE  |   LINK PARTNER
             * PAUSE | ASM_DIR | PAUSE | ASM_DIR | Result
             *-------|---------|-------|---------|--------------------
             *   1   |    1    |   0   |    1    | e1000_fc_rx_pause
             *
             */
            else if((mii_nway_adv_reg & NWAY_AR_PAUSE) &&
                    (mii_nway_adv_reg & NWAY_AR_ASM_DIR) &&
                    !(mii_nway_lp_ability_reg & NWAY_LPAR_PAUSE) &&
                    (mii_nway_lp_ability_reg & NWAY_LPAR_ASM_DIR)) {
                shared->fc = e1000_fc_rx_pause;
                DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
            }
            /* Per the IEEE spec, at this point flow control should be
             * disabled.  However, we want to consider that we could
             * be connected to a legacy switch that doesn't advertise
             * desired flow control, but can be forced on the link
             * partner.  So if we advertised no flow control, that is
             * what we will resolve to.  If we advertised some kind of
             * receive capability (Rx Pause Only or Full Flow Control)
             * and the link partner advertised none, we will configure
             * ourselves to enable Rx Flow Control only.  We can do
             * this safely for two reasons:  If the link partner really
             * didn't want flow control enabled, and we enable Rx, no
             * harm done since we won't be receiving any PAUSE frames
             * anyway.  If the intent on the link partner was to have
             * flow control enabled, then by us enabling RX only, we
             * can at least receive pause frames and process them.
             * This is a good idea because in most cases, since we are
             * predominantly a server NIC, more times than not we will
             * be asked to delay transmission of packets than asking
             * our link partner to pause transmission of frames.
             */
            else if(shared->original_fc == e1000_fc_none ||
                    shared->original_fc == e1000_fc_tx_pause) {
                shared->fc = e1000_fc_none;
                DEBUGOUT("Flow Control = NONE.\r\n");
            } else {
                shared->fc = e1000_fc_rx_pause;
                DEBUGOUT("Flow Control = RX PAUSE frames only.\r\n");
            }

            /* Now we need to do one last check...  If we auto-
             * negotiated to HALF DUPLEX, flow control should not be
             * enabled per IEEE 802.3 spec.
             */
            e1000_get_speed_and_duplex(shared, &speed, &duplex);

            if(duplex == HALF_DUPLEX)
                shared->fc = e1000_fc_none;

            /* Now we call a subroutine to actually force the MAC
             * controller to use the correct flow control settings.
             */
            e1000_force_mac_fc(shared);
        } else {
            DEBUGOUT("Copper PHY and Auto Neg has not completed.\r\n");
        }
    }
    return;  
}

/******************************************************************************
 * Checks to see if the link status of the hardware has changed.
 *
 * shared - Struct containing variables accessed by shared code
 *
 * Called by any function that needs to check the link status of the adapter.
 *****************************************************************************/
void
e1000_check_for_link(struct e1000_shared_adapter *shared)
{
    uint32_t rxcw_reg;
    uint32_t ctrl_reg;
    uint32_t status_reg;
    uint32_t rctl_reg;
    uint16_t phy_data;
    uint16_t lp_capability;

    DEBUGFUNC("e1000_check_for_link");

    ctrl_reg = E1000_READ_REG(shared, CTRL);
    status_reg = E1000_READ_REG(shared, STATUS);
    rxcw_reg = E1000_READ_REG(shared, RXCW);

    /* If we have a copper PHY then we only want to go out to the PHY
     * registers to see if Auto-Neg has completed and/or if our link
     * status has changed.  The get_link_status flag will be set if we
     * receive a Link Status Change interrupt or we have Rx Sequence
     * Errors.
     */
    if(shared->media_type == e1000_media_type_copper
       && shared->get_link_status) {
        /* First we want to see if the MII Status Register reports
         * link.  If so, then we want to get the current speed/duplex
         * of the PHY.
         * Read the register twice since the link bit is sticky.
         */
        phy_data = e1000_read_phy_reg(shared, PHY_STATUS);
        phy_data = e1000_read_phy_reg(shared, PHY_STATUS);

        if(phy_data & MII_SR_LINK_STATUS) {
            shared->get_link_status = FALSE;
        } else {
            DEBUGOUT("**** CFL - No link detected. ****\r\n");
            return;
        }

        /* If we are forcing speed/duplex, then we simply return since
         * we have already determined whether we have link or not.
         */
        if(!shared->autoneg) {
            return;
        }

        switch (shared->phy_id) {
        case M88E1000_12_PHY_ID:
        case M88E1000_14_PHY_ID:
        case M88E1000_I_PHY_ID:
            /* We have a M88E1000 PHY and Auto-Neg is enabled.  If we
             * have Si on board that is 82544 or newer, Auto
             * Speed Detection takes care of MAC speed/duplex
             * configuration.  So we only need to configure Collision
             * Distance in the MAC.  Otherwise, we need to force
             * speed/duplex on the MAC to the current PHY speed/duplex
             * settings.
             */
            if(shared->mac_type >= e1000_82544) {
                DEBUGOUT("CFL - Auto-Neg complete.");
                DEBUGOUT("Configuring Collision Distance.");
                e1000_config_collision_dist(shared);
            } else {
                /* Read the Phy Specific Status register to get the
                 * resolved speed/duplex settings.  Then call
                 * e1000_config_mac_to_phy which will retrieve
                 * PHY register information and configure the MAC to
                 * equal the negotiated speed/duplex.
                 */
                phy_data = e1000_read_phy_reg(shared, 
                                              M88E1000_PHY_SPEC_STATUS);

                DEBUGOUT1("CFL - Auto-Neg complete.  phy_data = %x\r\n",
                          phy_data);
                e1000_config_mac_to_phy(shared, phy_data);
            }

            /* Configure Flow Control now that Auto-Neg has completed.
             * We need to first restore the users desired Flow
             * Control setting since we may have had to re-autoneg
             * with a different link partner.
             */
            e1000_config_fc_after_link_up(shared);
            break;

        default:
            DEBUGOUT("CFL - Invalid PHY detected.\r\n");

        } /* end switch statement */

        /* At this point we know that we are on copper, link is up, 
         * and we are auto-neg'd.  These are pre-conditions for checking
         * the link parter capabilities register.  We use the link partner
         * capabilities to determine if TBI Compatibility needs to be turned on
         * or turned off.  If the link partner advertises any speed in addition
         * to Gigabit, then we assume that they are GMII-based and TBI 
         * compatibility is not needed.
         * If no other speeds are advertised, then we assume the link partner
         * is TBI-based and we turn on TBI Compatibility.
         */
        if(shared->tbi_compatibility_en) {
            lp_capability = e1000_read_phy_reg(shared, PHY_LP_ABILITY);
            if(lp_capability & (NWAY_LPAR_10T_HD_CAPS |
                                NWAY_LPAR_10T_FD_CAPS |
                                NWAY_LPAR_100TX_HD_CAPS |
                                NWAY_LPAR_100TX_FD_CAPS |
                                NWAY_LPAR_100T4_CAPS)) {
                /* If our link partner advertises below Gig, then they do not
                 * need the special Tbi Compatibility mode. 
                 */
                if(shared->tbi_compatibility_on) {
                    /* If we previously were in the mode, turn it off, now. */
                    rctl_reg = E1000_READ_REG(shared, RCTL);
                    rctl_reg &= ~E1000_RCTL_SBP;
                    E1000_WRITE_REG(shared, RCTL, rctl_reg);
                    shared->tbi_compatibility_on = FALSE;
                }
            } else {
                /* If the mode is was previously off, turn it on. 
                 * For compatibility with a suspected Tbi link partners, 
                 * we will store bad packets.
                 * (Certain frames have an additional byte on the end and will 
                 * look like CRC errors to to the hardware).
                 */
                if(!shared->tbi_compatibility_on) {
                    shared->tbi_compatibility_on = TRUE;
                    rctl_reg = E1000_READ_REG(shared, RCTL);
                    rctl_reg |= E1000_RCTL_SBP;
                    E1000_WRITE_REG(shared, RCTL, rctl_reg);
                }
            }
        }
    } /* end if e1000_media_type_copper statement */
    /* If we don't have link (auto-negotiation failed or link partner
     * cannot auto-negotiate) and the cable is plugged in since we don't
     * have Loss-Of-Signal (we HAVE a signal) and our link partner is
     * not trying to AutoNeg with us (we are receiving idles/data
     * then we need to force our link to connect to a non
     * auto-negotiating link partner.  We also need to give
     * auto-negotiation time to complete in case the cable was just
     * plugged in.  The autoneg_failed flag does this.
     */
    else if((shared->media_type == e1000_media_type_fiber) &&  /* Fiber PHY */
            (!(status_reg & E1000_STATUS_LU)) &&        /* no link and    */
            (!(ctrl_reg & E1000_CTRL_SWDPIN1)) &&       /* we have signal */
            (!(rxcw_reg & E1000_RXCW_C))) {     /* and rxing idle/data */
        if(shared->autoneg_failed == 0) {      /* given AutoNeg time */
            shared->autoneg_failed = 1;
            return;
        }

        DEBUGOUT("NOT RXing /C/, disable AutoNeg and force link.\r\n");

        /* Disable auto-negotiation in the TXCW register */
        E1000_WRITE_REG(shared, TXCW, (shared->txcw_reg & ~E1000_TXCW_ANE));

        /* Force link-up and also force full-duplex. */
        ctrl_reg = E1000_READ_REG(shared, CTRL);
        ctrl_reg |= (E1000_CTRL_SLU | E1000_CTRL_FD);
        E1000_WRITE_REG(shared, CTRL, ctrl_reg);

        /* Configure Flow Control after forcing link up. */
        e1000_config_fc_after_link_up(shared);

    } else if((shared->media_type == e1000_media_type_fiber) && /* Fiber */
              (ctrl_reg & E1000_CTRL_SLU) &&    /* we have forced link */
              (rxcw_reg & E1000_RXCW_C)) {      /* and Rxing /C/ ordered sets */
        /* If we are forcing link and we are receiving /C/ ordered sets,
         * then re-enable auto-negotiation in the RXCW register and
         * disable forced link in the Device Control register in an attempt
         * to AutoNeg with our link partner.
         */
        DEBUGOUT("RXing /C/, enable AutoNeg and stop forcing link.\r\n");

        /* Enable auto-negotiation in the TXCW register and stop
         * forcing link.
         */
        E1000_WRITE_REG(shared, TXCW, shared->txcw_reg);

        E1000_WRITE_REG(shared, CTRL, (ctrl_reg & ~E1000_CTRL_SLU));
    }

    return;
}

/******************************************************************************
 * Clears all hardware statistics counters. 
 *
 * shared - Struct containing variables accessed by shared code
 *****************************************************************************/
void
e1000_clear_hw_cntrs(struct e1000_shared_adapter *shared)
{
    volatile uint32_t temp_reg;

    DEBUGFUNC("e1000_clear_hw_cntrs");

    /* if we are stopped or resetting exit gracefully */
    if(shared->adapter_stopped) {
        DEBUGOUT("Exiting because the adapter is stopped!!!\n");
        return;
    }

    temp_reg = E1000_READ_REG(shared, CRCERRS);
    temp_reg = E1000_READ_REG(shared, SYMERRS);
    temp_reg = E1000_READ_REG(shared, MPC);
    temp_reg = E1000_READ_REG(shared, SCC);
    temp_reg = E1000_READ_REG(shared, ECOL);
    temp_reg = E1000_READ_REG(shared, MCC);
    temp_reg = E1000_READ_REG(shared, LATECOL);
    temp_reg = E1000_READ_REG(shared, COLC);
    temp_reg = E1000_READ_REG(shared, DC);
    temp_reg = E1000_READ_REG(shared, SEC);
    temp_reg = E1000_READ_REG(shared, RLEC);
    temp_reg = E1000_READ_REG(shared, XONRXC);
    temp_reg = E1000_READ_REG(shared, XONTXC);
    temp_reg = E1000_READ_REG(shared, XOFFRXC);
    temp_reg = E1000_READ_REG(shared, XOFFTXC);
    temp_reg = E1000_READ_REG(shared, FCRUC);
    temp_reg = E1000_READ_REG(shared, PRC64);
    temp_reg = E1000_READ_REG(shared, PRC127);
    temp_reg = E1000_READ_REG(shared, PRC255);
    temp_reg = E1000_READ_REG(shared, PRC511);
    temp_reg = E1000_READ_REG(shared, PRC1023);
    temp_reg = E1000_READ_REG(shared, PRC1522);
    temp_reg = E1000_READ_REG(shared, GPRC);
    temp_reg = E1000_READ_REG(shared, BPRC);
    temp_reg = E1000_READ_REG(shared, MPRC);
    temp_reg = E1000_READ_REG(shared, GPTC);
    temp_reg = E1000_READ_REG(shared, GORCL);
    temp_reg = E1000_READ_REG(shared, GORCH);
    temp_reg = E1000_READ_REG(shared, GOTCL);
    temp_reg = E1000_READ_REG(shared, GOTCH);
    temp_reg = E1000_READ_REG(shared, RNBC);
    temp_reg = E1000_READ_REG(shared, RUC);
    temp_reg = E1000_READ_REG(shared, RFC);
    temp_reg = E1000_READ_REG(shared, ROC);
    temp_reg = E1000_READ_REG(shared, RJC);
    temp_reg = E1000_READ_REG(shared, TORL);
    temp_reg = E1000_READ_REG(shared, TORH);
    temp_reg = E1000_READ_REG(shared, TOTL);
    temp_reg = E1000_READ_REG(shared, TOTH);
    temp_reg = E1000_READ_REG(shared, TPR);
    temp_reg = E1000_READ_REG(shared, TPT);
    temp_reg = E1000_READ_REG(shared, PTC64);
    temp_reg = E1000_READ_REG(shared, PTC127);
    temp_reg = E1000_READ_REG(shared, PTC255);
    temp_reg = E1000_READ_REG(shared, PTC511);
    temp_reg = E1000_READ_REG(shared, PTC1023);
    temp_reg = E1000_READ_REG(shared, PTC1522);
    temp_reg = E1000_READ_REG(shared, MPTC);
    temp_reg = E1000_READ_REG(shared, BPTC);

    if(shared->mac_type < e1000_82543)
        return;

    temp_reg = E1000_READ_REG(shared, ALGNERRC);
    temp_reg = E1000_READ_REG(shared, RXERRC);
    temp_reg = E1000_READ_REG(shared, TNCRS);
    temp_reg = E1000_READ_REG(shared, CEXTERR);
    temp_reg = E1000_READ_REG(shared, TSCTC);
    temp_reg = E1000_READ_REG(shared, TSCTFC);
    return;
}

/******************************************************************************
 * Detects the current speed and duplex settings of the hardware.
 *
 * shared - Struct containing variables accessed by shared code
 * speed - Speed of the connection
 * duplex - Duplex setting of the connection
 *****************************************************************************/
void
e1000_get_speed_and_duplex(struct e1000_shared_adapter *shared,
                           uint16_t *speed,
                           uint16_t *duplex)
{
    uint32_t status_reg;
#if DBG
    uint16_t phy_data;
#endif

    DEBUGFUNC("e1000_get_speed_and_duplex");

    /* If the adapter is stopped we don't have a speed or duplex */
    if(shared->adapter_stopped) {
        *speed = 0;
        *duplex = 0;
        return;
    }

    if(shared->mac_type >= e1000_82543) {
        status_reg = E1000_READ_REG(shared, STATUS);
        if(status_reg & E1000_STATUS_SPEED_1000) {
            *speed = SPEED_1000;
            DEBUGOUT("1000 Mbs, ");
        } else if(status_reg & E1000_STATUS_SPEED_100) {
            *speed = SPEED_100;
            DEBUGOUT("100 Mbs, ");
        } else {
            *speed = SPEED_10;
            DEBUGOUT("10 Mbs, ");
        }

        if(status_reg & E1000_STATUS_FD) {
            *duplex = FULL_DUPLEX;
            DEBUGOUT("Full Duplex\r\n");
        } else {
            *duplex = HALF_DUPLEX;
            DEBUGOUT(" Half Duplex\r\n");
        }
    } else {
        DEBUGOUT("1000 Mbs, Full Duplex\r\n");
        *speed = SPEED_1000;
        *duplex = FULL_DUPLEX;
    }

#if DBG
    if(shared->phy_id == M88E1000_12_PHY_ID ||
       shared->phy_id == M88E1000_14_PHY_ID ||
       shared->phy_id == M88E1000_I_PHY_ID) {
        /* read the phy specific status register */
        phy_data = e1000_read_phy_reg(shared, M88E1000_PHY_SPEC_STATUS);
        DEBUGOUT1("M88E1000 Phy Specific Status Reg contents = %x\n", phy_data);
        phy_data = e1000_read_phy_reg(shared, PHY_STATUS);
        DEBUGOUT1("Phy MII Status Reg contents = %x\n", phy_data);
        DEBUGOUT1("Device Status Reg contents = %x\n", 
                  E1000_READ_REG(shared, STATUS));
    }
#endif
    return;
}

/******************************************************************************
 * Reads a 16 bit word from the EEPROM.
 *
 * shared - Struct containing variables accessed by shared code
 * offset - offset of 16 bit word in the EEPROM to read
 *****************************************************************************/
uint16_t
e1000_read_eeprom(struct e1000_shared_adapter *shared,
                  uint16_t offset)
{
    uint16_t data;

    /*  Prepare the EEPROM for reading  */
    e1000_setup_eeprom(shared);

    /*  Send the READ command (opcode + addr)  */
    e1000_shift_out_bits(shared, EEPROM_READ_OPCODE, 3);
    e1000_shift_out_bits(shared, offset, 6);

    /* Read the data */
    data = e1000_shift_in_bits(shared);

    /* End this read operation */
    e1000_standby_eeprom(shared);

    return (data);
}

/******************************************************************************
 * Verifies that the EEPROM has a valid checksum
 * 
 * shared - Struct containing variables accessed by shared code
 *
 * Reads the first 64 16 bit words of the EEPROM and sums the values read.
 * If the the sum of the 64 16 bit words is 0xBABA, the EEPROM's checksum is
 * valid.
 *****************************************************************************/
boolean_t
e1000_validate_eeprom_checksum(struct e1000_shared_adapter *shared)
{
    uint16_t checksum = 0;
    uint16_t i;

    for(i = 0; i < (EEPROM_CHECKSUM_REG + 1); i++)
        checksum += e1000_read_eeprom(shared, i);

    if(checksum == (uint16_t) EEPROM_SUM)
        return (TRUE);
    else
        return (FALSE);
}
/******************************************************************************
 * Reads the adapter's part number from the EEPROM
 *
 * shared - Struct containing variables accessed by shared code
 * part_num - Adapter's part number
 *****************************************************************************/
boolean_t
e1000_read_part_num(struct e1000_shared_adapter *shared,
                    uint32_t *part_num)
{
    uint16_t eeprom_word;

    DEBUGFUNC("e1000_read_part_num");

    /* Don't read the EEPROM if we are stopped */
    if(shared->adapter_stopped) {
        *part_num = 0;
        return (FALSE);
    }

    /* Get word 0 from EEPROM */
    eeprom_word = e1000_read_eeprom(shared, (uint16_t) (EEPROM_PBA_BYTE_1));

    DEBUGOUT("Read first part number word\n");

    /* Save word 0 in upper half is PartNumber */
    *part_num = (uint32_t) eeprom_word;
    *part_num = *part_num << 16;

    /* Get word 1 from EEPROM */
    eeprom_word =
        e1000_read_eeprom(shared, (uint16_t) (EEPROM_PBA_BYTE_1 + 1));

    DEBUGOUT("Read second part number word\n");

    /* Save word 1 in lower half of PartNumber */
    *part_num |= eeprom_word;

    /* read a valid part number */
    return (TRUE);
}

void
e1000_read_mac_addr(struct e1000_shared_adapter * shared)
{
    uint16_t temp, x;

    for(x = 0; x < NODE_ADDRESS_SIZE; x += 2) {
        temp = e1000_read_eeprom(shared, (uint16_t) 
                                         (EEPROM_NODE_ADDRESS_BYTE_0 + (x/2)));
        shared->perm_mac_addr[x] = (uint8_t) (temp & 0x00FF);
        shared->perm_mac_addr[x+1] = (uint8_t) (temp >> 8);
    }
    
    for(x = 0; x < NODE_ADDRESS_SIZE; x++)
        shared->mac_addr[x] = shared->perm_mac_addr[x];
}

/******************************************************************************
 * Adjusts the statistic counters when a frame is accepted by TBI_ACCEPT
 * 
 * shared - Struct containing variables accessed by shared code
 * frame_len - The length of the frame in question
 * mac_addr - The Ethernet destination address of the frame in question
 *****************************************************************************/
uint32_t
e1000_tbi_adjust_stats(struct e1000_shared_adapter *shared,
                       struct e1000_shared_stats *stats,
                       uint32_t frame_len,
                       uint8_t *mac_addr)
{
    uint64_t carry_bit;

    /* First adjust the frame length. */
    frame_len--;
    /* We need to adjust the statistics counters, since the hardware
     * counters overcount this packet as a CRC error and undercount
     * the packet as a good packet
     */
    /* This packet should not be counted as a CRC error.    */
    stats->crcerrs--;
    /* This packet does count as a Good Packet Received.    */
    stats->gprc++;

    /* Adjust the Good Octets received counters             */
    carry_bit = 0x80000000 & stats->gorcl;
    stats->gorcl += frame_len;
    /* If the high bit of Gorcl (the low 32 bits of the Good Octets
     * Received Count) was one before the addition, 
     * AND it is zero after, then we lost the carry out, 
     * need to add one to Gorch (Good Octets Received Count High).
     * This could be simplified if all environments supported 
     * 64-bit integers.
     */
    if(carry_bit && ((stats->gorcl & 0x80000000) == 0))
        stats->gorch++;
    /* Is this a broadcast or multicast?  Check broadcast first,
     * since the test for a multicast frame will test positive on 
     * a broadcast frame.
     */
    if((mac_addr[0] == (uint8_t) 0xff) && (mac_addr[1] == (uint8_t) 0xff))
        /* Broadcast packet */
        stats->bprc++;
    else if(*mac_addr & 0x01)
        /* Multicast packet */
        stats->mprc++;

    if(frame_len == shared->max_frame_size) {
        /* In this case, the hardware has overcounted the number of
         * oversize frames.
         */
        if(stats->roc > 0)
            stats->roc--;
    }

    /* Adjust the bin counters when the extra byte put the frame in the
     * wrong bin. Remember that the frame_len was adjusted above.
     */
    if(frame_len == 64) {
        stats->prc64++;
        stats->prc127--;
    } else if(frame_len == 127) {
        stats->prc127++;
        stats->prc255--;
    } else if(frame_len == 255) {
        stats->prc255++;
        stats->prc511--;
    } else if(frame_len == 511) {
        stats->prc511++;
        stats->prc1023--;
    } else if(frame_len == 1023) {
        stats->prc1023++;
        stats->prc1522--;
    } else if(frame_len == 1522) {
        stats->prc1522++;
    }
    return frame_len;
}

/******************************************************************************
 * Gets the current PCI bus type, speed, and width of the hardware
 *
 * shared - Struct containing variables accessed by shared code
 *****************************************************************************/
void
e1000_get_bus_info(struct e1000_shared_adapter *shared)
{
    uint32_t status_reg;

    if(shared->mac_type < e1000_82543) {
        shared->bus_type = e1000_bus_type_unknown;
        shared->bus_speed = e1000_bus_speed_unknown;
        shared->bus_width = e1000_bus_width_unknown;
        return;
    }

    status_reg = E1000_READ_REG(shared, STATUS);

    shared->bus_type = (status_reg & E1000_STATUS_PCIX_MODE) ?
        e1000_bus_type_pcix : e1000_bus_type_pci;

    if(shared->bus_type == e1000_bus_type_pci) {
        shared->bus_speed = (status_reg & E1000_STATUS_PCI66) ?
            e1000_bus_speed_66 : e1000_bus_speed_33;
    } else {
        switch (status_reg & E1000_STATUS_PCIX_SPEED) {
        case E1000_STATUS_PCIX_SPEED_66:
            shared->bus_speed = e1000_bus_speed_66;
            break;
        case E1000_STATUS_PCIX_SPEED_100:
            shared->bus_speed = e1000_bus_speed_100;
            break;
        case E1000_STATUS_PCIX_SPEED_133:
            shared->bus_speed = e1000_bus_speed_133;
            break;
        default:
            shared->bus_speed = e1000_bus_speed_reserved;
            break;
        }
    }

    shared->bus_width = (status_reg & E1000_STATUS_BUS64) ?
        e1000_bus_width_64 : e1000_bus_width_32;

    return;
}