162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) 2004 Topspin Corporation. All rights reserved. 362306a36Sopenharmony_ci * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * This software is available to you under a choice of one of two 662306a36Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 762306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 862306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the 962306a36Sopenharmony_ci * OpenIB.org BSD license below: 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or 1262306a36Sopenharmony_ci * without modification, are permitted provided that the following 1362306a36Sopenharmony_ci * conditions are met: 1462306a36Sopenharmony_ci * 1562306a36Sopenharmony_ci * - Redistributions of source code must retain the above 1662306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 1762306a36Sopenharmony_ci * disclaimer. 1862306a36Sopenharmony_ci * 1962306a36Sopenharmony_ci * - Redistributions in binary form must reproduce the above 2062306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 2162306a36Sopenharmony_ci * disclaimer in the documentation and/or other materials 2262306a36Sopenharmony_ci * provided with the distribution. 2362306a36Sopenharmony_ci * 2462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2562306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2662306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2762306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2862306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2962306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 3062306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3162306a36Sopenharmony_ci * SOFTWARE. 3262306a36Sopenharmony_ci */ 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci#include <linux/export.h> 3562306a36Sopenharmony_ci#include <linux/string.h> 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci#include <rdma/ib_pack.h> 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_cistatic u64 value_read(int offset, int size, void *structure) 4062306a36Sopenharmony_ci{ 4162306a36Sopenharmony_ci switch (size) { 4262306a36Sopenharmony_ci case 1: return *(u8 *) (structure + offset); 4362306a36Sopenharmony_ci case 2: return be16_to_cpup((__be16 *) (structure + offset)); 4462306a36Sopenharmony_ci case 4: return be32_to_cpup((__be32 *) (structure + offset)); 4562306a36Sopenharmony_ci case 8: return be64_to_cpup((__be64 *) (structure + offset)); 4662306a36Sopenharmony_ci default: 4762306a36Sopenharmony_ci pr_warn("Field size %d bits not handled\n", size * 8); 4862306a36Sopenharmony_ci return 0; 4962306a36Sopenharmony_ci } 5062306a36Sopenharmony_ci} 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci/** 5362306a36Sopenharmony_ci * ib_pack - Pack a structure into a buffer 5462306a36Sopenharmony_ci * @desc:Array of structure field descriptions 5562306a36Sopenharmony_ci * @desc_len:Number of entries in @desc 5662306a36Sopenharmony_ci * @structure:Structure to pack from 5762306a36Sopenharmony_ci * @buf:Buffer to pack into 5862306a36Sopenharmony_ci * 5962306a36Sopenharmony_ci * ib_pack() packs a list of structure fields into a buffer, 6062306a36Sopenharmony_ci * controlled by the array of fields in @desc. 6162306a36Sopenharmony_ci */ 6262306a36Sopenharmony_civoid ib_pack(const struct ib_field *desc, 6362306a36Sopenharmony_ci int desc_len, 6462306a36Sopenharmony_ci void *structure, 6562306a36Sopenharmony_ci void *buf) 6662306a36Sopenharmony_ci{ 6762306a36Sopenharmony_ci int i; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci for (i = 0; i < desc_len; ++i) { 7062306a36Sopenharmony_ci if (desc[i].size_bits <= 32) { 7162306a36Sopenharmony_ci int shift; 7262306a36Sopenharmony_ci u32 val; 7362306a36Sopenharmony_ci __be32 mask; 7462306a36Sopenharmony_ci __be32 *addr; 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci shift = 32 - desc[i].offset_bits - desc[i].size_bits; 7762306a36Sopenharmony_ci if (desc[i].struct_size_bytes) 7862306a36Sopenharmony_ci val = value_read(desc[i].struct_offset_bytes, 7962306a36Sopenharmony_ci desc[i].struct_size_bytes, 8062306a36Sopenharmony_ci structure) << shift; 8162306a36Sopenharmony_ci else 8262306a36Sopenharmony_ci val = 0; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci mask = cpu_to_be32(((1ull << desc[i].size_bits) - 1) << shift); 8562306a36Sopenharmony_ci addr = (__be32 *) buf + desc[i].offset_words; 8662306a36Sopenharmony_ci *addr = (*addr & ~mask) | (cpu_to_be32(val) & mask); 8762306a36Sopenharmony_ci } else if (desc[i].size_bits <= 64) { 8862306a36Sopenharmony_ci int shift; 8962306a36Sopenharmony_ci u64 val; 9062306a36Sopenharmony_ci __be64 mask; 9162306a36Sopenharmony_ci __be64 *addr; 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ci shift = 64 - desc[i].offset_bits - desc[i].size_bits; 9462306a36Sopenharmony_ci if (desc[i].struct_size_bytes) 9562306a36Sopenharmony_ci val = value_read(desc[i].struct_offset_bytes, 9662306a36Sopenharmony_ci desc[i].struct_size_bytes, 9762306a36Sopenharmony_ci structure) << shift; 9862306a36Sopenharmony_ci else 9962306a36Sopenharmony_ci val = 0; 10062306a36Sopenharmony_ci 10162306a36Sopenharmony_ci mask = cpu_to_be64((~0ull >> (64 - desc[i].size_bits)) << shift); 10262306a36Sopenharmony_ci addr = (__be64 *) ((__be32 *) buf + desc[i].offset_words); 10362306a36Sopenharmony_ci *addr = (*addr & ~mask) | (cpu_to_be64(val) & mask); 10462306a36Sopenharmony_ci } else { 10562306a36Sopenharmony_ci if (desc[i].offset_bits % 8 || 10662306a36Sopenharmony_ci desc[i].size_bits % 8) { 10762306a36Sopenharmony_ci pr_warn("Structure field %s of size %d bits is not byte-aligned\n", 10862306a36Sopenharmony_ci desc[i].field_name, desc[i].size_bits); 10962306a36Sopenharmony_ci } 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ci if (desc[i].struct_size_bytes) 11262306a36Sopenharmony_ci memcpy(buf + desc[i].offset_words * 4 + 11362306a36Sopenharmony_ci desc[i].offset_bits / 8, 11462306a36Sopenharmony_ci structure + desc[i].struct_offset_bytes, 11562306a36Sopenharmony_ci desc[i].size_bits / 8); 11662306a36Sopenharmony_ci else 11762306a36Sopenharmony_ci memset(buf + desc[i].offset_words * 4 + 11862306a36Sopenharmony_ci desc[i].offset_bits / 8, 11962306a36Sopenharmony_ci 0, 12062306a36Sopenharmony_ci desc[i].size_bits / 8); 12162306a36Sopenharmony_ci } 12262306a36Sopenharmony_ci } 12362306a36Sopenharmony_ci} 12462306a36Sopenharmony_ciEXPORT_SYMBOL(ib_pack); 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_cistatic void value_write(int offset, int size, u64 val, void *structure) 12762306a36Sopenharmony_ci{ 12862306a36Sopenharmony_ci switch (size * 8) { 12962306a36Sopenharmony_ci case 8: *( u8 *) (structure + offset) = val; break; 13062306a36Sopenharmony_ci case 16: *(__be16 *) (structure + offset) = cpu_to_be16(val); break; 13162306a36Sopenharmony_ci case 32: *(__be32 *) (structure + offset) = cpu_to_be32(val); break; 13262306a36Sopenharmony_ci case 64: *(__be64 *) (structure + offset) = cpu_to_be64(val); break; 13362306a36Sopenharmony_ci default: 13462306a36Sopenharmony_ci pr_warn("Field size %d bits not handled\n", size * 8); 13562306a36Sopenharmony_ci } 13662306a36Sopenharmony_ci} 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_ci/** 13962306a36Sopenharmony_ci * ib_unpack - Unpack a buffer into a structure 14062306a36Sopenharmony_ci * @desc:Array of structure field descriptions 14162306a36Sopenharmony_ci * @desc_len:Number of entries in @desc 14262306a36Sopenharmony_ci * @buf:Buffer to unpack from 14362306a36Sopenharmony_ci * @structure:Structure to unpack into 14462306a36Sopenharmony_ci * 14562306a36Sopenharmony_ci * ib_pack() unpacks a list of structure fields from a buffer, 14662306a36Sopenharmony_ci * controlled by the array of fields in @desc. 14762306a36Sopenharmony_ci */ 14862306a36Sopenharmony_civoid ib_unpack(const struct ib_field *desc, 14962306a36Sopenharmony_ci int desc_len, 15062306a36Sopenharmony_ci void *buf, 15162306a36Sopenharmony_ci void *structure) 15262306a36Sopenharmony_ci{ 15362306a36Sopenharmony_ci int i; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci for (i = 0; i < desc_len; ++i) { 15662306a36Sopenharmony_ci if (!desc[i].struct_size_bytes) 15762306a36Sopenharmony_ci continue; 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci if (desc[i].size_bits <= 32) { 16062306a36Sopenharmony_ci int shift; 16162306a36Sopenharmony_ci u32 val; 16262306a36Sopenharmony_ci u32 mask; 16362306a36Sopenharmony_ci __be32 *addr; 16462306a36Sopenharmony_ci 16562306a36Sopenharmony_ci shift = 32 - desc[i].offset_bits - desc[i].size_bits; 16662306a36Sopenharmony_ci mask = ((1ull << desc[i].size_bits) - 1) << shift; 16762306a36Sopenharmony_ci addr = (__be32 *) buf + desc[i].offset_words; 16862306a36Sopenharmony_ci val = (be32_to_cpup(addr) & mask) >> shift; 16962306a36Sopenharmony_ci value_write(desc[i].struct_offset_bytes, 17062306a36Sopenharmony_ci desc[i].struct_size_bytes, 17162306a36Sopenharmony_ci val, 17262306a36Sopenharmony_ci structure); 17362306a36Sopenharmony_ci } else if (desc[i].size_bits <= 64) { 17462306a36Sopenharmony_ci int shift; 17562306a36Sopenharmony_ci u64 val; 17662306a36Sopenharmony_ci u64 mask; 17762306a36Sopenharmony_ci __be64 *addr; 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ci shift = 64 - desc[i].offset_bits - desc[i].size_bits; 18062306a36Sopenharmony_ci mask = (~0ull >> (64 - desc[i].size_bits)) << shift; 18162306a36Sopenharmony_ci addr = (__be64 *) buf + desc[i].offset_words; 18262306a36Sopenharmony_ci val = (be64_to_cpup(addr) & mask) >> shift; 18362306a36Sopenharmony_ci value_write(desc[i].struct_offset_bytes, 18462306a36Sopenharmony_ci desc[i].struct_size_bytes, 18562306a36Sopenharmony_ci val, 18662306a36Sopenharmony_ci structure); 18762306a36Sopenharmony_ci } else { 18862306a36Sopenharmony_ci if (desc[i].offset_bits % 8 || 18962306a36Sopenharmony_ci desc[i].size_bits % 8) { 19062306a36Sopenharmony_ci pr_warn("Structure field %s of size %d bits is not byte-aligned\n", 19162306a36Sopenharmony_ci desc[i].field_name, desc[i].size_bits); 19262306a36Sopenharmony_ci } 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci memcpy(structure + desc[i].struct_offset_bytes, 19562306a36Sopenharmony_ci buf + desc[i].offset_words * 4 + 19662306a36Sopenharmony_ci desc[i].offset_bits / 8, 19762306a36Sopenharmony_ci desc[i].size_bits / 8); 19862306a36Sopenharmony_ci } 19962306a36Sopenharmony_ci } 20062306a36Sopenharmony_ci} 20162306a36Sopenharmony_ciEXPORT_SYMBOL(ib_unpack); 202