fc_frame.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. #ifndef _FC_FRAME_H_
  20. #define _FC_FRAME_H_
  21. #include <linux/scatterlist.h>
  22. #include <linux/skbuff.h>
  23. #include <scsi/scsi_cmnd.h>
  24. #include <scsi/fc/fc_fs.h>
  25. #include <scsi/fc/fc_fcp.h>
  26. #include <scsi/fc/fc_encaps.h>
  27. #include <linux/if_ether.h>
  28. /* some helpful macros */
  29. #define ntohll(x) be64_to_cpu(x)
  30. #define htonll(x) cpu_to_be64(x)
  31. static inline u32 ntoh24(const u8 *p)
  32. {
  33. return (p[0] << 16) | (p[1] << 8) | p[2];
  34. }
  35. static inline void hton24(u8 *p, u32 v)
  36. {
  37. p[0] = (v >> 16) & 0xff;
  38. p[1] = (v >> 8) & 0xff;
  39. p[2] = v & 0xff;
  40. }
  41. /*
  42. * The fc_frame interface is used to pass frame data between functions.
  43. * The frame includes the data buffer, length, and SOF / EOF delimiter types.
  44. * A pointer to the port structure of the receiving port is also includeded.
  45. */
  46. #define FC_FRAME_HEADROOM 32 /* headroom for VLAN + FCoE headers */
  47. #define FC_FRAME_TAILROOM 8 /* trailer space for FCoE */
  48. /* Max number of skb frags allowed, reserving one for fcoe_crc_eof page */
  49. #define FC_FRAME_SG_LEN (MAX_SKB_FRAGS - 1)
  50. #define fp_skb(fp) (&((fp)->skb))
  51. #define fr_hdr(fp) ((fp)->skb.data)
  52. #define fr_len(fp) ((fp)->skb.len)
  53. #define fr_cb(fp) ((struct fcoe_rcv_info *)&((fp)->skb.cb[0]))
  54. #define fr_dev(fp) (fr_cb(fp)->fr_dev)
  55. #define fr_seq(fp) (fr_cb(fp)->fr_seq)
  56. #define fr_sof(fp) (fr_cb(fp)->fr_sof)
  57. #define fr_eof(fp) (fr_cb(fp)->fr_eof)
  58. #define fr_flags(fp) (fr_cb(fp)->fr_flags)
  59. #define fr_encaps(fp) (fr_cb(fp)->fr_encaps)
  60. #define fr_max_payload(fp) (fr_cb(fp)->fr_max_payload)
  61. #define fr_fsp(fp) (fr_cb(fp)->fr_fsp)
  62. #define fr_crc(fp) (fr_cb(fp)->fr_crc)
  63. struct fc_frame {
  64. struct sk_buff skb;
  65. };
  66. struct fcoe_rcv_info {
  67. struct fc_lport *fr_dev; /* transport layer private pointer */
  68. struct fc_seq *fr_seq; /* for use with exchange manager */
  69. struct fc_fcp_pkt *fr_fsp; /* for the corresponding fcp I/O */
  70. u32 fr_crc;
  71. u16 fr_max_payload; /* max FC payload */
  72. u8 fr_sof; /* start of frame delimiter */
  73. u8 fr_eof; /* end of frame delimiter */
  74. u8 fr_flags; /* flags - see below */
  75. u8 fr_encaps; /* LLD encapsulation info (e.g. FIP) */
  76. u8 granted_mac[ETH_ALEN]; /* FCoE MAC address */
  77. };
  78. /*
  79. * Get fc_frame pointer for an skb that's already been imported.
  80. */
  81. static inline struct fcoe_rcv_info *fcoe_dev_from_skb(const struct sk_buff *skb)
  82. {
  83. BUILD_BUG_ON(sizeof(struct fcoe_rcv_info) > sizeof(skb->cb));
  84. return (struct fcoe_rcv_info *) skb->cb;
  85. }
  86. /*
  87. * fr_flags.
  88. */
  89. #define FCPHF_CRC_UNCHECKED 0x01 /* CRC not computed, still appended */
  90. /*
  91. * Initialize a frame.
  92. * We don't do a complete memset here for performance reasons.
  93. * The caller must set fr_free, fr_hdr, fr_len, fr_sof, and fr_eof eventually.
  94. */
  95. static inline void fc_frame_init(struct fc_frame *fp)
  96. {
  97. fr_dev(fp) = NULL;
  98. fr_seq(fp) = NULL;
  99. fr_flags(fp) = 0;
  100. fr_encaps(fp) = 0;
  101. }
  102. struct fc_frame *fc_frame_alloc_fill(struct fc_lport *, size_t payload_len);
  103. struct fc_frame *_fc_frame_alloc(size_t payload_len);
  104. /*
  105. * Allocate fc_frame structure and buffer. Set the initial length to
  106. * payload_size + sizeof (struct fc_frame_header).
  107. */
  108. static inline struct fc_frame *fc_frame_alloc(struct fc_lport *dev, size_t len)
  109. {
  110. struct fc_frame *fp;
  111. /*
  112. * Note: Since len will often be a constant multiple of 4,
  113. * this check will usually be evaluated and eliminated at compile time.
  114. */
  115. if (len && len % 4)
  116. fp = fc_frame_alloc_fill(dev, len);
  117. else
  118. fp = _fc_frame_alloc(len);
  119. return fp;
  120. }
  121. /*
  122. * Free the fc_frame structure and buffer.
  123. */
  124. static inline void fc_frame_free(struct fc_frame *fp)
  125. {
  126. kfree_skb(fp_skb(fp));
  127. }
  128. static inline int fc_frame_is_linear(struct fc_frame *fp)
  129. {
  130. return !skb_is_nonlinear(fp_skb(fp));
  131. }
  132. /*
  133. * Get frame header from message in fc_frame structure.
  134. * This version doesn't do a length check.
  135. */
  136. static inline
  137. struct fc_frame_header *__fc_frame_header_get(const struct fc_frame *fp)
  138. {
  139. return (struct fc_frame_header *)fr_hdr(fp);
  140. }
  141. /*
  142. * Get frame header from message in fc_frame structure.
  143. * This hides a cast and provides a place to add some checking.
  144. */
  145. static inline
  146. struct fc_frame_header *fc_frame_header_get(const struct fc_frame *fp)
  147. {
  148. WARN_ON(fr_len(fp) < sizeof(struct fc_frame_header));
  149. return __fc_frame_header_get(fp);
  150. }
  151. /*
  152. * Get source FC_ID (S_ID) from frame header in message.
  153. */
  154. static inline u32 fc_frame_sid(const struct fc_frame *fp)
  155. {
  156. return ntoh24(__fc_frame_header_get(fp)->fh_s_id);
  157. }
  158. /*
  159. * Get destination FC_ID (D_ID) from frame header in message.
  160. */
  161. static inline u32 fc_frame_did(const struct fc_frame *fp)
  162. {
  163. return ntoh24(__fc_frame_header_get(fp)->fh_d_id);
  164. }
  165. /*
  166. * Get frame payload from message in fc_frame structure.
  167. * This hides a cast and provides a place to add some checking.
  168. * The len parameter is the minimum length for the payload portion.
  169. * Returns NULL if the frame is too short.
  170. *
  171. * This assumes the interesting part of the payload is in the first part
  172. * of the buffer for received data. This may not be appropriate to use for
  173. * buffers being transmitted.
  174. */
  175. static inline void *fc_frame_payload_get(const struct fc_frame *fp,
  176. size_t len)
  177. {
  178. void *pp = NULL;
  179. if (fr_len(fp) >= sizeof(struct fc_frame_header) + len)
  180. pp = fc_frame_header_get(fp) + 1;
  181. return pp;
  182. }
  183. /*
  184. * Get frame payload opcode (first byte) from message in fc_frame structure.
  185. * This hides a cast and provides a place to add some checking. Return 0
  186. * if the frame has no payload.
  187. */
  188. static inline u8 fc_frame_payload_op(const struct fc_frame *fp)
  189. {
  190. u8 *cp;
  191. cp = fc_frame_payload_get(fp, sizeof(u8));
  192. if (!cp)
  193. return 0;
  194. return *cp;
  195. }
  196. /*
  197. * Get FC class from frame.
  198. */
  199. static inline enum fc_class fc_frame_class(const struct fc_frame *fp)
  200. {
  201. return fc_sof_class(fr_sof(fp));
  202. }
  203. /*
  204. * Check the CRC in a frame.
  205. * The CRC immediately follows the last data item *AFTER* the length.
  206. * The return value is zero if the CRC matches.
  207. */
  208. u32 fc_frame_crc_check(struct fc_frame *);
  209. static inline u8 fc_frame_rctl(const struct fc_frame *fp)
  210. {
  211. return fc_frame_header_get(fp)->fh_r_ctl;
  212. }
  213. static inline bool fc_frame_is_cmd(const struct fc_frame *fp)
  214. {
  215. return fc_frame_rctl(fp) == FC_RCTL_DD_UNSOL_CMD;
  216. }
  217. /*
  218. * Check for leaks.
  219. * Print the frame header of any currently allocated frame, assuming there
  220. * should be none at this point.
  221. */
  222. void fc_frame_leak_check(void);
  223. #endif /* _FC_FRAME_H_ */