Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright(c) 2007 Intel Corporation. All rights reserved.
0004  *
0005  * Maintained at www.Open-FCoE.org
0006  */
0007 
0008 /*
0009  * Frame allocation.
0010  */
0011 #include <linux/module.h>
0012 #include <linux/kernel.h>
0013 #include <linux/skbuff.h>
0014 #include <linux/crc32.h>
0015 #include <linux/gfp.h>
0016 
0017 #include <scsi/fc_frame.h>
0018 
0019 /*
0020  * Check the CRC in a frame.
0021  */
0022 u32 fc_frame_crc_check(struct fc_frame *fp)
0023 {
0024     u32 crc;
0025     u32 error;
0026     const u8 *bp;
0027     unsigned int len;
0028 
0029     WARN_ON(!fc_frame_is_linear(fp));
0030     fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
0031     len = (fr_len(fp) + 3) & ~3;    /* round up length to include fill */
0032     bp = (const u8 *) fr_hdr(fp);
0033     crc = ~crc32(~0, bp, len);
0034     error = crc ^ fr_crc(fp);
0035     return error;
0036 }
0037 EXPORT_SYMBOL(fc_frame_crc_check);
0038 
0039 /*
0040  * Allocate a frame intended to be sent.
0041  * Get an sk_buff for the frame and set the length.
0042  */
0043 struct fc_frame *_fc_frame_alloc(size_t len)
0044 {
0045     struct fc_frame *fp;
0046     struct sk_buff *skb;
0047 
0048     WARN_ON((len % sizeof(u32)) != 0);
0049     len += sizeof(struct fc_frame_header);
0050     skb = alloc_skb_fclone(len + FC_FRAME_HEADROOM + FC_FRAME_TAILROOM +
0051                    NET_SKB_PAD, GFP_ATOMIC);
0052     if (!skb)
0053         return NULL;
0054     skb_reserve(skb, NET_SKB_PAD + FC_FRAME_HEADROOM);
0055     fp = (struct fc_frame *) skb;
0056     fc_frame_init(fp);
0057     skb_put(skb, len);
0058     return fp;
0059 }
0060 EXPORT_SYMBOL(_fc_frame_alloc);
0061 
0062 struct fc_frame *fc_frame_alloc_fill(struct fc_lport *lp, size_t payload_len)
0063 {
0064     struct fc_frame *fp;
0065     size_t fill;
0066 
0067     fill = payload_len % 4;
0068     if (fill != 0)
0069         fill = 4 - fill;
0070     fp = _fc_frame_alloc(payload_len + fill);
0071     if (fp) {
0072         memset((char *) fr_hdr(fp) + payload_len, 0, fill);
0073         /* trim is OK, we just allocated it so there are no fragments */
0074         skb_trim(fp_skb(fp),
0075              payload_len + sizeof(struct fc_frame_header));
0076     }
0077     return fp;
0078 }
0079 EXPORT_SYMBOL(fc_frame_alloc_fill);