Merge with Linux 2.5.48.
[linux-2.6/linux-mips.git] / drivers / scsi / hosts.c
blobd2c75d0504799686cb35dac808187d27fd8a1513
1 /*
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
9 * <drew@colorado.edu>
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>
34 #include <linux/mm.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>
44 #include <asm/dma.h>
46 #include "scsi.h"
47 #include "hosts.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");
58 #ifndef MODULE
59 int __init scsi_setup(char *str)
61 scsihosts = str;
62 return 1;
65 __setup("scsihosts=", scsi_setup);
66 #endif
68 /**
69 * scsi_find_host_by_num - get a Scsi_Host by host no
71 * @host_no: host number to locate
73 * Return value:
74 * A pointer to located Scsi_Host or NULL.
75 **/
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) {
84 * The list is sorted.
86 break;
87 } else if (shost->host_no == host_no) {
88 shost_found = shost;
89 break;
92 spin_unlock(&scsi_host_list_lock);
93 return shost_found;
96 /**
97 * scsi_alloc_hostnum - choose new SCSI host number based on host name.
98 * @name: String to store in name field
100 * Return value:
101 * Pointer to a new Scsi_Host_Name
103 static int scsi_alloc_host_num(const char *name)
105 int hostnum;
106 int namelen;
107 const char *start, *end;
109 if (name) {
110 hostnum = 0;
111 namelen = strlen(name);
112 start = scsihosts;
113 while (1) {
114 int hostlen;
116 if (start && start[0] != '\0') {
117 end = strpbrk(start, ",:");
118 if (end) {
119 hostlen = (end - start);
120 end++;
121 } else
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)))
129 return hostnum;
130 start = end;
131 } else {
133 * Look for any unused numbers.
135 if (!scsi_find_host_by_num(hostnum))
136 return hostnum;
138 hostnum++;
140 } else
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
150 * Return value:
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);
165 callback(shost);
166 spin_lock(&scsi_host_list_lock);
170 spin_unlock(&scsi_host_list_lock);
172 return 0;
176 * scsi_host_generic_release - default release function for hosts
177 * @shost:
179 * Description:
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)
185 if (shost->irq)
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);
198 if (error)
199 return error;
201 if (shost->hostt->release)
202 (*shost->hostt->release)(shost);
203 else
204 scsi_host_legacy_release(shost);
206 if (pcount == scsi_hosts_registered)
207 scsi_unregister(shost);
208 return 0;
212 * scsi_remove_host - check a scsi host for release and release
213 * @shost: a pointer to a scsi host to release
215 * Return value:
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 !=
239 RQ_INACTIVE) {
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;
247 sdev = sdev->next) {
248 for (scmd = sdev->device_queue; scmd;
249 scmd = scmd->next)
250 if (scmd->request->rq_status ==
251 RQ_SCSI_DISCONNECTING)
252 scmd->request->rq_status = RQ_INACTIVE;
254 printk(KERN_ERR "Device busy???\n");
255 return 1;
258 * No, this device is really free. Mark it as such, and
259 * continue on.
261 scmd->state = SCSI_STATE_DISCONNECTING;
262 if (scmd->request)
263 scmd->request->rq_status =
264 RQ_SCSI_DISCONNECTING; /* Mark as
265 busy */
270 * Next we detach the high level drivers from the Scsi_Device
271 * structures
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",
279 sdev->attached);
280 return 1;
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;
293 if (sdev->inquiry)
294 kfree(sdev->inquiry);
295 kfree(sdev);
298 return 0;
301 int scsi_add_host(struct Scsi_Host *shost)
303 Scsi_Host_Template *sht = shost->hostt;
304 struct scsi_device *sdev;
305 int error = 0;
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);
317 if (error)
318 break;
321 return error;
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);
342 down(&sem);
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);
353 kfree(shost);
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
361 * Note:
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
366 * Return value:
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;
374 int gfp_mask;
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);
384 dump_stack();
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);
391 if (!shost) {
392 printk(KERN_ERR "%s: out of memory.\n", __FUNCTION__);
393 return NULL;
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;
410 shost->max_id = 8;
411 shost->max_lun = 8;
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;
425 #ifdef DEBUG
426 printk("%s: %x %x: %d\n", __FUNCTION_ (int)shost,
427 (int)shost->hostt, xtr_bytes);
428 #endif
431 * The next six are the default values which can be overridden if
432 * need be
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;
440 if (!blk_nohighio)
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
450 * list_add_tail.
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);
457 goto found;
460 list_add_tail(&shost->sh_list, &scsi_host_list);
461 found:
462 spin_unlock(&scsi_host_list_lock);
464 scsi_proc_host_add(shost);
466 strncpy(shost->host_driverfs_dev.name, shost_tp->proc_name,
467 DEVICE_NAME_SIZE-1);
468 sprintf(shost->host_driverfs_dev.bus_id, "scsi%d",
469 shost->host_no);
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.
477 down(&sem);
478 shost->eh_notify = NULL;
480 shost->hostt->present++;
482 return shost;
486 * scsi_register_host - register a low level host driver
487 * @shost_tp: pointer to a scsi host driver template
489 * Return value:
490 * 0 on Success / 1 on Failure.
492 int scsi_register_host(Scsi_Host_Template *shost_tp)
494 struct Scsi_Host *shost;
495 int cur_cnt;
498 * Check no detect routine.
500 if (!shost_tp->detect)
501 return 1;
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
512 * well.
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)
522 return 0;
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);
529 return 1;
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);
541 return 1;
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))
553 goto out_of_space;
555 return 0;
557 out_of_space:
558 scsi_unregister_host(shost_tp); /* easiest way to clean up?? */
559 return 1;
563 * scsi_unregister_host - unregister a low level host adapter driver
564 * @shost_tp: scsi host template to unregister.
566 * Description:
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.
570 * Return value:
571 * 0 on Success / 1 on Failure
573 * Notes:
574 * rmmod does not care what we return here the module will be
575 * removed.
577 int scsi_unregister_host(Scsi_Host_Template *shost_tp)
579 int pcount;
581 /* get the big kernel lock, so we don't race with open() */
582 lock_kernel();
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");
592 unlock_kernel();
593 return 0;
598 * *scsi_host_get_next - get scsi host and inc ref count
599 * @shost: pointer to a Scsi_Host or NULL to start.
601 * Return value:
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);
609 if (shost) {
610 /* XXX Dec ref on cur shost */
611 lh = shost->sh_list.next;
612 } else {
613 lh = scsi_host_list.next;
616 if (lh == &scsi_host_list) {
617 shost = (struct Scsi_Host *)NULL;
618 goto done;
621 shost = list_entry(lh, struct Scsi_Host, sh_list);
622 /* XXX Inc ref count */
624 done:
625 spin_unlock(&scsi_host_list_lock);
626 return shost;
630 * scsi_host_hn_get - get a Scsi_Host by host no and inc ref count
631 * @host_no: host number to locate
633 * Return value:
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 */
652 return;
656 * scsi_host_init - set up the scsi host number list based on any entries
657 * scsihosts.
659 void __init scsi_host_init(void)
661 char *shost_hn;
663 shost_hn = scsihosts;
664 while (shost_hn) {
665 scsi_host_next_hn++;
666 shost_hn = strpbrk(shost_hn, ":,");
667 if (shost_hn)
668 shost_hn++;
672 void scsi_host_busy_inc(struct Scsi_Host *shost, Scsi_Device *sdev)
674 unsigned long flags;
676 spin_lock_irqsave(shost->host_lock, flags);
677 shost->host_busy++;
678 sdev->device_busy++;
679 spin_unlock_irqrestore(shost->host_lock, flags);
682 void scsi_host_busy_dec_and_test(struct Scsi_Host *shost, Scsi_Device *sdev)
684 unsigned long flags;
686 spin_lock_irqsave(shost->host_lock, flags);
687 shost->host_busy--;
688 sdev->device_busy--;
689 if (shost->in_recovery && (shost->host_busy == shost->host_failed)) {
690 up(shost->eh_wait);
691 SCSI_LOG_ERROR_RECOVERY(5, printk("Waking error handler"
692 " thread\n"));
694 spin_unlock_irqrestore(shost->host_lock, flags);
697 void scsi_host_failed_inc_and_test(struct Scsi_Host *shost)
699 unsigned long flags;
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) {
705 up(shost->eh_wait);
706 SCSI_LOG_ERROR_RECOVERY(5, printk("Waking error handler"
707 " thread\n"));
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
716 * of the file.
717 * ---------------------------------------------------------------------------
718 * Local variables:
719 * c-indent-level: 4
720 * c-brace-imaginary-offset: 0
721 * c-brace-offset: -4
722 * c-argdecl-indent: 4
723 * c-label-offset: -4
724 * c-continued-statement-offset: 4
725 * c-continued-brace-offset: 0
726 * indent-tabs-mode: nil
727 * tab-width: 8
728 * End: