2 * Copyright (c) 2004 Topspin Corporation. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 * $Id: packer.c 1349 2004-12-16 21:09:43Z roland $
36 #include <linux/string.h>
38 #include <rdma/ib_pack.h>
40 static u64
value_read(int offset
, int size
, void *structure
)
43 case 1: return *(u8
*) (structure
+ offset
);
44 case 2: return be16_to_cpup((__be16
*) (structure
+ offset
));
45 case 4: return be32_to_cpup((__be32
*) (structure
+ offset
));
46 case 8: return be64_to_cpup((__be64
*) (structure
+ offset
));
48 printk(KERN_WARNING
"Field size %d bits not handled\n", size
* 8);
54 * ib_pack - Pack a structure into a buffer
55 * @desc:Array of structure field descriptions
56 * @desc_len:Number of entries in @desc
57 * @structure:Structure to pack from
58 * @buf:Buffer to pack into
60 * ib_pack() packs a list of structure fields into a buffer,
61 * controlled by the array of fields in @desc.
63 void ib_pack(const struct ib_field
*desc
,
70 for (i
= 0; i
< desc_len
; ++i
) {
71 if (desc
[i
].size_bits
<= 32) {
77 shift
= 32 - desc
[i
].offset_bits
- desc
[i
].size_bits
;
78 if (desc
[i
].struct_size_bytes
)
79 val
= value_read(desc
[i
].struct_offset_bytes
,
80 desc
[i
].struct_size_bytes
,
85 mask
= cpu_to_be32(((1ull << desc
[i
].size_bits
) - 1) << shift
);
86 addr
= (__be32
*) buf
+ desc
[i
].offset_words
;
87 *addr
= (*addr
& ~mask
) | (cpu_to_be32(val
) & mask
);
88 } else if (desc
[i
].size_bits
<= 64) {
94 shift
= 64 - desc
[i
].offset_bits
- desc
[i
].size_bits
;
95 if (desc
[i
].struct_size_bytes
)
96 val
= value_read(desc
[i
].struct_offset_bytes
,
97 desc
[i
].struct_size_bytes
,
102 mask
= cpu_to_be64((~0ull >> (64 - desc
[i
].size_bits
)) << shift
);
103 addr
= (__be64
*) ((__be32
*) buf
+ desc
[i
].offset_words
);
104 *addr
= (*addr
& ~mask
) | (cpu_to_be64(val
) & mask
);
106 if (desc
[i
].offset_bits
% 8 ||
107 desc
[i
].size_bits
% 8) {
108 printk(KERN_WARNING
"Structure field %s of size %d "
109 "bits is not byte-aligned\n",
110 desc
[i
].field_name
, desc
[i
].size_bits
);
113 if (desc
[i
].struct_size_bytes
)
114 memcpy(buf
+ desc
[i
].offset_words
* 4 +
115 desc
[i
].offset_bits
/ 8,
116 structure
+ desc
[i
].struct_offset_bytes
,
117 desc
[i
].size_bits
/ 8);
119 memset(buf
+ desc
[i
].offset_words
* 4 +
120 desc
[i
].offset_bits
/ 8,
122 desc
[i
].size_bits
/ 8);
126 EXPORT_SYMBOL(ib_pack
);
128 static void value_write(int offset
, int size
, u64 val
, void *structure
)
131 case 8: *( u8
*) (structure
+ offset
) = val
; break;
132 case 16: *(__be16
*) (structure
+ offset
) = cpu_to_be16(val
); break;
133 case 32: *(__be32
*) (structure
+ offset
) = cpu_to_be32(val
); break;
134 case 64: *(__be64
*) (structure
+ offset
) = cpu_to_be64(val
); break;
136 printk(KERN_WARNING
"Field size %d bits not handled\n", size
* 8);
141 * ib_unpack - Unpack a buffer into a structure
142 * @desc:Array of structure field descriptions
143 * @desc_len:Number of entries in @desc
144 * @buf:Buffer to unpack from
145 * @structure:Structure to unpack into
147 * ib_pack() unpacks a list of structure fields from a buffer,
148 * controlled by the array of fields in @desc.
150 void ib_unpack(const struct ib_field
*desc
,
157 for (i
= 0; i
< desc_len
; ++i
) {
158 if (!desc
[i
].struct_size_bytes
)
161 if (desc
[i
].size_bits
<= 32) {
167 shift
= 32 - desc
[i
].offset_bits
- desc
[i
].size_bits
;
168 mask
= ((1ull << desc
[i
].size_bits
) - 1) << shift
;
169 addr
= (__be32
*) buf
+ desc
[i
].offset_words
;
170 val
= (be32_to_cpup(addr
) & mask
) >> shift
;
171 value_write(desc
[i
].struct_offset_bytes
,
172 desc
[i
].struct_size_bytes
,
175 } else if (desc
[i
].size_bits
<= 64) {
181 shift
= 64 - desc
[i
].offset_bits
- desc
[i
].size_bits
;
182 mask
= (~0ull >> (64 - desc
[i
].size_bits
)) << shift
;
183 addr
= (__be64
*) buf
+ desc
[i
].offset_words
;
184 val
= (be64_to_cpup(addr
) & mask
) >> shift
;
185 value_write(desc
[i
].struct_offset_bytes
,
186 desc
[i
].struct_size_bytes
,
190 if (desc
[i
].offset_bits
% 8 ||
191 desc
[i
].size_bits
% 8) {
192 printk(KERN_WARNING
"Structure field %s of size %d "
193 "bits is not byte-aligned\n",
194 desc
[i
].field_name
, desc
[i
].size_bits
);
197 memcpy(structure
+ desc
[i
].struct_offset_bytes
,
198 buf
+ desc
[i
].offset_words
* 4 +
199 desc
[i
].offset_bits
/ 8,
200 desc
[i
].size_bits
/ 8);
204 EXPORT_SYMBOL(ib_unpack
);