[PATCH RFC net-next 07/19] net: dsa: tag_gswip: switch to bitfield helpers

From: Alexander Lobakin
Date: Mon Dec 30 2019 - 09:33:12 EST


Make code look cleaner and more readable by using bitfield macros instead
of open-coding masks and shifts.
Misc: remove redundant variable in gswip_tag_xmit(), make dsa_port const.

Signed-off-by: Alexander Lobakin <alobakin@xxxxxxxx>
---
net/dsa/tag_gswip.c | 24 +++++++++---------------
1 file changed, 9 insertions(+), 15 deletions(-)

diff --git a/net/dsa/tag_gswip.c b/net/dsa/tag_gswip.c
index 408d4af390a0..de920f6aac5b 100644
--- a/net/dsa/tag_gswip.c
+++ b/net/dsa/tag_gswip.c
@@ -5,6 +5,7 @@
* Copyright (C) 2017 - 2018 Hauke Mehrtens <hauke@xxxxxxxxxx>
*/

+#include <linux/bitfield.h>
#include <linux/bitops.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
@@ -45,26 +46,22 @@
#define GSWIP_TX_CLASS_MASK GENMASK(3, 0)

/* Byte 3 */
+#define GSWIP_TX_PORT_MAP(port) FIELD_PREP(GENMASK(6, 1), BIT(port))
#define GSWIP_TX_DPID_EN BIT(0)
-#define GSWIP_TX_PORT_MAP_SHIFT 1
-#define GSWIP_TX_PORT_MAP_MASK GENMASK(6, 1)

-#define GSWIP_RX_HEADER_LEN 8
+#define GSWIP_RX_HEADER_LEN 8

/* special tag in RX path header */
/* Byte 7 */
-#define GSWIP_RX_SPPID_SHIFT 4
-#define GSWIP_RX_SPPID_MASK GENMASK(6, 4)
+#define GSWIP_RX_SPPID(byte_7) FIELD_GET(GENMASK(6, 4), byte_7)

static struct sk_buff *gswip_tag_xmit(struct sk_buff *skb,
struct net_device *dev)
{
- struct dsa_port *dp = dsa_slave_to_port(dev);
- int err;
+ const struct dsa_port *dp = dsa_slave_to_port(dev);
u8 *gswip_tag;

- err = skb_cow_head(skb, GSWIP_TX_HEADER_LEN);
- if (err)
+ if (skb_cow_head(skb, GSWIP_TX_HEADER_LEN))
return NULL;

skb_push(skb, GSWIP_TX_HEADER_LEN);
@@ -73,8 +70,7 @@ static struct sk_buff *gswip_tag_xmit(struct sk_buff *skb,
gswip_tag[0] = GSWIP_TX_SLPID_CPU;
gswip_tag[1] = GSWIP_TX_DPID_ELAN;
gswip_tag[2] = GSWIP_TX_PORT_MAP_EN | GSWIP_TX_PORT_MAP_SEL;
- gswip_tag[3] = BIT(dp->index + GSWIP_TX_PORT_MAP_SHIFT) & GSWIP_TX_PORT_MAP_MASK;
- gswip_tag[3] |= GSWIP_TX_DPID_EN;
+ gswip_tag[3] = GSWIP_TX_PORT_MAP(dp->index) | GSWIP_TX_DPID_EN;

return skb;
}
@@ -84,15 +80,13 @@ static struct sk_buff *gswip_tag_rcv(struct sk_buff *skb,
struct packet_type *pt)
{
int port;
- u8 *gswip_tag;

if (unlikely(!pskb_may_pull(skb, GSWIP_RX_HEADER_LEN)))
return NULL;

- gswip_tag = skb->data - ETH_HLEN;
-
/* Get source port information */
- port = (gswip_tag[7] & GSWIP_RX_SPPID_MASK) >> GSWIP_RX_SPPID_SHIFT;
+ port = GSWIP_RX_SPPID(*(skb->data - ETH_HLEN + 7));
+
skb->dev = dsa_master_find_slave(dev, 0, port);
if (!skb->dev)
return NULL;
--
2.24.1