Merge tag 'devicetree-for-linus' of git://git.secretlab.ca/git/linux
[linux-2.6.git] / net / dsa / tag_edsa.c
blobe70c43c25e64c9310d3d5a61b407d8eeb76402d2
1 /*
2 * net/dsa/tag_edsa.c - Ethertype DSA tagging
3 * Copyright (c) 2008-2009 Marvell Semiconductor
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 */
11 #include <linux/etherdevice.h>
12 #include <linux/list.h>
13 #include <linux/netdevice.h>
14 #include <linux/slab.h>
15 #include "dsa_priv.h"
17 #define DSA_HLEN 4
18 #define EDSA_HLEN 8
20 netdev_tx_t edsa_xmit(struct sk_buff *skb, struct net_device *dev)
22 struct dsa_slave_priv *p = netdev_priv(dev);
23 u8 *edsa_header;
25 dev->stats.tx_packets++;
26 dev->stats.tx_bytes += skb->len;
29 * Convert the outermost 802.1q tag to a DSA tag and prepend
30 * a DSA ethertype field is the packet is tagged, or insert
31 * a DSA ethertype plus DSA tag between the addresses and the
32 * current ethertype field if the packet is untagged.
34 if (skb->protocol == htons(ETH_P_8021Q)) {
35 if (skb_cow_head(skb, DSA_HLEN) < 0)
36 goto out_free;
37 skb_push(skb, DSA_HLEN);
39 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
42 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
44 edsa_header = skb->data + 2 * ETH_ALEN;
45 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
46 edsa_header[1] = ETH_P_EDSA & 0xff;
47 edsa_header[2] = 0x00;
48 edsa_header[3] = 0x00;
49 edsa_header[4] = 0x60 | p->parent->index;
50 edsa_header[5] = p->port << 3;
53 * Move CFI field from byte 6 to byte 5.
55 if (edsa_header[6] & 0x10) {
56 edsa_header[5] |= 0x01;
57 edsa_header[6] &= ~0x10;
59 } else {
60 if (skb_cow_head(skb, EDSA_HLEN) < 0)
61 goto out_free;
62 skb_push(skb, EDSA_HLEN);
64 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
67 * Construct untagged FROM_CPU DSA tag.
69 edsa_header = skb->data + 2 * ETH_ALEN;
70 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
71 edsa_header[1] = ETH_P_EDSA & 0xff;
72 edsa_header[2] = 0x00;
73 edsa_header[3] = 0x00;
74 edsa_header[4] = 0x40 | p->parent->index;
75 edsa_header[5] = p->port << 3;
76 edsa_header[6] = 0x00;
77 edsa_header[7] = 0x00;
80 skb->protocol = htons(ETH_P_EDSA);
82 skb->dev = p->parent->dst->master_netdev;
83 dev_queue_xmit(skb);
85 return NETDEV_TX_OK;
87 out_free:
88 kfree_skb(skb);
89 return NETDEV_TX_OK;
92 static int edsa_rcv(struct sk_buff *skb, struct net_device *dev,
93 struct packet_type *pt, struct net_device *orig_dev)
95 struct dsa_switch_tree *dst = dev->dsa_ptr;
96 struct dsa_switch *ds;
97 u8 *edsa_header;
98 int source_device;
99 int source_port;
101 if (unlikely(dst == NULL))
102 goto out_drop;
104 skb = skb_unshare(skb, GFP_ATOMIC);
105 if (skb == NULL)
106 goto out;
108 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
109 goto out_drop;
112 * Skip the two null bytes after the ethertype.
114 edsa_header = skb->data + 2;
117 * Check that frame type is either TO_CPU or FORWARD.
119 if ((edsa_header[0] & 0xc0) != 0x00 && (edsa_header[0] & 0xc0) != 0xc0)
120 goto out_drop;
123 * Determine source device and port.
125 source_device = edsa_header[0] & 0x1f;
126 source_port = (edsa_header[1] >> 3) & 0x1f;
129 * Check that the source device exists and that the source
130 * port is a registered DSA port.
132 if (source_device >= dst->pd->nr_chips)
133 goto out_drop;
134 ds = dst->ds[source_device];
135 if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
136 goto out_drop;
139 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
140 * tag and delete the ethertype part. If the 'tagged' bit is
141 * clear, delete the ethertype and the DSA tag parts.
143 if (edsa_header[0] & 0x20) {
144 u8 new_header[4];
147 * Insert 802.1q ethertype and copy the VLAN-related
148 * fields, but clear the bit that will hold CFI (since
149 * DSA uses that bit location for another purpose).
151 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
152 new_header[1] = ETH_P_8021Q & 0xff;
153 new_header[2] = edsa_header[2] & ~0x10;
154 new_header[3] = edsa_header[3];
157 * Move CFI bit from its place in the DSA header to
158 * its 802.1q-designated place.
160 if (edsa_header[1] & 0x01)
161 new_header[2] |= 0x10;
163 skb_pull_rcsum(skb, DSA_HLEN);
166 * Update packet checksum if skb is CHECKSUM_COMPLETE.
168 if (skb->ip_summed == CHECKSUM_COMPLETE) {
169 __wsum c = skb->csum;
170 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
171 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
172 skb->csum = c;
175 memcpy(edsa_header, new_header, DSA_HLEN);
177 memmove(skb->data - ETH_HLEN,
178 skb->data - ETH_HLEN - DSA_HLEN,
179 2 * ETH_ALEN);
180 } else {
182 * Remove DSA tag and update checksum.
184 skb_pull_rcsum(skb, EDSA_HLEN);
185 memmove(skb->data - ETH_HLEN,
186 skb->data - ETH_HLEN - EDSA_HLEN,
187 2 * ETH_ALEN);
190 skb->dev = ds->ports[source_port];
191 skb_push(skb, ETH_HLEN);
192 skb->pkt_type = PACKET_HOST;
193 skb->protocol = eth_type_trans(skb, skb->dev);
195 skb->dev->stats.rx_packets++;
196 skb->dev->stats.rx_bytes += skb->len;
198 netif_receive_skb(skb);
200 return 0;
202 out_drop:
203 kfree_skb(skb);
204 out:
205 return 0;
208 struct packet_type edsa_packet_type __read_mostly = {
209 .type = cpu_to_be16(ETH_P_EDSA),
210 .func = edsa_rcv,