Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
/* Copyright (c) 2019-2020 Marvell International Ltd. All rights reserved */

#include <linux/etherdevice.h>
#include <linux/jiffies.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/netdev_features.h>
#include <linux/of.h>
#include <linux/of_net.h>
#include <linux/if_vlan.h>
#include <linux/phylink.h>

#include "prestera.h"
#include "prestera_hw.h"
#include "prestera_acl.h"
#include "prestera_flow.h"
#include "prestera_span.h"
#include "prestera_rxtx.h"
#include "prestera_devlink.h"
#include "prestera_ethtool.h"
#include "prestera_counter.h"
#include "prestera_switchdev.h"

#define PRESTERA_MTU_DEFAULT	1536

#define PRESTERA_STATS_DELAY_MS	1000

#define PRESTERA_MAC_ADDR_NUM_MAX	255

static struct workqueue_struct *prestera_wq;
static struct workqueue_struct *prestera_owq;

void prestera_queue_work(struct work_struct *work)
{
	queue_work(prestera_owq, work);
}

int prestera_port_learning_set(struct prestera_port *port, bool learn)
{
	return prestera_hw_port_learning_set(port, learn);
}

int prestera_port_uc_flood_set(struct prestera_port *port, bool flood)
{
	return prestera_hw_port_uc_flood_set(port, flood);
}

int prestera_port_mc_flood_set(struct prestera_port *port, bool flood)
{
	return prestera_hw_port_mc_flood_set(port, flood);
}

int prestera_port_pvid_set(struct prestera_port *port, u16 vid)
{
	enum prestera_accept_frm_type frm_type;
	int err;

	frm_type = PRESTERA_ACCEPT_FRAME_TYPE_TAGGED;

	if (vid) {
		err = prestera_hw_vlan_port_vid_set(port, vid);
		if (err)
			return err;

		frm_type = PRESTERA_ACCEPT_FRAME_TYPE_ALL;
	}

	err = prestera_hw_port_accept_frm_type(port, frm_type);
	if (err && frm_type == PRESTERA_ACCEPT_FRAME_TYPE_ALL)
		prestera_hw_vlan_port_vid_set(port, port->pvid);

	port->pvid = vid;
	return 0;
}

struct prestera_port *prestera_port_find_by_hwid(struct prestera_switch *sw,
						 u32 dev_id, u32 hw_id)
{
	struct prestera_port *port = NULL, *tmp;

	read_lock(&sw->port_list_lock);
	list_for_each_entry(tmp, &sw->port_list, list) {
		if (tmp->dev_id == dev_id && tmp->hw_id == hw_id) {
			port = tmp;
			break;
		}
	}
	read_unlock(&sw->port_list_lock);

	return port;
}

struct prestera_port *prestera_find_port(struct prestera_switch *sw, u32 id)
{
	struct prestera_port *port = NULL, *tmp;

	read_lock(&sw->port_list_lock);
	list_for_each_entry(tmp, &sw->port_list, list) {
		if (tmp->id == id) {
			port = tmp;
			break;
		}
	}
	read_unlock(&sw->port_list_lock);

	return port;
}

struct prestera_switch *prestera_switch_get(struct net_device *dev)
{
	struct prestera_port *port;

	port = prestera_port_dev_lower_find(dev);
	return port ? port->sw : NULL;
}

int prestera_port_cfg_mac_read(struct prestera_port *port,
			       struct prestera_port_mac_config *cfg)
{
	*cfg = port->cfg_mac;
	return 0;
}

int prestera_port_cfg_mac_write(struct prestera_port *port,
				struct prestera_port_mac_config *cfg)
{
	int err;

	err = prestera_hw_port_mac_mode_set(port, cfg->admin,
					    cfg->mode, cfg->inband, cfg->speed,
					    cfg->duplex, cfg->fec);
	if (err)
		return err;

	port->cfg_mac = *cfg;
	return 0;
}

static int prestera_port_open(struct net_device *dev)
{
	struct prestera_port *port = netdev_priv(dev);
	struct prestera_port_mac_config cfg_mac;
	int err = 0;

	if (port->phy_link) {
		phylink_start(port->phy_link);
	} else {
		if (port->caps.transceiver == PRESTERA_PORT_TCVR_SFP) {
			err = prestera_port_cfg_mac_read(port, &cfg_mac);
			if (!err) {
				cfg_mac.admin = true;
				err = prestera_port_cfg_mac_write(port,
								  &cfg_mac);
			}
		} else {
			port->cfg_phy.admin = true;
			err = prestera_hw_port_phy_mode_set(port, true,
							    port->autoneg,
							    port->cfg_phy.mode,
							    port->adver_link_modes,
							    port->cfg_phy.mdix);
		}
	}

	netif_start_queue(dev);

	return err;
}

static int prestera_port_close(struct net_device *dev)
{
	struct prestera_port *port = netdev_priv(dev);
	struct prestera_port_mac_config cfg_mac;
	int err = 0;

	netif_stop_queue(dev);

	if (port->phy_link) {
		phylink_stop(port->phy_link);
		phylink_disconnect_phy(port->phy_link);
		err = prestera_port_cfg_mac_read(port, &cfg_mac);
		if (!err) {
			cfg_mac.admin = false;
			prestera_port_cfg_mac_write(port, &cfg_mac);
		}
	} else {
		if (port->caps.transceiver == PRESTERA_PORT_TCVR_SFP) {
			err = prestera_port_cfg_mac_read(port, &cfg_mac);
			if (!err) {
				cfg_mac.admin = false;
				prestera_port_cfg_mac_write(port, &cfg_mac);
			}
		} else {
			port->cfg_phy.admin = false;
			err = prestera_hw_port_phy_mode_set(port, false, port->autoneg,
							    port->cfg_phy.mode,
							    port->adver_link_modes,
							    port->cfg_phy.mdix);
		}
	}

	return err;
}

static void
prestera_port_mac_state_cache_read(struct prestera_port *port,
				   struct prestera_port_mac_state *state)
{
	spin_lock(&port->state_mac_lock);
	*state = port->state_mac;
	spin_unlock(&port->state_mac_lock);
}

static void
prestera_port_mac_state_cache_write(struct prestera_port *port,
				    struct prestera_port_mac_state *state)
{
	spin_lock(&port->state_mac_lock);
	port->state_mac = *state;
	spin_unlock(&port->state_mac_lock);
}

static struct prestera_port *prestera_pcs_to_port(struct phylink_pcs *pcs)
{
	return container_of(pcs, struct prestera_port, phylink_pcs);
}

static void prestera_mac_config(struct phylink_config *config,
				unsigned int an_mode,
				const struct phylink_link_state *state)
{
}

static void prestera_mac_link_down(struct phylink_config *config,
				   unsigned int mode, phy_interface_t interface)
{
	struct net_device *ndev = to_net_dev(config->dev);
	struct prestera_port *port = netdev_priv(ndev);
	struct prestera_port_mac_state state_mac;

	/* Invalidate. Parameters will update on next link event. */
	memset(&state_mac, 0, sizeof(state_mac));
	state_mac.valid = false;
	prestera_port_mac_state_cache_write(port, &state_mac);
}

static void prestera_mac_link_up(struct phylink_config *config,
				 struct phy_device *phy,
				 unsigned int mode, phy_interface_t interface,
				 int speed, int duplex,
				 bool tx_pause, bool rx_pause)
{
}

static struct phylink_pcs *
prestera_mac_select_pcs(struct phylink_config *config,
			phy_interface_t interface)
{
	struct net_device *dev = to_net_dev(config->dev);
	struct prestera_port *port = netdev_priv(dev);

	return &port->phylink_pcs;
}

static void prestera_pcs_get_state(struct phylink_pcs *pcs,
				   struct phylink_link_state *state)
{
	struct prestera_port *port = container_of(pcs, struct prestera_port,
						  phylink_pcs);
	struct prestera_port_mac_state smac;

	prestera_port_mac_state_cache_read(port, &smac);

	if (smac.valid) {
		state->link = smac.oper ? 1 : 0;
		/* AN is completed, when port is up */
		state->an_complete = (smac.oper && port->autoneg) ? 1 : 0;
		state->speed = smac.speed;
		state->duplex = smac.duplex;
	} else {
		state->link = 0;
		state->an_complete = 0;
	}
}

static int prestera_pcs_config(struct phylink_pcs *pcs,
			       unsigned int mode,
			       phy_interface_t interface,
			       const unsigned long *advertising,
			       bool permit_pause_to_mac)
{
	struct prestera_port *port = prestera_pcs_to_port(pcs);
	struct prestera_port_mac_config cfg_mac;
	int err;

	err = prestera_port_cfg_mac_read(port, &cfg_mac);
	if (err)
		return err;

	cfg_mac.admin = true;
	cfg_mac.fec = PRESTERA_PORT_FEC_OFF;

	switch (interface) {
	case PHY_INTERFACE_MODE_10GBASER:
		cfg_mac.speed = SPEED_10000;
		cfg_mac.inband = 0;
		cfg_mac.mode = PRESTERA_MAC_MODE_SR_LR;
		break;
	case PHY_INTERFACE_MODE_2500BASEX:
		cfg_mac.speed = SPEED_2500;
		cfg_mac.duplex = DUPLEX_FULL;
		cfg_mac.inband = test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
					  advertising);
		cfg_mac.mode = PRESTERA_MAC_MODE_SGMII;
		break;
	case PHY_INTERFACE_MODE_SGMII:
		cfg_mac.inband = 1;
		cfg_mac.mode = PRESTERA_MAC_MODE_SGMII;
		break;
	case PHY_INTERFACE_MODE_1000BASEX:
	default:
		cfg_mac.speed = SPEED_1000;
		cfg_mac.duplex = DUPLEX_FULL;
		cfg_mac.inband = test_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
					  advertising);
		cfg_mac.mode = PRESTERA_MAC_MODE_1000BASE_X;
		break;
	}

	err = prestera_port_cfg_mac_write(port, &cfg_mac);
	if (err)
		return err;

	return 0;
}

static void prestera_pcs_an_restart(struct phylink_pcs *pcs)
{
	/* TODO: add 1000basex AN restart support
	 * (Currently FW has no support for 1000baseX AN restart, but it will in the future,
	 * so as for now the function would stay empty.)
	 */
}

static const struct phylink_mac_ops prestera_mac_ops = {
	.validate = phylink_generic_validate,
	.mac_select_pcs = prestera_mac_select_pcs,
	.mac_config = prestera_mac_config,
	.mac_link_down = prestera_mac_link_down,
	.mac_link_up = prestera_mac_link_up,
};

static const struct phylink_pcs_ops prestera_pcs_ops = {
	.pcs_get_state = prestera_pcs_get_state,
	.pcs_config = prestera_pcs_config,
	.pcs_an_restart = prestera_pcs_an_restart,
};

static int prestera_port_sfp_bind(struct prestera_port *port)
{
	struct prestera_switch *sw = port->sw;
	struct device_node *ports, *node;
	struct fwnode_handle *fwnode;
	struct phylink *phy_link;
	int err;

	if (!sw->np)
		return 0;

	of_node_get(sw->np);
	ports = of_find_node_by_name(sw->np, "ports");

	for_each_child_of_node(ports, node) {
		int num;

		err = of_property_read_u32(node, "prestera,port-num", &num);
		if (err) {
			dev_err(sw->dev->dev,
				"device node %pOF has no valid reg property: %d\n",
				node, err);
			goto out;
		}

		if (port->fp_id != num)
			continue;

		port->phylink_pcs.ops = &prestera_pcs_ops;

		port->phy_config.dev = &port->dev->dev;
		port->phy_config.type = PHYLINK_NETDEV;

		fwnode = of_fwnode_handle(node);

		__set_bit(PHY_INTERFACE_MODE_10GBASER,
			  port->phy_config.supported_interfaces);
		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
			  port->phy_config.supported_interfaces);
		__set_bit(PHY_INTERFACE_MODE_SGMII,
			  port->phy_config.supported_interfaces);
		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
			  port->phy_config.supported_interfaces);

		port->phy_config.mac_capabilities =
			MAC_1000 | MAC_2500FD | MAC_10000FD;

		phy_link = phylink_create(&port->phy_config, fwnode,
					  PHY_INTERFACE_MODE_INTERNAL,
					  &prestera_mac_ops);
		if (IS_ERR(phy_link)) {
			netdev_err(port->dev, "failed to create phylink\n");
			err = PTR_ERR(phy_link);
			goto out;
		}

		port->phy_link = phy_link;
		break;
	}

out:
	of_node_put(node);
	of_node_put(ports);
	return err;
}

static int prestera_port_sfp_unbind(struct prestera_port *port)
{
	if (port->phy_link)
		phylink_destroy(port->phy_link);

	return 0;
}

static netdev_tx_t prestera_port_xmit(struct sk_buff *skb,
				      struct net_device *dev)
{
	return prestera_rxtx_xmit(netdev_priv(dev), skb);
}

int prestera_is_valid_mac_addr(struct prestera_port *port, const u8 *addr)
{
	if (!is_valid_ether_addr(addr))
		return -EADDRNOTAVAIL;

	/* firmware requires that port's MAC address contains first 5 bytes
	 * of the base MAC address
	 */
	if (memcmp(port->sw->base_mac, addr, ETH_ALEN - 1))
		return -EINVAL;

	return 0;
}

static int prestera_port_set_mac_address(struct net_device *dev, void *p)
{
	struct prestera_port *port = netdev_priv(dev);
	struct sockaddr *addr = p;
	int err;

	err = prestera_is_valid_mac_addr(port, addr->sa_data);
	if (err)
		return err;

	err = prestera_hw_port_mac_set(port, addr->sa_data);
	if (err)
		return err;

	eth_hw_addr_set(dev, addr->sa_data);

	return 0;
}

static int prestera_port_change_mtu(struct net_device *dev, int mtu)
{
	struct prestera_port *port = netdev_priv(dev);
	int err;

	err = prestera_hw_port_mtu_set(port, mtu);
	if (err)
		return err;

	dev->mtu = mtu;

	return 0;
}

static void prestera_port_get_stats64(struct net_device *dev,
				      struct rtnl_link_stats64 *stats)
{
	struct prestera_port *port = netdev_priv(dev);
	struct prestera_port_stats *port_stats = &port->cached_hw_stats.stats;

	stats->rx_packets = port_stats->broadcast_frames_received +
				port_stats->multicast_frames_received +
				port_stats->unicast_frames_received;

	stats->tx_packets = port_stats->broadcast_frames_sent +
				port_stats->multicast_frames_sent +
				port_stats->unicast_frames_sent;

	stats->rx_bytes = port_stats->good_octets_received;

	stats->tx_bytes = port_stats->good_octets_sent;

	stats->rx_errors = port_stats->rx_error_frame_received;
	stats->tx_errors = port_stats->mac_trans_error;

	stats->rx_dropped = port_stats->buffer_overrun;
	stats->tx_dropped = 0;

	stats->multicast = port_stats->multicast_frames_received;
	stats->collisions = port_stats->excessive_collision;

	stats->rx_crc_errors = port_stats->bad_crc;
}

static void prestera_port_get_hw_stats(struct prestera_port *port)
{
	prestera_hw_port_stats_get(port, &port->cached_hw_stats.stats);
}

static void prestera_port_stats_update(struct work_struct *work)
{
	struct prestera_port *port =
		container_of(work, struct prestera_port,
			     cached_hw_stats.caching_dw.work);

	prestera_port_get_hw_stats(port);

	queue_delayed_work(prestera_wq, &port->cached_hw_stats.caching_dw,
			   msecs_to_jiffies(PRESTERA_STATS_DELAY_MS));
}

static int prestera_port_setup_tc(struct net_device *dev,
				  enum tc_setup_type type,
				  void *type_data)
{
	struct prestera_port *port = netdev_priv(dev);

	switch (type) {
	case TC_SETUP_BLOCK:
		return prestera_flow_block_setup(port, type_data);
	default:
		return -EOPNOTSUPP;
	}
}

static const struct net_device_ops prestera_netdev_ops = {
	.ndo_open = prestera_port_open,
	.ndo_stop = prestera_port_close,
	.ndo_start_xmit = prestera_port_xmit,
	.ndo_setup_tc = prestera_port_setup_tc,
	.ndo_change_mtu = prestera_port_change_mtu,
	.ndo_get_stats64 = prestera_port_get_stats64,
	.ndo_set_mac_address = prestera_port_set_mac_address,
	.ndo_get_devlink_port = prestera_devlink_get_port,
};

int prestera_port_autoneg_set(struct prestera_port *port, u64 link_modes)
{
	int err;

	if (port->autoneg && port->adver_link_modes == link_modes)
		return 0;

	err = prestera_hw_port_phy_mode_set(port, port->cfg_phy.admin,
					    true, 0, link_modes,
					    port->cfg_phy.mdix);
	if (err)
		return err;

	port->adver_fec = BIT(PRESTERA_PORT_FEC_OFF);
	port->adver_link_modes = link_modes;
	port->cfg_phy.mode = 0;
	port->autoneg = true;

	return 0;
}

static void prestera_port_list_add(struct prestera_port *port)
{
	write_lock(&port->sw->port_list_lock);
	list_add(&port->list, &port->sw->port_list);
	write_unlock(&port->sw->port_list_lock);
}

static void prestera_port_list_del(struct prestera_port *port)
{
	write_lock(&port->sw->port_list_lock);
	list_del(&port->list);
	write_unlock(&port->sw->port_list_lock);
}

static int prestera_port_create(struct prestera_switch *sw, u32 id)
{
	struct prestera_port_mac_config cfg_mac;
	struct prestera_port *port;
	struct net_device *dev;
	int err;

	dev = alloc_etherdev(sizeof(*port));
	if (!dev)
		return -ENOMEM;

	port = netdev_priv(dev);

	INIT_LIST_HEAD(&port->vlans_list);
	port->pvid = PRESTERA_DEFAULT_VID;
	port->lag = NULL;
	port->dev = dev;
	port->id = id;
	port->sw = sw;

	spin_lock_init(&port->state_mac_lock);

	err = prestera_hw_port_info_get(port, &port->dev_id, &port->hw_id,
					&port->fp_id);
	if (err) {
		dev_err(prestera_dev(sw), "Failed to get port(%u) info\n", id);
		goto err_port_info_get;
	}

	err = prestera_devlink_port_register(port);
	if (err)
		goto err_dl_port_register;

	dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_HW_TC;
	dev->netdev_ops = &prestera_netdev_ops;
	dev->ethtool_ops = &prestera_ethtool_ops;
	SET_NETDEV_DEV(dev, sw->dev->dev);

	if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP)
		netif_carrier_off(dev);

	dev->mtu = min_t(unsigned int, sw->mtu_max, PRESTERA_MTU_DEFAULT);
	dev->min_mtu = sw->mtu_min;
	dev->max_mtu = sw->mtu_max;

	err = prestera_hw_port_mtu_set(port, dev->mtu);
	if (err) {
		dev_err(prestera_dev(sw), "Failed to set port(%u) mtu(%d)\n",
			id, dev->mtu);
		goto err_port_init;
	}

	if (port->fp_id >= PRESTERA_MAC_ADDR_NUM_MAX) {
		err = -EINVAL;
		goto err_port_init;
	}

	eth_hw_addr_gen(dev, sw->base_mac, port->fp_id);
	/* firmware requires that port's MAC address consist of the first
	 * 5 bytes of the base MAC address
	 */
	if (memcmp(dev->dev_addr, sw->base_mac, ETH_ALEN - 1)) {
		dev_warn(prestera_dev(sw), "Port MAC address wraps for port(%u)\n", id);
		dev_addr_mod(dev, 0, sw->base_mac, ETH_ALEN - 1);
	}

	err = prestera_hw_port_mac_set(port, dev->dev_addr);
	if (err) {
		dev_err(prestera_dev(sw), "Failed to set port(%u) mac addr\n", id);
		goto err_port_init;
	}

	err = prestera_hw_port_cap_get(port, &port->caps);
	if (err) {
		dev_err(prestera_dev(sw), "Failed to get port(%u) caps\n", id);
		goto err_port_init;
	}

	port->adver_link_modes = port->caps.supp_link_modes;
	port->adver_fec = 0;
	port->autoneg = true;

	/* initialize config mac */
	if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP) {
		cfg_mac.admin = true;
		cfg_mac.mode = PRESTERA_MAC_MODE_INTERNAL;
	} else {
		cfg_mac.admin = false;
		cfg_mac.mode = PRESTERA_MAC_MODE_MAX;
	}
	cfg_mac.inband = 0;
	cfg_mac.speed = 0;
	cfg_mac.duplex = DUPLEX_UNKNOWN;
	cfg_mac.fec = PRESTERA_PORT_FEC_OFF;

	err = prestera_port_cfg_mac_write(port, &cfg_mac);
	if (err) {
		dev_err(prestera_dev(sw),
			"Failed to set port(%u) mac mode\n", id);
		goto err_port_init;
	}

	/* initialize config phy (if this is inegral) */
	if (port->caps.transceiver != PRESTERA_PORT_TCVR_SFP) {
		port->cfg_phy.mdix = ETH_TP_MDI_AUTO;
		port->cfg_phy.admin = false;
		err = prestera_hw_port_phy_mode_set(port,
						    port->cfg_phy.admin,
						    false, 0, 0,
						    port->cfg_phy.mdix);
		if (err) {
			dev_err(prestera_dev(sw),
				"Failed to set port(%u) phy mode\n", id);
			goto err_port_init;
		}
	}

	err = prestera_rxtx_port_init(port);
	if (err)
		goto err_port_init;

	INIT_DELAYED_WORK(&port->cached_hw_stats.caching_dw,
			  &prestera_port_stats_update);

	prestera_port_list_add(port);

	err = register_netdev(dev);
	if (err)
		goto err_register_netdev;

	prestera_devlink_port_set(port);

	err = prestera_port_sfp_bind(port);
	if (err)
		goto err_sfp_bind;

	return 0;

err_sfp_bind:
	unregister_netdev(dev);
err_register_netdev:
	prestera_port_list_del(port);
err_port_init:
	prestera_devlink_port_unregister(port);
err_dl_port_register:
err_port_info_get:
	free_netdev(dev);
	return err;
}

static void prestera_port_destroy(struct prestera_port *port)
{
	struct net_device *dev = port->dev;

	cancel_delayed_work_sync(&port->cached_hw_stats.caching_dw);
	prestera_devlink_port_clear(port);
	unregister_netdev(dev);
	prestera_port_list_del(port);
	prestera_devlink_port_unregister(port);
	free_netdev(dev);
}

static void prestera_destroy_ports(struct prestera_switch *sw)
{
	struct prestera_port *port, *tmp;

	list_for_each_entry_safe(port, tmp, &sw->port_list, list)
		prestera_port_destroy(port);
}

static int prestera_create_ports(struct prestera_switch *sw)
{
	struct prestera_port *port, *tmp;
	u32 port_idx;
	int err;

	for (port_idx = 0; port_idx < sw->port_count; port_idx++) {
		err = prestera_port_create(sw, port_idx);
		if (err)
			goto err_port_create;
	}

	return 0;

err_port_create:
	list_for_each_entry_safe(port, tmp, &sw->port_list, list) {
		prestera_port_sfp_unbind(port);
		prestera_port_destroy(port);
	}

	return err;
}

static void prestera_port_handle_event(struct prestera_switch *sw,
				       struct prestera_event *evt, void *arg)
{
	struct prestera_port_mac_state smac;
	struct prestera_port_event *pevt;
	struct delayed_work *caching_dw;
	struct prestera_port *port;

	if (evt->id == PRESTERA_PORT_EVENT_MAC_STATE_CHANGED) {
		pevt = &evt->port_evt;
		port = prestera_find_port(sw, pevt->port_id);
		if (!port || !port->dev)
			return;

		caching_dw = &port->cached_hw_stats.caching_dw;

		memset(&smac, 0, sizeof(smac));
		smac.valid = true;
		smac.oper = pevt->data.mac.oper;
		if (smac.oper) {
			smac.mode = pevt->data.mac.mode;
			smac.speed = pevt->data.mac.speed;
			smac.duplex = pevt->data.mac.duplex;
			smac.fc = pevt->data.mac.fc;
			smac.fec = pevt->data.mac.fec;
		}
		prestera_port_mac_state_cache_write(port, &smac);

		if (port->state_mac.oper) {
			if (port->phy_link)
				phylink_mac_change(port->phy_link, true);
			else
				netif_carrier_on(port->dev);

			if (!delayed_work_pending(caching_dw))
				queue_delayed_work(prestera_wq, caching_dw, 0);
		} else {
			if (port->phy_link)
				phylink_mac_change(port->phy_link, false);
			else if (netif_running(port->dev) && netif_carrier_ok(port->dev))
				netif_carrier_off(port->dev);

			if (delayed_work_pending(caching_dw))
				cancel_delayed_work(caching_dw);
		}
	}
}

static int prestera_event_handlers_register(struct prestera_switch *sw)
{
	return prestera_hw_event_handler_register(sw, PRESTERA_EVENT_TYPE_PORT,
						  prestera_port_handle_event,
						  NULL);
}

static void prestera_event_handlers_unregister(struct prestera_switch *sw)
{
	prestera_hw_event_handler_unregister(sw, PRESTERA_EVENT_TYPE_PORT,
					     prestera_port_handle_event);
}

static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw)
{
	struct device_node *base_mac_np;
	int ret = 0;

	if (sw->np) {
		base_mac_np = of_parse_phandle(sw->np, "base-mac-provider", 0);
		if (base_mac_np) {
			ret = of_get_mac_address(base_mac_np, sw->base_mac);
			of_node_put(base_mac_np);
		}
	}

	if (!is_valid_ether_addr(sw->base_mac) || ret) {
		eth_random_addr(sw->base_mac);
		dev_info(prestera_dev(sw), "using random base mac address\n");
	}

	return prestera_hw_switch_mac_set(sw, sw->base_mac);
}

struct prestera_lag *prestera_lag_by_id(struct prestera_switch *sw, u16 id)
{
	return id < sw->lag_max ? &sw->lags[id] : NULL;
}

static struct prestera_lag *prestera_lag_by_dev(struct prestera_switch *sw,
						struct net_device *dev)
{
	struct prestera_lag *lag;
	u16 id;

	for (id = 0; id < sw->lag_max; id++) {
		lag = &sw->lags[id];
		if (lag->dev == dev)
			return lag;
	}

	return NULL;
}

int prestera_lag_id(struct prestera_switch *sw,
		    struct net_device *lag_dev, u16 *lag_id)
{
	struct prestera_lag *lag;
	int free_id = -1;
	int id;

	for (id = 0; id < sw->lag_max; id++) {
		lag = prestera_lag_by_id(sw, id);
		if (lag->member_count) {
			if (lag->dev == lag_dev) {
				*lag_id = id;
				return 0;
			}
		} else if (free_id < 0) {
			free_id = id;
		}
	}
	if (free_id < 0)
		return -ENOSPC;
	*lag_id = free_id;
	return 0;
}

static struct prestera_lag *prestera_lag_create(struct prestera_switch *sw,
						struct net_device *lag_dev)
{
	struct prestera_lag *lag = NULL;
	u16 id;

	for (id = 0; id < sw->lag_max; id++) {
		lag = &sw->lags[id];
		if (!lag->dev)
			break;
	}
	if (lag) {
		INIT_LIST_HEAD(&lag->members);
		lag->dev = lag_dev;
	}

	return lag;
}

static void prestera_lag_destroy(struct prestera_switch *sw,
				 struct prestera_lag *lag)
{
	WARN_ON(!list_empty(&lag->members));
	lag->member_count = 0;
	lag->dev = NULL;
}

static int prestera_lag_port_add(struct prestera_port *port,
				 struct net_device *lag_dev)
{
	struct prestera_switch *sw = port->sw;
	struct prestera_lag *lag;
	int err;

	lag = prestera_lag_by_dev(sw, lag_dev);
	if (!lag) {
		lag = prestera_lag_create(sw, lag_dev);
		if (!lag)
			return -ENOSPC;
	}

	if (lag->member_count >= sw->lag_member_max)
		return -ENOSPC;

	err = prestera_hw_lag_member_add(port, lag->lag_id);
	if (err) {
		if (!lag->member_count)
			prestera_lag_destroy(sw, lag);
		return err;
	}

	list_add(&port->lag_member, &lag->members);
	lag->member_count++;
	port->lag = lag;

	return 0;
}

static int prestera_lag_port_del(struct prestera_port *port)
{
	struct prestera_switch *sw = port->sw;
	struct prestera_lag *lag = port->lag;
	int err;

	if (!lag || !lag->member_count)
		return -EINVAL;

	err = prestera_hw_lag_member_del(port, lag->lag_id);
	if (err)
		return err;

	list_del(&port->lag_member);
	lag->member_count--;
	port->lag = NULL;

	if (netif_is_bridge_port(lag->dev)) {
		struct net_device *br_dev;

		br_dev = netdev_master_upper_dev_get(lag->dev);

		prestera_bridge_port_leave(br_dev, port);
	}

	if (!lag->member_count)
		prestera_lag_destroy(sw, lag);

	return 0;
}

bool prestera_port_is_lag_member(const struct prestera_port *port)
{
	return !!port->lag;
}

u16 prestera_port_lag_id(const struct prestera_port *port)
{
	return port->lag->lag_id;
}

static int prestera_lag_init(struct prestera_switch *sw)
{
	u16 id;

	sw->lags = kcalloc(sw->lag_max, sizeof(*sw->lags), GFP_KERNEL);
	if (!sw->lags)
		return -ENOMEM;

	for (id = 0; id < sw->lag_max; id++)
		sw->lags[id].lag_id = id;

	return 0;
}

static void prestera_lag_fini(struct prestera_switch *sw)
{
	u8 idx;

	for (idx = 0; idx < sw->lag_max; idx++)
		WARN_ON(sw->lags[idx].member_count);

	kfree(sw->lags);
}

bool prestera_netdev_check(const struct net_device *dev)
{
	return dev->netdev_ops == &prestera_netdev_ops;
}

static int prestera_lower_dev_walk(struct net_device *dev,
				   struct netdev_nested_priv *priv)
{
	struct prestera_port **pport = (struct prestera_port **)priv->data;

	if (prestera_netdev_check(dev)) {
		*pport = netdev_priv(dev);
		return 1;
	}

	return 0;
}

struct prestera_port *prestera_port_dev_lower_find(struct net_device *dev)
{
	struct prestera_port *port = NULL;
	struct netdev_nested_priv priv = {
		.data = (void *)&port,
	};

	if (prestera_netdev_check(dev))
		return netdev_priv(dev);

	netdev_walk_all_lower_dev(dev, prestera_lower_dev_walk, &priv);

	return port;
}

static int prestera_netdev_port_lower_event(struct net_device *dev,
					    unsigned long event, void *ptr)
{
	struct netdev_notifier_changelowerstate_info *info = ptr;
	struct netdev_lag_lower_state_info *lower_state_info;
	struct prestera_port *port = netdev_priv(dev);
	bool enabled;

	if (!netif_is_lag_port(dev))
		return 0;
	if (!prestera_port_is_lag_member(port))
		return 0;

	lower_state_info = info->lower_state_info;
	enabled = lower_state_info->link_up && lower_state_info->tx_enabled;

	return prestera_hw_lag_member_enable(port, port->lag->lag_id, enabled);
}

static bool prestera_lag_master_check(struct net_device *lag_dev,
				      struct netdev_lag_upper_info *info,
				      struct netlink_ext_ack *ext_ack)
{
	if (info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
		NL_SET_ERR_MSG_MOD(ext_ack, "Unsupported LAG Tx type");
		return false;
	}

	return true;
}

static int prestera_netdev_port_event(struct net_device *lower,
				      struct net_device *dev,
				      unsigned long event, void *ptr)
{
	struct netdev_notifier_info *info = ptr;
	struct netdev_notifier_changeupper_info *cu_info;
	struct prestera_port *port = netdev_priv(dev);
	struct netlink_ext_ack *extack;
	struct net_device *upper;

	extack = netdev_notifier_info_to_extack(info);
	cu_info = container_of(info,
			       struct netdev_notifier_changeupper_info,
			       info);

	switch (event) {
	case NETDEV_PRECHANGEUPPER:
		upper = cu_info->upper_dev;
		if (!netif_is_bridge_master(upper) &&
		    !netif_is_lag_master(upper)) {
			NL_SET_ERR_MSG_MOD(extack, "Unknown upper device type");
			return -EINVAL;
		}

		if (!cu_info->linking)
			break;

		if (netdev_has_any_upper_dev(upper)) {
			NL_SET_ERR_MSG_MOD(extack, "Upper device is already enslaved");
			return -EINVAL;
		}

		if (netif_is_lag_master(upper) &&
		    !prestera_lag_master_check(upper, cu_info->upper_info, extack))
			return -EOPNOTSUPP;
		if (netif_is_lag_master(upper) && vlan_uses_dev(dev)) {
			NL_SET_ERR_MSG_MOD(extack,
					   "Master device is a LAG master and port has a VLAN");
			return -EINVAL;
		}
		if (netif_is_lag_port(dev) && is_vlan_dev(upper) &&
		    !netif_is_lag_master(vlan_dev_real_dev(upper))) {
			NL_SET_ERR_MSG_MOD(extack,
					   "Can not put a VLAN on a LAG port");
			return -EINVAL;
		}
		break;

	case NETDEV_CHANGEUPPER:
		upper = cu_info->upper_dev;
		if (netif_is_bridge_master(upper)) {
			if (cu_info->linking)
				return prestera_bridge_port_join(upper, port,
								 extack);
			else
				prestera_bridge_port_leave(upper, port);
		} else if (netif_is_lag_master(upper)) {
			if (cu_info->linking)
				return prestera_lag_port_add(port, upper);
			else
				prestera_lag_port_del(port);
		}
		break;

	case NETDEV_CHANGELOWERSTATE:
		return prestera_netdev_port_lower_event(dev, event, ptr);
	}

	return 0;
}

static int prestera_netdevice_lag_event(struct net_device *lag_dev,
					unsigned long event, void *ptr)
{
	struct net_device *dev;
	struct list_head *iter;
	int err;

	netdev_for_each_lower_dev(lag_dev, dev, iter) {
		if (prestera_netdev_check(dev)) {
			err = prestera_netdev_port_event(lag_dev, dev, event,
							 ptr);
			if (err)
				return err;
		}
	}

	return 0;
}

static int prestera_netdev_event_handler(struct notifier_block *nb,
					 unsigned long event, void *ptr)
{
	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
	int err = 0;

	if (prestera_netdev_check(dev))
		err = prestera_netdev_port_event(dev, dev, event, ptr);
	else if (netif_is_lag_master(dev))
		err = prestera_netdevice_lag_event(dev, event, ptr);

	return notifier_from_errno(err);
}

struct prestera_mdb_entry *
prestera_mdb_entry_create(struct prestera_switch *sw,
			  const unsigned char *addr, u16 vid)
{
	struct prestera_flood_domain *flood_domain;
	struct prestera_mdb_entry *mdb_entry;

	mdb_entry = kzalloc(sizeof(*mdb_entry), GFP_KERNEL);
	if (!mdb_entry)
		goto err_mdb_alloc;

	flood_domain = prestera_flood_domain_create(sw);
	if (!flood_domain)
		goto err_flood_domain_create;

	mdb_entry->sw = sw;
	mdb_entry->vid = vid;
	mdb_entry->flood_domain = flood_domain;
	ether_addr_copy(mdb_entry->addr, addr);

	if (prestera_hw_mdb_create(mdb_entry))
		goto err_mdb_hw_create;

	return mdb_entry;

err_mdb_hw_create:
	prestera_flood_domain_destroy(flood_domain);
err_flood_domain_create:
	kfree(mdb_entry);
err_mdb_alloc:
	return NULL;
}

void prestera_mdb_entry_destroy(struct prestera_mdb_entry *mdb_entry)
{
	prestera_hw_mdb_destroy(mdb_entry);
	prestera_flood_domain_destroy(mdb_entry->flood_domain);
	kfree(mdb_entry);
}

struct prestera_flood_domain *
prestera_flood_domain_create(struct prestera_switch *sw)
{
	struct prestera_flood_domain *domain;

	domain = kzalloc(sizeof(*domain), GFP_KERNEL);
	if (!domain)
		return NULL;

	domain->sw = sw;

	if (prestera_hw_flood_domain_create(domain)) {
		kfree(domain);
		return NULL;
	}

	INIT_LIST_HEAD(&domain->flood_domain_port_list);

	return domain;
}

void prestera_flood_domain_destroy(struct prestera_flood_domain *flood_domain)
{
	WARN_ON(!list_empty(&flood_domain->flood_domain_port_list));
	WARN_ON_ONCE(prestera_hw_flood_domain_destroy(flood_domain));
	kfree(flood_domain);
}

int
prestera_flood_domain_port_create(struct prestera_flood_domain *flood_domain,
				  struct net_device *dev,
				  u16 vid)
{
	struct prestera_flood_domain_port *flood_domain_port;
	bool is_first_port_in_list = false;
	int err;

	flood_domain_port = kzalloc(sizeof(*flood_domain_port), GFP_KERNEL);
	if (!flood_domain_port) {
		err = -ENOMEM;
		goto err_port_alloc;
	}

	flood_domain_port->vid = vid;

	if (list_empty(&flood_domain->flood_domain_port_list))
		is_first_port_in_list = true;

	list_add(&flood_domain_port->flood_domain_port_node,
		 &flood_domain->flood_domain_port_list);

	flood_domain_port->flood_domain = flood_domain;
	flood_domain_port->dev = dev;

	if (!is_first_port_in_list) {
		err = prestera_hw_flood_domain_ports_reset(flood_domain);
		if (err)
			goto err_prestera_mdb_port_create_hw;
	}

	err = prestera_hw_flood_domain_ports_set(flood_domain);
	if (err)
		goto err_prestera_mdb_port_create_hw;

	return 0;

err_prestera_mdb_port_create_hw:
	list_del(&flood_domain_port->flood_domain_port_node);
	kfree(flood_domain_port);
err_port_alloc:
	return err;
}

void
prestera_flood_domain_port_destroy(struct prestera_flood_domain_port *port)
{
	struct prestera_flood_domain *flood_domain = port->flood_domain;

	list_del(&port->flood_domain_port_node);

	WARN_ON_ONCE(prestera_hw_flood_domain_ports_reset(flood_domain));

	if (!list_empty(&flood_domain->flood_domain_port_list))
		WARN_ON_ONCE(prestera_hw_flood_domain_ports_set(flood_domain));

	kfree(port);
}

struct prestera_flood_domain_port *
prestera_flood_domain_port_find(struct prestera_flood_domain *flood_domain,
				struct net_device *dev, u16 vid)
{
	struct prestera_flood_domain_port *flood_domain_port;

	list_for_each_entry(flood_domain_port,
			    &flood_domain->flood_domain_port_list,
			    flood_domain_port_node)
		if (flood_domain_port->dev == dev &&
		    vid == flood_domain_port->vid)
			return flood_domain_port;

	return NULL;
}

static int prestera_netdev_event_handler_register(struct prestera_switch *sw)
{
	sw->netdev_nb.notifier_call = prestera_netdev_event_handler;

	return register_netdevice_notifier(&sw->netdev_nb);
}

static void prestera_netdev_event_handler_unregister(struct prestera_switch *sw)
{
	unregister_netdevice_notifier(&sw->netdev_nb);
}

static int prestera_switch_init(struct prestera_switch *sw)
{
	int err;

	sw->np = of_find_compatible_node(NULL, NULL, "marvell,prestera");

	err = prestera_hw_switch_init(sw);
	if (err) {
		dev_err(prestera_dev(sw), "Failed to init Switch device\n");
		return err;
	}

	rwlock_init(&sw->port_list_lock);
	INIT_LIST_HEAD(&sw->port_list);

	err = prestera_switch_set_base_mac_addr(sw);
	if (err)
		return err;

	err = prestera_netdev_event_handler_register(sw);
	if (err)
		return err;

	err = prestera_router_init(sw);
	if (err)
		goto err_router_init;

	err = prestera_switchdev_init(sw);
	if (err)
		goto err_swdev_register;

	err = prestera_rxtx_switch_init(sw);
	if (err)
		goto err_rxtx_register;

	err = prestera_event_handlers_register(sw);
	if (err)
		goto err_handlers_register;

	err = prestera_counter_init(sw);
	if (err)
		goto err_counter_init;

	err = prestera_acl_init(sw);
	if (err)
		goto err_acl_init;

	err = prestera_span_init(sw);
	if (err)
		goto err_span_init;

	err = prestera_devlink_traps_register(sw);
	if (err)
		goto err_dl_register;

	err = prestera_lag_init(sw);
	if (err)
		goto err_lag_init;

	err = prestera_create_ports(sw);
	if (err)
		goto err_ports_create;

	prestera_devlink_register(sw);
	return 0;

err_ports_create:
	prestera_lag_fini(sw);
err_lag_init:
	prestera_devlink_traps_unregister(sw);
err_dl_register:
	prestera_span_fini(sw);
err_span_init:
	prestera_acl_fini(sw);
err_acl_init:
	prestera_counter_fini(sw);
err_counter_init:
	prestera_event_handlers_unregister(sw);
err_handlers_register:
	prestera_rxtx_switch_fini(sw);
err_rxtx_register:
	prestera_switchdev_fini(sw);
err_swdev_register:
	prestera_router_fini(sw);
err_router_init:
	prestera_netdev_event_handler_unregister(sw);
	prestera_hw_switch_fini(sw);

	return err;
}

static void prestera_switch_fini(struct prestera_switch *sw)
{
	prestera_devlink_unregister(sw);
	prestera_destroy_ports(sw);
	prestera_lag_fini(sw);
	prestera_devlink_traps_unregister(sw);
	prestera_span_fini(sw);
	prestera_acl_fini(sw);
	prestera_counter_fini(sw);
	prestera_event_handlers_unregister(sw);
	prestera_rxtx_switch_fini(sw);
	prestera_switchdev_fini(sw);
	prestera_router_fini(sw);
	prestera_netdev_event_handler_unregister(sw);
	prestera_hw_switch_fini(sw);
	of_node_put(sw->np);
}

int prestera_device_register(struct prestera_device *dev)
{
	struct prestera_switch *sw;
	int err;

	sw = prestera_devlink_alloc(dev);
	if (!sw)
		return -ENOMEM;

	dev->priv = sw;
	sw->dev = dev;

	err = prestera_switch_init(sw);
	if (err) {
		prestera_devlink_free(sw);
		return err;
	}

	return 0;
}
EXPORT_SYMBOL(prestera_device_register);

void prestera_device_unregister(struct prestera_device *dev)
{
	struct prestera_switch *sw = dev->priv;

	prestera_switch_fini(sw);
	prestera_devlink_free(sw);
}
EXPORT_SYMBOL(prestera_device_unregister);

static int __init prestera_module_init(void)
{
	prestera_wq = alloc_workqueue("prestera", 0, 0);
	if (!prestera_wq)
		return -ENOMEM;

	prestera_owq = alloc_ordered_workqueue("prestera_ordered", 0);
	if (!prestera_owq) {
		destroy_workqueue(prestera_wq);
		return -ENOMEM;
	}

	return 0;
}

static void __exit prestera_module_exit(void)
{
	destroy_workqueue(prestera_wq);
	destroy_workqueue(prestera_owq);
}

module_init(prestera_module_init);
module_exit(prestera_module_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("Marvell Prestera switch driver");