ud_header.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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: ud_header.c 1349 2004-12-16 21:09:43Z roland $
  34. */
  35. #include <linux/errno.h>
  36. #include <rdma/ib_pack.h>
  37. #define STRUCT_FIELD(header, field) \
  38. .struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field), \
  39. .struct_size_bytes = sizeof ((struct ib_unpacked_ ## header *) 0)->field, \
  40. .field_name = #header ":" #field
  41. static const struct ib_field lrh_table[] = {
  42. { STRUCT_FIELD(lrh, virtual_lane),
  43. .offset_words = 0,
  44. .offset_bits = 0,
  45. .size_bits = 4 },
  46. { STRUCT_FIELD(lrh, link_version),
  47. .offset_words = 0,
  48. .offset_bits = 4,
  49. .size_bits = 4 },
  50. { STRUCT_FIELD(lrh, service_level),
  51. .offset_words = 0,
  52. .offset_bits = 8,
  53. .size_bits = 4 },
  54. { RESERVED,
  55. .offset_words = 0,
  56. .offset_bits = 12,
  57. .size_bits = 2 },
  58. { STRUCT_FIELD(lrh, link_next_header),
  59. .offset_words = 0,
  60. .offset_bits = 14,
  61. .size_bits = 2 },
  62. { STRUCT_FIELD(lrh, destination_lid),
  63. .offset_words = 0,
  64. .offset_bits = 16,
  65. .size_bits = 16 },
  66. { RESERVED,
  67. .offset_words = 1,
  68. .offset_bits = 0,
  69. .size_bits = 5 },
  70. { STRUCT_FIELD(lrh, packet_length),
  71. .offset_words = 1,
  72. .offset_bits = 5,
  73. .size_bits = 11 },
  74. { STRUCT_FIELD(lrh, source_lid),
  75. .offset_words = 1,
  76. .offset_bits = 16,
  77. .size_bits = 16 }
  78. };
  79. static const struct ib_field grh_table[] = {
  80. { STRUCT_FIELD(grh, ip_version),
  81. .offset_words = 0,
  82. .offset_bits = 0,
  83. .size_bits = 4 },
  84. { STRUCT_FIELD(grh, traffic_class),
  85. .offset_words = 0,
  86. .offset_bits = 4,
  87. .size_bits = 8 },
  88. { STRUCT_FIELD(grh, flow_label),
  89. .offset_words = 0,
  90. .offset_bits = 12,
  91. .size_bits = 20 },
  92. { STRUCT_FIELD(grh, payload_length),
  93. .offset_words = 1,
  94. .offset_bits = 0,
  95. .size_bits = 16 },
  96. { STRUCT_FIELD(grh, next_header),
  97. .offset_words = 1,
  98. .offset_bits = 16,
  99. .size_bits = 8 },
  100. { STRUCT_FIELD(grh, hop_limit),
  101. .offset_words = 1,
  102. .offset_bits = 24,
  103. .size_bits = 8 },
  104. { STRUCT_FIELD(grh, source_gid),
  105. .offset_words = 2,
  106. .offset_bits = 0,
  107. .size_bits = 128 },
  108. { STRUCT_FIELD(grh, destination_gid),
  109. .offset_words = 6,
  110. .offset_bits = 0,
  111. .size_bits = 128 }
  112. };
  113. static const struct ib_field bth_table[] = {
  114. { STRUCT_FIELD(bth, opcode),
  115. .offset_words = 0,
  116. .offset_bits = 0,
  117. .size_bits = 8 },
  118. { STRUCT_FIELD(bth, solicited_event),
  119. .offset_words = 0,
  120. .offset_bits = 8,
  121. .size_bits = 1 },
  122. { STRUCT_FIELD(bth, mig_req),
  123. .offset_words = 0,
  124. .offset_bits = 9,
  125. .size_bits = 1 },
  126. { STRUCT_FIELD(bth, pad_count),
  127. .offset_words = 0,
  128. .offset_bits = 10,
  129. .size_bits = 2 },
  130. { STRUCT_FIELD(bth, transport_header_version),
  131. .offset_words = 0,
  132. .offset_bits = 12,
  133. .size_bits = 4 },
  134. { STRUCT_FIELD(bth, pkey),
  135. .offset_words = 0,
  136. .offset_bits = 16,
  137. .size_bits = 16 },
  138. { RESERVED,
  139. .offset_words = 1,
  140. .offset_bits = 0,
  141. .size_bits = 8 },
  142. { STRUCT_FIELD(bth, destination_qpn),
  143. .offset_words = 1,
  144. .offset_bits = 8,
  145. .size_bits = 24 },
  146. { STRUCT_FIELD(bth, ack_req),
  147. .offset_words = 2,
  148. .offset_bits = 0,
  149. .size_bits = 1 },
  150. { RESERVED,
  151. .offset_words = 2,
  152. .offset_bits = 1,
  153. .size_bits = 7 },
  154. { STRUCT_FIELD(bth, psn),
  155. .offset_words = 2,
  156. .offset_bits = 8,
  157. .size_bits = 24 }
  158. };
  159. static const struct ib_field deth_table[] = {
  160. { STRUCT_FIELD(deth, qkey),
  161. .offset_words = 0,
  162. .offset_bits = 0,
  163. .size_bits = 32 },
  164. { RESERVED,
  165. .offset_words = 1,
  166. .offset_bits = 0,
  167. .size_bits = 8 },
  168. { STRUCT_FIELD(deth, source_qpn),
  169. .offset_words = 1,
  170. .offset_bits = 8,
  171. .size_bits = 24 }
  172. };
  173. /**
  174. * ib_ud_header_init - Initialize UD header structure
  175. * @payload_bytes:Length of packet payload
  176. * @grh_present:GRH flag (if non-zero, GRH will be included)
  177. * @header:Structure to initialize
  178. *
  179. * ib_ud_header_init() initializes the lrh.link_version, lrh.link_next_header,
  180. * lrh.packet_length, grh.ip_version, grh.payload_length,
  181. * grh.next_header, bth.opcode, bth.pad_count and
  182. * bth.transport_header_version fields of a &struct ib_ud_header given
  183. * the payload length and whether a GRH will be included.
  184. */
  185. void ib_ud_header_init(int payload_bytes,
  186. int grh_present,
  187. struct ib_ud_header *header)
  188. {
  189. int header_len;
  190. u16 packet_length;
  191. memset(header, 0, sizeof *header);
  192. header_len =
  193. IB_LRH_BYTES +
  194. IB_BTH_BYTES +
  195. IB_DETH_BYTES;
  196. if (grh_present) {
  197. header_len += IB_GRH_BYTES;
  198. }
  199. header->lrh.link_version = 0;
  200. header->lrh.link_next_header =
  201. grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL;
  202. packet_length = (IB_LRH_BYTES +
  203. IB_BTH_BYTES +
  204. IB_DETH_BYTES +
  205. payload_bytes +
  206. 4 + /* ICRC */
  207. 3) / 4; /* round up */
  208. header->grh_present = grh_present;
  209. if (grh_present) {
  210. packet_length += IB_GRH_BYTES / 4;
  211. header->grh.ip_version = 6;
  212. header->grh.payload_length =
  213. cpu_to_be16((IB_BTH_BYTES +
  214. IB_DETH_BYTES +
  215. payload_bytes +
  216. 4 + /* ICRC */
  217. 3) & ~3); /* round up */
  218. header->grh.next_header = 0x1b;
  219. }
  220. header->lrh.packet_length = cpu_to_be16(packet_length);
  221. if (header->immediate_present)
  222. header->bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  223. else
  224. header->bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  225. header->bth.pad_count = (4 - payload_bytes) & 3;
  226. header->bth.transport_header_version = 0;
  227. }
  228. EXPORT_SYMBOL(ib_ud_header_init);
  229. /**
  230. * ib_ud_header_pack - Pack UD header struct into wire format
  231. * @header:UD header struct
  232. * @buf:Buffer to pack into
  233. *
  234. * ib_ud_header_pack() packs the UD header structure @header into wire
  235. * format in the buffer @buf.
  236. */
  237. int ib_ud_header_pack(struct ib_ud_header *header,
  238. void *buf)
  239. {
  240. int len = 0;
  241. ib_pack(lrh_table, ARRAY_SIZE(lrh_table),
  242. &header->lrh, buf);
  243. len += IB_LRH_BYTES;
  244. if (header->grh_present) {
  245. ib_pack(grh_table, ARRAY_SIZE(grh_table),
  246. &header->grh, buf + len);
  247. len += IB_GRH_BYTES;
  248. }
  249. ib_pack(bth_table, ARRAY_SIZE(bth_table),
  250. &header->bth, buf + len);
  251. len += IB_BTH_BYTES;
  252. ib_pack(deth_table, ARRAY_SIZE(deth_table),
  253. &header->deth, buf + len);
  254. len += IB_DETH_BYTES;
  255. if (header->immediate_present) {
  256. memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data);
  257. len += sizeof header->immediate_data;
  258. }
  259. return len;
  260. }
  261. EXPORT_SYMBOL(ib_ud_header_pack);
  262. /**
  263. * ib_ud_header_unpack - Unpack UD header struct from wire format
  264. * @header:UD header struct
  265. * @buf:Buffer to pack into
  266. *
  267. * ib_ud_header_pack() unpacks the UD header structure @header from wire
  268. * format in the buffer @buf.
  269. */
  270. int ib_ud_header_unpack(void *buf,
  271. struct ib_ud_header *header)
  272. {
  273. ib_unpack(lrh_table, ARRAY_SIZE(lrh_table),
  274. buf, &header->lrh);
  275. buf += IB_LRH_BYTES;
  276. if (header->lrh.link_version != 0) {
  277. printk(KERN_WARNING "Invalid LRH.link_version %d\n",
  278. header->lrh.link_version);
  279. return -EINVAL;
  280. }
  281. switch (header->lrh.link_next_header) {
  282. case IB_LNH_IBA_LOCAL:
  283. header->grh_present = 0;
  284. break;
  285. case IB_LNH_IBA_GLOBAL:
  286. header->grh_present = 1;
  287. ib_unpack(grh_table, ARRAY_SIZE(grh_table),
  288. buf, &header->grh);
  289. buf += IB_GRH_BYTES;
  290. if (header->grh.ip_version != 6) {
  291. printk(KERN_WARNING "Invalid GRH.ip_version %d\n",
  292. header->grh.ip_version);
  293. return -EINVAL;
  294. }
  295. if (header->grh.next_header != 0x1b) {
  296. printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n",
  297. header->grh.next_header);
  298. return -EINVAL;
  299. }
  300. break;
  301. default:
  302. printk(KERN_WARNING "Invalid LRH.link_next_header %d\n",
  303. header->lrh.link_next_header);
  304. return -EINVAL;
  305. }
  306. ib_unpack(bth_table, ARRAY_SIZE(bth_table),
  307. buf, &header->bth);
  308. buf += IB_BTH_BYTES;
  309. switch (header->bth.opcode) {
  310. case IB_OPCODE_UD_SEND_ONLY:
  311. header->immediate_present = 0;
  312. break;
  313. case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE:
  314. header->immediate_present = 1;
  315. break;
  316. default:
  317. printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n",
  318. header->bth.opcode);
  319. return -EINVAL;
  320. }
  321. if (header->bth.transport_header_version != 0) {
  322. printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n",
  323. header->bth.transport_header_version);
  324. return -EINVAL;
  325. }
  326. ib_unpack(deth_table, ARRAY_SIZE(deth_table),
  327. buf, &header->deth);
  328. buf += IB_DETH_BYTES;
  329. if (header->immediate_present)
  330. memcpy(&header->immediate_data, buf, sizeof header->immediate_data);
  331. return 0;
  332. }
  333. EXPORT_SYMBOL(ib_ud_header_unpack);