summaryrefslogblamecommitdiffstats
path: root/private/ntos/ndis/madge/driver/hwi_sm16.c
blob: 75c637c2155ebcef95b3f79cf265775afb002a8e (plain) (tree)
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






































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                  
/****************************************************************************
*                                                                          
* HWI_SM16.C : Part of the FASTMAC TOOL-KIT (FTK)                     
* 
* HARDWARE INTERFACE MODULE FOR SMART 16 CARDS
*                                                                         
* Copyright (c) Madge Networks Ltd. 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_SM16.C module contains the routines specific to the Smart16 card 
* which are necessary to  install an adapter, to initialize an adapter, to  
* remove an adapter, and to handle interrupts on an adapter.               
*                                                                          
/***************************************************************************/

/*---------------------------------------------------------------------------
|                                                                          
| 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  */

#ifndef FTK_NO_SMART16

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

local WBOOLEAN
hwi_smart16_read_node_address(
    ADAPTER * adapter
    );

local WBOOLEAN
hwi_smart16_valid_io_location(
    WORD   io_location
    );

local WBOOLEAN
hwi_smart16_valid_transfer_mode(
    UINT   transfer_mode
    );

#ifndef FTK_NO_PROBE

local WBOOLEAN
hwi_smart16_check_for_card(
    WORD  io_location
    );

#endif

local WBOOLEAN 
hwi_smart16_valid_irq_channel(
    ADAPTER * adapter
    );

#ifndef FTK_NO_PROBE
/****************************************************************************
*
*                        hwi_smart16_probe_card
*                        ======================
*
*
* PARAMETERS (passed by hwi_probe_adapter) :
* ==========================================
*
* 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. For smart 16 adapters with should be a subset of
* {0x4a20, 0x4e20, 0x6a20, 0x6e20}.
*
* UINT    number_locations
*
* This is the number of IO locations in the above list.
*
* BODY :
* ======
* 
* The  hwi_smart16_probe_card routine is called by  hwi_probe_adapter.  It
* checks for the existence of a card by reading its node address. This is 
* about all we can do for a smart 16.
*
*
* RETURNS :
* =========
* 
* The routine returns the number of adapters found, or PROBE_FAILURE if
* there's a problem.
*
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_smart16_probe_card)
#endif

export UINT
hwi_smart16_probe_card(
    PROBE *    resources,
    UINT       length,
    WORD *     valid_locations,
    UINT       number_locations
    )
{
    WORD       control_1; 
    WORD       control_2; 
    UINT       i;
    UINT       j;

    /*
     * Check the bounds are sensible.
     */

    if(length <= 0 || number_locations <= 0)
    {
        return PROBE_FAILURE;
    }

    /*
     * Range check the IO locations.
     */

    for(i = 0; i < number_locations; i++)
    {
        if(!hwi_smart16_valid_io_location(valid_locations[i]))
        {
           return PROBE_FAILURE;
        }
    }    

    /*
     * j is the number of adapters found.
     */

    j = 0;

    for(i = 0; i < number_locations; i++)
    {
        /* 
         * Check we aren't out of PROBE structures.
         */

        if(j >= length)
        {
           return j;
        }

        /*
         * Set up the control register locations.
         */

        control_1       = valid_locations[i] + SMART16_CONTROL_REGISTER_1;
        control_2       = valid_locations[i] + SMART16_CONTROL_REGISTER_2;

#ifndef FTK_NO_IO_ENABLE
        macro_probe_enable_io(valid_locations[i], SMART16_IO_RANGE);
#endif
        /*
         * Reset the card.
         */

        sys_probe_outsb(control_1, 0);

        if (hwi_smart16_check_for_card(valid_locations[i]))
        {

           /*
            * We have obviously found a valid smart 16 by this point so
            * set up some values.
            */

           resources[j].adapter_card_bus_type = ADAPTER_CARD_SMART16_BUS_TYPE;
           resources[j].adapter_card_type     = ADAPTER_CARD_TYPE_SMART_16;
           resources[j].adapter_card_revision = ADAPTER_CARD_SMART_16;
           resources[j].adapter_ram_size      = 128;
           resources[j].io_location           = valid_locations[i];
           resources[j].interrupt_number      = SMART16_DEFAULT_INTERRUPT;
           resources[j].dma_channel           = 0;
           resources[j].transfer_mode         = PIO_DATA_TRANSFER_MODE;

           /*
            * And increment j to point at the next free PROBE structure.
            */

           j++;
        }

#ifndef FTK_NO_IO_ENABLE
        macro_probe_disable_io(resources->io_location, SMART16_IO_RANGE);
#endif

    }

    return j;
}
#endif

/****************************************************************************
*                                                                          
*                      hwi_smart16_install_card                            
*                      ========================                            
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_install_adapter) :                             
* ============================================                             
*                                                                          
* 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 :                                                                   
* ======                                                                   
*                                                                          
* hwi_smart16_install_card is called by  hwi_install_adapter.  It  sets up 
* the adapter card and downloads the required  code  to  it.  Firstly,  it 
* checks there is a valid adapter at the required IO  address  by  reading 
* the node address from the BIA PROM. It then sets up  and  checks various 
* on-board registers for correct operation.                                
*                                                                          
* Then, it halts the EAGLE, downloads the code,  restarts  the  EAGLE  and 
* waits  up to 3 seconds  for  a valid  bring-up  code. If  interrupts are 
* required,  these  are  enabled  by  operating  system  specific   calls. 
* The adapter is set up for Eagle Pseudo-DMA, since real DMA is not used.  
*                                                                          
*                                                                          
* 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_smart16_install_card)
#endif

export WBOOLEAN
hwi_smart16_install_card(
    ADAPTER *        adapter,
    DOWNLOAD_IMAGE * download_image
    )
{
    ADAPTER_HANDLE   adapter_handle = adapter->adapter_handle;
    WORD             control_1      = adapter->io_location +
                                          SMART16_CONTROL_REGISTER_1;
    WORD             control_2      = adapter->io_location +
                                          SMART16_CONTROL_REGISTER_2;
    WORD             sif_base;

    /*
     * Check the IO location is valid.
     */

    if(!hwi_smart16_valid_io_location(adapter->io_location))
    {
        adapter->error_record.type  = ERROR_TYPE_HWI;
        adapter->error_record.value = HWI_E_02_BAD_IO_LOCATION;

        return FALSE;
    }

    /*
     * Check the transfer mode is valid.
     */

    if(!hwi_smart16_valid_transfer_mode(adapter->transfer_mode))
    {
        adapter->error_record.type  = ERROR_TYPE_HWI;
        adapter->error_record.value = HWI_E_02_BAD_IO_LOCATION;

        return FALSE;
    }

    if (!hwi_smart16_valid_irq_channel(adapter))
    {
        return FALSE;
    }

    /*
     * Record the locations of the SIF registers.
     */

    sif_base = adapter->io_location + SMART16_FIRST_SIF_REGISTER;

    adapter->sif_dat     = sif_base + EAGLE_SIFDAT;
    adapter->sif_datinc  = sif_base + EAGLE_SIFDAT_INC;
    adapter->sif_adr     = sif_base + EAGLE_SIFADR;
    adapter->sif_int     = sif_base + EAGLE_SIFINT;
    adapter->sif_acl     = sif_base + EAGLE_SIFACL;
    adapter->sif_adr2    = sif_base + EAGLE_SIFADR;
    adapter->sif_adx     = sif_base + EAGLE_SIFADX;
    adapter->sif_dmalen  = sif_base + EAGLE_DMALEN;
    adapter->sif_sdmadat = sif_base + EAGLE_SDMADAT;
    adapter->sif_sdmaadr = sif_base + EAGLE_SDMAADR;
    adapter->sif_sdmaadx = sif_base + EAGLE_SDMAADX;

    adapter->io_range    = SMART16_IO_RANGE;

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    /* 
     * You might want to check that we have not already checked for a card
     * at this address (or its rev3/4 equivalent). 
     */

    /* 
     * Reset adapter (SMART16_CTRL1_SRESET = 0). This is necessary for
     * reading the BIA.                                                     
     */

    sys_outsb(adapter_handle, control_1, 0);

    /* 
     * Read the node address for the specified IO location. This will check 
     * that it is a valid Madge address, which is the only way we have of   
     * identifying the card.                                                
     */

    if (!hwi_smart16_read_node_address(adapter))
    {
        /*
         * Fill in error record and return                                  
         */

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

#ifndef FTK_NO_IO_ENABLE
        macro_disable_io(adapter);
#endif

        return FALSE;
    }

    /*
     * Make sure that SCS bit is zero (see below where we bring card out of 
     * reset).                                                              
     */

    sys_outsb(adapter_handle, control_1, 0);

    /*
     * nselout_bits are needed to select the IRQ on the card.
     */

    switch (adapter->interrupt_number)
    {
        case 2  :
            adapter->nselout_bits = SMART16_IRQ_2;
            break;
        case 3  :
            adapter->nselout_bits = SMART16_IRQ_3;
            break;
        case 7  :
            adapter->nselout_bits = SMART16_IRQ_7;
            break;
        default :
            break;

    }
  
    /*
     * These things can all be assumed for the smart16.                     
     */

    adapter->adapter_card_type     = ADAPTER_CARD_TYPE_SMART_16;
    adapter->adapter_card_revision = ADAPTER_CARD_SMART_16;
    adapter->adapter_ram_size      = 128;
    adapter->edge_triggered_ints   = TRUE;
    adapter->EaglePsDMA            = TRUE;
    adapter->max_frame_size        = MAX_FRAME_SIZE_16_MBITS;
    adapter->ring_speed            = 16;

    /*
     * Bring adapter out of reset state (ensure that SCS is zero before     
     * doing this).                                                         
     */

    sys_outsb(adapter_handle, control_1, 1);

    /*
     * Halt the Eagle prior to downloading the MAC code - this will also    
     * write the interrupt bits into the SIFACL register, where the MAC can 
     * find them.                                                           
     */

    hwi_halt_eagle(adapter);

    /*
     * Download code to adapter.                                             
     * View download image as a sequence of download records. Pass address
     * of routine to set up DIO addresses on ATULA cards.       
     * If routine fails return failure (error record already filled in).
     */

    if (!hwi_download_code(adapter,
			   (DOWNLOAD_RECORD *) download_image,
			   hwi_smart16_set_dio_address))
    {
#ifndef FTK_NO_IO_ENABLE
        macro_disable_io(adapter);
#endif
        return FALSE;
    }

    /*
     * Restart the Eagle to initiate bring up diagnostics.                  
     */

    hwi_start_eagle(adapter);

    /*
     * Wait for a valid bring up code, may wait 3 seconds.
     * If routine fails return failure (error record already filled in).
     */

    if (!hwi_get_bring_up_code(adapter))
    {
#ifndef FTK_NO_IO_ENABLE
        macro_disable_io(adapter);
#endif
        return FALSE;
    }

    /*
     * Set DIO address to point to EAGLE DATA page 0x10000L.
     */

    hwi_smart16_set_dio_address(adapter, DIO_LOCATION_EAGLE_DATA_PAGE);

    /*
     * If not in polling mode then set up interrupts.
     * Interrupts_on field is used when disabling interrupts for adapter.
     */

    if (adapter->interrupt_number != POLLING_INTERRUPTS_MODE)
    {
	adapter->interrupts_on =
	    sys_enable_irq_channel(adapter_handle, adapter->interrupt_number);

        /*
	 * If fail enable irq channel then fill in error record and return.
         */

	if (!adapter->interrupts_on)
	{
	    adapter->error_record.type = ERROR_TYPE_HWI;
	    adapter->error_record.value = HWI_E_0B_FAIL_IRQ_ENABLE;
#ifndef FTK_NO_IO_ENABLE
            macro_disable_io(adapter);
#endif
            return FALSE;
	}
    }
    else
    {
	adapter->interrupts_on = TRUE;
    }

    /* return successfully                                                  */

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif
    return TRUE;

}


/****************************************************************************
*                                                                         
*                       hwi_smart16_interrupt_handler                    
*                       =============================                       
*                                                                           
*                                                                           
* PARAMETERS (passed by hwi_interrupt_entry) :                             
* ============================================                             
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_smart16_interrupt_handler routine  is  called, when an interrupt 
* occurs, by hwi_interrupt_entry.  It checks to see if a  particular  card 
* has interrupted. The interrupt could be generated by  the SIF for either 
* a PIO data transfer or a normal condition (received frame, SRB complete, 
* ARB indication etc). Note it could in fact be the case that no interrupt 
* has  occured  on the particular adapter being checked.                   
*                                                                          
* On normal SIF interrupts, the interrupt is acknowledged and cleared. The 
* value in the SIF interrupt register is recorded in order to pass  it  to 
* the driver_interrupt_entry routine (along with the adapter details).     
*                                                                          
* On PseudoDMA interrupts, the length, direction and physical  address  of 
* the  transfer  is  determined. A system provided routine is called to do 
* the data transfer itself.                                                
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                          
****************************************************************************/

#ifdef FTK_IRQ_FUNCTION
#pragma FTK_IRQ_FUNCTION(hwi_smart16_interrupt_handler)
#endif

export void
hwi_smart16_interrupt_handler(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE adapter_handle  = adapter->adapter_handle;
    WORD           sifacl_value;
    WORD           sifint_value;
    WORD           sifint_tmp;
    WBOOLEAN       sifint_occurred = FALSE;
    WBOOLEAN       pioint_occurred = FALSE;
    WORD           pio_len_bytes;
    WBOOLEAN       pio_from_adapter;
    BYTE     FAR * pio_address;
    WORD           lo_word;
    DWORD          hi_word;

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

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    /*
     * Check for SIF interrupt or PIO interrupt.
     */

    /*
     * Mask off any further interrupts while we read SIFINT (note that this 
     * does not mask off Pseudo DMA interrupts).
     */

    macro_clearw_bit(
        adapter_handle,
        adapter->sif_acl,
        EAGLE_SIFACL_SINTEN
        );

    sifint_value = sys_insw(adapter_handle, adapter->sif_int);
    do
    {
        sifint_tmp = sifint_value;
        sifint_value = sys_insw(
                           adapter_handle,
                           adapter->sif_int
                           );
    }
    while (sifint_tmp != sifint_value);

    if ((sifint_value & EAGLE_SIFINT_SYSTEM) != 0)
    {
        /*
	 * SIF interrupt has occurred.
	 * SRB free, adapter check or received frame interrupt.
         */

        sifint_occurred = TRUE;

        /*
	 * Clear EAGLE_SIFINT_HOST_IRQ to acknowledge interrupt at SIF.
         */

	sys_outsw( adapter_handle, adapter->sif_int, 0);

    }

    sifacl_value = sys_insw(adapter_handle, adapter->sif_acl);

    if ((sifacl_value & EAGLE_SIFACL_SWHRQ) != 0)
	{
        /*
	 * PIO interrupt has occurred.
	 * Data transfer to/from adapter interrupt.
         */

        pioint_occurred = TRUE;

        macro_setw_bit(
            adapter_handle,
            adapter->sif_acl,
            EAGLE_SIFACL_SWHLDA
            );

	/*
         * Determine what direction the data transfer is to take place in.
         */

	pio_from_adapter = sys_insw(
                               adapter_handle,
                               adapter->sif_acl
                               ) & EAGLE_SIFACL_SWDDIR;

        pio_len_bytes = sys_insw(
                            adapter_handle,
                            adapter->sif_dmalen
                            );

        lo_word = sys_insw(
                      adapter_handle,
                      adapter->sif_sdmaadr 
                      );

        hi_word = (DWORD) sys_insw(
                              adapter_handle,
                              adapter->sif_sdmaadx
                              );

        pio_address = (BYTE FAR *) ((hi_word << 16) | ((DWORD) lo_word));

        /*
	 * Do the actual data transfer.
	 * Note that Fastmac only copies whole UINTs to DWORD boundaries.
         * FastmacPlus, however, can transfer any length to any address.    
         */

	if (pio_from_adapter)
	    {
            /*
	     * Transfer into host memory from adapter.
	     * First, check if host address is on an odd byte boundary.     
             */

	    if ((card_t)pio_address % 2)
	    {
		pio_len_bytes--;
		*(pio_address++) =
			sys_insb(adapter_handle,
                            (WORD) (adapter->sif_sdmadat + 1));
	    }

            /*
	     * Now transfer the bulk of the data.                           
             */

	    sys_rep_insw(
                adapter_handle,
                adapter->sif_sdmadat,
                pio_address,
                (WORD) (pio_len_bytes >> 1));

            /*
	     * Finally transfer any trailing byte.                          
             */

	    if (pio_len_bytes % 2)
            {
		*(pio_address+pio_len_bytes-1) =
			    sys_insb(adapter_handle,
				adapter->sif_sdmadat);
            }
	}
	else
	{
            /*
	     * Transfer into adapter memory from the host.
             */

            if ((card_t)pio_address % 2)
	    {
		pio_len_bytes--;
		sys_outsb(
                    adapter_handle,
                    (WORD) (adapter->sif_sdmadat + 1),
                    *(pio_address++)
                    );
	    }

	    sys_rep_outsw(
                adapter_handle,
                adapter->sif_sdmadat,
                pio_address,
                (WORD) (pio_len_bytes >> 1)
                );

	    if (pio_len_bytes % 2)
            {
		sys_outsb(
                    adapter_handle,
                    adapter->sif_sdmadat,
                    *(pio_address+pio_len_bytes-1)
                    );
            }
	}
    }

#ifndef FTK_NO_CLEAR_IRQ

    if (sifint_occurred || pioint_occurred)
    {
        /*
	 * Acknowledge/clear interrupt at interrupt controller.
         */
	sys_clear_controller_interrupt(
             adapter_handle,
             adapter->interrupt_number);
    }

#endif

    if (sifint_occurred)
    {
        /*
	 * Call driver with details of SIF interrupt.
         */

	driver_interrupt_entry(
            adapter_handle,
            adapter,
            sifint_value);
    }

    /*
     * Read SIFACL until the SWHLDA bit has cleared.
     */

    do
    {
        sifacl_value = sys_insw(adapter_handle, adapter->sif_acl);
    }
    while ((sifacl_value & EAGLE_SIFACL_SWHLDA) != 0);

    /*
     * Now set SINTEN in SIFACL to regenerate interrupts.
     */

    sys_outsw(
        adapter_handle, 
        adapter->sif_acl, 
        (WORD) (sifacl_value | EAGLE_SIFACL_SINTEN)
        );

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

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io( adapter);
#endif
}


/****************************************************************************
*                                                                          
*                      hwi_smart16_remove_card                             
*                      =======================                             
*                                                                          
*                                                                          
* PARAMETERS (passed by hwi_remove_adapter) :                              
* ===========================================                              
*                                                                          
* ADAPTER * adapter                                                        
*                                                                          
* This structure is used to identify and record specific information about 
* the required adapter.                                                    
*                                                                          
*                                                                          
* BODY :                                                                   
* ======                                                                   
*                                                                          
* The hwi_smart16_remove_card routine is called by hwi_remove_adapter. It  
* disables interrupts if they are being used. It also resets the adapter.  
*                                                                          
*                                                                          
* RETURNS :                                                                
* =========                                                                
*                                                                          
* The routine always successfully completes.                               
*                                                                          
****************************************************************************/

#ifdef FTK_RES_FUNCTION
#pragma FTK_RES_FUNCTION(hwi_smart16_remove_card)
#endif
                                                                       
export void
hwi_smart16_remove_card(
    ADAPTER *      adapter
    )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    WORD           control_1      = adapter->io_location +
                                        SMART16_CONTROL_REGISTER_1;
    WORD           sifacl_value;

    /*
     * Interrupt must be disabled at adapter before unpatching interrupt.
     * Even in polling mode we must turn off interrupts at adapter.
     */

#ifndef FTK_NO_IO_ENABLE
    macro_enable_io(adapter);
#endif

    sifacl_value = sys_insw(adapter_handle, adapter->sif_acl);
    sifacl_value = (sifacl_value & ~(EAGLE_SIFACL_PSDMAEN | EAGLE_SIFACL_SINTEN));
    sys_outsw(
        adapter_handle,
        adapter->sif_acl,
        sifacl_value);

    if (adapter->interrupts_on)
    {
	if (adapter->interrupt_number != POLLING_INTERRUPTS_MODE)
	{
	    sys_disable_irq_channel(
                adapter_handle,
                adapter->interrupt_number);
	}

	adapter->interrupts_on = FALSE;
    }

    /*
     * perform adapter reset, set BALD_EAGLE_CTRL1_NSRESET low              
     */

    sys_outsb(adapter_handle, control_1, 0);

#ifndef FTK_NO_IO_ENABLE
    macro_disable_io(adapter);
#endif
}


/****************************************************************************
*                                                                          
*                      hwi_smart16_set_dio_address                         
*                      ===========================                         
*                                                                          
* The hwi_smart16_set_dio_address routine is used, with Smart16 cards, for 
* putting  a  32 bit DIO address into the SIF DIO address and extended DIO 
* address registers. Note that the extended  address  register  should  be 
* loaded first.                                                            
*                                                                          
****************************************************************************/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_smart16_set_dio_address)
#endif

export void
hwi_smart16_set_dio_address(
    ADAPTER *      adapter,
    DWORD          dio_address
    )
{
    ADAPTER_HANDLE adapter_handle = adapter->adapter_handle;
    WORD           sif_dio_adr    = adapter->sif_adr; 
    WORD           sif_dio_adrx   = adapter->sif_adx;

    /*
     * Load extended DIO address register with top 16 bits of address.
     * Always load extended address register first.
     */
    sys_outsw(
        adapter_handle,
        sif_dio_adrx,
        (WORD)(dio_address >> 16));

    /*
     * Load DIO address register with low 16 bits of address.
     */

    sys_outsw(
        adapter_handle,
        sif_dio_adr,
        (WORD)(dio_address & 0x0000FFFF));

}


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

#ifndef FTK_NO_PROBE
/*---------------------------------------------------------------------------
|                                                                          
|                       hwi_smart16_check_for_card                         
|                       ==========================                         
|                                                                          
| The hwi_smart16_check_for_card routine reads in the node address from    
| the BIA, and checks that it is a valid Madge node address. Basically     
| it's just the same as hwi_smart16_read_node_address.                     
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_smart16_check_for_card)
#endif

local WBOOLEAN
hwi_smart16_check_for_card(
    WORD  io_location
    )
{
    WORD  control_2    = io_location + SMART16_CONTROL_REGISTER_2;
    WORD  port;
    BYTE  i;
    BYTE  j;
    BYTE  two_bits;
    DWORD node_address = 0;

    for (i = 0; i < 4; i++)
    {
        sys_probe_outsb(control_2, i);

        /*
         * Read the 8 bit node address 2 bits at a time.
         */

        port = io_location;

        for (j = 0; j < 4; j++)
        {
            two_bits = (BYTE)(sys_probe_insb(port) & 3);
            node_address = (node_address << 2) | two_bits;
            port += 8;
        }
    }

    /* 
     * If we find that the high byte is not f6 then we know we haven't
     * got a valid card so we fail.
     */

    return  (((node_address >> 24) & 0x000000ffL) == 0x000000f6L &&
              (node_address & 0x00ffffffL)        != 0x00ffffffL &&
              (node_address & 0x00ffffffL)        != 0x00000000L);
}
#endif

/*---------------------------------------------------------------------------
|                                                                          
|                      hwi_smart16_read_node_address                       
|                      =============================                       
|                                                                          
| The hwi_smart16_read_node_address routine reads in the node address from 
| the BIA, and checks that it is a valid Madge node address.               
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_smart16_read_node_address)
#endif

local WBOOLEAN
hwi_smart16_read_node_address(
    ADAPTER *  adapter
    )
{
    WORD       control_2    = adapter->io_location +
                                  SMART16_CONTROL_REGISTER_2;
    WORD       port;
    BYTE       i;
    BYTE       j;
    BYTE       two_bits;
    DWORD      node_address = 0;

    for (i = 0; i < 4; i++)
    {
        sys_outsb(
            adapter->adapter_handle,
            control_2,
            i);

        /*
         * Read the 8 bit node address 2 bits at a time.
         */

        port = adapter->io_location;

        for (j = 0; j < 4; j++)
        {
            two_bits = (BYTE)(sys_insb(adapter->adapter_handle, port) & 3);
            node_address = (node_address << 2) | two_bits;
            port += 8;
        }
    }

    adapter->permanent_address.byte[0] = 0;
    adapter->permanent_address.byte[1] = 0;

    for (i = 0; i < 4; i++)
    {
        adapter->permanent_address.byte[5-i]
            = (BYTE)((node_address >> (8 * i)) & 0x0ff);
    }

    return (adapter->permanent_address.byte[2] == MADGE_NODE_BYTE_2);
}


/*---------------------------------------------------------------------------
|                                                                          
|                     hwi_smart16_valid_io_location                        
|                     =============================                        
|                                                                          
| The hwi_smart16_valid_io_location routine checks to see if the user has  
| supplied a valid IO location for a smart 16 adapter card.        
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_smart16_valid_io_location)
#endif

local WBOOLEAN
hwi_smart16_valid_io_location(
    WORD     io_location
    )
{
    WBOOLEAN io_valid;

    switch (io_location & ~SMART16_REV3)
    {

	case 0x4A20     :
	case 0x4E20     :
	case 0x6A20     :
	case 0x6E20     :

            /*
	     * These are the valid user supplied io locations.
             */

	    io_valid = TRUE;
	    break;


	default         :

            /*
	     * Anything else is invalid.                                    
             */

	    io_valid = FALSE;

	    break;
    }

    return(io_valid);
}


/*---------------------------------------------------------------------------
|                                                                          
|                    hwi_smart16_valid_transfer_mode                        
|                    ===============================
|                                                                          
| The hwi_smart16_valid_transfer_mode routine checks to see if the user has  
| supplied a valid transfer mode for a smart 16 adapter card (that's PIO to
| you and me).       
|                                                                          
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_smart16_valid_transfer_mode)
#endif

local WBOOLEAN
hwi_smart16_valid_transfer_mode(
    UINT transfer_mode
    )
{
    return(transfer_mode == PIO_DATA_TRANSFER_MODE);
}


/*---------------------------------------------------------------------------
|
|                      hwi_smart16_valid_irq_channel
|                      =============================
|
| The hwi_smart16_valid_irq_channel routine checks to see if  the  user  has
| supplied a valid interrupt number for a Smart16 adapter card.
|
---------------------------------------------------------------------------*/

#ifdef FTK_INIT_FUNCTION
#pragma FTK_INIT_FUNCTION(hwi_smart16_valid_irq_channel)
#endif

local WBOOLEAN 
hwi_smart16_valid_irq_channel(
    ADAPTER * adapter
    )
{
    WBOOLEAN int_valid;

    /*
     * Assume that interrupt number is valid.
     */

    int_valid = TRUE;

    /*
     * No need to do any check on interrupt number if in polling mode.
     */

    if (adapter->interrupt_number != POLLING_INTERRUPTS_MODE)
    {
	switch (adapter->interrupt_number)
	{
	    case 2  :
	    case 3  :
	    case 7  :
                    break;

	    default :
	        int_valid = FALSE;
	        break;

        }
    }

    if (!int_valid)
    {
	adapter->error_record.type  = ERROR_TYPE_HWI;
	adapter->error_record.value = HWI_E_03_BAD_INTERRUPT_NUMBER;
    }

    return int_valid;
}

#endif

/******** End of HWI_SM16.C file *******************************************/