2019-05-19 20:08:55 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* hosts.c Copyright (C) 1992 Drew Eckhardt
|
|
|
|
* Copyright (C) 1993, 1994, 1995 Eric Youngdale
|
|
|
|
* Copyright (C) 2002-2003 Christoph Hellwig
|
|
|
|
*
|
|
|
|
* mid to lowlevel SCSI driver interface
|
|
|
|
* Initial versions: Drew Eckhardt
|
|
|
|
* Subsequent revisions: Eric Youngdale
|
|
|
|
*
|
|
|
|
* <drew@colorado.edu>
|
|
|
|
*
|
|
|
|
* Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
|
|
|
|
* Added QLOGIC QLA1280 SCSI controller kernel host support.
|
|
|
|
* August 4, 1999 Fred Lewis, Intel DuPont
|
|
|
|
*
|
|
|
|
* Updated to reflect the new initialization scheme for the higher
|
|
|
|
* level of scsi drivers (sd/sr/st)
|
|
|
|
* September 17, 2000 Torben Mathiasen <tmm@image.dk>
|
|
|
|
*
|
|
|
|
* Restructured scsi_host lists and associated functions.
|
|
|
|
* September 04, 2002 Mike Anderson (andmike@us.ibm.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/blkdev.h>
|
|
|
|
#include <linux/kernel.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 16:04:11 +08:00
|
|
|
#include <linux/slab.h>
|
2005-09-06 20:04:26 +08:00
|
|
|
#include <linux/kthread.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/completion.h>
|
|
|
|
#include <linux/transport_class.h>
|
2005-10-30 02:07:23 +08:00
|
|
|
#include <linux/platform_device.h>
|
[SCSI] implement runtime Power Management
This patch (as1398b) adds runtime PM support to the SCSI layer. Only
the machanism is provided; use of it is up to the various high-level
drivers, and the patch doesn't change any of them. Except for sg --
the patch expicitly prevents a device from being runtime-suspended
while its sg device file is open.
The implementation is simplistic. In general, hosts and targets are
automatically suspended when all their children are asleep, but for
them the runtime-suspend code doesn't actually do anything. (A host's
runtime PM status is propagated up the device tree, though, so a
runtime-PM-aware lower-level driver could power down the host adapter
hardware at the appropriate times.) There are comments indicating
where a transport class might be notified or some other hooks added.
LUNs are runtime-suspended by calling the drivers' existing suspend
handlers (and likewise for runtime-resume). Somewhat arbitrarily, the
implementation delays for 100 ms before suspending an eligible LUN.
This is because there typically are occasions during bootup when the
same device file is opened and closed several times in quick
succession.
The way this all works is that the SCSI core increments a device's
PM-usage count when it is registered. If a high-level driver does
nothing then the device will not be eligible for runtime-suspend
because of the elevated usage count. If a high-level driver wants to
use runtime PM then it can call scsi_autopm_put_device() in its probe
routine to decrement the usage count and scsi_autopm_get_device() in
its remove routine to restore the original count.
Hosts, targets, and LUNs are not suspended while they are being probed
or removed, or while the error handler is running. In fact, a fairly
large part of the patch consists of code to make sure that things
aren't suspended at such times.
[jejb: fix up compile issues in PM config variations]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: James Bottomley <James.Bottomley@suse.de>
2010-06-17 22:41:42 +08:00
|
|
|
#include <linux/pm_runtime.h>
|
2016-01-21 03:48:00 +08:00
|
|
|
#include <linux/idr.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <scsi/scsi_device.h>
|
|
|
|
#include <scsi/scsi_host.h>
|
|
|
|
#include <scsi/scsi_transport.h>
|
2019-10-25 14:58:55 +08:00
|
|
|
#include <scsi/scsi_cmnd.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#include "scsi_priv.h"
|
|
|
|
#include "scsi_logging.h"
|
|
|
|
|
|
|
|
|
2018-02-13 02:41:29 +08:00
|
|
|
static int shost_eh_deadline = -1;
|
|
|
|
|
|
|
|
module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR);
|
|
|
|
MODULE_PARM_DESC(eh_deadline,
|
|
|
|
"SCSI EH timeout in seconds (should be between 0 and 2^31-1)");
|
|
|
|
|
2016-01-21 03:48:00 +08:00
|
|
|
static DEFINE_IDA(host_index_ida);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
|
2008-02-22 07:13:36 +08:00
|
|
|
static void scsi_host_cls_release(struct device *dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-02-22 07:13:36 +08:00
|
|
|
put_device(&class_to_shost(dev)->shost_gendev);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct class shost_class = {
|
|
|
|
.name = "scsi_host",
|
2008-02-22 07:13:36 +08:00
|
|
|
.dev_release = scsi_host_cls_release,
|
2021-11-17 06:31:15 +08:00
|
|
|
.dev_groups = scsi_shost_groups,
|
2005-04-17 06:20:36 +08:00
|
|
|
};
|
|
|
|
|
2005-06-17 02:12:38 +08:00
|
|
|
/**
|
2007-11-04 02:30:39 +08:00
|
|
|
* scsi_host_set_state - Take the given host through the host state model.
|
2005-06-17 02:12:38 +08:00
|
|
|
* @shost: scsi host to change the state of.
|
|
|
|
* @state: state to change to.
|
|
|
|
*
|
|
|
|
* Returns zero if unsuccessful or an error if the requested
|
|
|
|
* transition is illegal.
|
|
|
|
**/
|
|
|
|
int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
|
|
|
|
{
|
|
|
|
enum scsi_host_state oldstate = shost->shost_state;
|
|
|
|
|
|
|
|
if (state == oldstate)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case SHOST_CREATED:
|
|
|
|
/* There are no legal states that come back to
|
|
|
|
* created. This is the manually initialised start
|
|
|
|
* state */
|
|
|
|
goto illegal;
|
|
|
|
|
|
|
|
case SHOST_RUNNING:
|
|
|
|
switch (oldstate) {
|
|
|
|
case SHOST_CREATED:
|
|
|
|
case SHOST_RECOVERY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto illegal;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHOST_RECOVERY:
|
|
|
|
switch (oldstate) {
|
|
|
|
case SHOST_RUNNING:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto illegal;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHOST_CANCEL:
|
|
|
|
switch (oldstate) {
|
|
|
|
case SHOST_CREATED:
|
|
|
|
case SHOST_RUNNING:
|
2005-09-19 04:05:20 +08:00
|
|
|
case SHOST_CANCEL_RECOVERY:
|
2005-06-17 02:12:38 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto illegal;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHOST_DEL:
|
|
|
|
switch (oldstate) {
|
|
|
|
case SHOST_CANCEL:
|
2005-09-19 04:05:20 +08:00
|
|
|
case SHOST_DEL_RECOVERY:
|
2005-06-17 02:12:38 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto illegal;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2005-09-19 04:05:20 +08:00
|
|
|
case SHOST_CANCEL_RECOVERY:
|
|
|
|
switch (oldstate) {
|
|
|
|
case SHOST_CANCEL:
|
|
|
|
case SHOST_RECOVERY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto illegal;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHOST_DEL_RECOVERY:
|
|
|
|
switch (oldstate) {
|
|
|
|
case SHOST_CANCEL_RECOVERY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto illegal;
|
|
|
|
}
|
|
|
|
break;
|
2005-06-17 02:12:38 +08:00
|
|
|
}
|
|
|
|
shost->shost_state = state;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
illegal:
|
|
|
|
SCSI_LOG_ERROR_RECOVERY(1,
|
2005-10-03 00:45:08 +08:00
|
|
|
shost_printk(KERN_ERR, shost,
|
|
|
|
"Illegal host state transition"
|
|
|
|
"%s->%s\n",
|
|
|
|
scsi_host_state_name(oldstate),
|
|
|
|
scsi_host_state_name(state)));
|
2005-06-17 02:12:38 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/**
|
|
|
|
* scsi_remove_host - remove a scsi host
|
|
|
|
* @shost: a pointer to a scsi host to remove
|
|
|
|
**/
|
|
|
|
void scsi_remove_host(struct Scsi_Host *shost)
|
|
|
|
{
|
2005-09-19 04:05:20 +08:00
|
|
|
unsigned long flags;
|
[SCSI] implement runtime Power Management
This patch (as1398b) adds runtime PM support to the SCSI layer. Only
the machanism is provided; use of it is up to the various high-level
drivers, and the patch doesn't change any of them. Except for sg --
the patch expicitly prevents a device from being runtime-suspended
while its sg device file is open.
The implementation is simplistic. In general, hosts and targets are
automatically suspended when all their children are asleep, but for
them the runtime-suspend code doesn't actually do anything. (A host's
runtime PM status is propagated up the device tree, though, so a
runtime-PM-aware lower-level driver could power down the host adapter
hardware at the appropriate times.) There are comments indicating
where a transport class might be notified or some other hooks added.
LUNs are runtime-suspended by calling the drivers' existing suspend
handlers (and likewise for runtime-resume). Somewhat arbitrarily, the
implementation delays for 100 ms before suspending an eligible LUN.
This is because there typically are occasions during bootup when the
same device file is opened and closed several times in quick
succession.
The way this all works is that the SCSI core increments a device's
PM-usage count when it is registered. If a high-level driver does
nothing then the device will not be eligible for runtime-suspend
because of the elevated usage count. If a high-level driver wants to
use runtime PM then it can call scsi_autopm_put_device() in its probe
routine to decrement the usage count and scsi_autopm_get_device() in
its remove routine to restore the original count.
Hosts, targets, and LUNs are not suspended while they are being probed
or removed, or while the error handler is running. In fact, a fairly
large part of the patch consists of code to make sure that things
aren't suspended at such times.
[jejb: fix up compile issues in PM config variations]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: James Bottomley <James.Bottomley@suse.de>
2010-06-17 22:41:42 +08:00
|
|
|
|
2006-01-11 20:16:10 +08:00
|
|
|
mutex_lock(&shost->scan_mutex);
|
2005-09-19 04:05:20 +08:00
|
|
|
spin_lock_irqsave(shost->host_lock, flags);
|
|
|
|
if (scsi_host_set_state(shost, SHOST_CANCEL))
|
|
|
|
if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) {
|
|
|
|
spin_unlock_irqrestore(shost->host_lock, flags);
|
2006-01-11 20:16:10 +08:00
|
|
|
mutex_unlock(&shost->scan_mutex);
|
2005-09-19 04:05:20 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(shost->host_lock, flags);
|
[SCSI] implement runtime Power Management
This patch (as1398b) adds runtime PM support to the SCSI layer. Only
the machanism is provided; use of it is up to the various high-level
drivers, and the patch doesn't change any of them. Except for sg --
the patch expicitly prevents a device from being runtime-suspended
while its sg device file is open.
The implementation is simplistic. In general, hosts and targets are
automatically suspended when all their children are asleep, but for
them the runtime-suspend code doesn't actually do anything. (A host's
runtime PM status is propagated up the device tree, though, so a
runtime-PM-aware lower-level driver could power down the host adapter
hardware at the appropriate times.) There are comments indicating
where a transport class might be notified or some other hooks added.
LUNs are runtime-suspended by calling the drivers' existing suspend
handlers (and likewise for runtime-resume). Somewhat arbitrarily, the
implementation delays for 100 ms before suspending an eligible LUN.
This is because there typically are occasions during bootup when the
same device file is opened and closed several times in quick
succession.
The way this all works is that the SCSI core increments a device's
PM-usage count when it is registered. If a high-level driver does
nothing then the device will not be eligible for runtime-suspend
because of the elevated usage count. If a high-level driver wants to
use runtime PM then it can call scsi_autopm_put_device() in its probe
routine to decrement the usage count and scsi_autopm_get_device() in
its remove routine to restore the original count.
Hosts, targets, and LUNs are not suspended while they are being probed
or removed, or while the error handler is running. In fact, a fairly
large part of the patch consists of code to make sure that things
aren't suspended at such times.
[jejb: fix up compile issues in PM config variations]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: James Bottomley <James.Bottomley@suse.de>
2010-06-17 22:41:42 +08:00
|
|
|
|
|
|
|
scsi_autopm_get_host(shost);
|
2013-11-11 20:44:54 +08:00
|
|
|
flush_workqueue(shost->tmf_work_q);
|
2005-04-17 06:20:36 +08:00
|
|
|
scsi_forget_host(shost);
|
2009-11-18 06:10:11 +08:00
|
|
|
mutex_unlock(&shost->scan_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
scsi_proc_host_rm(shost);
|
|
|
|
|
2022-08-26 08:26:34 +08:00
|
|
|
/*
|
|
|
|
* New SCSI devices cannot be attached anymore because of the SCSI host
|
|
|
|
* state so drop the tag set refcnt. Wait until the tag set refcnt drops
|
|
|
|
* to zero because .exit_cmd_priv implementations may need the host
|
|
|
|
* pointer.
|
|
|
|
*/
|
|
|
|
kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
|
|
|
|
wait_for_completion(&shost->tagset_freed);
|
|
|
|
|
2005-09-19 04:05:20 +08:00
|
|
|
spin_lock_irqsave(shost->host_lock, flags);
|
|
|
|
if (scsi_host_set_state(shost, SHOST_DEL))
|
|
|
|
BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
|
|
|
|
spin_unlock_irqrestore(shost->host_lock, flags);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
transport_unregister_device(&shost->shost_gendev);
|
2008-02-22 07:13:36 +08:00
|
|
|
device_unregister(&shost->shost_dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
device_del(&shost->shost_gendev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(scsi_remove_host);
|
|
|
|
|
|
|
|
/**
|
2009-11-06 03:33:12 +08:00
|
|
|
* scsi_add_host_with_dma - add a scsi host with dma device
|
2005-04-17 06:20:36 +08:00
|
|
|
* @shost: scsi host pointer to add
|
|
|
|
* @dev: a struct device of type scsi class
|
2009-11-06 03:33:12 +08:00
|
|
|
* @dma_dev: dma device for the host
|
|
|
|
*
|
|
|
|
* Note: You rarely need to worry about this unless you're in a
|
|
|
|
* virtualised host environments, so use the simpler scsi_add_host()
|
|
|
|
* function instead.
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Return value:
|
|
|
|
* 0 on success / != 0 for error
|
|
|
|
**/
|
2009-11-06 03:33:12 +08:00
|
|
|
int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
|
|
|
|
struct device *dma_dev)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct scsi_host_template *sht = shost->hostt;
|
|
|
|
int error = -EINVAL;
|
|
|
|
|
2014-06-25 22:39:59 +08:00
|
|
|
shost_printk(KERN_INFO, shost, "%s\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
sht->info ? sht->info(shost) : sht->name);
|
|
|
|
|
|
|
|
if (!shost->can_queue) {
|
2014-06-25 22:39:59 +08:00
|
|
|
shost_printk(KERN_ERR, shost,
|
|
|
|
"can_queue = 0 no longer supported\n");
|
2008-04-21 23:57:20 +08:00
|
|
|
goto fail;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-08 12:35:46 +08:00
|
|
|
/* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */
|
|
|
|
shost->cmd_per_lun = min_t(int, shost->cmd_per_lun,
|
2021-05-19 22:31:02 +08:00
|
|
|
shost->can_queue);
|
|
|
|
|
2017-01-03 13:28:41 +08:00
|
|
|
error = scsi_init_sense_cache(shost);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!shost->shost_gendev.parent)
|
|
|
|
shost->shost_gendev.parent = dev ? dev : &platform_bus;
|
2012-05-04 17:40:04 +08:00
|
|
|
if (!dma_dev)
|
|
|
|
dma_dev = shost->shost_gendev.parent;
|
|
|
|
|
2009-11-06 03:33:12 +08:00
|
|
|
shost->dma_dev = dma_dev;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2022-07-14 19:15:26 +08:00
|
|
|
if (dma_dev->dma_mask) {
|
|
|
|
shost->max_sectors = min_t(unsigned int, shost->max_sectors,
|
|
|
|
dma_max_mapping_size(dma_dev) >> SECTOR_SHIFT);
|
|
|
|
}
|
|
|
|
|
2022-03-30 19:38:35 +08:00
|
|
|
error = scsi_mq_setup_tags(shost);
|
|
|
|
if (error)
|
|
|
|
goto fail;
|
|
|
|
|
2022-08-26 08:26:34 +08:00
|
|
|
kref_init(&shost->tagset_refcnt);
|
|
|
|
init_completion(&shost->tagset_freed);
|
|
|
|
|
2016-02-18 16:54:13 +08:00
|
|
|
/*
|
|
|
|
* Increase usage count temporarily here so that calling
|
|
|
|
* scsi_autopm_put_host() will trigger runtime idle if there is
|
|
|
|
* nothing else preventing suspending the device.
|
|
|
|
*/
|
|
|
|
pm_runtime_get_noresume(&shost->shost_gendev);
|
[SCSI] implement runtime Power Management
This patch (as1398b) adds runtime PM support to the SCSI layer. Only
the machanism is provided; use of it is up to the various high-level
drivers, and the patch doesn't change any of them. Except for sg --
the patch expicitly prevents a device from being runtime-suspended
while its sg device file is open.
The implementation is simplistic. In general, hosts and targets are
automatically suspended when all their children are asleep, but for
them the runtime-suspend code doesn't actually do anything. (A host's
runtime PM status is propagated up the device tree, though, so a
runtime-PM-aware lower-level driver could power down the host adapter
hardware at the appropriate times.) There are comments indicating
where a transport class might be notified or some other hooks added.
LUNs are runtime-suspended by calling the drivers' existing suspend
handlers (and likewise for runtime-resume). Somewhat arbitrarily, the
implementation delays for 100 ms before suspending an eligible LUN.
This is because there typically are occasions during bootup when the
same device file is opened and closed several times in quick
succession.
The way this all works is that the SCSI core increments a device's
PM-usage count when it is registered. If a high-level driver does
nothing then the device will not be eligible for runtime-suspend
because of the elevated usage count. If a high-level driver wants to
use runtime PM then it can call scsi_autopm_put_device() in its probe
routine to decrement the usage count and scsi_autopm_get_device() in
its remove routine to restore the original count.
Hosts, targets, and LUNs are not suspended while they are being probed
or removed, or while the error handler is running. In fact, a fairly
large part of the patch consists of code to make sure that things
aren't suspended at such times.
[jejb: fix up compile issues in PM config variations]
Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: James Bottomley <James.Bottomley@suse.de>
2010-06-17 22:41:42 +08:00
|
|
|
pm_runtime_set_active(&shost->shost_gendev);
|
|
|
|
pm_runtime_enable(&shost->shost_gendev);
|
|
|
|
device_enable_async_suspend(&shost->shost_gendev);
|
|
|
|
|
2016-08-04 03:49:03 +08:00
|
|
|
error = device_add(&shost->shost_gendev);
|
|
|
|
if (error)
|
2017-01-03 02:55:26 +08:00
|
|
|
goto out_disable_runtime_pm;
|
2016-08-04 03:49:03 +08:00
|
|
|
|
2005-06-17 02:12:38 +08:00
|
|
|
scsi_host_set_state(shost, SHOST_RUNNING);
|
2005-04-17 06:20:36 +08:00
|
|
|
get_device(shost->shost_gendev.parent);
|
|
|
|
|
2010-02-09 02:18:26 +08:00
|
|
|
device_enable_async_suspend(&shost->shost_dev);
|
|
|
|
|
2021-06-02 21:30:28 +08:00
|
|
|
get_device(&shost->shost_gendev);
|
2008-02-22 07:13:36 +08:00
|
|
|
error = device_add(&shost->shost_dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (error)
|
|
|
|
goto out_del_gendev;
|
|
|
|
|
2008-03-22 05:18:23 +08:00
|
|
|
if (shost->transportt->host_size) {
|
|
|
|
shost->shost_data = kzalloc(shost->transportt->host_size,
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (shost->shost_data == NULL) {
|
|
|
|
error = -ENOMEM;
|
2008-02-22 07:13:36 +08:00
|
|
|
goto out_del_dev;
|
2008-03-22 05:18:23 +08:00
|
|
|
}
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (shost->transportt->create_work_queue) {
|
2008-05-02 12:02:41 +08:00
|
|
|
snprintf(shost->work_q_name, sizeof(shost->work_q_name),
|
|
|
|
"scsi_wq_%d", shost->host_no);
|
2020-07-01 11:07:45 +08:00
|
|
|
shost->work_q = alloc_workqueue("%s",
|
|
|
|
WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
|
|
|
|
1, shost->work_q_name);
|
|
|
|
|
2008-03-22 05:18:23 +08:00
|
|
|
if (!shost->work_q) {
|
|
|
|
error = -EINVAL;
|
2021-06-02 21:30:27 +08:00
|
|
|
goto out_del_dev;
|
2008-03-22 05:18:23 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
error = scsi_sysfs_add_host(shost);
|
|
|
|
if (error)
|
2021-06-02 21:30:27 +08:00
|
|
|
goto out_del_dev;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
scsi_proc_host_add(shost);
|
2016-02-18 16:54:13 +08:00
|
|
|
scsi_autopm_put_host(shost);
|
2005-04-17 06:20:36 +08:00
|
|
|
return error;
|
|
|
|
|
2021-06-02 21:30:27 +08:00
|
|
|
/*
|
2022-08-22 06:04:59 +08:00
|
|
|
* Any host allocation in this function will be freed in
|
|
|
|
* scsi_host_dev_release().
|
2021-06-02 21:30:27 +08:00
|
|
|
*/
|
2008-02-22 07:13:36 +08:00
|
|
|
out_del_dev:
|
|
|
|
device_del(&shost->shost_dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
out_del_gendev:
|
2021-06-02 21:30:28 +08:00
|
|
|
/*
|
|
|
|
* Host state is SHOST_RUNNING so we have to explicitly release
|
|
|
|
* ->shost_dev.
|
|
|
|
*/
|
|
|
|
put_device(&shost->shost_dev);
|
2005-04-17 06:20:36 +08:00
|
|
|
device_del(&shost->shost_gendev);
|
2017-01-03 02:55:26 +08:00
|
|
|
out_disable_runtime_pm:
|
2016-08-04 03:49:03 +08:00
|
|
|
device_disable_async_suspend(&shost->shost_gendev);
|
|
|
|
pm_runtime_disable(&shost->shost_gendev);
|
|
|
|
pm_runtime_set_suspended(&shost->shost_gendev);
|
|
|
|
pm_runtime_put_noidle(&shost->shost_gendev);
|
2022-08-26 08:26:34 +08:00
|
|
|
kref_put(&shost->tagset_refcnt, scsi_mq_free_tags);
|
2008-04-21 23:57:20 +08:00
|
|
|
fail:
|
2005-04-17 06:20:36 +08:00
|
|
|
return error;
|
|
|
|
}
|
2009-11-06 03:33:12 +08:00
|
|
|
EXPORT_SYMBOL(scsi_add_host_with_dma);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static void scsi_host_dev_release(struct device *dev)
|
|
|
|
{
|
|
|
|
struct Scsi_Host *shost = dev_to_shost(dev);
|
|
|
|
struct device *parent = dev->parent;
|
|
|
|
|
2009-02-28 05:51:42 +08:00
|
|
|
scsi_proc_hostdir_rm(shost->hostt);
|
|
|
|
|
2021-05-10 19:35:26 +08:00
|
|
|
/* Wait for functions invoked through call_rcu(&scmd->rcu, ...) */
|
2017-12-05 02:06:23 +08:00
|
|
|
rcu_barrier();
|
|
|
|
|
2013-11-11 20:44:54 +08:00
|
|
|
if (shost->tmf_work_q)
|
|
|
|
destroy_workqueue(shost->tmf_work_q);
|
2005-09-06 20:04:26 +08:00
|
|
|
if (shost->ehandler)
|
|
|
|
kthread_stop(shost->ehandler);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (shost->work_q)
|
|
|
|
destroy_workqueue(shost->work_q);
|
|
|
|
|
2015-11-19 06:56:36 +08:00
|
|
|
if (shost->shost_state == SHOST_CREATED) {
|
|
|
|
/*
|
|
|
|
* Free the shost_dev device name here if scsi_host_alloc()
|
|
|
|
* and scsi_host_put() have been called but neither
|
|
|
|
* scsi_host_add() nor scsi_host_remove() has been called.
|
|
|
|
* This avoids that the memory allocated for the shost_dev
|
|
|
|
* name is leaked.
|
|
|
|
*/
|
|
|
|
kfree(dev_name(&shost->shost_dev));
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
kfree(shost->shost_data);
|
|
|
|
|
2022-05-27 16:30:49 +08:00
|
|
|
ida_free(&host_index_ida, shost->host_no);
|
2016-01-21 03:48:00 +08:00
|
|
|
|
2021-06-02 21:30:29 +08:00
|
|
|
if (shost->shost_state != SHOST_CREATED)
|
2005-04-17 06:20:36 +08:00
|
|
|
put_device(parent);
|
|
|
|
kfree(shost);
|
|
|
|
}
|
|
|
|
|
2008-07-04 14:47:33 +08:00
|
|
|
static struct device_type scsi_host_type = {
|
2008-03-18 21:32:28 +08:00
|
|
|
.name = "scsi_host",
|
|
|
|
.release = scsi_host_dev_release,
|
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/**
|
|
|
|
* scsi_host_alloc - register a scsi host adapter instance.
|
|
|
|
* @sht: pointer to scsi host template
|
|
|
|
* @privsize: extra bytes to allocate for driver
|
|
|
|
*
|
|
|
|
* Note:
|
|
|
|
* Allocate a new Scsi_Host and perform basic initialization.
|
|
|
|
* The host is not published to the scsi midlayer until scsi_add_host
|
|
|
|
* is called.
|
|
|
|
*
|
|
|
|
* Return value:
|
|
|
|
* Pointer to a new Scsi_Host
|
|
|
|
**/
|
|
|
|
struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
|
|
|
|
{
|
|
|
|
struct Scsi_Host *shost;
|
2021-11-17 06:31:15 +08:00
|
|
|
int index;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2021-03-31 15:29:58 +08:00
|
|
|
shost = kzalloc(sizeof(struct Scsi_Host) + privsize, GFP_KERNEL);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!shost)
|
|
|
|
return NULL;
|
|
|
|
|
2006-11-05 03:11:36 +08:00
|
|
|
shost->host_lock = &shost->default_lock;
|
|
|
|
spin_lock_init(shost->host_lock);
|
2005-06-17 02:12:38 +08:00
|
|
|
shost->shost_state = SHOST_CREATED;
|
2005-04-17 06:20:36 +08:00
|
|
|
INIT_LIST_HEAD(&shost->__devices);
|
|
|
|
INIT_LIST_HEAD(&shost->__targets);
|
2021-10-30 03:43:10 +08:00
|
|
|
INIT_LIST_HEAD(&shost->eh_abort_list);
|
2005-04-17 06:20:36 +08:00
|
|
|
INIT_LIST_HEAD(&shost->eh_cmd_q);
|
|
|
|
INIT_LIST_HEAD(&shost->starved_list);
|
|
|
|
init_waitqueue_head(&shost->host_wait);
|
2006-01-11 20:16:10 +08:00
|
|
|
mutex_init(&shost->scan_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2022-05-27 16:30:49 +08:00
|
|
|
index = ida_alloc(&host_index_ida, GFP_KERNEL);
|
2021-06-02 21:30:26 +08:00
|
|
|
if (index < 0) {
|
|
|
|
kfree(shost);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-21 03:48:00 +08:00
|
|
|
shost->host_no = index;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
shost->dma_channel = 0xff;
|
|
|
|
|
|
|
|
/* These three are default values which can be overridden */
|
|
|
|
shost->max_channel = 0;
|
|
|
|
shost->max_id = 8;
|
|
|
|
shost->max_lun = 8;
|
|
|
|
|
|
|
|
/* Give each shost a default transportt */
|
|
|
|
shost->transportt = &blank_transport_template;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All drivers right now should be able to handle 12 byte
|
|
|
|
* commands. Every so often there are requests for 16 byte
|
|
|
|
* commands, but individual low-level drivers need to certify that
|
|
|
|
* they actually do something sensible with such commands.
|
|
|
|
*/
|
|
|
|
shost->max_cmd_len = 12;
|
|
|
|
shost->hostt = sht;
|
|
|
|
shost->this_id = sht->this_id;
|
|
|
|
shost->can_queue = sht->can_queue;
|
|
|
|
shost->sg_tablesize = sht->sg_tablesize;
|
2010-09-11 02:50:10 +08:00
|
|
|
shost->sg_prot_tablesize = sht->sg_prot_tablesize;
|
2005-04-17 06:20:36 +08:00
|
|
|
shost->cmd_per_lun = sht->cmd_per_lun;
|
2013-10-23 18:25:40 +08:00
|
|
|
shost->no_write_same = sht->no_write_same;
|
2020-08-19 23:20:30 +08:00
|
|
|
shost->host_tagset = sht->host_tagset;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2014-01-17 23:05:38 +08:00
|
|
|
if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
|
2013-11-11 20:44:56 +08:00
|
|
|
shost->eh_deadline = -1;
|
|
|
|
else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
|
|
|
|
shost_printk(KERN_WARNING, shost,
|
|
|
|
"eh_deadline %u too large, setting to %u\n",
|
|
|
|
shost_eh_deadline, INT_MAX / HZ);
|
|
|
|
shost->eh_deadline = INT_MAX;
|
|
|
|
} else
|
|
|
|
shost->eh_deadline = shost_eh_deadline * HZ;
|
|
|
|
|
2007-09-26 11:45:53 +08:00
|
|
|
if (sht->supported_mode == MODE_UNKNOWN)
|
|
|
|
/* means we didn't set it ... default to INITIATOR */
|
|
|
|
shost->active_mode = MODE_INITIATOR;
|
|
|
|
else
|
|
|
|
shost->active_mode = sht->supported_mode;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (sht->max_host_blocked)
|
|
|
|
shost->max_host_blocked = sht->max_host_blocked;
|
|
|
|
else
|
|
|
|
shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the driver imposes no hard sector transfer limit, start at
|
|
|
|
* machine infinity initially.
|
|
|
|
*/
|
|
|
|
if (sht->max_sectors)
|
|
|
|
shost->max_sectors = sht->max_sectors;
|
|
|
|
else
|
|
|
|
shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
|
|
|
|
|
2018-12-13 23:17:03 +08:00
|
|
|
if (sht->max_segment_size)
|
|
|
|
shost->max_segment_size = sht->max_segment_size;
|
|
|
|
else
|
|
|
|
shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* assume a 4GB boundary, if not set
|
|
|
|
*/
|
|
|
|
if (sht->dma_boundary)
|
|
|
|
shost->dma_boundary = sht->dma_boundary;
|
|
|
|
else
|
|
|
|
shost->dma_boundary = 0xffffffff;
|
|
|
|
|
2019-06-17 20:19:53 +08:00
|
|
|
if (sht->virt_boundary_mask)
|
|
|
|
shost->virt_boundary_mask = sht->virt_boundary_mask;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
device_initialize(&shost->shost_gendev);
|
2008-12-04 05:41:36 +08:00
|
|
|
dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
|
2008-03-18 21:32:28 +08:00
|
|
|
shost->shost_gendev.bus = &scsi_bus_type;
|
|
|
|
shost->shost_gendev.type = &scsi_host_type;
|
2021-10-07 05:54:51 +08:00
|
|
|
scsi_enable_async_suspend(&shost->shost_gendev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-02-22 07:13:36 +08:00
|
|
|
device_initialize(&shost->shost_dev);
|
|
|
|
shost->shost_dev.parent = &shost->shost_gendev;
|
|
|
|
shost->shost_dev.class = &shost_class;
|
2008-12-04 05:41:36 +08:00
|
|
|
dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
|
2021-11-17 06:31:15 +08:00
|
|
|
shost->shost_dev.groups = sht->shost_groups;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-09-06 20:04:26 +08:00
|
|
|
shost->ehandler = kthread_run(scsi_error_handler, shost,
|
|
|
|
"scsi_eh_%d", shost->host_no);
|
|
|
|
if (IS_ERR(shost->ehandler)) {
|
2014-06-25 22:39:59 +08:00
|
|
|
shost_printk(KERN_WARNING, shost,
|
|
|
|
"error handler thread failed to spawn, error = %ld\n",
|
|
|
|
PTR_ERR(shost->ehandler));
|
scsi: core: Fix bad pointer dereference when ehandler kthread is invalid
Commit 66a834d09293 ("scsi: core: Fix error handling of scsi_host_alloc()")
changed the allocation logic to call put_device() to perform host cleanup
with the assumption that IDA removal and stopping the kthread would
properly be performed in scsi_host_dev_release(). However, in the unlikely
case that the error handler thread fails to spawn, shost->ehandler is set
to ERR_PTR(-ENOMEM).
The error handler cleanup code in scsi_host_dev_release() will call
kthread_stop() if shost->ehandler != NULL which will always be the case
whether the kthread was successfully spawned or not. In the case that it
failed to spawn this has the nasty side effect of trying to dereference an
invalid pointer when kthread_stop() is called. The following splat provides
an example of this behavior in the wild:
scsi host11: error handler thread failed to spawn, error = -4
Kernel attempted to read user page (10c) - exploit attempt? (uid: 0)
BUG: Kernel NULL pointer dereference on read at 0x0000010c
Faulting instruction address: 0xc00000000818e9a8
Oops: Kernel access of bad area, sig: 11 [#1]
LE PAGE_SIZE=64K MMU=Hash SMP NR_CPUS=2048 NUMA pSeries
Modules linked in: ibmvscsi(+) scsi_transport_srp dm_multipath dm_mirror dm_region
hash dm_log dm_mod fuse overlay squashfs loop
CPU: 12 PID: 274 Comm: systemd-udevd Not tainted 5.13.0-rc7 #1
NIP: c00000000818e9a8 LR: c0000000089846e8 CTR: 0000000000007ee8
REGS: c000000037d12ea0 TRAP: 0300 Not tainted (5.13.0-rc7)
MSR: 800000000280b033 <SF,VEC,VSX,EE,FP,ME,IR,DR,RI,LE> CR: 28228228
XER: 20040001
CFAR: c0000000089846e4 DAR: 000000000000010c DSISR: 40000000 IRQMASK: 0
GPR00: c0000000089846e8 c000000037d13140 c000000009cc1100 fffffffffffffffc
GPR04: 0000000000000001 0000000000000000 0000000000000000 c000000037dc0000
GPR08: 0000000000000000 c000000037dc0000 0000000000000001 00000000fffff7ff
GPR12: 0000000000008000 c00000000a049000 c000000037d13d00 000000011134d5a0
GPR16: 0000000000001740 c0080000190d0000 c0080000190d1740 c000000009129288
GPR20: c000000037d13bc0 0000000000000001 c000000037d13bc0 c0080000190b7898
GPR24: c0080000190b7708 0000000000000000 c000000033bb2c48 0000000000000000
GPR28: c000000046b28280 0000000000000000 000000000000010c fffffffffffffffc
NIP [c00000000818e9a8] kthread_stop+0x38/0x230
LR [c0000000089846e8] scsi_host_dev_release+0x98/0x160
Call Trace:
[c000000033bb2c48] 0xc000000033bb2c48 (unreliable)
[c0000000089846e8] scsi_host_dev_release+0x98/0x160
[c00000000891e960] device_release+0x60/0x100
[c0000000087e55c4] kobject_release+0x84/0x210
[c00000000891ec78] put_device+0x28/0x40
[c000000008984ea4] scsi_host_alloc+0x314/0x430
[c0080000190b38bc] ibmvscsi_probe+0x54/0xad0 [ibmvscsi]
[c000000008110104] vio_bus_probe+0xa4/0x4b0
[c00000000892a860] really_probe+0x140/0x680
[c00000000892aefc] driver_probe_device+0x15c/0x200
[c00000000892b63c] device_driver_attach+0xcc/0xe0
[c00000000892b740] __driver_attach+0xf0/0x200
[c000000008926f28] bus_for_each_dev+0xa8/0x130
[c000000008929ce4] driver_attach+0x34/0x50
[c000000008928fc0] bus_add_driver+0x1b0/0x300
[c00000000892c798] driver_register+0x98/0x1a0
[c00000000810eb60] __vio_register_driver+0x80/0xe0
[c0080000190b4a30] ibmvscsi_module_init+0x9c/0xdc [ibmvscsi]
[c0000000080121d0] do_one_initcall+0x60/0x2d0
[c000000008261abc] do_init_module+0x7c/0x320
[c000000008265700] load_module+0x2350/0x25b0
[c000000008265cb4] __do_sys_finit_module+0xd4/0x160
[c000000008031110] system_call_exception+0x150/0x2d0
[c00000000800d35c] system_call_common+0xec/0x278
Fix this be nulling shost->ehandler when the kthread fails to spawn.
Link: https://lore.kernel.org/r/20210701195659.3185475-1-tyreld@linux.ibm.com
Fixes: 66a834d09293 ("scsi: core: Fix error handling of scsi_host_alloc()")
Cc: stable@vger.kernel.org
Reviewed-by: Ming Lei <ming.lei@redhat.com>
Signed-off-by: Tyrel Datwyler <tyreld@linux.ibm.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
2021-07-02 03:56:59 +08:00
|
|
|
shost->ehandler = NULL;
|
2021-06-02 21:30:26 +08:00
|
|
|
goto fail;
|
2005-09-06 20:04:26 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-11-11 20:44:54 +08:00
|
|
|
shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
|
2020-07-01 11:07:45 +08:00
|
|
|
WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS,
|
2013-11-11 20:44:54 +08:00
|
|
|
1, shost->host_no);
|
|
|
|
if (!shost->tmf_work_q) {
|
2014-10-24 20:27:02 +08:00
|
|
|
shost_printk(KERN_WARNING, shost,
|
|
|
|
"failed to create tmf workq\n");
|
2021-06-02 21:30:26 +08:00
|
|
|
goto fail;
|
2013-11-11 20:44:54 +08:00
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
scsi_proc_hostdir_add(shost->hostt);
|
|
|
|
return shost;
|
2021-06-02 21:30:26 +08:00
|
|
|
fail:
|
|
|
|
/*
|
|
|
|
* Host state is still SHOST_CREATED and that is enough to release
|
|
|
|
* ->shost_gendev. scsi_host_dev_release() will free
|
|
|
|
* dev_name(&shost->shost_dev).
|
|
|
|
*/
|
|
|
|
put_device(&shost->shost_gendev);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(scsi_host_alloc);
|
|
|
|
|
2013-02-02 03:40:17 +08:00
|
|
|
static int __scsi_host_match(struct device *dev, const void *data)
|
2008-01-22 14:01:34 +08:00
|
|
|
{
|
|
|
|
struct Scsi_Host *p;
|
2013-02-02 03:40:17 +08:00
|
|
|
const unsigned short *hostnum = data;
|
2008-01-22 14:01:34 +08:00
|
|
|
|
2008-02-22 07:13:36 +08:00
|
|
|
p = class_to_shost(dev);
|
2008-01-22 14:01:34 +08:00
|
|
|
return p->host_no == *hostnum;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/**
|
|
|
|
* scsi_host_lookup - get a reference to a Scsi_Host by host no
|
|
|
|
* @hostnum: host number to locate
|
|
|
|
*
|
|
|
|
* Return value:
|
|
|
|
* A pointer to located Scsi_Host or NULL.
|
2008-06-12 08:21:00 +08:00
|
|
|
*
|
|
|
|
* The caller must do a scsi_host_put() to drop the reference
|
|
|
|
* that scsi_host_get() took. The put_device() below dropped
|
|
|
|
* the reference from class_find_device().
|
2005-04-17 06:20:36 +08:00
|
|
|
**/
|
|
|
|
struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
|
|
|
|
{
|
2008-02-22 07:13:36 +08:00
|
|
|
struct device *cdev;
|
2008-08-08 08:49:30 +08:00
|
|
|
struct Scsi_Host *shost = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-05-23 05:21:08 +08:00
|
|
|
cdev = class_find_device(&shost_class, NULL, &hostnum,
|
|
|
|
__scsi_host_match);
|
2008-06-12 08:21:00 +08:00
|
|
|
if (cdev) {
|
2008-01-22 14:01:34 +08:00
|
|
|
shost = scsi_host_get(class_to_shost(cdev));
|
2008-06-12 08:21:00 +08:00
|
|
|
put_device(cdev);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
return shost;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(scsi_host_lookup);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scsi_host_get - inc a Scsi_Host ref count
|
|
|
|
* @shost: Pointer to Scsi_Host to inc.
|
|
|
|
**/
|
|
|
|
struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
|
|
|
|
{
|
2005-06-17 02:12:38 +08:00
|
|
|
if ((shost->shost_state == SHOST_DEL) ||
|
2005-04-17 06:20:36 +08:00
|
|
|
!get_device(&shost->shost_gendev))
|
|
|
|
return NULL;
|
|
|
|
return shost;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(scsi_host_get);
|
|
|
|
|
2022-07-06 20:03:53 +08:00
|
|
|
static bool scsi_host_check_in_flight(struct request *rq, void *data)
|
2019-10-25 14:58:55 +08:00
|
|
|
{
|
|
|
|
int *count = data;
|
|
|
|
struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
|
|
|
|
|
|
|
|
if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
|
|
|
|
(*count)++;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-06-24 22:03:26 +08:00
|
|
|
/**
|
|
|
|
* scsi_host_busy - Return the host busy counter
|
|
|
|
* @shost: Pointer to Scsi_Host to inc.
|
|
|
|
**/
|
|
|
|
int scsi_host_busy(struct Scsi_Host *shost)
|
|
|
|
{
|
2019-10-25 14:58:55 +08:00
|
|
|
int cnt = 0;
|
|
|
|
|
|
|
|
blk_mq_tagset_busy_iter(&shost->tag_set,
|
|
|
|
scsi_host_check_in_flight, &cnt);
|
|
|
|
return cnt;
|
2018-06-24 22:03:26 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(scsi_host_busy);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/**
|
|
|
|
* scsi_host_put - dec a Scsi_Host ref count
|
|
|
|
* @shost: Pointer to Scsi_Host to dec.
|
|
|
|
**/
|
|
|
|
void scsi_host_put(struct Scsi_Host *shost)
|
|
|
|
{
|
|
|
|
put_device(&shost->shost_gendev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(scsi_host_put);
|
|
|
|
|
|
|
|
int scsi_init_hosts(void)
|
|
|
|
{
|
|
|
|
return class_register(&shost_class);
|
|
|
|
}
|
|
|
|
|
|
|
|
void scsi_exit_hosts(void)
|
|
|
|
{
|
|
|
|
class_unregister(&shost_class);
|
2016-01-21 03:48:00 +08:00
|
|
|
ida_destroy(&host_index_ida);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int scsi_is_host_device(const struct device *dev)
|
|
|
|
{
|
2008-03-18 21:32:28 +08:00
|
|
|
return dev->type == &scsi_host_type;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL(scsi_is_host_device);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scsi_queue_work - Queue work to the Scsi_Host workqueue.
|
|
|
|
* @shost: Pointer to Scsi_Host.
|
|
|
|
* @work: Work to queue for execution.
|
|
|
|
*
|
|
|
|
* Return value:
|
2006-08-05 01:09:24 +08:00
|
|
|
* 1 - work queued for execution
|
|
|
|
* 0 - work is already queued
|
|
|
|
* -EINVAL - work queue doesn't exist
|
2005-04-17 06:20:36 +08:00
|
|
|
**/
|
|
|
|
int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
|
|
|
|
{
|
|
|
|
if (unlikely(!shost->work_q)) {
|
2014-06-25 22:39:59 +08:00
|
|
|
shost_printk(KERN_ERR, shost,
|
2005-04-17 06:20:36 +08:00
|
|
|
"ERROR: Scsi host '%s' attempted to queue scsi-work, "
|
|
|
|
"when no workqueue created.\n", shost->hostt->name);
|
|
|
|
dump_stack();
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return queue_work(shost->work_q, work);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(scsi_queue_work);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scsi_flush_work - Flush a Scsi_Host's workqueue.
|
|
|
|
* @shost: Pointer to Scsi_Host.
|
|
|
|
**/
|
|
|
|
void scsi_flush_work(struct Scsi_Host *shost)
|
|
|
|
{
|
|
|
|
if (!shost->work_q) {
|
2014-06-25 22:39:59 +08:00
|
|
|
shost_printk(KERN_ERR, shost,
|
2005-04-17 06:20:36 +08:00
|
|
|
"ERROR: Scsi host '%s' attempted to flush scsi-work, "
|
|
|
|
"when no workqueue created.\n", shost->hostt->name);
|
|
|
|
dump_stack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
flush_workqueue(shost->work_q);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(scsi_flush_work);
|
2020-02-28 15:53:07 +08:00
|
|
|
|
2022-07-06 20:03:53 +08:00
|
|
|
static bool complete_all_cmds_iter(struct request *rq, void *data)
|
2020-02-28 15:53:07 +08:00
|
|
|
{
|
|
|
|
struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
|
2021-05-24 10:54:57 +08:00
|
|
|
enum scsi_host_status status = *(enum scsi_host_status *)data;
|
2020-02-28 15:53:07 +08:00
|
|
|
|
|
|
|
scsi_dma_unmap(scmd);
|
2021-05-24 10:54:57 +08:00
|
|
|
scmd->result = 0;
|
|
|
|
set_host_byte(scmd, status);
|
2021-10-08 04:46:11 +08:00
|
|
|
scsi_done(scmd);
|
2020-02-28 15:53:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scsi_host_complete_all_commands - Terminate all running commands
|
|
|
|
* @shost: Scsi Host on which commands should be terminated
|
|
|
|
* @status: Status to be set for the terminated commands
|
|
|
|
*
|
|
|
|
* There is no protection against modification of the number
|
|
|
|
* of outstanding commands. It is the responsibility of the
|
|
|
|
* caller to ensure that concurrent I/O submission and/or
|
|
|
|
* completion is stopped when calling this function.
|
|
|
|
*/
|
2021-05-24 10:54:57 +08:00
|
|
|
void scsi_host_complete_all_commands(struct Scsi_Host *shost,
|
|
|
|
enum scsi_host_status status)
|
2020-02-28 15:53:07 +08:00
|
|
|
{
|
|
|
|
blk_mq_tagset_busy_iter(&shost->tag_set, complete_all_cmds_iter,
|
|
|
|
&status);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(scsi_host_complete_all_commands);
|
2020-02-28 15:53:15 +08:00
|
|
|
|
|
|
|
struct scsi_host_busy_iter_data {
|
2022-07-06 20:03:53 +08:00
|
|
|
bool (*fn)(struct scsi_cmnd *, void *);
|
2020-02-28 15:53:15 +08:00
|
|
|
void *priv;
|
|
|
|
};
|
|
|
|
|
2022-07-06 20:03:53 +08:00
|
|
|
static bool __scsi_host_busy_iter_fn(struct request *req, void *priv)
|
2020-02-28 15:53:15 +08:00
|
|
|
{
|
|
|
|
struct scsi_host_busy_iter_data *iter_data = priv;
|
|
|
|
struct scsi_cmnd *sc = blk_mq_rq_to_pdu(req);
|
|
|
|
|
2022-07-06 20:03:53 +08:00
|
|
|
return iter_data->fn(sc, iter_data->priv);
|
2020-02-28 15:53:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* scsi_host_busy_iter - Iterate over all busy commands
|
|
|
|
* @shost: Pointer to Scsi_Host.
|
|
|
|
* @fn: Function to call on each busy command
|
|
|
|
* @priv: Data pointer passed to @fn
|
|
|
|
*
|
|
|
|
* If locking against concurrent command completions is required
|
|
|
|
* ithas to be provided by the caller
|
|
|
|
**/
|
|
|
|
void scsi_host_busy_iter(struct Scsi_Host *shost,
|
2022-07-06 20:03:53 +08:00
|
|
|
bool (*fn)(struct scsi_cmnd *, void *),
|
2020-02-28 15:53:15 +08:00
|
|
|
void *priv)
|
|
|
|
{
|
|
|
|
struct scsi_host_busy_iter_data iter_data = {
|
|
|
|
.fn = fn,
|
|
|
|
.priv = priv,
|
|
|
|
};
|
|
|
|
|
|
|
|
blk_mq_tagset_busy_iter(&shost->tag_set, __scsi_host_busy_iter_fn,
|
|
|
|
&iter_data);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(scsi_host_busy_iter);
|