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