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