1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
|
/*
* Stream co-processor driver for the ETRAX FS
*
* Copyright (C) 2003-2007 Axis Communications AB
*/
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/spinlock.h>
#include <linux/stddef.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/atomic.h>
#include <linux/list.h>
#include <linux/interrupt.h>
#include <asm/signal.h>
#include <asm/irq.h>
#include <dma.h>
#include <hwregs/dma.h>
#include <hwregs/reg_map.h>
#include <hwregs/reg_rdwr.h>
#include <hwregs/intr_vect_defs.h>
#include <hwregs/strcop.h>
#include <hwregs/strcop_defs.h>
#include <cryptocop.h>
#ifdef CONFIG_ETRAXFS
#define IN_DMA 9
#define OUT_DMA 8
#define IN_DMA_INST regi_dma9
#define OUT_DMA_INST regi_dma8
#define DMA_IRQ DMA9_INTR_VECT
#else
#define IN_DMA 3
#define OUT_DMA 2
#define IN_DMA_INST regi_dma3
#define OUT_DMA_INST regi_dma2
#define DMA_IRQ DMA3_INTR_VECT
#endif
#define DESCR_ALLOC_PAD (31)
struct cryptocop_dma_desc {
char *free_buf; /* If non-null will be kfreed in free_cdesc() */
dma_descr_data *dma_descr;
unsigned char dma_descr_buf[sizeof(dma_descr_data) + DESCR_ALLOC_PAD];
unsigned int from_pool:1; /* If 1 'allocated' from the descriptor pool. */
struct cryptocop_dma_desc *next;
};
struct cryptocop_int_operation{
void *alloc_ptr;
cryptocop_session_id sid;
dma_descr_context ctx_out;
dma_descr_context ctx_in;
/* DMA descriptors allocated by driver. */
struct cryptocop_dma_desc *cdesc_out;
struct cryptocop_dma_desc *cdesc_in;
/* Strcop config to use. */
cryptocop_3des_mode tdes_mode;
cryptocop_csum_type csum_mode;
/* DMA descrs provided by consumer. */
dma_descr_data *ddesc_out;
dma_descr_data *ddesc_in;
};
struct cryptocop_tfrm_ctx {
cryptocop_tfrm_id tid;
unsigned int blocklength;
unsigned int start_ix;
struct cryptocop_tfrm_cfg *tcfg;
struct cryptocop_transform_ctx *tctx;
unsigned char previous_src;
unsigned char current_src;
/* Values to use in metadata out. */
unsigned char hash_conf;
unsigned char hash_mode;
unsigned char ciph_conf;
unsigned char cbcmode;
unsigned char decrypt;
unsigned int requires_padding:1;
unsigned int strict_block_length:1;
unsigned int active:1;
unsigned int done:1;
size_t consumed;
size_t produced;
/* Pad (input) descriptors to put in the DMA out list when the transform
* output is put on the DMA in list. */
struct cryptocop_dma_desc *pad_descs;
struct cryptocop_tfrm_ctx *prev_src;
struct cryptocop_tfrm_ctx *curr_src;
/* Mapping to HW. */
unsigned char unit_no;
};
struct cryptocop_private{
cryptocop_session_id sid;
struct cryptocop_private *next;
};
/* Session list. */
struct cryptocop_transform_ctx{
struct cryptocop_transform_init init;
unsigned char dec_key[CRYPTOCOP_MAX_KEY_LENGTH];
unsigned int dec_key_set:1;
struct cryptocop_transform_ctx *next;
};
struct cryptocop_session{
cryptocop_session_id sid;
struct cryptocop_transform_ctx *tfrm_ctx;
struct cryptocop_session *next;
};
/* Priority levels for jobs sent to the cryptocop. Checksum operations from
kernel have highest priority since TCPIP stack processing must not
be a bottleneck. */
typedef enum {
cryptocop_prio_kernel_csum = 0,
cryptocop_prio_kernel = 1,
cryptocop_prio_user = 2,
cryptocop_prio_no_prios = 3
} cryptocop_queue_priority;
struct cryptocop_prio_queue{
struct list_head jobs;
cryptocop_queue_priority prio;
};
struct cryptocop_prio_job{
struct list_head node;
cryptocop_queue_priority prio;
struct cryptocop_operation *oper;
struct cryptocop_int_operation *iop;
};
struct ioctl_job_cb_ctx {
unsigned int processed:1;
};
static struct cryptocop_session *cryptocop_sessions = NULL;
spinlock_t cryptocop_sessions_lock;
/* Next Session ID to assign. */
static cryptocop_session_id next_sid = 1;
/* Pad for checksum. */
static const char csum_zero_pad[1] = {0x00};
/* Trash buffer for mem2mem operations. */
#define MEM2MEM_DISCARD_BUF_LENGTH (512)
static unsigned char mem2mem_discard_buf[MEM2MEM_DISCARD_BUF_LENGTH];
/* Descriptor pool. */
/* FIXME Tweak this value. */
#define CRYPTOCOP_DESCRIPTOR_POOL_SIZE (100)
static struct cryptocop_dma_desc descr_pool[CRYPTOCOP_DESCRIPTOR_POOL_SIZE];
static struct cryptocop_dma_desc *descr_pool_free_list;
static int descr_pool_no_free;
static spinlock_t descr_pool_lock;
/* Lock to stop cryptocop to start processing of a new operation. The holder
of this lock MUST call cryptocop_start_job() after it is unlocked. */
spinlock_t cryptocop_process_lock;
static struct cryptocop_prio_queue cryptocop_job_queues[cryptocop_prio_no_prios];
static spinlock_t cryptocop_job_queue_lock;
static struct cryptocop_prio_job *cryptocop_running_job = NULL;
static spinlock_t running_job_lock;
/* The interrupt handler appends completed jobs to this list. The scehduled
* tasklet removes them upon sending the response to the crypto consumer. */
static struct list_head cryptocop_completed_jobs;
static spinlock_t cryptocop_completed_jobs_lock;
DECLARE_WAIT_QUEUE_HEAD(cryptocop_ioc_process_wq);
/** Local functions. **/
static int cryptocop_open(struct inode *, struct file *);
static int cryptocop_release(struct inode *, struct file *);
static long cryptocop_ioctl(struct file *file,
unsigned int cmd, unsigned long arg);
static void cryptocop_start_job(void);
static int cryptocop_job_queue_insert(cryptocop_queue_priority prio, struct cryptocop_operation *operation);
static int cryptocop_job_setup(struct cryptocop_prio_job **pj, struct cryptocop_operation *operation);
static int cryptocop_job_queue_init(void);
static void cryptocop_job_queue_close(void);
static int create_md5_pad(int alloc_flag, unsigned long long hashed_length, char **pad, size_t *pad_length);
static int create_sha1_pad(int alloc_flag, unsigned long long hashed_length, char **pad, size_t *pad_length);
static int transform_ok(struct cryptocop_transform_init *tinit);
static struct cryptocop_session *get_session(cryptocop_session_id sid);
static struct cryptocop_transform_ctx *get_transform_ctx(struct cryptocop_session *sess, cryptocop_tfrm_id tid);
static void delete_internal_operation(struct cryptocop_int_operation *iop);
static void get_aes_decrypt_key(unsigned char *dec_key, const unsigned char *key, unsigned int keylength);
static int init_stream_coprocessor(void);
static void __exit exit_stream_coprocessor(void);
/*#define LDEBUG*/
#ifdef LDEBUG
#define DEBUG(s) s
#define DEBUG_API(s) s
static void print_cryptocop_operation(struct cryptocop_operation *cop);
static void print_dma_descriptors(struct cryptocop_int_operation *iop);
static void print_strcop_crypto_op(struct strcop_crypto_op *cop);
static void print_lock_status(void);
static void print_user_dma_lists(struct cryptocop_dma_list_operation *dma_op);
#define assert(s) do{if (!(s)) panic(#s);} while(0);
#else
#define DEBUG(s)
#define DEBUG_API(s)
#define assert(s)
#endif
/* Transform constants. */
#define DES_BLOCK_LENGTH (8)
#define AES_BLOCK_LENGTH (16)
#define MD5_BLOCK_LENGTH (64)
#define SHA1_BLOCK_LENGTH (64)
#define CSUM_BLOCK_LENGTH (2)
#define MD5_STATE_LENGTH (16)
#define SHA1_STATE_LENGTH (20)
/* The device number. */
#define CRYPTOCOP_MAJOR (254)
#define CRYPTOCOP_MINOR (0)
const struct file_operations cryptocop_fops = {
.owner = THIS_MODULE,
.open = cryptocop_open,
.release = cryptocop_release,
.unlocked_ioctl = cryptocop_ioctl,
.llseek = noop_llseek,
};
static void free_cdesc(struct cryptocop_dma_desc *cdesc)
{
DEBUG(printk("free_cdesc: cdesc 0x%p, from_pool=%d\n", cdesc, cdesc->from_pool));
kfree(cdesc->free_buf);
if (cdesc->from_pool) {
unsigned long int flags;
spin_lock_irqsave(&descr_pool_lock, flags);
cdesc->next = descr_pool_free_list;
descr_pool_free_list = cdesc;
++descr_pool_no_free;
spin_unlock_irqrestore(&descr_pool_lock, flags);
} else {
kfree(cdesc);
}
}
static struct cryptocop_dma_desc *alloc_cdesc(int alloc_flag)
{
int use_pool = (alloc_flag & GFP_ATOMIC) ? 1 : 0;
struct cryptocop_dma_desc *cdesc;
if (use_pool) {
unsigned long int flags;
spin_lock_irqsave(&descr_pool_lock, flags);
if (!descr_pool_free_list) {
spin_unlock_irqrestore(&descr_pool_lock, flags);
DEBUG_API(printk("alloc_cdesc: pool is empty\n"));
return NULL;
}
cdesc = descr_pool_free_list;
descr_pool_free_list = descr_pool_free_list->next;
--descr_pool_no_free;
spin_unlock_irqrestore(&descr_pool_lock, flags);
cdesc->from_pool = 1;
} else {
cdesc = kmalloc(sizeof(struct cryptocop_dma_desc), alloc_flag);
if (!cdesc) {
DEBUG_API(printk("alloc_cdesc: kmalloc\n"));
return NULL;
}
cdesc->from_pool = 0;
}
cdesc->dma_descr = (dma_descr_data*)(((unsigned long int)cdesc + offsetof(struct cryptocop_dma_desc, dma_descr_buf) + DESCR_ALLOC_PAD) & ~0x0000001F);
cdesc->next = NULL;
cdesc->free_buf = NULL;
cdesc->dma_descr->out_eop = 0;
cdesc->dma_descr->in_eop = 0;
cdesc->dma_descr->intr = 0;
cdesc->dma_descr->eol = 0;
cdesc->dma_descr->wait = 0;
cdesc->dma_descr->buf = NULL;
cdesc->dma_descr->after = NULL;
DEBUG_API(printk("alloc_cdesc: return 0x%p, cdesc->dma_descr=0x%p, from_pool=%d\n", cdesc, cdesc->dma_descr, cdesc->from_pool));
return cdesc;
}
static void setup_descr_chain(struct cryptocop_dma_desc *cd)
{
DEBUG(printk("setup_descr_chain: entering\n"));
while (cd) {
if (cd->next) {
cd->dma_descr->next = (dma_descr_data*)virt_to_phys(cd->next->dma_descr);
} else {
cd->dma_descr->next = NULL;
}
cd = cd->next;
}
DEBUG(printk("setup_descr_chain: exit\n"));
}
/* Create a pad descriptor for the transform.
* Return -1 for error, 0 if pad created. */
static int create_pad_descriptor(struct cryptocop_tfrm_ctx *tc, struct cryptocop_dma_desc **pad_desc, int alloc_flag)
{
struct cryptocop_dma_desc *cdesc = NULL;
int error = 0;
struct strcop_meta_out mo = {
.ciphsel = src_none,
.hashsel = src_none,
.csumsel = src_none
};
char *pad;
size_t plen;
DEBUG(printk("create_pad_descriptor: start.\n"));
/* Setup pad descriptor. */
DEBUG(printk("create_pad_descriptor: setting up padding.\n"));
cdesc = alloc_cdesc(alloc_flag);
if (!cdesc){
DEBUG_API(printk("create_pad_descriptor: alloc pad desc\n"));
goto error_cleanup;
}
switch (tc->unit_no) {
case src_md5:
error = create_md5_pad(alloc_flag, tc->consumed, &pad, &plen);
if (error){
DEBUG_API(printk("create_pad_descriptor: create_md5_pad_failed\n"));
goto error_cleanup;
}
cdesc->free_buf = pad;
mo.hashsel = src_dma;
mo.hashconf = tc->hash_conf;
mo.hashmode = tc->hash_mode;
break;
case src_sha1:
error = create_sha1_pad(alloc_flag, tc->consumed, &pad, &plen);
if (error){
DEBUG_API(printk("create_pad_descriptor: create_sha1_pad_failed\n"));
goto error_cleanup;
}
cdesc->free_buf = pad;
mo.hashsel = src_dma;
mo.hashconf = tc->hash_conf;
mo.hashmode = tc->hash_mode;
break;
case src_csum:
if (tc->consumed % tc->blocklength){
pad = (char*)csum_zero_pad;
plen = 1;
} else {
pad = (char*)cdesc; /* Use any pointer. */
plen = 0;
}
mo.csumsel = src_dma;
break;
}
cdesc->dma_descr->wait = 1;
cdesc->dma_descr->out_eop = 1; /* Since this is a pad output is pushed. EOP is ok here since the padded unit is the only one active. */
cdesc->dma_descr->buf = (char*)virt_to_phys((char*)pad);
cdesc->dma_descr->after = cdesc->dma_descr->buf + plen;
cdesc->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_out, mo);
*pad_desc = cdesc;
return 0;
error_cleanup:
if (cdesc) free_cdesc(cdesc);
return -1;
}
static int setup_key_dl_desc(struct cryptocop_tfrm_ctx *tc, struct cryptocop_dma_desc **kd, int alloc_flag)
{
struct cryptocop_dma_desc *key_desc = alloc_cdesc(alloc_flag);
struct strcop_meta_out mo = {0};
DEBUG(printk("setup_key_dl_desc\n"));
if (!key_desc) {
DEBUG_API(printk("setup_key_dl_desc: failed descriptor allocation.\n"));
return -ENOMEM;
}
/* Download key. */
if ((tc->tctx->init.alg == cryptocop_alg_aes) && (tc->tcfg->flags & CRYPTOCOP_DECRYPT)) {
/* Precook the AES decrypt key. */
if (!tc->tctx->dec_key_set){
get_aes_decrypt_key(tc->tctx->dec_key, tc->tctx->init.key, tc->tctx->init.keylen);
tc->tctx->dec_key_set = 1;
}
key_desc->dma_descr->buf = (char*)virt_to_phys(tc->tctx->dec_key);
key_desc->dma_descr->after = key_desc->dma_descr->buf + tc->tctx->init.keylen/8;
} else {
key_desc->dma_descr->buf = (char*)virt_to_phys(tc->tctx->init.key);
key_desc->dma_descr->after = key_desc->dma_descr->buf + tc->tctx->init.keylen/8;
}
/* Setup metadata. */
mo.dlkey = 1;
switch (tc->tctx->init.keylen) {
case 64:
mo.decrypt = 0;
mo.hashmode = 0;
break;
case 128:
mo.decrypt = 0;
mo.hashmode = 1;
break;
case 192:
mo.decrypt = 1;
mo.hashmode = 0;
break;
case 256:
mo.decrypt = 1;
mo.hashmode = 1;
break;
default:
break;
}
mo.ciphsel = mo.hashsel = mo.csumsel = src_none;
key_desc->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_out, mo);
key_desc->dma_descr->out_eop = 1;
key_desc->dma_descr->wait = 1;
key_desc->dma_descr->intr = 0;
*kd = key_desc;
return 0;
}
static int setup_cipher_iv_desc(struct cryptocop_tfrm_ctx *tc, struct cryptocop_dma_desc **id, int alloc_flag)
{
struct cryptocop_dma_desc *iv_desc = alloc_cdesc(alloc_flag);
struct strcop_meta_out mo = {0};
DEBUG(printk("setup_cipher_iv_desc\n"));
if (!iv_desc) {
DEBUG_API(printk("setup_cipher_iv_desc: failed CBC IV descriptor allocation.\n"));
return -ENOMEM;
}
/* Download IV. */
iv_desc->dma_descr->buf = (char*)virt_to_phys(tc->tcfg->iv);
iv_desc->dma_descr->after = iv_desc->dma_descr->buf + tc->blocklength;
/* Setup metadata. */
mo.hashsel = mo.csumsel = src_none;
mo.ciphsel = src_dma;
mo.ciphconf = tc->ciph_conf;
mo.cbcmode = tc->cbcmode;
iv_desc->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_out, mo);
iv_desc->dma_descr->out_eop = 0;
iv_desc->dma_descr->wait = 1;
iv_desc->dma_descr->intr = 0;
*id = iv_desc;
return 0;
}
/* Map the ouput length of the transform to operation output starting on the inject index. */
static int create_input_descriptors(struct cryptocop_operation *operation, struct cryptocop_tfrm_ctx *tc, struct cryptocop_dma_desc **id, int alloc_flag)
{
int err = 0;
struct cryptocop_dma_desc head = {0};
struct cryptocop_dma_desc *outdesc = &head;
size_t iov_offset = 0;
size_t out_ix = 0;
int outiov_ix = 0;
struct strcop_meta_in mi = {0};
size_t out_length = tc->produced;
int rem_length;
int dlength;
assert(out_length != 0);
if (((tc->produced + tc->tcfg->inject_ix) > operation->tfrm_op.outlen) || (tc->produced && (operation->tfrm_op.outlen == 0))) {
DEBUG_API(printk("create_input_descriptors: operation outdata too small\n"));
return -EINVAL;
}
/* Traverse the out iovec until the result inject index is reached. */
while ((outiov_ix < operation->tfrm_op.outcount) && ((out_ix + operation->tfrm_op.outdata[outiov_ix].iov_len) <= tc->tcfg->inject_ix)){
out_ix += operation->tfrm_op.outdata[outiov_ix].iov_len;
outiov_ix++;
}
if (outiov_ix >= operation->tfrm_op.outcount){
DEBUG_API(printk("create_input_descriptors: operation outdata too small\n"));
return -EINVAL;
}
iov_offset = tc->tcfg->inject_ix - out_ix;
mi.dmasel = tc->unit_no;
/* Setup the output descriptors. */
while ((out_length > 0) && (outiov_ix < operation->tfrm_op.outcount)) {
outdesc->next = alloc_cdesc(alloc_flag);
if (!outdesc->next) {
DEBUG_API(printk("create_input_descriptors: alloc_cdesc\n"));
err = -ENOMEM;
goto error_cleanup;
}
outdesc = outdesc->next;
rem_length = operation->tfrm_op.outdata[outiov_ix].iov_len - iov_offset;
dlength = (out_length < rem_length) ? out_length : rem_length;
DEBUG(printk("create_input_descriptors:\n"
"outiov_ix=%d, rem_length=%d, dlength=%d\n"
"iov_offset=%d, outdata[outiov_ix].iov_len=%d\n"
"outcount=%d, outiov_ix=%d\n",
outiov_ix, rem_length, dlength, iov_offset, operation->tfrm_op.outdata[outiov_ix].iov_len, operation->tfrm_op.outcount, outiov_ix));
outdesc->dma_descr->buf = (char*)virt_to_phys(operation->tfrm_op.outdata[outiov_ix].iov_base + iov_offset);
outdesc->dma_descr->after = outdesc->dma_descr->buf + dlength;
outdesc->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_in, mi);
out_length -= dlength;
iov_offset += dlength;
if (iov_offset >= operation->tfrm_op.outdata[outiov_ix].iov_len) {
iov_offset = 0;
++outiov_ix;
}
}
if (out_length > 0){
DEBUG_API(printk("create_input_descriptors: not enough room for output, %d remained\n", out_length));
err = -EINVAL;
goto error_cleanup;
}
/* Set sync in last descriptor. */
mi.sync = 1;
outdesc->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_in, mi);
*id = head.next;
return 0;
error_cleanup:
while (head.next) {
outdesc = head.next->next;
free_cdesc(head.next);
head.next = outdesc;
}
return err;
}
static int create_output_descriptors(struct cryptocop_operation *operation, int *iniov_ix, int *iniov_offset, size_t desc_len, struct cryptocop_dma_desc **current_out_cdesc, struct strcop_meta_out *meta_out, int alloc_flag)
{
while (desc_len != 0) {
struct cryptocop_dma_desc *cdesc;
int rem_length = operation->tfrm_op.indata[*iniov_ix].iov_len - *iniov_offset;
int dlength = (desc_len < rem_length) ? desc_len : rem_length;
cdesc = alloc_cdesc(alloc_flag);
if (!cdesc) {
DEBUG_API(printk("create_output_descriptors: alloc_cdesc\n"));
return -ENOMEM;
}
(*current_out_cdesc)->next = cdesc;
(*current_out_cdesc) = cdesc;
cdesc->free_buf = NULL;
cdesc->dma_descr->buf = (char*)virt_to_phys(operation->tfrm_op.indata[*iniov_ix].iov_base + *iniov_offset);
cdesc->dma_descr->after = cdesc->dma_descr->buf + dlength;
assert(desc_len >= dlength);
desc_len -= dlength;
*iniov_offset += dlength;
if (*iniov_offset >= operation->tfrm_op.indata[*iniov_ix].iov_len) {
*iniov_offset = 0;
++(*iniov_ix);
if (*iniov_ix > operation->tfrm_op.incount) {
DEBUG_API(printk("create_output_descriptors: not enough indata in operation."));
return -EINVAL;
}
}
cdesc->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_out, (*meta_out));
} /* while (desc_len != 0) */
/* Last DMA descriptor gets a 'wait' bit to signal expected change in metadata. */
(*current_out_cdesc)->dma_descr->wait = 1; /* This will set extraneous WAIT in some situations, e.g. when padding hashes and checksums. */
return 0;
}
static int append_input_descriptors(struct cryptocop_operation *operation, struct cryptocop_dma_desc **current_in_cdesc, struct cryptocop_dma_desc **current_out_cdesc, struct cryptocop_tfrm_ctx *tc, int alloc_flag)
{
DEBUG(printk("append_input_descriptors, tc=0x%p, unit_no=%d\n", tc, tc->unit_no));
if (tc->tcfg) {
int failed = 0;
struct cryptocop_dma_desc *idescs = NULL;
DEBUG(printk("append_input_descriptors: pushing output, consumed %d produced %d bytes.\n", tc->consumed, tc->produced));
if (tc->pad_descs) {
DEBUG(printk("append_input_descriptors: append pad descriptors to DMA out list.\n"));
while (tc->pad_descs) {
DEBUG(printk("append descriptor 0x%p\n", tc->pad_descs));
(*current_out_cdesc)->next = tc->pad_descs;
tc->pad_descs = tc->pad_descs->next;
(*current_out_cdesc) = (*current_out_cdesc)->next;
}
}
/* Setup and append output descriptors to DMA in list. */
if (tc->unit_no == src_dma){
/* mem2mem. Setup DMA in descriptors to discard all input prior to the requested mem2mem data. */
struct strcop_meta_in mi = {.sync = 0, .dmasel = src_dma};
unsigned int start_ix = tc->start_ix;
while (start_ix){
unsigned int desclen = start_ix < MEM2MEM_DISCARD_BUF_LENGTH ? start_ix : MEM2MEM_DISCARD_BUF_LENGTH;
(*current_in_cdesc)->next = alloc_cdesc(alloc_flag);
if (!(*current_in_cdesc)->next){
DEBUG_API(printk("append_input_descriptors: alloc_cdesc mem2mem discard failed\n"));
return -ENOMEM;
}
(*current_in_cdesc) = (*current_in_cdesc)->next;
(*current_in_cdesc)->dma_descr->buf = (char*)virt_to_phys(mem2mem_discard_buf);
(*current_in_cdesc)->dma_descr->after = (*current_in_cdesc)->dma_descr->buf + desclen;
(*current_in_cdesc)->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_in, mi);
start_ix -= desclen;
}
mi.sync = 1;
(*current_in_cdesc)->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_in, mi);
}
failed = create_input_descriptors(operation, tc, &idescs, alloc_flag);
if (failed){
DEBUG_API(printk("append_input_descriptors: output descriptor setup failed\n"));
return failed;
}
DEBUG(printk("append_input_descriptors: append output descriptors to DMA in list.\n"));
while (idescs) {
DEBUG(printk("append descriptor 0x%p\n", idescs));
(*current_in_cdesc)->next = idescs;
idescs = idescs->next;
(*current_in_cdesc) = (*current_in_cdesc)->next;
}
}
return 0;
}
static int cryptocop_setup_dma_list(struct cryptocop_operation *operation, struct cryptocop_int_operation **int_op, int alloc_flag)
{
struct cryptocop_session *sess;
struct cryptocop_transform_ctx *tctx;
struct cryptocop_tfrm_ctx digest_ctx = {
.previous_src = src_none,
.current_src = src_none,
.start_ix = 0,
.requires_padding = 1,
.strict_block_length = 0,
.hash_conf = 0,
.hash_mode = 0,
.ciph_conf = 0,
.cbcmode = 0,
.decrypt = 0,
.consumed = 0,
.produced = 0,
.pad_descs = NULL,
.active = 0,
.done = 0,
.prev_src = NULL,
.curr_src = NULL,
.tcfg = NULL};
struct cryptocop_tfrm_ctx cipher_ctx = {
.previous_src = src_none,
.current_src = src_none,
.start_ix = 0,
.requires_padding = 0,
.strict_block_length = 1,
.hash_conf = 0,
.hash_mode = 0,
.ciph_conf = 0,
.cbcmode = 0,
.decrypt = 0,
.consumed = 0,
.produced = 0,
.pad_descs = NULL,
.active = 0,
.done = 0,
.prev_src = NULL,
.curr_src = NULL,
.tcfg = NULL};
struct cryptocop_tfrm_ctx csum_ctx = {
.previous_src = src_none,
.current_src = src_none,
.start_ix = 0,
.blocklength = 2,
.requires_padding = 1,
.strict_block_length = 0,
.hash_conf = 0,
.hash_mode = 0,
.ciph_conf = 0,
.cbcmode = 0,
.decrypt = 0,
.consumed = 0,
.produced = 0,
.pad_descs = NULL,
.active = 0,
.done = 0,
.tcfg = NULL,
.prev_src = NULL,
.curr_src = NULL,
.unit_no = src_csum};
struct cryptocop_tfrm_cfg *tcfg = operation->tfrm_op.tfrm_cfg;
unsigned int indata_ix = 0;
/* iovec accounting. */
int iniov_ix = 0;
int iniov_offset = 0;
/* Operation descriptor cfg traversal pointer. */
struct cryptocop_desc *odsc;
int failed = 0;
/* List heads for allocated descriptors. */
struct cryptocop_dma_desc out_cdesc_head = {0};
struct cryptocop_dma_desc in_cdesc_head = {0};
struct cryptocop_dma_desc *current_out_cdesc = &out_cdesc_head;
struct cryptocop_dma_desc *current_in_cdesc = &in_cdesc_head;
struct cryptocop_tfrm_ctx *output_tc = NULL;
void *iop_alloc_ptr;
assert(operation != NULL);
assert(int_op != NULL);
DEBUG(printk("cryptocop_setup_dma_list: start\n"));
DEBUG(print_cryptocop_operation(operation));
sess = get_session(operation->sid);
if (!sess) {
DEBUG_API(printk("cryptocop_setup_dma_list: no session found for operation.\n"));
failed = -EINVAL;
goto error_cleanup;
}
iop_alloc_ptr = kmalloc(DESCR_ALLOC_PAD + sizeof(struct cryptocop_int_operation), alloc_flag);
if (!iop_alloc_ptr) {
DEBUG_API(printk("cryptocop_setup_dma_list: kmalloc cryptocop_int_operation\n"));
failed = -ENOMEM;
goto error_cleanup;
}
(*int_op) = (struct cryptocop_int_operation*)(((unsigned long int)(iop_alloc_ptr + DESCR_ALLOC_PAD + offsetof(struct cryptocop_int_operation, ctx_out)) & ~0x0000001F) - offsetof(struct cryptocop_int_operation, ctx_out));
DEBUG(memset((*int_op), 0xff, sizeof(struct cryptocop_int_operation)));
(*int_op)->alloc_ptr = iop_alloc_ptr;
DEBUG(printk("cryptocop_setup_dma_list: *int_op=0x%p, alloc_ptr=0x%p\n", *int_op, (*int_op)->alloc_ptr));
(*int_op)->sid = operation->sid;
(*int_op)->cdesc_out = NULL;
(*int_op)->cdesc_in = NULL;
(*int_op)->tdes_mode = cryptocop_3des_ede;
(*int_op)->csum_mode = cryptocop_csum_le;
(*int_op)->ddesc_out = NULL;
(*int_op)->ddesc_in = NULL;
/* Scan operation->tfrm_op.tfrm_cfg for bad configuration and set up the local contexts. */
if (!tcfg) {
DEBUG_API(printk("cryptocop_setup_dma_list: no configured transforms in operation.\n"));
failed = -EINVAL;
goto error_cleanup;
}
while (tcfg) {
tctx = get_transform_ctx(sess, tcfg->tid);
if (!tctx) {
DEBUG_API(printk("cryptocop_setup_dma_list: no transform id %d in session.\n", tcfg->tid));
failed = -EINVAL;
goto error_cleanup;
}
if (tcfg->inject_ix > operation->tfrm_op.outlen){
DEBUG_API(printk("cryptocop_setup_dma_list: transform id %d inject_ix (%d) > operation->tfrm_op.outlen(%d)", tcfg->tid, tcfg->inject_ix, operation->tfrm_op.outlen));
failed = -EINVAL;
goto error_cleanup;
}
switch (tctx->init.alg){
case cryptocop_alg_mem2mem:
if (cipher_ctx.tcfg != NULL){
DEBUG_API(printk("cryptocop_setup_dma_list: multiple ciphers in operation.\n"));
failed = -EINVAL;
goto error_cleanup;
}
/* mem2mem is handled as a NULL cipher. */
cipher_ctx.cbcmode = 0;
cipher_ctx.decrypt = 0;
cipher_ctx.blocklength = 1;
cipher_ctx.ciph_conf = 0;
cipher_ctx.unit_no = src_dma;
cipher_ctx.tcfg = tcfg;
cipher_ctx.tctx = tctx;
break;
case cryptocop_alg_des:
case cryptocop_alg_3des:
case cryptocop_alg_aes:
/* cipher */
if (cipher_ctx.tcfg != NULL){
DEBUG_API(printk("cryptocop_setup_dma_list: multiple ciphers in operation.\n"));
failed = -EINVAL;
goto error_cleanup;
}
cipher_ctx.tcfg = tcfg;
cipher_ctx.tctx = tctx;
if (cipher_ctx.tcfg->flags & CRYPTOCOP_DECRYPT){
cipher_ctx.decrypt = 1;
}
switch (tctx->init.cipher_mode) {
case cryptocop_cipher_mode_ecb:
cipher_ctx.cbcmode = 0;
break;
case cryptocop_cipher_mode_cbc:
cipher_ctx.cbcmode = 1;
break;
default:
DEBUG_API(printk("cryptocop_setup_dma_list: cipher_ctx, bad cipher mode==%d\n", tctx->init.cipher_mode));
failed = -EINVAL;
goto error_cleanup;
}
DEBUG(printk("cryptocop_setup_dma_list: cipher_ctx, set CBC mode==%d\n", cipher_ctx.cbcmode));
switch (tctx->init.alg){
case cryptocop_alg_des:
cipher_ctx.ciph_conf = 0;
cipher_ctx.unit_no = src_des;
cipher_ctx.blocklength = DES_BLOCK_LENGTH;
break;
case cryptocop_alg_3des:
cipher_ctx.ciph_conf = 1;
cipher_ctx.unit_no = src_des;
cipher_ctx.blocklength = DES_BLOCK_LENGTH;
break;
case cryptocop_alg_aes:
cipher_ctx.ciph_conf = 2;
cipher_ctx.unit_no = src_aes;
cipher_ctx.blocklength = AES_BLOCK_LENGTH;
break;
default:
panic("cryptocop_setup_dma_list: impossible algorithm %d\n", tctx->init.alg);
}
(*int_op)->tdes_mode = tctx->init.tdes_mode;
break;
case cryptocop_alg_md5:
case cryptocop_alg_sha1:
/* digest */
if (digest_ctx.tcfg != NULL){
DEBUG_API(printk("cryptocop_setup_dma_list: multiple digests in operation.\n"));
failed = -EINVAL;
goto error_cleanup;
}
digest_ctx.tcfg = tcfg;
digest_ctx.tctx = tctx;
digest_ctx.hash_mode = 0; /* Don't use explicit IV in this API. */
switch (tctx->init.alg){
case cryptocop_alg_md5:
digest_ctx.blocklength = MD5_BLOCK_LENGTH;
digest_ctx.unit_no = src_md5;
digest_ctx.hash_conf = 1; /* 1 => MD-5 */
break;
case cryptocop_alg_sha1:
digest_ctx.blocklength = SHA1_BLOCK_LENGTH;
digest_ctx.unit_no = src_sha1;
digest_ctx.hash_conf = 0; /* 0 => SHA-1 */
break;
default:
panic("cryptocop_setup_dma_list: impossible digest algorithm\n");
}
break;
case cryptocop_alg_csum:
/* digest */
if (csum_ctx.tcfg != NULL){
DEBUG_API(printk("cryptocop_setup_dma_list: multiple checksums in operation.\n"));
failed = -EINVAL;
goto error_cleanup;
}
(*int_op)->csum_mode = tctx->init.csum_mode;
csum_ctx.tcfg = tcfg;
csum_ctx.tctx = tctx;
break;
default:
/* no algorithm. */
DEBUG_API(printk("cryptocop_setup_dma_list: invalid algorithm %d specified in tfrm %d.\n", tctx->init.alg, tcfg->tid));
failed = -EINVAL;
goto error_cleanup;
}
tcfg = tcfg->next;
}
/* Download key if a cipher is used. */
if (cipher_ctx.tcfg && (cipher_ctx.tctx->init.alg != cryptocop_alg_mem2mem)){
struct cryptocop_dma_desc *key_desc = NULL;
failed = setup_key_dl_desc(&cipher_ctx, &key_desc, alloc_flag);
if (failed) {
DEBUG_API(printk("cryptocop_setup_dma_list: setup key dl\n"));
goto error_cleanup;
}
current_out_cdesc->next = key_desc;
current_out_cdesc = key_desc;
indata_ix += (unsigned int)(key_desc->dma_descr->after - key_desc->dma_descr->buf);
/* Download explicit IV if a cipher is used and CBC mode and explicit IV selected. */
if ((cipher_ctx.tctx->init.cipher_mode == cryptocop_cipher_mode_cbc) && (cipher_ctx.tcfg->flags & CRYPTOCOP_EXPLICIT_IV)) {
struct cryptocop_dma_desc *iv_desc = NULL;
DEBUG(printk("cryptocop_setup_dma_list: setup cipher CBC IV descriptor.\n"));
failed = setup_cipher_iv_desc(&cipher_ctx, &iv_desc, alloc_flag);
if (failed) {
DEBUG_API(printk("cryptocop_setup_dma_list: CBC IV descriptor.\n"));
goto error_cleanup;
}
current_out_cdesc->next = iv_desc;
current_out_cdesc = iv_desc;
indata_ix += (unsigned int)(iv_desc->dma_descr->after - iv_desc->dma_descr->buf);
}
}
/* Process descriptors. */
odsc = operation->tfrm_op.desc;
while (odsc) {
struct cryptocop_desc_cfg *dcfg = odsc->cfg;
struct strcop_meta_out meta_out = {0};
size_t desc_len = odsc->length;
int active_count, eop_needed_count;
output_tc = NULL;
DEBUG(printk("cryptocop_setup_dma_list: parsing an operation descriptor\n"));
while (dcfg) {
struct cryptocop_tfrm_ctx *tc = NULL;
DEBUG(printk("cryptocop_setup_dma_list: parsing an operation descriptor configuration.\n"));
/* Get the local context for the transform and mark it as the output unit if it produces output. */
if (digest_ctx.tcfg && (digest_ctx.tcfg->tid == dcfg->tid)){
tc = &digest_ctx;
} else if (cipher_ctx.tcfg && (cipher_ctx.tcfg->tid == dcfg->tid)){
tc = &cipher_ctx;
} else if (csum_ctx.tcfg && (csum_ctx.tcfg->tid == dcfg->tid)){
tc = &csum_ctx;
}
if (!tc) {
DEBUG_API(printk("cryptocop_setup_dma_list: invalid transform %d specified in descriptor.\n", dcfg->tid));
failed = -EINVAL;
goto error_cleanup;
}
if (tc->done) {
DEBUG_API(printk("cryptocop_setup_dma_list: completed transform %d reused.\n", dcfg->tid));
failed = -EINVAL;
goto error_cleanup;
}
if (!tc->active) {
tc->start_ix = indata_ix;
tc->active = 1;
}
tc->previous_src = tc->current_src;
tc->prev_src = tc->curr_src;
/* Map source unit id to DMA source config. */
switch (dcfg->src){
case cryptocop_source_dma:
tc->current_src = src_dma;
break;
case cryptocop_source_des:
tc->current_src = src_des;
break;
case cryptocop_source_3des:
tc->current_src = src_des;
break;
case cryptocop_source_aes:
tc->current_src = src_aes;
break;
case cryptocop_source_md5:
case cryptocop_source_sha1:
case cryptocop_source_csum:
case cryptocop_source_none:
default:
/* We do not allow using accumulating style units (SHA-1, MD5, checksum) as sources to other units.
*/
DEBUG_API(printk("cryptocop_setup_dma_list: bad unit source configured %d.\n", dcfg->src));
failed = -EINVAL;
goto error_cleanup;
}
if (tc->current_src != src_dma) {
/* Find the unit we are sourcing from. */
if (digest_ctx.unit_no == tc->current_src){
tc->curr_src = &digest_ctx;
} else if (cipher_ctx.unit_no == tc->current_src){
tc->curr_src = &cipher_ctx;
} else if (csum_ctx.unit_no == tc->current_src){
tc->curr_src = &csum_ctx;
}
if ((tc->curr_src == tc) && (tc->unit_no != src_dma)){
DEBUG_API(printk("cryptocop_setup_dma_list: unit %d configured to source from itself.\n", tc->unit_no));
failed = -EINVAL;
goto error_cleanup;
}
} else {
tc->curr_src = NULL;
}
/* Detect source switch. */
DEBUG(printk("cryptocop_setup_dma_list: tc->active=%d tc->unit_no=%d tc->current_src=%d tc->previous_src=%d, tc->curr_src=0x%p, tc->prev_srv=0x%p\n", tc->active, tc->unit_no, tc->current_src, tc->previous_src, tc->curr_src, tc->prev_src));
if (tc->active && (tc->current_src != tc->previous_src)) {
/* Only allow source switch when both the old source unit and the new one have
* no pending data to process (i.e. the consumed length must be a multiple of the
* transform blocklength). */
/* Note: if the src == NULL we are actually sourcing from DMA out. */
if (((tc->prev_src != NULL) && (tc->prev_src->consumed % tc->prev_src->blocklength)) ||
((tc->curr_src != NULL) && (tc->curr_src->consumed % tc->curr_src->blocklength)))
{
DEBUG_API(printk("cryptocop_setup_dma_list: can only disconnect from or connect to a unit on a multiple of the blocklength, old: cons=%d, prod=%d, block=%d, new: cons=%d prod=%d, block=%d.\n", tc->prev_src ? tc->prev_src->consumed : INT_MIN, tc->prev_src ? tc->prev_src->produced : INT_MIN, tc->prev_src ? tc->prev_src->blocklength : INT_MIN, tc->curr_src ? tc->curr_src->consumed : INT_MIN, tc->curr_src ? tc->curr_src->produced : INT_MIN, tc->curr_src ? tc->curr_src->blocklength : INT_MIN));
failed = -EINVAL;
goto error_cleanup;
}
}
/* Detect unit deactivation. */
if (dcfg->last) {
/* Length check of this is handled below. */
tc->done = 1;
}
dcfg = dcfg->next;
} /* while (dcfg) */
DEBUG(printk("cryptocop_setup_dma_list: parsing operation descriptor configuration complete.\n"));
if (cipher_ctx.active && (cipher_ctx.curr_src != NULL) && !cipher_ctx.curr_src->active){
DEBUG_API(printk("cryptocop_setup_dma_list: cipher source from inactive unit %d\n", cipher_ctx.curr_src->unit_no));
failed = -EINVAL;
goto error_cleanup;
}
if (digest_ctx.active && (digest_ctx.curr_src != NULL) && !digest_ctx.curr_src->active){
DEBUG_API(printk("cryptocop_setup_dma_list: digest source from inactive unit %d\n", digest_ctx.curr_src->unit_no));
failed = -EINVAL;
goto error_cleanup;
}
if (csum_ctx.active && (csum_ctx.curr_src != NULL) && !csum_ctx.curr_src->active){
DEBUG_API(printk("cryptocop_setup_dma_list: cipher source from inactive unit %d\n", csum_ctx.curr_src->unit_no));
failed = -EINVAL;
goto error_cleanup;
}
/* Update consumed and produced lengths.
The consumed length accounting here is actually cheating. If a unit source from DMA (or any
other unit that process data in blocks of one octet) it is correct, but if it source from a
block processing unit, i.e. a cipher, it will be temporarily incorrect at some times. However
since it is only allowed--by the HW--to change source to or from a block processing unit at times where that
unit has processed an exact multiple of its block length the end result will be correct.
Beware that if the source change restriction change this code will need to be (much) reworked.
*/
DEBUG(printk("cryptocop_setup_dma_list: desc->length=%d, desc_len=%d.\n", odsc->length, desc_len));
if (csum_ctx.active) {
csum_ctx.consumed += desc_len;
if (csum_ctx.done) {
csum_ctx.produced = 2;
}
DEBUG(printk("cryptocop_setup_dma_list: csum_ctx producing: consumed=%d, produced=%d, blocklength=%d.\n", csum_ctx.consumed, csum_ctx.produced, csum_ctx.blocklength));
}
if (digest_ctx.active) {
digest_ctx.consumed += desc_len;
if (digest_ctx.done) {
if (digest_ctx.unit_no == src_md5) {
digest_ctx.produced = MD5_STATE_LENGTH;
} else {
digest_ctx.produced = SHA1_STATE_LENGTH;
}
}
DEBUG(printk("cryptocop_setup_dma_list: digest_ctx producing: consumed=%d, produced=%d, blocklength=%d.\n", digest_ctx.consumed, digest_ctx.produced, digest_ctx.blocklength));
}
if (cipher_ctx.active) {
/* Ciphers are allowed only to source from DMA out. That is filtered above. */
assert(cipher_ctx.current_src == src_dma);
cipher_ctx.consumed += desc_len;
cipher_ctx.produced = cipher_ctx.blocklength * (cipher_ctx.consumed / cipher_ctx.blocklength);
if (cipher_ctx.cbcmode && !(cipher_ctx.tcfg->flags & CRYPTOCOP_EXPLICIT_IV) && cipher_ctx.produced){
cipher_ctx.produced -= cipher_ctx.blocklength; /* Compensate for CBC iv. */
}
DEBUG(printk("cryptocop_setup_dma_list: cipher_ctx producing: consumed=%d, produced=%d, blocklength=%d.\n", cipher_ctx.consumed, cipher_ctx.produced, cipher_ctx.blocklength));
}
/* Setup the DMA out descriptors. */
/* Configure the metadata. */
active_count = 0;
eop_needed_count = 0;
if (cipher_ctx.active) {
++active_count;
if (cipher_ctx.unit_no == src_dma){
/* mem2mem */
meta_out.ciphsel = src_none;
} else {
meta_out.ciphsel = cipher_ctx.current_src;
}
meta_out.ciphconf = cipher_ctx.ciph_conf;
meta_out.cbcmode = cipher_ctx.cbcmode;
meta_out.decrypt = cipher_ctx.decrypt;
DEBUG(printk("set ciphsel=%d ciphconf=%d cbcmode=%d decrypt=%d\n", meta_out.ciphsel, meta_out.ciphconf, meta_out.cbcmode, meta_out.decrypt));
if (cipher_ctx.done) ++eop_needed_count;
} else {
meta_out.ciphsel = src_none;
}
if (digest_ctx.active) {
++active_count;
meta_out.hashsel = digest_ctx.current_src;
meta_out.hashconf = digest_ctx.hash_conf;
meta_out.hashmode = 0; /* Explicit mode is not used here. */
DEBUG(printk("set hashsel=%d hashconf=%d hashmode=%d\n", meta_out.hashsel, meta_out.hashconf, meta_out.hashmode));
if (digest_ctx.done) {
assert(digest_ctx.pad_descs == NULL);
failed = create_pad_descriptor(&digest_ctx, &digest_ctx.pad_descs, alloc_flag);
if (failed) {
DEBUG_API(printk("cryptocop_setup_dma_list: failed digest pad creation.\n"));
goto error_cleanup;
}
}
} else {
meta_out.hashsel = src_none;
}
if (csum_ctx.active) {
++active_count;
meta_out.csumsel = csum_ctx.current_src;
if (csum_ctx.done) {
assert(csum_ctx.pad_descs == NULL);
failed = create_pad_descriptor(&csum_ctx, &csum_ctx.pad_descs, alloc_flag);
if (failed) {
DEBUG_API(printk("cryptocop_setup_dma_list: failed csum pad creation.\n"));
goto error_cleanup;
}
}
} else {
meta_out.csumsel = src_none;
}
DEBUG(printk("cryptocop_setup_dma_list: %d eop needed, %d active units\n", eop_needed_count, active_count));
/* Setup DMA out descriptors for the indata. */
failed = create_output_descriptors(operation, &iniov_ix, &iniov_offset, desc_len, ¤t_out_cdesc, &meta_out, alloc_flag);
if (failed) {
DEBUG_API(printk("cryptocop_setup_dma_list: create_output_descriptors %d\n", failed));
goto error_cleanup;
}
/* Setup out EOP. If there are active units that are not done here they cannot get an EOP
* so we ust setup a zero length descriptor to DMA to signal EOP only to done units.
* If there is a pad descriptor EOP for the padded unit will be EOPed by it.
*/
assert(active_count >= eop_needed_count);
assert((eop_needed_count == 0) || (eop_needed_count == 1));
if (eop_needed_count) {
/* This means that the bulk operation (cipeher/m2m) is terminated. */
if (active_count > 1) {
/* Use zero length EOP descriptor. */
struct cryptocop_dma_desc *ed = alloc_cdesc(alloc_flag);
struct strcop_meta_out ed_mo = {0};
if (!ed) {
DEBUG_API(printk("cryptocop_setup_dma_list: alloc EOP descriptor for cipher\n"));
failed = -ENOMEM;
goto error_cleanup;
}
assert(cipher_ctx.active && cipher_ctx.done);
if (cipher_ctx.unit_no == src_dma){
/* mem2mem */
ed_mo.ciphsel = src_none;
} else {
ed_mo.ciphsel = cipher_ctx.current_src;
}
ed_mo.ciphconf = cipher_ctx.ciph_conf;
ed_mo.cbcmode = cipher_ctx.cbcmode;
ed_mo.decrypt = cipher_ctx.decrypt;
ed->free_buf = NULL;
ed->dma_descr->wait = 1;
ed->dma_descr->out_eop = 1;
ed->dma_descr->buf = (char*)virt_to_phys(&ed); /* Use any valid physical address for zero length descriptor. */
ed->dma_descr->after = ed->dma_descr->buf;
ed->dma_descr->md = REG_TYPE_CONV(unsigned short int, struct strcop_meta_out, ed_mo);
current_out_cdesc->next = ed;
current_out_cdesc = ed;
} else {
/* Set EOP in the current out descriptor since the only active module is
* the one needing the EOP. */
current_out_cdesc->dma_descr->out_eop = 1;
}
}
if (cipher_ctx.done && cipher_ctx.active) cipher_ctx.active = 0;
if (digest_ctx.done && digest_ctx.active) digest_ctx.active = 0;
if (csum_ctx.done && csum_ctx.active) csum_ctx.active = 0;
indata_ix += odsc->length;
odsc = odsc->next;
} /* while (odsc) */ /* Process descriptors. */
DEBUG(printk("cryptocop_setup_dma_list: done parsing operation descriptors\n"));
if (cipher_ctx.tcfg && (cipher_ctx.active || !cipher_ctx.done)){
DEBUG_API(printk("cryptocop_setup_dma_list: cipher operation not terminated.\n"));
failed = -EINVAL;
goto error_cleanup;
}
if (digest_ctx.tcfg && (digest_ctx.active || !digest_ctx.done)){
DEBUG_API(printk("cryptocop_setup_dma_list: digest operation not terminated.\n"));
failed = -EINVAL;
goto error_cleanup;
}
if (csum_ctx.tcfg && (csum_ctx.active || !csum_ctx.done)){
DEBUG_API(printk("cryptocop_setup_dma_list: csum operation not terminated.\n"));
failed = -EINVAL;
goto error_cleanup;
}
failed = append_input_descriptors(operation, ¤t_in_cdesc, ¤t_out_cdesc, &cipher_ctx, alloc_flag);
if (failed){
DEBUG_API(printk("cryptocop_setup_dma_list: append_input_descriptors cipher_ctx %d\n", failed));
goto error_cleanup;
}
failed = append_input_descriptors(operation, ¤t_in_cdesc, ¤t_out_cdesc, &digest_ctx, alloc_flag);
if (failed){
DEBUG_API(printk("cryptocop_setup_dma_list: append_input_descriptors cipher_ctx %d\n", failed));
goto error_cleanup;
}
failed = append_input_descriptors(operation, ¤t_in_cdesc, ¤t_out_cdesc, &csum_ctx, alloc_flag);
if (failed){
DEBUG_API(printk("cryptocop_setup_dma_list: append_input_descriptors cipher_ctx %d\n", failed));
goto error_cleanup;
}
DEBUG(printk("cryptocop_setup_dma_list: int_op=0x%p, *int_op=0x%p\n", int_op, *int_op));
(*int_op)->cdesc_out = out_cdesc_head.next;
(*int_op)->cdesc_in = in_cdesc_head.next;
DEBUG(printk("cryptocop_setup_dma_list: out_cdesc_head=0x%p in_cdesc_head=0x%p\n", (*int_op)->cdesc_out, (*int_op)->cdesc_in));
setup_descr_chain(out_cdesc_head.next);
setup_descr_chain(in_cdesc_head.next);
/* Last but not least: mark the last DMA in descriptor for a INTR and EOL and the the
* last DMA out descriptor for EOL.
*/
current_in_cdesc->dma_descr->intr = 1;
current_in_cdesc->dma_descr->eol = 1;
current_out_cdesc->dma_descr->eol = 1;
/* Setup DMA contexts. */
(*int_op)->ctx_out.next = NULL;
(*int_op)->ctx_out.eol = 1;
(*int_op)->ctx_out.intr = 0;
(*int_op)->ctx_out.store_mode = 0;
(*int_op)->ctx_out.en = 0;
(*int_op)->ctx_out.dis = 0;
(*int_op)->ctx_out.md0 = 0;
(*int_op)->ctx_out.md1 = 0;
(*int_op)->ctx_out.md2 = 0;
(*int_op)->ctx_out.md3 = 0;
(*int_op)->ctx_out.md4 = 0;
(*int_op)->ctx_out.saved_data = (dma_descr_data*)virt_to_phys((*int_op)->cdesc_out->dma_descr);
(*int_op)->ctx_out.saved_data_buf = (*int_op)->cdesc_out->dma_descr->buf; /* Already physical address. */
(*int_op)->ctx_in.next = NULL;
(*int_op)->ctx_in.eol = 1;
(*int_op)->ctx_in.intr = 0;
(*int_op)->ctx_in.store_mode = 0;
(*int_op)->ctx_in.en = 0;
(*int_op)->ctx_in.dis = 0;
(*int_op)->ctx_in.md0 = 0;
(*int_op)->ctx_in.md1 = 0;
(*int_op)->ctx_in.md2 = 0;
(*int_op)->ctx_in.md3 = 0;
(*int_op)->ctx_in.md4 = 0;
(*int_op)->ctx_in.saved_data = (dma_descr_data*)virt_to_phys((*int_op)->cdesc_in->dma_descr);
(*int_op)->ctx_in.saved_data_buf = (*int_op)->cdesc_in->dma_descr->buf; /* Already physical address. */
DEBUG(printk("cryptocop_setup_dma_list: done\n"));
return 0;
error_cleanup:
{
/* Free all allocated resources. */
struct cryptocop_dma_desc *tmp_cdesc;
while (digest_ctx.pad_descs){
tmp_cdesc = digest_ctx.pad_descs->next;
free_cdesc(digest_ctx.pad_descs);
digest_ctx.pad_descs = tmp_cdesc;
}
while (csum_ctx.pad_descs){
tmp_cdesc = csum_ctx.pad_descs->next;
free_cdesc(csum_ctx.pad_descs);
csum_ctx.pad_descs = tmp_cdesc;
}
assert(cipher_ctx.pad_descs == NULL); /* The ciphers are never padded. */
if (*int_op != NULL) delete_internal_operation(*int_op);
}
DEBUG_API(printk("cryptocop_setup_dma_list: done with error %d\n", failed));
return failed;
}
static void delete_internal_operation(struct cryptocop_int_operation *iop)
{
void *ptr = iop->alloc_ptr;
struct cryptocop_dma_desc *cd = iop->cdesc_out;
struct cryptocop_dma_desc *next;
DEBUG(printk("delete_internal_operation: iop=0x%p, alloc_ptr=0x%p\n", iop, ptr));
while (cd) {
next = cd->next;
free_cdesc(cd);
cd = next;
}
cd = iop->cdesc_in;
while (cd) {
next = cd->next;
free_cdesc(cd);
cd = next;
}
kfree(ptr);
}
#define MD5_MIN_PAD_LENGTH (9)
#define MD5_PAD_LENGTH_FIELD_LENGTH (8)
static int create_md5_pad(int alloc_flag, unsigned long long hashed_length, char **pad, size_t *pad_length)
{
size_t padlen = MD5_BLOCK_LENGTH - (hashed_length % MD5_BLOCK_LENGTH);
unsigned char *p;
int i;
unsigned long long int bit_length = hashed_length << 3;
if (padlen < MD5_MIN_PAD_LENGTH) padlen += MD5_BLOCK_LENGTH;
p = kmalloc(padlen, alloc_flag);
if (!p) return -ENOMEM;
*p = 0x80;
memset(p+1, 0, padlen - 1);
DEBUG(printk("create_md5_pad: hashed_length=%lld bits == %lld bytes\n", bit_length, hashed_length));
i = padlen - MD5_PAD_LENGTH_FIELD_LENGTH;
while (bit_length != 0){
p[i++] = bit_length % 0x100;
bit_length >>= 8;
}
*pad = (char*)p;
*pad_length = padlen;
return 0;
}
#define SHA1_MIN_PAD_LENGTH (9)
#define SHA1_PAD_LENGTH_FIELD_LENGTH (8)
static int create_sha1_pad(int alloc_flag, unsigned long long hashed_length, char **pad, size_t *pad_length)
{
size_t padlen = SHA1_BLOCK_LENGTH - (hashed_length % SHA1_BLOCK_LENGTH);
unsigned char *p;
int i;
unsigned long long int bit_length = hashed_length << 3;
if (padlen < SHA1_MIN_PAD_LENGTH) padlen += SHA1_BLOCK_LENGTH;
p = kmalloc(padlen, alloc_flag);
if (!p) return -ENOMEM;
*p = 0x80;
memset(p+1, 0, padlen - 1);
DEBUG(printk("create_sha1_pad: hashed_length=%lld bits == %lld bytes\n", bit_length, hashed_length));
i = padlen - 1;
while (bit_length != 0){
p[i--] = bit_length % 0x100;
bit_length >>= 8;
}
*pad = (char*)p;
*pad_length = padlen;
return 0;
}
static int transform_ok(struct cryptocop_transform_init *tinit)
{
switch (tinit->alg){
case cryptocop_alg_csum:
switch (tinit->csum_mode){
case cryptocop_csum_le:
case cryptocop_csum_be:
break;
default:
DEBUG_API(printk("transform_ok: Bad mode set for csum transform\n"));
return -EINVAL;
}
case cryptocop_alg_mem2mem:
case cryptocop_alg_md5:
case cryptocop_alg_sha1:
if (tinit->keylen != 0) {
DEBUG_API(printk("transform_ok: non-zero keylength, %d, for a digest/csum algorithm\n", tinit->keylen));
return -EINVAL; /* This check is a bit strict. */
}
break;
case cryptocop_alg_des:
if (tinit->keylen != 64) {
DEBUG_API(printk("transform_ok: keylen %d invalid for DES\n", tinit->keylen));
return -EINVAL;
}
break;
case cryptocop_alg_3des:
if (tinit->keylen != 192) {
DEBUG_API(printk("transform_ok: keylen %d invalid for 3DES\n", tinit->keylen));
return -EINVAL;
}
break;
case cryptocop_alg_aes:
if (tinit->keylen != 128 && tinit->keylen != 192 && tinit->keylen != 256) {
DEBUG_API(printk("transform_ok: keylen %d invalid for AES\n", tinit->keylen));
return -EINVAL;
}
break;
case cryptocop_no_alg:
default:
DEBUG_API(printk("transform_ok: no such algorithm %d\n", tinit->alg));
return -EINVAL;
}
switch (tinit->alg){
case cryptocop_alg_des:
case cryptocop_alg_3des:
case cryptocop_alg_aes:
if (tinit->cipher_mode != cryptocop_cipher_mode_ecb && tinit->cipher_mode != cryptocop_cipher_mode_cbc) return -EINVAL;
default:
break;
}
return 0;
}
int cryptocop_new_session(cryptocop_session_id *sid, struct cryptocop_transform_init *tinit, int alloc_flag)
{
struct cryptocop_session *sess;
struct cryptocop_transform_init *tfrm_in = tinit;
struct cryptocop_transform_init *tmp_in;
int no_tfrms = 0;
int i;
unsigned long int flags;
init_stream_coprocessor(); /* For safety if we are called early */
while (tfrm_in){
int err;
++no_tfrms;
if ((err = transform_ok(tfrm_in))) {
DEBUG_API(printk("cryptocop_new_session, bad transform\n"));
return err;
}
tfrm_in = tfrm_in->next;
}
if (0 == no_tfrms) {
DEBUG_API(printk("cryptocop_new_session, no transforms specified\n"));
return -EINVAL;
}
sess = kmalloc(sizeof(struct cryptocop_session), alloc_flag);
if (!sess){
DEBUG_API(printk("cryptocop_new_session, kmalloc cryptocop_session\n"));
return -ENOMEM;
}
sess->tfrm_ctx = kmalloc(no_tfrms * sizeof(struct cryptocop_transform_ctx), alloc_flag);
if (!sess->tfrm_ctx) {
DEBUG_API(printk("cryptocop_new_session, kmalloc cryptocop_transform_ctx\n"));
kfree(sess);
return -ENOMEM;
}
tfrm_in = tinit;
for (i = 0; i < no_tfrms; i++){
tmp_in = tfrm_in->next;
while (tmp_in){
if (tmp_in->tid == tfrm_in->tid) {
DEBUG_API(printk("cryptocop_new_session, duplicate transform ids\n"));
kfree(sess->tfrm_ctx);
kfree(sess);
return -EINVAL;
}
tmp_in = tmp_in->next;
}
memcpy(&sess->tfrm_ctx[i].init, tfrm_in, sizeof(struct cryptocop_transform_init));
sess->tfrm_ctx[i].dec_key_set = 0;
sess->tfrm_ctx[i].next = &sess->tfrm_ctx[i] + 1;
tfrm_in = tfrm_in->next;
}
sess->tfrm_ctx[i-1].next = NULL;
spin_lock_irqsave(&cryptocop_sessions_lock, flags);
sess->sid = next_sid;
next_sid++;
/* TODO If we are really paranoid we should do duplicate check to handle sid wraparound.
* OTOH 2^64 is a really large number of session. */
if (next_sid == 0) next_sid = 1;
/* Prepend to session list. */
sess->next = cryptocop_sessions;
cryptocop_sessions = sess;
spin_unlock_irqrestore(&cryptocop_sessions_lock, flags);
*sid = sess->sid;
return 0;
}
int cryptocop_free_session(cryptocop_session_id sid)
{
struct cryptocop_transform_ctx *tc;
struct cryptocop_session *sess = NULL;
struct cryptocop_session *psess = NULL;
unsigned long int flags;
int i;
LIST_HEAD(remove_list);
struct list_head *node, *tmp;
struct cryptocop_prio_job *pj;
DEBUG(printk("cryptocop_free_session: sid=%lld\n", sid));
spin_lock_irqsave(&cryptocop_sessions_lock, flags);
sess = cryptocop_sessions;
while (sess && sess->sid != sid){
psess = sess;
sess = sess->next;
}
if (sess){
if (psess){
psess->next = sess->next;
} else {
cryptocop_sessions = sess->next;
}
}
spin_unlock_irqrestore(&cryptocop_sessions_lock, flags);
if (!sess) return -EINVAL;
/* Remove queued jobs. */
spin_lock_irqsave(&cryptocop_job_queue_lock, flags);
for (i = 0; i < cryptocop_prio_no_prios; i++){
if (!list_empty(&(cryptocop_job_queues[i].jobs))){
list_for_each_safe(node, tmp, &(cryptocop_job_queues[i].jobs)) {
pj = list_entry(node, struct cryptocop_prio_job, node);
if (pj->oper->sid == sid) {
list_move_tail(node, &remove_list);
}
}
}
}
spin_unlock_irqrestore(&cryptocop_job_queue_lock, flags);
list_for_each_safe(node, tmp, &remove_list) {
list_del(node);
pj = list_entry(node, struct cryptocop_prio_job, node);
pj->oper->operation_status = -EAGAIN; /* EAGAIN is not ideal for job/session terminated but it's the best choice I know of. */
DEBUG(printk("cryptocop_free_session: pj=0x%p, pj->oper=0x%p, pj->iop=0x%p\n", pj, pj->oper, pj->iop));
pj->oper->cb(pj->oper, pj->oper->cb_data);
delete_internal_operation(pj->iop);
kfree(pj);
}
tc = sess->tfrm_ctx;
/* Erase keying data. */
while (tc){
DEBUG(printk("cryptocop_free_session: memset keys, tfrm id=%d\n", tc->init.tid));
memset(tc->init.key, 0xff, CRYPTOCOP_MAX_KEY_LENGTH);
memset(tc->dec_key, 0xff, CRYPTOCOP_MAX_KEY_LENGTH);
tc = tc->next;
}
kfree(sess->tfrm_ctx);
kfree(sess);
return 0;
}
static struct cryptocop_session *get_session(cryptocop_session_id sid)
{
struct cryptocop_session *sess;
unsigned long int flags;
spin_lock_irqsave(&cryptocop_sessions_lock, flags);
sess = cryptocop_sessions;
while (sess && (sess->sid != sid)){
sess = sess->next;
}
spin_unlock_irqrestore(&cryptocop_sessions_lock, flags);
return sess;
}
static struct cryptocop_transform_ctx *get_transform_ctx(struct cryptocop_session *sess, cryptocop_tfrm_id tid)
{
struct cryptocop_transform_ctx *tc = sess->tfrm_ctx;
DEBUG(printk("get_transform_ctx, sess=0x%p, tid=%d\n", sess, tid));
assert(sess != NULL);
while (tc && tc->init.tid != tid){
DEBUG(printk("tc=0x%p, tc->next=0x%p\n", tc, tc->next));
tc = tc->next;
}
DEBUG(printk("get_transform_ctx, returning tc=0x%p\n", tc));
return tc;
}
/* The AES s-transform matrix (s-box). */
static const u8 aes_sbox[256] = {
99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118,
202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192,
183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21,
4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117,
9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132,
83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207,
208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168,
81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210,
205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115,
96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219,
224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121,
231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8,
186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138,
112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158,
225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223,
140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22
};
/* AES has a 32 bit word round constants for each round in the
* key schedule. round_constant[i] is really Rcon[i+1] in FIPS187.
*/
static u32 round_constant[11] = {
0x01000000, 0x02000000, 0x04000000, 0x08000000,
0x10000000, 0x20000000, 0x40000000, 0x80000000,
0x1B000000, 0x36000000, 0x6C000000
};
/* Apply the s-box to each of the four occtets in w. */
static u32 aes_ks_subword(const u32 w)
{
u8 bytes[4];
*(u32*)(&bytes[0]) = w;
bytes[0] = aes_sbox[bytes[0]];
bytes[1] = aes_sbox[bytes[1]];
bytes[2] = aes_sbox[bytes[2]];
bytes[3] = aes_sbox[bytes[3]];
return *(u32*)(&bytes[0]);
}
/* The encrypt (forward) Rijndael key schedule algorithm pseudo code:
* (Note that AES words are 32 bit long)
*
* KeyExpansion(byte key[4*Nk], word w[Nb*(Nr+1)], Nk){
* word temp
* i = 0
* while (i < Nk) {
* w[i] = word(key[4*i, 4*i + 1, 4*i + 2, 4*i + 3])
* i = i + 1
* }
* i = Nk
*
* while (i < (Nb * (Nr + 1))) {
* temp = w[i - 1]
* if ((i mod Nk) == 0) {
* temp = SubWord(RotWord(temp)) xor Rcon[i/Nk]
* }
* else if ((Nk > 6) && ((i mod Nk) == 4)) {
* temp = SubWord(temp)
* }
* w[i] = w[i - Nk] xor temp
* }
* RotWord(t) does a 8 bit cyclic shift left on a 32 bit word.
* SubWord(t) applies the AES s-box individually to each octet
* in a 32 bit word.
*
* For AES Nk can have the values 4, 6, and 8 (corresponding to
* values for Nr of 10, 12, and 14). Nb is always 4.
*
* To construct w[i], w[i - 1] and w[i - Nk] must be
* available. Consequently we must keep a state of the last Nk words
* to be able to create the last round keys.
*/
static void get_aes_decrypt_key(unsigned char *dec_key, const unsigned char *key, unsigned int keylength)
{
u32 temp;
u32 w_ring[8]; /* nk is max 8, use elements 0..(nk - 1) as a ringbuffer */
u8 w_last_ix;
int i;
u8 nr, nk;
switch (keylength){
case 128:
nk = 4;
nr = 10;
break;
case 192:
nk = 6;
nr = 12;
break;
case 256:
nk = 8;
nr = 14;
break;
default:
panic("stream co-processor: bad aes key length in get_aes_decrypt_key\n");
};
/* Need to do host byte order correction here since key is byte oriented and the
* kx algorithm is word (u32) oriented. */
for (i = 0; i < nk; i+=1) {
w_ring[i] = be32_to_cpu(*(u32*)&key[4*i]);
}
i = (int)nk;
w_last_ix = i - 1;
while (i < (4 * (nr + 2))) {
temp = w_ring[w_last_ix];
if (!(i % nk)) {
/* RotWord(temp) */
temp = (temp << 8) | (temp >> 24);
temp = aes_ks_subword(temp);
temp ^= round_constant[i/nk - 1];
} else if ((nk > 6) && ((i % nk) == 4)) {
temp = aes_ks_subword(temp);
}
w_last_ix = (w_last_ix + 1) % nk; /* This is the same as (i-Nk) mod Nk */
temp ^= w_ring[w_last_ix];
w_ring[w_last_ix] = temp;
/* We need the round keys for round Nr+1 and Nr+2 (round key
* Nr+2 is the round key beyond the last one used when
* encrypting). Rounds are numbered starting from 0, Nr=10
* implies 11 rounds are used in encryption/decryption.
*/
if (i >= (4 * nr)) {
/* Need to do host byte order correction here, the key
* is byte oriented. */
*(u32*)dec_key = cpu_to_be32(temp);
dec_key += 4;
}
++i;
}
}
/**** Job/operation management. ****/
int cryptocop_job_queue_insert_csum(struct cryptocop_operation *operation)
{
return cryptocop_job_queue_insert(cryptocop_prio_kernel_csum, operation);
}
int cryptocop_job_queue_insert_crypto(struct cryptocop_operation *operation)
{
return cryptocop_job_queue_insert(cryptocop_prio_kernel, operation);
}
int cryptocop_job_queue_insert_user_job(struct cryptocop_operation *operation)
{
return cryptocop_job_queue_insert(cryptocop_prio_user, operation);
}
static int cryptocop_job_queue_insert(cryptocop_queue_priority prio, struct cryptocop_operation *operation)
{
int ret;
struct cryptocop_prio_job *pj = NULL;
unsigned long int flags;
DEBUG(printk("cryptocop_job_queue_insert(%d, 0x%p)\n", prio, operation));
if (!operation || !operation->cb){
DEBUG_API(printk("cryptocop_job_queue_insert oper=0x%p, NULL operation or callback\n", operation));
return -EINVAL;
}
if ((ret = cryptocop_job_setup(&pj, operation)) != 0){
DEBUG_API(printk("cryptocop_job_queue_insert: job setup failed\n"));
return ret;
}
assert(pj != NULL);
spin_lock_irqsave(&cryptocop_job_queue_lock, flags);
list_add_tail(&pj->node, &cryptocop_job_queues[prio].jobs);
spin_unlock_irqrestore(&cryptocop_job_queue_lock, flags);
/* Make sure a job is running */
cryptocop_start_job();
return 0;
}
static void cryptocop_do_tasklet(unsigned long unused);
DECLARE_TASKLET (cryptocop_tasklet, cryptocop_do_tasklet, 0);
static void cryptocop_do_tasklet(unsigned long unused)
{
struct list_head *node;
struct cryptocop_prio_job *pj = NULL;
unsigned long flags;
DEBUG(printk("cryptocop_do_tasklet: entering\n"));
do {
spin_lock_irqsave(&cryptocop_completed_jobs_lock, flags);
if (!list_empty(&cryptocop_completed_jobs)){
node = cryptocop_completed_jobs.next;
list_del(node);
pj = list_entry(node, struct cryptocop_prio_job, node);
} else {
pj = NULL;
}
spin_unlock_irqrestore(&cryptocop_completed_jobs_lock, flags);
if (pj) {
assert(pj->oper != NULL);
/* Notify consumer of operation completeness. */
DEBUG(printk("cryptocop_do_tasklet: callback 0x%p, data 0x%p\n", pj->oper->cb, pj->oper->cb_data));
pj->oper->operation_status = 0; /* Job is completed. */
pj->oper->cb(pj->oper, pj->oper->cb_data);
delete_internal_operation(pj->iop);
kfree(pj);
}
} while (pj != NULL);
DEBUG(printk("cryptocop_do_tasklet: exiting\n"));
}
static irqreturn_t
dma_done_interrupt(int irq, void *dev_id)
{
struct cryptocop_prio_job *done_job;
reg_dma_rw_ack_intr ack_intr = {
.data = 1,
};
REG_WR(dma, IN_DMA_INST, rw_ack_intr, ack_intr);
DEBUG(printk("cryptocop DMA done\n"));
spin_lock(&running_job_lock);
if (cryptocop_running_job == NULL){
printk("stream co-processor got interrupt when not busy\n");
spin_unlock(&running_job_lock);
return IRQ_HANDLED;
}
done_job = cryptocop_running_job;
cryptocop_running_job = NULL;
spin_unlock(&running_job_lock);
/* Start processing a job. */
if (!spin_trylock(&cryptocop_process_lock)){
DEBUG(printk("cryptocop irq handler, not starting a job\n"));
} else {
cryptocop_start_job();
spin_unlock(&cryptocop_process_lock);
}
done_job->oper->operation_status = 0; /* Job is completed. */
if (done_job->oper->fast_callback){
/* This operation wants callback from interrupt. */
done_job->oper->cb(done_job->oper, done_job->oper->cb_data);
delete_internal_operation(done_job->iop);
kfree(done_job);
} else {
spin_lock(&cryptocop_completed_jobs_lock);
list_add_tail(&(done_job->node), &cryptocop_completed_jobs);
spin_unlock(&cryptocop_completed_jobs_lock);
tasklet_schedule(&cryptocop_tasklet);
}
DEBUG(printk("cryptocop leave irq handler\n"));
return IRQ_HANDLED;
}
/* Setup interrupts and DMA channels. */
static int init_cryptocop(void)
{
unsigned long flags;
reg_dma_rw_cfg dma_cfg = {.en = 1};
reg_dma_rw_intr_mask intr_mask_in = {.data = regk_dma_yes}; /* Only want descriptor interrupts from the DMA in channel. */
reg_dma_rw_ack_intr ack_intr = {.data = 1,.in_eop = 1 };
reg_strcop_rw_cfg strcop_cfg = {
.ipend = regk_strcop_little,
.td1 = regk_strcop_e,
.td2 = regk_strcop_d,
.td3 = regk_strcop_e,
.ignore_sync = 0,
.en = 1
};
if (request_irq(DMA_IRQ, dma_done_interrupt, 0,
"stream co-processor DMA", NULL))
panic("request_irq stream co-processor irq dma9");
(void)crisv32_request_dma(OUT_DMA, "strcop", DMA_PANIC_ON_ERROR,
0, dma_strp);
(void)crisv32_request_dma(IN_DMA, "strcop", DMA_PANIC_ON_ERROR,
0, dma_strp);
local_irq_save(flags);
/* Reset and enable the cryptocop. */
strcop_cfg.en = 0;
REG_WR(strcop, regi_strcop, rw_cfg, strcop_cfg);
strcop_cfg.en = 1;
REG_WR(strcop, regi_strcop, rw_cfg, strcop_cfg);
/* Enable DMAs. */
REG_WR(dma, IN_DMA_INST, rw_cfg, dma_cfg); /* input DMA */
REG_WR(dma, OUT_DMA_INST, rw_cfg, dma_cfg); /* output DMA */
/* Set up wordsize = 4 for DMAs. */
DMA_WR_CMD(OUT_DMA_INST, regk_dma_set_w_size4);
DMA_WR_CMD(IN_DMA_INST, regk_dma_set_w_size4);
/* Enable interrupts. */
REG_WR(dma, IN_DMA_INST, rw_intr_mask, intr_mask_in);
/* Clear intr ack. */
REG_WR(dma, IN_DMA_INST, rw_ack_intr, ack_intr);
local_irq_restore(flags);
return 0;
}
/* Free used cryptocop hw resources (interrupt and DMA channels). */
static void release_cryptocop(void)
{
unsigned long flags;
reg_dma_rw_cfg dma_cfg = {.en = 0};
reg_dma_rw_intr_mask intr_mask_in = {0};
reg_dma_rw_ack_intr ack_intr = {.data = 1,.in_eop = 1 };
local_irq_save(flags);
/* Clear intr ack. */
REG_WR(dma, IN_DMA_INST, rw_ack_intr, ack_intr);
/* Disable DMAs. */
REG_WR(dma, IN_DMA_INST, rw_cfg, dma_cfg); /* input DMA */
REG_WR(dma, OUT_DMA_INST, rw_cfg, dma_cfg); /* output DMA */
/* Disable interrupts. */
REG_WR(dma, IN_DMA_INST, rw_intr_mask, intr_mask_in);
local_irq_restore(flags);
free_irq(DMA_IRQ, NULL);
(void)crisv32_free_dma(OUT_DMA);
(void)crisv32_free_dma(IN_DMA);
}
/* Init job queue. */
static int cryptocop_job_queue_init(void)
{
int i;
INIT_LIST_HEAD(&cryptocop_completed_jobs);
for (i = 0; i < cryptocop_prio_no_prios; i++){
cryptocop_job_queues[i].prio = (cryptocop_queue_priority)i;
INIT_LIST_HEAD(&cryptocop_job_queues[i].jobs);
}
return 0;
}
static void cryptocop_job_queue_close(void)
{
struct list_head *node, *tmp;
struct cryptocop_prio_job *pj = NULL;
unsigned long int process_flags, flags;
int i;
/* FIXME: This is as yet untested code. */
/* Stop strcop from getting an operation to process while we are closing the
module. */
spin_lock_irqsave(&cryptocop_process_lock, process_flags);
/* Empty the job queue. */
for (i = 0; i < cryptocop_prio_no_prios; i++){
if (!list_empty(&(cryptocop_job_queues[i].jobs))){
list_for_each_safe(node, tmp, &(cryptocop_job_queues[i].jobs)) {
pj = list_entry(node, struct cryptocop_prio_job, node);
list_del(node);
/* Call callback to notify consumer of job removal. */
DEBUG(printk("cryptocop_job_queue_close: callback 0x%p, data 0x%p\n", pj->oper->cb, pj->oper->cb_data));
pj->oper->operation_status = -EINTR; /* Job is terminated without completion. */
pj->oper->cb(pj->oper, pj->oper->cb_data);
delete_internal_operation(pj->iop);
kfree(pj);
}
}
}
spin_unlock_irqrestore(&cryptocop_process_lock, process_flags);
/* Remove the running job, if any. */
spin_lock_irqsave(&running_job_lock, flags);
if (cryptocop_running_job){
reg_strcop_rw_cfg rw_cfg;
reg_dma_rw_cfg dma_out_cfg, dma_in_cfg;
/* Stop DMA. */
dma_out_cfg = REG_RD(dma, OUT_DMA_INST, rw_cfg);
dma_out_cfg.en = regk_dma_no;
REG_WR(dma, OUT_DMA_INST, rw_cfg, dma_out_cfg);
dma_in_cfg = REG_RD(dma, IN_DMA_INST, rw_cfg);
dma_in_cfg.en = regk_dma_no;
REG_WR(dma, IN_DMA_INST, rw_cfg, dma_in_cfg);
/* Disble the cryptocop. */
rw_cfg = REG_RD(strcop, regi_strcop, rw_cfg);
rw_cfg.en = 0;
REG_WR(strcop, regi_strcop, rw_cfg, rw_cfg);
pj = cryptocop_running_job;
cryptocop_running_job = NULL;
/* Call callback to notify consumer of job removal. */
DEBUG(printk("cryptocop_job_queue_close: callback 0x%p, data 0x%p\n", pj->oper->cb, pj->oper->cb_data));
pj->oper->operation_status = -EINTR; /* Job is terminated without completion. */
pj->oper->cb(pj->oper, pj->oper->cb_data);
delete_internal_operation(pj->iop);
kfree(pj);
}
spin_unlock_irqrestore(&running_job_lock, flags);
/* Remove completed jobs, if any. */
spin_lock_irqsave(&cryptocop_completed_jobs_lock, flags);
list_for_each_safe(node, tmp, &cryptocop_completed_jobs) {
pj = list_entry(node, struct cryptocop_prio_job, node);
list_del(node);
/* Call callback to notify consumer of job removal. */
DEBUG(printk("cryptocop_job_queue_close: callback 0x%p, data 0x%p\n", pj->oper->cb, pj->oper->cb_data));
pj->oper->operation_status = -EINTR; /* Job is terminated without completion. */
pj->oper->cb(pj->oper, pj->oper->cb_data);
delete_internal_operation(pj->iop);
kfree(pj);
}
spin_unlock_irqrestore(&cryptocop_completed_jobs_lock, flags);
}
static void cryptocop_start_job(void)
{
int i;
struct cryptocop_prio_job *pj;
unsigned long int flags;
unsigned long int running_job_flags;
reg_strcop_rw_cfg rw_cfg = {.en = 1, .ignore_sync = 0};
DEBUG(printk("cryptocop_start_job: entering\n"));
spin_lock_irqsave(&running_job_lock, running_job_flags);
if (cryptocop_running_job != NULL){
/* Already running. */
DEBUG(printk("cryptocop_start_job: already running, exit\n"));
spin_unlock_irqrestore(&running_job_lock, running_job_flags);
return;
}
spin_lock_irqsave(&cryptocop_job_queue_lock, flags);
/* Check the queues in priority order. */
for (i = cryptocop_prio_kernel_csum; (i < cryptocop_prio_no_prios) && list_empty(&cryptocop_job_queues[i].jobs); i++);
if (i == cryptocop_prio_no_prios) {
spin_unlock_irqrestore(&cryptocop_job_queue_lock, flags);
spin_unlock_irqrestore(&running_job_lock, running_job_flags);
DEBUG(printk("cryptocop_start_job: no jobs to run\n"));
return; /* No jobs to run */
}
DEBUG(printk("starting job for prio %d\n", i));
/* TODO: Do not starve lower priority jobs. Let in a lower
* prio job for every N-th processed higher prio job or some
* other scheduling policy. This could reasonably be
* tweakable since the optimal balance would depend on the
* type of load on the system. */
/* Pull the DMA lists from the job and start the DMA client. */
pj = list_entry(cryptocop_job_queues[i].jobs.next, struct cryptocop_prio_job, node);
list_del(&pj->node);
spin_unlock_irqrestore(&cryptocop_job_queue_lock, flags);
cryptocop_running_job = pj;
/* Set config register (3DES and CSUM modes). */
switch (pj->iop->tdes_mode){
case cryptocop_3des_eee:
rw_cfg.td1 = regk_strcop_e;
rw_cfg.td2 = regk_strcop_e;
rw_cfg.td3 = regk_strcop_e;
break;
case cryptocop_3des_eed:
rw_cfg.td1 = regk_strcop_e;
rw_cfg.td2 = regk_strcop_e;
rw_cfg.td3 = regk_strcop_d;
break;
case cryptocop_3des_ede:
rw_cfg.td1 = regk_strcop_e;
rw_cfg.td2 = regk_strcop_d;
rw_cfg.td3 = regk_strcop_e;
break;
case cryptocop_3des_edd:
rw_cfg.td1 = regk_strcop_e;
rw_cfg.td2 = regk_strcop_d;
rw_cfg.td3 = regk_strcop_d;
break;
case cryptocop_3des_dee:
rw_cfg.td1 = regk_strcop_d;
rw_cfg.td2 = regk_strcop_e;
rw_cfg.td3 = regk_strcop_e;
break;
case cryptocop_3des_ded:
rw_cfg.td1 = regk_strcop_d;
rw_cfg.td2 = regk_strcop_e;
rw_cfg.td3 = regk_strcop_d;
break;
case cryptocop_3des_dde:
rw_cfg.td1 = regk_strcop_d;
rw_cfg.td2 = regk_strcop_d;
rw_cfg.td3 = regk_strcop_e;
break;
case cryptocop_3des_ddd:
rw_cfg.td1 = regk_strcop_d;
rw_cfg.td2 = regk_strcop_d;
rw_cfg.td3 = regk_strcop_d;
break;
default:
DEBUG(printk("cryptocop_setup_dma_list: bad 3DES mode\n"));
}
switch (pj->iop->csum_mode){
case cryptocop_csum_le:
rw_cfg.ipend = regk_strcop_little;
break;
case cryptocop_csum_be:
rw_cfg.ipend = regk_strcop_big;
break;
default:
DEBUG(printk("cryptocop_setup_dma_list: bad checksum mode\n"));
}
REG_WR(strcop, regi_strcop, rw_cfg, rw_cfg);
DEBUG(printk("cryptocop_start_job: starting DMA, new cryptocop_running_job=0x%p\n"
"ctx_in: 0x%p, phys: 0x%p\n"
"ctx_out: 0x%p, phys: 0x%p\n",
pj,
&pj->iop->ctx_in, (char*)virt_to_phys(&pj->iop->ctx_in),
&pj->iop->ctx_out, (char*)virt_to_phys(&pj->iop->ctx_out)));
/* Start input DMA. */
flush_dma_context(&pj->iop->ctx_in);
DMA_START_CONTEXT(IN_DMA_INST, virt_to_phys(&pj->iop->ctx_in));
/* Start output DMA. */
DMA_START_CONTEXT(OUT_DMA_INST, virt_to_phys(&pj->iop->ctx_out));
spin_unlock_irqrestore(&running_job_lock, running_job_flags);
DEBUG(printk("cryptocop_start_job: exiting\n"));
}
static int cryptocop_job_setup(struct cryptocop_prio_job **pj, struct cryptocop_operation *operation)
{
int err;
int alloc_flag = operation->in_interrupt ? GFP_ATOMIC : GFP_KERNEL;
void *iop_alloc_ptr = NULL;
*pj = kmalloc(sizeof (struct cryptocop_prio_job), alloc_flag);
if (!*pj) return -ENOMEM;
DEBUG(printk("cryptocop_job_setup: operation=0x%p\n", operation));
(*pj)->oper = operation;
DEBUG(printk("cryptocop_job_setup, cb=0x%p cb_data=0x%p\n", (*pj)->oper->cb, (*pj)->oper->cb_data));
if (operation->use_dmalists) {
DEBUG(print_user_dma_lists(&operation->list_op));
if (!operation->list_op.inlist || !operation->list_op.outlist || !operation->list_op.out_data_buf || !operation->list_op.in_data_buf){
DEBUG_API(printk("cryptocop_job_setup: bad indata (use_dmalists)\n"));
kfree(*pj);
return -EINVAL;
}
iop_alloc_ptr = kmalloc(DESCR_ALLOC_PAD + sizeof(struct cryptocop_int_operation), alloc_flag);
if (!iop_alloc_ptr) {
DEBUG_API(printk("cryptocop_job_setup: kmalloc cryptocop_int_operation\n"));
kfree(*pj);
return -ENOMEM;
}
(*pj)->iop = (struct cryptocop_int_operation*)(((unsigned long int)(iop_alloc_ptr + DESCR_ALLOC_PAD + offsetof(struct cryptocop_int_operation, ctx_out)) & ~0x0000001F) - offsetof(struct cryptocop_int_operation, ctx_out));
DEBUG(memset((*pj)->iop, 0xff, sizeof(struct cryptocop_int_operation)));
(*pj)->iop->alloc_ptr = iop_alloc_ptr;
(*pj)->iop->sid = operation->sid;
(*pj)->iop->cdesc_out = NULL;
(*pj)->iop->cdesc_in = NULL;
(*pj)->iop->tdes_mode = operation->list_op.tdes_mode;
(*pj)->iop->csum_mode = operation->list_op.csum_mode;
(*pj)->iop->ddesc_out = operation->list_op.outlist;
(*pj)->iop->ddesc_in = operation->list_op.inlist;
/* Setup DMA contexts. */
(*pj)->iop->ctx_out.next = NULL;
(*pj)->iop->ctx_out.eol = 1;
(*pj)->iop->ctx_out.saved_data = operation->list_op.outlist;
(*pj)->iop->ctx_out.saved_data_buf = operation->list_op.out_data_buf;
(*pj)->iop->ctx_in.next = NULL;
(*pj)->iop->ctx_in.eol = 1;
(*pj)->iop->ctx_in.saved_data = operation->list_op.inlist;
(*pj)->iop->ctx_in.saved_data_buf = operation->list_op.in_data_buf;
} else {
if ((err = cryptocop_setup_dma_list(operation, &(*pj)->iop, alloc_flag))) {
DEBUG_API(printk("cryptocop_job_setup: cryptocop_setup_dma_list failed %d\n", err));
kfree(*pj);
return err;
}
}
DEBUG(print_dma_descriptors((*pj)->iop));
DEBUG(printk("cryptocop_job_setup, DMA list setup successful\n"));
return 0;
}
static int cryptocop_open(struct inode *inode, struct file *filp)
{
int p = iminor(inode);
if (p != CRYPTOCOP_MINOR) return -EINVAL;
filp->private_data = NULL;
return 0;
}
static int cryptocop_release(struct inode *inode, struct file *filp)
{
struct cryptocop_private *dev = filp->private_data;
struct cryptocop_private *dev_next;
while (dev){
dev_next = dev->next;
if (dev->sid != CRYPTOCOP_SESSION_ID_NONE) {
(void)cryptocop_free_session(dev->sid);
}
kfree(dev);
dev = dev_next;
}
return 0;
}
static int cryptocop_ioctl_close_session(struct inode *inode, struct file *filp,
unsigned int cmd, unsigned long arg)
{
struct cryptocop_private *dev = filp->private_data;
struct cryptocop_private *prev_dev = NULL;
struct strcop_session_op *sess_op = (struct strcop_session_op *)arg;
struct strcop_session_op sop;
int err;
DEBUG(printk("cryptocop_ioctl_close_session\n"));
if (!access_ok(VERIFY_READ, sess_op, sizeof(struct strcop_session_op)))
return -EFAULT;
err = copy_from_user(&sop, sess_op, sizeof(struct strcop_session_op));
if (err) return -EFAULT;
while (dev && (dev->sid != sop.ses_id)) {
prev_dev = dev;
dev = dev->next;
}
if (dev){
if (prev_dev){
prev_dev->next = dev->next;
} else {
filp->private_data = dev->next;
}
err = cryptocop_free_session(dev->sid);
if (err) return -EFAULT;
} else {
DEBUG_API(printk("cryptocop_ioctl_close_session: session %lld not found\n", sop.ses_id));
return -EINVAL;
}
return 0;
}
static void ioctl_process_job_callback(struct cryptocop_operation *op, void*cb_data)
{
struct ioctl_job_cb_ctx *jc = (struct ioctl_job_cb_ctx *)cb_data;
DEBUG(printk("ioctl_process_job_callback: op=0x%p, cb_data=0x%p\n", op, cb_data));
jc->processed = 1;
wake_up(&cryptocop_ioc_process_wq);
}
#define CRYPTOCOP_IOCTL_CIPHER_TID (1)
#define CRYPTOCOP_IOCTL_DIGEST_TID (2)
#define CRYPTOCOP_IOCTL_CSUM_TID (3)
static size_t first_cfg_change_ix(struct strcop_crypto_op *crp_op)
{
size_t ch_ix = 0;
if (crp_op->do_cipher) ch_ix = crp_op->cipher_start;
if (crp_op->do_digest && (crp_op->digest_start < ch_ix)) ch_ix = crp_op->digest_start;
if (crp_op->do_csum && (crp_op->csum_start < ch_ix)) ch_ix = crp_op->csum_start;
DEBUG(printk("first_cfg_change_ix: ix=%d\n", ch_ix));
return ch_ix;
}
static size_t next_cfg_change_ix(struct strcop_crypto_op *crp_op, size_t ix)
{
size_t ch_ix = INT_MAX;
size_t tmp_ix = 0;
if (crp_op->do_cipher && ((crp_op->cipher_start + crp_op->cipher_len) > ix)){
if (crp_op->cipher_start > ix) {
ch_ix = crp_op->cipher_start;
} else {
ch_ix = crp_op->cipher_start + crp_op->cipher_len;
}
}
if (crp_op->do_digest && ((crp_op->digest_start + crp_op->digest_len) > ix)){
if (crp_op->digest_start > ix) {
tmp_ix = crp_op->digest_start;
} else {
tmp_ix = crp_op->digest_start + crp_op->digest_len;
}
if (tmp_ix < ch_ix) ch_ix = tmp_ix;
}
if (crp_op->do_csum && ((crp_op->csum_start + crp_op->csum_len) > ix)){
if (crp_op->csum_start > ix) {
tmp_ix = crp_op->csum_start;
} else {
tmp_ix = crp_op->csum_start + crp_op->csum_len;
}
if (tmp_ix < ch_ix) ch_ix = tmp_ix;
}
if (ch_ix == INT_MAX) ch_ix = ix;
DEBUG(printk("next_cfg_change_ix prev ix=%d, next ix=%d\n", ix, ch_ix));
return ch_ix;
}
/* Map map_length bytes from the pages starting on *pageix and *pageoffset to iovecs starting on *iovix.
* Return -1 for ok, 0 for fail. */
static int map_pages_to_iovec(struct iovec *iov, int iovlen, int *iovix, struct page **pages, int nopages, int *pageix, int *pageoffset, int map_length )
{
int tmplen;
assert(iov != NULL);
assert(iovix != NULL);
assert(pages != NULL);
assert(pageix != NULL);
assert(pageoffset != NULL);
DEBUG(printk("map_pages_to_iovec, map_length=%d, iovlen=%d, *iovix=%d, nopages=%d, *pageix=%d, *pageoffset=%d\n", map_length, iovlen, *iovix, nopages, *pageix, *pageoffset));
while (map_length > 0){
DEBUG(printk("map_pages_to_iovec, map_length=%d, iovlen=%d, *iovix=%d, nopages=%d, *pageix=%d, *pageoffset=%d\n", map_length, iovlen, *iovix, nopages, *pageix, *pageoffset));
if (*iovix >= iovlen){
DEBUG_API(printk("map_page_to_iovec: *iovix=%d >= iovlen=%d\n", *iovix, iovlen));
return 0;
}
if (*pageix >= nopages){
DEBUG_API(printk("map_page_to_iovec: *pageix=%d >= nopages=%d\n", *pageix, nopages));
return 0;
}
iov[*iovix].iov_base = (unsigned char*)page_address(pages[*pageix]) + *pageoffset;
tmplen = PAGE_SIZE - *pageoffset;
if (tmplen < map_length){
(*pageoffset) = 0;
(*pageix)++;
} else {
tmplen = map_length;
(*pageoffset) += map_length;
}
DEBUG(printk("mapping %d bytes from page %d (or %d) to iovec %d\n", tmplen, *pageix, *pageix-1, *iovix));
iov[*iovix].iov_len = tmplen;
map_length -= tmplen;
(*iovix)++;
}
DEBUG(printk("map_page_to_iovec, exit, *iovix=%d\n", *iovix));
return -1;
}
static int cryptocop_ioctl_process(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
int i;
struct cryptocop_private *dev = filp->private_data;
struct strcop_crypto_op *crp_oper = (struct strcop_crypto_op *)arg;
struct strcop_crypto_op oper = {0};
int err = 0;
struct cryptocop_operation *cop = NULL;
struct ioctl_job_cb_ctx *jc = NULL;
struct page **inpages = NULL;
struct page **outpages = NULL;
int noinpages = 0;
int nooutpages = 0;
struct cryptocop_desc descs[5]; /* Max 5 descriptors are needed, there are three transforms that
* can get connected/disconnected on different places in the indata. */
struct cryptocop_desc_cfg dcfgs[5*3];
int desc_ix = 0;
int dcfg_ix = 0;
struct cryptocop_tfrm_cfg ciph_tcfg = {0};
struct cryptocop_tfrm_cfg digest_tcfg = {0};
struct cryptocop_tfrm_cfg csum_tcfg = {0};
unsigned char *digest_result = NULL;
int digest_length = 0;
int cblocklen = 0;
unsigned char csum_result[CSUM_BLOCK_LENGTH];
struct cryptocop_session *sess;
int iovlen = 0;
int iovix = 0;
int pageix = 0;
int pageoffset = 0;
size_t prev_ix = 0;
size_t next_ix;
int cipher_active, digest_active, csum_active;
int end_digest, end_csum;
int digest_done = 0;
int cipher_done = 0;
int csum_done = 0;
DEBUG(printk("cryptocop_ioctl_process\n"));
if (!access_ok(VERIFY_WRITE, crp_oper, sizeof(struct strcop_crypto_op))){
DEBUG_API(printk("cryptocop_ioctl_process: !access_ok crp_oper!\n"));
return -EFAULT;
}
if (copy_from_user(&oper, crp_oper, sizeof(struct strcop_crypto_op))) {
DEBUG_API(printk("cryptocop_ioctl_process: copy_from_user\n"));
return -EFAULT;
}
DEBUG(print_strcop_crypto_op(&oper));
while (dev && dev->sid != oper.ses_id) dev = dev->next;
if (!dev){
DEBUG_API(printk("cryptocop_ioctl_process: session %lld not found\n", oper.ses_id));
return -EINVAL;
}
/* Check buffers. */
if (((oper.indata + oper.inlen) < oper.indata) || ((oper.cipher_outdata + oper.cipher_outlen) < oper.cipher_outdata)){
DEBUG_API(printk("cryptocop_ioctl_process: user buffers wrapped around, bad user!\n"));
return -EINVAL;
}
if (!access_ok(VERIFY_WRITE, oper.cipher_outdata, oper.cipher_outlen)){
DEBUG_API(printk("cryptocop_ioctl_process: !access_ok out data!\n"));
return -EFAULT;
}
if (!access_ok(VERIFY_READ, oper.indata, oper.inlen)){
DEBUG_API(printk("cryptocop_ioctl_process: !access_ok in data!\n"));
return -EFAULT;
}
cop = kmalloc(sizeof(struct cryptocop_operation), GFP_KERNEL);
if (!cop) {
DEBUG_API(printk("cryptocop_ioctl_process: kmalloc\n"));
return -ENOMEM;
}
jc = kmalloc(sizeof(struct ioctl_job_cb_ctx), GFP_KERNEL);
if (!jc) {
DEBUG_API(printk("cryptocop_ioctl_process: kmalloc\n"));
err = -ENOMEM;
goto error_cleanup;
}
jc->processed = 0;
cop->cb_data = jc;
cop->cb = ioctl_process_job_callback;
cop->operation_status = 0;
cop->use_dmalists = 0;
cop->in_interrupt = 0;
cop->fast_callback = 0;
cop->tfrm_op.tfrm_cfg = NULL;
cop->tfrm_op.desc = NULL;
cop->tfrm_op.indata = NULL;
cop->tfrm_op.incount = 0;
cop->tfrm_op.inlen = 0;
cop->tfrm_op.outdata = NULL;
cop->tfrm_op.outcount = 0;
cop->tfrm_op.outlen = 0;
sess = get_session(oper.ses_id);
if (!sess){
DEBUG_API(printk("cryptocop_ioctl_process: bad session id.\n"));
kfree(cop);
kfree(jc);
return -EINVAL;
}
if (oper.do_cipher) {
unsigned int cipher_outlen = 0;
struct cryptocop_transform_ctx *tc = get_transform_ctx(sess, CRYPTOCOP_IOCTL_CIPHER_TID);
if (!tc) {
DEBUG_API(printk("cryptocop_ioctl_process: no cipher transform in session.\n"));
err = -EINVAL;
goto error_cleanup;
}
ciph_tcfg.tid = CRYPTOCOP_IOCTL_CIPHER_TID;
ciph_tcfg.inject_ix = 0;
ciph_tcfg.flags = 0;
if ((oper.cipher_start < 0) || (oper.cipher_len <= 0) || (oper.cipher_start > oper.inlen) || ((oper.cipher_start + oper.cipher_len) > oper.inlen)){
DEBUG_API(printk("cryptocop_ioctl_process: bad cipher length\n"));
kfree(cop);
kfree(jc);
return -EINVAL;
}
cblocklen = tc->init.alg == cryptocop_alg_aes ? AES_BLOCK_LENGTH : DES_BLOCK_LENGTH;
if (oper.cipher_len % cblocklen) {
kfree(cop);
kfree(jc);
DEBUG_API(printk("cryptocop_ioctl_process: cipher inlength not multiple of block length.\n"));
return -EINVAL;
}
cipher_outlen = oper.cipher_len;
if (tc->init.cipher_mode == cryptocop_cipher_mode_cbc){
if (oper.cipher_explicit) {
ciph_tcfg.flags |= CRYPTOCOP_EXPLICIT_IV;
memcpy(ciph_tcfg.iv, oper.cipher_iv, cblocklen);
} else {
cipher_outlen = oper.cipher_len - cblocklen;
}
} else {
if (oper.cipher_explicit){
kfree(cop);
kfree(jc);
DEBUG_API(printk("cryptocop_ioctl_process: explicit_iv when not CBC mode\n"));
return -EINVAL;
}
}
if (oper.cipher_outlen != cipher_outlen) {
kfree(cop);
kfree(jc);
DEBUG_API(printk("cryptocop_ioctl_process: cipher_outlen incorrect, should be %d not %d.\n", cipher_outlen, oper.cipher_outlen));
return -EINVAL;
}
if (oper.decrypt){
ciph_tcfg.flags |= CRYPTOCOP_DECRYPT;
} else {
ciph_tcfg.flags |= CRYPTOCOP_ENCRYPT;
}
ciph_tcfg.next = cop->tfrm_op.tfrm_cfg;
cop->tfrm_op.tfrm_cfg = &ciph_tcfg;
}
if (oper.do_digest){
struct cryptocop_transform_ctx *tc = get_transform_ctx(sess, CRYPTOCOP_IOCTL_DIGEST_TID);
if (!tc) {
DEBUG_API(printk("cryptocop_ioctl_process: no digest transform in session.\n"));
err = -EINVAL;
goto error_cleanup;
}
digest_length = tc->init.alg == cryptocop_alg_md5 ? 16 : 20;
digest_result = kmalloc(digest_length, GFP_KERNEL);
if (!digest_result) {
DEBUG_API(printk("cryptocop_ioctl_process: kmalloc digest_result\n"));
err = -EINVAL;
goto error_cleanup;
}
DEBUG(memset(digest_result, 0xff, digest_length));
digest_tcfg.tid = CRYPTOCOP_IOCTL_DIGEST_TID;
digest_tcfg.inject_ix = 0;
ciph_tcfg.inject_ix += digest_length;
if ((oper.digest_start < 0) || (oper.digest_len <= 0) || (oper.digest_start > oper.inlen) || ((oper.digest_start + oper.digest_len) > oper.inlen)){
DEBUG_API(printk("cryptocop_ioctl_process: bad digest length\n"));
err = -EINVAL;
goto error_cleanup;
}
digest_tcfg.next = cop->tfrm_op.tfrm_cfg;
cop->tfrm_op.tfrm_cfg = &digest_tcfg;
}
if (oper.do_csum){
csum_tcfg.tid = CRYPTOCOP_IOCTL_CSUM_TID;
csum_tcfg.inject_ix = digest_length;
ciph_tcfg.inject_ix += 2;
if ((oper.csum_start < 0) || (oper.csum_len <= 0) || (oper.csum_start > oper.inlen) || ((oper.csum_start + oper.csum_len) > oper.inlen)){
DEBUG_API(printk("cryptocop_ioctl_process: bad csum length\n"));
kfree(cop);
kfree(jc);
return -EINVAL;
}
csum_tcfg.next = cop->tfrm_op.tfrm_cfg;
cop->tfrm_op.tfrm_cfg = &csum_tcfg;
}
prev_ix = first_cfg_change_ix(&oper);
if (prev_ix > oper.inlen) {
DEBUG_API(printk("cryptocop_ioctl_process: length mismatch\n"));
nooutpages = noinpages = 0;
err = -EINVAL;
goto error_cleanup;
}
DEBUG(printk("cryptocop_ioctl_process: inlen=%d, cipher_outlen=%d\n", oper.inlen, oper.cipher_outlen));
/* Map user pages for in and out data of the operation. */
noinpages = (((unsigned long int)(oper.indata + prev_ix) & ~PAGE_MASK) + oper.inlen - 1 - prev_ix + ~PAGE_MASK) >> PAGE_SHIFT;
DEBUG(printk("cryptocop_ioctl_process: noinpages=%d\n", noinpages));
inpages = kmalloc(noinpages * sizeof(struct page*), GFP_KERNEL);
if (!inpages){
DEBUG_API(printk("cryptocop_ioctl_process: kmalloc inpages\n"));
nooutpages = noinpages = 0;
err = -ENOMEM;
goto error_cleanup;
}
if (oper.do_cipher){
nooutpages = (((unsigned long int)oper.cipher_outdata & ~PAGE_MASK) + oper.cipher_outlen - 1 + ~PAGE_MASK) >> PAGE_SHIFT;
DEBUG(printk("cryptocop_ioctl_process: nooutpages=%d\n", nooutpages));
outpages = kmalloc(nooutpages * sizeof(struct page*), GFP_KERNEL);
if (!outpages){
DEBUG_API(printk("cryptocop_ioctl_process: kmalloc outpages\n"));
nooutpages = noinpages = 0;
err = -ENOMEM;
goto error_cleanup;
}
}
/* Acquire the mm page semaphore. */
down_read(¤t->mm->mmap_sem);
err = get_user_pages(current,
current->mm,
(unsigned long int)(oper.indata + prev_ix),
noinpages,
0, /* read access only for in data */
0, /* no force */
inpages,
NULL);
if (err < 0) {
up_read(¤t->mm->mmap_sem);
nooutpages = noinpages = 0;
DEBUG_API(printk("cryptocop_ioctl_process: get_user_pages indata\n"));
goto error_cleanup;
}
noinpages = err;
if (oper.do_cipher){
err = get_user_pages(current,
current->mm,
(unsigned long int)oper.cipher_outdata,
nooutpages,
1, /* write access for out data */
0, /* no force */
outpages,
NULL);
up_read(¤t->mm->mmap_sem);
if (err < 0) {
nooutpages = 0;
DEBUG_API(printk("cryptocop_ioctl_process: get_user_pages outdata\n"));
goto error_cleanup;
}
nooutpages = err;
} else {
up_read(¤t->mm->mmap_sem);
}
/* Add 6 to nooutpages to make room for possibly inserted buffers for storing digest and
* csum output and splits when units are (dis-)connected. */
cop->tfrm_op.indata = kmalloc((noinpages) * sizeof(struct iovec), GFP_KERNEL);
cop->tfrm_op.outdata = kmalloc((6 + nooutpages) * sizeof(struct iovec), GFP_KERNEL);
if (!cop->tfrm_op.indata || !cop->tfrm_op.outdata) {
DEBUG_API(printk("cryptocop_ioctl_process: kmalloc iovecs\n"));
err = -ENOMEM;
goto error_cleanup;
}
cop->tfrm_op.inlen = oper.inlen - prev_ix;
cop->tfrm_op.outlen = 0;
if (oper.do_cipher) cop->tfrm_op.outlen += oper.cipher_outlen;
if (oper.do_digest) cop->tfrm_op.outlen += digest_length;
if (oper.do_csum) cop->tfrm_op.outlen += 2;
/* Setup the in iovecs. */
cop->tfrm_op.incount = noinpages;
if (noinpages > 1){
size_t tmplen = cop->tfrm_op.inlen;
cop->tfrm_op.indata[0].iov_len = PAGE_SIZE - ((unsigned long int)(oper.indata + prev_ix) & ~PAGE_MASK);
cop->tfrm_op.indata[0].iov_base = (unsigned char*)page_address(inpages[0]) + ((unsigned long int)(oper.indata + prev_ix) & ~PAGE_MASK);
tmplen -= cop->tfrm_op.indata[0].iov_len;
for (i = 1; i<noinpages; i++){
cop->tfrm_op.indata[i].iov_len = tmplen < PAGE_SIZE ? tmplen : PAGE_SIZE;
cop->tfrm_op.indata[i].iov_base = (unsigned char*)page_address(inpages[i]);
tmplen -= PAGE_SIZE;
}
} else {
cop->tfrm_op.indata[0].iov_len = oper.inlen - prev_ix;
cop->tfrm_op.indata[0].iov_base = (unsigned char*)page_address(inpages[0]) + ((unsigned long int)(oper.indata + prev_ix) & ~PAGE_MASK);
}
iovlen = nooutpages + 6;
pageoffset = oper.do_cipher ? ((unsigned long int)oper.cipher_outdata & ~PAGE_MASK) : 0;
next_ix = next_cfg_change_ix(&oper, prev_ix);
if (prev_ix == next_ix){
DEBUG_API(printk("cryptocop_ioctl_process: length configuration broken.\n"));
err = -EINVAL; /* This should be impossible barring bugs. */
goto error_cleanup;
}
while (prev_ix != next_ix){
end_digest = end_csum = cipher_active = digest_active = csum_active = 0;
descs[desc_ix].cfg = NULL;
descs[desc_ix].length = next_ix - prev_ix;
if (oper.do_cipher && (oper.cipher_start < next_ix) && (prev_ix < (oper.cipher_start + oper.cipher_len))) {
dcfgs[dcfg_ix].tid = CRYPTOCOP_IOCTL_CIPHER_TID;
dcfgs[dcfg_ix].src = cryptocop_source_dma;
cipher_active = 1;
if (next_ix == (oper.cipher_start + oper.cipher_len)){
cipher_done = 1;
dcfgs[dcfg_ix].last = 1;
} else {
dcfgs[dcfg_ix].last = 0;
}
dcfgs[dcfg_ix].next = descs[desc_ix].cfg;
descs[desc_ix].cfg = &dcfgs[dcfg_ix];
++dcfg_ix;
}
if (oper.do_digest && (oper.digest_start < next_ix) && (prev_ix < (oper.digest_start + oper.digest_len))) {
digest_active = 1;
dcfgs[dcfg_ix].tid = CRYPTOCOP_IOCTL_DIGEST_TID;
dcfgs[dcfg_ix].src = cryptocop_source_dma;
if (next_ix == (oper.digest_start + oper.digest_len)){
assert(!digest_done);
digest_done = 1;
dcfgs[dcfg_ix].last = 1;
} else {
dcfgs[dcfg_ix].last = 0;
}
dcfgs[dcfg_ix].next = descs[desc_ix].cfg;
descs[desc_ix].cfg = &dcfgs[dcfg_ix];
++dcfg_ix;
}
if (oper.do_csum && (oper.csum_start < next_ix) && (prev_ix < (oper.csum_start + oper.csum_len))){
csum_active = 1;
dcfgs[dcfg_ix].tid = CRYPTOCOP_IOCTL_CSUM_TID;
dcfgs[dcfg_ix].src = cryptocop_source_dma;
if (next_ix == (oper.csum_start + oper.csum_len)){
csum_done = 1;
dcfgs[dcfg_ix].last = 1;
} else {
dcfgs[dcfg_ix].last = 0;
}
dcfgs[dcfg_ix].next = descs[desc_ix].cfg;
descs[desc_ix].cfg = &dcfgs[dcfg_ix];
++dcfg_ix;
}
if (!descs[desc_ix].cfg){
DEBUG_API(printk("cryptocop_ioctl_process: data segment %d (%d to %d) had no active transforms\n", desc_ix, prev_ix, next_ix));
err = -EINVAL;
goto error_cleanup;
}
descs[desc_ix].next = &(descs[desc_ix]) + 1;
++desc_ix;
prev_ix = next_ix;
next_ix = next_cfg_change_ix(&oper, prev_ix);
}
if (desc_ix > 0){
descs[desc_ix-1].next = NULL;
} else {
descs[0].next = NULL;
}
if (oper.do_digest) {
DEBUG(printk("cryptocop_ioctl_process: mapping %d byte digest output to iovec %d\n", digest_length, iovix));
/* Add outdata iovec, length == <length of type of digest> */
cop->tfrm_op.outdata[iovix].iov_base = digest_result;
cop->tfrm_op.outdata[iovix].iov_len = digest_length;
++iovix;
}
if (oper.do_csum) {
/* Add outdata iovec, length == 2, the length of csum. */
DEBUG(printk("cryptocop_ioctl_process: mapping 2 byte csum output to iovec %d\n", iovix));
/* Add outdata iovec, length == <length of type of digest> */
cop->tfrm_op.outdata[iovix].iov_base = csum_result;
cop->tfrm_op.outdata[iovix].iov_len = 2;
++iovix;
}
if (oper.do_cipher) {
if (!map_pages_to_iovec(cop->tfrm_op.outdata, iovlen, &iovix, outpages, nooutpages, &pageix, &pageoffset, oper.cipher_outlen)){
DEBUG_API(printk("cryptocop_ioctl_process: failed to map pages to iovec.\n"));
err = -ENOSYS; /* This should be impossible barring bugs. */
goto error_cleanup;
}
}
DEBUG(printk("cryptocop_ioctl_process: setting cop->tfrm_op.outcount %d\n", iovix));
cop->tfrm_op.outcount = iovix;
assert(iovix <= (nooutpages + 6));
cop->sid = oper.ses_id;
cop->tfrm_op.desc = &descs[0];
DEBUG(printk("cryptocop_ioctl_process: inserting job, cb_data=0x%p\n", cop->cb_data));
if ((err = cryptocop_job_queue_insert_user_job(cop)) != 0) {
DEBUG_API(printk("cryptocop_ioctl_process: insert job %d\n", err));
err = -EINVAL;
goto error_cleanup;
}
DEBUG(printk("cryptocop_ioctl_process: begin wait for result\n"));
wait_event(cryptocop_ioc_process_wq, (jc->processed != 0));
DEBUG(printk("cryptocop_ioctl_process: end wait for result\n"));
if (!jc->processed){
printk(KERN_WARNING "cryptocop_ioctl_process: job not processed at completion\n");
err = -EIO;
goto error_cleanup;
}
/* Job process done. Cipher output should already be correct in job so no post processing of outdata. */
DEBUG(printk("cryptocop_ioctl_process: operation_status = %d\n", cop->operation_status));
if (cop->operation_status == 0){
if (oper.do_digest){
DEBUG(printk("cryptocop_ioctl_process: copy %d bytes digest to user\n", digest_length));
err = copy_to_user((unsigned char*)crp_oper + offsetof(struct strcop_crypto_op, digest), digest_result, digest_length);
if (0 != err){
DEBUG_API(printk("cryptocop_ioctl_process: copy_to_user, digest length %d, err %d\n", digest_length, err));
err = -EFAULT;
goto error_cleanup;
}
}
if (oper.do_csum){
DEBUG(printk("cryptocop_ioctl_process: copy 2 bytes checksum to user\n"));
err = copy_to_user((unsigned char*)crp_oper + offsetof(struct strcop_crypto_op, csum), csum_result, 2);
if (0 != err){
DEBUG_API(printk("cryptocop_ioctl_process: copy_to_user, csum, err %d\n", err));
err = -EFAULT;
goto error_cleanup;
}
}
err = 0;
} else {
DEBUG(printk("cryptocop_ioctl_process: returning err = operation_status = %d\n", cop->operation_status));
err = cop->operation_status;
}
error_cleanup:
/* Release page caches. */
for (i = 0; i < noinpages; i++){
put_page(inpages[i]);
}
for (i = 0; i < nooutpages; i++){
int spdl_err;
/* Mark output pages dirty. */
spdl_err = set_page_dirty_lock(outpages[i]);
DEBUG(if (spdl_err < 0)printk("cryptocop_ioctl_process: set_page_dirty_lock returned %d\n", spdl_err));
}
for (i = 0; i < nooutpages; i++){
put_page(outpages[i]);
}
kfree(digest_result);
kfree(inpages);
kfree(outpages);
if (cop){
kfree(cop->tfrm_op.indata);
kfree(cop->tfrm_op.outdata);
kfree(cop);
}
kfree(jc);
DEBUG(print_lock_status());
return err;
}
static int cryptocop_ioctl_create_session(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
cryptocop_session_id sid;
int err;
struct cryptocop_private *dev;
struct strcop_session_op *sess_op = (struct strcop_session_op *)arg;
struct strcop_session_op sop;
struct cryptocop_transform_init *tis = NULL;
struct cryptocop_transform_init ti_cipher = {0};
struct cryptocop_transform_init ti_digest = {0};
struct cryptocop_transform_init ti_csum = {0};
if (!access_ok(VERIFY_WRITE, sess_op, sizeof(struct strcop_session_op)))
return -EFAULT;
err = copy_from_user(&sop, sess_op, sizeof(struct strcop_session_op));
if (err) return -EFAULT;
if (sop.cipher != cryptocop_cipher_none) {
if (!access_ok(VERIFY_READ, sop.key, sop.keylen)) return -EFAULT;
}
DEBUG(printk("cryptocop_ioctl_create_session, sess_op:\n"));
DEBUG(printk("\tcipher:%d\n"
"\tcipher_mode:%d\n"
"\tdigest:%d\n"
"\tcsum:%d\n",
(int)sop.cipher,
(int)sop.cmode,
(int)sop.digest,
(int)sop.csum));
if (sop.cipher != cryptocop_cipher_none){
/* Init the cipher. */
switch (sop.cipher){
case cryptocop_cipher_des:
ti_cipher.alg = cryptocop_alg_des;
break;
case cryptocop_cipher_3des:
ti_cipher.alg = cryptocop_alg_3des;
break;
case cryptocop_cipher_aes:
ti_cipher.alg = cryptocop_alg_aes;
break;
default:
DEBUG_API(printk("create session, bad cipher algorithm %d\n", sop.cipher));
return -EINVAL;
};
DEBUG(printk("setting cipher transform %d\n", ti_cipher.alg));
copy_from_user(ti_cipher.key, sop.key, sop.keylen/8);
ti_cipher.keylen = sop.keylen;
switch (sop.cmode){
case cryptocop_cipher_mode_cbc:
case cryptocop_cipher_mode_ecb:
ti_cipher.cipher_mode = sop.cmode;
break;
default:
DEBUG_API(printk("create session, bad cipher mode %d\n", sop.cmode));
return -EINVAL;
}
DEBUG(printk("cryptocop_ioctl_create_session: setting CBC mode %d\n", ti_cipher.cipher_mode));
switch (sop.des3_mode){
case cryptocop_3des_eee:
case cryptocop_3des_eed:
case cryptocop_3des_ede:
case cryptocop_3des_edd:
case cryptocop_3des_dee:
case cryptocop_3des_ded:
case cryptocop_3des_dde:
case cryptocop_3des_ddd:
ti_cipher.tdes_mode = sop.des3_mode;
break;
default:
DEBUG_API(printk("create session, bad 3DES mode %d\n", sop.des3_mode));
return -EINVAL;
}
ti_cipher.tid = CRYPTOCOP_IOCTL_CIPHER_TID;
ti_cipher.next = tis;
tis = &ti_cipher;
} /* if (sop.cipher != cryptocop_cipher_none) */
if (sop.digest != cryptocop_digest_none){
DEBUG(printk("setting digest transform\n"));
switch (sop.digest){
case cryptocop_digest_md5:
ti_digest.alg = cryptocop_alg_md5;
break;
case cryptocop_digest_sha1:
ti_digest.alg = cryptocop_alg_sha1;
break;
default:
DEBUG_API(printk("create session, bad digest algorithm %d\n", sop.digest));
return -EINVAL;
}
ti_digest.tid = CRYPTOCOP_IOCTL_DIGEST_TID;
ti_digest.next = tis;
tis = &ti_digest;
} /* if (sop.digest != cryptocop_digest_none) */
if (sop.csum != cryptocop_csum_none){
DEBUG(printk("setting csum transform\n"));
switch (sop.csum){
case cryptocop_csum_le:
case cryptocop_csum_be:
ti_csum.csum_mode = sop.csum;
break;
default:
DEBUG_API(printk("create session, bad checksum algorithm %d\n", sop.csum));
return -EINVAL;
}
ti_csum.alg = cryptocop_alg_csum;
ti_csum.tid = CRYPTOCOP_IOCTL_CSUM_TID;
ti_csum.next = tis;
tis = &ti_csum;
} /* (sop.csum != cryptocop_csum_none) */
dev = kmalloc(sizeof(struct cryptocop_private), GFP_KERNEL);
if (!dev){
DEBUG_API(printk("create session, alloc dev\n"));
return -ENOMEM;
}
err = cryptocop_new_session(&sid, tis, GFP_KERNEL);
DEBUG({ if (err) printk("create session, cryptocop_new_session %d\n", err);});
if (err) {
kfree(dev);
return err;
}
sess_op->ses_id = sid;
dev->sid = sid;
dev->next = filp->private_data;
filp->private_data = dev;
return 0;
}
static long cryptocop_ioctl_unlocked(struct inode *inode,
struct file *filp, unsigned int cmd, unsigned long arg)
{
int err = 0;
if (_IOC_TYPE(cmd) != ETRAXCRYPTOCOP_IOCTYPE) {
DEBUG_API(printk("cryptocop_ioctl: wrong type\n"));
return -ENOTTY;
}
if (_IOC_NR(cmd) > CRYPTOCOP_IO_MAXNR){
return -ENOTTY;
}
/* Access check of the argument. Some commands, e.g. create session and process op,
needs additional checks. Those are handled in the command handling functions. */
if (_IOC_DIR(cmd) & _IOC_READ)
err = !access_ok(VERIFY_WRITE, (void *)arg, _IOC_SIZE(cmd));
else if (_IOC_DIR(cmd) & _IOC_WRITE)
err = !access_ok(VERIFY_READ, (void *)arg, _IOC_SIZE(cmd));
if (err) return -EFAULT;
switch (cmd) {
case CRYPTOCOP_IO_CREATE_SESSION:
return cryptocop_ioctl_create_session(inode, filp, cmd, arg);
case CRYPTOCOP_IO_CLOSE_SESSION:
return cryptocop_ioctl_close_session(inode, filp, cmd, arg);
case CRYPTOCOP_IO_PROCESS_OP:
return cryptocop_ioctl_process(inode, filp, cmd, arg);
default:
DEBUG_API(printk("cryptocop_ioctl: unknown command\n"));
return -ENOTTY;
}
return 0;
}
static long
cryptocop_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct inode *inode = file->f_path.dentry->d_inode;
long ret;
mutex_lock(&cryptocop_mutex);
ret = cryptocop_ioctl_unlocked(inode, filp, cmd, arg);
mutex_unlock(&cryptocop_mutex);
return ret;
}
#ifdef LDEBUG
static void print_dma_descriptors(struct cryptocop_int_operation *iop)
{
struct cryptocop_dma_desc *cdesc_out = iop->cdesc_out;
struct cryptocop_dma_desc *cdesc_in = iop->cdesc_in;
int i;
printk("print_dma_descriptors start\n");
printk("iop:\n");
printk("\tsid: 0x%lld\n", iop->sid);
printk("\tcdesc_out: 0x%p\n", iop->cdesc_out);
printk("\tcdesc_in: 0x%p\n", iop->cdesc_in);
printk("\tddesc_out: 0x%p\n", iop->ddesc_out);
printk("\tddesc_in: 0x%p\n", iop->ddesc_in);
printk("\niop->ctx_out: 0x%p phys: 0x%p\n", &iop->ctx_out, (char*)virt_to_phys(&iop->ctx_out));
printk("\tnext: 0x%p\n"
"\tsaved_data: 0x%p\n"
"\tsaved_data_buf: 0x%p\n",
iop->ctx_out.next,
iop->ctx_out.saved_data,
iop->ctx_out.saved_data_buf);
printk("\niop->ctx_in: 0x%p phys: 0x%p\n", &iop->ctx_in, (char*)virt_to_phys(&iop->ctx_in));
printk("\tnext: 0x%p\n"
"\tsaved_data: 0x%p\n"
"\tsaved_data_buf: 0x%p\n",
iop->ctx_in.next,
iop->ctx_in.saved_data,
iop->ctx_in.saved_data_buf);
i = 0;
while (cdesc_out) {
dma_descr_data *td;
printk("cdesc_out %d, desc=0x%p\n", i, cdesc_out->dma_descr);
printk("\n\tvirt_to_phys(desc): 0x%p\n", (char*)virt_to_phys(cdesc_out->dma_descr));
td = cdesc_out->dma_descr;
printk("\n\tbuf: 0x%p\n"
"\tafter: 0x%p\n"
"\tmd: 0x%04x\n"
"\tnext: 0x%p\n",
td->buf,
td->after,
td->md,
td->next);
printk("flags:\n"
"\twait:\t%d\n"
"\teol:\t%d\n"
"\touteop:\t%d\n"
"\tineop:\t%d\n"
"\tintr:\t%d\n",
td->wait,
td->eol,
td->out_eop,
td->in_eop,
td->intr);
cdesc_out = cdesc_out->next;
i++;
}
i = 0;
while (cdesc_in) {
dma_descr_data *td;
printk("cdesc_in %d, desc=0x%p\n", i, cdesc_in->dma_descr);
printk("\n\tvirt_to_phys(desc): 0x%p\n", (char*)virt_to_phys(cdesc_in->dma_descr));
td = cdesc_in->dma_descr;
printk("\n\tbuf: 0x%p\n"
"\tafter: 0x%p\n"
"\tmd: 0x%04x\n"
"\tnext: 0x%p\n",
td->buf,
td->after,
td->md,
td->next);
printk("flags:\n"
"\twait:\t%d\n"
"\teol:\t%d\n"
"\touteop:\t%d\n"
"\tineop:\t%d\n"
"\tintr:\t%d\n",
td->wait,
td->eol,
td->out_eop,
td->in_eop,
td->intr);
cdesc_in = cdesc_in->next;
i++;
}
printk("print_dma_descriptors end\n");
}
static void print_strcop_crypto_op(struct strcop_crypto_op *cop)
{
printk("print_strcop_crypto_op, 0x%p\n", cop);
/* Indata. */
printk("indata=0x%p\n"
"inlen=%d\n"
"do_cipher=%d\n"
"decrypt=%d\n"
"cipher_explicit=%d\n"
"cipher_start=%d\n"
"cipher_len=%d\n"
"outdata=0x%p\n"
"outlen=%d\n",
cop->indata,
cop->inlen,
cop->do_cipher,
cop->decrypt,
cop->cipher_explicit,
cop->cipher_start,
cop->cipher_len,
cop->cipher_outdata,
cop->cipher_outlen);
printk("do_digest=%d\n"
"digest_start=%d\n"
"digest_len=%d\n",
cop->do_digest,
cop->digest_start,
cop->digest_len);
printk("do_csum=%d\n"
"csum_start=%d\n"
"csum_len=%d\n",
cop->do_csum,
cop->csum_start,
cop->csum_len);
}
static void print_cryptocop_operation(struct cryptocop_operation *cop)
{
struct cryptocop_desc *d;
struct cryptocop_tfrm_cfg *tc;
struct cryptocop_desc_cfg *dc;
int i;
printk("print_cryptocop_operation, cop=0x%p\n\n", cop);
printk("sid: %lld\n", cop->sid);
printk("operation_status=%d\n"
"use_dmalists=%d\n"
"in_interrupt=%d\n"
"fast_callback=%d\n",
cop->operation_status,
cop->use_dmalists,
cop->in_interrupt,
cop->fast_callback);
if (cop->use_dmalists){
print_user_dma_lists(&cop->list_op);
} else {
printk("cop->tfrm_op\n"
"tfrm_cfg=0x%p\n"
"desc=0x%p\n"
"indata=0x%p\n"
"incount=%d\n"
"inlen=%d\n"
"outdata=0x%p\n"
"outcount=%d\n"
"outlen=%d\n\n",
cop->tfrm_op.tfrm_cfg,
cop->tfrm_op.desc,
cop->tfrm_op.indata,
cop->tfrm_op.incount,
cop->tfrm_op.inlen,
cop->tfrm_op.outdata,
cop->tfrm_op.outcount,
cop->tfrm_op.outlen);
tc = cop->tfrm_op.tfrm_cfg;
while (tc){
printk("tfrm_cfg, 0x%p\n"
"tid=%d\n"
"flags=%d\n"
"inject_ix=%d\n"
"next=0x%p\n",
tc,
tc->tid,
tc->flags,
tc->inject_ix,
tc->next);
tc = tc->next;
}
d = cop->tfrm_op.desc;
while (d){
printk("\n======================desc, 0x%p\n"
"length=%d\n"
"cfg=0x%p\n"
"next=0x%p\n",
d,
d->length,
d->cfg,
d->next);
dc = d->cfg;
while (dc){
printk("=========desc_cfg, 0x%p\n"
"tid=%d\n"
"src=%d\n"
"last=%d\n"
"next=0x%p\n",
dc,
dc->tid,
dc->src,
dc->last,
dc->next);
dc = dc->next;
}
d = d->next;
}
printk("\n====iniov\n");
for (i = 0; i < cop->tfrm_op.incount; i++){
printk("indata[%d]\n"
"base=0x%p\n"
"len=%d\n",
i,
cop->tfrm_op.indata[i].iov_base,
cop->tfrm_op.indata[i].iov_len);
}
printk("\n====outiov\n");
for (i = 0; i < cop->tfrm_op.outcount; i++){
printk("outdata[%d]\n"
"base=0x%p\n"
"len=%d\n",
i,
cop->tfrm_op.outdata[i].iov_base,
cop->tfrm_op.outdata[i].iov_len);
}
}
printk("------------end print_cryptocop_operation\n");
}
static void print_user_dma_lists(struct cryptocop_dma_list_operation *dma_op)
{
dma_descr_data *dd;
int i;
printk("print_user_dma_lists, dma_op=0x%p\n", dma_op);
printk("out_data_buf = 0x%p, phys_to_virt(out_data_buf) = 0x%p\n", dma_op->out_data_buf, phys_to_virt((unsigned long int)dma_op->out_data_buf));
printk("in_data_buf = 0x%p, phys_to_virt(in_data_buf) = 0x%p\n", dma_op->in_data_buf, phys_to_virt((unsigned long int)dma_op->in_data_buf));
printk("##############outlist\n");
dd = phys_to_virt((unsigned long int)dma_op->outlist);
i = 0;
while (dd != NULL) {
printk("#%d phys_to_virt(desc) 0x%p\n", i, dd);
printk("\n\tbuf: 0x%p\n"
"\tafter: 0x%p\n"
"\tmd: 0x%04x\n"
"\tnext: 0x%p\n",
dd->buf,
dd->after,
dd->md,
dd->next);
printk("flags:\n"
"\twait:\t%d\n"
"\teol:\t%d\n"
"\touteop:\t%d\n"
"\tineop:\t%d\n"
"\tintr:\t%d\n",
dd->wait,
dd->eol,
dd->out_eop,
dd->in_eop,
dd->intr);
if (dd->eol)
dd = NULL;
else
dd = phys_to_virt((unsigned long int)dd->next);
++i;
}
printk("##############inlist\n");
dd = phys_to_virt((unsigned long int)dma_op->inlist);
i = 0;
while (dd != NULL) {
printk("#%d phys_to_virt(desc) 0x%p\n", i, dd);
printk("\n\tbuf: 0x%p\n"
"\tafter: 0x%p\n"
"\tmd: 0x%04x\n"
"\tnext: 0x%p\n",
dd->buf,
dd->after,
dd->md,
dd->next);
printk("flags:\n"
"\twait:\t%d\n"
"\teol:\t%d\n"
"\touteop:\t%d\n"
"\tineop:\t%d\n"
"\tintr:\t%d\n",
dd->wait,
dd->eol,
dd->out_eop,
dd->in_eop,
dd->intr);
if (dd->eol)
dd = NULL;
else
dd = phys_to_virt((unsigned long int)dd->next);
++i;
}
}
static void print_lock_status(void)
{
printk("**********************print_lock_status\n");
printk("cryptocop_completed_jobs_lock %d\n", spin_is_locked(&cryptocop_completed_jobs_lock));
printk("cryptocop_job_queue_lock %d\n", spin_is_locked(&cryptocop_job_queue_lock));
printk("descr_pool_lock %d\n", spin_is_locked(&descr_pool_lock));
printk("cryptocop_sessions_lock %d\n", spin_is_locked(cryptocop_sessions_lock));
printk("running_job_lock %d\n", spin_is_locked(running_job_lock));
printk("cryptocop_process_lock %d\n", spin_is_locked(cryptocop_process_lock));
}
#endif /* LDEBUG */
static const char cryptocop_name[] = "ETRAX FS stream co-processor";
static int init_stream_coprocessor(void)
{
int err;
int i;
static int initialized = 0;
if (initialized)
return 0;
initialized = 1;
printk("ETRAX FS stream co-processor driver v0.01, (c) 2003 Axis Communications AB\n");
err = register_chrdev(CRYPTOCOP_MAJOR, cryptocop_name, &cryptocop_fops);
if (err < 0) {
printk(KERN_ERR "stream co-processor: could not get major number.\n");
return err;
}
err = init_cryptocop();
if (err) {
(void)unregister_chrdev(CRYPTOCOP_MAJOR, cryptocop_name);
return err;
}
err = cryptocop_job_queue_init();
if (err) {
release_cryptocop();
(void)unregister_chrdev(CRYPTOCOP_MAJOR, cryptocop_name);
return err;
}
/* Init the descriptor pool. */
for (i = 0; i < CRYPTOCOP_DESCRIPTOR_POOL_SIZE - 1; i++) {
descr_pool[i].from_pool = 1;
descr_pool[i].next = &descr_pool[i + 1];
}
descr_pool[i].from_pool = 1;
descr_pool[i].next = NULL;
descr_pool_free_list = &descr_pool[0];
descr_pool_no_free = CRYPTOCOP_DESCRIPTOR_POOL_SIZE;
spin_lock_init(&cryptocop_completed_jobs_lock);
spin_lock_init(&cryptocop_job_queue_lock);
spin_lock_init(&descr_pool_lock);
spin_lock_init(&cryptocop_sessions_lock);
spin_lock_init(&running_job_lock);
spin_lock_init(&cryptocop_process_lock);
cryptocop_sessions = NULL;
next_sid = 1;
cryptocop_running_job = NULL;
printk("stream co-processor: init done.\n");
return 0;
}
static void __exit exit_stream_coprocessor(void)
{
release_cryptocop();
cryptocop_job_queue_close();
}
module_init(init_stream_coprocessor);
module_exit(exit_stream_coprocessor);
|