fc_frame.h 6.1 KB

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