xref: /linux/drivers/scsi/libfc/fc_libfc.c (revision 273b281fa22c293963ee3e6eec418f5dda2dbc83)
1 /*
2  * Copyright(c) 2009 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 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/scatterlist.h>
23 #include <linux/crc32.h>
24 
25 #include <scsi/libfc.h>
26 
27 #include "fc_libfc.h"
28 
29 MODULE_AUTHOR("Open-FCoE.org");
30 MODULE_DESCRIPTION("libfc");
31 MODULE_LICENSE("GPL v2");
32 
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");
36 
37 /**
38  * libfc_init() - Initialize libfc.ko
39  */
40 static int __init libfc_init(void)
41 {
42 	int rc = 0;
43 
44 	rc = fc_setup_fcp();
45 	if (rc)
46 		return rc;
47 
48 	rc = fc_setup_exch_mgr();
49 	if (rc)
50 		goto destroy_pkt_cache;
51 
52 	rc = fc_setup_rport();
53 	if (rc)
54 		goto destroy_em;
55 
56 	return rc;
57 destroy_em:
58 	fc_destroy_exch_mgr();
59 destroy_pkt_cache:
60 	fc_destroy_fcp();
61 	return rc;
62 }
63 module_init(libfc_init);
64 
65 /**
66  * libfc_exit() - Tear down libfc.ko
67  */
68 static void __exit libfc_exit(void)
69 {
70 	fc_destroy_fcp();
71 	fc_destroy_exch_mgr();
72 	fc_destroy_rport();
73 }
74 module_exit(libfc_exit);
75 
76 /**
77  * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
78  *				into a scatter-gather list (SG list).
79  *
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.
88  */
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)
93 {
94 	size_t remaining = len;
95 	u32 copy_len = 0;
96 
97 	while (remaining > 0 && sg) {
98 		size_t off, sg_bytes;
99 		void *page_addr;
100 
101 		if (*offset >= sg->length) {
102 			/*
103 			 * Check for end and drop resources
104 			 * from the last iteration.
105 			 */
106 			if (!(*nents))
107 				break;
108 			--(*nents);
109 			*offset -= sg->length;
110 			sg = sg_next(sg);
111 			continue;
112 		}
113 		sg_bytes = min(remaining, sg->length - *offset);
114 
115 		/*
116 		 * The scatterlist item may be bigger than PAGE_SIZE,
117 		 * but we are limited to mapping PAGE_SIZE at a time.
118 		 */
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),
123 					km_type);
124 		if (crc)
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);
128 		buf += sg_bytes;
129 		*offset += sg_bytes;
130 		remaining -= sg_bytes;
131 		copy_len += sg_bytes;
132 	}
133 	return copy_len;
134 }
135