Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
   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
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
/*
 *
 *  sep_crypto.c - Crypto interface structures
 *
 *  Copyright(c) 2009-2011 Intel Corporation. All rights reserved.
 *  Contributions(c) 2009-2010 Discretix. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 *  more details.
 *
 *  You should have received a copy of the GNU General Public License along with
 *  this program; if not, write to the Free Software Foundation, Inc., 59
 *  Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 *  CONTACTS:
 *
 *  Mark Allyn		mark.a.allyn@intel.com
 *  Jayant Mangalampalli jayant.mangalampalli@intel.com
 *
 *  CHANGES:
 *
 *  2009.06.26	Initial publish
 *  2010.09.14  Upgrade to Medfield
 *  2011.02.22  Enable Kernel Crypto
 *
 */

/* #define DEBUG */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/mutex.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/wait.h>
#include <linux/pci.h>
#include <linux/pm_runtime.h>
#include <linux/err.h>
#include <linux/device.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/clk.h>
#include <linux/irq.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/list.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/workqueue.h>
#include <linux/crypto.h>
#include <crypto/internal/hash.h>
#include <crypto/scatterwalk.h>
#include <crypto/sha.h>
#include <crypto/md5.h>
#include <crypto/aes.h>
#include <crypto/des.h>
#include <crypto/hash.h>
#include "sep_driver_hw_defs.h"
#include "sep_driver_config.h"
#include "sep_driver_api.h"
#include "sep_dev.h"
#include "sep_crypto.h"

#if defined(CONFIG_CRYPTO) || defined(CONFIG_CRYPTO_MODULE)

/* Globals for queuing */
static spinlock_t queue_lock;
static struct crypto_queue sep_queue;

/* Declare of dequeuer */
static void sep_dequeuer(void *data);

/* TESTING */
/**
 *	sep_do_callback
 *	@work: pointer to work_struct
 *	This is what is called by the queue; it is generic so that it
 *	can be used by any type of operation as each different callback
 *	function can use the data parameter in its own way
 */
static void sep_do_callback(struct work_struct *work)
{
	struct sep_work_struct *sep_work = container_of(work,
		struct sep_work_struct, work);
	if (sep_work != NULL) {
		(sep_work->callback)(sep_work->data);
		kfree(sep_work);
	} else {
		pr_debug("sep crypto: do callback - NULL container\n");
	}
}

/**
 *	sep_submit_work
 *	@work_queue: pointer to struct_workqueue
 *	@funct: pointer to function to execute
 *	@data: pointer to data; function will know
 *		how to use it
 *	This is a generic API to submit something to
 *	the queue. The callback function will depend
 *	on what operation is to be done
 */
static int sep_submit_work(struct workqueue_struct *work_queue,
	void(*funct)(void *),
	void *data)
{
	struct sep_work_struct *sep_work;
	int result;

	sep_work = kmalloc(sizeof(struct sep_work_struct), GFP_ATOMIC);

	if (sep_work == NULL) {
		pr_debug("sep crypto: cant allocate work structure\n");
		return -ENOMEM;
	}

	sep_work->callback = funct;
	sep_work->data = data;
	INIT_WORK(&sep_work->work, sep_do_callback);
	result = queue_work(work_queue, &sep_work->work);
	if (!result) {
		pr_debug("sep_crypto: queue_work failed\n");
		return -EINVAL;
	}
	return 0;
}

/**
 *	sep_alloc_sg_buf -
 *	@sep: pointer to struct sep_device
 *	@size: total size of area
 *	@block_size: minimum size of chunks
 *	each page is minimum or modulo this size
 *	@returns: pointer to struct scatterlist for new
 *	buffer
 **/
static struct scatterlist *sep_alloc_sg_buf(
	struct sep_device *sep,
	size_t size,
	size_t block_size)
{
	u32 nbr_pages;
	u32 ct1;
	void *buf;
	size_t current_size;
	size_t real_page_size;

	struct scatterlist *sg, *sg_temp;

	if (size == 0)
		return NULL;

	dev_dbg(&sep->pdev->dev, "sep alloc sg buf\n");

	current_size = 0;
	nbr_pages = 0;
	real_page_size = PAGE_SIZE - (PAGE_SIZE % block_size);
	/**
	 * The size of each page must be modulo of the operation
	 * block size; increment by the modified page size until
	 * the total size is reached, then you have the number of
	 * pages
	 */
	while (current_size < size) {
		current_size += real_page_size;
		nbr_pages += 1;
	}

	sg = kmalloc_array(nbr_pages, sizeof(struct scatterlist), GFP_ATOMIC);
	if (!sg)
		return NULL;

	sg_init_table(sg, nbr_pages);

	current_size = 0;
	sg_temp = sg;
	for (ct1 = 0; ct1 < nbr_pages; ct1 += 1) {
		buf = (void *)get_zeroed_page(GFP_ATOMIC);
		if (!buf) {
			dev_warn(&sep->pdev->dev,
				"Cannot allocate page for new buffer\n");
			kfree(sg);
			return NULL;
		}

		sg_set_buf(sg_temp, buf, real_page_size);
		if ((size - current_size) > real_page_size) {
			sg_temp->length = real_page_size;
			current_size += real_page_size;
		} else {
			sg_temp->length = (size - current_size);
			current_size = size;
		}
		sg_temp = sg_next(sg);
	}
	return sg;
}

/**
 *	sep_free_sg_buf -
 *	@sg: pointer to struct scatterlist; points to area to free
 */
static void sep_free_sg_buf(struct scatterlist *sg)
{
	struct scatterlist *sg_temp = sg;
		while (sg_temp) {
			free_page((unsigned long)sg_virt(sg_temp));
			sg_temp = sg_next(sg_temp);
		}
		kfree(sg);
}

/**
 *	sep_copy_sg -
 *	@sep: pointer to struct sep_device
 *	@sg_src: pointer to struct scatterlist for source
 *	@sg_dst: pointer to struct scatterlist for destination
 *      @size: size (in bytes) of data to copy
 *
 *	Copy data from one scatterlist to another; both must
 *	be the same size
 */
static void sep_copy_sg(
	struct sep_device *sep,
	struct scatterlist *sg_src,
	struct scatterlist *sg_dst,
	size_t size)
{
	u32 seg_size;
	u32 in_offset, out_offset;

	u32 count = 0;
	struct scatterlist *sg_src_tmp = sg_src;
	struct scatterlist *sg_dst_tmp = sg_dst;
	in_offset = 0;
	out_offset = 0;

	dev_dbg(&sep->pdev->dev, "sep copy sg\n");

	if ((sg_src == NULL) || (sg_dst == NULL) || (size == 0))
		return;

	dev_dbg(&sep->pdev->dev, "sep copy sg not null\n");

	while (count < size) {
		if ((sg_src_tmp->length - in_offset) >
			(sg_dst_tmp->length - out_offset))
			seg_size = sg_dst_tmp->length - out_offset;
		else
			seg_size = sg_src_tmp->length - in_offset;

		if (seg_size > (size - count))
			seg_size = (size = count);

		memcpy(sg_virt(sg_dst_tmp) + out_offset,
			sg_virt(sg_src_tmp) + in_offset,
			seg_size);

		in_offset += seg_size;
		out_offset += seg_size;
		count += seg_size;

		if (in_offset >= sg_src_tmp->length) {
			sg_src_tmp = sg_next(sg_src_tmp);
			in_offset = 0;
		}

		if (out_offset >= sg_dst_tmp->length) {
			sg_dst_tmp = sg_next(sg_dst_tmp);
			out_offset = 0;
		}
	}
}

/**
 *	sep_oddball_pages -
 *	@sep: pointer to struct sep_device
 *	@sg: pointer to struct scatterlist - buffer to check
 *	@size: total data size
 *	@blocksize: minimum block size; must be multiples of this size
 *	@to_copy: 1 means do copy, 0 means do not copy
 *	@new_sg: pointer to location to put pointer to new sg area
 *	@returns: 1 if new scatterlist is needed; 0 if not needed;
 *		error value if operation failed
 *
 *	The SEP device requires all pages to be multiples of the
 *	minimum block size appropriate for the operation
 *	This function check all pages; if any are oddball sizes
 *	(not multiple of block sizes), it creates a new scatterlist.
 *	If the to_copy parameter is set to 1, then a scatter list
 *	copy is performed. The pointer to the new scatterlist is
 *	put into the address supplied by the new_sg parameter; if
 *	no new scatterlist is needed, then a NULL is put into
 *	the location at new_sg.
 *
 */
static int sep_oddball_pages(
	struct sep_device *sep,
	struct scatterlist *sg,
	size_t data_size,
	u32 block_size,
	struct scatterlist **new_sg,
	u32 do_copy)
{
	struct scatterlist *sg_temp;
	u32 flag;
	u32 nbr_pages, page_count;

	dev_dbg(&sep->pdev->dev, "sep oddball\n");
	if ((sg == NULL) || (data_size == 0) || (data_size < block_size))
		return 0;

	dev_dbg(&sep->pdev->dev, "sep oddball not null\n");
	flag = 0;
	nbr_pages = 0;
	page_count = 0;
	sg_temp = sg;

	while (sg_temp) {
		nbr_pages += 1;
		sg_temp = sg_next(sg_temp);
	}

	sg_temp = sg;
	while ((sg_temp) && (flag == 0)) {
		page_count += 1;
		if (sg_temp->length % block_size)
			flag = 1;
		else
			sg_temp = sg_next(sg_temp);
	}

	/* Do not process if last (or only) page is oddball */
	if (nbr_pages == page_count)
		flag = 0;

	if (flag) {
		dev_dbg(&sep->pdev->dev, "sep oddball processing\n");
		*new_sg = sep_alloc_sg_buf(sep, data_size, block_size);
		if (*new_sg == NULL) {
			dev_warn(&sep->pdev->dev, "cannot allocate new sg\n");
			return -ENOMEM;
		}

		if (do_copy)
			sep_copy_sg(sep, sg, *new_sg, data_size);

		return 1;
	} else {
		return 0;
	}
}

/**
 *	sep_copy_offset_sg -
 *	@sep: pointer to struct sep_device;
 *	@sg: pointer to struct scatterlist
 *	@offset: offset into scatterlist memory
 *	@dst: place to put data
 *	@len: length of data
 *	@returns: number of bytes copies
 *
 *	This copies data from scatterlist buffer
 *	offset from beginning - it is needed for
 *	handling tail data in hash
 */
static size_t sep_copy_offset_sg(
	struct sep_device *sep,
	struct scatterlist *sg,
	u32 offset,
	void *dst,
	u32 len)
{
	size_t page_start;
	size_t page_end;
	size_t offset_within_page;
	size_t length_within_page;
	size_t length_remaining;
	size_t current_offset;

	/* Find which page is beginning of segment */
	page_start = 0;
	page_end = sg->length;
	while ((sg) && (offset > page_end)) {
		page_start += sg->length;
		sg = sg_next(sg);
		if (sg)
			page_end += sg->length;
	}

	if (sg == NULL)
		return -ENOMEM;

	offset_within_page = offset - page_start;
	if ((sg->length - offset_within_page) >= len) {
		/* All within this page */
		memcpy(dst, sg_virt(sg) + offset_within_page, len);
		return len;
	} else {
		/* Scattered multiple pages */
		current_offset = 0;
		length_remaining = len;
		while ((sg) && (current_offset < len)) {
			length_within_page = sg->length - offset_within_page;
			if (length_within_page >= length_remaining) {
				memcpy(dst+current_offset,
					sg_virt(sg) + offset_within_page,
					length_remaining);
				length_remaining = 0;
				current_offset = len;
			} else {
				memcpy(dst+current_offset,
					sg_virt(sg) + offset_within_page,
					length_within_page);
				length_remaining -= length_within_page;
				current_offset += length_within_page;
				offset_within_page = 0;
				sg = sg_next(sg);
			}
		}

		if (sg == NULL)
			return -ENOMEM;
	}
	return len;
}

/**
 *	partial_overlap -
 *	@src_ptr: source pointer
 *	@dst_ptr: destination pointer
 *	@nbytes: number of bytes
 *	@returns: 0 for success; -1 for failure
 *	We cannot have any partial overlap. Total overlap
 *	where src is the same as dst is okay
 */
static int partial_overlap(void *src_ptr, void *dst_ptr, u32 nbytes)
{
	/* Check for partial overlap */
	if (src_ptr != dst_ptr) {
		if (src_ptr < dst_ptr) {
			if ((src_ptr + nbytes) > dst_ptr)
				return -EINVAL;
		} else {
			if ((dst_ptr + nbytes) > src_ptr)
				return -EINVAL;
		}
	}

	return 0;
}

/* Debug - prints only if DEBUG is defined */
static void sep_dump_ivs(struct ablkcipher_request *req, char *reason)

	{
	unsigned char *cptr;
	struct sep_aes_internal_context *aes_internal;
	struct sep_des_internal_context *des_internal;
	int ct1;

	struct this_task_ctx *ta_ctx;
	struct crypto_ablkcipher *tfm;
	struct sep_system_ctx *sctx;

	ta_ctx = ablkcipher_request_ctx(req);
	tfm = crypto_ablkcipher_reqtfm(req);
	sctx = crypto_ablkcipher_ctx(tfm);

	dev_dbg(&ta_ctx->sep_used->pdev->dev, "IV DUMP - %s\n", reason);
	if ((ta_ctx->current_request == DES_CBC) &&
		(ta_ctx->des_opmode == SEP_DES_CBC)) {

		des_internal = (struct sep_des_internal_context *)
			sctx->des_private_ctx.ctx_buf;
		/* print vendor */
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"sep - vendor iv for DES\n");
		cptr = (unsigned char *)des_internal->iv_context;
		for (ct1 = 0; ct1 < crypto_ablkcipher_ivsize(tfm); ct1 += 1)
			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"%02x\n", *(cptr + ct1));

		/* print walk */
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"sep - walk from kernel crypto iv for DES\n");
		cptr = (unsigned char *)ta_ctx->walk.iv;
		for (ct1 = 0; ct1 < crypto_ablkcipher_ivsize(tfm); ct1 += 1)
			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"%02x\n", *(cptr + ct1));
	} else if ((ta_ctx->current_request == AES_CBC) &&
		(ta_ctx->aes_opmode == SEP_AES_CBC)) {

		aes_internal = (struct sep_aes_internal_context *)
			sctx->aes_private_ctx.cbuff;
		/* print vendor */
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"sep - vendor iv for AES\n");
		cptr = (unsigned char *)aes_internal->aes_ctx_iv;
		for (ct1 = 0; ct1 < crypto_ablkcipher_ivsize(tfm); ct1 += 1)
			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"%02x\n", *(cptr + ct1));

		/* print walk */
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"sep - walk from kernel crypto iv for AES\n");
		cptr = (unsigned char *)ta_ctx->walk.iv;
		for (ct1 = 0; ct1 < crypto_ablkcipher_ivsize(tfm); ct1 += 1)
			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"%02x\n", *(cptr + ct1));
	}
}

/**
 * RFC2451: Weak key check
 * Returns: 1 (weak), 0 (not weak)
 */
static int sep_weak_key(const u8 *key, unsigned int keylen)
{
	static const u8 parity[] = {
	8, 1, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 2, 8,
	0, 8, 8, 0, 8, 0, 0, 8, 8,
	0, 0, 8, 0, 8, 8, 3,
	0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
	8, 0, 0, 8, 0, 8, 8, 0, 0,
	8, 8, 0, 8, 0, 0, 8,
	0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
	8, 0, 0, 8, 0, 8, 8, 0, 0,
	8, 8, 0, 8, 0, 0, 8,
	8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
	0, 8, 8, 0, 8, 0, 0, 8, 8,
	0, 0, 8, 0, 8, 8, 0,
	0, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
	8, 0, 0, 8, 0, 8, 8, 0, 0,
	8, 8, 0, 8, 0, 0, 8,
	8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
	0, 8, 8, 0, 8, 0, 0, 8, 8,
	0, 0, 8, 0, 8, 8, 0,
	8, 0, 0, 8, 0, 8, 8, 0, 0, 8, 8, 0, 8, 0, 0, 8,
	0, 8, 8, 0, 8, 0, 0, 8, 8,
	0, 0, 8, 0, 8, 8, 0,
	4, 8, 8, 0, 8, 0, 0, 8, 8, 0, 0, 8, 0, 8, 8, 0,
	8, 5, 0, 8, 0, 8, 8, 0, 0,
	8, 8, 0, 8, 0, 6, 8,
	};

	u32 n, w;

	n  = parity[key[0]]; n <<= 4;
	n |= parity[key[1]]; n <<= 4;
	n |= parity[key[2]]; n <<= 4;
	n |= parity[key[3]]; n <<= 4;
	n |= parity[key[4]]; n <<= 4;
	n |= parity[key[5]]; n <<= 4;
	n |= parity[key[6]]; n <<= 4;
	n |= parity[key[7]];
	w = 0x88888888L;

	/* 1 in 10^10 keys passes this test */
	if (!((n - (w >> 3)) & w)) {
		if (n < 0x41415151) {
			if (n < 0x31312121) {
				if (n < 0x14141515) {
					/* 01 01 01 01 01 01 01 01 */
					if (n == 0x11111111)
						goto weak;
					/* 01 1F 01 1F 01 0E 01 0E */
					if (n == 0x13131212)
						goto weak;
				} else {
					/* 01 E0 01 E0 01 F1 01 F1 */
					if (n == 0x14141515)
						goto weak;
					/* 01 FE 01 FE 01 FE 01 FE */
					if (n == 0x16161616)
						goto weak;
				}
			} else {
				if (n < 0x34342525) {
					/* 1F 01 1F 01 0E 01 0E 01 */
					if (n == 0x31312121)
						goto weak;
					/* 1F 1F 1F 1F 0E 0E 0E 0E (?) */
					if (n == 0x33332222)
						goto weak;
				} else {
					/* 1F E0 1F E0 0E F1 0E F1 */
					if (n == 0x34342525)
						goto weak;
					/* 1F FE 1F FE 0E FE 0E FE */
					if (n == 0x36362626)
						goto weak;
				}
			}
		} else {
			if (n < 0x61616161) {
				if (n < 0x44445555) {
					/* E0 01 E0 01 F1 01 F1 01 */
					if (n == 0x41415151)
						goto weak;
					/* E0 1F E0 1F F1 0E F1 0E */
					if (n == 0x43435252)
						goto weak;
				} else {
					/* E0 E0 E0 E0 F1 F1 F1 F1 (?) */
					if (n == 0x44445555)
						goto weak;
					/* E0 FE E0 FE F1 FE F1 FE */
					if (n == 0x46465656)
						goto weak;
				}
			} else {
				if (n < 0x64646565) {
					/* FE 01 FE 01 FE 01 FE 01 */
					if (n == 0x61616161)
						goto weak;
					/* FE 1F FE 1F FE 0E FE 0E */
					if (n == 0x63636262)
						goto weak;
				} else {
					/* FE E0 FE E0 FE F1 FE F1 */
					if (n == 0x64646565)
						goto weak;
					/* FE FE FE FE FE FE FE FE */
					if (n == 0x66666666)
						goto weak;
				}
			}
		}
	}
	return 0;
weak:
	return 1;
}
/**
 *	sep_sg_nents
 */
static u32 sep_sg_nents(struct scatterlist *sg)
{
	u32 ct1 = 0;
	while (sg) {
		ct1 += 1;
		sg = sg_next(sg);
	}

	return ct1;
}

/**
 *	sep_start_msg -
 *	@ta_ctx: pointer to struct this_task_ctx
 *	@returns: offset to place for the next word in the message
 *	Set up pointer in message pool for new message
 */
static u32 sep_start_msg(struct this_task_ctx *ta_ctx)
{
	u32 *word_ptr;
	ta_ctx->msg_len_words = 2;
	ta_ctx->msgptr = ta_ctx->msg;
	memset(ta_ctx->msg, 0, SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES);
	ta_ctx->msgptr += sizeof(u32) * 2;
	word_ptr = (u32 *)ta_ctx->msgptr;
	*word_ptr = SEP_START_MSG_TOKEN;
	return sizeof(u32) * 2;
}

/**
 *	sep_end_msg -
 *	@ta_ctx: pointer to struct this_task_ctx
 *	@messages_offset: current message offset
 *	Returns: 0 for success; <0 otherwise
 *	End message; set length and CRC; and
 *	send interrupt to the SEP
 */
static void sep_end_msg(struct this_task_ctx *ta_ctx, u32 msg_offset)
{
	u32 *word_ptr;
	/* Msg size goes into msg after token */
	ta_ctx->msg_len_words = msg_offset / sizeof(u32) + 1;
	word_ptr = (u32 *)ta_ctx->msgptr;
	word_ptr += 1;
	*word_ptr = ta_ctx->msg_len_words;

	/* CRC (currently 0) goes at end of msg */
	word_ptr = (u32 *)(ta_ctx->msgptr + msg_offset);
	*word_ptr = 0;
}

/**
 *	sep_start_inbound_msg -
 *	@ta_ctx: pointer to struct this_task_ctx
 *	@msg_offset: offset to place for the next word in the message
 *	@returns: 0 for success; error value for failure
 *	Set up pointer in message pool for inbound message
 */
static u32 sep_start_inbound_msg(struct this_task_ctx *ta_ctx, u32 *msg_offset)
{
	u32 *word_ptr;
	u32 token;
	u32 error = SEP_OK;

	*msg_offset = sizeof(u32) * 2;
	word_ptr = (u32 *)ta_ctx->msgptr;
	token = *word_ptr;
	ta_ctx->msg_len_words = *(word_ptr + 1);

	if (token != SEP_START_MSG_TOKEN) {
		error = SEP_INVALID_START;
		goto end_function;
	}

end_function:

	return error;
}

/**
 *	sep_write_msg -
 *	@ta_ctx: pointer to struct this_task_ctx
 *	@in_addr: pointer to start of parameter
 *	@size: size of parameter to copy (in bytes)
 *	@max_size: size to move up offset; SEP mesg is in word sizes
 *	@msg_offset: pointer to current offset (is updated)
 *	@byte_array: flag ti indicate whether endian must be changed
 *	Copies data into the message area from caller
 */
static void sep_write_msg(struct this_task_ctx *ta_ctx, void *in_addr,
	u32 size, u32 max_size, u32 *msg_offset, u32 byte_array)
{
	u32 *word_ptr;
	void *void_ptr;
	void_ptr = ta_ctx->msgptr + *msg_offset;
	word_ptr = (u32 *)void_ptr;
	memcpy(void_ptr, in_addr, size);
	*msg_offset += max_size;

	/* Do we need to manipulate endian? */
	if (byte_array) {
		u32 i;
		for (i = 0; i < ((size + 3) / 4); i += 1)
			*(word_ptr + i) = CHG_ENDIAN(*(word_ptr + i));
	}
}

/**
 *	sep_make_header
 *	@ta_ctx: pointer to struct this_task_ctx
 *	@msg_offset: pointer to current offset (is updated)
 *	@op_code: op code to put into message
 *	Puts op code into message and updates offset
 */
static void sep_make_header(struct this_task_ctx *ta_ctx, u32 *msg_offset,
			    u32 op_code)
{
	u32 *word_ptr;

	*msg_offset = sep_start_msg(ta_ctx);
	word_ptr = (u32 *)(ta_ctx->msgptr + *msg_offset);
	*word_ptr = op_code;
	*msg_offset += sizeof(u32);
}



/**
 *	sep_read_msg -
 *	@ta_ctx: pointer to struct this_task_ctx
 *	@in_addr: pointer to start of parameter
 *	@size: size of parameter to copy (in bytes)
 *	@max_size: size to move up offset; SEP mesg is in word sizes
 *	@msg_offset: pointer to current offset (is updated)
 *	@byte_array: flag ti indicate whether endian must be changed
 *	Copies data out of the message area to caller
 */
static void sep_read_msg(struct this_task_ctx *ta_ctx, void *in_addr,
	u32 size, u32 max_size, u32 *msg_offset, u32 byte_array)
{
	u32 *word_ptr;
	void *void_ptr;
	void_ptr = ta_ctx->msgptr + *msg_offset;
	word_ptr = (u32 *)void_ptr;

	/* Do we need to manipulate endian? */
	if (byte_array) {
		u32 i;
		for (i = 0; i < ((size + 3) / 4); i += 1)
			*(word_ptr + i) = CHG_ENDIAN(*(word_ptr + i));
	}

	memcpy(in_addr, void_ptr, size);
	*msg_offset += max_size;
}

/**
 *	sep_verify_op -
 *	@ta_ctx: pointer to struct this_task_ctx
 *	@op_code: expected op_code
 *      @msg_offset: pointer to current offset (is updated)
 *	@returns: 0 for success; error for failure
 */
static u32 sep_verify_op(struct this_task_ctx *ta_ctx, u32 op_code,
			 u32 *msg_offset)
{
	u32 error;
	u32 in_ary[2];

	struct sep_device *sep = ta_ctx->sep_used;

	dev_dbg(&sep->pdev->dev, "dumping return message\n");
	error = sep_start_inbound_msg(ta_ctx, msg_offset);
	if (error) {
		dev_warn(&sep->pdev->dev,
			"sep_start_inbound_msg error\n");
		return error;
	}

	sep_read_msg(ta_ctx, in_ary, sizeof(u32) * 2, sizeof(u32) * 2,
		msg_offset, 0);

	if (in_ary[0] != op_code) {
		dev_warn(&sep->pdev->dev,
			"sep got back wrong opcode\n");
		dev_warn(&sep->pdev->dev,
			"got back %x; expected %x\n",
			in_ary[0], op_code);
		return SEP_WRONG_OPCODE;
	}

	if (in_ary[1] != SEP_OK) {
		dev_warn(&sep->pdev->dev,
			"sep execution error\n");
		dev_warn(&sep->pdev->dev,
			"got back %x; expected %x\n",
			in_ary[1], SEP_OK);
		return in_ary[0];
	}

return 0;
}

/**
 * sep_read_context -
 * @ta_ctx: pointer to struct this_task_ctx
 * @msg_offset: point to current place in SEP msg; is updated
 * @dst: pointer to place to put the context
 * @len: size of the context structure (differs for crypro/hash)
 * This function reads the context from the msg area
 * There is a special way the vendor needs to have the maximum
 * length calculated so that the msg_offset is updated properly;
 * it skips over some words in the msg area depending on the size
 * of the context
 */
static void sep_read_context(struct this_task_ctx *ta_ctx, u32 *msg_offset,
	void *dst, u32 len)
{
	u32 max_length = ((len + 3) / sizeof(u32)) * sizeof(u32);
	sep_read_msg(ta_ctx, dst, len, max_length, msg_offset, 0);
}

/**
 * sep_write_context -
 * @ta_ctx: pointer to struct this_task_ctx
 * @msg_offset: point to current place in SEP msg; is updated
 * @src: pointer to the current context
 * @len: size of the context structure (differs for crypro/hash)
 * This function writes the context to the msg area
 * There is a special way the vendor needs to have the maximum
 * length calculated so that the msg_offset is updated properly;
 * it skips over some words in the msg area depending on the size
 * of the context
 */
static void sep_write_context(struct this_task_ctx *ta_ctx, u32 *msg_offset,
	void *src, u32 len)
{
	u32 max_length = ((len + 3) / sizeof(u32)) * sizeof(u32);
	sep_write_msg(ta_ctx, src, len, max_length, msg_offset, 0);
}

/**
 * sep_clear_out -
 * @ta_ctx: pointer to struct this_task_ctx
 * Clear out crypto related values in sep device structure
 * to enable device to be used by anyone; either kernel
 * crypto or userspace app via middleware
 */
static void sep_clear_out(struct this_task_ctx *ta_ctx)
{
	if (ta_ctx->src_sg_hold) {
		sep_free_sg_buf(ta_ctx->src_sg_hold);
		ta_ctx->src_sg_hold = NULL;
	}

	if (ta_ctx->dst_sg_hold) {
		sep_free_sg_buf(ta_ctx->dst_sg_hold);
		ta_ctx->dst_sg_hold = NULL;
	}

	ta_ctx->src_sg = NULL;
	ta_ctx->dst_sg = NULL;

	sep_free_dma_table_data_handler(ta_ctx->sep_used, &ta_ctx->dma_ctx);

	if (ta_ctx->i_own_sep) {
		/**
		 * The following unlocks the sep and makes it available
		 * to any other application
		 * First, null out crypto entries in sep before releasing it
		 */
		ta_ctx->sep_used->current_hash_req = NULL;
		ta_ctx->sep_used->current_cypher_req = NULL;
		ta_ctx->sep_used->current_request = 0;
		ta_ctx->sep_used->current_hash_stage = 0;
		ta_ctx->sep_used->ta_ctx = NULL;
		ta_ctx->sep_used->in_kernel = 0;

		ta_ctx->call_status.status = 0;

		/* Remove anything confidential */
		memset(ta_ctx->sep_used->shared_addr, 0,
			SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES);

		sep_queue_status_remove(ta_ctx->sep_used, &ta_ctx->queue_elem);

#ifdef SEP_ENABLE_RUNTIME_PM
		ta_ctx->sep_used->in_use = 0;
		pm_runtime_mark_last_busy(&ta_ctx->sep_used->pdev->dev);
		pm_runtime_put_autosuspend(&ta_ctx->sep_used->pdev->dev);
#endif

		clear_bit(SEP_WORKING_LOCK_BIT,
			&ta_ctx->sep_used->in_use_flags);
		ta_ctx->sep_used->pid_doing_transaction = 0;

		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"[PID%d] waking up next transaction\n",
			current->pid);

		clear_bit(SEP_TRANSACTION_STARTED_LOCK_BIT,
			&ta_ctx->sep_used->in_use_flags);
		wake_up(&ta_ctx->sep_used->event_transactions);

		ta_ctx->i_own_sep = 0;
	}
}

/**
  * Release crypto infrastructure from EINPROGRESS and
  * clear sep_dev so that SEP is available to anyone
  */
static void sep_crypto_release(struct sep_system_ctx *sctx,
	struct this_task_ctx *ta_ctx, u32 error)
{
	struct ahash_request *hash_req = ta_ctx->current_hash_req;
	struct ablkcipher_request *cypher_req =
		ta_ctx->current_cypher_req;
	struct sep_device *sep = ta_ctx->sep_used;

	sep_clear_out(ta_ctx);

	/**
	 * This may not yet exist depending when we
	 * chose to bail out. If it does exist, set
	 * it to 1
	 */
	if (ta_ctx->are_we_done_yet != NULL)
		*ta_ctx->are_we_done_yet = 1;

	if (cypher_req != NULL) {
		if ((sctx->key_sent == 1) ||
			((error != 0) && (error != -EINPROGRESS))) {
			if (cypher_req->base.complete == NULL) {
				dev_dbg(&sep->pdev->dev,
					"release is null for cypher!");
			} else {
				cypher_req->base.complete(
					&cypher_req->base, error);
			}
		}
	}

	if (hash_req != NULL) {
		if (hash_req->base.complete == NULL) {
			dev_dbg(&sep->pdev->dev,
				"release is null for hash!");
		} else {
			hash_req->base.complete(
				&hash_req->base, error);
		}
	}
}

/**
 *	This is where we grab the sep itself and tell it to do something.
 *	It will sleep if the sep is currently busy
 *	and it will return 0 if sep is now ours; error value if there
 *	were problems
 */
static int sep_crypto_take_sep(struct this_task_ctx *ta_ctx)
{
	struct sep_device *sep = ta_ctx->sep_used;
	int result;
	struct sep_msgarea_hdr *my_msg_header;

	my_msg_header = (struct sep_msgarea_hdr *)ta_ctx->msg;

	/* add to status queue */
	ta_ctx->queue_elem = sep_queue_status_add(sep, my_msg_header->opcode,
		ta_ctx->nbytes, current->pid,
		current->comm, sizeof(current->comm));

	if (!ta_ctx->queue_elem) {
		dev_dbg(&sep->pdev->dev,
			"[PID%d] updating queue status error\n", current->pid);
		return -EINVAL;
	}

	/* get the device; this can sleep */
	result = sep_wait_transaction(sep);
	if (result)
		return result;

	if (sep_dev->power_save_setup == 1)
		pm_runtime_get_sync(&sep_dev->pdev->dev);

	/* Copy in the message */
	memcpy(sep->shared_addr, ta_ctx->msg,
		SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES);

	/* Copy in the dcb information if there is any */
	if (ta_ctx->dcb_region) {
		result = sep_activate_dcb_dmatables_context(sep,
			&ta_ctx->dcb_region, &ta_ctx->dmatables_region,
			ta_ctx->dma_ctx);
		if (result)
			return result;
	}

	/* Mark the device so we know how to finish the job in the tasklet */
	if (ta_ctx->current_hash_req)
		sep->current_hash_req = ta_ctx->current_hash_req;
	else
		sep->current_cypher_req = ta_ctx->current_cypher_req;

	sep->current_request = ta_ctx->current_request;
	sep->current_hash_stage = ta_ctx->current_hash_stage;
	sep->ta_ctx = ta_ctx;
	sep->in_kernel = 1;
	ta_ctx->i_own_sep = 1;

	/* need to set bit first to avoid race condition with interrupt */
	set_bit(SEP_LEGACY_SENDMSG_DONE_OFFSET, &ta_ctx->call_status.status);

	result = sep_send_command_handler(sep);

	dev_dbg(&sep->pdev->dev, "[PID%d]: sending command to the sep\n",
		current->pid);

	if (!result)
		dev_dbg(&sep->pdev->dev, "[PID%d]: command sent okay\n",
			current->pid);
	else {
		dev_dbg(&sep->pdev->dev, "[PID%d]: cant send command\n",
			current->pid);
		clear_bit(SEP_LEGACY_SENDMSG_DONE_OFFSET,
			&ta_ctx->call_status.status);
	}

	return result;
}

/**
 * This function sets things up for a crypto data block process
 * This does all preparation, but does not try to grab the
 * sep
 * @req: pointer to struct ablkcipher_request
 * returns: 0 if all went well, non zero if error
 */
static int sep_crypto_block_data(struct ablkcipher_request *req)
{

	int int_error;
	u32 msg_offset;
	static u32 msg[10];
	void *src_ptr;
	void *dst_ptr;

	static char small_buf[100];
	ssize_t copy_result;
	int result;

	struct scatterlist *new_sg;
	struct this_task_ctx *ta_ctx;
	struct crypto_ablkcipher *tfm;
	struct sep_system_ctx *sctx;

	struct sep_des_internal_context *des_internal;
	struct sep_aes_internal_context *aes_internal;

	ta_ctx = ablkcipher_request_ctx(req);
	tfm = crypto_ablkcipher_reqtfm(req);
	sctx = crypto_ablkcipher_ctx(tfm);

	/* start the walk on scatterlists */
	ablkcipher_walk_init(&ta_ctx->walk, req->src, req->dst, req->nbytes);
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "sep crypto block data size of %x\n",
		req->nbytes);

	int_error = ablkcipher_walk_phys(req, &ta_ctx->walk);
	if (int_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev, "walk phys error %x\n",
			int_error);
		return -ENOMEM;
	}

	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"crypto block: src is %lx dst is %lx\n",
		(unsigned long)req->src, (unsigned long)req->dst);

	/* Make sure all pages are even block */
	int_error = sep_oddball_pages(ta_ctx->sep_used, req->src,
		req->nbytes, ta_ctx->walk.blocksize, &new_sg, 1);

	if (int_error < 0) {
		dev_warn(&ta_ctx->sep_used->pdev->dev, "oddball page error\n");
		return -ENOMEM;
	} else if (int_error == 1) {
		ta_ctx->src_sg = new_sg;
		ta_ctx->src_sg_hold = new_sg;
	} else {
		ta_ctx->src_sg = req->src;
		ta_ctx->src_sg_hold = NULL;
	}

	int_error = sep_oddball_pages(ta_ctx->sep_used, req->dst,
		req->nbytes, ta_ctx->walk.blocksize, &new_sg, 0);

	if (int_error < 0) {
		dev_warn(&ta_ctx->sep_used->pdev->dev, "walk phys error %x\n",
			int_error);
		return -ENOMEM;
	} else if (int_error == 1) {
		ta_ctx->dst_sg = new_sg;
		ta_ctx->dst_sg_hold = new_sg;
	} else {
		ta_ctx->dst_sg = req->dst;
		ta_ctx->dst_sg_hold = NULL;
	}

	/* set nbytes for queue status */
	ta_ctx->nbytes = req->nbytes;

	/* Key already done; this is for data */
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "sending data\n");

	/* check for valid data and proper spacing */
	src_ptr = sg_virt(ta_ctx->src_sg);
	dst_ptr = sg_virt(ta_ctx->dst_sg);

	if (!src_ptr || !dst_ptr ||
		(ta_ctx->current_cypher_req->nbytes %
		crypto_ablkcipher_blocksize(tfm))) {

		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"cipher block size odd\n");
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"cipher block size is %x\n",
			crypto_ablkcipher_blocksize(tfm));
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"cipher data size is %x\n",
			ta_ctx->current_cypher_req->nbytes);
		return -EINVAL;
	}

	if (partial_overlap(src_ptr, dst_ptr,
		ta_ctx->current_cypher_req->nbytes)) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"block partial overlap\n");
		return -EINVAL;
	}

	/* Put together the message */
	sep_make_header(ta_ctx, &msg_offset, ta_ctx->block_opcode);

	/* If des, and size is 1 block, put directly in msg */
	if ((ta_ctx->block_opcode == SEP_DES_BLOCK_OPCODE) &&
		(req->nbytes == crypto_ablkcipher_blocksize(tfm))) {

		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"writing out one block des\n");

		copy_result = sg_copy_to_buffer(
			ta_ctx->src_sg, sep_sg_nents(ta_ctx->src_sg),
			small_buf, crypto_ablkcipher_blocksize(tfm));

		if (copy_result != crypto_ablkcipher_blocksize(tfm)) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"des block copy failed\n");
			return -ENOMEM;
		}

		/* Put data into message */
		sep_write_msg(ta_ctx, small_buf,
			crypto_ablkcipher_blocksize(tfm),
			crypto_ablkcipher_blocksize(tfm) * 2,
			&msg_offset, 1);

		/* Put size into message */
		sep_write_msg(ta_ctx, &req->nbytes,
			sizeof(u32), sizeof(u32), &msg_offset, 0);
	} else {
		/* Otherwise, fill out dma tables */
		ta_ctx->dcb_input_data.app_in_address = src_ptr;
		ta_ctx->dcb_input_data.data_in_size = req->nbytes;
		ta_ctx->dcb_input_data.app_out_address = dst_ptr;
		ta_ctx->dcb_input_data.block_size =
			crypto_ablkcipher_blocksize(tfm);
		ta_ctx->dcb_input_data.tail_block_size = 0;
		ta_ctx->dcb_input_data.is_applet = 0;
		ta_ctx->dcb_input_data.src_sg = ta_ctx->src_sg;
		ta_ctx->dcb_input_data.dst_sg = ta_ctx->dst_sg;

		result = sep_create_dcb_dmatables_context_kernel(
			ta_ctx->sep_used,
			&ta_ctx->dcb_region,
			&ta_ctx->dmatables_region,
			&ta_ctx->dma_ctx,
			&ta_ctx->dcb_input_data,
			1);
		if (result) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"crypto dma table create failed\n");
			return -EINVAL;
		}

		/* Portion of msg is nulled (no data) */
		msg[0] = (u32)0;
		msg[1] = (u32)0;
		msg[2] = (u32)0;
		msg[3] = (u32)0;
		msg[4] = (u32)0;
		sep_write_msg(ta_ctx, (void *)msg, sizeof(u32) * 5,
			sizeof(u32) * 5, &msg_offset, 0);
		}

	/**
	 * Before we write the message, we need to overwrite the
	 * vendor's IV with the one from our own ablkcipher walk
	 * iv because this is needed for dm-crypt
	 */
	sep_dump_ivs(req, "sending data block to sep\n");
	if ((ta_ctx->current_request == DES_CBC) &&
		(ta_ctx->des_opmode == SEP_DES_CBC)) {

		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"overwrite vendor iv on DES\n");
		des_internal = (struct sep_des_internal_context *)
			sctx->des_private_ctx.ctx_buf;
		memcpy((void *)des_internal->iv_context,
			ta_ctx->walk.iv, crypto_ablkcipher_ivsize(tfm));
	} else if ((ta_ctx->current_request == AES_CBC) &&
		(ta_ctx->aes_opmode == SEP_AES_CBC)) {

		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"overwrite vendor iv on AES\n");
		aes_internal = (struct sep_aes_internal_context *)
			sctx->aes_private_ctx.cbuff;
		memcpy((void *)aes_internal->aes_ctx_iv,
			ta_ctx->walk.iv, crypto_ablkcipher_ivsize(tfm));
	}

	/* Write context into message */
	if (ta_ctx->block_opcode == SEP_DES_BLOCK_OPCODE) {
		sep_write_context(ta_ctx, &msg_offset,
			&sctx->des_private_ctx,
			sizeof(struct sep_des_private_context));
	} else {
		sep_write_context(ta_ctx, &msg_offset,
			&sctx->aes_private_ctx,
			sizeof(struct sep_aes_private_context));
	}

	/* conclude message */
	sep_end_msg(ta_ctx, msg_offset);

	/* Parent (caller) is now ready to tell the sep to do ahead */
	return 0;
}


/**
 * This function sets things up for a crypto key submit process
 * This does all preparation, but does not try to grab the
 * sep
 * @req: pointer to struct ablkcipher_request
 * returns: 0 if all went well, non zero if error
 */
static int sep_crypto_send_key(struct ablkcipher_request *req)
{

	int int_error;
	u32 msg_offset;
	static u32 msg[10];

	u32 max_length;
	struct this_task_ctx *ta_ctx;
	struct crypto_ablkcipher *tfm;
	struct sep_system_ctx *sctx;

	ta_ctx = ablkcipher_request_ctx(req);
	tfm = crypto_ablkcipher_reqtfm(req);
	sctx = crypto_ablkcipher_ctx(tfm);

	dev_dbg(&ta_ctx->sep_used->pdev->dev, "sending key\n");

	/* start the walk on scatterlists */
	ablkcipher_walk_init(&ta_ctx->walk, req->src, req->dst, req->nbytes);
	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"sep crypto block data size of %x\n", req->nbytes);

	int_error = ablkcipher_walk_phys(req, &ta_ctx->walk);
	if (int_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev, "walk phys error %x\n",
			int_error);
		return -ENOMEM;
	}

	/* check iv */
	if ((ta_ctx->current_request == DES_CBC) &&
		(ta_ctx->des_opmode == SEP_DES_CBC)) {
		if (!ta_ctx->walk.iv) {
			dev_warn(&ta_ctx->sep_used->pdev->dev, "no iv found\n");
			return -EINVAL;
		}

		memcpy(ta_ctx->iv, ta_ctx->walk.iv, SEP_DES_IV_SIZE_BYTES);
	}

	if ((ta_ctx->current_request == AES_CBC) &&
		(ta_ctx->aes_opmode == SEP_AES_CBC)) {
		if (!ta_ctx->walk.iv) {
			dev_warn(&ta_ctx->sep_used->pdev->dev, "no iv found\n");
			return -EINVAL;
		}

		memcpy(ta_ctx->iv, ta_ctx->walk.iv, SEP_AES_IV_SIZE_BYTES);
	}

	/* put together message to SEP */
	/* Start with op code */
	sep_make_header(ta_ctx, &msg_offset, ta_ctx->init_opcode);

	/* now deal with IV */
	if (ta_ctx->init_opcode == SEP_DES_INIT_OPCODE) {
		if (ta_ctx->des_opmode == SEP_DES_CBC) {
			sep_write_msg(ta_ctx, ta_ctx->iv,
				SEP_DES_IV_SIZE_BYTES, sizeof(u32) * 4,
				&msg_offset, 1);
		} else {
			/* Skip if ECB */
			msg_offset += 4 * sizeof(u32);
		}
	} else {
		max_length = ((SEP_AES_IV_SIZE_BYTES + 3) /
			sizeof(u32)) * sizeof(u32);
		if (ta_ctx->aes_opmode == SEP_AES_CBC) {
			sep_write_msg(ta_ctx, ta_ctx->iv,
				SEP_AES_IV_SIZE_BYTES, max_length,
				&msg_offset, 1);
		} else {
				/* Skip if ECB */
				msg_offset += max_length;
			}
		}

	/* load the key */
	if (ta_ctx->init_opcode == SEP_DES_INIT_OPCODE) {
		sep_write_msg(ta_ctx, (void *)&sctx->key.des.key1,
			sizeof(u32) * 8, sizeof(u32) * 8,
			&msg_offset, 1);

		msg[0] = (u32)sctx->des_nbr_keys;
		msg[1] = (u32)ta_ctx->des_encmode;
		msg[2] = (u32)ta_ctx->des_opmode;

		sep_write_msg(ta_ctx, (void *)msg,
			sizeof(u32) * 3, sizeof(u32) * 3,
			&msg_offset, 0);
	} else {
		sep_write_msg(ta_ctx, (void *)&sctx->key.aes,
			sctx->keylen,
			SEP_AES_MAX_KEY_SIZE_BYTES,
			&msg_offset, 1);

		msg[0] = (u32)sctx->aes_key_size;
		msg[1] = (u32)ta_ctx->aes_encmode;
		msg[2] = (u32)ta_ctx->aes_opmode;
		msg[3] = (u32)0; /* Secret key is not used */
		sep_write_msg(ta_ctx, (void *)msg,
			sizeof(u32) * 4, sizeof(u32) * 4,
			&msg_offset, 0);
	}

	/* conclude message */
	sep_end_msg(ta_ctx, msg_offset);

	/* Parent (caller) is now ready to tell the sep to do ahead */
	return 0;
}


/* This needs to be run as a work queue as it can be put asleep */
static void sep_crypto_block(void *data)
{
	unsigned long end_time;

	int result;

	struct ablkcipher_request *req;
	struct this_task_ctx *ta_ctx;
	struct crypto_ablkcipher *tfm;
	struct sep_system_ctx *sctx;
	int are_we_done_yet;

	req = (struct ablkcipher_request *)data;
	ta_ctx = ablkcipher_request_ctx(req);
	tfm = crypto_ablkcipher_reqtfm(req);
	sctx = crypto_ablkcipher_ctx(tfm);

	ta_ctx->are_we_done_yet = &are_we_done_yet;

	pr_debug("sep_crypto_block\n");
	pr_debug("tfm is %p sctx is %p ta_ctx is %p\n",
		tfm, sctx, ta_ctx);
	pr_debug("key_sent is %d\n", sctx->key_sent);

	/* do we need to send the key */
	if (sctx->key_sent == 0) {
		are_we_done_yet = 0;
		result = sep_crypto_send_key(req); /* prep to send key */
		if (result != 0) {
			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"could not prep key %x\n", result);
			sep_crypto_release(sctx, ta_ctx, result);
			return;
		}

		result = sep_crypto_take_sep(ta_ctx);
		if (result) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"sep_crypto_take_sep for key send failed\n");
			sep_crypto_release(sctx, ta_ctx, result);
			return;
		}

		/* now we sit and wait up to a fixed time for completion */
		end_time = jiffies + (WAIT_TIME * HZ);
		while ((time_before(jiffies, end_time)) &&
			(are_we_done_yet == 0))
			schedule();

		/* Done waiting; still not done yet? */
		if (are_we_done_yet == 0) {
			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"Send key job never got done\n");
			sep_crypto_release(sctx, ta_ctx, -EINVAL);
			return;
		}

		/* Set the key sent variable so this can be skipped later */
		sctx->key_sent = 1;
	}

	/* Key sent (or maybe not if we did not have to), now send block */
	are_we_done_yet = 0;

	result = sep_crypto_block_data(req);

	if (result != 0) {
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"could prep not send block %x\n", result);
		sep_crypto_release(sctx, ta_ctx, result);
		return;
	}

	result = sep_crypto_take_sep(ta_ctx);
	if (result) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"sep_crypto_take_sep for block send failed\n");
		sep_crypto_release(sctx, ta_ctx, result);
		return;
	}

	/* now we sit and wait up to a fixed time for completion */
	end_time = jiffies + (WAIT_TIME * HZ);
	while ((time_before(jiffies, end_time)) && (are_we_done_yet == 0))
		schedule();

	/* Done waiting; still not done yet? */
	if (are_we_done_yet == 0) {
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"Send block job never got done\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
		return;
	}

	/* That's it; entire thing done, get out of queue */

	pr_debug("crypto_block leaving\n");
	pr_debug("tfm is %p sctx is %p ta_ctx is %p\n", tfm, sctx, ta_ctx);
}

/**
 * Post operation (after interrupt) for crypto block
 */
static u32 crypto_post_op(struct sep_device *sep)
{
	/* HERE */
	u32 u32_error;
	u32 msg_offset;

	ssize_t copy_result;
	static char small_buf[100];

	struct ablkcipher_request *req;
	struct this_task_ctx *ta_ctx;
	struct sep_system_ctx *sctx;
	struct crypto_ablkcipher *tfm;

	struct sep_des_internal_context *des_internal;
	struct sep_aes_internal_context *aes_internal;

	if (!sep->current_cypher_req)
		return -EINVAL;

	/* hold req since we need to submit work after clearing sep */
	req = sep->current_cypher_req;

	ta_ctx = ablkcipher_request_ctx(sep->current_cypher_req);
	tfm = crypto_ablkcipher_reqtfm(sep->current_cypher_req);
	sctx = crypto_ablkcipher_ctx(tfm);

	pr_debug("crypto_post op\n");
	pr_debug("key_sent is %d tfm is %p sctx is %p ta_ctx is %p\n",
		sctx->key_sent, tfm, sctx, ta_ctx);

	dev_dbg(&ta_ctx->sep_used->pdev->dev, "crypto post_op\n");
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "crypto post_op message dump\n");

	/* first bring msg from shared area to local area */
	memcpy(ta_ctx->msg, sep->shared_addr,
		SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES);

	/* Is this the result of performing init (key to SEP */
	if (sctx->key_sent == 0) {

		/* Did SEP do it okay */
		u32_error = sep_verify_op(ta_ctx, ta_ctx->init_opcode,
			&msg_offset);
		if (u32_error) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"aes init error %x\n", u32_error);
			sep_crypto_release(sctx, ta_ctx, u32_error);
			return u32_error;
			}

		/* Read Context */
		if (ta_ctx->init_opcode == SEP_DES_INIT_OPCODE) {
			sep_read_context(ta_ctx, &msg_offset,
			&sctx->des_private_ctx,
			sizeof(struct sep_des_private_context));
		} else {
			sep_read_context(ta_ctx, &msg_offset,
			&sctx->aes_private_ctx,
			sizeof(struct sep_aes_private_context));
		}

		sep_dump_ivs(req, "after sending key to sep\n");

		/* key sent went okay; release sep, and set are_we_done_yet */
		sctx->key_sent = 1;
		sep_crypto_release(sctx, ta_ctx, -EINPROGRESS);

	} else {

		/**
		 * This is the result of a block request
		 */
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"crypto_post_op block response\n");

		u32_error = sep_verify_op(ta_ctx, ta_ctx->block_opcode,
			&msg_offset);

		if (u32_error) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"sep block error %x\n", u32_error);
			sep_crypto_release(sctx, ta_ctx, u32_error);
			return -EINVAL;
			}

		if (ta_ctx->block_opcode == SEP_DES_BLOCK_OPCODE) {

			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"post op for DES\n");

			/* special case for 1 block des */
			if (sep->current_cypher_req->nbytes ==
				crypto_ablkcipher_blocksize(tfm)) {

				sep_read_msg(ta_ctx, small_buf,
					crypto_ablkcipher_blocksize(tfm),
					crypto_ablkcipher_blocksize(tfm) * 2,
					&msg_offset, 1);

				dev_dbg(&ta_ctx->sep_used->pdev->dev,
					"reading in block des\n");

				copy_result = sg_copy_from_buffer(
					ta_ctx->dst_sg,
					sep_sg_nents(ta_ctx->dst_sg),
					small_buf,
					crypto_ablkcipher_blocksize(tfm));

				if (copy_result !=
					crypto_ablkcipher_blocksize(tfm)) {

					dev_warn(&ta_ctx->sep_used->pdev->dev,
						"des block copy failed\n");
					sep_crypto_release(sctx, ta_ctx,
						-ENOMEM);
					return -ENOMEM;
				}
			}

			/* Read Context */
			sep_read_context(ta_ctx, &msg_offset,
				&sctx->des_private_ctx,
				sizeof(struct sep_des_private_context));
		} else {

			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"post op for AES\n");

			/* Skip the MAC Output */
			msg_offset += (sizeof(u32) * 4);

			/* Read Context */
			sep_read_context(ta_ctx, &msg_offset,
				&sctx->aes_private_ctx,
				sizeof(struct sep_aes_private_context));
		}

		/* Copy to correct sg if this block had oddball pages */
		if (ta_ctx->dst_sg_hold)
			sep_copy_sg(ta_ctx->sep_used,
				ta_ctx->dst_sg,
				ta_ctx->current_cypher_req->dst,
				ta_ctx->current_cypher_req->nbytes);

		/**
		 * Copy the iv's back to the walk.iv
		 * This is required for dm_crypt
		 */
		sep_dump_ivs(req, "got data block from sep\n");
		if ((ta_ctx->current_request == DES_CBC) &&
			(ta_ctx->des_opmode == SEP_DES_CBC)) {

			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"returning result iv to walk on DES\n");
			des_internal = (struct sep_des_internal_context *)
				sctx->des_private_ctx.ctx_buf;
			memcpy(ta_ctx->walk.iv,
				(void *)des_internal->iv_context,
				crypto_ablkcipher_ivsize(tfm));
		} else if ((ta_ctx->current_request == AES_CBC) &&
			(ta_ctx->aes_opmode == SEP_AES_CBC)) {

			dev_dbg(&ta_ctx->sep_used->pdev->dev,
				"returning result iv to walk on AES\n");
			aes_internal = (struct sep_aes_internal_context *)
				sctx->aes_private_ctx.cbuff;
			memcpy(ta_ctx->walk.iv,
				(void *)aes_internal->aes_ctx_iv,
				crypto_ablkcipher_ivsize(tfm));
		}

		/* finished, release everything */
		sep_crypto_release(sctx, ta_ctx, 0);
	}
	pr_debug("crypto_post_op done\n");
	pr_debug("key_sent is %d tfm is %p sctx is %p ta_ctx is %p\n",
		sctx->key_sent, tfm, sctx, ta_ctx);

	return 0;
}

static u32 hash_init_post_op(struct sep_device *sep)
{
	u32 u32_error;
	u32 msg_offset;
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(sep->current_hash_req);
	struct this_task_ctx *ta_ctx = ahash_request_ctx(sep->current_hash_req);
	struct sep_system_ctx *sctx = crypto_ahash_ctx(tfm);
	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"hash init post op\n");

	/* first bring msg from shared area to local area */
	memcpy(ta_ctx->msg, sep->shared_addr,
		SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES);

	u32_error = sep_verify_op(ta_ctx, SEP_HASH_INIT_OPCODE,
		&msg_offset);

	if (u32_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev, "hash init error %x\n",
			u32_error);
		sep_crypto_release(sctx, ta_ctx, u32_error);
		return u32_error;
		}

	/* Read Context */
	sep_read_context(ta_ctx, &msg_offset,
		&sctx->hash_private_ctx,
		sizeof(struct sep_hash_private_context));

	/* Signal to crypto infrastructure and clear out */
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "hash init post op done\n");
	sep_crypto_release(sctx, ta_ctx, 0);
	return 0;
}

static u32 hash_update_post_op(struct sep_device *sep)
{
	u32 u32_error;
	u32 msg_offset;
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(sep->current_hash_req);
	struct this_task_ctx *ta_ctx = ahash_request_ctx(sep->current_hash_req);
	struct sep_system_ctx *sctx = crypto_ahash_ctx(tfm);
	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"hash update post op\n");

	/* first bring msg from shared area to local area */
	memcpy(ta_ctx->msg, sep->shared_addr,
		SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES);

	u32_error = sep_verify_op(ta_ctx, SEP_HASH_UPDATE_OPCODE,
		&msg_offset);

	if (u32_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev, "hash init error %x\n",
			u32_error);
		sep_crypto_release(sctx, ta_ctx, u32_error);
		return u32_error;
		}

	/* Read Context */
	sep_read_context(ta_ctx, &msg_offset,
		&sctx->hash_private_ctx,
		sizeof(struct sep_hash_private_context));

	/**
	 * Following is only for finup; if we just completed the
	 * data portion of finup, we now need to kick off the
	 * finish portion of finup.
	 */

	if (ta_ctx->sep_used->current_hash_stage == HASH_FINUP_DATA) {

		/* first reset stage to HASH_FINUP_FINISH */
		ta_ctx->sep_used->current_hash_stage = HASH_FINUP_FINISH;

		/* now enqueue the finish operation */
		spin_lock_irq(&queue_lock);
		u32_error = crypto_enqueue_request(&sep_queue,
			&ta_ctx->sep_used->current_hash_req->base);
		spin_unlock_irq(&queue_lock);

		if ((u32_error != 0) && (u32_error != -EINPROGRESS)) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"spe cypher post op cant queue\n");
			sep_crypto_release(sctx, ta_ctx, u32_error);
			return u32_error;
		}

		/* schedule the data send */
		u32_error = sep_submit_work(ta_ctx->sep_used->workqueue,
			sep_dequeuer, (void *)&sep_queue);

		if (u32_error) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"cant submit work sep_crypto_block\n");
			sep_crypto_release(sctx, ta_ctx, -EINVAL);
			return -EINVAL;
		}
	}

	/* Signal to crypto infrastructure and clear out */
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "hash update post op done\n");
	sep_crypto_release(sctx, ta_ctx, 0);
	return 0;
}

static u32 hash_final_post_op(struct sep_device *sep)
{
	int max_length;
	u32 u32_error;
	u32 msg_offset;
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(sep->current_hash_req);
	struct sep_system_ctx *sctx = crypto_ahash_ctx(tfm);
	struct this_task_ctx *ta_ctx = ahash_request_ctx(sep->current_hash_req);
	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"hash final post op\n");

	/* first bring msg from shared area to local area */
	memcpy(ta_ctx->msg, sep->shared_addr,
		SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES);

	u32_error = sep_verify_op(ta_ctx, SEP_HASH_FINISH_OPCODE,
		&msg_offset);

	if (u32_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev, "hash finish error %x\n",
			u32_error);
		sep_crypto_release(sctx, ta_ctx, u32_error);
		return u32_error;
		}

	/* Grab the result */
	if (ta_ctx->current_hash_req->result == NULL) {
		/* Oops, null buffer; error out here */
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"hash finish null buffer\n");
		sep_crypto_release(sctx, ta_ctx, (u32)-ENOMEM);
		return -ENOMEM;
		}

	max_length = (((SEP_HASH_RESULT_SIZE_WORDS * sizeof(u32)) + 3) /
		sizeof(u32)) * sizeof(u32);

	sep_read_msg(ta_ctx,
		ta_ctx->current_hash_req->result,
		crypto_ahash_digestsize(tfm), max_length,
		&msg_offset, 0);

	/* Signal to crypto infrastructure and clear out */
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "hash finish post op done\n");
	sep_crypto_release(sctx, ta_ctx, 0);
	return 0;
}

static u32 hash_digest_post_op(struct sep_device *sep)
{
	int max_length;
	u32 u32_error;
	u32 msg_offset;
	struct crypto_ahash *tfm = crypto_ahash_reqtfm(sep->current_hash_req);
	struct sep_system_ctx *sctx = crypto_ahash_ctx(tfm);
	struct this_task_ctx *ta_ctx = ahash_request_ctx(sep->current_hash_req);
	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"hash digest post op\n");

	/* first bring msg from shared area to local area */
	memcpy(ta_ctx->msg, sep->shared_addr,
		SEP_DRIVER_MESSAGE_SHARED_AREA_SIZE_IN_BYTES);

	u32_error = sep_verify_op(ta_ctx, SEP_HASH_SINGLE_OPCODE,
		&msg_offset);

	if (u32_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"hash digest finish error %x\n", u32_error);

		sep_crypto_release(sctx, ta_ctx, u32_error);
		return u32_error;
		}

	/* Grab the result */
	if (ta_ctx->current_hash_req->result == NULL) {
		/* Oops, null buffer; error out here */
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"hash digest finish null buffer\n");
		sep_crypto_release(sctx, ta_ctx, (u32)-ENOMEM);
		return -ENOMEM;
		}

	max_length = (((SEP_HASH_RESULT_SIZE_WORDS * sizeof(u32)) + 3) /
		sizeof(u32)) * sizeof(u32);

	sep_read_msg(ta_ctx,
		ta_ctx->current_hash_req->result,
		crypto_ahash_digestsize(tfm), max_length,
		&msg_offset, 0);

	/* Signal to crypto infrastructure and clear out */
	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"hash digest finish post op done\n");

	sep_crypto_release(sctx, ta_ctx, 0);
	return 0;
}

/**
 * The sep_finish function is the function that is scheduled (via tasklet)
 * by the interrupt service routine when the SEP sends and interrupt
 * This is only called by the interrupt handler as a tasklet.
 */
static void sep_finish(unsigned long data)
{
	struct sep_device *sep_dev;
	int res;

	res = 0;

	if (data == 0) {
		pr_debug("sep_finish called with null data\n");
		return;
	}

	sep_dev = (struct sep_device *)data;
	if (sep_dev == NULL) {
		pr_debug("sep_finish; sep_dev is NULL\n");
		return;
	}

	if (sep_dev->in_kernel == (u32)0) {
		dev_warn(&sep_dev->pdev->dev,
			"sep_finish; not in kernel operation\n");
		return;
	}

	/* Did we really do a sep command prior to this? */
	if (0 == test_bit(SEP_LEGACY_SENDMSG_DONE_OFFSET,
		&sep_dev->ta_ctx->call_status.status)) {

		dev_warn(&sep_dev->pdev->dev, "[PID%d] sendmsg not called\n",
			current->pid);
		return;
	}

	if (sep_dev->send_ct != sep_dev->reply_ct) {
		dev_warn(&sep_dev->pdev->dev,
			"[PID%d] poll; no message came back\n",
			current->pid);
		return;
	}

	/* Check for error (In case time ran out) */
	if ((res != 0x0) && (res != 0x8)) {
		dev_warn(&sep_dev->pdev->dev,
			"[PID%d] poll; poll error GPR3 is %x\n",
			current->pid, res);
		return;
	}

	/* What kind of interrupt from sep was this? */
	res = sep_read_reg(sep_dev, HW_HOST_SEP_HOST_GPR2_REG_ADDR);

	dev_dbg(&sep_dev->pdev->dev, "[PID%d] GPR2 at crypto finish is %x\n",
		current->pid, res);

	/* Print request? */
	if ((res >> 30) & 0x1) {
		dev_dbg(&sep_dev->pdev->dev, "[PID%d] sep print req\n",
			current->pid);
		dev_dbg(&sep_dev->pdev->dev, "[PID%d] contents: %s\n",
			current->pid,
			(char *)(sep_dev->shared_addr +
			SEP_DRIVER_PRINTF_OFFSET_IN_BYTES));
		return;
	}

	/* Request for daemon (not currently in POR)? */
	if (res >> 31) {
		dev_dbg(&sep_dev->pdev->dev,
			"[PID%d] sep request; ignoring\n",
			current->pid);
		return;
	}

	/* If we got here, then we have a replay to a sep command */

	dev_dbg(&sep_dev->pdev->dev,
		"[PID%d] sep reply to command; processing request: %x\n",
		current->pid, sep_dev->current_request);

	switch (sep_dev->current_request) {
	case AES_CBC:
	case AES_ECB:
	case DES_CBC:
	case DES_ECB:
		res = crypto_post_op(sep_dev);
		break;
	case SHA1:
	case MD5:
	case SHA224:
	case SHA256:
		switch (sep_dev->current_hash_stage) {
		case HASH_INIT:
			res = hash_init_post_op(sep_dev);
			break;
		case HASH_UPDATE:
		case HASH_FINUP_DATA:
			res = hash_update_post_op(sep_dev);
			break;
		case HASH_FINUP_FINISH:
		case HASH_FINISH:
			res = hash_final_post_op(sep_dev);
			break;
		case HASH_DIGEST:
			res = hash_digest_post_op(sep_dev);
			break;
		default:
			pr_debug("sep - invalid stage for hash finish\n");
		}
		break;
	default:
		pr_debug("sep - invalid request for finish\n");
	}

	if (res)
		pr_debug("sep - finish returned error %x\n", res);
}

static int sep_hash_cra_init(struct crypto_tfm *tfm)
	{
	const char *alg_name = crypto_tfm_alg_name(tfm);

	pr_debug("sep_hash_cra_init name is %s\n", alg_name);

	crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
		sizeof(struct this_task_ctx));
	return 0;
	}

static void sep_hash_cra_exit(struct crypto_tfm *tfm)
{
	pr_debug("sep_hash_cra_exit\n");
}

static void sep_hash_init(void *data)
{
	u32 msg_offset;
	int result;
	struct ahash_request *req;
	struct crypto_ahash *tfm;
	struct this_task_ctx *ta_ctx;
	struct sep_system_ctx *sctx;
	unsigned long end_time;
	int are_we_done_yet;

	req = (struct ahash_request *)data;
	tfm = crypto_ahash_reqtfm(req);
	sctx = crypto_ahash_ctx(tfm);
	ta_ctx = ahash_request_ctx(req);
	ta_ctx->sep_used = sep_dev;

	ta_ctx->are_we_done_yet = &are_we_done_yet;

	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"sep_hash_init\n");
	ta_ctx->current_hash_stage = HASH_INIT;
	/* opcode and mode */
	sep_make_header(ta_ctx, &msg_offset, SEP_HASH_INIT_OPCODE);
	sep_write_msg(ta_ctx, &ta_ctx->hash_opmode,
		sizeof(u32), sizeof(u32), &msg_offset, 0);
	sep_end_msg(ta_ctx, msg_offset);

	are_we_done_yet = 0;
	result = sep_crypto_take_sep(ta_ctx);
	if (result) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"sep_hash_init take sep failed\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
	}

	/* now we sit and wait up to a fixed time for completion */
	end_time = jiffies + (WAIT_TIME * HZ);
	while ((time_before(jiffies, end_time)) && (are_we_done_yet == 0))
		schedule();

	/* Done waiting; still not done yet? */
	if (are_we_done_yet == 0) {
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"hash init never got done\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
		return;
	}

}

static void sep_hash_update(void *data)
{
	int int_error;
	u32 msg_offset;
	u32 len;
	struct sep_hash_internal_context *int_ctx;
	u32 block_size;
	u32 head_len;
	u32 tail_len;
	int are_we_done_yet;

	static u32 msg[10];
	static char small_buf[100];
	void *src_ptr;
	struct scatterlist *new_sg;
	ssize_t copy_result;
	struct ahash_request *req;
	struct crypto_ahash *tfm;
	struct this_task_ctx *ta_ctx;
	struct sep_system_ctx *sctx;
	unsigned long end_time;

	req = (struct ahash_request *)data;
	tfm = crypto_ahash_reqtfm(req);
	sctx = crypto_ahash_ctx(tfm);
	ta_ctx = ahash_request_ctx(req);
	ta_ctx->sep_used = sep_dev;

	ta_ctx->are_we_done_yet = &are_we_done_yet;

	/* length for queue status */
	ta_ctx->nbytes = req->nbytes;

	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"sep_hash_update\n");
	ta_ctx->current_hash_stage = HASH_UPDATE;
	len = req->nbytes;

	block_size = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
	tail_len = req->nbytes % block_size;
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "length is %x\n", len);
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "block_size is %x\n", block_size);
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "tail len is %x\n", tail_len);

	/* Compute header/tail sizes */
	int_ctx = (struct sep_hash_internal_context *)&sctx->
		hash_private_ctx.internal_context;
	head_len = (block_size - int_ctx->prev_update_bytes) % block_size;
	tail_len = (req->nbytes - head_len) % block_size;

	/* Make sure all pages are an even block */
	int_error = sep_oddball_pages(ta_ctx->sep_used, req->src,
		req->nbytes,
		block_size, &new_sg, 1);

	if (int_error < 0) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"oddball pages error in crash update\n");
		sep_crypto_release(sctx, ta_ctx, -ENOMEM);
		return;
	} else if (int_error == 1) {
		ta_ctx->src_sg = new_sg;
		ta_ctx->src_sg_hold = new_sg;
	} else {
		ta_ctx->src_sg = req->src;
		ta_ctx->src_sg_hold = NULL;
	}

	src_ptr = sg_virt(ta_ctx->src_sg);

	if ((!req->nbytes) || (!ta_ctx->src_sg)) {
		/* null data */
		src_ptr = NULL;
	}

	ta_ctx->dcb_input_data.app_in_address = src_ptr;
	ta_ctx->dcb_input_data.data_in_size =
		req->nbytes - (head_len + tail_len);
	ta_ctx->dcb_input_data.app_out_address = NULL;
	ta_ctx->dcb_input_data.block_size = block_size;
	ta_ctx->dcb_input_data.tail_block_size = 0;
	ta_ctx->dcb_input_data.is_applet = 0;
	ta_ctx->dcb_input_data.src_sg = ta_ctx->src_sg;
	ta_ctx->dcb_input_data.dst_sg = NULL;

	int_error = sep_create_dcb_dmatables_context_kernel(
		ta_ctx->sep_used,
		&ta_ctx->dcb_region,
		&ta_ctx->dmatables_region,
		&ta_ctx->dma_ctx,
		&ta_ctx->dcb_input_data,
		1);
	if (int_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"hash update dma table create failed\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
		return;
	}

	/* Construct message to SEP */
	sep_make_header(ta_ctx, &msg_offset, SEP_HASH_UPDATE_OPCODE);

	msg[0] = (u32)0;
	msg[1] = (u32)0;
	msg[2] = (u32)0;

	sep_write_msg(ta_ctx, msg, sizeof(u32) * 3, sizeof(u32) * 3,
		&msg_offset, 0);

	/* Handle remainders */

	/* Head */
	sep_write_msg(ta_ctx, &head_len, sizeof(u32),
		sizeof(u32), &msg_offset, 0);

	if (head_len) {
		copy_result = sg_copy_to_buffer(
			req->src,
			sep_sg_nents(ta_ctx->src_sg),
			small_buf, head_len);

		if (copy_result != head_len) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"sg head copy failure in hash block\n");
			sep_crypto_release(sctx, ta_ctx, -ENOMEM);
			return;
		}

		sep_write_msg(ta_ctx, small_buf, head_len,
			sizeof(u32) * 32, &msg_offset, 1);
	} else {
		msg_offset += sizeof(u32) * 32;
	}

	/* Tail */
	sep_write_msg(ta_ctx, &tail_len, sizeof(u32),
		sizeof(u32), &msg_offset, 0);

	if (tail_len) {
		copy_result = sep_copy_offset_sg(
			ta_ctx->sep_used,
			ta_ctx->src_sg,
			req->nbytes - tail_len,
			small_buf, tail_len);

		if (copy_result != tail_len) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"sg tail copy failure in hash block\n");
			sep_crypto_release(sctx, ta_ctx, -ENOMEM);
			return;
		}

		sep_write_msg(ta_ctx, small_buf, tail_len,
			sizeof(u32) * 32, &msg_offset, 1);
	} else {
		msg_offset += sizeof(u32) * 32;
	}

	/* Context */
	sep_write_context(ta_ctx, &msg_offset, &sctx->hash_private_ctx,
		sizeof(struct sep_hash_private_context));

	sep_end_msg(ta_ctx, msg_offset);
	are_we_done_yet = 0;
	int_error = sep_crypto_take_sep(ta_ctx);
	if (int_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"sep_hash_update take sep failed\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
	}

	/* now we sit and wait up to a fixed time for completion */
	end_time = jiffies + (WAIT_TIME * HZ);
	while ((time_before(jiffies, end_time)) && (are_we_done_yet == 0))
		schedule();

	/* Done waiting; still not done yet? */
	if (are_we_done_yet == 0) {
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"hash update never got done\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
		return;
	}

}

static void sep_hash_final(void *data)
{
	u32 msg_offset;
	struct ahash_request *req;
	struct crypto_ahash *tfm;
	struct this_task_ctx *ta_ctx;
	struct sep_system_ctx *sctx;
	int result;
	unsigned long end_time;
	int are_we_done_yet;

	req = (struct ahash_request *)data;
	tfm = crypto_ahash_reqtfm(req);
	sctx = crypto_ahash_ctx(tfm);
	ta_ctx = ahash_request_ctx(req);
	ta_ctx->sep_used = sep_dev;

	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"sep_hash_final\n");
	ta_ctx->current_hash_stage = HASH_FINISH;

	ta_ctx->are_we_done_yet = &are_we_done_yet;

	/* opcode and mode */
	sep_make_header(ta_ctx, &msg_offset, SEP_HASH_FINISH_OPCODE);

	/* Context */
	sep_write_context(ta_ctx, &msg_offset, &sctx->hash_private_ctx,
		sizeof(struct sep_hash_private_context));

	sep_end_msg(ta_ctx, msg_offset);
	are_we_done_yet = 0;
	result = sep_crypto_take_sep(ta_ctx);
	if (result) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"sep_hash_final take sep failed\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
	}

	/* now we sit and wait up to a fixed time for completion */
	end_time = jiffies + (WAIT_TIME * HZ);
	while ((time_before(jiffies, end_time)) && (are_we_done_yet == 0))
		schedule();

	/* Done waiting; still not done yet? */
	if (are_we_done_yet == 0) {
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"hash final job never got done\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
		return;
	}

}

static void sep_hash_digest(void *data)
{
	int int_error;
	u32 msg_offset;
	u32 block_size;
	u32 msg[10];
	size_t copy_result;
	int result;
	int are_we_done_yet;
	u32 tail_len;
	static char small_buf[100];
	struct scatterlist *new_sg;
	void *src_ptr;

	struct ahash_request *req;
	struct crypto_ahash *tfm;
	struct this_task_ctx *ta_ctx;
	struct sep_system_ctx *sctx;
	unsigned long end_time;

	req = (struct ahash_request *)data;
	tfm = crypto_ahash_reqtfm(req);
	sctx = crypto_ahash_ctx(tfm);
	ta_ctx = ahash_request_ctx(req);
	ta_ctx->sep_used = sep_dev;

	dev_dbg(&ta_ctx->sep_used->pdev->dev,
		"sep_hash_digest\n");
	ta_ctx->current_hash_stage = HASH_DIGEST;

	ta_ctx->are_we_done_yet = &are_we_done_yet;

	/* length for queue status */
	ta_ctx->nbytes = req->nbytes;

	block_size = crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm));
	tail_len = req->nbytes % block_size;
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "length is %x\n", req->nbytes);
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "block_size is %x\n", block_size);
	dev_dbg(&ta_ctx->sep_used->pdev->dev, "tail len is %x\n", tail_len);

	/* Make sure all pages are an even block */
	int_error = sep_oddball_pages(ta_ctx->sep_used, req->src,
		req->nbytes,
		block_size, &new_sg, 1);

	if (int_error < 0) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"oddball pages error in crash update\n");
		sep_crypto_release(sctx, ta_ctx, -ENOMEM);
		return;
	} else if (int_error == 1) {
		ta_ctx->src_sg = new_sg;
		ta_ctx->src_sg_hold = new_sg;
	} else {
		ta_ctx->src_sg = req->src;
		ta_ctx->src_sg_hold = NULL;
	}

	src_ptr = sg_virt(ta_ctx->src_sg);

	if ((!req->nbytes) || (!ta_ctx->src_sg)) {
		/* null data */
		src_ptr = NULL;
	}

	ta_ctx->dcb_input_data.app_in_address = src_ptr;
	ta_ctx->dcb_input_data.data_in_size = req->nbytes - tail_len;
	ta_ctx->dcb_input_data.app_out_address = NULL;
	ta_ctx->dcb_input_data.block_size = block_size;
	ta_ctx->dcb_input_data.tail_block_size = 0;
	ta_ctx->dcb_input_data.is_applet = 0;
	ta_ctx->dcb_input_data.src_sg = ta_ctx->src_sg;
	ta_ctx->dcb_input_data.dst_sg = NULL;

	int_error = sep_create_dcb_dmatables_context_kernel(
		ta_ctx->sep_used,
		&ta_ctx->dcb_region,
		&ta_ctx->dmatables_region,
		&ta_ctx->dma_ctx,
		&ta_ctx->dcb_input_data,
		1);
	if (int_error) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"hash update dma table create failed\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
		return;
	}

	/* Construct message to SEP */
	sep_make_header(ta_ctx, &msg_offset, SEP_HASH_SINGLE_OPCODE);
	sep_write_msg(ta_ctx, &ta_ctx->hash_opmode,
		sizeof(u32), sizeof(u32), &msg_offset, 0);

	msg[0] = (u32)0;
	msg[1] = (u32)0;
	msg[2] = (u32)0;

	sep_write_msg(ta_ctx, msg, sizeof(u32) * 3, sizeof(u32) * 3,
		&msg_offset, 0);

	/* Tail */
	sep_write_msg(ta_ctx, &tail_len, sizeof(u32),
		sizeof(u32), &msg_offset, 0);

	if (tail_len) {
		copy_result = sep_copy_offset_sg(
			ta_ctx->sep_used,
			ta_ctx->src_sg,
			req->nbytes - tail_len,
			small_buf, tail_len);

		if (copy_result != tail_len) {
			dev_warn(&ta_ctx->sep_used->pdev->dev,
				"sg tail copy failure in hash block\n");
			sep_crypto_release(sctx, ta_ctx, -ENOMEM);
			return;
		}

		sep_write_msg(ta_ctx, small_buf, tail_len,
			sizeof(u32) * 32, &msg_offset, 1);
	} else {
		msg_offset += sizeof(u32) * 32;
	}

	sep_end_msg(ta_ctx, msg_offset);

	are_we_done_yet = 0;
	result = sep_crypto_take_sep(ta_ctx);
	if (result) {
		dev_warn(&ta_ctx->sep_used->pdev->dev,
			"sep_hash_digest take sep failed\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
	}

	/* now we sit and wait up to a fixed time for completion */
	end_time = jiffies + (WAIT_TIME * HZ);
	while ((time_before(jiffies, end_time)) && (are_we_done_yet == 0))
		schedule();

	/* Done waiting; still not done yet? */
	if (are_we_done_yet == 0) {
		dev_dbg(&ta_ctx->sep_used->pdev->dev,
			"hash digest job never got done\n");
		sep_crypto_release(sctx, ta_ctx, -EINVAL);
		return;
	}

}

/**
 * This is what is called by each of the API's provided
 * in the kernel crypto descriptors. It is run in a process
 * context using the kernel workqueues. Therefore it can
 * be put to sleep.
 */
static void sep_dequeuer(void *data)
{
	struct crypto_queue *this_queue;
	struct crypto_async_request *async_req;
	struct crypto_async_request *backlog;
	struct ablkcipher_request *cypher_req;
	struct ahash_request *hash_req;
	struct sep_system_ctx *sctx;
	struct crypto_ahash *hash_tfm;
	struct this_task_ctx *ta_ctx;


	this_queue = (struct crypto_queue *)data;

	spin_lock_irq(&queue_lock);
	backlog = crypto_get_backlog(this_queue);
	async_req = crypto_dequeue_request(this_queue);
	spin_unlock_irq(&queue_lock);

	if (!async_req) {
		pr_debug("sep crypto queue is empty\n");
		return;
	}

	if (backlog) {
		pr_debug("sep crypto backlog set\n");
		if (backlog->complete)
			backlog->complete(backlog, -EINPROGRESS);
		backlog = NULL;
	}

	if (!async_req->tfm) {
		pr_debug("sep crypto queue null tfm\n");
		return;
	}

	if (!async_req->tfm->__crt_alg) {
		pr_debug("sep crypto queue null __crt_alg\n");
		return;
	}

	if (!async_req->tfm->__crt_alg->cra_type) {
		pr_debug("sep crypto queue null cra_type\n");
		return;
	}

	/* we have stuff in the queue */
	if (async_req->tfm->__crt_alg->cra_type !=
		&crypto_ahash_type) {
		/* This is for a cypher */
		pr_debug("sep crypto queue doing cipher\n");
		cypher_req = container_of(async_req,
			struct ablkcipher_request,
			base);
		if (!cypher_req) {
			pr_debug("sep crypto queue null cypher_req\n");
			return;
		}

		sep_crypto_block((void *)cypher_req);
		return;
	} else {
		/* This is a hash */
		pr_debug("sep crypto queue doing hash\n");
		/**
		 * This is a bit more complex than cipher; we
		 * need to figure out what type of operation
		 */
		hash_req = ahash_request_cast(async_req);
		if (!hash_req) {
			pr_debug("sep crypto queue null hash_req\n");
			return;
		}

		hash_tfm = crypto_ahash_reqtfm(hash_req);
		if (!hash_tfm) {
			pr_debug("sep crypto queue null hash_tfm\n");
			return;
		}


		sctx = crypto_ahash_ctx(hash_tfm);
		if (!sctx) {
			pr_debug("sep crypto queue null sctx\n");
			return;
		}

		ta_ctx = ahash_request_ctx(hash_req);

		if (ta_ctx->current_hash_stage == HASH_INIT) {
			pr_debug("sep crypto queue hash init\n");
			sep_hash_init((void *)hash_req);
			return;
		} else if (ta_ctx->current_hash_stage == HASH_UPDATE) {
			pr_debug("sep crypto queue hash update\n");
			sep_hash_update((void *)hash_req);
			return;
		} else if (ta_ctx->current_hash_stage == HASH_FINISH) {
			pr_debug("sep crypto queue hash final\n");
			sep_hash_final((void *)hash_req);
			return;
		} else if (ta_ctx->current_hash_stage == HASH_DIGEST) {
			pr_debug("sep crypto queue hash digest\n");
			sep_hash_digest((void *)hash_req);
			return;
		} else if (ta_ctx->current_hash_stage == HASH_FINUP_DATA) {
			pr_debug("sep crypto queue hash digest\n");
			sep_hash_update((void *)hash_req);
			return;
		} else if (ta_ctx->current_hash_stage == HASH_FINUP_FINISH) {
			pr_debug("sep crypto queue hash digest\n");
			sep_hash_final((void *)hash_req);
			return;
		} else {
			pr_debug("sep crypto queue hash oops nothing\n");
			return;
		}
	}
}

static int sep_sha1_init(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);

	pr_debug("sep - doing sha1 init\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA1;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA1;
	ta_ctx->current_hash_stage = HASH_INIT;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha1_update(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);

	pr_debug("sep - doing sha1 update\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA1;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA1;
	ta_ctx->current_hash_stage = HASH_UPDATE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha1_final(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha1 final\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA1;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA1;
	ta_ctx->current_hash_stage = HASH_FINISH;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha1_digest(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha1 digest\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA1;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA1;
	ta_ctx->current_hash_stage = HASH_DIGEST;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha1_finup(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha1 finup\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA1;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA1;
	ta_ctx->current_hash_stage = HASH_FINUP_DATA;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_md5_init(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing md5 init\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = MD5;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_MD5;
	ta_ctx->current_hash_stage = HASH_INIT;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_md5_update(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing md5 update\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = MD5;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_MD5;
	ta_ctx->current_hash_stage = HASH_UPDATE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_md5_final(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing md5 final\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = MD5;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_MD5;
	ta_ctx->current_hash_stage = HASH_FINISH;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_md5_digest(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);

	pr_debug("sep - doing md5 digest\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = MD5;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_MD5;
	ta_ctx->current_hash_stage = HASH_DIGEST;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_md5_finup(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);

	pr_debug("sep - doing md5 finup\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = MD5;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_MD5;
	ta_ctx->current_hash_stage = HASH_FINUP_DATA;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha224_init(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha224 init\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA224;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA224;
	ta_ctx->current_hash_stage = HASH_INIT;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha224_update(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha224 update\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA224;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA224;
	ta_ctx->current_hash_stage = HASH_UPDATE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha224_final(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha224 final\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA224;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA224;
	ta_ctx->current_hash_stage = HASH_FINISH;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha224_digest(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);

	pr_debug("sep - doing sha224 digest\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA224;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA224;
	ta_ctx->current_hash_stage = HASH_DIGEST;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha224_finup(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);

	pr_debug("sep - doing sha224 finup\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA224;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA224;
	ta_ctx->current_hash_stage = HASH_FINUP_DATA;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha256_init(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha256 init\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA256;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA256;
	ta_ctx->current_hash_stage = HASH_INIT;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha256_update(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha256 update\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA256;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA256;
	ta_ctx->current_hash_stage = HASH_UPDATE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha256_final(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);
	pr_debug("sep - doing sha256 final\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA256;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA256;
	ta_ctx->current_hash_stage = HASH_FINISH;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha256_digest(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);

	pr_debug("sep - doing sha256 digest\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA256;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA256;
	ta_ctx->current_hash_stage = HASH_DIGEST;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_sha256_finup(struct ahash_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ahash_request_ctx(req);

	pr_debug("sep - doing sha256 finup\n");

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = SHA256;
	ta_ctx->current_hash_req = req;
	ta_ctx->current_cypher_req = NULL;
	ta_ctx->hash_opmode = SEP_HASH_SHA256;
	ta_ctx->current_hash_stage = HASH_FINUP_DATA;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_crypto_init(struct crypto_tfm *tfm)
{
	const char *alg_name = crypto_tfm_alg_name(tfm);

	if (alg_name == NULL)
		pr_debug("sep_crypto_init alg is NULL\n");
	else
		pr_debug("sep_crypto_init alg is %s\n", alg_name);

	tfm->crt_ablkcipher.reqsize = sizeof(struct this_task_ctx);
	return 0;
}

static void sep_crypto_exit(struct crypto_tfm *tfm)
{
	pr_debug("sep_crypto_exit\n");
}

static int sep_aes_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
	unsigned int keylen)
{
	struct sep_system_ctx *sctx = crypto_ablkcipher_ctx(tfm);

	pr_debug("sep aes setkey\n");

	pr_debug("tfm is %p sctx is %p\n", tfm, sctx);
	switch (keylen) {
	case SEP_AES_KEY_128_SIZE:
		sctx->aes_key_size = AES_128;
		break;
	case SEP_AES_KEY_192_SIZE:
		sctx->aes_key_size = AES_192;
		break;
	case SEP_AES_KEY_256_SIZE:
		sctx->aes_key_size = AES_256;
		break;
	case SEP_AES_KEY_512_SIZE:
		sctx->aes_key_size = AES_512;
		break;
	default:
		pr_debug("invalid sep aes key size %x\n",
			keylen);
		return -EINVAL;
	}

	memset(&sctx->key.aes, 0, sizeof(u32) *
		SEP_AES_MAX_KEY_SIZE_WORDS);
	memcpy(&sctx->key.aes, key, keylen);
	sctx->keylen = keylen;
	/* Indicate to encrypt/decrypt function to send key to SEP */
	sctx->key_sent = 0;

	return 0;
}

static int sep_aes_ecb_encrypt(struct ablkcipher_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ablkcipher_request_ctx(req);

	pr_debug("sep - doing aes ecb encrypt\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = AES_ECB;
	ta_ctx->current_hash_req = NULL;
	ta_ctx->current_cypher_req = req;
	ta_ctx->aes_encmode = SEP_AES_ENCRYPT;
	ta_ctx->aes_opmode = SEP_AES_ECB;
	ta_ctx->init_opcode = SEP_AES_INIT_OPCODE;
	ta_ctx->block_opcode = SEP_AES_BLOCK_OPCODE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_aes_ecb_decrypt(struct ablkcipher_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ablkcipher_request_ctx(req);

	pr_debug("sep - doing aes ecb decrypt\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = AES_ECB;
	ta_ctx->current_hash_req = NULL;
	ta_ctx->current_cypher_req = req;
	ta_ctx->aes_encmode = SEP_AES_DECRYPT;
	ta_ctx->aes_opmode = SEP_AES_ECB;
	ta_ctx->init_opcode = SEP_AES_INIT_OPCODE;
	ta_ctx->block_opcode = SEP_AES_BLOCK_OPCODE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_aes_cbc_encrypt(struct ablkcipher_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ablkcipher_request_ctx(req);
	struct sep_system_ctx *sctx = crypto_ablkcipher_ctx(
		crypto_ablkcipher_reqtfm(req));

	pr_debug("sep - doing aes cbc encrypt\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	pr_debug("tfm is %p sctx is %p and ta_ctx is %p\n",
		crypto_ablkcipher_reqtfm(req), sctx, ta_ctx);

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = AES_CBC;
	ta_ctx->current_hash_req = NULL;
	ta_ctx->current_cypher_req = req;
	ta_ctx->aes_encmode = SEP_AES_ENCRYPT;
	ta_ctx->aes_opmode = SEP_AES_CBC;
	ta_ctx->init_opcode = SEP_AES_INIT_OPCODE;
	ta_ctx->block_opcode = SEP_AES_BLOCK_OPCODE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_aes_cbc_decrypt(struct ablkcipher_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ablkcipher_request_ctx(req);
	struct sep_system_ctx *sctx = crypto_ablkcipher_ctx(
		crypto_ablkcipher_reqtfm(req));

	pr_debug("sep - doing aes cbc decrypt\n");

	pr_debug("tfm is %p sctx is %p and ta_ctx is %p\n",
		crypto_ablkcipher_reqtfm(req), sctx, ta_ctx);

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = AES_CBC;
	ta_ctx->current_hash_req = NULL;
	ta_ctx->current_cypher_req = req;
	ta_ctx->aes_encmode = SEP_AES_DECRYPT;
	ta_ctx->aes_opmode = SEP_AES_CBC;
	ta_ctx->init_opcode = SEP_AES_INIT_OPCODE;
	ta_ctx->block_opcode = SEP_AES_BLOCK_OPCODE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_des_setkey(struct crypto_ablkcipher *tfm, const u8 *key,
	unsigned int keylen)
{
	struct sep_system_ctx *sctx = crypto_ablkcipher_ctx(tfm);
	struct crypto_tfm *ctfm = crypto_ablkcipher_tfm(tfm);
	u32 *flags  = &ctfm->crt_flags;

	pr_debug("sep des setkey\n");

	switch (keylen) {
	case DES_KEY_SIZE:
		sctx->des_nbr_keys = DES_KEY_1;
		break;
	case DES_KEY_SIZE * 2:
		sctx->des_nbr_keys = DES_KEY_2;
		break;
	case DES_KEY_SIZE * 3:
		sctx->des_nbr_keys = DES_KEY_3;
		break;
	default:
		pr_debug("invalid key size %x\n",
			keylen);
		return -EINVAL;
	}

	if ((*flags & CRYPTO_TFM_REQ_WEAK_KEY) &&
		(sep_weak_key(key, keylen))) {

		*flags |= CRYPTO_TFM_RES_WEAK_KEY;
		pr_debug("weak key\n");
		return -EINVAL;
	}

	memset(&sctx->key.des, 0, sizeof(struct sep_des_key));
	memcpy(&sctx->key.des.key1, key, keylen);
	sctx->keylen = keylen;
	/* Indicate to encrypt/decrypt function to send key to SEP */
	sctx->key_sent = 0;

	return 0;
}

static int sep_des_ebc_encrypt(struct ablkcipher_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ablkcipher_request_ctx(req);

	pr_debug("sep - doing des ecb encrypt\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = DES_ECB;
	ta_ctx->current_hash_req = NULL;
	ta_ctx->current_cypher_req = req;
	ta_ctx->des_encmode = SEP_DES_ENCRYPT;
	ta_ctx->des_opmode = SEP_DES_ECB;
	ta_ctx->init_opcode = SEP_DES_INIT_OPCODE;
	ta_ctx->block_opcode = SEP_DES_BLOCK_OPCODE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_des_ebc_decrypt(struct ablkcipher_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ablkcipher_request_ctx(req);

	pr_debug("sep - doing des ecb decrypt\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = DES_ECB;
	ta_ctx->current_hash_req = NULL;
	ta_ctx->current_cypher_req = req;
	ta_ctx->des_encmode = SEP_DES_DECRYPT;
	ta_ctx->des_opmode = SEP_DES_ECB;
	ta_ctx->init_opcode = SEP_DES_INIT_OPCODE;
	ta_ctx->block_opcode = SEP_DES_BLOCK_OPCODE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_des_cbc_encrypt(struct ablkcipher_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ablkcipher_request_ctx(req);

	pr_debug("sep - doing des cbc encrypt\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = DES_CBC;
	ta_ctx->current_hash_req = NULL;
	ta_ctx->current_cypher_req = req;
	ta_ctx->des_encmode = SEP_DES_ENCRYPT;
	ta_ctx->des_opmode = SEP_DES_CBC;
	ta_ctx->init_opcode = SEP_DES_INIT_OPCODE;
	ta_ctx->block_opcode = SEP_DES_BLOCK_OPCODE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static int sep_des_cbc_decrypt(struct ablkcipher_request *req)
{
	int error;
	int error1;
	struct this_task_ctx *ta_ctx = ablkcipher_request_ctx(req);

	pr_debug("sep - doing des ecb decrypt\n");

	/* Clear out task context */
	memset(ta_ctx, 0, sizeof(struct this_task_ctx));

	ta_ctx->sep_used = sep_dev;
	ta_ctx->current_request = DES_CBC;
	ta_ctx->current_hash_req = NULL;
	ta_ctx->current_cypher_req = req;
	ta_ctx->des_encmode = SEP_DES_DECRYPT;
	ta_ctx->des_opmode = SEP_DES_CBC;
	ta_ctx->init_opcode = SEP_DES_INIT_OPCODE;
	ta_ctx->block_opcode = SEP_DES_BLOCK_OPCODE;

	/* lock necessary so that only one entity touches the queues */
	spin_lock_irq(&queue_lock);
	error = crypto_enqueue_request(&sep_queue, &req->base);

	if ((error != 0) && (error != -EINPROGRESS))
		pr_debug(" sep - crypto enqueue failed: %x\n",
			error);
	error1 = sep_submit_work(ta_ctx->sep_used->workqueue,
		sep_dequeuer, (void *)&sep_queue);
	if (error1)
		pr_debug(" sep - workqueue submit failed: %x\n",
			error1);
	spin_unlock_irq(&queue_lock);
	/* We return result of crypto enqueue */
	return error;
}

static struct ahash_alg hash_algs[] = {
{
	.init		= sep_sha1_init,
	.update		= sep_sha1_update,
	.final		= sep_sha1_final,
	.digest		= sep_sha1_digest,
	.finup		= sep_sha1_finup,
	.halg		= {
		.digestsize	= SHA1_DIGEST_SIZE,
		.base	= {
		.cra_name		= "sha1",
		.cra_driver_name	= "sha1-sep",
		.cra_priority		= 100,
		.cra_flags		= CRYPTO_ALG_TYPE_AHASH |
						CRYPTO_ALG_ASYNC,
		.cra_blocksize		= SHA1_BLOCK_SIZE,
		.cra_ctxsize		= sizeof(struct sep_system_ctx),
		.cra_alignmask		= 0,
		.cra_module		= THIS_MODULE,
		.cra_init		= sep_hash_cra_init,
		.cra_exit		= sep_hash_cra_exit,
		}
	}
},
{
	.init		= sep_md5_init,
	.update		= sep_md5_update,
	.final		= sep_md5_final,
	.digest		= sep_md5_digest,
	.finup		= sep_md5_finup,
	.halg		= {
		.digestsize	= MD5_DIGEST_SIZE,
		.base	= {
		.cra_name		= "md5",
		.cra_driver_name	= "md5-sep",
		.cra_priority		= 100,
		.cra_flags		= CRYPTO_ALG_TYPE_AHASH |
						CRYPTO_ALG_ASYNC,
		.cra_blocksize		= SHA1_BLOCK_SIZE,
		.cra_ctxsize		= sizeof(struct sep_system_ctx),
		.cra_alignmask		= 0,
		.cra_module		= THIS_MODULE,
		.cra_init		= sep_hash_cra_init,
		.cra_exit		= sep_hash_cra_exit,
		}
	}
},
{
	.init		= sep_sha224_init,
	.update		= sep_sha224_update,
	.final		= sep_sha224_final,
	.digest		= sep_sha224_digest,
	.finup		= sep_sha224_finup,
	.halg		= {
		.digestsize	= SHA224_DIGEST_SIZE,
		.base	= {
		.cra_name		= "sha224",
		.cra_driver_name	= "sha224-sep",
		.cra_priority		= 100,
		.cra_flags		= CRYPTO_ALG_TYPE_AHASH |
						CRYPTO_ALG_ASYNC,
		.cra_blocksize		= SHA224_BLOCK_SIZE,
		.cra_ctxsize		= sizeof(struct sep_system_ctx),
		.cra_alignmask		= 0,
		.cra_module		= THIS_MODULE,
		.cra_init		= sep_hash_cra_init,
		.cra_exit		= sep_hash_cra_exit,
		}
	}
},
{
	.init		= sep_sha256_init,
	.update		= sep_sha256_update,
	.final		= sep_sha256_final,
	.digest		= sep_sha256_digest,
	.finup		= sep_sha256_finup,
	.halg		= {
		.digestsize	= SHA256_DIGEST_SIZE,
		.base	= {
		.cra_name		= "sha256",
		.cra_driver_name	= "sha256-sep",
		.cra_priority		= 100,
		.cra_flags		= CRYPTO_ALG_TYPE_AHASH |
						CRYPTO_ALG_ASYNC,
		.cra_blocksize		= SHA256_BLOCK_SIZE,
		.cra_ctxsize		= sizeof(struct sep_system_ctx),
		.cra_alignmask		= 0,
		.cra_module		= THIS_MODULE,
		.cra_init		= sep_hash_cra_init,
		.cra_exit		= sep_hash_cra_exit,
		}
	}
}
};

static struct crypto_alg crypto_algs[] = {
{
	.cra_name		= "ecb(aes)",
	.cra_driver_name	= "ecb-aes-sep",
	.cra_priority		= 100,
	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
	.cra_blocksize		= AES_BLOCK_SIZE,
	.cra_ctxsize		= sizeof(struct sep_system_ctx),
	.cra_alignmask		= 0,
	.cra_type		= &crypto_ablkcipher_type,
	.cra_module		= THIS_MODULE,
	.cra_init		= sep_crypto_init,
	.cra_exit		= sep_crypto_exit,
	.cra_u.ablkcipher = {
		.min_keysize	= AES_MIN_KEY_SIZE,
		.max_keysize	= AES_MAX_KEY_SIZE,
		.setkey		= sep_aes_setkey,
		.encrypt	= sep_aes_ecb_encrypt,
		.decrypt	= sep_aes_ecb_decrypt,
	}
},
{
	.cra_name		= "cbc(aes)",
	.cra_driver_name	= "cbc-aes-sep",
	.cra_priority		= 100,
	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
	.cra_blocksize		= AES_BLOCK_SIZE,
	.cra_ctxsize		= sizeof(struct sep_system_ctx),
	.cra_alignmask		= 0,
	.cra_type		= &crypto_ablkcipher_type,
	.cra_module		= THIS_MODULE,
	.cra_init		= sep_crypto_init,
	.cra_exit		= sep_crypto_exit,
	.cra_u.ablkcipher = {
		.min_keysize	= AES_MIN_KEY_SIZE,
		.max_keysize	= AES_MAX_KEY_SIZE,
		.setkey		= sep_aes_setkey,
		.encrypt	= sep_aes_cbc_encrypt,
		.ivsize		= AES_BLOCK_SIZE,
		.decrypt	= sep_aes_cbc_decrypt,
	}
},
{
	.cra_name		= "ebc(des)",
	.cra_driver_name	= "ebc-des-sep",
	.cra_priority		= 100,
	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
	.cra_blocksize		= DES_BLOCK_SIZE,
	.cra_ctxsize		= sizeof(struct sep_system_ctx),
	.cra_alignmask		= 0,
	.cra_type		= &crypto_ablkcipher_type,
	.cra_module		= THIS_MODULE,
	.cra_init		= sep_crypto_init,
	.cra_exit		= sep_crypto_exit,
	.cra_u.ablkcipher = {
		.min_keysize	= DES_KEY_SIZE,
		.max_keysize	= DES_KEY_SIZE,
		.setkey		= sep_des_setkey,
		.encrypt	= sep_des_ebc_encrypt,
		.decrypt	= sep_des_ebc_decrypt,
	}
},
{
	.cra_name		= "cbc(des)",
	.cra_driver_name	= "cbc-des-sep",
	.cra_priority		= 100,
	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
	.cra_blocksize		= DES_BLOCK_SIZE,
	.cra_ctxsize		= sizeof(struct sep_system_ctx),
	.cra_alignmask		= 0,
	.cra_type		= &crypto_ablkcipher_type,
	.cra_module		= THIS_MODULE,
	.cra_init		= sep_crypto_init,
	.cra_exit		= sep_crypto_exit,
	.cra_u.ablkcipher = {
		.min_keysize	= DES_KEY_SIZE,
		.max_keysize	= DES_KEY_SIZE,
		.setkey		= sep_des_setkey,
		.encrypt	= sep_des_cbc_encrypt,
		.ivsize		= DES_BLOCK_SIZE,
		.decrypt	= sep_des_cbc_decrypt,
	}
},
{
	.cra_name		= "ebc(des3-ede)",
	.cra_driver_name	= "ebc-des3-ede-sep",
	.cra_priority		= 100,
	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
	.cra_blocksize		= DES_BLOCK_SIZE,
	.cra_ctxsize		= sizeof(struct sep_system_ctx),
	.cra_alignmask		= 0,
	.cra_type		= &crypto_ablkcipher_type,
	.cra_module		= THIS_MODULE,
	.cra_init		= sep_crypto_init,
	.cra_exit		= sep_crypto_exit,
	.cra_u.ablkcipher = {
		.min_keysize	= DES3_EDE_KEY_SIZE,
		.max_keysize	= DES3_EDE_KEY_SIZE,
		.setkey		= sep_des_setkey,
		.encrypt	= sep_des_ebc_encrypt,
		.decrypt	= sep_des_ebc_decrypt,
	}
},
{
	.cra_name		= "cbc(des3-ede)",
	.cra_driver_name	= "cbc-des3--ede-sep",
	.cra_priority		= 100,
	.cra_flags		= CRYPTO_ALG_TYPE_ABLKCIPHER | CRYPTO_ALG_ASYNC,
	.cra_blocksize		= DES_BLOCK_SIZE,
	.cra_ctxsize		= sizeof(struct sep_system_ctx),
	.cra_alignmask		= 0,
	.cra_type		= &crypto_ablkcipher_type,
	.cra_module		= THIS_MODULE,
	.cra_init		= sep_crypto_init,
	.cra_exit		= sep_crypto_exit,
	.cra_u.ablkcipher = {
		.min_keysize	= DES3_EDE_KEY_SIZE,
		.max_keysize	= DES3_EDE_KEY_SIZE,
		.setkey		= sep_des_setkey,
		.encrypt	= sep_des_cbc_encrypt,
		.decrypt	= sep_des_cbc_decrypt,
	}
}
};

int sep_crypto_setup(void)
{
	int err, i, j, k;
	tasklet_init(&sep_dev->finish_tasklet, sep_finish,
		(unsigned long)sep_dev);

	crypto_init_queue(&sep_queue, SEP_QUEUE_LENGTH);

	sep_dev->workqueue = create_singlethread_workqueue(
		"sep_crypto_workqueue");
	if (!sep_dev->workqueue) {
		dev_warn(&sep_dev->pdev->dev, "cant create workqueue\n");
		return -ENOMEM;
	}

	spin_lock_init(&queue_lock);

	err = 0;
	for (i = 0; i < ARRAY_SIZE(hash_algs); i++) {
		err = crypto_register_ahash(&hash_algs[i]);
		if (err)
			goto err_algs;
	}

	err = 0;
	for (j = 0; j < ARRAY_SIZE(crypto_algs); j++) {
		err = crypto_register_alg(&crypto_algs[j]);
		if (err)
			goto err_crypto_algs;
	}

	return err;

err_algs:
	for (k = 0; k < i; k++)
		crypto_unregister_ahash(&hash_algs[k]);
	return err;

err_crypto_algs:
	for (k = 0; k < j; k++)
		crypto_unregister_alg(&crypto_algs[k]);
	goto err_algs;
}

void sep_crypto_takedown(void)
{

	int i;

	for (i = 0; i < ARRAY_SIZE(hash_algs); i++)
		crypto_unregister_ahash(&hash_algs[i]);
	for (i = 0; i < ARRAY_SIZE(crypto_algs); i++)
		crypto_unregister_alg(&crypto_algs[i]);

	tasklet_kill(&sep_dev->finish_tasklet);
}

#endif