packer.c 6.3 KB

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