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