]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | # |
2 | # Copyright(c) 2019 Intel Corporation | |
3 | # SPDX-License-Identifier: BSD-3-Clause-Clear | |
4 | # | |
5 | ||
6 | from ctypes import ( | |
7 | c_void_p, | |
8 | c_int, | |
9 | c_uint32, | |
10 | c_uint64, | |
11 | CFUNCTYPE, | |
12 | Structure, | |
13 | POINTER, | |
14 | byref, | |
15 | cast, | |
16 | ) | |
17 | from enum import IntEnum | |
18 | ||
19 | from ..ocf import OcfLib | |
20 | from .data import Data | |
9f95a23c TL |
21 | |
22 | ||
23 | class IoDir(IntEnum): | |
24 | READ = 0 | |
25 | WRITE = 1 | |
26 | ||
27 | ||
28 | class IoOps(Structure): | |
29 | pass | |
30 | ||
31 | ||
32 | class Io(Structure): | |
33 | START = CFUNCTYPE(None, c_void_p) | |
34 | HANDLE = CFUNCTYPE(None, c_void_p, c_void_p) | |
35 | END = CFUNCTYPE(None, c_void_p, c_int) | |
36 | ||
37 | _instances_ = {} | |
38 | _fields_ = [ | |
9f95a23c TL |
39 | ("_addr", c_uint64), |
40 | ("_flags", c_uint64), | |
41 | ("_bytes", c_uint32), | |
42 | ("_class", c_uint32), | |
43 | ("_dir", c_uint32), | |
44 | ("_io_queue", c_void_p), | |
45 | ("_start", START), | |
46 | ("_handle", HANDLE), | |
47 | ("_end", END), | |
48 | ("_priv1", c_void_p), | |
49 | ("_priv2", c_void_p), | |
50 | ] | |
51 | ||
52 | @classmethod | |
53 | def from_pointer(cls, ref): | |
54 | c = cls.from_address(ref) | |
55 | cls._instances_[ref] = c | |
56 | OcfLib.getInstance().ocf_io_set_cmpl_wrapper( | |
57 | byref(c), None, None, c.c_end | |
58 | ) | |
59 | return c | |
60 | ||
61 | @classmethod | |
62 | def get_instance(cls, ref): | |
63 | return cls._instances_[cast(ref, c_void_p).value] | |
64 | ||
65 | def del_object(self): | |
66 | del type(self)._instances_[cast(byref(self), c_void_p).value] | |
67 | ||
68 | def put(self): | |
69 | OcfLib.getInstance().ocf_io_put(byref(self)) | |
70 | ||
71 | def get(self): | |
72 | OcfLib.getInstance().ocf_io_get(byref(self)) | |
73 | ||
74 | @staticmethod | |
75 | @END | |
76 | def c_end(io, err): | |
77 | Io.get_instance(io).end(err) | |
78 | ||
79 | @staticmethod | |
80 | @START | |
81 | def c_start(io): | |
82 | Io.get_instance(io).start() | |
83 | ||
84 | @staticmethod | |
85 | @HANDLE | |
86 | def c_handle(io, opaque): | |
87 | Io.get_instance(io).handle(opaque) | |
88 | ||
89 | def end(self, err): | |
90 | try: | |
91 | self.callback(err) | |
f67539c2 | 92 | except: # noqa E722 |
9f95a23c TL |
93 | pass |
94 | ||
95 | self.put() | |
96 | self.del_object() | |
97 | ||
98 | def submit(self): | |
99 | return OcfLib.getInstance().ocf_core_submit_io_wrapper(byref(self)) | |
100 | ||
f67539c2 | 101 | def set_data(self, data: Data, offset: int = 0): |
9f95a23c | 102 | self.data = data |
f67539c2 | 103 | OcfLib.getInstance().ocf_io_set_data(byref(self), data, offset) |
9f95a23c TL |
104 | |
105 | ||
106 | IoOps.SET_DATA = CFUNCTYPE(c_int, POINTER(Io), c_void_p, c_uint32) | |
107 | IoOps.GET_DATA = CFUNCTYPE(c_void_p, POINTER(Io)) | |
108 | ||
109 | IoOps._fields_ = [("_set_data", IoOps.SET_DATA), ("_get_data", IoOps.GET_DATA)] | |
110 | ||
111 | lib = OcfLib.getInstance() | |
9f95a23c | 112 | lib.ocf_io_set_cmpl_wrapper.argtypes = [POINTER(Io), c_void_p, c_void_p, Io.END] |
9f95a23c TL |
113 | |
114 | lib.ocf_core_new_io_wrapper.argtypes = [c_void_p] | |
115 | lib.ocf_core_new_io_wrapper.restype = c_void_p | |
116 | ||
f67539c2 TL |
117 | lib.ocf_io_set_data.argtypes = [POINTER(Io), c_void_p, c_uint32] |
118 | lib.ocf_io_set_data.restype = c_int |