diff mbox series

[111/117] Use the scsi_status union more widely

Message ID 20210420021402.27678-21-bvanassche@acm.org
State New
Headers show
Series Make better use of static type checking | expand

Commit Message

Bart Van Assche April 20, 2021, 2:13 a.m. UTC
Change the type of the SCSI status argument of the following functions
from int into union scsi_status:
* scsi_status_is_good();
* status_byte();
* msg_byte();
* host_byte();
* driver_byte();
* scsi_hostbyte_string();
* scsi_driverbyte_string().

Make all callers of these functions pass a union scsi_result as argument.

Remove the scsi_status_to_int() macro.

Cc: Christoph Hellwig <hch@lst.de>
Cc: Ming Lei <ming.lei@redhat.com>
Cc: Hannes Reinecke <hare@suse.com>
Cc: John Garry <john.garry@huawei.com>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/scsi/constants.c    |  8 ++++----
 drivers/scsi/scsi_logging.c |  4 ++--
 drivers/scsi/sd.c           |  4 ++--
 include/scsi/scsi.h         | 28 +++++++---------------------
 include/scsi/scsi_dbg.h     | 10 ++++++----
 5 files changed, 21 insertions(+), 33 deletions(-)
diff mbox series

Patch

diff --git a/drivers/scsi/constants.c b/drivers/scsi/constants.c
index 84d73f57292b..0ea510b4ca52 100644
--- a/drivers/scsi/constants.c
+++ b/drivers/scsi/constants.c
@@ -410,10 +410,10 @@  static const char * const driverbyte_table[]={
 "DRIVER_OK", "DRIVER_BUSY", "DRIVER_SOFT",  "DRIVER_MEDIA", "DRIVER_ERROR",
 "DRIVER_INVALID", "DRIVER_TIMEOUT", "DRIVER_HARD", "DRIVER_SENSE"};
 
-const char *scsi_hostbyte_string(int result)
+const char *scsi_hostbyte_string(union scsi_status result)
 {
 	const char *hb_string = NULL;
-	int hb = host_byte(result);
+	enum host_status hb = host_byte(result);
 
 	if (hb < ARRAY_SIZE(hostbyte_table))
 		hb_string = hostbyte_table[hb];
@@ -421,10 +421,10 @@  const char *scsi_hostbyte_string(int result)
 }
 EXPORT_SYMBOL(scsi_hostbyte_string);
 
-const char *scsi_driverbyte_string(int result)
+const char *scsi_driverbyte_string(union scsi_status result)
 {
 	const char *db_string = NULL;
-	int db = driver_byte(result);
+	enum driver_status db = driver_byte(result);
 
 	if (db < ARRAY_SIZE(driverbyte_table))
 		db_string = driverbyte_table[db];
diff --git a/drivers/scsi/scsi_logging.c b/drivers/scsi/scsi_logging.c
index 5c994ba1fad8..3cba3ff97559 100644
--- a/drivers/scsi/scsi_logging.c
+++ b/drivers/scsi/scsi_logging.c
@@ -384,8 +384,8 @@  void scsi_print_result(const struct scsi_cmnd *cmd, const char *msg,
 	char *logbuf;
 	size_t off, logbuf_len;
 	const char *mlret_string = scsi_mlreturn_string(disposition);
-	const char *hb_string = scsi_hostbyte_string(cmd->status.combined);
-	const char *db_string = scsi_driverbyte_string(cmd->status.combined);
+	const char *hb_string = scsi_hostbyte_string(cmd->status);
+	const char *db_string = scsi_driverbyte_string(cmd->status);
 	unsigned long cmd_age = (jiffies - cmd->jiffies_at_alloc) / HZ;
 
 	logbuf = scsi_log_reserve_buffer(&logbuf_len);
diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 8df2f25e4129..756fe99794a7 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -3812,8 +3812,8 @@  void sd_print_sense_hdr(struct scsi_disk *sdkp, struct scsi_sense_hdr *sshdr)
 void sd_print_result(const struct scsi_disk *sdkp, const char *msg,
 		     union scsi_status result)
 {
-	const char *hb_string = scsi_hostbyte_string(result.combined);
-	const char *db_string = scsi_driverbyte_string(result.combined);
+	const char *hb_string = scsi_hostbyte_string(result);
+	const char *db_string = scsi_driverbyte_string(result);
 
 	if (hb_string || db_string)
 		sd_printk(KERN_INFO, sdkp,
diff --git a/include/scsi/scsi.h b/include/scsi/scsi.h
index 18bb1fb2458f..03f047333e52 100644
--- a/include/scsi/scsi.h
+++ b/include/scsi/scsi.h
@@ -33,20 +33,20 @@  enum scsi_timeouts {
 
 /** scsi_status_is_good - check the status return.
  *
- * @status: the status passed up from the driver (including host and
+ * @scsi_status: the status passed up from the driver (including host and
  *          driver components)
  *
  * This returns true for known good conditions that may be treated as
  * command completed normally
  */
-static inline bool __scsi_status_is_good(int status)
+static inline bool scsi_status_is_good(union scsi_status scsi_status)
 {
 	/*
 	 * FIXME: bit0 is listed as reserved in SCSI-2, but is
 	 * significant in SCSI-3.  For now, we follow the SCSI-2
 	 * behaviour and ignore reserved bits.
 	 */
-	status &= 0xfe;
+	const u8 status = scsi_status.combined & 0xfe;
 	return ((status == SAM_STAT_GOOD) ||
 		(status == SAM_STAT_CONDITION_MET) ||
 		/* Next two "intermediate" statuses are obsolete in SAM-4 */
@@ -56,20 +56,6 @@  static inline bool __scsi_status_is_good(int status)
 		(status == SAM_STAT_COMMAND_TERMINATED));
 }
 
-/*
- * If the 'status' argument has type int, unsigned int or union scsi_status,
- * return the combined SCSI status. If the 'status' argument has another type,
- * trigger a compiler error by passing a struct to a context where an integer
- * is expected.
- */
-#define scsi_status_to_int(status)			\
-	__builtin_choose_expr(sizeof(status) == 4,	\
-			      *(int32_t *)&(status),	\
-			      (union scsi_status){})
-
-#define scsi_status_is_good(status)				\
-	__scsi_status_is_good(scsi_status_to_int(status))
-
 
 /*
  * standard mode-select header prepended to all mode-select commands
@@ -148,10 +134,10 @@  enum scsi_disposition {
  *      driver_byte = set by mid-level.
  */
 #define status_byte(result) ((enum sam_status_divided_by_two)	\
-			     ((scsi_status_to_int((result)) >> 1) & 0x7f))
-#define msg_byte(result)    ((scsi_status_to_int((result)) >> 8) & 0xff)
-#define host_byte(result)   ((scsi_status_to_int((result)) >> 16) & 0xff)
-#define driver_byte(result) ((scsi_status_to_int((result)) >> 24) & 0xff)
+			     ((result).b.status >> 1))
+#define msg_byte(result)    ((result).b.msg)
+#define host_byte(result)   ((result).b.host)
+#define driver_byte(result) ((result).b.driver)
 
 #define sense_class(sense)  (((sense) >> 4) & 0x7)
 #define sense_error(sense)  ((sense) & 0xf)
diff --git a/include/scsi/scsi_dbg.h b/include/scsi/scsi_dbg.h
index 7b196d234626..d3a868b6dd89 100644
--- a/include/scsi/scsi_dbg.h
+++ b/include/scsi/scsi_dbg.h
@@ -2,6 +2,8 @@ 
 #ifndef _SCSI_SCSI_DBG_H
 #define _SCSI_SCSI_DBG_H
 
+#include <scsi/scsi_status.h>
+
 struct scsi_cmnd;
 struct scsi_device;
 struct scsi_sense_hdr;
@@ -23,8 +25,8 @@  extern const char *scsi_sense_key_string(unsigned char);
 extern const char *scsi_extd_sense_format(unsigned char, unsigned char,
 					  const char **);
 extern const char *scsi_mlreturn_string(int);
-extern const char *scsi_hostbyte_string(int);
-extern const char *scsi_driverbyte_string(int);
+extern const char *scsi_hostbyte_string(union scsi_status result);
+extern const char *scsi_driverbyte_string(union scsi_status result);
 #else
 static inline bool
 scsi_opcode_sa_name(int cmd, int sa,
@@ -71,13 +73,13 @@  scsi_mlreturn_string(int result)
 }
 
 static inline const char *
-scsi_hostbyte_string(int result)
+scsi_hostbyte_string(union scsi_status result)
 {
 	return NULL;
 }
 
 static inline const char *
-scsi_driverbyte_string(int result)
+scsi_driverbyte_string(union scsi_status result)
 {
 	return NULL;
 }