]>
Commit | Line | Data |
---|---|---|
5037d532 FF |
1 | /* |
2 | * Broadcom tag support | |
3 | * | |
4 | * Copyright (C) 2014 Broadcom Corporation | |
5 | * | |
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. | |
10 | */ | |
11 | ||
12 | #include <linux/etherdevice.h> | |
13 | #include <linux/list.h> | |
5037d532 | 14 | #include <linux/slab.h> |
ea5dd34b | 15 | |
5037d532 FF |
16 | #include "dsa_priv.h" |
17 | ||
18 | /* This tag length is 4 bytes, older ones were 6 bytes, we do not | |
19 | * handle them | |
20 | */ | |
21 | #define BRCM_TAG_LEN 4 | |
22 | ||
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. | |
27 | */ | |
28 | ||
29 | /* Ingress and egress opcodes */ | |
30 | #define BRCM_OPCODE_SHIFT 5 | |
31 | #define BRCM_OPCODE_MASK 0x7 | |
32 | ||
33 | /* Ingress fields */ | |
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 | |
43 | ||
44 | /* Egress fields */ | |
45 | ||
46 | /* 2nd byte in the tag */ | |
47 | #define BRCM_EG_CID_MASK 0xff | |
48 | ||
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 | |
61 | ||
4ed70ce9 | 62 | static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb, struct net_device *dev) |
5037d532 FF |
63 | { |
64 | struct dsa_slave_priv *p = netdev_priv(dev); | |
65 | u8 *brcm_tag; | |
66 | ||
5037d532 | 67 | if (skb_cow_head(skb, BRCM_TAG_LEN) < 0) |
fe47d563 | 68 | return NULL; |
5037d532 FF |
69 | |
70 | skb_push(skb, BRCM_TAG_LEN); | |
71 | ||
72 | memmove(skb->data, skb->data + BRCM_TAG_LEN, 2 * ETH_ALEN); | |
73 | ||
74 | /* Build the tag after the MAC Source Address */ | |
75 | brcm_tag = skb->data + 2 * ETH_ALEN; | |
76 | ||
77 | /* Set the ingress opcode, traffic class, tag enforcment is | |
78 | * deprecated | |
79 | */ | |
80 | brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) | | |
81 | ((skb->priority << BRCM_IG_TC_SHIFT) & BRCM_IG_TC_MASK); | |
82 | brcm_tag[1] = 0; | |
83 | brcm_tag[2] = 0; | |
afdcf151 | 84 | if (p->dp->index == 8) |
5037d532 | 85 | brcm_tag[2] = BRCM_IG_DSTMAP2_MASK; |
afdcf151 | 86 | brcm_tag[3] = (1 << p->dp->index) & BRCM_IG_DSTMAP1_MASK; |
5037d532 | 87 | |
4ed70ce9 | 88 | return skb; |
5037d532 FF |
89 | } |
90 | ||
a86d8bec FF |
91 | static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev, |
92 | struct packet_type *pt, | |
93 | struct net_device *orig_dev) | |
5037d532 FF |
94 | { |
95 | struct dsa_switch_tree *dst = dev->dsa_ptr; | |
3cc9f257 FF |
96 | struct dsa_port *cpu_dp = dsa_get_cpu_port(dst); |
97 | struct dsa_switch *ds = cpu_dp->ds; | |
5037d532 FF |
98 | int source_port; |
99 | u8 *brcm_tag; | |
100 | ||
5037d532 | 101 | if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN))) |
54709795 | 102 | return NULL; |
5037d532 FF |
103 | |
104 | /* skb->data points to the EtherType, the tag is right before it */ | |
105 | brcm_tag = skb->data - 2; | |
106 | ||
107 | /* The opcode should never be different than 0b000 */ | |
108 | if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK)) | |
54709795 | 109 | return NULL; |
5037d532 FF |
110 | |
111 | /* We should never see a reserved reason code without knowing how to | |
112 | * handle it | |
113 | */ | |
82272db8 | 114 | if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD)) |
54709795 | 115 | return NULL; |
5037d532 FF |
116 | |
117 | /* Locate which port this is coming from */ | |
118 | source_port = brcm_tag[3] & BRCM_EG_PID_MASK; | |
119 | ||
120 | /* Validate port against switch setup, either the port is totally */ | |
26895e29 | 121 | if (source_port >= ds->num_ports || !ds->ports[source_port].netdev) |
54709795 | 122 | return NULL; |
5037d532 FF |
123 | |
124 | /* Remove Broadcom tag and update checksum */ | |
125 | skb_pull_rcsum(skb, BRCM_TAG_LEN); | |
126 | ||
127 | /* Move the Ethernet DA and SA */ | |
128 | memmove(skb->data - ETH_HLEN, | |
129 | skb->data - ETH_HLEN - BRCM_TAG_LEN, | |
130 | 2 * ETH_ALEN); | |
131 | ||
c8b09808 | 132 | skb->dev = ds->ports[source_port].netdev; |
5037d532 | 133 | |
a86d8bec | 134 | return skb; |
5037d532 FF |
135 | } |
136 | ||
137 | const struct dsa_device_ops brcm_netdev_ops = { | |
138 | .xmit = brcm_tag_xmit, | |
139 | .rcv = brcm_tag_rcv, | |
140 | }; |