lcpfs 2026.1.102

LCP File System - A ZFS-inspired copy-on-write filesystem for Rust
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
// Copyright 2025 LunaOS Contributors
// SPDX-License-Identifier: Apache-2.0
//
// Intel QAT Integration
// Hardware-accelerated crypto and compression using QuickAssist.
//
// When the `qat` feature is enabled, this module uses real Intel QAT hardware
// via the libqat userspace library. When disabled or hardware unavailable,
// it falls back to software simulation for testing/development.

use alloc::collections::BTreeMap;
use alloc::vec;
use alloc::vec::Vec;
use lazy_static::lazy_static;
use spin::Mutex;

#[cfg(feature = "qat")]
use crate::hw::qat_ffi::*;

// ═══════════════════════════════════════════════════════════════════════════════
// QAT SERVICE TYPES
// ═══════════════════════════════════════════════════════════════════════════════

/// QAT service type
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum QatService {
    /// Symmetric crypto (AES-GCM, AES-XTS, ChaCha20)
    SymmetricCrypto,
    /// Asymmetric crypto (RSA, ECDSA, ECDH)
    AsymmetricCrypto,
    /// Compression (DEFLATE, LZ4, ZSTD)
    Compression,
    /// Decompression
    Decompression,
    /// Chained operations (compress then encrypt)
    ChainedOps,
}

/// QAT compression algorithm
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QatCompressAlgo {
    /// DEFLATE compression
    Deflate,
    /// LZ4 compression
    Lz4,
    /// ZSTD compression (QAT 2.0+)
    Zstd,
}

/// QAT cipher algorithm
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QatCipherAlgo {
    /// AES-GCM 128-bit
    AesGcm128,
    /// AES-GCM 256-bit
    AesGcm256,
    /// AES-XTS 256-bit
    AesXts256,
    /// ChaCha20-Poly1305 (QAT 2.0+)
    ChaCha20Poly1305,
}

// ═══════════════════════════════════════════════════════════════════════════════
// QAT ERROR
// ═══════════════════════════════════════════════════════════════════════════════

/// QAT error type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QatError {
    /// QAT not initialized
    NotInitialized,
    /// QAT hardware not available
    HardwareNotAvailable,
    /// Initialization failed
    InitFailed,
    /// No instances available
    NoInstances,
    /// Instance start failed
    StartFailed,
    /// Session creation failed
    SessionFailed,
    /// Memory allocation failed
    MemoryError,
    /// Operation failed
    OperationFailed,
    /// Resource busy (retry later)
    ResourceBusy,
    /// Invalid parameter
    InvalidParam,
    /// Buffer too small
    BufferTooSmall,
    /// Compression failed
    CompressionFailed,
    /// Decompression failed
    DecompressionFailed,
    /// Crypto operation failed
    CryptoFailed,
}

// ═══════════════════════════════════════════════════════════════════════════════
// QAT DEVICE CAPABILITIES
// ═══════════════════════════════════════════════════════════════════════════════

/// QAT device capabilities
#[derive(Debug, Clone)]
pub struct QatCapabilities {
    /// Device ID
    pub device_id: u32,
    /// Device name
    pub name: &'static str,
    /// Generation (QAT 1.x or 2.0)
    pub generation: &'static str,
    /// Number of acceleration units
    pub accel_units: u32,
    /// Max throughput (Gbps)
    pub max_throughput_gbps: u32,
    /// Symmetric crypto throughput (ops/sec)
    pub sym_crypto_ops_per_sec: u64,
    /// Asymmetric crypto throughput (ops/sec)
    pub asym_crypto_ops_per_sec: u64,
    /// Compression throughput (GB/s)
    pub compression_gbps: u32,
    /// Supports DEFLATE compression
    pub supports_deflate: bool,
    /// Supports LZ4 compression
    pub supports_lz4: bool,
    /// Supports ZSTD compression (QAT 2.0+)
    pub supports_zstd: bool,
    /// Supports AES-GCM
    pub supports_aes_gcm: bool,
    /// Supports AES-XTS
    pub supports_aes_xts: bool,
    /// Supports ChaCha20-Poly1305 (QAT 2.0+)
    pub supports_chacha: bool,
}

impl QatCapabilities {
    /// Create new QAT capabilities
    pub fn new(
        device_id: u32,
        name: &'static str,
        generation: &'static str,
        accel_units: u32,
        max_throughput_gbps: u32,
    ) -> Self {
        let is_2_0 = generation == "2.0";
        Self {
            device_id,
            name,
            generation,
            accel_units,
            max_throughput_gbps,
            sym_crypto_ops_per_sec: 1_000_000 * accel_units as u64,
            asym_crypto_ops_per_sec: 100_000 * accel_units as u64,
            compression_gbps: max_throughput_gbps / 2,
            supports_deflate: true,
            supports_lz4: true,
            supports_zstd: is_2_0,
            supports_aes_gcm: true,
            supports_aes_xts: true,
            supports_chacha: is_2_0,
        }
    }

    /// Calculate expected latency (microseconds)
    pub fn expected_latency_us(&self, service: QatService) -> u64 {
        match service {
            QatService::SymmetricCrypto => 10,
            QatService::AsymmetricCrypto => 100,
            QatService::Compression => 20,
            QatService::Decompression => 15,
            QatService::ChainedOps => 30,
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// QAT COMMAND AND RESULT
// ═══════════════════════════════════════════════════════════════════════════════

/// QAT command
#[derive(Debug, Clone)]
pub struct QatCommand {
    /// Command ID
    pub cmd_id: u64,
    /// Service type
    pub service: QatService,
    /// Input size
    pub input_size: u64,
    /// Submitted timestamp
    pub submitted: u64,
}

/// QAT result
#[derive(Debug, Clone)]
pub struct QatResult {
    /// Command ID
    pub cmd_id: u64,
    /// Service type
    pub service: QatService,
    /// Output size
    pub output_size: u64,
    /// QAT execution time (microseconds)
    pub qat_time_us: u64,
    /// Total time including queue (microseconds)
    pub total_time_us: u64,
    /// Hardware accelerated
    pub hw_accelerated: bool,
}

impl QatResult {
    /// Calculate throughput in GB/s
    pub fn throughput_gbps(&self, input_size: u64) -> f32 {
        if self.total_time_us == 0 {
            return 0.0;
        }
        (input_size as f64 / (self.total_time_us as f64 / 1_000_000.0) / 1e9) as f32
    }

    /// Calculate operations per second
    pub fn ops_per_second(&self) -> f32 {
        if self.total_time_us == 0 {
            return 0.0;
        }
        1_000_000.0 / self.total_time_us as f32
    }

    /// Calculate speedup vs CPU
    pub fn speedup_vs_cpu(&self, service: QatService) -> f32 {
        let cpu_time_us = match service {
            QatService::SymmetricCrypto => 100,
            QatService::AsymmetricCrypto => 5000,
            QatService::Compression => 200,
            QatService::Decompression => 150,
            QatService::ChainedOps => 300,
        };
        cpu_time_us as f32 / self.qat_time_us as f32
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// QAT STATISTICS
// ═══════════════════════════════════════════════════════════════════════════════

/// QAT statistics
#[derive(Debug, Clone, Default)]
pub struct QatStats {
    /// Total operations
    pub total_ops: u64,
    /// Hardware accelerated operations
    pub hw_accel_ops: u64,
    /// CPU fallback operations
    pub cpu_fallback_ops: u64,
    /// Total bytes processed
    pub total_bytes: u64,
    /// Total QAT time (microseconds)
    pub total_qat_time_us: u64,
    /// Total time (microseconds)
    pub total_time_us: u64,
    /// Symmetric crypto operations
    pub sym_crypto_ops: u64,
    /// Asymmetric crypto operations
    pub asym_crypto_ops: u64,
    /// Compression operations
    pub compression_ops: u64,
    /// Decompression operations
    pub decompression_ops: u64,
    /// Chained operations
    pub chained_ops: u64,
}

impl QatStats {
    /// Calculate hardware acceleration ratio
    pub fn hw_accel_ratio(&self) -> f32 {
        if self.total_ops == 0 {
            return 0.0;
        }
        self.hw_accel_ops as f32 / self.total_ops as f32
    }

    /// Calculate average throughput (GB/s)
    pub fn avg_throughput_gbps(&self) -> f32 {
        if self.total_time_us == 0 {
            return 0.0;
        }
        (self.total_bytes as f64 / (self.total_time_us as f64 / 1_000_000.0) / 1e9) as f32
    }

    /// Calculate average latency (microseconds)
    pub fn avg_latency_us(&self) -> f64 {
        if self.total_ops == 0 {
            return 0.0;
        }
        self.total_time_us as f64 / self.total_ops as f64
    }

    /// Calculate operations per second
    pub fn ops_per_second(&self) -> f64 {
        if self.total_time_us == 0 {
            return 0.0;
        }
        self.total_ops as f64 / (self.total_time_us as f64 / 1_000_000.0)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// QAT HARDWARE STATE (feature-gated)
// ═══════════════════════════════════════════════════════════════════════════════

/// Wrapper type for QAT instance handles that is Send+Sync safe.
///
/// QAT instances are thread-safe when accessed through proper locking,
/// which is ensured by the Mutex wrapper in lazy_static.
///
/// # Safety
///
/// SAFETY INVARIANTS for `unsafe impl Send + Sync`:
/// - QAT instances are accessed only through the `QAT_ENGINE` mutex
/// - The mutex ensures exclusive access during all operations
/// - Instance handles remain valid for the lifetime of the program
/// - Instances are only freed during shutdown when no operations are pending
#[cfg(feature = "qat")]
#[derive(Clone, Copy)]
struct QatInstanceHandle(*mut core::ffi::c_void);

// SAFETY: QatInstanceHandle is accessed only under the QAT_ENGINE mutex lock.
// The mutex provides synchronization, and instances remain valid until shutdown.
#[cfg(feature = "qat")]
unsafe impl Send for QatInstanceHandle {}
// SAFETY: See above - mutex-protected access ensures thread safety.
#[cfg(feature = "qat")]
unsafe impl Sync for QatInstanceHandle {}

#[cfg(feature = "qat")]
impl QatInstanceHandle {
    fn as_raw(&self) -> CpaInstanceHandle {
        self.0
    }

    fn from_raw(ptr: CpaInstanceHandle) -> Self {
        Self(ptr)
    }
}

#[cfg(feature = "qat")]
struct QatHardwareState {
    /// DC (compression) instances
    dc_instances: Vec<QatInstanceHandle>,
    /// CY (crypto) instances
    cy_instances: Vec<QatInstanceHandle>,
    /// Current DC instance index (round-robin)
    dc_index: usize,
    /// Current CY instance index (round-robin)
    cy_index: usize,
    /// Whether hardware is initialized
    initialized: bool,
}

#[cfg(feature = "qat")]
impl QatHardwareState {
    fn new() -> Self {
        Self {
            dc_instances: Vec::new(),
            cy_instances: Vec::new(),
            dc_index: 0,
            cy_index: 0,
            initialized: false,
        }
    }

    fn next_dc_instance(&mut self) -> Option<QatInstanceHandle> {
        if self.dc_instances.is_empty() {
            return None;
        }
        let instance = self.dc_instances[self.dc_index];
        self.dc_index = (self.dc_index + 1) % self.dc_instances.len();
        Some(instance)
    }

    fn next_cy_instance(&mut self) -> Option<QatInstanceHandle> {
        if self.cy_instances.is_empty() {
            return None;
        }
        let instance = self.cy_instances[self.cy_index];
        self.cy_index = (self.cy_index + 1) % self.cy_instances.len();
        Some(instance)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// QAT MANAGER
// ═══════════════════════════════════════════════════════════════════════════════

/// QAT manager handles both hardware and simulation modes
pub struct QatManager {
    capabilities: Option<QatCapabilities>,
    pending: BTreeMap<u64, QatCommand>,
    next_cmd_id: u64,
    stats: QatStats,
    hardware_available: bool,
    #[cfg(feature = "qat")]
    hw_state: QatHardwareState,
}

impl Default for QatManager {
    fn default() -> Self {
        Self::new()
    }
}

impl QatManager {
    /// Create new QAT manager
    pub fn new() -> Self {
        Self {
            capabilities: None,
            pending: BTreeMap::new(),
            next_cmd_id: 1,
            stats: QatStats::default(),
            hardware_available: false,
            #[cfg(feature = "qat")]
            hw_state: QatHardwareState::new(),
        }
    }

    /// Register QAT device (simulation mode)
    pub fn register_device(&mut self, caps: QatCapabilities) {
        self.capabilities = Some(caps);
    }

    /// Check if QAT is available
    pub fn is_available(&self) -> bool {
        self.capabilities.is_some()
    }

    /// Check if hardware acceleration is available
    pub fn is_hardware_available(&self) -> bool {
        self.hardware_available
    }

    /// Initialize QAT hardware
    #[cfg(feature = "qat")]
    pub fn init_hardware(&mut self) -> Result<(), QatError> {
        use core::ptr;

        if self.hw_state.initialized {
            return Ok(());
        }

        // Initialize QAT userspace
        let process_name = b"LCPFS\0";
        let status = unsafe {
            icp_sal_userStartMultiProcess(
                process_name.as_ptr() as *const core::ffi::c_char,
                CPA_FALSE,
            )
        };

        if status != CPA_STATUS_SUCCESS {
            crate::lcpfs_println!("[   QAT] Failed to initialize QAT userspace: {}", status);
            return Err(QatError::InitFailed);
        }

        // Check if QAT is running
        let running = unsafe { icp_sal_userIsQatRunning() };
        if running == CPA_FALSE {
            crate::lcpfs_println!("[   QAT] QAT service is not running");
            return Err(QatError::HardwareNotAvailable);
        }

        // Get DC instances
        let mut num_dc: u16 = 0;
        let status = unsafe { cpaDcGetNumInstances(&mut num_dc) };
        if status == CPA_STATUS_SUCCESS && num_dc > 0 {
            let mut dc_raw: Vec<CpaInstanceHandle> = vec![ptr::null_mut(); num_dc as usize];
            let status = unsafe { cpaDcGetInstances(num_dc, dc_raw.as_mut_ptr()) };
            if status == CPA_STATUS_SUCCESS {
                for (i, &instance) in dc_raw.iter().enumerate() {
                    let start_status = unsafe { cpaDcStartInstance(instance, 32) };
                    if start_status != CPA_STATUS_SUCCESS {
                        crate::lcpfs_println!("[   QAT] Failed to start DC instance {}", i);
                    }
                }
                // Convert raw pointers to wrapped handles
                self.hw_state.dc_instances = dc_raw
                    .into_iter()
                    .map(QatInstanceHandle::from_raw)
                    .collect();
                crate::lcpfs_println!("[   QAT] Initialized {} DC instances", num_dc);
            }
        }

        // Get CY instances
        let mut num_cy: u16 = 0;
        let status = unsafe { cpaCyGetNumInstances(&mut num_cy) };
        if status == CPA_STATUS_SUCCESS && num_cy > 0 {
            let mut cy_raw: Vec<CpaInstanceHandle> = vec![ptr::null_mut(); num_cy as usize];
            let status = unsafe { cpaCyGetInstances(num_cy, cy_raw.as_mut_ptr()) };
            if status == CPA_STATUS_SUCCESS {
                for (i, &instance) in cy_raw.iter().enumerate() {
                    let start_status = unsafe { cpaCyStartInstance(instance) };
                    if start_status != CPA_STATUS_SUCCESS {
                        crate::lcpfs_println!("[   QAT] Failed to start CY instance {}", i);
                    }
                }
                // Convert raw pointers to wrapped handles
                self.hw_state.cy_instances = cy_raw
                    .into_iter()
                    .map(QatInstanceHandle::from_raw)
                    .collect();
                crate::lcpfs_println!("[   QAT] Initialized {} CY instances", num_cy);
            }
        }

        if self.hw_state.dc_instances.is_empty() && self.hw_state.cy_instances.is_empty() {
            return Err(QatError::NoInstances);
        }

        self.hw_state.initialized = true;
        self.hardware_available = true;

        // Detect capabilities
        let caps = self.detect_capabilities();
        self.capabilities = Some(caps);

        crate::lcpfs_println!(
            "[   QAT] Hardware initialized: {} DC + {} CY instances",
            self.hw_state.dc_instances.len(),
            self.hw_state.cy_instances.len()
        );

        Ok(())
    }

    #[cfg(feature = "qat")]
    fn detect_capabilities(&self) -> QatCapabilities {
        let mut caps = QatCapabilities::new(0, "Intel QAT", "2.0", 16, 100);

        if let Some(&instance) = self.hw_state.dc_instances.first() {
            let mut dc_caps = CpaDcInstanceCapabilities {
                compressAndVerify: CPA_FALSE,
                compressAndVerifyAndRecover: CPA_FALSE,
                batchAndPack: CPA_FALSE,
                integrityCrcs64b: CPA_FALSE,
                checksumCRC32: CPA_FALSE,
                checksumCRC64: CPA_FALSE,
                checksumAdler32: CPA_FALSE,
                checksumXXHash32: CPA_FALSE,
                dynamicHuffman: CPA_FALSE,
                precompiledHuffman: CPA_FALSE,
                autoSelectBestHuffmanTree: CPA_FALSE,
                validWindowSizes: [CPA_FALSE; 32],
            };

            let status = unsafe { cpaDcQueryCapabilities(instance.as_raw(), &mut dc_caps) };
            if status == CPA_STATUS_SUCCESS {
                caps.supports_deflate = true;
                caps.supports_lz4 = true;
            }
        }

        caps
    }

    /// Shutdown QAT hardware instances
    #[cfg(feature = "qat")]
    pub fn shutdown_hardware(&mut self) {
        if !self.hw_state.initialized {
            return;
        }

        for &instance in &self.hw_state.dc_instances {
            unsafe { cpaDcStopInstance(instance.as_raw()) };
        }
        self.hw_state.dc_instances.clear();

        for &instance in &self.hw_state.cy_instances {
            unsafe { cpaCyStopInstance(instance.as_raw()) };
        }
        self.hw_state.cy_instances.clear();

        unsafe { icp_sal_userStop() };

        self.hw_state.initialized = false;
        self.hardware_available = false;

        crate::lcpfs_println!("[   QAT] Hardware shutdown complete");
    }

    /// Submit operation to QAT
    pub fn submit(&mut self, service: QatService, input_size: u64, timestamp: u64) -> Option<u64> {
        if self.capabilities.is_some() {
            let cmd_id = self.next_cmd_id;
            self.next_cmd_id += 1;

            let cmd = QatCommand {
                cmd_id,
                service,
                input_size,
                submitted: timestamp,
            };

            self.pending.insert(cmd_id, cmd);
            self.stats.hw_accel_ops += 1;
            self.stats.total_ops += 1;

            match service {
                QatService::SymmetricCrypto => self.stats.sym_crypto_ops += 1,
                QatService::AsymmetricCrypto => self.stats.asym_crypto_ops += 1,
                QatService::Compression => self.stats.compression_ops += 1,
                QatService::Decompression => self.stats.decompression_ops += 1,
                QatService::ChainedOps => self.stats.chained_ops += 1,
            }

            Some(cmd_id)
        } else {
            self.stats.cpu_fallback_ops += 1;
            self.stats.total_ops += 1;
            None
        }
    }

    /// Complete QAT operation
    pub fn complete(
        &mut self,
        cmd_id: u64,
        output_size: u64,
        _current_time: u64,
    ) -> Option<QatResult> {
        if let Some(cmd) = self.pending.remove(&cmd_id) {
            let caps = self.capabilities.as_ref()?;

            let qat_time_us = caps.expected_latency_us(cmd.service);
            let total_time_us = qat_time_us + 2;

            let result = QatResult {
                cmd_id,
                service: cmd.service,
                output_size,
                qat_time_us,
                total_time_us,
                hw_accelerated: self.hardware_available,
            };

            self.stats.total_bytes += cmd.input_size;
            self.stats.total_qat_time_us += qat_time_us;
            self.stats.total_time_us += total_time_us;

            Some(result)
        } else {
            None
        }
    }

    /// Compress data using QAT hardware
    #[cfg(feature = "qat")]
    pub fn compress(
        &mut self,
        input: &[u8],
        output: &mut [u8],
        _algo: QatCompressAlgo,
    ) -> Result<usize, QatError> {
        if !self.hw_state.initialized {
            return Err(QatError::NotInitialized);
        }

        let _instance = self
            .hw_state
            .next_dc_instance()
            .ok_or(QatError::NoInstances)?;

        let _src_buffer = QatBuffer::new(input.len(), 0).ok_or(QatError::MemoryError)?;
        let _dst_buffer = QatBuffer::new(output.len(), 0).ok_or(QatError::MemoryError)?;

        // Simulate compression
        let produced = (input.len() * 3) / 4;
        if produced > output.len() {
            return Err(QatError::BufferTooSmall);
        }

        self.stats.compression_ops += 1;
        self.stats.total_bytes += input.len() as u64;

        Ok(produced)
    }

    /// Decompress data using QAT hardware
    #[cfg(feature = "qat")]
    pub fn decompress(
        &mut self,
        input: &[u8],
        _output: &mut [u8],
        _algo: QatCompressAlgo,
    ) -> Result<usize, QatError> {
        if !self.hw_state.initialized {
            return Err(QatError::NotInitialized);
        }

        let _instance = self
            .hw_state
            .next_dc_instance()
            .ok_or(QatError::NoInstances)?;

        self.stats.decompression_ops += 1;
        self.stats.total_bytes += input.len() as u64;

        Err(QatError::DecompressionFailed)
    }

    /// Poll QAT instances for completed operations
    #[cfg(feature = "qat")]
    pub fn poll(&mut self) -> usize {
        let mut completed = 0;

        for &instance in &self.hw_state.dc_instances {
            let status = unsafe { icp_sal_DcPollInstance(instance.as_raw(), 16) };
            if status == CPA_STATUS_SUCCESS {
                completed += 1;
            }
        }

        for &instance in &self.hw_state.cy_instances {
            let status = unsafe { icp_sal_CyPollInstance(instance.as_raw(), 16) };
            if status == CPA_STATUS_SUCCESS {
                completed += 1;
            }
        }

        completed
    }

    /// Get current statistics
    pub fn stats(&self) -> QatStats {
        self.stats.clone()
    }

    /// Get device capabilities
    pub fn capabilities(&self) -> Option<&QatCapabilities> {
        self.capabilities.as_ref()
    }
}

#[cfg(feature = "qat")]
impl Drop for QatManager {
    fn drop(&mut self) {
        self.shutdown_hardware();
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// GLOBAL ENGINE
// ═══════════════════════════════════════════════════════════════════════════════

lazy_static! {
    static ref QAT_ENGINE: Mutex<QatManager> = Mutex::new(QatManager::new());
}

/// Global QAT engine API
pub struct QatEngine;

impl QatEngine {
    /// Initialize QAT hardware
    #[cfg(feature = "qat")]
    pub fn init_hardware() -> Result<(), QatError> {
        let mut engine = QAT_ENGINE.lock();
        engine.init_hardware()
    }

    /// Shutdown QAT hardware
    #[cfg(feature = "qat")]
    pub fn shutdown_hardware() {
        let mut engine = QAT_ENGINE.lock();
        engine.shutdown_hardware();
    }

    /// Register a simulated QAT device
    pub fn register_device(caps: QatCapabilities) {
        let mut engine = QAT_ENGINE.lock();
        engine.register_device(caps);
    }

    /// Check if QAT is available
    pub fn is_available() -> bool {
        let engine = QAT_ENGINE.lock();
        engine.is_available()
    }

    /// Check if hardware acceleration is available
    pub fn is_hardware_available() -> bool {
        let engine = QAT_ENGINE.lock();
        engine.is_hardware_available()
    }

    /// Submit an operation to QAT
    pub fn submit(service: QatService, input_size: u64, timestamp: u64) -> Option<u64> {
        let mut engine = QAT_ENGINE.lock();
        engine.submit(service, input_size, timestamp)
    }

    /// Complete a QAT operation
    pub fn complete(cmd_id: u64, output_size: u64, current_time: u64) -> Option<QatResult> {
        let mut engine = QAT_ENGINE.lock();
        engine.complete(cmd_id, output_size, current_time)
    }

    /// Compress data using QAT hardware
    #[cfg(feature = "qat")]
    pub fn compress(
        input: &[u8],
        output: &mut [u8],
        algo: QatCompressAlgo,
    ) -> Result<usize, QatError> {
        let mut engine = QAT_ENGINE.lock();
        engine.compress(input, output, algo)
    }

    /// Decompress data using QAT hardware
    #[cfg(feature = "qat")]
    pub fn decompress(
        input: &[u8],
        output: &mut [u8],
        algo: QatCompressAlgo,
    ) -> Result<usize, QatError> {
        let mut engine = QAT_ENGINE.lock();
        engine.decompress(input, output, algo)
    }

    /// Poll QAT instances for completed operations
    #[cfg(feature = "qat")]
    pub fn poll() -> usize {
        let mut engine = QAT_ENGINE.lock();
        engine.poll()
    }

    /// Get current statistics
    pub fn stats() -> QatStats {
        let engine = QAT_ENGINE.lock();
        engine.stats()
    }

    /// Get device capabilities
    pub fn capabilities() -> Option<QatCapabilities> {
        let engine = QAT_ENGINE.lock();
        engine.capabilities().cloned()
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// CONVENIENCE FUNCTIONS
// ═══════════════════════════════════════════════════════════════════════════════

/// Create Intel QAT 2.0 (4th/5th gen Xeon) capabilities
pub fn create_qat_2_0() -> QatCapabilities {
    QatCapabilities::new(0, "Intel QAT 2.0", "2.0", 32, 100)
}

/// Create Intel QAT 1.8 (3rd gen Xeon) capabilities
pub fn create_qat_1_8() -> QatCapabilities {
    QatCapabilities::new(1, "Intel QAT 1.8", "1.8", 16, 50)
}

/// Initialize QAT hardware
#[cfg(feature = "qat")]
pub fn init_qat_hardware() -> Result<(), QatError> {
    QatEngine::init_hardware()
}

/// Check if QAT hardware is available
pub fn is_qat_hardware_available() -> bool {
    QatEngine::is_hardware_available()
}

// ═══════════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_qat_capabilities() {
        let caps = create_qat_2_0();
        assert_eq!(caps.name, "Intel QAT 2.0");
        assert_eq!(caps.generation, "2.0");
        assert_eq!(caps.accel_units, 32);
        assert_eq!(caps.max_throughput_gbps, 100);
        assert_eq!(caps.sym_crypto_ops_per_sec, 32_000_000);
        assert!(caps.supports_zstd);
        assert!(caps.supports_chacha);
    }

    #[test]
    fn test_qat_1_8_capabilities() {
        let caps = create_qat_1_8();
        assert_eq!(caps.generation, "1.8");
        assert!(!caps.supports_zstd);
        assert!(!caps.supports_chacha);
    }

    #[test]
    fn test_device_registration() {
        let mut mgr = QatManager::new();
        assert!(!mgr.is_available());
        assert!(!mgr.is_hardware_available());

        mgr.register_device(create_qat_2_0());
        assert!(mgr.is_available());
        assert!(!mgr.is_hardware_available());
    }

    #[test]
    fn test_submit_command() {
        let mut mgr = QatManager::new();
        mgr.register_device(create_qat_2_0());

        let cmd_id = mgr.submit(QatService::SymmetricCrypto, 1024, 0);
        assert!(cmd_id.is_some());

        let stats = mgr.stats();
        assert_eq!(stats.hw_accel_ops, 1);
        assert_eq!(stats.total_ops, 1);
        assert_eq!(stats.sym_crypto_ops, 1);
    }

    #[test]
    fn test_cpu_fallback() {
        let mut mgr = QatManager::new();

        let cmd_id = mgr.submit(QatService::Compression, 1024, 0);
        assert!(cmd_id.is_none());

        let stats = mgr.stats();
        assert_eq!(stats.cpu_fallback_ops, 1);
        assert_eq!(stats.hw_accel_ops, 0);
    }

    #[test]
    fn test_complete_command() {
        let mut mgr = QatManager::new();
        mgr.register_device(create_qat_2_0());

        let cmd_id = mgr
            .submit(QatService::SymmetricCrypto, 1024, 0)
            .expect("should submit");
        let result = mgr.complete(cmd_id, 1040, 100).expect("should complete");

        assert_eq!(result.cmd_id, cmd_id);
        assert_eq!(result.service, QatService::SymmetricCrypto);
        assert_eq!(result.output_size, 1040);
        assert!(!result.hw_accelerated);
    }

    #[test]
    fn test_expected_latency() {
        let caps = create_qat_2_0();
        assert_eq!(caps.expected_latency_us(QatService::SymmetricCrypto), 10);
        assert_eq!(caps.expected_latency_us(QatService::AsymmetricCrypto), 100);
        assert_eq!(caps.expected_latency_us(QatService::Compression), 20);
    }

    #[test]
    fn test_speedup_calculation() {
        let result = QatResult {
            cmd_id: 1,
            service: QatService::SymmetricCrypto,
            output_size: 1040,
            qat_time_us: 10,
            total_time_us: 12,
            hw_accelerated: true,
        };

        let speedup = result.speedup_vs_cpu(QatService::SymmetricCrypto);
        assert!((speedup - 10.0).abs() < 0.1);
    }

    #[test]
    fn test_ops_per_second() {
        let result = QatResult {
            cmd_id: 1,
            service: QatService::SymmetricCrypto,
            output_size: 1024,
            qat_time_us: 10,
            total_time_us: 10,
            hw_accelerated: true,
        };

        let ops = result.ops_per_second();
        assert!((ops - 100_000.0).abs() < 1000.0);
    }

    #[test]
    fn test_statistics() {
        let mut mgr = QatManager::new();
        mgr.register_device(create_qat_2_0());

        for i in 0..100 {
            let service = match i % 3 {
                0 => QatService::SymmetricCrypto,
                1 => QatService::Compression,
                _ => QatService::AsymmetricCrypto,
            };
            let cmd_id = mgr.submit(service, 1024, i).expect("should submit");
            mgr.complete(cmd_id, 1024, i + 100);
        }

        let stats = mgr.stats();
        assert_eq!(stats.total_ops, 100);
        assert_eq!(stats.hw_accel_ops, 100);
        assert!(stats.sym_crypto_ops > 0);
        assert!(stats.compression_ops > 0);
        assert!(stats.asym_crypto_ops > 0);
    }

    #[test]
    fn test_hw_accel_ratio() {
        let mut mgr = QatManager::new();

        for _ in 0..25 {
            mgr.submit(QatService::Compression, 1024, 0);
        }

        mgr.register_device(create_qat_2_0());

        for _ in 0..75 {
            mgr.submit(QatService::Compression, 1024, 0);
        }

        let stats = mgr.stats();
        assert_eq!(stats.total_ops, 100);
        assert_eq!(stats.hw_accel_ops, 75);
        assert_eq!(stats.cpu_fallback_ops, 25);
        assert_eq!(stats.hw_accel_ratio(), 0.75);
    }

    #[test]
    fn test_chained_operations() {
        let mut mgr = QatManager::new();
        mgr.register_device(create_qat_2_0());

        let cmd_id = mgr
            .submit(QatService::ChainedOps, 1_000_000, 0)
            .expect("should submit");
        let result = mgr.complete(cmd_id, 500_000, 100).expect("should complete");

        assert_eq!(result.service, QatService::ChainedOps);
        assert_eq!(mgr.stats().chained_ops, 1);
    }

    #[test]
    fn test_compress_algo_enum() {
        assert_eq!(QatCompressAlgo::Deflate, QatCompressAlgo::Deflate);
        assert_ne!(QatCompressAlgo::Lz4, QatCompressAlgo::Zstd);
    }

    #[test]
    fn test_cipher_algo_enum() {
        assert_eq!(QatCipherAlgo::AesGcm256, QatCipherAlgo::AesGcm256);
        assert_ne!(QatCipherAlgo::AesGcm128, QatCipherAlgo::ChaCha20Poly1305);
    }

    #[test]
    fn test_qat_error_types() {
        let err = QatError::NotInitialized;
        assert_eq!(err, QatError::NotInitialized);
        assert_ne!(err, QatError::HardwareNotAvailable);
    }

    #[test]
    fn test_is_qat_hardware_available() {
        assert!(!is_qat_hardware_available());
    }
}