summaryrefslogtreecommitdiff
path: root/drivers/staging/fsl_dpa_offload/dpa_ipsec_desc.c
blob: 0fac5ae1111651c4b066dc89347b6b6391362d10 (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
/* Copyright 2008-2012 Freescale Semiconductor, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Freescale Semiconductor nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *
 * ALTERNATIVELY, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") as published by the Free Software
 * Foundation, either version 2 of that License or (at your option) any
 * later version.
 *
 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <linux/version.h>
#include <linux/platform_device.h>

#include "compat.h"
#include "desc.h"
#include "error.h"
#include "jr.h"
#include "ctrl.h"

#include "dpa_ipsec_desc.h"

static struct device *get_jrdev(struct dpa_ipsec *dpa_ipsec);

/* If SEC ERA is unknown default to this value */
#define SEC_DEF_ERA	2 /* like in P4080 */

/*
 * to retrieve a 256 byte aligned buffer address from an address
 * we need to copy only the first 7 bytes
 */
#define ALIGNED_PTR_ADDRESS_SZ	(CAAM_PTR_SZ - 1)

#define JOB_DESC_HDR_LEN	CAAM_CMD_SZ
#define SEQ_OUT_PTR_SGF_MASK	0x01000000;
/* relative offset where the input pointer should be updated in the descriptor*/
#define IN_PTR_REL_OFF		4 /* words from current location */
/* dummy pointer value */
#define DUMMY_PTR_VAL		0x00000000
#define PTR_LEN			2	/* Descriptor is created only for 8 byte
					 * pointer. PTR_LEN is in words. */

/* retrieve and store SEC information */
int get_sec_info(struct dpa_ipsec *dpa_ipsec)
{
	struct device_node *sec_node;
	const u32 *sec_era;
	int prop_size;

	sec_node = of_find_node_with_property(NULL, "fsl,sec-era");
	if (sec_node) {
		sec_era = of_get_property(sec_node, "fsl,sec-era", &prop_size);
		if (sec_era && prop_size == sizeof(*sec_era) && *sec_era > 0)
			dpa_ipsec->sec_era = *sec_era;
		of_node_put(sec_node);
	}

	if (dpa_ipsec->sec_era == 0) {
		dpa_ipsec->sec_era = SEC_DEF_ERA;
		log_warn("Unable to acquire the SEC era from the device tree. Defaulting to SEC era %d.\n",
			dpa_ipsec->sec_era);
	}

	dpa_ipsec->jrdev = get_jrdev(dpa_ipsec);
	if (!dpa_ipsec->jrdev)
		return -ENODEV;

	return 0;
}


static struct device *get_jrdev(struct dpa_ipsec *dpa_ipsec)
{
	struct device *sec_jr_dev;

	if (!IS_ERR_OR_NULL(dpa_ipsec->jrdev))
		return dpa_ipsec->jrdev;

	sec_jr_dev = caam_jr_alloc();
	if (IS_ERR(sec_jr_dev)) {
		log_err("No available SEC job-ring\n");
		return NULL;
	}

	return sec_jr_dev;
}

static inline u32 get_ipsec_op_type(enum dpa_ipsec_direction sa_dir)
{
	return sa_dir == DPA_IPSEC_INBOUND ?  OP_TYPE_DECAP_PROTOCOL :
					      OP_TYPE_ENCAP_PROTOCOL;
}

static inline int get_cipher_params(enum dpa_ipsec_cipher_alg cipher_alg,
				    uint32_t *iv_length, uint32_t *icv_length,
				    uint32_t *max_pad_length)
{
	switch (cipher_alg) {
	case DPA_IPSEC_CIPHER_ALG_3DES_CBC_HMAC_96_MD5_128:
	case DPA_IPSEC_CIPHER_ALG_3DES_CBC_HMAC_96_SHA_160:
		*iv_length = 8;
		*max_pad_length = 8;
		*icv_length = 12;
		break;
	case DPA_IPSEC_CIPHER_ALG_3DES_CBC_HMAC_MD5_128:
		*iv_length = 8;
		*max_pad_length = 8;
		*icv_length = 16;
		break;
	case DPA_IPSEC_CIPHER_ALG_3DES_CBC_HMAC_SHA_160:
	case DPA_IPSEC_CIPHER_ALG_3DES_CBC_HMAC_SHA_256_128:
		*iv_length = 8;
		*max_pad_length = 8;
		*icv_length = 20;
		break;
	case DPA_IPSEC_CIPHER_ALG_3DES_CBC_HMAC_SHA_384_192:
		*iv_length = 8;
		*max_pad_length = 8;
		*icv_length = 24;
		break;
	case DPA_IPSEC_CIPHER_ALG_3DES_CBC_HMAC_SHA_512_256:
		*iv_length = 8;
		*max_pad_length = 8;
		*icv_length = 32;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CBC_HMAC_96_MD5_128:
	case DPA_IPSEC_CIPHER_ALG_AES_CBC_HMAC_96_SHA_160:
	case DPA_IPSEC_CIPHER_ALG_AES_CBC_AES_XCBC_MAC_96:
		*iv_length = 16;
		*max_pad_length = 16;
		*icv_length = 12;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CBC_HMAC_MD5_128:
	case DPA_IPSEC_CIPHER_ALG_AES_CBC_HMAC_SHA_256_128:
		*iv_length = 16;
		*max_pad_length = 16;
		*icv_length = 16;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CBC_HMAC_SHA_160:
		*iv_length = 16;
		*max_pad_length = 16;
		*icv_length = 20;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CBC_HMAC_SHA_384_192:
		*iv_length = 16;
		*max_pad_length = 16;
		*icv_length = 24;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CBC_HMAC_SHA_512_256:
		*iv_length = 16;
		*max_pad_length = 16;
		*icv_length = 32;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CTR_HMAC_96_MD5_128:
	case DPA_IPSEC_CIPHER_ALG_AES_CTR_HMAC_96_SHA_160:
	case DPA_IPSEC_CIPHER_ALG_AES_CTR_AES_XCBC_MAC_96:
		*iv_length = 16;
		*max_pad_length = 16;
		*icv_length = 12;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CTR_HMAC_MD5_128:
	case DPA_IPSEC_CIPHER_ALG_AES_CTR_HMAC_SHA_256_128:
		*iv_length = 8;
		*max_pad_length = 4;
		*icv_length = 16;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CTR_HMAC_SHA_160:
		*iv_length = 8;
		*max_pad_length = 4;
		*icv_length = 20;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CTR_HMAC_SHA_384_192:
		*iv_length = 8;
		*max_pad_length = 4;
		*icv_length = 24;
		break;
	case DPA_IPSEC_CIPHER_ALG_AES_CTR_HMAC_SHA_512_256:
		*iv_length = 8;
		*max_pad_length = 4;
		*icv_length = 32;
		break;
	default:
		*iv_length = 0;
		*icv_length = 0;
		*max_pad_length = 0;
		log_err("Unsupported cipher suite %d\n", cipher_alg);
		return -EINVAL;
	}

	return 0;
}

static inline void build_stats_descriptor_part(struct dpa_ipsec_sa *sa,
					       size_t pdb_len)
{
	u32 *desc, *padding_jump;
	u32 block_size, stats_offset, offset;

	BUG_ON(!sa);

	desc = (u32 *) sa->sec_desc->desc;

	stats_offset = sizeof(sa->sec_desc->hdr_word) + pdb_len -
		       DPA_IPSEC_STATS_LEN * sizeof(u32);
	sa->stats_offset = stats_offset;
	memset((u8 *)desc + stats_offset, 0, DPA_IPSEC_STATS_LEN * sizeof(u32));

	/* Copy from descriptor to MATH REG 0 the current statistics */
	append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_MATH0 | MOVE_WAITCOMP |
		    (stats_offset << MOVE_OFFSET_SHIFT) | sizeof(u64));

	/* Load 1 in MATH REG 1 */
	append_math_add(desc, REG1, ZERO, ONE, MATH_LEN_8BYTE);

	/*
	 * Perform 32-bit left shift of DEST and concatenate with left 32 bits
	 * of SRC1 i.e MATH REG 1 = 0x00000001_00000000
	 */
	append_math_shld(desc, REG1, REG0, REG1, MATH_LEN_8BYTE);

	if (sa->sa_dir == DPA_IPSEC_INBOUND) {
		/* MATH REG 2 = Sequence in length */
		append_math_add_imm_u32(desc, REG2, SEQINLEN, IMM, 0);
		goto after_padding;
	} else {
		/* MATH REG 2 = Sequence in length + 2 */
		append_math_add_imm_u32(desc, REG2, SEQINLEN, IMM, 2);
	}

	switch (sa->cipher_data.cipher_type) {
	case OP_PCL_IPSEC_3DES:
		block_size = 8; /* block size in bytes */
		break;
	case OP_PCL_IPSEC_AES_CBC:
	case OP_PCL_IPSEC_AES_CTR:
	case OP_PCL_IPSEC_AES_XTS:
	case OP_PCL_IPSEC_AES_CCM8:
	case OP_PCL_IPSEC_AES_CCM12:
	case OP_PCL_IPSEC_AES_CCM16:
	case OP_PCL_IPSEC_AES_GCM8:
	case OP_PCL_IPSEC_AES_GCM12:
	case OP_PCL_IPSEC_AES_GCM16:
		block_size = 16; /* block size in bytes */
		break;
	default:
		pr_crit("Invalid cipher algorithm for SA %d\n", sa->id);
		return;
	}

	/* Adding padding to byte counter */
	append_math_and_imm_u32(desc, REG3, REG2, IMM, block_size - 1);

	/* Previous operation result is 0 i.e padding added to bytes count */
	padding_jump = append_jump(desc, CLASS_BOTH | JUMP_TEST_ALL |
				   JUMP_COND_MATH_Z);

	/* MATH REG 2 = MATH REG 2 + 1 */
	append_math_add(desc, REG2, REG2, ONE, MATH_LEN_8BYTE);

	/* jump back to adding padding i.e jump back 4 words */
	offset = (-4) & 0x000000FF;
	append_jump(desc, (offset << JUMP_OFFSET_SHIFT));

	set_jump_tgt_here(desc, padding_jump);
	/* Done adding padding to byte counter */

after_padding:
	/* MATH REG 1  = MATH REG 1 + MATH REG 2 */
	append_math_add(desc, REG1, REG1, REG2, MATH_LEN_8BYTE);

	/* MATH REG0 = MATH REG 0 + MATH REG1 */
	append_math_add(desc, REG0, REG0, REG1, MATH_LEN_8BYTE);

	/* Store in the descriptor but not in external memory */
	append_move(desc, MOVE_SRC_MATH0 | MOVE_DEST_DESCBUF | MOVE_WAITCOMP |
		    (stats_offset << MOVE_OFFSET_SHIFT) | sizeof(u64));
}

static inline void save_stats_in_external_mem(struct dpa_ipsec_sa *sa)
{
	u32 *desc;
	u32 stats_offset;

	desc = (u32 *) sa->sec_desc->desc;

	/* statistics offset = predetermined offset */
	stats_offset = sa->stats_offset;

	/* Store command: in the case of the Descriptor Buffer the length
	 * is specified in 4-byte words, but in all other cases the length
	 * is specified in bytes. Offset in 4 byte words */
	append_store(desc, 0, DPA_IPSEC_STATS_LEN, LDST_CLASS_DECO |
		     ((stats_offset / 4) << LDST_OFFSET_SHIFT) |
		     LDST_SRCDST_WORD_DESCBUF_SHARED);

	/* Jump with CALM to be sure previous operation was finished */
	append_jump(desc, JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT));
}

/* insert a cmd in the desc at a given index and optionally update desc len */
static void insert_sec_cmd(uint32_t *desc, uint32_t index, uint32_t cmd,
			  bool update_len)
{
	uint32_t *desc_cmd;

	desc_cmd = desc + index;
	*desc_cmd = cmd;

	if (update_len)
		(*desc)++;
}

/* insert cmds for SEQ_IN/OUT_PTR copy with specified offset (shr_desc_len) */
static void insert_ptr_copy_cmds(uint32_t *desc, uint32_t index,
				 uint32_t shr_desc_len, bool update_desc_len)
{
	uint32_t cmd, off, len;

	/*
	 * insert the commands at the specified index
	 * if index == 0 insert at next position in desc
	 */
	if (!index)
		index = desc_len(desc);

	/*
	 * move out ptr (from job desc) to math reg 1 & 2, except the last byte;
	 * assuming all buffers are 256 bits aligned, setting the last address
	 * byte to 0x00  will give the buffer address;
	 */
	off = CAAM_PTR_SZ;
	off = (shr_desc_len * CAAM_CMD_SZ + off) << MOVE_OFFSET_SHIFT;
	len = CAAM_CMD_SZ + JOB_DESC_HDR_LEN + ALIGNED_PTR_ADDRESS_SZ;
	cmd = CMD_MOVE | MOVE_SRC_DESCBUF | MOVE_DEST_MATH1 | off | len;
	insert_sec_cmd(desc, index, cmd, update_desc_len);

	/*
	 * move in ptr (from job desc) to math reg 0, except the last byte;
	 * assuming all buffers are 256 bits aligned, setting the last address
	 * byte to 0x00  will give the buffer address;
	 */
	off = JOB_DESC_HDR_LEN + 3 * CAAM_CMD_SZ + 2 * CAAM_PTR_SZ;
	off = (shr_desc_len * CAAM_CMD_SZ + off) << MOVE_OFFSET_SHIFT;
	len = ALIGNED_PTR_ADDRESS_SZ;
	cmd = CMD_MOVE | MOVE_SRC_DESCBUF | MOVE_DEST_MATH0 | off | len;
	insert_sec_cmd(desc, ++index, cmd, update_desc_len);
}

/* build the command set for copying the frame meta data */
static void build_meta_data_desc_cmds(struct dpa_ipsec_sa *sa,
				      unsigned int sec_era,
				      unsigned int move_size)
{
	uint32_t *desc, off, len, opt, *no_sg_jump;
	uint32_t sg_mask = SEQ_OUT_PTR_SGF_MASK;

	BUG_ON(!sa);

	desc = (uint32_t *) sa->sec_desc->desc;

	/* insert cmds to copy SEQ_IN/OUT_PTR - offset will be updated later */
	insert_ptr_copy_cmds(desc, 0, 0, true);

	/* detect & handle scatter / gather frames */

	/*
	 * the SEQ OUT PTR command is now in math reg 1, so the SGF bit can be
	 * checked using a math command;
	 */
	append_math_and_imm_u32(desc, NONE, REG1, IMM, sg_mask);

	opt = CLASS_NONE | JUMP_TYPE_LOCAL | JUMP_COND_MATH_Z | JUMP_TEST_ALL;
	no_sg_jump = append_jump(desc, opt);

	if (sec_era == 2) {
		/* disable iNFO FIFO entries for p4080rev2 & ??? */
		len = 0x10 << LDST_LEN_SHIFT;
		append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO | len);

		/*
		 * load in IN FIFO the S/G Entry located in the 5th reg after
		 * MATH3 -> offset = sizeof(GT_REG) * 4 + offset_math3_to_GT_REG
		 * len = sizeof(S/G entry)
		 */
		opt = MOVE_SRC_MATH3 | MOVE_DEST_CLASS1INFIFO;
		off = 127 << MOVE_OFFSET_SHIFT;
		len = 49 << MOVE_LEN_SHIFT;
		append_move(desc, opt | off | len);

		/* enable iNFO FIFO entries */
		append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
	} else {
		/* ????? */
		opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
		len = 0x10 << LDST_LEN_SHIFT;
		append_cmd(desc, CMD_LOAD | opt | len);

		/*
		 * load in IN FIFO the S/G Entry located in the 5th reg after
		 * MATH3 -> offset = sizeof(GT_REG) * 4 + offset_math3_to_GT_REG
		 * len = sizeof(S/G entry)
		 */
		opt = MOVE_SRC_MATH3 | MOVE_DEST_INFIFO_NOINFO;
		off = 127 << MOVE_OFFSET_SHIFT;
		len = 49 << MOVE_LEN_SHIFT;
		append_move(desc, opt | off | len);
	}

	/*
	 * throw away the first part of the S/G table and keep only the buffer
	 * address;
	 * offset = undefined memory after MATH3;
	 * len =
	 */
	opt = MOVE_SRC_INFIFO | MOVE_DEST_MATH3;
	off = 8 << MOVE_OFFSET_SHIFT;
	len = 41 << MOVE_LEN_SHIFT;
	append_move(desc, opt | off | len);

	/* put the buffer address (still in the IN FIFO) in MATH2 */
	opt = MOVE_SRC_INFIFO | MOVE_DEST_MATH2;
	off = 0 << MOVE_OFFSET_SHIFT;
	len = 8 << MOVE_LEN_SHIFT;
	append_move(desc, opt | off | len);

	/* update no S/G jump location */
	set_jump_tgt_here(desc, no_sg_jump);

	/* save input pointer to predefined location in descriptor */
	opt = MOVE_SRC_MATH0 | MOVE_DEST_DESCBUF;
	off = ((desc_len(desc) + IN_PTR_REL_OFF) << 2) << MOVE_OFFSET_SHIFT;
	len = ALIGNED_PTR_ADDRESS_SZ << MOVE_LEN_SHIFT;
	append_move(desc, opt | off | len);

	/* save output pointer to predefined location in descriptor */
	opt = MOVE_WAITCOMP | MOVE_SRC_MATH2 | MOVE_DEST_DESCBUF;
	off += (CAAM_PTR_SZ +  2 * CAAM_CMD_SZ) << MOVE_OFFSET_SHIFT;
	len = ALIGNED_PTR_ADDRESS_SZ << MOVE_LEN_SHIFT;
	append_move(desc, opt | off | len);

	/* fix LIODN */
	opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
	off = 0x40 << LDST_OFFSET_SHIFT; /* SEQ LIODN */
	append_cmd(desc, CMD_LOAD | opt | off);

	/* actual move commands - pointers will be updated at runtime */

	/* load the data to be moved - insert dummy pointer */
	opt = LDST_CLASS_2_CCB | LDST_SRCDST_WORD_CLASS_CTX;
	off = 0 << LDST_OFFSET_SHIFT;
	len = move_size << LDST_LEN_SHIFT;
	append_load(desc, DUMMY_PTR_VAL, len, opt | off);

	/* wait for completion */
	opt = JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT);
	append_jump(desc, opt);

	/* store the data to the output fifo - insert dummy pointer */
	opt = LDST_CLASS_2_CCB | LDST_SRCDST_WORD_CLASS_CTX;
	off = 0 << LDST_OFFSET_SHIFT;
	len = move_size << LDST_LEN_SHIFT;
	append_store(desc, DUMMY_PTR_VAL, len, opt | off);

	/* fix LIODN */
	opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
	off = 0x80 << LDST_OFFSET_SHIFT; /* NON_SEQ LIODN */
	append_cmd(desc, CMD_LOAD | opt | off);
}

int build_shared_descriptor(struct dpa_ipsec_sa *sa,
			    dma_addr_t auth_key_dma,
			    dma_addr_t crypto_key_dma, u32 bytes_to_copy)
{
	uint32_t *desc, *key_jump_cmd, copy_ptr_index = 0;
	int opthdrsz;
	size_t pdb_len = 0;

	desc = (u32 *) sa->sec_desc->desc;

	/* Reserve 2 words for statistics */
	if (sa->enable_stats)
		pdb_len = DPA_IPSEC_STATS_LEN * sizeof(u32);

	if (sa->sa_dir == DPA_IPSEC_OUTBOUND) {
		/* Compute optional header size, rounded up to descriptor
		 * word size */
		opthdrsz = (sa->sec_desc->pdb_en.ip_hdr_len + 3) & ~3;
		pdb_len += sizeof(struct ipsec_encap_pdb) + opthdrsz;
		init_sh_desc_pdb(desc, HDR_SAVECTX | HDR_SHARE_SERIAL, pdb_len);
	} else {
		pdb_len += sizeof(struct ipsec_decap_pdb);
		init_sh_desc_pdb(desc, HDR_SAVECTX | HDR_SHARE_SERIAL, pdb_len);
	}

	/* Key jump */
	key_jump_cmd = append_jump(desc, CLASS_BOTH | JUMP_TEST_ALL |
				   JUMP_COND_SHRD | JUMP_COND_SELF);

	/* check whether a split of a normal key is used */
	if (sa->auth_data.split_key_len)
		/* Append split authentication key */
		append_key(desc, auth_key_dma, sa->auth_data.split_key_len,
			   CLASS_2 | KEY_ENC | KEY_DEST_MDHA_SPLIT);
	else
		/* Append normal authentication key */
		append_key(desc, auth_key_dma, sa->auth_data.auth_key_len,
			   CLASS_2 | KEY_DEST_CLASS_REG);

	/* Append cipher key */
	append_key(desc, crypto_key_dma, sa->cipher_data.cipher_key_len,
		   CLASS_1 | KEY_DEST_CLASS_REG);

	set_jump_tgt_here(desc, key_jump_cmd);

	/* copy frame meta data (IC) to enable DSCP / ECN propagation */
	if (sa->dscp_copy || sa->ecn_copy) {
		/* save location of ptr copy commands to update offset later */
		copy_ptr_index = desc_len(desc);
		build_meta_data_desc_cmds(sa, sa->dpa_ipsec->sec_era, 64);
	}

	if (bytes_to_copy == 0)
		goto skip_byte_copy;

	/* Copy L2 header from the original packet to the outer packet */

	/* ld: deco-deco-ctrl len=0 offs=8 imm -auto-nfifo-entries */
	append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);

	/* seqfifold: both msgdata-last2-last1-flush1 len=4 */
	append_seq_fifo_load(desc, bytes_to_copy, FIFOLD_TYPE_MSG |
			     FIFOLD_CLASS_BOTH | FIFOLD_TYPE_LAST1 |
			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_FLUSH1);

	/* ld: deco-deco-ctrl len=0 offs=4 imm +auto-nfifo-entries */
	append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);

	/* move: ififo->deco-alnblk -> ofifo, len=4 */
	append_move(desc, MOVE_SRC_INFIFO | MOVE_DEST_OUTFIFO | bytes_to_copy);

	/* seqfifostr: msgdata len=4 */
	append_seq_fifo_store(desc, FIFOST_TYPE_MESSAGE_DATA, bytes_to_copy);

	/* Done coping L2 header from the original packet to the outer packet */

skip_byte_copy:

	if (sa->enable_stats)
		build_stats_descriptor_part(sa, pdb_len);

	/* Protocol specific operation */
	append_operation(desc, OP_PCLID_IPSEC |
			 get_ipsec_op_type(sa->sa_dir) |
			 sa->cipher_data.cipher_type | sa->auth_data.auth_type);

	if (sa->enable_stats)
		save_stats_in_external_mem(sa);

	if (sa->dscp_copy || sa->ecn_copy)
		/* insert cmds to copy SEQ_IN/OUT_PTR - with updated offset */
		insert_ptr_copy_cmds(desc, copy_ptr_index,
				     desc_len(desc), false);

	if (desc_len(desc) >= MAX_CAAM_SHARED_DESCSIZE) {
		if (sa->enable_stats)
			memset((uint8_t *)desc + sa->stats_offset, 0,
				MAX_CAAM_DESCSIZE * sizeof(u32) -
				sa->stats_offset);
		return -EPERM;
	}

	return 0;
}

/* Move size should be set to 64 bytes */
int built_encap_extra_material(struct dpa_ipsec_sa *sa,
			       dma_addr_t auth_key_dma,
			       dma_addr_t crypto_key_dma,
			       unsigned int move_size)
{
	uint32_t *extra_cmds, *padding_jump, *key_jump_cmd;
	uint32_t len, off_b, off_w, off, opt;
	unsigned char job_desc_len, block_size;

	/*
	 * sec_desc_extra_cmds is the address were the first SEC extra command
	 * is located, from here SEC will overwrite Job descriptor part. Need
	 * to insert a dummy command because the LINUX CAAM API uses first word
	 * for storing the length of the descriptor.
	 */
	extra_cmds = sa->sec_desc_extra_cmds - 1;

	/*
	 * Dummy command - will not be executed at all. Only for setting to 1
	 * the length of the extra_cmds descriptor so that first extra material
	 * command will be located exactly at sec_desc_extra_cmds address.
	 */
	append_cmd(extra_cmds, 0xdead0000);

	/* Start Extra Material Group 1 */
	/* Load from the input address 64 bytes into internal register */
	/* load the data to be moved - insert dummy pointer */
	opt = LDST_CLASS_2_CCB | LDST_SRCDST_WORD_CLASS_CTX;
	off = 0 << LDST_OFFSET_SHIFT;
	len = move_size << LDST_LEN_SHIFT;
	append_load(extra_cmds, DUMMY_PTR_VAL, len, opt | off);

	/* Wait to finish previous operation */
	opt = JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT);
	append_jump(extra_cmds, opt);

	/* Store the data to the output FIFO - insert dummy pointer */
	opt = LDST_CLASS_2_CCB | LDST_SRCDST_WORD_CLASS_CTX;
	off = 0 << LDST_OFFSET_SHIFT;
	len = move_size << LDST_LEN_SHIFT;
	append_store(extra_cmds, DUMMY_PTR_VAL, len, opt | off);

	/* Fix LIODN */
	opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
	off = 0x80 << LDST_OFFSET_SHIFT; /* NON_SEQ LIODN */
	append_cmd(extra_cmds, CMD_LOAD | opt | off);

	/* MATH0 += 1 (packet counter) */
	append_math_add(extra_cmds, REG0, REG0, ONE, MATH_LEN_8BYTE);

	/* Overwrite the job-desc location (word 51 or 53) with the second
	 * group (10 words) */
	job_desc_len = sa->job_desc_len;
	opt   = MOVE_SRC_INFIFO | MOVE_DEST_DESCBUF | MOVE_WAITCOMP;
	off_w = MAX_CAAM_DESCSIZE - job_desc_len;
	off_b = off_w * sizeof(uint32_t); /* calculate off in bytes */
	len   = (10 * sizeof(uint32_t)) << MOVE_LEN_SHIFT;
	append_move(extra_cmds, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/*
	 * Jump to the beginning of the JOB Descriptor to start executing
	 * the extra material group 2
	 */
	append_cmd(extra_cmds, 0xa00000f6);

	/* End of Extra Material Group 1 */

	/* Start Extra Material Group 2 */
	/* MATH REG 2 = Sequence in length + 2; 2 for pad-len and NH field */
	append_math_add_imm_u32(extra_cmds, REG2, SEQINLEN, IMM, 2);

	switch (sa->cipher_data.cipher_type) {
	case OP_PCL_IPSEC_3DES:
		block_size = 8; /* block size in bytes */
		break;
	case OP_PCL_IPSEC_AES_CBC:
	case OP_PCL_IPSEC_AES_CTR:
	case OP_PCL_IPSEC_AES_XTS:
	case OP_PCL_IPSEC_AES_CCM8:
	case OP_PCL_IPSEC_AES_CCM12:
	case OP_PCL_IPSEC_AES_CCM16:
	case OP_PCL_IPSEC_AES_GCM8:
	case OP_PCL_IPSEC_AES_GCM12:
	case OP_PCL_IPSEC_AES_GCM16:
		block_size = 16; /* block size in bytes */
		break;
	default:
		pr_crit("Invalid cipher algorithm for SA %d\n", sa->id);
		return -EINVAL;
	}

	/* Adding padding to byte counter */
	append_math_and_imm_u32(extra_cmds, REG3, REG2, IMM, block_size - 1);

	/* Previous operation result is 0 i.e padding added to bytes count */
	padding_jump = append_jump(extra_cmds, CLASS_BOTH | JUMP_TEST_ALL |
				   JUMP_COND_MATH_Z);

	/* MATH REG 2 = MATH REG 2 + 1 */
	append_math_add(extra_cmds, REG2, REG2, ONE, MATH_LEN_4BYTE);

	/* jump back to adding padding i.e jump back 4 words */
	off = (-4) & 0x000000FF;
	append_jump(extra_cmds, (off << JUMP_OFFSET_SHIFT));

	set_jump_tgt_here(extra_cmds, padding_jump);
	/* Done adding padding to byte counter */

	/*
	 * Perform 32-bit left shift of DEST and concatenate with left 32 bits
	 * of SRC1 i.e MATH REG 2 = 0x00bytecount_00000000
	 */
	append_math_shld(extra_cmds, REG2, REG0, REG2, MATH_LEN_8BYTE);

	/* MATH REG 0  = MATH REG 0 + MATH REG 2 */
	append_math_add(extra_cmds, REG0, REG0, REG2, MATH_LEN_8BYTE);

	/*
	 * Overwrite the job-desc location (word 51 or 53) with the third
	 * group (11 words)
	 */
	opt   = MOVE_SRC_INFIFO | MOVE_DEST_DESCBUF | MOVE_WAITCOMP;
	off_w = MAX_CAAM_DESCSIZE - job_desc_len;
	off_b = off_w * sizeof(uint32_t); /* calculate off in bytes */
	len   = (11 * sizeof(uint32_t)) << MOVE_LEN_SHIFT;
	append_move(extra_cmds, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/*
	 * Jump to the beginning of the JOB Descriptor to start executing
	 * the extra material group 3. The command for jumping back is already
	 * here from extra material group 1
	 */

	/* End of Extra Material Group 2 */

	/* Start Extra Material Group 3 */

	if (sa->enable_stats) {
		/* Store statistics in the CAAM internal descriptor */
		off_b = sa->stats_indx * CAAM_CMD_SZ;
		append_move(extra_cmds, MOVE_SRC_MATH0 | MOVE_DEST_DESCBUF |
			    (off_b << MOVE_OFFSET_SHIFT) |
			    sizeof(uint64_t));
	} else {
		/* Statistics are disabled. Do not update descriptor counter */
		append_cmd(extra_cmds, 0xA0000001); /* NOP for SEC */
	}

	/* Key jump */
	key_jump_cmd = append_jump(extra_cmds, CLASS_BOTH | JUMP_TEST_ALL |
				   JUMP_COND_SHRD);

	/* check whether a split of a normal key is used */
	if (sa->auth_data.split_key_len)
		/* Append split authentication key */
		append_key(extra_cmds, auth_key_dma,
			   sa->auth_data.split_key_len,
			   CLASS_2 | KEY_ENC | KEY_DEST_MDHA_SPLIT);
	else
		/* Append normal authentication key */
		append_key(extra_cmds, auth_key_dma, sa->auth_data.auth_key_len,
			   CLASS_2 | KEY_DEST_CLASS_REG);

	/* Append cipher key */
	append_key(extra_cmds, crypto_key_dma, sa->cipher_data.cipher_key_len,
		   CLASS_1 | KEY_DEST_CLASS_REG);

	set_jump_tgt_here(extra_cmds, key_jump_cmd);

	/* Protocol specific operation */
	append_operation(extra_cmds, OP_PCLID_IPSEC | OP_TYPE_ENCAP_PROTOCOL |
			 sa->cipher_data.cipher_type | sa->auth_data.auth_type);

	if (sa->enable_stats) {
		/*
		 * Store command: in the case of the Descriptor Buffer the
		 * length is specified in 4-byte words, but in all other cases
		 * the length is specified in bytes. Offset in 4 byte words
		 */
		off_w = sa->stats_indx;
		append_store(extra_cmds, 0, DPA_IPSEC_STATS_LEN,
			     LDST_CLASS_DECO | (off_w << LDST_OFFSET_SHIFT) |
			     LDST_SRCDST_WORD_DESCBUF_SHARED);
	} else {
		/* Do not store lifetime counter in external memory */
		append_cmd(extra_cmds, 0xA0000001); /* NOP for SEC */
	}

	/* Jump with CALM to be sure previous operation was finished */
	append_jump(extra_cmds, JUMP_TYPE_HALT_USER | JUMP_COND_CALM);

	/* End of Extra Material Group 3 */

	return 0;
}

/* Move size should be set to 64 bytes */
void built_decap_extra_material(struct dpa_ipsec_sa *sa,
			       dma_addr_t auth_key_dma,
			       dma_addr_t crypto_key_dma)
{
	uint32_t *extra_cmds;
	uint32_t off_b, off_w, data;

	/*
	 * sec_desc_extra_cmds is the address were the first SEC extra command
	 * is located, from here SEC will overwrite Job descriptor part. Need
	 * to insert a dummy command because the LINUX CAAM API uses first word
	 * for storing the length of the descriptor.
	 */
	extra_cmds = sa->sec_desc_extra_cmds - 1;

	/*
	 * Dummy command - will not be executed at all. Only for setting to 1
	 * the length of the extra_cmds descriptor so that first extra material
	 * command will be located exactly at sec_desc_extra_cmds address.
	 */
	append_cmd(extra_cmds, 0xdead0000);

	data = 16;
	append_math_rshift_imm_u64(extra_cmds, REG2, REG2, IMM, data);

	/* math: (math1 - math2)->math1 len=8 */
	append_math_sub(extra_cmds, REG1, REG1, REG2, MATH_LEN_8BYTE);

	/* math: (math0 + 1)->math0 len=8 */
	append_math_add(extra_cmds, REG0, REG0, ONE, MATH_LEN_8BYTE);

	append_math_shld(extra_cmds, REG1, REG0, REG1, MATH_LEN_8BYTE);

	append_math_add(extra_cmds, REG0, REG0, REG1, MATH_LEN_8BYTE);

	append_cmd(extra_cmds, 0x7883c824);

	/* Store in the descriptor but not in external memory */
	off_b = sa->stats_offset;
	append_move(extra_cmds, MOVE_SRC_MATH0 | MOVE_DEST_DESCBUF |
		    MOVE_WAITCOMP | (off_b << MOVE_OFFSET_SHIFT) | sizeof(u64));

	append_cmd(extra_cmds, 0xa70040fe);

	append_cmd(extra_cmds, 0xa00000f7);

	/* check whether a split of a normal key is used */
	if (sa->auth_data.split_key_len)
		/* Append split authentication key */
		append_key(extra_cmds, auth_key_dma,
			   sa->auth_data.split_key_len,
			   CLASS_2 | KEY_ENC | KEY_DEST_MDHA_SPLIT);
	else
		/* Append normal authentication key */
		append_key(extra_cmds, auth_key_dma, sa->auth_data.auth_key_len,
			   CLASS_2 | KEY_DEST_CLASS_REG);

	/* Append cipher key */
	append_key(extra_cmds, crypto_key_dma, sa->cipher_data.cipher_key_len,
		   CLASS_1 | KEY_DEST_CLASS_REG);

	/* Protocol specific operation */
	append_operation(extra_cmds, OP_PCLID_IPSEC | OP_TYPE_DECAP_PROTOCOL |
			 sa->cipher_data.cipher_type | sa->auth_data.auth_type);

	/*
	 * Store command: in the case of the Descriptor Buffer the length
	 * is specified in 4-byte words, but in all other cases the length
	 * is specified in bytes. Offset in 4 byte words
	 */
	off_w = sa->stats_indx;
	append_store(extra_cmds, 0, DPA_IPSEC_STATS_LEN,
		     LDST_CLASS_DECO | (off_w << LDST_OFFSET_SHIFT) |
		     LDST_SRCDST_WORD_DESCBUF_SHARED);

	append_jump(extra_cmds, JUMP_TYPE_HALT_USER | JUMP_COND_CALM);
}

int build_extended_encap_shared_descriptor(struct dpa_ipsec_sa *sa,
				     dma_addr_t auth_key_dma,
				     dma_addr_t crypto_key_dma,
				     uint32_t bytes_to_copy,
				     int sec_era)
{
	uint32_t *desc, *no_sg_jump, *extra_cmds;
	uint32_t len, off_b, off_w, opt, stats_off_b, sg_mask;
	struct device *jrdev;
	unsigned int extra_cmds_len;
	unsigned char job_desc_len;
	dma_addr_t dma_extra_cmds;
	int ret;

	desc = (uint32_t *)sa->sec_desc->desc;

	if (sec_era == 2) {
		if (sa->enable_stats)
			sa->stats_indx = 27;
		sa->next_cmd_indx = 29;
	} else {
		if (sa->enable_stats)
			sa->stats_indx = 28;
		sa->next_cmd_indx = 30;
	}

	/* This code only works when SEC is configured to use PTR on 64 bit
	 * so the Job Descriptor length is 13 words long when DPOWRD is set */
	job_desc_len = 13;

	/* Set CAAM Job Descriptor length */
	sa->job_desc_len = job_desc_len;

	/* Set lifetime counter stats offset */
	sa->stats_offset = sa->stats_indx * sizeof(uint32_t);

	ret = built_encap_extra_material(sa, auth_key_dma, crypto_key_dma, 64);
	if (ret < 0) {
		log_err("Failed to create extra CAAM commands\n");
		return -EAGAIN;
	}

	extra_cmds = sa->sec_desc_extra_cmds - 1;
	extra_cmds_len = desc_len(extra_cmds) - 1;

	/* get the jr device  */
	jrdev = get_jrdev(sa->dpa_ipsec);
	if (!jrdev) {
		log_err("Failed to get the job ring device, check the dts\n");
		return -EINVAL;
	}

	dma_extra_cmds = dma_map_single(jrdev, sa->sec_desc_extra_cmds,
					extra_cmds_len * sizeof(uint32_t),
					DMA_TO_DEVICE);
	if (!dma_extra_cmds) {
		log_err("Could not DMA map extra CAAM commands\n");
		return -ENXIO;
	}

	init_sh_desc_pdb(desc, HDR_SAVECTX | HDR_SHARE_SERIAL,
			 (sa->next_cmd_indx - 1) * sizeof(uint32_t));

	if (sec_era == 2) {
		/* disable iNFO FIFO entries for p4080rev2 & ??? */
		len = 0x10 << LDST_LEN_SHIFT;
		append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO | len);

		/*
		 * load in IN FIFO the S/G Entry located in the 5th reg after
		 * MATH3 -> offset = sizeof(GT_REG) * 4 + offset_math3_to_GT_REG
		 * len = sizeof(S/G entry)
		 * Offset refers to SRC
		 */
		opt   = MOVE_SRC_MATH3 | MOVE_DEST_CLASS1INFIFO;
		off_b = 127 << MOVE_OFFSET_SHIFT;
		len   = 49 << MOVE_LEN_SHIFT;
		append_move(desc, opt | off_b | len);

		/*
		 * L2 part 1
		 * Load from input packet to INPUT DATA FIFO first bytes_to_copy
		 * bytes.
		 */
		append_seq_fifo_load(desc, bytes_to_copy, FIFOLD_TYPE_MSG |
				     FIFOLD_CLASS_BOTH | FIFOLD_TYPE_LAST1 |
				     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_FLUSH1);

		/*
		 * Extra word part 1
		 * Load extra words for this descriptor into the INPUT DATA FIFO
		 */
		append_fifo_load(desc, dma_extra_cmds,
				 extra_cmds_len * sizeof(uint32_t),
				 FIFOLD_TYPE_MSG | FIFOLD_CLASS_BOTH |
				 FIFOLD_TYPE_LAST1 | FIFOLD_TYPE_LAST2 |
				 FIFOLD_TYPE_FLUSH1);

		/* enable iNFO FIFO entries */
		append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
	} else {
		/* ????? */
		opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
		len = 0x10 << LDST_LEN_SHIFT;
		append_cmd(desc, CMD_LOAD | opt | len);

		/*
		 * load in IN FIFO the S/G Entry located in the 5th reg after
		 * MATH3 -> offset = sizeof(GT_REG) * 4 + offset_math3_to_GT_REG
		 * len = sizeof(S/G entry)
		 */
		opt   = MOVE_SRC_MATH3 | MOVE_DEST_INFIFO_NOINFO;
		off_b = 127 << MOVE_OFFSET_SHIFT;
		len   = 49 << MOVE_LEN_SHIFT;
		append_move(desc, opt | off_b | len);

		/*
		 * L2 part 1
		 * Load from input packet to INPUT DATA FIFO first bytes_to_copy
		 * bytes. No information FIFO entry even if automatic
		 * iNformation FIFO entries are enabled.
		 */
		append_seq_fifo_load(desc, bytes_to_copy, FIFOLD_CLASS_BOTH |
				     FIFOLD_TYPE_NOINFOFIFO);

		/*
		 * Extra word part 1
		 * Load extra words for this descriptor into the INPUT DATA FIFO
		 */
		append_fifo_load(desc, dma_extra_cmds,
				 extra_cmds_len * sizeof(uint32_t),
				 FIFOLD_CLASS_BOTH | FIFOLD_TYPE_NOINFOFIFO);
	}

	/*
	 * throw away the first part of the S/G table and keep only the buffer
	 * address;
	 * offset = undefined memory after MATH3; Refers to the destination.
	 * len = 41 bytes to discard
	 */
	opt   = MOVE_SRC_INFIFO | MOVE_DEST_MATH3;
	off_b = 8 << MOVE_OFFSET_SHIFT;
	len   = 41 << MOVE_LEN_SHIFT;
	append_move(desc, opt | off_b | len);

	/* put the buffer address (still in the IN FIFO) in MATH2 */
	opt   = MOVE_SRC_INFIFO | MOVE_DEST_MATH3;
	off_b = 0 << MOVE_OFFSET_SHIFT;
	len   = 8 << MOVE_LEN_SHIFT;
	append_move(desc, opt | off_b | len);

	/* copy 15 bytes starting at 4 bytes before the OUT-PTR-CMD in
	 * the job-desc into math1
	 * i.e. in the low-part of math1 we have the out-ptr-cmd and
	 * in the math2 we will have the address of the out-ptr
	 */
	opt = MOVE_SRC_DESCBUF | MOVE_DEST_MATH1;
	off_b = (MAX_CAAM_DESCSIZE - job_desc_len + PTR_LEN) * sizeof(uint32_t);
	len = (8 + 4 * PTR_LEN - 1) << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/* Copy 7 bytes of the in-ptr into math0 */
	opt   = MOVE_SRC_DESCBUF | MOVE_DEST_MATH0;
	off_w = MAX_CAAM_DESCSIZE - job_desc_len + 1 + 3 + 2 * PTR_LEN;
	off_b = off_w * sizeof(uint32_t); /* calculate off in bytes */
	len   = ALIGNED_PTR_ADDRESS_SZ << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/*
	 * the SEQ OUT PTR command is now in math reg 1, so the SGF bit can be
	 * checked using a math command;
	 */
	sg_mask = SEQ_OUT_PTR_SGF_MASK;
	append_math_and_imm_u32(desc, NONE, REG1, IMM, sg_mask);

	opt = CLASS_NONE | JUMP_TYPE_LOCAL | JUMP_COND_MATH_Z | JUMP_TEST_ALL;
	no_sg_jump = append_jump(desc, opt);

	append_math_add(desc, REG2, ZERO, REG3, MATH_LEN_8BYTE);

	/* update no S/G jump location */
	set_jump_tgt_here(desc, no_sg_jump);

	/* seqfifostr: msgdata len=4 */
	append_seq_fifo_store(desc, FIFOST_TYPE_MESSAGE_DATA, bytes_to_copy);

	/* move: ififo->deco-alnblk -> ofifo, len=4 */
	append_move(desc, MOVE_SRC_INFIFO | MOVE_DEST_OUTFIFO | bytes_to_copy);

	/* Overwrite the job-desc location (word 51 or 53) with the first
	 * group (11 words)*/
	opt   = MOVE_SRC_INFIFO | MOVE_DEST_DESCBUF;
	off_w = MAX_CAAM_DESCSIZE - job_desc_len;
	off_b = off_w * sizeof(uint32_t); /* calculate off in bytes */
	len   = (11 * sizeof(uint32_t)) << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/*
	 * Copy the context of math0 (input address) to words 52+53 or 54+56
	 * depending where the Job Descriptor starts.
	 * They will be used later by the load command.
	 */
	opt = MOVE_SRC_MATH0 | MOVE_DEST_DESCBUF;
	off_w = MAX_CAAM_DESCSIZE - job_desc_len + 1; /* 52 + 53 or 54 + 55 */
	off_b = off_w * sizeof(uint32_t);
	len = ALIGNED_PTR_ADDRESS_SZ << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/*
	 * Copy the context of math2 (output address) to words 56+57 or 58+59
	 * depending where the Job Descriptor starts.
	 * They will be used later by the store command.
	 */
	opt = MOVE_SRC_MATH2 | MOVE_DEST_DESCBUF;
	off_w = MAX_CAAM_DESCSIZE - job_desc_len + 5; /* 56 + 57 or 58 + 59 */
	off_b = off_w * sizeof(uint32_t);
	len = ALIGNED_PTR_ADDRESS_SZ << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/* Fix LIODN - OFFSET[0:1] - 01 = SEQ LIODN */
	opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
	off_b = 0x40; /* SEQ LIODN */
	append_cmd(desc, CMD_LOAD | opt | (off_b << LDST_OFFSET_SHIFT));

	/* Copy the context of the counters from word 29 into math0 */
	/* Copy from descriptor to MATH REG 0 the current statistics */
	stats_off_b = sa->stats_indx * CAAM_CMD_SZ;
	append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_MATH0 |
		    (stats_off_b << MOVE_OFFSET_SHIFT) | sizeof(uint64_t));

	dma_unmap_single(sa->dpa_ipsec->jrdev, dma_extra_cmds,
			 extra_cmds_len * sizeof(uint32_t), DMA_TO_DEVICE);

	return 0;
}

int build_extended_decap_shared_descriptor(struct dpa_ipsec_sa *sa,
					   dma_addr_t auth_key_dma,
					   dma_addr_t crypto_key_dma,
					   uint32_t bytes_to_copy,
					   uint8_t move_size,
					   int sec_era)
{
	uint32_t *desc, *no_sg_jump, *extra_cmds;
	uint32_t len, off_b, off_w, opt, stats_off_b, sg_mask, extra_cmds_len,
		 esp_length, iv_length, icv_length, max_pad, data;
	dma_addr_t dma_extra_cmds;
	struct device *jrdev;

	desc = (uint32_t *)sa->sec_desc->desc;

	/* CAAM hdr cmd + PDB size in words */
	sa->next_cmd_indx =
		sizeof(struct ipsec_decap_pdb) / sizeof(uint32_t) + 1;
	if (sa->enable_stats) {
		sa->stats_indx = sa->next_cmd_indx;
		sa->next_cmd_indx += 2;
		if (sec_era != 2) {
			sa->stats_indx += 1;
			sa->next_cmd_indx += 1;
		}
	}

	/* Set lifetime counter stats offset */
	sa->stats_offset = sa->stats_indx * sizeof(uint32_t);

	built_decap_extra_material(sa, auth_key_dma, crypto_key_dma);

	extra_cmds = sa->sec_desc_extra_cmds - 1;
	extra_cmds_len = desc_len(extra_cmds) - 1;

	/* get the jr device  */
	jrdev = get_jrdev(sa->dpa_ipsec);
	if (!jrdev) {
		log_err("Failed to get the job ring device, check the dts\n");
		return -EINVAL;
	}

	dma_extra_cmds = dma_map_single(jrdev, sa->sec_desc_extra_cmds,
					extra_cmds_len * sizeof(uint32_t),
					DMA_TO_DEVICE);
	if (!dma_extra_cmds) {
		log_err("Could not DMA map extra CAAM commands\n");
		return -ENXIO;
	}

	init_sh_desc_pdb(desc, HDR_SAVECTX | HDR_SHARE_SERIAL,
			 (sa->next_cmd_indx - 1) * sizeof(uint32_t));

	if (sec_era == 2) {
		/* disable iNFO FIFO entries for p4080rev2 & ??? */
		len = 0x10 << LDST_LEN_SHIFT;
		append_cmd(desc, CMD_LOAD | DISABLE_AUTO_INFO_FIFO | len);

		/*
		 * load in IN FIFO the S/G Entry located in the 5th reg after
		 * MATH3 -> offset = sizeof(GT_REG) * 4 + offset_math3_to_GT_REG
		 * len = sizeof(S/G entry)
		 * Offset refers to SRC
		 */
		opt   = MOVE_SRC_MATH3 | MOVE_DEST_CLASS1INFIFO;
		off_b = 127 << MOVE_OFFSET_SHIFT;
		len   = 49 << MOVE_LEN_SHIFT;
		append_move(desc, opt | off_b | len);

		/*
		 * L2 part 1
		 * Load from input packet to INPUT DATA FIFO first bytes_to_copy
		 * bytes.
		 */
		append_seq_fifo_load(desc, bytes_to_copy, FIFOLD_TYPE_MSG |
				     FIFOLD_CLASS_BOTH | FIFOLD_TYPE_LAST1 |
				     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_FLUSH1);

		/*
		 * Extra word part 1
		 * Load extra words for this descriptor into the INPUT DATA FIFO
		 */
		append_fifo_load(desc, dma_extra_cmds,
				 extra_cmds_len * sizeof(uint32_t),
				 FIFOLD_TYPE_MSG | FIFOLD_CLASS_BOTH |
				 FIFOLD_TYPE_LAST1 | FIFOLD_TYPE_LAST2 |
				 FIFOLD_TYPE_FLUSH1);

		/* enable iNFO FIFO entries */
		append_cmd(desc, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);
	} else {
		/* ????? */
		opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
		len = 0x10 << LDST_LEN_SHIFT;
		append_cmd(desc, CMD_LOAD | opt | len);

		/*
		 * load in IN FIFO the S/G Entry located in the 5th reg after
		 * MATH3 -> offset = sizeof(GT_REG) * 4 + offset_math3_to_GT_REG
		 * len = sizeof(S/G entry)
		 */
		opt   = MOVE_SRC_MATH3 | MOVE_DEST_INFIFO_NOINFO;
		off_b = 127 << MOVE_OFFSET_SHIFT;
		len   = 49 << MOVE_LEN_SHIFT;
		append_move(desc, opt | off_b | len);

		/*
		 * L2 part 1
		 * Load from input packet to INPUT DATA FIFO first bytes_to_copy
		 * bytes. No information FIFO entry even if automatic
		 * iNformation FIFO entries are enabled.
		 */
		append_seq_fifo_load(desc, bytes_to_copy, FIFOLD_CLASS_BOTH |
				     FIFOLD_TYPE_NOINFOFIFO);

		/*
		 * Extra word part 1
		 * Load extra words for this descriptor into the INPUT DATA FIFO
		 */
		append_fifo_load(desc, dma_extra_cmds,
				 extra_cmds_len * sizeof(uint32_t),
				 FIFOLD_CLASS_BOTH | FIFOLD_TYPE_NOINFOFIFO);
	}

	/*
	 * throw away the first part of the S/G table and keep only the buffer
	 * address;
	 * offset = undefined memory after MATH3; Refers to the destination.
	 * len = 41 bytes to discard
	 */
	opt   = MOVE_SRC_INFIFO | MOVE_DEST_MATH3;
	off_b = 8 << MOVE_OFFSET_SHIFT;
	len   = 41 << MOVE_LEN_SHIFT;
	append_move(desc, opt | off_b | len);

	/* put the buffer address (still in the IN FIFO) in MATH2 */
	opt   = MOVE_SRC_INFIFO | MOVE_DEST_MATH3;
	off_b = 0 << MOVE_OFFSET_SHIFT;
	len   = 8 << MOVE_LEN_SHIFT;
	append_move(desc, opt | off_b | len);

	/*
	 * Copy 15 bytes starting at 4 bytes before the OUT-PTR-CMD in
	 * the job-desc into math1
	 * i.e. in the low-part of math1 we have the out-ptr-cmd and
	 * in the math2 we will have the address of the out-ptr
	 */
	opt = MOVE_SRC_DESCBUF | MOVE_DEST_MATH1;
	off_b = (50 + 1 * PTR_LEN) * sizeof(uint32_t);
	len = (8 + 4 * PTR_LEN - 1) << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/* Copy 7 bytes of the in-ptr into math0 */
	opt   = MOVE_SRC_DESCBUF | MOVE_DEST_MATH0;
	off_w = 50 + 1 + 3 + 2 * PTR_LEN;
	off_b = off_w * sizeof(uint32_t); /* calculate off in bytes */
	len   = ALIGNED_PTR_ADDRESS_SZ << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/*
	 * the SEQ OUT PTR command is now in math reg 1, so the SGF bit can be
	 * checked using a math command;
	 */
	sg_mask = SEQ_OUT_PTR_SGF_MASK;
	append_math_and_imm_u32(desc, NONE, REG1, IMM, sg_mask);

	opt = CLASS_NONE | JUMP_TYPE_LOCAL | JUMP_COND_MATH_Z | JUMP_TEST_ALL;
	no_sg_jump = append_jump(desc, opt);

	append_math_add(desc, REG2, ZERO, REG3, MATH_LEN_8BYTE);

	/* update no S/G jump location */
	set_jump_tgt_here(desc, no_sg_jump);

	/* seqfifostr: msgdata len=4 */
	append_seq_fifo_store(desc, FIFOST_TYPE_MESSAGE_DATA, bytes_to_copy);

	/* move: ififo->deco-alnblk -> ofifo, len */
	append_move(desc, MOVE_SRC_INFIFO | MOVE_DEST_OUTFIFO | bytes_to_copy);

	/* Overwrite the job-desc location (word 50) with the first
	 * group (10 words)*/
	opt   = MOVE_SRC_INFIFO | MOVE_DEST_DESCBUF;
	off_w = 50;
	off_b = off_w * sizeof(uint32_t); /* calculate off in bytes */
	len   = (10 * sizeof(uint32_t)) << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/*
	 * Copy the context of math0 (input address) to words 32+33
	 * They will be used later by the load command.
	 */
	opt = MOVE_SRC_MATH0 | MOVE_DEST_DESCBUF;
	off_w = 32;
	off_b = off_w * sizeof(uint32_t);
	len = ALIGNED_PTR_ADDRESS_SZ << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/*
	 * Copy the context of math2 (output address) to words 56+57 or 58+59
	 * depending where the Job Descriptor starts.
	 * They will be used later by the store command.
	 */
	opt = MOVE_SRC_MATH2 | MOVE_DEST_DESCBUF | MOVE_WAITCOMP;
	off_w = 36;
	off_b = off_w * sizeof(uint32_t);
	len = ALIGNED_PTR_ADDRESS_SZ << MOVE_LEN_SHIFT;
	append_move(desc, opt | (off_b << MOVE_OFFSET_SHIFT) | len);

	/* Fix LIODN - OFFSET[0:1] - 01 = SEQ LIODN */
	opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
	off_b = 0x40; /* SEQ LIODN */
	append_cmd(desc, CMD_LOAD | opt | (off_b << LDST_OFFSET_SHIFT));

	/* Load from the input address 64 bytes into internal register */
	/* load the data to be moved - insert dummy pointer */
	opt = LDST_CLASS_2_CCB | LDST_SRCDST_WORD_CLASS_CTX;
	off_b = 0 << LDST_OFFSET_SHIFT;
	len = move_size << LDST_LEN_SHIFT;
	append_load(desc, DUMMY_PTR_VAL, len, opt | off_b);

	/* Wait to finish previous operation */
	opt = JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT);
	append_jump(desc, opt);

	/* Store the data to the output FIFO - insert dummy pointer */
	opt = LDST_CLASS_2_CCB | LDST_SRCDST_WORD_CLASS_CTX;
	off_b = 0 << LDST_OFFSET_SHIFT;
	len = move_size << LDST_LEN_SHIFT;
	append_store(desc, DUMMY_PTR_VAL, len, opt | off_b);

	/* Fix LIODN */
	opt = LDST_IMM | LDST_CLASS_DECO | LDST_SRCDST_WORD_DECOCTRL;
	off_b = 0x80 << LDST_OFFSET_SHIFT; /* NON_SEQ LIODN */
	append_cmd(desc, CMD_LOAD | opt | off_b);

	/* Copy from descriptor to MATH REG 0 the current statistics */
	stats_off_b = sa->stats_indx * CAAM_CMD_SZ;
	append_move(desc, MOVE_SRC_DESCBUF | MOVE_DEST_MATH0 | MOVE_WAITCOMP |
		    (stats_off_b << MOVE_OFFSET_SHIFT) | sizeof(uint64_t));

	/* Remove unnecessary headers
	 * MATH1 = 0 - (esp_length + iv_length + icv_length) */
	esp_length = 8; /* SPI + SEQ NUM */
	get_cipher_params(sa->alg_suite, &iv_length, &icv_length, &max_pad);
	data = (uint32_t) (esp_length + iv_length + icv_length);
	append_math_sub_imm_u64(desc, REG1, ZERO, IMM, data);

	/* MATH1 += SIL (bytes counter) */
	append_math_add(desc, REG1, SEQINLEN, REG1, MATH_LEN_8BYTE);

	/* data = outer IP header - should be read from DPOVRD register
	 * MATH 2 = outer IP header length */
	data = 20;
	opt = LDST_CLASS_DECO | LDST_SRCDST_WORD_DECO_MATH2;
	len = sizeof(data) << LDST_LEN_SHIFT;
	append_load_as_imm(desc, &data, len, opt);

	off_w = 7;
	append_jump(desc, (off_w << JUMP_OFFSET_SHIFT));

	/* jump: all-match[] always-jump offset=0 local->[00] */
	append_jump(desc, (0 << JUMP_OFFSET_SHIFT));

	/* jump: all-match[] always-jump offset=0 local->[00] */
	append_jump(desc, (0 << JUMP_OFFSET_SHIFT));

	data = 0x00ff0000;
	append_math_and_imm_u64(desc, REG2, DPOVRD, IMM, data);

	dma_unmap_single(sa->dpa_ipsec->jrdev, dma_extra_cmds,
			 extra_cmds_len * sizeof(uint32_t), DMA_TO_DEVICE);

	return 0;
}


int create_sec_descriptor(struct dpa_ipsec_sa *sa)
{
	struct sec_descriptor *sec_desc;
	struct device *jrdev;
	dma_addr_t auth_key_dma;
	dma_addr_t crypto_key_dma;
	int ret = 0;

	/* get the jr device  */
	jrdev = get_jrdev(sa->dpa_ipsec);
	if (!jrdev) {
		log_err("Failed to get the job ring device, check the dts\n");
		return -EINVAL;
	}

	/* check whether a split of a normal key is used */
	if (sa->auth_data.split_key_len)
		auth_key_dma = dma_map_single(jrdev, sa->auth_data.split_key,
					      sa->auth_data.split_key_pad_len,
					      DMA_TO_DEVICE);
	else
		auth_key_dma = dma_map_single(jrdev, sa->auth_data.auth_key,
					      sa->auth_data.auth_key_len,
					      DMA_TO_DEVICE);
	if (!auth_key_dma) {
		log_err("Could not DMA map authentication key\n");
		return -EINVAL;
	}

	crypto_key_dma = dma_map_single(jrdev, sa->cipher_data.cipher_key,
					sa->cipher_data.cipher_key_len,
					DMA_TO_DEVICE);
	if (!crypto_key_dma) {
		log_err("Could not DMA map cipher key\n");
		return -EINVAL;
	}

	/*
	 * Build the shared descriptor and see if its length is less than
	 * 64 words. If build_shared_descriptor returns -EPERM than it is
	 * required to build the extended shared descriptor in order to have
	 * all the SA features that were required.
	 */
	ret = build_shared_descriptor(sa, auth_key_dma, crypto_key_dma,
				      sa->l2_hdr_size);
	switch (ret) {
	case 0:
		sa->sec_desc_extended = false;
		goto done_shared_desc;
	case -EPERM:
		sa->sec_desc_extended = true;
		goto build_extended_shared_desc;
	default:
		log_err("Failed to create SEC descriptor for SA %d\n", sa->id);
		return -EFAULT;
	}

build_extended_shared_desc:
	/* Build the extended shared descriptor */
	if (sa->sa_dir == DPA_IPSEC_INBOUND)
		ret = build_extended_decap_shared_descriptor(sa, auth_key_dma,
				crypto_key_dma, sa->l2_hdr_size, 64,
				sa->dpa_ipsec->sec_era);
	else
		ret = build_extended_encap_shared_descriptor(sa, auth_key_dma,
				crypto_key_dma, sa->l2_hdr_size,
				sa->dpa_ipsec->sec_era);
	if (ret < 0) {
		log_err("Failed to create SEC descriptor for SA %d\n", sa->id);
		return -EFAULT;
	}

done_shared_desc:
	sec_desc = sa->sec_desc;
	/* setup preheader */
	sec_desc->preheader.hi.field.idlen = desc_len((u32 *) sec_desc->desc);
	sec_desc->preheader.lo.field.pool_id = sa->sa_bpid;
	sec_desc->preheader.lo.field.pool_buffer_size = sa->sa_bufsize;
	sec_desc->preheader.lo.field.offset =
		(sa->sa_dir == DPA_IPSEC_INBOUND) ?
			sa->dpa_ipsec->config.post_sec_in_params.data_off :
			sa->dpa_ipsec->config.post_sec_out_params.data_off;

	dma_unmap_single(jrdev, auth_key_dma,
			 sa->auth_data.split_key_pad_len, DMA_TO_DEVICE);
	dma_unmap_single(jrdev, crypto_key_dma,
			 sa->cipher_data.cipher_key_len, DMA_TO_DEVICE);
	return 0;
}

/*
 * Create descriptor for updating the anti replay window size
 * [21] B0951A1D       jobhdr: shrsz=21 shr share=serial reo len=29
 * [22] 00000000               sharedesc->@0x029a9a608
 * [23] 29A9A608
 * [24] 79340008         move: descbuf+0[00] -> math0, len=8 wait
 * [25] A82CC108         math: (0 - 1)->math1 len=8
 * [26] AC214108         math: (math1 - imm1)->math1 len=8 ifb
 * [27] 000000C0               imm1=192
 * [28] A8501008         math: (math0 & math1)->math0 len=8
 * [29] 1640180A           ld: deco-descbuf len=10 offs=24
 * [30] 00000000               ptr->@0x02965ca34
 * [31] 2965CA34
 * [32] A1001001         jump: jsl1 all-match[calm] offset=1 local->[33]
 * [33] A00000F7         jump: all-match[] always-jump offset=-9 local->[24]
 * [34] AC404008         math: (math0 | imm1)->math0 len=8 ifb
 * [35] 000000C0               imm1=192
 * [36] 79430008         move: math0 -> descbuf+0[00], len=8 wait
 * [37] 79631804         move: math2 -> descbuf+24[06], len=4 wait
 * [38] 56420107          str: deco-shrdesc+1 len=7
 * [39] 16401806           ld: deco-descbuf len=6 offs=24
 * [40] 00000000               ptr->@0x02965ca5c
 * [41] 2965CA5C
 * [42] A1001001         jump: jsl1 all-match[calm] offset=1 local->[43]
 * [43] A00000F7         jump: all-match[] always-jump offset=-9 local->[34]
 * [44] 16860800           ld: deco-ctrl len=0 offs=8 imm -auto-nfifo-entries
 * [45] 2E17000A    seqfifold: both msgdata-last2-last1-flush1 len=10
 * [46] 16860400           ld: deco-ctrl len=0 offs=4 imm +auto-nfifo-entries
 * [47] 7882000A         move: ififo->deco-alnblk -> ofifo, len=10
 * [48] 6830000A   seqfifostr: msgdata len=10
 * [49] A1C01002         jump: jsl1 all-match[calm] halt-user status=2
 *
 * The msg_len represent the length of the message written in the output frame
 * in order to differentiate between modify operations
 */
int build_rjob_desc_ars_update(struct dpa_ipsec_sa *sa, enum dpa_ipsec_arw arw,
			       u32 msg_len)
{
	uint32_t *desc, *rjobd, off;
	uint8_t options;
	enum dpa_ipsec_arw c_arw;
	size_t ars_off;
	dma_addr_t dma_shdesc;

	/* Check input parameters */
	BUG_ON(!sa);
	BUG_ON(!sa->sec_desc);
	desc = (uint32_t *)sa->sec_desc->desc;
	options = (uint8_t)(*(desc + 1) & 0x000000FF);
	c_arw = options >> 6;
	if (c_arw == arw) {
		log_err("SA %d has already set this ARS %d\n", sa->id, arw);
		return -EALREADY;
	}

	/* Get DMA address for this SA shared descriptor */
	dma_shdesc = dma_map_single(sa->dpa_ipsec->jrdev, sa->sec_desc->desc,
				    desc_len(sa->sec_desc->desc) * sizeof(u32),
				    DMA_BIDIRECTIONAL);
	if (!dma_shdesc) {
		log_err("Failed DMA map shared descriptor for SA %d\n", sa->id);
		return -ENXIO;
	}

	/* Create replacement job descriptor for ARS update */
	BUG_ON(!sa->rjob_desc);
	rjobd = sa->rjob_desc;

	init_job_desc(rjobd, HDR_SHARE_SERIAL | HDR_SHARED | HDR_REVERSE |
		      (desc_len(sa->sec_desc->desc) << HDR_START_IDX_SHIFT));

	/* Set DMA address of the shared descriptor */
	append_ptr(rjobd, dma_shdesc);

	/* Retrieve header and options from PDB in MATH 0 */
	append_move(rjobd, MOVE_SRC_DESCBUF | MOVE_DEST_MATH0 | MOVE_WAITCOMP |
		    (0 << MOVE_OFFSET_SHIFT) | sizeof(u64));

	/* MATH_REG1 = 0xFFFFFFFF_FFFFFFFF */
	append_math_sub(rjobd, REG1, ZERO, ONE, MATH_LEN_8BYTE);

	/* MATH_REG1 = 0xFFFFFFFF_FFFFFF3F */
	append_math_sub_imm_u64(rjobd, REG1, REG1, IMM, 0xC0);

	/* Reset ARS bits */
	append_math_and(rjobd, REG0, REG0, REG1, MATH_LEN_8BYTE);

	/*
	 * Overwrite RJD immediately after the SHD pointer i.e shared descriptor
	 * length plus 1 plus another 3 words
	 * Offset and length are expressed in words
	 * 3w - RJD header + SHD pointer
	 * 5w - five instructions for doing some part of ARS modification
	 * 3w - load instruction + pointer
	 * 1w - jump calm
	 * 1w - jump back to the remaining descriptor
	 */
	append_load(rjobd, virt_to_phys((void *)(rjobd + 3 + 5 + 3 + 1 + 1)),
		    10, LDST_CLASS_DECO | LDST_SRCDST_WORD_DESCBUF |
		    ((desc_len(sa->sec_desc->desc) + 3) << LDST_OFFSET_SHIFT));

	/* wait for completion o previous operation */
	append_jump(rjobd, JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT));

	/* jump back to remaining descriptor i.e jump back 9 words */
	off = (-9) & 0x000000FF;
	append_jump(rjobd, (off << JUMP_OFFSET_SHIFT));

	/* Convert PDB ARS to new size */
	switch (arw) {
	case DPA_IPSEC_ARSNONE:
		/*
		 * nothing to do because previous command reseted ARS bits
		 * add 2 NOPs to conserve descriptor size
		 */
		append_cmd(rjobd, 0xA0000001); /* NOP for SEC */
		append_cmd(rjobd, 0xA0000001); /* NOP for SEC */
		break;
	case DPA_IPSEC_ARS32:
		append_math_or_imm_u64(rjobd, REG0, REG0, IMM,
				       PDBOPTS_ESP_ARS32);
		break;
	case DPA_IPSEC_ARS64:
		append_math_or_imm_u64(rjobd, REG0, REG0, IMM,
				       PDBOPTS_ESP_ARS64);
		break;
	default:
		log_err("Invalid ARS\n");
		BUG();
	}

	/* Put header and options back to PDB */
	append_move(rjobd, MOVE_SRC_MATH0 | MOVE_DEST_DESCBUF | MOVE_WAITCOMP |
		    (0 << MOVE_OFFSET_SHIFT) | sizeof(u64));

	/*
	 * anti_replay[0] - used for 32ARS - LS bit represent the frame with
	 * highest SEQ number that has been successfully authenticated so far
	 * i.e the frame that had SEQ/ESN from PDB seq_num/seq_num_ext_hi
	 *
	 * anti_replay[1] - used when 64ARS is configured - LS bit represent
	 * a frame with a immediate older SEQ number than the MS bit of the
	 * anti_replay[0] i.e
	 * SEQ(LS bit of anti_replay[1]) = SEQ(MS bit of anti_replay[0]) - 1;
	 *
	 * always reset to 0 all bits from anti_replay[1]
	 * reset to 0 all bits from anti_replay[0] only if updating from ARS to
	 * no ARS.
	 * MOVE_SRC_MATH2 was not used until now i.e has value 0
	 */
	ars_off = offsetof(struct ipsec_decap_pdb, anti_replay);
	if (arw == DPA_IPSEC_ARSNONE)
		append_move(rjobd, MOVE_SRC_MATH2 | MOVE_DEST_DESCBUF |
			    MOVE_WAITCOMP | (ars_off << MOVE_OFFSET_SHIFT) |
			    sizeof(u64));
	else
		append_move(rjobd, MOVE_SRC_MATH2 | MOVE_DEST_DESCBUF |
			    MOVE_WAITCOMP |
			    ((ars_off + 4) << MOVE_OFFSET_SHIFT) | sizeof(u32));

	/*
	 * Update shared descriptor in memory - only PDB
	 * special case - offset and length are in words
	 */
	append_store(rjobd, 0, sizeof(struct ipsec_decap_pdb) / sizeof(u32),
		     LDST_CLASS_DECO | (1 << LDST_OFFSET_SHIFT) |
		     LDST_SRCDST_WORD_DESCBUF_SHARED);

	append_load(rjobd,
		    virt_to_phys((void *)(rjobd + 3 + 5 + 3 + 1 + 1 + 10)), 6,
		    LDST_CLASS_DECO | LDST_SRCDST_WORD_DESCBUF |
		    ((desc_len(sa->sec_desc->desc) + 3) << LDST_OFFSET_SHIFT));

	/* wait for completion of the previous operation */
	append_jump(rjobd, JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT));

	/* jump back to remaining descriptor i.e jump back 9 words */
	off = (-9) & 0x000000FF;
	append_jump(rjobd, (off << JUMP_OFFSET_SHIFT));

	/* ld: deco-deco-ctrl len=0 offs=8 imm -auto-nfifo-entries */
	append_cmd(rjobd, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);

	/* seqfifold: both msgdata-last2-last1-flush1 len=4 */
	append_seq_fifo_load(rjobd, msg_len, FIFOLD_TYPE_MSG |
			     FIFOLD_CLASS_BOTH | FIFOLD_TYPE_LAST1 |
			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_FLUSH1);

	/* ld: deco-deco-ctrl len=0 offs=4 imm +auto-nfifo-entries */
	append_cmd(rjobd, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);

	/* message "Modify anti replay window for SA n" */
	append_move(rjobd, MOVE_SRC_INFIFO | MOVE_DEST_OUTFIFO | msg_len);

	/* seqfifostr: msgdata len=4 */
	append_seq_fifo_store(rjobd, FIFOST_TYPE_MESSAGE_DATA, msg_len);

	/*
	 * Exit replacement job descriptor, halt with user error
	 * FD status will be a special user error, generated only on request by
	 * a descriptor command (not by any other circumstance) i.e no confusing
	 * this frame for any other error. Jump with CALM to be sure previous
	 * operation was finished
	 */
	append_cmd(rjobd, 0xA1C01002);

	dma_unmap_single(sa->dpa_ipsec->jrdev, dma_shdesc,
			 desc_len(sa->sec_desc->desc) * sizeof(u32),
			 DMA_BIDIRECTIONAL);

	return 0;
}

/*
 * The SEQ number value will be placed at the address specified by SEQ pointer
 */
int build_rjob_desc_seq_read(struct dpa_ipsec_sa *sa, u32 msg_len)
{
	uint32_t *rjobd, off_b = 0, off;
	dma_addr_t dma_shdesc, out_addr;

	/* Check input parameters */
	BUG_ON(!sa);
	BUG_ON(!sa->sec_desc);

	/* Get DMA address for this SA shared descriptor */
	dma_shdesc = dma_map_single(sa->dpa_ipsec->jrdev, sa->sec_desc->desc,
				    desc_len(sa->sec_desc->desc) * sizeof(u32),
				    DMA_BIDIRECTIONAL);
	if (!dma_shdesc) {
		log_err("Failed DMA map shared descriptor for SA %d\n", sa->id);
		return -ENXIO;
	}

	/* Get DMA address for this SA shared descriptor */
	out_addr = dma_map_single(sa->dpa_ipsec->jrdev, &sa->r_seq_num,
				  sizeof(sa->r_seq_num), DMA_BIDIRECTIONAL);
	if (!out_addr) {
		log_err("Failed DMA map output address for SA %d\n", sa->id);
		dma_unmap_single(sa->dpa_ipsec->jrdev, dma_shdesc,
				 desc_len(sa->sec_desc->desc) * sizeof(u32),
				 DMA_BIDIRECTIONAL);
		return -ENXIO;
	}

	/* Create replacement job descriptor for SEQ/ESEQ Number update */
	BUG_ON(!sa->rjob_desc);
	rjobd = sa->rjob_desc;

	init_job_desc(rjobd, HDR_SHARE_SERIAL | HDR_SHARED | HDR_REVERSE |
		      (desc_len(sa->sec_desc->desc) << HDR_START_IDX_SHIFT));

	/* Set DMA address of the shared descriptor */
	append_ptr(rjobd, dma_shdesc);

	/* Retrieve SEQ number from PDB in MATH 0 - offset is in bytes */
	off_b = sa_is_inbound(sa) ?
		offsetof(struct ipsec_decap_pdb, seq_num_ext_hi) + sizeof(u32) :
		offsetof(struct ipsec_encap_pdb, seq_num_ext_hi) + sizeof(u32);

	append_move(rjobd, MOVE_SRC_DESCBUF | MOVE_DEST_MATH0 | MOVE_WAITCOMP |
		    (off_b << MOVE_OFFSET_SHIFT) | sizeof(u64));

	/* Store SEQ number - length is in bytes */
	append_store(rjobd, out_addr, sizeof(sa->r_seq_num),
		     LDST_CLASS_DECO | LDST_SRCDST_WORD_DECO_MATH0);

	/* wait for completion of previous operation */
	append_jump(rjobd, JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT));

	/*
	 * Overwrite RJD immediately after the SHD pointer i.e shared descriptor
	 * length plus 1 plus another 3 words
	 * Offset and length are expressed in words
	 * 3w - RJD header + SHD pointer
	 * 5w - five instructions for doing some part of SEQ number modification
	 * 3w - load instruction + pointer
	 * 1w - jump calm
	 * 1w - jump back to the remaining descriptor
	 */
	append_load(rjobd, virt_to_phys((void *)(rjobd + 3 + 5 + 3 + 1 + 1)),
		    6, LDST_CLASS_DECO | LDST_SRCDST_WORD_DESCBUF |
		    ((desc_len(sa->sec_desc->desc) + 3) << LDST_OFFSET_SHIFT));

	/* wait for completion of previous operation */
	append_jump(rjobd, JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT));

	/* jump back to remaining descriptor i.e jump back 9 words */
	off = (-9) & 0x000000FF;
	append_jump(rjobd, (off << JUMP_OFFSET_SHIFT));

	/*
	 * The following instructions are used to copy the completion
	 * message into the output frame
	 */

	/* ld: deco-deco-ctrl len=0 offs=8 imm -auto-nfifo-entries */
	append_cmd(rjobd, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);

	/* seqfifold: both msgdata-last2-last1-flush1 len=4 */
	append_seq_fifo_load(rjobd, msg_len, FIFOLD_TYPE_MSG |
			     FIFOLD_CLASS_BOTH | FIFOLD_TYPE_LAST1 |
			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_FLUSH1);

	/* ld: deco-deco-ctrl len=0 offs=4 imm +auto-nfifo-entries */
	append_cmd(rjobd, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);

	/* copy completion message */
	append_move(rjobd, MOVE_SRC_INFIFO | MOVE_DEST_OUTFIFO | msg_len);

	/* seqfifostr: msgdata len=4 */
	append_seq_fifo_store(rjobd, FIFOST_TYPE_MESSAGE_DATA, msg_len);

	/*
	 * Exit replacement job descriptor, halt with user error
	 * FD status will be a special user error, generated only on request by
	 * a descriptor command
	 */
	append_cmd(rjobd, 0xA1C01002);

	dma_unmap_single(sa->dpa_ipsec->jrdev, dma_shdesc,
			 desc_len(sa->sec_desc->desc) * sizeof(u32),
			 DMA_BIDIRECTIONAL);

	dma_unmap_single(sa->dpa_ipsec->jrdev, out_addr,
			 sizeof(sa->r_seq_num),
			 DMA_BIDIRECTIONAL);

	return 0;
}

/*
 * The SEQ number value will be read from the SA structure and written to PDB of
 * the shared descriptor corresponding to this SA
 */
int build_rjob_desc_seq_write(struct dpa_ipsec_sa *sa, u32 msg_len)
{
	uint32_t *rjobd, off_b, off = 0;
	dma_addr_t dma_shdesc, in_addr;

	/* Check input parameters */
	BUG_ON(!sa);
	BUG_ON(!sa->sec_desc);

	/* Get DMA address for this SA shared descriptor */
	dma_shdesc = dma_map_single(sa->dpa_ipsec->jrdev, sa->sec_desc->desc,
				    desc_len(sa->sec_desc->desc) * sizeof(u32),
				    DMA_BIDIRECTIONAL);
	if (!dma_shdesc) {
		log_err("Failed DMA map shared descriptor for SA %d\n", sa->id);
		return -ENXIO;
	}

	in_addr = dma_map_single(sa->dpa_ipsec->jrdev, &sa->w_seq_num,
				 sizeof(sa->w_seq_num), DMA_BIDIRECTIONAL);
	if (!in_addr) {
		log_err("Failed DMA map output address for SA %d\n", sa->id);
		dma_unmap_single(sa->dpa_ipsec->jrdev, dma_shdesc,
				 desc_len(sa->sec_desc->desc) * sizeof(u32),
				 DMA_BIDIRECTIONAL);
		return -ENXIO;
	}

	/* Create replacement job descriptor for SEQ/ESEQ Number update */
	BUG_ON(!sa->rjob_desc);
	rjobd = sa->rjob_desc;

	init_job_desc(rjobd, HDR_SHARE_SERIAL | HDR_SHARED | HDR_REVERSE |
		      (desc_len(sa->sec_desc->desc) << HDR_START_IDX_SHIFT));

	/* Set DMA address of the shared descriptor */
	append_ptr(rjobd, dma_shdesc);

	/* Copy from SA SEQ to descriptor - offset & length is in words */
	off_b = sa_is_inbound(sa) ?
		offsetof(struct ipsec_decap_pdb, seq_num_ext_hi) + sizeof(u32) :
		offsetof(struct ipsec_encap_pdb, seq_num_ext_hi) + sizeof(u32);

	append_load(rjobd, in_addr, sizeof(sa->w_seq_num) / sizeof(u32),
		    LDST_CLASS_DECO | LDST_SRCDST_WORD_DESCBUF |
		    (off_b / sizeof(u32)) << LDST_OFFSET_SHIFT);

	/* wait for completion of previous operation */
	append_jump(rjobd, JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT));

	/*
	 * Update shared descriptor in memory - only PDB
	 * special case - offset and length are in words
	 */
	append_store(rjobd, 0, sizeof(sa->w_seq_num) / sizeof(u32),
		     LDST_CLASS_DECO |
		     (off_b / sizeof(u32) << LDST_OFFSET_SHIFT) |
		     LDST_SRCDST_WORD_DESCBUF_SHARED);

	/*
	 * Overwrite RJD immediately after the SHD pointer i.e shared descriptor
	 * length plus 1 plus another 3 words
	 * Offset and length are expressed in words
	 * 3w - RJD header + SHD pointer
	 * 5w - five instructions for doing some part of SEQ number modification
	 * 3w - load instruction + pointer
	 * 1w - jump calm
	 * 1w - jump back to the remaining descriptor
	 */
	append_load(rjobd, virt_to_phys((void *)(rjobd + 3 + 5 + 3 + 1 + 1)),
		    6, LDST_CLASS_DECO | LDST_SRCDST_WORD_DESCBUF |
		    ((desc_len(sa->sec_desc->desc) + 3) << LDST_OFFSET_SHIFT));

	/* wait for completion o previous operation */
	append_jump(rjobd, JUMP_COND_CALM | (1 << JUMP_OFFSET_SHIFT));

	/* jump back to remaining descriptor i.e jump back 9 words */
	off = (-9) & 0x000000FF;
	append_jump(rjobd, (off << JUMP_OFFSET_SHIFT));

	/*
	 * The following instructions are used to copy the completion
	 * message into the output frame
	 */

	/* ld: deco-deco-ctrl len=0 offs=8 imm -auto-nfifo-entries */
	append_cmd(rjobd, CMD_LOAD | DISABLE_AUTO_INFO_FIFO);

	/* seqfifold: both msgdata-last2-last1-flush1 len=4 */
	append_seq_fifo_load(rjobd, msg_len, FIFOLD_TYPE_MSG |
			     FIFOLD_CLASS_BOTH | FIFOLD_TYPE_LAST1 |
			     FIFOLD_TYPE_LAST2 | FIFOLD_TYPE_FLUSH1);

	/* ld: deco-deco-ctrl len=0 offs=4 imm +auto-nfifo-entries */
	append_cmd(rjobd, CMD_LOAD | ENABLE_AUTO_INFO_FIFO);

	/* copy completion message */
	append_move(rjobd, MOVE_SRC_INFIFO | MOVE_DEST_OUTFIFO | msg_len);

	/* seqfifostr: msgdata len=4 */
	append_seq_fifo_store(rjobd, FIFOST_TYPE_MESSAGE_DATA, msg_len);

	/*
	 * Exit replacement job descriptor, halt with user error
	 * FD status will be a special user error, generated only on request by
	 * a descriptor command (not by any other error)
	 */
	append_cmd(rjobd, 0xA1C01002);

	dma_unmap_single(sa->dpa_ipsec->jrdev, dma_shdesc,
			 desc_len(sa->sec_desc->desc) * sizeof(u32),
			 DMA_BIDIRECTIONAL);

	dma_unmap_single(sa->dpa_ipsec->jrdev, in_addr,
			 sizeof(sa->w_seq_num),
			 DMA_BIDIRECTIONAL);

	return 0;
}

static void split_key_done(struct device *dev, u32 *desc, u32 err,
			   void *context)
{
	register atomic_t *done = context;

	if (err)
		caam_jr_strstatus(dev, err);

	atomic_set(done, 1);
}

/* determine the HASH algorithm and the coresponding split key length */
int get_split_key_info(struct auth_params *auth_param, u32 *hmac_alg)
{
	/*
	 * Sizes for MDHA pads (*not* keys): MD5, SHA1, 224, 256, 384, 512
	 * Running digest size
	 */
	const u8 mdpadlen[] = {16, 20, 32, 32, 64, 64};

	switch (auth_param->auth_type) {
	case OP_PCL_IPSEC_HMAC_MD5_96:
	case OP_PCL_IPSEC_HMAC_MD5_128:
		*hmac_alg = OP_ALG_ALGSEL_MD5;
		break;
	case OP_PCL_IPSEC_HMAC_SHA1_96:
	case OP_PCL_IPSEC_HMAC_SHA1_160:
		*hmac_alg = OP_ALG_ALGSEL_SHA1;
		break;
	case OP_PCL_IPSEC_HMAC_SHA2_256_128:
		*hmac_alg = OP_ALG_ALGSEL_SHA256;
		break;
	case OP_PCL_IPSEC_HMAC_SHA2_384_192:
		*hmac_alg = OP_ALG_ALGSEL_SHA384;
		break;
	case OP_PCL_IPSEC_HMAC_SHA2_512_256:
		*hmac_alg = OP_ALG_ALGSEL_SHA512;
		break;
	case OP_PCL_IPSEC_AES_XCBC_MAC_96:
		*hmac_alg = 0;
		auth_param->split_key_len = 0;
		break;
	default:
		log_err("Unsupported authentication algorithm\n");
		return -EINVAL;
	}

	if (*hmac_alg)
		auth_param->split_key_len =
				mdpadlen[(*hmac_alg & OP_ALG_ALGSEL_SUBMASK) >>
					 OP_ALG_ALGSEL_SHIFT] * 2;

	return 0;
}

int generate_split_key(struct auth_params *auth_param)
{
	struct device *jrdev;
	dma_addr_t dma_addr_in, dma_addr_out;
	u32 *desc, timeout = 1000000, alg_sel = 0;
	struct dpa_ipsec_sa *sa;
	atomic_t done;
	int ret = 0;

	sa = container_of(auth_param, struct dpa_ipsec_sa, auth_data);
	BUG_ON(!sa->dpa_ipsec);

	ret = get_split_key_info(auth_param, &alg_sel);
	/* exit if error or there is no need to compute a split key */
	if (ret < 0 || alg_sel == 0)
		return ret;

	jrdev = get_jrdev(sa->dpa_ipsec);
	if (!jrdev) {
		log_err("Could not get job ring device, please check dts\n");
		return -ENODEV;
	}

	desc = kmalloc(CAAM_CMD_SZ * 6 + CAAM_PTR_SZ * 2, GFP_KERNEL | GFP_DMA);
	if (!desc) {
		log_err("Allocate memory failed for split key desc\n");
		return -ENOMEM;
	}

	auth_param->split_key_pad_len = ALIGN(auth_param->split_key_len, 16);

	dma_addr_in = dma_map_single(jrdev, auth_param->auth_key,
				     auth_param->auth_key_len, DMA_TO_DEVICE);
	if (dma_mapping_error(jrdev, dma_addr_in)) {
		dev_err(jrdev, "Unable to DMA map the input key address\n");
		kfree(desc);
		return -ENOMEM;
	}

	dma_addr_out = dma_map_single(jrdev, auth_param->split_key,
				      auth_param->split_key_pad_len,
				      DMA_FROM_DEVICE);
	if (dma_mapping_error(jrdev, dma_addr_out)) {
		dev_err(jrdev, "Unable to DMA map the output key address\n");
		dma_unmap_single(jrdev, dma_addr_in, auth_param->auth_key_len,
				 DMA_TO_DEVICE);
		kfree(desc);
		return -ENOMEM;
	}

	init_job_desc(desc, 0);

	append_key(desc, dma_addr_in, auth_param->auth_key_len,
		   CLASS_2 | KEY_DEST_CLASS_REG);

	/* Sets MDHA up into an HMAC-INIT */
	append_operation(desc, (OP_ALG_TYPE_CLASS2 << OP_ALG_TYPE_SHIFT) |
			 alg_sel | OP_ALG_AAI_HMAC |
			OP_ALG_DECRYPT | OP_ALG_AS_INIT);

	/* Do a FIFO_LOAD of zero, this will trigger the internal key expansion
	   into both pads inside MDHA */
	append_fifo_load_as_imm(desc, NULL, 0, LDST_CLASS_2_CCB |
				FIFOLD_TYPE_MSG | FIFOLD_TYPE_LAST2);

	/* FIFO_STORE with the explicit split-key content store
	 * (0x26 output type) */
	append_fifo_store(desc, dma_addr_out, auth_param->split_key_len,
			  LDST_CLASS_2_CCB | FIFOST_TYPE_SPLIT_KEK);

	atomic_set(&done, 0);
	ret = caam_jr_enqueue(jrdev, desc, split_key_done, &done);

	while (!atomic_read(&done) && --timeout) {
		udelay(1);
		cpu_relax();
	}

	if (timeout == 0)
		log_err("Timeout waiting for job ring to complete\n");

	dma_unmap_single(jrdev, dma_addr_out, auth_param->split_key_pad_len,
			 DMA_FROM_DEVICE);
	dma_unmap_single(jrdev, dma_addr_in, auth_param->auth_key_len,
			 DMA_TO_DEVICE);
	kfree(desc);
	return ret;
}