0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/if_vlan.h>
0010 #include <linux/dsa/8021q.h>
0011
0012 #include "dsa_priv.h"
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 #define DSA_8021Q_RSV_VAL 3
0038 #define DSA_8021Q_RSV_SHIFT 10
0039 #define DSA_8021Q_RSV_MASK GENMASK(11, 10)
0040 #define DSA_8021Q_RSV ((DSA_8021Q_RSV_VAL << DSA_8021Q_RSV_SHIFT) & \
0041 DSA_8021Q_RSV_MASK)
0042
0043 #define DSA_8021Q_SWITCH_ID_SHIFT 6
0044 #define DSA_8021Q_SWITCH_ID_MASK GENMASK(8, 6)
0045 #define DSA_8021Q_SWITCH_ID(x) (((x) << DSA_8021Q_SWITCH_ID_SHIFT) & \
0046 DSA_8021Q_SWITCH_ID_MASK)
0047
0048 #define DSA_8021Q_VBID_HI_SHIFT 9
0049 #define DSA_8021Q_VBID_HI_MASK GENMASK(9, 9)
0050 #define DSA_8021Q_VBID_LO_SHIFT 4
0051 #define DSA_8021Q_VBID_LO_MASK GENMASK(5, 4)
0052 #define DSA_8021Q_VBID_HI(x) (((x) & GENMASK(2, 2)) >> 2)
0053 #define DSA_8021Q_VBID_LO(x) ((x) & GENMASK(1, 0))
0054 #define DSA_8021Q_VBID(x) \
0055 (((DSA_8021Q_VBID_LO(x) << DSA_8021Q_VBID_LO_SHIFT) & \
0056 DSA_8021Q_VBID_LO_MASK) | \
0057 ((DSA_8021Q_VBID_HI(x) << DSA_8021Q_VBID_HI_SHIFT) & \
0058 DSA_8021Q_VBID_HI_MASK))
0059
0060 #define DSA_8021Q_PORT_SHIFT 0
0061 #define DSA_8021Q_PORT_MASK GENMASK(3, 0)
0062 #define DSA_8021Q_PORT(x) (((x) << DSA_8021Q_PORT_SHIFT) & \
0063 DSA_8021Q_PORT_MASK)
0064
0065 u16 dsa_tag_8021q_bridge_vid(unsigned int bridge_num)
0066 {
0067
0068
0069
0070 return DSA_8021Q_RSV | DSA_8021Q_VBID(bridge_num);
0071 }
0072 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_vid);
0073
0074
0075
0076
0077 u16 dsa_tag_8021q_standalone_vid(const struct dsa_port *dp)
0078 {
0079 return DSA_8021Q_RSV | DSA_8021Q_SWITCH_ID(dp->ds->index) |
0080 DSA_8021Q_PORT(dp->index);
0081 }
0082 EXPORT_SYMBOL_GPL(dsa_tag_8021q_standalone_vid);
0083
0084
0085 int dsa_8021q_rx_switch_id(u16 vid)
0086 {
0087 return (vid & DSA_8021Q_SWITCH_ID_MASK) >> DSA_8021Q_SWITCH_ID_SHIFT;
0088 }
0089 EXPORT_SYMBOL_GPL(dsa_8021q_rx_switch_id);
0090
0091
0092 int dsa_8021q_rx_source_port(u16 vid)
0093 {
0094 return (vid & DSA_8021Q_PORT_MASK) >> DSA_8021Q_PORT_SHIFT;
0095 }
0096 EXPORT_SYMBOL_GPL(dsa_8021q_rx_source_port);
0097
0098
0099 static int dsa_tag_8021q_rx_vbid(u16 vid)
0100 {
0101 u16 vbid_hi = (vid & DSA_8021Q_VBID_HI_MASK) >> DSA_8021Q_VBID_HI_SHIFT;
0102 u16 vbid_lo = (vid & DSA_8021Q_VBID_LO_MASK) >> DSA_8021Q_VBID_LO_SHIFT;
0103
0104 return (vbid_hi << 2) | vbid_lo;
0105 }
0106
0107 bool vid_is_dsa_8021q(u16 vid)
0108 {
0109 u16 rsv = (vid & DSA_8021Q_RSV_MASK) >> DSA_8021Q_RSV_SHIFT;
0110
0111 return rsv == DSA_8021Q_RSV_VAL;
0112 }
0113 EXPORT_SYMBOL_GPL(vid_is_dsa_8021q);
0114
0115 static struct dsa_tag_8021q_vlan *
0116 dsa_tag_8021q_vlan_find(struct dsa_8021q_context *ctx, int port, u16 vid)
0117 {
0118 struct dsa_tag_8021q_vlan *v;
0119
0120 list_for_each_entry(v, &ctx->vlans, list)
0121 if (v->vid == vid && v->port == port)
0122 return v;
0123
0124 return NULL;
0125 }
0126
0127 static int dsa_port_do_tag_8021q_vlan_add(struct dsa_port *dp, u16 vid,
0128 u16 flags)
0129 {
0130 struct dsa_8021q_context *ctx = dp->ds->tag_8021q_ctx;
0131 struct dsa_switch *ds = dp->ds;
0132 struct dsa_tag_8021q_vlan *v;
0133 int port = dp->index;
0134 int err;
0135
0136
0137 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
0138 return ds->ops->tag_8021q_vlan_add(ds, port, vid, flags);
0139
0140 v = dsa_tag_8021q_vlan_find(ctx, port, vid);
0141 if (v) {
0142 refcount_inc(&v->refcount);
0143 return 0;
0144 }
0145
0146 v = kzalloc(sizeof(*v), GFP_KERNEL);
0147 if (!v)
0148 return -ENOMEM;
0149
0150 err = ds->ops->tag_8021q_vlan_add(ds, port, vid, flags);
0151 if (err) {
0152 kfree(v);
0153 return err;
0154 }
0155
0156 v->vid = vid;
0157 v->port = port;
0158 refcount_set(&v->refcount, 1);
0159 list_add_tail(&v->list, &ctx->vlans);
0160
0161 return 0;
0162 }
0163
0164 static int dsa_port_do_tag_8021q_vlan_del(struct dsa_port *dp, u16 vid)
0165 {
0166 struct dsa_8021q_context *ctx = dp->ds->tag_8021q_ctx;
0167 struct dsa_switch *ds = dp->ds;
0168 struct dsa_tag_8021q_vlan *v;
0169 int port = dp->index;
0170 int err;
0171
0172
0173 if (!(dsa_port_is_cpu(dp) || dsa_port_is_dsa(dp)))
0174 return ds->ops->tag_8021q_vlan_del(ds, port, vid);
0175
0176 v = dsa_tag_8021q_vlan_find(ctx, port, vid);
0177 if (!v)
0178 return -ENOENT;
0179
0180 if (!refcount_dec_and_test(&v->refcount))
0181 return 0;
0182
0183 err = ds->ops->tag_8021q_vlan_del(ds, port, vid);
0184 if (err) {
0185 refcount_inc(&v->refcount);
0186 return err;
0187 }
0188
0189 list_del(&v->list);
0190 kfree(v);
0191
0192 return 0;
0193 }
0194
0195 static bool
0196 dsa_port_tag_8021q_vlan_match(struct dsa_port *dp,
0197 struct dsa_notifier_tag_8021q_vlan_info *info)
0198 {
0199 return dsa_port_is_dsa(dp) || dsa_port_is_cpu(dp) || dp == info->dp;
0200 }
0201
0202 int dsa_switch_tag_8021q_vlan_add(struct dsa_switch *ds,
0203 struct dsa_notifier_tag_8021q_vlan_info *info)
0204 {
0205 struct dsa_port *dp;
0206 int err;
0207
0208
0209
0210
0211
0212
0213 if (!ds->ops->tag_8021q_vlan_add || !ds->tag_8021q_ctx)
0214 return 0;
0215
0216 dsa_switch_for_each_port(dp, ds) {
0217 if (dsa_port_tag_8021q_vlan_match(dp, info)) {
0218 u16 flags = 0;
0219
0220 if (dsa_port_is_user(dp))
0221 flags |= BRIDGE_VLAN_INFO_UNTAGGED |
0222 BRIDGE_VLAN_INFO_PVID;
0223
0224 err = dsa_port_do_tag_8021q_vlan_add(dp, info->vid,
0225 flags);
0226 if (err)
0227 return err;
0228 }
0229 }
0230
0231 return 0;
0232 }
0233
0234 int dsa_switch_tag_8021q_vlan_del(struct dsa_switch *ds,
0235 struct dsa_notifier_tag_8021q_vlan_info *info)
0236 {
0237 struct dsa_port *dp;
0238 int err;
0239
0240 if (!ds->ops->tag_8021q_vlan_del || !ds->tag_8021q_ctx)
0241 return 0;
0242
0243 dsa_switch_for_each_port(dp, ds) {
0244 if (dsa_port_tag_8021q_vlan_match(dp, info)) {
0245 err = dsa_port_do_tag_8021q_vlan_del(dp, info->vid);
0246 if (err)
0247 return err;
0248 }
0249 }
0250
0251 return 0;
0252 }
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273 int dsa_tag_8021q_bridge_join(struct dsa_switch *ds, int port,
0274 struct dsa_bridge bridge)
0275 {
0276 struct dsa_port *dp = dsa_to_port(ds, port);
0277 u16 standalone_vid, bridge_vid;
0278 int err;
0279
0280
0281
0282
0283 standalone_vid = dsa_tag_8021q_standalone_vid(dp);
0284 bridge_vid = dsa_tag_8021q_bridge_vid(bridge.num);
0285
0286 err = dsa_port_tag_8021q_vlan_add(dp, bridge_vid, true);
0287 if (err)
0288 return err;
0289
0290 dsa_port_tag_8021q_vlan_del(dp, standalone_vid, false);
0291
0292 return 0;
0293 }
0294 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_join);
0295
0296 void dsa_tag_8021q_bridge_leave(struct dsa_switch *ds, int port,
0297 struct dsa_bridge bridge)
0298 {
0299 struct dsa_port *dp = dsa_to_port(ds, port);
0300 u16 standalone_vid, bridge_vid;
0301 int err;
0302
0303
0304
0305
0306 standalone_vid = dsa_tag_8021q_standalone_vid(dp);
0307 bridge_vid = dsa_tag_8021q_bridge_vid(bridge.num);
0308
0309 err = dsa_port_tag_8021q_vlan_add(dp, standalone_vid, false);
0310 if (err) {
0311 dev_err(ds->dev,
0312 "Failed to delete tag_8021q standalone VLAN %d from port %d: %pe\n",
0313 standalone_vid, port, ERR_PTR(err));
0314 }
0315
0316 dsa_port_tag_8021q_vlan_del(dp, bridge_vid, true);
0317 }
0318 EXPORT_SYMBOL_GPL(dsa_tag_8021q_bridge_leave);
0319
0320
0321 static int dsa_tag_8021q_port_setup(struct dsa_switch *ds, int port)
0322 {
0323 struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
0324 struct dsa_port *dp = dsa_to_port(ds, port);
0325 u16 vid = dsa_tag_8021q_standalone_vid(dp);
0326 struct net_device *master;
0327 int err;
0328
0329
0330
0331
0332 if (!dsa_port_is_user(dp))
0333 return 0;
0334
0335 master = dp->cpu_dp->master;
0336
0337 err = dsa_port_tag_8021q_vlan_add(dp, vid, false);
0338 if (err) {
0339 dev_err(ds->dev,
0340 "Failed to apply standalone VID %d to port %d: %pe\n",
0341 vid, port, ERR_PTR(err));
0342 return err;
0343 }
0344
0345
0346 vlan_vid_add(master, ctx->proto, vid);
0347
0348 return err;
0349 }
0350
0351 static void dsa_tag_8021q_port_teardown(struct dsa_switch *ds, int port)
0352 {
0353 struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
0354 struct dsa_port *dp = dsa_to_port(ds, port);
0355 u16 vid = dsa_tag_8021q_standalone_vid(dp);
0356 struct net_device *master;
0357
0358
0359
0360
0361 if (!dsa_port_is_user(dp))
0362 return;
0363
0364 master = dp->cpu_dp->master;
0365
0366 dsa_port_tag_8021q_vlan_del(dp, vid, false);
0367
0368 vlan_vid_del(master, ctx->proto, vid);
0369 }
0370
0371 static int dsa_tag_8021q_setup(struct dsa_switch *ds)
0372 {
0373 int err, port;
0374
0375 ASSERT_RTNL();
0376
0377 for (port = 0; port < ds->num_ports; port++) {
0378 err = dsa_tag_8021q_port_setup(ds, port);
0379 if (err < 0) {
0380 dev_err(ds->dev,
0381 "Failed to setup VLAN tagging for port %d: %pe\n",
0382 port, ERR_PTR(err));
0383 return err;
0384 }
0385 }
0386
0387 return 0;
0388 }
0389
0390 static void dsa_tag_8021q_teardown(struct dsa_switch *ds)
0391 {
0392 int port;
0393
0394 ASSERT_RTNL();
0395
0396 for (port = 0; port < ds->num_ports; port++)
0397 dsa_tag_8021q_port_teardown(ds, port);
0398 }
0399
0400 int dsa_tag_8021q_register(struct dsa_switch *ds, __be16 proto)
0401 {
0402 struct dsa_8021q_context *ctx;
0403
0404 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
0405 if (!ctx)
0406 return -ENOMEM;
0407
0408 ctx->proto = proto;
0409 ctx->ds = ds;
0410
0411 INIT_LIST_HEAD(&ctx->vlans);
0412
0413 ds->tag_8021q_ctx = ctx;
0414
0415 return dsa_tag_8021q_setup(ds);
0416 }
0417 EXPORT_SYMBOL_GPL(dsa_tag_8021q_register);
0418
0419 void dsa_tag_8021q_unregister(struct dsa_switch *ds)
0420 {
0421 struct dsa_8021q_context *ctx = ds->tag_8021q_ctx;
0422 struct dsa_tag_8021q_vlan *v, *n;
0423
0424 dsa_tag_8021q_teardown(ds);
0425
0426 list_for_each_entry_safe(v, n, &ctx->vlans, list) {
0427 list_del(&v->list);
0428 kfree(v);
0429 }
0430
0431 ds->tag_8021q_ctx = NULL;
0432
0433 kfree(ctx);
0434 }
0435 EXPORT_SYMBOL_GPL(dsa_tag_8021q_unregister);
0436
0437 struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev,
0438 u16 tpid, u16 tci)
0439 {
0440
0441
0442
0443 return vlan_insert_tag(skb, htons(tpid), tci);
0444 }
0445 EXPORT_SYMBOL_GPL(dsa_8021q_xmit);
0446
0447 struct net_device *dsa_tag_8021q_find_port_by_vbid(struct net_device *master,
0448 int vbid)
0449 {
0450 struct dsa_port *cpu_dp = master->dsa_ptr;
0451 struct dsa_switch_tree *dst = cpu_dp->dst;
0452 struct dsa_port *dp;
0453
0454 if (WARN_ON(!vbid))
0455 return NULL;
0456
0457 dsa_tree_for_each_user_port(dp, dst) {
0458 if (!dp->bridge)
0459 continue;
0460
0461 if (dp->stp_state != BR_STATE_LEARNING &&
0462 dp->stp_state != BR_STATE_FORWARDING)
0463 continue;
0464
0465 if (dp->cpu_dp != cpu_dp)
0466 continue;
0467
0468 if (dsa_port_bridge_num_get(dp) == vbid)
0469 return dp->slave;
0470 }
0471
0472 return NULL;
0473 }
0474 EXPORT_SYMBOL_GPL(dsa_tag_8021q_find_port_by_vbid);
0475
0476 void dsa_8021q_rcv(struct sk_buff *skb, int *source_port, int *switch_id,
0477 int *vbid)
0478 {
0479 u16 vid, tci;
0480
0481 if (skb_vlan_tag_present(skb)) {
0482 tci = skb_vlan_tag_get(skb);
0483 __vlan_hwaccel_clear_tag(skb);
0484 } else {
0485 skb_push_rcsum(skb, ETH_HLEN);
0486 __skb_vlan_pop(skb, &tci);
0487 skb_pull_rcsum(skb, ETH_HLEN);
0488 }
0489
0490 vid = tci & VLAN_VID_MASK;
0491
0492 *source_port = dsa_8021q_rx_source_port(vid);
0493 *switch_id = dsa_8021q_rx_switch_id(vid);
0494
0495 if (vbid)
0496 *vbid = dsa_tag_8021q_rx_vbid(vid);
0497
0498 skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT;
0499 }
0500 EXPORT_SYMBOL_GPL(dsa_8021q_rcv);