xref: /openbmc/linux/drivers/scsi/scsi.c (revision f0c0a376)
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>
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds #include <scsi/scsi.h>
591da177e4SLinus Torvalds #include <scsi/scsi_cmnd.h>
601da177e4SLinus Torvalds #include <scsi/scsi_dbg.h>
611da177e4SLinus Torvalds #include <scsi/scsi_device.h>
627b3d9545SLinus Torvalds #include <scsi/scsi_driver.h>
631da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
641da177e4SLinus Torvalds #include <scsi/scsi_host.h>
651da177e4SLinus Torvalds #include <scsi/scsi_tcq.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include "scsi_priv.h"
681da177e4SLinus Torvalds #include "scsi_logging.h"
691da177e4SLinus Torvalds 
7052c1da39SAdrian Bunk static void scsi_done(struct scsi_cmnd *cmd);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds  * Definitions and constants.
741da177e4SLinus Torvalds  */
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #define MIN_RESET_DELAY (2*HZ)
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds /* Do not call reset on error if we just did a reset within 15 sec. */
791da177e4SLinus Torvalds #define MIN_RESET_PERIOD (15*HZ)
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 
908a1cdc9cSMatthew Wilcox /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
918a1cdc9cSMatthew Wilcox  * You may not alter any existing entry (although adding new ones is
928a1cdc9cSMatthew Wilcox  * encouraged once assigned by ANSI/INCITS T10
938a1cdc9cSMatthew Wilcox  */
944ff36718SMatthew Wilcox static const char *const scsi_device_types[] = {
958a1cdc9cSMatthew Wilcox 	"Direct-Access    ",
968a1cdc9cSMatthew Wilcox 	"Sequential-Access",
971da177e4SLinus Torvalds 	"Printer          ",
981da177e4SLinus Torvalds 	"Processor        ",
991da177e4SLinus Torvalds 	"WORM             ",
1008a1cdc9cSMatthew Wilcox 	"CD-ROM           ",
1011da177e4SLinus Torvalds 	"Scanner          ",
1028a1cdc9cSMatthew Wilcox 	"Optical Device   ",
1038a1cdc9cSMatthew Wilcox 	"Medium Changer   ",
1041da177e4SLinus Torvalds 	"Communications   ",
1054ff36718SMatthew Wilcox 	"ASC IT8          ",
1064ff36718SMatthew Wilcox 	"ASC IT8          ",
1071da177e4SLinus Torvalds 	"RAID             ",
1081da177e4SLinus Torvalds 	"Enclosure        ",
1098a1cdc9cSMatthew Wilcox 	"Direct-Access-RBC",
1104ff36718SMatthew Wilcox 	"Optical card     ",
1114ff36718SMatthew Wilcox 	"Bridge controller",
1124ff36718SMatthew Wilcox 	"Object storage   ",
1134ff36718SMatthew Wilcox 	"Automation/Drive ",
1141da177e4SLinus Torvalds };
1154ff36718SMatthew Wilcox 
116eb44820cSRob Landley /**
117eb44820cSRob Landley  * scsi_device_type - Return 17 char string indicating device type.
118eb44820cSRob Landley  * @type: type number to look up
119eb44820cSRob Landley  */
120eb44820cSRob Landley 
1214ff36718SMatthew Wilcox const char * scsi_device_type(unsigned type)
1224ff36718SMatthew Wilcox {
1234ff36718SMatthew Wilcox 	if (type == 0x1e)
1244ff36718SMatthew Wilcox 		return "Well-known LUN   ";
1254ff36718SMatthew Wilcox 	if (type == 0x1f)
1264ff36718SMatthew Wilcox 		return "No Device        ";
12780c6e3c0SEric Sesterhenn 	if (type >= ARRAY_SIZE(scsi_device_types))
1284ff36718SMatthew Wilcox 		return "Unknown          ";
1294ff36718SMatthew Wilcox 	return scsi_device_types[type];
1304ff36718SMatthew Wilcox }
1314ff36718SMatthew Wilcox 
1324ff36718SMatthew Wilcox EXPORT_SYMBOL(scsi_device_type);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds struct scsi_host_cmd_pool {
1355b7f1680SJames Bottomley 	struct kmem_cache	*cmd_slab;
1365b7f1680SJames Bottomley 	struct kmem_cache	*sense_slab;
1371da177e4SLinus Torvalds 	unsigned int		users;
1385b7f1680SJames Bottomley 	char			*cmd_name;
1395b7f1680SJames Bottomley 	char			*sense_name;
1401da177e4SLinus Torvalds 	unsigned int		slab_flags;
141c53033f6SAl Viro 	gfp_t			gfp_mask;
1421da177e4SLinus Torvalds };
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_pool = {
1455b7f1680SJames Bottomley 	.cmd_name	= "scsi_cmd_cache",
1465b7f1680SJames Bottomley 	.sense_name	= "scsi_sense_cache",
1471da177e4SLinus Torvalds 	.slab_flags	= SLAB_HWCACHE_ALIGN,
1481da177e4SLinus Torvalds };
1491da177e4SLinus Torvalds 
1501da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
1515b7f1680SJames Bottomley 	.cmd_name	= "scsi_cmd_cache(DMA)",
1525b7f1680SJames Bottomley 	.sense_name	= "scsi_sense_cache(DMA)",
1531da177e4SLinus Torvalds 	.slab_flags	= SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
1541da177e4SLinus Torvalds 	.gfp_mask	= __GFP_DMA,
1551da177e4SLinus Torvalds };
1561da177e4SLinus Torvalds 
1570b950672SArjan van de Ven static DEFINE_MUTEX(host_cmd_pool_mutex);
1581da177e4SLinus Torvalds 
159eb44820cSRob Landley /**
160e507e30bSJames Bottomley  * scsi_pool_alloc_command - internal function to get a fully allocated command
161e507e30bSJames Bottomley  * @pool:	slab pool to allocate the command from
162e507e30bSJames Bottomley  * @gfp_mask:	mask for the allocation
163e507e30bSJames Bottomley  *
164e507e30bSJames Bottomley  * Returns a fully allocated command (with the allied sense buffer) or
165e507e30bSJames Bottomley  * NULL on failure
166e507e30bSJames Bottomley  */
167e507e30bSJames Bottomley static struct scsi_cmnd *
168e507e30bSJames Bottomley scsi_pool_alloc_command(struct scsi_host_cmd_pool *pool, gfp_t gfp_mask)
169e507e30bSJames Bottomley {
170e507e30bSJames Bottomley 	struct scsi_cmnd *cmd;
171e507e30bSJames Bottomley 
172e507e30bSJames Bottomley 	cmd = kmem_cache_alloc(pool->cmd_slab, gfp_mask | pool->gfp_mask);
173e507e30bSJames Bottomley 	if (!cmd)
174e507e30bSJames Bottomley 		return NULL;
175e507e30bSJames Bottomley 
176e507e30bSJames Bottomley 	memset(cmd, 0, sizeof(*cmd));
177e507e30bSJames Bottomley 
178e507e30bSJames Bottomley 	cmd->sense_buffer = kmem_cache_alloc(pool->sense_slab,
179e507e30bSJames Bottomley 					     gfp_mask | pool->gfp_mask);
180e507e30bSJames Bottomley 	if (!cmd->sense_buffer) {
181e507e30bSJames Bottomley 		kmem_cache_free(pool->cmd_slab, cmd);
182e507e30bSJames Bottomley 		return NULL;
183e507e30bSJames Bottomley 	}
184e507e30bSJames Bottomley 
185e507e30bSJames Bottomley 	return cmd;
186e507e30bSJames Bottomley }
187e507e30bSJames Bottomley 
188e507e30bSJames Bottomley /**
189e507e30bSJames Bottomley  * scsi_pool_free_command - internal function to release a command
190e507e30bSJames Bottomley  * @pool:	slab pool to allocate the command from
191e507e30bSJames Bottomley  * @cmd:	command to release
192e507e30bSJames Bottomley  *
193e507e30bSJames Bottomley  * the command must previously have been allocated by
194e507e30bSJames Bottomley  * scsi_pool_alloc_command.
195e507e30bSJames Bottomley  */
196e507e30bSJames Bottomley static void
197e507e30bSJames Bottomley scsi_pool_free_command(struct scsi_host_cmd_pool *pool,
198e507e30bSJames Bottomley 			 struct scsi_cmnd *cmd)
199e507e30bSJames Bottomley {
2007027ad72SMartin K. Petersen 	if (cmd->prot_sdb)
2017027ad72SMartin K. Petersen 		kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
2027027ad72SMartin K. Petersen 
203e507e30bSJames Bottomley 	kmem_cache_free(pool->sense_slab, cmd->sense_buffer);
204e507e30bSJames Bottomley 	kmem_cache_free(pool->cmd_slab, cmd);
205e507e30bSJames Bottomley }
206e507e30bSJames Bottomley 
207e507e30bSJames Bottomley /**
2087027ad72SMartin K. Petersen  * scsi_host_alloc_command - internal function to allocate command
2097027ad72SMartin K. Petersen  * @shost:	SCSI host whose pool to allocate from
2107027ad72SMartin K. Petersen  * @gfp_mask:	mask for the allocation
2117027ad72SMartin K. Petersen  *
2127027ad72SMartin K. Petersen  * Returns a fully allocated command with sense buffer and protection
2137027ad72SMartin K. Petersen  * data buffer (where applicable) or NULL on failure
2147027ad72SMartin K. Petersen  */
2157027ad72SMartin K. Petersen static struct scsi_cmnd *
2167027ad72SMartin K. Petersen scsi_host_alloc_command(struct Scsi_Host *shost, gfp_t gfp_mask)
2177027ad72SMartin K. Petersen {
2187027ad72SMartin K. Petersen 	struct scsi_cmnd *cmd;
2197027ad72SMartin K. Petersen 
2207027ad72SMartin K. Petersen 	cmd = scsi_pool_alloc_command(shost->cmd_pool, gfp_mask);
2217027ad72SMartin K. Petersen 	if (!cmd)
2227027ad72SMartin K. Petersen 		return NULL;
2237027ad72SMartin K. Petersen 
2247027ad72SMartin K. Petersen 	if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
2257027ad72SMartin K. Petersen 		cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp_mask);
2267027ad72SMartin K. Petersen 
2277027ad72SMartin K. Petersen 		if (!cmd->prot_sdb) {
2287027ad72SMartin K. Petersen 			scsi_pool_free_command(shost->cmd_pool, cmd);
2297027ad72SMartin K. Petersen 			return NULL;
2307027ad72SMartin K. Petersen 		}
2317027ad72SMartin K. Petersen 	}
2327027ad72SMartin K. Petersen 
2337027ad72SMartin K. Petersen 	return cmd;
2347027ad72SMartin K. Petersen }
2357027ad72SMartin K. Petersen 
2367027ad72SMartin K. Petersen /**
237eb44820cSRob Landley  * __scsi_get_command - Allocate a struct scsi_cmnd
238eb44820cSRob Landley  * @shost: host to transmit command
239eb44820cSRob Landley  * @gfp_mask: allocation mask
240eb44820cSRob Landley  *
241eb44820cSRob Landley  * Description: allocate a struct scsi_cmd from host's slab, recycling from the
242eb44820cSRob Landley  *              host's free_list if necessary.
243eb44820cSRob Landley  */
244b58d9154SFUJITA Tomonori struct scsi_cmnd *__scsi_get_command(struct Scsi_Host *shost, gfp_t gfp_mask)
2451da177e4SLinus Torvalds {
2461da177e4SLinus Torvalds 	struct scsi_cmnd *cmd;
247de25deb1SFUJITA Tomonori 	unsigned char *buf;
2481da177e4SLinus Torvalds 
2497027ad72SMartin K. Petersen 	cmd = scsi_host_alloc_command(shost, gfp_mask);
250164fc5dcSHugh Dickins 
2511da177e4SLinus Torvalds 	if (unlikely(!cmd)) {
2521da177e4SLinus Torvalds 		unsigned long flags;
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 		spin_lock_irqsave(&shost->free_list_lock, flags);
2551da177e4SLinus Torvalds 		if (likely(!list_empty(&shost->free_list))) {
2561da177e4SLinus Torvalds 			cmd = list_entry(shost->free_list.next,
2571da177e4SLinus Torvalds 					 struct scsi_cmnd, list);
2581da177e4SLinus Torvalds 			list_del_init(&cmd->list);
2591da177e4SLinus Torvalds 		}
2601da177e4SLinus Torvalds 		spin_unlock_irqrestore(&shost->free_list_lock, flags);
261de25deb1SFUJITA Tomonori 
262de25deb1SFUJITA Tomonori 		if (cmd) {
263de25deb1SFUJITA Tomonori 			buf = cmd->sense_buffer;
264de25deb1SFUJITA Tomonori 			memset(cmd, 0, sizeof(*cmd));
265de25deb1SFUJITA Tomonori 			cmd->sense_buffer = buf;
266de25deb1SFUJITA Tomonori 		}
2671da177e4SLinus Torvalds 	}
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	return cmd;
2701da177e4SLinus Torvalds }
271b58d9154SFUJITA Tomonori EXPORT_SYMBOL_GPL(__scsi_get_command);
2721da177e4SLinus Torvalds 
273eb44820cSRob Landley /**
274eb44820cSRob Landley  * scsi_get_command - Allocate and setup a scsi command block
275eb44820cSRob Landley  * @dev: parent scsi device
276eb44820cSRob Landley  * @gfp_mask: allocator flags
2771da177e4SLinus Torvalds  *
2781da177e4SLinus Torvalds  * Returns:	The allocated scsi command structure.
2791da177e4SLinus Torvalds  */
280c53033f6SAl Viro struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	struct scsi_cmnd *cmd;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	/* Bail if we can't get a reference to the device */
2851da177e4SLinus Torvalds 	if (!get_device(&dev->sdev_gendev))
2861da177e4SLinus Torvalds 		return NULL;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	cmd = __scsi_get_command(dev->host, gfp_mask);
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	if (likely(cmd != NULL)) {
2911da177e4SLinus Torvalds 		unsigned long flags;
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 		cmd->device = dev;
2941da177e4SLinus Torvalds 		INIT_LIST_HEAD(&cmd->list);
2951da177e4SLinus Torvalds 		spin_lock_irqsave(&dev->list_lock, flags);
2961da177e4SLinus Torvalds 		list_add_tail(&cmd->list, &dev->cmd_list);
2971da177e4SLinus Torvalds 		spin_unlock_irqrestore(&dev->list_lock, flags);
29879e448bfSMatthew Dobson 		cmd->jiffies_at_alloc = jiffies;
2991da177e4SLinus Torvalds 	} else
3001da177e4SLinus Torvalds 		put_device(&dev->sdev_gendev);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	return cmd;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_get_command);
3051da177e4SLinus Torvalds 
306eb44820cSRob Landley /**
307eb44820cSRob Landley  * __scsi_put_command - Free a struct scsi_cmnd
308eb44820cSRob Landley  * @shost: dev->host
309eb44820cSRob Landley  * @cmd: Command to free
310eb44820cSRob Landley  * @dev: parent scsi device
311eb44820cSRob Landley  */
312b58d9154SFUJITA Tomonori void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd,
313b58d9154SFUJITA Tomonori 			struct device *dev)
314b58d9154SFUJITA Tomonori {
315b58d9154SFUJITA Tomonori 	unsigned long flags;
316b58d9154SFUJITA Tomonori 
317b58d9154SFUJITA Tomonori 	/* changing locks here, don't need to restore the irq state */
318b58d9154SFUJITA Tomonori 	spin_lock_irqsave(&shost->free_list_lock, flags);
319b58d9154SFUJITA Tomonori 	if (unlikely(list_empty(&shost->free_list))) {
320b58d9154SFUJITA Tomonori 		list_add(&cmd->list, &shost->free_list);
321b58d9154SFUJITA Tomonori 		cmd = NULL;
322b58d9154SFUJITA Tomonori 	}
323b58d9154SFUJITA Tomonori 	spin_unlock_irqrestore(&shost->free_list_lock, flags);
324b58d9154SFUJITA Tomonori 
325e507e30bSJames Bottomley 	if (likely(cmd != NULL))
326e507e30bSJames Bottomley 		scsi_pool_free_command(shost->cmd_pool, cmd);
327b58d9154SFUJITA Tomonori 
328b58d9154SFUJITA Tomonori 	put_device(dev);
329b58d9154SFUJITA Tomonori }
330b58d9154SFUJITA Tomonori EXPORT_SYMBOL(__scsi_put_command);
331b58d9154SFUJITA Tomonori 
332eb44820cSRob Landley /**
333eb44820cSRob Landley  * scsi_put_command - Free a scsi command block
334eb44820cSRob Landley  * @cmd: command block to free
3351da177e4SLinus Torvalds  *
3361da177e4SLinus Torvalds  * Returns:	Nothing.
3371da177e4SLinus Torvalds  *
3381da177e4SLinus Torvalds  * Notes:	The command must not belong to any lists.
3391da177e4SLinus Torvalds  */
3401da177e4SLinus Torvalds void scsi_put_command(struct scsi_cmnd *cmd)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	struct scsi_device *sdev = cmd->device;
3431da177e4SLinus Torvalds 	unsigned long flags;
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	/* serious error if the command hasn't come from a device list */
3461da177e4SLinus Torvalds 	spin_lock_irqsave(&cmd->device->list_lock, flags);
3471da177e4SLinus Torvalds 	BUG_ON(list_empty(&cmd->list));
3481da177e4SLinus Torvalds 	list_del_init(&cmd->list);
349b58d9154SFUJITA Tomonori 	spin_unlock_irqrestore(&cmd->device->list_lock, flags);
3501da177e4SLinus Torvalds 
351b58d9154SFUJITA Tomonori 	__scsi_put_command(cmd->device->host, cmd, &sdev->sdev_gendev);
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_put_command);
3541da177e4SLinus Torvalds 
3551c353f7dSJames Bottomley static struct scsi_host_cmd_pool *scsi_get_host_cmd_pool(gfp_t gfp_mask)
3561da177e4SLinus Torvalds {
3571c353f7dSJames Bottomley 	struct scsi_host_cmd_pool *retval = NULL, *pool;
3581da177e4SLinus Torvalds 	/*
3591da177e4SLinus Torvalds 	 * Select a command slab for this host and create it if not
360eb44820cSRob Landley 	 * yet existent.
3611da177e4SLinus Torvalds 	 */
3620b950672SArjan van de Ven 	mutex_lock(&host_cmd_pool_mutex);
3631c353f7dSJames Bottomley 	pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool :
3641c353f7dSJames Bottomley 		&scsi_cmd_pool;
3651da177e4SLinus Torvalds 	if (!pool->users) {
3665b7f1680SJames Bottomley 		pool->cmd_slab = kmem_cache_create(pool->cmd_name,
3671da177e4SLinus Torvalds 						   sizeof(struct scsi_cmnd), 0,
36820c2df83SPaul Mundt 						   pool->slab_flags, NULL);
3695b7f1680SJames Bottomley 		if (!pool->cmd_slab)
3701da177e4SLinus Torvalds 			goto fail;
3715b7f1680SJames Bottomley 
3725b7f1680SJames Bottomley 		pool->sense_slab = kmem_cache_create(pool->sense_name,
3735b7f1680SJames Bottomley 						     SCSI_SENSE_BUFFERSIZE, 0,
3745b7f1680SJames Bottomley 						     pool->slab_flags, NULL);
3755b7f1680SJames Bottomley 		if (!pool->sense_slab) {
3765b7f1680SJames Bottomley 			kmem_cache_destroy(pool->cmd_slab);
3775b7f1680SJames Bottomley 			goto fail;
3785b7f1680SJames Bottomley 		}
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds 	pool->users++;
3821c353f7dSJames Bottomley 	retval = pool;
3831c353f7dSJames Bottomley  fail:
3840b950672SArjan van de Ven 	mutex_unlock(&host_cmd_pool_mutex);
3851c353f7dSJames Bottomley 	return retval;
3861c353f7dSJames Bottomley }
3871da177e4SLinus Torvalds 
3881c353f7dSJames Bottomley static void scsi_put_host_cmd_pool(gfp_t gfp_mask)
3891c353f7dSJames Bottomley {
3901c353f7dSJames Bottomley 	struct scsi_host_cmd_pool *pool;
391de25deb1SFUJITA Tomonori 
392166a7287SFUJITA Tomonori 	mutex_lock(&host_cmd_pool_mutex);
3931c353f7dSJames Bottomley 	pool = (gfp_mask & __GFP_DMA) ? &scsi_cmd_dma_pool :
3941c353f7dSJames Bottomley 		&scsi_cmd_pool;
3951c353f7dSJames Bottomley 	/*
3961c353f7dSJames Bottomley 	 * This may happen if a driver has a mismatched get and put
3971c353f7dSJames Bottomley 	 * of the command pool; the driver should be implicated in
3981c353f7dSJames Bottomley 	 * the stack trace
3991c353f7dSJames Bottomley 	 */
4001c353f7dSJames Bottomley 	BUG_ON(pool->users == 0);
4011c353f7dSJames Bottomley 
4025b7f1680SJames Bottomley 	if (!--pool->users) {
4035b7f1680SJames Bottomley 		kmem_cache_destroy(pool->cmd_slab);
4045b7f1680SJames Bottomley 		kmem_cache_destroy(pool->sense_slab);
4055b7f1680SJames Bottomley 	}
4060b950672SArjan van de Ven 	mutex_unlock(&host_cmd_pool_mutex);
4071c353f7dSJames Bottomley }
4081c353f7dSJames Bottomley 
4091c353f7dSJames Bottomley /**
4101c353f7dSJames Bottomley  * scsi_allocate_command - get a fully allocated SCSI command
4111c353f7dSJames Bottomley  * @gfp_mask:	allocation mask
4121c353f7dSJames Bottomley  *
4131c353f7dSJames Bottomley  * This function is for use outside of the normal host based pools.
4141c353f7dSJames Bottomley  * It allocates the relevant command and takes an additional reference
4151c353f7dSJames Bottomley  * on the pool it used.  This function *must* be paired with
4161c353f7dSJames Bottomley  * scsi_free_command which also has the identical mask, otherwise the
4171c353f7dSJames Bottomley  * free pool counts will eventually go wrong and you'll trigger a bug.
4181c353f7dSJames Bottomley  *
4191c353f7dSJames Bottomley  * This function should *only* be used by drivers that need a static
4201c353f7dSJames Bottomley  * command allocation at start of day for internal functions.
4211c353f7dSJames Bottomley  */
4221c353f7dSJames Bottomley struct scsi_cmnd *scsi_allocate_command(gfp_t gfp_mask)
4231c353f7dSJames Bottomley {
4241c353f7dSJames Bottomley 	struct scsi_host_cmd_pool *pool = scsi_get_host_cmd_pool(gfp_mask);
4251c353f7dSJames Bottomley 
4261c353f7dSJames Bottomley 	if (!pool)
4271c353f7dSJames Bottomley 		return NULL;
4281c353f7dSJames Bottomley 
4291c353f7dSJames Bottomley 	return scsi_pool_alloc_command(pool, gfp_mask);
4301c353f7dSJames Bottomley }
4311c353f7dSJames Bottomley EXPORT_SYMBOL(scsi_allocate_command);
4321c353f7dSJames Bottomley 
4331c353f7dSJames Bottomley /**
4341c353f7dSJames Bottomley  * scsi_free_command - free a command allocated by scsi_allocate_command
4351c353f7dSJames Bottomley  * @gfp_mask:	mask used in the original allocation
4361c353f7dSJames Bottomley  * @cmd:	command to free
4371c353f7dSJames Bottomley  *
4381c353f7dSJames Bottomley  * Note: using the original allocation mask is vital because that's
4391c353f7dSJames Bottomley  * what determines which command pool we use to free the command.  Any
4401c353f7dSJames Bottomley  * mismatch will cause the system to BUG eventually.
4411c353f7dSJames Bottomley  */
4421c353f7dSJames Bottomley void scsi_free_command(gfp_t gfp_mask, struct scsi_cmnd *cmd)
4431c353f7dSJames Bottomley {
4441c353f7dSJames Bottomley 	struct scsi_host_cmd_pool *pool = scsi_get_host_cmd_pool(gfp_mask);
4451c353f7dSJames Bottomley 
4461c353f7dSJames Bottomley 	/*
4471c353f7dSJames Bottomley 	 * this could trigger if the mask to scsi_allocate_command
4481c353f7dSJames Bottomley 	 * doesn't match this mask.  Otherwise we're guaranteed that this
4491c353f7dSJames Bottomley 	 * succeeds because scsi_allocate_command must have taken a reference
4501c353f7dSJames Bottomley 	 * on the pool
4511c353f7dSJames Bottomley 	 */
4521c353f7dSJames Bottomley 	BUG_ON(!pool);
4531c353f7dSJames Bottomley 
4541c353f7dSJames Bottomley 	scsi_pool_free_command(pool, cmd);
4551c353f7dSJames Bottomley 	/*
4561c353f7dSJames Bottomley 	 * scsi_put_host_cmd_pool is called twice; once to release the
4571c353f7dSJames Bottomley 	 * reference we took above, and once to release the reference
4581c353f7dSJames Bottomley 	 * originally taken by scsi_allocate_command
4591c353f7dSJames Bottomley 	 */
4601c353f7dSJames Bottomley 	scsi_put_host_cmd_pool(gfp_mask);
4611c353f7dSJames Bottomley 	scsi_put_host_cmd_pool(gfp_mask);
4621c353f7dSJames Bottomley }
4631c353f7dSJames Bottomley EXPORT_SYMBOL(scsi_free_command);
4641c353f7dSJames Bottomley 
4651c353f7dSJames Bottomley /**
4661c353f7dSJames Bottomley  * scsi_setup_command_freelist - Setup the command freelist for a scsi host.
4671c353f7dSJames Bottomley  * @shost: host to allocate the freelist for.
4681c353f7dSJames Bottomley  *
4691c353f7dSJames Bottomley  * Description: The command freelist protects against system-wide out of memory
4701c353f7dSJames Bottomley  * deadlock by preallocating one SCSI command structure for each host, so the
4711c353f7dSJames Bottomley  * system can always write to a swap file on a device associated with that host.
4721c353f7dSJames Bottomley  *
4731c353f7dSJames Bottomley  * Returns:	Nothing.
4741c353f7dSJames Bottomley  */
4751c353f7dSJames Bottomley int scsi_setup_command_freelist(struct Scsi_Host *shost)
4761c353f7dSJames Bottomley {
4771c353f7dSJames Bottomley 	struct scsi_cmnd *cmd;
4781c353f7dSJames Bottomley 	const gfp_t gfp_mask = shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL;
4791c353f7dSJames Bottomley 
4801c353f7dSJames Bottomley 	spin_lock_init(&shost->free_list_lock);
4811c353f7dSJames Bottomley 	INIT_LIST_HEAD(&shost->free_list);
4821c353f7dSJames Bottomley 
4831c353f7dSJames Bottomley 	shost->cmd_pool = scsi_get_host_cmd_pool(gfp_mask);
4841c353f7dSJames Bottomley 
4851c353f7dSJames Bottomley 	if (!shost->cmd_pool)
4861da177e4SLinus Torvalds 		return -ENOMEM;
4871c353f7dSJames Bottomley 
4881c353f7dSJames Bottomley 	/*
4891c353f7dSJames Bottomley 	 * Get one backup command for this host.
4901c353f7dSJames Bottomley 	 */
4917027ad72SMartin K. Petersen 	cmd = scsi_host_alloc_command(shost, gfp_mask);
4921c353f7dSJames Bottomley 	if (!cmd) {
4931c353f7dSJames Bottomley 		scsi_put_host_cmd_pool(gfp_mask);
49461d7416aSAlan D. Brunelle 		shost->cmd_pool = NULL;
4951c353f7dSJames Bottomley 		return -ENOMEM;
4961c353f7dSJames Bottomley 	}
4971c353f7dSJames Bottomley 	list_add(&cmd->list, &shost->free_list);
4981c353f7dSJames Bottomley 	return 0;
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
501eb44820cSRob Landley /**
502eb44820cSRob Landley  * scsi_destroy_command_freelist - Release the command freelist for a scsi host.
503eb44820cSRob Landley  * @shost: host whose freelist is going to be destroyed
5041da177e4SLinus Torvalds  */
5051da177e4SLinus Torvalds void scsi_destroy_command_freelist(struct Scsi_Host *shost)
5061da177e4SLinus Torvalds {
50761d7416aSAlan D. Brunelle 	/*
50861d7416aSAlan D. Brunelle 	 * If cmd_pool is NULL the free list was not initialized, so
50961d7416aSAlan D. Brunelle 	 * do not attempt to release resources.
51061d7416aSAlan D. Brunelle 	 */
51161d7416aSAlan D. Brunelle 	if (!shost->cmd_pool)
51261d7416aSAlan D. Brunelle 		return;
51361d7416aSAlan D. Brunelle 
5141da177e4SLinus Torvalds 	while (!list_empty(&shost->free_list)) {
5151da177e4SLinus Torvalds 		struct scsi_cmnd *cmd;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 		cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
5181da177e4SLinus Torvalds 		list_del_init(&cmd->list);
519e507e30bSJames Bottomley 		scsi_pool_free_command(shost->cmd_pool, cmd);
5201da177e4SLinus Torvalds 	}
5211c353f7dSJames Bottomley 	shost->cmd_pool = NULL;
5221c353f7dSJames Bottomley 	scsi_put_host_cmd_pool(shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL);
523de25deb1SFUJITA Tomonori }
524de25deb1SFUJITA Tomonori 
5251da177e4SLinus Torvalds #ifdef CONFIG_SCSI_LOGGING
5261da177e4SLinus Torvalds void scsi_log_send(struct scsi_cmnd *cmd)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	unsigned int level;
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	/*
5311da177e4SLinus Torvalds 	 * If ML QUEUE log level is greater than or equal to:
5321da177e4SLinus Torvalds 	 *
5331da177e4SLinus Torvalds 	 * 1: nothing (match completion)
5341da177e4SLinus Torvalds 	 *
5351da177e4SLinus Torvalds 	 * 2: log opcode + command of all commands
5361da177e4SLinus Torvalds 	 *
5371da177e4SLinus Torvalds 	 * 3: same as 2 plus dump cmd address
5381da177e4SLinus Torvalds 	 *
5391da177e4SLinus Torvalds 	 * 4: same as 3 plus dump extra junk
5401da177e4SLinus Torvalds 	 */
5411da177e4SLinus Torvalds 	if (unlikely(scsi_logging_level)) {
5421da177e4SLinus Torvalds 		level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
5431da177e4SLinus Torvalds 				       SCSI_LOG_MLQUEUE_BITS);
5441da177e4SLinus Torvalds 		if (level > 1) {
545a4d04a4cSMartin K. Petersen 			scmd_printk(KERN_INFO, cmd, "Send: ");
5461da177e4SLinus Torvalds 			if (level > 2)
5471da177e4SLinus Torvalds 				printk("0x%p ", cmd);
548a4d04a4cSMartin K. Petersen 			printk("\n");
5491da177e4SLinus Torvalds 			scsi_print_command(cmd);
5501da177e4SLinus Torvalds 			if (level > 3) {
5511da177e4SLinus Torvalds 				printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
5527b3d9545SLinus Torvalds 				       " queuecommand 0x%p\n",
553a73e45b3SBoaz Harrosh 					scsi_sglist(cmd), scsi_bufflen(cmd),
554a4d04a4cSMartin K. Petersen 					cmd->device->host->hostt->queuecommand);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 			}
5571da177e4SLinus Torvalds 		}
5581da177e4SLinus Torvalds 	}
5591da177e4SLinus Torvalds }
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
5621da177e4SLinus Torvalds {
5631da177e4SLinus Torvalds 	unsigned int level;
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds 	/*
5661da177e4SLinus Torvalds 	 * If ML COMPLETE log level is greater than or equal to:
5671da177e4SLinus Torvalds 	 *
5681da177e4SLinus Torvalds 	 * 1: log disposition, result, opcode + command, and conditionally
5691da177e4SLinus Torvalds 	 * sense data for failures or non SUCCESS dispositions.
5701da177e4SLinus Torvalds 	 *
5711da177e4SLinus Torvalds 	 * 2: same as 1 but for all command completions.
5721da177e4SLinus Torvalds 	 *
5731da177e4SLinus Torvalds 	 * 3: same as 2 plus dump cmd address
5741da177e4SLinus Torvalds 	 *
5751da177e4SLinus Torvalds 	 * 4: same as 3 plus dump extra junk
5761da177e4SLinus Torvalds 	 */
5771da177e4SLinus Torvalds 	if (unlikely(scsi_logging_level)) {
5781da177e4SLinus Torvalds 		level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
5791da177e4SLinus Torvalds 				       SCSI_LOG_MLCOMPLETE_BITS);
5801da177e4SLinus Torvalds 		if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
5811da177e4SLinus Torvalds 		    (level > 1)) {
582a4d04a4cSMartin K. Petersen 			scmd_printk(KERN_INFO, cmd, "Done: ");
5831da177e4SLinus Torvalds 			if (level > 2)
5841da177e4SLinus Torvalds 				printk("0x%p ", cmd);
5851da177e4SLinus Torvalds 			/*
5861da177e4SLinus Torvalds 			 * Dump truncated values, so we usually fit within
5871da177e4SLinus Torvalds 			 * 80 chars.
5881da177e4SLinus Torvalds 			 */
5891da177e4SLinus Torvalds 			switch (disposition) {
5901da177e4SLinus Torvalds 			case SUCCESS:
591a4d04a4cSMartin K. Petersen 				printk("SUCCESS\n");
5921da177e4SLinus Torvalds 				break;
5931da177e4SLinus Torvalds 			case NEEDS_RETRY:
594a4d04a4cSMartin K. Petersen 				printk("RETRY\n");
5951da177e4SLinus Torvalds 				break;
5961da177e4SLinus Torvalds 			case ADD_TO_MLQUEUE:
597a4d04a4cSMartin K. Petersen 				printk("MLQUEUE\n");
5981da177e4SLinus Torvalds 				break;
5991da177e4SLinus Torvalds 			case FAILED:
600a4d04a4cSMartin K. Petersen 				printk("FAILED\n");
6011da177e4SLinus Torvalds 				break;
6021da177e4SLinus Torvalds 			case TIMEOUT_ERROR:
6031da177e4SLinus Torvalds 				/*
6041da177e4SLinus Torvalds 				 * If called via scsi_times_out.
6051da177e4SLinus Torvalds 				 */
606a4d04a4cSMartin K. Petersen 				printk("TIMEOUT\n");
6071da177e4SLinus Torvalds 				break;
6081da177e4SLinus Torvalds 			default:
609a4d04a4cSMartin K. Petersen 				printk("UNKNOWN\n");
6101da177e4SLinus Torvalds 			}
611a4d04a4cSMartin K. Petersen 			scsi_print_result(cmd);
6121da177e4SLinus Torvalds 			scsi_print_command(cmd);
613a4d04a4cSMartin K. Petersen 			if (status_byte(cmd->result) & CHECK_CONDITION)
6141da177e4SLinus Torvalds 				scsi_print_sense("", cmd);
615a4d04a4cSMartin K. Petersen 			if (level > 3)
616a4d04a4cSMartin K. Petersen 				scmd_printk(KERN_INFO, cmd,
617a4d04a4cSMartin K. Petersen 					    "scsi host busy %d failed %d\n",
618a4d04a4cSMartin K. Petersen 					    cmd->device->host->host_busy,
619a4d04a4cSMartin K. Petersen 					    cmd->device->host->host_failed);
6201da177e4SLinus Torvalds 		}
6211da177e4SLinus Torvalds 	}
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds #endif
6241da177e4SLinus Torvalds 
625eb44820cSRob Landley /**
626eb44820cSRob Landley  * scsi_cmd_get_serial - Assign a serial number to a command
627eb44820cSRob Landley  * @host: the scsi host
628eb44820cSRob Landley  * @cmd: command to assign serial number to
629eb44820cSRob Landley  *
630eb44820cSRob Landley  * Description: a serial number identifies a request for error recovery
6311da177e4SLinus Torvalds  * and debugging purposes.  Protected by the Host_Lock of host.
6321da177e4SLinus Torvalds  */
6331da177e4SLinus Torvalds static inline void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	cmd->serial_number = host->cmd_serial_number++;
6361da177e4SLinus Torvalds 	if (cmd->serial_number == 0)
6371da177e4SLinus Torvalds 		cmd->serial_number = host->cmd_serial_number++;
6381da177e4SLinus Torvalds }
6391da177e4SLinus Torvalds 
640eb44820cSRob Landley /**
641eb44820cSRob Landley  * scsi_dispatch_command - Dispatch a command to the low-level driver.
642eb44820cSRob Landley  * @cmd: command block we are dispatching.
6431da177e4SLinus Torvalds  *
644eb44820cSRob Landley  * Return: nonzero return request was rejected and device's queue needs to be
645eb44820cSRob Landley  * plugged.
6461da177e4SLinus Torvalds  */
6471da177e4SLinus Torvalds int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	struct Scsi_Host *host = cmd->device->host;
6501da177e4SLinus Torvalds 	unsigned long flags = 0;
6511da177e4SLinus Torvalds 	unsigned long timeout;
6521da177e4SLinus Torvalds 	int rtn = 0;
6531da177e4SLinus Torvalds 
654242f9dcbSJens Axboe 	/*
655242f9dcbSJens Axboe 	 * We will use a queued command if possible, otherwise we will
656242f9dcbSJens Axboe 	 * emulate the queuing and calling of completion function ourselves.
657242f9dcbSJens Axboe 	 */
658242f9dcbSJens Axboe 	atomic_inc(&cmd->device->iorequest_cnt);
659242f9dcbSJens Axboe 
6601da177e4SLinus Torvalds 	/* check if the device is still usable */
6611da177e4SLinus Torvalds 	if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
6621da177e4SLinus Torvalds 		/* in SDEV_DEL we error all commands. DID_NO_CONNECT
6631da177e4SLinus Torvalds 		 * returns an immediate error upwards, and signals
6641da177e4SLinus Torvalds 		 * that the device is no longer present */
6651da177e4SLinus Torvalds 		cmd->result = DID_NO_CONNECT << 16;
666242f9dcbSJens Axboe 		scsi_done(cmd);
6671da177e4SLinus Torvalds 		/* return 0 (because the command has been processed) */
6681da177e4SLinus Torvalds 		goto out;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 
6710f1d87a2SJames Bottomley 	/* Check to see if the scsi lld made this device blocked. */
6720f1d87a2SJames Bottomley 	if (unlikely(scsi_device_blocked(cmd->device))) {
6731da177e4SLinus Torvalds 		/*
6740f1d87a2SJames Bottomley 		 * in blocked state, the command is just put back on
6750f1d87a2SJames Bottomley 		 * the device queue.  The suspend state has already
6760f1d87a2SJames Bottomley 		 * blocked the queue so future requests should not
6770f1d87a2SJames Bottomley 		 * occur until the device transitions out of the
6780f1d87a2SJames Bottomley 		 * suspend state.
6791da177e4SLinus Torvalds 		 */
680242f9dcbSJens Axboe 
6811da177e4SLinus Torvalds 		scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 		SCSI_LOG_MLQUEUE(3, printk("queuecommand : device blocked \n"));
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 		/*
6861da177e4SLinus Torvalds 		 * NOTE: rtn is still zero here because we don't need the
6871da177e4SLinus Torvalds 		 * queue to be plugged on return (it's already stopped)
6881da177e4SLinus Torvalds 		 */
6891da177e4SLinus Torvalds 		goto out;
6901da177e4SLinus Torvalds 	}
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	/*
6931da177e4SLinus Torvalds 	 * If SCSI-2 or lower, store the LUN value in cmnd.
6941da177e4SLinus Torvalds 	 */
6954d7db04aSJames Bottomley 	if (cmd->device->scsi_level <= SCSI_2 &&
6964d7db04aSJames Bottomley 	    cmd->device->scsi_level != SCSI_UNKNOWN) {
6971da177e4SLinus Torvalds 		cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
6981da177e4SLinus Torvalds 			       (cmd->device->lun << 5 & 0xe0);
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	/*
7021da177e4SLinus Torvalds 	 * We will wait MIN_RESET_DELAY clock ticks after the last reset so
7031da177e4SLinus Torvalds 	 * we can avoid the drive not being ready.
7041da177e4SLinus Torvalds 	 */
7051da177e4SLinus Torvalds 	timeout = host->last_reset + MIN_RESET_DELAY;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	if (host->resetting && time_before(jiffies, timeout)) {
7081da177e4SLinus Torvalds 		int ticks_remaining = timeout - jiffies;
7091da177e4SLinus Torvalds 		/*
7101da177e4SLinus Torvalds 		 * NOTE: This may be executed from within an interrupt
7111da177e4SLinus Torvalds 		 * handler!  This is bad, but for now, it'll do.  The irq
7121da177e4SLinus Torvalds 		 * level of the interrupt handler has been masked out by the
7131da177e4SLinus Torvalds 		 * platform dependent interrupt handling code already, so the
7141da177e4SLinus Torvalds 		 * sti() here will not cause another call to the SCSI host's
7151da177e4SLinus Torvalds 		 * interrupt handler (assuming there is one irq-level per
7161da177e4SLinus Torvalds 		 * host).
7171da177e4SLinus Torvalds 		 */
7181da177e4SLinus Torvalds 		while (--ticks_remaining >= 0)
7191da177e4SLinus Torvalds 			mdelay(1 + 999 / HZ);
7201da177e4SLinus Torvalds 		host->resetting = 0;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds 	scsi_log_send(cmd);
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	/*
7261da177e4SLinus Torvalds 	 * Before we queue this command, check if the command
7271da177e4SLinus Torvalds 	 * length exceeds what the host adapter can handle.
7281da177e4SLinus Torvalds 	 */
729db4742ddSBoaz Harrosh 	if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
7301da177e4SLinus Torvalds 		SCSI_LOG_MLQUEUE(3,
731db4742ddSBoaz Harrosh 			printk("queuecommand : command too long. "
732db4742ddSBoaz Harrosh 			       "cdb_size=%d host->max_cmd_len=%d\n",
733db4742ddSBoaz Harrosh 			       cmd->cmd_len, cmd->device->host->max_cmd_len));
7341da177e4SLinus Torvalds 		cmd->result = (DID_ABORT << 16);
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds 		scsi_done(cmd);
7371da177e4SLinus Torvalds 		goto out;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	spin_lock_irqsave(host->host_lock, flags);
741242f9dcbSJens Axboe 	/*
742242f9dcbSJens Axboe 	 * AK: unlikely race here: for some reason the timer could
743242f9dcbSJens Axboe 	 * expire before the serial number is set up below.
744242f9dcbSJens Axboe 	 *
745242f9dcbSJens Axboe 	 * TODO: kill serial or move to blk layer
746242f9dcbSJens Axboe 	 */
7471da177e4SLinus Torvalds 	scsi_cmd_get_serial(host, cmd);
7481da177e4SLinus Torvalds 
749d2c9d9eaSMike Anderson 	if (unlikely(host->shost_state == SHOST_DEL)) {
7501da177e4SLinus Torvalds 		cmd->result = (DID_NO_CONNECT << 16);
7511da177e4SLinus Torvalds 		scsi_done(cmd);
7521da177e4SLinus Torvalds 	} else {
7531da177e4SLinus Torvalds 		rtn = host->hostt->queuecommand(cmd, scsi_done);
7541da177e4SLinus Torvalds 	}
7551da177e4SLinus Torvalds 	spin_unlock_irqrestore(host->host_lock, flags);
7561da177e4SLinus Torvalds 	if (rtn) {
757f0c0a376SMike Christie 		if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
758f0c0a376SMike Christie 		    rtn != SCSI_MLQUEUE_TARGET_BUSY)
759f0c0a376SMike Christie 			rtn = SCSI_MLQUEUE_HOST_BUSY;
760f0c0a376SMike Christie 
761f0c0a376SMike Christie 		scsi_queue_insert(cmd, rtn);
762f0c0a376SMike Christie 
7631da177e4SLinus Torvalds 		SCSI_LOG_MLQUEUE(3,
7641da177e4SLinus Torvalds 		    printk("queuecommand : request rejected\n"));
7651da177e4SLinus Torvalds 	}
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds  out:
7681da177e4SLinus Torvalds 	SCSI_LOG_MLQUEUE(3, printk("leaving scsi_dispatch_cmnd()\n"));
7691da177e4SLinus Torvalds 	return rtn;
7701da177e4SLinus Torvalds }
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds /**
7731da177e4SLinus Torvalds  * scsi_done - Enqueue the finished SCSI command into the done queue.
7741da177e4SLinus Torvalds  * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
7751da177e4SLinus Torvalds  * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
7761da177e4SLinus Torvalds  *
777eb44820cSRob Landley  * Description: This function is the mid-level's (SCSI Core) interrupt routine,
778eb44820cSRob Landley  * which regains ownership of the SCSI command (de facto) from a LLDD, and
779eb44820cSRob Landley  * enqueues the command to the done queue for further processing.
7801da177e4SLinus Torvalds  *
7811da177e4SLinus Torvalds  * This is the producer of the done queue who enqueues at the tail.
7821da177e4SLinus Torvalds  *
7831da177e4SLinus Torvalds  * This function is interrupt context safe.
7841da177e4SLinus Torvalds  */
78552c1da39SAdrian Bunk static void scsi_done(struct scsi_cmnd *cmd)
7861da177e4SLinus Torvalds {
787242f9dcbSJens Axboe 	blk_complete_request(cmd->request);
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
7907b3d9545SLinus Torvalds /* Move this to a header if it becomes more generally useful */
7917b3d9545SLinus Torvalds static struct scsi_driver *scsi_cmd_to_driver(struct scsi_cmnd *cmd)
7927b3d9545SLinus Torvalds {
7937b3d9545SLinus Torvalds 	return *(struct scsi_driver **)cmd->request->rq_disk->private_data;
7947b3d9545SLinus Torvalds }
7957b3d9545SLinus Torvalds 
796eb44820cSRob Landley /**
797eb44820cSRob Landley  * scsi_finish_command - cleanup and pass command back to upper layer
798eb44820cSRob Landley  * @cmd: the command
7991da177e4SLinus Torvalds  *
800eb44820cSRob Landley  * Description: Pass command off to upper layer for finishing of I/O
8011da177e4SLinus Torvalds  *              request, waking processes that are waiting on results,
8021da177e4SLinus Torvalds  *              etc.
8031da177e4SLinus Torvalds  */
8041da177e4SLinus Torvalds void scsi_finish_command(struct scsi_cmnd *cmd)
8051da177e4SLinus Torvalds {
8061da177e4SLinus Torvalds 	struct scsi_device *sdev = cmd->device;
807f0c0a376SMike Christie 	struct scsi_target *starget = scsi_target(sdev);
8081da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
8097b3d9545SLinus Torvalds 	struct scsi_driver *drv;
8107b3d9545SLinus Torvalds 	unsigned int good_bytes;
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	scsi_device_unbusy(sdev);
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds         /*
8151da177e4SLinus Torvalds          * Clear the flags which say that the device/host is no longer
8161da177e4SLinus Torvalds          * capable of accepting new commands.  These are set in scsi_queue.c
8171da177e4SLinus Torvalds          * for both the queue full condition on a device, and for a
8181da177e4SLinus Torvalds          * host full condition on the host.
8191da177e4SLinus Torvalds 	 *
8201da177e4SLinus Torvalds 	 * XXX(hch): What about locking?
8211da177e4SLinus Torvalds          */
8221da177e4SLinus Torvalds         shost->host_blocked = 0;
823f0c0a376SMike Christie 	starget->target_blocked = 0;
8241da177e4SLinus Torvalds         sdev->device_blocked = 0;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	/*
8271da177e4SLinus Torvalds 	 * If we have valid sense information, then some kind of recovery
8281da177e4SLinus Torvalds 	 * must have taken place.  Make a note of this.
8291da177e4SLinus Torvalds 	 */
8301da177e4SLinus Torvalds 	if (SCSI_SENSE_VALID(cmd))
8311da177e4SLinus Torvalds 		cmd->result |= (DRIVER_SENSE << 24);
8321da177e4SLinus Torvalds 
8333bf743e7SJeff Garzik 	SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
8343bf743e7SJeff Garzik 				"Notifying upper driver of completion "
8353bf743e7SJeff Garzik 				"(result %x)\n", cmd->result));
8361da177e4SLinus Torvalds 
837f18573abSFUJITA Tomonori 	good_bytes = scsi_bufflen(cmd);
8387b3d9545SLinus Torvalds         if (cmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
839427e59f0SJames Bottomley 		int old_good_bytes = good_bytes;
8407b3d9545SLinus Torvalds 		drv = scsi_cmd_to_driver(cmd);
8417b3d9545SLinus Torvalds 		if (drv->done)
8427b3d9545SLinus Torvalds 			good_bytes = drv->done(cmd);
843427e59f0SJames Bottomley 		/*
844427e59f0SJames Bottomley 		 * USB may not give sense identifying bad sector and
845427e59f0SJames Bottomley 		 * simply return a residue instead, so subtract off the
846427e59f0SJames Bottomley 		 * residue if drv->done() error processing indicates no
847427e59f0SJames Bottomley 		 * change to the completion length.
848427e59f0SJames Bottomley 		 */
849427e59f0SJames Bottomley 		if (good_bytes == old_good_bytes)
850427e59f0SJames Bottomley 			good_bytes -= scsi_get_resid(cmd);
8517b3d9545SLinus Torvalds 	}
8527b3d9545SLinus Torvalds 	scsi_io_completion(cmd, good_bytes);
8531da177e4SLinus Torvalds }
8541da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_finish_command);
8551da177e4SLinus Torvalds 
856eb44820cSRob Landley /**
857eb44820cSRob Landley  * scsi_adjust_queue_depth - Let low level drivers change a device's queue depth
858eb44820cSRob Landley  * @sdev: SCSI Device in question
859eb44820cSRob Landley  * @tagged: Do we use tagged queueing (non-0) or do we treat
8601da177e4SLinus Torvalds  *          this device as an untagged device (0)
861eb44820cSRob Landley  * @tags: Number of tags allowed if tagged queueing enabled,
8621da177e4SLinus Torvalds  *        or number of commands the low level driver can
8631da177e4SLinus Torvalds  *        queue up in non-tagged mode (as per cmd_per_lun).
8641da177e4SLinus Torvalds  *
8651da177e4SLinus Torvalds  * Returns:	Nothing
8661da177e4SLinus Torvalds  *
8671da177e4SLinus Torvalds  * Lock Status:	None held on entry
8681da177e4SLinus Torvalds  *
8691da177e4SLinus Torvalds  * Notes:	Low level drivers may call this at any time and we will do
8701da177e4SLinus Torvalds  * 		the right thing depending on whether or not the device is
8711da177e4SLinus Torvalds  * 		currently active and whether or not it even has the
8721da177e4SLinus Torvalds  * 		command blocks built yet.
8731da177e4SLinus Torvalds  */
8741da177e4SLinus Torvalds void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
8751da177e4SLinus Torvalds {
8761da177e4SLinus Torvalds 	unsigned long flags;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	/*
8791da177e4SLinus Torvalds 	 * refuse to set tagged depth to an unworkable size
8801da177e4SLinus Torvalds 	 */
8811da177e4SLinus Torvalds 	if (tags <= 0)
8821da177e4SLinus Torvalds 		return;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
8851da177e4SLinus Torvalds 
886885ace9eSMike Christie 	/*
887885ace9eSMike Christie 	 * Check to see if the queue is managed by the block layer.
888885ace9eSMike Christie 	 * If it is, and we fail to adjust the depth, exit.
889885ace9eSMike Christie 	 *
890885ace9eSMike Christie 	 * Do not resize the tag map if it is a host wide share bqt,
891885ace9eSMike Christie 	 * because the size should be the hosts's can_queue. If there
892885ace9eSMike Christie 	 * is more IO than the LLD's can_queue (so there are not enuogh
893885ace9eSMike Christie 	 * tags) request_fn's host queue ready check will handle it.
894885ace9eSMike Christie 	 */
895885ace9eSMike Christie 	if (!sdev->host->bqt) {
8961da177e4SLinus Torvalds 		if (blk_queue_tagged(sdev->request_queue) &&
8971da177e4SLinus Torvalds 		    blk_queue_resize_tags(sdev->request_queue, tags) != 0)
8981da177e4SLinus Torvalds 			goto out;
899885ace9eSMike Christie 	}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds 	sdev->queue_depth = tags;
9021da177e4SLinus Torvalds 	switch (tagged) {
9031da177e4SLinus Torvalds 		case MSG_ORDERED_TAG:
9041da177e4SLinus Torvalds 			sdev->ordered_tags = 1;
9051da177e4SLinus Torvalds 			sdev->simple_tags = 1;
9061da177e4SLinus Torvalds 			break;
9071da177e4SLinus Torvalds 		case MSG_SIMPLE_TAG:
9081da177e4SLinus Torvalds 			sdev->ordered_tags = 0;
9091da177e4SLinus Torvalds 			sdev->simple_tags = 1;
9101da177e4SLinus Torvalds 			break;
9111da177e4SLinus Torvalds 		default:
9129ccfc756SJames Bottomley 			sdev_printk(KERN_WARNING, sdev,
9131da177e4SLinus Torvalds 				    "scsi_adjust_queue_depth, bad queue type, "
9149ccfc756SJames Bottomley 				    "disabled\n");
9151da177e4SLinus Torvalds 		case 0:
9161da177e4SLinus Torvalds 			sdev->ordered_tags = sdev->simple_tags = 0;
9171da177e4SLinus Torvalds 			sdev->queue_depth = tags;
9181da177e4SLinus Torvalds 			break;
9191da177e4SLinus Torvalds 	}
9201da177e4SLinus Torvalds  out:
9211da177e4SLinus Torvalds 	spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
9221da177e4SLinus Torvalds }
9231da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_adjust_queue_depth);
9241da177e4SLinus Torvalds 
925eb44820cSRob Landley /**
926eb44820cSRob Landley  * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
927eb44820cSRob Landley  * @sdev: SCSI Device in question
928eb44820cSRob Landley  * @depth: Current number of outstanding SCSI commands on this device,
929eb44820cSRob Landley  *         not counting the one returned as QUEUE_FULL.
9301da177e4SLinus Torvalds  *
931eb44820cSRob Landley  * Description:	This function will track successive QUEUE_FULL events on a
9321da177e4SLinus Torvalds  * 		specific SCSI device to determine if and when there is a
9331da177e4SLinus Torvalds  * 		need to adjust the queue depth on the device.
9341da177e4SLinus Torvalds  *
935eb44820cSRob Landley  * Returns:	0 - No change needed, >0 - Adjust queue depth to this new depth,
9361da177e4SLinus Torvalds  * 		-1 - Drop back to untagged operation using host->cmd_per_lun
9371da177e4SLinus Torvalds  * 			as the untagged command depth
9381da177e4SLinus Torvalds  *
9391da177e4SLinus Torvalds  * Lock Status:	None held on entry
9401da177e4SLinus Torvalds  *
9411da177e4SLinus Torvalds  * Notes:	Low level drivers may call this at any time and we will do
9421da177e4SLinus Torvalds  * 		"The Right Thing."  We are interrupt context safe.
9431da177e4SLinus Torvalds  */
9441da177e4SLinus Torvalds int scsi_track_queue_full(struct scsi_device *sdev, int depth)
9451da177e4SLinus Torvalds {
9461da177e4SLinus Torvalds 	if ((jiffies >> 4) == sdev->last_queue_full_time)
9471da177e4SLinus Torvalds 		return 0;
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	sdev->last_queue_full_time = (jiffies >> 4);
9501da177e4SLinus Torvalds 	if (sdev->last_queue_full_depth != depth) {
9511da177e4SLinus Torvalds 		sdev->last_queue_full_count = 1;
9521da177e4SLinus Torvalds 		sdev->last_queue_full_depth = depth;
9531da177e4SLinus Torvalds 	} else {
9541da177e4SLinus Torvalds 		sdev->last_queue_full_count++;
9551da177e4SLinus Torvalds 	}
9561da177e4SLinus Torvalds 
9571da177e4SLinus Torvalds 	if (sdev->last_queue_full_count <= 10)
9581da177e4SLinus Torvalds 		return 0;
9591da177e4SLinus Torvalds 	if (sdev->last_queue_full_depth < 8) {
9601da177e4SLinus Torvalds 		/* Drop back to untagged */
9611da177e4SLinus Torvalds 		scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
9621da177e4SLinus Torvalds 		return -1;
9631da177e4SLinus Torvalds 	}
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	if (sdev->ordered_tags)
9661da177e4SLinus Torvalds 		scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
9671da177e4SLinus Torvalds 	else
9681da177e4SLinus Torvalds 		scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
9691da177e4SLinus Torvalds 	return depth;
9701da177e4SLinus Torvalds }
9711da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_track_queue_full);
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds /**
974eb44820cSRob Landley  * scsi_device_get  -  get an additional reference to a scsi_device
9751da177e4SLinus Torvalds  * @sdev:	device to get a reference to
9761da177e4SLinus Torvalds  *
977eb44820cSRob Landley  * Description: Gets a reference to the scsi_device and increments the use count
9781da177e4SLinus Torvalds  * of the underlying LLDD module.  You must hold host_lock of the
9791da177e4SLinus Torvalds  * parent Scsi_Host or already have a reference when calling this.
9801da177e4SLinus Torvalds  */
9811da177e4SLinus Torvalds int scsi_device_get(struct scsi_device *sdev)
9821da177e4SLinus Torvalds {
98385b6c720SJames Bottomley 	if (sdev->sdev_state == SDEV_DEL)
9841da177e4SLinus Torvalds 		return -ENXIO;
9851da177e4SLinus Torvalds 	if (!get_device(&sdev->sdev_gendev))
9861da177e4SLinus Torvalds 		return -ENXIO;
98785b6c720SJames Bottomley 	/* We can fail this if we're doing SCSI operations
98885b6c720SJames Bottomley 	 * from module exit (like cache flush) */
98985b6c720SJames Bottomley 	try_module_get(sdev->host->hostt->module);
99085b6c720SJames Bottomley 
9911da177e4SLinus Torvalds 	return 0;
9921da177e4SLinus Torvalds }
9931da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_get);
9941da177e4SLinus Torvalds 
9951da177e4SLinus Torvalds /**
9961da177e4SLinus Torvalds  * scsi_device_put  -  release a reference to a scsi_device
9971da177e4SLinus Torvalds  * @sdev:	device to release a reference on.
9981da177e4SLinus Torvalds  *
999eb44820cSRob Landley  * Description: Release a reference to the scsi_device and decrements the use
1000eb44820cSRob Landley  * count of the underlying LLDD module.  The device is freed once the last
10011da177e4SLinus Torvalds  * user vanishes.
10021da177e4SLinus Torvalds  */
10031da177e4SLinus Torvalds void scsi_device_put(struct scsi_device *sdev)
10041da177e4SLinus Torvalds {
1005504fb37aSIngo Molnar #ifdef CONFIG_MODULE_UNLOAD
1006f479ab87SJames Bottomley 	struct module *module = sdev->host->hostt->module;
1007f479ab87SJames Bottomley 
100885b6c720SJames Bottomley 	/* The module refcount will be zero if scsi_device_get()
100985b6c720SJames Bottomley 	 * was called from a module removal routine */
1010f479ab87SJames Bottomley 	if (module && module_refcount(module) != 0)
1011f479ab87SJames Bottomley 		module_put(module);
1012a506b44bSDaniel Walker #endif
10131da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_put);
10161da177e4SLinus Torvalds 
1017eb44820cSRob Landley /* helper for shost_for_each_device, see that for documentation */
10181da177e4SLinus Torvalds struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
10191da177e4SLinus Torvalds 					   struct scsi_device *prev)
10201da177e4SLinus Torvalds {
10211da177e4SLinus Torvalds 	struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
10221da177e4SLinus Torvalds 	struct scsi_device *next = NULL;
10231da177e4SLinus Torvalds 	unsigned long flags;
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
10261da177e4SLinus Torvalds 	while (list->next != &shost->__devices) {
10271da177e4SLinus Torvalds 		next = list_entry(list->next, struct scsi_device, siblings);
10281da177e4SLinus Torvalds 		/* skip devices that we can't get a reference to */
10291da177e4SLinus Torvalds 		if (!scsi_device_get(next))
10301da177e4SLinus Torvalds 			break;
10311da177e4SLinus Torvalds 		next = NULL;
10321da177e4SLinus Torvalds 		list = list->next;
10331da177e4SLinus Torvalds 	}
10341da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
10351da177e4SLinus Torvalds 
10361da177e4SLinus Torvalds 	if (prev)
10371da177e4SLinus Torvalds 		scsi_device_put(prev);
10381da177e4SLinus Torvalds 	return next;
10391da177e4SLinus Torvalds }
10401da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_iterate_devices);
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds /**
10431da177e4SLinus Torvalds  * starget_for_each_device  -  helper to walk all devices of a target
10441da177e4SLinus Torvalds  * @starget:	target whose devices we want to iterate over.
1045eb44820cSRob Landley  * @data:	Opaque passed to each function call.
1046eb44820cSRob Landley  * @fn:		Function to call on each device
10471da177e4SLinus Torvalds  *
1048522939d4SMaciej W. Rozycki  * This traverses over each device of @starget.  The devices have
10491da177e4SLinus Torvalds  * a reference that must be released by scsi_host_put when breaking
10501da177e4SLinus Torvalds  * out of the loop.
10511da177e4SLinus Torvalds  */
10521da177e4SLinus Torvalds void starget_for_each_device(struct scsi_target *starget, void *data,
10531da177e4SLinus Torvalds 		     void (*fn)(struct scsi_device *, void *))
10541da177e4SLinus Torvalds {
10551da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
10561da177e4SLinus Torvalds 	struct scsi_device *sdev;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	shost_for_each_device(sdev, shost) {
10591da177e4SLinus Torvalds 		if ((sdev->channel == starget->channel) &&
10601da177e4SLinus Torvalds 		    (sdev->id == starget->id))
10611da177e4SLinus Torvalds 			fn(sdev, data);
10621da177e4SLinus Torvalds 	}
10631da177e4SLinus Torvalds }
10641da177e4SLinus Torvalds EXPORT_SYMBOL(starget_for_each_device);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds /**
106714f501a4SRandy Dunlap  * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
1068522939d4SMaciej W. Rozycki  * @starget:	target whose devices we want to iterate over.
106914f501a4SRandy Dunlap  * @data:	parameter for callback @fn()
107014f501a4SRandy Dunlap  * @fn:		callback function that is invoked for each device
1071522939d4SMaciej W. Rozycki  *
1072522939d4SMaciej W. Rozycki  * This traverses over each device of @starget.  It does _not_
1073522939d4SMaciej W. Rozycki  * take a reference on the scsi_device, so the whole loop must be
1074522939d4SMaciej W. Rozycki  * protected by shost->host_lock.
1075522939d4SMaciej W. Rozycki  *
1076522939d4SMaciej W. Rozycki  * Note:  The only reason why drivers would want to use this is because
1077522939d4SMaciej W. Rozycki  * they need to access the device list in irq context.  Otherwise you
1078522939d4SMaciej W. Rozycki  * really want to use starget_for_each_device instead.
1079522939d4SMaciej W. Rozycki  **/
1080522939d4SMaciej W. Rozycki void __starget_for_each_device(struct scsi_target *starget, void *data,
1081522939d4SMaciej W. Rozycki 			       void (*fn)(struct scsi_device *, void *))
1082522939d4SMaciej W. Rozycki {
1083522939d4SMaciej W. Rozycki 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1084522939d4SMaciej W. Rozycki 	struct scsi_device *sdev;
1085522939d4SMaciej W. Rozycki 
1086522939d4SMaciej W. Rozycki 	__shost_for_each_device(sdev, shost) {
1087522939d4SMaciej W. Rozycki 		if ((sdev->channel == starget->channel) &&
1088522939d4SMaciej W. Rozycki 		    (sdev->id == starget->id))
1089522939d4SMaciej W. Rozycki 			fn(sdev, data);
1090522939d4SMaciej W. Rozycki 	}
1091522939d4SMaciej W. Rozycki }
1092522939d4SMaciej W. Rozycki EXPORT_SYMBOL(__starget_for_each_device);
1093522939d4SMaciej W. Rozycki 
1094522939d4SMaciej W. Rozycki /**
10951da177e4SLinus Torvalds  * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
10961da177e4SLinus Torvalds  * @starget:	SCSI target pointer
10971da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
10981da177e4SLinus Torvalds  *
1099eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @lun for a given
11001da177e4SLinus Torvalds  * @starget.  The returned scsi_device does not have an additional
11011da177e4SLinus Torvalds  * reference.  You must hold the host's host_lock over this call and
11021da177e4SLinus Torvalds  * any access to the returned scsi_device.
11031da177e4SLinus Torvalds  *
1104dc8875e1SRandy Dunlap  * Note:  The only reason why drivers should use this is because
1105eb44820cSRob Landley  * they need to access the device list in irq context.  Otherwise you
11061da177e4SLinus Torvalds  * really want to use scsi_device_lookup_by_target instead.
11071da177e4SLinus Torvalds  **/
11081da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
11091da177e4SLinus Torvalds 						   uint lun)
11101da177e4SLinus Torvalds {
11111da177e4SLinus Torvalds 	struct scsi_device *sdev;
11121da177e4SLinus Torvalds 
11131da177e4SLinus Torvalds 	list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
11141da177e4SLinus Torvalds 		if (sdev->lun ==lun)
11151da177e4SLinus Torvalds 			return sdev;
11161da177e4SLinus Torvalds 	}
11171da177e4SLinus Torvalds 
11181da177e4SLinus Torvalds 	return NULL;
11191da177e4SLinus Torvalds }
11201da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup_by_target);
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds /**
11231da177e4SLinus Torvalds  * scsi_device_lookup_by_target - find a device given the target
11241da177e4SLinus Torvalds  * @starget:	SCSI target pointer
11251da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
11261da177e4SLinus Torvalds  *
1127eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
1128eb44820cSRob Landley  * for a given host.  The returned scsi_device has an additional reference that
1129eb44820cSRob Landley  * needs to be released with scsi_device_put once you're done with it.
11301da177e4SLinus Torvalds  **/
11311da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
11321da177e4SLinus Torvalds 						 uint lun)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	struct scsi_device *sdev;
11351da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
11361da177e4SLinus Torvalds 	unsigned long flags;
11371da177e4SLinus Torvalds 
11381da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
11391da177e4SLinus Torvalds 	sdev = __scsi_device_lookup_by_target(starget, lun);
11401da177e4SLinus Torvalds 	if (sdev && scsi_device_get(sdev))
11411da177e4SLinus Torvalds 		sdev = NULL;
11421da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	return sdev;
11451da177e4SLinus Torvalds }
11461da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup_by_target);
11471da177e4SLinus Torvalds 
11481da177e4SLinus Torvalds /**
1149eb44820cSRob Landley  * __scsi_device_lookup - find a device given the host (UNLOCKED)
11501da177e4SLinus Torvalds  * @shost:	SCSI host pointer
11511da177e4SLinus Torvalds  * @channel:	SCSI channel (zero if only one channel)
1152eb44820cSRob Landley  * @id:		SCSI target number (physical unit number)
11531da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
11541da177e4SLinus Torvalds  *
1155eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
1156eb44820cSRob Landley  * for a given host. The returned scsi_device does not have an additional
1157eb44820cSRob Landley  * reference.  You must hold the host's host_lock over this call and any access
1158eb44820cSRob Landley  * to the returned scsi_device.
11591da177e4SLinus Torvalds  *
11601da177e4SLinus Torvalds  * Note:  The only reason why drivers would want to use this is because
1161eb44820cSRob Landley  * they need to access the device list in irq context.  Otherwise you
11621da177e4SLinus Torvalds  * really want to use scsi_device_lookup instead.
11631da177e4SLinus Torvalds  **/
11641da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
11651da177e4SLinus Torvalds 		uint channel, uint id, uint lun)
11661da177e4SLinus Torvalds {
11671da177e4SLinus Torvalds 	struct scsi_device *sdev;
11681da177e4SLinus Torvalds 
11691da177e4SLinus Torvalds 	list_for_each_entry(sdev, &shost->__devices, siblings) {
11701da177e4SLinus Torvalds 		if (sdev->channel == channel && sdev->id == id &&
11711da177e4SLinus Torvalds 				sdev->lun ==lun)
11721da177e4SLinus Torvalds 			return sdev;
11731da177e4SLinus Torvalds 	}
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	return NULL;
11761da177e4SLinus Torvalds }
11771da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup);
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds /**
11801da177e4SLinus Torvalds  * scsi_device_lookup - find a device given the host
11811da177e4SLinus Torvalds  * @shost:	SCSI host pointer
11821da177e4SLinus Torvalds  * @channel:	SCSI channel (zero if only one channel)
11831da177e4SLinus Torvalds  * @id:		SCSI target number (physical unit number)
11841da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
11851da177e4SLinus Torvalds  *
1186eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
1187eb44820cSRob Landley  * for a given host.  The returned scsi_device has an additional reference that
1188eb44820cSRob Landley  * needs to be released with scsi_device_put once you're done with it.
11891da177e4SLinus Torvalds  **/
11901da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
11911da177e4SLinus Torvalds 		uint channel, uint id, uint lun)
11921da177e4SLinus Torvalds {
11931da177e4SLinus Torvalds 	struct scsi_device *sdev;
11941da177e4SLinus Torvalds 	unsigned long flags;
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
11971da177e4SLinus Torvalds 	sdev = __scsi_device_lookup(shost, channel, id, lun);
11981da177e4SLinus Torvalds 	if (sdev && scsi_device_get(sdev))
11991da177e4SLinus Torvalds 		sdev = NULL;
12001da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
12011da177e4SLinus Torvalds 
12021da177e4SLinus Torvalds 	return sdev;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup);
12051da177e4SLinus Torvalds 
12061da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI core");
12071da177e4SLinus Torvalds MODULE_LICENSE("GPL");
12081da177e4SLinus Torvalds 
12091da177e4SLinus Torvalds module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
12101da177e4SLinus Torvalds MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
12111da177e4SLinus Torvalds 
12121da177e4SLinus Torvalds static int __init init_scsi(void)
12131da177e4SLinus Torvalds {
12149bf09c23SJens Axboe 	int error;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 	error = scsi_init_queue();
12171da177e4SLinus Torvalds 	if (error)
12181da177e4SLinus Torvalds 		return error;
12191da177e4SLinus Torvalds 	error = scsi_init_procfs();
12201da177e4SLinus Torvalds 	if (error)
12211da177e4SLinus Torvalds 		goto cleanup_queue;
12221da177e4SLinus Torvalds 	error = scsi_init_devinfo();
12231da177e4SLinus Torvalds 	if (error)
12241da177e4SLinus Torvalds 		goto cleanup_procfs;
12251da177e4SLinus Torvalds 	error = scsi_init_hosts();
12261da177e4SLinus Torvalds 	if (error)
12271da177e4SLinus Torvalds 		goto cleanup_devlist;
12281da177e4SLinus Torvalds 	error = scsi_init_sysctl();
12291da177e4SLinus Torvalds 	if (error)
12301da177e4SLinus Torvalds 		goto cleanup_hosts;
12311da177e4SLinus Torvalds 	error = scsi_sysfs_register();
12321da177e4SLinus Torvalds 	if (error)
12331da177e4SLinus Torvalds 		goto cleanup_sysctl;
12341da177e4SLinus Torvalds 
123584314fd4SJames Smart 	scsi_netlink_init();
123684314fd4SJames Smart 
12371da177e4SLinus Torvalds 	printk(KERN_NOTICE "SCSI subsystem initialized\n");
12381da177e4SLinus Torvalds 	return 0;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds cleanup_sysctl:
12411da177e4SLinus Torvalds 	scsi_exit_sysctl();
12421da177e4SLinus Torvalds cleanup_hosts:
12431da177e4SLinus Torvalds 	scsi_exit_hosts();
12441da177e4SLinus Torvalds cleanup_devlist:
12451da177e4SLinus Torvalds 	scsi_exit_devinfo();
12461da177e4SLinus Torvalds cleanup_procfs:
12471da177e4SLinus Torvalds 	scsi_exit_procfs();
12481da177e4SLinus Torvalds cleanup_queue:
12491da177e4SLinus Torvalds 	scsi_exit_queue();
12501da177e4SLinus Torvalds 	printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
12511da177e4SLinus Torvalds 	       -error);
12521da177e4SLinus Torvalds 	return error;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds static void __exit exit_scsi(void)
12561da177e4SLinus Torvalds {
125784314fd4SJames Smart 	scsi_netlink_exit();
12581da177e4SLinus Torvalds 	scsi_sysfs_unregister();
12591da177e4SLinus Torvalds 	scsi_exit_sysctl();
12601da177e4SLinus Torvalds 	scsi_exit_hosts();
12611da177e4SLinus Torvalds 	scsi_exit_devinfo();
12621da177e4SLinus Torvalds 	scsi_exit_procfs();
12631da177e4SLinus Torvalds 	scsi_exit_queue();
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds subsys_initcall(init_scsi);
12671da177e4SLinus Torvalds module_exit(exit_scsi);
1268