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