packer.c 6.3 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. #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. printk(KERN_WARNING "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. printk(KERN_WARNING "Structure field %s of size %d "
  99. "bits is not byte-aligned\n",
  100. desc[i].field_name, desc[i].size_bits);
  101. }
  102. if (desc[i].struct_size_bytes)
  103. memcpy(buf + desc[i].offset_words * 4 +
  104. desc[i].offset_bits / 8,
  105. structure + desc[i].struct_offset_bytes,
  106. desc[i].size_bits / 8);
  107. else
  108. memset(buf + desc[i].offset_words * 4 +
  109. desc[i].offset_bits / 8,
  110. 0,
  111. desc[i].size_bits / 8);
  112. }
  113. }
  114. }
  115. EXPORT_SYMBOL(ib_pack);
  116. static void value_write(int offset, int size, u64 val, void *structure)
  117. {
  118. switch (size * 8) {
  119. case 8: *( u8 *) (structure + offset) = val; break;
  120. case 16: *(__be16 *) (structure + offset) = cpu_to_be16(val); break;
  121. case 32: *(__be32 *) (structure + offset) = cpu_to_be32(val); break;
  122. case 64: *(__be64 *) (structure + offset) = cpu_to_be64(val); break;
  123. default:
  124. printk(KERN_WARNING "Field size %d bits not handled\n", size * 8);
  125. }
  126. }
  127. /**
  128. * ib_unpack - Unpack a buffer into a structure
  129. * @desc:Array of structure field descriptions
  130. * @desc_len:Number of entries in @desc
  131. * @buf:Buffer to unpack from
  132. * @structure:Structure to unpack into
  133. *
  134. * ib_pack() unpacks a list of structure fields from a buffer,
  135. * controlled by the array of fields in @desc.
  136. */
  137. void ib_unpack(const struct ib_field *desc,
  138. int desc_len,
  139. void *buf,
  140. void *structure)
  141. {
  142. int i;
  143. for (i = 0; i < desc_len; ++i) {
  144. if (!desc[i].struct_size_bytes)
  145. continue;
  146. if (desc[i].size_bits <= 32) {
  147. int shift;
  148. u32 val;
  149. u32 mask;
  150. __be32 *addr;
  151. shift = 32 - desc[i].offset_bits - desc[i].size_bits;
  152. mask = ((1ull << desc[i].size_bits) - 1) << shift;
  153. addr = (__be32 *) buf + desc[i].offset_words;
  154. val = (be32_to_cpup(addr) & mask) >> shift;
  155. value_write(desc[i].struct_offset_bytes,
  156. desc[i].struct_size_bytes,
  157. val,
  158. structure);
  159. } else if (desc[i].size_bits <= 64) {
  160. int shift;
  161. u64 val;
  162. u64 mask;
  163. __be64 *addr;
  164. shift = 64 - desc[i].offset_bits - desc[i].size_bits;
  165. mask = (~0ull >> (64 - desc[i].size_bits)) << shift;
  166. addr = (__be64 *) buf + desc[i].offset_words;
  167. val = (be64_to_cpup(addr) & mask) >> shift;
  168. value_write(desc[i].struct_offset_bytes,
  169. desc[i].struct_size_bytes,
  170. val,
  171. structure);
  172. } else {
  173. if (desc[i].offset_bits % 8 ||
  174. desc[i].size_bits % 8) {
  175. printk(KERN_WARNING "Structure field %s of size %d "
  176. "bits is not byte-aligned\n",
  177. desc[i].field_name, desc[i].size_bits);
  178. }
  179. memcpy(structure + desc[i].struct_offset_bytes,
  180. buf + desc[i].offset_words * 4 +
  181. desc[i].offset_bits / 8,
  182. desc[i].size_bits / 8);
  183. }
  184. }
  185. }
  186. EXPORT_SYMBOL(ib_unpack);