Skip to contentSkip to conversation

staging: wilc1000: return zero on success and non-zero on function failure

PATCH In review [email protected] 2 participants 4 messages Jan 23, 2020 – Jan 27, 2020 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 staging: wilc1000: return zero on success and non-zero on function failure

From: Ajay Singh <[email protected]>

Some of the HIF layer API's return zero for failure and non-zero for success condition. Now, modified the functions to return zero for success and non-zero for failure as its recommended approach suggested in [1].

1. https://lore.kernel.org/driverdev-devel/[email protected]/

---
 drivers/staging/wilc1000/netdev.c |  45 +++-----
 drivers/staging/wilc1000/sdio.c   | 178 +++++++++++++-----------------
 drivers/staging/wilc1000/spi.c    | 138 ++++++++++++-----------
 drivers/staging/wilc1000/wlan.c   |  75 +++++++------
 4 files changed, 197 insertions(+), 239 deletions(-)
drivers/staging/wilc1000/netdev.c +17 -28
index 960dbc8d5173..0f48e74aa3ea 100644
@@ -183,7 +183,7 @@ static int wilc_wlan_get_firmware(struct net_device *dev)
 {
 	struct wilc_vif *vif = netdev_priv(dev);
 	struct wilc *wilc = vif->wilc;
-	int chip_id, ret = 0;
+	int chip_id;
 	const struct firmware *wilc_firmware;
 	char *firmware;
 
@@ -198,14 +198,11 @@ static int wilc_wlan_get_firmware(struct net_device *dev)
 
 	if (request_firmware(&wilc_firmware, firmware, wilc->dev) != 0) {
 		netdev_err(dev, "%s - firmware not available\n", firmware);
-		ret = -1;
-		goto fail;
+		return -EINVAL;
 	}
 	wilc->firmware = wilc_firmware;
 
-fail:
-
-	return ret;
+	return 0;
 }
 
 static int wilc_start_firmware(struct net_device *dev)
@@ -215,7 +212,7 @@ static int wilc_start_firmware(struct net_device *dev)
 	int ret = 0;
 
 	ret = wilc_wlan_start(wilc);
-	if (ret < 0)
+	if (ret)
 		return ret;
 
 	if (!wait_for_completion_timeout(&wilc->sync_event,
@@ -238,7 +235,7 @@ static int wilc1000_firmware_download(struct net_device *dev)
 
 	ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
 					  wilc->firmware->size);
-	if (ret < 0)
+	if (ret)
 		return ret;
 
 	release_firmware(wilc->firmware);
@@ -417,7 +414,7 @@ static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
 	return 0;
 
 fail:
-	return -1;
+	return -EINVAL;
 }
 
 static void wlan_deinitialize_threads(struct net_device *dev)
@@ -497,14 +494,12 @@ static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
 		wl->close = 0;
 
 		ret = wilc_wlan_init(dev);
-		if (ret < 0)
-			return -EIO;
+		if (ret)
+			return ret;
 
 		ret = wlan_initialize_threads(dev);
-		if (ret < 0) {
-			ret = -EIO;
+		if (ret)
 			goto fail_wilc_wlan;
-		}
 
 		if (wl->gpio_irq && init_irq(dev)) {
 			ret = -EIO;
@@ -518,22 +513,17 @@ static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
 			goto fail_irq_init;
 		}
 
-		if (wilc_wlan_get_firmware(dev)) {
-			ret = -EIO;
+		ret = wilc_wlan_get_firmware(dev);
+		if (ret)
 			goto fail_irq_enable;
-		}
 
 		ret = wilc1000_firmware_download(dev);
-		if (ret < 0) {
-			ret = -EIO;
+		if (ret)
 			goto fail_irq_enable;
-		}
 
 		ret = wilc_start_firmware(dev);
-		if (ret < 0) {
-			ret = -EIO;
+		if (ret)
 			goto fail_irq_enable;
-		}
 
 		if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
 			int size;
@@ -545,11 +535,10 @@ static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
 			firmware_ver[size] = '\0';
 			netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
 		}
-		ret = wilc_init_fw_config(dev, vif);
 
-		if (ret < 0) {
+		ret = wilc_init_fw_config(dev, vif);
+		if (ret) {
 			netdev_err(dev, "Failed to configure firmware\n");
-			ret = -EIO;
 			goto fail_fw_start;
 		}
 		wl->initialized = true;
@@ -600,11 +589,11 @@ static int wilc_mac_open(struct net_device *ndev)
 	netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
 
 	ret = wilc_init_host_int(ndev);
-	if (ret < 0)
+	if (ret)
 		return ret;
 
 	ret = wilc_wlan_initialize(ndev, vif);
-	if (ret < 0) {
+	if (ret) {
 		wilc_deinit_host_int(ndev);
 		return ret;
 	}
drivers/staging/wilc1000/sdio.c +75 -103

493 lines not on this page · raw message

drivers/staging/wilc1000/spi.c +68 -70

426 lines not on this page · raw message

drivers/staging/wilc1000/wlan.c +37 -38

237 lines not on this page · raw message

Signature · 1 line
2.24.0
#1 lore raw

PATCH 2/2 staging: wilc1000: avoid mutex unlock without lock in wilc_wlan_handle_txq()

From: Ajay Singh <[email protected]>

In wilc_wlan_handle_txq(), mutex unlock was called without acquiring it. Also error code for full VMM condition was incorrect as discussed in [1]. Now used a proper code to indicate VMM is full, for which transfer to VMM is required again. 'wilc_wlan_handle_txq()' should be called again if the VMM space was full earlier or otherwise based on 'txq_event' signal.

1. https://lore.kernel.org/driverdev-devel/[email protected]/

---
 drivers/staging/wilc1000/netdev.c |  2 +-
 drivers/staging/wilc1000/wlan.c   | 15 ++++++++++-----
 drivers/staging/wilc1000/wlan.h   |  1 +
 3 files changed, 12 insertions(+), 6 deletions(-)
drivers/staging/wilc1000/netdev.c +1 -1
index 0f48e74aa3ea..fce5bf2d82fa 100644
@@ -174,7 +174,7 @@ static int wilc_txq_task(void *vp)
 				}
 				srcu_read_unlock(&wl->srcu, srcu_idx);
 			}
-		} while (ret == -ENOBUFS && !wl->close);
+		} while (ret == WILC_VMM_ENTRY_FULL_RETRY && !wl->close);
 	}
 	return 0;
 }
drivers/staging/wilc1000/wlan.c +10 -5
index b904eda42806..601e4d1345d2 100644
@@ -489,12 +489,12 @@ int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
 	struct wilc_vif *vif;
 
 	if (wilc->quit)
-		goto out;
+		goto out_update_cnt;
 
 	mutex_lock(&wilc->txq_add_to_head_cs);
 	tqe = wilc_wlan_txq_get_first(wilc);
 	if (!tqe)
-		goto out;
+		goto out_unlock;
 	dev = tqe->vif->ndev;
 	wilc_wlan_txq_filter_dup_tcp_ack(dev);
 	i = 0;
@@ -526,7 +526,7 @@ int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
 	}
 
 	if (i == 0)
-		goto out;
+		goto out_unlock;
 	vmm_table[i] = 0x0;
 
 	acquire_bus(wilc, WILC_BUS_ACQUIRE_AND_WAKEUP);
@@ -595,7 +595,11 @@ int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
 		goto out_release_bus;
 
 	if (entries == 0) {
-		ret = -ENOBUFS;
+		/*
+		 * No VMM space available in firmware so retry to transmit
+		 * the packet from tx queue.
+		 */
+		ret = WILC_VMM_ENTRY_FULL_RETRY;
 		goto out_release_bus;
 	}
 
@@ -662,9 +666,10 @@ int wilc_wlan_handle_txq(struct wilc *wilc, u32 *txq_count)
 out_release_bus:
 	release_bus(wilc, WILC_BUS_RELEASE_ALLOW_SLEEP);
 
-out:
+out_unlock:
 	mutex_unlock(&wilc->txq_add_to_head_cs);
 
+out_update_cnt:
 	*txq_count = wilc->txq_entries;
 	return ret;
 }
drivers/staging/wilc1000/wlan.h +1 -0
index 44ae6ed6882c..8c4634262adb 100644
@@ -198,6 +198,7 @@
 #define IS_MGMT_STATUS_SUCCES	0x040
 
 #define WILC_WID_TYPE		GENMASK(15, 12)
+#define WILC_VMM_ENTRY_FULL_RETRY	1
 /********************************************
  *
  *      Tx/Rx Queue Structure
Signature · 1 line
2.24.0
Reviewed-by on v1 1/2
#2 lore raw
In reply to [email protected]

Looks good.

drivers/staging/wilc1000/sdio.c static int wilc_sdio_write_reg(struct wilc *wilc, u32 addr, u32 data) · line 391

Quoted 10 lines · On Thu, Jan 23, 2020 at 12:50:47PM +0000, [email protected] wrote:
drivers/staging/wilc1000/sdio.c +1 -3
@@ -384,19 +378,18 @@ static int wilc_sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
 		cmd.address = addr;
 		cmd.data = data;
 		ret = wilc_sdio_cmd52(wilc, &cmd);
-		if (ret) {
+		if (ret)
 			dev_err(&func->dev,
 				"Failed cmd 52, read reg (%08x) ...\n", addr);
-			goto fail;
-		}

Please don't resend, but try to avoid this sort of anti-pattern in the future. You're treating the last failure condition in the function as special. In this case it's particularly difficult to read because we are far from the bottom of the function, but even if we were right at the bottom, I would try to avoid it.

I am extreme enough that I would avoid even things like:

	ret = frob();
	if (ret)
		printf("ret failed\n");
	return ret;

Instead I would write:

	ret = frob();
	if (ret) {
		printf("ret failed\n");
		return ret;
	}
	return 0;

It's just nice to have the error path at indent level 2 and the success path at indent level 1. And the other thing that I like is the BIG BOLD "return 0;" at the end of the function. Some functions return positive numbers on success so if I see "return result;" then I have to look back a few lines to see if "result" can be positive.

The other anti-pattern which people often do is success handling (instead of error handling) for the last error condition in a function.

	ret = one();
	if (ret)
		return ret;
	ret = two();
	if (ret)
		return ret;
	ret = three();
	if (!ret)
		ret = four();
	return ret;

Never never do that. :P

Anyway, don't resend. It's just food for thought.

regards,
dan carpenter

Quoted 33 lines
drivers/staging/wilc1000/sdio.c +5 -10
 	} else {
 		struct sdio_cmd53 cmd;
 
 		/**
 		 *      set the AHB address
 		 **/
-		if (!wilc_sdio_set_func0_csa_address(wilc, addr))
-			goto fail;
+		ret = wilc_sdio_set_func0_csa_address(wilc, addr);
+		if (ret)
+			return ret;
 
 		cmd.read_write = 1;
 		cmd.function = 0;
@@ -407,18 +400,12 @@ static int wilc_sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
 		cmd.buffer = (u8 *)&data;
 		cmd.block_size = sdio_priv->block_size;
 		ret = wilc_sdio_cmd53(wilc, &cmd);
-		if (ret) {
+		if (ret)
 			dev_err(&func->dev,
 				"Failed cmd53, write reg (%08x)...\n", addr);
-			goto fail;
-		}
 	}
 
-	return 1;
-
-fail:
-
-	return 0;
+	return ret;
 }
#3 lore raw

Hi Dan,

Quoted 48 lines · Dan Carpenter

Looks good.

drivers/staging/wilc1000/sdio.c static int wilc_sdio_write_reg(struct wilc *wilc, u32 addr, u32 data) · line 391

Quoted 10 lines · On Thu, Jan 23, 2020 at 12:50:47PM +0000, [email protected] wrote:
drivers/staging/wilc1000/sdio.c +1 -3
@@ -384,19 +378,18 @@ static int wilc_sdio_write_reg(struct wilc *wilc, u32 addr, u32 data)
              cmd.address = addr;
              cmd.data = data;
              ret = wilc_sdio_cmd52(wilc, &cmd);
-             if (ret) {
+             if (ret)
                      dev_err(&func->dev,
                              "Failed cmd 52, read reg (%08x) ...\n", addr);
-                     goto fail;
-             }

Please don't resend, but try to avoid this sort of anti-pattern in the future. You're treating the last failure condition in the function as special. In this case it's particularly difficult to read because we are far from the bottom of the function, but even if we were right at the bottom, I would try to avoid it.

I am extreme enough that I would avoid even things like:

        ret = frob();
        if (ret)
                printf("ret failed\n");
        return ret;

Instead I would write:

        ret = frob();
        if (ret) {
                printf("ret failed\n");
                return ret;
        }
        return 0;

It's just nice to have the error path at indent level 2 and the success path at indent level 1. And the other thing that I like is the BIG BOLD "return 0;" at the end of the function. Some functions return positive numbers on success so if I see "return result;" then I have to look back a few lines to see if "result" can be positive.

The other anti-pattern which people often do is success handling (instead of error handling) for the last error condition in a function.

Quoted 11 lines
        ret = one();
        if (ret)
                return ret;
        ret = two();
        if (ret)
                return ret;
        ret = three();
        if (!ret)
                ret = four();
        return ret;

Thanks for your useful advice to avoid anti-pattern. I shall keep these points in mind while working on future patches.

Regards,
Ajay