]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/scsi/libfc/fc_libfc.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[mv-sheeva.git] / drivers / scsi / libfc / fc_libfc.c
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 #include <scsi/fc_encode.h>
27
28 #include "fc_libfc.h"
29
30 MODULE_AUTHOR("Open-FCoE.org");
31 MODULE_DESCRIPTION("libfc");
32 MODULE_LICENSE("GPL v2");
33
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");
37
38 /**
39  * libfc_init() - Initialize libfc.ko
40  */
41 static int __init libfc_init(void)
42 {
43         int rc = 0;
44
45         rc = fc_setup_fcp();
46         if (rc)
47                 return rc;
48
49         rc = fc_setup_exch_mgr();
50         if (rc)
51                 goto destroy_pkt_cache;
52
53         rc = fc_setup_rport();
54         if (rc)
55                 goto destroy_em;
56
57         return rc;
58 destroy_em:
59         fc_destroy_exch_mgr();
60 destroy_pkt_cache:
61         fc_destroy_fcp();
62         return rc;
63 }
64 module_init(libfc_init);
65
66 /**
67  * libfc_exit() - Tear down libfc.ko
68  */
69 static void __exit libfc_exit(void)
70 {
71         fc_destroy_fcp();
72         fc_destroy_exch_mgr();
73         fc_destroy_rport();
74 }
75 module_exit(libfc_exit);
76
77 /**
78  * fc_copy_buffer_to_sglist() - This routine copies the data of a buffer
79  *                              into a scatter-gather list (SG list).
80  *
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.
89  */
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)
94 {
95         size_t remaining = len;
96         u32 copy_len = 0;
97
98         while (remaining > 0 && sg) {
99                 size_t off, sg_bytes;
100                 void *page_addr;
101
102                 if (*offset >= sg->length) {
103                         /*
104                          * Check for end and drop resources
105                          * from the last iteration.
106                          */
107                         if (!(*nents))
108                                 break;
109                         --(*nents);
110                         *offset -= sg->length;
111                         sg = sg_next(sg);
112                         continue;
113                 }
114                 sg_bytes = min(remaining, sg->length - *offset);
115
116                 /*
117                  * The scatterlist item may be bigger than PAGE_SIZE,
118                  * but we are limited to mapping PAGE_SIZE at a time.
119                  */
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),
124                                         km_type);
125                 if (crc)
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);
129                 buf += sg_bytes;
130                 *offset += sg_bytes;
131                 remaining -= sg_bytes;
132                 copy_len += sg_bytes;
133         }
134         return copy_len;
135 }
136
137 /**
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
145  */
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)
148 {
149         struct fc_frame_header *fh;
150         struct fc_frame_header *in_fh;
151         struct fc_seq *sp;
152         u32 fill;
153
154         fh = __fc_frame_header_get(fp);
155         in_fh = __fc_frame_header_get(in_fp);
156
157         if (f_ctl & FC_FC_END_SEQ) {
158                 fill = -fr_len(fp) & 3;
159                 if (fill) {
160                         /* TODO, this may be a problem with fragmented skb */
161                         memset(skb_put(fp_skb(fp), fill), 0, fill);
162                         f_ctl |= fill;
163                 }
164                 fr_eof(fp) = FC_EOF_T;
165         } else {
166                 WARN_ON(fr_len(fp) % 4 != 0);   /* no pad to non last frame */
167                 fr_eof(fp) = FC_EOF_N;
168         }
169
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;
177         fh->fh_cs_ctl = 0;
178         fh->fh_df_ctl = 0;
179         fh->fh_parm_offset = htonl(parm_offset);
180
181         sp = fr_seq(in_fp);
182         if (sp) {
183                 fr_seq(fp) = sp;
184                 fh->fh_seq_id = sp->id;
185                 seq_cnt = sp->cnt;
186         } else {
187                 fh->fh_seq_id = 0;
188         }
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);
192 }
193 EXPORT_SYMBOL(fc_fill_hdr);
194
195 /**
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
201  */
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)
204 {
205         struct fc_seq *sp;
206
207         sp = fr_seq(in_fp);
208         if (sp)
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);
211 }
212 EXPORT_SYMBOL(fc_fill_reply_hdr);