netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marek Vasut <marex@denx.de>
To: netdev@vger.kernel.org
Cc: Marek Vasut <marex@denx.de>,
	"David S . Miller" <davem@davemloft.net>,
	Lukas Wunner <lukas@wunner.de>, Petr Stetiar <ynezz@true.cz>,
	YueHaibing <yuehaibing@huawei.com>
Subject: [PATCH V3 10/18] net: ks8851: Factor out bus lock handling
Date: Sat, 28 Mar 2020 01:31:40 +0100	[thread overview]
Message-ID: <20200328003148.498021-11-marex@denx.de> (raw)
In-Reply-To: <20200328003148.498021-1-marex@denx.de>

Pull out bus access locking code into separate functions, this is done
in preparation for unifying the driver with the parallel bus one. The
parallel bus driver does not need heavy mutex locking of the bus and
works better with spinlocks, hence prepare these locking functions to
be overridden then.

Signed-off-by: Marek Vasut <marex@denx.de>
Cc: David S. Miller <davem@davemloft.net>
Cc: Lukas Wunner <lukas@wunner.de>
Cc: Petr Stetiar <ynezz@true.cz>
Cc: YueHaibing <yuehaibing@huawei.com>
---
V3: New patch
---
 drivers/net/ethernet/micrel/ks8851.c | 97 +++++++++++++++++++---------
 1 file changed, 68 insertions(+), 29 deletions(-)

diff --git a/drivers/net/ethernet/micrel/ks8851.c b/drivers/net/ethernet/micrel/ks8851.c
index b9820068c839..7a91d30472f2 100644
--- a/drivers/net/ethernet/micrel/ks8851.c
+++ b/drivers/net/ethernet/micrel/ks8851.c
@@ -151,6 +151,30 @@ static int msg_enable;
 /* turn register number and byte-enable mask into data for start of packet */
 #define MK_OP(_byteen, _reg) (BYTE_EN(_byteen) | (_reg)  << (8+2) | (_reg) >> 6)
 
+/**
+ * ks8851_lock - register access lock
+ * @ks: The chip state
+ * @flags: Spinlock flags
+ *
+ * Claim chip register access lock
+ */
+static void ks8851_lock(struct ks8851_net *ks, unsigned long *flags)
+{
+	mutex_lock(&ks->lock);
+}
+
+/**
+ * ks8851_unlock - register access unlock
+ * @ks: The chip state
+ * @flags: Spinlock flags
+ *
+ * Release chip register access lock
+ */
+static void ks8851_unlock(struct ks8851_net *ks, unsigned long *flags)
+{
+	mutex_unlock(&ks->lock);
+}
+
 /* SPI register read/write calls.
  *
  * All these calls issue SPI transactions to access the chip's registers. They
@@ -304,10 +328,11 @@ static void ks8851_set_powermode(struct ks8851_net *ks, unsigned pwrmode)
 static int ks8851_write_mac_addr(struct net_device *dev)
 {
 	struct ks8851_net *ks = netdev_priv(dev);
+	unsigned long flags;
 	u16 val;
 	int i;
 
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 
 	/*
 	 * Wake up chip in case it was powered off when stopped; otherwise,
@@ -323,7 +348,7 @@ static int ks8851_write_mac_addr(struct net_device *dev)
 	if (!netif_running(dev))
 		ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
 
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 
 	return 0;
 }
@@ -337,10 +362,11 @@ static int ks8851_write_mac_addr(struct net_device *dev)
 static void ks8851_read_mac_addr(struct net_device *dev)
 {
 	struct ks8851_net *ks = netdev_priv(dev);
+	unsigned long flags;
 	u16 reg;
 	int i;
 
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 
 	for (i = 0; i < ETH_ALEN; i += 2) {
 		reg = ks8851_rdreg16(ks, KS_MAR(i));
@@ -348,7 +374,7 @@ static void ks8851_read_mac_addr(struct net_device *dev)
 		dev->dev_addr[i + 1] = reg & 0xff;
 	}
 
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 }
 
 /**
@@ -534,10 +560,11 @@ static void ks8851_rx_pkts(struct ks8851_net *ks)
 static irqreturn_t ks8851_irq(int irq, void *_ks)
 {
 	struct ks8851_net *ks = _ks;
-	unsigned status;
 	unsigned handled = 0;
+	unsigned long flags;
+	unsigned status;
 
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 
 	status = ks8851_rdreg16(ks, KS_ISR);
 
@@ -606,7 +633,7 @@ static irqreturn_t ks8851_irq(int irq, void *_ks)
 		ks8851_wrreg16(ks, KS_RXCR1, rxc->rxcr1);
 	}
 
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 
 	if (status & IRQ_LCI)
 		mii_check_link(&ks->mii);
@@ -700,10 +727,11 @@ static void ks8851_done_tx(struct ks8851_net *ks, struct sk_buff *txb)
 static void ks8851_tx_work(struct work_struct *work)
 {
 	struct ks8851_net *ks = container_of(work, struct ks8851_net, tx_work);
+	unsigned long flags;
 	struct sk_buff *txb;
 	bool last = skb_queue_empty(&ks->txq);
 
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 
 	while (!last) {
 		txb = skb_dequeue(&ks->txq);
@@ -719,7 +747,7 @@ static void ks8851_tx_work(struct work_struct *work)
 		}
 	}
 
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 }
 
 /**
@@ -732,6 +760,7 @@ static void ks8851_tx_work(struct work_struct *work)
 static int ks8851_net_open(struct net_device *dev)
 {
 	struct ks8851_net *ks = netdev_priv(dev);
+	unsigned long flags;
 	int ret;
 
 	ret = request_threaded_irq(dev->irq, NULL, ks8851_irq,
@@ -744,7 +773,7 @@ static int ks8851_net_open(struct net_device *dev)
 
 	/* lock the card, even if we may not actually be doing anything
 	 * else at the moment */
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 
 	netif_dbg(ks, ifup, ks->netdev, "opening\n");
 
@@ -804,7 +833,7 @@ static int ks8851_net_open(struct net_device *dev)
 
 	netif_dbg(ks, ifup, ks->netdev, "network device up\n");
 
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 	mii_check_link(&ks->mii);
 	return 0;
 }
@@ -820,22 +849,23 @@ static int ks8851_net_open(struct net_device *dev)
 static int ks8851_net_stop(struct net_device *dev)
 {
 	struct ks8851_net *ks = netdev_priv(dev);
+	unsigned long flags;
 
 	netif_info(ks, ifdown, dev, "shutting down\n");
 
 	netif_stop_queue(dev);
 
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 	/* turn off the IRQs and ack any outstanding */
 	ks8851_wrreg16(ks, KS_IER, 0x0000);
 	ks8851_wrreg16(ks, KS_ISR, 0xffff);
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 
 	/* stop any outstanding work */
 	flush_work(&ks->tx_work);
 	flush_work(&ks->rxctrl_work);
 
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 	/* shutdown RX process */
 	ks8851_wrreg16(ks, KS_RXCR1, 0x0000);
 
@@ -844,7 +874,7 @@ static int ks8851_net_stop(struct net_device *dev)
 
 	/* set powermode to soft power down to save power */
 	ks8851_set_powermode(ks, PMECR_PM_SOFTDOWN);
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 
 	/* ensure any queued tx buffers are dumped */
 	while (!skb_queue_empty(&ks->txq)) {
@@ -916,13 +946,14 @@ static netdev_tx_t ks8851_start_xmit(struct sk_buff *skb,
 static void ks8851_rxctrl_work(struct work_struct *work)
 {
 	struct ks8851_net *ks = container_of(work, struct ks8851_net, rxctrl_work);
+	unsigned long flags;
 
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 
 	/* need to shutdown RXQ before modifying filter parameters */
 	ks8851_wrreg16(ks, KS_RXCR1, 0x00);
 
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 }
 
 static void ks8851_set_rx_mode(struct net_device *dev)
@@ -1104,11 +1135,6 @@ static void ks8851_eeprom_regwrite(struct eeprom_93cx6 *ee)
  */
 static int ks8851_eeprom_claim(struct ks8851_net *ks)
 {
-	if (!(ks->rc_ccr & CCR_EEPROM))
-		return -ENOENT;
-
-	mutex_lock(&ks->lock);
-
 	/* start with clock low, cs high */
 	ks8851_wrreg16(ks, KS_EEPCR, EEPCR_EESA | EEPCR_EECS);
 	return 0;
@@ -1125,7 +1151,6 @@ static void ks8851_eeprom_release(struct ks8851_net *ks)
 	unsigned val = ks8851_rdreg16(ks, KS_EEPCR);
 
 	ks8851_wrreg16(ks, KS_EEPCR, val & ~EEPCR_EESA);
-	mutex_unlock(&ks->lock);
 }
 
 #define KS_EEPROM_MAGIC (0x00008851)
@@ -1135,6 +1160,7 @@ static int ks8851_set_eeprom(struct net_device *dev,
 {
 	struct ks8851_net *ks = netdev_priv(dev);
 	int offset = ee->offset;
+	unsigned long flags;
 	int len = ee->len;
 	u16 tmp;
 
@@ -1145,9 +1171,13 @@ static int ks8851_set_eeprom(struct net_device *dev,
 	if (ee->magic != KS_EEPROM_MAGIC)
 		return -EINVAL;
 
-	if (ks8851_eeprom_claim(ks))
+	if (!(ks->rc_ccr & CCR_EEPROM))
 		return -ENOENT;
 
+	ks8851_lock(ks, &flags);
+
+	ks8851_eeprom_claim(ks);
+
 	eeprom_93cx6_wren(&ks->eeprom, true);
 
 	/* ethtool currently only supports writing bytes, which means
@@ -1167,6 +1197,7 @@ static int ks8851_set_eeprom(struct net_device *dev,
 	eeprom_93cx6_wren(&ks->eeprom, false);
 
 	ks8851_eeprom_release(ks);
+	ks8851_unlock(ks, &flags);
 
 	return 0;
 }
@@ -1176,19 +1207,25 @@ static int ks8851_get_eeprom(struct net_device *dev,
 {
 	struct ks8851_net *ks = netdev_priv(dev);
 	int offset = ee->offset;
+	unsigned long flags;
 	int len = ee->len;
 
 	/* must be 2 byte aligned */
 	if (len & 1 || offset & 1)
 		return -EINVAL;
 
-	if (ks8851_eeprom_claim(ks))
+	if (!(ks->rc_ccr & CCR_EEPROM))
 		return -ENOENT;
 
+	ks8851_lock(ks, &flags);
+
+	ks8851_eeprom_claim(ks);
+
 	ee->magic = KS_EEPROM_MAGIC;
 
 	eeprom_93cx6_multiread(&ks->eeprom, offset/2, (__le16 *)data, len/2);
 	ks8851_eeprom_release(ks);
+	ks8851_unlock(ks, &flags);
 
 	return 0;
 }
@@ -1262,6 +1299,7 @@ static int ks8851_phy_reg(int reg)
 static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
 {
 	struct ks8851_net *ks = netdev_priv(dev);
+	unsigned long flags;
 	int ksreg;
 	int result;
 
@@ -1269,9 +1307,9 @@ static int ks8851_phy_read(struct net_device *dev, int phy_addr, int reg)
 	if (!ksreg)
 		return 0x0;	/* no error return allowed, so use zero */
 
-	mutex_lock(&ks->lock);
+	ks8851_lock(ks, &flags);
 	result = ks8851_rdreg16(ks, ksreg);
-	mutex_unlock(&ks->lock);
+	ks8851_unlock(ks, &flags);
 
 	return result;
 }
@@ -1280,13 +1318,14 @@ static void ks8851_phy_write(struct net_device *dev,
 			     int phy, int reg, int value)
 {
 	struct ks8851_net *ks = netdev_priv(dev);
+	unsigned long flags;
 	int ksreg;
 
 	ksreg = ks8851_phy_reg(reg);
 	if (ksreg) {
-		mutex_lock(&ks->lock);
+		ks8851_lock(ks, &flags);
 		ks8851_wrreg16(ks, ksreg, value);
-		mutex_unlock(&ks->lock);
+		ks8851_unlock(ks, &flags);
 	}
 }
 
-- 
2.25.1


  parent reply	other threads:[~2020-03-28  0:32 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-28  0:31 [PATCH V3 00/18] net: ks8851: Unify KS8851 SPI and MLL drivers Marek Vasut
2020-03-28  0:31 ` [PATCH V3 01/18] net: ks8851: Factor out spi->dev in probe()/remove() Marek Vasut
2020-03-28  0:31 ` [PATCH V3 02/18] net: ks8851: Rename ndev to netdev in probe Marek Vasut
2020-03-28  0:31 ` [PATCH V3 03/18] net: ks8851: Replace dev_err() with netdev_err() in IRQ handler Marek Vasut
2020-03-28  0:31 ` [PATCH V3 04/18] net: ks8851: Pass device node into ks8851_init_mac() Marek Vasut
2020-03-28  0:31 ` [PATCH V3 05/18] net: ks8851: Use devm_alloc_etherdev() Marek Vasut
2020-03-28  0:31 ` [PATCH V3 06/18] net: ks8851: Use dev_{get,set}_drvdata() Marek Vasut
2020-03-28  0:31 ` [PATCH V3 07/18] net: ks8851: Remove ks8851_rdreg32() Marek Vasut
2020-03-28  0:31 ` [PATCH V3 08/18] net: ks8851: Use 16-bit writes to program MAC address Marek Vasut
2020-03-28  0:31 ` [PATCH V3 09/18] net: ks8851: Use 16-bit read of RXFC register Marek Vasut
2020-03-28  0:31 ` Marek Vasut [this message]
2020-03-28  0:31 ` [PATCH V3 11/18] net: ks8851: Factor out SKB receive function Marek Vasut
2020-03-28  0:31 ` [PATCH V3 12/18] net: ks8851: Split out SPI specific entries in struct ks8851_net Marek Vasut
2020-03-28  0:31 ` [PATCH V3 13/18] net: ks8851: Split out SPI specific code from probe() and remove() Marek Vasut
2020-03-28  0:31 ` [PATCH V3 14/18] net: ks8851: Factor out TX work flush function Marek Vasut
2020-03-28  0:31 ` [PATCH V3 15/18] net: ks8851: Permit overridding interrupt enable register Marek Vasut
2020-03-28  0:31 ` [PATCH V3 16/18] net: ks8851: Separate SPI operations into separate file Marek Vasut
2020-03-28  2:45   ` kbuild test robot
2020-03-28  4:11   ` kbuild test robot
2020-03-28  0:31 ` [PATCH V3 17/18] net: ks8851: Implement Parallel bus operations Marek Vasut
2020-03-28  0:31 ` [PATCH V3 18/18] net: ks8851: Remove ks8851_mll.c Marek Vasut
2020-03-29 16:15 ` [PATCH V3 00/18] net: ks8851: Unify KS8851 SPI and MLL drivers Marek Vasut
2020-04-06  3:16 ` Lukas Wunner
2020-04-06 11:20   ` Marek Vasut
2020-04-08 19:49     ` Marek Vasut
2020-04-10 11:01       ` Lukas Wunner
2020-04-10 18:10         ` Marek Vasut
2020-04-14 18:20           ` [PATCH V4 00/19] " Marek Vasut
2020-04-14 18:20             ` [PATCH V4 01/19] net: ks8851: Factor out spi->dev in probe()/remove() Marek Vasut
2020-04-14 18:20             ` [PATCH V4 02/19] net: ks8851: Rename ndev to netdev in probe Marek Vasut
2020-04-14 18:20             ` [PATCH V4 03/19] net: ks8851: Replace dev_err() with netdev_err() in IRQ handler Marek Vasut
2020-04-14 18:20             ` [PATCH V4 04/19] net: ks8851: Pass device node into ks8851_init_mac() Marek Vasut
2020-04-14 18:20             ` [PATCH V4 05/19] net: ks8851: Use devm_alloc_etherdev() Marek Vasut
2020-04-14 18:20             ` [PATCH V4 06/19] net: ks8851: Use dev_{get,set}_drvdata() Marek Vasut
2020-04-14 18:20             ` [PATCH V4 07/19] net: ks8851: Remove ks8851_rdreg32() Marek Vasut
2020-04-20 14:07               ` Lukas Wunner
2020-04-20 14:12                 ` Marek Vasut
2020-04-20 14:20                   ` Lukas Wunner
2020-04-20 14:24                     ` Marek Vasut
2020-04-20 14:44                       ` Lukas Wunner
2020-04-20 15:38                         ` Marek Vasut
2020-04-20 15:50                           ` Andrew Lunn
2020-05-07 14:22                             ` Marek Vasut
2020-04-14 18:20             ` [PATCH V4 08/19] net: ks8851: Use 16-bit writes to program MAC address Marek Vasut
2020-04-14 18:20             ` [PATCH V4 09/19] net: ks8851: Use 16-bit read of RXFC register Marek Vasut
2020-04-14 18:20             ` [PATCH V4 10/19] net: ks8851: Factor out bus lock handling Marek Vasut
2020-04-14 18:20             ` [PATCH V4 11/19] net: ks8851: Factor out SKB receive function Marek Vasut
2020-04-14 18:20             ` [PATCH V4 12/19] net: ks8851: Split out SPI specific entries in struct ks8851_net Marek Vasut
2020-04-14 18:20             ` [PATCH V4 13/19] net: ks8851: Split out SPI specific code from probe() and remove() Marek Vasut
2020-04-14 18:20             ` [PATCH V4 14/19] net: ks8851: Factor out TX work flush function Marek Vasut
2020-04-14 18:20             ` [PATCH V4 15/19] net: ks8851: Permit overridding interrupt enable register Marek Vasut
2020-04-14 18:20             ` [PATCH V4 16/19] net: ks8851: Implement register, FIFO, lock accessor callbacks Marek Vasut
2020-04-14 18:20             ` [PATCH V4 17/19] net: ks8851: Separate SPI operations into separate file Marek Vasut
2020-04-15 14:56               ` Lukas Wunner
2020-04-16  9:58                 ` Marek Vasut
2020-04-14 18:20             ` [PATCH V4 18/19] net: ks8851: Implement Parallel bus operations Marek Vasut
2020-04-14 18:20             ` [PATCH V4 19/19] net: ks8851: Remove ks8851_mll.c Marek Vasut
2020-04-14 20:13             ` [PATCH V4 00/19] net: ks8851: Unify KS8851 SPI and MLL drivers David Miller
2020-04-15 14:39             ` Lukas Wunner
2020-04-15 14:51               ` Marek Vasut
2020-04-15 15:12                 ` Lukas Wunner
2020-04-16 10:18                   ` Marek Vasut

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=20200328003148.498021-11-marex@denx.de \
    --to=marex@denx.de \
    --cc=davem@davemloft.net \
    --cc=lukas@wunner.de \
    --cc=netdev@vger.kernel.org \
    --cc=ynezz@true.cz \
    --cc=yuehaibing@huawei.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).