2 * Copyright(c) 2009 Intel Corporation. All rights reserved.
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.
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
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.
17 * Maintained at www.Open-FCoE.org
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/scatterlist.h>
23 #include <linux/crc32.h>
25 #include <scsi/libfc.h>
29 MODULE_AUTHOR("Open-FCoE.org");
30 MODULE_DESCRIPTION("libfc");
31 MODULE_LICENSE("GPL v2");
33 unsigned int fc_debug_logging
;
34 module_param_named(debug_logging
, fc_debug_logging
, int, S_IRUGO
|S_IWUSR
);
35 MODULE_PARM_DESC(debug_logging
, "a bit mask of logging levels");
38 * libfc_init() - Initialize libfc.ko
40 static int __init
libfc_init(void)
48 rc
= fc_setup_exch_mgr();
50 goto destroy_pkt_cache
;
52 rc
= fc_setup_rport();
58 fc_destroy_exch_mgr();
63 module_init(libfc_init
);
66 * libfc_exit() - Tear down libfc.ko
68 static void __exit
libfc_exit(void)
71 fc_destroy_exch_mgr();
74 module_exit(libfc_exit
);
77 * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
78 * into a scatter-gather list (SG list).
80 * @buf: pointer to the data buffer.
81 * @len: the byte-length of the data buffer.
82 * @sg: pointer to the pointer of the SG list.
83 * @nents: pointer to the remaining number of entries in the SG list.
84 * @offset: pointer to the current offset in the SG list.
85 * @km_type: dedicated page table slot type for kmap_atomic.
86 * @crc: pointer to the 32-bit crc value.
87 * If crc is NULL, CRC is not calculated.
89 u32
fc_copy_buffer_to_sglist(void *buf
, size_t len
,
90 struct scatterlist
*sg
,
91 u32
*nents
, size_t *offset
,
92 enum km_type km_type
, u32
*crc
)
94 size_t remaining
= len
;
97 while (remaining
> 0 && sg
) {
101 if (*offset
>= sg
->length
) {
103 * Check for end and drop resources
104 * from the last iteration.
109 *offset
-= sg
->length
;
113 sg_bytes
= min(remaining
, sg
->length
- *offset
);
116 * The scatterlist item may be bigger than PAGE_SIZE,
117 * but we are limited to mapping PAGE_SIZE at a time.
119 off
= *offset
+ sg
->offset
;
120 sg_bytes
= min(sg_bytes
,
121 (size_t)(PAGE_SIZE
- (off
& ~PAGE_MASK
)));
122 page_addr
= kmap_atomic(sg_page(sg
) + (off
>> PAGE_SHIFT
),
125 *crc
= crc32(*crc
, buf
, sg_bytes
);
126 memcpy((char *)page_addr
+ (off
& ~PAGE_MASK
), buf
, sg_bytes
);
127 kunmap_atomic(page_addr
, km_type
);
130 remaining
-= sg_bytes
;
131 copy_len
+= sg_bytes
;