2 * hosts.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
4 * Copyright (C) 2002-2003 Christoph Hellwig
6 * mid to lowlevel SCSI driver interface
7 * Initial versions: Drew Eckhardt
8 * Subsequent revisions: Eric Youngdale
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
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>
20 * Restructured scsi_host lists and associated functions.
21 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
24 #include <linux/module.h>
25 #include <linux/blkdev.h>
26 #include <linux/kernel.h>
27 #include <linux/string.h>
29 #include <linux/init.h>
30 #include <linux/completion.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_transport.h>
36 #include "scsi_priv.h"
37 #include "scsi_logging.h"
40 static int scsi_host_next_hn
; /* host_no for next new host */
43 static void scsi_host_cls_release(struct class_device
*class_dev
)
45 put_device(&class_to_shost(class_dev
)->shost_gendev
);
48 static struct class shost_class
= {
50 .release
= scsi_host_cls_release
,
53 static int scsi_device_cancel_cb(struct device
*dev
, void *data
)
55 return scsi_device_cancel(to_scsi_device(dev
), *(int *)data
);
59 * scsi_host_cancel - cancel outstanding IO to this host
60 * @shost: pointer to struct Scsi_Host
61 * recovery: recovery requested to run.
63 void scsi_host_cancel(struct Scsi_Host
*shost
, int recovery
)
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
)));
73 * scsi_remove_host - remove a scsi host
74 * @shost: a pointer to a scsi host to remove
76 void scsi_remove_host(struct Scsi_Host
*shost
)
78 scsi_forget_host(shost
);
79 scsi_host_cancel(shost
, 0);
80 scsi_proc_host_rm(shost
);
82 set_bit(SHOST_DEL
, &shost
->shost_state
);
84 class_device_unregister(&shost
->shost_classdev
);
85 device_del(&shost
->shost_gendev
);
89 * scsi_add_host - add a scsi host
90 * @shost: scsi host pointer to add
91 * @dev: a struct device of type scsi class
94 * 0 on success / != 0 for error
96 int scsi_add_host(struct Scsi_Host
*shost
, struct device
*dev
)
98 struct scsi_host_template
*sht
= shost
->hostt
;
101 printk(KERN_INFO
"scsi%d : %s\n", shost
->host_no
,
102 sht
->info
? sht
->info(shost
) : sht
->name
);
104 if (!shost
->can_queue
) {
105 printk(KERN_ERR
"%s: can_queue = 0 no longer supported\n",
110 if (!shost
->shost_gendev
.parent
)
111 shost
->shost_gendev
.parent
= dev
? dev
: &platform_bus
;
113 error
= device_add(&shost
->shost_gendev
);
117 set_bit(SHOST_ADD
, &shost
->shost_state
);
118 get_device(shost
->shost_gendev
.parent
);
120 error
= class_device_add(&shost
->shost_classdev
);
124 get_device(&shost
->shost_gendev
);
126 error
= scsi_sysfs_add_host(shost
);
128 goto out_del_classdev
;
130 scsi_proc_host_add(shost
);
134 class_device_del(&shost
->shost_classdev
);
136 device_del(&shost
->shost_gendev
);
141 static void scsi_host_dev_release(struct device
*dev
)
143 struct Scsi_Host
*shost
= dev_to_shost(dev
);
144 struct device
*parent
= dev
->parent
;
146 if (shost
->ehandler
) {
147 DECLARE_COMPLETION(sem
);
148 shost
->eh_notify
= &sem
;
151 wait_for_completion(&sem
);
152 shost
->eh_notify
= NULL
;
155 scsi_proc_hostdir_rm(shost
->hostt
);
156 scsi_destroy_command_freelist(shost
);
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.
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
174 * Allocate a new Scsi_Host and perform basic initialization.
175 * The host is not published to the scsi midlayer until scsi_add_host
179 * Pointer to a new Scsi_Host
181 struct Scsi_Host
*scsi_host_alloc(struct scsi_host_template
*sht
, int privsize
)
183 struct Scsi_Host
*shost
;
184 int gfp_mask
= GFP_KERNEL
, rval
;
185 DECLARE_COMPLETION(complete
);
187 if (sht
->unchecked_isa_dma
&& privsize
)
188 gfp_mask
|= __GFP_DMA
;
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 "
197 "ERROR: The error handling must be added to "
198 "this driver\n", sht
->proc_name
);
202 shost
= kmalloc(sizeof(struct Scsi_Host
) + privsize
, gfp_mask
);
205 memset(shost
, 0, sizeof(struct Scsi_Host
) + privsize
);
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
);
214 init_MUTEX(&shost
->scan_mutex
);
216 shost
->host_no
= scsi_host_next_hn
++; /* XXX(hch): still racy */
217 shost
->dma_channel
= 0xff;
219 /* These three are default values which can be overridden */
220 shost
->max_channel
= 0;
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
;
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.
235 shost
->max_cmd_len
= 12;
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
;
244 if (sht
->max_host_blocked
)
245 shost
->max_host_blocked
= sht
->max_host_blocked
;
247 shost
->max_host_blocked
= SCSI_DEFAULT_HOST_BLOCKED
;
250 * If the driver imposes no hard sector transfer limit, start at
251 * machine infinity initially.
253 if (sht
->max_sectors
)
254 shost
->max_sectors
= sht
->max_sectors
;
256 shost
->max_sectors
= SCSI_DEFAULT_MAX_SECTORS
;
259 * assume a 4GB boundary, if not set
261 if (sht
->dma_boundary
)
262 shost
->dma_boundary
= sht
->dma_boundary
;
264 shost
->dma_boundary
= 0xffffffff;
266 rval
= scsi_setup_command_freelist(shost
);
270 device_initialize(&shost
->shost_gendev
);
271 snprintf(shost
->shost_gendev
.bus_id
, BUS_ID_SIZE
, "host%d",
273 shost
->shost_gendev
.release
= scsi_host_dev_release
;
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",
281 shost
->eh_notify
= &complete
;
282 rval
= kernel_thread(scsi_error_handler
, shost
, 0);
284 goto fail_destroy_freelist
;
285 wait_for_completion(&complete
);
286 shost
->eh_notify
= NULL
;
287 scsi_proc_hostdir_add(shost
->hostt
);
290 fail_destroy_freelist
:
291 scsi_destroy_command_freelist(shost
);
297 struct Scsi_Host
*scsi_register(struct scsi_host_template
*sht
, int privsize
)
299 struct Scsi_Host
*shost
= scsi_host_alloc(sht
, privsize
);
302 printk(KERN_WARNING
"scsi_register() called on new-style "
303 "template for driver %s\n", sht
->name
);
308 list_add_tail(&shost
->sht_legacy_list
, &sht
->legacy_hosts
);
312 void scsi_unregister(struct Scsi_Host
*shost
)
314 list_del(&shost
->sht_legacy_list
);
315 scsi_host_put(shost
);
319 * scsi_host_lookup - get a reference to a Scsi_Host by host no
321 * @hostnum: host number to locate
324 * A pointer to located Scsi_Host or NULL.
326 struct Scsi_Host
*scsi_host_lookup(unsigned short hostnum
)
328 struct class *class = &shost_class
;
329 struct class_device
*cdev
;
330 struct Scsi_Host
*shost
= ERR_PTR(-ENXIO
), *p
;
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
);
340 up_read(&class->subsys
.rwsem
);
346 * scsi_host_get - inc a Scsi_Host ref count
347 * @shost: Pointer to Scsi_Host to inc.
349 struct Scsi_Host
*scsi_host_get(struct Scsi_Host
*shost
)
351 if (test_bit(SHOST_DEL
, &shost
->shost_state
) ||
352 !get_device(&shost
->shost_gendev
))
358 * scsi_host_put - dec a Scsi_Host ref count
359 * @shost: Pointer to Scsi_Host to dec.
361 void scsi_host_put(struct Scsi_Host
*shost
)
363 put_device(&shost
->shost_gendev
);
366 int scsi_init_hosts(void)
368 return class_register(&shost_class
);
371 void scsi_exit_hosts(void)
373 class_unregister(&shost_class
);