CTC++ Coverage Report - Execution Profile    #596/1532

Files Summary | Functions Summary | Execution Profile | Index | No Index
First | Previous | Next | Last


File: drivers/scsi/scsi.c
Instrumentation mode: function-decision-multicondition
TER: 28 % (121/440)

Start/ End/    
True False - Line Source

  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 #include <linux/mutex.h>
  59 
  60 #include <scsi/scsi.h>
  61 #include <scsi/scsi_cmnd.h>
  62 #include <scsi/scsi_dbg.h>
  63 #include <scsi/scsi_device.h>
  64 #include <scsi/scsi_eh.h>
  65 #include <scsi/scsi_host.h>
  66 #include <scsi/scsi_tcq.h>
  67 #include <scsi/scsi_request.h>
  68 
  69 #include "scsi_priv.h"
  70 #include "scsi_logging.h"
  71 
  72 static void scsi_done(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    "Direct-Access-RBC",
  117 };
  118 EXPORT_SYMBOL(scsi_device_types);
  119 
  120 /*
  121  * Function:    scsi_allocate_request
  122  *
  123  * Purpose:     Allocate a request descriptor.
  124  *
  125  * Arguments:   device      - device for which we want a request
  126  *      gfp_mask   - allocation flags passed to kmalloc
  127  *
  128  * Lock status: No locks assumed to be held.  This function is SMP-safe.
  129  *
  130  * Returns:     Pointer to request block.
  131  */
 
- 132 struct scsi_request *scsi_allocate_request(struct scsi_device *sdev,
  133                   gfp_t gfp_mask)
  134 {
  135    const int offset = ALIGN(sizeof(struct scsi_request), 4);
  136    const int size = offset + sizeof(struct request);
  137    struct scsi_request *sreq;
  138   
  139    sreq = kmalloc(size, gfp_mask);
- 140    if (likely(sreq != NULL)) {
  141       memset(sreq, 0, size);
  142       sreq->sr_request = (struct request *)(((char *)sreq) + offset);
  143       sreq->sr_device = sdev;
  144       sreq->sr_host = sdev->host;
  145       sreq->sr_magic = SCSI_REQ_MAGIC;
  146       sreq->sr_data_direction = DMA_BIDIRECTIONAL;
  147    }
  148 
 - 149    return sreq;
  150 }
  151 EXPORT_SYMBOL(scsi_allocate_request);
  152 
 
- 153 void __scsi_release_request(struct scsi_request *sreq)
  154 {
  155    struct request *req = sreq->sr_request;
  156 
  157    /* unlikely because the tag was usually ended earlier by the
  158     * mid-layer. However, for layering reasons ULD's don't end
  159     * the tag of commands they generate. */
- 160    if (unlikely(blk_rq_tagged(req))) {
  161       unsigned long flags;
  162       struct request_queue *q = req->q;
  163 
    164       spin_lock_irqsave(q->queue_lock, flags);
    164     do
    164       do
- 164       do-while (0)
- 164     do-while (0)
    164     do
    164       do
- 164       do-while (0)
- 164     do-while (0)
- 164   do-while (0)
  165       blk_queue_end_tag(q, req);
    166       spin_unlock_irqrestore(q->queue_lock, flags);
    166     do
    166       do
- 166       do-while (0)
- 166     do-while (0)
- 166   do-while (0)
  167    }
  168 
  169 
- 170    if (likely(sreq->sr_command != NULL)) {
  171       struct scsi_cmnd *cmd = sreq->sr_command;
  172 
  173       sreq->sr_command = NULL;
  174       scsi_next_command(cmd);
  175    }
  176 }
  177 
  178 /*
  179  * Function:    scsi_release_request
  180  *
  181  * Purpose:     Release a request descriptor.
  182  *
  183  * Arguments:   sreq    - request to release
  184  *
  185  * Lock status: No locks assumed to be held.  This function is SMP-safe.
  186  */
 
- 187 void scsi_release_request(struct scsi_request *sreq)
  188 {
  189    __scsi_release_request(sreq);
  190    kfree(sreq);
  191 }
  192 EXPORT_SYMBOL(scsi_release_request);
  193 
  194 struct scsi_host_cmd_pool {
  195    kmem_cache_t   *slab;
  196    unsigned int   users;
  197    char      *name;
  198    unsigned int   slab_flags;
  199    gfp_t      gfp_mask;
  200 };
  201 
  202 static struct scsi_host_cmd_pool scsi_cmd_pool = {
  203    .name      = "scsi_cmd_cache",
  204    .slab_flags   = SLAB_HWCACHE_ALIGN,
  205 };
  206 
  207 static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
  208    .name      = "scsi_cmd_cache(DMA)",
  209    .slab_flags   = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
  210    .gfp_mask   = __GFP_DMA,
  211 };
  212 
  213 static DEFINE_MUTEX(host_cmd_pool_mutex);
  214 
 
2098   215 static struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost,
  216                    gfp_t gfp_mask)
  217 {
  218    struct scsi_cmnd *cmd;
  219 
  220    cmd = kmem_cache_alloc(shost->cmd_pool->slab,
  221          gfp_mask | shost->cmd_pool->gfp_mask);
  222 
2098 - 223    if (unlikely(!cmd)) {
  224       unsigned long flags;
  225 
    226       spin_lock_irqsave(&shost->free_list_lock, flags);
    226     do
    226       do
- 226       do-while (0)
- 226     do-while (0)
    226     do
    226       do
- 226       do-while (0)
- 226     do-while (0)
- 226   do-while (0)
- 227       if (likely(!list_empty(&shost->free_list))) {
  228          cmd = list_entry(shost->free_list.next,
  229                 struct scsi_cmnd, list);
  230          list_del_init(&cmd->list);
  231       }
    232       spin_unlock_irqrestore(&shost->free_list_lock, flags);
    232     do
    232       do
- 232       do-while (0)
- 232     do-while (0)
- 232   do-while (0)
  233    }
  234 
2098    235    return cmd;
  236 }
  237 
  238 /*
  239  * Function:   scsi_get_command()
  240  *
  241  * Purpose:   Allocate and setup a scsi command block
  242  *
  243  * Arguments:   dev   - parent scsi device
  244  *      gfp_mask- allocator flags
  245  *
  246  * Returns:   The allocated scsi command structure.
  247  */
 
2098   248 struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask)
  249 {
  250    struct scsi_cmnd *cmd;
  251 
  252    /* Bail if we can't get a reference to the device */
2098 - 253    if (!get_device(&dev->sdev_gendev))
 - 254       return NULL;
  255 
  256    cmd = __scsi_get_command(dev->host, gfp_mask);
  257 
2098 - 258    if (likely(cmd != NULL)) {
  259       unsigned long flags;
  260 
  261       memset(cmd, 0, sizeof(*cmd));
  262       cmd->device = dev;
  263       init_timer(&cmd->eh_timeout);
  264       INIT_LIST_HEAD(&cmd->list);
    265       spin_lock_irqsave(&dev->list_lock, flags);
    265     do
    265       do
2098 - 265       do-while (0)
2098 - 265     do-while (0)
    265     do
    265       do
2098 - 265       do-while (0)
2098 - 265     do-while (0)
2098 - 265   do-while (0)
  266       list_add_tail(&cmd->list, &dev->cmd_list);
    267       spin_unlock_irqrestore(&dev->list_lock, flags);
    267     do
    267       do
2098 - 267       do-while (0)
2098 - 267     do-while (0)
2098 - 267   do-while (0)
  268       cmd->jiffies_at_alloc = jiffies;
    269    } else
  270       put_device(&dev->sdev_gendev);
  271 
2098    272    return cmd;
  273 }            
  274 EXPORT_SYMBOL(scsi_get_command);
  275 
  276 /*
  277  * Function:   scsi_put_command()
  278  *
  279  * Purpose:   Free a scsi command block
  280  *
  281  * Arguments:   cmd   - command block to free
  282  *
  283  * Returns:   Nothing.
  284  *
  285  * Notes:   The command must not belong to any lists.
  286  */
 
2098 2098   287 void scsi_put_command(struct scsi_cmnd *cmd)
  288 {
  289    struct scsi_device *sdev = cmd->device;
  290    struct Scsi_Host *shost = sdev->host;
  291    unsigned long flags;
  292    
  293    /* serious error if the command hasn't come from a device list */
    294    spin_lock_irqsave(&cmd->device->list_lock, flags);
    294   do
    294     do
2098 - 294     do-while (0)
2098 - 294   do-while (0)
    294   do
    294     do
2098 - 294     do-while (0)
2098 - 294   do-while (0)
2098 - 294 do-while (0)
    295    BUG_ON(list_empty(&cmd->list));
2098 - 295   if (__builtin_expect ( ! ! ( ( list_empty ( ..
2098 - 295 do-while (0)
  296    list_del_init(&cmd->list);
    297    spin_unlock(&cmd->device->list_lock);
    297   do
2098 - 297   do-while (0)
2098 - 297 do-while (0)
  298    /* changing locks here, don't need to restore the irq state */
    299    spin_lock(&shost->free_list_lock);
    299   do
2098 - 299   do-while (0)
2098 - 299 do-while (0)
2098 - 300    if (unlikely(list_empty(&shost->free_list))) {
  301       list_add(&cmd->list, &shost->free_list);
  302       cmd = NULL;
  303    }
    304    spin_unlock_irqrestore(&shost->free_list_lock, flags);
    304   do
    304     do
2098 - 304     do-while (0)
2098 - 304   do-while (0)
2098 - 304 do-while (0)
  305 
2098 - 306    if (likely(cmd != NULL))
  307       kmem_cache_free(shost->cmd_pool->slab, cmd);
  308 
  309    put_device(&sdev->sdev_gendev);
  310 }
  311 EXPORT_SYMBOL(scsi_put_command);
  312 
  313 /*
  314  * Function:   scsi_setup_command_freelist()
  315  *
  316  * Purpose:   Setup the command freelist for a scsi host.
  317  *
  318  * Arguments:   shost   - host to allocate the freelist for.
  319  *
  320  * Returns:   Nothing.
  321  */
 
  322 int scsi_setup_command_freelist(struct Scsi_Host *shost)
  323 {
  324    struct scsi_host_cmd_pool *pool;
  325    struct scsi_cmnd *cmd;
  326 
    327    spin_lock_init(&shost->free_list_lock);
- 327 do-while (0)
  328    INIT_LIST_HEAD(&shost->free_list);
  329 
  330    /*
  331     * Select a command slab for this host and create it if not
  332     * yet existant.
  333     */
  334    mutex_lock(&host_cmd_pool_mutex);
    335    pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool);
- 335 ternary-?: shost -> unchecked_isa_dma
- 336    if (!pool->users) {
  337       pool->slab = kmem_cache_create(pool->name,
  338             sizeof(struct scsi_cmnd), 0,
  339             pool->slab_flags, NULL, NULL);
- 340       if (!pool->slab)
 - 341          goto fail;
  342    }
  343 
  344    pool->users++;
  345    shost->cmd_pool = pool;
  346    mutex_unlock(&host_cmd_pool_mutex);
  347 
  348    /*
  349     * Get one backup command for this host.
  350     */
  351    cmd = kmem_cache_alloc(shost->cmd_pool->slab,
  352          GFP_KERNEL | shost->cmd_pool->gfp_mask);
- 353    if (!cmd)
 - 354       goto fail2;
  355    list_add(&cmd->list, &shost->free_list);      
   356    return 0;
  357 
  358  fail2:
- 359    if (!--pool->users)
  360       kmem_cache_destroy(pool->slab);
 - 361    return -ENOMEM;
  362  fail:
  363    mutex_unlock(&host_cmd_pool_mutex);
 - 364    return -ENOMEM;
  365 
  366 }
  367 
  368 /*
  369  * Function:   scsi_destroy_command_freelist()
  370  *
  371  * Purpose:   Release the command freelist for a scsi host.
  372  *
  373  * Arguments:   shost   - host that's freelist is going to be destroyed
  374  */
 
- 375 void scsi_destroy_command_freelist(struct Scsi_Host *shost)
  376 {
- 377    while (!list_empty(&shost->free_list)) {
  378       struct scsi_cmnd *cmd;
  379 
  380       cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
  381       list_del_init(&cmd->list);
  382       kmem_cache_free(shost->cmd_pool->slab, cmd);
  383    }
  384 
  385    mutex_lock(&host_cmd_pool_mutex);
- 386    if (!--shost->cmd_pool->users)
  387       kmem_cache_destroy(shost->cmd_pool->slab);
  388    mutex_unlock(&host_cmd_pool_mutex);
  389 }
  390 
  391 #ifdef CONFIG_SCSI_LOGGING
  392 void scsi_log_send(struct scsi_cmnd *cmd)
  393 {
  394    unsigned int level;
  395    struct scsi_device *sdev;
  396 
  397    /*
  398     * If ML QUEUE log level is greater than or equal to:
  399     *
  400     * 1: nothing (match completion)
  401     *
  402     * 2: log opcode + command of all commands
  403     *
  404     * 3: same as 2 plus dump cmd address
  405     *
  406     * 4: same as 3 plus dump extra junk
  407     */
  408    if (unlikely(scsi_logging_level)) {
  409       level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
  410                    SCSI_LOG_MLQUEUE_BITS);
  411       if (level > 1) {
  412          sdev = cmd->device;
  413          sdev_printk(KERN_INFO, sdev, "send ");
  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          sdev_printk(KERN_INFO, sdev, "done ");
  458          if (level > 2)
  459             printk("0x%p ", cmd);
  460          /*
  461           * Dump truncated values, so we usually fit within
  462           * 80 chars.
  463           */
  464          switch (disposition) {
  465          case SUCCESS:
  466             printk("SUCCESS");
  467             break;
  468          case NEEDS_RETRY:
  469             printk("RETRY  ");
  470             break;
  471          case ADD_TO_MLQUEUE:
  472             printk("MLQUEUE");
  473             break;
  474          case FAILED:
  475             printk("FAILED ");
  476             break;
  477          case TIMEOUT_ERROR:
  478             /* 
  479              * If called via scsi_times_out.
  480              */
  481             printk("TIMEOUT");
  482             break;
  483          default:
  484             printk("UNKNOWN");
  485          }
  486          printk(" %8x ", cmd->result);
  487          scsi_print_command(cmd);
  488          if (status_byte(cmd->result) & CHECK_CONDITION) {
  489             /*
  490              * XXX The scsi_print_sense formatting/prefix
  491              * doesn't match this function.
  492              */
  493             scsi_print_sense("", cmd);
  494          }
  495          if (level > 3) {
  496             printk(KERN_INFO "