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
|
/*
* Copyright (c) 2024 Marvell.
* SPDX-License-Identifier: Apache-2.0
* https://spdx.org/licenses/Apache-2.0.html
*/
#include <vnet/dev/dev.h>
#include <vnet/devices/devices.h>
#include <dev_octeon/octeon.h>
#include <dev_octeon/crypto.h>
#include <base/roc_api.h>
#include <common.h>
oct_crypto_main_t oct_crypto_main;
oct_crypto_dev_t oct_crypto_dev;
VLIB_REGISTER_LOG_CLASS (oct_log, static) = {
.class_name = "octeon",
.subclass_name = "crypto",
};
static_always_inline void
oct_map_keyindex_to_session (oct_crypto_sess_t *sess, u32 key_index, u8 type)
{
oct_crypto_main_t *ocm = &oct_crypto_main;
oct_crypto_key_t *ckey;
ckey = vec_elt_at_index (ocm->keys[type], key_index);
ckey->sess = sess;
sess->key_index = key_index;
}
static_always_inline oct_crypto_sess_t *
oct_crypto_session_alloc (vlib_main_t *vm, u8 type)
{
extern oct_plt_init_param_t oct_plt_init_param;
oct_crypto_sess_t *addr = NULL;
oct_crypto_main_t *ocm;
oct_crypto_dev_t *ocd;
u32 size;
ocm = &oct_crypto_main;
ocd = ocm->crypto_dev[type];
size = sizeof (oct_crypto_sess_t);
addr = oct_plt_init_param.oct_plt_zmalloc (size, CLIB_CACHE_LINE_BYTES);
if (addr == NULL)
{
log_err (ocd->dev, "Failed to allocate crypto session memory");
return NULL;
}
return addr;
}
static_always_inline i32
oct_crypto_session_create (vlib_main_t *vm, vnet_crypto_key_index_t key_index,
int op_type)
{
oct_crypto_main_t *ocm = &oct_crypto_main;
oct_crypto_sess_t *session;
vnet_crypto_key_t *key;
oct_crypto_key_t *ckey;
key = vnet_crypto_get_key (key_index);
if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
{
/*
* Read crypto or integ key session. And map link key index to same.
*/
if (key->index_crypto != UINT32_MAX)
{
ckey = vec_elt_at_index (ocm->keys[op_type], key->index_crypto);
session = ckey->sess;
}
else if (key->index_integ != UINT32_MAX)
{
ckey = vec_elt_at_index (ocm->keys[op_type], key->index_integ);
session = ckey->sess;
}
else
return -1;
}
else
{
session = oct_crypto_session_alloc (vm, op_type);
if (session == NULL)
return -1;
}
oct_map_keyindex_to_session (session, key_index, op_type);
return 0;
}
void
oct_crypto_key_del_handler (vlib_main_t *vm, vnet_crypto_key_index_t key_index)
{
extern oct_plt_init_param_t oct_plt_init_param;
oct_crypto_main_t *ocm = &oct_crypto_main;
oct_crypto_key_t *ckey_linked;
oct_crypto_key_t *ckey;
vec_validate (ocm->keys[VNET_CRYPTO_OP_TYPE_ENCRYPT], key_index);
ckey = vec_elt_at_index (ocm->keys[VNET_CRYPTO_OP_TYPE_ENCRYPT], key_index);
if (ckey->sess)
{
/*
* If in case link algo is pointing to same sesison, reset the pointer.
*/
if (ckey->sess->key_index != key_index)
{
ckey_linked = vec_elt_at_index (
ocm->keys[VNET_CRYPTO_OP_TYPE_ENCRYPT], ckey->sess->key_index);
ckey_linked->sess = NULL;
}
oct_plt_init_param.oct_plt_free (ckey->sess);
ckey->sess = NULL;
}
ckey = vec_elt_at_index (ocm->keys[VNET_CRYPTO_OP_TYPE_DECRYPT], key_index);
if (ckey->sess)
{
/*
* If in case link algo is pointing to same sesison, reset the pointer.
*/
if (ckey->sess->key_index != key_index)
{
ckey_linked = vec_elt_at_index (
ocm->keys[VNET_CRYPTO_OP_TYPE_DECRYPT], ckey->sess->key_index);
ckey_linked->sess = NULL;
}
oct_plt_init_param.oct_plt_free (ckey->sess);
ckey->sess = NULL;
}
}
void
oct_crypto_key_add_handler (vlib_main_t *vm, vnet_crypto_key_index_t key_index)
{
oct_crypto_main_t *ocm = &oct_crypto_main;
oct_crypto_key_t *ckey;
oct_crypto_dev_t *ocd = &oct_crypto_dev;
vec_validate (ocm->keys[VNET_CRYPTO_OP_TYPE_ENCRYPT], key_index);
ckey = vec_elt_at_index (ocm->keys[VNET_CRYPTO_OP_TYPE_ENCRYPT], key_index);
if (ckey->sess == NULL)
{
if (oct_crypto_session_create (vm, key_index,
VNET_CRYPTO_OP_TYPE_ENCRYPT))
{
log_err (ocd->dev, "Unable to create crypto session");
return;
}
}
vec_validate (ocm->keys[VNET_CRYPTO_OP_TYPE_DECRYPT], key_index);
ckey = vec_elt_at_index (ocm->keys[VNET_CRYPTO_OP_TYPE_DECRYPT], key_index);
if (ckey->sess == NULL)
{
if (oct_crypto_session_create (vm, key_index,
VNET_CRYPTO_OP_TYPE_DECRYPT))
{
log_err (ocd->dev, "Unable to create crypto session");
return;
}
}
}
void
oct_crypto_key_handler (vlib_main_t *vm, vnet_crypto_key_op_t kop,
vnet_crypto_key_index_t idx)
{
oct_crypto_main_t *ocm = &oct_crypto_main;
if (kop == VNET_CRYPTO_KEY_OP_DEL)
{
oct_crypto_key_del_handler (vm, idx);
return;
}
oct_crypto_key_add_handler (vm, idx);
ocm->started = 1;
}
static_always_inline void
oct_crypto_session_free (vlib_main_t *vm, oct_crypto_sess_t *sess)
{
extern oct_plt_init_param_t oct_plt_init_param;
oct_plt_init_param.oct_plt_free (sess);
return;
}
#ifdef PLATFORM_OCTEON9
static inline void
oct_cpt_inst_submit (struct cpt_inst_s *inst, uint64_t lmtline,
uint64_t io_addr)
{
uint64_t lmt_status;
do
{
/* Copy CPT command to LMTLINE */
roc_lmt_mov64 ((void *) lmtline, inst);
/*
* Make sure compiler does not reorder memcpy and ldeor.
* LMTST transactions are always flushed from the write
* buffer immediately, a DMB is not required to push out
* LMTSTs.
*/
asm volatile ("dmb oshst" : : : "memory");
lmt_status = roc_lmt_submit_ldeor (io_addr);
}
while (lmt_status == 0);
}
#endif
static_always_inline void
oct_crypto_burst_submit (oct_crypto_dev_t *crypto_dev, struct cpt_inst_s *inst,
u32 n_left)
{
u64 lmt_base;
u64 io_addr;
u32 count;
#ifdef PLATFORM_OCTEON9
lmt_base = crypto_dev->lf.lmt_base;
io_addr = crypto_dev->lf.io_addr;
for (count = 0; count < n_left; count++)
oct_cpt_inst_submit (inst + count, lmt_base, io_addr);
#else
u64 *lmt_line[OCT_MAX_LMT_SZ];
u64 lmt_arg, core_lmt_id;
lmt_base = crypto_dev->lmtline.lmt_base;
io_addr = crypto_dev->lmtline.io_addr;
ROC_LMT_CPT_BASE_ID_GET (lmt_base, core_lmt_id);
for (count = 0; count < 16; count++)
{
lmt_line[count] = OCT_CPT_LMT_GET_LINE_ADDR (lmt_base, count);
}
while (n_left > OCT_MAX_LMT_SZ)
{
/*
* Add a memory barrier so that LMTLINEs from the previous iteration
* can be reused for a subsequent transfer.
*/
asm volatile ("dmb oshst" ::: "memory");
lmt_arg = ROC_CN10K_CPT_LMT_ARG | (u64) core_lmt_id;
for (count = 0; count < 16; count++)
{
roc_lmt_mov_seg ((void *) lmt_line[count], inst + count,
CPT_LMT_SIZE_COPY);
}
/* Set number of LMTSTs, excluding the first */
lmt_arg |= (OCT_MAX_LMT_SZ - 1) << 12;
roc_lmt_submit_steorl (lmt_arg, io_addr);
inst += OCT_MAX_LMT_SZ;
n_left -= OCT_MAX_LMT_SZ;
}
if (n_left > 0)
{
/*
* Add a memory barrier so that LMTLINEs from the previous iteration
* can be reused for a subsequent transfer.
*/
asm volatile ("dmb oshst" ::: "memory");
lmt_arg = ROC_CN10K_CPT_LMT_ARG | (u64) core_lmt_id;
for (count = 0; count < n_left; count++)
{
roc_lmt_mov_seg ((void *) lmt_line[count], inst + count,
CPT_LMT_SIZE_COPY);
}
/* Set number of LMTSTs, excluding the first */
lmt_arg |= (n_left - 1) << 12;
roc_lmt_submit_steorl (lmt_arg, io_addr);
}
#endif
}
static_always_inline uint32_t
oct_crypto_fill_sg_comp_from_iov (struct roc_sglist_comp *list, uint32_t i,
struct roc_se_iov_ptr *from,
uint32_t from_offset, uint32_t *psize,
struct roc_se_buf_ptr *extra_buf,
uint32_t extra_offset)
{
uint32_t extra_len = extra_buf ? extra_buf->size : 0;
uint32_t size = *psize;
int32_t j;
for (j = 0; j < from->buf_cnt; j++)
{
struct roc_sglist_comp *to = &list[i >> 2];
uint32_t buf_sz = from->bufs[j].size;
void *vaddr = from->bufs[j].vaddr;
uint64_t e_vaddr;
uint32_t e_len;
if (PREDICT_FALSE (from_offset))
{
if (from_offset >= buf_sz)
{
from_offset -= buf_sz;
continue;
}
e_vaddr = (uint64_t) vaddr + from_offset;
e_len = clib_min ((buf_sz - from_offset), size);
from_offset = 0;
}
else
{
e_vaddr = (uint64_t) vaddr;
e_len = clib_min (buf_sz, size);
}
to->u.s.len[i % 4] = clib_host_to_net_u16 (e_len);
to->ptr[i % 4] = clib_host_to_net_u64 (e_vaddr);
if (extra_len && (e_len >= extra_offset))
{
/* Break the data at given offset */
uint32_t next_len = e_len - extra_offset;
uint64_t next_vaddr = e_vaddr + extra_offset;
if (!extra_offset)
{
i--;
}
else
{
e_len = extra_offset;
size -= e_len;
to->u.s.len[i % 4] = clib_host_to_net_u16 (e_len);
}
extra_len = clib_min (extra_len, size);
/* Insert extra data ptr */
if (extra_len)
{
i++;
to = &list[i >> 2];
to->u.s.len[i % 4] = clib_host_to_net_u16 (extra_len);
to->ptr[i % 4] =
clib_host_to_net_u64 ((uint64_t) extra_buf->vaddr);
size -= extra_len;
}
next_len = clib_min (next_len, size);
/* insert the rest of the data */
if (next_len)
{
i++;
to = &list[i >> 2];
to->u.s.len[i % 4] = clib_host_to_net_u16 (next_len);
to->ptr[i % 4] = clib_host_to_net_u64 (next_vaddr);
size -= next_len;
}
extra_len = 0;
}
else
{
size -= e_len;
}
if (extra_offset)
extra_offset -= size;
i++;
if (PREDICT_FALSE (!size))
break;
}
*psize = size;
return (uint32_t) i;
}
static_always_inline u32
oct_crypto_fill_sg2_comp_from_iov (struct roc_sg2list_comp *list, u32 i,
struct roc_se_iov_ptr *from,
u32 from_offset, u32 *psize,
struct roc_se_buf_ptr *extra_buf,
u32 extra_offset)
{
u32 extra_len = extra_buf ? extra_buf->size : 0;
u32 size = *psize, buf_sz, e_len, next_len;
struct roc_sg2list_comp *to;
u64 e_vaddr, next_vaddr;
void *vaddr;
i32 j;
for (j = 0; j < from->buf_cnt; j++)
{
to = &list[i / 3];
buf_sz = from->bufs[j].size;
vaddr = from->bufs[j].vaddr;
if (PREDICT_FALSE (from_offset))
{
if (from_offset >= buf_sz)
{
from_offset -= buf_sz;
continue;
}
e_vaddr = (u64) vaddr + from_offset;
e_len = clib_min ((buf_sz - from_offset), size);
from_offset = 0;
}
else
{
e_vaddr = (u64) vaddr;
e_len = clib_min (buf_sz, size);
}
to->u.s.len[i % 3] = (e_len);
to->ptr[i % 3] = (e_vaddr);
to->u.s.valid_segs = (i % 3) + 1;
if (extra_len && (e_len >= extra_offset))
{
/* Break the data at given offset */
next_len = e_len - extra_offset;
next_vaddr = e_vaddr + extra_offset;
if (!extra_offset)
i--;
else
{
e_len = extra_offset;
size -= e_len;
to->u.s.len[i % 3] = (e_len);
}
extra_len = clib_min (extra_len, size);
/* Insert extra data ptr */
if (extra_len)
{
i++;
to = &list[i / 3];
to->u.s.len[i % 3] = (extra_len);
to->ptr[i % 3] = ((u64) extra_buf->vaddr);
to->u.s.valid_segs = (i % 3) + 1;
size -= extra_len;
}
next_len = clib_min (next_len, size);
/* insert the rest of the data */
if (next_len)
{
i++;
to = &list[i / 3];
to->u.s.len[i % 3] = (next_len);
to->ptr[i % 3] = (next_vaddr);
to->u.s.valid_segs = (i % 3) + 1;
size -= next_len;
}
extra_len = 0;
}
else
size -= e_len;
if (extra_offset)
extra_offset -= size;
i++;
if (PREDICT_FALSE (!size))
break;
}
*psize = size;
return (u32) i;
}
static_always_inline uint32_t
oct_crypto_fill_sg_comp_from_buf (struct roc_sglist_comp *list, uint32_t i,
struct roc_se_buf_ptr *from)
{
struct roc_sglist_comp *to = &list[i >> 2];
to->u.s.len[i % 4] = clib_host_to_net_u16 (from->size);
to->ptr[i % 4] = clib_host_to_net_u64 ((uint64_t) from->vaddr);
return ++i;
}
static_always_inline uint32_t
oct_crypto_fill_sg_comp (struct roc_sglist_comp *list, uint32_t i,
uint64_t dma_addr, uint32_t size)
{
struct roc_sglist_comp *to = &list[i >> 2];
to->u.s.len[i % 4] = clib_host_to_net_u16 (size);
to->ptr[i % 4] = clib_host_to_net_u64 (dma_addr);
return ++i;
}
static_always_inline u32
oct_crypto_fill_sg2_comp (struct roc_sg2list_comp *list, u32 index,
u64 dma_addr, u32 size)
{
struct roc_sg2list_comp *to = &list[index / 3];
to->u.s.len[index % 3] = (size);
to->ptr[index % 3] = (dma_addr);
to->u.s.valid_segs = (index % 3) + 1;
return ++index;
}
static_always_inline u32
oct_crypto_fill_sg2_comp_from_buf (struct roc_sg2list_comp *list, u32 index,
struct roc_se_buf_ptr *from)
{
struct roc_sg2list_comp *to = &list[index / 3];
to->u.s.len[index % 3] = (from->size);
to->ptr[index % 3] = ((u64) from->vaddr);
to->u.s.valid_segs = (index % 3) + 1;
return ++index;
}
static_always_inline int __attribute__ ((unused))
oct_crypto_sg_inst_prep (struct roc_se_fc_params *params,
struct cpt_inst_s *inst, uint64_t offset_ctrl,
const uint8_t *iv_s, int iv_len, uint8_t pack_iv,
uint8_t pdcp_alg_type, int32_t inputlen,
int32_t outputlen, uint32_t passthrough_len,
uint32_t req_flags, int pdcp_flag, int decrypt)
{
struct roc_sglist_comp *gather_comp, *scatter_comp;
void *m_vaddr = params->meta_buf.vaddr;
struct roc_se_buf_ptr *aad_buf = NULL;
uint32_t mac_len = 0, aad_len = 0;
struct roc_se_ctx *se_ctx;
uint32_t i, g_size_bytes;
uint64_t *offset_vaddr;
uint32_t s_size_bytes;
uint8_t *in_buffer;
uint32_t size;
uint8_t *iv_d;
int ret = 0;
se_ctx = params->ctx;
mac_len = se_ctx->mac_len;
if (PREDICT_FALSE (req_flags & ROC_SE_VALID_AAD_BUF))
{
/* We don't support both AAD and auth data separately */
aad_len = params->aad_buf.size;
aad_buf = ¶ms->aad_buf;
}
/* save space for iv */
offset_vaddr = m_vaddr;
m_vaddr =
(uint8_t *) m_vaddr + ROC_SE_OFF_CTRL_LEN + PLT_ALIGN_CEIL (iv_len, 8);
inst->w4.s.opcode_major |= (uint64_t) ROC_DMA_MODE_SG;
/* iv offset is 0 */
*offset_vaddr = offset_ctrl;
iv_d = ((uint8_t *) offset_vaddr + ROC_SE_OFF_CTRL_LEN);
if (PREDICT_TRUE (iv_len))
memcpy (iv_d, iv_s, iv_len);
/* DPTR has SG list */
/* TODO Add error check if space will be sufficient */
gather_comp = (struct roc_sglist_comp *) ((uint8_t *) m_vaddr + 8);
/*
* Input Gather List
*/
i = 0;
/* Offset control word followed by iv */
i = oct_crypto_fill_sg_comp (gather_comp, i, (uint64_t) offset_vaddr,
ROC_SE_OFF_CTRL_LEN + iv_len);
/* Add input data */
if (decrypt && (req_flags & ROC_SE_VALID_MAC_BUF))
{
size = inputlen - iv_len - mac_len;
if (PREDICT_TRUE (size))
{
uint32_t aad_offset = aad_len ? passthrough_len : 0;
i = oct_crypto_fill_sg_comp_from_iov (
gather_comp, i, params->src_iov, 0, &size, aad_buf, aad_offset);
if (PREDICT_FALSE (size))
{
clib_warning ("Cryptodev: Insufficient buffer"
" space, size %d needed",
size);
return -1;
}
}
if (mac_len)
i =
oct_crypto_fill_sg_comp_from_buf (gather_comp, i, ¶ms->mac_buf);
}
else
{
/* input data */
size = inputlen - iv_len;
if (size)
{
uint32_t aad_offset = aad_len ? passthrough_len : 0;
i = oct_crypto_fill_sg_comp_from_iov (
gather_comp, i, params->src_iov, 0, &size, aad_buf, aad_offset);
if (PREDICT_FALSE (size))
{
clib_warning ("Cryptodev: Insufficient buffer space,"
" size %d needed",
size);
return -1;
}
}
}
in_buffer = m_vaddr;
((uint16_t *) in_buffer)[0] = 0;
((uint16_t *) in_buffer)[1] = 0;
((uint16_t *) in_buffer)[2] = clib_host_to_net_u16 (i);
g_size_bytes = ((i + 3) / 4) * sizeof (struct roc_sglist_comp);
/*
* Output Scatter List
*/
i = 0;
scatter_comp =
(struct roc_sglist_comp *) ((uint8_t *) gather_comp + g_size_bytes);
i = oct_crypto_fill_sg_comp (
scatter_comp, i, (uint64_t) offset_vaddr + ROC_SE_OFF_CTRL_LEN, iv_len);
/* Add output data */
if ((!decrypt) && (req_flags & ROC_SE_VALID_MAC_BUF))
{
size = outputlen - iv_len - mac_len;
if (size)
{
uint32_t aad_offset = aad_len ? passthrough_len : 0;
i = oct_crypto_fill_sg_comp_from_iov (
scatter_comp, i, params->dst_iov, 0, &size, aad_buf, aad_offset);
if (PREDICT_FALSE (size))
{
clib_warning ("Cryptodev: Insufficient buffer space,"
" size %d needed",
size);
return -1;
}
}
/* mac data */
if (mac_len)
i =
oct_crypto_fill_sg_comp_from_buf (scatter_comp, i, ¶ms->mac_buf);
}
else
{
/* Output including mac */
size = outputlen - iv_len;
if (size)
{
uint32_t aad_offset = aad_len ? passthrough_len : 0;
i = oct_crypto_fill_sg_comp_from_iov (
scatter_comp, i, params->dst_iov, 0, &size, aad_buf, aad_offset);
if (PREDICT_FALSE (size))
{
clib_warning ("Cryptodev: Insufficient buffer space,"
" size %d needed",
size);
return -1;
}
}
}
((uint16_t *) in_buffer)[3] = clib_host_to_net_u16 (i);
s_size_bytes = ((i + 3) / 4) * sizeof (struct roc_sglist_comp);
size = g_size_bytes + s_size_bytes + ROC_SG_LIST_HDR_SIZE;
/* This is DPTR len in case of SG mode */
inst->w4.s.dlen = size;
if (PREDICT_FALSE (size > ROC_SG_MAX_DLEN_SIZE))
{
clib_warning (
"Cryptodev: Exceeds max supported components. Reduce segments");
ret = -1;
}
inst->dptr = (uint64_t) in_buffer;
return ret;
}
static_always_inline int __attribute__ ((unused))
oct_crypto_sg2_inst_prep (struct roc_se_fc_params *params,
struct cpt_inst_s *inst, u64 offset_ctrl,
const u8 *iv_s, int iv_len, u8 pack_iv,
u8 pdcp_alg_type, i32 inputlen, i32 outputlen,
u32 passthrough_len, u32 req_flags, int pdcp_flag,
int decrypt)
{
u32 mac_len = 0, aad_len = 0, size, index, g_size_bytes;
struct roc_sg2list_comp *gather_comp, *scatter_comp;
void *m_vaddr = params->meta_buf.vaddr;
struct roc_se_buf_ptr *aad_buf = NULL;
union cpt_inst_w5 cpt_inst_w5;
union cpt_inst_w6 cpt_inst_w6;
u16 scatter_sz, gather_sz;
struct roc_se_ctx *se_ctx;
u64 *offset_vaddr;
int ret = 0;
u8 *iv_d;
se_ctx = params->ctx;
mac_len = se_ctx->mac_len;
if (PREDICT_FALSE (req_flags & ROC_SE_VALID_AAD_BUF))
{
/* We don't support both AAD and auth data separately */
aad_len = params->aad_buf.size;
aad_buf = ¶ms->aad_buf;
}
/* save space for iv */
offset_vaddr = m_vaddr;
m_vaddr = (u8 *) m_vaddr + ROC_SE_OFF_CTRL_LEN + PLT_ALIGN_CEIL (iv_len, 8);
inst->w4.s.opcode_major |= (u64) ROC_DMA_MODE_SG;
/* This is DPTR len in case of SG mode */
inst->w4.s.dlen = inputlen + ROC_SE_OFF_CTRL_LEN;
/* iv offset is 0 */
*offset_vaddr = offset_ctrl;
iv_d = ((u8 *) offset_vaddr + ROC_SE_OFF_CTRL_LEN);
if (PREDICT_TRUE (iv_len))
clib_memcpy (iv_d, iv_s, iv_len);
/* DPTR has SG list */
gather_comp = (struct roc_sg2list_comp *) ((u8 *) m_vaddr);
/*
* Input Gather List
*/
index = 0;
/* Offset control word followed by iv */
index = oct_crypto_fill_sg2_comp (gather_comp, index, (u64) offset_vaddr,
ROC_SE_OFF_CTRL_LEN + iv_len);
/* Add input data */
if (decrypt && (req_flags & ROC_SE_VALID_MAC_BUF))
{
size = inputlen - iv_len - mac_len;
if (size)
{
/* input data only */
u32 aad_offset = aad_len ? passthrough_len : 0;
index = oct_crypto_fill_sg2_comp_from_iov (gather_comp, index,
params->src_iov, 0, &size,
aad_buf, aad_offset);
if (PREDICT_FALSE (size))
{
clib_warning ("Cryptodev: Insufficient buffer"
" space, size %d needed",
size);
return -1;
}
}
/* mac data */
if (mac_len)
index = oct_crypto_fill_sg2_comp_from_buf (gather_comp, index,
¶ms->mac_buf);
}
else
{
/* input data */
size = inputlen - iv_len;
if (size)
{
u32 aad_offset = aad_len ? passthrough_len : 0;
index = oct_crypto_fill_sg2_comp_from_iov (gather_comp, index,
params->src_iov, 0, &size,
aad_buf, aad_offset);
if (PREDICT_FALSE (size))
{
clib_warning ("Cryptodev: Insufficient buffer space,"
" size %d needed",
size);
return -1;
}
}
}
gather_sz = (index + 2) / 3;
g_size_bytes = gather_sz * sizeof (struct roc_sg2list_comp);
/*
* Output Scatter List
*/
index = 0;
scatter_comp =
(struct roc_sg2list_comp *) ((u8 *) gather_comp + g_size_bytes);
index = oct_crypto_fill_sg2_comp (
scatter_comp, index, (u64) offset_vaddr + ROC_SE_OFF_CTRL_LEN, iv_len);
/* Add output data */
if ((!decrypt) && (req_flags & ROC_SE_VALID_MAC_BUF))
{
size = outputlen - iv_len - mac_len;
if (size)
{
u32 aad_offset = aad_len ? passthrough_len : 0;
index = oct_crypto_fill_sg2_comp_from_iov (scatter_comp, index,
params->dst_iov, 0, &size,
aad_buf, aad_offset);
if (PREDICT_FALSE (size))
{
clib_warning ("Cryptodev: Insufficient buffer space,"
" size %d needed",
size);
return -1;
}
}
/* mac data */
if (mac_len)
index = oct_crypto_fill_sg2_comp_from_buf (scatter_comp, index,
¶ms->mac_buf);
}
else
{
/* Output including mac */
size = outputlen - iv_len;
if (size)
{
u32 aad_offset = aad_len ? passthrough_len : 0;
index = oct_crypto_fill_sg2_comp_from_iov (scatter_comp, index,
params->dst_iov, 0, &size,
aad_buf, aad_offset);
if (PREDICT_FALSE (size))
{
clib_warning ("Cryptodev: Insufficient buffer space,"
" size %d needed",
size);
return -1;
}
}
}
scatter_sz = (index + 2) / 3;
cpt_inst_w5.s.gather_sz = gather_sz;
cpt_inst_w6.s.scatter_sz = scatter_sz;
cpt_inst_w5.s.dptr = (u64) gather_comp;
cpt_inst_w6.s.rptr = (u64) scatter_comp;
inst->w5.u64 = cpt_inst_w5.u64;
inst->w6.u64 = cpt_inst_w6.u64;
if (PREDICT_FALSE ((scatter_sz >> 4) || (gather_sz >> 4)))
{
clib_warning (
"Cryptodev: Exceeds max supported components. Reduce segments");
ret = -1;
}
return ret;
}
static_always_inline int
oct_crypto_cpt_hmac_prep (u32 flags, u64 d_offs, u64 d_lens,
struct roc_se_fc_params *fc_params,
struct cpt_inst_s *inst, u8 is_decrypt)
{
u32 encr_data_len, auth_data_len, aad_len = 0;
i32 inputlen, outputlen, enc_dlen, auth_dlen;
u32 encr_offset, auth_offset, iv_offset = 0;
union cpt_inst_w4 cpt_inst_w4;
u32 cipher_type;
struct roc_se_ctx *se_ctx;
u32 passthrough_len = 0;
const u8 *src = NULL;
u64 offset_ctrl;
u8 iv_len = 16;
u8 op_minor;
u32 mac_len;
int ret;
encr_offset = ROC_SE_ENCR_OFFSET (d_offs);
auth_offset = ROC_SE_AUTH_OFFSET (d_offs);
encr_data_len = ROC_SE_ENCR_DLEN (d_lens);
auth_data_len = ROC_SE_AUTH_DLEN (d_lens);
if (PREDICT_FALSE (flags & ROC_SE_VALID_AAD_BUF))
{
/* We don't support both AAD and auth data separately */
auth_data_len = 0;
auth_offset = 0;
aad_len = fc_params->aad_buf.size;
}
se_ctx = fc_params->ctx;
cipher_type = se_ctx->enc_cipher;
mac_len = se_ctx->mac_len;
cpt_inst_w4.u64 = se_ctx->template_w4.u64;
op_minor = cpt_inst_w4.s.opcode_minor;
if (PREDICT_FALSE (flags & ROC_SE_VALID_AAD_BUF))
{
/*
* When AAD is given, data above encr_offset is pass through
* Since AAD is given as separate pointer and not as offset,
* this is a special case as we need to fragment input data
* into passthrough + encr_data and then insert AAD in between.
*/
passthrough_len = encr_offset;
auth_offset = passthrough_len + iv_len;
encr_offset = passthrough_len + aad_len + iv_len;
auth_data_len = aad_len + encr_data_len;
}
else
{
encr_offset += iv_len;
auth_offset += iv_len;
}
auth_dlen = auth_offset + auth_data_len;
enc_dlen = encr_data_len + encr_offset;
cpt_inst_w4.s.opcode_major = ROC_SE_MAJOR_OP_FC;
if (is_decrypt)
{
cpt_inst_w4.s.opcode_minor |= ROC_SE_FC_MINOR_OP_DECRYPT;
if (auth_dlen > enc_dlen)
{
inputlen = auth_dlen + mac_len;
outputlen = auth_dlen;
}
else
{
inputlen = enc_dlen + mac_len;
outputlen = enc_dlen;
}
}
else
{
cpt_inst_w4.s.opcode_minor |= ROC_SE_FC_MINOR_OP_ENCRYPT;
/* Round up to 16 bytes alignment */
if (PREDICT_FALSE (encr_data_len & 0xf))
{
if (PREDICT_TRUE (cipher_type == ROC_SE_AES_CBC) ||
(cipher_type == ROC_SE_DES3_CBC))
enc_dlen = PLT_ALIGN_CEIL (encr_data_len, 8) + encr_offset;
}
/*
* auth_dlen is larger than enc_dlen in Authentication cases
* like AES GMAC Authentication
*/
if (PREDICT_FALSE (auth_dlen > enc_dlen))
{
inputlen = auth_dlen;
outputlen = auth_dlen + mac_len;
}
else
{
inputlen = enc_dlen;
outputlen = enc_dlen + mac_len;
}
}
if (op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST)
outputlen = enc_dlen;
cpt_inst_w4.s.param1 = encr_data_len;
cpt_inst_w4.s.param2 = auth_data_len;
if (PREDICT_FALSE ((encr_offset >> 16) || (iv_offset >> 8) ||
(auth_offset >> 8)))
{
clib_warning ("Cryptodev: Offset not supported");
clib_warning (
"Cryptodev: enc_offset: %d, iv_offset : %d, auth_offset: %d",
encr_offset, iv_offset, auth_offset);
return -1;
}
offset_ctrl = clib_host_to_net_u64 (
((u64) encr_offset << 16) | ((u64) iv_offset << 8) | ((u64) auth_offset));
src = fc_params->iv_buf;
inst->w4.u64 = cpt_inst_w4.u64;
#ifdef PLATFORM_OCTEON9
ret = oct_crypto_sg_inst_prep (fc_params, inst, offset_ctrl, src, iv_len, 0,
0, inputlen, outputlen, passthrough_len,
flags, 0, is_decrypt);
#else
ret = oct_crypto_sg2_inst_prep (fc_params, inst, offset_ctrl, src, iv_len, 0,
0, inputlen, outputlen, passthrough_len,
flags, 0, is_decrypt);
#endif
if (PREDICT_FALSE (ret))
return -1;
return 0;
}
static_always_inline int
oct_crypto_fill_fc_params (oct_crypto_sess_t *sess, struct cpt_inst_s *inst,
const bool is_aead, u8 aad_length, u8 *payload,
vnet_crypto_async_frame_elt_t *elts, void *mdata,
u32 cipher_data_length, u32 cipher_data_offset,
u32 auth_data_length, u32 auth_data_offset,
vlib_buffer_t *b, u16 adj_len)
{
struct roc_se_fc_params fc_params = { 0 };
struct roc_se_ctx *ctx = &sess->cpt_ctx;
u64 d_offs = 0, d_lens = 0;
vlib_buffer_t *buffer = b;
u32 flags = 0, index = 0;
u8 op_minor = 0, cpt_op;
char src[SRC_IOV_SIZE];
u32 *iv_buf;
cpt_op = sess->cpt_op;
if (is_aead)
{
flags |= ROC_SE_VALID_IV_BUF;
iv_buf = (u32 *) elts->iv;
iv_buf[3] = clib_host_to_net_u32 (0x1);
fc_params.iv_buf = elts->iv;
d_offs = cipher_data_offset;
d_offs = d_offs << 16;
d_lens = cipher_data_length;
d_lens = d_lens << 32;
fc_params.aad_buf.vaddr = elts->aad;
fc_params.aad_buf.size = aad_length;
flags |= ROC_SE_VALID_AAD_BUF;
if (sess->cpt_ctx.mac_len)
{
flags |= ROC_SE_VALID_MAC_BUF;
fc_params.mac_buf.size = sess->cpt_ctx.mac_len;
fc_params.mac_buf.vaddr = elts->tag;
}
}
else
{
op_minor = ctx->template_w4.s.opcode_minor;
flags |= ROC_SE_VALID_IV_BUF;
fc_params.iv_buf = elts->iv;
d_offs = cipher_data_offset;
d_offs = (d_offs << 16) | auth_data_offset;
d_lens = cipher_data_length;
d_lens = (d_lens << 32) | auth_data_length;
if (PREDICT_TRUE (sess->cpt_ctx.mac_len))
{
if (!(op_minor & ROC_SE_FC_MINOR_OP_HMAC_FIRST))
{
flags |= ROC_SE_VALID_MAC_BUF;
fc_params.mac_buf.size = sess->cpt_ctx.mac_len;
fc_params.mac_buf.vaddr = elts->digest;
}
}
}
fc_params.ctx = &sess->cpt_ctx;
fc_params.src_iov = (void *) src;
fc_params.src_iov->bufs[index].vaddr = payload;
fc_params.src_iov->bufs[index].size = b->current_length - adj_len;
index++;
while (buffer->flags & VLIB_BUFFER_NEXT_PRESENT)
{
buffer = vlib_get_buffer (vlib_get_main (), buffer->next_buffer);
fc_params.src_iov->bufs[index].vaddr =
buffer->data + buffer->current_data;
fc_params.src_iov->bufs[index].size = buffer->current_length;
index++;
}
fc_params.src_iov->buf_cnt = index;
fc_params.dst_iov = (void *) src;
fc_params.meta_buf.vaddr = mdata;
fc_params.meta_buf.size = OCT_SCATTER_GATHER_BUFFER_SIZE;
return oct_crypto_cpt_hmac_prep (flags, d_offs, d_lens, &fc_params, inst,
cpt_op);
}
static_always_inline u64
oct_cpt_inst_w7_get (oct_crypto_sess_t *sess, struct roc_cpt *roc_cpt)
{
union cpt_inst_w7 inst_w7;
inst_w7.u64 = 0;
inst_w7.s.cptr = (u64) &sess->cpt_ctx.se_ctx.fctx;
/* Set the engine group */
inst_w7.s.egrp = roc_cpt->eng_grp[CPT_ENG_TYPE_IE];
return inst_w7.u64;
}
static_always_inline i32
oct_crypto_link_session_update (vlib_main_t *vm, oct_crypto_sess_t *sess,
u32 key_index, u8 type)
{
vnet_crypto_key_t *crypto_key, *auth_key;
roc_se_cipher_type enc_type = 0;
roc_se_auth_type auth_type = 0;
vnet_crypto_key_t *key;
u32 digest_len = ~0;
i32 rv = 0;
key = vnet_crypto_get_key (key_index);
switch (key->async_alg)
{
case VNET_CRYPTO_ALG_AES_128_CBC_SHA1_TAG12:
case VNET_CRYPTO_ALG_AES_192_CBC_SHA1_TAG12:
case VNET_CRYPTO_ALG_AES_256_CBC_SHA1_TAG12:
enc_type = ROC_SE_AES_CBC;
auth_type = ROC_SE_SHA1_TYPE;
digest_len = 12;
break;
case VNET_CRYPTO_ALG_AES_128_CBC_SHA224_TAG14:
case VNET_CRYPTO_ALG_AES_192_CBC_SHA224_TAG14:
case VNET_CRYPTO_ALG_AES_256_CBC_SHA224_TAG14:
enc_type = ROC_SE_AES_CBC;
auth_type = ROC_SE_SHA2_SHA224;
digest_len = 14;
break;
case VNET_CRYPTO_ALG_AES_128_CBC_SHA256_TAG16:
case VNET_CRYPTO_ALG_AES_192_CBC_SHA256_TAG16:
case VNET_CRYPTO_ALG_AES_256_CBC_SHA256_TAG16:
enc_type = ROC_SE_AES_CBC;
auth_type = ROC_SE_SHA2_SHA256;
digest_len = 16;
break;
case VNET_CRYPTO_ALG_AES_128_CBC_SHA384_TAG24:
case VNET_CRYPTO_ALG_AES_192_CBC_SHA384_TAG24:
case VNET_CRYPTO_ALG_AES_256_CBC_SHA384_TAG24:
enc_type = ROC_SE_AES_CBC;
auth_type = ROC_SE_SHA2_SHA384;
digest_len = 24;
break;
case VNET_CRYPTO_ALG_AES_128_CBC_SHA512_TAG32:
case VNET_CRYPTO_ALG_AES_192_CBC_SHA512_TAG32:
case VNET_CRYPTO_ALG_AES_256_CBC_SHA512_TAG32:
enc_type = ROC_SE_AES_CBC;
auth_type = ROC_SE_SHA2_SHA512;
digest_len = 32;
break;
case VNET_CRYPTO_ALG_AES_128_CTR_SHA1_TAG12:
case VNET_CRYPTO_ALG_AES_192_CTR_SHA1_TAG12:
case VNET_CRYPTO_ALG_AES_256_CTR_SHA1_TAG12:
enc_type = ROC_SE_AES_CTR;
auth_type = ROC_SE_SHA1_TYPE;
digest_len = 12;
break;
case VNET_CRYPTO_ALG_3DES_CBC_MD5_TAG12:
enc_type = ROC_SE_DES3_CBC;
auth_type = ROC_SE_MD5_TYPE;
digest_len = 12;
break;
case VNET_CRYPTO_ALG_3DES_CBC_SHA1_TAG12:
enc_type = ROC_SE_DES3_CBC;
auth_type = ROC_SE_SHA1_TYPE;
digest_len = 12;
break;
case VNET_CRYPTO_ALG_3DES_CBC_SHA224_TAG14:
enc_type = ROC_SE_DES3_CBC;
auth_type = ROC_SE_SHA2_SHA224;
digest_len = 14;
break;
case VNET_CRYPTO_ALG_3DES_CBC_SHA256_TAG16:
enc_type = ROC_SE_DES3_CBC;
auth_type = ROC_SE_SHA2_SHA256;
digest_len = 16;
break;
case VNET_CRYPTO_ALG_3DES_CBC_SHA384_TAG24:
enc_type = ROC_SE_DES3_CBC;
auth_type = ROC_SE_SHA2_SHA384;
digest_len = 24;
break;
case VNET_CRYPTO_ALG_3DES_CBC_SHA512_TAG32:
enc_type = ROC_SE_DES3_CBC;
auth_type = ROC_SE_SHA2_SHA512;
digest_len = 32;
break;
default:
clib_warning (
"Cryptodev: Undefined link algo %u specified. Key index %u",
key->async_alg, key_index);
return -1;
}
if (type == VNET_CRYPTO_OP_TYPE_ENCRYPT)
sess->cpt_ctx.ciph_then_auth = true;
else
sess->cpt_ctx.auth_then_ciph = true;
sess->iv_length = 16;
sess->cpt_op = type;
crypto_key = vnet_crypto_get_key (key->index_crypto);
rv = roc_se_ciph_key_set (&sess->cpt_ctx, enc_type, crypto_key->data,
vec_len (crypto_key->data));
if (rv)
{
clib_warning ("Cryptodev: Error in setting cipher key for enc type %u",
enc_type);
return -1;
}
auth_key = vnet_crypto_get_key (key->index_integ);
rv = roc_se_auth_key_set (&sess->cpt_ctx, auth_type, auth_key->data,
vec_len (auth_key->data), digest_len);
if (rv)
{
clib_warning ("Cryptodev: Error in setting auth key for auth type %u",
auth_type);
return -1;
}
return 0;
}
static_always_inline i32
oct_crypto_aead_session_update (vlib_main_t *vm, oct_crypto_sess_t *sess,
u32 key_index, u8 type)
{
vnet_crypto_key_t *key = vnet_crypto_get_key (key_index);
roc_se_cipher_type enc_type = 0;
roc_se_auth_type auth_type = 0;
u32 digest_len = ~0;
i32 rv = 0;
switch (key->async_alg)
{
case VNET_CRYPTO_ALG_AES_128_GCM:
case VNET_CRYPTO_ALG_AES_192_GCM:
case VNET_CRYPTO_ALG_AES_256_GCM:
enc_type = ROC_SE_AES_GCM;
sess->aes_gcm = 1;
sess->iv_offset = 0;
sess->iv_length = 16;
sess->cpt_ctx.mac_len = 16;
sess->cpt_op = type;
digest_len = 16;
break;
default:
clib_warning (
"Cryptodev: Undefined cipher algo %u specified. Key index %u",
key->async_alg, key_index);
return -1;
}
rv = roc_se_ciph_key_set (&sess->cpt_ctx, enc_type, key->data,
vec_len (key->data));
if (rv)
{
clib_warning ("Cryptodev: Error in setting cipher key for enc type %u",
enc_type);
return -1;
}
rv = roc_se_auth_key_set (&sess->cpt_ctx, auth_type, NULL, 0, digest_len);
if (rv)
{
clib_warning ("Cryptodev: Error in setting auth key for auth type %u",
auth_type);
return -1;
}
return 0;
}
static_always_inline i32
oct_crypto_session_init (vlib_main_t *vm, oct_crypto_sess_t *session,
vnet_crypto_key_index_t key_index, int op_type)
{
oct_crypto_main_t *ocm = &oct_crypto_main;
vnet_crypto_key_t *key;
oct_crypto_dev_t *ocd;
i32 rv = 0;
ocd = ocm->crypto_dev[op_type];
key = vnet_crypto_get_key (key_index);
if (key->type == VNET_CRYPTO_KEY_TYPE_LINK)
rv = oct_crypto_link_session_update (vm, session, key_index, op_type);
else
rv = oct_crypto_aead_session_update (vm, session, key_index, op_type);
if (rv)
{
oct_crypto_session_free (vm, session);
return -1;
}
session->crypto_dev = ocd;
session->cpt_inst_w7 =
oct_cpt_inst_w7_get (session, session->crypto_dev->roc_cpt);
session->initialised = 1;
return 0;
}
static_always_inline void
oct_crypto_update_frame_error_status (vnet_crypto_async_frame_t *f, u32 index,
vnet_crypto_op_status_t s)
{
u32 i;
for (i = index; i < f->n_elts; i++)
f->elts[i].status = s;
if (index == 0)
f->state = VNET_CRYPTO_FRAME_STATE_NOT_PROCESSED;
}
static_always_inline int
oct_crypto_enqueue_enc_dec (vlib_main_t *vm, vnet_crypto_async_frame_t *frame,
const u8 is_aead, u8 aad_len, const u8 type)
{
u32 i, enq_tail, enc_auth_len, buffer_index, nb_infl_allowed;
struct cpt_inst_s inst[VNET_CRYPTO_FRAME_SIZE];
u32 crypto_start_offset, integ_start_offset;
oct_crypto_main_t *ocm = &oct_crypto_main;
vnet_crypto_async_frame_elt_t *elts;
oct_crypto_dev_t *crypto_dev = NULL;
oct_crypto_inflight_req_t *infl_req;
oct_crypto_pending_queue_t *pend_q;
u64 dptr_start_ptr, curr_ptr;
oct_crypto_sess_t *sess;
u32 crypto_total_length;
oct_crypto_key_t *key;
vlib_buffer_t *buffer;
u16 adj_len;
int ret;
/* GCM packets having 8 bytes of aad and 8 bytes of iv */
u8 aad_iv = 8 + 8;
pend_q = &ocm->pend_q[vlib_get_thread_index ()];
enq_tail = pend_q->enq_tail;
nb_infl_allowed = pend_q->n_desc - pend_q->n_crypto_inflight;
if (PREDICT_FALSE (nb_infl_allowed == 0))
{
oct_crypto_update_frame_error_status (
frame, 0, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
return -1;
}
infl_req = &pend_q->req_queue[enq_tail];
infl_req->frame = frame;
for (i = 0; i < frame->n_elts; i++)
{
elts = &frame->elts[i];
buffer_index = frame->buffer_indices[i];
key = vec_elt_at_index (ocm->keys[type], elts->key_index);
if (PREDICT_FALSE (!key->sess))
{
oct_crypto_update_frame_error_status (
frame, i, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
return -1;
}
sess = key->sess;
if (PREDICT_FALSE (!sess->initialised))
oct_crypto_session_init (vm, sess, elts->key_index, type);
crypto_dev = sess->crypto_dev;
clib_memset (inst + i, 0, sizeof (struct cpt_inst_s));
buffer = vlib_get_buffer (vm, buffer_index);
if (is_aead)
{
dptr_start_ptr =
(u64) (buffer->data + (elts->crypto_start_offset - aad_iv));
curr_ptr = (u64) (buffer->data + buffer->current_data);
adj_len = (u16) (dptr_start_ptr - curr_ptr);
crypto_total_length = elts->crypto_total_length;
crypto_start_offset = aad_iv;
integ_start_offset = 0;
ret = oct_crypto_fill_fc_params (
sess, inst + i, is_aead, aad_len, (u8 *) dptr_start_ptr, elts,
(oct_crypto_scatter_gather_t *) (infl_req->sg_data) + i,
crypto_total_length /* cipher_len */,
crypto_start_offset /* cipher_offset */, 0 /* auth_len */,
integ_start_offset /* auth_off */, buffer, adj_len);
if (PREDICT_FALSE (ret < 0))
{
oct_crypto_update_frame_error_status (
frame, i, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
return -1;
}
}
else
{
dptr_start_ptr = (u64) (buffer->data + elts->integ_start_offset);
enc_auth_len = elts->crypto_total_length + elts->integ_length_adj;
curr_ptr = (u64) (buffer->data + buffer->current_data);
adj_len = (u16) (dptr_start_ptr - curr_ptr);
crypto_total_length = elts->crypto_total_length;
crypto_start_offset =
elts->crypto_start_offset - elts->integ_start_offset;
integ_start_offset = 0;
ret = oct_crypto_fill_fc_params (
sess, inst + i, is_aead, aad_len, (u8 *) dptr_start_ptr, elts,
(oct_crypto_scatter_gather_t *) (infl_req->sg_data) + i,
crypto_total_length /* cipher_len */,
crypto_start_offset /* cipher_offset */,
enc_auth_len /* auth_len */, integ_start_offset /* auth_off */,
buffer, adj_len);
if (PREDICT_FALSE (ret < 0))
{
oct_crypto_update_frame_error_status (
frame, i, VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR);
return -1;
}
}
inst[i].w7.u64 = sess->cpt_inst_w7;
inst[i].res_addr = (u64) &infl_req->res[i];
}
oct_crypto_burst_submit (crypto_dev, inst, frame->n_elts);
infl_req->elts = frame->n_elts;
OCT_MOD_INC (pend_q->enq_tail, pend_q->n_desc);
pend_q->n_crypto_inflight++;
return 0;
}
int
oct_crypto_enqueue_linked_alg_enc (vlib_main_t *vm,
vnet_crypto_async_frame_t *frame)
{
return oct_crypto_enqueue_enc_dec (
vm, frame, 0 /* is_aead */, 0 /* aad_len */, VNET_CRYPTO_OP_TYPE_ENCRYPT);
}
int
oct_crypto_enqueue_linked_alg_dec (vlib_main_t *vm,
vnet_crypto_async_frame_t *frame)
{
return oct_crypto_enqueue_enc_dec (
vm, frame, 0 /* is_aead */, 0 /* aad_len */, VNET_CRYPTO_OP_TYPE_DECRYPT);
}
int
oct_crypto_enqueue_aead_aad_enc (vlib_main_t *vm,
vnet_crypto_async_frame_t *frame, u8 aad_len)
{
return oct_crypto_enqueue_enc_dec (vm, frame, 1 /* is_aead */, aad_len,
VNET_CRYPTO_OP_TYPE_ENCRYPT);
}
static_always_inline int
oct_crypto_enqueue_aead_aad_dec (vlib_main_t *vm,
vnet_crypto_async_frame_t *frame, u8 aad_len)
{
return oct_crypto_enqueue_enc_dec (vm, frame, 1 /* is_aead */, aad_len,
VNET_CRYPTO_OP_TYPE_DECRYPT);
}
int
oct_crypto_enqueue_aead_aad_8_enc (vlib_main_t *vm,
vnet_crypto_async_frame_t *frame)
{
return oct_crypto_enqueue_aead_aad_enc (vm, frame, 8);
}
int
oct_crypto_enqueue_aead_aad_12_enc (vlib_main_t *vm,
vnet_crypto_async_frame_t *frame)
{
return oct_crypto_enqueue_aead_aad_enc (vm, frame, 12);
}
int
oct_crypto_enqueue_aead_aad_8_dec (vlib_main_t *vm,
vnet_crypto_async_frame_t *frame)
{
return oct_crypto_enqueue_aead_aad_dec (vm, frame, 8);
}
int
oct_crypto_enqueue_aead_aad_12_dec (vlib_main_t *vm,
vnet_crypto_async_frame_t *frame)
{
return oct_crypto_enqueue_aead_aad_dec (vm, frame, 12);
}
vnet_crypto_async_frame_t *
oct_crypto_frame_dequeue (vlib_main_t *vm, u32 *nb_elts_processed,
u32 *enqueue_thread_idx)
{
oct_crypto_main_t *ocm = &oct_crypto_main;
u32 deq_head, status = VNET_CRYPTO_OP_STATUS_COMPLETED;
vnet_crypto_async_frame_elt_t *fe = NULL;
oct_crypto_inflight_req_t *infl_req;
oct_crypto_pending_queue_t *pend_q;
vnet_crypto_async_frame_t *frame;
volatile union cpt_res_s *res;
int i;
pend_q = &ocm->pend_q[vlib_get_thread_index ()];
if (!pend_q->n_crypto_inflight)
return NULL;
deq_head = pend_q->deq_head;
infl_req = &pend_q->req_queue[deq_head];
frame = infl_req->frame;
fe = frame->elts;
for (i = infl_req->deq_elts; i < infl_req->elts; ++i)
{
res = &infl_req->res[i];
if (PREDICT_FALSE (res->cn10k.compcode == CPT_COMP_NOT_DONE))
return NULL;
if (PREDICT_FALSE (res->cn10k.uc_compcode))
{
if (res->cn10k.uc_compcode == ROC_SE_ERR_GC_ICV_MISCOMPARE)
status = fe[i].status = VNET_CRYPTO_OP_STATUS_FAIL_BAD_HMAC;
else
status = fe[i].status = VNET_CRYPTO_OP_STATUS_FAIL_ENGINE_ERR;
}
infl_req->deq_elts++;
}
clib_memset ((void *) infl_req->res, 0,
sizeof (union cpt_res_s) * VNET_CRYPTO_FRAME_SIZE);
OCT_MOD_INC (pend_q->deq_head, pend_q->n_desc);
pend_q->n_crypto_inflight--;
frame->state = status == VNET_CRYPTO_OP_STATUS_COMPLETED ?
VNET_CRYPTO_FRAME_STATE_SUCCESS :
VNET_CRYPTO_FRAME_STATE_ELT_ERROR;
*nb_elts_processed = frame->n_elts;
*enqueue_thread_idx = frame->enqueue_thread_index;
infl_req->deq_elts = 0;
infl_req->elts = 0;
return frame;
}
int
oct_init_crypto_engine_handlers (vlib_main_t *vm, vnet_dev_t *dev)
{
u32 engine_index;
engine_index = vnet_crypto_register_engine (vm, "oct_cryptodev", 100,
"OCT Cryptodev Engine");
#define _(n, k, t, a) \
vnet_crypto_register_enqueue_handler ( \
vm, engine_index, VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_ENC, \
oct_crypto_enqueue_aead_aad_##a##_enc); \
vnet_crypto_register_enqueue_handler ( \
vm, engine_index, VNET_CRYPTO_OP_##n##_TAG##t##_AAD##a##_DEC, \
oct_crypto_enqueue_aead_aad_##a##_dec);
foreach_oct_crypto_aead_async_alg
#undef _
#define _(c, h, k, d) \
vnet_crypto_register_enqueue_handler ( \
vm, engine_index, VNET_CRYPTO_OP_##c##_##h##_TAG##d##_ENC, \
oct_crypto_enqueue_linked_alg_enc); \
vnet_crypto_register_enqueue_handler ( \
vm, engine_index, VNET_CRYPTO_OP_##c##_##h##_TAG##d##_DEC, \
oct_crypto_enqueue_linked_alg_dec);
foreach_oct_crypto_link_async_alg;
#undef _
vnet_crypto_register_dequeue_handler (vm, engine_index,
oct_crypto_frame_dequeue);
vnet_crypto_register_key_handler (vm, engine_index, oct_crypto_key_handler);
return 0;
}
int
oct_conf_sw_queue (vlib_main_t *vm, vnet_dev_t *dev)
{
oct_crypto_main_t *ocm = &oct_crypto_main;
vlib_thread_main_t *tm = vlib_get_thread_main ();
extern oct_plt_init_param_t oct_plt_init_param;
oct_crypto_inflight_req_t *infl_req_queue;
u32 n_inflight_req;
int i, j = 0;
ocm->pend_q = oct_plt_init_param.oct_plt_zmalloc (
tm->n_vlib_mains * sizeof (oct_crypto_pending_queue_t),
CLIB_CACHE_LINE_BYTES);
if (ocm->pend_q == NULL)
{
log_err (dev, "Failed to allocate memory for crypto pending queue");
return -1;
}
/*
* Each pending queue will get number of cpt desc / number of cores.
* And that desc count is shared across inflight entries.
*/
n_inflight_req =
(OCT_CPT_LF_MAX_NB_DESC / tm->n_vlib_mains) / VNET_CRYPTO_FRAME_SIZE;
for (i = 0; i < tm->n_vlib_mains; ++i)
{
ocm->pend_q[i].n_desc = n_inflight_req;
ocm->pend_q[i].req_queue = oct_plt_init_param.oct_plt_zmalloc (
ocm->pend_q[i].n_desc * sizeof (oct_crypto_inflight_req_t),
CLIB_CACHE_LINE_BYTES);
if (ocm->pend_q[i].req_queue == NULL)
{
log_err (dev,
"Failed to allocate memory for crypto inflight request");
goto free;
}
for (j = 0; j <= ocm->pend_q[i].n_desc; ++j)
{
infl_req_queue = &ocm->pend_q[i].req_queue[j];
infl_req_queue->sg_data = oct_plt_init_param.oct_plt_zmalloc (
OCT_SCATTER_GATHER_BUFFER_SIZE * VNET_CRYPTO_FRAME_SIZE,
CLIB_CACHE_LINE_BYTES);
if (infl_req_queue->sg_data == NULL)
{
log_err (dev, "Failed to allocate crypto scatter gather memory");
goto free;
}
}
}
return 0;
free:
for (; i >= 0; i--)
{
if (ocm->pend_q[i].req_queue == NULL)
continue;
for (; j >= 0; j--)
{
infl_req_queue = &ocm->pend_q[i].req_queue[j];
if (infl_req_queue->sg_data == NULL)
continue;
oct_plt_init_param.oct_plt_free (infl_req_queue->sg_data);
}
oct_plt_init_param.oct_plt_free (ocm->pend_q[i].req_queue);
}
oct_plt_init_param.oct_plt_free (ocm->pend_q);
return -1;
}
|