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

#include <asm/system.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/string.h>
#include <linux/malloc.h>
#include <asm/irq.h>
#include <asm/dma.h>
#include <linux/ioport.h>

#include "../block/blk.h"
#include "scsi.h"
#include "hosts.h"
#include "constants.h"

#undef USE_STATIC_SCSI_MEMORY

/*
static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/scsi.c,v 1.5 1993/09/24 12:45:18 drew Exp drew $";
*/

/* Command groups 3 and 4 are reserved and should never be used.  */
const unsigned char scsi_command_size[8] = { 6, 10, 10, 12, 12, 12, 10, 10 };

#define INTERNAL_ERROR (panic ("Internal error in file %s, line %d.\n", __FILE__, __LINE__))

static void scsi_done (Scsi_Cmnd *SCpnt);
static int update_timeout (Scsi_Cmnd *, int);
static void print_inquiry(unsigned char *data);
static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid);

static int time_start;
static int time_elapsed;
static volatile struct Scsi_Host * host_active = NULL;
#define SCSI_BLOCK(HOST) ((HOST->block && host_active && HOST != host_active) \
		   || (HOST->can_queue && HOST->host_busy >= HOST->can_queue))

#define MAX_SCSI_DEVICE_CODE 10
const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
{
 "Direct-Access    ",
 "Sequential-Access",
 "Printer          ",
 "Processor        ",
 "WORM             ",
 "CD-ROM           ",
 "Scanner          ",
 "Optical Device   ",
 "Medium Changer   ",
 "Communications   "
};


/*
	global variables :
	scsi_devices an array of these specifying the address for each
	(host, id, LUN)
*/

Scsi_Device * scsi_devices = NULL;

/* Process ID of SCSI commands */
unsigned long scsi_pid = 0;

static unsigned char generic_sense[6] = {REQUEST_SENSE, 0,0,0, 255, 0};

/* This variable is merely a hook so that we can debug the kernel with gdb. */
Scsi_Cmnd * last_cmnd = NULL;

/*
 *	As the scsi do command functions are intelligent, and may need to
 *	redo a command, we need to keep track of the last command
 *	executed on each one.
 */

#define WAS_RESET	0x01
#define WAS_TIMEDOUT	0x02
#define WAS_SENSE	0x04
#define IS_RESETTING	0x08
#define IS_ABORTING	0x10
#define ASKED_FOR_SENSE 0x20

/*
 *	This is the number  of clock ticks we should wait before we time out
 *	and abort the command.  This is for  where the scsi.c module generates
 *	the command, not where it originates from a higher level, in which
 *	case the timeout is specified there.
 *
 *	ABORT_TIMEOUT and RESET_TIMEOUT are the timeouts for RESET and ABORT
 *	respectively.
 */

#ifdef DEBUG_TIMEOUT
static void scsi_dump_status(void);
#endif


#ifdef DEBUG
	#define SCSI_TIMEOUT (5*HZ)
#else
	#define SCSI_TIMEOUT (1*HZ)
#endif

#ifdef DEBUG
	#define SENSE_TIMEOUT SCSI_TIMEOUT
	#define ABORT_TIMEOUT SCSI_TIMEOUT
	#define RESET_TIMEOUT SCSI_TIMEOUT
#else
	#define SENSE_TIMEOUT (5*HZ/10)
	#define RESET_TIMEOUT (5*HZ/10)
	#define ABORT_TIMEOUT (5*HZ/10)
#endif

#define MIN_RESET_DELAY (1*HZ)

/* Do not call reset on error if we just did a reset within 10 sec. */
#define MIN_RESET_PERIOD (10*HZ)

/* The following devices are known not to tolerate a lun != 0 scan for
   one reason or another.  Some will respond to all luns, others will
   lock up. */

     struct blist{
       char * vendor;
       char * model;
       char * revision; /* Latest revision known to be bad.  Not used yet */
     };

static struct blist blacklist[] =
{
   {"CHINON","CD-ROM CDS-431","H42"},  /* Locks up if polled for lun != 0 */
   {"CHINON","CD-ROM CDS-535","Q14"}, /* Lockup if polled for lun != 0 */
   {"DENON","DRD-25X","V"},   /* A cdrom that locks up when probed at lun != 0 */
   {"HITACHI","DK312C","CM81"},   /* Responds to all lun - dtg */
   {"HITACHI","DK314C","CR21" }, /* responds to all lun */
   {"IMS", "CDD521/10","2.06"},   /* Locks-up when LUN>0 polled. */
   {"MAXTOR","XT-3280","PR02"},   /* Locks-up when LUN>0 polled. */
   {"MAXTOR","XT-4380S","B3C"},   /* Locks-up when LUN>0 polled. */
   {"MAXTOR","MXT-1240S","I1.2"}, /* Locks up when LUN > 0 polled */
   {"MAXTOR","XT-4170S","B5A"},   /* Locks-up sometimes when LUN>0 polled. */
   {"MAXTOR","XT-8760S","B7B"},   /* guess what? */
   {"NEC","CD-ROM DRIVE:841","1.0"},  /* Locks-up when LUN>0 polled. */
   {"RODIME","RO3000S","2.33"},  /* Locks up if polled for lun != 0 */
   {"SEAGATE", "ST157N", "\004|j"}, /* causes failed REQUEST SENSE on lun 1 for aha152x
				     * controller, which causes SCSI code to reset bus.*/
   {"SEAGATE", "ST296","921"},   /* Responds to all lun */
   {"SONY","CD-ROM CDU-541","4.3d"},
   {"SONY","CD-ROM CDU-55S","1.0i"},
   {"TANDBERG","TDC 3600","U07"},  /* Locks up if polled for lun != 0 */
   {"TEAC","CD-ROM","1.06"},	/* causes failed REQUEST SENSE on lun 1 for seagate
				 * controller, which causes SCSI code to reset bus.*/
   {"TEXEL","CD-ROM","1.06"},   /* causes failed REQUEST SENSE on lun 1 for seagate
				 * controller, which causes SCSI code to reset bus.*/
   {"QUANTUM","LPS525S","3110"},/* Locks sometimes if polled for lun != 0 */
   {"QUANTUM","PD1225S","3110"},/* Locks sometimes if polled for lun != 0 */
   {"MEDIAVIS","CDR-H93MV","1.31"},  /* Locks up if polled for lun != 0 */
   {"SANKYO", "CP525","6.64"},  /* causes failed REQ SENSE, extra reset */
   {"HP", "C1750A", "3226"},    /* scanjet iic */
   {"HP", "C1790A", ""},	/* scanjet iip */
   {"HP", "C2500A", ""},	/* scanjet iicx */
   {NULL, NULL, NULL}};

static int blacklisted(unsigned char * response_data){
  int i = 0;
  unsigned char * pnt;
  for(i=0; 1; i++){
    if(blacklist[i].vendor == NULL) return 0;
    pnt = &response_data[8];
    while(*pnt && *pnt == ' ') pnt++;
    if(memcmp(blacklist[i].vendor, pnt,
	       strlen(blacklist[i].vendor))) continue;
    pnt = &response_data[16];
    while(*pnt && *pnt == ' ') pnt++;
    if(memcmp(blacklist[i].model, pnt,
	       strlen(blacklist[i].model))) continue;
    return 1;
  }
}

/*
 *	As the actual SCSI command runs in the background, we must set up a
 *	flag that tells scan_scsis() when the result it has is valid.
 *	scan_scsis can set the_result to -1, and watch for it to become the
 *	actual return code for that call.  the scan_scsis_done function() is
 *	our user specified completion function that is passed on to the
 *	scsi_do_cmd() function.
 */

volatile int in_scan_scsis = 0;
static int the_result;

void scsi_make_blocked_list(void)  {
  int block_count = 0, index;
  unsigned int flags;
  struct Scsi_Host * sh[128], * shpnt;

  /*
   * Create a circular linked list from the scsi hosts which have
   * the "wish_block" field in the Scsi_Host structure set.
   * The blocked list should include all the scsi hosts using ISA DMA.
   * In some systems, using two dma channels simultaneously causes
   * unpredictable results.
   * Among the scsi hosts in the blocked list, only one host at a time
   * is allowed to have active commands queued. The transition from
   * one active host to the next one is allowed only when host_busy == 0
   * for the active host (which implies host_busy == 0 for all the hosts
   * in the list). Moreover for block devices the transition to a new
   * active host is allowed only when a request is completed, since a
   * block device request can be divided into multiple scsi commands
   * (when there are few sg lists or clustering is disabled).
   *
   * (DB, 4 Feb 1995)
   */

  save_flags(flags);
  cli();
  host_active = NULL;

  for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next) {

#if 0
     /*
      * Is this is a candidate for the blocked list?
      * Useful to put into the blocked list all the hosts whose driver
      * does not know about the host->block feature.
      */
     if (shpnt->unchecked_isa_dma) shpnt->wish_block = 1;
#endif

     if (shpnt->wish_block) sh[block_count++] = shpnt;
     }

  if (block_count == 1) sh[0]->block = NULL;

  else if (block_count > 1) {

     for(index = 0; index < block_count - 1; index++) {
	sh[index]->block = sh[index + 1];
	printk("scsi%d : added to blocked host list.\n",
	       sh[index]->host_no);
	}

     sh[block_count - 1]->block = sh[0];
     printk("scsi%d : added to blocked host list.\n",
	    sh[index]->host_no);
     }

  restore_flags(flags);
  }

static void scan_scsis_done (Scsi_Cmnd * SCpnt)
	{

#ifdef DEBUG
	printk ("scan_scsis_done(%p, %06x)\n", SCpnt->host, SCpnt->result);
#endif
	SCpnt->request.dev = 0xfffe;

	if (SCpnt->request.sem != NULL)
	  up(SCpnt->request.sem);
	}

#ifdef CONFIG_SCSI_MULTI_LUN
static int max_scsi_luns = 8;
#else
static int max_scsi_luns = 1;
#endif

void scsi_luns_setup(char *str, int *ints) {
    if (ints[0] != 1)
	printk("scsi_luns_setup : usage max_scsi_luns=n (n should be between 1 and 8)\n");
    else
	max_scsi_luns = ints[1];
}

/*
 *	Detecting SCSI devices :
 *	We scan all present host adapter's busses,  from ID 0 to ID 6.
 *	We use the INQUIRY command, determine device type, and pass the ID /
 *	lun address of all sequential devices to the tape driver, all random
 *	devices to the disk driver.
 */

void scan_scsis (struct Scsi_Host * shpnt)
{
  int dev, lun, type;
  unsigned char scsi_cmd [12];
  unsigned char scsi_result0 [256];
  unsigned char * scsi_result;
  Scsi_Device * SDpnt, *SDtail;
  struct Scsi_Device_Template * sdtpnt;
  Scsi_Cmnd  *SCpnt;

  ++in_scan_scsis;
  lun = 0;
  type = -1;
  SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC|GFP_DMA);
  SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device), GFP_ATOMIC);
  SDtail = scsi_devices;

  if(scsi_devices) while(SDtail->next) SDtail = SDtail->next;

  /* Make sure we have something that is valid for DMA purposes */
  scsi_result = ((current == task[0]  || !shpnt->unchecked_isa_dma)
		 ?  &scsi_result0[0] : scsi_malloc(512));


  shpnt->host_queue = SCpnt;  /* We need this so that commands can time out */

  for (dev = 0; dev < 8; ++dev)
    if (shpnt->this_id != dev)

/*
 * We need the for so our continue, etc. work fine.
 */
      for (lun = 0; lun < max_scsi_luns; ++lun)
	{
	  memset(SDpnt, 0, sizeof(Scsi_Device));
	  SDpnt->host = shpnt;
	  SDpnt->id = dev;
	  SDpnt->lun = lun;

	  /* Some low level driver could use device->type (DB) */
	  SDpnt->type = -1;
/*
 * Assume that the device will have handshaking problems, and then
 * fix this field later if it turns out it doesn't.
 */
	  SDpnt->borken = 1;

	  scsi_cmd[0] = TEST_UNIT_READY;
	  scsi_cmd[1] = lun << 5;
	  scsi_cmd[2] = scsi_cmd[3] = scsi_cmd[4] = scsi_cmd[5] = 0;

	  memset(SCpnt, 0,  sizeof(Scsi_Cmnd));
	  SCpnt->host = SDpnt->host;
	  SCpnt->device = SDpnt;
	  SCpnt->target = SDpnt->id;
	  SCpnt->lun = SDpnt->lun;

	  /* Used for mutex if loading devices after boot */
	  SCpnt->request.sem = NULL;

	  SCpnt->request.dev = 0xffff; /* Mark not busy */

	  scsi_do_cmd (SCpnt, (void *)  scsi_cmd, (void *) scsi_result,
		       256,  scan_scsis_done, SCSI_TIMEOUT + 4 * HZ, 5);

	  /* Wait for command to finish. Use simple wait if we are booting, else
	     do it right and use a mutex */

	  if (current == task[0])
	      while (SCpnt->request.dev != 0xfffe) barrier();
	  else if (SCpnt->request.dev != 0xfffe) {
	      struct semaphore sem = MUTEX_LOCKED;

	      SCpnt->request.sem = &sem;
	      down(&sem);

	      /* Hmm.. Have to ask about this one */
	      while (SCpnt->request.dev != 0xfffe) schedule();
	      }

#if defined(DEBUG) || defined(DEBUG_INIT)
	  printk("scsi: scan SCSIS id %d lun %d\n", dev, lun);
	  printk("scsi: return code %08x\n", SCpnt->result);
#endif


	  if(SCpnt->result) {
	    if (((driver_byte(SCpnt->result) & DRIVER_SENSE) ||
		 (status_byte(SCpnt->result) & CHECK_CONDITION)) &&
		((SCpnt->sense_buffer[0] & 0x70) >> 4) == 7) {
	      if (SCpnt->sense_buffer[2] &0xe0)
		continue; /* No devices here... */
	      if(((SCpnt->sense_buffer[2] & 0xf) != NOT_READY) &&
		 ((SCpnt->sense_buffer[2] & 0xf) != UNIT_ATTENTION))
		continue;
	    }
	    else
	      break;
	  }

#if defined (DEBUG) || defined(DEBUG_INIT)
	  printk("scsi: performing INQUIRY\n");
#endif

	  /*
	   * Build an INQUIRY command block.
	   */
	  scsi_cmd[0] = INQUIRY;
	  scsi_cmd[1] = (lun << 5) & 0xe0;
	  scsi_cmd[2] = 0;
	  scsi_cmd[3] = 0;
	  scsi_cmd[4] = 255;
	  scsi_cmd[5] = 0;

	  SCpnt->request.dev = 0xffff; /* Mark not busy */
	  SCpnt->cmd_len = 0;

	  scsi_do_cmd (SCpnt, (void *)  scsi_cmd, (void *) scsi_result,
		       256,  scan_scsis_done, SCSI_TIMEOUT, 3);

	  if (current == task[0])
	      while (SCpnt->request.dev != 0xfffe) barrier();
	  else if (SCpnt->request.dev != 0xfffe) {
	      struct semaphore sem = MUTEX_LOCKED;

	      SCpnt->request.sem = &sem;
	      down(&sem);

	      /* Hmm.. Have to ask about this one */
	      while (SCpnt->request.dev != 0xfffe) schedule();
	      }

	  the_result = SCpnt->result;

#if defined(DEBUG) || defined(DEBUG_INIT)
	  if (!the_result)
	    printk("scsi: INQUIRY successful\n");
	  else
	    printk("scsi: INQUIRY failed with code %08x\n", the_result);
#endif

	  if(the_result) break;

	  /* skip other luns on this device */

	  if (!the_result)
	    {
		/* It would seem some TOSHIBA CD-ROM gets things wrong */
		if (!strncmp(scsi_result+8,"TOSHIBA",7) &&
		    !strncmp(scsi_result+16,"CD-ROM",6) &&
		    scsi_result[0] == TYPE_DISK) {
			scsi_result[0] = TYPE_ROM;
			scsi_result[1] |= 0x80;  /* removable */
		}

	      if (!strncmp(scsi_result+8,"NEC",3)) {
		  if (!strncmp(scsi_result+16,"CD-ROM DRIVE:84 ",16) ||
		      !strncmp(scsi_result+16,"CD-ROM DRIVE:25",15))
		      SDpnt->manufacturer = SCSI_MAN_NEC_OLDCDR;
		  else
		      SDpnt->manufacturer = SCSI_MAN_NEC;
	      } else if (!strncmp(scsi_result+8,"TOSHIBA",7))
		  SDpnt->manufacturer = SCSI_MAN_TOSHIBA;
	      else
		  SDpnt->manufacturer = SCSI_MAN_UNKNOWN;

	      SDpnt->removable = (0x80 &
				  scsi_result[1]) >> 7;
	      SDpnt->lockable = SDpnt->removable;
	      SDpnt->changed = 0;
	      SDpnt->access_count = 0;
	      SDpnt->busy = 0;
/*
 *	Currently, all sequential devices are assumed to be tapes,
 *	all random devices disk, with the appropriate read only
 *	flags set for ROM / WORM treated as RO.
 */

	      switch (type = (scsi_result[0] & 0x1f))
		{
		case TYPE_TAPE :
		case TYPE_DISK :
		case TYPE_MOD :
		case TYPE_PROCESSOR :
		case TYPE_SCANNER :
		  SDpnt->writeable = 1;
		  break;
		case TYPE_WORM :
		case TYPE_ROM :
		  SDpnt->writeable = 0;
		  break;
		default :
#if 0
#ifdef DEBUG
		  printk("scsi: unknown type %d\n", type);
		  print_inquiry(scsi_result);
#endif
		  type = -1;
#endif
		}

	      SDpnt->soft_reset =
		(scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
	      SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
	      SDpnt->type = (type & 0x1f);

	      if (type != -1)
		{
		  print_inquiry(scsi_result);

		  for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
		    if(sdtpnt->detect) SDpnt->attached +=
		      (*sdtpnt->detect)(SDpnt);

		  SDpnt->scsi_level = scsi_result[2] & 0x07;
		  if (SDpnt->scsi_level >= 2 ||
		      (SDpnt->scsi_level == 1 &&
		       (scsi_result[3] & 0x0f) == 1))
		    SDpnt->scsi_level++;
/*
 * Set the tagged_queue flag for SCSI-II devices that purport to support
 * tagged queuing in the INQUIRY data.
 */

		  SDpnt->tagged_queue = 0;

		  if ((SDpnt->scsi_level >= SCSI_2) &&
		      (scsi_result[7] & 2)) {
		    SDpnt->tagged_supported = 1;
		    SDpnt->current_tag = 0;
		  }

/*
 * Accommodate drivers that want to sleep when they should be in a polling
 * loop.
 */

		  SDpnt->disconnect = 0;

/*
 * Some revisions of the Texel CD ROM drives have handshaking
 * problems when used with the Seagate controllers.  Before we
 * know what type of device we're talking to, we assume it's
 * borken and then change it here if it turns out that it isn't
 * a TEXEL drive.
 */

		  if(strncmp("TEXEL", (char *) &scsi_result[8], 5) != 0 ||
		     strncmp("CD-ROM", (char *) &scsi_result[16], 6) != 0
/*
 * XXX 1.06 has problems, some one should figure out the others too so
 * ALL TEXEL drives don't suffer in performance, especially when I finish
 * integrating my seagate patches which do multiple I_T_L nexuses.
 */

#ifdef notyet
		     || (strncmp("1.06", (char *) &scsi_result[[, 4) != 0)))
#endif
				 )
		    SDpnt->borken = 0;


		  /* These devices need this "key" to unlock the device
		     so we can use it */
		  if(memcmp("INSITE", &scsi_result[8], 6) == 0 &&
		     (memcmp("Floptical   F*8I", &scsi_result[16], 16) == 0
		      || memcmp("I325VM", &scsi_result[16], 6) == 0)) {
		    printk("Unlocked floptical drive.\n");
		    SDpnt->lockable = 0;
		    scsi_cmd[0] = MODE_SENSE;
		    scsi_cmd[1] = (lun << 5) & 0xe0;
		    scsi_cmd[2] = 0x2e;
		    scsi_cmd[3] = 0;
		    scsi_cmd[4] = 0x2a;
		    scsi_cmd[5] = 0;

		    SCpnt->request.dev = 0xffff; /* Mark not busy */
		    SCpnt->cmd_len = 0;

		    scsi_do_cmd (SCpnt, (void *)  scsi_cmd,
				 (void *) scsi_result, 0x2a,  scan_scsis_done,
				 SCSI_TIMEOUT, 3);

		    if (current == task[0])
			while (SCpnt->request.dev != 0xfffe) barrier();
		    else if (SCpnt->request.dev != 0xfffe) {
			struct semaphore sem = MUTEX_LOCKED;

			SCpnt->request.sem = &sem;
			down(&sem);

			/* Hmm.. Have to ask about this one */
			while (SCpnt->request.dev != 0xfffe) schedule();
			}
		  }
		  /* Add this device to the linked list at the end */
		  if(SDtail)
		    SDtail->next = SDpnt;
		  else
		    scsi_devices = SDpnt;
		  SDtail = SDpnt;

		  SDpnt = (Scsi_Device *) scsi_init_malloc(sizeof (Scsi_Device), GFP_ATOMIC);
		  /* Some scsi devices cannot be polled for lun != 0
		     due to firmware bugs */
		  if(blacklisted(scsi_result)) break;
		  /* Old drives like the MAXTOR XT-3280 say vers=0 */
		  if ((scsi_result[2] & 0x07) == 0)
		    break;
		  /* Some scsi-1 peripherals do not handle lun != 0.
		     I am assuming that scsi-2 peripherals do better */
		  if((scsi_result[2] & 0x07) == 1 &&
		     (scsi_result[3] & 0x0f) == 0) break;
		}
	    }       /* if result == DID_OK ends */
	}       /* for lun ends */

  shpnt->host_queue = NULL;  /* No longer needed here */

  /* Last device block does not exist.  Free memory. */
  scsi_init_free((char *) SDpnt, sizeof(Scsi_Device));

  scsi_init_free((char *) SCpnt, sizeof(Scsi_Cmnd));


  /* If we allocated a buffer so we could do DMA, free it now */
  if (scsi_result != &scsi_result0[0]) scsi_free(scsi_result, 512);

  in_scan_scsis = 0;
}       /* scan_scsis  ends */

/*
 *	Flag bits for the internal_timeout array
 */

#define NORMAL_TIMEOUT 0
#define IN_ABORT 1
#define IN_RESET 2
/*
	This is our time out function, called when the timer expires for a
	given host adapter.  It will attempt to abort the currently executing
	command, that failing perform a kernel panic.
*/

static void scsi_times_out (Scsi_Cmnd * SCpnt, int pid)
	{

	switch (SCpnt->internal_timeout & (IN_ABORT | IN_RESET))
		{
		case NORMAL_TIMEOUT:
			if (!in_scan_scsis) {
#ifdef DEBUG_TIMEOUT
			  scsi_dump_status();
#endif
			}

			if (!scsi_abort	(SCpnt, DID_TIME_OUT, pid))
				return;
		case IN_ABORT:
			printk("SCSI host %d abort() timed out - resetting\n",
				SCpnt->host->host_no);
			if (!scsi_reset (SCpnt))
				return;
		case IN_RESET:
		case (IN_ABORT | IN_RESET):
		  /* This might be controversial, but if there is a bus hang,
		     you might conceivably want the machine up and running
		     esp if you have an ide disk. */
			printk("Unable to reset scsi host %d - ",SCpnt->host->host_no);
			printk("probably a SCSI bus hang.\n");
			return;

		default:
			INTERNAL_ERROR;
		}

	}


/* This function takes a quick look at a request, and decides if it
can be queued now, or if there would be a stall while waiting for
something else to finish.  This routine assumes that interrupts are
turned off when entering the routine.  It is the responsibility
of the calling code to ensure that this is the case. */

Scsi_Cmnd * request_queueable (struct request * req, Scsi_Device * device)
{
  Scsi_Cmnd * SCpnt = NULL;
  int tablesize;
  struct buffer_head * bh, *bhp;

  if (!device)
    panic ("No device passed to request_queueable().\n");

  if (req && req->dev <= 0)
    panic("Invalid device in request_queueable");

  SCpnt =  device->host->host_queue;
    while(SCpnt){
      if(SCpnt->target == device->id &&
	 SCpnt->lun == device->lun)
	if(SCpnt->request.dev < 0) break;
      SCpnt = SCpnt->next;
    }

  if (!SCpnt) return NULL;

  if (SCSI_BLOCK(device->host)) return NULL;

  if (req) {
    memcpy(&SCpnt->request, req, sizeof(struct request));
    tablesize = device->host->sg_tablesize;
    bhp = bh = req->bh;
    if(!tablesize) bh = NULL;
    /* Take a quick look through the table to see how big it is.  We already
       have our copy of req, so we can mess with that if we want to.  */
    while(req->nr_sectors && bh){
	    bhp = bhp->b_reqnext;
	    if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
	    req->nr_sectors -= bh->b_size >> 9;
	    req->sector += bh->b_size >> 9;
	    if(!tablesize) break;
	    bh = bhp;
    }
    if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
      SCpnt->request.bhtail = bh;
      req->bh = bh->b_reqnext; /* Divide request */
      bh->b_reqnext = NULL;
      bh = req->bh;

      /* Now reset things so that req looks OK */
      SCpnt->request.nr_sectors -= req->nr_sectors;
      req->current_nr_sectors = bh->b_size >> 9;
      req->buffer = bh->b_data;
      SCpnt->request.sem = NULL; /* Wait until whole thing done */
    } else {
      req->dev = -1;
      wake_up(&wait_for_request);
    }
  } else {
    SCpnt->request.dev = 0xffff; /* Busy, but no request */
    SCpnt->request.sem = NULL;  /* And no one is waiting for the device either */
  }

  SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
  SCpnt->old_use_sg  = 0;
  SCpnt->transfersize = 0;
  SCpnt->underflow = 0;
  SCpnt->cmd_len = 0;
  return SCpnt;
}

/* This function returns a structure pointer that will be valid for
the device.  The wait parameter tells us whether we should wait for
the unit to become free or not.  We are also able to tell this routine
not to return a descriptor if the host is unable to accept any more
commands for the time being.  We need to keep in mind that there is no
guarantee that the host remain not busy.  Keep in mind the
request_queueable function also knows the internal allocation scheme
of the packets for each device */

Scsi_Cmnd * allocate_device (struct request ** reqp, Scsi_Device * device,
			     int wait)
{
  int dev = -1;
  struct request * req = NULL;
  int tablesize;
  unsigned int flags;
  struct buffer_head * bh, *bhp;
  struct Scsi_Host * host;
  Scsi_Cmnd * SCpnt = NULL;
  Scsi_Cmnd * SCwait = NULL;

  if (!device)
    panic ("No device passed to allocate_device().\n");

  if (reqp) req = *reqp;

    /* See if this request has already been queued by an interrupt routine */
  if (req && (dev = req->dev) <= 0) return NULL;

  host = device->host;

  if (intr_count && SCSI_BLOCK(host)) return NULL;

  while (1==1){
    SCpnt = host->host_queue;
    while(SCpnt){
      if(SCpnt->target == device->id &&
	 SCpnt->lun == device->lun) {
	SCwait = SCpnt;
	if(SCpnt->request.dev < 0) break;
      }
      SCpnt = SCpnt->next;
    }
    save_flags(flags);
    cli();
    /* See if this request has already been queued by an interrupt routine */
    if (req && ((req->dev < 0) || (req->dev != dev))) {
      restore_flags(flags);
      return NULL;
    }
    if (!SCpnt || SCpnt->request.dev >= 0)  /* Might have changed */
      {
	restore_flags(flags);
	if(!wait) return NULL;
	if (!SCwait) {
	  printk("Attempt to allocate device target %d, lun %d\n",
		 device->id ,device->lun);
	  panic("No device found in allocate_device\n");
	}
	SCSI_SLEEP(&device->device_wait,
		   (SCwait->request.dev > 0));
      } else {
	if (req) {
	  memcpy(&SCpnt->request, req, sizeof(struct request));
	  tablesize = device->host->sg_tablesize;
	  bhp = bh = req->bh;
	  if(!tablesize) bh = NULL;
	  /* Take a quick look through the table to see how big it is.  We already
	     have our copy of req, so we can mess with that if we want to.  */
	  while(req->nr_sectors && bh){
	    bhp = bhp->b_reqnext;
	    if(!bhp || !CONTIGUOUS_BUFFERS(bh,bhp)) tablesize--;
	    req->nr_sectors -= bh->b_size >> 9;
	    req->sector += bh->b_size >> 9;
	    if(!tablesize) break;
	    bh = bhp;
	  }
	  if(req->nr_sectors && bh && bh->b_reqnext){  /* Any leftovers? */
	    SCpnt->request.bhtail = bh;
	    req->bh = bh->b_reqnext; /* Divide request */
	    bh->b_reqnext = NULL;
	    bh = req->bh;
	    /* Now reset things so that req looks OK */
	    SCpnt->request.nr_sectors -= req->nr_sectors;
	    req->current_nr_sectors = bh->b_size >> 9;
	    req->buffer = bh->b_data;
	    SCpnt->request.sem = NULL; /* Wait until whole thing done */
	  }
	  else
	    {
	      req->dev = -1;
	      *reqp = req->next;
	      wake_up(&wait_for_request);
	    }
	} else {
	  SCpnt->request.dev = 0xffff; /* Busy */
	  SCpnt->request.sem = NULL;  /* And no one is waiting for this to complete */
	}
	restore_flags(flags);
	break;
      }
  }

  SCpnt->use_sg = 0;  /* Reset the scatter-gather flag */
  SCpnt->old_use_sg  = 0;
  SCpnt->transfersize = 0;      /* No default transfer size */
  SCpnt->cmd_len = 0;
  SCpnt->underflow = 0;         /* Do not flag underflow conditions */
  return SCpnt;
}

/*
	This is inline because we have stack problemes if we recurse to deeply.
*/

inline void internal_cmnd (Scsi_Cmnd * SCpnt)
	{
	int temp;
	struct Scsi_Host * host;
	unsigned int flags;
#ifdef DEBUG_DELAY
	int clock;
#endif

	if ((unsigned long) &SCpnt < current->kernel_stack_page)
	  panic("Kernel stack overflow.");

	host = SCpnt->host;

/*
	We will wait MIN_RESET_DELAY clock ticks after the last reset so
	we can avoid the drive not being ready.
*/
save_flags(flags);
sti();
temp = host->last_reset + MIN_RESET_DELAY;
while (jiffies < temp);
restore_flags(flags);

update_timeout(SCpnt, SCpnt->timeout_per_command);

/*
	We will use a queued command if possible, otherwise we will emulate the
	queuing and calling of completion function ourselves.
*/
#ifdef DEBUG
	printk("internal_cmnd (host = %d, target = %d, command = %p, buffer = %p, \n"
		"bufflen = %d, done = %p)\n", SCpnt->host->host_no, SCpnt->target, SCpnt->cmnd, SCpnt->buffer, SCpnt->bufflen, SCpnt->done);
#endif

	if (host->can_queue)
		{
#ifdef DEBUG
	printk("queuecommand : routine at %p\n",
		host->hostt->queuecommand);
#endif
		  /* This locking tries to prevent all sorts of races between
		     queuecommand and the interrupt code.  In effect,
		     we are only allowed to be in queuecommand once at
		     any given time, and we can only be in the interrupt
		     handler and the queuecommand function at the same time
		     when queuecommand is called while servicing the
		     interrupt. */

		if(!intr_count && SCpnt->host->irq)
		  disable_irq(SCpnt->host->irq);

		host->hostt->queuecommand (SCpnt, scsi_done);

		if(!intr_count && SCpnt->host->irq)
		  enable_irq(SCpnt->host->irq);
		}
	else
		{

#ifdef DEBUG
	printk("command() :  routine at %p\n", host->hostt->command);
#endif
		temp=host->hostt->command (SCpnt);
		SCpnt->result = temp;
#ifdef DEBUG_DELAY
	clock = jiffies + 4*HZ;
	while (jiffies < clock);
	printk("done(host = %d, result = %04x) : routine at %08x\n", host->host_no, temp);
#endif
		scsi_done(SCpnt);
		}
#ifdef DEBUG
	printk("leaving internal_cmnd()\n");
#endif
	}

static void scsi_request_sense (Scsi_Cmnd * SCpnt)
	{
	unsigned int flags;

	save_flags(flags);
	cli();
	SCpnt->flags |= WAS_SENSE | ASKED_FOR_SENSE;
	update_timeout(SCpnt, SENSE_TIMEOUT);
	restore_flags(flags);


	memcpy ((void *) SCpnt->cmnd , (void *) generic_sense,
		sizeof(generic_sense));

	SCpnt->cmnd[1] = SCpnt->lun << 5;
	SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);

	SCpnt->request_buffer = &SCpnt->sense_buffer;
	SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
	SCpnt->use_sg = 0;
	SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
	internal_cmnd (SCpnt);
	}



/*
	scsi_do_cmd sends all the commands out to the low-level driver.  It
	handles the specifics required for each low level driver - ie queued
	or non queued.  It also prevents conflicts when different high level
	drivers go for the same host at the same time.
*/

void scsi_do_cmd (Scsi_Cmnd * SCpnt, const void *cmnd ,
		  void *buffer, unsigned bufflen, void (*done)(Scsi_Cmnd *),
		  int timeout, int retries
		   )
	{
	unsigned long flags;
	struct Scsi_Host * host = SCpnt->host;

#ifdef DEBUG
	{
	int i;
	int target = SCpnt->target;
	printk ("scsi_do_cmd (host = %d, target = %d, buffer =%p, "
		"bufflen = %d, done = %p, timeout = %d, retries = %d)\n"
		"command : " , host->host_no, target, buffer, bufflen, done, timeout, retries);
	for (i = 0; i < 10; ++i)
		printk ("%02x  ", ((unsigned char *) cmnd)[i]);
	printk("\n");
      }
#endif

	if (!host)
		{
		panic ("Invalid or not present host.\n");
		}


/*
	We must prevent reentrancy to the lowlevel host driver.  This prevents
	it - we enter a loop until the host we want to talk to is not busy.
	Race conditions are prevented, as interrupts are disabled in between the
	time we check for the host being not busy, and the time we mark it busy
	ourselves.
*/

	save_flags(flags);
	cli();
	SCpnt->pid = scsi_pid++;

	while (SCSI_BLOCK(host)) {
	   restore_flags(flags);
	   SCSI_SLEEP(&host->host_wait, SCSI_BLOCK(host));
	   cli();
	   }

	if (host->block) host_active = host;

	host->host_busy++;
	restore_flags(flags);

/*
	Our own function scsi_done (which marks the host as not busy, disables
	the timeout counter, etc) will be called by us or by the
	scsi_hosts[host].queuecommand() function needs to also call
	the completion function for the high level driver.

*/

	memcpy ((void *) SCpnt->data_cmnd , (void *) cmnd, 12);
#if 0
	SCpnt->host = host;
	SCpnt->target = target;
	SCpnt->lun = (SCpnt->data_cmnd[1] >> 5);
#endif
	SCpnt->bufflen = bufflen;
	SCpnt->buffer = buffer;
	SCpnt->flags=0;
	SCpnt->retries=0;
	SCpnt->allowed=retries;
	SCpnt->done = done;
	SCpnt->timeout_per_command = timeout;

	memcpy ((void *) SCpnt->cmnd , (void *) cmnd, 12);
	/* Zero the sense buffer.  Some host adapters automatically request
	   sense on error.  0 is not a valid sense code.  */
	memset ((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
	SCpnt->request_buffer = buffer;
	SCpnt->request_bufflen = bufflen;
	SCpnt->old_use_sg = SCpnt->use_sg;
	if (SCpnt->cmd_len == 0)
		SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
	SCpnt->old_cmd_len = SCpnt->cmd_len;

	/* Start the timer ticking.  */

	SCpnt->internal_timeout = 0;
	SCpnt->abort_reason = 0;
	internal_cmnd (SCpnt);

#ifdef DEBUG
	printk ("Leaving scsi_do_cmd()\n");
#endif
	}



/*
	The scsi_done() function disables the timeout timer for the scsi host,
	marks the host as not busy, and calls the user specified completion
	function for that host's current command.
*/

static void reset (Scsi_Cmnd * SCpnt)
{
#ifdef DEBUG
	printk("scsi: reset(%d)\n", SCpnt->host->host_no);
#endif

	SCpnt->flags |= (WAS_RESET | IS_RESETTING);
	scsi_reset(SCpnt);

#ifdef DEBUG
	printk("performing request sense\n");
#endif

#if 0  /* FIXME - remove this when done */
	if(SCpnt->flags & NEEDS_JUMPSTART) {
	  SCpnt->flags &= ~NEEDS_JUMPSTART;
	  scsi_request_sense (SCpnt);
	}
#endif
}



static int check_sense (Scsi_Cmnd * SCpnt)
	{
  /* If there is no sense information, request it.  If we have already
     requested it, there is no point in asking again - the firmware must be
     confused. */
  if (((SCpnt->sense_buffer[0] & 0x70) >> 4) != 7) {
    if(!(SCpnt->flags & ASKED_FOR_SENSE))
      return SUGGEST_SENSE;
    else
      return SUGGEST_RETRY;
      }

  SCpnt->flags &= ~ASKED_FOR_SENSE;

#ifdef DEBUG_INIT
	printk("scsi%d : ", SCpnt->host->host_no);
	print_sense("", SCpnt);
	printk("\n");
#endif
		if (SCpnt->sense_buffer[2] &0xe0)
		  return SUGGEST_ABORT;

		switch (SCpnt->sense_buffer[2] & 0xf)
		{
		case NO_SENSE:
			return 0;
		case RECOVERED_ERROR:
			return SUGGEST_IS_OK;

		case ABORTED_COMMAND:
			return SUGGEST_RETRY;
		case NOT_READY:
		case UNIT_ATTENTION:
			return SUGGEST_ABORT;

		/* these three are not supported */
		case COPY_ABORTED:
		case VOLUME_OVERFLOW:
		case MISCOMPARE:

		case MEDIUM_ERROR:
			return SUGGEST_REMAP;
		case BLANK_CHECK:
		case DATA_PROTECT:
		case HARDWARE_ERROR:
		case ILLEGAL_REQUEST:
		default:
			return SUGGEST_ABORT;
		}
	      }

/* This function is the mid-level interrupt routine, which decides how
 *  to handle error conditions.  Each invocation of this function must
 *  do one and *only* one of the following:
 *
 *  (1) Call last_cmnd[host].done.  This is done for fatal errors and
 *      normal completion, and indicates that the handling for this
 *      request is complete.
 *  (2) Call internal_cmnd to requeue the command.  This will result in
 *      scsi_done being called again when the retry is complete.
 *  (3) Call scsi_request_sense.  This asks the host adapter/drive for
 *      more information about the error condition.  When the information
 *      is available, scsi_done will be called again.
 *  (4) Call reset().  This is sort of a last resort, and the idea is that
 *      this may kick things loose and get the drive working again.  reset()
 *      automatically calls scsi_request_sense, and thus scsi_done will be
 *      called again once the reset is complete.
 *
 *      If none of the above actions are taken, the drive in question
 * will hang. If more than one of the above actions are taken by
 * scsi_done, then unpredictable behavior will result.
 */
static void scsi_done (Scsi_Cmnd * SCpnt)
	{
	int status=0;
	int exit=0;
	int checked;
	int oldto;
	struct Scsi_Host * host = SCpnt->host;
	int result = SCpnt->result;
	oldto = update_timeout(SCpnt, 0);

#ifdef DEBUG_TIMEOUT
	if(result) printk("Non-zero result in scsi_done %x %d:%d\n",
			  result, SCpnt->target, SCpnt->lun);
#endif

	/* If we requested an abort, (and we got it) then fix up the return
	   status to say why */
	if(host_byte(result) == DID_ABORT && SCpnt->abort_reason)
	  SCpnt->result = result = (result & 0xff00ffff) |
	    (SCpnt->abort_reason << 16);


#define FINISHED 0
#define MAYREDO  1
#define REDO	 3
#define PENDING  4

#ifdef DEBUG
	printk("In scsi_done(host = %d, result = %06x)\n", host->host_no, result);
#endif

	if(SCpnt->flags & WAS_SENSE)
	{
		SCpnt->use_sg = SCpnt->old_use_sg;
		SCpnt->cmd_len = SCpnt->old_cmd_len;
	}

	switch (host_byte(result))
	{
	case DID_OK:
		if (status_byte(result) && (SCpnt->flags & WAS_SENSE))
			/* Failed to obtain sense information */
			{
			SCpnt->flags &= ~WAS_SENSE;
			SCpnt->internal_timeout &= ~SENSE_TIMEOUT;

			if (!(SCpnt->flags & WAS_RESET))
				{
				printk("scsi%d : target %d lun %d request sense failed, performing reset.\n",
					SCpnt->host->host_no, SCpnt->target, SCpnt->lun);
				reset(SCpnt);
				return;
				}
			else
				{
				exit = (DRIVER_HARD | SUGGEST_ABORT);
				status = FINISHED;
				}
			}
		else switch(msg_byte(result))
			{
			case COMMAND_COMPLETE:
			switch (status_byte(result))
			{
			case GOOD:
				if (SCpnt->flags & WAS_SENSE)
					{
#ifdef DEBUG
	printk ("In scsi_done, GOOD status, COMMAND COMPLETE, parsing sense information.\n");
#endif

					SCpnt->flags &= ~WAS_SENSE;
					SCpnt->internal_timeout &= ~SENSE_TIMEOUT;

					switch (checked = check_sense(SCpnt))
					{
					case SUGGEST_SENSE:
					case 0:
#ifdef DEBUG
	printk("NO SENSE.  status = REDO\n");
#endif

						update_timeout(SCpnt, oldto);
						status = REDO;
						break;
					case SUGGEST_IS_OK:
						break;
					case SUGGEST_REMAP:
					case SUGGEST_RETRY:
#ifdef DEBUG
	printk("SENSE SUGGEST REMAP or SUGGEST RETRY - status = MAYREDO\n");
#endif

						status = MAYREDO;
						exit = DRIVER_SENSE | SUGGEST_RETRY;
						break;
					case SUGGEST_ABORT:
#ifdef DEBUG
	printk("SENSE SUGGEST ABORT - status = FINISHED");
#endif

						status = FINISHED;
						exit =  DRIVER_SENSE | SUGGEST_ABORT;
						break;
					default:
						printk ("Internal error %s %d \n", __FILE__,
							__LINE__);
					}
					}
				else
					{
#ifdef DEBUG
	printk("COMMAND COMPLETE message returned, status = FINISHED. \n");
#endif

					exit =  DRIVER_OK;
					status = FINISHED;
					}
				break;

			case CHECK_CONDITION:
				switch (check_sense(SCpnt))
				  {
				  case 0:
				    update_timeout(SCpnt, oldto);
				    status = REDO;
				    break;
				  case SUGGEST_REMAP:
				  case SUGGEST_RETRY:
				    status = MAYREDO;
				    exit = DRIVER_SENSE | SUGGEST_RETRY;
				    break;
				  case SUGGEST_ABORT:
				    status = FINISHED;
				    exit =  DRIVER_SENSE | SUGGEST_ABORT;
				    break;
				  case SUGGEST_SENSE:
				scsi_request_sense (SCpnt);
				status = PENDING;
				break;
				  }
				break;

			case CONDITION_GOOD:
			case INTERMEDIATE_GOOD:
			case INTERMEDIATE_C_GOOD:
				break;

			case BUSY:
				update_timeout(SCpnt, oldto);
				status = REDO;
				break;

			case RESERVATION_CONFLICT:
				printk("scsi%d : RESERVATION CONFLICT performing reset.\n",
					SCpnt->host->host_no);
				reset(SCpnt);
				return;
#if 0
				exit = DRIVER_SOFT | SUGGEST_ABORT;
				status = MAYREDO;
				break;
#endif
			default:
				printk ("Internal error %s %d \n"
					"status byte = %d \n", __FILE__,
					__LINE__, status_byte(result));

			}
			break;
			default:
				panic("scsi: unsupported message byte %d received\n", msg_byte(result));
			}
			break;
	case DID_TIME_OUT:
#ifdef DEBUG
	printk("Host returned DID_TIME_OUT - ");
#endif

		if (SCpnt->flags & WAS_TIMEDOUT)
			{
#ifdef DEBUG
	printk("Aborting\n");
#endif
			exit = (DRIVER_TIMEOUT | SUGGEST_ABORT);
			}
		else
			{
#ifdef DEBUG
			printk ("Retrying.\n");
#endif
			SCpnt->flags  |= WAS_TIMEDOUT;
			SCpnt->internal_timeout &= ~IN_ABORT;
			status = REDO;
			}
		break;
	case DID_BUS_BUSY:
	case DID_PARITY:
		status = REDO;
		break;
	case DID_NO_CONNECT:
#ifdef DEBUG
		printk("Couldn't connect.\n");
#endif
		exit  = (DRIVER_HARD | SUGGEST_ABORT);
		break;
	case DID_ERROR:
		status = MAYREDO;
		exit = (DRIVER_HARD | SUGGEST_ABORT);
		break;
	case DID_BAD_TARGET:
	case DID_ABORT:
		exit = (DRIVER_INVALID | SUGGEST_ABORT);
		break;
	case DID_RESET:
		if (SCpnt->flags & IS_RESETTING)
			{
			SCpnt->flags &= ~IS_RESETTING;
			status = REDO;
			break;
			}

		if(msg_byte(result) == GOOD &&
		      status_byte(result) == CHECK_CONDITION) {
			switch (check_sense(SCpnt)) {
			case 0:
			    update_timeout(SCpnt, oldto);
			    status = REDO;
			    break;
			case SUGGEST_REMAP:
			case SUGGEST_RETRY:
			    status = MAYREDO;
			    exit = DRIVER_SENSE | SUGGEST_RETRY;
			    break;
			case SUGGEST_ABORT:
			    status = FINISHED;
			    exit =  DRIVER_SENSE | SUGGEST_ABORT;
			    break;
			case SUGGEST_SENSE:
			      scsi_request_sense (SCpnt);
			      status = PENDING;
			      break;
			}
		} else {
		status=REDO;
		exit = SUGGEST_RETRY;
		}
		break;
	default :
		exit = (DRIVER_ERROR | SUGGEST_DIE);
	}

	switch (status)
		{
		case FINISHED:
		case PENDING:
			break;
		case MAYREDO:

#ifdef DEBUG
	printk("In MAYREDO, allowing %d retries, have %d\n",
	       SCpnt->allowed, SCpnt->retries);
#endif

			if ((++SCpnt->retries) < SCpnt->allowed)
			{
			if ((SCpnt->retries >= (SCpnt->allowed >> 1))
			    && !(jiffies < SCpnt->host->last_reset + MIN_RESET_PERIOD)
			    && !(SCpnt->flags & WAS_RESET))
				{
					printk("scsi%d : resetting for second half of retries.\n",
						SCpnt->host->host_no);
					reset(SCpnt);
					break;
				}

			}
			else
				{
				status = FINISHED;
				break;
				}
			/* fall through to REDO */

		case REDO:

			if (SCpnt->flags & WAS_SENSE)
				scsi_request_sense(SCpnt);
			else
			  {
			    memcpy ((void *) SCpnt->cmnd,
				    (void*) SCpnt->data_cmnd,
				    sizeof(SCpnt->data_cmnd));
			    SCpnt->request_buffer = SCpnt->buffer;
			    SCpnt->request_bufflen = SCpnt->bufflen;
			    SCpnt->use_sg = SCpnt->old_use_sg;
			    SCpnt->cmd_len = SCpnt->old_cmd_len;
			    internal_cmnd (SCpnt);
			  }
			break;
		default:
			INTERNAL_ERROR;
		}


	if (status == FINISHED) {
#ifdef DEBUG
	   printk("Calling done function - at address %p\n", SCpnt->done);
#endif
	   host->host_busy--; /* Indicate that we are free */

	   if (host->block && host->host_busy == 0) {
	      host_active = NULL;

	      /* For block devices "wake_up" is done in end_scsi_request */
	      if (MAJOR(SCpnt->request.dev) != SCSI_DISK_MAJOR &&
			MAJOR(SCpnt->request.dev) != SCSI_CDROM_MAJOR) {
		 struct Scsi_Host * next;

		 for (next = host->block; next != host; next = next->block)
		    wake_up(&next->host_wait);
		 }

	      }

	   wake_up(&host->host_wait);
	   SCpnt->result = result | ((exit & 0xff) << 24);
	   SCpnt->use_sg = SCpnt->old_use_sg;
	   SCpnt->cmd_len = SCpnt->old_cmd_len;
	   SCpnt->done (SCpnt);
	   }

#undef FINISHED
#undef REDO
#undef MAYREDO
#undef PENDING
	}

/*
	The scsi_abort function interfaces with the abort() function of the host
	we are aborting, and causes the current command to not complete.  The
	caller should deal with any error messages or status returned on the
	next call.

	This will not be called reentrantly for a given host.
*/

/*
	Since we're nice guys and specified that abort() and reset()
	can be non-reentrant.  The internal_timeout flags are used for
	this.
*/


int scsi_abort (Scsi_Cmnd * SCpnt, int why, int pid)
	{
	int oldto;
	unsigned long flags;
	struct Scsi_Host * host = SCpnt->host;

	while(1)
		{
		save_flags(flags);
		cli();

		/*
		 * Protect against races here.  If the command is done, or we are
		 * on a different command forget it.
		 */
		if (SCpnt->request.dev == -1 || pid != SCpnt->pid) {
		  restore_flags(flags);
		  return 0;
		}

		if (SCpnt->internal_timeout & IN_ABORT)
			{
			restore_flags(flags);
			while (SCpnt->internal_timeout & IN_ABORT)
				barrier();
			}
		else
			{
			SCpnt->internal_timeout |= IN_ABORT;
			oldto = update_timeout(SCpnt, ABORT_TIMEOUT);

			if ((SCpnt->flags & IS_RESETTING) &&
			    SCpnt->device->soft_reset) {
			  /* OK, this command must have died when we did the
			     reset.  The device itself must have lied. */
			  printk("Stale command on %d:%d appears to have died when"
				 " the bus was reset\n", SCpnt->target, SCpnt->lun);
			}

			restore_flags(flags);
			if (!host->host_busy) {
			  SCpnt->internal_timeout &= ~IN_ABORT;
			  update_timeout(SCpnt, oldto);
			  return 0;
			}
			printk("scsi : aborting command due to timeout : pid %lu, scsi%d, id %d, lun %d ",
			       SCpnt->pid, SCpnt->host->host_no, (int) SCpnt->target, (int)
			       SCpnt->lun);
			print_command (SCpnt->cmnd);
			if (SCpnt->request.dev == -1 || pid != SCpnt->pid)
			  return 0;
			SCpnt->abort_reason = why;
			switch(host->hostt->abort(SCpnt)) {
			  /* We do not know how to abort.  Try waiting another
			     time increment and see if this helps. Set the
			     WAS_TIMEDOUT flag set so we do not try this twice
			     */
			case SCSI_ABORT_BUSY: /* Tough call - returning 1 from
						 this is too severe */
			case SCSI_ABORT_SNOOZE:
			  if(why == DID_TIME_OUT) {
			    save_flags(flags);
			    cli();
			    SCpnt->internal_timeout &= ~IN_ABORT;
			    if(SCpnt->flags & WAS_TIMEDOUT) {
			      restore_flags(flags);
			      return 1; /* Indicate we cannot handle this.
					   We drop down into the reset handler
					   and try again */
			    } else {
			      SCpnt->flags |= WAS_TIMEDOUT;
			      oldto = SCpnt->timeout_per_command;
			      update_timeout(SCpnt, oldto);
			    }
			    restore_flags(flags);
			  }
			  return 0;
			case SCSI_ABORT_PENDING:
			  if(why != DID_TIME_OUT) {
			    save_flags(flags);
			    cli();
			    update_timeout(SCpnt, oldto);
			    restore_flags(flags);
			  }
			  return 0;
			case SCSI_ABORT_SUCCESS:
			  /* We should have already aborted this one.  No
			     need to adjust timeout */
			case SCSI_ABORT_NOT_RUNNING:
			  SCpnt->internal_timeout &= ~IN_ABORT;
			  update_timeout(SCpnt, 0);
			  return 0;
			case SCSI_ABORT_ERROR:
			default:
			  SCpnt->internal_timeout &= ~IN_ABORT;
			  return 1;
			}
		      }
	      }
      }

int scsi_reset (Scsi_Cmnd * SCpnt)
	{
	int temp, oldto;
	unsigned long flags;
	Scsi_Cmnd * SCpnt1;
	struct Scsi_Host * host = SCpnt->host;

#ifdef DEBUG
	printk("Danger Will Robinson! - SCSI bus for host %d is being reset.\n",host->host_no);
#endif
	while (1) {
		save_flags(flags);
		cli();
		if (SCpnt->internal_timeout & IN_RESET)
			{
			restore_flags(flags);
			while (SCpnt->internal_timeout & IN_RESET)
				barrier();
			}
		else
			{
			SCpnt->internal_timeout |= IN_RESET;
			oldto = update_timeout(SCpnt, RESET_TIMEOUT);

			if (host->host_busy)
				{
				restore_flags(flags);
				SCpnt1 = host->host_queue;
				while(SCpnt1) {
				  if (SCpnt1->request.dev > 0) {
#if 0
				    if (!(SCpnt1->flags & IS_RESETTING) &&
				      !(SCpnt1->internal_timeout & IN_ABORT))
				    scsi_abort(SCpnt1, DID_RESET, SCpnt->pid);
#endif
				    SCpnt1->flags |= IS_RESETTING;
				  }
				  SCpnt1 = SCpnt1->next;
				}

				host->last_reset = jiffies;
				temp = host->hostt->reset(SCpnt);
				host->last_reset = jiffies;
				}
			else
				{
				if (!host->block) host->host_busy++;
				restore_flags(flags);
				host->last_reset = jiffies;
				temp = host->hostt->reset(SCpnt);
				host->last_reset = jiffies;
				if (!host->block) host->host_busy--;
				}

#ifdef DEBUG
			printk("scsi reset function returned %d\n", temp);
#endif
			switch(temp) {
			case SCSI_RESET_SUCCESS:
			  save_flags(flags);
			  cli();
			  SCpnt->internal_timeout &= ~IN_RESET;
			  update_timeout(SCpnt, oldto);
			  restore_flags(flags);
			  return 0;
			case SCSI_RESET_PENDING:
			  return 0;
			case SCSI_RESET_PUNT:
			case SCSI_RESET_WAKEUP:
			  SCpnt->internal_timeout &= ~IN_RESET;
			  scsi_request_sense (SCpnt);
			  return 0;
			case SCSI_RESET_SNOOZE:
			  /* In this case, we set the timeout field to 0
			     so that this command does not time out any more,
			     and we return 1 so that we get a message on the
			     screen. */
			  save_flags(flags);
			  cli();
			  SCpnt->internal_timeout &= ~IN_RESET;
			  update_timeout(SCpnt, 0);
			  restore_flags(flags);
			  /* If you snooze, you lose... */
			case SCSI_RESET_ERROR:
			default:
			  return 1;
			}

			return temp;
			}
		}
	}


static void scsi_main_timeout(void)
	{
	/*
		We must not enter update_timeout with a timeout condition still pending.
	*/

	int timed_out, pid;
	unsigned long flags;
	struct Scsi_Host * host;
	Scsi_Cmnd * SCpnt = NULL;

	do {
		save_flags(flags);
		cli();

		update_timeout(NULL, 0);
	/*
		Find all timers such that they have 0 or negative (shouldn't happen)
		time remaining on them.
	*/

		timed_out = 0;
		for(host = scsi_hostlist; host; host = host->next) {
		  for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
		    if (SCpnt->timeout == -1)
		      {
			SCpnt->timeout = 0;
			pid = SCpnt->pid;
			restore_flags(flags);
			scsi_times_out(SCpnt, pid);
			++timed_out;
			save_flags(flags);
			cli();
		      }
		}
	      } while (timed_out);
	restore_flags(flags);
      }

/*
	The strategy is to cause the timer code to call scsi_times_out()
	when the soonest timeout is pending.
	The arguments are used when we are queueing a new command, because
	we do not want to subtract the time used from this time, but when we
	set the timer, we want to take this value into account.
*/

static int update_timeout(Scsi_Cmnd * SCset, int timeout)
	{
	unsigned int least, used;
	unsigned int oldto;
	unsigned long flags;
	struct Scsi_Host * host;
	Scsi_Cmnd * SCpnt = NULL;

	save_flags(flags);
	cli();

/*
	Figure out how much time has passed since the last time the timeouts
	were updated
*/
	used = (time_start) ? (jiffies - time_start) : 0;

/*
	Find out what is due to timeout soonest, and adjust all timeouts for
	the amount of time that has passed since the last time we called
	update_timeout.
*/

	oldto = 0;

	if(SCset){
	  oldto = SCset->timeout - used;
	  SCset->timeout = timeout + used;
	}

	least = 0xffffffff;

	for(host = scsi_hostlist; host; host = host->next)
	  for(SCpnt = host->host_queue; SCpnt; SCpnt = SCpnt->next)
	    if (SCpnt->timeout > 0) {
	      SCpnt->timeout -= used;
	      if(SCpnt->timeout <= 0) SCpnt->timeout = -1;
	      if(SCpnt->timeout > 0 && SCpnt->timeout < least)
		least = SCpnt->timeout;
	    }

/*
	If something is due to timeout again, then we will set the next timeout
	interrupt to occur.  Otherwise, timeouts are disabled.
*/

	if (least != 0xffffffff)
		{
		time_start = jiffies;
		timer_table[SCSI_TIMER].expires = (time_elapsed = least) + jiffies;
		timer_active |= 1 << SCSI_TIMER;
		}
	else
		{
		timer_table[SCSI_TIMER].expires = time_start = time_elapsed = 0;
		timer_active &= ~(1 << SCSI_TIMER);
		}
	restore_flags(flags);
	return oldto;
	}


static unsigned char * dma_malloc_freelist = NULL;
static int scsi_need_isa_bounce_buffers;
static unsigned int dma_sectors = 0;
unsigned int dma_free_sectors = 0;
unsigned int need_isa_buffer = 0;
static unsigned char ** dma_malloc_pages = NULL;
#define MALLOC_PAGEBITS 12

static int scsi_register_host(Scsi_Host_Template *);
static void scsi_unregister_host(Scsi_Host_Template *);

void *scsi_malloc(unsigned int len)
{
  unsigned int nbits, mask;
  unsigned long flags;
  int i, j;
  if((len & 0x1ff) || len > (1<<MALLOC_PAGEBITS))
    return NULL;

  save_flags(flags);
  cli();
  nbits = len >> 9;
  mask = (1 << nbits) - 1;

  for(i=0;i < (dma_sectors >> (MALLOC_PAGEBITS - 9)); i++)
    for(j=0; j<=(sizeof(*dma_malloc_freelist) * 8) - nbits; j++){
      if ((dma_malloc_freelist[i] & (mask << j)) == 0){
	dma_malloc_freelist[i] |= (mask << j);
	restore_flags(flags);
	dma_free_sectors -= nbits;
#ifdef DEBUG
	printk("SMalloc: %d %p ",len, dma_malloc_pages[i] + (j << 9));
#endif
	return (void *) ((unsigned long) dma_malloc_pages[i] + (j << 9));
      }
    }
  restore_flags(flags);
  return NULL;  /* Nope.  No more */
}

int scsi_free(void *obj, unsigned int len)
{
  int page, sector, nbits, mask;
  long offset;
  unsigned long flags;

#ifdef DEBUG
  printk("Sfree %p %d\n",obj, len);
#endif

   offset = -1;
  for (page = 0; page < (dma_sectors >> 3); page++)
	if ((unsigned long) obj >= (unsigned long) dma_malloc_pages[page] &&
		(unsigned long) obj < (unsigned long) dma_malloc_pages[page] + (1 << MALLOC_PAGEBITS))
		{
			offset = ((unsigned long) obj) - ((unsigned long)dma_malloc_pages[page]);
			break;
		}

  if (page == (dma_sectors >> 3)) panic("Bad offset");
  sector = offset >> 9;
  if(sector >= dma_sectors) panic ("Bad page");

  sector = (offset >> 9) & (sizeof(*dma_malloc_freelist) * 8 - 1);
  nbits = len >> 9;
  mask = (1 << nbits) - 1;

  if ((mask << sector) > 0xffff) panic ("Bad memory alignment");

  save_flags(flags);
  cli();
  if(dma_malloc_freelist[page] & (mask << sector) != (mask<<sector))
    panic("Trying to free unused memory");

  dma_free_sectors += nbits;
  dma_malloc_freelist[page] &= ~(mask << sector);
  restore_flags(flags);
  return 0;
}


/* These are special functions that can be used to obtain memory at boot time.
   They act line a malloc function, but they simply take memory from the
   pool */

static unsigned long scsi_init_memory_start = 0;
static unsigned long scsi_memory_lower_value = 0;
static unsigned long scsi_memory_upper_value = 0;
int scsi_loadable_module_flag; /* Set after we scan builtin drivers */

void * scsi_init_malloc(unsigned int size, int priority)
{
  unsigned long retval;

/* Use the statically allocated memory instead of kmalloc  (DB) */
#if defined(USE_STATIC_SCSI_MEMORY)
  if(scsi_loadable_module_flag && !(priority & GFP_DMA))
#else
  if(scsi_loadable_module_flag)
#endif
    retval = (unsigned long) kmalloc(size, priority);
  else {
    /*
     * Keep all memory aligned on 16-byte boundaries. Some host adaptors
     * (e.g. BusLogic BT-445S) require DMA buffers to be aligned that way.
     */
    size = (size + 15) & ~15;

    if(scsi_loadable_module_flag &&
       (scsi_init_memory_start + size) > scsi_memory_upper_value) {
       retval = 0;
       printk("scsi_init_malloc: no more statically allocated memory.\n");
       }
    else {
       retval = scsi_init_memory_start;
       scsi_init_memory_start += size;
       }
    }
  memset((void *) retval, 0, size);
  return (void *) retval;
}


void scsi_init_free(char * ptr, unsigned int size)
{ /* We need to compare addresses to see whether this was kmalloc'd or not */

  if((unsigned long) ptr >= scsi_init_memory_start ||
     (unsigned long) ptr <  scsi_memory_lower_value) kfree(ptr);
  else {
    size = (size + 15) & ~15; /* Use the same alignment as scsi_init_malloc() */

    if(((unsigned long) ptr) + size == scsi_init_memory_start)
      scsi_init_memory_start = (unsigned long) ptr;
    }
}

/*
	scsi_dev_init() is our initialization routine, which in turn calls host
	initialization, bus scanning, and sd/st initialization routines.  It
	should be called from main().
*/

unsigned long scsi_dev_init (unsigned long memory_start,unsigned long memory_end)
	{
	struct Scsi_Host * host = NULL;
	Scsi_Device * SDpnt;
	struct Scsi_Host * shpnt;
	struct Scsi_Device_Template * sdtpnt;
	Scsi_Cmnd * SCpnt;
	int i;
#ifdef FOO_ON_YOU
	return;
#endif

	/* Init a few things so we can "malloc" memory. */
	scsi_loadable_module_flag = 0;
	/* Align everything on 16-byte boundaries. */
	scsi_init_memory_start = (memory_start + 15) & ~ 15;
	scsi_memory_lower_value = scsi_init_memory_start;

	timer_table[SCSI_TIMER].fn = scsi_main_timeout;
	timer_table[SCSI_TIMER].expires = 0;

	/* initialize all hosts */
	scsi_init();

	scsi_devices = (Scsi_Device *) NULL;

	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
	  scan_scsis(shpnt);           /* scan for scsi devices */

	printk("scsi : detected ");
	for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
	  if (sdtpnt->dev_noticed && sdtpnt->name)
	    printk("%d SCSI %s%s ", sdtpnt->dev_noticed, sdtpnt->name,
	    (sdtpnt->dev_noticed != 1) ? "s" : "");
	printk("total.\n");

	for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
	  if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();

	for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
	  int j;
	  SDpnt->scsi_request_fn = NULL;
	  for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
	      if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);

	  if(SDpnt->attached){
	    for(j=0;j<SDpnt->host->cmd_per_lun;j++){
	      SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
	      SCpnt->host = SDpnt->host;
	      SCpnt->device = SDpnt;
	      SCpnt->target = SDpnt->id;
	      SCpnt->lun = SDpnt->lun;
	      SCpnt->request.dev = -1; /* Mark not busy */
	      SCpnt->use_sg = 0;
	      SCpnt->old_use_sg = 0;
	      SCpnt->old_cmd_len = 0;
	      SCpnt->timeout = 0;
	      SCpnt->underflow = 0;
	      SCpnt->transfersize = 0;
	      SCpnt->host_scribble = NULL;
	      host = SDpnt->host;
	      if(host->host_queue)
		host->host_queue->prev = SCpnt;
	      SCpnt->next = host->host_queue;
	      SCpnt->prev = NULL;
	      host->host_queue = SCpnt;
	    }
	  }
	}

	if (scsi_devicelist)
	  dma_sectors = 16;  /* Base value we use */

	if (memory_end-1 > ISA_DMA_THRESHOLD)
	  scsi_need_isa_bounce_buffers = 1;
	else
	  scsi_need_isa_bounce_buffers = 0;

	for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
	  host = SDpnt->host;

	  if(SDpnt->type != TYPE_TAPE)
	    dma_sectors += ((host->sg_tablesize *
			     sizeof(struct scatterlist) + 511) >> 9) *
			       host->cmd_per_lun;

	  if(host->unchecked_isa_dma &&
	     memory_end - 1 > ISA_DMA_THRESHOLD &&
	     SDpnt->type != TYPE_TAPE) {
	    dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
	      host->cmd_per_lun;
	    need_isa_buffer++;
	  }
	}

	dma_sectors = (dma_sectors + 15) & 0xfff0;
	dma_free_sectors = dma_sectors;  /* This must be a multiple of 16 */

	dma_malloc_freelist = (unsigned char *)
	  scsi_init_malloc(dma_sectors >> 3, GFP_ATOMIC);
	memset(dma_malloc_freelist, 0, dma_sectors >> 3);

	dma_malloc_pages = (unsigned char **)
	  scsi_init_malloc(dma_sectors >> 1, GFP_ATOMIC);
	memset(dma_malloc_pages, 0, dma_sectors >> 1);

	for(i=0; i< dma_sectors >> 3; i++)
	  dma_malloc_pages[i] = (unsigned char *)
	    scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);


	/* OK, now we finish the initialization by doing spin-up, read
	   capacity, etc, etc */
	for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
	  if(sdtpnt->finish && sdtpnt->nr_dev)
	    (*sdtpnt->finish)();

	scsi_loadable_module_flag = 1;


/* This allocates statically some extra memory to be used for modules,
   until the kmalloc problem is fixed (DB) */

#if defined(USE_STATIC_SCSI_MEMORY)
	scsi_memory_upper_value = scsi_init_memory_start + 256 * 1024;
	printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
		(scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
		(scsi_init_memory_start - scsi_memory_lower_value) / 1024,
		(scsi_memory_upper_value - scsi_init_memory_start) / 1024);
	return scsi_memory_upper_value;
#else
	return scsi_init_memory_start;
#endif
	}

static void print_inquiry(unsigned char *data)
{
	int i;

	printk("  Vendor: ");
	for (i = 8; i < 16; i++)
		{
		if (data[i] >= 0x20 && i < data[4] + 5)
			printk("%c", data[i]);
		else
			printk(" ");
		}

	printk("  Model: ");
	for (i = 16; i < 32; i++)
		{
		if (data[i] >= 0x20 && i < data[4] + 5)
			printk("%c", data[i]);
		else
			printk(" ");
		}

	printk("  Rev: ");
	for (i = 32; i < 36; i++)
		{
		if (data[i] >= 0x20 && i < data[4] + 5)
			printk("%c", data[i]);
		else
			printk(" ");
		}

	printk("\n");

	i = data[0] & 0x1f;

	printk("  Type:   %s ",
	       i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          " );
	printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
	if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
	  printk(" CCS\n");
	else
	  printk("\n");
}

/*
 * This entry point should be called by a loadable module if it is trying
 * add a low level scsi driver to the system.
 */
static int scsi_register_host(Scsi_Host_Template * tpnt)
{
  int pcount;
  struct Scsi_Host * shpnt;
  struct Scsi_Host * host = NULL;
  unsigned long flags;
  Scsi_Device * SDpnt;
  Scsi_Cmnd * SCpnt;
  struct Scsi_Device_Template * sdtpnt;
  int j, i;
  const char * name;

  if (tpnt->next || !tpnt->detect) return 1;  /* Must be already loaded, or
					       no detect routine available */
  pcount = next_scsi_host;
  if ((tpnt->present = tpnt->detect(tpnt)))
    {
      if(pcount == next_scsi_host) {
	if(tpnt->present > 1) {
	  printk("Failure to register low-level scsi driver");
	  scsi_unregister_host(tpnt);
	  return 1;
	}
	/* The low-level driver failed to register a driver.  We
	   can do this now. */
	scsi_register(tpnt,0);
      }
      tpnt->next = scsi_hosts; /* Add to the linked list */
      scsi_hosts = tpnt;

      for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
	if(shpnt->hostt == tpnt)
	  {
	    if(tpnt->info)
	      name = tpnt->info(shpnt);
	    else
	      name = tpnt->name;
	    printk ("scsi%d : %s\n", /* And print a little message */
		    shpnt->host_no, name);
	  }

      printk ("scsi : %d host%s.\n", next_scsi_host,
		(next_scsi_host == 1) ? "" : "s");

      scsi_make_blocked_list();

      /* The next step is to call scan_scsis here.  This generates the
	 Scsi_Devices entries */

      for(shpnt=scsi_hostlist; shpnt; shpnt = shpnt->next)
	if(shpnt->hostt == tpnt) scan_scsis(shpnt);

      for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
	if(sdtpnt->init && sdtpnt->dev_noticed) (*sdtpnt->init)();

      /* Next we create the Scsi_Cmnd structures for this host */

      for(SDpnt = scsi_devices; SDpnt; SDpnt = SDpnt->next)
	if(SDpnt->host->hostt == tpnt)
	  {
	    for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
	      if(sdtpnt->attach) (*sdtpnt->attach)(SDpnt);
	    if(SDpnt->attached){
	      for(j=0;j<SDpnt->host->cmd_per_lun;j++){
		SCpnt = (Scsi_Cmnd *) scsi_init_malloc(sizeof(Scsi_Cmnd), GFP_ATOMIC);
		SCpnt->host = SDpnt->host;
		SCpnt->device = SDpnt;
		SCpnt->target = SDpnt->id;
		SCpnt->lun = SDpnt->lun;
		SCpnt->request.dev = -1; /* Mark not busy */
		SCpnt->request.sem = NULL;
		SCpnt->use_sg = 0;
		SCpnt->old_use_sg = 0;
		SCpnt->underflow = 0;
		SCpnt->timeout = 0;
		SCpnt->transfersize = 0;
		SCpnt->host_scribble = NULL;
		host = SDpnt->host;
		SCpnt->next = host->host_queue;
		SCpnt->prev = NULL;
		host->host_queue = SCpnt;
		if(host->host_queue)
		  host->host_queue->prev = SCpnt;
	      }
	    }
	  }
	/* Next, check to see if we need to extend the DMA buffer pool */
      {
	  unsigned char * new_dma_malloc_freelist = NULL;
	  unsigned int new_dma_sectors = 0;
	  unsigned int new_need_isa_buffer = 0;
	  unsigned char ** new_dma_malloc_pages = NULL;

	  if (scsi_devicelist)
	    new_dma_sectors = 16;  /* Base value we use */

	  for (SDpnt=scsi_devices; SDpnt; SDpnt = SDpnt->next) {
	    host = SDpnt->host;

	    if(SDpnt->type != TYPE_TAPE)
	      new_dma_sectors += ((host->sg_tablesize *
				   sizeof(struct scatterlist) + 511) >> 9) *
				     host->cmd_per_lun;

	    if(host->unchecked_isa_dma &&
	       scsi_need_isa_bounce_buffers &&
	       SDpnt->type != TYPE_TAPE) {
	      new_dma_sectors += (PAGE_SIZE >> 9) * host->sg_tablesize *
		host->cmd_per_lun;
	      new_need_isa_buffer++;
	    }
	  }

	  new_dma_sectors = (new_dma_sectors + 15) & 0xfff0;

	  new_dma_malloc_freelist = (unsigned char *)
	    scsi_init_malloc(new_dma_sectors >> 3, GFP_ATOMIC);
	  memset(new_dma_malloc_freelist, 0, new_dma_sectors >> 3);

	  new_dma_malloc_pages = (unsigned char **)
	    scsi_init_malloc(new_dma_sectors >> 1, GFP_ATOMIC);
	  memset(new_dma_malloc_pages, 0, new_dma_sectors >> 1);

	  for(i=dma_sectors >> 3; i< new_dma_sectors >> 3; i++)
	    new_dma_malloc_pages[i] = (unsigned char *)
	      scsi_init_malloc(PAGE_SIZE, GFP_ATOMIC | GFP_DMA);


	  /* When we dick with the actual DMA list, we need to protect things */

	  save_flags(flags);
	  cli();
	  memcpy(new_dma_malloc_freelist, dma_malloc_freelist, dma_sectors >> 3);
	  scsi_init_free(dma_malloc_freelist, dma_sectors>>3);
	  dma_malloc_freelist = new_dma_malloc_freelist;

	  memcpy(new_dma_malloc_pages, dma_malloc_pages, dma_sectors >> 1);
	  scsi_init_free((char *) dma_malloc_pages, dma_sectors>>1);

	  dma_free_sectors += new_dma_sectors - dma_sectors;
	  dma_malloc_pages = new_dma_malloc_pages;
	  dma_sectors = new_dma_sectors;
	  need_isa_buffer = new_need_isa_buffer;
	  restore_flags(flags);


	}
      /* This does any final handling that is required. */
      for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
	if(sdtpnt->finish && sdtpnt->nr_dev)
	  (*sdtpnt->finish)();
    }

#if defined(USE_STATIC_SCSI_MEMORY)
  printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
	  (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
	  (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
	  (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
#endif

  return 0;
}

/*
 * Similarly, this entry point should be called by a loadable module if it
 * is trying to remove a low level scsi driver from the system.
 */
static void scsi_unregister_host(Scsi_Host_Template * tpnt)
{
  Scsi_Host_Template * SHT, *SHTp;
  Scsi_Device *sdpnt, * sdppnt, * sdpnt1;
  Scsi_Cmnd * SCpnt;
  unsigned long flags;
  struct Scsi_Device_Template * sdtpnt;
  struct Scsi_Host * shpnt, *sh1;
  int pcount;

  /* First verify that this host adapter is completely free with no pending
     commands */

  for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
    if(sdpnt->host->hostt == tpnt && sdpnt->host->hostt->usage_count
				 && *sdpnt->host->hostt->usage_count) return;

  for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
    {
      if (shpnt->hostt != tpnt) continue;
      for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
	{
	  save_flags(flags);
	  cli();
	  if(SCpnt->request.dev != -1) {
	    restore_flags(flags);
	    for(SCpnt = shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
	      if(SCpnt->request.dev == 0xffe0) SCpnt->request.dev = -1;
	    printk("Device busy???\n");
	    return;
	  }
	  SCpnt->request.dev = 0xffe0;  /* Mark as busy */
	  restore_flags(flags);
	}
    }
  /* Next we detach the high level drivers from the Scsi_Device structures */

  for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
    if(sdpnt->host->hostt == tpnt)
      {
	for(sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
	  if(sdtpnt->detach) (*sdtpnt->detach)(sdpnt);
	/* If something still attached, punt */
	if (sdpnt->attached) {
	  printk("Attached usage count = %d\n", sdpnt->attached);
	  return;
	}
      }

  /* Next we free up the Scsi_Cmnd structures for this host */

  for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt->next)
    if(sdpnt->host->hostt == tpnt)
      while (sdpnt->host->host_queue) {
	SCpnt = sdpnt->host->host_queue->next;
	scsi_init_free((char *) sdpnt->host->host_queue, sizeof(Scsi_Cmnd));
	sdpnt->host->host_queue = SCpnt;
	if (SCpnt) SCpnt->prev = NULL;
      }

  /* Next free up the Scsi_Device structures for this host */

  sdppnt = NULL;
  for(sdpnt = scsi_devices; sdpnt; sdpnt = sdpnt1)
    {
      sdpnt1 = sdpnt->next;
      if (sdpnt->host->hostt == tpnt) {
	if (sdppnt)
	  sdppnt->next = sdpnt->next;
	else
	  scsi_devices = sdpnt->next;
	scsi_init_free((char *) sdpnt, sizeof (Scsi_Device));
      } else
	sdppnt = sdpnt;
    }

  /* Next we go through and remove the instances of the individual hosts
     that were detected */

  shpnt = scsi_hostlist;
  while(shpnt) {
   sh1 = shpnt->next;
    if(shpnt->hostt == tpnt) {
      if(shpnt->loaded_as_module) {
	pcount = next_scsi_host;
	if(tpnt->release)
	  (*tpnt->release)(shpnt);
	else {
	  /* This is the default case for the release function.  It should do the right
	     thing for most correctly written host adapters. */
	  if (shpnt->irq) free_irq(shpnt->irq);
	  if (shpnt->dma_channel != 0xff) free_dma(shpnt->dma_channel);
	  if (shpnt->io_port && shpnt->n_io_port)
	    release_region(shpnt->io_port, shpnt->n_io_port);
	}
	if(pcount == next_scsi_host) scsi_unregister(shpnt);
	tpnt->present--;
      }
    }
    shpnt = sh1;
  }

  printk ("scsi : %d host%s.\n", next_scsi_host,
	  (next_scsi_host == 1) ? "" : "s");

#if defined(USE_STATIC_SCSI_MEMORY)
  printk ("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
	  (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
	  (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
	  (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
#endif

  scsi_make_blocked_list();

  /* There were some hosts that were loaded at boot time, so we cannot
     do any more than this */
  if (tpnt->present) return;

  /* OK, this is the very last step.  Remove this host adapter from the
     linked list. */
  for(SHTp=NULL, SHT=scsi_hosts; SHT; SHTp=SHT, SHT=SHT->next)
    if(SHT == tpnt) {
      if(SHTp)
	SHTp->next = SHT->next;
      else
	scsi_hosts = SHT->next;
      SHT->next = NULL;
      break;
    }
}

int scsi_register_module(int module_type, void * ptr)
{
  switch(module_type){
  case MODULE_SCSI_HA:
    return scsi_register_host((Scsi_Host_Template *) ptr);
    /* The rest of these are not yet implemented */

    /* Load constants.o */
  case MODULE_SCSI_CONST:

    /* Load specialized ioctl handler for some device.  Intended for cdroms that
       have non-SCSI2 audio command sets. */
  case MODULE_SCSI_IOCTL:

    /* Load upper level device handler of some kind */
  case MODULE_SCSI_DEV:
  default:
    return 1;
  }
}

void scsi_unregister_module(int module_type, void * ptr)
{
  switch(module_type) {
  case MODULE_SCSI_HA:
    scsi_unregister_host((Scsi_Host_Template *) ptr);
    break;
    /* The rest of these are not yet implemented. */
  case MODULE_SCSI_CONST:
  case MODULE_SCSI_IOCTL:
  case MODULE_SCSI_DEV:
  default:
  }
  return;
}

#ifdef DEBUG_TIMEOUT
static void
scsi_dump_status(void)
{
  int i;
  struct Scsi_Host * shpnt;
  Scsi_Cmnd * SCpnt;
  printk("Dump of scsi parameters:\n");
  i = 0;
  for(shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next)
    for(SCpnt=shpnt->host_queue; SCpnt; SCpnt = SCpnt->next)
      {
	/*  (0) 0:0:0 (802 123434 8 8 0) (3 3 2) (%d %d %d) %d %x      */
	printk("(%d) %d:%d:%d (%4.4x %ld %ld %ld %ld) (%d %d %x) (%d %d %d) %x %x %x\n",
	       i++, SCpnt->host->host_no,
	       SCpnt->target,
	       SCpnt->lun,
	       SCpnt->request.dev,
	       SCpnt->request.sector,
	       SCpnt->request.nr_sectors,
	       SCpnt->request.current_nr_sectors,
	       SCpnt->use_sg,
	       SCpnt->retries,
	       SCpnt->allowed,
	       SCpnt->flags,
	       SCpnt->timeout_per_command,
	       SCpnt->timeout,
	       SCpnt->internal_timeout,
	       SCpnt->cmnd[0],
	       SCpnt->sense_buffer[2],
	       SCpnt->result);
      }
  printk("wait_for_request = %p\n", wait_for_request);
  /* Now dump the request lists for each block device */
  printk("Dump of pending block device requests\n");
  for(i=0; i<MAX_BLKDEV; i++)
    if(blk_dev[i].current_request)
      {
	struct request * req;
	printk("%d: ", i);
	req = blk_dev[i].current_request;
	while(req) {
	  printk("(%x %d %ld %ld %ld) ",
		 req->dev,
		 req->cmd,
		 req->sector,
		 req->nr_sectors,
		 req->current_nr_sectors);
	  req = req->next;
	}
	printk("\n");
      }
}
#endif

/*
 * Overrides for Emacs so that we follow Linus's tabbing style.
 * Emacs will notice this stuff at the end of the file and automatically
 * adjust the settings for this buffer only.  This must remain at the end
 * of the file.
 * ---------------------------------------------------------------------------
 * Local variables:
 * c-indent-level: 8
 * c-brace-imaginary-offset: 0
 * c-brace-offset: -8
 * c-argdecl-indent: 8
 * c-label-offset: -8
 * c-continued-statement-offset: 8
 * c-continued-brace-offset: 0
 * End:
 */