diff mbox series

[v2] scsi: stop judging after finding a VPD page expected to be processed.

Message ID 20250227060618.15787-1-wdhh6@aliyun.com (mailing list archive)
State New
Headers show
Series [v2] scsi: stop judging after finding a VPD page expected to be processed. | expand

Commit Message

Chaohai Chen Feb. 27, 2025, 6:06 a.m. UTC
When the vpd_buf->data[i] is expected to be processed, stop other
judgments.

Signed-off-by: Chaohai Chen <wdhh6@aliyun.com>
---
v1: https://lore.kernel.org/all/20250226065802.234144-1-wdhh6@aliyun.com/
v1->v2:
 * Modify this function so that there is no code duplication
---
---
 drivers/scsi/scsi.c        | 39 +++++++++++++++++++++-----------------
 include/scsi/scsi_device.h | 12 ++++++++++++
 2 files changed, 34 insertions(+), 17 deletions(-)

Comments

John Garry Feb. 27, 2025, 7:55 a.m. UTC | #1
On 27/02/2025 06:06, Chaohai Chen wrote:
> +#define SCSI_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - sizeof(char))
> +
> +#define VPD_PAGE_INFO(vpd_page)							\
> +	{ 0x##vpd_page, offsetof(struct scsi_device, vpd_pg##vpd_page) + \
> +		SCSI_BUILD_BUG_ON(!__same_type(&((struct scsi_device *)NULL)->vpd_pg##vpd_page, \
> +				struct scsi_vpd __rcu **))} \
> +
>   #define	to_scsi_device(d)	\

That's unreadable....
diff mbox series

Patch

diff --git a/drivers/scsi/scsi.c b/drivers/scsi/scsi.c
index a77e0499b738..e840616d4deb 100644
--- a/drivers/scsi/scsi.c
+++ b/drivers/scsi/scsi.c
@@ -498,8 +498,18 @@  static void scsi_update_vpd_page(struct scsi_device *sdev, u8 page,
  */
 void scsi_attach_vpd(struct scsi_device *sdev)
 {
-	int i;
+	int i, j;
 	struct scsi_vpd *vpd_buf;
+	static const struct vpd_page_info cached_page[] = {
+		VPD_PAGE_INFO(0),
+		VPD_PAGE_INFO(80),
+		VPD_PAGE_INFO(83),
+		VPD_PAGE_INFO(89),
+		VPD_PAGE_INFO(b0),
+		VPD_PAGE_INFO(b1),
+		VPD_PAGE_INFO(b2),
+		VPD_PAGE_INFO(b7),
+	};
 
 	if (!scsi_device_supports_vpd(sdev))
 		return;
@@ -510,22 +520,17 @@  void scsi_attach_vpd(struct scsi_device *sdev)
 		return;
 
 	for (i = 4; i < vpd_buf->len; i++) {
-		if (vpd_buf->data[i] == 0x0)
-			scsi_update_vpd_page(sdev, 0x0, &sdev->vpd_pg0);
-		if (vpd_buf->data[i] == 0x80)
-			scsi_update_vpd_page(sdev, 0x80, &sdev->vpd_pg80);
-		if (vpd_buf->data[i] == 0x83)
-			scsi_update_vpd_page(sdev, 0x83, &sdev->vpd_pg83);
-		if (vpd_buf->data[i] == 0x89)
-			scsi_update_vpd_page(sdev, 0x89, &sdev->vpd_pg89);
-		if (vpd_buf->data[i] == 0xb0)
-			scsi_update_vpd_page(sdev, 0xb0, &sdev->vpd_pgb0);
-		if (vpd_buf->data[i] == 0xb1)
-			scsi_update_vpd_page(sdev, 0xb1, &sdev->vpd_pgb1);
-		if (vpd_buf->data[i] == 0xb2)
-			scsi_update_vpd_page(sdev, 0xb2, &sdev->vpd_pgb2);
-		if (vpd_buf->data[i] == 0xb7)
-			scsi_update_vpd_page(sdev, 0xb7, &sdev->vpd_pgb7);
+		for (j = 0; j < ARRAY_SIZE(cached_page); j++) {
+			const u8 page_code = cached_page[j].page_code;
+			const u16 offset = cached_page[j].offset;
+			struct scsi_vpd __rcu **vpd_data =
+				(void *)&sdev + offset;
+
+			if (vpd_buf->data[i] == page_code) {
+				scsi_update_vpd_page(sdev, page_code, vpd_data);
+				break;
+			}
+		}
 	}
 	kfree(vpd_buf);
 }
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index 7acd0ec82bb0..46f17875b758 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -284,6 +284,18 @@  struct scsi_device {
 	unsigned long		sdev_data[];
 } __attribute__((aligned(sizeof(unsigned long))));
 
+struct vpd_page_info {
+	u8 page_code;
+	u16 offset; /* offset in struct scsi_device of vpd_pg... member */
+};
+
+#define SCSI_BUILD_BUG_ON(cond) (sizeof(char[1 - 2 * !!(cond)]) - sizeof(char))
+
+#define VPD_PAGE_INFO(vpd_page)							\
+	{ 0x##vpd_page, offsetof(struct scsi_device, vpd_pg##vpd_page) + \
+		SCSI_BUILD_BUG_ON(!__same_type(&((struct scsi_device *)NULL)->vpd_pg##vpd_page, \
+				struct scsi_vpd __rcu **))} \
+
 #define	to_scsi_device(d)	\
 	container_of(d, struct scsi_device, sdev_gendev)
 #define	class_to_sdev(d)	\