2 * hosts.c Copyright (C) 1992 Drew Eckhardt
3 * Copyright (C) 1993, 1994, 1995 Eric Youngdale
5 * mid to lowlevel SCSI driver interface
6 * Initial versions: Drew Eckhardt
7 * Subsequent revisions: Eric Youngdale
11 * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
12 * Added QLOGIC QLA1280 SCSI controller kernel host support.
13 * August 4, 1999 Fred Lewis, Intel DuPont
15 * Updated to reflect the new initialization scheme for the higher
16 * level of scsi drivers (sd/sr/st)
17 * September 17, 2000 Torben Mathiasen <tmm@image.dk>
19 * Restructured scsi_host lists and associated functions.
20 * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
25 * This file contains the medium level SCSI
26 * host interface initialization, as well as the scsi_hosts list of SCSI
27 * hosts currently present in the system.
30 #include <linux/module.h>
31 #include <linux/blk.h>
32 #include <linux/kernel.h>
33 #include <linux/string.h>
35 #include <linux/proc_fs.h>
36 #include <linux/init.h>
37 #include <linux/interrupt.h>
38 #include <linux/list.h>
39 #include <linux/smp_lock.h>
41 #define __KERNEL_SYSCALLS__
43 #include <linux/unistd.h>
49 static LIST_HEAD(scsi_host_list
);
50 static spinlock_t scsi_host_list_lock
= SPIN_LOCK_UNLOCKED
;
52 static int scsi_host_next_hn
; /* host_no for next new host */
53 static int scsi_hosts_registered
; /* cnt of registered scsi hosts */
54 static char *scsihosts
;
56 MODULE_PARM(scsihosts
, "s");
57 MODULE_PARM_DESC(scsihosts
, "scsihosts=driver1,driver2,driver3");
59 int __init
scsi_setup(char *str
)
65 __setup("scsihosts=", scsi_setup
);
69 * scsi_find_host_by_num - get a Scsi_Host by host no
71 * @host_no: host number to locate
74 * A pointer to located Scsi_Host or NULL.
76 static struct Scsi_Host
*scsi_find_host_by_num(unsigned short host_no
)
78 struct Scsi_Host
*shost
, *shost_found
= NULL
;
80 spin_lock(&scsi_host_list_lock
);
81 list_for_each_entry(shost
, &scsi_host_list
, sh_list
) {
82 if (shost
->host_no
> host_no
) {
87 } else if (shost
->host_no
== host_no
) {
92 spin_unlock(&scsi_host_list_lock
);
97 * scsi_alloc_hostnum - choose new SCSI host number based on host name.
98 * @name: String to store in name field
101 * Pointer to a new Scsi_Host_Name
103 static int scsi_alloc_host_num(const char *name
)
107 const char *start
, *end
;
111 namelen
= strlen(name
);
116 if (start
&& start
[0] != '\0') {
117 end
= strpbrk(start
, ",:");
119 hostlen
= (end
- start
);
122 hostlen
= strlen(start
);
124 * Look for a match on the scsihosts list.
126 if ((hostlen
== namelen
) &&
127 (strncmp(name
, start
, hostlen
) == 0) &&
128 (!scsi_find_host_by_num(hostnum
)))
133 * Look for any unused numbers.
135 if (!scsi_find_host_by_num(hostnum
))
141 return scsi_host_next_hn
++;
146 * scsi_tp_for_each_host - call function for each scsi host off a template
147 * @shost_tp: a pointer to a scsi host template
148 * @callback: a pointer to callback function
151 * 0 on Success / 1 on Failure
153 int scsi_tp_for_each_host(Scsi_Host_Template
*shost_tp
, int
154 (*callback
)(struct Scsi_Host
*shost
))
156 struct list_head
*lh
, *lh_sf
;
157 struct Scsi_Host
*shost
;
159 spin_lock(&scsi_host_list_lock
);
161 list_for_each_safe(lh
, lh_sf
, &scsi_host_list
) {
162 shost
= list_entry(lh
, struct Scsi_Host
, sh_list
);
163 if (shost
->hostt
== shost_tp
) {
164 spin_unlock(&scsi_host_list_lock
);
166 spin_lock(&scsi_host_list_lock
);
170 spin_unlock(&scsi_host_list_lock
);
176 * scsi_host_generic_release - default release function for hosts
180 * This is the default case for the release function. Its completely
181 * useless for anything but old ISA adapters
183 static void scsi_host_legacy_release(struct Scsi_Host
*shost
)
186 free_irq(shost
->irq
, NULL
);
187 if (shost
->dma_channel
!= 0xff)
188 free_dma(shost
->dma_channel
);
189 if (shost
->io_port
&& shost
->n_io_port
)
190 release_region(shost
->io_port
, shost
->n_io_port
);
193 static int scsi_remove_legacy_host(struct Scsi_Host
*shost
)
195 int error
, pcount
= scsi_hosts_registered
;
197 error
= scsi_remove_host(shost
);
201 if (shost
->hostt
->release
)
202 (*shost
->hostt
->release
)(shost
);
204 scsi_host_legacy_release(shost
);
206 if (pcount
== scsi_hosts_registered
)
207 scsi_unregister(shost
);
212 * scsi_remove_host - check a scsi host for release and release
213 * @shost: a pointer to a scsi host to release
216 * 0 on Success / 1 on Failure
218 int scsi_remove_host(struct Scsi_Host
*shost
)
220 struct scsi_device
*sdev
;
221 struct scsi_cmnd
*scmd
;
224 * FIXME Do ref counting. We force all of the devices offline to
225 * help prevent race conditions where other hosts/processors could
226 * try and get in and queue a command.
228 for (sdev
= shost
->host_queue
; sdev
; sdev
= sdev
->next
)
229 sdev
->online
= FALSE
;
231 for (sdev
= shost
->host_queue
; sdev
; sdev
= sdev
->next
) {
233 * Loop over all of the commands associated with the
234 * device. If any of them are busy, then set the state
235 * back to inactive and bail.
237 for (scmd
= sdev
->device_queue
; scmd
; scmd
= scmd
->next
) {
238 if (scmd
->request
&& scmd
->request
->rq_status
!=
240 printk(KERN_ERR
"SCSI device not inactive"
241 "- rq_status=%d, target=%d, pid=%ld,"
242 "state=%d, owner=%d.\n",
243 scmd
->request
->rq_status
,
244 scmd
->target
, scmd
->pid
,
245 scmd
->state
, scmd
->owner
);
246 for (sdev
= shost
->host_queue
; sdev
;
248 for (scmd
= sdev
->device_queue
; scmd
;
250 if (scmd
->request
->rq_status
==
251 RQ_SCSI_DISCONNECTING
)
252 scmd
->request
->rq_status
= RQ_INACTIVE
;
254 printk(KERN_ERR
"Device busy???\n");
258 * No, this device is really free. Mark it as such, and
261 scmd
->state
= SCSI_STATE_DISCONNECTING
;
263 scmd
->request
->rq_status
=
264 RQ_SCSI_DISCONNECTING
; /* Mark as
270 * Next we detach the high level drivers from the Scsi_Device
273 for (sdev
= shost
->host_queue
; sdev
; sdev
= sdev
->next
) {
274 scsi_detach_device(sdev
);
276 /* If something still attached, punt */
277 if (sdev
->attached
) {
278 printk(KERN_ERR
"Attached usage count = %d\n",
282 devfs_unregister(sdev
->de
);
283 device_unregister(&sdev
->sdev_driverfs_dev
);
286 /* Next we free up the Scsi_Cmnd structures for this host */
288 for (sdev
= shost
->host_queue
; sdev
;
289 sdev
= shost
->host_queue
) {
290 blk_cleanup_queue(&sdev
->request_queue
);
291 /* Next free up the Scsi_Device structures for this host */
292 shost
->host_queue
= sdev
->next
;
294 kfree(sdev
->inquiry
);
301 int scsi_add_host(struct Scsi_Host
*shost
)
303 Scsi_Host_Template
*sht
= shost
->hostt
;
304 struct scsi_device
*sdev
;
307 printk(KERN_INFO
"scsi%d : %s\n", shost
->host_no
,
308 sht
->info
? sht
->info(shost
) : sht
->name
);
310 device_register(&shost
->host_driverfs_dev
);
311 scan_scsis(shost
, 0, 0, 0, 0);
313 for (sdev
= shost
->host_queue
; sdev
; sdev
= sdev
->next
) {
314 if (sdev
->host
->hostt
!= sht
)
315 continue; /* XXX(hch): can this really happen? */
316 error
= scsi_attach_device(sdev
);
325 * scsi_unregister - unregister a scsi host
326 * @shost: scsi host to be unregistered
328 void scsi_unregister(struct Scsi_Host
*shost
)
330 /* Remove shost from scsi_host_list */
331 spin_lock(&scsi_host_list_lock
);
332 list_del(&shost
->sh_list
);
333 spin_unlock(&scsi_host_list_lock
);
336 * Next, kill the kernel error recovery thread for this host.
338 if (shost
->ehandler
) {
339 DECLARE_MUTEX_LOCKED(sem
);
340 shost
->eh_notify
= &sem
;
341 send_sig(SIGHUP
, shost
->ehandler
, 1);
343 shost
->eh_notify
= NULL
;
346 scsi_hosts_registered
--;
347 shost
->hostt
->present
--;
349 /* Cleanup proc and driverfs */
350 scsi_proc_host_rm(shost
);
351 device_unregister(&shost
->host_driverfs_dev
);
357 * scsi_register - register a scsi host adapter instance.
358 * @shost_tp: pointer to scsi host template
359 * @xtr_bytes: extra bytes to allocate for driver
362 * We call this when we come across a new host adapter. We only do
363 * this once we are 100% sure that we want to use this host adapter -
364 * it is a pain to reverse this, so we try to avoid it
367 * Pointer to a new Scsi_Host
369 extern int blk_nohighio
;
370 struct Scsi_Host
* scsi_register(Scsi_Host_Template
*shost_tp
, int xtr_bytes
)
372 struct Scsi_Host
*shost
, *shost_scr
;
373 struct list_head
*lh
;
375 DECLARE_MUTEX_LOCKED(sem
);
377 /* Check to see if this host has any error handling facilities */
378 if(shost_tp
->eh_strategy_handler
== NULL
&&
379 shost_tp
->eh_abort_handler
== NULL
&&
380 shost_tp
->eh_device_reset_handler
== NULL
&&
381 shost_tp
->eh_bus_reset_handler
== NULL
&&
382 shost_tp
->eh_host_reset_handler
== NULL
) {
383 printk(KERN_ERR
"ERROR: SCSI host `%s' has no error handling\nERROR: This is not a safe way to run your SCSI host\nERROR: The error handling must be added to this driver\n", shost_tp
->proc_name
);
386 gfp_mask
= GFP_KERNEL
;
387 if (shost_tp
->unchecked_isa_dma
&& xtr_bytes
)
388 gfp_mask
|= __GFP_DMA
;
390 shost
= kmalloc(sizeof(struct Scsi_Host
) + xtr_bytes
, gfp_mask
);
392 printk(KERN_ERR
"%s: out of memory.\n", __FUNCTION__
);
396 memset(shost
, 0, sizeof(struct Scsi_Host
) + xtr_bytes
);
398 shost
->host_no
= scsi_alloc_host_num(shost_tp
->proc_name
);
399 scsi_hosts_registered
++;
401 spin_lock_init(&shost
->default_lock
);
402 scsi_assign_lock(shost
, &shost
->default_lock
);
403 atomic_set(&shost
->host_active
,0);
405 init_waitqueue_head(&shost
->host_wait
);
406 shost
->dma_channel
= 0xff;
408 /* These three are default values which can be overridden */
409 shost
->max_channel
= 0;
414 * All drivers right now should be able to handle 12 byte
415 * commands. Every so often there are requests for 16 byte
416 * commands, but individual low-level drivers need to certify that
417 * they actually do something sensible with such commands.
419 shost
->max_cmd_len
= 12;
420 shost
->hostt
= shost_tp
;
421 shost
->host_blocked
= 0;
422 shost
->host_self_blocked
= FALSE
;
423 shost
->max_host_blocked
= shost_tp
->max_host_blocked
? shost_tp
->max_host_blocked
: SCSI_DEFAULT_HOST_BLOCKED
;
426 printk("%s: %x %x: %d\n", __FUNCTION_ (int)shost
,
427 (int)shost
->hostt
, xtr_bytes
);
431 * The next six are the default values which can be overridden if
434 shost
->this_id
= shost_tp
->this_id
;
435 shost
->can_queue
= shost_tp
->can_queue
;
436 shost
->sg_tablesize
= shost_tp
->sg_tablesize
;
437 shost
->cmd_per_lun
= shost_tp
->cmd_per_lun
;
438 shost
->unchecked_isa_dma
= shost_tp
->unchecked_isa_dma
;
439 shost
->use_clustering
= shost_tp
->use_clustering
;
441 shost
->highmem_io
= shost_tp
->highmem_io
;
443 shost
->max_sectors
= shost_tp
->max_sectors
;
444 shost
->use_blk_tcq
= shost_tp
->use_blk_tcq
;
446 spin_lock(&scsi_host_list_lock
);
448 * FIXME When device naming is complete remove this step that
449 * orders the scsi_host_list by host number and just do a
452 list_for_each(lh
, &scsi_host_list
) {
453 shost_scr
= list_entry(lh
, struct Scsi_Host
, sh_list
);
454 if (shost
->host_no
< shost_scr
->host_no
) {
455 __list_add(&shost
->sh_list
, shost_scr
->sh_list
.prev
,
456 &shost_scr
->sh_list
);
460 list_add_tail(&shost
->sh_list
, &scsi_host_list
);
462 spin_unlock(&scsi_host_list_lock
);
464 scsi_proc_host_add(shost
);
466 strncpy(shost
->host_driverfs_dev
.name
, shost_tp
->proc_name
,
468 sprintf(shost
->host_driverfs_dev
.bus_id
, "scsi%d",
471 shost
->eh_notify
= &sem
;
472 kernel_thread((int (*)(void *)) scsi_error_handler
, (void *) shost
, 0);
474 * Now wait for the kernel error thread to initialize itself
475 * as it might be needed when we scan the bus.
478 shost
->eh_notify
= NULL
;
480 shost
->hostt
->present
++;
486 * scsi_register_host - register a low level host driver
487 * @shost_tp: pointer to a scsi host driver template
490 * 0 on Success / 1 on Failure.
492 int scsi_register_host(Scsi_Host_Template
*shost_tp
)
494 struct Scsi_Host
*shost
;
498 * Check no detect routine.
500 if (!shost_tp
->detect
)
503 /* If max_sectors isn't set, default to max */
504 if (!shost_tp
->max_sectors
)
505 shost_tp
->max_sectors
= 1024;
507 cur_cnt
= scsi_hosts_registered
;
510 * The detect routine must carefully spinunlock/spinlock if it
511 * enables interrupts, since all interrupt handlers do spinlock as
516 * detect should do its own locking
517 * FIXME present is now set is scsi_register which breaks manual
518 * registration code below.
520 shost_tp
->detect(shost_tp
);
521 if (!shost_tp
->present
)
524 if (cur_cnt
== scsi_hosts_registered
) {
525 if (shost_tp
->present
> 1) {
526 printk(KERN_ERR
"scsi: Failure to register"
527 "low-level scsi driver");
528 scsi_unregister_host(shost_tp
);
533 * The low-level driver failed to register a driver.
534 * We can do this now.
536 * XXX Who needs manual registration and why???
538 if (!scsi_register(shost_tp
, 0)) {
539 printk(KERN_ERR
"scsi: register failed.\n");
540 scsi_unregister_host(shost_tp
);
547 * XXX(hch) use scsi_tp_for_each_host() once it propagates
548 * error returns properly.
550 list_for_each_entry(shost
, &scsi_host_list
, sh_list
)
551 if (shost
->hostt
== shost_tp
)
552 if (scsi_add_host(shost
))
558 scsi_unregister_host(shost_tp
); /* easiest way to clean up?? */
563 * scsi_unregister_host - unregister a low level host adapter driver
564 * @shost_tp: scsi host template to unregister.
567 * Similarly, this entry point should be called by a loadable module
568 * if it is trying to remove a low level scsi driver from the system.
571 * 0 on Success / 1 on Failure
574 * rmmod does not care what we return here the module will be
577 int scsi_unregister_host(Scsi_Host_Template
*shost_tp
)
581 /* get the big kernel lock, so we don't race with open() */
584 pcount
= scsi_hosts_registered
;
586 scsi_tp_for_each_host(shost_tp
, scsi_remove_legacy_host
);
588 if (pcount
!= scsi_hosts_registered
)
589 printk(KERN_INFO
"scsi : %d host%s left.\n", scsi_hosts_registered
,
590 (scsi_hosts_registered
== 1) ? "" : "s");
598 * *scsi_host_get_next - get scsi host and inc ref count
599 * @shost: pointer to a Scsi_Host or NULL to start.
602 * A pointer to next Scsi_Host in list or NULL.
604 struct Scsi_Host
*scsi_host_get_next(struct Scsi_Host
*shost
)
606 struct list_head
*lh
= NULL
;
608 spin_lock(&scsi_host_list_lock
);
610 /* XXX Dec ref on cur shost */
611 lh
= shost
->sh_list
.next
;
613 lh
= scsi_host_list
.next
;
616 if (lh
== &scsi_host_list
) {
617 shost
= (struct Scsi_Host
*)NULL
;
621 shost
= list_entry(lh
, struct Scsi_Host
, sh_list
);
622 /* XXX Inc ref count */
625 spin_unlock(&scsi_host_list_lock
);
630 * scsi_host_hn_get - get a Scsi_Host by host no and inc ref count
631 * @host_no: host number to locate
634 * A pointer to located Scsi_Host or NULL.
636 struct Scsi_Host
*scsi_host_hn_get(unsigned short host_no
)
638 /* XXX Inc ref count */
639 return scsi_find_host_by_num(host_no
);
643 * *scsi_host_put - dec a Scsi_Host ref count
644 * @shost: Pointer to Scsi_Host to dec.
646 void scsi_host_put(struct Scsi_Host
*shost
)
649 /* XXX Get list lock */
650 /* XXX dec ref count */
651 /* XXX Release list lock */
656 * scsi_host_init - set up the scsi host number list based on any entries
659 void __init
scsi_host_init(void)
663 shost_hn
= scsihosts
;
666 shost_hn
= strpbrk(shost_hn
, ":,");
672 void scsi_host_busy_inc(struct Scsi_Host
*shost
, Scsi_Device
*sdev
)
676 spin_lock_irqsave(shost
->host_lock
, flags
);
679 spin_unlock_irqrestore(shost
->host_lock
, flags
);
682 void scsi_host_busy_dec_and_test(struct Scsi_Host
*shost
, Scsi_Device
*sdev
)
686 spin_lock_irqsave(shost
->host_lock
, flags
);
689 if (shost
->in_recovery
&& (shost
->host_busy
== shost
->host_failed
)) {
691 SCSI_LOG_ERROR_RECOVERY(5, printk("Waking error handler"
694 spin_unlock_irqrestore(shost
->host_lock
, flags
);
697 void scsi_host_failed_inc_and_test(struct Scsi_Host
*shost
)
701 spin_lock_irqsave(shost
->host_lock
, flags
);
702 shost
->in_recovery
= 1;
703 shost
->host_failed
++;
704 if (shost
->host_busy
== shost
->host_failed
) {
706 SCSI_LOG_ERROR_RECOVERY(5, printk("Waking error handler"
709 spin_unlock_irqrestore(shost
->host_lock
, flags
);
713 * Overrides for Emacs so that we follow Linus's tabbing style.
714 * Emacs will notice this stuff at the end of the file and automatically
715 * adjust the settings for this buffer only. This must remain at the end
717 * ---------------------------------------------------------------------------
720 * c-brace-imaginary-offset: 0
722 * c-argdecl-indent: 4
724 * c-continued-statement-offset: 4
725 * c-continued-brace-offset: 0
726 * indent-tabs-mode: nil