ud_header.c 9.9 KB

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