File: /usr/src/linux/drivers/scsi/scsi.c

1     /*
2      *  scsi.c Copyright (C) 1992 Drew Eckhardt
3      *         Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4      *
5      *  generic mid-level SCSI driver
6      *      Initial versions: Drew Eckhardt
7      *      Subsequent revisions: Eric Youngdale
8      *
9      *  <drew@colorado.edu>
10      *
11      *  Bug correction thanks go to :
12      *      Rik Faith <faith@cs.unc.edu>
13      *      Tommy Thorn <tthorn>
14      *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
15      *
16      *  Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to
17      *  add scatter-gather, multiple outstanding request, and other
18      *  enhancements.
19      *
20      *  Native multichannel, wide scsi, /proc/scsi and hot plugging
21      *  support added by Michael Neuffer <mike@i-connect.net>
22      *
23      *  Added request_module("scsi_hostadapter") for kerneld:
24      *  (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modules.conf)
25      *  Bjorn Ekwall  <bj0rn@blox.se>
26      *  (changed to kmod)
27      *
28      *  Major improvements to the timeout, abort, and reset processing,
29      *  as well as performance modifications for large queue depths by
30      *  Leonard N. Zubkoff <lnz@dandelion.com>
31      *
32      *  Converted cli() code to spinlocks, Ingo Molnar
33      *
34      *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
35      *
36      *  out_of_space hacks, D. Gilbert (dpg) 990608
37      */
38     
39     #define REVISION	"Revision: 1.00"
40     #define VERSION		"Id: scsi.c 1.00 2000/09/26"
41     
42     #include <linux/config.h>
43     #include <linux/module.h>
44     
45     #include <linux/sched.h>
46     #include <linux/timer.h>
47     #include <linux/string.h>
48     #include <linux/slab.h>
49     #include <linux/ioport.h>
50     #include <linux/kernel.h>
51     #include <linux/stat.h>
52     #include <linux/blk.h>
53     #include <linux/interrupt.h>
54     #include <linux/delay.h>
55     #include <linux/init.h>
56     #include <linux/smp_lock.h>
57     #include <linux/completion.h>
58     
59     #define __KERNEL_SYSCALLS__
60     
61     #include <linux/unistd.h>
62     #include <linux/spinlock.h>
63     
64     #include <asm/system.h>
65     #include <asm/irq.h>
66     #include <asm/dma.h>
67     #include <asm/uaccess.h>
68     
69     #include "scsi.h"
70     #include "hosts.h"
71     #include "constants.h"
72     
73     #ifdef CONFIG_KMOD
74     #include <linux/kmod.h>
75     #endif
76     
77     #undef USE_STATIC_SCSI_MEMORY
78     
79     struct proc_dir_entry *proc_scsi;
80     
81     #ifdef CONFIG_PROC_FS
82     static int scsi_proc_info(char *buffer, char **start, off_t offset, int length);
83     static void scsi_dump_status(int level);
84     #endif
85     
86     /*
87        static const char RCSid[] = "$Header: /vger/u4/cvs/linux/drivers/scsi/scsi.c,v 1.38 1997/01/19 23:07:18 davem Exp $";
88      */
89     
90     /*
91      * Definitions and constants.
92      */
93     
94     #define MIN_RESET_DELAY (2*HZ)
95     
96     /* Do not call reset on error if we just did a reset within 15 sec. */
97     #define MIN_RESET_PERIOD (15*HZ)
98     
99     
100     /*
101      * Data declarations.
102      */
103     unsigned long scsi_pid;
104     Scsi_Cmnd *last_cmnd;
105     /* Command groups 3 and 4 are reserved and should never be used.  */
106     const unsigned char scsi_command_size[8] =
107     {
108     	6, 10, 10, 12,
109     	12, 12, 10, 10
110     };
111     static unsigned long serial_number;
112     static Scsi_Cmnd *scsi_bh_queue_head;
113     static Scsi_Cmnd *scsi_bh_queue_tail;
114     
115     /*
116      * Note - the initial logging level can be set here to log events at boot time.
117      * After the system is up, you may enable logging via the /proc interface.
118      */
119     unsigned int scsi_logging_level;
120     
121     const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
122     {
123     	"Direct-Access    ",
124     	"Sequential-Access",
125     	"Printer          ",
126     	"Processor        ",
127     	"WORM             ",
128     	"CD-ROM           ",
129     	"Scanner          ",
130     	"Optical Device   ",
131     	"Medium Changer   ",
132     	"Communications   ",
133     	"Unknown          ",
134     	"Unknown          ",
135     	"Unknown          ",
136     	"Enclosure        ",
137     };
138     
139     /* 
140      * Function prototypes.
141      */
142     extern void scsi_times_out(Scsi_Cmnd * SCpnt);
143     void scsi_build_commandblocks(Scsi_Device * SDpnt);
144     
145     /*
146      * These are the interface to the old error handling code.  It should go away
147      * someday soon.
148      */
149     extern void scsi_old_done(Scsi_Cmnd * SCpnt);
150     extern void scsi_old_times_out(Scsi_Cmnd * SCpnt);
151     
152     
153     /*
154      * Function:    scsi_initialize_queue()
155      *
156      * Purpose:     Selects queue handler function for a device.
157      *
158      * Arguments:   SDpnt   - device for which we need a handler function.
159      *
160      * Returns:     Nothing
161      *
162      * Lock status: No locking assumed or required.
163      *
164      * Notes:       Most devices will end up using scsi_request_fn for the
165      *              handler function (at least as things are done now).
166      *              The "block" feature basically ensures that only one of
167      *              the blocked hosts is active at one time, mainly to work around
168      *              buggy DMA chipsets where the memory gets starved.
169      *              For this case, we have a special handler function, which
170      *              does some checks and ultimately calls scsi_request_fn.
171      *
172      *              The single_lun feature is a similar special case.
173      *
174      *              We handle these things by stacking the handlers.  The
175      *              special case handlers simply check a few conditions,
176      *              and return if they are not supposed to do anything.
177      *              In the event that things are OK, then they call the next
178      *              handler in the list - ultimately they call scsi_request_fn
179      *              to do the dirty deed.
180      */
181     void  scsi_initialize_queue(Scsi_Device * SDpnt, struct Scsi_Host * SHpnt) {
182     	blk_init_queue(&SDpnt->request_queue, scsi_request_fn);
183             blk_queue_headactive(&SDpnt->request_queue, 0);
184             SDpnt->request_queue.queuedata = (void *) SDpnt;
185     }
186     
187     #ifdef MODULE
188     MODULE_PARM(scsi_logging_level, "i");
189     MODULE_PARM_DESC(scsi_logging_level, "SCSI logging level; should be zero or nonzero");
190     
191     #else
192     
193     static int __init scsi_logging_setup(char *str)
194     {
195     	int tmp;
196     
197     	if (get_option(&str, &tmp) == 1) {
198     		scsi_logging_level = (tmp ? ~0 : 0);
199     		return 1;
200     	} else {
201     		printk(KERN_INFO "scsi_logging_setup : usage scsi_logging_level=n "
202     		       "(n should be 0 or non-zero)\n");
203     		return 0;
204     	}
205     }
206     
207     __setup("scsi_logging=", scsi_logging_setup);
208     
209     #endif
210     
211     /*
212      *	Issue a command and wait for it to complete
213      */
214      
215     static void scsi_wait_done(Scsi_Cmnd * SCpnt)
216     {
217     	struct request *req;
218     
219     	req = &SCpnt->request;
220     	req->rq_status = RQ_SCSI_DONE;	/* Busy, but indicate request done */
221     
222     	if (req->waiting != NULL) {
223     		complete(req->waiting);
224     	}
225     }
226     
227     /*
228      * This lock protects the freelist for all devices on the system.
229      * We could make this finer grained by having a single lock per
230      * device if it is ever found that there is excessive contention
231      * on this lock.
232      */
233     static spinlock_t device_request_lock = SPIN_LOCK_UNLOCKED;
234     
235     /*
236      * Used to protect insertion into and removal from the queue of
237      * commands to be processed by the bottom half handler.
238      */
239     static spinlock_t scsi_bhqueue_lock = SPIN_LOCK_UNLOCKED;
240     
241     /*
242      * Function:    scsi_allocate_request
243      *
244      * Purpose:     Allocate a request descriptor.
245      *
246      * Arguments:   device    - device for which we want a request
247      *
248      * Lock status: No locks assumed to be held.  This function is SMP-safe.
249      *
250      * Returns:     Pointer to request block.
251      *
252      * Notes:       With the new queueing code, it becomes important
253      *              to track the difference between a command and a
254      *              request.  A request is a pending item in the queue that
255      *              has not yet reached the top of the queue.
256      */
257     
258     Scsi_Request *scsi_allocate_request(Scsi_Device * device)
259     {
260       	Scsi_Request *SRpnt = NULL;
261       
262       	if (!device)
263       		panic("No device passed to scsi_allocate_request().\n");
264       
265     	SRpnt = (Scsi_Request *) kmalloc(sizeof(Scsi_Request), GFP_ATOMIC);
266     	if( SRpnt == NULL )
267     	{
268     		return NULL;
269     	}
270     
271     	memset(SRpnt, 0, sizeof(Scsi_Request));
272     	SRpnt->sr_device = device;
273     	SRpnt->sr_host = device->host;
274     	SRpnt->sr_magic = SCSI_REQ_MAGIC;
275     	SRpnt->sr_data_direction = SCSI_DATA_UNKNOWN;
276     
277     	return SRpnt;
278     }
279     
280     /*
281      * Function:    scsi_release_request
282      *
283      * Purpose:     Release a request descriptor.
284      *
285      * Arguments:   device    - device for which we want a request
286      *
287      * Lock status: No locks assumed to be held.  This function is SMP-safe.
288      *
289      * Returns:     Pointer to request block.
290      *
291      * Notes:       With the new queueing code, it becomes important
292      *              to track the difference between a command and a
293      *              request.  A request is a pending item in the queue that
294      *              has not yet reached the top of the queue.  We still need
295      *              to free a request when we are done with it, of course.
296      */
297     void scsi_release_request(Scsi_Request * req)
298     {
299     	if( req->sr_command != NULL )
300     	{
301     		scsi_release_command(req->sr_command);
302     		req->sr_command = NULL;
303     	}
304     
305     	kfree(req);
306     }
307     
308     /*
309      * Function:    scsi_allocate_device
310      *
311      * Purpose:     Allocate a command descriptor.
312      *
313      * Arguments:   device    - device for which we want a command descriptor
314      *              wait      - 1 if we should wait in the event that none
315      *                          are available.
316      *              interruptible - 1 if we should unblock and return NULL
317      *                          in the event that we must wait, and a signal
318      *                          arrives.
319      *
320      * Lock status: No locks assumed to be held.  This function is SMP-safe.
321      *
322      * Returns:     Pointer to command descriptor.
323      *
324      * Notes:       Prior to the new queue code, this function was not SMP-safe.
325      *
326      *              If the wait flag is true, and we are waiting for a free
327      *              command block, this function will interrupt and return
328      *              NULL in the event that a signal arrives that needs to
329      *              be handled.
330      *
331      *              This function is deprecated, and drivers should be
332      *              rewritten to use Scsi_Request instead of Scsi_Cmnd.
333      */
334     
335     Scsi_Cmnd *scsi_allocate_device(Scsi_Device * device, int wait, 
336                                     int interruptable)
337     {
338      	struct Scsi_Host *host;
339       	Scsi_Cmnd *SCpnt = NULL;
340     	Scsi_Device *SDpnt;
341     	unsigned long flags;
342       
343       	if (!device)
344       		panic("No device passed to scsi_allocate_device().\n");
345       
346       	host = device->host;
347       
348     	spin_lock_irqsave(&device_request_lock, flags);
349      
350     	while (1 == 1) {
351     		SCpnt = NULL;
352     		if (!device->device_blocked) {
353     			if (device->single_lun) {
354     				/*
355     				 * FIXME(eric) - this is not at all optimal.  Given that
356     				 * single lun devices are rare and usually slow
357     				 * (i.e. CD changers), this is good enough for now, but
358     				 * we may want to come back and optimize this later.
359     				 *
360     				 * Scan through all of the devices attached to this
361     				 * host, and see if any are active or not.  If so,
362     				 * we need to defer this command.
363     				 *
364     				 * We really need a busy counter per device.  This would
365     				 * allow us to more easily figure out whether we should
366     				 * do anything here or not.
367     				 */
368     				for (SDpnt = host->host_queue;
369     				     SDpnt;
370     				     SDpnt = SDpnt->next) {
371     					/*
372     					 * Only look for other devices on the same bus
373     					 * with the same target ID.
374     					 */
375     					if (SDpnt->channel != device->channel
376     					    || SDpnt->id != device->id
377     					    || SDpnt == device) {
378      						continue;
379     					}
380                                             if( atomic_read(&SDpnt->device_active) != 0)
381                                             {
382                                                     break;
383                                             }
384     				}
385     				if (SDpnt) {
386     					/*
387     					 * Some other device in this cluster is busy.
388     					 * If asked to wait, we need to wait, otherwise
389     					 * return NULL.
390     					 */
391     					SCpnt = NULL;
392     					goto busy;
393     				}
394     			}
395     			/*
396     			 * Now we can check for a free command block for this device.
397     			 */
398     			for (SCpnt = device->device_queue; SCpnt; SCpnt = SCpnt->next) {
399     				if (SCpnt->request.rq_status == RQ_INACTIVE)
400     					break;
401     			}
402     		}
403     		/*
404     		 * If we couldn't find a free command block, and we have been
405     		 * asked to wait, then do so.
406     		 */
407     		if (SCpnt) {
408     			break;
409     		}
410           busy:
411     		/*
412     		 * If we have been asked to wait for a free block, then
413     		 * wait here.
414     		 */
415     		if (wait) {
416                             DECLARE_WAITQUEUE(wait, current);
417     
418                             /*
419                              * We need to wait for a free commandblock.  We need to
420                              * insert ourselves into the list before we release the
421                              * lock.  This way if a block were released the same
422                              * microsecond that we released the lock, the call
423                              * to schedule() wouldn't block (well, it might switch,
424                              * but the current task will still be schedulable.
425                              */
426                             add_wait_queue(&device->scpnt_wait, &wait);
427                             if( interruptable ) {
428                                     set_current_state(TASK_INTERRUPTIBLE);
429                             } else {
430                                     set_current_state(TASK_UNINTERRUPTIBLE);
431                             }
432     
433                             spin_unlock_irqrestore(&device_request_lock, flags);
434     
435     			/*
436     			 * This should block until a device command block
437     			 * becomes available.
438     			 */
439                             schedule();
440     
441     			spin_lock_irqsave(&device_request_lock, flags);
442     
443                             remove_wait_queue(&device->scpnt_wait, &wait);
444                             /*
445                              * FIXME - Isn't this redundant??  Someone
446                              * else will have forced the state back to running.
447                              */
448                             set_current_state(TASK_RUNNING);
449                             /*
450                              * In the event that a signal has arrived that we need
451                              * to consider, then simply return NULL.  Everyone
452                              * that calls us should be prepared for this
453                              * possibility, and pass the appropriate code back
454                              * to the user.
455                              */
456                             if( interruptable ) {
457                                     if (signal_pending(current)) {
458                                             spin_unlock_irqrestore(&device_request_lock, flags);
459                                             return NULL;
460                                     }
461                             }
462     		} else {
463                             spin_unlock_irqrestore(&device_request_lock, flags);
464     			return NULL;
465     		}
466     	}
467     
468     	SCpnt->request.rq_status = RQ_SCSI_BUSY;
469     	SCpnt->request.waiting = NULL;	/* And no one is waiting for this
470     					 * to complete */
471     	atomic_inc(&SCpnt->host->host_active);
472     	atomic_inc(&SCpnt->device->device_active);
473     
474     	SCpnt->buffer  = NULL;
475     	SCpnt->bufflen = 0;
476     	SCpnt->request_buffer = NULL;
477     	SCpnt->request_bufflen = 0;
478     
479     	SCpnt->use_sg = 0;	/* Reset the scatter-gather flag */
480     	SCpnt->old_use_sg = 0;
481     	SCpnt->transfersize = 0;	/* No default transfer size */
482     	SCpnt->cmd_len = 0;
483     
484     	SCpnt->sc_data_direction = SCSI_DATA_UNKNOWN;
485     	SCpnt->sc_request = NULL;
486     	SCpnt->sc_magic = SCSI_CMND_MAGIC;
487     
488             SCpnt->result = 0;
489     	SCpnt->underflow = 0;	/* Do not flag underflow conditions */
490     	SCpnt->old_underflow = 0;
491     	SCpnt->resid = 0;
492     	SCpnt->state = SCSI_STATE_INITIALIZING;
493     	SCpnt->owner = SCSI_OWNER_HIGHLEVEL;
494     
495     	spin_unlock_irqrestore(&device_request_lock, flags);
496     
497     	SCSI_LOG_MLQUEUE(5, printk("Activating command for device %d (%d)\n",
498     				   SCpnt->target,
499     				atomic_read(&SCpnt->host->host_active)));
500     
501     	return SCpnt;
502     }
503     
504     inline void __scsi_release_command(Scsi_Cmnd * SCpnt)
505     {
506     	unsigned long flags;
507             Scsi_Device * SDpnt;
508     
509     	spin_lock_irqsave(&device_request_lock, flags);
510     
511             SDpnt = SCpnt->device;
512     
513     	SCpnt->request.rq_status = RQ_INACTIVE;
514     	SCpnt->state = SCSI_STATE_UNUSED;
515     	SCpnt->owner = SCSI_OWNER_NOBODY;
516     	atomic_dec(&SCpnt->host->host_active);
517     	atomic_dec(&SDpnt->device_active);
518     
519     	SCSI_LOG_MLQUEUE(5, printk("Deactivating command for device %d (active=%d, failed=%d)\n",
520     				   SCpnt->target,
521     				   atomic_read(&SCpnt->host->host_active),
522     				   SCpnt->host->host_failed));
523     	if (SCpnt->host->host_failed != 0) {
524     		SCSI_LOG_ERROR_RECOVERY(5, printk("Error handler thread %d %d\n",
525     						SCpnt->host->in_recovery,
526     						SCpnt->host->eh_active));
527     	}
528     	/*
529     	 * If the host is having troubles, then look to see if this was the last
530     	 * command that might have failed.  If so, wake up the error handler.
531     	 */
532     	if (SCpnt->host->in_recovery
533     	    && !SCpnt->host->eh_active
534     	    && SCpnt->host->host_busy == SCpnt->host->host_failed) {
535     		SCSI_LOG_ERROR_RECOVERY(5, printk("Waking error handler thread (%d)\n",
536     			     atomic_read(&SCpnt->host->eh_wait->count)));
537     		up(SCpnt->host->eh_wait);
538     	}
539     
540     	spin_unlock_irqrestore(&device_request_lock, flags);
541     
542             /*
543              * Wake up anyone waiting for this device.  Do this after we
544              * have released the lock, as they will need it as soon as
545              * they wake up.  
546              */
547     	wake_up(&SDpnt->scpnt_wait);
548     }
549     
550     /*
551      * Function:    scsi_release_command
552      *
553      * Purpose:     Release a command block.
554      *
555      * Arguments:   SCpnt - command block we are releasing.
556      *
557      * Notes:       The command block can no longer be used by the caller once
558      *              this funciton is called.  This is in effect the inverse
559      *              of scsi_allocate_device.  Note that we also must perform
560      *              a couple of additional tasks.  We must first wake up any
561      *              processes that might have blocked waiting for a command
562      *              block, and secondly we must hit the queue handler function
563      *              to make sure that the device is busy.  Note - there is an
564      *              option to not do this - there were instances where we could
565      *              recurse too deeply and blow the stack if this happened
566      *              when we were indirectly called from the request function
567      *              itself.
568      *
569      *              The idea is that a lot of the mid-level internals gunk
570      *              gets hidden in this function.  Upper level drivers don't
571      *              have any chickens to wave in the air to get things to
572      *              work reliably.
573      *
574      *              This function is deprecated, and drivers should be
575      *              rewritten to use Scsi_Request instead of Scsi_Cmnd.
576      */
577     void scsi_release_command(Scsi_Cmnd * SCpnt)
578     {
579             request_queue_t *q;
580             Scsi_Device * SDpnt;
581     
582             SDpnt = SCpnt->device;
583     
584             __scsi_release_command(SCpnt);
585     
586             /*
587              * Finally, hit the queue request function to make sure that
588              * the device is actually busy if there are requests present.
589              * This won't block - if the device cannot take any more, life
590              * will go on.  
591              */
592             q = &SDpnt->request_queue;
593             scsi_queue_next_request(q, NULL);                
594     }
595     
596     /*
597      * Function:    scsi_dispatch_command
598      *
599      * Purpose:     Dispatch a command to the low-level driver.
600      *
601      * Arguments:   SCpnt - command block we are dispatching.
602      *
603      * Notes:
604      */
605     int scsi_dispatch_cmd(Scsi_Cmnd * SCpnt)
606     {
607     #ifdef DEBUG_DELAY
608     	unsigned long clock;
609     #endif
610     	struct Scsi_Host *host;
611     	int rtn = 0;
612     	unsigned long flags = 0;
613     	unsigned long timeout;
614     
615     	ASSERT_LOCK(&io_request_lock, 0);
616     
617     #if DEBUG
618     	unsigned long *ret = 0;
619     #ifdef __mips__
620     	__asm__ __volatile__("move\t%0,$31":"=r"(ret));
621     #else
622     	ret = __builtin_return_address(0);
623     #endif
624     #endif
625     
626     	host = SCpnt->host;
627     
628     	/* Assign a unique nonzero serial_number. */
629     	if (++serial_number == 0)
630     		serial_number = 1;
631     	SCpnt->serial_number = serial_number;
632     
633     	/*
634     	 * We will wait MIN_RESET_DELAY clock ticks after the last reset so
635     	 * we can avoid the drive not being ready.
636     	 */
637     	timeout = host->last_reset + MIN_RESET_DELAY;
638     
639     	if (host->resetting && time_before(jiffies, timeout)) {
640     		int ticks_remaining = timeout - jiffies;
641     		/*
642     		 * NOTE: This may be executed from within an interrupt
643     		 * handler!  This is bad, but for now, it'll do.  The irq
644     		 * level of the interrupt handler has been masked out by the
645     		 * platform dependent interrupt handling code already, so the
646     		 * sti() here will not cause another call to the SCSI host's
647     		 * interrupt handler (assuming there is one irq-level per
648     		 * host).
649     		 */
650     		while (--ticks_remaining >= 0)
651     			mdelay(1 + 999 / HZ);
652     		host->resetting = 0;
653     	}
654     	if (host->hostt->use_new_eh_code) {
655     		scsi_add_timer(SCpnt, SCpnt->timeout_per_command, scsi_times_out);
656     	} else {
657     		scsi_add_timer(SCpnt, SCpnt->timeout_per_command,
658     			       scsi_old_times_out);
659     	}
660     
661     	/*
662     	 * We will use a queued command if possible, otherwise we will emulate the
663     	 * queuing and calling of completion function ourselves.
664     	 */
665     	SCSI_LOG_MLQUEUE(3, printk("scsi_dispatch_cmnd (host = %d, channel = %d, target = %d, "
666     	       "command = %p, buffer = %p, \nbufflen = %d, done = %p)\n",
667     	SCpnt->host->host_no, SCpnt->channel, SCpnt->target, SCpnt->cmnd,
668     			    SCpnt->buffer, SCpnt->bufflen, SCpnt->done));
669     
670     	SCpnt->state = SCSI_STATE_QUEUED;
671     	SCpnt->owner = SCSI_OWNER_LOWLEVEL;
672     	if (host->can_queue) {
673     		SCSI_LOG_MLQUEUE(3, printk("queuecommand : routine at %p\n",
674     					   host->hostt->queuecommand));
675     		/*
676     		 * Use the old error handling code if we haven't converted the driver
677     		 * to use the new one yet.  Note - only the new queuecommand variant
678     		 * passes a meaningful return value.
679     		 */
680     		if (host->hostt->use_new_eh_code) {
681                             spin_lock_irqsave(&io_request_lock, flags);
682     			rtn = host->hostt->queuecommand(SCpnt, scsi_done);
683                             spin_unlock_irqrestore(&io_request_lock, flags);
684     			if (rtn != 0) {
685     				scsi_delete_timer(SCpnt);
686     				scsi_mlqueue_insert(SCpnt, SCSI_MLQUEUE_HOST_BUSY);
687                                     SCSI_LOG_MLQUEUE(3, printk("queuecommand : request rejected\n"));                                
688     			}
689     		} else {
690                             spin_lock_irqsave(&io_request_lock, flags);
691     			host->hostt->queuecommand(SCpnt, scsi_old_done);
692                             spin_unlock_irqrestore(&io_request_lock, flags);
693     		}
694     	} else {
695     		int temp;
696     
697     		SCSI_LOG_MLQUEUE(3, printk("command() :  routine at %p\n", host->hostt->command));
698                     spin_lock_irqsave(&io_request_lock, flags);
699     		temp = host->hostt->command(SCpnt);
700     		SCpnt->result = temp;
701     #ifdef DEBUG_DELAY
702                     spin_unlock_irqrestore(&io_request_lock, flags);
703     		clock = jiffies + 4 * HZ;
704     		while (time_before(jiffies, clock))
705     			barrier();
706     		printk("done(host = %d, result = %04x) : routine at %p\n",
707     		       host->host_no, temp, host->hostt->command);
708                     spin_lock_irqsave(&io_request_lock, flags);
709     #endif
710     		if (host->hostt->use_new_eh_code) {
711     			scsi_done(SCpnt);
712     		} else {
713     			scsi_old_done(SCpnt);
714     		}
715                     spin_unlock_irqrestore(&io_request_lock, flags);
716     	}
717     	SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
718     	return rtn;
719     }
720     
721     devfs_handle_t scsi_devfs_handle;
722     
723     /*
724      * scsi_do_cmd sends all the commands out to the low-level driver.  It
725      * handles the specifics required for each low level driver - ie queued
726      * or non queued.  It also prevents conflicts when different high level
727      * drivers go for the same host at the same time.
728      */
729     
730     void scsi_wait_req (Scsi_Request * SRpnt, const void *cmnd ,
731      		  void *buffer, unsigned bufflen, 
732      		  int timeout, int retries)
733     {
734     	DECLARE_COMPLETION(wait);
735     	
736     	SRpnt->sr_request.waiting = &wait;
737     	SRpnt->sr_request.rq_status = RQ_SCSI_BUSY;
738     	scsi_do_req (SRpnt, (void *) cmnd,
739     		buffer, bufflen, scsi_wait_done, timeout, retries);
740     	wait_for_completion(&wait);
741     	SRpnt->sr_request.waiting = NULL;
742     	if( SRpnt->sr_command != NULL )
743     	{
744     		scsi_release_command(SRpnt->sr_command);
745     		SRpnt->sr_command = NULL;
746     	}
747     
748     }
749      
750     /*
751      * Function:    scsi_do_req
752      *
753      * Purpose:     Queue a SCSI request
754      *
755      * Arguments:   SRpnt     - command descriptor.
756      *              cmnd      - actual SCSI command to be performed.
757      *              buffer    - data buffer.
758      *              bufflen   - size of data buffer.
759      *              done      - completion function to be run.
760      *              timeout   - how long to let it run before timeout.
761      *              retries   - number of retries we allow.
762      *
763      * Lock status: With the new queueing code, this is SMP-safe, and no locks
764      *              need be held upon entry.   The old queueing code the lock was
765      *              assumed to be held upon entry.
766      *
767      * Returns:     Nothing.
768      *
769      * Notes:       Prior to the new queue code, this function was not SMP-safe.
770      *              Also, this function is now only used for queueing requests
771      *              for things like ioctls and character device requests - this
772      *              is because we essentially just inject a request into the
773      *              queue for the device. Normal block device handling manipulates
774      *              the queue directly.
775      */
776     void scsi_do_req(Scsi_Request * SRpnt, const void *cmnd,
777     	      void *buffer, unsigned bufflen, void (*done) (Scsi_Cmnd *),
778     		 int timeout, int retries)
779     {
780     	Scsi_Device * SDpnt = SRpnt->sr_device;
781     	struct Scsi_Host *host = SDpnt->host;
782     
783     	ASSERT_LOCK(&io_request_lock, 0);
784     
785     	SCSI_LOG_MLQUEUE(4,
786     			 {
787     			 int i;
788     			 int target = SDpnt->id;
789     			 printk("scsi_do_req (host = %d, channel = %d target = %d, "
790     		    "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
791     				"retries = %d)\n"
792     				"command : ", host->host_no, SDpnt->channel, target, buffer,
793     				bufflen, done, timeout, retries);
794     			 for (i = 0; i < 10; ++i)
795     			 printk("%02x  ", ((unsigned char *) cmnd)[i]);
796     			 printk("\n");
797     			 });
798     
799     	if (!host) {
800     		panic("Invalid or not present host.\n");
801     	}
802     
803     	/*
804     	 * If the upper level driver is reusing these things, then
805     	 * we should release the low-level block now.  Another one will
806     	 * be allocated later when this request is getting queued.
807     	 */
808     	if( SRpnt->sr_command != NULL )
809     	{
810     		scsi_release_command(SRpnt->sr_command);
811     		SRpnt->sr_command = NULL;
812     	}
813     
814     	/*
815     	 * We must prevent reentrancy to the lowlevel host driver.  This prevents
816     	 * it - we enter a loop until the host we want to talk to is not busy.
817     	 * Race conditions are prevented, as interrupts are disabled in between the
818     	 * time we check for the host being not busy, and the time we mark it busy
819     	 * ourselves.
820     	 */
821     
822     
823     	/*
824     	 * Our own function scsi_done (which marks the host as not busy, disables
825     	 * the timeout counter, etc) will be called by us or by the
826     	 * scsi_hosts[host].queuecommand() function needs to also call
827     	 * the completion function for the high level driver.
828     	 */
829     
830     	memcpy((void *) SRpnt->sr_cmnd, (const void *) cmnd, 
831     	       sizeof(SRpnt->sr_cmnd));
832     	SRpnt->sr_bufflen = bufflen;
833     	SRpnt->sr_buffer = buffer;
834     	SRpnt->sr_allowed = retries;
835     	SRpnt->sr_done = done;
836     	SRpnt->sr_timeout_per_command = timeout;
837     
838     	if (SRpnt->sr_cmd_len == 0)
839     		SRpnt->sr_cmd_len = COMMAND_SIZE(SRpnt->sr_cmnd[0]);
840     
841     	/*
842     	 * At this point, we merely set up the command, stick it in the normal
843     	 * request queue, and return.  Eventually that request will come to the
844     	 * top of the list, and will be dispatched.
845     	 */
846     	scsi_insert_special_req(SRpnt, 0);
847     
848     	SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_do_req()\n"));
849     }
850      
851     /*
852      * Function:    scsi_init_cmd_from_req
853      *
854      * Purpose:     Queue a SCSI command
855      * Purpose:     Initialize a Scsi_Cmnd from a Scsi_Request
856      *
857      * Arguments:   SCpnt     - command descriptor.
858      *              SRpnt     - Request from the queue.
859      *
860      * Lock status: None needed.
861      *
862      * Returns:     Nothing.
863      *
864      * Notes:       Mainly transfer data from the request structure to the
865      *              command structure.  The request structure is allocated
866      *              using the normal memory allocator, and requests can pile
867      *              up to more or less any depth.  The command structure represents
868      *              a consumable resource, as these are allocated into a pool
869      *              when the SCSI subsystem initializes.  The preallocation is
870      *              required so that in low-memory situations a disk I/O request
871      *              won't cause the memory manager to try and write out a page.
872      *              The request structure is generally used by ioctls and character
873      *              devices.
874      */
875     void scsi_init_cmd_from_req(Scsi_Cmnd * SCpnt, Scsi_Request * SRpnt)
876     {
877     	struct Scsi_Host *host = SCpnt->host;
878     
879     	ASSERT_LOCK(&io_request_lock, 0);
880     
881     	SCpnt->owner = SCSI_OWNER_MIDLEVEL;
882     	SRpnt->sr_command = SCpnt;
883     
884     	if (!host) {
885     		panic("Invalid or not present host.\n");
886     	}
887     
888     	SCpnt->cmd_len = SRpnt->sr_cmd_len;
889     	SCpnt->use_sg = SRpnt->sr_use_sg;
890     
891     	memcpy((void *) &SCpnt->request, (const void *) &SRpnt->sr_request,
892     	       sizeof(SRpnt->sr_request));
893     	memcpy((void *) SCpnt->data_cmnd, (const void *) SRpnt->sr_cmnd, 
894     	       sizeof(SCpnt->data_cmnd));
895     	SCpnt->reset_chain = NULL;
896     	SCpnt->serial_number = 0;
897     	SCpnt->serial_number_at_timeout = 0;
898     	SCpnt->bufflen = SRpnt->sr_bufflen;
899     	SCpnt->buffer = SRpnt->sr_buffer;
900     	SCpnt->flags = 0;
901     	SCpnt->retries = 0;
902     	SCpnt->allowed = SRpnt->sr_allowed;
903     	SCpnt->done = SRpnt->sr_done;
904     	SCpnt->timeout_per_command = SRpnt->sr_timeout_per_command;
905     
906     	SCpnt->sc_data_direction = SRpnt->sr_data_direction;
907     
908     	SCpnt->sglist_len = SRpnt->sr_sglist_len;
909     	SCpnt->underflow = SRpnt->sr_underflow;
910     
911     	SCpnt->sc_request = SRpnt;
912     
913     	memcpy((void *) SCpnt->cmnd, (const void *) SRpnt->sr_cmnd, 
914     	       sizeof(SCpnt->cmnd));
915     	/* Zero the sense buffer.  Some host adapters automatically request
916     	 * sense on error.  0 is not a valid sense code.
917     	 */
918     	memset((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
919     	SCpnt->request_buffer = SRpnt->sr_buffer;
920     	SCpnt->request_bufflen = SRpnt->sr_bufflen;
921     	SCpnt->old_use_sg = SCpnt->use_sg;
922     	if (SCpnt->cmd_len == 0)
923     		SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
924     	SCpnt->old_cmd_len = SCpnt->cmd_len;
925     	SCpnt->sc_old_data_direction = SCpnt->sc_data_direction;
926     	SCpnt->old_underflow = SCpnt->underflow;
927     
928     	/* Start the timer ticking.  */
929     
930     	SCpnt->internal_timeout = NORMAL_TIMEOUT;
931     	SCpnt->abort_reason = 0;
932     	SCpnt->result = 0;
933     
934     	SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_init_cmd_from_req()\n"));
935     }
936     
937     /*
938      * Function:    scsi_do_cmd
939      *
940      * Purpose:     Queue a SCSI command
941      *
942      * Arguments:   SCpnt     - command descriptor.
943      *              cmnd      - actual SCSI command to be performed.
944      *              buffer    - data buffer.
945      *              bufflen   - size of data buffer.
946      *              done      - completion function to be run.
947      *              timeout   - how long to let it run before timeout.
948      *              retries   - number of retries we allow.
949      *
950      * Lock status: With the new queueing code, this is SMP-safe, and no locks
951      *              need be held upon entry.   The old queueing code the lock was
952      *              assumed to be held upon entry.
953      *
954      * Returns:     Nothing.
955      *
956      * Notes:       Prior to the new queue code, this function was not SMP-safe.
957      *              Also, this function is now only used for queueing requests
958      *              for things like ioctls and character device requests - this
959      *              is because we essentially just inject a request into the
960      *              queue for the device. Normal block device handling manipulates
961      *              the queue directly.
962      */
963     void scsi_do_cmd(Scsi_Cmnd * SCpnt, const void *cmnd,
964     	      void *buffer, unsigned bufflen, void (*done) (Scsi_Cmnd *),
965     		 int timeout, int retries)
966     {
967     	struct Scsi_Host *host = SCpnt->host;
968     
969     	ASSERT_LOCK(&io_request_lock, 0);
970     
971     	SCpnt->owner = SCSI_OWNER_MIDLEVEL;
972     
973     	SCSI_LOG_MLQUEUE(4,
974     			 {
975     			 int i;
976     			 int target = SCpnt->target;
977     			 printk("scsi_do_cmd (host = %d, channel = %d target = %d, "
978     		    "buffer =%p, bufflen = %d, done = %p, timeout = %d, "
979     				"retries = %d)\n"
980     				"command : ", host->host_no, SCpnt->channel, target, buffer,
981     				bufflen, done, timeout, retries);
982     			 for (i = 0; i < 10; ++i)
983     			 printk("%02x  ", ((unsigned char *) cmnd)[i]);
984     			 printk("\n");
985     			 });
986     
987     	if (!host) {
988     		panic("Invalid or not present host.\n");
989     	}
990     	/*
991     	 * We must prevent reentrancy to the lowlevel host driver.  This prevents
992     	 * it - we enter a loop until the host we want to talk to is not busy.
993     	 * Race conditions are prevented, as interrupts are disabled in between the
994     	 * time we check for the host being not busy, and the time we mark it busy
995     	 * ourselves.
996     	 */
997     
998     
999     	/*
1000     	 * Our own function scsi_done (which marks the host as not busy, disables
1001     	 * the timeout counter, etc) will be called by us or by the
1002     	 * scsi_hosts[host].queuecommand() function needs to also call
1003     	 * the completion function for the high level driver.
1004     	 */
1005     
1006     	memcpy((void *) SCpnt->data_cmnd, (const void *) cmnd, 
1007                    sizeof(SCpnt->data_cmnd));
1008     	SCpnt->reset_chain = NULL;
1009     	SCpnt->serial_number = 0;
1010     	SCpnt->serial_number_at_timeout = 0;
1011     	SCpnt->bufflen = bufflen;
1012     	SCpnt->buffer = buffer;
1013     	SCpnt->flags = 0;
1014     	SCpnt->retries = 0;
1015     	SCpnt->allowed = retries;
1016     	SCpnt->done = done;
1017     	SCpnt->timeout_per_command = timeout;
1018     
1019     	memcpy((void *) SCpnt->cmnd, (const void *) cmnd, 
1020                    sizeof(SCpnt->cmnd));
1021     	/* Zero the sense buffer.  Some host adapters automatically request
1022     	 * sense on error.  0 is not a valid sense code.
1023     	 */
1024     	memset((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1025     	SCpnt->request_buffer = buffer;
1026     	SCpnt->request_bufflen = bufflen;
1027     	SCpnt->old_use_sg = SCpnt->use_sg;
1028     	if (SCpnt->cmd_len == 0)
1029     		SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
1030     	SCpnt->old_cmd_len = SCpnt->cmd_len;
1031     	SCpnt->sc_old_data_direction = SCpnt->sc_data_direction;
1032     	SCpnt->old_underflow = SCpnt->underflow;
1033     
1034     	/* Start the timer ticking.  */
1035     
1036     	SCpnt->internal_timeout = NORMAL_TIMEOUT;
1037     	SCpnt->abort_reason = 0;
1038     	SCpnt->result = 0;
1039     
1040     	/*
1041     	 * At this point, we merely set up the command, stick it in the normal
1042     	 * request queue, and return.  Eventually that request will come to the
1043     	 * top of the list, and will be dispatched.
1044     	 */
1045     	scsi_insert_special_cmd(SCpnt, 0);
1046     
1047     	SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_do_cmd()\n"));
1048     }
1049     
1050     /*
1051      * This function is the mid-level interrupt routine, which decides how
1052      *  to handle error conditions.  Each invocation of this function must
1053      *  do one and *only* one of the following:
1054      *
1055      *      1) Insert command in BH queue.
1056      *      2) Activate error handler for host.
1057      *
1058      * FIXME(eric) - I am concerned about stack overflow (still).  An
1059      * interrupt could come while we are processing the bottom queue,
1060      * which would cause another command to be stuffed onto the bottom
1061      * queue, and it would in turn be processed as that interrupt handler
1062      * is returning.  Given a sufficiently steady rate of returning
1063      * commands, this could cause the stack to overflow.  I am not sure
1064      * what is the most appropriate solution here - we should probably
1065      * keep a depth count, and not process any commands while we still
1066      * have a bottom handler active higher in the stack.
1067      *
1068      * There is currently code in the bottom half handler to monitor
1069      * recursion in the bottom handler and report if it ever happens.  If
1070      * this becomes a problem, it won't be hard to engineer something to
1071      * deal with it so that only the outer layer ever does any real
1072      * processing.  
1073      */
1074     void scsi_done(Scsi_Cmnd * SCpnt)
1075     {
1076     	unsigned long flags;
1077     	int tstatus;
1078     
1079     	/*
1080     	 * We don't have to worry about this one timing out any more.
1081     	 */
1082     	tstatus = scsi_delete_timer(SCpnt);
1083     
1084     	/*
1085     	 * If we are unable to remove the timer, it means that the command
1086     	 * has already timed out.  In this case, we have no choice but to
1087     	 * let the timeout function run, as we have no idea where in fact
1088     	 * that function could really be.  It might be on another processor,
1089     	 * etc, etc.
1090     	 */
1091     	if (!tstatus) {
1092     		SCpnt->done_late = 1;
1093     		return;
1094     	}
1095     	/* Set the serial numbers back to zero */
1096     	SCpnt->serial_number = 0;
1097     
1098     	/*
1099     	 * First, see whether this command already timed out.  If so, we ignore
1100     	 * the response.  We treat it as if the command never finished.
1101     	 *
1102     	 * Since serial_number is now 0, the error handler cound detect this
1103     	 * situation and avoid to call the low level driver abort routine.
1104     	 * (DB)
1105              *
1106              * FIXME(eric) - I believe that this test is now redundant, due to
1107              * the test of the return status of del_timer().
1108     	 */
1109     	if (SCpnt->state == SCSI_STATE_TIMEOUT) {
1110     		SCSI_LOG_MLCOMPLETE(1, printk("Ignoring completion of %p due to timeout status", SCpnt));
1111     		return;
1112     	}
1113     	spin_lock_irqsave(&scsi_bhqueue_lock, flags);
1114     
1115     	SCpnt->serial_number_at_timeout = 0;
1116     	SCpnt->state = SCSI_STATE_BHQUEUE;
1117     	SCpnt->owner = SCSI_OWNER_BH_HANDLER;
1118     	SCpnt->bh_next = NULL;
1119     
1120     	/*
1121     	 * Next, put this command in the BH queue.
1122     	 * 
1123     	 * We need a spinlock here, or compare and exchange if we can reorder incoming
1124     	 * Scsi_Cmnds, as it happens pretty often scsi_done is called multiple times
1125     	 * before bh is serviced. -jj
1126     	 *
1127     	 * We already have the io_request_lock here, since we are called from the
1128     	 * interrupt handler or the error handler. (DB)
1129     	 *
1130     	 * This may be true at the moment, but I would like to wean all of the low
1131     	 * level drivers away from using io_request_lock.   Technically they should
1132     	 * all use their own locking.  I am adding a small spinlock to protect
1133     	 * this datastructure to make it safe for that day.  (ERY)
1134     	 */
1135     	if (!scsi_bh_queue_head) {
1136     		scsi_bh_queue_head = SCpnt;
1137     		scsi_bh_queue_tail = SCpnt;
1138     	} else {
1139     		scsi_bh_queue_tail->bh_next = SCpnt;
1140     		scsi_bh_queue_tail = SCpnt;
1141     	}
1142     
1143     	spin_unlock_irqrestore(&scsi_bhqueue_lock, flags);
1144     	/*
1145     	 * Mark the bottom half handler to be run.
1146     	 */
1147     	mark_bh(SCSI_BH);
1148     }
1149     
1150     /*
1151      * Procedure:   scsi_bottom_half_handler
1152      *
1153      * Purpose:     Called after we have finished processing interrupts, it
1154      *              performs post-interrupt handling for commands that may
1155      *              have completed.
1156      *
1157      * Notes:       This is called with all interrupts enabled.  This should reduce
1158      *              interrupt latency, stack depth, and reentrancy of the low-level
1159      *              drivers.
1160      *
1161      * The io_request_lock is required in all the routine. There was a subtle
1162      * race condition when scsi_done is called after a command has already
1163      * timed out but before the time out is processed by the error handler.
1164      * (DB)
1165      *
1166      * I believe I have corrected this.  We simply monitor the return status of
1167      * del_timer() - if this comes back as 0, it means that the timer has fired
1168      * and that a timeout is in progress.   I have modified scsi_done() such
1169      * that in this instance the command is never inserted in the bottom
1170      * half queue.  Thus the only time we hold the lock here is when
1171      * we wish to atomically remove the contents of the queue.
1172      */
1173     void scsi_bottom_half_handler(void)
1174     {
1175     	Scsi_Cmnd *SCpnt;
1176     	Scsi_Cmnd *SCnext;
1177     	unsigned long flags;
1178     
1179     
1180     	while (1 == 1) {
1181     		spin_lock_irqsave(&scsi_bhqueue_lock, flags);
1182     		SCpnt = scsi_bh_queue_head;
1183     		scsi_bh_queue_head = NULL;
1184     		spin_unlock_irqrestore(&scsi_bhqueue_lock, flags);
1185     
1186     		if (SCpnt == NULL) {
1187     			return;
1188     		}
1189     		SCnext = SCpnt->bh_next;
1190     
1191     		for (; SCpnt; SCpnt = SCnext) {
1192     			SCnext = SCpnt->bh_next;
1193     
1194     			switch (scsi_decide_disposition(SCpnt)) {
1195     			case SUCCESS:
1196     				/*
1197     				 * Add to BH queue.
1198     				 */
1199     				SCSI_LOG_MLCOMPLETE(3, printk("Command finished %d %d 0x%x\n", SCpnt->host->host_busy,
1200     						SCpnt->host->host_failed,
1201     							 SCpnt->result));
1202     
1203     				scsi_finish_command(SCpnt);
1204     				break;
1205     			case NEEDS_RETRY:
1206     				/*
1207     				 * We only come in here if we want to retry a command.  The
1208     				 * test to see whether the command should be retried should be
1209     				 * keeping track of the number of tries, so we don't end up looping,
1210     				 * of course.
1211     				 */
1212     				SCSI_LOG_MLCOMPLETE(3, printk("Command needs retry %d %d 0x%x\n", SCpnt->host->host_busy,
1213     				SCpnt->host->host_failed, SCpnt->result));
1214     
1215     				scsi_retry_command(SCpnt);
1216     				break;
1217     			case ADD_TO_MLQUEUE:
1218     				/* 
1219     				 * This typically happens for a QUEUE_FULL message -
1220     				 * typically only when the queue depth is only
1221     				 * approximate for a given device.  Adding a command
1222     				 * to the queue for the device will prevent further commands
1223     				 * from being sent to the device, so we shouldn't end up
1224     				 * with tons of things being sent down that shouldn't be.
1225     				 */
1226     				SCSI_LOG_MLCOMPLETE(3, printk("Command rejected as device queue full, put on ml queue %p\n",
1227                                                                   SCpnt));
1228     				scsi_mlqueue_insert(SCpnt, SCSI_MLQUEUE_DEVICE_BUSY);
1229     				break;
1230     			default:
1231     				/*
1232     				 * Here we have a fatal error of some sort.  Turn it over to
1233     				 * the error handler.
1234     				 */
1235     				SCSI_LOG_MLCOMPLETE(3, printk("Command failed %p %x active=%d busy=%d failed=%d\n",
1236     						    SCpnt, SCpnt->result,
1237     				  atomic_read(&SCpnt->host->host_active),
1238     						  SCpnt->host->host_busy,
1239     					      SCpnt->host->host_failed));
1240     
1241     				/*
1242     				 * Dump the sense information too.
1243     				 */
1244     				if ((status_byte(SCpnt->result) & CHECK_CONDITION) != 0) {
1245     					SCSI_LOG_MLCOMPLETE(3, print_sense("bh", SCpnt));
1246     				}
1247     				if (SCpnt->host->eh_wait != NULL) {
1248     					SCpnt->host->host_failed++;
1249     					SCpnt->owner = SCSI_OWNER_ERROR_HANDLER;
1250     					SCpnt->state = SCSI_STATE_FAILED;
1251     					SCpnt->host->in_recovery = 1;
1252     					/*
1253     					 * If the host is having troubles, then look to see if this was the last
1254     					 * command that might have failed.  If so, wake up the error handler.
1255     					 */
1256     					if (SCpnt->host->host_busy == SCpnt->host->host_failed) {
1257     						SCSI_LOG_ERROR_RECOVERY(5, printk("Waking error handler thread (%d)\n",
1258     										  atomic_read(&SCpnt->host->eh_wait->count)));
1259     						up(SCpnt->host->eh_wait);
1260     					}
1261     				} else {
1262     					/*
1263     					 * We only get here if the error recovery thread has died.
1264     					 */
1265     					scsi_finish_command(SCpnt);
1266     				}
1267     			}
1268     		}		/* for(; SCpnt...) */
1269     
1270     	}			/* while(1==1) */
1271     
1272     }
1273     
1274     /*
1275      * Function:    scsi_retry_command
1276      *
1277      * Purpose:     Send a command back to the low level to be retried.
1278      *
1279      * Notes:       This command is always executed in the context of the
1280      *              bottom half handler, or the error handler thread. Low
1281      *              level drivers should not become re-entrant as a result of
1282      *              this.
1283      */
1284     int scsi_retry_command(Scsi_Cmnd * SCpnt)
1285     {
1286     	memcpy((void *) SCpnt->cmnd, (void *) SCpnt->data_cmnd,
1287     	       sizeof(SCpnt->data_cmnd));
1288     	SCpnt->request_buffer = SCpnt->buffer;
1289     	SCpnt->request_bufflen = SCpnt->bufflen;
1290     	SCpnt->use_sg = SCpnt->old_use_sg;
1291     	SCpnt->cmd_len = SCpnt->old_cmd_len;
1292     	SCpnt->sc_data_direction = SCpnt->sc_old_data_direction;
1293     	SCpnt->underflow = SCpnt->old_underflow;
1294     
1295             /*
1296              * Zero the sense information from the last time we tried
1297              * this command.
1298              */
1299     	memset((void *) SCpnt->sense_buffer, 0, sizeof SCpnt->sense_buffer);
1300     
1301     	return scsi_dispatch_cmd(SCpnt);
1302     }
1303     
1304     /*
1305      * Function:    scsi_finish_command
1306      *
1307      * Purpose:     Pass command off to upper layer for finishing of I/O
1308      *              request, waking processes that are waiting on results,
1309      *              etc.
1310      */
1311     void scsi_finish_command(Scsi_Cmnd * SCpnt)
1312     {
1313     	struct Scsi_Host *host;
1314     	Scsi_Device *device;
1315     	Scsi_Request * SRpnt;
1316     	unsigned long flags;
1317     
1318     	ASSERT_LOCK(&io_request_lock, 0);
1319     
1320     	host = SCpnt->host;
1321     	device = SCpnt->device;
1322     
1323             /*
1324              * We need to protect the decrement, as otherwise a race condition
1325              * would exist.  Fiddling with SCpnt isn't a problem as the
1326              * design only allows a single SCpnt to be active in only
1327              * one execution context, but the device and host structures are
1328              * shared.
1329              */
1330     	spin_lock_irqsave(&io_request_lock, flags);
1331     	host->host_busy--;	/* Indicate that we are free */
1332     	device->device_busy--;	/* Decrement device usage counter. */
1333     	spin_unlock_irqrestore(&io_request_lock, flags);
1334     
1335             /*
1336              * Clear the flags which say that the device/host is no longer
1337              * capable of accepting new commands.  These are set in scsi_queue.c
1338              * for both the queue full condition on a device, and for a
1339              * host full condition on the host.
1340              */
1341             host->host_blocked = FALSE;
1342             device->device_blocked = FALSE;
1343     
1344     	/*
1345     	 * If we have valid sense information, then some kind of recovery
1346     	 * must have taken place.  Make a note of this.
1347     	 */
1348     	if (scsi_sense_valid(SCpnt)) {
1349     		SCpnt->result |= (DRIVER_SENSE << 24);
1350     	}
1351     	SCSI_LOG_MLCOMPLETE(3, printk("Notifying upper driver of completion for device %d %x\n",
1352     				      SCpnt->device->id, SCpnt->result));
1353     
1354     	SCpnt->owner = SCSI_OWNER_HIGHLEVEL;
1355     	SCpnt->state = SCSI_STATE_FINISHED;
1356     
1357     	/* We can get here with use_sg=0, causing a panic in the upper level (DB) */
1358     	SCpnt->use_sg = SCpnt->old_use_sg;
1359     
1360            /*
1361     	* If there is an associated request structure, copy the data over before we call the
1362     	* completion function.
1363     	*/
1364     	SRpnt = SCpnt->sc_request;
1365     	if( SRpnt != NULL ) {
1366     	       SRpnt->sr_result = SRpnt->sr_command->result;
1367     	       if( SRpnt->sr_result != 0 ) {
1368     		       memcpy(SRpnt->sr_sense_buffer,
1369     			      SRpnt->sr_command->sense_buffer,
1370     			      sizeof(SRpnt->sr_sense_buffer));
1371     	       }
1372     	}
1373     
1374     	SCpnt->done(SCpnt);
1375     }
1376     
1377     static int scsi_register_host(Scsi_Host_Template *);
1378     static int scsi_unregister_host(Scsi_Host_Template *);
1379     
1380     /*
1381      * Function:    scsi_release_commandblocks()
1382      *
1383      * Purpose:     Release command blocks associated with a device.
1384      *
1385      * Arguments:   SDpnt   - device
1386      *
1387      * Returns:     Nothing
1388      *
1389      * Lock status: No locking assumed or required.
1390      *
1391      * Notes:
1392      */
1393     void scsi_release_commandblocks(Scsi_Device * SDpnt)
1394     {
1395     	Scsi_Cmnd *SCpnt, *SCnext;
1396     	unsigned long flags;
1397     
1398      	spin_lock_irqsave(&device_request_lock, flags);
1399     	for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCnext) {
1400     		SDpnt->device_queue = SCnext = SCpnt->next;
1401     		kfree((char *) SCpnt);
1402     	}
1403     	SDpnt->has_cmdblocks = 0;
1404     	SDpnt->queue_depth = 0;
1405     	spin_unlock_irqrestore(&device_request_lock, flags);
1406     }
1407     
1408     /*
1409      * Function:    scsi_build_commandblocks()
1410      *
1411      * Purpose:     Allocate command blocks associated with a device.
1412      *
1413      * Arguments:   SDpnt   - device
1414      *
1415      * Returns:     Nothing
1416      *
1417      * Lock status: No locking assumed or required.
1418      *
1419      * Notes:
1420      */
1421     void scsi_build_commandblocks(Scsi_Device * SDpnt)
1422     {
1423     	unsigned long flags;
1424     	struct Scsi_Host *host = SDpnt->host;
1425     	int j;
1426     	Scsi_Cmnd *SCpnt;
1427     
1428     	spin_lock_irqsave(&device_request_lock, flags);
1429     
1430     	if (SDpnt->queue_depth == 0)
1431     	{
1432     		SDpnt->queue_depth = host->cmd_per_lun;
1433     		if (SDpnt->queue_depth == 0)
1434     			SDpnt->queue_depth = 1; /* live to fight another day */
1435     	}
1436     	SDpnt->device_queue = NULL;
1437     
1438     	for (j = 0; j < SDpnt->queue_depth; j++) {
1439     		SCpnt = (Scsi_Cmnd *)
1440     		    kmalloc(sizeof(Scsi_Cmnd),
1441     				     GFP_ATOMIC |
1442     				(host->unchecked_isa_dma ? GFP_DMA : 0));
1443     		if (NULL == SCpnt)
1444     			break;	/* If not, the next line will oops ... */
1445     		memset(SCpnt, 0, sizeof(Scsi_Cmnd));
1446     		SCpnt->host = host;
1447     		SCpnt->device = SDpnt;
1448     		SCpnt->target = SDpnt->id;
1449     		SCpnt->lun = SDpnt->lun;
1450     		SCpnt->channel = SDpnt->channel;
1451     		SCpnt->request.rq_status = RQ_INACTIVE;
1452     		SCpnt->use_sg = 0;
1453     		SCpnt->old_use_sg = 0;
1454     		SCpnt->old_cmd_len = 0;
1455     		SCpnt->underflow = 0;
1456     		SCpnt->old_underflow = 0;
1457     		SCpnt->transfersize = 0;
1458     		SCpnt->resid = 0;
1459     		SCpnt->serial_number = 0;
1460     		SCpnt->serial_number_at_timeout = 0;
1461     		SCpnt->host_scribble = NULL;
1462     		SCpnt->next = SDpnt->device_queue;
1463     		SDpnt->device_queue = SCpnt;
1464     		SCpnt->state = SCSI_STATE_UNUSED;
1465     		SCpnt->owner = SCSI_OWNER_NOBODY;
1466     	}
1467     	if (j < SDpnt->queue_depth) {	/* low on space (D.Gilbert 990424) */
1468     		printk(KERN_WARNING "scsi_build_commandblocks: want=%d, space for=%d blocks\n",
1469     		       SDpnt->queue_depth, j);
1470     		SDpnt->queue_depth = j;
1471     		SDpnt->has_cmdblocks = (0 != j);
1472     	} else {
1473     		SDpnt->has_cmdblocks = 1;
1474     	}
1475     	spin_unlock_irqrestore(&device_request_lock, flags);
1476     }
1477     
1478     static int proc_scsi_gen_write(struct file * file, const char * buf,
1479                                   unsigned long length, void *data);
1480     
1481     void __init scsi_host_no_insert(char *str, int n)
1482     {
1483         Scsi_Host_Name *shn, *shn2;
1484         int len;
1485         
1486         len = strlen(str);
1487         if (len && (shn = (Scsi_Host_Name *) kmalloc(sizeof(Scsi_Host_Name), GFP_ATOMIC))) {
1488     	if ((shn->name = kmalloc(len+1, GFP_ATOMIC))) {
1489     	    strncpy(shn->name, str, len);
1490     	    shn->name[len] = 0;
1491     	    shn->host_no = n;
1492     	    shn->host_registered = 0;
1493     	    shn->loaded_as_module = 1; /* numbers shouldn't be freed in any case */
1494     	    shn->next = NULL;
1495     	    if (scsi_host_no_list) {
1496     		for (shn2 = scsi_host_no_list;shn2->next;shn2 = shn2->next)
1497     		    ;
1498     		shn2->next = shn;
1499     	    }
1500     	    else
1501     		scsi_host_no_list = shn;
1502     	    max_scsi_hosts = n+1;
1503     	}
1504     	else
1505     	    kfree((char *) shn);
1506         }
1507     }
1508     
1509     #ifdef CONFIG_PROC_FS
1510     static int scsi_proc_info(char *buffer, char **start, off_t offset, int length)
1511     {
1512     	Scsi_Device *scd;
1513     	struct Scsi_Host *HBA_ptr;
1514     	int size, len = 0;
1515     	off_t begin = 0;
1516     	off_t pos = 0;
1517     
1518     	/*
1519     	 * First, see if there are any attached devices or not.
1520     	 */
1521     	for (HBA_ptr = scsi_hostlist; HBA_ptr; HBA_ptr = HBA_ptr->next) {
1522     		if (HBA_ptr->host_queue != NULL) {
1523     			break;
1524     		}
1525     	}
1526     	size = sprintf(buffer + len, "Attached devices: %s\n", (HBA_ptr) ? "" : "none");
1527     	len += size;
1528     	pos = begin + len;
1529     	for (HBA_ptr = scsi_hostlist; HBA_ptr; HBA_ptr = HBA_ptr->next) {
1530     #if 0
1531     		size += sprintf(buffer + len, "scsi%2d: %s\n", (int) HBA_ptr->host_no,
1532     				HBA_ptr->hostt->procname);
1533     		len += size;
1534     		pos = begin + len;
1535     #endif
1536     		for (scd = HBA_ptr->host_queue; scd; scd = scd->next) {
1537     			proc_print_scsidevice(scd, buffer, &size, len);
1538     			len += size;
1539     			pos = begin + len;
1540     
1541     			if (pos < offset) {
1542     				len = 0;
1543     				begin = pos;
1544     			}
1545     			if (pos > offset + length)
1546     				goto stop_output;
1547     		}
1548     	}
1549     
1550     stop_output:
1551     	*start = buffer + (offset - begin);	/* Start of wanted data */
1552     	len -= (offset - begin);	/* Start slop */
1553     	if (len > length)
1554     		len = length;	/* Ending slop */
1555     	return (len);
1556     }
1557     
1558     static int proc_scsi_gen_write(struct file * file, const char * buf,
1559                                   unsigned long length, void *data)
1560     {
1561     	struct Scsi_Device_Template *SDTpnt;
1562     	Scsi_Device *scd;
1563     	struct Scsi_Host *HBA_ptr;
1564     	char *p;
1565     	int host, channel, id, lun;
1566     	char * buffer;
1567     	int err;
1568     
1569     	if (!buf || length>PAGE_SIZE)
1570     		return -EINVAL;
1571     
1572     	if (!(buffer = (char *) __get_free_page(GFP_KERNEL)))
1573     		return -ENOMEM;
1574     	if(copy_from_user(buffer, buf, length))
1575     	{
1576     		err =-EFAULT;
1577     		goto out;
1578     	}
1579     
1580     	err = -EINVAL;
1581     
1582     	if (length < PAGE_SIZE)
1583     		buffer[length] = '\0';
1584     	else if (buffer[PAGE_SIZE-1])
1585     		goto out;
1586     
1587     	if (length < 11 || strncmp("scsi", buffer, 4))
1588     		goto out;
1589     
1590     	/*
1591     	 * Usage: echo "scsi dump #N" > /proc/scsi/scsi
1592     	 * to dump status of all scsi commands.  The number is used to specify the level
1593     	 * of detail in the dump.
1594     	 */
1595     	if (!strncmp("dump", buffer + 5, 4)) {
1596     		unsigned int level;
1597     
1598     		p = buffer + 10;
1599     
1600     		if (*p == '\0')
1601     			goto out;
1602     
1603     		level = simple_strtoul(p, NULL, 0);
1604     		scsi_dump_status(level);
1605     	}
1606     	/*
1607     	 * Usage: echo "scsi log token #N" > /proc/scsi/scsi
1608     	 * where token is one of [error,scan,mlqueue,mlcomplete,llqueue,
1609     	 * llcomplete,hlqueue,hlcomplete]
1610     	 */
1611     #ifdef CONFIG_SCSI_LOGGING		/* { */
1612     
1613     	if (!strncmp("log", buffer + 5, 3)) {
1614     		char *token;
1615     		unsigned int level;
1616     
1617     		p = buffer + 9;
1618     		token = p;
1619     		while (*p != ' ' && *p != '\t' && *p != '\0') {
1620     			p++;
1621     		}
1622     
1623     		if (*p == '\0') {
1624     			if (strncmp(token, "all", 3) == 0) {
1625     				/*
1626     				 * Turn on absolutely everything.
1627     				 */
1628     				scsi_logging_level = ~0;
1629     			} else if (strncmp(token, "none", 4) == 0) {
1630     				/*
1631     				 * Turn off absolutely everything.
1632     				 */
1633     				scsi_logging_level = 0;
1634     			} else {
1635     				goto out;
1636     			}
1637     		} else {
1638     			*p++ = '\0';
1639     
1640     			level = simple_strtoul(p, NULL, 0);
1641     
1642     			/*
1643     			 * Now figure out what to do with it.
1644     			 */
1645     			if (strcmp(token, "error") == 0) {
1646     				SCSI_SET_ERROR_RECOVERY_LOGGING(level);
1647     			} else if (strcmp(token, "timeout") == 0) {
1648     				SCSI_SET_TIMEOUT_LOGGING(level);
1649     			} else if (strcmp(token, "scan") == 0) {
1650     				SCSI_SET_SCAN_BUS_LOGGING(level);
1651     			} else if (strcmp(token, "mlqueue") == 0) {
1652     				SCSI_SET_MLQUEUE_LOGGING(level);
1653     			} else if (strcmp(token, "mlcomplete") == 0) {
1654     				SCSI_SET_MLCOMPLETE_LOGGING(level);
1655     			} else if (strcmp(token, "llqueue") == 0) {
1656     				SCSI_SET_LLQUEUE_LOGGING(level);
1657     			} else if (strcmp(token, "llcomplete") == 0) {
1658     				SCSI_SET_LLCOMPLETE_LOGGING(level);
1659     			} else if (strcmp(token, "hlqueue") == 0) {
1660     				SCSI_SET_HLQUEUE_LOGGING(level);
1661     			} else if (strcmp(token, "hlcomplete") == 0) {
1662     				SCSI_SET_HLCOMPLETE_LOGGING(level);
1663     			} else if (strcmp(token, "ioctl") == 0) {
1664     				SCSI_SET_IOCTL_LOGGING(level);
1665     			} else {
1666     				goto out;
1667     			}
1668     		}
1669     
1670     		printk(KERN_INFO "scsi logging level set to 0x%8.8x\n", scsi_logging_level);
1671     	}
1672     #endif	/* CONFIG_SCSI_LOGGING */ /* } */
1673     
1674     	/*
1675     	 * Usage: echo "scsi add-single-device 0 1 2 3" >/proc/scsi/scsi
1676     	 * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
1677     	 * Consider this feature BETA.
1678     	 *     CAUTION: This is not for hotplugging your peripherals. As
1679     	 *     SCSI was not designed for this you could damage your
1680     	 *     hardware !
1681     	 * However perhaps it is legal to switch on an
1682     	 * already connected device. It is perhaps not
1683     	 * guaranteed this device doesn't corrupt an ongoing data transfer.
1684     	 */
1685     	if (!strncmp("add-single-device", buffer + 5, 17)) {
1686     		p = buffer + 23;
1687     
1688     		host = simple_strtoul(p, &p, 0);
1689     		channel = simple_strtoul(p + 1, &p, 0);
1690     		id = simple_strtoul(p + 1, &p, 0);
1691     		lun = simple_strtoul(p + 1, &p, 0);
1692     
1693     		printk(KERN_INFO "scsi singledevice %d %d %d %d\n", host, channel,
1694     		       id, lun);
1695     
1696     		for (HBA_ptr = scsi_hostlist; HBA_ptr; HBA_ptr = HBA_ptr->next) {
1697     			if (HBA_ptr->host_no == host) {
1698     				break;
1699     			}
1700     		}
1701     		err = -ENXIO;
1702     		if (!HBA_ptr)
1703     			goto out;
1704     
1705     		for (scd = HBA_ptr->host_queue; scd; scd = scd->next) {
1706     			if ((scd->channel == channel
1707     			     && scd->id == id
1708     			     && scd->lun == lun)) {
1709     				break;
1710     			}
1711     		}
1712     
1713     		err = -ENOSYS;
1714     		if (scd)
1715     			goto out;	/* We do not yet support unplugging */
1716     
1717     		scan_scsis(HBA_ptr, 1, channel, id, lun);
1718     
1719     		/* FIXME (DB) This assumes that the queue_depth routines can be used
1720     		   in this context as well, while they were all designed to be
1721     		   called only once after the detect routine. (DB) */
1722     		/* queue_depth routine moved to inside scan_scsis(,1,,,) so
1723     		   it is called before build_commandblocks() */
1724     
1725     		err = length;
1726     		goto out;
1727     	}
1728     	/*
1729     	 * Usage: echo "scsi remove-single-device 0 1 2 3" >/proc/scsi/scsi
1730     	 * with  "0 1 2 3" replaced by your "Host Channel Id Lun".
1731     	 *
1732     	 * Consider this feature pre-BETA.
1733     	 *
1734     	 *     CAUTION: This is not for hotplugging your peripherals. As
1735     	 *     SCSI was not designed for this you could damage your
1736     	 *     hardware and thoroughly confuse the SCSI subsystem.
1737     	 *
1738     	 */
1739     	else if (!strncmp("remove-single-device", buffer + 5, 20)) {
1740     		p = buffer + 26;
1741     
1742     		host = simple_strtoul(p, &p, 0);
1743     		channel = simple_strtoul(p + 1, &p, 0);
1744     		id = simple_strtoul(p + 1, &p, 0);
1745     		lun = simple_strtoul(p + 1, &p, 0);
1746     
1747     
1748     		for (HBA_ptr = scsi_hostlist; HBA_ptr; HBA_ptr = HBA_ptr->next) {
1749     			if (HBA_ptr->host_no == host) {
1750     				break;
1751     			}
1752     		}
1753     		err = -ENODEV;
1754     		if (!HBA_ptr)
1755     			goto out;
1756     
1757     		for (scd = HBA_ptr->host_queue; scd; scd = scd->next) {
1758     			if ((scd->channel == channel
1759     			     && scd->id == id
1760     			     && scd->lun == lun)) {
1761     				break;
1762     			}
1763     		}
1764     
1765     		if (scd == NULL)
1766     			goto out;	/* there is no such device attached */
1767     
1768     		err = -EBUSY;
1769     		if (scd->access_count)
1770     			goto out;
1771     
1772     		SDTpnt = scsi_devicelist;
1773     		while (SDTpnt != NULL) {
1774     			if (SDTpnt->detach)
1775     				(*SDTpnt->detach) (scd);
1776     			SDTpnt = SDTpnt->next;
1777     		}
1778     
1779     		if (scd->attached == 0) {
1780     			/*
1781     			 * Nobody is using this device any more.
1782     			 * Free all of the command structures.
1783     			 */
1784                             if (HBA_ptr->hostt->revoke)
1785                                     HBA_ptr->hostt->revoke(scd);
1786     			devfs_unregister (scd->de);
1787     			scsi_release_commandblocks(scd);
1788     
1789     			/* Now we can remove the device structure */
1790     			if (scd->next != NULL)
1791     				scd->next->prev = scd->prev;
1792     
1793     			if (scd->prev != NULL)
1794     				scd->prev->next = scd->next;
1795     
1796     			if (HBA_ptr->host_queue == scd) {
1797     				HBA_ptr->host_queue = scd->next;
1798     			}
1799     			blk_cleanup_queue(&scd->request_queue);
1800     			kfree((char *) scd);
1801     		} else {
1802     			goto out;
1803     		}
1804     		err = 0;
1805     	}
1806     out:
1807     	
1808     	free_page((unsigned long) buffer);
1809     	return err;
1810     }
1811     #endif
1812     
1813     /*
1814      * This entry point should be called by a driver if it is trying
1815      * to add a low level scsi driver to the system.
1816      */
1817     static int scsi_register_host(Scsi_Host_Template * tpnt)
1818     {
1819     	int pcount;
1820     	struct Scsi_Host *shpnt;
1821     	Scsi_Device *SDpnt;
1822     	struct Scsi_Device_Template *sdtpnt;
1823     	const char *name;
1824     	unsigned long flags;
1825     	int out_of_space = 0;
1826     
1827     	if (tpnt->next || !tpnt->detect)
1828     		return 1;	/* Must be already loaded, or
1829     				 * no detect routine available
1830     				 */
1831     
1832     	/* If max_sectors isn't set, default to max */
1833     	if (!tpnt->max_sectors)
1834     		tpnt->max_sectors = MAX_SECTORS;
1835     
1836     	pcount = next_scsi_host;
1837     
1838     	/* The detect routine must carefully spinunlock/spinlock if 
1839     	   it enables interrupts, since all interrupt handlers do 
1840     	   spinlock as well.
1841     	   All lame drivers are going to fail due to the following 
1842     	   spinlock. For the time beeing let's use it only for drivers 
1843     	   using the new scsi code. NOTE: the detect routine could
1844     	   redefine the value tpnt->use_new_eh_code. (DB, 13 May 1998) */
1845     
1846     	if (tpnt->use_new_eh_code) {
1847     		spin_lock_irqsave(&io_request_lock, flags);
1848     		tpnt->present = tpnt->detect(tpnt);
1849     		spin_unlock_irqrestore(&io_request_lock, flags);
1850     	} else
1851     		tpnt->present = tpnt->detect(tpnt);
1852     
1853     	if (tpnt->present) {
1854     		if (pcount == next_scsi_host) {
1855     			if (tpnt->present > 1) {
1856     				printk(KERN_ERR "scsi: Failure to register low-level scsi driver");
1857     				scsi_unregister_host(tpnt);
1858     				return 1;
1859     			}
1860     			/* 
1861     			 * The low-level driver failed to register a driver.
1862     			 * We can do this now.
1863     			 */
1864     			if(scsi_register(tpnt, 0)==NULL)
1865     			{
1866     				printk(KERN_ERR "scsi: register failed.\n");
1867     				scsi_unregister_host(tpnt);
1868     				return 1;
1869     			}
1870     		}
1871     		tpnt->next = scsi_hosts;	/* Add to the linked list */
1872     		scsi_hosts = tpnt;
1873     
1874     		/* Add the new driver to /proc/scsi */
1875     #ifdef CONFIG_PROC_FS
1876     		build_proc_dir_entries(tpnt);
1877     #endif
1878     
1879     
1880     		/*
1881     		 * Add the kernel threads for each host adapter that will
1882     		 * handle error correction.
1883     		 */
1884     		for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
1885     			if (shpnt->hostt == tpnt && shpnt->hostt->use_new_eh_code) {
1886     				DECLARE_MUTEX_LOCKED(sem);
1887     
1888     				shpnt->eh_notify = &sem;
1889     				kernel_thread((int (*)(void *)) scsi_error_handler,
1890     					      (void *) shpnt, 0);
1891     
1892     				/*
1893     				 * Now wait for the kernel error thread to initialize itself
1894     				 * as it might be needed when we scan the bus.
1895     				 */
1896     				down(&sem);
1897     				shpnt->eh_notify = NULL;
1898     			}
1899     		}
1900     
1901     		for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
1902     			if (shpnt->hostt == tpnt) {
1903     				if (tpnt->info) {
1904     					name = tpnt->info(shpnt);
1905     				} else {
1906     					name = tpnt->name;
1907     				}
1908     				printk(KERN_INFO "scsi%d : %s\n",		/* And print a little message */
1909     				       shpnt->host_no, name);
1910     			}
1911     		}
1912     
1913     		/* The next step is to call scan_scsis here.  This generates the
1914     		 * Scsi_Devices entries
1915     		 */
1916     		for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
1917     			if (shpnt->hostt == tpnt) {
1918     				scan_scsis(shpnt, 0, 0, 0, 0);
1919     				if (shpnt->select_queue_depths != NULL) {
1920     					(shpnt->select_queue_depths) (shpnt, shpnt->host_queue);
1921     				}
1922     			}
1923     		}
1924     
1925     		for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next) {
1926     			if (sdtpnt->init && sdtpnt->dev_noticed)
1927     				(*sdtpnt->init) ();
1928     		}
1929     
1930     		/*
1931     		 * Next we create the Scsi_Cmnd structures for this host 
1932     		 */
1933     		for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
1934     			for (SDpnt = shpnt->host_queue; SDpnt; SDpnt = SDpnt->next)
1935     				if (SDpnt->host->hostt == tpnt) {
1936     					for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
1937     						if (sdtpnt->attach)
1938     							(*sdtpnt->attach) (SDpnt);
1939     					if (SDpnt->attached) {
1940     						scsi_build_commandblocks(SDpnt);
1941     						if (0 == SDpnt->has_cmdblocks)
1942     							out_of_space = 1;
1943     					}
1944     				}
1945     		}
1946     
1947     		/*
1948     		 * Now that we have all of the devices, resize the DMA pool,
1949     		 * as required.  */
1950     		if (!out_of_space)
1951     			scsi_resize_dma_pool();
1952     
1953     
1954     		/* This does any final handling that is required. */
1955     		for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next) {
1956     			if (sdtpnt->finish && sdtpnt->nr_dev) {
1957     				(*sdtpnt->finish) ();
1958     			}
1959     		}
1960     	}
1961     #if defined(USE_STATIC_SCSI_MEMORY)
1962     	printk("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
1963     	       (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
1964     	       (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
1965     	       (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
1966     #endif
1967     
1968     	MOD_INC_USE_COUNT;
1969     
1970     	if (out_of_space) {
1971     		scsi_unregister_host(tpnt);	/* easiest way to clean up?? */
1972     		return 1;
1973     	} else
1974     		return 0;
1975     }
1976     
1977     /*
1978      * Similarly, this entry point should be called by a loadable module if it
1979      * is trying to remove a low level scsi driver from the system.
1980      */
1981     static int scsi_unregister_host(Scsi_Host_Template * tpnt)
1982     {
1983     	int online_status;
1984     	int pcount0, pcount;
1985     	Scsi_Cmnd *SCpnt;
1986     	Scsi_Device *SDpnt;
1987     	Scsi_Device *SDpnt1;
1988     	struct Scsi_Device_Template *sdtpnt;
1989     	struct Scsi_Host *sh1;
1990     	struct Scsi_Host *shpnt;
1991     	char name[10];	/* host_no>=10^9? I don't think so. */
1992     
1993     	/* get the big kernel lock, so we don't race with open() */
1994     	lock_kernel();
1995     
1996     	/*
1997     	 * First verify that this host adapter is completely free with no pending
1998     	 * commands 
1999     	 */
2000     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2001     		for (SDpnt = shpnt->host_queue; SDpnt;
2002     		     SDpnt = SDpnt->next) {
2003     			if (SDpnt->host->hostt == tpnt
2004     			    && SDpnt->host->hostt->module
2005     			    && GET_USE_COUNT(SDpnt->host->hostt->module))
2006     				goto err_out;
2007     			/* 
2008     			 * FIXME(eric) - We need to find a way to notify the
2009     			 * low level driver that we are shutting down - via the
2010     			 * special device entry that still needs to get added. 
2011     			 *
2012     			 * Is detach interface below good enough for this?
2013     			 */
2014     		}
2015     	}
2016     
2017     	/*
2018     	 * FIXME(eric) put a spinlock on this.  We force all of the devices offline
2019     	 * to help prevent race conditions where other hosts/processors could try and
2020     	 * get in and queue a command.
2021     	 */
2022     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2023     		for (SDpnt = shpnt->host_queue; SDpnt;
2024     		     SDpnt = SDpnt->next) {
2025     			if (SDpnt->host->hostt == tpnt)
2026     				SDpnt->online = FALSE;
2027     
2028     		}
2029     	}
2030     
2031     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2032     		if (shpnt->hostt != tpnt) {
2033     			continue;
2034     		}
2035     		for (SDpnt = shpnt->host_queue; SDpnt;
2036     		     SDpnt = SDpnt->next) {
2037     			/*
2038     			 * Loop over all of the commands associated with the device.  If any of
2039     			 * them are busy, then set the state back to inactive and bail.
2040     			 */
2041     			for (SCpnt = SDpnt->device_queue; SCpnt;
2042     			     SCpnt = SCpnt->next) {
2043     				online_status = SDpnt->online;
2044     				SDpnt->online = FALSE;
2045     				if (SCpnt->request.rq_status != RQ_INACTIVE) {
2046     					printk(KERN_ERR "SCSI device not inactive - rq_status=%d, target=%d, pid=%ld, state=%d, owner=%d.\n",
2047     					       SCpnt->request.rq_status, SCpnt->target, SCpnt->pid,
2048     					     SCpnt->state, SCpnt->owner);
2049     					for (SDpnt1 = shpnt->host_queue; SDpnt1;
2050     					     SDpnt1 = SDpnt1->next) {
2051     						for (SCpnt = SDpnt1->device_queue; SCpnt;
2052     						     SCpnt = SCpnt->next)
2053     							if (SCpnt->request.rq_status == RQ_SCSI_DISCONNECTING)
2054     								SCpnt->request.rq_status = RQ_INACTIVE;
2055     					}
2056     					SDpnt->online = online_status;
2057     					printk(KERN_ERR "Device busy???\n");
2058     					goto err_out;
2059     				}
2060     				/*
2061     				 * No, this device is really free.  Mark it as such, and
2062     				 * continue on.
2063     				 */
2064     				SCpnt->state = SCSI_STATE_DISCONNECTING;
2065     				SCpnt->request.rq_status = RQ_SCSI_DISCONNECTING;	/* Mark as busy */
2066     			}
2067     		}
2068     	}
2069     	/* Next we detach the high level drivers from the Scsi_Device structures */
2070     
2071     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2072     		if (shpnt->hostt != tpnt) {
2073     			continue;
2074     		}
2075     		for (SDpnt = shpnt->host_queue; SDpnt;
2076     		     SDpnt = SDpnt->next) {
2077     			for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
2078     				if (sdtpnt->detach)
2079     					(*sdtpnt->detach) (SDpnt);
2080     
2081     			/* If something still attached, punt */
2082     			if (SDpnt->attached) {
2083     				printk(KERN_ERR "Attached usage count = %d\n", SDpnt->attached);
2084     				goto err_out;
2085     			}
2086     			devfs_unregister (SDpnt->de);
2087     		}
2088     	}
2089     
2090     	/*
2091     	 * Next, kill the kernel error recovery thread for this host.
2092     	 */
2093     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2094     		if (shpnt->hostt == tpnt
2095     		    && shpnt->hostt->use_new_eh_code
2096     		    && shpnt->ehandler != NULL) {
2097     			DECLARE_MUTEX_LOCKED(sem);
2098     
2099     			shpnt->eh_notify = &sem;
2100     			send_sig(SIGHUP, shpnt->ehandler, 1);
2101     			down(&sem);
2102     			shpnt->eh_notify = NULL;
2103     		}
2104     	}
2105     
2106     	/* Next we free up the Scsi_Cmnd structures for this host */
2107     
2108     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2109     		if (shpnt->hostt != tpnt) {
2110     			continue;
2111     		}
2112     		for (SDpnt = shpnt->host_queue; SDpnt;
2113     		     SDpnt = shpnt->host_queue) {
2114     			scsi_release_commandblocks(SDpnt);
2115     
2116     			blk_cleanup_queue(&SDpnt->request_queue);
2117     			/* Next free up the Scsi_Device structures for this host */
2118     			shpnt->host_queue = SDpnt->next;
2119     			kfree((char *) SDpnt);
2120     
2121     		}
2122     	}
2123     
2124     	/* Next we go through and remove the instances of the individual hosts
2125     	 * that were detected */
2126     
2127     	pcount0 = next_scsi_host;
2128     	for (shpnt = scsi_hostlist; shpnt; shpnt = sh1) {
2129     		sh1 = shpnt->next;
2130     		if (shpnt->hostt != tpnt)
2131     			continue;
2132     		pcount = next_scsi_host;
2133     		/* Remove the /proc/scsi directory entry */
2134     		sprintf(name,"%d",shpnt->host_no);
2135     		remove_proc_entry(name, tpnt->proc_dir);
2136     		if (tpnt->release)
2137     			(*tpnt->release) (shpnt);
2138     		else {
2139     			/* This is the default case for the release function.
2140     			 * It should do the right thing for most correctly
2141     			 * written host adapters.
2142     			 */
2143     			if (shpnt->irq)
2144     				free_irq(shpnt->irq, NULL);
2145     			if (shpnt->dma_channel != 0xff)
2146     				free_dma(shpnt->dma_channel);
2147     			if (shpnt->io_port && shpnt->n_io_port)
2148     				release_region(shpnt->io_port, shpnt->n_io_port);
2149     		}
2150     		if (pcount == next_scsi_host)
2151     			scsi_unregister(shpnt);
2152     		tpnt->present--;
2153     	}
2154     
2155     	/*
2156     	 * If there are absolutely no more hosts left, it is safe
2157     	 * to completely nuke the DMA pool.  The resize operation will
2158     	 * do the right thing and free everything.
2159     	 */
2160     	if (!scsi_hosts)
2161     		scsi_resize_dma_pool();
2162     
2163     	if (pcount0 != next_scsi_host)
2164     		printk(KERN_INFO "scsi : %d host%s left.\n", next_scsi_host,
2165     		       (next_scsi_host == 1) ? "" : "s");
2166     
2167     #if defined(USE_STATIC_SCSI_MEMORY)
2168     	printk("SCSI memory: total %ldKb, used %ldKb, free %ldKb.\n",
2169     	       (scsi_memory_upper_value - scsi_memory_lower_value) / 1024,
2170     	       (scsi_init_memory_start - scsi_memory_lower_value) / 1024,
2171     	       (scsi_memory_upper_value - scsi_init_memory_start) / 1024);
2172     #endif
2173     
2174     	/*
2175     	 * Remove it from the linked list and /proc if all
2176     	 * hosts were successfully removed (ie preset == 0)
2177     	 */
2178     	if (!tpnt->present) {
2179     		Scsi_Host_Template **SHTp = &scsi_hosts;
2180     		Scsi_Host_Template *SHT;
2181     
2182     		while ((SHT = *SHTp) != NULL) {
2183     			if (SHT == tpnt) {
2184     				*SHTp = SHT->next;
2185     				remove_proc_entry(tpnt->proc_name, proc_scsi);
2186     				break;
2187     			}
2188     			SHTp = &SHT->next;
2189     		}
2190     	}
2191     	MOD_DEC_USE_COUNT;
2192     
2193     	unlock_kernel();
2194     	return 0;
2195     
2196     err_out:
2197     	unlock_kernel();
2198     	return -1;
2199     }
2200     
2201     static int scsi_unregister_device(struct Scsi_Device_Template *tpnt);
2202     
2203     /*
2204      * This entry point should be called by a loadable module if it is trying
2205      * add a high level scsi driver to the system.
2206      */
2207     static int scsi_register_device_module(struct Scsi_Device_Template *tpnt)
2208     {
2209     	Scsi_Device *SDpnt;
2210     	struct Scsi_Host *shpnt;
2211     	int out_of_space = 0;
2212     
2213     	if (tpnt->next)
2214     		return 1;
2215     
2216     	scsi_register_device(tpnt);
2217     	/*
2218     	 * First scan the devices that we know about, and see if we notice them.
2219     	 */
2220     
2221     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2222     		for (SDpnt = shpnt->host_queue; SDpnt;
2223     		     SDpnt = SDpnt->next) {
2224     			if (tpnt->detect)
2225     				SDpnt->attached += (*tpnt->detect) (SDpnt);
2226     		}
2227     	}
2228     
2229     	/*
2230     	 * If any of the devices would match this driver, then perform the
2231     	 * init function.
2232     	 */
2233     	if (tpnt->init && tpnt->dev_noticed)
2234     		if ((*tpnt->init) ())
2235     			return 1;
2236     
2237     	/*
2238     	 * Now actually connect the devices to the new driver.
2239     	 */
2240     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2241     		for (SDpnt = shpnt->host_queue; SDpnt;
2242     		     SDpnt = SDpnt->next) {
2243     			if (tpnt->attach)
2244     				(*tpnt->attach) (SDpnt);
2245     			/*
2246     			 * If this driver attached to the device, and don't have any
2247     			 * command blocks for this device, allocate some.
2248     			 */
2249     			if (SDpnt->attached && SDpnt->has_cmdblocks == 0) {
2250     				SDpnt->online = TRUE;
2251     				scsi_build_commandblocks(SDpnt);
2252     				if (0 == SDpnt->has_cmdblocks)
2253     					out_of_space = 1;
2254     			}
2255     		}
2256     	}
2257     
2258     	/*
2259     	 * This does any final handling that is required.
2260     	 */
2261     	if (tpnt->finish && tpnt->nr_dev)
2262     		(*tpnt->finish) ();
2263     	if (!out_of_space)
2264     		scsi_resize_dma_pool();
2265     	MOD_INC_USE_COUNT;
2266     
2267     	if (out_of_space) {
2268     		scsi_unregister_device(tpnt);	/* easiest way to clean up?? */
2269     		return 1;
2270     	} else
2271     		return 0;
2272     }
2273     
2274     static int scsi_unregister_device(struct Scsi_Device_Template *tpnt)
2275     {
2276     	Scsi_Device *SDpnt;
2277     	struct Scsi_Host *shpnt;
2278     	struct Scsi_Device_Template *spnt;
2279     	struct Scsi_Device_Template *prev_spnt;
2280     	
2281     	lock_kernel();
2282     	/*
2283     	 * If we are busy, this is not going to fly.
2284     	 */
2285     	if (GET_USE_COUNT(tpnt->module) != 0)
2286     		goto error_out;
2287     
2288     	/*
2289     	 * Next, detach the devices from the driver.
2290     	 */
2291     
2292     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2293     		for (SDpnt = shpnt->host_queue; SDpnt;
2294     		     SDpnt = SDpnt->next) {
2295     			if (tpnt->detach)
2296     				(*tpnt->detach) (SDpnt);
2297     			if (SDpnt->attached == 0) {
2298     				SDpnt->online = FALSE;
2299     
2300     				/*
2301     				 * Nobody is using this device any more.  Free all of the
2302     				 * command structures.
2303     				 */
2304     				scsi_release_commandblocks(SDpnt);
2305     			}
2306     		}
2307     	}
2308     	/*
2309     	 * Extract the template from the linked list.
2310     	 */
2311     	spnt = scsi_devicelist;
2312     	prev_spnt = NULL;
2313     	while (spnt != tpnt) {
2314     		prev_spnt = spnt;
2315     		spnt = spnt->next;
2316     	}
2317     	if (prev_spnt == NULL)
2318     		scsi_devicelist = tpnt->next;
2319     	else
2320     		prev_spnt->next = spnt->next;
2321     
2322     	MOD_DEC_USE_COUNT;
2323     	unlock_kernel();
2324     	/*
2325     	 * Final cleanup for the driver is done in the driver sources in the
2326     	 * cleanup function.
2327     	 */
2328     	return 0;
2329     error_out:
2330     	unlock_kernel();
2331     	return -1;
2332     }
2333     
2334     
2335     /* This function should be called by drivers which needs to register
2336      * with the midlevel scsi system. As of 2.4.0-test9pre3 this is our
2337      * main device/hosts register function	/mathiasen
2338      */
2339     int scsi_register_module(int module_type, void *ptr)
2340     {
2341     	switch (module_type) {
2342     	case MODULE_SCSI_HA:
2343     		return scsi_register_host((Scsi_Host_Template *) ptr);
2344     
2345     		/* Load upper level device handler of some kind */
2346     	case MODULE_SCSI_DEV:
2347     #ifdef CONFIG_KMOD
2348     		if (scsi_hosts == NULL)
2349     			request_module("scsi_hostadapter");
2350     #endif
2351     		return scsi_register_device_module((struct Scsi_Device_Template *) ptr);
2352     		/* The rest of these are not yet implemented */
2353     
2354     		/* Load constants.o */
2355     	case MODULE_SCSI_CONST:
2356     
2357     		/* Load specialized ioctl handler for some device.  Intended for
2358     		 * cdroms that have non-SCSI2 audio command sets. */
2359     	case MODULE_SCSI_IOCTL:
2360     
2361     	default:
2362     		return 1;
2363     	}
2364     }
2365     
2366     /* Reverse the actions taken above
2367      */
2368     int scsi_unregister_module(int module_type, void *ptr)
2369     {
2370     	int retval = 0;
2371     
2372     	switch (module_type) {
2373     	case MODULE_SCSI_HA:
2374     		retval = scsi_unregister_host((Scsi_Host_Template *) ptr);
2375     		break;
2376     	case MODULE_SCSI_DEV:
2377     		retval = scsi_unregister_device((struct Scsi_Device_Template *)ptr);
2378      		break;
2379     		/* The rest of these are not yet implemented. */
2380     	case MODULE_SCSI_CONST:
2381     	case MODULE_SCSI_IOCTL:
2382     		break;
2383     	default:;
2384     	}
2385     	return retval;
2386     }
2387     
2388     #ifdef CONFIG_PROC_FS
2389     /*
2390      * Function:    scsi_dump_status
2391      *
2392      * Purpose:     Brain dump of scsi system, used for problem solving.
2393      *
2394      * Arguments:   level - used to indicate level of detail.
2395      *
2396      * Notes:       The level isn't used at all yet, but we need to find some way
2397      *              of sensibly logging varying degrees of information.  A quick one-line
2398      *              display of each command, plus the status would be most useful.
2399      *
2400      *              This does depend upon CONFIG_SCSI_LOGGING - I do want some way of turning
2401      *              it all off if the user wants a lean and mean kernel.  It would probably
2402      *              also be useful to allow the user to specify one single host to be dumped.
2403      *              A second argument to the function would be useful for that purpose.
2404      *
2405      *              FIXME - some formatting of the output into tables would be very handy.
2406      */
2407     static void scsi_dump_status(int level)
2408     {
2409     #ifdef CONFIG_SCSI_LOGGING		/* { */
2410     	int i;
2411     	struct Scsi_Host *shpnt;
2412     	Scsi_Cmnd *SCpnt;
2413     	Scsi_Device *SDpnt;
2414     	printk(KERN_INFO "Dump of scsi host parameters:\n");
2415     	i = 0;
2416     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2417     		printk(KERN_INFO " %d %d %d : %d %d\n",
2418     		       shpnt->host_failed,
2419     		       shpnt->host_busy,
2420     		       atomic_read(&shpnt->host_active),
2421     		       shpnt->host_blocked,
2422     		       shpnt->host_self_blocked);
2423     	}
2424     
2425     	printk(KERN_INFO "\n\n");
2426     	printk(KERN_INFO "Dump of scsi command parameters:\n");
2427     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2428     		printk(KERN_INFO "h:c:t:l (dev sect nsect cnumsec sg) (ret all flg) (to/cmd to ito) cmd snse result\n");
2429     		for (SDpnt = shpnt->host_queue; SDpnt; SDpnt = SDpnt->next) {
2430     			for (SCpnt = SDpnt->device_queue; SCpnt; SCpnt = SCpnt->next) {
2431     				/*  (0) h:c:t:l (dev sect nsect cnumsec sg) (ret all flg) (to/cmd to ito) cmd snse result %d %x      */
2432     				printk(KERN_INFO "(%3d) %2d:%1d:%2d:%2d (%6s %4ld %4ld %4ld %4x %1d) (%1d %1d 0x%2x) (%4d %4d %4d) 0x%2.2x 0x%2.2x 0x%8.8x\n",
2433     				       i++,
2434     
2435     				       SCpnt->host->host_no,
2436     				       SCpnt->channel,
2437     				       SCpnt->target,
2438     				       SCpnt->lun,
2439     
2440     				       kdevname(SCpnt->request.rq_dev),
2441     				       SCpnt->request.sector,
2442     				       SCpnt->request.nr_sectors,
2443     				       SCpnt->request.current_nr_sectors,
2444     				       SCpnt->request.rq_status,
2445     				       SCpnt->use_sg,
2446     
2447     				       SCpnt->retries,
2448     				       SCpnt->allowed,
2449     				       SCpnt->flags,
2450     
2451     				       SCpnt->timeout_per_command,
2452     				       SCpnt->timeout,
2453     				       SCpnt->internal_timeout,
2454     
2455     				       SCpnt->cmnd[0],
2456     				       SCpnt->sense_buffer[2],
2457     				       SCpnt->result);
2458     			}
2459     		}
2460     	}
2461     
2462     	for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
2463     		for (SDpnt = shpnt->host_queue; SDpnt; SDpnt = SDpnt->next) {
2464     			/* Now dump the request lists for each block device */
2465     			printk(KERN_INFO "Dump of pending block device requests\n");
2466     			for (i = 0; i < MAX_BLKDEV; i++) {
2467     				struct list_head * queue_head;
2468     
2469     				queue_head = &blk_dev[i].request_queue.queue_head;
2470     				if (!list_empty(queue_head)) {
2471     					struct request *req;
2472     					struct list_head * entry;
2473     
2474     					printk(KERN_INFO "%d: ", i);
2475     					entry = queue_head->next;
2476     					do {
2477     						req = blkdev_entry_to_request(entry);
2478     						printk("(%s %d %ld %ld %ld) ",
2479     						   kdevname(req->rq_dev),
2480     						       req->cmd,
2481     						       req->sector,
2482     						       req->nr_sectors,
2483     						req->current_nr_sectors);
2484     					} while ((entry = entry->next) != queue_head);
2485     					printk("\n");
2486     				}
2487     			}
2488     		}
2489     	}
2490     #endif	/* CONFIG_SCSI_LOGGING */ /* } */
2491     }
2492     #endif				/* CONFIG_PROC_FS */
2493     
2494     static int __init scsi_host_no_init (char *str)
2495     {
2496         static int next_no = 0;
2497         char *temp;
2498     
2499         while (str) {
2500     	temp = str;
2501     	while (*temp && (*temp != ':') && (*temp != ','))
2502     	    temp++;
2503     	if (!*temp)
2504     	    temp = NULL;
2505     	else
2506     	    *temp++ = 0;
2507     	scsi_host_no_insert(str, next_no);
2508     	str = temp;
2509     	next_no++;
2510         }
2511         return 1;
2512     }
2513     
2514     static char *scsihosts;
2515     
2516     MODULE_PARM(scsihosts, "s");
2517     MODULE_DESCRIPTION("SCSI core");
2518     
2519     #ifndef MODULE
2520     int __init scsi_setup(char *str)
2521     {
2522     	scsihosts = str;
2523     	return 1;
2524     }
2525     
2526     __setup("scsihosts=", scsi_setup);
2527     #endif
2528     
2529     static int __init init_scsi(void)
2530     {
2531     	struct proc_dir_entry *generic;
2532     
2533     	printk(KERN_INFO "SCSI subsystem driver " REVISION "\n");
2534     
2535             if( scsi_init_minimal_dma_pool() != 0 )
2536             {
2537                     return 1;
2538             }
2539     
2540     	/*
2541     	 * This makes /proc/scsi and /proc/scsi/scsi visible.
2542     	 */
2543     #ifdef CONFIG_PROC_FS
2544     	proc_scsi = proc_mkdir("scsi", 0);
2545     	if (!proc_scsi) {
2546     		printk (KERN_ERR "cannot init /proc/scsi\n");
2547     		return -ENOMEM;
2548     	}
2549     	generic = create_proc_info_entry ("scsi/scsi", 0, 0, scsi_proc_info);
2550     	if (!generic) {
2551     		printk (KERN_ERR "cannot init /proc/scsi/scsi\n");
2552     		remove_proc_entry("scsi", 0);
2553     		return -ENOMEM;
2554     	}
2555     	generic->write_proc = proc_scsi_gen_write;
2556     #endif
2557     
2558             scsi_devfs_handle = devfs_mk_dir (NULL, "scsi", NULL);
2559             if (scsihosts)
2560     		printk(KERN_INFO "scsi: host order: %s\n", scsihosts);	
2561     	scsi_host_no_init (scsihosts);
2562     	/*
2563     	 * This is where the processing takes place for most everything
2564     	 * when commands are completed.
2565     	 */
2566     	init_bh(SCSI_BH, scsi_bottom_half_handler);
2567     
2568     	return 0;
2569     }
2570     
2571     static void __exit exit_scsi(void)
2572     {
2573     	Scsi_Host_Name *shn, *shn2 = NULL;
2574     
2575     	remove_bh(SCSI_BH);
2576     
2577             devfs_unregister (scsi_devfs_handle);
2578             for (shn = scsi_host_no_list;shn;shn = shn->next) {
2579     		if (shn->name)
2580     			kfree(shn->name);
2581                     if (shn2)
2582     			kfree (shn2);
2583                     shn2 = shn;
2584             }
2585             if (shn2)
2586     		kfree (shn2);
2587     
2588     #ifdef CONFIG_PROC_FS
2589     	/* No, we're not here anymore. Don't show the /proc/scsi files. */
2590     	remove_proc_entry ("scsi/scsi", 0);
2591     	remove_proc_entry ("scsi", 0);
2592     #endif
2593     	
2594     	/*
2595     	 * Free up the DMA pool.
2596     	 */
2597     	scsi_resize_dma_pool();
2598     
2599     }
2600     
2601     module_init(init_scsi);
2602     module_exit(exit_scsi);
2603     
2604     /*
2605      * Function:    scsi_get_host_dev()
2606      *
2607      * Purpose:     Create a Scsi_Device that points to the host adapter itself.
2608      *
2609      * Arguments:   SHpnt   - Host that needs a Scsi_Device
2610      *
2611      * Lock status: None assumed.
2612      *
2613      * Returns:     The Scsi_Device or NULL
2614      *
2615      * Notes:
2616      */
2617     Scsi_Device * scsi_get_host_dev(struct Scsi_Host * SHpnt)
2618     {
2619             Scsi_Device * SDpnt;
2620     
2621             /*
2622              * Attach a single Scsi_Device to the Scsi_Host - this should
2623              * be made to look like a "pseudo-device" that points to the
2624              * HA itself.  For the moment, we include it at the head of
2625              * the host_queue itself - I don't think we want to show this
2626              * to the HA in select_queue_depths(), as this would probably confuse
2627              * matters.
2628              * Note - this device is not accessible from any high-level
2629              * drivers (including generics), which is probably not
2630              * optimal.  We can add hooks later to attach 
2631              */
2632             SDpnt = (Scsi_Device *) kmalloc(sizeof(Scsi_Device),
2633                                             GFP_ATOMIC);
2634             if(SDpnt == NULL)
2635             	return NULL;
2636             	
2637             memset(SDpnt, 0, sizeof(Scsi_Device));
2638     
2639             SDpnt->host = SHpnt;
2640             SDpnt->id = SHpnt->this_id;
2641             SDpnt->type = -1;
2642             SDpnt->queue_depth = 1;
2643             
2644     	scsi_build_commandblocks(SDpnt);
2645     
2646     	scsi_initialize_queue(SDpnt, SHpnt);
2647     
2648     	SDpnt->online = TRUE;
2649     
2650             /*
2651              * Initialize the object that we will use to wait for command blocks.
2652              */
2653     	init_waitqueue_head(&SDpnt->scpnt_wait);
2654             return SDpnt;
2655     }
2656     
2657     /*
2658      * Function:    scsi_free_host_dev()
2659      *
2660      * Purpose:     Create a Scsi_Device that points to the host adapter itself.
2661      *
2662      * Arguments:   SHpnt   - Host that needs a Scsi_Device
2663      *
2664      * Lock status: None assumed.
2665      *
2666      * Returns:     Nothing
2667      *
2668      * Notes:
2669      */
2670     void scsi_free_host_dev(Scsi_Device * SDpnt)
2671     {
2672             if( (unsigned char) SDpnt->id != (unsigned char) SDpnt->host->this_id )
2673             {
2674                     panic("Attempt to delete wrong device\n");
2675             }
2676     
2677             blk_cleanup_queue(&SDpnt->request_queue);
2678     
2679             /*
2680              * We only have a single SCpnt attached to this device.  Free
2681              * it now.
2682              */
2683     	scsi_release_commandblocks(SDpnt);
2684             kfree(SDpnt);
2685     }
2686     
2687     /*
2688      * Overrides for Emacs so that we follow Linus's tabbing style.
2689      * Emacs will notice this stuff at the end of the file and automatically
2690      * adjust the settings for this buffer only.  This must remain at the end
2691      * of the file.
2692      * ---------------------------------------------------------------------------
2693      * Local variables:
2694      * c-indent-level: 4
2695      * c-brace-imaginary-offset: 0
2696      * c-brace-offset: -4
2697      * c-argdecl-indent: 4
2698      * c-label-offset: -4
2699      * c-continued-statement-offset: 4
2700      * c-continued-brace-offset: 0
2701      * indent-tabs-mode: nil
2702      * tab-width: 8
2703      * End:
2704      */
2705