summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/madge/driver/hwi_gen.c
blob: d0a2d7928039cc64af230836cf5e0cc199d32b6a (plain) (blame)
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
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
/****************************************************************************
*                                                                          
* HWI_GEN.C : Part of the FASTMAC TOOL-KIT (FTK)                      
*                                                                          
* THE GENERAL HARDWARE INTERFACE MODULE                             
*                                                                          
* Copyright (c) Madge Networks Ltd. 1990-1994                         
*                                                                          
* COMPANY CONFIDENTIAL                                                        
*                                                                          
*****************************************************************************
*                                                                          
* The purpose of the Hardware Interface (HWI) is to supply an adapter card 
* independent interface to any driver.  It  performs  nearly  all  of  the 
* functions  that  involve  affecting  SIF registers on the adapter cards. 
* This includes downloading code to, initializing, and removing adapters.  
*                                                                          
* The HWI_GEN.C module contains the general routines necessary to  install 
* an adapter, to initialize an adapter, to remove an adapter and to handle 
* interrupts on an adapter. It does not contain the routines specific to a 
* particular  card  type  involved in any of these processes. These are in 
* the relevant HWI_<card_type>.C modules.                                  
*                                                                          
****************************************************************************/

/*---------------------------------------------------------------------------
|                                                                          
| DEFINITIONS                                         
|                                                                          
---------------------------------------------------------------------------*/

#include "ftk_defs.h"

/*---------------------------------------------------------------------------
|                                                                          
| MODULE ENTRY POINTS                                 
|                                                                          
---------------------------------------------------------------------------*/

#include "ftk_intr.h"   /* routines internal to FTK */
#include "ftk_extr.h"   /* routines provided or used by external FTK user */

/*---------------------------------------------------------------------------
|                                                                          
| GLOBAL VARIABLES                                   
|                                                                          
---------------------------------------------------------------------------*/

local BYTE    scb_test_pattern[] = SCB_TEST_PATTERN_DATA;
local BYTE    ssb_test_pattern[] = SSB_TEST_PATTERN_DATA;

/*---------------------------------------------------------------------------
|                                                                          
| LOCAL PROCEDURES                                    
|                                                                          
---------------------------------------------------------------------------*/

local void
hwi_copy_to_dio_space(
    ADAPTER * adapter,
    DWORD     dio_location,
    BYTE *    download_data,
    WORD      data_length_bytes
    );

local WBOOLEAN
hwi_get_init_code(
    ADAPTER * adapter
    );

#ifndef FTK_NO_PROBE
/****************************************************************************
*                                                                          
*                       hwi_deprobe_adapter                                
*                       ===================                                
*                                                                          
*                                                                          
* PARAMETERS :                                                             
* ============                                                             
*                                                                          
* PROBE * probe_values                                                     
*                                                                          
* This structure identifies cards that have been probed.                   
* 
* UINT    length
*
* The length of the above array.
*                                                                        
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_deprobe_adapter routine uses the card bus  type  to  call  the   
* correct hwi_<card_type>_deprobe_card routine.                              
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine returns TRUE if it succeeds.                                 
*                                                                          
****************************************************************************/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_deprobe_adapter)
#endif

export WBOOLEAN
hwi_deprobe_adapter(
    PROBE *   resources,
    UINT      length
    )
{
    WBOOLEAN  success = TRUE;
    UINT      i;

    for(i = 0; i < length; i++)
    {
        switch(resources[i].adapter_card_bus_type)
        {
        /*
         * As it stands the only adapter that needs to be deprobed is a
         * PCMCIA, since it has to deregister with card services.
         */

#ifndef FTK_NO_PCMCIA
            case ADAPTER_CARD_PCMCIA_BUS_TYPE :
                success = success && hwi_pcmcia_deprobe_card(resources[i]);
                break;
#endif
            default :
                break;
        } 
    }

    return(success);
}

/****************************************************************************
*
*                         hwi_probe_adapter
*                         =================
*
*
* PARAMETERS (passed by driver_probe_adapter) :
* =============================================
*
* WORD    adapter_card_bus_type
*
* the bus type of the card, so we can switch to the correct hwi module.
*
* PROBE * resources
*
* resources is an array structures used to identify and record specific 
* information about adapters found.
*
* UINT    length
*
* length is the number of structures pointed to by reources.
*
* WORD *  valid_locations
*
* valid_locations is an array of IO locations to examine for the presence
* of an adapter. 
*
* UINT    number_locations
*
* This is the number of IO locations in the above list.
*
* BODY :
* ======
* 
* The  hwi_probe_adapter routine is called by driver_probe_adapter.  It
* switches to a hwi_<card type>_probe_card routine which does the work. 
*
*
* RETURNS :
* =========
* 
* The routine  returns the value returned from below.
*
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_probe_adapter)
#endif

export UINT
hwi_probe_adapter(
    WORD    adapter_card_bus_type,
    PROBE * resources,
    UINT    length,
    WORD *  valid_locations,
    UINT    number_locations
    )
{
    UINT    number_found;
    UINT    i;

    /*
     * Mark all resource entries as undefined to start off with.
     */

    for (i = 0; i < length; i++)
    {
        resources[i].socket                = FTK_UNDEFINED;
        resources[i].adapter_card_bus_type = FTK_UNDEFINED;
        resources[i].adapter_card_type     = FTK_UNDEFINED;
        resources[i].adapter_card_revision = FTK_UNDEFINED;
        resources[i].adapter_ram_size      = FTK_UNDEFINED;
        resources[i].io_location           = FTK_UNDEFINED;
        resources[i].interrupt_number      = FTK_UNDEFINED;
        resources[i].dma_channel           = FTK_UNDEFINED;
        resources[i].transfer_mode         = FTK_UNDEFINED;
        resources[i].mmio_base_address     = FTK_UNDEFINED;
    }

    /*
     * And call the appropriate probe routine.
     */

    switch(adapter_card_bus_type)
    {
#ifndef FTK_NO_ATULA
        case ADAPTER_CARD_ATULA_BUS_TYPE :
            number_found = hwi_atula_probe_card(resources,
                                                 length,
                                                 valid_locations,
                                                 number_locations);
            break;
#endif
#ifndef FTK_NO_PNP
        case ADAPTER_CARD_PNP_BUS_TYPE :
            number_found = hwi_pnp_probe_card(resources,
                                              length,
                                              valid_locations,
                                              number_locations);
            break;
#endif
#ifndef FTK_NO_MC
        case ADAPTER_CARD_MC_BUS_TYPE :
            number_found = hwi_mc_probe_card(resources,
                                              length,
                                              valid_locations,
                                              number_locations);
            break;
#endif
#ifndef FTK_NO_EISA
        case ADAPTER_CARD_EISA_BUS_TYPE :
            number_found = hwi_eisa_probe_card(resources,
                                                length,
                                                valid_locations,
                                                number_locations);
            break;
#endif
#ifndef FTK_NO_SMART16
        case ADAPTER_CARD_SMART16_BUS_TYPE :
            number_found = hwi_smart16_probe_card(resources,
                                                length,
                                                valid_locations,
                                                number_locations);
            break;
#endif
#ifndef FTK_NO_PCI
        case ADAPTER_CARD_PCI_BUS_TYPE :
            number_found = hwi_pci_probe_card(resources,
                                               length,
                                               valid_locations,
                                               number_locations);
            break;
#endif
#ifndef FTK_NO_PCMCIA
        case ADAPTER_CARD_PCMCIA_BUS_TYPE :
            number_found = hwi_pcmcia_probe_card(resources,
                                                  length,
                                                  valid_locations,
                                                  number_locations);
            break;
#endif
#ifndef FTK_NO_PCI_TI
        case ADAPTER_CARD_TI_PCI_BUS_TYPE :
            number_found = hwi_pcit_probe_card(resources,
                                               length,
                                               valid_locations,
                                               number_locations);
            break;
#endif
#ifndef FTK_NO_PCI2
        case ADAPTER_CARD_PCI2_BUS_TYPE :
            number_found = hwi_pci2_probe_card(resources,
                                               length,
                                               valid_locations,
                                               number_locations);
            break;
#endif

        default :

            /*
             * Bad adapter card bus type so fail.
             */               

            number_found = 0;
            break;
    }

    return number_found;
}
#endif        


#ifndef FTK_NO_DETECT
/****************************************************************************
*
*                         hwi_read_rate_error
*                         ===================
*
*
* PARAMETERS (passed by driver_probe_adapter) :
* =============================================
*
* ADAPTER *    adapter
*
*  The adapter structure
*
*
* BODY :
* ======
* 
* The  hwi_read_rate_error switches to a hwi_<card type>_read_rate_error
* which reads the rate error bit of the C30. Normally this is visible in
* the IO space of the bus interface chip, except for the AT space when we
* read it through DIO.
*  
* RETURNS :
* =========
* 
* The routine  returns TRUE if there was a rate error.
*
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_read_rate_error)
#endif

export WORD
hwi_read_rate_error(
    ADAPTER * adapter
   )
{
   WBOOLEAN wrong_speed = FALSE;

   switch (adapter->adapter_card_bus_type)
   {

#ifndef FTK_NO_ATULA
        case ADAPTER_CARD_ATULA_BUS_TYPE :
            wrong_speed = hwi_atula_read_rate_error(
                                  adapter);

            break;
#endif
      default:
      {
         wrong_speed = NOT_SUPP;
         break;
      }
   }

   return wrong_speed;
}
#endif /* FTK_NO_DETECT */


/****************************************************************************
*                                                                          
*                      hwi_install_adapter                                 
*                      ===================                                 
*                                                                          
*                                                                          
* PARAMETERS :                                                             
* ============                                                             
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
* DOWNLOAD_IMAGE * download_image                                          
*                                                                          
* This  is  the code to be downloaded to the adapter. The image must be of 
* the correct type i.e.  must be downloadable into  the  adapter.  If  the 
* pointer is 0 downloading is not done.                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_install_adapter routine uses the  card  bus  type  to  call  the 
* correct hwi_<card_type>_install_card routine.                            
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine returns TRUE if it succeeds.  If this routine fails (returns 
* FALSE)  then a subsequent call to driver_explain_error, with the adapter 
* handle corresponding to the adapter parameter used here,  will  give  an 
* explanation.                                                             
*                                                                          
****************************************************************************/       

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_install_adapter)
#endif

export WBOOLEAN
hwi_install_adapter(
    ADAPTER *        adapter,
    DOWNLOAD_IMAGE * download_image
    )
{
    ADAPTER_HANDLE   handle           = adapter->adapter_handle;
    WBOOLEAN         install_success;

    /*
     * Record fact that adapter does not have interrupts or DMA enabled.
     */

    adapter->interrupts_on = FALSE;
    adapter->dma_on        = FALSE;

   /*
   *  Cards do not support speed detect unless they have a C30.  
   */
   adapter->speed_detect = FALSE;

    /*
     * Call correct install routine dependent on adapter card bus type.
     */

    switch (adapter->adapter_card_bus_type)
    {
#ifndef FTK_NO_ATULA
        case ADAPTER_CARD_ATULA_BUS_TYPE :

            /*
             * Set up pointers to the functions we will need later.
             */

            adapter->interrupt_handler = hwi_atula_interrupt_handler;
            adapter->remove_card       = hwi_atula_remove_card;
            adapter->set_dio_address   = hwi_atula_set_dio_address;

            /*
             * Call the install routine.
             */

            install_success = hwi_atula_install_card(
                                  adapter,
                                  download_image);

            break;
#endif
#ifndef FTK_NO_PNP
        case ADAPTER_CARD_PNP_BUS_TYPE :

            /*
             * Set up pointers to the functions we will need later.
             */

            adapter->interrupt_handler = hwi_pnp_interrupt_handler;
            adapter->remove_card       = hwi_pnp_remove_card;
            adapter->set_dio_address   = hwi_pnp_set_dio_address;

            /*
             * Call the install routine.
             */

            install_success = hwi_pnp_install_card(
                                  adapter,
                                  download_image);

            break;
#endif
#ifndef FTK_NO_MC
        case ADAPTER_CARD_MC_BUS_TYPE :

            adapter->interrupt_handler = hwi_mc_interrupt_handler;
            adapter->remove_card       = hwi_mc_remove_card;
            adapter->set_dio_address   = hwi_mc_set_dio_address;

            install_success = hwi_mc_install_card(
                                  adapter,
                                  download_image);

            break;
#endif
#ifndef FTK_NO_EISA
        case ADAPTER_CARD_EISA_BUS_TYPE :

            adapter->interrupt_handler = hwi_eisa_interrupt_handler;
            adapter->remove_card       = hwi_eisa_remove_card;
            adapter->set_dio_address   = hwi_eisa_set_dio_address;


            install_success = hwi_eisa_install_card(
                                  adapter,
                                  download_image);

            break;
#endif
#ifndef FTK_NO_SMART16
        case ADAPTER_CARD_SMART16_BUS_TYPE :

            adapter->interrupt_handler = hwi_smart16_interrupt_handler;
            adapter->remove_card       = hwi_smart16_remove_card;
            adapter->set_dio_address   = hwi_smart16_set_dio_address;

            install_success = hwi_smart16_install_card(
                                  adapter,
                                  download_image);

            break;
#endif
#ifndef FTK_NO_PCI
        case ADAPTER_CARD_PCI_BUS_TYPE :

            adapter->interrupt_handler = hwi_pci_interrupt_handler;
            adapter->remove_card       = hwi_pci_remove_card;
            adapter->set_dio_address   = hwi_pci_set_dio_address;


            install_success = hwi_pci_install_card(
                                  adapter,
                                  download_image);

            break;
#endif
#ifndef FTK_NO_PCMCIA
        case ADAPTER_CARD_PCMCIA_BUS_TYPE :

            adapter->interrupt_handler = hwi_pcmcia_interrupt_handler;
            adapter->remove_card       = hwi_pcmcia_remove_card;
            adapter->set_dio_address   = hwi_pcmcia_set_dio_address;

            install_success = hwi_pcmcia_install_card(adapter, download_image);

            break;
#endif
#ifndef FTK_NO_PCIT
        case ADAPTER_CARD_TI_PCI_BUS_TYPE :

            adapter->interrupt_handler = hwi_pcit_interrupt_handler;
            adapter->remove_card       = hwi_pcit_remove_card;
            adapter->set_dio_address   = hwi_pcit_set_dio_address;

            install_success = hwi_pcit_install_card(
                                  adapter,
                                  download_image);

            break;
#endif

#ifndef FTK_NO_PCI2
        case ADAPTER_CARD_PCI2_BUS_TYPE :

            adapter->interrupt_handler = hwi_pci2_interrupt_handler;
            adapter->remove_card       = hwi_pci2_remove_card;
            adapter->set_dio_address   = hwi_pci2_set_dio_address;

            install_success = hwi_pci2_install_card(
                                  adapter,
                                  download_image);

            break;
#endif

        default :

            /*
             * Bad adapter card bus type so fail.
             */

            install_success = FALSE;

            adapter->error_record.type = ERROR_TYPE_HWI;
            adapter->error_record.value = HWI_E_01_BAD_CARD_BUS_TYPE;

            break;
    }

    return install_success;
}


/****************************************************************************
*                                                                          
*                      hwi_initialize_adapter                              
*                      ======================                              
*                                                                          
*                                                                          
* PARAMETERS :                                                             
* ============                                                             
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
* INITIALIZATION_BLOCK * init_block                                        
*                                                                          
* This is the initialization block that is to be  copied  into  DIO  space 
* before performing the actual chipset initialization.                     
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The  hwi_initialize_adapter  routine  performs the initialization of the 
* chipset. It sets up the TI initialization  block  and  the  general  MAC 
* level smart software initialization parameters and copies these into DIO 
* space at 0x10A00 on the EAGLE. These are followed in DIO  space  by  the 
* Fastmac    module    specific   initialization   block   details.    The 
* initialization is started and then the routine waits up  to  11  seconds 
* for  success  or failure to be registered by the SIF interrupt register. 
* The DMAs that occur during initialization are also checked for  success. 
* Note  these DMAs may actually be done by PIO transfers by the HWI itself 
* for ATULA cards.                                                         
*                                                                          
* During downloading BYTE fields in structures  need  to  be  swapped  and 
* DWORDs  need to have the UINTs within swapped around. This is because of 
* the low-high byte ordering on Intel machines and the  high-low  ordering 
* of  the  EAGLE  and  the  byte  swapping  that automatically occurs when 
* downloading through the SIF.  Note  that  the  automatic  byte  swapping 
* means UINTs do not themselves need any special treatment.                
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine returns TRUE if it succeeds.  If this routine fails (returns 
* FALSE)  then a subsequent call to driver_explain_error, with the adapter 
* handle corresponding to the adapter parameter used here,  will  give  an 
* explanation.                                                             
*                                                                          
* On success we also exit  with  0x0001  in  the  EAGLE_SIDADRX  register. 
* This should never need to be altered by any user. This means for example 
* that  the  FTK  driver need only interest itself in the non-extended SIF 
* registers.                                                               
*                                                                          
****************************************************************************/                             

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_initialize_adapter)
#endif

#if 1
sys_int53( void);
#endif

export WBOOLEAN
hwi_initialize_adapter(
    ADAPTER *              adapter,
    INITIALIZATION_BLOCK * init_block
    )
{
    ADAPTER_HANDLE         handle              = adapter->adapter_handle;
    FASTMAC_INIT_PARMS *   fastmac_parms       = &init_block->fastmac_parms;
    WBOOLEAN               init_success;
    UINT                   i;
    BYTE FAR *             dma_test_scb_buff;
    BYTE FAR *             dma_test_ssb_buff;
    DWORD                  dma_test_scb_phys;
    DWORD                  dma_test_ssb_phys;
#ifdef FMPLUS
    WORD                   fmplus_buffer_size;
    DWORD                  fmplus_max_buffmem;
    WORD                   fmplus_max_buffers;
#endif

    /*
     * Set up the TI initialization parameters. Those fields not set up here
     * must be zero. Set up for burst DMA.
     */

    init_block->ti_parms.init_options = TI_INIT_OPTIONS_BURST_DMA;

    /*
     * Copy in 16/4 MC 32 configuration information. This data is zero for
     * non-16/4 MC 32 cards.
     */

    init_block->ti_parms.madge_mc32_config = adapter->mc32_config;

    /*
     * Set up retry counts.
     */

    init_block->ti_parms.parity_retry = TI_INIT_RETRY_DEFAULT;
    init_block->ti_parms.dma_retry    = TI_INIT_RETRY_DEFAULT;

    /*
     * NB It is not safe to use the Fastmac receive buffer for the DMA test 
     * because if auto-open is used, a frame could be received before we    
     * have a chance to test the contents of this memory. So put both SSB   
     * and SCB in the Tx buffer.                                            
     */

#ifdef FMPLUS
    /*
     * FMPlus does not have a transmit buffer it can use for the test DMAs, 
     * so we use a buffer previously allocated in the drv_init module.      
     */

    dma_test_scb_buff = (BYTE FAR *) adapter->dma_test_buf_virt;
    dma_test_scb_phys = adapter->dma_test_buf_phys;

    dma_test_ssb_phys = dma_test_scb_phys + SCB_TEST_PATTERN_LENGTH;
    dma_test_ssb_buff = dma_test_scb_buff + SCB_TEST_PATTERN_LENGTH;

#else
    /*
     * For Fastmac, we use the transmit buffer for the test DMAs.
     */

    dma_test_scb_buff = (BYTE FAR *) adapter->tx_buffer_virt;
    dma_test_scb_phys = adapter->tx_buffer_phys;

    dma_test_ssb_phys = dma_test_scb_phys + SCB_TEST_PATTERN_LENGTH;
    dma_test_ssb_buff = dma_test_scb_buff + SCB_TEST_PATTERN_LENGTH;

    if (adapter->transfer_mode != DMA_DATA_TRANSFER_MODE)
    {
	fastmac_parms->tx_buf_physaddr = adapter->tx_buffer_virt;

        fastmac_parms->rx_buf_physaddr = adapter->rx_buffer_virt;
    }

#endif

    if (adapter->transfer_mode != DMA_DATA_TRANSFER_MODE)
    {
        /*
         * PIO uses virtual addresses to save having to perform Phys-to-Virt 
         * conversions in the interrupt service routine.                     
         */

        init_block->ti_parms.scb_addr = (DWORD)dma_test_scb_buff;
        init_block->ti_parms.ssb_addr = (DWORD)dma_test_ssb_buff;
    }
    else
    {
        /*
         * DMA must use physical addresses, however. We already have these
         * saved.
         */

        init_block->ti_parms.scb_addr = dma_test_scb_phys;
        init_block->ti_parms.ssb_addr = dma_test_ssb_phys;
    }


    macro_word_swap_dword(init_block->ti_parms.scb_addr);
    macro_word_swap_dword(init_block->ti_parms.ssb_addr);

   #if 0
   /*
   *  TMSDebug support
   */
      printf("\nTMSDebug Enabled\n");
    init_block->smart_parms.reserved_1 = 3;
    sys_int53();
   #endif

    /*
     * Set up the smart software initialization parameters. Those fields
     * not set up here must be zero.                            
     * Set up header to identify the smart software initialization parms.
     */

    init_block->smart_parms.header.length    = sizeof(SMART_INIT_PARMS);
    init_block->smart_parms.header.signature = SMART_INIT_HEADER_SIGNATURE;
    init_block->smart_parms.header.version   = SMART_INIT_HEADER_VERSION;

    /* 
     * Byte swap the permanent node address when setting it up.
     */

    init_block->smart_parms.permanent_address = adapter->permanent_address;

    util_byte_swap_structure(
       (BYTE *) &init_block->smart_parms.permanent_address,
       sizeof(NODE_ADDRESS));

#ifdef FMPLUS
    /*
     * Must set min_buffer_ram field for backwards compatibility with other
     * Madge code (see FMPlus programming spec.)                            
     */

    init_block->smart_parms.min_buffer_ram = SMART_INIT_MIN_RAM_DEFAULT;
#endif

    /*
     * Need to byte swap fields in the Fastmac specific init parms.     
     * These fields are the opening node address and the buffer addresses.
     */

    util_byte_swap_structure(
       (BYTE *)&fastmac_parms->open_address,
       sizeof(NODE_ADDRESS));

#ifdef FMPLUS
    /*
     * Now work out the number of transmit and receive buffers.             
     * The number of buffers allocated depends on the maximum frame size    
     * anticipated, and on the amount of memory on the card. Unfortunately, 
     * one cannot know the maximum possible frame size until after the mac  
     * code has started and worked out what the ring speed is. Thus it is   
     * necessary to make an assumption about what the largest frame size    
     * supported will be.                                                   
     */

    /*
     * First, work out how big the buffers are. Unless a different size has 
     * been specified, use the default buffer size. 
     */                   

    fmplus_buffer_size = init_block->smart_parms.rx_tx_buffer_size;

    /*
     * MC32, EISA and PCIx cards use a smaller default buffer size than other
     * cards do (16/4 AT and 16/4 MC cards).                            
     */

    if (adapter->adapter_card_type == ADAPTER_CARD_TYPE_16_4_EISA  ||
        adapter->adapter_card_type == ADAPTER_CARD_TYPE_16_4_MC_32 ||
        adapter->adapter_card_type == ADAPTER_CARD_TYPE_16_4_PCI   ||
        adapter->adapter_card_type == ADAPTER_CARD_TYPE_16_4_PCIT  ||
        adapter->adapter_card_type == ADAPTER_CARD_TYPE_16_4_PCI2)
    {
        if (fmplus_buffer_size)
        {
            fmplus_buffer_size =
                max(FMPLUS_MIN_TXRX_BUFF_SIZE,
                    min(FMPLUS_DEFAULT_BUFF_SIZE_SMALL, fmplus_buffer_size)
                    );
        }
        else
        {
            fmplus_buffer_size = FMPLUS_DEFAULT_BUFF_SIZE_SMALL;
        }
    }
    else
    {
        if (fmplus_buffer_size)
        {
            fmplus_buffer_size =
                max(FMPLUS_MIN_TXRX_BUFF_SIZE, fmplus_buffer_size);
        }
        else
        {
            fmplus_buffer_size = FMPLUS_DEFAULT_BUFF_SIZE_LARGE;
        }
    }

    init_block->smart_parms.rx_tx_buffer_size = fmplus_buffer_size;

    if (fmplus_buffer_size < FMPLUS_MIN_TXRX_BUFF_SIZE)
    {
        adapter->error_record.type = ERROR_TYPE_HWI;
        adapter->error_record.value = HWI_E_10_BAD_TX_RX_BUFF_SIZE;
        return FALSE;
    }

    /*
     * Next, work out how much memory on the card we can use for buffers.
     */

    if (adapter->adapter_ram_size == 128)
    {
        fmplus_max_buffmem = FMPLUS_MAX_BUFFMEM_IN_128K;
    }
    else if (adapter->adapter_ram_size == 256)
    {
        fmplus_max_buffmem = FMPLUS_MAX_BUFFMEM_IN_256K;
    }
    else if (adapter->adapter_ram_size == 512)
    {
        fmplus_max_buffmem = FMPLUS_MAX_BUFFMEM_IN_512K;
    }
    else
    {
        fmplus_max_buffmem = FMPLUS_MAX_BUFFMEM_IN_128K;
    }

    /*
     * Use the two numbers worked out above to determine the maximum number 
     * of buffers we can fit on the card.                                   
     * The calculation is to round the buffer size up to the nearest 1K,    
     * and to divide that into the total amount of memory. The rounding up  
     * has to take account of the eight bytes buffer header that is added   
     * by FMP i.e. buffer_allocation = (buffer_size + 8 + 1023)   1024    
     * (there is also a "fudge-factor" of 5 buffers to allow the binary to  
     *  grow a little from its current size!)                               
     */

    fmplus_max_buffers = 
        (WORD)(fmplus_max_buffmem / ((fmplus_buffer_size + 1031) & ~1023)) -
        5;

    /*
     * Finally, allocate the buffers between transmit and receive. Notice   
     * that we allow here for frames as big as they are ever going to get   
     * on token ring. For smaller frames and 4 Mbps rings, this will just   
     * have the effect of improving back-to-back transmit performance.      
     */

    fastmac_parms->tx_bufs = 2 * 
                             ((fastmac_parms->max_frame_size +
                              fmplus_buffer_size) /
                             fmplus_buffer_size);

    fastmac_parms->rx_bufs = fmplus_max_buffers -
                             fastmac_parms->tx_slots -
                             fastmac_parms->tx_bufs;

    /*
     * When an error occurs is a little subjective at this point. It is, in 
     * fact, possible to receive a frame with only about three buffers, but 
     * for safety's sake, we demand that there be enough to receive a max.  
     * sized frame.                                                         
     */

    if (fastmac_parms->rx_bufs < fastmac_parms->tx_bufs)
    {
        adapter->error_record.type = ERROR_TYPE_HWI;
        adapter->error_record.value = HWI_E_11_TOO_MANY_TX_RX_BUFFS;
        return (FALSE);
    }

#else
    macro_word_swap_dword(fastmac_parms->tx_buf_physaddr);
    macro_word_swap_dword(fastmac_parms->rx_buf_physaddr);
#endif

    /*
     * Inform the system about the IO ports we are going to access.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    /* 
     * Download initialization block to required location in DIO space.
     * Note routine leaves 0x0001 in EAGLE SIFADRX register.
     */

    hwi_copy_to_dio_space(
        adapter,
        DIO_LOCATION_INIT_BLOCK,
        (BYTE *) init_block,
        sizeof(INITIALIZATION_BLOCK));

    /*
     * After downloading byte swap back some Fastmac specific init parms.
     * Fields are the opening node address and the Fastmac buffer addresses.
     * This is especially important to do for the buffer addresses
     * because they are used in transmit/receive routines.
     */

    util_byte_swap_structure(
       (BYTE *)&fastmac_parms->open_address,
       sizeof(NODE_ADDRESS));

#ifndef FMPLUS
    macro_word_swap_dword(fastmac_parms->tx_buf_physaddr);
    macro_word_swap_dword(fastmac_parms->rx_buf_physaddr);

    /*
     * The  mainline  transmit receive  code  assumes  that these values ar
     * physical addresses, so we had better  convert  them  back...  The al- 
     * ternative would be to rewrite all the transmit  and  receive  modules 
     * to note which transfer mode is in use.                                
     */
                      
    if (adapter->transfer_mode != DMA_DATA_TRANSFER_MODE)
    {
	fastmac_parms->tx_buf_physaddr = adapter->tx_buffer_phys;

        fastmac_parms->rx_buf_physaddr = adapter->rx_buffer_phys;
    }

#endif

    /*
     * Start initialization by output 0x9080 to SIF interrupt register.
     */

    sys_outsw(handle, adapter->sif_int, EAGLE_INIT_START_CODE);

    /* 
     * Wait for a valid initialization code, may wait 11 seconds.
     * During this process test DMAs need to occur, hence in PIO mode needs
     * calls to hwi_interrupt_entry, hence need interrupts or polling active.
     */

    init_success = hwi_get_init_code(adapter);

    /*
     * Let the system know we have finished accessing the IO ports.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io( adapter );
#endif

    if (!init_success)
    {
        return FALSE;
    }

    /*
     * Check that test DMAs were successful.
     */

    /* 
     * First check SCB for correct test pattern. Remember used Fastmac
     * transmit buffer address for SCB address.
     */

    for (i = 0; i < SCB_TEST_PATTERN_LENGTH; i++)
    {
        if (dma_test_scb_buff[i] != scb_test_pattern[i])
        {
            adapter->error_record.type = ERROR_TYPE_HWI;
            adapter->error_record.value = HWI_E_07_FAILED_TEST_DMA;
            return FALSE;
        }
    }

    /*
     * Now check SSB for correct test pattern. Remember used Fastmac
     * receive buffer address for SSB address.
     */

    for (i = 0; i < SSB_TEST_PATTERN_LENGTH; i++)
    {
        if (dma_test_ssb_buff[i] != ssb_test_pattern[i])
        {
            /* fill in error record and fail if pattern doesn't match       */

            adapter->error_record.type = ERROR_TYPE_HWI;
            adapter->error_record.value = HWI_E_07_FAILED_TEST_DMA;
            return FALSE;
        }
    }

    /*
     * Successful completion of initialization.
     */

    return TRUE;
}


/****************************************************************************
*                                                                          
*                      hwi_get_node_address_check                          
*                      ==========================                          
*                                                                          
*                                                                          
* PARAMETERS :                                                             
* ============                                                             
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_get_node_address_check routine  reads  the  adapter's  permanent 
* node  address  from the Fastmac status block (STB) in DIO space. It then 
* checks to see if this node address  is  a  valid  Madge  address.   This 
* checks that Fastmac is correctly installed.                              
*                                                                          
* For  ATULA and MC cards, the adapter node address has actually been read 
* once already. There is no harm in getting it  again, from  Fastmac  this 
* time;  (previously  it  was read from the BIA PROM). For EISA cards, the 
* node address has not been read previously. This is the first time we can 
* get it. The node address is not readable from the host with EISA  cards. 
* Only Fastmac can find it out for us.                                     
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine returns TRUE if it succeeds.  If this routine fails (returns 
* FALSE)  then a subsequent call to driver_explain_error, with the adapter 
* handle corresponding to the adapter parameter used here,  will  give  an 
* explanation.                                                             
*                                                                          
****************************************************************************/     

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_get_node_address_check)
#endif

export WBOOLEAN
hwi_get_node_address_check(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE handle  = adapter->adapter_handle;

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    /*
     * Get the permanent node address from the STB in DIO space.
     * Record address in adapter structure.
     */

    sys_outsw( 
        handle,
        adapter->sif_adr,
        (WORD)(card_t)&adapter->stb_dio_addr->permanent_address);

    sys_rep_insw(
        handle,
        adapter->sif_datinc,
        (BYTE *)&adapter->permanent_address,
        (sizeof(NODE_ADDRESS) / 2));

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io( adapter );
#endif

    /*
     * Byte swap node address after reading from adapter.
     */

    util_byte_swap_structure(
        (BYTE *) &adapter->permanent_address,
        sizeof(NODE_ADDRESS));

    return TRUE;
}


/****************************************************************************
*                                                                          
*                      hwi_interrupt_entry                                 
*                      ===================                                 
*                                                                          
*                                                                          
* PARAMETERS :                                                             
* ============                                                             
*                                                                          
* UINT interrupt_number                                                    
*                                                                          
* This  is the interrupt number of the interrupting adapter card. If it is 
* zero (POLLING_INTERRUPTS_MODE) then all cards must be checked to see  if 
* they require servicing.                                                  
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_interrupt_entry routine is either called by an operating  system 
* specific  interrupt detection routine with an actual interrupt number as 
* the parameter   or,   at   intervals,   by   a   routine   passing   the 
* POLLING_INTERRUPTS_MODE parameter. In the former case all adapters using 
* the  given  interrupt  number are checked for outstanding interrupts. In 
* the latter case, ALL adapters are checked for outstanding interrupts.    
*                                                                          
* It is important  that  hwi_interrupt_entry  is  not  re-entered  with  a 
* subsequent  interrupt  on the same adapter.  Hence, in the polling case, 
* the routine that called hwi_interrupt_entry must not call it again until 
* the hwi finishes it's current execution.  If real interrupts  are  being 
* used,  then  care  must  be  taken not to turn interrupts on and allow a 
* further interrupt to cause hwi_interrupt_entry to be re-entered.         
*                                                                          
* The details of interrupt  handling  are  dealt  with  by  card  specific 
* interrupt   handlers.   In  general,  for  SIF Fastmac  interrupts,  th
* interrupt is acknowledged at the  SIF.  Driver_interrupt_entry  is  then 
* called with the interrupt number, details of the  relevant  adapter  and 
* the  contents of the EAGLE_SIFINT register. If instead a PIO transfer is 
* required, then the  necessary data transfer  is  performed  between  the 
* adapter and host memory. PIO can only occur with ATULA adapter cards.    
*                                                                          
*                                                                          
* Note on increasing speed:                                                
*                                                                          
* One  way  of  speeding up execution of the interrupt routine would be to 
* replace the sys_outsw and sys_insw routines by similar routines supplied 
* with your C compiler and have  them  compiled  in-line.  This  would  be 
* particularly advantageous when handling PIO data transfer.               
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* This routine always completes successfully.                              
*                                                                          
****************************************************************************/

#ifdef FTK_IRQ_FUNCTION
#pragma FTK_IRQ_FUNCTION(hwi_interrupt_entry)
#endif

export void
hwi_interrupt_entry(
    ADAPTER_HANDLE adapter_handle,
    WORD           interrupt_number
    )
{
    ADAPTER *      adapter;

#ifndef FTK_NO_SHARED_IRQ_POLL
    for (adapter_handle = 0;
         adapter_handle < MAX_NUMBER_OF_ADAPTERS;
         adapter_handle++)
    {
#endif

     /*
      * Get pointer to adapter structure. 
      */                            

    adapter = adapter_record[adapter_handle];

    /*
     * In polling mode check ALL adapters.
     * In interrupt mode check all adapters with correct int number.     
     * Only check actual running, working adapters.
     */                 

    if ((adapter != NULL) &&
        (adapter->adapter_status == ADAPTER_RUNNING) &&
        ((interrupt_number == POLLING_INTERRUPTS_MODE) ||
         (adapter->interrupt_number == interrupt_number)))
    {
        /*
         * Interrupts are handled by adapter modules.
         */

        (*(adapter->interrupt_handler))(adapter);
    }

#ifndef FTK_NO_SHARED_IRQ_POLL
    }
#endif

    return;
}


/****************************************************************************
*                                                                          
*                      hwi_remove_adapter                                  
*                      ==================                                  
*                                                                          
*                                                                          
* PARAMETERS :                                                             
* ============                                                             
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The  hwi_remove_adapter  routine  uses  the  card  bus  type to call the 
* correct hwi_<card_type>_remove_card routine.                             
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                          
****************************************************************************/         

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_remove_adapter)
#endif
                                                                 
export void
hwi_remove_adapter(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE handle  = adapter->adapter_handle;

    /*
     * Call correct remove routine.
     */  

    (*(adapter->remove_card))(adapter);

    return;
}

/****************************************************************************
*
* UPCALL PROCEDURES - Used by hwi_<card type>.c modules                            
*
****************************************************************************/

/****************************************************************************
*                                                                          
*                      hwi_halt_eagle                                      
*                      ==============                                      
*                                                                          
* The hwi_halt_eagle routine halts the EAGLE. It does this by setting  the 
* halt EAGLE bit in the SIF adapter  control register. It also  resets the 
* adapter by twiddling the adapter reset bit in the same register.         
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_halt_eagle)
#endif

export void
hwi_halt_eagle(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE handle                       = adapter->adapter_handle;
    WORD           sif_adapter_control_register = adapter->sif_acl;
    WORD           acontrol_output;

    /*
     * Set output for SIF register EAGLE_ACONTROL.                           
     * Maintain parity; set halt, reset, enable SIF interrupts.
     */

    acontrol_output = (sys_insw(handle, sif_adapter_control_register) &
                      EAGLE_SIFACL_PARITY) |
                      EAGLE_SIFACL_ARESET  |
                      EAGLE_SIFACL_CPHALT  |
                      EAGLE_SIFACL_BOOT    |
                      EAGLE_SIFACL_SINTEN  |
                      adapter->nselout_bits;

    if (adapter->EaglePsDMA)
    {
        acontrol_output |= EAGLE_SIFACL_PSDMAEN;
    }

    /*
     * Wait at least 14 microseconds before putting adapter in reset state.  
     * We may have just taken adapter out of reset state.
     * 14us is the minimum time must hold ARESET low between resets.
     * Disable and re-enable accessing IO locations around wait so 
     * OS can reschedule this task and not effect others running.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io( adapter );
#endif

    sys_wait_at_least_microseconds(14);

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io( adapter );
#endif

    /*
     * Output to SIF register EAGLE_ACONTROL to halt EAGLE.
     */             

    sys_outsw(
        handle,
        sif_adapter_control_register,
        acontrol_output);

    /*
     * Wait at least 14 microseconds before taking adapter out of reset
     * state.  
     * 14us is the minimum time must hold ARESET low between resets.
     * Disable and re-enable accessing IO locations around wait so 
     * OS can reschedule this task and not effect others running.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io( adapter );
#endif

    sys_wait_at_least_microseconds(14);

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io( adapter );
#endif

    /*
     * Bring EAGLE out of reset state, maintain halt status.
     */

    sys_outsw( 
        handle,
        sif_adapter_control_register,
        (WORD) (acontrol_output & ~EAGLE_SIFACL_ARESET));

    return;
}


/****************************************************************************
*                                                                          
*                      hwi_download_code                                   
*                      =================                                   
*                                                                          
* The  hwi_download_code  routine downloads the given data to the adapter. 
* This must be done with the EAGLE halted. Besides details of the  adapter 
* and a pointer to the first download reocrd of the  download  image,  the 
* routine  is  passed  a  helper routine for setting DIO addresses for the 
* actual downloading.                                                      
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_download_code)
#endif

export WBOOLEAN
hwi_download_code(
    ADAPTER *          adapter,
    DOWNLOAD_RECORD *  download_record,
    void               (*set_dio_address)(ADAPTER *, DWORD)
    )
{
    ADAPTER_HANDLE     handle                = adapter->adapter_handle;
    WORD               sif_data_inc_register = adapter->sif_datinc;
    UINT               i;

    /*
     * If there is no code to be downloaded then fail.                       
     */

    if (download_record == NULL)
    {
        adapter->error_record.type = ERROR_TYPE_HWI;
        adapter->error_record.value = HWI_E_0A_NO_DOWNLOAD_IMAGE;
        return FALSE;
    }

    /*
     * The first record in the image must be a MODULE type record.
     */

    if (download_record->type != DOWNLOAD_RECORD_TYPE_MODULE)
    {
        adapter->error_record.type = ERROR_TYPE_HWI;
        adapter->error_record.value = HWI_E_09_BAD_DOWNLOAD_IMAGE;
        return FALSE;
    }

    /*
     * The code to be downloaded must be Fastmac (Plus).
     */

    if ((download_record->body.module.download_features &
            DOWNLOAD_FASTMAC_INTERFACE) == 0)
    {
        adapter->error_record.type = ERROR_TYPE_HWI;
        adapter->error_record.value = HWI_E_09_BAD_DOWNLOAD_IMAGE;
        return FALSE;
    }

    /*
     * Get the next download record.
     */

    macro_get_next_record(download_record);

    /* 
     * Now download the data; a zero length record marks the end.
     */

    while (download_record->length != 0)
    {
        /*
         * the action depends on type of record.
         */

        if (download_record->type == DOWNLOAD_RECORD_TYPE_DATA_32)
        {
            /*
             * Set DIO address for downloading data to in SIF registers.
             */

            (*set_dio_address)(
                adapter,
                download_record->body.data_32.dio_addr);

            /*
             * Download data.
             */

            for (i = 0; i < download_record->body.data_32.word_count; i++)
            {
                sys_outsw(
                    handle,
                    sif_data_inc_register,
                    download_record->body.data_32.data[i]);
            }

            /*
             * Check download worked by reading back and comparing.
             */

            (*set_dio_address)(
                adapter,
                download_record->body.data_32.dio_addr);

            for (i = 0; i < download_record->body.data_32.word_count; i++)
            {
                if (sys_insw(handle, sif_data_inc_register) !=
                        download_record->body.data_32.data[i])
                {
                    /*
                     * Fill in error record if read back not correct.
                     */

                    adapter->error_record.type = ERROR_TYPE_HWI;
                    adapter->error_record.value = HWI_E_08_BAD_DOWNLOAD;
                    return FALSE;
                }
            }
        }
        else if (download_record->type == DOWNLOAD_RECORD_TYPE_FILL_32)
        {
            /*
             * Set DIO address for downloading to in SIF registers.
             */

            (*set_dio_address)(
                adapter,
                download_record->body.fill_32.dio_addr);

            /*
             * Fill EAGLE memory with required pattern.
             */

            for (i = 0; i < download_record->body.fill_32.word_count; i++)
            {
                sys_outsw(
                    handle,
                    sif_data_inc_register,
                    download_record->body.fill_32.pattern);
            }

            /*
             * Check download worked by reading back and comparing.
             */

            (*set_dio_address)(
                adapter,
                download_record->body.fill_32.dio_addr);

            for (i = 0; i < download_record->body.fill_32.word_count; i++)
            {
                WORD x;

                if ((x = sys_insw(handle, sif_data_inc_register)) !=
                        download_record->body.fill_32.pattern)
                {
                    /*
                     * Fill in error record if read back not correct.
                     */

                    adapter->error_record.type = ERROR_TYPE_HWI;
                    adapter->error_record.value = HWI_E_08_BAD_DOWNLOAD;
                    return FALSE;
                }
            }
        }
        else
        {
            /*
             * Can only have DATA and FILL records after first MODULE.
             */

            adapter->error_record.type = ERROR_TYPE_HWI;
            adapter->error_record.value = HWI_E_09_BAD_DOWNLOAD_IMAGE;
            return FALSE;
        }

        /*
         * Get the next download record.
         */

        macro_get_next_record(download_record);
    }

    /*
     * Successful downloading complete.
     */

    return TRUE;
}


/****************************************************************************
*                                                                          
*                      hwi_start_eagle                                     
*                      ===============                                     
*                                                                          
* The hwi_start_eagle routine takes the EAGLE out of the halt state it  is 
* in.                                                                      
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_start_eagle)
#endif

export void
hwi_start_eagle(
    ADAPTER * adapter
    )
{
    WORD      sif_adapter_control_register = adapter->sif_acl;

    /*
     * Only change the halt status in the SIF register EAGLE_ACONTROL.
     */

    sys_outsw(
        adapter->adapter_handle,
        sif_adapter_control_register,
        (WORD) (sys_insw(
                    adapter->adapter_handle,
                    sif_adapter_control_register) & ~EAGLE_SIFACL_CPHALT));

}


/****************************************************************************
*                                                                          
*                      hwi_get_bring_up_code                               
*                      =====================                               
*                                                                          
* The hwi_get_bring_up_code routine waits for at least 15  seconds  for  a 
* valid bring up code to appear in the SIF interrupt register. If bring up 
* fails then this routine will retry up to 10 times. If even this fails,
* then an error record is filled in.
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_get_bring_up_code)
#endif

export WBOOLEAN
hwi_get_bring_up_code(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE handle                 = adapter->adapter_handle;
    WORD           sif_interrupt_register = adapter->sif_int;
    UINT           index;
    BYTE           bring_up_code;
    BYTE           bring_up_error;
    UINT           retry;

    /*
     * We'll retry 10 times and if we don't get any other error we'll
     * return a timeout.
     */

    retry          = 10;
    bring_up_error = BRING_UP_E_10_TIME_OUT;

    do
    {
        retry--;

        for (index = 0; index < 60; index++)
        {
            /*
             * No bring up code available yet. Wait at least a quarter of a
             * second before trying again. Disable and re-enable accessing IO
             * locations around wait so delay can reschedule this task and not
             * effect others running.
             */

#ifndef FTK_NO_IO_ENABLE
            macro_disable_io( adapter );
#endif

            sys_wait_at_least_milliseconds(250); 

#ifndef FTK_NO_IO_ENABLE
            macro_enable_io( adapter );
#endif

            /*
             * Get bring up code from SIFSTS register.
             */

            bring_up_code = sys_insb(handle, sif_interrupt_register);

            /*
             * Check for successful bring up in top nibble of SIFSTS register.
             * Success is shown by INITIALIZE bit being set.
             */

            if ((bring_up_code & EAGLE_BRING_UP_TOP_NIBBLE) ==
                     EAGLE_BRING_UP_SUCCESS)
            {
                return TRUE;
            }

            /* 
             * Check for failed bring up in top nibble of SIFSTS register.
             * Failure is shown by the TEST and ERROR bits being set.
             */

            if ((bring_up_code & EAGLE_BRING_UP_TOP_NIBBLE) ==
                    EAGLE_BRING_UP_FAILURE)
            {
                /*
                 * Get actual bring up error code from bottom nibble of 
                 * SIFSTS.
                 */

                bring_up_error = bring_up_code & EAGLE_BRING_UP_BOTTOM_NIBBLE;
                break;
            }
        }

        /*
         * We have failed to do a bring up, if retry hasn't reached
         * zero yet then write 0xff00 to SIFINT to reset the Eagle
         * and start bring up again.
         */

        if (retry > 0)
        {
            sys_outsw(handle, sif_interrupt_register, (WORD) 0xff00);
        }
    }
    while (retry > 0);

    /*
     * We have completely failed bring up so return an error.
     */

    adapter->error_record.type  = ERROR_TYPE_BRING_UP;
    adapter->error_record.value = bring_up_error;
    
    return FALSE;
}


/****************************************************************************
*                                                                          
*                      hwi_get_max_frame_size                              
*                      ======================                              
*                                                                          
* The hwi_get_max_frame_size routine uses the ring speed register  in  DIO 
* space  to find the ring speed and hence the maximum allowable frame size 
* set by the ISO standard.                                                 
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_get_max_frame_size)
#endif

export WORD
hwi_get_max_frame_size(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE handle                   = adapter->adapter_handle;
    WORD           sif_dio_address_register = adapter->sif_adr;
    WORD           sif_dio_data_register    = adapter->sif_dat;

    /*
     * Set DIO address register to point to ring speed register.             
     * Note that SIFADRX already equals 0x0001 (for DATA page at 0x10000).
     */

    sys_outsw(
        handle,
        sif_dio_address_register,
        DIO_LOCATION_RING_SPEED_REG);

    /* 
     * Use the contents of the ring speed register to determine ring speed.
     */

    if (sys_insw(handle, sif_dio_data_register) & RING_SPEED_REG_4_MBITS_MASK)
    {
        return MAX_FRAME_SIZE_4_MBITS;
    }
    else
    {
        return MAX_FRAME_SIZE_16_MBITS;
    }

}


/****************************************************************************
*                                                                          
*                      hwi_get_ring_speed                                  
*                      ==================                                  
*                                                                          
* The hwi_get_ring_speed routine uses the ring speed register  in  DIO     
* space  to find the ring speed.                                           
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_get_ring_speed)
#endif

export UINT
hwi_get_ring_speed(
    ADAPTER *      adapter
    )
{
    WORD           sif_dio_address_register = adapter->sif_adr;
    WORD           sif_dio_data_register    = adapter->sif_dat;
    ADAPTER_HANDLE hnd                      = adapter->adapter_handle;

    /*
     * Set DIO address register to point to ring speed register.
     * Note that SIFADRX already equals 0x0001 (for DATA page at 0x10000).
     */

    sys_outsw(
        hnd,
        sif_dio_address_register,
        DIO_LOCATION_RING_SPEED_REG);

    /* 
     * Use the contents of the ring speed register to determine ring speed.
     */

    if (sys_insw(hnd, sif_dio_data_register) & RING_SPEED_REG_4_MBITS_MASK)
    {
        return 4;
    }
    else
    {
        return 16;
    }

}


/*---------------------------------------------------------------------------
|                                                                          
| LOCAL PROCEDURES                                    
|                                                                          
---------------------------------------------------------------------------*/

/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_copy_to_dio_space                               
|                      =====================                               
|                                                                          
| The  hwi_copy_to_dio_space  routine copies the given amount of data into 
| DIO space at the specified location. The method of setting  up  the  SIF 
| DIO address registers depends on the type of adapter card.               
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_copy_to_dio_space)
#endif

local void
hwi_copy_to_dio_space(
    ADAPTER *      adapter,
    DWORD          dio_location,
    BYTE *         download_data,
    WORD           data_length_bytes
    )
{
    ADAPTER_HANDLE handle            = adapter->adapter_handle;

    /*
     * Set up DIO address in SIF DIO address registers.
     * This needs to be done differently depending on adapter card type.
     */

    (*(adapter->set_dio_address))(adapter, dio_location);

    /*
     * Copy data into DIO space.
     */
    sys_rep_outsw(
        handle,
        adapter->sif_datinc,
        download_data,
        (WORD) (data_length_bytes / 2));

    if (data_length_bytes & 1)
    {
      /*
      *  Byte out instructions do not work on the Ti PCI card,
      *  as it is not definate whether we ship this card, the fix
      *  shall stay here for now. PRR
      */
      if (adapter->adapter_card_type != ADAPTER_CARD_TYPE_16_4_PCIT)
      {
         sys_outsb(
            handle,
            adapter->sif_datinc,
            *(download_data + data_length_bytes - 1));
      }
      #ifndef  FTK_NO_PCIT
      else
      {
         WORD  last_byte;
         last_byte = *(download_data + data_length_bytes - 1);
         last_byte <<= 8;
         last_byte &= 0xFF00;
         sys_outsw(
            handle,
            adapter->sif_datinc,
            last_byte);

      }
      #endif
    }

    return;
}


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_read_sifint
|                      ===============                                   
|                                                                          
| Read the SIFINT register. This function is called via 
Ý sys_sych_with_interruptto avoid DMA/SIF problems on PciT adapters.
Ý
---------------------------------------------------------------------------*/

local WBOOLEAN
hwi_read_sifint(
    void * ptr
    );

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_read_sifint)
#endif

local WBOOLEAN
hwi_read_sifint(
    void * ptr
    )
{
    ADAPTER * adapter = (ADAPTER *) ptr;

    return sys_insb(adapter->adapter_handle, adapter->sif_int);
}


/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_get_init_code                                   
|                      =================                                   
|                                                                          
| The  hwi_get_init_code routine waits for at least 11 seconds for a valid 
| initialization  code  to  appear  in  the  SIF  interrupt  register.  If 
| initialization  fails  then  this  routine  fills  in  the adapter error 
| record.                                                                  
|                                                                          
---------------------------------------------------------------------------*/


#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_get_init_code)
#endif

local WBOOLEAN
hwi_get_init_code(
    ADAPTER * adapter
    )
{
    UINT      index;
    BYTE      init_code;

    for (index = 0; index < 100; index++)
    {
        /*
         * Get initialization code from SIFSTS register.
         */

        init_code = (BYTE) sys_sync_with_interrupt(
                               adapter->adapter_handle, 
                               hwi_read_sifint, 
                               (void *) adapter
                               );

        /* 
         * Check for successful initialization in top nibble of SIFSTS.
         * Success is shown by INITIALIZE, TEST and ERROR bits being zero.
         */

        if ((init_code & EAGLE_INIT_SUCCESS_MASK) == 0)
        {
            return TRUE;
        }

        /*
         * Check for failed initialization in top nibble of SIFSTS.
         * Failure is shown by the ERROR bit being set.
         */

        if ((init_code & EAGLE_INIT_FAILURE_MASK) != 0)
        {
            /*
             * Get actual init error code from bottom nibble of SIFSTS
             */

            init_code = init_code & EAGLE_INIT_BOTTOM_NIBBLE;

            /*
             * Fill in error record with actual initialization error code.
             */

            adapter->error_record.type = ERROR_TYPE_INIT;
            adapter->error_record.value = init_code;
            return FALSE;
        }

        /*
         * No initialization code available yet. Wait at least a quarter of
         * a second before trying again. Disable and re-enable accessing IO
         * locations around wait so OS can reschedule this task and not
         * effect others running.
         */

#ifndef FTK_NO_IO_ENABLE
        macro_disable_io( adapter );
#endif

        sys_wait_at_least_milliseconds(250);

#ifndef FTK_NO_IO_ENABLE
        macro_enable_io( adapter );
#endif
        
    }

    /* 
     * At least 11 seconds have gone so return time out failure.
     */

    adapter->error_record.type = ERROR_TYPE_INIT;
    adapter->error_record.value = INIT_E_10_TIME_OUT;
    return FALSE;

}


/******* End of HWI_GEN.C **************************************************/