Blame view

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