blob: 29dd97d5b53a916d00da624a6ad5498e2d7bc115 [file] [log] [blame]
Robert Love42e9a922008-12-09 15:10:17 -08001/*
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
Chris Leech11b56182009-11-03 11:46:29 -080031#include <linux/if_ether.h>
32
Robert Love42e9a922008-12-09 15:10:17 -080033/*
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.
37 */
38
39#define FC_FRAME_HEADROOM 32 /* headroom for VLAN + FCoE headers */
40#define FC_FRAME_TAILROOM 8 /* trailer space for FCoE */
41
Yi Zoud37322a2009-10-21 16:27:58 -070042/* Max number of skb frags allowed, reserving one for fcoe_crc_eof page */
43#define FC_FRAME_SG_LEN (MAX_SKB_FRAGS - 1)
44
Robert Love42e9a922008-12-09 15:10:17 -080045#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)
Joe Eykholtf60e12e2010-07-20 15:20:14 -070054#define fr_encaps(fp) (fr_cb(fp)->fr_encaps)
Robert Love42e9a922008-12-09 15:10:17 -080055#define fr_max_payload(fp) (fr_cb(fp)->fr_max_payload)
Yi Zoub277d2a2009-02-27 14:07:21 -080056#define fr_fsp(fp) (fr_cb(fp)->fr_fsp)
Robert Love42e9a922008-12-09 15:10:17 -080057#define fr_crc(fp) (fr_cb(fp)->fr_crc)
58
59struct fc_frame {
60 struct sk_buff skb;
61};
62
63struct fcoe_rcv_info {
64 struct packet_type *ptype;
65 struct fc_lport *fr_dev; /* transport layer private pointer */
66 struct fc_seq *fr_seq; /* for use with exchange manager */
Yi Zoub277d2a2009-02-27 14:07:21 -080067 struct fc_fcp_pkt *fr_fsp; /* for the corresponding fcp I/O */
Robert Love42e9a922008-12-09 15:10:17 -080068 u32 fr_crc;
69 u16 fr_max_payload; /* max FC payload */
Bart Van Assched058fd32010-07-20 15:19:15 -070070 u8 fr_sof; /* start of frame delimiter */
71 u8 fr_eof; /* end of frame delimiter */
Robert Love42e9a922008-12-09 15:10:17 -080072 u8 fr_flags; /* flags - see below */
Joe Eykholtf60e12e2010-07-20 15:20:14 -070073 u8 fr_encaps; /* LLD encapsulation info (e.g. FIP) */
Chris Leech11b56182009-11-03 11:46:29 -080074 u8 granted_mac[ETH_ALEN]; /* FCoE MAC address */
Robert Love42e9a922008-12-09 15:10:17 -080075};
76
77
78/*
79 * Get fc_frame pointer for an skb that's already been imported.
80 */
81static inline struct fcoe_rcv_info *fcoe_dev_from_skb(const struct sk_buff *skb)
82{
83 BUILD_BUG_ON(sizeof(struct fcoe_rcv_info) > sizeof(skb->cb));
84 return (struct fcoe_rcv_info *) skb->cb;
85}
86
87/*
88 * fr_flags.
89 */
90#define FCPHF_CRC_UNCHECKED 0x01 /* CRC not computed, still appended */
91
92/*
93 * Initialize a frame.
94 * We don't do a complete memset here for performance reasons.
95 * The caller must set fr_free, fr_hdr, fr_len, fr_sof, and fr_eof eventually.
96 */
97static inline void fc_frame_init(struct fc_frame *fp)
98{
99 fr_dev(fp) = NULL;
100 fr_seq(fp) = NULL;
101 fr_flags(fp) = 0;
Joe Eykholtf60e12e2010-07-20 15:20:14 -0700102 fr_encaps(fp) = 0;
Robert Love42e9a922008-12-09 15:10:17 -0800103}
104
105struct fc_frame *fc_frame_alloc_fill(struct fc_lport *, size_t payload_len);
Vasu Deva7bbc7f2009-11-03 11:47:55 -0800106struct fc_frame *_fc_frame_alloc(size_t payload_len);
Robert Love42e9a922008-12-09 15:10:17 -0800107
108/*
109 * Allocate fc_frame structure and buffer. Set the initial length to
110 * payload_size + sizeof (struct fc_frame_header).
111 */
112static inline struct fc_frame *fc_frame_alloc(struct fc_lport *dev, size_t len)
113{
114 struct fc_frame *fp;
115
116 /*
117 * Note: Since len will often be a constant multiple of 4,
118 * this check will usually be evaluated and eliminated at compile time.
119 */
Vasu Deva7bbc7f2009-11-03 11:47:55 -0800120 if (len && len % 4)
Robert Love42e9a922008-12-09 15:10:17 -0800121 fp = fc_frame_alloc_fill(dev, len);
122 else
Vasu Deva7bbc7f2009-11-03 11:47:55 -0800123 fp = _fc_frame_alloc(len);
Robert Love42e9a922008-12-09 15:10:17 -0800124 return fp;
125}
126
127/*
128 * Free the fc_frame structure and buffer.
129 */
130static inline void fc_frame_free(struct fc_frame *fp)
131{
132 kfree_skb(fp_skb(fp));
133}
134
135static inline int fc_frame_is_linear(struct fc_frame *fp)
136{
137 return !skb_is_nonlinear(fp_skb(fp));
138}
139
140/*
141 * Get frame header from message in fc_frame structure.
142 * This hides a cast and provides a place to add some checking.
143 */
144static inline
145struct fc_frame_header *fc_frame_header_get(const struct fc_frame *fp)
146{
147 WARN_ON(fr_len(fp) < sizeof(struct fc_frame_header));
148 return (struct fc_frame_header *) fr_hdr(fp);
149}
150
151/*
152 * Get frame payload from message in fc_frame structure.
153 * This hides a cast and provides a place to add some checking.
154 * The len parameter is the minimum length for the payload portion.
155 * Returns NULL if the frame is too short.
156 *
157 * This assumes the interesting part of the payload is in the first part
158 * of the buffer for received data. This may not be appropriate to use for
159 * buffers being transmitted.
160 */
161static inline void *fc_frame_payload_get(const struct fc_frame *fp,
162 size_t len)
163{
164 void *pp = NULL;
165
166 if (fr_len(fp) >= sizeof(struct fc_frame_header) + len)
167 pp = fc_frame_header_get(fp) + 1;
168 return pp;
169}
170
171/*
172 * Get frame payload opcode (first byte) from message in fc_frame structure.
173 * This hides a cast and provides a place to add some checking. Return 0
174 * if the frame has no payload.
175 */
176static inline u8 fc_frame_payload_op(const struct fc_frame *fp)
177{
178 u8 *cp;
179
180 cp = fc_frame_payload_get(fp, sizeof(u8));
181 if (!cp)
182 return 0;
183 return *cp;
184
185}
186
187/*
188 * Get FC class from frame.
189 */
190static inline enum fc_class fc_frame_class(const struct fc_frame *fp)
191{
192 return fc_sof_class(fr_sof(fp));
193}
194
195/*
196 * Check the CRC in a frame.
197 * The CRC immediately follows the last data item *AFTER* the length.
198 * The return value is zero if the CRC matches.
199 */
200u32 fc_frame_crc_check(struct fc_frame *);
201
202static inline u8 fc_frame_rctl(const struct fc_frame *fp)
203{
204 return fc_frame_header_get(fp)->fh_r_ctl;
205}
206
207static inline bool fc_frame_is_cmd(const struct fc_frame *fp)
208{
209 return fc_frame_rctl(fp) == FC_RCTL_DD_UNSOL_CMD;
210}
211
Robert Love42e9a922008-12-09 15:10:17 -0800212/*
213 * Check for leaks.
214 * Print the frame header of any currently allocated frame, assuming there
215 * should be none at this point.
216 */
217void fc_frame_leak_check(void);
218
219#endif /* _FC_FRAME_H_ */