Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Broadcom tag support |
| 3 | * |
| 4 | * Copyright (C) 2014 Broadcom Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/etherdevice.h> |
| 13 | #include <linux/list.h> |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 14 | #include <linux/slab.h> |
Vivien Didelot | ea5dd34 | 2017-05-17 15:46:03 -0400 | [diff] [blame] | 15 | |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 16 | #include "dsa_priv.h" |
| 17 | |
| 18 | /* This tag length is 4 bytes, older ones were 6 bytes, we do not |
| 19 | * handle them |
| 20 | */ |
| 21 | #define BRCM_TAG_LEN 4 |
| 22 | |
| 23 | /* Tag is constructed and desconstructed using byte by byte access |
| 24 | * because the tag is placed after the MAC Source Address, which does |
| 25 | * not make it 4-bytes aligned, so this might cause unaligned accesses |
| 26 | * on most systems where this is used. |
| 27 | */ |
| 28 | |
| 29 | /* Ingress and egress opcodes */ |
| 30 | #define BRCM_OPCODE_SHIFT 5 |
| 31 | #define BRCM_OPCODE_MASK 0x7 |
| 32 | |
| 33 | /* Ingress fields */ |
| 34 | /* 1st byte in the tag */ |
| 35 | #define BRCM_IG_TC_SHIFT 2 |
| 36 | #define BRCM_IG_TC_MASK 0x7 |
| 37 | /* 2nd byte in the tag */ |
| 38 | #define BRCM_IG_TE_MASK 0x3 |
| 39 | #define BRCM_IG_TS_SHIFT 7 |
| 40 | /* 3rd byte in the tag */ |
| 41 | #define BRCM_IG_DSTMAP2_MASK 1 |
| 42 | #define BRCM_IG_DSTMAP1_MASK 0xff |
| 43 | |
| 44 | /* Egress fields */ |
| 45 | |
| 46 | /* 2nd byte in the tag */ |
| 47 | #define BRCM_EG_CID_MASK 0xff |
| 48 | |
| 49 | /* 3rd byte in the tag */ |
| 50 | #define BRCM_EG_RC_MASK 0xff |
| 51 | #define BRCM_EG_RC_RSVD (3 << 6) |
| 52 | #define BRCM_EG_RC_EXCEPTION (1 << 5) |
| 53 | #define BRCM_EG_RC_PROT_SNOOP (1 << 4) |
| 54 | #define BRCM_EG_RC_PROT_TERM (1 << 3) |
| 55 | #define BRCM_EG_RC_SWITCH (1 << 2) |
| 56 | #define BRCM_EG_RC_MAC_LEARN (1 << 1) |
| 57 | #define BRCM_EG_RC_MIRROR (1 << 0) |
| 58 | #define BRCM_EG_TC_SHIFT 5 |
| 59 | #define BRCM_EG_TC_MASK 0x7 |
| 60 | #define BRCM_EG_PID_MASK 0x1f |
| 61 | |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 62 | static struct sk_buff *brcm_tag_xmit_ll(struct sk_buff *skb, |
| 63 | struct net_device *dev, |
| 64 | unsigned int offset) |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 65 | { |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 66 | struct dsa_port *dp = dsa_slave_to_port(dev); |
Florian Fainelli | 0f15b09 | 2017-09-03 20:27:01 -0700 | [diff] [blame] | 67 | u16 queue = skb_get_queue_mapping(skb); |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 68 | u8 *brcm_tag; |
| 69 | |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 70 | if (skb_cow_head(skb, BRCM_TAG_LEN) < 0) |
Vivien Didelot | fe47d56 | 2017-06-01 16:07:15 -0400 | [diff] [blame] | 71 | return NULL; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 72 | |
Florian Fainelli | bf08c34 | 2018-01-03 22:13:00 -0800 | [diff] [blame] | 73 | /* The Ethernet switch we are interfaced with needs packets to be at |
| 74 | * least 64 bytes (including FCS) otherwise they will be discarded when |
| 75 | * they enter the switch port logic. When Broadcom tags are enabled, we |
| 76 | * need to make sure that packets are at least 68 bytes |
| 77 | * (including FCS and tag) because the length verification is done after |
| 78 | * the Broadcom tag is stripped off the ingress packet. |
| 79 | * |
| 80 | * Let dsa_slave_xmit() free the SKB |
| 81 | */ |
| 82 | if (__skb_put_padto(skb, ETH_ZLEN + BRCM_TAG_LEN, false)) |
| 83 | return NULL; |
| 84 | |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 85 | skb_push(skb, BRCM_TAG_LEN); |
| 86 | |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 87 | if (offset) |
| 88 | memmove(skb->data, skb->data + BRCM_TAG_LEN, offset); |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 89 | |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 90 | brcm_tag = skb->data + offset; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 91 | |
| 92 | /* Set the ingress opcode, traffic class, tag enforcment is |
| 93 | * deprecated |
| 94 | */ |
| 95 | brcm_tag[0] = (1 << BRCM_OPCODE_SHIFT) | |
Florian Fainelli | 0f15b09 | 2017-09-03 20:27:01 -0700 | [diff] [blame] | 96 | ((queue & BRCM_IG_TC_MASK) << BRCM_IG_TC_SHIFT); |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 97 | brcm_tag[1] = 0; |
| 98 | brcm_tag[2] = 0; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 99 | if (dp->index == 8) |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 100 | brcm_tag[2] = BRCM_IG_DSTMAP2_MASK; |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 101 | brcm_tag[3] = (1 << dp->index) & BRCM_IG_DSTMAP1_MASK; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 102 | |
Florian Fainelli | 0a5f14c | 2017-10-11 10:57:49 -0700 | [diff] [blame] | 103 | /* Now tell the master network device about the desired output queue |
| 104 | * as well |
| 105 | */ |
Vivien Didelot | d945097 | 2017-10-16 11:12:15 -0400 | [diff] [blame] | 106 | skb_set_queue_mapping(skb, BRCM_TAG_SET_PORT_QUEUE(dp->index, queue)); |
Florian Fainelli | 0a5f14c | 2017-10-11 10:57:49 -0700 | [diff] [blame] | 107 | |
Florian Fainelli | 4ed70ce | 2015-07-31 11:42:56 -0700 | [diff] [blame] | 108 | return skb; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 111 | static struct sk_buff *brcm_tag_rcv_ll(struct sk_buff *skb, |
| 112 | struct net_device *dev, |
| 113 | struct packet_type *pt, |
| 114 | unsigned int offset) |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 115 | { |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 116 | int source_port; |
| 117 | u8 *brcm_tag; |
| 118 | |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 119 | if (unlikely(!pskb_may_pull(skb, BRCM_TAG_LEN))) |
Vivien Didelot | 5470979 | 2017-06-01 16:07:14 -0400 | [diff] [blame] | 120 | return NULL; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 121 | |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 122 | brcm_tag = skb->data - offset; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 123 | |
| 124 | /* The opcode should never be different than 0b000 */ |
| 125 | if (unlikely((brcm_tag[0] >> BRCM_OPCODE_SHIFT) & BRCM_OPCODE_MASK)) |
Vivien Didelot | 5470979 | 2017-06-01 16:07:14 -0400 | [diff] [blame] | 126 | return NULL; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 127 | |
| 128 | /* We should never see a reserved reason code without knowing how to |
| 129 | * handle it |
| 130 | */ |
Florian Fainelli | 82272db | 2017-01-23 19:19:07 -0800 | [diff] [blame] | 131 | if (unlikely(brcm_tag[2] & BRCM_EG_RC_RSVD)) |
Vivien Didelot | 5470979 | 2017-06-01 16:07:14 -0400 | [diff] [blame] | 132 | return NULL; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 133 | |
| 134 | /* Locate which port this is coming from */ |
| 135 | source_port = brcm_tag[3] & BRCM_EG_PID_MASK; |
| 136 | |
Vivien Didelot | 2231c43 | 2017-10-16 11:12:17 -0400 | [diff] [blame] | 137 | skb->dev = dsa_master_find_slave(dev, 0, source_port); |
Vivien Didelot | 3775b1b | 2017-09-29 17:19:15 -0400 | [diff] [blame] | 138 | if (!skb->dev) |
Vivien Didelot | 5470979 | 2017-06-01 16:07:14 -0400 | [diff] [blame] | 139 | return NULL; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 140 | |
| 141 | /* Remove Broadcom tag and update checksum */ |
| 142 | skb_pull_rcsum(skb, BRCM_TAG_LEN); |
| 143 | |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 144 | return skb; |
| 145 | } |
| 146 | |
Florian Fainelli | b74b70c | 2017-11-10 15:22:54 -0800 | [diff] [blame] | 147 | #ifdef CONFIG_NET_DSA_TAG_BRCM |
| 148 | static struct sk_buff *brcm_tag_xmit(struct sk_buff *skb, |
| 149 | struct net_device *dev) |
| 150 | { |
| 151 | /* Build the tag after the MAC Source Address */ |
| 152 | return brcm_tag_xmit_ll(skb, dev, 2 * ETH_ALEN); |
| 153 | } |
| 154 | |
| 155 | |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 156 | static struct sk_buff *brcm_tag_rcv(struct sk_buff *skb, struct net_device *dev, |
| 157 | struct packet_type *pt) |
| 158 | { |
| 159 | struct sk_buff *nskb; |
| 160 | |
| 161 | /* skb->data points to the EtherType, the tag is right before it */ |
| 162 | nskb = brcm_tag_rcv_ll(skb, dev, pt, 2); |
| 163 | if (!nskb) |
| 164 | return nskb; |
| 165 | |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 166 | /* Move the Ethernet DA and SA */ |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 167 | memmove(nskb->data - ETH_HLEN, |
| 168 | nskb->data - ETH_HLEN - BRCM_TAG_LEN, |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 169 | 2 * ETH_ALEN); |
| 170 | |
Florian Fainelli | f7c39e3 | 2017-11-10 15:22:53 -0800 | [diff] [blame] | 171 | return nskb; |
Florian Fainelli | 5037d53 | 2014-08-27 17:04:55 -0700 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | const struct dsa_device_ops brcm_netdev_ops = { |
| 175 | .xmit = brcm_tag_xmit, |
| 176 | .rcv = brcm_tag_rcv, |
| 177 | }; |
Florian Fainelli | b74b70c | 2017-11-10 15:22:54 -0800 | [diff] [blame] | 178 | #endif |
| 179 | |
| 180 | #ifdef CONFIG_NET_DSA_TAG_BRCM_PREPEND |
| 181 | static struct sk_buff *brcm_tag_xmit_prepend(struct sk_buff *skb, |
| 182 | struct net_device *dev) |
| 183 | { |
| 184 | /* tag is prepended to the packet */ |
| 185 | return brcm_tag_xmit_ll(skb, dev, 0); |
| 186 | } |
| 187 | |
| 188 | static struct sk_buff *brcm_tag_rcv_prepend(struct sk_buff *skb, |
| 189 | struct net_device *dev, |
| 190 | struct packet_type *pt) |
| 191 | { |
| 192 | /* tag is prepended to the packet */ |
| 193 | return brcm_tag_rcv_ll(skb, dev, pt, ETH_HLEN); |
| 194 | } |
| 195 | |
| 196 | const struct dsa_device_ops brcm_prepend_netdev_ops = { |
| 197 | .xmit = brcm_tag_xmit_prepend, |
| 198 | .rcv = brcm_tag_rcv_prepend, |
| 199 | }; |
| 200 | #endif |