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