xref: /openbmc/linux/drivers/scsi/scsi.c (revision d278d4a8)
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 
1011da177e4SLinus Torvalds struct scsi_host_cmd_pool {
1025b7f1680SJames Bottomley 	struct kmem_cache	*cmd_slab;
1035b7f1680SJames Bottomley 	struct kmem_cache	*sense_slab;
1041da177e4SLinus Torvalds 	unsigned int		users;
1055b7f1680SJames Bottomley 	char			*cmd_name;
1065b7f1680SJames Bottomley 	char			*sense_name;
1071da177e4SLinus Torvalds 	unsigned int		slab_flags;
108c53033f6SAl Viro 	gfp_t			gfp_mask;
1091da177e4SLinus Torvalds };
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_pool = {
1125b7f1680SJames Bottomley 	.cmd_name	= "scsi_cmd_cache",
1135b7f1680SJames Bottomley 	.sense_name	= "scsi_sense_cache",
1141da177e4SLinus Torvalds 	.slab_flags	= SLAB_HWCACHE_ALIGN,
1151da177e4SLinus Torvalds };
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds static struct scsi_host_cmd_pool scsi_cmd_dma_pool = {
1185b7f1680SJames Bottomley 	.cmd_name	= "scsi_cmd_cache(DMA)",
1195b7f1680SJames Bottomley 	.sense_name	= "scsi_sense_cache(DMA)",
1201da177e4SLinus Torvalds 	.slab_flags	= SLAB_HWCACHE_ALIGN|SLAB_CACHE_DMA,
1211da177e4SLinus Torvalds 	.gfp_mask	= __GFP_DMA,
1221da177e4SLinus Torvalds };
1231da177e4SLinus Torvalds 
1240b950672SArjan van de Ven static DEFINE_MUTEX(host_cmd_pool_mutex);
1251da177e4SLinus Torvalds 
126eb44820cSRob Landley /**
1277c283341SChristoph Hellwig  * scsi_host_free_command - internal function to release a command
1287c283341SChristoph Hellwig  * @shost:	host to free the command for
129e507e30bSJames Bottomley  * @cmd:	command to release
130e507e30bSJames Bottomley  *
131e507e30bSJames Bottomley  * the command must previously have been allocated by
1327c283341SChristoph Hellwig  * scsi_host_alloc_command.
133e507e30bSJames Bottomley  */
134e507e30bSJames Bottomley static void
1357c283341SChristoph Hellwig scsi_host_free_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
136e507e30bSJames Bottomley {
1377c283341SChristoph Hellwig 	struct scsi_host_cmd_pool *pool = shost->cmd_pool;
1387c283341SChristoph Hellwig 
1397027ad72SMartin K. Petersen 	if (cmd->prot_sdb)
1407027ad72SMartin K. Petersen 		kmem_cache_free(scsi_sdb_cache, cmd->prot_sdb);
141e507e30bSJames Bottomley 	kmem_cache_free(pool->sense_slab, cmd->sense_buffer);
142e507e30bSJames Bottomley 	kmem_cache_free(pool->cmd_slab, cmd);
143e507e30bSJames Bottomley }
144e507e30bSJames Bottomley 
145e507e30bSJames Bottomley /**
1467027ad72SMartin K. Petersen  * scsi_host_alloc_command - internal function to allocate command
1477027ad72SMartin K. Petersen  * @shost:	SCSI host whose pool to allocate from
1487027ad72SMartin K. Petersen  * @gfp_mask:	mask for the allocation
1497027ad72SMartin K. Petersen  *
1507027ad72SMartin K. Petersen  * Returns a fully allocated command with sense buffer and protection
1517027ad72SMartin K. Petersen  * data buffer (where applicable) or NULL on failure
1527027ad72SMartin K. Petersen  */
1537027ad72SMartin K. Petersen static struct scsi_cmnd *
1547027ad72SMartin K. Petersen scsi_host_alloc_command(struct Scsi_Host *shost, gfp_t gfp_mask)
1557027ad72SMartin K. Petersen {
1567c283341SChristoph Hellwig 	struct scsi_host_cmd_pool *pool = shost->cmd_pool;
1577027ad72SMartin K. Petersen 	struct scsi_cmnd *cmd;
1587027ad72SMartin K. Petersen 
1597c283341SChristoph Hellwig 	cmd = kmem_cache_zalloc(pool->cmd_slab, gfp_mask | pool->gfp_mask);
1607027ad72SMartin K. Petersen 	if (!cmd)
1617c283341SChristoph Hellwig 		goto fail;
1627c283341SChristoph Hellwig 
1637c283341SChristoph Hellwig 	cmd->sense_buffer = kmem_cache_alloc(pool->sense_slab,
1647c283341SChristoph Hellwig 					     gfp_mask | pool->gfp_mask);
1657c283341SChristoph Hellwig 	if (!cmd->sense_buffer)
1667c283341SChristoph Hellwig 		goto fail_free_cmd;
1677027ad72SMartin K. Petersen 
1687027ad72SMartin K. Petersen 	if (scsi_host_get_prot(shost) >= SHOST_DIX_TYPE0_PROTECTION) {
1697027ad72SMartin K. Petersen 		cmd->prot_sdb = kmem_cache_zalloc(scsi_sdb_cache, gfp_mask);
1707c283341SChristoph Hellwig 		if (!cmd->prot_sdb)
1717c283341SChristoph Hellwig 			goto fail_free_sense;
1727027ad72SMartin K. Petersen 	}
1737027ad72SMartin K. Petersen 
1747027ad72SMartin K. Petersen 	return cmd;
1757c283341SChristoph Hellwig 
1767c283341SChristoph Hellwig fail_free_sense:
1777c283341SChristoph Hellwig 	kmem_cache_free(pool->sense_slab, cmd->sense_buffer);
1787c283341SChristoph Hellwig fail_free_cmd:
1797c283341SChristoph Hellwig 	kmem_cache_free(pool->cmd_slab, cmd);
1807c283341SChristoph Hellwig fail:
1817c283341SChristoph Hellwig 	return NULL;
1827027ad72SMartin K. Petersen }
1837027ad72SMartin K. Petersen 
1847027ad72SMartin K. Petersen /**
185eb44820cSRob Landley  * __scsi_get_command - Allocate a struct scsi_cmnd
186eb44820cSRob Landley  * @shost: host to transmit command
187eb44820cSRob Landley  * @gfp_mask: allocation mask
188eb44820cSRob Landley  *
189eb44820cSRob Landley  * Description: allocate a struct scsi_cmd from host's slab, recycling from the
190eb44820cSRob Landley  *              host's free_list if necessary.
191eb44820cSRob Landley  */
192f1bea55dSChristoph Hellwig static struct scsi_cmnd *
193f1bea55dSChristoph Hellwig __scsi_get_command(struct Scsi_Host *shost, gfp_t gfp_mask)
1941da177e4SLinus Torvalds {
195b4c2554dSMartin K. Petersen 	struct scsi_cmnd *cmd = scsi_host_alloc_command(shost, gfp_mask);
196164fc5dcSHugh Dickins 
1971da177e4SLinus Torvalds 	if (unlikely(!cmd)) {
1981da177e4SLinus Torvalds 		unsigned long flags;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 		spin_lock_irqsave(&shost->free_list_lock, flags);
2011da177e4SLinus Torvalds 		if (likely(!list_empty(&shost->free_list))) {
2021da177e4SLinus Torvalds 			cmd = list_entry(shost->free_list.next,
2031da177e4SLinus Torvalds 					 struct scsi_cmnd, list);
2041da177e4SLinus Torvalds 			list_del_init(&cmd->list);
2051da177e4SLinus Torvalds 		}
2061da177e4SLinus Torvalds 		spin_unlock_irqrestore(&shost->free_list_lock, flags);
207de25deb1SFUJITA Tomonori 
208de25deb1SFUJITA Tomonori 		if (cmd) {
209b4c2554dSMartin K. Petersen 			void *buf, *prot;
210b4c2554dSMartin K. Petersen 
211de25deb1SFUJITA Tomonori 			buf = cmd->sense_buffer;
212b4c2554dSMartin K. Petersen 			prot = cmd->prot_sdb;
213b4c2554dSMartin K. Petersen 
214de25deb1SFUJITA Tomonori 			memset(cmd, 0, sizeof(*cmd));
215b4c2554dSMartin K. Petersen 
216de25deb1SFUJITA Tomonori 			cmd->sense_buffer = buf;
217b4c2554dSMartin K. Petersen 			cmd->prot_sdb = prot;
218de25deb1SFUJITA Tomonori 		}
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	return cmd;
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
224eb44820cSRob Landley /**
225eb44820cSRob Landley  * scsi_get_command - Allocate and setup a scsi command block
226eb44820cSRob Landley  * @dev: parent scsi device
227eb44820cSRob Landley  * @gfp_mask: allocator flags
2281da177e4SLinus Torvalds  *
2291da177e4SLinus Torvalds  * Returns:	The allocated scsi command structure.
2301da177e4SLinus Torvalds  */
231c53033f6SAl Viro struct scsi_cmnd *scsi_get_command(struct scsi_device *dev, gfp_t gfp_mask)
2321da177e4SLinus Torvalds {
23304796336SChristoph Hellwig 	struct scsi_cmnd *cmd = __scsi_get_command(dev->host, gfp_mask);
2341da177e4SLinus Torvalds 	unsigned long flags;
2351da177e4SLinus Torvalds 
23604796336SChristoph Hellwig 	if (unlikely(cmd == NULL))
23704796336SChristoph Hellwig 		return NULL;
23804796336SChristoph Hellwig 
2391da177e4SLinus Torvalds 	cmd->device = dev;
2401da177e4SLinus Torvalds 	INIT_LIST_HEAD(&cmd->list);
241e494f6a7SHannes Reinecke 	INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
2421da177e4SLinus Torvalds 	spin_lock_irqsave(&dev->list_lock, flags);
2431da177e4SLinus Torvalds 	list_add_tail(&cmd->list, &dev->cmd_list);
2441da177e4SLinus Torvalds 	spin_unlock_irqrestore(&dev->list_lock, flags);
24579e448bfSMatthew Dobson 	cmd->jiffies_at_alloc = jiffies;
2461da177e4SLinus Torvalds 	return cmd;
2471da177e4SLinus Torvalds }
2481da177e4SLinus Torvalds 
249eb44820cSRob Landley /**
250eb44820cSRob Landley  * __scsi_put_command - Free a struct scsi_cmnd
251eb44820cSRob Landley  * @shost: dev->host
252eb44820cSRob Landley  * @cmd: Command to free
253eb44820cSRob Landley  */
254f1bea55dSChristoph Hellwig static void __scsi_put_command(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
255b58d9154SFUJITA Tomonori {
256b58d9154SFUJITA Tomonori 	unsigned long flags;
257b58d9154SFUJITA Tomonori 
258b58d9154SFUJITA Tomonori 	if (unlikely(list_empty(&shost->free_list))) {
25944b93b59SChristoph Hellwig 		spin_lock_irqsave(&shost->free_list_lock, flags);
26044b93b59SChristoph Hellwig 		if (list_empty(&shost->free_list)) {
261b58d9154SFUJITA Tomonori 			list_add(&cmd->list, &shost->free_list);
262b58d9154SFUJITA Tomonori 			cmd = NULL;
263b58d9154SFUJITA Tomonori 		}
264b58d9154SFUJITA Tomonori 		spin_unlock_irqrestore(&shost->free_list_lock, flags);
26544b93b59SChristoph Hellwig 	}
266b58d9154SFUJITA Tomonori 
267e507e30bSJames Bottomley 	if (likely(cmd != NULL))
2687c283341SChristoph Hellwig 		scsi_host_free_command(shost, cmd);
269b58d9154SFUJITA Tomonori }
270b58d9154SFUJITA Tomonori 
271eb44820cSRob Landley /**
272eb44820cSRob Landley  * scsi_put_command - Free a scsi command block
273eb44820cSRob Landley  * @cmd: command block to free
2741da177e4SLinus Torvalds  *
2751da177e4SLinus Torvalds  * Returns:	Nothing.
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * Notes:	The command must not belong to any lists.
2781da177e4SLinus Torvalds  */
2791da177e4SLinus Torvalds void scsi_put_command(struct scsi_cmnd *cmd)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	unsigned long flags;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/* serious error if the command hasn't come from a device list */
2841da177e4SLinus Torvalds 	spin_lock_irqsave(&cmd->device->list_lock, flags);
2851da177e4SLinus Torvalds 	BUG_ON(list_empty(&cmd->list));
2861da177e4SLinus Torvalds 	list_del_init(&cmd->list);
287b58d9154SFUJITA Tomonori 	spin_unlock_irqrestore(&cmd->device->list_lock, flags);
2881da177e4SLinus Torvalds 
289fcc95a76SBart Van Assche 	BUG_ON(delayed_work_pending(&cmd->abort_work));
290e494f6a7SHannes Reinecke 
29104796336SChristoph Hellwig 	__scsi_put_command(cmd->device->host, cmd);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
29489d9a567SChristoph Hellwig static struct scsi_host_cmd_pool *
29589d9a567SChristoph Hellwig scsi_find_host_cmd_pool(struct Scsi_Host *shost)
2961da177e4SLinus Torvalds {
29789d9a567SChristoph Hellwig 	if (shost->hostt->cmd_size)
29889d9a567SChristoph Hellwig 		return shost->hostt->cmd_pool;
29989d9a567SChristoph Hellwig 	if (shost->unchecked_isa_dma)
30089d9a567SChristoph Hellwig 		return &scsi_cmd_dma_pool;
30189d9a567SChristoph Hellwig 	return &scsi_cmd_pool;
30289d9a567SChristoph Hellwig }
30389d9a567SChristoph Hellwig 
30489d9a567SChristoph Hellwig static void
30589d9a567SChristoph Hellwig scsi_free_host_cmd_pool(struct scsi_host_cmd_pool *pool)
30689d9a567SChristoph Hellwig {
30789d9a567SChristoph Hellwig 	kfree(pool->sense_name);
30889d9a567SChristoph Hellwig 	kfree(pool->cmd_name);
30989d9a567SChristoph Hellwig 	kfree(pool);
31089d9a567SChristoph Hellwig }
31189d9a567SChristoph Hellwig 
31289d9a567SChristoph Hellwig static struct scsi_host_cmd_pool *
31389d9a567SChristoph Hellwig scsi_alloc_host_cmd_pool(struct Scsi_Host *shost)
31489d9a567SChristoph Hellwig {
31589d9a567SChristoph Hellwig 	struct scsi_host_template *hostt = shost->hostt;
31689d9a567SChristoph Hellwig 	struct scsi_host_cmd_pool *pool;
31789d9a567SChristoph Hellwig 
31889d9a567SChristoph Hellwig 	pool = kzalloc(sizeof(*pool), GFP_KERNEL);
31989d9a567SChristoph Hellwig 	if (!pool)
32089d9a567SChristoph Hellwig 		return NULL;
32189d9a567SChristoph Hellwig 
322884ffee0SJames Bottomley 	pool->cmd_name = kasprintf(GFP_KERNEL, "%s_cmd", hostt->proc_name);
323884ffee0SJames Bottomley 	pool->sense_name = kasprintf(GFP_KERNEL, "%s_sense", hostt->proc_name);
32489d9a567SChristoph Hellwig 	if (!pool->cmd_name || !pool->sense_name) {
32589d9a567SChristoph Hellwig 		scsi_free_host_cmd_pool(pool);
32689d9a567SChristoph Hellwig 		return NULL;
32789d9a567SChristoph Hellwig 	}
32889d9a567SChristoph Hellwig 
32989d9a567SChristoph Hellwig 	pool->slab_flags = SLAB_HWCACHE_ALIGN;
33089d9a567SChristoph Hellwig 	if (shost->unchecked_isa_dma) {
33189d9a567SChristoph Hellwig 		pool->slab_flags |= SLAB_CACHE_DMA;
33289d9a567SChristoph Hellwig 		pool->gfp_mask = __GFP_DMA;
33389d9a567SChristoph Hellwig 	}
334f6105c08SJuergen Gross 
335f6105c08SJuergen Gross 	if (hostt->cmd_size)
336f6105c08SJuergen Gross 		hostt->cmd_pool = pool;
337f6105c08SJuergen Gross 
33889d9a567SChristoph Hellwig 	return pool;
33989d9a567SChristoph Hellwig }
34089d9a567SChristoph Hellwig 
34189d9a567SChristoph Hellwig static struct scsi_host_cmd_pool *
34289d9a567SChristoph Hellwig scsi_get_host_cmd_pool(struct Scsi_Host *shost)
34389d9a567SChristoph Hellwig {
34489d9a567SChristoph Hellwig 	struct scsi_host_template *hostt = shost->hostt;
3451c353f7dSJames Bottomley 	struct scsi_host_cmd_pool *retval = NULL, *pool;
34689d9a567SChristoph Hellwig 	size_t cmd_size = sizeof(struct scsi_cmnd) + hostt->cmd_size;
34789d9a567SChristoph Hellwig 
3481da177e4SLinus Torvalds 	/*
3491da177e4SLinus Torvalds 	 * Select a command slab for this host and create it if not
350eb44820cSRob Landley 	 * yet existent.
3511da177e4SLinus Torvalds 	 */
3520b950672SArjan van de Ven 	mutex_lock(&host_cmd_pool_mutex);
35389d9a567SChristoph Hellwig 	pool = scsi_find_host_cmd_pool(shost);
35489d9a567SChristoph Hellwig 	if (!pool) {
35589d9a567SChristoph Hellwig 		pool = scsi_alloc_host_cmd_pool(shost);
35689d9a567SChristoph Hellwig 		if (!pool)
35789d9a567SChristoph Hellwig 			goto out;
35889d9a567SChristoph Hellwig 	}
35989d9a567SChristoph Hellwig 
3601da177e4SLinus Torvalds 	if (!pool->users) {
36189d9a567SChristoph Hellwig 		pool->cmd_slab = kmem_cache_create(pool->cmd_name, cmd_size, 0,
36220c2df83SPaul Mundt 						   pool->slab_flags, NULL);
3635b7f1680SJames Bottomley 		if (!pool->cmd_slab)
36489d9a567SChristoph Hellwig 			goto out_free_pool;
3655b7f1680SJames Bottomley 
3665b7f1680SJames Bottomley 		pool->sense_slab = kmem_cache_create(pool->sense_name,
3675b7f1680SJames Bottomley 						     SCSI_SENSE_BUFFERSIZE, 0,
3685b7f1680SJames Bottomley 						     pool->slab_flags, NULL);
36989d9a567SChristoph Hellwig 		if (!pool->sense_slab)
37089d9a567SChristoph Hellwig 			goto out_free_slab;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	pool->users++;
3741c353f7dSJames Bottomley 	retval = pool;
37589d9a567SChristoph Hellwig out:
3760b950672SArjan van de Ven 	mutex_unlock(&host_cmd_pool_mutex);
3771c353f7dSJames Bottomley 	return retval;
37889d9a567SChristoph Hellwig 
37989d9a567SChristoph Hellwig out_free_slab:
38089d9a567SChristoph Hellwig 	kmem_cache_destroy(pool->cmd_slab);
38189d9a567SChristoph Hellwig out_free_pool:
382f6105c08SJuergen Gross 	if (hostt->cmd_size) {
38389d9a567SChristoph Hellwig 		scsi_free_host_cmd_pool(pool);
384f6105c08SJuergen Gross 		hostt->cmd_pool = NULL;
385f6105c08SJuergen Gross 	}
38689d9a567SChristoph Hellwig 	goto out;
3871c353f7dSJames Bottomley }
3881da177e4SLinus Torvalds 
38989d9a567SChristoph Hellwig static void scsi_put_host_cmd_pool(struct Scsi_Host *shost)
3901c353f7dSJames Bottomley {
39189d9a567SChristoph Hellwig 	struct scsi_host_template *hostt = shost->hostt;
3921c353f7dSJames Bottomley 	struct scsi_host_cmd_pool *pool;
393de25deb1SFUJITA Tomonori 
394166a7287SFUJITA Tomonori 	mutex_lock(&host_cmd_pool_mutex);
39589d9a567SChristoph Hellwig 	pool = scsi_find_host_cmd_pool(shost);
39689d9a567SChristoph Hellwig 
3971c353f7dSJames Bottomley 	/*
3981c353f7dSJames Bottomley 	 * This may happen if a driver has a mismatched get and put
3991c353f7dSJames Bottomley 	 * of the command pool; the driver should be implicated in
4001c353f7dSJames Bottomley 	 * the stack trace
4011c353f7dSJames Bottomley 	 */
4021c353f7dSJames Bottomley 	BUG_ON(pool->users == 0);
4031c353f7dSJames Bottomley 
4045b7f1680SJames Bottomley 	if (!--pool->users) {
4055b7f1680SJames Bottomley 		kmem_cache_destroy(pool->cmd_slab);
4065b7f1680SJames Bottomley 		kmem_cache_destroy(pool->sense_slab);
407f6105c08SJuergen Gross 		if (hostt->cmd_size) {
40889d9a567SChristoph Hellwig 			scsi_free_host_cmd_pool(pool);
409f6105c08SJuergen Gross 			hostt->cmd_pool = NULL;
410f6105c08SJuergen Gross 		}
4115b7f1680SJames Bottomley 	}
4120b950672SArjan van de Ven 	mutex_unlock(&host_cmd_pool_mutex);
4131c353f7dSJames Bottomley }
4141c353f7dSJames Bottomley 
4151c353f7dSJames Bottomley /**
4161c353f7dSJames Bottomley  * scsi_setup_command_freelist - Setup the command freelist for a scsi host.
4171c353f7dSJames Bottomley  * @shost: host to allocate the freelist for.
4181c353f7dSJames Bottomley  *
4191c353f7dSJames Bottomley  * Description: The command freelist protects against system-wide out of memory
4201c353f7dSJames Bottomley  * deadlock by preallocating one SCSI command structure for each host, so the
4211c353f7dSJames Bottomley  * system can always write to a swap file on a device associated with that host.
4221c353f7dSJames Bottomley  *
4231c353f7dSJames Bottomley  * Returns:	Nothing.
4241c353f7dSJames Bottomley  */
4251c353f7dSJames Bottomley int scsi_setup_command_freelist(struct Scsi_Host *shost)
4261c353f7dSJames Bottomley {
4271c353f7dSJames Bottomley 	const gfp_t gfp_mask = shost->unchecked_isa_dma ? GFP_DMA : GFP_KERNEL;
42889d9a567SChristoph Hellwig 	struct scsi_cmnd *cmd;
4291c353f7dSJames Bottomley 
4301c353f7dSJames Bottomley 	spin_lock_init(&shost->free_list_lock);
4311c353f7dSJames Bottomley 	INIT_LIST_HEAD(&shost->free_list);
4321c353f7dSJames Bottomley 
43389d9a567SChristoph Hellwig 	shost->cmd_pool = scsi_get_host_cmd_pool(shost);
4341c353f7dSJames Bottomley 	if (!shost->cmd_pool)
4351da177e4SLinus Torvalds 		return -ENOMEM;
4361c353f7dSJames Bottomley 
4371c353f7dSJames Bottomley 	/*
4381c353f7dSJames Bottomley 	 * Get one backup command for this host.
4391c353f7dSJames Bottomley 	 */
4407027ad72SMartin K. Petersen 	cmd = scsi_host_alloc_command(shost, gfp_mask);
4411c353f7dSJames Bottomley 	if (!cmd) {
44289d9a567SChristoph Hellwig 		scsi_put_host_cmd_pool(shost);
44361d7416aSAlan D. Brunelle 		shost->cmd_pool = NULL;
4441c353f7dSJames Bottomley 		return -ENOMEM;
4451c353f7dSJames Bottomley 	}
4461c353f7dSJames Bottomley 	list_add(&cmd->list, &shost->free_list);
4471c353f7dSJames Bottomley 	return 0;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
450eb44820cSRob Landley /**
451eb44820cSRob Landley  * scsi_destroy_command_freelist - Release the command freelist for a scsi host.
452eb44820cSRob Landley  * @shost: host whose freelist is going to be destroyed
4531da177e4SLinus Torvalds  */
4541da177e4SLinus Torvalds void scsi_destroy_command_freelist(struct Scsi_Host *shost)
4551da177e4SLinus Torvalds {
45661d7416aSAlan D. Brunelle 	/*
45761d7416aSAlan D. Brunelle 	 * If cmd_pool is NULL the free list was not initialized, so
45861d7416aSAlan D. Brunelle 	 * do not attempt to release resources.
45961d7416aSAlan D. Brunelle 	 */
46061d7416aSAlan D. Brunelle 	if (!shost->cmd_pool)
46161d7416aSAlan D. Brunelle 		return;
46261d7416aSAlan D. Brunelle 
4631da177e4SLinus Torvalds 	while (!list_empty(&shost->free_list)) {
4641da177e4SLinus Torvalds 		struct scsi_cmnd *cmd;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 		cmd = list_entry(shost->free_list.next, struct scsi_cmnd, list);
4671da177e4SLinus Torvalds 		list_del_init(&cmd->list);
4687c283341SChristoph Hellwig 		scsi_host_free_command(shost, cmd);
4691da177e4SLinus Torvalds 	}
4701c353f7dSJames Bottomley 	shost->cmd_pool = NULL;
47189d9a567SChristoph Hellwig 	scsi_put_host_cmd_pool(shost);
472de25deb1SFUJITA Tomonori }
473de25deb1SFUJITA Tomonori 
4741da177e4SLinus Torvalds #ifdef CONFIG_SCSI_LOGGING
4751da177e4SLinus Torvalds void scsi_log_send(struct scsi_cmnd *cmd)
4761da177e4SLinus Torvalds {
4771da177e4SLinus Torvalds 	unsigned int level;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	/*
4801da177e4SLinus Torvalds 	 * If ML QUEUE log level is greater than or equal to:
4811da177e4SLinus Torvalds 	 *
4821da177e4SLinus Torvalds 	 * 1: nothing (match completion)
4831da177e4SLinus Torvalds 	 *
484c11c004bSHannes Reinecke 	 * 2: log opcode + command of all commands + cmd address
4851da177e4SLinus Torvalds 	 *
486c11c004bSHannes Reinecke 	 * 3: same as 2
4871da177e4SLinus Torvalds 	 *
488a4a6afb4SHannes Reinecke 	 * 4: same as 3
4891da177e4SLinus Torvalds 	 */
4901da177e4SLinus Torvalds 	if (unlikely(scsi_logging_level)) {
4911da177e4SLinus Torvalds 		level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
4921da177e4SLinus Torvalds 				       SCSI_LOG_MLQUEUE_BITS);
4931da177e4SLinus Torvalds 		if (level > 1) {
494c11c004bSHannes Reinecke 			scmd_printk(KERN_INFO, cmd,
495c11c004bSHannes Reinecke 				    "Send: scmd 0x%p\n", cmd);
4961da177e4SLinus Torvalds 			scsi_print_command(cmd);
4971da177e4SLinus Torvalds 		}
4981da177e4SLinus Torvalds 	}
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
5021da177e4SLinus Torvalds {
5031da177e4SLinus Torvalds 	unsigned int level;
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	/*
5061da177e4SLinus Torvalds 	 * If ML COMPLETE log level is greater than or equal to:
5071da177e4SLinus Torvalds 	 *
5081da177e4SLinus Torvalds 	 * 1: log disposition, result, opcode + command, and conditionally
5091da177e4SLinus Torvalds 	 * sense data for failures or non SUCCESS dispositions.
5101da177e4SLinus Torvalds 	 *
5111da177e4SLinus Torvalds 	 * 2: same as 1 but for all command completions.
5121da177e4SLinus Torvalds 	 *
513c11c004bSHannes Reinecke 	 * 3: same as 2
5141da177e4SLinus Torvalds 	 *
5151da177e4SLinus Torvalds 	 * 4: same as 3 plus dump extra junk
5161da177e4SLinus Torvalds 	 */
5171da177e4SLinus Torvalds 	if (unlikely(scsi_logging_level)) {
5181da177e4SLinus Torvalds 		level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
5191da177e4SLinus Torvalds 				       SCSI_LOG_MLCOMPLETE_BITS);
5201da177e4SLinus Torvalds 		if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
5211da177e4SLinus Torvalds 		    (level > 1)) {
522026f8da8SHannes Reinecke 			scsi_print_result(cmd, "Done", disposition);
5231da177e4SLinus Torvalds 			scsi_print_command(cmd);
524a4d04a4cSMartin K. Petersen 			if (status_byte(cmd->result) & CHECK_CONDITION)
525d811b848SHannes Reinecke 				scsi_print_sense(cmd);
526a4d04a4cSMartin K. Petersen 			if (level > 3)
527a4d04a4cSMartin K. Petersen 				scmd_printk(KERN_INFO, cmd,
528a4d04a4cSMartin K. Petersen 					    "scsi host busy %d failed %d\n",
52974665016SChristoph Hellwig 					    atomic_read(&cmd->device->host->host_busy),
530a4d04a4cSMartin K. Petersen 					    cmd->device->host->host_failed);
5311da177e4SLinus Torvalds 		}
5321da177e4SLinus Torvalds 	}
5331da177e4SLinus Torvalds }
5341da177e4SLinus Torvalds #endif
5351da177e4SLinus Torvalds 
536eb44820cSRob Landley /**
537eb44820cSRob Landley  * scsi_cmd_get_serial - Assign a serial number to a command
538eb44820cSRob Landley  * @host: the scsi host
539eb44820cSRob Landley  * @cmd: command to assign serial number to
540eb44820cSRob Landley  *
541eb44820cSRob Landley  * Description: a serial number identifies a request for error recovery
5421da177e4SLinus Torvalds  * and debugging purposes.  Protected by the Host_Lock of host.
5431da177e4SLinus Torvalds  */
544f281233dSJeff Garzik void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
5451da177e4SLinus Torvalds {
5461da177e4SLinus Torvalds 	cmd->serial_number = host->cmd_serial_number++;
5471da177e4SLinus Torvalds 	if (cmd->serial_number == 0)
5481da177e4SLinus Torvalds 		cmd->serial_number = host->cmd_serial_number++;
5491da177e4SLinus Torvalds }
550f281233dSJeff Garzik EXPORT_SYMBOL(scsi_cmd_get_serial);
5511da177e4SLinus Torvalds 
552eb44820cSRob Landley /**
553eb44820cSRob Landley  * scsi_finish_command - cleanup and pass command back to upper layer
554eb44820cSRob Landley  * @cmd: the command
5551da177e4SLinus Torvalds  *
556eb44820cSRob Landley  * Description: Pass command off to upper layer for finishing of I/O
5571da177e4SLinus Torvalds  *              request, waking processes that are waiting on results,
5581da177e4SLinus Torvalds  *              etc.
5591da177e4SLinus Torvalds  */
5601da177e4SLinus Torvalds void scsi_finish_command(struct scsi_cmnd *cmd)
5611da177e4SLinus Torvalds {
5621da177e4SLinus Torvalds 	struct scsi_device *sdev = cmd->device;
563f0c0a376SMike Christie 	struct scsi_target *starget = scsi_target(sdev);
5641da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
5657b3d9545SLinus Torvalds 	struct scsi_driver *drv;
5667b3d9545SLinus Torvalds 	unsigned int good_bytes;
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	scsi_device_unbusy(sdev);
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	/*
571cd9070c9SChristoph Hellwig 	 * Clear the flags that say that the device/target/host is no longer
572cd9070c9SChristoph Hellwig 	 * capable of accepting new commands.
5731da177e4SLinus Torvalds 	 */
574cd9070c9SChristoph Hellwig 	if (atomic_read(&shost->host_blocked))
575cd9070c9SChristoph Hellwig 		atomic_set(&shost->host_blocked, 0);
576cd9070c9SChristoph Hellwig 	if (atomic_read(&starget->target_blocked))
577cd9070c9SChristoph Hellwig 		atomic_set(&starget->target_blocked, 0);
578cd9070c9SChristoph Hellwig 	if (atomic_read(&sdev->device_blocked))
579cd9070c9SChristoph Hellwig 		atomic_set(&sdev->device_blocked, 0);
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 	/*
5821da177e4SLinus Torvalds 	 * If we have valid sense information, then some kind of recovery
5831da177e4SLinus Torvalds 	 * must have taken place.  Make a note of this.
5841da177e4SLinus Torvalds 	 */
5851da177e4SLinus Torvalds 	if (SCSI_SENSE_VALID(cmd))
5861da177e4SLinus Torvalds 		cmd->result |= (DRIVER_SENSE << 24);
5871da177e4SLinus Torvalds 
5883bf743e7SJeff Garzik 	SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
5893bf743e7SJeff Garzik 				"Notifying upper driver of completion "
5903bf743e7SJeff Garzik 				"(result %x)\n", cmd->result));
5911da177e4SLinus Torvalds 
592f18573abSFUJITA Tomonori 	good_bytes = scsi_bufflen(cmd);
5937b3d9545SLinus Torvalds         if (cmd->request->cmd_type != REQ_TYPE_BLOCK_PC) {
594427e59f0SJames Bottomley 		int old_good_bytes = good_bytes;
5957b3d9545SLinus Torvalds 		drv = scsi_cmd_to_driver(cmd);
5967b3d9545SLinus Torvalds 		if (drv->done)
5977b3d9545SLinus Torvalds 			good_bytes = drv->done(cmd);
598427e59f0SJames Bottomley 		/*
599427e59f0SJames Bottomley 		 * USB may not give sense identifying bad sector and
600427e59f0SJames Bottomley 		 * simply return a residue instead, so subtract off the
601427e59f0SJames Bottomley 		 * residue if drv->done() error processing indicates no
602427e59f0SJames Bottomley 		 * change to the completion length.
603427e59f0SJames Bottomley 		 */
604427e59f0SJames Bottomley 		if (good_bytes == old_good_bytes)
605427e59f0SJames Bottomley 			good_bytes -= scsi_get_resid(cmd);
6067b3d9545SLinus Torvalds 	}
6077b3d9545SLinus Torvalds 	scsi_io_completion(cmd, good_bytes);
6081da177e4SLinus Torvalds }
6091da177e4SLinus Torvalds 
610eb44820cSRob Landley /**
611db5ed4dfSChristoph Hellwig  * scsi_change_queue_depth - change a device's queue depth
612eb44820cSRob Landley  * @sdev: SCSI Device in question
613db5ed4dfSChristoph Hellwig  * @depth: number of commands allowed to be queued to the driver
6141da177e4SLinus Torvalds  *
615db5ed4dfSChristoph Hellwig  * Sets the device queue depth and returns the new value.
6161da177e4SLinus Torvalds  */
617db5ed4dfSChristoph Hellwig int scsi_change_queue_depth(struct scsi_device *sdev, int depth)
6181da177e4SLinus Torvalds {
61964d513acSChristoph Hellwig 	if (depth > 0) {
62064d513acSChristoph Hellwig 		sdev->queue_depth = depth;
62164d513acSChristoph Hellwig 		wmb();
622885ace9eSMike Christie 	}
6231da177e4SLinus Torvalds 
624d278d4a8SJens Axboe 	if (sdev->request_queue)
625d278d4a8SJens Axboe 		blk_set_queue_depth(sdev->request_queue, depth);
626d278d4a8SJens Axboe 
627db5ed4dfSChristoph Hellwig 	return sdev->queue_depth;
6281da177e4SLinus Torvalds }
629db5ed4dfSChristoph Hellwig EXPORT_SYMBOL(scsi_change_queue_depth);
6301da177e4SLinus Torvalds 
631eb44820cSRob Landley /**
632eb44820cSRob Landley  * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
633eb44820cSRob Landley  * @sdev: SCSI Device in question
634eb44820cSRob Landley  * @depth: Current number of outstanding SCSI commands on this device,
635eb44820cSRob Landley  *         not counting the one returned as QUEUE_FULL.
6361da177e4SLinus Torvalds  *
637eb44820cSRob Landley  * Description:	This function will track successive QUEUE_FULL events on a
6381da177e4SLinus Torvalds  * 		specific SCSI device to determine if and when there is a
6391da177e4SLinus Torvalds  * 		need to adjust the queue depth on the device.
6401da177e4SLinus Torvalds  *
641eb44820cSRob Landley  * Returns:	0 - No change needed, >0 - Adjust queue depth to this new depth,
6421da177e4SLinus Torvalds  * 		-1 - Drop back to untagged operation using host->cmd_per_lun
6431da177e4SLinus Torvalds  * 			as the untagged command depth
6441da177e4SLinus Torvalds  *
6451da177e4SLinus Torvalds  * Lock Status:	None held on entry
6461da177e4SLinus Torvalds  *
6471da177e4SLinus Torvalds  * Notes:	Low level drivers may call this at any time and we will do
6481da177e4SLinus Torvalds  * 		"The Right Thing."  We are interrupt context safe.
6491da177e4SLinus Torvalds  */
6501da177e4SLinus Torvalds int scsi_track_queue_full(struct scsi_device *sdev, int depth)
6511da177e4SLinus Torvalds {
6524a84067dSVasu Dev 
6534a84067dSVasu Dev 	/*
6544a84067dSVasu Dev 	 * Don't let QUEUE_FULLs on the same
6554a84067dSVasu Dev 	 * jiffies count, they could all be from
6564a84067dSVasu Dev 	 * same event.
6574a84067dSVasu Dev 	 */
6584a84067dSVasu Dev 	if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4))
6591da177e4SLinus Torvalds 		return 0;
6601da177e4SLinus Torvalds 
6614a84067dSVasu Dev 	sdev->last_queue_full_time = jiffies;
6621da177e4SLinus Torvalds 	if (sdev->last_queue_full_depth != depth) {
6631da177e4SLinus Torvalds 		sdev->last_queue_full_count = 1;
6641da177e4SLinus Torvalds 		sdev->last_queue_full_depth = depth;
6651da177e4SLinus Torvalds 	} else {
6661da177e4SLinus Torvalds 		sdev->last_queue_full_count++;
6671da177e4SLinus Torvalds 	}
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	if (sdev->last_queue_full_count <= 10)
6701da177e4SLinus Torvalds 		return 0;
6711da177e4SLinus Torvalds 
672db5ed4dfSChristoph Hellwig 	return scsi_change_queue_depth(sdev, depth);
6731da177e4SLinus Torvalds }
6741da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_track_queue_full);
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds /**
677881a256dSMatthew Wilcox  * scsi_vpd_inquiry - Request a device provide us with a VPD page
678881a256dSMatthew Wilcox  * @sdev: The device to ask
679881a256dSMatthew Wilcox  * @buffer: Where to put the result
680881a256dSMatthew Wilcox  * @page: Which Vital Product Data to return
681881a256dSMatthew Wilcox  * @len: The length of the buffer
682881a256dSMatthew Wilcox  *
683881a256dSMatthew Wilcox  * This is an internal helper function.  You probably want to use
684881a256dSMatthew Wilcox  * scsi_get_vpd_page instead.
685881a256dSMatthew Wilcox  *
686bc8945dfSHannes Reinecke  * Returns size of the vpd page on success or a negative error number.
687881a256dSMatthew Wilcox  */
688881a256dSMatthew Wilcox static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer,
689881a256dSMatthew Wilcox 							u8 page, unsigned len)
690881a256dSMatthew Wilcox {
691881a256dSMatthew Wilcox 	int result;
692881a256dSMatthew Wilcox 	unsigned char cmd[16];
693881a256dSMatthew Wilcox 
694bc8945dfSHannes Reinecke 	if (len < 4)
695bc8945dfSHannes Reinecke 		return -EINVAL;
696bc8945dfSHannes Reinecke 
697881a256dSMatthew Wilcox 	cmd[0] = INQUIRY;
698881a256dSMatthew Wilcox 	cmd[1] = 1;		/* EVPD */
699881a256dSMatthew Wilcox 	cmd[2] = page;
700881a256dSMatthew Wilcox 	cmd[3] = len >> 8;
701881a256dSMatthew Wilcox 	cmd[4] = len & 0xff;
702881a256dSMatthew Wilcox 	cmd[5] = 0;		/* Control byte */
703881a256dSMatthew Wilcox 
704881a256dSMatthew Wilcox 	/*
705881a256dSMatthew Wilcox 	 * I'm not convinced we need to try quite this hard to get VPD, but
706881a256dSMatthew Wilcox 	 * all the existing users tried this hard.
707881a256dSMatthew Wilcox 	 */
708881a256dSMatthew Wilcox 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer,
70995a3639eSJames Bottomley 				  len, NULL, 30 * HZ, 3, NULL);
710881a256dSMatthew Wilcox 	if (result)
711bc8945dfSHannes Reinecke 		return -EIO;
712881a256dSMatthew Wilcox 
713881a256dSMatthew Wilcox 	/* Sanity check that we got the page back that we asked for */
714881a256dSMatthew Wilcox 	if (buffer[1] != page)
715881a256dSMatthew Wilcox 		return -EIO;
716881a256dSMatthew Wilcox 
717bc8945dfSHannes Reinecke 	return get_unaligned_be16(&buffer[2]) + 4;
718881a256dSMatthew Wilcox }
719881a256dSMatthew Wilcox 
720881a256dSMatthew Wilcox /**
721881a256dSMatthew Wilcox  * scsi_get_vpd_page - Get Vital Product Data from a SCSI device
722881a256dSMatthew Wilcox  * @sdev: The device to ask
723881a256dSMatthew Wilcox  * @page: Which Vital Product Data to return
724786f8ba2SRandy Dunlap  * @buf: where to store the VPD
725786f8ba2SRandy Dunlap  * @buf_len: number of bytes in the VPD buffer area
726881a256dSMatthew Wilcox  *
727881a256dSMatthew Wilcox  * SCSI devices may optionally supply Vital Product Data.  Each 'page'
728881a256dSMatthew Wilcox  * of VPD is defined in the appropriate SCSI document (eg SPC, SBC).
729881a256dSMatthew Wilcox  * If the device supports this VPD page, this routine returns a pointer
730881a256dSMatthew Wilcox  * to a buffer containing the data from that page.  The caller is
731881a256dSMatthew Wilcox  * responsible for calling kfree() on this pointer when it is no longer
732881a256dSMatthew Wilcox  * needed.  If we cannot retrieve the VPD page this routine returns %NULL.
733881a256dSMatthew Wilcox  */
734e3deec09SJames Bottomley int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
735e3deec09SJames Bottomley 		      int buf_len)
736881a256dSMatthew Wilcox {
737881a256dSMatthew Wilcox 	int i, result;
738881a256dSMatthew Wilcox 
7397562523eSMartin K. Petersen 	if (sdev->skip_vpd_pages)
7407562523eSMartin K. Petersen 		goto fail;
7417562523eSMartin K. Petersen 
742881a256dSMatthew Wilcox 	/* Ask for all the pages supported by this device */
743e3deec09SJames Bottomley 	result = scsi_vpd_inquiry(sdev, buf, 0, buf_len);
744bc8945dfSHannes Reinecke 	if (result < 4)
745881a256dSMatthew Wilcox 		goto fail;
746881a256dSMatthew Wilcox 
747881a256dSMatthew Wilcox 	/* If the user actually wanted this page, we can skip the rest */
748881a256dSMatthew Wilcox 	if (page == 0)
74916d3ea26SMartin K. Petersen 		return 0;
750881a256dSMatthew Wilcox 
751bc8945dfSHannes Reinecke 	for (i = 4; i < min(result, buf_len); i++)
752bc8945dfSHannes Reinecke 		if (buf[i] == page)
753881a256dSMatthew Wilcox 			goto found;
754e3deec09SJames Bottomley 
755bc8945dfSHannes Reinecke 	if (i < result && i >= buf_len)
756e3deec09SJames Bottomley 		/* ran off the end of the buffer, give us benefit of doubt */
757e3deec09SJames Bottomley 		goto found;
758881a256dSMatthew Wilcox 	/* The device claims it doesn't support the requested page */
759881a256dSMatthew Wilcox 	goto fail;
760881a256dSMatthew Wilcox 
761881a256dSMatthew Wilcox  found:
762e3deec09SJames Bottomley 	result = scsi_vpd_inquiry(sdev, buf, page, buf_len);
763bc8945dfSHannes Reinecke 	if (result < 0)
764881a256dSMatthew Wilcox 		goto fail;
765881a256dSMatthew Wilcox 
766e3deec09SJames Bottomley 	return 0;
767881a256dSMatthew Wilcox 
768881a256dSMatthew Wilcox  fail:
769e3deec09SJames Bottomley 	return -EINVAL;
770881a256dSMatthew Wilcox }
771881a256dSMatthew Wilcox EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
772881a256dSMatthew Wilcox 
773881a256dSMatthew Wilcox /**
774b3ae8780SHannes Reinecke  * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure
775b3ae8780SHannes Reinecke  * @sdev: The device to ask
776b3ae8780SHannes Reinecke  *
777b3ae8780SHannes Reinecke  * Attach the 'Device Identification' VPD page (0x83) and the
778b3ae8780SHannes Reinecke  * 'Unit Serial Number' VPD page (0x80) to a SCSI device
779b3ae8780SHannes Reinecke  * structure. This information can be used to identify the device
780b3ae8780SHannes Reinecke  * uniquely.
781b3ae8780SHannes Reinecke  */
782b3ae8780SHannes Reinecke void scsi_attach_vpd(struct scsi_device *sdev)
783b3ae8780SHannes Reinecke {
784b3ae8780SHannes Reinecke 	int result, i;
785b3ae8780SHannes Reinecke 	int vpd_len = SCSI_VPD_PG_LEN;
786b3ae8780SHannes Reinecke 	int pg80_supported = 0;
787b3ae8780SHannes Reinecke 	int pg83_supported = 0;
78809e2b0b1SHannes Reinecke 	unsigned char __rcu *vpd_buf, *orig_vpd_buf = NULL;
789b3ae8780SHannes Reinecke 
7905ddfe085SHannes Reinecke 	if (!scsi_device_supports_vpd(sdev))
791b3ae8780SHannes Reinecke 		return;
7925ddfe085SHannes Reinecke 
793b3ae8780SHannes Reinecke retry_pg0:
794b3ae8780SHannes Reinecke 	vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
795b3ae8780SHannes Reinecke 	if (!vpd_buf)
796b3ae8780SHannes Reinecke 		return;
797b3ae8780SHannes Reinecke 
798b3ae8780SHannes Reinecke 	/* Ask for all the pages supported by this device */
799b3ae8780SHannes Reinecke 	result = scsi_vpd_inquiry(sdev, vpd_buf, 0, vpd_len);
800b3ae8780SHannes Reinecke 	if (result < 0) {
801b3ae8780SHannes Reinecke 		kfree(vpd_buf);
802b3ae8780SHannes Reinecke 		return;
803b3ae8780SHannes Reinecke 	}
804b3ae8780SHannes Reinecke 	if (result > vpd_len) {
805b3ae8780SHannes Reinecke 		vpd_len = result;
806b3ae8780SHannes Reinecke 		kfree(vpd_buf);
807b3ae8780SHannes Reinecke 		goto retry_pg0;
808b3ae8780SHannes Reinecke 	}
809b3ae8780SHannes Reinecke 
810b3ae8780SHannes Reinecke 	for (i = 4; i < result; i++) {
811b3ae8780SHannes Reinecke 		if (vpd_buf[i] == 0x80)
812b3ae8780SHannes Reinecke 			pg80_supported = 1;
813b3ae8780SHannes Reinecke 		if (vpd_buf[i] == 0x83)
814b3ae8780SHannes Reinecke 			pg83_supported = 1;
815b3ae8780SHannes Reinecke 	}
816b3ae8780SHannes Reinecke 	kfree(vpd_buf);
817b3ae8780SHannes Reinecke 	vpd_len = SCSI_VPD_PG_LEN;
818b3ae8780SHannes Reinecke 
819b3ae8780SHannes Reinecke 	if (pg80_supported) {
820b3ae8780SHannes Reinecke retry_pg80:
821b3ae8780SHannes Reinecke 		vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
822b3ae8780SHannes Reinecke 		if (!vpd_buf)
823b3ae8780SHannes Reinecke 			return;
824b3ae8780SHannes Reinecke 
825b3ae8780SHannes Reinecke 		result = scsi_vpd_inquiry(sdev, vpd_buf, 0x80, vpd_len);
826b3ae8780SHannes Reinecke 		if (result < 0) {
827b3ae8780SHannes Reinecke 			kfree(vpd_buf);
828b3ae8780SHannes Reinecke 			return;
829b3ae8780SHannes Reinecke 		}
830b3ae8780SHannes Reinecke 		if (result > vpd_len) {
831b3ae8780SHannes Reinecke 			vpd_len = result;
832b3ae8780SHannes Reinecke 			kfree(vpd_buf);
833b3ae8780SHannes Reinecke 			goto retry_pg80;
834b3ae8780SHannes Reinecke 		}
83509e2b0b1SHannes Reinecke 		mutex_lock(&sdev->inquiry_mutex);
83609e2b0b1SHannes Reinecke 		orig_vpd_buf = sdev->vpd_pg80;
837b3ae8780SHannes Reinecke 		sdev->vpd_pg80_len = result;
83809e2b0b1SHannes Reinecke 		rcu_assign_pointer(sdev->vpd_pg80, vpd_buf);
83909e2b0b1SHannes Reinecke 		mutex_unlock(&sdev->inquiry_mutex);
84009e2b0b1SHannes Reinecke 		synchronize_rcu();
84109e2b0b1SHannes Reinecke 		if (orig_vpd_buf) {
84209e2b0b1SHannes Reinecke 			kfree(orig_vpd_buf);
84309e2b0b1SHannes Reinecke 			orig_vpd_buf = NULL;
84409e2b0b1SHannes Reinecke 		}
845b3ae8780SHannes Reinecke 		vpd_len = SCSI_VPD_PG_LEN;
846b3ae8780SHannes Reinecke 	}
847b3ae8780SHannes Reinecke 
848b3ae8780SHannes Reinecke 	if (pg83_supported) {
849b3ae8780SHannes Reinecke retry_pg83:
850b3ae8780SHannes Reinecke 		vpd_buf = kmalloc(vpd_len, GFP_KERNEL);
851b3ae8780SHannes Reinecke 		if (!vpd_buf)
852b3ae8780SHannes Reinecke 			return;
853b3ae8780SHannes Reinecke 
854b3ae8780SHannes Reinecke 		result = scsi_vpd_inquiry(sdev, vpd_buf, 0x83, vpd_len);
855b3ae8780SHannes Reinecke 		if (result < 0) {
856b3ae8780SHannes Reinecke 			kfree(vpd_buf);
857b3ae8780SHannes Reinecke 			return;
858b3ae8780SHannes Reinecke 		}
859b3ae8780SHannes Reinecke 		if (result > vpd_len) {
860b3ae8780SHannes Reinecke 			vpd_len = result;
861b3ae8780SHannes Reinecke 			kfree(vpd_buf);
862b3ae8780SHannes Reinecke 			goto retry_pg83;
863b3ae8780SHannes Reinecke 		}
86409e2b0b1SHannes Reinecke 		mutex_lock(&sdev->inquiry_mutex);
86509e2b0b1SHannes Reinecke 		orig_vpd_buf = sdev->vpd_pg83;
866b3ae8780SHannes Reinecke 		sdev->vpd_pg83_len = result;
86709e2b0b1SHannes Reinecke 		rcu_assign_pointer(sdev->vpd_pg83, vpd_buf);
86809e2b0b1SHannes Reinecke 		mutex_unlock(&sdev->inquiry_mutex);
86909e2b0b1SHannes Reinecke 		synchronize_rcu();
87009e2b0b1SHannes Reinecke 		if (orig_vpd_buf)
87109e2b0b1SHannes Reinecke 			kfree(orig_vpd_buf);
872b3ae8780SHannes Reinecke 	}
873b3ae8780SHannes Reinecke }
874b3ae8780SHannes Reinecke 
875b3ae8780SHannes Reinecke /**
8763c6bdaeaSMartin K. Petersen  * scsi_report_opcode - Find out if a given command opcode is supported
8773c6bdaeaSMartin K. Petersen  * @sdev:	scsi device to query
8783c6bdaeaSMartin K. Petersen  * @buffer:	scratch buffer (must be at least 20 bytes long)
8793c6bdaeaSMartin K. Petersen  * @len:	length of buffer
8803c6bdaeaSMartin K. Petersen  * @opcode:	opcode for command to look up
8813c6bdaeaSMartin K. Petersen  *
8823c6bdaeaSMartin K. Petersen  * Uses the REPORT SUPPORTED OPERATION CODES to look up the given
88366c28f97SMartin K. Petersen  * opcode. Returns -EINVAL if RSOC fails, 0 if the command opcode is
88466c28f97SMartin K. Petersen  * unsupported and 1 if the device claims to support the command.
8853c6bdaeaSMartin K. Petersen  */
8863c6bdaeaSMartin K. Petersen int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
8873c6bdaeaSMartin K. Petersen 		       unsigned int len, unsigned char opcode)
8883c6bdaeaSMartin K. Petersen {
8893c6bdaeaSMartin K. Petersen 	unsigned char cmd[16];
8903c6bdaeaSMartin K. Petersen 	struct scsi_sense_hdr sshdr;
8913c6bdaeaSMartin K. Petersen 	int result;
8923c6bdaeaSMartin K. Petersen 
8933c6bdaeaSMartin K. Petersen 	if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3)
89466c28f97SMartin K. Petersen 		return -EINVAL;
8953c6bdaeaSMartin K. Petersen 
8963c6bdaeaSMartin K. Petersen 	memset(cmd, 0, 16);
8973c6bdaeaSMartin K. Petersen 	cmd[0] = MAINTENANCE_IN;
8983c6bdaeaSMartin K. Petersen 	cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;
8993c6bdaeaSMartin K. Petersen 	cmd[2] = 1;		/* One command format */
9003c6bdaeaSMartin K. Petersen 	cmd[3] = opcode;
9013c6bdaeaSMartin K. Petersen 	put_unaligned_be32(len, &cmd[6]);
9023c6bdaeaSMartin K. Petersen 	memset(buffer, 0, len);
9033c6bdaeaSMartin K. Petersen 
9043c6bdaeaSMartin K. Petersen 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
9053c6bdaeaSMartin K. Petersen 				  &sshdr, 30 * HZ, 3, NULL);
9063c6bdaeaSMartin K. Petersen 
9073c6bdaeaSMartin K. Petersen 	if (result && scsi_sense_valid(&sshdr) &&
9083c6bdaeaSMartin K. Petersen 	    sshdr.sense_key == ILLEGAL_REQUEST &&
9093c6bdaeaSMartin K. Petersen 	    (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00)
91066c28f97SMartin K. Petersen 		return -EINVAL;
9113c6bdaeaSMartin K. Petersen 
9123c6bdaeaSMartin K. Petersen 	if ((buffer[1] & 3) == 3) /* Command supported */
9133c6bdaeaSMartin K. Petersen 		return 1;
9143c6bdaeaSMartin K. Petersen 
9153c6bdaeaSMartin K. Petersen 	return 0;
9163c6bdaeaSMartin K. Petersen }
9173c6bdaeaSMartin K. Petersen EXPORT_SYMBOL(scsi_report_opcode);
9183c6bdaeaSMartin K. Petersen 
9193c6bdaeaSMartin K. Petersen /**
920eb44820cSRob Landley  * scsi_device_get  -  get an additional reference to a scsi_device
9211da177e4SLinus Torvalds  * @sdev:	device to get a reference to
9221da177e4SLinus Torvalds  *
923eb44820cSRob Landley  * Description: Gets a reference to the scsi_device and increments the use count
9241da177e4SLinus Torvalds  * of the underlying LLDD module.  You must hold host_lock of the
9251da177e4SLinus Torvalds  * parent Scsi_Host or already have a reference when calling this.
926cff549e4SChristoph Hellwig  *
927cff549e4SChristoph Hellwig  * This will fail if a device is deleted or cancelled, or when the LLD module
928cff549e4SChristoph Hellwig  * is in the process of being unloaded.
9291da177e4SLinus Torvalds  */
9301da177e4SLinus Torvalds int scsi_device_get(struct scsi_device *sdev)
9311da177e4SLinus Torvalds {
932cff549e4SChristoph Hellwig 	if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
933cff549e4SChristoph Hellwig 		goto fail;
9341da177e4SLinus Torvalds 	if (!get_device(&sdev->sdev_gendev))
935cff549e4SChristoph Hellwig 		goto fail;
936cff549e4SChristoph Hellwig 	if (!try_module_get(sdev->host->hostt->module))
937cff549e4SChristoph Hellwig 		goto fail_put_device;
9381da177e4SLinus Torvalds 	return 0;
939cff549e4SChristoph Hellwig 
940cff549e4SChristoph Hellwig fail_put_device:
941cff549e4SChristoph Hellwig 	put_device(&sdev->sdev_gendev);
942cff549e4SChristoph Hellwig fail:
943cff549e4SChristoph Hellwig 	return -ENXIO;
9441da177e4SLinus Torvalds }
9451da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_get);
9461da177e4SLinus Torvalds 
9471da177e4SLinus Torvalds /**
9481da177e4SLinus Torvalds  * scsi_device_put  -  release a reference to a scsi_device
9491da177e4SLinus Torvalds  * @sdev:	device to release a reference on.
9501da177e4SLinus Torvalds  *
951eb44820cSRob Landley  * Description: Release a reference to the scsi_device and decrements the use
952eb44820cSRob Landley  * count of the underlying LLDD module.  The device is freed once the last
9531da177e4SLinus Torvalds  * user vanishes.
9541da177e4SLinus Torvalds  */
9551da177e4SLinus Torvalds void scsi_device_put(struct scsi_device *sdev)
9561da177e4SLinus Torvalds {
957dc4515eaSRusty Russell 	module_put(sdev->host->hostt->module);
9581da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
9591da177e4SLinus Torvalds }
9601da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_put);
9611da177e4SLinus Torvalds 
962eb44820cSRob Landley /* helper for shost_for_each_device, see that for documentation */
9631da177e4SLinus Torvalds struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
9641da177e4SLinus Torvalds 					   struct scsi_device *prev)
9651da177e4SLinus Torvalds {
9661da177e4SLinus Torvalds 	struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
9671da177e4SLinus Torvalds 	struct scsi_device *next = NULL;
9681da177e4SLinus Torvalds 	unsigned long flags;
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
9711da177e4SLinus Torvalds 	while (list->next != &shost->__devices) {
9721da177e4SLinus Torvalds 		next = list_entry(list->next, struct scsi_device, siblings);
9731da177e4SLinus Torvalds 		/* skip devices that we can't get a reference to */
9741da177e4SLinus Torvalds 		if (!scsi_device_get(next))
9751da177e4SLinus Torvalds 			break;
9761da177e4SLinus Torvalds 		next = NULL;
9771da177e4SLinus Torvalds 		list = list->next;
9781da177e4SLinus Torvalds 	}
9791da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
9801da177e4SLinus Torvalds 
9811da177e4SLinus Torvalds 	if (prev)
9821da177e4SLinus Torvalds 		scsi_device_put(prev);
9831da177e4SLinus Torvalds 	return next;
9841da177e4SLinus Torvalds }
9851da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_iterate_devices);
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds /**
9881da177e4SLinus Torvalds  * starget_for_each_device  -  helper to walk all devices of a target
9891da177e4SLinus Torvalds  * @starget:	target whose devices we want to iterate over.
990eb44820cSRob Landley  * @data:	Opaque passed to each function call.
991eb44820cSRob Landley  * @fn:		Function to call on each device
9921da177e4SLinus Torvalds  *
993522939d4SMaciej W. Rozycki  * This traverses over each device of @starget.  The devices have
9941da177e4SLinus Torvalds  * a reference that must be released by scsi_host_put when breaking
9951da177e4SLinus Torvalds  * out of the loop.
9961da177e4SLinus Torvalds  */
9971da177e4SLinus Torvalds void starget_for_each_device(struct scsi_target *starget, void *data,
9981da177e4SLinus Torvalds 		     void (*fn)(struct scsi_device *, void *))
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
10011da177e4SLinus Torvalds 	struct scsi_device *sdev;
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds 	shost_for_each_device(sdev, shost) {
10041da177e4SLinus Torvalds 		if ((sdev->channel == starget->channel) &&
10051da177e4SLinus Torvalds 		    (sdev->id == starget->id))
10061da177e4SLinus Torvalds 			fn(sdev, data);
10071da177e4SLinus Torvalds 	}
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds EXPORT_SYMBOL(starget_for_each_device);
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds /**
101214f501a4SRandy Dunlap  * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
1013522939d4SMaciej W. Rozycki  * @starget:	target whose devices we want to iterate over.
101414f501a4SRandy Dunlap  * @data:	parameter for callback @fn()
101514f501a4SRandy Dunlap  * @fn:		callback function that is invoked for each device
1016522939d4SMaciej W. Rozycki  *
1017522939d4SMaciej W. Rozycki  * This traverses over each device of @starget.  It does _not_
1018522939d4SMaciej W. Rozycki  * take a reference on the scsi_device, so the whole loop must be
1019522939d4SMaciej W. Rozycki  * protected by shost->host_lock.
1020522939d4SMaciej W. Rozycki  *
1021522939d4SMaciej W. Rozycki  * Note:  The only reason why drivers would want to use this is because
1022522939d4SMaciej W. Rozycki  * they need to access the device list in irq context.  Otherwise you
1023522939d4SMaciej W. Rozycki  * really want to use starget_for_each_device instead.
1024522939d4SMaciej W. Rozycki  **/
1025522939d4SMaciej W. Rozycki void __starget_for_each_device(struct scsi_target *starget, void *data,
1026522939d4SMaciej W. Rozycki 			       void (*fn)(struct scsi_device *, void *))
1027522939d4SMaciej W. Rozycki {
1028522939d4SMaciej W. Rozycki 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1029522939d4SMaciej W. Rozycki 	struct scsi_device *sdev;
1030522939d4SMaciej W. Rozycki 
1031522939d4SMaciej W. Rozycki 	__shost_for_each_device(sdev, shost) {
1032522939d4SMaciej W. Rozycki 		if ((sdev->channel == starget->channel) &&
1033522939d4SMaciej W. Rozycki 		    (sdev->id == starget->id))
1034522939d4SMaciej W. Rozycki 			fn(sdev, data);
1035522939d4SMaciej W. Rozycki 	}
1036522939d4SMaciej W. Rozycki }
1037522939d4SMaciej W. Rozycki EXPORT_SYMBOL(__starget_for_each_device);
1038522939d4SMaciej W. Rozycki 
1039522939d4SMaciej W. Rozycki /**
10401da177e4SLinus Torvalds  * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
10411da177e4SLinus Torvalds  * @starget:	SCSI target pointer
10421da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
10431da177e4SLinus Torvalds  *
1044eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @lun for a given
10451da177e4SLinus Torvalds  * @starget.  The returned scsi_device does not have an additional
10461da177e4SLinus Torvalds  * reference.  You must hold the host's host_lock over this call and
104732aeef60SHannes Reinecke  * any access to the returned scsi_device. A scsi_device in state
104832aeef60SHannes Reinecke  * SDEV_DEL is skipped.
10491da177e4SLinus Torvalds  *
1050dc8875e1SRandy Dunlap  * Note:  The only reason why drivers should use this is because
1051eb44820cSRob Landley  * they need to access the device list in irq context.  Otherwise you
10521da177e4SLinus Torvalds  * really want to use scsi_device_lookup_by_target instead.
10531da177e4SLinus Torvalds  **/
10541da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
10559cb78c16SHannes Reinecke 						   u64 lun)
10561da177e4SLinus Torvalds {
10571da177e4SLinus Torvalds 	struct scsi_device *sdev;
10581da177e4SLinus Torvalds 
10591da177e4SLinus Torvalds 	list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
106032aeef60SHannes Reinecke 		if (sdev->sdev_state == SDEV_DEL)
106132aeef60SHannes Reinecke 			continue;
10621da177e4SLinus Torvalds 		if (sdev->lun ==lun)
10631da177e4SLinus Torvalds 			return sdev;
10641da177e4SLinus Torvalds 	}
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	return NULL;
10671da177e4SLinus Torvalds }
10681da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup_by_target);
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds /**
10711da177e4SLinus Torvalds  * scsi_device_lookup_by_target - find a device given the target
10721da177e4SLinus Torvalds  * @starget:	SCSI target pointer
10731da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
10741da177e4SLinus Torvalds  *
1075477e608cSBartlomiej Zolnierkiewicz  * Description: Looks up the scsi_device with the specified @lun for a given
1076477e608cSBartlomiej Zolnierkiewicz  * @starget.  The returned scsi_device has an additional reference that
1077eb44820cSRob Landley  * needs to be released with scsi_device_put once you're done with it.
10781da177e4SLinus Torvalds  **/
10791da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
10809cb78c16SHannes Reinecke 						 u64 lun)
10811da177e4SLinus Torvalds {
10821da177e4SLinus Torvalds 	struct scsi_device *sdev;
10831da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
10841da177e4SLinus Torvalds 	unsigned long flags;
10851da177e4SLinus Torvalds 
10861da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
10871da177e4SLinus Torvalds 	sdev = __scsi_device_lookup_by_target(starget, lun);
10881da177e4SLinus Torvalds 	if (sdev && scsi_device_get(sdev))
10891da177e4SLinus Torvalds 		sdev = NULL;
10901da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
10911da177e4SLinus Torvalds 
10921da177e4SLinus Torvalds 	return sdev;
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup_by_target);
10951da177e4SLinus Torvalds 
10961da177e4SLinus Torvalds /**
1097eb44820cSRob Landley  * __scsi_device_lookup - find a device given the host (UNLOCKED)
10981da177e4SLinus Torvalds  * @shost:	SCSI host pointer
10991da177e4SLinus Torvalds  * @channel:	SCSI channel (zero if only one channel)
1100eb44820cSRob Landley  * @id:		SCSI target number (physical unit number)
11011da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
11021da177e4SLinus Torvalds  *
1103eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
1104eb44820cSRob Landley  * for a given host. The returned scsi_device does not have an additional
1105eb44820cSRob Landley  * reference.  You must hold the host's host_lock over this call and any access
1106eb44820cSRob Landley  * to the returned scsi_device.
11071da177e4SLinus Torvalds  *
11081da177e4SLinus Torvalds  * Note:  The only reason why drivers would want to use this is because
1109eb44820cSRob Landley  * they need to access the device list in irq context.  Otherwise you
11101da177e4SLinus Torvalds  * really want to use scsi_device_lookup instead.
11111da177e4SLinus Torvalds  **/
11121da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
11139cb78c16SHannes Reinecke 		uint channel, uint id, u64 lun)
11141da177e4SLinus Torvalds {
11151da177e4SLinus Torvalds 	struct scsi_device *sdev;
11161da177e4SLinus Torvalds 
11171da177e4SLinus Torvalds 	list_for_each_entry(sdev, &shost->__devices, siblings) {
11181da177e4SLinus Torvalds 		if (sdev->channel == channel && sdev->id == id &&
11191da177e4SLinus Torvalds 				sdev->lun ==lun)
11201da177e4SLinus Torvalds 			return sdev;
11211da177e4SLinus Torvalds 	}
11221da177e4SLinus Torvalds 
11231da177e4SLinus Torvalds 	return NULL;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup);
11261da177e4SLinus Torvalds 
11271da177e4SLinus Torvalds /**
11281da177e4SLinus Torvalds  * scsi_device_lookup - find a device given the host
11291da177e4SLinus Torvalds  * @shost:	SCSI host pointer
11301da177e4SLinus Torvalds  * @channel:	SCSI channel (zero if only one channel)
11311da177e4SLinus Torvalds  * @id:		SCSI target number (physical unit number)
11321da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
11331da177e4SLinus Torvalds  *
1134eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
1135eb44820cSRob Landley  * for a given host.  The returned scsi_device has an additional reference that
1136eb44820cSRob Landley  * needs to be released with scsi_device_put once you're done with it.
11371da177e4SLinus Torvalds  **/
11381da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
11399cb78c16SHannes Reinecke 		uint channel, uint id, u64 lun)
11401da177e4SLinus Torvalds {
11411da177e4SLinus Torvalds 	struct scsi_device *sdev;
11421da177e4SLinus Torvalds 	unsigned long flags;
11431da177e4SLinus Torvalds 
11441da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
11451da177e4SLinus Torvalds 	sdev = __scsi_device_lookup(shost, channel, id, lun);
11461da177e4SLinus Torvalds 	if (sdev && scsi_device_get(sdev))
11471da177e4SLinus Torvalds 		sdev = NULL;
11481da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 	return sdev;
11511da177e4SLinus Torvalds }
11521da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup);
11531da177e4SLinus Torvalds 
11541da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI core");
11551da177e4SLinus Torvalds MODULE_LICENSE("GPL");
11561da177e4SLinus Torvalds 
11571da177e4SLinus Torvalds module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
11581da177e4SLinus Torvalds MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
11591da177e4SLinus Torvalds 
116024c20f10SChristoph Hellwig #ifdef CONFIG_SCSI_MQ_DEFAULT
116124c20f10SChristoph Hellwig bool scsi_use_blk_mq = true;
116224c20f10SChristoph Hellwig #else
1163d285203cSChristoph Hellwig bool scsi_use_blk_mq = false;
116424c20f10SChristoph Hellwig #endif
1165d285203cSChristoph Hellwig module_param_named(use_blk_mq, scsi_use_blk_mq, bool, S_IWUSR | S_IRUGO);
1166d285203cSChristoph Hellwig 
11671da177e4SLinus Torvalds static int __init init_scsi(void)
11681da177e4SLinus Torvalds {
11699bf09c23SJens Axboe 	int error;
11701da177e4SLinus Torvalds 
11711da177e4SLinus Torvalds 	error = scsi_init_queue();
11721da177e4SLinus Torvalds 	if (error)
11731da177e4SLinus Torvalds 		return error;
11741da177e4SLinus Torvalds 	error = scsi_init_procfs();
11751da177e4SLinus Torvalds 	if (error)
11761da177e4SLinus Torvalds 		goto cleanup_queue;
11771da177e4SLinus Torvalds 	error = scsi_init_devinfo();
11781da177e4SLinus Torvalds 	if (error)
11791da177e4SLinus Torvalds 		goto cleanup_procfs;
11801da177e4SLinus Torvalds 	error = scsi_init_hosts();
11811da177e4SLinus Torvalds 	if (error)
11821da177e4SLinus Torvalds 		goto cleanup_devlist;
11831da177e4SLinus Torvalds 	error = scsi_init_sysctl();
11841da177e4SLinus Torvalds 	if (error)
11851da177e4SLinus Torvalds 		goto cleanup_hosts;
11861da177e4SLinus Torvalds 	error = scsi_sysfs_register();
11871da177e4SLinus Torvalds 	if (error)
11881da177e4SLinus Torvalds 		goto cleanup_sysctl;
11891da177e4SLinus Torvalds 
119084314fd4SJames Smart 	scsi_netlink_init();
119184314fd4SJames Smart 
11921da177e4SLinus Torvalds 	printk(KERN_NOTICE "SCSI subsystem initialized\n");
11931da177e4SLinus Torvalds 	return 0;
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds cleanup_sysctl:
11961da177e4SLinus Torvalds 	scsi_exit_sysctl();
11971da177e4SLinus Torvalds cleanup_hosts:
11981da177e4SLinus Torvalds 	scsi_exit_hosts();
11991da177e4SLinus Torvalds cleanup_devlist:
12001da177e4SLinus Torvalds 	scsi_exit_devinfo();
12011da177e4SLinus Torvalds cleanup_procfs:
12021da177e4SLinus Torvalds 	scsi_exit_procfs();
12031da177e4SLinus Torvalds cleanup_queue:
12041da177e4SLinus Torvalds 	scsi_exit_queue();
12051da177e4SLinus Torvalds 	printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
12061da177e4SLinus Torvalds 	       -error);
12071da177e4SLinus Torvalds 	return error;
12081da177e4SLinus Torvalds }
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds static void __exit exit_scsi(void)
12111da177e4SLinus Torvalds {
121284314fd4SJames Smart 	scsi_netlink_exit();
12131da177e4SLinus Torvalds 	scsi_sysfs_unregister();
12141da177e4SLinus Torvalds 	scsi_exit_sysctl();
12151da177e4SLinus Torvalds 	scsi_exit_hosts();
12161da177e4SLinus Torvalds 	scsi_exit_devinfo();
12171da177e4SLinus Torvalds 	scsi_exit_procfs();
12181da177e4SLinus Torvalds 	scsi_exit_queue();
1219a4683487SDan Williams 	async_unregister_domain(&scsi_sd_probe_domain);
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds subsys_initcall(init_scsi);
12231da177e4SLinus Torvalds module_exit(exit_scsi);
1224