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