2 * Copyright(c) 2007 Intel Corporation. All rights reserved.
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.
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
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.
17 * Maintained at www.Open-FCoE.org
23 #include <linux/scatterlist.h>
24 #include <linux/skbuff.h>
25 #include <scsi/scsi_cmnd.h>
27 #include <scsi/fc/fc_fs.h>
28 #include <scsi/fc/fc_fcp.h>
29 #include <scsi/fc/fc_encaps.h>
31 #include <linux/if_ether.h>
34 * The fc_frame interface is used to pass frame data between functions.
35 * The frame includes the data buffer, length, and SOF / EOF delimiter types.
36 * A pointer to the port structure of the receiving port is also includeded.
39 #define FC_FRAME_HEADROOM 32 /* headroom for VLAN + FCoE headers */
40 #define FC_FRAME_TAILROOM 8 /* trailer space for FCoE */
42 /* Max number of skb frags allowed, reserving one for fcoe_crc_eof page */
43 #define FC_FRAME_SG_LEN (MAX_SKB_FRAGS - 1)
45 #define fp_skb(fp) (&((fp)->skb))
46 #define fr_hdr(fp) ((fp)->skb.data)
47 #define fr_len(fp) ((fp)->skb.len)
48 #define fr_cb(fp) ((struct fcoe_rcv_info *)&((fp)->skb.cb[0]))
49 #define fr_dev(fp) (fr_cb(fp)->fr_dev)
50 #define fr_seq(fp) (fr_cb(fp)->fr_seq)
51 #define fr_sof(fp) (fr_cb(fp)->fr_sof)
52 #define fr_eof(fp) (fr_cb(fp)->fr_eof)
53 #define fr_flags(fp) (fr_cb(fp)->fr_flags)
54 #define fr_max_payload(fp) (fr_cb(fp)->fr_max_payload)
55 #define fr_fsp(fp) (fr_cb(fp)->fr_fsp)
56 #define fr_crc(fp) (fr_cb(fp)->fr_crc)
62 struct fcoe_rcv_info
{
63 struct packet_type
*ptype
;
64 struct fc_lport
*fr_dev
; /* transport layer private pointer */
65 struct fc_seq
*fr_seq
; /* for use with exchange manager */
66 struct fc_fcp_pkt
*fr_fsp
; /* for the corresponding fcp I/O */
68 u16 fr_max_payload
; /* max FC payload */
69 enum fc_sof fr_sof
; /* start of frame delimiter */
70 enum fc_eof fr_eof
; /* end of frame delimiter */
71 u8 fr_flags
; /* flags - see below */
72 u8 granted_mac
[ETH_ALEN
]; /* FCoE MAC address */
77 * Get fc_frame pointer for an skb that's already been imported.
79 static inline struct fcoe_rcv_info
*fcoe_dev_from_skb(const struct sk_buff
*skb
)
81 BUILD_BUG_ON(sizeof(struct fcoe_rcv_info
) > sizeof(skb
->cb
));
82 return (struct fcoe_rcv_info
*) skb
->cb
;
88 #define FCPHF_CRC_UNCHECKED 0x01 /* CRC not computed, still appended */
92 * We don't do a complete memset here for performance reasons.
93 * The caller must set fr_free, fr_hdr, fr_len, fr_sof, and fr_eof eventually.
95 static inline void fc_frame_init(struct fc_frame
*fp
)
102 struct fc_frame
*fc_frame_alloc_fill(struct fc_lport
*, size_t payload_len
);
103 struct fc_frame
*_fc_frame_alloc(size_t payload_len
);
106 * Allocate fc_frame structure and buffer. Set the initial length to
107 * payload_size + sizeof (struct fc_frame_header).
109 static inline struct fc_frame
*fc_frame_alloc(struct fc_lport
*dev
, size_t len
)
114 * Note: Since len will often be a constant multiple of 4,
115 * this check will usually be evaluated and eliminated at compile time.
118 fp
= fc_frame_alloc_fill(dev
, len
);
120 fp
= _fc_frame_alloc(len
);
125 * Free the fc_frame structure and buffer.
127 static inline void fc_frame_free(struct fc_frame
*fp
)
129 kfree_skb(fp_skb(fp
));
132 static inline int fc_frame_is_linear(struct fc_frame
*fp
)
134 return !skb_is_nonlinear(fp_skb(fp
));
138 * Get frame header from message in fc_frame structure.
139 * This hides a cast and provides a place to add some checking.
142 struct fc_frame_header
*fc_frame_header_get(const struct fc_frame
*fp
)
144 WARN_ON(fr_len(fp
) < sizeof(struct fc_frame_header
));
145 return (struct fc_frame_header
*) fr_hdr(fp
);
149 * Get frame payload from message in fc_frame structure.
150 * This hides a cast and provides a place to add some checking.
151 * The len parameter is the minimum length for the payload portion.
152 * Returns NULL if the frame is too short.
154 * This assumes the interesting part of the payload is in the first part
155 * of the buffer for received data. This may not be appropriate to use for
156 * buffers being transmitted.
158 static inline void *fc_frame_payload_get(const struct fc_frame
*fp
,
163 if (fr_len(fp
) >= sizeof(struct fc_frame_header
) + len
)
164 pp
= fc_frame_header_get(fp
) + 1;
169 * Get frame payload opcode (first byte) from message in fc_frame structure.
170 * This hides a cast and provides a place to add some checking. Return 0
171 * if the frame has no payload.
173 static inline u8
fc_frame_payload_op(const struct fc_frame
*fp
)
177 cp
= fc_frame_payload_get(fp
, sizeof(u8
));
185 * Get FC class from frame.
187 static inline enum fc_class
fc_frame_class(const struct fc_frame
*fp
)
189 return fc_sof_class(fr_sof(fp
));
193 * Check the CRC in a frame.
194 * The CRC immediately follows the last data item *AFTER* the length.
195 * The return value is zero if the CRC matches.
197 u32
fc_frame_crc_check(struct fc_frame
*);
199 static inline u8
fc_frame_rctl(const struct fc_frame
*fp
)
201 return fc_frame_header_get(fp
)->fh_r_ctl
;
204 static inline bool fc_frame_is_cmd(const struct fc_frame
*fp
)
206 return fc_frame_rctl(fp
) == FC_RCTL_DD_UNSOL_CMD
;
211 * Print the frame header of any currently allocated frame, assuming there
212 * should be none at this point.
214 void fc_frame_leak_check(void);
216 #endif /* _FC_FRAME_H_ */