summaryrefslogtreecommitdiffstats
path: root/drivers/scsi/buslogic.c
blob: a5824b04be091350032207da10c72973a0d9766b (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
/*
 *	buslogic.c	Copyright (C) 1993, 1994 David B. Gentzel
 *	Low-level scsi driver for BusLogic adapters
 *	by David B. Gentzel, Whitfield Software Services, Carnegie, PA
 *	    (gentzel@nova.enet.dec.com)
 *	Thanks to BusLogic for providing the necessary documentation
 *
 *	The original version of this driver was derived from aha1542.[ch],
 *	which is Copyright (C) 1992 Tommy Thorn.  Much has been reworked, but
 *	most of basic structure and substantial chunks of code still remain.
 *
 *	Furthermore, many subsequent fixes and improvements to the aha1542
 *	driver have been folded back into this driver.  These changes to
 *	aha1542.[ch] are Copyright (C) 1993, 1994 Eric Youngdale.
 *
 *	Thanks to the following individuals who have made contributions (of
 *	(code, information, support, or testing) to this driver:
 *		Eric Youngdale		Leonard Zubkoff
 *		Tomas Hurka		Andrew Walker
 */

/*
 * TODO:
 *	1. Clean up error handling & reporting.
 *	2. Find out why scatter/gather is limited to 16 requests per command.
 *	3. Test/improve/fix abort & reset functions.
 *	4. Look at command linking.
 *	5. Allow multiple boards to share an IRQ if the bus allows (EISA, MCA,
 *	   and PCI).
 *	6. Avoid using the 445S workaround for board revs >= D.
 */

/*
 * NOTES:
 *    BusLogic (formerly BusTek) manufactures an extensive family of
 *    intelligent, high performance SCSI-2 host adapters.  They all support
 *    command queueing and scatter/gather I/O.  Most importantly, they all
 *    support identical programming interfaces, so a single driver can be used
 *    for all boards.
 *
 *    Actually, they all support TWO identical programming interfaces!  They
 *    have an Adaptec 154x compatible interface (complete with 24 bit
 *    addresses) as well as a "native" 32 bit interface.  As such, the Linux
 *    aha1542 driver can be used to drive them, but with less than optimal
 *    performance (at least for the EISA, VESA, and MCA boards).
 *
 *    Here is the scoop on the various models:
 *	BT-542B - ISA first-party DMA with floppy support.
 *	BT-545S - 542B + FAST SCSI and active termination.
 *	BT-545D - 545S + differential termination.
 *	BT-640A - MCA bus-master with floppy support.
 *	BT-646S - 640A + FAST SCSI and active termination.
 *	BT-646D - 646S + differential termination.
 *	BT-742A - EISA bus-master with floppy support.
 *	BT-747S - 742A + FAST SCSI, active termination, and 2.88M floppy.
 *	BT-747D - 747S + differential termination.
 *	BT-757S - 747S + WIDE SCSI.
 *	BT-757D - 747D + WIDE SCSI.
 *	BT-445S - VESA bus-master FAST SCSI with active termination
 *		  and floppy support.
 *	BT-445C - 445S + enhanced BIOS & firmware options.
 *	BT-946C - PCI bus-master FAST SCSI.
 *	BT-956C - PCI bus-master FAST/WIDE SCSI.
 *
 *    ??? I believe other boards besides the 445 now have a "C" model, but I
 *    have no facts on them.
 *
 *    This driver SHOULD support all of these boards.  It has only been tested
 *    with a 747S, 445S, 946C, and 956C; there is no PCI-specific support as
 *    yet.
 *
 *    Should you require further information on any of these boards, BusLogic
 *    can be reached at (408)492-9090.  Their BBS # is (408)492-1984 (maybe BBS
 *    stands for "Big Brother System"?).
 *
 *    Places flagged with a triple question-mark are things which are either
 *    unfinished, questionable, or wrong.
 */

#ifdef MODULE
#include <linux/module.h>
#endif

#include <linux/string.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/head.h>
#include <linux/types.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/config.h>

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

#include "../block/blk.h"
#include "scsi.h"
#include "hosts.h"
#include "sd.h"
#define BUSLOGIC_PRIVATE_H	/* Get the "private" stuff */
#include "buslogic.h"

#ifndef BUSLOGIC_DEBUG
# define BUSLOGIC_DEBUG 0
#endif

/* ??? Until kmalloc actually implements GFP_DMA, we can't depend on it... */
#undef GFP_DMA

/* If different port addresses are needed (e.g. to install more than two
   cards), you must define BUSLOGIC_PORT_OVERRIDE to be a comma-separated list
   of the addresses which will be checked.  This can also be used to resolve a
   conflict if the port-probing at a standard port causes problems with
   another board. */
/* #define BUSLOGIC_PORT_OVERRIDE 0x330, 0x334, 0x130, 0x134, 0x230, 0x234 */

/* Define this to be either BIOS_TRANSLATION_DEFAULT or BIOS_TRANSLATION_BIG
   if you wish to bypass the test for this, which uses an undocumented port.
   The test is believed to fail on at least some AMI BusLogic clones. */
/* #define BIOS_TRANSLATION_OVERRIDE BIOS_TRANSLATION_BIG */

#define BUSLOGIC_VERSION "1.15"

/* Not a random value - if this is too large, the system hangs for a long time
   waiting for something to happen if a board is not installed. */
/* ??? I don't really like this as it will wait longer on slow machines.
   Perhaps we should base this on the loops_per_second "Bogomips" value? */
#define WAITNEXTTIMEOUT 3000000

/* This is for the scsi_malloc call in buslogic_queuecommand. */
/* ??? I'd up this to 4096, but would we be in danger of using up the
   scsi_malloc memory pool? */
/* This could be a concern, I guess.  It may be possible to fix things so that
   the table generated in sd.c is compatible with the low-level code, but
   don't hold your breath.  -ERY */
#define BUSLOGIC_SG_MALLOC 512

/* Since the SG list is malloced, we have to limit the length. */
#define BUSLOGIC_MAX_SG (BUSLOGIC_SG_MALLOC / sizeof (struct chain))

/* Since the host adapters have room to buffer 32 commands internally, there
   is some virtue in setting BUSLOGIC_MAILBOXES to 32.  The maximum value
   appears to be 255, since the Count parameter to the Initialize Extended
   Mailbox command is limited to one byte. */
#define BUSLOGIC_MAILBOXES 32

#define BUSLOGIC_CMDLUN 4	/* Arbitrary, but seems to work well. */

/* BusLogic boards can be configured for quite a number of port addresses (six
   to be exact), but I generally do not want the driver poking around at
   random.  We allow two port addresses - this allows people to use a BusLogic
   with a MIDI card, which frequently also uses 0x330.

   This can also be overridden on the command line to the kernel, via LILO or
   LOADLIN. */
static unsigned short bases[7] = {
#ifdef BUSLOGIC_PORT_OVERRIDE
    BUSLOGIC_PORT_OVERRIDE,
#else
    0x330, 0x334, /* 0x130, 0x134, 0x230, 0x234, */
#endif
    0
};

#define BIOS_TRANSLATION_DEFAULT 0	/* Default case */
#define BIOS_TRANSLATION_BIG 1		/* Big disk (> 1G) case */

struct hostdata {
    unsigned int bus_type;
    unsigned int bios_translation: 1;	/* BIOS mapping (for compatibility) */
    int last_mbi_used;
    int last_mbo_used;
    char model[7];
    char firmware_rev[6];
    Scsi_Cmnd *sc[BUSLOGIC_MAILBOXES];
    struct mailbox mb[2 * BUSLOGIC_MAILBOXES];
    struct ccb ccbs[BUSLOGIC_MAILBOXES];
};

#define HOSTDATA(host) ((struct hostdata *)&(host)->hostdata)

/* One for each IRQ level (9-15), although 13 will never be used. */
static struct Scsi_Host *host[7] = { NULL, };

static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt);
static int restart(struct Scsi_Host *shpnt);

#define INTR_RESET(base) outb(RINT, CONTROL(base))

#define buslogic_printk buslogic_prefix(__PRETTY_FUNCTION__),printk

#if defined(MODULE) && !defined(GFP_DMA)
# define CHECK_DMA_ADDR(isa, addr, badstmt) \
    do { if ((isa) && (addr) > (void *)ISA_DMA_THRESHOLD) badstmt; } while (0)
#else
# define CHECK_DMA_ADDR(isa, addr, badstmt)
#endif

#define CHECK(cond) if (cond) ; else goto fail

#define WAIT(port, allof, noneof) \
    CHECK(wait(port, allof, noneof, WAITNEXTTIMEOUT, FALSE))
#define WAIT_WHILE(port, mask) WAIT(port, 0, mask)
#define WAIT_UNTIL(port, mask) WAIT(port, mask, 0)
#define WAIT_FAST(port, allof, noneof) \
    CHECK(wait(port, allof, noneof, 100, TRUE))
#define WAIT_WHILE_FAST(port, mask) WAIT_FAST(port, 0, mask)
#define WAIT_UNTIL_FAST(port, mask) WAIT_FAST(port, mask, 0)

/* If delay != 0, we use the udelay call to regulate the amount of time we
   wait.

   This is inline as it is always called with constant arguments and hence
   will be very well optimized. */
static __inline__ int wait(unsigned short port,
			   unsigned char allof, unsigned char noneof,
			   unsigned int timeout, int delay)
{
    int bits;

    for (;;) {
	bits = inb(port);
	if ((bits & allof) == allof && (bits & noneof) == 0)
	    return TRUE;
	if (delay)
	    udelay(1000);
	if (--timeout == 0)
	    return FALSE;
    }
}

static void buslogic_prefix(const char *func)
{
    printk("BusLogic SCSI: %s: ", func);
}

static void buslogic_stat(unsigned int base)
{
    int s = inb(STATUS(base)), i = inb(INTERRUPT(base));

    buslogic_printk("status=%02X intrflags=%02X\n", s, i);
}

/* This is a bit complicated, but we need to make sure that an interrupt
   routine does not send something out while we are in the middle of this.
   Fortunately, it is only at boot time that multi-byte messages are ever
   sent. */
static int buslogic_out(unsigned int base, const unsigned char *cmdp,
			size_t len)
{
    unsigned long flags = 0;
    
    if (len == 1) {
	for (;;) {
	    WAIT_WHILE(STATUS(base), CPRBSY);
	    save_flags(flags);
	    cli();
	    if (!(inb(STATUS(base)) & CPRBSY)) {
		outb(*cmdp, COMMAND_PARAMETER(base));
		restore_flags(flags);
		return FALSE;
	    }
	    restore_flags(flags);
	}
    } else {
	save_flags(flags);
	cli();
	while (len--) {
	    WAIT_WHILE(STATUS(base), CPRBSY);
	    outb(*cmdp++, COMMAND_PARAMETER(base));
	}
	restore_flags(flags);
    }
    return FALSE;
  fail:
    restore_flags(flags);
    buslogic_printk("failed(%u): ", len + 1);
    buslogic_stat(base);
    return TRUE;
}

/* Only used at boot time, so we do not need to worry about latency as much
   here.  This waits a very short period of time.  We use this if we are not
   sure whether the board will respond to the command we just sent. */
static int buslogic_in(unsigned int base, unsigned char *cmdp, size_t len)
{
    unsigned long flags;
    
    save_flags(flags);
    cli();
    while (len--) {
	WAIT_UNTIL_FAST(STATUS(base), DIRRDY);
	*cmdp++ = inb(DATA_IN(base));
    }
    restore_flags(flags);
    return FALSE;
  fail:
    restore_flags(flags);
#if (BUSLOGIC_DEBUG & BD_IO)
    buslogic_printk("failed(%u): ", len + 1);
    buslogic_stat(base);
#endif
    return TRUE;
}

static unsigned int makecode(unsigned int haerr, unsigned int scsierr)
{
    unsigned int hosterr;
    const char *errstr = NULL;
#if (BUSLOGIC_DEBUG & BD_ERRORS) && defined(CONFIG_SCSI_CONSTANTS)
    static const char *const buslogic_status[] = {
    /* 00 */	"Command completed normally",
    /* 01-07 */	NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    /* 08-09 */	NULL, NULL,
    /* 0A */	"Linked command completed normally",
    /* 0B */	"Linked command completed normally, interrupt generated",
    /* 0C-0F */	NULL, NULL, NULL, NULL,
    /* 10 */	NULL,
    /* 11 */	"Selection timed out",
    /* 12 */	"Data overrun/underrun",
    /* 13 */	"Unexpected bus free",
    /* 14 */	"Target bus phase sequence failure",
    /* 15 */	"First byte of outgoing MB was invalid",
    /* 16 */	"Invalid CCB Operation Code",
    /* 17 */	"Linked CCB does not have the same LUN",
    /* 18 */	"Invalid Target Direction received from Host",
    /* 19 */	"Duplicate CCB Received in Target Mode",
    /* 1A */	"Invalid CCB or Segment List Parameter",
    /* 1B */	"Auto request sense failed",
    /* 1C */	"SCSI-2 tagged queueing message was rejected by the target",
    /* 1D-1F */	NULL, NULL, NULL,
    /* 20 */	"Host adapter hardware failure",
    /* 21 */	"Target did not respond to SCSI ATN and the HA SCSI bus reset",
    /* 22 */	"Host adapter asserted a SCSI bus reset",
    /* 23 */	"Other SCSI devices asserted a SCSI bus reset",
    };
#endif

    switch (haerr) {
      case 0x00:	/* Normal completion. */
      case 0x0A:	/* Linked command complete without error and linked
			   normally. */
      case 0x0B:	/* Linked command complete without error, interrupt
			   generated. */
	hosterr = DID_OK;
	break;

      case 0x11:	/* Selection time out: the initiator selection or
			   target reselection was not complete within the SCSI
			   time out period. */
	hosterr = DID_TIME_OUT;
	break;

      case 0x14:	/* Target bus phase sequence failure - An invalid bus
			   phase or bus phase sequence was requested by the
			   target.  The host adapter will generate a SCSI
			   Reset Condition, notifying the host with a RSTS
			   interrupt. */
      case 0x21:	/* The target did not respond to SCSI ATN and the host
			   adapter consequently issued a SCSI bus reset to
			   clear up the failure. */
      case 0x22:	/* The host adapter asserted a SCSI bus reset. */
	hosterr = DID_RESET;
	break;

      case 0x12:	/* Data overrun/underrun: the target attempted to
			   transfer more data than was allocated by the Data
			   Length field or the sum of the Scatter/Gather Data
			   Length fields. */
      case 0x13:	/* Unexpected bus free - The target dropped the SCSI
			   BSY at an unexpected time. */
      case 0x15:	/* MBO command was not 00, 01, or 02 - The first byte
			   of the MB was invalid.  This usually indicates a
			   software failure. */
      case 0x16:	/* Invalid CCB Operation Code - The first byte of the
			   CCB was invalid.  This usually indicates a software
			   failure. */
      case 0x17:	/* Linked CCB does not have the same LUN - A
			   subsequent CCB of a set of linked CCB's does not
			   specify the same logical unit number as the
			   first. */
      case 0x18:	/* Invalid Target Direction received from Host - The
			   direction of a Target Mode CCB was invalid. */
      case 0x19:	/* Duplicate CCB Received in Target Mode - More than
			   once CCB was received to service data transfer
			   between the same target LUN and initiator SCSI ID
			   in the same direction. */
      case 0x1A:	/* Invalid CCB or Segment List Parameter - A segment
			   list with a zero length segment or invalid segment
			   list boundaries was received.  A CCB parameter was
			   invalid. */
      case 0x1B:	/* Auto request sense failed. */
      case 0x1C:	/* SCSI-2 tagged queueing message was rejected by the
			   target. */
      case 0x20:	/* The host adapter hardware failed. */
      case 0x23:	/* Other SCSI devices asserted a SCSI bus reset. */
	hosterr = DID_ERROR;	/* ??? Couldn't find any better. */
	break;

      default:
#ifndef CONFIG_SCSI_CONSTANTS
	errstr = "unknown hoststatus";
#endif
	hosterr = DID_ERROR;
	break;
    }
#if (BUSLOGIC_DEBUG & BD_ERRORS)
# ifdef CONFIG_SCSI_CONSTANTS
    if (hosterr != DID_OK) {
	if (haerr < ARRAY_SIZE(buslogic_status))
	    errstr = buslogic_status[haerr];
	if (errstr == NULL)
	    errstr = "unknown hoststatus";
    }
# else
    if (hosterr == DID_ERROR)
	errstr = "";
# endif
#endif
    if (errstr != NULL)
	buslogic_printk("%s (%02X)\n", errstr, haerr);
    return (hosterr << 16) | scsierr;
}

/* ??? this should really be "const struct Scsi_Host *" */
const char *buslogic_info(struct Scsi_Host *shpnt)
{
    return "BusLogic SCSI driver " BUSLOGIC_VERSION;
}

/*
  This is a major rewrite of the interrupt handler to support the newer
  and faster PCI cards.  While the previous interrupt handler was supposed
  to handle multiple incoming becoming available mailboxes during the same
  interrupt, my testing showed that in practice only a single mailbox was
  ever made available.  With the 946C and 956C, multiple incoming mailboxes
  being ready for processing during a single interrupt occurs much more
  frequently, and so care must be taken to avoid race conditions managing
  the Host Adapter Interrupt Register, which can lead to lost interrupts.

  Leonard N. Zubkoff, 23-Mar-95
*/

static void buslogic_interrupt(int irq, struct pt_regs * regs)
{
    int mbi, saved_mbo[BUSLOGIC_MAILBOXES];
    int base, interrupt_flags, found, i;
    struct Scsi_Host *shpnt;
    Scsi_Cmnd *sctmp;
    struct mailbox *mb;
    struct ccb *ccb;

    shpnt = host[irq - 9];
    if (shpnt == NULL)
      panic("buslogic_interrupt: NULL SCSI host entry");

    mb = HOSTDATA(shpnt)->mb;
    ccb = HOSTDATA(shpnt)->ccbs;
    base = shpnt->io_port;

    /*
      This interrupt handler is now specified to use the SA_INTERRUPT
      protocol, so interrupts are inhibited on entry until explicitly
      allowed again.  Read the Host Adapter Interrupt Register, and
      complain if there is no pending interrupt being signaled.
    */

    interrupt_flags = inb(INTERRUPT(base));

    if (!(interrupt_flags & INTV))
      buslogic_printk("interrupt received, but INTV not set\n");

    /*
      Reset the Host Adapter Interrupt Register.  It appears to be
      important that this is only done once per interrupt to avoid
      losing interrupts under heavy loads.
    */

    INTR_RESET(base);

    if (interrupt_flags & RSTS)
      {
	restart(shpnt);
	return;
      }

    /*
      With interrupts still inhibited, scan through the incoming mailboxes
      in strict round robin fashion saving the status information and
      then freeing the mailbox.  A second pass over the completed commands
      will be made separately to complete their processing.
    */

    mbi = HOSTDATA(shpnt)->last_mbi_used + 1;
    if (mbi >= 2*BUSLOGIC_MAILBOXES)
      mbi = BUSLOGIC_MAILBOXES;

    found = 0;

    while (mb[mbi].status != MBX_NOT_IN_USE && found < BUSLOGIC_MAILBOXES)
      {
	int mbo = (struct ccb *)mb[mbi].ccbptr - ccb;

	sctmp = HOSTDATA(shpnt)->sc[mbo];

	/*
	  If sctmp has become NULL, higher level code must have aborted
	  this operation and called the necessary completion routine.
	*/

	if (sctmp != NULL && mb[mbi].status != MBX_COMPLETION_NOT_FOUND)
	  {
	    int result = 0;

	    saved_mbo[found++] = mbo;

	    if (mb[mbi].status != MBX_COMPLETION_OK)
	      result = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);

	    sctmp->result = result;

	    mb[mbi].status = MBX_NOT_IN_USE;
	  }

	HOSTDATA(shpnt)->last_mbi_used = mbi;

	if (++mbi >= 2*BUSLOGIC_MAILBOXES)
	  mbi = BUSLOGIC_MAILBOXES;
      }

    /*
      With interrupts no longer inhibited, iterate over the completed
      commands freeing resources and calling the completion routines.
      Since we exit upon completion of this loop, there is no need to
      inhibit interrupts before exit, as this will be handled by the
      fast interrupt assembly code we return to.
    */

    sti();

    for (i = 0; i < found; i++)
      {
	int mbo = saved_mbo[i];
	sctmp = HOSTDATA(shpnt)->sc[mbo];
	if (sctmp == NULL) continue;
	/*
	  First, free any storage allocated for a scatter/gather
	  data segment list.
	*/
	if (sctmp->host_scribble)
	  scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
	/*
	  Next, mark the SCSI Command as completed so it may be reused
	  for another command by buslogic_queuecommand.  This also signals
	  to buslogic_reset that the command is no longer active.
	*/
	HOSTDATA(shpnt)->sc[mbo] = NULL;
	/*
	  Finally, call the SCSI command completion handler.
	*/
	sctmp->scsi_done(sctmp);
      }
}


/* ??? Why does queuecommand return a value?  scsi.c never looks at it... */
int buslogic_queuecommand(Scsi_Cmnd *scpnt, void (*done)(Scsi_Cmnd *))
{
    static const unsigned char buscmd[] = { CMD_START_SCSI };
    unsigned char direction;
    unsigned char *cmd = (unsigned char *)scpnt->cmnd;
    unsigned char target = scpnt->target;
    unsigned char lun = scpnt->lun;
    void *buff = scpnt->request_buffer;
    int bufflen = scpnt->request_bufflen;
    int mbo;
    unsigned long flags;
    struct Scsi_Host *shpnt = scpnt->host;
    struct mailbox *mb = HOSTDATA(shpnt)->mb;
    struct ccb *ccb;


#if (BUSLOGIC_DEBUG & BD_COMMAND)
    if (target > 1) {
	scpnt->result = DID_TIME_OUT << 16;
	done(scpnt);
	return 0;
    }
#endif

    if (*cmd == REQUEST_SENSE) {
#if (BUSLOGIC_DEBUG & (BD_COMMAND | BD_ERRORS))
	if (bufflen != sizeof scpnt->sense_buffer) {
	    buslogic_printk("wrong buffer length supplied for request sense"
			    " (%d).\n",
			    bufflen);
	}
#endif
	scpnt->result = 0;
	done(scpnt);
	return 0;
    }

#if (BUSLOGIC_DEBUG & BD_COMMAND)
    {
	int i;

	if (*cmd == READ_10 || *cmd == WRITE_10
	    || *cmd == READ_6 || *cmd == WRITE_6)
	    i = *(int *)(cmd + 2);
	else
	    i = -1;
	buslogic_printk("dev %d cmd %02X pos %d len %d ",
			target, *cmd, i, bufflen);
	buslogic_stat(shpnt->io_port);
	buslogic_printk("dumping scsi cmd:");
	for (i = 0; i < scpnt->cmd_len; i++)
	    printk(" %02X", cmd[i]);
	printk("\n");
	if (*cmd == WRITE_10 || *cmd == WRITE_6)
	    return 0;	/* we are still testing, so *don't* write */
    }
#endif

    /* Use the outgoing mailboxes in a round-robin fashion, because this
       is how the host adapter will scan for them. */

    save_flags(flags);
    cli();

    mbo = HOSTDATA(shpnt)->last_mbo_used + 1;
    if (mbo >= BUSLOGIC_MAILBOXES)
	mbo = 0;

    do {
	if (mb[mbo].status == MBX_NOT_IN_USE
	    && HOSTDATA(shpnt)->sc[mbo] == NULL)
	    break;
	mbo++;
	if (mbo >= BUSLOGIC_MAILBOXES)
	    mbo = 0;
    } while (mbo != HOSTDATA(shpnt)->last_mbo_used);

    if (mb[mbo].status != MBX_NOT_IN_USE || HOSTDATA(shpnt)->sc[mbo]) {
	/* ??? Instead of failing, should we enable OMBR interrupts and sleep
	   until we get one? */
	restore_flags(flags);
	buslogic_printk("unable to find empty mailbox.\n");
	goto fail;
    }

    HOSTDATA(shpnt)->sc[mbo] = scpnt;		/* This will effectively
						   prevent someone else from
						   screwing with this cdb. */

    HOSTDATA(shpnt)->last_mbo_used = mbo;

    restore_flags(flags);

#if (BUSLOGIC_DEBUG & BD_COMMAND)
    buslogic_printk("sending command (%d %08X)...", mbo, done);
#endif

    ccb = &HOSTDATA(shpnt)->ccbs[mbo];

    /* This gets trashed for some reason */
    mb[mbo].ccbptr = ccb;

    memset(ccb, 0, sizeof (struct ccb));

    ccb->cdblen = scpnt->cmd_len;		/* SCSI Command Descriptor
						   Block Length */

    direction = 0;
    if (*cmd == READ_10 || *cmd == READ_6)
	direction = 8;
    else if (*cmd == WRITE_10 || *cmd == WRITE_6)
	direction = 16;

    memcpy(ccb->cdb, cmd, ccb->cdblen);

    if (scpnt->use_sg) {
	struct scatterlist *sgpnt;
	struct chain *cptr;
	size_t i;

	ccb->op = CCB_OP_INIT_SG;	/* SCSI Initiator Command
					   w/scatter-gather */
	scpnt->host_scribble
	    = (unsigned char *)scsi_malloc(BUSLOGIC_SG_MALLOC);
	if (scpnt->host_scribble == NULL) {
	    buslogic_printk("unable to allocate DMA memory.\n");
	    goto fail;
	}
	sgpnt = (struct scatterlist *)scpnt->request_buffer;
	cptr = (struct chain *)scpnt->host_scribble;
	if (scpnt->use_sg > shpnt->sg_tablesize) {
	    buslogic_printk("bad segment list, %d > %d.\n",
			    scpnt->use_sg, shpnt->sg_tablesize);
	    goto fail;
	}
	for (i = 0; i < scpnt->use_sg; i++) {
	    CHECK_DMA_ADDR(shpnt->unchecked_isa_dma, sgpnt[i].address,
			   goto baddma);
	    cptr[i].dataptr = sgpnt[i].address;
	    cptr[i].datalen = sgpnt[i].length;
	}
	ccb->datalen = scpnt->use_sg * sizeof (struct chain);
	ccb->dataptr = cptr;
#if (BUSLOGIC_DEBUG & BD_COMMAND)
	{
	    unsigned char *ptr;

	    buslogic_printk("cptr %08X:", cptr);
	    ptr = (unsigned char *)cptr;
	    for (i = 0; i < 18; i++)
		printk(" %02X", ptr[i]);
	    printk("\n");
	}
#endif
    } else {
	ccb->op = CCB_OP_INIT;	/* SCSI Initiator Command */
	scpnt->host_scribble = NULL;
	CHECK_DMA_ADDR(shpnt->unchecked_isa_dma, buff, goto baddma);
	ccb->datalen = bufflen;
	ccb->dataptr = buff;
    }
    ccb->id = target;
    ccb->lun = lun;
    ccb->dir = direction;
    ccb->rsalen = sizeof scpnt->sense_buffer;
    ccb->senseptr = scpnt->sense_buffer;
    /* ccbcontrol, commlinkid, and linkptr are 0 due to above memset. */

#if (BUSLOGIC_DEBUG & BD_COMMAND)
    {
	size_t i;

	buslogic_printk("sending...");
	for (i = 0; i < sizeof(struct ccb) - 10; i++)
	    printk(" %02X", ((unsigned char *)ccb)[i]);
	printk("\n");
    }
#endif

    if (done) {
#if (BUSLOGIC_DEBUG & BD_COMMAND)
	buslogic_printk("now waiting for interrupt: ");
	buslogic_stat(shpnt->io_port);
#endif
	scpnt->scsi_done = done;
	mb[mbo].status = MBX_ACTION_START;
	/* start scsi command */
	buslogic_out(shpnt->io_port, buscmd, sizeof buscmd);
#if (BUSLOGIC_DEBUG & BD_COMMAND)
	buslogic_stat(shpnt->io_port);
#endif
    } else
	buslogic_printk("done can't be NULL.\n");

    while (0) {
#if defined(MODULE) && !defined(GFP_DMA)
      baddma:
	buslogic_printk("address > 16MB used for ISA HA.\n");
#endif
      fail:
	scpnt->result = DID_ERROR << 16;
	done(scpnt);
    }

    return 0;
}

#if 0
static void internal_done(Scsi_Cmnd *scpnt)
{
    scpnt->SCp.Status++;
}

int buslogic_command(Scsi_Cmnd *scpnt)
{
#if (BUSLOGIC_DEBUG & BD_COMMAND)
    buslogic_printk("calling buslogic_queuecommand.\n");
#endif

    buslogic_queuecommand(scpnt, internal_done);

    scpnt->SCp.Status = 0;
    while (!scpnt->SCp.Status)
	barrier();
    return scpnt->result;
}
#endif

/* Initialize mailboxes. */
static int setup_mailboxes(unsigned int base, struct Scsi_Host *shpnt)
{
    size_t i;
    int ok = FALSE;		/* Innocent until proven guilty... */
    struct mailbox *mb = HOSTDATA(shpnt)->mb;
    struct ccb *ccb = HOSTDATA(shpnt)->ccbs;
    struct {
	unsigned char cmd, count;
	void *base PACKED;
    } cmd = { CMD_INITEXTMB, BUSLOGIC_MAILBOXES, mb };

    for (i = 0; i < BUSLOGIC_MAILBOXES; i++) {
	mb[i].status = mb[BUSLOGIC_MAILBOXES + i].status = MBX_NOT_IN_USE;
	mb[i].ccbptr = &ccb[i];
    }
    INTR_RESET(base);	/* reset interrupts, so they don't block */

    if (buslogic_out(base, (unsigned char *)&cmd, sizeof cmd))
	goto fail;
    WAIT_UNTIL(INTERRUPT(base), CMDC);

    ok = TRUE;

    while (0) {
      fail:
	buslogic_printk("failed setting up mailboxes.\n");
    }

    INTR_RESET(base);

    return !ok;
}

static int getconfig(unsigned int base, unsigned char *irq,
		     unsigned char *dma, unsigned char *id,
		     char *bus_type, unsigned short *max_sg,
		     const unsigned char **bios)
{
    unsigned char inquiry_cmd[2];
    unsigned char inquiry_result[4];
    int i;

#if (BUSLOGIC_DEBUG & BD_DETECT)
    buslogic_printk("called\n");
#endif

    i = inb(STATUS(base));
    if (i & DIRRDY)
	i = inb(DATA_IN(base));
    inquiry_cmd[0] = CMD_RETCONF;
    buslogic_out(base, inquiry_cmd, 1);
    if (buslogic_in(base, inquiry_result, 3))
	goto fail;
    WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
    INTR_RESET(base);
    /* Defer using the DMA value until we know the bus type. */
    *dma = inquiry_result[0];
    switch (inquiry_result[1]) {
      case 0x01:
	*irq = 9;
	break;
      case 0x02:
	*irq = 10;
	break;
      case 0x04:
	*irq = 11;
	break;
      case 0x08:
	*irq = 12;
	break;
      case 0x20:
	*irq = 14;
	break;
      case 0x40:
	*irq = 15;
	break;
      default:
	buslogic_printk("unable to determine BusLogic IRQ level, "
			" disabling board.\n");
	goto fail;
    }
    *id = inquiry_result[2] & 0x7;

    /* I expected Adaptec boards to fail on this, but it doesn't happen... */
    inquiry_cmd[0] = CMD_INQEXTSETUP;
    inquiry_cmd[1] = 4;
    if (buslogic_out(base, inquiry_cmd, 2))
	goto fail;
    if (buslogic_in(base, inquiry_result, inquiry_cmd[1]))
	goto fail;
    WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
    if (inb(STATUS(base)) & CMDINV)
	goto fail;
    INTR_RESET(base);

    *bus_type = inquiry_result[0];
    CHECK(*bus_type == 'A' || *bus_type == 'E' || *bus_type == 'M');

    *bios = (const unsigned char *)((unsigned int)inquiry_result[1] << 12);

    *max_sg = (inquiry_result[3] << 8) | inquiry_result[2];

    /* We only need a DMA channel for ISA boards.  Some other types of boards
       (such as the 747S) have an option to report a DMA channel even though
       none is used (for compatibility with Adaptec drivers which require a
       DMA channel).  We ignore this. */
    if (*bus_type == 'A')
	switch (*dma) {
	  case 0:	/* This indicates that no DMA channel is used. */
	    *dma = 0;
	    break;
	  case 0x20:
	    *dma = 5;
	    break;
	  case 0x40:
	    *dma = 6;
	    break;
	  case 0x80:
	    *dma = 7;
	    break;
	  default:
	    buslogic_printk("unable to determine BusLogic DMA channel,"
			    " disabling board.\n");
	    goto fail;
	}
    else
	*dma = 0;

    while (0) {
      fail:
#if (BUSLOGIC_DEBUG & BD_DETECT)
	buslogic_printk("query board settings\n");
#endif
	return TRUE;
    }

    return FALSE;
}

/* Query the board.  This acts both as part of the detection sequence and as a
   means to get necessary configuration information. */
static int buslogic_query(unsigned int base, unsigned char *trans,
			  unsigned char *irq, unsigned char *dma,
			  unsigned char *id, char *bus_type,
			  unsigned short *max_sg, const unsigned char **bios,
			  char *model, char *firmware_rev)
{
    unsigned char inquiry_cmd[2];
    unsigned char inquiry_result[6];
    unsigned char geo;
    unsigned int i;

#if (BUSLOGIC_DEBUG & BD_DETECT)
    buslogic_printk("called\n");
#endif

    /* Quick and dirty test for presence of the card. */
    if (inb(STATUS(base)) == 0xFF)
	goto fail;

    /* Check the GEOMETRY port early for quick bailout on Adaptec boards. */
    geo = inb(GEOMETRY(base));
#if (BUSLOGIC_DEBUG & BD_DETECT)
    buslogic_printk("geometry bits: %02X\n", geo);
#endif
    /* Here is where we tell the men from the boys (i.e. Adaptec's don't
       support the GEOMETRY port, the men do :-) */
    if (geo == 0xFF)
	goto fail;

    /* In case some other card was probing here, reset interrupts. */
    INTR_RESET(base);

    /* Reset the adapter.  I ought to make a hard reset, but it's not really
       necessary. */
    outb(RSOFT | RINT/* | RSBUS*/, CONTROL(base));

    /* Wait a little bit for things to settle down. */
    i = jiffies + 2;
    while (i > jiffies);

    /* Expect INREQ and HARDY, any of the others are bad. */
    WAIT(STATUS(base), INREQ | HARDY, DACT | DFAIL | CMDINV | DIRRDY | CPRBSY);

    /* Shouldn't have generated any interrupts during reset. */
    if (inb(INTERRUPT(base)) & INTRMASK)
	goto fail;

    /* Getting the BusLogic firmware revision level is a bit tricky.  We get
       the first two digits (d.d) from CMD_INQUIRY and then use two undocumented
       commands to get the remaining digit and letter (d.ddl as in 3.31C). */

    inquiry_cmd[0] = CMD_INQUIRY;
    buslogic_out(base, inquiry_cmd, 1);
    if (buslogic_in(base, inquiry_result, 4))
	goto fail;
    /* Reading port should reset DIRRDY. */
    if (inb(STATUS(base)) & DIRRDY)
	goto fail;
    WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
    INTR_RESET(base);
    firmware_rev[0] = inquiry_result[2];
    firmware_rev[1] = '.';
    firmware_rev[2] = inquiry_result[3];
    firmware_rev[3] = '\0';
#if 0
    buslogic_printk("inquiry bytes: %02X(%c) %02X(%c)\n",
		    inquiry_result[0], inquiry_result[0],
		    inquiry_result[1], inquiry_result[1]);
#endif

    if (getconfig(base, irq, dma, id, bus_type, max_sg, bios))
	goto fail;

    /* Set up defaults */
#ifdef BIOS_TRANSLATION_OVERRIDE
    *trans = BIOS_TRANSLATION_OVERRIDE;
#else
    *trans = BIOS_TRANSLATION_DEFAULT;
#endif
    model[0] = '\0';
    model[6] = 0;

    /* ??? Begin undocumented command use.
       These may not be supported by clones. */

    do {
	/* ??? It appears as though AMI BusLogic clones don't implement this
	   feature.  As an experiment, if we read a 00 we ignore the GEO_GT_1GB
	   bit and skip all further undocumented commands. */
	if (geo == 0x00)
	    break;
#ifndef BIOS_TRANSLATION_OVERRIDE
	*trans = ((geo & GEO_GT_1GB)
		  ? BIOS_TRANSLATION_BIG : BIOS_TRANSLATION_DEFAULT);
#endif

	inquiry_cmd[0] = CMD_VER_NO_LAST;
	buslogic_out(base, inquiry_cmd, 1);
	if (buslogic_in(base, inquiry_result, 1))
	    break;
	WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
	INTR_RESET(base);
	firmware_rev[3] = inquiry_result[0];
	firmware_rev[4] = '\0';

	inquiry_cmd[0] = CMD_VER_NO_LETTER;
	buslogic_out(base, inquiry_cmd, 1);
	if (buslogic_in(base, inquiry_result, 1))
	    break;
	WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
	INTR_RESET(base);
	firmware_rev[4] = inquiry_result[0];
	firmware_rev[5] = '\0';

	/* Use undocumented command to get model number and revision. */

	inquiry_cmd[0] = CMD_RET_MODEL_NO;
	inquiry_cmd[1] = 6;
	buslogic_out(base, inquiry_cmd, 2);
	if (buslogic_in(base, inquiry_result, inquiry_cmd[1]))
	    break;
	WAIT_UNTIL_FAST(INTERRUPT(base), CMDC);
	INTR_RESET(base);
	memcpy(model, inquiry_result, 5);
	model[5] = '\0';
	model[6] = inquiry_result[5];
    } while (0);

    /* ??? End undocumented command use. */

    /* bus_type from getconfig doesn't differentiate between EISA/VESA.  We
       override using the model number here. */
    switch (*bus_type) {
      case 'E':
	switch (model[0]) {
	  case '4':
	    *bus_type = 'V';
	    break;
	  case '9':
	    *bus_type = 'P';
	    break;
	  case '7':
	    break;
	  default:
	    *bus_type = 'X';
	    break;
	}
	break;
      default:
	break;
    }

    while (0) {
      fail:
#if (BUSLOGIC_DEBUG & BD_DETECT)
	buslogic_printk("query board settings\n");
#endif
	return TRUE;
    }

    return FALSE;
}

/* return non-zero on detection */
int buslogic_detect(Scsi_Host_Template *tpnt)
{
    unsigned char dma;
    unsigned char irq;
    unsigned int base;
    unsigned char id;
    char bus_type;
    unsigned short max_sg;
    unsigned char bios_translation;
    unsigned long flags;
    const unsigned char *bios;
    char *model;
    char *firmware_rev;
    struct Scsi_Host *shpnt;
    size_t indx;
    int unchecked_isa_dma;
    int count = 0;

#if (BUSLOGIC_DEBUG & BD_DETECT)
    buslogic_printk("called\n");
#endif

    tpnt->can_queue = BUSLOGIC_MAILBOXES;
    for (indx = 0; bases[indx] != 0; indx++)
	if (!check_region(bases[indx], 4)) {
	    shpnt = scsi_register(tpnt, sizeof (struct hostdata));

	    base = bases[indx];

	    model = HOSTDATA(shpnt)->model;
	    firmware_rev = HOSTDATA(shpnt)->firmware_rev;
	    if (buslogic_query(base, &bios_translation, &irq, &dma, &id,
			       &bus_type, &max_sg, &bios, model, firmware_rev))
		goto unregister;

#if (BUSLOGIC_DEBUG & BD_DETECT)
	    buslogic_stat(base);
#endif

	    /* Only type 'A' (AT/ISA) bus adapters use unchecked DMA. */
	    unchecked_isa_dma = (bus_type == 'A');
#ifndef CONFIG_NO_BUGGY_BUSLOGIC
	    /* There is a hardware bug in the BT-445S prior to revision D.
	       When the BIOS is enabled and you have more than 16MB of memory,
	       the card mishandles memory transfers over 16MB which (if viewed
	       as a 24-bit address) overlap with the BIOS address space.  For
	       example if you have the BIOS located at physical address
	       0xDC000 and a DMA transfer from the card to RAM starts at
	       physical address 0x10DC000 then the transfer is messed up.  To
	       be more precise every fourth byte of the transfer is messed up.
	       (This analysis courtesy of Tomas Hurka, author of the NeXTSTEP
	       BusLogic driver.) */

	    if (bus_type == 'V'				    /* 445 */
		&& firmware_rev[0] <= '3'		    /* S */
		&& bios != NULL) {			    /* BIOS enabled */
#if 1
		/* Now that LNZ's forbidden_addr stuff is in the higher level
		   scsi code, we can use this instead. */
		/* Avoid addresses which "mirror" the BIOS for DMA. */
		shpnt->forbidden_addr = (unsigned long)bios;
		shpnt->forbidden_size = 16 * 1024;
#else
		/* Use double-buffering. */
		unchecked_isa_dma = TRUE;
#endif
	    }
#endif

	    CHECK_DMA_ADDR(unchecked_isa_dma, shpnt, goto unregister);

	    if (setup_mailboxes(base, shpnt))
		goto unregister;

	    /* Set the Bus on/off-times as not to ruin floppy performance.
	       CMD_BUSOFF_TIME is a noop for EISA boards (and possibly
	       others???). */
	    if (bus_type != 'E' && bus_type != 'P') {
		/* The default ON/OFF times for BusLogic adapters is 7/4. */
		static const unsigned char oncmd[] = { CMD_BUSON_TIME, 7 };
		static const unsigned char offcmd[] = { CMD_BUSOFF_TIME, 5 };

		INTR_RESET(base);
		buslogic_out(base, oncmd, sizeof oncmd);
		WAIT_UNTIL(INTERRUPT(base), CMDC);
		INTR_RESET(base);
		buslogic_out(base, offcmd, sizeof offcmd);
		WAIT_UNTIL(INTERRUPT(base), CMDC);
		while (0) {
		  fail:
		    buslogic_printk("setting bus on/off-time failed.\n");
		}
		INTR_RESET(base);
	    }

	    buslogic_printk("configuring %s HA at port 0x%03X, IRQ %u",
			    (bus_type == 'A' ? "ISA"
			     : (bus_type == 'E' ? "EISA"
				: (bus_type == 'M' ? "MCA"
				   : (bus_type == 'P' ? "PCI"
				      : (bus_type == 'V' ? "VESA"
					 : (bus_type == 'X' ? "EISA/VESA/PCI"
					    : "Unknown")))))),
			    base, irq);
	    if (bios != NULL)
		printk(", BIOS 0x%05X", (unsigned int)bios);
	    if (dma != 0)
		printk(", DMA %u", dma);
	    printk(", ID %u\n", id);
	    buslogic_printk("Model Number: %s",
			    (model[0] ? model : "Unknown"));
	    if (model[0])
		printk(" (revision %d)", model[6]);
	    printk("\n");
	    buslogic_printk("firmware revision: %s\n", firmware_rev);

#if (BUSLOGIC_DEBUG & BD_DETECT)
	    buslogic_stat(base);
#endif

#if (BUSLOGIC_DEBUG & BD_DETECT)
	    buslogic_printk("enable interrupt channel %d.\n", irq);
#endif

	    save_flags(flags);
	    cli();
	    if (request_irq(irq, buslogic_interrupt,
			    SA_INTERRUPT, "buslogic")) {
		buslogic_printk("unable to allocate IRQ for "
				"BusLogic controller.\n");
		restore_flags(flags);
		goto unregister;
	    }

	    if (dma) {
		if (request_dma(dma, "buslogic")) {
		    buslogic_printk("unable to allocate DMA channel for "
				    "BusLogic controller.\n");
		    free_irq(irq);
		    restore_flags(flags);
		    goto unregister;
		}

		/* The DMA-Controller.  We need to fool with this because we
		   want to be able to use an ISA BusLogic without having to
		   have the BIOS enabled. */
		set_dma_mode(dma, DMA_MODE_CASCADE);
		enable_dma(dma);
	    }

	    host[irq - 9] = shpnt;
	    shpnt->this_id = id;
	    shpnt->unchecked_isa_dma = unchecked_isa_dma;
	    /* Have to keep cmd_per_lun at 1 for ISA machines otherwise lots
	       of memory gets sucked up for bounce buffers.  */
	    shpnt->cmd_per_lun = (unchecked_isa_dma ? 1 : BUSLOGIC_CMDLUN);
	    shpnt->sg_tablesize = max_sg;
	    if (shpnt->sg_tablesize > BUSLOGIC_MAX_SG)
		shpnt->sg_tablesize = BUSLOGIC_MAX_SG;
	    /* ??? shpnt->base should really be "const unsigned char *"... */
	    shpnt->base = (unsigned char *)bios;
	    shpnt->io_port = base;
	    shpnt->n_io_port = 4;	/* Number of bytes of I/O space used */
	    shpnt->dma_channel = dma;
	    shpnt->irq = irq;
	    HOSTDATA(shpnt)->bios_translation = bios_translation;
	    if (bios_translation == BIOS_TRANSLATION_BIG)
		buslogic_printk("using extended bios translation.\n");
	    HOSTDATA(shpnt)->last_mbi_used = 2 * BUSLOGIC_MAILBOXES - 1;
	    HOSTDATA(shpnt)->last_mbo_used = BUSLOGIC_MAILBOXES - 1;
	    memset(HOSTDATA(shpnt)->sc, 0, sizeof HOSTDATA(shpnt)->sc);
	    restore_flags(flags);

#if 0
	    {
		unsigned char buf[8];
		unsigned char cmd[]
		    = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		size_t i;

#if (BUSLOGIC_DEBUG & BD_DETECT)
		buslogic_printk("*** READ CAPACITY ***\n");
#endif
		for (i = 0; i < sizeof buf; i++)
		    buf[i] = 0x87;
		for (i = 0; i < 2; i++)
		    if (!buslogic_command(i, cmd, buf, sizeof buf)) {
			buslogic_printk("LU %u sector_size %d device_size %d\n",
					i, *(int *)(buf + 4), *(int *)buf);
		    }

#if (BUSLOGIC_DEBUG & BD_DETECT)
		buslogic_printk("*** NOW RUNNING MY OWN TEST ***\n");
#endif
		for (i = 0; i < 4; i++) {
		    static buffer[512];

		    cmd[0] = READ_10;
		    cmd[1] = 0;
		    xany2scsi(cmd + 2, i);
		    cmd[6] = 0;
		    cmd[7] = 0;
		    cmd[8] = 1;
		    cmd[9] = 0;
		    buslogic_command(0, cmd, buffer, sizeof buffer);
		}
	    }
#endif

	    request_region(bases[indx], 4,"buslogic");
	    /* Register the IO ports that we use */
	    count++;
	    continue;
	  unregister:
	    scsi_unregister(shpnt);
	}
    return count;
}

static int restart(struct Scsi_Host *shpnt)
{
    unsigned int i;
    unsigned int count = 0;
#if 0
    static const unsigned char buscmd[] = { CMD_START_SCSI };
#endif

    for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
	if (HOSTDATA(shpnt)->sc[i]
	    && !HOSTDATA(shpnt)->sc[i]->device->soft_reset) {
#if 0
	    HOSTDATA(shpnt)->mb[i].status
		= MBX_ACTION_START;	/* Indicate ready to restart... */
#endif
	    count++;
	}

    buslogic_printk("potential to restart %d stalled commands...\n", count);
#if 0
    /* start scsi command */
    if (count)
	buslogic_out(shpnt->host->io_port, buscmd, sizeof buscmd);
#endif
    return 0;
}

/* ??? The abort command for the aha1542 does not leave the device in a clean
   state where it is available to be used again.  As it is not clear whether
   the same problem exists with BusLogic boards, we will enable this and see
   if it works. */
int buslogic_abort(Scsi_Cmnd *scpnt)
{
#if 1
    static const unsigned char buscmd[] = { CMD_START_SCSI };
    struct mailbox *mb;
    int mbi, mbo, last_mbi;
    unsigned long flags;
    unsigned int i;

    buslogic_printk("%X %X\n",
		    inb(STATUS(scpnt->host->io_port)),
		    inb(INTERRUPT(scpnt->host->io_port)));

    save_flags(flags);
    cli();
    mb = HOSTDATA(scpnt->host)->mb;
    last_mbi = HOSTDATA(scpnt->host)->last_mbi_used;
    mbi = last_mbi + 1;
    if (mbi >= 2 * BUSLOGIC_MAILBOXES)
	mbi = BUSLOGIC_MAILBOXES;

    do {
	if (mb[mbi].status != MBX_NOT_IN_USE)
	    break;
	last_mbi = mbi;
	mbi++;
	if (mbi >= 2 * BUSLOGIC_MAILBOXES)
	    mbi = BUSLOGIC_MAILBOXES;
    } while (mbi != HOSTDATA(scpnt->host)->last_mbi_used);

    if (mb[mbi].status != MBX_NOT_IN_USE) {
	buslogic_printk("lost interrupt discovered on irq %d, "
			" - attempting to recover...\n",
			scpnt->host->irq);
	HOSTDATA(scpnt->host)->last_mbi_used = last_mbi;
	buslogic_interrupt(scpnt->host->irq, NULL);
	restore_flags(flags);
	return SCSI_ABORT_SUCCESS;
    }
    restore_flags(flags);

    /* OK, no lost interrupt.  Try looking to see how many pending commands we
       think we have. */
    for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
	if (HOSTDATA(scpnt->host)->sc[i]) {
	    if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
		buslogic_printk("timed out command pending for %4.4X.\n",
				scpnt->request.dev);
		if (HOSTDATA(scpnt->host)->mb[i].status != MBX_NOT_IN_USE) {
		    buslogic_printk("OGMB still full - restarting...\n");
		    buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
		}
	    } else
		buslogic_printk("other pending command: %4.4X\n",
				scpnt->request.dev);
	}
#endif

#if (BUSLOGIC_DEBUG & BD_ABORT)
    buslogic_printk("called\n");
#endif

#if 1
    /* This section of code should be used carefully - some devices cannot
       abort a command, and this merely makes it worse. */
    save_flags(flags);
    cli();
    for (mbo = 0; mbo < BUSLOGIC_MAILBOXES; mbo++)
	if (scpnt == HOSTDATA(scpnt->host)->sc[mbo]) {
	    mb[mbo].status = MBX_ACTION_ABORT;
	    buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);
	    break;
	}
    restore_flags(flags);
#endif

    return SCSI_ABORT_SNOOZE;
}

/* We do not implement a reset function here, but the upper level code assumes
   that it will get some kind of response for the command in scpnt.  We must
   oblige, or the command will hang the SCSI system.  For a first go, we assume
   that the BusLogic notifies us with all of the pending commands (it does
   implement soft reset, after all). */
int buslogic_reset(Scsi_Cmnd *scpnt)
{
    static const unsigned char buscmd[] = { CMD_START_SCSI };
    unsigned int i;

#if (BUSLOGIC_DEBUG & BD_RESET)
    buslogic_printk("called\n");
#endif
#if 0
    /* This does a scsi reset for all devices on the bus. */
    outb(RSBUS, CONTROL(scpnt->host->io_port));
#else
    /* This does a selective reset of just the one device. */
    /* First locate the ccb for this command. */
    for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
	if (HOSTDATA(scpnt->host)->sc[i] == scpnt) {
	    HOSTDATA(scpnt->host)->ccbs[i].op = CCB_OP_BUS_RESET;

	    /* Now tell the BusLogic to flush all pending commands for this
	       target. */
	    buslogic_out(scpnt->host->io_port, buscmd, sizeof buscmd);

	    /* Here is the tricky part.  What to do next.  Do we get an
	       interrupt for the commands that we aborted with the specified
	       target, or do we generate this on our own?  Try it without first
	       and see what happens. */
	    buslogic_printk("sent BUS DEVICE RESET to target %d.\n",
			    scpnt->target);

	    /* If the first does not work, then try the second.  I think the
	       first option is more likely to be correct.  Free the command
	       block for all commands running on this target... */
#if 1
	    for (i = 0; i < BUSLOGIC_MAILBOXES; i++)
		if (HOSTDATA(scpnt->host)->sc[i]
		    && HOSTDATA(scpnt->host)->sc[i]->target == scpnt->target) {
		    Scsi_Cmnd *sctmp = HOSTDATA(scpnt->host)->sc[i];

		    sctmp->result = DID_RESET << 16;
		    if (sctmp->host_scribble)
			scsi_free(sctmp->host_scribble, BUSLOGIC_SG_MALLOC);
		    buslogic_printk("sending DID_RESET for target %d.\n",
				    scpnt->target);
		    sctmp->scsi_done(scpnt);

		    HOSTDATA(scpnt->host)->sc[i] = NULL;
		    HOSTDATA(scpnt->host)->mb[i].status = MBX_NOT_IN_USE;
		}
	    return SCSI_RESET_SUCCESS;
#else
	    return SCSI_RESET_PENDING;
#endif
	}
#endif
    /* No active command at this time, so this means that each time we got some
       kind of response the last time through.  Tell the mid-level code to
       request sense information in order to decide what to do next. */
    return SCSI_RESET_PUNT;
}

/* ??? This is probably not correct for series "C" boards.  I believe these
   support separate mappings for each disk.  We would need to issue a
   CMD_READ_FW_LOCAL_RAM command to check for the particular drive being
   queried.  Note that series "C" boards can be differentiated by having
   HOSTDATA(disk->device->host)->firmware_rev[0] >= '4'. */
int buslogic_biosparam(Disk *disk, int dev, int *ip)
{
    unsigned int size = disk->capacity;

    /* ip[0] == heads, ip[1] == sectors, ip[2] == cylinders */
    if (HOSTDATA(disk->device->host)->bios_translation == BIOS_TRANSLATION_BIG
	&& size >= 0x200000) {		/* 1GB */
	if (size >= 0x400000) {		/* 2GB */
#if 0	/* ??? Used in earlier kernels, but disagrees with BusLogic info. */
	    if (mb >= 0x800000) {	/* 4GB */
		ip[0] = 256;
		ip[1] = 64;
	    } else {
		ip[0] = 256;
		ip[1] = 32;
	    }
#else
	    ip[0] = 256;
	    ip[1] = 64;
#endif
	} else {
	    ip[0] = 128;
	    ip[1] = 32;
	}
    } else {
	ip[0] = 64;
	ip[1] = 32;
    }
    ip[2] = size / (ip[0] * ip[1]);
/*    if (ip[2] > 1024)
	ip[2] = 1024; */
    return 0;
}

/* called from init/main.c */
void buslogic_setup(char *str, int *ints)
{
    static const unsigned short valid_bases[]
	= { 0x130, 0x134, 0x230, 0x234, 0x330, 0x334 };
    static size_t setup_idx = 0;
    size_t i;

    if (setup_idx >= ARRAY_SIZE(bases) - 1) {
	buslogic_printk("called too many times.  Bad LILO params?\n");
	return;
    }
    if (ints[0] != 1) {
	buslogic_printk("malformed command line.\n");
	buslogic_printk("usage: buslogic=<portbase>\n");
	return;
    }
    for (i = 0; i < ARRAY_SIZE(valid_bases); i++)
	if (valid_bases[i] == ints[1]) {
	    bases[setup_idx++] = ints[1];
	    bases[setup_idx] = 0;
	    return;
	}
    buslogic_printk("invalid base 0x%X specified.\n", ints[1]);
}

#ifdef MODULE
/* Eventually this will go into an include file, but that's later... */
Scsi_Host_Template driver_template = BUSLOGIC;

# include "scsi_module.c"
#endif