aboutsummaryrefslogblamecommitdiffstats
path: root/drivers/net/tokenring/smctr.c
blob: 23e012f5bbf42bc4509a30a22c0811a7432a9808 (plain) (tree)
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
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
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
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641


























                                                                             
                                                                       


                         











                            






                              
                           










                                                      
 

                                                                 












                                           
                                     


























                                                              

                                                            

















                                                                       
















                                                                     
                                                          




































































































































































































































































































































































































                                                                                              
                                                                                   
























































































































































































































                                                                          

                                                           












                                                                            



                                                               





































































































































































































































































































                                                                                       
                                                                                 





















































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                       
                                                         








                                                                     

                                






























































































































































































































































































































                                                                                                                                                                                                                      
                                                                    











































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                   
                                  





                                                                          




                                                                       





                                                                  



                                         

                                                                          

                                                                        
         

                                          


                                                     





                                                                          






                                                                               
                                                      







                                                             
                                               
 
                                                                                                                                               












                                                                     

                             


























                                                                          
                                                                   

























                                                                            
                         






                                                   
                                         



                                                                  
                                                 






                                       
                                 




                                                                  
                                 





                                                 




                                      























































































































































































































































































                                                                                
                                                                          








































































































































































                                                                                                                               



































                                                                               







                                                           

















































                                                                                   
                                            
                                     













                                                               

























































































                                                                                                      


                                                                    


























                                                                                
                         












































































                                                                                
                                                            





                                                 

                                                        

































































































































































































































































































































































































































































































                                                                                                                    



                                                                             



                                                                              



                                                                             



                                                                               



                                                                             



                                                                             



                                                                        



                                                                        



                                                                         















































                                                                               
                                                                                     





                                                                         

                                                                                
















































































































                                                                            
                                                                                  






                                               
                            




















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                       
                                




                                      
                                                   




























                                                                       
 
                            














                                                             
                                























                                                                        
/*
 *  smctr.c: A network driver for the SMC Token Ring Adapters.
 *
 *  Written by Jay Schulist <jschlst@samba.org>
 *
 *  This software may be used and distributed according to the terms
 *  of the GNU General Public License, incorporated herein by reference.
 *
 *  This device driver works with the following SMC adapters:
 *      - SMC TokenCard Elite   (8115T, chips 825/584)
 *      - SMC TokenCard Elite/A MCA (8115T/A, chips 825/594)
 *
 *  Source(s):
 *  	- SMC TokenCard SDK.
 *
 *  Maintainer(s):
 *    JS        Jay Schulist <jschlst@samba.org>
 *
 * Changes:
 *    07102000          JS      Fixed a timing problem in smctr_wait_cmd();
 *                              Also added a bit more discriptive error msgs.
 *    07122000          JS      Fixed problem with detecting a card with
 *				module io/irq/mem specified.
 *
 *  To do:
 *    1. Multicast support.
 *
 *  Initial 2.5 cleanup Alan Cox <alan@lxorguk.ukuu.org.uk>  2002/10/28
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/fcntl.h>
#include <linux/interrupt.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/in.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/time.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/mca-legacy.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/trdevice.h>
#include <linux/bitops.h>
#include <linux/firmware.h>

#include <asm/system.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/irq.h>

#if BITS_PER_LONG == 64
#error FIXME: driver does not support 64-bit platforms
#endif

#include "smctr.h"               /* Our Stuff */

static const char version[] __initdata =
	KERN_INFO "smctr.c: v1.4 7/12/00 by jschlst@samba.org\n";
static const char cardname[] = "smctr";


#define SMCTR_IO_EXTENT   20

#ifdef CONFIG_MCA_LEGACY
static unsigned int smctr_posid = 0x6ec6;
#endif

static int ringspeed;

/* SMC Name of the Adapter. */
static char smctr_name[] = "SMC TokenCard";
static char *smctr_model = "Unknown";

/* Use 0 for production, 1 for verification, 2 for debug, and
 * 3 for very verbose debug.
 */
#ifndef SMCTR_DEBUG
#define SMCTR_DEBUG 1
#endif
static unsigned int smctr_debug = SMCTR_DEBUG;

/* smctr.c prototypes and functions are arranged alphabeticly 
 * for clearity, maintainability and pure old fashion fun. 
 */
/* A */
static int smctr_alloc_shared_memory(struct net_device *dev);

/* B */
static int smctr_bypass_state(struct net_device *dev);

/* C */
static int smctr_checksum_firmware(struct net_device *dev);
static int __init smctr_chk_isa(struct net_device *dev);
static int smctr_chg_rx_mask(struct net_device *dev);
static int smctr_clear_int(struct net_device *dev);
static int smctr_clear_trc_reset(int ioaddr);
static int smctr_close(struct net_device *dev);

/* D */
static int smctr_decode_firmware(struct net_device *dev,
				 const struct firmware *fw);
static int smctr_disable_16bit(struct net_device *dev);
static int smctr_disable_adapter_ctrl_store(struct net_device *dev);
static int smctr_disable_bic_int(struct net_device *dev);

/* E */
static int smctr_enable_16bit(struct net_device *dev);
static int smctr_enable_adapter_ctrl_store(struct net_device *dev);
static int smctr_enable_adapter_ram(struct net_device *dev);
static int smctr_enable_bic_int(struct net_device *dev);

/* G */
static int __init smctr_get_boardid(struct net_device *dev, int mca);
static int smctr_get_group_address(struct net_device *dev);
static int smctr_get_functional_address(struct net_device *dev);
static unsigned int smctr_get_num_rx_bdbs(struct net_device *dev);
static int smctr_get_physical_drop_number(struct net_device *dev);
static __u8 *smctr_get_rx_pointer(struct net_device *dev, short queue);
static int smctr_get_station_id(struct net_device *dev);
static FCBlock *smctr_get_tx_fcb(struct net_device *dev, __u16 queue,
        __u16 bytes_count);
static int smctr_get_upstream_neighbor_addr(struct net_device *dev);

/* H */
static int smctr_hardware_send_packet(struct net_device *dev,
        struct net_local *tp);
/* I */
static int smctr_init_acbs(struct net_device *dev);
static int smctr_init_adapter(struct net_device *dev);
static int smctr_init_card_real(struct net_device *dev);
static int smctr_init_rx_bdbs(struct net_device *dev);
static int smctr_init_rx_fcbs(struct net_device *dev);
static int smctr_init_shared_memory(struct net_device *dev);
static int smctr_init_tx_bdbs(struct net_device *dev);
static int smctr_init_tx_fcbs(struct net_device *dev);
static int smctr_internal_self_test(struct net_device *dev);
static irqreturn_t smctr_interrupt(int irq, void *dev_id);
static int smctr_issue_enable_int_cmd(struct net_device *dev,
        __u16 interrupt_enable_mask);
static int smctr_issue_int_ack(struct net_device *dev, __u16 iack_code,
        __u16 ibits);
static int smctr_issue_init_timers_cmd(struct net_device *dev);
static int smctr_issue_init_txrx_cmd(struct net_device *dev);
static int smctr_issue_insert_cmd(struct net_device *dev);
static int smctr_issue_read_ring_status_cmd(struct net_device *dev);
static int smctr_issue_read_word_cmd(struct net_device *dev, __u16 aword_cnt);
static int smctr_issue_remove_cmd(struct net_device *dev);
static int smctr_issue_resume_acb_cmd(struct net_device *dev);
static int smctr_issue_resume_rx_bdb_cmd(struct net_device *dev, __u16 queue);
static int smctr_issue_resume_rx_fcb_cmd(struct net_device *dev, __u16 queue);
static int smctr_issue_resume_tx_fcb_cmd(struct net_device *dev, __u16 queue);
static int smctr_issue_test_internal_rom_cmd(struct net_device *dev);
static int smctr_issue_test_hic_cmd(struct net_device *dev);
static int smctr_issue_test_mac_reg_cmd(struct net_device *dev);
static int smctr_issue_trc_loopback_cmd(struct net_device *dev);
static int smctr_issue_tri_loopback_cmd(struct net_device *dev);
static int smctr_issue_write_byte_cmd(struct net_device *dev,
        short aword_cnt, void *byte);
static int smctr_issue_write_word_cmd(struct net_device *dev,
        short aword_cnt, void *word);

/* J */
static int smctr_join_complete_state(struct net_device *dev);

/* L */
static int smctr_link_tx_fcbs_to_bdbs(struct net_device *dev);
static int smctr_load_firmware(struct net_device *dev);
static int smctr_load_node_addr(struct net_device *dev);
static int smctr_lobe_media_test(struct net_device *dev);
static int smctr_lobe_media_test_cmd(struct net_device *dev);
static int smctr_lobe_media_test_state(struct net_device *dev);

/* M */
static int smctr_make_8025_hdr(struct net_device *dev,
        MAC_HEADER *rmf, MAC_HEADER *tmf, __u16 ac_fc);
static int smctr_make_access_pri(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);
static int smctr_make_addr_mod(struct net_device *dev, MAC_SUB_VECTOR *tsv);
static int smctr_make_auth_funct_class(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);
static int smctr_make_corr(struct net_device *dev,
        MAC_SUB_VECTOR *tsv, __u16 correlator);
static int smctr_make_funct_addr(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);
static int smctr_make_group_addr(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);
static int smctr_make_phy_drop_num(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);
static int smctr_make_product_id(struct net_device *dev, MAC_SUB_VECTOR *tsv);
static int smctr_make_station_id(struct net_device *dev, MAC_SUB_VECTOR *tsv);
static int smctr_make_ring_station_status(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);
static int smctr_make_ring_station_version(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);
static int smctr_make_tx_status_code(struct net_device *dev,
        MAC_SUB_VECTOR *tsv, __u16 tx_fstatus);
static int smctr_make_upstream_neighbor_addr(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);
static int smctr_make_wrap_data(struct net_device *dev,
        MAC_SUB_VECTOR *tsv);

/* O */
static int smctr_open(struct net_device *dev);
static int smctr_open_tr(struct net_device *dev);

/* P */
struct net_device *smctr_probe(int unit);
static int __init smctr_probe1(struct net_device *dev, int ioaddr);
static int smctr_process_rx_packet(MAC_HEADER *rmf, __u16 size,
        struct net_device *dev, __u16 rx_status);

/* R */
static int smctr_ram_memory_test(struct net_device *dev);
static int smctr_rcv_chg_param(struct net_device *dev, MAC_HEADER *rmf,
        __u16 *correlator);
static int smctr_rcv_init(struct net_device *dev, MAC_HEADER *rmf,
        __u16 *correlator);
static int smctr_rcv_tx_forward(struct net_device *dev, MAC_HEADER *rmf);
static int smctr_rcv_rq_addr_state_attch(struct net_device *dev,
        MAC_HEADER *rmf, __u16 *correlator);
static int smctr_rcv_unknown(struct net_device *dev, MAC_HEADER *rmf,
        __u16 *correlator);
static int smctr_reset_adapter(struct net_device *dev);
static int smctr_restart_tx_chain(struct net_device *dev, short queue);
static int smctr_ring_status_chg(struct net_device *dev);
static int smctr_rx_frame(struct net_device *dev);

/* S */
static int smctr_send_dat(struct net_device *dev);
static int smctr_send_packet(struct sk_buff *skb, struct net_device *dev);
static int smctr_send_lobe_media_test(struct net_device *dev);
static int smctr_send_rpt_addr(struct net_device *dev, MAC_HEADER *rmf,
        __u16 correlator);
static int smctr_send_rpt_attch(struct net_device *dev, MAC_HEADER *rmf,
        __u16 correlator);
static int smctr_send_rpt_state(struct net_device *dev, MAC_HEADER *rmf,
        __u16 correlator);
static int smctr_send_rpt_tx_forward(struct net_device *dev,
        MAC_HEADER *rmf, __u16 tx_fstatus);
static int smctr_send_rsp(struct net_device *dev, MAC_HEADER *rmf,
        __u16 rcode, __u16 correlator);
static int smctr_send_rq_init(struct net_device *dev);
static int smctr_send_tx_forward(struct net_device *dev, MAC_HEADER *rmf,
        __u16 *tx_fstatus);
static int smctr_set_auth_access_pri(struct net_device *dev,
        MAC_SUB_VECTOR *rsv);
static int smctr_set_auth_funct_class(struct net_device *dev,
        MAC_SUB_VECTOR *rsv);
static int smctr_set_corr(struct net_device *dev, MAC_SUB_VECTOR *rsv,
	__u16 *correlator);
static int smctr_set_error_timer_value(struct net_device *dev,
        MAC_SUB_VECTOR *rsv);
static int smctr_set_frame_forward(struct net_device *dev,
        MAC_SUB_VECTOR *rsv, __u8 dc_sc);
static int smctr_set_local_ring_num(struct net_device *dev,
        MAC_SUB_VECTOR *rsv);
static unsigned short smctr_set_ctrl_attention(struct net_device *dev);
static void smctr_set_multicast_list(struct net_device *dev);
static int smctr_set_page(struct net_device *dev, __u8 *buf);
static int smctr_set_phy_drop(struct net_device *dev,
        MAC_SUB_VECTOR *rsv);
static int smctr_set_ring_speed(struct net_device *dev);
static int smctr_set_rx_look_ahead(struct net_device *dev);
static int smctr_set_trc_reset(int ioaddr);
static int smctr_setup_single_cmd(struct net_device *dev,
        __u16 command, __u16 subcommand);
static int smctr_setup_single_cmd_w_data(struct net_device *dev,
        __u16 command, __u16 subcommand);
static char *smctr_malloc(struct net_device *dev, __u16 size);
static int smctr_status_chg(struct net_device *dev);

/* T */
static void smctr_timeout(struct net_device *dev);
static int smctr_trc_send_packet(struct net_device *dev, FCBlock *fcb,
        __u16 queue);
static __u16 smctr_tx_complete(struct net_device *dev, __u16 queue);
static unsigned short smctr_tx_move_frame(struct net_device *dev,
        struct sk_buff *skb, __u8 *pbuff, unsigned int bytes);

/* U */
static int smctr_update_err_stats(struct net_device *dev);
static int smctr_update_rx_chain(struct net_device *dev, __u16 queue);
static int smctr_update_tx_chain(struct net_device *dev, FCBlock *fcb,
        __u16 queue);

/* W */
static int smctr_wait_cmd(struct net_device *dev);
static int smctr_wait_while_cbusy(struct net_device *dev);

#define TO_256_BYTE_BOUNDRY(X)  (((X + 0xff) & 0xff00) - X)
#define TO_PARAGRAPH_BOUNDRY(X) (((X + 0x0f) & 0xfff0) - X)
#define PARAGRAPH_BOUNDRY(X)    smctr_malloc(dev, TO_PARAGRAPH_BOUNDRY(X))

/* Allocate Adapter Shared Memory.
 * IMPORTANT NOTE: Any changes to this function MUST be mirrored in the
 * function "get_num_rx_bdbs" below!!!
 *
 * Order of memory allocation:
 *
 *       0. Initial System Configuration Block Pointer
 *       1. System Configuration Block
 *       2. System Control Block
 *       3. Action Command Block
 *       4. Interrupt Status Block
 *
 *       5. MAC TX FCB'S
 *       6. NON-MAC TX FCB'S
 *       7. MAC TX BDB'S
 *       8. NON-MAC TX BDB'S
 *       9. MAC RX FCB'S
 *      10. NON-MAC RX FCB'S
 *      11. MAC RX BDB'S
 *      12. NON-MAC RX BDB'S
 *      13. MAC TX Data Buffer( 1, 256 byte buffer)
 *      14. MAC RX Data Buffer( 1, 256 byte buffer)
 *
 *      15. NON-MAC TX Data Buffer
 *      16. NON-MAC RX Data Buffer
 */
static int smctr_alloc_shared_memory(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_alloc_shared_memory\n", dev->name);

        /* Allocate initial System Control Block pointer.
         * This pointer is located in the last page, last offset - 4.
         */
        tp->iscpb_ptr = (ISCPBlock *)(tp->ram_access + ((__u32)64 * 0x400)
                - (long)ISCP_BLOCK_SIZE);

        /* Allocate System Control Blocks. */
        tp->scgb_ptr = (SCGBlock *)smctr_malloc(dev, sizeof(SCGBlock));
        PARAGRAPH_BOUNDRY(tp->sh_mem_used);

        tp->sclb_ptr = (SCLBlock *)smctr_malloc(dev, sizeof(SCLBlock));
        PARAGRAPH_BOUNDRY(tp->sh_mem_used);

        tp->acb_head = (ACBlock *)smctr_malloc(dev,
                sizeof(ACBlock)*tp->num_acbs);
        PARAGRAPH_BOUNDRY(tp->sh_mem_used);

        tp->isb_ptr = (ISBlock *)smctr_malloc(dev, sizeof(ISBlock));
        PARAGRAPH_BOUNDRY(tp->sh_mem_used);

        tp->misc_command_data = (__u16 *)smctr_malloc(dev, MISC_DATA_SIZE);
        PARAGRAPH_BOUNDRY(tp->sh_mem_used);

        /* Allocate transmit FCBs. */
        tp->tx_fcb_head[MAC_QUEUE] = (FCBlock *)smctr_malloc(dev,
                sizeof(FCBlock) * tp->num_tx_fcbs[MAC_QUEUE]);

        tp->tx_fcb_head[NON_MAC_QUEUE] = (FCBlock *)smctr_malloc(dev,
                sizeof(FCBlock) * tp->num_tx_fcbs[NON_MAC_QUEUE]);

        tp->tx_fcb_head[BUG_QUEUE] = (FCBlock *)smctr_malloc(dev,
                sizeof(FCBlock) * tp->num_tx_fcbs[BUG_QUEUE]);

        /* Allocate transmit BDBs. */
        tp->tx_bdb_head[MAC_QUEUE] = (BDBlock *)smctr_malloc(dev,
                sizeof(BDBlock) * tp->num_tx_bdbs[MAC_QUEUE]);

        tp->tx_bdb_head[NON_MAC_QUEUE] = (BDBlock *)smctr_malloc(dev,
                sizeof(BDBlock) * tp->num_tx_bdbs[NON_MAC_QUEUE]);

        tp->tx_bdb_head[BUG_QUEUE] = (BDBlock *)smctr_malloc(dev,
                sizeof(BDBlock) * tp->num_tx_bdbs[BUG_QUEUE]);

        /* Allocate receive FCBs. */
        tp->rx_fcb_head[MAC_QUEUE] = (FCBlock *)smctr_malloc(dev,
                sizeof(FCBlock) * tp->num_rx_fcbs[MAC_QUEUE]);

        tp->rx_fcb_head[NON_MAC_QUEUE] = (FCBlock *)smctr_malloc(dev,
                sizeof(FCBlock) * tp->num_rx_fcbs[NON_MAC_QUEUE]);

        /* Allocate receive BDBs. */
        tp->rx_bdb_head[MAC_QUEUE] = (BDBlock *)smctr_malloc(dev,
                sizeof(BDBlock) * tp->num_rx_bdbs[MAC_QUEUE]);

        tp->rx_bdb_end[MAC_QUEUE] = (BDBlock *)smctr_malloc(dev, 0);

        tp->rx_bdb_head[NON_MAC_QUEUE] = (BDBlock *)smctr_malloc(dev,
                sizeof(BDBlock) * tp->num_rx_bdbs[NON_MAC_QUEUE]);

        tp->rx_bdb_end[NON_MAC_QUEUE] = (BDBlock *)smctr_malloc(dev, 0);

        /* Allocate MAC transmit buffers.
         * MAC Tx Buffers doen't have to be on an ODD Boundry.
         */
        tp->tx_buff_head[MAC_QUEUE]
                = (__u16 *)smctr_malloc(dev, tp->tx_buff_size[MAC_QUEUE]);
        tp->tx_buff_curr[MAC_QUEUE] = tp->tx_buff_head[MAC_QUEUE];
        tp->tx_buff_end [MAC_QUEUE] = (__u16 *)smctr_malloc(dev, 0);

        /* Allocate BUG transmit buffers. */
        tp->tx_buff_head[BUG_QUEUE]
                = (__u16 *)smctr_malloc(dev, tp->tx_buff_size[BUG_QUEUE]);
        tp->tx_buff_curr[BUG_QUEUE] = tp->tx_buff_head[BUG_QUEUE];
        tp->tx_buff_end[BUG_QUEUE] = (__u16 *)smctr_malloc(dev, 0);

        /* Allocate MAC receive data buffers.
         * MAC Rx buffer doesn't have to be on a 256 byte boundary.
         */
        tp->rx_buff_head[MAC_QUEUE] = (__u16 *)smctr_malloc(dev,
                RX_DATA_BUFFER_SIZE * tp->num_rx_bdbs[MAC_QUEUE]);
        tp->rx_buff_end[MAC_QUEUE] = (__u16 *)smctr_malloc(dev, 0);

        /* Allocate Non-MAC transmit buffers.
         * ?? For maximum Netware performance, put Tx Buffers on
         * ODD Boundry and then restore malloc to Even Boundrys.
         */
        smctr_malloc(dev, 1L);
        tp->tx_buff_head[NON_MAC_QUEUE]
                = (__u16 *)smctr_malloc(dev, tp->tx_buff_size[NON_MAC_QUEUE]);
        tp->tx_buff_curr[NON_MAC_QUEUE] = tp->tx_buff_head[NON_MAC_QUEUE];
        tp->tx_buff_end [NON_MAC_QUEUE] = (__u16 *)smctr_malloc(dev, 0);
        smctr_malloc(dev, 1L);

        /* Allocate Non-MAC receive data buffers.
         * To guarantee a minimum of 256 contigous memory to
         * UM_Receive_Packet's lookahead pointer, before a page
         * change or ring end is encountered, place each rx buffer on
         * a 256 byte boundary.
         */
        smctr_malloc(dev, TO_256_BYTE_BOUNDRY(tp->sh_mem_used));
        tp->rx_buff_head[NON_MAC_QUEUE] = (__u16 *)smctr_malloc(dev,
                RX_DATA_BUFFER_SIZE * tp->num_rx_bdbs[NON_MAC_QUEUE]);
        tp->rx_buff_end[NON_MAC_QUEUE] = (__u16 *)smctr_malloc(dev, 0);

        return (0);
}

/* Enter Bypass state. */
static int smctr_bypass_state(struct net_device *dev)
{
        int err;

	if(smctr_debug > 10)
        	printk(KERN_DEBUG "%s: smctr_bypass_state\n", dev->name);

        err = smctr_setup_single_cmd(dev, ACB_CMD_CHANGE_JOIN_STATE, JS_BYPASS_STATE);

        return (err);
}

static int smctr_checksum_firmware(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        __u16 i, checksum = 0;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_checksum_firmware\n", dev->name);

        smctr_enable_adapter_ctrl_store(dev);

        for(i = 0; i < CS_RAM_SIZE; i += 2)
                checksum += *((__u16 *)(tp->ram_access + i));

        tp->microcode_version = *(__u16 *)(tp->ram_access
                + CS_RAM_VERSION_OFFSET);
        tp->microcode_version >>= 8;

        smctr_disable_adapter_ctrl_store(dev);

        if(checksum)
                return (checksum);

        return (0);
}

static int __init smctr_chk_mca(struct net_device *dev)
{
#ifdef CONFIG_MCA_LEGACY
	struct net_local *tp = netdev_priv(dev);
	int current_slot;
	__u8 r1, r2, r3, r4, r5;

	current_slot = mca_find_unused_adapter(smctr_posid, 0);
	if(current_slot == MCA_NOTFOUND)
		return (-ENODEV);

	mca_set_adapter_name(current_slot, smctr_name);
	mca_mark_as_used(current_slot);
	tp->slot_num = current_slot;

	r1 = mca_read_stored_pos(tp->slot_num, 2);
	r2 = mca_read_stored_pos(tp->slot_num, 3);

	if(tp->slot_num)
		outb(CNFG_POS_CONTROL_REG, (__u8)((tp->slot_num - 1) | CNFG_SLOT_ENABLE_BIT));
	else
		outb(CNFG_POS_CONTROL_REG, (__u8)((tp->slot_num) | CNFG_SLOT_ENABLE_BIT));

	r1 = inb(CNFG_POS_REG1);
	r2 = inb(CNFG_POS_REG0);

	tp->bic_type = BIC_594_CHIP;

	/* IO */
	r2 = mca_read_stored_pos(tp->slot_num, 2);
	r2 &= 0xF0;
	dev->base_addr = ((__u16)r2 << 8) + (__u16)0x800;
	request_region(dev->base_addr, SMCTR_IO_EXTENT, smctr_name);

	/* IRQ */
	r5 = mca_read_stored_pos(tp->slot_num, 5);
	r5 &= 0xC;
        switch(r5)
	{
            	case 0:
			dev->irq = 3;
               		break;

            	case 0x4:
			dev->irq = 4;
               		break;

            	case 0x8:
			dev->irq = 10;
               		break;

            	default:
			dev->irq = 15;
               		break;
	}
	if (request_irq(dev->irq, smctr_interrupt, IRQF_SHARED, smctr_name, dev)) {
		release_region(dev->base_addr, SMCTR_IO_EXTENT);
		return -ENODEV;
	}

	/* Get RAM base */
	r3 = mca_read_stored_pos(tp->slot_num, 3);
	tp->ram_base = ((__u32)(r3 & 0x7) << 13) + 0x0C0000;
	if (r3 & 0x8)
		tp->ram_base += 0x010000;
	if (r3 & 0x80)
		tp->ram_base += 0xF00000;

	/* Get Ram Size */
	r3 &= 0x30;
	r3 >>= 4;

	tp->ram_usable = (__u16)CNFG_SIZE_8KB << r3;
	tp->ram_size = (__u16)CNFG_SIZE_64KB;
	tp->board_id |= TOKEN_MEDIA;

	r4 = mca_read_stored_pos(tp->slot_num, 4);
	tp->rom_base = ((__u32)(r4 & 0x7) << 13) + 0x0C0000;
	if (r4 & 0x8)
		tp->rom_base += 0x010000;

	/* Get ROM size. */
	r4 >>= 4;
	switch (r4) {
		case 0:
			tp->rom_size = CNFG_SIZE_8KB;
			break;
		case 1:
			tp->rom_size = CNFG_SIZE_16KB;
			break;
		case 2:
			tp->rom_size = CNFG_SIZE_32KB;
			break;
		default:
			tp->rom_size = ROM_DISABLE;
	}

	/* Get Media Type. */
	r5 = mca_read_stored_pos(tp->slot_num, 5);
	r5 &= CNFG_MEDIA_TYPE_MASK;
	switch(r5)
	{
		case (0):
			tp->media_type = MEDIA_STP_4;
			break;

		case (1):
			tp->media_type = MEDIA_STP_16;
			break;

		case (3):
			tp->media_type = MEDIA_UTP_16;
			break;

		default:
			tp->media_type = MEDIA_UTP_4;
			break;
	}
	tp->media_menu = 14;

	r2 = mca_read_stored_pos(tp->slot_num, 2);
	if(!(r2 & 0x02))
		tp->mode_bits |= EARLY_TOKEN_REL;

	/* Disable slot */
	outb(CNFG_POS_CONTROL_REG, 0);

	tp->board_id = smctr_get_boardid(dev, 1);
	switch(tp->board_id & 0xffff)
        {
                case WD8115TA:
                        smctr_model = "8115T/A";
                        break;

                case WD8115T:
			if(tp->extra_info & CHIP_REV_MASK)
                                smctr_model = "8115T rev XE";
                        else
                                smctr_model = "8115T rev XD";
                        break;

                default:
                        smctr_model = "Unknown";
                        break;
        }

	return (0);
#else
	return (-1);
#endif /* CONFIG_MCA_LEGACY */
}

static int smctr_chg_rx_mask(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int err = 0;

        if(smctr_debug > 10)
		printk(KERN_DEBUG "%s: smctr_chg_rx_mask\n", dev->name);

        smctr_enable_16bit(dev);
        smctr_set_page(dev, (__u8 *)tp->ram_access);

        if(tp->mode_bits & LOOPING_MODE_MASK)
                tp->config_word0 |= RX_OWN_BIT;
        else
                tp->config_word0 &= ~RX_OWN_BIT;

        if(tp->receive_mask & PROMISCUOUS_MODE)
                tp->config_word0 |= PROMISCUOUS_BIT;
        else
                tp->config_word0 &= ~PROMISCUOUS_BIT;

        if(tp->receive_mask & ACCEPT_ERR_PACKETS)
                tp->config_word0 |= SAVBAD_BIT;
        else
                tp->config_word0 &= ~SAVBAD_BIT;

        if(tp->receive_mask & ACCEPT_ATT_MAC_FRAMES)
                tp->config_word0 |= RXATMAC;
        else
                tp->config_word0 &= ~RXATMAC;

        if(tp->receive_mask & ACCEPT_MULTI_PROM)
                tp->config_word1 |= MULTICAST_ADDRESS_BIT;
        else
                tp->config_word1 &= ~MULTICAST_ADDRESS_BIT;

        if(tp->receive_mask & ACCEPT_SOURCE_ROUTING_SPANNING)
                tp->config_word1 |= SOURCE_ROUTING_SPANNING_BITS;
        else
        {
                if(tp->receive_mask & ACCEPT_SOURCE_ROUTING)
                        tp->config_word1 |= SOURCE_ROUTING_EXPLORER_BIT;
                else
                        tp->config_word1 &= ~SOURCE_ROUTING_SPANNING_BITS;
        }

        if((err = smctr_issue_write_word_cmd(dev, RW_CONFIG_REGISTER_0,
                &tp->config_word0)))
        {
                return (err);
        }

        if((err = smctr_issue_write_word_cmd(dev, RW_CONFIG_REGISTER_1,
                &tp->config_word1)))
        {
                return (err);
        }

        smctr_disable_16bit(dev);

        return (0);
}

static int smctr_clear_int(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);

        outb((tp->trc_mask | CSR_CLRTINT), dev->base_addr + CSR);

        return (0);
}

static int smctr_clear_trc_reset(int ioaddr)
{
        __u8 r;

        r = inb(ioaddr + MSR);
        outb(~MSR_RST & r, ioaddr + MSR);

        return (0);
}

/*
 * The inverse routine to smctr_open().
 */
static int smctr_close(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        struct sk_buff *skb;
        int err;

	netif_stop_queue(dev);
	
	tp->cleanup = 1;

        /* Check to see if adapter is already in a closed state. */
        if(tp->status != OPEN)
                return (0);

        smctr_enable_16bit(dev);
        smctr_set_page(dev, (__u8 *)tp->ram_access);

        if((err = smctr_issue_remove_cmd(dev)))
        {
                smctr_disable_16bit(dev);
                return (err);
        }

        for(;;)
        {
                skb = skb_dequeue(&tp->SendSkbQueue);
                if(skb == NULL)
                        break;
                tp->QueueSkb++;
                dev_kfree_skb(skb);
        }


        return (0);
}

static int smctr_decode_firmware(struct net_device *dev,
				 const struct firmware *fw)
{
        struct net_local *tp = netdev_priv(dev);
        short bit = 0x80, shift = 12;
        DECODE_TREE_NODE *tree;
        short branch, tsize;
        __u16 buff = 0;
        long weight;
        __u8 *ucode;
        __u16 *mem;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_decode_firmware\n", dev->name);

        weight  = *(long *)(fw->data + WEIGHT_OFFSET);
        tsize   = *(__u8 *)(fw->data + TREE_SIZE_OFFSET);
        tree    = (DECODE_TREE_NODE *)(fw->data + TREE_OFFSET);
        ucode   = (__u8 *)(fw->data + TREE_OFFSET
                        + (tsize * sizeof(DECODE_TREE_NODE)));
        mem     = (__u16 *)(tp->ram_access);

        while(weight)
        {
                branch = ROOT;
                while((tree + branch)->tag != LEAF && weight)
                {
                        branch = *ucode & bit ? (tree + branch)->llink
                                : (tree + branch)->rlink;

                        bit >>= 1;
                        weight--;

                        if(bit == 0)
                        {
                                bit = 0x80;
                                ucode++;
                        }
                }

                buff |= (tree + branch)->info << shift;
                shift -= 4;

                if(shift < 0)
                {
                        *(mem++) = SWAP_BYTES(buff);
                        buff    = 0;
                        shift   = 12;
                }
        }

        /* The following assumes the Control Store Memory has
         * been initialized to zero. If the last partial word
         * is zero, it will not be written.
         */
        if(buff)
                *(mem++) = SWAP_BYTES(buff);

        return (0);
}

static int smctr_disable_16bit(struct net_device *dev)
{
        return (0);
}

/*
 * On Exit, Adapter is:
 * 1. TRC is in a reset state and un-initialized.
 * 2. Adapter memory is enabled.
 * 3. Control Store memory is out of context (-WCSS is 1).
 */
static int smctr_disable_adapter_ctrl_store(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int ioaddr = dev->base_addr;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_disable_adapter_ctrl_store\n", dev->name);

        tp->trc_mask |= CSR_WCSS;
        outb(tp->trc_mask, ioaddr + CSR);

        return (0);
}

static int smctr_disable_bic_int(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int ioaddr = dev->base_addr;

        tp->trc_mask = CSR_MSK_ALL | CSR_MSKCBUSY
	        | CSR_MSKTINT | CSR_WCSS;
        outb(tp->trc_mask, ioaddr + CSR);

        return (0);
}

static int smctr_enable_16bit(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        __u8    r;

        if(tp->adapter_bus == BUS_ISA16_TYPE)
        {
                r = inb(dev->base_addr + LAAR);
                outb((r | LAAR_MEM16ENB), dev->base_addr + LAAR);
        }

        return (0);
}

/*
 * To enable the adapter control store memory:
 * 1. Adapter must be in a RESET state.
 * 2. Adapter memory must be enabled.
 * 3. Control Store Memory is in context (-WCSS is 0).
 */
static int smctr_enable_adapter_ctrl_store(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int ioaddr = dev->base_addr;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_enable_adapter_ctrl_store\n", dev->name);

        smctr_set_trc_reset(ioaddr);
        smctr_enable_adapter_ram(dev);

        tp->trc_mask &= ~CSR_WCSS;
        outb(tp->trc_mask, ioaddr + CSR);

        return (0);
}

static int smctr_enable_adapter_ram(struct net_device *dev)
{
        int ioaddr = dev->base_addr;
        __u8 r;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_enable_adapter_ram\n", dev->name);

        r = inb(ioaddr + MSR);
        outb(MSR_MEMB | r, ioaddr + MSR);

        return (0);
}

static int smctr_enable_bic_int(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int ioaddr = dev->base_addr;
        __u8 r;

        switch(tp->bic_type)
        {
                case (BIC_584_CHIP):
                        tp->trc_mask = CSR_MSKCBUSY | CSR_WCSS;
                        outb(tp->trc_mask, ioaddr + CSR);
                        r = inb(ioaddr + IRR);
                        outb(r | IRR_IEN, ioaddr + IRR);
                        break;

                case (BIC_594_CHIP):
                        tp->trc_mask = CSR_MSKCBUSY | CSR_WCSS;
                        outb(tp->trc_mask, ioaddr + CSR);
                        r = inb(ioaddr + IMCCR);
                        outb(r | IMCCR_EIL, ioaddr + IMCCR);
                        break;
        }

        return (0);
}

static int __init smctr_chk_isa(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int ioaddr = dev->base_addr;
        __u8 r1, r2, b, chksum = 0;
        __u16 r;
	int i;
	int err = -ENODEV;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_chk_isa %#4x\n", dev->name, ioaddr);

	if((ioaddr & 0x1F) != 0)
                goto out;

        /* Grab the region so that no one else tries to probe our ioports. */
	if (!request_region(ioaddr, SMCTR_IO_EXTENT, smctr_name)) {
		err = -EBUSY;
		goto out;
	}

        /* Checksum SMC node address */
        for(i = 0; i < 8; i++)
        {
                b = inb(ioaddr + LAR0 + i);
                chksum += b;
        }

        if (chksum != NODE_ADDR_CKSUM)
                goto out2;

        b = inb(ioaddr + BDID);
	if(b != BRD_ID_8115T)
        {
                printk(KERN_ERR "%s: The adapter found is not supported\n", dev->name);
                goto out2;
        }

        /* Check for 8115T Board ID */
        r2 = 0;
        for(r = 0; r < 8; r++)
        {
            r1 = inb(ioaddr + 0x8 + r);
            r2 += r1;
        }

        /* value of RegF adds up the sum to 0xFF */
        if((r2 != 0xFF) && (r2 != 0xEE))
                goto out2;

        /* Get adapter ID */
        tp->board_id = smctr_get_boardid(dev, 0);
        switch(tp->board_id & 0xffff)
        {
                case WD8115TA:
                        smctr_model = "8115T/A";
                        break;

                case WD8115T:
			if(tp->extra_info & CHIP_REV_MASK)
                                smctr_model = "8115T rev XE";
                        else
                                smctr_model = "8115T rev XD";
                        break;

                default:
                        smctr_model = "Unknown";
                        break;
        }

        /* Store BIC type. */
        tp->bic_type = BIC_584_CHIP;
        tp->nic_type = NIC_825_CHIP;

        /* Copy Ram Size */
        tp->ram_usable  = CNFG_SIZE_16KB;
        tp->ram_size    = CNFG_SIZE_64KB;

        /* Get 58x Ram Base */
        r1 = inb(ioaddr);
        r1 &= 0x3F;

        r2 = inb(ioaddr + CNFG_LAAR_584);
        r2 &= CNFG_LAAR_MASK;
        r2 <<= 3;
        r2 |= ((r1 & 0x38) >> 3);

        tp->ram_base = ((__u32)r2 << 16) + (((__u32)(r1 & 0x7)) << 13);

        /* Get 584 Irq */
        r1 = 0;
        r1 = inb(ioaddr + CNFG_ICR_583);
        r1 &= CNFG_ICR_IR2_584;

        r2 = inb(ioaddr + CNFG_IRR_583);
        r2 &= CNFG_IRR_IRQS;     /* 0x60 */
        r2 >>= 5;

        switch(r2)
        {
                case 0:
                        if(r1 == 0)
                                dev->irq = 2;
                        else
                                dev->irq = 10;
                        break;

                case 1:
                        if(r1 == 0)
                                dev->irq = 3;
                        else
                                dev->irq = 11;
                        break;

                case 2:
                        if(r1 == 0)
                        {
                                if(tp->extra_info & ALTERNATE_IRQ_BIT)
                                        dev->irq = 5;
                                else
                                        dev->irq = 4;
                        }
                        else
                                dev->irq = 15;
                        break;

                case 3:
                        if(r1 == 0)
                                dev->irq = 7;
                        else
                                dev->irq = 4;
                        break;

                default:
                        printk(KERN_ERR "%s: No IRQ found aborting\n", dev->name);
                        goto out2;
         }

        if (request_irq(dev->irq, smctr_interrupt, IRQF_SHARED, smctr_name, dev))
                goto out2;

        /* Get 58x Rom Base */
        r1 = inb(ioaddr + CNFG_BIO_583);
        r1 &= 0x3E;
        r1 |= 0x40;

        tp->rom_base = (__u32)r1 << 13;

        /* Get 58x Rom Size */
        r1 = inb(ioaddr + CNFG_BIO_583);
        r1 &= 0xC0;
        if(r1 == 0)
                tp->rom_size = ROM_DISABLE;
        else
        {
                r1 >>= 6;
                tp->rom_size = (__u16)CNFG_SIZE_8KB << r1;
        }

        /* Get 58x Boot Status */
        r1 = inb(ioaddr + CNFG_GP2);

        tp->mode_bits &= (~BOOT_STATUS_MASK);

        if(r1 & CNFG_GP2_BOOT_NIBBLE)
                tp->mode_bits |= BOOT_TYPE_1;

        /* Get 58x Zero Wait State */
        tp->mode_bits &= (~ZERO_WAIT_STATE_MASK);

        r1 = inb(ioaddr + CNFG_IRR_583);

        if(r1 & CNFG_IRR_ZWS)
                 tp->mode_bits |= ZERO_WAIT_STATE_8_BIT;

        if(tp->board_id & BOARD_16BIT)
        {
                r1 = inb(ioaddr + CNFG_LAAR_584);

                if(r1 & CNFG_LAAR_ZWS)
                        tp->mode_bits |= ZERO_WAIT_STATE_16_BIT;
        }

        /* Get 584 Media Menu */
        tp->media_menu = 14;
        r1 = inb(ioaddr + CNFG_IRR_583);

        tp->mode_bits &= 0xf8ff;       /* (~CNFG_INTERFACE_TYPE_MASK) */
        if((tp->board_id & TOKEN_MEDIA) == TOKEN_MEDIA)
        {
                /* Get Advanced Features */
                if(((r1 & 0x6) >> 1) == 0x3)
                        tp->media_type |= MEDIA_UTP_16;
                else
                {
                        if(((r1 & 0x6) >> 1) == 0x2)
                                tp->media_type |= MEDIA_STP_16;
                        else
                        {
                                if(((r1 & 0x6) >> 1) == 0x1)
                                        tp->media_type |= MEDIA_UTP_4;

                                else
                                        tp->media_type |= MEDIA_STP_4;
                        }
                }

                r1 = inb(ioaddr + CNFG_GP2);
                if(!(r1 & 0x2) )           /* GP2_ETRD */
                        tp->mode_bits |= EARLY_TOKEN_REL;

                /* see if the chip is corrupted
                if(smctr_read_584_chksum(ioaddr))
                {
                        printk(KERN_ERR "%s: EEPROM Checksum Failure\n", dev->name);
			free_irq(dev->irq, dev);
                        goto out2;
                }
		*/
        }

        return (0);

out2:
	release_region(ioaddr, SMCTR_IO_EXTENT);
out:
	return err;
}

static int __init smctr_get_boardid(struct net_device *dev, int mca)
{
        struct net_local *tp = netdev_priv(dev);
        int ioaddr = dev->base_addr;
        __u8 r, r1, IdByte;
        __u16 BoardIdMask;

        tp->board_id = BoardIdMask = 0;

	if(mca)
	{
		BoardIdMask |= (MICROCHANNEL+INTERFACE_CHIP+TOKEN_MEDIA+PAGED_RAM+BOARD_16BIT);
		tp->extra_info |= (INTERFACE_594_CHIP+RAM_SIZE_64K+NIC_825_BIT+ALTERNATE_IRQ_BIT+SLOT_16BIT);
	}
	else
	{
        	BoardIdMask|=(INTERFACE_CHIP+TOKEN_MEDIA+PAGED_RAM+BOARD_16BIT);
        	tp->extra_info |= (INTERFACE_584_CHIP + RAM_SIZE_64K
        	        + NIC_825_BIT + ALTERNATE_IRQ_BIT);
	}

	if(!mca)
	{
        	r = inb(ioaddr + BID_REG_1);
        	r &= 0x0c;
       		outb(r, ioaddr + BID_REG_1);
        	r = inb(ioaddr + BID_REG_1);

        	if(r & BID_SIXTEEN_BIT_BIT)
        	{
        	        tp->extra_info |= SLOT_16BIT;
        	        tp->adapter_bus = BUS_ISA16_TYPE;
        	}
        	else
        	        tp->adapter_bus = BUS_ISA8_TYPE;
	}
	else
		tp->adapter_bus = BUS_MCA_TYPE;

        /* Get Board Id Byte */
        IdByte = inb(ioaddr + BID_BOARD_ID_BYTE);

        /* if Major version > 1.0 then
         *      return;
         */
        if(IdByte & 0xF8)
                return (-1);

        r1 = inb(ioaddr + BID_REG_1);
        r1 &= BID_ICR_MASK;
        r1 |= BID_OTHER_BIT;

        outb(r1, ioaddr + BID_REG_1);
        r1 = inb(ioaddr + BID_REG_3);

        r1 &= BID_EAR_MASK;
        r1 |= BID_ENGR_PAGE;

        outb(r1, ioaddr + BID_REG_3);
        r1 = inb(ioaddr + BID_REG_1);
        r1 &= BID_ICR_MASK;
        r1 |= (BID_RLA | BID_OTHER_BIT);

        outb(r1, ioaddr + BID_REG_1);

        r1 = inb(ioaddr + BID_REG_1);
        while(r1 & BID_RECALL_DONE_MASK)
                r1 = inb(ioaddr + BID_REG_1);

        r = inb(ioaddr + BID_LAR_0 + BID_REG_6);

        /* clear chip rev bits */
        tp->extra_info &= ~CHIP_REV_MASK;
        tp->extra_info |= ((r & BID_EEPROM_CHIP_REV_MASK) << 6);

        r1 = inb(ioaddr + BID_REG_1);
        r1 &= BID_ICR_MASK;
        r1 |= BID_OTHER_BIT;

        outb(r1, ioaddr + BID_REG_1);
        r1 = inb(ioaddr + BID_REG_3);

        r1 &= BID_EAR_MASK;
        r1 |= BID_EA6;

        outb(r1, ioaddr + BID_REG_3);
        r1 = inb(ioaddr + BID_REG_1);

        r1 &= BID_ICR_MASK;
        r1 |= BID_RLA;

        outb(r1, ioaddr + BID_REG_1);
        r1 = inb(ioaddr + BID_REG_1);

        while(r1 & BID_RECALL_DONE_MASK)
                r1 = inb(ioaddr + BID_REG_1);

        return (BoardIdMask);
}

static int smctr_get_group_address(struct net_device *dev)
{
        smctr_issue_read_word_cmd(dev, RW_INDIVIDUAL_GROUP_ADDR);

        return(smctr_wait_cmd(dev));
}

static int smctr_get_functional_address(struct net_device *dev)
{
        smctr_issue_read_word_cmd(dev, RW_FUNCTIONAL_ADDR);

        return(smctr_wait_cmd(dev));
}

/* Calculate number of Non-MAC receive BDB's and data buffers.
 * This function must simulate allocateing shared memory exactly
 * as the allocate_shared_memory function above.
 */
static unsigned int smctr_get_num_rx_bdbs(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int mem_used = 0;

        /* Allocate System Control Blocks. */
        mem_used += sizeof(SCGBlock);

        mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
        mem_used += sizeof(SCLBlock);

        mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
        mem_used += sizeof(ACBlock) * tp->num_acbs;

        mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
        mem_used += sizeof(ISBlock);

        mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);
        mem_used += MISC_DATA_SIZE;

        /* Allocate transmit FCB's. */
        mem_used += TO_PARAGRAPH_BOUNDRY(mem_used);

        mem_used += sizeof(FCBlock) * tp->num_tx_fcbs[MAC_QUEUE];
        mem_used += sizeof(FCBlock) * tp->num_tx_fcbs[NON_MAC_QUEUE];
        mem_used += sizeof(FCBlock) * tp->num_tx_fcbs[BUG_QUEUE];

        /* Allocate transmit BDBs. */
        mem_used += sizeof(BDBlock) * tp->num_tx_bdbs[MAC_QUEUE];
        mem_used += sizeof(BDBlock) * tp->num_tx_bdbs[NON_MAC_QUEUE];
        mem_used += sizeof(BDBlock) * tp->num_tx_bdbs[BUG_QUEUE];

        /* Allocate receive FCBs. */
        mem_used += sizeof(FCBlock) * tp->num_rx_fcbs[MAC_QUEUE];
        mem_used += sizeof(FCBlock) * tp->num_rx_fcbs[NON_MAC_QUEUE];

        /* Allocate receive BDBs. */
        mem_used += sizeof(BDBlock) * tp->num_rx_bdbs[MAC_QUEUE];

        /* Allocate MAC transmit buffers.
         * MAC transmit buffers don't have to be on an ODD Boundry.
         */
        mem_used += tp->tx_buff_size[MAC_QUEUE];

        /* Allocate BUG transmit buffers. */
        mem_used += tp->tx_buff_size[BUG_QUEUE];

        /* Allocate MAC receive data buffers.
         * MAC receive buffers don't have to be on a 256 byte boundary.
         */
        mem_used += RX_DATA_BUFFER_SIZE * tp->num_rx_bdbs[MAC_QUEUE];

        /* Allocate Non-MAC transmit buffers.
         * For maximum Netware performance, put Tx Buffers on
         * ODD Boundry,and then restore malloc to Even Boundrys.
         */
        mem_used += 1L;
        mem_used += tp->tx_buff_size[NON_MAC_QUEUE];
        mem_used += 1L;

        /* CALCULATE NUMBER OF NON-MAC RX BDB'S
         * AND NON-MAC RX DATA BUFFERS
         *
         * Make sure the mem_used offset at this point is the
         * same as in allocate_shared memory or the following
         * boundary adjustment will be incorrect (i.e. not allocating
         * the non-mac receive buffers above cannot change the 256
         * byte offset).
         *
         * Since this cannot be guaranteed, adding the full 256 bytes
         * to the amount of shared memory used at this point will guaranteed
         * that the rx data buffers do not overflow shared memory.
         */
        mem_used += 0x100;

        return((0xffff - mem_used) / (RX_DATA_BUFFER_SIZE + sizeof(BDBlock)));
}

static int smctr_get_physical_drop_number(struct net_device *dev)
{
        smctr_issue_read_word_cmd(dev, RW_PHYSICAL_DROP_NUMBER);

        return(smctr_wait_cmd(dev));
}

static __u8 * smctr_get_rx_pointer(struct net_device *dev, short queue)
{
        struct net_local *tp = netdev_priv(dev);
        BDBlock *bdb;

        bdb = (BDBlock *)((__u32)tp->ram_access
                + (__u32)(tp->rx_fcb_curr[queue]->trc_bdb_ptr));

        tp->rx_fcb_curr[queue]->bdb_ptr = bdb;

        return ((__u8 *)bdb->data_block_ptr);
}

static int smctr_get_station_id(struct net_device *dev)
{
        smctr_issue_read_word_cmd(dev, RW_INDIVIDUAL_MAC_ADDRESS);

        return(smctr_wait_cmd(dev));
}

/*
 * Get the current statistics. This may be called with the card open
 * or closed.
 */
static struct net_device_stats *smctr_get_stats(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);

        return ((struct net_device_stats *)&tp->MacStat);
}

static FCBlock *smctr_get_tx_fcb(struct net_device *dev, __u16 queue,
        __u16 bytes_count)
{
        struct net_local *tp = netdev_priv(dev);
        FCBlock *pFCB;
        BDBlock *pbdb;
        unsigned short alloc_size;
        unsigned short *temp;

        if(smctr_debug > 20)
                printk(KERN_DEBUG "smctr_get_tx_fcb\n");

        /* check if there is enough FCB blocks */
        if(tp->num_tx_fcbs_used[queue] >= tp->num_tx_fcbs[queue])
                return ((FCBlock *)(-1L));

        /* round off the input pkt size to the nearest even number */
        alloc_size = (bytes_count + 1) & 0xfffe;

        /* check if enough mem */
        if((tp->tx_buff_used[queue] + alloc_size) > tp->tx_buff_size[queue])
                return ((FCBlock *)(-1L));

        /* check if past the end ;
         * if exactly enough mem to end of ring, alloc from front.
         * this avoids update of curr when curr = end
         */
        if(((unsigned long)(tp->tx_buff_curr[queue]) + alloc_size)
                >= (unsigned long)(tp->tx_buff_end[queue]))
        {
                /* check if enough memory from ring head */
                alloc_size = alloc_size +
                        (__u16)((__u32)tp->tx_buff_end[queue]
                        - (__u32)tp->tx_buff_curr[queue]);

                if((tp->tx_buff_used[queue] + alloc_size)
                        > tp->tx_buff_size[queue])
                {
                        return ((FCBlock *)(-1L));
                }

                /* ring wrap */
                tp->tx_buff_curr[queue] = tp->tx_buff_head[queue];
        }

        tp->tx_buff_used[queue] += alloc_size;
        tp->num_tx_fcbs_used[queue]++;
        tp->tx_fcb_curr[queue]->frame_length = bytes_count;
        tp->tx_fcb_curr[queue]->memory_alloc = alloc_size;
        temp = tp->tx_buff_curr[queue];
        tp->tx_buff_curr[queue]
                = (__u16 *)((__u32)temp + (__u32)((bytes_count + 1) & 0xfffe));

        pbdb = tp->tx_fcb_curr[queue]->bdb_ptr;
        pbdb->buffer_length = bytes_count;
        pbdb->data_block_ptr = temp;
        pbdb->trc_data_block_ptr = TRC_POINTER(temp);

        pFCB = tp->tx_fcb_curr[queue];
        tp->tx_fcb_curr[queue] = tp->tx_fcb_curr[queue]->next_ptr;

        return (pFCB);
}

static int smctr_get_upstream_neighbor_addr(struct net_device *dev)
{
        smctr_issue_read_word_cmd(dev, RW_UPSTREAM_NEIGHBOR_ADDRESS);

        return(smctr_wait_cmd(dev));
}

static int smctr_hardware_send_packet(struct net_device *dev,
        struct net_local *tp)
{
        struct tr_statistics *tstat = &tp->MacStat;
        struct sk_buff *skb;
        FCBlock *fcb;

        if(smctr_debug > 10)
                printk(KERN_DEBUG"%s: smctr_hardware_send_packet\n", dev->name);

        if(tp->status != OPEN)
                return (-1);

        if(tp->monitor_state_ready != 1)
                return (-1);

        for(;;)
        {
                /* Send first buffer from queue */
                skb = skb_dequeue(&tp->SendSkbQueue);
                if(skb == NULL)
                        return (-1);

                tp->QueueSkb++;

                if(skb->len < SMC_HEADER_SIZE || skb->len > tp->max_packet_size)                        return (-1);

                smctr_enable_16bit(dev);
                smctr_set_page(dev, (__u8 *)tp->ram_access);

                if((fcb = smctr_get_tx_fcb(dev, NON_MAC_QUEUE, skb->len))
                        == (FCBlock *)(-1L))
                {
                        smctr_disable_16bit(dev);
                        return (-1);
                }

                smctr_tx_move_frame(dev, skb,
                        (__u8 *)fcb->bdb_ptr->data_block_ptr, skb->len);

                smctr_set_page(dev, (__u8 *)fcb);

                smctr_trc_send_packet(dev, fcb, NON_MAC_QUEUE);
                dev_kfree_skb(skb);

                tstat->tx_packets++;

                smctr_disable_16bit(dev);
        }

        return (0);
}

static int smctr_init_acbs(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i;
        ACBlock *acb;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_init_acbs\n", dev->name);

        acb                     = tp->acb_head;
        acb->cmd_done_status    = (ACB_COMMAND_DONE | ACB_COMMAND_SUCCESSFUL);
        acb->cmd_info           = ACB_CHAIN_END;
        acb->cmd                = 0;
        acb->subcmd             = 0;
        acb->data_offset_lo     = 0;
        acb->data_offset_hi     = 0;
        acb->next_ptr
                = (ACBlock *)(((char *)acb) + sizeof(ACBlock));
        acb->trc_next_ptr       = TRC_POINTER(acb->next_ptr);

        for(i = 1; i < tp->num_acbs; i++)
        {
                acb             = acb->next_ptr;
                acb->cmd_done_status
                        = (ACB_COMMAND_DONE | ACB_COMMAND_SUCCESSFUL);
                acb->cmd_info = ACB_CHAIN_END;
                acb->cmd        = 0;
                acb->subcmd     = 0;
                acb->data_offset_lo = 0;
                acb->data_offset_hi = 0;
                acb->next_ptr
                        = (ACBlock *)(((char *)acb) + sizeof(ACBlock));
                acb->trc_next_ptr = TRC_POINTER(acb->next_ptr);
        }

        acb->next_ptr           = tp->acb_head;
        acb->trc_next_ptr       = TRC_POINTER(tp->acb_head);
        tp->acb_next            = tp->acb_head->next_ptr;
        tp->acb_curr            = tp->acb_head->next_ptr;
        tp->num_acbs_used       = 0;

        return (0);
}

static int smctr_init_adapter(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int err;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_init_adapter\n", dev->name);

        tp->status              = CLOSED;
        tp->page_offset_mask    = (tp->ram_usable * 1024) - 1;
        skb_queue_head_init(&tp->SendSkbQueue);
        tp->QueueSkb = MAX_TX_QUEUE;

        if(!(tp->group_address_0 & 0x0080))
                tp->group_address_0 |= 0x00C0;

        if(!(tp->functional_address_0 & 0x00C0))
                tp->functional_address_0 |= 0x00C0;

        tp->functional_address[0] &= 0xFF7F;

        if(tp->authorized_function_classes == 0)
                tp->authorized_function_classes = 0x7FFF;

        if(tp->authorized_access_priority == 0)
                tp->authorized_access_priority = 0x06;

        smctr_disable_bic_int(dev);
        smctr_set_trc_reset(dev->base_addr);

        smctr_enable_16bit(dev);
        smctr_set_page(dev, (__u8 *)tp->ram_access);

        if(smctr_checksum_firmware(dev))
	{
                printk(KERN_ERR "%s: Previously loaded firmware is missing\n",dev->name);                return (-ENOENT);
        }

        if((err = smctr_ram_memory_test(dev)))
	{
                printk(KERN_ERR "%s: RAM memory test failed.\n", dev->name);
                return (-EIO);
        }

	smctr_set_rx_look_ahead(dev);
        smctr_load_node_addr(dev);

        /* Initialize adapter for Internal Self Test. */
        smctr_reset_adapter(dev);
        if((err = smctr_init_card_real(dev)))
	{
                printk(KERN_ERR "%s: Initialization of card failed (%d)\n",
                        dev->name, err);
                return (-EINVAL);
        }

        /* This routine clobbers the TRC's internal registers. */
        if((err = smctr_internal_self_test(dev)))
	{
                printk(KERN_ERR "%s: Card failed internal self test (%d)\n",
                        dev->name, err);
                return (-EINVAL);
        }

        /* Re-Initialize adapter's internal registers */
        smctr_reset_adapter(dev);
        if((err = smctr_init_card_real(dev)))
	{
                printk(KERN_ERR "%s: Initialization of card failed (%d)\n",
                        dev->name, err);
                return (-EINVAL);
        }

        smctr_enable_bic_int(dev);

        if((err = smctr_issue_enable_int_cmd(dev, TRC_INTERRUPT_ENABLE_MASK)))
                return (err);

        smctr_disable_16bit(dev);

        return (0);
}

static int smctr_init_card_real(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int err = 0;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_init_card_real\n", dev->name);

        tp->sh_mem_used = 0;
        tp->num_acbs    = NUM_OF_ACBS;

        /* Range Check Max Packet Size */
        if(tp->max_packet_size < 256)
                tp->max_packet_size = 256;
        else
        {
                if(tp->max_packet_size > NON_MAC_TX_BUFFER_MEMORY)
                        tp->max_packet_size = NON_MAC_TX_BUFFER_MEMORY;
        }

        tp->num_of_tx_buffs = (NON_MAC_TX_BUFFER_MEMORY
                / tp->max_packet_size) - 1;

        if(tp->num_of_tx_buffs > NUM_NON_MAC_TX_FCBS)
                tp->num_of_tx_buffs = NUM_NON_MAC_TX_FCBS;
        else
        {
                if(tp->num_of_tx_buffs == 0)
                        tp->num_of_tx_buffs = 1;
        }

        /* Tx queue constants */
        tp->num_tx_fcbs        [BUG_QUEUE]     = NUM_BUG_TX_FCBS;
        tp->num_tx_bdbs        [BUG_QUEUE]     = NUM_BUG_TX_BDBS;
        tp->tx_buff_size       [BUG_QUEUE]     = BUG_TX_BUFFER_MEMORY;
        tp->tx_buff_used       [BUG_QUEUE]     = 0;
        tp->tx_queue_status    [BUG_QUEUE]     = NOT_TRANSMITING;

        tp->num_tx_fcbs        [MAC_QUEUE]     = NUM_MAC_TX_FCBS;
        tp->num_tx_bdbs        [MAC_QUEUE]     = NUM_MAC_TX_BDBS;
        tp->tx_buff_size       [MAC_QUEUE]     = MAC_TX_BUFFER_MEMORY;
        tp->tx_buff_used       [MAC_QUEUE]     = 0;
        tp->tx_queue_status    [MAC_QUEUE]     = NOT_TRANSMITING;

        tp->num_tx_fcbs        [NON_MAC_QUEUE] = NUM_NON_MAC_TX_FCBS;
        tp->num_tx_bdbs        [NON_MAC_QUEUE] = NUM_NON_MAC_TX_BDBS;
        tp->tx_buff_size       [NON_MAC_QUEUE] = NON_MAC_TX_BUFFER_MEMORY;
        tp->tx_buff_used       [NON_MAC_QUEUE] = 0;
        tp->tx_queue_status    [NON_MAC_QUEUE] = NOT_TRANSMITING;

        /* Receive Queue Constants */
        tp->num_rx_fcbs[MAC_QUEUE] = NUM_MAC_RX_FCBS;
        tp->num_rx_bdbs[MAC_QUEUE] = NUM_MAC_RX_BDBS;

        if(tp->extra_info & CHIP_REV_MASK)
                tp->num_rx_fcbs[NON_MAC_QUEUE] = 78;    /* 825 Rev. XE */
        else
                tp->num_rx_fcbs[NON_MAC_QUEUE] = 7;     /* 825 Rev. XD */

        tp->num_rx_bdbs[NON_MAC_QUEUE] = smctr_get_num_rx_bdbs(dev);

        smctr_alloc_shared_memory(dev);
        smctr_init_shared_memory(dev);

        if((err = smctr_issue_init_timers_cmd(dev)))
                return (err);

        if((err = smctr_issue_init_txrx_cmd(dev)))
	{
                printk(KERN_ERR "%s: Hardware failure\n", dev->name);
                return (err);
        }

        return (0);
}

static int smctr_init_rx_bdbs(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i, j;
        BDBlock *bdb;
        __u16 *buf;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_init_rx_bdbs\n", dev->name);

        for(i = 0; i < NUM_RX_QS_USED; i++)
        {
                bdb = tp->rx_bdb_head[i];
                buf = tp->rx_buff_head[i];
                bdb->info = (BDB_CHAIN_END | BDB_NO_WARNING);
                bdb->buffer_length = RX_DATA_BUFFER_SIZE;
                bdb->next_ptr = (BDBlock *)(((char *)bdb) + sizeof(BDBlock));
                bdb->data_block_ptr = buf;
                bdb->trc_next_ptr = TRC_POINTER(bdb->next_ptr);

                if(i == NON_MAC_QUEUE)
                        bdb->trc_data_block_ptr = RX_BUFF_TRC_POINTER(buf);
                else
                        bdb->trc_data_block_ptr = TRC_POINTER(buf);

                for(j = 1; j < tp->num_rx_bdbs[i]; j++)
                {
                        bdb->next_ptr->back_ptr = bdb;
                        bdb = bdb->next_ptr;
                        buf = (__u16 *)((char *)buf + RX_DATA_BUFFER_SIZE);
                        bdb->info = (BDB_NOT_CHAIN_END | BDB_NO_WARNING);
                        bdb->buffer_length = RX_DATA_BUFFER_SIZE;
                        bdb->next_ptr = (BDBlock *)(((char *)bdb) + sizeof(BDBlock));
                        bdb->data_block_ptr = buf;
                        bdb->trc_next_ptr = TRC_POINTER(bdb->next_ptr);

                        if(i == NON_MAC_QUEUE)
                                bdb->trc_data_block_ptr = RX_BUFF_TRC_POINTER(buf);
                        else
                                bdb->trc_data_block_ptr = TRC_POINTER(buf);
                }

                bdb->next_ptr           = tp->rx_bdb_head[i];
                bdb->trc_next_ptr       = TRC_POINTER(tp->rx_bdb_head[i]);

                tp->rx_bdb_head[i]->back_ptr    = bdb;
                tp->rx_bdb_curr[i]              = tp->rx_bdb_head[i]->next_ptr;
        }

        return (0);
}

static int smctr_init_rx_fcbs(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i, j;
        FCBlock *fcb;

        for(i = 0; i < NUM_RX_QS_USED; i++)
        {
                fcb               = tp->rx_fcb_head[i];
                fcb->frame_status = 0;
                fcb->frame_length = 0;
                fcb->info         = FCB_CHAIN_END;
                fcb->next_ptr     = (FCBlock *)(((char*)fcb) + sizeof(FCBlock));
                if(i == NON_MAC_QUEUE)
                        fcb->trc_next_ptr = RX_FCB_TRC_POINTER(fcb->next_ptr);
                else
                        fcb->trc_next_ptr = TRC_POINTER(fcb->next_ptr);

                for(j = 1; j < tp->num_rx_fcbs[i]; j++)
                {
                        fcb->next_ptr->back_ptr = fcb;
                        fcb                     = fcb->next_ptr;
                        fcb->frame_status       = 0;
                        fcb->frame_length       = 0;
                        fcb->info               = FCB_WARNING;
                        fcb->next_ptr
                                = (FCBlock *)(((char *)fcb) + sizeof(FCBlock));

                        if(i == NON_MAC_QUEUE)
                                fcb->trc_next_ptr
                                        = RX_FCB_TRC_POINTER(fcb->next_ptr);
                        else
                                fcb->trc_next_ptr
                                        = TRC_POINTER(fcb->next_ptr);
                }

                fcb->next_ptr = tp->rx_fcb_head[i];

                if(i == NON_MAC_QUEUE)
                        fcb->trc_next_ptr = RX_FCB_TRC_POINTER(fcb->next_ptr);
                else
                        fcb->trc_next_ptr = TRC_POINTER(fcb->next_ptr);

                tp->rx_fcb_head[i]->back_ptr    = fcb;
                tp->rx_fcb_curr[i]              = tp->rx_fcb_head[i]->next_ptr;
        }

        return(0);
}

static int smctr_init_shared_memory(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i;
        __u32 *iscpb;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_init_shared_memory\n", dev->name);

        smctr_set_page(dev, (__u8 *)(unsigned int)tp->iscpb_ptr);

        /* Initialize Initial System Configuration Point. (ISCP) */
        iscpb = (__u32 *)PAGE_POINTER(&tp->iscpb_ptr->trc_scgb_ptr);
        *iscpb = (__u32)(SWAP_WORDS(TRC_POINTER(tp->scgb_ptr)));

        smctr_set_page(dev, (__u8 *)tp->ram_access);

        /* Initialize System Configuration Pointers. (SCP) */
        tp->scgb_ptr->config = (SCGB_ADDRESS_POINTER_FORMAT
                | SCGB_MULTI_WORD_CONTROL | SCGB_DATA_FORMAT
                | SCGB_BURST_LENGTH);

        tp->scgb_ptr->trc_sclb_ptr      = TRC_POINTER(tp->sclb_ptr);
        tp->scgb_ptr->trc_acb_ptr       = TRC_POINTER(tp->acb_head);
        tp->scgb_ptr->trc_isb_ptr       = TRC_POINTER(tp->isb_ptr);
        tp->scgb_ptr->isbsiz            = (sizeof(ISBlock)) - 2;

        /* Initialize System Control Block. (SCB) */
        tp->sclb_ptr->valid_command    = SCLB_VALID | SCLB_CMD_NOP;
        tp->sclb_ptr->iack_code        = 0;
        tp->sclb_ptr->resume_control   = 0;
        tp->sclb_ptr->int_mask_control = 0;
        tp->sclb_ptr->int_mask_state   = 0;

        /* Initialize Interrupt Status Block. (ISB) */
        for(i = 0; i < NUM_OF_INTERRUPTS; i++)
        {
                tp->isb_ptr->IStatus[i].IType = 0xf0;
                tp->isb_ptr->IStatus[i].ISubtype = 0;
        }

        tp->current_isb_index = 0;

        /* Initialize Action Command Block. (ACB) */
        smctr_init_acbs(dev);

        /* Initialize transmit FCB's and BDB's. */
        smctr_link_tx_fcbs_to_bdbs(dev);
        smctr_init_tx_bdbs(dev);
        smctr_init_tx_fcbs(dev);

        /* Initialize receive FCB's and BDB's. */
        smctr_init_rx_bdbs(dev);
        smctr_init_rx_fcbs(dev);

        return (0);
}

static int smctr_init_tx_bdbs(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i, j;
        BDBlock *bdb;

        for(i = 0; i < NUM_TX_QS_USED; i++)
        {
                bdb = tp->tx_bdb_head[i];
                bdb->info = (BDB_NOT_CHAIN_END | BDB_NO_WARNING);
                bdb->next_ptr = (BDBlock *)(((char *)bdb) + sizeof(BDBlock));
                bdb->trc_next_ptr = TRC_POINTER(bdb->next_ptr);

                for(j = 1; j < tp->num_tx_bdbs[i]; j++)
                {
                        bdb->next_ptr->back_ptr = bdb;
                        bdb = bdb->next_ptr;
                        bdb->info = (BDB_NOT_CHAIN_END | BDB_NO_WARNING);
                        bdb->next_ptr
                                = (BDBlock *)(((char *)bdb) + sizeof( BDBlock));                        bdb->trc_next_ptr = TRC_POINTER(bdb->next_ptr);
                }

                bdb->next_ptr = tp->tx_bdb_head[i];
                bdb->trc_next_ptr = TRC_POINTER(tp->tx_bdb_head[i]);
                tp->tx_bdb_head[i]->back_ptr = bdb;
        }

        return (0);
}

static int smctr_init_tx_fcbs(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i, j;
        FCBlock *fcb;

        for(i = 0; i < NUM_TX_QS_USED; i++)
        {
                fcb               = tp->tx_fcb_head[i];
                fcb->frame_status = 0;
                fcb->frame_length = 0;
                fcb->info         = FCB_CHAIN_END;
                fcb->next_ptr = (FCBlock *)(((char *)fcb) + sizeof(FCBlock));
                fcb->trc_next_ptr = TRC_POINTER(fcb->next_ptr);

                for(j = 1; j < tp->num_tx_fcbs[i]; j++)
                {
                        fcb->next_ptr->back_ptr = fcb;
                        fcb                     = fcb->next_ptr;
                        fcb->frame_status       = 0;
                        fcb->frame_length       = 0;
                        fcb->info               = FCB_CHAIN_END;
                        fcb->next_ptr
                                = (FCBlock *)(((char *)fcb) + sizeof(FCBlock));
                        fcb->trc_next_ptr = TRC_POINTER(fcb->next_ptr);
                }

                fcb->next_ptr           = tp->tx_fcb_head[i];
                fcb->trc_next_ptr       = TRC_POINTER(tp->tx_fcb_head[i]);

                tp->tx_fcb_head[i]->back_ptr    = fcb;
                tp->tx_fcb_end[i]               = tp->tx_fcb_head[i]->next_ptr;
                tp->tx_fcb_curr[i]              = tp->tx_fcb_head[i]->next_ptr;
                tp->num_tx_fcbs_used[i]         = 0;
        }

        return (0);
}

static int smctr_internal_self_test(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int err;

        if((err = smctr_issue_test_internal_rom_cmd(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        if(tp->acb_head->cmd_done_status & 0xff)
                return (-1);

        if((err = smctr_issue_test_hic_cmd(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        if(tp->acb_head->cmd_done_status & 0xff)
                return (-1);

        if((err = smctr_issue_test_mac_reg_cmd(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        if(tp->acb_head->cmd_done_status & 0xff)
                return (-1);

        return (0);
}

/*
 * The typical workload of the driver: Handle the network interface interrupts.
 */
static irqreturn_t smctr_interrupt(int irq, void *dev_id)
{
        struct net_device *dev = dev_id;
        struct net_local *tp;
        int ioaddr;
        __u16 interrupt_unmask_bits = 0, interrupt_ack_code = 0xff00;
        __u16 err1, err = NOT_MY_INTERRUPT;
        __u8 isb_type, isb_subtype;
        __u16 isb_index;

        ioaddr = dev->base_addr;
        tp = netdev_priv(dev);

        if(tp->status == NOT_INITIALIZED)
                return IRQ_NONE;

        spin_lock(&tp->lock);
        
        smctr_disable_bic_int(dev);
        smctr_enable_16bit(dev);

        smctr_clear_int(dev);

        /* First read the LSB */
        while((tp->isb_ptr->IStatus[tp->current_isb_index].IType & 0xf0) == 0)
        {
                isb_index       = tp->current_isb_index;
                isb_type        = tp->isb_ptr->IStatus[isb_index].IType;
                isb_subtype     = tp->isb_ptr->IStatus[isb_index].ISubtype;

                (tp->current_isb_index)++;
                if(tp->current_isb_index == NUM_OF_INTERRUPTS)
                        tp->current_isb_index = 0;

                if(isb_type >= 0x10)
                {
                        smctr_disable_16bit(dev);
		        spin_unlock(&tp->lock);
                        return IRQ_HANDLED;
                }

                err = HARDWARE_FAILED;
                interrupt_ack_code = isb_index;
                tp->isb_ptr->IStatus[isb_index].IType |= 0xf0;

                interrupt_unmask_bits |= (1 << (__u16)isb_type);

                switch(isb_type)
                {
                        case ISB_IMC_MAC_TYPE_3:
                                smctr_disable_16bit(dev);

                                switch(isb_subtype)
                                {
                                        case 0:
                                                tp->monitor_state = MS_MONITOR_FSM_INACTIVE;
                                               break;

                                        case 1:
                                                tp->monitor_state = MS_REPEAT_BEACON_STATE;
                                                break;

                                        case 2:
                                                tp->monitor_state = MS_REPEAT_CLAIM_TOKEN_STATE;
                                                break;

                                        case 3:
                                                tp->monitor_state = MS_TRANSMIT_CLAIM_TOKEN_STATE;                                                break;

                                        case 4:
                                                tp->monitor_state = MS_STANDBY_MONITOR_STATE;
                                                break;

                                        case 5:
                                                tp->monitor_state = MS_TRANSMIT_BEACON_STATE;
                                                break;

                                        case 6:
                                                tp->monitor_state = MS_ACTIVE_MONITOR_STATE;
                                                break;

                                        case 7:
                                                tp->monitor_state = MS_TRANSMIT_RING_PURGE_STATE;
                                                break;

                                        case 8:   /* diagnostic state */
                                                break;

                                        case 9:
                                                tp->monitor_state = MS_BEACON_TEST_STATE;
                                                if(smctr_lobe_media_test(dev))
                                                {
                                                        tp->ring_status_flags = RING_STATUS_CHANGED;
                                                        tp->ring_status = AUTO_REMOVAL_ERROR;
                                                        smctr_ring_status_chg(dev);
                                                        smctr_bypass_state(dev);
                                                }
                                                else
                                                        smctr_issue_insert_cmd(dev);
                                                break;

                                        /* case 0x0a-0xff, illegal states */
                                        default:
                                                break;
                                }

                                tp->ring_status_flags = MONITOR_STATE_CHANGED;
                                err = smctr_ring_status_chg(dev);

                                smctr_enable_16bit(dev);
                                break;

                        /* Type 0x02 - MAC Error Counters Interrupt
                         * One or more MAC Error Counter is half full
                         *      MAC Error Counters
                         *      Lost_FR_Error_Counter
                         *      RCV_Congestion_Counter
                         *      FR_copied_Error_Counter
                         *      FREQ_Error_Counter
                         *      Token_Error_Counter
                         *      Line_Error_Counter
                         *      Internal_Error_Count
                         */
                        case ISB_IMC_MAC_ERROR_COUNTERS:
                                /* Read 802.5 Error Counters */
                                err = smctr_issue_read_ring_status_cmd(dev);
                                break;

                        /* Type 0x04 - MAC Type 2 Interrupt
                         * HOST needs to enqueue MAC Frame for transmission
                         * SubType Bit 15 - RQ_INIT_PDU( Request Initialization)                         * Changed from RQ_INIT_PDU to
                         * TRC_Status_Changed_Indicate
                         */
                        case ISB_IMC_MAC_TYPE_2:
                                err = smctr_issue_read_ring_status_cmd(dev);
                                break;


                        /* Type 0x05 - TX Frame Interrupt (FI). */
                        case ISB_IMC_TX_FRAME:
                                /* BUG QUEUE for TRC stuck receive BUG */
                                if(isb_subtype & TX_PENDING_PRIORITY_2)
                                {
                                        if((err = smctr_tx_complete(dev, BUG_QUEUE)) != SUCCESS)
                                                break;
                                }

                                /* NON-MAC frames only */
                                if(isb_subtype & TX_PENDING_PRIORITY_1)
                                {
                                        if((err = smctr_tx_complete(dev, NON_MAC_QUEUE)) != SUCCESS)
                                                break;
                                }

                                /* MAC frames only */
                                if(isb_subtype & TX_PENDING_PRIORITY_0)
                                        err = smctr_tx_complete(dev, MAC_QUEUE);                                break;

                        /* Type 0x06 - TX END OF QUEUE (FE) */
                        case ISB_IMC_END_OF_TX_QUEUE:
                                /* BUG queue */
                                if(isb_subtype & TX_PENDING_PRIORITY_2)
                                {
                                        /* ok to clear Receive FIFO overrun
                                         * imask send_BUG now completes.
                                         */
                                        interrupt_unmask_bits |= 0x800;

                                        tp->tx_queue_status[BUG_QUEUE] = NOT_TRANSMITING;
                                        if((err = smctr_tx_complete(dev, BUG_QUEUE)) != SUCCESS)
                                                break;
                                        if((err = smctr_restart_tx_chain(dev, BUG_QUEUE)) != SUCCESS)
                                                break;
                                }

                                /* NON-MAC queue only */
                                if(isb_subtype & TX_PENDING_PRIORITY_1)
                                {
                                        tp->tx_queue_status[NON_MAC_QUEUE] = NOT_TRANSMITING;
                                        if((err = smctr_tx_complete(dev, NON_MAC_QUEUE)) != SUCCESS)
                                                break;
                                        if((err = smctr_restart_tx_chain(dev, NON_MAC_QUEUE)) != SUCCESS)
                                                break;
                                }

                                /* MAC queue only */
                                if(isb_subtype & TX_PENDING_PRIORITY_0)
                                {
                                        tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
                                        if((err = smctr_tx_complete(dev, MAC_QUEUE)) != SUCCESS)
                                                break;

                                        err = smctr_restart_tx_chain(dev, MAC_QUEUE);
                                }
                                break;

                        /* Type 0x07 - NON-MAC RX Resource Interrupt
                         *   Subtype bit 12 - (BW) BDB warning
                         *   Subtype bit 13 - (FW) FCB warning
                         *   Subtype bit 14 - (BE) BDB End of chain
                         *   Subtype bit 15 - (FE) FCB End of chain
                         */
                        case ISB_IMC_NON_MAC_RX_RESOURCE:
                                tp->rx_fifo_overrun_count = 0;
                                tp->receive_queue_number = NON_MAC_QUEUE;
                                err1 = smctr_rx_frame(dev);

                                if(isb_subtype & NON_MAC_RX_RESOURCE_FE)
                                {
                                        if((err = smctr_issue_resume_rx_fcb_cmd(                                                dev, NON_MAC_QUEUE)) != SUCCESS)                                                break;

                                        if(tp->ptr_rx_fcb_overruns)
                                                (*tp->ptr_rx_fcb_overruns)++;
                                }

                                if(isb_subtype & NON_MAC_RX_RESOURCE_BE)
                                {
                                        if((err = smctr_issue_resume_rx_bdb_cmd(                                                dev, NON_MAC_QUEUE)) != SUCCESS)                                                break;

                                        if(tp->ptr_rx_bdb_overruns)
                                                (*tp->ptr_rx_bdb_overruns)++;
                                }
                                err = err1;
                                break;

                        /* Type 0x08 - MAC RX Resource Interrupt
                         *   Subtype bit 12 - (BW) BDB warning
                         *   Subtype bit 13 - (FW) FCB warning
                         *   Subtype bit 14 - (BE) BDB End of chain
                         *   Subtype bit 15 - (FE) FCB End of chain
                         */
                        case ISB_IMC_MAC_RX_RESOURCE:
                                tp->receive_queue_number = MAC_QUEUE;
                                err1 = smctr_rx_frame(dev);

                                if(isb_subtype & MAC_RX_RESOURCE_FE)
                                {
                                        if((err = smctr_issue_resume_rx_fcb_cmd(                                                dev, MAC_QUEUE)) != SUCCESS)
                                                break;

                                        if(tp->ptr_rx_fcb_overruns)
                                                (*tp->ptr_rx_fcb_overruns)++;
                                }

                                if(isb_subtype & MAC_RX_RESOURCE_BE)
                                {
                                        if((err = smctr_issue_resume_rx_bdb_cmd(                                                dev, MAC_QUEUE)) != SUCCESS)
                                                break;

                                        if(tp->ptr_rx_bdb_overruns)
                                                (*tp->ptr_rx_bdb_overruns)++;
                                }
                                err = err1;
                                break;

                        /* Type 0x09 - NON_MAC RX Frame Interrupt */
                        case ISB_IMC_NON_MAC_RX_FRAME:
                                tp->rx_fifo_overrun_count = 0;
                                tp->receive_queue_number = NON_MAC_QUEUE;
                                err = smctr_rx_frame(dev);
                                break;

                        /* Type 0x0A - MAC RX Frame Interrupt */
                        case ISB_IMC_MAC_RX_FRAME:
                                tp->receive_queue_number = MAC_QUEUE;
                                err = smctr_rx_frame(dev);
                                break;

                        /* Type 0x0B - TRC status
                         * TRC has encountered an error condition
                         * subtype bit 14 - transmit FIFO underrun
                         * subtype bit 15 - receive FIFO overrun
                         */
                        case ISB_IMC_TRC_FIFO_STATUS:
                                if(isb_subtype & TRC_FIFO_STATUS_TX_UNDERRUN)
                                {
                                        if(tp->ptr_tx_fifo_underruns)
                                                (*tp->ptr_tx_fifo_underruns)++;
                                }

                                if(isb_subtype & TRC_FIFO_STATUS_RX_OVERRUN)
                                {
                                        /* update overrun stuck receive counter
                                         * if >= 3, has to clear it by sending
                                         * back to back frames. We pick
                                         * DAT(duplicate address MAC frame)
                                         */
                                        tp->rx_fifo_overrun_count++;

                                        if(tp->rx_fifo_overrun_count >= 3)
                                        {
                                                tp->rx_fifo_overrun_count = 0;

                                                /* delay clearing fifo overrun
                                                 * imask till send_BUG tx
                                                 * complete posted
                                                 */
                                                interrupt_unmask_bits &= (~0x800);
                                                printk(KERN_CRIT "Jay please send bug\n");//                                              smctr_send_bug(dev);
                                        }

                                        if(tp->ptr_rx_fifo_overruns)
                                                (*tp->ptr_rx_fifo_overruns)++;
                                }

                                err = SUCCESS;
                                break;

                        /* Type 0x0C - Action Command Status Interrupt
                         * Subtype bit 14 - CB end of command chain (CE)
                         * Subtype bit 15 - CB command interrupt (CI)
                         */
                        case ISB_IMC_COMMAND_STATUS:
                                err = SUCCESS;
                                if(tp->acb_head->cmd == ACB_CMD_HIC_NOP)
                                {
                                        printk(KERN_ERR "i1\n");
                                        smctr_disable_16bit(dev);

                                        /* XXXXXXXXXXXXXXXXX */
                                /*      err = UM_Interrupt(dev); */

                                        smctr_enable_16bit(dev);
                                }
                                else
                                {
                                        if((tp->acb_head->cmd
                                                == ACB_CMD_READ_TRC_STATUS)
                                                && (tp->acb_head->subcmd
                                                == RW_TRC_STATUS_BLOCK))
                                        {
                                                if(tp->ptr_bcn_type)
                                                {
                                                        *(tp->ptr_bcn_type)
                                                                = (__u32)((SBlock *)tp->misc_command_data)->BCN_Type;
                                                }

                                                if(((SBlock *)tp->misc_command_data)->Status_CHG_Indicate & ERROR_COUNTERS_CHANGED)
                                                {
                                                        smctr_update_err_stats(dev);
                                                }

                                                if(((SBlock *)tp->misc_command_data)->Status_CHG_Indicate & TI_NDIS_RING_STATUS_CHANGED)
                                                {
                                                        tp->ring_status
                                                                = ((SBlock*)tp->misc_command_data)->TI_NDIS_Ring_Status;
                                                        smctr_disable_16bit(dev);
                                                        err = smctr_ring_status_chg(dev);
                                                        smctr_enable_16bit(dev);
                                                        if((tp->ring_status & REMOVE_RECEIVED)
                                                                && (tp->config_word0 & NO_AUTOREMOVE))
                                                        {
                                                                smctr_issue_remove_cmd(dev);
                                                        }

                                                        if(err != SUCCESS)
                                                        {
                                                                tp->acb_pending = 0;
                                                                break;
                                                        }
                                                }

                                                if(((SBlock *)tp->misc_command_data)->Status_CHG_Indicate & UNA_CHANGED)
                                                {
                                                        if(tp->ptr_una)
                                                        {
                                                                tp->ptr_una[0] = SWAP_BYTES(((SBlock *)tp->misc_command_data)->UNA[0]);
                                                                tp->ptr_una[1] = SWAP_BYTES(((SBlock *)tp->misc_command_data)->UNA[1]);
                                                                tp->ptr_una[2] = SWAP_BYTES(((SBlock *)tp->misc_command_data)->UNA[2]);
                                                        }

                                                }

                                                if(((SBlock *)tp->misc_command_data)->Status_CHG_Indicate & READY_TO_SEND_RQ_INIT)                                                {
                                                        err = smctr_send_rq_init(dev);
                                                }
                                        }
                                }

                                tp->acb_pending = 0;
                                break;

                        /* Type 0x0D - MAC Type 1 interrupt
                         * Subtype -- 00 FR_BCN received at S12
                         *            01 FR_BCN received at S21
                         *            02 FR_DAT(DA=MA, A<>0) received at S21
                         *            03 TSM_EXP at S21
                         *            04 FR_REMOVE received at S42
                         *            05 TBR_EXP, BR_FLAG_SET at S42
                         *            06 TBT_EXP at S53
                         */
                        case ISB_IMC_MAC_TYPE_1:
                                if(isb_subtype > 8)
                                {
                                        err = HARDWARE_FAILED;
                                        break;
                                }

                                err = SUCCESS;
                                switch(isb_subtype)
                                {
                                        case 0:
                                                tp->join_state = JS_BYPASS_STATE;
                                                if(tp->status != CLOSED)
                                                {
                                                        tp->status = CLOSED;
                                                        err = smctr_status_chg(dev);
                                                }
                                                break;

                                        case 1:
                                                tp->join_state = JS_LOBE_TEST_STATE;
                                                break;

                                        case 2:
                                                tp->join_state = JS_DETECT_MONITOR_PRESENT_STATE;
                                                break;

                                        case 3:
                                                tp->join_state = JS_AWAIT_NEW_MONITOR_STATE;
                                                break;

                                        case 4:
                                                tp->join_state = JS_DUPLICATE_ADDRESS_TEST_STATE;
                                                break;

                                        case 5:
                                                tp->join_state = JS_NEIGHBOR_NOTIFICATION_STATE;
                                                break;

                                        case 6:
                                                tp->join_state = JS_REQUEST_INITIALIZATION_STATE;
                                                break;

                                        case 7:
                                                tp->join_state = JS_JOIN_COMPLETE_STATE;
                                                tp->status = OPEN;
                                                err = smctr_status_chg(dev);
                                                break;

                                        case 8:
                                                tp->join_state = JS_BYPASS_WAIT_STATE;
                                                break;
                                }
                                break ;

                        /* Type 0x0E - TRC Initialization Sequence Interrupt
                         * Subtype -- 00-FF Initializatin sequence complete
                         */
                        case ISB_IMC_TRC_INTRNL_TST_STATUS:
                                tp->status = INITIALIZED;
                                smctr_disable_16bit(dev);
                                err = smctr_status_chg(dev);
                                smctr_enable_16bit(dev);
                                break;

                        /* other interrupt types, illegal */
                        default:
                                break;
                }

                if(err != SUCCESS)
                        break;
        }

        /* Checking the ack code instead of the unmask bits here is because :
         * while fixing the stuck receive, DAT frame are sent and mask off
         * FIFO overrun interrupt temporarily (interrupt_unmask_bits = 0)
         * but we still want to issue ack to ISB
         */
        if(!(interrupt_ack_code & 0xff00))
                smctr_issue_int_ack(dev, interrupt_ack_code, interrupt_unmask_bits);

        smctr_disable_16bit(dev);
        smctr_enable_bic_int(dev);
        spin_unlock(&tp->lock);

        return IRQ_HANDLED;
}

static int smctr_issue_enable_int_cmd(struct net_device *dev,
        __u16 interrupt_enable_mask)
{
        struct net_local *tp = netdev_priv(dev);
        int err;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        tp->sclb_ptr->int_mask_control  = interrupt_enable_mask;
        tp->sclb_ptr->valid_command     = SCLB_VALID | SCLB_CMD_CLEAR_INTERRUPT_MASK;

        smctr_set_ctrl_attention(dev);

        return (0);
}

static int smctr_issue_int_ack(struct net_device *dev, __u16 iack_code, __u16 ibits)
{
        struct net_local *tp = netdev_priv(dev);

        if(smctr_wait_while_cbusy(dev))
                return (-1);

        tp->sclb_ptr->int_mask_control = ibits;
        tp->sclb_ptr->iack_code = iack_code << 1; /* use the offset from base */        tp->sclb_ptr->resume_control = 0;
        tp->sclb_ptr->valid_command = SCLB_VALID | SCLB_IACK_CODE_VALID | SCLB_CMD_CLEAR_INTERRUPT_MASK;

        smctr_set_ctrl_attention(dev);

        return (0);
}

static int smctr_issue_init_timers_cmd(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i;
        int err;
        __u16 *pTimer_Struc = (__u16 *)tp->misc_command_data;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        tp->config_word0 = THDREN | DMA_TRIGGER | USETPT | NO_AUTOREMOVE;
        tp->config_word1 = 0;

        if((tp->media_type == MEDIA_STP_16)
                || (tp->media_type == MEDIA_UTP_16)
                || (tp->media_type == MEDIA_STP_16_UTP_16))
        {
                tp->config_word0 |= FREQ_16MB_BIT;
        }

        if(tp->mode_bits & EARLY_TOKEN_REL)
                tp->config_word0 |= ETREN;

        if(tp->mode_bits & LOOPING_MODE_MASK)
                tp->config_word0 |= RX_OWN_BIT;
        else
                tp->config_word0 &= ~RX_OWN_BIT;

        if(tp->receive_mask & PROMISCUOUS_MODE)
                tp->config_word0 |= PROMISCUOUS_BIT;
        else
                tp->config_word0 &= ~PROMISCUOUS_BIT;

        if(tp->receive_mask & ACCEPT_ERR_PACKETS)
                tp->config_word0 |= SAVBAD_BIT;
        else
                tp->config_word0 &= ~SAVBAD_BIT;

        if(tp->receive_mask & ACCEPT_ATT_MAC_FRAMES)
                tp->config_word0 |= RXATMAC;
        else
                tp->config_word0 &= ~RXATMAC;

        if(tp->receive_mask & ACCEPT_MULTI_PROM)
                tp->config_word1 |= MULTICAST_ADDRESS_BIT;
        else
                tp->config_word1 &= ~MULTICAST_ADDRESS_BIT;

        if(tp->receive_mask & ACCEPT_SOURCE_ROUTING_SPANNING)
                tp->config_word1 |= SOURCE_ROUTING_SPANNING_BITS;
        else
        {
                if(tp->receive_mask & ACCEPT_SOURCE_ROUTING)
                        tp->config_word1 |= SOURCE_ROUTING_EXPLORER_BIT;
                else
                        tp->config_word1 &= ~SOURCE_ROUTING_SPANNING_BITS;
        }

        if((tp->media_type == MEDIA_STP_16)
                || (tp->media_type == MEDIA_UTP_16)
                || (tp->media_type == MEDIA_STP_16_UTP_16))
        {
                tp->config_word1 |= INTERFRAME_SPACING_16;
        }
        else
                tp->config_word1 |= INTERFRAME_SPACING_4;

        *pTimer_Struc++ = tp->config_word0;
        *pTimer_Struc++ = tp->config_word1;

        if((tp->media_type == MEDIA_STP_4)
                || (tp->media_type == MEDIA_UTP_4)
                || (tp->media_type == MEDIA_STP_4_UTP_4))
        {
                *pTimer_Struc++ = 0x00FA;       /* prescale */
                *pTimer_Struc++ = 0x2710;       /* TPT_limit */
                *pTimer_Struc++ = 0x2710;       /* TQP_limit */
                *pTimer_Struc++ = 0x0A28;       /* TNT_limit */
                *pTimer_Struc++ = 0x3E80;       /* TBT_limit */
                *pTimer_Struc++ = 0x3A98;       /* TSM_limit */
                *pTimer_Struc++ = 0x1B58;       /* TAM_limit */
                *pTimer_Struc++ = 0x00C8;       /* TBR_limit */
                *pTimer_Struc++ = 0x07D0;       /* TER_limit */
                *pTimer_Struc++ = 0x000A;       /* TGT_limit */
                *pTimer_Struc++ = 0x1162;       /* THT_limit */
                *pTimer_Struc++ = 0x07D0;       /* TRR_limit */
                *pTimer_Struc++ = 0x1388;       /* TVX_limit */
                *pTimer_Struc++ = 0x0000;       /* reserved */
        }
        else
        {
                *pTimer_Struc++ = 0x03E8;       /* prescale */
                *pTimer_Struc++ = 0x9C40;       /* TPT_limit */
                *pTimer_Struc++ = 0x9C40;       /* TQP_limit */
                *pTimer_Struc++ = 0x0A28;       /* TNT_limit */
                *pTimer_Struc++ = 0x3E80;       /* TBT_limit */
                *pTimer_Struc++ = 0x3A98;       /* TSM_limit */
                *pTimer_Struc++ = 0x1B58;       /* TAM_limit */
                *pTimer_Struc++ = 0x00C8;       /* TBR_limit */
                *pTimer_Struc++ = 0x07D0;       /* TER_limit */
                *pTimer_Struc++ = 0x000A;       /* TGT_limit */
                *pTimer_Struc++ = 0x4588;       /* THT_limit */
                *pTimer_Struc++ = 0x1F40;       /* TRR_limit */
                *pTimer_Struc++ = 0x4E20;       /* TVX_limit */
                *pTimer_Struc++ = 0x0000;       /* reserved */
        }

        /* Set node address. */
        *pTimer_Struc++ = dev->dev_addr[0] << 8
                | (dev->dev_addr[1] & 0xFF);
        *pTimer_Struc++ = dev->dev_addr[2] << 8
                | (dev->dev_addr[3] & 0xFF);
        *pTimer_Struc++ = dev->dev_addr[4] << 8
                | (dev->dev_addr[5] & 0xFF);

        /* Set group address. */
        *pTimer_Struc++ = tp->group_address_0 << 8
                | tp->group_address_0 >> 8;
        *pTimer_Struc++ = tp->group_address[0] << 8
                | tp->group_address[0] >> 8;
        *pTimer_Struc++ = tp->group_address[1] << 8
                | tp->group_address[1] >> 8;

        /* Set functional address. */
        *pTimer_Struc++ = tp->functional_address_0 << 8
                | tp->functional_address_0 >> 8;
        *pTimer_Struc++ = tp->functional_address[0] << 8
                | tp->functional_address[0] >> 8;
        *pTimer_Struc++ = tp->functional_address[1] << 8
                | tp->functional_address[1] >> 8;

        /* Set Bit-Wise group address. */
        *pTimer_Struc++ = tp->bitwise_group_address[0] << 8
                | tp->bitwise_group_address[0] >> 8;
        *pTimer_Struc++ = tp->bitwise_group_address[1] << 8
                | tp->bitwise_group_address[1] >> 8;

        /* Set ring number address. */
        *pTimer_Struc++ = tp->source_ring_number;
        *pTimer_Struc++ = tp->target_ring_number;

        /* Physical drop number. */
        *pTimer_Struc++ = (unsigned short)0;
        *pTimer_Struc++ = (unsigned short)0;

        /* Product instance ID. */
        for(i = 0; i < 9; i++)
                *pTimer_Struc++ = (unsigned short)0;

        err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_INIT_TRC_TIMERS, 0);

        return (err);
}

static int smctr_issue_init_txrx_cmd(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i;
        int err;
        void **txrx_ptrs = (void *)tp->misc_command_data;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
	{
                printk(KERN_ERR "%s: Hardware failure\n", dev->name);
                return (err);
        }

        /* Initialize Transmit Queue Pointers that are used, to point to
         * a single FCB.
         */
        for(i = 0; i < NUM_TX_QS_USED; i++)
                *txrx_ptrs++ = (void *)TRC_POINTER(tp->tx_fcb_head[i]);

        /* Initialize Transmit Queue Pointers that are NOT used to ZERO. */
        for(; i < MAX_TX_QS; i++)
                *txrx_ptrs++ = (void *)0;

        /* Initialize Receive Queue Pointers (MAC and Non-MAC) that are
         * used, to point to a single FCB and a BDB chain of buffers.
         */
        for(i = 0; i < NUM_RX_QS_USED; i++)
        {
                *txrx_ptrs++ = (void *)TRC_POINTER(tp->rx_fcb_head[i]);
                *txrx_ptrs++ = (void *)TRC_POINTER(tp->rx_bdb_head[i]);
        }

        /* Initialize Receive Queue Pointers that are NOT used to ZERO. */
        for(; i < MAX_RX_QS; i++)
        {
                *txrx_ptrs++ = (void *)0;
                *txrx_ptrs++ = (void *)0;
        }

        err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_INIT_TX_RX, 0);

        return (err);
}

static int smctr_issue_insert_cmd(struct net_device *dev)
{
        int err;

        err = smctr_setup_single_cmd(dev, ACB_CMD_INSERT, ACB_SUB_CMD_NOP);

        return (err);
}

static int smctr_issue_read_ring_status_cmd(struct net_device *dev)
{
        int err;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_READ_TRC_STATUS,
                RW_TRC_STATUS_BLOCK);

        return (err);
}

static int smctr_issue_read_word_cmd(struct net_device *dev, __u16 aword_cnt)
{
        int err;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_MCT_READ_VALUE,
                aword_cnt);

        return (err);
}

static int smctr_issue_remove_cmd(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int err;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        tp->sclb_ptr->resume_control    = 0;
        tp->sclb_ptr->valid_command     = SCLB_VALID | SCLB_CMD_REMOVE;

        smctr_set_ctrl_attention(dev);

        return (0);
}

static int smctr_issue_resume_acb_cmd(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int err;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        tp->sclb_ptr->resume_control = SCLB_RC_ACB;
        tp->sclb_ptr->valid_command  = SCLB_VALID | SCLB_RESUME_CONTROL_VALID;

        tp->acb_pending = 1;

        smctr_set_ctrl_attention(dev);

        return (0);
}

static int smctr_issue_resume_rx_bdb_cmd(struct net_device *dev, __u16 queue)
{
        struct net_local *tp = netdev_priv(dev);
        int err;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        if(queue == MAC_QUEUE)
                tp->sclb_ptr->resume_control = SCLB_RC_RX_MAC_BDB;
        else
                tp->sclb_ptr->resume_control = SCLB_RC_RX_NON_MAC_BDB;

        tp->sclb_ptr->valid_command = SCLB_VALID | SCLB_RESUME_CONTROL_VALID;

        smctr_set_ctrl_attention(dev);

        return (0);
}

static int smctr_issue_resume_rx_fcb_cmd(struct net_device *dev, __u16 queue)
{
        struct net_local *tp = netdev_priv(dev);

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_issue_resume_rx_fcb_cmd\n", dev->name);

        if(smctr_wait_while_cbusy(dev))
                return (-1);

        if(queue == MAC_QUEUE)
                tp->sclb_ptr->resume_control = SCLB_RC_RX_MAC_FCB;
        else
                tp->sclb_ptr->resume_control = SCLB_RC_RX_NON_MAC_FCB;

        tp->sclb_ptr->valid_command = SCLB_VALID | SCLB_RESUME_CONTROL_VALID;

        smctr_set_ctrl_attention(dev);

        return (0);
}

static int smctr_issue_resume_tx_fcb_cmd(struct net_device *dev, __u16 queue)
{
        struct net_local *tp = netdev_priv(dev);

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_issue_resume_tx_fcb_cmd\n", dev->name);

        if(smctr_wait_while_cbusy(dev))
                return (-1);

        tp->sclb_ptr->resume_control = (SCLB_RC_TFCB0 << queue);
        tp->sclb_ptr->valid_command = SCLB_RESUME_CONTROL_VALID | SCLB_VALID;

        smctr_set_ctrl_attention(dev);

        return (0);
}

static int smctr_issue_test_internal_rom_cmd(struct net_device *dev)
{
        int err;

        err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
                TRC_INTERNAL_ROM_TEST);

        return (err);
}

static int smctr_issue_test_hic_cmd(struct net_device *dev)
{
        int err;

        err = smctr_setup_single_cmd(dev, ACB_CMD_HIC_TEST,
                TRC_HOST_INTERFACE_REG_TEST);

        return (err);
}

static int smctr_issue_test_mac_reg_cmd(struct net_device *dev)
{
        int err;

        err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
                TRC_MAC_REGISTERS_TEST);

        return (err);
}

static int smctr_issue_trc_loopback_cmd(struct net_device *dev)
{
        int err;

        err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
                TRC_INTERNAL_LOOPBACK);

        return (err);
}

static int smctr_issue_tri_loopback_cmd(struct net_device *dev)
{
        int err;

        err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
                TRC_TRI_LOOPBACK);

        return (err);
}

static int smctr_issue_write_byte_cmd(struct net_device *dev,
        short aword_cnt, void *byte)
{
	struct net_local *tp = netdev_priv(dev);
        unsigned int iword, ibyte;
	int err;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        for(iword = 0, ibyte = 0; iword < (unsigned int)(aword_cnt & 0xff);
        	iword++, ibyte += 2)
        {
                tp->misc_command_data[iword] = (*((__u8 *)byte + ibyte) << 8)
			| (*((__u8 *)byte + ibyte + 1));
        }

        return (smctr_setup_single_cmd_w_data(dev, ACB_CMD_MCT_WRITE_VALUE, 
		aword_cnt));
}

static int smctr_issue_write_word_cmd(struct net_device *dev,
        short aword_cnt, void *word)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i, err;

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        for(i = 0; i < (unsigned int)(aword_cnt & 0xff); i++)
                tp->misc_command_data[i] = *((__u16 *)word + i);

        err = smctr_setup_single_cmd_w_data(dev, ACB_CMD_MCT_WRITE_VALUE,
                aword_cnt);

        return (err);
}

static int smctr_join_complete_state(struct net_device *dev)
{
        int err;

        err = smctr_setup_single_cmd(dev, ACB_CMD_CHANGE_JOIN_STATE,
                JS_JOIN_COMPLETE_STATE);

        return (err);
}

static int smctr_link_tx_fcbs_to_bdbs(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i, j;
        FCBlock *fcb;
        BDBlock *bdb;

        for(i = 0; i < NUM_TX_QS_USED; i++)
        {
                fcb = tp->tx_fcb_head[i];
                bdb = tp->tx_bdb_head[i];

                for(j = 0; j < tp->num_tx_fcbs[i]; j++)
                {
                        fcb->bdb_ptr            = bdb;
                        fcb->trc_bdb_ptr        = TRC_POINTER(bdb);
                        fcb = (FCBlock *)((char *)fcb + sizeof(FCBlock));
                        bdb = (BDBlock *)((char *)bdb + sizeof(BDBlock));
                }
        }

        return (0);
}

static int smctr_load_firmware(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
	const struct firmware *fw;
        __u16 i, checksum = 0;
        int err = 0;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_load_firmware\n", dev->name);

	if (request_firmware(&fw, "tr_smctr.bin", &dev->dev)) {
		printk(KERN_ERR "%s: firmware not found\n", dev->name);
		return (UCODE_NOT_PRESENT);
	}

        tp->num_of_tx_buffs     = 4;
        tp->mode_bits          |= UMAC;
        tp->receive_mask        = 0;
        tp->max_packet_size     = 4177;

        /* Can only upload the firmware once per adapter reset. */
        if (tp->microcode_version != 0) {
		err = (UCODE_PRESENT);
		goto out;
	}

        /* Verify the firmware exists and is there in the right amount. */
        if (!fw->data
                || (*(fw->data + UCODE_VERSION_OFFSET) < UCODE_VERSION))
        {
                err = (UCODE_NOT_PRESENT);
		goto out;
        }

        /* UCODE_SIZE is not included in Checksum. */
        for(i = 0; i < *((__u16 *)(fw->data + UCODE_SIZE_OFFSET)); i += 2)
                checksum += *((__u16 *)(fw->data + 2 + i));
        if (checksum) {
		err = (UCODE_NOT_PRESENT);
		goto out;
	}

        /* At this point we have a valid firmware image, lets kick it on up. */
        smctr_enable_adapter_ram(dev);
        smctr_enable_16bit(dev);
        smctr_set_page(dev, (__u8 *)tp->ram_access);

        if((smctr_checksum_firmware(dev))
                || (*(fw->data + UCODE_VERSION_OFFSET)
                > tp->microcode_version))
        {
                smctr_enable_adapter_ctrl_store(dev);

                /* Zero out ram space for firmware. */
                for(i = 0; i < CS_RAM_SIZE; i += 2)
                        *((__u16 *)(tp->ram_access + i)) = 0;

                smctr_decode_firmware(dev, fw);

                tp->microcode_version = *(fw->data + UCODE_VERSION_OFFSET);                *((__u16 *)(tp->ram_access + CS_RAM_VERSION_OFFSET))
                        = (tp->microcode_version << 8);
                *((__u16 *)(tp->ram_access + CS_RAM_CHECKSUM_OFFSET))
                        = ~(tp->microcode_version << 8) + 1;

                smctr_disable_adapter_ctrl_store(dev);

                if(smctr_checksum_firmware(dev))
                        err = HARDWARE_FAILED;
        }
        else
                err = UCODE_PRESENT;

        smctr_disable_16bit(dev);
 out:
	release_firmware(fw);
        return (err);
}

static int smctr_load_node_addr(struct net_device *dev)
{
        int ioaddr = dev->base_addr;
        unsigned int i;
        __u8 r;

        for(i = 0; i < 6; i++)
        {
                r = inb(ioaddr + LAR0 + i);
                dev->dev_addr[i] = (char)r;
        }
        dev->addr_len = 6;

        return (0);
}

/* Lobe Media Test.
 * During the transmission of the initial 1500 lobe media MAC frames,
 * the phase lock loop in the 805 chip may lock, and then un-lock, causing
 * the 825 to go into a PURGE state. When performing a PURGE, the MCT
 * microcode will not transmit any frames given to it by the host, and
 * will consequently cause a timeout.
 *
 * NOTE 1: If the monitor_state is MS_BEACON_TEST_STATE, all transmit
 * queues other than the one used for the lobe_media_test should be
 * disabled.!?
 *
 * NOTE 2: If the monitor_state is MS_BEACON_TEST_STATE and the receive_mask
 * has any multi-cast or promiscous bits set, the receive_mask needs to
 * be changed to clear the multi-cast or promiscous mode bits, the lobe_test
 * run, and then the receive mask set back to its original value if the test
 * is successful.
 */
static int smctr_lobe_media_test(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i, perror = 0;
        unsigned short saved_rcv_mask;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_lobe_media_test\n", dev->name);

        /* Clear receive mask for lobe test. */
        saved_rcv_mask          = tp->receive_mask;
        tp->receive_mask        = 0;

        smctr_chg_rx_mask(dev);

        /* Setup the lobe media test. */
        smctr_lobe_media_test_cmd(dev);
        if(smctr_wait_cmd(dev))
		goto err;

        /* Tx lobe media test frames. */
        for(i = 0; i < 1500; ++i)
        {
                if(smctr_send_lobe_media_test(dev))
                {
                        if(perror)
				goto err;
                        else
                        {
                                perror = 1;
                                if(smctr_lobe_media_test_cmd(dev))
					goto err;
                        }
                }
        }

        if(smctr_send_dat(dev))
        {
                if(smctr_send_dat(dev))
			goto err;
        }

        /* Check if any frames received during test. */
        if((tp->rx_fcb_curr[MAC_QUEUE]->frame_status)
                || (tp->rx_fcb_curr[NON_MAC_QUEUE]->frame_status))
			goto err;

        /* Set receive mask to "Promisc" mode. */
        tp->receive_mask = saved_rcv_mask;

        smctr_chg_rx_mask(dev);

	 return 0;
err:
	smctr_reset_adapter(dev);
	tp->status = CLOSED;
	return LOBE_MEDIA_TEST_FAILED;
}

static int smctr_lobe_media_test_cmd(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int err;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_lobe_media_test_cmd\n", dev->name);

        /* Change to lobe media test state. */
        if(tp->monitor_state != MS_BEACON_TEST_STATE)
        {
                smctr_lobe_media_test_state(dev);
                if(smctr_wait_cmd(dev))
                {
                        printk(KERN_ERR "Lobe Failed test state\n");
                        return (LOBE_MEDIA_TEST_FAILED);
                }
        }

        err = smctr_setup_single_cmd(dev, ACB_CMD_MCT_TEST,
                TRC_LOBE_MEDIA_TEST);

        return (err);
}

static int smctr_lobe_media_test_state(struct net_device *dev)
{
        int err;

        err = smctr_setup_single_cmd(dev, ACB_CMD_CHANGE_JOIN_STATE,
                JS_LOBE_TEST_STATE);

        return (err);
}

static int smctr_make_8025_hdr(struct net_device *dev,
        MAC_HEADER *rmf, MAC_HEADER *tmf, __u16 ac_fc)
{
        tmf->ac = MSB(ac_fc);                 /* msb is access control */
        tmf->fc = LSB(ac_fc);                 /* lsb is frame control */

        tmf->sa[0] = dev->dev_addr[0];
        tmf->sa[1] = dev->dev_addr[1];
        tmf->sa[2] = dev->dev_addr[2];
        tmf->sa[3] = dev->dev_addr[3];
        tmf->sa[4] = dev->dev_addr[4];
        tmf->sa[5] = dev->dev_addr[5];

        switch(tmf->vc)
        {
		/* Send RQ_INIT to RPS */
                case RQ_INIT:
                        tmf->da[0] = 0xc0;
                        tmf->da[1] = 0x00;
                        tmf->da[2] = 0x00;
                        tmf->da[3] = 0x00;
                        tmf->da[4] = 0x00;
                        tmf->da[5] = 0x02;
                        break;

		/* Send RPT_TX_FORWARD to CRS */
                case RPT_TX_FORWARD:
                        tmf->da[0] = 0xc0;
                        tmf->da[1] = 0x00;
                        tmf->da[2] = 0x00;
                        tmf->da[3] = 0x00;
                        tmf->da[4] = 0x00;
                        tmf->da[5] = 0x10;
                        break;

		/* Everything else goes to sender */
                default:
                        tmf->da[0] = rmf->sa[0];
                        tmf->da[1] = rmf->sa[1];
                        tmf->da[2] = rmf->sa[2];
                        tmf->da[3] = rmf->sa[3];
                        tmf->da[4] = rmf->sa[4];
                        tmf->da[5] = rmf->sa[5];
                        break;
        }

        return (0);
}

static int smctr_make_access_pri(struct net_device *dev, MAC_SUB_VECTOR *tsv)
{
        struct net_local *tp = netdev_priv(dev);

        tsv->svi = AUTHORIZED_ACCESS_PRIORITY;
        tsv->svl = S_AUTHORIZED_ACCESS_PRIORITY;

        tsv->svv[0] = MSB(tp->authorized_access_priority);
        tsv->svv[1] = LSB(tp->authorized_access_priority);

	return (0);
}

static int smctr_make_addr_mod(struct net_device *dev, MAC_SUB_VECTOR *tsv)
{
        tsv->svi = ADDRESS_MODIFER;
        tsv->svl = S_ADDRESS_MODIFER;

        tsv->svv[0] = 0;
        tsv->svv[1] = 0;

        return (0);
}

static int smctr_make_auth_funct_class(struct net_device *dev,
        MAC_SUB_VECTOR *tsv)
{
        struct net_local *tp = netdev_priv(dev);

        tsv->svi = AUTHORIZED_FUNCTION_CLASS;
        tsv->svl = S_AUTHORIZED_FUNCTION_CLASS;

        tsv->svv[0] = MSB(tp->authorized_function_classes);
        tsv->svv[1] = LSB(tp->authorized_function_classes);

        return (0);
}

static int smctr_make_corr(struct net_device *dev,
        MAC_SUB_VECTOR *tsv, __u16 correlator)
{
        tsv->svi = CORRELATOR;
        tsv->svl = S_CORRELATOR;

        tsv->svv[0] = MSB(correlator);
        tsv->svv[1] = LSB(correlator);

        return (0);
}

static int smctr_make_funct_addr(struct net_device *dev, MAC_SUB_VECTOR *tsv)
{
        struct net_local *tp = netdev_priv(dev);

        smctr_get_functional_address(dev);

        tsv->svi = FUNCTIONAL_ADDRESS;
        tsv->svl = S_FUNCTIONAL_ADDRESS;

        tsv->svv[0] = MSB(tp->misc_command_data[0]);
        tsv->svv[1] = LSB(tp->misc_command_data[0]);

        tsv->svv[2] = MSB(tp->misc_command_data[1]);
        tsv->svv[3] = LSB(tp->misc_command_data[1]);

        return (0);
}

static int smctr_make_group_addr(struct net_device *dev, MAC_SUB_VECTOR *tsv)
{
        struct net_local *tp = netdev_priv(dev);

        smctr_get_group_address(dev);

        tsv->svi = GROUP_ADDRESS;
        tsv->svl = S_GROUP_ADDRESS;

        tsv->svv[0] = MSB(tp->misc_command_data[0]);
        tsv->svv[1] = LSB(tp->misc_command_data[0]);

        tsv->svv[2] = MSB(tp->misc_command_data[1]);
        tsv->svv[3] = LSB(tp->misc_command_data[1]);

        /* Set Group Address Sub-vector to all zeros if only the
         * Group Address/Functional Address Indicator is set.
         */
        if(tsv->svv[0] == 0x80 && tsv->svv[1] == 0x00
        	&& tsv->svv[2] == 0x00 && tsv->svv[3] == 0x00)
                tsv->svv[0] = 0x00;

        return (0);
}

static int smctr_make_phy_drop_num(struct net_device *dev,
        MAC_SUB_VECTOR *tsv)
{
        struct net_local *tp = netdev_priv(dev);

        smctr_get_physical_drop_number(dev);

        tsv->svi = PHYSICAL_DROP;
        tsv->svl = S_PHYSICAL_DROP;

        tsv->svv[0] = MSB(tp->misc_command_data[0]);
        tsv->svv[1] = LSB(tp->misc_command_data[0]);

        tsv->svv[2] = MSB(tp->misc_command_data[1]);
        tsv->svv[3] = LSB(tp->misc_command_data[1]);

        return (0);
}

static int smctr_make_product_id(struct net_device *dev, MAC_SUB_VECTOR *tsv)
{
        int i;

        tsv->svi = PRODUCT_INSTANCE_ID;
        tsv->svl = S_PRODUCT_INSTANCE_ID;

        for(i = 0; i < 18; i++)
                tsv->svv[i] = 0xF0;

        return (0);
}

static int smctr_make_station_id(struct net_device *dev, MAC_SUB_VECTOR *tsv)
{
        struct net_local *tp = netdev_priv(dev);

        smctr_get_station_id(dev);

        tsv->svi = STATION_IDENTIFER;
        tsv->svl = S_STATION_IDENTIFER;

        tsv->svv[0] = MSB(tp->misc_command_data[0]);
        tsv->svv[1] = LSB(tp->misc_command_data[0]);

        tsv->svv[2] = MSB(tp->misc_command_data[1]);
        tsv->svv[3] = LSB(tp->misc_command_data[1]);

        tsv->svv[4] = MSB(tp->misc_command_data[2]);
        tsv->svv[5] = LSB(tp->misc_command_data[2]);

        return (0);
}

static int smctr_make_ring_station_status(struct net_device *dev,
        MAC_SUB_VECTOR * tsv)
{
        tsv->svi = RING_STATION_STATUS;
        tsv->svl = S_RING_STATION_STATUS;

        tsv->svv[0] = 0;
        tsv->svv[1] = 0;
        tsv->svv[2] = 0;
        tsv->svv[3] = 0;
        tsv->svv[4] = 0;
        tsv->svv[5] = 0;

        return (0);
}

static int smctr_make_ring_station_version(struct net_device *dev,
        MAC_SUB_VECTOR *tsv)
{
        struct net_local *tp = netdev_priv(dev);

        tsv->svi = RING_STATION_VERSION_NUMBER;
        tsv->svl = S_RING_STATION_VERSION_NUMBER;

        tsv->svv[0] = 0xe2;            /* EBCDIC - S */
        tsv->svv[1] = 0xd4;            /* EBCDIC - M */
        tsv->svv[2] = 0xc3;            /* EBCDIC - C */
        tsv->svv[3] = 0x40;            /* EBCDIC -   */
        tsv->svv[4] = 0xe5;            /* EBCDIC - V */
        tsv->svv[5] = 0xF0 + (tp->microcode_version >> 4);
        tsv->svv[6] = 0xF0 + (tp->microcode_version & 0x0f);
        tsv->svv[7] = 0x40;            /* EBCDIC -   */
        tsv->svv[8] = 0xe7;            /* EBCDIC - X */

        if(tp->extra_info & CHIP_REV_MASK)
                tsv->svv[9] = 0xc5;    /* EBCDIC - E */
        else
                tsv->svv[9] = 0xc4;    /* EBCDIC - D */

        return (0);
}

static int smctr_make_tx_status_code(struct net_device *dev,
        MAC_SUB_VECTOR *tsv, __u16 tx_fstatus)
{
        tsv->svi = TRANSMIT_STATUS_CODE;
        tsv->svl = S_TRANSMIT_STATUS_CODE;

	tsv->svv[0] = ((tx_fstatus & 0x0100 >> 6) | IBM_PASS_SOURCE_ADDR);

        /* Stripped frame status of Transmitted Frame */
        tsv->svv[1] = tx_fstatus & 0xff;

        return (0);
}

static int smctr_make_upstream_neighbor_addr(struct net_device *dev,
        MAC_SUB_VECTOR *tsv)
{
        struct net_local *tp = netdev_priv(dev);

        smctr_get_upstream_neighbor_addr(dev);

        tsv->svi = UPSTREAM_NEIGHBOR_ADDRESS;
        tsv->svl = S_UPSTREAM_NEIGHBOR_ADDRESS;

        tsv->svv[0] = MSB(tp->misc_command_data[0]);
        tsv->svv[1] = LSB(tp->misc_command_data[0]);

        tsv->svv[2] = MSB(tp->misc_command_data[1]);
        tsv->svv[3] = LSB(tp->misc_command_data[1]);

        tsv->svv[4] = MSB(tp->misc_command_data[2]);
        tsv->svv[5] = LSB(tp->misc_command_data[2]);

        return (0);
}

static int smctr_make_wrap_data(struct net_device *dev, MAC_SUB_VECTOR *tsv)
{
        tsv->svi = WRAP_DATA;
        tsv->svl = S_WRAP_DATA;

        return (0);
}

/*
 * Open/initialize the board. This is called sometime after
 * booting when the 'ifconfig' program is run.
 *
 * This routine should set everything up anew at each open, even
 * registers that "should" only need to be set once at boot, so that
 * there is non-reboot way to recover if something goes wrong.
 */
static int smctr_open(struct net_device *dev)
{
        int err;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_open\n", dev->name);

        err = smctr_init_adapter(dev);
        if(err < 0)
                return (err);

        return (err);
}

/* Interrupt driven open of Token card. */
static int smctr_open_tr(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned long flags;
        int err;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_open_tr\n", dev->name);

        /* Now we can actually open the adapter. */
        if(tp->status == OPEN)
                return (0);
        if(tp->status != INITIALIZED)
                return (-1);

	/* FIXME: it would work a lot better if we masked the irq sources
	   on the card here, then we could skip the locking and poll nicely */
	spin_lock_irqsave(&tp->lock, flags);
	
        smctr_set_page(dev, (__u8 *)tp->ram_access);

        if((err = smctr_issue_resume_rx_fcb_cmd(dev, (short)MAC_QUEUE)))
                goto out;

        if((err = smctr_issue_resume_rx_bdb_cmd(dev, (short)MAC_QUEUE)))
                goto out;

        if((err = smctr_issue_resume_rx_fcb_cmd(dev, (short)NON_MAC_QUEUE)))
                goto out;

        if((err = smctr_issue_resume_rx_bdb_cmd(dev, (short)NON_MAC_QUEUE)))
                goto out;

        tp->status = CLOSED;

        /* Insert into the Ring or Enter Loopback Mode. */
        if((tp->mode_bits & LOOPING_MODE_MASK) == LOOPBACK_MODE_1)
        {
                tp->status = CLOSED;

                if(!(err = smctr_issue_trc_loopback_cmd(dev)))
                {
                        if(!(err = smctr_wait_cmd(dev)))
                                tp->status = OPEN;
                }

                smctr_status_chg(dev);
        }
        else
        {
                if((tp->mode_bits & LOOPING_MODE_MASK) == LOOPBACK_MODE_2)
                {
                        tp->status = CLOSED;
                        if(!(err = smctr_issue_tri_loopback_cmd(dev)))
                        {
                                if(!(err = smctr_wait_cmd(dev)))
                                        tp->status = OPEN;
                        }

                        smctr_status_chg(dev);
                }
                else
                {
                        if((tp->mode_bits & LOOPING_MODE_MASK)
                                == LOOPBACK_MODE_3)
                        {
                                tp->status = CLOSED;
                                if(!(err = smctr_lobe_media_test_cmd(dev)))
                                {
                                        if(!(err = smctr_wait_cmd(dev)))
                                                tp->status = OPEN;
                                }
                                smctr_status_chg(dev);
                        }
                        else
                        {
                                if(!(err = smctr_lobe_media_test(dev)))
                                        err = smctr_issue_insert_cmd(dev);
				else
                                {
                                        if(err == LOBE_MEDIA_TEST_FAILED)
                                                printk(KERN_WARNING "%s: Lobe Media Test Failure - Check cable?\n", dev->name);
                                }
                        }
                }
        }

out:
        spin_unlock_irqrestore(&tp->lock, flags);

        return (err);
}

/* Check for a network adapter of this type, 
 * and return device structure if one exists.
 */
struct net_device __init *smctr_probe(int unit)
{
	struct net_device *dev = alloc_trdev(sizeof(struct net_local));
	static const unsigned ports[] = {
		0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0, 0x300,
		0x320, 0x340, 0x360, 0x380, 0
	};
	const unsigned *port;
        int err = 0;

	if (!dev)
		return ERR_PTR(-ENOMEM);

	if (unit >= 0) {
		sprintf(dev->name, "tr%d", unit);
		netdev_boot_setup_check(dev);
	}

        if (dev->base_addr > 0x1ff)    /* Check a single specified location. */
		err = smctr_probe1(dev, dev->base_addr);
        else if(dev->base_addr != 0)  /* Don't probe at all. */
                err =-ENXIO;
	else {
		for (port = ports; *port; port++) {
			err = smctr_probe1(dev, *port);
			if (!err)
				break;
		}
	}
	if (err)
		goto out;
	err = register_netdev(dev);
	if (err)
		goto out1;
	return dev;
out1:
#ifdef CONFIG_MCA_LEGACY
	{ struct net_local *tp = netdev_priv(dev);
	  if (tp->slot_num)
		mca_mark_as_unused(tp->slot_num);
	}
#endif
	release_region(dev->base_addr, SMCTR_IO_EXTENT);
	free_irq(dev->irq, dev);
out:
	free_netdev(dev);
	return ERR_PTR(err);
}

static const struct net_device_ops smctr_netdev_ops = {
	.ndo_open          = smctr_open,
	.ndo_stop          = smctr_close,
	.ndo_start_xmit    = smctr_send_packet,
	.ndo_tx_timeout	   = smctr_timeout,
	.ndo_get_stats     = smctr_get_stats,
	.ndo_set_multicast_list = smctr_set_multicast_list,
};

static int __init smctr_probe1(struct net_device *dev, int ioaddr)
{
        static unsigned version_printed;
        struct net_local *tp = netdev_priv(dev);
        int err;
        __u32 *ram;

        if(smctr_debug && version_printed++ == 0)
                printk(version);

        spin_lock_init(&tp->lock);
        dev->base_addr = ioaddr;

	/* Actually detect an adapter now. */
        err = smctr_chk_isa(dev);
        if(err < 0)
        {
		if ((err = smctr_chk_mca(dev)) < 0) {
			err = -ENODEV;
			goto out;
		}
        }

        tp = netdev_priv(dev);
        dev->mem_start = tp->ram_base;
        dev->mem_end = dev->mem_start + 0x10000;
        ram = (__u32 *)phys_to_virt(dev->mem_start);
        tp->ram_access = *(__u32 *)&ram;
	tp->status = NOT_INITIALIZED;

        err = smctr_load_firmware(dev);
        if(err != UCODE_PRESENT && err != SUCCESS)
        {
                printk(KERN_ERR "%s: Firmware load failed (%d)\n", dev->name, err);
		err = -EIO;
		goto out;
        }

	/* Allow user to specify ring speed on module insert. */
	if(ringspeed == 4)
		tp->media_type = MEDIA_UTP_4;
	else
		tp->media_type = MEDIA_UTP_16;

        printk(KERN_INFO "%s: %s %s at Io %#4x, Irq %d, Rom %#4x, Ram %#4x.\n",
                dev->name, smctr_name, smctr_model,
                (unsigned int)dev->base_addr,
                dev->irq, tp->rom_base, tp->ram_base);

	dev->netdev_ops = &smctr_netdev_ops;
        dev->watchdog_timeo	= HZ;
        return (0);

out:
	return err;
}

static int smctr_process_rx_packet(MAC_HEADER *rmf, __u16 size,
        struct net_device *dev, __u16 rx_status)
{
        struct net_local *tp = netdev_priv(dev);
        struct sk_buff *skb;
        __u16 rcode, correlator;
        int err = 0;
        __u8 xframe = 1;

        rmf->vl = SWAP_BYTES(rmf->vl);
        if(rx_status & FCB_RX_STATUS_DA_MATCHED)
        {
                switch(rmf->vc)
                {
                        /* Received MAC Frames Processed by RS. */
                        case INIT:
                                if((rcode = smctr_rcv_init(dev, rmf, &correlator)) == HARDWARE_FAILED)
                                {
                                        return (rcode);
                                }

                                if((err = smctr_send_rsp(dev, rmf, rcode,
                                        correlator)))
                                {
                                        return (err);
                                }
                                break;

                        case CHG_PARM:
                                if((rcode = smctr_rcv_chg_param(dev, rmf,
                                        &correlator)) ==HARDWARE_FAILED)
                                {
                                        return (rcode);
                                }

                                if((err = smctr_send_rsp(dev, rmf, rcode,
                                        correlator)))
                                {
                                        return (err);
                                }
                                break;

                        case RQ_ADDR:
                                if((rcode = smctr_rcv_rq_addr_state_attch(dev,
                                        rmf, &correlator)) != POSITIVE_ACK)
                                {
                                        if(rcode == HARDWARE_FAILED)
                                                return (rcode);
                                        else
                                                return (smctr_send_rsp(dev, rmf,
                                                        rcode, correlator));
                                }

                                if((err = smctr_send_rpt_addr(dev, rmf,
                                        correlator)))
                                {
                                        return (err);
                                }
                                break;

                        case RQ_ATTCH:
                                if((rcode = smctr_rcv_rq_addr_state_attch(dev,
                                        rmf, &correlator)) != POSITIVE_ACK)
                                {
                                        if(rcode == HARDWARE_FAILED)
                                                return (rcode);
                                        else
                                                return (smctr_send_rsp(dev, rmf,
                                                        rcode,
                                                        correlator));
                                }

                                if((err = smctr_send_rpt_attch(dev, rmf,
                                        correlator)))
                                {
                                        return (err);
                                }
                                break;

                        case RQ_STATE:
                                if((rcode = smctr_rcv_rq_addr_state_attch(dev,
                                        rmf, &correlator)) != POSITIVE_ACK)
                                {
                                        if(rcode == HARDWARE_FAILED)
                                                return (rcode);
                                        else
                                                return (smctr_send_rsp(dev, rmf,
                                                        rcode,
                                                        correlator));
                                }

                                if((err = smctr_send_rpt_state(dev, rmf,
                                        correlator)))
                                {
                                        return (err);
                                }
                                break;

                        case TX_FORWARD: {
        			__u16 uninitialized_var(tx_fstatus);

                                if((rcode = smctr_rcv_tx_forward(dev, rmf))
                                        != POSITIVE_ACK)
                                {
                                        if(rcode == HARDWARE_FAILED)
                                                return (rcode);
                                        else
                                                return (smctr_send_rsp(dev, rmf,
                                                        rcode,
                                                        correlator));
                                }

                                if((err = smctr_send_tx_forward(dev, rmf,
                                        &tx_fstatus)) == HARDWARE_FAILED)
                                {
                                        return (err);
                                }

                                if(err == A_FRAME_WAS_FORWARDED)
                                {
                                        if((err = smctr_send_rpt_tx_forward(dev,
						rmf, tx_fstatus))
                                                == HARDWARE_FAILED)
                                        {
                                                return (err);
                                        }
                                }
                                break;
			}

                        /* Received MAC Frames Processed by CRS/REM/RPS. */
                        case RSP:
                        case RQ_INIT:
                        case RPT_NEW_MON:
                        case RPT_SUA_CHG:
                        case RPT_ACTIVE_ERR:
                        case RPT_NN_INCMP:
                        case RPT_ERROR:
                        case RPT_ATTCH:
                        case RPT_STATE:
                        case RPT_ADDR:
                                break;

                        /* Rcvd Att. MAC Frame (if RXATMAC set) or UNKNOWN */
                        default:
                                xframe = 0;
                                if(!(tp->receive_mask & ACCEPT_ATT_MAC_FRAMES))
                                {
                                        rcode = smctr_rcv_unknown(dev, rmf,
                                                &correlator);
                                        if((err = smctr_send_rsp(dev, rmf,rcode,
                                                correlator)))
                                        {
                                                return (err);
                                        }
                                }

                                break;
                }
        }
        else
        {
                /* 1. DA doesn't match (Promiscuous Mode).
                 * 2. Parse for Extended MAC Frame Type.
                 */
                switch(rmf->vc)
                {
                        case RSP:
                        case INIT:
                        case RQ_INIT:
                        case RQ_ADDR:
                        case RQ_ATTCH:
                        case RQ_STATE:
                        case CHG_PARM:
                        case RPT_ADDR:
                        case RPT_ERROR:
                        case RPT_ATTCH:
                        case RPT_STATE:
                        case RPT_NEW_MON:
                        case RPT_SUA_CHG:
                        case RPT_NN_INCMP:
                        case RPT_ACTIVE_ERR:
                                break;

                        default:
                                xframe = 0;
                                break;
                }
        }

        /* NOTE: UNKNOWN MAC frames will NOT be passed up unless
         * ACCEPT_ATT_MAC_FRAMES is set.
         */
        if(((tp->receive_mask & ACCEPT_ATT_MAC_FRAMES)
                && (xframe == (__u8)0))
                || ((tp->receive_mask & ACCEPT_EXT_MAC_FRAMES)
                && (xframe == (__u8)1)))
        {
                rmf->vl = SWAP_BYTES(rmf->vl);

                if (!(skb = dev_alloc_skb(size)))
			return -ENOMEM;
                skb->len = size;

                /* Slide data into a sleek skb. */
                skb_put(skb, skb->len);
                skb_copy_to_linear_data(skb, rmf, skb->len);

                /* Update Counters */
                tp->MacStat.rx_packets++;
                tp->MacStat.rx_bytes += skb->len;

                /* Kick the packet on up. */
                skb->protocol = tr_type_trans(skb, dev);
                netif_rx(skb);
                err = 0;
        }

        return (err);
}

/* Adapter RAM test. Incremental word ODD boundary data test. */
static int smctr_ram_memory_test(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        __u16 page, pages_of_ram, start_pattern = 0, word_pattern = 0,
                word_read = 0, err_word = 0, err_pattern = 0;
        unsigned int err_offset;
        __u32 j, pword;
        __u8 err = 0;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_ram_memory_test\n", dev->name);

        start_pattern   = 0x0001;
        pages_of_ram    = tp->ram_size / tp->ram_usable;
        pword           = tp->ram_access;

        /* Incremental word ODD boundary test. */
        for(page = 0; (page < pages_of_ram) && (~err);
                page++, start_pattern += 0x8000)
        {
                smctr_set_page(dev, (__u8 *)(tp->ram_access
                        + (page * tp->ram_usable * 1024) + 1));
                word_pattern = start_pattern;

                for(j = 1; j < (__u32)(tp->ram_usable * 1024) - 1; j += 2)
                        *(__u16 *)(pword + j) = word_pattern++;

                word_pattern = start_pattern;

                for(j = 1; j < (__u32)(tp->ram_usable * 1024) - 1
                        && (~err); j += 2, word_pattern++)
                {
                        word_read = *(__u16 *)(pword + j);
                        if(word_read != word_pattern)
                        {
                                err             = (__u8)1;
                                err_offset      = j;
                                err_word        = word_read;
                                err_pattern     = word_pattern;
                                return (RAM_TEST_FAILED);
                        }
                }
        }

        /* Zero out memory. */
        for(page = 0; page < pages_of_ram && (~err); page++)
        {
                smctr_set_page(dev, (__u8 *)(tp->ram_access
                        + (page * tp->ram_usable * 1024)));
                word_pattern = 0;

                for(j = 0; j < (__u32)tp->ram_usable * 1024; j +=2)
                        *(__u16 *)(pword + j) = word_pattern;

                for(j =0; j < (__u32)tp->ram_usable * 1024
                        && (~err); j += 2)
                {
                        word_read = *(__u16 *)(pword + j);
                        if(word_read != word_pattern)
                        {
                                err             = (__u8)1;
                                err_offset      = j;
                                err_word        = word_read;
                                err_pattern     = word_pattern;
                                return (RAM_TEST_FAILED);
                        }
                }
        }

        smctr_set_page(dev, (__u8 *)tp->ram_access);

        return (0);
}

static int smctr_rcv_chg_param(struct net_device *dev, MAC_HEADER *rmf,
        __u16 *correlator)
{
        MAC_SUB_VECTOR *rsv;
        signed short vlen;
        __u16 rcode = POSITIVE_ACK;
        unsigned int svectors = F_NO_SUB_VECTORS_FOUND;

        /* This Frame can only come from a CRS */
        if((rmf->dc_sc & SC_MASK) != SC_CRS)
                return(E_INAPPROPRIATE_SOURCE_CLASS);

        /* Remove MVID Length from total length. */
        vlen = (signed short)rmf->vl - 4;

        /* Point to First SVID */
        rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));

        /* Search for Appropriate SVID's. */
        while((vlen > 0) && (rcode == POSITIVE_ACK))
        {
                switch(rsv->svi)
                {
                        case CORRELATOR:
                                svectors |= F_CORRELATOR;
                                rcode = smctr_set_corr(dev, rsv, correlator);
                                break;

                        case LOCAL_RING_NUMBER:
                                svectors |= F_LOCAL_RING_NUMBER;
                                rcode = smctr_set_local_ring_num(dev, rsv);
                                break;

                        case ASSIGN_PHYSICAL_DROP:
                                svectors |= F_ASSIGN_PHYSICAL_DROP;
                                rcode = smctr_set_phy_drop(dev, rsv);
                                break;

                        case ERROR_TIMER_VALUE:
                                svectors |= F_ERROR_TIMER_VALUE;
                                rcode = smctr_set_error_timer_value(dev, rsv);
                                break;

                        case AUTHORIZED_FUNCTION_CLASS:
                                svectors |= F_AUTHORIZED_FUNCTION_CLASS;
                                rcode = smctr_set_auth_funct_class(dev, rsv);
                                break;

                        case AUTHORIZED_ACCESS_PRIORITY:
                                svectors |= F_AUTHORIZED_ACCESS_PRIORITY;
                                rcode = smctr_set_auth_access_pri(dev, rsv);
                                break;

                        default:
                                rcode = E_SUB_VECTOR_UNKNOWN;
                                break;
                }

                /* Let Sender Know if SUM of SV length's is
                 * larger then length in MVID length field
                 */
                if((vlen -= rsv->svl) < 0)
                        rcode = E_VECTOR_LENGTH_ERROR;

                rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
        }

        if(rcode == POSITIVE_ACK)
        {
                /* Let Sender Know if MVID length field
                 * is larger then SUM of SV length's
                 */
                if(vlen != 0)
                        rcode = E_VECTOR_LENGTH_ERROR;
                else
		{
                	/* Let Sender Know if Expected SVID Missing */
                	if((svectors & R_CHG_PARM) ^ R_CHG_PARM)
                        	rcode = E_MISSING_SUB_VECTOR;
		}
        }

        return (rcode);
}

static int smctr_rcv_init(struct net_device *dev, MAC_HEADER *rmf,
        __u16 *correlator)
{
        MAC_SUB_VECTOR *rsv;
        signed short vlen;
        __u16 rcode = POSITIVE_ACK;
        unsigned int svectors = F_NO_SUB_VECTORS_FOUND;

        /* This Frame can only come from a RPS */
        if((rmf->dc_sc & SC_MASK) != SC_RPS)
                return (E_INAPPROPRIATE_SOURCE_CLASS);

        /* Remove MVID Length from total length. */
        vlen = (signed short)rmf->vl - 4;

        /* Point to First SVID */
        rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));

        /* Search for Appropriate SVID's */
        while((vlen > 0) && (rcode == POSITIVE_ACK))
        {
                switch(rsv->svi)
                {
                        case CORRELATOR:
                                svectors |= F_CORRELATOR;
                                rcode = smctr_set_corr(dev, rsv, correlator);
                                break;

                        case LOCAL_RING_NUMBER:
                                svectors |= F_LOCAL_RING_NUMBER;
                                rcode = smctr_set_local_ring_num(dev, rsv);
                                break;

                        case ASSIGN_PHYSICAL_DROP:
                                svectors |= F_ASSIGN_PHYSICAL_DROP;
                                rcode = smctr_set_phy_drop(dev, rsv);
                                break;

                        case ERROR_TIMER_VALUE:
                                svectors |= F_ERROR_TIMER_VALUE;
                                rcode = smctr_set_error_timer_value(dev, rsv);
                                break;

                        default:
                                rcode = E_SUB_VECTOR_UNKNOWN;
                                break;
                }

                /* Let Sender Know if SUM of SV length's is
                 * larger then length in MVID length field
		 */
                if((vlen -= rsv->svl) < 0)
                        rcode = E_VECTOR_LENGTH_ERROR;

                rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
        }

        if(rcode == POSITIVE_ACK)
        {
                /* Let Sender Know if MVID length field
                 * is larger then SUM of SV length's
                 */
                if(vlen != 0)
                        rcode = E_VECTOR_LENGTH_ERROR;
                else
		{
                	/* Let Sender Know if Expected SV Missing */
                	if((svectors & R_INIT) ^ R_INIT)
                        	rcode = E_MISSING_SUB_VECTOR;
		}
        }

        return (rcode);
}

static int smctr_rcv_tx_forward(struct net_device *dev, MAC_HEADER *rmf)
{
        MAC_SUB_VECTOR *rsv;
        signed short vlen;
        __u16 rcode = POSITIVE_ACK;
        unsigned int svectors = F_NO_SUB_VECTORS_FOUND;

        /* This Frame can only come from a CRS */
        if((rmf->dc_sc & SC_MASK) != SC_CRS)
                return (E_INAPPROPRIATE_SOURCE_CLASS);

        /* Remove MVID Length from total length */
        vlen = (signed short)rmf->vl - 4;

        /* Point to First SVID */
        rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));

        /* Search for Appropriate SVID's */
        while((vlen > 0) && (rcode == POSITIVE_ACK))
        {
                switch(rsv->svi)
                {
                        case FRAME_FORWARD:
                                svectors |= F_FRAME_FORWARD;
                                rcode = smctr_set_frame_forward(dev, rsv, 
					rmf->dc_sc);
                                break;

                        default:
                                rcode = E_SUB_VECTOR_UNKNOWN;
                                break;
                }

                /* Let Sender Know if SUM of SV length's is
                 * larger then length in MVID length field
		 */
                if((vlen -= rsv->svl) < 0)
                        rcode = E_VECTOR_LENGTH_ERROR;

                rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
        }

        if(rcode == POSITIVE_ACK)
        {
                /* Let Sender Know if MVID length field
                 * is larger then SUM of SV length's
                 */
                if(vlen != 0)
                        rcode = E_VECTOR_LENGTH_ERROR;
                else
		{
                	/* Let Sender Know if Expected SV Missing */
                	if((svectors & R_TX_FORWARD) ^ R_TX_FORWARD)
                        	rcode = E_MISSING_SUB_VECTOR;
		}
        }

        return (rcode);
}

static int smctr_rcv_rq_addr_state_attch(struct net_device *dev,
        MAC_HEADER *rmf, __u16 *correlator)
{
        MAC_SUB_VECTOR *rsv;
        signed short vlen;
        __u16 rcode = POSITIVE_ACK;
        unsigned int svectors = F_NO_SUB_VECTORS_FOUND;

        /* Remove MVID Length from total length */
        vlen = (signed short)rmf->vl - 4;

        /* Point to First SVID */
        rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));

        /* Search for Appropriate SVID's */
        while((vlen > 0) && (rcode == POSITIVE_ACK))
        {
                switch(rsv->svi)
                {
                        case CORRELATOR:
                                svectors |= F_CORRELATOR;
                                rcode = smctr_set_corr(dev, rsv, correlator);
                                break;

                        default:
                                rcode = E_SUB_VECTOR_UNKNOWN;
                                break;
                }

                /* Let Sender Know if SUM of SV length's is
                 * larger then length in MVID length field
                 */
                if((vlen -= rsv->svl) < 0)
                        rcode = E_VECTOR_LENGTH_ERROR;

                rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
        }

        if(rcode == POSITIVE_ACK)
        {
                /* Let Sender Know if MVID length field
                 * is larger then SUM of SV length's
                 */
                if(vlen != 0)
                        rcode = E_VECTOR_LENGTH_ERROR;
                else
		{
                	/* Let Sender Know if Expected SVID Missing */
                	if((svectors & R_RQ_ATTCH_STATE_ADDR) 
				^ R_RQ_ATTCH_STATE_ADDR)
                        	rcode = E_MISSING_SUB_VECTOR;
			}
        }

        return (rcode);
}

static int smctr_rcv_unknown(struct net_device *dev, MAC_HEADER *rmf,
        __u16 *correlator)
{
        MAC_SUB_VECTOR *rsv;
        signed short vlen;

        *correlator = 0;

        /* Remove MVID Length from total length */
        vlen = (signed short)rmf->vl - 4;

        /* Point to First SVID */
        rsv = (MAC_SUB_VECTOR *)((__u32)rmf + sizeof(MAC_HEADER));

        /* Search for CORRELATOR for RSP to UNKNOWN */
        while((vlen > 0) && (*correlator == 0))
        {
                switch(rsv->svi)
                {
                        case CORRELATOR:
                                smctr_set_corr(dev, rsv, correlator);
                                break;

                        default:
                                break;
                }

                vlen -= rsv->svl;
                rsv = (MAC_SUB_VECTOR *)((__u32)rsv + rsv->svl);
        }

        return (E_UNRECOGNIZED_VECTOR_ID);
}

/*
 * Reset the 825 NIC and exit w:
 * 1. The NIC reset cleared (non-reset state), halted and un-initialized.
 * 2. TINT masked.
 * 3. CBUSY masked.
 * 4. TINT clear.
 * 5. CBUSY clear.
 */
static int smctr_reset_adapter(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int ioaddr = dev->base_addr;

        /* Reseting the NIC will put it in a halted and un-initialized state. */        smctr_set_trc_reset(ioaddr);
        mdelay(200); /* ~2 ms */

        smctr_clear_trc_reset(ioaddr);
        mdelay(200); /* ~2 ms */

        /* Remove any latched interrupts that occurred prior to reseting the
         * adapter or possibily caused by line glitches due to the reset.
         */
        outb(tp->trc_mask | CSR_CLRTINT | CSR_CLRCBUSY, ioaddr + CSR);

        return (0);
}

static int smctr_restart_tx_chain(struct net_device *dev, short queue)
{
        struct net_local *tp = netdev_priv(dev);
        int err = 0;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_restart_tx_chain\n", dev->name);

        if(tp->num_tx_fcbs_used[queue] != 0
                && tp->tx_queue_status[queue] == NOT_TRANSMITING)
        {
                tp->tx_queue_status[queue] = TRANSMITING;
                err = smctr_issue_resume_tx_fcb_cmd(dev, queue);
        }

        return (err);
}

static int smctr_ring_status_chg(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_ring_status_chg\n", dev->name);

        /* Check for ring_status_flag: whenever MONITOR_STATE_BIT
         * Bit is set, check value of monitor_state, only then we
         * enable and start transmit/receive timeout (if and only
         * if it is MS_ACTIVE_MONITOR_STATE or MS_STANDBY_MONITOR_STATE)
         */
        if(tp->ring_status_flags == MONITOR_STATE_CHANGED)
        {
                if((tp->monitor_state == MS_ACTIVE_MONITOR_STATE)
                        || (tp->monitor_state == MS_STANDBY_MONITOR_STATE))
                {
                        tp->monitor_state_ready = 1;
                }
                else
                {
                        /* if adapter is NOT in either active monitor
                         * or standby monitor state => Disable
                         * transmit/receive timeout.
                         */
                        tp->monitor_state_ready = 0;

			/* Ring speed problem, switching to auto mode. */
			if(tp->monitor_state == MS_MONITOR_FSM_INACTIVE
				&& !tp->cleanup)
			{
				printk(KERN_INFO "%s: Incorrect ring speed switching.\n",
					dev->name);
				smctr_set_ring_speed(dev);
			}
                }
        }

        if(!(tp->ring_status_flags & RING_STATUS_CHANGED))
                return (0);

        switch(tp->ring_status)
        {
                case RING_RECOVERY:
                        printk(KERN_INFO "%s: Ring Recovery\n", dev->name);
                        break;

                case SINGLE_STATION:
                        printk(KERN_INFO "%s: Single Statinon\n", dev->name);
                        break;

                case COUNTER_OVERFLOW:
                        printk(KERN_INFO "%s: Counter Overflow\n", dev->name);
                        break;

                case REMOVE_RECEIVED:
                        printk(KERN_INFO "%s: Remove Received\n", dev->name);
                        break;

                case AUTO_REMOVAL_ERROR:
                        printk(KERN_INFO "%s: Auto Remove Error\n", dev->name);
                        break;

                case LOBE_WIRE_FAULT:
                        printk(KERN_INFO "%s: Lobe Wire Fault\n", dev->name);
                        break;

                case TRANSMIT_BEACON:
                        printk(KERN_INFO "%s: Transmit Beacon\n", dev->name);
                        break;

                case SOFT_ERROR:
                        printk(KERN_INFO "%s: Soft Error\n", dev->name);
                        break;

                case HARD_ERROR:
                        printk(KERN_INFO "%s: Hard Error\n", dev->name);
                        break;

                case SIGNAL_LOSS:
                        printk(KERN_INFO "%s: Signal Loss\n", dev->name);
                        break;

                default:
			printk(KERN_INFO "%s: Unknown ring status change\n",
				dev->name);
                        break;
        }

        return (0);
}

static int smctr_rx_frame(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        __u16 queue, status, rx_size, err = 0;
        __u8 *pbuff;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_rx_frame\n", dev->name);

        queue = tp->receive_queue_number;

        while((status = tp->rx_fcb_curr[queue]->frame_status) != SUCCESS)
        {
                err = HARDWARE_FAILED;

                if(((status & 0x007f) == 0)
                        || ((tp->receive_mask & ACCEPT_ERR_PACKETS) != 0))
                {
                        /* frame length less the CRC (4 bytes) + FS (1 byte) */
                        rx_size = tp->rx_fcb_curr[queue]->frame_length - 5;

                        pbuff = smctr_get_rx_pointer(dev, queue);

                        smctr_set_page(dev, pbuff);
                        smctr_disable_16bit(dev);

                        /* pbuff points to addr within one page */
                        pbuff = (__u8 *)PAGE_POINTER(pbuff);

                        if(queue == NON_MAC_QUEUE)
                        {
                                struct sk_buff *skb;

                                skb = dev_alloc_skb(rx_size);
				if (skb) {
                                	skb_put(skb, rx_size);

					skb_copy_to_linear_data(skb, pbuff, rx_size);

                                	/* Update Counters */
                                	tp->MacStat.rx_packets++;
                                	tp->MacStat.rx_bytes += skb->len;

                                	/* Kick the packet on up. */
                                	skb->protocol = tr_type_trans(skb, dev);
                                	netif_rx(skb);
				} else {
				}
                        }
                        else
                                smctr_process_rx_packet((MAC_HEADER *)pbuff,
                                        rx_size, dev, status);
                }

                smctr_enable_16bit(dev);
                smctr_set_page(dev, (__u8 *)tp->ram_access);
                smctr_update_rx_chain(dev, queue);

                if(err != SUCCESS)
                        break;
        }

        return (err);
}

static int smctr_send_dat(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int i, err;
        MAC_HEADER *tmf;
        FCBlock *fcb;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_send_dat\n", dev->name);

        if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE,
                sizeof(MAC_HEADER))) == (FCBlock *)(-1L))
        {
                return (OUT_OF_RESOURCES);
        }

        /* Initialize DAT Data Fields. */
        tmf = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
        tmf->ac = MSB(AC_FC_DAT);
        tmf->fc = LSB(AC_FC_DAT);

        for(i = 0; i < 6; i++)
        {
                tmf->sa[i] = dev->dev_addr[i];
                tmf->da[i] = dev->dev_addr[i];

        }

        tmf->vc        = DAT;
        tmf->dc_sc     = DC_RS | SC_RS;
        tmf->vl        = 4;
        tmf->vl        = SWAP_BYTES(tmf->vl);

        /* Start Transmit. */
        if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
                return (err);

        /* Wait for Transmit to Complete */
        for(i = 0; i < 10000; i++)
        {
                if(fcb->frame_status & FCB_COMMAND_DONE)
                        break;
                mdelay(1);
        }

        /* Check if GOOD frame Tx'ed. */
        if(!(fcb->frame_status &  FCB_COMMAND_DONE)
                || fcb->frame_status & (FCB_TX_STATUS_E | FCB_TX_AC_BITS))
        {
                return (INITIALIZE_FAILED);
        }

        /* De-allocated Tx FCB and Frame Buffer
         * The FCB must be de-allocated manually if executing with
         * interrupts disabled, other wise the ISR (LM_Service_Events)
         * will de-allocate it when the interrupt occurs.
         */
        tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
        smctr_update_tx_chain(dev, fcb, MAC_QUEUE);

        return (0);
}

static void smctr_timeout(struct net_device *dev)
{
	/*
         * If we get here, some higher level has decided we are broken.
         * There should really be a "kick me" function call instead.
         *
         * Resetting the token ring adapter takes a long time so just
         * fake transmission time and go on trying. Our own timeout
         * routine is in sktr_timer_chk()
         */
        dev->trans_start = jiffies;
        netif_wake_queue(dev);
}

/*
 * Gets skb from system, queues it and checks if it can be sent
 */
static int smctr_send_packet(struct sk_buff *skb, struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_send_packet\n", dev->name);

        /*
         * Block a transmit overlap
         */
         
        netif_stop_queue(dev);

        if(tp->QueueSkb == 0)
                return NETDEV_TX_BUSY;     /* Return with tbusy set: queue full */

        tp->QueueSkb--;
        skb_queue_tail(&tp->SendSkbQueue, skb);
        smctr_hardware_send_packet(dev, tp);
        if(tp->QueueSkb > 0)
		netif_wake_queue(dev);
		
        return NETDEV_TX_OK;
}

static int smctr_send_lobe_media_test(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
	MAC_SUB_VECTOR *tsv;
	MAC_HEADER *tmf;
        FCBlock *fcb;
	__u32 i;
	int err;

        if(smctr_debug > 15)
                printk(KERN_DEBUG "%s: smctr_send_lobe_media_test\n", dev->name);

        if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(struct trh_hdr)
                + S_WRAP_DATA + S_WRAP_DATA)) == (FCBlock *)(-1L))
        {
                return (OUT_OF_RESOURCES);
        }

        /* Initialize DAT Data Fields. */
        tmf = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
        tmf->ac = MSB(AC_FC_LOBE_MEDIA_TEST);
        tmf->fc = LSB(AC_FC_LOBE_MEDIA_TEST);

        for(i = 0; i < 6; i++)
        {
                tmf->da[i] = 0;
                tmf->sa[i] = dev->dev_addr[i];
        }

        tmf->vc        = LOBE_MEDIA_TEST;
        tmf->dc_sc     = DC_RS | SC_RS;
        tmf->vl        = 4;

        tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
        smctr_make_wrap_data(dev, tsv);
        tmf->vl += tsv->svl;

        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_wrap_data(dev, tsv);
        tmf->vl += tsv->svl;

        /* Start Transmit. */
        tmf->vl = SWAP_BYTES(tmf->vl);
        if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
                return (err);

        /* Wait for Transmit to Complete. (10 ms). */
        for(i=0; i < 10000; i++)
        {
                if(fcb->frame_status & FCB_COMMAND_DONE)
                        break;
                mdelay(1);
        }

        /* Check if GOOD frame Tx'ed */
        if(!(fcb->frame_status & FCB_COMMAND_DONE)
                || fcb->frame_status & (FCB_TX_STATUS_E | FCB_TX_AC_BITS))
        {
                return (LOBE_MEDIA_TEST_FAILED);
        }

        /* De-allocated Tx FCB and Frame Buffer
         * The FCB must be de-allocated manually if executing with
         * interrupts disabled, other wise the ISR (LM_Service_Events)
         * will de-allocate it when the interrupt occurs.
         */
        tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
        smctr_update_tx_chain(dev, fcb, MAC_QUEUE);

        return (0);
}

static int smctr_send_rpt_addr(struct net_device *dev, MAC_HEADER *rmf,
        __u16 correlator)
{
        MAC_HEADER *tmf;
        MAC_SUB_VECTOR *tsv;
        FCBlock *fcb;

        if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
		+ S_CORRELATOR + S_PHYSICAL_DROP + S_UPSTREAM_NEIGHBOR_ADDRESS
		+ S_ADDRESS_MODIFER + S_GROUP_ADDRESS + S_FUNCTIONAL_ADDRESS))
		== (FCBlock *)(-1L))
        {
                return (0);
        }

        tmf 		= (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
        tmf->vc    	= RPT_ADDR;
        tmf->dc_sc 	= (rmf->dc_sc & SC_MASK) << 4;
        tmf->vl    	= 4;

        smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_ADDR);

        tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
        smctr_make_corr(dev, tsv, correlator);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_phy_drop_num(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_upstream_neighbor_addr(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_addr_mod(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_group_addr(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_funct_addr(dev, tsv);

        tmf->vl += tsv->svl;

        /* Subtract out MVID and MVL which is
         * include in both vl and MAC_HEADER
         */
/*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
        fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
*/
        tmf->vl = SWAP_BYTES(tmf->vl);

        return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
}

static int smctr_send_rpt_attch(struct net_device *dev, MAC_HEADER *rmf,
        __u16 correlator)
{
        MAC_HEADER *tmf;
        MAC_SUB_VECTOR *tsv;
        FCBlock *fcb;

        if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
		+ S_CORRELATOR + S_PRODUCT_INSTANCE_ID + S_FUNCTIONAL_ADDRESS
		+ S_AUTHORIZED_FUNCTION_CLASS + S_AUTHORIZED_ACCESS_PRIORITY))
		== (FCBlock *)(-1L))
        {
                return (0);
        }

        tmf 	   = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
        tmf->vc    = RPT_ATTCH;
        tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
        tmf->vl    = 4;

        smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_ATTCH);

        tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
        smctr_make_corr(dev, tsv, correlator);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_product_id(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_funct_addr(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_auth_funct_class(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_access_pri(dev, tsv);

        tmf->vl += tsv->svl;

        /* Subtract out MVID and MVL which is
         * include in both vl and MAC_HEADER
         */
/*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
        fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
*/
        tmf->vl = SWAP_BYTES(tmf->vl);

        return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
}

static int smctr_send_rpt_state(struct net_device *dev, MAC_HEADER *rmf,
        __u16 correlator)
{
        MAC_HEADER *tmf;
        MAC_SUB_VECTOR *tsv;
        FCBlock *fcb;

        if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
		+ S_CORRELATOR + S_RING_STATION_VERSION_NUMBER
		+ S_RING_STATION_STATUS + S_STATION_IDENTIFER))
		== (FCBlock *)(-1L))
        {
                return (0);
        }

        tmf 	   = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
        tmf->vc    = RPT_STATE;
        tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
        tmf->vl    = 4;

        smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_STATE);

        tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
        smctr_make_corr(dev, tsv, correlator);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_ring_station_version(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_ring_station_status(dev, tsv);

        tmf->vl += tsv->svl;
        tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
        smctr_make_station_id(dev, tsv);

        tmf->vl += tsv->svl;

        /* Subtract out MVID and MVL which is
         * include in both vl and MAC_HEADER
         */
/*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
        fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
*/
        tmf->vl = SWAP_BYTES(tmf->vl);

        return (smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
}

static int smctr_send_rpt_tx_forward(struct net_device *dev,
        MAC_HEADER *rmf, __u16 tx_fstatus)
{
        MAC_HEADER *tmf;
        MAC_SUB_VECTOR *tsv;
        FCBlock *fcb;

        if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
		+ S_TRANSMIT_STATUS_CODE)) == (FCBlock *)(-1L))
        {
                return (0);
        }

        tmf 	   = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
        tmf->vc    = RPT_TX_FORWARD;
        tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
        tmf->vl    = 4;

        smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RPT_TX_FORWARD);

        tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
        smctr_make_tx_status_code(dev, tsv, tx_fstatus);

        tmf->vl += tsv->svl;

        /* Subtract out MVID and MVL which is
         * include in both vl and MAC_HEADER
         */
/*      fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
        fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
*/
        tmf->vl = SWAP_BYTES(tmf->vl);

        return(smctr_trc_send_packet(dev, fcb, MAC_QUEUE));
}

static int smctr_send_rsp(struct net_device *dev, MAC_HEADER *rmf,
        __u16 rcode, __u16 correlator)
{
        MAC_HEADER *tmf;
        MAC_SUB_VECTOR *tsv;
        FCBlock *fcb;

        if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
		+ S_CORRELATOR + S_RESPONSE_CODE)) == (FCBlock *)(-1L))
        {
                return (0);
        }

        tmf 	   = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
        tmf->vc    = RSP;
        tmf->dc_sc = (rmf->dc_sc & SC_MASK) << 4;
        tmf->vl    = 4;

        smctr_make_8025_hdr(dev, rmf, tmf, AC_FC_RSP);

        tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
        smctr_make_corr(dev, tsv, correlator);

        return (0);
}

static int smctr_send_rq_init(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        MAC_HEADER *tmf;
        MAC_SUB_VECTOR *tsv;
        FCBlock *fcb;
	unsigned int i, count = 0;
	__u16 fstatus;
	int err;

        do {
        	if(((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, sizeof(MAC_HEADER)
			+ S_PRODUCT_INSTANCE_ID + S_UPSTREAM_NEIGHBOR_ADDRESS
			+ S_RING_STATION_VERSION_NUMBER + S_ADDRESS_MODIFER))
			== (FCBlock *)(-1L)))
                {
                        return (0);
                }

                tmf 	   = (MAC_HEADER *)fcb->bdb_ptr->data_block_ptr;
                tmf->vc    = RQ_INIT;
                tmf->dc_sc = DC_RPS | SC_RS;
                tmf->vl    = 4;

                smctr_make_8025_hdr(dev, NULL, tmf, AC_FC_RQ_INIT);

                tsv = (MAC_SUB_VECTOR *)((__u32)tmf + sizeof(MAC_HEADER));
                smctr_make_product_id(dev, tsv);

                tmf->vl += tsv->svl;
                tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
                smctr_make_upstream_neighbor_addr(dev, tsv);

                tmf->vl += tsv->svl;
                tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
                smctr_make_ring_station_version(dev, tsv);

                tmf->vl += tsv->svl;
                tsv = (MAC_SUB_VECTOR *)((__u32)tsv + tsv->svl);
                smctr_make_addr_mod(dev, tsv);

                tmf->vl += tsv->svl;

                /* Subtract out MVID and MVL which is
                 * include in both vl and MAC_HEADER
                 */
/*              fcb->frame_length           = tmf->vl + sizeof(MAC_HEADER) - 4;
                fcb->bdb_ptr->buffer_length = tmf->vl + sizeof(MAC_HEADER) - 4;
*/
                tmf->vl = SWAP_BYTES(tmf->vl);

                if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
                        return (err);

                /* Wait for Transmit to Complete */
      		for(i = 0; i < 10000; i++) 
		{
          		if(fcb->frame_status & FCB_COMMAND_DONE)
              			break;
          		mdelay(1);
      		}

                /* Check if GOOD frame Tx'ed */
                fstatus = fcb->frame_status;

                if(!(fstatus & FCB_COMMAND_DONE))
                        return (HARDWARE_FAILED);

                if(!(fstatus & FCB_TX_STATUS_E))
                        count++;

                /* De-allocated Tx FCB and Frame Buffer
                 * The FCB must be de-allocated manually if executing with
                 * interrupts disabled, other wise the ISR (LM_Service_Events)
                 * will de-allocate it when the interrupt occurs.
                 */
                tp->tx_queue_status[MAC_QUEUE] = NOT_TRANSMITING;
                smctr_update_tx_chain(dev, fcb, MAC_QUEUE);
        } while(count < 4 && ((fstatus & FCB_TX_AC_BITS) ^ FCB_TX_AC_BITS));

	return (smctr_join_complete_state(dev));
}

static int smctr_send_tx_forward(struct net_device *dev, MAC_HEADER *rmf,
        __u16 *tx_fstatus)
{
        struct net_local *tp = netdev_priv(dev);
        FCBlock *fcb;
        unsigned int i;
	int err;

        /* Check if this is the END POINT of the Transmit Forward Chain. */
        if(rmf->vl <= 18)
                return (0);

        /* Allocate Transmit FCB only by requesting 0 bytes
         * of data buffer.
         */
        if((fcb = smctr_get_tx_fcb(dev, MAC_QUEUE, 0)) == (FCBlock *)(-1L))
                return (0);

        /* Set pointer to Transmit Frame Buffer to the data
         * portion of the received TX Forward frame, making
         * sure to skip over the Vector Code (vc) and Vector
         * length (vl).
         */
        fcb->bdb_ptr->trc_data_block_ptr = TRC_POINTER((__u32)rmf 
		+ sizeof(MAC_HEADER) + 2);
        fcb->bdb_ptr->data_block_ptr     = (__u16 *)((__u32)rmf 
		+ sizeof(MAC_HEADER) + 2);

        fcb->frame_length                = rmf->vl - 4 - 2;
        fcb->bdb_ptr->buffer_length      = rmf->vl - 4 - 2;

        if((err = smctr_trc_send_packet(dev, fcb, MAC_QUEUE)))
                return (err);

        /* Wait for Transmit to Complete */
   	for(i = 0; i < 10000; i++) 
	{
       		if(fcb->frame_status & FCB_COMMAND_DONE)
           		break;
        	mdelay(1);
   	}

        /* Check if GOOD frame Tx'ed */
        if(!(fcb->frame_status & FCB_COMMAND_DONE))
        {
                if((err = smctr_issue_resume_tx_fcb_cmd(dev, MAC_QUEUE)))
                        return (err);

      		for(i = 0; i < 10000; i++) 
		{
          		if(fcb->frame_status & FCB_COMMAND_DONE)
              			break;
        		mdelay(1);
      		}

                if(!(fcb->frame_status & FCB_COMMAND_DONE))
                        return (HARDWARE_FAILED);
        }

        *tx_fstatus = fcb->frame_status;

        return (A_FRAME_WAS_FORWARDED);
}

static int smctr_set_auth_access_pri(struct net_device *dev,
        MAC_SUB_VECTOR *rsv)
{
        struct net_local *tp = netdev_priv(dev);

        if(rsv->svl != S_AUTHORIZED_ACCESS_PRIORITY)
                return (E_SUB_VECTOR_LENGTH_ERROR);

        tp->authorized_access_priority = (rsv->svv[0] << 8 | rsv->svv[1]);

        return (POSITIVE_ACK);
}

static int smctr_set_auth_funct_class(struct net_device *dev,
        MAC_SUB_VECTOR *rsv)
{
        struct net_local *tp = netdev_priv(dev);

        if(rsv->svl != S_AUTHORIZED_FUNCTION_CLASS)
                return (E_SUB_VECTOR_LENGTH_ERROR);

        tp->authorized_function_classes = (rsv->svv[0] << 8 | rsv->svv[1]);

        return (POSITIVE_ACK);
}

static int smctr_set_corr(struct net_device *dev, MAC_SUB_VECTOR *rsv,
        __u16 *correlator)
{
        if(rsv->svl != S_CORRELATOR)
                return (E_SUB_VECTOR_LENGTH_ERROR);

        *correlator = (rsv->svv[0] << 8 | rsv->svv[1]);

        return (POSITIVE_ACK);
}

static int smctr_set_error_timer_value(struct net_device *dev,
        MAC_SUB_VECTOR *rsv)
{
	__u16 err_tval;
	int err;

        if(rsv->svl != S_ERROR_TIMER_VALUE)
                return (E_SUB_VECTOR_LENGTH_ERROR);

        err_tval = (rsv->svv[0] << 8 | rsv->svv[1])*10;

        smctr_issue_write_word_cmd(dev, RW_TER_THRESHOLD, &err_tval);

        if((err = smctr_wait_cmd(dev)))
                return (err);

        return (POSITIVE_ACK);
}

static int smctr_set_frame_forward(struct net_device *dev,
        MAC_SUB_VECTOR *rsv, __u8 dc_sc)
{
        if((rsv->svl < 2) || (rsv->svl > S_FRAME_FORWARD))
                return (E_SUB_VECTOR_LENGTH_ERROR);

        if((dc_sc & DC_MASK) != DC_CRS)
        {
                if(rsv->svl >= 2 && rsv->svl < 20)
                	return (E_TRANSMIT_FORWARD_INVALID);

                if((rsv->svv[0] != 0) || (rsv->svv[1] != 0))
                        return (E_TRANSMIT_FORWARD_INVALID);
        }

        return (POSITIVE_ACK);
}

static int smctr_set_local_ring_num(struct net_device *dev,
        MAC_SUB_VECTOR *rsv)
{
        struct net_local *tp = netdev_priv(dev);

        if(rsv->svl != S_LOCAL_RING_NUMBER)
                return (E_SUB_VECTOR_LENGTH_ERROR);

        if(tp->ptr_local_ring_num)
                *(__u16 *)(tp->ptr_local_ring_num) 
			= (rsv->svv[0] << 8 | rsv->svv[1]);

        return (POSITIVE_ACK);
}

static unsigned short smctr_set_ctrl_attention(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        int ioaddr = dev->base_addr;

        if(tp->bic_type == BIC_585_CHIP)
                outb((tp->trc_mask | HWR_CA), ioaddr + HWR);
        else
        {
                outb((tp->trc_mask | CSR_CA), ioaddr + CSR);
                outb(tp->trc_mask, ioaddr + CSR);
        }

        return (0);
}

static void smctr_set_multicast_list(struct net_device *dev)
{
        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_set_multicast_list\n", dev->name);

        return;
}

static int smctr_set_page(struct net_device *dev, __u8 *buf)
{
        struct net_local *tp = netdev_priv(dev);
        __u8 amask;
        __u32 tptr;

        tptr = (__u32)buf - (__u32)tp->ram_access;
        amask = (__u8)((tptr & PR_PAGE_MASK) >> 8);
        outb(amask, dev->base_addr + PR);

        return (0);
}

static int smctr_set_phy_drop(struct net_device *dev, MAC_SUB_VECTOR *rsv)
{
	int err;

        if(rsv->svl != S_PHYSICAL_DROP)
                return (E_SUB_VECTOR_LENGTH_ERROR);

        smctr_issue_write_byte_cmd(dev, RW_PHYSICAL_DROP_NUMBER, &rsv->svv[0]);
        if((err = smctr_wait_cmd(dev)))
                return (err);

        return (POSITIVE_ACK);
}

/* Reset the ring speed to the opposite of what it was. This auto-pilot
 * mode requires a complete reset and re-init of the adapter.
 */
static int smctr_set_ring_speed(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
	int err;

        if(tp->media_type == MEDIA_UTP_16)
                tp->media_type = MEDIA_UTP_4;
        else
                tp->media_type = MEDIA_UTP_16;

        smctr_enable_16bit(dev);

        /* Re-Initialize adapter's internal registers */
        smctr_reset_adapter(dev);

        if((err = smctr_init_card_real(dev)))
                return (err);

        smctr_enable_bic_int(dev);

        if((err = smctr_issue_enable_int_cmd(dev, TRC_INTERRUPT_ENABLE_MASK)))
                return (err);

        smctr_disable_16bit(dev);

	return (0);
}

static int smctr_set_rx_look_ahead(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        __u16 sword, rword;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_set_rx_look_ahead_flag\n", dev->name);

        tp->adapter_flags &= ~(FORCED_16BIT_MODE);
        tp->adapter_flags |= RX_VALID_LOOKAHEAD;

        if(tp->adapter_bus == BUS_ISA16_TYPE)
        {
                sword = *((__u16 *)(tp->ram_access));
                *((__u16 *)(tp->ram_access)) = 0x1234;

                smctr_disable_16bit(dev);
                rword = *((__u16 *)(tp->ram_access));
                smctr_enable_16bit(dev);

                if(rword != 0x1234)
                        tp->adapter_flags |= FORCED_16BIT_MODE;

                *((__u16 *)(tp->ram_access)) = sword;
        }

        return (0);
}

static int smctr_set_trc_reset(int ioaddr)
{
        __u8 r;

        r = inb(ioaddr + MSR);
        outb(MSR_RST | r, ioaddr + MSR);

        return (0);
}

/*
 * This function can be called if the adapter is busy or not.
 */
static int smctr_setup_single_cmd(struct net_device *dev,
        __u16 command, __u16 subcommand)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int err;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_setup_single_cmd\n", dev->name);

        if((err = smctr_wait_while_cbusy(dev)))
                return (err);

        if((err = (unsigned int)smctr_wait_cmd(dev)))
                return (err);

        tp->acb_head->cmd_done_status   = 0;
        tp->acb_head->cmd               = command;
        tp->acb_head->subcmd            = subcommand;

        err = smctr_issue_resume_acb_cmd(dev);

        return (err);
}

/*
 * This function can not be called with the adapter busy.
 */
static int smctr_setup_single_cmd_w_data(struct net_device *dev,
        __u16 command, __u16 subcommand)
{
        struct net_local *tp = netdev_priv(dev);

        tp->acb_head->cmd_done_status   = ACB_COMMAND_NOT_DONE;
        tp->acb_head->cmd               = command;
        tp->acb_head->subcmd            = subcommand;
        tp->acb_head->data_offset_lo
                = (__u16)TRC_POINTER(tp->misc_command_data);

        return(smctr_issue_resume_acb_cmd(dev));
}

static char *smctr_malloc(struct net_device *dev, __u16 size)
{
        struct net_local *tp = netdev_priv(dev);
        char *m;

        m = (char *)(tp->ram_access + tp->sh_mem_used);
        tp->sh_mem_used += (__u32)size;

        return (m);
}

static int smctr_status_chg(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_status_chg\n", dev->name);

        switch(tp->status)
        {
                case OPEN:
                        break;

                case CLOSED:
                        break;

                /* Interrupt driven open() completion. XXX */
                case INITIALIZED:
                        tp->group_address_0 = 0;
                        tp->group_address[0] = 0;
                        tp->group_address[1] = 0;
                        tp->functional_address_0 = 0;
                        tp->functional_address[0] = 0;
                        tp->functional_address[1] = 0;
                        smctr_open_tr(dev);
                        break;

                default:
                        printk(KERN_INFO "%s: status change unknown %x\n",
                                dev->name, tp->status);
                        break;
        }

        return (0);
}

static int smctr_trc_send_packet(struct net_device *dev, FCBlock *fcb,
        __u16 queue)
{
        struct net_local *tp = netdev_priv(dev);
        int err = 0;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_trc_send_packet\n", dev->name);

        fcb->info = FCB_CHAIN_END | FCB_ENABLE_TFS;
        if(tp->num_tx_fcbs[queue] != 1)
                fcb->back_ptr->info = FCB_INTERRUPT_ENABLE | FCB_ENABLE_TFS;

        if(tp->tx_queue_status[queue] == NOT_TRANSMITING)
        {
                tp->tx_queue_status[queue] = TRANSMITING;
                err = smctr_issue_resume_tx_fcb_cmd(dev, queue);
        }

        return (err);
}

static __u16 smctr_tx_complete(struct net_device *dev, __u16 queue)
{
        struct net_local *tp = netdev_priv(dev);
        __u16 status, err = 0;
        int cstatus;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_tx_complete\n", dev->name);

        while((status = tp->tx_fcb_end[queue]->frame_status) != SUCCESS)
        {
                if(status & 0x7e00 )
                {
                        err = HARDWARE_FAILED;
                        break;
                }

                if((err = smctr_update_tx_chain(dev, tp->tx_fcb_end[queue],
                        queue)) != SUCCESS)
                        break;

                smctr_disable_16bit(dev);

                if(tp->mode_bits & UMAC)
                {
                        if(!(status & (FCB_TX_STATUS_AR1 | FCB_TX_STATUS_AR2)))
                                cstatus = NO_SUCH_DESTINATION;
                        else
                        {
                                if(!(status & (FCB_TX_STATUS_CR1 | FCB_TX_STATUS_CR2)))
                                        cstatus = DEST_OUT_OF_RESOURCES;
                                else
                                {
                                        if(status & FCB_TX_STATUS_E)
                                                cstatus = MAX_COLLISIONS;
                                        else
                                                cstatus = SUCCESS;
                                }
                        }
                }
                else
                        cstatus = SUCCESS;

                if(queue == BUG_QUEUE)
                        err = SUCCESS;

                smctr_enable_16bit(dev);
                if(err != SUCCESS)
                        break;
        }

        return (err);
}

static unsigned short smctr_tx_move_frame(struct net_device *dev,
        struct sk_buff *skb, __u8 *pbuff, unsigned int bytes)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int ram_usable;
        __u32 flen, len, offset = 0;
        __u8 *frag, *page;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_tx_move_frame\n", dev->name);

        ram_usable = ((unsigned int)tp->ram_usable) << 10;
        frag       = skb->data;
        flen       = skb->len;

        while(flen > 0 && bytes > 0)
        {
                smctr_set_page(dev, pbuff);

                offset = SMC_PAGE_OFFSET(pbuff);

                if(offset + flen > ram_usable)
                        len = ram_usable - offset;
                else
                        len = flen;

                if(len > bytes)
                        len = bytes;

                page = (char *) (offset + tp->ram_access);
                memcpy(page, frag, len);

                flen -=len;
                bytes -= len;
                frag += len;
                pbuff += len;
        }

        return (0);
}

/* Update the error statistic counters for this adapter. */
static int smctr_update_err_stats(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        struct tr_statistics *tstat = &tp->MacStat;

        if(tstat->internal_errors)
                tstat->internal_errors
                        += *(tp->misc_command_data + 0) & 0x00ff;

        if(tstat->line_errors)
                tstat->line_errors += *(tp->misc_command_data + 0) >> 8;

        if(tstat->A_C_errors)
                tstat->A_C_errors += *(tp->misc_command_data + 1) & 0x00ff;

        if(tstat->burst_errors)
                tstat->burst_errors += *(tp->misc_command_data + 1) >> 8;

        if(tstat->abort_delimiters)
                tstat->abort_delimiters += *(tp->misc_command_data + 2) >> 8;

        if(tstat->recv_congest_count)
                tstat->recv_congest_count
                        += *(tp->misc_command_data + 3) & 0x00ff;

        if(tstat->lost_frames)
                tstat->lost_frames
                        += *(tp->misc_command_data + 3) >> 8;

        if(tstat->frequency_errors)
                tstat->frequency_errors += *(tp->misc_command_data + 4) & 0x00ff;

        if(tstat->frame_copied_errors)
                 tstat->frame_copied_errors
                        += *(tp->misc_command_data + 4) >> 8;

        if(tstat->token_errors)
                tstat->token_errors += *(tp->misc_command_data + 5) >> 8;

        return (0);
}

static int smctr_update_rx_chain(struct net_device *dev, __u16 queue)
{
        struct net_local *tp = netdev_priv(dev);
        FCBlock *fcb;
        BDBlock *bdb;
        __u16 size, len;

        fcb = tp->rx_fcb_curr[queue];
        len = fcb->frame_length;

        fcb->frame_status = 0;
        fcb->info = FCB_CHAIN_END;
        fcb->back_ptr->info = FCB_WARNING;

        tp->rx_fcb_curr[queue] = tp->rx_fcb_curr[queue]->next_ptr;

        /* update RX BDBs */
        size = (len >> RX_BDB_SIZE_SHIFT);
        if(len & RX_DATA_BUFFER_SIZE_MASK)
                size += sizeof(BDBlock);
        size &= (~RX_BDB_SIZE_MASK);

        /* check if wrap around */
        bdb = (BDBlock *)((__u32)(tp->rx_bdb_curr[queue]) + (__u32)(size));
        if((__u32)bdb >= (__u32)tp->rx_bdb_end[queue])
        {
                bdb = (BDBlock *)((__u32)(tp->rx_bdb_head[queue])
                        + (__u32)(bdb) - (__u32)(tp->rx_bdb_end[queue]));
        }

        bdb->back_ptr->info = BDB_CHAIN_END;
        tp->rx_bdb_curr[queue]->back_ptr->info = BDB_NOT_CHAIN_END;
        tp->rx_bdb_curr[queue] = bdb;

        return (0);
}

static int smctr_update_tx_chain(struct net_device *dev, FCBlock *fcb,
        __u16 queue)
{
        struct net_local *tp = netdev_priv(dev);

        if(smctr_debug > 20)
                printk(KERN_DEBUG "smctr_update_tx_chain\n");

        if(tp->num_tx_fcbs_used[queue] <= 0)
                return (HARDWARE_FAILED);
        else
        {
                if(tp->tx_buff_used[queue] < fcb->memory_alloc)
                {
                        tp->tx_buff_used[queue] = 0;
                        return (HARDWARE_FAILED);
                }

                tp->tx_buff_used[queue] -= fcb->memory_alloc;

                /* if all transmit buffer are cleared
                 * need to set the tx_buff_curr[] to tx_buff_head[]
                 * otherwise, tx buffer will be segregate and cannot
                 * accommodate and buffer greater than (curr - head) and
                 * (end - curr) since we do not allow wrap around allocation.
                 */
                if(tp->tx_buff_used[queue] == 0)
                        tp->tx_buff_curr[queue] = tp->tx_buff_head[queue];

                tp->num_tx_fcbs_used[queue]--;
                fcb->frame_status = 0;
                tp->tx_fcb_end[queue] = fcb->next_ptr;
		netif_wake_queue(dev);
                return (0);
        }
}

static int smctr_wait_cmd(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int loop_count = 0x20000;

        if(smctr_debug > 10)
                printk(KERN_DEBUG "%s: smctr_wait_cmd\n", dev->name);

        while(loop_count)
        {
                if(tp->acb_head->cmd_done_status & ACB_COMMAND_DONE)
                        break;
		udelay(1);
                loop_count--;
        }

        if(loop_count == 0)
                return(HARDWARE_FAILED);

        if(tp->acb_head->cmd_done_status & 0xff)
                return(HARDWARE_FAILED);

        return (0);
}

static int smctr_wait_while_cbusy(struct net_device *dev)
{
        struct net_local *tp = netdev_priv(dev);
        unsigned int timeout = 0x20000;
        int ioaddr = dev->base_addr;
        __u8 r;

        if(tp->bic_type == BIC_585_CHIP)
        {
                while(timeout)
                {
                        r = inb(ioaddr + HWR);
                        if((r & HWR_CBUSY) == 0)
                                break;
                        timeout--;
                }
        }
        else
        {
                while(timeout)
                {
                        r = inb(ioaddr + CSR);
                        if((r & CSR_CBUSY) == 0)
                                break;
                        timeout--;
                }
        }

        if(timeout)
                return (0);
        else
                return (HARDWARE_FAILED);
}

#ifdef MODULE

static struct net_device* dev_smctr[SMCTR_MAX_ADAPTERS];
static int io[SMCTR_MAX_ADAPTERS];
static int irq[SMCTR_MAX_ADAPTERS];

MODULE_LICENSE("GPL");
MODULE_FIRMWARE("tr_smctr.bin");

module_param_array(io, int, NULL, 0);
module_param_array(irq, int, NULL, 0);
module_param(ringspeed, int, 0);

static struct net_device * __init setup_card(int n)
{
	struct net_device *dev = alloc_trdev(sizeof(struct net_local));
	int err;
	
	if (!dev)
		return ERR_PTR(-ENOMEM);

	dev->irq = irq[n];
	err = smctr_probe1(dev, io[n]);
	if (err) 
		goto out;
		
	err = register_netdev(dev);
	if (err)
		goto out1;
	return dev;
 out1:
#ifdef CONFIG_MCA_LEGACY
	{ struct net_local *tp = netdev_priv(dev);
	  if (tp->slot_num)
		mca_mark_as_unused(tp->slot_num);
	}
#endif
	release_region(dev->base_addr, SMCTR_IO_EXTENT);
	free_irq(dev->irq, dev);
out:
	free_netdev(dev);
	return ERR_PTR(err);
}

int __init init_module(void)
{
        int i, found = 0;
	struct net_device *dev;

        for(i = 0; i < SMCTR_MAX_ADAPTERS; i++) {
		dev = io[0]? setup_card(i) : smctr_probe(-1);
		if (!IS_ERR(dev)) {
			++found;
			dev_smctr[i] = dev;
		}
        }

        return found ? 0 : -ENODEV;
}

void __exit cleanup_module(void)
{
        int i;

        for(i = 0; i < SMCTR_MAX_ADAPTERS; i++) {
		struct net_device *dev = dev_smctr[i];

		if (dev) {

			unregister_netdev(dev);
#ifdef CONFIG_MCA_LEGACY
			{ struct net_local *tp = netdev_priv(dev);
			if (tp->slot_num)
				mca_mark_as_unused(tp->slot_num);
			}
#endif
			release_region(dev->base_addr, SMCTR_IO_EXTENT);
			if (dev->irq)
				free_irq(dev->irq, dev);

			free_netdev(dev);
		}
        }
}
#endif /* MODULE */