[PATCH] scsi: use assign_bit() where applicable

Peng Fan (OSS) posted 1 patch 6 days, 8 hours ago
drivers/scsi/bnx2fc/bnx2fc_fcoe.c  |  6 ++---
drivers/scsi/bnx2i/bnx2i_hwi.c     |  6 ++---
drivers/scsi/esas2r/esas2r_flash.c |  6 ++---
drivers/scsi/esas2r/esas2r_io.c    |  5 +----
drivers/scsi/isci/request.c        | 18 +++++----------
drivers/scsi/lpfc/lpfc_ct.c        |  6 ++---
drivers/scsi/lpfc/lpfc_els.c       | 36 ++++++++++--------------------
drivers/scsi/lpfc/lpfc_hbadisc.c   |  6 ++---
drivers/scsi/lpfc/lpfc_sli.c       |  7 ++----
9 files changed, 31 insertions(+), 65 deletions(-)
[PATCH] scsi: use assign_bit() where applicable
Posted by Peng Fan (OSS) 6 days, 8 hours ago
From: Peng Fan <peng.fan@nxp.com>

Convert open-coded if/else with set_bit/clear_bit and their
non-atomic __set_bit/__clear_bit variants to the assign_bit/__assign_bit
API. Where a bit is toggled based on its own value, use change_bit()
instead.

Done with Coccinelle semantic patch:
    // set_bit -> clear_bit => assign_bit

    @@
    expression cond, bit, addr;
    @@

    -if (cond)
    -        set_bit(bit, addr);
    -else
    -        clear_bit(bit, addr);
    +assign_bit(bit, addr, cond);

    // clear_bit -> set_bit => assign_bit

    @@
    expression cond, bit, addr;
    @@

    -if (cond)
    -        clear_bit(bit, addr);
    -else
    -        set_bit(bit, addr);
    +assign_bit(bit, addr, !cond);

    // __set_bit -> __clear_bit => __assign_bit

    @@
    expression cond, bit, addr;
    @@

    -if (cond)
    -        __set_bit(bit, addr);
    -else
    -        __clear_bit(bit, addr);
    +__assign_bit(bit, addr, cond);

    // __clear_bit -> __set_bit => __assign_bit

    @@
    expression cond, bit, addr;
    @@

    -if (cond)
    -        __clear_bit(bit, addr);
    -else
    -        __set_bit(bit, addr);
    +__assign_bit(bit, addr, !cond);

Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
 drivers/scsi/bnx2fc/bnx2fc_fcoe.c  |  6 ++---
 drivers/scsi/bnx2i/bnx2i_hwi.c     |  6 ++---
 drivers/scsi/esas2r/esas2r_flash.c |  6 ++---
 drivers/scsi/esas2r/esas2r_io.c    |  5 +----
 drivers/scsi/isci/request.c        | 18 +++++----------
 drivers/scsi/lpfc/lpfc_ct.c        |  6 ++---
 drivers/scsi/lpfc/lpfc_els.c       | 36 ++++++++++--------------------
 drivers/scsi/lpfc/lpfc_hbadisc.c   |  6 ++---
 drivers/scsi/lpfc/lpfc_sli.c       |  7 ++----
 9 files changed, 31 insertions(+), 65 deletions(-)

diff --git a/drivers/scsi/bnx2fc/bnx2fc_fcoe.c b/drivers/scsi/bnx2fc/bnx2fc_fcoe.c
index c95b084cad69..d9957e8bc04b 100644
--- a/drivers/scsi/bnx2fc/bnx2fc_fcoe.c
+++ b/drivers/scsi/bnx2fc/bnx2fc_fcoe.c
@@ -785,10 +785,8 @@ static int bnx2fc_link_ok(struct fc_lport *lport)
  */
 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
 {
-	if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
-		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
-	else
-		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
+	assign_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state,
+		   test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state));
 }
 
 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
diff --git a/drivers/scsi/bnx2i/bnx2i_hwi.c b/drivers/scsi/bnx2i/bnx2i_hwi.c
index d24cc2c795d6..225d0e7a6d1d 100644
--- a/drivers/scsi/bnx2i/bnx2i_hwi.c
+++ b/drivers/scsi/bnx2i/bnx2i_hwi.c
@@ -96,10 +96,8 @@ static void bnx2i_adjust_qp_size(struct bnx2i_hba *hba)
  */
 static void bnx2i_get_link_state(struct bnx2i_hba *hba)
 {
-	if (test_bit(__LINK_STATE_NOCARRIER, &hba->netdev->state))
-		set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
-	else
-		clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
+	assign_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state,
+		   test_bit(__LINK_STATE_NOCARRIER, &hba->netdev->state));
 }
 
 
diff --git a/drivers/scsi/esas2r/esas2r_flash.c b/drivers/scsi/esas2r/esas2r_flash.c
index f910e2553fbb..44cf5af9fe90 100644
--- a/drivers/scsi/esas2r/esas2r_flash.c
+++ b/drivers/scsi/esas2r/esas2r_flash.c
@@ -1240,10 +1240,8 @@ static void esas2r_nvram_callback(struct esas2r_adapter *a,
 
 	if (rq->req_stat != RS_PENDING) {
 		/* update the NVRAM state */
-		if (rq->req_stat == RS_SUCCESS)
-			set_bit(AF_NVR_VALID, &a->flags);
-		else
-			clear_bit(AF_NVR_VALID, &a->flags);
+		assign_bit(AF_NVR_VALID, &a->flags,
+			   rq->req_stat == RS_SUCCESS);
 
 		esas2r_enable_heartbeat(a);
 
diff --git a/drivers/scsi/esas2r/esas2r_io.c b/drivers/scsi/esas2r/esas2r_io.c
index a8df916cd57a..75c0876f804e 100644
--- a/drivers/scsi/esas2r/esas2r_io.c
+++ b/drivers/scsi/esas2r/esas2r_io.c
@@ -149,10 +149,7 @@ void esas2r_start_vda_request(struct esas2r_adapter *a,
 	if (a->last_write >= a->list_size) {
 		a->last_write = 0;
 		/* update the toggle bit */
-		if (test_bit(AF_COMM_LIST_TOGGLE, &a->flags))
-			clear_bit(AF_COMM_LIST_TOGGLE, &a->flags);
-		else
-			set_bit(AF_COMM_LIST_TOGGLE, &a->flags);
+		change_bit(AF_COMM_LIST_TOGGLE, &a->flags);
 	}
 
 	element =
diff --git a/drivers/scsi/isci/request.c b/drivers/scsi/isci/request.c
index bb89a2e33eb4..7c3eeb7496ea 100644
--- a/drivers/scsi/isci/request.c
+++ b/drivers/scsi/isci/request.c
@@ -888,10 +888,8 @@ sci_io_request_terminate(struct isci_request *ireq)
 		sci_change_state(&ireq->sm, SCI_REQ_ABORTING);
 		fallthrough;	/* and handle like ABORTING */
 	case SCI_REQ_ABORTING:
-		if (!isci_remote_device_is_safe_to_abort(ireq->target_device))
-			set_bit(IREQ_PENDING_ABORT, &ireq->flags);
-		else
-			clear_bit(IREQ_PENDING_ABORT, &ireq->flags);
+		assign_bit(IREQ_PENDING_ABORT, &ireq->flags,
+			   !isci_remote_device_is_safe_to_abort(ireq->target_device));
 		/* If the request is only waiting on the remote device
 		 * suspension, return SUCCESS so the caller will wait too.
 		 */
@@ -2696,10 +2694,8 @@ static void isci_request_handle_controller_specific_errors(
 		*response_ptr = SAS_TASK_UNDELIVERED;
 		*status_ptr = SAS_SAM_STAT_TASK_ABORTED;
 
-		if (task->task_proto == SAS_PROTOCOL_SMP)
-			set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
-		else
-			clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
+		assign_bit(IREQ_COMPLETE_IN_TARGET, &request->flags,
+			   task->task_proto == SAS_PROTOCOL_SMP);
 		break;
 	}
 }
@@ -2886,10 +2882,8 @@ static void isci_request_io_request_complete(struct isci_host *ihost,
 		else
 			status = SAS_ABORTED_TASK;
 
-		if (SAS_PROTOCOL_SMP == task->task_proto)
-			set_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
-		else
-			clear_bit(IREQ_COMPLETE_IN_TARGET, &request->flags);
+		assign_bit(IREQ_COMPLETE_IN_TARGET, &request->flags,
+			   SAS_PROTOCOL_SMP == task->task_proto);
 		break;
 	}
 
diff --git a/drivers/scsi/lpfc/lpfc_ct.c b/drivers/scsi/lpfc/lpfc_ct.c
index 0734ab3be3e3..c90e93f20f34 100644
--- a/drivers/scsi/lpfc/lpfc_ct.c
+++ b/drivers/scsi/lpfc/lpfc_ct.c
@@ -832,10 +832,8 @@ lpfc_ns_rsp_audit_did(struct lpfc_vport *vport, uint32_t Did, uint8_t fc4_type)
 			if (ndlp->nlp_type != NLP_NVME_INITIATOR ||
 			    ndlp->nlp_state != NLP_STE_UNMAPPED_NODE)
 				continue;
-			if (ndlp->nlp_DID == Did)
-				clear_bit(NLP_NVMET_RECOV, &ndlp->nlp_flag);
-			else
-				set_bit(NLP_NVMET_RECOV, &ndlp->nlp_flag);
+			assign_bit(NLP_NVMET_RECOV, &ndlp->nlp_flag,
+				   ndlp->nlp_DID != Did);
 		}
 	}
 }
diff --git a/drivers/scsi/lpfc/lpfc_els.c b/drivers/scsi/lpfc/lpfc_els.c
index 6f6394a0047c..d91f8362aafa 100644
--- a/drivers/scsi/lpfc/lpfc_els.c
+++ b/drivers/scsi/lpfc/lpfc_els.c
@@ -1810,48 +1810,36 @@ lpfc_plogi_confirm_nport(struct lpfc_hba *phba, uint32_t *prsp,
 	new_ndlp->nlp_flag = ndlp->nlp_flag;
 
 	/* if new_ndlp had NLP_UNREG_INP set, keep it */
-	if (test_bit(NLP_UNREG_INP, &keep_new_nlp_flag))
-		set_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag);
-	else
-		clear_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag);
+	assign_bit(NLP_UNREG_INP, &new_ndlp->nlp_flag,
+		   test_bit(NLP_UNREG_INP, &keep_new_nlp_flag));
 
 	/* if new_ndlp had NLP_RPI_REGISTERED set, keep it */
-	if (test_bit(NLP_RPI_REGISTERED, &keep_new_nlp_flag))
-		set_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag);
-	else
-		clear_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag);
+	assign_bit(NLP_RPI_REGISTERED, &new_ndlp->nlp_flag,
+		   test_bit(NLP_RPI_REGISTERED, &keep_new_nlp_flag));
 
 	/*
 	 * Retain the DROPPED flag. This will take care of the init
 	 * refcount when affecting the state change
 	 */
-	if (test_bit(NLP_DROPPED, &keep_new_nlp_flag))
-		set_bit(NLP_DROPPED, &new_ndlp->nlp_flag);
-	else
-		clear_bit(NLP_DROPPED, &new_ndlp->nlp_flag);
+	assign_bit(NLP_DROPPED, &new_ndlp->nlp_flag,
+		   test_bit(NLP_DROPPED, &keep_new_nlp_flag));
 
 	ndlp->nlp_flag = keep_new_nlp_flag;
 
 	/* if ndlp had NLP_UNREG_INP set, keep it */
-	if (test_bit(NLP_UNREG_INP, &keep_nlp_flag))
-		set_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
-	else
-		clear_bit(NLP_UNREG_INP, &ndlp->nlp_flag);
+	assign_bit(NLP_UNREG_INP, &ndlp->nlp_flag,
+		   test_bit(NLP_UNREG_INP, &keep_nlp_flag));
 
 	/* if ndlp had NLP_RPI_REGISTERED set, keep it */
-	if (test_bit(NLP_RPI_REGISTERED, &keep_nlp_flag))
-		set_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
-	else
-		clear_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag);
+	assign_bit(NLP_RPI_REGISTERED, &ndlp->nlp_flag,
+		   test_bit(NLP_RPI_REGISTERED, &keep_nlp_flag));
 
 	/*
 	 * Retain the DROPPED flag. This will take care of the init
 	 * refcount when affecting the state change
 	 */
-	if (test_bit(NLP_DROPPED, &keep_nlp_flag))
-		set_bit(NLP_DROPPED, &ndlp->nlp_flag);
-	else
-		clear_bit(NLP_DROPPED, &ndlp->nlp_flag);
+	assign_bit(NLP_DROPPED, &ndlp->nlp_flag,
+		   test_bit(NLP_DROPPED, &keep_nlp_flag));
 
 	spin_unlock_irq(&new_ndlp->lock);
 	spin_unlock_irq(&ndlp->lock);
diff --git a/drivers/scsi/lpfc/lpfc_hbadisc.c b/drivers/scsi/lpfc/lpfc_hbadisc.c
index 4c673dffa671..1abed5aaddd5 100644
--- a/drivers/scsi/lpfc/lpfc_hbadisc.c
+++ b/drivers/scsi/lpfc/lpfc_hbadisc.c
@@ -3776,10 +3776,8 @@ lpfc_mbx_cmpl_read_topology(struct lpfc_hba *phba, LPFC_MBOXQ_t *pmb)
 
 	memcpy(&phba->alpa_map[0], mp->virt, 128);
 
-	if (bf_get(lpfc_mbx_read_top_pb, la))
-		set_bit(FC_BYPASSED_MODE, &vport->fc_flag);
-	else
-		clear_bit(FC_BYPASSED_MODE, &vport->fc_flag);
+	assign_bit(FC_BYPASSED_MODE, &vport->fc_flag,
+		   bf_get(lpfc_mbx_read_top_pb, la));
 
 	if (phba->fc_eventTag <= la->eventTag) {
 		phba->fc_stat.LinkMultiEvent++;
diff --git a/drivers/scsi/lpfc/lpfc_sli.c b/drivers/scsi/lpfc/lpfc_sli.c
index cd285e87c278..e21bc1acd76a 100644
--- a/drivers/scsi/lpfc/lpfc_sli.c
+++ b/drivers/scsi/lpfc/lpfc_sli.c
@@ -8575,11 +8575,8 @@ lpfc_sli4_hba_setup(struct lpfc_hba *phba)
 	if (!test_bit(HBA_FCOE_MODE, &phba->hba_flag))
 		lpfc_get_platform_uuid(phba);
 
-	if (bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) ==
-		LPFC_DCBX_CEE_MODE)
-		set_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
-	else
-		clear_bit(HBA_FIP_SUPPORT, &phba->hba_flag);
+	assign_bit(HBA_FIP_SUPPORT, &phba->hba_flag,
+		   bf_get(lpfc_mbx_rd_rev_cee_ver, &mqe->un.read_rev) == LPFC_DCBX_CEE_MODE);
 
 	clear_bit(HBA_IOQ_FLUSH, &phba->hba_flag);
 
-- 
2.51.0