Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright(c) 2007 Intel Corporation. All rights reserved.
4 *
5 * Maintained at www.Open-FCoE.org
6 */
7
8/*
9 * Frame allocation.
10 */
11#include <linux/module.h>
12#include <linux/kernel.h>
13#include <linux/skbuff.h>
14#include <linux/crc32.h>
15#include <linux/gfp.h>
16
17#include <scsi/fc_frame.h>
18
19/*
20 * Check the CRC in a frame.
21 */
22u32 fc_frame_crc_check(struct fc_frame *fp)
23{
24 u32 crc;
25 u32 error;
26 const u8 *bp;
27 unsigned int len;
28
29 WARN_ON(!fc_frame_is_linear(fp));
30 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
31 len = (fr_len(fp) + 3) & ~3; /* round up length to include fill */
32 bp = (const u8 *) fr_hdr(fp);
33 crc = ~crc32(~0, bp, len);
34 error = crc ^ fr_crc(fp);
35 return error;
36}
37EXPORT_SYMBOL(fc_frame_crc_check);
38
39/*
40 * Allocate a frame intended to be sent.
41 * Get an sk_buff for the frame and set the length.
42 */
43struct fc_frame *_fc_frame_alloc(size_t len)
44{
45 struct fc_frame *fp;
46 struct sk_buff *skb;
47
48 WARN_ON((len % sizeof(u32)) != 0);
49 len += sizeof(struct fc_frame_header);
50 skb = alloc_skb_fclone(len + FC_FRAME_HEADROOM + FC_FRAME_TAILROOM +
51 NET_SKB_PAD, GFP_ATOMIC);
52 if (!skb)
53 return NULL;
54 skb_reserve(skb, NET_SKB_PAD + FC_FRAME_HEADROOM);
55 fp = (struct fc_frame *) skb;
56 fc_frame_init(fp);
57 skb_put(skb, len);
58 return fp;
59}
60EXPORT_SYMBOL(_fc_frame_alloc);
61
62struct fc_frame *fc_frame_alloc_fill(struct fc_lport *lp, size_t payload_len)
63{
64 struct fc_frame *fp;
65 size_t fill;
66
67 fill = payload_len % 4;
68 if (fill != 0)
69 fill = 4 - fill;
70 fp = _fc_frame_alloc(payload_len + fill);
71 if (fp) {
72 memset((char *) fr_hdr(fp) + payload_len, 0, fill);
73 /* trim is OK, we just allocated it so there are no fragments */
74 skb_trim(fp_skb(fp),
75 payload_len + sizeof(struct fc_frame_header));
76 }
77 return fp;
78}
79EXPORT_SYMBOL(fc_frame_alloc_fill);
1/*
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#include <linux/gfp.h>
28
29#include <scsi/fc_frame.h>
30
31/*
32 * Check the CRC in a frame.
33 */
34u32 fc_frame_crc_check(struct fc_frame *fp)
35{
36 u32 crc;
37 u32 error;
38 const u8 *bp;
39 unsigned int len;
40
41 WARN_ON(!fc_frame_is_linear(fp));
42 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
43 len = (fr_len(fp) + 3) & ~3; /* round up length to include fill */
44 bp = (const u8 *) fr_hdr(fp);
45 crc = ~crc32(~0, bp, len);
46 error = crc ^ fr_crc(fp);
47 return error;
48}
49EXPORT_SYMBOL(fc_frame_crc_check);
50
51/*
52 * Allocate a frame intended to be sent.
53 * Get an sk_buff for the frame and set the length.
54 */
55struct fc_frame *_fc_frame_alloc(size_t len)
56{
57 struct fc_frame *fp;
58 struct sk_buff *skb;
59
60 WARN_ON((len % sizeof(u32)) != 0);
61 len += sizeof(struct fc_frame_header);
62 skb = alloc_skb_fclone(len + FC_FRAME_HEADROOM + FC_FRAME_TAILROOM +
63 NET_SKB_PAD, GFP_ATOMIC);
64 if (!skb)
65 return NULL;
66 skb_reserve(skb, NET_SKB_PAD + FC_FRAME_HEADROOM);
67 fp = (struct fc_frame *) skb;
68 fc_frame_init(fp);
69 skb_put(skb, len);
70 return fp;
71}
72EXPORT_SYMBOL(_fc_frame_alloc);
73
74struct fc_frame *fc_frame_alloc_fill(struct fc_lport *lp, size_t payload_len)
75{
76 struct fc_frame *fp;
77 size_t fill;
78
79 fill = payload_len % 4;
80 if (fill != 0)
81 fill = 4 - fill;
82 fp = _fc_frame_alloc(payload_len + fill);
83 if (fp) {
84 memset((char *) fr_hdr(fp) + payload_len, 0, fill);
85 /* trim is OK, we just allocated it so there are no fragments */
86 skb_trim(fp_skb(fp),
87 payload_len + sizeof(struct fc_frame_header));
88 }
89 return fp;
90}
91EXPORT_SYMBOL(fc_frame_alloc_fill);