fc_frame.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * Allocate fc_frame structure and buffer. Set the initial length to
  91. * payload_size + sizeof (struct fc_frame_header).
  92. */
  93. static inline struct fc_frame *fc_frame_alloc(struct fc_lport *dev, size_t len)
  94. {
  95. struct fc_frame *fp;
  96. /*
  97. * Note: Since len will often be a constant multiple of 4,
  98. * this check will usually be evaluated and eliminated at compile time.
  99. */
  100. if (len && len % 4)
  101. fp = fc_frame_alloc_fill(dev, len);
  102. else
  103. fp = _fc_frame_alloc(len);
  104. return fp;
  105. }
  106. /*
  107. * Free the fc_frame structure and buffer.
  108. */
  109. static inline void fc_frame_free(struct fc_frame *fp)
  110. {
  111. kfree_skb(fp_skb(fp));
  112. }
  113. static inline int fc_frame_is_linear(struct fc_frame *fp)
  114. {
  115. return !skb_is_nonlinear(fp_skb(fp));
  116. }
  117. /*
  118. * Get frame header from message in fc_frame structure.
  119. * This hides a cast and provides a place to add some checking.
  120. */
  121. static inline
  122. struct fc_frame_header *fc_frame_header_get(const struct fc_frame *fp)
  123. {
  124. WARN_ON(fr_len(fp) < sizeof(struct fc_frame_header));
  125. return (struct fc_frame_header *) fr_hdr(fp);
  126. }
  127. /*
  128. * Get frame payload from message in fc_frame structure.
  129. * This hides a cast and provides a place to add some checking.
  130. * The len parameter is the minimum length for the payload portion.
  131. * Returns NULL if the frame is too short.
  132. *
  133. * This assumes the interesting part of the payload is in the first part
  134. * of the buffer for received data. This may not be appropriate to use for
  135. * buffers being transmitted.
  136. */
  137. static inline void *fc_frame_payload_get(const struct fc_frame *fp,
  138. size_t len)
  139. {
  140. void *pp = NULL;
  141. if (fr_len(fp) >= sizeof(struct fc_frame_header) + len)
  142. pp = fc_frame_header_get(fp) + 1;
  143. return pp;
  144. }
  145. /*
  146. * Get frame payload opcode (first byte) from message in fc_frame structure.
  147. * This hides a cast and provides a place to add some checking. Return 0
  148. * if the frame has no payload.
  149. */
  150. static inline u8 fc_frame_payload_op(const struct fc_frame *fp)
  151. {
  152. u8 *cp;
  153. cp = fc_frame_payload_get(fp, sizeof(u8));
  154. if (!cp)
  155. return 0;
  156. return *cp;
  157. }
  158. /*
  159. * Get FC class from frame.
  160. */
  161. static inline enum fc_class fc_frame_class(const struct fc_frame *fp)
  162. {
  163. return fc_sof_class(fr_sof(fp));
  164. }
  165. /*
  166. * Check the CRC in a frame.
  167. * The CRC immediately follows the last data item *AFTER* the length.
  168. * The return value is zero if the CRC matches.
  169. */
  170. u32 fc_frame_crc_check(struct fc_frame *);
  171. static inline u8 fc_frame_rctl(const struct fc_frame *fp)
  172. {
  173. return fc_frame_header_get(fp)->fh_r_ctl;
  174. }
  175. static inline bool fc_frame_is_cmd(const struct fc_frame *fp)
  176. {
  177. return fc_frame_rctl(fp) == FC_RCTL_DD_UNSOL_CMD;
  178. }
  179. /*
  180. * Check for leaks.
  181. * Print the frame header of any currently allocated frame, assuming there
  182. * should be none at this point.
  183. */
  184. void fc_frame_leak_check(void);
  185. #endif /* _FC_FRAME_H_ */