fc_frame.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. /*
  28. * The fc_frame interface is used to pass frame data between functions.
  29. * The frame includes the data buffer, length, and SOF / EOF delimiter types.
  30. * A pointer to the port structure of the receiving port is also includeded.
  31. */
  32. #define FC_FRAME_HEADROOM 32 /* headroom for VLAN + FCoE headers */
  33. #define FC_FRAME_TAILROOM 8 /* trailer space for FCoE */
  34. /*
  35. * Information about an individual fibre channel frame received or to be sent.
  36. * The buffer may be in up to 4 additional non-contiguous sections,
  37. * but the linear section must hold the frame header.
  38. */
  39. #define FC_FRAME_SG_LEN 4 /* scatter/gather list maximum length */
  40. #define fp_skb(fp) (&((fp)->skb))
  41. #define fr_hdr(fp) ((fp)->skb.data)
  42. #define fr_len(fp) ((fp)->skb.len)
  43. #define fr_cb(fp) ((struct fcoe_rcv_info *)&((fp)->skb.cb[0]))
  44. #define fr_dev(fp) (fr_cb(fp)->fr_dev)
  45. #define fr_seq(fp) (fr_cb(fp)->fr_seq)
  46. #define fr_sof(fp) (fr_cb(fp)->fr_sof)
  47. #define fr_eof(fp) (fr_cb(fp)->fr_eof)
  48. #define fr_flags(fp) (fr_cb(fp)->fr_flags)
  49. #define fr_max_payload(fp) (fr_cb(fp)->fr_max_payload)
  50. #define fr_fsp(fp) (fr_cb(fp)->fr_fsp)
  51. #define fr_crc(fp) (fr_cb(fp)->fr_crc)
  52. struct fc_frame {
  53. struct sk_buff skb;
  54. };
  55. struct fcoe_rcv_info {
  56. struct packet_type *ptype;
  57. struct fc_lport *fr_dev; /* transport layer private pointer */
  58. struct fc_seq *fr_seq; /* for use with exchange manager */
  59. struct fc_fcp_pkt *fr_fsp; /* for the corresponding fcp I/O */
  60. u32 fr_crc;
  61. u16 fr_max_payload; /* max FC payload */
  62. enum fc_sof fr_sof; /* start of frame delimiter */
  63. enum fc_eof fr_eof; /* end of frame delimiter */
  64. u8 fr_flags; /* flags - see below */
  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. }
  89. struct fc_frame *fc_frame_alloc_fill(struct fc_lport *, size_t payload_len);
  90. struct fc_frame *__fc_frame_alloc(size_t payload_len);
  91. /*
  92. * Get frame for sending via port.
  93. */
  94. static inline struct fc_frame *_fc_frame_alloc(struct fc_lport *dev,
  95. size_t payload_len)
  96. {
  97. return __fc_frame_alloc(payload_len);
  98. }
  99. /*
  100. * Allocate fc_frame structure and buffer. Set the initial length to
  101. * payload_size + sizeof (struct fc_frame_header).
  102. */
  103. static inline struct fc_frame *fc_frame_alloc(struct fc_lport *dev, size_t len)
  104. {
  105. struct fc_frame *fp;
  106. /*
  107. * Note: Since len will often be a constant multiple of 4,
  108. * this check will usually be evaluated and eliminated at compile time.
  109. */
  110. if ((len % 4) != 0)
  111. fp = fc_frame_alloc_fill(dev, len);
  112. else
  113. fp = _fc_frame_alloc(dev, len);
  114. return fp;
  115. }
  116. /*
  117. * Free the fc_frame structure and buffer.
  118. */
  119. static inline void fc_frame_free(struct fc_frame *fp)
  120. {
  121. kfree_skb(fp_skb(fp));
  122. }
  123. static inline int fc_frame_is_linear(struct fc_frame *fp)
  124. {
  125. return !skb_is_nonlinear(fp_skb(fp));
  126. }
  127. /*
  128. * Get frame header from message in fc_frame structure.
  129. * This hides a cast and provides a place to add some checking.
  130. */
  131. static inline
  132. struct fc_frame_header *fc_frame_header_get(const struct fc_frame *fp)
  133. {
  134. WARN_ON(fr_len(fp) < sizeof(struct fc_frame_header));
  135. return (struct fc_frame_header *) fr_hdr(fp);
  136. }
  137. /*
  138. * Get frame payload from message in fc_frame structure.
  139. * This hides a cast and provides a place to add some checking.
  140. * The len parameter is the minimum length for the payload portion.
  141. * Returns NULL if the frame is too short.
  142. *
  143. * This assumes the interesting part of the payload is in the first part
  144. * of the buffer for received data. This may not be appropriate to use for
  145. * buffers being transmitted.
  146. */
  147. static inline void *fc_frame_payload_get(const struct fc_frame *fp,
  148. size_t len)
  149. {
  150. void *pp = NULL;
  151. if (fr_len(fp) >= sizeof(struct fc_frame_header) + len)
  152. pp = fc_frame_header_get(fp) + 1;
  153. return pp;
  154. }
  155. /*
  156. * Get frame payload opcode (first byte) from message in fc_frame structure.
  157. * This hides a cast and provides a place to add some checking. Return 0
  158. * if the frame has no payload.
  159. */
  160. static inline u8 fc_frame_payload_op(const struct fc_frame *fp)
  161. {
  162. u8 *cp;
  163. cp = fc_frame_payload_get(fp, sizeof(u8));
  164. if (!cp)
  165. return 0;
  166. return *cp;
  167. }
  168. /*
  169. * Get FC class from frame.
  170. */
  171. static inline enum fc_class fc_frame_class(const struct fc_frame *fp)
  172. {
  173. return fc_sof_class(fr_sof(fp));
  174. }
  175. /*
  176. * Check the CRC in a frame.
  177. * The CRC immediately follows the last data item *AFTER* the length.
  178. * The return value is zero if the CRC matches.
  179. */
  180. u32 fc_frame_crc_check(struct fc_frame *);
  181. static inline u8 fc_frame_rctl(const struct fc_frame *fp)
  182. {
  183. return fc_frame_header_get(fp)->fh_r_ctl;
  184. }
  185. static inline bool fc_frame_is_cmd(const struct fc_frame *fp)
  186. {
  187. return fc_frame_rctl(fp) == FC_RCTL_DD_UNSOL_CMD;
  188. }
  189. /*
  190. * Check for leaks.
  191. * Print the frame header of any currently allocated frame, assuming there
  192. * should be none at this point.
  193. */
  194. void fc_frame_leak_check(void);
  195. #endif /* _FC_FRAME_H_ */