fc_frame.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #define fp_skb(fp) (&((fp)->skb))
  35. #define fr_hdr(fp) ((fp)->skb.data)
  36. #define fr_len(fp) ((fp)->skb.len)
  37. #define fr_cb(fp) ((struct fcoe_rcv_info *)&((fp)->skb.cb[0]))
  38. #define fr_dev(fp) (fr_cb(fp)->fr_dev)
  39. #define fr_seq(fp) (fr_cb(fp)->fr_seq)
  40. #define fr_sof(fp) (fr_cb(fp)->fr_sof)
  41. #define fr_eof(fp) (fr_cb(fp)->fr_eof)
  42. #define fr_flags(fp) (fr_cb(fp)->fr_flags)
  43. #define fr_max_payload(fp) (fr_cb(fp)->fr_max_payload)
  44. #define fr_fsp(fp) (fr_cb(fp)->fr_fsp)
  45. #define fr_crc(fp) (fr_cb(fp)->fr_crc)
  46. struct fc_frame {
  47. struct sk_buff skb;
  48. };
  49. struct fcoe_rcv_info {
  50. struct packet_type *ptype;
  51. struct fc_lport *fr_dev; /* transport layer private pointer */
  52. struct fc_seq *fr_seq; /* for use with exchange manager */
  53. struct fc_fcp_pkt *fr_fsp; /* for the corresponding fcp I/O */
  54. u32 fr_crc;
  55. u16 fr_max_payload; /* max FC payload */
  56. enum fc_sof fr_sof; /* start of frame delimiter */
  57. enum fc_eof fr_eof; /* end of frame delimiter */
  58. u8 fr_flags; /* flags - see below */
  59. };
  60. /*
  61. * Get fc_frame pointer for an skb that's already been imported.
  62. */
  63. static inline struct fcoe_rcv_info *fcoe_dev_from_skb(const struct sk_buff *skb)
  64. {
  65. BUILD_BUG_ON(sizeof(struct fcoe_rcv_info) > sizeof(skb->cb));
  66. return (struct fcoe_rcv_info *) skb->cb;
  67. }
  68. /*
  69. * fr_flags.
  70. */
  71. #define FCPHF_CRC_UNCHECKED 0x01 /* CRC not computed, still appended */
  72. /*
  73. * Initialize a frame.
  74. * We don't do a complete memset here for performance reasons.
  75. * The caller must set fr_free, fr_hdr, fr_len, fr_sof, and fr_eof eventually.
  76. */
  77. static inline void fc_frame_init(struct fc_frame *fp)
  78. {
  79. fr_dev(fp) = NULL;
  80. fr_seq(fp) = NULL;
  81. fr_flags(fp) = 0;
  82. }
  83. struct fc_frame *fc_frame_alloc_fill(struct fc_lport *, size_t payload_len);
  84. struct fc_frame *__fc_frame_alloc(size_t payload_len);
  85. /*
  86. * Get frame for sending via port.
  87. */
  88. static inline struct fc_frame *_fc_frame_alloc(struct fc_lport *dev,
  89. size_t payload_len)
  90. {
  91. return __fc_frame_alloc(payload_len);
  92. }
  93. /*
  94. * Allocate fc_frame structure and buffer. Set the initial length to
  95. * payload_size + sizeof (struct fc_frame_header).
  96. */
  97. static inline struct fc_frame *fc_frame_alloc(struct fc_lport *dev, size_t len)
  98. {
  99. struct fc_frame *fp;
  100. /*
  101. * Note: Since len will often be a constant multiple of 4,
  102. * this check will usually be evaluated and eliminated at compile time.
  103. */
  104. if ((len % 4) != 0)
  105. fp = fc_frame_alloc_fill(dev, len);
  106. else
  107. fp = _fc_frame_alloc(dev, len);
  108. return fp;
  109. }
  110. /*
  111. * Free the fc_frame structure and buffer.
  112. */
  113. static inline void fc_frame_free(struct fc_frame *fp)
  114. {
  115. kfree_skb(fp_skb(fp));
  116. }
  117. static inline int fc_frame_is_linear(struct fc_frame *fp)
  118. {
  119. return !skb_is_nonlinear(fp_skb(fp));
  120. }
  121. /*
  122. * Get frame header from message in fc_frame structure.
  123. * This hides a cast and provides a place to add some checking.
  124. */
  125. static inline
  126. struct fc_frame_header *fc_frame_header_get(const struct fc_frame *fp)
  127. {
  128. WARN_ON(fr_len(fp) < sizeof(struct fc_frame_header));
  129. return (struct fc_frame_header *) fr_hdr(fp);
  130. }
  131. /*
  132. * Get frame payload from message in fc_frame structure.
  133. * This hides a cast and provides a place to add some checking.
  134. * The len parameter is the minimum length for the payload portion.
  135. * Returns NULL if the frame is too short.
  136. *
  137. * This assumes the interesting part of the payload is in the first part
  138. * of the buffer for received data. This may not be appropriate to use for
  139. * buffers being transmitted.
  140. */
  141. static inline void *fc_frame_payload_get(const struct fc_frame *fp,
  142. size_t len)
  143. {
  144. void *pp = NULL;
  145. if (fr_len(fp) >= sizeof(struct fc_frame_header) + len)
  146. pp = fc_frame_header_get(fp) + 1;
  147. return pp;
  148. }
  149. /*
  150. * Get frame payload opcode (first byte) from message in fc_frame structure.
  151. * This hides a cast and provides a place to add some checking. Return 0
  152. * if the frame has no payload.
  153. */
  154. static inline u8 fc_frame_payload_op(const struct fc_frame *fp)
  155. {
  156. u8 *cp;
  157. cp = fc_frame_payload_get(fp, sizeof(u8));
  158. if (!cp)
  159. return 0;
  160. return *cp;
  161. }
  162. /*
  163. * Get FC class from frame.
  164. */
  165. static inline enum fc_class fc_frame_class(const struct fc_frame *fp)
  166. {
  167. return fc_sof_class(fr_sof(fp));
  168. }
  169. /*
  170. * Check the CRC in a frame.
  171. * The CRC immediately follows the last data item *AFTER* the length.
  172. * The return value is zero if the CRC matches.
  173. */
  174. u32 fc_frame_crc_check(struct fc_frame *);
  175. static inline u8 fc_frame_rctl(const struct fc_frame *fp)
  176. {
  177. return fc_frame_header_get(fp)->fh_r_ctl;
  178. }
  179. static inline bool fc_frame_is_cmd(const struct fc_frame *fp)
  180. {
  181. return fc_frame_rctl(fp) == FC_RCTL_DD_UNSOL_CMD;
  182. }
  183. /*
  184. * Check for leaks.
  185. * Print the frame header of any currently allocated frame, assuming there
  186. * should be none at this point.
  187. */
  188. void fc_frame_leak_check(void);
  189. #endif /* _FC_FRAME_H_ */