xref: /openbmc/linux/drivers/infiniband/core/packer.c (revision c819e2cf)
1 /*
2  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
3  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4  *
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:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
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.
23  *
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
31  * SOFTWARE.
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 		printk(KERN_WARNING "Field size %d bits not handled\n", size * 8);
48 		return 0;
49 	}
50 }
51 
52 /**
53  * ib_pack - Pack a structure into a buffer
54  * @desc:Array of structure field descriptions
55  * @desc_len:Number of entries in @desc
56  * @structure:Structure to pack from
57  * @buf:Buffer to pack into
58  *
59  * ib_pack() packs a list of structure fields into a buffer,
60  * controlled by the array of fields in @desc.
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 				printk(KERN_WARNING "Structure field %s of size %d "
108 				       "bits is not byte-aligned\n",
109 				       desc[i].field_name, desc[i].size_bits);
110 			}
111 
112 			if (desc[i].struct_size_bytes)
113 				memcpy(buf + desc[i].offset_words * 4 +
114 				       desc[i].offset_bits / 8,
115 				       structure + desc[i].struct_offset_bytes,
116 				       desc[i].size_bits / 8);
117 			else
118 				memset(buf + desc[i].offset_words * 4 +
119 				       desc[i].offset_bits / 8,
120 				       0,
121 				       desc[i].size_bits / 8);
122 		}
123 	}
124 }
125 EXPORT_SYMBOL(ib_pack);
126 
127 static void value_write(int offset, int size, u64 val, void *structure)
128 {
129 	switch (size * 8) {
130 	case 8:  *(    u8 *) (structure + offset) = val; break;
131 	case 16: *(__be16 *) (structure + offset) = cpu_to_be16(val); break;
132 	case 32: *(__be32 *) (structure + offset) = cpu_to_be32(val); break;
133 	case 64: *(__be64 *) (structure + offset) = cpu_to_be64(val); break;
134 	default:
135 		printk(KERN_WARNING "Field size %d bits not handled\n", size * 8);
136 	}
137 }
138 
139 /**
140  * ib_unpack - Unpack a buffer into a structure
141  * @desc:Array of structure field descriptions
142  * @desc_len:Number of entries in @desc
143  * @buf:Buffer to unpack from
144  * @structure:Structure to unpack into
145  *
146  * ib_pack() unpacks a list of structure fields from a buffer,
147  * controlled by the array of fields in @desc.
148  */
149 void ib_unpack(const struct ib_field        *desc,
150 	       int                           desc_len,
151 	       void                         *buf,
152 	       void                         *structure)
153 {
154 	int i;
155 
156 	for (i = 0; i < desc_len; ++i) {
157 		if (!desc[i].struct_size_bytes)
158 			continue;
159 
160 		if (desc[i].size_bits <= 32) {
161 			int shift;
162 			u32  val;
163 			u32  mask;
164 			__be32 *addr;
165 
166 			shift = 32 - desc[i].offset_bits - desc[i].size_bits;
167 			mask = ((1ull << desc[i].size_bits) - 1) << shift;
168 			addr = (__be32 *) buf + desc[i].offset_words;
169 			val = (be32_to_cpup(addr) & mask) >> shift;
170 			value_write(desc[i].struct_offset_bytes,
171 				    desc[i].struct_size_bytes,
172 				    val,
173 				    structure);
174 		} else if (desc[i].size_bits <= 64) {
175 			int shift;
176 			u64  val;
177 			u64  mask;
178 			__be64 *addr;
179 
180 			shift = 64 - desc[i].offset_bits - desc[i].size_bits;
181 			mask = (~0ull >> (64 - desc[i].size_bits)) << shift;
182 			addr = (__be64 *) buf + desc[i].offset_words;
183 			val = (be64_to_cpup(addr) & mask) >> shift;
184 			value_write(desc[i].struct_offset_bytes,
185 				    desc[i].struct_size_bytes,
186 				    val,
187 				    structure);
188 		} else {
189 			if (desc[i].offset_bits % 8 ||
190 			    desc[i].size_bits   % 8) {
191 				printk(KERN_WARNING "Structure field %s of size %d "
192 				       "bits is not byte-aligned\n",
193 				       desc[i].field_name, desc[i].size_bits);
194 			}
195 
196 			memcpy(structure + desc[i].struct_offset_bytes,
197 			       buf + desc[i].offset_words * 4 +
198 			       desc[i].offset_bits / 8,
199 			       desc[i].size_bits / 8);
200 		}
201 	}
202 }
203 EXPORT_SYMBOL(ib_unpack);
204