Path: blob/master/drivers/infiniband/core/packer.c
37212 views
/*1* Copyright (c) 2004 Topspin Corporation. All rights reserved.2* Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.3*4* This software is available to you under a choice of one of two5* licenses. You may choose to be licensed under the terms of the GNU6* General Public License (GPL) Version 2, available from the file7* COPYING in the main directory of this source tree, or the8* OpenIB.org BSD license below:9*10* Redistribution and use in source and binary forms, with or11* without modification, are permitted provided that the following12* conditions are met:13*14* - Redistributions of source code must retain the above15* copyright notice, this list of conditions and the following16* disclaimer.17*18* - Redistributions in binary form must reproduce the above19* copyright notice, this list of conditions and the following20* disclaimer in the documentation and/or other materials21* 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 OF25* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND26* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS27* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN28* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN29* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE30* SOFTWARE.31*/3233#include <linux/string.h>3435#include <rdma/ib_pack.h>3637static u64 value_read(int offset, int size, void *structure)38{39switch (size) {40case 1: return *(u8 *) (structure + offset);41case 2: return be16_to_cpup((__be16 *) (structure + offset));42case 4: return be32_to_cpup((__be32 *) (structure + offset));43case 8: return be64_to_cpup((__be64 *) (structure + offset));44default:45printk(KERN_WARNING "Field size %d bits not handled\n", size * 8);46return 0;47}48}4950/**51* ib_pack - Pack a structure into a buffer52* @desc:Array of structure field descriptions53* @desc_len:Number of entries in @desc54* @structure:Structure to pack from55* @buf:Buffer to pack into56*57* ib_pack() packs a list of structure fields into a buffer,58* controlled by the array of fields in @desc.59*/60void ib_pack(const struct ib_field *desc,61int desc_len,62void *structure,63void *buf)64{65int i;6667for (i = 0; i < desc_len; ++i) {68if (desc[i].size_bits <= 32) {69int shift;70u32 val;71__be32 mask;72__be32 *addr;7374shift = 32 - desc[i].offset_bits - desc[i].size_bits;75if (desc[i].struct_size_bytes)76val = value_read(desc[i].struct_offset_bytes,77desc[i].struct_size_bytes,78structure) << shift;79else80val = 0;8182mask = cpu_to_be32(((1ull << desc[i].size_bits) - 1) << shift);83addr = (__be32 *) buf + desc[i].offset_words;84*addr = (*addr & ~mask) | (cpu_to_be32(val) & mask);85} else if (desc[i].size_bits <= 64) {86int shift;87u64 val;88__be64 mask;89__be64 *addr;9091shift = 64 - desc[i].offset_bits - desc[i].size_bits;92if (desc[i].struct_size_bytes)93val = value_read(desc[i].struct_offset_bytes,94desc[i].struct_size_bytes,95structure) << shift;96else97val = 0;9899mask = cpu_to_be64((~0ull >> (64 - desc[i].size_bits)) << shift);100addr = (__be64 *) ((__be32 *) buf + desc[i].offset_words);101*addr = (*addr & ~mask) | (cpu_to_be64(val) & mask);102} else {103if (desc[i].offset_bits % 8 ||104desc[i].size_bits % 8) {105printk(KERN_WARNING "Structure field %s of size %d "106"bits is not byte-aligned\n",107desc[i].field_name, desc[i].size_bits);108}109110if (desc[i].struct_size_bytes)111memcpy(buf + desc[i].offset_words * 4 +112desc[i].offset_bits / 8,113structure + desc[i].struct_offset_bytes,114desc[i].size_bits / 8);115else116memset(buf + desc[i].offset_words * 4 +117desc[i].offset_bits / 8,1180,119desc[i].size_bits / 8);120}121}122}123EXPORT_SYMBOL(ib_pack);124125static void value_write(int offset, int size, u64 val, void *structure)126{127switch (size * 8) {128case 8: *( u8 *) (structure + offset) = val; break;129case 16: *(__be16 *) (structure + offset) = cpu_to_be16(val); break;130case 32: *(__be32 *) (structure + offset) = cpu_to_be32(val); break;131case 64: *(__be64 *) (structure + offset) = cpu_to_be64(val); break;132default:133printk(KERN_WARNING "Field size %d bits not handled\n", size * 8);134}135}136137/**138* ib_unpack - Unpack a buffer into a structure139* @desc:Array of structure field descriptions140* @desc_len:Number of entries in @desc141* @buf:Buffer to unpack from142* @structure:Structure to unpack into143*144* ib_pack() unpacks a list of structure fields from a buffer,145* controlled by the array of fields in @desc.146*/147void ib_unpack(const struct ib_field *desc,148int desc_len,149void *buf,150void *structure)151{152int i;153154for (i = 0; i < desc_len; ++i) {155if (!desc[i].struct_size_bytes)156continue;157158if (desc[i].size_bits <= 32) {159int shift;160u32 val;161u32 mask;162__be32 *addr;163164shift = 32 - desc[i].offset_bits - desc[i].size_bits;165mask = ((1ull << desc[i].size_bits) - 1) << shift;166addr = (__be32 *) buf + desc[i].offset_words;167val = (be32_to_cpup(addr) & mask) >> shift;168value_write(desc[i].struct_offset_bytes,169desc[i].struct_size_bytes,170val,171structure);172} else if (desc[i].size_bits <= 64) {173int shift;174u64 val;175u64 mask;176__be64 *addr;177178shift = 64 - desc[i].offset_bits - desc[i].size_bits;179mask = (~0ull >> (64 - desc[i].size_bits)) << shift;180addr = (__be64 *) buf + desc[i].offset_words;181val = (be64_to_cpup(addr) & mask) >> shift;182value_write(desc[i].struct_offset_bytes,183desc[i].struct_size_bytes,184val,185structure);186} else {187if (desc[i].offset_bits % 8 ||188desc[i].size_bits % 8) {189printk(KERN_WARNING "Structure field %s of size %d "190"bits is not byte-aligned\n",191desc[i].field_name, desc[i].size_bits);192}193194memcpy(structure + desc[i].struct_offset_bytes,195buf + desc[i].offset_words * 4 +196desc[i].offset_bits / 8,197desc[i].size_bits / 8);198}199}200}201EXPORT_SYMBOL(ib_unpack);202203204