diff mbox series

[RFC,3/6] scsi: core: Add scsi_get_dev()

Message ID 1663669630-21333-4-git-send-email-john.garry@huawei.com
State Superseded
Headers show
Series libata/scsi/libsas: Allocate SCSI device earlier for ata port probe | expand

Commit Message

John Garry Sept. 20, 2022, 10:27 a.m. UTC
Add a function which allows use to alloc a sdev with configurable
device parent, and channel:id:lun.

This is useful for separating adding a scsi device into separate alloc
and scan steps.

Signed-off-by: John Garry <john.garry@huawei.com>
---
 drivers/scsi/scsi_scan.c | 25 +++++++++++++++++++++++++
 include/scsi/scsi_host.h |  3 +++
 2 files changed, 28 insertions(+)

Comments

Damien Le Moal Sept. 20, 2022, 9:58 p.m. UTC | #1
On 9/20/22 19:27, John Garry wrote:
> Add a function which allows use to alloc a sdev with configurable
> device parent, and channel:id:lun.
> 
> This is useful for separating adding a scsi device into separate alloc
> and scan steps.
> 
> Signed-off-by: John Garry <john.garry@huawei.com>
> ---
>   drivers/scsi/scsi_scan.c | 25 +++++++++++++++++++++++++
>   include/scsi/scsi_host.h |  3 +++
>   2 files changed, 28 insertions(+)
> 
> diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
> index 3759b1a77504..fd15ddac01b6 100644
> --- a/drivers/scsi/scsi_scan.c
> +++ b/drivers/scsi/scsi_scan.c
> @@ -1988,3 +1988,28 @@ void scsi_forget_host(struct Scsi_Host *shost)
>   	spin_unlock_irqrestore(shost->host_lock, flags);
>   }
>   
> +struct scsi_device *scsi_get_dev(struct device *parent, int channel, uint id, u64 lun)
> +{
> +	struct Scsi_Host *shost = dev_to_shost(parent);
> +	struct scsi_device *sdev = NULL;
> +	struct scsi_target *starget;
> +
> +	mutex_lock(&shost->scan_mutex);
> +	if (!scsi_host_scan_allowed(shost))
> +		goto out;
> +
> +	starget = scsi_alloc_target(parent, 0, id);
> +	if (!starget)
> +		goto out;
> +
> +	sdev = scsi_alloc_sdev(starget, 0, NULL);
> +	if (sdev)
> +		sdev->borken = 0;
> +	else
> +		scsi_target_reap(starget);
> +	put_device(&starget->dev);
> + out:
> +	mutex_unlock(&shost->scan_mutex);
> +	return sdev;
> +}
> +EXPORT_SYMBOL(scsi_get_dev);
> diff --git a/include/scsi/scsi_host.h b/include/scsi/scsi_host.h
> index aa7b7496c93a..5142c7df7647 100644
> --- a/include/scsi/scsi_host.h
> +++ b/include/scsi/scsi_host.h
> @@ -794,7 +794,10 @@ void scsi_host_busy_iter(struct Scsi_Host *,
>   
>   struct class_container;
>   
> +extern struct scsi_device *scsi_get_dev(struct device *parent, int channel, uint id, u64 lun);

You should not need extern here, and long line...

> +
>   /*
> +

white line change.

>    * DIF defines the exchange of protection information between
>    * initiator and SBC block device.
>    *
diff mbox series

Patch

diff --git a/drivers/scsi/scsi_scan.c b/drivers/scsi/scsi_scan.c
index 3759b1a77504..fd15ddac01b6 100644
--- a/drivers/scsi/scsi_scan.c
+++ b/drivers/scsi/scsi_scan.c
@@ -1988,3 +1988,28 @@  void scsi_forget_host(struct Scsi_Host *shost)
 	spin_unlock_irqrestore(shost->host_lock, flags);
 }
 
+struct scsi_device *scsi_get_dev(struct device *parent, int channel, uint id, u64 lun)
+{
+	struct Scsi_Host *shost = dev_to_shost(parent);
+	struct scsi_device *sdev = NULL;
+	struct scsi_target *starget;
+
+	mutex_lock(&shost->scan_mutex);
+	if (!scsi_host_scan_allowed(shost))
+		goto out;
+
+	starget = scsi_alloc_target(parent, 0, id);
+	if (!starget)
+		goto out;
+
+	sdev = scsi_alloc_sdev(starget, 0, NULL);
+	if (sdev)
+		sdev->borken = 0;
+	else
+		scsi_target_reap(starget);
+	put_device(&starget->dev);
+ out:
+	mutex_unlock(&shost->scan_mutex);
+	return sdev;
+}
+EXPORT_SYMBOL(scsi_get_dev);
diff --git a/include/scsi/scsi_host.h b/include/scsi/scsi_host.h
index aa7b7496c93a..5142c7df7647 100644
--- a/include/scsi/scsi_host.h
+++ b/include/scsi/scsi_host.h
@@ -794,7 +794,10 @@  void scsi_host_busy_iter(struct Scsi_Host *,
 
 struct class_container;
 
+extern struct scsi_device *scsi_get_dev(struct device *parent, int channel, uint id, u64 lun);
+
 /*
+
  * DIF defines the exchange of protection information between
  * initiator and SBC block device.
  *