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.
11 #include <linux/etherdevice.h>
12 #include <linux/list.h>
13 #include <linux/slab.h>
20 #define FRAME_TYPE_TO_CPU 0x00
21 #define FRAME_TYPE_FORWARD 0x03
23 #define TO_CPU_CODE_MGMT_TRAP 0x00
24 #define TO_CPU_CODE_FRAME2REG 0x01
25 #define TO_CPU_CODE_IGMP_MLD_TRAP 0x02
26 #define TO_CPU_CODE_POLICY_TRAP 0x03
27 #define TO_CPU_CODE_ARP_MIRROR 0x04
28 #define TO_CPU_CODE_POLICY_MIRROR 0x05
30 static struct sk_buff *edsa_xmit(struct sk_buff *skb, struct net_device *dev)
32 struct dsa_port *dp = dsa_slave_to_port(dev);
36 * Convert the outermost 802.1q tag to a DSA tag and prepend
37 * a DSA ethertype field is the packet is tagged, or insert
38 * a DSA ethertype plus DSA tag between the addresses and the
39 * current ethertype field if the packet is untagged.
41 if (skb->protocol == htons(ETH_P_8021Q)) {
42 if (skb_cow_head(skb, DSA_HLEN) < 0)
44 skb_push(skb, DSA_HLEN);
46 memmove(skb->data, skb->data + DSA_HLEN, 2 * ETH_ALEN);
49 * Construct tagged FROM_CPU DSA tag from 802.1q tag.
51 edsa_header = skb->data + 2 * ETH_ALEN;
52 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
53 edsa_header[1] = ETH_P_EDSA & 0xff;
54 edsa_header[2] = 0x00;
55 edsa_header[3] = 0x00;
56 edsa_header[4] = 0x60 | dp->ds->index;
57 edsa_header[5] = dp->index << 3;
60 * Move CFI field from byte 6 to byte 5.
62 if (edsa_header[6] & 0x10) {
63 edsa_header[5] |= 0x01;
64 edsa_header[6] &= ~0x10;
67 if (skb_cow_head(skb, EDSA_HLEN) < 0)
69 skb_push(skb, EDSA_HLEN);
71 memmove(skb->data, skb->data + EDSA_HLEN, 2 * ETH_ALEN);
74 * Construct untagged FROM_CPU DSA tag.
76 edsa_header = skb->data + 2 * ETH_ALEN;
77 edsa_header[0] = (ETH_P_EDSA >> 8) & 0xff;
78 edsa_header[1] = ETH_P_EDSA & 0xff;
79 edsa_header[2] = 0x00;
80 edsa_header[3] = 0x00;
81 edsa_header[4] = 0x40 | dp->ds->index;
82 edsa_header[5] = dp->index << 3;
83 edsa_header[6] = 0x00;
84 edsa_header[7] = 0x00;
90 static struct sk_buff *edsa_rcv(struct sk_buff *skb, struct net_device *dev,
91 struct packet_type *pt)
99 if (unlikely(!pskb_may_pull(skb, EDSA_HLEN)))
103 * Skip the two null bytes after the ethertype.
105 edsa_header = skb->data + 2;
108 * Check that frame type is either TO_CPU or FORWARD.
110 frame_type = edsa_header[0] >> 6;
112 switch (frame_type) {
113 case FRAME_TYPE_TO_CPU:
114 code = (edsa_header[1] & 0x6) | ((edsa_header[2] >> 4) & 1);
117 * Mark the frame to never egress on any port of the same switch
118 * unless it's a trapped IGMP/MLD packet, in which case the
119 * bridge might want to forward it.
121 if (code != TO_CPU_CODE_IGMP_MLD_TRAP)
122 skb->offload_fwd_mark = 1;
126 case FRAME_TYPE_FORWARD:
127 skb->offload_fwd_mark = 1;
135 * Determine source device and port.
137 source_device = edsa_header[0] & 0x1f;
138 source_port = (edsa_header[1] >> 3) & 0x1f;
140 skb->dev = dsa_master_find_slave(dev, source_device, source_port);
145 * If the 'tagged' bit is set, convert the DSA tag to a 802.1q
146 * tag and delete the ethertype part. If the 'tagged' bit is
147 * clear, delete the ethertype and the DSA tag parts.
149 if (edsa_header[0] & 0x20) {
153 * Insert 802.1q ethertype and copy the VLAN-related
154 * fields, but clear the bit that will hold CFI (since
155 * DSA uses that bit location for another purpose).
157 new_header[0] = (ETH_P_8021Q >> 8) & 0xff;
158 new_header[1] = ETH_P_8021Q & 0xff;
159 new_header[2] = edsa_header[2] & ~0x10;
160 new_header[3] = edsa_header[3];
163 * Move CFI bit from its place in the DSA header to
164 * its 802.1q-designated place.
166 if (edsa_header[1] & 0x01)
167 new_header[2] |= 0x10;
169 skb_pull_rcsum(skb, DSA_HLEN);
172 * Update packet checksum if skb is CHECKSUM_COMPLETE.
174 if (skb->ip_summed == CHECKSUM_COMPLETE) {
175 __wsum c = skb->csum;
176 c = csum_add(c, csum_partial(new_header + 2, 2, 0));
177 c = csum_sub(c, csum_partial(edsa_header + 2, 2, 0));
181 memcpy(edsa_header, new_header, DSA_HLEN);
183 memmove(skb->data - ETH_HLEN,
184 skb->data - ETH_HLEN - DSA_HLEN,
188 * Remove DSA tag and update checksum.
190 skb_pull_rcsum(skb, EDSA_HLEN);
191 memmove(skb->data - ETH_HLEN,
192 skb->data - ETH_HLEN - EDSA_HLEN,
199 const struct dsa_device_ops edsa_netdev_ops = {