packer.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/export.h>
  34. #include <linux/string.h>
  35. #include <rdma/ib_pack.h>
  36. static u64 value_read(int offset, int size, void *structure)
  37. {
  38. switch (size) {
  39. case 1: return *(u8 *) (structure + offset);
  40. case 2: return be16_to_cpup((__be16 *) (structure + offset));
  41. case 4: return be32_to_cpup((__be32 *) (structure + offset));
  42. case 8: return be64_to_cpup((__be64 *) (structure + offset));
  43. default:
  44. pr_warn("Field size %d bits not handled\n", size * 8);
  45. return 0;
  46. }
  47. }
  48. /**
  49. * ib_pack - Pack a structure into a buffer
  50. * @desc:Array of structure field descriptions
  51. * @desc_len:Number of entries in @desc
  52. * @structure:Structure to pack from
  53. * @buf:Buffer to pack into
  54. *
  55. * ib_pack() packs a list of structure fields into a buffer,
  56. * controlled by the array of fields in @desc.
  57. */
  58. void ib_pack(const struct ib_field *desc,
  59. int desc_len,
  60. void *structure,
  61. void *buf)
  62. {
  63. int i;
  64. for (i = 0; i < desc_len; ++i) {
  65. if (desc[i].size_bits <= 32) {
  66. int shift;
  67. u32 val;
  68. __be32 mask;
  69. __be32 *addr;
  70. shift = 32 - desc[i].offset_bits - desc[i].size_bits;
  71. if (desc[i].struct_size_bytes)
  72. val = value_read(desc[i].struct_offset_bytes,
  73. desc[i].struct_size_bytes,
  74. structure) << shift;
  75. else
  76. val = 0;
  77. mask = cpu_to_be32(((1ull << desc[i].size_bits) - 1) << shift);
  78. addr = (__be32 *) buf + desc[i].offset_words;
  79. *addr = (*addr & ~mask) | (cpu_to_be32(val) & mask);
  80. } else if (desc[i].size_bits <= 64) {
  81. int shift;
  82. u64 val;
  83. __be64 mask;
  84. __be64 *addr;
  85. shift = 64 - desc[i].offset_bits - desc[i].size_bits;
  86. if (desc[i].struct_size_bytes)
  87. val = value_read(desc[i].struct_offset_bytes,
  88. desc[i].struct_size_bytes,
  89. structure) << shift;
  90. else
  91. val = 0;
  92. mask = cpu_to_be64((~0ull >> (64 - desc[i].size_bits)) << shift);
  93. addr = (__be64 *) ((__be32 *) buf + desc[i].offset_words);
  94. *addr = (*addr & ~mask) | (cpu_to_be64(val) & mask);
  95. } else {
  96. if (desc[i].offset_bits % 8 ||
  97. desc[i].size_bits % 8) {
  98. pr_warn("Structure field %s of size %d 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. pr_warn("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. pr_warn("Structure field %s of size %d bits is not byte-aligned\n",
  175. desc[i].field_name, desc[i].size_bits);
  176. }
  177. memcpy(structure + desc[i].struct_offset_bytes,
  178. buf + desc[i].offset_words * 4 +
  179. desc[i].offset_bits / 8,
  180. desc[i].size_bits / 8);
  181. }
  182. }
  183. }
  184. EXPORT_SYMBOL(ib_unpack);