fc_frame.h 6.3 KB

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