This source file includes following definitions.
- value_read
- ib_pack
- value_write
- ib_unpack
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 #include <linux/export.h>
35 #include <linux/string.h>
36
37 #include <rdma/ib_pack.h>
38
39 static u64 value_read(int offset, int size, void *structure)
40 {
41 switch (size) {
42 case 1: return *(u8 *) (structure + offset);
43 case 2: return be16_to_cpup((__be16 *) (structure + offset));
44 case 4: return be32_to_cpup((__be32 *) (structure + offset));
45 case 8: return be64_to_cpup((__be64 *) (structure + offset));
46 default:
47 pr_warn("Field size %d bits not handled\n", size * 8);
48 return 0;
49 }
50 }
51
52
53
54
55
56
57
58
59
60
61
62 void ib_pack(const struct ib_field *desc,
63 int desc_len,
64 void *structure,
65 void *buf)
66 {
67 int i;
68
69 for (i = 0; i < desc_len; ++i) {
70 if (desc[i].size_bits <= 32) {
71 int shift;
72 u32 val;
73 __be32 mask;
74 __be32 *addr;
75
76 shift = 32 - desc[i].offset_bits - desc[i].size_bits;
77 if (desc[i].struct_size_bytes)
78 val = value_read(desc[i].struct_offset_bytes,
79 desc[i].struct_size_bytes,
80 structure) << shift;
81 else
82 val = 0;
83
84 mask = cpu_to_be32(((1ull << desc[i].size_bits) - 1) << shift);
85 addr = (__be32 *) buf + desc[i].offset_words;
86 *addr = (*addr & ~mask) | (cpu_to_be32(val) & mask);
87 } else if (desc[i].size_bits <= 64) {
88 int shift;
89 u64 val;
90 __be64 mask;
91 __be64 *addr;
92
93 shift = 64 - desc[i].offset_bits - desc[i].size_bits;
94 if (desc[i].struct_size_bytes)
95 val = value_read(desc[i].struct_offset_bytes,
96 desc[i].struct_size_bytes,
97 structure) << shift;
98 else
99 val = 0;
100
101 mask = cpu_to_be64((~0ull >> (64 - desc[i].size_bits)) << shift);
102 addr = (__be64 *) ((__be32 *) buf + desc[i].offset_words);
103 *addr = (*addr & ~mask) | (cpu_to_be64(val) & mask);
104 } else {
105 if (desc[i].offset_bits % 8 ||
106 desc[i].size_bits % 8) {
107 pr_warn("Structure field %s of size %d bits is not byte-aligned\n",
108 desc[i].field_name, desc[i].size_bits);
109 }
110
111 if (desc[i].struct_size_bytes)
112 memcpy(buf + desc[i].offset_words * 4 +
113 desc[i].offset_bits / 8,
114 structure + desc[i].struct_offset_bytes,
115 desc[i].size_bits / 8);
116 else
117 memset(buf + desc[i].offset_words * 4 +
118 desc[i].offset_bits / 8,
119 0,
120 desc[i].size_bits / 8);
121 }
122 }
123 }
124 EXPORT_SYMBOL(ib_pack);
125
126 static void value_write(int offset, int size, u64 val, void *structure)
127 {
128 switch (size * 8) {
129 case 8: *( u8 *) (structure + offset) = val; break;
130 case 16: *(__be16 *) (structure + offset) = cpu_to_be16(val); break;
131 case 32: *(__be32 *) (structure + offset) = cpu_to_be32(val); break;
132 case 64: *(__be64 *) (structure + offset) = cpu_to_be64(val); break;
133 default:
134 pr_warn("Field size %d bits not handled\n", size * 8);
135 }
136 }
137
138
139
140
141
142
143
144
145
146
147
148 void ib_unpack(const struct ib_field *desc,
149 int desc_len,
150 void *buf,
151 void *structure)
152 {
153 int i;
154
155 for (i = 0; i < desc_len; ++i) {
156 if (!desc[i].struct_size_bytes)
157 continue;
158
159 if (desc[i].size_bits <= 32) {
160 int shift;
161 u32 val;
162 u32 mask;
163 __be32 *addr;
164
165 shift = 32 - desc[i].offset_bits - desc[i].size_bits;
166 mask = ((1ull << desc[i].size_bits) - 1) << shift;
167 addr = (__be32 *) buf + desc[i].offset_words;
168 val = (be32_to_cpup(addr) & mask) >> shift;
169 value_write(desc[i].struct_offset_bytes,
170 desc[i].struct_size_bytes,
171 val,
172 structure);
173 } else if (desc[i].size_bits <= 64) {
174 int shift;
175 u64 val;
176 u64 mask;
177 __be64 *addr;
178
179 shift = 64 - desc[i].offset_bits - desc[i].size_bits;
180 mask = (~0ull >> (64 - desc[i].size_bits)) << shift;
181 addr = (__be64 *) buf + desc[i].offset_words;
182 val = (be64_to_cpup(addr) & mask) >> shift;
183 value_write(desc[i].struct_offset_bytes,
184 desc[i].struct_size_bytes,
185 val,
186 structure);
187 } else {
188 if (desc[i].offset_bits % 8 ||
189 desc[i].size_bits % 8) {
190 pr_warn("Structure field %s of size %d bits is not byte-aligned\n",
191 desc[i].field_name, desc[i].size_bits);
192 }
193
194 memcpy(structure + desc[i].struct_offset_bytes,
195 buf + desc[i].offset_words * 4 +
196 desc[i].offset_bits / 8,
197 desc[i].size_bits / 8);
198 }
199 }
200 }
201 EXPORT_SYMBOL(ib_unpack);