packer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. * $Id: packer.c 1349 2004-12-16 21:09:43Z roland $
  34. */
  35. #include <linux/string.h>
  36. #include <rdma/ib_pack.h>
  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. * ib_pack - Pack a structure into a buffer
  51. * @desc:Array of structure field descriptions
  52. * @desc_len:Number of entries in @desc
  53. * @structure:Structure to pack from
  54. * @buf:Buffer to pack into
  55. *
  56. * ib_pack() packs a list of structure fields into a buffer,
  57. * controlled by the array of fields in @desc.
  58. */
  59. void ib_pack(const struct ib_field *desc,
  60. int desc_len,
  61. void *structure,
  62. void *buf)
  63. {
  64. int i;
  65. for (i = 0; i < desc_len; ++i) {
  66. if (desc[i].size_bits <= 32) {
  67. int shift;
  68. u32 val;
  69. __be32 mask;
  70. __be32 *addr;
  71. shift = 32 - desc[i].offset_bits - desc[i].size_bits;
  72. if (desc[i].struct_size_bytes)
  73. val = value_read(desc[i].struct_offset_bytes,
  74. desc[i].struct_size_bytes,
  75. structure) << shift;
  76. else
  77. val = 0;
  78. mask = cpu_to_be32(((1ull << desc[i].size_bits) - 1) << shift);
  79. addr = (__be32 *) buf + desc[i].offset_words;
  80. *addr = (*addr & ~mask) | (cpu_to_be32(val) & mask);
  81. } else if (desc[i].size_bits <= 64) {
  82. int shift;
  83. u64 val;
  84. __be64 mask;
  85. __be64 *addr;
  86. shift = 64 - desc[i].offset_bits - desc[i].size_bits;
  87. if (desc[i].struct_size_bytes)
  88. val = value_read(desc[i].struct_offset_bytes,
  89. desc[i].struct_size_bytes,
  90. structure) << shift;
  91. else
  92. val = 0;
  93. mask = cpu_to_be64((~0ull >> (64 - desc[i].size_bits)) << shift);
  94. addr = (__be64 *) ((__be32 *) buf + desc[i].offset_words);
  95. *addr = (*addr & ~mask) | (cpu_to_be64(val) & mask);
  96. } else {
  97. if (desc[i].offset_bits % 8 ||
  98. desc[i].size_bits % 8) {
  99. printk(KERN_WARNING "Structure field %s of size %d "
  100. "bits is not byte-aligned\n",
  101. desc[i].field_name, desc[i].size_bits);
  102. }
  103. if (desc[i].struct_size_bytes)
  104. memcpy(buf + desc[i].offset_words * 4 +
  105. desc[i].offset_bits / 8,
  106. structure + desc[i].struct_offset_bytes,
  107. desc[i].size_bits / 8);
  108. else
  109. memset(buf + desc[i].offset_words * 4 +
  110. desc[i].offset_bits / 8,
  111. 0,
  112. desc[i].size_bits / 8);
  113. }
  114. }
  115. }
  116. EXPORT_SYMBOL(ib_pack);
  117. static void value_write(int offset, int size, u64 val, void *structure)
  118. {
  119. switch (size * 8) {
  120. case 8: *( u8 *) (structure + offset) = val; break;
  121. case 16: *(__be16 *) (structure + offset) = cpu_to_be16(val); break;
  122. case 32: *(__be32 *) (structure + offset) = cpu_to_be32(val); break;
  123. case 64: *(__be64 *) (structure + offset) = cpu_to_be64(val); break;
  124. default:
  125. printk(KERN_WARNING "Field size %d bits not handled\n", size * 8);
  126. }
  127. }
  128. /**
  129. * ib_unpack - Unpack a buffer into a structure
  130. * @desc:Array of structure field descriptions
  131. * @desc_len:Number of entries in @desc
  132. * @buf:Buffer to unpack from
  133. * @structure:Structure to unpack into
  134. *
  135. * ib_pack() unpacks a list of structure fields from a buffer,
  136. * controlled by the array of fields in @desc.
  137. */
  138. void ib_unpack(const struct ib_field *desc,
  139. int desc_len,
  140. void *buf,
  141. void *structure)
  142. {
  143. int i;
  144. for (i = 0; i < desc_len; ++i) {
  145. if (!desc[i].struct_size_bytes)
  146. continue;
  147. if (desc[i].size_bits <= 32) {
  148. int shift;
  149. u32 val;
  150. u32 mask;
  151. __be32 *addr;
  152. shift = 32 - desc[i].offset_bits - desc[i].size_bits;
  153. mask = ((1ull << desc[i].size_bits) - 1) << shift;
  154. addr = (__be32 *) buf + desc[i].offset_words;
  155. val = (be32_to_cpup(addr) & mask) >> shift;
  156. value_write(desc[i].struct_offset_bytes,
  157. desc[i].struct_size_bytes,
  158. val,
  159. structure);
  160. } else if (desc[i].size_bits <= 64) {
  161. int shift;
  162. u64 val;
  163. u64 mask;
  164. __be64 *addr;
  165. shift = 64 - desc[i].offset_bits - desc[i].size_bits;
  166. mask = (~0ull >> (64 - desc[i].size_bits)) << shift;
  167. addr = (__be64 *) buf + desc[i].offset_words;
  168. val = (be64_to_cpup(addr) & mask) >> shift;
  169. value_write(desc[i].struct_offset_bytes,
  170. desc[i].struct_size_bytes,
  171. val,
  172. structure);
  173. } else {
  174. if (desc[i].offset_bits % 8 ||
  175. desc[i].size_bits % 8) {
  176. printk(KERN_WARNING "Structure field %s of size %d "
  177. "bits is not byte-aligned\n",
  178. desc[i].field_name, desc[i].size_bits);
  179. }
  180. memcpy(structure + desc[i].struct_offset_bytes,
  181. buf + desc[i].offset_words * 4 +
  182. desc[i].offset_bits / 8,
  183. desc[i].size_bits / 8);
  184. }
  185. }
  186. }
  187. EXPORT_SYMBOL(ib_unpack);