patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / scsi / hosts.c
1 /*
2  *  hosts.c Copyright (C) 1992 Drew Eckhardt
3  *          Copyright (C) 1993, 1994, 1995 Eric Youngdale
4  *          Copyright (C) 2002-2003 Christoph Hellwig
5  *
6  *  mid to lowlevel SCSI driver interface
7  *      Initial versions: Drew Eckhardt
8  *      Subsequent revisions: Eric Youngdale
9  *
10  *  <drew@colorado.edu>
11  *
12  *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
13  *  Added QLOGIC QLA1280 SCSI controller kernel host support. 
14  *     August 4, 1999 Fred Lewis, Intel DuPont
15  *
16  *  Updated to reflect the new initialization scheme for the higher 
17  *  level of scsi drivers (sd/sr/st)
18  *  September 17, 2000 Torben Mathiasen <tmm@image.dk>
19  *
20  *  Restructured scsi_host lists and associated functions.
21  *  September 04, 2002 Mike Anderson (andmike@us.ibm.com)
22  */
23
24 #include <linux/module.h>
25 #include <linux/blkdev.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
28 #include <linux/mm.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/completion.h>
32 #include <linux/unistd.h>
33
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_transport.h>
36 #include "scsi.h"
37
38 #include "scsi_priv.h"
39 #include "scsi_logging.h"
40
41
42 static int scsi_host_next_hn;           /* host_no for next new host */
43
44
45 static void scsi_host_cls_release(struct class_device *class_dev)
46 {
47         put_device(&class_to_shost(class_dev)->shost_gendev);
48 }
49
50 static struct class shost_class = {
51         .name           = "scsi_host",
52         .release        = scsi_host_cls_release,
53 };
54
55 static int scsi_device_cancel_cb(struct device *dev, void *data)
56 {
57         return scsi_device_cancel(to_scsi_device(dev), *(int *)data);
58 }
59
60 /**
61  * scsi_host_cancel - cancel outstanding IO to this host
62  * @shost:      pointer to struct Scsi_Host
63  * recovery:    recovery requested to run.
64  **/
65 void scsi_host_cancel(struct Scsi_Host *shost, int recovery)
66 {
67         set_bit(SHOST_CANCEL, &shost->shost_state);
68         device_for_each_child(&shost->shost_gendev, &recovery,
69                               scsi_device_cancel_cb);
70         wait_event(shost->host_wait, (!test_bit(SHOST_RECOVERY,
71                                                 &shost->shost_state)));
72 }
73
74 /**
75  * scsi_remove_host - remove a scsi host
76  * @shost:      a pointer to a scsi host to remove
77  **/
78 void scsi_remove_host(struct Scsi_Host *shost)
79 {
80         scsi_host_cancel(shost, 0);
81         scsi_proc_host_rm(shost);
82         scsi_forget_host(shost);
83
84         set_bit(SHOST_DEL, &shost->shost_state);
85
86         class_device_unregister(&shost->shost_classdev);
87         device_del(&shost->shost_gendev);
88 }
89
90 /**
91  * scsi_add_host - add a scsi host
92  * @shost:      scsi host pointer to add
93  * @dev:        a struct device of type scsi class
94  *
95  * Return value: 
96  *      0 on success / != 0 for error
97  **/
98 int scsi_add_host(struct Scsi_Host *shost, struct device *dev)
99 {
100         struct scsi_host_template *sht = shost->hostt;
101         int error;
102
103         printk(KERN_INFO "scsi%d : %s\n", shost->host_no,
104                         sht->info ? sht->info(shost) : sht->name);
105
106         if (!shost->can_queue) {
107                 printk(KERN_ERR "%s: can_queue = 0 no longer supported\n",
108                                 sht->name);
109                 return -EINVAL;
110         }
111
112         if (!shost->shost_gendev.parent)
113                 shost->shost_gendev.parent = dev ? dev : &platform_bus;
114
115         error = device_add(&shost->shost_gendev);
116         if (error)
117                 goto out;
118
119         set_bit(SHOST_ADD, &shost->shost_state);
120         get_device(shost->shost_gendev.parent);
121
122         error = class_device_add(&shost->shost_classdev);
123         if (error)
124                 goto out_del_gendev;
125
126         get_device(&shost->shost_gendev);
127
128         error = scsi_sysfs_add_host(shost);
129         if (error)
130                 goto out_del_classdev;
131
132         scsi_proc_host_add(shost);
133         return error;
134
135  out_del_classdev:
136         class_device_del(&shost->shost_classdev);
137  out_del_gendev:
138         device_del(&shost->shost_gendev);
139  out:
140         return error;
141 }
142
143 static void scsi_host_dev_release(struct device *dev)
144 {
145         struct Scsi_Host *shost = dev_to_shost(dev);
146         struct device *parent = dev->parent;
147
148         if (shost->ehandler) {
149                 DECLARE_COMPLETION(sem);
150                 shost->eh_notify = &sem;
151                 shost->eh_kill = 1;
152                 up(shost->eh_wait);
153                 wait_for_completion(&sem);
154                 shost->eh_notify = NULL;
155         }
156
157         scsi_proc_hostdir_rm(shost->hostt);
158         scsi_destroy_command_freelist(shost);
159
160         /*
161          * Some drivers (eg aha1542) do scsi_register()/scsi_unregister()
162          * during probing without performing a scsi_set_device() in between.
163          * In this case dev->parent is NULL.
164          */
165         if (parent)
166                 put_device(parent);
167         kfree(shost);
168 }
169
170 /**
171  * scsi_host_alloc - register a scsi host adapter instance.
172  * @sht:        pointer to scsi host template
173  * @privsize:   extra bytes to allocate for driver
174  *
175  * Note:
176  *      Allocate a new Scsi_Host and perform basic initialization.
177  *      The host is not published to the scsi midlayer until scsi_add_host
178  *      is called.
179  *
180  * Return value:
181  *      Pointer to a new Scsi_Host
182  **/
183 struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
184 {
185         struct Scsi_Host *shost;
186         int gfp_mask = GFP_KERNEL, rval;
187         DECLARE_COMPLETION(complete);
188
189         if (sht->unchecked_isa_dma && privsize)
190                 gfp_mask |= __GFP_DMA;
191
192         /* Check to see if this host has any error handling facilities */
193         if (!sht->eh_strategy_handler && !sht->eh_abort_handler &&
194             !sht->eh_device_reset_handler && !sht->eh_bus_reset_handler &&
195             !sht->eh_host_reset_handler) {
196                 printk(KERN_ERR "ERROR: SCSI host `%s' has no error handling\n"
197                                 "ERROR: This is not a safe way to run your "
198                                         "SCSI host\n"
199                                 "ERROR: The error handling must be added to "
200                                 "this driver\n", sht->proc_name);
201                 dump_stack();
202         }
203
204         shost = kmalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
205         if (!shost)
206                 return NULL;
207         memset(shost, 0, sizeof(struct Scsi_Host) + privsize);
208
209         spin_lock_init(&shost->default_lock);
210         scsi_assign_lock(shost, &shost->default_lock);
211         INIT_LIST_HEAD(&shost->__devices);
212         INIT_LIST_HEAD(&shost->eh_cmd_q);
213         INIT_LIST_HEAD(&shost->starved_list);
214         init_waitqueue_head(&shost->host_wait);
215
216         init_MUTEX(&shost->scan_mutex);
217
218         shost->host_no = scsi_host_next_hn++; /* XXX(hch): still racy */
219         shost->dma_channel = 0xff;
220
221         /* These three are default values which can be overridden */
222         shost->max_channel = 0;
223         shost->max_id = 8;
224         shost->max_lun = 8;
225
226         /* Give each shost a default transportt if the driver
227          * doesn't yet support Transport Attributes */
228         if (!shost->transportt) 
229                 shost->transportt = &blank_transport_template;
230
231         /*
232          * All drivers right now should be able to handle 12 byte
233          * commands.  Every so often there are requests for 16 byte
234          * commands, but individual low-level drivers need to certify that
235          * they actually do something sensible with such commands.
236          */
237         shost->max_cmd_len = 12;
238         shost->hostt = sht;
239         shost->this_id = sht->this_id;
240         shost->can_queue = sht->can_queue;
241         shost->sg_tablesize = sht->sg_tablesize;
242         shost->cmd_per_lun = sht->cmd_per_lun;
243         shost->unchecked_isa_dma = sht->unchecked_isa_dma;
244         shost->use_clustering = sht->use_clustering;
245
246         if (sht->max_host_blocked)
247                 shost->max_host_blocked = sht->max_host_blocked;
248         else
249                 shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
250
251         /*
252          * If the driver imposes no hard sector transfer limit, start at
253          * machine infinity initially.
254          */
255         if (sht->max_sectors)
256                 shost->max_sectors = sht->max_sectors;
257         else
258                 shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
259
260         /*
261          * assume a 4GB boundary, if not set
262          */
263         if (sht->dma_boundary)
264                 shost->dma_boundary = sht->dma_boundary;
265         else
266                 shost->dma_boundary = 0xffffffff;
267
268         rval = scsi_setup_command_freelist(shost);
269         if (rval)
270                 goto fail_kfree;
271
272         device_initialize(&shost->shost_gendev);
273         snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",
274                 shost->host_no);
275         shost->shost_gendev.release = scsi_host_dev_release;
276
277         class_device_initialize(&shost->shost_classdev);
278         shost->shost_classdev.dev = &shost->shost_gendev;
279         shost->shost_classdev.class = &shost_class;
280         snprintf(shost->shost_classdev.class_id, BUS_ID_SIZE, "host%d",
281                   shost->host_no);
282
283         shost->eh_notify = &complete;
284         rval = kernel_thread(scsi_error_handler, shost, 0);
285         if (rval < 0)
286                 goto fail_destroy_freelist;
287         wait_for_completion(&complete);
288         shost->eh_notify = NULL;
289         scsi_proc_hostdir_add(shost->hostt);
290         return shost;
291
292  fail_destroy_freelist:
293         scsi_destroy_command_freelist(shost);
294  fail_kfree:
295         kfree(shost);
296         return NULL;
297 }
298
299 struct Scsi_Host *scsi_register(struct scsi_host_template *sht, int privsize)
300 {
301         struct Scsi_Host *shost = scsi_host_alloc(sht, privsize);
302
303         if (!sht->detect) {
304                 printk(KERN_WARNING "scsi_register() called on new-style "
305                                     "template for driver %s\n", sht->name);
306                 dump_stack();
307         }
308
309         if (shost)
310                 list_add_tail(&shost->sht_legacy_list, &sht->legacy_hosts);
311         return shost;
312 }
313
314 void scsi_unregister(struct Scsi_Host *shost)
315 {
316         list_del(&shost->sht_legacy_list);
317         scsi_host_put(shost);
318 }
319
320 /**
321  * scsi_host_lookup - get a reference to a Scsi_Host by host no
322  *
323  * @hostnum:    host number to locate
324  *
325  * Return value:
326  *      A pointer to located Scsi_Host or NULL.
327  **/
328 struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
329 {
330         struct class *class = &shost_class;
331         struct class_device *cdev;
332         struct Scsi_Host *shost = ERR_PTR(-ENXIO), *p;
333
334         down_read(&class->subsys.rwsem);
335         list_for_each_entry(cdev, &class->children, node) {
336                 p = class_to_shost(cdev);
337                 if (p->host_no == hostnum) {
338                         shost = scsi_host_get(p);
339                         break;
340                 }
341         }
342         up_read(&class->subsys.rwsem);
343
344         return shost;
345 }
346
347 /**
348  * scsi_host_get - inc a Scsi_Host ref count
349  * @shost:      Pointer to Scsi_Host to inc.
350  **/
351 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
352 {
353         if (test_bit(SHOST_DEL, &shost->shost_state) ||
354                 !get_device(&shost->shost_gendev))
355                 return NULL;
356         return shost;
357 }
358
359 /**
360  * scsi_host_put - dec a Scsi_Host ref count
361  * @shost:      Pointer to Scsi_Host to dec.
362  **/
363 void scsi_host_put(struct Scsi_Host *shost)
364 {
365         put_device(&shost->shost_gendev);
366 }
367
368 int scsi_init_hosts(void)
369 {
370         return class_register(&shost_class);
371 }
372
373 void scsi_exit_hosts(void)
374 {
375         class_unregister(&shost_class);
376 }