aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/crypto/dpaa2_sec/hw/desc/pdcp.h
blob: 719ef605fdc4b0b8f322e9edd2bfc6eed4a6939c (plain)
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
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
/*
 * Copyright 2008-2013 Freescale Semiconductor, Inc.
 *
 * SPDX-License-Identifier: BSD-3-Clause or GPL-2.0+
 */

#ifndef __DESC_PDCP_H__
#define __DESC_PDCP_H__

#include "hw/rta.h"
#include "common.h"

/**
 * DOC: PDCP Shared Descriptor Constructors
 *
 * Shared descriptors for PDCP protocol.
 */

/**
 * PDCP_NULL_MAX_FRAME_LEN - The maximum frame frame length that is supported by
 *                           PDCP NULL protocol.
 */
#define PDCP_NULL_MAX_FRAME_LEN		0x00002FFF

/**
 * PDCP_MAC_I_LEN - The length of the MAC-I for PDCP protocol operation
 */
#define PDCP_MAC_I_LEN			0x00000004

/**
 * PDCP_MAX_FRAME_LEN_STATUS - The status returned in FD status/command field in
 *                             case the input frame is larger than
 *                             PDCP_NULL_MAX_FRAME_LEN.
 */
#define PDCP_MAX_FRAME_LEN_STATUS	0xF1

/**
 * PDCP_C_PLANE_SN_MASK - This mask is used in the PDCP descriptors for
 *                        extracting the sequence number (SN) from the PDCP
 *                        Control Plane header. For PDCP Control Plane, the SN
 *                        is constant (5 bits) as opposed to PDCP Data Plane
 *                        (7/12/15 bits).
 */
#define PDCP_C_PLANE_SN_MASK		0x1F000000
#define PDCP_C_PLANE_SN_MASK_BE		0x0000001F

/**
 * PDCP_U_PLANE_15BIT_SN_MASK - This mask is used in the PDCP descriptors for
 *                              extracting the sequence number (SN) from the
 *                              PDCP User Plane header. For PDCP Control Plane,
 *                              the SN is constant (5 bits) as opposed to PDCP
 *                              Data Plane (7/12/15 bits).
 */
#define PDCP_U_PLANE_15BIT_SN_MASK	0xFF7F0000
#define PDCP_U_PLANE_15BIT_SN_MASK_BE	0x00007FFF

/**
 * PDCP_BEARER_MASK - This mask is used masking out the bearer for PDCP
 *                    processing with SNOW f9 in LTE.
 *
 * The value on which this mask is applied is formatted as below:
 *     Count-C (32 bit) | Bearer (5 bit) | Direction (1 bit) | 0 (26 bits)
 *
 * Applying this mask is done for creating the upper 64 bits of the IV needed
 * for SNOW f9.
 *
 * The lower 32 bits of the mask are used for masking the direction for AES
 * CMAC IV.
 */
#define PDCP_BEARER_MASK		0x00000004FFFFFFFFull
#define PDCP_BEARER_MASK_BE		0xFFFFFFFF04000000ull

/**
 * PDCP_DIR_MASK - This mask is used masking out the direction for PDCP
 *                 processing with SNOW f9 in LTE.
 *
 * The value on which this mask is applied is formatted as below:
 *     Bearer (5 bit) | Direction (1 bit) | 0 (26 bits)
 *
 * Applying this mask is done for creating the lower 32 bits of the IV needed
 * for SNOW f9.
 *
 * The upper 32 bits of the mask are used for masking the direction for AES
 * CMAC IV.
 */
#define PDCP_DIR_MASK			0x00000000000000F8ull
#define PDCP_DIR_MASK_BE			0xF800000000000000ull

/**
 * PDCP_NULL_INT_MAC_I_VAL - The value of the PDCP PDU MAC-I in case NULL
 *                           integrity is used.
 */

#define PDCP_NULL_INT_MAC_I_VAL		0x00000000

/**
 * PDCP_NULL_INT_ICV_CHECK_FAILED_STATUS - The status used to report ICV check
 *                                         failed in case of NULL integrity
 *                                         Control Plane processing.
 */
#define PDCP_NULL_INT_ICV_CHECK_FAILED_STATUS	0x0A
/**
 * PDCP_DPOVRD_HFN_OV_EN - Value to be used in the FD status/cmd field to
 *                         indicate the HFN override mechanism is active for the
 *                         frame.
 */
#define PDCP_DPOVRD_HFN_OV_EN		0x80000000

/**
 * PDCP_P4080REV2_HFN_OV_BUFLEN - The length in bytes of the supplementary space
 *                                that must be provided by the user at the
 *                                beginning of the input frame buffer for
 *                                P4080 REV 2.
 *
 * The format of the frame buffer is the following:
 *
 *  |<---PDCP_P4080REV2_HFN_OV_BUFLEN-->|
 * //===================================||============||==============\\
 * || PDCP_DPOVRD_HFN_OV_EN | HFN value || PDCP Header|| PDCP Payload ||
 * \\===================================||============||==============//
 *
 * If HFN override mechanism is not desired, then the MSB of the first 4 bytes
 * must be set to 0b.
 */
#define PDCP_P4080REV2_HFN_OV_BUFLEN	4

/**
 * enum cipher_type_pdcp - Type selectors for cipher types in PDCP protocol OP
 *                         instructions.
 * @PDCP_CIPHER_TYPE_NULL: NULL
 * @PDCP_CIPHER_TYPE_SNOW: SNOW F8
 * @PDCP_CIPHER_TYPE_AES: AES
 * @PDCP_CIPHER_TYPE_ZUC: ZUCE
 * @PDCP_CIPHER_TYPE_INVALID: invalid option
 */
enum cipher_type_pdcp {
	PDCP_CIPHER_TYPE_NULL,
	PDCP_CIPHER_TYPE_SNOW,
	PDCP_CIPHER_TYPE_AES,
	PDCP_CIPHER_TYPE_ZUC,
	PDCP_CIPHER_TYPE_INVALID
};

/**
 * enum auth_type_pdcp - Type selectors for integrity types in PDCP protocol OP
 *                       instructions.
 * @PDCP_AUTH_TYPE_NULL: NULL
 * @PDCP_AUTH_TYPE_SNOW: SNOW F9
 * @PDCP_AUTH_TYPE_AES: AES CMAC
 * @PDCP_AUTH_TYPE_ZUC: ZUCA
 * @PDCP_AUTH_TYPE_INVALID: invalid option
 */
enum auth_type_pdcp {
	PDCP_AUTH_TYPE_NULL,
	PDCP_AUTH_TYPE_SNOW,
	PDCP_AUTH_TYPE_AES,
	PDCP_AUTH_TYPE_ZUC,
	PDCP_AUTH_TYPE_INVALID
};

/**
 * enum pdcp_dir - Type selectors for direction for PDCP protocol
 * @PDCP_DIR_UPLINK: uplink direction
 * @PDCP_DIR_DOWNLINK: downlink direction
 * @PDCP_DIR_INVALID: invalid option
 */
enum pdcp_dir {
	PDCP_DIR_UPLINK = 0,
	PDCP_DIR_DOWNLINK = 1,
	PDCP_DIR_INVALID
};

/**
 * enum pdcp_plane - PDCP domain selectors
 * @PDCP_CONTROL_PLANE: Control Plane
 * @PDCP_DATA_PLANE: Data Plane
 * @PDCP_SHORT_MAC: Short MAC
 */
enum pdcp_plane {
	PDCP_CONTROL_PLANE,
	PDCP_DATA_PLANE,
	PDCP_SHORT_MAC
};

/**
 * enum pdcp_sn_size - Sequence Number Size selectors for PDCP protocol
 * @PDCP_SN_SIZE_5: 5bit sequence number
 * @PDCP_SN_SIZE_7: 7bit sequence number
 * @PDCP_SN_SIZE_12: 12bit sequence number
 * @PDCP_SN_SIZE_15: 15bit sequence number
 * @PDCP_SN_SIZE_18: 18bit sequence number
 */
enum pdcp_sn_size {
	PDCP_SN_SIZE_5 = 5,
	PDCP_SN_SIZE_7 = 7,
	PDCP_SN_SIZE_12 = 12,
	PDCP_SN_SIZE_15 = 15
};

/*
 * PDCP Control Plane Protocol Data Blocks
 */
#define PDCP_C_PLANE_PDB_HFN_SHIFT		5
#define PDCP_C_PLANE_PDB_BEARER_SHIFT		27
#define PDCP_C_PLANE_PDB_DIR_SHIFT		26
#define PDCP_C_PLANE_PDB_HFN_THR_SHIFT		5

#define PDCP_U_PLANE_PDB_OPT_SHORT_SN		0x2
#define PDCP_U_PLANE_PDB_OPT_15B_SN		0x4
#define PDCP_U_PLANE_PDB_SHORT_SN_HFN_SHIFT	7
#define PDCP_U_PLANE_PDB_LONG_SN_HFN_SHIFT	12
#define PDCP_U_PLANE_PDB_15BIT_SN_HFN_SHIFT	15
#define PDCP_U_PLANE_PDB_BEARER_SHIFT		27
#define PDCP_U_PLANE_PDB_DIR_SHIFT		26
#define PDCP_U_PLANE_PDB_SHORT_SN_HFN_THR_SHIFT	7
#define PDCP_U_PLANE_PDB_LONG_SN_HFN_THR_SHIFT	12
#define PDCP_U_PLANE_PDB_15BIT_SN_HFN_THR_SHIFT	15

struct pdcp_pdb {
	union {
		uint32_t opt;
		uint32_t rsvd;
	} opt_res;
	uint32_t hfn_res;	/* HyperFrame number,(27, 25 or 21 bits),
				 * left aligned & right-padded with zeros.
				 */
	uint32_t bearer_dir_res;/* Bearer(5 bits), packet direction (1 bit),
				 * left aligned & right-padded with zeros.
				 */
	uint32_t hfn_thr_res;	/* HyperFrame number threshold (27, 25 or 21
				 * bits), left aligned & right-padded with
				 * zeros.
				 */
};

/*
 * PDCP internal PDB types
 */
enum pdb_type_e {
	PDCP_PDB_TYPE_NO_PDB,
	PDCP_PDB_TYPE_FULL_PDB,
	PDCP_PDB_TYPE_REDUCED_PDB,
	PDCP_PDB_TYPE_INVALID
};

/*
 * Function for appending the portion of a PDCP Control Plane shared descriptor
 * which performs NULL encryption and integrity (i.e. copies the input frame
 * to the output frame, appending 32 bits of zeros at the end (MAC-I for
 * NULL integrity).
 */
static inline int
pdcp_insert_cplane_null_op(struct program *p,
			   bool swap __maybe_unused,
			   struct alginfo *cipherdata __maybe_unused,
			   struct alginfo *authdata __maybe_unused,
			   unsigned int dir,
			   unsigned char era_2_sw_hfn_ovrd __maybe_unused)
{
	LABEL(local_offset);
	REFERENCE(move_cmd_read_descbuf);
	REFERENCE(move_cmd_write_descbuf);

	if (rta_sec_era > RTA_SEC_ERA_2) {
		MATHB(p, SEQINSZ, ADD, ZERO, VSEQINSZ, 4, 0);
		if (dir == OP_TYPE_ENCAP_PROTOCOL)
			MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);
		else
			MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);
	} else {
		MATHB(p, SEQINSZ, ADD, ONE, VSEQINSZ, 4, 0);
		MATHB(p, VSEQINSZ, SUB, ONE, VSEQINSZ, 4, 0);

		if (dir == OP_TYPE_ENCAP_PROTOCOL) {
			MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);
			MATHB(p, VSEQINSZ, SUB, ONE, MATH0, 4, 0);
		} else {
			MATHB(p, VSEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQINSZ, 4,
			      IMMED2);
			MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);
			MATHB(p, VSEQOUTSZ, SUB, ONE, MATH0, 4, 0);
		}

		MATHB(p, MATH0, ADD, ONE, MATH0, 4, 0);

		/*
		 * Since MOVELEN is available only starting with
		 * SEC ERA 3, use poor man's MOVELEN: create a MOVE
		 * command dynamically by writing the length from M1 by
		 * OR-ing the command in the M1 register and MOVE the
		 * result into the descriptor buffer. Care must be taken
		 * wrt. the location of the command because of SEC
		 * pipelining. The actual MOVEs are written at the end
		 * of the descriptor due to calculations needed on the
		 * offset in the descriptor for the MOVE command.
		 */
		move_cmd_read_descbuf = MOVE(p, DESCBUF, 0, MATH0, 0, 6,
					     IMMED);
		move_cmd_write_descbuf = MOVE(p, MATH0, 0, DESCBUF, 0, 8,
					      WAITCOMP | IMMED);
	}
	MATHB(p, VSEQINSZ, SUB, PDCP_NULL_MAX_FRAME_LEN, NONE, 4,
	      IMMED2);
	JUMP(p, PDCP_MAX_FRAME_LEN_STATUS, HALT_STATUS, ALL_FALSE, MATH_N);

	if (rta_sec_era > RTA_SEC_ERA_2) {
		if (dir == OP_TYPE_ENCAP_PROTOCOL)
			MATHB(p, VSEQINSZ, ADD, ZERO, MATH0, 4, 0);
		else
			MATHB(p, VSEQOUTSZ, ADD, ZERO, MATH0, 4, 0);
	}
	SEQFIFOSTORE(p, MSG, 0, 0, VLF);
	SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);

	if (rta_sec_era > RTA_SEC_ERA_2) {
		MOVE(p, AB1, 0, OFIFO, 0, MATH0, 0);
	} else {
		SET_LABEL(p, local_offset);

		/* Shut off automatic Info FIFO entries */
		LOAD(p, 0, DCTRL, LDOFF_DISABLE_AUTO_NFIFO, 0, IMMED);
		/* Placeholder for MOVE command with length from M1 register */
		MOVE(p, IFIFOAB1, 0, OFIFO, 0, 0, IMMED);
		/* Enable automatic Info FIFO entries */
		LOAD(p, 0, DCTRL, LDOFF_ENABLE_AUTO_NFIFO, 0, IMMED);
	}

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		MATHB(p, MATH1, XOR, MATH1, MATH0, 8, 0);
		MOVE(p, MATH0, 0, OFIFO, 0, 4, IMMED);
	}

	if (rta_sec_era < RTA_SEC_ERA_3) {
		PATCH_MOVE(p, move_cmd_read_descbuf, local_offset);
		PATCH_MOVE(p, move_cmd_write_descbuf, local_offset);
	}

	return 0;
}

static inline int
insert_copy_frame_op(struct program *p,
		     struct alginfo *cipherdata __maybe_unused,
		     unsigned int dir __maybe_unused)
{
	LABEL(local_offset);
	REFERENCE(move_cmd_read_descbuf);
	REFERENCE(move_cmd_write_descbuf);

	if (rta_sec_era > RTA_SEC_ERA_2) {
		MATHB(p, SEQINSZ, ADD, ZERO, VSEQINSZ,  4, 0);
		MATHB(p, SEQINSZ, ADD, ZERO, VSEQOUTSZ,  4, 0);
	} else {
		MATHB(p, SEQINSZ, ADD, ONE, VSEQINSZ,  4, 0);
		MATHB(p, VSEQINSZ, SUB, ONE, VSEQINSZ,  4, 0);
		MATHB(p, SEQINSZ, ADD, ONE, VSEQOUTSZ,  4, 0);
		MATHB(p, VSEQOUTSZ, SUB, ONE, VSEQOUTSZ,  4, 0);
		MATHB(p, VSEQINSZ, SUB, ONE, MATH0,  4, 0);
		MATHB(p, MATH0, ADD, ONE, MATH0,  4, 0);

		/*
		 * Since MOVELEN is available only starting with
		 * SEC ERA 3, use poor man's MOVELEN: create a MOVE
		 * command dynamically by writing the length from M1 by
		 * OR-ing the command in the M1 register and MOVE the
		 * result into the descriptor buffer. Care must be taken
		 * wrt. the location of the command because of SEC
		 * pipelining. The actual MOVEs are written at the end
		 * of the descriptor due to calculations needed on the
		 * offset in the descriptor for the MOVE command.
		 */
		move_cmd_read_descbuf = MOVE(p, DESCBUF, 0, MATH0, 0, 6,
					     IMMED);
		move_cmd_write_descbuf = MOVE(p, MATH0, 0, DESCBUF, 0, 8,
					      WAITCOMP | IMMED);
	}
	MATHB(p, SEQINSZ, SUB, PDCP_NULL_MAX_FRAME_LEN, NONE,  4,
	      IFB | IMMED2);
	JUMP(p, PDCP_MAX_FRAME_LEN_STATUS, HALT_STATUS, ALL_FALSE, MATH_N);

	if (rta_sec_era > RTA_SEC_ERA_2)
		MATHB(p, VSEQINSZ, ADD, ZERO, MATH0,  4, 0);

	SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);
	if (rta_sec_era > RTA_SEC_ERA_2) {
		MOVE(p, AB1, 0, OFIFO, 0, MATH0, 0);
	} else {
		SET_LABEL(p, local_offset);

		/* Shut off automatic Info FIFO entries */
		LOAD(p, 0, DCTRL, LDOFF_DISABLE_AUTO_NFIFO, 0, IMMED);

		/* Placeholder for MOVE command with length from M0 register */
		MOVE(p, IFIFOAB1, 0, OFIFO, 0, 0, IMMED);

		/* Enable automatic Info FIFO entries */
		LOAD(p, 0, DCTRL, LDOFF_ENABLE_AUTO_NFIFO, 0, IMMED);
	}

	SEQFIFOSTORE(p, MSG, 0, 0, VLF);

	if (rta_sec_era < RTA_SEC_ERA_3) {
		PATCH_MOVE(p, move_cmd_read_descbuf, local_offset);
		PATCH_MOVE(p, move_cmd_write_descbuf, local_offset);
	}
	return 0;
}

static inline int
pdcp_insert_cplane_int_only_op(struct program *p,
			       bool swap __maybe_unused,
			       struct alginfo *cipherdata __maybe_unused,
			       struct alginfo *authdata, unsigned int dir,
			       unsigned char era_2_sw_hfn_ovrd)
{
	LABEL(local_offset);
	REFERENCE(move_cmd_read_descbuf);
	REFERENCE(move_cmd_write_descbuf);

	switch (authdata->algtype) {
	case PDCP_AUTH_TYPE_SNOW:
		/* Insert Auth Key */
		KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		SEQLOAD(p, MATH0, 7, 1, 0);
		JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);

		if (rta_sec_era > RTA_SEC_ERA_2 ||
		    (rta_sec_era == RTA_SEC_ERA_2 &&
				   era_2_sw_hfn_ovrd == 0)) {
			SEQINPTR(p, 0, 1, RTO);
		} else {
			SEQINPTR(p, 0, 5, RTO);
			SEQFIFOLOAD(p, SKIP, 4, 0);
		}

		if (swap == false) {
			MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1,  8,
			      IFB | IMMED2);
			MATHB(p, MATH1, SHLD, MATH1, MATH1,  8, 0);

			MOVEB(p, DESCBUF, 8, MATH2, 0, 8, WAITCOMP | IMMED);

			MATHB(p, MATH2, AND, PDCP_BEARER_MASK, MATH2, 8,
			      IMMED2);
			MOVEB(p, DESCBUF, 0x0C, MATH3, 0, 4, WAITCOMP | IMMED);
			MATHB(p, MATH3, AND, PDCP_DIR_MASK, MATH3, 8, IMMED2);
			MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
			MOVEB(p, MATH2, 0, CONTEXT2, 0, 0x0C, WAITCOMP | IMMED);
		} else {
			MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK_BE, MATH1,  8,
			      IFB | IMMED2);
			MATHB(p, MATH1, SHLD, MATH1, MATH1,  8, 0);

			MOVE(p, DESCBUF, 8, MATH2, 0, 8, WAITCOMP | IMMED);
			MATHB(p, MATH2, AND, PDCP_BEARER_MASK_BE, MATH2, 8,
			      IMMED2);

			MOVE(p, DESCBUF, 0x0C, MATH3, 0, 4, WAITCOMP | IMMED);
			MATHB(p, MATH3, AND, PDCP_DIR_MASK_BE, MATH3, 8,
			      IMMED2);
			MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
			MOVE(p, MATH2, 0, CONTEXT2, 0, 0x0C, WAITCOMP | IMMED);
		}

		if (dir == OP_TYPE_DECAP_PROTOCOL) {
			MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, MATH1, 4,
			      IMMED2);
		} else {
			if (rta_sec_era > RTA_SEC_ERA_2) {
				MATHB(p, SEQINSZ, SUB, ZERO, MATH1, 4,
				      0);
			} else {
				MATHB(p, SEQINSZ, ADD, ONE, MATH1, 4,
				      0);
				MATHB(p, MATH1, SUB, ONE, MATH1, 4,
				      0);
			}
		}

		if (rta_sec_era > RTA_SEC_ERA_2) {
			MATHB(p, MATH1, SUB, ZERO, VSEQINSZ, 4, 0);
			MATHB(p, MATH1, SUB, ZERO, VSEQOUTSZ, 4, 0);
		} else {
			MATHB(p, ZERO, ADD, MATH1, VSEQINSZ, 4, 0);
			MATHB(p, ZERO, ADD, MATH1, VSEQOUTSZ, 4, 0);

			/*
			 * Since MOVELEN is available only starting with
			 * SEC ERA 3, use poor man's MOVELEN: create a MOVE
			 * command dynamically by writing the length from M1 by
			 * OR-ing the command in the M1 register and MOVE the
			 * result into the descriptor buffer. Care must be taken
			 * wrt. the location of the command because of SEC
			 * pipelining. The actual MOVEs are written at the end
			 * of the descriptor due to calculations needed on the
			 * offset in the descriptor for the MOVE command.
			 */
			move_cmd_read_descbuf = MOVE(p, DESCBUF, 0, MATH1, 0, 6,
						     IMMED);
			move_cmd_write_descbuf = MOVE(p, MATH1, 0, DESCBUF, 0,
						      8, WAITCOMP | IMMED);
		}

		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
		ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F9, OP_ALG_AAI_F9,
			      OP_ALG_AS_INITFINAL,
			      dir == OP_TYPE_ENCAP_PROTOCOL ?
				     ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
			      DIR_ENC);

		if (rta_sec_era > RTA_SEC_ERA_2) {
			SEQFIFOLOAD(p, MSGINSNOOP, 0,
				    VLF | LAST1 | LAST2 | FLUSH1);
			MOVE(p, AB1, 0, OFIFO, 0, MATH1, 0);
		} else {
			SEQFIFOLOAD(p, MSGINSNOOP, 0,
				    VLF | LAST1 | LAST2 | FLUSH1);
			SET_LABEL(p, local_offset);

			/* Shut off automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_DISABLE_AUTO_NFIFO, 0, IMMED);
			/*
			 * Placeholder for MOVE command with length from M1
			 * register
			 */
			MOVE(p, IFIFOAB1, 0, OFIFO, 0, 0, IMMED);
			/* Enable automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_ENABLE_AUTO_NFIFO, 0, IMMED);
		}

		if (dir == OP_TYPE_DECAP_PROTOCOL)
			SEQFIFOLOAD(p, ICV2, 4, LAST2);
		else
			SEQSTORE(p, CONTEXT2, 0, 4, 0);

		break;

	case PDCP_AUTH_TYPE_AES:
		/* Insert Auth Key */
		KEY(p, KEY1, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		SEQLOAD(p, MATH0, 7, 1, 0);
		JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
		if (rta_sec_era > RTA_SEC_ERA_2 ||
		    (rta_sec_era == RTA_SEC_ERA_2 &&
		     era_2_sw_hfn_ovrd == 0)) {
			SEQINPTR(p, 0, 1, RTO);
		} else {
			SEQINPTR(p, 0, 5, RTO);
			SEQFIFOLOAD(p, SKIP, 4, 0);
		}

		if (swap == false) {
			MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			      IFB | IMMED2);
			MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);

			MOVEB(p, DESCBUF, 8, MATH2, 0, 8, WAITCOMP | IMMED);
			MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
			MOVEB(p, MATH2, 0, IFIFOAB1, 0, 8, IMMED);
		} else {
			MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK_BE, MATH1, 8,
			      IFB | IMMED2);
			MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);

			MOVE(p, DESCBUF, 8, MATH2, 0, 8, WAITCOMP | IMMED);
			MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
			MOVE(p, MATH2, 0, IFIFOAB1, 0, 8, IMMED);
		}

		if (dir == OP_TYPE_DECAP_PROTOCOL) {
			MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, MATH1, 4,
			      IMMED2);
		} else {
			if (rta_sec_era > RTA_SEC_ERA_2) {
				MATHB(p, SEQINSZ, SUB, ZERO, MATH1, 4,
				      0);
			} else {
				MATHB(p, SEQINSZ, ADD, ONE, MATH1, 4,
				      0);
				MATHB(p, MATH1, SUB, ONE, MATH1, 4,
				      0);
			}
		}

		if (rta_sec_era > RTA_SEC_ERA_2) {
			MATHB(p, MATH1, SUB, ZERO, VSEQINSZ, 4, 0);
			MATHB(p, MATH1, SUB, ZERO, VSEQOUTSZ, 4, 0);
		} else {
			MATHB(p, ZERO, ADD, MATH1, VSEQINSZ, 4, 0);
			MATHB(p, ZERO, ADD, MATH1, VSEQOUTSZ, 4, 0);

			/*
			 * Since MOVELEN is available only starting with
			 * SEC ERA 3, use poor man's MOVELEN: create a MOVE
			 * command dynamically by writing the length from M1 by
			 * OR-ing the command in the M1 register and MOVE the
			 * result into the descriptor buffer. Care must be taken
			 * wrt. the location of the command because of SEC
			 * pipelining. The actual MOVEs are written at the end
			 * of the descriptor due to calculations needed on the
			 * offset in the descriptor for the MOVE command.
			 */
			move_cmd_read_descbuf = MOVE(p, DESCBUF, 0, MATH1, 0, 6,
						     IMMED);
			move_cmd_write_descbuf = MOVE(p, MATH1, 0, DESCBUF, 0,
						      8, WAITCOMP | IMMED);
		}
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
		ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
			      OP_ALG_AAI_CMAC,
			      OP_ALG_AS_INITFINAL,
			      dir == OP_TYPE_ENCAP_PROTOCOL ?
				     ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
			      DIR_ENC);

		if (rta_sec_era > RTA_SEC_ERA_2) {
			MOVE(p, AB2, 0, OFIFO, 0, MATH1, 0);
			SEQFIFOLOAD(p, MSGINSNOOP, 0,
				    VLF | LAST1 | LAST2 | FLUSH1);
		} else {
			SEQFIFOLOAD(p, MSGINSNOOP, 0,
				    VLF | LAST1 | LAST2 | FLUSH1);
			SET_LABEL(p, local_offset);

			/* Shut off automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_DISABLE_AUTO_NFIFO, 0, IMMED);

			/*
			 * Placeholder for MOVE command with length from
			 * M1 register
			 */
			MOVE(p, IFIFOAB2, 0, OFIFO, 0, 0, IMMED);

			/* Enable automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_ENABLE_AUTO_NFIFO, 0, IMMED);
		}

		if (dir == OP_TYPE_DECAP_PROTOCOL)
			SEQFIFOLOAD(p, ICV1, 4, LAST1 | FLUSH1);
		else
			SEQSTORE(p, CONTEXT1, 0, 4, 0);

		break;

	case PDCP_AUTH_TYPE_ZUC:
		if (rta_sec_era < RTA_SEC_ERA_5) {
			pr_err("Invalid era for selected algorithm\n");
			return -ENOTSUP;
		}
		/* Insert Auth Key */
		KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		SEQLOAD(p, MATH0, 7, 1, 0);
		JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
		SEQINPTR(p, 0, 1, RTO);
		if (swap == false) {
			MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			      IFB | IMMED2);
			MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);

			MOVEB(p, DESCBUF, 8, MATH2, 0, 8, WAITCOMP | IMMED);
			MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
			MOVEB(p, MATH2, 0, CONTEXT2, 0, 8, IMMED);

		} else {
			MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK_BE, MATH1, 8,
			      IFB | IMMED2);
			MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);

			MOVE(p, DESCBUF, 8, MATH2, 0, 8, WAITCOMP | IMMED);
			MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
			MOVE(p, MATH2, 0, CONTEXT2, 0, 8, IMMED);
		}
		if (dir == OP_TYPE_DECAP_PROTOCOL)
			MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, MATH1, 4,
			      IMMED2);
		else
			MATHB(p, SEQINSZ, SUB, ZERO, MATH1, 4, 0);

		MATHB(p, MATH1, SUB, ZERO, VSEQINSZ, 4, 0);
		MATHB(p, MATH1, SUB, ZERO, VSEQOUTSZ, 4, 0);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
		ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCA,
			      OP_ALG_AAI_F9,
			      OP_ALG_AS_INITFINAL,
			      dir == OP_TYPE_ENCAP_PROTOCOL ?
				     ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
			      DIR_ENC);
		SEQFIFOLOAD(p, MSGINSNOOP, 0, VLF | LAST1 | LAST2 | FLUSH1);
		MOVE(p, AB1, 0, OFIFO, 0, MATH1, 0);

		if (dir == OP_TYPE_DECAP_PROTOCOL)
			SEQFIFOLOAD(p, ICV2, 4, LAST2);
		else
			SEQSTORE(p, CONTEXT2, 0, 4, 0);

		break;

	default:
		pr_err("%s: Invalid integrity algorithm selected: %d\n",
		       "pdcp_insert_cplane_int_only_op", authdata->algtype);
		return -EINVAL;
	}

	if (rta_sec_era < RTA_SEC_ERA_3) {
		PATCH_MOVE(p, move_cmd_read_descbuf, local_offset);
		PATCH_MOVE(p, move_cmd_write_descbuf, local_offset);
	}

	return 0;
}

static inline int
pdcp_insert_cplane_enc_only_op(struct program *p,
			       bool swap __maybe_unused,
			       struct alginfo *cipherdata,
			       struct alginfo *authdata __maybe_unused,
			       unsigned int dir,
			       unsigned char era_2_sw_hfn_ovrd __maybe_unused)
{
	/* Insert Cipher Key */
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_CTRL_MIXED,
				(uint16_t)cipherdata->algtype << 8);
		return 0;
	}

	SEQLOAD(p, MATH0, 7, 1, 0);
	JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
	if (swap == false)
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);
	else
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK_BE, MATH1, 8,
			IFB | IMMED2);
	SEQSTORE(p, MATH0, 7, 1, 0);
	MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);
	MOVE(p, DESCBUF, 8, MATH2, 0, 8, WAITCOMP | IMMED);
	MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);

	switch (cipherdata->algtype) {
	case PDCP_CIPHER_TYPE_SNOW:
		MOVE(p, MATH2, 0, CONTEXT1, 0, 8, WAITCOMP | IMMED);

		if (rta_sec_era > RTA_SEC_ERA_2) {
			MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
		} else {
			MATHB(p, SEQINSZ, SUB, ONE, MATH1, 4, 0);
			MATHB(p, MATH1, ADD, ONE, VSEQINSZ, 4, 0);
		}

		if (dir == OP_TYPE_ENCAP_PROTOCOL)
			MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);
		else
			MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);
		ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F8,
			      OP_ALG_AAI_F8,
			      OP_ALG_AS_INITFINAL, ICV_CHECK_DISABLE,
			      dir == OP_TYPE_ENCAP_PROTOCOL ?
					DIR_ENC : DIR_DEC);
		break;

	case PDCP_CIPHER_TYPE_AES:
		MOVE(p, MATH2, 0, CONTEXT1, 0x10, 0x10, WAITCOMP | IMMED);

		if (rta_sec_era > RTA_SEC_ERA_2) {
			MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
		} else {
			MATHB(p, SEQINSZ, SUB, ONE, MATH1, 4, 0);
			MATHB(p, MATH1, ADD, ONE, VSEQINSZ, 4, 0);
		}

		if (dir == OP_TYPE_ENCAP_PROTOCOL)
			MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);
		else
			MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);

		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);
		ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
			      OP_ALG_AAI_CTR,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      dir == OP_TYPE_ENCAP_PROTOCOL ?
					DIR_ENC : DIR_DEC);
		break;

	case PDCP_CIPHER_TYPE_ZUC:
		if (rta_sec_era < RTA_SEC_ERA_5) {
			pr_err("Invalid era for selected algorithm\n");
			return -ENOTSUP;
		}

		MOVE(p, MATH2, 0, CONTEXT1, 0, 0x08, IMMED);
		MOVE(p, MATH2, 0, CONTEXT1, 0x08, 0x08, WAITCOMP | IMMED);
		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
		if (dir == OP_TYPE_ENCAP_PROTOCOL)
			MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);
		else
			MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4,
			      IMMED2);

		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);
		ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCE,
			      OP_ALG_AAI_F8,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      dir == OP_TYPE_ENCAP_PROTOCOL ?
					DIR_ENC : DIR_DEC);
		break;

	default:
		pr_err("%s: Invalid encrypt algorithm selected: %d\n",
		       "pdcp_insert_cplane_enc_only_op", cipherdata->algtype);
		return -EINVAL;
	}

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		SEQFIFOLOAD(p, MSG1, 0, VLF);
		FIFOLOAD(p, MSG1, PDCP_NULL_INT_MAC_I_VAL, 4,
			 LAST1 | FLUSH1 | IMMED);
	} else {
		SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);
		MOVE(p, OFIFO, 0, MATH1, 4, PDCP_MAC_I_LEN, WAITCOMP | IMMED);
		MATHB(p, MATH1, XOR, PDCP_NULL_INT_MAC_I_VAL, NONE, 4, IMMED2);
		JUMP(p, PDCP_NULL_INT_ICV_CHECK_FAILED_STATUS,
		     HALT_STATUS, ALL_FALSE, MATH_Z);
	}

	return 0;
}

static inline int
pdcp_insert_cplane_acc_op(struct program *p,
			  bool swap __maybe_unused,
			  struct alginfo *cipherdata,
			  struct alginfo *authdata,
			  unsigned int dir,
			  unsigned char era_2_hfn_ovrd __maybe_unused)
{
	/* Insert Auth Key */
	KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
	    INLINE_KEY(authdata));

	/* Insert Cipher Key */
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));
	PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_CTRL, (uint16_t)cipherdata->algtype);

	return 0;
}

static inline int
pdcp_insert_cplane_snow_aes_op(struct program *p,
			       bool swap __maybe_unused,
			       struct alginfo *cipherdata,
			       struct alginfo *authdata,
			       unsigned int dir,
			       unsigned char era_2_sw_hfn_ovrd)
{
	LABEL(back_to_sd_offset);
	LABEL(end_desc);
	LABEL(local_offset);
	LABEL(jump_to_beginning);
	LABEL(fifo_load_mac_i_offset);
	REFERENCE(seqin_ptr_read);
	REFERENCE(seqin_ptr_write);
	REFERENCE(seq_out_read);
	REFERENCE(jump_back_to_sd_cmd);
	REFERENCE(move_mac_i_to_desc_buf);

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
				cipherdata->keylen, INLINE_KEY(cipherdata));
		KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
				authdata->keylen, INLINE_KEY(authdata));

		PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_CTRL_MIXED,
			 ((uint16_t)cipherdata->algtype << 8) |
			 (uint16_t)authdata->algtype);

		return 0;
	}

	SEQLOAD(p, MATH0, 7, 1, 0);
	JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
	if (swap == false)
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);
	else
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK_BE, MATH1, 8,
			IFB | IMMED2);
	MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);
	MOVE(p, DESCBUF, 4, MATH2, 0, 0x08, WAITCOMP | IMMED);
	MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
	SEQSTORE(p, MATH0, 7, 1, 0);
	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		if (rta_sec_era > RTA_SEC_ERA_2 ||
		    (rta_sec_era == RTA_SEC_ERA_2 &&
				   era_2_sw_hfn_ovrd == 0)) {
			SEQINPTR(p, 0, 1, RTO);
		} else {
			SEQINPTR(p, 0, 5, RTO);
			SEQFIFOLOAD(p, SKIP, 4, 0);
		}
		KEY(p, KEY1, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		MOVE(p, MATH2, 0, IFIFOAB1, 0, 0x08, IMMED);

		if (rta_sec_era > RTA_SEC_ERA_2) {
			MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
			MATHB(p, SEQINSZ, SUB, ZERO, MATH1, 4, 0);
			MATHB(p, VSEQINSZ, ADD, PDCP_MAC_I_LEN - 1, VSEQOUTSZ,
			      4, IMMED2);
		} else {
			MATHB(p, SEQINSZ, SUB, MATH3, VSEQINSZ, 4, 0);
			MATHB(p, VSEQINSZ, ADD, PDCP_MAC_I_LEN - 1, VSEQOUTSZ,
			      4, IMMED2);
			/*
			 * Note: Although the calculations below might seem a
			 * little off, the logic is the following:
			 *
			 * - SEQ IN PTR RTO below needs the full length of the
			 *   frame; in case of P4080_REV_2_HFN_OV_WORKAROUND,
			 *   this means the length of the frame to be processed
			 *   + 4 bytes (the HFN override flag and value).
			 *   The length of the frame to be processed minus 1
			 *   byte is in the VSIL register (because
			 *   VSIL = SIL + 3, due to 1 byte, the header being
			 *   already written by the SEQ STORE above). So for
			 *   calculating the length to use in RTO, I add one
			 *   to the VSIL value in order to obtain the total
			 *   frame length. This helps in case of P4080 which
			 *   can have the value 0 as an operand in a MATH
			 *   command only as SRC1 When the HFN override
			 *   workaround is not enabled, the length of the
			 *   frame is given by the SIL register; the
			 *   calculation is similar to the one in the SEC 4.2
			 *   and SEC 5.3 cases.
			 */
			if (era_2_sw_hfn_ovrd)
				MATHB(p, VSEQOUTSZ, ADD, ONE, MATH1, 4,
				      0);
			else
				MATHB(p, SEQINSZ, ADD, MATH3, MATH1, 4,
				      0);
		}
		/*
		 * Placeholder for filling the length in
		 * SEQIN PTR RTO below
		 */
		seqin_ptr_read = MOVE(p, DESCBUF, 0, MATH1, 0, 6, IMMED);
		seqin_ptr_write = MOVE(p, MATH1, 0, DESCBUF, 0, 8,
				       WAITCOMP | IMMED);
		ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
			      OP_ALG_AAI_CMAC,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_DEC);
		SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);
		MOVE(p, CONTEXT1, 0, MATH3, 0, 4, WAITCOMP | IMMED);
		if (rta_sec_era <= RTA_SEC_ERA_3)
			LOAD(p, CLRW_CLR_C1KEY |
			     CLRW_CLR_C1CTX |
			     CLRW_CLR_C1ICV |
			     CLRW_CLR_C1DATAS |
			     CLRW_CLR_C1MODE,
			     CLRW, 0, 4, IMMED);
		else
			LOAD(p, CLRW_RESET_CLS1_CHA |
			     CLRW_CLR_C1KEY |
			     CLRW_CLR_C1CTX |
			     CLRW_CLR_C1ICV |
			     CLRW_CLR_C1DATAS |
			     CLRW_CLR_C1MODE,
			     CLRW, 0, 4, IMMED);

		if (rta_sec_era <= RTA_SEC_ERA_3)
			LOAD(p, CCTRL_RESET_CHA_ALL, CCTRL, 0, 4, IMMED);

		KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
		    cipherdata->keylen, INLINE_KEY(cipherdata));
		SET_LABEL(p, local_offset);
		MOVE(p, MATH2, 0, CONTEXT1, 0, 8, IMMED);
		SEQINPTR(p, 0, 0, RTO);

		if (rta_sec_era == RTA_SEC_ERA_2 && era_2_sw_hfn_ovrd) {
			SEQFIFOLOAD(p, SKIP, 5, 0);
			MATHB(p, SEQINSZ, ADD, ONE, SEQINSZ, 4, 0);
		}

		MATHB(p, SEQINSZ, SUB, ONE, VSEQINSZ, 4, 0);
		ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F8,
			      OP_ALG_AAI_F8,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_ENC);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);

		if (rta_sec_era > RTA_SEC_ERA_2 ||
		    (rta_sec_era == RTA_SEC_ERA_2 &&
				   era_2_sw_hfn_ovrd == 0))
			SEQFIFOLOAD(p, SKIP, 1, 0);

		SEQFIFOLOAD(p, MSG1, 0, VLF);
		MOVE(p, MATH3, 0, IFIFOAB1, 0, 4, LAST1 | FLUSH1 | IMMED);
		PATCH_MOVE(p, seqin_ptr_read, local_offset);
		PATCH_MOVE(p, seqin_ptr_write, local_offset);
	} else {
		MOVE(p, MATH2, 0, CONTEXT1, 0, 8, IMMED);

		if (rta_sec_era >= RTA_SEC_ERA_5)
			MOVE(p, CONTEXT1, 0, CONTEXT2, 0, 8, IMMED);

		if (rta_sec_era > RTA_SEC_ERA_2)
			MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
		else
			MATHB(p, SEQINSZ, SUB, MATH3, VSEQINSZ, 4, 0);

		MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);
/*
 * TODO: To be changed when proper support is added in RTA (can't load a
 * command that is also written by RTA (or patch it for that matter).
 * Change when proper RTA support is added.
 */
		if (p->ps)
			WORD(p, 0x168B0004);
		else
			WORD(p, 0x16880404);

		jump_back_to_sd_cmd = JUMP(p, 0, LOCAL_JUMP, ALL_TRUE, 0);
		/*
		 * Placeholder for command reading  the SEQ OUT command in
		 * JD. Done for rereading the decrypted data and performing
		 * the integrity check
		 */
/*
 * TODO: RTA currently doesn't support patching of length of a MOVE command
 * Thus, it is inserted as a raw word, as per PS setting.
 */
		if (p->ps)
			seq_out_read = MOVE(p, DESCBUF, 0, MATH1, 0, 20,
					    WAITCOMP | IMMED);
		else
			seq_out_read = MOVE(p, DESCBUF, 0, MATH1, 0, 16,
					    WAITCOMP | IMMED);

		MATHB(p, MATH1, XOR, CMD_SEQ_IN_PTR ^ CMD_SEQ_OUT_PTR, MATH1, 4,
		      IMMED2);
		/* Placeholder for overwriting the SEQ IN  with SEQ OUT */
/*
 * TODO: RTA currently doesn't support patching of length of a MOVE command
 * Thus, it is inserted as a raw word, as per PS setting.
 */
		if (p->ps)
			MOVE(p, MATH1, 0, DESCBUF, 0, 24, IMMED);
		else
			MOVE(p, MATH1, 0, DESCBUF, 0, 20, IMMED);

		KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
		    cipherdata->keylen, INLINE_KEY(cipherdata));

		if (rta_sec_era >= RTA_SEC_ERA_4)
			MOVE(p, CONTEXT1, 0, CONTEXT2, 0, 8, IMMED);
		else
			MOVE(p, CONTEXT1, 0, MATH3, 0, 8, IMMED);

		ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F8,
			      OP_ALG_AAI_F8,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_DEC);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);
		SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);

		if (rta_sec_era <= RTA_SEC_ERA_3)
			move_mac_i_to_desc_buf = MOVE(p, OFIFO, 0, DESCBUF, 0,
						      4, WAITCOMP | IMMED);
		else
			MOVE(p, OFIFO, 0, MATH3, 0, 4, IMMED);

		if (rta_sec_era <= RTA_SEC_ERA_3)
			LOAD(p, CCTRL_RESET_CHA_ALL, CCTRL, 0, 4, IMMED);
		else
			LOAD(p, CLRW_RESET_CLS1_CHA |
			     CLRW_CLR_C1KEY |
			     CLRW_CLR_C1CTX |
			     CLRW_CLR_C1ICV |
			     CLRW_CLR_C1DATAS |
			     CLRW_CLR_C1MODE,
			     CLRW, 0, 4, IMMED);

		KEY(p, KEY1, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		/*
		 * Placeholder for jump in SD for executing the new SEQ IN PTR
		 * command (which is actually the old SEQ OUT PTR command
		 * copied over from JD.
		 */
		SET_LABEL(p, jump_to_beginning);
		JUMP(p, 1 - jump_to_beginning, LOCAL_JUMP, ALL_TRUE, 0);
		SET_LABEL(p, back_to_sd_offset);
		ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
			      OP_ALG_AAI_CMAC,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_ENABLE,
			      DIR_DEC);

		/* Read the # of bytes written in the output buffer + 1 (HDR) */
		MATHB(p, VSEQOUTSZ, ADD, ONE, VSEQINSZ, 4, 0);

		if (rta_sec_era <= RTA_SEC_ERA_3)
			MOVE(p, MATH3, 0, IFIFOAB1, 0, 8, IMMED);
		else
			MOVE(p, CONTEXT2, 0, IFIFOAB1, 0, 8, IMMED);

		if (rta_sec_era == RTA_SEC_ERA_2 && era_2_sw_hfn_ovrd)
			SEQFIFOLOAD(p, SKIP, 4, 0);

		SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);

		if (rta_sec_era >= RTA_SEC_ERA_4) {
			LOAD(p, NFIFOENTRY_STYPE_ALTSOURCE |
			     NFIFOENTRY_DEST_CLASS1 |
			     NFIFOENTRY_DTYPE_ICV |
			     NFIFOENTRY_LC1 |
			     NFIFOENTRY_FC1 | 4, NFIFO_SZL, 0, 4, IMMED);
			MOVE(p, MATH3, 0, ALTSOURCE, 0, 4, IMMED);
		} else {
			SET_LABEL(p, fifo_load_mac_i_offset);
			FIFOLOAD(p, ICV1, fifo_load_mac_i_offset, 4,
				 LAST1 | FLUSH1 | IMMED);
		}

		SET_LABEL(p, end_desc);

		if (!p->ps) {
			PATCH_MOVE(p, seq_out_read, end_desc + 1);
			PATCH_JUMP(p, jump_back_to_sd_cmd,
				   back_to_sd_offset + jump_back_to_sd_cmd - 5);

			if (rta_sec_era <= RTA_SEC_ERA_3)
				PATCH_MOVE(p, move_mac_i_to_desc_buf,
					   fifo_load_mac_i_offset + 1);
		} else {
			PATCH_MOVE(p, seq_out_read, end_desc + 2);
			PATCH_JUMP(p, jump_back_to_sd_cmd,
				   back_to_sd_offset + jump_back_to_sd_cmd - 5);

			if (rta_sec_era <= RTA_SEC_ERA_3)
				PATCH_MOVE(p, move_mac_i_to_desc_buf,
					   fifo_load_mac_i_offset + 1);
		}
	}

	return 0;
}

static inline int
pdcp_insert_cplane_aes_snow_op(struct program *p,
			       bool swap __maybe_unused,
			       struct alginfo *cipherdata,
			       struct alginfo *authdata,
			       unsigned int dir,
			       unsigned char era_2_sw_hfn_ovrd __maybe_unused)
{
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));
	KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
	    INLINE_KEY(authdata));

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_CTRL_MIXED,
			 ((uint16_t)cipherdata->algtype << 8) |
			 (uint16_t)authdata->algtype);

		return 0;
	}

	if (dir == OP_TYPE_ENCAP_PROTOCOL)
		MATHB(p, SEQINSZ, SUB, ONE, VSEQINSZ, 4, 0);

	SEQLOAD(p, MATH0, 7, 1, 0);
	JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
	MOVE(p, MATH0, 7, IFIFOAB2, 0, 1, IMMED);
	if (swap == false)
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);
	else
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK_BE, MATH1, 8,
			IFB | IMMED2);

	SEQSTORE(p, MATH0, 7, 1, 0);
	MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);
	MOVE(p, DESCBUF, 4, MATH2, 0, 8, WAITCOMP | IMMED);
	MATHB(p, MATH1, OR, MATH2, MATH1, 8, 0);
	MOVE(p, MATH1, 0, CONTEXT1, 16, 8, IMMED);
	MOVE(p, MATH1, 0, CONTEXT2, 0, 4, IMMED);
	if (swap == false) {
		MATHB(p, MATH1, AND, lower_32_bits(PDCP_BEARER_MASK), MATH2, 4,
			IMMED2);
		MATHB(p, MATH1, AND, upper_32_bits(PDCP_DIR_MASK), MATH3, 4,
			IMMED2);
	} else {
		MATHB(p, MATH1, AND, lower_32_bits(PDCP_BEARER_MASK_BE), MATH2,
			4, IMMED2);
		MATHB(p, MATH1, AND, upper_32_bits(PDCP_DIR_MASK_BE), MATH3,
			4, IMMED2);
	}
	MATHB(p, MATH3, SHLD, MATH3, MATH3, 8, 0);
	MOVE(p, MATH2, 4, OFIFO, 0, 12, IMMED);
	MOVE(p, OFIFO, 0, CONTEXT2, 4, 12, IMMED);
	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);
	} else {
		MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, MATH1, 4, IMMED2);

		MATHB(p, ZERO, ADD, MATH1, VSEQOUTSZ, 4, 0);
		MATHB(p, ZERO, ADD, MATH1, VSEQINSZ, 4, 0);
	}

	if (dir == OP_TYPE_ENCAP_PROTOCOL)
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
	else
		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);

	ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F9,
		      OP_ALG_AAI_F9,
		      OP_ALG_AS_INITFINAL,
		      dir == OP_TYPE_ENCAP_PROTOCOL ?
			     ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
		      DIR_DEC);
	ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
		      OP_ALG_AAI_CTR,
		      OP_ALG_AS_INITFINAL,
		      ICV_CHECK_DISABLE,
		      dir == OP_TYPE_ENCAP_PROTOCOL ? DIR_ENC : DIR_DEC);

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		SEQFIFOLOAD(p, MSGINSNOOP, 0, VLF | LAST2);
		MOVE(p, CONTEXT2, 0, IFIFOAB1, 0, 4, LAST1 | FLUSH1 | IMMED);
	} else {
		SEQFIFOLOAD(p, MSGOUTSNOOP, 0, VLF | LAST2);
		SEQFIFOLOAD(p, MSG1, 4, LAST1 | FLUSH1);
		JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CLASS1 | NOP | NIFP);

		if (rta_sec_era >= RTA_SEC_ERA_6)
			LOAD(p, 0, DCTRL, 0, LDLEN_RST_CHA_OFIFO_PTR, IMMED);

		MOVE(p, OFIFO, 0, MATH0, 0, 4, WAITCOMP | IMMED);

		NFIFOADD(p, IFIFO, ICV2, 4, LAST2);

		if (rta_sec_era <= RTA_SEC_ERA_2) {
			/* Shut off automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_DISABLE_AUTO_NFIFO, 0, IMMED);
			MOVE(p, MATH0, 0, IFIFOAB2, 0, 4, WAITCOMP | IMMED);
		} else {
			MOVE(p, MATH0, 0, IFIFO, 0, 4, WAITCOMP | IMMED);
		}
	}

	return 0;
}

static inline int
pdcp_insert_cplane_snow_zuc_op(struct program *p,
			       bool swap __maybe_unused,
			       struct alginfo *cipherdata,
			       struct alginfo *authdata,
			       unsigned int dir,
			       unsigned char era_2_sw_hfn_ovrd __maybe_unused)
{
	LABEL(keyjump);
	REFERENCE(pkeyjump);

	if (rta_sec_era < RTA_SEC_ERA_5) {
		pr_err("Invalid era for selected algorithm\n");
		return -ENOTSUP;
	}

	pkeyjump = JUMP(p, keyjump, LOCAL_JUMP, ALL_TRUE, SHRD | SELF | BOTH);
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));
	KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
	    INLINE_KEY(authdata));

	SET_LABEL(p, keyjump);

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_CTRL_MIXED,
			 ((uint16_t)cipherdata->algtype << 8) |
			 (uint16_t)authdata->algtype);
		return 0;
	}

	SEQLOAD(p, MATH0, 7, 1, 0);
	JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
	MOVE(p, MATH0, 7, IFIFOAB2, 0, 1, IMMED);
	if (swap == false)
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);
	else
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);

	MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);
	MOVE(p, DESCBUF, 4, MATH2, 0, 8, WAITCOMP | IMMED);
	MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
	MOVE(p, MATH2, 0, CONTEXT1, 0, 8, IMMED);
	MOVE(p, MATH2, 0, CONTEXT2, 0, 8, WAITCOMP | IMMED);

	if (dir == OP_TYPE_ENCAP_PROTOCOL)
		MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);
	else
		MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);

	MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
	SEQSTORE(p, MATH0, 7, 1, 0);

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
		SEQFIFOLOAD(p, MSGINSNOOP, 0, VLF | LAST2);
	} else {
		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);
		SEQFIFOLOAD(p, MSGOUTSNOOP, 0, VLF | LAST1 | FLUSH1);
	}

	ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCA,
		      OP_ALG_AAI_F9,
		      OP_ALG_AS_INITFINAL,
		      dir == OP_TYPE_ENCAP_PROTOCOL ?
			     ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
		      DIR_ENC);

	ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F8,
		      OP_ALG_AAI_F8,
		      OP_ALG_AS_INITFINAL,
		      ICV_CHECK_DISABLE,
		      dir == OP_TYPE_ENCAP_PROTOCOL ? DIR_ENC : DIR_DEC);
	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		MOVE(p, CONTEXT2, 0, IFIFOAB1, 0, 4, LAST1 | FLUSH1 | IMMED);
	} else {
		/* Save ICV */
		MOVE(p, OFIFO, 0, MATH0, 0, 4, IMMED);
		LOAD(p, NFIFOENTRY_STYPE_ALTSOURCE |
		     NFIFOENTRY_DEST_CLASS2 |
		     NFIFOENTRY_DTYPE_ICV |
		     NFIFOENTRY_LC2 | 4, NFIFO_SZL, 0, 4, IMMED);
		MOVE(p, MATH0, 0, ALTSOURCE, 0, 4, WAITCOMP | IMMED);
	}

	/* Reset ZUCA mode and done interrupt */
	LOAD(p, CLRW_CLR_C2MODE, CLRW, 0, 4, IMMED);
	LOAD(p, CIRQ_ZADI, ICTRL, 0, 4, IMMED);

	PATCH_JUMP(p, pkeyjump, keyjump);
	return 0;
}

static inline int
pdcp_insert_cplane_aes_zuc_op(struct program *p,
			      bool swap __maybe_unused,
			      struct alginfo *cipherdata,
			      struct alginfo *authdata,
			      unsigned int dir,
			      unsigned char era_2_sw_hfn_ovrd __maybe_unused)
{
	LABEL(keyjump);
	REFERENCE(pkeyjump);

	if (rta_sec_era < RTA_SEC_ERA_5) {
		pr_err("Invalid era for selected algorithm\n");
		return -ENOTSUP;
	}

	pkeyjump = JUMP(p, keyjump, LOCAL_JUMP, ALL_TRUE, SHRD | SELF | BOTH);
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));
	KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
	    INLINE_KEY(authdata));

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_CTRL_MIXED,
			 ((uint16_t)cipherdata->algtype << 8) |
			 (uint16_t)authdata->algtype);

		return 0;
	}

	SET_LABEL(p, keyjump);
	SEQLOAD(p, MATH0, 7, 1, 0);
	JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
	MOVE(p, MATH0, 7, IFIFOAB2, 0, 1, IMMED);
	if (swap == false)
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);
	else
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);

	MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);
	MOVE(p, DESCBUF, 4, MATH2, 0, 8, WAITCOMP | IMMED);
	MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
	MOVE(p, MATH2, 0, CONTEXT1, 16, 8, IMMED);
	MOVE(p, MATH2, 0, CONTEXT2, 0, 8, WAITCOMP | IMMED);

	if (dir == OP_TYPE_ENCAP_PROTOCOL)
		MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);
	else
		MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);

	MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
	SEQSTORE(p, MATH0, 7, 1, 0);

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
		SEQFIFOLOAD(p, MSGINSNOOP, 0, VLF | LAST2);
	} else {
		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);
		SEQFIFOLOAD(p, MSGOUTSNOOP, 0, VLF | LAST1 | FLUSH1);
	}

	ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCA,
		      OP_ALG_AAI_F9,
		      OP_ALG_AS_INITFINAL,
		      dir == OP_TYPE_ENCAP_PROTOCOL ?
			     ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
		      DIR_ENC);

	ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
		      OP_ALG_AAI_CTR,
		      OP_ALG_AS_INITFINAL,
		      ICV_CHECK_DISABLE,
		      dir == OP_TYPE_ENCAP_PROTOCOL ? DIR_ENC : DIR_DEC);

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		MOVE(p, CONTEXT2, 0, IFIFOAB1, 0, 4, LAST1 | FLUSH1 | IMMED);
	} else {
		/* Save ICV */
		MOVE(p, OFIFO, 0, MATH0, 0, 4, IMMED);

		LOAD(p, NFIFOENTRY_STYPE_ALTSOURCE |
		     NFIFOENTRY_DEST_CLASS2 |
		     NFIFOENTRY_DTYPE_ICV |
		     NFIFOENTRY_LC2 | 4, NFIFO_SZL, 0, 4, IMMED);
		MOVE(p, MATH0, 0, ALTSOURCE, 0, 4, WAITCOMP | IMMED);
	}

	/* Reset ZUCA mode and done interrupt */
	LOAD(p, CLRW_CLR_C2MODE, CLRW, 0, 4, IMMED);
	LOAD(p, CIRQ_ZADI, ICTRL, 0, 4, IMMED);

	PATCH_JUMP(p, pkeyjump, keyjump);

	return 0;
}

static inline int
pdcp_insert_cplane_zuc_snow_op(struct program *p,
			       bool swap __maybe_unused,
			       struct alginfo *cipherdata,
			       struct alginfo *authdata,
			       unsigned int dir,
			       unsigned char era_2_sw_hfn_ovrd __maybe_unused)
{
	LABEL(keyjump);
	REFERENCE(pkeyjump);

	if (rta_sec_era < RTA_SEC_ERA_5) {
		pr_err("Invalid era for selected algorithm\n");
		return -ENOTSUP;
	}

	pkeyjump = JUMP(p, keyjump, LOCAL_JUMP, ALL_TRUE, SHRD | SELF | BOTH);
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));
	KEY(p, KEY2, authdata->key_enc_flags, authdata->key, authdata->keylen,
	    INLINE_KEY(authdata));

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_CTRL_MIXED,
			 ((uint16_t)cipherdata->algtype << 8) |
			 (uint16_t)authdata->algtype);

		return 0;
	}

	SET_LABEL(p, keyjump);
	SEQLOAD(p, MATH0, 7, 1, 0);
	JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
	MOVE(p, MATH0, 7, IFIFOAB2, 0, 1, IMMED);
	if (swap == false)
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);
	else
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);

	MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);
	MOVE(p, DESCBUF, 4, MATH2, 0, 8, WAITCOMP | IMMED);
	MATHB(p, MATH1, OR, MATH2, MATH1, 8, 0);
	MOVE(p, MATH1, 0, CONTEXT1, 0, 8, IMMED);
	MOVE(p, MATH1, 0, CONTEXT2, 0, 4, IMMED);
	if (swap == false) {
		MATHB(p, MATH1, AND, lower_32_bits(PDCP_BEARER_MASK), MATH2,
			4, IMMED2);
		MATHB(p, MATH1, AND, upper_32_bits(PDCP_DIR_MASK), MATH3,
			4, IMMED2);
	} else {
		MATHB(p, MATH1, AND, lower_32_bits(PDCP_BEARER_MASK_BE), MATH2,
			4, IMMED2);
		MATHB(p, MATH1, AND, upper_32_bits(PDCP_DIR_MASK_BE), MATH3,
			4, IMMED2);
	}
	MATHB(p, MATH3, SHLD, MATH3, MATH3, 8, 0);
	MOVE(p, MATH2, 4, OFIFO, 0, 12, IMMED);
	MOVE(p, OFIFO, 0, CONTEXT2, 4, 12, IMMED);

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		MATHB(p, SEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);
		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
	} else {
		MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);
		MATHB(p, VSEQOUTSZ, SUB, ZERO, VSEQINSZ, 4, 0);
	}

	SEQSTORE(p, MATH0, 7, 1, 0);

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
		SEQFIFOLOAD(p, MSGINSNOOP, 0, VLF | LAST2);
	} else {
		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);
		SEQFIFOLOAD(p, MSGOUTSNOOP, 0, VLF | LAST2);
	}

	ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F9,
		      OP_ALG_AAI_F9,
		      OP_ALG_AS_INITFINAL,
		      dir == OP_TYPE_ENCAP_PROTOCOL ?
			     ICV_CHECK_DISABLE : ICV_CHECK_ENABLE,
		      DIR_DEC);

	ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCE,
		      OP_ALG_AAI_F8,
		      OP_ALG_AS_INITFINAL,
		      ICV_CHECK_DISABLE,
		      dir == OP_TYPE_ENCAP_PROTOCOL ? DIR_ENC : DIR_DEC);

	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		MOVE(p, CONTEXT2, 0, IFIFOAB1, 0, 4, LAST1 | FLUSH1 | IMMED);
	} else {
		SEQFIFOLOAD(p, MSG1, 4, LAST1 | FLUSH1);
		JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CLASS1 | NOP | NIFP);

		if (rta_sec_era >= RTA_SEC_ERA_6)
			/*
			 * For SEC ERA 6, there's a problem with the OFIFO
			 * pointer, and thus it needs to be reset here before
			 * moving to M0.
			 */
			LOAD(p, 0, DCTRL, 0, LDLEN_RST_CHA_OFIFO_PTR, IMMED);

		/* Put ICV to M0 before sending it to C2 for comparison. */
		MOVE(p, OFIFO, 0, MATH0, 0, 4, WAITCOMP | IMMED);

		LOAD(p, NFIFOENTRY_STYPE_ALTSOURCE |
		     NFIFOENTRY_DEST_CLASS2 |
		     NFIFOENTRY_DTYPE_ICV |
		     NFIFOENTRY_LC2 | 4, NFIFO_SZL, 0, 4, IMMED);
		MOVE(p, MATH0, 0, ALTSOURCE, 0, 4, IMMED);
	}

	PATCH_JUMP(p, pkeyjump, keyjump);
	return 0;
}

static inline int
pdcp_insert_cplane_zuc_aes_op(struct program *p,
			      bool swap __maybe_unused,
			      struct alginfo *cipherdata,
			      struct alginfo *authdata,
			      unsigned int dir,
			      unsigned char era_2_sw_hfn_ovrd __maybe_unused)
{
	if (rta_sec_era < RTA_SEC_ERA_5) {
		pr_err("Invalid era for selected algorithm\n");
		return -ENOTSUP;
	}

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
				cipherdata->keylen, INLINE_KEY(cipherdata));
		KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
				authdata->keylen, INLINE_KEY(authdata));

		PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_CTRL_MIXED,
			 ((uint16_t)cipherdata->algtype << 8) |
			 (uint16_t)authdata->algtype);
		return 0;
	}

	SEQLOAD(p, MATH0, 7, 1, 0);
	JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
	if (swap == false)
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);
	else
		MATHB(p, MATH0, AND, PDCP_C_PLANE_SN_MASK, MATH1, 8,
			IFB | IMMED2);

	MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);
	MOVE(p, DESCBUF, 4, MATH2, 0, 0x08, WAITCOMP | IMMED);
	MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);
	SEQSTORE(p, MATH0, 7, 1, 0);
	if (dir == OP_TYPE_ENCAP_PROTOCOL) {
		KEY(p, KEY1, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		MOVE(p, MATH2, 0, IFIFOAB1, 0, 0x08, IMMED);
		MOVE(p, MATH0, 7, IFIFOAB1, 0, 1, IMMED);

		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
		MATHB(p, VSEQINSZ, ADD, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);

		ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
			      OP_ALG_AAI_CMAC,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_DEC);
		SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);
		MOVE(p, CONTEXT1, 0, MATH3, 0, 4, WAITCOMP | IMMED);
		LOAD(p, CLRW_RESET_CLS1_CHA |
		     CLRW_CLR_C1KEY |
		     CLRW_CLR_C1CTX |
		     CLRW_CLR_C1ICV |
		     CLRW_CLR_C1DATAS |
		     CLRW_CLR_C1MODE,
		     CLRW, 0, 4, IMMED);

		KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
		    cipherdata->keylen, INLINE_KEY(cipherdata));

		MOVE(p, MATH2, 0, CONTEXT1, 0, 8, IMMED);
		SEQINPTR(p, 0, PDCP_NULL_MAX_FRAME_LEN, RTO);

		ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCE,
			      OP_ALG_AAI_F8,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_ENC);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);

		SEQFIFOLOAD(p, SKIP, 1, 0);

		SEQFIFOLOAD(p, MSG1, 0, VLF);
		MOVE(p, MATH3, 0, IFIFOAB1, 0, 4, LAST1 | FLUSH1 | IMMED);
	} else {
		MOVE(p, MATH2, 0, CONTEXT1, 0, 8, IMMED);

		MOVE(p, CONTEXT1, 0, CONTEXT2, 0, 8, IMMED);

		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);

		MATHB(p, SEQINSZ, SUB, PDCP_MAC_I_LEN, VSEQOUTSZ, 4, IMMED2);

		KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
		    cipherdata->keylen, INLINE_KEY(cipherdata));

		MOVE(p, CONTEXT1, 0, CONTEXT2, 0, 8, IMMED);

		ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCE,
			      OP_ALG_AAI_F8,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_DEC);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF | CONT);
		SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);

		MOVE(p, OFIFO, 0, MATH3, 0, 4, IMMED);

		LOAD(p, CLRW_RESET_CLS1_CHA |
		     CLRW_CLR_C1KEY |
		     CLRW_CLR_C1CTX |
		     CLRW_CLR_C1ICV |
		     CLRW_CLR_C1DATAS |
		     CLRW_CLR_C1MODE,
		     CLRW, 0, 4, IMMED);

		KEY(p, KEY1, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));

		SEQINPTR(p, 0, 0, SOP);

		ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
			      OP_ALG_AAI_CMAC,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_ENABLE,
			      DIR_DEC);

		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);

		MOVE(p, CONTEXT2, 0, IFIFOAB1, 0, 8, IMMED);

		SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);

		LOAD(p, NFIFOENTRY_STYPE_ALTSOURCE |
		     NFIFOENTRY_DEST_CLASS1 |
		     NFIFOENTRY_DTYPE_ICV |
		     NFIFOENTRY_LC1 |
		     NFIFOENTRY_FC1 | 4, NFIFO_SZL, 0, 4, IMMED);
		MOVE(p, MATH3, 0, ALTSOURCE, 0, 4, IMMED);
	}

	return 0;
}

static inline int
pdcp_insert_uplane_15bit_op(struct program *p,
			    bool swap __maybe_unused,
			    struct alginfo *cipherdata,
			    unsigned int dir)
{
	int op;
	/* Insert Cipher Key */
	KEY(p, KEY1, cipherdata->key_enc_flags, cipherdata->key,
	    cipherdata->keylen, INLINE_KEY(cipherdata));

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		PROTOCOL(p, dir, OP_PCLID_LTE_PDCP_USER,
			 (uint16_t)cipherdata->algtype);
		return 0;
	}

	SEQLOAD(p, MATH0, 6, 2, 0);
	JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
	if (swap == false)
		MATHB(p, MATH0, AND, PDCP_U_PLANE_15BIT_SN_MASK, MATH1, 8,
		      IFB | IMMED2);
	else
		MATHB(p, MATH0, AND, PDCP_U_PLANE_15BIT_SN_MASK_BE, MATH1, 8,
		      IFB | IMMED2);
	SEQSTORE(p, MATH0, 6, 2, 0);
	MATHB(p, MATH1, SHLD, MATH1, MATH1, 8, 0);
	MOVE(p, DESCBUF, 8, MATH2, 0, 8, WAITCOMP | IMMED);
	MATHB(p, MATH1, OR, MATH2, MATH2, 8, 0);

	MATHB(p, SEQINSZ, SUB, MATH3, VSEQINSZ, 4, 0);
	MATHB(p, SEQINSZ, SUB, MATH3, VSEQOUTSZ, 4, 0);

	SEQFIFOSTORE(p, MSG, 0, 0, VLF);

	op = dir == OP_TYPE_ENCAP_PROTOCOL ? DIR_ENC : DIR_DEC;
	switch (cipherdata->algtype) {
	case PDCP_CIPHER_TYPE_SNOW:
		MOVE(p, MATH2, 0, CONTEXT1, 0, 8, WAITCOMP | IMMED);
		ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F8,
			      OP_ALG_AAI_F8,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      op);
		break;

	case PDCP_CIPHER_TYPE_AES:
		MOVE(p, MATH2, 0, CONTEXT1, 0x10, 0x10, WAITCOMP | IMMED);
		ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
			      OP_ALG_AAI_CTR,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      op);
		break;

	case PDCP_CIPHER_TYPE_ZUC:
		if (rta_sec_era < RTA_SEC_ERA_5) {
			pr_err("Invalid era for selected algorithm\n");
			return -ENOTSUP;
		}
		MOVE(p, MATH2, 0, CONTEXT1, 0, 0x08, IMMED);
		MOVE(p, MATH2, 0, CONTEXT1, 0x08, 0x08, WAITCOMP | IMMED);

		ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCE,
			      OP_ALG_AAI_F8,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      op);
		break;

	default:
		pr_err("%s: Invalid encrypt algorithm selected: %d\n",
		       "pdcp_insert_uplane_15bit_op", cipherdata->algtype);
		return -EINVAL;
	}

	SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | FLUSH1);

	return 0;
}

/*
 * Function for inserting the snippet of code responsible for creating
 * the HFN override code via either DPOVRD or via the input frame.
 */
static inline int
insert_hfn_ov_op(struct program *p,
		 uint32_t shift,
		 enum pdb_type_e pdb_type,
		 unsigned char era_2_sw_hfn_ovrd)
{
	uint32_t imm = PDCP_DPOVRD_HFN_OV_EN;
	uint16_t hfn_pdb_offset;

	if (rta_sec_era == RTA_SEC_ERA_2 && !era_2_sw_hfn_ovrd)
		return 0;

	switch (pdb_type) {
	case PDCP_PDB_TYPE_NO_PDB:
		/*
		 * If there is no PDB, then HFN override mechanism does not
		 * make any sense, thus in this case the function will
		 * return the pointer to the current position in the
		 * descriptor buffer
		 */
		return 0;

	case PDCP_PDB_TYPE_REDUCED_PDB:
		hfn_pdb_offset = 4;
		break;

	case PDCP_PDB_TYPE_FULL_PDB:
		hfn_pdb_offset = 8;
		break;

	default:
		return -EINVAL;
	}

	if (rta_sec_era > RTA_SEC_ERA_2) {
		MATHB(p, DPOVRD, AND, imm, NONE, 8, IFB | IMMED2);
	} else {
		SEQLOAD(p, MATH0, 4, 4, 0);
		JUMP(p, 1, LOCAL_JUMP, ALL_TRUE, CALM);
		MATHB(p, MATH0, AND, imm, NONE, 8, IFB | IMMED2);
		SEQSTORE(p, MATH0, 4, 4, 0);
	}

	if (rta_sec_era >= RTA_SEC_ERA_8)
		JUMP(p, 6, LOCAL_JUMP, ALL_TRUE, MATH_Z);
	else
		JUMP(p, 5, LOCAL_JUMP, ALL_TRUE, MATH_Z);

	if (rta_sec_era > RTA_SEC_ERA_2)
		MATHB(p, DPOVRD, LSHIFT, shift, MATH0, 4, IMMED2);
	else
		MATHB(p, MATH0, LSHIFT, shift, MATH0, 4, IMMED2);

	MATHB(p, MATH0, SHLD, MATH0, MATH0, 8, 0);
	MOVE(p, MATH0, 0, DESCBUF, hfn_pdb_offset, 4, IMMED);

	if (rta_sec_era >= RTA_SEC_ERA_8)
		/*
		 * For ERA8, DPOVRD could be handled by the PROTOCOL command
		 * itself. For now, this is not done. Thus, clear DPOVRD here
		 * to alleviate any side-effects.
		 */
		MATHB(p, DPOVRD, AND, ZERO, DPOVRD, 4, STL);

	return 0;
}

/*
 * PDCP Control PDB creation function
 */
static inline enum pdb_type_e
cnstr_pdcp_c_plane_pdb(struct program *p,
		       uint32_t hfn,
		       unsigned char bearer,
		       unsigned char direction,
		       uint32_t hfn_threshold,
		       struct alginfo *cipherdata,
		       struct alginfo *authdata)
{
	struct pdcp_pdb pdb;
	enum pdb_type_e
		pdb_mask[PDCP_CIPHER_TYPE_INVALID][PDCP_AUTH_TYPE_INVALID] = {
			{	/* NULL */
				PDCP_PDB_TYPE_NO_PDB,		/* NULL */
				PDCP_PDB_TYPE_FULL_PDB,		/* SNOW f9 */
				PDCP_PDB_TYPE_FULL_PDB,		/* AES CMAC */
				PDCP_PDB_TYPE_FULL_PDB		/* ZUC-I */
			},
			{	/* SNOW f8 */
				PDCP_PDB_TYPE_FULL_PDB,		/* NULL */
				PDCP_PDB_TYPE_FULL_PDB,		/* SNOW f9 */
				PDCP_PDB_TYPE_REDUCED_PDB,	/* AES CMAC */
				PDCP_PDB_TYPE_REDUCED_PDB	/* ZUC-I */
			},
			{	/* AES CTR */
				PDCP_PDB_TYPE_FULL_PDB,		/* NULL */
				PDCP_PDB_TYPE_REDUCED_PDB,	/* SNOW f9 */
				PDCP_PDB_TYPE_FULL_PDB,		/* AES CMAC */
				PDCP_PDB_TYPE_REDUCED_PDB	/* ZUC-I */
			},
			{	/* ZUC-E */
				PDCP_PDB_TYPE_FULL_PDB,		/* NULL */
				PDCP_PDB_TYPE_REDUCED_PDB,	/* SNOW f9 */
				PDCP_PDB_TYPE_REDUCED_PDB,	/* AES CMAC */
				PDCP_PDB_TYPE_FULL_PDB		/* ZUC-I */
			},
	};

	if (rta_sec_era >= RTA_SEC_ERA_8) {
		memset(&pdb, 0x00, sizeof(struct pdcp_pdb));

		/* This is a HW issue. Bit 2 should be set to zero,
		 * but it does not work this way. Override here.
		 */
		pdb.opt_res.rsvd = 0x00000002;

		/* Copy relevant information from user to PDB */
		pdb.hfn_res = hfn << PDCP_C_PLANE_PDB_HFN_SHIFT;
		pdb.bearer_dir_res = (uint32_t)
				((bearer << PDCP_C_PLANE_PDB_BEARER_SHIFT) |
				 (direction << PDCP_C_PLANE_PDB_DIR_SHIFT));
		pdb.hfn_thr_res =
				hfn_threshold << PDCP_C_PLANE_PDB_HFN_THR_SHIFT;

		/* copy PDB in descriptor*/
		__rta_out32(p, pdb.opt_res.opt);
		__rta_out32(p, pdb.hfn_res);
		__rta_out32(p, pdb.bearer_dir_res);
		__rta_out32(p, pdb.hfn_thr_res);

		return PDCP_PDB_TYPE_FULL_PDB;
	}

	switch (pdb_mask[cipherdata->algtype][authdata->algtype]) {
	case PDCP_PDB_TYPE_NO_PDB:
		break;

	case PDCP_PDB_TYPE_REDUCED_PDB:
		__rta_out32(p, (hfn << PDCP_C_PLANE_PDB_HFN_SHIFT));
		__rta_out32(p,
			    (uint32_t)((bearer <<
					PDCP_C_PLANE_PDB_BEARER_SHIFT) |
					(direction <<
					 PDCP_C_PLANE_PDB_DIR_SHIFT)));
		break;

	case PDCP_PDB_TYPE_FULL_PDB:
		memset(&pdb, 0x00, sizeof(struct pdcp_pdb));

		/* This is a HW issue. Bit 2 should be set to zero,
		 * but it does not work this way. Override here.
		 */
		pdb.opt_res.rsvd = 0x00000002;

		/* Copy relevant information from user to PDB */
		pdb.hfn_res = hfn << PDCP_C_PLANE_PDB_HFN_SHIFT;
		pdb.bearer_dir_res = (uint32_t)
			((bearer << PDCP_C_PLANE_PDB_BEARER_SHIFT) |
			 (direction << PDCP_C_PLANE_PDB_DIR_SHIFT));
		pdb.hfn_thr_res =
			hfn_threshold << PDCP_C_PLANE_PDB_HFN_THR_SHIFT;

		/* copy PDB in descriptor*/
		__rta_out32(p, pdb.opt_res.opt);
		__rta_out32(p, pdb.hfn_res);
		__rta_out32(p, pdb.bearer_dir_res);
		__rta_out32(p, pdb.hfn_thr_res);

		break;

	default:
		return PDCP_PDB_TYPE_INVALID;
	}

	return pdb_mask[cipherdata->algtype][authdata->algtype];
}

/*
 * PDCP UPlane PDB creation function
 */
static inline int
cnstr_pdcp_u_plane_pdb(struct program *p,
		       enum pdcp_sn_size sn_size,
		       uint32_t hfn, unsigned short bearer,
		       unsigned short direction,
		       uint32_t hfn_threshold)
{
	struct pdcp_pdb pdb;
	/* Read options from user */
	/* Depending on sequence number length, the HFN and HFN threshold
	 * have different lengths.
	 */
	memset(&pdb, 0x00, sizeof(struct pdcp_pdb));

	switch (sn_size) {
	case PDCP_SN_SIZE_7:
		pdb.opt_res.opt |= PDCP_U_PLANE_PDB_OPT_SHORT_SN;
		pdb.hfn_res = hfn << PDCP_U_PLANE_PDB_SHORT_SN_HFN_SHIFT;
		pdb.hfn_thr_res =
			hfn_threshold<<PDCP_U_PLANE_PDB_SHORT_SN_HFN_THR_SHIFT;
		break;

	case PDCP_SN_SIZE_12:
		pdb.opt_res.opt &= (uint32_t)(~PDCP_U_PLANE_PDB_OPT_SHORT_SN);
		pdb.hfn_res = hfn << PDCP_U_PLANE_PDB_LONG_SN_HFN_SHIFT;
		pdb.hfn_thr_res =
			hfn_threshold<<PDCP_U_PLANE_PDB_LONG_SN_HFN_THR_SHIFT;
		break;

	case PDCP_SN_SIZE_15:
		pdb.opt_res.opt = (uint32_t)(PDCP_U_PLANE_PDB_OPT_15B_SN);
		pdb.hfn_res = hfn << PDCP_U_PLANE_PDB_15BIT_SN_HFN_SHIFT;
		pdb.hfn_thr_res =
			hfn_threshold<<PDCP_U_PLANE_PDB_15BIT_SN_HFN_THR_SHIFT;
		break;

	default:
		pr_err("Invalid Sequence Number Size setting in PDB\n");
		return -EINVAL;
	}

	pdb.bearer_dir_res = (uint32_t)
				((bearer << PDCP_U_PLANE_PDB_BEARER_SHIFT) |
				 (direction << PDCP_U_PLANE_PDB_DIR_SHIFT));

	/* copy PDB in descriptor*/
	__rta_out32(p, pdb.opt_res.opt);
	__rta_out32(p, pdb.hfn_res);
	__rta_out32(p, pdb.bearer_dir_res);
	__rta_out32(p, pdb.hfn_thr_res);

	return 0;
}
/**
 * cnstr_shdsc_pdcp_c_plane_encap - Function for creating a PDCP Control Plane
 *                                  encapsulation descriptor.
 * @descbuf: pointer to buffer for descriptor construction
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @hfn: starting Hyper Frame Number to be used together with the SN from the
 *       PDCP frames.
 * @bearer: radio bearer ID
 * @direction: the direction of the PDCP frame (UL/DL)
 * @hfn_threshold: HFN value that once reached triggers a warning from SEC that
 *                 keys should be renegotiated at the earliest convenience.
 * @cipherdata: pointer to block cipher transform definitions
 *              Valid algorithm values are those from cipher_type_pdcp enum.
 * @authdata: pointer to authentication transform definitions
 *            Valid algorithm values are those from auth_type_pdcp enum.
 * @era_2_sw_hfn_ovrd: if software HFN override mechanism is desired for
 *                     this descriptor. Note: Can only be used for
 *                     SEC ERA 2.
 * Return: size of descriptor written in words or negative number on error.
 *         Once the function returns, the value of this parameter can be used
 *         for reclaiming the space that wasn't used for the descriptor.
 *
 * Note: descbuf must be large enough to contain a full 256 byte long
 * descriptor; after the function returns, by subtracting the actual number of
 * bytes used, the user can reuse the remaining buffer space for other purposes.
 */
static inline int
cnstr_shdsc_pdcp_c_plane_encap(uint32_t *descbuf,
			       bool ps,
			       bool swap,
			       uint32_t hfn,
			       unsigned char bearer,
			       unsigned char direction,
			       uint32_t hfn_threshold,
			       struct alginfo *cipherdata,
			       struct alginfo *authdata,
			       unsigned char era_2_sw_hfn_ovrd)
{
	static int
		(*pdcp_cp_fp[PDCP_CIPHER_TYPE_INVALID][PDCP_AUTH_TYPE_INVALID])
			(struct program*, bool swap, struct alginfo *,
			 struct alginfo *, unsigned int,
			unsigned char __maybe_unused) = {
		{	/* NULL */
			pdcp_insert_cplane_null_op,	/* NULL */
			pdcp_insert_cplane_int_only_op,	/* SNOW f9 */
			pdcp_insert_cplane_int_only_op,	/* AES CMAC */
			pdcp_insert_cplane_int_only_op	/* ZUC-I */
		},
		{	/* SNOW f8 */
			pdcp_insert_cplane_enc_only_op,	/* NULL */
			pdcp_insert_cplane_acc_op,	/* SNOW f9 */
			pdcp_insert_cplane_snow_aes_op,	/* AES CMAC */
			pdcp_insert_cplane_snow_zuc_op	/* ZUC-I */
		},
		{	/* AES CTR */
			pdcp_insert_cplane_enc_only_op,	/* NULL */
			pdcp_insert_cplane_aes_snow_op,	/* SNOW f9 */
			pdcp_insert_cplane_acc_op,	/* AES CMAC */
			pdcp_insert_cplane_aes_zuc_op	/* ZUC-I */
		},
		{	/* ZUC-E */
			pdcp_insert_cplane_enc_only_op,	/* NULL */
			pdcp_insert_cplane_zuc_snow_op,	/* SNOW f9 */
			pdcp_insert_cplane_zuc_aes_op,	/* AES CMAC */
			pdcp_insert_cplane_acc_op	/* ZUC-I */
		},
	};
	static enum rta_share_type
		desc_share[PDCP_CIPHER_TYPE_INVALID][PDCP_AUTH_TYPE_INVALID] = {
		{	/* NULL */
			SHR_WAIT,	/* NULL */
			SHR_ALWAYS,	/* SNOW f9 */
			SHR_ALWAYS,	/* AES CMAC */
			SHR_ALWAYS	/* ZUC-I */
		},
		{	/* SNOW f8 */
			SHR_ALWAYS,	/* NULL */
			SHR_ALWAYS,	/* SNOW f9 */
			SHR_WAIT,	/* AES CMAC */
			SHR_WAIT	/* ZUC-I */
		},
		{	/* AES CTR */
			SHR_ALWAYS,	/* NULL */
			SHR_ALWAYS,	/* SNOW f9 */
			SHR_ALWAYS,	/* AES CMAC */
			SHR_WAIT	/* ZUC-I */
		},
		{	/* ZUC-E */
			SHR_ALWAYS,	/* NULL */
			SHR_WAIT,	/* SNOW f9 */
			SHR_WAIT,	/* AES CMAC */
			SHR_ALWAYS	/* ZUC-I */
		},
	};
	enum pdb_type_e pdb_type;
	struct program prg;
	struct program *p = &prg;
	int err;
	LABEL(pdb_end);

	if (rta_sec_era != RTA_SEC_ERA_2 && era_2_sw_hfn_ovrd) {
		pr_err("Cannot select SW HFN override for other era than 2");
		return -EINVAL;
	}

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, desc_share[cipherdata->algtype][authdata->algtype], 0, 0);

	pdb_type = cnstr_pdcp_c_plane_pdb(p,
			hfn,
			bearer,
			direction,
			hfn_threshold,
			cipherdata,
			authdata);

	SET_LABEL(p, pdb_end);

	err = insert_hfn_ov_op(p, PDCP_SN_SIZE_5, pdb_type,
			       era_2_sw_hfn_ovrd);
	if (err)
		return err;

	err = pdcp_cp_fp[cipherdata->algtype][authdata->algtype](p,
		swap,
		cipherdata,
		authdata,
		OP_TYPE_ENCAP_PROTOCOL,
		era_2_sw_hfn_ovrd);
	if (err)
		return err;

	PATCH_HDR(p, 0, pdb_end);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_pdcp_c_plane_decap - Function for creating a PDCP Control Plane
 *                                  decapsulation descriptor.
 * @descbuf: pointer to buffer for descriptor construction
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @hfn: starting Hyper Frame Number to be used together with the SN from the
 *       PDCP frames.
 * @bearer: radio bearer ID
 * @direction: the direction of the PDCP frame (UL/DL)
 * @hfn_threshold: HFN value that once reached triggers a warning from SEC that
 *                 keys should be renegotiated at the earliest convenience.
 * @cipherdata: pointer to block cipher transform definitions
 *              Valid algorithm values are those from cipher_type_pdcp enum.
 * @authdata: pointer to authentication transform definitions
 *            Valid algorithm values are those from auth_type_pdcp enum.
 * @era_2_sw_hfn_ovrd: if software HFN override mechanism is desired for
 *                     this descriptor. Note: Can only be used for
 *                     SEC ERA 2.
 *
 * Return: size of descriptor written in words or negative number on error.
 *         Once the function returns, the value of this parameter can be used
 *         for reclaiming the space that wasn't used for the descriptor.
 *
 * Note: descbuf must be large enough to contain a full 256 byte long
 * descriptor; after the function returns, by subtracting the actual number of
 * bytes used, the user can reuse the remaining buffer space for other purposes.
 */
static inline int
cnstr_shdsc_pdcp_c_plane_decap(uint32_t *descbuf,
			       bool ps,
			       bool swap,
			       uint32_t hfn,
			       unsigned char bearer,
			       unsigned char direction,
			       uint32_t hfn_threshold,
			       struct alginfo *cipherdata,
			       struct alginfo *authdata,
			       unsigned char era_2_sw_hfn_ovrd)
{
	static int
		(*pdcp_cp_fp[PDCP_CIPHER_TYPE_INVALID][PDCP_AUTH_TYPE_INVALID])
			(struct program*, bool swap, struct alginfo *,
			 struct alginfo *, unsigned int, unsigned char) = {
		{	/* NULL */
			pdcp_insert_cplane_null_op,	/* NULL */
			pdcp_insert_cplane_int_only_op,	/* SNOW f9 */
			pdcp_insert_cplane_int_only_op,	/* AES CMAC */
			pdcp_insert_cplane_int_only_op	/* ZUC-I */
		},
		{	/* SNOW f8 */
			pdcp_insert_cplane_enc_only_op,	/* NULL */
			pdcp_insert_cplane_acc_op,	/* SNOW f9 */
			pdcp_insert_cplane_snow_aes_op,	/* AES CMAC */
			pdcp_insert_cplane_snow_zuc_op	/* ZUC-I */
		},
		{	/* AES CTR */
			pdcp_insert_cplane_enc_only_op,	/* NULL */
			pdcp_insert_cplane_aes_snow_op,	/* SNOW f9 */
			pdcp_insert_cplane_acc_op,	/* AES CMAC */
			pdcp_insert_cplane_aes_zuc_op	/* ZUC-I */
		},
		{	/* ZUC-E */
			pdcp_insert_cplane_enc_only_op,	/* NULL */
			pdcp_insert_cplane_zuc_snow_op,	/* SNOW f9 */
			pdcp_insert_cplane_zuc_aes_op,	/* AES CMAC */
			pdcp_insert_cplane_acc_op	/* ZUC-I */
		},
	};
	static enum rta_share_type
		desc_share[PDCP_CIPHER_TYPE_INVALID][PDCP_AUTH_TYPE_INVALID] = {
		{	/* NULL */
			SHR_WAIT,	/* NULL */
			SHR_ALWAYS,	/* SNOW f9 */
			SHR_ALWAYS,	/* AES CMAC */
			SHR_ALWAYS	/* ZUC-I */
		},
		{	/* SNOW f8 */
			SHR_ALWAYS,	/* NULL */
			SHR_ALWAYS,	/* SNOW f9 */
			SHR_WAIT,	/* AES CMAC */
			SHR_WAIT	/* ZUC-I */
		},
		{	/* AES CTR */
			SHR_ALWAYS,	/* NULL */
			SHR_ALWAYS,	/* SNOW f9 */
			SHR_ALWAYS,	/* AES CMAC */
			SHR_WAIT	/* ZUC-I */
		},
		{	/* ZUC-E */
			SHR_ALWAYS,	/* NULL */
			SHR_WAIT,	/* SNOW f9 */
			SHR_WAIT,	/* AES CMAC */
			SHR_ALWAYS	/* ZUC-I */
		},
	};
	enum pdb_type_e pdb_type;
	struct program prg;
	struct program *p = &prg;
	int err;
	LABEL(pdb_end);

	if (rta_sec_era != RTA_SEC_ERA_2 && era_2_sw_hfn_ovrd) {
		pr_err("Cannot select SW HFN override for other era than 2");
		return -EINVAL;
	}

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, desc_share[cipherdata->algtype][authdata->algtype], 0, 0);

	pdb_type = cnstr_pdcp_c_plane_pdb(p,
			hfn,
			bearer,
			direction,
			hfn_threshold,
			cipherdata,
			authdata);

	SET_LABEL(p, pdb_end);

	err = insert_hfn_ov_op(p, PDCP_SN_SIZE_5, pdb_type,
			       era_2_sw_hfn_ovrd);
	if (err)
		return err;

	err = pdcp_cp_fp[cipherdata->algtype][authdata->algtype](p,
		swap,
		cipherdata,
		authdata,
		OP_TYPE_DECAP_PROTOCOL,
		era_2_sw_hfn_ovrd);
	if (err)
		return err;

	PATCH_HDR(p, 0, pdb_end);

	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_pdcp_u_plane_encap - Function for creating a PDCP User Plane
 *                                  encapsulation descriptor.
 * @descbuf: pointer to buffer for descriptor construction
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @sn_size: selects Sequence Number Size: 7/12/15 bits
 * @hfn: starting Hyper Frame Number to be used together with the SN from the
 *       PDCP frames.
 * @bearer: radio bearer ID
 * @direction: the direction of the PDCP frame (UL/DL)
 * @hfn_threshold: HFN value that once reached triggers a warning from SEC that
 *                 keys should be renegotiated at the earliest convenience.
 * @cipherdata: pointer to block cipher transform definitions
 *              Valid algorithm values are those from cipher_type_pdcp enum.
 * @era_2_sw_hfn_ovrd: if software HFN override mechanism is desired for
 *                     this descriptor. Note: Can only be used for
 *                     SEC ERA 2.
 *
 * Return: size of descriptor written in words or negative number on error.
 *         Once the function returns, the value of this parameter can be used
 *         for reclaiming the space that wasn't used for the descriptor.
 *
 * Note: descbuf must be large enough to contain a full 256 byte long
 * descriptor; after the function returns, by subtracting the actual number of
 * bytes used, the user can reuse the remaining buffer space for other purposes.
 */
static inline int
cnstr_shdsc_pdcp_u_plane_encap(uint32_t *descbuf,
			       bool ps,
			       bool swap,
			       enum pdcp_sn_size sn_size,
			       uint32_t hfn,
			       unsigned short bearer,
			       unsigned short direction,
			       uint32_t hfn_threshold,
			       struct alginfo *cipherdata,
			       unsigned char era_2_sw_hfn_ovrd)
{
	struct program prg;
	struct program *p = &prg;
	int err;
	LABEL(pdb_end);

	if (rta_sec_era != RTA_SEC_ERA_2 && era_2_sw_hfn_ovrd) {
		pr_err("Cannot select SW HFN ovrd for other era than 2");
		return -EINVAL;
	}

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, SHR_ALWAYS, 0, 0);
	if (cnstr_pdcp_u_plane_pdb(p, sn_size, hfn, bearer, direction,
				   hfn_threshold)) {
		pr_err("Error creating PDCP UPlane PDB\n");
		return -EINVAL;
	}
	SET_LABEL(p, pdb_end);

	err = insert_hfn_ov_op(p, sn_size, PDCP_PDB_TYPE_FULL_PDB,
			       era_2_sw_hfn_ovrd);
	if (err)
		return err;

	switch (sn_size) {
	case PDCP_SN_SIZE_7:
	case PDCP_SN_SIZE_12:
		switch (cipherdata->algtype) {
		case PDCP_CIPHER_TYPE_ZUC:
			if (rta_sec_era < RTA_SEC_ERA_5) {
				pr_err("Invalid era for selected algorithm\n");
				return -ENOTSUP;
			}
		case PDCP_CIPHER_TYPE_AES:
		case PDCP_CIPHER_TYPE_SNOW:
			/* Insert Cipher Key */
			KEY(p, KEY1, cipherdata->key_enc_flags,
			    (uint64_t)cipherdata->key, cipherdata->keylen,
			    INLINE_KEY(cipherdata));
			PROTOCOL(p, OP_TYPE_ENCAP_PROTOCOL,
				 OP_PCLID_LTE_PDCP_USER,
				 (uint16_t)cipherdata->algtype);
			break;
		case PDCP_CIPHER_TYPE_NULL:
			insert_copy_frame_op(p,
					     cipherdata,
					     OP_TYPE_ENCAP_PROTOCOL);
			break;
		default:
			pr_err("%s: Invalid encrypt algorithm selected: %d\n",
			       "cnstr_pcl_shdsc_pdcp_u_plane_decap",
			       cipherdata->algtype);
			return -EINVAL;
		}
		break;

	case PDCP_SN_SIZE_15:
		switch (cipherdata->algtype) {
		case PDCP_CIPHER_TYPE_NULL:
			insert_copy_frame_op(p,
					     cipherdata,
					     OP_TYPE_ENCAP_PROTOCOL);
			break;

		default:
			err = pdcp_insert_uplane_15bit_op(p, swap, cipherdata,
				OP_TYPE_ENCAP_PROTOCOL);
			if (err)
				return err;
			break;
		}
		break;

	case PDCP_SN_SIZE_5:
	default:
		pr_err("Invalid SN size selected\n");
		return -ENOTSUP;
	}

	PATCH_HDR(p, 0, pdb_end);
	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_pdcp_u_plane_decap - Function for creating a PDCP User Plane
 *                                  decapsulation descriptor.
 * @descbuf: pointer to buffer for descriptor construction
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @sn_size: selects Sequence Number Size: 7/12/15 bits
 * @hfn: starting Hyper Frame Number to be used together with the SN from the
 *       PDCP frames.
 * @bearer: radio bearer ID
 * @direction: the direction of the PDCP frame (UL/DL)
 * @hfn_threshold: HFN value that once reached triggers a warning from SEC that
 *                 keys should be renegotiated at the earliest convenience.
 * @cipherdata: pointer to block cipher transform definitions
 *              Valid algorithm values are those from cipher_type_pdcp enum.
 * @era_2_sw_hfn_ovrd: if software HFN override mechanism is desired for
 *                     this descriptor. Note: Can only be used for
 *                     SEC ERA 2.
 *
 * Return: size of descriptor written in words or negative number on error.
 *         Once the function returns, the value of this parameter can be used
 *         for reclaiming the space that wasn't used for the descriptor.
 *
 * Note: descbuf must be large enough to contain a full 256 byte long
 * descriptor; after the function returns, by subtracting the actual number of
 * bytes used, the user can reuse the remaining buffer space for other purposes.
 */
static inline int
cnstr_shdsc_pdcp_u_plane_decap(uint32_t *descbuf,
			       bool ps,
			       bool swap,
			       enum pdcp_sn_size sn_size,
			       uint32_t hfn,
			       unsigned short bearer,
			       unsigned short direction,
			       uint32_t hfn_threshold,
			       struct alginfo *cipherdata,
			       unsigned char era_2_sw_hfn_ovrd)
{
	struct program prg;
	struct program *p = &prg;
	int err;
	LABEL(pdb_end);

	if (rta_sec_era != RTA_SEC_ERA_2 && era_2_sw_hfn_ovrd) {
		pr_err("Cannot select SW HFN override for other era than 2");
		return -EINVAL;
	}

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, SHR_ALWAYS, 0, 0);
	if (cnstr_pdcp_u_plane_pdb(p, sn_size, hfn, bearer, direction,
				   hfn_threshold)) {
		pr_err("Error creating PDCP UPlane PDB\n");
		return -EINVAL;
	}
	SET_LABEL(p, pdb_end);

	err = insert_hfn_ov_op(p, sn_size, PDCP_PDB_TYPE_FULL_PDB,
			       era_2_sw_hfn_ovrd);
	if (err)
		return err;

	switch (sn_size) {
	case PDCP_SN_SIZE_7:
	case PDCP_SN_SIZE_12:
		switch (cipherdata->algtype) {
		case PDCP_CIPHER_TYPE_ZUC:
			if (rta_sec_era < RTA_SEC_ERA_5) {
				pr_err("Invalid era for selected algorithm\n");
				return -ENOTSUP;
			}
		case PDCP_CIPHER_TYPE_AES:
		case PDCP_CIPHER_TYPE_SNOW:
			/* Insert Cipher Key */
			KEY(p, KEY1, cipherdata->key_enc_flags,
			    cipherdata->key, cipherdata->keylen,
			    INLINE_KEY(cipherdata));
			PROTOCOL(p, OP_TYPE_DECAP_PROTOCOL,
				 OP_PCLID_LTE_PDCP_USER,
				 (uint16_t)cipherdata->algtype);
			break;
		case PDCP_CIPHER_TYPE_NULL:
			insert_copy_frame_op(p,
					     cipherdata,
					     OP_TYPE_DECAP_PROTOCOL);
			break;
		default:
			pr_err("%s: Invalid encrypt algorithm selected: %d\n",
			       "cnstr_pcl_shdsc_pdcp_u_plane_decap",
			       cipherdata->algtype);
			return -EINVAL;
		}
		break;

	case PDCP_SN_SIZE_15:
		switch (cipherdata->algtype) {
		case PDCP_CIPHER_TYPE_NULL:
			insert_copy_frame_op(p,
					     cipherdata,
					     OP_TYPE_DECAP_PROTOCOL);
			break;

		default:
			err = pdcp_insert_uplane_15bit_op(p, swap, cipherdata,
				OP_TYPE_DECAP_PROTOCOL);
			if (err)
				return err;
			break;
		}
		break;

	case PDCP_SN_SIZE_5:
	default:
		pr_err("Invalid SN size selected\n");
		return -ENOTSUP;
	}

	PATCH_HDR(p, 0, pdb_end);
	return PROGRAM_FINALIZE(p);
}

/**
 * cnstr_shdsc_pdcp_short_mac - Function for creating a PDCP Short MAC
 *                              descriptor.
 * @descbuf: pointer to buffer for descriptor construction
 * @ps: if 36/40bit addressing is desired, this parameter must be true
 * @swap: must be true when core endianness doesn't match SEC endianness
 * @authdata: pointer to authentication transform definitions
 *            Valid algorithm values are those from auth_type_pdcp enum.
 *
 * Return: size of descriptor written in words or negative number on error.
 *         Once the function returns, the value of this parameter can be used
 *         for reclaiming the space that wasn't used for the descriptor.
 *
 * Note: descbuf must be large enough to contain a full 256 byte long
 * descriptor; after the function returns, by subtracting the actual number of
 * bytes used, the user can reuse the remaining buffer space for other purposes.
 */
static inline int
cnstr_shdsc_pdcp_short_mac(uint32_t *descbuf,
			   bool ps,
			   bool swap,
			   struct alginfo *authdata)
{
	struct program prg;
	struct program *p = &prg;
	uint32_t iv[3] = {0, 0, 0};
	LABEL(local_offset);
	REFERENCE(move_cmd_read_descbuf);
	REFERENCE(move_cmd_write_descbuf);

	PROGRAM_CNTXT_INIT(p, descbuf, 0);
	if (swap)
		PROGRAM_SET_BSWAP(p);
	if (ps)
		PROGRAM_SET_36BIT_ADDR(p);

	SHR_HDR(p, SHR_ALWAYS, 1, 0);

	if (rta_sec_era > RTA_SEC_ERA_2) {
		MATHB(p, SEQINSZ, SUB, ZERO, VSEQINSZ, 4, 0);
		MATHB(p, SEQINSZ, SUB, ZERO, MATH1, 4, 0);
	} else {
		MATHB(p, SEQINSZ, ADD, ONE, MATH1, 4, 0);
		MATHB(p, MATH1, SUB, ONE, MATH1, 4, 0);
		MATHB(p, ZERO, ADD, MATH1, VSEQINSZ, 4, 0);
		MOVE(p, MATH1, 0, MATH0, 0, 8, IMMED);

		/*
		 * Since MOVELEN is available only starting with
		 * SEC ERA 3, use poor man's MOVELEN: create a MOVE
		 * command dynamically by writing the length from M1 by
		 * OR-ing the command in the M1 register and MOVE the
		 * result into the descriptor buffer. Care must be taken
		 * wrt. the location of the command because of SEC
		 * pipelining. The actual MOVEs are written at the end
		 * of the descriptor due to calculations needed on the
		 * offset in the descriptor for the MOVE command.
		 */
		move_cmd_read_descbuf = MOVE(p, DESCBUF, 0, MATH0, 0, 6,
					     IMMED);
		move_cmd_write_descbuf = MOVE(p, MATH0, 0, DESCBUF, 0, 8,
					      WAITCOMP | IMMED);
	}
	MATHB(p, ZERO, ADD, MATH1, VSEQOUTSZ, 4, 0);

	switch (authdata->algtype) {
	case PDCP_AUTH_TYPE_NULL:
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
		if (rta_sec_era > RTA_SEC_ERA_2) {
			MOVE(p, AB1, 0, OFIFO, 0, MATH1, 0);
		} else {
			SET_LABEL(p, local_offset);

			/* Shut off automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_DISABLE_AUTO_NFIFO, 0, IMMED);

			/* Placeholder for MOVE command with length from M1
			 * register
			 */
			MOVE(p, IFIFOAB1, 0, OFIFO, 0, 0, IMMED);

			/* Enable automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_ENABLE_AUTO_NFIFO, 0, IMMED);
		}

		LOAD(p, (uintptr_t)iv, MATH0, 0, 8, IMMED | COPY);
		SEQFIFOLOAD(p, MSG1, 0, VLF | LAST1 | LAST2 | FLUSH1);
		SEQSTORE(p, MATH0, 0, 4, 0);

		break;

	case PDCP_AUTH_TYPE_SNOW:
		iv[0] = 0xFFFFFFFF;
		iv[1] = swap ? swab32(0x04000000) : 0x04000000;
		iv[2] = swap ? swab32(0xF8000000) : 0xF8000000;

		KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		LOAD(p, (uintptr_t)&iv, CONTEXT2, 0, 12, IMMED | COPY);
		ALG_OPERATION(p, OP_ALG_ALGSEL_SNOW_F9,
			      OP_ALG_AAI_F9,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_ENC);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);

		if (rta_sec_era > RTA_SEC_ERA_2) {
			MOVE(p, AB1, 0, OFIFO, 0, MATH1, 0);
		} else {
			SET_LABEL(p, local_offset);


			/* Shut off automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_DISABLE_AUTO_NFIFO, 0, IMMED);

			/* Placeholder for MOVE command with length from M1
			 * register
			 */
			MOVE(p, IFIFOAB1, 0, OFIFO, 0, 0, IMMED);

			/* Enable automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_ENABLE_AUTO_NFIFO, 0, IMMED);
		}
		SEQFIFOLOAD(p, MSGINSNOOP, 0, VLF | LAST1 | LAST2 | FLUSH1);
		SEQSTORE(p, CONTEXT2, 0, 4, 0);

		break;

	case PDCP_AUTH_TYPE_AES:
		iv[0] = 0xFFFFFFFF;
		iv[1] = swap ? swab32(0xFC000000) : 0xFC000000;
		iv[2] = 0x00000000; /* unused */

		KEY(p, KEY1, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		LOAD(p, (uintptr_t)&iv, MATH0, 0, 8, IMMED | COPY);
		MOVE(p, MATH0, 0, IFIFOAB1, 0, 8, IMMED);
		ALG_OPERATION(p, OP_ALG_ALGSEL_AES,
			      OP_ALG_AAI_CMAC,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_ENC);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);

		if (rta_sec_era > RTA_SEC_ERA_2) {
			MOVE(p, AB2, 0, OFIFO, 0, MATH1, 0);
		} else {
			SET_LABEL(p, local_offset);

			/* Shut off automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_DISABLE_AUTO_NFIFO, 0, IMMED);

			/* Placeholder for MOVE command with length from M1
			 * register
			 */
			MOVE(p, IFIFOAB2, 0, OFIFO, 0, 0, IMMED);

			/* Enable automatic Info FIFO entries */
			LOAD(p, 0, DCTRL, LDOFF_ENABLE_AUTO_NFIFO, 0, IMMED);
		}
		SEQFIFOLOAD(p, MSGINSNOOP, 0, VLF | LAST1 | LAST2 | FLUSH1);
		SEQSTORE(p, CONTEXT1, 0, 4, 0);

		break;

	case PDCP_AUTH_TYPE_ZUC:
		if (rta_sec_era < RTA_SEC_ERA_5) {
			pr_err("Invalid era for selected algorithm\n");
			return -ENOTSUP;
		}
		iv[0] = 0xFFFFFFFF;
		iv[1] = swap ? swab32(0xFC000000) : 0xFC000000;
		iv[2] = 0x00000000; /* unused */

		KEY(p, KEY2, authdata->key_enc_flags, authdata->key,
		    authdata->keylen, INLINE_KEY(authdata));
		LOAD(p, (uintptr_t)&iv, CONTEXT2, 0, 12, IMMED | COPY);
		ALG_OPERATION(p, OP_ALG_ALGSEL_ZUCA,
			      OP_ALG_AAI_F9,
			      OP_ALG_AS_INITFINAL,
			      ICV_CHECK_DISABLE,
			      DIR_ENC);
		SEQFIFOSTORE(p, MSG, 0, 0, VLF);
		MOVE(p, AB1, 0, OFIFO, 0, MATH1, 0);
		SEQFIFOLOAD(p, MSGINSNOOP, 0, VLF | LAST1 | LAST2 | FLUSH1);
		SEQSTORE(p, CONTEXT2, 0, 4, 0);

		break;

	default:
		pr_err("%s: Invalid integrity algorithm selected: %d\n",
		       "cnstr_shdsc_pdcp_short_mac", authdata->algtype);
		return -EINVAL;
	}


	if (rta_sec_era < RTA_SEC_ERA_3) {
		PATCH_MOVE(p, move_cmd_read_descbuf, local_offset);
		PATCH_MOVE(p, move_cmd_write_descbuf, local_offset);
	}

	return PROGRAM_FINALIZE(p);
}

#endif /* __DESC_PDCP_H__ */