xref: /openbmc/linux/drivers/scsi/scsi.c (revision f664a3cc)
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 
101eb44820cSRob Landley /**
102eb44820cSRob Landley  * scsi_put_command - Free a scsi command block
103eb44820cSRob Landley  * @cmd: command block to free
1041da177e4SLinus Torvalds  *
1051da177e4SLinus Torvalds  * Returns:	Nothing.
1061da177e4SLinus Torvalds  *
1071da177e4SLinus Torvalds  * Notes:	The command must not belong to any lists.
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds void scsi_put_command(struct scsi_cmnd *cmd)
1101da177e4SLinus Torvalds {
1112dd6fb59SBart Van Assche 	scsi_del_cmd_from_list(cmd);
112fcc95a76SBart Van Assche 	BUG_ON(delayed_work_pending(&cmd->abort_work));
113de25deb1SFUJITA Tomonori }
114de25deb1SFUJITA Tomonori 
1151da177e4SLinus Torvalds #ifdef CONFIG_SCSI_LOGGING
1161da177e4SLinus Torvalds void scsi_log_send(struct scsi_cmnd *cmd)
1171da177e4SLinus Torvalds {
1181da177e4SLinus Torvalds 	unsigned int level;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	/*
1211da177e4SLinus Torvalds 	 * If ML QUEUE log level is greater than or equal to:
1221da177e4SLinus Torvalds 	 *
1231da177e4SLinus Torvalds 	 * 1: nothing (match completion)
1241da177e4SLinus Torvalds 	 *
125c11c004bSHannes Reinecke 	 * 2: log opcode + command of all commands + cmd address
1261da177e4SLinus Torvalds 	 *
127c11c004bSHannes Reinecke 	 * 3: same as 2
1281da177e4SLinus Torvalds 	 *
129a4a6afb4SHannes Reinecke 	 * 4: same as 3
1301da177e4SLinus Torvalds 	 */
1311da177e4SLinus Torvalds 	if (unlikely(scsi_logging_level)) {
1321da177e4SLinus Torvalds 		level = SCSI_LOG_LEVEL(SCSI_LOG_MLQUEUE_SHIFT,
1331da177e4SLinus Torvalds 				       SCSI_LOG_MLQUEUE_BITS);
1341da177e4SLinus Torvalds 		if (level > 1) {
135c11c004bSHannes Reinecke 			scmd_printk(KERN_INFO, cmd,
136c11c004bSHannes Reinecke 				    "Send: scmd 0x%p\n", cmd);
1371da177e4SLinus Torvalds 			scsi_print_command(cmd);
1381da177e4SLinus Torvalds 		}
1391da177e4SLinus Torvalds 	}
1401da177e4SLinus Torvalds }
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds void scsi_log_completion(struct scsi_cmnd *cmd, int disposition)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	unsigned int level;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/*
1471da177e4SLinus Torvalds 	 * If ML COMPLETE log level is greater than or equal to:
1481da177e4SLinus Torvalds 	 *
1491da177e4SLinus Torvalds 	 * 1: log disposition, result, opcode + command, and conditionally
1501da177e4SLinus Torvalds 	 * sense data for failures or non SUCCESS dispositions.
1511da177e4SLinus Torvalds 	 *
1521da177e4SLinus Torvalds 	 * 2: same as 1 but for all command completions.
1531da177e4SLinus Torvalds 	 *
154c11c004bSHannes Reinecke 	 * 3: same as 2
1551da177e4SLinus Torvalds 	 *
1561da177e4SLinus Torvalds 	 * 4: same as 3 plus dump extra junk
1571da177e4SLinus Torvalds 	 */
1581da177e4SLinus Torvalds 	if (unlikely(scsi_logging_level)) {
1591da177e4SLinus Torvalds 		level = SCSI_LOG_LEVEL(SCSI_LOG_MLCOMPLETE_SHIFT,
1601da177e4SLinus Torvalds 				       SCSI_LOG_MLCOMPLETE_BITS);
1611da177e4SLinus Torvalds 		if (((level > 0) && (cmd->result || disposition != SUCCESS)) ||
1621da177e4SLinus Torvalds 		    (level > 1)) {
163026f8da8SHannes Reinecke 			scsi_print_result(cmd, "Done", disposition);
1641da177e4SLinus Torvalds 			scsi_print_command(cmd);
165c65be1a6SJohannes Thumshirn 			if (status_byte(cmd->result) == CHECK_CONDITION)
166d811b848SHannes Reinecke 				scsi_print_sense(cmd);
167a4d04a4cSMartin K. Petersen 			if (level > 3)
168a4d04a4cSMartin K. Petersen 				scmd_printk(KERN_INFO, cmd,
169a4d04a4cSMartin K. Petersen 					    "scsi host busy %d failed %d\n",
170c84b023aSMing Lei 					    scsi_host_busy(cmd->device->host),
171a4d04a4cSMartin K. Petersen 					    cmd->device->host->host_failed);
1721da177e4SLinus Torvalds 		}
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds }
1751da177e4SLinus Torvalds #endif
1761da177e4SLinus Torvalds 
177eb44820cSRob Landley /**
178eb44820cSRob Landley  * scsi_cmd_get_serial - Assign a serial number to a command
179eb44820cSRob Landley  * @host: the scsi host
180eb44820cSRob Landley  * @cmd: command to assign serial number to
181eb44820cSRob Landley  *
182eb44820cSRob Landley  * Description: a serial number identifies a request for error recovery
1831da177e4SLinus Torvalds  * and debugging purposes.  Protected by the Host_Lock of host.
1841da177e4SLinus Torvalds  */
185f281233dSJeff Garzik void scsi_cmd_get_serial(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	cmd->serial_number = host->cmd_serial_number++;
1881da177e4SLinus Torvalds 	if (cmd->serial_number == 0)
1891da177e4SLinus Torvalds 		cmd->serial_number = host->cmd_serial_number++;
1901da177e4SLinus Torvalds }
191f281233dSJeff Garzik EXPORT_SYMBOL(scsi_cmd_get_serial);
1921da177e4SLinus Torvalds 
193eb44820cSRob Landley /**
194eb44820cSRob Landley  * scsi_finish_command - cleanup and pass command back to upper layer
195eb44820cSRob Landley  * @cmd: the command
1961da177e4SLinus Torvalds  *
197eb44820cSRob Landley  * Description: Pass command off to upper layer for finishing of I/O
1981da177e4SLinus Torvalds  *              request, waking processes that are waiting on results,
1991da177e4SLinus Torvalds  *              etc.
2001da177e4SLinus Torvalds  */
2011da177e4SLinus Torvalds void scsi_finish_command(struct scsi_cmnd *cmd)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	struct scsi_device *sdev = cmd->device;
204f0c0a376SMike Christie 	struct scsi_target *starget = scsi_target(sdev);
2051da177e4SLinus Torvalds 	struct Scsi_Host *shost = sdev->host;
2067b3d9545SLinus Torvalds 	struct scsi_driver *drv;
2077b3d9545SLinus Torvalds 	unsigned int good_bytes;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	scsi_device_unbusy(sdev);
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds 	/*
212cd9070c9SChristoph Hellwig 	 * Clear the flags that say that the device/target/host is no longer
213cd9070c9SChristoph Hellwig 	 * capable of accepting new commands.
2141da177e4SLinus Torvalds 	 */
215cd9070c9SChristoph Hellwig 	if (atomic_read(&shost->host_blocked))
216cd9070c9SChristoph Hellwig 		atomic_set(&shost->host_blocked, 0);
217cd9070c9SChristoph Hellwig 	if (atomic_read(&starget->target_blocked))
218cd9070c9SChristoph Hellwig 		atomic_set(&starget->target_blocked, 0);
219cd9070c9SChristoph Hellwig 	if (atomic_read(&sdev->device_blocked))
220cd9070c9SChristoph Hellwig 		atomic_set(&sdev->device_blocked, 0);
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	/*
2231da177e4SLinus Torvalds 	 * If we have valid sense information, then some kind of recovery
2241da177e4SLinus Torvalds 	 * must have taken place.  Make a note of this.
2251da177e4SLinus Torvalds 	 */
2261da177e4SLinus Torvalds 	if (SCSI_SENSE_VALID(cmd))
2271da177e4SLinus Torvalds 		cmd->result |= (DRIVER_SENSE << 24);
2281da177e4SLinus Torvalds 
2293bf743e7SJeff Garzik 	SCSI_LOG_MLCOMPLETE(4, sdev_printk(KERN_INFO, sdev,
2303bf743e7SJeff Garzik 				"Notifying upper driver of completion "
2313bf743e7SJeff Garzik 				"(result %x)\n", cmd->result));
2321da177e4SLinus Torvalds 
233f18573abSFUJITA Tomonori 	good_bytes = scsi_bufflen(cmd);
23457292b58SChristoph Hellwig 	if (!blk_rq_is_passthrough(cmd->request)) {
235427e59f0SJames Bottomley 		int old_good_bytes = good_bytes;
2367b3d9545SLinus Torvalds 		drv = scsi_cmd_to_driver(cmd);
2377b3d9545SLinus Torvalds 		if (drv->done)
2387b3d9545SLinus Torvalds 			good_bytes = drv->done(cmd);
239427e59f0SJames Bottomley 		/*
240427e59f0SJames Bottomley 		 * USB may not give sense identifying bad sector and
241427e59f0SJames Bottomley 		 * simply return a residue instead, so subtract off the
242427e59f0SJames Bottomley 		 * residue if drv->done() error processing indicates no
243427e59f0SJames Bottomley 		 * change to the completion length.
244427e59f0SJames Bottomley 		 */
245427e59f0SJames Bottomley 		if (good_bytes == old_good_bytes)
246427e59f0SJames Bottomley 			good_bytes -= scsi_get_resid(cmd);
2477b3d9545SLinus Torvalds 	}
2487b3d9545SLinus Torvalds 	scsi_io_completion(cmd, good_bytes);
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
251eb44820cSRob Landley /**
252db5ed4dfSChristoph Hellwig  * scsi_change_queue_depth - change a device's queue depth
253eb44820cSRob Landley  * @sdev: SCSI Device in question
254db5ed4dfSChristoph Hellwig  * @depth: number of commands allowed to be queued to the driver
2551da177e4SLinus Torvalds  *
256db5ed4dfSChristoph Hellwig  * Sets the device queue depth and returns the new value.
2571da177e4SLinus Torvalds  */
258db5ed4dfSChristoph Hellwig int scsi_change_queue_depth(struct scsi_device *sdev, int depth)
2591da177e4SLinus Torvalds {
26064d513acSChristoph Hellwig 	if (depth > 0) {
26164d513acSChristoph Hellwig 		sdev->queue_depth = depth;
26264d513acSChristoph Hellwig 		wmb();
263885ace9eSMike Christie 	}
2641da177e4SLinus Torvalds 
265d278d4a8SJens Axboe 	if (sdev->request_queue)
266d278d4a8SJens Axboe 		blk_set_queue_depth(sdev->request_queue, depth);
267d278d4a8SJens Axboe 
268db5ed4dfSChristoph Hellwig 	return sdev->queue_depth;
2691da177e4SLinus Torvalds }
270db5ed4dfSChristoph Hellwig EXPORT_SYMBOL(scsi_change_queue_depth);
2711da177e4SLinus Torvalds 
272eb44820cSRob Landley /**
273eb44820cSRob Landley  * scsi_track_queue_full - track QUEUE_FULL events to adjust queue depth
274eb44820cSRob Landley  * @sdev: SCSI Device in question
275eb44820cSRob Landley  * @depth: Current number of outstanding SCSI commands on this device,
276eb44820cSRob Landley  *         not counting the one returned as QUEUE_FULL.
2771da177e4SLinus Torvalds  *
278eb44820cSRob Landley  * Description:	This function will track successive QUEUE_FULL events on a
2791da177e4SLinus Torvalds  * 		specific SCSI device to determine if and when there is a
2801da177e4SLinus Torvalds  * 		need to adjust the queue depth on the device.
2811da177e4SLinus Torvalds  *
282eb44820cSRob Landley  * Returns:	0 - No change needed, >0 - Adjust queue depth to this new depth,
2831da177e4SLinus Torvalds  * 		-1 - Drop back to untagged operation using host->cmd_per_lun
2841da177e4SLinus Torvalds  * 			as the untagged command depth
2851da177e4SLinus Torvalds  *
2861da177e4SLinus Torvalds  * Lock Status:	None held on entry
2871da177e4SLinus Torvalds  *
2881da177e4SLinus Torvalds  * Notes:	Low level drivers may call this at any time and we will do
2891da177e4SLinus Torvalds  * 		"The Right Thing."  We are interrupt context safe.
2901da177e4SLinus Torvalds  */
2911da177e4SLinus Torvalds int scsi_track_queue_full(struct scsi_device *sdev, int depth)
2921da177e4SLinus Torvalds {
2934a84067dSVasu Dev 
2944a84067dSVasu Dev 	/*
2954a84067dSVasu Dev 	 * Don't let QUEUE_FULLs on the same
2964a84067dSVasu Dev 	 * jiffies count, they could all be from
2974a84067dSVasu Dev 	 * same event.
2984a84067dSVasu Dev 	 */
2994a84067dSVasu Dev 	if ((jiffies >> 4) == (sdev->last_queue_full_time >> 4))
3001da177e4SLinus Torvalds 		return 0;
3011da177e4SLinus Torvalds 
3024a84067dSVasu Dev 	sdev->last_queue_full_time = jiffies;
3031da177e4SLinus Torvalds 	if (sdev->last_queue_full_depth != depth) {
3041da177e4SLinus Torvalds 		sdev->last_queue_full_count = 1;
3051da177e4SLinus Torvalds 		sdev->last_queue_full_depth = depth;
3061da177e4SLinus Torvalds 	} else {
3071da177e4SLinus Torvalds 		sdev->last_queue_full_count++;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds 	if (sdev->last_queue_full_count <= 10)
3111da177e4SLinus Torvalds 		return 0;
3121da177e4SLinus Torvalds 
313db5ed4dfSChristoph Hellwig 	return scsi_change_queue_depth(sdev, depth);
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_track_queue_full);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds /**
318881a256dSMatthew Wilcox  * scsi_vpd_inquiry - Request a device provide us with a VPD page
319881a256dSMatthew Wilcox  * @sdev: The device to ask
320881a256dSMatthew Wilcox  * @buffer: Where to put the result
321881a256dSMatthew Wilcox  * @page: Which Vital Product Data to return
322881a256dSMatthew Wilcox  * @len: The length of the buffer
323881a256dSMatthew Wilcox  *
324881a256dSMatthew Wilcox  * This is an internal helper function.  You probably want to use
325881a256dSMatthew Wilcox  * scsi_get_vpd_page instead.
326881a256dSMatthew Wilcox  *
327bc8945dfSHannes Reinecke  * Returns size of the vpd page on success or a negative error number.
328881a256dSMatthew Wilcox  */
329881a256dSMatthew Wilcox static int scsi_vpd_inquiry(struct scsi_device *sdev, unsigned char *buffer,
330881a256dSMatthew Wilcox 							u8 page, unsigned len)
331881a256dSMatthew Wilcox {
332881a256dSMatthew Wilcox 	int result;
333881a256dSMatthew Wilcox 	unsigned char cmd[16];
334881a256dSMatthew Wilcox 
335bc8945dfSHannes Reinecke 	if (len < 4)
336bc8945dfSHannes Reinecke 		return -EINVAL;
337bc8945dfSHannes Reinecke 
338881a256dSMatthew Wilcox 	cmd[0] = INQUIRY;
339881a256dSMatthew Wilcox 	cmd[1] = 1;		/* EVPD */
340881a256dSMatthew Wilcox 	cmd[2] = page;
341881a256dSMatthew Wilcox 	cmd[3] = len >> 8;
342881a256dSMatthew Wilcox 	cmd[4] = len & 0xff;
343881a256dSMatthew Wilcox 	cmd[5] = 0;		/* Control byte */
344881a256dSMatthew Wilcox 
345881a256dSMatthew Wilcox 	/*
346881a256dSMatthew Wilcox 	 * I'm not convinced we need to try quite this hard to get VPD, but
347881a256dSMatthew Wilcox 	 * all the existing users tried this hard.
348881a256dSMatthew Wilcox 	 */
349881a256dSMatthew Wilcox 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer,
35095a3639eSJames Bottomley 				  len, NULL, 30 * HZ, 3, NULL);
351881a256dSMatthew Wilcox 	if (result)
352bc8945dfSHannes Reinecke 		return -EIO;
353881a256dSMatthew Wilcox 
354881a256dSMatthew Wilcox 	/* Sanity check that we got the page back that we asked for */
355881a256dSMatthew Wilcox 	if (buffer[1] != page)
356881a256dSMatthew Wilcox 		return -EIO;
357881a256dSMatthew Wilcox 
358bc8945dfSHannes Reinecke 	return get_unaligned_be16(&buffer[2]) + 4;
359881a256dSMatthew Wilcox }
360881a256dSMatthew Wilcox 
361881a256dSMatthew Wilcox /**
362881a256dSMatthew Wilcox  * scsi_get_vpd_page - Get Vital Product Data from a SCSI device
363881a256dSMatthew Wilcox  * @sdev: The device to ask
364881a256dSMatthew Wilcox  * @page: Which Vital Product Data to return
365786f8ba2SRandy Dunlap  * @buf: where to store the VPD
366786f8ba2SRandy Dunlap  * @buf_len: number of bytes in the VPD buffer area
367881a256dSMatthew Wilcox  *
368881a256dSMatthew Wilcox  * SCSI devices may optionally supply Vital Product Data.  Each 'page'
369881a256dSMatthew Wilcox  * of VPD is defined in the appropriate SCSI document (eg SPC, SBC).
370881a256dSMatthew Wilcox  * If the device supports this VPD page, this routine returns a pointer
371881a256dSMatthew Wilcox  * to a buffer containing the data from that page.  The caller is
372881a256dSMatthew Wilcox  * responsible for calling kfree() on this pointer when it is no longer
373881a256dSMatthew Wilcox  * needed.  If we cannot retrieve the VPD page this routine returns %NULL.
374881a256dSMatthew Wilcox  */
375e3deec09SJames Bottomley int scsi_get_vpd_page(struct scsi_device *sdev, u8 page, unsigned char *buf,
376e3deec09SJames Bottomley 		      int buf_len)
377881a256dSMatthew Wilcox {
378881a256dSMatthew Wilcox 	int i, result;
379881a256dSMatthew Wilcox 
3807562523eSMartin K. Petersen 	if (sdev->skip_vpd_pages)
3817562523eSMartin K. Petersen 		goto fail;
3827562523eSMartin K. Petersen 
383881a256dSMatthew Wilcox 	/* Ask for all the pages supported by this device */
384e3deec09SJames Bottomley 	result = scsi_vpd_inquiry(sdev, buf, 0, buf_len);
385bc8945dfSHannes Reinecke 	if (result < 4)
386881a256dSMatthew Wilcox 		goto fail;
387881a256dSMatthew Wilcox 
388881a256dSMatthew Wilcox 	/* If the user actually wanted this page, we can skip the rest */
389881a256dSMatthew Wilcox 	if (page == 0)
39016d3ea26SMartin K. Petersen 		return 0;
391881a256dSMatthew Wilcox 
392bc8945dfSHannes Reinecke 	for (i = 4; i < min(result, buf_len); i++)
393bc8945dfSHannes Reinecke 		if (buf[i] == page)
394881a256dSMatthew Wilcox 			goto found;
395e3deec09SJames Bottomley 
396bc8945dfSHannes Reinecke 	if (i < result && i >= buf_len)
397e3deec09SJames Bottomley 		/* ran off the end of the buffer, give us benefit of doubt */
398e3deec09SJames Bottomley 		goto found;
399881a256dSMatthew Wilcox 	/* The device claims it doesn't support the requested page */
400881a256dSMatthew Wilcox 	goto fail;
401881a256dSMatthew Wilcox 
402881a256dSMatthew Wilcox  found:
403e3deec09SJames Bottomley 	result = scsi_vpd_inquiry(sdev, buf, page, buf_len);
404bc8945dfSHannes Reinecke 	if (result < 0)
405881a256dSMatthew Wilcox 		goto fail;
406881a256dSMatthew Wilcox 
407e3deec09SJames Bottomley 	return 0;
408881a256dSMatthew Wilcox 
409881a256dSMatthew Wilcox  fail:
410e3deec09SJames Bottomley 	return -EINVAL;
411881a256dSMatthew Wilcox }
412881a256dSMatthew Wilcox EXPORT_SYMBOL_GPL(scsi_get_vpd_page);
413881a256dSMatthew Wilcox 
414881a256dSMatthew Wilcox /**
4151e3f720aSBart Van Assche  * scsi_get_vpd_buf - Get Vital Product Data from a SCSI device
4161e3f720aSBart Van Assche  * @sdev: The device to ask
4171e3f720aSBart Van Assche  * @page: Which Vital Product Data to return
4181e3f720aSBart Van Assche  *
4191e3f720aSBart Van Assche  * Returns %NULL upon failure.
4201e3f720aSBart Van Assche  */
421ccf1e004SBart Van Assche static struct scsi_vpd *scsi_get_vpd_buf(struct scsi_device *sdev, u8 page)
4221e3f720aSBart Van Assche {
423ccf1e004SBart Van Assche 	struct scsi_vpd *vpd_buf;
4241e3f720aSBart Van Assche 	int vpd_len = SCSI_VPD_PG_LEN, result;
4251e3f720aSBart Van Assche 
4261e3f720aSBart Van Assche retry_pg:
427ccf1e004SBart Van Assche 	vpd_buf = kmalloc(sizeof(*vpd_buf) + vpd_len, GFP_KERNEL);
4281e3f720aSBart Van Assche 	if (!vpd_buf)
4291e3f720aSBart Van Assche 		return NULL;
4301e3f720aSBart Van Assche 
431ccf1e004SBart Van Assche 	result = scsi_vpd_inquiry(sdev, vpd_buf->data, page, vpd_len);
4321e3f720aSBart Van Assche 	if (result < 0) {
4331e3f720aSBart Van Assche 		kfree(vpd_buf);
4341e3f720aSBart Van Assche 		return NULL;
4351e3f720aSBart Van Assche 	}
4361e3f720aSBart Van Assche 	if (result > vpd_len) {
4371e3f720aSBart Van Assche 		vpd_len = result;
4381e3f720aSBart Van Assche 		kfree(vpd_buf);
4391e3f720aSBart Van Assche 		goto retry_pg;
4401e3f720aSBart Van Assche 	}
4411e3f720aSBart Van Assche 
442ccf1e004SBart Van Assche 	vpd_buf->len = result;
4431e3f720aSBart Van Assche 
4441e3f720aSBart Van Assche 	return vpd_buf;
4451e3f720aSBart Van Assche }
4461e3f720aSBart Van Assche 
4471e3f720aSBart Van Assche static void scsi_update_vpd_page(struct scsi_device *sdev, u8 page,
448ccf1e004SBart Van Assche 				 struct scsi_vpd __rcu **sdev_vpd_buf)
4491e3f720aSBart Van Assche {
450ccf1e004SBart Van Assche 	struct scsi_vpd *vpd_buf;
4511e3f720aSBart Van Assche 
452ccf1e004SBart Van Assche 	vpd_buf = scsi_get_vpd_buf(sdev, page);
4531e3f720aSBart Van Assche 	if (!vpd_buf)
4541e3f720aSBart Van Assche 		return;
4551e3f720aSBart Van Assche 
4561e3f720aSBart Van Assche 	mutex_lock(&sdev->inquiry_mutex);
4571e3f720aSBart Van Assche 	rcu_swap_protected(*sdev_vpd_buf, vpd_buf,
4581e3f720aSBart Van Assche 			   lockdep_is_held(&sdev->inquiry_mutex));
4591e3f720aSBart Van Assche 	mutex_unlock(&sdev->inquiry_mutex);
4601e3f720aSBart Van Assche 
461ccf1e004SBart Van Assche 	if (vpd_buf)
462ccf1e004SBart Van Assche 		kfree_rcu(vpd_buf, rcu);
4631e3f720aSBart Van Assche }
4641e3f720aSBart Van Assche 
4651e3f720aSBart Van Assche /**
466b3ae8780SHannes Reinecke  * scsi_attach_vpd - Attach Vital Product Data to a SCSI device structure
467b3ae8780SHannes Reinecke  * @sdev: The device to ask
468b3ae8780SHannes Reinecke  *
469b3ae8780SHannes Reinecke  * Attach the 'Device Identification' VPD page (0x83) and the
470b3ae8780SHannes Reinecke  * 'Unit Serial Number' VPD page (0x80) to a SCSI device
471b3ae8780SHannes Reinecke  * structure. This information can be used to identify the device
472b3ae8780SHannes Reinecke  * uniquely.
473b3ae8780SHannes Reinecke  */
474b3ae8780SHannes Reinecke void scsi_attach_vpd(struct scsi_device *sdev)
475b3ae8780SHannes Reinecke {
476ccf1e004SBart Van Assche 	int i;
477ccf1e004SBart Van Assche 	struct scsi_vpd *vpd_buf;
478b3ae8780SHannes Reinecke 
4795ddfe085SHannes Reinecke 	if (!scsi_device_supports_vpd(sdev))
480b3ae8780SHannes Reinecke 		return;
4815ddfe085SHannes Reinecke 
482b3ae8780SHannes Reinecke 	/* Ask for all the pages supported by this device */
483ccf1e004SBart Van Assche 	vpd_buf = scsi_get_vpd_buf(sdev, 0);
4841e3f720aSBart Van Assche 	if (!vpd_buf)
485b3ae8780SHannes Reinecke 		return;
486b3ae8780SHannes Reinecke 
487ccf1e004SBart Van Assche 	for (i = 4; i < vpd_buf->len; i++) {
488ccf1e004SBart Van Assche 		if (vpd_buf->data[i] == 0x80)
489ccf1e004SBart Van Assche 			scsi_update_vpd_page(sdev, 0x80, &sdev->vpd_pg80);
490ccf1e004SBart Van Assche 		if (vpd_buf->data[i] == 0x83)
491ccf1e004SBart Van Assche 			scsi_update_vpd_page(sdev, 0x83, &sdev->vpd_pg83);
492b3ae8780SHannes Reinecke 	}
493b3ae8780SHannes Reinecke 	kfree(vpd_buf);
494b3ae8780SHannes Reinecke }
495b3ae8780SHannes Reinecke 
496b3ae8780SHannes Reinecke /**
4973c6bdaeaSMartin K. Petersen  * scsi_report_opcode - Find out if a given command opcode is supported
4983c6bdaeaSMartin K. Petersen  * @sdev:	scsi device to query
4993c6bdaeaSMartin K. Petersen  * @buffer:	scratch buffer (must be at least 20 bytes long)
5003c6bdaeaSMartin K. Petersen  * @len:	length of buffer
5013c6bdaeaSMartin K. Petersen  * @opcode:	opcode for command to look up
5023c6bdaeaSMartin K. Petersen  *
5033c6bdaeaSMartin K. Petersen  * Uses the REPORT SUPPORTED OPERATION CODES to look up the given
50466c28f97SMartin K. Petersen  * opcode. Returns -EINVAL if RSOC fails, 0 if the command opcode is
50566c28f97SMartin K. Petersen  * unsupported and 1 if the device claims to support the command.
5063c6bdaeaSMartin K. Petersen  */
5073c6bdaeaSMartin K. Petersen int scsi_report_opcode(struct scsi_device *sdev, unsigned char *buffer,
5083c6bdaeaSMartin K. Petersen 		       unsigned int len, unsigned char opcode)
5093c6bdaeaSMartin K. Petersen {
5103c6bdaeaSMartin K. Petersen 	unsigned char cmd[16];
5113c6bdaeaSMartin K. Petersen 	struct scsi_sense_hdr sshdr;
5123c6bdaeaSMartin K. Petersen 	int result;
5133c6bdaeaSMartin K. Petersen 
5143c6bdaeaSMartin K. Petersen 	if (sdev->no_report_opcodes || sdev->scsi_level < SCSI_SPC_3)
51566c28f97SMartin K. Petersen 		return -EINVAL;
5163c6bdaeaSMartin K. Petersen 
5173c6bdaeaSMartin K. Petersen 	memset(cmd, 0, 16);
5183c6bdaeaSMartin K. Petersen 	cmd[0] = MAINTENANCE_IN;
5193c6bdaeaSMartin K. Petersen 	cmd[1] = MI_REPORT_SUPPORTED_OPERATION_CODES;
5203c6bdaeaSMartin K. Petersen 	cmd[2] = 1;		/* One command format */
5213c6bdaeaSMartin K. Petersen 	cmd[3] = opcode;
5223c6bdaeaSMartin K. Petersen 	put_unaligned_be32(len, &cmd[6]);
5233c6bdaeaSMartin K. Petersen 	memset(buffer, 0, len);
5243c6bdaeaSMartin K. Petersen 
5253c6bdaeaSMartin K. Petersen 	result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
5263c6bdaeaSMartin K. Petersen 				  &sshdr, 30 * HZ, 3, NULL);
5273c6bdaeaSMartin K. Petersen 
5283c6bdaeaSMartin K. Petersen 	if (result && scsi_sense_valid(&sshdr) &&
5293c6bdaeaSMartin K. Petersen 	    sshdr.sense_key == ILLEGAL_REQUEST &&
5303c6bdaeaSMartin K. Petersen 	    (sshdr.asc == 0x20 || sshdr.asc == 0x24) && sshdr.ascq == 0x00)
53166c28f97SMartin K. Petersen 		return -EINVAL;
5323c6bdaeaSMartin K. Petersen 
5333c6bdaeaSMartin K. Petersen 	if ((buffer[1] & 3) == 3) /* Command supported */
5343c6bdaeaSMartin K. Petersen 		return 1;
5353c6bdaeaSMartin K. Petersen 
5363c6bdaeaSMartin K. Petersen 	return 0;
5373c6bdaeaSMartin K. Petersen }
5383c6bdaeaSMartin K. Petersen EXPORT_SYMBOL(scsi_report_opcode);
5393c6bdaeaSMartin K. Petersen 
5403c6bdaeaSMartin K. Petersen /**
541eb44820cSRob Landley  * scsi_device_get  -  get an additional reference to a scsi_device
5421da177e4SLinus Torvalds  * @sdev:	device to get a reference to
5431da177e4SLinus Torvalds  *
544eb44820cSRob Landley  * Description: Gets a reference to the scsi_device and increments the use count
5451da177e4SLinus Torvalds  * of the underlying LLDD module.  You must hold host_lock of the
5461da177e4SLinus Torvalds  * parent Scsi_Host or already have a reference when calling this.
547cff549e4SChristoph Hellwig  *
548cff549e4SChristoph Hellwig  * This will fail if a device is deleted or cancelled, or when the LLD module
549cff549e4SChristoph Hellwig  * is in the process of being unloaded.
5501da177e4SLinus Torvalds  */
5511da177e4SLinus Torvalds int scsi_device_get(struct scsi_device *sdev)
5521da177e4SLinus Torvalds {
553cff549e4SChristoph Hellwig 	if (sdev->sdev_state == SDEV_DEL || sdev->sdev_state == SDEV_CANCEL)
554cff549e4SChristoph Hellwig 		goto fail;
5551da177e4SLinus Torvalds 	if (!get_device(&sdev->sdev_gendev))
556cff549e4SChristoph Hellwig 		goto fail;
557cff549e4SChristoph Hellwig 	if (!try_module_get(sdev->host->hostt->module))
558cff549e4SChristoph Hellwig 		goto fail_put_device;
5591da177e4SLinus Torvalds 	return 0;
560cff549e4SChristoph Hellwig 
561cff549e4SChristoph Hellwig fail_put_device:
562cff549e4SChristoph Hellwig 	put_device(&sdev->sdev_gendev);
563cff549e4SChristoph Hellwig fail:
564cff549e4SChristoph Hellwig 	return -ENXIO;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_get);
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds /**
5691da177e4SLinus Torvalds  * scsi_device_put  -  release a reference to a scsi_device
5701da177e4SLinus Torvalds  * @sdev:	device to release a reference on.
5711da177e4SLinus Torvalds  *
572eb44820cSRob Landley  * Description: Release a reference to the scsi_device and decrements the use
573eb44820cSRob Landley  * count of the underlying LLDD module.  The device is freed once the last
5741da177e4SLinus Torvalds  * user vanishes.
5751da177e4SLinus Torvalds  */
5761da177e4SLinus Torvalds void scsi_device_put(struct scsi_device *sdev)
5771da177e4SLinus Torvalds {
578dc4515eaSRusty Russell 	module_put(sdev->host->hostt->module);
5791da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
5801da177e4SLinus Torvalds }
5811da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_put);
5821da177e4SLinus Torvalds 
583eb44820cSRob Landley /* helper for shost_for_each_device, see that for documentation */
5841da177e4SLinus Torvalds struct scsi_device *__scsi_iterate_devices(struct Scsi_Host *shost,
5851da177e4SLinus Torvalds 					   struct scsi_device *prev)
5861da177e4SLinus Torvalds {
5871da177e4SLinus Torvalds 	struct list_head *list = (prev ? &prev->siblings : &shost->__devices);
5881da177e4SLinus Torvalds 	struct scsi_device *next = NULL;
5891da177e4SLinus Torvalds 	unsigned long flags;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
5921da177e4SLinus Torvalds 	while (list->next != &shost->__devices) {
5931da177e4SLinus Torvalds 		next = list_entry(list->next, struct scsi_device, siblings);
5941da177e4SLinus Torvalds 		/* skip devices that we can't get a reference to */
5951da177e4SLinus Torvalds 		if (!scsi_device_get(next))
5961da177e4SLinus Torvalds 			break;
5971da177e4SLinus Torvalds 		next = NULL;
5981da177e4SLinus Torvalds 		list = list->next;
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	if (prev)
6031da177e4SLinus Torvalds 		scsi_device_put(prev);
6041da177e4SLinus Torvalds 	return next;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_iterate_devices);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds /**
6091da177e4SLinus Torvalds  * starget_for_each_device  -  helper to walk all devices of a target
6101da177e4SLinus Torvalds  * @starget:	target whose devices we want to iterate over.
611eb44820cSRob Landley  * @data:	Opaque passed to each function call.
612eb44820cSRob Landley  * @fn:		Function to call on each device
6131da177e4SLinus Torvalds  *
614522939d4SMaciej W. Rozycki  * This traverses over each device of @starget.  The devices have
6151da177e4SLinus Torvalds  * a reference that must be released by scsi_host_put when breaking
6161da177e4SLinus Torvalds  * out of the loop.
6171da177e4SLinus Torvalds  */
6181da177e4SLinus Torvalds void starget_for_each_device(struct scsi_target *starget, void *data,
6191da177e4SLinus Torvalds 		     void (*fn)(struct scsi_device *, void *))
6201da177e4SLinus Torvalds {
6211da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
6221da177e4SLinus Torvalds 	struct scsi_device *sdev;
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	shost_for_each_device(sdev, shost) {
6251da177e4SLinus Torvalds 		if ((sdev->channel == starget->channel) &&
6261da177e4SLinus Torvalds 		    (sdev->id == starget->id))
6271da177e4SLinus Torvalds 			fn(sdev, data);
6281da177e4SLinus Torvalds 	}
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds EXPORT_SYMBOL(starget_for_each_device);
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds /**
63314f501a4SRandy Dunlap  * __starget_for_each_device - helper to walk all devices of a target (UNLOCKED)
634522939d4SMaciej W. Rozycki  * @starget:	target whose devices we want to iterate over.
63514f501a4SRandy Dunlap  * @data:	parameter for callback @fn()
63614f501a4SRandy Dunlap  * @fn:		callback function that is invoked for each device
637522939d4SMaciej W. Rozycki  *
638522939d4SMaciej W. Rozycki  * This traverses over each device of @starget.  It does _not_
639522939d4SMaciej W. Rozycki  * take a reference on the scsi_device, so the whole loop must be
640522939d4SMaciej W. Rozycki  * protected by shost->host_lock.
641522939d4SMaciej W. Rozycki  *
642522939d4SMaciej W. Rozycki  * Note:  The only reason why drivers would want to use this is because
643522939d4SMaciej W. Rozycki  * they need to access the device list in irq context.  Otherwise you
644522939d4SMaciej W. Rozycki  * really want to use starget_for_each_device instead.
645522939d4SMaciej W. Rozycki  **/
646522939d4SMaciej W. Rozycki void __starget_for_each_device(struct scsi_target *starget, void *data,
647522939d4SMaciej W. Rozycki 			       void (*fn)(struct scsi_device *, void *))
648522939d4SMaciej W. Rozycki {
649522939d4SMaciej W. Rozycki 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
650522939d4SMaciej W. Rozycki 	struct scsi_device *sdev;
651522939d4SMaciej W. Rozycki 
652522939d4SMaciej W. Rozycki 	__shost_for_each_device(sdev, shost) {
653522939d4SMaciej W. Rozycki 		if ((sdev->channel == starget->channel) &&
654522939d4SMaciej W. Rozycki 		    (sdev->id == starget->id))
655522939d4SMaciej W. Rozycki 			fn(sdev, data);
656522939d4SMaciej W. Rozycki 	}
657522939d4SMaciej W. Rozycki }
658522939d4SMaciej W. Rozycki EXPORT_SYMBOL(__starget_for_each_device);
659522939d4SMaciej W. Rozycki 
660522939d4SMaciej W. Rozycki /**
6611da177e4SLinus Torvalds  * __scsi_device_lookup_by_target - find a device given the target (UNLOCKED)
6621da177e4SLinus Torvalds  * @starget:	SCSI target pointer
6631da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
6641da177e4SLinus Torvalds  *
665eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @lun for a given
6661da177e4SLinus Torvalds  * @starget.  The returned scsi_device does not have an additional
6671da177e4SLinus Torvalds  * reference.  You must hold the host's host_lock over this call and
66832aeef60SHannes Reinecke  * any access to the returned scsi_device. A scsi_device in state
66932aeef60SHannes Reinecke  * SDEV_DEL is skipped.
6701da177e4SLinus Torvalds  *
671dc8875e1SRandy Dunlap  * Note:  The only reason why drivers should use this is because
672eb44820cSRob Landley  * they need to access the device list in irq context.  Otherwise you
6731da177e4SLinus Torvalds  * really want to use scsi_device_lookup_by_target instead.
6741da177e4SLinus Torvalds  **/
6751da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup_by_target(struct scsi_target *starget,
6769cb78c16SHannes Reinecke 						   u64 lun)
6771da177e4SLinus Torvalds {
6781da177e4SLinus Torvalds 	struct scsi_device *sdev;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	list_for_each_entry(sdev, &starget->devices, same_target_siblings) {
68132aeef60SHannes Reinecke 		if (sdev->sdev_state == SDEV_DEL)
68232aeef60SHannes Reinecke 			continue;
6831da177e4SLinus Torvalds 		if (sdev->lun ==lun)
6841da177e4SLinus Torvalds 			return sdev;
6851da177e4SLinus Torvalds 	}
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	return NULL;
6881da177e4SLinus Torvalds }
6891da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup_by_target);
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds /**
6921da177e4SLinus Torvalds  * scsi_device_lookup_by_target - find a device given the target
6931da177e4SLinus Torvalds  * @starget:	SCSI target pointer
6941da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
6951da177e4SLinus Torvalds  *
696477e608cSBartlomiej Zolnierkiewicz  * Description: Looks up the scsi_device with the specified @lun for a given
697477e608cSBartlomiej Zolnierkiewicz  * @starget.  The returned scsi_device has an additional reference that
698eb44820cSRob Landley  * needs to be released with scsi_device_put once you're done with it.
6991da177e4SLinus Torvalds  **/
7001da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup_by_target(struct scsi_target *starget,
7019cb78c16SHannes Reinecke 						 u64 lun)
7021da177e4SLinus Torvalds {
7031da177e4SLinus Torvalds 	struct scsi_device *sdev;
7041da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
7051da177e4SLinus Torvalds 	unsigned long flags;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
7081da177e4SLinus Torvalds 	sdev = __scsi_device_lookup_by_target(starget, lun);
7091da177e4SLinus Torvalds 	if (sdev && scsi_device_get(sdev))
7101da177e4SLinus Torvalds 		sdev = NULL;
7111da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	return sdev;
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup_by_target);
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /**
718eb44820cSRob Landley  * __scsi_device_lookup - find a device given the host (UNLOCKED)
7191da177e4SLinus Torvalds  * @shost:	SCSI host pointer
7201da177e4SLinus Torvalds  * @channel:	SCSI channel (zero if only one channel)
721eb44820cSRob Landley  * @id:		SCSI target number (physical unit number)
7221da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
7231da177e4SLinus Torvalds  *
724eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
725eb44820cSRob Landley  * for a given host. The returned scsi_device does not have an additional
726eb44820cSRob Landley  * reference.  You must hold the host's host_lock over this call and any access
727eb44820cSRob Landley  * to the returned scsi_device.
7281da177e4SLinus Torvalds  *
7291da177e4SLinus Torvalds  * Note:  The only reason why drivers would want to use this is because
730eb44820cSRob Landley  * they need to access the device list in irq context.  Otherwise you
7311da177e4SLinus Torvalds  * really want to use scsi_device_lookup instead.
7321da177e4SLinus Torvalds  **/
7331da177e4SLinus Torvalds struct scsi_device *__scsi_device_lookup(struct Scsi_Host *shost,
7349cb78c16SHannes Reinecke 		uint channel, uint id, u64 lun)
7351da177e4SLinus Torvalds {
7361da177e4SLinus Torvalds 	struct scsi_device *sdev;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	list_for_each_entry(sdev, &shost->__devices, siblings) {
7394ff7adc8SZhou Zhengping 		if (sdev->sdev_state == SDEV_DEL)
7404ff7adc8SZhou Zhengping 			continue;
7411da177e4SLinus Torvalds 		if (sdev->channel == channel && sdev->id == id &&
7421da177e4SLinus Torvalds 				sdev->lun ==lun)
7431da177e4SLinus Torvalds 			return sdev;
7441da177e4SLinus Torvalds 	}
7451da177e4SLinus Torvalds 
7461da177e4SLinus Torvalds 	return NULL;
7471da177e4SLinus Torvalds }
7481da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_device_lookup);
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds /**
7511da177e4SLinus Torvalds  * scsi_device_lookup - find a device given the host
7521da177e4SLinus Torvalds  * @shost:	SCSI host pointer
7531da177e4SLinus Torvalds  * @channel:	SCSI channel (zero if only one channel)
7541da177e4SLinus Torvalds  * @id:		SCSI target number (physical unit number)
7551da177e4SLinus Torvalds  * @lun:	SCSI Logical Unit Number
7561da177e4SLinus Torvalds  *
757eb44820cSRob Landley  * Description: Looks up the scsi_device with the specified @channel, @id, @lun
758eb44820cSRob Landley  * for a given host.  The returned scsi_device has an additional reference that
759eb44820cSRob Landley  * needs to be released with scsi_device_put once you're done with it.
7601da177e4SLinus Torvalds  **/
7611da177e4SLinus Torvalds struct scsi_device *scsi_device_lookup(struct Scsi_Host *shost,
7629cb78c16SHannes Reinecke 		uint channel, uint id, u64 lun)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	struct scsi_device *sdev;
7651da177e4SLinus Torvalds 	unsigned long flags;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
7681da177e4SLinus Torvalds 	sdev = __scsi_device_lookup(shost, channel, id, lun);
7691da177e4SLinus Torvalds 	if (sdev && scsi_device_get(sdev))
7701da177e4SLinus Torvalds 		sdev = NULL;
7711da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	return sdev;
7741da177e4SLinus Torvalds }
7751da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_device_lookup);
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds MODULE_DESCRIPTION("SCSI core");
7781da177e4SLinus Torvalds MODULE_LICENSE("GPL");
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds module_param(scsi_logging_level, int, S_IRUGO|S_IWUSR);
7811da177e4SLinus Torvalds MODULE_PARM_DESC(scsi_logging_level, "a bit mask of logging levels");
7821da177e4SLinus Torvalds 
783f664a3ccSJens Axboe /* This should go away in the future, it doesn't do anything anymore */
78424c20f10SChristoph Hellwig bool scsi_use_blk_mq = true;
785d285203cSChristoph Hellwig module_param_named(use_blk_mq, scsi_use_blk_mq, bool, S_IWUSR | S_IRUGO);
786d285203cSChristoph Hellwig 
7871da177e4SLinus Torvalds static int __init init_scsi(void)
7881da177e4SLinus Torvalds {
7899bf09c23SJens Axboe 	int error;
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds 	error = scsi_init_queue();
7921da177e4SLinus Torvalds 	if (error)
7931da177e4SLinus Torvalds 		return error;
7941da177e4SLinus Torvalds 	error = scsi_init_procfs();
7951da177e4SLinus Torvalds 	if (error)
7961da177e4SLinus Torvalds 		goto cleanup_queue;
7971da177e4SLinus Torvalds 	error = scsi_init_devinfo();
7981da177e4SLinus Torvalds 	if (error)
7991da177e4SLinus Torvalds 		goto cleanup_procfs;
8001da177e4SLinus Torvalds 	error = scsi_init_hosts();
8011da177e4SLinus Torvalds 	if (error)
8021da177e4SLinus Torvalds 		goto cleanup_devlist;
8031da177e4SLinus Torvalds 	error = scsi_init_sysctl();
8041da177e4SLinus Torvalds 	if (error)
8051da177e4SLinus Torvalds 		goto cleanup_hosts;
8061da177e4SLinus Torvalds 	error = scsi_sysfs_register();
8071da177e4SLinus Torvalds 	if (error)
8081da177e4SLinus Torvalds 		goto cleanup_sysctl;
8091da177e4SLinus Torvalds 
81084314fd4SJames Smart 	scsi_netlink_init();
81184314fd4SJames Smart 
8121da177e4SLinus Torvalds 	printk(KERN_NOTICE "SCSI subsystem initialized\n");
8131da177e4SLinus Torvalds 	return 0;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds cleanup_sysctl:
8161da177e4SLinus Torvalds 	scsi_exit_sysctl();
8171da177e4SLinus Torvalds cleanup_hosts:
8181da177e4SLinus Torvalds 	scsi_exit_hosts();
8191da177e4SLinus Torvalds cleanup_devlist:
8201da177e4SLinus Torvalds 	scsi_exit_devinfo();
8211da177e4SLinus Torvalds cleanup_procfs:
8221da177e4SLinus Torvalds 	scsi_exit_procfs();
8231da177e4SLinus Torvalds cleanup_queue:
8241da177e4SLinus Torvalds 	scsi_exit_queue();
8251da177e4SLinus Torvalds 	printk(KERN_ERR "SCSI subsystem failed to initialize, error = %d\n",
8261da177e4SLinus Torvalds 	       -error);
8271da177e4SLinus Torvalds 	return error;
8281da177e4SLinus Torvalds }
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds static void __exit exit_scsi(void)
8311da177e4SLinus Torvalds {
83284314fd4SJames Smart 	scsi_netlink_exit();
8331da177e4SLinus Torvalds 	scsi_sysfs_unregister();
8341da177e4SLinus Torvalds 	scsi_exit_sysctl();
8351da177e4SLinus Torvalds 	scsi_exit_hosts();
8361da177e4SLinus Torvalds 	scsi_exit_devinfo();
8371da177e4SLinus Torvalds 	scsi_exit_procfs();
8381da177e4SLinus Torvalds 	scsi_exit_queue();
839a4683487SDan Williams 	async_unregister_domain(&scsi_sd_probe_domain);
8401da177e4SLinus Torvalds }
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds subsys_initcall(init_scsi);
8431da177e4SLinus Torvalds module_exit(exit_scsi);
844