1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
|
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */
#include <linux/if_bridge.h>
#include <linux/if_vlan.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/notifier.h>
#include <net/netevent.h>
#include <net/switchdev.h>
#include "prestera.h"
#include "prestera_hw.h"
#include "prestera_switchdev.h"
#define PRESTERA_VID_ALL (0xffff)
#define PRESTERA_DEFAULT_AGEING_TIME_MS 300000
#define PRESTERA_MAX_AGEING_TIME_MS 1000000000
#define PRESTERA_MIN_AGEING_TIME_MS 32000
struct prestera_fdb_event_work {
struct work_struct work;
struct switchdev_notifier_fdb_info fdb_info;
struct net_device *dev;
unsigned long event;
};
struct prestera_switchdev {
struct prestera_switch *sw;
struct list_head bridge_list;
bool bridge_8021q_exists;
struct notifier_block swdev_nb_blk;
struct notifier_block swdev_nb;
};
struct prestera_bridge {
struct list_head head;
struct net_device *dev;
struct prestera_switchdev *swdev;
struct list_head port_list;
bool vlan_enabled;
u16 bridge_id;
};
struct prestera_bridge_port {
struct list_head head;
struct net_device *dev;
struct prestera_bridge *bridge;
struct list_head vlan_list;
refcount_t ref_count;
unsigned long flags;
u8 stp_state;
};
struct prestera_bridge_vlan {
struct list_head head;
struct list_head port_vlan_list;
u16 vid;
};
struct prestera_port_vlan {
struct list_head br_vlan_head;
struct list_head port_head;
struct prestera_port *port;
struct prestera_bridge_port *br_port;
u16 vid;
};
static struct workqueue_struct *swdev_wq;
static void prestera_bridge_port_put(struct prestera_bridge_port *br_port);
static int prestera_port_vid_stp_set(struct prestera_port *port, u16 vid,
u8 state);
static struct prestera_bridge_vlan *
prestera_bridge_vlan_create(struct prestera_bridge_port *br_port, u16 vid)
{
struct prestera_bridge_vlan *br_vlan;
br_vlan = kzalloc(sizeof(*br_vlan), GFP_KERNEL);
if (!br_vlan)
return NULL;
INIT_LIST_HEAD(&br_vlan->port_vlan_list);
br_vlan->vid = vid;
list_add(&br_vlan->head, &br_port->vlan_list);
return br_vlan;
}
static void prestera_bridge_vlan_destroy(struct prestera_bridge_vlan *br_vlan)
{
list_del(&br_vlan->head);
WARN_ON(!list_empty(&br_vlan->port_vlan_list));
kfree(br_vlan);
}
static struct prestera_bridge_vlan *
prestera_bridge_vlan_by_vid(struct prestera_bridge_port *br_port, u16 vid)
{
struct prestera_bridge_vlan *br_vlan;
list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
if (br_vlan->vid == vid)
return br_vlan;
}
return NULL;
}
static int prestera_bridge_vlan_port_count(struct prestera_bridge *bridge,
u16 vid)
{
struct prestera_bridge_port *br_port;
struct prestera_bridge_vlan *br_vlan;
int count = 0;
list_for_each_entry(br_port, &bridge->port_list, head) {
list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
if (br_vlan->vid == vid) {
count += 1;
break;
}
}
}
return count;
}
static void prestera_bridge_vlan_put(struct prestera_bridge_vlan *br_vlan)
{
if (list_empty(&br_vlan->port_vlan_list))
prestera_bridge_vlan_destroy(br_vlan);
}
static struct prestera_port_vlan *
prestera_port_vlan_by_vid(struct prestera_port *port, u16 vid)
{
struct prestera_port_vlan *port_vlan;
list_for_each_entry(port_vlan, &port->vlans_list, port_head) {
if (port_vlan->vid == vid)
return port_vlan;
}
return NULL;
}
static struct prestera_port_vlan *
prestera_port_vlan_create(struct prestera_port *port, u16 vid, bool untagged)
{
struct prestera_port_vlan *port_vlan;
int err;
port_vlan = prestera_port_vlan_by_vid(port, vid);
if (port_vlan)
return ERR_PTR(-EEXIST);
err = prestera_hw_vlan_port_set(port, vid, true, untagged);
if (err)
return ERR_PTR(err);
port_vlan = kzalloc(sizeof(*port_vlan), GFP_KERNEL);
if (!port_vlan) {
err = -ENOMEM;
goto err_port_vlan_alloc;
}
port_vlan->port = port;
port_vlan->vid = vid;
list_add(&port_vlan->port_head, &port->vlans_list);
return port_vlan;
err_port_vlan_alloc:
prestera_hw_vlan_port_set(port, vid, false, false);
return ERR_PTR(err);
}
static void
prestera_port_vlan_bridge_leave(struct prestera_port_vlan *port_vlan)
{
u32 fdb_flush_mode = PRESTERA_FDB_FLUSH_MODE_DYNAMIC;
struct prestera_port *port = port_vlan->port;
struct prestera_bridge_vlan *br_vlan;
struct prestera_bridge_port *br_port;
bool last_port, last_vlan;
u16 vid = port_vlan->vid;
int port_count;
br_port = port_vlan->br_port;
port_count = prestera_bridge_vlan_port_count(br_port->bridge, vid);
br_vlan = prestera_bridge_vlan_by_vid(br_port, vid);
last_vlan = list_is_singular(&br_port->vlan_list);
last_port = port_count == 1;
if (last_vlan)
prestera_hw_fdb_flush_port(port, fdb_flush_mode);
else if (last_port)
prestera_hw_fdb_flush_vlan(port->sw, vid, fdb_flush_mode);
else
prestera_hw_fdb_flush_port_vlan(port, vid, fdb_flush_mode);
list_del(&port_vlan->br_vlan_head);
prestera_bridge_vlan_put(br_vlan);
prestera_bridge_port_put(br_port);
port_vlan->br_port = NULL;
}
static void prestera_port_vlan_destroy(struct prestera_port_vlan *port_vlan)
{
struct prestera_port *port = port_vlan->port;
u16 vid = port_vlan->vid;
if (port_vlan->br_port)
prestera_port_vlan_bridge_leave(port_vlan);
prestera_hw_vlan_port_set(port, vid, false, false);
list_del(&port_vlan->port_head);
kfree(port_vlan);
}
static struct prestera_bridge *
prestera_bridge_create(struct prestera_switchdev *swdev, struct net_device *dev)
{
bool vlan_enabled = br_vlan_enabled(dev);
struct prestera_bridge *bridge;
u16 bridge_id;
int err;
if (vlan_enabled && swdev->bridge_8021q_exists) {
netdev_err(dev, "Only one VLAN-aware bridge is supported\n");
return ERR_PTR(-EINVAL);
}
bridge = kzalloc(sizeof(*bridge), GFP_KERNEL);
if (!bridge)
return ERR_PTR(-ENOMEM);
if (vlan_enabled) {
swdev->bridge_8021q_exists = true;
} else {
err = prestera_hw_bridge_create(swdev->sw, &bridge_id);
if (err) {
kfree(bridge);
return ERR_PTR(err);
}
bridge->bridge_id = bridge_id;
}
bridge->vlan_enabled = vlan_enabled;
bridge->swdev = swdev;
bridge->dev = dev;
INIT_LIST_HEAD(&bridge->port_list);
list_add(&bridge->head, &swdev->bridge_list);
return bridge;
}
static void prestera_bridge_destroy(struct prestera_bridge *bridge)
{
struct prestera_switchdev *swdev = bridge->swdev;
list_del(&bridge->head);
if (bridge->vlan_enabled)
swdev->bridge_8021q_exists = false;
else
prestera_hw_bridge_delete(swdev->sw, bridge->bridge_id);
WARN_ON(!list_empty(&bridge->port_list));
kfree(bridge);
}
static void prestera_bridge_put(struct prestera_bridge *bridge)
{
if (list_empty(&bridge->port_list))
prestera_bridge_destroy(bridge);
}
static
struct prestera_bridge *prestera_bridge_by_dev(struct prestera_switchdev *swdev,
const struct net_device *dev)
{
struct prestera_bridge *bridge;
list_for_each_entry(bridge, &swdev->bridge_list, head)
if (bridge->dev == dev)
return bridge;
return NULL;
}
static struct prestera_bridge_port *
__prestera_bridge_port_by_dev(struct prestera_bridge *bridge,
struct net_device *dev)
{
struct prestera_bridge_port *br_port;
list_for_each_entry(br_port, &bridge->port_list, head) {
if (br_port->dev == dev)
return br_port;
}
return NULL;
}
static struct prestera_bridge_port *
prestera_bridge_port_by_dev(struct prestera_switchdev *swdev,
struct net_device *dev)
{
struct net_device *br_dev = netdev_master_upper_dev_get(dev);
struct prestera_bridge *bridge;
if (!br_dev)
return NULL;
bridge = prestera_bridge_by_dev(swdev, br_dev);
if (!bridge)
return NULL;
return __prestera_bridge_port_by_dev(bridge, dev);
}
static struct prestera_bridge_port *
prestera_bridge_port_create(struct prestera_bridge *bridge,
struct net_device *dev)
{
struct prestera_bridge_port *br_port;
br_port = kzalloc(sizeof(*br_port), GFP_KERNEL);
if (!br_port)
return NULL;
br_port->flags = BR_LEARNING | BR_FLOOD | BR_LEARNING_SYNC |
BR_MCAST_FLOOD;
br_port->stp_state = BR_STATE_DISABLED;
refcount_set(&br_port->ref_count, 1);
br_port->bridge = bridge;
br_port->dev = dev;
INIT_LIST_HEAD(&br_port->vlan_list);
list_add(&br_port->head, &bridge->port_list);
return br_port;
}
static void
prestera_bridge_port_destroy(struct prestera_bridge_port *br_port)
{
list_del(&br_port->head);
WARN_ON(!list_empty(&br_port->vlan_list));
kfree(br_port);
}
static void prestera_bridge_port_get(struct prestera_bridge_port *br_port)
{
refcount_inc(&br_port->ref_count);
}
static void prestera_bridge_port_put(struct prestera_bridge_port *br_port)
{
struct prestera_bridge *bridge = br_port->bridge;
if (refcount_dec_and_test(&br_port->ref_count)) {
prestera_bridge_port_destroy(br_port);
prestera_bridge_put(bridge);
}
}
static struct prestera_bridge_port *
prestera_bridge_port_add(struct prestera_bridge *bridge, struct net_device *dev)
{
struct prestera_bridge_port *br_port;
br_port = __prestera_bridge_port_by_dev(bridge, dev);
if (br_port) {
prestera_bridge_port_get(br_port);
return br_port;
}
br_port = prestera_bridge_port_create(bridge, dev);
if (!br_port)
return ERR_PTR(-ENOMEM);
return br_port;
}
static int
prestera_bridge_1d_port_join(struct prestera_bridge_port *br_port)
{
struct prestera_port *port = netdev_priv(br_port->dev);
struct prestera_bridge *bridge = br_port->bridge;
int err;
err = prestera_hw_bridge_port_add(port, bridge->bridge_id);
if (err)
return err;
err = prestera_hw_port_flood_set(port, br_port->flags & BR_FLOOD);
if (err)
goto err_port_flood_set;
err = prestera_hw_port_learning_set(port, br_port->flags & BR_LEARNING);
if (err)
goto err_port_learning_set;
return 0;
err_port_learning_set:
prestera_hw_port_flood_set(port, false);
err_port_flood_set:
prestera_hw_bridge_port_delete(port, bridge->bridge_id);
return err;
}
static int prestera_port_bridge_join(struct prestera_port *port,
struct net_device *upper)
{
struct prestera_switchdev *swdev = port->sw->swdev;
struct prestera_bridge_port *br_port;
struct prestera_bridge *bridge;
int err;
bridge = prestera_bridge_by_dev(swdev, upper);
if (!bridge) {
bridge = prestera_bridge_create(swdev, upper);
if (IS_ERR(bridge))
return PTR_ERR(bridge);
}
br_port = prestera_bridge_port_add(bridge, port->dev);
if (IS_ERR(br_port)) {
err = PTR_ERR(br_port);
goto err_brport_create;
}
if (bridge->vlan_enabled)
return 0;
err = prestera_bridge_1d_port_join(br_port);
if (err)
goto err_port_join;
return 0;
err_port_join:
prestera_bridge_port_put(br_port);
err_brport_create:
prestera_bridge_put(bridge);
return err;
}
static void prestera_bridge_1q_port_leave(struct prestera_bridge_port *br_port)
{
struct prestera_port *port = netdev_priv(br_port->dev);
prestera_hw_fdb_flush_port(port, PRESTERA_FDB_FLUSH_MODE_ALL);
prestera_port_pvid_set(port, PRESTERA_DEFAULT_VID);
}
static void prestera_bridge_1d_port_leave(struct prestera_bridge_port *br_port)
{
struct prestera_port *port = netdev_priv(br_port->dev);
prestera_hw_fdb_flush_port(port, PRESTERA_FDB_FLUSH_MODE_ALL);
prestera_hw_bridge_port_delete(port, br_port->bridge->bridge_id);
}
static int prestera_port_vid_stp_set(struct prestera_port *port, u16 vid,
u8 state)
{
u8 hw_state = state;
switch (state) {
case BR_STATE_DISABLED:
hw_state = PRESTERA_STP_DISABLED;
break;
case BR_STATE_BLOCKING:
case BR_STATE_LISTENING:
hw_state = PRESTERA_STP_BLOCK_LISTEN;
break;
case BR_STATE_LEARNING:
hw_state = PRESTERA_STP_LEARN;
break;
case BR_STATE_FORWARDING:
hw_state = PRESTERA_STP_FORWARD;
break;
default:
return -EINVAL;
}
return prestera_hw_vlan_port_stp_set(port, vid, hw_state);
}
static void prestera_port_bridge_leave(struct prestera_port *port,
struct net_device *upper)
{
struct prestera_switchdev *swdev = port->sw->swdev;
struct prestera_bridge_port *br_port;
struct prestera_bridge *bridge;
bridge = prestera_bridge_by_dev(swdev, upper);
if (!bridge)
return;
br_port = __prestera_bridge_port_by_dev(bridge, port->dev);
if (!br_port)
return;
bridge = br_port->bridge;
if (bridge->vlan_enabled)
prestera_bridge_1q_port_leave(br_port);
else
prestera_bridge_1d_port_leave(br_port);
prestera_hw_port_learning_set(port, false);
prestera_hw_port_flood_set(port, false);
prestera_port_vid_stp_set(port, PRESTERA_VID_ALL, BR_STATE_FORWARDING);
prestera_bridge_port_put(br_port);
}
int prestera_bridge_port_event(struct net_device *dev, unsigned long event,
void *ptr)
{
struct netdev_notifier_changeupper_info *info = ptr;
struct netlink_ext_ack *extack;
struct prestera_port *port;
struct net_device *upper;
int err;
extack = netdev_notifier_info_to_extack(&info->info);
port = netdev_priv(dev);
upper = info->upper_dev;
switch (event) {
case NETDEV_PRECHANGEUPPER:
if (!netif_is_bridge_master(upper)) {
NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
return -EINVAL;
}
if (!info->linking)
break;
if (netdev_has_any_upper_dev(upper)) {
NL_SET_ERR_MSG_MOD(extack, "Upper device is already enslaved");
return -EINVAL;
}
break;
case NETDEV_CHANGEUPPER:
if (!netif_is_bridge_master(upper))
break;
if (info->linking) {
err = prestera_port_bridge_join(port, upper);
if (err)
return err;
} else {
prestera_port_bridge_leave(port, upper);
}
break;
}
return 0;
}
static int prestera_port_attr_br_flags_set(struct prestera_port *port,
struct net_device *dev,
struct switchdev_brport_flags flags)
{
struct prestera_bridge_port *br_port;
int err;
br_port = prestera_bridge_port_by_dev(port->sw->swdev, dev);
if (!br_port)
return 0;
if (flags.mask & BR_FLOOD) {
err = prestera_hw_port_flood_set(port, flags.val & BR_FLOOD);
if (err)
return err;
}
if (flags.mask & BR_LEARNING) {
err = prestera_hw_port_learning_set(port,
flags.val & BR_LEARNING);
if (err)
return err;
}
memcpy(&br_port->flags, &flags.val, sizeof(flags.val));
return 0;
}
static int prestera_port_attr_br_ageing_set(struct prestera_port *port,
unsigned long ageing_clock_t)
{
unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
u32 ageing_time_ms = jiffies_to_msecs(ageing_jiffies);
struct prestera_switch *sw = port->sw;
if (ageing_time_ms < PRESTERA_MIN_AGEING_TIME_MS ||
ageing_time_ms > PRESTERA_MAX_AGEING_TIME_MS)
return -ERANGE;
return prestera_hw_switch_ageing_set(sw, ageing_time_ms);
}
static int prestera_port_attr_br_vlan_set(struct prestera_port *port,
struct net_device *dev,
bool vlan_enabled)
{
struct prestera_switch *sw = port->sw;
struct prestera_bridge *bridge;
bridge = prestera_bridge_by_dev(sw->swdev, dev);
if (WARN_ON(!bridge))
return -EINVAL;
if (bridge->vlan_enabled == vlan_enabled)
return 0;
netdev_err(bridge->dev, "VLAN filtering can't be changed for existing bridge\n");
return -EINVAL;
}
static int prestera_port_bridge_vlan_stp_set(struct prestera_port *port,
struct prestera_bridge_vlan *br_vlan,
u8 state)
{
struct prestera_port_vlan *port_vlan;
list_for_each_entry(port_vlan, &br_vlan->port_vlan_list, br_vlan_head) {
if (port_vlan->port != port)
continue;
return prestera_port_vid_stp_set(port, br_vlan->vid, state);
}
return 0;
}
static int prestera_port_attr_stp_state_set(struct prestera_port *port,
struct net_device *dev,
u8 state)
{
struct prestera_bridge_port *br_port;
struct prestera_bridge_vlan *br_vlan;
int err;
u16 vid;
br_port = prestera_bridge_port_by_dev(port->sw->swdev, dev);
if (!br_port)
return 0;
if (!br_port->bridge->vlan_enabled) {
vid = br_port->bridge->bridge_id;
err = prestera_port_vid_stp_set(port, vid, state);
if (err)
goto err_port_stp_set;
} else {
list_for_each_entry(br_vlan, &br_port->vlan_list, head) {
err = prestera_port_bridge_vlan_stp_set(port, br_vlan,
state);
if (err)
goto err_port_vlan_stp_set;
}
}
br_port->stp_state = state;
return 0;
err_port_vlan_stp_set:
list_for_each_entry_continue_reverse(br_vlan, &br_port->vlan_list, head)
prestera_port_bridge_vlan_stp_set(port, br_vlan, br_port->stp_state);
return err;
err_port_stp_set:
prestera_port_vid_stp_set(port, vid, br_port->stp_state);
return err;
}
static int prestera_port_obj_attr_set(struct net_device *dev,
const struct switchdev_attr *attr,
struct netlink_ext_ack *extack)
{
struct prestera_port *port = netdev_priv(dev);
int err = 0;
switch (attr->id) {
case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
err = prestera_port_attr_stp_state_set(port, attr->orig_dev,
attr->u.stp_state);
break;
case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
if (attr->u.brport_flags.mask &
~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD))
err = -EINVAL;
break;
case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
err = prestera_port_attr_br_flags_set(port, attr->orig_dev,
attr->u.brport_flags);
break;
case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
err = prestera_port_attr_br_ageing_set(port,
attr->u.ageing_time);
break;
case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
err = prestera_port_attr_br_vlan_set(port, attr->orig_dev,
attr->u.vlan_filtering);
break;
default:
err = -EOPNOTSUPP;
}
return err;
}
static void
prestera_fdb_offload_notify(struct prestera_port *port,
struct switchdev_notifier_fdb_info *info)
{
struct switchdev_notifier_fdb_info send_info;
send_info.addr = info->addr;
send_info.vid = info->vid;
send_info.offloaded = true;
call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, port->dev,
&send_info.info, NULL);
}
static int prestera_port_fdb_set(struct prestera_port *port,
struct switchdev_notifier_fdb_info *fdb_info,
bool adding)
{
struct prestera_switch *sw = port->sw;
struct prestera_bridge_port *br_port;
struct prestera_bridge *bridge;
int err;
u16 vid;
br_port = prestera_bridge_port_by_dev(sw->swdev, port->dev);
if (!br_port)
return -EINVAL;
bridge = br_port->bridge;
if (bridge->vlan_enabled)
vid = fdb_info->vid;
else
vid = bridge->bridge_id;
if (adding)
err = prestera_hw_fdb_add(port, fdb_info->addr, vid, false);
else
err = prestera_hw_fdb_del(port, fdb_info->addr, vid);
return err;
}
static void prestera_fdb_event_work(struct work_struct *work)
{
struct switchdev_notifier_fdb_info *fdb_info;
struct prestera_fdb_event_work *swdev_work;
struct prestera_port *port;
struct net_device *dev;
int err;
swdev_work = container_of(work, struct prestera_fdb_event_work, work);
dev = swdev_work->dev;
rtnl_lock();
port = prestera_port_dev_lower_find(dev);
if (!port)
goto out_unlock;
switch (swdev_work->event) {
case SWITCHDEV_FDB_ADD_TO_DEVICE:
fdb_info = &swdev_work->fdb_info;
if (!fdb_info->added_by_user || fdb_info->is_local)
break;
err = prestera_port_fdb_set(port, fdb_info, true);
if (err)
break;
prestera_fdb_offload_notify(port, fdb_info);
break;
case SWITCHDEV_FDB_DEL_TO_DEVICE:
fdb_info = &swdev_work->fdb_info;
prestera_port_fdb_set(port, fdb_info, false);
break;
}
out_unlock:
rtnl_unlock();
kfree(swdev_work->fdb_info.addr);
kfree(swdev_work);
dev_put(dev);
}
static int prestera_switchdev_event(struct notifier_block *unused,
unsigned long event, void *ptr)
{
struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
struct switchdev_notifier_fdb_info *fdb_info;
struct switchdev_notifier_info *info = ptr;
struct prestera_fdb_event_work *swdev_work;
struct net_device *upper;
int err;
if (event == SWITCHDEV_PORT_ATTR_SET) {
err = switchdev_handle_port_attr_set(dev, ptr,
prestera_netdev_check,
prestera_port_obj_attr_set);
return notifier_from_errno(err);
}
if (!prestera_netdev_check(dev))
return NOTIFY_DONE;
upper = netdev_master_upper_dev_get_rcu(dev);
if (!upper)
return NOTIFY_DONE;
if (!netif_is_bridge_master(upper))
return NOTIFY_DONE;
swdev_work = kzalloc(sizeof(*swdev_work), GFP_ATOMIC);
if (!swdev_work)
return NOTIFY_BAD;
swdev_work->event = event;
swdev_work->dev = dev;
switch (event) {
case SWITCHDEV_FDB_ADD_TO_DEVICE:
case SWITCHDEV_FDB_DEL_TO_DEVICE:
fdb_info = container_of(info,
struct switchdev_notifier_fdb_info,
info);
INIT_WORK(&swdev_work->work, prestera_fdb_event_work);
memcpy(&swdev_work->fdb_info, ptr,
sizeof(swdev_work->fdb_info));
swdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
if (!swdev_work->fdb_info.addr)
goto out_bad;
ether_addr_copy((u8 *)swdev_work->fdb_info.addr,
fdb_info->addr);
dev_hold(dev);
break;
default:
kfree(swdev_work);
return NOTIFY_DONE;
}
queue_work(swdev_wq, &swdev_work->work);
return NOTIFY_DONE;
out_bad:
kfree(swdev_work);
return NOTIFY_BAD;
}
static int
prestera_port_vlan_bridge_join(struct prestera_port_vlan *port_vlan,
struct prestera_bridge_port *br_port)
{
struct prestera_port *port = port_vlan->port;
struct prestera_bridge_vlan *br_vlan;
u16 vid = port_vlan->vid;
int err;
if (port_vlan->br_port)
return 0;
err = prestera_hw_port_flood_set(port, br_port->flags & BR_FLOOD);
if (err)
return err;
err = prestera_hw_port_learning_set(port, br_port->flags & BR_LEARNING);
if (err)
goto err_port_learning_set;
err = prestera_port_vid_stp_set(port, vid, br_port->stp_state);
if (err)
goto err_port_vid_stp_set;
br_vlan = prestera_bridge_vlan_by_vid(br_port, vid);
if (!br_vlan) {
br_vlan = prestera_bridge_vlan_create(br_port, vid);
if (!br_vlan) {
err = -ENOMEM;
goto err_bridge_vlan_get;
}
}
list_add(&port_vlan->br_vlan_head, &br_vlan->port_vlan_list);
prestera_bridge_port_get(br_port);
port_vlan->br_port = br_port;
return 0;
err_bridge_vlan_get:
prestera_port_vid_stp_set(port, vid, BR_STATE_FORWARDING);
err_port_vid_stp_set:
prestera_hw_port_learning_set(port, false);
err_port_learning_set:
return err;
}
static int
prestera_bridge_port_vlan_add(struct prestera_port *port,
struct prestera_bridge_port *br_port,
u16 vid, bool is_untagged, bool is_pvid,
struct netlink_ext_ack *extack)
{
struct prestera_port_vlan *port_vlan;
u16 old_pvid = port->pvid;
u16 pvid;
int err;
if (is_pvid)
pvid = vid;
else
pvid = port->pvid == vid ? 0 : port->pvid;
port_vlan = prestera_port_vlan_by_vid(port, vid);
if (port_vlan && port_vlan->br_port != br_port)
return -EEXIST;
if (!port_vlan) {
port_vlan = prestera_port_vlan_create(port, vid, is_untagged);
if (IS_ERR(port_vlan))
return PTR_ERR(port_vlan);
} else {
err = prestera_hw_vlan_port_set(port, vid, true, is_untagged);
if (err)
goto err_port_vlan_set;
}
err = prestera_port_pvid_set(port, pvid);
if (err)
goto err_port_pvid_set;
err = prestera_port_vlan_bridge_join(port_vlan, br_port);
if (err)
goto err_port_vlan_bridge_join;
return 0;
err_port_vlan_bridge_join:
prestera_port_pvid_set(port, old_pvid);
err_port_pvid_set:
prestera_hw_vlan_port_set(port, vid, false, false);
err_port_vlan_set:
prestera_port_vlan_destroy(port_vlan);
return err;
}
static void
prestera_bridge_port_vlan_del(struct prestera_port *port,
struct prestera_bridge_port *br_port, u16 vid)
{
u16 pvid = port->pvid == vid ? 0 : port->pvid;
struct prestera_port_vlan *port_vlan;
port_vlan = prestera_port_vlan_by_vid(port, vid);
if (WARN_ON(!port_vlan))
return;
prestera_port_vlan_bridge_leave(port_vlan);
prestera_port_pvid_set(port, pvid);
prestera_port_vlan_destroy(port_vlan);
}
static int prestera_port_vlans_add(struct prestera_port *port,
const struct switchdev_obj_port_vlan *vlan,
struct netlink_ext_ack *extack)
{
bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
struct net_device *dev = vlan->obj.orig_dev;
struct prestera_bridge_port *br_port;
struct prestera_switch *sw = port->sw;
struct prestera_bridge *bridge;
if (netif_is_bridge_master(dev))
return 0;
br_port = prestera_bridge_port_by_dev(sw->swdev, dev);
if (WARN_ON(!br_port))
return -EINVAL;
bridge = br_port->bridge;
if (!bridge->vlan_enabled)
return 0;
return prestera_bridge_port_vlan_add(port, br_port,
vlan->vid, flag_untagged,
flag_pvid, extack);
}
static int prestera_port_obj_add(struct net_device *dev,
const struct switchdev_obj *obj,
struct netlink_ext_ack *extack)
{
struct prestera_port *port = netdev_priv(dev);
const struct switchdev_obj_port_vlan *vlan;
switch (obj->id) {
case SWITCHDEV_OBJ_ID_PORT_VLAN:
vlan = SWITCHDEV_OBJ_PORT_VLAN(obj);
return prestera_port_vlans_add(port, vlan, extack);
default:
return -EOPNOTSUPP;
}
}
static int prestera_port_vlans_del(struct prestera_port *port,
const struct switchdev_obj_port_vlan *vlan)
{
struct net_device *dev = vlan->obj.orig_dev;
struct prestera_bridge_port *br_port;
struct prestera_switch *sw = port->sw;
if (netif_is_bridge_master(dev))
return -EOPNOTSUPP;
br_port = prestera_bridge_port_by_dev(sw->swdev, dev);
if (WARN_ON(!br_port))
return -EINVAL;
if (!br_port->bridge->vlan_enabled)
return 0;
prestera_bridge_port_vlan_del(port, br_port, vlan->vid);
return 0;
}
static int prestera_port_obj_del(struct net_device *dev,
const struct switchdev_obj *obj)
{
struct prestera_port *port = netdev_priv(dev);
switch (obj->id) {
case SWITCHDEV_OBJ_ID_PORT_VLAN:
return prestera_port_vlans_del(port, SWITCHDEV_OBJ_PORT_VLAN(obj));
default:
return -EOPNOTSUPP;
}
}
static int prestera_switchdev_blk_event(struct notifier_block *unused,
unsigned long event, void *ptr)
{
struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
int err;
switch (event) {
case SWITCHDEV_PORT_OBJ_ADD:
err = switchdev_handle_port_obj_add(dev, ptr,
prestera_netdev_check,
prestera_port_obj_add);
break;
case SWITCHDEV_PORT_OBJ_DEL:
err = switchdev_handle_port_obj_del(dev, ptr,
prestera_netdev_check,
prestera_port_obj_del);
break;
case SWITCHDEV_PORT_ATTR_SET:
err = switchdev_handle_port_attr_set(dev, ptr,
prestera_netdev_check,
prestera_port_obj_attr_set);
break;
default:
err = -EOPNOTSUPP;
}
return notifier_from_errno(err);
}
static void prestera_fdb_event(struct prestera_switch *sw,
struct prestera_event *evt, void *arg)
{
struct switchdev_notifier_fdb_info info;
struct prestera_port *port;
port = prestera_find_port(sw, evt->fdb_evt.port_id);
if (!port)
return;
info.addr = evt->fdb_evt.data.mac;
info.vid = evt->fdb_evt.vid;
info.offloaded = true;
rtnl_lock();
switch (evt->id) {
case PRESTERA_FDB_EVENT_LEARNED:
call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_BRIDGE,
port->dev, &info.info, NULL);
break;
case PRESTERA_FDB_EVENT_AGED:
call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_BRIDGE,
port->dev, &info.info, NULL);
break;
}
rtnl_unlock();
}
static int prestera_fdb_init(struct prestera_switch *sw)
{
int err;
err = prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_FDB,
prestera_fdb_event, NULL);
if (err)
return err;
err = prestera_hw_switch_ageing_set(sw, PRESTERA_DEFAULT_AGEING_TIME_MS);
if (err)
goto err_ageing_set;
return 0;
err_ageing_set:
prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_FDB,
prestera_fdb_event);
return err;
}
static void prestera_fdb_fini(struct prestera_switch *sw)
{
prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_FDB,
prestera_fdb_event);
}
static int prestera_switchdev_handler_init(struct prestera_switchdev *swdev)
{
int err;
swdev->swdev_nb.notifier_call = prestera_switchdev_event;
err = register_switchdev_notifier(&swdev->swdev_nb);
if (err)
goto err_register_swdev_notifier;
swdev->swdev_nb_blk.notifier_call = prestera_switchdev_blk_event;
err = register_switchdev_blocking_notifier(&swdev->swdev_nb_blk);
if (err)
goto err_register_blk_swdev_notifier;
return 0;
err_register_blk_swdev_notifier:
unregister_switchdev_notifier(&swdev->swdev_nb);
err_register_swdev_notifier:
destroy_workqueue(swdev_wq);
return err;
}
static void prestera_switchdev_handler_fini(struct prestera_switchdev *swdev)
{
unregister_switchdev_blocking_notifier(&swdev->swdev_nb_blk);
unregister_switchdev_notifier(&swdev->swdev_nb);
}
int prestera_switchdev_init(struct prestera_switch *sw)
{
struct prestera_switchdev *swdev;
int err;
swdev = kzalloc(sizeof(*swdev), GFP_KERNEL);
if (!swdev)
return -ENOMEM;
sw->swdev = swdev;
swdev->sw = sw;
INIT_LIST_HEAD(&swdev->bridge_list);
swdev_wq = alloc_ordered_workqueue("%s_ordered", 0, "prestera_br");
if (!swdev_wq) {
err = -ENOMEM;
goto err_alloc_wq;
}
err = prestera_switchdev_handler_init(swdev);
if (err)
goto err_swdev_init;
err = prestera_fdb_init(sw);
if (err)
goto err_fdb_init;
return 0;
err_fdb_init:
err_swdev_init:
destroy_workqueue(swdev_wq);
err_alloc_wq:
kfree(swdev);
return err;
}
void prestera_switchdev_fini(struct prestera_switch *sw)
{
struct prestera_switchdev *swdev = sw->swdev;
prestera_fdb_fini(sw);
prestera_switchdev_handler_fini(swdev);
destroy_workqueue(swdev_wq);
kfree(swdev);
}
|