summaryrefslogtreecommitdiffstats
path: root/drivers/scsi/hosts.c
blob: b626d3fddc006aaf562086025205337b0e6d737b (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
/*
 *  hosts.c Copyright (C) 1992 Drew Eckhardt
 *          Copyright (C) 1993, 1994, 1995 Eric Youngdale
 *
 *  mid to lowlevel SCSI driver interface
 *      Initial versions: Drew Eckhardt
 *      Subsequent revisions: Eric Youngdale
 *
 *  <drew@colorado.edu>
 *
 *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
 *  Added QLOGIC QLA1280 SCSI controller kernel host support. 
 *     August 4, 1999 Fred Lewis, Intel DuPont
 */


/*
 *  This file contains the medium level SCSI
 *  host interface initialization, as well as the scsi_hosts array of SCSI
 *  hosts currently present in the system.
 */

#define __NO_VERSION__
#include <linux/module.h>

#include <linux/config.h>
#include <linux/blk.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/proc_fs.h>
#include <linux/init.h>

#define __KERNEL_SYSCALLS__

#include <linux/unistd.h>

#include "scsi.h"

#ifndef NULL
#define NULL 0L
#endif

#define HOSTS_C

#include "hosts.h"

#if defined(CONFIG_A4000T_SCSI) || \
    defined(CONFIG_WARPENGINE_SCSI) || \
    defined(CONFIG_A4091_SCSI) || \
    defined (CONFIG_GVP_TURBO_SCSI) || \
    defined (CONFIG_BLZ603EPLUS_SCSI)
#define AMIGA7XXCONFIG
#endif

#ifdef AMIGA7XXCONFIG
#include "amiga7xx.h"
#endif

#ifdef CONFIG_MVME16x_SCSI
#include "mvme16x.h"
#endif

#ifdef CONFIG_BVME6000_SCSI
#include "bvme6000.h"
#endif

#ifdef CONFIG_SCSI_SIM710
#include "sim710.h"
#endif

#ifdef CONFIG_A3000_SCSI
#include "a3000.h"
#endif

#ifdef CONFIG_A2091_SCSI
#include "a2091.h"
#endif

#ifdef CONFIG_GVP11_SCSI
#include "gvp11.h"
#endif

#ifdef CONFIG_CYBERSTORM_SCSI
#include "cyberstorm.h"
#endif

#ifdef CONFIG_CYBERSTORMII_SCSI
#include "cyberstormII.h"
#endif

#ifdef CONFIG_BLZ2060_SCSI
#include "blz2060.h"
#endif

#ifdef CONFIG_BLZ1230_SCSI
#include "blz1230.h"
#endif

#ifdef CONFIG_FASTLANE_SCSI
#include "fastlane.h"
#endif

#ifdef CONFIG_OKTAGON_SCSI
#include "oktagon_esp.h"
#endif

#ifdef CONFIG_ATARI_SCSI
#include "atari_scsi.h"
#endif

#if defined(CONFIG_MAC_SCSI) || defined(CONFIG_MAC_SCSI_OLD)
#include "mac_scsi.h"
#endif

#ifdef CONFIG_SUN3_SCSI
#include "sun3_scsi.h"
#endif

#ifdef CONFIG_SCSI_MAC_ESP
#include "mac_esp.h"
#endif

#ifdef CONFIG_SCSI_ADVANSYS
#include "advansys.h"
#endif

#ifdef CONFIG_SCSI_AHA152X
#include "aha152x.h"
#endif

#ifdef CONFIG_SCSI_AHA1542
#include "aha1542.h"
#endif

#ifdef CONFIG_SCSI_AHA1740
#include "aha1740.h"
#endif

#ifdef CONFIG_SCSI_AIC7XXX
#include "aic7xxx.h"
#endif

#ifdef CONFIG_SCSI_IPS
#include "ips.h"
#endif

#ifdef CONFIG_SCSI_BUSLOGIC
#include "BusLogic.h"
#endif

#ifdef CONFIG_SCSI_EATA_DMA
#include "eata_dma.h"
#endif

#ifdef CONFIG_SCSI_EATA_PIO
#include "eata_pio.h"
#endif

#ifdef CONFIG_SCSI_U14_34F
#include "u14-34f.h"
#endif

#ifdef CONFIG_SCSI_FD_MCS
#include "fd_mcs.h"
#endif

#ifdef CONFIG_SCSI_FUTURE_DOMAIN
#include "fdomain.h"
#endif

#ifdef CONFIG_SCSI_GENERIC_NCR5380
#include "g_NCR5380.h"
#endif

#ifdef CONFIG_SCSI_IN2000
#include "in2000.h"
#endif

#ifdef CONFIG_SCSI_PAS16
#include "pas16.h"
#endif

#ifdef CONFIG_SCSI_QLOGIC_FAS
#include "qlogicfas.h"
#endif

#ifdef CONFIG_SCSI_QLOGIC_ISP
#include "qlogicisp.h"
#endif

#ifdef CONFIG_SCSI_QLOGIC_FC
#include "qlogicfc.h"
#endif

#ifdef CONFIG_SCSI_QLOGIC_1280
#include "qla1280.h"
#endif

#ifdef CONFIG_SCSI_SEAGATE
#include "seagate.h"
#endif

#ifdef CONFIG_SCSI_T128
#include "t128.h"
#endif

#ifdef CONFIG_SCSI_DTC3280
#include "dtc.h"
#endif

#ifdef CONFIG_SCSI_NCR53C7xx
#include "53c7,8xx.h"
#endif

#ifdef CONFIG_SCSI_SYM53C8XX
#include "sym53c8xx.h"
#endif

#ifdef CONFIG_SCSI_NCR53C8XX
#include "ncr53c8xx.h"
#endif

#ifdef CONFIG_SCSI_ULTRASTOR
#include "ultrastor.h"
#endif

#ifdef CONFIG_SCSI_7000FASST
#include "wd7000.h"
#endif

#ifdef CONFIG_SCSI_MCA_53C9X
#include "mca_53c9x.h"
#endif

#ifdef CONFIG_SCSI_IBMMCA
#include "ibmmca.h"
#endif

#ifdef CONFIG_SCSI_EATA
#include "eata.h"
#endif

#ifdef CONFIG_SCSI_NCR53C406A
#include "NCR53c406a.h"
#endif

#ifdef CONFIG_SCSI_SYM53C416 
#include "sym53c416.h" 
#endif

#ifdef CONFIG_SCSI_DC390T
#include "dc390.h"
#endif

#ifdef CONFIG_SCSI_AM53C974
#include "AM53C974.h"
#endif

#ifdef CONFIG_SCSI_MEGARAID
#include "megaraid.h"
#endif

#ifdef CONFIG_SCSI_ACARD
#include "atp870u.h"
#endif

#ifdef CONFIG_SCSI_SUNESP
#include "esp.h"
#endif

#ifdef CONFIG_SCSI_SGIWD93
#include "sgiwd93.h"
#endif

#ifdef CONFIG_SCSI_QLOGICPTI
#include "qlogicpti.h"
#endif

#ifdef CONFIG_BLK_DEV_IDESCSI
#include "ide-scsi.h"
#endif

#ifdef CONFIG_SCSI_MESH
#include "mesh.h"
#endif

#ifdef CONFIG_SCSI_MAC53C94
#include "mac53c94.h"
#endif

#ifdef CONFIG_SCSI_GDTH
#include "gdth.h"
#endif

#ifdef CONFIG_SCSI_PCI2000
#include "pci2000.h"
#endif

#ifdef CONFIG_SCSI_PCI2220I
#include "pci2220i.h"
#endif

#ifdef CONFIG_SCSI_PSI240I
#include "psi240i.h"
#endif

#ifdef CONFIG_SCSI_PLUTO
#include "pluto.h"
#endif

#ifdef CONFIG_SCSI_INITIO
#include "ini9100u.h"
#endif

#ifdef CONFIG_SCSI_INIA100
#include "inia100.h"
#endif

#ifdef CONFIG_SCSI_DEBUG
#include "scsi_debug.h"
#endif

#ifdef CONFIG_SCSI_ACORNSCSI_3
#include "../acorn/scsi/acornscsi.h"
#endif

#ifdef CONFIG_SCSI_CUMANA_1
#include "../acorn/scsi/cumana_1.h"
#endif

#ifdef CONFIG_SCSI_CUMANA_2
#include "../acorn/scsi/cumana_2.h"
#endif

#ifdef CONFIG_SCSI_ECOSCSI
#include "../acorn/scsi/ecoscsi.h"
#endif

#ifdef CONFIG_SCSI_OAK1
#include "../acorn/scsi/oak.h"
#endif

#ifdef CONFIG_SCSI_POWERTECSCSI
#include "../acorn/scsi/powertec.h"
#endif

#ifdef CONFIG_JAZZ_ESP
#include "jazz_esp.h"
#endif

#ifdef CONFIG_SCSI_DECNCR
#include "dec_esp.h"
#endif

#ifdef CONFIG_SUN3X_ESP
#include "sun3x_esp.h"
#endif

#ifdef CONFIG_IPHASE5526
#include "../net/fc/iph5526_scsi.h"
#endif

#ifdef CONFIG_BLK_DEV_3W_XXXX_RAID
#include "3w-xxxx.h"
#endif

/*
 * Moved ppa driver to the end of the probe list
 * since it is a removable host adapter.
 * This means the parallel ZIP drive will not bump
 * the order of the /dev/sd devices - campbell@torque.net
 */
#ifdef CONFIG_SCSI_PPA
#include "ppa.h"
#endif

#ifdef CONFIG_SCSI_IMM
#include "imm.h"
#endif

/*
static const char RCSid[] = "$Header: /vger/u4/cvs/linux/drivers/scsi/hosts.c,v 1.20 1996/12/12 19:18:32 davem Exp $";
*/

/*
 *  The scsi host entries should be in the order you wish the
 *  cards to be detected.  A driver may appear more than once IFF
 *  it can deal with being detected (and therefore initialized)
 *  with more than one simultaneous host number, can handle being
 *  reentrant, etc.
 *
 *  They may appear in any order, as each SCSI host is told which host 
 *  number it is during detection.
 */

/* This is a placeholder for controllers that are not configured into
 * the system - we do this to ensure that the controller numbering is
 * always consistent, no matter how the kernel is configured. */

#define NO_CONTROLLER {NULL, NULL, NULL, NULL, NULL, NULL, NULL, \
			   NULL, NULL, 0, 0, 0, 0, 0, 0}

/*
 *  When figure is run, we don't want to link to any object code.  Since
 *  the macro for each host will contain function pointers, we cannot
 *  use it and instead must use a "blank" that does no such
 *  idiocy.
 */

Scsi_Host_Template * scsi_hosts = NULL;

static Scsi_Host_Template builtin_scsi_hosts[] =
{
#ifdef CONFIG_AMIGA
#ifdef AMIGA7XXCONFIG
	AMIGA7XX_SCSI,
#endif
#ifdef CONFIG_A3000_SCSI
	A3000_SCSI,
#endif
#ifdef CONFIG_A2091_SCSI
	A2091_SCSI,
#endif
#ifdef CONFIG_GVP11_SCSI
	GVP11_SCSI,
#endif
#ifdef CONFIG_CYBERSTORM_SCSI
	SCSI_CYBERSTORM,
#endif
#ifdef CONFIG_CYBERSTORMII_SCSI
	SCSI_CYBERSTORMII,
#endif
#ifdef CONFIG_BLZ2060_SCSI
	SCSI_BLZ2060,
#endif
#ifdef CONFIG_BLZ1230_SCSI
	SCSI_BLZ1230,
#endif
#ifdef CONFIG_FASTLANE_SCSI
	SCSI_FASTLANE,
#endif
#ifdef CONFIG_OKTAGON_SCSI
	SCSI_OKTAGON_ESP,
#endif
#endif

#ifdef CONFIG_ATARI
#ifdef CONFIG_ATARI_SCSI
	ATARI_SCSI,
#endif
#endif

#ifdef CONFIG_MAC
#ifdef CONFIG_MAC_SCSI_OLD
	MAC_SCSI,
#endif
#ifdef CONFIG_SCSI_MAC_ESP
        SCSI_MAC_ESP,
#endif
#ifdef CONFIG_MAC_SCSI
	MAC_NCR5380,
#endif
#endif

#ifdef CONFIG_SUN3_SCSI
	SUN3_NCR5380,
#endif

#ifdef CONFIG_MVME16x_SCSI
	MVME16x_SCSI,
#endif
#ifdef CONFIG_BVME6000_SCSI
	BVME6000_SCSI,
#endif
#ifdef CONFIG_SCSI_SIM710
	SIM710_SCSI,
#endif
#ifdef CONFIG_SCSI_ADVANSYS
	ADVANSYS,
#endif

#ifdef CONFIG_SCSI_PCI2000
        PCI2000,
#endif
#ifdef CONFIG_SCSI_PCI2220I
        PCI2220I,
#endif
#ifdef CONFIG_SCSI_PSI240I
        PSI240I,
#endif

/* BusLogic must come before aha1542.c */
#ifdef CONFIG_SCSI_BUSLOGIC
    BUSLOGIC,
#endif
#ifdef CONFIG_SCSI_U14_34F
    ULTRASTOR_14_34F,
#endif
#ifdef CONFIG_SCSI_ULTRASTOR
    ULTRASTOR_14F,
#endif
#ifdef CONFIG_SCSI_AHA152X
    AHA152X,
#endif
#ifdef CONFIG_SCSI_AHA1542
    AHA1542,
#endif
#ifdef CONFIG_SCSI_AHA1740
    AHA1740,
#endif
#ifdef CONFIG_SCSI_AIC7XXX
    AIC7XXX,
#endif
#ifdef CONFIG_SCSI_IPS
    IPS,
#endif
#ifdef CONFIG_SCSI_FD_MCS
   FD_MCS,
#endif
#ifdef CONFIG_SCSI_FUTURE_DOMAIN
    FDOMAIN_16X0,
#endif
#ifdef CONFIG_SCSI_IN2000
    IN2000,
#endif
#ifdef CONFIG_SCSI_GENERIC_NCR5380
    GENERIC_NCR5380,
#endif
#ifdef CONFIG_SCSI_NCR53C406A	/* 53C406A should come before QLOGIC */
    NCR53c406a,
#endif
#ifdef CONFIG_SCSI_SYM53C416 
    SYM53C416, 
#endif
#ifdef CONFIG_SCSI_QLOGIC_FAS
    QLOGICFAS,
#endif
#ifdef CONFIG_SCSI_QLOGIC_ISP
    QLOGICISP,
#endif
#ifdef CONFIG_SCSI_QLOGIC_FC
    QLOGICFC,
#endif
#ifdef CONFIG_SCSI_QLOGIC_1280
    QLA1280_LINUX_TEMPLATE, 
#endif
#ifdef CONFIG_SCSI_PAS16
    MV_PAS16,
#endif
#ifdef CONFIG_SCSI_SEAGATE
    SEAGATE_ST0X,
#endif
#ifdef CONFIG_SCSI_T128
    TRANTOR_T128,
#endif
#ifdef CONFIG_SCSI_DTC3280
        DTC3x80,
#endif
#ifdef CONFIG_SCSI_NCR53C7xx
    NCR53c7xx,
#endif
#ifdef CONFIG_SCSI_SYM53C8XX
    SYM53C8XX,
#endif
#ifdef CONFIG_SCSI_NCR53C8XX
    NCR53C8XX,
#endif
#ifdef CONFIG_SCSI_EATA_DMA
    EATA_DMA,
#endif
#ifdef CONFIG_SCSI_EATA_PIO
    EATA_PIO,
#endif
#ifdef CONFIG_SCSI_7000FASST
    WD7000,
#endif
#ifdef CONFIG_SCSI_MCA_53C9X
    MCA_53C9X,
#endif
#ifdef CONFIG_SCSI_IBMMCA
    IBMMCA,
#endif
#ifdef CONFIG_SCSI_EATA
    EATA,
#endif
#ifdef CONFIG_SCSI_DC390T
    DC390_T,
#endif
#ifdef CONFIG_SCSI_AM53C974
    AM53C974,
#endif
#ifdef CONFIG_SCSI_MEGARAID
    MEGARAID,
#endif
#ifdef CONFIG_SCSI_ACARD
    ATP870U,
#endif
#ifdef CONFIG_SCSI_SUNESP
    SCSI_SPARC_ESP,
#endif
#ifdef CONFIG_SCSI_GDTH
    GDTH,
#endif
#ifdef CONFIG_SCSI_INITIO
    INI9100U,
#endif
#ifdef CONFIG_SCSI_INIA100
    INIA100,
#endif
#ifdef CONFIG_SCSI_QLOGICPTI
    QLOGICPTI,
#endif
#ifdef CONFIG_BLK_DEV_IDESCSI
    IDESCSI,
#endif
#ifdef CONFIG_SCSI_MESH
    SCSI_MESH,
#endif
#ifdef CONFIG_SCSI_MAC53C94
    SCSI_MAC53C94,
#endif
#ifdef CONFIG_SCSI_PLUTO
    PLUTO,
#endif
#ifdef CONFIG_ARCH_ACORN
#ifdef CONFIG_SCSI_ACORNSCSI_3
    ACORNSCSI_3,
#endif
#ifdef CONFIG_SCSI_CUMANA_1
    CUMANA_NCR5380,
#endif
#ifdef CONFIG_SCSI_CUMANA_2
    CUMANA_FAS216,
#endif
#ifdef CONFIG_SCSI_ECOSCSI
    ECOSCSI_NCR5380,
#endif
#ifdef CONFIG_SCSI_OAK1
    OAK_NCR5380,
#endif
#ifdef CONFIG_SCSI_POWERTECSCSI
    POWERTECSCSI,
#endif
#endif
#ifdef CONFIG_IPHASE5526
	IPH5526_SCSI_FC,
#endif
#ifdef CONFIG_SCSI_DECNCR	
    SCSI_DEC_ESP,
#endif
#ifdef CONFIG_BLK_DEV_3W_XXXX_RAID
	TWXXXX,
#endif
/* "Removable host adapters" below this line (Parallel Port/USB/other) */
#ifdef CONFIG_SCSI_PPA
    PPA,
#endif
#ifdef CONFIG_SCSI_IMM
    IMM,
#endif
#ifdef CONFIG_SCSI_SGIWD93
    SGIWD93_SCSI,
#endif
#ifdef CONFIG_JAZZ_ESP	
    SCSI_JAZZ_ESP,
#endif
#ifdef CONFIG_SUN3X_ESP  
    SCSI_SUN3X_ESP,
#endif
#ifdef CONFIG_SCSI_DEBUG
    SCSI_DEBUG,
#endif
};

#define MAX_SCSI_HOSTS (sizeof(builtin_scsi_hosts) / sizeof(Scsi_Host_Template))


/*
 *  Our semaphores and timeout counters, where size depends on 
 *      MAX_SCSI_HOSTS here.
 */

Scsi_Host_Name * scsi_host_no_list = NULL;
struct Scsi_Host * scsi_hostlist = NULL;
struct Scsi_Device_Template * scsi_devicelist = NULL;

int max_scsi_hosts = 0;
int next_scsi_host = 0;

void
scsi_unregister(struct Scsi_Host * sh){
    struct Scsi_Host * shpnt;
    Scsi_Host_Name *shn;
        
    if(scsi_hostlist == sh)
	scsi_hostlist = sh->next;
    else {
	shpnt = scsi_hostlist;
	while(shpnt->next != sh) shpnt = shpnt->next;
	shpnt->next = shpnt->next->next;
    }

    /*
     * We have to unregister the host from the scsi_host_no_list as well.
     * Decide by the host_no not by the name because most host drivers are
     * able to handle more than one adapters from the same kind (or family).
     */
    for ( shn=scsi_host_no_list; shn && (sh->host_no != shn->host_no);
	  shn=shn->next);
    if (shn) shn->host_registered = 0;
    /* else {} : This should not happen, we should panic here... */
    
    /* If we are removing the last host registered, it is safe to reuse
     * its host number (this avoids "holes" at boot time) (DB) 
     * It is also safe to reuse those of numbers directly below which have
     * been released earlier (to avoid some holes in numbering).
     */
    if(sh->host_no == max_scsi_hosts - 1) {
	while(--max_scsi_hosts >= next_scsi_host) {
	    shpnt = scsi_hostlist;
	    while(shpnt && shpnt->host_no != max_scsi_hosts - 1)
		shpnt = shpnt->next;
	    if(shpnt)
		break;
	}
    }
    next_scsi_host--;
    kfree((char *) sh);
}

/* We call this when we come across a new host adapter. We only do this
 * once we are 100% sure that we want to use this host adapter -  it is a
 * pain to reverse this, so we try to avoid it 
 */

struct Scsi_Host * scsi_register(Scsi_Host_Template * tpnt, int j){
    struct Scsi_Host * retval, *shpnt;
    Scsi_Host_Name *shn, *shn2;
    int new = 1;
    retval = (struct Scsi_Host *)kmalloc(sizeof(struct Scsi_Host) + j,
					 (tpnt->unchecked_isa_dma && j ? GFP_DMA : 0) | GFP_ATOMIC);
    memset(retval, 0, sizeof(struct Scsi_Host) + j);

    /* trying to find a reserved entry (host_no) */
    for (shn = scsi_host_no_list;shn;shn = shn->next)
	if (!(shn->host_registered) && shn->loaded_as_module && tpnt->proc_dir &&
		tpnt->proc_dir->name && !strncmp(tpnt->proc_dir->name, shn->name, strlen(tpnt->proc_dir->name))) {
	    new = 0;
	    retval->host_no = shn->host_no;
	    shn->host_registered = 1;
	    shn->loaded_as_module = scsi_loadable_module_flag;
	    break;
	}
    atomic_set(&retval->host_active,0);
    retval->host_busy = 0;
    retval->host_failed = 0;
    if(j > 0xffff) panic("Too many extra bytes requested\n");
    retval->extra_bytes = j;
    retval->loaded_as_module = scsi_loadable_module_flag;
    if (new) {
	int len = 0;
	shn = (Scsi_Host_Name *) kmalloc(sizeof(Scsi_Host_Name), GFP_ATOMIC);
	if (tpnt->proc_dir)
	    len = strlen(tpnt->proc_dir->name);
	shn->name = kmalloc(len+1, GFP_ATOMIC);
	if (tpnt->proc_dir)
	    strncpy(shn->name, tpnt->proc_dir->name, len);
	shn->name[len] = 0;
	shn->host_no = max_scsi_hosts++;
	shn->host_registered = 1;
	shn->loaded_as_module = scsi_loadable_module_flag;
	shn->next = NULL;
	if (scsi_host_no_list) {
	    for (shn2 = scsi_host_no_list;shn2->next;shn2 = shn2->next)
		;
	    shn2->next = shn;
	}
	else
	    scsi_host_no_list = shn;
	retval->host_no = shn->host_no;
    }
    next_scsi_host++;
    retval->host_queue = NULL;
    init_waitqueue_head(&retval->host_wait);
    retval->resetting = 0;
    retval->last_reset = 0;
    retval->irq = 0;
    retval->dma_channel = 0xff;

    /* These three are default values which can be overridden */
    retval->max_channel = 0; 
    retval->max_id = 8;      
    retval->max_lun = 8;

    /*
     * All drivers right now should be able to handle 12 byte commands.
     * Every so often there are requests for 16 byte commands, but individual
     * low-level drivers need to certify that they actually do something
     * sensible with such commands.
     */
    retval->max_cmd_len = 12;

    retval->unique_id = 0;
    retval->io_port = 0;
    retval->hostt = tpnt;
    retval->next = NULL;
    retval->in_recovery = 0;
    retval->ehandler = NULL;    /* Initial value until the thing starts up. */
    retval->eh_notify   = NULL;    /* Who we notify when we exit. */


    retval->host_blocked = FALSE;
    retval->host_self_blocked = FALSE;

#ifdef DEBUG
    printk("Register %x %x: %d\n", (int)retval, (int)retval->hostt, j);
#endif

    /* The next six are the default values which can be overridden
     * if need be */
    retval->this_id = tpnt->this_id;
    retval->can_queue = tpnt->can_queue;
    retval->sg_tablesize = tpnt->sg_tablesize;
    retval->cmd_per_lun = tpnt->cmd_per_lun;
    retval->unchecked_isa_dma = tpnt->unchecked_isa_dma;
    retval->use_clustering = tpnt->use_clustering;   

    retval->select_queue_depths = tpnt->select_queue_depths;

    if(!scsi_hostlist)
	scsi_hostlist = retval;
    else
    {
	shpnt = scsi_hostlist;
	while(shpnt->next) shpnt = shpnt->next;
	shpnt->next = retval;
    }
    
    return retval;
}

int
scsi_register_device(struct Scsi_Device_Template * sdpnt)
{
    if(sdpnt->next) panic("Device already registered");
    sdpnt->next = scsi_devicelist;
    scsi_devicelist = sdpnt;
    return 0;
}

/*
 * Why is this a separate function?  Because the kernel_thread code
 * effectively does a fork, and there is a builtin exit() call when
 * the child returns.   The difficulty is that scsi_init() is
 * marked __init, which means the memory is unmapped after bootup
 * is complete, which means that the thread's exit() call gets wiped.
 *
 * The lesson is to *NEVER*, *NEVER* call kernel_thread() from an
 * __init function, if that function could ever return.
 */
static void launch_error_handler_thread(struct Scsi_Host * shpnt)
{
            DECLARE_MUTEX_LOCKED(sem);
            
            shpnt->eh_notify = &sem;

            kernel_thread((int (*)(void *))scsi_error_handler, 
                          (void *) shpnt, 0);

            /*
             * Now wait for the kernel error thread to initialize itself
             * as it might be needed when we scan the bus.
             */
            down (&sem);
            shpnt->eh_notify = NULL;
}

unsigned int __init scsi_init(void)
{
    static int called = 0;
    int i, pcount;
    unsigned long flags;
    Scsi_Host_Template * tpnt;
    struct Scsi_Host * shpnt;
    const char * name;
    
    if(called) return 0;
    
    called = 1;
    for (tpnt = &builtin_scsi_hosts[0], i = 0; i < MAX_SCSI_HOSTS; ++i, tpnt++)
    {
	/*
	 * Initialize our semaphores.  -1 is interpreted to mean
	 * "inactive" - where as 0 will indicate a time out condition.
	 */
	
	pcount = next_scsi_host;
	if (tpnt->detect) {

           /* The detect routine must carefully spinunlock/spinlock if 
              it enables interrupts, since all interrupt handlers do 
              spinlock as well.
              All lame drivers are going to fail due to the following 
              spinlock. For the time beeing let's use it only for drivers 
              using the new scsi code. NOTE: the detect routine could
              redefine the value tpnt->use_new_eh_code. (DB, 13 May 1998) */

           if (tpnt->use_new_eh_code) {
              spin_lock_irqsave(&io_request_lock, flags);
              tpnt->present = tpnt->detect(tpnt);
              spin_unlock_irqrestore(&io_request_lock, flags);
              }
           else
              tpnt->present = tpnt->detect(tpnt);

           }

	if (tpnt->detect && tpnt->present)
	{
	    /* The only time this should come up is when people use
	     * some kind of patched driver of some kind or another. */
	    if(pcount == next_scsi_host) {
		if(tpnt->present > 1)
		    panic("Failure to register low-level scsi driver");
		/* The low-level driver failed to register a driver.  We
		 * can do this now. */
		scsi_register(tpnt,0);
	    }
	    tpnt->next = scsi_hosts;
	    scsi_hosts = tpnt;

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

        /*
         * Now start the error recovery thread for the host.
         */
        if( shpnt->hostt->use_new_eh_code )
        {
            launch_error_handler_thread(shpnt);
        }
    }
    
    printk ("scsi : %d host%s.\n", next_scsi_host,
	    (next_scsi_host == 1) ? "" : "s");
    
    /* Now attach the high level drivers */
#ifdef CONFIG_BLK_DEV_SD
    scsi_register_device(&sd_template);
#endif
#ifdef CONFIG_BLK_DEV_SR
    scsi_register_device(&sr_template);
#endif
#ifdef CONFIG_CHR_DEV_ST
    scsi_register_device(&st_template);
#endif
#ifdef CONFIG_CHR_DEV_SG
    scsi_register_device(&sg_template);
#endif
    
#if 0      
    max_scsi_hosts = next_scsi_host;
#endif
    return 0;
}

/*
 * 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: 4
 * c-brace-imaginary-offset: 0
 * c-brace-offset: -4
 * c-argdecl-indent: 4
 * c-label-offset: -4
 * c-continued-statement-offset: 4
 * c-continued-brace-offset: 0
 * indent-tabs-mode: nil
 * tab-width: 8
 * End:
 */