This commit was manufactured by cvs2svn to create tag
[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 /**
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                 class_device_unregister(&shost->transport_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 = -EINVAL;
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                 goto out;
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         if (shost->transportt->host_size &&
129             (shost->shost_data = kmalloc(shost->transportt->host_size,
130                                          GFP_KERNEL)) == NULL)
131                 goto out_del_classdev;
132
133         if (shost->transportt->host_setup)
134                 shost->transportt->host_setup(shost);
135
136         error = scsi_sysfs_add_host(shost);
137         if (error)
138                 goto out_destroy_host;
139
140         scsi_proc_host_add(shost);
141         return error;
142
143  out_destroy_host:
144         if (shost->transportt->host_destroy)
145                 shost->transportt->host_destroy(shost);
146  out_del_classdev:
147         class_device_del(&shost->shost_classdev);
148  out_del_gendev:
149         device_del(&shost->shost_gendev);
150  out:
151         return error;
152 }
153
154 static void scsi_host_dev_release(struct device *dev)
155 {
156         struct Scsi_Host *shost = dev_to_shost(dev);
157         struct device *parent = dev->parent;
158
159         if (shost->ehandler) {
160                 DECLARE_COMPLETION(sem);
161                 shost->eh_notify = &sem;
162                 shost->eh_kill = 1;
163                 up(shost->eh_wait);
164                 wait_for_completion(&sem);
165                 shost->eh_notify = NULL;
166         }
167
168         scsi_proc_hostdir_rm(shost->hostt);
169         scsi_destroy_command_freelist(shost);
170         kfree(shost->shost_data);
171
172         /*
173          * Some drivers (eg aha1542) do scsi_register()/scsi_unregister()
174          * during probing without performing a scsi_set_device() in between.
175          * In this case dev->parent is NULL.
176          */
177         if (parent)
178                 put_device(parent);
179         kfree(shost);
180 }
181
182 /**
183  * scsi_host_alloc - register a scsi host adapter instance.
184  * @sht:        pointer to scsi host template
185  * @privsize:   extra bytes to allocate for driver
186  *
187  * Note:
188  *      Allocate a new Scsi_Host and perform basic initialization.
189  *      The host is not published to the scsi midlayer until scsi_add_host
190  *      is called.
191  *
192  * Return value:
193  *      Pointer to a new Scsi_Host
194  **/
195 struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
196 {
197         struct Scsi_Host *shost;
198         int gfp_mask = GFP_KERNEL, rval;
199         DECLARE_COMPLETION(complete);
200
201         if (sht->unchecked_isa_dma && privsize)
202                 gfp_mask |= __GFP_DMA;
203
204         /* Check to see if this host has any error handling facilities */
205         if (!sht->eh_strategy_handler && !sht->eh_abort_handler &&
206             !sht->eh_device_reset_handler && !sht->eh_bus_reset_handler &&
207             !sht->eh_host_reset_handler) {
208                 printk(KERN_ERR "ERROR: SCSI host `%s' has no error handling\n"
209                                 "ERROR: This is not a safe way to run your "
210                                         "SCSI host\n"
211                                 "ERROR: The error handling must be added to "
212                                 "this driver\n", sht->proc_name);
213                 dump_stack();
214         }
215
216         shost = kmalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
217         if (!shost)
218                 return NULL;
219         memset(shost, 0, sizeof(struct Scsi_Host) + privsize);
220
221         spin_lock_init(&shost->default_lock);
222         scsi_assign_lock(shost, &shost->default_lock);
223         INIT_LIST_HEAD(&shost->__devices);
224         INIT_LIST_HEAD(&shost->eh_cmd_q);
225         INIT_LIST_HEAD(&shost->starved_list);
226         init_waitqueue_head(&shost->host_wait);
227
228         init_MUTEX(&shost->scan_mutex);
229
230         shost->host_no = scsi_host_next_hn++; /* XXX(hch): still racy */
231         shost->dma_channel = 0xff;
232
233         /* These three are default values which can be overridden */
234         shost->max_channel = 0;
235         shost->max_id = 8;
236         shost->max_lun = 8;
237
238         /* Give each shost a default transportt */
239         shost->transportt = &blank_transport_template;
240
241         /*
242          * All drivers right now should be able to handle 12 byte
243          * commands.  Every so often there are requests for 16 byte
244          * commands, but individual low-level drivers need to certify that
245          * they actually do something sensible with such commands.
246          */
247         shost->max_cmd_len = 12;
248         shost->hostt = sht;
249         shost->this_id = sht->this_id;
250         shost->can_queue = sht->can_queue;
251         shost->sg_tablesize = sht->sg_tablesize;
252         shost->cmd_per_lun = sht->cmd_per_lun;
253         shost->unchecked_isa_dma = sht->unchecked_isa_dma;
254         shost->use_clustering = sht->use_clustering;
255
256         if (sht->max_host_blocked)
257                 shost->max_host_blocked = sht->max_host_blocked;
258         else
259                 shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
260
261         /*
262          * If the driver imposes no hard sector transfer limit, start at
263          * machine infinity initially.
264          */
265         if (sht->max_sectors)
266                 shost->max_sectors = sht->max_sectors;
267         else
268                 shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
269
270         /*
271          * assume a 4GB boundary, if not set
272          */
273         if (sht->dma_boundary)
274                 shost->dma_boundary = sht->dma_boundary;
275         else
276                 shost->dma_boundary = 0xffffffff;
277
278         rval = scsi_setup_command_freelist(shost);
279         if (rval)
280                 goto fail_kfree;
281
282         device_initialize(&shost->shost_gendev);
283         snprintf(shost->shost_gendev.bus_id, BUS_ID_SIZE, "host%d",
284                 shost->host_no);
285         shost->shost_gendev.release = scsi_host_dev_release;
286
287         class_device_initialize(&shost->shost_classdev);
288         shost->shost_classdev.dev = &shost->shost_gendev;
289         shost->shost_classdev.class = &shost_class;
290         snprintf(shost->shost_classdev.class_id, BUS_ID_SIZE, "host%d",
291                   shost->host_no);
292
293         shost->eh_notify = &complete;
294         rval = kernel_thread(scsi_error_handler, shost, 0);
295         if (rval < 0)
296                 goto fail_destroy_freelist;
297         wait_for_completion(&complete);
298         shost->eh_notify = NULL;
299
300         scsi_proc_hostdir_add(shost->hostt);
301         return shost;
302
303  fail_destroy_freelist:
304         scsi_destroy_command_freelist(shost);
305  fail_kfree:
306         kfree(shost);
307         return NULL;
308 }
309
310 struct Scsi_Host *scsi_register(struct scsi_host_template *sht, int privsize)
311 {
312         struct Scsi_Host *shost = scsi_host_alloc(sht, privsize);
313
314         if (!sht->detect) {
315                 printk(KERN_WARNING "scsi_register() called on new-style "
316                                     "template for driver %s\n", sht->name);
317                 dump_stack();
318         }
319
320         if (shost)
321                 list_add_tail(&shost->sht_legacy_list, &sht->legacy_hosts);
322         return shost;
323 }
324
325 void scsi_unregister(struct Scsi_Host *shost)
326 {
327         list_del(&shost->sht_legacy_list);
328         scsi_host_put(shost);
329 }
330
331 /**
332  * scsi_host_lookup - get a reference to a Scsi_Host by host no
333  *
334  * @hostnum:    host number to locate
335  *
336  * Return value:
337  *      A pointer to located Scsi_Host or NULL.
338  **/
339 struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
340 {
341         struct class *class = &shost_class;
342         struct class_device *cdev;
343         struct Scsi_Host *shost = ERR_PTR(-ENXIO), *p;
344
345         down_read(&class->subsys.rwsem);
346         list_for_each_entry(cdev, &class->children, node) {
347                 p = class_to_shost(cdev);
348                 if (p->host_no == hostnum) {
349                         shost = scsi_host_get(p);
350                         break;
351                 }
352         }
353         up_read(&class->subsys.rwsem);
354
355         return shost;
356 }
357
358 /**
359  * scsi_host_get - inc a Scsi_Host ref count
360  * @shost:      Pointer to Scsi_Host to inc.
361  **/
362 struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
363 {
364         if (test_bit(SHOST_DEL, &shost->shost_state) ||
365                 !get_device(&shost->shost_gendev))
366                 return NULL;
367         return shost;
368 }
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
379 int scsi_init_hosts(void)
380 {
381         return class_register(&shost_class);
382 }
383
384 void scsi_exit_hosts(void)
385 {
386         class_unregister(&shost_class);
387 }