/*1* Copyright(c) 2007 Intel Corporation. All rights reserved.2*3* This program is free software; you can redistribute it and/or modify it4* under the terms and conditions of the GNU General Public License,5* version 2, as published by the Free Software Foundation.6*7* This program is distributed in the hope it will be useful, but WITHOUT8* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or9* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for10* more details.11*12* You should have received a copy of the GNU General Public License along with13* this program; if not, write to the Free Software Foundation, Inc.,14* 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.15*16* Maintained at www.Open-FCoE.org17*/1819#ifndef _FC_FRAME_H_20#define _FC_FRAME_H_2122#include <linux/scatterlist.h>23#include <linux/skbuff.h>24#include <scsi/scsi_cmnd.h>2526#include <scsi/fc/fc_fs.h>27#include <scsi/fc/fc_fcp.h>28#include <scsi/fc/fc_encaps.h>2930#include <linux/if_ether.h>3132/* some helpful macros */3334#define ntohll(x) be64_to_cpu(x)35#define htonll(x) cpu_to_be64(x)3637static inline u32 ntoh24(const u8 *p)38{39return (p[0] << 16) | (p[1] << 8) | p[2];40}4142static inline void hton24(u8 *p, u32 v)43{44p[0] = (v >> 16) & 0xff;45p[1] = (v >> 8) & 0xff;46p[2] = v & 0xff;47}4849/*50* The fc_frame interface is used to pass frame data between functions.51* The frame includes the data buffer, length, and SOF / EOF delimiter types.52* A pointer to the port structure of the receiving port is also includeded.53*/5455#define FC_FRAME_HEADROOM 32 /* headroom for VLAN + FCoE headers */56#define FC_FRAME_TAILROOM 8 /* trailer space for FCoE */5758/* Max number of skb frags allowed, reserving one for fcoe_crc_eof page */59#define FC_FRAME_SG_LEN (MAX_SKB_FRAGS - 1)6061#define fp_skb(fp) (&((fp)->skb))62#define fr_hdr(fp) ((fp)->skb.data)63#define fr_len(fp) ((fp)->skb.len)64#define fr_cb(fp) ((struct fcoe_rcv_info *)&((fp)->skb.cb[0]))65#define fr_dev(fp) (fr_cb(fp)->fr_dev)66#define fr_seq(fp) (fr_cb(fp)->fr_seq)67#define fr_sof(fp) (fr_cb(fp)->fr_sof)68#define fr_eof(fp) (fr_cb(fp)->fr_eof)69#define fr_flags(fp) (fr_cb(fp)->fr_flags)70#define fr_encaps(fp) (fr_cb(fp)->fr_encaps)71#define fr_max_payload(fp) (fr_cb(fp)->fr_max_payload)72#define fr_fsp(fp) (fr_cb(fp)->fr_fsp)73#define fr_crc(fp) (fr_cb(fp)->fr_crc)7475struct fc_frame {76struct sk_buff skb;77};7879struct fcoe_rcv_info {80struct packet_type *ptype;81struct fc_lport *fr_dev; /* transport layer private pointer */82struct fc_seq *fr_seq; /* for use with exchange manager */83struct fc_fcp_pkt *fr_fsp; /* for the corresponding fcp I/O */84u32 fr_crc;85u16 fr_max_payload; /* max FC payload */86u8 fr_sof; /* start of frame delimiter */87u8 fr_eof; /* end of frame delimiter */88u8 fr_flags; /* flags - see below */89u8 fr_encaps; /* LLD encapsulation info (e.g. FIP) */90u8 granted_mac[ETH_ALEN]; /* FCoE MAC address */91};929394/*95* Get fc_frame pointer for an skb that's already been imported.96*/97static inline struct fcoe_rcv_info *fcoe_dev_from_skb(const struct sk_buff *skb)98{99BUILD_BUG_ON(sizeof(struct fcoe_rcv_info) > sizeof(skb->cb));100return (struct fcoe_rcv_info *) skb->cb;101}102103/*104* fr_flags.105*/106#define FCPHF_CRC_UNCHECKED 0x01 /* CRC not computed, still appended */107108/*109* Initialize a frame.110* We don't do a complete memset here for performance reasons.111* The caller must set fr_free, fr_hdr, fr_len, fr_sof, and fr_eof eventually.112*/113static inline void fc_frame_init(struct fc_frame *fp)114{115fr_dev(fp) = NULL;116fr_seq(fp) = NULL;117fr_flags(fp) = 0;118fr_encaps(fp) = 0;119}120121struct fc_frame *fc_frame_alloc_fill(struct fc_lport *, size_t payload_len);122struct fc_frame *_fc_frame_alloc(size_t payload_len);123124/*125* Allocate fc_frame structure and buffer. Set the initial length to126* payload_size + sizeof (struct fc_frame_header).127*/128static inline struct fc_frame *fc_frame_alloc(struct fc_lport *dev, size_t len)129{130struct fc_frame *fp;131132/*133* Note: Since len will often be a constant multiple of 4,134* this check will usually be evaluated and eliminated at compile time.135*/136if (len && len % 4)137fp = fc_frame_alloc_fill(dev, len);138else139fp = _fc_frame_alloc(len);140return fp;141}142143/*144* Free the fc_frame structure and buffer.145*/146static inline void fc_frame_free(struct fc_frame *fp)147{148kfree_skb(fp_skb(fp));149}150151static inline int fc_frame_is_linear(struct fc_frame *fp)152{153return !skb_is_nonlinear(fp_skb(fp));154}155156/*157* Get frame header from message in fc_frame structure.158* This version doesn't do a length check.159*/160static inline161struct fc_frame_header *__fc_frame_header_get(const struct fc_frame *fp)162{163return (struct fc_frame_header *)fr_hdr(fp);164}165166/*167* Get frame header from message in fc_frame structure.168* This hides a cast and provides a place to add some checking.169*/170static inline171struct fc_frame_header *fc_frame_header_get(const struct fc_frame *fp)172{173WARN_ON(fr_len(fp) < sizeof(struct fc_frame_header));174return __fc_frame_header_get(fp);175}176177/*178* Get source FC_ID (S_ID) from frame header in message.179*/180static inline u32 fc_frame_sid(const struct fc_frame *fp)181{182return ntoh24(__fc_frame_header_get(fp)->fh_s_id);183}184185/*186* Get destination FC_ID (D_ID) from frame header in message.187*/188static inline u32 fc_frame_did(const struct fc_frame *fp)189{190return ntoh24(__fc_frame_header_get(fp)->fh_d_id);191}192193/*194* Get frame payload from message in fc_frame structure.195* This hides a cast and provides a place to add some checking.196* The len parameter is the minimum length for the payload portion.197* Returns NULL if the frame is too short.198*199* This assumes the interesting part of the payload is in the first part200* of the buffer for received data. This may not be appropriate to use for201* buffers being transmitted.202*/203static inline void *fc_frame_payload_get(const struct fc_frame *fp,204size_t len)205{206void *pp = NULL;207208if (fr_len(fp) >= sizeof(struct fc_frame_header) + len)209pp = fc_frame_header_get(fp) + 1;210return pp;211}212213/*214* Get frame payload opcode (first byte) from message in fc_frame structure.215* This hides a cast and provides a place to add some checking. Return 0216* if the frame has no payload.217*/218static inline u8 fc_frame_payload_op(const struct fc_frame *fp)219{220u8 *cp;221222cp = fc_frame_payload_get(fp, sizeof(u8));223if (!cp)224return 0;225return *cp;226227}228229/*230* Get FC class from frame.231*/232static inline enum fc_class fc_frame_class(const struct fc_frame *fp)233{234return fc_sof_class(fr_sof(fp));235}236237/*238* Check the CRC in a frame.239* The CRC immediately follows the last data item *AFTER* the length.240* The return value is zero if the CRC matches.241*/242u32 fc_frame_crc_check(struct fc_frame *);243244static inline u8 fc_frame_rctl(const struct fc_frame *fp)245{246return fc_frame_header_get(fp)->fh_r_ctl;247}248249static inline bool fc_frame_is_cmd(const struct fc_frame *fp)250{251return fc_frame_rctl(fp) == FC_RCTL_DD_UNSOL_CMD;252}253254/*255* Check for leaks.256* Print the frame header of any currently allocated frame, assuming there257* should be none at this point.258*/259void fc_frame_leak_check(void);260261#endif /* _FC_FRAME_H_ */262263264