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;	/* host_no for next new host */
85 int next_scsi_host;	/* count of registered scsi hosts */
86 
87 void
scsi_unregister(struct Scsi_Host * sh)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     next_scsi_host--;
111 
112     kfree((char *) sh);
113 }
114 
115 /* We call this when we come across a new host adapter. We only do this
116  * once we are 100% sure that we want to use this host adapter -  it is a
117  * pain to reverse this, so we try to avoid it
118  */
119 extern int blk_nohighio;
scsi_register(Scsi_Host_Template * tpnt,int j)120 struct Scsi_Host * scsi_register(Scsi_Host_Template * tpnt, int j){
121     struct Scsi_Host * retval, *shpnt, *o_shp;
122     Scsi_Host_Name *shn, *shn2;
123     int flag_new = 1;
124     const char * hname;
125     size_t hname_len;
126     retval = (struct Scsi_Host *)kmalloc(sizeof(struct Scsi_Host) + j,
127 					 (tpnt->unchecked_isa_dma && j ?
128 					  GFP_DMA : 0) | GFP_ATOMIC);
129     if(retval == NULL)
130     {
131         printk("scsi: out of memory in scsi_register.\n");
132     	return NULL;
133     }
134 
135     memset(retval, 0, sizeof(struct Scsi_Host) + j);
136 
137     /* trying to find a reserved entry (host_no) */
138     hname = (tpnt->proc_name) ?  tpnt->proc_name : "";
139     hname_len = strlen(hname);
140     for (shn = scsi_host_no_list;shn;shn = shn->next) {
141 	if (!(shn->host_registered) &&
142 	    (hname_len > 0) && (0 == strncmp(hname, shn->name, hname_len))) {
143 	    flag_new = 0;
144 	    retval->host_no = shn->host_no;
145 	    shn->host_registered = 1;
146 	    shn->loaded_as_module = 1;
147 	    break;
148 	}
149     }
150     atomic_set(&retval->host_active,0);
151     retval->host_busy = 0;
152     retval->host_failed = 0;
153     if(j > 0xffff) panic("Too many extra bytes requested\n");
154     retval->extra_bytes = j;
155     retval->loaded_as_module = 1;
156     if (flag_new) {
157 	shn = (Scsi_Host_Name *) kmalloc(sizeof(Scsi_Host_Name), GFP_ATOMIC);
158         if (!shn) {
159                 kfree(retval);
160                 printk(KERN_ERR "scsi: out of memory(2) in scsi_register.\n");
161                 return NULL;
162         }
163 	shn->name = kmalloc(hname_len + 1, GFP_ATOMIC);
164 	if (hname_len > 0)
165 	    strncpy(shn->name, hname, hname_len);
166 	shn->name[hname_len] = 0;
167 	shn->host_no = max_scsi_hosts++;
168 	shn->host_registered = 1;
169 	shn->loaded_as_module = 1;
170 	shn->next = NULL;
171 	if (scsi_host_no_list) {
172 	    for (shn2 = scsi_host_no_list;shn2->next;shn2 = shn2->next)
173 		;
174 	    shn2->next = shn;
175 	}
176 	else
177 	    scsi_host_no_list = shn;
178 	retval->host_no = shn->host_no;
179     }
180     next_scsi_host++;
181     retval->host_queue = NULL;
182     init_waitqueue_head(&retval->host_wait);
183     retval->resetting = 0;
184     retval->last_reset = 0;
185     retval->irq = 0;
186     retval->dma_channel = 0xff;
187 
188     /* These three are default values which can be overridden */
189     retval->max_channel = 0;
190     retval->max_id = 8;
191     retval->max_lun = 8;
192 
193     /*
194      * All drivers right now should be able to handle 12 byte commands.
195      * Every so often there are requests for 16 byte commands, but individual
196      * low-level drivers need to certify that they actually do something
197      * sensible with such commands.
198      */
199     retval->max_cmd_len = 12;
200 
201     retval->unique_id = 0;
202     retval->io_port = 0;
203     retval->hostt = tpnt;
204     retval->next = NULL;
205     retval->in_recovery = 0;
206     retval->ehandler = NULL;    /* Initial value until the thing starts up. */
207     retval->eh_notify   = NULL;    /* Who we notify when we exit. */
208 
209 
210     retval->host_blocked = FALSE;
211     retval->host_self_blocked = FALSE;
212 
213 #ifdef DEBUG
214     printk("Register %x %x: %d\n", (int)retval, (int)retval->hostt, j);
215 #endif
216 
217     /* The next six are the default values which can be overridden
218      * if need be */
219     retval->this_id = tpnt->this_id;
220     retval->can_queue = tpnt->can_queue;
221     retval->sg_tablesize = tpnt->sg_tablesize;
222     retval->cmd_per_lun = tpnt->cmd_per_lun;
223     retval->unchecked_isa_dma = tpnt->unchecked_isa_dma;
224     retval->use_clustering = tpnt->use_clustering;
225     if (!blk_nohighio)
226 	retval->highmem_io = tpnt->highmem_io;
227 
228     retval->select_queue_depths = tpnt->select_queue_depths;
229     retval->max_sectors = tpnt->max_sectors;
230 
231     if(!scsi_hostlist)
232 	scsi_hostlist = retval;
233     else {
234 	shpnt = scsi_hostlist;
235 	if (retval->host_no < shpnt->host_no) {
236 	    retval->next = shpnt;
237 	    wmb(); /* want all to see these writes in this order */
238 	    scsi_hostlist = retval;
239 	}
240 	else {
241 	    for (o_shp = shpnt, shpnt = shpnt->next; shpnt;
242 		 o_shp = shpnt, shpnt = shpnt->next) {
243 		if (retval->host_no < shpnt->host_no) {
244 		    retval->next = shpnt;
245 		    wmb();
246 		    o_shp->next = retval;
247 		    break;
248 		}
249 	    }
250 	    if (! shpnt)
251 		o_shp->next = retval;
252         }
253     }
254 
255     return retval;
256 }
257 
258 int
scsi_register_device(struct Scsi_Device_Template * sdpnt)259 scsi_register_device(struct Scsi_Device_Template * sdpnt)
260 {
261     if(sdpnt->next) panic("Device already registered");
262     sdpnt->next = scsi_devicelist;
263     scsi_devicelist = sdpnt;
264     return 0;
265 }
266 
267 void
scsi_deregister_device(struct Scsi_Device_Template * tpnt)268 scsi_deregister_device(struct Scsi_Device_Template * tpnt)
269 {
270     struct Scsi_Device_Template *spnt;
271     struct Scsi_Device_Template *prev_spnt;
272 
273     spnt = scsi_devicelist;
274     prev_spnt = NULL;
275     while (spnt != tpnt) {
276 	prev_spnt = spnt;
277 	spnt = spnt->next;
278     }
279     if (prev_spnt == NULL)
280         scsi_devicelist = tpnt->next;
281     else
282         prev_spnt->next = spnt->next;
283 }
284 
285 /*
286  * Overrides for Emacs so that we follow Linus's tabbing style.
287  * Emacs will notice this stuff at the end of the file and automatically
288  * adjust the settings for this buffer only.  This must remain at the end
289  * of the file.
290  * ---------------------------------------------------------------------------
291  * Local variables:
292  * c-indent-level: 4
293  * c-brace-imaginary-offset: 0
294  * c-brace-offset: -4
295  * c-argdecl-indent: 4
296  * c-label-offset: -4
297  * c-continued-statement-offset: 4
298  * c-continued-brace-offset: 0
299  * indent-tabs-mode: nil
300  * tab-width: 8
301  * End:
302  */
303