]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/dsa/tag_ksz.c
Merge tag 'for-linus-20170825' of git://git.infradead.org/linux-mtd
[mirror_ubuntu-artful-kernel.git] / net / dsa / tag_ksz.c
1 /*
2 * net/dsa/tag_ksz.c - Microchip KSZ Switch tag format handling
3 * Copyright (c) 2017 Microchip Technology
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>
13 #include <linux/slab.h>
14 #include <net/dsa.h>
15 #include "dsa_priv.h"
16
17 /* For Ingress (Host -> KSZ), 2 bytes are added before FCS.
18 * ---------------------------------------------------------------------------
19 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|tag1(1byte)|FCS(4bytes)
20 * ---------------------------------------------------------------------------
21 * tag0 : Prioritization (not used now)
22 * tag1 : each bit represents port (eg, 0x01=port1, 0x02=port2, 0x10=port5)
23 *
24 * For Egress (KSZ -> Host), 1 byte is added before FCS.
25 * ---------------------------------------------------------------------------
26 * DA(6bytes)|SA(6bytes)|....|Data(nbytes)|tag0(1byte)|FCS(4bytes)
27 * ---------------------------------------------------------------------------
28 * tag0 : zero-based value represents port
29 * (eg, 0x00=port1, 0x02=port3, 0x06=port7)
30 */
31
32 #define KSZ_INGRESS_TAG_LEN 2
33 #define KSZ_EGRESS_TAG_LEN 1
34
35 static struct sk_buff *ksz_xmit(struct sk_buff *skb, struct net_device *dev)
36 {
37 struct dsa_slave_priv *p = netdev_priv(dev);
38 struct sk_buff *nskb;
39 int padlen;
40 u8 *tag;
41
42 padlen = (skb->len >= ETH_ZLEN) ? 0 : ETH_ZLEN - skb->len;
43
44 if (skb_tailroom(skb) >= padlen + KSZ_INGRESS_TAG_LEN) {
45 if (skb_put_padto(skb, skb->len + padlen))
46 return NULL;
47
48 nskb = skb;
49 } else {
50 nskb = alloc_skb(NET_IP_ALIGN + skb->len +
51 padlen + KSZ_INGRESS_TAG_LEN, GFP_ATOMIC);
52 if (!nskb)
53 return NULL;
54 skb_reserve(nskb, NET_IP_ALIGN);
55
56 skb_reset_mac_header(nskb);
57 skb_set_network_header(nskb,
58 skb_network_header(skb) - skb->head);
59 skb_set_transport_header(nskb,
60 skb_transport_header(skb) - skb->head);
61 skb_copy_and_csum_dev(skb, skb_put(nskb, skb->len));
62
63 if (skb_put_padto(nskb, nskb->len + padlen)) {
64 kfree_skb(nskb);
65 return NULL;
66 }
67
68 kfree_skb(skb);
69 }
70
71 tag = skb_put(nskb, KSZ_INGRESS_TAG_LEN);
72 tag[0] = 0;
73 tag[1] = 1 << p->dp->index; /* destination port */
74
75 return nskb;
76 }
77
78 static struct sk_buff *ksz_rcv(struct sk_buff *skb, struct net_device *dev,
79 struct packet_type *pt,
80 struct net_device *orig_dev)
81 {
82 struct dsa_switch_tree *dst = dev->dsa_ptr;
83 struct dsa_port *cpu_dp = dsa_get_cpu_port(dst);
84 struct dsa_switch *ds = cpu_dp->ds;
85 u8 *tag;
86 int source_port;
87
88 tag = skb_tail_pointer(skb) - KSZ_EGRESS_TAG_LEN;
89
90 source_port = tag[0] & 7;
91 if (source_port >= ds->num_ports || !ds->ports[source_port].netdev)
92 return NULL;
93
94 pskb_trim_rcsum(skb, skb->len - KSZ_EGRESS_TAG_LEN);
95
96 skb->dev = ds->ports[source_port].netdev;
97
98 return skb;
99 }
100
101 const struct dsa_device_ops ksz_netdev_ops = {
102 .xmit = ksz_xmit,
103 .rcv = ksz_rcv,
104 };