fc_frame.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 packet_type *ptype;
  68. struct fc_lport *fr_dev; /* transport layer private pointer */
  69. struct fc_seq *fr_seq; /* for use with exchange manager */
  70. struct fc_fcp_pkt *fr_fsp; /* for the corresponding fcp I/O */
  71. u32 fr_crc;
  72. u16 fr_max_payload; /* max FC payload */
  73. u8 fr_sof; /* start of frame delimiter */
  74. u8 fr_eof; /* end of frame delimiter */
  75. u8 fr_flags; /* flags - see below */
  76. u8 fr_encaps; /* LLD encapsulation info (e.g. FIP) */
  77. u8 granted_mac[ETH_ALEN]; /* FCoE MAC address */
  78. };
  79. /*
  80. * Get fc_frame pointer for an skb that's already been imported.
  81. */
  82. static inline struct fcoe_rcv_info *fcoe_dev_from_skb(const struct sk_buff *skb)
  83. {
  84. BUILD_BUG_ON(sizeof(struct fcoe_rcv_info) > sizeof(skb->cb));
  85. return (struct fcoe_rcv_info *) skb->cb;
  86. }
  87. /*
  88. * fr_flags.
  89. */
  90. #define FCPHF_CRC_UNCHECKED 0x01 /* CRC not computed, still appended */
  91. /*
  92. * Initialize a frame.
  93. * We don't do a complete memset here for performance reasons.
  94. * The caller must set fr_free, fr_hdr, fr_len, fr_sof, and fr_eof eventually.
  95. */
  96. static inline void fc_frame_init(struct fc_frame *fp)
  97. {
  98. fr_dev(fp) = NULL;
  99. fr_seq(fp) = NULL;
  100. fr_flags(fp) = 0;
  101. fr_encaps(fp) = 0;
  102. }
  103. struct fc_frame *fc_frame_alloc_fill(struct fc_lport *, size_t payload_len);
  104. struct fc_frame *_fc_frame_alloc(size_t payload_len);
  105. /*
  106. * Allocate fc_frame structure and buffer. Set the initial length to
  107. * payload_size + sizeof (struct fc_frame_header).
  108. */
  109. static inline struct fc_frame *fc_frame_alloc(struct fc_lport *dev, size_t len)
  110. {
  111. struct fc_frame *fp;
  112. /*
  113. * Note: Since len will often be a constant multiple of 4,
  114. * this check will usually be evaluated and eliminated at compile time.
  115. */
  116. if (len && len % 4)
  117. fp = fc_frame_alloc_fill(dev, len);
  118. else
  119. fp = _fc_frame_alloc(len);
  120. return fp;
  121. }
  122. /*
  123. * Free the fc_frame structure and buffer.
  124. */
  125. static inline void fc_frame_free(struct fc_frame *fp)
  126. {
  127. kfree_skb(fp_skb(fp));
  128. }
  129. static inline int fc_frame_is_linear(struct fc_frame *fp)
  130. {
  131. return !skb_is_nonlinear(fp_skb(fp));
  132. }
  133. /*
  134. * Get frame header from message in fc_frame structure.
  135. * This version doesn't do a length check.
  136. */
  137. static inline
  138. struct fc_frame_header *__fc_frame_header_get(const struct fc_frame *fp)
  139. {
  140. return (struct fc_frame_header *)fr_hdr(fp);
  141. }
  142. /*
  143. * Get frame header from message in fc_frame structure.
  144. * This hides a cast and provides a place to add some checking.
  145. */
  146. static inline
  147. struct fc_frame_header *fc_frame_header_get(const struct fc_frame *fp)
  148. {
  149. WARN_ON(fr_len(fp) < sizeof(struct fc_frame_header));
  150. return __fc_frame_header_get(fp);
  151. }
  152. /*
  153. * Get source FC_ID (S_ID) from frame header in message.
  154. */
  155. static inline u32 fc_frame_sid(const struct fc_frame *fp)
  156. {
  157. return ntoh24(__fc_frame_header_get(fp)->fh_s_id);
  158. }
  159. /*
  160. * Get destination FC_ID (D_ID) from frame header in message.
  161. */
  162. static inline u32 fc_frame_did(const struct fc_frame *fp)
  163. {
  164. return ntoh24(__fc_frame_header_get(fp)->fh_d_id);
  165. }
  166. /*
  167. * Get frame payload from message in fc_frame structure.
  168. * This hides a cast and provides a place to add some checking.
  169. * The len parameter is the minimum length for the payload portion.
  170. * Returns NULL if the frame is too short.
  171. *
  172. * This assumes the interesting part of the payload is in the first part
  173. * of the buffer for received data. This may not be appropriate to use for
  174. * buffers being transmitted.
  175. */
  176. static inline void *fc_frame_payload_get(const struct fc_frame *fp,
  177. size_t len)
  178. {
  179. void *pp = NULL;
  180. if (fr_len(fp) >= sizeof(struct fc_frame_header) + len)
  181. pp = fc_frame_header_get(fp) + 1;
  182. return pp;
  183. }
  184. /*
  185. * Get frame payload opcode (first byte) from message in fc_frame structure.
  186. * This hides a cast and provides a place to add some checking. Return 0
  187. * if the frame has no payload.
  188. */
  189. static inline u8 fc_frame_payload_op(const struct fc_frame *fp)
  190. {
  191. u8 *cp;
  192. cp = fc_frame_payload_get(fp, sizeof(u8));
  193. if (!cp)
  194. return 0;
  195. return *cp;
  196. }
  197. /*
  198. * Get FC class from frame.
  199. */
  200. static inline enum fc_class fc_frame_class(const struct fc_frame *fp)
  201. {
  202. return fc_sof_class(fr_sof(fp));
  203. }
  204. /*
  205. * Check the CRC in a frame.
  206. * The CRC immediately follows the last data item *AFTER* the length.
  207. * The return value is zero if the CRC matches.
  208. */
  209. u32 fc_frame_crc_check(struct fc_frame *);
  210. static inline u8 fc_frame_rctl(const struct fc_frame *fp)
  211. {
  212. return fc_frame_header_get(fp)->fh_r_ctl;
  213. }
  214. static inline bool fc_frame_is_cmd(const struct fc_frame *fp)
  215. {
  216. return fc_frame_rctl(fp) == FC_RCTL_DD_UNSOL_CMD;
  217. }
  218. /*
  219. * Check for leaks.
  220. * Print the frame header of any currently allocated frame, assuming there
  221. * should be none at this point.
  222. */
  223. void fc_frame_leak_check(void);
  224. #endif /* _FC_FRAME_H_ */