File: /usr/src/linux/drivers/scsi/hosts.c

1     /*
2      *  hosts.c Copyright (C) 1992 Drew Eckhardt
3      *          Copyright (C) 1993, 1994, 1995 Eric Youngdale
4      *
5      *  mid to lowlevel SCSI driver interface
6      *      Initial versions: Drew Eckhardt
7      *      Subsequent revisions: Eric Youngdale
8      *
9      *  <drew@colorado.edu>
10      *
11      *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
12      *  Added QLOGIC QLA1280 SCSI controller kernel host support. 
13      *     August 4, 1999 Fred Lewis, Intel DuPont
14      *
15      *  Updated to reflect the new initialization scheme for the higher 
16      *  level of scsi drivers (sd/sr/st)
17      *  September 17, 2000 Torben Mathiasen <tmm@image.dk>
18      */
19     
20     
21     /*
22      *  This file contains the medium level SCSI
23      *  host interface initialization, as well as the scsi_hosts array of SCSI
24      *  hosts currently present in the system.
25      */
26     
27     #define __NO_VERSION__
28     #include <linux/module.h>
29     #include <linux/blk.h>
30     #include <linux/kernel.h>
31     #include <linux/string.h>
32     #include <linux/mm.h>
33     #include <linux/proc_fs.h>
34     #include <linux/init.h>
35     
36     #define __KERNEL_SYSCALLS__
37     
38     #include <linux/unistd.h>
39     
40     #include "scsi.h"
41     #include "hosts.h"
42     
43     /*
44     static const char RCSid[] = "$Header: /vger/u4/cvs/linux/drivers/scsi/hosts.c,v 1.20 1996/12/12 19:18:32 davem Exp $";
45     */
46     
47     /*
48      *  The scsi host entries should be in the order you wish the
49      *  cards to be detected.  A driver may appear more than once IFF
50      *  it can deal with being detected (and therefore initialized)
51      *  with more than one simultaneous host number, can handle being
52      *  reentrant, etc.
53      *
54      *  They may appear in any order, as each SCSI host is told which host 
55      *  number it is during detection.
56      */
57     
58     /* This is a placeholder for controllers that are not configured into
59      * the system - we do this to ensure that the controller numbering is
60      * always consistent, no matter how the kernel is configured. */
61     
62     #define NO_CONTROLLER {NULL, NULL, NULL, NULL, NULL, NULL, NULL, \
63     			   NULL, NULL, 0, 0, 0, 0, 0, 0}
64     
65     /*
66      *  When figure is run, we don't want to link to any object code.  Since
67      *  the macro for each host will contain function pointers, we cannot
68      *  use it and instead must use a "blank" that does no such
69      *  idiocy.
70      */
71     
72     Scsi_Host_Template * scsi_hosts;
73     
74     
75     /*
76      *  Our semaphores and timeout counters, where size depends on 
77      *      MAX_SCSI_HOSTS here.
78      */
79     
80     Scsi_Host_Name * scsi_host_no_list;
81     struct Scsi_Host * scsi_hostlist;
82     struct Scsi_Device_Template * scsi_devicelist;
83     
84     int max_scsi_hosts;
85     int next_scsi_host;
86     
87     void
88     scsi_unregister(struct Scsi_Host * sh){
89         struct Scsi_Host * shpnt;
90         Scsi_Host_Name *shn;
91             
92         if(scsi_hostlist == sh)
93     	scsi_hostlist = sh->next;
94         else {
95     	shpnt = scsi_hostlist;
96     	while(shpnt->next != sh) shpnt = shpnt->next;
97     	shpnt->next = shpnt->next->next;
98         }
99     
100         /*
101          * We have to unregister the host from the scsi_host_no_list as well.
102          * Decide by the host_no not by the name because most host drivers are
103          * able to handle more than one adapters from the same kind (or family).
104          */
105         for ( shn=scsi_host_no_list; shn && (sh->host_no != shn->host_no);
106     	  shn=shn->next);
107         if (shn) shn->host_registered = 0;
108         /* else {} : This should not happen, we should panic here... */
109         
110         /* If we are removing the last host registered, it is safe to reuse
111          * its host number (this avoids "holes" at boot time) (DB) 
112          * It is also safe to reuse those of numbers directly below which have
113          * been released earlier (to avoid some holes in numbering).
114          */
115         if(sh->host_no == max_scsi_hosts - 1) {
116     	while(--max_scsi_hosts >= next_scsi_host) {
117     	    shpnt = scsi_hostlist;
118     	    while(shpnt && shpnt->host_no != max_scsi_hosts - 1)
119     		shpnt = shpnt->next;
120     	    if(shpnt)
121     		break;
122     	}
123         }
124         next_scsi_host--;
125         kfree((char *) sh);
126     }
127     
128     /* We call this when we come across a new host adapter. We only do this
129      * once we are 100% sure that we want to use this host adapter -  it is a
130      * pain to reverse this, so we try to avoid it 
131      */
132     
133     struct Scsi_Host * scsi_register(Scsi_Host_Template * tpnt, int j){
134         struct Scsi_Host * retval, *shpnt, *o_shp;
135         Scsi_Host_Name *shn, *shn2;
136         int flag_new = 1;
137         const char * hname;
138         size_t hname_len;
139         retval = (struct Scsi_Host *)kmalloc(sizeof(struct Scsi_Host) + j,
140     					 (tpnt->unchecked_isa_dma && j ? 
141     					  GFP_DMA : 0) | GFP_ATOMIC);
142         if(retval == NULL)
143         {
144             printk("scsi: out of memory in scsi_register.\n");
145         	return NULL;
146         }
147         	
148         memset(retval, 0, sizeof(struct Scsi_Host) + j);
149     
150         /* trying to find a reserved entry (host_no) */
151         hname = (tpnt->proc_name) ?  tpnt->proc_name : "";
152         hname_len = strlen(hname);
153         for (shn = scsi_host_no_list;shn;shn = shn->next) {
154     	if (!(shn->host_registered) && 
155     	    (hname_len > 0) && (0 == strncmp(hname, shn->name, hname_len))) {
156     	    flag_new = 0;
157     	    retval->host_no = shn->host_no;
158     	    shn->host_registered = 1;
159     	    shn->loaded_as_module = 1;
160     	    break;
161     	}
162         }
163         atomic_set(&retval->host_active,0);
164         retval->host_busy = 0;
165         retval->host_failed = 0;
166         if(j > 0xffff) panic("Too many extra bytes requested\n");
167         retval->extra_bytes = j;
168         retval->loaded_as_module = 1;
169         if (flag_new) {
170     	shn = (Scsi_Host_Name *) kmalloc(sizeof(Scsi_Host_Name), GFP_ATOMIC);
171             if (!shn) {
172                     kfree(retval);
173                     printk(KERN_ERR "scsi: out of memory(2) in scsi_register.\n");
174                     return NULL;
175             }
176     	shn->name = kmalloc(hname_len + 1, GFP_ATOMIC);
177     	if (hname_len > 0)
178     	    strncpy(shn->name, hname, hname_len);
179     	shn->name[hname_len] = 0;
180     	shn->host_no = max_scsi_hosts++;
181     	shn->host_registered = 1;
182     	shn->loaded_as_module = 1;
183     	shn->next = NULL;
184     	if (scsi_host_no_list) {
185     	    for (shn2 = scsi_host_no_list;shn2->next;shn2 = shn2->next)
186     		;
187     	    shn2->next = shn;
188     	}
189     	else
190     	    scsi_host_no_list = shn;
191     	retval->host_no = shn->host_no;
192         }
193         next_scsi_host++;
194         retval->host_queue = NULL;
195         init_waitqueue_head(&retval->host_wait);
196         retval->resetting = 0;
197         retval->last_reset = 0;
198         retval->irq = 0;
199         retval->dma_channel = 0xff;
200     
201         /* These three are default values which can be overridden */
202         retval->max_channel = 0; 
203         retval->max_id = 8;      
204         retval->max_lun = 8;
205     
206         /*
207          * All drivers right now should be able to handle 12 byte commands.
208          * Every so often there are requests for 16 byte commands, but individual
209          * low-level drivers need to certify that they actually do something
210          * sensible with such commands.
211          */
212         retval->max_cmd_len = 12;
213     
214         retval->unique_id = 0;
215         retval->io_port = 0;
216         retval->hostt = tpnt;
217         retval->next = NULL;
218         retval->in_recovery = 0;
219         retval->ehandler = NULL;    /* Initial value until the thing starts up. */
220         retval->eh_notify   = NULL;    /* Who we notify when we exit. */
221     
222     
223         retval->host_blocked = FALSE;
224         retval->host_self_blocked = FALSE;
225     
226     #ifdef DEBUG
227         printk("Register %x %x: %d\n", (int)retval, (int)retval->hostt, j);
228     #endif
229     
230         /* The next six are the default values which can be overridden
231          * if need be */
232         retval->this_id = tpnt->this_id;
233         retval->can_queue = tpnt->can_queue;
234         retval->sg_tablesize = tpnt->sg_tablesize;
235         retval->cmd_per_lun = tpnt->cmd_per_lun;
236         retval->unchecked_isa_dma = tpnt->unchecked_isa_dma;
237         retval->use_clustering = tpnt->use_clustering;   
238     
239         retval->select_queue_depths = tpnt->select_queue_depths;
240         retval->max_sectors = tpnt->max_sectors;
241     
242         if(!scsi_hostlist)
243     	scsi_hostlist = retval;
244         else {
245     	shpnt = scsi_hostlist;
246     	if (retval->host_no < shpnt->host_no) {
247     	    retval->next = shpnt;
248     	    wmb(); /* want all to see these writes in this order */
249     	    scsi_hostlist = retval;
250     	}
251     	else {
252     	    for (o_shp = shpnt, shpnt = shpnt->next; shpnt; 
253     		 o_shp = shpnt, shpnt = shpnt->next) {
254     		if (retval->host_no < shpnt->host_no) {
255     		    retval->next = shpnt;
256     		    wmb();
257     		    o_shp->next = retval;
258     		    break;
259     		}
260     	    }
261     	    if (! shpnt)
262     		o_shp->next = retval;
263             }
264         }
265         
266         return retval;
267     }
268     
269     int
270     scsi_register_device(struct Scsi_Device_Template * sdpnt)
271     {
272         if(sdpnt->next) panic("Device already registered");
273         sdpnt->next = scsi_devicelist;
274         scsi_devicelist = sdpnt;
275         return 0;
276     }
277     
278     /*
279      * Overrides for Emacs so that we follow Linus's tabbing style.
280      * Emacs will notice this stuff at the end of the file and automatically
281      * adjust the settings for this buffer only.  This must remain at the end
282      * of the file.
283      * ---------------------------------------------------------------------------
284      * Local variables:
285      * c-indent-level: 4
286      * c-brace-imaginary-offset: 0
287      * c-brace-offset: -4
288      * c-argdecl-indent: 4
289      * c-label-offset: -4
290      * c-continued-statement-offset: 4
291      * c-continued-brace-offset: 0
292      * indent-tabs-mode: nil
293      * tab-width: 8
294      * End:
295      */
296