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/string.h>*/ 35 36#include <rdma/ib_pack.h> 37 38#include <linux/module.h> 39 40#include <asm/byteorder.h> 41 42static u64 value_read(int offset, int size, void *structure) { 43 switch (size) { 44 case 1: 45 return *(u8 *) (structure + offset); 46 case 2: 47 return be16_to_cpup((__be16 *) (structure + offset)); 48 case 4: 49 return be32_to_cpup((__be32 *) (structure + offset)); 50 case 8: 51 return be64_to_cpup((__be64 *) (structure + offset)); 52 default: 53 printf("Field size %d bits not handled\n", size * 8); 54 return 0; 55 } 56} 57 58/** 59 * ib_pack - Pack a structure into a buffer 60 * @desc:Array of structure field descriptions 61 * @desc_len:Number of entries in @desc 62 * @structure:Structure to pack from 63 * @buf:Buffer to pack into 64 * 65 * ib_pack() packs a list of structure fields into a buffer, 66 * controlled by the array of fields in @desc. 67 */ 68void ib_pack(const struct ib_field *desc, int desc_len, void *structure, 69 void *buf) { 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, 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, structure) << shift; 99 else 100 val = 0; 101 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); 105 } else { 106 if (desc[i].offset_bits % 8 || desc[i].size_bits % 8) { 107 printf("Structure field %s of size %d " 108 "bits is not byte-aligned\n", desc[i].field_name, 109 desc[i].size_bits); 110 } 111 112 if (desc[i].struct_size_bytes) 113 memcpy(buf + desc[i].offset_words * 4 + desc[i].offset_bits / 8, 114 structure + desc[i].struct_offset_bytes, 115 desc[i].size_bits / 8); 116 else 117 memset(buf + desc[i].offset_words * 4 + desc[i].offset_bits / 8, 118 0, desc[i].size_bits / 8); 119 } 120 } 121} 122EXPORT_SYMBOL(ib_pack); 123 124static void value_write(int offset, int size, u64 val, void *structure) { 125 switch (size * 8) { 126 case 8: 127 *(u8 *) (structure + offset) = val; 128 break; 129 case 16: 130 *(__be16 *) (structure + offset) = cpu_to_be16(val); 131 break; 132 case 32: 133 *(__be32 *) (structure + offset) = cpu_to_be32(val); 134 break; 135 case 64: 136 *(__be64 *) (structure + offset) = cpu_to_be64(val); 137 break; 138 default: 139 printf("Field size %d bits not handled\n", size * 8); 140 } 141} 142 143/** 144 * ib_unpack - Unpack a buffer into a structure 145 * @desc:Array of structure field descriptions 146 * @desc_len:Number of entries in @desc 147 * @buf:Buffer to unpack from 148 * @structure:Structure to unpack into 149 * 150 * ib_pack() unpacks a list of structure fields from a buffer, 151 * controlled by the array of fields in @desc. 152 */ 153void ib_unpack(const struct ib_field *desc, int desc_len, void *buf, 154 void *structure) { 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, desc[i].struct_size_bytes, 172 val, structure); 173 } else if (desc[i].size_bits <= 64) { 174 int shift; 175 u64 val; 176 u64 mask; 177 __be64 *addr; 178 179 shift = 64 - desc[i].offset_bits - desc[i].size_bits; 180 mask = (~0ull >> (64 - desc[i].size_bits)) << shift; 181 addr = (__be64 *) buf + desc[i].offset_words; 182 val = (be64_to_cpup(addr) & mask) >> shift; 183 value_write(desc[i].struct_offset_bytes, desc[i].struct_size_bytes, 184 val, structure); 185 } else { 186 if (desc[i].offset_bits % 8 || desc[i].size_bits % 8) { 187 printf("Structure field %s of size %d " 188 "bits is not byte-aligned\n", desc[i].field_name, 189 desc[i].size_bits); 190 } 191 192 memcpy(structure + desc[i].struct_offset_bytes, 193 buf + desc[i].offset_words * 4 + desc[i].offset_bits / 8, 194 desc[i].size_bits / 8); 195 } 196 } 197} 198EXPORT_SYMBOL(ib_unpack); 199