fix CONFIG_HIGHMEM compile error in drivers/gpu/drm/i915/i915_gem.c
[linux-2.6/linux-2.6-openrd.git] / net / dsa / tag_edsa.c
blobf985ea993843bcf253c3c1e409cde1f873b6a3f6
1 /*
2 * net/dsa/tag_edsa.c - Ethertype DSA tagging
3 * Copyright (c) 2008 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 "dsa_priv.h"
16 #define DSA_HLEN 4
17 #define EDSA_HLEN 8
19 int edsa_xmit(struct sk_buff *skb, struct net_device *dev)
21 struct dsa_slave_priv *p = netdev_priv(dev);
22 u8 *edsa_header;
24 dev->stats.tx_packets++;
25 dev->stats.tx_bytes += skb->len;
28 * Convert the outermost 802.1q tag to a DSA tag and prepend
29 * a DSA ethertype field is the packet is tagged, or insert
30 * a DSA ethertype plus DSA tag between the addresses and the
31 * current ethertype field if the packet is untagged.
33 if (skb->protocol == htons(ETH_P_8021Q)) {
34 if (skb_cow_head(skb, DSA_HLEN) < 0)
35 goto out_free;
36 skb_push(skb, DSA_HLEN);
38 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
41 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
43 edsa_header = skb->data + 2 * ETH_ALEN;
44 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
45 edsa_header[1] = ETH_P_EDSA & 0xff;
46 edsa_header[2] = 0x00;
47 edsa_header[3] = 0x00;
48 edsa_header[4] = 0x60;
49 edsa_header[5] = p->port << 3;
52 * Move CFI field from byte 6 to byte 5.
54 if (edsa_header[6] & 0x10) {
55 edsa_header[5] |= 0x01;
56 edsa_header[6] &= ~0x10;
58 } else {
59 if (skb_cow_head(skb, EDSA_HLEN) < 0)
60 goto out_free;
61 skb_push(skb, EDSA_HLEN);
63 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
66 * Construct untagged FROM_CPU DSA tag.
68 edsa_header = skb->data + 2 * ETH_ALEN;
69 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
70 edsa_header[1] = ETH_P_EDSA & 0xff;
71 edsa_header[2] = 0x00;
72 edsa_header[3] = 0x00;
73 edsa_header[4] = 0x40;
74 edsa_header[5] = p->port << 3;
75 edsa_header[6] = 0x00;
76 edsa_header[7] = 0x00;
79 skb->protocol = htons(ETH_P_EDSA);
81 skb->dev = p->parent->master_netdev;
82 dev_queue_xmit(skb);
84 return NETDEV_TX_OK;
86 out_free:
87 kfree_skb(skb);
88 return NETDEV_TX_OK;
91 static int edsa_rcv(struct sk_buff *skb, struct net_device *dev,
92 struct packet_type *pt, struct net_device *orig_dev)
94 struct dsa_switch *ds = dev->dsa_ptr;
95 u8 *edsa_header;
96 int source_port;
98 if (unlikely(ds == NULL))
99 goto out_drop;
101 skb = skb_unshare(skb, GFP_ATOMIC);
102 if (skb == NULL)
103 goto out;
105 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
106 goto out_drop;
109 * Skip the two null bytes after the ethertype.
111 edsa_header = skb->data + 2;
114 * Check that frame type is either TO_CPU or FORWARD, and
115 * that the source device is zero.
117 if ((edsa_header[0] & 0xdf) != 0x00 && (edsa_header[0] & 0xdf) != 0xc0)
118 goto out_drop;
121 * Check that the source port is a registered DSA port.
123 source_port = (edsa_header[1] >> 3) & 0x1f;
124 if (source_port >= DSA_MAX_PORTS || ds->ports[source_port] == NULL)
125 goto out_drop;
128 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
129 * tag and delete the ethertype part. If the 'tagged' bit is
130 * clear, delete the ethertype and the DSA tag parts.
132 if (edsa_header[0] & 0x20) {
133 u8 new_header[4];
136 * Insert 802.1q ethertype and copy the VLAN-related
137 * fields, but clear the bit that will hold CFI (since
138 * DSA uses that bit location for another purpose).
140 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
141 new_header[1] = ETH_P_8021Q & 0xff;
142 new_header[2] = edsa_header[2] & ~0x10;
143 new_header[3] = edsa_header[3];
146 * Move CFI bit from its place in the DSA header to
147 * its 802.1q-designated place.
149 if (edsa_header[1] & 0x01)
150 new_header[2] |= 0x10;
152 skb_pull_rcsum(skb, DSA_HLEN);
155 * Update packet checksum if skb is CHECKSUM_COMPLETE.
157 if (skb->ip_summed == CHECKSUM_COMPLETE) {
158 __wsum c = skb->csum;
159 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
160 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
161 skb->csum = c;
164 memcpy(edsa_header, new_header, DSA_HLEN);
166 memmove(skb->data - ETH_HLEN,
167 skb->data - ETH_HLEN - DSA_HLEN,
168 2 * ETH_ALEN);
169 } else {
171 * Remove DSA tag and update checksum.
173 skb_pull_rcsum(skb, EDSA_HLEN);
174 memmove(skb->data - ETH_HLEN,
175 skb->data - ETH_HLEN - EDSA_HLEN,
176 2 * ETH_ALEN);
179 skb->dev = ds->ports[source_port];
180 skb_push(skb, ETH_HLEN);
181 skb->protocol = eth_type_trans(skb, skb->dev);
183 skb->dev->last_rx = jiffies;
184 skb->dev->stats.rx_packets++;
185 skb->dev->stats.rx_bytes += skb->len;
187 netif_receive_skb(skb);
189 return 0;
191 out_drop:
192 kfree_skb(skb);
193 out:
194 return 0;
197 static struct packet_type edsa_packet_type = {
198 .type = __constant_htons(ETH_P_EDSA),
199 .func = edsa_rcv,
202 static int __init edsa_init_module(void)
204 dev_add_pack(&edsa_packet_type);
205 return 0;
207 module_init(edsa_init_module);
209 static void __exit edsa_cleanup_module(void)
211 dev_remove_pack(&edsa_packet_type);
213 module_exit(edsa_cleanup_module);