1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2004 Topspin Corporation.  All rights reserved.
5  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  *
35  * $FreeBSD$
36  */
37 
38 #include <linux/string.h>
39 
40 #include <rdma/ib_pack.h>
41 
42 static u64 value_read(int offset, int size, void *structure)
43 {
44 	switch (size) {
45 	case 1: return                *(u8  *) ((char *)structure + offset);
46 	case 2: return be16_to_cpup((__be16 *) ((char *)structure + offset));
47 	case 4: return be32_to_cpup((__be32 *) ((char *)structure + offset));
48 	case 8: return be64_to_cpup((__be64 *) ((char *)structure + offset));
49 	default:
50 		pr_warn("Field size %d bits not handled\n", size * 8);
51 		return 0;
52 	}
53 }
54 
55 /**
56  * ib_pack - Pack a structure into a buffer
57  * @desc:Array of structure field descriptions
58  * @desc_len:Number of entries in @desc
59  * @structure:Structure to pack from
60  * @buf:Buffer to pack into
61  *
62  * ib_pack() packs a list of structure fields into a buffer,
63  * controlled by the array of fields in @desc.
64  */
65 void ib_pack(const struct ib_field        *desc,
66 	     int                           desc_len,
67 	     void                         *structure,
68 	     void                         *buf)
69 {
70 	int i;
71 
72 	for (i = 0; i < desc_len; ++i) {
73 		if (desc[i].size_bits <= 32) {
74 			int shift;
75 			u32 val;
76 			__be32 mask;
77 			__be32 *addr;
78 
79 			shift = 32 - desc[i].offset_bits - desc[i].size_bits;
80 			if (desc[i].struct_size_bytes)
81 				val = value_read(desc[i].struct_offset_bytes,
82 						 desc[i].struct_size_bytes,
83 						 structure) << shift;
84 			else
85 				val = 0;
86 
87 			mask = cpu_to_be32(((1ull << desc[i].size_bits) - 1) << shift);
88 			addr = (__be32 *) buf + desc[i].offset_words;
89 			*addr = (*addr & ~mask) | (cpu_to_be32(val) & mask);
90 		} else if (desc[i].size_bits <= 64) {
91 			int shift;
92 			u64 val;
93 			__be64 mask;
94 			__be64 *addr;
95 
96 			shift = 64 - desc[i].offset_bits - desc[i].size_bits;
97 			if (desc[i].struct_size_bytes)
98 				val = value_read(desc[i].struct_offset_bytes,
99 						 desc[i].struct_size_bytes,
100 						 structure) << shift;
101 			else
102 				val = 0;
103 
104 			mask = cpu_to_be64((~0ull >> (64 - desc[i].size_bits)) << shift);
105 			addr = (__be64 *) ((__be32 *) buf + desc[i].offset_words);
106 			*addr = (*addr & ~mask) | (cpu_to_be64(val) & mask);
107 		} else {
108 			if (desc[i].offset_bits % 8 ||
109 			    desc[i].size_bits   % 8) {
110 				pr_warn("Structure field %s of size %d bits is not byte-aligned\n",
111 					desc[i].field_name, desc[i].size_bits);
112 			}
113 
114 			if (desc[i].struct_size_bytes)
115 				memcpy((char *)buf + desc[i].offset_words * 4 +
116 				       desc[i].offset_bits / 8,
117 				       (char *)structure + desc[i].struct_offset_bytes,
118 				       desc[i].size_bits / 8);
119 			else
120 				memset((char *)buf + desc[i].offset_words * 4 +
121 				       desc[i].offset_bits / 8,
122 				       0,
123 				       desc[i].size_bits / 8);
124 		}
125 	}
126 }
127 EXPORT_SYMBOL(ib_pack);
128 
129 static void value_write(int offset, int size, u64 val, void *structure)
130 {
131 	switch (size * 8) {
132 	case 8:  *(    u8 *) ((char *)structure + offset) = val; break;
133 	case 16: *(__be16 *) ((char *)structure + offset) = cpu_to_be16(val); break;
134 	case 32: *(__be32 *) ((char *)structure + offset) = cpu_to_be32(val); break;
135 	case 64: *(__be64 *) ((char *)structure + offset) = cpu_to_be64(val); break;
136 	default:
137 		pr_warn("Field size %d bits not handled\n", size * 8);
138 	}
139 }
140 
141 /**
142  * ib_unpack - Unpack a buffer into a structure
143  * @desc:Array of structure field descriptions
144  * @desc_len:Number of entries in @desc
145  * @buf:Buffer to unpack from
146  * @structure:Structure to unpack into
147  *
148  * ib_pack() unpacks a list of structure fields from a buffer,
149  * controlled by the array of fields in @desc.
150  */
151 void ib_unpack(const struct ib_field        *desc,
152 	       int                           desc_len,
153 	       void                         *buf,
154 	       void                         *structure)
155 {
156 	int i;
157 
158 	for (i = 0; i < desc_len; ++i) {
159 		if (!desc[i].struct_size_bytes)
160 			continue;
161 
162 		if (desc[i].size_bits <= 32) {
163 			int shift;
164 			u32  val;
165 			u32  mask;
166 			__be32 *addr;
167 
168 			shift = 32 - desc[i].offset_bits - desc[i].size_bits;
169 			mask = ((1ull << desc[i].size_bits) - 1) << shift;
170 			addr = (__be32 *) buf + desc[i].offset_words;
171 			val = (be32_to_cpup(addr) & mask) >> shift;
172 			value_write(desc[i].struct_offset_bytes,
173 				    desc[i].struct_size_bytes,
174 				    val,
175 				    structure);
176 		} else if (desc[i].size_bits <= 64) {
177 			int shift;
178 			u64  val;
179 			u64  mask;
180 			__be64 *addr;
181 
182 			shift = 64 - desc[i].offset_bits - desc[i].size_bits;
183 			mask = (~0ull >> (64 - desc[i].size_bits)) << shift;
184 			addr = (__be64 *) buf + desc[i].offset_words;
185 			val = (be64_to_cpup(addr) & mask) >> shift;
186 			value_write(desc[i].struct_offset_bytes,
187 				    desc[i].struct_size_bytes,
188 				    val,
189 				    structure);
190 		} else {
191 			if (desc[i].offset_bits % 8 ||
192 			    desc[i].size_bits   % 8) {
193 				pr_warn("Structure field %s of size %d bits is not byte-aligned\n",
194 					desc[i].field_name, desc[i].size_bits);
195 			}
196 
197 			memcpy((char *)structure + desc[i].struct_offset_bytes,
198 			       (char *)buf + desc[i].offset_words * 4 +
199 			       desc[i].offset_bits / 8,
200 			       desc[i].size_bits / 8);
201 		}
202 	}
203 }
204 EXPORT_SYMBOL(ib_unpack);
205