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 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
|
// SPDX-License-Identifier: GPL-2.0
/* NXP C45 PTP PHY driver interface
* Copyright 2023 NXP
* Author: Radu Pirea <radu-nicolae.pirea@oss.nxp.com>
*/
#include <linux/delay.h>
#include <linux/ethtool_netlink.h>
#include <linux/kernel.h>
#include <linux/mii.h>
#include <linux/module.h>
#include <linux/phy.h>
#include <linux/processor.h>
#include <net/dst_metadata.h>
#include <net/macsec.h>
#include "nxp-c45-tja11xx.h"
#define MACSEC_REG_SIZE 32
#define TX_SC_MAX 4
#define TX_SC_BIT(secy_id) BIT(MACSEC_REG_SIZE - (secy_id) - 1)
#define VEND1_MACSEC_BASE 0x9000
#define MACSEC_CFG 0x0000
#define MACSEC_CFG_BYPASS BIT(1)
#define MACSEC_CFG_S0I BIT(0)
#define MACSEC_TPNET 0x0044
#define PN_WRAP_THRESHOLD 0xffffffff
#define MACSEC_RXSCA 0x0080
#define MACSEC_RXSCKA 0x0084
#define MACSEC_TXSCA 0x00C0
#define MACSEC_TXSCKA 0x00C4
#define MACSEC_RXSC_SCI_1H 0x0100
#define MACSEC_RXSC_CFG 0x0128
#define MACSEC_RXSC_CFG_XPN BIT(25)
#define MACSEC_RXSC_CFG_AES_256 BIT(24)
#define MACSEC_RXSC_CFG_SCI_EN BIT(11)
#define MACSEC_RXSC_CFG_RP BIT(10)
#define MACSEC_RXSC_CFG_VF_MASK GENMASK(9, 8)
#define MACSEC_RXSC_CFG_VF_OFF 8
#define MACSEC_RPW 0x012C
#define MACSEC_RXSA_A_CS 0x0180
#define MACSEC_RXSA_A_NPN 0x0184
#define MACSEC_RXSA_A_XNPN 0x0188
#define MACSEC_RXSA_A_LNPN 0x018C
#define MACSEC_RXSA_A_LXNPN 0x0190
#define MACSEC_RXSA_B_CS 0x01C0
#define MACSEC_RXSA_B_NPN 0x01C4
#define MACSEC_RXSA_B_XNPN 0x01C8
#define MACSEC_RXSA_B_LNPN 0x01CC
#define MACSEC_RXSA_B_LXNPN 0x01D0
#define MACSEC_RXSA_CS_AN_OFF 1
#define MACSEC_RXSA_CS_EN BIT(0)
#define MACSEC_TXSC_SCI_1H 0x0200
#define MACSEC_TXSC_CFG 0x0228
#define MACSEC_TXSC_CFG_XPN BIT(25)
#define MACSEC_TXSC_CFG_AES_256 BIT(24)
#define MACSEC_TXSC_CFG_AN_MASK GENMASK(19, 18)
#define MACSEC_TXSC_CFG_AN_OFF 18
#define MACSEC_TXSC_CFG_ASA BIT(17)
#define MACSEC_TXSC_CFG_SCE BIT(16)
#define MACSEC_TXSC_CFG_ENCRYPT BIT(4)
#define MACSEC_TXSC_CFG_PROTECT BIT(3)
#define MACSEC_TXSC_CFG_SEND_SCI BIT(2)
#define MACSEC_TXSC_CFG_END_STATION BIT(1)
#define MACSEC_TXSC_CFG_SCB BIT(0)
#define MACSEC_TXSA_A_CS 0x0280
#define MACSEC_TXSA_A_NPN 0x0284
#define MACSEC_TXSA_A_XNPN 0x0288
#define MACSEC_TXSA_B_CS 0x02C0
#define MACSEC_TXSA_B_NPN 0x02C4
#define MACSEC_TXSA_B_XNPN 0x02C8
#define MACSEC_SA_CS_A BIT(31)
#define MACSEC_EVR 0x0400
#define MACSEC_EVER 0x0404
#define MACSEC_RXSA_A_KA 0x0700
#define MACSEC_RXSA_A_SSCI 0x0720
#define MACSEC_RXSA_A_SALT 0x0724
#define MACSEC_RXSA_B_KA 0x0740
#define MACSEC_RXSA_B_SSCI 0x0760
#define MACSEC_RXSA_B_SALT 0x0764
#define MACSEC_TXSA_A_KA 0x0780
#define MACSEC_TXSA_A_SSCI 0x07A0
#define MACSEC_TXSA_A_SALT 0x07A4
#define MACSEC_TXSA_B_KA 0x07C0
#define MACSEC_TXSA_B_SSCI 0x07E0
#define MACSEC_TXSA_B_SALT 0x07E4
#define MACSEC_UPFR0D2 0x0A08
#define MACSEC_UPFR0M1 0x0A10
#define MACSEC_OVP BIT(12)
#define MACSEC_UPFR0M2 0x0A14
#define ETYPE_MASK 0xffff
#define MACSEC_UPFR0R 0x0A18
#define MACSEC_UPFR_EN BIT(0)
#define ADPTR_CNTRL 0x0F00
#define ADPTR_CNTRL_CONFIG_EN BIT(14)
#define ADPTR_CNTRL_ADPTR_EN BIT(12)
#define ADPTR_TX_TAG_CNTRL 0x0F0C
#define ADPTR_TX_TAG_CNTRL_ENA BIT(31)
#define TX_SC_FLT_BASE 0x800
#define TX_SC_FLT_SIZE 0x10
#define TX_FLT_BASE(flt_id) (TX_SC_FLT_BASE + \
TX_SC_FLT_SIZE * (flt_id))
#define TX_SC_FLT_OFF_MAC_DA_SA 0x04
#define TX_SC_FLT_OFF_MAC_SA 0x08
#define TX_SC_FLT_OFF_MAC_CFG 0x0C
#define TX_SC_FLT_BY_SA BIT(14)
#define TX_SC_FLT_EN BIT(8)
#define TX_SC_FLT_MAC_DA_SA(base) ((base) + TX_SC_FLT_OFF_MAC_DA_SA)
#define TX_SC_FLT_MAC_SA(base) ((base) + TX_SC_FLT_OFF_MAC_SA)
#define TX_SC_FLT_MAC_CFG(base) ((base) + TX_SC_FLT_OFF_MAC_CFG)
#define ADAPTER_EN BIT(6)
#define MACSEC_EN BIT(5)
#define MACSEC_INOV1HS 0x0140
#define MACSEC_INOV2HS 0x0144
#define MACSEC_INOD1HS 0x0148
#define MACSEC_INOD2HS 0x014C
#define MACSEC_RXSCIPUS 0x0150
#define MACSEC_RXSCIPDS 0x0154
#define MACSEC_RXSCIPLS 0x0158
#define MACSEC_RXAN0INUSS 0x0160
#define MACSEC_RXAN0IPUSS 0x0170
#define MACSEC_RXSA_A_IPOS 0x0194
#define MACSEC_RXSA_A_IPIS 0x01B0
#define MACSEC_RXSA_A_IPNVS 0x01B4
#define MACSEC_RXSA_B_IPOS 0x01D4
#define MACSEC_RXSA_B_IPIS 0x01F0
#define MACSEC_RXSA_B_IPNVS 0x01F4
#define MACSEC_OPUS 0x021C
#define MACSEC_OPTLS 0x022C
#define MACSEC_OOP1HS 0x0240
#define MACSEC_OOP2HS 0x0244
#define MACSEC_OOE1HS 0x0248
#define MACSEC_OOE2HS 0x024C
#define MACSEC_TXSA_A_OPPS 0x028C
#define MACSEC_TXSA_A_OPES 0x0290
#define MACSEC_TXSA_B_OPPS 0x02CC
#define MACSEC_TXSA_B_OPES 0x02D0
#define MACSEC_INPWTS 0x0630
#define MACSEC_INPBTS 0x0638
#define MACSEC_IPSNFS 0x063C
#define TJA11XX_TLV_TX_NEEDED_HEADROOM (32)
#define TJA11XX_TLV_NEEDED_TAILROOM (0)
#define ETH_P_TJA11XX_TLV (0x4e58)
enum nxp_c45_sa_type {
TX_SA,
RX_SA,
};
struct nxp_c45_sa {
void *sa;
const struct nxp_c45_sa_regs *regs;
enum nxp_c45_sa_type type;
bool is_key_a;
u8 an;
struct list_head list;
};
struct nxp_c45_secy {
struct macsec_secy *secy;
struct macsec_rx_sc *rx_sc;
struct list_head sa_list;
int secy_id;
bool rx_sc0_impl;
struct list_head list;
};
struct nxp_c45_macsec {
struct list_head secy_list;
DECLARE_BITMAP(secy_bitmap, TX_SC_MAX);
DECLARE_BITMAP(tx_sc_bitmap, TX_SC_MAX);
};
struct nxp_c45_sa_regs {
u16 cs;
u16 npn;
u16 xnpn;
u16 lnpn;
u16 lxnpn;
u16 ka;
u16 ssci;
u16 salt;
u16 ipis;
u16 ipnvs;
u16 ipos;
u16 opps;
u16 opes;
};
static const struct nxp_c45_sa_regs rx_sa_a_regs = {
.cs = MACSEC_RXSA_A_CS,
.npn = MACSEC_RXSA_A_NPN,
.xnpn = MACSEC_RXSA_A_XNPN,
.lnpn = MACSEC_RXSA_A_LNPN,
.lxnpn = MACSEC_RXSA_A_LXNPN,
.ka = MACSEC_RXSA_A_KA,
.ssci = MACSEC_RXSA_A_SSCI,
.salt = MACSEC_RXSA_A_SALT,
.ipis = MACSEC_RXSA_A_IPIS,
.ipnvs = MACSEC_RXSA_A_IPNVS,
.ipos = MACSEC_RXSA_A_IPOS,
};
static const struct nxp_c45_sa_regs rx_sa_b_regs = {
.cs = MACSEC_RXSA_B_CS,
.npn = MACSEC_RXSA_B_NPN,
.xnpn = MACSEC_RXSA_B_XNPN,
.lnpn = MACSEC_RXSA_B_LNPN,
.lxnpn = MACSEC_RXSA_B_LXNPN,
.ka = MACSEC_RXSA_B_KA,
.ssci = MACSEC_RXSA_B_SSCI,
.salt = MACSEC_RXSA_B_SALT,
.ipis = MACSEC_RXSA_B_IPIS,
.ipnvs = MACSEC_RXSA_B_IPNVS,
.ipos = MACSEC_RXSA_B_IPOS,
};
static const struct nxp_c45_sa_regs tx_sa_a_regs = {
.cs = MACSEC_TXSA_A_CS,
.npn = MACSEC_TXSA_A_NPN,
.xnpn = MACSEC_TXSA_A_XNPN,
.ka = MACSEC_TXSA_A_KA,
.ssci = MACSEC_TXSA_A_SSCI,
.salt = MACSEC_TXSA_A_SALT,
.opps = MACSEC_TXSA_A_OPPS,
.opes = MACSEC_TXSA_A_OPES,
};
static const struct nxp_c45_sa_regs tx_sa_b_regs = {
.cs = MACSEC_TXSA_B_CS,
.npn = MACSEC_TXSA_B_NPN,
.xnpn = MACSEC_TXSA_B_XNPN,
.ka = MACSEC_TXSA_B_KA,
.ssci = MACSEC_TXSA_B_SSCI,
.salt = MACSEC_TXSA_B_SALT,
.opps = MACSEC_TXSA_B_OPPS,
.opes = MACSEC_TXSA_B_OPES,
};
static const
struct nxp_c45_sa_regs *nxp_c45_sa_regs_get(enum nxp_c45_sa_type sa_type,
bool key_a)
{
if (sa_type == RX_SA)
if (key_a)
return &rx_sa_a_regs;
else
return &rx_sa_b_regs;
else if (sa_type == TX_SA)
if (key_a)
return &tx_sa_a_regs;
else
return &tx_sa_b_regs;
else
return NULL;
}
static int nxp_c45_macsec_write(struct phy_device *phydev, u16 addr, u32 value)
{
u32 lvalue = value;
u16 laddr;
int ret;
WARN_ON_ONCE(addr % 4);
phydev_dbg(phydev, "write addr 0x%x value 0x%x\n", addr, value);
laddr = VEND1_MACSEC_BASE + addr / 2;
ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, laddr, lvalue);
if (ret)
return ret;
laddr += 1;
lvalue >>= 16;
ret = phy_write_mmd(phydev, MDIO_MMD_VEND2, laddr, lvalue);
return ret;
}
static int nxp_c45_macsec_read(struct phy_device *phydev, u16 addr, u32 *value)
{
u32 lvalue;
u16 laddr;
int ret;
WARN_ON_ONCE(addr % 4);
laddr = VEND1_MACSEC_BASE + addr / 2;
ret = phy_read_mmd(phydev, MDIO_MMD_VEND2, laddr);
if (ret < 0)
return ret;
laddr += 1;
lvalue = (u32)ret & 0xffff;
ret = phy_read_mmd(phydev, MDIO_MMD_VEND2, laddr);
if (ret < 0)
return ret;
lvalue |= (u32)ret << 16;
*value = lvalue;
phydev_dbg(phydev, "read addr 0x%x value 0x%x\n", addr, *value);
return 0;
}
static void nxp_c45_macsec_read32_64(struct phy_device *phydev, u16 addr,
u64 *value)
{
u32 lvalue;
nxp_c45_macsec_read(phydev, addr, &lvalue);
*value = lvalue;
}
static void nxp_c45_macsec_read64(struct phy_device *phydev, u16 addr,
u64 *value)
{
u32 lvalue;
nxp_c45_macsec_read(phydev, addr, &lvalue);
*value = (u64)lvalue << 32;
nxp_c45_macsec_read(phydev, addr + 4, &lvalue);
*value |= lvalue;
}
static void nxp_c45_secy_irq_en(struct phy_device *phydev,
struct nxp_c45_secy *phy_secy, bool en)
{
u32 reg;
nxp_c45_macsec_read(phydev, MACSEC_EVER, ®);
if (en)
reg |= TX_SC_BIT(phy_secy->secy_id);
else
reg &= ~TX_SC_BIT(phy_secy->secy_id);
nxp_c45_macsec_write(phydev, MACSEC_EVER, reg);
}
static struct nxp_c45_secy *nxp_c45_find_secy(struct list_head *secy_list,
sci_t sci)
{
struct nxp_c45_secy *pos, *tmp;
list_for_each_entry_safe(pos, tmp, secy_list, list)
if (pos->secy->sci == sci)
return pos;
return ERR_PTR(-EINVAL);
}
static struct
nxp_c45_secy *nxp_c45_find_secy_by_id(struct list_head *secy_list,
int id)
{
struct nxp_c45_secy *pos, *tmp;
list_for_each_entry_safe(pos, tmp, secy_list, list)
if (pos->secy_id == id)
return pos;
return ERR_PTR(-EINVAL);
}
static void nxp_c45_secy_free(struct nxp_c45_secy *phy_secy)
{
list_del(&phy_secy->list);
kfree(phy_secy);
}
static struct nxp_c45_sa *nxp_c45_find_sa(struct list_head *sa_list,
enum nxp_c45_sa_type sa_type, u8 an)
{
struct nxp_c45_sa *pos, *tmp;
list_for_each_entry_safe(pos, tmp, sa_list, list)
if (pos->an == an && pos->type == sa_type)
return pos;
return ERR_PTR(-EINVAL);
}
static struct nxp_c45_sa *nxp_c45_sa_alloc(struct list_head *sa_list, void *sa,
enum nxp_c45_sa_type sa_type, u8 an)
{
struct nxp_c45_sa *first = NULL, *pos, *tmp;
int occurrences = 0;
list_for_each_entry_safe(pos, tmp, sa_list, list) {
if (pos->type != sa_type)
continue;
if (pos->an == an)
return ERR_PTR(-EINVAL);
first = pos;
occurrences++;
if (occurrences >= 2)
return ERR_PTR(-ENOSPC);
}
tmp = kzalloc(sizeof(*tmp), GFP_KERNEL);
if (!tmp)
return ERR_PTR(-ENOMEM);
if (first)
tmp->is_key_a = !first->is_key_a;
else
tmp->is_key_a = true;
tmp->sa = sa;
tmp->type = sa_type;
tmp->an = an;
tmp->regs = nxp_c45_sa_regs_get(tmp->type, tmp->is_key_a);
list_add_tail(&tmp->list, sa_list);
return tmp;
}
static void nxp_c45_sa_free(struct nxp_c45_sa *sa)
{
list_del(&sa->list);
kfree(sa);
}
static void nxp_c45_sa_list_free(struct list_head *sa_list)
{
struct nxp_c45_sa *pos, *tmp;
list_for_each_entry_safe(pos, tmp, sa_list, list)
nxp_c45_sa_free(pos);
}
static void nxp_c45_sa_set_pn(struct phy_device *phydev,
struct nxp_c45_sa *sa, u64 pn,
u32 replay_window)
{
const struct nxp_c45_sa_regs *sa_regs = sa->regs;
pn_t npn = {.full64 = pn};
pn_t lnpn;
nxp_c45_macsec_write(phydev, sa_regs->npn, npn.lower);
nxp_c45_macsec_write(phydev, sa_regs->xnpn, npn.upper);
if (sa->type != RX_SA)
return;
if (pn > replay_window)
lnpn.full64 = pn - replay_window;
else
lnpn.full64 = 1;
nxp_c45_macsec_write(phydev, sa_regs->lnpn, lnpn.lower);
nxp_c45_macsec_write(phydev, sa_regs->lxnpn, lnpn.upper);
}
static void nxp_c45_sa_set_key(struct macsec_context *ctx,
const struct nxp_c45_sa_regs *sa_regs,
u8 *salt, ssci_t ssci)
{
struct phy_device *phydev = ctx->phydev;
u32 key_size = ctx->secy->key_len / 4;
u32 salt_size = MACSEC_SALT_LEN / 4;
u32 *key_u32 = (u32 *)ctx->sa.key;
u32 *salt_u32 = (u32 *)salt;
u32 reg, value;
int i;
for (i = 0; i < key_size; i++) {
reg = sa_regs->ka + i * 4;
value = (__force u32)cpu_to_be32(key_u32[i]);
nxp_c45_macsec_write(phydev, reg, value);
}
if (ctx->secy->xpn) {
for (i = 0; i < salt_size; i++) {
reg = sa_regs->salt + (2 - i) * 4;
value = (__force u32)cpu_to_be32(salt_u32[i]);
nxp_c45_macsec_write(phydev, reg, value);
}
value = (__force u32)cpu_to_be32((__force u32)ssci);
nxp_c45_macsec_write(phydev, sa_regs->ssci, value);
}
nxp_c45_macsec_write(phydev, sa_regs->cs, MACSEC_SA_CS_A);
}
static void nxp_c45_rx_sa_clear_stats(struct phy_device *phydev,
struct nxp_c45_sa *sa)
{
nxp_c45_macsec_write(phydev, sa->regs->ipis, 0);
nxp_c45_macsec_write(phydev, sa->regs->ipnvs, 0);
nxp_c45_macsec_write(phydev, sa->regs->ipos, 0);
nxp_c45_macsec_write(phydev, MACSEC_RXAN0INUSS + sa->an * 4, 0);
nxp_c45_macsec_write(phydev, MACSEC_RXAN0IPUSS + sa->an * 4, 0);
}
static void nxp_c45_rx_sa_read_stats(struct phy_device *phydev,
struct nxp_c45_sa *sa,
struct macsec_rx_sa_stats *stats)
{
nxp_c45_macsec_read(phydev, sa->regs->ipis, &stats->InPktsInvalid);
nxp_c45_macsec_read(phydev, sa->regs->ipnvs, &stats->InPktsNotValid);
nxp_c45_macsec_read(phydev, sa->regs->ipos, &stats->InPktsOK);
}
static void nxp_c45_tx_sa_clear_stats(struct phy_device *phydev,
struct nxp_c45_sa *sa)
{
nxp_c45_macsec_write(phydev, sa->regs->opps, 0);
nxp_c45_macsec_write(phydev, sa->regs->opes, 0);
}
static void nxp_c45_tx_sa_read_stats(struct phy_device *phydev,
struct nxp_c45_sa *sa,
struct macsec_tx_sa_stats *stats)
{
nxp_c45_macsec_read(phydev, sa->regs->opps, &stats->OutPktsProtected);
nxp_c45_macsec_read(phydev, sa->regs->opes, &stats->OutPktsEncrypted);
}
static void nxp_c45_rx_sa_update(struct phy_device *phydev,
struct nxp_c45_sa *sa, bool en)
{
const struct nxp_c45_sa_regs *sa_regs = sa->regs;
u32 cfg;
cfg = sa->an << MACSEC_RXSA_CS_AN_OFF;
cfg |= en ? MACSEC_RXSA_CS_EN : 0;
nxp_c45_macsec_write(phydev, sa_regs->cs, cfg);
}
static void nxp_c45_tx_sa_update(struct phy_device *phydev,
struct nxp_c45_sa *sa, bool en)
{
u32 cfg = 0;
nxp_c45_macsec_read(phydev, MACSEC_TXSC_CFG, &cfg);
cfg &= ~MACSEC_TXSC_CFG_AN_MASK;
cfg |= sa->an << MACSEC_TXSC_CFG_AN_OFF;
if (sa->is_key_a)
cfg &= ~MACSEC_TXSC_CFG_ASA;
else
cfg |= MACSEC_TXSC_CFG_ASA;
if (en)
cfg |= MACSEC_TXSC_CFG_SCE;
else
cfg &= ~MACSEC_TXSC_CFG_SCE;
nxp_c45_macsec_write(phydev, MACSEC_TXSC_CFG, cfg);
}
static void nxp_c45_set_sci(struct phy_device *phydev, u16 sci_base_addr,
sci_t sci)
{
u64 lsci = sci_to_cpu(sci);
nxp_c45_macsec_write(phydev, sci_base_addr, lsci >> 32);
nxp_c45_macsec_write(phydev, sci_base_addr + 4, lsci);
}
static bool nxp_c45_port_is_1(sci_t sci)
{
u16 port = sci_to_cpu(sci);
return port == 1;
}
static void nxp_c45_select_secy(struct phy_device *phydev, u8 id)
{
nxp_c45_macsec_write(phydev, MACSEC_RXSCA, id);
nxp_c45_macsec_write(phydev, MACSEC_RXSCKA, id);
nxp_c45_macsec_write(phydev, MACSEC_TXSCA, id);
nxp_c45_macsec_write(phydev, MACSEC_TXSCKA, id);
}
static bool nxp_c45_secy_valid(struct nxp_c45_secy *phy_secy,
bool can_rx_sc0_impl)
{
bool end_station = phy_secy->secy->tx_sc.end_station;
bool scb = phy_secy->secy->tx_sc.scb;
phy_secy->rx_sc0_impl = false;
if (end_station) {
if (!nxp_c45_port_is_1(phy_secy->secy->sci))
return false;
if (!phy_secy->rx_sc)
return true;
return nxp_c45_port_is_1(phy_secy->rx_sc->sci);
}
if (scb)
return false;
if (!can_rx_sc0_impl)
return false;
if (phy_secy->secy_id != 0)
return false;
phy_secy->rx_sc0_impl = true;
return true;
}
static bool nxp_c45_rx_sc0_impl(struct nxp_c45_secy *phy_secy)
{
bool end_station = phy_secy->secy->tx_sc.end_station;
bool send_sci = phy_secy->secy->tx_sc.send_sci;
bool scb = phy_secy->secy->tx_sc.scb;
return !end_station && !send_sci && !scb;
}
static bool nxp_c45_mac_addr_free(struct macsec_context *ctx)
{
struct nxp_c45_phy *priv = ctx->phydev->priv;
struct nxp_c45_secy *pos, *tmp;
list_for_each_entry_safe(pos, tmp, &priv->macsec->secy_list, list) {
if (pos->secy == ctx->secy)
continue;
if (memcmp(pos->secy->netdev->dev_addr,
ctx->secy->netdev->dev_addr, ETH_ALEN) == 0)
return false;
}
return true;
}
static void nxp_c45_tx_sc_en_flt(struct phy_device *phydev, int secy_id,
bool en)
{
u32 tx_flt_base = TX_FLT_BASE(secy_id);
u32 reg = 0;
nxp_c45_macsec_read(phydev, TX_SC_FLT_MAC_CFG(tx_flt_base), ®);
if (en)
reg |= TX_SC_FLT_EN;
else
reg &= ~TX_SC_FLT_EN;
nxp_c45_macsec_write(phydev, TX_SC_FLT_MAC_CFG(tx_flt_base), reg);
}
static void nxp_c45_tx_sc_set_flt(struct phy_device *phydev,
struct nxp_c45_secy *phy_secy)
{
const u8 *dev_addr = phy_secy->secy->netdev->dev_addr;
u32 tx_flt_base = TX_FLT_BASE(phy_secy->secy_id);
u32 reg;
reg = dev_addr[0] << 8 | dev_addr[1];
nxp_c45_macsec_write(phydev, TX_SC_FLT_MAC_DA_SA(tx_flt_base), reg);
reg = dev_addr[5] | dev_addr[4] << 8 | dev_addr[3] << 16 |
dev_addr[2] << 24;
nxp_c45_macsec_write(phydev, TX_SC_FLT_MAC_SA(tx_flt_base), reg);
nxp_c45_macsec_read(phydev, TX_SC_FLT_MAC_CFG(tx_flt_base), ®);
reg &= TX_SC_FLT_EN;
reg |= TX_SC_FLT_BY_SA | phy_secy->secy_id;
nxp_c45_macsec_write(phydev, TX_SC_FLT_MAC_CFG(tx_flt_base), reg);
}
static void nxp_c45_tx_sc_update(struct phy_device *phydev,
struct nxp_c45_secy *phy_secy)
{
u32 cfg = 0;
nxp_c45_macsec_read(phydev, MACSEC_TXSC_CFG, &cfg);
phydev_dbg(phydev, "XPN %s\n", phy_secy->secy->xpn ? "on" : "off");
if (phy_secy->secy->xpn)
cfg |= MACSEC_TXSC_CFG_XPN;
else
cfg &= ~MACSEC_TXSC_CFG_XPN;
phydev_dbg(phydev, "key len %u\n", phy_secy->secy->key_len);
if (phy_secy->secy->key_len == 32)
cfg |= MACSEC_TXSC_CFG_AES_256;
else
cfg &= ~MACSEC_TXSC_CFG_AES_256;
phydev_dbg(phydev, "encryption %s\n",
phy_secy->secy->tx_sc.encrypt ? "on" : "off");
if (phy_secy->secy->tx_sc.encrypt)
cfg |= MACSEC_TXSC_CFG_ENCRYPT;
else
cfg &= ~MACSEC_TXSC_CFG_ENCRYPT;
phydev_dbg(phydev, "protect frames %s\n",
phy_secy->secy->protect_frames ? "on" : "off");
if (phy_secy->secy->protect_frames)
cfg |= MACSEC_TXSC_CFG_PROTECT;
else
cfg &= ~MACSEC_TXSC_CFG_PROTECT;
phydev_dbg(phydev, "send sci %s\n",
phy_secy->secy->tx_sc.send_sci ? "on" : "off");
if (phy_secy->secy->tx_sc.send_sci)
cfg |= MACSEC_TXSC_CFG_SEND_SCI;
else
cfg &= ~MACSEC_TXSC_CFG_SEND_SCI;
phydev_dbg(phydev, "end station %s\n",
phy_secy->secy->tx_sc.end_station ? "on" : "off");
if (phy_secy->secy->tx_sc.end_station)
cfg |= MACSEC_TXSC_CFG_END_STATION;
else
cfg &= ~MACSEC_TXSC_CFG_END_STATION;
phydev_dbg(phydev, "scb %s\n",
phy_secy->secy->tx_sc.scb ? "on" : "off");
if (phy_secy->secy->tx_sc.scb)
cfg |= MACSEC_TXSC_CFG_SCB;
else
cfg &= ~MACSEC_TXSC_CFG_SCB;
nxp_c45_macsec_write(phydev, MACSEC_TXSC_CFG, cfg);
}
static void nxp_c45_tx_sc_clear_stats(struct phy_device *phydev,
struct nxp_c45_secy *phy_secy)
{
struct nxp_c45_sa *pos, *tmp;
list_for_each_entry_safe(pos, tmp, &phy_secy->sa_list, list)
if (pos->type == TX_SA)
nxp_c45_tx_sa_clear_stats(phydev, pos);
nxp_c45_macsec_write(phydev, MACSEC_OPUS, 0);
nxp_c45_macsec_write(phydev, MACSEC_OPTLS, 0);
nxp_c45_macsec_write(phydev, MACSEC_OOP1HS, 0);
nxp_c45_macsec_write(phydev, MACSEC_OOP2HS, 0);
nxp_c45_macsec_write(phydev, MACSEC_OOE1HS, 0);
nxp_c45_macsec_write(phydev, MACSEC_OOE2HS, 0);
}
static void nxp_c45_set_rx_sc0_impl(struct phy_device *phydev,
bool enable)
{
u32 reg = 0;
nxp_c45_macsec_read(phydev, MACSEC_CFG, ®);
if (enable)
reg |= MACSEC_CFG_S0I;
else
reg &= ~MACSEC_CFG_S0I;
nxp_c45_macsec_write(phydev, MACSEC_CFG, reg);
}
static bool nxp_c45_is_rx_sc0_impl(struct list_head *secy_list)
{
struct nxp_c45_secy *pos, *tmp;
list_for_each_entry_safe(pos, tmp, secy_list, list)
if (pos->rx_sc0_impl)
return pos->rx_sc0_impl;
return false;
}
static void nxp_c45_rx_sc_en(struct phy_device *phydev,
struct macsec_rx_sc *rx_sc, bool en)
{
u32 reg = 0;
nxp_c45_macsec_read(phydev, MACSEC_RXSC_CFG, ®);
if (rx_sc->active && en)
reg |= MACSEC_RXSC_CFG_SCI_EN;
else
reg &= ~MACSEC_RXSC_CFG_SCI_EN;
nxp_c45_macsec_write(phydev, MACSEC_RXSC_CFG, reg);
}
static void nxp_c45_rx_sc_update(struct phy_device *phydev,
struct nxp_c45_secy *phy_secy)
{
struct macsec_rx_sc *rx_sc = phy_secy->rx_sc;
struct nxp_c45_phy *priv = phydev->priv;
u32 cfg = 0;
nxp_c45_macsec_read(phydev, MACSEC_RXSC_CFG, &cfg);
cfg &= ~MACSEC_RXSC_CFG_VF_MASK;
cfg = phy_secy->secy->validate_frames << MACSEC_RXSC_CFG_VF_OFF;
phydev_dbg(phydev, "validate frames %u\n",
phy_secy->secy->validate_frames);
phydev_dbg(phydev, "replay_protect %s window %u\n",
phy_secy->secy->replay_protect ? "on" : "off",
phy_secy->secy->replay_window);
if (phy_secy->secy->replay_protect) {
cfg |= MACSEC_RXSC_CFG_RP;
nxp_c45_macsec_write(phydev, MACSEC_RPW,
phy_secy->secy->replay_window);
} else {
cfg &= ~MACSEC_RXSC_CFG_RP;
}
phydev_dbg(phydev, "rx_sc->active %s\n",
rx_sc->active ? "on" : "off");
if (rx_sc->active &&
test_bit(phy_secy->secy_id, priv->macsec->secy_bitmap))
cfg |= MACSEC_RXSC_CFG_SCI_EN;
else
cfg &= ~MACSEC_RXSC_CFG_SCI_EN;
phydev_dbg(phydev, "key len %u\n", phy_secy->secy->key_len);
if (phy_secy->secy->key_len == 32)
cfg |= MACSEC_RXSC_CFG_AES_256;
else
cfg &= ~MACSEC_RXSC_CFG_AES_256;
phydev_dbg(phydev, "XPN %s\n", phy_secy->secy->xpn ? "on" : "off");
if (phy_secy->secy->xpn)
cfg |= MACSEC_RXSC_CFG_XPN;
else
cfg &= ~MACSEC_RXSC_CFG_XPN;
nxp_c45_macsec_write(phydev, MACSEC_RXSC_CFG, cfg);
}
static void nxp_c45_rx_sc_clear_stats(struct phy_device *phydev,
struct nxp_c45_secy *phy_secy)
{
struct nxp_c45_sa *pos, *tmp;
int i;
list_for_each_entry_safe(pos, tmp, &phy_secy->sa_list, list)
if (pos->type == RX_SA)
nxp_c45_rx_sa_clear_stats(phydev, pos);
nxp_c45_macsec_write(phydev, MACSEC_INOD1HS, 0);
nxp_c45_macsec_write(phydev, MACSEC_INOD2HS, 0);
nxp_c45_macsec_write(phydev, MACSEC_INOV1HS, 0);
nxp_c45_macsec_write(phydev, MACSEC_INOV2HS, 0);
nxp_c45_macsec_write(phydev, MACSEC_RXSCIPDS, 0);
nxp_c45_macsec_write(phydev, MACSEC_RXSCIPLS, 0);
nxp_c45_macsec_write(phydev, MACSEC_RXSCIPUS, 0);
for (i = 0; i < MACSEC_NUM_AN; i++) {
nxp_c45_macsec_write(phydev, MACSEC_RXAN0INUSS + i * 4, 0);
nxp_c45_macsec_write(phydev, MACSEC_RXAN0IPUSS + i * 4, 0);
}
}
static void nxp_c45_rx_sc_del(struct phy_device *phydev,
struct nxp_c45_secy *phy_secy)
{
struct nxp_c45_sa *pos, *tmp;
nxp_c45_macsec_write(phydev, MACSEC_RXSC_CFG, 0);
nxp_c45_macsec_write(phydev, MACSEC_RPW, 0);
nxp_c45_set_sci(phydev, MACSEC_RXSC_SCI_1H, 0);
nxp_c45_rx_sc_clear_stats(phydev, phy_secy);
list_for_each_entry_safe(pos, tmp, &phy_secy->sa_list, list) {
if (pos->type == RX_SA) {
nxp_c45_rx_sa_update(phydev, pos, false);
nxp_c45_sa_free(pos);
}
}
}
static void nxp_c45_clear_global_stats(struct phy_device *phydev)
{
nxp_c45_macsec_write(phydev, MACSEC_INPBTS, 0);
nxp_c45_macsec_write(phydev, MACSEC_INPWTS, 0);
nxp_c45_macsec_write(phydev, MACSEC_IPSNFS, 0);
}
static void nxp_c45_macsec_en(struct phy_device *phydev, bool en)
{
u32 reg;
nxp_c45_macsec_read(phydev, MACSEC_CFG, ®);
if (en)
reg |= MACSEC_CFG_BYPASS;
else
reg &= ~MACSEC_CFG_BYPASS;
nxp_c45_macsec_write(phydev, MACSEC_CFG, reg);
}
static int nxp_c45_mdo_dev_open(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
int any_bit_set;
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_tx_sc_en_flt(phydev, phy_secy->secy_id, true);
nxp_c45_set_rx_sc0_impl(phydev, phy_secy->rx_sc0_impl);
if (phy_secy->rx_sc)
nxp_c45_rx_sc_en(phydev, phy_secy->rx_sc, true);
any_bit_set = find_first_bit(priv->macsec->secy_bitmap, TX_SC_MAX);
if (any_bit_set == TX_SC_MAX)
nxp_c45_macsec_en(phydev, true);
set_bit(phy_secy->secy_id, priv->macsec->secy_bitmap);
return 0;
}
static int nxp_c45_mdo_dev_stop(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
int any_bit_set;
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_tx_sc_en_flt(phydev, phy_secy->secy_id, false);
if (phy_secy->rx_sc)
nxp_c45_rx_sc_en(phydev, phy_secy->rx_sc, false);
nxp_c45_set_rx_sc0_impl(phydev, false);
clear_bit(phy_secy->secy_id, priv->macsec->secy_bitmap);
any_bit_set = find_first_bit(priv->macsec->secy_bitmap, TX_SC_MAX);
if (any_bit_set == TX_SC_MAX)
nxp_c45_macsec_en(phydev, false);
return 0;
}
static int nxp_c45_mdo_add_secy(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
bool can_rx_sc0_impl;
int idx;
phydev_dbg(phydev, "add SecY SCI %016llx\n",
sci_to_cpu(ctx->secy->sci));
if (!nxp_c45_mac_addr_free(ctx))
return -EBUSY;
if (nxp_c45_is_rx_sc0_impl(&priv->macsec->secy_list))
return -EBUSY;
idx = find_first_zero_bit(priv->macsec->tx_sc_bitmap, TX_SC_MAX);
if (idx == TX_SC_MAX)
return -ENOSPC;
phy_secy = kzalloc(sizeof(*phy_secy), GFP_KERNEL);
if (!phy_secy)
return -ENOMEM;
INIT_LIST_HEAD(&phy_secy->sa_list);
phy_secy->secy = ctx->secy;
phy_secy->secy_id = idx;
/* If the point to point mode should be enabled, we should have no
* SecY added yet.
*/
can_rx_sc0_impl = list_count_nodes(&priv->macsec->secy_list) == 0;
if (!nxp_c45_secy_valid(phy_secy, can_rx_sc0_impl)) {
kfree(phy_secy);
return -EINVAL;
}
phy_secy->rx_sc0_impl = nxp_c45_rx_sc0_impl(phy_secy);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_set_sci(phydev, MACSEC_TXSC_SCI_1H, ctx->secy->sci);
nxp_c45_tx_sc_set_flt(phydev, phy_secy);
nxp_c45_tx_sc_update(phydev, phy_secy);
if (phy_interrupt_is_valid(phydev))
nxp_c45_secy_irq_en(phydev, phy_secy, true);
set_bit(idx, priv->macsec->tx_sc_bitmap);
list_add_tail(&phy_secy->list, &priv->macsec->secy_list);
return 0;
}
static void nxp_c45_tx_sa_next(struct nxp_c45_secy *phy_secy,
struct nxp_c45_sa *next_sa, u8 encoding_sa)
{
struct nxp_c45_sa *sa;
sa = nxp_c45_find_sa(&phy_secy->sa_list, TX_SA, encoding_sa);
if (!IS_ERR(sa)) {
memcpy(next_sa, sa, sizeof(*sa));
} else {
next_sa->is_key_a = true;
next_sa->an = encoding_sa;
}
}
static int nxp_c45_mdo_upd_secy(struct macsec_context *ctx)
{
u8 encoding_sa = ctx->secy->tx_sc.encoding_sa;
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
struct nxp_c45_sa next_sa;
bool can_rx_sc0_impl;
phydev_dbg(phydev, "update SecY SCI %016llx\n",
sci_to_cpu(ctx->secy->sci));
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
if (!nxp_c45_mac_addr_free(ctx))
return -EBUSY;
/* If the point to point mode should be enabled, we should have only
* one SecY added, respectively the updated one.
*/
can_rx_sc0_impl = list_count_nodes(&priv->macsec->secy_list) == 1;
if (!nxp_c45_secy_valid(phy_secy, can_rx_sc0_impl))
return -EINVAL;
phy_secy->rx_sc0_impl = nxp_c45_rx_sc0_impl(phy_secy);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_tx_sc_set_flt(phydev, phy_secy);
nxp_c45_tx_sc_update(phydev, phy_secy);
nxp_c45_tx_sa_next(phy_secy, &next_sa, encoding_sa);
nxp_c45_tx_sa_update(phydev, &next_sa, ctx->secy->operational);
nxp_c45_set_rx_sc0_impl(phydev, phy_secy->rx_sc0_impl);
if (phy_secy->rx_sc)
nxp_c45_rx_sc_update(phydev, phy_secy);
return 0;
}
static int nxp_c45_mdo_del_secy(struct macsec_context *ctx)
{
u8 encoding_sa = ctx->secy->tx_sc.encoding_sa;
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
struct nxp_c45_sa next_sa;
phydev_dbg(phydev, "delete SecY SCI %016llx\n",
sci_to_cpu(ctx->secy->sci));
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_mdo_dev_stop(ctx);
nxp_c45_tx_sa_next(phy_secy, &next_sa, encoding_sa);
nxp_c45_tx_sa_update(phydev, &next_sa, false);
nxp_c45_tx_sc_clear_stats(phydev, phy_secy);
if (phy_secy->rx_sc)
nxp_c45_rx_sc_del(phydev, phy_secy);
nxp_c45_sa_list_free(&phy_secy->sa_list);
if (phy_interrupt_is_valid(phydev))
nxp_c45_secy_irq_en(phydev, phy_secy, false);
clear_bit(phy_secy->secy_id, priv->macsec->tx_sc_bitmap);
nxp_c45_secy_free(phy_secy);
if (list_empty(&priv->macsec->secy_list))
nxp_c45_clear_global_stats(phydev);
return 0;
}
static int nxp_c45_mdo_add_rxsc(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
phydev_dbg(phydev, "add RX SC SCI %016llx %s\n",
sci_to_cpu(ctx->rx_sc->sci),
ctx->rx_sc->active ? "enabled" : "disabled");
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
if (phy_secy->rx_sc)
return -ENOSPC;
if (phy_secy->secy->tx_sc.end_station &&
!nxp_c45_port_is_1(ctx->rx_sc->sci))
return -EINVAL;
phy_secy->rx_sc = ctx->rx_sc;
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_set_sci(phydev, MACSEC_RXSC_SCI_1H, ctx->rx_sc->sci);
nxp_c45_rx_sc_update(phydev, phy_secy);
return 0;
}
static int nxp_c45_mdo_upd_rxsc(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
phydev_dbg(phydev, "update RX SC SCI %016llx %s\n",
sci_to_cpu(ctx->rx_sc->sci),
ctx->rx_sc->active ? "enabled" : "disabled");
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_rx_sc_update(phydev, phy_secy);
return 0;
}
static int nxp_c45_mdo_del_rxsc(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
phydev_dbg(phydev, "delete RX SC SCI %016llx %s\n",
sci_to_cpu(ctx->rx_sc->sci),
ctx->rx_sc->active ? "enabled" : "disabled");
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_rx_sc_del(phydev, phy_secy);
phy_secy->rx_sc = NULL;
return 0;
}
static int nxp_c45_mdo_add_rxsa(struct macsec_context *ctx)
{
struct macsec_rx_sa *rx_sa = ctx->sa.rx_sa;
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
u8 an = ctx->sa.assoc_num;
struct nxp_c45_sa *sa;
phydev_dbg(phydev, "add RX SA %u %s to RX SC SCI %016llx\n",
an, rx_sa->active ? "enabled" : "disabled",
sci_to_cpu(rx_sa->sc->sci));
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
sa = nxp_c45_sa_alloc(&phy_secy->sa_list, rx_sa, RX_SA, an);
if (IS_ERR(sa))
return PTR_ERR(sa);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_sa_set_pn(phydev, sa, rx_sa->next_pn,
ctx->secy->replay_window);
nxp_c45_sa_set_key(ctx, sa->regs, rx_sa->key.salt.bytes, rx_sa->ssci);
nxp_c45_rx_sa_update(phydev, sa, rx_sa->active);
return 0;
}
static int nxp_c45_mdo_upd_rxsa(struct macsec_context *ctx)
{
struct macsec_rx_sa *rx_sa = ctx->sa.rx_sa;
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
u8 an = ctx->sa.assoc_num;
struct nxp_c45_sa *sa;
phydev_dbg(phydev, "update RX SA %u %s to RX SC SCI %016llx\n",
an, rx_sa->active ? "enabled" : "disabled",
sci_to_cpu(rx_sa->sc->sci));
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
sa = nxp_c45_find_sa(&phy_secy->sa_list, RX_SA, an);
if (IS_ERR(sa))
return PTR_ERR(sa);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
if (ctx->sa.update_pn)
nxp_c45_sa_set_pn(phydev, sa, rx_sa->next_pn,
ctx->secy->replay_window);
nxp_c45_rx_sa_update(phydev, sa, rx_sa->active);
return 0;
}
static int nxp_c45_mdo_del_rxsa(struct macsec_context *ctx)
{
struct macsec_rx_sa *rx_sa = ctx->sa.rx_sa;
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
u8 an = ctx->sa.assoc_num;
struct nxp_c45_sa *sa;
phydev_dbg(phydev, "delete RX SA %u %s to RX SC SCI %016llx\n",
an, rx_sa->active ? "enabled" : "disabled",
sci_to_cpu(rx_sa->sc->sci));
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
sa = nxp_c45_find_sa(&phy_secy->sa_list, RX_SA, an);
if (IS_ERR(sa))
return PTR_ERR(sa);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_rx_sa_update(phydev, sa, false);
nxp_c45_rx_sa_clear_stats(phydev, sa);
nxp_c45_sa_free(sa);
return 0;
}
static int nxp_c45_mdo_add_txsa(struct macsec_context *ctx)
{
struct macsec_tx_sa *tx_sa = ctx->sa.tx_sa;
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
u8 an = ctx->sa.assoc_num;
struct nxp_c45_sa *sa;
phydev_dbg(phydev, "add TX SA %u %s to TX SC %016llx\n",
an, ctx->sa.tx_sa->active ? "enabled" : "disabled",
sci_to_cpu(ctx->secy->sci));
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
sa = nxp_c45_sa_alloc(&phy_secy->sa_list, tx_sa, TX_SA, an);
if (IS_ERR(sa))
return PTR_ERR(sa);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_sa_set_pn(phydev, sa, tx_sa->next_pn, 0);
nxp_c45_sa_set_key(ctx, sa->regs, tx_sa->key.salt.bytes, tx_sa->ssci);
if (ctx->secy->tx_sc.encoding_sa == sa->an)
nxp_c45_tx_sa_update(phydev, sa, tx_sa->active);
return 0;
}
static int nxp_c45_mdo_upd_txsa(struct macsec_context *ctx)
{
struct macsec_tx_sa *tx_sa = ctx->sa.tx_sa;
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
u8 an = ctx->sa.assoc_num;
struct nxp_c45_sa *sa;
phydev_dbg(phydev, "update TX SA %u %s to TX SC %016llx\n",
an, ctx->sa.tx_sa->active ? "enabled" : "disabled",
sci_to_cpu(ctx->secy->sci));
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
sa = nxp_c45_find_sa(&phy_secy->sa_list, TX_SA, an);
if (IS_ERR(sa))
return PTR_ERR(sa);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
if (ctx->sa.update_pn)
nxp_c45_sa_set_pn(phydev, sa, tx_sa->next_pn, 0);
if (ctx->secy->tx_sc.encoding_sa == sa->an)
nxp_c45_tx_sa_update(phydev, sa, tx_sa->active);
return 0;
}
static int nxp_c45_mdo_del_txsa(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *phy_secy;
u8 an = ctx->sa.assoc_num;
struct nxp_c45_sa *sa;
phydev_dbg(phydev, "delete TX SA %u %s to TX SC %016llx\n",
an, ctx->sa.tx_sa->active ? "enabled" : "disabled",
sci_to_cpu(ctx->secy->sci));
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
sa = nxp_c45_find_sa(&phy_secy->sa_list, TX_SA, an);
if (IS_ERR(sa))
return PTR_ERR(sa);
nxp_c45_select_secy(phydev, phy_secy->secy_id);
if (ctx->secy->tx_sc.encoding_sa == sa->an)
nxp_c45_tx_sa_update(phydev, sa, false);
nxp_c45_tx_sa_clear_stats(phydev, sa);
nxp_c45_sa_free(sa);
return 0;
}
static int nxp_c45_mdo_get_dev_stats(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct macsec_dev_stats *dev_stats;
struct nxp_c45_secy *phy_secy;
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
dev_stats = ctx->stats.dev_stats;
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_macsec_read32_64(phydev, MACSEC_OPUS,
&dev_stats->OutPktsUntagged);
nxp_c45_macsec_read32_64(phydev, MACSEC_OPTLS,
&dev_stats->OutPktsTooLong);
nxp_c45_macsec_read32_64(phydev, MACSEC_INPBTS,
&dev_stats->InPktsBadTag);
if (phy_secy->secy->validate_frames == MACSEC_VALIDATE_STRICT)
nxp_c45_macsec_read32_64(phydev, MACSEC_INPWTS,
&dev_stats->InPktsNoTag);
else
nxp_c45_macsec_read32_64(phydev, MACSEC_INPWTS,
&dev_stats->InPktsUntagged);
if (phy_secy->secy->validate_frames == MACSEC_VALIDATE_STRICT)
nxp_c45_macsec_read32_64(phydev, MACSEC_IPSNFS,
&dev_stats->InPktsNoSCI);
else
nxp_c45_macsec_read32_64(phydev, MACSEC_IPSNFS,
&dev_stats->InPktsUnknownSCI);
/* Always 0. */
dev_stats->InPktsOverrun = 0;
return 0;
}
static int nxp_c45_mdo_get_tx_sc_stats(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct macsec_tx_sa_stats tx_sa_stats;
struct macsec_tx_sc_stats *stats;
struct nxp_c45_secy *phy_secy;
struct nxp_c45_sa *pos, *tmp;
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
stats = ctx->stats.tx_sc_stats;
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_macsec_read64(phydev, MACSEC_OOE1HS,
&stats->OutOctetsEncrypted);
nxp_c45_macsec_read64(phydev, MACSEC_OOP1HS,
&stats->OutOctetsProtected);
list_for_each_entry_safe(pos, tmp, &phy_secy->sa_list, list) {
if (pos->type != TX_SA)
continue;
memset(&tx_sa_stats, 0, sizeof(tx_sa_stats));
nxp_c45_tx_sa_read_stats(phydev, pos, &tx_sa_stats);
stats->OutPktsEncrypted += tx_sa_stats.OutPktsEncrypted;
stats->OutPktsProtected += tx_sa_stats.OutPktsProtected;
}
return 0;
}
static int nxp_c45_mdo_get_tx_sa_stats(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct macsec_tx_sa_stats *stats;
struct nxp_c45_secy *phy_secy;
u8 an = ctx->sa.assoc_num;
struct nxp_c45_sa *sa;
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
sa = nxp_c45_find_sa(&phy_secy->sa_list, TX_SA, an);
if (IS_ERR(sa))
return PTR_ERR(sa);
stats = ctx->stats.tx_sa_stats;
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_tx_sa_read_stats(phydev, sa, stats);
return 0;
}
static int nxp_c45_mdo_get_rx_sc_stats(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct macsec_rx_sa_stats rx_sa_stats;
struct macsec_rx_sc_stats *stats;
struct nxp_c45_secy *phy_secy;
struct nxp_c45_sa *pos, *tmp;
u32 reg = 0;
int i;
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
if (phy_secy->rx_sc != ctx->rx_sc)
return -EINVAL;
stats = ctx->stats.rx_sc_stats;
nxp_c45_select_secy(phydev, phy_secy->secy_id);
list_for_each_entry_safe(pos, tmp, &phy_secy->sa_list, list) {
if (pos->type != RX_SA)
continue;
memset(&rx_sa_stats, 0, sizeof(rx_sa_stats));
nxp_c45_rx_sa_read_stats(phydev, pos, &rx_sa_stats);
stats->InPktsInvalid += rx_sa_stats.InPktsInvalid;
stats->InPktsNotValid += rx_sa_stats.InPktsNotValid;
stats->InPktsOK += rx_sa_stats.InPktsOK;
}
for (i = 0; i < MACSEC_NUM_AN; i++) {
nxp_c45_macsec_read(phydev, MACSEC_RXAN0INUSS + i * 4, ®);
stats->InPktsNotUsingSA += reg;
nxp_c45_macsec_read(phydev, MACSEC_RXAN0IPUSS + i * 4, ®);
stats->InPktsUnusedSA += reg;
}
nxp_c45_macsec_read64(phydev, MACSEC_INOD1HS,
&stats->InOctetsDecrypted);
nxp_c45_macsec_read64(phydev, MACSEC_INOV1HS,
&stats->InOctetsValidated);
nxp_c45_macsec_read32_64(phydev, MACSEC_RXSCIPDS,
&stats->InPktsDelayed);
nxp_c45_macsec_read32_64(phydev, MACSEC_RXSCIPLS,
&stats->InPktsLate);
nxp_c45_macsec_read32_64(phydev, MACSEC_RXSCIPUS,
&stats->InPktsUnchecked);
return 0;
}
static int nxp_c45_mdo_get_rx_sa_stats(struct macsec_context *ctx)
{
struct phy_device *phydev = ctx->phydev;
struct nxp_c45_phy *priv = phydev->priv;
struct macsec_rx_sa_stats *stats;
struct nxp_c45_secy *phy_secy;
u8 an = ctx->sa.assoc_num;
struct nxp_c45_sa *sa;
phy_secy = nxp_c45_find_secy(&priv->macsec->secy_list, ctx->secy->sci);
if (IS_ERR(phy_secy))
return PTR_ERR(phy_secy);
sa = nxp_c45_find_sa(&phy_secy->sa_list, RX_SA, an);
if (IS_ERR(sa))
return PTR_ERR(sa);
stats = ctx->stats.rx_sa_stats;
nxp_c45_select_secy(phydev, phy_secy->secy_id);
nxp_c45_rx_sa_read_stats(phydev, sa, stats);
nxp_c45_macsec_read(phydev, MACSEC_RXAN0INUSS + an * 4,
&stats->InPktsNotUsingSA);
nxp_c45_macsec_read(phydev, MACSEC_RXAN0IPUSS + an * 4,
&stats->InPktsUnusedSA);
return 0;
}
struct tja11xx_tlv_header {
struct ethhdr eth;
u8 subtype;
u8 len;
u8 payload[28];
};
static int nxp_c45_mdo_insert_tx_tag(struct phy_device *phydev,
struct sk_buff *skb)
{
struct tja11xx_tlv_header *tlv;
struct ethhdr *eth;
eth = eth_hdr(skb);
tlv = skb_push(skb, TJA11XX_TLV_TX_NEEDED_HEADROOM);
memmove(tlv, eth, sizeof(*eth));
skb_reset_mac_header(skb);
tlv->eth.h_proto = htons(ETH_P_TJA11XX_TLV);
tlv->subtype = 1;
tlv->len = sizeof(tlv->payload);
memset(tlv->payload, 0, sizeof(tlv->payload));
return 0;
}
static const struct macsec_ops nxp_c45_macsec_ops = {
.mdo_dev_open = nxp_c45_mdo_dev_open,
.mdo_dev_stop = nxp_c45_mdo_dev_stop,
.mdo_add_secy = nxp_c45_mdo_add_secy,
.mdo_upd_secy = nxp_c45_mdo_upd_secy,
.mdo_del_secy = nxp_c45_mdo_del_secy,
.mdo_add_rxsc = nxp_c45_mdo_add_rxsc,
.mdo_upd_rxsc = nxp_c45_mdo_upd_rxsc,
.mdo_del_rxsc = nxp_c45_mdo_del_rxsc,
.mdo_add_rxsa = nxp_c45_mdo_add_rxsa,
.mdo_upd_rxsa = nxp_c45_mdo_upd_rxsa,
.mdo_del_rxsa = nxp_c45_mdo_del_rxsa,
.mdo_add_txsa = nxp_c45_mdo_add_txsa,
.mdo_upd_txsa = nxp_c45_mdo_upd_txsa,
.mdo_del_txsa = nxp_c45_mdo_del_txsa,
.mdo_get_dev_stats = nxp_c45_mdo_get_dev_stats,
.mdo_get_tx_sc_stats = nxp_c45_mdo_get_tx_sc_stats,
.mdo_get_tx_sa_stats = nxp_c45_mdo_get_tx_sa_stats,
.mdo_get_rx_sc_stats = nxp_c45_mdo_get_rx_sc_stats,
.mdo_get_rx_sa_stats = nxp_c45_mdo_get_rx_sa_stats,
.mdo_insert_tx_tag = nxp_c45_mdo_insert_tx_tag,
.needed_headroom = TJA11XX_TLV_TX_NEEDED_HEADROOM,
.needed_tailroom = TJA11XX_TLV_NEEDED_TAILROOM,
};
int nxp_c45_macsec_config_init(struct phy_device *phydev)
{
struct nxp_c45_phy *priv = phydev->priv;
int ret;
if (!priv->macsec)
return 0;
ret = phy_set_bits_mmd(phydev, MDIO_MMD_VEND1, VEND1_PORT_FUNC_ENABLES,
MACSEC_EN | ADAPTER_EN);
if (ret)
return ret;
ret = nxp_c45_macsec_write(phydev, ADPTR_CNTRL, ADPTR_CNTRL_CONFIG_EN |
ADPTR_CNTRL_ADPTR_EN);
if (ret)
return ret;
ret = nxp_c45_macsec_write(phydev, ADPTR_TX_TAG_CNTRL,
ADPTR_TX_TAG_CNTRL_ENA);
if (ret)
return ret;
ret = nxp_c45_macsec_write(phydev, ADPTR_CNTRL, ADPTR_CNTRL_ADPTR_EN);
if (ret)
return ret;
ret = nxp_c45_macsec_write(phydev, MACSEC_TPNET, PN_WRAP_THRESHOLD);
if (ret)
return ret;
/* Set MKA filter. */
ret = nxp_c45_macsec_write(phydev, MACSEC_UPFR0D2, ETH_P_PAE);
if (ret)
return ret;
ret = nxp_c45_macsec_write(phydev, MACSEC_UPFR0M1, MACSEC_OVP);
if (ret)
return ret;
ret = nxp_c45_macsec_write(phydev, MACSEC_UPFR0M2, ETYPE_MASK);
if (ret)
return ret;
ret = nxp_c45_macsec_write(phydev, MACSEC_UPFR0R, MACSEC_UPFR_EN);
return ret;
}
int nxp_c45_macsec_probe(struct phy_device *phydev)
{
struct nxp_c45_phy *priv = phydev->priv;
struct device *dev = &phydev->mdio.dev;
priv->macsec = devm_kzalloc(dev, sizeof(*priv->macsec), GFP_KERNEL);
if (!priv->macsec)
return -ENOMEM;
INIT_LIST_HEAD(&priv->macsec->secy_list);
phydev->macsec_ops = &nxp_c45_macsec_ops;
return 0;
}
void nxp_c45_macsec_remove(struct phy_device *phydev)
{
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *secy_p, *secy_t;
struct nxp_c45_sa *sa_p, *sa_t;
struct list_head *secy_list;
if (!priv->macsec)
return;
secy_list = &priv->macsec->secy_list;
nxp_c45_macsec_en(phydev, false);
list_for_each_entry_safe(secy_p, secy_t, secy_list, list) {
list_for_each_entry_safe(sa_p, sa_t, &secy_p->sa_list, list)
nxp_c45_sa_free(sa_p);
nxp_c45_secy_free(secy_p);
}
}
void nxp_c45_handle_macsec_interrupt(struct phy_device *phydev,
irqreturn_t *ret)
{
struct nxp_c45_phy *priv = phydev->priv;
struct nxp_c45_secy *secy;
struct nxp_c45_sa *sa;
u8 encoding_sa;
int secy_id;
u32 reg = 0;
if (!priv->macsec)
return;
do {
nxp_c45_macsec_read(phydev, MACSEC_EVR, ®);
if (!reg)
return;
secy_id = MACSEC_REG_SIZE - ffs(reg);
secy = nxp_c45_find_secy_by_id(&priv->macsec->secy_list,
secy_id);
if (IS_ERR(secy)) {
WARN_ON(1);
goto macsec_ack_irq;
}
encoding_sa = secy->secy->tx_sc.encoding_sa;
phydev_dbg(phydev, "pn_wrapped: TX SC %d, encoding_sa %u\n",
secy->secy_id, encoding_sa);
sa = nxp_c45_find_sa(&secy->sa_list, TX_SA, encoding_sa);
if (!IS_ERR(sa))
macsec_pn_wrapped(secy->secy, sa->sa);
else
WARN_ON(1);
macsec_ack_irq:
nxp_c45_macsec_write(phydev, MACSEC_EVR,
TX_SC_BIT(secy_id));
*ret = IRQ_HANDLED;
} while (reg);
}
|