]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/dsa/tag_edsa.c
UBUNTU: [Config] CONFIG_SERIAL_8250_ASPEED_VUART=m
[mirror_ubuntu-artful-kernel.git] / net / dsa / tag_edsa.c
CommitLineData
91da11f8
LB
1/*
2 * net/dsa/tag_edsa.c - Ethertype DSA tagging
e84665c9 3 * Copyright (c) 2008-2009 Marvell Semiconductor
91da11f8
LB
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 */
10
11#include <linux/etherdevice.h>
12#include <linux/list.h>
5a0e3ad6 13#include <linux/slab.h>
ea5dd34b 14
91da11f8
LB
15#include "dsa_priv.h"
16
17#define DSA_HLEN 4
18#define EDSA_HLEN 8
19
4ed70ce9 20static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
91da11f8
LB
21{
22 struct dsa_slave_priv *p = netdev_priv(dev);
23 u8 *edsa_header;
24
91da11f8
LB
25 /*
26 * Convert the outermost 802.1q tag to a DSA tag and prepend
27 * a DSA ethertype field is the packet is tagged, or insert
28 * a DSA ethertype plus DSA tag between the addresses and the
29 * current ethertype field if the packet is untagged.
30 */
31 if (skb->protocol == htons(ETH_P_8021Q)) {
32 if (skb_cow_head(skb, DSA_HLEN) < 0)
fe47d563 33 return NULL;
91da11f8
LB
34 skb_push(skb, DSA_HLEN);
35
36 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
37
38 /*
39 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
40 */
41 edsa_header = skb->data + 2 * ETH_ALEN;
42 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
43 edsa_header[1] = ETH_P_EDSA & 0xff;
44 edsa_header[2] = 0x00;
45 edsa_header[3] = 0x00;
afdcf151
VD
46 edsa_header[4] = 0x60 | p->dp->ds->index;
47 edsa_header[5] = p->dp->index << 3;
91da11f8
LB
48
49 /*
50 * Move CFI field from byte 6 to byte 5.
51 */
52 if (edsa_header[6] & 0x10) {
53 edsa_header[5] |= 0x01;
54 edsa_header[6] &= ~0x10;
55 }
56 } else {
57 if (skb_cow_head(skb, EDSA_HLEN) < 0)
fe47d563 58 return NULL;
91da11f8
LB
59 skb_push(skb, EDSA_HLEN);
60
61 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
62
63 /*
64 * Construct untagged FROM_CPU DSA tag.
65 */
66 edsa_header = skb->data + 2 * ETH_ALEN;
67 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
68 edsa_header[1] = ETH_P_EDSA & 0xff;
69 edsa_header[2] = 0x00;
70 edsa_header[3] = 0x00;
afdcf151
VD
71 edsa_header[4] = 0x40 | p->dp->ds->index;
72 edsa_header[5] = p->dp->index << 3;
91da11f8
LB
73 edsa_header[6] = 0x00;
74 edsa_header[7] = 0x00;
75 }
76
4ed70ce9 77 return skb;
91da11f8
LB
78}
79
a86d8bec
FF
80static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev,
81 struct packet_type *pt,
82 struct net_device *orig_dev)
91da11f8 83{
e84665c9
LB
84 struct dsa_switch_tree *dst = dev->dsa_ptr;
85 struct dsa_switch *ds;
91da11f8 86 u8 *edsa_header;
e84665c9 87 int source_device;
91da11f8
LB
88 int source_port;
89
91da11f8 90 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
54709795 91 return NULL;
91da11f8
LB
92
93 /*
94 * Skip the two null bytes after the ethertype.
95 */
96 edsa_header = skb->data + 2;
97
98 /*
e84665c9 99 * Check that frame type is either TO_CPU or FORWARD.
91da11f8 100 */
e84665c9 101 if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0)
54709795 102 return NULL;
91da11f8
LB
103
104 /*
e84665c9 105 * Determine source device and port.
91da11f8 106 */
e84665c9 107 source_device = edsa_header[0] & 0x1f;
91da11f8 108 source_port = (edsa_header[1] >> 3) & 0x1f;
e84665c9
LB
109
110 /*
111 * Check that the source device exists and that the source
112 * port is a registered DSA port.
113 */
149cafd7 114 if (source_device >= DSA_MAX_SWITCHES)
54709795 115 return NULL;
149cafd7 116
e84665c9 117 ds = dst->ds[source_device];
149cafd7 118 if (!ds)
54709795 119 return NULL;
149cafd7 120
26895e29 121 if (source_port >= ds->num_ports || !ds->ports[source_port].netdev)
54709795 122 return NULL;
91da11f8
LB
123
124 /*
125 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
126 * tag and delete the ethertype part. If the 'tagged' bit is
127 * clear, delete the ethertype and the DSA tag parts.
128 */
129 if (edsa_header[0] & 0x20) {
130 u8 new_header[4];
131
132 /*
133 * Insert 802.1q ethertype and copy the VLAN-related
134 * fields, but clear the bit that will hold CFI (since
135 * DSA uses that bit location for another purpose).
136 */
137 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
138 new_header[1] = ETH_P_8021Q & 0xff;
139 new_header[2] = edsa_header[2] & ~0x10;
140 new_header[3] = edsa_header[3];
141
142 /*
143 * Move CFI bit from its place in the DSA header to
144 * its 802.1q-designated place.
145 */
146 if (edsa_header[1] & 0x01)
147 new_header[2] |= 0x10;
148
149 skb_pull_rcsum(skb, DSA_HLEN);
150
151 /*
152 * Update packet checksum if skb is CHECKSUM_COMPLETE.
153 */
154 if (skb->ip_summed == CHECKSUM_COMPLETE) {
155 __wsum c = skb->csum;
156 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
157 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
158 skb->csum = c;
159 }
160
161 memcpy(edsa_header, new_header, DSA_HLEN);
162
163 memmove(skb->data - ETH_HLEN,
164 skb->data - ETH_HLEN - DSA_HLEN,
165 2 * ETH_ALEN);
166 } else {
167 /*
168 * Remove DSA tag and update checksum.
169 */
170 skb_pull_rcsum(skb, EDSA_HLEN);
171 memmove(skb->data - ETH_HLEN,
172 skb->data - ETH_HLEN - EDSA_HLEN,
173 2 * ETH_ALEN);
174 }
175
c8b09808 176 skb->dev = ds->ports[source_port].netdev;
91da11f8 177
a86d8bec 178 return skb;
91da11f8
LB
179}
180
3e8a72d1
FF
181const struct dsa_device_ops edsa_netdev_ops = {
182 .xmit = edsa_xmit,
183 .rcv = edsa_rcv,
91da11f8 184};