All of lore.kernel.org
 help / color / mirror / Atom feed
From: Oliver Neukum <oneukum@suse.com>
To: hayeswang@realtek.com, netdev@vger.kernel.org
Cc: Oliver Neukum <oneukum@suse.com>
Subject: [RFC] r8152: pass through needs to be singular
Date: Thu, 28 Jul 2022 21:18:51 +0200	[thread overview]
Message-ID: <20220728191851.30402-1-oneukum@suse.com> (raw)

If multiple devices are connected only one of them
is allowed to get the pass through MAC

Signed-off-by: Oliver Neukum <oneukum@suse.com>
---
 drivers/net/usb/r8152.c | 38 ++++++++++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/drivers/net/usb/r8152.c b/drivers/net/usb/r8152.c
index 0f6efaabaa32..15f695398284 100644
--- a/drivers/net/usb/r8152.c
+++ b/drivers/net/usb/r8152.c
@@ -1193,6 +1193,9 @@ static unsigned int agg_buf_sz = 16384;
 
 #define RTL_LIMITED_TSO_SIZE	(size_to_mtu(agg_buf_sz) - sizeof(struct tx_desc))
 
+static struct r8152 *holder_of_pass_through = NULL;
+static DEFINE_MUTEX(pass_through_lock);
+
 static
 int get_registers(struct r8152 *tp, u16 value, u16 index, u16 size, void *data)
 {
@@ -1587,8 +1590,19 @@ static int __rtl8152_set_mac_address(struct net_device *netdev, void *p,
 	return ret;
 }
 
+static void give_up_pass_through(struct r8152 *tp)
+{
+	mutex_lock(&pass_through_lock);
+	if (tp == holder_of_pass_through)
+		holder_of_pass_through = NULL;
+	mutex_unlock(&pass_through_lock);
+}
+
 static int rtl8152_set_mac_address(struct net_device *netdev, void *p)
 {
+	struct r8152 *tp = netdev_priv(netdev);
+
+	give_up_pass_through(tp);
 	return __rtl8152_set_mac_address(netdev, p, false);
 }
 
@@ -1608,6 +1622,12 @@ static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
 	acpi_object_type mac_obj_type;
 	int mac_strlen;
 
+	mutex_lock(&pass_through_lock);
+
+	if (!holder_of_pass_through) {
+		ret = -EBUSY;
+		goto failout;
+	}
 	if (tp->lenovo_macpassthru) {
 		mac_obj_name = "\\MACA";
 		mac_obj_type = ACPI_TYPE_STRING;
@@ -1621,7 +1641,8 @@ static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
 			if ((ocp_data & PASS_THRU_MASK) != 1) {
 				netif_dbg(tp, probe, tp->netdev,
 						"No efuse for RTL8153-AD MAC pass through\n");
-				return -ENODEV;
+				ret = -ENODEV;
+				goto failout;
 			}
 		} else {
 			/* test for RTL8153-BND and RTL8153-BD */
@@ -1629,7 +1650,8 @@ static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
 			if ((ocp_data & BND_MASK) == 0 && (ocp_data & BD_MASK) == 0) {
 				netif_dbg(tp, probe, tp->netdev,
 						"Invalid variant for MAC pass through\n");
-				return -ENODEV;
+				ret = -ENODEV;
+				goto failout;
 			}
 		}
 
@@ -1641,8 +1663,10 @@ static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
 	/* returns _AUXMAC_#AABBCCDDEEFF# */
 	status = acpi_evaluate_object(NULL, mac_obj_name, NULL, &buffer);
 	obj = (union acpi_object *)buffer.pointer;
-	if (!ACPI_SUCCESS(status))
-		return -ENODEV;
+	if (!ACPI_SUCCESS(status)) {
+		ret = -ENODEV;
+		goto failout;
+	}
 	if (obj->type != mac_obj_type || obj->string.length != mac_strlen) {
 		netif_warn(tp, probe, tp->netdev,
 			   "Invalid buffer for pass-thru MAC addr: (%d, %d)\n",
@@ -1670,6 +1694,10 @@ static int vendor_mac_passthru_addr_read(struct r8152 *tp, struct sockaddr *sa)
 
 amacout:
 	kfree(obj);
+failout:
+	if (!ret)
+		holder_of_pass_through = tp;
+	mutex_unlock(&pass_through_lock);
 	return ret;
 }
 
@@ -8287,6 +8315,7 @@ static int rtl8152_pre_reset(struct usb_interface *intf)
 		tp->rtl_ops.disable(tp);
 		mutex_unlock(&tp->control);
 	}
+	give_up_pass_through(tp);
 
 	return 0;
 }
@@ -9802,6 +9831,7 @@ static void rtl8152_disconnect(struct usb_interface *intf)
 		cancel_delayed_work_sync(&tp->hw_phy_work);
 		if (tp->rtl_ops.unload)
 			tp->rtl_ops.unload(tp);
+		give_up_pass_through(tp);
 		rtl8152_release_firmware(tp);
 		free_netdev(tp->netdev);
 	}
-- 
2.35.3


             reply	other threads:[~2022-07-28 19:18 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-28 19:18 Oliver Neukum [this message]
2022-07-28 22:11 ` [RFC] r8152: pass through needs to be singular Andrew Lunn
2022-08-02 10:52   ` Oliver Neukum
2022-08-02 13:31     ` Andrew Lunn
2022-08-04  9:24       ` Oliver Neukum
2022-08-04 13:24         ` Andrew Lunn
2022-08-04 13:29           ` Oliver Neukum
2022-08-04 14:31             ` Andrew Lunn
2022-08-03  3:48 ` Hayes Wang
2022-08-04  8:57   ` Oliver Neukum
2022-08-08  7:04     ` Hayes Wang
2022-08-23  7:53       ` Oliver Neukum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220728191851.30402-1-oneukum@suse.com \
    --to=oneukum@suse.com \
    --cc=hayeswang@realtek.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.