blob: 6da01c6169641ee882df5f56359b474485abef30 [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/*
21 * Frame allocation.
22 */
23#include <linux/module.h>
24#include <linux/kernel.h>
25#include <linux/skbuff.h>
26#include <linux/crc32.h>
27
28#include <scsi/fc_frame.h>
29
30/*
31 * Check the CRC in a frame.
32 */
33u32 fc_frame_crc_check(struct fc_frame *fp)
34{
35 u32 crc;
36 u32 error;
37 const u8 *bp;
38 unsigned int len;
39
40 WARN_ON(!fc_frame_is_linear(fp));
41 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
42 len = (fr_len(fp) + 3) & ~3; /* round up length to include fill */
43 bp = (const u8 *) fr_hdr(fp);
44 crc = ~crc32(~0, bp, len);
45 error = crc ^ fr_crc(fp);
46 return error;
47}
48EXPORT_SYMBOL(fc_frame_crc_check);
49
50/*
51 * Allocate a frame intended to be sent via fcoe_xmit.
52 * Get an sk_buff for the frame and set the length.
53 */
Vasu Deva7bbc7f2009-11-03 11:47:55 -080054struct fc_frame *_fc_frame_alloc(size_t len)
Robert Love42e9a922008-12-09 15:10:17 -080055{
56 struct fc_frame *fp;
57 struct sk_buff *skb;
58
59 WARN_ON((len % sizeof(u32)) != 0);
60 len += sizeof(struct fc_frame_header);
Chris Leech18fa11e2009-11-03 11:50:05 -080061 skb = alloc_skb_fclone(len + FC_FRAME_HEADROOM + FC_FRAME_TAILROOM +
62 NET_SKB_PAD, GFP_ATOMIC);
Robert Love42e9a922008-12-09 15:10:17 -080063 if (!skb)
64 return NULL;
Chris Leech18fa11e2009-11-03 11:50:05 -080065 skb_reserve(skb, NET_SKB_PAD + FC_FRAME_HEADROOM);
Robert Love42e9a922008-12-09 15:10:17 -080066 fp = (struct fc_frame *) skb;
67 fc_frame_init(fp);
Robert Love42e9a922008-12-09 15:10:17 -080068 skb_put(skb, len);
69 return fp;
70}
Vasu Deva7bbc7f2009-11-03 11:47:55 -080071EXPORT_SYMBOL(_fc_frame_alloc);
Robert Love42e9a922008-12-09 15:10:17 -080072
Robert Love42e9a922008-12-09 15:10:17 -080073struct fc_frame *fc_frame_alloc_fill(struct fc_lport *lp, size_t payload_len)
74{
75 struct fc_frame *fp;
76 size_t fill;
77
78 fill = payload_len % 4;
79 if (fill != 0)
80 fill = 4 - fill;
Vasu Deva7bbc7f2009-11-03 11:47:55 -080081 fp = _fc_frame_alloc(payload_len + fill);
Robert Love42e9a922008-12-09 15:10:17 -080082 if (fp) {
83 memset((char *) fr_hdr(fp) + payload_len, 0, fill);
84 /* trim is OK, we just allocated it so there are no fragments */
85 skb_trim(fp_skb(fp),
86 payload_len + sizeof(struct fc_frame_header));
87 }
88 return fp;
89}
Chris Leechdc8596d2009-11-03 11:47:18 -080090EXPORT_SYMBOL(fc_frame_alloc_fill);