4 * Copyright (C) 2014 Broadcom Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/etherdevice.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
18 /* This tag length is 4 bytes, older ones were 6 bytes, we do not
21 #define BRCM_TAG_LEN 4
23 /* Tag is constructed and desconstructed using byte by byte access
24 * because the tag is placed after the MAC Source Address, which does
25 * not make it 4-bytes aligned, so this might cause unaligned accesses
26 * on most systems where this is used.
29 /* Ingress and egress opcodes */
30 #define BRCM_OPCODE_SHIFT 5
31 #define BRCM_OPCODE_MASK 0x7
34 /* 1st byte in the tag */
35 #define BRCM_IG_TC_SHIFT 2
36 #define BRCM_IG_TC_MASK 0x7
37 /* 2nd byte in the tag */
38 #define BRCM_IG_TE_MASK 0x3
39 #define BRCM_IG_TS_SHIFT 7
40 /* 3rd byte in the tag */
41 #define BRCM_IG_DSTMAP2_MASK 1
42 #define BRCM_IG_DSTMAP1_MASK 0xff
46 /* 2nd byte in the tag */
47 #define BRCM_EG_CID_MASK 0xff
49 /* 3rd byte in the tag */
50 #define BRCM_EG_RC_MASK 0xff
51 #define BRCM_EG_RC_RSVD (3 << 6)
52 #define BRCM_EG_RC_EXCEPTION (1 << 5)
53 #define BRCM_EG_RC_PROT_SNOOP (1 << 4)
54 #define BRCM_EG_RC_PROT_TERM (1 << 3)
55 #define BRCM_EG_RC_SWITCH (1 << 2)
56 #define BRCM_EG_RC_MAC_LEARN (1 << 1)
57 #define BRCM_EG_RC_MIRROR (1 << 0)
58 #define BRCM_EG_TC_SHIFT 5
59 #define BRCM_EG_TC_MASK 0x7
60 #define BRCM_EG_PID_MASK 0x1f
62 static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb, struct net_device *dev)
64 struct dsa_port *dp = dsa_slave_to_port(dev);
65 u16 queue = skb_get_queue_mapping(skb);
68 if (skb_cow_head(skb, BRCM_TAG_LEN) < 0)
71 skb_push(skb, BRCM_TAG_LEN);
73 memmove(skb->data, skb->data + BRCM_TAG_LEN, 2 * ETH_ALEN);
75 /* Build the tag after the MAC Source Address */
76 brcm_tag = skb->data + 2 * ETH_ALEN;
78 /* Set the ingress opcode, traffic class, tag enforcment is
81 brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) |
82 ((queue & BRCM_IG_TC_MASK) << BRCM_IG_TC_SHIFT);
86 brcm_tag[2] = BRCM_IG_DSTMAP2_MASK;
87 brcm_tag[3] = (1 << dp->index) & BRCM_IG_DSTMAP1_MASK;
89 /* Now tell the master network device about the desired output queue
92 skb_set_queue_mapping(skb, BRCM_TAG_SET_PORT_QUEUE(dp->index, queue));
97 static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev,
98 struct packet_type *pt)
103 if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN)))
106 /* skb->data points to the EtherType, the tag is right before it */
107 brcm_tag = skb->data - 2;
109 /* The opcode should never be different than 0b000 */
110 if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK))
113 /* We should never see a reserved reason code without knowing how to
116 if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD))
119 /* Locate which port this is coming from */
120 source_port = brcm_tag[3] & BRCM_EG_PID_MASK;
122 skb->dev = dsa_master_find_slave(dev, 0, source_port);
126 /* Remove Broadcom tag and update checksum */
127 skb_pull_rcsum(skb, BRCM_TAG_LEN);
129 /* Move the Ethernet DA and SA */
130 memmove(skb->data - ETH_HLEN,
131 skb->data - ETH_HLEN - BRCM_TAG_LEN,
137 const struct dsa_device_ops brcm_netdev_ops = {
138 .xmit = brcm_tag_xmit,