1afe1e592af427cbe506f2367dd75f1c57722c62
[pandora-kernel.git] / drivers / scsi / scsi.c
1 /*
2  *  scsi.c Copyright (C) 1992 Drew Eckhardt
3  *         Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale
4  *         Copyright (C) 2002, 2003 Christoph Hellwig
5  *
6  *  generic mid-level SCSI driver
7  *      Initial versions: Drew Eckhardt
8  *      Subsequent revisions: Eric Youngdale
9  *
10  *  <drew@colorado.edu>
11  *
12  *  Bug correction thanks go to :
13  *      Rik Faith <faith@cs.unc.edu>
14  *      Tommy Thorn <tthorn>
15  *      Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de>
16  *
17  *  Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to
18  *  add scatter-gather, multiple outstanding request, and other
19  *  enhancements.
20  *
21  *  Native multichannel, wide scsi, /proc/scsi and hot plugging
22  *  support added by Michael Neuffer <mike@i-connect.net>
23  *
24  *  Added request_module("scsi_hostadapter") for kerneld:
25  *  (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf)
26  *  Bjorn Ekwall  <bj0rn@blox.se>
27  *  (changed to kmod)
28  *
29  *  Major improvements to the timeout, abort, and reset processing,
30  *  as well as performance modifications for large queue depths by
31  *  Leonard N. Zubkoff <lnz@dandelion.com>
32  *
33  *  Converted cli() code to spinlocks, Ingo Molnar
34  *
35  *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
36  *
37  *  out_of_space hacks, D. Gilbert (dpg) 990608
38  */
39
40 #include <linux/module.h>
41 #include <linux/moduleparam.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/timer.h>
45 #include <linux/string.h>
46 #include <linux/slab.h>
47 #include <linux/blkdev.h>
48 #include <linux/delay.h>
49 #include <linux/init.h>
50 #include <linux/completion.h>
51 #include <linux/devfs_fs_kernel.h>
52 #include <linux/unistd.h>
53 #include <linux/spinlock.h>
54 #include <linux/kmod.h>
55 #include <linux/interrupt.h>
56 #include <linux/notifier.h>
57 #include <linux/cpu.h>
58
59 #include <scsi/scsi.h>
60 #include <scsi/scsi_cmnd.h>
61 #include <scsi/scsi_dbg.h>
62 #include <scsi/scsi_device.h>
63 #include <scsi/scsi_eh.h>
64 #include <scsi/scsi_host.h>
65 #include <scsi/scsi_tcq.h>
66 #include <scsi/scsi_request.h>
67
68 #include "scsi_priv.h"
69 #include "scsi_logging.h"
70
71 static void scsi_done(struct scsi_cmnd *cmd);
72 static int scsi_retry_command(struct scsi_cmnd *cmd);
73
74 /*
75  * Definitions and constants.
76  */
77
78 #define MIN_RESET_DELAY (2*HZ)
79
80 /* Do not call reset on error if we just did a reset within 15 sec. */
81 #define MIN_RESET_PERIOD (15*HZ)
82
83 /*
84  * Macro to determine the size of SCSI command. This macro takes vendor
85  * unique commands into account. SCSI commands in groups 6 and 7 are
86  * vendor unique and we will depend upon the command length being
87  * supplied correctly in cmd_len.
88  */
89 #define CDB_SIZE(cmd)   (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \
90                                 COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len)
91
92 /*
93  * Note - the initial logging level can be set here to log events at boot time.
94  * After the system is up, you may enable logging via the /proc interface.
95  */
96 unsigned int scsi_logging_level;
97 #if defined(CONFIG_SCSI_LOGGING)
98 EXPORT_SYMBOL(scsi_logging_level);
99 #endif
100
101 const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] = {
102         "Direct-Access    ",
103         "Sequential-Access",
104         "Printer          ",
105         "Processor        ",
106         "WORM             ",
107         "CD-ROM           ",
108         "Scanner          ",
109         "Optical Device   ",
110         "Medium Changer   ",
111         "Communications   ",
112         "Unknown          ",
113         "Unknown          ",
114         "RAID             ",
115         "Enclosure        ",
116 };
117 EXPORT_SYMBOL(scsi_device_types);
118
119 /*
120  * Function:    scsi_allocate_request
121  *
122  * Purpose:     Allocate a request descriptor.
123  *
124  * Arguments:   device          - device for which we want a request
125  *              gfp_mask        - allocation flags passed to kmalloc
126  *
127  * Lock status: No locks assumed to be held.  This function is SMP-safe.
128  *
129  * Returns:     Pointer to request block.
130  */
131 struct scsi_request *scsi_allocate_request(struct scsi_device *sdev,
132                                            int gfp_mask)
133 {
134         const int offset = ALIGN(sizeof(struct scsi_request), 4);
135         const int size = offset + sizeof(struct request);
136         struct scsi_request *sreq;
137   
138         sreq = kmalloc(size, gfp_mask);
139         if (likely(sreq != NULL)) {
140                 memset(sreq, 0, size);
141                 sreq->sr_request = (struct request *)(((char *)sreq) + offset);
142                 sreq->sr_device = sdev;
143                 sreq->sr_host = sdev->host;
144                 sreq->sr_magic = SCSI_REQ_MAGIC;
145                 sreq->sr_data_direction = DMA_BIDIRECTIONAL;
146         }
147
148         return sreq;
149 }
150 EXPORT_SYMBOL(scsi_allocate_request);
151
152 void __scsi_release_request(struct scsi_request *sreq)
153 {
154         struct request *req = sreq->sr_request;
155
156         /* unlikely because the tag was usually ended earlier by the
157          * mid-layer. However, for layering reasons ULD's don't end
158          * the tag of commands they generate. */
159         if (unlikely(blk_rq_tagged(req))) {
160                 unsigned long flags;
161                 struct request_queue *q = req->q;
162
163                 spin_lock_irqsave(q->queue_lock, flags);
164                 blk_queue_end_tag(q, req);
165                 spin_unlock_irqrestore(q->queue_lock, flags);
166         }
167
168
169         if (likely(sreq->sr_command != NULL)) {
170                 struct scsi_cmnd *cmd = sreq->sr_command;
171
172                 sreq->sr_command = NULL;
173                 scsi_next_command(cmd);
174         }
175 }
176
177 /*
178  * Function:    scsi_release_request
179  *
180  * Purpose:     Release a request descriptor.
181  *
182  * Arguments:   sreq    - request to release
183  *
184  * Lock status: No locks assumed to be held.  This function is SMP-safe.
185  */
186 void scsi_release_request(struct scsi_request *sreq)
187 {
188         __scsi_release_request(sreq);
189         kfree(sreq);
190 }
191 EXPORT_SYMBOL(scsi_release_request);
192
193 struct scsi_host_cmd_pool {
194         kmem_cache_t    *slab;
195         unsigned int    users;
196         char            *name;
197         unsigned int    slab_flags;
198         unsigned int    gfp_mask;
199 };
200
201 static struct scsi_host_cmd_pool scsi_cmd_pool = {
202         .name           = "scsi_cmd_cache",
203         .slab_flags     = SLAB_HWCACHE_ALIGN,
204 };
205
206 static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
207         .name           = "scsi_cmd_cache(DMA)",
208         .slab_flags     = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
209         .gfp_mask       = __GFP_DMA,
210 };
211
212 static DECLARE_MUTEX(host_cmd_pool_mutex);
213
214 static struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost,
215                                             int gfp_mask)
216 {
217         struct scsi_cmnd *cmd;
218
219         cmd = kmem_cache_alloc(shost->cmd_pool->slab,
220                         gfp_mask | shost->cmd_pool->gfp_mask);
221
222         if (unlikely(!cmd)) {
223                 unsigned long flags;
224
225                 spin_lock_irqsave(&shost->free_list_lock, flags);
226                 if (likely(!list_empty(&shost->free_list))) {
227                         cmd = list_entry(shost->free_list.next,
228                                          struct scsi_cmnd, list);
229                         list_del_init(&cmd->list);
230                 }
231                 spin_unlock_irqrestore(&shost->free_list_lock, flags);
232         }
233
234         return cmd;
235 }
236
237 /*
238  * Function:    scsi_get_command()
239  *
240  * Purpose:     Allocate and setup a scsi command block
241  *
242  * Arguments:   dev     - parent scsi device
243  *              gfp_mask- allocator flags
244  *
245  * Returns:     The allocated scsi command structure.
246  */
247 struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, int gfp_mask)
248 {
249         struct scsi_cmnd *cmd;
250
251         /* Bail if we can't get a reference to the device */
252         if (!get_device(&dev->sdev_gendev))
253                 return NULL;
254
255         cmd = __scsi_get_command(dev->host, gfp_mask);
256
257         if (likely(cmd != NULL)) {
258                 unsigned long flags;
259
260                 memset(cmd, 0, sizeof(*cmd));
261                 cmd->device = dev;
262                 init_timer(&cmd->eh_timeout);
263                 INIT_LIST_HEAD(&cmd->list);
264                 spin_lock_irqsave(&dev->list_lock, flags);
265                 list_add_tail(&cmd->list, &dev->cmd_list);
266                 spin_unlock_irqrestore(&dev->list_lock, flags);
267         } else
268                 put_device(&dev->sdev_gendev);
269
270         return cmd;
271 }                               
272 EXPORT_SYMBOL(scsi_get_command);
273
274 /*
275  * Function:    scsi_put_command()
276  *
277  * Purpose:     Free a scsi command block
278  *
279  * Arguments:   cmd     - command block to free
280  *
281  * Returns:     Nothing.
282  *
283  * Notes:       The command must not belong to any lists.
284  */
285 void scsi_put_command(struct scsi_cmnd *cmd)
286 {
287         struct scsi_device *sdev = cmd->device;
288         struct Scsi_Host *shost = sdev->host;
289         unsigned long flags;
290         
291         /* serious error if the command hasn't come from a device list */
292         spin_lock_irqsave(&cmd->device->list_lock, flags);
293         BUG_ON(list_empty(&cmd->list));
294         list_del_init(&cmd->list);
295         spin_unlock(&cmd->device->list_lock);
296         /* changing locks here, don't need to restore the irq state */
297         spin_lock(&shost->free_list_lock);
298         if (unlikely(list_empty(&shost->free_list))) {
299                 list_add(&cmd->list, &shost->free_list);
300                 cmd = NULL;
301         }
302         spin_unlock_irqrestore(&shost->free_list_lock, flags);
303
304         if (likely(cmd != NULL))
305                 kmem_cache_free(shost->cmd_pool->slab, cmd);
306
307         put_device(&sdev->sdev_gendev);
308 }
309 EXPORT_SYMBOL(scsi_put_command);
310
311 /*
312  * Function:    scsi_setup_command_freelist()
313  *
314  * Purpose:     Setup the command freelist for a scsi host.
315  *
316  * Arguments:   shost   - host to allocate the freelist for.
317  *
318  * Returns:     Nothing.
319  */
320 int scsi_setup_command_freelist(struct Scsi_Host *shost)
321 {
322         struct scsi_host_cmd_pool *pool;
323         struct scsi_cmnd *cmd;
324
325         spin_lock_init(&shost->free_list_lock);
326         INIT_LIST_HEAD(&shost->free_list);
327
328         /*
329          * Select a command slab for this host and create it if not
330          * yet existant.
331          */
332         down(&host_cmd_pool_mutex);
333         pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool);
334         if (!pool->users) {
335                 pool->slab = kmem_cache_create(pool->name,
336                                 sizeof(struct scsi_cmnd), 0,
337                                 pool->slab_flags, NULL, NULL);
338                 if (!pool->slab)
339                         goto fail;
340         }
341
342         pool->users++;
343         shost->cmd_pool = pool;
344         up(&host_cmd_pool_mutex);
345
346         /*
347          * Get one backup command for this host.
348          */
349         cmd = kmem_cache_alloc(shost->cmd_pool->slab,
350                         GFP_KERNEL | shost->cmd_pool->gfp_mask);
351         if (!cmd)
352                 goto fail2;
353         list_add(&cmd->list, &shost->free_list);                
354         return 0;
355
356  fail2:
357         if (!--pool->users)
358                 kmem_cache_destroy(pool->slab);
359         return -ENOMEM;
360  fail:
361         up(&host_cmd_pool_mutex);
362         return -ENOMEM;
363
364 }
365
366 /*
367  * Function:    scsi_destroy_command_freelist()
368  *
369  * Purpose:     Release the command freelist for a scsi host.
370  *
371  * Arguments:   shost   - host that's freelist is going to be destroyed
372  */
373 void scsi_destroy_command_freelist(struct Scsi_Host *shost)
374 {
375         while (!list_empty(&shost->free_list)) {
376                 struct scsi_cmnd *cmd;
377
378                 cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
379                 list_del_init(&cmd->list);
380                 kmem_cache_free(shost->cmd_pool->slab, cmd);
381         }
382
383         down(&host_cmd_pool_mutex);
384         if (!--shost->cmd_pool->users)
385                 kmem_cache_destroy(shost->cmd_pool->slab);
386         up(&host_cmd_pool_mutex);
387 }
388
389 #ifdef CONFIG_SCSI_LOGGING
390 void scsi_log_send(struct scsi_cmnd *cmd)
391 {
392         unsigned int level;
393         struct scsi_device *sdev;
394
395         /*
396          * If ML QUEUE log level is greater than or equal to:
397          *
398          * 1: nothing (match completion)
399          *
400          * 2: log opcode + command of all commands
401          *
402          * 3: same as 2 plus dump cmd address
403          *
404          * 4: same as 3 plus dump extra junk
405          */
406         if (unlikely(scsi_logging_level)) {
407                 level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
408                                        SCSI_LOG_MLQUEUE_BITS);
409                 if (level > 1) {
410                         sdev = cmd->device;
411                         printk(KERN_INFO "scsi <%d:%d:%d:%d> send ",
412                                sdev->host->host_no, sdev->channel, sdev->id,
413                                sdev->lun);
414                         if (level > 2)
415                                 printk("0x%p ", cmd);
416                         /*
417                          * spaces to match disposition and cmd->result
418                          * output in scsi_log_completion.
419                          */
420                         printk("                 ");
421                         scsi_print_command(cmd);
422                         if (level > 3) {
423                                 printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
424                                        " done = 0x%p, queuecommand 0x%p\n",
425                                         cmd->buffer, cmd->bufflen,
426                                         cmd->done,
427                                         sdev->host->hostt->queuecommand);
428
429                         }
430                 }
431         }
432 }
433
434 void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
435 {
436         unsigned int level;
437         struct scsi_device *sdev;
438
439         /*
440          * If ML COMPLETE log level is greater than or equal to:
441          *
442          * 1: log disposition, result, opcode + command, and conditionally
443          * sense data for failures or non SUCCESS dispositions.
444          *
445          * 2: same as 1 but for all command completions.
446          *
447          * 3: same as 2 plus dump cmd address
448          *
449          * 4: same as 3 plus dump extra junk
450          */
451         if (unlikely(scsi_logging_level)) {
452                 level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
453                                        SCSI_LOG_MLCOMPLETE_BITS);
454                 if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
455                     (level > 1)) {
456                         sdev = cmd->device;
457                         printk(KERN_INFO "scsi <%d:%d:%d:%d> done ",
458                                sdev->host->host_no, sdev->channel, sdev->id,
459                                sdev->lun);
460                         if (level > 2)
461                                 printk("0x%p ", cmd);
462                         /*
463                          * Dump truncated values, so we usually fit within
464                          * 80 chars.
465                          */
466                         switch (disposition) {
467                         case SUCCESS:
468                                 printk("SUCCESS");
469                                 break;
470                         case NEEDS_RETRY:
471                                 printk("RETRY  ");
472                                 break;
473                         case ADD_TO_MLQUEUE:
474                                 printk("MLQUEUE");
475                                 break;
476                         case FAILED:
477                                 printk("FAILED ");
478                                 break;
479                         case TIMEOUT_ERROR:
480                                 /* 
481                                  * If called via scsi_times_out.
482                                  */
483                                 printk("TIMEOUT");
484                                 break;
485                         default:
486                                 printk("UNKNOWN");
487                         }
488                         printk(" %8x ", cmd->result);
489                         scsi_print_command(cmd);
490                         if (status_byte(cmd->result) & CHECK_CONDITION) {
491                                 /*
492                                  * XXX The scsi_print_sense formatting/prefix
493                                  * doesn't match this function.
494                                  */
495                                 scsi_print_sense("", cmd);
496                         }
497                         if (level > 3) {
498                                 printk(KERN_INFO "scsi host busy %d failed %d\n",
499                                        sdev->host->host_busy,
500                                        sdev->host->host_failed);
501                         }
502                 }
503         }
504 }
505 #endif
506
507 /* 
508  * Assign a serial number and pid to the request for error recovery
509  * and debugging purposes.  Protected by the Host_Lock of host.
510  */
511 static inline void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
512 {
513         cmd->serial_number = host->cmd_serial_number++;
514         if (cmd->serial_number == 0) 
515                 cmd->serial_number = host->cmd_serial_number++;
516         
517         cmd->pid = host->cmd_pid++;
518         if (cmd->pid == 0)
519                 cmd->pid = host->cmd_pid++;
520 }
521
522 /*
523  * Function:    scsi_dispatch_command
524  *
525  * Purpose:     Dispatch a command to the low-level driver.
526  *
527  * Arguments:   cmd - command block we are dispatching.
528  *
529  * Notes:
530  */
531 int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
532 {
533         struct Scsi_Host *host = cmd->device->host;
534         unsigned long flags = 0;
535         unsigned long timeout;
536         int rtn = 0;
537
538         /* check if the device is still usable */
539         if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
540                 /* in SDEV_DEL we error all commands. DID_NO_CONNECT
541                  * returns an immediate error upwards, and signals
542                  * that the device is no longer present */
543                 cmd->result = DID_NO_CONNECT << 16;
544                 atomic_inc(&cmd->device->iorequest_cnt);
545                 __scsi_done(cmd);
546                 /* return 0 (because the command has been processed) */
547                 goto out;
548         }
549
550         /* Check to see if the scsi lld put this device into state SDEV_BLOCK. */
551         if (unlikely(cmd->device->sdev_state == SDEV_BLOCK)) {
552                 /* 
553                  * in SDEV_BLOCK, the command is just put back on the device
554                  * queue.  The suspend state has already blocked the queue so
555                  * future requests should not occur until the device 
556                  * transitions out of the suspend state.
557                  */
558                 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
559
560                 SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n"));
561
562                 /*
563                  * NOTE: rtn is still zero here because we don't need the
564                  * queue to be plugged on return (it's already stopped)
565                  */
566                 goto out;
567         }
568
569         /* 
570          * If SCSI-2 or lower, store the LUN value in cmnd.
571          */
572         if (cmd->device->scsi_level <= SCSI_2) {
573                 cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
574                                (cmd->device->lun << 5 & 0xe0);
575         }
576
577         /*
578          * We will wait MIN_RESET_DELAY clock ticks after the last reset so
579          * we can avoid the drive not being ready.
580          */
581         timeout = host->last_reset + MIN_RESET_DELAY;
582
583         if (host->resetting && time_before(jiffies, timeout)) {
584                 int ticks_remaining = timeout - jiffies;
585                 /*
586                  * NOTE: This may be executed from within an interrupt
587                  * handler!  This is bad, but for now, it'll do.  The irq
588                  * level of the interrupt handler has been masked out by the
589                  * platform dependent interrupt handling code already, so the
590                  * sti() here will not cause another call to the SCSI host's
591                  * interrupt handler (assuming there is one irq-level per
592                  * host).
593                  */
594                 while (--ticks_remaining >= 0)
595                         mdelay(1 + 999 / HZ);
596                 host->resetting = 0;
597         }
598
599         /* 
600          * AK: unlikely race here: for some reason the timer could
601          * expire before the serial number is set up below.
602          */
603         scsi_add_timer(cmd, cmd->timeout_per_command, scsi_times_out);
604
605         scsi_log_send(cmd);
606
607         /*
608          * We will use a queued command if possible, otherwise we will
609          * emulate the queuing and calling of completion function ourselves.
610          */
611         atomic_inc(&cmd->device->iorequest_cnt);
612
613         /*
614          * Before we queue this command, check if the command
615          * length exceeds what the host adapter can handle.
616          */
617         if (CDB_SIZE(cmd) > cmd->device->host->max_cmd_len) {
618                 SCSI_LOG_MLQUEUE(3,
619                                 printk("queuecommand : command too long.\n"));
620                 cmd->result = (DID_ABORT << 16);
621
622                 scsi_done(cmd);
623                 goto out;
624         }
625
626         spin_lock_irqsave(host->host_lock, flags);
627         scsi_cmd_get_serial(host, cmd); 
628
629         if (unlikely(test_bit(SHOST_CANCEL, &host->shost_state))) {
630                 cmd->result = (DID_NO_CONNECT << 16);
631                 scsi_done(cmd);
632         } else {
633                 rtn = host->hostt->queuecommand(cmd, scsi_done);
634         }
635         spin_unlock_irqrestore(host->host_lock, flags);
636         if (rtn) {
637                 if (scsi_delete_timer(cmd)) {
638                         atomic_inc(&cmd->device->iodone_cnt);
639                         scsi_queue_insert(cmd,
640                                           (rtn == SCSI_MLQUEUE_DEVICE_BUSY) ?
641                                           rtn : SCSI_MLQUEUE_HOST_BUSY);
642                 }
643                 SCSI_LOG_MLQUEUE(3,
644                     printk("queuecommand : request rejected\n"));
645         }
646
647  out:
648         SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
649         return rtn;
650 }
651
652 /*
653  * Function:    scsi_init_cmd_from_req
654  *
655  * Purpose:     Queue a SCSI command
656  * Purpose:     Initialize a struct scsi_cmnd from a struct scsi_request
657  *
658  * Arguments:   cmd       - command descriptor.
659  *              sreq      - Request from the queue.
660  *
661  * Lock status: None needed.
662  *
663  * Returns:     Nothing.
664  *
665  * Notes:       Mainly transfer data from the request structure to the
666  *              command structure.  The request structure is allocated
667  *              using the normal memory allocator, and requests can pile
668  *              up to more or less any depth.  The command structure represents
669  *              a consumable resource, as these are allocated into a pool
670  *              when the SCSI subsystem initializes.  The preallocation is
671  *              required so that in low-memory situations a disk I/O request
672  *              won't cause the memory manager to try and write out a page.
673  *              The request structure is generally used by ioctls and character
674  *              devices.
675  */
676 void scsi_init_cmd_from_req(struct scsi_cmnd *cmd, struct scsi_request *sreq)
677 {
678         sreq->sr_command = cmd;
679
680         cmd->cmd_len = sreq->sr_cmd_len;
681         cmd->use_sg = sreq->sr_use_sg;
682
683         cmd->request = sreq->sr_request;
684         memcpy(cmd->data_cmnd, sreq->sr_cmnd, sizeof(cmd->data_cmnd));
685         cmd->serial_number = 0;
686         cmd->bufflen = sreq->sr_bufflen;
687         cmd->buffer = sreq->sr_buffer;
688         cmd->retries = 0;
689         cmd->allowed = sreq->sr_allowed;
690         cmd->done = sreq->sr_done;
691         cmd->timeout_per_command = sreq->sr_timeout_per_command;
692         cmd->sc_data_direction = sreq->sr_data_direction;
693         cmd->sglist_len = sreq->sr_sglist_len;
694         cmd->underflow = sreq->sr_underflow;
695         cmd->sc_request = sreq;
696         memcpy(cmd->cmnd, sreq->sr_cmnd, sizeof(sreq->sr_cmnd));
697
698         /*
699          * Zero the sense buffer.  Some host adapters automatically request
700          * sense on error.  0 is not a valid sense code.
701          */
702         memset(cmd->sense_buffer, 0, sizeof(sreq->sr_sense_buffer));
703         cmd->request_buffer = sreq->sr_buffer;
704         cmd->request_bufflen = sreq->sr_bufflen;
705         cmd->old_use_sg = cmd->use_sg;
706         if (cmd->cmd_len == 0)
707                 cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
708         cmd->old_cmd_len = cmd->cmd_len;
709         cmd->sc_old_data_direction = cmd->sc_data_direction;
710         cmd->old_underflow = cmd->underflow;
711
712         /*
713          * Start the timer ticking.
714          */
715         cmd->result = 0;
716
717         SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_init_cmd_from_req()\n"));
718 }
719
720 /*
721  * Per-CPU I/O completion queue.
722  */
723 static DEFINE_PER_CPU(struct list_head, scsi_done_q);
724
725 /**
726  * scsi_done - Enqueue the finished SCSI command into the done queue.
727  * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
728  * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
729  *
730  * This function is the mid-level's (SCSI Core) interrupt routine, which
731  * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues
732  * the command to the done queue for further processing.
733  *
734  * This is the producer of the done queue who enqueues at the tail.
735  *
736  * This function is interrupt context safe.
737  */
738 static void scsi_done(struct scsi_cmnd *cmd)
739 {
740         /*
741          * We don't have to worry about this one timing out any more.
742          * If we are unable to remove the timer, then the command
743          * has already timed out.  In which case, we have no choice but to
744          * let the timeout function run, as we have no idea where in fact
745          * that function could really be.  It might be on another processor,
746          * etc, etc.
747          */
748         if (!scsi_delete_timer(cmd))
749                 return;
750         __scsi_done(cmd);
751 }
752
753 /* Private entry to scsi_done() to complete a command when the timer
754  * isn't running --- used by scsi_times_out */
755 void __scsi_done(struct scsi_cmnd *cmd)
756 {
757         unsigned long flags;
758
759         /*
760          * Set the serial numbers back to zero
761          */
762         cmd->serial_number = 0;
763
764         atomic_inc(&cmd->device->iodone_cnt);
765         if (cmd->result)
766                 atomic_inc(&cmd->device->ioerr_cnt);
767
768         /*
769          * Next, enqueue the command into the done queue.
770          * It is a per-CPU queue, so we just disable local interrupts
771          * and need no spinlock.
772          */
773         local_irq_save(flags);
774         list_add_tail(&cmd->eh_entry, &__get_cpu_var(scsi_done_q));
775         raise_softirq_irqoff(SCSI_SOFTIRQ);
776         local_irq_restore(flags);
777 }
778
779 /**
780  * scsi_softirq - Perform post-interrupt processing of finished SCSI commands.
781  *
782  * This is the consumer of the done queue.
783  *
784  * This is called with all interrupts enabled.  This should reduce
785  * interrupt latency, stack depth, and reentrancy of the low-level
786  * drivers.
787  */
788 static void scsi_softirq(struct softirq_action *h)
789 {
790         int disposition;
791         LIST_HEAD(local_q);
792
793         local_irq_disable();
794         list_splice_init(&__get_cpu_var(scsi_done_q), &local_q);
795         local_irq_enable();
796
797         while (!list_empty(&local_q)) {
798                 struct scsi_cmnd *cmd = list_entry(local_q.next,
799                                                    struct scsi_cmnd, eh_entry);
800                 list_del_init(&cmd->eh_entry);
801
802                 disposition = scsi_decide_disposition(cmd);
803                 scsi_log_completion(cmd, disposition);
804                 switch (disposition) {
805                 case SUCCESS:
806                         scsi_finish_command(cmd);
807                         break;
808                 case NEEDS_RETRY:
809                         scsi_retry_command(cmd);
810                         break;
811                 case ADD_TO_MLQUEUE:
812                         scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
813                         break;
814                 default:
815                         if (!scsi_eh_scmd_add(cmd, 0))
816                                 scsi_finish_command(cmd);
817                 }
818         }
819 }
820
821 /*
822  * Function:    scsi_retry_command
823  *
824  * Purpose:     Send a command back to the low level to be retried.
825  *
826  * Notes:       This command is always executed in the context of the
827  *              bottom half handler, or the error handler thread. Low
828  *              level drivers should not become re-entrant as a result of
829  *              this.
830  */
831 static int scsi_retry_command(struct scsi_cmnd *cmd)
832 {
833         /*
834          * Restore the SCSI command state.
835          */
836         scsi_setup_cmd_retry(cmd);
837
838         /*
839          * Zero the sense information from the last time we tried
840          * this command.
841          */
842         memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
843
844         return scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
845 }
846
847 /*
848  * Function:    scsi_finish_command
849  *
850  * Purpose:     Pass command off to upper layer for finishing of I/O
851  *              request, waking processes that are waiting on results,
852  *              etc.
853  */
854 void scsi_finish_command(struct scsi_cmnd *cmd)
855 {
856         struct scsi_device *sdev = cmd->device;
857         struct Scsi_Host *shost = sdev->host;
858         struct scsi_request *sreq;
859
860         scsi_device_unbusy(sdev);
861
862         /*
863          * Clear the flags which say that the device/host is no longer
864          * capable of accepting new commands.  These are set in scsi_queue.c
865          * for both the queue full condition on a device, and for a
866          * host full condition on the host.
867          *
868          * XXX(hch): What about locking?
869          */
870         shost->host_blocked = 0;
871         sdev->device_blocked = 0;
872
873         /*
874          * If we have valid sense information, then some kind of recovery
875          * must have taken place.  Make a note of this.
876          */
877         if (SCSI_SENSE_VALID(cmd))
878                 cmd->result |= (DRIVER_SENSE << 24);
879
880         SCSI_LOG_MLCOMPLETE(4, printk("Notifying upper driver of completion "
881                                 "for device %d %x\n", sdev->id, cmd->result));
882
883         /*
884          * We can get here with use_sg=0, causing a panic in the upper level
885          */
886         cmd->use_sg = cmd->old_use_sg;
887
888         /*
889          * If there is an associated request structure, copy the data over
890          * before we call the completion function.
891          */
892         sreq = cmd->sc_request;
893         if (sreq) {
894                sreq->sr_result = sreq->sr_command->result;
895                if (sreq->sr_result) {
896                        memcpy(sreq->sr_sense_buffer,
897                               sreq->sr_command->sense_buffer,
898                               sizeof(sreq->sr_sense_buffer));
899                }
900         }
901
902         cmd->done(cmd);
903 }
904 EXPORT_SYMBOL(scsi_finish_command);
905
906 /*
907  * Function:    scsi_adjust_queue_depth()
908  *
909  * Purpose:     Allow low level drivers to tell us to change the queue depth
910  *              on a specific SCSI device
911  *
912  * Arguments:   sdev    - SCSI Device in question
913  *              tagged  - Do we use tagged queueing (non-0) or do we treat
914  *                        this device as an untagged device (0)
915  *              tags    - Number of tags allowed if tagged queueing enabled,
916  *                        or number of commands the low level driver can
917  *                        queue up in non-tagged mode (as per cmd_per_lun).
918  *
919  * Returns:     Nothing
920  *
921  * Lock Status: None held on entry
922  *
923  * Notes:       Low level drivers may call this at any time and we will do
924  *              the right thing depending on whether or not the device is
925  *              currently active and whether or not it even has the
926  *              command blocks built yet.
927  */
928 void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
929 {
930         unsigned long flags;
931
932         /*
933          * refuse to set tagged depth to an unworkable size
934          */
935         if (tags <= 0)
936                 return;
937
938         spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
939
940         /* Check to see if the queue is managed by the block layer
941          * if it is, and we fail to adjust the depth, exit */
942         if (blk_queue_tagged(sdev->request_queue) &&
943             blk_queue_resize_tags(sdev->request_queue, tags) != 0)
944                 goto out;
945
946         sdev->queue_depth = tags;
947         switch (tagged) {
948                 case MSG_ORDERED_TAG:
949                         sdev->ordered_tags = 1;
950                         sdev->simple_tags = 1;
951                         break;
952                 case MSG_SIMPLE_TAG:
953                         sdev->ordered_tags = 0;
954                         sdev->simple_tags = 1;
955                         break;
956                 default:
957                         printk(KERN_WARNING "(scsi%d:%d:%d:%d) "
958                                 "scsi_adjust_queue_depth, bad queue type, "
959                                 "disabled\n", sdev->host->host_no,
960                                 sdev->channel, sdev->id, sdev->lun); 
961                 case 0:
962                         sdev->ordered_tags = sdev->simple_tags = 0;
963                         sdev->queue_depth = tags;
964                         break;
965         }
966  out:
967         spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
968 }
969 EXPORT_SYMBOL(scsi_adjust_queue_depth);
970
971 /*
972  * Function:    scsi_track_queue_full()
973  *
974  * Purpose:     This function will track successive QUEUE_FULL events on a
975  *              specific SCSI device to determine if and when there is a
976  *              need to adjust the queue depth on the device.
977  *
978  * Arguments:   sdev    - SCSI Device in question
979  *              depth   - Current number of outstanding SCSI commands on
980  *                        this device, not counting the one returned as
981  *                        QUEUE_FULL.
982  *
983  * Returns:     0 - No change needed
984  *              >0 - Adjust queue depth to this new depth
985  *              -1 - Drop back to untagged operation using host->cmd_per_lun
986  *                      as the untagged command depth
987  *
988  * Lock Status: None held on entry
989  *
990  * Notes:       Low level drivers may call this at any time and we will do
991  *              "The Right Thing."  We are interrupt context safe.
992  */
993 int scsi_track_queue_full(struct scsi_device *sdev, int depth)
994 {
995         if ((jiffies >> 4) == sdev->last_queue_full_time)
996                 return 0;
997
998         sdev->last_queue_full_time = (jiffies >> 4);
999         if (sdev->last_queue_full_depth != depth) {
1000                 sdev->last_queue_full_count = 1;
1001                 sdev->last_queue_full_depth = depth;
1002         } else {
1003                 sdev->last_queue_full_count++;
1004         }
1005
1006         if (sdev->last_queue_full_count <= 10)
1007                 return 0;
1008         if (sdev->last_queue_full_depth < 8) {
1009                 /* Drop back to untagged */
1010                 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
1011                 return -1;
1012         }
1013         
1014         if (sdev->ordered_tags)
1015                 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
1016         else
1017                 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
1018         return depth;
1019 }
1020 EXPORT_SYMBOL(scsi_track_queue_full);
1021
1022 /**
1023  * scsi_device_get  -  get an addition reference to a scsi_device
1024  * @sdev:       device to get a reference to
1025  *
1026  * Gets a reference to the scsi_device and increments the use count
1027  * of the underlying LLDD module.  You must hold host_lock of the
1028  * parent Scsi_Host or already have a reference when calling this.
1029  */
1030 int scsi_device_get(struct scsi_device *sdev)
1031 {
1032         if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
1033                 return -ENXIO;
1034         if (!get_device(&sdev->sdev_gendev))
1035                 return -ENXIO;
1036         if (!try_module_get(sdev->host->hostt->module)) {
1037                 put_device(&sdev->sdev_gendev);
1038                 return -ENXIO;
1039         }
1040         return 0;
1041 }
1042 EXPORT_SYMBOL(scsi_device_get);
1043
1044 /**
1045  * scsi_device_put  -  release a reference to a scsi_device
1046  * @sdev:       device to release a reference on.
1047  *
1048  * Release a reference to the scsi_device and decrements the use count
1049  * of the underlying LLDD module.  The device is freed once the last
1050  * user vanishes.
1051  */
1052 void scsi_device_put(struct scsi_device *sdev)
1053 {
1054         module_put(sdev->host->hostt->module);
1055         put_device(&sdev->sdev_gendev);
1056 }
1057 EXPORT_SYMBOL(scsi_device_put);
1058
1059 /* helper for shost_for_each_device, thus not documented */
1060 struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
1061                                            struct scsi_device *prev)
1062 {
1063         struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
1064         struct scsi_device *next = NULL;
1065         unsigned long flags;
1066
1067         spin_lock_irqsave(shost->host_lock, flags);
1068         while (list->next != &shost->__devices) {
1069                 next = list_entry(list->next, struct scsi_device, siblings);
1070                 /* skip devices that we can't get a reference to */
1071                 if (!scsi_device_get(next))
1072                         break;
1073                 next = NULL;
1074                 list = list->next;
1075         }
1076         spin_unlock_irqrestore(shost->host_lock, flags);
1077
1078         if (prev)
1079                 scsi_device_put(prev);
1080         return next;
1081 }
1082 EXPORT_SYMBOL(__scsi_iterate_devices);
1083
1084 /**
1085  * starget_for_each_device  -  helper to walk all devices of a target
1086  * @starget:    target whose devices we want to iterate over.
1087  *
1088  * This traverses over each devices of @shost.  The devices have
1089  * a reference that must be released by scsi_host_put when breaking
1090  * out of the loop.
1091  */
1092 void starget_for_each_device(struct scsi_target *starget, void * data,
1093                      void (*fn)(struct scsi_device *, void *))
1094 {
1095         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1096         struct scsi_device *sdev;
1097
1098         shost_for_each_device(sdev, shost) {
1099                 if ((sdev->channel == starget->channel) &&
1100                     (sdev->id == starget->id))
1101                         fn(sdev, data);
1102         }
1103 }
1104 EXPORT_SYMBOL(starget_for_each_device);
1105
1106 /**
1107  * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
1108  * @starget:    SCSI target pointer
1109  * @lun:        SCSI Logical Unit Number
1110  *
1111  * Looks up the scsi_device with the specified @lun for a give
1112  * @starget. The returned scsi_device does not have an additional
1113  * reference.  You must hold the host's host_lock over this call and
1114  * any access to the returned scsi_device.
1115  *
1116  * Note:  The only reason why drivers would want to use this is because
1117  * they're need to access the device list in irq context.  Otherwise you
1118  * really want to use scsi_device_lookup_by_target instead.
1119  **/
1120 struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
1121                                                    uint lun)
1122 {
1123         struct scsi_device *sdev;
1124
1125         list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
1126                 if (sdev->lun ==lun)
1127                         return sdev;
1128         }
1129
1130         return NULL;
1131 }
1132 EXPORT_SYMBOL(__scsi_device_lookup_by_target);
1133
1134 /**
1135  * scsi_device_lookup_by_target - find a device given the target
1136  * @starget:    SCSI target pointer
1137  * @lun:        SCSI Logical Unit Number
1138  *
1139  * Looks up the scsi_device with the specified @channel, @id, @lun for a
1140  * give host.  The returned scsi_device has an additional reference that
1141  * needs to be release with scsi_host_put once you're done with it.
1142  **/
1143 struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
1144                                                  uint lun)
1145 {
1146         struct scsi_device *sdev;
1147         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1148         unsigned long flags;
1149
1150         spin_lock_irqsave(shost->host_lock, flags);
1151         sdev = __scsi_device_lookup_by_target(starget, lun);
1152         if (sdev && scsi_device_get(sdev))
1153                 sdev = NULL;
1154         spin_unlock_irqrestore(shost->host_lock, flags);
1155
1156         return sdev;
1157 }
1158 EXPORT_SYMBOL(scsi_device_lookup_by_target);
1159
1160 /**
1161  * scsi_device_lookup - find a device given the host (UNLOCKED)
1162  * @shost:      SCSI host pointer
1163  * @channel:    SCSI channel (zero if only one channel)
1164  * @pun:        SCSI target number (physical unit number)
1165  * @lun:        SCSI Logical Unit Number
1166  *
1167  * Looks up the scsi_device with the specified @channel, @id, @lun for a
1168  * give host. The returned scsi_device does not have an additional reference.
1169  * You must hold the host's host_lock over this call and any access to the
1170  * returned scsi_device.
1171  *
1172  * Note:  The only reason why drivers would want to use this is because
1173  * they're need to access the device list in irq context.  Otherwise you
1174  * really want to use scsi_device_lookup instead.
1175  **/
1176 struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
1177                 uint channel, uint id, uint lun)
1178 {
1179         struct scsi_device *sdev;
1180
1181         list_for_each_entry(sdev, &shost->__devices, siblings) {
1182                 if (sdev->channel == channel && sdev->id == id &&
1183                                 sdev->lun ==lun)
1184                         return sdev;
1185         }
1186
1187         return NULL;
1188 }
1189 EXPORT_SYMBOL(__scsi_device_lookup);
1190
1191 /**
1192  * scsi_device_lookup - find a device given the host
1193  * @shost:      SCSI host pointer
1194  * @channel:    SCSI channel (zero if only one channel)
1195  * @id:         SCSI target number (physical unit number)
1196  * @lun:        SCSI Logical Unit Number
1197  *
1198  * Looks up the scsi_device with the specified @channel, @id, @lun for a
1199  * give host.  The returned scsi_device has an additional reference that
1200  * needs to be release with scsi_host_put once you're done with it.
1201  **/
1202 struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
1203                 uint channel, uint id, uint lun)
1204 {
1205         struct scsi_device *sdev;
1206         unsigned long flags;
1207
1208         spin_lock_irqsave(shost->host_lock, flags);
1209         sdev = __scsi_device_lookup(shost, channel, id, lun);
1210         if (sdev && scsi_device_get(sdev))
1211                 sdev = NULL;
1212         spin_unlock_irqrestore(shost->host_lock, flags);
1213
1214         return sdev;
1215 }
1216 EXPORT_SYMBOL(scsi_device_lookup);
1217
1218 /**
1219  * scsi_device_cancel - cancel outstanding IO to this device
1220  * @sdev:       Pointer to struct scsi_device
1221  * @recovery:   Boolean instructing function to recover device or not.
1222  *
1223  **/
1224 int scsi_device_cancel(struct scsi_device *sdev, int recovery)
1225 {
1226         struct scsi_cmnd *scmd;
1227         LIST_HEAD(active_list);
1228         struct list_head *lh, *lh_sf;
1229         unsigned long flags;
1230
1231         scsi_device_set_state(sdev, SDEV_CANCEL);
1232
1233         spin_lock_irqsave(&sdev->list_lock, flags);
1234         list_for_each_entry(scmd, &sdev->cmd_list, list) {
1235                 if (scmd->request && scmd->request->rq_status != RQ_INACTIVE) {
1236                         /*
1237                          * If we are unable to remove the timer, it means
1238                          * that the command has already timed out or
1239                          * finished.
1240                          */
1241                         if (!scsi_delete_timer(scmd))
1242                                 continue;
1243                         list_add_tail(&scmd->eh_entry, &active_list);
1244                 }
1245         }
1246         spin_unlock_irqrestore(&sdev->list_lock, flags);
1247
1248         if (!list_empty(&active_list)) {
1249                 list_for_each_safe(lh, lh_sf, &active_list) {
1250                         scmd = list_entry(lh, struct scsi_cmnd, eh_entry);
1251                         list_del_init(lh);
1252                         if (recovery) {
1253                                 scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD);
1254                         } else {
1255                                 scmd->result = (DID_ABORT << 16);
1256                                 scsi_finish_command(scmd);
1257                         }
1258                 }
1259         }
1260
1261         return 0;
1262 }
1263 EXPORT_SYMBOL(scsi_device_cancel);
1264
1265 #ifdef CONFIG_HOTPLUG_CPU
1266 static int scsi_cpu_notify(struct notifier_block *self,
1267                            unsigned long action, void *hcpu)
1268 {
1269         int cpu = (unsigned long)hcpu;
1270
1271         switch(action) {
1272         case CPU_DEAD:
1273                 /* Drain scsi_done_q. */
1274                 local_irq_disable();
1275                 list_splice_init(&per_cpu(scsi_done_q, cpu),
1276                                  &__get_cpu_var(scsi_done_q));
1277                 raise_softirq_irqoff(SCSI_SOFTIRQ);
1278                 local_irq_enable();
1279                 break;
1280         default:
1281                 break;
1282         }
1283         return NOTIFY_OK;
1284 }
1285
1286 static struct notifier_block __devinitdata scsi_cpu_nb = {
1287         .notifier_call  = scsi_cpu_notify,
1288 };
1289
1290 #define register_scsi_cpu() register_cpu_notifier(&scsi_cpu_nb)
1291 #define unregister_scsi_cpu() unregister_cpu_notifier(&scsi_cpu_nb)
1292 #else
1293 #define register_scsi_cpu()
1294 #define unregister_scsi_cpu()
1295 #endif /* CONFIG_HOTPLUG_CPU */
1296
1297 MODULE_DESCRIPTION("SCSI core");
1298 MODULE_LICENSE("GPL");
1299
1300 module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
1301 MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
1302
1303 static int __init init_scsi(void)
1304 {
1305         int error, i;
1306
1307         error = scsi_init_queue();
1308         if (error)
1309                 return error;
1310         error = scsi_init_procfs();
1311         if (error)
1312                 goto cleanup_queue;
1313         error = scsi_init_devinfo();
1314         if (error)
1315                 goto cleanup_procfs;
1316         error = scsi_init_hosts();
1317         if (error)
1318                 goto cleanup_devlist;
1319         error = scsi_init_sysctl();
1320         if (error)
1321                 goto cleanup_hosts;
1322         error = scsi_sysfs_register();
1323         if (error)
1324                 goto cleanup_sysctl;
1325
1326         for (i = 0; i < NR_CPUS; i++)
1327                 INIT_LIST_HEAD(&per_cpu(scsi_done_q, i));
1328
1329         devfs_mk_dir("scsi");
1330         open_softirq(SCSI_SOFTIRQ, scsi_softirq, NULL);
1331         register_scsi_cpu();
1332         printk(KERN_NOTICE "SCSI subsystem initialized\n");
1333         return 0;
1334
1335 cleanup_sysctl:
1336         scsi_exit_sysctl();
1337 cleanup_hosts:
1338         scsi_exit_hosts();
1339 cleanup_devlist:
1340         scsi_exit_devinfo();
1341 cleanup_procfs:
1342         scsi_exit_procfs();
1343 cleanup_queue:
1344         scsi_exit_queue();
1345         printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
1346                -error);
1347         return error;
1348 }
1349
1350 static void __exit exit_scsi(void)
1351 {
1352         scsi_sysfs_unregister();
1353         scsi_exit_sysctl();
1354         scsi_exit_hosts();
1355         scsi_exit_devinfo();
1356         devfs_remove("scsi");
1357         scsi_exit_procfs();
1358         scsi_exit_queue();
1359         unregister_scsi_cpu();
1360 }
1361
1362 subsys_initcall(init_scsi);
1363 module_exit(exit_scsi);