11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * scsi.c Copyright (C) 1992 Drew Eckhardt 31da177e4SLinus Torvalds * Copyright (C) 1993, 1994, 1995, 1999 Eric Youngdale 41da177e4SLinus Torvalds * Copyright (C) 2002, 2003 Christoph Hellwig 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * generic mid-level SCSI driver 71da177e4SLinus Torvalds * Initial versions: Drew Eckhardt 81da177e4SLinus Torvalds * Subsequent revisions: Eric Youngdale 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * <drew@colorado.edu> 111da177e4SLinus Torvalds * 121da177e4SLinus Torvalds * Bug correction thanks go to : 131da177e4SLinus Torvalds * Rik Faith <faith@cs.unc.edu> 141da177e4SLinus Torvalds * Tommy Thorn <tthorn> 151da177e4SLinus Torvalds * Thomas Wuensche <tw@fgb1.fgb.mw.tu-muenchen.de> 161da177e4SLinus Torvalds * 171da177e4SLinus Torvalds * Modified by Eric Youngdale eric@andante.org or ericy@gnu.ai.mit.edu to 181da177e4SLinus Torvalds * add scatter-gather, multiple outstanding request, and other 191da177e4SLinus Torvalds * enhancements. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Native multichannel, wide scsi, /proc/scsi and hot plugging 221da177e4SLinus Torvalds * support added by Michael Neuffer <mike@i-connect.net> 231da177e4SLinus Torvalds * 241da177e4SLinus Torvalds * Added request_module("scsi_hostadapter") for kerneld: 251da177e4SLinus Torvalds * (Put an "alias scsi_hostadapter your_hostadapter" in /etc/modprobe.conf) 261da177e4SLinus Torvalds * Bjorn Ekwall <bj0rn@blox.se> 271da177e4SLinus Torvalds * (changed to kmod) 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * Major improvements to the timeout, abort, and reset processing, 301da177e4SLinus Torvalds * as well as performance modifications for large queue depths by 311da177e4SLinus Torvalds * Leonard N. Zubkoff <lnz@dandelion.com> 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Converted cli() code to spinlocks, Ingo Molnar 341da177e4SLinus Torvalds * 351da177e4SLinus Torvalds * Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli 361da177e4SLinus Torvalds * 371da177e4SLinus Torvalds * out_of_space hacks, D. Gilbert (dpg) 990608 381da177e4SLinus Torvalds */ 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #include <linux/module.h> 411da177e4SLinus Torvalds #include <linux/moduleparam.h> 421da177e4SLinus Torvalds #include <linux/kernel.h> 431da177e4SLinus Torvalds #include <linux/sched.h> 441da177e4SLinus Torvalds #include <linux/timer.h> 451da177e4SLinus Torvalds #include <linux/string.h> 461da177e4SLinus Torvalds #include <linux/slab.h> 471da177e4SLinus Torvalds #include <linux/blkdev.h> 481da177e4SLinus Torvalds #include <linux/delay.h> 491da177e4SLinus Torvalds #include <linux/init.h> 501da177e4SLinus Torvalds #include <linux/completion.h> 511da177e4SLinus Torvalds #include <linux/devfs_fs_kernel.h> 521da177e4SLinus Torvalds #include <linux/unistd.h> 531da177e4SLinus Torvalds #include <linux/spinlock.h> 541da177e4SLinus Torvalds #include <linux/kmod.h> 551da177e4SLinus Torvalds #include <linux/interrupt.h> 561da177e4SLinus Torvalds #include <linux/notifier.h> 571da177e4SLinus Torvalds #include <linux/cpu.h> 580b950672SArjan van de Ven #include <linux/mutex.h> 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds #include <scsi/scsi.h> 611da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h> 621da177e4SLinus Torvalds #include <scsi/scsi_dbg.h> 631da177e4SLinus Torvalds #include <scsi/scsi_device.h> 641da177e4SLinus Torvalds #include <scsi/scsi_eh.h> 651da177e4SLinus Torvalds #include <scsi/scsi_host.h> 661da177e4SLinus Torvalds #include <scsi/scsi_tcq.h> 671da177e4SLinus Torvalds #include <scsi/scsi_request.h> 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #include "scsi_priv.h" 701da177e4SLinus Torvalds #include "scsi_logging.h" 711da177e4SLinus Torvalds 7252c1da39SAdrian Bunk static void scsi_done(struct scsi_cmnd *cmd); 731da177e4SLinus Torvalds 741da177e4SLinus Torvalds /* 751da177e4SLinus Torvalds * Definitions and constants. 761da177e4SLinus Torvalds */ 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds #define MIN_RESET_DELAY (2*HZ) 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /* Do not call reset on error if we just did a reset within 15 sec. */ 811da177e4SLinus Torvalds #define MIN_RESET_PERIOD (15*HZ) 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds /* 841da177e4SLinus Torvalds * Macro to determine the size of SCSI command. This macro takes vendor 851da177e4SLinus Torvalds * unique commands into account. SCSI commands in groups 6 and 7 are 861da177e4SLinus Torvalds * vendor unique and we will depend upon the command length being 871da177e4SLinus Torvalds * supplied correctly in cmd_len. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds #define CDB_SIZE(cmd) (((((cmd)->cmnd[0] >> 5) & 7) < 6) ? \ 901da177e4SLinus Torvalds COMMAND_SIZE((cmd)->cmnd[0]) : (cmd)->cmd_len) 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* 931da177e4SLinus Torvalds * Note - the initial logging level can be set here to log events at boot time. 941da177e4SLinus Torvalds * After the system is up, you may enable logging via the /proc interface. 951da177e4SLinus Torvalds */ 961da177e4SLinus Torvalds unsigned int scsi_logging_level; 971da177e4SLinus Torvalds #if defined(CONFIG_SCSI_LOGGING) 981da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_logging_level); 991da177e4SLinus Torvalds #endif 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] = { 1021da177e4SLinus Torvalds "Direct-Access ", 1031da177e4SLinus Torvalds "Sequential-Access", 1041da177e4SLinus Torvalds "Printer ", 1051da177e4SLinus Torvalds "Processor ", 1061da177e4SLinus Torvalds "WORM ", 1071da177e4SLinus Torvalds "CD-ROM ", 1081da177e4SLinus Torvalds "Scanner ", 1091da177e4SLinus Torvalds "Optical Device ", 1101da177e4SLinus Torvalds "Medium Changer ", 1111da177e4SLinus Torvalds "Communications ", 1121da177e4SLinus Torvalds "Unknown ", 1131da177e4SLinus Torvalds "Unknown ", 1141da177e4SLinus Torvalds "RAID ", 1151da177e4SLinus Torvalds "Enclosure ", 1167f602c53SJames Bottomley "Direct-Access-RBC", 1171da177e4SLinus Torvalds }; 1181da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_types); 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds /* 1211da177e4SLinus Torvalds * Function: scsi_allocate_request 1221da177e4SLinus Torvalds * 1231da177e4SLinus Torvalds * Purpose: Allocate a request descriptor. 1241da177e4SLinus Torvalds * 1251da177e4SLinus Torvalds * Arguments: device - device for which we want a request 1261da177e4SLinus Torvalds * gfp_mask - allocation flags passed to kmalloc 1271da177e4SLinus Torvalds * 1281da177e4SLinus Torvalds * Lock status: No locks assumed to be held. This function is SMP-safe. 1291da177e4SLinus Torvalds * 1301da177e4SLinus Torvalds * Returns: Pointer to request block. 1311da177e4SLinus Torvalds */ 1321da177e4SLinus Torvalds struct scsi_request *scsi_allocate_request(struct scsi_device *sdev, 133c53033f6SAl Viro gfp_t gfp_mask) 1341da177e4SLinus Torvalds { 1351da177e4SLinus Torvalds const int offset = ALIGN(sizeof(struct scsi_request), 4); 1361da177e4SLinus Torvalds const int size = offset + sizeof(struct request); 1371da177e4SLinus Torvalds struct scsi_request *sreq; 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds sreq = kmalloc(size, gfp_mask); 1401da177e4SLinus Torvalds if (likely(sreq != NULL)) { 1411da177e4SLinus Torvalds memset(sreq, 0, size); 1421da177e4SLinus Torvalds sreq->sr_request = (struct request *)(((char *)sreq) + offset); 1431da177e4SLinus Torvalds sreq->sr_device = sdev; 1441da177e4SLinus Torvalds sreq->sr_host = sdev->host; 1451da177e4SLinus Torvalds sreq->sr_magic = SCSI_REQ_MAGIC; 1461da177e4SLinus Torvalds sreq->sr_data_direction = DMA_BIDIRECTIONAL; 1471da177e4SLinus Torvalds } 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds return sreq; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_allocate_request); 1521da177e4SLinus Torvalds 1531da177e4SLinus Torvalds void __scsi_release_request(struct scsi_request *sreq) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds struct request *req = sreq->sr_request; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* unlikely because the tag was usually ended earlier by the 1581da177e4SLinus Torvalds * mid-layer. However, for layering reasons ULD's don't end 1591da177e4SLinus Torvalds * the tag of commands they generate. */ 1601da177e4SLinus Torvalds if (unlikely(blk_rq_tagged(req))) { 1611da177e4SLinus Torvalds unsigned long flags; 1621da177e4SLinus Torvalds struct request_queue *q = req->q; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds spin_lock_irqsave(q->queue_lock, flags); 1651da177e4SLinus Torvalds blk_queue_end_tag(q, req); 1661da177e4SLinus Torvalds spin_unlock_irqrestore(q->queue_lock, flags); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds if (likely(sreq->sr_command != NULL)) { 1711da177e4SLinus Torvalds struct scsi_cmnd *cmd = sreq->sr_command; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds sreq->sr_command = NULL; 1741da177e4SLinus Torvalds scsi_next_command(cmd); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds /* 1791da177e4SLinus Torvalds * Function: scsi_release_request 1801da177e4SLinus Torvalds * 1811da177e4SLinus Torvalds * Purpose: Release a request descriptor. 1821da177e4SLinus Torvalds * 1831da177e4SLinus Torvalds * Arguments: sreq - request to release 1841da177e4SLinus Torvalds * 1851da177e4SLinus Torvalds * Lock status: No locks assumed to be held. This function is SMP-safe. 1861da177e4SLinus Torvalds */ 1871da177e4SLinus Torvalds void scsi_release_request(struct scsi_request *sreq) 1881da177e4SLinus Torvalds { 1891da177e4SLinus Torvalds __scsi_release_request(sreq); 1901da177e4SLinus Torvalds kfree(sreq); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_release_request); 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds struct scsi_host_cmd_pool { 1951da177e4SLinus Torvalds kmem_cache_t *slab; 1961da177e4SLinus Torvalds unsigned int users; 1971da177e4SLinus Torvalds char *name; 1981da177e4SLinus Torvalds unsigned int slab_flags; 199c53033f6SAl Viro gfp_t gfp_mask; 2001da177e4SLinus Torvalds }; 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_pool = { 2031da177e4SLinus Torvalds .name = "scsi_cmd_cache", 2041da177e4SLinus Torvalds .slab_flags = SLAB_HWCACHE_ALIGN, 2051da177e4SLinus Torvalds }; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_dma_pool = { 2081da177e4SLinus Torvalds .name = "scsi_cmd_cache(DMA)", 2091da177e4SLinus Torvalds .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA, 2101da177e4SLinus Torvalds .gfp_mask = __GFP_DMA, 2111da177e4SLinus Torvalds }; 2121da177e4SLinus Torvalds 2130b950672SArjan van de Ven static DEFINE_MUTEX(host_cmd_pool_mutex); 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds static struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost, 216c53033f6SAl Viro gfp_t gfp_mask) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds struct scsi_cmnd *cmd; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds cmd = kmem_cache_alloc(shost->cmd_pool->slab, 2211da177e4SLinus Torvalds gfp_mask | shost->cmd_pool->gfp_mask); 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds if (unlikely(!cmd)) { 2241da177e4SLinus Torvalds unsigned long flags; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds spin_lock_irqsave(&shost->free_list_lock, flags); 2271da177e4SLinus Torvalds if (likely(!list_empty(&shost->free_list))) { 2281da177e4SLinus Torvalds cmd = list_entry(shost->free_list.next, 2291da177e4SLinus Torvalds struct scsi_cmnd, list); 2301da177e4SLinus Torvalds list_del_init(&cmd->list); 2311da177e4SLinus Torvalds } 2321da177e4SLinus Torvalds spin_unlock_irqrestore(&shost->free_list_lock, flags); 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds return cmd; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds /* 2391da177e4SLinus Torvalds * Function: scsi_get_command() 2401da177e4SLinus Torvalds * 2411da177e4SLinus Torvalds * Purpose: Allocate and setup a scsi command block 2421da177e4SLinus Torvalds * 2431da177e4SLinus Torvalds * Arguments: dev - parent scsi device 2441da177e4SLinus Torvalds * gfp_mask- allocator flags 2451da177e4SLinus Torvalds * 2461da177e4SLinus Torvalds * Returns: The allocated scsi command structure. 2471da177e4SLinus Torvalds */ 248c53033f6SAl Viro struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask) 2491da177e4SLinus Torvalds { 2501da177e4SLinus Torvalds struct scsi_cmnd *cmd; 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds /* Bail if we can't get a reference to the device */ 2531da177e4SLinus Torvalds if (!get_device(&dev->sdev_gendev)) 2541da177e4SLinus Torvalds return NULL; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds cmd = __scsi_get_command(dev->host, gfp_mask); 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds if (likely(cmd != NULL)) { 2591da177e4SLinus Torvalds unsigned long flags; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds memset(cmd, 0, sizeof(*cmd)); 2621da177e4SLinus Torvalds cmd->device = dev; 2631da177e4SLinus Torvalds init_timer(&cmd->eh_timeout); 2641da177e4SLinus Torvalds INIT_LIST_HEAD(&cmd->list); 2651da177e4SLinus Torvalds spin_lock_irqsave(&dev->list_lock, flags); 2661da177e4SLinus Torvalds list_add_tail(&cmd->list, &dev->cmd_list); 2671da177e4SLinus Torvalds spin_unlock_irqrestore(&dev->list_lock, flags); 26879e448bfSMatthew Dobson cmd->jiffies_at_alloc = jiffies; 2691da177e4SLinus Torvalds } else 2701da177e4SLinus Torvalds put_device(&dev->sdev_gendev); 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds return cmd; 2731da177e4SLinus Torvalds } 2741da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_get_command); 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds /* 2771da177e4SLinus Torvalds * Function: scsi_put_command() 2781da177e4SLinus Torvalds * 2791da177e4SLinus Torvalds * Purpose: Free a scsi command block 2801da177e4SLinus Torvalds * 2811da177e4SLinus Torvalds * Arguments: cmd - command block to free 2821da177e4SLinus Torvalds * 2831da177e4SLinus Torvalds * Returns: Nothing. 2841da177e4SLinus Torvalds * 2851da177e4SLinus Torvalds * Notes: The command must not belong to any lists. 2861da177e4SLinus Torvalds */ 2871da177e4SLinus Torvalds void scsi_put_command(struct scsi_cmnd *cmd) 2881da177e4SLinus Torvalds { 2891da177e4SLinus Torvalds struct scsi_device *sdev = cmd->device; 2901da177e4SLinus Torvalds struct Scsi_Host *shost = sdev->host; 2911da177e4SLinus Torvalds unsigned long flags; 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds /* serious error if the command hasn't come from a device list */ 2941da177e4SLinus Torvalds spin_lock_irqsave(&cmd->device->list_lock, flags); 2951da177e4SLinus Torvalds BUG_ON(list_empty(&cmd->list)); 2961da177e4SLinus Torvalds list_del_init(&cmd->list); 2971da177e4SLinus Torvalds spin_unlock(&cmd->device->list_lock); 2981da177e4SLinus Torvalds /* changing locks here, don't need to restore the irq state */ 2991da177e4SLinus Torvalds spin_lock(&shost->free_list_lock); 3001da177e4SLinus Torvalds if (unlikely(list_empty(&shost->free_list))) { 3011da177e4SLinus Torvalds list_add(&cmd->list, &shost->free_list); 3021da177e4SLinus Torvalds cmd = NULL; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds spin_unlock_irqrestore(&shost->free_list_lock, flags); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds if (likely(cmd != NULL)) 3071da177e4SLinus Torvalds kmem_cache_free(shost->cmd_pool->slab, cmd); 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds put_device(&sdev->sdev_gendev); 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_put_command); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds /* 3141da177e4SLinus Torvalds * Function: scsi_setup_command_freelist() 3151da177e4SLinus Torvalds * 3161da177e4SLinus Torvalds * Purpose: Setup the command freelist for a scsi host. 3171da177e4SLinus Torvalds * 3181da177e4SLinus Torvalds * Arguments: shost - host to allocate the freelist for. 3191da177e4SLinus Torvalds * 3201da177e4SLinus Torvalds * Returns: Nothing. 3211da177e4SLinus Torvalds */ 3221da177e4SLinus Torvalds int scsi_setup_command_freelist(struct Scsi_Host *shost) 3231da177e4SLinus Torvalds { 3241da177e4SLinus Torvalds struct scsi_host_cmd_pool *pool; 3251da177e4SLinus Torvalds struct scsi_cmnd *cmd; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds spin_lock_init(&shost->free_list_lock); 3281da177e4SLinus Torvalds INIT_LIST_HEAD(&shost->free_list); 3291da177e4SLinus Torvalds 3301da177e4SLinus Torvalds /* 3311da177e4SLinus Torvalds * Select a command slab for this host and create it if not 3321da177e4SLinus Torvalds * yet existant. 3331da177e4SLinus Torvalds */ 3340b950672SArjan van de Ven mutex_lock(&host_cmd_pool_mutex); 3351da177e4SLinus Torvalds pool = (shost->unchecked_isa_dma ? &scsi_cmd_dma_pool : &scsi_cmd_pool); 3361da177e4SLinus Torvalds if (!pool->users) { 3371da177e4SLinus Torvalds pool->slab = kmem_cache_create(pool->name, 3381da177e4SLinus Torvalds sizeof(struct scsi_cmnd), 0, 3391da177e4SLinus Torvalds pool->slab_flags, NULL, NULL); 3401da177e4SLinus Torvalds if (!pool->slab) 3411da177e4SLinus Torvalds goto fail; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds pool->users++; 3451da177e4SLinus Torvalds shost->cmd_pool = pool; 3460b950672SArjan van de Ven mutex_unlock(&host_cmd_pool_mutex); 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds /* 3491da177e4SLinus Torvalds * Get one backup command for this host. 3501da177e4SLinus Torvalds */ 3511da177e4SLinus Torvalds cmd = kmem_cache_alloc(shost->cmd_pool->slab, 3521da177e4SLinus Torvalds GFP_KERNEL | shost->cmd_pool->gfp_mask); 3531da177e4SLinus Torvalds if (!cmd) 3541da177e4SLinus Torvalds goto fail2; 3551da177e4SLinus Torvalds list_add(&cmd->list, &shost->free_list); 3561da177e4SLinus Torvalds return 0; 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds fail2: 3591da177e4SLinus Torvalds if (!--pool->users) 3601da177e4SLinus Torvalds kmem_cache_destroy(pool->slab); 3611da177e4SLinus Torvalds return -ENOMEM; 3621da177e4SLinus Torvalds fail: 3630b950672SArjan van de Ven mutex_unlock(&host_cmd_pool_mutex); 3641da177e4SLinus Torvalds return -ENOMEM; 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds 3681da177e4SLinus Torvalds /* 3691da177e4SLinus Torvalds * Function: scsi_destroy_command_freelist() 3701da177e4SLinus Torvalds * 3711da177e4SLinus Torvalds * Purpose: Release the command freelist for a scsi host. 3721da177e4SLinus Torvalds * 3731da177e4SLinus Torvalds * Arguments: shost - host that's freelist is going to be destroyed 3741da177e4SLinus Torvalds */ 3751da177e4SLinus Torvalds void scsi_destroy_command_freelist(struct Scsi_Host *shost) 3761da177e4SLinus Torvalds { 3771da177e4SLinus Torvalds while (!list_empty(&shost->free_list)) { 3781da177e4SLinus Torvalds struct scsi_cmnd *cmd; 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list); 3811da177e4SLinus Torvalds list_del_init(&cmd->list); 3821da177e4SLinus Torvalds kmem_cache_free(shost->cmd_pool->slab, cmd); 3831da177e4SLinus Torvalds } 3841da177e4SLinus Torvalds 3850b950672SArjan van de Ven mutex_lock(&host_cmd_pool_mutex); 3861da177e4SLinus Torvalds if (!--shost->cmd_pool->users) 3871da177e4SLinus Torvalds kmem_cache_destroy(shost->cmd_pool->slab); 3880b950672SArjan van de Ven mutex_unlock(&host_cmd_pool_mutex); 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds #ifdef CONFIG_SCSI_LOGGING 3921da177e4SLinus Torvalds void scsi_log_send(struct scsi_cmnd *cmd) 3931da177e4SLinus Torvalds { 3941da177e4SLinus Torvalds unsigned int level; 3951da177e4SLinus Torvalds struct scsi_device *sdev; 3961da177e4SLinus Torvalds 3971da177e4SLinus Torvalds /* 3981da177e4SLinus Torvalds * If ML QUEUE log level is greater than or equal to: 3991da177e4SLinus Torvalds * 4001da177e4SLinus Torvalds * 1: nothing (match completion) 4011da177e4SLinus Torvalds * 4021da177e4SLinus Torvalds * 2: log opcode + command of all commands 4031da177e4SLinus Torvalds * 4041da177e4SLinus Torvalds * 3: same as 2 plus dump cmd address 4051da177e4SLinus Torvalds * 4061da177e4SLinus Torvalds * 4: same as 3 plus dump extra junk 4071da177e4SLinus Torvalds */ 4081da177e4SLinus Torvalds if (unlikely(scsi_logging_level)) { 4091da177e4SLinus Torvalds level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT, 4101da177e4SLinus Torvalds SCSI_LOG_MLQUEUE_BITS); 4111da177e4SLinus Torvalds if (level > 1) { 4121da177e4SLinus Torvalds sdev = cmd->device; 4139ccfc756SJames Bottomley sdev_printk(KERN_INFO, sdev, "send "); 4141da177e4SLinus Torvalds if (level > 2) 4151da177e4SLinus Torvalds printk("0x%p ", cmd); 4161da177e4SLinus Torvalds /* 4171da177e4SLinus Torvalds * spaces to match disposition and cmd->result 4181da177e4SLinus Torvalds * output in scsi_log_completion. 4191da177e4SLinus Torvalds */ 4201da177e4SLinus Torvalds printk(" "); 4211da177e4SLinus Torvalds scsi_print_command(cmd); 4221da177e4SLinus Torvalds if (level > 3) { 4231da177e4SLinus Torvalds printk(KERN_INFO "buffer = 0x%p, bufflen = %d," 4241da177e4SLinus Torvalds " done = 0x%p, queuecommand 0x%p\n", 4251da177e4SLinus Torvalds cmd->buffer, cmd->bufflen, 4261da177e4SLinus Torvalds cmd->done, 4271da177e4SLinus Torvalds sdev->host->hostt->queuecommand); 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds } 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds void scsi_log_completion(struct scsi_cmnd *cmd, int disposition) 4351da177e4SLinus Torvalds { 4361da177e4SLinus Torvalds unsigned int level; 4371da177e4SLinus Torvalds struct scsi_device *sdev; 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds /* 4401da177e4SLinus Torvalds * If ML COMPLETE log level is greater than or equal to: 4411da177e4SLinus Torvalds * 4421da177e4SLinus Torvalds * 1: log disposition, result, opcode + command, and conditionally 4431da177e4SLinus Torvalds * sense data for failures or non SUCCESS dispositions. 4441da177e4SLinus Torvalds * 4451da177e4SLinus Torvalds * 2: same as 1 but for all command completions. 4461da177e4SLinus Torvalds * 4471da177e4SLinus Torvalds * 3: same as 2 plus dump cmd address 4481da177e4SLinus Torvalds * 4491da177e4SLinus Torvalds * 4: same as 3 plus dump extra junk 4501da177e4SLinus Torvalds */ 4511da177e4SLinus Torvalds if (unlikely(scsi_logging_level)) { 4521da177e4SLinus Torvalds level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT, 4531da177e4SLinus Torvalds SCSI_LOG_MLCOMPLETE_BITS); 4541da177e4SLinus Torvalds if (((level > 0) && (cmd->result || disposition != SUCCESS)) || 4551da177e4SLinus Torvalds (level > 1)) { 4561da177e4SLinus Torvalds sdev = cmd->device; 4579ccfc756SJames Bottomley sdev_printk(KERN_INFO, sdev, "done "); 4581da177e4SLinus Torvalds if (level > 2) 4591da177e4SLinus Torvalds printk("0x%p ", cmd); 4601da177e4SLinus Torvalds /* 4611da177e4SLinus Torvalds * Dump truncated values, so we usually fit within 4621da177e4SLinus Torvalds * 80 chars. 4631da177e4SLinus Torvalds */ 4641da177e4SLinus Torvalds switch (disposition) { 4651da177e4SLinus Torvalds case SUCCESS: 4661da177e4SLinus Torvalds printk("SUCCESS"); 4671da177e4SLinus Torvalds break; 4681da177e4SLinus Torvalds case NEEDS_RETRY: 4691da177e4SLinus Torvalds printk("RETRY "); 4701da177e4SLinus Torvalds break; 4711da177e4SLinus Torvalds case ADD_TO_MLQUEUE: 4721da177e4SLinus Torvalds printk("MLQUEUE"); 4731da177e4SLinus Torvalds break; 4741da177e4SLinus Torvalds case FAILED: 4751da177e4SLinus Torvalds printk("FAILED "); 4761da177e4SLinus Torvalds break; 4771da177e4SLinus Torvalds case TIMEOUT_ERROR: 4781da177e4SLinus Torvalds /* 4791da177e4SLinus Torvalds * If called via scsi_times_out. 4801da177e4SLinus Torvalds */ 4811da177e4SLinus Torvalds printk("TIMEOUT"); 4821da177e4SLinus Torvalds break; 4831da177e4SLinus Torvalds default: 4841da177e4SLinus Torvalds printk("UNKNOWN"); 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds printk(" %8x ", cmd->result); 4871da177e4SLinus Torvalds scsi_print_command(cmd); 4881da177e4SLinus Torvalds if (status_byte(cmd->result) & CHECK_CONDITION) { 4891da177e4SLinus Torvalds /* 490db9dff36S * XXX The scsi_print_sense formatting/prefix 4911da177e4SLinus Torvalds * doesn't match this function. 4921da177e4SLinus Torvalds */ 4931da177e4SLinus Torvalds scsi_print_sense("", cmd); 4941da177e4SLinus Torvalds } 4951da177e4SLinus Torvalds if (level > 3) { 4961da177e4SLinus Torvalds printk(KERN_INFO "scsi host busy %d failed %d\n", 4971da177e4SLinus Torvalds sdev->host->host_busy, 4981da177e4SLinus Torvalds sdev->host->host_failed); 4991da177e4SLinus Torvalds } 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds } 5021da177e4SLinus Torvalds } 5031da177e4SLinus Torvalds #endif 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /* 5061da177e4SLinus Torvalds * Assign a serial number and pid to the request for error recovery 5071da177e4SLinus Torvalds * and debugging purposes. Protected by the Host_Lock of host. 5081da177e4SLinus Torvalds */ 5091da177e4SLinus Torvalds static inline void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd) 5101da177e4SLinus Torvalds { 5111da177e4SLinus Torvalds cmd->serial_number = host->cmd_serial_number++; 5121da177e4SLinus Torvalds if (cmd->serial_number == 0) 5131da177e4SLinus Torvalds cmd->serial_number = host->cmd_serial_number++; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds cmd->pid = host->cmd_pid++; 5161da177e4SLinus Torvalds if (cmd->pid == 0) 5171da177e4SLinus Torvalds cmd->pid = host->cmd_pid++; 5181da177e4SLinus Torvalds } 5191da177e4SLinus Torvalds 5201da177e4SLinus Torvalds /* 5211da177e4SLinus Torvalds * Function: scsi_dispatch_command 5221da177e4SLinus Torvalds * 5231da177e4SLinus Torvalds * Purpose: Dispatch a command to the low-level driver. 5241da177e4SLinus Torvalds * 5251da177e4SLinus Torvalds * Arguments: cmd - command block we are dispatching. 5261da177e4SLinus Torvalds * 5271da177e4SLinus Torvalds * Notes: 5281da177e4SLinus Torvalds */ 5291da177e4SLinus Torvalds int scsi_dispatch_cmd(struct scsi_cmnd *cmd) 5301da177e4SLinus Torvalds { 5311da177e4SLinus Torvalds struct Scsi_Host *host = cmd->device->host; 5321da177e4SLinus Torvalds unsigned long flags = 0; 5331da177e4SLinus Torvalds unsigned long timeout; 5341da177e4SLinus Torvalds int rtn = 0; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds /* check if the device is still usable */ 5371da177e4SLinus Torvalds if (unlikely(cmd->device->sdev_state == SDEV_DEL)) { 5381da177e4SLinus Torvalds /* in SDEV_DEL we error all commands. DID_NO_CONNECT 5391da177e4SLinus Torvalds * returns an immediate error upwards, and signals 5401da177e4SLinus Torvalds * that the device is no longer present */ 5411da177e4SLinus Torvalds cmd->result = DID_NO_CONNECT << 16; 5421da177e4SLinus Torvalds atomic_inc(&cmd->device->iorequest_cnt); 54369b52893SMike Christie __scsi_done(cmd); 5441da177e4SLinus Torvalds /* return 0 (because the command has been processed) */ 5451da177e4SLinus Torvalds goto out; 5461da177e4SLinus Torvalds } 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds /* Check to see if the scsi lld put this device into state SDEV_BLOCK. */ 5491da177e4SLinus Torvalds if (unlikely(cmd->device->sdev_state == SDEV_BLOCK)) { 5501da177e4SLinus Torvalds /* 5511da177e4SLinus Torvalds * in SDEV_BLOCK, the command is just put back on the device 5521da177e4SLinus Torvalds * queue. The suspend state has already blocked the queue so 5531da177e4SLinus Torvalds * future requests should not occur until the device 5541da177e4SLinus Torvalds * transitions out of the suspend state. 5551da177e4SLinus Torvalds */ 5561da177e4SLinus Torvalds scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY); 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n")); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds /* 5611da177e4SLinus Torvalds * NOTE: rtn is still zero here because we don't need the 5621da177e4SLinus Torvalds * queue to be plugged on return (it's already stopped) 5631da177e4SLinus Torvalds */ 5641da177e4SLinus Torvalds goto out; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds /* 5681da177e4SLinus Torvalds * If SCSI-2 or lower, store the LUN value in cmnd. 5691da177e4SLinus Torvalds */ 5701da177e4SLinus Torvalds if (cmd->device->scsi_level <= SCSI_2) { 5711da177e4SLinus Torvalds cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) | 5721da177e4SLinus Torvalds (cmd->device->lun << 5 & 0xe0); 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* 5761da177e4SLinus Torvalds * We will wait MIN_RESET_DELAY clock ticks after the last reset so 5771da177e4SLinus Torvalds * we can avoid the drive not being ready. 5781da177e4SLinus Torvalds */ 5791da177e4SLinus Torvalds timeout = host->last_reset + MIN_RESET_DELAY; 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds if (host->resetting && time_before(jiffies, timeout)) { 5821da177e4SLinus Torvalds int ticks_remaining = timeout - jiffies; 5831da177e4SLinus Torvalds /* 5841da177e4SLinus Torvalds * NOTE: This may be executed from within an interrupt 5851da177e4SLinus Torvalds * handler! This is bad, but for now, it'll do. The irq 5861da177e4SLinus Torvalds * level of the interrupt handler has been masked out by the 5871da177e4SLinus Torvalds * platform dependent interrupt handling code already, so the 5881da177e4SLinus Torvalds * sti() here will not cause another call to the SCSI host's 5891da177e4SLinus Torvalds * interrupt handler (assuming there is one irq-level per 5901da177e4SLinus Torvalds * host). 5911da177e4SLinus Torvalds */ 5921da177e4SLinus Torvalds while (--ticks_remaining >= 0) 5931da177e4SLinus Torvalds mdelay(1 + 999 / HZ); 5941da177e4SLinus Torvalds host->resetting = 0; 5951da177e4SLinus Torvalds } 5961da177e4SLinus Torvalds 5971da177e4SLinus Torvalds /* 5981da177e4SLinus Torvalds * AK: unlikely race here: for some reason the timer could 5991da177e4SLinus Torvalds * expire before the serial number is set up below. 6001da177e4SLinus Torvalds */ 6011da177e4SLinus Torvalds scsi_add_timer(cmd, cmd->timeout_per_command, scsi_times_out); 6021da177e4SLinus Torvalds 6031da177e4SLinus Torvalds scsi_log_send(cmd); 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds /* 6061da177e4SLinus Torvalds * We will use a queued command if possible, otherwise we will 6071da177e4SLinus Torvalds * emulate the queuing and calling of completion function ourselves. 6081da177e4SLinus Torvalds */ 6091da177e4SLinus Torvalds atomic_inc(&cmd->device->iorequest_cnt); 6101da177e4SLinus Torvalds 6111da177e4SLinus Torvalds /* 6121da177e4SLinus Torvalds * Before we queue this command, check if the command 6131da177e4SLinus Torvalds * length exceeds what the host adapter can handle. 6141da177e4SLinus Torvalds */ 6151da177e4SLinus Torvalds if (CDB_SIZE(cmd) > cmd->device->host->max_cmd_len) { 6161da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, 6171da177e4SLinus Torvalds printk("queuecommand : command too long.\n")); 6181da177e4SLinus Torvalds cmd->result = (DID_ABORT << 16); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds scsi_done(cmd); 6211da177e4SLinus Torvalds goto out; 6221da177e4SLinus Torvalds } 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds spin_lock_irqsave(host->host_lock, flags); 6251da177e4SLinus Torvalds scsi_cmd_get_serial(host, cmd); 6261da177e4SLinus Torvalds 627d2c9d9eaSMike Anderson if (unlikely(host->shost_state == SHOST_DEL)) { 6281da177e4SLinus Torvalds cmd->result = (DID_NO_CONNECT << 16); 6291da177e4SLinus Torvalds scsi_done(cmd); 6301da177e4SLinus Torvalds } else { 6311da177e4SLinus Torvalds rtn = host->hostt->queuecommand(cmd, scsi_done); 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds spin_unlock_irqrestore(host->host_lock, flags); 6341da177e4SLinus Torvalds if (rtn) { 635d8c37e7bSTejun Heo if (scsi_delete_timer(cmd)) { 6361da177e4SLinus Torvalds atomic_inc(&cmd->device->iodone_cnt); 6371da177e4SLinus Torvalds scsi_queue_insert(cmd, 6381da177e4SLinus Torvalds (rtn == SCSI_MLQUEUE_DEVICE_BUSY) ? 6391da177e4SLinus Torvalds rtn : SCSI_MLQUEUE_HOST_BUSY); 640d8c37e7bSTejun Heo } 6411da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, 6421da177e4SLinus Torvalds printk("queuecommand : request rejected\n")); 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds out: 6461da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n")); 6471da177e4SLinus Torvalds return rtn; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /* 6511da177e4SLinus Torvalds * Function: scsi_init_cmd_from_req 6521da177e4SLinus Torvalds * 6531da177e4SLinus Torvalds * Purpose: Queue a SCSI command 6541da177e4SLinus Torvalds * Purpose: Initialize a struct scsi_cmnd from a struct scsi_request 6551da177e4SLinus Torvalds * 6561da177e4SLinus Torvalds * Arguments: cmd - command descriptor. 6571da177e4SLinus Torvalds * sreq - Request from the queue. 6581da177e4SLinus Torvalds * 6591da177e4SLinus Torvalds * Lock status: None needed. 6601da177e4SLinus Torvalds * 6611da177e4SLinus Torvalds * Returns: Nothing. 6621da177e4SLinus Torvalds * 6631da177e4SLinus Torvalds * Notes: Mainly transfer data from the request structure to the 6641da177e4SLinus Torvalds * command structure. The request structure is allocated 6651da177e4SLinus Torvalds * using the normal memory allocator, and requests can pile 6661da177e4SLinus Torvalds * up to more or less any depth. The command structure represents 6671da177e4SLinus Torvalds * a consumable resource, as these are allocated into a pool 6681da177e4SLinus Torvalds * when the SCSI subsystem initializes. The preallocation is 6691da177e4SLinus Torvalds * required so that in low-memory situations a disk I/O request 6701da177e4SLinus Torvalds * won't cause the memory manager to try and write out a page. 6711da177e4SLinus Torvalds * The request structure is generally used by ioctls and character 6721da177e4SLinus Torvalds * devices. 6731da177e4SLinus Torvalds */ 6741da177e4SLinus Torvalds void scsi_init_cmd_from_req(struct scsi_cmnd *cmd, struct scsi_request *sreq) 6751da177e4SLinus Torvalds { 6761da177e4SLinus Torvalds sreq->sr_command = cmd; 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds cmd->cmd_len = sreq->sr_cmd_len; 6791da177e4SLinus Torvalds cmd->use_sg = sreq->sr_use_sg; 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds cmd->request = sreq->sr_request; 6821da177e4SLinus Torvalds memcpy(cmd->data_cmnd, sreq->sr_cmnd, sizeof(cmd->data_cmnd)); 6831da177e4SLinus Torvalds cmd->serial_number = 0; 6841da177e4SLinus Torvalds cmd->bufflen = sreq->sr_bufflen; 6851da177e4SLinus Torvalds cmd->buffer = sreq->sr_buffer; 6861da177e4SLinus Torvalds cmd->retries = 0; 6871da177e4SLinus Torvalds cmd->allowed = sreq->sr_allowed; 6881da177e4SLinus Torvalds cmd->done = sreq->sr_done; 6891da177e4SLinus Torvalds cmd->timeout_per_command = sreq->sr_timeout_per_command; 6901da177e4SLinus Torvalds cmd->sc_data_direction = sreq->sr_data_direction; 6911da177e4SLinus Torvalds cmd->sglist_len = sreq->sr_sglist_len; 6921da177e4SLinus Torvalds cmd->underflow = sreq->sr_underflow; 6931da177e4SLinus Torvalds cmd->sc_request = sreq; 6941da177e4SLinus Torvalds memcpy(cmd->cmnd, sreq->sr_cmnd, sizeof(sreq->sr_cmnd)); 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /* 6971da177e4SLinus Torvalds * Zero the sense buffer. Some host adapters automatically request 6981da177e4SLinus Torvalds * sense on error. 0 is not a valid sense code. 6991da177e4SLinus Torvalds */ 7001da177e4SLinus Torvalds memset(cmd->sense_buffer, 0, sizeof(sreq->sr_sense_buffer)); 7011da177e4SLinus Torvalds cmd->request_buffer = sreq->sr_buffer; 7021da177e4SLinus Torvalds cmd->request_bufflen = sreq->sr_bufflen; 7031da177e4SLinus Torvalds cmd->old_use_sg = cmd->use_sg; 7041da177e4SLinus Torvalds if (cmd->cmd_len == 0) 7051da177e4SLinus Torvalds cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]); 7061da177e4SLinus Torvalds cmd->old_cmd_len = cmd->cmd_len; 7071da177e4SLinus Torvalds cmd->sc_old_data_direction = cmd->sc_data_direction; 7081da177e4SLinus Torvalds cmd->old_underflow = cmd->underflow; 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds /* 7111da177e4SLinus Torvalds * Start the timer ticking. 7121da177e4SLinus Torvalds */ 7131da177e4SLinus Torvalds cmd->result = 0; 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, printk("Leaving scsi_init_cmd_from_req()\n")); 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds /* 7191da177e4SLinus Torvalds * Per-CPU I/O completion queue. 7201da177e4SLinus Torvalds */ 7211da177e4SLinus Torvalds static DEFINE_PER_CPU(struct list_head, scsi_done_q); 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /** 7241da177e4SLinus Torvalds * scsi_done - Enqueue the finished SCSI command into the done queue. 7251da177e4SLinus Torvalds * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives 7261da177e4SLinus Torvalds * ownership back to SCSI Core -- i.e. the LLDD has finished with it. 7271da177e4SLinus Torvalds * 7281da177e4SLinus Torvalds * This function is the mid-level's (SCSI Core) interrupt routine, which 7291da177e4SLinus Torvalds * regains ownership of the SCSI command (de facto) from a LLDD, and enqueues 7301da177e4SLinus Torvalds * the command to the done queue for further processing. 7311da177e4SLinus Torvalds * 7321da177e4SLinus Torvalds * This is the producer of the done queue who enqueues at the tail. 7331da177e4SLinus Torvalds * 7341da177e4SLinus Torvalds * This function is interrupt context safe. 7351da177e4SLinus Torvalds */ 73652c1da39SAdrian Bunk static void scsi_done(struct scsi_cmnd *cmd) 7371da177e4SLinus Torvalds { 7381da177e4SLinus Torvalds /* 7391da177e4SLinus Torvalds * We don't have to worry about this one timing out any more. 7401da177e4SLinus Torvalds * If we are unable to remove the timer, then the command 7411da177e4SLinus Torvalds * has already timed out. In which case, we have no choice but to 7421da177e4SLinus Torvalds * let the timeout function run, as we have no idea where in fact 7431da177e4SLinus Torvalds * that function could really be. It might be on another processor, 7441da177e4SLinus Torvalds * etc, etc. 7451da177e4SLinus Torvalds */ 7461da177e4SLinus Torvalds if (!scsi_delete_timer(cmd)) 7471da177e4SLinus Torvalds return; 7481da177e4SLinus Torvalds __scsi_done(cmd); 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds /* Private entry to scsi_done() to complete a command when the timer 7521da177e4SLinus Torvalds * isn't running --- used by scsi_times_out */ 7531da177e4SLinus Torvalds void __scsi_done(struct scsi_cmnd *cmd) 7541da177e4SLinus Torvalds { 7551aea6434SJens Axboe struct request *rq = cmd->request; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds /* 7581da177e4SLinus Torvalds * Set the serial numbers back to zero 7591da177e4SLinus Torvalds */ 7601da177e4SLinus Torvalds cmd->serial_number = 0; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds atomic_inc(&cmd->device->iodone_cnt); 7631da177e4SLinus Torvalds if (cmd->result) 7641da177e4SLinus Torvalds atomic_inc(&cmd->device->ioerr_cnt); 7651da177e4SLinus Torvalds 7661aea6434SJens Axboe BUG_ON(!rq); 7671aea6434SJens Axboe 7681da177e4SLinus Torvalds /* 7691aea6434SJens Axboe * The uptodate/nbytes values don't matter, as we allow partial 7701aea6434SJens Axboe * completes and thus will check this in the softirq callback 7711da177e4SLinus Torvalds */ 7721aea6434SJens Axboe rq->completion_data = cmd; 7731aea6434SJens Axboe blk_complete_request(rq); 7741da177e4SLinus Torvalds } 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds /* 7771da177e4SLinus Torvalds * Function: scsi_retry_command 7781da177e4SLinus Torvalds * 7791da177e4SLinus Torvalds * Purpose: Send a command back to the low level to be retried. 7801da177e4SLinus Torvalds * 7811da177e4SLinus Torvalds * Notes: This command is always executed in the context of the 7821da177e4SLinus Torvalds * bottom half handler, or the error handler thread. Low 7831da177e4SLinus Torvalds * level drivers should not become re-entrant as a result of 7841da177e4SLinus Torvalds * this. 7851da177e4SLinus Torvalds */ 7861aea6434SJens Axboe int scsi_retry_command(struct scsi_cmnd *cmd) 7871da177e4SLinus Torvalds { 7881da177e4SLinus Torvalds /* 7891da177e4SLinus Torvalds * Restore the SCSI command state. 7901da177e4SLinus Torvalds */ 7911da177e4SLinus Torvalds scsi_setup_cmd_retry(cmd); 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* 7941da177e4SLinus Torvalds * Zero the sense information from the last time we tried 7951da177e4SLinus Torvalds * this command. 7961da177e4SLinus Torvalds */ 7971da177e4SLinus Torvalds memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer)); 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds return scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY); 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds /* 8031da177e4SLinus Torvalds * Function: scsi_finish_command 8041da177e4SLinus Torvalds * 8051da177e4SLinus Torvalds * Purpose: Pass command off to upper layer for finishing of I/O 8061da177e4SLinus Torvalds * request, waking processes that are waiting on results, 8071da177e4SLinus Torvalds * etc. 8081da177e4SLinus Torvalds */ 8091da177e4SLinus Torvalds void scsi_finish_command(struct scsi_cmnd *cmd) 8101da177e4SLinus Torvalds { 8111da177e4SLinus Torvalds struct scsi_device *sdev = cmd->device; 8121da177e4SLinus Torvalds struct Scsi_Host *shost = sdev->host; 8131da177e4SLinus Torvalds struct scsi_request *sreq; 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds scsi_device_unbusy(sdev); 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds /* 8181da177e4SLinus Torvalds * Clear the flags which say that the device/host is no longer 8191da177e4SLinus Torvalds * capable of accepting new commands. These are set in scsi_queue.c 8201da177e4SLinus Torvalds * for both the queue full condition on a device, and for a 8211da177e4SLinus Torvalds * host full condition on the host. 8221da177e4SLinus Torvalds * 8231da177e4SLinus Torvalds * XXX(hch): What about locking? 8241da177e4SLinus Torvalds */ 8251da177e4SLinus Torvalds shost->host_blocked = 0; 8261da177e4SLinus Torvalds sdev->device_blocked = 0; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /* 8291da177e4SLinus Torvalds * If we have valid sense information, then some kind of recovery 8301da177e4SLinus Torvalds * must have taken place. Make a note of this. 8311da177e4SLinus Torvalds */ 8321da177e4SLinus Torvalds if (SCSI_SENSE_VALID(cmd)) 8331da177e4SLinus Torvalds cmd->result |= (DRIVER_SENSE << 24); 8341da177e4SLinus Torvalds 8353bf743e7SJeff Garzik SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev, 8363bf743e7SJeff Garzik "Notifying upper driver of completion " 8373bf743e7SJeff Garzik "(result %x)\n", cmd->result)); 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds /* 8401da177e4SLinus Torvalds * We can get here with use_sg=0, causing a panic in the upper level 8411da177e4SLinus Torvalds */ 8421da177e4SLinus Torvalds cmd->use_sg = cmd->old_use_sg; 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* 8451da177e4SLinus Torvalds * If there is an associated request structure, copy the data over 8461da177e4SLinus Torvalds * before we call the completion function. 8471da177e4SLinus Torvalds */ 8481da177e4SLinus Torvalds sreq = cmd->sc_request; 8491da177e4SLinus Torvalds if (sreq) { 8501da177e4SLinus Torvalds sreq->sr_result = sreq->sr_command->result; 8511da177e4SLinus Torvalds if (sreq->sr_result) { 8521da177e4SLinus Torvalds memcpy(sreq->sr_sense_buffer, 8531da177e4SLinus Torvalds sreq->sr_command->sense_buffer, 8541da177e4SLinus Torvalds sizeof(sreq->sr_sense_buffer)); 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds cmd->done(cmd); 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_finish_command); 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds /* 8631da177e4SLinus Torvalds * Function: scsi_adjust_queue_depth() 8641da177e4SLinus Torvalds * 8651da177e4SLinus Torvalds * Purpose: Allow low level drivers to tell us to change the queue depth 8661da177e4SLinus Torvalds * on a specific SCSI device 8671da177e4SLinus Torvalds * 8681da177e4SLinus Torvalds * Arguments: sdev - SCSI Device in question 8691da177e4SLinus Torvalds * tagged - Do we use tagged queueing (non-0) or do we treat 8701da177e4SLinus Torvalds * this device as an untagged device (0) 8711da177e4SLinus Torvalds * tags - Number of tags allowed if tagged queueing enabled, 8721da177e4SLinus Torvalds * or number of commands the low level driver can 8731da177e4SLinus Torvalds * queue up in non-tagged mode (as per cmd_per_lun). 8741da177e4SLinus Torvalds * 8751da177e4SLinus Torvalds * Returns: Nothing 8761da177e4SLinus Torvalds * 8771da177e4SLinus Torvalds * Lock Status: None held on entry 8781da177e4SLinus Torvalds * 8791da177e4SLinus Torvalds * Notes: Low level drivers may call this at any time and we will do 8801da177e4SLinus Torvalds * the right thing depending on whether or not the device is 8811da177e4SLinus Torvalds * currently active and whether or not it even has the 8821da177e4SLinus Torvalds * command blocks built yet. 8831da177e4SLinus Torvalds */ 8841da177e4SLinus Torvalds void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags) 8851da177e4SLinus Torvalds { 8861da177e4SLinus Torvalds unsigned long flags; 8871da177e4SLinus Torvalds 8881da177e4SLinus Torvalds /* 8891da177e4SLinus Torvalds * refuse to set tagged depth to an unworkable size 8901da177e4SLinus Torvalds */ 8911da177e4SLinus Torvalds if (tags <= 0) 8921da177e4SLinus Torvalds return; 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds spin_lock_irqsave(sdev->request_queue->queue_lock, flags); 8951da177e4SLinus Torvalds 8961da177e4SLinus Torvalds /* Check to see if the queue is managed by the block layer 8971da177e4SLinus Torvalds * if it is, and we fail to adjust the depth, exit */ 8981da177e4SLinus Torvalds if (blk_queue_tagged(sdev->request_queue) && 8991da177e4SLinus Torvalds blk_queue_resize_tags(sdev->request_queue, tags) != 0) 9001da177e4SLinus Torvalds goto out; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds sdev->queue_depth = tags; 9031da177e4SLinus Torvalds switch (tagged) { 9041da177e4SLinus Torvalds case MSG_ORDERED_TAG: 9051da177e4SLinus Torvalds sdev->ordered_tags = 1; 9061da177e4SLinus Torvalds sdev->simple_tags = 1; 9071da177e4SLinus Torvalds break; 9081da177e4SLinus Torvalds case MSG_SIMPLE_TAG: 9091da177e4SLinus Torvalds sdev->ordered_tags = 0; 9101da177e4SLinus Torvalds sdev->simple_tags = 1; 9111da177e4SLinus Torvalds break; 9121da177e4SLinus Torvalds default: 9139ccfc756SJames Bottomley sdev_printk(KERN_WARNING, sdev, 9141da177e4SLinus Torvalds "scsi_adjust_queue_depth, bad queue type, " 9159ccfc756SJames Bottomley "disabled\n"); 9161da177e4SLinus Torvalds case 0: 9171da177e4SLinus Torvalds sdev->ordered_tags = sdev->simple_tags = 0; 9181da177e4SLinus Torvalds sdev->queue_depth = tags; 9191da177e4SLinus Torvalds break; 9201da177e4SLinus Torvalds } 9211da177e4SLinus Torvalds out: 9221da177e4SLinus Torvalds spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags); 9231da177e4SLinus Torvalds } 9241da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_adjust_queue_depth); 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds /* 9271da177e4SLinus Torvalds * Function: scsi_track_queue_full() 9281da177e4SLinus Torvalds * 9291da177e4SLinus Torvalds * Purpose: This function will track successive QUEUE_FULL events on a 9301da177e4SLinus Torvalds * specific SCSI device to determine if and when there is a 9311da177e4SLinus Torvalds * need to adjust the queue depth on the device. 9321da177e4SLinus Torvalds * 9331da177e4SLinus Torvalds * Arguments: sdev - SCSI Device in question 9341da177e4SLinus Torvalds * depth - Current number of outstanding SCSI commands on 9351da177e4SLinus Torvalds * this device, not counting the one returned as 9361da177e4SLinus Torvalds * QUEUE_FULL. 9371da177e4SLinus Torvalds * 9381da177e4SLinus Torvalds * Returns: 0 - No change needed 9391da177e4SLinus Torvalds * >0 - Adjust queue depth to this new depth 9401da177e4SLinus Torvalds * -1 - Drop back to untagged operation using host->cmd_per_lun 9411da177e4SLinus Torvalds * as the untagged command depth 9421da177e4SLinus Torvalds * 9431da177e4SLinus Torvalds * Lock Status: None held on entry 9441da177e4SLinus Torvalds * 9451da177e4SLinus Torvalds * Notes: Low level drivers may call this at any time and we will do 9461da177e4SLinus Torvalds * "The Right Thing." We are interrupt context safe. 9471da177e4SLinus Torvalds */ 9481da177e4SLinus Torvalds int scsi_track_queue_full(struct scsi_device *sdev, int depth) 9491da177e4SLinus Torvalds { 9501da177e4SLinus Torvalds if ((jiffies >> 4) == sdev->last_queue_full_time) 9511da177e4SLinus Torvalds return 0; 9521da177e4SLinus Torvalds 9531da177e4SLinus Torvalds sdev->last_queue_full_time = (jiffies >> 4); 9541da177e4SLinus Torvalds if (sdev->last_queue_full_depth != depth) { 9551da177e4SLinus Torvalds sdev->last_queue_full_count = 1; 9561da177e4SLinus Torvalds sdev->last_queue_full_depth = depth; 9571da177e4SLinus Torvalds } else { 9581da177e4SLinus Torvalds sdev->last_queue_full_count++; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 9611da177e4SLinus Torvalds if (sdev->last_queue_full_count <= 10) 9621da177e4SLinus Torvalds return 0; 9631da177e4SLinus Torvalds if (sdev->last_queue_full_depth < 8) { 9641da177e4SLinus Torvalds /* Drop back to untagged */ 9651da177e4SLinus Torvalds scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun); 9661da177e4SLinus Torvalds return -1; 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds if (sdev->ordered_tags) 9701da177e4SLinus Torvalds scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth); 9711da177e4SLinus Torvalds else 9721da177e4SLinus Torvalds scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth); 9731da177e4SLinus Torvalds return depth; 9741da177e4SLinus Torvalds } 9751da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_track_queue_full); 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds /** 9781da177e4SLinus Torvalds * scsi_device_get - get an addition reference to a scsi_device 9791da177e4SLinus Torvalds * @sdev: device to get a reference to 9801da177e4SLinus Torvalds * 9811da177e4SLinus Torvalds * Gets a reference to the scsi_device and increments the use count 9821da177e4SLinus Torvalds * of the underlying LLDD module. You must hold host_lock of the 9831da177e4SLinus Torvalds * parent Scsi_Host or already have a reference when calling this. 9841da177e4SLinus Torvalds */ 9851da177e4SLinus Torvalds int scsi_device_get(struct scsi_device *sdev) 9861da177e4SLinus Torvalds { 9871da177e4SLinus Torvalds if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL) 9881da177e4SLinus Torvalds return -ENXIO; 9891da177e4SLinus Torvalds if (!get_device(&sdev->sdev_gendev)) 9901da177e4SLinus Torvalds return -ENXIO; 9911da177e4SLinus Torvalds if (!try_module_get(sdev->host->hostt->module)) { 9921da177e4SLinus Torvalds put_device(&sdev->sdev_gendev); 9931da177e4SLinus Torvalds return -ENXIO; 9941da177e4SLinus Torvalds } 9951da177e4SLinus Torvalds return 0; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_get); 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds /** 10001da177e4SLinus Torvalds * scsi_device_put - release a reference to a scsi_device 10011da177e4SLinus Torvalds * @sdev: device to release a reference on. 10021da177e4SLinus Torvalds * 10031da177e4SLinus Torvalds * Release a reference to the scsi_device and decrements the use count 10041da177e4SLinus Torvalds * of the underlying LLDD module. The device is freed once the last 10051da177e4SLinus Torvalds * user vanishes. 10061da177e4SLinus Torvalds */ 10071da177e4SLinus Torvalds void scsi_device_put(struct scsi_device *sdev) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds module_put(sdev->host->hostt->module); 10101da177e4SLinus Torvalds put_device(&sdev->sdev_gendev); 10111da177e4SLinus Torvalds } 10121da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_put); 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds /* helper for shost_for_each_device, thus not documented */ 10151da177e4SLinus Torvalds struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost, 10161da177e4SLinus Torvalds struct scsi_device *prev) 10171da177e4SLinus Torvalds { 10181da177e4SLinus Torvalds struct list_head *list = (prev ? &prev->siblings : &shost->__devices); 10191da177e4SLinus Torvalds struct scsi_device *next = NULL; 10201da177e4SLinus Torvalds unsigned long flags; 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 10231da177e4SLinus Torvalds while (list->next != &shost->__devices) { 10241da177e4SLinus Torvalds next = list_entry(list->next, struct scsi_device, siblings); 10251da177e4SLinus Torvalds /* skip devices that we can't get a reference to */ 10261da177e4SLinus Torvalds if (!scsi_device_get(next)) 10271da177e4SLinus Torvalds break; 10281da177e4SLinus Torvalds next = NULL; 10291da177e4SLinus Torvalds list = list->next; 10301da177e4SLinus Torvalds } 10311da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds if (prev) 10341da177e4SLinus Torvalds scsi_device_put(prev); 10351da177e4SLinus Torvalds return next; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_iterate_devices); 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds /** 10401da177e4SLinus Torvalds * starget_for_each_device - helper to walk all devices of a target 10411da177e4SLinus Torvalds * @starget: target whose devices we want to iterate over. 10421da177e4SLinus Torvalds * 10431da177e4SLinus Torvalds * This traverses over each devices of @shost. The devices have 10441da177e4SLinus Torvalds * a reference that must be released by scsi_host_put when breaking 10451da177e4SLinus Torvalds * out of the loop. 10461da177e4SLinus Torvalds */ 10471da177e4SLinus Torvalds void starget_for_each_device(struct scsi_target *starget, void * data, 10481da177e4SLinus Torvalds void (*fn)(struct scsi_device *, void *)) 10491da177e4SLinus Torvalds { 10501da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 10511da177e4SLinus Torvalds struct scsi_device *sdev; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds shost_for_each_device(sdev, shost) { 10541da177e4SLinus Torvalds if ((sdev->channel == starget->channel) && 10551da177e4SLinus Torvalds (sdev->id == starget->id)) 10561da177e4SLinus Torvalds fn(sdev, data); 10571da177e4SLinus Torvalds } 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds EXPORT_SYMBOL(starget_for_each_device); 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds /** 10621da177e4SLinus Torvalds * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED) 10631da177e4SLinus Torvalds * @starget: SCSI target pointer 10641da177e4SLinus Torvalds * @lun: SCSI Logical Unit Number 10651da177e4SLinus Torvalds * 10661da177e4SLinus Torvalds * Looks up the scsi_device with the specified @lun for a give 10671da177e4SLinus Torvalds * @starget. The returned scsi_device does not have an additional 10681da177e4SLinus Torvalds * reference. You must hold the host's host_lock over this call and 10691da177e4SLinus Torvalds * any access to the returned scsi_device. 10701da177e4SLinus Torvalds * 10711da177e4SLinus Torvalds * Note: The only reason why drivers would want to use this is because 10721da177e4SLinus Torvalds * they're need to access the device list in irq context. Otherwise you 10731da177e4SLinus Torvalds * really want to use scsi_device_lookup_by_target instead. 10741da177e4SLinus Torvalds **/ 10751da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget, 10761da177e4SLinus Torvalds uint lun) 10771da177e4SLinus Torvalds { 10781da177e4SLinus Torvalds struct scsi_device *sdev; 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds list_for_each_entry(sdev, &starget->devices, same_target_siblings) { 10811da177e4SLinus Torvalds if (sdev->lun ==lun) 10821da177e4SLinus Torvalds return sdev; 10831da177e4SLinus Torvalds } 10841da177e4SLinus Torvalds 10851da177e4SLinus Torvalds return NULL; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup_by_target); 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /** 10901da177e4SLinus Torvalds * scsi_device_lookup_by_target - find a device given the target 10911da177e4SLinus Torvalds * @starget: SCSI target pointer 10921da177e4SLinus Torvalds * @lun: SCSI Logical Unit Number 10931da177e4SLinus Torvalds * 10941da177e4SLinus Torvalds * Looks up the scsi_device with the specified @channel, @id, @lun for a 10951da177e4SLinus Torvalds * give host. The returned scsi_device has an additional reference that 10961da177e4SLinus Torvalds * needs to be release with scsi_host_put once you're done with it. 10971da177e4SLinus Torvalds **/ 10981da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget, 10991da177e4SLinus Torvalds uint lun) 11001da177e4SLinus Torvalds { 11011da177e4SLinus Torvalds struct scsi_device *sdev; 11021da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 11031da177e4SLinus Torvalds unsigned long flags; 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 11061da177e4SLinus Torvalds sdev = __scsi_device_lookup_by_target(starget, lun); 11071da177e4SLinus Torvalds if (sdev && scsi_device_get(sdev)) 11081da177e4SLinus Torvalds sdev = NULL; 11091da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds return sdev; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup_by_target); 11141da177e4SLinus Torvalds 11151da177e4SLinus Torvalds /** 11161da177e4SLinus Torvalds * scsi_device_lookup - find a device given the host (UNLOCKED) 11171da177e4SLinus Torvalds * @shost: SCSI host pointer 11181da177e4SLinus Torvalds * @channel: SCSI channel (zero if only one channel) 11191da177e4SLinus Torvalds * @pun: SCSI target number (physical unit number) 11201da177e4SLinus Torvalds * @lun: SCSI Logical Unit Number 11211da177e4SLinus Torvalds * 11221da177e4SLinus Torvalds * Looks up the scsi_device with the specified @channel, @id, @lun for a 11231da177e4SLinus Torvalds * give host. The returned scsi_device does not have an additional reference. 11241da177e4SLinus Torvalds * You must hold the host's host_lock over this call and any access to the 11251da177e4SLinus Torvalds * returned scsi_device. 11261da177e4SLinus Torvalds * 11271da177e4SLinus Torvalds * Note: The only reason why drivers would want to use this is because 11281da177e4SLinus Torvalds * they're need to access the device list in irq context. Otherwise you 11291da177e4SLinus Torvalds * really want to use scsi_device_lookup instead. 11301da177e4SLinus Torvalds **/ 11311da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost, 11321da177e4SLinus Torvalds uint channel, uint id, uint lun) 11331da177e4SLinus Torvalds { 11341da177e4SLinus Torvalds struct scsi_device *sdev; 11351da177e4SLinus Torvalds 11361da177e4SLinus Torvalds list_for_each_entry(sdev, &shost->__devices, siblings) { 11371da177e4SLinus Torvalds if (sdev->channel == channel && sdev->id == id && 11381da177e4SLinus Torvalds sdev->lun ==lun) 11391da177e4SLinus Torvalds return sdev; 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds return NULL; 11431da177e4SLinus Torvalds } 11441da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup); 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds /** 11471da177e4SLinus Torvalds * scsi_device_lookup - find a device given the host 11481da177e4SLinus Torvalds * @shost: SCSI host pointer 11491da177e4SLinus Torvalds * @channel: SCSI channel (zero if only one channel) 11501da177e4SLinus Torvalds * @id: SCSI target number (physical unit number) 11511da177e4SLinus Torvalds * @lun: SCSI Logical Unit Number 11521da177e4SLinus Torvalds * 11531da177e4SLinus Torvalds * Looks up the scsi_device with the specified @channel, @id, @lun for a 11541da177e4SLinus Torvalds * give host. The returned scsi_device has an additional reference that 11551da177e4SLinus Torvalds * needs to be release with scsi_host_put once you're done with it. 11561da177e4SLinus Torvalds **/ 11571da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost, 11581da177e4SLinus Torvalds uint channel, uint id, uint lun) 11591da177e4SLinus Torvalds { 11601da177e4SLinus Torvalds struct scsi_device *sdev; 11611da177e4SLinus Torvalds unsigned long flags; 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 11641da177e4SLinus Torvalds sdev = __scsi_device_lookup(shost, channel, id, lun); 11651da177e4SLinus Torvalds if (sdev && scsi_device_get(sdev)) 11661da177e4SLinus Torvalds sdev = NULL; 11671da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds return sdev; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup); 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds /** 11741da177e4SLinus Torvalds * scsi_device_cancel - cancel outstanding IO to this device 11751da177e4SLinus Torvalds * @sdev: Pointer to struct scsi_device 11761da177e4SLinus Torvalds * @recovery: Boolean instructing function to recover device or not. 11771da177e4SLinus Torvalds * 11781da177e4SLinus Torvalds **/ 11791da177e4SLinus Torvalds int scsi_device_cancel(struct scsi_device *sdev, int recovery) 11801da177e4SLinus Torvalds { 11811da177e4SLinus Torvalds struct scsi_cmnd *scmd; 11821da177e4SLinus Torvalds LIST_HEAD(active_list); 11831da177e4SLinus Torvalds struct list_head *lh, *lh_sf; 11841da177e4SLinus Torvalds unsigned long flags; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds scsi_device_set_state(sdev, SDEV_CANCEL); 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds spin_lock_irqsave(&sdev->list_lock, flags); 11891da177e4SLinus Torvalds list_for_each_entry(scmd, &sdev->cmd_list, list) { 11901da177e4SLinus Torvalds if (scmd->request && scmd->request->rq_status != RQ_INACTIVE) { 11911da177e4SLinus Torvalds /* 11921da177e4SLinus Torvalds * If we are unable to remove the timer, it means 11931da177e4SLinus Torvalds * that the command has already timed out or 11941da177e4SLinus Torvalds * finished. 11951da177e4SLinus Torvalds */ 11961da177e4SLinus Torvalds if (!scsi_delete_timer(scmd)) 11971da177e4SLinus Torvalds continue; 11981da177e4SLinus Torvalds list_add_tail(&scmd->eh_entry, &active_list); 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds spin_unlock_irqrestore(&sdev->list_lock, flags); 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds if (!list_empty(&active_list)) { 12041da177e4SLinus Torvalds list_for_each_safe(lh, lh_sf, &active_list) { 12051da177e4SLinus Torvalds scmd = list_entry(lh, struct scsi_cmnd, eh_entry); 12061da177e4SLinus Torvalds list_del_init(lh); 1207939647eeSJames Bottomley if (recovery && 1208939647eeSJames Bottomley !scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD)) { 12091da177e4SLinus Torvalds scmd->result = (DID_ABORT << 16); 12101da177e4SLinus Torvalds scsi_finish_command(scmd); 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds } 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds return 0; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_cancel); 12181da177e4SLinus Torvalds 12191da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI core"); 12201da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR); 12231da177e4SLinus Torvalds MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels"); 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds static int __init init_scsi(void) 12261da177e4SLinus Torvalds { 12271da177e4SLinus Torvalds int error, i; 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds error = scsi_init_queue(); 12301da177e4SLinus Torvalds if (error) 12311da177e4SLinus Torvalds return error; 12321da177e4SLinus Torvalds error = scsi_init_procfs(); 12331da177e4SLinus Torvalds if (error) 12341da177e4SLinus Torvalds goto cleanup_queue; 12351da177e4SLinus Torvalds error = scsi_init_devinfo(); 12361da177e4SLinus Torvalds if (error) 12371da177e4SLinus Torvalds goto cleanup_procfs; 12381da177e4SLinus Torvalds error = scsi_init_hosts(); 12391da177e4SLinus Torvalds if (error) 12401da177e4SLinus Torvalds goto cleanup_devlist; 12411da177e4SLinus Torvalds error = scsi_init_sysctl(); 12421da177e4SLinus Torvalds if (error) 12431da177e4SLinus Torvalds goto cleanup_hosts; 12441da177e4SLinus Torvalds error = scsi_sysfs_register(); 12451da177e4SLinus Torvalds if (error) 12461da177e4SLinus Torvalds goto cleanup_sysctl; 12471da177e4SLinus Torvalds 124888a2a4acSEric Dumazet for_each_cpu(i) 12491da177e4SLinus Torvalds INIT_LIST_HEAD(&per_cpu(scsi_done_q, i)); 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds devfs_mk_dir("scsi"); 12521da177e4SLinus Torvalds printk(KERN_NOTICE "SCSI subsystem initialized\n"); 12531da177e4SLinus Torvalds return 0; 12541da177e4SLinus Torvalds 12551da177e4SLinus Torvalds cleanup_sysctl: 12561da177e4SLinus Torvalds scsi_exit_sysctl(); 12571da177e4SLinus Torvalds cleanup_hosts: 12581da177e4SLinus Torvalds scsi_exit_hosts(); 12591da177e4SLinus Torvalds cleanup_devlist: 12601da177e4SLinus Torvalds scsi_exit_devinfo(); 12611da177e4SLinus Torvalds cleanup_procfs: 12621da177e4SLinus Torvalds scsi_exit_procfs(); 12631da177e4SLinus Torvalds cleanup_queue: 12641da177e4SLinus Torvalds scsi_exit_queue(); 12651da177e4SLinus Torvalds printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n", 12661da177e4SLinus Torvalds -error); 12671da177e4SLinus Torvalds return error; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds static void __exit exit_scsi(void) 12711da177e4SLinus Torvalds { 12721da177e4SLinus Torvalds scsi_sysfs_unregister(); 12731da177e4SLinus Torvalds scsi_exit_sysctl(); 12741da177e4SLinus Torvalds scsi_exit_hosts(); 12751da177e4SLinus Torvalds scsi_exit_devinfo(); 12761da177e4SLinus Torvalds devfs_remove("scsi"); 12771da177e4SLinus Torvalds scsi_exit_procfs(); 12781da177e4SLinus Torvalds scsi_exit_queue(); 12791da177e4SLinus Torvalds } 12801da177e4SLinus Torvalds 12811da177e4SLinus Torvalds subsys_initcall(init_scsi); 12821da177e4SLinus Torvalds module_exit(exit_scsi); 1283