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>
26 #include <scsi/fc_encode.h>
30 MODULE_AUTHOR("Open-FCoE.org");
31 MODULE_DESCRIPTION("libfc");
32 MODULE_LICENSE("GPL v2");
34 unsigned int fc_debug_logging
;
35 module_param_named(debug_logging
, fc_debug_logging
, int, S_IRUGO
|S_IWUSR
);
36 MODULE_PARM_DESC(debug_logging
, "a bit mask of logging levels");
39 * libfc_init() - Initialize libfc.ko
41 static int __init
libfc_init(void)
49 rc
= fc_setup_exch_mgr();
51 goto destroy_pkt_cache
;
53 rc
= fc_setup_rport();
59 fc_destroy_exch_mgr();
64 module_init(libfc_init
);
67 * libfc_exit() - Tear down libfc.ko
69 static void __exit
libfc_exit(void)
72 fc_destroy_exch_mgr();
75 module_exit(libfc_exit
);
78 * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
79 * into a scatter-gather list (SG list).
81 * @buf: pointer to the data buffer.
82 * @len: the byte-length of the data buffer.
83 * @sg: pointer to the pointer of the SG list.
84 * @nents: pointer to the remaining number of entries in the SG list.
85 * @offset: pointer to the current offset in the SG list.
86 * @km_type: dedicated page table slot type for kmap_atomic.
87 * @crc: pointer to the 32-bit crc value.
88 * If crc is NULL, CRC is not calculated.
90 u32
fc_copy_buffer_to_sglist(void *buf
, size_t len
,
91 struct scatterlist
*sg
,
92 u32
*nents
, size_t *offset
,
93 enum km_type km_type
, u32
*crc
)
95 size_t remaining
= len
;
98 while (remaining
> 0 && sg
) {
102 if (*offset
>= sg
->length
) {
104 * Check for end and drop resources
105 * from the last iteration.
110 *offset
-= sg
->length
;
114 sg_bytes
= min(remaining
, sg
->length
- *offset
);
117 * The scatterlist item may be bigger than PAGE_SIZE,
118 * but we are limited to mapping PAGE_SIZE at a time.
120 off
= *offset
+ sg
->offset
;
121 sg_bytes
= min(sg_bytes
,
122 (size_t)(PAGE_SIZE
- (off
& ~PAGE_MASK
)));
123 page_addr
= kmap_atomic(sg_page(sg
) + (off
>> PAGE_SHIFT
),
126 *crc
= crc32(*crc
, buf
, sg_bytes
);
127 memcpy((char *)page_addr
+ (off
& ~PAGE_MASK
), buf
, sg_bytes
);
128 kunmap_atomic(page_addr
, km_type
);
131 remaining
-= sg_bytes
;
132 copy_len
+= sg_bytes
;
138 * fc_fill_hdr() - fill FC header fields based on request
139 * @fp: reply frame containing header to be filled in
140 * @in_fp: request frame containing header to use in filling in reply
141 * @r_ctl: R_CTL value for header
142 * @f_ctl: F_CTL value for header, with 0 pad
143 * @seq_cnt: sequence count for the header, ignored if frame has a sequence
144 * @parm_offset: parameter / offset value
146 void fc_fill_hdr(struct fc_frame
*fp
, const struct fc_frame
*in_fp
,
147 enum fc_rctl r_ctl
, u32 f_ctl
, u16 seq_cnt
, u32 parm_offset
)
149 struct fc_frame_header
*fh
;
150 struct fc_frame_header
*in_fh
;
154 fh
= __fc_frame_header_get(fp
);
155 in_fh
= __fc_frame_header_get(in_fp
);
157 if (f_ctl
& FC_FC_END_SEQ
) {
158 fill
= -fr_len(fp
) & 3;
160 /* TODO, this may be a problem with fragmented skb */
161 memset(skb_put(fp_skb(fp
), fill
), 0, fill
);
164 fr_eof(fp
) = FC_EOF_T
;
166 WARN_ON(fr_len(fp
) % 4 != 0); /* no pad to non last frame */
167 fr_eof(fp
) = FC_EOF_N
;
170 fh
->fh_r_ctl
= r_ctl
;
171 memcpy(fh
->fh_d_id
, in_fh
->fh_s_id
, sizeof(fh
->fh_d_id
));
172 memcpy(fh
->fh_s_id
, in_fh
->fh_d_id
, sizeof(fh
->fh_s_id
));
173 fh
->fh_type
= in_fh
->fh_type
;
174 hton24(fh
->fh_f_ctl
, f_ctl
);
175 fh
->fh_ox_id
= in_fh
->fh_ox_id
;
176 fh
->fh_rx_id
= in_fh
->fh_rx_id
;
179 fh
->fh_parm_offset
= htonl(parm_offset
);
184 fh
->fh_seq_id
= sp
->id
;
189 fh
->fh_seq_cnt
= ntohs(seq_cnt
);
190 fr_sof(fp
) = seq_cnt
? FC_SOF_N3
: FC_SOF_I3
;
191 fr_encaps(fp
) = fr_encaps(in_fp
);
193 EXPORT_SYMBOL(fc_fill_hdr
);
196 * fc_fill_reply_hdr() - fill FC reply header fields based on request
197 * @fp: reply frame containing header to be filled in
198 * @in_fp: request frame containing header to use in filling in reply
199 * @r_ctl: R_CTL value for reply
200 * @parm_offset: parameter / offset value
202 void fc_fill_reply_hdr(struct fc_frame
*fp
, const struct fc_frame
*in_fp
,
203 enum fc_rctl r_ctl
, u32 parm_offset
)
209 fr_seq(fp
) = fr_dev(in_fp
)->tt
.seq_start_next(sp
);
210 fc_fill_hdr(fp
, in_fp
, r_ctl
, FC_FCTL_RESP
, 0, parm_offset
);
212 EXPORT_SYMBOL(fc_fill_reply_hdr
);