xref: /openbmc/linux/drivers/scsi/scsi.c (revision d285203c)
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 
751da177e4SLinus Torvalds /*
761da177e4SLinus Torvalds  * Definitions and constants.
771da177e4SLinus Torvalds  */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /*
801da177e4SLinus Torvalds  * Note - the initial logging level can be set here to log events at boot time.
811da177e4SLinus Torvalds  * After the system is up, you may enable logging via the /proc interface.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds unsigned int scsi_logging_level;
841da177e4SLinus Torvalds #if defined(CONFIG_SCSI_LOGGING)
851da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_logging_level);
861da177e4SLinus Torvalds #endif
871da177e4SLinus Torvalds 
88ea80dadeSJames Bottomley /* sd, scsi core and power management need to coordinate flushing async actions */
892955b47dSDan Williams ASYNC_DOMAIN(scsi_sd_probe_domain);
90a7a20d10SDan Williams EXPORT_SYMBOL(scsi_sd_probe_domain);
91a7a20d10SDan Williams 
923c31b52fSDan Williams /*
933c31b52fSDan Williams  * Separate domain (from scsi_sd_probe_domain) to maximize the benefit of
943c31b52fSDan Williams  * asynchronous system resume operations.  It is marked 'exclusive' to avoid
953c31b52fSDan Williams  * being included in the async_synchronize_full() that is invoked by
963c31b52fSDan Williams  * dpm_resume()
973c31b52fSDan Williams  */
983c31b52fSDan Williams ASYNC_DOMAIN_EXCLUSIVE(scsi_sd_pm_domain);
993c31b52fSDan Williams EXPORT_SYMBOL(scsi_sd_pm_domain);
1003c31b52fSDan Williams 
1018a1cdc9cSMatthew Wilcox /* NB: These are exposed through /proc/scsi/scsi and form part of the ABI.
1028a1cdc9cSMatthew Wilcox  * You may not alter any existing entry (although adding new ones is
1038a1cdc9cSMatthew Wilcox  * encouraged once assigned by ANSI/INCITS T10
1048a1cdc9cSMatthew Wilcox  */
1054ff36718SMatthew Wilcox static const char *const scsi_device_types[] = {
1068a1cdc9cSMatthew Wilcox 	"Direct-Access    ",
1078a1cdc9cSMatthew Wilcox 	"Sequential-Access",
1081da177e4SLinus Torvalds 	"Printer          ",
1091da177e4SLinus Torvalds 	"Processor        ",
1101da177e4SLinus Torvalds 	"WORM             ",
1118a1cdc9cSMatthew Wilcox 	"CD-ROM           ",
1121da177e4SLinus Torvalds 	"Scanner          ",
1138a1cdc9cSMatthew Wilcox 	"Optical Device   ",
1148a1cdc9cSMatthew Wilcox 	"Medium Changer   ",
1151da177e4SLinus Torvalds 	"Communications   ",
1164ff36718SMatthew Wilcox 	"ASC IT8          ",
1174ff36718SMatthew Wilcox 	"ASC IT8          ",
1181da177e4SLinus Torvalds 	"RAID             ",
1191da177e4SLinus Torvalds 	"Enclosure        ",
1208a1cdc9cSMatthew Wilcox 	"Direct-Access-RBC",
1214ff36718SMatthew Wilcox 	"Optical card     ",
1224ff36718SMatthew Wilcox 	"Bridge controller",
1234ff36718SMatthew Wilcox 	"Object storage   ",
1244ff36718SMatthew Wilcox 	"Automation/Drive ",
1251da177e4SLinus Torvalds };
1264ff36718SMatthew Wilcox 
127eb44820cSRob Landley /**
128eb44820cSRob Landley  * scsi_device_type - Return 17 char string indicating device type.
129eb44820cSRob Landley  * @type: type number to look up
130eb44820cSRob Landley  */
131eb44820cSRob Landley 
1324ff36718SMatthew Wilcox const char * scsi_device_type(unsigned type)
1334ff36718SMatthew Wilcox {
1344ff36718SMatthew Wilcox 	if (type == 0x1e)
1354ff36718SMatthew Wilcox 		return "Well-known LUN   ";
1364ff36718SMatthew Wilcox 	if (type == 0x1f)
1374ff36718SMatthew Wilcox 		return "No Device        ";
13880c6e3c0SEric Sesterhenn 	if (type >= ARRAY_SIZE(scsi_device_types))
1394ff36718SMatthew Wilcox 		return "Unknown          ";
1404ff36718SMatthew Wilcox 	return scsi_device_types[type];
1414ff36718SMatthew Wilcox }
1424ff36718SMatthew Wilcox 
1434ff36718SMatthew Wilcox EXPORT_SYMBOL(scsi_device_type);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds struct scsi_host_cmd_pool {
1465b7f1680SJames Bottomley 	struct kmem_cache	*cmd_slab;
1475b7f1680SJames Bottomley 	struct kmem_cache	*sense_slab;
1481da177e4SLinus Torvalds 	unsigned int		users;
1495b7f1680SJames Bottomley 	char			*cmd_name;
1505b7f1680SJames Bottomley 	char			*sense_name;
1511da177e4SLinus Torvalds 	unsigned int		slab_flags;
152c53033f6SAl Viro 	gfp_t			gfp_mask;
1531da177e4SLinus Torvalds };
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_pool = {
1565b7f1680SJames Bottomley 	.cmd_name	= "scsi_cmd_cache",
1575b7f1680SJames Bottomley 	.sense_name	= "scsi_sense_cache",
1581da177e4SLinus Torvalds 	.slab_flags	= SLAB_HWCACHE_ALIGN,
1591da177e4SLinus Torvalds };
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
1625b7f1680SJames Bottomley 	.cmd_name	= "scsi_cmd_cache(DMA)",
1635b7f1680SJames Bottomley 	.sense_name	= "scsi_sense_cache(DMA)",
1641da177e4SLinus Torvalds 	.slab_flags	= SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
1651da177e4SLinus Torvalds 	.gfp_mask	= __GFP_DMA,
1661da177e4SLinus Torvalds };
1671da177e4SLinus Torvalds 
1680b950672SArjan van de Ven static DEFINE_MUTEX(host_cmd_pool_mutex);
1691da177e4SLinus Torvalds 
170eb44820cSRob Landley /**
1717c283341SChristoph Hellwig  * scsi_host_free_command - internal function to release a command
1727c283341SChristoph Hellwig  * @shost:	host to free the command for
173e507e30bSJames Bottomley  * @cmd:	command to release
174e507e30bSJames Bottomley  *
175e507e30bSJames Bottomley  * the command must previously have been allocated by
1767c283341SChristoph Hellwig  * scsi_host_alloc_command.
177e507e30bSJames Bottomley  */
178e507e30bSJames Bottomley static void
1797c283341SChristoph Hellwig scsi_host_free_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
180e507e30bSJames Bottomley {
1817c283341SChristoph Hellwig 	struct scsi_host_cmd_pool *pool = shost->cmd_pool;
1827c283341SChristoph Hellwig 
1837027ad72SMartin K. Petersen 	if (cmd->prot_sdb)
1847027ad72SMartin K. Petersen 		kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
185e507e30bSJames Bottomley 	kmem_cache_free(pool->sense_slab, cmd->sense_buffer);
186e507e30bSJames Bottomley 	kmem_cache_free(pool->cmd_slab, cmd);
187e507e30bSJames Bottomley }
188e507e30bSJames Bottomley 
189e507e30bSJames Bottomley /**
1907027ad72SMartin K. Petersen  * scsi_host_alloc_command - internal function to allocate command
1917027ad72SMartin K. Petersen  * @shost:	SCSI host whose pool to allocate from
1927027ad72SMartin K. Petersen  * @gfp_mask:	mask for the allocation
1937027ad72SMartin K. Petersen  *
1947027ad72SMartin K. Petersen  * Returns a fully allocated command with sense buffer and protection
1957027ad72SMartin K. Petersen  * data buffer (where applicable) or NULL on failure
1967027ad72SMartin K. Petersen  */
1977027ad72SMartin K. Petersen static struct scsi_cmnd *
1987027ad72SMartin K. Petersen scsi_host_alloc_command(struct Scsi_Host *shost, gfp_t gfp_mask)
1997027ad72SMartin K. Petersen {
2007c283341SChristoph Hellwig 	struct scsi_host_cmd_pool *pool = shost->cmd_pool;
2017027ad72SMartin K. Petersen 	struct scsi_cmnd *cmd;
2027027ad72SMartin K. Petersen 
2037c283341SChristoph Hellwig 	cmd = kmem_cache_zalloc(pool->cmd_slab, gfp_mask | pool->gfp_mask);
2047027ad72SMartin K. Petersen 	if (!cmd)
2057c283341SChristoph Hellwig 		goto fail;
2067c283341SChristoph Hellwig 
2077c283341SChristoph Hellwig 	cmd->sense_buffer = kmem_cache_alloc(pool->sense_slab,
2087c283341SChristoph Hellwig 					     gfp_mask | pool->gfp_mask);
2097c283341SChristoph Hellwig 	if (!cmd->sense_buffer)
2107c283341SChristoph Hellwig 		goto fail_free_cmd;
2117027ad72SMartin K. Petersen 
2127027ad72SMartin K. Petersen 	if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
2137027ad72SMartin K. Petersen 		cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp_mask);
2147c283341SChristoph Hellwig 		if (!cmd->prot_sdb)
2157c283341SChristoph Hellwig 			goto fail_free_sense;
2167027ad72SMartin K. Petersen 	}
2177027ad72SMartin K. Petersen 
2187027ad72SMartin K. Petersen 	return cmd;
2197c283341SChristoph Hellwig 
2207c283341SChristoph Hellwig fail_free_sense:
2217c283341SChristoph Hellwig 	kmem_cache_free(pool->sense_slab, cmd->sense_buffer);
2227c283341SChristoph Hellwig fail_free_cmd:
2237c283341SChristoph Hellwig 	kmem_cache_free(pool->cmd_slab, cmd);
2247c283341SChristoph Hellwig fail:
2257c283341SChristoph Hellwig 	return NULL;
2267027ad72SMartin K. Petersen }
2277027ad72SMartin K. Petersen 
2287027ad72SMartin K. Petersen /**
229eb44820cSRob Landley  * __scsi_get_command - Allocate a struct scsi_cmnd
230eb44820cSRob Landley  * @shost: host to transmit command
231eb44820cSRob Landley  * @gfp_mask: allocation mask
232eb44820cSRob Landley  *
233eb44820cSRob Landley  * Description: allocate a struct scsi_cmd from host's slab, recycling from the
234eb44820cSRob Landley  *              host's free_list if necessary.
235eb44820cSRob Landley  */
236f1bea55dSChristoph Hellwig static struct scsi_cmnd *
237f1bea55dSChristoph Hellwig __scsi_get_command(struct Scsi_Host *shost, gfp_t gfp_mask)
2381da177e4SLinus Torvalds {
239b4c2554dSMartin K. Petersen 	struct scsi_cmnd *cmd = scsi_host_alloc_command(shost, gfp_mask);
240164fc5dcSHugh Dickins 
2411da177e4SLinus Torvalds 	if (unlikely(!cmd)) {
2421da177e4SLinus Torvalds 		unsigned long flags;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 		spin_lock_irqsave(&shost->free_list_lock, flags);
2451da177e4SLinus Torvalds 		if (likely(!list_empty(&shost->free_list))) {
2461da177e4SLinus Torvalds 			cmd = list_entry(shost->free_list.next,
2471da177e4SLinus Torvalds 					 struct scsi_cmnd, list);
2481da177e4SLinus Torvalds 			list_del_init(&cmd->list);
2491da177e4SLinus Torvalds 		}
2501da177e4SLinus Torvalds 		spin_unlock_irqrestore(&shost->free_list_lock, flags);
251de25deb1SFUJITA Tomonori 
252de25deb1SFUJITA Tomonori 		if (cmd) {
253b4c2554dSMartin K. Petersen 			void *buf, *prot;
254b4c2554dSMartin K. Petersen 
255de25deb1SFUJITA Tomonori 			buf = cmd->sense_buffer;
256b4c2554dSMartin K. Petersen 			prot = cmd->prot_sdb;
257b4c2554dSMartin K. Petersen 
258de25deb1SFUJITA Tomonori 			memset(cmd, 0, sizeof(*cmd));
259b4c2554dSMartin K. Petersen 
260de25deb1SFUJITA Tomonori 			cmd->sense_buffer = buf;
261b4c2554dSMartin K. Petersen 			cmd->prot_sdb = prot;
262de25deb1SFUJITA Tomonori 		}
2631da177e4SLinus Torvalds 	}
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	return cmd;
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
268eb44820cSRob Landley /**
269eb44820cSRob Landley  * scsi_get_command - Allocate and setup a scsi command block
270eb44820cSRob Landley  * @dev: parent scsi device
271eb44820cSRob Landley  * @gfp_mask: allocator flags
2721da177e4SLinus Torvalds  *
2731da177e4SLinus Torvalds  * Returns:	The allocated scsi command structure.
2741da177e4SLinus Torvalds  */
275c53033f6SAl Viro struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask)
2761da177e4SLinus Torvalds {
27704796336SChristoph Hellwig 	struct scsi_cmnd *cmd = __scsi_get_command(dev->host, gfp_mask);
2781da177e4SLinus Torvalds 	unsigned long flags;
2791da177e4SLinus Torvalds 
28004796336SChristoph Hellwig 	if (unlikely(cmd == NULL))
28104796336SChristoph Hellwig 		return NULL;
28204796336SChristoph Hellwig 
2831da177e4SLinus Torvalds 	cmd->device = dev;
2841da177e4SLinus Torvalds 	INIT_LIST_HEAD(&cmd->list);
285e494f6a7SHannes Reinecke 	INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
2861da177e4SLinus Torvalds 	spin_lock_irqsave(&dev->list_lock, flags);
2871da177e4SLinus Torvalds 	list_add_tail(&cmd->list, &dev->cmd_list);
2881da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dev->list_lock, flags);
28979e448bfSMatthew Dobson 	cmd->jiffies_at_alloc = jiffies;
2901da177e4SLinus Torvalds 	return cmd;
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
293eb44820cSRob Landley /**
294eb44820cSRob Landley  * __scsi_put_command - Free a struct scsi_cmnd
295eb44820cSRob Landley  * @shost: dev->host
296eb44820cSRob Landley  * @cmd: Command to free
297eb44820cSRob Landley  */
298f1bea55dSChristoph Hellwig static void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
299b58d9154SFUJITA Tomonori {
300b58d9154SFUJITA Tomonori 	unsigned long flags;
301b58d9154SFUJITA Tomonori 
302b58d9154SFUJITA Tomonori 	if (unlikely(list_empty(&shost->free_list))) {
30344b93b59SChristoph Hellwig 		spin_lock_irqsave(&shost->free_list_lock, flags);
30444b93b59SChristoph Hellwig 		if (list_empty(&shost->free_list)) {
305b58d9154SFUJITA Tomonori 			list_add(&cmd->list, &shost->free_list);
306b58d9154SFUJITA Tomonori 			cmd = NULL;
307b58d9154SFUJITA Tomonori 		}
308b58d9154SFUJITA Tomonori 		spin_unlock_irqrestore(&shost->free_list_lock, flags);
30944b93b59SChristoph Hellwig 	}
310b58d9154SFUJITA Tomonori 
311e507e30bSJames Bottomley 	if (likely(cmd != NULL))
3127c283341SChristoph Hellwig 		scsi_host_free_command(shost, cmd);
313b58d9154SFUJITA Tomonori }
314b58d9154SFUJITA Tomonori 
315eb44820cSRob Landley /**
316eb44820cSRob Landley  * scsi_put_command - Free a scsi command block
317eb44820cSRob Landley  * @cmd: command block to free
3181da177e4SLinus Torvalds  *
3191da177e4SLinus Torvalds  * Returns:	Nothing.
3201da177e4SLinus Torvalds  *
3211da177e4SLinus Torvalds  * Notes:	The command must not belong to any lists.
3221da177e4SLinus Torvalds  */
3231da177e4SLinus Torvalds void scsi_put_command(struct scsi_cmnd *cmd)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	unsigned long flags;
3261da177e4SLinus Torvalds 
3271da177e4SLinus Torvalds 	/* serious error if the command hasn't come from a device list */
3281da177e4SLinus Torvalds 	spin_lock_irqsave(&cmd->device->list_lock, flags);
3291da177e4SLinus Torvalds 	BUG_ON(list_empty(&cmd->list));
3301da177e4SLinus Torvalds 	list_del_init(&cmd->list);
331b58d9154SFUJITA Tomonori 	spin_unlock_irqrestore(&cmd->device->list_lock, flags);
3321da177e4SLinus Torvalds 
333fcc95a76SBart Van Assche 	BUG_ON(delayed_work_pending(&cmd->abort_work));
334e494f6a7SHannes Reinecke 
33504796336SChristoph Hellwig 	__scsi_put_command(cmd->device->host, cmd);
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
33889d9a567SChristoph Hellwig static struct scsi_host_cmd_pool *
33989d9a567SChristoph Hellwig scsi_find_host_cmd_pool(struct Scsi_Host *shost)
3401da177e4SLinus Torvalds {
34189d9a567SChristoph Hellwig 	if (shost->hostt->cmd_size)
34289d9a567SChristoph Hellwig 		return shost->hostt->cmd_pool;
34389d9a567SChristoph Hellwig 	if (shost->unchecked_isa_dma)
34489d9a567SChristoph Hellwig 		return &scsi_cmd_dma_pool;
34589d9a567SChristoph Hellwig 	return &scsi_cmd_pool;
34689d9a567SChristoph Hellwig }
34789d9a567SChristoph Hellwig 
34889d9a567SChristoph Hellwig static void
34989d9a567SChristoph Hellwig scsi_free_host_cmd_pool(struct scsi_host_cmd_pool *pool)
35089d9a567SChristoph Hellwig {
35189d9a567SChristoph Hellwig 	kfree(pool->sense_name);
35289d9a567SChristoph Hellwig 	kfree(pool->cmd_name);
35389d9a567SChristoph Hellwig 	kfree(pool);
35489d9a567SChristoph Hellwig }
35589d9a567SChristoph Hellwig 
35689d9a567SChristoph Hellwig static struct scsi_host_cmd_pool *
35789d9a567SChristoph Hellwig scsi_alloc_host_cmd_pool(struct Scsi_Host *shost)
35889d9a567SChristoph Hellwig {
35989d9a567SChristoph Hellwig 	struct scsi_host_template *hostt = shost->hostt;
36089d9a567SChristoph Hellwig 	struct scsi_host_cmd_pool *pool;
36189d9a567SChristoph Hellwig 
36289d9a567SChristoph Hellwig 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
36389d9a567SChristoph Hellwig 	if (!pool)
36489d9a567SChristoph Hellwig 		return NULL;
36589d9a567SChristoph Hellwig 
36689d9a567SChristoph Hellwig 	pool->cmd_name = kasprintf(GFP_KERNEL, "%s_cmd", hostt->name);
36789d9a567SChristoph Hellwig 	pool->sense_name = kasprintf(GFP_KERNEL, "%s_sense", hostt->name);
36889d9a567SChristoph Hellwig 	if (!pool->cmd_name || !pool->sense_name) {
36989d9a567SChristoph Hellwig 		scsi_free_host_cmd_pool(pool);
37089d9a567SChristoph Hellwig 		return NULL;
37189d9a567SChristoph Hellwig 	}
37289d9a567SChristoph Hellwig 
37389d9a567SChristoph Hellwig 	pool->slab_flags = SLAB_HWCACHE_ALIGN;
37489d9a567SChristoph Hellwig 	if (shost->unchecked_isa_dma) {
37589d9a567SChristoph Hellwig 		pool->slab_flags |= SLAB_CACHE_DMA;
37689d9a567SChristoph Hellwig 		pool->gfp_mask = __GFP_DMA;
37789d9a567SChristoph Hellwig 	}
37889d9a567SChristoph Hellwig 	return pool;
37989d9a567SChristoph Hellwig }
38089d9a567SChristoph Hellwig 
38189d9a567SChristoph Hellwig static struct scsi_host_cmd_pool *
38289d9a567SChristoph Hellwig scsi_get_host_cmd_pool(struct Scsi_Host *shost)
38389d9a567SChristoph Hellwig {
38489d9a567SChristoph Hellwig 	struct scsi_host_template *hostt = shost->hostt;
3851c353f7dSJames Bottomley 	struct scsi_host_cmd_pool *retval = NULL, *pool;
38689d9a567SChristoph Hellwig 	size_t cmd_size = sizeof(struct scsi_cmnd) + hostt->cmd_size;
38789d9a567SChristoph Hellwig 
3881da177e4SLinus Torvalds 	/*
3891da177e4SLinus Torvalds 	 * Select a command slab for this host and create it if not
390eb44820cSRob Landley 	 * yet existent.
3911da177e4SLinus Torvalds 	 */
3920b950672SArjan van de Ven 	mutex_lock(&host_cmd_pool_mutex);
39389d9a567SChristoph Hellwig 	pool = scsi_find_host_cmd_pool(shost);
39489d9a567SChristoph Hellwig 	if (!pool) {
39589d9a567SChristoph Hellwig 		pool = scsi_alloc_host_cmd_pool(shost);
39689d9a567SChristoph Hellwig 		if (!pool)
39789d9a567SChristoph Hellwig 			goto out;
39889d9a567SChristoph Hellwig 	}
39989d9a567SChristoph Hellwig 
4001da177e4SLinus Torvalds 	if (!pool->users) {
40189d9a567SChristoph Hellwig 		pool->cmd_slab = kmem_cache_create(pool->cmd_name, cmd_size, 0,
40220c2df83SPaul Mundt 						   pool->slab_flags, NULL);
4035b7f1680SJames Bottomley 		if (!pool->cmd_slab)
40489d9a567SChristoph Hellwig 			goto out_free_pool;
4055b7f1680SJames Bottomley 
4065b7f1680SJames Bottomley 		pool->sense_slab = kmem_cache_create(pool->sense_name,
4075b7f1680SJames Bottomley 						     SCSI_SENSE_BUFFERSIZE, 0,
4085b7f1680SJames Bottomley 						     pool->slab_flags, NULL);
40989d9a567SChristoph Hellwig 		if (!pool->sense_slab)
41089d9a567SChristoph Hellwig 			goto out_free_slab;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	pool->users++;
4141c353f7dSJames Bottomley 	retval = pool;
41589d9a567SChristoph Hellwig out:
4160b950672SArjan van de Ven 	mutex_unlock(&host_cmd_pool_mutex);
4171c353f7dSJames Bottomley 	return retval;
41889d9a567SChristoph Hellwig 
41989d9a567SChristoph Hellwig out_free_slab:
42089d9a567SChristoph Hellwig 	kmem_cache_destroy(pool->cmd_slab);
42189d9a567SChristoph Hellwig out_free_pool:
42289d9a567SChristoph Hellwig 	if (hostt->cmd_size)
42389d9a567SChristoph Hellwig 		scsi_free_host_cmd_pool(pool);
42489d9a567SChristoph Hellwig 	goto out;
4251c353f7dSJames Bottomley }
4261da177e4SLinus Torvalds 
42789d9a567SChristoph Hellwig static void scsi_put_host_cmd_pool(struct Scsi_Host *shost)
4281c353f7dSJames Bottomley {
42989d9a567SChristoph Hellwig 	struct scsi_host_template *hostt = shost->hostt;
4301c353f7dSJames Bottomley 	struct scsi_host_cmd_pool *pool;
431de25deb1SFUJITA Tomonori 
432166a7287SFUJITA Tomonori 	mutex_lock(&host_cmd_pool_mutex);
43389d9a567SChristoph Hellwig 	pool = scsi_find_host_cmd_pool(shost);
43489d9a567SChristoph Hellwig 
4351c353f7dSJames Bottomley 	/*
4361c353f7dSJames Bottomley 	 * This may happen if a driver has a mismatched get and put
4371c353f7dSJames Bottomley 	 * of the command pool; the driver should be implicated in
4381c353f7dSJames Bottomley 	 * the stack trace
4391c353f7dSJames Bottomley 	 */
4401c353f7dSJames Bottomley 	BUG_ON(pool->users == 0);
4411c353f7dSJames Bottomley 
4425b7f1680SJames Bottomley 	if (!--pool->users) {
4435b7f1680SJames Bottomley 		kmem_cache_destroy(pool->cmd_slab);
4445b7f1680SJames Bottomley 		kmem_cache_destroy(pool->sense_slab);
44589d9a567SChristoph Hellwig 		if (hostt->cmd_size)
44689d9a567SChristoph Hellwig 			scsi_free_host_cmd_pool(pool);
4475b7f1680SJames Bottomley 	}
4480b950672SArjan van de Ven 	mutex_unlock(&host_cmd_pool_mutex);
4491c353f7dSJames Bottomley }
4501c353f7dSJames Bottomley 
4511c353f7dSJames Bottomley /**
4521c353f7dSJames Bottomley  * scsi_setup_command_freelist - Setup the command freelist for a scsi host.
4531c353f7dSJames Bottomley  * @shost: host to allocate the freelist for.
4541c353f7dSJames Bottomley  *
4551c353f7dSJames Bottomley  * Description: The command freelist protects against system-wide out of memory
4561c353f7dSJames Bottomley  * deadlock by preallocating one SCSI command structure for each host, so the
4571c353f7dSJames Bottomley  * system can always write to a swap file on a device associated with that host.
4581c353f7dSJames Bottomley  *
4591c353f7dSJames Bottomley  * Returns:	Nothing.
4601c353f7dSJames Bottomley  */
4611c353f7dSJames Bottomley int scsi_setup_command_freelist(struct Scsi_Host *shost)
4621c353f7dSJames Bottomley {
4631c353f7dSJames Bottomley 	const gfp_t gfp_mask = shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL;
46489d9a567SChristoph Hellwig 	struct scsi_cmnd *cmd;
4651c353f7dSJames Bottomley 
4661c353f7dSJames Bottomley 	spin_lock_init(&shost->free_list_lock);
4671c353f7dSJames Bottomley 	INIT_LIST_HEAD(&shost->free_list);
4681c353f7dSJames Bottomley 
46989d9a567SChristoph Hellwig 	shost->cmd_pool = scsi_get_host_cmd_pool(shost);
4701c353f7dSJames Bottomley 	if (!shost->cmd_pool)
4711da177e4SLinus Torvalds 		return -ENOMEM;
4721c353f7dSJames Bottomley 
4731c353f7dSJames Bottomley 	/*
4741c353f7dSJames Bottomley 	 * Get one backup command for this host.
4751c353f7dSJames Bottomley 	 */
4767027ad72SMartin K. Petersen 	cmd = scsi_host_alloc_command(shost, gfp_mask);
4771c353f7dSJames Bottomley 	if (!cmd) {
47889d9a567SChristoph Hellwig 		scsi_put_host_cmd_pool(shost);
47961d7416aSAlan D. Brunelle 		shost->cmd_pool = NULL;
4801c353f7dSJames Bottomley 		return -ENOMEM;
4811c353f7dSJames Bottomley 	}
4821c353f7dSJames Bottomley 	list_add(&cmd->list, &shost->free_list);
4831c353f7dSJames Bottomley 	return 0;
4841da177e4SLinus Torvalds }
4851da177e4SLinus Torvalds 
486eb44820cSRob Landley /**
487eb44820cSRob Landley  * scsi_destroy_command_freelist - Release the command freelist for a scsi host.
488eb44820cSRob Landley  * @shost: host whose freelist is going to be destroyed
4891da177e4SLinus Torvalds  */
4901da177e4SLinus Torvalds void scsi_destroy_command_freelist(struct Scsi_Host *shost)
4911da177e4SLinus Torvalds {
49261d7416aSAlan D. Brunelle 	/*
49361d7416aSAlan D. Brunelle 	 * If cmd_pool is NULL the free list was not initialized, so
49461d7416aSAlan D. Brunelle 	 * do not attempt to release resources.
49561d7416aSAlan D. Brunelle 	 */
49661d7416aSAlan D. Brunelle 	if (!shost->cmd_pool)
49761d7416aSAlan D. Brunelle 		return;
49861d7416aSAlan D. Brunelle 
4991da177e4SLinus Torvalds 	while (!list_empty(&shost->free_list)) {
5001da177e4SLinus Torvalds 		struct scsi_cmnd *cmd;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 		cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
5031da177e4SLinus Torvalds 		list_del_init(&cmd->list);
5047c283341SChristoph Hellwig 		scsi_host_free_command(shost, cmd);
5051da177e4SLinus Torvalds 	}
5061c353f7dSJames Bottomley 	shost->cmd_pool = NULL;
50789d9a567SChristoph Hellwig 	scsi_put_host_cmd_pool(shost);
508de25deb1SFUJITA Tomonori }
509de25deb1SFUJITA Tomonori 
5101da177e4SLinus Torvalds #ifdef CONFIG_SCSI_LOGGING
5111da177e4SLinus Torvalds void scsi_log_send(struct scsi_cmnd *cmd)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	unsigned int level;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	/*
5161da177e4SLinus Torvalds 	 * If ML QUEUE log level is greater than or equal to:
5171da177e4SLinus Torvalds 	 *
5181da177e4SLinus Torvalds 	 * 1: nothing (match completion)
5191da177e4SLinus Torvalds 	 *
5201da177e4SLinus Torvalds 	 * 2: log opcode + command of all commands
5211da177e4SLinus Torvalds 	 *
5221da177e4SLinus Torvalds 	 * 3: same as 2 plus dump cmd address
5231da177e4SLinus Torvalds 	 *
5241da177e4SLinus Torvalds 	 * 4: same as 3 plus dump extra junk
5251da177e4SLinus Torvalds 	 */
5261da177e4SLinus Torvalds 	if (unlikely(scsi_logging_level)) {
5271da177e4SLinus Torvalds 		level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
5281da177e4SLinus Torvalds 				       SCSI_LOG_MLQUEUE_BITS);
5291da177e4SLinus Torvalds 		if (level > 1) {
530a4d04a4cSMartin K. Petersen 			scmd_printk(KERN_INFO, cmd, "Send: ");
5311da177e4SLinus Torvalds 			if (level > 2)
5321da177e4SLinus Torvalds 				printk("0x%p ", cmd);
533a4d04a4cSMartin K. Petersen 			printk("\n");
5341da177e4SLinus Torvalds 			scsi_print_command(cmd);
5351da177e4SLinus Torvalds 			if (level > 3) {
5361da177e4SLinus Torvalds 				printk(KERN_INFO "buffer = 0x%p, bufflen = %d,"
5377b3d9545SLinus Torvalds 				       " queuecommand 0x%p\n",
538a73e45b3SBoaz Harrosh 					scsi_sglist(cmd), scsi_bufflen(cmd),
539a4d04a4cSMartin K. Petersen 					cmd->device->host->hostt->queuecommand);
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds 			}
5421da177e4SLinus Torvalds 		}
5431da177e4SLinus Torvalds 	}
5441da177e4SLinus Torvalds }
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
5471da177e4SLinus Torvalds {
5481da177e4SLinus Torvalds 	unsigned int level;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	/*
5511da177e4SLinus Torvalds 	 * If ML COMPLETE log level is greater than or equal to:
5521da177e4SLinus Torvalds 	 *
5531da177e4SLinus Torvalds 	 * 1: log disposition, result, opcode + command, and conditionally
5541da177e4SLinus Torvalds 	 * sense data for failures or non SUCCESS dispositions.
5551da177e4SLinus Torvalds 	 *
5561da177e4SLinus Torvalds 	 * 2: same as 1 but for all command completions.
5571da177e4SLinus Torvalds 	 *
5581da177e4SLinus Torvalds 	 * 3: same as 2 plus dump cmd address
5591da177e4SLinus Torvalds 	 *
5601da177e4SLinus Torvalds 	 * 4: same as 3 plus dump extra junk
5611da177e4SLinus Torvalds 	 */
5621da177e4SLinus Torvalds 	if (unlikely(scsi_logging_level)) {
5631da177e4SLinus Torvalds 		level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
5641da177e4SLinus Torvalds 				       SCSI_LOG_MLCOMPLETE_BITS);
5651da177e4SLinus Torvalds 		if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
5661da177e4SLinus Torvalds 		    (level > 1)) {
567a4d04a4cSMartin K. Petersen 			scmd_printk(KERN_INFO, cmd, "Done: ");
5681da177e4SLinus Torvalds 			if (level > 2)
5691da177e4SLinus Torvalds 				printk("0x%p ", cmd);
5701da177e4SLinus Torvalds 			/*
5711da177e4SLinus Torvalds 			 * Dump truncated values, so we usually fit within
5721da177e4SLinus Torvalds 			 * 80 chars.
5731da177e4SLinus Torvalds 			 */
5741da177e4SLinus Torvalds 			switch (disposition) {
5751da177e4SLinus Torvalds 			case SUCCESS:
576a4d04a4cSMartin K. Petersen 				printk("SUCCESS\n");
5771da177e4SLinus Torvalds 				break;
5781da177e4SLinus Torvalds 			case NEEDS_RETRY:
579a4d04a4cSMartin K. Petersen 				printk("RETRY\n");
5801da177e4SLinus Torvalds 				break;
5811da177e4SLinus Torvalds 			case ADD_TO_MLQUEUE:
582a4d04a4cSMartin K. Petersen 				printk("MLQUEUE\n");
5831da177e4SLinus Torvalds 				break;
5841da177e4SLinus Torvalds 			case FAILED:
585a4d04a4cSMartin K. Petersen 				printk("FAILED\n");
5861da177e4SLinus Torvalds 				break;
5871da177e4SLinus Torvalds 			case TIMEOUT_ERROR:
5881da177e4SLinus Torvalds 				/*
5891da177e4SLinus Torvalds 				 * If called via scsi_times_out.
5901da177e4SLinus Torvalds 				 */
591a4d04a4cSMartin K. Petersen 				printk("TIMEOUT\n");
5921da177e4SLinus Torvalds 				break;
5931da177e4SLinus Torvalds 			default:
594a4d04a4cSMartin K. Petersen 				printk("UNKNOWN\n");
5951da177e4SLinus Torvalds 			}
596a4d04a4cSMartin K. Petersen 			scsi_print_result(cmd);
5971da177e4SLinus Torvalds 			scsi_print_command(cmd);
598a4d04a4cSMartin K. Petersen 			if (status_byte(cmd->result) & CHECK_CONDITION)
5991da177e4SLinus Torvalds 				scsi_print_sense("", cmd);
600a4d04a4cSMartin K. Petersen 			if (level > 3)
601a4d04a4cSMartin K. Petersen 				scmd_printk(KERN_INFO, cmd,
602a4d04a4cSMartin K. Petersen 					    "scsi host busy %d failed %d\n",
60374665016SChristoph Hellwig 					    atomic_read(&cmd->device->host->host_busy),
604a4d04a4cSMartin K. Petersen 					    cmd->device->host->host_failed);
6051da177e4SLinus Torvalds 		}
6061da177e4SLinus Torvalds 	}
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds #endif
6091da177e4SLinus Torvalds 
610eb44820cSRob Landley /**
611eb44820cSRob Landley  * scsi_cmd_get_serial - Assign a serial number to a command
612eb44820cSRob Landley  * @host: the scsi host
613eb44820cSRob Landley  * @cmd: command to assign serial number to
614eb44820cSRob Landley  *
615eb44820cSRob Landley  * Description: a serial number identifies a request for error recovery
6161da177e4SLinus Torvalds  * and debugging purposes.  Protected by the Host_Lock of host.
6171da177e4SLinus Torvalds  */
618f281233dSJeff Garzik void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
6191da177e4SLinus Torvalds {
6201da177e4SLinus Torvalds 	cmd->serial_number = host->cmd_serial_number++;
6211da177e4SLinus Torvalds 	if (cmd->serial_number == 0)
6221da177e4SLinus Torvalds 		cmd->serial_number = host->cmd_serial_number++;
6231da177e4SLinus Torvalds }
624f281233dSJeff Garzik EXPORT_SYMBOL(scsi_cmd_get_serial);
6251da177e4SLinus Torvalds 
626eb44820cSRob Landley /**
627eb44820cSRob Landley  * scsi_dispatch_command - Dispatch a command to the low-level driver.
628eb44820cSRob Landley  * @cmd: command block we are dispatching.
6291da177e4SLinus Torvalds  *
630eb44820cSRob Landley  * Return: nonzero return request was rejected and device's queue needs to be
631eb44820cSRob Landley  * plugged.
6321da177e4SLinus Torvalds  */
6331da177e4SLinus Torvalds int scsi_dispatch_cmd(struct scsi_cmnd *cmd)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	struct Scsi_Host *host = cmd->device->host;
6361da177e4SLinus Torvalds 	int rtn = 0;
6371da177e4SLinus Torvalds 
638242f9dcbSJens Axboe 	atomic_inc(&cmd->device->iorequest_cnt);
639242f9dcbSJens Axboe 
6401da177e4SLinus Torvalds 	/* check if the device is still usable */
6411da177e4SLinus Torvalds 	if (unlikely(cmd->device->sdev_state == SDEV_DEL)) {
6421da177e4SLinus Torvalds 		/* in SDEV_DEL we error all commands. DID_NO_CONNECT
6431da177e4SLinus Torvalds 		 * returns an immediate error upwards, and signals
6441da177e4SLinus Torvalds 		 * that the device is no longer present */
6451da177e4SLinus Torvalds 		cmd->result = DID_NO_CONNECT << 16;
646d0d3bbf9SChristoph Hellwig 		goto done;
6471da177e4SLinus Torvalds 	}
6481da177e4SLinus Torvalds 
6490f1d87a2SJames Bottomley 	/* Check to see if the scsi lld made this device blocked. */
6500f1d87a2SJames Bottomley 	if (unlikely(scsi_device_blocked(cmd->device))) {
6511da177e4SLinus Torvalds 		/*
6520f1d87a2SJames Bottomley 		 * in blocked state, the command is just put back on
6530f1d87a2SJames Bottomley 		 * the device queue.  The suspend state has already
6540f1d87a2SJames Bottomley 		 * blocked the queue so future requests should not
6550f1d87a2SJames Bottomley 		 * occur until the device transitions out of the
6560f1d87a2SJames Bottomley 		 * suspend state.
6571da177e4SLinus Torvalds 		 */
65891921e01SHannes Reinecke 		SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
65991921e01SHannes Reinecke 			"queuecommand : device blocked\n"));
660d0d3bbf9SChristoph Hellwig 		return SCSI_MLQUEUE_DEVICE_BUSY;
6611da177e4SLinus Torvalds 	}
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	/*
6641da177e4SLinus Torvalds 	 * If SCSI-2 or lower, store the LUN value in cmnd.
6651da177e4SLinus Torvalds 	 */
6664d7db04aSJames Bottomley 	if (cmd->device->scsi_level <= SCSI_2 &&
6674d7db04aSJames Bottomley 	    cmd->device->scsi_level != SCSI_UNKNOWN) {
6681da177e4SLinus Torvalds 		cmd->cmnd[1] = (cmd->cmnd[1] & 0x1f) |
6691da177e4SLinus Torvalds 			       (cmd->device->lun << 5 & 0xe0);
6701da177e4SLinus Torvalds 	}
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	scsi_log_send(cmd);
6731da177e4SLinus Torvalds 
6741da177e4SLinus Torvalds 	/*
6751da177e4SLinus Torvalds 	 * Before we queue this command, check if the command
6761da177e4SLinus Torvalds 	 * length exceeds what the host adapter can handle.
6771da177e4SLinus Torvalds 	 */
678db4742ddSBoaz Harrosh 	if (cmd->cmd_len > cmd->device->host->max_cmd_len) {
67991921e01SHannes Reinecke 		SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
68091921e01SHannes Reinecke 			       "queuecommand : command too long. "
681db4742ddSBoaz Harrosh 			       "cdb_size=%d host->max_cmd_len=%d\n",
682db4742ddSBoaz Harrosh 			       cmd->cmd_len, cmd->device->host->max_cmd_len));
6831da177e4SLinus Torvalds 		cmd->result = (DID_ABORT << 16);
684d0d3bbf9SChristoph Hellwig 		goto done;
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 
687d2c9d9eaSMike Anderson 	if (unlikely(host->shost_state == SHOST_DEL)) {
6881da177e4SLinus Torvalds 		cmd->result = (DID_NO_CONNECT << 16);
689d0d3bbf9SChristoph Hellwig 		goto done;
690d0d3bbf9SChristoph Hellwig 
6911da177e4SLinus Torvalds 	}
692f281233dSJeff Garzik 
693d0d3bbf9SChristoph Hellwig 	trace_scsi_dispatch_cmd_start(cmd);
694d0d3bbf9SChristoph Hellwig 	rtn = host->hostt->queuecommand(host, cmd);
6951da177e4SLinus Torvalds 	if (rtn) {
696bf816235SKei Tokunaga 		trace_scsi_dispatch_cmd_error(cmd, rtn);
697f0c0a376SMike Christie 		if (rtn != SCSI_MLQUEUE_DEVICE_BUSY &&
698f0c0a376SMike Christie 		    rtn != SCSI_MLQUEUE_TARGET_BUSY)
699f0c0a376SMike Christie 			rtn = SCSI_MLQUEUE_HOST_BUSY;
700f0c0a376SMike Christie 
70191921e01SHannes Reinecke 		SCSI_LOG_MLQUEUE(3, scmd_printk(KERN_INFO, cmd,
70291921e01SHannes Reinecke 			"queuecommand : request rejected\n"));
7031da177e4SLinus Torvalds 	}
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	return rtn;
706d0d3bbf9SChristoph Hellwig  done:
7073b5382c4SChristoph Hellwig 	cmd->scsi_done(cmd);
708d0d3bbf9SChristoph Hellwig 	return 0;
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds /**
712eb44820cSRob Landley  * scsi_finish_command - cleanup and pass command back to upper layer
713eb44820cSRob Landley  * @cmd: the command
7141da177e4SLinus Torvalds  *
715eb44820cSRob Landley  * Description: Pass command off to upper layer for finishing of I/O
7161da177e4SLinus Torvalds  *              request, waking processes that are waiting on results,
7171da177e4SLinus Torvalds  *              etc.
7181da177e4SLinus Torvalds  */
7191da177e4SLinus Torvalds void scsi_finish_command(struct scsi_cmnd *cmd)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	struct scsi_device *sdev = cmd->device;
722f0c0a376SMike Christie 	struct scsi_target *starget = scsi_target(sdev);
7231da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
7247b3d9545SLinus Torvalds 	struct scsi_driver *drv;
7257b3d9545SLinus Torvalds 	unsigned int good_bytes;
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	scsi_device_unbusy(sdev);
7281da177e4SLinus Torvalds 
7291da177e4SLinus Torvalds 	/*
730cd9070c9SChristoph Hellwig 	 * Clear the flags that say that the device/target/host is no longer
731cd9070c9SChristoph Hellwig 	 * capable of accepting new commands.
7321da177e4SLinus Torvalds 	 */
733cd9070c9SChristoph Hellwig 	if (atomic_read(&shost->host_blocked))
734cd9070c9SChristoph Hellwig 		atomic_set(&shost->host_blocked, 0);
735cd9070c9SChristoph Hellwig 	if (atomic_read(&starget->target_blocked))
736cd9070c9SChristoph Hellwig 		atomic_set(&starget->target_blocked, 0);
737cd9070c9SChristoph Hellwig 	if (atomic_read(&sdev->device_blocked))
738cd9070c9SChristoph Hellwig 		atomic_set(&sdev->device_blocked, 0);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	/*
7411da177e4SLinus Torvalds 	 * If we have valid sense information, then some kind of recovery
7421da177e4SLinus Torvalds 	 * must have taken place.  Make a note of this.
7431da177e4SLinus Torvalds 	 */
7441da177e4SLinus Torvalds 	if (SCSI_SENSE_VALID(cmd))
7451da177e4SLinus Torvalds 		cmd->result |= (DRIVER_SENSE << 24);
7461da177e4SLinus Torvalds 
7473bf743e7SJeff Garzik 	SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
7483bf743e7SJeff Garzik 				"Notifying upper driver of completion "
7493bf743e7SJeff Garzik 				"(result %x)\n", cmd->result));
7501da177e4SLinus Torvalds 
751f18573abSFUJITA Tomonori 	good_bytes = scsi_bufflen(cmd);
7527b3d9545SLinus Torvalds         if (cmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
753427e59f0SJames Bottomley 		int old_good_bytes = good_bytes;
7547b3d9545SLinus Torvalds 		drv = scsi_cmd_to_driver(cmd);
7557b3d9545SLinus Torvalds 		if (drv->done)
7567b3d9545SLinus Torvalds 			good_bytes = drv->done(cmd);
757427e59f0SJames Bottomley 		/*
758427e59f0SJames Bottomley 		 * USB may not give sense identifying bad sector and
759427e59f0SJames Bottomley 		 * simply return a residue instead, so subtract off the
760427e59f0SJames Bottomley 		 * residue if drv->done() error processing indicates no
761427e59f0SJames Bottomley 		 * change to the completion length.
762427e59f0SJames Bottomley 		 */
763427e59f0SJames Bottomley 		if (good_bytes == old_good_bytes)
764427e59f0SJames Bottomley 			good_bytes -= scsi_get_resid(cmd);
7657b3d9545SLinus Torvalds 	}
7667b3d9545SLinus Torvalds 	scsi_io_completion(cmd, good_bytes);
7671da177e4SLinus Torvalds }
7681da177e4SLinus Torvalds 
769eb44820cSRob Landley /**
770eb44820cSRob Landley  * scsi_adjust_queue_depth - Let low level drivers change a device's queue depth
771eb44820cSRob Landley  * @sdev: SCSI Device in question
772eb44820cSRob Landley  * @tagged: Do we use tagged queueing (non-0) or do we treat
7731da177e4SLinus Torvalds  *          this device as an untagged device (0)
774eb44820cSRob Landley  * @tags: Number of tags allowed if tagged queueing enabled,
7751da177e4SLinus Torvalds  *        or number of commands the low level driver can
7761da177e4SLinus Torvalds  *        queue up in non-tagged mode (as per cmd_per_lun).
7771da177e4SLinus Torvalds  *
7781da177e4SLinus Torvalds  * Returns:	Nothing
7791da177e4SLinus Torvalds  *
7801da177e4SLinus Torvalds  * Lock Status:	None held on entry
7811da177e4SLinus Torvalds  *
7821da177e4SLinus Torvalds  * Notes:	Low level drivers may call this at any time and we will do
7831da177e4SLinus Torvalds  * 		the right thing depending on whether or not the device is
7841da177e4SLinus Torvalds  * 		currently active and whether or not it even has the
7851da177e4SLinus Torvalds  * 		command blocks built yet.
7861da177e4SLinus Torvalds  */
7871da177e4SLinus Torvalds void scsi_adjust_queue_depth(struct scsi_device *sdev, int tagged, int tags)
7881da177e4SLinus Torvalds {
7891da177e4SLinus Torvalds 	unsigned long flags;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	/*
7921da177e4SLinus Torvalds 	 * refuse to set tagged depth to an unworkable size
7931da177e4SLinus Torvalds 	 */
7941da177e4SLinus Torvalds 	if (tags <= 0)
7951da177e4SLinus Torvalds 		return;
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds 	spin_lock_irqsave(sdev->request_queue->queue_lock, flags);
7981da177e4SLinus Torvalds 
799885ace9eSMike Christie 	/*
800885ace9eSMike Christie 	 * Check to see if the queue is managed by the block layer.
801885ace9eSMike Christie 	 * If it is, and we fail to adjust the depth, exit.
802885ace9eSMike Christie 	 *
803885ace9eSMike Christie 	 * Do not resize the tag map if it is a host wide share bqt,
804885ace9eSMike Christie 	 * because the size should be the hosts's can_queue. If there
805885ace9eSMike Christie 	 * is more IO than the LLD's can_queue (so there are not enuogh
806885ace9eSMike Christie 	 * tags) request_fn's host queue ready check will handle it.
807885ace9eSMike Christie 	 */
808d285203cSChristoph Hellwig 	if (!shost_use_blk_mq(sdev->host) && !sdev->host->bqt) {
8091da177e4SLinus Torvalds 		if (blk_queue_tagged(sdev->request_queue) &&
8101da177e4SLinus Torvalds 		    blk_queue_resize_tags(sdev->request_queue, tags) != 0)
8111da177e4SLinus Torvalds 			goto out;
812885ace9eSMike Christie 	}
8131da177e4SLinus Torvalds 
8141da177e4SLinus Torvalds 	sdev->queue_depth = tags;
8151da177e4SLinus Torvalds 	switch (tagged) {
816cb23f912SDouglas Gilbert 		case 0:
817cb23f912SDouglas Gilbert 			sdev->ordered_tags = 0;
818cb23f912SDouglas Gilbert 			sdev->simple_tags = 0;
819cb23f912SDouglas Gilbert 			break;
8201da177e4SLinus Torvalds 		case MSG_ORDERED_TAG:
8211da177e4SLinus Torvalds 			sdev->ordered_tags = 1;
8221da177e4SLinus Torvalds 			sdev->simple_tags = 1;
8231da177e4SLinus Torvalds 			break;
8241da177e4SLinus Torvalds 		case MSG_SIMPLE_TAG:
8251da177e4SLinus Torvalds 			sdev->ordered_tags = 0;
8261da177e4SLinus Torvalds 			sdev->simple_tags = 1;
8271da177e4SLinus Torvalds 			break;
8281da177e4SLinus Torvalds 		default:
829cb23f912SDouglas Gilbert 			sdev->ordered_tags = 0;
830cb23f912SDouglas Gilbert 			sdev->simple_tags = 0;
8319ccfc756SJames Bottomley 			sdev_printk(KERN_WARNING, sdev,
8321da177e4SLinus Torvalds 				    "scsi_adjust_queue_depth, bad queue type, "
8339ccfc756SJames Bottomley 				    "disabled\n");
8341da177e4SLinus Torvalds 	}
8351da177e4SLinus Torvalds  out:
8361da177e4SLinus Torvalds 	spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
8371da177e4SLinus Torvalds }
8381da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_adjust_queue_depth);
8391da177e4SLinus Torvalds 
840eb44820cSRob Landley /**
841eb44820cSRob Landley  * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
842eb44820cSRob Landley  * @sdev: SCSI Device in question
843eb44820cSRob Landley  * @depth: Current number of outstanding SCSI commands on this device,
844eb44820cSRob Landley  *         not counting the one returned as QUEUE_FULL.
8451da177e4SLinus Torvalds  *
846eb44820cSRob Landley  * Description:	This function will track successive QUEUE_FULL events on a
8471da177e4SLinus Torvalds  * 		specific SCSI device to determine if and when there is a
8481da177e4SLinus Torvalds  * 		need to adjust the queue depth on the device.
8491da177e4SLinus Torvalds  *
850eb44820cSRob Landley  * Returns:	0 - No change needed, >0 - Adjust queue depth to this new depth,
8511da177e4SLinus Torvalds  * 		-1 - Drop back to untagged operation using host->cmd_per_lun
8521da177e4SLinus Torvalds  * 			as the untagged command depth
8531da177e4SLinus Torvalds  *
8541da177e4SLinus Torvalds  * Lock Status:	None held on entry
8551da177e4SLinus Torvalds  *
8561da177e4SLinus Torvalds  * Notes:	Low level drivers may call this at any time and we will do
8571da177e4SLinus Torvalds  * 		"The Right Thing."  We are interrupt context safe.
8581da177e4SLinus Torvalds  */
8591da177e4SLinus Torvalds int scsi_track_queue_full(struct scsi_device *sdev, int depth)
8601da177e4SLinus Torvalds {
8614a84067dSVasu Dev 
8624a84067dSVasu Dev 	/*
8634a84067dSVasu Dev 	 * Don't let QUEUE_FULLs on the same
8644a84067dSVasu Dev 	 * jiffies count, they could all be from
8654a84067dSVasu Dev 	 * same event.
8664a84067dSVasu Dev 	 */
8674a84067dSVasu Dev 	if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4))
8681da177e4SLinus Torvalds 		return 0;
8691da177e4SLinus Torvalds 
8704a84067dSVasu Dev 	sdev->last_queue_full_time = jiffies;
8711da177e4SLinus Torvalds 	if (sdev->last_queue_full_depth != depth) {
8721da177e4SLinus Torvalds 		sdev->last_queue_full_count = 1;
8731da177e4SLinus Torvalds 		sdev->last_queue_full_depth = depth;
8741da177e4SLinus Torvalds 	} else {
8751da177e4SLinus Torvalds 		sdev->last_queue_full_count++;
8761da177e4SLinus Torvalds 	}
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	if (sdev->last_queue_full_count <= 10)
8791da177e4SLinus Torvalds 		return 0;
8801da177e4SLinus Torvalds 	if (sdev->last_queue_full_depth < 8) {
8811da177e4SLinus Torvalds 		/* Drop back to untagged */
8821da177e4SLinus Torvalds 		scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
8831da177e4SLinus Torvalds 		return -1;
8841da177e4SLinus Torvalds 	}
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	if (sdev->ordered_tags)
8871da177e4SLinus Torvalds 		scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG, depth);
8881da177e4SLinus Torvalds 	else
8891da177e4SLinus Torvalds 		scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
8901da177e4SLinus Torvalds 	return depth;
8911da177e4SLinus Torvalds }
8921da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_track_queue_full);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds /**
895881a256dSMatthew Wilcox  * scsi_vpd_inquiry - Request a device provide us with a VPD page
896881a256dSMatthew Wilcox  * @sdev: The device to ask
897881a256dSMatthew Wilcox  * @buffer: Where to put the result
898881a256dSMatthew Wilcox  * @page: Which Vital Product Data to return
899881a256dSMatthew Wilcox  * @len: The length of the buffer
900881a256dSMatthew Wilcox  *
901881a256dSMatthew Wilcox  * This is an internal helper function.  You probably want to use
902881a256dSMatthew Wilcox  * scsi_get_vpd_page instead.
903881a256dSMatthew Wilcox  *
904bc8945dfSHannes Reinecke  * Returns size of the vpd page on success or a negative error number.
905881a256dSMatthew Wilcox  */
906881a256dSMatthew Wilcox static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer,
907881a256dSMatthew Wilcox 							u8 page, unsigned len)
908881a256dSMatthew Wilcox {
909881a256dSMatthew Wilcox 	int result;
910881a256dSMatthew Wilcox 	unsigned char cmd[16];
911881a256dSMatthew Wilcox 
912bc8945dfSHannes Reinecke 	if (len < 4)
913bc8945dfSHannes Reinecke 		return -EINVAL;
914bc8945dfSHannes Reinecke 
915881a256dSMatthew Wilcox 	cmd[0] = INQUIRY;
916881a256dSMatthew Wilcox 	cmd[1] = 1;		/* EVPD */
917881a256dSMatthew Wilcox 	cmd[2] = page;
918881a256dSMatthew Wilcox 	cmd[3] = len >> 8;
919881a256dSMatthew Wilcox 	cmd[4] = len & 0xff;
920881a256dSMatthew Wilcox 	cmd[5] = 0;		/* Control byte */
921881a256dSMatthew Wilcox 
922881a256dSMatthew Wilcox 	/*
923881a256dSMatthew Wilcox 	 * I'm not convinced we need to try quite this hard to get VPD, but
924881a256dSMatthew Wilcox 	 * all the existing users tried this hard.
925881a256dSMatthew Wilcox 	 */
926881a256dSMatthew Wilcox 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer,
92795a3639eSJames Bottomley 				  len, NULL, 30 * HZ, 3, NULL);
928881a256dSMatthew Wilcox 	if (result)
929bc8945dfSHannes Reinecke 		return -EIO;
930881a256dSMatthew Wilcox 
931881a256dSMatthew Wilcox 	/* Sanity check that we got the page back that we asked for */
932881a256dSMatthew Wilcox 	if (buffer[1] != page)
933881a256dSMatthew Wilcox 		return -EIO;
934881a256dSMatthew Wilcox 
935bc8945dfSHannes Reinecke 	return get_unaligned_be16(&buffer[2]) + 4;
936881a256dSMatthew Wilcox }
937881a256dSMatthew Wilcox 
938881a256dSMatthew Wilcox /**
939881a256dSMatthew Wilcox  * scsi_get_vpd_page - Get Vital Product Data from a SCSI device
940881a256dSMatthew Wilcox  * @sdev: The device to ask
941881a256dSMatthew Wilcox  * @page: Which Vital Product Data to return
942786f8ba2SRandy Dunlap  * @buf: where to store the VPD
943786f8ba2SRandy Dunlap  * @buf_len: number of bytes in the VPD buffer area
944881a256dSMatthew Wilcox  *
945881a256dSMatthew Wilcox  * SCSI devices may optionally supply Vital Product Data.  Each 'page'
946881a256dSMatthew Wilcox  * of VPD is defined in the appropriate SCSI document (eg SPC, SBC).
947881a256dSMatthew Wilcox  * If the device supports this VPD page, this routine returns a pointer
948881a256dSMatthew Wilcox  * to a buffer containing the data from that page.  The caller is
949881a256dSMatthew Wilcox  * responsible for calling kfree() on this pointer when it is no longer
950881a256dSMatthew Wilcox  * needed.  If we cannot retrieve the VPD page this routine returns %NULL.
951881a256dSMatthew Wilcox  */
952e3deec09SJames Bottomley int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
953e3deec09SJames Bottomley 		      int buf_len)
954881a256dSMatthew Wilcox {
955881a256dSMatthew Wilcox 	int i, result;
956881a256dSMatthew Wilcox 
9577562523eSMartin K. Petersen 	if (sdev->skip_vpd_pages)
9587562523eSMartin K. Petersen 		goto fail;
9597562523eSMartin K. Petersen 
960881a256dSMatthew Wilcox 	/* Ask for all the pages supported by this device */
961e3deec09SJames Bottomley 	result = scsi_vpd_inquiry(sdev, buf, 0, buf_len);
962bc8945dfSHannes Reinecke 	if (result < 4)
963881a256dSMatthew Wilcox 		goto fail;
964881a256dSMatthew Wilcox 
965881a256dSMatthew Wilcox 	/* If the user actually wanted this page, we can skip the rest */
966881a256dSMatthew Wilcox 	if (page == 0)
96716d3ea26SMartin K. Petersen 		return 0;
968881a256dSMatthew Wilcox 
969bc8945dfSHannes Reinecke 	for (i = 4; i < min(result, buf_len); i++)
970bc8945dfSHannes Reinecke 		if (buf[i] == page)
971881a256dSMatthew Wilcox 			goto found;
972e3deec09SJames Bottomley 
973bc8945dfSHannes Reinecke 	if (i < result && i >= buf_len)
974e3deec09SJames Bottomley 		/* ran off the end of the buffer, give us benefit of doubt */
975e3deec09SJames Bottomley 		goto found;
976881a256dSMatthew Wilcox 	/* The device claims it doesn't support the requested page */
977881a256dSMatthew Wilcox 	goto fail;
978881a256dSMatthew Wilcox 
979881a256dSMatthew Wilcox  found:
980e3deec09SJames Bottomley 	result = scsi_vpd_inquiry(sdev, buf, page, buf_len);
981bc8945dfSHannes Reinecke 	if (result < 0)
982881a256dSMatthew Wilcox 		goto fail;
983881a256dSMatthew Wilcox 
984e3deec09SJames Bottomley 	return 0;
985881a256dSMatthew Wilcox 
986881a256dSMatthew Wilcox  fail:
987e3deec09SJames Bottomley 	return -EINVAL;
988881a256dSMatthew Wilcox }
989881a256dSMatthew Wilcox EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
990881a256dSMatthew Wilcox 
991881a256dSMatthew Wilcox /**
992b3ae8780SHannes Reinecke  * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure
993b3ae8780SHannes Reinecke  * @sdev: The device to ask
994b3ae8780SHannes Reinecke  *
995b3ae8780SHannes Reinecke  * Attach the 'Device Identification' VPD page (0x83) and the
996b3ae8780SHannes Reinecke  * 'Unit Serial Number' VPD page (0x80) to a SCSI device
997b3ae8780SHannes Reinecke  * structure. This information can be used to identify the device
998b3ae8780SHannes Reinecke  * uniquely.
999b3ae8780SHannes Reinecke  */
1000b3ae8780SHannes Reinecke void scsi_attach_vpd(struct scsi_device *sdev)
1001b3ae8780SHannes Reinecke {
1002b3ae8780SHannes Reinecke 	int result, i;
1003b3ae8780SHannes Reinecke 	int vpd_len = SCSI_VPD_PG_LEN;
1004b3ae8780SHannes Reinecke 	int pg80_supported = 0;
1005b3ae8780SHannes Reinecke 	int pg83_supported = 0;
1006b3ae8780SHannes Reinecke 	unsigned char *vpd_buf;
1007b3ae8780SHannes Reinecke 
1008b3ae8780SHannes Reinecke 	if (sdev->skip_vpd_pages)
1009b3ae8780SHannes Reinecke 		return;
1010b3ae8780SHannes Reinecke retry_pg0:
1011b3ae8780SHannes Reinecke 	vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
1012b3ae8780SHannes Reinecke 	if (!vpd_buf)
1013b3ae8780SHannes Reinecke 		return;
1014b3ae8780SHannes Reinecke 
1015b3ae8780SHannes Reinecke 	/* Ask for all the pages supported by this device */
1016b3ae8780SHannes Reinecke 	result = scsi_vpd_inquiry(sdev, vpd_buf, 0, vpd_len);
1017b3ae8780SHannes Reinecke 	if (result < 0) {
1018b3ae8780SHannes Reinecke 		kfree(vpd_buf);
1019b3ae8780SHannes Reinecke 		return;
1020b3ae8780SHannes Reinecke 	}
1021b3ae8780SHannes Reinecke 	if (result > vpd_len) {
1022b3ae8780SHannes Reinecke 		vpd_len = result;
1023b3ae8780SHannes Reinecke 		kfree(vpd_buf);
1024b3ae8780SHannes Reinecke 		goto retry_pg0;
1025b3ae8780SHannes Reinecke 	}
1026b3ae8780SHannes Reinecke 
1027b3ae8780SHannes Reinecke 	for (i = 4; i < result; i++) {
1028b3ae8780SHannes Reinecke 		if (vpd_buf[i] == 0x80)
1029b3ae8780SHannes Reinecke 			pg80_supported = 1;
1030b3ae8780SHannes Reinecke 		if (vpd_buf[i] == 0x83)
1031b3ae8780SHannes Reinecke 			pg83_supported = 1;
1032b3ae8780SHannes Reinecke 	}
1033b3ae8780SHannes Reinecke 	kfree(vpd_buf);
1034b3ae8780SHannes Reinecke 	vpd_len = SCSI_VPD_PG_LEN;
1035b3ae8780SHannes Reinecke 
1036b3ae8780SHannes Reinecke 	if (pg80_supported) {
1037b3ae8780SHannes Reinecke retry_pg80:
1038b3ae8780SHannes Reinecke 		vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
1039b3ae8780SHannes Reinecke 		if (!vpd_buf)
1040b3ae8780SHannes Reinecke 			return;
1041b3ae8780SHannes Reinecke 
1042b3ae8780SHannes Reinecke 		result = scsi_vpd_inquiry(sdev, vpd_buf, 0x80, vpd_len);
1043b3ae8780SHannes Reinecke 		if (result < 0) {
1044b3ae8780SHannes Reinecke 			kfree(vpd_buf);
1045b3ae8780SHannes Reinecke 			return;
1046b3ae8780SHannes Reinecke 		}
1047b3ae8780SHannes Reinecke 		if (result > vpd_len) {
1048b3ae8780SHannes Reinecke 			vpd_len = result;
1049b3ae8780SHannes Reinecke 			kfree(vpd_buf);
1050b3ae8780SHannes Reinecke 			goto retry_pg80;
1051b3ae8780SHannes Reinecke 		}
1052b3ae8780SHannes Reinecke 		sdev->vpd_pg80_len = result;
1053b3ae8780SHannes Reinecke 		sdev->vpd_pg80 = vpd_buf;
1054b3ae8780SHannes Reinecke 		vpd_len = SCSI_VPD_PG_LEN;
1055b3ae8780SHannes Reinecke 	}
1056b3ae8780SHannes Reinecke 
1057b3ae8780SHannes Reinecke 	if (pg83_supported) {
1058b3ae8780SHannes Reinecke retry_pg83:
1059b3ae8780SHannes Reinecke 		vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
1060b3ae8780SHannes Reinecke 		if (!vpd_buf)
1061b3ae8780SHannes Reinecke 			return;
1062b3ae8780SHannes Reinecke 
1063b3ae8780SHannes Reinecke 		result = scsi_vpd_inquiry(sdev, vpd_buf, 0x83, vpd_len);
1064b3ae8780SHannes Reinecke 		if (result < 0) {
1065b3ae8780SHannes Reinecke 			kfree(vpd_buf);
1066b3ae8780SHannes Reinecke 			return;
1067b3ae8780SHannes Reinecke 		}
1068b3ae8780SHannes Reinecke 		if (result > vpd_len) {
1069b3ae8780SHannes Reinecke 			vpd_len = result;
1070b3ae8780SHannes Reinecke 			kfree(vpd_buf);
1071b3ae8780SHannes Reinecke 			goto retry_pg83;
1072b3ae8780SHannes Reinecke 		}
1073b3ae8780SHannes Reinecke 		sdev->vpd_pg83_len = result;
1074b3ae8780SHannes Reinecke 		sdev->vpd_pg83 = vpd_buf;
1075b3ae8780SHannes Reinecke 	}
1076b3ae8780SHannes Reinecke }
1077b3ae8780SHannes Reinecke 
1078b3ae8780SHannes Reinecke /**
10793c6bdaeaSMartin K. Petersen  * scsi_report_opcode - Find out if a given command opcode is supported
10803c6bdaeaSMartin K. Petersen  * @sdev:	scsi device to query
10813c6bdaeaSMartin K. Petersen  * @buffer:	scratch buffer (must be at least 20 bytes long)
10823c6bdaeaSMartin K. Petersen  * @len:	length of buffer
10833c6bdaeaSMartin K. Petersen  * @opcode:	opcode for command to look up
10843c6bdaeaSMartin K. Petersen  *
10853c6bdaeaSMartin K. Petersen  * Uses the REPORT SUPPORTED OPERATION CODES to look up the given
108666c28f97SMartin K. Petersen  * opcode. Returns -EINVAL if RSOC fails, 0 if the command opcode is
108766c28f97SMartin K. Petersen  * unsupported and 1 if the device claims to support the command.
10883c6bdaeaSMartin K. Petersen  */
10893c6bdaeaSMartin K. Petersen int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
10903c6bdaeaSMartin K. Petersen 		       unsigned int len, unsigned char opcode)
10913c6bdaeaSMartin K. Petersen {
10923c6bdaeaSMartin K. Petersen 	unsigned char cmd[16];
10933c6bdaeaSMartin K. Petersen 	struct scsi_sense_hdr sshdr;
10943c6bdaeaSMartin K. Petersen 	int result;
10953c6bdaeaSMartin K. Petersen 
10963c6bdaeaSMartin K. Petersen 	if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3)
109766c28f97SMartin K. Petersen 		return -EINVAL;
10983c6bdaeaSMartin K. Petersen 
10993c6bdaeaSMartin K. Petersen 	memset(cmd, 0, 16);
11003c6bdaeaSMartin K. Petersen 	cmd[0] = MAINTENANCE_IN;
11013c6bdaeaSMartin K. Petersen 	cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;
11023c6bdaeaSMartin K. Petersen 	cmd[2] = 1;		/* One command format */
11033c6bdaeaSMartin K. Petersen 	cmd[3] = opcode;
11043c6bdaeaSMartin K. Petersen 	put_unaligned_be32(len, &cmd[6]);
11053c6bdaeaSMartin K. Petersen 	memset(buffer, 0, len);
11063c6bdaeaSMartin K. Petersen 
11073c6bdaeaSMartin K. Petersen 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
11083c6bdaeaSMartin K. Petersen 				  &sshdr, 30 * HZ, 3, NULL);
11093c6bdaeaSMartin K. Petersen 
11103c6bdaeaSMartin K. Petersen 	if (result && scsi_sense_valid(&sshdr) &&
11113c6bdaeaSMartin K. Petersen 	    sshdr.sense_key == ILLEGAL_REQUEST &&
11123c6bdaeaSMartin K. Petersen 	    (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00)
111366c28f97SMartin K. Petersen 		return -EINVAL;
11143c6bdaeaSMartin K. Petersen 
11153c6bdaeaSMartin K. Petersen 	if ((buffer[1] & 3) == 3) /* Command supported */
11163c6bdaeaSMartin K. Petersen 		return 1;
11173c6bdaeaSMartin K. Petersen 
11183c6bdaeaSMartin K. Petersen 	return 0;
11193c6bdaeaSMartin K. Petersen }
11203c6bdaeaSMartin K. Petersen EXPORT_SYMBOL(scsi_report_opcode);
11213c6bdaeaSMartin K. Petersen 
11223c6bdaeaSMartin K. Petersen /**
1123eb44820cSRob Landley  * scsi_device_get  -  get an additional reference to a scsi_device
11241da177e4SLinus Torvalds  * @sdev:	device to get a reference to
11251da177e4SLinus Torvalds  *
1126eb44820cSRob Landley  * Description: Gets a reference to the scsi_device and increments the use count
11271da177e4SLinus Torvalds  * of the underlying LLDD module.  You must hold host_lock of the
11281da177e4SLinus Torvalds  * parent Scsi_Host or already have a reference when calling this.
11291da177e4SLinus Torvalds  */
11301da177e4SLinus Torvalds int scsi_device_get(struct scsi_device *sdev)
11311da177e4SLinus Torvalds {
113285b6c720SJames Bottomley 	if (sdev->sdev_state == SDEV_DEL)
11331da177e4SLinus Torvalds 		return -ENXIO;
11341da177e4SLinus Torvalds 	if (!get_device(&sdev->sdev_gendev))
11351da177e4SLinus Torvalds 		return -ENXIO;
113685b6c720SJames Bottomley 	/* We can fail this if we're doing SCSI operations
113785b6c720SJames Bottomley 	 * from module exit (like cache flush) */
113885b6c720SJames Bottomley 	try_module_get(sdev->host->hostt->module);
113985b6c720SJames Bottomley 
11401da177e4SLinus Torvalds 	return 0;
11411da177e4SLinus Torvalds }
11421da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_get);
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds /**
11451da177e4SLinus Torvalds  * scsi_device_put  -  release a reference to a scsi_device
11461da177e4SLinus Torvalds  * @sdev:	device to release a reference on.
11471da177e4SLinus Torvalds  *
1148eb44820cSRob Landley  * Description: Release a reference to the scsi_device and decrements the use
1149eb44820cSRob Landley  * count of the underlying LLDD module.  The device is freed once the last
11501da177e4SLinus Torvalds  * user vanishes.
11511da177e4SLinus Torvalds  */
11521da177e4SLinus Torvalds void scsi_device_put(struct scsi_device *sdev)
11531da177e4SLinus Torvalds {
1154504fb37aSIngo Molnar #ifdef CONFIG_MODULE_UNLOAD
1155f479ab87SJames Bottomley 	struct module *module = sdev->host->hostt->module;
1156f479ab87SJames Bottomley 
115785b6c720SJames Bottomley 	/* The module refcount will be zero if scsi_device_get()
115885b6c720SJames Bottomley 	 * was called from a module removal routine */
1159f479ab87SJames Bottomley 	if (module && module_refcount(module) != 0)
1160f479ab87SJames Bottomley 		module_put(module);
1161a506b44bSDaniel Walker #endif
11621da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
11631da177e4SLinus Torvalds }
11641da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_put);
11651da177e4SLinus Torvalds 
1166eb44820cSRob Landley /* helper for shost_for_each_device, see that for documentation */
11671da177e4SLinus Torvalds struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
11681da177e4SLinus Torvalds 					   struct scsi_device *prev)
11691da177e4SLinus Torvalds {
11701da177e4SLinus Torvalds 	struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
11711da177e4SLinus Torvalds 	struct scsi_device *next = NULL;
11721da177e4SLinus Torvalds 	unsigned long flags;
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
11751da177e4SLinus Torvalds 	while (list->next != &shost->__devices) {
11761da177e4SLinus Torvalds 		next = list_entry(list->next, struct scsi_device, siblings);
11771da177e4SLinus Torvalds 		/* skip devices that we can't get a reference to */
11781da177e4SLinus Torvalds 		if (!scsi_device_get(next))
11791da177e4SLinus Torvalds 			break;
11801da177e4SLinus Torvalds 		next = NULL;
11811da177e4SLinus Torvalds 		list = list->next;
11821da177e4SLinus Torvalds 	}
11831da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
11841da177e4SLinus Torvalds 
11851da177e4SLinus Torvalds 	if (prev)
11861da177e4SLinus Torvalds 		scsi_device_put(prev);
11871da177e4SLinus Torvalds 	return next;
11881da177e4SLinus Torvalds }
11891da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_iterate_devices);
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds /**
11921da177e4SLinus Torvalds  * starget_for_each_device  -  helper to walk all devices of a target
11931da177e4SLinus Torvalds  * @starget:	target whose devices we want to iterate over.
1194eb44820cSRob Landley  * @data:	Opaque passed to each function call.
1195eb44820cSRob Landley  * @fn:		Function to call on each device
11961da177e4SLinus Torvalds  *
1197522939d4SMaciej W. Rozycki  * This traverses over each device of @starget.  The devices have
11981da177e4SLinus Torvalds  * a reference that must be released by scsi_host_put when breaking
11991da177e4SLinus Torvalds  * out of the loop.
12001da177e4SLinus Torvalds  */
12011da177e4SLinus Torvalds void starget_for_each_device(struct scsi_target *starget, void *data,
12021da177e4SLinus Torvalds 		     void (*fn)(struct scsi_device *, void *))
12031da177e4SLinus Torvalds {
12041da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
12051da177e4SLinus Torvalds 	struct scsi_device *sdev;
12061da177e4SLinus Torvalds 
12071da177e4SLinus Torvalds 	shost_for_each_device(sdev, shost) {
12081da177e4SLinus Torvalds 		if ((sdev->channel == starget->channel) &&
12091da177e4SLinus Torvalds 		    (sdev->id == starget->id))
12101da177e4SLinus Torvalds 			fn(sdev, data);
12111da177e4SLinus Torvalds 	}
12121da177e4SLinus Torvalds }
12131da177e4SLinus Torvalds EXPORT_SYMBOL(starget_for_each_device);
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds /**
121614f501a4SRandy Dunlap  * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
1217522939d4SMaciej W. Rozycki  * @starget:	target whose devices we want to iterate over.
121814f501a4SRandy Dunlap  * @data:	parameter for callback @fn()
121914f501a4SRandy Dunlap  * @fn:		callback function that is invoked for each device
1220522939d4SMaciej W. Rozycki  *
1221522939d4SMaciej W. Rozycki  * This traverses over each device of @starget.  It does _not_
1222522939d4SMaciej W. Rozycki  * take a reference on the scsi_device, so the whole loop must be
1223522939d4SMaciej W. Rozycki  * protected by shost->host_lock.
1224522939d4SMaciej W. Rozycki  *
1225522939d4SMaciej W. Rozycki  * Note:  The only reason why drivers would want to use this is because
1226522939d4SMaciej W. Rozycki  * they need to access the device list in irq context.  Otherwise you
1227522939d4SMaciej W. Rozycki  * really want to use starget_for_each_device instead.
1228522939d4SMaciej W. Rozycki  **/
1229522939d4SMaciej W. Rozycki void __starget_for_each_device(struct scsi_target *starget, void *data,
1230522939d4SMaciej W. Rozycki 			       void (*fn)(struct scsi_device *, void *))
1231522939d4SMaciej W. Rozycki {
1232522939d4SMaciej W. Rozycki 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1233522939d4SMaciej W. Rozycki 	struct scsi_device *sdev;
1234522939d4SMaciej W. Rozycki 
1235522939d4SMaciej W. Rozycki 	__shost_for_each_device(sdev, shost) {
1236522939d4SMaciej W. Rozycki 		if ((sdev->channel == starget->channel) &&
1237522939d4SMaciej W. Rozycki 		    (sdev->id == starget->id))
1238522939d4SMaciej W. Rozycki 			fn(sdev, data);
1239522939d4SMaciej W. Rozycki 	}
1240522939d4SMaciej W. Rozycki }
1241522939d4SMaciej W. Rozycki EXPORT_SYMBOL(__starget_for_each_device);
1242522939d4SMaciej W. Rozycki 
1243522939d4SMaciej W. Rozycki /**
12441da177e4SLinus Torvalds  * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
12451da177e4SLinus Torvalds  * @starget:	SCSI target pointer
12461da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
12471da177e4SLinus Torvalds  *
1248eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @lun for a given
12491da177e4SLinus Torvalds  * @starget.  The returned scsi_device does not have an additional
12501da177e4SLinus Torvalds  * reference.  You must hold the host's host_lock over this call and
125132aeef60SHannes Reinecke  * any access to the returned scsi_device. A scsi_device in state
125232aeef60SHannes Reinecke  * SDEV_DEL is skipped.
12531da177e4SLinus Torvalds  *
1254dc8875e1SRandy Dunlap  * Note:  The only reason why drivers should use this is because
1255eb44820cSRob Landley  * they need to access the device list in irq context.  Otherwise you
12561da177e4SLinus Torvalds  * really want to use scsi_device_lookup_by_target instead.
12571da177e4SLinus Torvalds  **/
12581da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
12599cb78c16SHannes Reinecke 						   u64 lun)
12601da177e4SLinus Torvalds {
12611da177e4SLinus Torvalds 	struct scsi_device *sdev;
12621da177e4SLinus Torvalds 
12631da177e4SLinus Torvalds 	list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
126432aeef60SHannes Reinecke 		if (sdev->sdev_state == SDEV_DEL)
126532aeef60SHannes Reinecke 			continue;
12661da177e4SLinus Torvalds 		if (sdev->lun ==lun)
12671da177e4SLinus Torvalds 			return sdev;
12681da177e4SLinus Torvalds 	}
12691da177e4SLinus Torvalds 
12701da177e4SLinus Torvalds 	return NULL;
12711da177e4SLinus Torvalds }
12721da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup_by_target);
12731da177e4SLinus Torvalds 
12741da177e4SLinus Torvalds /**
12751da177e4SLinus Torvalds  * scsi_device_lookup_by_target - find a device given the target
12761da177e4SLinus Torvalds  * @starget:	SCSI target pointer
12771da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
12781da177e4SLinus Torvalds  *
1279477e608cSBartlomiej Zolnierkiewicz  * Description: Looks up the scsi_device with the specified @lun for a given
1280477e608cSBartlomiej Zolnierkiewicz  * @starget.  The returned scsi_device has an additional reference that
1281eb44820cSRob Landley  * needs to be released with scsi_device_put once you're done with it.
12821da177e4SLinus Torvalds  **/
12831da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
12849cb78c16SHannes Reinecke 						 u64 lun)
12851da177e4SLinus Torvalds {
12861da177e4SLinus Torvalds 	struct scsi_device *sdev;
12871da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
12881da177e4SLinus Torvalds 	unsigned long flags;
12891da177e4SLinus Torvalds 
12901da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
12911da177e4SLinus Torvalds 	sdev = __scsi_device_lookup_by_target(starget, lun);
12921da177e4SLinus Torvalds 	if (sdev && scsi_device_get(sdev))
12931da177e4SLinus Torvalds 		sdev = NULL;
12941da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	return sdev;
12971da177e4SLinus Torvalds }
12981da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup_by_target);
12991da177e4SLinus Torvalds 
13001da177e4SLinus Torvalds /**
1301eb44820cSRob Landley  * __scsi_device_lookup - find a device given the host (UNLOCKED)
13021da177e4SLinus Torvalds  * @shost:	SCSI host pointer
13031da177e4SLinus Torvalds  * @channel:	SCSI channel (zero if only one channel)
1304eb44820cSRob Landley  * @id:		SCSI target number (physical unit number)
13051da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
13061da177e4SLinus Torvalds  *
1307eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
1308eb44820cSRob Landley  * for a given host. The returned scsi_device does not have an additional
1309eb44820cSRob Landley  * reference.  You must hold the host's host_lock over this call and any access
1310eb44820cSRob Landley  * to the returned scsi_device.
13111da177e4SLinus Torvalds  *
13121da177e4SLinus Torvalds  * Note:  The only reason why drivers would want to use this is because
1313eb44820cSRob Landley  * they need to access the device list in irq context.  Otherwise you
13141da177e4SLinus Torvalds  * really want to use scsi_device_lookup instead.
13151da177e4SLinus Torvalds  **/
13161da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
13179cb78c16SHannes Reinecke 		uint channel, uint id, u64 lun)
13181da177e4SLinus Torvalds {
13191da177e4SLinus Torvalds 	struct scsi_device *sdev;
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	list_for_each_entry(sdev, &shost->__devices, siblings) {
13221da177e4SLinus Torvalds 		if (sdev->channel == channel && sdev->id == id &&
13231da177e4SLinus Torvalds 				sdev->lun ==lun)
13241da177e4SLinus Torvalds 			return sdev;
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
13271da177e4SLinus Torvalds 	return NULL;
13281da177e4SLinus Torvalds }
13291da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup);
13301da177e4SLinus Torvalds 
13311da177e4SLinus Torvalds /**
13321da177e4SLinus Torvalds  * scsi_device_lookup - find a device given the host
13331da177e4SLinus Torvalds  * @shost:	SCSI host pointer
13341da177e4SLinus Torvalds  * @channel:	SCSI channel (zero if only one channel)
13351da177e4SLinus Torvalds  * @id:		SCSI target number (physical unit number)
13361da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
13371da177e4SLinus Torvalds  *
1338eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
1339eb44820cSRob Landley  * for a given host.  The returned scsi_device has an additional reference that
1340eb44820cSRob Landley  * needs to be released with scsi_device_put once you're done with it.
13411da177e4SLinus Torvalds  **/
13421da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
13439cb78c16SHannes Reinecke 		uint channel, uint id, u64 lun)
13441da177e4SLinus Torvalds {
13451da177e4SLinus Torvalds 	struct scsi_device *sdev;
13461da177e4SLinus Torvalds 	unsigned long flags;
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
13491da177e4SLinus Torvalds 	sdev = __scsi_device_lookup(shost, channel, id, lun);
13501da177e4SLinus Torvalds 	if (sdev && scsi_device_get(sdev))
13511da177e4SLinus Torvalds 		sdev = NULL;
13521da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
13531da177e4SLinus Torvalds 
13541da177e4SLinus Torvalds 	return sdev;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup);
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI core");
13591da177e4SLinus Torvalds MODULE_LICENSE("GPL");
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
13621da177e4SLinus Torvalds MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
13631da177e4SLinus Torvalds 
1364d285203cSChristoph Hellwig bool scsi_use_blk_mq = false;
1365d285203cSChristoph Hellwig module_param_named(use_blk_mq, scsi_use_blk_mq, bool, S_IWUSR | S_IRUGO);
1366d285203cSChristoph Hellwig 
13671da177e4SLinus Torvalds static int __init init_scsi(void)
13681da177e4SLinus Torvalds {
13699bf09c23SJens Axboe 	int error;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	error = scsi_init_queue();
13721da177e4SLinus Torvalds 	if (error)
13731da177e4SLinus Torvalds 		return error;
13741da177e4SLinus Torvalds 	error = scsi_init_procfs();
13751da177e4SLinus Torvalds 	if (error)
13761da177e4SLinus Torvalds 		goto cleanup_queue;
13771da177e4SLinus Torvalds 	error = scsi_init_devinfo();
13781da177e4SLinus Torvalds 	if (error)
13791da177e4SLinus Torvalds 		goto cleanup_procfs;
13801da177e4SLinus Torvalds 	error = scsi_init_hosts();
13811da177e4SLinus Torvalds 	if (error)
13821da177e4SLinus Torvalds 		goto cleanup_devlist;
13831da177e4SLinus Torvalds 	error = scsi_init_sysctl();
13841da177e4SLinus Torvalds 	if (error)
13851da177e4SLinus Torvalds 		goto cleanup_hosts;
13861da177e4SLinus Torvalds 	error = scsi_sysfs_register();
13871da177e4SLinus Torvalds 	if (error)
13881da177e4SLinus Torvalds 		goto cleanup_sysctl;
13891da177e4SLinus Torvalds 
139084314fd4SJames Smart 	scsi_netlink_init();
139184314fd4SJames Smart 
13921da177e4SLinus Torvalds 	printk(KERN_NOTICE "SCSI subsystem initialized\n");
13931da177e4SLinus Torvalds 	return 0;
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds cleanup_sysctl:
13961da177e4SLinus Torvalds 	scsi_exit_sysctl();
13971da177e4SLinus Torvalds cleanup_hosts:
13981da177e4SLinus Torvalds 	scsi_exit_hosts();
13991da177e4SLinus Torvalds cleanup_devlist:
14001da177e4SLinus Torvalds 	scsi_exit_devinfo();
14011da177e4SLinus Torvalds cleanup_procfs:
14021da177e4SLinus Torvalds 	scsi_exit_procfs();
14031da177e4SLinus Torvalds cleanup_queue:
14041da177e4SLinus Torvalds 	scsi_exit_queue();
14051da177e4SLinus Torvalds 	printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
14061da177e4SLinus Torvalds 	       -error);
14071da177e4SLinus Torvalds 	return error;
14081da177e4SLinus Torvalds }
14091da177e4SLinus Torvalds 
14101da177e4SLinus Torvalds static void __exit exit_scsi(void)
14111da177e4SLinus Torvalds {
141284314fd4SJames Smart 	scsi_netlink_exit();
14131da177e4SLinus Torvalds 	scsi_sysfs_unregister();
14141da177e4SLinus Torvalds 	scsi_exit_sysctl();
14151da177e4SLinus Torvalds 	scsi_exit_hosts();
14161da177e4SLinus Torvalds 	scsi_exit_devinfo();
14171da177e4SLinus Torvalds 	scsi_exit_procfs();
14181da177e4SLinus Torvalds 	scsi_exit_queue();
1419a4683487SDan Williams 	async_unregister_domain(&scsi_sd_probe_domain);
14201da177e4SLinus Torvalds }
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds subsys_initcall(init_scsi);
14231da177e4SLinus Torvalds module_exit(exit_scsi);
1424