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