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/timer.h> 441da177e4SLinus Torvalds #include <linux/string.h> 451da177e4SLinus Torvalds #include <linux/slab.h> 461da177e4SLinus Torvalds #include <linux/blkdev.h> 471da177e4SLinus Torvalds #include <linux/delay.h> 481da177e4SLinus Torvalds #include <linux/init.h> 491da177e4SLinus Torvalds #include <linux/completion.h> 501da177e4SLinus Torvalds #include <linux/unistd.h> 511da177e4SLinus Torvalds #include <linux/spinlock.h> 521da177e4SLinus Torvalds #include <linux/kmod.h> 531da177e4SLinus Torvalds #include <linux/interrupt.h> 541da177e4SLinus Torvalds #include <linux/notifier.h> 551da177e4SLinus Torvalds #include <linux/cpu.h> 560b950672SArjan van de Ven #include <linux/mutex.h> 572955b47dSDan Williams #include <linux/async.h> 583c6bdaeaSMartin K. Petersen #include <asm/unaligned.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> 647b3d9545SLinus Torvalds #include <scsi/scsi_driver.h> 651da177e4SLinus Torvalds #include <scsi/scsi_eh.h> 661da177e4SLinus Torvalds #include <scsi/scsi_host.h> 671da177e4SLinus Torvalds #include <scsi/scsi_tcq.h> 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds #include "scsi_priv.h" 701da177e4SLinus Torvalds #include "scsi_logging.h" 711da177e4SLinus Torvalds 72bf816235SKei Tokunaga #define CREATE_TRACE_POINTS 73bf816235SKei Tokunaga #include <trace/events/scsi.h> 74bf816235SKei Tokunaga 7552c1da39SAdrian Bunk static void scsi_done(struct scsi_cmnd *cmd); 761da177e4SLinus Torvalds 771da177e4SLinus Torvalds /* 781da177e4SLinus Torvalds * Definitions and constants. 791da177e4SLinus Torvalds */ 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* 821da177e4SLinus Torvalds * Note - the initial logging level can be set here to log events at boot time. 831da177e4SLinus Torvalds * After the system is up, you may enable logging via the /proc interface. 841da177e4SLinus Torvalds */ 851da177e4SLinus Torvalds unsigned int scsi_logging_level; 861da177e4SLinus Torvalds #if defined(CONFIG_SCSI_LOGGING) 871da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_logging_level); 881da177e4SLinus Torvalds #endif 891da177e4SLinus Torvalds 90ea80dadeSJames Bottomley /* sd, scsi core and power management need to coordinate flushing async actions */ 912955b47dSDan Williams ASYNC_DOMAIN(scsi_sd_probe_domain); 92a7a20d10SDan Williams EXPORT_SYMBOL(scsi_sd_probe_domain); 93a7a20d10SDan Williams 943c31b52fSDan Williams /* 953c31b52fSDan Williams * Separate domain (from scsi_sd_probe_domain) to maximize the benefit of 963c31b52fSDan Williams * asynchronous system resume operations. It is marked 'exclusive' to avoid 973c31b52fSDan Williams * being included in the async_synchronize_full() that is invoked by 983c31b52fSDan Williams * dpm_resume() 993c31b52fSDan Williams */ 1003c31b52fSDan Williams ASYNC_DOMAIN_EXCLUSIVE(scsi_sd_pm_domain); 1013c31b52fSDan Williams EXPORT_SYMBOL(scsi_sd_pm_domain); 1023c31b52fSDan Williams 1038a1cdc9cSMatthew Wilcox /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI. 1048a1cdc9cSMatthew Wilcox * You may not alter any existing entry (although adding new ones is 1058a1cdc9cSMatthew Wilcox * encouraged once assigned by ANSI/INCITS T10 1068a1cdc9cSMatthew Wilcox */ 1074ff36718SMatthew Wilcox static const char *const scsi_device_types[] = { 1088a1cdc9cSMatthew Wilcox "Direct-Access ", 1098a1cdc9cSMatthew Wilcox "Sequential-Access", 1101da177e4SLinus Torvalds "Printer ", 1111da177e4SLinus Torvalds "Processor ", 1121da177e4SLinus Torvalds "WORM ", 1138a1cdc9cSMatthew Wilcox "CD-ROM ", 1141da177e4SLinus Torvalds "Scanner ", 1158a1cdc9cSMatthew Wilcox "Optical Device ", 1168a1cdc9cSMatthew Wilcox "Medium Changer ", 1171da177e4SLinus Torvalds "Communications ", 1184ff36718SMatthew Wilcox "ASC IT8 ", 1194ff36718SMatthew Wilcox "ASC IT8 ", 1201da177e4SLinus Torvalds "RAID ", 1211da177e4SLinus Torvalds "Enclosure ", 1228a1cdc9cSMatthew Wilcox "Direct-Access-RBC", 1234ff36718SMatthew Wilcox "Optical card ", 1244ff36718SMatthew Wilcox "Bridge controller", 1254ff36718SMatthew Wilcox "Object storage ", 1264ff36718SMatthew Wilcox "Automation/Drive ", 1271da177e4SLinus Torvalds }; 1284ff36718SMatthew Wilcox 129eb44820cSRob Landley /** 130eb44820cSRob Landley * scsi_device_type - Return 17 char string indicating device type. 131eb44820cSRob Landley * @type: type number to look up 132eb44820cSRob Landley */ 133eb44820cSRob Landley 1344ff36718SMatthew Wilcox const char * scsi_device_type(unsigned type) 1354ff36718SMatthew Wilcox { 1364ff36718SMatthew Wilcox if (type == 0x1e) 1374ff36718SMatthew Wilcox return "Well-known LUN "; 1384ff36718SMatthew Wilcox if (type == 0x1f) 1394ff36718SMatthew Wilcox return "No Device "; 14080c6e3c0SEric Sesterhenn if (type >= ARRAY_SIZE(scsi_device_types)) 1414ff36718SMatthew Wilcox return "Unknown "; 1424ff36718SMatthew Wilcox return scsi_device_types[type]; 1434ff36718SMatthew Wilcox } 1444ff36718SMatthew Wilcox 1454ff36718SMatthew Wilcox EXPORT_SYMBOL(scsi_device_type); 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds struct scsi_host_cmd_pool { 1485b7f1680SJames Bottomley struct kmem_cache *cmd_slab; 1495b7f1680SJames Bottomley struct kmem_cache *sense_slab; 1501da177e4SLinus Torvalds unsigned int users; 1515b7f1680SJames Bottomley char *cmd_name; 1525b7f1680SJames Bottomley char *sense_name; 1531da177e4SLinus Torvalds unsigned int slab_flags; 154c53033f6SAl Viro gfp_t gfp_mask; 1551da177e4SLinus Torvalds }; 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_pool = { 1585b7f1680SJames Bottomley .cmd_name = "scsi_cmd_cache", 1595b7f1680SJames Bottomley .sense_name = "scsi_sense_cache", 1601da177e4SLinus Torvalds .slab_flags = SLAB_HWCACHE_ALIGN, 1611da177e4SLinus Torvalds }; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_dma_pool = { 1645b7f1680SJames Bottomley .cmd_name = "scsi_cmd_cache(DMA)", 1655b7f1680SJames Bottomley .sense_name = "scsi_sense_cache(DMA)", 1661da177e4SLinus Torvalds .slab_flags = SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA, 1671da177e4SLinus Torvalds .gfp_mask = __GFP_DMA, 1681da177e4SLinus Torvalds }; 1691da177e4SLinus Torvalds 1700b950672SArjan van de Ven static DEFINE_MUTEX(host_cmd_pool_mutex); 1711da177e4SLinus Torvalds 172eb44820cSRob Landley /** 173e507e30bSJames Bottomley * scsi_pool_alloc_command - internal function to get a fully allocated command 174e507e30bSJames Bottomley * @pool: slab pool to allocate the command from 175e507e30bSJames Bottomley * @gfp_mask: mask for the allocation 176e507e30bSJames Bottomley * 177e507e30bSJames Bottomley * Returns a fully allocated command (with the allied sense buffer) or 178e507e30bSJames Bottomley * NULL on failure 179e507e30bSJames Bottomley */ 180e507e30bSJames Bottomley static struct scsi_cmnd * 181e507e30bSJames Bottomley scsi_pool_alloc_command(struct scsi_host_cmd_pool *pool, gfp_t gfp_mask) 182e507e30bSJames Bottomley { 183e507e30bSJames Bottomley struct scsi_cmnd *cmd; 184e507e30bSJames Bottomley 185ebef264bSWei Yongjun cmd = kmem_cache_zalloc(pool->cmd_slab, gfp_mask | pool->gfp_mask); 186e507e30bSJames Bottomley if (!cmd) 187e507e30bSJames Bottomley return NULL; 188e507e30bSJames Bottomley 189e507e30bSJames Bottomley cmd->sense_buffer = kmem_cache_alloc(pool->sense_slab, 190e507e30bSJames Bottomley gfp_mask | pool->gfp_mask); 191e507e30bSJames Bottomley if (!cmd->sense_buffer) { 192e507e30bSJames Bottomley kmem_cache_free(pool->cmd_slab, cmd); 193e507e30bSJames Bottomley return NULL; 194e507e30bSJames Bottomley } 195e507e30bSJames Bottomley 196e507e30bSJames Bottomley return cmd; 197e507e30bSJames Bottomley } 198e507e30bSJames Bottomley 199e507e30bSJames Bottomley /** 200e507e30bSJames Bottomley * scsi_pool_free_command - internal function to release a command 201e507e30bSJames Bottomley * @pool: slab pool to allocate the command from 202e507e30bSJames Bottomley * @cmd: command to release 203e507e30bSJames Bottomley * 204e507e30bSJames Bottomley * the command must previously have been allocated by 205e507e30bSJames Bottomley * scsi_pool_alloc_command. 206e507e30bSJames Bottomley */ 207e507e30bSJames Bottomley static void 208e507e30bSJames Bottomley scsi_pool_free_command(struct scsi_host_cmd_pool *pool, 209e507e30bSJames Bottomley struct scsi_cmnd *cmd) 210e507e30bSJames Bottomley { 2117027ad72SMartin K. Petersen if (cmd->prot_sdb) 2127027ad72SMartin K. Petersen kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb); 2137027ad72SMartin K. Petersen 214e507e30bSJames Bottomley kmem_cache_free(pool->sense_slab, cmd->sense_buffer); 215e507e30bSJames Bottomley kmem_cache_free(pool->cmd_slab, cmd); 216e507e30bSJames Bottomley } 217e507e30bSJames Bottomley 218e507e30bSJames Bottomley /** 2197027ad72SMartin K. Petersen * scsi_host_alloc_command - internal function to allocate command 2207027ad72SMartin K. Petersen * @shost: SCSI host whose pool to allocate from 2217027ad72SMartin K. Petersen * @gfp_mask: mask for the allocation 2227027ad72SMartin K. Petersen * 2237027ad72SMartin K. Petersen * Returns a fully allocated command with sense buffer and protection 2247027ad72SMartin K. Petersen * data buffer (where applicable) or NULL on failure 2257027ad72SMartin K. Petersen */ 2267027ad72SMartin K. Petersen static struct scsi_cmnd * 2277027ad72SMartin K. Petersen scsi_host_alloc_command(struct Scsi_Host *shost, gfp_t gfp_mask) 2287027ad72SMartin K. Petersen { 2297027ad72SMartin K. Petersen struct scsi_cmnd *cmd; 2307027ad72SMartin K. Petersen 2317027ad72SMartin K. Petersen cmd = scsi_pool_alloc_command(shost->cmd_pool, gfp_mask); 2327027ad72SMartin K. Petersen if (!cmd) 2337027ad72SMartin K. Petersen return NULL; 2347027ad72SMartin K. Petersen 2357027ad72SMartin K. Petersen if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) { 2367027ad72SMartin K. Petersen cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp_mask); 2377027ad72SMartin K. Petersen 2387027ad72SMartin K. Petersen if (!cmd->prot_sdb) { 2397027ad72SMartin K. Petersen scsi_pool_free_command(shost->cmd_pool, cmd); 2407027ad72SMartin K. Petersen return NULL; 2417027ad72SMartin K. Petersen } 2427027ad72SMartin K. Petersen } 2437027ad72SMartin K. Petersen 2447027ad72SMartin K. Petersen return cmd; 2457027ad72SMartin K. Petersen } 2467027ad72SMartin K. Petersen 2477027ad72SMartin K. Petersen /** 248eb44820cSRob Landley * __scsi_get_command - Allocate a struct scsi_cmnd 249eb44820cSRob Landley * @shost: host to transmit command 250eb44820cSRob Landley * @gfp_mask: allocation mask 251eb44820cSRob Landley * 252eb44820cSRob Landley * Description: allocate a struct scsi_cmd from host's slab, recycling from the 253eb44820cSRob Landley * host's free_list if necessary. 254eb44820cSRob Landley */ 255b58d9154SFUJITA Tomonori struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost, gfp_t gfp_mask) 2561da177e4SLinus Torvalds { 257b4c2554dSMartin K. Petersen struct scsi_cmnd *cmd = scsi_host_alloc_command(shost, gfp_mask); 258164fc5dcSHugh Dickins 2591da177e4SLinus Torvalds if (unlikely(!cmd)) { 2601da177e4SLinus Torvalds unsigned long flags; 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds spin_lock_irqsave(&shost->free_list_lock, flags); 2631da177e4SLinus Torvalds if (likely(!list_empty(&shost->free_list))) { 2641da177e4SLinus Torvalds cmd = list_entry(shost->free_list.next, 2651da177e4SLinus Torvalds struct scsi_cmnd, list); 2661da177e4SLinus Torvalds list_del_init(&cmd->list); 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds spin_unlock_irqrestore(&shost->free_list_lock, flags); 269de25deb1SFUJITA Tomonori 270de25deb1SFUJITA Tomonori if (cmd) { 271b4c2554dSMartin K. Petersen void *buf, *prot; 272b4c2554dSMartin K. Petersen 273de25deb1SFUJITA Tomonori buf = cmd->sense_buffer; 274b4c2554dSMartin K. Petersen prot = cmd->prot_sdb; 275b4c2554dSMartin K. Petersen 276de25deb1SFUJITA Tomonori memset(cmd, 0, sizeof(*cmd)); 277b4c2554dSMartin K. Petersen 278de25deb1SFUJITA Tomonori cmd->sense_buffer = buf; 279b4c2554dSMartin K. Petersen cmd->prot_sdb = prot; 280de25deb1SFUJITA Tomonori } 2811da177e4SLinus Torvalds } 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds return cmd; 2841da177e4SLinus Torvalds } 285b58d9154SFUJITA Tomonori EXPORT_SYMBOL_GPL(__scsi_get_command); 2861da177e4SLinus Torvalds 287eb44820cSRob Landley /** 288eb44820cSRob Landley * scsi_get_command - Allocate and setup a scsi command block 289eb44820cSRob Landley * @dev: parent scsi device 290eb44820cSRob Landley * @gfp_mask: allocator flags 2911da177e4SLinus Torvalds * 2921da177e4SLinus Torvalds * Returns: The allocated scsi command structure. 2931da177e4SLinus Torvalds */ 294c53033f6SAl Viro struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask) 2951da177e4SLinus Torvalds { 2961da177e4SLinus Torvalds struct scsi_cmnd *cmd; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds /* Bail if we can't get a reference to the device */ 2991da177e4SLinus Torvalds if (!get_device(&dev->sdev_gendev)) 3001da177e4SLinus Torvalds return NULL; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds cmd = __scsi_get_command(dev->host, gfp_mask); 3031da177e4SLinus Torvalds 3041da177e4SLinus Torvalds if (likely(cmd != NULL)) { 3051da177e4SLinus Torvalds unsigned long flags; 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds cmd->device = dev; 3081da177e4SLinus Torvalds INIT_LIST_HEAD(&cmd->list); 309e494f6a7SHannes Reinecke INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler); 3101da177e4SLinus Torvalds spin_lock_irqsave(&dev->list_lock, flags); 3111da177e4SLinus Torvalds list_add_tail(&cmd->list, &dev->cmd_list); 3121da177e4SLinus Torvalds spin_unlock_irqrestore(&dev->list_lock, flags); 31379e448bfSMatthew Dobson cmd->jiffies_at_alloc = jiffies; 3141da177e4SLinus Torvalds } else 3151da177e4SLinus Torvalds put_device(&dev->sdev_gendev); 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds return cmd; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_get_command); 3201da177e4SLinus Torvalds 321eb44820cSRob Landley /** 322eb44820cSRob Landley * __scsi_put_command - Free a struct scsi_cmnd 323eb44820cSRob Landley * @shost: dev->host 324eb44820cSRob Landley * @cmd: Command to free 325eb44820cSRob Landley * @dev: parent scsi device 326eb44820cSRob Landley */ 327b58d9154SFUJITA Tomonori void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd, 328b58d9154SFUJITA Tomonori struct device *dev) 329b58d9154SFUJITA Tomonori { 330b58d9154SFUJITA Tomonori unsigned long flags; 331b58d9154SFUJITA Tomonori 332b58d9154SFUJITA Tomonori /* changing locks here, don't need to restore the irq state */ 333b58d9154SFUJITA Tomonori spin_lock_irqsave(&shost->free_list_lock, flags); 334b58d9154SFUJITA Tomonori if (unlikely(list_empty(&shost->free_list))) { 335b58d9154SFUJITA Tomonori list_add(&cmd->list, &shost->free_list); 336b58d9154SFUJITA Tomonori cmd = NULL; 337b58d9154SFUJITA Tomonori } 338b58d9154SFUJITA Tomonori spin_unlock_irqrestore(&shost->free_list_lock, flags); 339b58d9154SFUJITA Tomonori 340e507e30bSJames Bottomley if (likely(cmd != NULL)) 341e507e30bSJames Bottomley scsi_pool_free_command(shost->cmd_pool, cmd); 342b58d9154SFUJITA Tomonori 343b58d9154SFUJITA Tomonori put_device(dev); 344b58d9154SFUJITA Tomonori } 345b58d9154SFUJITA Tomonori EXPORT_SYMBOL(__scsi_put_command); 346b58d9154SFUJITA Tomonori 347eb44820cSRob Landley /** 348eb44820cSRob Landley * scsi_put_command - Free a scsi command block 349eb44820cSRob Landley * @cmd: command block to free 3501da177e4SLinus Torvalds * 3511da177e4SLinus Torvalds * Returns: Nothing. 3521da177e4SLinus Torvalds * 3531da177e4SLinus Torvalds * Notes: The command must not belong to any lists. 3541da177e4SLinus Torvalds */ 3551da177e4SLinus Torvalds void scsi_put_command(struct scsi_cmnd *cmd) 3561da177e4SLinus Torvalds { 3571da177e4SLinus Torvalds struct scsi_device *sdev = cmd->device; 3581da177e4SLinus Torvalds unsigned long flags; 3591da177e4SLinus Torvalds 3601da177e4SLinus Torvalds /* serious error if the command hasn't come from a device list */ 3611da177e4SLinus Torvalds spin_lock_irqsave(&cmd->device->list_lock, flags); 3621da177e4SLinus Torvalds BUG_ON(list_empty(&cmd->list)); 3631da177e4SLinus Torvalds list_del_init(&cmd->list); 364b58d9154SFUJITA Tomonori spin_unlock_irqrestore(&cmd->device->list_lock, flags); 3651da177e4SLinus Torvalds 366e494f6a7SHannes Reinecke cancel_delayed_work(&cmd->abort_work); 367e494f6a7SHannes Reinecke 368b58d9154SFUJITA Tomonori __scsi_put_command(cmd->device->host, cmd, &sdev->sdev_gendev); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_put_command); 3711da177e4SLinus Torvalds 3721c353f7dSJames Bottomley static struct scsi_host_cmd_pool *scsi_get_host_cmd_pool(gfp_t gfp_mask) 3731da177e4SLinus Torvalds { 3741c353f7dSJames Bottomley struct scsi_host_cmd_pool *retval = NULL, *pool; 3751da177e4SLinus Torvalds /* 3761da177e4SLinus Torvalds * Select a command slab for this host and create it if not 377eb44820cSRob Landley * yet existent. 3781da177e4SLinus Torvalds */ 3790b950672SArjan van de Ven mutex_lock(&host_cmd_pool_mutex); 3801c353f7dSJames Bottomley pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool : 3811c353f7dSJames Bottomley &scsi_cmd_pool; 3821da177e4SLinus Torvalds if (!pool->users) { 3835b7f1680SJames Bottomley pool->cmd_slab = kmem_cache_create(pool->cmd_name, 3841da177e4SLinus Torvalds sizeof(struct scsi_cmnd), 0, 38520c2df83SPaul Mundt pool->slab_flags, NULL); 3865b7f1680SJames Bottomley if (!pool->cmd_slab) 3871da177e4SLinus Torvalds goto fail; 3885b7f1680SJames Bottomley 3895b7f1680SJames Bottomley pool->sense_slab = kmem_cache_create(pool->sense_name, 3905b7f1680SJames Bottomley SCSI_SENSE_BUFFERSIZE, 0, 3915b7f1680SJames Bottomley pool->slab_flags, NULL); 3925b7f1680SJames Bottomley if (!pool->sense_slab) { 3935b7f1680SJames Bottomley kmem_cache_destroy(pool->cmd_slab); 3945b7f1680SJames Bottomley goto fail; 3955b7f1680SJames Bottomley } 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds pool->users++; 3991c353f7dSJames Bottomley retval = pool; 4001c353f7dSJames Bottomley fail: 4010b950672SArjan van de Ven mutex_unlock(&host_cmd_pool_mutex); 4021c353f7dSJames Bottomley return retval; 4031c353f7dSJames Bottomley } 4041da177e4SLinus Torvalds 4051c353f7dSJames Bottomley static void scsi_put_host_cmd_pool(gfp_t gfp_mask) 4061c353f7dSJames Bottomley { 4071c353f7dSJames Bottomley struct scsi_host_cmd_pool *pool; 408de25deb1SFUJITA Tomonori 409166a7287SFUJITA Tomonori mutex_lock(&host_cmd_pool_mutex); 4101c353f7dSJames Bottomley pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool : 4111c353f7dSJames Bottomley &scsi_cmd_pool; 4121c353f7dSJames Bottomley /* 4131c353f7dSJames Bottomley * This may happen if a driver has a mismatched get and put 4141c353f7dSJames Bottomley * of the command pool; the driver should be implicated in 4151c353f7dSJames Bottomley * the stack trace 4161c353f7dSJames Bottomley */ 4171c353f7dSJames Bottomley BUG_ON(pool->users == 0); 4181c353f7dSJames Bottomley 4195b7f1680SJames Bottomley if (!--pool->users) { 4205b7f1680SJames Bottomley kmem_cache_destroy(pool->cmd_slab); 4215b7f1680SJames Bottomley kmem_cache_destroy(pool->sense_slab); 4225b7f1680SJames Bottomley } 4230b950672SArjan van de Ven mutex_unlock(&host_cmd_pool_mutex); 4241c353f7dSJames Bottomley } 4251c353f7dSJames Bottomley 4261c353f7dSJames Bottomley /** 4271c353f7dSJames Bottomley * scsi_allocate_command - get a fully allocated SCSI command 4281c353f7dSJames Bottomley * @gfp_mask: allocation mask 4291c353f7dSJames Bottomley * 4301c353f7dSJames Bottomley * This function is for use outside of the normal host based pools. 4311c353f7dSJames Bottomley * It allocates the relevant command and takes an additional reference 4321c353f7dSJames Bottomley * on the pool it used. This function *must* be paired with 4331c353f7dSJames Bottomley * scsi_free_command which also has the identical mask, otherwise the 4341c353f7dSJames Bottomley * free pool counts will eventually go wrong and you'll trigger a bug. 4351c353f7dSJames Bottomley * 4361c353f7dSJames Bottomley * This function should *only* be used by drivers that need a static 4371c353f7dSJames Bottomley * command allocation at start of day for internal functions. 4381c353f7dSJames Bottomley */ 4391c353f7dSJames Bottomley struct scsi_cmnd *scsi_allocate_command(gfp_t gfp_mask) 4401c353f7dSJames Bottomley { 4411c353f7dSJames Bottomley struct scsi_host_cmd_pool *pool = scsi_get_host_cmd_pool(gfp_mask); 4421c353f7dSJames Bottomley 4431c353f7dSJames Bottomley if (!pool) 4441c353f7dSJames Bottomley return NULL; 4451c353f7dSJames Bottomley 4461c353f7dSJames Bottomley return scsi_pool_alloc_command(pool, gfp_mask); 4471c353f7dSJames Bottomley } 4481c353f7dSJames Bottomley EXPORT_SYMBOL(scsi_allocate_command); 4491c353f7dSJames Bottomley 4501c353f7dSJames Bottomley /** 4511c353f7dSJames Bottomley * scsi_free_command - free a command allocated by scsi_allocate_command 4521c353f7dSJames Bottomley * @gfp_mask: mask used in the original allocation 4531c353f7dSJames Bottomley * @cmd: command to free 4541c353f7dSJames Bottomley * 4551c353f7dSJames Bottomley * Note: using the original allocation mask is vital because that's 4561c353f7dSJames Bottomley * what determines which command pool we use to free the command. Any 4571c353f7dSJames Bottomley * mismatch will cause the system to BUG eventually. 4581c353f7dSJames Bottomley */ 4591c353f7dSJames Bottomley void scsi_free_command(gfp_t gfp_mask, struct scsi_cmnd *cmd) 4601c353f7dSJames Bottomley { 4611c353f7dSJames Bottomley struct scsi_host_cmd_pool *pool = scsi_get_host_cmd_pool(gfp_mask); 4621c353f7dSJames Bottomley 4631c353f7dSJames Bottomley /* 4641c353f7dSJames Bottomley * this could trigger if the mask to scsi_allocate_command 4651c353f7dSJames Bottomley * doesn't match this mask. Otherwise we're guaranteed that this 4661c353f7dSJames Bottomley * succeeds because scsi_allocate_command must have taken a reference 4671c353f7dSJames Bottomley * on the pool 4681c353f7dSJames Bottomley */ 4691c353f7dSJames Bottomley BUG_ON(!pool); 4701c353f7dSJames Bottomley 4711c353f7dSJames Bottomley scsi_pool_free_command(pool, cmd); 4721c353f7dSJames Bottomley /* 4731c353f7dSJames Bottomley * scsi_put_host_cmd_pool is called twice; once to release the 4741c353f7dSJames Bottomley * reference we took above, and once to release the reference 4751c353f7dSJames Bottomley * originally taken by scsi_allocate_command 4761c353f7dSJames Bottomley */ 4771c353f7dSJames Bottomley scsi_put_host_cmd_pool(gfp_mask); 4781c353f7dSJames Bottomley scsi_put_host_cmd_pool(gfp_mask); 4791c353f7dSJames Bottomley } 4801c353f7dSJames Bottomley EXPORT_SYMBOL(scsi_free_command); 4811c353f7dSJames Bottomley 4821c353f7dSJames Bottomley /** 4831c353f7dSJames Bottomley * scsi_setup_command_freelist - Setup the command freelist for a scsi host. 4841c353f7dSJames Bottomley * @shost: host to allocate the freelist for. 4851c353f7dSJames Bottomley * 4861c353f7dSJames Bottomley * Description: The command freelist protects against system-wide out of memory 4871c353f7dSJames Bottomley * deadlock by preallocating one SCSI command structure for each host, so the 4881c353f7dSJames Bottomley * system can always write to a swap file on a device associated with that host. 4891c353f7dSJames Bottomley * 4901c353f7dSJames Bottomley * Returns: Nothing. 4911c353f7dSJames Bottomley */ 4921c353f7dSJames Bottomley int scsi_setup_command_freelist(struct Scsi_Host *shost) 4931c353f7dSJames Bottomley { 4941c353f7dSJames Bottomley struct scsi_cmnd *cmd; 4951c353f7dSJames Bottomley const gfp_t gfp_mask = shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL; 4961c353f7dSJames Bottomley 4971c353f7dSJames Bottomley spin_lock_init(&shost->free_list_lock); 4981c353f7dSJames Bottomley INIT_LIST_HEAD(&shost->free_list); 4991c353f7dSJames Bottomley 5001c353f7dSJames Bottomley shost->cmd_pool = scsi_get_host_cmd_pool(gfp_mask); 5011c353f7dSJames Bottomley 5021c353f7dSJames Bottomley if (!shost->cmd_pool) 5031da177e4SLinus Torvalds return -ENOMEM; 5041c353f7dSJames Bottomley 5051c353f7dSJames Bottomley /* 5061c353f7dSJames Bottomley * Get one backup command for this host. 5071c353f7dSJames Bottomley */ 5087027ad72SMartin K. Petersen cmd = scsi_host_alloc_command(shost, gfp_mask); 5091c353f7dSJames Bottomley if (!cmd) { 5101c353f7dSJames Bottomley scsi_put_host_cmd_pool(gfp_mask); 51161d7416aSAlan D. Brunelle shost->cmd_pool = NULL; 5121c353f7dSJames Bottomley return -ENOMEM; 5131c353f7dSJames Bottomley } 5141c353f7dSJames Bottomley list_add(&cmd->list, &shost->free_list); 5151c353f7dSJames Bottomley return 0; 5161da177e4SLinus Torvalds } 5171da177e4SLinus Torvalds 518eb44820cSRob Landley /** 519eb44820cSRob Landley * scsi_destroy_command_freelist - Release the command freelist for a scsi host. 520eb44820cSRob Landley * @shost: host whose freelist is going to be destroyed 5211da177e4SLinus Torvalds */ 5221da177e4SLinus Torvalds void scsi_destroy_command_freelist(struct Scsi_Host *shost) 5231da177e4SLinus Torvalds { 52461d7416aSAlan D. Brunelle /* 52561d7416aSAlan D. Brunelle * If cmd_pool is NULL the free list was not initialized, so 52661d7416aSAlan D. Brunelle * do not attempt to release resources. 52761d7416aSAlan D. Brunelle */ 52861d7416aSAlan D. Brunelle if (!shost->cmd_pool) 52961d7416aSAlan D. Brunelle return; 53061d7416aSAlan D. Brunelle 5311da177e4SLinus Torvalds while (!list_empty(&shost->free_list)) { 5321da177e4SLinus Torvalds struct scsi_cmnd *cmd; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list); 5351da177e4SLinus Torvalds list_del_init(&cmd->list); 536e507e30bSJames Bottomley scsi_pool_free_command(shost->cmd_pool, cmd); 5371da177e4SLinus Torvalds } 5381c353f7dSJames Bottomley shost->cmd_pool = NULL; 5391c353f7dSJames Bottomley scsi_put_host_cmd_pool(shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL); 540de25deb1SFUJITA Tomonori } 541de25deb1SFUJITA Tomonori 5421da177e4SLinus Torvalds #ifdef CONFIG_SCSI_LOGGING 5431da177e4SLinus Torvalds void scsi_log_send(struct scsi_cmnd *cmd) 5441da177e4SLinus Torvalds { 5451da177e4SLinus Torvalds unsigned int level; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /* 5481da177e4SLinus Torvalds * If ML QUEUE log level is greater than or equal to: 5491da177e4SLinus Torvalds * 5501da177e4SLinus Torvalds * 1: nothing (match completion) 5511da177e4SLinus Torvalds * 5521da177e4SLinus Torvalds * 2: log opcode + command of all commands 5531da177e4SLinus Torvalds * 5541da177e4SLinus Torvalds * 3: same as 2 plus dump cmd address 5551da177e4SLinus Torvalds * 5561da177e4SLinus Torvalds * 4: same as 3 plus dump extra junk 5571da177e4SLinus Torvalds */ 5581da177e4SLinus Torvalds if (unlikely(scsi_logging_level)) { 5591da177e4SLinus Torvalds level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT, 5601da177e4SLinus Torvalds SCSI_LOG_MLQUEUE_BITS); 5611da177e4SLinus Torvalds if (level > 1) { 562a4d04a4cSMartin K. Petersen scmd_printk(KERN_INFO, cmd, "Send: "); 5631da177e4SLinus Torvalds if (level > 2) 5641da177e4SLinus Torvalds printk("0x%p ", cmd); 565a4d04a4cSMartin K. Petersen printk("\n"); 5661da177e4SLinus Torvalds scsi_print_command(cmd); 5671da177e4SLinus Torvalds if (level > 3) { 5681da177e4SLinus Torvalds printk(KERN_INFO "buffer = 0x%p, bufflen = %d," 5697b3d9545SLinus Torvalds " queuecommand 0x%p\n", 570a73e45b3SBoaz Harrosh scsi_sglist(cmd), scsi_bufflen(cmd), 571a4d04a4cSMartin K. Petersen cmd->device->host->hostt->queuecommand); 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds } 5751da177e4SLinus Torvalds } 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds void scsi_log_completion(struct scsi_cmnd *cmd, int disposition) 5791da177e4SLinus Torvalds { 5801da177e4SLinus Torvalds unsigned int level; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* 5831da177e4SLinus Torvalds * If ML COMPLETE log level is greater than or equal to: 5841da177e4SLinus Torvalds * 5851da177e4SLinus Torvalds * 1: log disposition, result, opcode + command, and conditionally 5861da177e4SLinus Torvalds * sense data for failures or non SUCCESS dispositions. 5871da177e4SLinus Torvalds * 5881da177e4SLinus Torvalds * 2: same as 1 but for all command completions. 5891da177e4SLinus Torvalds * 5901da177e4SLinus Torvalds * 3: same as 2 plus dump cmd address 5911da177e4SLinus Torvalds * 5921da177e4SLinus Torvalds * 4: same as 3 plus dump extra junk 5931da177e4SLinus Torvalds */ 5941da177e4SLinus Torvalds if (unlikely(scsi_logging_level)) { 5951da177e4SLinus Torvalds level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT, 5961da177e4SLinus Torvalds SCSI_LOG_MLCOMPLETE_BITS); 5971da177e4SLinus Torvalds if (((level > 0) && (cmd->result || disposition != SUCCESS)) || 5981da177e4SLinus Torvalds (level > 1)) { 599a4d04a4cSMartin K. Petersen scmd_printk(KERN_INFO, cmd, "Done: "); 6001da177e4SLinus Torvalds if (level > 2) 6011da177e4SLinus Torvalds printk("0x%p ", cmd); 6021da177e4SLinus Torvalds /* 6031da177e4SLinus Torvalds * Dump truncated values, so we usually fit within 6041da177e4SLinus Torvalds * 80 chars. 6051da177e4SLinus Torvalds */ 6061da177e4SLinus Torvalds switch (disposition) { 6071da177e4SLinus Torvalds case SUCCESS: 608a4d04a4cSMartin K. Petersen printk("SUCCESS\n"); 6091da177e4SLinus Torvalds break; 6101da177e4SLinus Torvalds case NEEDS_RETRY: 611a4d04a4cSMartin K. Petersen printk("RETRY\n"); 6121da177e4SLinus Torvalds break; 6131da177e4SLinus Torvalds case ADD_TO_MLQUEUE: 614a4d04a4cSMartin K. Petersen printk("MLQUEUE\n"); 6151da177e4SLinus Torvalds break; 6161da177e4SLinus Torvalds case FAILED: 617a4d04a4cSMartin K. Petersen printk("FAILED\n"); 6181da177e4SLinus Torvalds break; 6191da177e4SLinus Torvalds case TIMEOUT_ERROR: 6201da177e4SLinus Torvalds /* 6211da177e4SLinus Torvalds * If called via scsi_times_out. 6221da177e4SLinus Torvalds */ 623a4d04a4cSMartin K. Petersen printk("TIMEOUT\n"); 6241da177e4SLinus Torvalds break; 6251da177e4SLinus Torvalds default: 626a4d04a4cSMartin K. Petersen printk("UNKNOWN\n"); 6271da177e4SLinus Torvalds } 628a4d04a4cSMartin K. Petersen scsi_print_result(cmd); 6291da177e4SLinus Torvalds scsi_print_command(cmd); 630a4d04a4cSMartin K. Petersen if (status_byte(cmd->result) & CHECK_CONDITION) 6311da177e4SLinus Torvalds scsi_print_sense("", cmd); 632a4d04a4cSMartin K. Petersen if (level > 3) 633a4d04a4cSMartin K. Petersen scmd_printk(KERN_INFO, cmd, 634a4d04a4cSMartin K. Petersen "scsi host busy %d failed %d\n", 635a4d04a4cSMartin K. Petersen cmd->device->host->host_busy, 636a4d04a4cSMartin K. Petersen cmd->device->host->host_failed); 6371da177e4SLinus Torvalds } 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds #endif 6411da177e4SLinus Torvalds 642eb44820cSRob Landley /** 643eb44820cSRob Landley * scsi_cmd_get_serial - Assign a serial number to a command 644eb44820cSRob Landley * @host: the scsi host 645eb44820cSRob Landley * @cmd: command to assign serial number to 646eb44820cSRob Landley * 647eb44820cSRob Landley * Description: a serial number identifies a request for error recovery 6481da177e4SLinus Torvalds * and debugging purposes. Protected by the Host_Lock of host. 6491da177e4SLinus Torvalds */ 650f281233dSJeff Garzik void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd) 6511da177e4SLinus Torvalds { 6521da177e4SLinus Torvalds cmd->serial_number = host->cmd_serial_number++; 6531da177e4SLinus Torvalds if (cmd->serial_number == 0) 6541da177e4SLinus Torvalds cmd->serial_number = host->cmd_serial_number++; 6551da177e4SLinus Torvalds } 656f281233dSJeff Garzik EXPORT_SYMBOL(scsi_cmd_get_serial); 6571da177e4SLinus Torvalds 658eb44820cSRob Landley /** 659eb44820cSRob Landley * scsi_dispatch_command - Dispatch a command to the low-level driver. 660eb44820cSRob Landley * @cmd: command block we are dispatching. 6611da177e4SLinus Torvalds * 662eb44820cSRob Landley * Return: nonzero return request was rejected and device's queue needs to be 663eb44820cSRob Landley * plugged. 6641da177e4SLinus Torvalds */ 6651da177e4SLinus Torvalds int scsi_dispatch_cmd(struct scsi_cmnd *cmd) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds struct Scsi_Host *host = cmd->device->host; 6681da177e4SLinus Torvalds int rtn = 0; 6691da177e4SLinus Torvalds 670242f9dcbSJens Axboe atomic_inc(&cmd->device->iorequest_cnt); 671242f9dcbSJens Axboe 6721da177e4SLinus Torvalds /* check if the device is still usable */ 6731da177e4SLinus Torvalds if (unlikely(cmd->device->sdev_state == SDEV_DEL)) { 6741da177e4SLinus Torvalds /* in SDEV_DEL we error all commands. DID_NO_CONNECT 6751da177e4SLinus Torvalds * returns an immediate error upwards, and signals 6761da177e4SLinus Torvalds * that the device is no longer present */ 6771da177e4SLinus Torvalds cmd->result = DID_NO_CONNECT << 16; 678242f9dcbSJens Axboe scsi_done(cmd); 6791da177e4SLinus Torvalds /* return 0 (because the command has been processed) */ 6801da177e4SLinus Torvalds goto out; 6811da177e4SLinus Torvalds } 6821da177e4SLinus Torvalds 6830f1d87a2SJames Bottomley /* Check to see if the scsi lld made this device blocked. */ 6840f1d87a2SJames Bottomley if (unlikely(scsi_device_blocked(cmd->device))) { 6851da177e4SLinus Torvalds /* 6860f1d87a2SJames Bottomley * in blocked state, the command is just put back on 6870f1d87a2SJames Bottomley * the device queue. The suspend state has already 6880f1d87a2SJames Bottomley * blocked the queue so future requests should not 6890f1d87a2SJames Bottomley * occur until the device transitions out of the 6900f1d87a2SJames Bottomley * suspend state. 6911da177e4SLinus Torvalds */ 692242f9dcbSJens Axboe 6931da177e4SLinus Torvalds scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY); 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n")); 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds /* 6981da177e4SLinus Torvalds * NOTE: rtn is still zero here because we don't need the 6991da177e4SLinus Torvalds * queue to be plugged on return (it's already stopped) 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds goto out; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds /* 7051da177e4SLinus Torvalds * If SCSI-2 or lower, store the LUN value in cmnd. 7061da177e4SLinus Torvalds */ 7074d7db04aSJames Bottomley if (cmd->device->scsi_level <= SCSI_2 && 7084d7db04aSJames Bottomley cmd->device->scsi_level != SCSI_UNKNOWN) { 7091da177e4SLinus Torvalds cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) | 7101da177e4SLinus Torvalds (cmd->device->lun << 5 & 0xe0); 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds 7131da177e4SLinus Torvalds scsi_log_send(cmd); 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds /* 7161da177e4SLinus Torvalds * Before we queue this command, check if the command 7171da177e4SLinus Torvalds * length exceeds what the host adapter can handle. 7181da177e4SLinus Torvalds */ 719db4742ddSBoaz Harrosh if (cmd->cmd_len > cmd->device->host->max_cmd_len) { 7201da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, 721db4742ddSBoaz Harrosh printk("queuecommand : command too long. " 722db4742ddSBoaz Harrosh "cdb_size=%d host->max_cmd_len=%d\n", 723db4742ddSBoaz Harrosh cmd->cmd_len, cmd->device->host->max_cmd_len)); 7241da177e4SLinus Torvalds cmd->result = (DID_ABORT << 16); 7251da177e4SLinus Torvalds 7261da177e4SLinus Torvalds scsi_done(cmd); 7271da177e4SLinus Torvalds goto out; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 730d2c9d9eaSMike Anderson if (unlikely(host->shost_state == SHOST_DEL)) { 7311da177e4SLinus Torvalds cmd->result = (DID_NO_CONNECT << 16); 7321da177e4SLinus Torvalds scsi_done(cmd); 7331da177e4SLinus Torvalds } else { 734bf816235SKei Tokunaga trace_scsi_dispatch_cmd_start(cmd); 735f281233dSJeff Garzik cmd->scsi_done = scsi_done; 736f281233dSJeff Garzik rtn = host->hostt->queuecommand(host, cmd); 7371da177e4SLinus Torvalds } 738f281233dSJeff Garzik 7391da177e4SLinus Torvalds if (rtn) { 740bf816235SKei Tokunaga trace_scsi_dispatch_cmd_error(cmd, rtn); 741f0c0a376SMike Christie if (rtn != SCSI_MLQUEUE_DEVICE_BUSY && 742f0c0a376SMike Christie rtn != SCSI_MLQUEUE_TARGET_BUSY) 743f0c0a376SMike Christie rtn = SCSI_MLQUEUE_HOST_BUSY; 744f0c0a376SMike Christie 745f0c0a376SMike Christie scsi_queue_insert(cmd, rtn); 746f0c0a376SMike Christie 7471da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, 7481da177e4SLinus Torvalds printk("queuecommand : request rejected\n")); 7491da177e4SLinus Torvalds } 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds out: 7521da177e4SLinus Torvalds SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n")); 7531da177e4SLinus Torvalds return rtn; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds /** 7576ad55502SHannes Reinecke * scsi_done - Invoke completion on finished SCSI command. 7581da177e4SLinus Torvalds * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives 7591da177e4SLinus Torvalds * ownership back to SCSI Core -- i.e. the LLDD has finished with it. 7601da177e4SLinus Torvalds * 761eb44820cSRob Landley * Description: This function is the mid-level's (SCSI Core) interrupt routine, 762eb44820cSRob Landley * which regains ownership of the SCSI command (de facto) from a LLDD, and 7636ad55502SHannes Reinecke * calls blk_complete_request() for further processing. 7641da177e4SLinus Torvalds * 7651da177e4SLinus Torvalds * This function is interrupt context safe. 7661da177e4SLinus Torvalds */ 76752c1da39SAdrian Bunk static void scsi_done(struct scsi_cmnd *cmd) 7681da177e4SLinus Torvalds { 769bf816235SKei Tokunaga trace_scsi_dispatch_cmd_done(cmd); 770242f9dcbSJens Axboe blk_complete_request(cmd->request); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 773eb44820cSRob Landley /** 774eb44820cSRob Landley * scsi_finish_command - cleanup and pass command back to upper layer 775eb44820cSRob Landley * @cmd: the command 7761da177e4SLinus Torvalds * 777eb44820cSRob Landley * Description: Pass command off to upper layer for finishing of I/O 7781da177e4SLinus Torvalds * request, waking processes that are waiting on results, 7791da177e4SLinus Torvalds * etc. 7801da177e4SLinus Torvalds */ 7811da177e4SLinus Torvalds void scsi_finish_command(struct scsi_cmnd *cmd) 7821da177e4SLinus Torvalds { 7831da177e4SLinus Torvalds struct scsi_device *sdev = cmd->device; 784f0c0a376SMike Christie struct scsi_target *starget = scsi_target(sdev); 7851da177e4SLinus Torvalds struct Scsi_Host *shost = sdev->host; 7867b3d9545SLinus Torvalds struct scsi_driver *drv; 7877b3d9545SLinus Torvalds unsigned int good_bytes; 7881da177e4SLinus Torvalds 7891da177e4SLinus Torvalds scsi_device_unbusy(sdev); 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds /* 7921da177e4SLinus Torvalds * Clear the flags which say that the device/host is no longer 7931da177e4SLinus Torvalds * capable of accepting new commands. These are set in scsi_queue.c 7941da177e4SLinus Torvalds * for both the queue full condition on a device, and for a 7951da177e4SLinus Torvalds * host full condition on the host. 7961da177e4SLinus Torvalds * 7971da177e4SLinus Torvalds * XXX(hch): What about locking? 7981da177e4SLinus Torvalds */ 7991da177e4SLinus Torvalds shost->host_blocked = 0; 800f0c0a376SMike Christie starget->target_blocked = 0; 8011da177e4SLinus Torvalds sdev->device_blocked = 0; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds /* 8041da177e4SLinus Torvalds * If we have valid sense information, then some kind of recovery 8051da177e4SLinus Torvalds * must have taken place. Make a note of this. 8061da177e4SLinus Torvalds */ 8071da177e4SLinus Torvalds if (SCSI_SENSE_VALID(cmd)) 8081da177e4SLinus Torvalds cmd->result |= (DRIVER_SENSE << 24); 8091da177e4SLinus Torvalds 8103bf743e7SJeff Garzik SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev, 8113bf743e7SJeff Garzik "Notifying upper driver of completion " 8123bf743e7SJeff Garzik "(result %x)\n", cmd->result)); 8131da177e4SLinus Torvalds 814f18573abSFUJITA Tomonori good_bytes = scsi_bufflen(cmd); 8157b3d9545SLinus Torvalds if (cmd->request->cmd_type != REQ_TYPE_BLOCK_PC) { 816427e59f0SJames Bottomley int old_good_bytes = good_bytes; 8177b3d9545SLinus Torvalds drv = scsi_cmd_to_driver(cmd); 8187b3d9545SLinus Torvalds if (drv->done) 8197b3d9545SLinus Torvalds good_bytes = drv->done(cmd); 820427e59f0SJames Bottomley /* 821427e59f0SJames Bottomley * USB may not give sense identifying bad sector and 822427e59f0SJames Bottomley * simply return a residue instead, so subtract off the 823427e59f0SJames Bottomley * residue if drv->done() error processing indicates no 824427e59f0SJames Bottomley * change to the completion length. 825427e59f0SJames Bottomley */ 826427e59f0SJames Bottomley if (good_bytes == old_good_bytes) 827427e59f0SJames Bottomley good_bytes -= scsi_get_resid(cmd); 8287b3d9545SLinus Torvalds } 8297b3d9545SLinus Torvalds scsi_io_completion(cmd, good_bytes); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_finish_command); 8321da177e4SLinus Torvalds 833eb44820cSRob Landley /** 834eb44820cSRob Landley * scsi_adjust_queue_depth - Let low level drivers change a device's queue depth 835eb44820cSRob Landley * @sdev: SCSI Device in question 836eb44820cSRob Landley * @tagged: Do we use tagged queueing (non-0) or do we treat 8371da177e4SLinus Torvalds * this device as an untagged device (0) 838eb44820cSRob Landley * @tags: Number of tags allowed if tagged queueing enabled, 8391da177e4SLinus Torvalds * or number of commands the low level driver can 8401da177e4SLinus Torvalds * queue up in non-tagged mode (as per cmd_per_lun). 8411da177e4SLinus Torvalds * 8421da177e4SLinus Torvalds * Returns: Nothing 8431da177e4SLinus Torvalds * 8441da177e4SLinus Torvalds * Lock Status: None held on entry 8451da177e4SLinus Torvalds * 8461da177e4SLinus Torvalds * Notes: Low level drivers may call this at any time and we will do 8471da177e4SLinus Torvalds * the right thing depending on whether or not the device is 8481da177e4SLinus Torvalds * currently active and whether or not it even has the 8491da177e4SLinus Torvalds * command blocks built yet. 8501da177e4SLinus Torvalds */ 8511da177e4SLinus Torvalds void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags) 8521da177e4SLinus Torvalds { 8531da177e4SLinus Torvalds unsigned long flags; 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds /* 8561da177e4SLinus Torvalds * refuse to set tagged depth to an unworkable size 8571da177e4SLinus Torvalds */ 8581da177e4SLinus Torvalds if (tags <= 0) 8591da177e4SLinus Torvalds return; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds spin_lock_irqsave(sdev->request_queue->queue_lock, flags); 8621da177e4SLinus Torvalds 863885ace9eSMike Christie /* 864885ace9eSMike Christie * Check to see if the queue is managed by the block layer. 865885ace9eSMike Christie * If it is, and we fail to adjust the depth, exit. 866885ace9eSMike Christie * 867885ace9eSMike Christie * Do not resize the tag map if it is a host wide share bqt, 868885ace9eSMike Christie * because the size should be the hosts's can_queue. If there 869885ace9eSMike Christie * is more IO than the LLD's can_queue (so there are not enuogh 870885ace9eSMike Christie * tags) request_fn's host queue ready check will handle it. 871885ace9eSMike Christie */ 872885ace9eSMike Christie if (!sdev->host->bqt) { 8731da177e4SLinus Torvalds if (blk_queue_tagged(sdev->request_queue) && 8741da177e4SLinus Torvalds blk_queue_resize_tags(sdev->request_queue, tags) != 0) 8751da177e4SLinus Torvalds goto out; 876885ace9eSMike Christie } 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds sdev->queue_depth = tags; 8791da177e4SLinus Torvalds switch (tagged) { 8801da177e4SLinus Torvalds case MSG_ORDERED_TAG: 8811da177e4SLinus Torvalds sdev->ordered_tags = 1; 8821da177e4SLinus Torvalds sdev->simple_tags = 1; 8831da177e4SLinus Torvalds break; 8841da177e4SLinus Torvalds case MSG_SIMPLE_TAG: 8851da177e4SLinus Torvalds sdev->ordered_tags = 0; 8861da177e4SLinus Torvalds sdev->simple_tags = 1; 8871da177e4SLinus Torvalds break; 8881da177e4SLinus Torvalds default: 8899ccfc756SJames Bottomley sdev_printk(KERN_WARNING, sdev, 8901da177e4SLinus Torvalds "scsi_adjust_queue_depth, bad queue type, " 8919ccfc756SJames Bottomley "disabled\n"); 8921da177e4SLinus Torvalds case 0: 8931da177e4SLinus Torvalds sdev->ordered_tags = sdev->simple_tags = 0; 8941da177e4SLinus Torvalds sdev->queue_depth = tags; 8951da177e4SLinus Torvalds break; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds out: 8981da177e4SLinus Torvalds spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_adjust_queue_depth); 9011da177e4SLinus Torvalds 902eb44820cSRob Landley /** 903eb44820cSRob Landley * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth 904eb44820cSRob Landley * @sdev: SCSI Device in question 905eb44820cSRob Landley * @depth: Current number of outstanding SCSI commands on this device, 906eb44820cSRob Landley * not counting the one returned as QUEUE_FULL. 9071da177e4SLinus Torvalds * 908eb44820cSRob Landley * Description: This function will track successive QUEUE_FULL events on a 9091da177e4SLinus Torvalds * specific SCSI device to determine if and when there is a 9101da177e4SLinus Torvalds * need to adjust the queue depth on the device. 9111da177e4SLinus Torvalds * 912eb44820cSRob Landley * Returns: 0 - No change needed, >0 - Adjust queue depth to this new depth, 9131da177e4SLinus Torvalds * -1 - Drop back to untagged operation using host->cmd_per_lun 9141da177e4SLinus Torvalds * as the untagged command depth 9151da177e4SLinus Torvalds * 9161da177e4SLinus Torvalds * Lock Status: None held on entry 9171da177e4SLinus Torvalds * 9181da177e4SLinus Torvalds * Notes: Low level drivers may call this at any time and we will do 9191da177e4SLinus Torvalds * "The Right Thing." We are interrupt context safe. 9201da177e4SLinus Torvalds */ 9211da177e4SLinus Torvalds int scsi_track_queue_full(struct scsi_device *sdev, int depth) 9221da177e4SLinus Torvalds { 9234a84067dSVasu Dev 9244a84067dSVasu Dev /* 9254a84067dSVasu Dev * Don't let QUEUE_FULLs on the same 9264a84067dSVasu Dev * jiffies count, they could all be from 9274a84067dSVasu Dev * same event. 9284a84067dSVasu Dev */ 9294a84067dSVasu Dev if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4)) 9301da177e4SLinus Torvalds return 0; 9311da177e4SLinus Torvalds 9324a84067dSVasu Dev sdev->last_queue_full_time = jiffies; 9331da177e4SLinus Torvalds if (sdev->last_queue_full_depth != depth) { 9341da177e4SLinus Torvalds sdev->last_queue_full_count = 1; 9351da177e4SLinus Torvalds sdev->last_queue_full_depth = depth; 9361da177e4SLinus Torvalds } else { 9371da177e4SLinus Torvalds sdev->last_queue_full_count++; 9381da177e4SLinus Torvalds } 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds if (sdev->last_queue_full_count <= 10) 9411da177e4SLinus Torvalds return 0; 9421da177e4SLinus Torvalds if (sdev->last_queue_full_depth < 8) { 9431da177e4SLinus Torvalds /* Drop back to untagged */ 9441da177e4SLinus Torvalds scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun); 9451da177e4SLinus Torvalds return -1; 9461da177e4SLinus Torvalds } 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds if (sdev->ordered_tags) 9491da177e4SLinus Torvalds scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth); 9501da177e4SLinus Torvalds else 9511da177e4SLinus Torvalds scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth); 9521da177e4SLinus Torvalds return depth; 9531da177e4SLinus Torvalds } 9541da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_track_queue_full); 9551da177e4SLinus Torvalds 9561da177e4SLinus Torvalds /** 957881a256dSMatthew Wilcox * scsi_vpd_inquiry - Request a device provide us with a VPD page 958881a256dSMatthew Wilcox * @sdev: The device to ask 959881a256dSMatthew Wilcox * @buffer: Where to put the result 960881a256dSMatthew Wilcox * @page: Which Vital Product Data to return 961881a256dSMatthew Wilcox * @len: The length of the buffer 962881a256dSMatthew Wilcox * 963881a256dSMatthew Wilcox * This is an internal helper function. You probably want to use 964881a256dSMatthew Wilcox * scsi_get_vpd_page instead. 965881a256dSMatthew Wilcox * 966881a256dSMatthew Wilcox * Returns 0 on success or a negative error number. 967881a256dSMatthew Wilcox */ 968881a256dSMatthew Wilcox static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer, 969881a256dSMatthew Wilcox u8 page, unsigned len) 970881a256dSMatthew Wilcox { 971881a256dSMatthew Wilcox int result; 972881a256dSMatthew Wilcox unsigned char cmd[16]; 973881a256dSMatthew Wilcox 974881a256dSMatthew Wilcox cmd[0] = INQUIRY; 975881a256dSMatthew Wilcox cmd[1] = 1; /* EVPD */ 976881a256dSMatthew Wilcox cmd[2] = page; 977881a256dSMatthew Wilcox cmd[3] = len >> 8; 978881a256dSMatthew Wilcox cmd[4] = len & 0xff; 979881a256dSMatthew Wilcox cmd[5] = 0; /* Control byte */ 980881a256dSMatthew Wilcox 981881a256dSMatthew Wilcox /* 982881a256dSMatthew Wilcox * I'm not convinced we need to try quite this hard to get VPD, but 983881a256dSMatthew Wilcox * all the existing users tried this hard. 984881a256dSMatthew Wilcox */ 985881a256dSMatthew Wilcox result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, 98695a3639eSJames Bottomley len, NULL, 30 * HZ, 3, NULL); 987881a256dSMatthew Wilcox if (result) 988881a256dSMatthew Wilcox return result; 989881a256dSMatthew Wilcox 990881a256dSMatthew Wilcox /* Sanity check that we got the page back that we asked for */ 991881a256dSMatthew Wilcox if (buffer[1] != page) 992881a256dSMatthew Wilcox return -EIO; 993881a256dSMatthew Wilcox 994881a256dSMatthew Wilcox return 0; 995881a256dSMatthew Wilcox } 996881a256dSMatthew Wilcox 997881a256dSMatthew Wilcox /** 998881a256dSMatthew Wilcox * scsi_get_vpd_page - Get Vital Product Data from a SCSI device 999881a256dSMatthew Wilcox * @sdev: The device to ask 1000881a256dSMatthew Wilcox * @page: Which Vital Product Data to return 1001786f8ba2SRandy Dunlap * @buf: where to store the VPD 1002786f8ba2SRandy Dunlap * @buf_len: number of bytes in the VPD buffer area 1003881a256dSMatthew Wilcox * 1004881a256dSMatthew Wilcox * SCSI devices may optionally supply Vital Product Data. Each 'page' 1005881a256dSMatthew Wilcox * of VPD is defined in the appropriate SCSI document (eg SPC, SBC). 1006881a256dSMatthew Wilcox * If the device supports this VPD page, this routine returns a pointer 1007881a256dSMatthew Wilcox * to a buffer containing the data from that page. The caller is 1008881a256dSMatthew Wilcox * responsible for calling kfree() on this pointer when it is no longer 1009881a256dSMatthew Wilcox * needed. If we cannot retrieve the VPD page this routine returns %NULL. 1010881a256dSMatthew Wilcox */ 1011e3deec09SJames Bottomley int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf, 1012e3deec09SJames Bottomley int buf_len) 1013881a256dSMatthew Wilcox { 1014881a256dSMatthew Wilcox int i, result; 1015881a256dSMatthew Wilcox 10167562523eSMartin K. Petersen if (sdev->skip_vpd_pages) 10177562523eSMartin K. Petersen goto fail; 10187562523eSMartin K. Petersen 1019881a256dSMatthew Wilcox /* Ask for all the pages supported by this device */ 1020e3deec09SJames Bottomley result = scsi_vpd_inquiry(sdev, buf, 0, buf_len); 1021881a256dSMatthew Wilcox if (result) 1022881a256dSMatthew Wilcox goto fail; 1023881a256dSMatthew Wilcox 1024881a256dSMatthew Wilcox /* If the user actually wanted this page, we can skip the rest */ 1025881a256dSMatthew Wilcox if (page == 0) 102616d3ea26SMartin K. Petersen return 0; 1027881a256dSMatthew Wilcox 1028e3deec09SJames Bottomley for (i = 0; i < min((int)buf[3], buf_len - 4); i++) 1029881a256dSMatthew Wilcox if (buf[i + 4] == page) 1030881a256dSMatthew Wilcox goto found; 1031e3deec09SJames Bottomley 103216d3ea26SMartin K. Petersen if (i < buf[3] && i >= buf_len - 4) 1033e3deec09SJames Bottomley /* ran off the end of the buffer, give us benefit of doubt */ 1034e3deec09SJames Bottomley goto found; 1035881a256dSMatthew Wilcox /* The device claims it doesn't support the requested page */ 1036881a256dSMatthew Wilcox goto fail; 1037881a256dSMatthew Wilcox 1038881a256dSMatthew Wilcox found: 1039e3deec09SJames Bottomley result = scsi_vpd_inquiry(sdev, buf, page, buf_len); 1040881a256dSMatthew Wilcox if (result) 1041881a256dSMatthew Wilcox goto fail; 1042881a256dSMatthew Wilcox 1043e3deec09SJames Bottomley return 0; 1044881a256dSMatthew Wilcox 1045881a256dSMatthew Wilcox fail: 1046e3deec09SJames Bottomley return -EINVAL; 1047881a256dSMatthew Wilcox } 1048881a256dSMatthew Wilcox EXPORT_SYMBOL_GPL(scsi_get_vpd_page); 1049881a256dSMatthew Wilcox 1050881a256dSMatthew Wilcox /** 10513c6bdaeaSMartin K. Petersen * scsi_report_opcode - Find out if a given command opcode is supported 10523c6bdaeaSMartin K. Petersen * @sdev: scsi device to query 10533c6bdaeaSMartin K. Petersen * @buffer: scratch buffer (must be at least 20 bytes long) 10543c6bdaeaSMartin K. Petersen * @len: length of buffer 10553c6bdaeaSMartin K. Petersen * @opcode: opcode for command to look up 10563c6bdaeaSMartin K. Petersen * 10573c6bdaeaSMartin K. Petersen * Uses the REPORT SUPPORTED OPERATION CODES to look up the given 105866c28f97SMartin K. Petersen * opcode. Returns -EINVAL if RSOC fails, 0 if the command opcode is 105966c28f97SMartin K. Petersen * unsupported and 1 if the device claims to support the command. 10603c6bdaeaSMartin K. Petersen */ 10613c6bdaeaSMartin K. Petersen int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer, 10623c6bdaeaSMartin K. Petersen unsigned int len, unsigned char opcode) 10633c6bdaeaSMartin K. Petersen { 10643c6bdaeaSMartin K. Petersen unsigned char cmd[16]; 10653c6bdaeaSMartin K. Petersen struct scsi_sense_hdr sshdr; 10663c6bdaeaSMartin K. Petersen int result; 10673c6bdaeaSMartin K. Petersen 10683c6bdaeaSMartin K. Petersen if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3) 106966c28f97SMartin K. Petersen return -EINVAL; 10703c6bdaeaSMartin K. Petersen 10713c6bdaeaSMartin K. Petersen memset(cmd, 0, 16); 10723c6bdaeaSMartin K. Petersen cmd[0] = MAINTENANCE_IN; 10733c6bdaeaSMartin K. Petersen cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES; 10743c6bdaeaSMartin K. Petersen cmd[2] = 1; /* One command format */ 10753c6bdaeaSMartin K. Petersen cmd[3] = opcode; 10763c6bdaeaSMartin K. Petersen put_unaligned_be32(len, &cmd[6]); 10773c6bdaeaSMartin K. Petersen memset(buffer, 0, len); 10783c6bdaeaSMartin K. Petersen 10793c6bdaeaSMartin K. Petersen result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len, 10803c6bdaeaSMartin K. Petersen &sshdr, 30 * HZ, 3, NULL); 10813c6bdaeaSMartin K. Petersen 10823c6bdaeaSMartin K. Petersen if (result && scsi_sense_valid(&sshdr) && 10833c6bdaeaSMartin K. Petersen sshdr.sense_key == ILLEGAL_REQUEST && 10843c6bdaeaSMartin K. Petersen (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00) 108566c28f97SMartin K. Petersen return -EINVAL; 10863c6bdaeaSMartin K. Petersen 10873c6bdaeaSMartin K. Petersen if ((buffer[1] & 3) == 3) /* Command supported */ 10883c6bdaeaSMartin K. Petersen return 1; 10893c6bdaeaSMartin K. Petersen 10903c6bdaeaSMartin K. Petersen return 0; 10913c6bdaeaSMartin K. Petersen } 10923c6bdaeaSMartin K. Petersen EXPORT_SYMBOL(scsi_report_opcode); 10933c6bdaeaSMartin K. Petersen 10943c6bdaeaSMartin K. Petersen /** 1095eb44820cSRob Landley * scsi_device_get - get an additional reference to a scsi_device 10961da177e4SLinus Torvalds * @sdev: device to get a reference to 10971da177e4SLinus Torvalds * 1098eb44820cSRob Landley * Description: Gets a reference to the scsi_device and increments the use count 10991da177e4SLinus Torvalds * of the underlying LLDD module. You must hold host_lock of the 11001da177e4SLinus Torvalds * parent Scsi_Host or already have a reference when calling this. 11011da177e4SLinus Torvalds */ 11021da177e4SLinus Torvalds int scsi_device_get(struct scsi_device *sdev) 11031da177e4SLinus Torvalds { 110485b6c720SJames Bottomley if (sdev->sdev_state == SDEV_DEL) 11051da177e4SLinus Torvalds return -ENXIO; 11061da177e4SLinus Torvalds if (!get_device(&sdev->sdev_gendev)) 11071da177e4SLinus Torvalds return -ENXIO; 110885b6c720SJames Bottomley /* We can fail this if we're doing SCSI operations 110985b6c720SJames Bottomley * from module exit (like cache flush) */ 111085b6c720SJames Bottomley try_module_get(sdev->host->hostt->module); 111185b6c720SJames Bottomley 11121da177e4SLinus Torvalds return 0; 11131da177e4SLinus Torvalds } 11141da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_get); 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds /** 11171da177e4SLinus Torvalds * scsi_device_put - release a reference to a scsi_device 11181da177e4SLinus Torvalds * @sdev: device to release a reference on. 11191da177e4SLinus Torvalds * 1120eb44820cSRob Landley * Description: Release a reference to the scsi_device and decrements the use 1121eb44820cSRob Landley * count of the underlying LLDD module. The device is freed once the last 11221da177e4SLinus Torvalds * user vanishes. 11231da177e4SLinus Torvalds */ 11241da177e4SLinus Torvalds void scsi_device_put(struct scsi_device *sdev) 11251da177e4SLinus Torvalds { 1126504fb37aSIngo Molnar #ifdef CONFIG_MODULE_UNLOAD 1127f479ab87SJames Bottomley struct module *module = sdev->host->hostt->module; 1128f479ab87SJames Bottomley 112985b6c720SJames Bottomley /* The module refcount will be zero if scsi_device_get() 113085b6c720SJames Bottomley * was called from a module removal routine */ 1131f479ab87SJames Bottomley if (module && module_refcount(module) != 0) 1132f479ab87SJames Bottomley module_put(module); 1133a506b44bSDaniel Walker #endif 11341da177e4SLinus Torvalds put_device(&sdev->sdev_gendev); 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_put); 11371da177e4SLinus Torvalds 1138eb44820cSRob Landley /* helper for shost_for_each_device, see that for documentation */ 11391da177e4SLinus Torvalds struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost, 11401da177e4SLinus Torvalds struct scsi_device *prev) 11411da177e4SLinus Torvalds { 11421da177e4SLinus Torvalds struct list_head *list = (prev ? &prev->siblings : &shost->__devices); 11431da177e4SLinus Torvalds struct scsi_device *next = NULL; 11441da177e4SLinus Torvalds unsigned long flags; 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 11471da177e4SLinus Torvalds while (list->next != &shost->__devices) { 11481da177e4SLinus Torvalds next = list_entry(list->next, struct scsi_device, siblings); 11491da177e4SLinus Torvalds /* skip devices that we can't get a reference to */ 11501da177e4SLinus Torvalds if (!scsi_device_get(next)) 11511da177e4SLinus Torvalds break; 11521da177e4SLinus Torvalds next = NULL; 11531da177e4SLinus Torvalds list = list->next; 11541da177e4SLinus Torvalds } 11551da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds if (prev) 11581da177e4SLinus Torvalds scsi_device_put(prev); 11591da177e4SLinus Torvalds return next; 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_iterate_devices); 11621da177e4SLinus Torvalds 11631da177e4SLinus Torvalds /** 11641da177e4SLinus Torvalds * starget_for_each_device - helper to walk all devices of a target 11651da177e4SLinus Torvalds * @starget: target whose devices we want to iterate over. 1166eb44820cSRob Landley * @data: Opaque passed to each function call. 1167eb44820cSRob Landley * @fn: Function to call on each device 11681da177e4SLinus Torvalds * 1169522939d4SMaciej W. Rozycki * This traverses over each device of @starget. The devices have 11701da177e4SLinus Torvalds * a reference that must be released by scsi_host_put when breaking 11711da177e4SLinus Torvalds * out of the loop. 11721da177e4SLinus Torvalds */ 11731da177e4SLinus Torvalds void starget_for_each_device(struct scsi_target *starget, void *data, 11741da177e4SLinus Torvalds void (*fn)(struct scsi_device *, void *)) 11751da177e4SLinus Torvalds { 11761da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 11771da177e4SLinus Torvalds struct scsi_device *sdev; 11781da177e4SLinus Torvalds 11791da177e4SLinus Torvalds shost_for_each_device(sdev, shost) { 11801da177e4SLinus Torvalds if ((sdev->channel == starget->channel) && 11811da177e4SLinus Torvalds (sdev->id == starget->id)) 11821da177e4SLinus Torvalds fn(sdev, data); 11831da177e4SLinus Torvalds } 11841da177e4SLinus Torvalds } 11851da177e4SLinus Torvalds EXPORT_SYMBOL(starget_for_each_device); 11861da177e4SLinus Torvalds 11871da177e4SLinus Torvalds /** 118814f501a4SRandy Dunlap * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED) 1189522939d4SMaciej W. Rozycki * @starget: target whose devices we want to iterate over. 119014f501a4SRandy Dunlap * @data: parameter for callback @fn() 119114f501a4SRandy Dunlap * @fn: callback function that is invoked for each device 1192522939d4SMaciej W. Rozycki * 1193522939d4SMaciej W. Rozycki * This traverses over each device of @starget. It does _not_ 1194522939d4SMaciej W. Rozycki * take a reference on the scsi_device, so the whole loop must be 1195522939d4SMaciej W. Rozycki * protected by shost->host_lock. 1196522939d4SMaciej W. Rozycki * 1197522939d4SMaciej W. Rozycki * Note: The only reason why drivers would want to use this is because 1198522939d4SMaciej W. Rozycki * they need to access the device list in irq context. Otherwise you 1199522939d4SMaciej W. Rozycki * really want to use starget_for_each_device instead. 1200522939d4SMaciej W. Rozycki **/ 1201522939d4SMaciej W. Rozycki void __starget_for_each_device(struct scsi_target *starget, void *data, 1202522939d4SMaciej W. Rozycki void (*fn)(struct scsi_device *, void *)) 1203522939d4SMaciej W. Rozycki { 1204522939d4SMaciej W. Rozycki struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 1205522939d4SMaciej W. Rozycki struct scsi_device *sdev; 1206522939d4SMaciej W. Rozycki 1207522939d4SMaciej W. Rozycki __shost_for_each_device(sdev, shost) { 1208522939d4SMaciej W. Rozycki if ((sdev->channel == starget->channel) && 1209522939d4SMaciej W. Rozycki (sdev->id == starget->id)) 1210522939d4SMaciej W. Rozycki fn(sdev, data); 1211522939d4SMaciej W. Rozycki } 1212522939d4SMaciej W. Rozycki } 1213522939d4SMaciej W. Rozycki EXPORT_SYMBOL(__starget_for_each_device); 1214522939d4SMaciej W. Rozycki 1215522939d4SMaciej W. Rozycki /** 12161da177e4SLinus Torvalds * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED) 12171da177e4SLinus Torvalds * @starget: SCSI target pointer 12181da177e4SLinus Torvalds * @lun: SCSI Logical Unit Number 12191da177e4SLinus Torvalds * 1220eb44820cSRob Landley * Description: Looks up the scsi_device with the specified @lun for a given 12211da177e4SLinus Torvalds * @starget. The returned scsi_device does not have an additional 12221da177e4SLinus Torvalds * reference. You must hold the host's host_lock over this call and 122332aeef60SHannes Reinecke * any access to the returned scsi_device. A scsi_device in state 122432aeef60SHannes Reinecke * SDEV_DEL is skipped. 12251da177e4SLinus Torvalds * 1226dc8875e1SRandy Dunlap * Note: The only reason why drivers should use this is because 1227eb44820cSRob Landley * they need to access the device list in irq context. Otherwise you 12281da177e4SLinus Torvalds * really want to use scsi_device_lookup_by_target instead. 12291da177e4SLinus Torvalds **/ 12301da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget, 12311da177e4SLinus Torvalds uint lun) 12321da177e4SLinus Torvalds { 12331da177e4SLinus Torvalds struct scsi_device *sdev; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds list_for_each_entry(sdev, &starget->devices, same_target_siblings) { 123632aeef60SHannes Reinecke if (sdev->sdev_state == SDEV_DEL) 123732aeef60SHannes Reinecke continue; 12381da177e4SLinus Torvalds if (sdev->lun ==lun) 12391da177e4SLinus Torvalds return sdev; 12401da177e4SLinus Torvalds } 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds return NULL; 12431da177e4SLinus Torvalds } 12441da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup_by_target); 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds /** 12471da177e4SLinus Torvalds * scsi_device_lookup_by_target - find a device given the target 12481da177e4SLinus Torvalds * @starget: SCSI target pointer 12491da177e4SLinus Torvalds * @lun: SCSI Logical Unit Number 12501da177e4SLinus Torvalds * 1251477e608cSBartlomiej Zolnierkiewicz * Description: Looks up the scsi_device with the specified @lun for a given 1252477e608cSBartlomiej Zolnierkiewicz * @starget. The returned scsi_device has an additional reference that 1253eb44820cSRob Landley * needs to be released with scsi_device_put once you're done with it. 12541da177e4SLinus Torvalds **/ 12551da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget, 12561da177e4SLinus Torvalds uint lun) 12571da177e4SLinus Torvalds { 12581da177e4SLinus Torvalds struct scsi_device *sdev; 12591da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 12601da177e4SLinus Torvalds unsigned long flags; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 12631da177e4SLinus Torvalds sdev = __scsi_device_lookup_by_target(starget, lun); 12641da177e4SLinus Torvalds if (sdev && scsi_device_get(sdev)) 12651da177e4SLinus Torvalds sdev = NULL; 12661da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds return sdev; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup_by_target); 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds /** 1273eb44820cSRob Landley * __scsi_device_lookup - find a device given the host (UNLOCKED) 12741da177e4SLinus Torvalds * @shost: SCSI host pointer 12751da177e4SLinus Torvalds * @channel: SCSI channel (zero if only one channel) 1276eb44820cSRob Landley * @id: SCSI target number (physical unit number) 12771da177e4SLinus Torvalds * @lun: SCSI Logical Unit Number 12781da177e4SLinus Torvalds * 1279eb44820cSRob Landley * Description: Looks up the scsi_device with the specified @channel, @id, @lun 1280eb44820cSRob Landley * for a given host. The returned scsi_device does not have an additional 1281eb44820cSRob Landley * reference. You must hold the host's host_lock over this call and any access 1282eb44820cSRob Landley * to the returned scsi_device. 12831da177e4SLinus Torvalds * 12841da177e4SLinus Torvalds * Note: The only reason why drivers would want to use this is because 1285eb44820cSRob Landley * they need to access the device list in irq context. Otherwise you 12861da177e4SLinus Torvalds * really want to use scsi_device_lookup instead. 12871da177e4SLinus Torvalds **/ 12881da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost, 12891da177e4SLinus Torvalds uint channel, uint id, uint lun) 12901da177e4SLinus Torvalds { 12911da177e4SLinus Torvalds struct scsi_device *sdev; 12921da177e4SLinus Torvalds 12931da177e4SLinus Torvalds list_for_each_entry(sdev, &shost->__devices, siblings) { 12941da177e4SLinus Torvalds if (sdev->channel == channel && sdev->id == id && 12951da177e4SLinus Torvalds sdev->lun ==lun) 12961da177e4SLinus Torvalds return sdev; 12971da177e4SLinus Torvalds } 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds return NULL; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup); 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds /** 13041da177e4SLinus Torvalds * scsi_device_lookup - find a device given the host 13051da177e4SLinus Torvalds * @shost: SCSI host pointer 13061da177e4SLinus Torvalds * @channel: SCSI channel (zero if only one channel) 13071da177e4SLinus Torvalds * @id: SCSI target number (physical unit number) 13081da177e4SLinus Torvalds * @lun: SCSI Logical Unit Number 13091da177e4SLinus Torvalds * 1310eb44820cSRob Landley * Description: Looks up the scsi_device with the specified @channel, @id, @lun 1311eb44820cSRob Landley * for a given host. The returned scsi_device has an additional reference that 1312eb44820cSRob Landley * needs to be released with scsi_device_put once you're done with it. 13131da177e4SLinus Torvalds **/ 13141da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost, 13151da177e4SLinus Torvalds uint channel, uint id, uint lun) 13161da177e4SLinus Torvalds { 13171da177e4SLinus Torvalds struct scsi_device *sdev; 13181da177e4SLinus Torvalds unsigned long flags; 13191da177e4SLinus Torvalds 13201da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 13211da177e4SLinus Torvalds sdev = __scsi_device_lookup(shost, channel, id, lun); 13221da177e4SLinus Torvalds if (sdev && scsi_device_get(sdev)) 13231da177e4SLinus Torvalds sdev = NULL; 13241da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds return sdev; 13271da177e4SLinus Torvalds } 13281da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup); 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI core"); 13311da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 13321da177e4SLinus Torvalds 13331da177e4SLinus Torvalds module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR); 13341da177e4SLinus Torvalds MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels"); 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds static int __init init_scsi(void) 13371da177e4SLinus Torvalds { 13389bf09c23SJens Axboe int error; 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds error = scsi_init_queue(); 13411da177e4SLinus Torvalds if (error) 13421da177e4SLinus Torvalds return error; 13431da177e4SLinus Torvalds error = scsi_init_procfs(); 13441da177e4SLinus Torvalds if (error) 13451da177e4SLinus Torvalds goto cleanup_queue; 13461da177e4SLinus Torvalds error = scsi_init_devinfo(); 13471da177e4SLinus Torvalds if (error) 13481da177e4SLinus Torvalds goto cleanup_procfs; 13491da177e4SLinus Torvalds error = scsi_init_hosts(); 13501da177e4SLinus Torvalds if (error) 13511da177e4SLinus Torvalds goto cleanup_devlist; 13521da177e4SLinus Torvalds error = scsi_init_sysctl(); 13531da177e4SLinus Torvalds if (error) 13541da177e4SLinus Torvalds goto cleanup_hosts; 13551da177e4SLinus Torvalds error = scsi_sysfs_register(); 13561da177e4SLinus Torvalds if (error) 13571da177e4SLinus Torvalds goto cleanup_sysctl; 13581da177e4SLinus Torvalds 135984314fd4SJames Smart scsi_netlink_init(); 136084314fd4SJames Smart 13611da177e4SLinus Torvalds printk(KERN_NOTICE "SCSI subsystem initialized\n"); 13621da177e4SLinus Torvalds return 0; 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds cleanup_sysctl: 13651da177e4SLinus Torvalds scsi_exit_sysctl(); 13661da177e4SLinus Torvalds cleanup_hosts: 13671da177e4SLinus Torvalds scsi_exit_hosts(); 13681da177e4SLinus Torvalds cleanup_devlist: 13691da177e4SLinus Torvalds scsi_exit_devinfo(); 13701da177e4SLinus Torvalds cleanup_procfs: 13711da177e4SLinus Torvalds scsi_exit_procfs(); 13721da177e4SLinus Torvalds cleanup_queue: 13731da177e4SLinus Torvalds scsi_exit_queue(); 13741da177e4SLinus Torvalds printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n", 13751da177e4SLinus Torvalds -error); 13761da177e4SLinus Torvalds return error; 13771da177e4SLinus Torvalds } 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds static void __exit exit_scsi(void) 13801da177e4SLinus Torvalds { 138184314fd4SJames Smart scsi_netlink_exit(); 13821da177e4SLinus Torvalds scsi_sysfs_unregister(); 13831da177e4SLinus Torvalds scsi_exit_sysctl(); 13841da177e4SLinus Torvalds scsi_exit_hosts(); 13851da177e4SLinus Torvalds scsi_exit_devinfo(); 13861da177e4SLinus Torvalds scsi_exit_procfs(); 13871da177e4SLinus Torvalds scsi_exit_queue(); 1388a4683487SDan Williams async_unregister_domain(&scsi_sd_probe_domain); 13891da177e4SLinus Torvalds } 13901da177e4SLinus Torvalds 13911da177e4SLinus Torvalds subsys_initcall(init_scsi); 13921da177e4SLinus Torvalds module_exit(exit_scsi); 1393