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 #include <linux/if_ether.h> 32 33 /* some helpful macros */ 34 35 #define ntohll(x) be64_to_cpu(x) 36 #define htonll(x) cpu_to_be64(x) 37 38 static inline u32 ntoh24(const u8 *p) 39 { 40 return (p[0] << 16) | (p[1] << 8) | p[2]; 41 } 42 43 static inline void hton24(u8 *p, u32 v) 44 { 45 p[0] = (v >> 16) & 0xff; 46 p[1] = (v >> 8) & 0xff; 47 p[2] = v & 0xff; 48 } 49 50 /* 51 * The fc_frame interface is used to pass frame data between functions. 52 * The frame includes the data buffer, length, and SOF / EOF delimiter types. 53 * A pointer to the port structure of the receiving port is also includeded. 54 */ 55 56 #define FC_FRAME_HEADROOM 32 /* headroom for VLAN + FCoE headers */ 57 #define FC_FRAME_TAILROOM 8 /* trailer space for FCoE */ 58 59 /* Max number of skb frags allowed, reserving one for fcoe_crc_eof page */ 60 #define FC_FRAME_SG_LEN (MAX_SKB_FRAGS - 1) 61 62 #define fp_skb(fp) (&((fp)->skb)) 63 #define fr_hdr(fp) ((fp)->skb.data) 64 #define fr_len(fp) ((fp)->skb.len) 65 #define fr_cb(fp) ((struct fcoe_rcv_info *)&((fp)->skb.cb[0])) 66 #define fr_dev(fp) (fr_cb(fp)->fr_dev) 67 #define fr_seq(fp) (fr_cb(fp)->fr_seq) 68 #define fr_sof(fp) (fr_cb(fp)->fr_sof) 69 #define fr_eof(fp) (fr_cb(fp)->fr_eof) 70 #define fr_flags(fp) (fr_cb(fp)->fr_flags) 71 #define fr_encaps(fp) (fr_cb(fp)->fr_encaps) 72 #define fr_max_payload(fp) (fr_cb(fp)->fr_max_payload) 73 #define fr_fsp(fp) (fr_cb(fp)->fr_fsp) 74 #define fr_crc(fp) (fr_cb(fp)->fr_crc) 75 76 struct fc_frame { 77 struct sk_buff skb; 78 }; 79 80 struct fcoe_rcv_info { 81 struct packet_type *ptype; 82 struct fc_lport *fr_dev; /* transport layer private pointer */ 83 struct fc_seq *fr_seq; /* for use with exchange manager */ 84 struct fc_fcp_pkt *fr_fsp; /* for the corresponding fcp I/O */ 85 u32 fr_crc; 86 u16 fr_max_payload; /* max FC payload */ 87 u8 fr_sof; /* start of frame delimiter */ 88 u8 fr_eof; /* end of frame delimiter */ 89 u8 fr_flags; /* flags - see below */ 90 u8 fr_encaps; /* LLD encapsulation info (e.g. FIP) */ 91 u8 granted_mac[ETH_ALEN]; /* FCoE MAC address */ 92 }; 93 94 95 /* 96 * Get fc_frame pointer for an skb that's already been imported. 97 */ 98 static inline struct fcoe_rcv_info *fcoe_dev_from_skb(const struct sk_buff *skb) 99 { 100 BUILD_BUG_ON(sizeof(struct fcoe_rcv_info) > sizeof(skb->cb)); 101 return (struct fcoe_rcv_info *) skb->cb; 102 } 103 104 /* 105 * fr_flags. 106 */ 107 #define FCPHF_CRC_UNCHECKED 0x01 /* CRC not computed, still appended */ 108 109 /* 110 * Initialize a frame. 111 * We don't do a complete memset here for performance reasons. 112 * The caller must set fr_free, fr_hdr, fr_len, fr_sof, and fr_eof eventually. 113 */ 114 static inline void fc_frame_init(struct fc_frame *fp) 115 { 116 fr_dev(fp) = NULL; 117 fr_seq(fp) = NULL; 118 fr_flags(fp) = 0; 119 fr_encaps(fp) = 0; 120 } 121 122 struct fc_frame *fc_frame_alloc_fill(struct fc_lport *, size_t payload_len); 123 struct fc_frame *_fc_frame_alloc(size_t payload_len); 124 125 /* 126 * Allocate fc_frame structure and buffer. Set the initial length to 127 * payload_size + sizeof (struct fc_frame_header). 128 */ 129 static inline struct fc_frame *fc_frame_alloc(struct fc_lport *dev, size_t len) 130 { 131 struct fc_frame *fp; 132 133 /* 134 * Note: Since len will often be a constant multiple of 4, 135 * this check will usually be evaluated and eliminated at compile time. 136 */ 137 if (len && len % 4) 138 fp = fc_frame_alloc_fill(dev, len); 139 else 140 fp = _fc_frame_alloc(len); 141 return fp; 142 } 143 144 /* 145 * Free the fc_frame structure and buffer. 146 */ 147 static inline void fc_frame_free(struct fc_frame *fp) 148 { 149 kfree_skb(fp_skb(fp)); 150 } 151 152 static inline int fc_frame_is_linear(struct fc_frame *fp) 153 { 154 return !skb_is_nonlinear(fp_skb(fp)); 155 } 156 157 /* 158 * Get frame header from message in fc_frame structure. 159 * This version doesn't do a length check. 160 */ 161 static inline 162 struct fc_frame_header *__fc_frame_header_get(const struct fc_frame *fp) 163 { 164 return (struct fc_frame_header *)fr_hdr(fp); 165 } 166 167 /* 168 * Get frame header from message in fc_frame structure. 169 * This hides a cast and provides a place to add some checking. 170 */ 171 static inline 172 struct fc_frame_header *fc_frame_header_get(const struct fc_frame *fp) 173 { 174 WARN_ON(fr_len(fp) < sizeof(struct fc_frame_header)); 175 return __fc_frame_header_get(fp); 176 } 177 178 /* 179 * Get source FC_ID (S_ID) from frame header in message. 180 */ 181 static inline u32 fc_frame_sid(const struct fc_frame *fp) 182 { 183 return ntoh24(__fc_frame_header_get(fp)->fh_s_id); 184 } 185 186 /* 187 * Get destination FC_ID (D_ID) from frame header in message. 188 */ 189 static inline u32 fc_frame_did(const struct fc_frame *fp) 190 { 191 return ntoh24(__fc_frame_header_get(fp)->fh_d_id); 192 } 193 194 /* 195 * Get frame payload from message in fc_frame structure. 196 * This hides a cast and provides a place to add some checking. 197 * The len parameter is the minimum length for the payload portion. 198 * Returns NULL if the frame is too short. 199 * 200 * This assumes the interesting part of the payload is in the first part 201 * of the buffer for received data. This may not be appropriate to use for 202 * buffers being transmitted. 203 */ 204 static inline void *fc_frame_payload_get(const struct fc_frame *fp, 205 size_t len) 206 { 207 void *pp = NULL; 208 209 if (fr_len(fp) >= sizeof(struct fc_frame_header) + len) 210 pp = fc_frame_header_get(fp) + 1; 211 return pp; 212 } 213 214 /* 215 * Get frame payload opcode (first byte) from message in fc_frame structure. 216 * This hides a cast and provides a place to add some checking. Return 0 217 * if the frame has no payload. 218 */ 219 static inline u8 fc_frame_payload_op(const struct fc_frame *fp) 220 { 221 u8 *cp; 222 223 cp = fc_frame_payload_get(fp, sizeof(u8)); 224 if (!cp) 225 return 0; 226 return *cp; 227 228 } 229 230 /* 231 * Get FC class from frame. 232 */ 233 static inline enum fc_class fc_frame_class(const struct fc_frame *fp) 234 { 235 return fc_sof_class(fr_sof(fp)); 236 } 237 238 /* 239 * Check the CRC in a frame. 240 * The CRC immediately follows the last data item *AFTER* the length. 241 * The return value is zero if the CRC matches. 242 */ 243 u32 fc_frame_crc_check(struct fc_frame *); 244 245 static inline u8 fc_frame_rctl(const struct fc_frame *fp) 246 { 247 return fc_frame_header_get(fp)->fh_r_ctl; 248 } 249 250 static inline bool fc_frame_is_cmd(const struct fc_frame *fp) 251 { 252 return fc_frame_rctl(fp) == FC_RCTL_DD_UNSOL_CMD; 253 } 254 255 /* 256 * Check for leaks. 257 * Print the frame header of any currently allocated frame, assuming there 258 * should be none at this point. 259 */ 260 void fc_frame_leak_check(void); 261 262 #endif /* _FC_FRAME_H_ */ 263